Browse Source

Ajout tableaux et pointeurs avec AFCA

Elies Tali 3 years ago
parent
commit
3b7a6cc505

+ 10
- 5
Fichiers_Tests/progC View File

@@ -1,7 +1,12 @@
1 1
 int main(){
2
-    int * b = 4 * 5;
3
-    int ** c,d;
4
-    int ** y = &b;
5
-    * c = 1;
6
-    &b = 2;
2
+	int b[3];
3
+	if (1){
4
+		int a = b[1];
5
+	}
6
+	else if (2){
7
+		int * c = &b[2];
8
+	}
9
+	else{
10
+		b[0] = 80;
11
+	}
7 12
 }

+ 2
- 0
Lex_Yacc/al.lex View File

@@ -29,6 +29,8 @@ yyerror (char const *s)
29 29
 "||"        { return tOR; }
30 30
 "else"      { return tELSE;}
31 31
 "&"         { return tADDR;}
32
+"["         { return tOCROCH;}
33
+"]" 		{ return tCCROCH;}
32 34
 
33 35
 
34 36
 [0-9]+	{ yylval.nombre = atoi(yytext); return tNB; }

+ 32
- 21
Lex_Yacc/as.y View File

@@ -20,6 +20,7 @@ int nbs_instructions_to_patch[10];
20 20
 %token tMAIN
21 21
 %token tOBRACKET tCBRACKET
22 22
 %token tOBRACE tCBRACE
23
+%token tOCROCH tCCROCH
23 24
 %token tINT
24 25
 %token tCONST
25 26
 %token tPV tCOMA
@@ -33,14 +34,14 @@ int nbs_instructions_to_patch[10];
33 34
 %token tAND tOR
34 35
 %token tADDR
35 36
 
36
-%left tAND tOR
37
-%left tNOT
38 37
 %left tLT tGT
39 38
 %left tEQCOND
39
+%left tAND tOR
40
+%left tNOT
40 41
 %left tADD tSUB
41 42
 %left tMUL tDIV
42 43
 
43
-%type<nombre> E Invocation DebutAff
44
+%type<nombre> E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBeforeAddr
44 45
 
45 46
 
46 47
 
@@ -63,11 +64,11 @@ Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf();
63 64
 
64 65
 Instructions : Instruction Instructions ;
65 66
 Instructions : ;
66
-Instruction : Aff {reset_temp_vars();};
67
-Instruction : Decl {reset_temp_vars();};
68
-Instruction : Invocation tPV{reset_temp_vars();};
69
-Instruction : If {reset_temp_vars();};
70
-Instruction : While {reset_temp_vars();};
67
+Instruction : Aff {};
68
+Instruction : Decl {};
69
+Instruction : Invocation tPV{};
70
+Instruction : If {};
71
+Instruction : While {};
71 72
 
72 73
 //On considère que la première ligne du code en ASM est la ligne 0
73 74
 If : tIF tOBRACE E tCBRACE {
@@ -76,8 +77,9 @@ Body  {int current = get_current_index();
76 77
 		patch($1,current + 1);
77 78
 		add_operation(JMP,0,0,0);
78 79
 		instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
79
-		nbs_instructions_to_patch[profondeur]++;}
80
-Else { printf("If reconnu\n");};
80
+		nbs_instructions_to_patch[profondeur]++;
81
+		decrement_temp_var();}
82
+Else {printf("If reconnu\n");};
81 83
 
82 84
 
83 85
 Else : tELSE If { printf("Else if reconnu\n"); };
@@ -94,7 +96,8 @@ for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
94 96
 nbs_instructions_to_patch[profondeur] = 0;};
95 97
 While : tWHILE tOBRACE E tCBRACE {
96 98
 add_operation(JMF,$3,0,0); 
97
-$1 = get_current_index() - 1;}
99
+$1 = get_current_index() - 1;
100
+decrement_temp_var();}
98 101
 
