forked from NUDT-compiler/nudt-compiler-cpp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.8 KiB
3.8 KiB
CLAUDE.md
SysY → ARM64/AArch64 编译器,CMake + C++17 + ANTLR 4.13.2。2026 编译系统设计赛(华为毕昇杯)ARM 赛道。
构建
# 首次:生成 ANTLR Lexer/Parser
mkdir -p build/generated/antlr4
java -jar third_party/antlr-4.13.2-complete.jar -Dlanguage=Cpp -visitor -no-listener \
-Xexact-output-dir -o build/generated/antlr4 src/antlr4/SysY.g4
# 全量构建
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCOMPILER_PARSE_ONLY=OFF
cmake --build build -j "$(nproc)"
可执行文件:./build/bin/compiler
编译器 CLI
compiler -S -o output.s input.sy # 汇编输出(比赛标准)
compiler -S -o output.s input.sy -O # 带优化
compiler --emit-ir input.sy # 打印 IR
compiler --emit-parse-tree input.sy # 打印语法树
架构
编译管线:SysY → ANTLR 语法树 → 语义分析 → IR 生成 → IR 优化 → MIR 降级 → 寄存器分配 → 栈帧 → 窥孔 → AArch64 汇编
源码目录:src/frontend/(ANTLR 驱动)、src/sem/(Sema/SymbolTable)、src/irgen/(语法树→IR)、src/ir/(Module→Function→BasicBlock→Instruction,passes/ 含 Mem2Reg/CFGSimplify/ConstFold/ConstProp/DCE/CSE/LICM)、src/mir/(MachineModule→MachineFunction→MachineBasicBlock→MachineInstr,Lowering/RegAlloc/FrameLowering/AsmPrinter/Peephole)
关键设计:IR 类型 void/i1/i32/float/i32*/float*;MIR 操作数 PhysReg/VReg/Imm/FrameIndex/Label/Symbol;-O 触发所有 IR pass;GP 可分配集含 x16/x17;xzr/wzr 为零寄存器,sp 为栈指针。
竞赛红线(零容忍)
- 禁止投机优化(不得识别特定函数名/输入特征)
- 禁止硬编码计算结果
- 禁止依赖 UB(数组越界、除法溢出等假设)
- 优化必须对所有合法 SysY2026 程序语义保持
历史故障模式——修改以下区域时必须遵守的预防规则
| 区域 | 预防规则 |
|---|---|
| 寄存器分配 | 合并后重算 degree;不修改遍历中的容器;Briggs 保守测试 |
| 栈帧 | 大偏移量(>12KB)必须用 movz/movk 合成偏移 |
| 活变量分析 | shift 链等密集 def-use 需保守干涉边(block defs>20 时全干涉) |
| spill | 大函数(>120 vregs)限制 spill 轮次 ≤5 |
| 活跃合并 | 合并前检查 u != v;move_adj 自环导致迭代器失效 |
| IR 优化 | Load/Store/Call 不能重排跨越彼此;浮点不能随意重关联 |
门禁
# 快门禁(每次 commit 前,~2分钟)
./2026test.sh -c functional -x && ./2026test.sh -c h_functional -x
# 中门禁(merge 前,~10分钟)
./2026test.sh -c functional -x && ./2026test.sh -c h_functional -x && ./2026test.sh -c performance -x
# 全门禁(关键节点,~30分钟)
./2026test.sh # 全量
- 绝不跳过门禁。功能测试失败不进入性能测量。门禁失败修复后重跑,不允许"先合并后修复"。
- 指令数基线:
./count_asm.sh;指令数基线.md记录历史最低值。性能退化 >5 用例阻止合并,2-5 用例标记关注。
代码规范
- 一定中文交流、注释、commit message、文档。标识符/文件名用英文。
- 变量
snake_case,函数/类PascalCase,成员变量snake_case_ - Git:
<type>(<scope>): <中文简述>。一 commit 一逻辑变更。不提交编译或测试失败的代码。功能分支开发,master 保护。
MCP 使用
| 场景 | 工具 | 不要 |
|---|---|---|
| 查找符号 | codegraph_search |
不要 grep |
| 调用关系 | codegraph_callers/callees |
不要手动 Read 追踪 |
| 改动影响 | codegraph_impact |
不要猜测 |
| 代码区探索 | codegraph_explore(一次) |
不要逐个 codegraph_node |
| 字面量 | grep |
不要用 codegraph |
WebFetch 不可用(DeepSeek 后端域名校验失败),用 bash scripts/fetch.sh <url> 替代。