While done (and tested)

This commit is contained in:
Raphaël LACROIX 2023-04-20 11:29:52 +02:00
parent 6661eb3865
commit ff74c5be44
4 changed files with 34 additions and 11 deletions

View file

@ -43,6 +43,7 @@ void reallocateASMArray(int size){
void addLine(char* s) { void addLine(char* s) {
strcpy(asmTable[lineCounter].name,s); strcpy(asmTable[lineCounter].name,s);
asmTable[lineCounter].jumpLine = -1; asmTable[lineCounter].jumpLine = -1;
asmTable[lineCounter].conditionAddr = -1;
lineCounter++; lineCounter++;
checkASMArraySanity(); checkASMArraySanity();
displayASMTable(); displayASMTable();
@ -51,7 +52,13 @@ void addLine(char* s) {
/*inserts the address in case of jumps*/ /*inserts the address in case of jumps*/
void setJumpLine(int index, int addr) { void setJumpLine(int index, int addr) {
asmTable[index].jumpLine = addr; asmTable[index].jumpLine = addr;
printf("\n%d aaaaa %d\n",index,addr); displayASMTable();
}
/*inserts the condition's address in case of jumps*/
void setConditionAddr(int index, int addr) {
asmTable[index].conditionAddr = addr;
displayASMTable(); displayASMTable();
} }
@ -69,7 +76,11 @@ void displayASMTable(){
if(a.jumpLine == -1) { if(a.jumpLine == -1) {
printf("%d | %s", i, a.name); printf("%d | %s", i, a.name);
} else { } else {
printf("%d | %s %d\n", i, a.name,a.jumpLine); if(a.conditionAddr == -1) {
printf("%d | %s @%d\n", i, a.name,a.jumpLine);
} else {
printf("%d | %s @%d @%d\n", i, a.name,a.conditionAddr,a.jumpLine);
}
} }
if (i != lineCounter -1) { if (i != lineCounter -1) {
line(); line();

View file

@ -10,6 +10,7 @@
typedef struct { typedef struct {
char name[LINE_MAX_LENGTH]; char name[LINE_MAX_LENGTH];
int conditionAddr;
int jumpLine; int jumpLine;
} ASMLine; } ASMLine;
@ -33,6 +34,9 @@ void initASMTable();
/*inserts the address in case of jumps*/ /*inserts the address in case of jumps*/
void setJumpLine(int index, int addr); void setJumpLine(int index, int addr);
/*inserts the condition's address in case of jumps*/
void setConditionAddr(int index, int addr);
/*returns the current line (i.e. next one to insert)*/ /*returns the current line (i.e. next one to insert)*/
int getCurrentLineNumber(); int getCurrentLineNumber();

View file

@ -109,7 +109,7 @@ void operation_copy(int addr1, int addr2){
/*prints the ASM instruction for the inferior condition /*prints the ASM instruction for the inferior condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_inf(int addr1, int addr2){ int cond_inf(int addr1, int addr2){
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "INF @%d @%d @%d\n", addr, addr1, addr2); sprintf(s, "INF @%d @%d @%d\n", addr, addr1, addr2);
printOp(s); printOp(s);
@ -119,7 +119,7 @@ int cond_inf(int addr1, int addr2){
/*prints the ASM instruction for the superior condition /*prints the ASM instruction for the superior condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_sup(int addr1, int addr2){ int cond_sup(int addr1, int addr2){
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "SUP @%d @%d @%d\n", addr, addr1, addr2); sprintf(s, "SUP @%d @%d @%d\n", addr, addr1, addr2);
printOp(s); printOp(s);
@ -129,16 +129,17 @@ int cond_sup(int addr1, int addr2){
/*prints the ASM instruction for the equality condition /*prints the ASM instruction for the equality condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_eq(int addr1, int addr2) { int cond_eq(int addr1, int addr2) {
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "EQU @%d @%d @%d\n", addr, addr1, addr2); sprintf(s, "EQU @%d @%d @%d\n", addr, addr1, addr2);
printOp(s); printOp(s);
return addr; return addr;
}
/*prints the ASM instruction for the negation condition /*prints the ASM instruction for the negation condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_not(int addr1){ int cond_not(int addr1){
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "NOT @%d @%d\n", addr, addr1); sprintf(s, "NOT @%d @%d\n", addr, addr1);
printOp(s); printOp(s);
@ -148,7 +149,7 @@ int cond_not(int addr1){
/*prints the ASM instruction for the and condition /*prints the ASM instruction for the and condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_and(int addr1, int addr2){ int cond_and(int addr1, int addr2){
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "AND @%d @%d @%d\n", addr, addr1, addr2); sprintf(s, "AND @%d @%d @%d\n", addr, addr1, addr2);
printOp(s); printOp(s);
@ -158,7 +159,7 @@ int cond_and(int addr1, int addr2){
/*prints the ASM instruction for the or condition /*prints the ASM instruction for the or condition
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int cond_or(int addr1, int addr2){ int cond_or(int addr1, int addr2){
int addr = addCondAndGetAddress(); int addr = addTempCONDAndGetAddress();
char s[ASM_TEXT_LEN]; char s[ASM_TEXT_LEN];
sprintf(s, "OR @%d @%d @%d\n", addr, addr1, addr2); sprintf(s, "OR @%d @%d @%d\n", addr, addr1, addr2);
printOp(s); printOp(s);

11
yacc.y
View file

@ -11,6 +11,7 @@
int t; int t;
int labelWhileStart; int labelWhileStart;
int labelWhileEnd; int labelWhileEnd;
int whileJumpAddr;
%} %}
%code provides{ %code provides{
@ -49,6 +50,8 @@ void yyerror (const char *);
%type <addr> Declaration %type <addr> Declaration
%type <addr> NbOrVariable %type <addr> NbOrVariable
%type <nbInt> IfStatement1 %type <nbInt> IfStatement1
%type <addr> Condition
%type <nbInt> InnerBlock
%token <nbInt> tIF %token <nbInt> tIF
%token <nbInt> tWHILE %token <nbInt> tWHILE
@ -77,7 +80,7 @@ InnerBlock : tLBRACE tRBRACE // a function or while loop can be empty cf GCC
/*Condition = the evaluated boolean expression for an if or while = ( ... ) */ /*Condition = the evaluated boolean expression for an if or while = ( ... ) */
Condition : tLPAR ConditionalExpression tRPAR; Condition : tLPAR ConditionalExpression tRPAR {$$ = $2;};
/*ConditionalExpression = expression that evaluates to a boolean*/ /*ConditionalExpression = expression that evaluates to a boolean*/
ConditionalExpression : tID { $$ = getOffset($1);} ConditionalExpression : tID { $$ = getOffset($1);}
@ -124,11 +127,15 @@ IfStatement : tIF Condition IfStatement1 InnerBlock tELSE {
WhileStatement : tWHILE { WhileStatement : tWHILE {
$1 = getCurrentLineNumber(); $1 = getCurrentLineNumber();
} Condition { } Condition {
int current = getCurrentLineNumber();
addLine("JMPF"); addLine("JMPF");
setConditionAddr(current,$3);
whileJumpAddr = current;
suppressCONDElements();
} InnerBlock { } InnerBlock {
addLine("JMP"); addLine("JMP");
int current = getCurrentLineNumber(); int current = getCurrentLineNumber();
setJumpLine($1, current); setJumpLine(whileJumpAddr, current);
setJumpLine(current-1, $1); setJumpLine(current-1, $1);
}; };