Browse Source

cross assembleur semble etre ok

Nahom 2 years ago
parent
commit
38f818afa1

Makefile → compiler/Makefile View File

@@ -6,7 +6,7 @@ compiler: analyse_lexicale.lex analyse_syntaxique.y table_symboles.c table_fonct
6 6
 		gcc -w *.c -ly -o compiler
7 7
 
8 8
 run: compiler
9
-		./compiler < test_file
9
+		./compiler < code_c
10 10
 
11 11
 clean:
12 12
 		rm -f lex.yy.c compiler analyse_syntaxique.output analyse_syntaxique.tab.c analyse_syntaxique.tab.h

+ 106
- 0
compiler/analyse_lexicale.lex View File

@@ -0,0 +1,106 @@
1
+%{ 
2
+#include "analyse_syntaxique.tab.h" 
3
+int yywrap(void){
4
+    return 1;
5
+}
6
+
7
+%}
8
+
9
+
10
+
11
+
12
+ADD         "+"
13
+SUB         "-"
14
+MUL         "*"
15
+DIV         "/"
16
+tPO         "("
17
+tPF         ")"
18
+tAO         "{"
19
+tAF         "}"
20
+EOL         "\n"
21
+EOI         ";"
22
+SPACE       " "
23
+TAB         "\t"
24
+VIRGULE     ","
25
+AFFECTATION        "="
26
+EQUAL       "=="
27
+LT          "<"
28
+GT          ">"
29
+LTE          "<="
30
+GTE          ">="
31
+tINT        "int"
32
+tMAIN       "main"  
33
+tPRINT      "printf"
34
+tRETURN     "return"
35
+tIF         "if"
36
+tELSE       "else"
37
+tWHILE      "while"
38
+tNOT        "!"
39
+tAND        "&&"
40
+tOR         "||"
41
+tDIFF       "!="
42
+tAPPERSAND  "&"
43
+DIGIT       [0-9]
44
+VARIABLE    [A-Za-z0-9_]+
45
+CONST       "const"
46
+DECIMAL     {DIGIT}+
47
+EXPONENTIEL {DIGIT}+"e"{DIGIT}+
48
+ENTIER      {DECIMAL}
49
+ENTIEREXP   {EXPONENTIEL}
50
+OPERATION   {ADD}|{SUB}|{MUL}|{DIV}
51
+COMPARATEUR {EGAL}|{LT}|{GT}
52
+SEPARATOR   {SPACE}|{TAB}
53
+
54
+%%
55
+
56
+{ADD}           {return tADD ;}
57
+{SUB}           {return tSUB ;}
58
+{MUL}           {return tMUL ;}
59
+{DIV}           {return tDIV ;}
60
+
61
+{tPO}           {return tPO ;}
62
+{tPF}           {return tPF ;}
63
+{tAO}           {return tAO ;}
64
+{tAF}           {return tAF ;}
65
+
66
+{EOI}           {return tPV ;}
67
+{SEPARATOR}     {}
68
+{EOL}           {}
69
+{VIRGULE}       {return tVIRGULE ;}
70
+
71
+{AFFECTATION}   {return tAFFECTATION ;}
72
+
73
+{EQUAL}          {return tEGAL ;}
74
+{tDIFF}          {return tDIFF ;}
75
+{LT}            {return tLT ;}
76
+{GT}            {return tGT ;}
77
+{LTE}           {return tLTE ;}
78
+{GTE}           {return tGTE ;}
79
+{tNOT}           {return tNOT ;}
80
+
81
+
82
+{tMAIN}         {return tMAIN ;}
83
+{tINT}          {return tINT ;}
84
+{tPRINT}        {return tPRINT ;}
85
+{tRETURN}       {return tRETURN ;}
86
+
87
+{tOR}           {return tOR ;}
88
+{tAND}          {return tAND ;}
89
+
90
+{tIF}           {return tIF ;}
91
+{tELSE}         {return tELSE ;}
92
+{tWHILE}        {return tWHILE ;}
93
+
94
+{tAPPERSAND}    {return tAPPERSAND;}
95
+{CONST}         {return tCONST ;}
96
+{ENTIER}        {yylval.nombre = atoi(yytext); return tENTIER ;}
97
+{ENTIEREXP}     {yylval.nombre = -1; return tENTIEREXP;}
98
+{VARIABLE}      {strcpy(yylval.id, yytext); return tVAR  ;}
99
+
100
+%%
101
+
102
+//int main(void){
103
+//    yylex();
104
+//}
105
+
106
+

+ 230
- 0
compiler/analyse_syntaxique.y View File

