Browse Source

Ajout pointeurs

Tali Elies 3 years ago
parent
commit
fb7127065f

+ 5
- 18
Fichiers_Tests/progC View File

@@ -1,20 +1,7 @@
1 1
 int main(){
2
-    int b = 4 * 5;
3
-	int a = 1;
4
-	while (b){
5
-		b = 2;
6
-		if (a){
7
-			int u = 8;		
8
-		}
9
-		else{
10
-			if (1){
11
-				int j = 7;
12
-			}
13
-			else if (5){
14
-				int g = 89;
15
-			}
16
-			int y = 9;
17
-		}
18
-	}	
19
-	int p = 85;
2
+    int * b = 4 * 5;
3
+    int ** c,d;
4
+    int ** y = &b;
5
+    * c = 1;
6
+    &b = 2;
20 7
 }

+ 1
- 0
Lex_Yacc/al.lex View File

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

+ 33
- 36
Lex_Yacc/as.y View File

@@ -10,7 +10,7 @@
10 10
 #include "../Tables/Instructions/tab_instruc.h"
11 11
 #define TAILLE 1024
12 12
 
13
-enum type_t type_courant;
13
+struct type_t type_courant;
14 14
 
15 15
 int instructions_ligne_to_patch[10][20];
16 16
 int nbs_instructions_to_patch[10];
@@ -31,6 +31,7 @@ int nbs_instructions_to_patch[10];
31 31
 %token<nombre> tIF tWHILE tELSE
32 32
 %token tLT tGT tEQCOND
33 33
 %token tAND tOR
34
+%token tADDR
34 35
 
35 36
 %left tAND tOR
36 37
 %left tNOT
@@ -39,7 +40,7 @@ int nbs_instructions_to_patch[10];
39 40
 %left tADD tSUB
40 41
 %left tMUL tDIV
41 42
 
42
-%type<nombre> E Invocation
43
+%type<nombre> E Invocation DebutAff
43 44
 
44 45
 
45 46
 
@@ -53,11 +54,11 @@ Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
53 54
 
54 55
 Params : { printf("Sans Params\n"); } ;
55 56
 Params : Param SuiteParams ;
56
-Param : DeclType tID { printf("Prametre : %s\n", $2); };
57
+Param : Type tID { printf("Prametre : %s\n", $2); };
57 58
 SuiteParams : tCOMA Param SuiteParams ;
58 59
 SuiteParams : ;
59 60
 
60
-Body : tOBRACKET Instructions tCBRACKET { } ;
61
+Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
61 62
 
62 63
 
63 64
 Instructions : Instruction Instructions ;
@@ -69,29 +70,17 @@ Instruction : If {reset_temp_vars();};
69 70
 Instruction : While {reset_temp_vars();};
70 71
 
71 72
 //On considère que la première ligne du code en ASM est la ligne 0
72
-If : tIF tOBRACE E tCBRACE { profondeur++;
73
+If : tIF tOBRACE E tCBRACE {
73 74
 add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;} 
74 75
 Body  {int current = get_current_index();
75 76
 		patch($1,current + 1);
76 77
 		add_operation(JMP,0,0,0);
77 78
 		instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
78 79
 		nbs_instructions_to_patch[profondeur]++;}
79
-Else { printf("If reconnu\n");  reset_pronf(); profondeur--;};
80
-
81
-
82
-//On considère que la première ligne du code en ASM est la ligne 0
83
-ElseIf : tIF tOBRACE E tCBRACE {add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;} 
84
-Body  {int current = get_current_index();
85
-		patch($1,current + 1);
86
-		add_operation(JMP,0,0,0);
87
-		instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
88
-		nbs_instructions_to_patch[profondeur]++;}
89 80
 Else { printf("If reconnu\n");};
90 81
 
91 82
 
92
-
93
-
94
-Else : tELSE ElseIf { printf("Else if reconnu\n"); };
83
+Else : tELSE If { printf("Else if reconnu\n"); };
95 84
 Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index(); 
96 85
 for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
97 86
 	patch(instructions_ligne_to_patch[profondeur][i],current);
@@ -103,44 +92,52 @@ for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
103 92
 	patch(instructions_ligne_to_patch[profondeur][i],current);
104 93
 }
105 94
 nbs_instructions_to_patch[profondeur] = 0;};
