Repository navigation
Expand file tree
/
Copy pathIR.cpp
More file actions
177 lines (163 loc) · 5.59 KB
/
Copy pathIR.cpp
File metadata and controls
177 lines (163 loc) · 5.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
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
#include "IR.h"
#include <sstream>
std::string irTypeToString(IRType type) {
switch (type) {
case IRType::Void: return "void";
case IRType::I8: return "i8";
case IRType::I32: return "i32";
case IRType::Ptr: return "ptr";
}
return "void";
}
std::string irOpcodeToString(IROpcode op) {
switch (op) {
case IROpcode::Alloca: return "alloca";
case IROpcode::Load: return "load";
case IROpcode::Store: return "store";
case IROpcode::GetElementPtr: return "gep";
case IROpcode::Add: return "add";
case IROpcode::Sub: return "sub";
case IROpcode::Mul: return "mul";
case IROpcode::Div: return "sdiv";
case IROpcode::Mod: return "smod";
case IROpcode::And: return "and";
case IROpcode::Or: return "or";
case IROpcode::Xor: return "xor";
case IROpcode::Shl: return "shl";
case IROpcode::Shr: return "shr";
case IROpcode::CmpEQ: return "cmp_eq";
case IROpcode::CmpNE: return "cmp_ne";
case IROpcode::CmpLT: return "cmp_lt";
case IROpcode::CmpLE: return "cmp_le";
case IROpcode::CmpGT: return "cmp_gt";
case IROpcode::CmpGE: return "cmp_ge";
case IROpcode::Neg: return "neg";
case IROpcode::Not: return "not";
case IROpcode::BitNot: return "bitnot";
case IROpcode::Cast: return "cast";
case IROpcode::Branch: return "br";
case IROpcode::CondBranch: return "condbr";
case IROpcode::Return: return "ret";
case IROpcode::Call: return "call";
}
return "unknown";
}
std::string IROperand::toString() const {
switch (kind) {
case Kind::ConstantInt:
return std::to_string(intValue);
case Kind::ConstantString:
return "@" + name;
case Kind::Register:
return "%t" + std::to_string(regNum);
case Kind::GlobalVar:
return "@" + name;
case Kind::None:
return "none";
}
return "none";
}
std::string IRInstruction::toString() const {
std::ostringstream ss;
if (!dest.isNone() && opcode != IROpcode::Store && opcode != IROpcode::Branch &&
opcode != IROpcode::CondBranch && opcode != IROpcode::Return) {
ss << dest.toString() << " = ";
}
ss << irOpcodeToString(opcode) << " ";
if (type != IRType::Void) {
ss << irTypeToString(type) << " ";
}
switch (opcode) {
case IROpcode::Alloca:
ss << "(bytes=" << allocSize << ")";
break;
case IROpcode::Load:
ss << src1.toString();
break;
case IROpcode::Store:
ss << src1.toString() << ", " << dest.toString();
break;
case IROpcode::GetElementPtr:
ss << src1.toString() << ", offset=" << offset;
break;
case IROpcode::Add:
case IROpcode::Sub:
case IROpcode::Mul:
case IROpcode::Div:
case IROpcode::Mod:
case IROpcode::And:
case IROpcode::Or:
case IROpcode::Xor:
case IROpcode::Shl:
case IROpcode::Shr:
case IROpcode::CmpEQ:
case IROpcode::CmpNE:
case IROpcode::CmpLT:
case IROpcode::CmpLE:
case IROpcode::CmpGT:
case IROpcode::CmpGE:
ss << src1.toString() << ", " << src2.toString();
break;
case IROpcode::Neg:
case IROpcode::Not:
case IROpcode::BitNot:
case IROpcode::Cast:
ss << src1.toString();
break;
case IROpcode::Branch:
ss << "label %" << targetLabel;
break;
case IROpcode::CondBranch:
ss << src1.toString() << ", label %" << targetLabel << ", label %" << falseLabel;
break;
case IROpcode::Return:
if (!src1.isNone()) {
ss << src1.toString();
}
break;
case IROpcode::Call:
ss << "@" << targetLabel << "(";
for (size_t i = 0; i < args.size(); ++i) {
if (i > 0) ss << ", ";
ss << args[i].toString();
}
ss << ")";
break;
}
return ss.str();
}
std::shared_ptr<IRBasicBlock> IRFunction::createBlock(const std::string& prefix) {
std::string bname = prefix + "_" + std::to_string(nextBlockId++);
auto block = std::make_shared<IRBasicBlock>(bname);
blocks.push_back(block);
return block;
}
void IRFunction::addBlock(std::shared_ptr<IRBasicBlock> block) {
blocks.push_back(std::move(block));
}
std::shared_ptr<IRFunction> IRModule::findFunction(const std::string& name) const {
for (const auto& fn : functions) {
if (fn->getName() == name) return fn;
}
return nullptr;
}
const IRGlobalVar* IRModule::findGlobal(const std::string& name) const {
for (const auto& g : globals) {
if (g.name == name) return &g;
}
return nullptr;
}
const IRStringConstant* IRModule::findString(const std::string& label) const {
for (const auto& s : strings) {
if (s.label == label) return &s;
}
return nullptr;
}
std::string IRModule::allocateStringLiteral(const std::string& value) {
for (const auto& s : strings) {
if (s.value == value) return s.label;
}
std::string label = "str." + std::to_string(nextStringId++);
strings.push_back({label, value});
return label;
}