shithub: scc

Download patch

ref: be7c5f83d4655f77d592b626a702dd2da07c3fb8
parent: ee42812835eb35735b73cc2f4588a8c25326355b
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Jul 17 14:05:07 EDT 2014

Add support for labels in cc2

Labels are the base for all the loops and jumps, so they are strictly
necessary.

--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -11,8 +11,15 @@
 #define NR_SYMBOLS 1024
 
 typedef struct {
-	char vartype;
-	char storage;
+	union {
+		struct {
+			char type;
+			char storage;
+		} v;
+		struct {
+			short addr;
+		} l;
+	} u;
 } Symbol;
 
 typedef struct node {
@@ -96,7 +103,7 @@
 
 	np->op = op;
 	np->u.id = id;
-	np->type = symtbl[id].vartype;
+	np->type = symtbl[id].u.v.type;
 	np->left = np->right = NULL;
 	push(np);
 }
@@ -125,6 +132,16 @@
 	push(np);
 }
 
+static void
+label(char op)
+{
+	Node *np = newnode();
+
+	np->left = np->right = NULL;
+	np->op = 'L';
+	push(np);
+}
+
 static Node *
 getroot(void)
 {
@@ -142,6 +159,7 @@
 	['A'] = variable,
 	['T'] = variable,
 	['G'] = variable,
+	['L'] = label,
 	['#'] = immediate,
 	['\177'] = NULL
 };
@@ -171,12 +189,20 @@
 	Symbol *sym = &symtbl[getid()];
 
 	getchar(); /* skip tab */
-	sym->storage = sclass;
-	sym->vartype = gettype();
+	sym->u.v.storage = sclass;
+	sym->u.v.type = gettype();
 	if (getchar() != '\n')
 		esyntax();
 }
 
+static void
+deflabel(void)
+{
+	Symbol *sym = &symtbl[getid()];
+
+	sym->u.l.addr = listp - listexp;
+}
+
 int
 parse(void)
 {
@@ -188,7 +214,7 @@
 			expression();
 			break;
 		case 'L':
-			/* label */
+			deflabel();
 			break;
 		case 'S':
 			/* struct */