Browse Source

Reste a creer la fonction patchant les jumps

Paul Faure 3 years ago
parent
commit
80022a688b
3 changed files with 126 additions and 70 deletions
  1. 93
    57
      as.y
  2. 29
    4
      tables.c
  3. 4
    9
      tables.h

+ 93
- 57
as.y View File

@@ -24,93 +24,129 @@ Programme : Instruction Programme;
24 24
 Programme : Instruction;
25 25
 
26 26
 Instruction : tMUL tNB tNB tNB {increment_time();
27
-																int reg_dest = get_reg_write($2, file);
28
-																int reg_src1 = get_reg_read($3, file);
29
-																int reg_src2 = get_reg_read($4, file);
30
-																fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);};
27
+																int added_instruction = 0;
28
+																int reg_dest = get_reg_write($2, file, &added_instruction);
29
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
30
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
31
+																fprintf(file, "MUL %d %d %d\n", reg_dest, reg_src1, reg_src2);
32
+																new_instruction(added_instruction + 1);};
31 33
 Instruction : tADD tNB tNB tNB {increment_time();
32
-																int reg_dest = get_reg_write($2, file);
33
-																int reg_src1 = get_reg_read($3, file);
34
-																int reg_src2 = get_reg_read($4, file);
35
-																fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);};
34
+																int added_instruction = 0;
35
+																int reg_dest = get_reg_write($2, file, &added_instruction);
36
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
37
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
38
+																fprintf(file, "ADD %d %d %d\n", reg_dest, reg_src1, reg_src2);
39
+																new_instruction(added_instruction + 1);};
36 40
 Instruction : tDIV tNB tNB tNB {increment_time();
37
-																int reg_dest = get_reg_write($2, file);
38
-																int reg_src1 = get_reg_read($3, file);
39
-																int reg_src2 = get_reg_read($4, file);
40
-																fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);};
41
+																int added_instruction = 0;
42
+																int reg_dest = get_reg_write($2, file, &added_instruction);
43
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
44
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
45
+																fprintf(file, "DIV %d %d %d\n", reg_dest, reg_src1, reg_src2);
46
+																new_instruction(added_instruction + 1);};
41 47
 Instruction : tSUB tNB tNB tNB {increment_time();
42
-																int reg_dest = get_reg_write($2, file);
43
-																int reg_src1 = get_reg_read($3, file);
44
-																int reg_src2 = get_reg_read($4, file);
45
-																fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);};
48
+																int added_instruction = 0;
49
+																int reg_dest = get_reg_write($2, file, &added_instruction);
50
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
51
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
52
+																fprintf(file, "SUB %d %d %d\n", reg_dest, reg_src1, reg_src2);
53
+																new_instruction(added_instruction + 1);};
46 54
 Instruction : tINF tNB tNB tNB {increment_time();
47
-																int reg_dest = get_reg_write($2, file);
48
-																int reg_src1 = get_reg_read($3, file);
49
-																int reg_src2 = get_reg_read($4, file);
50
-																fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);};
55
+																int added_instruction = 0;
56
+																int reg_dest = get_reg_write($2, file, &added_instruction);
57
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
58
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
59
+																fprintf(file, "INF %d %d %d\n", reg_dest, reg_src1, reg_src2);
60
+																new_instruction(added_instruction + 1);};
51 61
 Instruction : tSUP tNB tNB tNB {increment_time();
52
-																int reg_dest = get_reg_write($2, file);
53
-																int reg_src1 = get_reg_read($3, file);
54
-																int reg_src2 = get_reg_read($4, file);
55
-																fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);};
62
+																int added_instruction = 0;
63
+																int reg_dest = get_reg_write($2, file, &added_instruction);
64
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
65
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
66
+																fprintf(file, "SUP %d %d %d\n", reg_dest, reg_src1, reg_src2);
67
+																new_instruction(added_instruction + 1);};
56 68
 Instruction : tEQU tNB tNB tNB {increment_time();
57
-																int reg_dest = get_reg_write($2, file);
58
-																int reg_src1 = get_reg_read($3, file);
59
-																int reg_src2 = get_reg_read($4, file);
60
-																fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);};
69
+																int added_instruction = 0;
70
+																int reg_dest = get_reg_write($2, file, &added_instruction);
71
+																int reg_src1 = get_reg_read($3, file, &added_instruction);
72
+																int reg_src2 = get_reg_read($4, file, &added_instruction);
73
+																fprintf(file, "EQU %d %d %d\n", reg_dest, reg_src1, reg_src2);
74
+																new_instruction(added_instruction + 1);};
61 75
 
