-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMachineCode.cpp
More file actions
682 lines (618 loc) · 19 KB
/
Copy pathMachineCode.cpp
File metadata and controls
682 lines (618 loc) · 19 KB
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include "MachineCode.h"
#include <iostream>
#include "Type.h"
extern FILE* yyout;
int MachineBlock::label = 0;
MachineOperand::MachineOperand(int tp, int val)
{
this->type = tp;
if (tp == MachineOperand::IMM)
this->val = val;
else
this->reg_no = val;
}
MachineOperand::MachineOperand(std::string label)
{
this->type = MachineOperand::LABEL;
this->label = label;
}
bool MachineOperand::operator==(const MachineOperand& a) const
{
if (this->type != a.type)
return false;
if (this->type == IMM)
return this->val == a.val;
return this->reg_no == a.reg_no;
}
bool MachineOperand::operator<(const MachineOperand& a) const
{
if (this->type == a.type)
{
if (this->type == IMM)
return this->val < a.val;
return this->reg_no < a.reg_no;
}
return this->type < a.type;
if (this->type != a.type)
return false;
if (this->type == IMM)
return this->val == a.val;
return this->reg_no == a.reg_no;
}
void MachineOperand::PrintReg()
{
switch (reg_no)
{
case 11:
fprintf(yyout, "fp");
break;
case 13:
fprintf(yyout, "sp");
break;
case 14:
fprintf(yyout, "lr");
break;
case 15:
fprintf(yyout, "pc");
break;
default:
fprintf(yyout, "r%d", reg_no);
break;
}
}
void MachineOperand::output()
{
/* HINT:print operand
* Example:
* immediate num 1 -> print #1;
* register 1 -> print r1;
* lable addr_a -> print addr_a; */
switch (this->type)
{
case IMM:
fprintf(yyout, "#%d", this->val);
break;
case VREG:
fprintf(yyout, "v%d", this->reg_no);
break;
case REG:
PrintReg();
break;
case LABEL:
if (this->label.substr(0, 2) == ".L")
fprintf(yyout, "%s", this->label.c_str());
else if (this->label.substr(0, 1) == "@")
fprintf(yyout, "%s", this->label.c_str() + 1);
else
fprintf(yyout, "addr_%s%d", this->label.c_str(), parent->getParent()->getParent()->getParent()->getGlobalNum());
default:
break;
}
}
void MachineInstruction::PrintCond()
{
switch (cond)
{
case EQ:
fprintf(yyout, "eq");
break;
case NE:
fprintf(yyout, "ne");
break;
case LT:
fprintf(yyout, "lt");
break;
case LE:
fprintf(yyout, "le");
break;
case GT:
fprintf(yyout, "gt");
break;
case GE:
fprintf(yyout, "ge");
break;
default:
break;
}
}
void MachineInstruction::insertBefore(MachineInstruction* inst)
{
auto& instructions = parent->getInsts();
auto it = std::find(instructions.begin(), instructions.end(), this);
instructions.insert(it, inst);
}
void MachineInstruction::insertAfter(MachineInstruction* inst)
{
auto& instructions = parent->getInsts();
auto it = std::find(instructions.begin(), instructions.end(), this);
instructions.insert(++it, inst);
}
BinaryMInstruction::BinaryMInstruction(MachineBlock* p, int op, MachineOperand* dst, MachineOperand* src1, MachineOperand* src2, int cond)
{
this->parent = p;
this->type = MachineInstruction::BINARY;
this->op = op;
this->cond = cond;
this->def_list.push_back(dst);
this->use_list.push_back(src1);
this->use_list.push_back(src2);
dst->setParent(this);
src1->setParent(this);
src2->setParent(this);
}
void BinaryMInstruction::output()
{
switch (this->op)
{
case BinaryMInstruction::ADD:
fprintf(yyout, "\tadd ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
case BinaryMInstruction::SUB:
fprintf(yyout, "\tsub ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
case BinaryMInstruction::AND:
fprintf(yyout, "\tand ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
case BinaryMInstruction::OR:
fprintf(yyout, "\torr ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
case BinaryMInstruction::MUL:
fprintf(yyout, "\tmul ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
case BinaryMInstruction::DIV:
fprintf(yyout, "\tsdiv ");
this->PrintCond();
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
break;
default:
break;
}
}
LoadMInstruction::LoadMInstruction(MachineBlock* p, MachineOperand* dst, MachineOperand* src1, MachineOperand* src2, int cond)
{
this->parent = p;
this->type = MachineInstruction::LOAD;
this->op = -1;
this->cond = cond;
this->def_list.push_back(dst);
this->use_list.push_back(src1);
if (src2)
this->use_list.push_back(src2);
dst->setParent(this);
src1->setParent(this);
if (src2)
src2->setParent(this);
}
void LoadMInstruction::output()
{
fprintf(yyout, "\tldr ");
this->def_list[0]->output();
fprintf(yyout, ", ");
// Load immediate num, eg: ldr r1, =8
if (this->use_list[0]->isImm())
{
fprintf(yyout, "=%d\n", this->use_list[0]->getVal());
return;
}
// Load address
if (this->use_list[0]->isReg() || this->use_list[0]->isVReg())
fprintf(yyout, "[");
this->use_list[0]->output();
if (this->use_list.size() > 1)
{
fprintf(yyout, ", ");
this->use_list[1]->output();
}
if (this->use_list[0]->isReg() || this->use_list[0]->isVReg())
fprintf(yyout, "]");
fprintf(yyout, "\n");
}
StoreMInstruction::StoreMInstruction(MachineBlock* p, MachineOperand* src1, MachineOperand* src2, MachineOperand* src3, int cond)
{
this->parent = p;
this->type = MachineInstruction::STORE;
this->op = -1;
this->cond = cond;
this->use_list.push_back(src1);
this->use_list.push_back(src2);
if (src3)
this->use_list.push_back(src3);
src1->setParent(this);
src2->setParent(this);
if (src3)
src3->setParent(this);
}
void StoreMInstruction::output()
{
fprintf(yyout, "\tstr ");
this->use_list[0]->output();
fprintf(yyout, ", ");
// store address
if (this->use_list[1]->isReg() || this->use_list[1]->isVReg())
fprintf(yyout, "[");
this->use_list[1]->output();
if (this->use_list.size() > 2)
{
fprintf(yyout, ", ");
this->use_list[2]->output();
}
if (this->use_list[1]->isReg() || this->use_list[1]->isVReg())
fprintf(yyout, "]");
fprintf(yyout, "\n");
}
MovMInstruction::MovMInstruction(MachineBlock* p,int op, MachineOperand* dst, MachineOperand* src, int cond)
{
this->parent = p;
this->type = MachineInstruction::MOV;
this->op = op;
this->cond = cond;
this->def_list.push_back(dst);
this->use_list.push_back(src);
dst->setParent(this);
src->setParent(this);
}
void MovMInstruction::output()
{
fprintf(yyout, "\tmov");
PrintCond();
fprintf(yyout, " ");
this->def_list[0]->output();
fprintf(yyout, ", ");
this->use_list[0]->output();
fprintf(yyout, "\n");
}
BranchMInstruction::BranchMInstruction(MachineBlock* p, int op, MachineOperand* dst, int cond)
{
this->parent = p;
this->type = MachineInstruction::BRANCH;
this->op = op;
this->cond = cond;
this->use_list.push_back(dst);
dst->setParent(this);
}
void BranchMInstruction::output()
{
switch (op)
{
case B:
fprintf(yyout, "\tb");
PrintCond();
fprintf(yyout, " ");
this->use_list[0]->output();
fprintf(yyout, "\n");
break;
case BX:
fprintf(yyout, "\tbx");
PrintCond();
fprintf(yyout, " ");
this->use_list[0]->output();
fprintf(yyout, "\n");
break;
case BL:
fprintf(yyout, "\tbl");
PrintCond();
fprintf(yyout, " ");
this->use_list[0]->output();
fprintf(yyout, "\n");
break;
}
}
CmpMInstruction::CmpMInstruction(MachineBlock* p, MachineOperand* src1, MachineOperand* src2, int cond)
{
this->parent = p;
this->type = MachineInstruction::CMP;
this->op = -1;
this->cond = cond;
p->setCmpCond(cond);
this->use_list.push_back(src1);
this->use_list.push_back(src2);
src1->setParent(this);
src2->setParent(this);
}
void CmpMInstruction::output()
{
fprintf(yyout, "\tcmp ");
this->use_list[0]->output();
fprintf(yyout, ", ");
this->use_list[1]->output();
fprintf(yyout, "\n");
}
StackMInstrcuton::StackMInstrcuton(MachineBlock* p, int op, std::vector<MachineOperand*> srcs, MachineOperand* src, MachineOperand* src1, int cond)
{
this->parent = p;
this->type = MachineInstruction::STACK;
this->op = op;
this->cond = cond;
if (srcs.size())
{
for (auto it = srcs.begin(); it != srcs.end(); it++)
this->use_list.push_back(*it);
}
this->use_list.push_back(src);
src->setParent(this);
if (src1)
{
this->use_list.push_back(src1);
src1->setParent(this);
}
}
void StackMInstrcuton::output()
{
switch (op)
{
case PUSH:
fprintf(yyout, "\tpush ");
break;
case POP:
fprintf(yyout, "\tpop ");
break;
}
fprintf(yyout, "{");
this->use_list[0]->output();
for (long unsigned int i = 1; i < use_list.size(); i++)
{
fprintf(yyout, ", ");
this->use_list[i]->output();
}
fprintf(yyout, "}\n");
}
MachineFunction::MachineFunction(MachineUnit* p, SymbolEntry* sym_ptr)
{
this->parent = p;
this->sym_ptr = sym_ptr;
this->stack_size = 0;
this->paramsNum = ((FunctionType*)(sym_ptr->getType()))->getParamsSe().size();
};
void MachineBlock::output()
{
bool isR3Used = true;
int storeOffset = (parent->getSavedRegs().size() + 2) * 4;
int param_num = parent->getParamsNum();
int instCount = 0;
if (!inst_list.empty())
{
fprintf(yyout, ".L%d:\n", this->no);
for (auto it = inst_list.begin(); it != inst_list.end(); it++)
{
if ((*it)->isBX())
{
auto fp = new MachineOperand(MachineOperand::REG, 11);
auto lr = new MachineOperand(MachineOperand::REG, 14);
auto cur_inst = new StackMInstrcuton(this, StackMInstrcuton::POP, parent->getSavedRegs(), fp, lr);
cur_inst->output();
}
if (param_num > 4 && (*it)->isStore())// 12/9 函数参数大于四个,则有store指令时需要在栈中参数的上面
{
MachineOperand* operand = (*it)->getUse()[0];
if (operand->isReg() && operand->getReg() == 3) // 12/10 如果当前存的变量寄存器恰好为r3,则其为参数
{
if (isR3Used)
isR3Used = false;
else
{
auto fp = new MachineOperand(MachineOperand::REG, 11);
auto r3 = new MachineOperand(MachineOperand::REG, 3);
auto off = new MachineOperand(MachineOperand::IMM, storeOffset);
storeOffset += 4; // 12/10 一个int的大小
auto cur_inst = new LoadMInstruction(this, r3, fp, off);// 12/9 从栈中把后续参数取出。
cur_inst->output();
}
}
}
if ((*it)->isAdd())
{
auto dst = (*it)->getDef()[0];
auto src1 = (*it)->getUse()[0];
if (dst->isReg() && dst->getReg() == 13 && src1->isReg() && src1->getReg() == 13 && (*(it + 1))->isBX())
{
int size = parent->getAllocSpace();
if (size > VALID_MAX || size < -VALID_MAX)
{
auto r1 = new MachineOperand(MachineOperand::REG, 1);
auto off = new MachineOperand(MachineOperand::IMM, size);
(new LoadMInstruction(nullptr, r1, off))->output();
(*it)->getUse()[1]->setReg(1);
}
else
(*it)->getUse()[1]->setVal(size);
}
}
(*it)->output();
instCount++;
if (instCount % 500 == 0) // 12/10 当指令条数过多时需要有特殊标注
{
fprintf(yyout, "\tb .B%d\n", label);
fprintf(yyout, ".LTORG\n");
parent->getParent()->printGlobalBridge();
fprintf(yyout, ".B%d:\n", label++);
}
}
}
}
void MachineFunction::output()
{
fprintf(yyout, "\t.global %s\n", this->sym_ptr->toStr().c_str() + 1);
fprintf(yyout, "\t.type %s , %%function\n",
this->sym_ptr->toStr().c_str() + 1);
fprintf(yyout, "%s:\n", this->sym_ptr->toStr().c_str() + 1);
// TODO
/* Hint:
* 1. Save fp
* 2. fp = sp
* 3. Save callee saved register
* 4. Allocate stack space for local variable */
// Traverse all the block in block_list to print assembly code.
auto fp = new MachineOperand(MachineOperand::REG, 11);
auto sp = new MachineOperand(MachineOperand::REG, 13);
auto lr = new MachineOperand(MachineOperand::REG, 14);
(new StackMInstrcuton(nullptr, StackMInstrcuton::PUSH, getSavedRegs(), fp, lr))->output();
(new MovMInstruction(nullptr, MovMInstruction::MOV, fp, sp))->output();
int off = getAllocSpace();
auto size = new MachineOperand(MachineOperand::IMM, off);
if (off > VALID_MAX || off < -VALID_MAX)
{
auto r4 = new MachineOperand(MachineOperand::REG, 4);
(new LoadMInstruction(nullptr, r4, size))->output();
(new BinaryMInstruction(nullptr, BinaryMInstruction::SUB, sp, sp, r4))->output();
}
else
{
(new BinaryMInstruction(nullptr, BinaryMInstruction::SUB, sp, sp, size))->output();
}
int instCount = 0;
for (auto iter : block_list)
{
iter->output();
instCount += iter->getSize();
if(instCount > FUNC_INSTS_MAX)
{
fprintf(yyout, "\tb .F%d\n", parent->getGlobalNum());
fprintf(yyout, ".LTORG\n");
parent->printGlobalBridge();
fprintf(yyout, ".F%d:\n", parent->getGlobalNum()-1);
instCount = 0;
}
}
fprintf(yyout, "\n");
}
std::vector<MachineOperand*> MachineFunction::getSavedRegs()
{
std::vector<MachineOperand*> regs;
for (auto it = saved_regs.begin(); it != saved_regs.end(); it++)
{
auto reg = new MachineOperand(MachineOperand::REG, *it);
regs.push_back(reg);
}
return regs;
}
void MachineUnit::printGlobalBridge()
{
for (auto s : global_decl_list)
{
IdentifierSymbolEntry* se = (IdentifierSymbolEntry*)s;
fprintf(yyout, "addr_%s%d:\n", se->toStr().c_str(), globalNum);
fprintf(yyout, "\t.word %s\n", se->toStr().c_str());
}
globalNum++;
}
void MachineUnit::PrintGlobalDecl()
{
std::vector<int> zerosIndecies;
std::vector<int> constIndecies;
if (!global_decl_list.empty())
fprintf(yyout, "\t.data\n");
for (int i = 0; i < (int)global_decl_list.size(); i++)
{
IdentifierSymbolEntry* se = (IdentifierSymbolEntry*)global_decl_list[i];
if (se->isConst())
constIndecies.push_back(i);
else if (se->isAllZero())
zerosIndecies.push_back(i);
else
{
fprintf(yyout, "\t.global %s\n", se->toStr().c_str());
fprintf(yyout, "\t.align 4\n");
fprintf(yyout, "\t.size %s, %d\n", se->toStr().c_str(), se->getType()->getSize() / 8);
fprintf(yyout, "%s:\n", se->toStr().c_str());
if (se->getType()->isArray()) // 12/10 若为数组
{
int totalNum = se->getType()->getSize() / TypeSystem::intType->getSize();
int *arrayValue = se->getArrayValue();
for (int i = 0; i < totalNum; i++)
fprintf(yyout, "\t.word %d\n", arrayValue[i]); // 12/10 数组元素输出
}
else
fprintf(yyout, "\t.word %d\n", se->getValue());
}
}
if (!zerosIndecies.empty())
{
for (auto i : zerosIndecies)
{
IdentifierSymbolEntry* se = (IdentifierSymbolEntry*)global_decl_list[i];
if (se->getType()->isArray())
fprintf(yyout, "\t.comm %s, %d, 4\n", se->toStr().c_str(), se->getType()->getSize() / 8);
}
}
if (!constIndecies.empty())
{
fprintf(yyout, "\t.section .rodata\n");
for (auto i : constIndecies)
{
IdentifierSymbolEntry* se = (IdentifierSymbolEntry*)global_decl_list[i];
fprintf(yyout, "\t.global %s\n", se->toStr().c_str());
fprintf(yyout, "\t.align 4\n");
fprintf(yyout, "\t.size %s, %d\n", se->toStr().c_str(), se->getType()->getSize() / 8);
fprintf(yyout, "%s:\n", se->toStr().c_str());
if (se->getType()->isArray()) // 12/10 若为数组
{
int totalNum = se->getType()->getSize() / TypeSystem::intType->getSize();
int *arrayValue = se->getArrayValue();
for (int i = 0; i < totalNum; i++)
fprintf(yyout, "\t.word %d\n", arrayValue[i]); // 12/10 数组元素输出
}
else
fprintf(yyout, "\t.word %d\n", se->getValue());
}
}
}
void MachineUnit::output()
{
// TODO
/* Hint:
* 1. You need to print global variable/const declarition code;
* 2. Traverse all the function in func_list to print assembly code;
* 3. Don't forget print bridge label at the end of assembly code!! */
fprintf(yyout, "\t.arch armv8-a\n");
fprintf(yyout, "\t.arch_extension crc\n");
fprintf(yyout, "\t.arm\n");
PrintGlobalDecl();
fprintf(yyout, "\t.text\n");
for (auto iter : func_list)
iter->output();
printGlobalBridge();
}
void MachineUnit::insertGlobal(SymbolEntry* se)
{
global_decl_list.push_back(se);
}