Browse Source

Avancées sur les fonctions

Elies Tali 2 years ago
parent
commit
fffd6020f5

+ 10
- 2
Documentation/transcription_opcodes.md View File

@@ -30,6 +30,10 @@ Copie la valeur val à l'adresse @X
30 30
 
31 31
 Copie la valeur contenue dans @C à l'adresse @X en considérant que ce qui est copié est une adresse (et donc il faut ajouter BP)
32 32
 
33
+## AFCA @X addr
34
+
35
+Copie la valeur addr à l'adresse @X en considérant que ce qui est copié est une adresse (et donc il faut ajouter BP)
36
+
33 37
 ## JMP lig
34 38
 
35 39
 Saute vers la ligne lig dans le code sans condition
@@ -52,9 +56,13 @@ Met à l'adresse @X 1 si val en @A égale à celle en @B et 0 sinon
52 56
 
53 57
 ## READ @X @Y
54 58
 
55
-Va mettre à l'adresse @X ce qui est à l'addresse contenue à l'adresse @Y (on considère que ce qui est dans @Y est un adresse et on va voir à cette adresse)
59
+Va mettre à l'adresse @X ce qui est à l'addresse contenue à l'adresse @Y (on considère que ce qui est dans @Y est un adresse et on va voir à cette adresse). Attention, considérer des addresses globales (pas relatives).
56 60
 
57 61
 ## WR @X @Y
58 62
 
59
-Va mettre le contenue dans @Y dans l'adresse qui est la valeur dans @X (on considère que @X est un pointeur et on écrit dans l'adresse qui est contenue dans @X)
63
+Va mettre le contenue dans @Y dans l'adresse qui est la valeur dans @X (on considère que @X est un pointeur et on écrit dans l'adresse qui est contenue dans @X). Attention, considérer des addresses globales (pas relatives).
64
+
65
+##CALL lig taille
66
+
67
+Appelle la fonction dont la première ligne est lig est dont la taille des arguments est en @Y (afin de pouvoir bouger ebp).
60 68
 

+ 8
- 7
Fichiers_Tests/progC View File

@@ -1,9 +1,10 @@
1
+
2
+int fonction(int * p){ 
3
+	int b =  *p + 2;
4
+}
5
+
1 6
 int main(){
2
-	int b[3];
3
-	int * c = &b[2];
4
-	int * f = &b[1];
5
-	if(1){
6
-		int v;
7
-	}
8
-	int y;
7
+	int a = 255;
8
+	fonction(&a);
9 9
 }
10
+

+ 52
- 26
Lex_Yacc/as.y View File

@@ -3,15 +3,16 @@
3 3
     char id[30];
4 4
 }
