shithub: scc

Download patch

ref: 9f2aff7690c4222d1ad28eef68b042f9e6e61c27
parent: 17834699b2e8786effcd3fda322db20779710286
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Nov 15 02:05:42 EST 2014

Simplify mktype

This function had a switch with a lot of different options, which
can be simplified a lot using an array.

It is also needed share the pointer between aggregates and functions.
This is only temporal, because we want to unify them.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -49,8 +49,7 @@
 	short nelem;          /* number of elements in ary/ftn/strct/union */
 	union typeval {
 		unsigned char rank;   /* convertion rank */
-		Type **pars;         /* function parameters */
-		Field *fields;        /* aggregate fields */
+		void *pars;           /* parameters */
 	} u;
 };
 
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -258,7 +258,7 @@
 	register char *s, *t;
 
 	s = sym->name;
-	for (q = p = tp->u.fields; p; q = p, p = p->next) {
+	for (q = p = tp->u.pars; p; q = p, p = p->next) {
 		t = p->name;
 		if (*s == *t && !strcmp(s, t))
 			error("duplicated fields '%s' and '%s'", s, t);
@@ -273,7 +273,7 @@
 	p->id = sym->id;
 	p->type = sym->type;
 	if (!q)
-		tp->u.fields = p;
+		tp->u.pars= p;
 	else
 		q->next = p;
 
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -298,7 +298,7 @@
 		unexpected();
 	switch (np->typeop) {
 	case STRUCT: case UNION:
-		for (fp = np->type->u.fields; fp; fp = fp->next) {
+		for (fp = np->type->u.pars; fp; fp = fp->next) {
 			if (!strcmp(fp->name, yytext)) {
 				next();
 				return fieldcode(np, fp);
--- a/cc1/types.c
+++ b/cc1/types.c
@@ -188,67 +188,40 @@
 Type *
 mktype(Type *tp, uint8_t op, short nelem, void *data)
 {
-	static Type *typetab[NR_TYPE_HASH], **tbl;
-	static uint8_t t, def;
+	static Type *typetab[NR_TYPE_HASH], **tbl, type;
+	static uint8_t t;
 	register Type *bp;
-	char letter, look;
-	union typeval u;
+	static char letters[] = {
+		[PTR] = L_POINTER,   [ARY] = L_ARRAY,
+		[FTN] = L_FUNCTION,  [ENUM] = L_INT,
+		[STRUCT] = L_STRUCT, [UNION] = L_UNION
+	};
 
-	switch (op) {
-	case PTR:
-		if (tp == voidtype)
-			return pvoidtype;
-		letter = L_POINTER;
-		def = 1;
-		look = 1;
-		break;
-	case ARY:
-		letter = L_ARRAY;
-		def = nelem != 0;
-		look = 1;
-		break;
-	case FTN:
-		u.pars = data;
-		letter = L_FUNCTION;
-		def = 1;
-		look = 0;
-		break;
-	case ENUM:
-		letter = L_INT;
-		def = 1;
-		look = 0;
-		break;
-	case STRUCT: case UNION:
-		letter = (op == STRUCT) ? L_STRUCT : L_UNION;
-		def = 0;
-		look = 0;
-		u.fields = NULL;
-		break;
-	default:
-		fputs("internal type error, aborting\n", stderr);
-		abort();
-	}
+	if (op == PTR && tp == voidtype)
+		return pvoidtype;
 
-	t = (op  ^  (uint8_t) ((unsigned short) tp >> 3))
-	         & NR_TYPE_HASH-1;
+	type.type = tp;
+	type.op = op;
+	type.sign = 0;
+	type.letter = letters[op];
+	type.nelem = nelem;
+	type.u.pars = data;
+
+	if (op == ARY && nelem == 0 || op == STRUCT || op == UNION)
+		type.defined = 0;
+	else
+		type.defined = 1;
+
+	t = (op ^ (uint8_t) ((unsigned short) tp >> 3)) & NR_TYPE_HASH-1;
 	tbl = &typetab[t];
-	if (look) {
-		for (bp = *tbl; bp; bp = bp->next) {
-			if (bp->type == tp && bp->op == op &&
-			    (op != ARY || bp->nelem == nelem)) {
-				return bp;
-			}
-		}
+	for (bp = *tbl; bp; bp = bp->next) {
+		if (eqtype(bp, &type))
+			return bp;
 	}
 
-	bp = xcalloc(1, sizeof(*bp));
+	bp = xmalloc(sizeof(*bp));
+	*bp = type;
 	bp->next = *tbl;
-	bp->type = tp;
-	bp->op = op;
-	bp->letter = letter;
-	bp->defined = def;
-	bp->nelem = nelem;
-	bp->u = u;
 	return *tbl = bp;
 }
 
@@ -275,7 +248,7 @@
 		return 1;
 	case UNION:
 	case STRUCT: {
-		Field *fp1 = tp1->u.fields, *fp2 = tp2->u.fields;
+		Field *fp1 = tp1->u.pars, *fp2 = tp2->u.pars;
 
 		while (fp1 && fp2) {
 			if (!eqtype(fp1->type, fp2->type))