diff --git a/asmTable.c b/asmTable.c index c8eb7d4..f474441 100644 --- a/asmTable.c +++ b/asmTable.c @@ -7,7 +7,7 @@ static int labelCounter = 0; /*At the start of the execution : the whole array is empty*/ -static char** asmTable; +static ASMLine* asmTable; static int lineCounter = 0; static int maxIndex = START_TABLE_SIZE; @@ -23,7 +23,7 @@ void error(char* mess){ /* /!\ To be called at the beginning * Initializes the array of Symbols*/ void initASMTable(){ - asmTable = malloc(sizeof(char) * LINE_MAX_LENGTH * START_TABLE_SIZE); + asmTable = malloc(sizeof(ASMLine) * START_TABLE_SIZE); } /*Checks for the length of the array and reallocates if necessary*/ @@ -35,7 +35,7 @@ void checkArraySanity(){ /*reallocates the array with the specified size*/ void reallocateArray(int size){ - char **temp = (char **)realloc(asmTable, (sizeof(char) * LINE_MAX_LENGTH * size)); + char *temp = (ASMLine*) realloc(asmTable, (sizeof(ASMLine) * size)); if (temp != NULL){ asmTable = temp; } @@ -44,7 +44,14 @@ void reallocateArray(int size){ } } +/*inserts an asm code line at the current index*/ void addLine(char* s) { - strcpy(asmTable[lineCounter],s); + strcpy(asmTable[lineCounter].name,s); + asmTable[lineCounter].jumpLine; lineCounter++; } + +/*returns the current line (i.e. next one to insert)*/ +int getCurrentLineNumber() { + return lineCounter; +} diff --git a/asmTable.h b/asmTable.h index 805f385..ddb6e5c 100644 --- a/asmTable.h +++ b/asmTable.h @@ -8,6 +8,11 @@ #define START_TABLE_SIZE 128 #define LINE_MAX_LENGTH 50 +typedef struct { + char name[LINE_MAX_LENGTH]; + int jumpLine; +} ASMLine; + /*============================ Array and Reallocation ============================*/ diff --git a/table.h b/table.h index 798a038..3d4f4e7 100644 --- a/table.h +++ b/table.h @@ -39,8 +39,11 @@ void initSymbolTable(); /*resets the symbol table*/ void resetSymboltable(); +/*inserts an asm code line at the current index*/ void addLine(char* s); +/*returns the current line (i.e. next one to insert)*/ +int getCurrentLineNumber(); /*============================ Element Management diff --git a/yacc.y b/yacc.y index f1dccab..dabb766 100644 --- a/yacc.y +++ b/yacc.y @@ -98,16 +98,17 @@ Operation: tADD | tMUL | tSUB | tDIV; */ IfStatement : tIF Condition { - printf(f, « JMPF ???\n ») ; - int ligne = get_nb_lignes_asm(); - $1 = ligne ; + int ligne = getCurrentLineNumber(); + $1 = ligne ; } InnerBlock { - int current = get_nb_lignes_asm() ; // current == 4 - patch($1, current + 2) ; - int ligne = insert(JMP) ; // ligne == L5 - $1 = ligne ; - } + 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 WhileStatement : tWHILE {labelWhileStart = printNewLabel();}