Browse Source

Début génération assembleur

Faure Paul 8 months ago
parent
commit
eeba39940b

BIN
Documentation/Projet_Syst_Info_2021.pdf View File


+ 52
- 0
Documentation/grammaire_c_minimaliste View File

@@ -0,0 +1,52 @@
1
+les fonctions (avec parametres)
2
+le if
3
+le while
4
+les declarations
5
+les affectations
6
+les operations arith.
7
+le retour de fonction
8
+l'invocation de fonctions
9
+
10
+
11
+C : Fonctions ;
12
+Fonctions : Fonction Fonctions | Fonction ;
13
+
14
+Fonction : tInt tID tPO Params tPF Body ;
15
+
16
+Params : | Param SuiteParams ;
17
+Param : tInt tID ;
18
+
19
+SuiteParams : tVirgule Param SuiteParams | ;
20
+
21
+// Ps : P Ps | ;
22
+// P : tInt tID tVirgule
23
+// Ps =>* tInt tID tVirgule tInt tID tVirgule
24
+// Ps => P Ps => P P Ps ...
25
+
26
+Body : tAO Instructions tAF ;
27
+
28
+Instructions : Instruction Instructions | ;
29
+
30
+Instruction : Aff | If | While | Return | Decl | Invocation tPV ;
31
+
32
+Aff : tID tEQ E tPV ;
33
+
34
+E : tNB | tID | E tADD E | E tMUL E | E tMINUS E | E tDIV E | Invocation | tPO E tPF | tMINUS E ;
35
+
36
+// E : tID tADD tID | ...
37
+
38
+If : tIF tPO Cond tPF Body ;
39
+
40
+Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
41
+
42
+Invocation : tID tPO  Args  tPF ;
43
+
44
+Args : .... cf params
45
+
46
+Return : tRET E tPV ;
47
+
48
+
49
+
50
+
51
+
52
+

+ 15
- 0
Fichiers_Tests/progC View File

@@ -0,0 +1,15 @@
1
+int main(int x, int i){
2
+    const int a = 4; 
3
+    const int a, b, c = 2 + a - 5 * (7 / 8) + 5; 
4
+    printf(coucou);
5
+    int y = 7e8;
6
+    int res_2 = x + y;
7
+    if ( (a == 2) && b || c > (7*8)) {
8
+    }
9
+    else if (a) {
10
+        int x = 90;
11
+    } else {
12
+        int a = b;
13
+    }
14
+    printf(i);
15
+}

BIN
Lex_Yacc/a.out View File


+ 43
- 0
Lex_Yacc/al.lex View File

@@ -0,0 +1,43 @@
1
+%{
2
+#include "as.tab.h"
3
+int yywrap(void){return 1;}
4
+%}
5
+
6
+%%
7
+
8
+"main"      { return tMAIN ;} 
9
+"{"         { return tOBRACKET;}
10
+"}"         { return tCBRACKET; }
11
+"("			{ return tOBRACE; }
12
+")"			{ return tCBRACE; }
13
+"const"     { return tCONST; }
14
+"int"       { return tINT; }
15
+"printf"    { return tPRINTF; } //Degeu mais à degager
16
+"if"        { return tIF; }
17
+"while"     { return tWHILE; }
18
+"<"         { return tLT; }
19
+">"         { return tGT; }
20
+"=="        { return tEQCOND; }
21
+"&&"        { return tAND; }
22
+"||"        { return tOR; }
23
+"else"      { return tELSE;}
24
+
25
+
26
+[0-9]+	{ yylval.nombre = atoi(yytext); return tNB; }
27
+[0-9]+e[0-9]+	{ yylval.nombre = -1; return tNBEXP; } //Renvoyer le token tNB et pas tNBEXP
28
+"+"			{ return tADD; }
29
+"-"			{ return tSUB; }
30
+"*"         { return tMUL; }
31
+"/"         { return tDIV; }
32
+"="         { return tEQ; }
33
+";"			{ return tPV; }
34
+" "			{} //Ne pas les retourner à Yacc
35
+"   "       {} //Ne pas les retourner à Yacc
36
+","         { return tCOMA; }
37
+"\n"        {} //Ne pas les retourner à Yacc
38
+[a-zA-Z][a-zA-Z0-9_]* { strcpy(yylval.id, yytext); return tID; }
39
+.				{ return tERROR; }
40
+
41
+%%
42
+
43
+

