1--  LLVM binding
2--  Copyright (C) 2014 Tristan Gingold
3--
4--  This program is free software: you can redistribute it and/or modify
5--  it under the terms of the GNU General Public License as published by
6--  the Free Software Foundation, either version 2 of the License, or
7--  (at your option) any later version.
8--
9--  This program is distributed in the hope that it will be useful,
10--  but WITHOUT ANY WARRANTY; without even the implied warranty of
11--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12--  GNU General Public License for more details.
13--
14--  You should have received a copy of the GNU General Public License
15--  along with this program.  If not, see <gnu.org/licenses>.
16with System; use System;
17with Interfaces; use Interfaces;
18with Interfaces.C; use Interfaces.C;
19with LLVM.Core; use LLVM.Core;
20with LLVM.Target; use LLVM.Target;
21
22package LLVM.ExecutionEngine is
23   type GenericValueRef is new Address;
24   type GenericValueRefArray is array (unsigned range <>) of GenericValueRef;
25   pragma Convention (C, GenericValueRefArray);
26   type ExecutionEngineRef is new Address;
27
28   procedure LinkInJIT;
29   procedure LinkInMCJIT;
30   procedure LinkInInterpreter;
31
32   -- Operations on generic values --------------------------------------
33
34   function CreateGenericValueOfInt(Ty : TypeRef;
35                                    N : Unsigned_64;
36                                    IsSigned : Integer)
37                                   return GenericValueRef;
38
39   function CreateGenericValueOfPointer(P : System.Address)
40                                           return GenericValueRef;
41
42   function CreateGenericValueOfFloat(Ty : TypeRef; N : double)
43                                         return GenericValueRef;
44
45   function GenericValueIntWidth(GenValRef : GenericValueRef)
46                                    return unsigned;
47
48   function GenericValueToInt(GenVal : GenericValueRef;
49                                  IsSigned : Integer) return Unsigned_64;
50
51   function GenericValueToPointer(GenVal : GenericValueRef)
52                                     return System.Address;
53
54   function GenericValueToFloat(TyRef : TypeRef; GenVal : GenericValueRef)
55                               return double;
56
57   procedure DisposeGenericValue(GenVal : GenericValueRef);
58
59   -- Operations on execution engines -----------------------------------
60
61   function CreateExecutionEngineForModule
62     (EE : access ExecutionEngineRef; M : ModuleRef; Error : access Cstring)
63     return Bool;
64
65   function CreateInterpreterForModule (Interp : access ExecutionEngineRef;
66                                        M : ModuleRef;
67                                        Error : access Cstring)
68                                       return Bool;
69
70   function CreateJITCompilerForModule (JIT : access ExecutionEngineRef;
71                                        M : ModuleRef;
72                                        OptLevel : unsigned;
73                                        Error : access Cstring)
74     return Bool;
75
76
77   procedure DisposeExecutionEngine(EE : ExecutionEngineRef);
78
79   procedure RunStaticConstructors(EE : ExecutionEngineRef);
80
81   procedure RunStaticDestructors(EE : ExecutionEngineRef);
82
83   function RunFunctionAsMain(EE : ExecutionEngineRef;
84                              F : ValueRef;
85                              ArgC : unsigned; Argv : Address; EnvP : Address)
86                             return Integer;
87
88   function RunFunction(EE : ExecutionEngineRef;
89                        F : ValueRef;
90                        NumArgs : unsigned;
91                        Args : GenericValueRefArray)
92                       return GenericValueRef;
93
94   procedure FreeMachineCodeForFunction(EE : ExecutionEngineRef; F : ValueRef);
95
96   procedure AddModule(EE : ExecutionEngineRef; M : ModuleRef);
97
98   function RemoveModule(EE : ExecutionEngineRef;
99                         M : ModuleRef;
100                         OutMod : access ModuleRef;
101                         OutError : access Cstring) return Bool;
102
103   function FindFunction(EE : ExecutionEngineRef; Name : Cstring;
104                                                  OutFn : access ValueRef)
105                        return Integer;
106
107   function GetExecutionEngineTargetData(EE : ExecutionEngineRef)
108                                        return TargetDataRef;
109
110   procedure AddGlobalMapping(EE : ExecutionEngineRef; Global : ValueRef;
111                                                       Addr : Address);
112
113   function GetPointerToGlobal (EE : ExecutionEngineRef; GV : ValueRef)
114                               return Address;
115   function GetPointerToFunctionOrStub (EE : ExecutionEngineRef;
116                                        Func : ValueRef)
117                                       return Address;
118
119private
120   pragma Import (C, LinkInJIT, "LLVMLinkInJIT");
121   pragma Import (C, LinkInMCJIT, "LLVMLinkInMCJIT");
122   pragma Import (C, LinkInInterpreter, "LLVMLinkInInterpreter");
123
124   pragma Import (C, CreateGenericValueOfInt, "LLVMCreateGenericValueOfInt");
125   pragma Import (C, CreateGenericValueOfPointer,
126                  "LLVMCreateGenericValueOfPointer");
127   pragma Import (C, CreateGenericValueOfFloat,
128                  "LLVMCreateGenericValueOfFloat");
129   pragma Import (C, GenericValueIntWidth, "LLVMGenericValueIntWidth");
130   pragma Import (C, GenericValueToInt, "LLVMGenericValueToInt");
131   pragma Import (C, GenericValueToPointer, "LLVMGenericValueToPointer");
132   pragma Import (C, GenericValueToFloat, "LLVMGenericValueToFloat");
133   pragma Import (C, DisposeGenericValue, "LLVMDisposeGenericValue");
134
135   -- Operations on execution engines -----------------------------------
136
137   pragma Import (C, CreateExecutionEngineForModule,
138                  "LLVMCreateExecutionEngineForModule");
139   pragma Import (C, CreateInterpreterForModule,
140                  "LLVMCreateInterpreterForModule");
141   pragma Import (C, CreateJITCompilerForModule,
142                  "LLVMCreateJITCompilerForModule");
143   pragma Import (C, DisposeExecutionEngine, "LLVMDisposeExecutionEngine");
144   pragma Import (C, RunStaticConstructors, "LLVMRunStaticConstructors");
145   pragma Import (C, RunStaticDestructors, "LLVMRunStaticDestructors");
146   pragma Import (C, RunFunctionAsMain, "LLVMRunFunctionAsMain");
147   pragma Import (C, RunFunction, "LLVMRunFunction");
148   pragma Import (C, FreeMachineCodeForFunction,
149                  "LLVMFreeMachineCodeForFunction");
150   pragma Import (C, AddModule, "LLVMAddModule");
151   pragma Import (C, RemoveModule, "LLVMRemoveModule");
152   pragma Import (C, FindFunction, "LLVMFindFunction");
153   pragma Import (C, GetExecutionEngineTargetData,
154                "LLVMGetExecutionEngineTargetData");
155   pragma Import (C, AddGlobalMapping, "LLVMAddGlobalMapping");
156
157   pragma Import (C, GetPointerToFunctionOrStub,
158                  "LLVMGetPointerToFunctionOrStub");
159   pragma Import (C, GetPointerToGlobal,
160                  "LLVMGetPointerToGlobal");
161end LLVM.ExecutionEngine;
162