1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Runtime code generation and execution.

use super::prelude::*;
use super::target::LLVMTargetDataRef;
use super::target_machine::{LLVMCodeModel, LLVMTargetMachineRef};

#[derive(Debug)]
pub enum LLVMOpaqueGenericValue {}

#[derive(Debug)]
pub enum LLVMOpaqueExecutionEngine {}

#[derive(Debug)]
pub enum LLVMOpaqueMCJITMemoryManager {}

pub type LLVMGenericValueRef = *mut LLVMOpaqueGenericValue;
pub type LLVMExecutionEngineRef = *mut LLVMOpaqueExecutionEngine;
pub type LLVMMCJITMemoryManagerRef = *mut LLVMOpaqueMCJITMemoryManager;

#[repr(C)]
#[derive(Debug, Clone, Copy)]
#[allow(non_snake_case)]
pub struct LLVMMCJITCompilerOptions {
    pub OptLevel: ::libc::c_uint,
    pub CodeModel: LLVMCodeModel,
    pub NoFramePointerElim: LLVMBool,
    pub EnableFastISel: LLVMBool,
    pub MCJMM: LLVMMCJITMemoryManagerRef,
}

pub type LLVMMemoryManagerAllocateCodeSectionCallback = extern "C" fn(
    Opaque: *mut ::libc::c_void,
    Size: ::libc::uintptr_t,
    Alignment: ::libc::c_uint,
    SectionID: ::libc::c_uint,
    SectionName: *const ::libc::c_char,
) -> *mut u8;
pub type LLVMMemoryManagerAllocateDataSectionCallback = extern "C" fn(
    Opaque: *mut ::libc::c_void,
    Size: ::libc::uintptr_t,
    Alignment: ::libc::c_uint,
    SectionID: ::libc::c_uint,
    SectionName: *const ::libc::c_char,
    IsReadOnly: LLVMBool,
) -> *mut u8;
pub type LLVMMemoryManagerFinalizeMemoryCallback =
    extern "C" fn(Opaque: *mut ::libc::c_void, ErrMsg: *mut *mut ::libc::c_char) -> LLVMBool;
pub type LLVMMemoryManagerDestroyCallback = Option<extern "C" fn(Opaque: *mut ::libc::c_void)>;

