scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 8c13f50ab5ae9ca834cd32ed55faec745d1a96c6
parent bf42a6e903cc4c2e7968b4a1148d20923b1bc8b6
Author: Roberto E. Vargas Caballero <Roberto E. Vargas Caballero>
Date:   Mon, 18 Apr 2016 03:15:35 +0200

[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.

Diffstat:
Mcc2/arch/amd64-sysv/code.c | 16++++++++--------
Mcc2/arch/i386-sysv/code.c | 16++++++++--------
Mcc2/arch/qbe/cgen.c | 2+-
Mcc2/arch/qbe/code.c | 33+++++++++++++++++----------------
Mcc2/arch/z80/code.c | 20++++++++++----------
Mcc2/cc2.h | 25+++++++++++++------------
Mcc2/code.c | 10++++------
Mcc2/parser.c | 36++++++++++++++++++------------------
8 files changed, 79 insertions(+), 79 deletions(-)

diff --git a/cc2/arch/amd64-sysv/code.c b/cc2/arch/amd64-sysv/code.c @@ -38,9 +38,9 @@ symname(Symbol *sym) 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 @@ label(Symbol *sym) 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 @@ label(Symbol *sym) 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 @@ void 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"); diff --git a/cc2/arch/i386-sysv/code.c b/cc2/arch/i386-sysv/code.c @@ -38,9 +38,9 @@ symname(Symbol *sym) 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 @@ label(Symbol *sym) 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 @@ label(Symbol *sym) 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 @@ void 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"); diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -16,7 +16,7 @@ tmpnode(Node *np) sym = getsym(TMPSYM); sym->type = np->type; - sym->kind = TMP; + sym->kind = STMP; np->u.sym = sym; np->op = OTMP; np->flags |= ISTMP; diff --git a/cc2/arch/qbe/code.c b/cc2/arch/qbe/code.c @@ -42,13 +42,14 @@ static char 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 @@ symname(Symbol *sym) 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 @@ size2asm(Type *tp) 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 @@ writeout(void) 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 @@ addr2txt(Addr *a) 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(); diff --git a/cc2/arch/z80/code.c b/cc2/arch/z80/code.c @@ -39,11 +39,11 @@ symname(Symbol *sym) 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 @@ label(Symbol *sym) 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 @@ defpar(Symbol *sym) { 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 @@ defpar(Symbol *sym) offpar -= align-1 & ~align; sym->u.off = offpar; offpar -= size; - sym->kind = AUTO; + sym->kind = SAUTO; } void @@ -184,7 +184,7 @@ defvar(Symbol *sym) { 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,14 +192,14 @@ defvar(Symbol *sym) offvar += align-1 & ~align; sym->u.off = offvar; offvar += size; - sym->kind = AUTO; + sym->kind = SAUTO; } void 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"); diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -9,6 +9,19 @@ enum tflags { 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 @@ enum op { 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', diff --git a/cc2/code.c b/cc2/code.c @@ -30,17 +30,15 @@ static void 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: diff --git a/cc2/parser.c b/cc2/parser.c @@ -27,7 +27,7 @@ Type funtype = { union tokenop { void *arg; - int op; + unsigned op; }; typedef void parsefun(char *, union tokenop); @@ -44,14 +44,14 @@ static struct decoc { 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 @@ symbol(char *token, union tokenop u) { 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 @@ decl(Symbol *sym) 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);