ref: a9512a951e1cf0c28a90c5c39cbbb5a3a8e68629
parent: 8b1cf3fd57e7c94f6c162027e440ceb3e50ee50f
author: Ori Bernstein <[email protected]>
date: Sun Jul 15 20:11:18 EDT 2012
Move away from literals for tuples.
--- a/8/isel.c
+++ b/8/isel.c
@@ -560,7 +560,8 @@
case Osubeq: case Omuleq: case Odiveq: case Omodeq: case Oboreq:
case Obandeq: case Obxoreq: case Obsleq: case Obsreq: case Omemb:
case Oslice: case Oidx: case Osize: case Numops:
- case Oslbase: case Osllen: case Ocast: case Ocons:
+ case Ocons: case Otup: case Oarr:
+ case Oslbase: case Osllen: case Ocast:
dump(n, stdout);
die("Should not see %s in isel", opstr(exprop(n)));
break;
@@ -751,8 +752,6 @@
}
}
-void breakhere(){}
-
static void writelit(FILE *fd, Node *v)
{
char lbl[128];
@@ -769,10 +768,6 @@
fprintf(fd, "%s:\n", lbl);
writeblob(fd, v->lit.strval, strlen(v->lit.strval));
break;
- case Ltup:
- for (i = 0; i < v->lit.nelt; i++)
- writelit(fd, v->lit.tupval[i]->expr.args[0]);
- break;
case Lseq:
for (i = 0; i < v->lit.nelt; i++)
writelit(fd, v->lit.seqval[i]->expr.args[0]);
--- a/8/simp.c
+++ b/8/simp.c
@@ -740,6 +740,34 @@
return tmp;
}
+Node *assign(Simp *s, Node *lhs, Node *rhs)
+{
+ Node *t, *u, *v, *r;
+
+ if (exprop(lhs) == Otup) {
+ /* destructuring bind */
+ die("No destructuring binds implemented yet");
+ r = NULL;
+ } else {
+ t = lval(s, lhs);
+ u = rval(s, rhs, t);
+
+ /* if we stored the result into t, rval() should return that,
+ * so we know our work is done. */
+ if (u == t) {
+ r = t;
+ } else if (size(lhs) > Wordsz) {
+ t = addr(t, exprtype(lhs));
+ u = addr(u, exprtype(lhs));
+ v = word(lhs->line, size(lhs));
+ r = mkexpr(lhs->line, Oblit, t, u, v, NULL);
+ } else {
+ r = store(t, u);
+ }
+ }
+ return r;
+}
+
static Node *rval(Simp *s, Node *n, Node *dst)
{
Node *r; /* expression result */
@@ -845,7 +873,7 @@
case Lchr: case Lbool: case Lint:
r = n;
break;
- case Lstr: case Lseq: case Ltup: case Lflt:
+ case Lstr: case Lseq: case Lflt:
r = bloblit(s, n);
break;
case Lfunc:
@@ -871,21 +899,7 @@
jmp(s, s->endlbl);
break;
case Oasn:
- t = lval(s, args[0]);
- u = rval(s, args[1], t);
-
- /* if we stored the result into t, rval() should return that,
- * and we know our work is done. */
- if (u == t) {
- r = t;
- } else if (size(n) > Wordsz) {
- t = addr(t, exprtype(n));
- u = addr(u, exprtype(n));
- v = word(n->line, size(n));
- r = mkexpr(n->line, Oblit, t, u, v, NULL);
- } else {
- r = store(t, u);
- }
+ r = assign(s, args[0], args[1]);
break;
case Ocall:
if (exprtype(n)->type != Tyvoid && size(n) > Wordsz) {
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -178,11 +178,6 @@
for (i = 0; i < n->lit.nelt; i++)
outnode(n->lit.seqval[i], fd, depth+1);
break;
- case Ltup:
- fprintf(fd, " Ltup\n");
- for (i = 0; i < n->lit.nelt; i++)
- outnode(n->lit.tupval[i], fd, depth+1);
- break;
}
break;
case Nfunc:
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -505,7 +505,7 @@
| Toparen expr Tcparen
{$$ = $2;}
| Toparen tupbody Tcparen
- {$$ = mkexpr($1->line, Olit, mktuple($1->line, $2.nl, $2.nn), NULL);}
+ {$$ = mkexprl($1->line, Otup, $2.nl, $2.nn);}
| Tsizeof Toparen type Tcparen
{$$ = mkexpr($1->line, Osize, mkpseudodecl($3), NULL);}
;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -176,7 +176,6 @@
case Lstr: return mktyslice(n->line, mkty(n->line, Tychar)); break;
case Lfunc: return n->lit.fnval->func.type; break;
case Lseq: return NULL; break;
- case Ltup: return NULL; break;
};
die("Bad lit type %d", n->lit.littype);
return NULL;
@@ -386,27 +385,14 @@
settype(n, mktyarray(n->line, type(n->lit.seqval[0]), mkintlit(n->line, n->lit.nelt)));
}
-static void infertup(Node *n)
-{
- size_t i;
- Type **t;
-
- t = xalloc(sizeof(Type *)*n->lit.nelt);
- for (i = 0; i < n->lit.nelt; i++) {
- infernode(n->lit.tupval[i], NULL, NULL);
- t[i] = type(n->lit.tupval[i]);
- }
- settype(n, mktytuple(n->line, t, n->lit.nelt));
-}
-
static void inferexpr(Node *n, Type *ret, int *sawret)
{
Node **args;
+ Type **types;
+ size_t i, nargs;
Ucon *uc;
- int nargs;
Node *s;
Type *t;
- int i;
assert(n->type == Nexpr);
args = n->expr.args;
@@ -554,11 +540,21 @@
unify(n, uc->etype, type(args[1]));
settype(n, uc->utype);
break;
+ case Otup:
+ types = xalloc(sizeof(Type *)*n->expr.nargs);
+ for (i = 0; i < n->expr.nargs; i++)
+ types[i] = type(n->expr.args[i]);
+ settype(n, mktytuple(n->line, types, n->lit.nelt));
+ break;
+ case Oarr:
+ for (i = 0; i < n->expr.nargs; i++)
+ unify(n, type(n->expr.args[0]), type(n->expr.args[i]));
+ settype(n, mktyarray(n->line, type(n->expr.args[0]), mkintlit(n->line, n->expr.nargs)));
+ break;
case Olit: /* <lit>:@a::tyclass -> @a */
switch (args[0]->lit.littype) {
case Lfunc: infernode(args[0]->lit.fnval, NULL, NULL); break;
case Lseq: inferseq(args[0]); break;
- case Ltup: infertup(args[0]); break;
default: /* pass */ break;
}
settype(n, type(args[0]));
--- a/parse/lits.def
+++ b/parse/lits.def
@@ -5,4 +5,3 @@
L(Lstr)
L(Lfunc)
L(Lseq)
-L(Ltup)
--- a/parse/node.c
+++ b/parse/node.c
@@ -46,6 +46,17 @@
return n;
}
+Node *mkexprl(int line, Op op, Node **args, size_t nargs)
+{
+ Node *n;
+
+ n = mknode(line, Nexpr);
+ n->expr.op = op;
+ n->expr.args = args;
+ n->expr.nargs = nargs;
+ return n;
+}
+
Node *mkexpr(int line, Op op, ...)
{
Node *n;
@@ -214,18 +225,6 @@
n = mknode(line, Nlit);
n->lit.littype = Lseq;
- n->lit.nelt = nvals;
- n->lit.seqval = vals;
-
- return n;
-}
-
-Node *mktuple(int line, Node **vals, size_t nvals)
-{
- Node *n;
-
- n = mknode(line, Nlit);
- n->lit.littype = Ltup;
n->lit.nelt = nvals;
n->lit.seqval = vals;
--- a/parse/ops.def
+++ b/parse/ops.def
@@ -49,6 +49,8 @@
O(Olit, 1)
O(Olbl, 1)
O(Ocons, 1)
+O(Otup, 1)
+O(Oarr, 1)
/* backend-only */
O(Ocjmp, 1) /* conditional jump */
O(Oload, 1) /* load from memory */
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -351,6 +351,7 @@
Node *mknode(int line, Ntype nt);
Node *mkfile(char *name);
Node *mkuse(int line, char *use, int islocal);
+Node *mkexprl(int line, Op op, Node **args, size_t nargs);
Node *mkexpr(int line, Op op, ...); /* NULL terminated */
Node *mkcall(int line, Node *fn, Node **args, size_t nargs);
Node *mkifstmt(int line, Node *cond, Node *iftrue, Node *iffalse);
--- a/parse/pickle.c
+++ b/parse/pickle.c
@@ -316,10 +316,6 @@
for (i = 0; i < n->lit.nelt; i++)
pickle(n->lit.seqval[i], fd);
break;
- case Ltup:
- for (i = 0; i < n->lit.nelt; i++)
- pickle(n->lit.tupval[i], fd);
- break;
}
break;
case Nloopstmt:
@@ -436,10 +432,6 @@
case Lseq:
for (i = 0; i < n->lit.nelt; i++)
n->lit.seqval[i] = unpickle(fd);
- break;
- case Ltup:
- for (i = 0; i < n->lit.nelt; i++)
- n->lit.tupval[i] = unpickle(fd);
break;
}
break;
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -106,10 +106,6 @@
for (i = 0; i < n->lit.nelt; i++)
fixup(n->lit.seqval[i]);
break;
- case Ltup:
- for (i = 0; i < n->lit.nelt; i++)
- fixup(n->lit.tupval[i]);
- break;
case Lchr: case Lint: case Lflt: case Lstr: case Lbool:
break;
}
@@ -192,9 +188,6 @@
case Lseq:
for (i = 0; i < n->lit.nelt; i++)
r->lit.seqval[i] = specializenode(n->lit.seqval[i], tsmap);
- case Ltup:
- for (i = 0; i < n->lit.nelt; i++)
- r->lit.tupval[i] = specializenode(n->lit.tupval[i], tsmap);
break;
}
break;