scc

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

commit 061575df4ee33bf59cd7ab7f82af38798391bd53
parent 23597ee476bd27f80b8f3f88c7ba5811ee2e94b7
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 21 Mar 2015 06:22:44 -0400

Add neg and cpl operations

These unary operations are very similar between them,
so it was easy to implement them together.

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

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -139,7 +139,9 @@ enum { JP, AND, OR, - XOR + XOR, + CPL, + NEG }; enum { diff --git a/cc2/cgen.c b/cc2/cgen.c @@ -250,6 +250,24 @@ move(Node *np, Node *parent) case OASSIG: allocreg(np); break; + case ONEG: + case OCPL: + switch (np->op) { + case REG: + if (np->reg == A) + break; + /* PASSTHROUGH */ + case PAR: + case AUTO: + case CONST: + case MEM: + case INDEX: + accum(np); + break; + default: + abort(); + } + break; case OADD: case OSUB: case OBAND: @@ -394,6 +412,39 @@ nop(Node *np) { } +static void +cpl(Node *np) +{ + + Node *lp = np->left; + uint8_t op; + + switch (np->type.size) { + case 1: + move(lp, np); + switch (np->op) { + case OCPL: + op = CPL; + break; + case ONEG: + op = NEG; + break; + default: + abort(); + } + code(op, lp, NULL); + if ((np->op = lp->op) == REG) { + np->reg = lp->reg; + reguse[A] = np; + } + np->sym = lp->sym; + lp->used = 1; + break; + default: + abort(); + } +} + static void (*opnodes[])(Node *) = { [OADD] = add, [OSUB] = add, @@ -406,7 +457,9 @@ static void (*opnodes[])(Node *) = { [PAR] = nop, [OBOR] = add, [OBAND] = add, - [OBXOR] = add + [OBXOR] = add, + [OCPL] = cpl, + [ONEG] = cpl }; static void diff --git a/cc2/code.c b/cc2/code.c @@ -37,7 +37,10 @@ static void (*instcode[])(void) = { [JP] = inst1, [AND] = inst2, [OR] = inst2, - [XOR] = inst2 + [XOR] = inst2, + [CPL] = inst1, + [NEG] = inst1 + }; static char *insttext[] = { @@ -57,7 +60,9 @@ static char *insttext[] = { [JP] = "JP", [AND] = "AND", [OR] = "OR", - [XOR] = "XOR" + [XOR] = "XOR", + [CPL] = "CPL", + [NEG] = "NEG" }; Inst *pc, *prog;