Shrink:IR-change-1

Pomelo
Shrink 2 weeks ago
parent 192b8004ed
commit 477720eb5e

@ -0,0 +1,12 @@
## Lab1
# 1.构建
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCOMPILER_PARSE_ONLY=ON
cmake --build build -j "$(nproc)"
# 2.单例查看
./build/bin/compiler --emit-parse-tree test/test_case/functional/simple_add.sy
# 3.批量检查
find test/test_case -name '*.sy' | sort | while read f; do ./build/bin/compiler --emit-parse-tree "$f" >/dev/null || echo "FAIL $f"; done

@ -20,6 +20,8 @@ Lab2 的目标是在该示例基础上扩展语义覆盖范围,并逐步把更
- `include/sem/SymbolTable.h`
- `src/sem/Sema.cpp`
- `src/sem/SymbolTable.cpp`
- `include/ir/IR.h`
- `src/ir/Context.cpp`
- `src/ir/Value.cpp`

@ -0,0 +1,353 @@
# Lab2 小组协作修改方案(可直接执行)
## 1. 目标与现状
当前仓库的最小实现只能覆盖:
1. 局部 int 变量的 alloca/load/store。
2. 整数字面量、变量引用、加法表达式。
3. return 语句。
4. 单函数 main 的最小流程。
Lab2 的目标是把语法覆盖扩展到课程要求范围,并通过 IR 验证链路。
统一验收标准:
1. 能生成结构正确的 IR。
2. 通过脚本运行并和 .out 比对一致。
3. 最终覆盖 test/test_case 下应测样例。
---
## 2. 团队分工建议
建议至少拆成 4 条工作线并行推进。
### A. IR 基础设施组(你负责)
负责文件:
- include/ir/IR.h
- src/ir/Instruction.cpp
- src/ir/IRBuilder.cpp
- src/ir/Type.cpp
- src/ir/BasicBlock.cpp
- src/ir/Function.cpp
- src/ir/Module.cpp
- src/ir/IRPrinter.cpp
职责:
1. 补齐 IR 指令与构建接口(算术、比较、分支、调用、内存访问等)。
2. 保证基本块终结规则与 use-def 关系一致。
3. 保证 IRPrinter 输出格式可被 llc/clang 工具链接受。
### B. 语义分析组Sema
负责文件:
- include/sem/Sema.h
- include/sem/SymbolTable.h
- src/sem/Sema.cpp
- src/sem/SymbolTable.cpp
- src/sem/ConstEval.cpp
职责:
1. 名称绑定、作用域管理、重复定义与未定义检查。
2. 常量表达式求值与 const 相关约束。
3. 为 IRGen 提供稳定的绑定结果。
### C. IR 生成组IRGen
负责文件:
- include/irgen/IRGen.h
- src/irgen/IRGenFunc.cpp
- src/irgen/IRGenStmt.cpp
- src/irgen/IRGenExp.cpp
- src/irgen/IRGenDecl.cpp
- src/irgen/IRGenDriver.cpp
职责:
1. 按语法树节点生成 IR。
2. 正确构造控制流图if/while/break/continue
3. 正确处理函数定义、调用、参数、局部变量与数组访问。
### D. 测试与集成组
负责内容:
- 脚本化回归。
- 失败样例归因grammar/sema/irgen/ir/printer 哪层出错)。
- 每日合并后 smoke test。
---
## 3. 必须先对齐的接口约定(第一天完成)
这一步不做,后面会高频返工。
### 3.1 Sema -> IRGen 约定
1. 变量使用节点如何唯一绑定到声明节点。
2. 函数符号如何绑定(函数重名、参数信息、返回类型)。
3. 块作用域遮蔽规则:同名变量按最近作用域优先。
### 3.2 IRGen -> IRBuilder 约定
1. 每类语句/表达式映射到哪些构建接口。
2. 基本块终结规则ret/br 后禁止继续在同块插指令。
3. 比较和逻辑运算的返回类型与约定值。
4. 调用约定参数顺序、返回值处理、void 调用行为。
### 3.3 IR -> IRPrinter 约定
1. 新增指令的打印语法。
2. 类型打印规则int/float/ptr/array/function
3. 全局对象、函数声明与定义的输出顺序。
### 3.4 IR 第一版接口清单(函数名级别,建议 Day1 冻结)
这份清单用于你先做 src/ir其他同学按接口跟随开发。
第一批只覆盖 M1 到 M3 的最小闭环M4 和 M5 另开第二版接口。
1. 当前已存在并继续保留
- IRBuilder.CreateConstInt(int v)
- IRBuilder.CreateBinary(Opcode op, Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateAdd(Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateAllocaI32(const std::string& name)
- IRBuilder.CreateLoad(Value* ptr, const std::string& name)
- IRBuilder.CreateStore(Value* val, Value* ptr)
- IRBuilder.CreateRet(Value* v)
2. 第一版必须新增M1
- Opcode 扩展Div、Mod
- IRBuilder.CreateSub(Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateMul(Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateDiv(Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateMod(Value* lhs, Value* rhs, const std::string& name)
- 一元负号先复用 CreateSub(0, x)
- 一元逻辑非先复用比较接口(见下一条)
3. 第一版建议新增M1 到 M3
- CmpOp 枚举Eq、Ne、Lt、Le、Gt、Ge
- IRBuilder.CreateCmp(CmpOp op, Value* lhs, Value* rhs, const std::string& name)
- IRBuilder.CreateBr(BasicBlock* target)
- IRBuilder.CreateCondBr(Value* cond, BasicBlock* true_bb, BasicBlock* false_bb)
4. 第二版预留M2
- IRBuilder.CreateCall(Function* callee, const std::vector<Value*>& args, const std::string& name)
- Function 需要完整函数签名表示(返回类型 + 形参类型)
5. 基本块终结规则(第一版必须执行)
1. terminator 指令包含 Ret、Br、CondBr。
2. BasicBlock 一旦出现 terminator禁止继续追加普通指令。
3. IRGen 负责在新块上重新设置插入点。
6. 参数与类型约定(第一版)
1. M1 到 M3 阶段先统一为 i32 值语义。
2. cond 分支条件统一约定为 i320 为假,非 0 为真。
3. CreateBinary 输入输出类型必须一致;不一致直接报错。
7. 打印约定IRPrinter 第一版)
1. Sub、Mul、Div、Mod、比较、Br、CondBr、Call 必须同步可打印。
2. 新增指令的打印格式由 IR 组给出单页示例IRGen 组按示例比对。
8. 接口冻结与变更流程
1. Day1 晚上冻结第一版接口,不再改函数签名。
2. Day2 到 Day3 只允许修实现 bug不改接口名字和参数。
3. 必须改接口时,提前半天发迁移说明并提供一条替代写法。
---
## 4. 分阶段里程碑(按功能从低风险到高风险)
## M1整数基础表达式与赋值
目标样例:
- test/test_case/functional/simple_add.sy
- test/test_case/functional/11_add2.sy
- test/test_case/functional/13_sub2.sy
- test/test_case/functional/36_op_priority2.sy
完成定义:
1. 支持赋值语句。
2. 支持一元 + - !。
3. 支持二元 + - * / %。
4. 支持比较表达式的语义与 IR 生成。
## M2函数与作用域
目标样例:
- test/test_case/functional/09_func_defn.sy
- test/test_case/functional/25_scope3.sy
完成定义:
1. 多函数定义。
2. 参数传递与函数调用。
3. 块级作用域与变量遮蔽正确。
## M3控制流
目标样例:
- test/test_case/functional/29_break.sy
完成定义:
1. if/else。
2. while。
3. break/continue循环栈管理
## M4数组与全局对象
目标样例:
- test/test_case/functional/05_arr_defn4.sy
- test/test_case/functional/22_matrix_multiply.sy
完成定义:
1. 数组定义、初始化与下标访问。
2. 全局变量/常量。
3. constExp 维度与初始化相关检查。
## M5浮点若课程阶段要求
目标样例:
- test/test_case/functional/95_float.sy
完成定义:
1. float 类型与常量。
2. int/float 隐式转换。
3. float 运算、比较、控制流条件。
---
## 5. 你负责的 src/ir 详细任务清单
建议按下面顺序提交,每次只做一类能力,便于联调。
### T1. 指令枚举和 IRBuilder 基础扩展
1. 补齐一元/二元整数运算。
2. 补齐比较与条件分支指令。
3. 提供统一的 CreateBinary、CreateCmp、CreateBr、CreateCondBr、CreateCall。
### T2. 基本块与终结指令约束
1. BasicBlock 显式记录 terminator。
2. 插入终结指令后拒绝继续插普通指令。
3. Function 级别提供校验接口(可用于 debug 断言)。
### T3. 类型与函数签名表达
1. 类型系统支持函数参数和返回类型表达。
2. 调用点参数个数与签名一致性检查(至少 debug 模式可查)。
### T4. IRPrinter 同步
1. 新增指令全部可打印。
2. 打印结果可通过 llc。
3. 保证测试输出稳定(避免不必要随机命名波动)。
### T5. 数组与地址计算(如果 M4 开启)
1. 元素地址计算接口。
2. 多维下标线性化或 GEP 风格接口。
---
## 6. 每日协作机制(必须执行)
1. 每天固定 15 分钟站会:同步昨天完成、今天计划、阻塞点。
2. 每个接口变更先发讨论再改,避免主干反复冲突。
3. 每个里程碑前冻结接口半天,只做修 bug 和联调。
推荐沟通模板:
1. 我改了什么接口。
2. 旧行为与新行为差异。
3. 受影响文件。
4. 调用方需要同步的改动。
5. 最晚切换时间点。
---
## 7. 分支与提交流程
1. 一人一功能分支,不直接在主分支开发。
2. 提交粒度小而清晰:一个提交只解决一类问题。
3. 提交信息建议feat(ir), fix(irgen), fix(sema), test(lab2)。
4. 合并前必须附上通过样例列表。
---
## 8. 统一验证命令
构建:
```bash
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCOMPILER_PARSE_ONLY=OFF
cmake --build build -j "$(nproc)"
```
单样例验证:
```bash
./scripts/verify_ir.sh test/test_case/functional/simple_add.sy test/test_result/function/ir --run
```
功能样例批量验证:
```bash
for f in test/test_case/functional/*.sy; do
echo "== $f =="
./scripts/verify_ir.sh "$f" test/test_result/function/ir --run || break
done
```
---
## 9. 风险与应对
1. 风险grammar 变更导致 visitor 接口失配。
应对:变更 grammar 后第一时间重新生成 ANTLR 文件并通知 Sema/IRGen。
2. 风险IRPrinter 语法偏差导致 llc 失败。
应对:新增指令时同步补打印和最小样例回归。
3. 风险:组员并行修改同一接口冲突严重。
应对:接口 owner 机制IR 接口由 IR 组最终拍板。
4. 风险:只测 simple_add阶段性误判成功。
应对:每个里程碑绑定指定样例集,全部通过才进入下一阶段。
---
## 10. 本周可执行排期(示例)
1. Day1接口对齐会完成 M1 任务拆分。
2. Day2IR 组完成 T1/T2Sema/IRGen 同步接入。
3. Day3打通 M1 全样例。
4. Day4推进 M2函数与作用域
5. Day5推进 M3控制流并做一次集成回归。
如果进度顺利,再进入 M4数组与全局

@ -0,0 +1,108 @@
我先快速读取当前 IRGen 的入口与表达式/语句文件,然后按“节点到接口调用映射”给你一份可以直接交给 IRGen 同学实现的调用说明。
Read [](file:///home/shrink/nudt-compiler-cpp/include/ir/IR.h#320-320), lines 320 to 387
Read [](file:///home/shrink/nudt-compiler-cpp/include/irgen/IRGen.h)
Read [](file:///home/shrink/nudt-compiler-cpp/src/irgen/IRGenExp.cpp)
Read [](file:///home/shrink/nudt-compiler-cpp/src/irgen/IRGenStmt.cpp)
Read [](file:///home/shrink/nudt-compiler-cpp/src/irgen/IRGenFunc.cpp)
可以,下面给你一份面向 IRGen 同学的“如何调用你新增 IR 接口”的实现说明,按落地顺序写。
先看调用边界
1. IRGen 只负责把语法树翻译成 IR不做符号查找
变量和函数绑定依赖 Sema 结果,参考 IRGenExp.cpp 的变量读取流程。
2. 所有指令创建都通过 IRBuilder
新增接口定义在 IR.h。
3. 终结指令规则必须遵守
Ret/Br/CondBr 一旦插入当前块,这个块不能再插普通指令,规则在 Instruction.cpp 和 BasicBlock.cpp。
建议 IRGen 调用映射
一、表达式层
1. 加减乘除模
- a + b: CreateAdd(lhs, rhs, temp)
- a - b: CreateSub(lhs, rhs, temp)
- a * b: CreateMul(lhs, rhs, temp)
- a / b: CreateDiv(lhs, rhs, temp)
- a % b: CreateMod(lhs, rhs, temp)
2. 比较表达式
- == != < <= > >= 统一走 CreateCmp(op, lhs, rhs, temp)
- 返回 i32约定 0 为假,非 0 为真
3. 一元运算建议
- -x: CreateSub(const0, x, temp)
- !x: CreateCmp(Eq, x, const0, temp)
- +x: 直接返回 x
4. 变量读取和赋值
- 读取: 先由 Sema 绑定 use 到 decl再从 storage_map 找槽位CreateLoad
- 赋值: EvalExpr(rhs) 后对槽位 CreateStore(rhs_val, slot)
对应修改文件:
- IRGenExp.cpp
- IRGenDecl.cpp
- IRGenStmt.cpp
二、控制流层
1. if/else
- 创建三个块: then, else, merge
- cond = EvalExpr(condExp)
- 当前块插 CreateCondBr(cond, then, else)
- 切入 then 块生成语句,末尾若未终结则 CreateBr(merge)
- 切入 else 块同理
- 最后 SetInsertPoint(merge)
2. while
- 创建三个块: cond, body, exit
- 进入前先 CreateBr(cond)
- cond 块计算条件CreateCondBr(cond_val, body, exit)
- body 块生成循环体,若未终结则回跳 CreateBr(cond)
- 最后 SetInsertPoint(exit)
3. break/continue
- IRGen 维护循环栈,每层保存 continue_target 和 break_target
- break 直接 CreateBr(break_target)
- continue 直接 CreateBr(continue_target)
对应修改文件:
- IRGenStmt.cpp
三、函数与调用层
1. 函数定义
- 用 Module.CreateFunction(name, ret_type, param_types) 建函数
- 入口块 SetInsertPoint(entry)
- 形参落栈策略建议:每个参数先 alloca再 store 到本地槽位,后续统一按 load/store 访问
2. 函数调用
- 先拿到 callee 对应的 Function
- 实参逐个 EvalExpr 得到 args
- 调用 CreateCall(callee, args, temp)
- 若返回值非 void把返回 Value 继续参与表达式计算
对应修改文件:
- IRGenFunc.cpp
- IRGenExp.cpp
四、你要提醒 IRGen 同学的两个关键点
1. 插入点管理
每次生成 Br/CondBr/Ret 后,必须切换到新块再继续生成。否则会触发块已终结错误。
2. grammar 上下文名先对齐
你们当前 IRGen 里还在用 ParenExpContext、AdditiveExpContext 这类命名,实际 grammar 是另一套层级时要先改 Visitor 入口,否则接口再全也接不上。可先核对 SysY.g4 与 IRGen.h。
最小接入顺序
1. 先接表达式算术与比较M1
2. 再接 if/while/break/continueM3
3. 再接函数参数和调用M2
如果你要,我下一步可以给你一版“按当前 grammar 的具体 visitor 函数名”逐条对照表IRGen 同学可以直接照着改函数签名和调用点。

@ -151,8 +151,24 @@ class ConstantInt : public ConstantValue {
int value_{};
};
// 后续还需要扩展更多指令类型。
enum class Opcode { Add, Sub, Mul, Alloca, Load, Store, Ret };
// 第一版 Lab2 需要的指令集合。
enum class Opcode {
Add,
Sub,
Mul,
Div,
Mod,
Cmp,
Br,
CondBr,
Call,
Alloca,
Load,
Store,
Ret,
};
enum class CmpOp { Eq, Ne, Lt, Le, Gt, Ge };
// User 是所有“会使用其他 Value 作为输入”的 IR 对象的抽象基类。
// 当前实现中只有 Instruction 继承自 User。
@ -196,7 +212,19 @@ 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 CmpInst : public Instruction {
public:
CmpInst(CmpOp op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name);
CmpOp GetCmpOp() const;
Value* GetLhs() const;
Value* GetRhs() const;
private:
CmpOp cmp_op_;
};
class ReturnInst : public Instruction {
@ -223,6 +251,30 @@ class StoreInst : public Instruction {
Value* GetPtr() const;
};
class BranchInst : public Instruction {
public:
BranchInst(std::shared_ptr<Type> void_ty, BasicBlock* target);
BasicBlock* GetTarget() const;
};
class CondBranchInst : public Instruction {
public:
CondBranchInst(std::shared_ptr<Type> void_ty, Value* cond,
BasicBlock* true_bb, BasicBlock* false_bb);
Value* GetCond() const;
BasicBlock* GetTrueBlock() const;
BasicBlock* GetFalseBlock() const;
};
class CallInst : public Instruction {
public:
CallInst(std::shared_ptr<Type> ret_ty, Function* callee,
std::vector<Value*> args, std::string name);
Function* GetCallee() const;
size_t GetNumArgs() const;
Value* GetArg(size_t index) const;
};
// BasicBlock 已纳入 Value 体系,便于后续向更完整 IR 类图靠拢。
// 当前其类型仍使用 void 作为占位,后续可替换为专门的 label type。
class BasicBlock : public Value {
@ -234,6 +286,8 @@ class BasicBlock : public Value {
const std::vector<std::unique_ptr<Instruction>>& GetInstructions() const;
const std::vector<BasicBlock*>& GetPredecessors() const;
const std::vector<BasicBlock*>& GetSuccessors() const;
void AddPredecessor(BasicBlock* pred);
void AddSuccessor(BasicBlock* succ);
template <typename T, typename... Args>
T* Append(Args&&... args) {
if (HasTerminator()) {
@ -262,15 +316,18 @@ class BasicBlock : public Value {
// 形参和调用,通常需要引入专门的函数类型表示。
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::shared_ptr<Type>>& GetParamTypes() const;
size_t GetNumParams() const;
const std::vector<std::unique_ptr<BasicBlock>>& GetBlocks() const;
private:
BasicBlock* entry_ = nullptr;
std::vector<std::shared_ptr<Type>> param_types_;
std::vector<std::unique_ptr<BasicBlock>> blocks_;
};
@ -281,7 +338,8 @@ class Module {
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;
private:
@ -300,9 +358,19 @@ class IRBuilder {
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);
CmpInst* CreateCmp(CmpOp op, Value* lhs, Value* rhs, const std::string& name);
AllocaInst* CreateAllocaI32(const std::string& name);
LoadInst* CreateLoad(Value* ptr, const std::string& name);
StoreInst* CreateStore(Value* val, Value* ptr);
BranchInst* CreateBr(BasicBlock* target);
CondBranchInst* CreateCondBr(Value* cond, BasicBlock* true_bb,
BasicBlock* false_bb);
CallInst* CreateCall(Function* callee, const std::vector<Value*>& args,
const std::string& name);
ReturnInst* CreateRet(Value* v);
private:

6
node_modules/.package-lock.json generated vendored

@ -0,0 +1,6 @@
{
"name": "nudt-compiler-cpp",
"lockfileVersion": 2,
"requires": true,
"packages": {}
}

File diff suppressed because one or more lines are too long

@ -0,0 +1,3 @@
This project is dual-licensed under the Unlicense and MIT licenses.
You may use this code under the terms of either license.

@ -0,0 +1,46 @@
# `@img/sharp-libvips-linux-x64`
Prebuilt libvips and dependencies for use with sharp on Linux (glibc) x64.
## Licensing
This software contains third-party libraries
used under the terms of the following licences:
| Library | Used under the terms of |
|---------------|-----------------------------------------------------------------------------------------------------------|
| aom | BSD 2-Clause + [Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/) |
| cairo | Mozilla Public License 2.0 |
| cgif | MIT Licence |
| expat | MIT Licence |
| fontconfig | [fontconfig Licence](https://gitlab.freedesktop.org/fontconfig/fontconfig/blob/main/COPYING) (BSD-like) |
| freetype | [freetype Licence](https://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT) (BSD-like) |
| fribidi | LGPLv3 |
| glib | LGPLv3 |
| harfbuzz | MIT Licence |
| highway | Apache-2.0 License, BSD 3-Clause |
| lcms | MIT Licence |
| libarchive | BSD 2-Clause |
| libexif | LGPLv3 |
| libffi | MIT Licence |
| libheif | LGPLv3 |
| libimagequant | [BSD 2-Clause](https://github.com/lovell/libimagequant/blob/main/COPYRIGHT) |
| libnsgif | MIT Licence |
| libpng | [libpng License](https://github.com/pnggroup/libpng/blob/master/LICENSE) |
| librsvg | LGPLv3 |
| libspng | [BSD 2-Clause, libpng License](https://github.com/randy408/libspng/blob/master/LICENSE) |
| libtiff | [libtiff License](https://gitlab.com/libtiff/libtiff/blob/master/LICENSE.md) (BSD-like) |
| libvips | LGPLv3 |
| libwebp | New BSD License |
| libxml2 | MIT Licence |
| mozjpeg | [zlib License, IJG License, BSD-3-Clause](https://github.com/mozilla/mozjpeg/blob/master/LICENSE.md) |
| pango | LGPLv3 |
| pixman | MIT Licence |
| proxy-libintl | LGPLv3 |
| zlib-ng | [zlib Licence](https://github.com/zlib-ng/zlib-ng/blob/develop/LICENSE.md) |
Use of libraries under the terms of the LGPLv3 is via the
"any later version" clause of the LGPLv2 or LGPLv2.1.
Please report any errors or omissions via
https://github.com/lovell/sharp-libvips/issues/new

@ -0,0 +1,221 @@
/* glibconfig.h
*
* This is a generated file. Please modify 'glibconfig.h.in'
*/
#ifndef __GLIBCONFIG_H__
#define __GLIBCONFIG_H__
#include <glib/gmacros.h>
#include <limits.h>
#include <float.h>
#define GLIB_HAVE_ALLOCA_H
#define GLIB_STATIC_COMPILATION 1
#define GOBJECT_STATIC_COMPILATION 1
#define GIO_STATIC_COMPILATION 1
#define GMODULE_STATIC_COMPILATION 1
#define GI_STATIC_COMPILATION 1
#define G_INTL_STATIC_COMPILATION 1
#define FFI_STATIC_BUILD 1
/* Specifies that GLib's g_print*() functions wrap the
* system printf functions. This is useful to know, for example,
* when using glibc's register_printf_function().
*/
#define GLIB_USING_SYSTEM_PRINTF
G_BEGIN_DECLS
#define G_MINFLOAT FLT_MIN
#define G_MAXFLOAT FLT_MAX
#define G_MINDOUBLE DBL_MIN
#define G_MAXDOUBLE DBL_MAX
#define G_MINSHORT SHRT_MIN
#define G_MAXSHORT SHRT_MAX
#define G_MAXUSHORT USHRT_MAX
#define G_MININT INT_MIN
#define G_MAXINT INT_MAX
#define G_MAXUINT UINT_MAX
#define G_MINLONG LONG_MIN
#define G_MAXLONG LONG_MAX
#define G_MAXULONG ULONG_MAX
typedef signed char gint8;
typedef unsigned char guint8;
typedef signed short gint16;
typedef unsigned short guint16;
#define G_GINT16_MODIFIER "h"
#define G_GINT16_FORMAT "hi"
#define G_GUINT16_FORMAT "hu"
typedef signed int gint32;
typedef unsigned int guint32;
#define G_GINT32_MODIFIER ""
#define G_GINT32_FORMAT "i"
#define G_GUINT32_FORMAT "u"
#define G_HAVE_GINT64 1 /* deprecated, always true */
typedef signed long gint64;
typedef unsigned long guint64;
#define G_GINT64_CONSTANT(val) (val##L)
#define G_GUINT64_CONSTANT(val) (val##UL)
#define G_GINT64_MODIFIER "l"
#define G_GINT64_FORMAT "li"
#define G_GUINT64_FORMAT "lu"
#define GLIB_SIZEOF_VOID_P 8
#define GLIB_SIZEOF_LONG 8
#define GLIB_SIZEOF_SIZE_T 8
#define GLIB_SIZEOF_SSIZE_T 8
typedef signed long gssize;
typedef unsigned long gsize;
#define G_GSIZE_MODIFIER "l"
#define G_GSSIZE_MODIFIER "l"
#define G_GSIZE_FORMAT "lu"
#define G_GSSIZE_FORMAT "li"
#define G_MAXSIZE G_MAXULONG
#define G_MINSSIZE G_MINLONG
#define G_MAXSSIZE G_MAXLONG
typedef gint64 goffset;
#define G_MINOFFSET G_MININT64
#define G_MAXOFFSET G_MAXINT64
#define G_GOFFSET_MODIFIER G_GINT64_MODIFIER
#define G_GOFFSET_FORMAT G_GINT64_FORMAT
#define G_GOFFSET_CONSTANT(val) G_GINT64_CONSTANT(val)
#define G_POLLFD_FORMAT "%d"
#define GPOINTER_TO_INT(p) ((gint) (glong) (p))
#define GPOINTER_TO_UINT(p) ((guint) (gulong) (p))
#define GINT_TO_POINTER(i) ((gpointer) (glong) (i))
#define GUINT_TO_POINTER(u) ((gpointer) (gulong) (u))
typedef signed long gintptr;
typedef unsigned long guintptr;
#define G_GINTPTR_MODIFIER "l"
#define G_GINTPTR_FORMAT "li"
#define G_GUINTPTR_FORMAT "lu"
#define GLIB_MAJOR_VERSION 2
#define GLIB_MINOR_VERSION 86
#define GLIB_MICRO_VERSION 1
#define G_OS_UNIX
#define G_VA_COPY va_copy
#define G_VA_COPY_AS_ARRAY 1
#define G_HAVE_ISO_VARARGS 1
/* gcc-2.95.x supports both gnu style and ISO varargs, but if -ansi
* is passed ISO vararg support is turned off, and there is no work
* around to turn it on, so we unconditionally turn it off.
*/
#if __GNUC__ == 2 && __GNUC_MINOR__ == 95
# undef G_HAVE_ISO_VARARGS
#endif
#define G_HAVE_GROWING_STACK 0
#ifndef _MSC_VER
# define G_HAVE_GNUC_VARARGS 1
#endif
#if defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
#define G_GNUC_INTERNAL __attribute__((visibility("hidden")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
#define G_GNUC_INTERNAL __hidden
#elif defined (__GNUC__) && defined (G_HAVE_GNUC_VISIBILITY)
#define G_GNUC_INTERNAL __attribute__((visibility("hidden")))
#else
#define G_GNUC_INTERNAL
#endif
#define G_THREADS_ENABLED
#define G_THREADS_IMPL_POSIX
#define G_ATOMIC_LOCK_FREE
#define GINT16_TO_LE(val) ((gint16) (val))
#define GUINT16_TO_LE(val) ((guint16) (val))
#define GINT16_TO_BE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
#define GUINT16_TO_BE(val) (GUINT16_SWAP_LE_BE (val))
#define GINT32_TO_LE(val) ((gint32) (val))
#define GUINT32_TO_LE(val) ((guint32) (val))
#define GINT32_TO_BE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
#define GUINT32_TO_BE(val) (GUINT32_SWAP_LE_BE (val))
#define GINT64_TO_LE(val) ((gint64) (val))
#define GUINT64_TO_LE(val) ((guint64) (val))
#define GINT64_TO_BE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
#define GUINT64_TO_BE(val) (GUINT64_SWAP_LE_BE (val))
#define GLONG_TO_LE(val) ((glong) GINT64_TO_LE (val))
#define GULONG_TO_LE(val) ((gulong) GUINT64_TO_LE (val))
#define GLONG_TO_BE(val) ((glong) GINT64_TO_BE (val))
#define GULONG_TO_BE(val) ((gulong) GUINT64_TO_BE (val))
#define GINT_TO_LE(val) ((gint) GINT32_TO_LE (val))
#define GUINT_TO_LE(val) ((guint) GUINT32_TO_LE (val))
#define GINT_TO_BE(val) ((gint) GINT32_TO_BE (val))
#define GUINT_TO_BE(val) ((guint) GUINT32_TO_BE (val))
#define GSIZE_TO_LE(val) ((gsize) GUINT64_TO_LE (val))
#define GSSIZE_TO_LE(val) ((gssize) GINT64_TO_LE (val))
#define GSIZE_TO_BE(val) ((gsize) GUINT64_TO_BE (val))
#define GSSIZE_TO_BE(val) ((gssize) GINT64_TO_BE (val))
#define G_BYTE_ORDER G_LITTLE_ENDIAN
#define GLIB_SYSDEF_POLLIN =1
#define GLIB_SYSDEF_POLLOUT =4
#define GLIB_SYSDEF_POLLPRI =2
#define GLIB_SYSDEF_POLLHUP =16
#define GLIB_SYSDEF_POLLERR =8
#define GLIB_SYSDEF_POLLNVAL =32
/* No way to disable deprecation warnings for macros, so only emit deprecation
* warnings on platforms where usage of this macro is broken */
#if defined(__APPLE__) || defined(_MSC_VER) || defined(__CYGWIN__)
#define G_MODULE_SUFFIX "so" GLIB_DEPRECATED_MACRO_IN_2_76
#else
#define G_MODULE_SUFFIX "so"
#endif
typedef int GPid;
#define G_PID_FORMAT "i"
#define GLIB_SYSDEF_AF_UNIX 1
#define GLIB_SYSDEF_AF_INET 2
#define GLIB_SYSDEF_AF_INET6 10
#define GLIB_SYSDEF_MSG_OOB 1
#define GLIB_SYSDEF_MSG_PEEK 2
#define GLIB_SYSDEF_MSG_DONTROUTE 4
#define G_DIR_SEPARATOR '/'
#define G_DIR_SEPARATOR_S "/"
#define G_SEARCHPATH_SEPARATOR ':'
#define G_SEARCHPATH_SEPARATOR_S ":"
#undef G_HAVE_FREE_SIZED
G_END_DECLS
#endif /* __GLIBCONFIG_H__ */

@ -0,0 +1 @@
module.exports = __dirname;

@ -0,0 +1,42 @@
{
"name": "@img/sharp-libvips-linux-x64",
"version": "1.2.4",
"description": "Prebuilt libvips and dependencies for use with sharp on Linux (glibc) x64",
"author": "Lovell Fuller <npm@lovell.info>",
"homepage": "https://sharp.pixelplumbing.com",
"repository": {
"type": "git",
"url": "git+https://github.com/lovell/sharp-libvips.git",
"directory": "npm/linux-x64"
},
"license": "LGPL-3.0-or-later",
"funding": {
"url": "https://opencollective.com/libvips"
},
"preferUnplugged": true,
"publishConfig": {
"access": "public"
},
"files": [
"lib",
"versions.json"
],
"type": "commonjs",
"exports": {
"./lib": "./lib/index.js",
"./package": "./package.json",
"./versions": "./versions.json"
},
"config": {
"glibc": ">=2.26"
},
"os": [
"linux"
],
"libc": [
"glibc"
],
"cpu": [
"x64"
]
}

@ -0,0 +1,30 @@
{
"aom": "3.13.1",
"archive": "3.8.2",
"cairo": "1.18.4",
"cgif": "0.5.0",
"exif": "0.6.25",
"expat": "2.7.3",
"ffi": "3.5.2",
"fontconfig": "2.17.1",
"freetype": "2.14.1",
"fribidi": "1.0.16",
"glib": "2.86.1",
"harfbuzz": "12.1.0",
"heif": "1.20.2",
"highway": "1.3.0",
"imagequant": "2.4.1",
"lcms": "2.17",
"mozjpeg": "0826579",
"pango": "1.57.0",
"pixman": "0.46.4",
"png": "1.6.50",
"proxy-libintl": "0.5",
"rsvg": "2.61.2",
"spng": "0.7.4",
"tiff": "4.7.1",
"vips": "8.17.3",
"webp": "1.6.0",
"xml2": "2.15.1",
"zlib-ng": "2.2.5"
}

@ -0,0 +1,46 @@
# `@img/sharp-libvips-linuxmusl-x64`
Prebuilt libvips and dependencies for use with sharp on Linux (musl) x64.
## Licensing
This software contains third-party libraries
used under the terms of the following licences:
| Library | Used under the terms of |
|---------------|-----------------------------------------------------------------------------------------------------------|
| aom | BSD 2-Clause + [Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/) |
| cairo | Mozilla Public License 2.0 |
| cgif | MIT Licence |
| expat | MIT Licence |
| fontconfig | [fontconfig Licence](https://gitlab.freedesktop.org/fontconfig/fontconfig/blob/main/COPYING) (BSD-like) |
| freetype | [freetype Licence](https://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT) (BSD-like) |
| fribidi | LGPLv3 |
| glib | LGPLv3 |
| harfbuzz | MIT Licence |
| highway | Apache-2.0 License, BSD 3-Clause |
| lcms | MIT Licence |
| libarchive | BSD 2-Clause |
| libexif | LGPLv3 |
| libffi | MIT Licence |
| libheif | LGPLv3 |
| libimagequant | [BSD 2-Clause](https://github.com/lovell/libimagequant/blob/main/COPYRIGHT) |
| libnsgif | MIT Licence |
| libpng | [libpng License](https://github.com/pnggroup/libpng/blob/master/LICENSE) |
| librsvg | LGPLv3 |
| libspng | [BSD 2-Clause, libpng License](https://github.com/randy408/libspng/blob/master/LICENSE) |
| libtiff | [libtiff License](https://gitlab.com/libtiff/libtiff/blob/master/LICENSE.md) (BSD-like) |
| libvips | LGPLv3 |
| libwebp | New BSD License |
| libxml2 | MIT Licence |
| mozjpeg | [zlib License, IJG License, BSD-3-Clause](https://github.com/mozilla/mozjpeg/blob/master/LICENSE.md) |
| pango | LGPLv3 |
| pixman | MIT Licence |
| proxy-libintl | LGPLv3 |
| zlib-ng | [zlib Licence](https://github.com/zlib-ng/zlib-ng/blob/develop/LICENSE.md) |
Use of libraries under the terms of the LGPLv3 is via the
"any later version" clause of the LGPLv2 or LGPLv2.1.
Please report any errors or omissions via
https://github.com/lovell/sharp-libvips/issues/new

@ -0,0 +1,221 @@
/* glibconfig.h
*
* This is a generated file. Please modify 'glibconfig.h.in'
*/
#ifndef __GLIBCONFIG_H__
#define __GLIBCONFIG_H__
#include <glib/gmacros.h>
#include <limits.h>
#include <float.h>
#define GLIB_HAVE_ALLOCA_H
#define GLIB_STATIC_COMPILATION 1
#define GOBJECT_STATIC_COMPILATION 1
#define GIO_STATIC_COMPILATION 1
#define GMODULE_STATIC_COMPILATION 1
#define GI_STATIC_COMPILATION 1
#define G_INTL_STATIC_COMPILATION 1
#define FFI_STATIC_BUILD 1
/* Specifies that GLib's g_print*() functions wrap the
* system printf functions. This is useful to know, for example,
* when using glibc's register_printf_function().
*/
#define GLIB_USING_SYSTEM_PRINTF
G_BEGIN_DECLS
#define G_MINFLOAT FLT_MIN
#define G_MAXFLOAT FLT_MAX
#define G_MINDOUBLE DBL_MIN
#define G_MAXDOUBLE DBL_MAX
#define G_MINSHORT SHRT_MIN
#define G_MAXSHORT SHRT_MAX
#define G_MAXUSHORT USHRT_MAX
#define G_MININT INT_MIN
#define G_MAXINT INT_MAX
#define G_MAXUINT UINT_MAX
#define G_MINLONG LONG_MIN
#define G_MAXLONG LONG_MAX
#define G_MAXULONG ULONG_MAX
typedef signed char gint8;
typedef unsigned char guint8;
typedef signed short gint16;
typedef unsigned short guint16;
#define G_GINT16_MODIFIER "h"
#define G_GINT16_FORMAT "hi"
#define G_GUINT16_FORMAT "hu"
typedef signed int gint32;
typedef unsigned int guint32;
#define G_GINT32_MODIFIER ""
#define G_GINT32_FORMAT "i"
#define G_GUINT32_FORMAT "u"
#define G_HAVE_GINT64 1 /* deprecated, always true */
typedef signed long gint64;
typedef unsigned long guint64;
#define G_GINT64_CONSTANT(val) (val##L)
#define G_GUINT64_CONSTANT(val) (val##UL)
#define G_GINT64_MODIFIER "l"
#define G_GINT64_FORMAT "li"
#define G_GUINT64_FORMAT "lu"
#define GLIB_SIZEOF_VOID_P 8
#define GLIB_SIZEOF_LONG 8
#define GLIB_SIZEOF_SIZE_T 8
#define GLIB_SIZEOF_SSIZE_T 8
typedef signed long gssize;
typedef unsigned long gsize;
#define G_GSIZE_MODIFIER "l"
#define G_GSSIZE_MODIFIER "l"
#define G_GSIZE_FORMAT "lu"
#define G_GSSIZE_FORMAT "li"
#define G_MAXSIZE G_MAXULONG
#define G_MINSSIZE G_MINLONG
#define G_MAXSSIZE G_MAXLONG
typedef gint64 goffset;
#define G_MINOFFSET G_MININT64
#define G_MAXOFFSET G_MAXINT64
#define G_GOFFSET_MODIFIER G_GINT64_MODIFIER
#define G_GOFFSET_FORMAT G_GINT64_FORMAT
#define G_GOFFSET_CONSTANT(val) G_GINT64_CONSTANT(val)
#define G_POLLFD_FORMAT "%d"
#define GPOINTER_TO_INT(p) ((gint) (glong) (p))
#define GPOINTER_TO_UINT(p) ((guint) (gulong) (p))
#define GINT_TO_POINTER(i) ((gpointer) (glong) (i))
#define GUINT_TO_POINTER(u) ((gpointer) (gulong) (u))
typedef signed long gintptr;
typedef unsigned long guintptr;
#define G_GINTPTR_MODIFIER "l"
#define G_GINTPTR_FORMAT "li"
#define G_GUINTPTR_FORMAT "lu"
#define GLIB_MAJOR_VERSION 2
#define GLIB_MINOR_VERSION 86
#define GLIB_MICRO_VERSION 1
#define G_OS_UNIX
#define G_VA_COPY va_copy
#define G_VA_COPY_AS_ARRAY 1
#define G_HAVE_ISO_VARARGS 1
/* gcc-2.95.x supports both gnu style and ISO varargs, but if -ansi
* is passed ISO vararg support is turned off, and there is no work
* around to turn it on, so we unconditionally turn it off.
*/
#if __GNUC__ == 2 && __GNUC_MINOR__ == 95
# undef G_HAVE_ISO_VARARGS
#endif
#define G_HAVE_GROWING_STACK 0
#ifndef _MSC_VER
# define G_HAVE_GNUC_VARARGS 1
#endif
#if defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590)
#define G_GNUC_INTERNAL __attribute__((visibility("hidden")))
#elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
#define G_GNUC_INTERNAL __hidden
#elif defined (__GNUC__) && defined (G_HAVE_GNUC_VISIBILITY)
#define G_GNUC_INTERNAL __attribute__((visibility("hidden")))
#else
#define G_GNUC_INTERNAL
#endif
#define G_THREADS_ENABLED
#define G_THREADS_IMPL_POSIX
#define G_ATOMIC_LOCK_FREE
#define GINT16_TO_LE(val) ((gint16) (val))
#define GUINT16_TO_LE(val) ((guint16) (val))
#define GINT16_TO_BE(val) ((gint16) GUINT16_SWAP_LE_BE (val))
#define GUINT16_TO_BE(val) (GUINT16_SWAP_LE_BE (val))
#define GINT32_TO_LE(val) ((gint32) (val))
#define GUINT32_TO_LE(val) ((guint32) (val))
#define GINT32_TO_BE(val) ((gint32) GUINT32_SWAP_LE_BE (val))
#define GUINT32_TO_BE(val) (GUINT32_SWAP_LE_BE (val))
#define GINT64_TO_LE(val) ((gint64) (val))
#define GUINT64_TO_LE(val) ((guint64) (val))
#define GINT64_TO_BE(val) ((gint64) GUINT64_SWAP_LE_BE (val))
#define GUINT64_TO_BE(val) (GUINT64_SWAP_LE_BE (val))
#define GLONG_TO_LE(val) ((glong) GINT64_TO_LE (val))
#define GULONG_TO_LE(val) ((gulong) GUINT64_TO_LE (val))
#define GLONG_TO_BE(val) ((glong) GINT64_TO_BE (val))
#define GULONG_TO_BE(val) ((gulong) GUINT64_TO_BE (val))
#define GINT_TO_LE(val) ((gint) GINT32_TO_LE (val))
#define GUINT_TO_LE(val) ((guint) GUINT32_TO_LE (val))
#define GINT_TO_BE(val) ((gint) GINT32_TO_BE (val))
#define GUINT_TO_BE(val) ((guint) GUINT32_TO_BE (val))
#define GSIZE_TO_LE(val) ((gsize) GUINT64_TO_LE (val))
#define GSSIZE_TO_LE(val) ((gssize) GINT64_TO_LE (val))
#define GSIZE_TO_BE(val) ((gsize) GUINT64_TO_BE (val))
#define GSSIZE_TO_BE(val) ((gssize) GINT64_TO_BE (val))
#define G_BYTE_ORDER G_LITTLE_ENDIAN
#define GLIB_SYSDEF_POLLIN =1
#define GLIB_SYSDEF_POLLOUT =4
#define GLIB_SYSDEF_POLLPRI =2
#define GLIB_SYSDEF_POLLHUP =16
#define GLIB_SYSDEF_POLLERR =8
#define GLIB_SYSDEF_POLLNVAL =32
/* No way to disable deprecation warnings for macros, so only emit deprecation
* warnings on platforms where usage of this macro is broken */
#if defined(__APPLE__) || defined(_MSC_VER) || defined(__CYGWIN__)
#define G_MODULE_SUFFIX "so" GLIB_DEPRECATED_MACRO_IN_2_76
#else
#define G_MODULE_SUFFIX "so"
#endif
typedef int GPid;
#define G_PID_FORMAT "i"
#define GLIB_SYSDEF_AF_UNIX 1
#define GLIB_SYSDEF_AF_INET 2
#define GLIB_SYSDEF_AF_INET6 10
#define GLIB_SYSDEF_MSG_OOB 1
#define GLIB_SYSDEF_MSG_PEEK 2
#define GLIB_SYSDEF_MSG_DONTROUTE 4
#define G_DIR_SEPARATOR '/'
#define G_DIR_SEPARATOR_S "/"
#define G_SEARCHPATH_SEPARATOR ':'
#define G_SEARCHPATH_SEPARATOR_S ":"
#undef G_HAVE_FREE_SIZED
G_END_DECLS
#endif /* __GLIBCONFIG_H__ */

@ -0,0 +1 @@
module.exports = __dirname;

@ -0,0 +1,42 @@
{
"name": "@img/sharp-libvips-linuxmusl-x64",
"version": "1.2.4",
"description": "Prebuilt libvips and dependencies for use with sharp on Linux (musl) x64",
"author": "Lovell Fuller <npm@lovell.info>",
"homepage": "https://sharp.pixelplumbing.com",
"repository": {
"type": "git",
"url": "git+https://github.com/lovell/sharp-libvips.git",
"directory": "npm/linuxmusl-x64"
},
"license": "LGPL-3.0-or-later",
"funding": {
"url": "https://opencollective.com/libvips"
},
"preferUnplugged": true,
"publishConfig": {
"access": "public"
},
"files": [
"lib",
"versions.json"
],
"type": "commonjs",
"exports": {
"./lib": "./lib/index.js",
"./package": "./package.json",
"./versions": "./versions.json"
},
"config": {
"musl": ">=1.2.2"
},
"os": [
"linux"
],
"libc": [
"musl"
],
"cpu": [
"x64"
]
}

@ -0,0 +1,30 @@
{
"aom": "3.13.1",
"archive": "3.8.2",
"cairo": "1.18.4",
"cgif": "0.5.0",
"exif": "0.6.25",
"expat": "2.7.3",
"ffi": "3.5.2",
"fontconfig": "2.17.1",
"freetype": "2.14.1",
"fribidi": "1.0.16",
"glib": "2.86.1",
"harfbuzz": "12.1.0",
"heif": "1.20.2",
"highway": "1.3.0",
"imagequant": "2.4.1",
"lcms": "2.17",
"mozjpeg": "0826579",
"pango": "1.57.0",
"pixman": "0.46.4",
"png": "1.6.50",
"proxy-libintl": "0.5",
"rsvg": "2.61.2",
"spng": "0.7.4",
"tiff": "4.7.1",
"vips": "8.17.3",
"webp": "1.6.0",
"xml2": "2.15.1",
"zlib-ng": "2.2.5"
}

6
package-lock.json generated

@ -0,0 +1,6 @@
{
"name": "nudt-compiler-cpp",
"lockfileVersion": 2,
"requires": true,
"packages": {}
}

@ -0,0 +1,97 @@
Lab1 语法树构建
要做什么:补全 SysY 文法,保证更多合法程序可被解析并打印语法树。
主要改哪些文件:
Lab1-语法树构建.md
SysY.g4
AntlrDriver.cpp
SyntaxTreePrinter.cpp
修改方式:
扩展 grammar 规则和 token保持解析入口稳定错误信息要可定位到行列语法树打印结构清晰。
验收parse-tree 模式批量通过测试集。
Lab2 中间表示生成
要做什么:把语义检查和 IR 生成从最小子集扩展到课程要求语法。
主要改哪些文件:
Lab2-中间表示生成.md
Sema.h
SymbolTable.h
Sema.cpp
SymbolTable.cpp
IR.h
IRBuilder.cpp
Instruction.cpp
IRPrinter.cpp
IRGen.h
IRGenDecl.cpp
IRGenStmt.cpp
IRGenExp.cpp
IRGenFunc.cpp
IRGenDriver.cpp
修改方式:
先补语义绑定和错误检查,再补 IR 指令与类型,最后在 Visitor 里把各类语句表达式翻译到 IR。
验收IR 能生成,并且 verify_ir 脚本 run 模式和输出比对通过。
Lab3 指令选择与汇编生成
要做什么:把 IR 正确 lower 到 AArch64 汇编,覆盖更多语义。
主要改哪些文件:
Lab3-指令选择与汇编生成.md
MIR.h
Lowering.cpp
RegAlloc.cpp
FrameLowering.cpp
AsmPrinter.cpp
修改方式:
扩充 MIR 指令和操作数表示;完善 lowering 映射;保证栈帧和函数序言尾声正确;输出可汇编可运行的 asm。
验收verify_asm 脚本 run 模式通过。
Lab4 基本标量优化
要做什么:先做 mem2reg再做常量相关优化、DCE、CFG 简化、CSE 等。
主要改哪些文件:
Lab4-基本标量优化.md
Mem2Reg.cpp
ConstFold.cpp
ConstProp.cpp
DCE.cpp
CSE.cpp
CFGSimplify.cpp
PassManager.cpp
DominatorTree.cpp
LoopInfo.cpp
修改方式:
实现每个 pass 的核心逻辑,并在 PassManager 固化顺序和迭代策略。
验收优化前后语义一致IR/ASM 回归测试通过。
Lab5 寄存器分配与后端优化
要做什么:从固定寄存器模板,升级到虚拟寄存器+真实分配+spill/reload再做后端局部优化。
主要改哪些文件:
Lab5-寄存器分配.md
MIR.h
Lowering.cpp
RegAlloc.cpp
FrameLowering.cpp
AsmPrinter.cpp
Peephole.cpp
PassManager.cpp
修改方式:
Lowering 先产出 vregRA 选图着色或线扫;处理调用约定和栈槽;最后做 peephole 与冗余访存清理。
验收:全测试正确,且汇编明显减少无效 move/load/store。
Lab6 并行与循环优化
要做什么:识别循环结构并做循环优化,必要时尝试并行化识别。
主要改哪些文件:
Lab6-并行与循环优化.md
DominatorTree.cpp
LoopInfo.cpp
PassManager.cpp
CMakeLists.txt
修改方式:
补稳定的循环分析,再实现 LICM、强度削弱、展开、分裂中的一部分并接入 pass 流程。
验收:功能回归全通过,同时在代表性用例看到性能或代码质量收益。
你可以直接照这个顺序推进
先做 Lab2优先把语义和 IR 生成功能面补全。
再做 Lab3保证语义到汇编端到端正确。
接着做 Lab4把优化 pass 跑通。
然后做 Lab5完成真实寄存器分配。
最后做 Lab6补循环优化和并行化探索。

@ -2,7 +2,7 @@ import os
import subprocess
COMPILER = "./build/bin/compiler"
TEST_DIR = "./test/test_case/functional"
TEST_DIR = "./test/test_case/performance"
pass_cnt = 0
fail_cnt = 0

@ -9,6 +9,7 @@
#include "ir/IR.h"
#include <algorithm>
#include <utility>
namespace ir {
@ -42,4 +43,26 @@ const std::vector<BasicBlock*>& BasicBlock::GetSuccessors() const {
return successors_;
}
void BasicBlock::AddPredecessor(BasicBlock* pred) {
if (!pred) {
return;
}
if (std::find(predecessors_.begin(), predecessors_.end(), pred) !=
predecessors_.end()) {
return;
}
predecessors_.push_back(pred);
}
void BasicBlock::AddSuccessor(BasicBlock* succ) {
if (!succ) {
return;
}
if (std::find(successors_.begin(), successors_.end(), succ) !=
successors_.end()) {
return;
}
successors_.push_back(succ);
}
} // namespace ir

@ -5,8 +5,10 @@
namespace ir {
Function::Function(std::string name, std::shared_ptr<Type> ret_type)
: Value(std::move(ret_type), std::move(name)) {
Function::Function(std::string name, std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types)
: Value(std::move(ret_type), std::move(name)),
param_types_(std::move(param_types)) {
entry_ = CreateBlock("entry");
}
@ -25,6 +27,12 @@ BasicBlock* Function::GetEntry() { return entry_; }
const BasicBlock* Function::GetEntry() const { return entry_; }
const std::vector<std::shared_ptr<Type>>& Function::GetParamTypes() const {
return param_types_;
}
size_t Function::GetNumParams() const { return param_types_.size(); }
const std::vector<std::unique_ptr<BasicBlock>>& Function::GetBlocks() const {
return blocks_;
}

@ -42,6 +42,39 @@ BinaryInst* IRBuilder::CreateAdd(Value* lhs, Value* rhs,
return CreateBinary(Opcode::Add, lhs, rhs, name);
}
BinaryInst* IRBuilder::CreateSub(Value* lhs, Value* rhs,
const std::string& name) {
return CreateBinary(Opcode::Sub, lhs, rhs, name);
}
BinaryInst* IRBuilder::CreateMul(Value* lhs, Value* rhs,
const std::string& name) {
return CreateBinary(Opcode::Mul, lhs, rhs, name);
}
BinaryInst* IRBuilder::CreateDiv(Value* lhs, Value* rhs,
const std::string& name) {
return CreateBinary(Opcode::Div, lhs, rhs, name);
}
BinaryInst* IRBuilder::CreateMod(Value* lhs, Value* rhs,
const std::string& name) {
return CreateBinary(Opcode::Mod, lhs, rhs, name);
}
CmpInst* IRBuilder::CreateCmp(CmpOp op, Value* lhs, Value* rhs,
const std::string& name) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!lhs || !rhs) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateCmp 缺少操作数"));
}
return insert_block_->Append<CmpInst>(op, Type::GetInt32Type(), lhs, rhs,
name);
}
AllocaInst* IRBuilder::CreateAllocaI32(const std::string& name) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
@ -75,6 +108,43 @@ StoreInst* IRBuilder::CreateStore(Value* val, Value* ptr) {
return insert_block_->Append<StoreInst>(Type::GetVoidType(), val, ptr);
}
BranchInst* IRBuilder::CreateBr(BasicBlock* target) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!target) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateBr 缺少目标块"));
}
return insert_block_->Append<BranchInst>(Type::GetVoidType(), target);
}
CondBranchInst* IRBuilder::CreateCondBr(Value* cond, BasicBlock* true_bb,
BasicBlock* false_bb) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!cond || !true_bb || !false_bb) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateCondBr 参数不完整"));
}
return insert_block_->Append<CondBranchInst>(Type::GetVoidType(), cond,
true_bb, false_bb);
}
CallInst* IRBuilder::CreateCall(Function* callee,
const std::vector<Value*>& args,
const std::string& name) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));
}
if (!callee) {
throw std::runtime_error(
FormatError("ir", "IRBuilder::CreateCall 缺少被调函数"));
}
return insert_block_->Append<CallInst>(callee->GetType(), callee, args, name);
}
ReturnInst* IRBuilder::CreateRet(Value* v) {
if (!insert_block_) {
throw std::runtime_error(FormatError("ir", "IRBuilder 未设置插入点"));

@ -32,6 +32,18 @@ static const char* OpcodeToString(Opcode op) {
return "sub";
case Opcode::Mul:
return "mul";
case Opcode::Div:
return "sdiv";
case Opcode::Mod:
return "srem";
case Opcode::Cmp:
return "icmp";
case Opcode::Br:
return "br";
case Opcode::CondBr:
return "br";
case Opcode::Call:
return "call";
case Opcode::Alloca:
return "alloca";
case Opcode::Load:
@ -44,17 +56,47 @@ static const char* OpcodeToString(Opcode op) {
return "?";
}
static const char* CmpOpToString(CmpOp op) {
switch (op) {
case CmpOp::Eq:
return "eq";
case CmpOp::Ne:
return "ne";
case CmpOp::Lt:
return "slt";
case CmpOp::Le:
return "sle";
case CmpOp::Gt:
return "sgt";
case CmpOp::Ge:
return "sge";
}
return "?";
}
static std::string ValueToString(const Value* v) {
if (auto* ci = dynamic_cast<const ConstantInt*>(v)) {
return std::to_string(ci->GetValue());
}
if (auto* func = dynamic_cast<const Function*>(v)) {
return "@" + func->GetName();
}
return v ? v->GetName() : "<null>";
}
void IRPrinter::Print(const Module& module, std::ostream& os) {
for (const auto& func : module.GetFunctions()) {
std::string params;
const auto& param_types = func->GetParamTypes();
for (size_t i = 0; i < param_types.size(); ++i) {
if (i != 0) {
params += ", ";
}
params += TypeToString(*param_types[i]);
params += " %arg" + std::to_string(i);
}
os << "define " << TypeToString(*func->GetType()) << " @" << func->GetName()
<< "() {\n";
<< "(" << params << ") {\n";
for (const auto& bb : func->GetBlocks()) {
if (!bb) {
continue;
@ -65,7 +107,9 @@ void IRPrinter::Print(const Module& module, std::ostream& os) {
switch (inst->GetOpcode()) {
case Opcode::Add:
case Opcode::Sub:
case Opcode::Mul: {
case Opcode::Mul:
case Opcode::Div:
case Opcode::Mod: {
auto* bin = static_cast<const BinaryInst*>(inst);
os << " " << bin->GetName() << " = "
<< OpcodeToString(bin->GetOpcode()) << " "
@ -74,6 +118,15 @@ void IRPrinter::Print(const Module& module, std::ostream& os) {
<< ValueToString(bin->GetRhs()) << "\n";
break;
}
case Opcode::Cmp: {
auto* cmp = static_cast<const CmpInst*>(inst);
os << " " << cmp->GetName() << " = " << OpcodeToString(cmp->GetOpcode())
<< " " << CmpOpToString(cmp->GetCmpOp()) << " "
<< TypeToString(*cmp->GetLhs()->GetType()) << " "
<< ValueToString(cmp->GetLhs()) << ", "
<< ValueToString(cmp->GetRhs()) << "\n";
break;
}
case Opcode::Alloca: {
auto* alloca = static_cast<const AllocaInst*>(inst);
os << " " << alloca->GetName() << " = alloca i32\n";
@ -91,6 +144,37 @@ void IRPrinter::Print(const Module& module, std::ostream& os) {
<< ", i32* " << ValueToString(store->GetPtr()) << "\n";
break;
}
case Opcode::Br: {
auto* br = static_cast<const BranchInst*>(inst);
os << " br label %" << br->GetTarget()->GetName() << "\n";
break;
}
case Opcode::CondBr: {
auto* cbr = static_cast<const CondBranchInst*>(inst);
os << " br i32 " << ValueToString(cbr->GetCond()) << ", label %"
<< cbr->GetTrueBlock()->GetName() << ", label %"
<< cbr->GetFalseBlock()->GetName() << "\n";
break;
}
case Opcode::Call: {
auto* call = static_cast<const CallInst*>(inst);
if (!call->GetType()->IsVoid()) {
os << " " << call->GetName() << " = ";
} else {
os << " ";
}
os << "call " << TypeToString(*call->GetType()) << " @"
<< call->GetCallee()->GetName() << "(";
for (size_t i = 0; i < call->GetNumArgs(); ++i) {
if (i != 0) {
os << ", ";
}
auto* arg = call->GetArg(i);
os << TypeToString(*arg->GetType()) << " " << ValueToString(arg);
}
os << ")\n";
break;
}
case Opcode::Ret: {
auto* ret = static_cast<const ReturnInst*>(inst);
os << " ret " << TypeToString(*ret->GetValue()->GetType()) << " "

@ -3,7 +3,9 @@
// - 指令操作数与结果类型管理,支持打印与优化
#include "ir/IR.h"
#include <algorithm>
#include <stdexcept>
#include <utility>
#include "utils/Log.h"
@ -52,17 +54,44 @@ 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 ||
opcode_ == Opcode::CondBr;
}
BasicBlock* Instruction::GetParent() const { return parent_; }
void Instruction::SetParent(BasicBlock* parent) { parent_ = parent; }
void Instruction::SetParent(BasicBlock* parent) {
parent_ = parent;
if (!parent_) {
return;
}
if (auto* br = dynamic_cast<BranchInst*>(this)) {
auto* target = br->GetTarget();
parent_->AddSuccessor(target);
target->AddPredecessor(parent_);
return;
}
if (auto* cbr = dynamic_cast<CondBranchInst*>(this)) {
auto* true_bb = cbr->GetTrueBlock();
auto* false_bb = cbr->GetFalseBlock();
parent_->AddSuccessor(true_bb);
true_bb->AddPredecessor(parent_);
parent_->AddSuccessor(false_bb);
false_bb->AddPredecessor(parent_);
}
}
static bool IsBinaryOpcode(Opcode op) {
return op == Opcode::Add || op == Opcode::Sub || op == Opcode::Mul ||
op == Opcode::Div || op == Opcode::Mod;
}
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 (!IsBinaryOpcode(op)) {
throw std::runtime_error(FormatError("ir", "BinaryInst 非法二元操作码"));
}
if (!lhs || !rhs) {
throw std::runtime_error(FormatError("ir", "BinaryInst 缺少操作数"));
@ -85,6 +114,31 @@ Value* BinaryInst::GetLhs() const { return GetOperand(0); }
Value* BinaryInst::GetRhs() const { return GetOperand(1); }
CmpInst::CmpInst(CmpOp op, std::shared_ptr<Type> ty, Value* lhs, Value* rhs,
std::string name)
: Instruction(Opcode::Cmp, std::move(ty), std::move(name)), cmp_op_(op) {
if (!lhs || !rhs) {
throw std::runtime_error(FormatError("ir", "CmpInst 缺少操作数"));
}
if (!type_ || !lhs->GetType() || !rhs->GetType()) {
throw std::runtime_error(FormatError("ir", "CmpInst 缺少类型信息"));
}
if (!type_->IsInt32()) {
throw std::runtime_error(FormatError("ir", "CmpInst 结果类型必须为 i32"));
}
if (!lhs->GetType()->IsInt32() || !rhs->GetType()->IsInt32()) {
throw std::runtime_error(FormatError("ir", "CmpInst 当前只支持 i32 比较"));
}
AddOperand(lhs);
AddOperand(rhs);
}
CmpOp CmpInst::GetCmpOp() const { return cmp_op_; }
Value* CmpInst::GetLhs() const { return GetOperand(0); }
Value* CmpInst::GetRhs() const { return GetOperand(1); }
ReturnInst::ReturnInst(std::shared_ptr<Type> void_ty, Value* val)
: Instruction(Opcode::Ret, std::move(void_ty), "") {
if (!val) {
@ -148,4 +202,87 @@ Value* StoreInst::GetValue() const { return GetOperand(0); }
Value* StoreInst::GetPtr() const { return GetOperand(1); }
BranchInst::BranchInst(std::shared_ptr<Type> void_ty, BasicBlock* target)
: Instruction(Opcode::Br, std::move(void_ty), "") {
if (!target) {
throw std::runtime_error(FormatError("ir", "BranchInst 缺少目标块"));
}
if (!type_ || !type_->IsVoid()) {
throw std::runtime_error(FormatError("ir", "BranchInst 返回类型必须为 void"));
}
AddOperand(target);
}
BasicBlock* BranchInst::GetTarget() const {
return static_cast<BasicBlock*>(GetOperand(0));
}
CondBranchInst::CondBranchInst(std::shared_ptr<Type> void_ty, Value* cond,
BasicBlock* true_bb, BasicBlock* false_bb)
: Instruction(Opcode::CondBr, std::move(void_ty), "") {
if (!cond || !true_bb || !false_bb) {
throw std::runtime_error(FormatError("ir", "CondBranchInst 参数不完整"));
}
if (!type_ || !type_->IsVoid()) {
throw std::runtime_error(
FormatError("ir", "CondBranchInst 返回类型必须为 void"));
}
if (!cond->GetType() || !cond->GetType()->IsInt32()) {
throw std::runtime_error(FormatError("ir", "CondBranchInst 条件必须为 i32"));
}
AddOperand(cond);
AddOperand(true_bb);
AddOperand(false_bb);
}
Value* CondBranchInst::GetCond() const { return GetOperand(0); }
BasicBlock* CondBranchInst::GetTrueBlock() const {
return static_cast<BasicBlock*>(GetOperand(1));
}
BasicBlock* CondBranchInst::GetFalseBlock() const {
return static_cast<BasicBlock*>(GetOperand(2));
}
CallInst::CallInst(std::shared_ptr<Type> ret_ty, Function* callee,
std::vector<Value*> args, std::string name)
: Instruction(Opcode::Call, std::move(ret_ty), std::move(name)) {
if (!callee) {
throw std::runtime_error(FormatError("ir", "CallInst 缺少被调函数"));
}
const auto& param_types = callee->GetParamTypes();
if (args.size() != param_types.size()) {
throw std::runtime_error(FormatError("ir", "CallInst 参数个数不匹配"));
}
if (!type_ || !callee->GetType() || type_->GetKind() != callee->GetType()->GetKind()) {
throw std::runtime_error(FormatError("ir", "CallInst 返回类型与函数签名不匹配"));
}
AddOperand(callee);
for (size_t i = 0; i < args.size(); ++i) {
auto* arg = args[i];
if (!arg || !arg->GetType()) {
throw std::runtime_error(FormatError("ir", "CallInst 存在非法参数"));
}
if (!param_types[i] || arg->GetType()->GetKind() != param_types[i]->GetKind()) {
throw std::runtime_error(FormatError("ir", "CallInst 参数类型不匹配"));
}
AddOperand(arg);
}
}
Function* CallInst::GetCallee() const {
return static_cast<Function*>(GetOperand(0));
}
size_t CallInst::GetNumArgs() const { return GetNumOperands() - 1; }
Value* CallInst::GetArg(size_t index) const {
if (index >= GetNumArgs()) {
throw std::out_of_range("CallInst arg index out of range");
}
return GetOperand(index + 1);
}
} // namespace ir

@ -9,8 +9,10 @@ Context& Module::GetContext() { return context_; }
const Context& Module::GetContext() const { return context_; }
Function* Module::CreateFunction(const std::string& name,
std::shared_ptr<Type> ret_type) {
functions_.push_back(std::make_unique<Function>(name, std::move(ret_type)));
std::shared_ptr<Type> ret_type,
std::vector<std::shared_ptr<Type>> param_types) {
functions_.push_back(std::make_unique<Function>(
name, std::move(ret_type), std::move(param_types)));
return functions_.back().get();
}

Loading…
Cancel
Save