safe_fprintf(cout, "if (");
expr_genc(stmt->data.sif.pred, cout);
safe_fprintf(cout, ") {\n");
+
ARRAY_ITER(struct stmt *, s, i, &stmt->data.sif.then)
stmt_genc(s, indent+1, cout);
AIEND
- pindent(indent, cout);
- safe_fprintf(cout, "} else {\n");
- ARRAY_ITER(struct stmt *, s, i, &stmt->data.sif.els)
- stmt_genc(s, indent+1, cout);
- AIEND
+
+ if (ARRAY_SIZE(&stmt->data.sif.els) != 0) {
+ pindent(indent, cout);
+ safe_fprintf(cout, "} else {\n");
+ ARRAY_ITER(struct stmt *, s, i, &stmt->data.sif.els)
+ stmt_genc(s, indent+1, cout);
+ AIEND
+ }
pindent(indent, cout);
safe_fprintf(cout, "}\n");
break;
safe_fprintf(cout, "\n");
} else {
safe_fprintf(cout, "bsr %s\n", expr->data.efuncall.ident);
- safe_fprintf(cout, "ajs -%u\n",
- ARRAY_SIZE(&expr->data.efuncall.args));
- //TODO don't do this when the function returns void
- safe_fprintf(cout, "ldr RR\n");
}
+ safe_fprintf(cout, "ajs -%u\n",
+ ARRAY_SIZE(&expr->data.efuncall.args));
+ //TODO don't do this when the function returns void
+ safe_fprintf(cout, "ldr RR\n");
break;
case eint:
safe_fprintf(cout, "ldc %d\n", expr->data.eint);
static void stmt_genssm(struct genssmst *st, struct stmt *stmt, FILE *cout)
{
+ int l;
+ struct vref *el;
switch(stmt->type) {
case sassign:
-// pindent(indent, cout);
-// safe_fprintf(cout, "%s", stmt->data.sassign.ident);
-// ARRAY_ITER(char *, f, i, stmt->data.sassign.fields)
-// safe_fprintf(cout, "->%s", f);
-// AIEND
-// safe_fprintf(cout, " = ");
-// expr_genabc(st, stmt->data.sassign.expr, cout);
-// safe_fprintf(cout, ";\n");
+ //TODO fields
+ HASH_FIND_STR(st->refs, stmt->data.sassign.ident, el);
+ if (el == NULL)
+ die("unknown variable: %s???", stmt->data.sassign.ident);
+ switch(el->type) {
+ case global:
+ safe_fprintf(cout, "ldr R5\n");
+ expr_genssm(st, stmt->data.sassign.expr, cout);
+ safe_fprintf(cout, "lda %d\n", el->num);
+ break;
+ case arg:
+ expr_genssm(st, stmt->data.sassign.expr, cout);
+ safe_fprintf(cout, "stl -%d\n", el->num);
+ break;
+ case local:
+ expr_genssm(st, stmt->data.sassign.expr, cout);
+ safe_fprintf(cout, "stl %d\n", el->num);
+ break;
+ }
break;
case sif:
+ l = st->fresh++;
expr_genssm(st, stmt->data.sif.pred, cout);
- safe_fprintf(cout, "brf _else%d\n", st->fresh);
+ safe_fprintf(cout, "brf _else%d\n", l);
body_genssm(st, stmt->data.sif.then, cout);
- safe_fprintf(cout, "bra _endif%d\n", st->fresh);
- safe_fprintf(cout, "_else%d:\n", st->fresh);
+ safe_fprintf(cout, "bra _endif%d\n", l);
+ safe_fprintf(cout, "_else%d:\n", l);
body_genssm(st, stmt->data.sif.els, cout);
- safe_fprintf(cout, "_endif%d: \n", st->fresh++);
+ safe_fprintf(cout, "_endif%d: \n", l);
break;
case sreturn:
if (stmt->data.sreturn != NULL) {
add_vref(st, stmt->data.svardecl->ident, local, st->vdecl++);
break;
case swhile:
- safe_fprintf(cout, "_while%d: \n", st->fresh);
+ l = st->fresh++;
+ safe_fprintf(cout, "_while%d: \n", l);
expr_genssm(st, stmt->data.sif.pred, cout);
- safe_fprintf(cout, "brf _endwhile%d\n", st->fresh);
+ safe_fprintf(cout, "brf _endwhile%d\n", l);
body_genssm(st, stmt->data.swhile.body, cout);
- safe_fprintf(cout, "bra _while%d\n", st->fresh);
- safe_fprintf(cout, "_endwhile%d: \n", st->fresh++);
+ safe_fprintf(cout, "bra _while%d\n", l);
+ safe_fprintf(cout, "_endwhile%d: \n", l);
break;
default:
die("Unsupported stmt node\n");
}
}
-//static void vardecl_genssm(struct genssmst *st, struct vardecl *vardecl, FILE *cout)
-//{
-// //TODO add to dictionary
-// expr_genssm(st, vardecl->expr, cout);
-//}
-
static void global_genssm(int no, struct genssmst *st, struct vardecl *vardecl, FILE *cout)
{
add_vref(st, vardecl->ident, global, no+1);