llvm_sys/
execution_engine.rs

1//! Runtime code generation and execution.
2
3use super::prelude::*;
4use super::target::LLVMTargetDataRef;
5use super::target_machine::{LLVMCodeModel, LLVMTargetMachineRef};
6
7#[derive(Debug)]
8pub enum LLVMOpaqueGenericValue {}
9
10#[derive(Debug)]
11pub enum LLVMOpaqueExecutionEngine {}
12
13#[derive(Debug)]
14pub enum LLVMOpaqueMCJITMemoryManager {}
15
16pub type LLVMGenericValueRef = *mut LLVMOpaqueGenericValue;
17pub type LLVMExecutionEngineRef = *mut LLVMOpaqueExecutionEngine;
18pub type LLVMMCJITMemoryManagerRef = *mut LLVMOpaqueMCJITMemoryManager;
19
20#[repr(C)]
21#[derive(Debug, Clone, Copy)]
22#[allow(non_snake_case)]
23pub struct LLVMMCJITCompilerOptions {
24    pub OptLevel: ::libc::c_uint,
25    pub CodeModel: LLVMCodeModel,
26    pub NoFramePointerElim: LLVMBool,
27    pub EnableFastISel: LLVMBool,
28    pub MCJMM: LLVMMCJITMemoryManagerRef,
29}
30
31pub type LLVMMemoryManagerAllocateCodeSectionCallback = extern "C" fn(
32    Opaque: *mut ::libc::c_void,
33    Size: ::libc::uintptr_t,
34    Alignment: ::libc::c_uint,
35    SectionID: ::libc::c_uint,
36    SectionName: *const ::libc::c_char,
37) -> *mut u8;
38pub type LLVMMemoryManagerAllocateDataSectionCallback = extern "C" fn(
39    Opaque: *mut ::libc::c_void,
40    Size: ::libc::uintptr_t,
41    Alignment: ::libc::c_uint,
42    SectionID: ::libc::c_uint,
43    SectionName: *const ::libc::c_char,
44    IsReadOnly: LLVMBool,
45) -> *mut u8;
46pub type LLVMMemoryManagerFinalizeMemoryCallback =
47    extern "C" fn(Opaque: *mut ::libc::c_void, ErrMsg: *mut *mut ::libc::c_char) -> LLVMBool;
48pub type LLVMMemoryManagerDestroyCallback = Option<extern "C" fn(Opaque: *mut ::libc::c_void)>;
49
50extern "C" {
51    pub fn LLVMLinkInMCJIT();
52    pub fn LLVMLinkInInterpreter();
53
54    // Operations on generic values
55    pub fn LLVMCreateGenericValueOfInt(
56        Ty: LLVMTypeRef,
57        N: ::libc::c_ulonglong,
58        IsSigned: LLVMBool,
59    ) -> LLVMGenericValueRef;
60    pub fn LLVMCreateGenericValueOfPointer(P: *mut ::libc::c_void) -> LLVMGenericValueRef;
61    pub fn LLVMCreateGenericValueOfFloat(
62        Ty: LLVMTypeRef,
63        N: ::libc::c_double,
64    ) -> LLVMGenericValueRef;
65    pub fn LLVMGenericValueIntWidth(GenValRef: LLVMGenericValueRef) -> ::libc::c_uint;
66    pub fn LLVMGenericValueToInt(
67        GenVal: LLVMGenericValueRef,
68        IsSigned: LLVMBool,
69    ) -> ::libc::c_ulonglong;
70    pub fn LLVMGenericValueToPointer(GenVal: LLVMGenericValueRef) -> *mut ::libc::c_void;
71    pub fn LLVMGenericValueToFloat(
72        TyRef: LLVMTypeRef,
73        GenVal: LLVMGenericValueRef,
74    ) -> ::libc::c_double;
75    pub fn LLVMDisposeGenericValue(GenVal: LLVMGenericValueRef);
76
77    // Operations on execution engines
78    pub fn LLVMCreateExecutionEngineForModule(
79        OutEE: *mut LLVMExecutionEngineRef,
80        M: LLVMModuleRef,
81        OutError: *mut *mut ::libc::c_char,
82    ) -> LLVMBool;
83    pub fn LLVMCreateInterpreterForModule(
84        OutInterp: *mut LLVMExecutionEngineRef,
85        M: LLVMModuleRef,
86        OutError: *mut *mut ::libc::c_char,
87    ) -> LLVMBool;
88    pub fn LLVMCreateJITCompilerForModule(
89        OutJIT: *mut LLVMExecutionEngineRef,
90        M: LLVMModuleRef,
91        OptLevel: ::libc::c_uint,
92        OutError: *mut *mut ::libc::c_char,
93    ) -> LLVMBool;
94    pub fn LLVMInitializeMCJITCompilerOptions(
95        Options: *mut LLVMMCJITCompilerOptions,
96        SizeOfOptions: ::libc::size_t,
97    );
98
99    /// Create an MCJIT execution engine for a module, with the given options.
100    ///
101    /// It is
102    /// the responsibility of the caller to ensure that all fields in Options up to
103    /// the given SizeOfOptions are initialized. It is correct to pass a smaller
104    /// value of SizeOfOptions that omits some fields. The canonical way of using
105    /// this is:
106    ///
107    /// ```c++
108    /// LLVMMCJITCompilerOptions options;
109    /// LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
110    /// // ... fill in those options you care about
111    /// LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
112    ///                                  &error);
113    /// ```
114    ///
115    /// Note that this is also correct, though possibly suboptimal:
116    ///
117    /// ```c++
118    /// LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);
119    /// ```
120    ///
121    /// 0 is returned on success, or 1 on failure.
122    pub fn LLVMCreateMCJITCompilerForModule(
123        OutJIT: *mut LLVMExecutionEngineRef,
124        M: LLVMModuleRef,
125        Options: *mut LLVMMCJITCompilerOptions,
126        SizeOfOptions: ::libc::size_t,
127        OutError: *mut *mut ::libc::c_char,
128    ) -> LLVMBool;
129
130    pub fn LLVMDisposeExecutionEngine(EE: LLVMExecutionEngineRef);
131    pub fn LLVMRunStaticConstructors(EE: LLVMExecutionEngineRef);
132    pub fn LLVMRunStaticDestructors(EE: LLVMExecutionEngineRef);
133    pub fn LLVMRunFunctionAsMain(
134        EE: LLVMExecutionEngineRef,
135        F: LLVMValueRef,
136        ArgC: ::libc::c_uint,
137        ArgV: *const *const ::libc::c_char,
138        EnvP: *const *const ::libc::c_char,
139    ) -> ::libc::c_int;
140    pub fn LLVMRunFunction(
141        EE: LLVMExecutionEngineRef,
142        F: LLVMValueRef,
143        NumArgs: ::libc::c_uint,
144        Args: *mut LLVMGenericValueRef,
145    ) -> LLVMGenericValueRef;
146    pub fn LLVMFreeMachineCodeForFunction(EE: LLVMExecutionEngineRef, F: LLVMValueRef);
147    pub fn LLVMAddModule(EE: LLVMExecutionEngineRef, M: LLVMModuleRef);
148    pub fn LLVMRemoveModule(
149        EE: LLVMExecutionEngineRef,
150        M: LLVMModuleRef,
151        OutMod: *mut LLVMModuleRef,
152        OutError: *mut *mut ::libc::c_char,
153    ) -> LLVMBool;
154    pub fn LLVMFindFunction(
155        EE: LLVMExecutionEngineRef,
156        Name: *const ::libc::c_char,
157        OutFn: *mut LLVMValueRef,
158    ) -> LLVMBool;
159    pub fn LLVMRecompileAndRelinkFunction(
160        EE: LLVMExecutionEngineRef,
161        Fn: LLVMValueRef,
162    ) -> *mut ::libc::c_void;
163    pub fn LLVMGetExecutionEngineTargetData(EE: LLVMExecutionEngineRef) -> LLVMTargetDataRef;
164    pub fn LLVMGetExecutionEngineTargetMachine(EE: LLVMExecutionEngineRef) -> LLVMTargetMachineRef;
165    pub fn LLVMAddGlobalMapping(
166        EE: LLVMExecutionEngineRef,
167        Global: LLVMValueRef,
168        Addr: *mut ::libc::c_void,
169    );
170    pub fn LLVMGetPointerToGlobal(
171        EE: LLVMExecutionEngineRef,
172        Global: LLVMValueRef,
173    ) -> *mut ::libc::c_void;
174    pub fn LLVMGetGlobalValueAddress(
175        EE: LLVMExecutionEngineRef,
176        Name: *const ::libc::c_char,
177    ) -> u64;
178    pub fn LLVMGetFunctionAddress(EE: LLVMExecutionEngineRef, Name: *const ::libc::c_char) -> u64;
179
180    pub fn LLVMExecutionEngineGetErrMsg(
181        EE: LLVMExecutionEngineRef,
182        OutError: *mut *mut ::libc::c_char,
183    ) -> LLVMBool;
184
185    // Operations on memory managers
186    // Create a simple custom MCJIT memory manager.
187    //
188    // This memory manager can intercept allocations in a module-oblivious way. It will
189    // return NULL if any of the passed functions are NULL.
190    //
191    // `AllocateCodeSection` and `AllocateDataSection` are called to allocate blocks
192    // of memory for executable code and data, respectively. `FinalizeMemory` is called
193    // to set page permissions and flush caches, returning 0 on success and 1 on error.
194    //
195    // `Opaque` will be passed to the callbacks.
196    pub fn LLVMCreateSimpleMCJITMemoryManager(
197        Opaque: *mut ::libc::c_void,
198        AllocateCodeSection: LLVMMemoryManagerAllocateCodeSectionCallback,
199        AllocateDataSection: LLVMMemoryManagerAllocateDataSectionCallback,
200        FinalizeMemory: LLVMMemoryManagerFinalizeMemoryCallback,
201        Destroy: LLVMMemoryManagerDestroyCallback,
202    ) -> LLVMMCJITMemoryManagerRef;
203
204    pub fn LLVMDisposeMCJITMemoryManager(MM: LLVMMCJITMemoryManagerRef);
205
206    // JIT event listener functions
207    pub fn LLVMCreateGDBRegistrationListener() -> LLVMJITEventListenerRef;
208    pub fn LLVMCreateIntelJITEventListener() -> LLVMJITEventListenerRef;
209    pub fn LLVMCreateOProfileJITEventListener() -> LLVMJITEventListenerRef;
210    pub fn LLVMCreatePerfJITEventListener() -> LLVMJITEventListenerRef;
211
212}