shithub: scc

Download patch

ref: a512be02d9c534110deafcce1e73f452a0fa9068
parent: 9e7c2e2a2707f5db37322bf67f7eccf778d42136
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Apr 14 05:43:30 EDT 2014

Add ary2ptr()

Each time the name of an array is present in some expression
that is equivalent to a pointer to the first element of the array,
so this expression is going to happen in different places. So
it is a good candidate for being a function.

--- a/cc.h
+++ b/cc.h
@@ -223,7 +223,7 @@
 	OSHR, OLT, OGT, OGE, OLE, OEQ, ONE, OBAND,
 	OBXOR, OBOR, OASSIGN, OA_MUL, OA_DIV,
 	OA_MOD, OA_ADD, OA_SUB, OA_SHL, OA_SHR,
-	OA_AND, OA_XOR, OA_OR
+	OA_AND, OA_XOR, OA_OR, OADDR
 };
 
 extern void
--- a/code.c
+++ b/code.c
@@ -37,7 +37,8 @@
 	[OA_SHR] = ":r",
 	[OA_AND] = ":&",
 	[OA_XOR] = ":^",
-	[OA_OR] = ":|"
+	[OA_OR] = ":|",
+	[OADDR] = "a"
 };
 
 Node *
--- a/expr.c
+++ b/expr.c
@@ -37,6 +37,16 @@
 	return bincode(op, np1->type, np1, np2);
 }
 
+static Node *
+ary2ptr(Node *np)
+{
+	Type *tp;
+
+	tp = UNQUAL(np->type);
+	tp = mktype(tp->type, PTR, NULL, 0);
+	return unarycode(OADDR, tp, np);
+}
+
 /*
  * Convert a Node to a type
  */
@@ -109,15 +119,16 @@
 			goto incorrect;
 		}
 		break;
-	case PTR: case ARY:
+	case ARY:
+		np1 = ary2ptr(np1);
+		tp1 = np1->type;
+	case PTR:
 pointer:
 		switch (op) {
 		case OADD: case OSUB:
-			tp3 = tp1->type; /* TODO: I think tp3 is not needed */
-			if (!tp1->defined)
+			tp3 = tp1->type;
+			if (!tp3->defined)
 				goto nocomplete;
-			if (t1 == ARY)
-				tp1 = mktype(tp1->type, PTR, NULL, 0);
 			if (t2 != INT)
 				goto incorrect;
 			np2 = bincode(OMUL, tp1,
@@ -178,6 +189,8 @@
 			goto incompatibles;
 		}
 	case ARY:
+		np1 = ary2ptr(np1);
+		break;
 	case FTN:
 		/* TODO: cover this cases */
 	case PTR: