scc

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

commit 507e3c823892f2f2bac6acd42bab1b079798fa05
parent 9d1c91396f9fa249332adf91de26cb7f98a60f88
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 May 2015 17:05:11 +0200

Makes all emit*() only one parameter

We want to convert all the emit functions in only one function,
so the first step to become them more uniform, because in some
moment we will transform them into opcode[] elements.

Diffstat:
Mcc1/cc1.h | 5+++--
Mcc1/code.c | 18+++++++++---------
Mcc1/stmt.c | 32+++++++++++++++++++-------------
3 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -166,9 +166,10 @@ enum { extern void emitdcl(Symbol *), emitefun(void), emitexp(Node *), - emitprint(Node *), emitlabel(Symbol *), emitjump(Symbol *, Node *), + emitprint(Node *), emitlabel(Symbol *), emitjump(Symbol *), + emitbranch(Symbol *sym), emitbloop(void), emiteloop(void), - emitswitch(short, Node *), emitcase(Symbol *, Node *), + emitswitch(short), emitcase(Symbol *), emitret(Type *tp), emitfun(Symbol *sym), emitdefault(Symbol *), diff --git a/cc1/code.c b/cc1/code.c @@ -288,28 +288,28 @@ emiteloop(void) } void -emitjump(Symbol *sym, Node *np) +emitjump(Symbol *sym) +{ + printf("\tj\tL%d\n", sym->id); +} + +void +emitbranch(Symbol *sym) { printf("\tj\tL%d", sym->id); - if (!np) - putchar('\n'); - else - emitexp(np); } void -emitswitch(short nr, Node *np) +emitswitch(short nr) { printf("\teI\t#%0x", nr); - emitexp(np); } void -emitcase(Symbol *sym, Node *np) +emitcase(Symbol *sym) { fputs("\tw\t", stdout); printf("L%d", sym->id); - emitexp(np); } void diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -87,12 +87,13 @@ While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(WHILE); np = condition(); - emitjump(cond, NULL); + emitjump(cond); emitbloop(); emitlabel(begin); stmt(end, begin, lswitch); emitlabel(cond); - emitjump(begin, np); + emitbranch(begin); + emitexp(np); emiteloop(); emitlabel(end); freetree(np); @@ -118,13 +119,14 @@ For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(')'); emitexp(einit); - emitjump(cond, NULL); + emitjump(cond); emitbloop(); emitlabel(begin); stmt(end, begin, lswitch); emitexp(einc); emitlabel(cond); - emitjump(begin, econd); + emitbranch(begin); + emitexp(econd); emiteloop(); emitlabel(end); freetree(einit); @@ -146,7 +148,8 @@ Dowhile(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) stmt(end, begin, lswitch); expect(WHILE); np = condition(); - emitjump(begin, np); + emitbranch(begin); + emitexp(np); emiteloop(); emitlabel(end); freetree(np); @@ -182,7 +185,7 @@ Break(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(BREAK); if (!lbreak) error("break statement not within loop or switch"); - emitjump(lbreak, NULL); + emitjump(lbreak); expect(';'); } @@ -209,7 +212,7 @@ Continue(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(CONTINUE); if (!lcont) error("continue statement not within loop"); - emitjump(lcont, NULL); + emitjump(lcont); expect(';'); } @@ -220,7 +223,7 @@ Goto(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) if (yytoken != IDEN) unexpected(); - emitjump(label(yytext, 0), NULL); + emitjump(label(yytext, 0)); next(); expect(';'); } @@ -243,12 +246,14 @@ Switch(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) lbreak = install("", NS_LABEL); lcond = install("", NS_LABEL); - emitjump(lcond, NULL); + emitjump(lcond); stmt(lbreak, lcont, &lcase); emitlabel(lcond); - emitswitch(lcase.nr, cond); + emitswitch(lcase.nr); + emitexp(cond); for (p = lcase.head; p; p = next) { - emitcase(p->label, p->expr); + emitcase(p->label); + emitexp(p->expr); next = p->next; freetree(p->expr); free(p); @@ -301,11 +306,12 @@ If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(IF); np = condition(); NEGATE(np, 1); - emitjump(lelse, np); + emitbranch(lelse); + emitexp(np); stmt(lbreak, lcont, lswitch); if (accept(ELSE)) { end = install("", NS_LABEL); - emitjump(end, NULL); + emitjump(end); emitlabel(lelse); stmt(lbreak, lcont, lswitch); emitlabel(end);