scc

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

commit 42a80b685648b7cb5c417c00b7cc338dcbefc394
parent 4698e1692871826efbc031b0d2c9991ea8ee6be5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 12 Feb 2015 12:26:07 +0100

Allow that funtors in apply() returns a value

This is needed because some of the funtors can modify the tree, so
it is important that the value stored in the list of trees is modified,
so it is needed to receive some value from the funtors. It will not
be needed in some cases, but then you can return NULL.

Diffstat:
Mcc2/cc2.h | 4++--
Mcc2/cgen.c | 21+++++++++++++++------
Mcc2/parser.c | 6+++---
3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -111,10 +111,10 @@ enum { }; extern void error(unsigned nerror, ...); -extern void genaddable(Node *np); +extern Node *genaddable(Node *np); extern void generate(Symbol *fun); extern void genstack(Symbol *fun); -extern void apply(Node *list[], void (*fun)(Node *)); +extern void apply(Node *list[], Node *(*fun)(Node *)); extern Symbol *parse(void); extern void code(char op, ...); extern void prtree(Node *np); diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -93,7 +93,7 @@ conmute(Node *np) } static void -cgen(Node *np) +cgen(Node *np, Node *parent) { Node *lp, *rp; TINT imm; @@ -161,6 +161,13 @@ cgen(Node *np) } } +static Node * +applycgen(Node *np) +{ + cgen(np, NULL); + return NULL; +} + void generate(Symbol *fun) { @@ -176,7 +183,7 @@ generate(Symbol *fun) code(LD, SP, HL); } - apply(fun->u.f.body, cgen); + apply(fun->u.f.body, applycgen); if (frame) { code(LD, SP, IX); @@ -186,19 +193,21 @@ generate(Symbol *fun) } /* + * This is strongly influenced by + * http://plan9.bell-labs.com/sys/doc/compiler.ps * calculate addresability as follows * AUTO => 11 value+fp * REGISTER => 13 register * STATIC => 12 (value) * CONST => 20 $value */ -void +Node * genaddable(Node *np) { Node *lp, *rp; if (!np) - return; + return np; np->complex = 0; np->addable = 0; @@ -226,7 +235,7 @@ genaddable(Node *np) } if (np->addable > 10) - return; + return np; if (lp) np->complex = lp->complex; if (rp) { @@ -239,5 +248,5 @@ genaddable(Node *np) } if (np->complex == 0) ++np->complex; - return; + return np; } diff --git a/cc2/parser.c b/cc2/parser.c @@ -104,12 +104,12 @@ prtree(Node *np) } void -apply(Node *list[], void (*fun)(Node *)) +apply(Node *list[], Node *(*fun)(Node *)) { Node *np; - while (np = *list++) - (*fun)(np); + while (np = *list) + *list++ = (*fun)(np); } static Symbol *