141883975f82c8cbcf3636bd90d96bc7c9bde79e
[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 func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
39           options.C.OptLevel = C.uint(level)
40 }
41
42 // helpers
43 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
44         return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
45 }
46
47 //-------------------------------------------------------------------------
48 // llvm.GenericValue
49 //-------------------------------------------------------------------------
50
51 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
52         g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
53         return
54 }
55 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
56         g.C = C.LLVMCreateGenericValueOfPointer(p)
57         return
58 }
59 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
60         g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
61         return
62 }
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)))
66 }
67 func (g GenericValue) Float(t Type) float64 {
68         return float64(C.LLVMGenericValueToFloat(t.C, g.C))
69 }
70 func (g GenericValue) Pointer() unsafe.Pointer {
71         return C.LLVMGenericValueToPointer(g.C)
72 }
73 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
74
75 //-------------------------------------------------------------------------
76 // llvm.ExecutionEngine
77 //-------------------------------------------------------------------------
78
79 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
80         var cmsg *C.char
81         fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
82         if fail != 0 {
83                 ee.C = nil
84                 err = errors.New(C.GoString(cmsg))
85                 C.LLVMDisposeMessage(cmsg)
86         }
87         return
88 }
89
90 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
91         var cmsg *C.char
92         fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
93         if fail != 0 {
94                 ee.C = nil
95                 err = errors.New(C.GoString(cmsg))
96                 C.LLVMDisposeMessage(cmsg)
97         }
98         return
99 }
100
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}
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 }