db2c2153401811c9fc81e14dfe8d6a0986a3552f
[oota-llvm.git] / bindings / go / llvm / executionengine.go
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 /*
17 #include "llvm-c/ExecutionEngine.h"
18 #include <stdlib.h>
19 */
20 import "C"
21 import "unsafe"
22 import "errors"
23
24 func LinkInMCJIT()       { C.LLVMLinkInMCJIT() }
25 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
26
27 type GenericValue struct {
28         C C.LLVMGenericValueRef
29 }
30 type ExecutionEngine struct {
31         C C.LLVMExecutionEngineRef
32 }
33
34 type MCJITCompilerOptions struct {
35         C C.struct_LLVMMCJITCompilerOptions
36 }
37
38 // helpers
39 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
40         return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
41 }
42
43 //-------------------------------------------------------------------------
44 // llvm.GenericValue
45 //-------------------------------------------------------------------------
46
47 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
48         g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
49         return
50 }
51 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
52         g.C = C.LLVMCreateGenericValueOfPointer(p)
53         return
54 }
55 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
56         g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
57         return
58 }
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)))
62 }
63 func (g GenericValue) Float(t Type) float64 {
64         return float64(C.LLVMGenericValueToFloat(t.C, g.C))
65 }
66 func (g GenericValue) Pointer() unsafe.Pointer {
67         return C.LLVMGenericValueToPointer(g.C)
68 }
69 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
70
71 //-------------------------------------------------------------------------
72 // llvm.ExecutionEngine
73 //-------------------------------------------------------------------------
74
75 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
76         var cmsg *C.char
77         fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
78         if fail != 0 {
79                 ee.C = nil
80                 err = errors.New(C.GoString(cmsg))
81                 C.LLVMDisposeMessage(cmsg)
82         }
83         return
84 }
85
86 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
87         var cmsg *C.char
88         fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
89         if fail != 0 {
90                 ee.C = nil
91                 err = errors.New(C.GoString(cmsg))
92                 C.LLVMDisposeMessage(cmsg)
93         }
94         return
95 }
96
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}
101 }
102
103 func SetMCJITOptimizationLevel(options MCJITCompilerOptions, level uint) {
104         options.C.OptLevel = C.uint(level)
105 }
106
107 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
108         var cmsg *C.char
109         fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg)
110         if fail != 0 {
111                 ee.C = nil
112                 err = errors.New(C.GoString(cmsg))
113                 C.LLVMDisposeMessage(cmsg)
114         }
115         return
116 }
117
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) }
121
122 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
123         nargs := len(args)
124         var argptr *GenericValue
125         if nargs > 0 {
126                 argptr = &args[0]
127         }
128         g.C = C.LLVMRunFunction(ee.C, f.C,
129                 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
130         return
131 }
132
133 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
134         C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
135 }
136 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
137
138 func (ee ExecutionEngine) RemoveModule(m Module) {
139         var modtmp C.LLVMModuleRef
140         C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
141 }
142
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)
147         return
148 }
149
150 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
151         return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
152 }
153
154 func (ee ExecutionEngine) TargetData() (td TargetData) {
155         td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
156         return
157 }
158
159 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
160         C.LLVMAddGlobalMapping(ee.C, global.C, addr)
161 }
162
163 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
164         return C.LLVMGetPointerToGlobal(ee.C, global.C)
165 }