Browse Source

Modifications while a faire

Nahom 3 years ago
parent
commit
17419e1663
17 changed files with 2448 additions and 1926 deletions
  1. BIN
      .DS_Store
  2. 5
    0
      .gitignore
  3. 8
    0
      .idea/.gitignore
  4. 13
    0
      .idea/misc.xml
  5. 8
    0
      .idea/modules.xml
  6. 2
    0
      .idea/projet_systeme.iml
  7. 6
    0
      .idea/vcs.xml
  8. BIN
      a.out
  9. 751
    711
      analyse_syntaxique.output
  10. 969
    834
      analyse_syntaxique.tab.c
  11. 91
    68
      analyse_syntaxique.tab.h
  12. 52
    25
      analyse_syntaxique.y
  13. 228
    53
      gen_assembleur.c
  14. 77
    6
      gen_assembleur.h
  15. 15
    0
      instructions.txt
  16. 219
    227
      lex.yy.c
  17. 4
    2
      script.sh

BIN
.DS_Store View File


+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
1
+*.tab.c
2
+*.tab.h
3
+*.yy.c
4
+a.out
5
+poubelle/

+ 8
- 0
.idea/.gitignore View File

@@ -0,0 +1,8 @@
1
+# Default ignored files
2
+/shelf/
3
+/workspace.xml
4
+# Datasource local storage ignored files
5
+/dataSources/
6
+/dataSources.local.xml
7
+# Editor-based HTTP Client requests
8
+/httpRequests/

+ 13
- 0
.idea/misc.xml View File

@@ -0,0 +1,13 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
4
+  <component name="MakefileSettings">
5
+    <option name="linkedExternalProjectsSettings">
6
+      <MakefileProjectSettings>
7
+        <option name="externalProjectPath" value="$PROJECT_DIR$" />
8
+        <option name="version" value="2" />
9
+      </MakefileProjectSettings>
10
+    </option>
11
+  </component>
12
+  <component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
13
+</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/.idea/projet_systeme.iml" filepath="$PROJECT_DIR$/.idea/projet_systeme.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

+ 2
- 0
.idea/projet_systeme.iml View File

@@ -0,0 +1,2 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module classpath="External" type="CPP_MODULE" version="4" />

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="VcsDirectoryMappings">
4
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+  </component>
6
+</project>

BIN
a.out View File


+ 751
- 711
analyse_syntaxique.output
File diff suppressed because it is too large
View File


+ 969
- 834
analyse_syntaxique.tab.c
File diff suppressed because it is too large
View File


+ 91
- 68
analyse_syntaxique.tab.h View File

@@ -1,13 +1,14 @@
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 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 13
    This program is distributed in the hope that it will be useful,
13 14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -15,7 +16,9 @@
15 16
    GNU General Public License for more details.
16 17
 
17 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 23
 /* As a special exception, you may create a larger work that contains
21 24
    part or all of the Bison parser skeleton and distribute that work
@@ -30,77 +33,97 @@
30 33
    This special exception was added by the Free Software Foundation in
31 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 37
 #ifndef YYTOKENTYPE
45 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
+     tPV = 269,
54
+     tVIRGULE = 270,
55
+     tAFFECTATION = 271,
56
+     tEGAL = 272,
57
+     tDIFF = 273,
58
+     tLT = 274,
59
+     tGT = 275,
60
+     tGTE = 276,
61
+     tLTE = 277,
62
+     tMAIN = 278,
63
+     tINT = 279,
64
+     tPRINT = 280,
65
+     tRETURN = 281,
66
+     tOR = 282,
67
+     tAND = 283,
68
+     tIF = 284,
69
+     tELSE = 285,
70
+     tWHILE = 286,
71
+     tCONST = 287,
72
+     tVAR = 288,
73
+     tNOT = 289
74
+   };
81 75
 #endif
76
+/* Tokens.  */
77
+#define tENTIER 258
78
+#define tENTIEREXP 259
79
+#define tADD 260
80
+#define tSUB 261
81
+#define tMUL 262
82
+#define tDIV 263
83
+#define tPO 264
84
+#define tPF 265
85
+#define tAO 266
86
+#define tAF 267
87
+#define tERROR 268
88
+#define tPV 269
89
+#define tVIRGULE 270
90
+#define tAFFECTATION 271
91
+#define tEGAL 272
92
+#define tDIFF 273
93
+#define tLT 274
94
+#define tGT 275
95
+#define tGTE 276
96
+#define tLTE 277
97
+#define tMAIN 278
98
+#define tINT 279
99
+#define tPRINT 280
100
+#define tRETURN 281
101
+#define tOR 282
102
+#define tAND 283
103
+#define tIF 284
104
+#define tELSE 285
105
+#define tWHILE 286
106
+#define tCONST 287
107
+#define tVAR 288
108
+#define tNOT 289
109
+
82 110
 
83
-/* Value type.  */
84
-#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
85 111
 
86
-union YYSTYPE
87
-{
88
-#line 1 "analyse_syntaxique.y" /* yacc.c:1909  */
89 112
 
113
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
114
+typedef union YYSTYPE
115
+#line 1 "analyse_syntaxique.y"
116
+{
90 117
 int nombre;
91 118
 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
119
+}
120
+/* Line 1529 of yacc.c.  */
121
+#line 122 "analyse_syntaxique.tab.h"
122
+	YYSTYPE;
123
+# define yystype YYSTYPE /* obsolescent; will be withdrawn */
98 124
 # define YYSTYPE_IS_DECLARED 1
125
+# define YYSTYPE_IS_TRIVIAL 1
99 126
 #endif
100 127
 
101
-
102 128
 extern YYSTYPE yylval;
103 129
 
104
-int yyparse (void);
105
-
106
-#endif /* !YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED  */

+ 52
- 25
analyse_syntaxique.y View File

@@ -12,6 +12,7 @@ enum Initialised_Variable init;
12 12
 enum Symbole_Type type;
13 13
 Table_Symboles table;
14 14
 instructions_array array;
15
+int whileCondition;
15 16
 %}
16 17
 
17 18
 
@@ -23,12 +24,13 @@ instructions_array array;
23 24
 %type<nombre> Cond
24 25
 
25 26
 
27
+
26 28
 %token tADD
27 29
 %token tSUB
28 30
 %token tMUL
29 31
 %token tDIV
30 32
 
31
-%token tPO
33
+%token<nombre> tPO
32 34
 %token tPF
33 35
 %token tAO
34 36
 %token tAF
@@ -52,7 +54,7 @@ instructions_array array;
52 54
 %token tAND
53 55
 %token<nombre> tIF
54 56
 %token tELSE
55
-%token tWHILE
57
+%token<nombre> tWHILE
56 58
 %token tCONST
57 59
 %token<id> tVAR
58 60
 %token tNOT
@@ -110,12 +112,15 @@ Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);}
110 112
 
111 113
 Aff : tVAR tAFFECTATION E tPV {printf("Affectation : %s\n", $1); free_temp(&table);};
112 114
 
113
-E : tENTIER {int vt = gen_entier(&table, &array, $1); $$ = vt;};
114
-E : tVAR {int vt = gen_var(&table, &array, $1); $$ = vt;}; 
115
-E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
116
-E : E tMUL E {gen_arithmetique(&array, MUL, $1, $3); free_temp(&table); $$ = $1;}  ; 
117
-E : E tSUB E {gen_arithmetique(&array, SOU, $1, $3); free_temp(&table); $$ = $1;} ;
118
-E : E tDIV E {gen_arithmetique(&array, DIV, $1, $3); free_temp(&table); $$ = $1;} ;
115
+//E : tENTIER {int vt = gen_entier(&table, &array, $1); $$ = vt;};
116
+E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
117
+//E : tVAR {int vt = gen_var(&table, &array, $1); $$ = vt;};
118
+E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, CPY, vt, varAddr); $$ = vt;};
119
+//E : E tADD E {gen_arithmetique(&array, ADD, $1, $3); free_temp(&table); $$ = $1;} ;
120
+E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
121
+E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;}  ;
122
+E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
123
+E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
119 124
 E : tSUB E {printf("Variable negative\n");} ;
120 125
 E : Invocation {$$ = 1234;};
121 126
 E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
@@ -128,38 +133,59 @@ SuiteArgs : tVIRGULE tVAR SuiteArgs ;
128 133
 SuiteArgs : ;
129 134
 
