From 5d0902c86886fd70b3d25c12fece90ecd0c69a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20LACROIX?= Date: Tue, 18 Apr 2023 11:36:44 +0200 Subject: [PATCH] start of control/loops --- Makefile | 2 +- blocs.c | 24 ++++++++++++++++++++++++ blocs.h | 12 ++++++++++++ operations.c | 47 +++++++++++++++++++++++++++++++++++++++-------- operations.h | 10 +++++++++- table.c | 16 ++++++++++++++++ table.h | 3 +++ yacc.y | 15 ++++++++++----- 8 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 blocs.c create mode 100644 blocs.h diff --git a/Makefile b/Makefile index 20f7e57..4cc4dda 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ BIN=out CC=gcc CFLAGS=-Wall -g -OBJ=yacc.tab.o lex.yy.o table.o operations.o +OBJ=yacc.tab.o lex.yy.o table.o operations.o blocs.o all: $(BIN) @touch testFile # to prevent an error in case of deletion diff --git a/blocs.c b/blocs.c new file mode 100644 index 0000000..783783e --- /dev/null +++ b/blocs.c @@ -0,0 +1,24 @@ +// +// Created by chepycou on 4/18/23. +// + +#include "blocs.h" +#include + +/*TODO on écrit tout dans un fichier asm extérieur puis on : + * - fait un parcours pour stocker dans une liste chaînée par exemple les valeur de ligne des labels + * - faire un parcours pour retirer les labels au début des lignes + * - faire un parcours pour remplacer les labels par leur valeur + * */ + + + +int labelCount = 0; + +char* getNextLabel(){ + char label[NAME_MAX_LENGTH]; + sprintf(label, "%d_LABEL", labelCount); +} + + + diff --git a/blocs.h b/blocs.h new file mode 100644 index 0000000..8aa77ff --- /dev/null +++ b/blocs.h @@ -0,0 +1,12 @@ +// +// Created by chepycou on 4/18/23. +// + +#ifndef PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H +#define PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H + +int labelCount; +#define NAME_MAX_LENGTH 20 + + +#endif //PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H diff --git a/operations.c b/operations.c index 3d9e1a7..bf5e264 100644 --- a/operations.c +++ b/operations.c @@ -4,12 +4,31 @@ #include #include "table.h" +#include "operations.h" + +/*prints to the stdout and the out asm file*/ +void printOp(char* s){ + printf("%s",s); + FILE* fp; + fp = fopen("asm", "a"); + fputs(s, fp); + fclose(fp); +} + +/*clears the out asm file*/ +void clearOp(){ + FILE* fp; + fp = fopen("asm", "w"); + fclose(fp); +} /*prints the ASM instruction for the addition computation * and returns the address of the temporary variable*/ int operation_add(int addr1, int addr2){ int addr = addTempINTAndGetAddress(); - printf("ADD @%d @%d @%d\n", addr, addr1, addr2); + char s[ASM_TEXT_LEN]; + sprintf(s, "ADD @%d @%d @%d\n", addr, addr1, addr2); + printOp(s); return addr; } @@ -17,7 +36,9 @@ int operation_add(int addr1, int addr2){ * and returns the address of the temporary variable*/ int operation_sub(int addr1, int addr2){ int addr = addTempINTAndGetAddress(); - printf("SUB @%d @%d @%d\n", addr, addr1, addr2); + char s[ASM_TEXT_LEN]; + sprintf(s, "SUB @%d @%d @%d\n", addr, addr1, addr2); + printOp(s); return addr; } @@ -25,7 +46,9 @@ int operation_sub(int addr1, int addr2){ * and returns the address of the temporary variable*/ int operation_mul(int addr1, int addr2){ int addr = addTempINTAndGetAddress(); - printf("MUL @%d @%d @%d\n", addr, addr1, addr2); + char s[ASM_TEXT_LEN]; + sprintf(s, "MUL @%d @%d @%d\n", addr, addr1, addr2); + printOp(s); return addr; } @@ -33,7 +56,9 @@ int operation_mul(int addr1, int addr2){ * and returns the address of the temporary variable*/ int operation_divInt(int addr1, int addr2){ int addr = addTempINTAndGetAddress(); - printf("DIV_INT @%d @%d @%d\n", addr, addr1, addr2); + char s[ASM_TEXT_LEN]; + sprintf(s, "DIV_INT @%d @%d @%d\n", addr, addr1, addr2); + printOp(s); return addr; } @@ -41,7 +66,9 @@ int operation_divInt(int addr1, int addr2){ * and returns the address of the temporary variable*/ int operation_divRem(int addr1, int addr2){ int addr = addTempINTAndGetAddress(); - printf("DIV_REM @%d @%d @%d\n", addr, addr1, addr2); + char s[ASM_TEXT_LEN]; + sprintf(s, "DIV_REM @%d @%d @%d\n", addr, addr1, addr2); + printOp(s); return addr; } @@ -50,7 +77,9 @@ int operation_divRem(int addr1, int addr2){ * a = 2; */ void operation_afc_nb(int addr, int value){ - printf("AFC @%d #%d\n", addr, value); + char s[ASM_TEXT_LEN]; + sprintf(s, "AFC @%d #%d\n", addr, value); + printOp(s); } /*prints the ASM instruction for the affection of a temporary variable @@ -68,6 +97,8 @@ int operation_afc_nb_tmp(int value){ * EX : * a = b; */ -void operation_afc_addr(int addr1, int addr2){ - printf("AFC @%d @%d\n", addr1, addr2); +void operation_copy(int addr1, int addr2){ + char s[ASM_TEXT_LEN]; + sprintf(s, "COP @%d @%d\n", addr1, addr2); + printOp(s); } \ No newline at end of file diff --git a/operations.h b/operations.h index 9d46602..04a0eb5 100644 --- a/operations.h +++ b/operations.h @@ -5,6 +5,14 @@ #ifndef PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H #define PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H +#define ASM_TEXT_LEN 40 + +/*clears the out asm file*/ +void clearOp(); + +/*prints to the stdout and the out asm file*/ +void printOp(char* s); + /*prints the ASM instruction for the addition computation * and returns the address of the temporary variable*/ int operation_add(int addr1, int addr2); @@ -41,7 +49,7 @@ int operation_afc_nb_tmp(int value); * EX : * a = b; */ -void operation_afc_addr(int addr1, int addr2); +void operation_copy(int addr1, int addr2); #endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H diff --git a/table.c b/table.c index 29c3f8a..57d4674 100644 --- a/table.c +++ b/table.c @@ -26,6 +26,18 @@ void initSymbolTable(){ symbolTable = malloc(sizeof(Symbol) * START_TABLE_SIZE); } +/*resets the symbol table*/ +void resetSymboltable(){ + currentIndex = 0; + maxIndex = START_TABLE_SIZE; + +// stack pointers + esp = 0; + ebp = 0; + + currentDepth = 0; +} + /* Error display */ void error(char* mess){ @@ -56,6 +68,7 @@ int getIndex(char* name){ return i; } } + printf("%s",name); error("No index found"); return (0); } @@ -228,12 +241,15 @@ void displayTable(){ void suppressTempINTElement(){ if (tempCounter == 1){ suppressElement("0_TEMP"); + esp--; } else { if (tempCounter > 1){ suppressElement("0_TEMP"); suppressElement("1_TEMP"); + esp-= 2; } } + tempCounter = 0; } /*removes one element*/ diff --git a/table.h b/table.h index 52569c4..d6e653e 100644 --- a/table.h +++ b/table.h @@ -36,6 +36,9 @@ void checkArraySanity(); * Initializes the array of Symbols*/ void initSymbolTable(); +/*resets the symbol table*/ +void resetSymboltable(); + /*============================ Element Management diff --git a/yacc.y b/yacc.y index 94e88b1..3a47c1f 100644 --- a/yacc.y +++ b/yacc.y @@ -5,6 +5,7 @@ #include #include "table.h" #include "operations.h" +#include "blocs.h" int t; %} @@ -41,6 +42,7 @@ void yyerror (const char *); /* represents types with the values used in the table, see table.h */ %type Type %type Expression +%type Declaration %type NbOrVariable %start Program @@ -98,7 +100,9 @@ IfStatement : tIF Condition InnerBlock WhileStatement : tWHILE Condition InnerBlock; -Assignment : tID tASSIGN Expression tSEMI {setInit($1); operation_afc_addr(getOffset($1),$3); suppressTempINTElement();}; +Assignment : tID tASSIGN Expression tSEMI { + setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElement(); + }; /*Expression operation applied on variables or values*/ Expression : NbOrVariable {$$ = $1;} @@ -118,8 +122,8 @@ Expressions : Expression FunctionCall : tID tLPAR Expressions tRPAR; -FunctionDef : Type tID FunctionParams InnerBlock - | tVOID tID FunctionParams InnerBlock; +FunctionDef : Type tID FunctionParams InnerBlock {resetSymboltable();} + | tVOID tID FunctionParams InnerBlock {resetSymboltable();}; /*FunctionParams = the parameters of a function*/ FunctionParams : tLPAR tRPAR @@ -140,12 +144,12 @@ Type : tINT {$$ = INT;} Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ; Declaration : tID {addElement($1, (enumVarType) t);} - | tID tASSIGN Expression {addElement($1, (enumVarType) t);setInit($1);} ; + | tID {addElement($1, (enumVarType) t); setInit($1);} tASSIGN Expression {operation_copy(getOffset($1),$4); suppressTempINTElement();} ; Declarations1 : tCOMMA Declaration Declarations1 | {} ; -Return : tRETURN Expression tSEMI {decreaseDepth();}; +Return : tRETURN Expression tSEMI {}; Print : tPRINT tLPAR Expression tRPAR tSEMI; @@ -157,6 +161,7 @@ void yyerror(const char *msg) { } int main(void) { + clearOp(); initSymbolTable(); yyparse(); }