scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit bce2cfac7f576b3a9afe811fd9a6d71ae18d3cb0
parent 507e3c823892f2f2bac6acd42bab1b079798fa05
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 May 2015 18:04:32 +0200

Unify emitstruct, emitlabel, emitjump, emitbranch, emitcase, emitdefault

Diffstat:
cc1/cc1.h | 10+++++-----
cc1/code.c | 44+++++++++-----------------------------------
cc1/decl.c | 2+-
cc1/stmt.c | 58+++++++++++++++++++++++++++++-----------------------------
4 files changed, 44 insertions(+), 70 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -153,6 +153,7 @@ enum { OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR, OA_AND, OA_XOR, OA_OR, OADDR,ONEG, OCPL, OEXC, OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP, + OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH, /* TODO: This order is important, but must be changed */ OAND, OOR, /* @@ -166,14 +167,13 @@ enum { extern void emitdcl(Symbol *), emitefun(void), emitexp(Node *), - emitprint(Node *), emitlabel(Symbol *), emitjump(Symbol *), - emitbranch(Symbol *sym), + emitprint(Node *), + emitsymid(uint8_t op, Symbol *sym), emitbloop(void), emiteloop(void), - emitswitch(short), emitcase(Symbol *), + emitswitch(short), emitret(Type *tp), emitfun(Symbol *sym), - emitdefault(Symbol *), - emitstruct(Symbol *sym), emitestruct(void); + emitestruct(void); extern Node *node(uint8_t op, Type *tp, Node *left, Node *rigth); extern Node *symbol(Symbol *sym); diff --git a/cc1/code.c b/cc1/code.c @@ -48,7 +48,13 @@ char *optxt[] = { [OCPL] = "~", [OAND] = "y", [OOR] = "o", - [OCOMMA] = "," + [OCOMMA] = ",", + [OLABEL] = "L%d\n", + [ODEFAULT] = "\tf\tL%d\n", + [OCASE] = "\tw\tL%d", + [OSTRUCT] = "S%d\t(\n", + [OJUMP] = "\tj\tL%d\n", + [OBRANCH] = "\tj\tL%d", }; void (*opcode[])(void *) = { @@ -145,12 +151,6 @@ emitconst(Node *np) } void -emitstruct(Symbol *sym) -{ - printf("S%d\t(\n", sym->id); -} - -void emitestruct(void) { puts(")"); @@ -270,12 +270,6 @@ emitret(Type *tp) } void -emitlabel(Symbol *sym) -{ - printf("L%d\n", sym->id); -} - -void emitbloop(void) { puts("\td"); @@ -288,15 +282,9 @@ emiteloop(void) } void -emitjump(Symbol *sym) -{ - printf("\tj\tL%d\n", sym->id); -} - -void -emitbranch(Symbol *sym) +emitsymid(uint8_t op, Symbol *sym) { - printf("\tj\tL%d", sym->id); + printf(optxt[op], sym->id); } void @@ -306,20 +294,6 @@ emitswitch(short nr) } void -emitcase(Symbol *sym) -{ - fputs("\tw\t", stdout); - printf("L%d", sym->id); -} - -void -emitdefault(Symbol *sym) -{ - fputs("\tf\t", stdout); - emitlabel(sym); -} - -void emitfield(void *arg) { Node *np = arg, *lp = np->left; diff --git a/cc1/decl.c b/cc1/decl.c @@ -315,7 +315,7 @@ structdcl(void) if (tagtype->defined) error("redefinition of struct/union '%s'", yytext); tagtype->defined = 1; - emitstruct(tagsym); + emitsymid(OSTRUCT, tagsym); while (!accept('}')) { Type *base, *tp; diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -87,15 +87,15 @@ While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(WHILE); np = condition(); - emitjump(cond); + emitsymid(OJUMP, cond); emitbloop(); - emitlabel(begin); + emitsymid(OLABEL, begin); stmt(end, begin, lswitch); - emitlabel(cond); - emitbranch(begin); + emitsymid(OLABEL, cond); + emitsymid(OBRANCH, begin); emitexp(np); emiteloop(); - emitlabel(end); + emitsymid(OLABEL, end); freetree(np); } @@ -119,16 +119,16 @@ For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(')'); emitexp(einit); - emitjump(cond); + emitsymid(OJUMP, cond); emitbloop(); - emitlabel(begin); + emitsymid(OLABEL, begin); stmt(end, begin, lswitch); emitexp(einc); - emitlabel(cond); - emitbranch(begin); + emitsymid(OLABEL, cond); + emitsymid(OBRANCH, begin); emitexp(econd); emiteloop(); - emitlabel(end); + emitsymid(OLABEL, end); freetree(einit); freetree(econd); freetree(einc); @@ -144,14 +144,14 @@ Dowhile(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) end = install("", NS_LABEL); expect(DO); emitbloop(); - emitlabel(begin); + emitsymid(OLABEL, begin); stmt(end, begin, lswitch); expect(WHILE); np = condition(); - emitbranch(begin); + emitsymid(OBRANCH, begin); emitexp(np); emiteloop(); - emitlabel(end); + emitsymid(OLABEL, end); freetree(np); } @@ -185,7 +185,7 @@ Break(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(BREAK); if (!lbreak) error("break statement not within loop or switch"); - emitjump(lbreak); + emitsymid(OJUMP, lbreak); expect(';'); } @@ -196,7 +196,7 @@ Label(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) { switch (yytoken) { case IDEN: case TYPEIDEN: - emitlabel(label(yytext, 1)); + emitsymid(OLABEL, label(yytext, 1)); next(); expect(':'); stmt(lbreak, lcont, lswitch); @@ -212,7 +212,7 @@ Continue(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(CONTINUE); if (!lcont) error("continue statement not within loop"); - emitjump(lcont); + emitsymid(OJUMP, lcont); expect(';'); } @@ -223,7 +223,7 @@ Goto(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) if (yytoken != IDEN) unexpected(); - emitjump(label(yytext, 0)); + emitsymid(OJUMP, label(yytext, 0)); next(); expect(';'); } @@ -246,21 +246,21 @@ Switch(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) lbreak = install("", NS_LABEL); lcond = install("", NS_LABEL); - emitjump(lcond); + emitsymid(OJUMP, lcond); stmt(lbreak, lcont, &lcase); - emitlabel(lcond); + emitsymid(OLABEL, lcond); emitswitch(lcase.nr); emitexp(cond); for (p = lcase.head; p; p = next) { - emitcase(p->label); + emitsymid(OCASE, p->label); emitexp(p->expr); next = p->next; freetree(p->expr); free(p); } if (lcase.deflabel) - emitdefault(lcase.deflabel); - emitlabel(lbreak); + emitsymid(ODEFAULT, lcase.deflabel); + emitsymid(OLABEL, lbreak); freetree(cond); } @@ -280,7 +280,7 @@ Case(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) pcase = xmalloc(sizeof(*pcase)); pcase->expr = np; pcase->next = lswitch->head; - emitlabel(pcase->label = install("", NS_LABEL)); + emitsymid(OLABEL, pcase->label = install("", NS_LABEL)); lswitch->head = pcase; ++lswitch->nr; } @@ -292,7 +292,7 @@ Default(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(DEFAULT); expect(':'); - emitlabel(ldefault); + emitsymid(OLABEL, ldefault); lswitch->deflabel = ldefault; } @@ -306,17 +306,17 @@ If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(IF); np = condition(); NEGATE(np, 1); - emitbranch(lelse); + emitsymid(OBRANCH, lelse); emitexp(np); stmt(lbreak, lcont, lswitch); if (accept(ELSE)) { end = install("", NS_LABEL); - emitjump(end); - emitlabel(lelse); + emitsymid(OJUMP, end); + emitsymid(OLABEL, lelse); stmt(lbreak, lcont, lswitch); - emitlabel(end); + emitsymid(OLABEL, end); } else { - emitlabel(lelse); + emitsymid(OLABEL, lelse); } freetree(np); }