Sign in

    qbe

    Download zip
    Author
    Version
    0.21.2
    License
    Apache-2.0
    Last updated
    28 days ago
    Downloads
    61

    #Qbe.mbt

    A QBE reimplementation in MoonBit — the upstream compiler plus a programmatic IR builder, an in-memory JIT, and LoongArch64/WASM backends

    #Features

    • Byte-faithful QBE core: the optimizer pipeline and the amd64 / arm64 / rv64 backends, checked byte-for-byte against the pinned vendor/qbe reference (661ceb2) — every debug stage and the emitted assembly (406/406 per target)
    • Extra backends: LoongArch64 and WASM
    • Programmatic IR builder (ir_builder, C ABI ir_builder_capi, include/qbe_builder.h): construct QBE IL from MoonBit or C with no .ssa round-trip, print it back, or emit assembly text, a Mach-O arm64 object, or a JIT-ready code image
    • In-memory JIT for macOS/aarch64 (jit/): map and call arm64 machine code with no toolchain in the path, resolving libc via dlsym and host callbacks; usable from the C ABI or Rust
    • Rust glue layer (rust/, crate qbe-builder): Cranelift/inkwell-style Context / Module / FunctionBuilder / ins(), including Module::jit
    • Layered runtime: native/ (executable memory, symbol lookup, temp files, process spawn, dynamic linking) shared by jit/ and run_asm/
    • Debug info: the IL dbgfile/dbgloc statements emit .file/.loc DWARF line tables (byte-identical to the reference); -g adds a DWARF5 compilation unit and CFI/.eh_frame, giving source-level lldb debugging (doc/debug_info.md)

    #Status

    • amd64 / arm64 / rv64 debug dumps and assembly are byte-identical to vendor/qbe; the self-contained arm64 object matches clang on all 336 compilable cases (python tools/check_route_b.py)
    • --jit FUNC[,ARG]... runs code in-process; --run-asm FUNC[,ARG] does the same through clang; --run-wasm FUNC[,ARG] runs the WASM backend under node; --emit obj writes a self-contained Mach-O arm64 .o
    • dbgfile/dbgloc line info is emitted for amd64/arm64/rv64/la64 and matches vendor/qbe on every debug flag; -g adds a DWARF5 CU + CFI (.eh_frame) and is verified end to end under lldb

    #Roadmap

    • Debug info: variable/type DIEs (builder metadata), rv64/la64 CFI, and DWARF in the self-contained object / JIT path (doc/debug_info.md)
    • (TODO) IR debugger

    #Contributors

    #Quick start

    Clone and bootstrap on macOS or Linux:

    git clone https://github.com/azhzx/qbe.mbt.git cd qbe.mbt ./scripts/bootstrap.sh

    If MoonBit is already installed, use --skip-install to avoid installation attempts while still running smoke checks:

    ./scripts/bootstrap.sh --skip-install

    To allow automatic MoonBit installation when moon is missing:

    ./scripts/bootstrap.sh --install-moon

    Documentation:

    BSet

    using @azhzx/qbe/types { type BSet }

    Blk

    using @azhzx/qbe/types { type Blk }

    Class

    using @azhzx/qbe/types { type Class }

    Class - corresponds to enum Class in all.h

    Con

    using @azhzx/qbe/types { type Con }

    Dat

    using @azhzx/qbe/types { type Dat }

    using @azhzx/qbe/types { type Fn }

    Ins

    using @azhzx/qbe/types { type Ins }

    InterpValue

    Jump

    using @azhzx/qbe/types { type Jump }

    using @azhzx/qbe/types { type Op }

    Ref

    using @azhzx/qbe/types { type Ref }

    ADT replacing C bit-field (uint type:3; uint val:29)

    Tmp

    using @azhzx/qbe/types { type Tmp }

    Typ

    using @azhzx/qbe/types { type Typ }

    Target

    pub(all) enum Target {
    Amd64Sysv
    Wasm32
    Rv64
    La64
    Arm64
    }

    arm64_function_signature

    fn arm64_function_signature(text : String, name : String) -> (Array[Int], Int) raise

    compile

    fn compile(text : String, gas? : String) -> Result[String,
    QbeError
    ]

    compile_arm64

    fn compile_arm64(text : String, gas? : String) -> Result[String,
    QbeError
    ]

    compile_arm64_bin

    fn compile_arm64_bin(text : String) -> Array[Array[Int]] raise

    compile_arm64_bin_module

    fn compile_arm64_bin_module(text : String) ->
    Arm64BinModule
    raise

    compile_arm64_bin_module_ex

    compile_arm64_debug

    fn compile_arm64_debug(text : String, flags : String) -> Result[String,
    QbeError
    ]

    compile_arm64_object

    fn compile_arm64_object(text : String) -> Bytes raise

    compile_debug

    fn compile_debug(text : String, flags : String) -> Result[String,
    QbeError
    ]

    compile_ir_asm

    fn compile_ir_asm(funcs : Array[
    Fn
    ], datas : Array[
    Dat
    ], order : Array[String], typs : Array[
    Typ
    ], interner :
    Interner
    , gas? : String) -> String raise

    compile_ir_object

    fn compile_ir_object(funcs : Array[
    Fn
    ], datas : Array[
    Dat
    ], typs : Array[
    Typ
    ], interner :
    Interner
    ) -> Bytes raise

    compile_la64

    fn compile_la64(text : String, gas? : String) -> Result[String,
    QbeError
    ]

    compile_la64_debug

    fn compile_la64_debug(text : String, flags : String) -> Result[String,
    QbeError
    ]

    compile_rv64

    fn compile_rv64(text : String, gas? : String) -> Result[String,
    QbeError
    ]

    compile_rv64_debug

    fn compile_rv64_debug(text : String, flags : String) -> Result[String,
    QbeError
    ]

    compile_target

    fn compile_target(text : String, target : Target, gas? : String) -> Result[String,
    QbeError
    ]

    compile_target_debug

    fn compile_target_debug(text : String, target : Target, flags : String) -> Result[String,
    QbeError
    ]

    compile_wasm

    fn compile_wasm(text : String) -> Result[String,
    QbeError
    ]

    compile_wasm_debug

    fn compile_wasm_debug(text : String, flags : String) -> Result[String,
    QbeError
    ]

    interpret

    fn interpret(src : String, entry? : String, args? : Array[
    InterpValue
    ], max_steps? : Int, max_depth? : Int, hook? : (String, Array[
    InterpValue
    ]) ->
    InterpValue
    ??) -> Result[(
    InterpValue
    , String),
    QbeError
    ]