-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidCode.cpp
320 lines (296 loc) · 6.64 KB
/
midCode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "midCode.h"
#include <cstdio>
#pragma warning(disable : 4996)
#include <fstream>
using namespace std;
char s[] = "";
midCode MidCode(&Table, s);
void midCode::outPut(ofstream &tetraCodeFile) {
tetraCode * curCode = &(this->code_head);
while (curCode != 0) {
outPut(*curCode, tetraCodeFile);
curCode = curCode->next;
}
}
void midCode::outPut(tetraCode code, ofstream & tetraCodeFile) {
switch (code.op) {
case(CONST_I):
{
tetraCodeFile << "const int " << code.rd << " = " << code.rs_num << endl;
break;
}
case(CONST_C):
{
tetraCodeFile << "const char " << code.rd << " = " << '0'+code.rs_num-48 << endl;
break;
}
case (FUNC_DEF):
{
tetraCodeFile << ((code.rs_num == 20) ? "int " : (code.rs_num == 22) ? "void " : "char ") << code.rd << "()" << endl;
break;
}
case (PARA_I):
{
tetraCodeFile << "para int " << code.rd << endl;
break;
}
case (PARA_C):
{
tetraCodeFile << "para char " << code.rd << endl;
break;
}
case (VAR_I):
{
tetraCodeFile << "var int " << code.rd << endl;
break;
}
case (VAR_C):
{
tetraCodeFile << "var char " << code.rd << endl;
break;
}
case (ARRAY_I):
{
tetraCodeFile << "int array " << code.rd << "[" << code.rs_num << "]" << endl;
break;
}
case (ARRAY_C):
{
tetraCodeFile << "char array " << code.rd << "[" << code.rs_num << "]" << endl;
break;
}
case (ADD):
{
tetraCodeFile << code.rd << " = " << code.rs << " + " << code.rt << endl;
break;
}
case (SUB):
{
tetraCodeFile << code.rd << " = " << code.rs << " - " << code.rt << endl;
break;
}
case (MULT):
{
tetraCodeFile << code.rd << " = " << code.rs << " * " << code.rt << endl;
break;
}
case (DIV):
{
tetraCodeFile << code.rd << " = " << code.rs << " / " << code.rt << endl;
break;
}
case (LOAD_I):
case (LOAD_C):
{
tetraCodeFile << code.rd << " = " << code.rs << "[" << code.rt << "]" << endl;
break;
}
case (SAVE_I):
case (SAVE_C):
{
tetraCodeFile << code.rs << "[" << code.rt << "]" << " = " << code.rd << endl;
break;
}
case (CALL):
{
tetraCodeFile << "call " << code.rd << endl;
break;
}
case (GET_RET_C):
case (GET_RET_I):
{
tetraCodeFile << code.rd << " = RET" << endl;
break;
}
case (PUSH_C):
case (PUSH_I):
{
tetraCodeFile << "PUSH " << code.rd << endl;
break;
}
case (BEQ):
{
tetraCodeFile << "BEQ " << code.rs << " " << code.rt << " " << code.rd << endl;
break;
}
case (BNE):
{
tetraCodeFile << "BNE " << code.rs << " " << code.rt << " " << code.rd << endl;
break;
}
case (BGT):
{
tetraCodeFile << "BGT " << code.rs << " " << code.rt << " " << code.rd << endl;
break;
}
case (BGE):
{
tetraCodeFile << "BGE " << code.rs << " " << code.rt << " " << code.rd << endl;
break;
}
case (JUMP):
{
tetraCodeFile << "GOTO " << code.rd << endl;
break;
}
case (LAB):
{
tetraCodeFile << code.rd << ":" << endl;
break;
}
case (READ_I):
{
tetraCodeFile << code.rd << " = _GetI()" << endl;
break;
}
case (READ_C):
{
tetraCodeFile << code.rd << " = _GetC()" << endl;
break;
}
case (PRINT_I):
case (PRINT_C):
case (PRINT_S):
{
tetraCodeFile << "PRINT " << code.rd << endl;
break;
}
case (RET_C):
case (RET_I):
{
tetraCodeFile << "ret " << code.rd << endl;
break;
}
case (RET_V):
{
tetraCodeFile << "ret"<< endl;
break;
}
case(LI):
{
tetraCodeFile << "li " << code.rd << " = " << code.rs_num << endl;
}
default:
{
break;
}
}
}
struct tetraCode* midCode::add(int op, char* rd, int rs_num)
{
struct tetraCode * code = (struct tetraCode*)malloc(sizeof(struct tetraCode));
code->op = op;
this->add_head(code->rd, rd);
code->rs_num = rs_num;
code->next = NULL;
this->code_now->next = code;
this->code_now = code;
return code;
}
int midCode::add_head(char* dst_name, char* src_name)
{
if (this->extend_name == 0)
{
if (src_name != NULL)
{
strcpy(dst_name, src_name);
}
else {
dst_name = NULL;
}
return 0;
}
struct tableNode node;
if (src_name == NULL)
{
strcpy(dst_name, "");
return 0;
}
if (0 == strcmp(src_name, ZERO)) //若恒为0
{
sprintf(dst_name, ":%s", src_name);
}
else
if (src_name[0] == '&') //&是临时变量, ^是标签(没有放入符号表,也不需要)
{
sprintf(dst_name, "%s:%s", this->func_name, src_name);
}
else if (src_name[0] == '$') {
sprintf(dst_name, "%s", src_name);
}
else
{
strcpy(node.id.name, src_name);
node.id.lev = 1;
this->sym_table->find(node.id, &node);
if(node.id.lev==1)//是局部变量
sprintf(dst_name, "%s:%s", this->func_name, src_name);
else //是全局变量
sprintf(dst_name, ":%s", src_name);
}
return 0;
}
struct tetraCode* midCode::add(int op, char* rd, char* rs, char* rt)
{
struct tetraCode * code = (struct tetraCode*)malloc(sizeof(struct tetraCode));
code->op = op;
if(rs!=NULL) this->add_head(code->rs, rs);
if(rt != NULL)this->add_head(code->rt, rt);
if (rd != NULL)this->add_head(code->rd, rd);
this->code_now->next = code;
this->code_now = code;
code->next = NULL;
return code;
}
struct tetraCode* midCode::add(struct tetraCode* code)
{
struct tetraCode * new_code = (struct tetraCode*)malloc(sizeof(struct tetraCode));
*new_code = *code;
this->code_now->next = new_code;
new_code->next = NULL;
this->code_now = new_code;
return new_code;
}
//为了支持非线性生成中间代码,添加以下函数(用于do-for语句和函数调用语句)
//将节点插入到指定的head为开头的链中
struct tetraCode* midCode::addn(struct tetraCode& head, int op, char* rd, int rs_num)
{
struct tetraCode* next = &head;
while (next->next != NULL)
next = next->next;
struct tetraCode* new_node = (struct tetraCode*)malloc(sizeof(struct tetraCode));
new_node->next = NULL;
new_node->op = op;
this->add_head(new_node->rd, rd);
new_node->rs_num = rs_num;
next->next = new_node;
return new_node;
}
struct tetraCode* midCode::addn(struct tetraCode& head, int op, char* rd, char* rs, char* rt)
{
struct tetraCode* next = &head;
while (next->next != NULL)
next = next->next;
struct tetraCode* new_node = (struct tetraCode*)malloc(sizeof(struct tetraCode));
new_node->next = NULL;
new_node->op = op;
this->add_head(new_node->rd, rd);
this->add_head(new_node->rs, rs);
this->add_head(new_node->rt, rt);
next->next = new_node;
return new_node;
}
//将head开头的链插入到主链中
struct tetraCode* midCode::insert(struct tetraCode head, struct tetraCode *node_pre)
{
struct tetraCode* node_next = node_pre->next, *next = &head;
if (head.next == NULL)
return NULL;
while (next->next != NULL)
next = next->next;
node_pre->next = head.next;
next->next = node_next;
if (node_next == NULL)
this->code_now = next; //插到尾部的话,要改变code_now指向尾部
return head.next;
}