diff --git a/corec.y b/corec.y index d63de70b16d9762ecbdf5c083706dacf50afa81a..86af826fa78c71b87b95c0ea1afdeb99e7dfc424 100644 --- a/corec.y +++ b/corec.y @@ -914,34 +914,39 @@ F : COND : E OPREL E ternary_then B ternary_else B { - /* + struct symbol * boolean = newtemp(SYMTAB,INTEGER); + switch($2.opnum){ case 1: - gencode(CODE,OPREL_LT,tmp,tmpArr,t); + gencode(CODE,OPREL_LT,boolean,$1.ptr,$3.ptr); break; case 2: - gencode(CODE,OPREL_GT,tmp,tmpArr,t); + gencode(CODE,OPREL_GT,boolean,$1.ptr,$3.ptr); break; case 3: - gencode(CODE,OPREL_LE,tmp,tmpArr,t); + gencode(CODE,OPREL_LE,boolean,$1.ptr,$3.ptr); break; case 4: - gencode(CODE,OPREL_GE,tmp,tmpArr,t); + gencode(CODE,OPREL_GE,boolean,$1.ptr,$3.ptr); break; case 5: - gencode(CODE,OPREL_EQ,tmp,tmpArr,t); + gencode(CODE,OPREL_EQ,boolean,$1.ptr,$3.ptr); break; default: fprintf(stderr, "BUG\n"); break; - } - */ + name_t end_condition = snprintf(end_condition, sizeof(end_condition), "end_condition_%d", t->condition); + struct symbol * label = symtable_put_label(SYMTAB,end_condition); + name_t else_condition = snprintf(else_condition, sizeof(else_condition), "else_condition_%d", t->condition); + struct symbol * label = symtable_put_label(SYMTAB,else_condition); + + // TODO ahndle and gencode for if and else branches } ; B : diff --git a/lib.c b/lib.c index 4a5a17a5aab92d1a9f4a0d56efdbac7a4b91bceb..1b2680caea7e6610303eb462a2152b04919aab79 100644 --- a/lib.c +++ b/lib.c @@ -178,6 +178,8 @@ struct symbol* symtable_put_array(struct symtable * t, const char* var_name, uns struct symbol *newtemp(struct symtable * t, enum var_type ty) { + if(t->size==t->capacity) + symtable_grow(t); struct symbol* s; name_t name; sprintf(name,"t%d",t->temporary); @@ -191,6 +193,17 @@ struct symbol *newtemp(struct symtable * t, enum var_type ty) return s; } +struct symbol* symtable_put_label(struct symtable * t, const char * label_name, struct symbol* func_id) +{ + if(t->size==t->capacity) + symtable_grow(t); + struct symbol *s = &(t->symbols[t->size]); + s->kind = LABEL; + strcpy(s->u.name,label_name); + ++ (t->size); + return s; +} + void symtable_dump(struct symtable * t, FILE* fout) { unsigned int i; @@ -208,6 +221,8 @@ void symtable_dump(struct symtable * t, FILE* fout) fprintf(fout,"# %p = %s (in the scope of the '%s' function type : INTEGER)\n",&(t->symbols[i]),t->symbols[i].u.name,t->symbols[i].scope_function); else if(t->symbols[i].kind==CHAIN) fprintf(fout,"# %p = %s\n",&(t->symbols[i]),t->symbols[i].u.chaine); + else if(t->symbols[i].kind==LABEL) + fprintf(fout,"# %p = %s (LABEL)\n",&(t->symbols[i]),t->symbols[i].u.name); else if(t->symbols[i].kind==ARRAY){ fprintf(fout,"# %p = (%s,",&(t->symbols[i]),t->symbols[i].u.arr.name); if(t->symbols[i].u.arr.size[0]->kind == NAME_LOC || t->symbols[i].u.arr.size[0]->kind == NAME || t->symbols[i].u.arr.size[0]->kind == NAME_FLOAT) @@ -299,6 +314,8 @@ static void symbol_dump(struct symbol* s, FILE* fout) case ARRAY: fprintf(fout,"%s",s->u.arr.name); break; + case LABEL: + fprintf(fout,"%s",s->u.name); default: fprintf(stderr,"BUG SYMBOL DUMP NOT RECOGNIZED\n"); break; @@ -456,6 +473,10 @@ void code_mips_dump(struct symtable * t, struct code * c, FILE* fout){ else if (t->symbols[i].kind == ARRAY){ fprintf(fout,"\t%s: .word 0\n",t->symbols[i].u.name); } + // TODO determine if we can avoid using it + // else if (t->symbols[i].kind == LABEL) { + // fprintf(fout,"\t%s: .word 0\n",t->symbols[i].u.name); + // } else{ //NAME fprintf(fout,"\ttemp%d: .word 0\n",i); } diff --git a/lib.h b/lib.h index e4930c1e4208fd28e333b0b2f058f23dfdad3ee3..81097d9e1e80f484bb267f916bd210cb10fc32f8 100644 --- a/lib.h +++ b/lib.h @@ -35,7 +35,8 @@ enum kind{ CONSTANT_FLOAT, // -> use the value_float attribut in the union u CHAIN, // -> use the chaine attribut in the union u NAME_LOC, // -> use the name attribut, the symbol is just defined - ARRAY // -> use the name attribut in the union u + ARRAY, // -> use the name attribut in the union u + LABEL // -> use the name attribut in the union u }; // Struct symbol -> store a symbol @@ -57,6 +58,7 @@ struct symbol { struct symtable { unsigned int capacity; unsigned int temporary; + unsigned int condition; // TODO find if necessary and how to name the different labels unsigned int size; struct symbol* symbols; }; @@ -74,12 +76,14 @@ struct symbol* symtable_put_name(struct symtable * t, const char * id); struct symbol* symtable_put_name_float(struct symtable * t, const char * id); // Add a symbol in the table of a chaine of caracters (CHAIN) struct symbol* symtable_put_chaine(struct symtable * t, const char * chaine); -// Add a symbol in the table of a local variable (defined but isn't initialized) (NAME_LOC_INT and NAME_LOC_FLOAT) +// Add a symbol in the table of a local variable (defined but isn't initialized) (NAME_LOC) struct symbol* symtable_put_loc(struct symtable * t, const char * var_name, struct symbol* func_id); // Add a symbol in the table of a local array (defined but isn't initialized) (ARRAY) struct symbol* symtable_put_array(struct symtable * t, const char* var_name, unsigned int dim, struct symbol** arrayDim, struct symbol* dim1size, struct symbol* func_id); // Add to the symtable a temporary function which is used to transform multiples addresses instruction to a group of 3 addresses instructions (uses NAME kind) struct symbol* newtemp(struct symtable * t,enum var_type ty); +// Add to the symtable a label to branch different blocks of code +struct symbol* symtable_put_label(struct symtable * t, const char * label_name, struct symbol* func_id); // Get a pointer of a symbol with attribut name in the symtable (if 's' isn't in the table, return NULL) // (func_id is used for the scope of function on the local variable)