shithub: scc

Download patch

ref: a677ec7436ef171644dbfc8abccd332fceeeada1
parent: 3ebba91c4cc5aff7a1757f0c6b54928ca87fecb8
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Tue May 19 07:49:24 EDT 2015

Remove bitfields of Symbol in cc1

These bitfields can be implemented better as flags.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -29,13 +29,7 @@
 	uint8_t ctx;
 	uint8_t ns;
 	uint8_t token;
-	bool isglobal : 1;
-	bool isstatic : 1;
-	bool isauto : 1;
-	bool isregister : 1;
-	bool isdefined : 1;
-	bool isfield : 1;
-	bool isparameter : 1;
+	char flags;
 	union {
 		int i;
 		char *s;
@@ -99,6 +93,18 @@
 	NS_CPP,
 	NS_STRUCTS,
 	NR_NAMESPACES
+};
+
+/* symbol flags */
+enum {
+	ISGLOBAL   =  1,
+	ISSTATIC   =  2,
+	ISAUTO     =  4,
+	ISREGISTER =  8,
+	ISDEFINED  = 16,
+	ISFIELD    = 32,
+	ISPARAM    = 64,
+	ISEXTERN   =128
 };
 
 /* input tokens */
--- a/cc1/code.c
+++ b/cc1/code.c
@@ -159,17 +159,15 @@
 {
 	char c;
 
-	if (sym->isstatic && !sym->isglobal)
-		c = 'T';
-	else if (sym->isstatic && sym->isglobal)
-		c = 'Y';
-	else if (sym->isglobal)
+	if (sym->flags & ISSTATIC)
+		c = (sym->flags & ISGLOBAL) ? 'Y' : 'T';
+	else if (sym->flags & ISGLOBAL)
 		c = 'G';
-	else if (sym->isregister)
+	else if (sym->flags & ISREGISTER)
 		c = 'K';
-	else if (sym->isfield)
+	else if (sym->flags & ISFIELD)
 		c = 'M';
-	else if (sym->isparameter)
+	else if (sym->flags & ISPARAM)
 		c = 'P';
 	else
 		c = 'A';
@@ -271,7 +269,7 @@
 	Symbol *sym = arg;
 
 	printf("%c%d\tF\t%s\t{\n",
-	       sym->isglobal ? 'G' : 'Y', sym->id, sym->name);
+	       sym->flags & ISGLOBAL ? 'G' : 'Y', sym->id, sym->name);
 }
 
 static void
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -254,7 +254,7 @@
 static struct node *
 initializer(Symbol *sym)
 {
-	if (!sym->isdefined)
+	if (!(sym->flags & ISEXTERN))
 		error("'%s' initialized and declared extern", sym->name);
 
 	if (accept('{')) {
@@ -340,7 +340,7 @@
 
 		do {
 			sym = declarator(base, ID_EXPECTED, tagtype->ns);
-			sym->isfield = 1;
+			sym->flags |= ISFIELD;
 			tp = sym->type;
 			if (tp->op == FTN)
 				error("invalid type in struct/union");
@@ -404,7 +404,7 @@
 	if ((tp = specifier(&sclass)) == voidtype)
 		return NULL;
 	sym = declarator(tp, ID_ACCEPTED, NS_IDEN);
-	sym->isparameter = 1;
+	sym->flags |= ISPARAM;
 	tp = sym->type;
 	if (tp->op == FTN)
 		error("incorrect function type for a function parameter");
@@ -412,10 +412,10 @@
 		tp = mktype(tp->type, PTR, 0, NULL);
 	switch (sclass) {
 	case REGISTER:
-		sym->isregister = 1;
+		sym->flags |= ISREGISTER;
 		break;
 	case 0:
-		sym->isauto = 1;
+		sym->flags |= ISAUTO;
 		break;
 	default:
 		error("bad storage class in function parameter");
@@ -449,13 +449,13 @@
 			sym->token = TYPEIDEN;
 			continue;
 		case STATIC:
-			sym->isstatic = 1;
+			sym->flags |= ISSTATIC;
 			break;
 		case EXTERN:
-			sym->isdefined = 0;
+			sym->flags |= ISEXTERN;
 			break;
 		case REGISTER:
-			sym->isregister = 1;
+			sym->flags = ISREGISTER;
 			if (isfun)
 				goto bad_function;
 			break;
@@ -464,7 +464,7 @@
 				goto bad_function;
 			/* passtrough */
 		default:
-			sym->isauto = 1;
+			sym->flags |= ISAUTO;
 			break;
 		}
 		if (accept('='))
@@ -517,17 +517,17 @@
 			   problems with EOF */
 			sym = declarator(base, ID_EXPECTED, NS_IDEN);
 			tp = sym->type;
-			sym->isstatic = 1;
-			sym->isglobal= 1;
+			sym->flags |= ISSTATIC;
+			sym->flags |= ISGLOBAL;
 
 			switch (sclass) {
 			case REGISTER: case AUTO:
 				error("incorrect storage class for file-scope declaration");
 			case STATIC:
-				sym->isglobal = 0;
+				sym->flags |= ISSTATIC;
 				break;
 			case EXTERN:
-				sym->isdefined = 0;
+				sym->flags |= ISEXTERN;
 				break;
 			case TYPEDEF:
 				sym->token = TYPEIDEN;
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -420,7 +420,7 @@
 {
 	if (!np->lvalue)
 		error("lvalue required in unary expression");
-	if (np->symbol && np->sym->isregister)
+	if (np->symbol && (np->sym->flags & ISREGISTER))
 		error("address of register variable '%s' requested", yytext);
 	return node(op, mktype(np->type, PTR, 0, NULL), np, NULL);
 }
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -20,15 +20,18 @@
 
 	if ((sym = lookup(s, NS_LABEL)) != NULL) {
 		if (define) {
-			if (sym->isdefined)
+			if (sym->flags & ISDEFINED)
 				error("label '%s' already defined", s);
-			sym->isdefined = 1;
+			sym->flags |= ISDEFINED;
 		}
 		return sym;
 	}
 
 	sym = install(s, NS_LABEL);
-	sym->isdefined = define;
+	if (define)
+		sym->flags |= ISDEFINED;
+	else
+		sym->flags &= ~ISDEFINED;
 	return sym;
 }
 
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -38,7 +38,7 @@
 	for (sym = tbl->head; sym; sym = next) {
 		if (sym->ctx <= curctx)
 			break;
-		if (ns == NS_LABEL && !sym->isdefined)
+		if (ns == NS_LABEL && !(sym->flags & ISDEFINED))
 			error("label '%s' is not defined", sym->name);
 		if (ns == NS_TAG)
 			sym->type->defined = 0;
@@ -94,7 +94,7 @@
 	sym->ctx = curctx;
 	sym->token = IDEN;
 	sym->id = (curctx) ? ++localcnt : ++globalcnt;
-	sym->isdefined = 1;
+	sym->flags |= ISDEFINED;
 	sym->ns = ns;
 	tbl = &symtab[(ns > NS_STRUCTS) ? NS_STRUCTS : ns];
 	sym->next = tbl->head;