83
|
1 //===- executionengine.go - Bindings for executionengine ------------------===//
|
|
2 //
|
|
3 // The LLVM Compiler Infrastructure
|
|
4 //
|
|
5 // This file is distributed under the University of Illinois Open Source
|
|
6 // License. See LICENSE.TXT for details.
|
|
7 //
|
|
8 //===----------------------------------------------------------------------===//
|
|
9 //
|
|
10 // This file defines bindings for the executionengine component.
|
|
11 //
|
|
12 //===----------------------------------------------------------------------===//
|
|
13
|
|
14 package llvm
|
|
15
|
|
16 /*
|
100
|
17 #include "llvm-c/Core.h"
|
83
|
18 #include "llvm-c/ExecutionEngine.h"
|
|
19 #include <stdlib.h>
|
|
20 */
|
|
21 import "C"
|
|
22 import "unsafe"
|
|
23 import "errors"
|
|
24
|
|
25 func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
|
|
26 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
|
|
27
|
|
28 type GenericValue struct {
|
|
29 C C.LLVMGenericValueRef
|
|
30 }
|
|
31 type ExecutionEngine struct {
|
|
32 C C.LLVMExecutionEngineRef
|
|
33 }
|
|
34
|
|
35 type MCJITCompilerOptions struct {
|
|
36 C C.struct_LLVMMCJITCompilerOptions
|
|
37 }
|
|
38
|
|
39 func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
|
|
40 options.C.OptLevel = C.uint(level)
|
|
41 }
|
|
42
|
|
43 func (options *MCJITCompilerOptions) SetMCJITNoFramePointerElim(nfp bool) {
|
|
44 options.C.NoFramePointerElim = boolToLLVMBool(nfp)
|
|
45 }
|
|
46
|
|
47 func (options *MCJITCompilerOptions) SetMCJITEnableFastISel(fastisel bool) {
|
|
48 options.C.EnableFastISel = boolToLLVMBool(fastisel)
|
|
49 }
|
|
50
|
|
51 func (options *MCJITCompilerOptions) SetMCJITCodeModel(CodeModel CodeModel) {
|
|
52 options.C.CodeModel = C.LLVMCodeModel(CodeModel)
|
|
53 }
|
|
54
|
|
55 // helpers
|
|
56 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
|
|
57 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
|
|
58 }
|
|
59
|
|
60 //-------------------------------------------------------------------------
|
|
61 // llvm.GenericValue
|
|
62 //-------------------------------------------------------------------------
|
|
63
|
|
64 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
|
|
65 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
|
|
66 return
|
|
67 }
|
|
68 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
|
|
69 g.C = C.LLVMCreateGenericValueOfPointer(p)
|
|
70 return
|
|
71 }
|
|
72 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
|
|
73 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
|
|
74 return
|
|
75 }
|
|
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)))
|
|
79 }
|
|
80 func (g GenericValue) Float(t Type) float64 {
|
|
81 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
|
|
82 }
|
|
83 func (g GenericValue) Pointer() unsafe.Pointer {
|
|
84 return C.LLVMGenericValueToPointer(g.C)
|
|
85 }
|
|
86 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
|
|
87
|
|
88 //-------------------------------------------------------------------------
|
|
89 // llvm.ExecutionEngine
|
|
90 //-------------------------------------------------------------------------
|
|
91
|
|
92 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
|
|
93 var cmsg *C.char
|
|
94 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
|
|
95 if fail != 0 {
|
|
96 ee.C = nil
|
|
97 err = errors.New(C.GoString(cmsg))
|
|
98 C.LLVMDisposeMessage(cmsg)
|
|
99 }
|
|
100 return
|
|
101 }
|
|
102
|
|
103 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
|
|
104 var cmsg *C.char
|
|
105 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
|
|
106 if fail != 0 {
|
|
107 ee.C = nil
|
|
108 err = errors.New(C.GoString(cmsg))
|
|
109 C.LLVMDisposeMessage(cmsg)
|
|
110 }
|
|
111 return
|
|
112 }
|
|
113
|
|
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}
|
|
118 }
|
|
119
|
|
120 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
|
|
121 var cmsg *C.char
|
|
122 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg)
|
|
123 if fail != 0 {
|
|
124 ee.C = nil
|
|
125 err = errors.New(C.GoString(cmsg))
|
|
126 C.LLVMDisposeMessage(cmsg)
|
|
127 }
|
|
128 return
|
|
129 }
|
|
130
|
|
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) }
|
|
134
|
|
135 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
|
|
136 nargs := len(args)
|
|
137 var argptr *GenericValue
|
|
138 if nargs > 0 {
|
|
139 argptr = &args[0]
|
|
140 }
|
|
141 g.C = C.LLVMRunFunction(ee.C, f.C,
|
|
142 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
|
|
143 return
|
|
144 }
|
|
145
|
|
146 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
|
|
147 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
|
|
148 }
|
|
149 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
|
|
150
|
|
151 func (ee ExecutionEngine) RemoveModule(m Module) {
|
|
152 var modtmp C.LLVMModuleRef
|
|
153 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
|
|
154 }
|
|
155
|
|
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)
|
|
160 return
|
|
161 }
|
|
162
|
|
163 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
|
|
164 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
|
|
165 }
|
|
166
|
|
167 func (ee ExecutionEngine) TargetData() (td TargetData) {
|
|
168 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
|
|
169 return
|
|
170 }
|
|
171
|
|
172 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
|
|
173 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
|
|
174 }
|
|
175
|
|
176 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
|
|
177 return C.LLVMGetPointerToGlobal(ee.C, global.C)
|
|
178 }
|