Go: add binding for LLVMSetUnnamedAddr.
[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 type MCJITCompilerOptions struct {
34         OptLevel           uint
35         CodeModel          CodeModel
36         NoFramePointerElim bool
37         EnableFastISel     bool
38 }
39
40 // helpers
41 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
42         return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
43 }
44
45 //-------------------------------------------------------------------------
46 // llvm.GenericValue
47 //-------------------------------------------------------------------------
48
49 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
50         g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
51         return
52 }
53 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
54         g.C = C.LLVMCreateGenericValueOfPointer(p)
55         return
56 }
57 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
58         g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
59         return
60 }
61 func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
62 func (g GenericValue) Int(signed bool) uint64 {
63         return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
64 }
65 func (g GenericValue) Float(t Type) float64 {
66         return float64(C.LLVMGenericValueToFloat(t.C, g.C))
67 }
68 func (g GenericValue) Pointer() unsafe.Pointer {
69         return C.LLVMGenericValueToPointer(g.C)
70 }
71 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
72
73 //-------------------------------------------------------------------------
74 // llvm.ExecutionEngine
75 //-------------------------------------------------------------------------
76
77 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
78         var cmsg *C.char
79         fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
80         if fail != 0 {
81                 ee.C = nil
82                 err = errors.New(C.GoString(cmsg))
83                 C.LLVMDisposeMessage(cmsg)
84         }
85         return
86 }
87
88 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
89         var cmsg *C.char
90         fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
91         if fail != 0 {
92                 ee.C = nil
93                 err = errors.New(C.GoString(cmsg))
94                 C.LLVMDisposeMessage(cmsg)
95         }
96         return
97 }
98
99 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
100         var cmsg *C.char
101         copts := C.struct_LLVMMCJITCompilerOptions{
102                 OptLevel:           C.unsigned(options.OptLevel),
103                 CodeModel:          C.LLVMCodeModel(options.CodeModel),
104                 NoFramePointerElim: boolToLLVMBool(options.NoFramePointerElim),
105                 EnableFastISel:     boolToLLVMBool(options.EnableFastISel),
106         }
107         fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg)
108         if fail != 0 {
109                 ee.C = nil
110                 err = errors.New(C.GoString(cmsg))
111                 C.LLVMDisposeMessage(cmsg)
112         }
113         return
114 }
115
116 func (ee ExecutionEngine) Dispose()               { C.LLVMDisposeExecutionEngine(ee.C) }
117 func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
118 func (ee ExecutionEngine) RunStaticDestructors()  { C.LLVMRunStaticDestructors(ee.C) }
119
120 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
121         nargs := len(args)
122         var argptr *GenericValue
123         if nargs > 0 {
124                 argptr = &args[0]
125         }
126         g.C = C.LLVMRunFunction(ee.C, f.C,
127                 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
128         return
129 }
130
131 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
132         C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
133 }
134 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
135
136 func (ee ExecutionEngine) RemoveModule(m Module) {
137         var modtmp C.LLVMModuleRef
138         C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
139 }
140
141 func (ee ExecutionEngine) FindFunction(name string) (f Value) {
142         cname := C.CString(name)
143         defer C.free(unsafe.Pointer(cname))
144         C.LLVMFindFunction(ee.C, cname, &f.C)
145         return
146 }
147
148 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
149         return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
150 }
151
152 func (ee ExecutionEngine) TargetData() (td TargetData) {
153         td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
154         return
155 }
156
157 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
158         C.LLVMAddGlobalMapping(ee.C, global.C, addr)
159 }
160
161 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
162         return C.LLVMGetPointerToGlobal(ee.C, global.C)
163 }