106
-While : tWHILE tOBRACE E tCBRACE {profondeur++;
95
+While : tWHILE tOBRACE E tCBRACE {
107 96
 add_operation(JMF,$3,0,0); 
108 97
 $1 = get_current_index() - 1;}
109 98
 
110 99
 Body { printf("While reconnu\n");
111 100
 int current = get_current_index();
112 101
 patch($1,current + 1);
113
-add_operation(JMP,$1,0,0);
114
-reset_pronf(); profondeur--;};
102
+add_operation(JMP,$1,0,0);};
103
+
115 104
 
105
+Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0);} ; //besoin de get_address
116 106
 
117
-Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1); struct symbole_t * symbole  = get_variable($1); symbole->initialized = 1; add_operation(COP, symbole->adresse, $3,0);} ; //besoin de get_address
107
+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;};
118 110
 
119 111
 E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
120 112
 
121 113
 E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
122
-E : tID { printf("Id\n"); struct symbole_t * symbole  = get_variable($1); int addr = allocate_mem_temp_var(symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
123
-E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
124
-E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
125
-E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;};
126
-E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$3); $$ = 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
+E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
116
+E : E tDIV E { printf("Div\n");  add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();};
117
+E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();};
118
+E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();};
127 119
 E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
128 120
 E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
129
-E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, 0,addr,0); $$ = addr;};
130
-E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;};
131
-E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
132
-E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
121
+E : tSUB E { printf("Moins\n");  int addr = allocate_mem_temp_var(INT);  add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr);  $$ = $2; decrement_temp_var();};
122
+E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; decrement_temp_var();};
123
+E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; decrement_temp_var();};
124
+E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_temp_var();};
133 125
 E : tNOT E { printf("!\n"); };
134
-E : E tAND E {int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr, $1, $3); $$=addr;};
135
-E : E tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ;
126
+E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
127
+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;};
136 130
 
137 131
 
138 132
 
139 133
 //Créer un champ isConst dans la table des symboles
140
-DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
134
+Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; printf("Type int\n");} ;
135
+Type : Type tMUL {type_courant.pointeur_level++;  printf("Type int *\n");};
136
+//SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ; 
137
+//SuiteType : ;
141 138
 
142
-Decl : DeclType SuiteDecl FinDecl ;
143
-Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
139
+Decl : Type SuiteDecl FinDecl ;
140
+Decl : tCONST Type SuiteDeclConst FinDeclConst;
144 141
 
145 142
 SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
146 143
 SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};

+ 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 -ll -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 -ly -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

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

@@ -54,6 +54,12 @@ char * get_asm_line_from_op(struct operation_t op){
54 54
 		case (PRI):
55 55
 			sprintf(buffer,"PRI %d\n",op.arg1);
56 56
 			break;
57
+		case (GET):
58
+			sprintf(buffer,"GET %d %d\n",op.arg1, op.arg2);
59
+			break;
60
+		case (MOV):
61
+			sprintf(buffer,"MOV %d %d\n",op.arg1, op.arg2);
62
+			break;
57 63
 	}
58 64
 	return buffer;
59 65
 }

+ 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};
7
+enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,JMP,JMF,INF,SUP,EQU,PRI,GET,MOV};
8 8
 
9 9
 struct operation_t {
10 10
 	enum opcode_t opcode;

+ 29
- 15
Tables/Symboles/table_symboles.c View File

@@ -36,6 +36,7 @@ int last_addr = 0;
36 36
 int temp_addr = MAXADDR;
37 37
 int taille_types[] = {-1, 4};
38 38
 int profondeur = 0;
39
+int last_temp_var_size;
39 40
 
40 41
 struct element_t {
41 42
 	struct symbole_t symbole;
@@ -48,20 +49,28 @@ struct pile_t {
48 49
 };
49 50
 struct pile_t * pile;
50 51
 
51
-char * type_to_string(enum type_t type) {
52
-	if (type == INT) {
53
-		return "int";
54
-	} else {
55
-		return "unknown";	
52
+char * type_to_string(struct type_t type) {
53
+    char * star = "*";
54
+    char * resultat = malloc(sizeof(char)*20);
55
+    for (int i = 0; i< type.pointeur_level; i++){
56
+        strcat(resultat,star);
57
+    }
58
+	if (type.base == INT) {
59
+		strcat(resultat,"int");
60
+	} else {;
61
+	    strcat(resultat,"unknown");
56 62
 	}
63
+    return resultat;
57 64
 }
58 65
 
59 66
 void print_symbole(struct symbole_t symbole) {
67
+    char * type = type_to_string(symbole.type);
60 68
     if (symbole.initialized) {
61
-		printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type), symbole.profondeur);
69
+		printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:OUI, profondeur : %d}\n", symbole.nom, symbole.adresse, type, symbole.profondeur);
62 70
 	} else {
63
-		printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type_to_string(symbole.type),symbole.profondeur);
71
+		printf("\t\t{nom:%s, adresse:%ld, type:%s, initialized:NON, profondeur : %d}\n", symbole.nom, symbole.adresse, type,symbole.profondeur);
64 72
 	}
73
+    free(type);
65 74
 }
