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/ExecutionEngine.h"
24 func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
25 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
27 type GenericValue struct {
28 C C.LLVMGenericValueRef
30 type ExecutionEngine struct {
31 C C.LLVMExecutionEngineRef
34 type MCJITCompilerOptions struct {
35 C C.struct_LLVMMCJITCompilerOptions
39 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
40 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
43 //-------------------------------------------------------------------------
45 //-------------------------------------------------------------------------
47 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
48 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
51 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
52 g.C = C.LLVMCreateGenericValueOfPointer(p)
55 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
56 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
59 func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
60 func (g GenericValue) Int(signed bool) uint64 {
61 return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
63 func (g GenericValue) Float(t Type) float64 {
64 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
66 func (g GenericValue) Pointer() unsafe.Pointer {
67 return C.LLVMGenericValueToPointer(g.C)
69 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
71 //-------------------------------------------------------------------------
72 // llvm.ExecutionEngine
73 //-------------------------------------------------------------------------
75 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
77 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
80 err = errors.New(C.GoString(cmsg))
81 C.LLVMDisposeMessage(cmsg)
86 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
88 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
91 err = errors.New(C.GoString(cmsg))
92 C.LLVMDisposeMessage(cmsg)
97 func NewMCJITCompilerOptions() MCJITCompilerOptions {
98 var options C.struct_LLVMMCJITCompilerOptions
99 C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})))
100 return MCJITCompilerOptions{options}
103 func SetMCJITOptimizationLevel(options MCJITCompilerOptions, level uint) {
104 options.C.OptLevel = C.uint(level)
107 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
109 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg)
112 err = errors.New(C.GoString(cmsg))
113 C.LLVMDisposeMessage(cmsg)
118 func (ee ExecutionEngine) Dispose() { C.LLVMDisposeExecutionEngine(ee.C) }
119 func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
120 func (ee ExecutionEngine) RunStaticDestructors() { C.LLVMRunStaticDestructors(ee.C) }
122 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
124 var argptr *GenericValue
128 g.C = C.LLVMRunFunction(ee.C, f.C,
129 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
133 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
134 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
136 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
138 func (ee ExecutionEngine) RemoveModule(m Module) {
139 var modtmp C.LLVMModuleRef
140 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
143 func (ee ExecutionEngine) FindFunction(name string) (f Value) {
144 cname := C.CString(name)
145 defer C.free(unsafe.Pointer(cname))
146 C.LLVMFindFunction(ee.C, cname, &f.C)
150 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
151 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
154 func (ee ExecutionEngine) TargetData() (td TargetData) {
155 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
159 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
160 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
163 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
164 return C.LLVMGetPointerToGlobal(ee.C, global.C)