diff --git a/asmTable.c b/asmTable.c new file mode 100644 index 0000000..253cf61 --- /dev/null +++ b/asmTable.c @@ -0,0 +1,32 @@ + +#include "asmTable.h" + +int labelCounter = 0; + +/*At the start of the execution : the whole array is empty*/ +static char** table; +static int lineCounter = 0; +static int maxIndex = START_TABLE_SIZE; + + +#include "asmTable.h" + +/* /!\ To be called at the beginning + * Initializes the array of Symbols*/ +void initASMTable(){ + table = malloc(sizeof(Symbol) * START_TABLE_SIZE); +} + +/*resets the symbol table*/ +void resetASMTable(){ + currentIndex = 0; + maxIndex = START_TABLE_SIZE; + +// stack pointers + esp = 0; + ebp = 0; + + currentDepth = 0; +} + +// todo inserer parser détecter les labels diff --git a/asmTable.h b/asmTable.h new file mode 100644 index 0000000..a64fd12 --- /dev/null +++ b/asmTable.h @@ -0,0 +1,28 @@ +// +// Created by chepycou on 4/18/23. +// + +#ifndef PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H +#define PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H + +#define START_TABLE_SIZE 128 +#define LINE_MAX_LENGTH 50 + +/*============================ + Array and Reallocation + ============================*/ + +/*reallocates the array with the specified size*/ +void reallocateArray(int size); + +/*Checks for the length of the array and reallocates if necessary*/ +void checkArraySanity(); + +/* /!\ To be called at the beginning + * Initializes the array of Symbols*/ +void initSymbolTable(); + +/*resets the symbol table*/ +void resetSymboltable(); + +#endif //PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H diff --git a/blocs.c b/blocs.c index 6252e5f..c9a601d 100644 --- a/blocs.c +++ b/blocs.c @@ -3,22 +3,57 @@ // #include "blocs.h" +#include "operations.h" #include +#include /*TODO on écrit tout dans un fichier asm extérieur puis on : * - fait un parcours pour stocker dans une liste chaînée par exemple les valeur de ligne des labels * - faire un parcours pour retirer les labels au début des lignes * - faire un parcours pour remplacer les labels par leur valeur * */ - - - +/* int labelCount = 0; -char* getNextLabel(){ - char label[LABEL_MAX_LENGTH]; - sprintf(label, "%d_LABEL", labelCount); +int getNextLabel(){ + return(labelCount++); } +void printLabel(int labelWhile){ + char label[LABEL_MAX_LENGTH]; + sprintf(label,"%d_LABEL\n",labelWhile); + printf("%s",label); + FILE* fp; + fp = fopen("asm", "a"); + fputs(label, fp); + fclose(fp); +} + +int printNewLabel(){ + int l = getNextLabel(); + printLabel(l); + return l; +} + +void printJumpToLabel(int labelWhile) { + char instr[ASM_TEXT_LEN]; + sprintf(instr,"JMP %d_LABEL\n",labelWhile); + printf("%s",instr); + FILE* fp; + fp = fopen("asm", "a"); + fputs(instr, fp); + fclose(fp); +} + +void printJumpFToLabel(int labelWhile) { + char label[LABEL_MAX_LENGTH]; + sprintf(label,"%d_LABEL\n",labelWhile); + printf("JMF %s",label); + FILE* fp; + fp = fopen("asm", "a"); + fputs(label, fp); + fclose(fp); +}*/ + diff --git a/blocs.h b/blocs.h index 808c789..1cddf09 100644 --- a/blocs.h +++ b/blocs.h @@ -5,8 +5,22 @@ #ifndef PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H #define PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H -int labelCount; #define LABEL_MAX_LENGTH 20 +/*====================== + Label Management + ======================*/ + +/*returns the next label*/ +int getNextLabel(); + +// prints a new label and returns the index of it +int printNewLabel(); + +// prints a label and returns the index of it +void printLabel(int labelWhile); + +// prints a jump to the label and returns the index of it +void printJumpToLabel(int labelWhile); #endif //PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H diff --git a/yacc.y b/yacc.y index 3a47c1f..f1dccab 100644 --- a/yacc.y +++ b/yacc.y @@ -8,6 +8,8 @@ #include "blocs.h" int t; +int labelWhileStart; +int labelWhileEnd; %} %code provides{ @@ -95,10 +97,22 @@ NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT; Operation: tADD | tMUL | tSUB | tDIV; */ -IfStatement : tIF Condition InnerBlock +IfStatement : tIF Condition { + printf(f, « JMPF ???\n ») ; + int ligne = get_nb_lignes_asm(); + $1 = ligne ; + } + InnerBlock { + int current = get_nb_lignes_asm() ; // current == 4 + patch($1, current + 2) ; + int ligne = insert(JMP) ; // ligne == L5 + $1 = ligne ; + } | tIF Condition InnerBlock tELSE InnerBlock -WhileStatement : tWHILE Condition InnerBlock; +WhileStatement : tWHILE {labelWhileStart = printNewLabel();} + Condition InnerBlock {printJumpToLabel(labelWhileStart); + labelWhileEnd = printNewLabel();}; Assignment : tID tASSIGN Expression tSEMI { setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElement(); @@ -160,6 +174,11 @@ 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();