Browse Source

pointeur et fonction OK

Nahom 2 years ago
parent
commit
c363024e17
15 changed files with 3130 additions and 2279 deletions
  1. BIN
      a.out
  2. 2
    0
      analyse_lexicale.lex
  3. 1314
    948
      analyse_syntaxique.output
  4. 1172
    899
      analyse_syntaxique.tab.c
  5. 93
    68
      analyse_syntaxique.tab.h
  6. 67
    40
      analyse_syntaxique.y
  7. 44
    1
      gen_assembleur.c
  8. 11
    1
      gen_assembleur.h
  9. 10
    21
      instructions.txt
  10. 271
    274
      lex.yy.c
  11. 7
    12
      script.sh
  12. 59
    0
      table_fonctions.c
  13. 35
    0
      table_fonctions.h
  14. 38
    12
      table_symboles.c
  15. 7
    3
      table_symboles.h

BIN
a.out View File


+ 2
- 0
analyse_lexicale.lex View File

39
 tAND        "&&"
39
 tAND        "&&"
40
 tOR         "||"
40
 tOR         "||"
41
 tDIFF       "!="
41
 tDIFF       "!="
42
+tAPPERSAND  "&"
42
 DIGIT       [0-9]
43
 DIGIT       [0-9]
43
 VARIABLE    [A-Za-z0-9_]+
44
 VARIABLE    [A-Za-z0-9_]+
44
 CONST       "const"
45
 CONST       "const"
90
 {tELSE}         {return tELSE ;}
91
 {tELSE}         {return tELSE ;}
91
 {tWHILE}        {return tWHILE ;}
92
 {tWHILE}        {return tWHILE ;}
92
 
93
 
94
+{tAPPERSAND}    {return tAPPERSAND;}
93
 {CONST}         {return tCONST ;}
95
 {CONST}         {return tCONST ;}
94
 {ENTIER}        {yylval.nombre = atoi(yytext); return tENTIER ;}
96
 {ENTIER}        {yylval.nombre = atoi(yytext); return tENTIER ;}
95
 {ENTIEREXP}     {yylval.nombre = -1; return tENTIEREXP;}
97
 {ENTIEREXP}     {yylval.nombre = -1; return tENTIEREXP;}

+ 1314
- 948
analyse_syntaxique.output
File diff suppressed because it is too large
View File


+ 1172
- 899
analyse_syntaxique.tab.c
File diff suppressed because it is too large
View File


+ 93
- 68
analyse_syntaxique.tab.h View File

1
-/* A Bison parser, made by GNU Bison 3.0.4.  */
1
+/* A Bison parser, made by GNU Bison 2.3.  */
2
 
2
 
3
-/* Bison interface for Yacc-like parsers in C
3
+/* Skeleton interface for Bison's Yacc-like parsers in C
4
 
4
 
5
-   Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
5
+   Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
6
+   Free Software Foundation, Inc.
6
 
7
 
7
-   This program is free software: you can redistribute it and/or modify
8
+   This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    it under the terms of the GNU General Public License as published by
9
-   the Free Software Foundation, either version 3 of the License, or
10
-   (at your option) any later version.
10
+   the Free Software Foundation; either version 2, or (at your option)
11
+   any later version.
11
 
12
 
12
    This program is distributed in the hope that it will be useful,
13
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    GNU General Public License for more details.
16
    GNU General Public License for more details.
16
 
17
 
17
    You should have received a copy of the GNU General Public License
18
    You should have received a copy of the GNU General Public License
18
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
+   along with this program; if not, write to the Free Software
20
+   Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
+   Boston, MA 02110-1301, USA.  */
19
 
22
 
20
 /* As a special exception, you may create a larger work that contains
23
 /* As a special exception, you may create a larger work that contains
21
    part or all of the Bison parser skeleton and distribute that work
24
    part or all of the Bison parser skeleton and distribute that work
30
    This special exception was added by the Free Software Foundation in
33
    This special exception was added by the Free Software Foundation in
31
    version 2.2 of Bison.  */