@@ -0,0 +1,230 @@
1
+%union {
2
+int nombre;
3
+char id[30];
4
+}
5
+
6
+%{
7
+#include <stdio.h>
8
+#include "table_symboles.h"
9
+#include "table_fonctions.h"
10
+#include "gen_assembleur.h"
11
+
12
+enum Initialised_Variable init;
13
+enum Symbole_Type type;
14
+enum Return_Type return_type;
15
+Table_Symboles table;
16
+Table_Fonctions table_fonctions;
17
+instructions_array array;
18
+int whileCondition;
19
+int return_value;
20
+%}
21
+
22
+
23
+%token<nombre> tENTIER
24
+%token<nombre> tENTIEREXP
25
+
26
+%type<nombre> E
27
+%type<nombre> Return Instructions
28
+%type<nombre> Cond
29
+%type<nombre> While Else Invocation
30
+
31
+
32
+%token tADD
33
+%token tSUB
34
+%token tMUL
35
+%token tDIV
36
+
37
+%token<nombre> tPO
38
+%token tPF
39
+%token tAO
40
+%token tAF
41
+
42
+%token tERROR
43
+
44
+%token tAPPERSAND
45
+%token tPV
46
+%token tVIRGULE
47
+%token tAFFECTATION
48
+%token tEGAL
49
+%token tDIFF
50
+%token tLT
51
+%token tGT
52
+%token tGTE
53
+%token tLTE
54
+%token tMAIN
55
+%token tINT
56
+%token tPRINT
57
+%token tRETURN
58
+%token tOR
59
+%token tAND
60
+%token<nombre> tIF
61
+%token tELSE
62
+%token<nombre> tWHILE
63
+%token tCONST
64
+%token<id> tVAR
65
+%token tNOT
66
+
67
+%left tADD
68
+%left tSUB
69
+%left tMUL
70
+%left tDIV
71
+%right tEGAL
72
+
73
+
74
+%%
75
+
76
+/*C : Fonctions Main ;
77
+
78
+Fonctions : ;
79
+Fonctions : Fonction Fonctions ;
80
+
81
+Fonction : tINT tVAR tPO Params tPF Body;*/
82
+
83
+C : {generate_instruction_1(&array, JMP, -1);} Fonctions;
84
+
85
+Fonctions: Main;
86
+Fonctions: Fonction Fonctions;
87
+
88
+
89
+Main : tINT tMAIN {update_jmp(&array, 0, array.index); add_function(&table_fonctions, "Main", RET_INT, array.index); table.depth++;} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
90
+Fonction : Function_type tVAR {{add_function(&table_fonctions, $2, return_type, array.index); table.depth++;}} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
91
+
92
+Function_type: tINT {type = TYPE_INT;} ;
93
+Function_type: tINT tMUL {type = TYPE_INT_PTR;};
94
+
95
+Params : {} ;
96
+Params : Param SuiteParams ;
97
+
98
+Param : Param_type tVAR {add_symbole_top(&table, $2, type, INITIALISED, table.depth);} ;
99
+
100
+Param_type: tINT {type = TYPE_INT;} ;
101
+Param_type: tINT tMUL {type = TYPE_INT_PTR;};
102
+
103
+SuiteParams : tVIRGULE Param SuiteParams ;
104
+SuiteParams : ;
105
+
106
+
107
+Body : tAO Instructions Return tAF {} ;
108
+
109
+Instructions : Instruction Instructions {$$ = array.index;};
110
+Instructions : {$$ = array.index;};
111
+
112
+Instruction : Aff ;
113
+Instruction : If ;
114
+Instruction : While ;
115
+Instruction : Print ;
116
+Instruction : Decl ;
117
+Instruction : Invocation tPV ;
118
+
119
+Decl : Type Valeur SuiteDecl tPV ;
120
+
121
+SuiteDecl: tVIRGULE Valeur SuiteDecl ;
122
+SuiteDecl: ;
123
+
124
+Type : tINT {type = TYPE_INT;} ;
125
+Type : tCONST tINT {type = TYPE_CONST_INT;} ;
126
+Type : tINT tMUL {type = TYPE_INT_PTR;};
127
+
128
+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);};
129
+Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
130
+
131
+
132
+Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $3); free_temp(&table); };
133
+Aff : tMUL tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP_STR, varAddr, $4); free_temp(&table); };
134
+
135
+
136
+E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
137
+E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
138
+E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
139
+E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;}  ;
140
+E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
141
+E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
142
+E : tSUB E {printf("Variable negative\n");} ;
143
+E : Invocation {
144
+	//int vt = new_temp(&table);
145
+	//generate_instruction_2(&array, COP, vt, $1);
146
+	remove_symboles(&table);
147
+	table.depth--;
148
+	$$ = $1;};
149
+E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
150
+E : tAPPERSAND tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, LEA, vt, varAddr); $$ = vt;};
151
+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;};
152
+
153
+
154
+If : tIF tPO Cond tPF {
155
+    //gen_jmpf(&table, &array, $3, -1);
156
+    generate_instruction_2(&array, JMF, $3, -1);
157
+    free_temp(&table);
158
+    $1 = array.index;
159
+}
160
+tAO {table.depth++;} Instructions {generate_instruction_1(&array, JMP, -1);} tAF {remove_symboles(&table); table.depth--;}
161
+{
162
+    int adr_jmp = array.index;
163
+    update_jmf(&array, $1, adr_jmp);
164
+}
165
+Else {printf("updating jump\n"); update_jmp(&array, $8, $13);};
166
+
167
+Else : tELSE tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {$$ = array.index;} ;
168
+Else : {$$ = array.index;};
169
+Else : tELSE If {$$ = array.index;} ;
170
+
171
+While : tWHILE tPO {
172
+	$2 = array.index ;
173
+} Cond tPF {
174
+	//gen_jmpf(&table, &array, $4, -1);
175
+	generate_instruction_2(&array, JMF, $4, -1);
176
+	free_temp(&table);
177
+	$1 = array.index;
178
+}
179
+tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
180
+	int adr_jmp = array.index;
181
+	update_jmf(&array, $1, adr_jmp);
182
+	//gen_jmpf(&table, &array, $1, $2);
183
+	generate_instruction_1(&array, JMP, $2);
184
+};
185
+
186
+Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
187
+Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
188
+Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
189
+Cond : E tGT E {generate_instruction_3(&array, GT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
190
+Cond : E tLTE E {generate_instruction_3(&array, LTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
191
+Cond : E tGTE E {generate_instruction_3(&array, GTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
192
+Cond : E tAND E {generate_instruction_3(&array, AND, $1, $1, $3); free_temp(&table); $$ = $3;} ;
193
+Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table); $$ = $3;} ;
194
+Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
195
+Cond : E {$$ = $1; };
196
+
197
+Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableBottom);} Args  tPF
198
+	{int function_index = function_exists(&table_fonctions, $1);
199
+	int jmp_addr = (table_fonctions.array[function_index]).start_addr;
200
+	generate_instruction_2(&array, CALL, jmp_addr, table.indexAvailableTop);
201
+	$$ = return_value;
202
+	};
203
+
204
+Args : Arg SuiteArgs ;
205
+Args :
206
+Arg : E {int arg_addr = prepare_argument_push(&table); generate_instruction_2(&array, COP, arg_addr, $1); free_temp(&table);};
207
+SuiteArgs : tVIRGULE Arg SuiteArgs ;
208
+SuiteArgs : ;
209
+
210
+Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
211
+
212
+Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
213
+
214
+%%
215
+#include <stdio.h>
216
+void main(void){
217
+    //TODO: rajouter gestion des erreurs
218
+    initialise_table(&table);
219
+    initialise_function_table(&table_fonctions);
220
+    initialise_asm(&array);
221
+    yyparse();
222
+    print_table(&table);
223
+    printf("\n");
224
+    print_fonction_table(&table_fonctions);
225
+
226
+    //remove_symboles(&table, 0);
227
+    //print_table(&table);
228
+    exportInstructions(&array);
229
+}
230
+

+ 16
- 0
compiler/code_c View File

@@ -0,0 +1,16 @@
1
+int fonction1(int * a){
2
+      int b = *a;
3
+      printf(b);
4
+      return 1;
5
+    }
6
+
7
+int main(){
8
+  int l = 21;
9
+  int * p = &l;
10
+  int c = fonction1(p);
11
+  printf(c);
12
+  p = &c;
13
+  *p = 2;
14
+  printf(c);
15
+
16
+  return 0;

+ 357
- 0
compiler/gen_assembleur.c View File

@@ -0,0 +1,357 @@
1
+#include "gen_assembleur.h"
2
+#include <stdio.h>
3
+#include <stdlib.h>
4
+
5
+char * operationName(enum operation op){
6
+    switch(op){
7
+        case EQ:
8
+            return "EQ";
9
+        case NEQ:
10
+            return "NEQ";
11
+        case LT:
12
+            return "LT";
13
+        case GT:
14
+            return "GT";
15
+        case LTE:
16
+            return "LTE";
17
+        case GTE:
18
+            return "GTE";
19
+        case ADD:
20
+            return "ADD";
21
+        case SOU:
22
+            return "SOU";
23
+        case DIV:
24
+            return "DIV";
25
+        case MUL:
26
+            return "MUL";
27
+        case COP:
28
+            return "COP";
29
+        case AFC:
30
+            return "AFC";
31
+        case RET:
32
+            return "RET";
33
+        case JMF:
34
+            return "JPF";
35
+        case JMP:
36
+            return "JMP";
37
+        case AND:
38
+            return "AND";
39
+        case OR:
40
+            return "OR";
41
+        case NOT:
42
+            return "NOT";
43
+        case PRI:
44
+            return "PRI";
45
+        case LEA:
46
+            return "LEA";
47
+        case COP_LD:
48
+            return "COP_LD";
49
+        case COP_STR:
50
+            return "COP_STR";
51
+        case RET_FUN:
52
+            return "RET_FUN";
53
+        case CALL:
54
+            return "CALL";
55
+        default:
56
+            break;
57
+    }
58
+    return "";
59
+}
60
+
61
+void initialise_asm(instructions_array * array){
62
+    array->index = 0;
63
+}
64
+
65
+int add_instruction(instructions_array * array, instruction * instru){
66
+    if (array->index >= INSTRUCTION_TABLE_SIZE){
67
+        return 1;
68
+    }
69
+    array->array[array->index] = *instru;
70
+    array->index++;
71
+
72
+    return 0;
73
+}
74
+
75
+int new_temp(Table_Symboles * table){
76
+    int ret_addr ;
77
+    if(add_symbole_bottom(table) == -1) {
78
+        return -1;
79
+    }
80
+    ret_addr = table->indexAvailableBottom + 1;
81
+    return ret_addr;
82
+}
83
+
84
+int generate_instruction_0(instructions_array * array, enum operation op){
85
+    instruction instru;
86
+    char * opName = operationName(op);
87
+
88
+    instru.operation = op;
89
+
90
+    printf("%d\t %s\n", array->index, opName);
91
+
92
+    if (add_instruction(array, &instru) != 0){
93
+        //TODO: Error handling
94
+        exit(1);
95
+    }
96
+
97
+    return 0;
98
+}
99
+
100
+int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
101
+    instruction instru;
102
+    char * opName = operationName(op);
103
+
104
+    instru.operation = op;
105
+    instru.reg1 = arg1;
106
+
107
+    printf("%d\t %s %d\n", array->index, opName, instru.reg1);
108
+
109
+    if (add_instruction(array, &instru) != 0){
110
+        //TODO: Error handling
111
+        exit(1);
112
+    }
113
+
114
+    return 0;
115
+}
116
+
117
+int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2){
118
+    instruction instru;
119
+    char * opName = operationName(op);
120
+
121
+    instru.operation = op;
122
+    instru.reg1 = arg1;
123
+    instru.reg2 = arg2;
124
+
125
+    printf("%d\t %s %d %d\n", array->index, opName, instru.reg1, instru.reg2);
126
+
127
+    if (add_instruction(array, &instru) != 0){
128
+        //TODO: Error handling
129
+        exit(1);
130
+    }
131
+
132
+    return 0;
133
+}
134
+
135
+int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3){
136
+    instruction instru;
137
+    char * opName = operationName(op);
138
+
139
+    instru.operation = op;
140
+    instru.reg1 = arg1;
141
+    instru.reg2 = arg2;
142
+    instru.reg3 = arg3;
143
+
144
+    printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
145
+
146
+    if (add_instruction(array, &instru) != 0){
147
+        //TODO: Error handling
148
+        exit(1);
149
+    }
150
+
151
+    return 0;
152
+}
153
+
154
+void update_jmf(instructions_array * array, int instru_index, int adr_jmp){
155
+    array->array[instru_index - 1].reg2 = adr_jmp;
156
+    printf("%d\t JMP %d %d\n",  (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
157
+}
158
+
159
+void update_jmp(instructions_array * array, int instru_index, int adr_jmp){
160
+    array->array[instru_index].reg1 = adr_jmp;
161
+    printf("%d\t JMP %d\n",  (instru_index - 1), array->array[instru_index].reg1);
162
+}
163
+
164
+void exportInstructions(instructions_array * array){
165
+    FILE *file;
166
+    file = fopen("memory_oriented_assembly.txt", "w");
167
+    instruction instru;
168
+    enum operation op;
169
+
170
+    for (int i = 0; i < array->index; i++){
171
+        instru = array->array[i];
172
+        op = instru.operation;
173
+        switch (op) {
174
+            //0 parameters
175
+            case RET_FUN:
176
+                fprintf(file, "%s\n", operationName(op));
177
+                break;
178
+            //1 parameter
179
+            case JMP:
180
+            case PRI:
181
+            case RET:
182
+                fprintf(file, "%s %d\n", operationName(op), instru.reg1);
183
+                break;
184
+            //2 parameters
185
+            case JMF:
186
+            case NOT:
187
+            case AFC:
188
+            case COP:
189
+            case LEA:
190
+            case CALL:
191
+                fprintf(file, "%s %d %d\n", operationName(op), instru.reg1, instru.reg2);
192
+                break;
193
+            case COP_LD:
194
+                fprintf(file, "%s %d [%d]\n", operationName(op), instru.reg1, instru.reg2);
195
+                break;
196
+            case COP_STR:
197
+                fprintf(file, "%s [%d] %d\n", operationName(op), instru.reg1, instru.reg2);
198
+                break;
199
+            //3 parameters
200
+            case ADD:
201
+            case SOU:
202
+            case DIV:
203
+            case MUL:
204
+            case AND:
205
+            case OR:
206
+            case EQ:
207
+            case NEQ:
208
+            case LT:
209
+            case LTE:
210
+            case GT:
211
+            case GTE:
212
+                fprintf(file, "%s %d %d %d\n", operationName(op), instru.reg1, instru.reg2, instru.reg3);
213
+                break;
214
+            default:
215
+                break;
216
+        }
217
+    }
218
+
219
+    fclose(file);
220
+}
221
+
222
+/*int gen_print(Table_Symboles * table, instructions_array * array, int arg1){
223
+    instruction instru;
224
+    instru.operation = PRI;
225
+    instru.reg1 = arg1;
226
+
227
+    printf("%d\t PRI %d\n",  array->index, instru.reg1);
228
+
229
+    if (array->index < INSTRUCTION_TABLE_SIZE){
230
+        array->array[array->index] = instru;
231
+        array->index++;
232
+    }
233
+
234
+    free_temp(table);
235
+}*/
236
+
237
+/*void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2){
238
+    instruction instru;
239
+    instru.reg1 = arg1;
240
+    instru.reg2 = arg1;
241
+    instru.reg3 = arg2;
242
+
243
+    char * opName = operationName(op);
244
+    printf("%d\t %s %d %d %d\n", array->index, opName, arg1, arg1, arg2);
245
+
246
+    if (array->index < INSTRUCTION_TABLE_SIZE){
247
+        array->array[array->index] = instru;
248
+        array->index++;
249
+    }
250
+
251
+}
252
+
253
+int gen_var(Table_Symboles * table, instructions_array * array, char * varName){
254
+    int vt = new_temp(table);
255
+    int varAddr = variable_exists(table, varName);
256
+
257
+    //vérifier que non null
258
+    instruction instru;
259
+    instru.operation = COP;
260
+    instru.reg1 = vt;
261
+    instru.reg2 = varAddr;
262
+
263
+    printf("%d\t COP %d %d\n",  array->index, vt, varAddr);
264
+
265
+    if (array->index < INSTRUCTION_TABLE_SIZE){
266
+        array->array[array->index] = instru;
267
+        array->index++;
268
+    }
269
+
270
+    return vt;
271
+
272
+}
273
+
274
+int gen_entier(Table_Symboles * table, instructions_array * array, int entier){
275
+    int vt = new_temp(table);
276
+
277
+    //vérifier que non null
278
+    instruction instru;
279
+    instru.operation = AFC;
280
+    instru.reg1 = vt;
281
+    instru.reg2 = entier;
282
+
283
+    printf("%d\t AFC %d %d\n",  array->index, vt, entier);
284
+
285
+    if (array->index < INSTRUCTION_TABLE_SIZE){
286
+        array->array[array->index] = instru;
287
+        array->index++;
288
+    }
289
+
290
+    return vt;
291
+}
292
+
293
+int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op,  int arg1, int arg2){
294
+
295
+    char * opName = operationName(op);
296
+
297
+    instruction instru;
298
+    instru.operation = op;
299
+    instru.reg1 = arg1;
300
+    instru.reg2 = arg1;
301
+    if (op != NOT){
302
+        instru.reg3 = arg2;
303
+        printf("%d\t %s %d %d %d\n", array->index, opName, instru.reg1, instru.reg2, instru.reg3);
304
+        free_temp(table);
305
+    } else {
306
+        printf("%d\t %s %d %d \n", array->index, opName, instru.reg1, instru.reg2);
307
+    }
308
+
309
+    if (array->index < INSTRUCTION_TABLE_SIZE){
310
+        array->array[array->index] = instru;
311
+        array->index++;
312
+    }
313
+
314
+    return instru.reg1;
315
+}
316
+
317
+int gen_return(Table_Symboles * table, instructions_array * array, int adr){
318
+
319
+    //vérifier que non null
320
+    instruction instru;
321
+    instru.operation = RET;
322
+    instru.reg1 = adr;
323
+
324
+    printf("%d\t RET %d\n",  array->index, adr);
325
+
326
+    if (array->index < INSTRUCTION_TABLE_SIZE){
327
+        array->array[array->index] = instru;
328
+        array->index++;
329
+    }
330
+
331
+    //free_temp(table);
332
+
333
+    return adr;
334
+}
335
+
336
+int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest){
337
+       //vérifier que non null
338
+    instruction instru;
339
+    instru.operation = JMF;
340
+    instru.reg1 = cond;
341
+    instru.reg2 = dest;
342
+
343
+    printf("%d\t JMPF %d %d\n",  array->index, instru.reg1 , instru.reg2);
344
+
345
+    if (array->index < INSTRUCTION_TABLE_SIZE){
346
+        array->array[array->index] = instru;
347
+        array->index++;
348
+    }
349
+
350
+    //free_temp(table);
351
+
352
+    return cond;
353
+}
354
+
355
+ */
356
+
357
+

+ 121
- 0
compiler/gen_assembleur.h View File

@@ -0,0 +1,121 @@
1
+#ifndef GEN_ASSEMBLEUR_H
2
+#define GEN_ASSEMBLEUR_H
3
+
4
+#define INSTRUCTION_TABLE_SIZE 1000
5
+
6
+#include "table_symboles.h"
7
+
8
+enum operation{ADD, SOU, MUL, DIV, COP, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE,
9
+        GTE, AND, OR, NOT, PRI, LEA, COP_LD, COP_STR, CALL, RET_FUN};
10
+
11
+typedef struct instruction{
12
+    enum operation operation;
13
+    int reg1;
14
+    int reg2;
15
+    int reg3;
16
+}instruction;
17
+
18
+//table des instructions
19
+typedef struct instructions_array{
20
+    instruction array[INSTRUCTION_TABLE_SIZE];
21
+    int index;
22
+} instructions_array;
23
+
24
+/**
25
+ *
26
+ * @param op operation
27
+ * @return returns the string that corresponds to the enum operation op
28
+ */
29
+char * operationName(enum operation op);
30
+
31
+/**
32
+ * Initialises the instructions array
33
+ * @param array
34
+ */
35
+void initialise_asm(instructions_array * array);
36
+
37
+//renvoie l'index (ou valeur?) de la premiere @ dispo
38
+/**
39
+ * Fetch address of a temporary variable
40
+ * @param table
41
+ * @return first available temp address
42
+ */
43
+int new_temp(Table_Symboles * table);
44
+
45
+/**
46
+ * Adds intruction to instruction array
47
+ * @param array
48
+ * @param intru
49
+ * @return 0 if instruction was added successfully, -1 if not
50
+ */
51
+int add_instruction(instructions_array * array, instruction * intru);
52
+
53
+/**
54
+ * Generates intruction with no parameter
55
+ * @param array
56
+ * @param op
57
+ * @return
58
+ */
59
+int generate_instruction_0(instructions_array * array, enum operation op);
60
+
61
+/**
62
+ * Generates intruction with one parameter
63
+ * @param array
64
+ * @param op
65
+ * @param arg1
66
+ * @return
67
+ */
68
+int generate_instruction_1(instructions_array * array, enum operation op, int arg1);
69
+
70
+/**
71
+ * Generates intruction with two parameters
72
+ * @param array
73
+ * @param op
74
+ * @param arg1
75
+ * @param arg2
76
+ * @return
77
+ */
78
+int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2);
79
+
80
+/**
81
+ * Generates intruction with three parameters
82
+ * @param array
83
+ * @param op
84
+ * @param arg1
85
+ * @param arg2
86
+ * @param arg3
87
+ * @return
88
+ */
89
+int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3);
90
+
91
+/**
92
+ * Updates the JMF instruction with the correct jump destination address
93
+ * @param array
94
+ * @param instru_index
95
+ * @param adr_jmp
96
+ */
97
+void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
98
+
99
+void update_jmp(instructions_array * array, int instru_index, int adr_jmp);
100
+
101
+
102
+
103
+
104
+void exportInstructions(instructions_array * array);
105
+
106
+/*
107
+void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2);
108
+
109
+int gen_var(Table_Symboles * table, instructions_array * array, char * varName);
110
+
111
+int gen_entier(Table_Symboles * table, instructions_array * array, int entier);
112
+
113
+int gen_return(Table_Symboles * table, instructions_array * array, int adr);
114
+
115
+int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest);
116
+
117
+int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2);
118
+
119
+int gen_print(Table_Symboles * table, instructions_array * array, int arg1);
120
+ */
121
+#endif