62 76
 
63 77
 Instruction : tAFC tNB tNB     {increment_time();
64
-																int reg_dest = get_reg_write($2, file);
65
-															  fprintf(file, "AFC %d %d\n", reg_dest, $3);};
78
+																int added_instruction = 0;
79
+																int reg_dest = get_reg_write($2, file, &added_instruction);
80
+															  fprintf(file, "AFC %d %d\n", reg_dest, $3);
81
+																new_instruction(added_instruction + 1);};
66 82
 Instruction : tCPY tNB tNB     {increment_time();
67
-																int reg_dest = get_reg_write($2, file);
68
-																int reg_src = get_reg_read($3, file);
69
-																fprintf(file, "CPY %d %d\n", reg_dest, reg_src);};
83
+																int added_instruction = 0;
84
+																int reg_dest = get_reg_write($2, file, &added_instruction);
85
+																int reg_src = get_reg_read($3, file, &added_instruction);
86
+																fprintf(file, "CPY %d %d\n", reg_dest, reg_src);
87
+																new_instruction(added_instruction + 1);};
70 88
 Instruction : tAFCA tNB tNB     {increment_time();
71
-																int reg_dest = get_reg_write($2, file);
72
-															  fprintf(file, "AFCA %d %d\n", reg_dest, $3);};
89
+																int added_instruction = 0;
90
+																int reg_dest = get_reg_write($2, file, &added_instruction);
91
+															  fprintf(file, "AFCA %d %d\n", reg_dest, $3);
92
+																new_instruction(added_instruction + 1);};
73 93
 Instruction : tCPYA tNB tNB     {increment_time();
74
-																int reg_dest = get_reg_write($2, file);
75
-																int reg_src = get_reg_read($3, file);
76
-																fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);};
94
+																int added_instruction = 0;
95
+																int reg_dest = get_reg_write($2, file, &added_instruction);
96
+																int reg_src = get_reg_read($3, file, &added_instruction);
97
+																fprintf(file, "CPYA %d %d\n", reg_dest, reg_src);
98
+																new_instruction(added_instruction + 1);};
77 99
 
78 100
 
79 101
 Instruction : tJMP tNB         {increment_time();
80
-																fprintf(file, "JMP %d\n", $2);};
102
+																fprintf(file, "JMP %d\n", $2);
103
+																new_instruction(1);};
81 104
 Instruction : tJMF tNB tNB     {increment_time();
82
-																int reg_src = get_reg_read($2, file);
83
-																int reg_aux = get_reg_write(-1, file);
105
+																int added_instruction = 0;
106
+																int reg_src = get_reg_read($2, file, &added_instruction);
107
+																int reg_aux = get_reg_write(-1, file, &added_instruction);
84 108
 																fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
85
-																fprintf(file, "JMZ %d\n", $3);};
109
+																fprintf(file, "JMZ %d\n", $3);
110
+																new_instruction(added_instruction + 2);};
86 111
 
87 112
 Instruction : tWR tNB tNB      {increment_time();
88
-																int reg_dest = get_reg_write($2, file);
89
-																int reg_addr = get_reg_read($3, file);
90
-																fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);};
113
+																int added_instruction = 0;
114
+																int reg_dest = get_reg_write($2, file, &added_instruction);
115
+																int reg_addr = get_reg_read($3, file, &added_instruction);
116
+																fprintf(file, "LOADA %d %d\n", reg_dest, reg_addr);
117
+																new_instruction(added_instruction + 1);};
91 118
 Instruction : tREAD tNB tNB    {increment_time();
92
-																int reg_addr = get_reg_read($2, file);
93
-																int reg_value = get_reg_read($3, file);
94
-																fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);};
119
+																int added_instruction = 0;
120
+																int reg_addr = get_reg_read($2, file, &added_instruction);
121
+																int reg_value = get_reg_read($3, file, &added_instruction);
122
+																fprintf(file, "STOREA %d %d\n", reg_addr, reg_value);
123
+																new_instruction(added_instruction + 1);};
95 124
 
