|
@@ -6,11 +6,12 @@
|
6
|
6
|
#include <stdio.h>
|
7
|
7
|
|
8
|
8
|
FILE * file;
|
|
9
|
+FILE * file2;
|
9
|
10
|
|
10
|
11
|
%}
|
11
|
12
|
|
12
|
13
|
%token tMUL tDIV tADD tSUB tINF tSUP tEQU
|
13
|
|
-%token tAFC tCPY tAFCA tCPYA
|
|
14
|
+%token tAFC tCPY tAFCA
|
14
|
15
|
%token tREAD tWR
|
15
|
16
|
%token tJMP tJMF
|
16
|
17
|
%token tGET tPRI
|
|
@@ -25,134 +26,132 @@ Programme : Instruction;
|
25
|
26
|
|
26
|
27
|
Instruction : tMUL tNB tNB tNB {increment_time();
|
27
|
28
|
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);
|
|
29
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
30
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
31
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
32
|
+ add_instruction(MUL, reg_dest, reg_src1, reg_src2);
|
32
|
33
|
new_instruction(added_instruction + 1);};
|
33
|
34
|
Instruction : tADD tNB tNB tNB {increment_time();
|
34
|
35
|
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);
|
|
36
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
37
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
38
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
39
|
+ add_instruction(ADD, reg_dest, reg_src1, reg_src2);
|
39
|
40
|
new_instruction(added_instruction + 1);};
|
40
|
41
|
Instruction : tDIV tNB tNB tNB {increment_time();
|
41
|
42
|
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);
|
|
43
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
44
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
45
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
46
|
+ add_instruction(DIV, reg_dest, reg_src1, reg_src2);
|
46
|
47
|
new_instruction(added_instruction + 1);};
|
47
|
48
|
Instruction : tSUB tNB tNB tNB {increment_time();
|
48
|
49
|
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);
|
|
50
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
51
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
52
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
53
|
+ add_instruction(SUB, reg_dest, reg_src1, reg_src2);
|
53
|
54
|
new_instruction(added_instruction + 1);};
|
54
|
55
|
Instruction : tINF tNB tNB tNB {increment_time();
|
55
|
56
|
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);
|
|
57
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
58
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
59
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
60
|
+ add_instruction(INF, reg_dest, reg_src1, reg_src2);
|
60
|
61
|
new_instruction(added_instruction + 1);};
|
61
|
62
|
Instruction : tSUP tNB tNB tNB {increment_time();
|
62
|
63
|
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);
|
|
64
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
65
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
66
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
67
|
+ add_instruction(SUP, reg_dest, reg_src1, reg_src2);
|
67
|
68
|
new_instruction(added_instruction + 1);};
|
68
|
69
|
Instruction : tEQU tNB tNB tNB {increment_time();
|
69
|
70
|
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);
|
|
71
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
72
|
+ int reg_src1 = get_reg_read($3, &added_instruction);
|
|
73
|
+ int reg_src2 = get_reg_read($4, &added_instruction);
|
|
74
|
+ add_instruction(EQU, reg_dest, reg_src1, reg_src2);
|
74
|
75
|
new_instruction(added_instruction + 1);};
|
75
|
76
|
|
76
|
77
|
|
77
|
78
|
Instruction : tAFC tNB tNB {increment_time();
|
78
|
79
|
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);
|
|
80
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
81
|
+ add_instruction(AFC, reg_dest, $3, 0);
|
81
|
82
|
new_instruction(added_instruction + 1);};
|
82
|
83
|
Instruction : tCPY tNB tNB {increment_time();
|
83
|
84
|
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);
|
|
85
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
86
|
+ int reg_src = get_reg_read($3, &added_instruction);
|
|
87
|
+ add_instruction(CPY, reg_dest, reg_src, 0);
|
87
|
88
|
new_instruction(added_instruction + 1);};
|
88
|
89
|
Instruction : tAFCA tNB tNB {increment_time();
|
89
|
90
|
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);};
|
93
|
|
-Instruction : tCPYA tNB tNB {increment_time();
|
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);};
|
|
91
|
+ int reg_aux = get_reg_write(-1, &added_instruction);
|
|
92
|
+ add_instruction(AFC, reg_aux, $3, 0);
|
|
93
|
+ add_instruction(STOREA, $2, reg_aux, 0);
|
|
94
|
+ new_instruction(added_instruction + 2);};
|
99
|
95
|
|
100
|
96
|
|
101
|
97
|
Instruction : tJMP tNB {increment_time();
|
102
|
|
- fprintf(file, "JMP %d\n", $2);
|
|
98
|
+ add_instruction(JMP, $2, 0, 0);
|
103
|
99
|
new_instruction(1);};
|
104
|
100
|
Instruction : tJMF tNB tNB {increment_time();
|
105
|
101
|
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);
|
108
|
|
- fprintf(file, "SUB %d %d %d\n", reg_aux, reg_aux, reg_src);
|
109
|
|
- fprintf(file, "JMZ %d\n", $3);
|
|
102
|
+ int reg_src = get_reg_read($2, &added_instruction);
|
|
103
|
+ int reg_aux = get_reg_write(-1, &added_instruction);
|
|
104
|
+ add_instruction(SUB, reg_aux, reg_aux, reg_src);
|
|
105
|
+ add_instruction(JMZ, $3, 0, 0);
|
110
|
106
|
new_instruction(added_instruction + 2);};
|
111
|
107
|
|
112
|
108
|
Instruction : tWR tNB tNB {increment_time();
|
113
|
109
|
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);
|
|
110
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
111
|
+ int reg_addr = get_reg_read($3, &added_instruction);
|
|
112
|
+ add_instruction(LOADI, reg_dest, reg_addr, 0);
|
117
|
113
|
new_instruction(added_instruction + 1);};
|
118
|
114
|
Instruction : tREAD tNB tNB {increment_time();
|
119
|
115
|
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);
|
|
116
|
+ int reg_addr = get_reg_read($2, &added_instruction);
|
|
117
|
+ int reg_value = get_reg_read($3, &added_instruction);
|
|
118
|
+ add_instruction(STOREI, reg_addr, reg_value, 0);
|
123
|
119
|
new_instruction(added_instruction + 1);};
|
124
|
120
|
|
125
|
121
|
|
126
|
122
|
Instruction : tGET tNB {increment_time();
|
127
|
123
|
int added_instruction = 0;
|
128
|
|
- int reg_dest = get_reg_write($2, file, &added_instruction);
|
129
|
|
- fprintf(file, "GET %d\n", reg_dest);
|
|
124
|
+ int reg_dest = get_reg_write($2, &added_instruction);
|
|
125
|
+ add_instruction(GET, reg_dest, 0, 0);
|
130
|
126
|
new_instruction(added_instruction + 1);};
|
131
|
127
|
Instruction : tPRI tNB {increment_time();
|
132
|
128
|
int added_instruction = 0;
|
133
|
|
- int reg_src = get_reg_read($2, file, &added_instruction);
|
134
|
|
- fprintf(file, "PRI %d\n", reg_src);
|
|
129
|
+ int reg_src = get_reg_read($2, &added_instruction);
|
|
130
|
+ add_instruction(PRI, reg_src, 0, 0);
|
135
|
131
|
new_instruction(added_instruction + 1);};
|
136
|
132
|
|
137
|
133
|
|
138
|
134
|
Instruction : tCALL tNB tNB {increment_time();
|
139
|
135
|
int added_instruction = flush_and_init(file);
|
140
|
|
- fprintf(file, "CALL %d %d\n", $2, $3);
|
|
136
|
+ add_instruction(STOP, $2, $3, 0);
|
141
|
137
|
new_instruction(added_instruction + 1);};
|
142
|
138
|
Instruction : tRET {increment_time();
|
143
|
139
|
int added_instruction = flush_and_init(file);
|
144
|
|
- fprintf(file, "RET\n");
|
|
140
|
+ add_instruction(RET, 0, 0, 0);
|
145
|
141
|
new_instruction(added_instruction + 1);};
|
146
|
142
|
|
147
|
|
-Instruction : tSTOP {increment_time();
|
148
|
|
- fprintf(file, "STOP\n");
|
|
143
|
+Instruction : tSTOP tNB {increment_time();
|
|
144
|
+ add_instruction(STOP, $2, 0, 0);
|
149
|
145
|
new_instruction(1);};
|
150
|
146
|
|
151
|
147
|
%%
|
152
|
148
|
|
153
|
149
|
int main(void) {
|
154
|
|
- file = stdout;
|
155
|
|
- init();
|
|
150
|
+ file = fopen("output.asm", "w");
|
|
151
|
+ file2 = fopen("output.bin", "w");
|
|
152
|
+ init();
|
156
|
153
|
yyparse();
|
|
154
|
+ write_asm(file);
|
|
155
|
+ write_code_machine(file2);
|
157
|
156
|
return 0;
|
158
|
157
|
}
|