+ 1290
- 0
Lex_Yacc/as.output
File diff suppressed because it is too large
View File


+ 1714
- 0
Lex_Yacc/as.tab.c
File diff suppressed because it is too large
View File


+ 102
- 0
Lex_Yacc/as.tab.h View File

@@ -0,0 +1,102 @@
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_AS_TAB_H_INCLUDED
34
+# define YY_YY_AS_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
+    tMAIN = 258,
49
+    tOBRACKET = 259,
50
+    tCBRACKET = 260,
51
+    tOBRACE = 261,
52
+    tCBRACE = 262,
53
+    tINT = 263,
54
+    tCONST = 264,
55
+    tPV = 265,
56
+    tCOMA = 266,
57
+    tMUL = 267,
58
+    tDIV = 268,
59
+    tADD = 269,
60
+    tSUB = 270,
61
+    tEQ = 271,
62
+    tNB = 272,
63
+    tNBEXP = 273,
64
+    tID = 274,
65
+    tPRINTF = 275,
66
+    tERROR = 276,
67
+    tIF = 277,
68
+    tWHILE = 278,
69
+    tELSE = 279,
70
+    tLT = 280,
71
+    tGT = 281,
72
+    tEQCOND = 282,
73
+    tAND = 283,
74
+    tOR = 284,
75
+    tNOT = 285
76
+  };
77
+#endif
78
+
79
+/* Value type.  */
80
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
81
+
82
+union YYSTYPE
83
+{
84
+#line 1 "as.y" /* yacc.c:1909  */
85
+
86
+	int nombre;
87
+    char id[30];
88
+
89
+#line 90 "as.tab.h" /* yacc.c:1909  */
90
+};
91
+
92
+typedef union YYSTYPE YYSTYPE;
93
+# define YYSTYPE_IS_TRIVIAL 1
94
+# define YYSTYPE_IS_DECLARED 1
95
+#endif
96
+
97
+
98
+extern YYSTYPE yylval;
99
+
100
+int yyparse (void);
101
+
102
+#endif /* !YY_YY_AS_TAB_H_INCLUDED  */

+ 159
- 0
Lex_Yacc/as.y View File

