scc

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

commit ecd4d6b2a8336c5afbfaf469ada838d850755234
parent 97f296d2d458fd1a887b903aec6b6dd72c64efa2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 May 2015 19:19:19 +0200

Remove emiteloop, emitbloop, emitestruct, emitefun

These functions can be unified.

Diffstat:
Mcc1/cc1.h | 8+++-----
Mcc1/code.c | 36+++++++++++++-----------------------
Mcc1/decl.c | 4++--
Mcc1/stmt.c | 12++++++------
4 files changed, 24 insertions(+), 36 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -154,7 +154,7 @@ enum { OA_AND, OA_XOR, OA_OR, OADDR,ONEG, OCPL, OEXC, OCOMMA, OCAST, OSYM, OASK, OFIELD, OTYP, OLABEL, ODEFAULT, OCASE, OSTRUCT, OJUMP, OBRANCH, - OEXPR, + OEXPR, OEFUN, OESTRUCT, OELOOP, OBLOOP, /* TODO: This order is important, but must be changed */ OAND, OOR, /* @@ -166,14 +166,12 @@ enum { /*TODO: clean these declarations */ extern void - emitdcl(Symbol *), emitefun(void), + emitdcl(Symbol *), emit(uint8_t, void *), emitprint(Node *), - emitbloop(void), emiteloop(void), emitswitch(short), emitret(Type *tp), - emitfun(Symbol *sym), - emitestruct(void); + emitfun(Symbol *sym); 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 @@ -10,8 +10,8 @@ static void emitbin(uint8_t, void *), emitunary(uint8_t, void *), emitternary(uint8_t, void *), emitcast(uint8_t, void *), emitsym(uint8_t, void *), emitfield(uint8_t, void *), - emitsizeof(uint8_t, void *), emitexp(uint8_t op, void *arg), - emitsymid(uint8_t op, void *arg); + emitsizeof(uint8_t, void *), emitexp(uint8_t, void *), + emitsymid(uint8_t, void *), emittext(uint8_t, void *); char *optxt[] = { [OADD] = "+", @@ -57,6 +57,10 @@ char *optxt[] = { [OSTRUCT] = "S%d\t(\n", [OJUMP] = "\tj\tL%d\n", [OBRANCH] = "\tj\tL%d", + [OEFUN] = "}", + [OESTRUCT] = ")", + [OELOOP] = "\tb", + [OBLOOP] = "\td" }; void (*opcode[])(uint8_t, void *) = { @@ -108,6 +112,10 @@ void (*opcode[])(uint8_t, void *) = { [OSTRUCT] = emitsymid, [OJUMP] = emitsymid, [OBRANCH] = emitsymid, + [OEFUN] = emittext, + [OESTRUCT] = emittext, + [OELOOP] = emittext, + [OBLOOP] = emittext }; void @@ -175,12 +183,6 @@ emitconst(Node *np) } void -emitestruct(void) -{ - puts(")"); -} - -void emitsym(uint8_t op, void *arg) { Node *np = arg; @@ -268,28 +270,16 @@ emitfun(Symbol *sym) } void -emitefun(void) -{ - puts("}"); -} - -void emitret(Type *tp) { fputs("\ty", stdout); emittype(tp); } -void -emitbloop(void) -{ - puts("\td"); -} - -void -emiteloop(void) +static void +emittext(uint8_t op, void *arg) { - puts("\tb"); + puts(optxt[op]); } static void diff --git a/cc1/decl.c b/cc1/decl.c @@ -351,7 +351,7 @@ structdcl(void) expect(';'); } - emitestruct(); + emit(OESTRUCT, NULL); if ((n = bp - buff) != 0) { siz = sizeof(Type *) * n; tagtype->n.elem = n; @@ -538,7 +538,7 @@ extdecl(void) curfun = sym; emitfun(sym); compound(NULL, NULL, NULL); - emitefun(); + emit(OEFUN, NULL); popctx(); return; } diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -88,13 +88,13 @@ While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) expect(WHILE); np = condition(); emit(OJUMP, cond); - emitbloop(); + emit(OBLOOP, NULL); emit(OLABEL, begin); stmt(end, begin, lswitch); emit(OLABEL, cond); emit(OBRANCH, begin); emit(OEXPR, np); - emiteloop(); + emit(OELOOP, NULL); emit(OLABEL, end); freetree(np); } @@ -120,14 +120,14 @@ For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) emit(OEXPR, einit); emit(OJUMP, cond); - emitbloop(); + emit(OBLOOP, NULL); emit(OLABEL, begin); stmt(end, begin, lswitch); emit(OEXPR, einc); emit(OLABEL, cond); emit(OBRANCH, begin); emit(OEXPR, econd); - emiteloop(); + emit(OELOOP, NULL); emit(OLABEL, end); freetree(einit); freetree(econd); @@ -143,14 +143,14 @@ Dowhile(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) begin = install("", NS_LABEL); end = install("", NS_LABEL); expect(DO); - emitbloop(); + emit(OBLOOP, NULL); emit(OLABEL, begin); stmt(end, begin, lswitch); expect(WHILE); np = condition(); emit(OBRANCH, begin); emit(OEXPR, np); - emiteloop(); + emit(OELOOP, NULL); emit(OLABEL, end); freetree(np); }