commit 35f16441611ebbc740dfa103743ad9402d0155c6
parent 1ca89cac46831770fd9fcde016b7c8a1d5ff7e58
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 6 May 2015 18:56:16 +0200
Add emit() function to cc1
emit() will be the unique emit interface to the rest of the code.
This is a first version that only substitute to emitexp(), which
becomes a static function of code.c
Diffstat:
3 files changed, 28 insertions(+), 15 deletions(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -154,6 +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,
/* TODO: This order is important, but must be changed */
OAND, OOR,
/*
@@ -166,7 +167,7 @@ enum {
/*TODO: clean these declarations */
extern void
emitdcl(Symbol *), emitefun(void),
- emitexp(Node *),
+ emit(uint8_t, void *),
emitprint(Node *),
emitsymid(uint8_t op, Symbol *sym),
emitbloop(void), emiteloop(void),
diff --git a/cc1/code.c b/cc1/code.c
@@ -10,7 +10,7 @@
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 *);
+ emitsizeof(uint8_t, void *), emitexp(uint8_t op, void *arg);
char *optxt[] = {
[OADD] = "+",
@@ -99,7 +99,8 @@ void (*opcode[])(uint8_t, void *) = {
[OCAST] = emitcast,
[OSYM] = emitsym,
[OASK] = emitternary,
- [OFIELD]= emitfield
+ [OFIELD]= emitfield,
+ [OEXPR] = emitexp
};
void
@@ -122,6 +123,15 @@ emitnode(Node *np)
}
void
+emit(uint8_t op, void *arg)
+{
+ extern uint8_t failure;
+
+ if (failure)
+ return;
+ (*opcode[op])(op, arg);
+}
+
static void
emitvar(Symbol *sym)
{
@@ -228,9 +238,11 @@ emitsizeof(uint8_t op, void *arg)
printf("\t#%c", np->left->type->letter);
}
-void
-emitexp(Node *np)
+static void
+emitexp(uint8_t op, void *arg)
{
+ Node *np = arg;
+
emitnode(np);
putchar('\n');
}
diff --git a/cc1/stmt.c b/cc1/stmt.c
@@ -51,7 +51,7 @@ stmtexp(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
if (yytoken != ';') {
np = expr();
- emitexp(np);
+ emit(OEXPR, np);
}
expect(';');
@@ -93,7 +93,7 @@ While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
stmt(end, begin, lswitch);
emitsymid(OLABEL, cond);
emitsymid(OBRANCH, begin);
- emitexp(np);
+ emit(OEXPR, np);
emiteloop();
emitsymid(OLABEL, end);
freetree(np);
@@ -118,15 +118,15 @@ For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
einc = (yytoken != ')') ? expr() : NULL;
expect(')');
- emitexp(einit);
+ emit(OEXPR, einit);
emitsymid(OJUMP, cond);
emitbloop();
emitsymid(OLABEL, begin);
stmt(end, begin, lswitch);
- emitexp(einc);
+ emit(OEXPR, einc);
emitsymid(OLABEL, cond);
emitsymid(OBRANCH, begin);
- emitexp(econd);
+ emit(OEXPR, econd);
emiteloop();
emitsymid(OLABEL, end);
freetree(einit);
@@ -149,7 +149,7 @@ Dowhile(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
expect(WHILE);
np = condition();
emitsymid(OBRANCH, begin);
- emitexp(np);
+ emit(OEXPR, np);
emiteloop();
emitsymid(OLABEL, end);
freetree(np);
@@ -175,7 +175,7 @@ Return(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
error("incorrect type in return");
}
emitret(tp);
- emitexp(np);
+ emit(OEXPR, np);
freetree(np);
}
@@ -250,10 +250,10 @@ Switch(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
stmt(lbreak, lcont, &lcase);
emitsymid(OLABEL, lcond);
emitswitch(lcase.nr);
- emitexp(cond);
+ emit(OEXPR, cond);
for (p = lcase.head; p; p = next) {
emitsymid(OCASE, p->label);
- emitexp(p->expr);
+ emit(OEXPR, p->expr);
next = p->next;
freetree(p->expr);
free(p);
@@ -307,7 +307,7 @@ If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
np = condition();
NEGATE(np, 1);
emitsymid(OBRANCH, lelse);
- emitexp(np);
+ emit(OEXPR, np);
stmt(lbreak, lcont, lswitch);
if (accept(ELSE)) {
end = install("", NS_LABEL);