Browse Source

Interpreteur OK + ajout makefile

Nahom 3 years ago
parent
commit
dfdcf2feb4

BIN
.DS_Store View File


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

@@ -5,6 +5,11 @@
5 5
     <option name="linkedExternalProjectsSettings">
6 6
       <MakefileProjectSettings>
7 7
         <option name="externalProjectPath" value="$PROJECT_DIR$" />
8
+        <option name="modules">
9
+          <set>
10
+            <option value="$PROJECT_DIR$" />
11
+          </set>
12
+        </option>
8 13
         <option name="version" value="2" />
9 14
       </MakefileProjectSettings>
10 15
     </option>

+ 1
- 1
.idea/projet_systeme.iml View File

@@ -1,2 +1,2 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<module classpath="External" type="CPP_MODULE" version="4" />
2
+<module classpath="External" external.linked.project.id="projet_systeme" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="Makefile" type="CPP_MODULE" version="4" />

+ 12
- 0
Makefile View File

@@ -0,0 +1,12 @@
1
+all : compiler
2
+
3
+compiler: analyse_lexicale.lex analyse_syntaxique.y table_symboles.c table_fonctions.c
4
+		bison -d -t analyse_syntaxique.y -v
5
+		flex analyse_lexicale.lex
6
+		gcc -w *.c -ly -o compiler
7
+
8
+run: compiler
9
+		./compiler < test_file
10
+
11
+clean:
12
+		rm -f lex.yy.c compiler analyse_syntaxique.output analyse_syntaxique.tab.c analyse_syntaxique.tab.h

BIN
a.out View File


+ 0
- 1954
analyse_syntaxique.output
File diff suppressed because it is too large
View File


+ 0
- 2017
analyse_syntaxique.tab.c
File diff suppressed because it is too large
View File


+ 0
- 107
analyse_syntaxique.tab.h View File

@@ -1,107 +0,0 @@
1
-/* A Bison parser, made by GNU Bison 3.0.4.  */
2
-
3
-/* Bison interface for Yacc-like parsers in C
4
-
5
-   Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
6
-
7
-   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
-   the Free Software Foundation, either version 3 of the License, or
10
-   (at your option) any later version.
11
-
12
-   This program is distributed in the hope that it will be useful,
13
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
-   GNU General Public License for more details.
16
-
17
-   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
-
20
-/* 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
22
-   under terms of your choice, so long as that work isn't itself a
23
-   parser generator using the skeleton or a modified version thereof
24
-   as a parser skeleton.  Alternatively, if you modify or redistribute
25
-   the parser skeleton itself, you may (at your option) remove this
26
-   special exception, which will cause the skeleton and the resulting
27
-   Bison output files to be licensed under the GNU General Public
28
-   License without this special exception.
29
-
30
-   This special exception was added by the Free Software Foundation in
31
-   version 2.2 of Bison.  */
32
-
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.  */
44
-#ifndef YYTOKENTYPE
45
-# 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
-    tAPPERSAND = 269,
60
-    tPV = 270,
61
-    tVIRGULE = 271,
62
-    tAFFECTATION = 272,
63
-    tEGAL = 273,
64
-    tDIFF = 274,
65
-    tLT = 275,
66
-    tGT = 276,
67
-    tGTE = 277,
68
-    tLTE = 278,
69
-    tMAIN = 279,
70
-    tINT = 280,
71
-    tPRINT = 281,
72
-    tRETURN = 282,
73
-    tOR = 283,
74
-    tAND = 284,
75
-    tIF = 285,
76
-    tELSE = 286,
77
-    tWHILE = 287,
78
-    tCONST = 288,
79
-    tVAR = 289,
80
-    tNOT = 290
81
-  };
82
-#endif
83
-
84
-/* Value type.  */
85
-#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
86
-
87
-union YYSTYPE
88
-{
89
-#line 1 "analyse_syntaxique.y" /* yacc.c:1909  */
90
-
91
-int nombre;
92
-char id[30];
93
-
94
-#line 95 "analyse_syntaxique.tab.h" /* yacc.c:1909  */
95
-};
96
-
97
-typedef union YYSTYPE YYSTYPE;
98
-# define YYSTYPE_IS_TRIVIAL 1
99
-# define YYSTYPE_IS_DECLARED 1
100
-#endif
101
-
102
-
103
-extern YYSTYPE yylval;
104
-
105
-int yyparse (void);
106
-
107
-#endif /* !YY_YY_ANALYSE_SYNTAXIQUE_TAB_H_INCLUDED  */

