shithub: scc

Download patch

ref: a90c987aa4bf133d6b484e26684919f482ffad73
parent: b5fe10c6fb6606d271bc75e2245a801286f91bb4
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Mar 18 06:31:41 EDT 2015

Add prev pointer in Inst

We are going to add and remove elements from the list, so it is a good
idea to have a pointer to the previous element, because with this data
structure is easy to remove elements from it.

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -85,6 +85,25 @@
 	struct node *left, *right;
 };
 
+typedef struct inst Inst;
+typedef struct addr Addr;
+
+struct addr {
+	char kind;
+	union {
+		uint8_t reg;
+		TINT i;
+		Inst *pc;
+		Symbol *sym;
+	} u;
+};
+
+struct inst {
+	char op;
+	Addr from, to;
+	Inst *next, *prev;
+};
+
 enum nerrors {
 	EINTNUM,       /* too much internal identifiers */
 	EEXTNUM,       /* too much external identifiers */
@@ -101,8 +120,6 @@
 	ENUMERR
 };
 
-
-
 enum {
 	LDW,
 	LDL,
@@ -124,6 +141,7 @@
 };
 
 extern Symbol *curfun;
+extern Inst *prog, *pc;
 
 /* main.c */
 extern void error(unsigned nerror, ...);
--- a/cc2/code.c
+++ b/cc2/code.c
@@ -48,26 +48,8 @@
 	[INC] = "INC"
 };
 
-typedef struct inst Inst;
-typedef struct addr Addr;
+Inst *pc, *prog;
 
-struct addr {
-	char kind;
-	union {
-		uint8_t reg;
-		TINT i;
-		Inst *pc;
-		Symbol *sym;
-	} u;
-};
-
-struct inst {
-	char op;
-	Addr from, to;
-	Inst *next;
-};
-Inst *prog, *pc;
-
 Inst *
 nextpc(void)
 {
@@ -74,6 +56,8 @@
 	Inst *new;
 
 	new = xmalloc(sizeof(*new));
+	new->prev = pc;
+
 	if (!pc)
 		prog = new;
 	else