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