refactor(ir): ir改为更标准的实现
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
// - 提供类型信息与使用/被使用关系(按需要实现)
|
||||
#include "ir/IR.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace ir {
|
||||
|
||||
Value::Value(std::shared_ptr<Type> ty, std::string name)
|
||||
@@ -14,11 +16,68 @@ const std::string& Value::GetName() const { return name_; }
|
||||
|
||||
void Value::SetName(std::string n) { name_ = std::move(n); }
|
||||
|
||||
void Value::AddUser(Instruction* user) { users_.push_back(user); }
|
||||
bool Value::IsVoid() const { return type_ && type_->IsVoid(); }
|
||||
|
||||
const std::vector<Instruction*>& Value::GetUsers() const { return users_; }
|
||||
bool Value::IsInt32() const { return type_ && type_->IsInt32(); }
|
||||
|
||||
bool Value::IsPtrInt32() const { return type_ && type_->IsPtrInt32(); }
|
||||
|
||||
bool Value::IsConstant() const {
|
||||
return dynamic_cast<const ConstantValue*>(this) != nullptr;
|
||||
}
|
||||
|
||||
bool Value::IsInstruction() const {
|
||||
return dynamic_cast<const Instruction*>(this) != nullptr;
|
||||
}
|
||||
|
||||
bool Value::IsUser() const {
|
||||
return dynamic_cast<const User*>(this) != nullptr;
|
||||
}
|
||||
|
||||
bool Value::IsFunction() const {
|
||||
return dynamic_cast<const Function*>(this) != nullptr;
|
||||
}
|
||||
|
||||
void Value::AddUse(User* user, size_t operand_index) {
|
||||
if (!user) return;
|
||||
uses_.push_back(Use(this, user, operand_index));
|
||||
}
|
||||
|
||||
void Value::RemoveUse(User* user, size_t operand_index) {
|
||||
uses_.erase(
|
||||
std::remove_if(uses_.begin(), uses_.end(),
|
||||
[&](const Use& use) {
|
||||
return use.GetUser() == user &&
|
||||
use.GetOperandIndex() == operand_index;
|
||||
}),
|
||||
uses_.end());
|
||||
}
|
||||
|
||||
const std::vector<Use>& Value::GetUses() const { return uses_; }
|
||||
|
||||
void Value::ReplaceAllUsesWith(Value* new_value) {
|
||||
if (!new_value) {
|
||||
throw std::runtime_error("ReplaceAllUsesWith 缺少 new_value");
|
||||
}
|
||||
if (new_value == this) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto uses = uses_;
|
||||
for (const auto& use : uses) {
|
||||
auto* user = use.GetUser();
|
||||
if (!user) continue;
|
||||
size_t operand_index = use.GetOperandIndex();
|
||||
if (user->GetOperand(operand_index) == this) {
|
||||
user->SetOperand(operand_index, new_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ConstantValue::ConstantValue(std::shared_ptr<Type> ty, std::string name)
|
||||
: Value(std::move(ty), std::move(name)) {}
|
||||
|
||||
ConstantInt::ConstantInt(std::shared_ptr<Type> ty, int v)
|
||||
: Value(std::move(ty), ""), value_(v) {}
|
||||
: ConstantValue(std::move(ty), ""), value_(v) {}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
Reference in New Issue
Block a user