From ff74c5be44b04c5cad079603a1d8a76d26321ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20LACROIX?= Date: Thu, 20 Apr 2023 11:29:52 +0200 Subject: [PATCH] While done (and tested) --- asmTable.c | 15 +++++++++++++-- asmTable.h | 4 ++++ operations.c | 15 ++++++++------- yacc.y | 11 +++++++++-- 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/asmTable.c b/asmTable.c index e16b267..2d5a6cf 100644 --- a/asmTable.c +++ b/asmTable.c @@ -43,6 +43,7 @@ void reallocateASMArray(int size){ void addLine(char* s) { strcpy(asmTable[lineCounter].name,s); asmTable[lineCounter].jumpLine = -1; + asmTable[lineCounter].conditionAddr = -1; lineCounter++; checkASMArraySanity(); displayASMTable(); @@ -51,7 +52,13 @@ void addLine(char* s) { /*inserts the address in case of jumps*/ void setJumpLine(int index, int 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(); } @@ -69,7 +76,11 @@ void displayASMTable(){ if(a.jumpLine == -1) { printf("%d | %s", i, a.name); } 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) { line(); diff --git a/asmTable.h b/asmTable.h index 32b96e1..2a51c3a 100644 --- a/asmTable.h +++ b/asmTable.h @@ -10,6 +10,7 @@ typedef struct { char name[LINE_MAX_LENGTH]; + int conditionAddr; int jumpLine; } ASMLine; @@ -33,6 +34,9 @@ void initASMTable(); /*inserts the address in case of jumps*/ 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)*/ int getCurrentLineNumber(); diff --git a/operations.c b/operations.c index c5afa22..a95780a 100644 --- a/operations.c +++ b/operations.c @@ -109,7 +109,7 @@ void operation_copy(int addr1, int addr2){ /*prints the ASM instruction for the inferior condition * and returns the address of the temporary variable*/ int cond_inf(int addr1, int addr2){ - int addr = addCondAndGetAddress(); + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "INF @%d @%d @%d\n", addr, addr1, addr2); printOp(s); @@ -119,7 +119,7 @@ int cond_inf(int addr1, int addr2){ /*prints the ASM instruction for the superior condition * and returns the address of the temporary variable*/ int cond_sup(int addr1, int addr2){ - int addr = addCondAndGetAddress(); + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "SUP @%d @%d @%d\n", addr, addr1, addr2); printOp(s); @@ -128,17 +128,18 @@ int cond_sup(int addr1, int addr2){ /*prints the ASM instruction for the equality condition * and returns the address of the temporary variable*/ -int cond_eq(int addr1, int addr2){ - int addr = addCondAndGetAddress(); +int cond_eq(int addr1, int addr2) { + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "EQU @%d @%d @%d\n", addr, addr1, addr2); printOp(s); return addr; +} /*prints the ASM instruction for the negation condition * and returns the address of the temporary variable*/ int cond_not(int addr1){ - int addr = addCondAndGetAddress(); + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "NOT @%d @%d\n", addr, addr1); printOp(s); @@ -148,7 +149,7 @@ int cond_not(int addr1){ /*prints the ASM instruction for the and condition * and returns the address of the temporary variable*/ int cond_and(int addr1, int addr2){ - int addr = addCondAndGetAddress(); + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "AND @%d @%d @%d\n", addr, addr1, addr2); printOp(s); @@ -158,7 +159,7 @@ int cond_and(int addr1, int addr2){ /*prints the ASM instruction for the or condition * and returns the address of the temporary variable*/ int cond_or(int addr1, int addr2){ - int addr = addCondAndGetAddress(); + int addr = addTempCONDAndGetAddress(); char s[ASM_TEXT_LEN]; sprintf(s, "OR @%d @%d @%d\n", addr, addr1, addr2); printOp(s); diff --git a/yacc.y b/yacc.y index 6fa974c..2d6da98 100644 --- a/yacc.y +++ b/yacc.y @@ -11,6 +11,7 @@ int t; int labelWhileStart; int labelWhileEnd; +int whileJumpAddr; %} %code provides{ @@ -49,6 +50,8 @@ void yyerror (const char *); %type Declaration %type NbOrVariable %type IfStatement1 +%type Condition +%type InnerBlock %token tIF %token 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 : tLPAR ConditionalExpression tRPAR; +Condition : tLPAR ConditionalExpression tRPAR {$$ = $2;}; /*ConditionalExpression = expression that evaluates to a boolean*/ ConditionalExpression : tID { $$ = getOffset($1);} @@ -124,11 +127,15 @@ IfStatement : tIF Condition IfStatement1 InnerBlock tELSE { WhileStatement : tWHILE { $1 = getCurrentLineNumber(); } Condition { + int current = getCurrentLineNumber(); addLine("JMPF"); + setConditionAddr(current,$3); + whileJumpAddr = current; + suppressCONDElements(); } InnerBlock { addLine("JMP"); int current = getCurrentLineNumber(); - setJumpLine($1, current); + setJumpLine(whileJumpAddr, current); setJumpLine(current-1, $1); };