From 827b30c4dbe3a02221e5b3c717564ac642a3fb83 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Sun, 29 Dec 2024 15:18:52 +0100 Subject: [PATCH 1/4] remove commented code in code_dump --- lib.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib.c b/lib.c index 517ed00..d5b5229 100644 --- a/lib.c +++ b/lib.c @@ -284,7 +284,7 @@ static void quad_dump(struct quad * q, FILE* fout) { switch ( q->kind ) { - /*case BOP_PLUS: + case BOP_PLUS: symbol_dump(q->sym1,fout); fprintf(fout," := "); symbol_dump(q->sym2,fout); @@ -328,7 +328,7 @@ static void quad_dump(struct quad * q, FILE* fout) case DEF_FUNCTION: symbol_dump(q->sym1,fout); fprintf(fout,":"); - break;*/ + break; case CALL_PRINT: fprintf(fout,"print "); symbol_dump(q->sym1,fout); @@ -342,7 +342,7 @@ static void quad_dump(struct quad * q, FILE* fout) fprintf(fout," := "); symbol_dump(q->sym2,fout); break; - /*case ALLOCATE_ARRAY: + case ALLOCATE_ARRAY: fprintf(fout,"Allocate '"); symbol_dump(q->sym1,fout); fprintf(fout,"' to an one dimensional array of length "); @@ -365,7 +365,7 @@ static void quad_dump(struct quad * q, FILE* fout) fprintf(fout,"["); symbol_dump(q->sym3,fout); fprintf(fout,"]"); - break;*/ + break; default: fprintf(stderr,"BUG quad_dump(...) : q->kind inconnu\n"); break; @@ -431,8 +431,7 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ fprintf(fout,"\n\n.text\n main:\n"); - //int reg_counter=0; - //int max_temp_regs=7; + for(i = 0; i<c->nextquad; i++){ switch (c->quads[i].kind) @@ -465,7 +464,6 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym1->u.name); fprintf(fout,"\tmove $a0, $t0\n"); fprintf(fout,"\tli $v0, 1\n"); - //fprintf(stdout,"\tla $a0, %s\n",c->quads[i].sym1->u.name); fprintf(fout,"\tsyscall\n"); //Return to line fprintf(fout,"\tLF\n"); @@ -477,9 +475,7 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ break; case COPY: fprintf(fout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); - //fprintf(stdout,"\tsw $t%d, temp%d\n",reg_counter%max_temp_regs,indice_temp(t,c->quads[i].sym1)); fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); - //reg_counter++; break; default: fprintf(stderr,"#BUG %d\n",c->quads[i].kind); -- GitLab From 4591a2a55b6fa125426fe35bbb590ba8de2d5783 Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Mon, 30 Dec 2024 13:24:22 +0100 Subject: [PATCH 2/4] correct test --- Codes_en_COREC/tests/test2.corec | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/Codes_en_COREC/tests/test2.corec b/Codes_en_COREC/tests/test2.corec index aef0f5d..1cbbcef 100644 --- a/Codes_en_COREC/tests/test2.corec +++ b/Codes_en_COREC/tests/test2.corec @@ -8,41 +8,44 @@ prog Test2{ b = 2; c = a+b; - printstr("Addition, attendu 12"); + printstr("Addition, attendu 12: "); print(c); c = a-b; - printstr("Substraction, attendu 8"); + printstr("Substraction, attendu 8: "); print(c); c = a*b; - printstr("Multiplication, attendu 20"); + printstr("Multiplication, attendu 20: "); print(c); c = a/b; - printstr("Division, attendu 5"); + printstr("Division, attendu 5: "); print(c); a += b; - printstr("Addition aff, attendu 12"); - print(c); + printstr("Addition aff, attendu 12: "); + print(a); a *= b; - printstr("Addition mult, attendu 24"); - print(c); + printstr("Addition mult, attendu 24: "); + print(a); a /= b; - printstr("Addition mult, attendu 12"); - print(c); + printstr("Addition mult, attendu 12: "); + print(a); a -= 4; - printstr("Addition mult, attendu 8"); - print(c); + printstr("Addition mult, attendu 8: "); + print(a); c = a*a+2+b*b; + printstr("Addition mult, attendu 70: "); print(c); - c= a*(a+b)-c/2 + c= a*(a+b)-c/2; + printstr("Addition mult, attendu 45: "); + print(c) } } } -- GitLab From 606ecc92f45520fac89cdde50f1b5328f79a027f Mon Sep 17 00:00:00 2001 From: rugengande <jaenai.rugengande-ihimbazwe@etu.unistra.fr> Date: Mon, 30 Dec 2024 13:24:59 +0100 Subject: [PATCH 3/4] add mips code for arithmetic operations --- lib.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 19 deletions(-) diff --git a/lib.c b/lib.c index d5b5229..f3a474a 100644 --- a/lib.c +++ b/lib.c @@ -417,6 +417,9 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ else if(t->symbols[i].kind == NAME_LOC){ fprintf(fout,"\t%s: .word 0\n",t->symbols[i].u.name); } + else if(t->symbols[i].kind == NAME){ + fprintf(fout,"\t%s: .word 0\n",t->symbols[i].u.name); + } else{ //NAME fprintf(fout,"\ttemp%d: .word 0\n",i); } @@ -436,30 +439,88 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ for(i = 0; i<c->nextquad; i++){ switch (c->quads[i].kind) { - /*case BOP_PLUS: - fprintf(stdout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); - fprintf(stdout,"\tlw $t1, temp%d\n",indice_temp(t,c->quads[i].sym3)); - fprintf(stdout,"\tadd $t0, $t0, $t1\n"); - fprintf(stdout,"\tsw $t0, temp%d\n",indice_temp(t,c->quads[i].sym1)); + case BOP_PLUS: + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); + if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); + if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tadd $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); break; case BOP_MINUS: - fprintf(stdout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); - fprintf(stdout,"\tlw $t1, temp%d\n",indice_temp(t,c->quads[i].sym3)); - fprintf(stdout,"\tsub $t0, $t0, $t1\n"); - fprintf(stdout,"\tsw $t0, temp%d\n",indice_temp(t,c->quads[i].sym1)); + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); + if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); + if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tsub $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); break; case BOP_MULT: - fprintf(stdout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); - fprintf(stdout,"\tlw $t1, temp%d\n",indice_temp(t,c->quads[i].sym3)); - fprintf(stdout,"\tmul $t0, $t0, $t1\n"); - fprintf(stdout,"\tsw $t0, temp%d\n",indice_temp(t,c->quads[i].sym1)); + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); + if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); + if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tmul $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); break; + case BOP_DIV: + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); + if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); + if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tdiv $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + break; + case BOP_MOD: + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); + if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); + if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tdiv $t0, $t0, $t1\n"); + fprintf(fout,"\tmfhi $t0\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + break;//Check if we have variable names or just integers case UOP_MINUS: - fprintf(stdout,"\tli $t0, 0\n"); - fprintf(stdout,"\tlw $t1, temp%d\n",indice_temp(t,c->quads[i].sym2)); - fprintf(stdout,"\tsub $t0, $t0, $t1\n"); - fprintf(stdout,"\tsw $t0, temp%d\n",indice_temp(t,c->quads[i].sym1)); - break;*/ + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym1->u.name); + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym2->u.name); + else if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym2->u.value_int); + //Do the operation and save result in wanted variable + fprintf(fout,"\tsub $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + break; case CALL_PRINT: fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym1->u.name); fprintf(fout,"\tmove $a0, $t0\n"); @@ -474,7 +535,10 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ fprintf(fout,"\tsyscall\n"); break; case COPY: - fprintf(fout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); + else + fprintf(fout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); break; default: -- GitLab From e45a7f58b99408baf8a13c4259f209cbc4cd7c8a Mon Sep 17 00:00:00 2001 From: CLAVEL JULIEN <julien.clavel@etu.unistra.fr> Date: Mon, 30 Dec 2024 15:29:28 +0100 Subject: [PATCH 4/4] =?UTF-8?q?refactorisation=20du=20code,=20suppressions?= =?UTF-8?q?=20de=20code=20duppliqu=C3=A9=20etc=20=C3=A0=20utiliser=20comme?= =?UTF-8?q?=20base=20pour=20prochaines=20branches.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- corec.y | 2 +- lib.c | 296 ++++++++++++++++++++++++++++++++++++++------------------ lib.h | 69 ++++++++++++- 3 files changed, 270 insertions(+), 97 deletions(-) diff --git a/corec.y b/corec.y index 64e4465..73a514a 100644 --- a/corec.y +++ b/corec.y @@ -132,7 +132,7 @@ PROG : } id = symtable_put_name(SYMTAB,"Main"); symb_scope_function = id; - gencode(CODE,DEF_FUNCTION,id,NULL,NULL); + gencode(CODE,DEF_MAIN,id,NULL,NULL); } left_curly_bracket LOC DOM REC right_curly_bracket right_curly_bracket ; diff --git a/lib.c b/lib.c index f3a474a..951755b 100644 --- a/lib.c +++ b/lib.c @@ -1,6 +1,11 @@ #include "lib.h" +// TODO make it in a cleaner way +int indent = 0; +char tabulation[INDENT_BUF] = ""; + +#define INDENT_MAKER struct symtable * symtable_new() { struct symtable * t = malloc(sizeof(struct symtable)); @@ -325,6 +330,10 @@ static void quad_dump(struct quad * q, FILE* fout) fprintf(fout,"- "); symbol_dump(q->sym2,fout); break; + case DEF_MAIN: + symbol_dump(q->sym1,fout); + fprintf(fout,":"); + break; case DEF_FUNCTION: symbol_dump(q->sym1,fout); fprintf(fout,":"); @@ -425,121 +434,43 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ } } - //Macro for new line print - fprintf(fout,".macro LF\n"); - fprintf(fout,"\tli $v0, 4\n"); - fprintf(fout,"\tla $a0, lf\n"); - fprintf(fout,"\tsyscall\n"); - fprintf(fout,".end_macro\n"); - fprintf(fout,"\n\n.text\n main:\n"); - - - + print_macros(fout); + + // start of the .text segment + fprintf(fout,"\n\n.text\n"); for(i = 0; i<c->nextquad; i++){ switch (c->quads[i].kind) { case BOP_PLUS: - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); - if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); - if(c->quads[i].sym3->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tadd $t0, $t0, $t1\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_bop_plus(c,fout,i); break; case BOP_MINUS: - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); - if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); - if(c->quads[i].sym3->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tsub $t0, $t0, $t1\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_bop_minus(c,fout,i); break; case BOP_MULT: - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); - if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); - if(c->quads[i].sym3->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tmul $t0, $t0, $t1\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_bop_mult(c,fout,i); break; case BOP_DIV: - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); - if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); - if(c->quads[i].sym3->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tdiv $t0, $t0, $t1\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_bop_div(c,fout,i); break; case BOP_MOD: - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym3->u.name); - if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t0, %ld\n",c->quads[i].sym2->u.value_int); - if(c->quads[i].sym3->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym3->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tdiv $t0, $t0, $t1\n"); - fprintf(fout,"\tmfhi $t0\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); - break;//Check if we have variable names or just integers + print_bop_mod(c,fout,i); + break; case UOP_MINUS: - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym1->u.name); - //Check if we have variable names or just integers - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t1, %s\n",c->quads[i].sym2->u.name); - else if(c->quads[i].sym2->kind == CONSTANT_INT) - fprintf(fout,"\tli $t1, %ld\n",c->quads[i].sym2->u.value_int); - //Do the operation and save result in wanted variable - fprintf(fout,"\tsub $t0, $t0, $t1\n"); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_uop_minus(c,fout,i); + break; + case DEF_MAIN: + print_main_def(fout); break; case CALL_PRINT: - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym1->u.name); - fprintf(fout,"\tmove $a0, $t0\n"); - fprintf(fout,"\tli $v0, 1\n"); - fprintf(fout,"\tsyscall\n"); - //Return to line - fprintf(fout,"\tLF\n"); + print_print(c,fout,i); break; case CALL_PRINTSTR: - fprintf(fout,"\tli $v0, 4\n"); - fprintf(fout,"\tla $a0, temp%d\n",indice_temp(t,c->quads[i].sym1)); - fprintf(fout,"\tsyscall\n"); + print_printstr(t,c,fout,i); break; case COPY: - if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) - fprintf(fout,"\tlw $t0, %s\n",c->quads[i].sym2->u.name); - else - fprintf(fout,"\tlw $t0, temp%d\n",indice_temp(t,c->quads[i].sym2)); - fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); + print_copy(t,c,fout,i); break; default: fprintf(stderr,"#BUG %d\n",c->quads[i].kind); @@ -564,3 +495,178 @@ void free_exit(int code, struct code* c, struct symtable* t, FILE* fname){ if(fname!=NULL){fclose(fname);} exit(code); } + + +/************************* MIPS CODE GEN FUNCTIONS *************************/ + +// -------- MAIN RELATED -------- + +void print_main_def(FILE* fout){ + fprintf(fout,".globl start\n\n"); + fprintf(fout,"start:\n"); + add_indent(); +} + +// -------- ARITHMETIC RELATED -------- + +void print_bop_plus(struct code * c, FILE* fout,int i) +{ + //Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + //Do the operation and save result in wanted variable + fprintf(fout,"\tadd $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); +} + +void print_bop_minus(struct code * c, FILE* fout,int i) +{ + //Check if we have variable names or just integers + print_nameOrInteger(c,fout,i,0b110); + //Do the operation and save result in wanted variable + fprintf(fout,"\tsub $t0, $t0, $t1\n"); + fprintf(fout,"\tsw $t0, %s\n",c->quads[i].sym1->u.name); +} + +void print_bop_mult(struct code * c, FILE* fout,int i) +{ + //Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + //Do the operation and save result in wanted variable + fprintf(fout,"%smul $t0, $t0, $t1\n",tabulation); + fprintf(fout,"%ssw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); +} + +void print_bop_div(struct code * c, FILE* fout,int i) +{ + //Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + //Do the operation and save result in wanted variable + fprintf(fout,"%sdiv $t0, $t0, $t1\n",tabulation); + fprintf(fout,"%ssw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); +} + +void print_bop_mod(struct code * c, FILE* fout,int i) +{ + //Check if we have variable names or just integers for sym2 and sym3 + print_nameOrInteger(c,fout,i,0b110); + //Do the operation and save result in wanted variable + fprintf(fout,"%sdiv $t0, $t0, $t1\n",tabulation); + fprintf(fout,"%smfhi $t0\n",tabulation); + fprintf(fout,"%ssw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); +} + +void print_uop_minus(struct code * c, FILE* fout,int i) +{ + fprintf(fout,"%slw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); + //Check if we have variable names or just integers + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"%slw $t1, %s\n",tabulation,c->quads[i].sym2->u.name); + else if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"%sli $t1, %ld\n",tabulation,c->quads[i].sym2->u.value_int); + + //Do the operation and save result in wanted variable + fprintf(fout,"%ssub $t0, $t0, $t1\n",tabulation); + fprintf(fout,"%ssw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); +} + +// -------- PRINT RELATED -------- +void print_print(struct code * c, FILE* fout,int i) +{ + fprintf(fout,"%slw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); + fprintf(fout,"%smove $a0, $t0\n",tabulation); + fprintf(fout,"%sli $v0, 1\n",tabulation); + fprintf(fout,"%ssyscall\n",tabulation); + //Return to line + fprintf(fout,"%sLF\n",tabulation); +} + +void print_printstr(struct symtable * t, struct code * c, FILE* fout,int i) +{ + fprintf(fout,"%sli $v0, 4\n",tabulation); + fprintf(fout,"%sla $a0, temp%d\n",tabulation,indice_temp(t,c->quads[i].sym1)); + fprintf(fout,"%ssyscall\n",tabulation); +} + +// -------- FUNCTIONS RELATED -------- + +// -------- MISCELLANEOUS -------- + +void print_copy(struct symtable * t, struct code * c, FILE* fout,int i) +{ + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"%slw $t0, %s\n",tabulation,c->quads[i].sym2->u.name); + else + fprintf(fout,"%slw $t0, temp%d\n",tabulation,indice_temp(t,c->quads[i].sym2)); + fprintf(fout,"%ssw $t0, %s\n",tabulation,c->quads[i].sym1->u.name); +} + +/*** do the right thing based on whether we have an named var or a constant TODO finde a better name ! + * if on sym 1 sym = 0b1 do on sym1 (not implmented should never be of use) + * if on sym 2 sym = 0b10 etc + * if on sym 2 and 3 but not 1 simultaneously sym = 0b110 + * to use only on BOP operations PLUS MINUS MUL DIV MOD + * TODO sym may be useless and may be removed later + */ + +void print_nameOrInteger(struct code * c, FILE* fout,int i,int sym) +{ + if(sym & 0b10) + { + if(c->quads[i].sym2->kind == NAME || c->quads[i].sym2->kind == NAME_LOC) + fprintf(fout,"%slw $t0, %s\n",tabulation,c->quads[i].sym2->u.name); + else if(c->quads[i].sym2->kind == CONSTANT_INT) + fprintf(fout,"%sli $t0, %ld\n",tabulation,c->quads[i].sym2->u.value_int); + } + if( sym & 0b100) + { + if(c->quads[i].sym3->kind == NAME || c->quads[i].sym3->kind == NAME_LOC) + fprintf(fout,"%slw $t1, %s\n",tabulation,c->quads[i].sym3->u.name); + else if(c->quads[i].sym3->kind == CONSTANT_INT) + fprintf(fout,"%sli $t1, %ld\n",tabulation,c->quads[i].sym3->u.value_int); + } +} + + +/** Macro printer + * only print the LF macro for the moment + */ +void print_macros(FILE* fout){ + fprintf(fout,"\n# MACROS definition\n\n"); + print_LF_macro(fout); + fprintf(fout,"# End of MACROS definition\n"); +} + +void print_LF_macro(FILE* fout){ + fprintf(fout,".macro LF\n"); + fprintf(fout,"\tli $v0, 4\n"); + fprintf(fout,"\tla $a0, lf\n"); + fprintf(fout,"\tsyscall\n"); + fprintf(fout,".end_macro\n"); +} +// ETC FEEL FREE TO ADD MORE SECTIONS + +/************************* NAME TO DEFINE FUNCTIONS *************************/ + + + +void add_indent(){ + if(indent >= 1022) + { + printf("more than 1023 indentations ! write better code.\n"); + exit(-1); + } + tabulation[indent] = '\t'; + indent++; + tabulation[indent] = '\0'; +} + +int remove_indent(){ + if(indent == 0) + { + printf("shouldn't be removing a tab since there are none, resuming \n"); + return -1; + } + indent--; + tabulation[indent] = '\0'; + return 0; +} \ No newline at end of file diff --git a/lib.h b/lib.h index d8c32da..6a89439 100644 --- a/lib.h +++ b/lib.h @@ -6,6 +6,8 @@ extern unsigned int lineNumber; extern struct symbol* symb_scope_function; +#define INDENT_BUF 1024 + /* TABLE DES SYMBOLES */ typedef char name_t[64]; @@ -85,7 +87,7 @@ void symtable_free(struct symtable * t); * COPY_ARRAY -> tmp=sym1 array=sym2 element=sym3 -> tmp= array[element] */ struct quad { - enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR} kind; + enum quad_kind { BOP_PLUS, BOP_MINUS, BOP_MULT, BOP_DIV, BOP_MOD, UOP_MINUS, COPY, COPY_ARRAY, ALLOCATE_ARRAY, AFF_ARRAY, DEF_MAIN, DEF_FUNCTION, CALL_PRINT, CALL_PRINTSTR} kind; struct symbol* sym1; struct symbol* sym2; struct symbol* sym3; @@ -122,3 +124,68 @@ void code_free(struct code * c); void free_exit(int code, struct code* c, struct symtable* t, FILE* fname); +/************************* MIPS CODE GEN FUNCTIONS *************************/ + +// -------- MAIN RELATED -------- + +// print the starting flag so MIPS code know where he should start reading code from +void print_main_def(FILE* fout); + +// -------- ARITHMETIC RELATED -------- + +// add the BOP plus arithmetic operation TODO explain in more details i don't know what BOP stand for (Binary operator ?) +void print_bop_plus(struct code * c, FILE* fout,int i); + +// add the BOP minus arithmetic operation TODO explain in more details i don't know what BOP stand for (Binary operator ?) +void print_bop_minus(struct code * c, FILE* fout,int i); + +// add the BOP mult arithmetic operation TODO explain in more details i don't know what BOP stand for (Binary operator ?) +void print_bop_mult(struct code * c, FILE* fout,int i); + +// add the BOP div arithmetic operation TODO explain in more details i don't know what BOP stand for (Binary operator ?) +void print_bop_div(struct code * c, FILE* fout,int i); + +// add the BOP modulo arithmetic operation TODO explain in more details i don't know what BOP stand for (Binary operator ?) +void print_bop_mod(struct code * c, FILE* fout,int i); + +// add the UOP minus arithmetic operation TODO explain in more details i don't know what UOP stand for (Unary operator ?) +void print_uop_minus(struct code * c, FILE* fout,int i); + +// -------- PRINT RELATED -------- + +// add the code to print the asked variable +void print_print(struct code * c, FILE* fout,int i); + +// add the code to print the asked string +void print_printstr(struct symtable * t, struct code * c, FILE* fout,int i); + +// -------- FUNCTIONS RELATED -------- + +// -------- MISCELLANEOUS -------- + + +// 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); + +/** Macro printer + * only print the LF macro for the moment + * should becalled between .data and .text segments + */ +void print_macros(FILE* fout); + +/** Add a macro to print a LF in MIPS + */ +void print_LF_macro(FILE* fout); +// ETC FEEL FREE TO ADD MORE SECTIONS + +/************************* NAME TO DEFINE FUNCTIONS *************************/ + +// add a tabulation to the indentation string +void add_indent(); + +// remove a tabulation from the indentation string +int remove_indent(); + -- GitLab