llvm-c binding for Moonbit with friendly API, keeps the Cpp Style.
moon add Kaida-Amethyst/llvmimport {
"Kaida-Amethyst/llvm/IR" @llvm,
}preferred_target = "native"///|
test "build an integer addition function" {
let ctx = @llvm.Context::new()
let mod = ctx.addModule("example")
let builder = ctx.createBuilder()
let i32_ty = ctx.getInt32Ty()
let function_ty = ctx.getFunctionType(i32_ty, [i32_ty, i32_ty])
let function = mod.addFunction(function_ty, "add")
let entry = function.addBasicBlock(name="entry")
guard function.getArg(0) is Some(lhs) else {
fail("missing lhs")
}
guard function.getArg(1) is Some(rhs) else {
fail("missing rhs")
}
builder.setInsertPoint(entry)
let sum = builder.createAdd(lhs, rhs, name="sum")
builder.createRet(sum) |> ignore
mod.verify()
inspect(
function,
content=(
#|define i32 @add(i32 %0, i32 %1) {
#|entry:
#| %sum = add i32 %0, %1
#| ret i32 %sum
#|}
#|
),
)
}| Host | Minimum version |
|---|---|
| macOS ARM64 | macOS 11 |
| Linux x86_64 | glibc 2.31 |
moon info
moon fmt
moon check --target native
moon test --target nativemoon add Kaida-Amethyst/llvmimport {
"Kaida-Amethyst/llvm/IR" @llvm,
}preferred_target = "native"///|
test "构造整数加法函数" {
let ctx = @llvm.Context::new()
let mod = ctx.addModule("example")
let builder = ctx.createBuilder()
let i32_ty = ctx.getInt32Ty()
let function_ty = ctx.getFunctionType(i32_ty, [i32_ty, i32_ty])
let function = mod.addFunction(function_ty, "add")
let entry = function.addBasicBlock(name="entry")
guard function.getArg(0) is Some(lhs) else {
fail("缺少 lhs")
}
guard function.getArg(1) is Some(rhs) else {
fail("缺少 rhs")
}
builder.setInsertPoint(entry)
let sum = builder.createAdd(lhs, rhs, name="sum")
builder.createRet(sum) |> ignore
mod.verify()
inspect(
function,
content=(
#|define i32 @add(i32 %0, i32 %1) {
#|entry:
#| %sum = add i32 %0, %1
#| ret i32 %sum
#|}
#|
),
)
}| 宿主平台 | 最低版本 |
|---|---|
| macOS ARM64 | macOS 11 |
| Linux x86_64 | glibc 2.31 |
moon info
moon fmt
moon check --target native
moon test --target nativellvm-c binding for Moonbit with friendly API, keeps the Cpp Style.