shithub: scc

Download patch

ref: ea484c73dd3bb29affd04d7959d9b05f83ab1b4f
parent: aa002c2afd8736f8c39684e59754e54be5e997a2
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Apr 17 23:15:35 EDT 2016

[cc2] Create a enum for storage classes

This is the enum for the different storage class, or in a general
form, for the different kind of symbols we are going to have in
the the compiler (not all the target willl use all the different kinds).
This patch foud several errors, and it created new bugs.

--- a/cc2/arch/amd64-sysv/code.c
+++ b/cc2/arch/amd64-sysv/code.c
@@ -38,9 +38,9 @@
 
 	if (sym->name) {
 		switch (sym->kind) {
-		case EXTRN:
-		case GLOB:
-		case PRIVAT:
+		case SEXTRN:
+		case SGLOB:
+		case SPRIV:
 			return sym->name;
 		}
 	}
@@ -155,11 +155,11 @@
 	segment(seg);
 
 	switch (sym->kind) {
-	case EXTRN:
+	case SEXTRN:
 		printf("\t.extern\t%s\n", name);
-	case LOCAL:
+	case SLOCAL:
 		return;
-	case GLOB:
+	case SGLOB:
 		printf("\t.global\t%s\n", name);
 		if (seg == BSSSEG) {
 			printf("\t.comm\t%s,%llu\n",
@@ -169,7 +169,7 @@
 		break;
 	}
 	if (sym->type.align != 1)
-		printf("\t.align\t%d\n",sym->type.align );
+		printf("\t.align\t%lld\n", (long long) sym->type.align );
 	printf("%s:\n", name);
 }
 
@@ -177,7 +177,7 @@
 defglobal(Symbol *sym)
 {
 	label(sym);
-	if (sym->kind == EXTRN || (sym->type.flags & INITF))
+	if (sym->kind == SEXTRN || (sym->type.flags & INITF))
 		return;
 	size2asm(&sym->type);
 	puts("0");
--- a/cc2/arch/i386-sysv/code.c
+++ b/cc2/arch/i386-sysv/code.c
@@ -38,9 +38,9 @@
 
 	if (sym->name) {
 		switch (sym->kind) {
-		case EXTRN:
-		case GLOB:
-		case PRIVAT:
+		case SEXTRN:
+		case SGLOB:
+		case SPRIV:
 			return sym->name;
 		}
 	}
@@ -154,11 +154,11 @@
 	segment(seg);
 
 	switch (sym->kind) {
-	case EXTRN:
+	case SEXTRN:
 		printf("\t.extern\t%s\n", name);
-	case LOCAL:
+	case SLOCAL:
 		return;
-	case GLOB:
+	case SGLOB:
 		printf("\t.global\t%s\n", name);
 		if (seg == BSSSEG) {
 			printf("\t.comm\t%s,%llu\n",
@@ -168,7 +168,7 @@
 		break;
 	}
 	if (sym->type.align != 1)
-		printf("\t.align\t%d\n",sym->type.align );
+		printf("\t.align\t%lld\n", (long long) sym->type.align );
 	printf("%s:\n", name);
 }
 
@@ -176,7 +176,7 @@
 defglobal(Symbol *sym)
 {
 	label(sym);
-	if (sym->kind == EXTRN || (sym->type.flags & INITF))
+	if (sym->kind == SEXTRN || (sym->type.flags & INITF))
 		return;
 	size2asm(&sym->type);
 	puts("0");
--- a/cc2/arch/qbe/cgen.c
+++ b/cc2/arch/qbe/cgen.c
@@ -16,7 +16,7 @@
 
 	sym = getsym(TMPSYM);
 	sym->type = np->type;
-	sym->kind = TMP;
+	sym->kind = STMP;
 	np->u.sym = sym;
 	np->op = OTMP;
 	np->flags |= ISTMP;
--- a/cc2/arch/qbe/code.c
+++ b/cc2/arch/qbe/code.c
@@ -42,13 +42,14 @@
 sigil(Symbol *sym)
 {
 	switch (sym->kind) {
-	case EXTRN:
-	case GLOB:
-	case PRIVAT:
-	case LOCAL:
+	case SEXTRN:
+	case SGLOB:
+	case SPRIV:
+	case SLOCAL:
 		return '$';
-	case AUTO:
-	case REG:
+	case SAUTO:
+	case SREG:
+	case STMP:
 		return '%';
 	default:
 		abort();
@@ -64,10 +65,10 @@
 
 	if (sym->name) {
 		switch (sym->kind) {
-		case EXTRN:
-		case GLOB:
-		case PRIVAT:
-		case AUTO:
+		case SEXTRN:
+		case SGLOB:
+		case SPRIV:
+		case SAUTO:
 			sprintf(name, "%c%s", c, sym->name);
 			return name;
 		default:
@@ -156,9 +157,9 @@
 void
 defglobal(Symbol *sym)
 {
-	if (sym->kind == EXTRN)
+	if (sym->kind == SEXTRN)
 		return;
-	if (sym->kind == GLOB)
+	if (sym->kind == SGLOB)
 		fputs("export ", stdout);
 	printf("data %s = {\n", symname(sym));
 	if (sym->type.flags & INITF)
@@ -193,7 +194,7 @@
 	Type *tp;
 	char *sep;
 
-	if (curfun->kind == GLOB)
+	if (curfun->kind == SGLOB)
 		fputs("export ", stdout);
 	printf("function %s %s(", size2asm(&curfun->rtype), symname(curfun));
 
@@ -226,9 +227,9 @@
 	static char buff[40];
 
 	switch (a->kind) {
-	case AUTO:
-	case LABEL:
-	case TMP:
+	case SAUTO:
+	case SLABEL:
+	case STMP:
 		return symname(a->u.sym);
 	default:
 		abort();
--- a/cc2/arch/z80/code.c
+++ b/cc2/arch/z80/code.c
@@ -39,11 +39,11 @@
 
 	if (sym->name) {
 		switch (sym->kind) {
-		case GLOB:
-		case EXTRN:
+		case SGLOB:
+		case SEXTRN:
 			snprintf(name, sizeof(name), "_%s", sym->name);
 			return name;
-		case PRIVAT:
+		case SPRIV:
 			return sym->name;
 		}
 	}
@@ -70,10 +70,10 @@
 	segment(seg);
 
 	switch (sym->kind) {
-	case EXTRN:
+	case SEXTRN:
 		printf("\tEXTRN\t%s\n", name);
 		return;
-	case GLOB:
+	case SGLOB:
 		printf("\tPUBLIC\t%s\n", name);
 		break;
 	}
@@ -168,7 +168,7 @@
 {
 	TSIZE align, size;
 
-	if (sym->kind != REG && sym->kind != AUTO)
+	if (sym->kind != SREG && sym->kind != SAUTO)
 		return;
 	align = sym->type.align;
 	size = sym->type.size;
@@ -176,7 +176,7 @@
 	offpar -= align-1 & ~align;
 	sym->u.off = offpar;
 	offpar -= size;
-	sym->kind = AUTO;
+	sym->kind = SAUTO;
 }
 
 void
@@ -184,7 +184,7 @@
 {
 	TSIZE align, size;
 
-	if (sym->kind != REG && sym->kind != AUTO)
+	if (sym->kind != SREG && sym->kind != SAUTO)
 		return;
 	align = sym->type.align;
 	size = sym->type.size;
@@ -192,7 +192,7 @@
 	offvar += align-1 & ~align;
 	sym->u.off = offvar;
 	offvar += size;
-	sym->kind = AUTO;
+	sym->kind = SAUTO;
 }
 
 void
@@ -199,7 +199,7 @@
 defglobal(Symbol *sym)
 {
 	label(sym);
-	if (sym->kind == EXTRN || (sym->type.flags & INITF))
+	if (sym->kind == SEXTRN || (sym->type.flags & INITF))
 		return;
 	size2asm(&sym->type);
 	puts("0");
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -9,6 +9,19 @@
 	INITF   =   128
 };
 
+enum sclass {
+	SAUTO     = 'A',
+	SREG      = 'R',
+	SLABEL    = 'L',
+	SINDEX    = 'I',
+	STMP      = 'N',
+	SGLOB     = 'G',
+	SEXTRN    = 'X',
+	SPRIV     = 'Y',
+	SLOCAL    = 'T',
+	SMEMB     = 'M',
+};
+
 enum op {
 	/* types */
 	ELLIPSIS = 'E',
@@ -33,20 +46,8 @@
 	ONAME    = '"',
 	/* kind of operand */
 	NONE     = 0,
-	MEM      = 'M',
-	AUTO     = 'A',
-	REG      = 'R',
 	CONST    = '#',
 	STRING   = '"',
-	LABEL    = 'L',
-	INDEX    = 'I',
-	TMP      = 'T',
-	/* storage class */
-	GLOB     = 'G',
-	EXTRN    = 'X',
-	PRIVAT   = 'Y',
-	LOCAL    = 'T',
-	MEMBER   = 'M',
 	/* operands */
 	OMEM     = 'M',
 	OTMP     = 'T',
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -30,17 +30,15 @@
 addr(Node *np, Addr *addr)
 {
 	switch (addr->kind = np->op) {
-	case REG:
+	case SREG:
 		addr->u.reg = np->u.reg;
 		break;
 	case CONST:
 		abort();
 		break;
-	case LABEL:
-		addr->u.sym = np->u.sym;
-		break;
-	case AUTO:
-	case TMP:
+	case SLABEL:
+	case SAUTO:
+	case STMP:
 		addr->u.sym = np->u.sym;
 		break;
 	default:
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -27,7 +27,7 @@
 
 union tokenop {
 	void *arg;
-	int op;
+	unsigned op;
 };
 
 typedef void parsefun(char *, union tokenop);
@@ -44,14 +44,14 @@
 	void (*parse)(char *token, union tokenop);
 	union tokenop u;
 } optbl[] = {      /*  eval     parse           args */
-	['A']   = {  vardecl,  symbol, .u.op  =        OAUTO},
-	['R']   = {  vardecl,  symbol, .u.op  =         OREG},
-	['G']   = {  vardecl,  symbol, .u.op  =         OMEM},
-	['X']   = {  vardecl,  symbol, .u.op  =         OMEM},
-	['Y']   = {  vardecl,  symbol, .u.op  =         OMEM},
-	['T']   = {  vardecl,  symbol, .u.op  =         OMEM},
-	['M']   = {  flddecl,  symbol, .u.op  =         OMEM},
-	['L']   = { labeldcl,  symbol, .u.op  =       OLABEL},
+	['A']   = {  vardecl,  symbol, .u.op  =  SAUTO<<8 | OAUTO},
+	['R']   = {  vardecl,  symbol, .u.op  =   SREG<<8 |  OREG},
+	['G']   = {  vardecl,  symbol, .u.op  =  SGLOB<<8 |  OMEM},
+	['X']   = {  vardecl,  symbol, .u.op  = SEXTRN<<8 |  OMEM},
+	['Y']   = {  vardecl,  symbol, .u.op  =  SPRIV<<8 |  OMEM},
+	['T']   = {  vardecl,  symbol, .u.op  = SLOCAL<<8 |  OMEM},
+	['M']   = {  flddecl,  symbol, .u.op  =  SMEMB<<8 |  OMEM},
+	['L']   = { labeldcl,  symbol, .u.op  = SLABEL<<8 | OLABEL},
 
 	['C']   = {     NULL,    type, .u.arg =    &int8type},
 	['I']   = {     NULL,    type, .u.arg =   &int16type},
@@ -182,10 +182,10 @@
 {
 	Node *np;
 
-	sclass = *token++;
+	sclass = u.op >> 8;
 	np = newnode();
-	np->u.sym = getsym(atoi(token));
-	np->op = u.op;
+	np->u.sym = getsym(atoi(token+1));
+	np->op = u.op & 0xFF;
 	push(np);
 }
 
@@ -492,14 +492,14 @@
 		curfun = sym;
 	} else {
 		switch (sym->kind) {
-		case EXTRN:
-		case GLOB:
-		case PRIVAT:
-		case LOCAL:
+		case SEXTRN:
+		case SGLOB:
+		case SPRIV:
+		case SLOCAL:
 			defglobal(sym);
 			break;
-		case AUTO:
-		case REG:
+		case SAUTO:
+		case SREG:
 			if (!curfun)
 				error(ESYNTAX);
 			((inpars) ? defpar : defvar)(sym);