shithub: mc

Download patch

ref: 37bfca6f1d5043a969104037a0dfc4f8956e7c77
parent: 4c23b2a4324da13e2f6027a4f53107a46568373b
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jan 27 17:15:41 EST 2016

Make literal empty slices in blobs zero.

--- a/6/blob.c
+++ b/6/blob.c
@@ -83,6 +83,7 @@
 		break;
 	case Btseq:
 		for (i = 0; i < b->seq.nsub; i++)
+
 			blobfree(b->seq.sub[i]);
 		break;
 	default:
@@ -174,6 +175,7 @@
 {
 	Node *base, *lo, *hi;
 	ssize_t loval, hival, sz;
+	Blob *slbase;
 	char *lbl;
 
 	base = n->expr.args[0];
@@ -183,14 +185,19 @@
 	/* by this point, all slicing operations should have had their bases
 	 * pulled out, and we should have vars with their pseudo-decls in their
 	 * place */
-	if (exprop(base) != Ovar || !base->expr.isconst)
-		fatal(base, "slice base is not a constant value");
 	loval = getintlit(lo, "lower bound in slice is not constant literal");
 	hival = getintlit(hi, "upper bound in slice is not constant literal");
-	sz = tysize(tybase(exprtype(base))->sub[0]);
+	if (exprop(base) == Ovar && base->expr.isconst) {
+		sz = tysize(tybase(exprtype(base))->sub[0]);
+		lbl = htget(globls, base);
+		slbase = mkblobref(lbl, loval*sz, 1);
+	} else if (exprop(base) == Olit) {
+		slbase = mkblobi(Bti64, getintlit(base, "invalid base expr"));
+	} else {
+		fatal(base, "slice base is not a constant value");
+	}
 
-	lbl = htget(globls, base);
-	b(seq, mkblobref(lbl, loval*sz, 1));
+	b(seq, slbase);
 	b(seq, mkblobi(Bti64, (hival - loval)));
 	return 16;
 }
--- a/6/simp.c
+++ b/6/simp.c
@@ -1937,12 +1937,18 @@
 static void extractsub(Simp *s, Node *e)
 {
 	size_t i;
+	Node *sub;
 
 	assert(e != NULL);
 	switch (exprop(e)) {
 	case Oslice:
-		if (exprop(e->expr.args[0]) == Oarr)
+		sub = e->expr.args[0];
+		if (exprop(sub) == Oarr && sub->expr.nargs > 0) {
 			e->expr.args[0] = simpblob(s, e->expr.args[0]);
+		} else  {
+			e->expr.args[0] = mkintlit(e->loc, 0);
+			e->expr.args[0]->expr.type = tyintptr;
+		}
 		break;
 	case Oarr:
 	case Ostruct: