1*9880d681SAndroid Build Coastguard Worker //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines the C bindings for the ExecutionEngine library.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm-c/ExecutionEngine.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/ExecutionEngine.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/GenericValue.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CodeGenCWrappers.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
23*9880d681SAndroid Build Coastguard Worker #include <cstring>
24*9880d681SAndroid Build Coastguard Worker
25*9880d681SAndroid Build Coastguard Worker using namespace llvm;
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "jit"
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker // Wrapping the C bindings types.
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue,LLVMGenericValueRef)30*9880d681SAndroid Build Coastguard Worker DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker static LLVMTargetMachineRef wrap(const TargetMachine *P) {
34*9880d681SAndroid Build Coastguard Worker return
35*9880d681SAndroid Build Coastguard Worker reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
36*9880d681SAndroid Build Coastguard Worker }
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker /*===-- Operations on generic values --------------------------------------===*/
39*9880d681SAndroid Build Coastguard Worker
LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,unsigned long long N,LLVMBool IsSigned)40*9880d681SAndroid Build Coastguard Worker LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
41*9880d681SAndroid Build Coastguard Worker unsigned long long N,
42*9880d681SAndroid Build Coastguard Worker LLVMBool IsSigned) {
43*9880d681SAndroid Build Coastguard Worker GenericValue *GenVal = new GenericValue();
44*9880d681SAndroid Build Coastguard Worker GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
45*9880d681SAndroid Build Coastguard Worker return wrap(GenVal);
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker
LLVMCreateGenericValueOfPointer(void * P)48*9880d681SAndroid Build Coastguard Worker LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
49*9880d681SAndroid Build Coastguard Worker GenericValue *GenVal = new GenericValue();
50*9880d681SAndroid Build Coastguard Worker GenVal->PointerVal = P;
51*9880d681SAndroid Build Coastguard Worker return wrap(GenVal);
52*9880d681SAndroid Build Coastguard Worker }
53*9880d681SAndroid Build Coastguard Worker
LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef,double N)54*9880d681SAndroid Build Coastguard Worker LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
55*9880d681SAndroid Build Coastguard Worker GenericValue *GenVal = new GenericValue();
56*9880d681SAndroid Build Coastguard Worker switch (unwrap(TyRef)->getTypeID()) {
57*9880d681SAndroid Build Coastguard Worker case Type::FloatTyID:
58*9880d681SAndroid Build Coastguard Worker GenVal->FloatVal = N;
59*9880d681SAndroid Build Coastguard Worker break;
60*9880d681SAndroid Build Coastguard Worker case Type::DoubleTyID:
61*9880d681SAndroid Build Coastguard Worker GenVal->DoubleVal = N;
62*9880d681SAndroid Build Coastguard Worker break;
63*9880d681SAndroid Build Coastguard Worker default:
64*9880d681SAndroid Build Coastguard Worker llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
65*9880d681SAndroid Build Coastguard Worker }
66*9880d681SAndroid Build Coastguard Worker return wrap(GenVal);
67*9880d681SAndroid Build Coastguard Worker }
68*9880d681SAndroid Build Coastguard Worker
LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef)69*9880d681SAndroid Build Coastguard Worker unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
70*9880d681SAndroid Build Coastguard Worker return unwrap(GenValRef)->IntVal.getBitWidth();
71*9880d681SAndroid Build Coastguard Worker }
72*9880d681SAndroid Build Coastguard Worker
LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,LLVMBool IsSigned)73*9880d681SAndroid Build Coastguard Worker unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
74*9880d681SAndroid Build Coastguard Worker LLVMBool IsSigned) {
75*9880d681SAndroid Build Coastguard Worker GenericValue *GenVal = unwrap(GenValRef);
76*9880d681SAndroid Build Coastguard Worker if (IsSigned)
77*9880d681SAndroid Build Coastguard Worker return GenVal->IntVal.getSExtValue();
78*9880d681SAndroid Build Coastguard Worker else
79*9880d681SAndroid Build Coastguard Worker return GenVal->IntVal.getZExtValue();
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker
LLVMGenericValueToPointer(LLVMGenericValueRef GenVal)82*9880d681SAndroid Build Coastguard Worker void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
83*9880d681SAndroid Build Coastguard Worker return unwrap(GenVal)->PointerVal;
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker
LLVMGenericValueToFloat(LLVMTypeRef TyRef,LLVMGenericValueRef GenVal)86*9880d681SAndroid Build Coastguard Worker double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
87*9880d681SAndroid Build Coastguard Worker switch (unwrap(TyRef)->getTypeID()) {
88*9880d681SAndroid Build Coastguard Worker case Type::FloatTyID:
89*9880d681SAndroid Build Coastguard Worker return unwrap(GenVal)->FloatVal;
90*9880d681SAndroid Build Coastguard Worker case Type::DoubleTyID:
91*9880d681SAndroid Build Coastguard Worker return unwrap(GenVal)->DoubleVal;
92*9880d681SAndroid Build Coastguard Worker default:
93*9880d681SAndroid Build Coastguard Worker llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker }
96*9880d681SAndroid Build Coastguard Worker
LLVMDisposeGenericValue(LLVMGenericValueRef GenVal)97*9880d681SAndroid Build Coastguard Worker void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
98*9880d681SAndroid Build Coastguard Worker delete unwrap(GenVal);
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker /*===-- Operations on execution engines -----------------------------------===*/
102*9880d681SAndroid Build Coastguard Worker
LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef * OutEE,LLVMModuleRef M,char ** OutError)103*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
104*9880d681SAndroid Build Coastguard Worker LLVMModuleRef M,
105*9880d681SAndroid Build Coastguard Worker char **OutError) {
106*9880d681SAndroid Build Coastguard Worker std::string Error;
107*9880d681SAndroid Build Coastguard Worker EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
108*9880d681SAndroid Build Coastguard Worker builder.setEngineKind(EngineKind::Either)
109*9880d681SAndroid Build Coastguard Worker .setErrorStr(&Error);
110*9880d681SAndroid Build Coastguard Worker if (ExecutionEngine *EE = builder.create()){
111*9880d681SAndroid Build Coastguard Worker *OutEE = wrap(EE);
112*9880d681SAndroid Build Coastguard Worker return 0;
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker *OutError = strdup(Error.c_str());
115*9880d681SAndroid Build Coastguard Worker return 1;
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker
LLVMCreateInterpreterForModule(LLVMExecutionEngineRef * OutInterp,LLVMModuleRef M,char ** OutError)118*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
119*9880d681SAndroid Build Coastguard Worker LLVMModuleRef M,
120*9880d681SAndroid Build Coastguard Worker char **OutError) {
121*9880d681SAndroid Build Coastguard Worker std::string Error;
122*9880d681SAndroid Build Coastguard Worker EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
123*9880d681SAndroid Build Coastguard Worker builder.setEngineKind(EngineKind::Interpreter)
124*9880d681SAndroid Build Coastguard Worker .setErrorStr(&Error);
125*9880d681SAndroid Build Coastguard Worker if (ExecutionEngine *Interp = builder.create()) {
126*9880d681SAndroid Build Coastguard Worker *OutInterp = wrap(Interp);
127*9880d681SAndroid Build Coastguard Worker return 0;
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker *OutError = strdup(Error.c_str());
130*9880d681SAndroid Build Coastguard Worker return 1;
131*9880d681SAndroid Build Coastguard Worker }
132*9880d681SAndroid Build Coastguard Worker
LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef * OutJIT,LLVMModuleRef M,unsigned OptLevel,char ** OutError)133*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
134*9880d681SAndroid Build Coastguard Worker LLVMModuleRef M,
135*9880d681SAndroid Build Coastguard Worker unsigned OptLevel,
136*9880d681SAndroid Build Coastguard Worker char **OutError) {
137*9880d681SAndroid Build Coastguard Worker std::string Error;
138*9880d681SAndroid Build Coastguard Worker EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
139*9880d681SAndroid Build Coastguard Worker builder.setEngineKind(EngineKind::JIT)
140*9880d681SAndroid Build Coastguard Worker .setErrorStr(&Error)
141*9880d681SAndroid Build Coastguard Worker .setOptLevel((CodeGenOpt::Level)OptLevel);
142*9880d681SAndroid Build Coastguard Worker if (ExecutionEngine *JIT = builder.create()) {
143*9880d681SAndroid Build Coastguard Worker *OutJIT = wrap(JIT);
144*9880d681SAndroid Build Coastguard Worker return 0;
145*9880d681SAndroid Build Coastguard Worker }
146*9880d681SAndroid Build Coastguard Worker *OutError = strdup(Error.c_str());
147*9880d681SAndroid Build Coastguard Worker return 1;
148*9880d681SAndroid Build Coastguard Worker }
149*9880d681SAndroid Build Coastguard Worker
LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions * PassedOptions,size_t SizeOfPassedOptions)150*9880d681SAndroid Build Coastguard Worker void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
151*9880d681SAndroid Build Coastguard Worker size_t SizeOfPassedOptions) {
152*9880d681SAndroid Build Coastguard Worker LLVMMCJITCompilerOptions options;
153*9880d681SAndroid Build Coastguard Worker memset(&options, 0, sizeof(options)); // Most fields are zero by default.
154*9880d681SAndroid Build Coastguard Worker options.CodeModel = LLVMCodeModelJITDefault;
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker memcpy(PassedOptions, &options,
157*9880d681SAndroid Build Coastguard Worker std::min(sizeof(options), SizeOfPassedOptions));
158*9880d681SAndroid Build Coastguard Worker }
159*9880d681SAndroid Build Coastguard Worker
LLVMCreateMCJITCompilerForModule(LLVMExecutionEngineRef * OutJIT,LLVMModuleRef M,LLVMMCJITCompilerOptions * PassedOptions,size_t SizeOfPassedOptions,char ** OutError)160*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMCreateMCJITCompilerForModule(
161*9880d681SAndroid Build Coastguard Worker LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
162*9880d681SAndroid Build Coastguard Worker LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
163*9880d681SAndroid Build Coastguard Worker char **OutError) {
164*9880d681SAndroid Build Coastguard Worker LLVMMCJITCompilerOptions options;
165*9880d681SAndroid Build Coastguard Worker // If the user passed a larger sized options struct, then they were compiled
166*9880d681SAndroid Build Coastguard Worker // against a newer LLVM. Tell them that something is wrong.
167*9880d681SAndroid Build Coastguard Worker if (SizeOfPassedOptions > sizeof(options)) {
168*9880d681SAndroid Build Coastguard Worker *OutError = strdup(
169*9880d681SAndroid Build Coastguard Worker "Refusing to use options struct that is larger than my own; assuming "
170*9880d681SAndroid Build Coastguard Worker "LLVM library mismatch.");
171*9880d681SAndroid Build Coastguard Worker return 1;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker // Defend against the user having an old version of the API by ensuring that
175*9880d681SAndroid Build Coastguard Worker // any fields they didn't see are cleared. We must defend against fields being
176*9880d681SAndroid Build Coastguard Worker // set to the bitwise equivalent of zero, and assume that this means "do the
177*9880d681SAndroid Build Coastguard Worker // default" as if that option hadn't been available.
178*9880d681SAndroid Build Coastguard Worker LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
179*9880d681SAndroid Build Coastguard Worker memcpy(&options, PassedOptions, SizeOfPassedOptions);
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker TargetOptions targetOptions;
182*9880d681SAndroid Build Coastguard Worker targetOptions.EnableFastISel = options.EnableFastISel;
183*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> Mod(unwrap(M));
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker if (Mod)
186*9880d681SAndroid Build Coastguard Worker // Set function attribute "no-frame-pointer-elim" based on
187*9880d681SAndroid Build Coastguard Worker // NoFramePointerElim.
188*9880d681SAndroid Build Coastguard Worker for (auto &F : *Mod) {
189*9880d681SAndroid Build Coastguard Worker auto Attrs = F.getAttributes();
190*9880d681SAndroid Build Coastguard Worker auto Value = options.NoFramePointerElim ? "true" : "false";
191*9880d681SAndroid Build Coastguard Worker Attrs = Attrs.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
192*9880d681SAndroid Build Coastguard Worker "no-frame-pointer-elim", Value);
193*9880d681SAndroid Build Coastguard Worker F.setAttributes(Attrs);
194*9880d681SAndroid Build Coastguard Worker }
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker std::string Error;
197*9880d681SAndroid Build Coastguard Worker EngineBuilder builder(std::move(Mod));
198*9880d681SAndroid Build Coastguard Worker builder.setEngineKind(EngineKind::JIT)
199*9880d681SAndroid Build Coastguard Worker .setErrorStr(&Error)
200*9880d681SAndroid Build Coastguard Worker .setOptLevel((CodeGenOpt::Level)options.OptLevel)
201*9880d681SAndroid Build Coastguard Worker .setCodeModel(unwrap(options.CodeModel))
202*9880d681SAndroid Build Coastguard Worker .setTargetOptions(targetOptions);
203*9880d681SAndroid Build Coastguard Worker if (options.MCJMM)
204*9880d681SAndroid Build Coastguard Worker builder.setMCJITMemoryManager(
205*9880d681SAndroid Build Coastguard Worker std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
206*9880d681SAndroid Build Coastguard Worker if (ExecutionEngine *JIT = builder.create()) {
207*9880d681SAndroid Build Coastguard Worker *OutJIT = wrap(JIT);
208*9880d681SAndroid Build Coastguard Worker return 0;
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker *OutError = strdup(Error.c_str());
211*9880d681SAndroid Build Coastguard Worker return 1;
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker
LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE)214*9880d681SAndroid Build Coastguard Worker void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
215*9880d681SAndroid Build Coastguard Worker delete unwrap(EE);
216*9880d681SAndroid Build Coastguard Worker }
217*9880d681SAndroid Build Coastguard Worker
LLVMRunStaticConstructors(LLVMExecutionEngineRef EE)218*9880d681SAndroid Build Coastguard Worker void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
219*9880d681SAndroid Build Coastguard Worker unwrap(EE)->finalizeObject();
220*9880d681SAndroid Build Coastguard Worker unwrap(EE)->runStaticConstructorsDestructors(false);
221*9880d681SAndroid Build Coastguard Worker }
222*9880d681SAndroid Build Coastguard Worker
LLVMRunStaticDestructors(LLVMExecutionEngineRef EE)223*9880d681SAndroid Build Coastguard Worker void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
224*9880d681SAndroid Build Coastguard Worker unwrap(EE)->finalizeObject();
225*9880d681SAndroid Build Coastguard Worker unwrap(EE)->runStaticConstructorsDestructors(true);
226*9880d681SAndroid Build Coastguard Worker }
227*9880d681SAndroid Build Coastguard Worker
LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE,LLVMValueRef F,unsigned ArgC,const char * const * ArgV,const char * const * EnvP)228*9880d681SAndroid Build Coastguard Worker int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
229*9880d681SAndroid Build Coastguard Worker unsigned ArgC, const char * const *ArgV,
230*9880d681SAndroid Build Coastguard Worker const char * const *EnvP) {
231*9880d681SAndroid Build Coastguard Worker unwrap(EE)->finalizeObject();
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
234*9880d681SAndroid Build Coastguard Worker return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
235*9880d681SAndroid Build Coastguard Worker }
236*9880d681SAndroid Build Coastguard Worker
LLVMRunFunction(LLVMExecutionEngineRef EE,LLVMValueRef F,unsigned NumArgs,LLVMGenericValueRef * Args)237*9880d681SAndroid Build Coastguard Worker LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
238*9880d681SAndroid Build Coastguard Worker unsigned NumArgs,
239*9880d681SAndroid Build Coastguard Worker LLVMGenericValueRef *Args) {
240*9880d681SAndroid Build Coastguard Worker unwrap(EE)->finalizeObject();
241*9880d681SAndroid Build Coastguard Worker
242*9880d681SAndroid Build Coastguard Worker std::vector<GenericValue> ArgVec;
243*9880d681SAndroid Build Coastguard Worker ArgVec.reserve(NumArgs);
244*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I != NumArgs; ++I)
245*9880d681SAndroid Build Coastguard Worker ArgVec.push_back(*unwrap(Args[I]));
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker GenericValue *Result = new GenericValue();
248*9880d681SAndroid Build Coastguard Worker *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
249*9880d681SAndroid Build Coastguard Worker return wrap(Result);
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker
LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE,LLVMValueRef F)252*9880d681SAndroid Build Coastguard Worker void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
253*9880d681SAndroid Build Coastguard Worker }
254*9880d681SAndroid Build Coastguard Worker
LLVMAddModule(LLVMExecutionEngineRef EE,LLVMModuleRef M)255*9880d681SAndroid Build Coastguard Worker void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
256*9880d681SAndroid Build Coastguard Worker unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker
LLVMRemoveModule(LLVMExecutionEngineRef EE,LLVMModuleRef M,LLVMModuleRef * OutMod,char ** OutError)259*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
260*9880d681SAndroid Build Coastguard Worker LLVMModuleRef *OutMod, char **OutError) {
261*9880d681SAndroid Build Coastguard Worker Module *Mod = unwrap(M);
262*9880d681SAndroid Build Coastguard Worker unwrap(EE)->removeModule(Mod);
263*9880d681SAndroid Build Coastguard Worker *OutMod = wrap(Mod);
264*9880d681SAndroid Build Coastguard Worker return 0;
265*9880d681SAndroid Build Coastguard Worker }
266*9880d681SAndroid Build Coastguard Worker
LLVMFindFunction(LLVMExecutionEngineRef EE,const char * Name,LLVMValueRef * OutFn)267*9880d681SAndroid Build Coastguard Worker LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
268*9880d681SAndroid Build Coastguard Worker LLVMValueRef *OutFn) {
269*9880d681SAndroid Build Coastguard Worker if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
270*9880d681SAndroid Build Coastguard Worker *OutFn = wrap(F);
271*9880d681SAndroid Build Coastguard Worker return 0;
272*9880d681SAndroid Build Coastguard Worker }
273*9880d681SAndroid Build Coastguard Worker return 1;
274*9880d681SAndroid Build Coastguard Worker }
275*9880d681SAndroid Build Coastguard Worker
LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,LLVMValueRef Fn)276*9880d681SAndroid Build Coastguard Worker void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
277*9880d681SAndroid Build Coastguard Worker LLVMValueRef Fn) {
278*9880d681SAndroid Build Coastguard Worker return nullptr;
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker
LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE)281*9880d681SAndroid Build Coastguard Worker LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
282*9880d681SAndroid Build Coastguard Worker return wrap(&unwrap(EE)->getDataLayout());
283*9880d681SAndroid Build Coastguard Worker }
284*9880d681SAndroid Build Coastguard Worker
285*9880d681SAndroid Build Coastguard Worker LLVMTargetMachineRef
LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE)286*9880d681SAndroid Build Coastguard Worker LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
287*9880d681SAndroid Build Coastguard Worker return wrap(unwrap(EE)->getTargetMachine());
288*9880d681SAndroid Build Coastguard Worker }
289*9880d681SAndroid Build Coastguard Worker
LLVMAddGlobalMapping(LLVMExecutionEngineRef EE,LLVMValueRef Global,void * Addr)290*9880d681SAndroid Build Coastguard Worker void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
291*9880d681SAndroid Build Coastguard Worker void* Addr) {
292*9880d681SAndroid Build Coastguard Worker unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE,LLVMValueRef Global)295*9880d681SAndroid Build Coastguard Worker void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
296*9880d681SAndroid Build Coastguard Worker unwrap(EE)->finalizeObject();
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard Worker return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
299*9880d681SAndroid Build Coastguard Worker }
300*9880d681SAndroid Build Coastguard Worker
LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE,const char * Name)301*9880d681SAndroid Build Coastguard Worker uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) {
302*9880d681SAndroid Build Coastguard Worker return unwrap(EE)->getGlobalValueAddress(Name);
303*9880d681SAndroid Build Coastguard Worker }
304*9880d681SAndroid Build Coastguard Worker
LLVMGetFunctionAddress(LLVMExecutionEngineRef EE,const char * Name)305*9880d681SAndroid Build Coastguard Worker uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) {
306*9880d681SAndroid Build Coastguard Worker return unwrap(EE)->getFunctionAddress(Name);
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker
309*9880d681SAndroid Build Coastguard Worker /*===-- Operations on memory managers -------------------------------------===*/
310*9880d681SAndroid Build Coastguard Worker
311*9880d681SAndroid Build Coastguard Worker namespace {
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker struct SimpleBindingMMFunctions {
314*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
315*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
316*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
317*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerDestroyCallback Destroy;
318*9880d681SAndroid Build Coastguard Worker };
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker class SimpleBindingMemoryManager : public RTDyldMemoryManager {
321*9880d681SAndroid Build Coastguard Worker public:
322*9880d681SAndroid Build Coastguard Worker SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
323*9880d681SAndroid Build Coastguard Worker void *Opaque);
324*9880d681SAndroid Build Coastguard Worker ~SimpleBindingMemoryManager() override;
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
327*9880d681SAndroid Build Coastguard Worker unsigned SectionID,
328*9880d681SAndroid Build Coastguard Worker StringRef SectionName) override;
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
331*9880d681SAndroid Build Coastguard Worker unsigned SectionID, StringRef SectionName,
332*9880d681SAndroid Build Coastguard Worker bool isReadOnly) override;
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker bool finalizeMemory(std::string *ErrMsg) override;
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker private:
337*9880d681SAndroid Build Coastguard Worker SimpleBindingMMFunctions Functions;
338*9880d681SAndroid Build Coastguard Worker void *Opaque;
339*9880d681SAndroid Build Coastguard Worker };
340*9880d681SAndroid Build Coastguard Worker
SimpleBindingMemoryManager(const SimpleBindingMMFunctions & Functions,void * Opaque)341*9880d681SAndroid Build Coastguard Worker SimpleBindingMemoryManager::SimpleBindingMemoryManager(
342*9880d681SAndroid Build Coastguard Worker const SimpleBindingMMFunctions& Functions,
343*9880d681SAndroid Build Coastguard Worker void *Opaque)
344*9880d681SAndroid Build Coastguard Worker : Functions(Functions), Opaque(Opaque) {
345*9880d681SAndroid Build Coastguard Worker assert(Functions.AllocateCodeSection &&
346*9880d681SAndroid Build Coastguard Worker "No AllocateCodeSection function provided!");
347*9880d681SAndroid Build Coastguard Worker assert(Functions.AllocateDataSection &&
348*9880d681SAndroid Build Coastguard Worker "No AllocateDataSection function provided!");
349*9880d681SAndroid Build Coastguard Worker assert(Functions.FinalizeMemory &&
350*9880d681SAndroid Build Coastguard Worker "No FinalizeMemory function provided!");
351*9880d681SAndroid Build Coastguard Worker assert(Functions.Destroy &&
352*9880d681SAndroid Build Coastguard Worker "No Destroy function provided!");
353*9880d681SAndroid Build Coastguard Worker }
354*9880d681SAndroid Build Coastguard Worker
~SimpleBindingMemoryManager()355*9880d681SAndroid Build Coastguard Worker SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
356*9880d681SAndroid Build Coastguard Worker Functions.Destroy(Opaque);
357*9880d681SAndroid Build Coastguard Worker }
358*9880d681SAndroid Build Coastguard Worker
allocateCodeSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName)359*9880d681SAndroid Build Coastguard Worker uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
360*9880d681SAndroid Build Coastguard Worker uintptr_t Size, unsigned Alignment, unsigned SectionID,
361*9880d681SAndroid Build Coastguard Worker StringRef SectionName) {
362*9880d681SAndroid Build Coastguard Worker return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
363*9880d681SAndroid Build Coastguard Worker SectionName.str().c_str());
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker
allocateDataSection(uintptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool isReadOnly)366*9880d681SAndroid Build Coastguard Worker uint8_t *SimpleBindingMemoryManager::allocateDataSection(
367*9880d681SAndroid Build Coastguard Worker uintptr_t Size, unsigned Alignment, unsigned SectionID,
368*9880d681SAndroid Build Coastguard Worker StringRef SectionName, bool isReadOnly) {
369*9880d681SAndroid Build Coastguard Worker return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
370*9880d681SAndroid Build Coastguard Worker SectionName.str().c_str(),
371*9880d681SAndroid Build Coastguard Worker isReadOnly);
372*9880d681SAndroid Build Coastguard Worker }
373*9880d681SAndroid Build Coastguard Worker
finalizeMemory(std::string * ErrMsg)374*9880d681SAndroid Build Coastguard Worker bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
375*9880d681SAndroid Build Coastguard Worker char *errMsgCString = nullptr;
376*9880d681SAndroid Build Coastguard Worker bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
377*9880d681SAndroid Build Coastguard Worker assert((result || !errMsgCString) &&
378*9880d681SAndroid Build Coastguard Worker "Did not expect an error message if FinalizeMemory succeeded");
379*9880d681SAndroid Build Coastguard Worker if (errMsgCString) {
380*9880d681SAndroid Build Coastguard Worker if (ErrMsg)
381*9880d681SAndroid Build Coastguard Worker *ErrMsg = errMsgCString;
382*9880d681SAndroid Build Coastguard Worker free(errMsgCString);
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker return result;
385*9880d681SAndroid Build Coastguard Worker }
386*9880d681SAndroid Build Coastguard Worker
387*9880d681SAndroid Build Coastguard Worker } // anonymous namespace
388*9880d681SAndroid Build Coastguard Worker
LLVMCreateSimpleMCJITMemoryManager(void * Opaque,LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,LLVMMemoryManagerDestroyCallback Destroy)389*9880d681SAndroid Build Coastguard Worker LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
390*9880d681SAndroid Build Coastguard Worker void *Opaque,
391*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
392*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
393*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
394*9880d681SAndroid Build Coastguard Worker LLVMMemoryManagerDestroyCallback Destroy) {
395*9880d681SAndroid Build Coastguard Worker
396*9880d681SAndroid Build Coastguard Worker if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
397*9880d681SAndroid Build Coastguard Worker !Destroy)
398*9880d681SAndroid Build Coastguard Worker return nullptr;
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker SimpleBindingMMFunctions functions;
401*9880d681SAndroid Build Coastguard Worker functions.AllocateCodeSection = AllocateCodeSection;
402*9880d681SAndroid Build Coastguard Worker functions.AllocateDataSection = AllocateDataSection;
403*9880d681SAndroid Build Coastguard Worker functions.FinalizeMemory = FinalizeMemory;
404*9880d681SAndroid Build Coastguard Worker functions.Destroy = Destroy;
405*9880d681SAndroid Build Coastguard Worker return wrap(new SimpleBindingMemoryManager(functions, Opaque));
406*9880d681SAndroid Build Coastguard Worker }
407*9880d681SAndroid Build Coastguard Worker
LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM)408*9880d681SAndroid Build Coastguard Worker void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
409*9880d681SAndroid Build Coastguard Worker delete unwrap(MM);
410*9880d681SAndroid Build Coastguard Worker }
411*9880d681SAndroid Build Coastguard Worker
412