Skip to content

Commit

Permalink
Removed replace, caused by for inside a for, nw is stacked before for…
Browse files Browse the repository at this point in the history
… execution
  • Loading branch information
jefersonla committed Nov 20, 2016
1 parent 1bc8615 commit f45c7f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion codegen_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table){
/* Header of the for operation */
addInstructionMainQueue(mips_for_ini);

/* Add a symbol to symbol table */
symbolTablePushVar(actual_symbol_table);

/* Initialize a new symbol table just for iterator */
new_symbol_table_iterator = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);

Expand Down Expand Up @@ -834,12 +837,14 @@ bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table){
/* Pop local scope */
popSymbolTable(new_symbol_table);


/* Add the footer of the for instruction */
addInstructionMainQueueFormated(mips_end_for, count_for, count_for);

/* Pop local iterator */
popSymbolTable(new_symbol_table_iterator);

/* Pop var from symbol table */
symbolTablePopVar(actual_symbol_table);

/* Delte actual scope */
deleteSymbolTable(&new_symbol_table);
Expand Down
12 changes: 8 additions & 4 deletions codegen_models.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ const char mips_end_while[] =

/* Loop type for */
const char mips_for_ini[] =
"\t# v--------------------- For -------------------v #\n";
"\t# v--------------------- For -------------------v #\n"
"\taddiu $sp, $sp, -4\n"
"\tsw $a2, 4($sp)\n";

/* Store for interval type */
const char mips_for_interval[] =
Expand Down Expand Up @@ -586,9 +588,11 @@ const char mips_for_inc[] =

/* End of for defition */
const char mips_end_for[]=
"\tj start_for_%d\n"
"\tj start_for_%d\n"
"end_for_%d:\n"
"\t# ^----------------- End of For ----------------^ #\n";
"\tlw $a2, 4($sp)\n"
"\taddiu $sp, $sp, 4\n"
"\t# ^----------------- End of For ----------------^ #\n";

/* ------------------------------------------------------------- */
/* Function Definitions */
Expand Down Expand Up @@ -913,7 +917,7 @@ const char mips_start_return[] =
const char mips_return_multiple[] =
"\tli $t2, 0 # Load first value of 'i'\n"
"\tli $t3, %d # Load final value of 'i'\n"
"return_loop_%d: # while (there are values on stack)\n"
"return_loop_%d: # while (there are values on stack)\n"
"\tlw $t1, 4($s0) # Pick bottom value of exp stack\n"
"\tsw $t1, 4($sp) # and store on top of stack\n"
"\taddiu $s0, $s0, -4 # Decrease exp stack\n"
Expand Down

0 comments on commit f45c7f3

Please sign in to comment.