scc

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

commit 8a299a6aad02762c54b653af2551edae7cd62e53
parent a0eb6b42c41d7bf2f7a30374f975f927aa78b1d9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 15 Aug 2012 21:03:10 +0200

Added nodes of type node_comp

These nodes will be used later for the compound blocks, which are a set of
expressions. So this new node has a realloc'ed buffer of childs, instead of
having dedicated pointer for them.

Diffstat:
Msyntax.h | 20+++++++++-----------
Mtree.c | 52+++++++++++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 60 insertions(+), 12 deletions(-)

diff --git a/syntax.h b/syntax.h @@ -10,7 +10,7 @@ enum { OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND, OBXOR, OBOR, OAND, OOR, OTERN, OASSIGN, OA_MUL, OA_DIV, OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, OA_AND, - OA_XOR, OA_OR, OSYM + OA_XOR, OA_OR, OSYM, OCOMP }; struct node; @@ -21,17 +21,15 @@ extern struct node *expr(void); extern unsigned char decl(void); extern void type_name(void); -extern struct node * -node3(unsigned char op, struct node *l, struct node *i, struct node *r); +extern struct node *node3(unsigned char op, + struct node *l, struct node *i, struct node *r); +extern struct node *node2(unsigned char op, struct node *l, struct node *r); +extern struct node *node1(unsigned char op, struct node *i); -extern struct node * -node2(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 *nodesym(struct symbol *sym); +extern struct node *nodecomp(void); +extern struct node *addstmt(struct node *np, struct node *stmt); extern void prtree(register struct node *np); + #endif diff --git a/tree.c b/tree.c @@ -2,6 +2,7 @@ #include <assert.h> #include <stddef.h> #include <stdio.h> +#include <stdint.h> #include "cc.h" #include "syntax.h" @@ -34,6 +35,12 @@ struct node_sym { struct symbol *sym; }; +struct node_comp { + struct node base; + uint8_t nr, alloc; + struct node **body; +}; + struct node * nodesym(struct symbol *sym) { @@ -80,6 +87,39 @@ node1(unsigned char op, struct node *i) return (struct node *) np; } +struct node * +nodecomp(void) +{ + register struct node_comp *np = xmalloc(sizeof(*np)); + + np->base.op = OCOMP; + np->alloc = np->nr = 0; + np->body = NULL; + + return (struct node *) np; +} + +struct node * +addstmt(struct node *p, struct node *stmt) +{ + 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)); + } +#undef alloc_nr + + np->body[nr - 1] = stmt; + np->alloc = alloc; + return p; +} + void prtree(register struct node *np) { @@ -131,7 +171,8 @@ prtree(register struct node *np) [OA_AND] = {2, "&="}, [OA_XOR] = {2, "^="}, [OA_OR] = {2, "|="}, - [OSYM] = {0, "sym"} + [OSYM] = {0, "sym"}, + [OCOMP] = {255, "comp"} }; assert(np && np->op < ARRAY_SIZE(optab)); @@ -157,6 +198,15 @@ prtree(register struct node *np) prtree(((struct node_op3 *) np)->infix); prtree(((struct node_op3 *) np)->rigth); 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(*bp++); + break; + } } putchar(')'); }