scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit a662ba437aec0bcbd206ebd4637474a203dbe358
parent b69d387190663d6ca1b7f92daa1fa342b589a21e
Author: Quentin Rameau <quinq@fifth.space>
Date:   Mon,  9 May 2016 14:15:47 +0200

cc2: pass op type to newnode()

Using newnode(int op) saves us a few temporary variables as we almost
always assign an op after creating a new node.

Diffstat:
cc2/arch/qbe/cgen.c | 9+++------
cc2/cc2.h | 2+-
cc2/node.c | 7+++++--
cc2/parser.c | 57+++++++++++++++++----------------------------------------
4 files changed, 26 insertions(+), 49 deletions(-)

diff --git a/cc2/arch/qbe/cgen.c b/cc2/arch/qbe/cgen.c @@ -107,11 +107,10 @@ tmpnode(Node *np) static Node * load(Node *np) { - Node *new; int op; + Node *new = tmpnode(newnode(ONOP)); Type *tp = &np->type; - new = tmpnode(newnode()); new->left = np; new->type = *tp; @@ -191,7 +190,7 @@ cast(Node *nd, Node *ns) switch (ts->size) { case 1: case 2: - tmp = tmpnode(newnode()); + tmp = tmpnode(newnode(ONOP)); tmp->type = (ts->flags&SIGNF) ? int32type : uint32type; tmp->left = ns; nd->left = ns = cast(tmp, ns); @@ -228,9 +227,7 @@ cgen(Node *np) if (np->label) { setlabel(np->label); if (np->next == NULL) { - Node *tmp = newnode(); - tmp->op = ORET; - addstmt(tmp); + addstmt(newnode(ORET)); prevstmt(); } } diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -208,7 +208,7 @@ extern void apply(Node *(*fun)(Node *)); extern void cleannodes(void); extern void delnode(Node *np); extern void deltree(Node *np); -extern Node *newnode(void); +extern Node *newnode(int op); extern Node *addstmt(Node *np); extern Node *prevstmt(void), *nextstmt(void); diff --git a/cc2/node.c b/cc2/node.c @@ -23,7 +23,7 @@ static Node *freep; static int inhome; Node * -newnode(void) +newnode(int op) { struct arena *ap; Node *np; @@ -42,7 +42,10 @@ newnode(void) np = freep; freep = np->left; - return memset(np, 0, sizeof(*np)); + memset(np, 0, sizeof(*np)); + np->op = op; + + return np; } Node * diff --git a/cc2/parser.c b/cc2/parser.c @@ -179,15 +179,12 @@ getname(char *t, union tokenop u) static void symbol(char *token, union tokenop u) { - Node *np; - Symbol *sym; + Node *np = newnode(u.op & 0xFF); + Symbol *sym = getsym(atoi(token+1)); sclass = u.op >> 8; - np = newnode(); - sym = getsym(atoi(token+1)); np->u.sym = sym; np->type = sym->type; - np->op = u.op & 0xFF; push(np); } @@ -207,20 +204,20 @@ static void constant(char *token, union tokenop u) { static char letters[] = "0123456789ABCDEF"; - Node *np = newnode(); + Node *np; TUINT v; unsigned c; ++token; if (*token == OSTRING) { ++token; - np->op = OSTRING; + np = newnode(OSTRING); np->type.flags = STRF; np->type.size = strlen(token); np->type.align = int8type.align; np->u.s = xstrdup(token); } else { - np->op = OCONST; + np = newnode(OCONST); np->type = *gettype(token++); for (v = 0; c = *token++; v += c) { v <<= 4; @@ -234,8 +231,8 @@ constant(char *token, union tokenop u) static void assign(char *token, union tokenop u) { - int subop, op = u.op; - Node *np = newnode(); + int subop; + Node *np = newnode(u.op); switch (subop = *++token) { case '/': @@ -258,7 +255,6 @@ assign(char *token, union tokenop u) } np->u.subop = subop; - np->op = op; np->type = *gettype(token); np->right = pop(); np->left = pop(); @@ -268,18 +264,12 @@ assign(char *token, union tokenop u) static void ternary(char *token, union tokenop u) { - Node *ask, *colon; - Type *tp; - - tp = gettype(token+1); + Node *ask = newnode(OCOLON), *colon = newnode(OASK); + Type *tp = gettype(token+1); - colon = newnode(); - colon->op = OCOLON; colon->right = pop(); colon->left = pop(); - ask = newnode(); - ask->op = OASK; ask->type = *tp; ask->left = pop(); push(ask); @@ -328,10 +318,8 @@ repeat: static void oreturn(char *token, union tokenop u) { - Node *np; + Node *np = newnode(u.op); - np = newnode(); - np->op = u.op; eval(strtok(NULL, "\t\n")); if (!empty()) np->left = pop(); @@ -341,10 +329,8 @@ oreturn(char *token, union tokenop u) static void jump(char *token, union tokenop u) { - Node *np, *aux; + Node *aux, *np = newnode(u.op); - np = newnode(); - np->op = u.op; eval(strtok(NULL, "\t\n")); if (u.op != OJMP) @@ -358,10 +344,8 @@ jump(char *token, union tokenop u) static void casetbl(char *token, union tokenop u) { - Node *np, *aux; + Node *np = newnode(u.op); - np = newnode(); - np->op = u.op; eval(strtok(NULL, "\t\n")); np->left = pop(); push(np); @@ -370,19 +354,14 @@ casetbl(char *token, union tokenop u) static void loop(char *token, union tokenop u) { - Node *np; - - np = newnode(); - np->op = u.op; - push(np); + push(newnode(u.op)); } static void unary(char *token, union tokenop u) { - Node *np = newnode(); + Node *np = newnode(u.op); - np->op = u.op; np->type = *gettype(token+1); np->left = pop(); np->right = NULL; @@ -392,7 +371,7 @@ unary(char *token, union tokenop u) static void call(char *token, union tokenop u) { - Node *np, *par, *fun; + Node *np, *par, *fun = newnode(u.op); for (par = NULL;; par = np) { np = pop(); @@ -400,8 +379,7 @@ call(char *token, union tokenop u) break; np->right = par; } - fun = newnode(); - fun->op = u.op; + fun->type = *gettype(token+1); fun->left = np; fun->right = par; @@ -411,9 +389,8 @@ call(char *token, union tokenop u) static void binary(char *token, union tokenop u) { - Node *np = newnode(); + Node *np = newnode(u.op); - np->op = u.op; np->type = *gettype(token+1); np->right = pop(); np->left = pop();