34
    version 2.2 of Bison.  */
32
 
35
 
33
-#ifndef YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED
34
-# define YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED
35
-/* Debug traces.  */
36
-#ifndef YYDEBUG
37
-# define YYDEBUG 1
38
-#endif
39
-#if YYDEBUG
40
-extern int yydebug;
41
-#endif
42
-
43
-/* Token type.  */
36
+/* Tokens.  */
44
 #ifndef YYTOKENTYPE
37
 #ifndef YYTOKENTYPE
45
 # define YYTOKENTYPE
38
 # define YYTOKENTYPE
46
-  enum yytokentype
47
-  {
48
-    tENTIER = 258,
49
-    tENTIEREXP = 259,
50
-    tADD = 260,
51
-    tSUB = 261,
52
-    tMUL = 262,
53
-    tDIV = 263,
54
-    tPO = 264,
55
-    tPF = 265,
56
-    tAO = 266,
57
-    tAF = 267,
58
-    tERROR = 268,
59
-    tPV = 269,
60
-    tVIRGULE = 270,
61
-    tAFFECTATION = 271,
62
-    tEGAL = 272,
63
-    tDIFF = 273,
64
-    tLT = 274,
65
-    tGT = 275,
66
-    tGTE = 276,
67
-    tLTE = 277,
68
-    tMAIN = 278,
69
-    tINT = 279,
70
-    tPRINT = 280,
71
-    tRETURN = 281,
72
-    tOR = 282,
73
-    tAND = 283,
74
-    tIF = 284,
75
-    tELSE = 285,
76
-    tWHILE = 286,
77
-    tCONST = 287,
78
-    tVAR = 288,
79
-    tNOT = 289
80
-  };
39
+   /* Put the tokens into the symbol table, so that GDB and other debuggers
40
+      know about them.  */
41
+   enum yytokentype {
42
+     tENTIER = 258,
43
+     tENTIEREXP = 259,
44
+     tADD = 260,
45
+     tSUB = 261,
46
+     tMUL = 262,
47
+     tDIV = 263,
48
+     tPO = 264,
49
+     tPF = 265,
50
+     tAO = 266,
51
+     tAF = 267,
52
+     tERROR = 268,
53
+     tAPPERSAND = 269,
54
+     tPV = 270,
55
+     tVIRGULE = 271,
56
+     tAFFECTATION = 272,
57
+     tEGAL = 273,
58
+     tDIFF = 274,
59
+     tLT = 275,
60
+     tGT = 276,
61
+     tGTE = 277,
62
+     tLTE = 278,
63
+     tMAIN = 279,
64
+     tINT = 280,
65
+     tPRINT = 281,
66
+     tRETURN = 282,
67
+     tOR = 283,
68
+     tAND = 284,
69
+     tIF = 285,
70
+     tELSE = 286,
71
+     tWHILE = 287,
72
+     tCONST = 288,
73
+     tVAR = 289,
74
+     tNOT = 290
75
+   };
81
 #endif
76
 #endif
77
+/* Tokens.  */
78
+#define tENTIER 258
79
+#define tENTIEREXP 259
80
+#define tADD 260
81
+#define tSUB 261
82
+#define tMUL 262
83
+#define tDIV 263
84
+#define tPO 264
85
+#define tPF 265
86
+#define tAO 266
87
+#define tAF 267
88
+#define tERROR 268
89
+#define tAPPERSAND 269
90
+#define tPV 270
91
+#define tVIRGULE 271
92
+#define tAFFECTATION 272
93
+#define tEGAL 273
94
+#define tDIFF 274
95
+#define tLT 275
96
+#define tGT 276
97
+#define tGTE 277
98
+#define tLTE 278
99
+#define tMAIN 279
100
+#define tINT 280
101
+#define tPRINT 281
102
+#define tRETURN 282
103
+#define tOR 283
104
+#define tAND 284
105
+#define tIF 285
106
+#define tELSE 286
107
+#define tWHILE 287
108
+#define tCONST 288
109
+#define tVAR 289
110
+#define tNOT 290
111
+
82
 
