Skip to content

Instantly share code, notes, and snippets.

@nasser
Created June 8, 2015 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasser/7e79f125452d91a7fcd0 to your computer and use it in GitHub Desktop.
Save nasser/7e79f125452d91a7fcd0 to your computer and use it in GitHub Desktop.
;; https://pauladamsmith.com/blog/2015/01/how-to-get-started-with-llvm-c-api.html
(import LLVM
LLVMModuleRef
LLVMTypeRef
Wrap)
(let [module (LLVM/ModuleCreateWithName "my_module")
params (into-array [(LLVM/Int32Type) (LLVM/Int32Type)])
fntyp (LLVM/FunctionType (LLVM/Int32Type) params false)
sum (Wrap/AddFunction module "sum" fntyp)
entry (LLVM/AppendBasicBlock sum "entry")
builder (LLVM/CreateBuilder)
_ (LLVM/PositionBuilderAtEnd builder entry)
tmp (LLVM/BuildAdd builder
(LLVM/GetParam sum 0)
(LLVM/GetParam sum 1)
"tmp")
_ (LLVM/BuildRet builder tmp)
]
(LLVM/WriteBitcodeToFile module "sum.bc")
sum)
define i32 @sum(i32, i32) {
entry:
%tmp = add i32 %0, %1
ret i32 %tmp
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment