shithub: scc

Download patch

ref: c2698edb9fdaada886594ef754dad5cb79de737a
parent: 0eef6fc79e97b9097d8bc5a1f0e93aa4248649b9
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Sep 10 16:53:37 EDT 2014

Simplify variable function

Setting a function for every kind of variable we can simplify
a lot variable(), because there is no switch.

--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -176,40 +176,25 @@
 	}
 }
 
+static Symbol *
+symbol(uint8_t t, char *token)
+{
+	static Symbol *(*tbl[3])(char *)= {
+		[LOCAL] = local,
+		[GLOBAL] = global,
+		[PARAMETER] = parameter
+	};
+	return (*tbl[t])(token);
+}
+
 static void
-variable(char *token)
+variable(uint8_t t, char *token)
 {
-	Symbol *sym;
-	char op, public = 0;
 	Node *np = newnode();
+	Symbol *sym = symbol(t, token);
 
-	switch (token[0]) {
-	case 'T':
-		op = MEM;
-		goto local;
-	case 'P':
-		op = AUTO;
-		goto local;
-	case 'A':
-		op = AUTO;
-		goto local;
-	case 'R':
-		op = REG;
-	local:
-		sym = local(token);
-		break;
-	case 'X':
-		/* TODO */
-	case 'Y':
-	case 'G':
-	global:
-		sym = global(token);
-		op = MEM;
-		break;
-	}
-
 	np->u.sym = sym;
-	np->op = op;
+	np->op = sym->u.v.sclass;
 	np->type = sym->u.v.type;
 	np->left = np->right = NULL;
 	push(np);
@@ -216,6 +201,24 @@
 }
 
 static void
+localvar(char *token)
+{
+	variable(LOCAL, token);
+}
+
+static void
+globvar(char *token)
+{
+	variable(GLOBAL, token);
+}
+
+static void
+paramvar(char *token)
+{
+	variable(PARAMETER, token);
+}
+
+static void
 immediate(char *token)
 {
 	Node *np = newnode();
@@ -312,10 +315,11 @@
 	['^'] = operator,
 	[':'] = assignment,
 	[';'] = increment,
-	['Y'] = variable,
-	['A'] = variable,
-	['T'] = variable,
-	['G'] = variable,
+	['Y'] = globvar,
+	['A'] = localvar,
+	['T'] = localvar,
+	['G'] = globvar,
+	['P'] = paramvar,
 	['L'] = label,
 	['#'] = immediate,
 	['@'] = unary,
@@ -383,12 +387,7 @@
 static Symbol *
 declaration(uint8_t t, char class, char *token)
 {
-	static Symbol *(*tbl[3])(char *)= {
-		[LOCAL] = local,
-		[GLOBAL] = global,
-		[PARAMETER] = parameter
-	};
-	Symbol *sym = (*tbl[t])(token);
+	Symbol *sym = symbol(t, token);
 	char *s;
 
 	if (t == LOCAL && !curfun)