executionengine.go raw

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