Moonbit version of LLVM, A Tiny, Friendly Companion to LLVM
Dependencies
Need to generate assembly or machine code? For that, MoonLLVM works hand‑in‑hand with MoonMIR, a separate library that takes IR to target-specific assembly.
MoonLLVM = The best complement to LLVM for MoonBit developers.
moon update
moon add Kaida-Amethyst/MoonLLVMfn main {
// 1. Set up the context and IR builder.
let ctx = @IR.Context::new()
let mod = ctx.addModule("demo")
let builder = IRBuilder::new()
// 2. Define the function type: i32 add(i32, i32).
let i32_t = ctx.getInt32Ty()
let f_type = ctx.getFunctionType(i32_t, [i32_t, i32_t])
// 3. Add the function to the module and create an entry block.
let f = mod.addFunction(f_type, "add")
let bb = f.addBasicBlock(name="entry")
builder.setInsertPoint(bb)
// 4. Get the function args and name them.
let x = f.getArg(0).unwrap()
x.setName("x")
let y = f.getArg(1).unwrap()
y.setName("y")
// 5. Create the add instruction and return.
let sum = builder.createAdd(x, y, name="sum")
builder.createRet(sum)
// 6. Print the generated LLVM IR.
println(mod)
}moon run main.mbt > add.llllc -o add.s add.ll🌟 MoonBit 开发者眼中,最舒服、最易懂的 LLVM 补充。
需要生成汇编 / 机器码? 请搭配 MoonMIR 使用,它是一个用 MoonBit 实现的后端管线,可以把 IR 降成具体架构的汇编代码。
与 LLVM 紧密互操作的轻量伙伴,为 MoonBit 生态补齐「好读、好学、好改」的那一块。
moon update
moon add Kaida-Amethyst/MoonLLVMfn main {
// 1. 创建上下文和 IR builder。
let ctx = @IR.Context::new()
let mod = ctx.addModule("demo")
let builder = ctx.createBuilder()
// 2. 定义函数类型:i32 add(i32, i32)。
let i32_t = ctx.getInt32Ty()
let f_type = ctx.getFunctionType(i32_t, [i32_t, i32_t])
// 3. 向模块中添加函数并创建入口基本块。
let f = mod.addFunction(f_type, "add")
let bb = f.addBasicBlock(name="entry")
builder.setInsertPoint(bb)
// 4. 获取参数并命名。
let x = f.getArg(0).unwrap()
x.setName("x")
let y = f.getArg(1).unwrap()
y.setName("y")
// 5. 创建加法指令和返回指令。
let sum = builder.createAdd(x, y, name="sum")
let _ = builder.createRet(sum)
// 6. 打印生成的 LLVM IR。
println(mod)
}moon run main.mbt > add.llllc -o add.s add.llMoonbit version of LLVM, A Tiny, Friendly Companion to LLVM
Dependencies