-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.cpp
More file actions
89 lines (81 loc) · 2.59 KB
/
Copy pathFunction.cpp
File metadata and controls
89 lines (81 loc) · 2.59 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
#include "Function.h"
#include "Unit.h"
#include "Type.h"
#include <list>
extern FILE* yyout;
Function::Function(Unit *u, SymbolEntry *s)
{
u->insertFunc(this);
entry = new BasicBlock(this);
sym_ptr = s;
parent = u;
}
Function::~Function()
{
// auto delete_list = block_list;
// for (auto &i : delete_list)
// delete i;
// parent->removeFunc(this);
}
// remove the basicblock bb from its block_list.
void Function::remove(BasicBlock *bb)
{
block_list.erase(std::find(block_list.begin(), block_list.end(), bb));
}
void Function::output() const {
FunctionType* funcType = dynamic_cast<FunctionType*>(sym_ptr->getType());
Type* retType = funcType->getRetType();
std::vector<SymbolEntry*> paramsSe = funcType->getParamsSe();
if (!paramsSe.size())
fprintf(yyout, "define %s %s() {\n", retType->toStr().c_str(),
sym_ptr->toStr().c_str());
else {
fprintf(yyout, "define %s %s(", retType->toStr().c_str(),
sym_ptr->toStr().c_str());
for (long unsigned int i = 0; i < paramsSe.size(); i++) {
if (i)
fprintf(yyout, ", ");
fprintf(yyout, "%s %s", paramsSe[i]->getType()->toStr().c_str(),
paramsSe[i]->toStr().c_str());
}
fprintf(yyout, ") {\n");
}
std::set<BasicBlock*> v;
std::list<BasicBlock*> q;
q.push_back(entry);
v.insert(entry);
while (!q.empty()) {
auto bb = q.front();
q.pop_front();
bb->output();
for (auto succ = bb->succ_begin(); succ != bb->succ_end(); succ++) {
if (v.find(*succ) == v.end()) {
v.insert(*succ);
q.push_back(*succ);
}
}
}
fprintf(yyout, "}\n");
}
void Function::genMachineCode(AsmBuilder* builder)
{
auto cur_unit = builder->getUnit();
auto cur_func = new MachineFunction(cur_unit, this->sym_ptr);
builder->setFunction(cur_func);
std::map<BasicBlock*, MachineBlock*> map;
for(auto block : block_list)
{
block->genMachineCode(builder);
map[block] = builder->getBlock();
}
// Add pred and succ for every block
for(auto block : block_list)
{
auto mblock = map[block];
for (auto pred = block->pred_begin(); pred != block->pred_end(); pred++)
mblock->addPred(map[*pred]);
for (auto succ = block->succ_begin(); succ != block->succ_end(); succ++)
mblock->addSucc(map[*succ]);
}
cur_unit->InsertFunc(cur_func);
}