An open API service indexing awesome lists of open source software.

https://github.com/element39/bun-llvm

🍔 use LLVM with bun effortlessly
https://github.com/element39/bun-llvm

bindings bun c cpp ffi go ir javascript llvm llvm-bindings rust typescript zig

Last synced: 3 months ago
JSON representation

🍔 use LLVM with bun effortlessly

Awesome Lists containing this project

README

          

# 🍔 bun-llvm
> ` use LLVM with bun effortlessly `

```ts
import LLVM from "./llvm";

function main() {
// initialize LLVM
const ctx = new LLVM.Context();
const mod = new LLVM.Module("demo", ctx);
const builder = new LLVM.IRBuilder(ctx);

// define types
const i32 = LLVM.Type.int32(ctx);
const fnType = new LLVM.FunctionType([i32, i32], i32);

// {} are used to separate scopes (optional)
{
// create the function
const fn = mod.createFunction("add", fnType, { linkage: LLVM.Linkage.External });
const entry = fn.addBlock("entry");

builder.insertInto(entry);

{
// get parameters & add them
const [a, b] = fn.getArgs();
const sum = builder.add(a, b);

// return the result
builder.ret(sum);
}

// make sure the function is valid
fn.verify();
}

// verify the module
mod.verify();

// output IR
console.log(mod.toString());
}

main();
```