Start to clean things up to get into a state usable by others.
[cl-llvm.git] / src / stuff.lisp
1 (in-package :llvm)
2 ;; Load up the native codegen.
3 (cffi:defcfun ("CLLLVM_LLVMInitializeNativeTarget" CLLLVM_LLVMInitializeNativeTarget) :int)
4 (cffi:defcfun ("CLLLVM_LLVMModuleProviderGetModule" CLLLVM_LLVMModuleProviderGetModule) :pointer
5 (modprovider :pointer))
6 (cffi:defcfun ("CLLLVM_LLVMParseAssemblyString" CLLLVM_LLVMParseAssemblyString) :pointer
7 (asm-string :string)
8 (module :pointer)
9 (context :pointer))
11 (CLLLVM_LLVMInitializeNativeTarget)
13 ;; A global context. Most of LLVM is only thread-safe within a single "context". There is an
14 ;; internal-to-LLVM C default global context, implicitly used by a number of functions like
15 ;; LLVM*Type (without InContext on the end), but I make a lisp-side global context here for clarity.
17 ;; Delete with LLVMContextDispose when done.
19 ;; ...FIXME...or not. For some reason making a custom context makes things fail to verify, stating
20 ;; that I've mixed contexts. Need to figure out if this is my fault or a bug in LLVM.
22 ;; So just use the standard global context
23 ;; (defvar *llvm-context* (LLVMContextCreate))
24 (defvar *llvm-context* (LLVMGetGlobalContext))
26 ;; Top-level LLVM module for running the JIT in. Other modules can be made for codegen-to-disk, but
27 ;; only a single module for JIT execution can exist in the process.
28 (defvar *jit-module* (LLVMModuleCreateWithName "jit-module"))
30 ;; Module provider...dunno what the purpose of this is, it wraps the module
31 ;; Delete with LLVMDisposeModuleProvider; don't delete the wrapped module
32 (defvar *jit-module-provider* (LLVMCreateModuleProviderForExistingModule *jit-module*))
34 ;; Create the JIT compiler, optimization level 2 (whatever that means). This call fails if you run
35 ;; it twice in a process. (which is why we can have only one module for JIT code)
36 (defvar *jit-execution-engine* (LLVMCreateJITCompiler *jit-module-provider* 2))
38 ;; Optimization passes. Cleanup with LLVMDisposePassManager.
39 (defvar *jit-pass-manager* (LLVMCreateFunctionPassManager *jit-module-provider*))
40 (let ((pass *jit-pass-manager*))
41 (LLVMAddTargetData (LLVMGetExecutionEngineTargetData *jit-execution-engine*) pass)
42 (LLVMAddConstantPropagationPass pass)
43 (LLVMAddInstructionCombiningPass pass)
44 (LLVMAddPromoteMemoryToRegisterPass pass)
45 (LLVMAddGVNPass pass)
46 (LLVMAddCFGSimplificationPass pass))