--- marp: true theme: default size: 16:9 paginate: true header: "SysY 编译器课程实验 — 并行编译优化" footer: "程景愉 · 舒钰权 · 杨力嘉 | 实验验收汇报 | 2026.06.27" style: | :root { --ink: #0f172a; --muted: #475569; --soft: #64748b; --line: rgba(15, 23, 42, 0.10); --blue: #2563eb; --cyan: #0891b2; --gold: #d97706; --paper: #fffdf8; --paper-blue: #f4f8ff; } section { position: relative; padding: 48px 64px 42px; font-family: 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif; font-size: 25px; line-height: 1.35; color: var(--ink); background: url('csc-logo.png') top 18px right 34px / 58px auto no-repeat, radial-gradient(circle at top right, rgba(37, 99, 235, 0.16), transparent 28%), radial-gradient(circle at bottom left, rgba(8, 145, 178, 0.12), transparent 24%), linear-gradient(135deg, var(--paper) 0%, #f9fbff 45%, var(--paper-blue) 100%); } section::before { content: ""; position: absolute; inset: 22px; border: 1px solid var(--line); border-radius: 26px; pointer-events: none; z-index: 0; } section > * { position: relative; z-index: 1; } header, footer { color: #64748b; font-size: 13px; letter-spacing: 0.04em; } h1 { margin: 0 0 14px; font-family: 'Noto Serif CJK SC', 'STSong', serif; font-size: 52px; color: var(--ink); letter-spacing: -0.02em; } h2 { margin: 0 0 10px; font-size: 28px; color: var(--blue); font-weight: 700; } h3 { margin: 0 0 6px; font-size: 20px; color: var(--ink); font-weight: 700; } p, li { color: var(--muted); } strong { color: var(--blue); font-weight: 800; } code { font-size: 0.85em; color: #0f3c8a; background: rgba(37, 99, 235, 0.10); padding: 0.08em 0.35em; border-radius: 0.35em; } ul { margin: 0.25em 0 0; } li { margin: 0.16em 0; } section.cover { color: #e8eefc; background: url('csc-logo.png') top 18px right 34px / 65px auto no-repeat, radial-gradient(circle at top right, rgba(255, 255, 255, 0.14), transparent 24%), linear-gradient(135deg, #07111f 0%, #0f1f3d 42%, #1d4ed8 100%); } section.cover::before { border-color: rgba(255, 255, 255, 0.14); } section.cover header, section.cover footer, section.cover p, section.cover li, section.cover .muted, section.cover .eyebrow { color: rgba(232, 238, 252, 0.78); } section.cover h1, section.cover h2, section.cover h3, section.cover strong { color: #ffffff; } .eyebrow { font-size: 15px; font-weight: 700; letter-spacing: 0.16em; text-transform: uppercase; color: var(--soft); margin-bottom: 16px; } .lead { font-size: 18px; line-height: 1.45; margin-top: 6px; color: var(--muted); } .muted { color: var(--soft); } .chips { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 18px; } .chip { display: inline-block; padding: 7px 14px; border-radius: 999px; border: 1px solid rgba(255, 255, 255, 0.16); background: rgba(255, 255, 255, 0.08); font-size: 15px; color: #eef4ff; } .cover-link, .cover-link code { color: #f8fbff !important; background: rgba(255, 255, 255, 0.10); border: 1px solid rgba(255, 255, 255, 0.14); } .grid-2, .grid-3, .grid-4, .timeline { display: grid; gap: 14px; } .grid-2 { grid-template-columns: 1fr 1fr; } .grid-3 { grid-template-columns: repeat(3, 1fr); } .grid-4, .timeline { grid-template-columns: repeat(4, 1fr); } .card { background: rgba(255, 255, 255, 0.72); border: 1px solid rgba(148, 163, 184, 0.18); border-radius: 20px; padding: 14px 18px; box-shadow: 0 16px 38px rgba(15, 23, 42, 0.06); } .card h3 { margin-bottom: 10px; } .card strong { color: var(--gold); } .accent { color: var(--blue); font-weight: 800; } .metric { font-size: 15px; color: var(--soft); margin-top: 8px; } .flow { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; margin-top: 10px; } .flow-box { padding: 10px 6px; border-radius: 16px; text-align: center; background: rgba(37, 99, 235, 0.08); border: 1px solid rgba(37, 99, 235, 0.12); color: var(--ink); font-size: 15px; font-weight: 700; } .flow-box.dark { background: rgba(15, 23, 42, 0.88); color: #f8fbff; border-color: rgba(15, 23, 42, 0.9); } .arrow { text-align: center; color: var(--soft); font-size: 20px; align-self: center; } .stack { display: grid; grid-template-columns: 0.95fr 1.05fr; gap: 16px; margin-top: 8px; } .tech-list { display: grid; gap: 8px; margin-top: 4px; } .tech-item { border-radius: 16px; padding: 10px 14px; background: rgba(37, 99, 235, 0.06); border: 1px solid rgba(37, 99, 235, 0.10); font-size: 15px; line-height: 1.4; color: var(--muted); } .tech-item strong { display: inline-block; min-width: 5.2em; color: var(--ink); } .table { width: 100%; border-collapse: collapse; font-size: 15px; } .table th, .table td { border-bottom: 1px solid rgba(148, 163, 184, 0.22); padding: 6px 0; text-align: left; vertical-align: top; } .table th { color: var(--ink); font-weight: 700; } .mini { font-size: 14px; color: var(--soft); } .phase { background: rgba(255, 255, 255, 0.78); border: 1px solid rgba(148, 163, 184, 0.18); border-radius: 18px; padding: 14px; box-shadow: 0 12px 30px rgba(15, 23, 42, 0.05); } .phase .tag { display: inline-block; padding: 4px 8px; border-radius: 999px; background: rgba(37, 99, 235, 0.12); color: var(--blue); font-size: 12px; font-weight: 700; margin-bottom: 8px; } .role-tag { display: inline-block; padding: 4px 8px; border-radius: 999px; background: rgba(217, 119, 6, 0.10); color: var(--gold); font-size: 12px; font-weight: 700; margin-bottom: 6px; } .roles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-top: 6px; } .role-card { background: rgba(255, 255, 255, 0.76); border: 1px solid rgba(148, 163, 184, 0.18); border-radius: 18px; padding: 14px 14px 12px; box-shadow: 0 12px 30px rgba(15, 23, 42, 0.05); } .role-card h3 { font-size: 18px; margin-bottom: 6px; } .role-card ul { margin-top: 0.15em; padding-left: 1.1em; } .role-card li { font-size: 14px; line-height: 1.4; margin: 0.12em 0; } .closing { display: grid; place-items: center; height: 78%; text-align: center; } .closing h1 { font-size: 64px; margin-bottom: 12px; } .closing p { font-size: 24px; } ---
从 SysY 语言到 AArch64 汇编的完整编译器实现,涵盖语法分析、IR 生成、标量优化、寄存器分配与循环优化六大实验模块。
仓库地址:https://bdgit.educoder.net/pw7hgk2xf/nudt-compiler-cpp
--- # 项目概述与实验目标实现从 SysY 源程序到 AArch64 汇编的完整编译流程,打通前端解析、中端优化与后端代码生成的全部环节。
按 Lab1–Lab6 逐步完成语法树构建、IR 生成、指令选择、标量优化、寄存器分配与循环优化,形成层次清晰的编译器架构。
基于 Git 分支协作、CMake 构建系统、自动化测试脚本与 QEMU 模拟验证,完整复现工业级编译器开发流程。
SysY.g4 文法,覆盖完整 SysY 语言规范SyntaxTreePrinter 语法树可视化ANTLR4 的 labeled alternative 写法直接影响 SysYParser::*Context 类型名与访问接口,为后续 sem/irgen 的 Visitor 适配奠定基础。扩展文法后必须同步调整语义分析与 IR 生成的 visit* 逻辑。
i32 / float / 数组 / 指针成果:支持 int/float 常量表达式、多维数组、函数调用、短路求值、控制流,生成的 IR 通过 llc 编译与 clang 链接运行验证。
完整实现算术、比较、条件选择、分支跳转、函数调用、内存访存、浮点转换等核心指令子集,采用高可靠栈槽模型保证变量活跃期正确性。
完整实现前 8 个整型/指针参数及前 8 个浮点参数通过寄存器传递,返回值通过 w0/x0/s0 返回,支持多函数多基本块控制流。
浮点常量以 .word <bits> 二进制字面量输出,保证编译-汇编-运行全生命周期 100% 位一致,消除精度丢失问题。
解决双向迭代器/指针失效(vector 重配 → 野指针)、大栈帧寻址越界(ldur/stur 超出 [-256,255] 范围 → 寄存器偏移寻址回退)、参数指针二级间接等底层问题。重写 sylib/sylib.c 运行库,补齐全部 I/O 与十六进制浮点(%a)支持。
在栈槽后端正确处理 Phi 生命周期:控制流分叉块末尾生成条件拷贝(Condition Copy-Store),函数头部预分配 Phi 槽位。修复指针截断(64→32 位)、GEP 参数二级指针解引用、ConstProp 后 Phi 残留清理等隐蔽缺陷。
mov w8, w8 等无意义指令浮点常数加载(adrp + ldr)隐式占用通用寄存器 x8/w8,若窥孔器未感知会导致寄存器污染,引发浮点比较错误。解决方案:识别 MovImm 目标为浮点寄存器时,主动擦除 slot_to_reg 追踪中的 x8/w8 条目。
支配边界计算死循环漏洞:DCE/CFGSimplify 后可能产生从 Entry 不可达的死块,其 idom 为空或自环,导致 ComputeDF 的 while 循环无限挂起。修复方案:增加 runner == nullptr 与 next_runner == runner 的优雅阻断分支。
修正了 SysY.g4 中同级运算符分行写导致结合性错误的 ANTLR4 缺陷。将其合并为 addSubExp/mulDivModExp 等统一规则,并重构 Sema 与 IRGen 遍历逻辑,彻底解决了 fft0.sy 等复杂表达式的计算 Bug。
针对大局部数组初始化生成的几十万条冗余 store 指令进行了优化。在中端 IR 生成阶段,改用运行时 memset 函数调用,消除了汇编代码膨胀,使编译时间缩短 99% 以上。
在后端 Peephole 阶段新增了死栈槽分析。静态扫描发现从未被 load 或取地址的冗余 Store 槽位,直接予以删除,进一步缩减了栈帧空间并精简了指令流。
通过这几次的系统性优化与缺陷修复,编译器在语义正确性、编译效率、以及生成代码的精简度上均得到了极大的提升,实现了 21 个回归测试的 100% 完美通过。
--- # 关键技术难点与突破全局常量初始化走纯编译期求值,绝不生成 IR 指令。避免依赖 Runtime IRBuilder 插入点。
标量 alloca、聚合数组基址、数组形参指针退化三种语义严格区分,避免 Load/GEP 类型错乱。
常量折叠→IEEE 754 二进制位→.word 原样输出,确保全链路位精确一致。
ConstProp 后显式清理 Phi dead incoming 边;CFGSimplify 正确替换 Phi uses;Mem2Reg 沿支配树 DFS 栈式管理版本。
Vector 预分配容量避免迭代器失效;64 位指针强制 X 寄存器加载;参数 alloca 栈槽通过静态扫描提升至 8 字节。
ComputeDF 对不可达节点与自环做显式阻断;迭代 IDom 兼容非连通图与临时死块,保证收敛。
全部 11 项功能测试用例 与 10 项性能测试用例(共 21 项)在优化开启条件下均通过 verify_asm.sh --run 验证,输出与退出码 100% 匹配预期。
验证链路:SysY 源码 → IR 生成 → 标量优化 → 循环优化 → 指令选择 → 寄存器分配 → 窥孔优化 → AArch64 汇编 → QEMU 模拟运行 → 输出比对
--- # 人员分工分工遵循"组长主抓中端优化核心 + 成员按前后端专长协作推进"的模式,通过 Git 分支 + PR 评审完成协作。
--- # 实验总结与展望本项目已构建起一个结构清晰、可扩展、语义正确的 SysY 编译器框架,为后续继续深入编译器优化与并行化研究提供了坚实的基础。
---Conclusion
从语法树到 AArch64 汇编,从 SSA 优化到循环不变式外提
我们构建了一个完整、正确、可扩展的 SysY 编译器
Q & A
程景愉 · 舒钰权 · 杨力嘉 | 并行编译优化课程实验