-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRISCVAsmPrinter.cpp
More file actions
467 lines (444 loc) · 19.3 KB
/
Copy pathRISCVAsmPrinter.cpp
File metadata and controls
467 lines (444 loc) · 19.3 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
/**
* @file RISCVAsmPrinter.cpp
* @brief RISC-V 汇编代码生成器实现文件
* @details 本文件实现了 RISC-V 架构的汇编代码生成器,包括全局变量、临时变量、函数和段的处理。
* 提供了将中间表示(IR)转换为 RISC-V 汇编代码的功能。
*/
#include "../include/backend/RISCVAsmPrinter.hpp"
SegmentType __oldtype=TEXT;
SegmentType* oldtype = &__oldtype;
SegmentType ChangeSegmentType(SegmentType newtype) {
return newtype;
}
void PrintSegmentType(SegmentType newtype, SegmentType* oldtype) {
if (newtype == *oldtype) return;
else {
*oldtype = ChangeSegmentType(newtype);
if (newtype == TEXT)
std::cout << " .text" << std::endl;
else if (newtype == DATA)
std::cout << " .data" << std::endl;
else if (newtype == BSS)
std::cout << " .bss" << std::endl;
else if (newtype == RODATA)
std::cout << " .section .rodata" << std::endl;
else
std::cout << "ERROR: Illegal SegmentType" << std::endl;
}
}
//AsmPrinter
RISCVAsmPrinter::RISCVAsmPrinter(std::string filename, Module* unit, RISCVLoweringContext& ctx)
: filename(filename){
dataSegment* data = new dataSegment(unit,ctx);
this->data = data;
}
void RISCVAsmPrinter::SetTextSegment(textSegment* _text) {text=_text;}
dataSegment* &RISCVAsmPrinter::GetData(){return data;}
void RISCVAsmPrinter::set_use_cachelookup(bool condi) {
use_cachelookup = condi;
}
void RISCVAsmPrinter::printAsmGlobal() {
std::cout << " .file \"" << filename << "\"" << std::endl;
std::cout << " .attribute arch, \"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0_zba1p0_zbb1p0\"" << std::endl;
std::cout << " .attribute unaligned_access, 0" << std::endl;
std::cout << " .attribute stack_align, 16" << std::endl;
std::cout << " .text" << std::endl;
this->data->PrintDataSegment_Globval();
}
void RISCVAsmPrinter::printCacheLookUp() {
// std::ifstream file(this->cachefilepath);
// if (!file.is_open()) {
// std::cout << "Error: Cannot open file " << this->cachefilepath << std::endl;
// return;
// }
// std::string line;
// while (std::getline(file, line)) {
// std::cout << line << std::endl;
// }
// file.close();
// return;
static const char* cachelookuplib =
#include "../include/RISCVSupport/cachelib.hpp"
;
std::cout << cachelookuplib;
}
void RISCVAsmPrinter::printParallelLib(){
static const char* buildinlib=
#include "../include/RISCVSupport/parallel.hpp"
;
std::cout<<buildinlib;
}
void RISCVAsmPrinter::printAsm() {
this->printAsmGlobal();
this->text->PrintTextSegment();
this->data->PrintDataSegment_Tempvar();
if(Singleton<Enable_Parallel>().flag==true)
this->printParallelLib();
if(this->use_cachelookup == true)
this->printCacheLookUp();
}
//textSegment
textSegment::textSegment(RISCVLoweringContext& ctx) {
GenerateFuncList(ctx);
}
void textSegment::GenerateFuncList(RISCVLoweringContext& ctx) {
for (auto& function : ctx.GetFunctions()) {
functionSegment* funcSeg = new functionSegment(function.get());
function_list.push_back(funcSeg);
}
}
void textSegment::PrintTextSegment() {
PrintSegmentType(TEXT, oldtype);
for (auto& functionSegment : function_list) {
functionSegment->PrintFuncSegment();
}
}
//functionSegment
functionSegment::functionSegment(RISCVFunction* function)
:func(function){
align = 1;
name = function->GetName();
size = -1;
}
void functionSegment::PrintFuncSegment() {
std::cout << " .align " << align << std::endl;
std::cout << " .globl " << name << std::endl;
std::cout << " .type " << name << ", @" << ty << std::endl;
func->printfull();
if(size == -1)
std::cout << " .size " << name << ", " << ".-" << name << std::endl;
}
//dataSegment
dataSegment::dataSegment(Module* module, RISCVLoweringContext& ctx) {
GenerateGloblvarList(module, ctx);
num_lable=0;
// GenerateTempvarList(module);
}
void dataSegment::GenerateGloblvarList(Module* module, RISCVLoweringContext& ctx) {
for(auto& data : module->GetGlobalVariable()) {
globlvar* gvar = new globlvar(data.get());
ctx.insert_val2mop(dynamic_cast<Value*>(data.get()), gvar);
globlvar_list.push_back(gvar);
}
}
void dataSegment::GenerateTempvarList(RISCVLoweringContext& ctx) {
return;
for (auto& function : ctx.GetFunctions()) {
for (auto block : *function) {
for(mylist<RISCVBasicBlock,RISCVMIR>::iterator it=block->begin();it!=block->end();++it) {
RISCVMIR* machineinst = *it;
if(machineinst->GetOperandSize()==0) {
continue;
}
//生成需要放在只读数据段的内容, 应该只有浮点常量
for(int i=0; i<machineinst->GetOperandSize(); i++) {
if(Imm* used = dynamic_cast<Imm*>(machineinst->GetOperand(i))) {
if (auto constfloat = dynamic_cast<ConstIRFloat*>(used->Getdata())) {
tempvar* tempfloat = nullptr;
for(int i=0; i<tempvar_list.size(); i++) {
if(tempvar_list[i]->GetInit() == constfloat->GetVal()) {
tempfloat = tempvar_list[i];
break;
}
}
if(tempfloat==nullptr) {
tempfloat = new tempvar(num_lable, constfloat->GetVal());
this->num_lable++;
tempvar_list.push_back(tempfloat);
}
machineinst->SetOperand(i,tempfloat);
//在代码中修改加载方式;
// Change_LoadConstFloat(machineinst, tempfloat, it, used);
}
}
}
}
}
}
}
std::vector<tempvar*> dataSegment::get_tempvar_list() {return tempvar_list;}
void dataSegment::Change_LoadConstFloat(RISCVMIR* inst, tempvar* tempfloat, mylist<RISCVBasicBlock,RISCVMIR>::iterator it, Imm* used) {
if(inst->GetOpcode() == RISCVMIR::call) {return;}
std::string opcode(magic_enum::enum_name(inst->GetOpcode()));
RISCVBasicBlock* block = inst->GetParent();
std::unique_ptr<RISCVFrame>& frame = block->GetParent()->GetFrame();
std::string name = tempfloat->Getname();
VirRegister* lui_rd = new VirRegister(RISCVType::riscv_ptr);
LARegister* lui_rs = new LARegister(RISCVType::riscv_ptr, name);
VirRegister* flw_rd = new VirRegister(RISCVType::riscv_float32);
LARegister* flw_rs = new LARegister(RISCVType::riscv_ptr, name, lui_rd);
RISCVMIR* lui = new RISCVMIR(RISCVMIR::RISCVISA::_lui);
lui->SetDef(lui_rd);
lui->AddOperand(lui_rs);
frame->AddCantBeSpill(lui_rd);
RISCVMIR* flw = new RISCVMIR(RISCVMIR::RISCVISA::_flw);
flw->SetDef(flw_rd);
flw->AddOperand(flw_rs);
it.insert_before(lui);
it.insert_before(flw);
for (int i=0; i<inst->GetOperandSize(); i++) {
while(inst->GetOperand(i) == used) {
inst->SetOperand(i,flw_rd);
}
}
}
void dataSegment::PrintDataSegment_Globval() {
for(auto& gvar : globlvar_list) {
gvar->PrintGloblvar();
}
}
void dataSegment::PrintDataSegment_Tempvar() {
for(auto& gvar : tempvar_list) {
gvar->PrintTempvar();
}
}
void dataSegment::LegalizeGloablVar(RISCVLoweringContext& ctx) {
using ISA = RISCVMIR::RISCVISA;
std::map<globlvar*, VirRegister*> attached_normal;
std::map<globlvar*, VirRegister*> attached_mem;
RISCVFunction* cur_func = ctx.GetCurFunction();
for(auto block: *cur_func) {
attached_normal.clear();
attached_mem.clear();
for(mylist<RISCVBasicBlock,RISCVMIR>::iterator it=block->begin();it!=block->end();++it) {
auto inst = *it;
for(int i=0; i<inst->GetOperandSize(); i++) {
if(globlvar* gvar = dynamic_cast<globlvar*>(inst->GetOperand(i))) {
std::unique_ptr<RISCVFrame>& frame = cur_func->GetFrame();
ISA opcode = inst->GetOpcode();
if(opcode == ISA::call) {continue;}
// lui .1, %hi(name)
// ld/sd .2, %lo(name)(.1)
if((opcode>ISA::BeginMem&&opcode<ISA::EndMem) || (opcode>ISA::BeginFloatMem&&opcode<ISA::EndFloatMem)) {
if(attached_mem.find(gvar)!=attached_mem.end()) {
LARegister* lo_lareg = new LARegister(RISCVType::riscv_ptr, gvar->GetName(),dynamic_cast<VirRegister*>(attached_mem[gvar]));
inst->SetOperand(i, lo_lareg);
}
else {
RISCVMIR* hi = new RISCVMIR(RISCVMIR::RISCVISA::_lui);
VirRegister* hi_vreg = ctx.createVReg(RISCVType::riscv_ptr);
frame->AddCantBeSpill(hi_vreg);
LARegister* hi_lareg = new LARegister(RISCVType::riscv_ptr, gvar->GetName());
hi->SetDef(hi_vreg);
hi->AddOperand(hi_lareg);
it.insert_before(hi);
LARegister* lo_lareg = new LARegister(RISCVType::riscv_ptr, gvar->GetName(),dynamic_cast<VirRegister*>(hi->GetDef()));
inst->SetOperand(i, lo_lareg);
attached_mem[gvar] = hi_vreg;
}
}
// lui .1, %hi(name)
// addi .2, %lo(name)
else {
if(attached_normal.find(gvar)!=attached_normal.end()) {
inst->SetOperand(i, attached_normal[gvar]);
}
else {
RISCVMIR* hi = new RISCVMIR(RISCVMIR::RISCVISA::_lui);
VirRegister* hi_vreg = ctx.createVReg(RISCVType::riscv_ptr);
frame->AddCantBeSpill(hi_vreg);
LARegister* hi_lareg = new LARegister(RISCVType::riscv_ptr, gvar->GetName());
hi->SetDef(hi_vreg);
hi->AddOperand(hi_lareg);
it.insert_before(hi);
RISCVMIR* lo = new RISCVMIR(ISA::_addi);
VirRegister* lo_vreg = ctx.createVReg(RISCVType::riscv_ptr);
frame->AddCantBeSpill(lo_vreg);
LARegister* lo_lareg = new LARegister(RISCVType::riscv_ptr, gvar->GetName(), LARegister::LAReg::lo);
lo->SetDef(lo_vreg);
lo->AddOperand(hi->GetDef());
lo->AddOperand(lo_lareg);
it.insert_before(lo);
inst->SetOperand(i, lo_vreg);
attached_normal[gvar] = lo_vreg;
}
}
}
}
}
}
}
//globlvar
globlvar::globlvar(Variable* data):RISCVGlobalObject(data->GetType(),data->GetName()){
InnerDataType tp = (dynamic_cast<PointerType*>(data->GetType()))->GetSubType()->GetTypeEnum();
if(tp == InnerDataType::IR_Value_INT || tp == InnerDataType::IR_Value_Float) {
align = 2;
size = 4;
if (data->GetInitializer()) {
// sec = "data";
if(tp == InnerDataType::IR_Value_INT) {
std::string num = data->GetInitializer()->GetName();
int init = std::stoi(num);
init_vector.push_back(init);
}
if(tp == InnerDataType::IR_Value_Float) {
ConstIRFloat* temp = dynamic_cast<ConstIRFloat*>(data->GetInitializer());
float init = temp->GetVal();
init_vector.push_back(init);
}
}
// else {
// sec = "bss";
// }
}
else if (tp == InnerDataType::IR_ARRAY) {
align = 3;
Type* basetype = dynamic_cast<HasSubType*>(data->GetType())->get_baseType();
if(Initializer* arry_init = dynamic_cast<Initializer*>(data->GetInitializer())) {
size = arry_init->GetType()->get_size();
int init_size = arry_init->size();
if (init_size == 0) {
// sec = "bss";
}
else {
// sec = "data";
int limi = dynamic_cast<ArrayType*>(arry_init->GetType())->GetNumEle();
for(int i=0;i<limi;i++){
if(i < init_size){
if(auto inits=dynamic_cast<Initializer*>((*arry_init)[i])) {
//递归
generate_array_init(inits, basetype);
}
else {//Leaf
if(basetype->GetTypeEnum() == IR_Value_INT) {
std::string num = (*arry_init)[i]->GetName();
int init = std::stoi(num);
init_vector.push_back(init);
}
else if (basetype->GetTypeEnum() == IR_Value_Float) {
ConstIRFloat* temp = dynamic_cast<ConstIRFloat*>((*arry_init)[i]);
float init = temp->GetVal();
init_vector.push_back(init);
}
}
}
else {
Type* temptp = dynamic_cast<ArrayType*>(arry_init->GetType())->GetSubType();
size_t zeronum = temptp->get_size() / basetype->get_size();
for(int i=0; i<zeronum; i++) {
if(basetype->GetTypeEnum() == IR_Value_INT) {
init_vector.push_back(static_cast<int>(0));
}
else if (basetype->GetTypeEnum() == IR_Value_Float) {
init_vector.push_back(static_cast<float>(0));
}
}
}
}
}
}
else {
// undefined arr;
size = (dynamic_cast<PointerType*>(data->GetType()))->GetSubType()->get_size();
// sec = "bss";
}
}
else align = -1;//Error
}
void globlvar::generate_array_init(Initializer* arry_init, Type* basetype) {
int init_size = arry_init->size();
int limi = dynamic_cast<ArrayType*>(arry_init->GetType())->GetNumEle();
if (init_size == 0) {
auto zero_num=arry_init->GetType()->get_size()/basetype->get_size();
for(auto i=0;i<zero_num;i++){
if(basetype->GetTypeEnum()==IR_Value_INT)init_vector.push_back(static_cast<int>(0));
else init_vector.push_back(static_cast<float>(0));
}
}
else {
for(int i=0; i<limi; i++) {
if(i<init_size) {
if(auto inits=dynamic_cast<Initializer*>((*arry_init)[i]))
generate_array_init(inits, basetype);
else {
if(basetype->GetTypeEnum() == IR_Value_INT) {
std::string num = (*arry_init)[i]->GetName();
int init = std::stoi(num);
init_vector.push_back(init);
}
else if (basetype->GetTypeEnum() == IR_Value_Float) {
ConstIRFloat* temp = dynamic_cast<ConstIRFloat*>((*arry_init)[i]);
float init = temp->GetVal();
init_vector.push_back(init);
}
}
}
else {
Type* temptp = dynamic_cast<ArrayType*>(arry_init->GetType())->GetSubType();
size_t zeronum = temptp->get_size() / basetype->get_size();
for(int i=0; i<zeronum; i++) {
if (basetype->GetTypeEnum() == IR_Value_INT) {
init_vector.push_back(static_cast<int>(0));
}
else if (basetype->GetTypeEnum() == IR_Value_Float) {
init_vector.push_back(static_cast<float>(0));
}
}
}
}
}
}
void globlvar::PrintGloblvar() {
if (init_vector.empty()) {
std::cout << " .globl " << this->GetName() << std::endl;
PrintSegmentType(BSS, oldtype);
std::cout << " .align " << align << std::endl;
std::cout << " .type " << this->GetName() << ", @" << ty << std::endl;
std::cout << " .size " << this->GetName() << ", " << size << std::endl;
std::cout << this->GetName() << ":" << std::endl;
std::cout << " .zero " << size << std::endl;
}
else {
std::cout << " .globl " << this->GetName() << std::endl;
PrintSegmentType(DATA, oldtype);
std::cout << " .align " << align << std::endl;
std::cout << " .type " << this->GetName() << ", @" << ty << std::endl;
std::cout << " .size " << this->GetName() << ", " << size << std::endl;
std::cout << this->GetName() << ":" << std::endl;
int zero_count=0;
for(auto& init : init_vector) {
bool is_zero = std::visit([](auto&& value){return value==0;}, init);
if(is_zero) {
zero_count+=4;
}
else {
if(zero_count!=0) {
std::cout << " .zero " << zero_count << std::endl;
zero_count = 0;
}
if(std::holds_alternative<int>(init)) {
std::cout << " .word " << std::get<int>(init) << std::endl;
}
else {
//float type
FloatBits bits;
bits.floatValue = std::get<float>(init);
std::string binaryString = std::bitset<32>(bits.intBits).to_string();
int decNum = binaryToDecimal(binaryString);
std::cout << " .word " << decNum << std::endl;
}
}
if (&init == &init_vector.back() && is_zero) {
std::cout << " .zero " << zero_count << std::endl;
}
}
}
}
//tempvar
tempvar::tempvar(int num_lable, float init) :
RISCVTempFloatObject("file"), num_lable(num_lable), align(2), init(init) {
this->GetName() = ".LC"+ std::to_string(num_lable);
}
std::string tempvar::Getname() {
return this->GetName();
}
void tempvar::PrintTempvar() {
PrintSegmentType(RODATA, oldtype);
std::cout << " .align " << align << std::endl;
std::cout << this->GetName() << ":" << std::endl;
FloatBits bits;
bits.floatValue = init;
std::string binaryString = std::bitset<32>(bits.intBits).to_string();
int decNum = binaryToDecimal(binaryString);
std::cout << " .word " << decNum << std::endl;
}