1 //===- executionengine.go - Bindings for executionengine ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines bindings for the executionengine component.
12 //===----------------------------------------------------------------------===//
17 #include "llvm-c/Core.h"
18 #include "llvm-c/ExecutionEngine.h"
25 func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
26 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
28 type GenericValue struct {
29 C C.LLVMGenericValueRef
31 type ExecutionEngine struct {
32 C C.LLVMExecutionEngineRef
35 type MCJITCompilerOptions struct {
36 C C.struct_LLVMMCJITCompilerOptions
39 func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
40 options.C.OptLevel = C.uint(level)
43 func (options *MCJITCompilerOptions) SetMCJITNoFramePointerElim(nfp bool) {
44 options.C.NoFramePointerElim = boolToLLVMBool(nfp)
47 func (options *MCJITCompilerOptions) SetMCJITEnableFastISel(fastisel bool) {
48 options.C.EnableFastISel = boolToLLVMBool(fastisel)
51 func (options *MCJITCompilerOptions) SetMCJITCodeModel(CodeModel CodeModel) {
52 options.C.CodeModel = C.LLVMCodeModel(CodeModel)
56 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
57 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
60 //-------------------------------------------------------------------------
62 //-------------------------------------------------------------------------
64 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
65 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
68 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
69 g.C = C.LLVMCreateGenericValueOfPointer(p)
72 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
73 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
76 func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
77 func (g GenericValue) Int(signed bool) uint64 {
78 return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
80 func (g GenericValue) Float(t Type) float64 {
81 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
83 func (g GenericValue) Pointer() unsafe.Pointer {
84 return C.LLVMGenericValueToPointer(g.C)
86 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
88 //-------------------------------------------------------------------------
89 // llvm.ExecutionEngine
90 //-------------------------------------------------------------------------
92 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
94 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
97 err = errors.New(C.GoString(cmsg))
98 C.LLVMDisposeMessage(cmsg)
103 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
105 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
108 err = errors.New(C.GoString(cmsg))
109 C.LLVMDisposeMessage(cmsg)
114 func NewMCJITCompilerOptions() MCJITCompilerOptions {
115 var options C.struct_LLVMMCJITCompilerOptions
116 C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})))
117 return MCJITCompilerOptions{options}
120 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
122 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg)
125 err = errors.New(C.GoString(cmsg))
126 C.LLVMDisposeMessage(cmsg)
131 func (ee ExecutionEngine) Dispose() { C.LLVMDisposeExecutionEngine(ee.C) }
132 func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
133 func (ee ExecutionEngine) RunStaticDestructors() { C.LLVMRunStaticDestructors(ee.C) }
135 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
137 var argptr *GenericValue
141 g.C = C.LLVMRunFunction(ee.C, f.C,
142 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
146 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
147 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
149 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
151 func (ee ExecutionEngine) RemoveModule(m Module) {
152 var modtmp C.LLVMModuleRef
153 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
156 func (ee ExecutionEngine) FindFunction(name string) (f Value) {
157 cname := C.CString(name)
158 defer C.free(unsafe.Pointer(cname))
159 C.LLVMFindFunction(ee.C, cname, &f.C)
163 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
164 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
167 func (ee ExecutionEngine) TargetData() (td TargetData) {
168 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
172 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
173 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
176 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
177 return C.LLVMGetPointerToGlobal(ee.C, global.C)