diff --git a/asmTable.c b/asmTable.c index f474441..f2499f1 100644 --- a/asmTable.c +++ b/asmTable.c @@ -4,8 +4,6 @@ #include "stdlib.h" #include -static int labelCounter = 0; - /*At the start of the execution : the whole array is empty*/ static ASMLine* asmTable; static int lineCounter = 0; @@ -13,12 +11,7 @@ static int maxIndex = START_TABLE_SIZE; #include "asmTable.h" - -/* Error display */ -void error(char* mess){ - printf("ERROR : %s\n", mess); - exit(-1); -} +#include "table.h" /* /!\ To be called at the beginning * Initializes the array of Symbols*/ @@ -27,15 +20,15 @@ void initASMTable(){ } /*Checks for the length of the array and reallocates if necessary*/ -void checkArraySanity(){ +void checkASMArraySanity(){ if (lineCounter == maxIndex){ - reallocateArray(maxIndex * 2); + reallocateASMArray(maxIndex * 2); } } /*reallocates the array with the specified size*/ -void reallocateArray(int size){ - char *temp = (ASMLine*) realloc(asmTable, (sizeof(ASMLine) * size)); +void reallocateASMArray(int size){ + ASMLine *temp = (ASMLine*) realloc(asmTable, (sizeof(ASMLine) * size)); if (temp != NULL){ asmTable = temp; } @@ -47,11 +40,37 @@ void reallocateArray(int size){ /*inserts an asm code line at the current index*/ void addLine(char* s) { strcpy(asmTable[lineCounter].name,s); - asmTable[lineCounter].jumpLine; + asmTable[lineCounter].jumpLine = -1; lineCounter++; + checkASMArraySanity(); + displayASMTable(); +} + +/*inserts the address in case of jumps*/ +void setJumpLine(int index, int addr) { + asmTable[index].jumpLine = addr; + displayASMTable(); } /*returns the current line (i.e. next one to insert)*/ int getCurrentLineNumber() { return lineCounter; } + +/*displays the entire table at this moment*/ +void displayASMTable(){ + printf("\n"); + doubleLine(); + for (int i = 0; i < lineCounter; ++i) { + ASMLine a = asmTable[i]; + if(a.jumpLine == -1) { + printf("%d | %s", i, a.name); + } else { + printf("%d | %s %d\n", i, a.name,a.jumpLine); + } + if (i != lineCounter -1) { + line(); + } + } + doubleLine(); +} \ No newline at end of file diff --git a/asmTable.h b/asmTable.h index ddb6e5c..32b96e1 100644 --- a/asmTable.h +++ b/asmTable.h @@ -18,14 +18,26 @@ typedef struct { ============================*/ /*reallocates the array with the specified size*/ -void reallocateArray(int size); +void reallocateASMArray(int size); /*Checks for the length of the array and reallocates if necessary*/ -void checkArraySanity(); +void checkASMArraySanity(); + +/*inserts an asm code line at the current index*/ +void addLine(char* s); /* /!\ To be called at the beginning * Initializes the array of Symbols*/ void initASMTable(); +/*inserts the address in case of jumps*/ +void setJumpLine(int index, int addr); + +/*returns the current line (i.e. next one to insert)*/ +int getCurrentLineNumber(); + + +/*displays the entire table at this moment*/ +void displayASMTable(); #endif //PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H diff --git a/operations.c b/operations.c index bf5e264..50de8e5 100644 --- a/operations.c +++ b/operations.c @@ -5,14 +5,17 @@ #include #include "table.h" #include "operations.h" +#include "asmTable.h" /*prints to the stdout and the out asm file*/ void printOp(char* s){ - printf("%s",s); - FILE* fp; + //printf("%s",s); + addLine(s); + + /*FILE* fp; fp = fopen("asm", "a"); fputs(s, fp); - fclose(fp); + fclose(fp);*/ } /*clears the out asm file*/ diff --git a/table.c b/table.c index 57d4674..d343cbb 100644 --- a/table.c +++ b/table.c @@ -2,7 +2,7 @@ #include #include -#define VERBOSITY 1 // 1 -> displays the table, 0 no display +#define VERBOSITY 0 // 1 -> displays the table, 0 no display int memorySizes[2] = {1,1}; int tempCounter = 0; diff --git a/yacc.y b/yacc.y index dabb766..ad452c1 100644 --- a/yacc.y +++ b/yacc.y @@ -6,6 +6,7 @@ #include "table.h" #include "operations.h" #include "blocs.h" +#include "asmTable.h" int t; int labelWhileStart; @@ -19,7 +20,7 @@ void yyerror (const char *); %union {char str[NAME_MAX_LENGTH]; int nbInt; int addr; enumVarType type; } /*loops keywords*/ -%token tWHILE tIF tELSE +%token tWHILE /*tIF declared below*/ tELSE /*reserved keywords*/ %token tRETURN tPRINT /*types : integers, floats or void*/ @@ -46,6 +47,7 @@ void yyerror (const char *); %type Expression %type Declaration %type NbOrVariable +%token tIF %start Program %% @@ -69,6 +71,7 @@ Line : IfStatement InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC | tLBRACE {increaseDepth();} Lines tRBRACE {decreaseDepth();}; + /*Condition = the evaluated boolean expression for an if or while = ( ... ) */ Condition : tLPAR ConditionalExpression tRPAR; @@ -98,22 +101,22 @@ Operation: tADD | tMUL | tSUB | tDIV; */ IfStatement : tIF Condition { - int ligne = getCurrentLineNumber(); - $1 = ligne ; - } - InnerBlock { - int current = getCurrentLineNumber(); - addLabel($1, current + 2); - int line = insert(JMP); - patch(line, current + 1); - }{ int current = get_nb_lignes_asm() ; // current == 6 - ; - } - | tIF Condition InnerBlock tELSE InnerBlock + int ligne =getCurrentLineNumber(); addLine("JMPF"); $1 = ligne ; +} InnerBlock { + int current = getCurrentLineNumber(); printf("current Line %d", current); addLine("JMP"); $1 = current + 1; setJumpLine($1, current+1); +} tELSE InnerBlock { + int current = getCurrentLineNumber() ; setJumpLine($1, current); +} + | tIF Condition { +int ligne =getCurrentLineNumber(); addLine("JMPF"); $1 = ligne ; +} InnerBlock { + int current = getCurrentLineNumber(); printf("current Line %d", current); setJumpLine($1, current); +}; -WhileStatement : tWHILE {labelWhileStart = printNewLabel();} - Condition InnerBlock {printJumpToLabel(labelWhileStart); - labelWhileEnd = printNewLabel();}; +// todo it does either one of the two but not the two + +WhileStatement : tWHILE {} + Condition InnerBlock {}; Assignment : tID tASSIGN Expression tSEMI { setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElement(); @@ -175,14 +178,10 @@ void yyerror(const char *msg) { exit(1); } -void patch(int from, int to) { - /* Mémorisation pour patcher apres la compilation. */ - labels[from] = to ; -} - int main(void) { clearOp(); initSymbolTable(); + initASMTable(); yyparse(); } // SI >> SC