extern "C" {
    pub fn LLVMLinkInMCJIT();
    pub fn LLVMLinkInInterpreter();

    // Operations on generic values
    pub fn LLVMCreateGenericValueOfInt(
        Ty: LLVMTypeRef,
        N: ::libc::c_ulonglong,
        IsSigned: LLVMBool,
    ) -> LLVMGenericValueRef;
    pub fn LLVMCreateGenericValueOfPointer(P: *mut ::libc::c_void) -> LLVMGenericValueRef;
    pub fn LLVMCreateGenericValueOfFloat(
        Ty: LLVMTypeRef,
        N: ::libc::c_double,
    ) -> LLVMGenericValueRef;
    pub fn LLVMGenericValueIntWidth(GenValRef: LLVMGenericValueRef) -> ::libc::c_uint;
    pub fn LLVMGenericValueToInt(
        GenVal: LLVMGenericValueRef,
        IsSigned: LLVMBool,
    ) -> ::libc::c_ulonglong;
    pub fn LLVMGenericValueToPointer(GenVal: LLVMGenericValueRef) -> *mut ::libc::c_void;
    pub fn LLVMGenericValueToFloat(
        TyRef: LLVMTypeRef,
        GenVal: LLVMGenericValueRef,
    ) -> ::libc::c_double;
    pub fn LLVMDisposeGenericValue(GenVal: LLVMGenericValueRef);

    // Operations on execution engines
    pub fn LLVMCreateExecutionEngineForModule(
        OutEE: *mut LLVMExecutionEngineRef,
        M: LLVMModuleRef,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;
    pub fn LLVMCreateInterpreterForModule(
        OutInterp: *mut LLVMExecutionEngineRef,
        M: LLVMModuleRef,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;
    pub fn LLVMCreateJITCompilerForModule(
        OutJIT: *mut LLVMExecutionEngineRef,
        M: LLVMModuleRef,
        OptLevel: ::libc::c_uint,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;
    pub fn LLVMInitializeMCJITCompilerOptions(
        Options: *mut LLVMMCJITCompilerOptions,
        SizeOfOptions: ::libc::size_t,
    );

    /// Create an MCJIT execution engine for a module, with the given options.
    ///
    /// It is
    /// the responsibility of the caller to ensure that all fields in Options up to
    /// the given SizeOfOptions are initialized. It is correct to pass a smaller
    /// value of SizeOfOptions that omits some fields. The canonical way of using
    /// this is:
    ///
    /// ```c++
    /// LLVMMCJITCompilerOptions options;
    /// LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
    /// // ... fill in those options you care about
    /// LLVMCreateMCJITCompilerForModule(&jit, mod, &options, sizeof(options),
    ///                                  &error);
    /// ```
    ///
    /// Note that this is also correct, though possibly suboptimal:
    ///
    /// ```c++
    /// LLVMCreateMCJITCompilerForModule(&jit, mod, 0, 0, &error);
    /// ```
    ///
    /// 0 is returned on success, or 1 on failure.
    pub fn LLVMCreateMCJITCompilerForModule(
        OutJIT: *mut LLVMExecutionEngineRef,
        M: LLVMModuleRef,
        Options: *mut LLVMMCJITCompilerOptions,
        SizeOfOptions: ::libc::size_t,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;

    pub fn LLVMDisposeExecutionEngine(EE: LLVMExecutionEngineRef);
    pub fn LLVMRunStaticConstructors(EE: LLVMExecutionEngineRef);
    pub fn LLVMRunStaticDestructors(EE: LLVMExecutionEngineRef);
    pub fn LLVMRunFunctionAsMain(
        EE: LLVMExecutionEngineRef,
        F: LLVMValueRef,
        ArgC: ::libc::c_uint,
        ArgV: *const *const ::libc::c_char,
        EnvP: *const *const ::libc::c_char,
    ) -> ::libc::c_int;
    pub fn LLVMRunFunction(
        EE: LLVMExecutionEngineRef,
        F: LLVMValueRef,
        NumArgs: ::libc::c_uint,
        Args: *mut LLVMGenericValueRef,
    ) -> LLVMGenericValueRef;
    pub fn LLVMFreeMachineCodeForFunction(EE: LLVMExecutionEngineRef, F: LLVMValueRef);
    pub fn LLVMAddModule(EE: LLVMExecutionEngineRef, M: LLVMModuleRef);
    pub fn LLVMRemoveModule(
        EE: LLVMExecutionEngineRef,
        M: LLVMModuleRef,
        OutMod: *mut LLVMModuleRef,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;
    pub fn LLVMFindFunction(
        EE: LLVMExecutionEngineRef,
        Name: *const ::libc::c_char,
        OutFn: *mut LLVMValueRef,
    ) -> LLVMBool;
    pub fn LLVMRecompileAndRelinkFunction(
        EE: LLVMExecutionEngineRef,
        Fn: LLVMValueRef,
    ) -> *mut ::libc::c_void;
    pub fn LLVMGetExecutionEngineTargetData(EE: LLVMExecutionEngineRef) -> LLVMTargetDataRef;
    pub fn LLVMGetExecutionEngineTargetMachine(EE: LLVMExecutionEngineRef) -> LLVMTargetMachineRef;
    pub fn LLVMAddGlobalMapping(
        EE: LLVMExecutionEngineRef,
        Global: LLVMValueRef,
        Addr: *mut ::libc::c_void,
    );
    pub fn LLVMGetPointerToGlobal(
        EE: LLVMExecutionEngineRef,
        Global: LLVMValueRef,
    ) -> *mut ::libc::c_void;
    pub fn LLVMGetGlobalValueAddress(
        EE: LLVMExecutionEngineRef,
        Name: *const ::libc::c_char,
    ) -> u64;
    pub fn LLVMGetFunctionAddress(EE: LLVMExecutionEngineRef, Name: *const ::libc::c_char) -> u64;

    pub fn LLVMExecutionEngineGetErrMsg(
        EE: LLVMExecutionEngineRef,
        OutError: *mut *mut ::libc::c_char,
    ) -> LLVMBool;

    // Operations on memory managers
    // Create a simple custom MCJIT memory manager.
    //
    // This memory manager can intercept allocations in a module-oblivious way. It will
    // return NULL if any of the passed functions are NULL.
    //
    // `AllocateCodeSection` and `AllocateDataSection` are called to allocate blocks
    // of memory for executable code and data, respectively. `FinalizeMemory` is called
    // to set page permissions and flush caches, returning 0 on success and 1 on error.
    //
    // `Opaque` will be passed to the callbacks.
    pub fn LLVMCreateSimpleMCJITMemoryManager(
        Opaque: *mut ::libc::c_void,
        AllocateCodeSection: LLVMMemoryManagerAllocateCodeSectionCallback,
        AllocateDataSection: LLVMMemoryManagerAllocateDataSectionCallback,
        FinalizeMemory: LLVMMemoryManagerFinalizeMemoryCallback,
        Destroy: LLVMMemoryManagerDestroyCallback,
    ) -> LLVMMCJITMemoryManagerRef;

    pub fn LLVMDisposeMCJITMemoryManager(MM: LLVMMCJITMemoryManagerRef);

    // JIT event listener functions
    pub fn LLVMCreateGDBRegistrationListener() -> LLVMJITEventListenerRef;
    pub fn LLVMCreateIntelJITEventListener() -> LLVMJITEventListenerRef;
    pub fn LLVMCreateOProfileJITEventListener() -> LLVMJITEventListenerRef;
    pub fn LLVMCreatePerfJITEventListener() -> LLVMJITEventListenerRef;

}