scc

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

commit 3801978b851d9c808f913c22ced53e1e9e3be46b
parent f957dc886547ffab16c9f743889f51f23726f13a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 19 Mar 2015 20:56:41 +0000

Add SUB instruction

This instruction is really similar to ADD, so basically we only
had to add it.

Diffstat:
Mcc2/cc2.h | 3++-
Mcc2/cgen.c | 14++++++++------
Mcc2/code.c | 6++++--
3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -133,7 +133,8 @@ enum { POP, RET, NOP, - INC + INC, + SUB }; enum { diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -260,7 +260,7 @@ conmute(Node *np) } static void -add(Node *np) +add(Node *np, uint8_t op) { Node *lp = np->left, *rp = np->right; uint8_t i; @@ -321,7 +321,7 @@ add(Node *np) abort(); } add_A: - code(ADD, lp, rp); + code((op == OADD) ? ADD : SUB, lp, rp); np->op = REG; np->reg = A; break; @@ -333,7 +333,7 @@ add(Node *np) } static void -assign(Node *np) +assign(Node *np, uint8_t op) { Node *lp = np->left, *rp = np->right; Symbol *sym = lp->sym; @@ -368,9 +368,9 @@ assign(Node *np) np->reg = rp->reg; } - -static void (*opnodes[])(Node *) = { +static void (*opnodes[])(Node *, uint8_t) = { [OADD] = add, + [OSUB] = add, [OASSIG] = assign }; @@ -378,6 +378,7 @@ static void cgen(Node *np, Node *parent) { Node *lp, *rp; + uint8_t op; if (!np) return; @@ -400,7 +401,8 @@ cgen(Node *np, Node *parent) cgen(p, np); cgen(q, np); } - (*opnodes[np->op])(np); + op = np->op; + (*opnodes[op])(np, op); } static Node * diff --git a/cc2/code.c b/cc2/code.c @@ -31,7 +31,8 @@ static void (*instcode[])(void) = { [POP] = inst1, [RET] = inst0, [NOP] = inst0, - [INC] = inst1 + [INC] = inst1, + [SUB] = inst2 }; static char *insttext[] = { @@ -45,7 +46,8 @@ static char *insttext[] = { [POP] = "POP", [RET] = "RET", [NOP] = "NOP", - [INC] = "INC" + [INC] = "INC", + [SUB] = "SUB" }; Inst *pc, *prog;