shithub: scc

Download patch

ref: 63284fa474d0474e85fd63a0b2fb00878f7efeee
parent: 04bf7a4f601788f822f90698fc8f18d52b76f5e8
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Jul 20 15:23:42 EDT 2015

Remove sizeof nodes

We need calculate sizeof expressions in cc1, because in other case
we will not be able of calculate types like:

int a[sizeof(int)];

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -13,11 +13,18 @@
 typedef struct node Node;
 typedef struct input Input;
 
+/*
+ * TODO: Some of the data stored in type is shared with
+ *       cc2, so it should be stored in a table shared
+ *       between both programs, and dependant of the target.
+ */
 struct type {
 	unsigned char op;           /* type builder operator */
 	unsigned char ns;
 	char letter;                /* letter of the type */
 	bool defined;               /* type defined */
+	size_t size;                /* sizeof the type */
+	size_t align;               /* align of the type */
 	Type *type;                 /* base type */
 	Type *next;                 /* next element in the hash */
 	Type **pars;                /* type parameters */
@@ -211,7 +218,6 @@
 enum {
 	OPTR,
 	OADD,
-	OSIZE,
 	OMUL,
 	OSUB,
 	OINC,
@@ -337,7 +343,7 @@
 
 extern Type *voidtype, *pvoidtype, *booltype,
             *uchartype,   *chartype,
-            *uinttype,    *inttype,
+            *uinttype,    *inttype,     *sizetp,
             *ushortype,   *shortype,
             *longtype,    *ulongtype,
             *ullongtype,  *llongtype,
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -10,7 +10,7 @@
 static void emitbin(unsigned, void *),
             emitcast(unsigned, void *), emitswitch(unsigned, void *),
             emitsym(unsigned, void *), emitfield(unsigned, void *),
-            emitsizeof(unsigned, void *), emitexp(unsigned, void *),
+            emitexp(unsigned, void *),
             emitsymid(unsigned, void *), emittext(unsigned, void *),
             emitprint(unsigned, void *), emitfun(unsigned, void *),
             emitret(unsigned, void *), emitdcl(unsigned, void *);
@@ -21,7 +21,6 @@
 	[OMUL] = "*",
 	[OINC] = ";+",
 	[ODEC] =  ";-",
-	[OSIZE] = "#",
 	[OPTR] = "@",
 	[OMOD] = "%",
 	[ODIV] = "/",
@@ -72,7 +71,6 @@
 	[OMUL] = emitbin,
 	[OINC] = emitbin,
 	[ODEC] =  emitbin,
-	[OSIZE] = emitsizeof,
 	[OPTR] = emitbin,
 	[OMOD] = emitbin,
 	[ODIV] = emitbin,
@@ -235,13 +233,6 @@
 }
 
 static void
-emitsizeof(unsigned op, void *arg)
-{
-	Node *np = arg;
-	printf("\t#%c", np->left->type->letter);
-}
-
-static void
 emitexp(unsigned op, void *arg)
 {
 	Node *np = arg;
@@ -351,7 +342,10 @@
 sizeofnode(Type *tp)
 {
 	Node *np;
+	Symbol *sym;
 
-	np = node(0, tp, NULL, NULL);
-	return node(OSIZE, inttype, np, NULL);
+	sym = newsym(NS_IDEN);
+	sym->type = sizetp;
+	sym->u.i = tp->size;
+	return constnode(sym);
 }
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -24,12 +24,16 @@
 	},
 	{       /* 1 = pvoidtype */
 		.op = PTR,
-		.letter = L_POINTER
+		.letter = L_POINTER,
+		.size = 2,
+		.align = 2
 	},
 	{      /* 2 = booltype */
 		.op = INT,
 		.letter = L_BOOL,
 		.defined = 1,
+		.size = 1,
+		.align = 1,
 		.n.rank = RANK_BOOL
 	},
 	{       /* 3 = schartype */
@@ -36,6 +40,8 @@
 		.op = INT,
 		.letter = L_SCHAR,
 		.defined = 1,
+		.size = 1,
+		.align = 1,
 		.n.rank = RANK_SCHAR
 	},
 	{      /* 4 = uchartype */
@@ -42,6 +48,8 @@
 		.op = INT,
 		.letter = L_UCHAR,
 		.defined = 1,
+		.size = 1,
+		.align = 1,
 		.n.rank = RANK_UCHAR
 	},
 	{      /* 5 = chartype */
@@ -48,6 +56,8 @@
 		.op = INT,
 		.letter = L_CHAR,
 		.defined = 1,
+		.size = 1,
+		.align = 1,
 		.n.rank = RANK_CHAR
 	},
 	{       /* 6 = ushortype */
@@ -54,6 +64,8 @@
 		.op = INT,
 		.letter = L_USHORT,
 		.defined = 1,
+		.size = 2,
+		.align = 1,
 		.n.rank = RANK_USHORT
 	},
 	{       /* 7 = shortype */
@@ -60,6 +72,8 @@
 		.op = INT,
 		.letter = L_SHORT,
 		.defined = 1,
+		.size = 2,
+		.align = 1,
 		.n.rank = RANK_SHORT
 	},
 	{       /* 8 = uinttype */
@@ -66,6 +80,8 @@
 		.op = INT,
 		.letter = L_UINT,
 		.defined = 1,
+		.size = 2,
+		.align = 1,
 		.n.rank = RANK_UINT
 	},
 	{       /* 9 = inttype */
@@ -72,6 +88,8 @@
 		.op = INT,
 		.letter = L_INT,
 		.defined = 1,
+		.size = 2,
+		.align = 1,
 		.n.rank = RANK_INT
 	},
 	{      /* 10 = longtype */
@@ -78,6 +96,8 @@
 		.op = INT,
 		.letter = L_LONG,
 		.defined = 1,
+		.size = 4,
+		.align = 1,
 		.n.rank = RANK_LONG
 	},
 	{       /* 11 = ulongtype */
@@ -84,6 +104,8 @@
 		.op = INT,
 		.letter = L_ULONG,
 		.defined = 1,
+		.size = 4,
+		.align = 1,
 		.n.rank = RANK_ULONG
 	},
 	{	/* 12 = ullongtype */
@@ -90,6 +112,8 @@
 		.op = INT,
 		.letter = L_ULLONG,
 		.defined = 1,
+		.size = 8,
+		.align = 1,
 		.n.rank = RANK_ULLONG
 	},
 	{       /* 13 = llongtype */
@@ -96,6 +120,8 @@
 		.op = INT,
 		.letter = L_LLONG,
 		.defined = 1,
+		.size = 8,
+		.align = 1,
 		.n.rank = RANK_LLONG
 	},
 	{       /* 14 = floattype */
@@ -102,6 +128,8 @@
 		.op = FLOAT,
 		.letter = L_FLOAT,
 		.defined = 1,
+		.size = 4,
+		.align = 1,
 		.n.rank = RANK_FLOAT
 	},
 	{       /* 15 = doubletype */
@@ -108,6 +136,8 @@
 		.op = FLOAT,
 		.letter = L_DOUBLE,
 		.defined = 1,
+		.size = 8,
+		.align = 1,
 		.n.rank = RANK_DOUBLE
 	},
 	{       /* 16 = ldoubletype */
@@ -114,6 +144,8 @@
 		.op = FLOAT,
 		.letter = L_LDOUBLE,
 		.defined = 1,
+		.size = 16,
+		.align = 1,
 		.n.rank = RANK_LDOUBLE
 	}
 };
@@ -127,6 +159,8 @@
 	*ullongtype = &types[12], *llongtype = &types[13],
 	*floattype = &types[14], *doubletype = &types[15],
 	*ldoubletype = &types[16];
+
+Type *sizetp = &types[8]; /* TODO: This depend of the target */
 
 static Symbol dummy0 = {.u.i = 0, .type = &types[9]},
               dummy1 = {.u.i = 1, .type = &types[9]};