Revert "可以处理生成加法的IR"

This commit is contained in:
jing
2025-12-29 15:49:30 +08:00
parent e941cced9b
commit 8903bf73f9
42 changed files with 158 additions and 919 deletions

View File

@@ -1,144 +0,0 @@
// 极简 IR 定义:足以表示 int 返回 a+b。
#pragma once
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace ir {
class Type {
public:
enum class Kind { Void, Int32 };
explicit Type(Kind k) : kind_(k) {}
Kind kind() const { return kind_; }
static std::shared_ptr<Type> Void();
static std::shared_ptr<Type> Int32();
private:
Kind kind_;
};
class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name)
: type_(std::move(ty)), name_(std::move(name)) {}
virtual ~Value() = default;
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); }
protected:
std::shared_ptr<Type> type_;
std::string name_;
};
class ConstantInt : public Value {
public:
explicit ConstantInt(int v);
int value() const { return value_; }
private:
int value_{};
};
enum class Opcode { Add, Sub, Mul, Div, Ret };
class Instruction : public Value {
public:
Instruction(Opcode op, std::shared_ptr<Type> ty, std::string name = "")
: Value(std::move(ty), std::move(name)), opcode_(op) {}
Opcode opcode() const { return opcode_; }
private:
Opcode opcode_;
};
class BinaryInst : public Instruction {
public:
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* lhs() const { return lhs_; }
Value* rhs() const { return rhs_; }
private:
Value* lhs_;
Value* rhs_;
};
class ReturnInst : public Instruction {
public:
explicit ReturnInst(Value* val);
Value* value() const { return value_; }
private:
Value* value_;
};
class BasicBlock {
public:
explicit BasicBlock(std::string name) : name_(std::move(name)) {}
const std::string& name() const { return name_; }
const std::vector<std::unique_ptr<Instruction>>& instructions() const {
return instructions_;
}
template <typename T, typename... Args>
T* Append(Args&&... args) {
auto inst = std::make_unique<T>(std::forward<Args>(args)...);
auto* ptr = inst.get();
instructions_.push_back(std::move(inst));
return ptr;
}
private:
std::string name_;
std::vector<std::unique_ptr<Instruction>> instructions_;
};
class Function : public Value {
public:
explicit Function(std::string name);
BasicBlock* entry() { return entry_.get(); }
const BasicBlock* entry() const { return entry_.get(); }
void EnsureEntry();
private:
std::unique_ptr<BasicBlock> entry_;
};
class Module {
public:
Function* CreateFunction(const std::string& name);
const std::vector<std::unique_ptr<Function>>& functions() const {
return functions_;
}
private:
std::vector<std::unique_ptr<Function>> functions_;
};
class IRBuilder {
public:
explicit IRBuilder(BasicBlock* bb) : insertBlock_(bb) {}
void SetInsertPoint(BasicBlock* bb) { insertBlock_ = bb; }
BasicBlock* GetInsertBlock() const { return insertBlock_; }
ConstantInt* CreateConstInt(int v);
BinaryInst* CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name) {
return CreateBinary(Opcode::Add, lhs, rhs, name);
}
ReturnInst* CreateRet(Value* v);
private:
BasicBlock* insertBlock_;
};
class IRPrinter {
public:
void Print(const Module& module);
};
} // namespace ir