|
|
|
|
@ -53,47 +53,47 @@ void IRPrinter::Print(const Module& module) {
|
|
|
|
|
for (const auto& func : module.functions()) {
|
|
|
|
|
std::cout << "define " << TypeToString(*func->type()) << " @"
|
|
|
|
|
<< func->name() << "() {\n";
|
|
|
|
|
const auto* bb = func->entry();
|
|
|
|
|
if (!bb) {
|
|
|
|
|
std::cout << "}\n";
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
std::cout << "entry:\n";
|
|
|
|
|
for (const auto& instPtr : bb->instructions()) {
|
|
|
|
|
const auto* inst = instPtr.get();
|
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
|
case Opcode::Add:
|
|
|
|
|
case Opcode::Sub:
|
|
|
|
|
case Opcode::Mul: {
|
|
|
|
|
auto* bin = static_cast<const BinaryInst*>(inst);
|
|
|
|
|
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
|
|
|
|
|
<< " " << TypeToString(*bin->lhs()->type()) << " "
|
|
|
|
|
<< ValueToString(bin->lhs()) << ", "
|
|
|
|
|
<< ValueToString(bin->rhs()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Alloca: {
|
|
|
|
|
auto* alloca = static_cast<const AllocaInst*>(inst);
|
|
|
|
|
std::cout << " " << alloca->name() << " = alloca i32\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Load: {
|
|
|
|
|
auto* load = static_cast<const LoadInst*>(inst);
|
|
|
|
|
std::cout << " " << load->name() << " = load i32, i32* "
|
|
|
|
|
<< ValueToString(load->ptr()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Store: {
|
|
|
|
|
auto* store = static_cast<const StoreInst*>(inst);
|
|
|
|
|
std::cout << " store i32 " << ValueToString(store->value()) << ", i32* "
|
|
|
|
|
<< ValueToString(store->ptr()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Ret: {
|
|
|
|
|
auto* ret = static_cast<const ReturnInst*>(inst);
|
|
|
|
|
std::cout << " ret " << TypeToString(*ret->value()->type()) << " "
|
|
|
|
|
<< ValueToString(ret->value()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
for (const auto& bb : func->blocks()) {
|
|
|
|
|
if (!bb) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
std::cout << bb->name() << ":\n";
|
|
|
|
|
for (const auto& instPtr : bb->instructions()) {
|
|
|
|
|
const auto* inst = instPtr.get();
|
|
|
|
|
switch (inst->opcode()) {
|
|
|
|
|
case Opcode::Add:
|
|
|
|
|
case Opcode::Sub:
|
|
|
|
|
case Opcode::Mul: {
|
|
|
|
|
auto* bin = static_cast<const BinaryInst*>(inst);
|
|
|
|
|
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
|
|
|
|
|
<< " " << TypeToString(*bin->lhs()->type()) << " "
|
|
|
|
|
<< ValueToString(bin->lhs()) << ", "
|
|
|
|
|
<< ValueToString(bin->rhs()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Alloca: {
|
|
|
|
|
auto* alloca = static_cast<const AllocaInst*>(inst);
|
|
|
|
|
std::cout << " " << alloca->name() << " = alloca i32\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Load: {
|
|
|
|
|
auto* load = static_cast<const LoadInst*>(inst);
|
|
|
|
|
std::cout << " " << load->name() << " = load i32, i32* "
|
|
|
|
|
<< ValueToString(load->ptr()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Store: {
|
|
|
|
|
auto* store = static_cast<const StoreInst*>(inst);
|
|
|
|
|
std::cout << " store i32 " << ValueToString(store->value()) << ", i32* "
|
|
|
|
|
<< ValueToString(store->ptr()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case Opcode::Ret: {
|
|
|
|
|
auto* ret = static_cast<const ReturnInst*>(inst);
|
|
|
|
|
std::cout << " ret " << TypeToString(*ret->value()->type()) << " "
|
|
|
|
|
<< ValueToString(ret->value()) << "\n";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|