Revert "可以处理生成加法的IR"
This commit is contained in:
@@ -1 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR 基本块:
|
||||
// - 保存指令序列
|
||||
// - 维护或可计算前驱/后继关系,用于 CFG 分析与优化
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR 上下文:
|
||||
// - 管理类型与常量的创建/复用
|
||||
// - 保存字符串常量、符号等公共资源(按需要扩展)
|
||||
|
||||
|
||||
@@ -1,15 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR Function:
|
||||
// - 保存参数列表、基本块列表
|
||||
// - 记录函数属性/元信息(按需要扩展)
|
||||
|
||||
namespace ir {
|
||||
|
||||
Function::Function(std::string name)
|
||||
: Value(Type::Int32(), std::move(name)),
|
||||
entry_(std::make_unique<BasicBlock>("entry")) {}
|
||||
|
||||
void Function::EnsureEntry() {
|
||||
if (!entry_) {
|
||||
entry_ = std::make_unique<BasicBlock>("entry");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
144
src/ir/IR.h
144
src/ir/IR.h
@@ -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
|
||||
@@ -1,19 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR 构建工具:
|
||||
// - 管理插入点(当前基本块/位置)
|
||||
// - 提供创建各类指令的便捷接口,降低 IRGen 复杂度
|
||||
|
||||
namespace ir {
|
||||
|
||||
ConstantInt* IRBuilder::CreateConstInt(int v) {
|
||||
// 常量不需要挂在基本块里,直接返回局部对象指针。
|
||||
return new ConstantInt(v);
|
||||
}
|
||||
|
||||
BinaryInst* IRBuilder::CreateBinary(Opcode op, Value* lhs, Value* rhs,
|
||||
const std::string& name) {
|
||||
return insertBlock_->Append<BinaryInst>(op, Type::Int32(), lhs, rhs, name);
|
||||
}
|
||||
|
||||
ReturnInst* IRBuilder::CreateRet(Value* v) {
|
||||
return insertBlock_->Append<ReturnInst>(v);
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1,55 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR 文本输出:
|
||||
// - 将 IR 打印为 .ll 风格的文本
|
||||
// - 支撑调试与测试对比(diff)
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace ir {
|
||||
|
||||
static const char* OpcodeToString(Opcode op) {
|
||||
switch (op) {
|
||||
case Opcode::Add:
|
||||
return "add";
|
||||
case Opcode::Sub:
|
||||
return "sub";
|
||||
case Opcode::Mul:
|
||||
return "mul";
|
||||
case Opcode::Div:
|
||||
return "div";
|
||||
case Opcode::Ret:
|
||||
return "ret";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
void IRPrinter::Print(const Module& module) {
|
||||
for (const auto& func : module.functions()) {
|
||||
std::cout << "define i32 @" << func->name() << "() {\n";
|
||||
const auto* bb = func->entry();
|
||||
if (!bb) {
|
||||
std::cout << "}\n";
|
||||
continue;
|
||||
}
|
||||
for (const auto& instPtr : bb->instructions()) {
|
||||
const auto* inst = instPtr.get();
|
||||
switch (inst->opcode()) {
|
||||
case Opcode::Add:
|
||||
case Opcode::Sub:
|
||||
case Opcode::Mul:
|
||||
case Opcode::Div: {
|
||||
auto* bin = static_cast<const BinaryInst*>(inst);
|
||||
std::cout << " " << bin->name() << " = " << OpcodeToString(bin->opcode())
|
||||
<< " " << bin->lhs()->name() << ", " << bin->rhs()->name()
|
||||
<< "\n";
|
||||
break;
|
||||
}
|
||||
case Opcode::Ret: {
|
||||
auto* ret = static_cast<const ReturnInst*>(inst);
|
||||
std::cout << " ret " << ret->value()->name() << "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << "}\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1,12 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR 指令体系:
|
||||
// - 二元运算/比较、load/store、call、br/condbr、ret、phi、alloca 等
|
||||
// - 指令操作数与结果类型管理,支持打印与优化
|
||||
|
||||
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) {}
|
||||
|
||||
ReturnInst::ReturnInst(Value* val)
|
||||
: Instruction(Opcode::Ret, Type::Void(), ""), value_(val) {}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// IR Module:
|
||||
// - 保存全局变量与函数列表
|
||||
// - 维护与目标相关的模块级信息(如需要)与符号表
|
||||
|
||||
namespace ir {
|
||||
|
||||
Function* Module::CreateFunction(const std::string& name) {
|
||||
functions_.push_back(std::make_unique<Function>(name));
|
||||
return functions_.back().get();
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1,16 +1,4 @@
|
||||
// 极简类型系统:仅支持 void 与 i32。
|
||||
#include "ir/IR.h"
|
||||
// IR 类型系统:
|
||||
// - i32/f32/void、指针、数组、函数类型等
|
||||
// - 按 SysY 支持范围裁剪并逐步补齐
|
||||
|
||||
namespace ir {
|
||||
|
||||
std::shared_ptr<Type> Type::Void() {
|
||||
static auto ty = std::make_shared<Type>(Kind::Void);
|
||||
return ty;
|
||||
}
|
||||
|
||||
std::shared_ptr<Type> Type::Int32() {
|
||||
static auto ty = std::make_shared<Type>(Kind::Int32);
|
||||
return ty;
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
#include "ir/IR.h"
|
||||
// SSA 值体系抽象:
|
||||
// - 常量、参数、指令结果等统一为 Value
|
||||
// - 提供类型信息与使用/被使用关系(按需要实现)
|
||||
|
||||
namespace ir {
|
||||
|
||||
ConstantInt::ConstantInt(int v) : Value(Type::Int32(), ""), value_(v) {
|
||||
set_name(std::to_string(v));
|
||||
}
|
||||
|
||||
} // namespace ir
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// 支配树分析:
|
||||
// - 构建/查询 Dominator Tree 及相关关系
|
||||
// - 为 mem2reg、CFG 优化与循环分析提供基础能力
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// 循环分析:
|
||||
// - 识别循环结构与层级关系
|
||||
// - 为后续优化(可选)提供循环信息
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// CFG 简化:
|
||||
// - 删除不可达块、合并空块、简化分支等
|
||||
// - 改善 IR 结构,便于后续优化与后端生成
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// IR 常量折叠:
|
||||
// - 折叠可判定的常量表达式
|
||||
// - 简化常量控制流分支(按实现范围裁剪)
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// 死代码删除(DCE):
|
||||
// - 删除无用指令与无用基本块
|
||||
// - 通常与 CFG 简化配合使用
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// Mem2Reg(SSA 构造):
|
||||
// - 将局部变量的 alloca/load/store 提升为 SSA 形式
|
||||
// - 插入 PHI 并重写使用,依赖支配树等分析
|
||||
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
#include <vector>
|
||||
// IR Pass 管理:
|
||||
// - 按优化级别组织优化 pipeline
|
||||
// - 统一运行 pass、统计与调试输出(按需要扩展)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user