130 135
 If : tIF tPO Cond tPF {
131
-    gen_jmpf(&table, &array, $3);
136
+    //gen_jmpf(&table, &array, $3, -1);
137
+    generate_instruction_2(&array, JMF, $3, -1);
138
+    free_temp(&table);
132 139
     $1 = array.index;
133 140
 }
134
-tAO Instructions tAF 
141
+tAO Instructions tAF
135 142
 {
136 143
     int adr_jmp = array.index;
137
-    update_jump(&array, $1, adr_jmp);
144
+    update_jmf(&array, $1, adr_jmp);
138 145
 }
139
-Else {printf("Dans if\n");};
146
+Else {};
140 147
 
141 148
 Else : tELSE tAO Instructions tAF {printf("else\n");} ;
142 149
 Else : ;
143 150
 Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ;
144 151
 
145
-While : tWHILE tPO Cond tPF tAO Instructions tAF {printf("Dans while\n");};
152
+/*While : tWHILE tPO {
153
+	$2 = array.index + 1 ;
154
+} Cond tPF {*/
155
+While : tWHILE tPO Cond tPF {
156
+	//gen_jmpf(&table, &array, $3, -1);
157
+	generate_instruction_2(&array, JMF, $3, -1);
158
+	free_temp(&table);
159
+	$1 = array.index;
160
+}
161
+tAO Instructions tAF {
162
+	int adr_jmp = array.index;
163
+	update_jmf(&array, $1, adr_jmp);
164
+	//gen_jmpf(&table, &array, $1, $2);
165
+	generate_instruction_1(&array, JMP, $2);
166
+};
167
+
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;};
170
+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;} ;
172
+Cond : E tGT E {generate_instruction_3(&array, GT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
173
+Cond : E tLTE E {generate_instruction_3(&array, LTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
174
+Cond : E tGTE E {generate_instruction_3(&array, GTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
175
+Cond : E tAND E {generate_instruction_3(&array, AND, $1, $1, $3); free_temp(&table); $$ = $3;} ;
176
+Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table); $$ = $3;} ;
177
+Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
178
+Cond : E {$$ = $1; };
179
+
146 180
 
147
-Cond : E tEGAL E {printf("Cond ==\n");} ;
148
-Cond : E tDIFF E {printf("Cond !=\n");} ;
149
-Cond : E tLT E {printf("Cond <\n");} ;
150
-Cond : E tGT E {printf("Cond >\n");} ;
151
-Cond : E tLTE E {printf("Cond <=\n");} ;
152
-Cond : E tGTE E{printf("Cond >=\n");} ;
153
-Cond : E tAND Cond {printf("Cond &&\n");} ;
154
-Cond : E tOR Cond {printf("Cond ||\n");} ;
155
-Cond : tNOT Cond {printf("Cond !\n");} ;
156
-Cond : E;
157 181
 
158 182
 Invocation : tVAR tPO  Args  tPF {printf("Dans invocation\n");};
159 183
 
160
-Print : tPRINT tPO tVAR tPF tPV {printf("printf de %s\n", $3);};
184
+//Print : tPRINT tPO E tPF tPV {gen_print(&table, &array, $3);};
185
+Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
161 186
 
162
-Return : tRETURN E tPV {$$= gen_return(&table, &array, $2);};
187
+//Return : tRETURN E tPV {$$ = gen_return(&table, &array, $2);};
188
+Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
163 189
 
164 190
 %%
165 191
 #include <stdio.h>
@@ -171,5 +197,6 @@ void main(void){
171 197
     print_table(&table);
172 198
     //remove_symboles(&table, 0);
173 199
     //print_table(&table);
200
+    exportInstructions(&array);
174 201
 }
175 202
 

+ 228
- 53
gen_assembleur.c View File

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

+ 77
- 6
gen_assembleur.h View File

@@ -5,7 +5,7 @@
5 5
 
6 6
 #include "table_symboles.h"
7 7
 
8
-enum operation{ADD, SOU, MUL, DIV, CPY, AFC, RET, JMPF};
8
+enum operation{ADD, SOU, MUL, DIV, CPY, AFC, RET, JMF, JMP, EQ, NEQ, LT, GT, LTE, GTE, AND, OR, NOT, PRI};
9 9
 
10 10
 typedef struct instruction{
11 11
     enum operation operation;
@@ -20,8 +20,80 @@ typedef struct instructions_array{
20 20
     int index;
21 21
 } instructions_array;
22 22
 
23
+/**
24
+ *
25
+ * @param op operation
26
+ * @return returns the string that corresponds to the enum operation op
27
+ */
28
+char * operationName(enum operation op);
23 29
 
30
+/**
31
+ * Initialises the instructions array
32
+ * @param array
33
+ */
34
+void initialise_asm(instructions_array * array);
24 35
 
36
+//renvoie l'index (ou valeur?) de la premiere @ dispo
37
+/**
38
+ * Fetch address of a temporary variable
39
+ * @param table
40
+ * @return first available temp address
41
+ */
42
+int new_temp(Table_Symboles * table);
43
+
44
+/**
45
+ * Adds intruction to instruction array
46
+ * @param array
47
+ * @param intru
48
+ * @return 0 if instruction was added successfully, -1 if not
49
+ */
50
+int add_instruction(instructions_array * array, instruction * intru);
51
+
52
+/**
53
+ * Generates intruction with one parameter
54
+ * @param array
55
+ * @param op
56
+ * @param arg1
57
+ * @return
58
+ */
59
+int generate_instruction_1(instructions_array * array, enum operation op, int arg1);
60
+
61
+/**
62
+ * Generates intruction with two parameters
63
+ * @param array
64
+ * @param op
65
+ * @param arg1
66
+ * @param arg2
67
+ * @return
68
+ */
69
+int generate_instruction_2(instructions_array * array, enum operation op, int arg1, int arg2);
70
+
71
+/**
72
+ * Generates intruction with three parameters
73
+ * @param array
74
+ * @param op
75
+ * @param arg1
76
+ * @param arg2
77
+ * @param arg3
78
+ * @return
79
+ */
80
+int generate_instruction_3(instructions_array * array, enum operation op, int arg1, int arg2, int arg3);
81
+
82
+/**
83
+ * Updates the JMF instruction with the correct jump destination address
84
+ * @param array
85
+ * @param instru_index
86
+ * @param adr_jmp
87
+ */
88
+void update_jmf(instructions_array * array, int instru_index, int adr_jmp);
89
+
90
+
91
+
92
+
93
+
94
+void exportInstructions(instructions_array * array);
95
+
96
+/*
25 97
 void gen_arithmetique(instructions_array * array, enum operation op, int arg1, int arg2);
26 98
 
27 99
 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);
30 102
 
31 103
 int gen_return(Table_Symboles * table, instructions_array * array, int adr);
32 104
 
33
-int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond);
34
-
35
-void update_jump(instructions_array * array, int if_value, int adr_jmp);
105
+int gen_jmpf(Table_Symboles * table, instructions_array * array, int cond, int dest);
36 106
 
37
-//renvoie l'index (ou valeur?) de la premiere @ dispo
38
-int new_temp(Table_Symboles * table);
107
+int gen_condition(Table_Symboles * table, instructions_array * array, enum operation op, int arg1, int arg2);
39 108
 
109
+int gen_print(Table_Symboles * table, instructions_array * array, int arg1);
110
+ */
40 111
 #endif

+ 15
- 0
instructions.txt View File

@@ -0,0 +1,15 @@
1
+0	 AFC 49 1
2
+1	 AFC 49 2
3
+2	 AFC 48 3
4
+3	 ADD 49 49 48
5
+4	 AFC 49 4
6
+5	 CPY 49 1
7
+6	 NOT 49 49
8
+7	 JPF 49 10
9
+8	 AFC 49 1
10
+9	 AFC 49 4
11
+10	 JPM 4
12
+11	 CPY 49 1
13
+12	 PRI 49
14
+13	 AFC 49 5
15
+14	 RET 49

+ 219
- 227
lex.yy.c
File diff suppressed because it is too large
View File


+ 4
- 2
script.sh View File

@@ -6,9 +6,11 @@ echo "
6 6
         const int var1 = 1, var2;
7 7
         int var2 = 2 + 3;
8 8
         int var3, var4 = 4;
9
-        if( 4){
10
-            var2 = 6;
9
+        while (!var2){
10
+            var2 = 1;
11
+            var3 = 4;
11 12
         }
13
+        printf(var2);
12 14
         return 5;
13 15
     }
14 16
 " | ./a.out

Loading…
Cancel
Save