112
 
83
-/* Value type.  */
84
-#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
85
 
113
 
86
-union YYSTYPE
87
-{
88
-#line 1 "analyse_syntaxique.y" /* yacc.c:1909  */
89
 
114
 
115
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
116
+typedef union YYSTYPE
117
+#line 1 "analyse_syntaxique.y"
118
+{
90
 int nombre;
119
 int nombre;
91
 char id[30];
120
 char id[30];
92
-
93
-#line 94 "analyse_syntaxique.tab.h" /* yacc.c:1909  */
94
-};
95
-
96
-typedef union YYSTYPE YYSTYPE;
97
-# define YYSTYPE_IS_TRIVIAL 1
121
+}
122
+/* Line 1529 of yacc.c.  */
123
+#line 124 "analyse_syntaxique.tab.h"
124
+	YYSTYPE;
125
+# define yystype YYSTYPE /* obsolescent; will be withdrawn */
98
 # define YYSTYPE_IS_DECLARED 1
126
 # define YYSTYPE_IS_DECLARED 1
127
+# define YYSTYPE_IS_TRIVIAL 1
99
 #endif
128
 #endif
100
 
129
 
101
-
102
 extern YYSTYPE yylval;
130
 extern YYSTYPE yylval;
103
 
131
 
104
-int yyparse (void);
105
-
106
-#endif /* !YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED  */

+ 67
- 40
analyse_syntaxique.y View File

6
 %{
6
 %{
7
 #include <stdio.h>
7
 #include <stdio.h>
8
 #include "table_symboles.h"
8
 #include "table_symboles.h"
9
+#include "table_fonctions.h"
9
 #include "gen_assembleur.h"
10
 #include "gen_assembleur.h"
10
 
11
 
11
 enum Initialised_Variable init;
12
 enum Initialised_Variable init;
12
 enum Symbole_Type type;
13
 enum Symbole_Type type;
14
+enum Return_Type return_type;
13
 Table_Symboles table;
15
 Table_Symboles table;
16
+Table_Fonctions table_fonctions;
14
 instructions_array array;
17
 instructions_array array;
15
 int whileCondition;
18
 int whileCondition;
19
+int return_value;
16
 %}
20
 %}
17
 
21
 
18
 
22
 
20
 %token<nombre> tENTIEREXP
24
 %token<nombre> tENTIEREXP
21
 
25
 
22
 %type<nombre> E
26
 %type<nombre> E
23
-%type<nombre> Return
27
+%type<nombre> Return Instructions
24
 %type<nombre> Cond
28
 %type<nombre> Cond
25
-%type<nombre> While
26
-
29
+%type<nombre> While Else Invocation
27
 
30
 
28
 
31
 
29
 %token tADD
32
 %token tADD
38
 
41
 
39
 %token tERROR
42
 %token tERROR
40
 
43
 
44
+%token tAPPERSAND
41
 %token tPV
45
 %token tPV
42
 %token tVIRGULE
46
 %token tVIRGULE
43
 %token tAFFECTATION
47
 %token tAFFECTATION
69
 
73
 
70
 %%
74
 %%
71
 
75
 
72
-//C : Fonctions Main ;
76
+/*C : Fonctions Main ;
77
+
78
+Fonctions : ;
79
+Fonctions : Fonction Fonctions ;
80
+
81
+Fonction : tINT tVAR tPO Params tPF Body;*/
82
+
83
+C : Fonctions;
73
 
84
 
74
-//Fonctions : Fonction Fonctions | ;
75
-//Fonction : tINT tVAR tPO Params tPF Body;
85
+Fonctions: Main;
86
+Fonctions: Fonction Fonctions;
76
 