5 5
 %{
6
+#include "../Tables/Fonctions/tab_fonctions.h"
6 7
 #include "../Tables/Symboles/table_symboles.h"
7 8
 #include <stdio.h> 
8 9
 #include <string.h>
9 10
 #include <stdlib.h>
10 11
 #include "../Tables/Instructions/tab_instruc.h"
11
-#include "../Tables/Fonctions/tab_fonctions.h"
12 12
 #define TAILLE 1024
13 13
 
14 14
 struct type_t type_courant;
15
+struct type_t return_type_fonc;
15 16
 
16 17
 int instructions_ligne_to_patch[10][20];
17 18
 int nbs_instructions_to_patch[10];
@@ -42,7 +43,7 @@ int nbs_instructions_to_patch[10];
42 43
 %left tADD tSUB
43 44
 %left tMUL tDIV
44 45
 
45
-%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis ETer
46
+%type<nombre> E DebutAff SuiteAffPointeur DebutAffPointeur EBis Invocation Args ArgSuite Arg SuiteParams Params
46 47
 
47 48
 
48 49
 
@@ -52,20 +53,24 @@ int nbs_instructions_to_patch[10];
52 53
 
53 54
 %%
54 55
 
55
-Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ; 
56 56
 
57
-Params : { printf("Sans Params\n"); } ;
58
-Params : Param SuiteParams ;
59
-Param : Type tID { printf("Parametre : %s\n", $2); };
60
-SuiteParams : tCOMA Param SuiteParams ;
61
-SuiteParams : ;
62 57
 
58
+C : Fonction Fonctions;
59
+
60
+Fonctions : Fonction Fonctions;
61
+Fonctions : ;
62
+
63
+Main : tINT {printf("Déclaration du main\n");} tMAIN tOBRACE Args tCBRACE Body { print(); create_asm();} ; 
64
+
65
+Fonction : Type tID {return_type_fonc = type_courant; printf("Déclaration de la fonction  %s\n", $2);} tOBRACE {inc_prof();} Args {decrement_prof(); push_fonction($2,return_type_fonc,get_current_index(), $6);} tCBRACE Body { print_fonctions();} ;
66
+Fonction : Main {print_fonctions();};
63 67
 
64
-Args : Arg ArgSuite;
65
-Args : ;
66
-Arg : Type tID {int addr = push($2,1, type_courant);};
67
-ArgSuite : tCOMA Arg ArgSuite {} ;
68
-ArgSuite : ; 
68
+Args : Arg ArgSuite {$$ = $1 + $2; printf("Les arguments de la fonctions vont avoir une taille dans la pile de : %d\n",$$);};
69
+Args : {$$ = 0;};
70
+Arg : Type tID {int addr = push($2,1, type_courant); if (type_courant.pointeur_level > 0){$$ = taille_types[INT];} else{$$ = taille_types[type_courant.base];}};
71
+Arg : Type tID tOCROCH tCCROCH {int addr = push($2,1, type_courant); $$ = taille_types[INT];};
72
+ArgSuite : tCOMA Arg ArgSuite {$$ = $2 + $3;} ;
73
+ArgSuite : {$$ = 0;}; 
69 74
 
70 75
 
71 76
 Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
@@ -75,10 +80,24 @@ Instructions : Instruction Instructions ;
75 80
 Instructions : ;
76 81
 Instruction : Aff {};
77 82
 Instruction : Decl {};
78
-//Instruction : Invocation tPV{};
83
+Instruction : Invocation tPV{};
79 84
 Instruction : If {};
80 85
 Instruction : While {};
81 86
 
87
+
88
+Invocation : tID tOBRACE Params tCBRACE {multiple_pop($3); struct fonction_t fonc = get_fonction($1); add_operation(CALL,fonc.first_instruction_line, fonc.taille_args,0);};
89
+
90
+
91
+//Pour les tableaux quand on les passe en arguments, il faudra faire gaffe à bien les passer comme un pointeur vers un tableau et pas juste un tableau car sinon in ne pourra pas accéder au tableau de base depuis la fonction appellée. Il faut pour cela peut être rajouter un nouveau champ (isArg) dans la table des symboles (ou autre idée?)
92
+
93
+Params : {$$ = 0; printf("Sans Params\n"); } ;
94
+Params : Param SuiteParams {$$ = $2 + 1;};
95
+Param : E {printf("Parametre : %d\n", $1);};
96
+SuiteParams : tCOMA Param SuiteParams {$$ = $3 + 1;};
97
+SuiteParams : {$$ = 0;};
98
+
99
+
100
+
82 101
 //On considère que la première ligne du code en ASM est la ligne 0
83 102
 If : tIF tOBRACE E tCBRACE {
84 103
 add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;} 
@@ -131,7 +150,7 @@ E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
131 150
 E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
132 151
 E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
133 152
 E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
134
-//E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
153
+E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
135 154
 E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
136 155
 E : tSUB E { printf("Moins\n");  int addr = push("0_TEMPORARY", 1, integer);  add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr);  $$ = $2; pop();};
137 156
 E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
@@ -141,21 +160,32 @@ E : tNOT E { printf("!\n"); };
141 160
 E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
142 161
 E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
143 162
 E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
