shithub: scc

Download patch

ref: 1b0107f059a35efdbef66890593f2913c6b460ee
parent: 3801978b851d9c808f913c22ced53e1e9e3be46b
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Mar 20 01:20:55 EDT 2015

Add optimization for SUB to

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

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -134,7 +134,8 @@
 	RET,
 	NOP,
 	INC,
-	SUB
+	SUB,
+	DEC
 };
 
 enum {
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -32,7 +32,8 @@
 	[RET] = inst0,
 	[NOP] = inst0,
 	[INC] = inst1,
-	[SUB] = inst2
+	[SUB] = inst2,
+	[DEC] = inst1
 };
 
 static char *insttext[] = {
@@ -47,7 +48,8 @@
 	[RET] = "RET",
 	[NOP] = "NOP",
 	[INC] = "INC",
-	[SUB] = "SUB"
+	[SUB] = "SUB",
+	[DEC] = "DEC"
 };
 
 Inst *pc, *prog;
--- a/cc2/peep.c
+++ b/cc2/peep.c
@@ -10,6 +10,7 @@
 {
 	Addr to, from;
 	TINT i;
+	uint8_t op;
 
 	for (pc = prog; pc; pc = pc->next) {
 		to = pc->to;
@@ -16,13 +17,15 @@
 		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) */
 			}