docs(doc): lab5, lab6文档

This commit is contained in:
jing
2026-03-09 20:36:26 +08:00
parent e01995a33d
commit 0d3e4795ee
5 changed files with 364 additions and 14 deletions

View File

@@ -12,6 +12,7 @@ namespace ir {
class Type;
class ConstantInt;
class Instruction;
// IR 上下文:集中管理类型、常量等共享资源,便于复用与扩展。
class Context {
@@ -56,10 +57,13 @@ class Value {
const std::shared_ptr<Type>& type() const { return type_; }
const std::string& name() const { return name_; }
void set_name(std::string n) { name_ = std::move(n); }
void AddUser(Instruction* user) { users_.push_back(user); }
const std::vector<Instruction*>& users() const { return users_; }
protected:
std::shared_ptr<Type> type_;
std::string name_;
std::vector<Instruction*> users_;
};
class ConstantInt : public Value {

View File

@@ -7,18 +7,40 @@ namespace ir {
BinaryInst::BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs,
Value* rhs, std::string name)
: Instruction(op, std::move(ty), std::move(name)), lhs_(lhs), rhs_(rhs) {}
: Instruction(op, std::move(ty), std::move(name)), lhs_(lhs), rhs_(rhs) {
if (lhs_) {
lhs_->AddUser(this);
}
if (rhs_) {
rhs_->AddUser(this);
}
}
ReturnInst::ReturnInst(Value* val)
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {}
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {
if (value_) {
value_->AddUser(this);
}
}
AllocaInst::AllocaInst(std::string name)
: Instruction(Opcode::Alloca, Type::PtrInt32(), std::move(name)) {}
LoadInst::LoadInst(Value* ptr, std::string name)
: Instruction(Opcode::Load, Type::Int32(), std::move(name)), ptr_(ptr) {}
: Instruction(Opcode::Load, Type::Int32(), std::move(name)), ptr_(ptr) {
if (ptr_) {
ptr_->AddUser(this);
}
}
StoreInst::StoreInst(Value* val, Value* ptr)
: Instruction(Opcode::Store, Type::Void(), ""), value_(val), ptr_(ptr) {}
: Instruction(Opcode::Store, Type::Void(), ""), value_(val), ptr_(ptr) {
if (value_) {
value_->AddUser(this);
}
if (ptr_) {
ptr_->AddUser(this);
}
}
} // namespace ir