+ 26
- 0
compiler/memory_oriented_assembly.txt View File

@@ -0,0 +1,26 @@
1
+JMP 8
2
+COP 255 0
3
+COP_LD 255 [255]
4
+COP 1 255
5
+COP 255 1
6
+PRI 255
7
+AFC 255 1
8
+RET 255
9
+AFC 255 21
10
+COP 0 255
11
+LEA 255 0
12
+COP 1 255
13
+COP 255 1
14
+COP 5 255
15
+CALL 1 6
16
+COP 2 255
17
+COP 255 2
18
+PRI 255
19
+LEA 255 2
20
+COP 1 255
21
+AFC 255 2
22
+COP_STR [1] 255
23
+COP 255 2
24
+PRI 255
25
+AFC 255 0
26
+RET 255

+ 59
- 0
compiler/table_fonctions.c View File

@@ -0,0 +1,59 @@
1
+#include "table_fonctions.h"
2
+#include <string.h>
3
+#include <stdio.h>
4
+
5
+void initialise_function_table(Table_Fonctions * table){
6
+    table->depth = 1;
7
+}
8
+
9
+void add_function(Table_Fonctions * table, char * function_name, enum Return_Type return_type, int start_addr){
10
+    Fonction fonction;
11
+    strcpy(fonction.function_name,function_name);
12
+    fonction.start_addr = start_addr;
13
+    fonction.type = return_type;
14
+    fonction.function_depth = table->depth;
15
+    table->array[table->depth] = fonction;
16
+    table->depth++;
17
+}
18
+
19
+void print_function(Fonction * fonction){
20
+    char * function_name = fonction->function_name;
21
+    int start_addr = fonction->start_addr;
22
+    int depth = fonction->function_depth;
23
+    int return_type = fonction->type;
24
+    char typeStr[20];
25
+    if (return_type == RET_INT){
26
+        strcpy(typeStr, "INT");
27
+    } else if (return_type == RET_INT_PTR){
28
+        strcpy(typeStr, "INT_PTR");
29
+    }
30
+    printf("%-20s\t\t %-12s\t\t %-12d\t %-12d\n", function_name, typeStr, start_addr, depth);
31
+}
32
+
33
+void print_fonction_table(Table_Fonctions * table) {
34
+    printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\n", "Function Name", "Return Type", "Start Address", "Depth");
35
+    Fonction fonction;
36
+    for (int i = 1; i < table->depth; i++) {
37
+        fonction = table->array[i];
38
+        print_function(&fonction);
39
+    }
40
+}
41
+
42
+int function_exists(Table_Fonctions * table, char * func_name){
43
+    for (int i = 0; i < table->depth; i++){
44
+        if (strcmp(table->array[i].function_name, func_name) == 0){
45
+            return i;
46
+        }
47
+    }
48
+    return -1;
49
+}
50
+
51
+/*
52
+int main(){
53
+    Table_Fonctions table;
54
+    initialise_function_table(&table);
55
+    add_function(&table, "Fonction1", 0, 7);
56
+    add_function(&table, "Fonction2", 1, 23);
57
+    print_fonction_table(&table);
58
+    return 1;
59
+}*/

