Complete Lab2 IR generation and document process
This commit is contained in:
@@ -52,7 +52,9 @@ Instruction::Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name)
|
||||
|
||||
Opcode Instruction::GetOpcode() const { return opcode_; }
|
||||
|
||||
bool Instruction::IsTerminator() const { return opcode_ == Opcode::Ret; }
|
||||
bool Instruction::IsTerminator() const {
|
||||
return opcode_ == Opcode::Ret || opcode_ == Opcode::Br;
|
||||
}
|
||||
|
||||
BasicBlock* Instruction::GetParent() const { return parent_; }
|
||||
|
||||
@@ -61,22 +63,9 @@ void Instruction::SetParent(BasicBlock* parent) { parent_ = parent; }
|
||||
BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
|
||||
Value* rhs, std::string name)
|
||||
: Instruction(op, std::move(ty), std::move(name)) {
|
||||
if (op != Opcode::Add) {
|
||||
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 Add"));
|
||||
}
|
||||
if (!lhs || !rhs) {
|
||||
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
|
||||
}
|
||||
if (!type_ || !lhs->GetType() || !rhs->GetType()) {
|
||||
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少类型信息"));
|
||||
}
|
||||
if (lhs->GetType()->GetKind() != rhs->GetType()->GetKind() ||
|
||||
type_->GetKind() != lhs->GetType()->GetKind()) {
|
||||
throw std::runtime_error(FormatError("ir", "BinaryInst 类型不匹配"));
|
||||
}
|
||||
if (!type_->IsInt32()) {
|
||||
throw std::runtime_error(FormatError("ir", "BinaryInst 当前只支持 i32"));
|
||||
}
|
||||
AddOperand(lhs);
|
||||
AddOperand(rhs);
|
||||
}
|
||||
@@ -85,38 +74,85 @@ Value* BinaryInst::GetLhs() const { return GetOperand(0); }
|
||||
|
||||
Value* BinaryInst::GetRhs() const { return GetOperand(1); }
|
||||
|
||||
ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
|
||||
: Instruction(Opcode::Ret, std::move(void_ty), "") {
|
||||
if (!val) {
|
||||
throw std::runtime_error(FormatError("ir", "ReturnInst 缺少返回值"));
|
||||
BranchInst::BranchInst(BasicBlock* dest)
|
||||
: Instruction(Opcode::Br, Type::GetVoidType(), "") {
|
||||
AddOperand(dest);
|
||||
}
|
||||
|
||||
BranchInst::BranchInst(Value* cond, BasicBlock* if_true, BasicBlock* if_false)
|
||||
: Instruction(Opcode::Br, Type::GetVoidType(), "") {
|
||||
AddOperand(cond);
|
||||
AddOperand(if_true);
|
||||
AddOperand(if_false);
|
||||
}
|
||||
|
||||
bool BranchInst::IsConditional() const { return GetNumOperands() == 3; }
|
||||
|
||||
Value* BranchInst::GetCondition() const {
|
||||
return IsConditional() ? GetOperand(0) : nullptr;
|
||||
}
|
||||
|
||||
BasicBlock* BranchInst::GetIfTrue() const {
|
||||
return IsConditional() ? static_cast<BasicBlock*>(GetOperand(1)) : nullptr;
|
||||
}
|
||||
|
||||
BasicBlock* BranchInst::GetIfFalse() const {
|
||||
return IsConditional() ? static_cast<BasicBlock*>(GetOperand(2)) : nullptr;
|
||||
}
|
||||
|
||||
BasicBlock* BranchInst::GetDest() const {
|
||||
return !IsConditional() ? static_cast<BasicBlock*>(GetOperand(0)) : nullptr;
|
||||
}
|
||||
|
||||
CallInst::CallInst(Function* func, const std::vector<Value*>& args,
|
||||
std::string name)
|
||||
: Instruction(Opcode::Call, func->GetType(), std::move(name)) {
|
||||
AddOperand(func);
|
||||
for (auto* arg : args) {
|
||||
AddOperand(arg);
|
||||
}
|
||||
if (!type_ || !type_->IsVoid()) {
|
||||
throw std::runtime_error(FormatError("ir", "ReturnInst 返回类型必须为 void"));
|
||||
}
|
||||
|
||||
Function* CallInst::GetFunction() const {
|
||||
return static_cast<Function*>(GetOperand(0));
|
||||
}
|
||||
|
||||
GetElementPtrInst::GetElementPtrInst(std::shared_ptr<Type> ptr_ty, Value* ptr,
|
||||
const std::vector<Value*>& indices,
|
||||
std::string name)
|
||||
: Instruction(Opcode::GEP, std::move(ptr_ty), std::move(name)) {
|
||||
AddOperand(ptr);
|
||||
for (auto* idx : indices) {
|
||||
AddOperand(idx);
|
||||
}
|
||||
}
|
||||
|
||||
Value* GetElementPtrInst::GetPtr() const { return GetOperand(0); }
|
||||
|
||||
CastInst::CastInst(Opcode op, std::shared_ptr<Type> ty, Value* val,
|
||||
std::string name)
|
||||
: Instruction(op, std::move(ty), std::move(name)) {
|
||||
AddOperand(val);
|
||||
}
|
||||
|
||||
Value* ReturnInst::GetValue() const { return GetOperand(0); }
|
||||
Value* CastInst::GetValue() const { return GetOperand(0); }
|
||||
|
||||
AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
|
||||
: Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {
|
||||
if (!type_ || !type_->IsPtrInt32()) {
|
||||
throw std::runtime_error(FormatError("ir", "AllocaInst 当前只支持 i32*"));
|
||||
ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
|
||||
: Instruction(Opcode::Ret, std::move(void_ty), "") {
|
||||
if (val) {
|
||||
AddOperand(val);
|
||||
}
|
||||
}
|
||||
|
||||
Value* ReturnInst::GetValue() const {
|
||||
return GetNumOperands() > 0 ? GetOperand(0) : nullptr;
|
||||
}
|
||||
|
||||
AllocaInst::AllocaInst(std::shared_ptr<Type> ptr_ty, std::string name)
|
||||
: Instruction(Opcode::Alloca, std::move(ptr_ty), std::move(name)) {}
|
||||
|
||||
LoadInst::LoadInst(std::shared_ptr<Type> val_ty, Value* ptr, std::string name)
|
||||
: Instruction(Opcode::Load, std::move(val_ty), std::move(name)) {
|
||||
if (!ptr) {
|
||||
throw std::runtime_error(FormatError("ir", "LoadInst 缺少 ptr"));
|
||||
}
|
||||
if (!type_ || !type_->IsInt32()) {
|
||||
throw std::runtime_error(FormatError("ir", "LoadInst 当前只支持加载 i32"));
|
||||
}
|
||||
if (!ptr->GetType() || !ptr->GetType()->IsPtrInt32()) {
|
||||
throw std::runtime_error(
|
||||
FormatError("ir", "LoadInst 当前只支持从 i32* 加载"));
|
||||
}
|
||||
AddOperand(ptr);
|
||||
}
|
||||
|
||||
@@ -124,22 +160,6 @@ Value* LoadInst::GetPtr() const { return GetOperand(0); }
|
||||
|
||||
StoreInst::StoreInst(std::shared_ptr<Type> void_ty, Value* val, Value* ptr)
|
||||
: Instruction(Opcode::Store, std::move(void_ty), "") {
|
||||
if (!val) {
|
||||
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 value"));
|
||||
}
|
||||
if (!ptr) {
|
||||
throw std::runtime_error(FormatError("ir", "StoreInst 缺少 ptr"));
|
||||
}
|
||||
if (!type_ || !type_->IsVoid()) {
|
||||
throw std::runtime_error(FormatError("ir", "StoreInst 返回类型必须为 void"));
|
||||
}
|
||||
if (!val->GetType() || !val->GetType()->IsInt32()) {
|
||||
throw std::runtime_error(FormatError("ir", "StoreInst 当前只支持存储 i32"));
|
||||
}
|
||||
if (!ptr->GetType() || !ptr->GetType()->IsPtrInt32()) {
|
||||
throw std::runtime_error(
|
||||
FormatError("ir", "StoreInst 当前只支持写入 i32*"));
|
||||
}
|
||||
AddOperand(val);
|
||||
AddOperand(ptr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user