diff --git a/Codes_en_COREC/tests/test3.corec b/Codes_en_COREC/tests/test3.corec index 236481dda906f0a2a154f42fc5c499d9746ef149..93c55f2c0a8c1acfb037ffbb3f7892489f1b4505 100644 --- a/Codes_en_COREC/tests/test3.corec +++ b/Codes_en_COREC/tests/test3.corec @@ -9,16 +9,16 @@ prog Test3 { array1[4] = 2; f = 1.15; // Implicit array of dim 1 length 1 -> f[0] - print(f); // to delete test purpose only - array2[0] = f; + array1[1] = f; array2[1] = 5; - print(array2); // to delete test purpose only array1[3] = array2[1]; - + f[0] = 1.0; // Implicit array of dim 1 length 1 -> f[0] + array1[2] = f; - - print(array1) + printstr("Expected result : 2.0 1.15 1.0 5.0 2.0 \nObtained Result : "); + print(array1) // contain all the values exchanges here if the code is correct } } } + diff --git a/lib.c b/lib.c index ab80a221bcaee713a2248bbfa1a59ce9fca14dc1..77f9b64733758af39b89110d00a1443abb3b1c1a 100644 --- a/lib.c +++ b/lib.c @@ -479,6 +479,7 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ break; case DEF_MAIN: print_main_def(fout); + add_indent(1,c,t,fout); break; case CALL_PRINT: print_print(t,c,fout,i); @@ -521,7 +522,6 @@ void free_exit(int code, struct code* c, struct symtable* t, FILE* fname){ void print_main_def(FILE* fout){ fprintf(fout,".globl start\n\n"); fprintf(fout,"start:\n"); - add_indent(); } // -------- ARITHMETIC RELATED -------- @@ -605,7 +605,7 @@ void print_print(struct symtable * t,struct code * c, FILE* fout,int i) // we avoid having 2 identical labels by putting the quad index at the end of it // /!\ start of the loop /!\ -- fprintf(fout,"%sprint_array_loop%d:\n",tabulation,i); - add_indent(); + add_indent(1,c,t,fout); // bge by security to avoid miss matching (which shouldn't be happening) fprintf(fout,"%sbge $t2, $t0, end_array_loop%d\n",tabulation,i); @@ -681,12 +681,14 @@ void print_copy_array(struct symtable * t, struct code * c, FILE* fout,int i) fprintf(stdout,"Tried to do an Var = tab[index] operation but right part isn't an array should address this issue in the corec.y file\n"); exit(1); } - - get_tab_addr_macro("$t0", 1, 2, c->quads[i].sym2,c->quads[i].sym3,t,fout); - - load_symbol_macro("$t1",c->quads[i].sym1,t,fout); - - fprintf(fout,"%ssw $t1, ($t0)\n",tabulation); + // adress of the array element we want to copy + load_tab_addr_macro("$t0", 1, 2, c->quads[i].sym2,c->quads[i].sym3,t,fout); + // adress of the place we want to copy the array element in + load_addr_macro("$t1",c->quads[i].sym1,t,fout); + // load in $t2 what's inside the array element + fprintf(fout,"%slw $t2, ($t0)\n",tabulation); + // store it in the destination + fprintf(fout,"%ssw $t2, ($t1)\n",tabulation); #ifdef LIBDEBUG fprintf(fout,"%s# end of copy_array section\n",tabulation); @@ -727,7 +729,7 @@ void print_aff_array(struct symtable * t, struct code * c, FILE* fout,int i) } // V alternative, effectue directement le*4 en calcul vu qu'on devrait passer par un load immediate dans tout les cas // fprintf(fout,"%sla $t0, %s\n",tabulation,c->quads[i].sym1->u.name); - get_tab_addr_macro("$t0", 1, 2, c->quads[i].sym1,c->quads[i].sym2,t,fout); + load_tab_addr_macro("$t0", 1, 2, c->quads[i].sym1,c->quads[i].sym2,t,fout); load_symbol_macro("$t1",c->quads[i].sym3,t,fout); fprintf(fout,"%ssw $t1, ($t0)\n",tabulation); #ifdef LIBDEBUG @@ -803,26 +805,34 @@ void load_symbol_macro(char * dest, struct symbol * sym, struct symtable * t, FI } -void get_tab_addr_macro(char * dest,int tmp_used1, int tmp_used2, struct symbol * sym_addr, struct symbol * sym_index, struct symtable * t, FILE* fout){ +void load_tab_addr_macro(char * dest,int tmp_used1, int tmp_used2, struct symbol * sym_addr, struct symbol * sym_index, struct symtable * t, FILE* fout){ fprintf(fout,"%slw $t%d, %s\n",tabulation,tmp_used2,sym_addr->u.name); if(sym_index->kind == CONSTANT_INT) // put into dest the addr of array[index] element fprintf(fout,"%saddi %s, $t%d, %ld\n",tabulation,dest,tmp_used2,(sym_index->u.value_int)*4); // in other cases we need to load the value from the label of the corresponding symbol - else if( sym_index->kind == NAME || sym_index->kind == NAME_LOC) { + else if( sym_index->kind == NAME || sym_index->kind == NAME_LOC || sym_index->kind == ARRAY) { fprintf(fout,"%slw $t%d, %s\n",tabulation,tmp_used1,(sym_index->u.name)); } else { fprintf(fout,"%slw $t%d, temp%d\n",tabulation,tmp_used1,indice_temp(t,sym_index)); } if (sym_index->kind != CONSTANT_INT){ + // if not CONSTANT_INT wasn't yet multiplied by 4 fprintf(fout,"%ssll $t%d, $t%d, 2\n",tabulation,tmp_used1,tmp_used1); fprintf(fout,"%sadd %s, $t%d, $t%d\n",tabulation,dest,tmp_used2,tmp_used1); } } -/** Macro printer - * only print the LF macro for the moment - */ +void load_addr_macro(char * dest, struct symbol * sym, struct symtable * t, FILE* fout) +{ + if( sym->kind == NAME || sym->kind == NAME_LOC || sym->kind == ARRAY){ + fprintf(fout,"%sla %s, %s\n",tabulation,dest,sym->u.name); + } else + fprintf(fout,"%sla %s, temp%d\n",tabulation,dest,indice_temp(t,sym)); + +} + + void print_macros(FILE* fout){ fprintf(fout,"\n# MACROS definition\n\n"); print_LF_macro(fout); @@ -905,11 +915,11 @@ void debug_register_strings_declaration(FILE * fout){ -void add_indent(){ +void add_indent(int code, struct code* c, struct symtable* t, FILE* fname){ if(indent >= 1022) { printf("more than 1023 indentations ! write better code.\n"); - exit(-1); + free_exit(code,c,t,fname); } tabulation[indent] = '\t'; indent++; diff --git a/lib.h b/lib.h index 2108325291f13b7dcbdb6524291f36f0584e65f0..a9426d98523e5fbfb6e4f0c55fa744669cfd703d 100644 --- a/lib.h +++ b/lib.h @@ -160,10 +160,10 @@ void print_uop_minus(struct code * c, FILE* fout,int i); // add the code to print the asked variable void print_print(struct symtable * t, struct code * c, FILE* fout,int i); -// add the code to print the asked string +// add the code to print the asked string void print_printstr(struct symtable * t, struct code * c, FILE* fout,int i); -/** print the register's value, no LF and no SPACE +/** @brief print_reg make MIPS code to print the register's value, no LF and no SPACE * only the register's value type = 0 => int | type = 1 => float everything else is considered as an int TODO update this * TODO think about deleting type and just checking if the first reg character is an f if no other cases than int and floats */ @@ -173,39 +173,56 @@ void print_reg(char * reg,FILE * fout, int type); // -------- ARRAY RELATED -------- +/** @brief print_copy_array make MIPS code that copy to a temp the value in an array at a given index X <= array[i] + */ void print_copy_array(struct symtable * t, struct code * c, FILE* fout,int i); +/** @brief print_allocate_array make MIPS code to allocate the memory of a given array + */ void print_allocate_array(struct symtable * t, struct code * c, FILE* fout,int i); +/** @brief print_aff_array make MIPS code to affect a value in an array at a given index array[i]= X + */ void print_aff_array(struct symtable * t, struct code * c, FILE* fout,int i); // -------- MISCELLANEOUS -------- -// make a copy of a var TODO make a real comment +// make a copy of a var TODO make a real comment void print_copy(struct symtable * t, struct code * c, FILE* fout,int i); // load whether a named var or a constant TODO find a better name ! void print_nameOrInteger(struct code * c, FILE* fout,int i,int sym); // -------- MACROS -------- + +/** @brief load_symbol_alloc_macro load a symbol used for an sbrk allocation ( multiply it's value by 4) + */ void load_symbol_alloc_macro(char * dest, int tmp_used, struct symbol * sym, struct symtable * t, FILE* fout); +/** @brief load_symbol_macro load a symbol value into the dest register + */ void load_symbol_macro(char * dest, struct symbol * sym, struct symtable * t, FILE* fout); -void get_tab_addr_macro(char * dest, int tmp_used1, int tmp_used2, struct symbol * sym_addr, struct symbol * sym_index, struct symtable * t, FILE* fout); +/** @brief load_tab_addr_macro given a symbol adress and a symbol index put the addr of array[n] element in dest + * tmp_used1 and tmp_used2 won't be saved so ensure nothing that should be saved is in + */ +void load_tab_addr_macro(char * dest, int tmp_used1, int tmp_used2, struct symbol * sym_addr, struct symbol * sym_index, struct symtable * t, FILE* fout); + +/** @brief load_addr_macro put the memory adress of sym into the dest register + */ +void load_addr_macro(char * dest, struct symbol * sym, struct symtable * t, FILE* fout); -/** Macro printer - * only print the LF macro for the moment - * should becalled between .data and .text segments +/** @brief Macro printer only print the LF and SPACE macros for the moment + * should be called between .data and .text segments */ void print_macros(FILE* fout); -/** Add a macro to print a LF in MIPS code +/** @brief print_LF_macro add a macro to print a LF in MIPS code */ void print_LF_macro(FILE* fout); -/** Add a macro to print a SPCE in MIPS code +/** @brief print_SPACE_macro add a macro to print a SPCE in MIPS code */ void print_SPACE_macro(FILE* fout); @@ -221,7 +238,7 @@ void debug_register_strings_declaration(FILE * fout); /************************* NAME TO DEFINE FUNCTIONS *************************/ // add a tabulation to the indentation string -void add_indent(); +void add_indent(int code, struct code* c, struct symtable* t, FILE* fname); // remove a tabulation from the indentation string int remove_indent();