87
 
77
 
88
 
78
-Main : tINT tMAIN tPO Params tPF Body {};
89
+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--;};
90
+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--;};
79
 
91
 
80
-Params : {printf("Sans params\n");} ;
92
+Function_type: tINT {type = TYPE_INT;} ;
93
+Function_type: tINT tMUL {type = TYPE_INT_PTR;};
94
+
95
+Params : {} ;
81
 Params : Param SuiteParams ;
96
 Params : Param SuiteParams ;
82
 
97
 
83
-Param : tINT tVAR {printf("Parametre : %s\n", $2);} ;
98
+Param : Param_type tVAR {add_symbole_top(&table, $2, type, INITIALISED, table.depth);} ;
84
 
99
 
100
+Param_type: tINT {type = TYPE_INT;} ;
101
+Param_type: tINT tMUL {type = TYPE_INT_PTR;};
85
 
102
 
86
-SuiteParams : tVIRGULE Param SuiteParams tPV ;
103
+SuiteParams : tVIRGULE Param SuiteParams ;
87
 SuiteParams : ;
104
 SuiteParams : ;
88
 
105
 
89
 
106
 
90
-Body : tAO Instructions Return tAF {printf("Dans body\n");} ;
107
+Body : tAO Instructions Return tAF {} ;
91
 
108
 
92
-Instructions : Instruction Instructions ;
93
-Instructions : ;
109
+Instructions : Instruction Instructions {$$ = array.index;};
110
+Instructions : {$$ = array.index;};
94
 
111
 
95
 Instruction : Aff ;
112
 Instruction : Aff ;
96
 Instruction : If ;
113
 Instruction : If ;
104
 SuiteDecl: tVIRGULE Valeur SuiteDecl ;
121
 SuiteDecl: tVIRGULE Valeur SuiteDecl ;
105
 SuiteDecl: ;
122
 SuiteDecl: ;
106
 
123
 
107
-Type : tINT {type = TYPE_INT;} ; 
124
+Type : tINT {type = TYPE_INT;} ;
108
 Type : tCONST tINT {type = TYPE_CONST_INT;} ;
125
 Type : tCONST tINT {type = TYPE_CONST_INT;} ;
126
+Type : tINT tMUL {type = TYPE_INT_PTR;};
109
 
127
 
110
-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);};
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);};
111
 Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
129
 Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
112
 
130
 
113
 
131
 
114
-Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, $3, varAddr); free_temp(&table);};
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
+
115
 
135
 
116
-//E : tENTIER {int vt = gen_entier(&table, &array, $1); $$ = vt;};
117
 E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
136
 E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
118
-//E : tVAR {int vt = gen_var(&table, &array, $1);  $$ = vt;};
119
-E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, vt); $$ = vt;};
120
-//E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
137
+E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
121
 E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
138
 E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
122
 E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;}  ;
139
 E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;}  ;
123
 E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
140
 E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
124
 E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
141
 E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
125
 E : tSUB E {printf("Variable negative\n");} ;
142
 E : tSUB E {printf("Variable negative\n");} ;
126
-E : Invocation {$$ = 1234;};
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
+	$$ = vt;};
127
 E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
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;};
128
 
152
 
129
 
153
 
130
-Args : tVAR SuiteArgs ;
131
-Args : ;
132
-
133
-SuiteArgs : tVIRGULE tVAR SuiteArgs ;
134
-SuiteArgs : ;
135
-
136
 If : tIF tPO Cond tPF {
154
 If : tIF tPO Cond tPF {
137
     //gen_jmpf(&table, &array, $3, -1);
155
     //gen_jmpf(&table, &array, $3, -1);
138
     generate_instruction_2(&array, JMF, $3, -1);
156
     generate_instruction_2(&array, JMF, $3, -1);
139
     free_temp(&table);
157
     free_temp(&table);
140
     $1 = array.index;
158
     $1 = array.index;
141
 }
159
 }
