shithub: scc

Download patch

ref: 3801978b851d9c808f913c22ced53e1e9e3be46b
parent: f957dc886547ffab16c9f743889f51f23726f13a
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Mar 19 16:56:41 EDT 2015

Add SUB instruction

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

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -133,7 +133,8 @@
 	POP,
 	RET,
 	NOP,
-	INC
+	INC,
+	SUB
 };
 
 enum {
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -260,7 +260,7 @@
 }
 
 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 @@
 			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 @@
 }
 
 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 @@
 	np->reg = rp->reg;
 }
 
-
-static void (*opnodes[])(Node *) = {
+static void (*opnodes[])(Node *, uint8_t) = {
 	[OADD] = add,
+	[OSUB] = add,
 	[OASSIG] = assign
 };
 
@@ -378,6 +378,7 @@
 cgen(Node *np, Node *parent)
 {
 	Node *lp, *rp;
+	uint8_t op;
 
 	if (!np)
 		return;
@@ -400,7 +401,8 @@
 		cgen(p, np);
 		cgen(q, np);
 	}
-	(*opnodes[np->op])(np);
+	op = np->op;
+	(*opnodes[op])(np, op);
 }
 
 static Node *
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -31,7 +31,8 @@
 	[POP] = inst1,
 	[RET] = inst0,
 	[NOP] = inst0,
-	[INC] = inst1
+	[INC] = inst1,
+	[SUB] = inst2
 };
 
 static char *insttext[] = {
@@ -45,7 +46,8 @@
 	[POP] = "POP",
 	[RET] = "RET",
 	[NOP] = "NOP",
-	[INC] = "INC"
+	[INC] = "INC",
+	[SUB] = "SUB"
 };
 
 Inst *pc, *prog;