Repository navigation
Expand file tree
/
Copy pathIRPrinter.cpp
More file actions
73 lines (64 loc) · 2.06 KB
/
Copy pathIRPrinter.cpp
File metadata and controls
73 lines (64 loc) · 2.06 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
#include "IRPrinter.h"
void IRPrinter::print(const IRModule& module) {
out << "; --- IR Module ---\n\n";
// Print string constants
if (!module.getStrings().empty()) {
out << "; String Literals:\n";
for (const auto& s : module.getStrings()) {
out << "@" << s.label << " = constant c\"";
for (char c : s.value) {
if (c == '\n') out << "\\n";
else if (c == '\t') out << "\\t";
else if (c == '\r') out << "\\r";
else if (c == '\\') out << "\\\\";
else if (c == '\"') out << "\\\"";
else out << c;
}
out << "\\0\"\n";
}
out << "\n";
}
// Print global variables
if (!module.getGlobals().empty()) {
out << "; Global Variables:\n";
for (const auto& g : module.getGlobals()) {
out << "@" << g.name << " = global " << irTypeToString(g.type);
if (g.hasInit) {
out << " " << g.initValue;
} else {
out << " zeroinitializer";
}
out << ", size=" << g.sizeBytes << "\n";
}
out << "\n";
}
// Print functions
for (const auto& fn : module.getFunctions()) {
print(*fn);
out << "\n";
}
}
void IRPrinter::print(const IRFunction& fn) {
out << "define " << irTypeToString(fn.getReturnType()) << " @" << fn.getName() << "(";
const auto& pNames = fn.getParamNames();
const auto& pTypes = fn.getParamTypes();
for (size_t i = 0; i < pNames.size(); ++i) {
if (i > 0) out << ", ";
out << irTypeToString(pTypes[i]) << " %" << pNames[i];
}
out << ") {\n";
for (const auto& block : fn.getBlocks()) {
print(*block);
}
out << "}\n";
}
void IRPrinter::print(const IRBasicBlock& block) {
out << block.getName() << ":\n";
for (const auto& inst : block.getInstructions()) {
out << " ";
print(inst);
}
}
void IRPrinter::print(const IRInstruction& inst) {
out << inst.toString() << "\n";
}