changed asmTable way
This commit is contained in:
parent
86af52f0ac
commit
39bad0048b
4 changed files with 28 additions and 12 deletions
15
asmTable.c
15
asmTable.c
|
@ -7,7 +7,7 @@
|
||||||
static int labelCounter = 0;
|
static int labelCounter = 0;
|
||||||
|
|
||||||
/*At the start of the execution : the whole array is empty*/
|
/*At the start of the execution : the whole array is empty*/
|
||||||
static char** asmTable;
|
static ASMLine* asmTable;
|
||||||
static int lineCounter = 0;
|
static int lineCounter = 0;
|
||||||
static int maxIndex = START_TABLE_SIZE;
|
static int maxIndex = START_TABLE_SIZE;
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ void error(char* mess){
|
||||||
/* /!\ To be called at the beginning
|
/* /!\ To be called at the beginning
|
||||||
* Initializes the array of Symbols*/
|
* Initializes the array of Symbols*/
|
||||||
void initASMTable(){
|
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*/
|
/*Checks for the length of the array and reallocates if necessary*/
|
||||||
|
@ -35,7 +35,7 @@ void checkArraySanity(){
|
||||||
|
|
||||||
/*reallocates the array with the specified size*/
|
/*reallocates the array with the specified size*/
|
||||||
void reallocateArray(int 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){
|
if (temp != NULL){
|
||||||
asmTable = temp;
|
asmTable = temp;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,14 @@ void reallocateArray(int size){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*inserts an asm code line at the current index*/
|
||||||
void addLine(char* s) {
|
void addLine(char* s) {
|
||||||
strcpy(asmTable[lineCounter],s);
|
strcpy(asmTable[lineCounter].name,s);
|
||||||
|
asmTable[lineCounter].jumpLine;
|
||||||
lineCounter++;
|
lineCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*returns the current line (i.e. next one to insert)*/
|
||||||
|
int getCurrentLineNumber() {
|
||||||
|
return lineCounter;
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,11 @@
|
||||||
#define START_TABLE_SIZE 128
|
#define START_TABLE_SIZE 128
|
||||||
#define LINE_MAX_LENGTH 50
|
#define LINE_MAX_LENGTH 50
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char name[LINE_MAX_LENGTH];
|
||||||
|
int jumpLine;
|
||||||
|
} ASMLine;
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Array and Reallocation
|
Array and Reallocation
|
||||||
============================*/
|
============================*/
|
||||||
|
|
3
table.h
3
table.h
|
@ -39,8 +39,11 @@ void initSymbolTable();
|
||||||
/*resets the symbol table*/
|
/*resets the symbol table*/
|
||||||
void resetSymboltable();
|
void resetSymboltable();
|
||||||
|
|
||||||
|
/*inserts an asm code line at the current index*/
|
||||||
void addLine(char* s);
|
void addLine(char* s);
|
||||||
|
|
||||||
|
/*returns the current line (i.e. next one to insert)*/
|
||||||
|
int getCurrentLineNumber();
|
||||||
|
|
||||||
/*============================
|
/*============================
|
||||||
Element Management
|
Element Management
|
||||||
|
|
17
yacc.y
17
yacc.y
|
@ -98,16 +98,17 @@ Operation: tADD | tMUL | tSUB | tDIV;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
IfStatement : tIF Condition {
|
IfStatement : tIF Condition {
|
||||||
printf(f, « JMPF ???\n ») ;
|
int ligne = getCurrentLineNumber();
|
||||||
int ligne = get_nb_lignes_asm();
|
$1 = ligne ;
|
||||||
$1 = ligne ;
|
|
||||||
}
|
}
|
||||||
InnerBlock {
|
InnerBlock {
|
||||||
int current = get_nb_lignes_asm() ; // current == 4
|
int current = getCurrentLineNumber();
|
||||||
patch($1, current + 2) ;
|
addLabel($1, current + 2);
|
||||||
int ligne = insert(JMP) ; // ligne == L5
|
int line = insert(JMP);
|
||||||
$1 = ligne ;
|
patch(line, current + 1);
|
||||||
}
|
}{ int current = get_nb_lignes_asm() ; // current == 6
|
||||||
|
;
|
||||||
|
}
|
||||||
| tIF Condition InnerBlock tELSE InnerBlock
|
| tIF Condition InnerBlock tELSE InnerBlock
|
||||||
|
|
||||||
WhileStatement : tWHILE {labelWhileStart = printNewLabel();}
|
WhileStatement : tWHILE {labelWhileStart = printNewLabel();}
|
||||||
|
|
Loading…
Reference in a new issue