109467b48Spatrick /*===-- llvm-c/ExecutionEngine.h - ExecutionEngine Lib C Iface --*- C++ -*-===*\
209467b48Spatrick |*                                                                            *|
309467b48Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
409467b48Spatrick |* Exceptions.                                                                *|
509467b48Spatrick |* See https://llvm.org/LICENSE.txt for license information.                  *|
609467b48Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
709467b48Spatrick |*                                                                            *|
809467b48Spatrick |*===----------------------------------------------------------------------===*|
909467b48Spatrick |*                                                                            *|
1009467b48Spatrick |* This header declares the C interface to libLLVMExecutionEngine.o, which    *|
1109467b48Spatrick |* implements various analyses of the LLVM IR.                                *|
1209467b48Spatrick |*                                                                            *|
1309467b48Spatrick |* Many exotic languages can interoperate with C code but have a harder time  *|
1409467b48Spatrick |* with C++ due to name mangling. So in addition to C, this interface enables *|
1509467b48Spatrick |* tools written in such languages.                                           *|
1609467b48Spatrick |*                                                                            *|
1709467b48Spatrick \*===----------------------------------------------------------------------===*/
1809467b48Spatrick 
1909467b48Spatrick #ifndef LLVM_C_EXECUTIONENGINE_H
2009467b48Spatrick #define LLVM_C_EXECUTIONENGINE_H
2109467b48Spatrick 
2209467b48Spatrick #include "llvm-c/ExternC.h"
2309467b48Spatrick #include "llvm-c/Target.h"
2409467b48Spatrick #include "llvm-c/TargetMachine.h"
2509467b48Spatrick #include "llvm-c/Types.h"
2609467b48Spatrick 
2709467b48Spatrick LLVM_C_EXTERN_C_BEGIN
2809467b48Spatrick 
2909467b48Spatrick /**
3009467b48Spatrick  * @defgroup LLVMCExecutionEngine Execution Engine
3109467b48Spatrick  * @ingroup LLVMC
3209467b48Spatrick  *
3309467b48Spatrick  * @{
3409467b48Spatrick  */
3509467b48Spatrick 
3609467b48Spatrick void LLVMLinkInMCJIT(void);
3709467b48Spatrick void LLVMLinkInInterpreter(void);
3809467b48Spatrick 
3909467b48Spatrick typedef struct LLVMOpaqueGenericValue *LLVMGenericValueRef;
4009467b48Spatrick typedef struct LLVMOpaqueExecutionEngine *LLVMExecutionEngineRef;
4109467b48Spatrick typedef struct LLVMOpaqueMCJITMemoryManager *LLVMMCJITMemoryManagerRef;
4209467b48Spatrick 
4309467b48Spatrick struct LLVMMCJITCompilerOptions {
4409467b48Spatrick   unsigned OptLevel;
4509467b48Spatrick   LLVMCodeModel CodeModel;
4609467b48Spatrick   LLVMBool NoFramePointerElim;
4709467b48Spatrick   LLVMBool EnableFastISel;
4809467b48Spatrick   LLVMMCJITMemoryManagerRef MCJMM;
4909467b48Spatrick };
5009467b48Spatrick 
5109467b48Spatrick /*===-- Operations on generic values --------------------------------------===*/
5209467b48Spatrick 
5309467b48Spatrick LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
5409467b48Spatrick                                                 unsigned long long N,
5509467b48Spatrick                                                 LLVMBool IsSigned);
5609467b48Spatrick 
5709467b48Spatrick LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P);
5809467b48Spatrick 
5909467b48Spatrick LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef Ty, double N);
6009467b48Spatrick 
6109467b48Spatrick unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef);
6209467b48Spatrick 
6309467b48Spatrick unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenVal,
6409467b48Spatrick                                          LLVMBool IsSigned);
6509467b48Spatrick 
6609467b48Spatrick void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal);
6709467b48Spatrick 
6809467b48Spatrick double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal);
6909467b48Spatrick 
7009467b48Spatrick void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal);
7109467b48Spatrick 
7209467b48Spatrick /*===-- Operations on execution engines -----------------------------------===*/
7309467b48Spatrick 
7409467b48Spatrick LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
7509467b48Spatrick                                             LLVMModuleRef M,
7609467b48Spatrick                                             char **OutError);
7709467b48Spatrick 
7809467b48Spatrick LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
7909467b48Spatrick                                         LLVMModuleRef M,
8009467b48Spatrick                                         char **OutError);
8109467b48Spatrick 
8209467b48Spatrick LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
8309467b48Spatrick                                         LLVMModuleRef M,
8409467b48Spatrick                                         unsigned OptLevel,
8509467b48Spatrick                                         char **OutError);
8609467b48Spatrick 
8709467b48Spatrick void LLVMInitializeMCJITCompilerOptions(
8809467b48Spatrick   struct LLVMMCJITCompilerOptions *Options, size_t SizeOfOptions);
8909467b48Spatrick 
9009467b48Spatrick /**
9109467b48Spatrick  * Create an MCJIT execution engine for a module, with the given options. It is
9209467b48Spatrick  * the responsibility of the caller to ensure that all fields in Options up to
9309467b48Spatrick  * the given SizeOfOptions are initialized. It is correct to pass a smaller
9409467b48Spatrick  * value of SizeOfOptions that omits some fields. The canonical way of using
9509467b48Spatrick  * this is:
9609467b48Spatrick  *
9709467b48Spatrick  * LLVMMCJITCompilerOptions options;
9809467b48Spatrick  * LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
9909467b48Spatrick  * ... fill in those options you care about
10009467b48Spatrick  * LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
10109467b48Spatrick  *                                  &error);
10209467b48Spatrick  *
10309467b48Spatrick  * Note that this is also correct, though possibly suboptimal:
10409467b48Spatrick  *
10509467b48Spatrick  * LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);
10609467b48Spatrick  */
10709467b48Spatrick LLVMBool LLVMCreateMCJITCompilerForModule(
10809467b48Spatrick   LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
10909467b48Spatrick   struct LLVMMCJITCompilerOptions *Options, size_t SizeOfOptions,
11009467b48Spatrick   char **OutError);
11109467b48Spatrick 
11209467b48Spatrick void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE);
11309467b48Spatrick 
11409467b48Spatrick void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE);
11509467b48Spatrick 
11609467b48Spatrick void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE);
11709467b48Spatrick 
11809467b48Spatrick int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
11909467b48Spatrick                           unsigned ArgC, const char * const *ArgV,
12009467b48Spatrick                           const char * const *EnvP);
12109467b48Spatrick 
12209467b48Spatrick LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
12309467b48Spatrick                                     unsigned NumArgs,
12409467b48Spatrick                                     LLVMGenericValueRef *Args);
12509467b48Spatrick 
12609467b48Spatrick void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F);
12709467b48Spatrick 
12809467b48Spatrick void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M);
12909467b48Spatrick 
13009467b48Spatrick LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
13109467b48Spatrick                           LLVMModuleRef *OutMod, char **OutError);
13209467b48Spatrick 
13309467b48Spatrick LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
13409467b48Spatrick                           LLVMValueRef *OutFn);
13509467b48Spatrick 
13609467b48Spatrick void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
13709467b48Spatrick                                      LLVMValueRef Fn);
13809467b48Spatrick 
13909467b48Spatrick LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE);
14009467b48Spatrick LLVMTargetMachineRef
14109467b48Spatrick LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE);
14209467b48Spatrick 
14309467b48Spatrick void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
14409467b48Spatrick                           void* Addr);
14509467b48Spatrick 
14609467b48Spatrick void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global);
14709467b48Spatrick 
14809467b48Spatrick uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name);
14909467b48Spatrick 
15009467b48Spatrick uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name);
15109467b48Spatrick 
152*097a140dSpatrick /// Returns true on error, false on success. If true is returned then the error
153*097a140dSpatrick /// message is copied to OutStr and cleared in the ExecutionEngine instance.
154*097a140dSpatrick LLVMBool LLVMExecutionEngineGetErrMsg(LLVMExecutionEngineRef EE,
155*097a140dSpatrick                                       char **OutError);
156*097a140dSpatrick 
15709467b48Spatrick /*===-- Operations on memory managers -------------------------------------===*/
15809467b48Spatrick 
15909467b48Spatrick typedef uint8_t *(*LLVMMemoryManagerAllocateCodeSectionCallback)(
16009467b48Spatrick   void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
16109467b48Spatrick   const char *SectionName);
16209467b48Spatrick typedef uint8_t *(*LLVMMemoryManagerAllocateDataSectionCallback)(
16309467b48Spatrick   void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID,
16409467b48Spatrick   const char *SectionName, LLVMBool IsReadOnly);
16509467b48Spatrick typedef LLVMBool (*LLVMMemoryManagerFinalizeMemoryCallback)(
16609467b48Spatrick   void *Opaque, char **ErrMsg);
16709467b48Spatrick typedef void (*LLVMMemoryManagerDestroyCallback)(void *Opaque);
16809467b48Spatrick 
16909467b48Spatrick /**
17009467b48Spatrick  * Create a simple custom MCJIT memory manager. This memory manager can
17109467b48Spatrick  * intercept allocations in a module-oblivious way. This will return NULL
17209467b48Spatrick  * if any of the passed functions are NULL.
17309467b48Spatrick  *
17409467b48Spatrick  * @param Opaque An opaque client object to pass back to the callbacks.
17509467b48Spatrick  * @param AllocateCodeSection Allocate a block of memory for executable code.
17609467b48Spatrick  * @param AllocateDataSection Allocate a block of memory for data.
17709467b48Spatrick  * @param FinalizeMemory Set page permissions and flush cache. Return 0 on
17809467b48Spatrick  *   success, 1 on error.
17909467b48Spatrick  */
18009467b48Spatrick LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
18109467b48Spatrick   void *Opaque,
18209467b48Spatrick   LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
18309467b48Spatrick   LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
18409467b48Spatrick   LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
18509467b48Spatrick   LLVMMemoryManagerDestroyCallback Destroy);
18609467b48Spatrick 
18709467b48Spatrick void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM);
18809467b48Spatrick 
18909467b48Spatrick /*===-- JIT Event Listener functions -------------------------------------===*/
19009467b48Spatrick 
19109467b48Spatrick LLVMJITEventListenerRef LLVMCreateGDBRegistrationListener(void);
19209467b48Spatrick LLVMJITEventListenerRef LLVMCreateIntelJITEventListener(void);
19309467b48Spatrick LLVMJITEventListenerRef LLVMCreateOProfileJITEventListener(void);
19409467b48Spatrick LLVMJITEventListenerRef LLVMCreatePerfJITEventListener(void);
19509467b48Spatrick 
19609467b48Spatrick /**
19709467b48Spatrick  * @}
19809467b48Spatrick  */
19909467b48Spatrick 
20009467b48Spatrick LLVM_C_EXTERN_C_END
20109467b48Spatrick 
20209467b48Spatrick #endif
203