shithub: scc

Download patch

ref: afa081af77e19c67b8ded5203f8bed341af1cba6
parent: 63cde7ec0b3cc2c47cf7338ce3cc63b2591b473c
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Aug 8 07:17:42 EDT 2014

Simplify generation of addresability numbers

This code is inspired in the code written by Ken Thompson, so
it is a good idea follow the rules he wrote in 8c (which in
spirit is similar to z80).

--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -14,8 +14,8 @@
 typedef struct node {
 	char op;
 	char type;
-	int8_t sethi;
-	int8_t addrtype;
+	int8_t complex;
+	int8_t addable;
 	union {
 		Symbol *sym;
 		int imm;
@@ -41,4 +41,4 @@
 #define STATIC 'S'
 
 extern void error(unsigned nerror, ...);
-
+extern void genaddable(Node *np);
--- a/cc2/cgen.c
+++ b/cc2/cgen.c
@@ -5,49 +5,24 @@
 
 #include "cc2.h"
 
-struct addrtype {
-	char op;
-	char left;
-	char right;
-	char addrtype;
-} addrtbl[] = {
-	{'A', 0, 0, 1},
-	{'#', 0, 0, 2},
-	{'+', 1, 2, 3},
-	{0},
-};
-
-struct nodeattr {
-	char addrtype;
-	char sethi;
-};
-
-struct nodeattr
-genaddr(Node *np)
+void
+genaddable(Node *np)
 {
-	struct nodeattr left, right;
-	struct addrtype *bp;
+	if (!np)
+		return;
 
-	left = (np->left) ? genaddr(np->left) : (struct nodeattr) {0, -1};
-	right = (np->right) ? genaddr(np->right) : (struct nodeattr) {0, -1};
-
-	for (bp = addrtbl; bp->op; ++bp) {
-		if (bp->op == np->op &&
-		    left.addrtype == bp->left &&
-		    right.addrtype == bp->right) {
-			break;
-		}
+	np->complex = 0;
+	np->addable = 0;
+	switch (np->op) {
+	case AUTO:
+		np->addable = 11;
+		break;
+	case REGISTER:
+		np->addable = 13;
+		break;
+	case STATIC:
+		np->addable = 12;
+		break;
 	}
-
-	if ((np->addrtype = bp->addrtype) == 0) {
-		np->sethi = 0;
-	} else if (right.sethi < 0) {
-		np->sethi = (left.sethi > 1) ? left.sethi : 1;
-	} else  {
-		int8_t d = left.sethi - right.sethi;
-		np->sethi = ((d < 0) ? right.sethi : left.sethi) + 1;
-	}
-
-	return (struct nodeattr) {np->addrtype, np->sethi};
+	return;
 }
-
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -277,7 +277,7 @@
 			break;
 		case 'X':
 			function();
-			genaddr(listexp[0]);
+			genaddable(listexp[0]);
 			break;
 		case EOF:
 			return;