1 //===- executionengine.go - Bindings for executionengine ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines bindings for the executionengine component.
12 //===----------------------------------------------------------------------===//
17 #include "llvm-c/ExecutionEngine.h"
24 func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
25 func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
27 type GenericValue struct {
28 C C.LLVMGenericValueRef
30 type ExecutionEngine struct {
31 C C.LLVMExecutionEngineRef
33 type MCJITCompilerOptions struct {
36 NoFramePointerElim bool
41 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
42 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
45 //-------------------------------------------------------------------------
47 //-------------------------------------------------------------------------
49 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
50 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
53 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
54 g.C = C.LLVMCreateGenericValueOfPointer(p)
57 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
58 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
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)))
65 func (g GenericValue) Float(t Type) float64 {
66 return float64(C.LLVMGenericValueToFloat(t.C, g.C))
68 func (g GenericValue) Pointer() unsafe.Pointer {
69 return C.LLVMGenericValueToPointer(g.C)
71 func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
73 //-------------------------------------------------------------------------
74 // llvm.ExecutionEngine
75 //-------------------------------------------------------------------------
77 func NewExecutionEngine(m Module) (ee ExecutionEngine, err error) {
79 fail := C.LLVMCreateExecutionEngineForModule(&ee.C, m.C, &cmsg)
82 err = errors.New(C.GoString(cmsg))
83 C.LLVMDisposeMessage(cmsg)
88 func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
90 fail := C.LLVMCreateInterpreterForModule(&ee.C, m.C, &cmsg)
93 err = errors.New(C.GoString(cmsg))
94 C.LLVMDisposeMessage(cmsg)
99 func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
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),
107 fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg)
110 err = errors.New(C.GoString(cmsg))
111 C.LLVMDisposeMessage(cmsg)
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) }
120 func (ee ExecutionEngine) RunFunction(f Value, args []GenericValue) (g GenericValue) {
122 var argptr *GenericValue
126 g.C = C.LLVMRunFunction(ee.C, f.C,
127 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
131 func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
132 C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
134 func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
136 func (ee ExecutionEngine) RemoveModule(m Module) {
137 var modtmp C.LLVMModuleRef
138 C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
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)
148 func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
149 return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
152 func (ee ExecutionEngine) TargetData() (td TargetData) {
153 td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
157 func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
158 C.LLVMAddGlobalMapping(ee.C, global.C, addr)
161 func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
162 return C.LLVMGetPointerToGlobal(ee.C, global.C)