@@ -0,0 +1,159 @@
1
+%union {
2
+	int nombre;
3
+    char id[30];
4
+}
5
+%{
6
+#include "../Symboles/table_symboles.h"
7
+#include <stdio.h> 
8
+#include <string.h>
9
+#include <stdlib.h>
10
+
11
+int addr = 0;
12
+
13
+%}
14
+
15
+%token tMAIN
16
+%token tOBRACKET tCBRACKET
17
+%token tOBRACE tCBRACE
18
+%token tINT
19
+%token tCONST
20
+%token tPV tCOMA#include <string.h>
21
+%token tMUL tDIV tADD tSUB tEQ
22
+%token<nombre> tNB tNBEXP
23
+%token<id> tID
24
+%token tPRINTF
25
+%token tERROR
26
+%token tIF tWHILE tELSE
27
+%token tLT tGT tEQCOND
28
+%token tAND tOR
29
+
30
+%left tAND tOR
31
+%left tNOT
32
+%left tLT tGT
33
+%left tEQCOND
34
+%left tADD tSUB
35
+%left tMUL tDIV
36
+
37
+
38
+
39
+//%type<nombre> E
40
+
41
+/******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
42
+
43
+%%
44
+
45
+Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(pile);  printf("addr = %d\n",addr);} ; 
46
+
47
+Params : { printf("Sans Params\n"); } ;
48
+Params : Param SuiteParams ;
49
+Param : tINT tID { printf("Prametre : %s\n", $2); };
50
+SuiteParams : tCOMA Param SuiteParams ;
51
+SuiteParams : ;
52
+
53
+Body : tOBRACKET Instructions tCBRACKET { struct symbole_t symbole = {"Salut", 0x77b58af, INT, 1}; push(symbole, pile); } ;
54
+
55
+
56
+Instructions : Instruction Instructions ;
57
+Instructions : ;
58
+Instruction : Aff ;
59
+Instruction : Decl ;
60
+Instruction : Invocation tPV ;
61
+Instruction : If;
62
+Instruction : While;
63
+
64
+
65
+If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
66
+Else : tELSE If { printf("Else if reconnu\n"); };
67
+Else : tELSE Body { printf("Else reconnu\n"); };
68
+Else : ;
69
+While : tWHILE tOBRACE Cond tCBRACE Body { printf("While reconnu\n"); };
70
+
71
+Cond : E SuiteCond ;
72
+SuiteCond : ;
73
+SuiteCond : tAND E SuiteCond;
74
+SuiteCond : tOR E SuiteCond;
75
+
76
+
77
+Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
78
+
79
+E : tNB { printf("Nombre\n"); 
80
+ struct symbole_t symbole = ("", addr, INT, 1};
81
+ push(symbole, pile); 
82
+$$=addr;
83
+  addr++;
84
+ printf("AFC %d %d",addr,$1); } ;
85
+
86
+E : tNBEXP { printf("Nombre exp\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("AFC %d %d",addr,$1); };
87
+E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
88
+E : E tMUL E { printf("Mul\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("MUL %d %d %d",addr, $1,$2);};
89
+E : E tDIV E { printf("Div\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("DIV %d %d %d",addr, $1,$2);};
90
+E : E tSUB E { printf("Sub\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("SOU %d %d %d",addr, $1,$2);};
91
+E : E tADD E { printf("Add\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("ADD %d %d %d",addr, $1,$2);}};
92
+E : Invocation { printf("Invoc\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("AFC %d %d",addr, $1);};
93
+E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2};
94
+E : tSUB E { printf("Moins\n"); printf("SUB %d 0 %d",addr,$2);};
95
+E : E tEQCOND E { printf("==\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("EQU %d %d %d",addr, $1,$3);};
96
+E : E tGT E { printf(">\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("SUP %d %d %d",addr, $1,$3);};
97
+E : E tLT E { printf("<\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr;  addr++; printf("SUP %d %d %d",addr, $1,$3);};
98
+E : tNOT E { printf("!\n"); };
99
+
100
+
101
+
102
+//Créer un champ isConst dans la table des symboles
103
+Decl : tCONST tINT tID SuiteDeclConst { int init = ($3 != -1); 
104
+ if (init){
105
+    int val = *$2;
106
+    printf("AFC %ld %d",addr,val);
107
+ }
108
+struct symbole_t symbole = {$2, addr, INT, init};
109
+push(symbole, pile); 
110
+addr++;} ;
111
+SuiteDeclConst : tCOMA tID SuiteDecl { $$=$3; int init = ($3 != -1); 
112
+ if (init){
113
+    int val = *$2;
114
+    printf("AFC %ld %d",addr,val);
115
+ }
116
+struct symbole_t symbole = {$2, addr, INT, init}; push(symbole, pile); addr++;};
117
+SuiteDeclConst : tEQ E tPV { $$=$2; };
118
+SuiteDeclConst : tPV { $$=$2; };
119
+
120
+Decl : tINT tID SuiteDecl { int init = ($3 != -1);
121
+ if (init){
122
+    int val = *$2;
123
+    printf("AFC %ld %d",addr,val);
124
+ }
125
+ struct symbole_t symbole = {$2, addr, INT, init}; 
126
+push(symbole, pile); 
127
+addr++;} ;
128
+SuiteDecl : tCOMA tID SuiteDecl
129
+ { $$=$3; 
130
+ int init = ($3 != -1);
131
+ if (init){
132
+    int val = *$2;
133
+    printf("AFC %ld %d",addr,val);
134
+ }
135
+ struct symbole_t symbole = {$2, addr, INT, init};
136
+ push(symbole, pile); 
137
+ addr++;};
138
+SuiteDecl : tEQ E tPV { $$=$2;};
139
+SuiteDecl : tPV { $$=$2; };
140
+
141
+Invocation : tPRINTF tOBRACE  tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
142
+
143
+/*S : E tPV
144
+						{ printf("RES: %d\n", $1); }
145
+		S
146
+	|					{ printf("END\n"); }
147
+	;
148
+
149
+E : E tADD E	{ $$ = $1 + $3; }
150
+	| E tSUB E	{ $$ = $1 - $3; }
151
+	| tOB E tCB	{ $$ = $2; }
152
+	| tNB				{ $$ = $1; }
153
+	;*/
154
+
155
+%%
156
+void main(void) {
157
+    init();
158
+	yyparse();
159
+}