144
-E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;};
145
-E : tADDR ETer {add_operation(COPA,$2, $2,0); $$=$2;};
146 163
 E : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if (symbole->type.isTab){add_operation(AFCA, addr,symbole->adresse,0); } else{add_operation(COP, addr,symbole->adresse,0);} $$=addr;};
147
-E : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.pointeur_level > 0) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
164
+E : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.pointeur_level > 0) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3);
165
+ add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
166
+E : tADDR EBis {add_operation(COPA,$2, $2,0); $$=$2;};
148 167
 
149
-EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); if(type.pointeur_level > 0) {add_operation(COP, addr,symbole->adresse,0);} else{add_operation(AFCA, addr,symbole->adresse,0);} int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();};
150
-ETer : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); add_operation(AFCA, addr,symbole->adresse,0); $$=addr;};
168
+EBis : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); 
169
+struct type_t type = symbole->type; type.nb_blocs = 1; 
170
+int addr = push("0_TEMPORARY", 1, type); 
171
+if(type.pointeur_level > 0) {
172
+add_operation(COP, addr,symbole->adresse,0);
173
+}
174
+ else{
175
+add_operation(AFCA, addr,symbole->adresse,0);
176
+}
177
+int addr2 = push("0_TEMPORARY", 1, integer);
178
+add_operation(AFC, addr2, taille_types[symbole->type.base],0); 
179
+add_operation(MUL,$3,addr2,$3); 
180
+add_operation(ADD,$3,addr,$3); $$=$3; 
181
+pop(); pop();};
182
+EBis : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); struct type_t type = symbole->type; type.nb_blocs = 1; int addr = push("0_TEMPORARY", 1, type); add_operation(AFCA, addr,symbole->adresse,0); $$=addr;};
151 183
 
152 184
 
153 185
 
154 186
 //Créer un champ isConst dans la table des symboles
155 187
 Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.isTab = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
156 188
 Type : Type tMUL {type_courant.pointeur_level++;  printf("Type int *\n");};
157
-//SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ; 
158
-//SuiteType : ;
159 189
 
160 190
 Decl : Type SuiteDecl FinDecl ;
161 191
 Decl : tCONST Type SuiteDeclConst FinDeclConst;
@@ -169,10 +199,6 @@ FinDecl : tCOMA SuiteDecl FinDecl ;
169 199
 SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
170 200
 FinDeclConst : tPV;
171 201
 FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
172
-
173
-Fonction : Type tID {push_fonction($2,type_courant,get_current_index());} tOBRACE {} Args {} tCBRACE Body { printf("Déclaration de la fonction  %s\n", $1); } ;)
174
-
175
-
176 202
 %%
