[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / bindings / go / llvm / executionengine.go
blob5fa82047c1798c1697b62a56ff7781d55dbd1bc3
1 //===- executionengine.go - Bindings for executionengine ------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines bindings for the executionengine component.
11 //===----------------------------------------------------------------------===//
13 package llvm
16 #include "llvm-c/Core.h"
17 #include "llvm-c/ExecutionEngine.h"
18 #include <stdlib.h>
20 import "C"
21 import "unsafe"
22 import "errors"
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)
54 // helpers
55 func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
56 return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
59 //-------------------------------------------------------------------------
60 // llvm.GenericValue
61 //-------------------------------------------------------------------------
63 func NewGenericValueFromInt(t Type, n uint64, signed bool) (g GenericValue) {
64 g.C = C.LLVMCreateGenericValueOfInt(t.C, C.ulonglong(n), boolToLLVMBool(signed))
65 return
67 func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
68 g.C = C.LLVMCreateGenericValueOfPointer(p)
69 return
71 func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
72 g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
73 return
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) {
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)
99 return
102 func 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)
110 return
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) {
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)
127 return
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) {
135 nargs := len(args)
136 var argptr *GenericValue
137 if nargs > 0 {
138 argptr = &args[0]
140 g.C = C.LLVMRunFunction(ee.C, f.C,
141 C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
142 return
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)
159 return
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)
168 return
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)