forked from ppxf25tqu/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
// IR Function
|
|
#include "ir/IR.h"
|
|
|
|
namespace ir {
|
|
|
|
Function::Function(std::string name, std::shared_ptr<Type> ret_type)
|
|
: Value(std::move(ret_type), std::move(name)) {
|
|
entry_ = CreateBlock("entry");
|
|
}
|
|
|
|
BasicBlock* Function::CreateBlock(const std::string& name) {
|
|
auto block = std::make_unique<BasicBlock>(name);
|
|
auto* ptr = block.get();
|
|
ptr->SetParent(this);
|
|
blocks_.push_back(std::move(block));
|
|
if (!entry_) entry_ = ptr;
|
|
return ptr;
|
|
}
|
|
|
|
void Function::MoveBlockToEnd(BasicBlock* bb) {
|
|
for (size_t i = 0; i < blocks_.size(); ++i) {
|
|
if (blocks_[i].get() == bb) {
|
|
auto tmp = std::move(blocks_[i]);
|
|
blocks_.erase(blocks_.begin() + i);
|
|
blocks_.push_back(std::move(tmp));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
BasicBlock* Function::GetEntry() { return entry_; }
|
|
const BasicBlock* Function::GetEntry() const { return entry_; }
|
|
const std::vector<std::unique_ptr<BasicBlock>>& Function::GetBlocks() const {
|
|
return blocks_;
|
|
}
|
|
|
|
Argument* Function::AddArgument(std::shared_ptr<Type> ty,
|
|
const std::string& name) {
|
|
args_.push_back(std::make_unique<Argument>(std::move(ty), name));
|
|
return args_.back().get();
|
|
}
|
|
|
|
Argument* Function::GetArgument(size_t i) const {
|
|
if (i >= args_.size()) return nullptr;
|
|
return args_[i].get();
|
|
}
|
|
|
|
} // namespace ir
|