96 125
 
97 126
 Instruction : tGET tNB         {increment_time();
98
-																int reg_dest = get_reg_write($2, file);
99
-																fprintf(file, "GET %d\n", reg_dest);};
127
+																int added_instruction = 0;
128
+																int reg_dest = get_reg_write($2, file, &added_instruction);
129
+																fprintf(file, "GET %d\n", reg_dest);
130
+																new_instruction(added_instruction + 1);};
100 131
 Instruction : tPRI tNB         {increment_time();
101
-																int reg_src = get_reg_read($2, file);
102
-																fprintf(file, "PRI %d\n", reg_src);};
132
+																int added_instruction = 0;
133
+																int reg_src = get_reg_read($2, file, &added_instruction);
134
+																fprintf(file, "PRI %d\n", reg_src);
135
+																new_instruction(added_instruction + 1);};
103 136
 
104 137
 
105 138
 Instruction : tCALL tNB tNB    {increment_time();
106
-																flush_and_init(file);
107
-																fprintf(file, "CALL %d %d\n", $2, $3);};
139
+																int added_instruction = flush_and_init(file);
140
+																fprintf(file, "CALL %d %d\n", $2, $3);
141
+																new_instruction(added_instruction + 1);};
108 142
 Instruction : tRET             {increment_time();
109
-																flush_and_init(file);
110
-																fprintf(file, "RET\n");};
143
+																int added_instruction = flush_and_init(file);
144
+																fprintf(file, "RET\n");
145
+																new_instruction(added_instruction + 1);};
111 146
 
112 147
 Instruction : tSTOP             {increment_time();
113
-																fprintf(file, "STOP\n");};
148
+																fprintf(file, "STOP\n");
149
+																new_instruction(1);};
114 150
 
115 151
 %%
116 152
 

+ 29
- 4
tables.c View File

@@ -14,9 +14,19 @@
14 14
 #include "tables.h"
15 15
 #define NB_REG 4
16 16
 #define MEM_SIZE 1024
17
+#define NB_INSTRUCTIONS 1024
18
+
19
+
20
+
21
+struct case_adresse {
22
+	int adresse;
23
+	int registre;
24
+	char modifie;
25
+};
17 26
 
18 27
 struct case_adresse tableau[MEM_SIZE];
19 28
 int registres[NB_REG];
29
+int traduction_JMP[NB_INSTRUCTIONS];
20 30
 
21 31
 void init (void) {
22 32
   int i;
@@ -27,7 +37,7 @@ void init (void) {
27 37
 	}
28 38
 	for (i=0; i<NB_REG; i++) {
29 39
 		registres[i] = 0;
30
-	}
40
+	}	
31 41
 }
32 42
 
