forked from nsf/gollvm
-
Notifications
You must be signed in to change notification settings - Fork 21
/
executionengine.go
192 lines (166 loc) · 5.98 KB
/
executionengine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package llvm
/*
#include <llvm-c/ExecutionEngine.h>
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "errors"
func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
type (
GenericValue struct {
C C.LLVMGenericValueRef
}
ExecutionEngine struct {
C C.LLVMExecutionEngineRef
}
MCJITCompilerOptions struct {
OptLevel uint
CodeModel CodeModel
NoFramePointerElim bool
EnableFastISel bool
}
)
// helpers
func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
}
//-------------------------------------------------------------------------
// llvm.GenericValue
//-------------------------------------------------------------------------
func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
return
}
func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
g.C = C.LLVMCreateGenericValueOfPointer(p)
return
}
func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
return
}
func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
func (g GenericValue) Int(signed bool) uint64 {
return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
}
func (g GenericValue) Float(t Type) float64 {
return float64(C.LLVMGenericValueToFloat(t.C, g.C))
}
func (g GenericValue) Pointer() unsafe.Pointer {
return C.LLVMGenericValueToPointer(g.C)
}
func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
//-------------------------------------------------------------------------
// llvm.ExecutionEngine
//-------------------------------------------------------------------------
func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
var cmsg *C.char
fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
if fail != 0 {
ee.C = nil
err = errors.New(C.GoString(cmsg))
C.LLVMDisposeMessage(cmsg)
} else {
err = nil
}
return
}
func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
var cmsg *C.char
fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
if fail != 0 {
ee.C = nil
err = errors.New(C.GoString(cmsg))
C.LLVMDisposeMessage(cmsg)
} else {
err = nil
}
return
}
func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
var cmsg *C.char
copts := C.struct_LLVMMCJITCompilerOptions{
OptLevel: C.unsigned(options.OptLevel),
CodeModel: C.LLVMCodeModel(options.CodeModel),
NoFramePointerElim: boolToLLVMBool(options.NoFramePointerElim),
EnableFastISel: boolToLLVMBool(options.EnableFastISel),
}
fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg)
if fail != 0 {
ee.C = nil
err = errors.New(C.GoString(cmsg))
C.LLVMDisposeMessage(cmsg)
} else {
err = nil
}
return
}
// XXX: Don't port deprecated
// Deprecated: Use LLVMCreateExecutionEngineForModule instead.
//LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
// LLVMModuleProviderRef MP,
// char **OutError);
// Deprecated: Use LLVMCreateInterpreterForModule instead.
//LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
// LLVMModuleProviderRef MP,
// char **OutError);
// Deprecated: Use LLVMCreateJITCompilerForModule instead.
//LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
// LLVMModuleProviderRef MP,
// unsigned OptLevel,
// char **OutError);
func (ee ExecutionEngine) Dispose() { C.LLVMDisposeExecutionEngine(ee.C) }
func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
func (ee ExecutionEngine) RunStaticDestructors() { C.LLVMRunStaticDestructors(ee.C) }
// TODO(nsf): figure out how to convert that stuff from Go's "os.Argv"
//int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
// unsigned ArgC, const char * const *ArgV,
// const char * const *EnvP);
func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
nargs := len(args)
var argptr *GenericValue
if nargs > 0 {
argptr = &args[0]
}
g.C = C.LLVMRunFunction(ee.C, f.C,
C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
return
}
func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
}
func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
// XXX(nsf): Don't port deprecated
// Deprecated: Use LLVMAddModule instead.
//void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP);
func (ee ExecutionEngine) RemoveModule(m Module) {
var modtmp C.LLVMModuleRef
C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
}
// XXX(nsf): Don't port deprecated
// Deprecated: Use LLVMRemoveModule instead.
//LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
// LLVMModuleProviderRef MP,
// LLVMModuleRef *OutMod, char **OutError);
func (ee ExecutionEngine) FindFunction(name string) (f Value) {
cname := C.CString(name)
C.LLVMFindFunction(ee.C, cname, &f.C)
C.free(unsafe.Pointer(cname))
return
}
func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
}
func (ee ExecutionEngine) TargetData() (td TargetData) {
td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
return
}
func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
C.LLVMAddGlobalMapping(ee.C, global.C, addr)
}
func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
return C.LLVMGetPointerToGlobal(ee.C, global.C)
}
// vim: set ft=go: