Modifications while a faire
This commit is contained in:
parent
f6d265e641
commit
17419e1663
17 changed files with 2473 additions and 1951 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
*.tab.c
|
||||
*.tab.h
|
||||
*.yy.c
|
||||
a.out
|
||||
poubelle/
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
13
.idea/misc.xml
Normal file
13
.idea/misc.xml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MakefileSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<MakefileProjectSettings>
|
||||
<option name="externalProjectPath" value="$PROJECT_DIR$" />
|
||||
<option name="version" value="2" />
|
||||
</MakefileProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/projet_systeme.iml" filepath="$PROJECT_DIR$/.idea/projet_systeme.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
2
.idea/projet_systeme.iml
Normal file
2
.idea/projet_systeme.iml
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="External" type="CPP_MODULE" version="4" />
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
BIN
a.out
BIN
a.out
Binary file not shown.
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,97 @@
|
|||
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,
|
||||
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
|
||||
};
|
||||
#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 tPV 269
|
||||
#define tVIRGULE 270
|
||||
#define tAFFECTATION 271
|
||||
#define tEGAL 272
|
||||
#define tDIFF 273
|
||||
#define tLT 274
|
||||
#define tGT 275
|
||||
#define tGTE 276
|
||||
#define tLTE 277
|
||||
#define tMAIN 278
|
||||
#define tINT 279
|
||||
#define tPRINT 280
|
||||
#define tRETURN 281
|
||||
#define tOR 282
|
||||
#define tAND 283
|
||||
#define tIF 284
|
||||
#define tELSE 285
|
||||
#define tWHILE 286
|
||||
#define tCONST 287
|
||||
#define tVAR 288
|
||||
#define tNOT 289
|
||||
|
||||
|
||||
|
||||
|
||||
/* 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 122 "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 */
|
||||
|
|
|
@ -12,6 +12,7 @@ enum Initialised_Variable init;
|
|||
enum Symbole_Type type;
|
||||
Table_Symboles table;
|
||||
instructions_array array;
|
||||
int whileCondition;
|
||||
%}
|
||||
|
||||
|
||||
|
@ -23,12 +24,13 @@ instructions_array array;
|
|||
%type<nombre> Cond
|
||||
|
||||
|
||||
|
||||
%token tADD
|
||||
%token tSUB
|
||||
%token tMUL
|
||||
%token tDIV
|
||||
|
||||
%token tPO
|
||||
%token<nombre> tPO
|
||||
%token tPF
|
||||
%token tAO
|
||||
%token tAF
|
||||
|
@ -52,7 +54,7 @@ instructions_array array;
|
|||
%token tAND
|
||||
%token<nombre> tIF
|
||||
%token tELSE
|
||||
%token tWHILE
|
||||
%token<nombre> tWHILE
|
||||
%token tCONST
|
||||
%token<id> tVAR
|
||||
%token tNOT
|
||||
|
@ -110,12 +112,15 @@ Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);}
|
|||
|
||||
Aff : tVAR tAFFECTATION E tPV {printf("Affectation : %s\n", $1); free_temp(&table);};
|
||||
|
||||
E : tENTIER {int vt = gen_entier(&table, &array, $1); $$ = vt;};
|
||||
E : tVAR {int vt = gen_var(&table, &array, $1); $$ = vt;};
|
||||
E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
|
||||
E : E tMUL E {gen_arithmetique(&array, MUL, $1, $3); free_temp(&table); $$ = $1;} ;
|
||||
E : E tSUB E {gen_arithmetique(&array, SOU, $1, $3); free_temp(&table); $$ = $1;} ;
|
||||
E : E tDIV E {gen_arithmetique(&array, DIV, $1, $3); free_temp(&table); $$ = $1;} ;
|
||||
//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, CPY, vt, varAddr); $$ = 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 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 : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
|
||||
|
@ -128,38 +133,59 @@ SuiteArgs : tVIRGULE tVAR SuiteArgs ;
|
|||
SuiteArgs : ;
|
||||
|
||||
If : tIF tPO Cond tPF {
|
||||
gen_jmpf(&table, &array, $3);
|
||||
//gen_jmpf(&table, &array, $3, -1);
|
||||
generate_instruction_2(&array, JMF, $3, -1);
|
||||
free_temp(&table);
|
||||
$1 = array.index;
|
||||
}
|
||||
tAO Instructions tAF
|
||||
tAO Instructions tAF
|
||||
{
|
||||
int adr_jmp = array.index;
|
||||
update_jump(&array, $1, adr_jmp);
|
||||
update_jmf(&array, $1, adr_jmp);
|
||||
}
|
||||
Else {printf("Dans if\n");};
|
||||
Else {};
|
||||
|
||||
Else : tELSE tAO Instructions tAF {printf("else\n");} ;
|
||||
Else : ;
|
||||
Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ;
|
||||
|
||||
While : tWHILE tPO Cond tPF tAO Instructions tAF {printf("Dans while\n");};
|
||||
/*While : tWHILE tPO {
|
||||
$2 = array.index + 1 ;
|
||||
} Cond tPF {*/
|
||||
While : tWHILE 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 {
|
||||
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;} ;
|
||||
Cond : E tGT E {generate_instruction_3(&array, GT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
|
||||
Cond : E tLTE E {generate_instruction_3(&array, LTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
|
||||
Cond : E tGTE E {generate_instruction_3(&array, GTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
|
||||
Cond : E tAND E {generate_instruction_3(&array, AND, $1, $1, $3); free_temp(&table); $$ = $3;} ;
|
||||
Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table); $$ = $3;} ;
|
||||
Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
|
||||
Cond : E {$$ = $1; };
|
||||
|
||||
|
||||
Cond : E tEGAL E {printf("Cond ==\n");} ;
|
||||
Cond : E tDIFF E {printf("Cond !=\n");} ;
|
||||
Cond : E tLT E {printf("Cond <\n");} ;
|
||||
Cond : E tGT E {printf("Cond >\n");} ;
|
||||
Cond : E tLTE E {printf("Cond <=\n");} ;
|
||||
Cond : E tGTE E{printf("Cond >=\n");} ;
|
||||
Cond : E tAND Cond {printf("Cond &&\n");} ;
|
||||
Cond : E tOR Cond {printf("Cond ||\n");} ;
|
||||
Cond : tNOT Cond {printf("Cond !\n");} ;
|
||||
Cond : E;
|
||||
|
||||
Invocation : tVAR tPO Args tPF {printf("Dans invocation\n");};
|
||||
|
||||
Print : tPRINT tPO tVAR tPF tPV {printf("printf de %s\n", $3);};
|
||||
//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 {$$ = gen_return(&table, &array, $2);};
|
||||
Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
|
||||
|
||||
%%
|
||||
#include <stdio.h>
|
||||
|
@ -171,5 +197,6 @@ void main(void){
|
|||
print_table(&table);
|
||||
//remove_symboles(&table, 0);
|
||||
//print_table(&table);
|
||||
exportInstructions(&array);
|
||||
}
|
||||
|
||||
|
|
281
gen_assembleur.c
281
gen_assembleur.c
|
@ -1,54 +1,210 @@
|
|||
#include "gen_assembleur.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char * operationName(enum operation op){
|
||||
switch(op){
|
||||
case EQ:
|
||||
return "EQ";
|
||||
case NEQ:
|
||||
return "NEQ";
|
||||
case LT:
|
||||
return "LT";
|
||||
case GT:
|
||||
return "GT";
|
||||
case LTE:
|
||||
return "LTE";
|
||||
case GTE:
|
||||
return "GTE";
|
||||
case ADD:
|
||||
return "ADD";
|
||||
case SOU:
|
||||
return "SOU";
|
||||
case DIV:
|
||||
return "DIV";
|
||||
case MUL:
|
||||
return "MUL";
|
||||
case CPY:
|
||||
return "CPY";
|
||||
case AFC:
|
||||
return "AFC";
|
||||
case RET:
|
||||
return "RET";
|
||||
case JMF:
|
||||
return "JPF";
|
||||
case JMP:
|
||||
return "JPM";
|
||||
case AND:
|
||||
return "AND";
|
||||
case OR:
|
||||
return "OR";
|
||||
case NOT:
|
||||
return "NOT";
|
||||
case PRI:
|
||||
return "PRI";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
void initialise_asm(instructions_array * array){
|
||||
array->index = 0;
|
||||
}
|
||||
|
||||
int add_instruction(instructions_array * array, instruction * instru){
|
||||
if (array->index >= INSTRUCTION_TABLE_SIZE){
|
||||
return 1;
|
||||
}
|
||||
array->array[array->index] = *instru;
|
||||
array->index++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int new_temp(Table_Symboles * table){
|
||||
int ret_addr ;
|
||||
if(add_symbole_bottom(table) == -1) {
|
||||
return -1;
|
||||
}
|
||||
ret_addr = table->indexAvailableBottom +1;
|
||||
ret_addr = table->indexAvailableBottom + 1;
|
||||
return ret_addr;
|
||||
}
|
||||
|
||||
void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2){
|
||||
int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
|
||||
instruction instru;
|
||||
switch(op){
|
||||
case ADD:
|
||||
instru.operation = ADD;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
instru.reg3 = arg2;
|
||||
printf("%d ADD %d %d %d\n", array->index, arg1, arg1, arg2);
|
||||
break;
|
||||
case DIV:
|
||||
instru.operation = DIV;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
instru.reg3 = arg2;
|
||||
printf("%d DIV %d %d %d\n", array->index, arg1, arg1, arg2);
|
||||
break;
|
||||
case SOU:
|
||||
instru.operation = SOU;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
instru.reg3 = arg2;
|
||||
printf("%d SOU %d %d %d\n", array->index, arg1, arg1, arg2);
|
||||
break;
|
||||
case MUL:
|
||||
instru.operation = MUL;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
instru.reg3 = arg2;
|
||||
printf("%d MUL %d %d %d\n", array->index, arg1, arg1, arg2);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
char * opName = operationName(op);
|
||||
|
||||
instru.operation = op;
|
||||
instru.reg1 = arg1;
|
||||
|
||||
printf("%d\t %s %d\n", array->index, opName, instru.reg1);
|
||||
|
||||
if (add_instruction(array, &instru) != 0){
|
||||
//TODO: Error handling
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2){
|
||||
instruction instru;
|
||||
char * opName = operationName(op);
|
||||
|
||||
instru.operation = op;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg2;
|
||||
|
||||
printf("%d\t %s %d %d\n", array->index, opName, instru.reg1, instru.reg2);
|
||||
|
||||
if (add_instruction(array, &instru) != 0){
|
||||
//TODO: Error handling
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3){
|
||||
instruction instru;
|
||||
char * opName = operationName(op);
|
||||
|
||||
instru.operation = op;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg2;
|
||||
instru.reg3 = arg3;
|
||||
|
||||
printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
|
||||
|
||||
if (add_instruction(array, &instru) != 0){
|
||||
//TODO: Error handling
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void update_jmf(instructions_array * array, int instru_index, int adr_jmp){
|
||||
array->array[instru_index - 1].reg2 = adr_jmp;
|
||||
printf("%d\t JMP %d %d\n", (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
|
||||
}
|
||||
|
||||
void exportInstructions(instructions_array * array){
|
||||
FILE *file;
|
||||
file = fopen("instructions.txt", "w");
|
||||
instruction instru;
|
||||
enum operation op;
|
||||
|
||||
for (int i = 0; i < array->index; i++){
|
||||
instru = array->array[i];
|
||||
op = instru.operation;
|
||||
switch (op) {
|
||||
//1 parameter
|
||||
case JMP:
|
||||
case PRI:
|
||||
case RET:
|
||||
fprintf(file, "%d\t %s %d\n", i, operationName(op), instru.reg1);
|
||||
break;
|
||||
//2 parameters
|
||||
case JMF:
|
||||
case NOT:
|
||||
case AFC:
|
||||
case CPY:
|
||||
fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
|
||||
break;
|
||||
//3 parameters
|
||||
case ADD:
|
||||
case SOU:
|
||||
case DIV:
|
||||
case MUL:
|
||||
case AND:
|
||||
case OR:
|
||||
case EQ:
|
||||
case NEQ:
|
||||
case LT:
|
||||
case LTE:
|
||||
case GT:
|
||||
case GTE:
|
||||
fprintf(file, "%d\t %s %d %d %d\n", i, operationName(op), instru.reg1, instru.reg2, instru.reg3);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
/*int gen_print(Table_Symboles * table, instructions_array * array, int arg1){
|
||||
instruction instru;
|
||||
instru.operation = PRI;
|
||||
instru.reg1 = arg1;
|
||||
|
||||
printf("%d\t PRI %d\n", array->index, instru.reg1);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
array->index++;
|
||||
}
|
||||
|
||||
|
||||
free_temp(table);
|
||||
}*/
|
||||
|
||||
/*void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2){
|
||||
instruction instru;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
instru.reg3 = arg2;
|
||||
|
||||
char * opName = operationName(op);
|
||||
printf("%d\t %s %d %d %d\n", array->index, opName, arg1, arg1, arg2);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
array->index++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int gen_var(Table_Symboles * table, instructions_array * array, char * varName){
|
||||
|
@ -59,9 +215,9 @@ int gen_var(Table_Symboles * table, instructions_array * array, char * varName){
|
|||
instruction instru;
|
||||
instru.operation = CPY;
|
||||
instru.reg1 = vt;
|
||||
instru.reg2 = varAddr;
|
||||
instru.reg2 = varAddr;
|
||||
|
||||
printf("%d CPY %d %d\n", array->index, vt, varAddr);
|
||||
printf("%d\t CPY %d %d\n", array->index, vt, varAddr);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
|
@ -79,9 +235,9 @@ int gen_entier(Table_Symboles * table, instructions_array * array, int entier){
|
|||
instruction instru;
|
||||
instru.operation = AFC;
|
||||
instru.reg1 = vt;
|
||||
instru.reg2 = entier;
|
||||
instru.reg2 = entier;
|
||||
|
||||
printf("%d AFC %d %d\n", array->index, vt, entier);
|
||||
printf("%d\t AFC %d %d\n", array->index, vt, entier);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
|
@ -91,6 +247,30 @@ int gen_entier(Table_Symboles * table, instructions_array * array, int entier){
|
|||
return vt;
|
||||
}
|
||||
|
||||
int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2){
|
||||
|
||||
char * opName = operationName(op);
|
||||
|
||||
instruction instru;
|
||||
instru.operation = op;
|
||||
instru.reg1 = arg1;
|
||||
instru.reg2 = arg1;
|
||||
if (op != NOT){
|
||||
instru.reg3 = arg2;
|
||||
printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
|
||||
free_temp(table);
|
||||
} else {
|
||||
printf("%d\t %s %d %d \n", array->index, opName, instru.reg1, instru.reg2);
|
||||
}
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
array->index++;
|
||||
}
|
||||
|
||||
return instru.reg1;
|
||||
}
|
||||
|
||||
int gen_return(Table_Symboles * table, instructions_array * array, int adr){
|
||||
|
||||
//vérifier que non null
|
||||
|
@ -98,42 +278,37 @@ int gen_return(Table_Symboles * table, instructions_array * array, int adr){
|
|||
instru.operation = RET;
|
||||
instru.reg1 = adr;
|
||||
|
||||
printf("%d RET %d\n", array->index, adr);
|
||||
printf("%d\t RET %d\n", array->index, adr);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
array->index++;
|
||||
}
|
||||
|
||||
free_temp(table);
|
||||
//free_temp(table);
|
||||
|
||||
return adr;
|
||||
}
|
||||
|
||||
int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond){
|
||||
int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest){
|
||||
//vérifier que non null
|
||||
instruction instru;
|
||||
instru.operation = JMPF;
|
||||
instru.operation = JMF;
|
||||
instru.reg1 = cond;
|
||||
instru.reg2 = -1;
|
||||
instru.reg2 = dest;
|
||||
|
||||
printf("%d JMPF %d %d\n", array->index, cond, -1);
|
||||
printf("%d\t JMPF %d %d\n", array->index, instru.reg1 , instru.reg2);
|
||||
|
||||
if (array->index < INSTRUCTION_TABLE_SIZE){
|
||||
array->array[array->index] = instru;
|
||||
array->index++;
|
||||
}
|
||||
|
||||
free_temp(table);
|
||||
//free_temp(table);
|
||||
|
||||
return cond;
|
||||
}
|
||||
|
||||
void update_jump(instructions_array * array, int if_value, int adr_jmp){
|
||||
array->array[if_value].reg2 = adr_jmp;
|
||||
printf("%d JMPF %d %d\n", array->index, array->array[if_value].reg1, array->array[if_value].reg2);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
void initialise_asm(instructions_array * array){
|
||||
array->index = 0;
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "table_symboles.h"
|
||||
|
||||
enum operation{ADD, SOU, MUL, DIV, CPY, AFC, RET, JMPF};
|
||||
enum operation{ADD, SOU, MUL, DIV, CPY, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE, GTE, AND, OR, NOT, PRI};
|
||||
|
||||
typedef struct instruction{
|
||||
enum operation operation;
|
||||
|
@ -20,8 +20,80 @@ typedef struct instructions_array{
|
|||
int index;
|
||||
} instructions_array;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param op operation
|
||||
* @return returns the string that corresponds to the enum operation op
|
||||
*/
|
||||
char * operationName(enum operation op);
|
||||
|
||||
/**
|
||||
* Initialises the instructions array
|
||||
* @param array
|
||||
*/
|
||||
void initialise_asm(instructions_array * array);
|
||||
|
||||
//renvoie l'index (ou valeur?) de la premiere @ dispo
|
||||
/**
|
||||
* Fetch address of a temporary variable
|
||||
* @param table
|
||||
* @return first available temp address
|
||||
*/
|
||||
int new_temp(Table_Symboles * table);
|
||||
|
||||
/**
|
||||
* Adds intruction to instruction array
|
||||
* @param array
|
||||
* @param intru
|
||||
* @return 0 if instruction was added successfully, -1 if not
|
||||
*/
|
||||
int add_instruction(instructions_array * array, instruction * intru);
|
||||
|
||||
/**
|
||||
* Generates intruction with one parameter
|
||||
* @param array
|
||||
* @param op
|
||||
* @param arg1
|
||||
* @return
|
||||
*/
|
||||
int generate_instruction_1(instructions_array * array, enum operation op, int arg1);
|
||||
|
||||
/**
|
||||
* Generates intruction with two parameters
|
||||
* @param array
|
||||
* @param op
|
||||
* @param arg1
|
||||
* @param arg2
|
||||
* @return
|
||||
*/
|
||||
int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2);
|
||||
|
||||
/**
|
||||
* Generates intruction with three parameters
|
||||
* @param array
|
||||
* @param op
|
||||
* @param arg1
|
||||
* @param arg2
|
||||
* @param arg3
|
||||
* @return
|
||||
*/
|
||||
int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3);
|
||||
|
||||
/**
|
||||
* Updates the JMF instruction with the correct jump destination address
|
||||
* @param array
|
||||
* @param instru_index
|
||||
* @param adr_jmp
|
||||
*/
|
||||
void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void exportInstructions(instructions_array * array);
|
||||
|
||||
/*
|
||||
void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2);
|
||||
|
||||
int gen_var(Table_Symboles * table, instructions_array * array, char * varName);
|
||||
|
@ -30,11 +102,10 @@ int gen_entier(Table_Symboles * table, instructions_array * array, int entier);
|
|||
|
||||
int gen_return(Table_Symboles * table, instructions_array * array, int adr);
|
||||
|
||||
int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond);
|
||||
int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest);
|
||||
|
||||
void update_jump(instructions_array * array, int if_value, int adr_jmp);
|
||||
|
||||
//renvoie l'index (ou valeur?) de la premiere @ dispo
|
||||
int new_temp(Table_Symboles * table);
|
||||
int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2);
|
||||
|
||||
int gen_print(Table_Symboles * table, instructions_array * array, int arg1);
|
||||
*/
|
||||
#endif
|
15
instructions.txt
Normal file
15
instructions.txt
Normal file
|
@ -0,0 +1,15 @@
|
|||
0 AFC 49 1
|
||||
1 AFC 49 2
|
||||
2 AFC 48 3
|
||||
3 ADD 49 49 48
|
||||
4 AFC 49 4
|
||||
5 CPY 49 1
|
||||
6 NOT 49 49
|
||||
7 JPF 49 10
|
||||
8 AFC 49 1
|
||||
9 AFC 49 4
|
||||
10 JPM 4
|
||||
11 CPY 49 1
|
||||
12 PRI 49
|
||||
13 AFC 49 5
|
||||
14 RET 49
|
|
@ -6,9 +6,11 @@ echo "
|
|||
const int var1 = 1, var2;
|
||||
int var2 = 2 + 3;
|
||||
int var3, var4 = 4;
|
||||
if( 4){
|
||||
var2 = 6;
|
||||
while (!var2){
|
||||
var2 = 1;
|
||||
var3 = 4;
|
||||
}
|
||||
printf(var2);
|
||||
return 5;
|
||||
}
|
||||
" | ./a.out
|
Loading…
Reference in a new issue