scc

Simple C99 Compiler
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:
.gitignore | 2++
Makefile | 8++++++++
cc1/Makefile | 17+++++++----------
cc1/error.c | 10----------
cc1/wrapper.c | 51---------------------------------------------------
inc/cc.h | 16++++++++++++++++
inc/sizes.h | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
include/cc.h | 16----------------
include/sizes.h | 88-------------------------------------------------------------------------------
lib/Makefile | 8++++++++
lib/die.c | 16++++++++++++++++
lib/xcalloc.c | 13+++++++++++++
lib/xmalloc.c | 13+++++++++++++
lib/xrealloc.c | 13+++++++++++++
lib/xstrdup.c | 12++++++++++++
15 files changed, 196 insertions(+), 175 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/inc/cc.h b/inc/cc.h @@ -0,0 +1,15 @@ + +#ifndef CC_H_ +#define CC_H_ + +#ifndef __bool_true_and_false_defined +#include <stdbool.h> +#endif + +extern void die(const char *fmt, ...); +extern void *xmalloc(size_t size); +extern void *xcalloc(size_t nmemb, size_t size); +extern char *xstrdup(const char *s); +extern void *xrealloc(void *buff, register size_t size); + +#endif+ \ No newline at end of file diff --git a/inc/sizes.h b/inc/sizes.h @@ -0,0 +1,88 @@ +#ifndef SIZES_H +#define SIZES_H + +/* + * 15 nesting levels of compound statements, iteration control + * structures, and selection control structures + */ +#define NR_BLOCK 15 +/* + * 8 nesting levels of conditional inclusion + */ +#define NR_COND 8 +/* + * number of defined structs/unions in one translation unit + */ +#define NR_MAXSTRUCTS 127 +/* + * 12 pointer, array, and function declarators (in any combinations) + * modifying an arithmetic, a structure, a union, or an incomplete type + * in a declaration + */ +#define NR_DECLARATORS 12 +/* + * 31 declarators nested by parentheses within a full declarator. + */ +#define NR_SUBTYPE 31 +/* + * 32 expressions nested by parentheses within a full expression + */ +#define NR_SUBEXPR 32 +/* + * 31 significant initial characters in an internal identifier or a + * macro name + */ +#define IDENTSIZ 31 +/* + * 511 external identifiers in one translation unit + */ +#define NR_EXT_IDENT 511 +/* + * 127 identifiers with block scope declared in one block + */ +#define NR_INT_IDENT 127 +/* + * 31 parameters in one function definition * 6 significant initial + * characters in an external identifier. + */ +#define NR_FUNPARAM 31 +/* + * 31 parameters in one macro definition + */ +#define NR_MACROARG 31 +/* + * 509 characters in a logical source line. + */ +#define LINESIZ 509 +/* + * 509 characters in a character string literal or wide string literal + * (after concatenation) + */ +#define STRINGSIZ 509 +/* + * 8 nesting levels for #include'd files + */ +#define NR_INCLUDE 9 +/* + * 257 case labels for a switch statement (excluding those for any + * nested switch statements) + */ +#define NR_SWITCH 257 +/* + * 127 members in a single structure or union + */ +#define NR_FIELDS 127 +/* + * 127 enumeration constants in a single enumeration + */ +#define NR_ENUM_CTES 127 +/* + * 15 levels of nested structure or union definitions in a single + * struct-declaration-list + */ +#define NR_STRUCT_LEVEL 15 +/* + * 32767 bytes in an object (in a hosted environment only) + */ +#define OBJECTSIZ 32767 +#endif diff --git a/include/cc.h b/include/cc.h @@ -1,15 +0,0 @@ - -#ifndef CC_H_ -#define CC_H_ - -#ifndef __bool_true_and_false_defined -#include <stdbool.h> -#endif - -extern void die(const char *fmt, ...); -extern void *xmalloc(size_t size); -extern void *xcalloc(size_t nmemb, size_t size); -extern char *xstrdup(const char *s); -extern void *xrealloc(void *buff, register size_t size); - -#endif- \ No newline at end of file diff --git a/include/sizes.h b/include/sizes.h @@ -1,88 +0,0 @@ -#ifndef SIZES_H -#define SIZES_H - -/* - * 15 nesting levels of compound statements, iteration control - * structures, and selection control structures - */ -#define NR_BLOCK 15 -/* - * 8 nesting levels of conditional inclusion - */ -#define NR_COND 8 -/* - * number of defined structs/unions in one translation unit - */ -#define NR_MAXSTRUCTS 127 -/* - * 12 pointer, array, and function declarators (in any combinations) - * modifying an arithmetic, a structure, a union, or an incomplete type - * in a declaration - */ -#define NR_DECLARATORS 12 -/* - * 31 declarators nested by parentheses within a full declarator. - */ -#define NR_SUBTYPE 31 -/* - * 32 expressions nested by parentheses within a full expression - */ -#define NR_SUBEXPR 32 -/* - * 31 significant initial characters in an internal identifier or a - * macro name - */ -#define IDENTSIZ 31 -/* - * 511 external identifiers in one translation unit - */ -#define NR_EXT_IDENT 511 -/* - * 127 identifiers with block scope declared in one block - */ -#define NR_INT_IDENT 127 -/* - * 31 parameters in one function definition * 6 significant initial - * characters in an external identifier. - */ -#define NR_FUNPARAM 31 -/* - * 31 parameters in one macro definition - */ -#define NR_MACROARG 31 -/* - * 509 characters in a logical source line. - */ -#define LINESIZ 509 -/* - * 509 characters in a character string literal or wide string literal - * (after concatenation) - */ -#define STRINGSIZ 509 -/* - * 8 nesting levels for #include'd files - */ -#define NR_INCLUDE 9 -/* - * 257 case labels for a switch statement (excluding those for any - * nested switch statements) - */ -#define NR_SWITCH 257 -/* - * 127 members in a single structure or union - */ -#define NR_FIELDS 127 -/* - * 127 enumeration constants in a single enumeration - */ -#define NR_ENUM_CTES 127 -/* - * 15 levels of nested structure or union definitions in a single - * struct-declaration-list - */ -#define NR_STRUCT_LEVEL 15 -/* - * 32767 bytes in an object (in a hosted environment only) - */ -#define OBJECTSIZ 32767 -#endif 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); +}