scc

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

commit d5b51ff627be535ef485d228428c2a9b9f5824cc
parent 7d77cd4c9f3bd9c9a4ad49890b08a4181552ff3d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  3 Oct 2013 22:03:37 +0200

Remove node1

node1 is only a space optimization of node, but it requieres
that a walk procedure can difference between binary nodes and
unitary nodes. If we reduce the number of unitary nodes to
symbol nodes, run over the tree is trivial.

Diffstat:
Mexpr.c | 6+++---
Mflow.c | 14+++++++-------
Msyntax.h | 2--
Mtree.c | 55++++++++++++++++++-------------------------------------
4 files changed, 28 insertions(+), 49 deletions(-)

diff --git a/expr.c b/expr.c @@ -71,7 +71,7 @@ postfix(void) np1 = node(op, np1, nodesym(yyval.sym)); continue; next: - np1 = node1(op, np1); + np1 = node(op, np1, NULL); next(); continue; } @@ -107,11 +107,11 @@ unary(void) call_cast: next(); - return node1(op, cast()); + return node(op, cast(), NULL); call_unary: next(); - return node1(op, unary()); + return node(op, unary(), NULL); } static struct node * diff --git a/flow.c b/flow.c @@ -58,7 +58,7 @@ _goto(void) sym = yyval.sym; if (sym->ns != NS_LABEL) sym = newlabel(sym, yytext); - np = node1(OGOTO, nodesym(sym)); + np = node(OGOTO, nodesym(sym), NULL); expect(';'); return np; @@ -169,7 +169,7 @@ _break(void) expect(';'); if (blockp == blocks) error("break statement not within loop or switch"); - return node1(OBREAK, NULL); + return node(OBREAK, NULL, NULL); } static struct node * @@ -185,7 +185,7 @@ _continue(void) if (bp == blockp) error("continue statement not within loop"); - return node1(OCONT, NULL); + return node(OCONT, NULL, NULL); } static struct node * @@ -197,7 +197,7 @@ _return(void) np = expr(); expect(';'); - return node1(ORETURN, np); + return node(ORETURN, np, NULL); } static struct node * @@ -214,7 +214,7 @@ _case(void) ; /* nothing */ if (bp == blockp) error("case statement not within switch"); - np = node1(OCASE, exp); + np = node(OCASE, exp, NULL); expect(':'); return np; } @@ -230,7 +230,7 @@ _default(void) if (bp == blockp) error("default statement not within switch"); expect(':'); - return node1(ODEFAULT, NULL); + return node(ODEFAULT, NULL, NULL); } static struct node * @@ -278,7 +278,7 @@ struct node * function(register struct symbol *sym) { curfun = sym; - return node1(OFTN, compound()); + return node(OFTN, compound(), NULL); } void diff --git a/syntax.h b/syntax.h @@ -24,8 +24,6 @@ extern void type_name(void); extern struct node *function(struct symbol *sym); extern struct node *node(unsigned char op, struct node *l, struct node *r); -extern struct node *node1(unsigned char op, struct node *i); - extern struct node *nodesym(struct symbol *sym); extern struct node *nodecomp(void); extern struct node *addstmt(struct node *np, struct node *stmt); diff --git a/tree.c b/tree.c @@ -12,11 +12,6 @@ struct node { unsigned char op; }; -struct node_op1 { - struct node base; - struct node *infix; -}; - struct node_op2 { struct node base; struct node *left; @@ -61,17 +56,6 @@ node(unsigned char op, struct node *l, struct node *r) } struct node * -node1(unsigned char op, struct node *i) -{ - register struct node_op1 *np = xmalloc(sizeof(*np)); - - np->base.op = op; - np->infix = i; - - return (struct node *) np; -} - -struct node * nodecomp(void) { register struct node_comp *np = xmalloc(sizeof(*np)); @@ -111,20 +95,20 @@ prtree_helper(register struct node *np) unsigned char nchild; const char *txt; } *bp, optab [] = { - [OCALL] = {1, "()"}, + [OCALL] = {2, "()"}, [OARY] = {2, "[]"}, [OFIELD] = {2, "."}, [OPTR] = {2, "->"}, - [OPOSTINC] = {1, ".++"}, - [OPOSTDEC] = {1, ".--"}, - [OPREINC] = {1, "++."}, - [OPREDEC] = {1, "--."}, - [OADDR] = {1, "&."}, - [OINDIR] = {1, "[*]"}, - [OMINUS] = {1, "-."}, - [OPLUS] = {1, "+."}, - [OCPL] = {1, "~"}, - [ONEG] = {1, "!"}, + [OPOSTINC] = {2, ".++"}, + [OPOSTDEC] = {2, ".--"}, + [OPREINC] = {2, "++."}, + [OPREDEC] = {2, "--."}, + [OADDR] = {2, "&."}, + [OINDIR] = {2, "[*]"}, + [OMINUS] = {2, "-."}, + [OPLUS] = {2, "+."}, + [OCPL] = {2, "~"}, + [ONEG] = {2, "!"}, [OMUL] = {2, "*"}, [ODIV] = {2, "/"}, [OMOD] = {2, "%"}, @@ -164,13 +148,13 @@ prtree_helper(register struct node *np) [ODO] = {2, "do"}, [OWHILE] = {2, "while"}, [OLABEL] = {2, "label"}, - [OGOTO] = {1, "goto"}, - [OBREAK] = {1, "break"}, - [OCONT] = {1, "cont"}, - [ORETURN] = {1, "return"}, - [OCASE] = {1, "case"}, - [ODEFAULT] = {1, "default"}, - [OFTN] = {1, "function"}, + [OGOTO] = {2, "goto"}, + [OBREAK] = {2, "break"}, + [OCONT] = {2, "cont"}, + [ORETURN] = {2, "return"}, + [OCASE] = {2, "case"}, + [ODEFAULT] = {2, "default"}, + [OFTN] = {2, "function"}, [ODEF] = {2, "def"}, [O2EXP] = { 2, ":"} }; @@ -195,9 +179,6 @@ prtree_helper(register struct node *np) fputs((sym->name) ? sym->name : ".", stdout); return; } - case 1: - prtree_helper(((struct node_op1 *) np)->infix); - break; case 2: prtree_helper(((struct node_op2 *) np)->left); prtree_helper(((struct node_op2 *) np)->rigth);