142
-tAO Instructions tAF
160
+tAO {table.depth++;} Instructions {generate_instruction_1(&array, JMP, -1)} tAF {remove_symboles(&table); table.depth--;}
143
 {
161
 {
144
     int adr_jmp = array.index;
162
     int adr_jmp = array.index;
145
     update_jmf(&array, $1, adr_jmp);
163
     update_jmf(&array, $1, adr_jmp);
146
 }
164
 }
147
-Else {};
165
+Else {printf("updating jump\n"); update_jmp(&array, $8, $13);};
148
 
166
 
149
-Else : tELSE tAO Instructions tAF {printf("else\n");} ;
150
-Else : ;
151
-Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ;
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;} ;
152
 
170
 
153
 While : tWHILE tPO {
171
 While : tWHILE tPO {
154
 	$2 = array.index ;
172
 	$2 = array.index ;
158
 	free_temp(&table);
176
 	free_temp(&table);
159
 	$1 = array.index;
177
 	$1 = array.index;
160
 }
178
 }
161
-tAO Instructions tAF {
179
+tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
162
 	int adr_jmp = array.index;
180
 	int adr_jmp = array.index;
163
 	update_jmf(&array, $1, adr_jmp);
181
 	update_jmf(&array, $1, adr_jmp);
164
 	//gen_jmpf(&table, &array, $1, $2);
182
 	//gen_jmpf(&table, &array, $1, $2);
165
 	generate_instruction_1(&array, JMP, $2);
183
 	generate_instruction_1(&array, JMP, $2);
166
 };
184
 };
167
 
185
 
168
-//Cond : E tEGAL E {int vt = gen_condition(&table, &array, EQ, $1, $3); $$ = vt;} ;
169
 Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
186
 Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
170
 Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
187
 Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
171
 Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
188
 Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
177
 Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
194
 Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
178
 Cond : E {$$ = $1; };
195
 Cond : E {$$ = $1; };
179
 
196
 
197
+Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableTop - 1);} 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, (array.index + 1));
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 : ;
180
 
209
 
181
-
182
-
183
-Invocation : tVAR tPO  Args  tPF {printf("Dans invocation\n");};
184
-
185
-//Print : tPRINT tPO E tPF tPV {gen_print(&table, &array, $3);};
186
 Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
210
 Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
187
 
211
 
188
-//Return : tRETURN E tPV {$$ = gen_return(&table, &array, $2);};
189
 Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
212
 Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
190
 
213
 
191
 %%
214
 %%
193
 void main(void){
216
 void main(void){
194
     //TODO: rajouter gestion des erreurs
217
     //TODO: rajouter gestion des erreurs
195
     initialise_table(&table);
218
     initialise_table(&table);
219
+    initialise_function_table(&table_fonctions);
196
     initialise_asm(&array);
220
     initialise_asm(&array);
197
     yyparse();
221
     yyparse();
198
     print_table(&table);
222
     print_table(&table);
223
+    printf("\n");
224
+    print_fonction_table(&table_fonctions);
225
+
199
     //remove_symboles(&table, 0);
226
     //remove_symboles(&table, 0);
200
     //print_table(&table);
227
     //print_table(&table);
201
     exportInstructions(&array);
228
     exportInstructions(&array);

+ 44
- 1
gen_assembleur.c View File

33
         case JMF:
33
         case JMF:
34
             return "JPF";
34
             return "JPF";
35
         case JMP:
35
         case JMP:
36
-            return "JPM";
36
+            return "JMP";
37
         case AND:
37
         case AND:
38
             return "AND";
38
             return "AND";
39
         case OR:
39
         case OR:
42
             return "NOT";
42
             return "NOT";
43
         case PRI:
43
         case PRI:
44
             return "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";
45
         default:
55
         default:
46
             break;
56
             break;
47
     }
57
     }
71
     return ret_addr;
81
     return ret_addr;
72
 }
82
 }
73
 
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
+
74
 int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
