Permalink
Browse files

clear out the stack upon closing a block.

  • Loading branch information...
1 parent 50d22d2 commit 3cbbe91ad1f65fc6ca77c73b8f5b070fdcd0aa5f @kjs kjs committed Jun 23, 2012
Showing with 30 additions and 8 deletions.
  1. +25 −4 src/gencode.c
  2. +3 −3 src/instr.c
  3. +2 −1 src/stack.c
View
@@ -320,7 +320,18 @@ gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) {
assert(a != NULL);
- /* generate code for RHS and get number of registers that hold the result */
+ /* Generate code for RHS and get number of registers that hold the result
+ Note that since the AST for an assignment was right-recursive, for a = b = c,
+ it looks like this:
+
+ =
+ / \
+ a =
+ / \
+ b c
+
+ Since RHS is evaluated first, code for b=c is generated first.
+ */
rhs_reg_count = gencode_expr(comp, a->rhs);
/* Generate code for LHS and get number of registers that hold the result
@@ -362,8 +373,8 @@ gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) {
fprintf(OUT, "\tset \t%c%d, %c%d, x\n", reg_chars[(int)lhs.type], lhs.no,
reg_chars[(int)rhs.type], rhs.no);
- pushreg(comp->regstack, rhs);
- free_reg(comp, lhs); /* to free regs for constants; for symbols they should be frozen. */
+ pushreg(comp->regstack, lhs);
+ free_reg(comp, rhs); /* to free regs for constants; for symbols they should be frozen. */
}
else if (lhs_reg_count == 2) { /* complex lvalue; x[10] = 42 */
assert(rhs_reg_count == 1); /* lhs_reg_count+rhs_reg_count == 3, so pop 3 regs. */
@@ -382,8 +393,9 @@ gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) {
/* make result available for next in "chain" of assignments, if any (e.g, a = b = c = 42;). */
pushreg(comp->regstack, rhs);
+
}
- }
+ }
}
/*
@@ -2118,6 +2130,7 @@ gencode_cast(M1_compiler *comp, m1_castexpr *expr) {
}
+
static unsigned
gencode_expr(M1_compiler *comp, m1_expression *e) {
unsigned num_regs = 1;
@@ -2246,6 +2259,7 @@ gencode_expr(M1_compiler *comp, m1_expression *e) {
assert(0);
}
+ print_stack(comp->regstack, "EXPR");
return num_regs;
}
@@ -2313,6 +2327,13 @@ gencode_block(M1_compiler *comp, m1_block *block) {
/* restore parent scope. */
comp->currentsymtab = block->locals.parentscope;
+
+ /* pop all registers from the reg stack and free them. frozen regs will be unaffected. */
+ while (!regstack_isempty(comp->regstack)) {
+ m1_reg r = popreg(comp->regstack);
+ free_reg(comp, r);
+ }
+
}
static void
View
@@ -38,8 +38,8 @@ char const * const m0_instr_names[] = {
"div_n",
"mod_i",
"mod_n",
- "iton",
- "ntoi",
+ "convert_n_i",
+ "convert_i_n",
"ashr",
"lshr",
"shl",
@@ -225,7 +225,7 @@ mk_instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
comp->lastgenerated = ins;
- // write_instr(comp, ins);
+ //write_instr(comp, ins);
return ins;
}
View
@@ -9,7 +9,7 @@ Needed by code generator to store labels for break statements, etc.
#include <assert.h>
#include "stack.h"
-#define STACKDEBUG 0
+#define STACKDEBUG 1
m1_intstack *
new_intstack(void) {
@@ -100,6 +100,7 @@ popreg(m1_regstack *stack) {
return r;
}
+
m1_reg
topreg(m1_regstack *stack) {
m1_reg r;

0 comments on commit 3cbbe91

Please sign in to comment.