pointeur et fonction OK
This commit is contained in:
parent
bc2a2ee47d
commit
c363024e17
15 changed files with 3143 additions and 2292 deletions
BIN
a.out
BIN
a.out
Binary file not shown.
|
@ -39,6 +39,7 @@ tNOT "!"
|
|||
tAND "&&"
|
||||
tOR "||"
|
||||
tDIFF "!="
|
||||
tAPPERSAND "&"
|
||||
DIGIT [0-9]
|
||||
VARIABLE [A-Za-z0-9_]+
|
||||
CONST "const"
|
||||
|
@ -90,6 +91,7 @@ SEPARATOR {SPACE}|{TAB}
|
|||
{tELSE} {return tELSE ;}
|
||||
{tWHILE} {return tWHILE ;}
|
||||
|
||||
{tAPPERSAND} {return tAPPERSAND;}
|
||||
{CONST} {return tCONST ;}
|
||||
{ENTIER} {yylval.nombre = atoi(yytext); return tENTIER ;}
|
||||
{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
|
@ -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
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
the Free Software Foundation; either version 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
|
@ -15,7 +16,9 @@
|
|||
GNU General Public License for more details.
|
||||
|
||||
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
|
||||
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
|
||||
version 2.2 of Bison. */
|
||||
|
||||
#ifndef YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED
|
||||
# define YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED
|
||||
/* Debug traces. */
|
||||
#ifndef YYDEBUG
|
||||
# define YYDEBUG 1
|
||||
#endif
|
||||
#if YYDEBUG
|
||||
extern int yydebug;
|
||||
#endif
|
||||
|
||||
/* Token type. */
|
||||
/* Tokens. */
|
||||
#ifndef YYTOKENTYPE
|
||||
# define YYTOKENTYPE
|
||||
enum yytokentype
|
||||
{
|
||||
tENTIER = 258,
|
||||
tENTIEREXP = 259,
|
||||
tADD = 260,
|
||||
tSUB = 261,
|
||||
tMUL = 262,
|
||||
tDIV = 263,
|
||||
tPO = 264,
|
||||
tPF = 265,
|
||||
tAO = 266,
|
||||
tAF = 267,
|
||||
tERROR = 268,
|
||||
tPV = 269,
|
||||
tVIRGULE = 270,
|
||||
tAFFECTATION = 271,
|
||||
tEGAL = 272,
|
||||
tDIFF = 273,
|
||||
tLT = 274,
|
||||
tGT = 275,
|
||||
tGTE = 276,
|
||||
tLTE = 277,
|
||||
tMAIN = 278,
|
||||
tINT = 279,
|
||||
tPRINT = 280,
|
||||
tRETURN = 281,
|
||||
tOR = 282,
|
||||
tAND = 283,
|
||||
tIF = 284,
|
||||
tELSE = 285,
|
||||
tWHILE = 286,
|
||||
tCONST = 287,
|
||||
tVAR = 288,
|
||||
tNOT = 289
|
||||
};
|
||||
/* Put the tokens into the symbol table, so that GDB and other debuggers
|
||||
know about them. */
|
||||
enum yytokentype {
|
||||
tENTIER = 258,
|
||||
tENTIEREXP = 259,
|
||||
tADD = 260,
|
||||
tSUB = 261,
|
||||
tMUL = 262,
|
||||
tDIV = 263,
|
||||
tPO = 264,
|
||||
tPF = 265,
|
||||
tAO = 266,
|
||||
tAF = 267,
|
||||
tERROR = 268,
|
||||
tAPPERSAND = 269,
|
||||
tPV = 270,
|
||||
tVIRGULE = 271,
|
||||
tAFFECTATION = 272,
|
||||
tEGAL = 273,
|
||||
tDIFF = 274,
|
||||
tLT = 275,
|
||||
tGT = 276,
|
||||
tGTE = 277,
|
||||
tLTE = 278,
|
||||
tMAIN = 279,
|
||||
tINT = 280,
|
||||
tPRINT = 281,
|
||||
tRETURN = 282,
|
||||
tOR = 283,
|
||||
tAND = 284,
|
||||
tIF = 285,
|
||||
tELSE = 286,
|
||||
tWHILE = 287,
|
||||
tCONST = 288,
|
||||
tVAR = 289,
|
||||
tNOT = 290
|
||||
};
|
||||
#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
|
||||
|
||||
union YYSTYPE
|
||||
typedef union YYSTYPE
|
||||
#line 1 "analyse_syntaxique.y"
|
||||
{
|
||||
#line 1 "analyse_syntaxique.y" /* yacc.c:1909 */
|
||||
|
||||
int nombre;
|
||||
char id[30];
|
||||
|
||||
#line 94 "analyse_syntaxique.tab.h" /* yacc.c:1909 */
|
||||
};
|
||||
|
||||
typedef union YYSTYPE YYSTYPE;
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
}
|
||||
/* Line 1529 of yacc.c. */
|
||||
#line 124 "analyse_syntaxique.tab.h"
|
||||
YYSTYPE;
|
||||
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
|
||||
# define YYSTYPE_IS_DECLARED 1
|
||||
# define YYSTYPE_IS_TRIVIAL 1
|
||||
#endif
|
||||
|
||||
|
||||
extern YYSTYPE yylval;
|
||||
|
||||
int yyparse (void);
|
||||
|
||||
#endif /* !YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED */
|
||||
|
|
|
@ -6,13 +6,17 @@ char id[30];
|
|||
%{
|
||||
#include <stdio.h>
|
||||
#include "table_symboles.h"
|
||||
#include "table_fonctions.h"
|
||||
#include "gen_assembleur.h"
|
||||
|
||||
enum Initialised_Variable init;
|
||||
enum Symbole_Type type;
|
||||
enum Return_Type return_type;
|
||||
Table_Symboles table;
|
||||
Table_Fonctions table_fonctions;
|
||||
instructions_array array;
|
||||
int whileCondition;
|
||||
int return_value;
|
||||
%}
|
||||
|
||||
|
||||
|
@ -20,10 +24,9 @@ int whileCondition;
|
|||
%token<nombre> tENTIEREXP
|
||||
|
||||
%type<nombre> E
|
||||
%type<nombre> Return
|
||||
%type<nombre> Return Instructions
|
||||
%type<nombre> Cond
|
||||
%type<nombre> While
|
||||
|
||||
%type<nombre> While Else Invocation
|
||||
|
||||
|
||||
%token tADD
|
||||
|
@ -38,6 +41,7 @@ int whileCondition;
|
|||
|
||||
%token tERROR
|
||||
|
||||
%token tAPPERSAND
|
||||
%token tPV
|
||||
%token tVIRGULE
|
||||
%token tAFFECTATION
|
||||
|
@ -69,28 +73,41 @@ int whileCondition;
|
|||
|
||||
%%
|
||||
|
||||
//C : Fonctions Main ;
|
||||
/*C : Fonctions Main ;
|
||||
|
||||
//Fonctions : Fonction Fonctions | ;
|
||||
//Fonction : tINT tVAR tPO Params tPF Body;
|
||||
Fonctions : ;
|
||||
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 ;
|
||||
|
||||
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 : ;
|
||||
|
||||
|
||||
Body : tAO Instructions Return tAF {printf("Dans body\n");} ;
|
||||
Body : tAO Instructions Return tAF {} ;
|
||||
|
||||
Instructions : Instruction Instructions ;
|
||||
Instructions : ;
|
||||
Instructions : Instruction Instructions {$$ = array.index;};
|
||||
Instructions : {$$ = array.index;};
|
||||
|
||||
Instruction : Aff ;
|
||||
Instruction : If ;
|
||||
|
@ -106,49 +123,50 @@ SuiteDecl: ;
|
|||
|
||||
Type : tINT {type = TYPE_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);};
|
||||
|
||||
|
||||
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 : 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, varAddr, vt); $$ = vt;};
|
||||
//E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
|
||||
E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
|
||||
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 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 : 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 : 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 {
|
||||
//gen_jmpf(&table, &array, $3, -1);
|
||||
generate_instruction_2(&array, JMF, $3, -1);
|
||||
free_temp(&table);
|
||||
$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;
|
||||
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 : ;
|
||||
Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ;
|
||||
Else : tELSE tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {$$ = array.index;} ;
|
||||
Else : {$$ = array.index;};
|
||||
Else : tELSE If {$$ = array.index;} ;
|
||||
|
||||
While : tWHILE tPO {
|
||||
$2 = array.index ;
|
||||
|
@ -158,14 +176,13 @@ While : tWHILE tPO {
|
|||
free_temp(&table);
|
||||
$1 = array.index;
|
||||
}
|
||||
tAO Instructions tAF {
|
||||
tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
|
||||
int adr_jmp = array.index;
|
||||
update_jmf(&array, $1, adr_jmp);
|
||||
//gen_jmpf(&table, &array, $1, $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 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;} ;
|
||||
|
@ -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 : 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);};
|
||||
|
||||
//Return : tRETURN E tPV {$$ = gen_return(&table, &array, $2);};
|
||||
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){
|
||||
//TODO: rajouter gestion des erreurs
|
||||
initialise_table(&table);
|
||||
initialise_function_table(&table_fonctions);
|
||||
initialise_asm(&array);
|
||||
yyparse();
|
||||
print_table(&table);
|
||||
printf("\n");
|
||||
print_fonction_table(&table_fonctions);
|
||||
|
||||
//remove_symboles(&table, 0);
|
||||
//print_table(&table);
|
||||
exportInstructions(&array);
|
||||
|
|
|
@ -33,7 +33,7 @@ char * operationName(enum operation op){
|
|||
case JMF:
|
||||
return "JPF";
|
||||
case JMP:
|
||||
return "JPM";
|
||||
return "JMP";
|
||||
case AND:
|
||||
return "AND";
|
||||
case OR:
|
||||
|
@ -42,6 +42,16 @@ char * operationName(enum operation op){
|
|||
return "NOT";
|
||||
case 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:
|
||||
break;
|
||||
}
|
||||
|
@ -71,6 +81,22 @@ int new_temp(Table_Symboles * table){
|
|||
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){
|
||||
instruction instru;
|
||||
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);
|
||||
}
|
||||
|
||||
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){
|
||||
FILE *file;
|
||||
file = fopen("instructions.txt", "w");
|
||||
|
@ -140,6 +171,10 @@ void exportInstructions(instructions_array * array){
|
|||
instru = array->array[i];
|
||||
op = instru.operation;
|
||||
switch (op) {
|
||||
//0 parameters
|
||||
case RET_FUN:
|
||||
fprintf(file, "%d\t %s\n", i, operationName(op));
|
||||
break;
|
||||
//1 parameter
|
||||
case JMP:
|
||||
case PRI:
|
||||
|
@ -151,8 +186,16 @@ void exportInstructions(instructions_array * array){
|
|||
case NOT:
|
||||
case AFC:
|
||||
case COP:
|
||||
case LEA:
|
||||
case CALL:
|
||||
fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
|
||||
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
|
||||
case ADD:
|
||||
case SOU:
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
|
||||
#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{
|
||||
enum operation operation;
|
||||
|
@ -49,6 +50,14 @@ int new_temp(Table_Symboles * table);
|
|||
*/
|
||||
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
|
||||
* @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_jmp(instructions_array * array, int instru_index, int adr_jmp);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,22 +1,11 @@
|
|||
0 AFC 49 1
|
||||
1 COP 49 0
|
||||
2 AFC 49 2
|
||||
3 COP 49 1
|
||||
0 AFC 49 2
|
||||
1 COP 0 49
|
||||
2 LEA 49 0
|
||||
3 COP 1 49
|
||||
4 AFC 49 2
|
||||
5 AFC 48 3
|
||||
6 ADD 49 49 48
|
||||
7 COP 49 1
|
||||
8 COP 1 49
|
||||
9 NOT 49 49
|
||||
10 JPF 49 15
|
||||
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
|
||||
5 COP_STR [1] 49
|
||||
6 COP 49 1
|
||||
7 COP_LD 49 [49]
|
||||
8 COP 2 49
|
||||
9 AFC 49 0
|
||||
10 RET 49
|
||||
|
|
19
script.sh
19
script.sh
|
@ -3,17 +3,12 @@ flex analyse_lexicale.lex
|
|||
gcc -w *.c -ly
|
||||
echo "
|
||||
int main(){
|
||||
int var1 = 1;
|
||||
int var2 = 2;
|
||||
var2 = 2 + 3;
|
||||
int var3;
|
||||
if (!var2){
|
||||
var2 = 1;
|
||||
var3 = 4;
|
||||
} else {
|
||||
var1 = 2;
|
||||
}
|
||||
printf(var2);
|
||||
return 5;
|
||||
int c = 2;
|
||||
int * p;
|
||||
p = &c;
|
||||
*p = 2;
|
||||
int b = *p;
|
||||
|
||||
return 0;
|
||||
}
|
||||
" | ./a.out
|
59
table_fonctions.c
Normal file
59
table_fonctions.c
Normal 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
35
table_fonctions.h
Normal 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
|
|
@ -54,25 +54,47 @@ int add_symbole_bottom(Table_Symboles * table){
|
|||
return 0;
|
||||
}
|
||||
|
||||
int remove_symboles(Table_Symboles * table, int depth){
|
||||
int index;
|
||||
if (depth < table->depth){
|
||||
return -1;
|
||||
} else {
|
||||
index = table->indexAvailableTop;
|
||||
while(table->array[index].symbole_depth == depth && index >=0){
|
||||
//remove
|
||||
table->indexAvailableTop--;
|
||||
index--;
|
||||
int remove_symboles(Table_Symboles * table){
|
||||
if (table->indexAvailableTop > 0){
|
||||
while(table->indexAvailableTop > 0){
|
||||
if (table->array[table->indexAvailableTop-1].symbole_depth == table->depth){
|
||||
table->indexAvailableTop--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void free_temp(Table_Symboles * table){
|
||||
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){
|
||||
|
@ -91,8 +113,12 @@ void print_symbole(Symbole * symbole){
|
|||
char typeStr[20];
|
||||
if (type == TYPE_INT){
|
||||
strcpy(typeStr, "INT");
|
||||
} else{
|
||||
} else if (type == TYPE_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;
|
||||
char initStr[20];
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#define TABLE_SIZE 50
|
||||
#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};
|
||||
|
||||
typedef struct Symboles {
|
||||
|
@ -58,14 +58,18 @@ int variable_exists(Table_Symboles * table, char * varName);
|
|||
/**
|
||||
* Removes symbole from table having certain depth
|
||||
* @param table
|
||||
* @param depth
|
||||
* @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);
|
||||
|
||||
int prepare_function_call(Table_Symboles * table);
|
||||
|
||||
int prepare_argument_push(Table_Symboles * table);
|
||||
|
||||
|
||||
/**
|
||||
* Initialises an already exisiting symbole
|
||||
* @param table
|
||||
|
|
Loading…
Reference in a new issue