99 102
 Body { printf("While reconnu\n");
100 103
 int current = get_current_index();
@@ -102,16 +105,19 @@ patch($1,current + 1);
102 105
 add_operation(JMP,$1,0,0);};
103 106
 
104 107
 
105
-Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0);} ; //besoin de get_address
108
+Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); decrement_temp_var();} ; 
109
+Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); decrement_temp_var(); decrement_temp_var();};
106 110
 
107 111
 DebutAff : tID {struct symbole_t * symbole  = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
108
-DebutAff : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
109
-DebutAff : tADDR tID { int addr = allocate_mem_temp_var(INT);  struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
110 112
 
111
-E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
113
+DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
114
+DebutAffPointeur : SuiteAffPointeur {$$=$1;};
115
+SuiteAffPointeur : tMUL tID {struct symbole_t * symbole  = get_variable($2); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
116
+SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(AFC, addr,symbole->adresse,0); int addr2 = allocate_mem_temp_var(INT); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; decrement_temp_var(); decrement_temp_var();};
117
+
112 118
 
119
+E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
113 120
 E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
114
-E : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
115 121
 E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
116 122
 E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();};
117 123
 E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();};
@@ -125,13 +131,17 @@ E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_tem
125 131
 E : tNOT E { printf("!\n"); };
126 132
 E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
127 133
 E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();} ;
128
-E : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
129
-E : tADDR tID { int addr = allocate_mem_temp_var(INT);  struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
134
+E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
135
+E : tADDR EBeforeAddr {add_operation(AFCA,$2, $2,0); $$=$2;};
136
+E : EBeforeAddr {$$=$1;};
137
+
130 138
 
139
+EBeforeAddr : tID tOCROCH E tCCROCH {struct symbole_t * symbole  = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(AFC, addr,symbole->adresse,0); int addr2 = allocate_mem_temp_var(INT); 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; decrement_temp_var(); decrement_temp_var();};
140
+EBeforeAddr : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
131 141
 
132 142
 
133 143
 //Créer un champ isConst dans la table des symboles
134
-Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; printf("Type int\n");} ;
144
+Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
135 145
 Type : Type tMUL {type_courant.pointeur_level++;  printf("Type int *\n");};
136 146
 //SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ; 
137 147
 //SuiteType : ;
@@ -140,11 +150,12 @@ Decl : Type SuiteDecl FinDecl ;
140 150
 Decl : tCONST Type SuiteDeclConst FinDeclConst;
141 151
 
142 152
 SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
143
-SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
153
+SuiteDecl : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);};
154
+SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; decl_tab($1,type_courant,$3); type_courant.nb_blocs = 1;} ;
144 155
 FinDecl : tPV { printf("Fin Decl\n");};
145 156
 FinDecl : tCOMA SuiteDecl FinDecl ;
146 157
 
147
-SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
158
+SuiteDeclConst : tID tEQ E {decrement_temp_var(); int addr = push($1,1, type_courant);};
148 159
 FinDeclConst : tPV;
149 160
 FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
150 161
 

+ 1
- 1
Makefile View File

@@ -17,7 +17,7 @@ clean_Lex_Yacc:
17 17
 	@rm -f Lex_Yacc/as.output Lex_Yacc/as.tab.* Lex_Yacc/lex.yy.*
18 18
 
19 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 -ly -o rondoudou_gcc
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
21 21
 
22 22
 build_Symboles: clean_Symboles
23 23
 	gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o

+ 7
- 4
Tables/Instructions/tab_instruc.c View File

