ref: 57e6fd0760167a9e9b39a6c9212ef3292297380e
parent: 7a0a1eb532018cf6db1d49a49667ae18a0ed643e
author: Ori Bernstein <[email protected]>
date: Sun May 13 19:51:46 EDT 2012
Store the decl id in the Ovar expr. This means that we can look up the decl's location later. We also fail now if we can't look up the decl's loc.
--- a/8/isel.c
+++ b/8/isel.c
@@ -146,6 +146,8 @@
switch (exprop(n)) {
case Ovar:
+ if (!hthas(s->locs, (void*)n->expr.did))
+ die("%s(%ld) not found", declname(n->expr.args[0]), n->expr.did);
stkoff = (size_t)htget(s->locs, (void*)n->expr.did);
locmem(&l, stkoff, Resp, Rnone, ModeL);
break;
@@ -275,9 +277,13 @@
void load(Isel *s, Loc *a, Loc *b)
{
- assert(a->type == Loclit || a->type == Locmem || a->type == Loclbl);
+ Loc l;
assert(b->type == Locreg);
- g(s, Imov, a, b, NULL);
+ if (a->type == Locreg)
+ locmem(&l, 0, b->reg, Rnone, a->mode);
+ else
+ l = *a;
+ g(s, Imov, &l, b, NULL);
}
void stor(Isel *s, Loc *a, Loc *b)
@@ -424,7 +430,7 @@
break;
case Oload: /* mem -> reg */
a = selexpr(s, args[0]);
- b = selexpr(s, args[1]);
+ b = getreg(s, a.mode);
load(s, &b, &a);
r = b;
break;
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -131,8 +131,7 @@
{
char buf[128];
static int nexttmp;
- Node *n;
- Node *t;
+ Node *t, *r, *n;
Sym *s;
assert(e->type == Nexpr);
@@ -141,7 +140,9 @@
s = mksym(-1, n, e->expr.type);
t = mkdecl(-1, s);
declare(simp, t);
- return mkexpr(-1, Ovar, t, NULL);
+ r = mkexpr(-1, Ovar, t, NULL);
+ r->expr.did = s->id;
+ return r;
}
void jmp(Simp *s, Node *lbl) { append(s, mkexpr(-1, Ojmp, lbl, NULL)); }
@@ -237,12 +238,43 @@
return -1;
}
+static Node *one;
+
+static Node *membaddr(Simp *s, Node *n)
+{
+ Node *t, *u, *r;
+ Node **args;
+
+ args = n->expr.args;
+ if (n->expr.type->type != Typtr)
+ t = mkexpr(-1, Oaddr, args[0], NULL);
+ else
+ t = args[0];
+ u = mkint(-1, offsetof(args[0], args[1]));
+ u = mkexpr(-1, Olit, u, NULL);
+ r = mkexpr(-1, Oadd, t, u, NULL);
+ return r;
+}
+
Node *lval(Simp *s, Node *n)
{
- return rval(s, n);
+ Node *r;
+
+ if (!one)
+ one = mkexpr(-1, Olit, mkint(-1, 1), NULL);
+ switch (exprop(n)) {
+ case Ovar:
+ r = n;
+ case Omemb:
+ r = membaddr(s, n);
+ break;
+ default:
+ die("%s cannot be an lval", opstr(exprop(n)));
+ break;
+ }
+ return r;
}
-static Node *one;
Node *rval(Simp *s, Node *n)
{
@@ -273,13 +305,8 @@
die("Have not implemented lowering op %s", opstr(exprop(n)));
break;
case Omemb:
- if (n->expr.type->type != Typtr)
- t = mkexpr(-1, Oaddr, args[0], NULL);
- else
- t = args[0];
- u = mkint(-1, offsetof(args[0], args[1]));
- u = mkexpr(-1, Olit, u, NULL);
- r = mkexpr(-1, Oadd, t, u, NULL);
+ t = membaddr(s, n);
+ r = mkexpr(-1, Oload, t, NULL);
break;
/* fused ops:
@@ -369,6 +396,7 @@
assert(n->type == Ndecl);
f = s->fn;
+ printf("DECLARE %s(%ld) at %zd\n", declname(n), n->decl.sym->id, f->stksz);
htput(f->locs, (void*)n->decl.sym->id, (void*)f->stksz);
f->stksz += size(n);
}
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -195,10 +195,7 @@
if (i >= a->nsub)
fatal(ctx->line, "%s incompatible with %s near %s", tystr(a), tystr(b), ctxstr(ctx));
- /* FIXME: recurse properly.
- mergecstrs(ctx, a, b);
- unify(ctx, a->sub[i], b->sub[i]);
- */
+ unify(ctx, a->sub[i], b->sub[i]);
}
return b;
} else {