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
38 func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
39 options.C.OptLevel = C.uint(level)
43 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
44 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
47 //-------------------------------------------------------------------------
49 //-------------------------------------------------------------------------
51 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
52 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
55 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
56 g.C = C.LLVMCreateGenericValueOfPointer(p)
59 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
60 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
63 func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
64 func (g GenericValue) Int(signed bool) uint64 {
65 return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
67 func (g GenericValue) Float(t Type) float64 {
68 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
70 func (g GenericValue) Pointer() unsafe.Pointer {
71 return C.LLVMGenericValueToPointer(g.C)
73 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
75 //-------------------------------------------------------------------------
76 // llvm.ExecutionEngine
77 //-------------------------------------------------------------------------
79 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
81 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
84 err = errors.New(C.GoString(cmsg))
85 C.LLVMDisposeMessage(cmsg)
90 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
92 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
95 err = errors.New(C.GoString(cmsg))
96 C.LLVMDisposeMessage(cmsg)
101 func NewMCJITCompilerOptions() MCJITCompilerOptions {
102 var options C.struct_LLVMMCJITCompilerOptions
103 C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})))
104 return MCJITCompilerOptions{options}
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)