+ 5
- 0
Lex_Yacc/comp.sh View File

@@ -0,0 +1,5 @@
1
+bison -d -t as.y -v
2
+flex al.lex 
3
+gcc *.c ../Symboles/table_symboles.c -ly
4
+cat ../Fichiers_Tests/progC | ./a.out 
5
+

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


+ 118
- 0
Symboles/table_symboles.c View File

@@ -0,0 +1,118 @@
1
+/* TABLE DES SYMBOLE DU COMPILATEUR (PILE)
2
+
3
+-----------------------------------------------------
4
+|  symbole   |  adresse   |    type    | initialisé |
5
+-----------------------------------------------------
6
+|            |            |            |            |
7
+|            |            |            |            |
8
+|            |            |            |            |
9
+|      i     | 0x777756b8 |     int    |    false   |
10
+|    size    | 0x777756b8 |     int    |    true    |
11
+-----------------------------------------------------
12
+
13
+Types pour l'implémentation : 
14
+	- enum type_t : [int]
15
+	- struct symbole : {
16
+			char nom[30];
17
+			uintptr_t adresse;
18
+			enum type_t type;
19
+			char initialized;
20
+		}
21
+
22
+Opérations possible : 
23
+	- init -> pile * -> void
24
+	- push -> symbole -> pile * -> void
25
+	- pop -> pile * -> symbole
26
+	- exist -> pile * -> symbole -> char
27
+	- initialized -> pile * -> symbole -> char					*/
28
+
29
+#include "table_symboles.h"
30
+#include <stdlib.h>
31
+#include <string.h>
32
+#include <stdio.h>
33
+
34
+struct element_t {
35
+	struct symbole_t symbole;
36
+	struct element_t * suivant;
37
+};
38
+
39
+struct pile_t {
40
+	int taille;
41
+	struct element_t * first;
42
+};
43
+
44
+char * type_to_string(enum type_t type) {
45
+	if (type == INT) {
46
+		return "int";
47
+	} else {
48
+		return "unknown";	
49
+	}
50
+}
51
+
52
+void print_symbole(struct symbole_t symbole) {
53
+    if (symbole.initialized) {
54
+		printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type));
55
+	} else {
56
+		printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON}\n", symbole.nom, (void *)(symbole.adresse), type_to_string(symbole.type));
57
+	}
58
+}
59
+
60
+void init (void) {
61
+    pile = malloc(sizeof(struct pile_t));
62
+	pile->first = NULL;
63
+	pile->taille = 0;
64
+}
65
+
66
+void push(struct symbole_t symbole, struct pile_t * pile) {
67
+	struct element_t * aux = malloc(sizeof(struct element_t));
68
+	aux->symbole = symbole;
69
+	aux->suivant = pile->first;
70
+	pile->first = aux;
71
+	pile->taille++;
72
+}
73
+
74
+struct symbole_t pop(struct pile_t * pile) {
75
+	struct symbole_t retour = {"", 0, UNKNOWN, 0};
76
+	struct element_t * aux;
77
+	if (pile->taille > 0) {
78
+		aux = pile->first;
79
+		pile->first = pile->first->suivant;
80
+		retour = aux->symbole;
81
+		free(aux);
82
+		pile->taille--;
83
+	}
84
+	return retour;
85
+}
86
+		
87
+char status(char * nom, struct pile_t * pile) {
88
+	char retour = 0;
89
+	struct element_t * aux = pile->first;
90
+	int i;
91
+	for (i=0; i < pile->taille; i++) {
92
+		if (!strcmp(nom, aux->symbole.nom)) {
93
+			if (aux->symbole.initialized) {
94
+				retour = 1;
95
+			} else {
96
+				retour = 2;
97
+			}
98
+			break;
99
+		} else {
100
+			aux = aux->suivant;
101
+		}
102
+	}
103
+	return retour;
104
+}
105
+
106
+void print(struct pile_t * pile) {
107
+	printf("Affichage de la Table des Symboles\n\tSize : %d\n\tContenu : \n", pile->taille);
108
+	struct element_t * aux = pile->first;
109
+	int i;
110
+	for (i=0; i < pile->taille; i++) {
111
+		if (aux->symbole.initialized) {
112
+			printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:OUI}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
113
+		} else {
114
+			printf("\t\t{nom:%s, adresse:%p, type:%s, initialized:NON}\n", aux->symbole.nom, (void *)(aux->symbole.adresse), type_to_string(aux->symbole.type));
115
+		}
116
+		aux = aux->suivant;
117
+	}
118
+}

+ 48
- 0
Symboles/table_symboles.h View File

@@ -0,0 +1,48 @@
1
+/* TABLE DES SYMBOLE DU COMPILATEUR (PILE)
2
+
3
+-----------------------------------------------------
4
+|  symbole   |  adresse   |    type    | initialisé |
5
+-----------------------------------------------------
6
+|            |            |            |            |
7
+|            |            |            |            |
8
+|            |            |            |            |
9
+|      i     | 0x777756b8 |     int    |    false   |
10
+|    size    | 0x777756b8 |     int    |    true    |
11
+-----------------------------------------------------
12
+
13
+Types pour l'implémentation : 
14
+	- enum type_t : [int]
15
+	- struct symbole : {
16
+			char nom[30];
17
+			uintptr_t adresse;
18
+			enum type_t type;
19
+			char initialized;
20
+		}
21
+
22
+Opérations possible : 
23
+	- init -> pile * -> void
24
+	- push -> symbole -> pile * -> void
25
+	- pop -> pile * -> symbole
26
+	- status -> nom -> pile -> char					*/
27
+
28
+#include <stdint.h>
29
+
30
+enum type_t {UNKNOWN, INT};
31
+
32
+struct symbole_t {
33
+	char nom[30];
34
+	uintptr_t adresse;
35
+	enum type_t type;
36
+	char initialized;
37
+};
38
+
39
+void print_symbole(struct symbole_t symbole);
40
+
41
+struct pile_t * pile;
42
+
43
+void init(void);
44
+void push(struct symbole_t symbole, struct pile_t * pile);
45
+struct symbole_t pop(struct pile_t * pile);
46
+// renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
47
+char status(char * nom, struct pile_t * pile);
48
+void print(struct pile_t * pile);

BIN
Symboles/table_symboles.o View File


BIN
Symboles/test_table_symbole View File


+ 32
- 0
Symboles/test_table_symbole.c View File

@@ -0,0 +1,32 @@
1
+#include "table_symboles.h"
2
+#include <stdio.h>
3
+
4
+int main() {
5
+    printf("Procedure de test de la Table des Symboles\n");
6
+    init();
7
+    printf("Affichage de la Table des Symboles (vide)\n");
8
+    print(pile);
9
+
10
+    printf("Test de la fonction push :\n");
11
+    struct symbole_t symbole = {"Salut", 0x77b58af, INT, 1};
12
+    push(symbole, pile);
13
+    printf("Affichage de la Table des Symboles (1 élément : Salut)\n");
14
+    print(pile);
15
+    struct symbole_t symbole2 = {"Coucou", 0x77b54af, UNKNOWN, 0};
16
+    push(symbole2, pile);
17
+    printf("Affichage de la Table des Symboles (2 élément : Salut, Coucou)\n");
18
+    print(pile);
19
+
20
+    printf("Test de la fonction status :\n\tStatus de Salut (1 expected) : %d\n\tStatus de Coucou (2 expected) : %d\n\tStatus de Truc (0 expected) : %d\n", (int)status("Salut",pile), (int)status("Coucou",pile), (int)status("Truc",pile));
21
+    
22
+    printf("Test de la fonction pop :\n");
23
+    printf("Symbole expected Coucou\n\t");
24
+    print_symbole(pop(pile));
25
+    print(pile);
26
+    printf("Symbole expected Salut\n\t");
27
+    print_symbole(pop(pile));
28
+    print(pile);
29
+    printf("Symbole expected Aucun\n\t");
30
+    print_symbole(pop(pile));
31
+    print(pile);
32
+}

BIN
Symboles/test_table_symbole.o View File


Loading…
Cancel
Save