+ 10
- 10
analyse_syntaxique.y View File

@@ -80,14 +80,14 @@ Fonctions : Fonction Fonctions ;
80 80
 
81 81
 Fonction : tINT tVAR tPO Params tPF Body;*/
82 82
 
83
-C : Fonctions;
83
+C : {generate_instruction_1(&array, JMP, -1);} Fonctions;
84 84
 
85 85
 Fonctions: Main;
86 86
 Fonctions: Fonction Fonctions;
87 87
 
88 88
 
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--;};
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 91
 
92 92
 Function_type: tINT {type = TYPE_INT;} ;
93 93
 Function_type: tINT tMUL {type = TYPE_INT_PTR;};
@@ -129,8 +129,8 @@ Valeur : tVAR {add_symbole_top(&table, $1, type, INITIALISED, table.depth);} tAF
129 129
 Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
130 130
 
131 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);};
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 134
 
135 135
 
136 136
 E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
@@ -141,11 +141,11 @@ E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table)
141 141
 E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
142 142
 E : tSUB E {printf("Variable negative\n");} ;
143 143
 E : Invocation {
144
-	int vt = new_temp(&table);
145
-	generate_instruction_2(&array, COP, vt, $1);
144
+	//int vt = new_temp(&table);
145
+	//generate_instruction_2(&array, COP, vt, $1);
146 146
 	remove_symboles(&table);
147 147
 	table.depth--;
148
-	$$ = vt;};
148
+	$$ = $1;};
149 149
 E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
150 150
 E : tAPPERSAND tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, LEA, vt, varAddr); $$ = vt;};
151 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;};
@@ -194,10 +194,10 @@ Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table
194 194
 Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
195 195
 Cond : E {$$ = $1; };
196 196
 
197
-Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableTop - 1);} Args  tPF
197
+Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableBottom);} Args  tPF
198 198
 	{int function_index = function_exists(&table_fonctions, $1);
199 199
 	int jmp_addr = (table_fonctions.array[function_index]).start_addr;
200
-	generate_instruction_2(&array, CALL, jmp_addr, (array.index + 1));
200
+	generate_instruction_2(&array, CALL, jmp_addr, table.indexAvailableTop);
201 201
 	$$ = return_value;
202 202
 	};
203 203
 

+ 6
- 6
gen_assembleur.c View File

