pointeur et fonction OK

This commit is contained in:
Nahom 2021-04-30 17:08:29 +02:00
parent bc2a2ee47d
commit c363024e17
15 changed files with 3143 additions and 2292 deletions

BIN
a.out

Binary file not shown.

View file

@ -39,6 +39,7 @@ tNOT "!"
tAND "&&" tAND "&&"
tOR "||" tOR "||"
tDIFF "!=" tDIFF "!="
tAPPERSAND "&"
DIGIT [0-9] DIGIT [0-9]
VARIABLE [A-Za-z0-9_]+ VARIABLE [A-Za-z0-9_]+
CONST "const" CONST "const"
@ -90,6 +91,7 @@ SEPARATOR {SPACE}|{TAB}
{tELSE} {return tELSE ;} {tELSE} {return tELSE ;}
{tWHILE} {return tWHILE ;} {tWHILE} {return tWHILE ;}
{tAPPERSAND} {return tAPPERSAND;}
{CONST} {return tCONST ;} {CONST} {return tCONST ;}
{ENTIER} {yylval.nombre = atoi(yytext); return tENTIER ;} {ENTIER} {yylval.nombre = atoi(yytext); return tENTIER ;}
{ENTIEREXP} {yylval.nombre = -1; return tENTIEREXP;} {ENTIEREXP} {yylval.nombre = -1; return tENTIEREXP;}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,13 +1,14 @@
/* A Bison parser, made by GNU Bison 3.0.4. */ /* A Bison parser, made by GNU Bison 2.3. */
/* Bison interface for Yacc-like parsers in C /* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc. Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation; either version 2, or (at your option)
(at your option) any later version. any later version.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -15,7 +16,9 @@
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains /* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work part or all of the Bison parser skeleton and distribute that work
@ -30,77 +33,99 @@
This special exception was added by the Free Software Foundation in This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */ version 2.2 of Bison. */
#ifndef YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED /* Tokens. */
# define YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 1
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Token type. */
#ifndef YYTOKENTYPE #ifndef YYTOKENTYPE
# define YYTOKENTYPE # define YYTOKENTYPE
enum yytokentype /* Put the tokens into the symbol table, so that GDB and other debuggers
{ know about them. */
tENTIER = 258, enum yytokentype {
tENTIEREXP = 259, tENTIER = 258,
tADD = 260, tENTIEREXP = 259,
tSUB = 261, tADD = 260,
tMUL = 262, tSUB = 261,
tDIV = 263, tMUL = 262,
tPO = 264, tDIV = 263,
tPF = 265, tPO = 264,
tAO = 266, tPF = 265,
tAF = 267, tAO = 266,
tERROR = 268, tAF = 267,
tPV = 269, tERROR = 268,
tVIRGULE = 270, tAPPERSAND = 269,
tAFFECTATION = 271, tPV = 270,
tEGAL = 272, tVIRGULE = 271,
tDIFF = 273, tAFFECTATION = 272,
tLT = 274, tEGAL = 273,
tGT = 275, tDIFF = 274,
tGTE = 276, tLT = 275,
tLTE = 277, tGT = 276,
tMAIN = 278, tGTE = 277,
tINT = 279, tLTE = 278,
tPRINT = 280, tMAIN = 279,
tRETURN = 281, tINT = 280,
tOR = 282, tPRINT = 281,
tAND = 283, tRETURN = 282,
tIF = 284, tOR = 283,
tELSE = 285, tAND = 284,
tWHILE = 286, tIF = 285,
tCONST = 287, tELSE = 286,
tVAR = 288, tWHILE = 287,
tNOT = 289 tCONST = 288,
}; tVAR = 289,
tNOT = 290
};
#endif #endif
/* Tokens. */
#define tENTIER 258
#define tENTIEREXP 259
#define tADD 260
#define tSUB 261
#define tMUL 262
#define tDIV 263
#define tPO 264
#define tPF 265
#define tAO 266
#define tAF 267
#define tERROR 268
#define tAPPERSAND 269
#define tPV 270
#define tVIRGULE 271
#define tAFFECTATION 272
#define tEGAL 273
#define tDIFF 274
#define tLT 275
#define tGT 276
#define tGTE 277
#define tLTE 278
#define tMAIN 279
#define tINT 280
#define tPRINT 281
#define tRETURN 282
#define tOR 283
#define tAND 284
#define tIF 285
#define tELSE 286
#define tWHILE 287
#define tCONST 288
#define tVAR 289
#define tNOT 290
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
union YYSTYPE #line 1 "analyse_syntaxique.y"
{ {
#line 1 "analyse_syntaxique.y" /* yacc.c:1909 */
int nombre; int nombre;
char id[30]; char id[30];
}
#line 94 "analyse_syntaxique.tab.h" /* yacc.c:1909 */ /* Line 1529 of yacc.c. */
}; #line 124 "analyse_syntaxique.tab.h"
YYSTYPE;
typedef union YYSTYPE YYSTYPE; # define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1 # define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1
#endif #endif
extern YYSTYPE yylval; extern YYSTYPE yylval;
int yyparse (void);
#endif /* !YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED */

View file

@ -6,13 +6,17 @@ char id[30];
%{ %{
#include <stdio.h> #include <stdio.h>
#include "table_symboles.h" #include "table_symboles.h"
#include "table_fonctions.h"
#include "gen_assembleur.h" #include "gen_assembleur.h"
enum Initialised_Variable init; enum Initialised_Variable init;
enum Symbole_Type type; enum Symbole_Type type;
enum Return_Type return_type;
Table_Symboles table; Table_Symboles table;
Table_Fonctions table_fonctions;
instructions_array array; instructions_array array;
int whileCondition; int whileCondition;
int return_value;
%} %}
@ -20,10 +24,9 @@ int whileCondition;
%token<nombre> tENTIEREXP %token<nombre> tENTIEREXP
%type<nombre> E %type<nombre> E
%type<nombre> Return %type<nombre> Return Instructions
%type<nombre> Cond %type<nombre> Cond
%type<nombre> While %type<nombre> While Else Invocation
%token tADD %token tADD
@ -38,6 +41,7 @@ int whileCondition;
%token tERROR %token tERROR
%token tAPPERSAND
%token tPV %token tPV
%token tVIRGULE %token tVIRGULE
%token tAFFECTATION %token tAFFECTATION
@ -69,28 +73,41 @@ int whileCondition;
%% %%
//C : Fonctions Main ; /*C : Fonctions Main ;
//Fonctions : Fonction Fonctions | ; Fonctions : ;
//Fonction : tINT tVAR tPO Params tPF Body; Fonctions : Fonction Fonctions ;
Fonction : tINT tVAR tPO Params tPF Body;*/
C : Fonctions;
Fonctions: Main;
Fonctions: Fonction Fonctions;
Main : tINT tMAIN tPO Params tPF Body {}; Main : tINT tMAIN {add_function(&table_fonctions, "Main", RET_INT, array.index); table.depth++;} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
Fonction : Function_type tVAR {{add_function(&table_fonctions, $2, return_type, array.index); table.depth++;}} tPO Params tPF Body {generate_instruction_0(&array, RET_FUN); print_table(&table);remove_symboles(&table); table.depth--;};
Params : {printf("Sans params\n");} ; Function_type: tINT {type = TYPE_INT;} ;
Function_type: tINT tMUL {type = TYPE_INT_PTR;};
Params : {} ;
Params : Param SuiteParams ; Params : Param SuiteParams ;
Param : tINT tVAR {printf("Parametre : %s\n", $2);} ; Param : Param_type tVAR {add_symbole_top(&table, $2, type, INITIALISED, table.depth);} ;
Param_type: tINT {type = TYPE_INT;} ;
Param_type: tINT tMUL {type = TYPE_INT_PTR;};
SuiteParams : tVIRGULE Param SuiteParams tPV ; SuiteParams : tVIRGULE Param SuiteParams ;
SuiteParams : ; SuiteParams : ;
Body : tAO Instructions Return tAF {printf("Dans body\n");} ; Body : tAO Instructions Return tAF {} ;
Instructions : Instruction Instructions ; Instructions : Instruction Instructions {$$ = array.index;};
Instructions : ; Instructions : {$$ = array.index;};
Instruction : Aff ; Instruction : Aff ;
Instruction : If ; Instruction : If ;
@ -104,51 +121,52 @@ Decl : Type Valeur SuiteDecl tPV ;
SuiteDecl: tVIRGULE Valeur SuiteDecl ; SuiteDecl: tVIRGULE Valeur SuiteDecl ;
SuiteDecl: ; SuiteDecl: ;
Type : tINT {type = TYPE_INT;} ; Type : tINT {type = TYPE_INT;} ;
Type : tCONST tINT {type = TYPE_CONST_INT;} ; Type : tCONST tINT {type = TYPE_CONST_INT;} ;
Type : tINT tMUL {type = TYPE_INT_PTR;};
Valeur : tVAR {add_symbole_top(&table, $1, type, INITIALISED, table.depth);} tAFFECTATION E {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, $4, varAddr); free_temp(&table);}; Valeur : tVAR {add_symbole_top(&table, $1, type, INITIALISED, table.depth);} tAFFECTATION E {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $4); free_temp(&table);};
Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);}; Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, $3, varAddr); free_temp(&table);}; Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $3); free_temp(&table);};
Aff : tMUL tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP_STR, varAddr, $4); free_temp(&table);};
//E : tENTIER {int vt = gen_entier(&table, &array, $1); $$ = vt;};
E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;}; E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
//E : tVAR {int vt = gen_var(&table, &array, $1); $$ = vt;}; E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, vt); $$ = vt;};
//E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ; E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;} ; E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ; E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ; E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
E : tSUB E {printf("Variable negative\n");} ; E : tSUB E {printf("Variable negative\n");} ;
E : Invocation {$$ = 1234;}; E : Invocation {
int vt = new_temp(&table);
generate_instruction_2(&array, COP, vt, $1);
remove_symboles(&table);
table.depth--;
$$ = vt;};
E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ; E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
E : tAPPERSAND tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, LEA, vt, varAddr); $$ = vt;};
E : tMUL tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP, vt, varAddr); generate_instruction_2(&array, COP_LD, vt, vt); $$ = vt;};
Args : tVAR SuiteArgs ;
Args : ;
SuiteArgs : tVIRGULE tVAR SuiteArgs ;
SuiteArgs : ;
If : tIF tPO Cond tPF { If : tIF tPO Cond tPF {
//gen_jmpf(&table, &array, $3, -1); //gen_jmpf(&table, &array, $3, -1);
generate_instruction_2(&array, JMF, $3, -1); generate_instruction_2(&array, JMF, $3, -1);
free_temp(&table); free_temp(&table);
$1 = array.index; $1 = array.index;
} }
tAO Instructions tAF tAO {table.depth++;} Instructions {generate_instruction_1(&array, JMP, -1)} tAF {remove_symboles(&table); table.depth--;}
{ {
int adr_jmp = array.index; int adr_jmp = array.index;
update_jmf(&array, $1, adr_jmp); update_jmf(&array, $1, adr_jmp);
} }
Else {}; Else {printf("updating jump\n"); update_jmp(&array, $8, $13);};
Else : tELSE tAO Instructions tAF {printf("else\n");} ; Else : tELSE tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {$$ = array.index;} ;
Else : ; Else : {$$ = array.index;};
Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ; Else : tELSE If {$$ = array.index;} ;
While : tWHILE tPO { While : tWHILE tPO {
$2 = array.index ; $2 = array.index ;
@ -158,14 +176,13 @@ While : tWHILE tPO {
free_temp(&table); free_temp(&table);
$1 = array.index; $1 = array.index;
} }
tAO Instructions tAF { tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
int adr_jmp = array.index; int adr_jmp = array.index;
update_jmf(&array, $1, adr_jmp); update_jmf(&array, $1, adr_jmp);
//gen_jmpf(&table, &array, $1, $2); //gen_jmpf(&table, &array, $1, $2);
generate_instruction_1(&array, JMP, $2); generate_instruction_1(&array, JMP, $2);
}; };
//Cond : E tEGAL E {int vt = gen_condition(&table, &array, EQ, $1, $3); $$ = vt;} ;
Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;}; Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ; Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ; Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
@ -177,15 +194,21 @@ Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table
Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ; Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
Cond : E {$$ = $1; }; Cond : E {$$ = $1; };
Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableTop - 1);} Args tPF
{int function_index = function_exists(&table_fonctions, $1);
int jmp_addr = (table_fonctions.array[function_index]).start_addr;
generate_instruction_2(&array, CALL, jmp_addr, (array.index + 1));
$$ = return_value;
};
Args : Arg SuiteArgs ;
Args :
Arg : E {int arg_addr = prepare_argument_push(&table); generate_instruction_2(&array, COP, arg_addr, $1); free_temp(&table)};
SuiteArgs : tVIRGULE Arg SuiteArgs ;
SuiteArgs : ;
Invocation : tVAR tPO Args tPF {printf("Dans invocation\n");};
//Print : tPRINT tPO E tPF tPV {gen_print(&table, &array, $3);};
Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);}; Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
//Return : tRETURN E tPV {$$ = gen_return(&table, &array, $2);};
Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);}; Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
%% %%
@ -193,9 +216,13 @@ Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(
void main(void){ void main(void){
//TODO: rajouter gestion des erreurs //TODO: rajouter gestion des erreurs
initialise_table(&table); initialise_table(&table);
initialise_function_table(&table_fonctions);
initialise_asm(&array); initialise_asm(&array);
yyparse(); yyparse();
print_table(&table); print_table(&table);
printf("\n");
print_fonction_table(&table_fonctions);
//remove_symboles(&table, 0); //remove_symboles(&table, 0);
//print_table(&table); //print_table(&table);
exportInstructions(&array); exportInstructions(&array);

View file

@ -33,7 +33,7 @@ char * operationName(enum operation op){
case JMF: case JMF:
return "JPF"; return "JPF";
case JMP: case JMP:
return "JPM"; return "JMP";
case AND: case AND:
return "AND"; return "AND";
case OR: case OR:
@ -42,6 +42,16 @@ char * operationName(enum operation op){
return "NOT"; return "NOT";
case PRI: case PRI:
return "PRI"; return "PRI";
case LEA:
return "LEA";
case COP_LD:
return "COP_LD";
case COP_STR:
return "COP_STR";
case RET_FUN:
return "RET_FUN";
case CALL:
return "CALL";
default: default:
break; break;
} }
@ -71,6 +81,22 @@ int new_temp(Table_Symboles * table){
return ret_addr; return ret_addr;
} }
int generate_instruction_0(instructions_array * array, enum operation op){
instruction instru;
char * opName = operationName(op);
instru.operation = op;
printf("%d\t %s\n", array->index, opName);
if (add_instruction(array, &instru) != 0){
//TODO: Error handling
exit(1);
}
return 0;
}
int generate_instruction_1(instructions_array * array, enum operation op, int arg1){ int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
instruction instru; instruction instru;
char * opName = operationName(op); char * opName = operationName(op);
@ -130,6 +156,11 @@ void update_jmf(instructions_array * array, int instru_index, int adr_jmp){
printf("%d\t JMP %d %d\n", (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2); printf("%d\t JMP %d %d\n", (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
} }
void update_jmp(instructions_array * array, int instru_index, int adr_jmp){
array->array[instru_index].reg1 = adr_jmp;
printf("%d\t JMP %d\n", (instru_index - 1), array->array[instru_index].reg1);
}
void exportInstructions(instructions_array * array){ void exportInstructions(instructions_array * array){
FILE *file; FILE *file;
file = fopen("instructions.txt", "w"); file = fopen("instructions.txt", "w");
@ -140,6 +171,10 @@ void exportInstructions(instructions_array * array){
instru = array->array[i]; instru = array->array[i];
op = instru.operation; op = instru.operation;
switch (op) { switch (op) {
//0 parameters
case RET_FUN:
fprintf(file, "%d\t %s\n", i, operationName(op));
break;
//1 parameter //1 parameter
case JMP: case JMP:
case PRI: case PRI:
@ -151,8 +186,16 @@ void exportInstructions(instructions_array * array){
case NOT: case NOT:
case AFC: case AFC:
case COP: case COP:
case LEA:
case CALL:
fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2); fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
break; break;
case COP_LD:
fprintf(file, "%d\t %s %d [%d]\n", i, operationName(op), instru.reg1, instru.reg2);
break;
case COP_STR:
fprintf(file, "%d\t %s [%d] %d\n", i, operationName(op), instru.reg1, instru.reg2);
break;
//3 parameters //3 parameters
case ADD: case ADD:
case SOU: case SOU:

View file

@ -5,7 +5,8 @@
#include "table_symboles.h" #include "table_symboles.h"
enum operation{ADD, SOU, MUL, DIV, COP, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE, GTE, AND, OR, NOT, PRI}; enum operation{ADD, SOU, MUL, DIV, COP, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE,
GTE, AND, OR, NOT, PRI, LEA, COP_LD, COP_STR, CALL, RET_FUN};
typedef struct instruction{ typedef struct instruction{
enum operation operation; enum operation operation;
@ -49,6 +50,14 @@ int new_temp(Table_Symboles * table);
*/ */
int add_instruction(instructions_array * array, instruction * intru); int add_instruction(instructions_array * array, instruction * intru);
/**
* Generates intruction with no parameter
* @param array
* @param op
* @return
*/
int generate_instruction_0(instructions_array * array, enum operation op);
/** /**
* Generates intruction with one parameter * Generates intruction with one parameter
* @param array * @param array
@ -87,6 +96,7 @@ int generate_instruction_3(instructions_array * array, enum operation op, int ar
*/ */
void update_jmf(instructions_array * array, int instru_index, int adr_jmp); void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
void update_jmp(instructions_array * array, int instru_index, int adr_jmp);

View file

@ -1,22 +1,11 @@
0 AFC 49 1 0 AFC 49 2
1 COP 49 0 1 COP 0 49
2 AFC 49 2 2 LEA 49 0
3 COP 49 1 3 COP 1 49
4 AFC 49 2 4 AFC 49 2
5 AFC 48 3 5 COP_STR [1] 49
6 ADD 49 49 48 6 COP 49 1
7 COP 49 1 7 COP_LD 49 [49]
8 COP 1 49 8 COP 2 49
9 NOT 49 49 9 AFC 49 0
10 JPF 49 15 10 RET 49
11 AFC 49 1
12 COP 49 1
13 AFC 49 4
14 COP 49 2
15 AFC 49 4
16 AFC 48 2
17 COP 48 0
18 COP 1 48
19 PRI 48
20 AFC 48 5
21 RET 48

547
lex.yy.c

File diff suppressed because it is too large Load diff

View file

@ -3,17 +3,12 @@ flex analyse_lexicale.lex
gcc -w *.c -ly gcc -w *.c -ly
echo " echo "
int main(){ int main(){
int var1 = 1; int c = 2;
int var2 = 2; int * p;
var2 = 2 + 3; p = &c;
int var3; *p = 2;
if (!var2){ int b = *p;
var2 = 1;
var3 = 4; return 0;
} else {
var1 = 2;
}
printf(var2);
return 5;
} }
" | ./a.out " | ./a.out

59
table_fonctions.c Normal file
View file

@ -0,0 +1,59 @@
#include "table_fonctions.h"
#include <string.h>
#include <stdio.h>
void initialise_function_table(Table_Fonctions * table){
table->depth = 1;
}
void add_function(Table_Fonctions * table, char * function_name, enum Return_Type return_type, int start_addr){
Fonction fonction;
strcpy(fonction.function_name,function_name);
fonction.start_addr = start_addr;
fonction.type = return_type;
fonction.function_depth = table->depth;
table->array[table->depth] = fonction;
table->depth++;
}
void print_function(Fonction * fonction){
char * function_name = fonction->function_name;
int start_addr = fonction->start_addr;
int depth = fonction->function_depth;
int return_type = fonction->type;
char typeStr[20];
if (return_type == RET_INT){
strcpy(typeStr, "INT");
} else if (return_type == RET_INT_PTR){
strcpy(typeStr, "INT_PTR");
}
printf("%-20s\t\t %-12s\t\t %-12d\t %-12d\n", function_name, typeStr, start_addr, depth);
}
void print_fonction_table(Table_Fonctions * table) {
printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\n", "Function Name", "Return Type", "Start Address", "Depth");
Fonction fonction;
for (int i = 1; i < table->depth; i++) {
fonction = table->array[i];
print_function(&fonction);
}
}
int function_exists(Table_Fonctions * table, char * func_name){
for (int i = 0; i < table->depth; i++){
if (strcmp(table->array[i].function_name, func_name) == 0){
return i;
}
}
return -1;
}
/*
int main(){
Table_Fonctions table;
initialise_function_table(&table);
add_function(&table, "Fonction1", 0, 7);
add_function(&table, "Fonction2", 1, 23);
print_fonction_table(&table);
return 1;
}*/

35
table_fonctions.h Normal file
View file

@ -0,0 +1,35 @@
//
// Created by Nahom Belay on 29/04/2021.
//
#ifndef PROJET_SYSTEME_TABLE_FONCTIONS_H
#define PROJET_SYSTEME_TABLE_FONCTIONS_H
#define FUNCTION_TABLE_SIZE 50
#define FUNCTION_NAME_SIZE 30
enum Return_Type {RET_INT , RET_INT_PTR};
typedef struct Fonction {
char function_name[FUNCTION_NAME_SIZE];
int start_addr ;
enum Return_Type type;
int function_depth;
} Fonction;
typedef struct Table_Fonctions {
Fonction array[FUNCTION_TABLE_SIZE];
int depth;
} Table_Fonctions;
void initialise_function_table(Table_Fonctions * table);
void add_function(Table_Fonctions * table, char * function_name, enum Return_Type return_type, int start_addr);
void print_fonction_table(Table_Fonctions * table);
int function_exists(Table_Fonctions * table, char * func_name);
#endif //PROJET_SYSTEME_TABLE_FONCTIONS_H

View file

@ -54,25 +54,47 @@ int add_symbole_bottom(Table_Symboles * table){
return 0; return 0;
} }
int remove_symboles(Table_Symboles * table, int depth){ int remove_symboles(Table_Symboles * table){
int index; if (table->indexAvailableTop > 0){
if (depth < table->depth){ while(table->indexAvailableTop > 0){
return -1; if (table->array[table->indexAvailableTop-1].symbole_depth == table->depth){
} else { table->indexAvailableTop--;
index = table->indexAvailableTop; } else {
while(table->array[index].symbole_depth == depth && index >=0){ break;
//remove }
table->indexAvailableTop--;
index--;
} }
}
//TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
}
return 0; return 0;
} }
void free_temp(Table_Symboles * table){ void free_temp(Table_Symboles * table){
table->indexAvailableBottom++; table->indexAvailableBottom++;
if (table->indexAvailableBottom >= TABLE_SIZE){
printf("Huge error\n");
table->indexAvailableBottom--;
}
}
int prepare_function_call(Table_Symboles * table){
prepare_argument_push(table);
prepare_argument_push(table);
prepare_argument_push(table);
}
int prepare_argument_push(Table_Symboles * table){
Symbole symbole;
symbole.addr = table->indexAvailableTop;
symbole.symbole_depth = table->depth;
if (table->indexAvailableTop < table->indexAvailableBottom){
table->array[table->indexAvailableTop] = symbole;
table->indexAvailableTop++;
return (table->indexAvailableTop) - 1 ;
}
} }
int initialise_symbole(Table_Symboles * table, char * varName){ int initialise_symbole(Table_Symboles * table, char * varName){
@ -91,8 +113,12 @@ void print_symbole(Symbole * symbole){
char typeStr[20]; char typeStr[20];
if (type == TYPE_INT){ if (type == TYPE_INT){
strcpy(typeStr, "INT"); strcpy(typeStr, "INT");
} else{ } else if (type == TYPE_CONST_INT){
strcpy(typeStr, "CONST_INT"); strcpy(typeStr, "CONST_INT");
} else if (type == TYPE_INT_PTR) {
strcpy(typeStr, "INT_PTR");
} else {
strcpy(typeStr, "Error type");
} }
enum Initialised_Variable init = symbole->init; enum Initialised_Variable init = symbole->init;
char initStr[20]; char initStr[20];

View file

@ -4,7 +4,7 @@
#define TABLE_SIZE 50 #define TABLE_SIZE 50
#define VARIABLE_SIZE 30 #define VARIABLE_SIZE 30
enum Symbole_Type {TYPE_INT , TYPE_CONST_INT}; enum Symbole_Type {TYPE_INT , TYPE_CONST_INT, TYPE_INT_PTR};
enum Initialised_Variable{INITIALISED , NOT_INITIALISED}; enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
typedef struct Symboles { typedef struct Symboles {
@ -58,14 +58,18 @@ int variable_exists(Table_Symboles * table, char * varName);
/** /**
* Removes symbole from table having certain depth * Removes symbole from table having certain depth
* @param table * @param table
* @param depth
* @return -1 if the symbole isn't in the table, 0 otherwise * @return -1 if the symbole isn't in the table, 0 otherwise
*/ */
int remove_symboles(Table_Symboles * table, int depth); int remove_symboles(Table_Symboles * table);
void free_temp(Table_Symboles * table); void free_temp(Table_Symboles * table);
int prepare_function_call(Table_Symboles * table);
int prepare_argument_push(Table_Symboles * table);
/** /**
* Initialises an already exisiting symbole * Initialises an already exisiting symbole
* @param table * @param table