start of control/loops

This commit is contained in:
Raphaël LACROIX 2023-04-18 11:36:44 +02:00
parent dad1f6d927
commit 5d0902c868
8 changed files with 114 additions and 15 deletions

View file

@ -5,7 +5,7 @@ BIN=out
CC=gcc CC=gcc
CFLAGS=-Wall -g 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) all: $(BIN)
@touch testFile # to prevent an error in case of deletion @touch testFile # to prevent an error in case of deletion

24
blocs.c Normal file
View file

@ -0,0 +1,24 @@
//
// Created by chepycou on 4/18/23.
//
#include "blocs.h"
#include <stdio.h>
/*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);
}

12
blocs.h Normal file
View file

@ -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

View file

@ -4,12 +4,31 @@
#include <stdio.h> #include <stdio.h>
#include "table.h" #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 /*prints the ASM instruction for the addition computation
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_add(int addr1, int addr2){ int operation_add(int addr1, int addr2){
int addr = addTempINTAndGetAddress(); 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; return addr;
} }
@ -17,7 +36,9 @@ int operation_add(int addr1, int addr2){
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_sub(int addr1, int addr2){ int operation_sub(int addr1, int addr2){
int addr = addTempINTAndGetAddress(); 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; return addr;
} }
@ -25,7 +46,9 @@ int operation_sub(int addr1, int addr2){
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_mul(int addr1, int addr2){ int operation_mul(int addr1, int addr2){
int addr = addTempINTAndGetAddress(); 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; return addr;
} }
@ -33,7 +56,9 @@ int operation_mul(int addr1, int addr2){
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_divInt(int addr1, int addr2){ int operation_divInt(int addr1, int addr2){
int addr = addTempINTAndGetAddress(); 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; return addr;
} }
@ -41,7 +66,9 @@ int operation_divInt(int addr1, int addr2){
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_divRem(int addr1, int addr2){ int operation_divRem(int addr1, int addr2){
int addr = addTempINTAndGetAddress(); 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; return addr;
} }
@ -50,7 +77,9 @@ int operation_divRem(int addr1, int addr2){
* a = 2; * a = 2;
*/ */
void operation_afc_nb(int addr, int value){ 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 /*prints the ASM instruction for the affection of a temporary variable
@ -68,6 +97,8 @@ int operation_afc_nb_tmp(int value){
* EX : * EX :
* a = b; * a = b;
*/ */
void operation_afc_addr(int addr1, int addr2){ void operation_copy(int addr1, int addr2){
printf("AFC @%d @%d\n", addr1, addr2); char s[ASM_TEXT_LEN];
sprintf(s, "COP @%d @%d\n", addr1, addr2);
printOp(s);
} }

View file

@ -5,6 +5,14 @@
#ifndef PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H #ifndef PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H
#define 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 /*prints the ASM instruction for the addition computation
* and returns the address of the temporary variable*/ * and returns the address of the temporary variable*/
int operation_add(int addr1, int addr2); int operation_add(int addr1, int addr2);
@ -41,7 +49,7 @@ int operation_afc_nb_tmp(int value);
* EX : * EX :
* a = b; * a = b;
*/ */
void operation_afc_addr(int addr1, int addr2); void operation_copy(int addr1, int addr2);
#endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H #endif //PROJET_SYSTEMES_INFORMATIQUES_OPERATIONS_H

16
table.c
View file

@ -26,6 +26,18 @@ void initSymbolTable(){
symbolTable = malloc(sizeof(Symbol) * START_TABLE_SIZE); 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 */ /* Error display */
void error(char* mess){ void error(char* mess){
@ -56,6 +68,7 @@ int getIndex(char* name){
return i; return i;
} }
} }
printf("%s",name);
error("No index found"); error("No index found");
return (0); return (0);
} }
@ -228,12 +241,15 @@ void displayTable(){
void suppressTempINTElement(){ void suppressTempINTElement(){
if (tempCounter == 1){ if (tempCounter == 1){
suppressElement("0_TEMP"); suppressElement("0_TEMP");
esp--;
} else { } else {
if (tempCounter > 1){ if (tempCounter > 1){
suppressElement("0_TEMP"); suppressElement("0_TEMP");
suppressElement("1_TEMP"); suppressElement("1_TEMP");
esp-= 2;
} }
} }
tempCounter = 0;
} }
/*removes one element*/ /*removes one element*/

View file

@ -36,6 +36,9 @@ void checkArraySanity();
* Initializes the array of Symbols*/ * Initializes the array of Symbols*/
void initSymbolTable(); void initSymbolTable();
/*resets the symbol table*/
void resetSymboltable();
/*============================ /*============================
Element Management Element Management

15
yacc.y
View file

@ -5,6 +5,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "table.h" #include "table.h"
#include "operations.h" #include "operations.h"
#include "blocs.h"
int t; int t;
%} %}
@ -41,6 +42,7 @@ void yyerror (const char *);
/* represents types with the values used in the table, see table.h */ /* represents types with the values used in the table, see table.h */
%type <type> Type %type <type> Type
%type <addr> Expression %type <addr> Expression
%type <addr> Declaration
%type <addr> NbOrVariable %type <addr> NbOrVariable
%start Program %start Program
@ -98,7 +100,9 @@ IfStatement : tIF Condition InnerBlock
WhileStatement : tWHILE 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 operation applied on variables or values*/
Expression : NbOrVariable {$$ = $1;} Expression : NbOrVariable {$$ = $1;}
@ -118,8 +122,8 @@ Expressions : Expression
FunctionCall : tID tLPAR Expressions tRPAR; FunctionCall : tID tLPAR Expressions tRPAR;
FunctionDef : Type tID FunctionParams InnerBlock FunctionDef : Type tID FunctionParams InnerBlock {resetSymboltable();}
| tVOID tID FunctionParams InnerBlock; | tVOID tID FunctionParams InnerBlock {resetSymboltable();};
/*FunctionParams = the parameters of a function*/ /*FunctionParams = the parameters of a function*/
FunctionParams : tLPAR tRPAR FunctionParams : tLPAR tRPAR
@ -140,12 +144,12 @@ Type : tINT {$$ = INT;}
Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ; Declarations : Type { t = $1; } Declaration Declarations1 tSEMI ;
Declaration : tID {addElement($1, (enumVarType) t);} 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 | {} ; Declarations1 : tCOMMA Declaration Declarations1 | {} ;
Return : tRETURN Expression tSEMI {decreaseDepth();}; Return : tRETURN Expression tSEMI {};
Print : tPRINT tLPAR Expression tRPAR tSEMI; Print : tPRINT tLPAR Expression tRPAR tSEMI;
@ -157,6 +161,7 @@ void yyerror(const char *msg) {
} }
int main(void) { int main(void) {
clearOp();
initSymbolTable(); initSymbolTable();
yyparse(); yyparse();
} }