Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

clear out the stack upon closing a block.

  • Loading branch information...
commit 3cbbe91ad1f65fc6ca77c73b8f5b070fdcd0aa5f 1 parent 50d22d2
Klaas-Jan Stol authored
29  src/gencode.c
@@ -320,7 +320,18 @@ gencode_assign(M1_compiler *comp, NOTNULL(m1_assignment *a)) {
320 320
     		
321 321
     assert(a != NULL);
322 322
 	
323  
-	/* generate code for RHS and get number of registers that hold the result */
  323
+	/* Generate code for RHS and get number of registers that hold the result 
  324
+	   Note that since the AST for an assignment was right-recursive, for a = b = c,
  325
+	   it looks like this:
  326
+	   
  327
+	     =
  328
+	    / \
  329
+	   a   =
  330
+	      / \
  331
+	     b   c 
  332
+	     
  333
+	   Since RHS is evaluated first, code for b=c is generated first.   
  334
+	*/
324 335
     rhs_reg_count = gencode_expr(comp, a->rhs);
325 336
     
326 337
     /* 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)) {
362 373
             
363 374
             fprintf(OUT, "\tset \t%c%d, %c%d, x\n", reg_chars[(int)lhs.type], lhs.no, 
364 375
                                                     reg_chars[(int)rhs.type], rhs.no);
365  
-            pushreg(comp->regstack, rhs);
366  
-            free_reg(comp, lhs); /* to free regs for constants; for symbols they should be frozen. */
  376
+            pushreg(comp->regstack, lhs);
  377
+            free_reg(comp, rhs); /* to free regs for constants; for symbols they should be frozen. */
367 378
         }
368 379
         else if (lhs_reg_count == 2) { /* complex lvalue; x[10] = 42 */
369 380
             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)) {
382 393
             
383 394
             /* make result available for next in "chain" of assignments, if any (e.g, a = b = c = 42;). */              
384 395
             pushreg(comp->regstack, rhs);
  396
+
385 397
         }
386  
-    }    
  398
+    }        
387 399
 }
388 400
 
389 401
 /*
@@ -2118,6 +2130,7 @@ gencode_cast(M1_compiler *comp, m1_castexpr *expr) {
2118 2130
   
2119 2131
 }
2120 2132
 
  2133
+
2121 2134
 static unsigned
2122 2135
 gencode_expr(M1_compiler *comp, m1_expression *e) {
2123 2136
     unsigned num_regs = 1;
@@ -2246,6 +2259,7 @@ gencode_expr(M1_compiler *comp, m1_expression *e) {
2246 2259
             assert(0);
2247 2260
     }  
2248 2261
 
  2262
+    print_stack(comp->regstack, "EXPR");
2249 2263
     return num_regs; 
2250 2264
 
2251 2265
 }
@@ -2313,6 +2327,13 @@ gencode_block(M1_compiler *comp, m1_block *block) {
2313 2327
     
2314 2328
     /* restore parent scope. */
2315 2329
     comp->currentsymtab = block->locals.parentscope;
  2330
+    
  2331
+    /* pop all registers from the reg stack and free them. frozen regs will be unaffected. */
  2332
+    while (!regstack_isempty(comp->regstack)) {
  2333
+        m1_reg r = popreg(comp->regstack);
  2334
+        free_reg(comp, r);
  2335
+    }
  2336
+
2316 2337
 }
2317 2338
 
2318 2339
 static void
6  src/instr.c
@@ -38,8 +38,8 @@ char const * const m0_instr_names[] = {
38 38
     "div_n",
39 39
     "mod_i",
40 40
     "mod_n",
41  
-    "iton",
42  
-    "ntoi",
  41
+    "convert_n_i",
  42
+    "convert_i_n",
43 43
     "ashr",
44 44
     "lshr",
45 45
     "shl",
@@ -225,7 +225,7 @@ mk_instr(M1_compiler *comp, m0_opcode opcode, char const * const format, ...) {
225 225
     
226 226
     comp->lastgenerated = ins;
227 227
     
228  
-  //  write_instr(comp, ins);
  228
+    //write_instr(comp, ins);
229 229
     return ins;
230 230
     
231 231
 }
3  src/stack.c
@@ -9,7 +9,7 @@ Needed by code generator to store labels for break statements, etc.
9 9
 #include <assert.h>
10 10
 #include "stack.h"
11 11
 
12  
-#define STACKDEBUG  0
  12
+#define STACKDEBUG  1
13 13
 
14 14
 m1_intstack *
15 15
 new_intstack(void) {
@@ -100,6 +100,7 @@ popreg(m1_regstack *stack) {
100 100
     return r;
101 101
 }
102 102
 
  103
+
103 104
 m1_reg
104 105
 topreg(m1_regstack *stack) {
105 106
     m1_reg r;

0 notes on commit 3cbbe91

Please sign in to comment.
Something went wrong with that request. Please try again.