Complete Lab2 IR generation and document process

This commit is contained in:
2026-04-16 00:21:35 +08:00
parent 6fc0c89072
commit 979d271ebe
23 changed files with 2583 additions and 471 deletions

View File

@@ -37,6 +37,7 @@
#include <unordered_map>
#include <utility>
#include <vector>
#include <cstdint>
namespace ir {
@@ -45,6 +46,7 @@ class Value;
class User;
class ConstantValue;
class ConstantInt;
class ConstantFloat;
class GlobalValue;
class Instruction;
class BasicBlock;
@@ -83,17 +85,20 @@ class Context {
~Context();
// 去重创建 i32 常量。
ConstantInt* GetConstInt(int v);
// 去重创建 float 常量。
ConstantFloat* GetConstFloat(float v);
std::string NextTemp();
private:
std::unordered_map<int, std::unique_ptr<ConstantInt>> const_ints_;
std::unordered_map<float, std::unique_ptr<ConstantFloat>> const_floats_;
int temp_index_ = -1;
};
class Type {
class Type : public std::enable_shared_from_this<Type> {
public:
enum class Kind { Void, Int32, PtrInt32 };
enum class Kind { Void, Int32, PtrInt32, Float, PtrFloat, Label, Array };
explicit Type(Kind k);
// 使用静态共享对象获取类型。
// 同一类型可直接比较返回值是否相等,例如:
@@ -101,15 +106,36 @@ class Type {
static const std::shared_ptr<Type>& GetVoidType();
static const std::shared_ptr<Type>& GetInt32Type();
static const std::shared_ptr<Type>& GetPtrInt32Type();
static const std::shared_ptr<Type>& GetFloatType();
static const std::shared_ptr<Type>& GetPtrFloatType();
static const std::shared_ptr<Type>& GetLabelType();
Kind GetKind() const;
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
bool IsFloat() const;
bool IsPtrFloat() const;
bool IsLabel() const;
bool IsArray() const;
std::shared_ptr<class ArrayType> GetAsArrayType();
private:
Kind kind_;
};
class ArrayType : public Type {
public:
ArrayType(std::shared_ptr<Type> element_type, uint32_t num_elements);
static std::shared_ptr<ArrayType> Get(std::shared_ptr<Type> element_type,
uint32_t num_elements);
std::shared_ptr<Type> GetElementType() const { return element_type_; }
uint32_t GetNumElements() const { return num_elements_; }
private:
std::shared_ptr<Type> element_type_;
uint32_t num_elements_;
};
class Value {
public:
Value(std::shared_ptr<Type> ty, std::string name);
@@ -120,10 +146,15 @@ class Value {
bool IsVoid() const;
bool IsInt32() const;
bool IsPtrInt32() const;
bool IsFloat() const;
bool IsPtrFloat() const;
bool IsLabel() const;
bool IsConstant() const;
bool IsInstruction() const;
bool IsUser() const;
bool IsFunction() const;
bool IsGlobalValue() const;
bool IsArgument() const;
void AddUse(User* user, size_t operand_index);
void RemoveUse(User* user, size_t operand_index);
const std::vector<Use>& GetUses() const;
@@ -135,6 +166,19 @@ class Value {
std::vector<Use> uses_;
};
// Argument represents a function parameter.
class Argument : public Value {
public:
Argument(std::shared_ptr<Type> ty, std::string name, Function* parent,
unsigned arg_no);
Function* GetParent() const { return parent_; }
unsigned GetArgNo() const { return arg_no_; }
private:
Function* parent_;
unsigned arg_no_;
};
// ConstantValue 是常量体系的基类。
// 当前只实现了 ConstantInt后续可继续扩展更多常量种类。
class ConstantValue : public Value {
@@ -151,8 +195,49 @@ class ConstantInt : public ConstantValue {
int value_{};
};
class ConstantFloat : public ConstantValue {
public:
ConstantFloat(std::shared_ptr<Type> ty, float v);
float GetValue() const { return value_; }
private:
float value_{};
};
// 后续还需要扩展更多指令类型。
enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
enum class Opcode {
Add,
Sub,
Mul,
Div,
Mod,
FAdd,
FSub,
FMul,
FDiv,
ICmpEQ,
ICmpNE,
ICmpLT,
ICmpGT,
ICmpLE,
ICmpGE,
FCmpEQ,
FCmpNE,
FCmpLT,
FCmpGT,
FCmpLE,
FCmpGE,
Alloca,
Load,
Store,
Ret,
Br,
Call,
GEP,
ZExt,
SIToFP,
FPToSI
};
// User 是所有“会使用其他 Value 作为输入”的 IR 对象的抽象基类。
// 当前实现中只有 Instruction 继承自 User。
@@ -171,11 +256,15 @@ class User : public Value {
std::vector<Value*> operands_;
};
// GlobalValue 是全局值/全局变量体系的空壳占位类。
// 当前只补齐类层次,具体初始化器、打印和链接语义后续再补。
// GlobalValue 是全局值/全局变量体系的类。
class GlobalValue : public User {
public:
GlobalValue(std::shared_ptr<Type> ty, std::string name);
GlobalValue(std::shared_ptr<Type> ty, std::string name, ConstantValue* init = nullptr);
ConstantValue* GetInitializer() const { return init_; }
void SetInitializer(ConstantValue* init) { init_ = init; }
private:
ConstantValue* init_ = nullptr;
};
class Instruction : public User {
@@ -196,7 +285,40 @@ class BinaryInst : public Instruction {
BinaryInst(Opcode op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
Value* GetLhs() const;
Value* GetRhs() const;
Value* GetRhs() const;
};
class BranchInst : public Instruction {
public:
// Unconditional branch
explicit BranchInst(BasicBlock* dest);
// Conditional branch
BranchInst(Value* cond, BasicBlock* if_true, BasicBlock* if_false);
bool IsConditional() const;
Value* GetCondition() const;
BasicBlock* GetIfTrue() const;
BasicBlock* GetIfFalse() const;
BasicBlock* GetDest() const;
};
class CallInst : public Instruction {
public:
CallInst(Function* func, const std::vector<Value*>& args, std::string name = "");
Function* GetFunction() const;
};
class GetElementPtrInst : public Instruction {
public:
GetElementPtrInst(std::shared_ptr<Type> ptr_ty, Value* ptr,
const std::vector<Value*>& indices, std::string name = "");
Value* GetPtr() const;
};
class CastInst : public Instruction {
public:
CastInst(Opcode op, std::shared_ptr<Type> ty, Value* val, std::string name = "");
Value* GetValue() const;
};
class ReturnInst : public Instruction {
@@ -255,38 +377,41 @@ class BasicBlock : public Value {
};
// Function 当前也采用了最小实现。
// 需要特别注意:由于项目里还没有单独的 FunctionType
// Function 继承自 Value 后,其 type_ 目前只保存“返回类型”,
// 并不能完整表达“返回类型 + 形参列表”这一整套函数签名。
// 这对当前只支持 int main() 的最小 IR 足够,但后续若补普通函数、
// 形参和调用,通常需要引入专门的函数类型表示。
class Function : public Value {
public:
// 当前构造函数接收的也是返回类型,而不是完整函数类型。
Function(std::string name, std::shared_ptr<Type> ret_type);
Function(std::string name, std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types);
BasicBlock* CreateBlock(const std::string& name);
BasicBlock* GetEntry();
const BasicBlock* GetEntry() const;
const std::vector<std::unique_ptr<BasicBlock>>& GetBlocks() const;
const std::vector<std::unique_ptr<Argument>>& GetArguments() const;
private:
BasicBlock* entry_ = nullptr;
std::vector<std::unique_ptr<BasicBlock>> blocks_;
std::vector<std::unique_ptr<Argument>> arguments_;
};
class Module {
public:
Module() = default;
Context& GetContext();
const Context& GetContext() const;
// 创建函数时当前只显式传入返回类型,尚未接入完整的 FunctionType。
Function* CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type);
std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types = {});
const std::vector<std::unique_ptr<Function>>& GetFunctions() const;
GlobalValue* CreateGlobalValue(const std::string& name,
std::shared_ptr<Type> ty,
ConstantValue* init = nullptr);
const std::vector<std::unique_ptr<GlobalValue>>& GetGlobalValues() const;
private:
Context context_;
std::vector<std::unique_ptr<Function>> functions_;
std::vector<std::unique_ptr<GlobalValue>> global_values_;
};
class IRBuilder {
@@ -297,13 +422,41 @@ class IRBuilder {
// 构造常量、二元运算、返回指令的最小集合。
ConstantInt* CreateConstInt(int v);
ConstantFloat* CreateConstFloat(float v);
BinaryInst* CreateBinary(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateAdd(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateSub(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateMul(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateDiv(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateMod(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFAdd(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFSub(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFMul(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateFDiv(Value* lhs, Value* rhs, const std::string& name);
BinaryInst* CreateICmp(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
BinaryInst* CreateFCmp(Opcode op, Value* lhs, Value* rhs,
const std::string& name);
AllocaInst* CreateAlloca(std::shared_ptr<Type> ty, const std::string& name);
AllocaInst* CreateAllocaI32(const std::string& name);
LoadInst* CreateLoad(Value* ptr, const std::string& name);
StoreInst* CreateStore(Value* val, Value* ptr);
ReturnInst* CreateRet(Value* v);
BranchInst* CreateBr(BasicBlock* dest);
BranchInst* CreateCondBr(Value* cond, BasicBlock* if_true,
BasicBlock* if_false);
CallInst* CreateCall(Function* func, const std::vector<Value*>& args,
const std::string& name = "");
GetElementPtrInst* CreateGEP(std::shared_ptr<Type> ptr_ty, Value* ptr,
const std::vector<Value*>& indices,
const std::string& name = "");
CastInst* CreateZExt(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
CastInst* CreateSIToFP(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
CastInst* CreateFPToSI(Value* val, std::shared_ptr<Type> ty,
const std::string& name = "");
private:
Context& ctx_;

View File

@@ -5,8 +5,10 @@
#include <any>
#include <memory>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include "SysYBaseVisitor.h"
#include "SysYParser.h"
@@ -18,24 +20,56 @@ class Module;
class Function;
class IRBuilder;
class Value;
class BasicBlock;
}
class IRGenImpl final : public SysYBaseVisitor {
public:
IRGenImpl(ir::Module& module, const SemanticContext& sema);
// Top-level rules
std::any visitCompUnit(SysYParser::CompUnitContext* ctx) override;
std::any visitDecl(SysYParser::DeclContext* ctx) override;
std::any visitConstDecl(SysYParser::ConstDeclContext* ctx) override;
std::any visitVarDecl(SysYParser::VarDeclContext* ctx) override;
std::any visitConstDef(SysYParser::ConstDefContext* ctx) override;
std::any visitVarDef(SysYParser::VarDefContext* ctx) override;
std::any visitFuncDef(SysYParser::FuncDefContext* ctx) override;
std::any visitFuncFParam(SysYParser::FuncFParamContext* ctx) override;
// Statement rules
std::any visitBlockStmt(SysYParser::BlockStmtContext* ctx) override;
std::any visitBlockItem(SysYParser::BlockItemContext* ctx) override;
std::any visitDecl(SysYParser::DeclContext* ctx) override;
std::any visitStmt(SysYParser::StmtContext* ctx) override;
std::any visitVarDef(SysYParser::VarDefContext* ctx) override;
std::any visitAssignStmt(SysYParser::AssignStmtContext* ctx) override;
std::any visitReturnStmt(SysYParser::ReturnStmtContext* ctx) override;
std::any visitIfStmt(SysYParser::IfStmtContext* ctx) override;
std::any visitWhileStmt(SysYParser::WhileStmtContext* ctx) override;
std::any visitBreakStmt(SysYParser::BreakStmtContext* ctx) override;
std::any visitContinueStmt(SysYParser::ContinueStmtContext* ctx) override;
std::any visitExpStmt(SysYParser::ExpStmtContext* ctx) override;
// Expression rules
std::any visitParenExp(SysYParser::ParenExpContext* ctx) override;
std::any visitLValueExp(SysYParser::LValueExpContext* ctx) override;
std::any visitNumberExp(SysYParser::NumberExpContext* ctx) override;
std::any visitVarExp(SysYParser::VarExpContext* ctx) override;
std::any visitAdditiveExp(SysYParser::AdditiveExpContext* ctx) override;
std::any visitFuncCallExp(SysYParser::FuncCallExpContext* ctx) override;
std::any visitNotExp(SysYParser::NotExpContext* ctx) override;
std::any visitUnaryAddExp(SysYParser::UnaryAddExpContext* ctx) override;
std::any visitUnarySubExp(SysYParser::UnarySubExpContext* ctx) override;
std::any visitMulExp(SysYParser::MulExpContext* ctx) override;
std::any visitDivExp(SysYParser::DivExpContext* ctx) override;
std::any visitModExp(SysYParser::ModExpContext* ctx) override;
std::any visitAddExp(SysYParser::AddExpContext* ctx) override;
std::any visitSubExp(SysYParser::SubExpContext* ctx) override;
std::any visitLtExp(SysYParser::LtExpContext* ctx) override;
std::any visitLeExp(SysYParser::LeExpContext* ctx) override;
std::any visitGtExp(SysYParser::GtExpContext* ctx) override;
std::any visitGeExp(SysYParser::GeExpContext* ctx) override;
std::any visitEqExp(SysYParser::EqExpContext* ctx) override;
std::any visitNeExp(SysYParser::NeExpContext* ctx) override;
std::any visitAndExp(SysYParser::AndExpContext* ctx) override;
std::any visitOrExp(SysYParser::OrExpContext* ctx) override;
private:
enum class BlockFlow {
@@ -43,15 +77,35 @@ class IRGenImpl final : public SysYBaseVisitor {
Terminated,
};
BlockFlow VisitBlockItemResult(SysYParser::BlockItemContext& item);
ir::Value* EvalExpr(SysYParser::ExpContext& expr);
ir::ConstantValue* EvalConstExpr(SysYParser::ExpContext& expr);
ir::Value* GetLValuePtr(SysYParser::LValueContext* ctx);
ir::Value* DecayArrayPtr(SysYParser::LValueContext* ctx);
bool IsArrayLikeDef(antlr4::ParserRuleContext* def) const;
size_t GetArrayRank(antlr4::ParserRuleContext* def) const;
std::shared_ptr<ir::Type> GetDefType(antlr4::ParserRuleContext* def) const;
void ZeroInitializeLocal(ir::Value* ptr, std::shared_ptr<ir::Type> ty);
void EmitLocalInitValue(ir::Value* ptr, std::shared_ptr<ir::Type> ty,
SysYParser::InitValueContext* init);
ir::Module& module_;
const SemanticContext& sema_;
ir::Function* func_;
ir::IRBuilder builder_;
// 名称绑定由 Sema 负责IRGen 只维护“声明 -> 存储槽位”的代码生成状态。
std::unordered_map<SysYParser::VarDefContext*, ir::Value*> storage_map_;
// Maps a definition (VarDef, ConstDef, FuncFParam) to its IR value (Alloca or GlobalValue)
std::unordered_map<antlr4::ParserRuleContext*, ir::Value*> storage_map_;
// For global scope tracking
bool is_global_scope_ = true;
// For loop control
std::stack<ir::BasicBlock*> break_stack_;
std::stack<ir::BasicBlock*> continue_stack_;
// Helper to handle short-circuiting and comparison results
ir::Value* ToI1(ir::Value* v);
ir::Value* ToI32(ir::Value* v);
};
std::unique_ptr<ir::Module> GenerateIR(SysYParser::CompUnitContext& tree,

View File

@@ -1,30 +1,40 @@
// 基于语法树的语义检查与名称绑定。
#pragma once
#include <unordered_map>
#include <vector>
#include "SysYParser.h"
class SemanticContext {
public:
void BindVarUse(SysYParser::VarContext* use,
SysYParser::VarDefContext* decl) {
var_uses_[use] = decl;
void BindLValue(SysYParser::LValueContext* use,
antlr4::ParserRuleContext* def) {
lvalue_defs_[use] = def;
}
SysYParser::VarDefContext* ResolveVarUse(
const SysYParser::VarContext* use) const {
auto it = var_uses_.find(use);
return it == var_uses_.end() ? nullptr : it->second;
void BindFuncCall(SysYParser::FuncCallExpContext* use,
SysYParser::FuncDefContext* def) {
funccall_defs_[use] = def;
}
antlr4::ParserRuleContext* ResolveLValue(
const SysYParser::LValueContext* use) const {
auto it = lvalue_defs_.find(const_cast<SysYParser::LValueContext*>(use));
return it == lvalue_defs_.end() ? nullptr : it->second;
}
SysYParser::FuncDefContext* ResolveFuncCall(
const SysYParser::FuncCallExpContext* use) const {
auto it = funccall_defs_.find(const_cast<SysYParser::FuncCallExpContext*>(use));
return it == funccall_defs_.end() ? nullptr : it->second;
}
private:
std::unordered_map<const SysYParser::VarContext*,
SysYParser::VarDefContext*>
var_uses_;
std::unordered_map<SysYParser::LValueContext*, antlr4::ParserRuleContext*>
lvalue_defs_;
std::unordered_map<SysYParser::FuncCallExpContext*,
SysYParser::FuncDefContext*>
funccall_defs_;
};
// 目前仅检查:
// - 变量先声明后使用
// - 局部变量不允许重复定义
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);

View File

@@ -1,17 +1,30 @@
// 极简符号表:记录局部变量定义点。
#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "SysYParser.h"
struct Symbol {
enum class Kind { Variable, Constant, Function, Parameter };
Kind kind;
antlr4::ParserRuleContext* def_ctx;
bool is_const = false;
bool is_array = false;
// For functions, we can store pointers to their parameter types or just the
// FuncDefContext*
};
class SymbolTable {
public:
void Add(const std::string& name, SysYParser::VarDefContext* decl);
bool Contains(const std::string& name) const;
SysYParser::VarDefContext* Lookup(const std::string& name) const;
SymbolTable();
void PushScope();
void PopScope();
bool Add(const std::string& name, const Symbol& symbol);
Symbol* Lookup(const std::string& name);
bool IsInCurrentScope(const std::string& name) const;
private:
std::unordered_map<std::string, SysYParser::VarDefContext*> table_;
std::vector<std::unordered_map<std::string, Symbol>> scopes_;
};