NOT REAL PUSH

This commit is contained in:
Raphaël LACROIX 2023-04-18 16:13:40 +02:00
parent 5d7d2a82f9
commit c71e340389
5 changed files with 137 additions and 9 deletions

32
asmTable.c Normal file
View file

@ -0,0 +1,32 @@
#include "asmTable.h"
int labelCounter = 0;
/*At the start of the execution : the whole array is empty*/
static char** table;
static int lineCounter = 0;
static int maxIndex = START_TABLE_SIZE;
#include "asmTable.h"
/* /!\ To be called at the beginning
* Initializes the array of Symbols*/
void initASMTable(){
table = malloc(sizeof(Symbol) * START_TABLE_SIZE);
}
/*resets the symbol table*/
void resetASMTable(){
currentIndex = 0;
maxIndex = START_TABLE_SIZE;
// stack pointers
esp = 0;
ebp = 0;
currentDepth = 0;
}
// todo inserer parser détecter les labels

28
asmTable.h Normal file
View file

@ -0,0 +1,28 @@
//
// Created by chepycou on 4/18/23.
//
#ifndef PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H
#define PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H
#define START_TABLE_SIZE 128
#define LINE_MAX_LENGTH 50
/*============================
Array and Reallocation
============================*/
/*reallocates the array with the specified size*/
void reallocateArray(int size);
/*Checks for the length of the array and reallocates if necessary*/
void checkArraySanity();
/* /!\ To be called at the beginning
* Initializes the array of Symbols*/
void initSymbolTable();
/*resets the symbol table*/
void resetSymboltable();
#endif //PROJET_SYSTEMES_INFORMATIQUES_ASMTABLE_H

47
blocs.c
View file

@ -3,22 +3,57 @@
//
#include "blocs.h"
#include "operations.h"
#include <stdio.h>
#include <string.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[LABEL_MAX_LENGTH];
sprintf(label, "%d_LABEL", labelCount);
int getNextLabel(){
return(labelCount++);
}
void printLabel(int labelWhile){
char label[LABEL_MAX_LENGTH];
sprintf(label,"%d_LABEL\n",labelWhile);
printf("%s",label);
FILE* fp;
fp = fopen("asm", "a");
fputs(label, fp);
fclose(fp);
}
int printNewLabel(){
int l = getNextLabel();
printLabel(l);
return l;
}
void printJumpToLabel(int labelWhile) {
char instr[ASM_TEXT_LEN];
sprintf(instr,"JMP %d_LABEL\n",labelWhile);
printf("%s",instr);
FILE* fp;
fp = fopen("asm", "a");
fputs(instr, fp);
fclose(fp);
}
void printJumpFToLabel(int labelWhile) {
char label[LABEL_MAX_LENGTH];
sprintf(label,"%d_LABEL\n",labelWhile);
printf("JMF %s",label);
FILE* fp;
fp = fopen("asm", "a");
fputs(label, fp);
fclose(fp);
}*/

16
blocs.h
View file

@ -5,8 +5,22 @@
#ifndef PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H
#define PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H
int labelCount;
#define LABEL_MAX_LENGTH 20
/*======================
Label Management
======================*/
/*returns the next label*/
int getNextLabel();
// prints a new label and returns the index of it
int printNewLabel();
// prints a label and returns the index of it
void printLabel(int labelWhile);
// prints a jump to the label and returns the index of it
void printJumpToLabel(int labelWhile);
#endif //PROJET_SYSTEMES_INFORMATIQUES_BLOCS_H

23
yacc.y
View file

@ -8,6 +8,8 @@
#include "blocs.h"
int t;
int labelWhileStart;
int labelWhileEnd;
%}
%code provides{
@ -95,10 +97,22 @@ NumericalOperator : tLE | tGE | tEQ | tNE | tLT | tGT;
Operation: tADD | tMUL | tSUB | tDIV;
*/
IfStatement : tIF Condition InnerBlock
IfStatement : tIF Condition {
printf(f, « JMPF ???\n ») ;
int ligne = get_nb_lignes_asm();
$1 = ligne ;
}
InnerBlock {
int current = get_nb_lignes_asm() ; // current == 4
patch($1, current + 2) ;
int ligne = insert(JMP) ; // ligne == L5
$1 = ligne ;
}
| tIF Condition InnerBlock tELSE InnerBlock
WhileStatement : tWHILE Condition InnerBlock;
WhileStatement : tWHILE {labelWhileStart = printNewLabel();}
Condition InnerBlock {printJumpToLabel(labelWhileStart);
labelWhileEnd = printNewLabel();};
Assignment : tID tASSIGN Expression tSEMI {
setInit($1); operation_copy(getOffset($1),$3); suppressTempINTElement();
@ -160,6 +174,11 @@ 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();