Dependencies
用Moonbit重写qbe
///|
test {
let src =
#|export function w $add(w %a, w %b) {
#|@start
#| %s =w add %a, %b
#| ret %s
#|}
#|
match @qbe.compile(src) {
Ok(assembly) => {
assert_true(assembly.contains("addl"))
assert_true(assembly.contains("add:"))
}
Err(_) => fail("compile failed")
}
match @qbe.compile_debug(src, "P") {
Ok(dump) => assert_true(dump.contains("After parsing"))
Err(_) => fail("compile failed")
}
}///|
test {
let src =
#|export function w $add(w %a, w %b) {
#|@start
#| %s =w add %a, %b
#| ret %s
#|}
#|
match @qbe.compile_wasm(src) {
Ok(wat) => {
assert_true(wat.contains("(func $add"))
assert_true(wat.contains("i32.add"))
}
Err(_) => fail("wasm compile failed")
}
}///|
test {
let src =
#|export function w $add(w %a, w %b) {
#|@start
#| %s =w add %a, %b
#| ret %s
#|}
#|
match @qbe.compile_rv64(src) {
Ok(assembly) => {
assert_true(assembly.contains("add:"))
assert_true(assembly.contains("addw a0, a0, a1"))
}
Err(_) => fail("rv64 compile failed")
}
}| 阶段 | 包 | 说明 |
|---|---|---|
| 数据结构 | types | SSA 中间表示:Fn/Blk/Ins/Phi/Jump/Con/Tmp/Dat 等,全部后端包共享 |
| 通用工具 | util | 错误类型、字符串驻留 (Interner)、输出、排序 |
| 词法分析 | lexer | IL 文本 → token 序列,错误收集到 err_msgs 而非抛异常 |
| 语法分析 | parser | token 序列 → Fn/Dat/Typ,含 type/data/function 三种顶层定义 |
| CFG 分析 | cfg | 反向后序、前驱、支配者树、支配边界、循环深度、别名分析、跳转简化 |
| SSA 构造 | ssa | 使用链、memopt、phi 插入、块重命名、loadopt、copy 传播、合法性检查 |
| 常量折叠 | fold | 操作数均为常量的指令直接求值并替换 |
| Wasm ABI | abi_wasm | wasm 调用约定:Par/Arg→Nop,Call 简化 |
| Wasm 指令选择 | isel_wasm | wasm op 映射、地址模式分解、CFG→结构化控制流 |
| Wasm 汇编输出 | emit_wasm | WAT 文本格式输出 |
| ABI 处理 | abi | System V AMD64 调用约定:参数/返回寄存器、栈溢出、vararg |
| 指令选择 | isel | amd64 指令模式:立即数、地址模式、除法魔法数、条件跳转 |
| 活跃分析 | live | 反向数据流求 in/out,块边界统计 nlive_w/nlive_d |
| 寄存器溢出 | spill | 基于代价与循环加权选择溢出点,迭代到收敛 |
| 寄存器分配 | rega | 基于活跃集合构建干涉图,贪心染色 |
| 汇编输出 | emit | 渲染 GAS 汇编(Linux .L/macOS L、_ 前缀) |
| RISC-V ABI | abi_rv64 | rv64 调用约定:A0–A7 / FA0–FA7 参数与返回、聚合类型拆分 |
| RISC-V 指令选择 | isel_rv64 | rv64 指令映射、比较+分支合并 |
| RISC-V 汇编输出 | emit_rv64 | RISC-V GAS 文本输出 |
| CLI 入口 | cmd/main | 参数解析与文件 I/O(薄壳,调用 @qbe facade,-t 选目标) |
| 库入口 | . | 统一编译 API compile / compile_debug 与 IR 类型再导出 |
parse → fillrpo → fillpreds → filluse → memopt
→ filldom → fillfron → filllive(false) → phiins → renblk → filluse → ssacheck
→ fillloop → fillalias → loadopt → filluse → ssacheck
→ copy → filluse → fold
→ abi → fillpreds → filluse
→ isel
→ fillrpo → filllive → fillcost → spill → rega
→ fillrpo → simpljmp → fillrpo → fillpreds
→ emitfnparse → fillrpo → fillpreds → filluse → memopt
→ filldom → fillfron → filllive(false) → phiins → renblk → filluse → ssacheck
→ fillloop → fillalias → loadopt → filluse → ssacheck
→ copy → filluse → fold
→ abi_wasm → fillpreds → filluse
→ isel_wasm
→ [跳过 spill/rega — wasm 无物理寄存器]
→ emit_wasmparse → fillrpo → fillpreds → filluse → memopt
→ filldom → fillfron → filllive(false) → phiins → renblk → filluse → ssacheck
→ fillloop → fillalias → loadopt → filluse → ssacheck
→ copy → filluse → fold
→ abi_rv64 → fillpreds → filluse
→ isel_rv64
→ init_rv64_target() ← 切换 TargetCfg(寄存器布局)
→ fillrpo → filllive → fillcost → spill → rega
→ fillrpo → simpljmp → fillrpo → fillpreds
→ emit_rv64| amd64_sysv | wasm | rv64 | |
|---|---|---|---|
| 库入口 | compile / compile_debug | compile_wasm / compile_wasm_debug | compile_rv64 / compile_rv64_debug |
| CLI | -t amd64_sysv(默认) | -t wasm | -t rv64 |
| 输出 | x86-64 GAS | WAT | RISC-V GAS |
| 寄存器分配 | spill + rega | 跳过(栈机) | spill + rega(TargetCfg 切换) |
| 验证强度 | 差分回归逐字节 | 单测 + 快照 | 仅单测(无参考基线) |
© 2015-2017 Quentin Carbonneaux quentin@c9x.me
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.Install
Download zipDependencies