executionengine.go raw

   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.
  10  //
  11  //===----------------------------------------------------------------------===//
  12  
  13  package llvm
  14  
  15  /*
  16  #include "llvm-c/Core.h"
  17  #include "llvm-c/ExecutionEngine.h"
  18  #include <stdlib.h>
  19  */
  20  import "C"
  21  import "unsafe"
  22  import "errors"
  23  
  24  func LinkInMCJIT()       { C.LLVMLinkInMCJIT() }
  25  func LinkInInterpreter() { C.LLVMLinkInInterpreter() }
  26  
  27  type GenericValue struct {
  28  	C C.LLVMGenericValueRef
  29  }
  30  type ExecutionEngine struct {
  31  	C C.LLVMExecutionEngineRef
  32  }
  33  
  34  type MCJITCompilerOptions struct {
  35  	C C.struct_LLVMMCJITCompilerOptions
  36  }
  37  
  38  func (options *MCJITCompilerOptions) SetMCJITOptimizationLevel(level uint) {
  39  	options.C.OptLevel = C.uint(level)
  40  }
  41  
  42  func (options *MCJITCompilerOptions) SetMCJITNoFramePointerElim(nfp bool) {
  43  	options.C.NoFramePointerElim = boolToLLVMBool(nfp)
  44  }
  45  
  46  func (options *MCJITCompilerOptions) SetMCJITEnableFastISel(fastisel bool) {
  47  	options.C.EnableFastISel = boolToLLVMBool(fastisel)
  48  }
  49  
  50  func (options *MCJITCompilerOptions) SetMCJITCodeModel(CodeModel CodeModel) {
  51  	options.C.CodeModel = C.LLVMCodeModel(CodeModel)
  52  }
  53  
  54  // helpers
  55  func llvmGenericValueRefPtr(t *GenericValue) *C.LLVMGenericValueRef {
  56  	return (*C.LLVMGenericValueRef)(unsafe.Pointer(t))
  57  }
  58  
  59  //-------------------------------------------------------------------------
  60  // llvm.GenericValue
  61  //-------------------------------------------------------------------------
  62  
  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
  66  }
  67  func NewGenericValueFromPointer(p unsafe.Pointer) (g GenericValue) {
  68  	g.C = C.LLVMCreateGenericValueOfPointer(p)
  69  	return
  70  }
  71  func NewGenericValueFromFloat(t Type, n float64) (g GenericValue) {
  72  	g.C = C.LLVMCreateGenericValueOfFloat(t.C, C.double(n))
  73  	return
  74  }
  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)))
  78  }
  79  func (g GenericValue) Float(t Type) float64 {
  80  	return float64(C.LLVMGenericValueToFloat(t.C, g.C))
  81  }
  82  func (g GenericValue) Pointer() unsafe.Pointer {
  83  	return C.LLVMGenericValueToPointer(g.C)
  84  }
  85  func (g GenericValue) Dispose() { C.LLVMDisposeGenericValue(g.C) }
  86  
  87  //-------------------------------------------------------------------------
  88  // llvm.ExecutionEngine
  89  //-------------------------------------------------------------------------
  90  
  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)
  98  	}
  99  	return
 100  }
 101  
 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)
 109  	}
 110  	return
 111  }
 112  
 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}
 117  }
 118  
 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)
 126  	}
 127  	return
 128  }
 129  
 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) }
 133  
 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]
 139  	}
 140  	g.C = C.LLVMRunFunction(ee.C, f.C,
 141  		C.unsigned(nargs), llvmGenericValueRefPtr(argptr))
 142  	return
 143  }
 144  
 145  func (ee ExecutionEngine) FreeMachineCodeForFunction(f Value) {
 146  	C.LLVMFreeMachineCodeForFunction(ee.C, f.C)
 147  }
 148  func (ee ExecutionEngine) AddModule(m Module) { C.LLVMAddModule(ee.C, m.C) }
 149  
 150  func (ee ExecutionEngine) RemoveModule(m Module) {
 151  	var modtmp C.LLVMModuleRef
 152  	C.LLVMRemoveModule(ee.C, m.C, &modtmp, nil)
 153  }
 154  
 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
 160  }
 161  
 162  func (ee ExecutionEngine) RecompileAndRelinkFunction(f Value) unsafe.Pointer {
 163  	return C.LLVMRecompileAndRelinkFunction(ee.C, f.C)
 164  }
 165  
 166  func (ee ExecutionEngine) TargetData() (td TargetData) {
 167  	td.C = C.LLVMGetExecutionEngineTargetData(ee.C)
 168  	return
 169  }
 170  
 171  func (ee ExecutionEngine) AddGlobalMapping(global Value, addr unsafe.Pointer) {
 172  	C.LLVMAddGlobalMapping(ee.C, global.C, addr)
 173  }
 174  
 175  func (ee ExecutionEngine) PointerToGlobal(global Value) unsafe.Pointer {
 176  	return C.LLVMGetPointerToGlobal(ee.C, global.C)
 177  }
 178