1 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C bindings for the ExecutionEngine library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/ExecutionEngine.h"
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/GenericValue.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/ErrorHandling.h"
25 #define DEBUG_TYPE "jit"
27 // Wrapping the C bindings types.
28 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
31 inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
33 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
36 /*===-- Operations on generic values --------------------------------------===*/
38 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
41 GenericValue *GenVal = new GenericValue();
42 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
46 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
47 GenericValue *GenVal = new GenericValue();
48 GenVal->PointerVal = P;
52 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
53 GenericValue *GenVal = new GenericValue();
54 switch (unwrap(TyRef)->getTypeID()) {
58 case Type::DoubleTyID:
59 GenVal->DoubleVal = N;
62 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
67 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
68 return unwrap(GenValRef)->IntVal.getBitWidth();
71 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
73 GenericValue *GenVal = unwrap(GenValRef);
75 return GenVal->IntVal.getSExtValue();
77 return GenVal->IntVal.getZExtValue();
80 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
81 return unwrap(GenVal)->PointerVal;
84 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
85 switch (unwrap(TyRef)->getTypeID()) {
87 return unwrap(GenVal)->FloatVal;
88 case Type::DoubleTyID:
89 return unwrap(GenVal)->DoubleVal;
91 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
95 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
96 delete unwrap(GenVal);
99 /*===-- Operations on execution engines -----------------------------------===*/
101 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
105 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
106 builder.setEngineKind(EngineKind::Either)
107 .setErrorStr(&Error);
108 if (ExecutionEngine *EE = builder.create()){
112 *OutError = strdup(Error.c_str());
116 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
120 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
121 builder.setEngineKind(EngineKind::Interpreter)
122 .setErrorStr(&Error);
123 if (ExecutionEngine *Interp = builder.create()) {
124 *OutInterp = wrap(Interp);
127 *OutError = strdup(Error.c_str());
131 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
136 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
137 builder.setEngineKind(EngineKind::JIT)
139 .setOptLevel((CodeGenOpt::Level)OptLevel);
140 if (ExecutionEngine *JIT = builder.create()) {
144 *OutError = strdup(Error.c_str());
148 void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
149 size_t SizeOfPassedOptions) {
150 LLVMMCJITCompilerOptions options;
151 memset(&options, 0, sizeof(options)); // Most fields are zero by default.
152 options.CodeModel = LLVMCodeModelJITDefault;
154 memcpy(PassedOptions, &options,
155 std::min(sizeof(options), SizeOfPassedOptions));
158 LLVMBool LLVMCreateMCJITCompilerForModule(
159 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
160 LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
162 LLVMMCJITCompilerOptions options;
163 // If the user passed a larger sized options struct, then they were compiled
164 // against a newer LLVM. Tell them that something is wrong.
165 if (SizeOfPassedOptions > sizeof(options)) {
167 "Refusing to use options struct that is larger than my own; assuming "
168 "LLVM library mismatch.");
172 // Defend against the user having an old version of the API by ensuring that
173 // any fields they didn't see are cleared. We must defend against fields being
174 // set to the bitwise equivalent of zero, and assume that this means "do the
175 // default" as if that option hadn't been available.
176 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
177 memcpy(&options, PassedOptions, SizeOfPassedOptions);
179 TargetOptions targetOptions;
180 targetOptions.NoFramePointerElim = options.NoFramePointerElim;
181 targetOptions.EnableFastISel = options.EnableFastISel;
184 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
185 builder.setEngineKind(EngineKind::JIT)
188 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
189 .setCodeModel(unwrap(options.CodeModel))
190 .setTargetOptions(targetOptions);
192 builder.setMCJITMemoryManager(unwrap(options.MCJMM));
193 if (ExecutionEngine *JIT = builder.create()) {
197 *OutError = strdup(Error.c_str());
201 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
202 LLVMModuleProviderRef MP,
204 /* The module provider is now actually a module. */
205 return LLVMCreateExecutionEngineForModule(OutEE,
206 reinterpret_cast<LLVMModuleRef>(MP),
210 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
211 LLVMModuleProviderRef MP,
213 /* The module provider is now actually a module. */
214 return LLVMCreateInterpreterForModule(OutInterp,
215 reinterpret_cast<LLVMModuleRef>(MP),
219 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
220 LLVMModuleProviderRef MP,
223 /* The module provider is now actually a module. */
224 return LLVMCreateJITCompilerForModule(OutJIT,
225 reinterpret_cast<LLVMModuleRef>(MP),
230 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
234 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
235 unwrap(EE)->runStaticConstructorsDestructors(false);
238 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
239 unwrap(EE)->runStaticConstructorsDestructors(true);
242 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
243 unsigned ArgC, const char * const *ArgV,
244 const char * const *EnvP) {
245 unwrap(EE)->finalizeObject();
247 std::vector<std::string> ArgVec;
248 for (unsigned I = 0; I != ArgC; ++I)
249 ArgVec.push_back(ArgV[I]);
251 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
254 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
256 LLVMGenericValueRef *Args) {
257 unwrap(EE)->finalizeObject();
259 std::vector<GenericValue> ArgVec;
260 ArgVec.reserve(NumArgs);
261 for (unsigned I = 0; I != NumArgs; ++I)
262 ArgVec.push_back(*unwrap(Args[I]));
264 GenericValue *Result = new GenericValue();
265 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
269 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
270 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
273 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
274 unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
277 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
278 /* The module provider is now actually a module. */
279 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
282 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
283 LLVMModuleRef *OutMod, char **OutError) {
284 Module *Mod = unwrap(M);
285 unwrap(EE)->removeModule(Mod);
290 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
291 LLVMModuleProviderRef MP,
292 LLVMModuleRef *OutMod, char **OutError) {
293 /* The module provider is now actually a module. */
294 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
298 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
299 LLVMValueRef *OutFn) {
300 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
307 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
309 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
312 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
313 return wrap(unwrap(EE)->getDataLayout());
317 LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
318 return wrap(unwrap(EE)->getTargetMachine());
321 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
323 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
326 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
327 unwrap(EE)->finalizeObject();
329 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
332 /*===-- Operations on memory managers -------------------------------------===*/
336 struct SimpleBindingMMFunctions {
337 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
338 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
339 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
340 LLVMMemoryManagerDestroyCallback Destroy;
343 class SimpleBindingMemoryManager : public RTDyldMemoryManager {
345 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
347 virtual ~SimpleBindingMemoryManager();
349 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
351 StringRef SectionName) override;
353 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
354 unsigned SectionID, StringRef SectionName,
355 bool isReadOnly) override;
357 bool finalizeMemory(std::string *ErrMsg) override;
360 SimpleBindingMMFunctions Functions;
364 SimpleBindingMemoryManager::SimpleBindingMemoryManager(
365 const SimpleBindingMMFunctions& Functions,
367 : Functions(Functions), Opaque(Opaque) {
368 assert(Functions.AllocateCodeSection &&
369 "No AllocateCodeSection function provided!");
370 assert(Functions.AllocateDataSection &&
371 "No AllocateDataSection function provided!");
372 assert(Functions.FinalizeMemory &&
373 "No FinalizeMemory function provided!");
374 assert(Functions.Destroy &&
375 "No Destroy function provided!");
378 SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
379 Functions.Destroy(Opaque);
382 uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
383 uintptr_t Size, unsigned Alignment, unsigned SectionID,
384 StringRef SectionName) {
385 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
386 SectionName.str().c_str());
389 uint8_t *SimpleBindingMemoryManager::allocateDataSection(
390 uintptr_t Size, unsigned Alignment, unsigned SectionID,
391 StringRef SectionName, bool isReadOnly) {
392 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
393 SectionName.str().c_str(),
397 bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
398 char *errMsgCString = nullptr;
399 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
400 assert((result || !errMsgCString) &&
401 "Did not expect an error message if FinalizeMemory succeeded");
404 *ErrMsg = errMsgCString;
410 } // anonymous namespace
412 LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
414 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
415 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
416 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
417 LLVMMemoryManagerDestroyCallback Destroy) {
419 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
423 SimpleBindingMMFunctions functions;
424 functions.AllocateCodeSection = AllocateCodeSection;
425 functions.AllocateDataSection = AllocateDataSection;
426 functions.FinalizeMemory = FinalizeMemory;
427 functions.Destroy = Destroy;
428 return wrap(new SimpleBindingMemoryManager(functions, Opaque));
431 void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {