scc

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

commit aaeec1e16228d8a144d022621786639961912313
parent 8870f51518fcf0af3e34be171e9ebce325b0dbcf
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  5 Oct 2013 20:05:56 +0200

Remove node_comp

Removing this kind of node we simplify the tree a lot, because
we only need detect a type of special node, the symbol node, which
is also the terminal node, so it is something we already have
to detect, so it is great.

Diffstat:
Mdecl.c | 26++++++++++++++------------
Mflow.c | 10++++++----
Msyntax.h | 9+++++++--
Mtree.c | 52++++++++++++----------------------------------------
4 files changed, 39 insertions(+), 58 deletions(-)

diff --git a/decl.c b/decl.c @@ -288,27 +288,29 @@ declarator(struct ctype *tp, unsigned char ns) static struct node * initializer(register struct ctype *tp) { - register struct node *np; - if (accept('{')) { - np = nodecomp(); - addstmt(np, initializer(tp)); + struct compound c; + + nodecomp(&c); + addstmt(&c, initializer(tp)); while (accept(',')) { if (accept('}')) - return np; - addstmt(np, initializer(tp)); + return c.tree; + addstmt(&c, initializer(tp)); } expect('}'); + return c.tree; } else { - np = expr(); + return expr(); } - return np; } static struct node * listdcl(struct ctype *base) { - struct node *lp = nodecomp(); + struct compound c; + + nodecomp(&c); do { struct node *sp, *np; @@ -323,14 +325,14 @@ listdcl(struct ctype *base) sp = nodesym(cursym); if (tp->type == FTN && yytoken == '{') { np = node(ODEF, sp, function(cursym)); - return addstmt(lp, np); + return addstmt(&c, np); } np = node(ODEF, sp, accept('=') ? initializer(tp) : NULL); - lp = addstmt(lp, np); + addstmt(&c, np); } while (accept(',')); expect(';'); - return lp; + return c.tree; } struct node * diff --git a/flow.c b/flow.c @@ -236,17 +236,19 @@ _default(void) static struct node * compound(void) { - register struct node *lp = nodecomp(), *np; + register struct node *np; + struct compound c; + nodecomp(&c); expect('{'); new_ctx(); while (np = decl()) - addstmt(lp, np); + addstmt(&c, np); while (!accept('}')) - addstmt(lp, stmt()); + addstmt(&c, stmt()); del_ctx(); - return lp; + return c.tree; } static struct node * diff --git a/syntax.h b/syntax.h @@ -18,6 +18,11 @@ enum opcode { struct node; struct symbol; +struct compound { + struct node *tree; + struct node_op2 *last; +}; + extern struct node *expr(void); extern struct node *decl(void); extern void type_name(void); @@ -25,8 +30,8 @@ extern struct node *function(struct symbol *sym); extern struct node *node(unsigned char op, struct node *l, struct node *r); extern struct node *nodesym(struct symbol *sym); -extern struct node *nodecomp(void); -extern struct node *addstmt(struct node *np, struct node *stmt); +extern struct node *addstmt(struct compound *p, struct node *np); +extern struct node *addstmt(struct compound *p, struct node *np); extern void prtree(register struct node *np); diff --git a/tree.c b/tree.c @@ -23,12 +23,6 @@ struct nodesym { struct symbol *sym; }; -struct node_comp { - struct node base; - uint8_t nr, alloc; - struct node **body; -}; - static unsigned char indent; /* used for pretty printing the tree*/ @@ -55,37 +49,24 @@ node(unsigned char op, struct node *l, struct node *r) return (struct node *) np; } -struct node * -nodecomp(void) +void +nodecomp(register struct compound *p) { - register struct node_comp *np = xmalloc(sizeof(*np)); - - np->base.op = OCOMP; - np->alloc = np->nr = 0; - np->body = NULL; - - return (struct node *) np; + p->tree = node(OCOMP, NULL, NULL); + p->last = (struct node_op2 *) p->tree; } struct node * -addstmt(struct node *p, struct node *stmt) +addstmt(struct compound *p, struct node *np) { - register uint8_t nr, alloc; - register struct node_comp *np = (struct node_comp *) p; - - assert(np && np->base.op == OCOMP); - nr = ++np->nr, alloc = np->alloc; - -#define alloc_nr(x) ((((x)+16)*3)/2) - if (nr > alloc) { - alloc = alloc_nr(nr); - np->body = xrealloc(np->body, alloc * sizeof(*np->body)); + if (!p->last->left) { + p->last->left = np; + } else { + p->last = (struct node_op2 *) + (p->last->right = node(O2EXP, NULL, np)); } -#undef alloc_nr - np->body[nr - 1] = stmt; - np->alloc = alloc; - return p; + return p->tree; } static void @@ -140,7 +121,7 @@ prtree_helper(register struct node *np) [OA_XOR] = {2, "^="}, [OA_OR] = {2, "|="}, [OSYM] = {0, "sym"}, - [OCOMP] = {255, "comp"}, + [OCOMP] = {2, "comp"}, [OSWITCH] = {2, "switch"}, [OIF] = {2, "if"}, [OFOR] = {2, "for"}, @@ -183,15 +164,6 @@ prtree_helper(register struct node *np) prtree_helper(((struct node_op2 *) np)->left); prtree_helper(((struct node_op2 *) np)->right); break; - case 255: { - register struct node **bp, **lim; - - bp = ((struct node_comp *) np)->body; - lim = bp + ((struct node_comp *) np)->nr; - while (bp < lim) - prtree_helper(*bp++); - break; - } } putchar(')'); indent -= 2;