@@ -173,13 +173,13 @@ void exportInstructions(instructions_array * array){
173 173
         switch (op) {
174 174
             //0 parameters
175 175
             case RET_FUN:
176
-                fprintf(file, "%d\t %s\n", i, operationName(op));
176
+                fprintf(file, "%s\n", operationName(op));
177 177
                 break;
178 178
             //1 parameter
179 179
             case JMP:
180 180
             case PRI:
181 181
             case RET:
182
-                fprintf(file, "%d\t %s %d\n", i, operationName(op), instru.reg1);
182
+                fprintf(file, "%s %d\n", operationName(op), instru.reg1);
183 183
                 break;
184 184
             //2 parameters
185 185
             case JMF:
@@ -188,13 +188,13 @@ void exportInstructions(instructions_array * array){
188 188
             case COP:
189 189
             case LEA:
190 190
             case CALL:
191
-                fprintf(file, "%d\t %s %d %d\n", i, operationName(op), instru.reg1, instru.reg2);
191
+                fprintf(file, "%s %d %d\n", operationName(op), instru.reg1, instru.reg2);
192 192
                 break;
193 193
             case COP_LD:
194
-                fprintf(file, "%d\t %s %d [%d]\n", i, operationName(op), instru.reg1, instru.reg2);
194
+                fprintf(file, "%s %d [%d]\n", operationName(op), instru.reg1, instru.reg2);
195 195
                 break;
196 196
             case COP_STR:
197
-                fprintf(file, "%d\t %s [%d] %d\n", i, operationName(op), instru.reg1, instru.reg2);
197
+                fprintf(file, "%s [%d] %d\n", operationName(op), instru.reg1, instru.reg2);
198 198
                 break;
199 199
             //3 parameters
200 200
             case ADD:
@@ -209,7 +209,7 @@ void exportInstructions(instructions_array * array){
209 209
             case LTE:
210 210
             case GT:
211 211
             case GTE:
212
-                fprintf(file, "%d\t %s %d %d %d\n", i, operationName(op), instru.reg1, instru.reg2, instru.reg3);
212
+                fprintf(file, "%s %d %d %d\n", operationName(op), instru.reg1, instru.reg2, instru.reg3);
213 213
                 break;
214 214
             default:
215 215
                 break;

+ 70
- 0
instructions.md View File

@@ -0,0 +1,70 @@
1
+## Instructions generated by our compiler 
2
+
3
+# ADD @A @B @C
4
+> Adds the values stored in B and C and stores the result in A
5
+
6
+# SOU @A @B @C
7
+> Substracts the values stored in B and C and stores the result in A
8
+
9
+# MUL @A @B @C
10
+> Multiplies the values stored in B and C and stores the result in A
11
+
12
+# DIV @A @B @C
13
+> Divides the values stored in B and C and stores the result in A
14
+
15
+# COP @A @B
16
+> Copies at address A the contents from at address B 
17
+
18
+# AFC @A #B
19
+> Copies at address A the value B
20
+
21
+# JMF @A #B
22
+> If the value located at address A is equal to 0 jumps to instruction number B
23
+
24
+# JMP #A
25
+> Jumps to instruction number A
26
+
27
+# EQ @A @B @C
28
+> If values located at adresses B and C are equal, writes 1 at address A else writes 0
29
+
30
+# NEQ @A @B @C
31
+> If values located at addresses B and C are not equal, writes 1 at address A else writes 0
32
+
33
+# LT @A @B @C
34
+> If value located at address B is less than the one located at address C, writes 1 at address A else writes 0
35
+
36
+# GT @A @B @C
37
+> If value located at address B is greater than the one located at address C, writes 1 at address A else writes 0
38
+
39
+#LTE @A @B @C
40
+> If value located at address B is less than or equal to the one located at address C, writes 1 at address A else writes 0
41
+
42
+# GTE @A @B @C
43
+> If value located at address B is greater than or equal to the one located at address C, writes 1 at address A else writes 0
44
+
45
+# AND @A @B @C
46
+> If the values located at addresses B and C are both greater or equal to 1 writes 1 at address A, else writes 0
47
+
48
+# OR @A @B @C
49
+> If the values located at addresses B and C are both equal to 0 writes 0 at address A, else writes 1
50
+
51
+# NOT @A @B
52
+> If the value at address B is greater or equal to 1, writes 0 at address A else writes 1
53
+
54
+# PRI @A
55
+> Prints the content located at address A
56
+
57
+# LEA @A @B 
58
+> Copies the value of the address B at A
59
+
60
+# COP_LD @A @B 
61
+> Copies the value pointed by B at address A
62
+
63
+# COP_STR @A @B 
64
+> Copies the value of B at the address pointed by A
65
+
66
+# RET @A
67
+> Stores the return value at A, changes the values of EBP and ESP
68
+
69
+# CALL #A #B
70
+> Saves the values of EBP and the return address, upadtes EBP to EBP + B and jumps to instruction number A

+ 26
- 16
instructions.txt View File

@@ -1,16 +1,26 @@
1
-0	 AFC 49 4
2
-1	 COP 1 49
3
-2	 AFC 49 1
4
-3	 RET 49
5
-4	 RET_FUN
6
-5	 AFC 49 2
7
-6	 COP 0 49
8
-7	 LEA 49 0
9
-8	 COP 1 49
10
-9	 AFC 49 2
11
-10	 COP_STR [1] 49
12
-11	 COP 49 1
13
-12	 COP_LD 49 [49]
14
-13	 COP 2 49
15
-14	 AFC 49 0
16
-15	 RET 49
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

BIN
interpreter/.DS_Store View File


+ 26
- 0
interpreter/input.txt View File

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

BIN
interpreter/interpreter View File


+ 14
- 0
interpreter/makefile View File

@@ -0,0 +1,14 @@
1
+SRCC:= ./src/*.c
2
+
3
+all: interpreter
4
+
5
+interpreter: ./src/interpreter.y ./src/interpreter.l ./src/instructions.c
6
+	yacc -d ./src/interpreter.y
7
+	lex ./src/interpreter.l
8
+	gcc lex.yy.c y.tab.c ./src/instructions.c -Isrc -o interpreter
9
+
10
+run: interpreter
11
+	./interpreter < input.txt
12
+
13
+clean:
14
+	rm -f lex.yy.c interpreter y.tab.h y.tab.c *.o

+ 196
- 0
interpreter/src/instructions.c View File

@@ -0,0 +1,196 @@
1
+#include <stdio.h>
2
+#include "instructions.h"
3
+
4
+int EBP = 0;
5
+
6
+struct instruction {
7
+    char ins;
8
+    int arg1;
9
+    int arg2;
10
+    int arg3;
11
+};
12
+
13
+struct instruction instructions[MAX_INSTRUCTIONS_SIZE];
14
+int current_line;
15
+int has_error;
16
+
17
+int memory[MAX_MEMORY_SIZE];
18
+
19
+int exec(int ip);
20
+int valid_memory_addr(int address);
21
+
22
+/***** Public funciton *****/
23
+
24
+void asm_init() {
25
+    current_line = 0;
26
+    has_error = 0;
27
+}
28
+
29
+void asm_add_3(char ins, int arg1, int arg2, int arg3) {
30
+    if (current_line >= MAX_INSTRUCTIONS_SIZE) {
31
+        fprintf(stderr, "ERROR readfile : Too much instructions, please modify value of MAX_INSTRUCTIONS_SIZE.\n");
32
+        has_error = 1;
33
+        return;
34
+    }
35
+
36
+    // ip are validated at runtime; memory addr are validated here
37
+    if (ins == AFC || ins == JMF) {
38
+        if (!valid_memory_addr(arg1)) {
39
+            fprintf(stderr, "ERROR readfile : INVALID addr at line %d, please verify that addr is in range 0 to MAX_MEMORY_SIZE\n", current_line);
40
+            has_error = 1;
41
+            return;
42
+        }
43
+    } else if (ins == JMP) {
44
+        // do nothing
45
+    } else {
46
+        if (!(valid_memory_addr(arg1) && valid_memory_addr(arg2)
47
+            && valid_memory_addr(arg3))) {
48
+            fprintf(stderr, "ERROR readfile : INVALID addr at line %d, please verify that addr is in range 0 to MAX_MEMORY_SIZE\n", current_line);
49
+            has_error = 1;
50
+            return;
51
+        }
52
+    }
53
+
54
+    // When OK
55
+    instructions[current_line].ins = ins;
56
+    instructions[current_line].arg1 = arg1;
57
+    instructions[current_line].arg2 = arg2;
58
+    instructions[current_line].arg3 = arg3;
59
+    current_line++;
60
+}
61
+
62
+void asm_add_2(char ins, int arg1, int arg2) {
63
+    asm_add_3(ins, arg1, arg2, 0);
64
+}
65
+
66
+void asm_add_1(char ins, int arg1) {
67
+    asm_add_3(ins, arg1, 0, 0);
68
+}
69
+
70
+void asm_run() {
71
+    int ip = 0;
72
+    if (has_error) {
73
+        fprintf(stderr, "ERROR run : abandoned due to previous error.\n");
74
+        return;
75
+    }
76
+    printf("INFO run : begin\n");
77
+    while (ip >= 0 && ip < current_line) {
78
+        // wait for user input
79
+        //getchar();
80
+        // execution
81
+        ip = exec(ip);
82
+    }
83
+    printf("INFO run : end\n");
84
+}
85
+
86
+/***** Private funciton *****/
87
+
88
+int valid_memory_addr(int addr) {
89
+    return addr >= 0 && addr < MAX_MEMORY_SIZE;
90
+}
91
+
92
+int exec(int ip) {
93
+    int next_ip = ip + 1;
94
+    char ins = instructions[ip].ins;
95
+    int arg1 = instructions[ip].arg1;
96
+    int arg2 = instructions[ip].arg2;
97
+    int arg3 = instructions[ip].arg3;
98
+    printf("%d : ", ip);
99
+
100
+    // execute inst
101
+    switch (ins) {
102
+    case ADD:
103
+        printf("ADD @%d = @%d[%d] + @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
104
+        memory[arg1] = memory[arg2] + memory[arg3]; break;
105
+    case MUL:
106
+        printf("MUL @%d = @%d[%d] * @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
107
+        memory[arg1] = memory[arg2] * memory[arg3]; break;
108
+    case SOU:
109
+        printf("SOU @%d = @%d[%d] - @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
110
+        memory[arg1] = memory[arg2] - memory[arg3]; break;
111
+    case DIV:
112
+        printf("DIV @%d = @%d[%d] / @%d[%d]\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
113
+        memory[arg1] = memory[arg2] / memory[arg3]; break;
114
+    case COP:
115
+        printf("COP @%d = @%d[%d]\n", arg1, arg2, memory[arg2  + EBP]);
116
+        memory[arg1 + EBP] = memory[arg2]; break;
117
+    case AFC:
118
+        printf("AFC @%d = %d\n", arg1, arg2);
119
+        memory[arg1] = arg2; break;
120
+    case JMP:
121
+        printf("JMP to %d\n", arg1);
122
+        next_ip = arg1; break;
123
+    case JMF:
124
+        printf("JMF cond@%d[%d] to %d\n", arg1, memory[arg1 + EBP], arg2);
125
+        if (memory[arg1 + EBP] == 0) next_ip = arg2;
126
+        break;
127
+    case LT:
128
+        printf("LT @%d = @%d[%d] < @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
129
+        memory[arg1] = memory[arg2] < memory[arg3] ? 1 : 0;
130
+        break;
131
+    case GT:
132
+        printf("GT @%d = @%d[%d] > @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
133
+        memory[arg1] = memory[arg2] > memory[arg3] ? 1 : 0;
134
+        break;
135
+    case LTE:
136
+        printf("LTE @%d = @%d[%d] <= @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
137
+        memory[arg1] = memory[arg2] <= memory[arg3] ? 1 : 0;
138
+        break;
139
+    case GTE:
140
+        printf("GTE @%d = @%d[%d] >= @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
141
+        memory[arg1] = memory[arg2] >= memory[arg3] ? 1 : 0;
142
+        break;
143
+    case EQ:
144
+        printf("EQ @%d = @%d[%d] == @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
145
+        memory[arg1] = memory[arg2] == memory[arg3] ? 1 : 0;
146
+        break;
147
+    case NEQ:
148
+        printf("NEQ @%d = @%d[%d] != @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
149
+        memory[arg1] = memory[arg2] != memory[arg3] ? 1 : 0;
150
+        break;
151
+    case PRI:
152
+        printf("PRI @%d[%d]\n", arg1, memory[arg1]);
153
+        break;
154
+    case AND:
155
+        printf("AND @%d = @%d[%d] && @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
156
+        memory[arg1] = memory[arg2] && memory[arg3] ? 1 : 0;
157
+        break;
158
+    case OR:
159
+        printf("OR @%d = @%d[%d] || @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2], arg3, memory[arg3]);
160
+        memory[arg1] = memory[arg2] || memory[arg3] ? 1 : 0;
161
+        break;
162
+    case NOT:
163
+        printf("OR @%d = ! @%d[%d] ? 1 : 0\n", arg1, arg2, memory[arg2]);
164
+        memory[arg1] = ! memory[arg2] ? 1 : 0;
165
+        break;
166
+    case LEA:
167
+        printf("LEA @%d = %d\n", arg1, arg2 + EBP);
168
+        memory[arg1 + EBP] = arg2 + EBP;
169
+        break;
170
+    case COP_LD:
171
+        printf("COP_LD @%d = @%d[%d]\n", arg1, arg2, memory[arg2]);
172
+        memory[arg1] = memory[memory[arg2]];
173
+        break;
174
+    case COP_STR:
175
+        printf("COP_STR @%d = @%d[%d]\n", arg1, arg2, memory[arg2]);
176
+        memory[memory[arg1]] = memory[arg2];
177
+        break;
178
+    case RET:
179
+        printf("RET @%d[%d] \n", arg1, memory[arg1 + EBP]);
180
+        if (EBP != 0){
181
+            next_ip = memory[EBP - 2];
182
+            EBP = memory[EBP - 1];
183
+        }
184
+        break;
185
+    case CALL:
186
+        memory[EBP + arg2 - 1] = EBP;
187
+        memory[EBP + arg2 - 2] = next_ip;
188
+        next_ip = arg1;
189
+        EBP += arg2;
190
+        break;
191
+    default:
192
+        fprintf(stderr, "ERROR run : unknown inst.\n");
193
+    }
194
+
195
+    return next_ip;
196
+}

+ 38
- 0
interpreter/src/instructions.h View File

@@ -0,0 +1,38 @@
1
+#ifndef __INSTRUCTIONS_H__
2
+#define __INSTRUCTIONS_H__
3
+
4
+#define ADD 1
5
+#define MUL 2
6
+#define SOU 3
7
+#define DIV 4
8
+#define COP 5
9
+#define AFC 6
10
+#define JMP 7
11
+#define JMF 8
12
+#define LT 9
13
+#define GT 10
14
+#define EQ 11
15
+#define PRI 12
16
+#define LTE 13
17
+#define GTE 14
18
+#define NEQ 15
19
+#define AND 16
20
+#define OR 17
21
+#define NOT 18
22
+#define LEA 19
23
+#define COP_LD 20
24
+#define COP_STR 21
25
+#define CALL 22
26
+#define RET 23
27
+
28
+
29
+#define MAX_INSTRUCTIONS_SIZE 256
30
+#define MAX_MEMORY_SIZE 256
31
+
32
+void asm_init();
33
+void asm_add_3(char ins, int arg1, int arg2, int arg3);
34
+void asm_add_2(char ins, int arg1, int arg2);
35
+void asm_add_1(char ins, int arg1);
36
+void asm_run();
37
+
38
+#endif // #ifndef __INSTRUCTIONS_H__

+ 53
- 0
interpreter/src/interpreter.l View File

@@ -0,0 +1,53 @@
1
+%{
2
+    #include "y.tab.h"
3
+%}
4
+
5
+vSEP    [ \t\r\n]
6
+
7
+%%
8
+
9
+ADD {return tADD;}
10
+MUL {return tMUL;}
11
+SOU {return tSOU;}
12
+DIV {return tDIV;}
13
+COP {return tCOP;}
14
+AFC {return tAFC;}
15
+JMP {return tJMP;}
16
+JMF {return tJMF;}
17
+LT {return tLT;}
18
+LTE {return tLTE;}
19
+GT {return tGT;}
20
+GTE {return tGTE;}
21
+EQ {return tEQ;}
22
+NEQ {return tNEQ;}
23
+PRI {return tPRI;}
24
+AND {return tAND;}
25
+OR {return tOR;}
26
+NOT {return tNOT;}
27
+RET {return tRET;}
28
+LEA {return tLEA;}
29
+COP_LD {return tCOP_LD;}
30
+COP_STR {return tCOP_STR;}
31
+CALL {return tCALL;}
32
+
33
+"[" {return tOB;}
34
+"]" {return tCB;}
35
+
36
+
37
+-?[0-9]+  {
38
+        yylval.nb = atoi(yytext);
39
+        return tNB;
40
+        }
41
+
42
+
43
+{vSEP}  {}
44
+
45
+.       {
46
+        fprintf(stderr, "ERROR lex : Unknown pattern %s", yytext);
47
+        exit(1);
48
+        }
49
+
50
+%%
51
+
52
+int yywrap(void) { return 1; }
53
+//int main(int argc, char *argv[]) { while (yylex()!=0) ; return 0; }

+ 97
- 0
interpreter/src/interpreter.y View File

@@ -0,0 +1,97 @@
1
+%{
2
+    #include <stdio.h>
3
+    #include "instructions.h"
4
+    int yylex();
5
+    void yyerror(char*);
6
+    int yydebug = 1;
7
+    extern int yylineno;
8
+%}
9
+
10
+/* Union for yylval */
11
+%union {
12
+    int nb;
13
+}
14
+
15
+%token tADD tMUL tSOU tDIV tCOP tAFC tJMP tJMF tPRI tRET tCALL
16
+%token tLT tLTE tGT tGTE tEQ tNEQ tAND tOR tNOT
17
+%token tLEA tCOP_LD tCOP_STR
18
+%token tOB tCB
19
+%token <nb> tNB
20
+
21
+%%
22
+
23
+%start File;
24
+
25
+File:
26
+    Instructions;
27
+
28
+Instructions:
29
+    /* epsilon */
30
+    | Instructions Instruction
31
+    ;
32
+
33
+Instruction:
34
+    tADD tNB tNB tNB
35
+        {asm_add_3(ADD, $2, $3, $4);}
36
+    | tMUL tNB tNB tNB
37
+        {asm_add_3(MUL, $2, $3, $4);}
38
+    | tSOU tNB tNB tNB
39
+        {asm_add_3(SOU, $2, $3, $4);}
40
+    | tDIV tNB tNB tNB
41
+        {asm_add_3(DIV, $2, $3, $4);}
42
+    | tCOP tNB tNB
43
+        {asm_add_2(COP, $2, $3);}
44
+    | tAFC tNB tNB
45
+        {asm_add_2(AFC, $2, $3);}
46
+    | tJMP tNB
47
+        {asm_add_1(JMP, $2);}
48
+    | tJMF tNB tNB
49
+        {asm_add_2(JMF, $2, $3);}
50
+    | tLT tNB tNB tNB
51
+        {asm_add_3(LT, $2, $3, $4);}
52
+    | tGT tNB tNB tNB
53
+        {asm_add_3(GT, $2, $3, $4);}
54
+    | tLTE tNB tNB tNB
55
+        {asm_add_3(LTE, $2, $3, $4);}
56
+    | tGTE tNB tNB tNB
57
+    	{asm_add_3(GTE, $2, $3, $4);}
58
+    | tAND tNB tNB tNB
59
+        {asm_add_3(AND, $2, $3, $4);}
60
+    | tOR tNB tNB tNB
61
+    	{asm_add_3(OR, $2, $3, $4);}
62
+    | tEQ tNB tNB tNB
63
+        {asm_add_3(EQ, $2, $3, $4);}
64
+    | tNEQ tNB tNB tNB
65
+        {asm_add_3(NEQ, $2, $3, $4);}
66
+    | tNOT tNB tNB
67
+        {asm_add_2(NOT, $2, $3);}
68
+    | tPRI tNB
69
+        {asm_add_1(PRI, $2);}
70
+    | tLEA tNB tNB
71
+        {asm_add_2(LEA, $2, $3);}
72
+    | tCOP_LD tNB tOB tNB tCB
73
+    	{asm_add_2(COP_LD, $2, $4);}
74
+    | tCOP_STR tOB tNB tCB tNB
75
+        {asm_add_2(COP_STR, $3, $5);}
76
+    | tRET tNB
77
+        {asm_add_1(RET, $2);}
78
+    | tCALL tNB tNB
79
+        {asm_add_2(CALL, $2, $3);}
80
+    ;
81
+
82
+
83
+%%
84
+
85
+void yyerror(char* str) {
86
+    extern int yylineno;
87
+    fprintf(stderr, "ERROR yyparse : Line %d: %s\n", yylineno, str);
88
+}
89
+
90
+int main(int argc, char *argv[]) {
91
+    asm_init();
92
+    yyparse();
93
+    printf("INFO yyparse : Parsing End\n");
94
+    asm_run();
95
+    return 0;
96
+}
97
+

+ 0
- 1958
lex.yy.c
File diff suppressed because it is too large
View File


+ 8
- 5
script.sh View File

@@ -2,17 +2,20 @@ bison -d -t analyse_syntaxique.y -v
2 2
 flex analyse_lexicale.lex 
3 3
 gcc -w *.c -ly
4 4
 echo "
5
-    int fonction1(int a){
6
-      int b = 4;
5
+    int fonction1(int * a){
6
+      int b = *a;
7
+      printf(b);
7 8
       return 1;
8 9
     }
9 10
 
10 11
     int main(){
11
-      int c = 2;
12
-      int * p;
12
+      int l = 21;
13
+      int * p = &l;
14
+      int c = fonction1(p);
15
+      printf(c);
13 16
       p = &c;
14 17
       *p = 2;
15
-      int b = *p;
18
+      printf(c);
16 19
 
17 20
       return 0;
18 21
     }

+ 0
- 1
table_symboles.c View File

@@ -82,7 +82,6 @@ void free_temp(Table_Symboles * table){
82 82
 int prepare_function_call(Table_Symboles * table){
83 83
     prepare_argument_push(table);
84 84
     prepare_argument_push(table);
85
-    prepare_argument_push(table);
86 85
 }
87 86
 
88 87
 int prepare_argument_push(Table_Symboles * table){

+ 1
- 1
table_symboles.h View File

@@ -1,7 +1,7 @@
1 1
 #ifndef TABLE_SYMBOLES_H
2 2
 #define TABLE_SYMBOLES_H
3 3
 
4
-#define TABLE_SIZE 50
4
+#define TABLE_SIZE 256
5 5
 #define VARIABLE_SIZE 30
6 6
 
7 7
 enum Symbole_Type {TYPE_INT , TYPE_CONST_INT, TYPE_INT_PTR};

+ 16
- 0
test_file 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;

BIN
xilinx/.DS_Store View File


Loading…
Cancel
Save