33 43
 void print_case_adresse(struct case_adresse case_courante) {
@@ -88,7 +98,7 @@ int get_register() {
88 98
 	return index_max;
89 99
 }
90 100
 
91
-int get_reg_write(int adresse, FILE * file) {
101
+int get_reg_write(int adresse, FILE * file, int * added_instruction) {
92 102
 	if (adresse == -1) {
93 103
 		int dispo = get_register();
94 104
 		int previous_addr = get_adresse(dispo);
@@ -96,6 +106,7 @@ int get_reg_write(int adresse, FILE * file) {
96 106
 			struct case_adresse ancienne_case = get_info(previous_addr);
97 107
 			if (ancienne_case.modifie == 1) {
98 108
 				fprintf(file, "STORE %d %d\n", previous_addr, dispo);
109
+				*added_instruction = (*added_instruction) + 1;
99 110
 				set_modifie(previous_addr, 0);
100 111
 			}
101 112
 			set_registre(previous_addr, -1);
@@ -110,6 +121,7 @@ int get_reg_write(int adresse, FILE * file) {
110 121
 			if (previous_addr != -1) {
111 122
 				struct case_adresse ancienne_case = get_info(previous_addr);
112 123
 				if (ancienne_case.modifie == 1) {
124
+					*added_instruction = (*added_instruction) + 1;
113 125
 					fprintf(file, "STORE %d %d\n", previous_addr, dispo);
114 126
 					set_modifie(previous_addr, 0);
115 127
 				}
@@ -125,7 +137,7 @@ int get_reg_write(int adresse, FILE * file) {
125 137
 	}
126 138
 }
127 139
 
128
-int get_reg_read(int adresse, FILE * file) {
140
+int get_reg_read(int adresse, FILE * file, int * added_instruction) {
129 141
 	struct case_adresse ma_case = get_info(adresse);
130 142
 	if (ma_case.registre == -1) {
131 143
 		int dispo = get_register();
@@ -133,11 +145,13 @@ int get_reg_read(int adresse, FILE * file) {
133 145
 		if (previous_addr != -1) {
134 146
 			struct case_adresse ancienne_case = get_info(previous_addr);
135 147
 			if (ancienne_case.modifie == 1) {
148
+				*added_instruction = (*added_instruction) + 1;
136 149
 				fprintf(file, "STORE %d %d\n", previous_addr, dispo);
137 150
 				set_modifie(previous_addr, 0);
138 151
 			}
139 152
 			set_registre(previous_addr, -1);
140 153
 		}
154
+		*added_instruction = (*added_instruction) + 1;
141 155
 		fprintf(file, "LOAD %d %d\n", dispo, adresse);
142 156
 		set_registre(adresse, dispo);
143 157
 		refresh_registre(dispo);
@@ -149,14 +163,16 @@ int get_reg_read(int adresse, FILE * file) {
149 163
 }
150 164
 
151 165
 
152
-void flush_and_init(FILE * file) {
166
+int flush_and_init(FILE * file) {
153 167
 	int i;
168
+	int added_instruction = 0;
154 169
 	for (i = 0; i<MEM_SIZE; i++) {
155 170
 		if (tableau[i].registre != -1) {
156 171
 			if (tableau[i].modifie == 0) {
157 172
 				tableau[i].registre = -1;
158 173
 			} else {
159 174
 				fprintf(file, "STORE %d %d\n", i, tableau[i].registre);
175
+				added_instruction++;
160 176
 				tableau[i].registre = -1;
161 177
 				tableau[i].modifie = 0;
162 178
 			}
@@ -165,8 +181,17 @@ void flush_and_init(FILE * file) {
165 181
 	for (i=0; i<NB_REG; i++) {
166 182
 		registres[i] = 0;
167 183
 	}
184
+	return added_instruction;
168 185
 }
169 186
 
187
+void new_instruction(int nb_inst) {
188
+	static int last_intruction_adresse = 0;
189
+	static int current_instruction = 0; 
190
+	traduction_JMP[current_instruction] = last_intruction_adresse;
191
+	current_instruction++;
192
+	last_intruction_adresse += nb_inst;
193
+}
194
+	
170 195
 
171 196
 
172 197
 

+ 4
- 9
tables.h View File

@@ -16,16 +16,11 @@
16 16
 #include <stdint.h>
17 17
 #include <stdio.h>
18 18
 
19
-struct case_adresse {
20
-	int adresse;
21
-	int registre;
22
-	char modifie;
23
-};
24
-
25 19
 void init(void);
26 20
 void increment_time();
27
-int get_reg_read(int adresse, FILE * file);
28
-int get_reg_write(int adresse, FILE * file);
29
-void flush_and_init(FILE * file);
21
+int get_reg_read(int adresse, FILE * file, int * added_instruction);
22
+int get_reg_write(int adresse, FILE * file, int * added_instruction);
23
+int flush_and_init(FILE * file);
24
+void new_instruction(int nb_inst);
30 25
 
31 26
 #endif

Loading…
Cancel
Save