changed asmTable way

This commit is contained in:
Raphaël LACROIX 2023-04-18 16:55:52 +02:00
parent 86af52f0ac
commit 39bad0048b
4 changed files with 28 additions and 12 deletions

View file

@ -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;
}

View file

@ -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
============================*/

View file

@ -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

13
yacc.y
View file

@ -98,15 +98,16 @@ Operation: tADD | tMUL | tSUB | tDIV;
*/
IfStatement : tIF Condition {
printf(f, « JMPF ???\n ») ;
int ligne = get_nb_lignes_asm();
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