ref: f60dbb91eeb4bd2c39242669a5f35252aab21fce
parent: e91e8cce466408f4a45222c0b57a4261de87545c
author: Ori Bernstein <[email protected]>
date: Wed Jun 6 18:37:23 EDT 2012
Split size function to one that can take types. That makes it more useful.
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -52,6 +52,7 @@
Node *rval(Simp *s, Node *n);
Node *lval(Simp *s, Node *n);
void declarelocal(Simp *s, Node *n);
+size_t size(Node *n);
void append(Simp *s, Node *n)
{
@@ -89,18 +90,11 @@
return s;
}
-
-size_t size(Node *n)
+size_t tysize(Type *t)
{
- Type *t;
size_t sz;
int i;
- if (n->type == Nexpr)
- t = n->expr.type;
- else
- t = n->decl.sym->type;
-
sz = 0;
switch (t->type) {
case Tyvoid:
@@ -133,7 +127,7 @@
case Tyarray:
dump(t->asize, stdout);
assert(exprop(t->asize) == Olit);
- return t->asize->expr.args[0]->lit.intval;
+ return t->asize->expr.args[0]->lit.intval * tysize(t->sub[0]);
case Tytuple:
case Tystruct:
for (i = 0; i < t->nmemb; i++)
@@ -148,6 +142,18 @@
break;
}
return -1;
+}
+
+size_t size(Node *n)
+{
+ Type *t;
+
+ if (n->type == Nexpr)
+ t = n->expr.type;
+ else
+ t = n->decl.sym->type;
+
+ return tysize(t);
}
Node *genlbl(void)