+ 35
- 0
compiler/table_fonctions.h View File

@@ -0,0 +1,35 @@
1
+//
2
+// Created by Nahom Belay on 29/04/2021.
3
+//
4
+
5
+#ifndef PROJET_SYSTEME_TABLE_FONCTIONS_H
6
+#define PROJET_SYSTEME_TABLE_FONCTIONS_H
7
+
8
+#define FUNCTION_TABLE_SIZE 50
9
+#define FUNCTION_NAME_SIZE 30
10
+
11
+enum Return_Type {RET_INT , RET_INT_PTR};
12
+
13
+typedef struct Fonction {
14
+    char function_name[FUNCTION_NAME_SIZE];
15
+    int start_addr ;
16
+    enum Return_Type type;
17
+    int function_depth;
18
+} Fonction;
19
+
20
+typedef struct Table_Fonctions {
21
+    Fonction array[FUNCTION_TABLE_SIZE];
22
+    int depth;
23
+} Table_Fonctions;
24
+
25
+void initialise_function_table(Table_Fonctions * table);
26
+
27
+void add_function(Table_Fonctions * table, char * function_name, enum Return_Type return_type, int start_addr);
28
+
29
+void print_fonction_table(Table_Fonctions * table);
30
+
31
+int function_exists(Table_Fonctions * table, char * func_name);
32
+
33
+
34
+
35
+#endif //PROJET_SYSTEME_TABLE_FONCTIONS_H

