Dependencies
A MoonBit rewrite of 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")
}
}| Phase | Package | Description |
|---|---|---|
| Data Structures | types | SSA IR: Fn/Blk/Ins/Phi/Jump/Con/Tmp/Dat etc., shared by all backend packages |
| Utilities | util | Error types, string interning (Interner), output, sorting |
| Lexing | lexer | IL text → token sequence, errors collected to err_msgs instead of exceptions |
| Parsing | parser | Token sequence → Fn/Dat/Typ, supports type/data/function three top-level definitions |
| CFG Analysis | cfg | Reverse postorder, predecessors, dominator tree, dominance frontiers, loop depth, alias analysis, jump simplification |
| SSA Construction | ssa | Use chains, memopt, phi insertion, block renaming, loadopt, copy propagation, validity checking |
| Constant Folding | fold | Directly evaluates instructions whose operands are all constants and replaces with references |
| Wasm ABI | abi_wasm | Wasm calling convention: Par/Arg→Nop, Call simplification |
| Wasm Instruction Selection | isel_wasm | Wasm op mapping, address mode decomposition, CFG→structured control flow |
| Wasm Assembly Output | emit_wasm | WAT text format output |
| ABI Processing | abi | System V AMD64 calling convention: parameter/return registers, stack spilling, vararg |
| Instruction Selection | isel | amd64 instruction patterns: immediates, address modes, division magic numbers, conditional jumps |
| Liveness Analysis | live | Backward data flow to compute in/out, block boundary statistics nlive_w/nlive_d |
| Register Spilling | spill | Cost-based and loop-weighted spilling point selection, iterates to convergence |
| Register Allocation | rega | Builds interference graph from live sets, greedy coloring |
| Assembly Output | emit | Renders GAS assembly (Linux .L/macOS L, _ prefix) |
| RISC-V ABI | abi_rv64 | rv64 calling convention: A0–A7 / FA0–FA7 parameters and returns, aggregate type splitting |
| RISC-V Instruction Selection | isel_rv64 | rv64 instruction mapping, compare+branch merging |
| RISC-V Assembly Output | emit_rv64 | RISC-V GAS text output |
| CLI Entry | cmd/main | Argument parsing and file I/O (thin shell, calls @qbe facade, -t selects target) |
| Library Entry | . | Unified compilation API compile / compile_debug and IR type re-exports |
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
→ [skip spill/rega — wasm has no physical registers]
→ 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() ← switch TargetCfg (register layout)
→ fillrpo → filllive → fillcost → spill → rega
→ fillrpo → simpljmp → fillrpo → fillpreds
→ emit_rv64| amd64_sysv | wasm | rv64 | |
|---|---|---|---|
| Library entry | compile / compile_debug | compile_wasm / compile_wasm_debug | compile_rv64 / compile_rv64_debug |
| CLI | -t amd64_sysv (default) | -t wasm | -t rv64 |
| Output | x86-64 GAS | WAT | RISC-V GAS |
| Register allocation | spill + rega | skipped (stack machine) | spill + rega (TargetCfg switch) |
| Validation strength | Differential regression byte-by-byte | Unit tests + snapshots | Unit tests only (no reference baseline) |
© 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