NOT A COMMIT
This commit is contained in:
parent
39bad0048b
commit
18f64a1653
5 changed files with 73 additions and 40 deletions
45
asmTable.c
45
asmTable.c
|
@ -4,8 +4,6 @@
|
|||
#include "stdlib.h"
|
||||
#include <string.h>
|
||||
|
||||
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();
|
||||
}
|
16
asmTable.h
16
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
|
||||
|
|
|
@ -5,14 +5,17 @@
|
|||
#include <stdio.h>
|
||||
#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*/
|
||||
|
|
2
table.c
2
table.c
|
@ -2,7 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
|
|
41
yacc.y
41
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 <addr> Expression
|
||||
%type <addr> Declaration
|
||||
%type <addr> NbOrVariable
|
||||
%token <nbInt> 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
|
||||
|
|
Loading…
Reference in a new issue