]> git.proxmox.com Git - rustc.git/blame - src/llvm/bindings/go/llvm/executionengine.go
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / bindings / go / llvm / executionengine.go
CommitLineData
85aaf69f
SL
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
14package llvm
15
16/*
17#include "llvm-c/ExecutionEngine.h"
18#include <stdlib.h>
19*/
20import "C"
21import "unsafe"
22import "errors"
23
24func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
25func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
26
27type GenericValue struct {
28 C C.LLVMGenericValueRef
29}
30type ExecutionEngine struct {
31 C C.LLVMExecutionEngineRef
32}
33
34type MCJITCompilerOptions struct {
35 C C.struct_LLVMMCJITCompilerOptions
36}
37
38func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
39 options.C.OptLevel = C.uint(level)
40}
41
42func (options *MCJITCompilerOptions) SetMCJITNoFramePointerElim(nfp bool) {
43 options.C.NoFramePointerElim = boolToLLVMBool(nfp)
44}
45
46func (options *MCJITCompilerOptions) SetMCJITEnableFastISel(fastisel bool) {
47 options.C.EnableFastISel = boolToLLVMBool(fastisel)
48}
49
50func (options *MCJITCompilerOptions) SetMCJITCodeModel(CodeModel CodeModel) {
51 options.C.CodeModel = C.LLVMCodeModel(CodeModel)
52}
53
54// helpers
55func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
56 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
57}
58
59//-------------------------------------------------------------------------
60// llvm.GenericValue
61//-------------------------------------------------------------------------
62
63func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
64 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
65 return
66}
67func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
68 g.C = C.LLVMCreateGenericValueOfPointer(p)
69 return
70}
71func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
72 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
73 return
74}
75func (g GenericValue) IntWidth() int { return int(C.LLVMGenericValueIntWidth(g.C)) }
76func (g GenericValue) Int(signed bool) uint64 {
77 return uint64(C.LLVMGenericValueToInt(g.C, boolToLLVMBool(signed)))
78}
79func (g GenericValue) Float(t Type) float64 {
80 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
81}
82func (g GenericValue) Pointer() unsafe.Pointer {
83 return C.LLVMGenericValueToPointer(g.C)
84}
85func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
86
87//-------------------------------------------------------------------------
88// llvm.ExecutionEngine
89//-------------------------------------------------------------------------
90
91func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
92 var cmsg *C.char
93 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
94 if fail != 0 {
95 ee.C = nil
96 err = errors.New(C.GoString(cmsg))
97 C.LLVMDisposeMessage(cmsg)
98 }
99 return
100}
101
102func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
103 var cmsg *C.char
104 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
105 if fail != 0 {
106 ee.C = nil
107 err = errors.New(C.GoString(cmsg))
108 C.LLVMDisposeMessage(cmsg)
109 }
110 return
111}
112
113func NewMCJITCompilerOptions() MCJITCompilerOptions {
114 var options C.struct_LLVMMCJITCompilerOptions
115 C.LLVMInitializeMCJITCompilerOptions(&options, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})))
116 return MCJITCompilerOptions{options}
117}
118
119func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
120 var cmsg *C.char
121 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &options.C, C.size_t(unsafe.Sizeof(C.struct_LLVMMCJITCompilerOptions{})), &cmsg)
122 if fail != 0 {
123 ee.C = nil
124 err = errors.New(C.GoString(cmsg))
125 C.LLVMDisposeMessage(cmsg)
126 }
127 return
128}
129
130func (ee ExecutionEngine) Dispose() { C.LLVMDisposeExecutionEngine(ee.C) }
131func (ee ExecutionEngine) RunStaticConstructors() { C.LLVMRunStaticConstructors(ee.C) }
132func (ee ExecutionEngine) RunStaticDestructors() { C.LLVMRunStaticDestructors(ee.C) }
133
134func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
135 nargs := len(args)
136 var argptr *GenericValue
137 if nargs > 0 {
138 argptr = &args[0]
139 }
140 g.C = C.LLVMRunFunction(ee.C, f.C,
141 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
142 return
143}
144
145func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
146 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
147}
148func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
149
150func (ee ExecutionEngine) RemoveModule(m Module) {
151 var modtmp C.LLVMModuleRef
152 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
153}
154
155func (ee ExecutionEngine) FindFunction(name string) (f Value) {
156 cname := C.CString(name)
157 defer C.free(unsafe.Pointer(cname))
158 C.LLVMFindFunction(ee.C, cname, &f.C)
159 return
160}
161
162func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
163 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
164}
165
166func (ee ExecutionEngine) TargetData() (td TargetData) {
167 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
168 return
169}
170
171func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
172 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
173}
174
175func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
176 return C.LLVMGetPointerToGlobal(ee.C, global.C)
177}