1 //===- executionengine.go - Bindings for executionengine ------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines bindings for the executionengine component.
11 //===----------------------------------------------------------------------===//
16 #include "llvm-c/Core.h"
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
34 type MCJITCompilerOptions
struct {
35 C C
.struct_LLVMMCJITCompilerOptions
38 func (options
*MCJITCompilerOptions
) SetMCJITOptimizationLevel(level
uint) {
39 options
.C
.OptLevel
= C
.uint(level
)
42 func (options
*MCJITCompilerOptions
) SetMCJITNoFramePointerElim(nfp
bool) {
43 options
.C
.NoFramePointerElim
= boolToLLVMBool(nfp
)
46 func (options
*MCJITCompilerOptions
) SetMCJITEnableFastISel(fastisel
bool) {
47 options
.C
.EnableFastISel
= boolToLLVMBool(fastisel
)
50 func (options
*MCJITCompilerOptions
) SetMCJITCodeModel(CodeModel CodeModel
) {
51 options
.C
.CodeModel
= C
.LLVMCodeModel(CodeModel
)
55 func llvmGenericValueRefPtr(t
*GenericValue
) *C
.LLVMGenericValueRef
{
56 return (*C
.LLVMGenericValueRef
)(unsafe
.Pointer(t
))
59 //-------------------------------------------------------------------------
61 //-------------------------------------------------------------------------
63 func NewGenericValueFromInt(t Type
, n
uint64, signed
bool) (g GenericValue
) {
64 g
.C
= C
.LLVMCreateGenericValueOfInt(t
.C
, C
.ulonglong(n
), boolToLLVMBool(signed
))
67 func NewGenericValueFromPointer(p unsafe
.Pointer
) (g GenericValue
) {
68 g
.C
= C
.LLVMCreateGenericValueOfPointer(p
)
71 func NewGenericValueFromFloat(t Type
, n
float64) (g GenericValue
) {
72 g
.C
= C
.LLVMCreateGenericValueOfFloat(t
.C
, C
.double(n
))
75 func (g GenericValue
) IntWidth() int { return int(C
.LLVMGenericValueIntWidth(g
.C
)) }
76 func (g GenericValue
) Int(signed
bool) uint64 {
77 return uint64(C
.LLVMGenericValueToInt(g
.C
, boolToLLVMBool(signed
)))
79 func (g GenericValue
) Float(t Type
) float64 {
80 return float64(C
.LLVMGenericValueToFloat(t
.C
, g
.C
))
82 func (g GenericValue
) Pointer() unsafe
.Pointer
{
83 return C
.LLVMGenericValueToPointer(g
.C
)
85 func (g GenericValue
) Dispose() { C
.LLVMDisposeGenericValue(g
.C
) }
87 //-------------------------------------------------------------------------
88 // llvm.ExecutionEngine
89 //-------------------------------------------------------------------------
91 func NewExecutionEngine(m Module
) (ee ExecutionEngine
, err error
) {
93 fail
:= C
.LLVMCreateExecutionEngineForModule(&ee
.C
, m
.C
, &cmsg
)
96 err
= errors
.New(C
.GoString(cmsg
))
97 C
.LLVMDisposeMessage(cmsg
)
102 func NewInterpreter(m Module
) (ee ExecutionEngine
, err error
) {
104 fail
:= C
.LLVMCreateInterpreterForModule(&ee
.C
, m
.C
, &cmsg
)
107 err
= errors
.New(C
.GoString(cmsg
))
108 C
.LLVMDisposeMessage(cmsg
)
113 func NewMCJITCompilerOptions() MCJITCompilerOptions
{
114 var options C
.struct_LLVMMCJITCompilerOptions
115 C
.LLVMInitializeMCJITCompilerOptions(&options
, C
.size_t(unsafe
.Sizeof(C
.struct_LLVMMCJITCompilerOptions
{})))
116 return MCJITCompilerOptions
{options
}
119 func NewMCJITCompiler(m Module
, options MCJITCompilerOptions
) (ee ExecutionEngine
, err error
) {
121 fail
:= C
.LLVMCreateMCJITCompilerForModule(&ee
.C
, m
.C
, &options
.C
, C
.size_t(unsafe
.Sizeof(C
.struct_LLVMMCJITCompilerOptions
{})), &cmsg
)
124 err
= errors
.New(C
.GoString(cmsg
))
125 C
.LLVMDisposeMessage(cmsg
)
130 func (ee ExecutionEngine
) Dispose() { C
.LLVMDisposeExecutionEngine(ee
.C
) }
131 func (ee ExecutionEngine
) RunStaticConstructors() { C
.LLVMRunStaticConstructors(ee
.C
) }
132 func (ee ExecutionEngine
) RunStaticDestructors() { C
.LLVMRunStaticDestructors(ee
.C
) }
134 func (ee ExecutionEngine
) RunFunction(f Value
, args
[]GenericValue
) (g GenericValue
) {
136 var argptr
*GenericValue
140 g
.C
= C
.LLVMRunFunction(ee
.C
, f
.C
,
141 C
.unsigned(nargs
), llvmGenericValueRefPtr(argptr
))
145 func (ee ExecutionEngine
) FreeMachineCodeForFunction(f Value
) {
146 C
.LLVMFreeMachineCodeForFunction(ee
.C
, f
.C
)
148 func (ee ExecutionEngine
) AddModule(m Module
) { C
.LLVMAddModule(ee
.C
, m
.C
) }
150 func (ee ExecutionEngine
) RemoveModule(m Module
) {
151 var modtmp C
.LLVMModuleRef
152 C
.LLVMRemoveModule(ee
.C
, m
.C
, &modtmp
, nil)
155 func (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
)
162 func (ee ExecutionEngine
) RecompileAndRelinkFunction(f Value
) unsafe
.Pointer
{
163 return C
.LLVMRecompileAndRelinkFunction(ee
.C
, f
.C
)
166 func (ee ExecutionEngine
) TargetData() (td TargetData
) {
167 td
.C
= C
.LLVMGetExecutionEngineTargetData(ee
.C
)
171 func (ee ExecutionEngine
) AddGlobalMapping(global Value
, addr unsafe
.Pointer
) {
172 C
.LLVMAddGlobalMapping(ee
.C
, global
.C
, addr
)
175 func (ee ExecutionEngine
) PointerToGlobal(global Value
) unsafe
.Pointer
{
176 return C
.LLVMGetPointerToGlobal(ee
.C
, global
.C
)