177 203
 void main(void) {
178 204
     init();

+ 14
- 4
Makefile View File

@@ -1,7 +1,7 @@
1 1
 default : 
2 2
 	@echo "Spécifiez une cible"
3 3
 
4
-clean : clean_Symboles clean_Instructions clean_Lex_Yacc
4
+clean : clean_Symboles clean_Instructions clean_Lex_Yacc clean_Fonctions
5 5
 	@rm -f rondoudou_gcc
6 6
 	@rm -f output.txt
7 7
 
@@ -13,11 +13,15 @@ clean_Instructions:
13 13
 	@rm -f Tables/Instructions/*.o
14 14
 	@rm -f Tables/Instructions/test
15 15
 
16
+clean_Fonctions:
17
+	@rm -f Tables/Fonctions/*.o
18
+	@rm -f Tables/Fonctions/test
19
+
16 20
 clean_Lex_Yacc:
17 21
 	@rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.*
18 22
 
19
-build : clean build_Symboles build_Instructions build_Lex_Yacc
20
-	gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o -ll -o rondoudou_gcc
23
+build : clean build_Symboles build_Instructions build_Lex_Yacc build_Fonctions
24
+	gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o Tables/Fonctions/tab_fonctions.o -ll -o rondoudou_gcc
21 25
 
22 26
 build_Symboles: clean_Symboles
23 27
 	gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o
@@ -25,6 +29,9 @@ build_Symboles: clean_Symboles
25 29
 build_Instructions: clean_Instructions
26 30
 	gcc -c Tables/Instructions/tab_instruc.c -o Tables/Instructions/tab_instruc.o
27 31
 
32
+build_Fonctions : clean_Fonctions
33
+	gcc -c Tables/Fonctions/tab_fonctions.c -o Tables/Fonctions/tab_fonctions.o
34
+
28 35
 build_Lex_Yacc: clean_Lex_Yacc
29 36
 	bison -d -t -b Lex_Yacc/as Lex_Yacc/as.y
30 37
 	flex -o Lex_Yacc/lex.yy.c Lex_Yacc/al.lex
@@ -56,4 +63,7 @@ edit_Instructions:
56 63
 edit_Progs: 
57 64
 	pluma Fichiers_Tests/progC &
58 65
 
59
-edit: edit_Lex_Yacc edit_Symboles edit_Instructions edit_Progs
66
+edit_Fonctions :
67
+	pluma Tables/Fonctions/tab_fonctions.c Tables/Fonctions/tab_fonctions.h
68
+
69
+edit: edit_Lex_Yacc edit_Symboles edit_Instructions edit_Progs edit_Fonctions

+ 22
- 9
Tables/Fonctions/tab_fonctions.c View File

@@ -1,13 +1,15 @@
1
+#include "tab_fonctions.h"
2
+
1 3
 struct fonction_t tab_fonctions[MAX_TAILLE_FONC];
2
-int index = 0;
4
+int indexTab = 0;
3 5
 
4 6
 
5 7
 struct fonction_t get_fonction(char * name){
6 8
 	int not_found = 1;
7 9
 	int i = 0;
8
-	struct fonction_t res = NULL;
9
-	while (not_found && (i <= index)){
10
-		if (!strcmp(name,tab_fonctions[i].name){
10
+	struct fonction_t res;
11
+	while (not_found && (i <= indexTab)){
12
+		if (!strcmp(name,tab_fonctions[i].name)){
11 13
 			res = tab_fonctions[i];
12 14
 			not_found = 0;
13 15
 		}
@@ -16,14 +18,25 @@ struct fonction_t get_fonction(char * name){
16 18
 	return res;
17 19
 }
18 20
 
19
-void push_fonction(char * name, struct type_t type, int line){
20
-	if (index < MAX_TAILLE_FONC){
21
+void push_fonction(char * name, struct type_t type, int line, int taille_args){
22
+	 if (indexTab < MAX_TAILLE_FONC){
21 23
 		struct fonction_t fonc;
24
+		fonc.name = malloc(sizeof(char)*50);
22 25
 		strcpy(fonc.name,name);
23
-		fonc.type = type;
26
+		fonc.return_type = type;
24 27
 		fonc.first_instruction_line = line;
25
-		tab_fonctions[i] = fonc;
26
-		index++;
28
+		fonc.taille_args = taille_args;
29
+		tab_fonctions[indexTab] = fonc;
30
+		indexTab++;
31
+	}
32
+}
33
+
34
+void print_fonctions(){
35
+	printf("Affichage table des fonctions\n");
36
+	printf("\t Size : %d\n",indexTab);
37
+	printf("\t Contenu : \n"); 
38
+	for (int i =0; i<indexTab; i++){
39
+		printf("\t\t{Fonction : %s returns %s and starts at line %d and its args have a size of %d}\n",tab_fonctions[i].name, type_to_string(tab_fonctions[i].return_type), tab_fonctions[i].first_instruction_line, tab_fonctions[i].taille_args);
27 40
 	}
28 41
 }
29 42
 

+ 13
- 2
Tables/Fonctions/tab_fonctions.h View File

@@ -1,5 +1,11 @@
1
-#include "tab_symboles.h"
1
+
2
+#ifndef TAB_FONC_H
3
+#define TAB_FONC_H
4
+
5
+#include "../Symboles/table_symboles.h"
2 6
 #include <string.h>
7
+#include <stdio.h>
8
+#include <stdlib.h>
3 9
 
4 10
 #define MAX_TAILLE_FONC 50
5 11
 
@@ -8,9 +14,14 @@ struct fonction_t {
8 14
 	char * name;
9 15
 	struct type_t return_type;
10 16
 	int first_instruction_line;
17
+	int taille_args;
11 18
 };
12 19
 
13 20
 struct fonction_t get_fonction(char * name);
14 21
 
15
-void push_fonction(char * name, struct type_t type, int line);
22
+void push_fonction(char * name, struct type_t type, int line, int taille_args);
23
+
24
+void print_fonctions();
25
+
26
+#endif
16 27
 

BIN
Tables/Fonctions/tab_fonctions.o View File


+ 3
- 0
Tables/Instructions/tab_instruc.c View File

@@ -66,6 +66,9 @@ char * get_asm_line_from_op(struct operation_t op){
66 66
 		case (WR):
67 67
 			sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
68 68
 			break;
69
+		case (CALL):
70
+			sprintf(buffer,"CALL %d %d\n",op.arg1, op.arg2);
71
+			break;
69 72
 	}
70 73
 	return buffer;
71 74
 }

+ 6
- 1
Tables/Instructions/tab_instruc.h View File

@@ -1,10 +1,13 @@
1
+#ifndef TAB_INST_H
2
+#define TAB_INST_H
3
+
1 4
 #define MAXTAILLE 1024
2 5
 #include <stdlib.h>
3 6
 #include <string.h>
4 7
 #include <stdio.h>
5 8
 
6 9
 
7
-enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,COPA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR};
10
+enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,COPA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR,CALL};
8 11
 
9 12
 struct operation_t {
10 13
 	enum opcode_t opcode;
@@ -21,3 +24,5 @@ int get_current_index();
21 24
 void patch(int index, int arg);
22 25
 //Ecrit la table des intructions dans un fichier ASM
23 26
 void create_asm();
27
+
28
+#endif

+ 8
- 0
Tables/Symboles/table_symboles.c View File

@@ -109,6 +109,14 @@ struct symbole_t pop() {
109 109
 	return retour;
110 110
 }
111 111
 
112
+
113
+
114
+void multiple_pop(int n){
115
+	for (int i =0; i<n; i++){
116
+		pop();
117
+	}
118
+}
119
+
112 120
 struct symbole_t * get_variable(char * nom){
113 121
 	struct symbole_t * retour = NULL;
114 122
 	struct element_t * aux = pile->first;

+ 10
- 0
Tables/Symboles/table_symboles.h View File

@@ -25,6 +25,9 @@ Opérations possible :
25 25
 	- pop -> pile * -> symbole
26 26
 	- status -> nom -> pile -> char					*/
27 27
 
28
+#ifndef TAB_SYMB_H
29
+#define TAB_SYMB_H
30
+
28 31
 #include <stdint.h>
29 32
 
30 33
 
@@ -57,6 +60,9 @@ struct type_t {
57 60
 	int isTab;
58 61
 	 
59 62
 };
63
+
64
+//REtourne la représentation d'un type en string
65
+char * type_to_string(struct type_t type);
60 66
 //Constante pour les entiers
61 67
 extern const struct type_t integer;
62 68
 
@@ -119,6 +125,8 @@ void init(void);
119 125
 int push(char * nom, int isInit, struct type_t type);
120 126
 //Destruction et récupération du premier élément de la table
121 127
 struct symbole_t pop();
128
+//Destruction des n premiers elee=ments de la table des symboles
129
+void multiple_pop(int n);
122 130
 //Retourne la dernière adresse disponible
123 131
 int get_last_addr();
124 132
 //Renvoi un pointeur vers le symbole correspondant au nom de variable
@@ -126,3 +134,5 @@ struct symbole_t * get_variable(char * nom);
126 134
 //Affiche la table des symboles
127 135
 void print();
128 136
 
137
+
138
+#endif

Loading…
Cancel
Save