shithub: scc

Download patch

ref: 292c2611dbb22bc78f2fea52eda82252f071491b
parent: 9f0c6b1818a3dac48845572bb64c250673c41c88
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Apr 25 08:45:29 EDT 2014

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

--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,2 @@
+*.o
+*.a
--- /dev/null
+++ b/Makefile
@@ -1,0 +1,8 @@
+
+DIRS = lib cc1
+
+all clean:
+	for i in $(DIRS) ;\
+	do \
+		(cd $$i && $(MAKE) $@) ;\
+	done
--- 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
--- a/cc1/error.c
+++ b/cc1/error.c
@@ -41,13 +41,3 @@
 	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);
-}
--- a/cc1/wrapper.c
+++ /dev/null
@@ -1,51 +1,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;
-}
--- /dev/null
+++ b/inc/cc.h
@@ -1,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
--- /dev/null
+++ b/inc/sizes.h
@@ -1,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
--- a/include/cc.h
+++ /dev/null
@@ -1,15 +1,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
--- a/include/sizes.h
+++ /dev/null
@@ -1,88 +1,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
--- /dev/null
+++ b/lib/Makefile
@@ -1,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
--- /dev/null
+++ b/lib/die.c
@@ -1,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);
+}
--- /dev/null
+++ b/lib/xcalloc.c
@@ -1,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;
+}
--- /dev/null
+++ b/lib/xmalloc.c
@@ -1,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;
+}
--- /dev/null
+++ b/lib/xrealloc.c
@@ -1,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;
+}
--- /dev/null
+++ b/lib/xstrdup.c
@@ -1,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);
+}