+ 152
- 0
compiler/table_symboles.c View File

@@ -0,0 +1,152 @@
1
+#include "table_symboles.h"
2
+#include <stdio.h>
3
+#include <string.h>
4
+
5
+
6
+void initialise_table(Table_Symboles * table){
7
+    table->indexAvailableBottom = TABLE_SIZE - 1;
8
+    table->indexAvailableTop = 0;
9
+    table->depth = 0;
10
+}
11
+
12
+int variable_exists(Table_Symboles * table, char * varName){
13
+    for (int i = 0; i < table->indexAvailableTop; i++){
14
+        if (strcmp(varName, table->array[i].Variable_Name) == 0){
15
+            return i;
16
+        }
17
+    }
18
+
19
+    for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){
20
+        if (strcmp(varName, table->array[i].Variable_Name) == 0){
21
+            return i;
22
+        }
23
+    }
24
+    return 0;
25
+}
26
+int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init, int depth){
27
+    Symbole symbole;
28
+    strcpy(symbole.Variable_Name, varName);
29
+    symbole.addr = table->indexAvailableTop;
30
+    symbole.init = init;
31
+    symbole.type = type;
32
+    symbole.symbole_depth = table->depth;
33
+    if (table->indexAvailableTop >= table->indexAvailableBottom){
34
+        return -1;
35
+    } else if (variable_exists(table, varName) != 0){
36
+        return -2;
37
+    } else {
38
+        table->array[table->indexAvailableTop] = symbole;
39
+        table->indexAvailableTop++;
40
+    }
41
+    return 0;
42
+}
43
+
44
+int add_symbole_bottom(Table_Symboles * table){
45
+    Symbole symbole;
46
+    symbole.addr = table->indexAvailableBottom;
47
+    //symbole.symbole_depth = -1;
48
+    if (table->indexAvailableTop >= table->indexAvailableBottom){
49
+        return -1;
50
+    } else {
51
+        table->array[table->indexAvailableBottom] = symbole;
52
+        table->indexAvailableBottom--;
53
+    }
54
+    return 0;
55
+}
56
+
57
+int remove_symboles(Table_Symboles * table){
58
+    if (table->indexAvailableTop > 0){
59
+        while(table->indexAvailableTop > 0){
60
+            if (table->array[table->indexAvailableTop-1].symbole_depth == table->depth){
61
+                table->indexAvailableTop--;
62
+            } else {
63
+                break;
64
+            }
65
+
66
+        }
67
+    }
68
+
69
+
70
+        //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
71
+    return 0;
72
+}
73
+
74
+void free_temp(Table_Symboles * table){
75
+    table->indexAvailableBottom++;
76
+    if (table->indexAvailableBottom >= TABLE_SIZE){
77
+        printf("Huge error\n");
78
+        table->indexAvailableBottom--;
79
+    }
80
+}
81
+
82
+int prepare_function_call(Table_Symboles * table){
83
+    prepare_argument_push(table);
84
+    prepare_argument_push(table);
85
+}
86
+
87
+int prepare_argument_push(Table_Symboles * table){
88
+    Symbole symbole;
89
+    symbole.addr = table->indexAvailableTop;
90
+    symbole.symbole_depth = table->depth;
91
+    if (table->indexAvailableTop < table->indexAvailableBottom){
92
+        table->array[table->indexAvailableTop] = symbole;
93
+        table->indexAvailableTop++;
94
+        return (table->indexAvailableTop) - 1 ;
95
+    }
96
+
97
+}
98
+
99
+int initialise_symbole(Table_Symboles * table, char * varName){
100
+    int index = variable_exists(table, varName);
101
+    if (index == -1){
102
+        return -1;
103
+    } else {
104
+        table->array[index].init = INITIALISED;
105
+    }
106
+}
107
+
108
+void print_symbole(Symbole * symbole){
109
+    char * var = symbole->Variable_Name;
110
+    int addr = symbole->addr;
111
+    enum Symbole_Type type = symbole->type;
112
+    char typeStr[20];
113
+    if (type == TYPE_INT){
114
+        strcpy(typeStr, "INT");
115
+    } else if (type == TYPE_CONST_INT){
116
+        strcpy(typeStr, "CONST_INT");
117
+    } else if (type == TYPE_INT_PTR) {
118
+        strcpy(typeStr, "INT_PTR");
119
+    } else {
120
+        strcpy(typeStr, "Error type");
121
+    }
122
+    enum Initialised_Variable init = symbole->init;
123
+    char initStr[20];
124
+    if (init == INITIALISED){
125
+        strcpy(initStr,"INITIALISED");
126
+    } else{
127
+        strcpy(initStr,"NOT_INITIALISED");
128
+    }
129
+    int depth = symbole->symbole_depth;
130
+    printf("%-20s\t\t %-12s\t\t %-12d\t %-20s\t %-12d\n", var, typeStr, addr, initStr, depth);
131
+}
132
+
133
+void print_table(Table_Symboles * table){
134
+    printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised", "Depth");
135
+    int indexTop = table->indexAvailableTop;
136
+    int indexBottom = table->indexAvailableBottom;
137
+    Symbole symbole;
138
+    for (int i = 0; i < indexTop; i++){
139
+        symbole = table->array[i];
140
+        print_symbole(&symbole);
141
+    }
142
+    if (table->indexAvailableBottom != TABLE_SIZE - 1){
143
+        printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "...", "...", "...", "...", "...");
144
+        for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
145
+            symbole = table->array[i];
146
+            print_symbole(&symbole);
147
+        }
148
+    }
149
+
150
+    
151
+
152
+}