66 75
 
67 76
 void init (void) {
@@ -70,7 +79,7 @@ void init (void) {
70 79
 	pile->taille = 0;
71 80
 }
72 81
 
73
-int push(char * nom, int isInit, enum type_t type) {
82
+int push(char * nom, int isInit, struct type_t type) {
74 83
 	struct element_t * aux = malloc(sizeof(struct element_t));
75 84
 	struct symbole_t symbole = {"", last_addr, type, isInit,profondeur}; 
76 85
 	strcpy(symbole.nom,nom);
@@ -79,7 +88,7 @@ int push(char * nom, int isInit, enum type_t type) {
79 88
 	pile->first = aux;
80 89
 	pile->taille++;
81 90
 	int addr_var = last_addr;
82
-	last_addr += taille_types[type]; 
91
+	last_addr += taille_types[type.base]; 
83 92
 	return addr_var;
84 93
 }
85 94
 
@@ -92,7 +101,7 @@ struct symbole_t pop() {
92 101
 		retour = aux->symbole;
93 102
 		free(aux);
94 103
 		pile->taille--;
95
-		last_addr -= taille_types[retour.type]; 
104
+		last_addr -= taille_types[retour.type.base]; 
96 105
 	}
97 106
 	return retour;
98 107
 }
@@ -151,8 +160,9 @@ int get_last_addr(){
151 160
 }
152 161
 
153 162
 
154
-int allocate_mem_temp_var(enum type_t type){
155
-	temp_addr -= taille_types[type];
163
+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;
156 166
 	return temp_addr;
157 167
 }
158 168
 
@@ -162,8 +172,12 @@ void reset_temp_vars(){
162 172
 
163 173
 
164 174
 void reset_pronf(){
165
-	while (pile->first->symbole.profondeur == profondeur){
166
-		pop();
167
-	}
175
+    printf("Profondeur dans reset : %d\n", profondeur);
176
+    while (pile->first != NULL && pile->first->symbole.profondeur == profondeur){
177
+	    pop();
178
+    }
168 179
 }
169 180
 
181
+void decrement_temp_var(struct type_t type){
182
+   temp_addr += last_temp_var_size;
183
+}

+ 13
- 4
Tables/Symboles/table_symboles.h View File

@@ -27,29 +27,38 @@ Opérations possible :
27 27
 
28 28
 #include <stdint.h>
29 29
 
30
-enum type_t {UNKNOWN, INT};
30
+
31
+enum base_type_t {UNKNOWN, INT};
31 32
 extern int taille_types[];
32 33
 extern int profondeur;
33 34
 
35
+struct type_t {
36
+    enum base_type_t base; 
37
+    int pointeur_level;
38
+};
39
+
34 40
 struct symbole_t {
35 41
 	char nom[30];
36 42
 	uintptr_t adresse;
37
-	enum type_t type;
43
+	struct type_t type;
38 44
 	int initialized;
39 45
     int profondeur;
40 46
 };
41 47
 
48
+
49
+
42 50
 void print_symbole(struct symbole_t symbole);
43 51
 
44 52
 
45 53
 void init(void);
46
-int push(char * nom, int isInit, enum type_t type);
54
+int push(char * nom, int isInit, struct type_t type);
47 55
 struct symbole_t pop();
48 56
 // renvoi 0 si nom n'existe pas, 2 si nom existe sans etre initialisée, 1 sinon
49 57
 char status(char * nom);
50 58
 void print();
51 59
 int get_last_addr();
52 60
 struct symbole_t * get_variable(char * nom);
53
-int allocate_mem_temp_var(enum type_t type);
61
+int allocate_mem_temp_var(enum base_type_t type);
54 62
 void reset_temp_vars();
55 63
 void reset_pronf();
64
+void decrement_temp_var();

Loading…
Cancel
Save