scc

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

commit 1b0107f059a35efdbef66890593f2913c6b460ee
parent 3801978b851d9c808f913c22ced53e1e9e3be46b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 20 Mar 2015 05:20:55 +0000

Add optimization for SUB to

SUB accept the same optimizations that ADD, is only to change a
variable.

Diffstat:
cc2/cc2.h | 3++-
cc2/code.c | 6++++--
cc2/peep.c | 5++++-
3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -134,7 +134,8 @@ enum { RET, NOP, INC, - SUB + SUB, + DEC }; enum { diff --git a/cc2/code.c b/cc2/code.c @@ -32,7 +32,8 @@ static void (*instcode[])(void) = { [RET] = inst0, [NOP] = inst0, [INC] = inst1, - [SUB] = inst2 + [SUB] = inst2, + [DEC] = inst1 }; static char *insttext[] = { @@ -47,7 +48,8 @@ static char *insttext[] = { [RET] = "RET", [NOP] = "NOP", [INC] = "INC", - [SUB] = "SUB" + [SUB] = "SUB", + [DEC] = "DEC" }; Inst *pc, *prog; diff --git a/cc2/peep.c b/cc2/peep.c @@ -10,19 +10,22 @@ peephole(void) { Addr to, from; TINT i; + uint8_t op; for (pc = prog; pc; pc = pc->next) { to = pc->to; from = pc->from; switch (pc->op) { + case SUB: case ADD: if (from.kind == CONST) { if ((i = from.u.i) == 0 || i < 4) { delcode(); + op = (pc->op == ADD) ? INC : DEC; while (i--) - inscode(INC, &to, NULL); + inscode(op, &to, NULL); } /* TODO: More optimizations (ex: -1) */ }