-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir.c
591 lines (510 loc) · 16 KB
/
ir.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "ir.h"
#include "operand.h"
#include "basic-block.h"
#include "asm.h"
#include "register.h"
#include "predefine.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
extern FILE *asm_file; // Stream to store assembly code.
static Block blk_buf[MAX_LINE];
static int nr_blk;
IR global_var_buf[16];
int global_count;
// 指令缓冲区
static IR instr_buffer[MAX_LINE];
// 已经生成的指令数量
int nr_instr;
// 操作数答应缓冲区
static char rs_s[NAME_LEN];
static char rt_s[NAME_LEN];
static char rd_s[NAME_LEN];
struct {
IR_Type relop;
const char *str;
IR_Type anti;
} relop_dict[] = {
{ IR_BEQ, "==", IR_BNE },
{ IR_BLT, "<" , IR_BGE },
{ IR_BLE, "<=", IR_BGT },
{ IR_BGT, ">" , IR_BLE },
{ IR_BGE, ">=", IR_BLT },
{ IR_BNE, "!=", IR_BEQ }
};
#define LENGTH(x) (sizeof(x) / sizeof(*x))
//
// 中间代码构造函数
// 返回 IR 在缓冲区中的下标
//
void new_instr_(IR *pIR, IR_Type type, Operand rs, Operand rt, Operand rd)
{
assert(nr_instr < MAX_LINE);
pIR->type = type;
pIR->rs = rs;
pIR->rt = rt;
pIR->rd = rd;
}
int new_instr(IR_Type type, Operand rs, Operand rt, Operand rd)
{
new_instr_(&instr_buffer[nr_instr], type, rs, rt, rd);
return nr_instr++;
}
static const char *ir_format[] = {
[IR_NOP] = "", // NOP
[IR_LABEL] = "%sLABEL %s :", // LABEL
[IR_FUNC] = "%sFUNCTION %s :", // FUNCTION
[IR_ASSIGN] = "%s := %s", // ASSIGN
[IR_ADD] = "%s := %s + %s", // ADD
[IR_SUB] = "%s := %s - %s", // SUB
[IR_MUL] = "%s := %s * %s", // MUL
[IR_DIV] = "%s := %s / %s", // DIV
[IR_ADDR] = "%s := &%s", // ADDR
[IR_DEREF_R] = "%s := *%s", // DEREF_R
[IR_DEREF_L] = "%s*%s := %s", // DEREF_L, 虽然地址在左边, 但是是参数
[IR_JMP] = "%sGOTO %s", // JMP
[IR_BEQ] = "IF %s == %s GOTO %s", // BEQ
[IR_BLT] = "IF %s < %s GOTO %s", // BLT
[IR_BLE] = "IF %s <= %s GOTO %s", // BLE
[IR_BGT] = "IF %s > %s GOTO %s", // BGT
[IR_BGE] = "IF %s >= %s GOTO %s", // BGE
[IR_BNE] = "IF %s != %s GOTO %s", // BNE
[IR_RET] = "%sRETURN %s", // RETURN
[IR_DEC] = "%sDEC %s %s", // DEC, 第一个 %s 过滤 rd_s
[IR_ARG] = "%sARG %s", // Pass argument, 第一个 %s 过滤 rd_s
[IR_CALL] = "%s := CALL %s", // CALL
[IR_PARAM] = "%sPARAM %s", // DEC PARAM, 第一个 %s 过滤 rd_s
[IR_READ] = "READ %s", // READ
[IR_WRITE] = "%sWRITE %s", // WRITE, 第一个 %s 过滤 rd_s, 输出语义不用 rd
[IR_WRITEC] = "%sWRITEC %s", // WRITEC, 第一个 %s 过滤 rd_s, 输出语义不用 rd
[IR_GLOBAL] = "%sGLOBAL %s %s", // GLOABL, 第一个 %s 过滤 rd_s, 全局变量声明,第二个 %s 输出变量名,第三个 %s 输出初始化值。
};
//
// 单条指令打印函数
//
const char *ir_to_s(IR *pir)
{
static char buf[120];
strcpy(rd_s, print_operand(pir->rd));
strcpy(rs_s, print_operand(pir->rs));
strcpy(rt_s, print_operand(pir->rt));
// 约定 BEQ 和 BNE 包围所有 Branch 指令
if (IR_BEQ <= pir->type && pir->type <= IR_BNE) {
sprintf(buf, ir_format[pir->type], rs_s, rt_s, rd_s); // 交换顺序
}
else if (pir->type == IR_DEC) { // 规划干不过特例
sprintf(buf, ir_format[pir->type], rd_s, rs_s, rt_s + 1);
}
else {
sprintf(buf, ir_format[pir->type], rd_s, rs_s, rt_s);
}
return buf;
}
void print_single_instr(IR instr, FILE *file)
{
fprintf(file, "%s", ir_to_s(&instr));
if (instr.type != IR_NOP) {
fprintf(file, "\n");
}
}
//
// Calculate all variables' offset to the function entry
// Check whether the function has subroutines.
//
#define MAP_SIZE 4096
int in_func_check(IR buf[], int index, int n)
{
static bool exists[MAP_SIZE];
static IR *curr = NULL;
static int param_size;
TEST(n <= MAP_SIZE, "exceed exists-map size");
if (index >= n) {
return 0; // meaningless
}
IR_Type type = buf[index].type;
if (type == IR_FUNC) {
memset(exists, 0, sizeof(exists));
curr = &buf[index];
curr->rs->size = 0;
param_size = 0;
}
else if (type != IR_PARAM) {
for (int i = 0; i < 3; i++) {
Operand ope = buf[index].operand[i];
if (ope == NULL) {
continue;
}
switch (ope->type) {
case OPE_VAR:
case OPE_REF:
case OPE_TEMP:
case OPE_BOOL:
case OPE_ADDR:
if (!exists[ope->index]) {
curr->rs->size += ope->size;
ope->address = curr->rs->size; // Calc afterwards because the stack grows from high to low
exists[ope->index] = true;
}
break;
default:
;
}
}
IR_Type type = buf[index].type;
if (type == IR_CALL || type == IR_READ ||
type == IR_WRITE || type == IR_WRITEC) {
curr->rs->has_subroutine = true;
}
}
else {
curr->rs->nr_arg++;
buf[index].rs->is_param = true;
buf[index].rs->address = - param_size;
param_size += 4;
exists[buf[index].rs->index] = true;
}
return in_func_check(buf, index + 1, n);
}
//
// 打印指令缓冲区中所有的已生成指令
//
void preprocess_ir();
void optimize_in_block();
int compress_ir(IR buf[], int n);
void print_instr(FILE *file)
{
// 相当于窥孔优化
preprocess_ir();
in_func_check(instr_buffer, 0, nr_instr);
optimize_in_block();
#ifdef DEBUG
for (int i = 0; i < global_count; i++) {
print_single_instr(global_var_buf[i], file);
}
for (int i = 0; i < nr_instr; i++) {
print_single_instr(instr_buffer[i], file);
}
fclose(file);
#endif
////////////////////////////////////////////////////////
// Generate assembly code
////////////////////////////////////////////////////////
// Predefined variables
predefined_data(asm_file);
// 全局变量定义应该在数据段
// 所有 IR_GLOBAL,在此段进行翻译
for (int i = 0; i < global_count; i++){
gen_asm(&global_var_buf[i]);
}
// Predefined functions.
predefined_text(asm_file);
// Handle each basic block
for (int i = 0; i < nr_blk; i++) {
fprintf(asm_file, "#########################\n");
fprintf(asm_file, "### basic block ###\n");
fprintf(asm_file, "#########################\n");
Block *blk = &blk_buf[i];
int j;
for (j = blk->start; j < blk->end - 1; j++) {
IR *ir = instr_buffer + j;
// Update destination's liveness information
//
// We may use the destination's next_use field to judge whether it is worth generating.
// But we should not update the next_use information for the source.
//
// Given the case that this instruction is the last one use its rs, indexed by X.
// The previous next_use information for rs indicates that rs's next referrence is at X.
// And X stores the updated next_use information indicating that rs is no longer needed.
//
// If X uses another source operand which need loading into register, it is highly possible
// that the currently used source register will be overrided, leading to a wrong result.
//
// A more effective solution is dividing the gen_asm into two parts. The 1st part just ensures
// source operands having been loaded into registers. Then update the next use information.
// Therefore the destination can still be judged and can use the source operands' register as well
if (ir->rd) ir->rd->liveness = ir->rd_info.liveness, ir->rd->next_use = ir->rd_info.next_use;
gen_asm(ir);
// Update operand information
if (ir->rs) ir->rs->liveness = ir->rs_info.liveness, ir->rs->next_use = ir->rs_info.next_use;
if (ir->rt) ir->rt->liveness = ir->rt_info.liveness, ir->rt->next_use = ir->rt_info.next_use;
}
// Handle the last IR. We should choose a proper time to spill the value into memory.
if (can_jump(instr_buffer + j)) {
push_all(); // jump instr just load data, they don't change data.
gen_asm(instr_buffer + j);
}
else if (instr_buffer[j].type != IR_RET) {
gen_asm(instr_buffer + j); // May change variables
push_all();
}
else {
push_all(); // But we need to save global variables.
gen_asm(instr_buffer + j); // Local variables do not need to store when return
}
clear_reg_state();
}
}
//
// 预处理工具 - 全局替换操作数, 无论其出现在哪个位置
//
typedef enum {
REP_SRC = (1 << 0), // 只替换源操作数
REP_DST = (1 << 1), // 只替换目标操作数
REP_BLK = (1 << 2), // 只在块内替换
} RepOpeMode;
//
// 替换操作数, 返回替换数量
//
int replace_operand(Operand newbie, Operand old, RepOpeMode mode)
{
int rep_count = 0;
for (int i = 0; i < nr_instr; i++) {
IR *pIR = &instr_buffer[i];
if (pIR->type == IR_NOP) {
continue;
}
// TODO 检查 basic block
if (pIR->rs == old && (mode & REP_SRC)) {
pIR->rs = newbie;
rep_count++;
}
if (pIR->rt == old && (mode & REP_SRC)) {
pIR->rt = newbie;
rep_count++;
}
if (pIR->rd == old && (mode & REP_DST)) {
pIR->rd = newbie;
rep_count++;
}
}
return rep_count;
}
//
// 全局替换操作数
//
int replace_operand_global(Operand newbie, Operand old)
{
return replace_operand(newbie, old, REP_SRC | REP_DST | REP_BLK);
}
//
// 检查是否为分支类指令
//
int is_branch(IR *pIR)
{
return IR_BEQ <= pIR->type && pIR->type <= IR_BNE;
}
//
// 检查是否为跳转类指令
//
bool can_jump(IR *pIR)
{
if (is_branch(pIR)) {
return true;
}
else if (pIR->type == IR_JMP) {
return true;
}
else {
return false;
}
}
//
// relop 字典使用接口
//
const char *get_relop_symbol(IR_Type relop)
{
for (int i = 0; i < LENGTH(relop_dict); i++) {
if (relop_dict[i].relop == relop) {
return relop_dict[i].str;
}
}
return NULL;
}
IR_Type get_relop_anti(IR_Type relop)
{
for (int i = 0; i < LENGTH(relop_dict); i++) {
if (relop_dict[i].relop == relop) {
return relop_dict[i].anti;
}
}
return IR_NOP;
}
IR_Type get_relop(const char *sym)
{
for (int i = 0; i < LENGTH(relop_dict); i++) {
if (!strcmp(relop_dict[i].str, sym)) {
return relop_dict[i].relop;
}
}
return IR_NOP;
}
//
// 控制流分析工具
// 翻译期的优化我只能进行简单的常数折叠
// 更多优化还是要靠对指令的直接分析来决定, 毕竟还是不太敢在生成条件表达式时就对省略分支......
// 关于控制流分析的主要知识现在来自 http://www.cs.utexas.edu/users/mckinley/380C
//
// 对 LABEL 进行引用计数管理
void deref_label(IR *pIR)
{
assert(pIR->type == IR_LABEL);
pIR->rs->label_ref_cnt--;
if (pIR->rs->label_ref_cnt == 0) {
pIR->type = IR_NOP;
free(pIR->rs);
pIR->rs = NULL;
}
}
//
// 压缩指令, 删除NOP
//
int compress_ir(IR instr[], int n)
{
int size = 0;
for (int index = 0; index < n; index++) {
if (instr[index].type != IR_NOP) {
instr[size++] = instr[index];
}
}
return size;
}
//
// 预处理
// 预处理主要干以下工作:
// 1. 将 Label 的编号替换为指令编号, 方便阅读
// 2. 将下面的模式:
// IF cond GOTO L0
// GOTO L1
// LABEL L0
// 替换成:
// IF !cond GOTO L1
// 当然可能要确保 L0 的引用只有这一处, 否则还是要保留 LABEL L0 的
// 3. 将所有的全局变量声明移动到单独的 global_var_buf
// 4. 将 2、3 产生的 NOP 通过移动数组删除(那样子标签编号要最后做)
//
void preprocess_ir()
{
IR *pIR = &instr_buffer[0];
// 移动所有的全局变量定义
global_count = 0;
for (int i = 0; i < nr_instr; i++){
if(pIR->type == IR_GLOBAL){
memcpy(&global_var_buf[global_count], pIR, sizeof(*pIR));
global_count++;
pIR->type = IR_NOP;
}
pIR++;
}
// Label 的引用计数
pIR = &instr_buffer[0];
for (int i = 0; i < nr_instr; i++) {
if (is_branch(pIR)) {
pIR->rd->label_ref_cnt++;
}
else if (pIR->type == IR_JMP) {
pIR->rs->label_ref_cnt++;
}
pIR++;
}
// 简单的模式处理: Label true 就在 GOTO false 下面
// 以及 goto 后面就是对应的 label
pIR = &instr_buffer[0];
for (int i = 0; i < nr_instr - 2; i++) {
if (is_branch(pIR) &&
(pIR + 1)->type == IR_JMP &&
(pIR + 2)->type == IR_LABEL &&
pIR->rd == (pIR + 2)->rs) {
pIR->type = get_relop_anti(pIR->type);
deref_label(pIR + 2);
pIR->rd = (pIR + 1)->rs;
(pIR + 1)->type = IR_NOP;
}
else if (pIR->type == IR_JMP &&
(pIR + 1)->type == IR_LABEL &&
(pIR + 1)->rs == pIR->rs) {
// goto 后面就是对应的 label
pIR->type = IR_NOP;
deref_label(pIR + 1);
}
pIR++;
}
// 简单的模式处理: 连续 Label 归一
IR *preLabel = NULL;
pIR = &instr_buffer[0];
for (int i = 0; i < nr_instr; i++) {
if (pIR->type == IR_LABEL && preLabel == NULL) {
preLabel = pIR;
}
else if (pIR->type != IR_LABEL) {
preLabel = NULL;
}
else {
// 连续 Label
preLabel->rs->label_ref_cnt += pIR->rs->label_ref_cnt;
replace_operand_global(preLabel->rs, pIR->rs);
pIR->type = IR_NOP;
pIR->rs = NULL;
}
pIR++;
}
// 第一次压缩
nr_instr = compress_ir(instr_buffer, nr_instr);
}
//
// 分析基本块: 活跃性分析
// end 不可取
//
void optimize_liveness(int start, int end)
{
// Init
for (int i = end - 1; i >= start; i--) {
IR *ir = &instr_buffer[i];
for (int k = 0; k < NR_OPE; k++) {
Operand ope = ir->operand[k];
if (ope == NULL) {
continue;
}
if (ope->type == OPE_VAR || ope->type == OPE_BOOL) {
ope->liveness = ALIVE;
}
else {
ope->liveness = DISALIVE;
}
ope->next_use = MAX_LINE;
ope->is_using = false;
}
}
// Iteration
for (int i = end - 1; i >= start; i--) {
IR *ir = &instr_buffer[i];
// 保存当前状态
for (int k = 0; k < NR_OPE; k++) {
if (ir->operand[k]) {
ir->info[k].liveness = ir->operand[k]->liveness;
ir->info[k].next_use = ir->operand[k]->next_use;
}
}
if (ir->rd) {
ir->rd->liveness = DISALIVE;
ir->rd->next_use = NO_USE;
}
for (int k = 0; k < NR_OPE; k++) {
if (k != RD_IDX && ir->operand[k]) {
ir->operand[k]->liveness = ALIVE;
ir->operand[k]->next_use = i;
}
}
}
}
//
// 基本块
//
void optimize_in_block()
{
nr_blk = block_partition(blk_buf, instr_buffer, nr_instr);
for (int i = 0; i < nr_blk; i++) {
int beg = blk_buf[i].start;
int end = blk_buf[i].end;
optimize_liveness(beg, end);
}
}