scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 292c2611dbb22bc78f2fea52eda82252f071491b
parent 9f0c6b1818a3dac48845572bb64c250673c41c88
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 25 Apr 2014 12:45:29 +0200

Move common functions to a separate library

These functions are going to be used in different
programs of this project, so the best place for them
is in i common library

Diffstat:
A.gitignore | 2++
AMakefile | 8++++++++
Mcc1/Makefile | 17+++++++----------
Mcc1/error.c | 10----------
Dcc1/wrapper.c | 51---------------------------------------------------
Rinclude/cc.h -> inc/cc.h | 0
Rinclude/sizes.h -> inc/sizes.h | 0
Alib/Makefile | 8++++++++
Alib/die.c | 16++++++++++++++++
Alib/xcalloc.c | 13+++++++++++++
Alib/xmalloc.c | 13+++++++++++++
Alib/xrealloc.c | 13+++++++++++++
Alib/xstrdup.c | 12++++++++++++
13 files changed, 92 insertions(+), 71 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,2 @@ +*.o +*.a diff --git a/Makefile b/Makefile @@ -0,0 +1,8 @@ + +DIRS = lib cc1 + +all clean: + for i in $(DIRS) ;\ + do \ + (cd $$i && $(MAKE) $@) ;\ + done diff --git a/cc1/Makefile b/cc1/Makefile @@ -1,22 +1,19 @@ OBJS = types.o decl.o lex.o error.o symbol.o main.o expr.o \ - wrapper.o code.o stmt.o + code.o stmt.o -CFLAGS += -I../include +CPPFLAGS = -I../inc +LDFLAGS = -L../lib +LIBS = -lcc all: cc1 -$(OBJS) : cc1.h +$(OBJS) : cc1.h ../inc/cc.h -cc1: $(OBJS) - $(CC) $(LDFLAGS) $(CFLAGS) $(LIBS) $(OBJS) -o $@ +cc1: $(OBJS) ../lib/libcc.a + $(CC) $(LDFLAGS) $(CFLAGS) $(OBJS) $(LIBS) -o $@ clean: rm -f $(OBJS) rm -f cc1 -distclean: clean - rm -f *~ - rm -f tags - rm -f cscope.* - rm -f makefile diff --git a/cc1/error.c b/cc1/error.c @@ -41,13 +41,3 @@ error(const char *fmt, ...) warn_helper(-1, fmt, va); va_end(va); } - -void -die(const char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - fprintf(stderr, fmt, va); - va_end(va); - exit(EXIT_FAILURE); -} diff --git a/cc1/wrapper.c b/cc1/wrapper.c @@ -1,51 +0,0 @@ - -#include <stdlib.h> -#include <string.h> -#include <stdint.h> - -#include <cc.h> - -static void -out_of_memory(void) -{ - /* TODO: deal with out of memory errors */ - error("out of memory"); -} - -void * -xmalloc(size_t size) -{ - register void *p = malloc(size); - - if (!p) - out_of_memory(); - return p; -} - -void * -xcalloc(size_t nmemb, size_t size) -{ - register size_t nbytes = nmemb * size; - register void *p = xmalloc(nbytes); - - return memset(p, nbytes, 0); -} - -char * -xstrdup(const char *s) -{ - register size_t len = strlen(s) + 1; - register char *p = xmalloc(len); - - return memcpy(p, s, len); -} - -void * -xrealloc(void *buff, register size_t size) -{ - register void *p = realloc(buff, size); - - if (!p) - out_of_memory(); - return p; -} diff --git a/include/cc.h b/inc/cc.h diff --git a/include/sizes.h b/inc/sizes.h diff --git a/lib/Makefile b/lib/Makefile @@ -0,0 +1,8 @@ + +OBJS = die.o xcalloc.o xmalloc.o xrealloc.o xstrdup.o +CPPFLAGS = -I../inc + +all: libcc.a($(OBJS)) + +clean: + rm -f *.o *.a diff --git a/lib/die.c b/lib/die.c @@ -0,0 +1,16 @@ + +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> + +#include <cc.h> + +void +die(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + fprintf(stderr, fmt, va); + va_end(va); + exit(EXIT_FAILURE); +} diff --git a/lib/xcalloc.c b/lib/xcalloc.c @@ -0,0 +1,13 @@ + +#include <stdlib.h> +#include <cc.h> + +void * +xcalloc(size_t n, size_t size) +{ + register void *p = calloc(n, size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/xmalloc.c b/lib/xmalloc.c @@ -0,0 +1,13 @@ + +#include <stdlib.h> +#include <cc.h> + +void * +xmalloc(size_t size) +{ + register void *p = malloc(size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/xrealloc.c b/lib/xrealloc.c @@ -0,0 +1,13 @@ + +#include <stdlib.h> +#include <cc.h> + +void * +xrealloc(void *buff, register size_t size) +{ + register void *p = realloc(buff, size); + + if (!p) + die("out of memory"); + return p; +} diff --git a/lib/xstrdup.c b/lib/xstrdup.c @@ -0,0 +1,12 @@ + +#include <string.h> +#include <cc.h> + +char * +xstrdup(const char *s) +{ + register size_t len = strlen(s) + 1; + register char *p = xmalloc(len); + + return memcpy(p, s, len); +}