scc

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

commit ac87e5669ad21698f1564d8dd58a95fc0b731528
parent 4c7497f6177a9bfef0a5630634ab8ad948048a08
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 15 Apr 2014 07:47:14 +0200

Merge remote-tracking branch 'hal/master'

Diffstat:
Mcc.h | 3+--
Mcode.c | 6++++++
Mdecl.c | 4++--
Mexpr.c | 3++-
Mlex.c | 12++++++++++--
Msymbol.c | 28+++++++++-------------------
6 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/cc.h b/cc.h @@ -36,7 +36,6 @@ enum { NS_LABEL, NS_TAG, NR_NAMESPACES, - NS_KEYWORD, NS_FREE }; @@ -230,7 +229,7 @@ enum { extern void emitsym(Node *), emitunary(Node *), - emitbin(Node *), emitexp(Node *); + emitbin(Node *), emitexp(Node *), emitconst(Node *np); extern Node *node(Inst code, Type *tp, union unode u, uint8_t nchilds), diff --git a/code.c b/code.c @@ -72,6 +72,12 @@ emitsym(Node *np) } void +emitconst(Node *np) +{ + printf("\t#%X", np->u.sym->u.i); +} + +void emitcast(Node *np) { Node *child = np->childs[0]; diff --git a/decl.c b/decl.c @@ -75,7 +75,7 @@ directdcl(struct dcldata *dp, uint8_t ns, int8_t flags) if (yytoken != IDEN) { if (flags & ID_EXPECTED) goto expected; - sym = install(NULL, ns); + sym = install("", ns); } else { sym = newiden(ns); } @@ -348,7 +348,7 @@ newtag(uint8_t tag) } next(); } else { - sym = install(NULL, NS_TAG); + sym = install("", NS_TAG); } tp = sym->type = mktype(NULL, tag, NULL, 0); sym->u.ns = ++namespace; diff --git a/expr.c b/expr.c @@ -293,8 +293,9 @@ primary(void) next(); break; case CONSTANT: + sym = yylval.sym; + np = node(emitconst, sym->type, SYM(sym), 0); next(); - /* TODO: do something */ break; case '(': next(); diff --git a/lex.c b/lex.c @@ -24,11 +24,13 @@ static uint8_t integer(char *s, char base) { static Type *tp; + static Symbol *sym; static char ch; /* TODO: implement again */ -type: switch (ch = toupper(getc(yyin))) { +type: + switch (ch = toupper(getc(yyin))) { case 'L': goto type; case 'U': @@ -37,6 +39,10 @@ type: switch (ch = toupper(getc(yyin))) { ungetc(ch, yyin); } + sym = install("", NS_IDEN); + sym->type = inttype; + sym->u.i = atoi(yytext); + yynlval.sym = sym; return CONSTANT; } @@ -127,12 +133,14 @@ init_keywords(void) {NULL, 0, 0}, }; register Symbol *sym; + extern short symid; for (bp = buff; bp->str; ++bp) { - sym = install(bp->str, NS_KEYWORD); + sym = install(bp->str, NS_IDEN); sym->token = bp->token; sym->u.token = bp->value; } + symid = 0; } static uint8_t diff --git a/symbol.c b/symbol.c @@ -9,7 +9,8 @@ #define NR_SYM_HASH 32 uint8_t curctx; -uint8_t namespace = NS_KEYWORD + 1 ; +uint8_t namespace = NS_FREE + 1; +short symid; static struct symtab { Symbol *head; @@ -77,34 +78,23 @@ lookup(register char *s, uint8_t ns) Symbol * install(char *s, uint8_t ns) { - register Symbol *sym; - register Symbol **t; + register Symbol *sym, **t; struct symtab *tbl; - static short id; - - if (ns == NS_KEYWORD) { - ns = NS_IDEN; - } else { - ++id; - if (s != NULL) - s = xstrdup(s); - } sym = xcalloc(1, sizeof(*sym)); - sym->name = s; + sym->name = xstrdup(s); sym->ctx = curctx; sym->token = IDEN; sym->ns = ns; - sym->id = id; + sym->id = symid++; tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns]; sym->next = tbl->head; tbl->head = sym; - if (s != NULL) { - t = &tbl->htab[hash(s)]; - sym->hash = *t; - *t = sym; - } + + t = &tbl->htab[hash(s)]; + sym->hash = *t; + *t = sym; return sym; }