@@ -36,6 +36,9 @@ char * get_asm_line_from_op(struct operation_t op){
36 36
 		case (AFC):
37 37
 			sprintf(buffer,"AFC %d %d\n",op.arg1, op.arg2);
38 38
 			break;
39
+		case (AFCA):
40
+			sprintf(buffer,"AFCA %d %d\n",op.arg1, op.arg2);
41
+			break;
39 42
 		case (JMP):
40 43
 			sprintf(buffer,"JMP %d\n",op.arg1);
41 44
 			break;
@@ -54,11 +57,11 @@ char * get_asm_line_from_op(struct operation_t op){
54 57
 		case (PRI):
55 58
 			sprintf(buffer,"PRI %d\n",op.arg1);
56 59
 			break;
57
-		case (GET):
58
-			sprintf(buffer,"GET %d %d\n",op.arg1, op.arg2);
60
+		case (READ):
61
+			sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
59 62
 			break;
60
-		case (MOV):
61
-			sprintf(buffer,"MOV %d %d\n",op.arg1, op.arg2);
63
+		case (WR):
64
+			sprintf(buffer,"WR %d %d\n",op.arg1, op.arg2);
62 65
 			break;
63 66
 	}
64 67
 	return buffer;

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

@@ -4,7 +4,7 @@
4 4
 #include <stdio.h>
5 5
 
6 6
 
7
-enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI,GET,MOV};
7
+enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR};
8 8
 
9 9
 struct operation_t {
10 10
 	enum opcode_t opcode;

+ 12
- 10
Tables/Symboles/table_symboles.c View File

@@ -79,6 +79,8 @@ void init (void) {
79 79
 	pile->taille = 0;
80 80
 }
81 81
 
82
+
83
+
82 84
 int push(char * nom, int isInit, struct type_t type) {
83 85
 	struct element_t * aux = malloc(sizeof(struct element_t));
84 86
 	struct symbole_t symbole = {"", last_addr, type, isInit,profondeur}; 
@@ -105,6 +107,11 @@ struct symbole_t pop() {
105 107
 	}
106 108
 	return retour;
107 109
 }
110
+
111
+struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs){
112
+	push(name,0,type);
113
+	last_addr += (nb_blocs-1)*taille_types[type.base];
114
+}
108 115
 		
109 116
 char status(char * nom) {
110 117
 	char retour = 0;
@@ -161,16 +168,11 @@ int get_last_addr(){
161 168
 
162 169
 
163 170
 int allocate_mem_temp_var(enum base_type_t type){
164
-	last_temp_var_size = taille_types[type];
165
-    temp_addr -= last_temp_var_size;
166
-	return temp_addr;
171
+	struct type_t type_bis = {type,0};
172
+	int addr = push("",1,type_bis);
173
+	return addr;
167 174
 }
168 175
 
169
-void reset_temp_vars(){
170
-	temp_addr = MAXADDR;
171
-}
172
-
173
-
174 176
 void reset_pronf(){
175 177
     printf("Profondeur dans reset : %d\n", profondeur);
176 178
     while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
@@ -178,6 +180,6 @@ void reset_pronf(){
178 180
     }
179 181
 }
180 182
 
181
-void decrement_temp_var(struct type_t type){
182
-   temp_addr += last_temp_var_size;
183
+void decrement_temp_var(){
184
+   pop();
183 185
 }

+ 3
- 3
Tables/Symboles/table_symboles.h View File

@@ -33,8 +33,9 @@ extern int taille_types[];
33 33
 extern int profondeur;
34 34
 
35 35
 struct type_t {
36
-    enum base_type_t base; 
36
+    enum base_type_t base;
37 37
     int pointeur_level;
38
+	int nb_blocs;
38 39
 };
39 40
 
40 41
 struct symbole_t {
@@ -45,14 +46,13 @@ struct symbole_t {
45 46
     int profondeur;
46 47
 };
47 48
 
48
-
49
-
50 49
 void print_symbole(struct symbole_t symbole);
51 50
 
52 51
 
53 52
 void init(void);
54 53
 int push(char * nom, int isInit, struct type_t type);
55 54
 struct symbole_t pop();
55
+struct symbole_t decl_tab(char * name, struct type_t type, int nb_blocs);
56 56
 // renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
57 57
 char status(char * nom);
58 58
 void print();

Loading…
Cancel
Save