shithub: scc

Download patch

ref: 627efdb4af18c26fa5707e247fc276837cfed3db
parent: eaf8259846c2b6d77467cddf8fe143c6cce568dd
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Mar 17 12:43:36 EDT 2014

Unify STRUCT and UNIONS declaration

There are the same thing, and the only difference between them is the
offset of the fields, that in the case of the unions is allways 0
for all the fields of the union.

--- a/decl.c
+++ b/decl.c
@@ -252,7 +252,7 @@
 {
 	struct ctype *tp;
 	struct symbol *sym;
-	short offset = 0;
+	short offset = 0, size;
 
 	switch (yytoken) {
 	case IDEN:
@@ -275,8 +275,11 @@
 		do {
 			sym = declarator(tp, ns, 1);
 			sym->u.offset = offset;
+			size = sym->type->size;
 			if (type == STRUCT)
-				offset += sym->type->size;
+				offset += size;
+			else if (offset < size)
+				offset = size;
 		} while (accept(','));
 	}
 
@@ -305,7 +308,7 @@
 	} else {
 		sym = install(NULL, NS_TAG);
 	}
-	sym->type = tp = mktype(NULL, tag, NULL, 0);
+	sym->type = tp = mktype(NULL, STRUCT, NULL, 0);
 	++namespace;
 
 	if (yytoken != ';') {
--- a/types.c
+++ b/types.c
@@ -162,7 +162,7 @@
 	t = (op  ^  (uint8_t) ((unsigned short) tp >> 3))
 	         & NR_TYPE_HASH-1;
 	tbl = &typetab[t];
-	if (op != FTN || op != STRUCT || op != UNION || op != ENUM) {
+	if (op != FTN || op != STRUCT || op != ENUM) {
 		for (bp = *tbl; bp; bp = bp->next) {
 			if (bp->type == tp && bp->op == op &&
 			    bp->sym == sym && bp->nelem == nelem) {
@@ -176,7 +176,7 @@
 	case FTN: size = 0; break;
 	case ARY: size = tp->size * nelem; break;
 	case ENUM: size = INTSIZE;
-	case STRUCT: case UNION: size = 0; break;
+	case STRUCT: size = 0; break;
 	default:  size = tp->size; break;
 	}
 	bp = xmalloc(sizeof(*bp));