summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--lisc/lisc.h25
-rw-r--r--lisc/parse.c13
-rw-r--r--lisc/ssa.c22
3 files changed, 34 insertions, 26 deletions
diff --git a/lisc/lisc.h b/lisc/lisc.h
index a6d4ead..f49aa6f 100644
--- a/lisc/lisc.h
+++ b/lisc/lisc.h
@@ -7,7 +7,6 @@ typedef unsigned short ushort;
 typedef unsigned char uchar;
 
 enum {
-	R       = 0,  /* invalid reference */
 	NReg    = 32,
 	Tmp0    = NReg+1,
 	NString = 32,
@@ -22,19 +21,29 @@ typedef struct Phi Phi;
 typedef struct Blk Blk;
 typedef struct Sym Sym;
 typedef struct Fn Fn;
-typedef ushort Ref;
+typedef struct Ref Ref;
+
+struct Ref {
+	ushort type:1;
+	ushort val:15;
+};
+
+static inline int
+req(Ref a, Ref b)
+{
+	return a.type == b.type && a.val == b.val;
+}
+
+#define R (Ref){0, 0} // Invalid reference
 
 enum {
 	RSym = 0,
 	RConst = 1,
-
-	RMask = 1,
-	RShift = 1,
-	NRef = ((ushort)-1)>>RShift,
+	NRef = ((ushort)-1)>>1,
 };
 
-#define SYM(x)   (((x)<<RShift) | RSym)
-#define CONST(x) (((x)<<RShift) | RConst)
+#define SYM(x)   (Ref){ RSym, x }
+#define CONST(x) (Ref){ RConst, x }
 
 enum {
 	OXXX,
diff --git a/lisc/parse.c b/lisc/parse.c
index 526e55e..0d647c1 100644
--- a/lisc/parse.c
+++ b/lisc/parse.c
@@ -303,7 +303,7 @@ closeblk()
 static PState
 parseline(PState ps)
 {
-	Ref arg[NPred] = {0};
+	Ref arg[NPred] = {R};
 	Blk *blk[NPred];
 	Phi *phi;
 	Ref r;
@@ -311,7 +311,6 @@ parseline(PState ps)
 	Blk *b;
 	int op, i;
 
-	assert(arg[0] == R);
 	do
 		t = next();
 	while (t == TNL);
@@ -347,7 +346,7 @@ parseline(PState ps)
 	case TJez:
 		curb->jmp.type = JJez;
 		r = parseref();
-		if (r == R)
+		if (req(r, R))
 			err("invalid argument for jez jump");
 		curb->jmp.arg = r;
 		expect(TComma);
@@ -400,7 +399,7 @@ parseline(PState ps)
 				blk[i] = findblk(tokval.str);
 			}
 			arg[i] = parseref();
-			if (arg[i] == R)
+			if (req(arg[i], R))
 				err("invalid instruction argument");
 			i++;
 			t = peek();
@@ -475,12 +474,12 @@ parsefn(FILE *f)
 static void
 printref(Ref r, Fn *fn, FILE *f)
 {
-	switch (r&RMask) {
+	switch (r.type) {
 	case RSym:
-		fprintf(f, "%%%s", fn->sym[r>>RShift].name);
+		fprintf(f, "%%%s", fn->sym[r.val].name);
 		break;
 	case RConst:
-		fprintf(f, "%d", r>>RShift);
+		fprintf(f, "%d", r.val);
 		break;
 	}
 }
diff --git a/lisc/ssa.c b/lisc/ssa.c
index d884901..637ac80 100644
--- a/lisc/ssa.c
+++ b/lisc/ssa.c
@@ -90,7 +90,7 @@ botdef(Blk *b, Fn *f)
 {
 	Ref r;
 
-	if (bot[b->id] != R)
+	if (!req(bot[b->id], R))
 		return bot[b->id];
 	r = topdef(b, f);
 	bot[b->id] = r;
@@ -105,7 +105,7 @@ topdef(Blk *b, Fn *f)
 	Ref r;
 	Phi *p;
 
-	if (top[b->id] != R)
+	if (!req(top[b->id], R))
 		return top[b->id];
 	assert(b->npred && "invalid ssa program detected");
 	if (b->npred == 1) {
@@ -150,23 +150,23 @@ ssafix(Fn *f, int t)
 		t1 = 0;
 		/* rename defs and some in-blocks uses */
 		for (p=b->phi; p; p=p->link)
-			if (p->to == rt) {
+			if (req(p->to, rt)) {
 				t1 = f->ntmp++;
 				p->to = SYM(t1);
 			}
 		for (i=b->ins; i-b->ins < b->nins; i++) {
 			if (t1) {
-				if (i->l == rt)
+				if (req(i->l, rt))
 					i->l = SYM(t1);
-				if (i->r == rt)
+				if (req(i->r, rt))
 					i->r = SYM(t1);
 			}
-			if (i->to == rt) {
+			if (req(i->to, rt)) {
 				t1 = f->ntmp++;
 				i->to = SYM(t1);
 			}
 		}
-		if (t1 && b->jmp.arg == rt)
+		if (t1 && req(b->jmp.arg, rt))
 			b->jmp.arg = SYM(t1);
 		top[b->id] = R;
 		bot[b->id] = t1 ? SYM(t1) : R;
@@ -174,15 +174,15 @@ ssafix(Fn *f, int t)
 	for (b=f->start; b; b=b->link) {
 		for (p=b->phi; p; p=p->link)
 			for (n=0; n<p->narg; n++)
-				if (p->arg[n] == rt)
+				if (req(p->arg[n], rt))
 					p->arg[n] = botdef(p->blk[n], f);
 		for (i=b->ins; i-b->ins < b->nins; i++) {
-			if (i->l == rt)
+			if (req(i->l, rt))
 				i->l = topdef(b, f);
-			if (i->r == rt)
+			if (req(i->r, rt))
 				i->r = topdef(b, f);
 		}
-		if (b->jmp.arg == rt)
+		if (req(b->jmp.arg, rt))
 			b->jmp.arg = topdef(b, f);
 	}
 	/* add new symbols */