scc

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

commit 2149f9e083fe7d75fa41e011ba8923b5c8a7da2e
parent 0681c99653ef080b235082875a2b6e5490ec4ff8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 21 Apr 2014 07:58:40 +0200

Unify symbol and constant code

The only difference in this case is what code is called, so
it is a good idea merge them.

Diffstat:
Mcc1.h | 5+++--
Mcode.c | 19++++++++++---------
Mexpr.c | 19+++++++++----------
3 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/cc1.h b/cc1.h @@ -195,6 +195,7 @@ typedef struct node { struct { bool lvalue : 1; bool symbol: 1; + bool constant : 1; } b; union unode { Symbol *sym; @@ -223,7 +224,7 @@ enum { extern void emitdcl(Symbol *), emitsframe(Symbol *), emiteframe(Symbol *), emitsym(Node *), emitunary(Node *), - emitbin(Node *), emitexp(Node *), emitconst(Node *np), + emitbin(Node *), emitexp(Node *), emitprint(Node *); extern Node @@ -233,7 +234,7 @@ extern Node *castcode(Node *child, Type *tp), *sizeofcode(Type *tp), *ternarycode(Node *cond, Node *ifyes, Node *ifno), - *constcode(Symbol *sym); + *symcode(Symbol *sym); #define SYM(s) ((union unode) {.sym = s}) #define OP(s) ((union unode) {.op = s}) diff --git a/code.c b/code.c @@ -78,11 +78,17 @@ emitsym2(Symbol *sym) printf("%c%d", c, sym->id); } +static void +emitconst(Node *np) +{ + printf("#%x", np->u.sym->u.i); +} + void emitsym(Node *np) { putchar('\t'); - emitsym2(np->u.sym); + (np->b.constant) ? emitconst(np) : emitsym2(np->u.sym); } static void @@ -101,12 +107,6 @@ emitdcl(Symbol *sym) } void -emitconst(Node *np) -{ - printf("\t#%x", np->u.sym->u.i); -} - -void emitcast(Node *np) { Node *child = np->childs[0]; @@ -244,11 +244,12 @@ ternarycode(Node *cond, Node *ifyes, Node *ifno) } Node * -constcode(Symbol *sym) +symcode(Symbol *sym) { Node *np; - np = node(emitconst, inttype, SYM(sym), 0); + np = node(emitsym, inttype, SYM(sym), 0); np->b.symbol = 1; + np->b.constant = 1; return np; } diff --git a/expr.c b/expr.c @@ -226,7 +226,7 @@ exp2cond(Node *np, char neg) return np; } - return compare(ONE ^ neg, np, constcode(zero)); + return compare(ONE ^ neg, np, symcode(zero)); } static Node * @@ -270,7 +270,7 @@ iszero(Node *np) { if (ISNODECMP(np)) return np; - return compare(ONE, np, constcode(zero)); + return compare(ONE, np, symcode(zero)); } static Node * @@ -278,7 +278,7 @@ eval(Node *np) { if (!ISNODECMP(np)) return np; - return ternarycode(np, constcode(one), constcode(zero)); + return ternarycode(np, symcode(one), symcode(zero)); } static Node * @@ -356,15 +356,14 @@ primary(void) Symbol *sym; switch (yytoken) { - case IDEN: + case CONSTANT: case IDEN: if ((sym = yylval.sym) == NULL) error("'%s' undeclared", yytext); - np = node(emitsym, sym->type, SYM(sym), 0); - np->b.symbol = np->b.lvalue = 1; - next(); - break; - case CONSTANT: - np = constcode(yylval.sym); + np = symcode(yylval.sym); + if (yytoken == IDEN) { + np->b.lvalue = 1; + np->b.constant = 0; + } next(); break; /* TODO: case STRING: */