100
 int generate_instruction_1(instructions_array * array, enum operation op, int arg1){
75
     instruction instru;
101
     instruction instru;
76
     char * opName = operationName(op);
102
     char * opName = operationName(op);
130
     printf("%d\t JMP %d %d\n",  (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
156
     printf("%d\t JMP %d %d\n",  (instru_index - 1), array->array[instru_index].reg1, array->array[instru_index].reg2);
131
 }
157
 }
132
 
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
+
133
 void exportInstructions(instructions_array * array){
164
 void exportInstructions(instructions_array * array){
134
     FILE *file;
165
     FILE *file;
135
     file = fopen("instructions.txt", "w");
166
     file = fopen("instructions.txt", "w");
140
         instru = array->array[i];
171
         instru = array->array[i];
141
         op = instru.operation;
172
         op = instru.operation;
142
         switch (op) {
173
         switch (op) {
174
+            //0 parameters
175
+            case RET_FUN:
176
+                fprintf(file, "%d\t %s\n", i, operationName(op));
177
+                break;
143
             //1 parameter
178
             //1 parameter
144
             case JMP:
179
             case JMP:
145
             case PRI:
180
             case PRI:
151
             case NOT:
186
             case NOT:
152
             case AFC:
187
             case AFC:
153
             case COP:
188
             case COP:
189
+            case LEA:
190
+            case CALL:
154
                 fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
191
                 fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
155
                 break;
192
                 break;
193
+            case COP_LD:
194
+                fprintf(file, "%d\t %s %d [%d]\n", i, operationName(op), instru.reg1, instru.reg2);
195
+                break;
196
+            case COP_STR:
197
+                fprintf(file, "%d\t %s [%d] %d\n", i, operationName(op), instru.reg1, instru.reg2);
198
+                break;
156
             //3 parameters
199
             //3 parameters
157
             case ADD:
200
             case ADD:
158
             case SOU:
201
             case SOU:

+ 11
- 1
gen_assembleur.h View File

5
 
5
 
6
 #include "table_symboles.h"
6
 #include "table_symboles.h"
7
 
7
 
8
-enum operation{ADD, SOU, MUL, DIV, COP, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE, GTE, AND, OR, NOT, PRI};
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};
9
 
10
 
10
 typedef struct instruction{
11
 typedef struct instruction{
11
     enum operation operation;
12
     enum operation operation;
50
 int add_instruction(instructions_array * array, instruction * intru);
51
 int add_instruction(instructions_array * array, instruction * intru);
51
 
52
 
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
+/**
53
  * Generates intruction with one parameter
62
  * Generates intruction with one parameter
54
  * @param array
63
  * @param array
55
  * @param op
64
  * @param op
87
  */
96
  */
88
 void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
97
 void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
89
 
98
 
99
+void update_jmp(instructions_array * array, int instru_index, int adr_jmp);
90
 
100
 
91
 
101
 
92
 
102
 

+ 10
- 21
instructions.txt View File

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

+ 271
- 274
lex.yy.c
File diff suppressed because it is too large
View File


+ 7
- 12
script.sh View File

3
 gcc -w *.c -ly
3
 gcc -w *.c -ly
4
 echo "
4
 echo "
5
     int main(){
5
     int main(){
6
-        int var1 = 1;
7
-        int var2 = 2;
8
-        var2 = 2 + 3;
9
-        int var3;
10
-        if (!var2){
11
-            var2 = 1;
12
-            var3 = 4;
13
-        } else {
14
-            var1 = 2;
15
-        }
16
-        printf(var2);
17
-        return 5;
6
+      int c = 2;
7
+      int * p;
8
+      p = &c;
9
+      *p = 2;
10
+      int b = *p;
11
+
12
+      return 0;
18
     }
13
     }
19
 " | ./a.out
14
 " | ./a.out

+ 59
- 0
table_fonctions.c View File

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
table_fonctions.h View File

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

+ 38
- 12
table_symboles.c View File

54
     return 0;
54
     return 0;
55
 }
55
 }
56
 
56
 
57
-int remove_symboles(Table_Symboles * table, int depth){
58
-    int index;
59
-    if (depth < table->depth){
60
-        return -1;
61
-    } else {
62
-        index = table->indexAvailableTop;
63
-        while(table->array[index].symbole_depth == depth && index >=0){
64
-            //remove
65
-            table->indexAvailableTop--;
66
-            index--;
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
+
67
         }
66
         }
67
+    }
68
+
68
 
69
 
69
         //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
70
         //TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
70
-    }
71
     return 0;
71
     return 0;
72
 }
72
 }
73
 
73
 
74
 void free_temp(Table_Symboles * table){
74
 void free_temp(Table_Symboles * table){
75
     table->indexAvailableBottom++;
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
+    prepare_argument_push(table);
86
+}
87
+
88
+int prepare_argument_push(Table_Symboles * table){
89
+    Symbole symbole;
90
+    symbole.addr = table->indexAvailableTop;
91
+    symbole.symbole_depth = table->depth;
92
+    if (table->indexAvailableTop < table->indexAvailableBottom){
93
+        table->array[table->indexAvailableTop] = symbole;
94
+        table->indexAvailableTop++;
95
+        return (table->indexAvailableTop) - 1 ;
96
+    }
97
+
76
 }
98
 }
77
 
99
 
78
 int initialise_symbole(Table_Symboles * table, char * varName){
100
 int initialise_symbole(Table_Symboles * table, char * varName){
91
     char typeStr[20];
113
     char typeStr[20];
92
     if (type == TYPE_INT){
114
     if (type == TYPE_INT){
93
         strcpy(typeStr, "INT");
115
         strcpy(typeStr, "INT");
94
-    } else{
116
+    } else if (type == TYPE_CONST_INT){
95
         strcpy(typeStr, "CONST_INT");
117
         strcpy(typeStr, "CONST_INT");
118
+    } else if (type == TYPE_INT_PTR) {
119
+        strcpy(typeStr, "INT_PTR");
120
+    } else {
121
+        strcpy(typeStr, "Error type");
96
     }
122
     }
97
     enum Initialised_Variable init = symbole->init;
123
     enum Initialised_Variable init = symbole->init;
98
     char initStr[20];
124
     char initStr[20];

+ 7
- 3
table_symboles.h View File

4
 #define TABLE_SIZE 50
4
 #define TABLE_SIZE 50
5
 #define VARIABLE_SIZE 30
5
 #define VARIABLE_SIZE 30
6
 
6
 
7
-enum Symbole_Type {TYPE_INT , TYPE_CONST_INT};
7
+enum Symbole_Type {TYPE_INT , TYPE_CONST_INT, TYPE_INT_PTR};
8
 enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
8
 enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
9
 
9
 
10
 typedef struct Symboles {
10
 typedef struct Symboles {
58
 /**
58
 /**
59
  * Removes symbole from table having certain depth
59
  * Removes symbole from table having certain depth
60
  * @param table
60
  * @param table
61
- * @param depth
62
  * @return -1 if the symbole isn't in the table, 0 otherwise
61
  * @return -1 if the symbole isn't in the table, 0 otherwise
63
  */
62
  */
64
-int remove_symboles(Table_Symboles * table, int depth);
63
+int remove_symboles(Table_Symboles * table);
65
 
64
 
66
 
65
 
67
 void free_temp(Table_Symboles * table);
66
 void free_temp(Table_Symboles * table);
68
 
67
 
68
+int prepare_function_call(Table_Symboles * table);
69
+
70
+int prepare_argument_push(Table_Symboles * table);
71
+
72
+
69
 /**
73
 /**
70
  * Initialises an already exisiting symbole
74
  * Initialises an already exisiting symbole
71
  * @param table
75
  * @param table

Loading…
Cancel
Save