+ 95
- 0
compiler/table_symboles.h View File

@@ -0,0 +1,95 @@
1
+#ifndef TABLE_SYMBOLES_H
2
+#define TABLE_SYMBOLES_H
3
+
4
+#define TABLE_SIZE 256
5
+#define VARIABLE_SIZE 30
6
+
7
+enum Symbole_Type {TYPE_INT , TYPE_CONST_INT, TYPE_INT_PTR};
8
+enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
9
+
10
+typedef struct Symboles {
11
+    char Variable_Name[VARIABLE_SIZE];
12
+    int addr ;
13
+    enum Symbole_Type type;
14
+    enum Initialised_Variable init;
15
+    int symbole_depth;
16
+} Symbole;
17
+
18
+typedef struct Table_Symboles {
19
+    Symbole array[TABLE_SIZE];
20
+    int indexAvailableTop;
21
+    int indexAvailableBottom;
22
+    int depth;
23
+} Table_Symboles;
24
+
25
+/**
26
+ * Initialises indexAvailableTop at 0 and indexAvailableBottom at TABLE_SIZE - 1
27
+ * @param table
28
+ */
29
+void initialise_table(Table_Symboles * table);
30
+
31
+/**
32
+ * Adds a symbole at the top (regular varaibles)
33
+ * @param table
34
+ * @param varName
35
+ * @param type
36
+ * @param init
37
+ * @return if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table
38
+ */
39
+int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init, int depth);
40
+
41
+/**
42
+ * Adds a symbole at the bottom (temp variables)
43
+ * @param table
44
+ * @return 0 if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table
45
+ */
46
+int add_symbole_bottom(Table_Symboles * table);
47
+
48
+
49
+
50
+/**
51
+ * Verifies if a varaible name is already present in the table to avoid duplicates
52
+ * @param table
53
+ * @param varName
54
+ * @return -1 if the varaible name exists, 0 if it doesn't
55
+ */
56
+int variable_exists(Table_Symboles * table, char * varName);
57
+
58
+/**
59
+ * Removes symbole from table having certain depth
60
+ * @param table
61
+ * @return -1 if the symbole isn't in the table, 0 otherwise
62
+ */
63
+int remove_symboles(Table_Symboles * table);
64
+
65
+
66
+void free_temp(Table_Symboles * table);
67
+
68
+int prepare_function_call(Table_Symboles * table);
69
+
70
+int prepare_argument_push(Table_Symboles * table);
71
+
72
+
73
+/**
74
+ * Initialises an already exisiting symbole
75
+ * @param table
76
+ * @param varName
77
+ * @return -1 if the symbole isn't in the table, 0 otherwise
78
+ */
79
+int initialise_symbole(Table_Symboles * table, char * varName);
80
+
81
+
82
+/**
83
+ * Prints a symbole with this format
84
+ * varName      | Type  | Address   | Initialised/Not_Initialised
85
+ * @param symbole
86
+ */
87
+void print_symbole(Symbole * symbole);
88
+
89
+/**
90
+ * Prints the table
91
+ * @param table
92
+ */
93
+void print_table(Table_Symboles * table);
94
+
95
+#endif

interpreter/input.txt → interpreter/interpreter_input.txt View File


Loading…
Cancel
Save