scc

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

commit 0b39941d7622ebf6733e8d626c4b57404383d42f
parent 3421169ce3c39355690ad27754764dff1e76af2a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 11 May 2015 10:23:24 +0200

Create different functions for constant and variable nodes

Diffstat:
Mcc1/cc1.h | 3++-
Mcc1/code.c | 15++++++++++++++-
Mcc1/expr.c | 23++++++++++++-----------
Mcc1/stmt.c | 2+-
4 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -274,7 +274,8 @@ extern void delinput(void); /* code.c */ extern void emit(uint8_t, void *); extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth); -extern Node *symbol(Symbol *sym); +extern Node *varnode(Symbol *sym); +extern Node *constnode(Symbol *sym); extern void freetree(Node *np); /* expr.c */ diff --git a/cc1/code.c b/cc1/code.c @@ -330,7 +330,20 @@ node(uint8_t op, Type *tp, Node *left, Node *right) } Node * -symbol(Symbol *sym) +varnode(Symbol *sym) +{ + Node *np; + + np = node(OSYM, sym->type, NULL, NULL); + np->lvalue = 1; + np->constant = 0; + np->symbol = 1; + np->sym = sym; + return np; +} + +Node * +constnode(Symbol *sym) { Node *np; diff --git a/cc1/expr.c b/cc1/expr.c @@ -67,7 +67,7 @@ eval(Node *np) return NULL; if (np->op != OAND && np->op != OOR) return np; - p = node(OCOLON, inttype, symbol(one), symbol(zero)); + p = node(OCOLON, inttype, constnode(one), constnode(zero)); return node(OASK, inttype, np, p); } @@ -315,7 +315,7 @@ exp2cond(Node *np, char neg) { if (isnodecmp(np)) return (neg) ? negate(np) : np; - return compare(ONE ^ neg, np, symbol(zero)); + return compare(ONE ^ neg, np, constnode(zero)); } static Node * @@ -342,7 +342,7 @@ field(Node *np) error("incorrect field in struct/union"); lex_ns = NS_IDEN; next(); - return node(OFIELD, sym->type, symbol(sym), np); + return node(OFIELD, sym->type, varnode(sym), np); default: error("struct or union expected"); } @@ -369,7 +369,7 @@ iszero(Node *np) { if (isnodecmp(np)) return np; - return compare(ONE, np, symbol(zero)); + return compare(ONE, np, constnode(zero)); } static Node * @@ -403,7 +403,7 @@ incdec(Node *np, char op) break; case INT: case FLOAT: - inc = symbol(one); + inc = constnode(one); break; default: error("incorrect type in arithmetic operation"); @@ -459,18 +459,19 @@ static Node * primary(void) { Node *np; - Symbol *sym; switch (yytoken) { case CONSTANT: + np = constnode(yylval.sym); + next(); + break; case IDEN: - if ((sym = yylval.sym) == NULL) + if (yylval.sym == NULL) { + yylval.sym = install(yytext, NS_IDEN); + yylval.sym->type = inttype; error("'%s' undeclared", yytext); - np = symbol(yylval.sym); - if (yytoken == IDEN) { - np->lvalue = 1; - np->constant = 0; } + np = varnode(yylval.sym); next(); break; case '(': diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -57,7 +57,7 @@ condition(void) if (!setjmp(recover)) np = expr(); else - np = symbol(zero); + np = constnode(zero); np = iszero(np); expect(')'); return np;