llvm.org GIT mirror llvm / 2e855e6
C and Ocaml bindings for ExecutionEngine (i.e., the JIT compiler). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45335 91177308-0d34-0410-b5e6-96231b3b80d8 Gordon Henriksen 11 years ago
12 changed file(s) with 1033 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
8484 9F4B0E600D0E02580061F270 /* llvm_bitreader.mli */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = llvm_bitreader.mli; sourceTree = ""; };
8585 9F4B0E8C0D0E05ED0061F270 /* BitReader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BitReader.cpp; sourceTree = ""; };
8686 9F4B0E8D0D0E05ED0061F270 /* DeserializeAPFloat.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DeserializeAPFloat.cpp; sourceTree = ""; };
87 9F502ADB0D1D8CA3007939DF /* executionengine_ocaml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = executionengine_ocaml.c; sourceTree = ""; };
88 9F502ADC0D1D8CA3007939DF /* llvm_executionengine.ml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = llvm_executionengine.ml; sourceTree = ""; };
89 9F502ADD0D1D8CA3007939DF /* llvm_executionengine.mli */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = llvm_executionengine.mli; sourceTree = ""; };
90 9F502AEC0D1D8CF8007939DF /* executionengine.ml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = executionengine.ml; sourceTree = ""; };
91 9F502B090D1D8D8D007939DF /* ExecutionEngineBindings.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExecutionEngineBindings.cpp; sourceTree = ""; };
8792 9F5B90CB0D0CE87100CDFDEA /* StringPool.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringPool.cpp; sourceTree = ""; };
8893 9F5B90CE0D0CE89300CDFDEA /* AlignOf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AlignOf.h; sourceTree = ""; };
8994 9F5B90CF0D0CE89300CDFDEA /* Registry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Registry.h; sourceTree = ""; };
232237 9FE450E00C77ABE400C4FEA4 /* ArchiveInternals.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = ArchiveInternals.h; sourceTree = ""; };
233238 9FE450E10C77ABE400C4FEA4 /* ArchiveReader.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = ArchiveReader.cpp; sourceTree = ""; };
234239 9FE450E20C77ABE400C4FEA4 /* ArchiveWriter.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = ArchiveWriter.cpp; sourceTree = ""; };
240 9FEB8C550D1CD1E200EE46BC /* ExecutionEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExecutionEngine.h; sourceTree = ""; };
235241 CF1ACC9709C9DE4400D3C5EB /* IntrinsicInst.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = IntrinsicInst.cpp; path = ../lib/VMCore/IntrinsicInst.cpp; sourceTree = ""; };
236242 CF26835B09178F5500C5F253 /* TargetInstrItineraries.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TargetInstrItineraries.h; sourceTree = ""; };
237243 CF32AF5C0AEE6A4E00D24CD4 /* LLVMTargetMachine.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LLVMTargetMachine.cpp; sourceTree = ""; };
11201126 path = bitreader;
11211127 sourceTree = "";
11221128 };
1129 9F502ACD0D1D8CA3007939DF /* executionengine */ = {
1130 isa = PBXGroup;
1131 children = (
1132 9F502ADB0D1D8CA3007939DF /* executionengine_ocaml.c */,
1133 9F502ADC0D1D8CA3007939DF /* llvm_executionengine.ml */,
1134 9F502ADD0D1D8CA3007939DF /* llvm_executionengine.mli */,
1135 );
1136 path = executionengine;
1137 sourceTree = "";
1138 };
11231139 9F68EB030C77AD2C004AA152 /* lib/Bitcode */ = {
11241140 isa = PBXGroup;
11251141 children = (
12431259 9F7C2C4F0CB9496C00498408 /* analysis.ml */,
12441260 9F6B2CC00D0F6E56000F00FD /* bitreader.ml */,
12451261 9F7C2C520CB9496C00498408 /* bitwriter.ml */,
1262 9F502AEC0D1D8CF8007939DF /* executionengine.ml */,
12461263 9F7C2C5D0CB9496C00498408 /* vmcore.ml */,
12471264 );
12481265 path = Ocaml;
12681285 9FD3E56F0CA0116100E54D15 /* ocaml */ = {
12691286 isa = PBXGroup;
12701287 children = (
1288 9F502ACD0D1D8CA3007939DF /* executionengine */,
12711289 9F7C240B0CB81ECD00498408 /* analysis */,
12721290 9F4B0E5D0D0E02580061F270 /* bitreader */,
12731291 9FD3E5700CA0116100E54D15 /* bitwriter */,
13151333 9F5B90E70D0DF19100CDFDEA /* BitReader.h */,
13161334 9FD3E58D0CA0125F00E54D15 /* BitWriter.h */,
13171335 9FD3E58E0CA0125F00E54D15 /* Core.h */,
1336 9FEB8C550D1CD1E200EE46BC /* ExecutionEngine.h */,
13181337 CF8F1B490B64F7AB00BB4199 /* LinkTimeOptimizer.h */,
13191338 );
13201339 name = "include/llvm-c";
16061625 DE66EDC508ABEC9000323D32 /* Interpreter */,
16071626 DE66EDD308ABEC9000323D32 /* JIT */,
16081627 DE66EDC408ABEC9000323D32 /* ExecutionEngine.cpp */,
1628 9F502B090D1D8D8D007939DF /* ExecutionEngineBindings.cpp */,
16091629 );
16101630 name = lib/ExecutionEngine;
16111631 path = ../lib/ExecutionEngine;
77 ##===----------------------------------------------------------------------===##
88
99 LEVEL := ../..
10 DIRS = llvm bitreader bitwriter analysis
10 DIRS = llvm bitreader bitwriter analysis executionengine
1111
1212 include $(LEVEL)/Makefile.common
4040 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
4141 $(shell $(LLVM_CONFIG) --ldflags)) \
4242 $(UsedLibs))
43
44 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
45 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
46 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
47
48 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
49 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
43
44 ifneq ($(ENABLE_OPTIMIZED),1)
45 OCAMLDEBUGFLAG := -g
46 endif
47
48 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
49 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
50 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
51 -o)
52
53 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
54 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
5055
5156 # Source files
5257 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
134139 build-deplibs: $(OutputLibs)
135140
136141 $(OcamlDir)/%.a: $(LibDir)/%.a
142 $(Verb) ln -sf $< $@
143
144 $(OcamlDir)/%.o: $(LibDir)/%.o
137145 $(Verb) ln -sf $< $@
138146
139147 clean-deplibs:
1313
1414 #include "llvm-c/BitReader.h"
1515 #include "caml/alloc.h"
16 #include "caml/mlvalues.h"
16 #include "caml/fail.h"
1717 #include "caml/memory.h"
18 #include
1918
2019
2120 /* Can't use the recommended caml_named_value mechanism for backwards
2827 return Val_unit;
2928 }
3029
31 void llvm_raise(value Prototype, char *Message);
30 static void llvm_raise(value Prototype, char *Message) {
31 CAMLparam1(Prototype);
32 CAMLlocal1(CamlMessage);
33
34 CamlMessage = copy_string(Message);
35 LLVMDisposeMessage(Message);
36
37 raise_with_arg(Prototype, CamlMessage);
38 abort(); /* NOTREACHED */
39 CAMLnoreturn;
40 }
3241
3342
3443 /*===-- Modules -----------------------------------------------------------===*/
0 ##===- bindings/ocaml/executionengine/Makefile --------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file was developed by Gordon Henriksen and is distributed under the
5 # University of Illinois Open Source License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8 #
9 # This is the makefile for the Objective Caml Llvm_executionengine interface.
10 #
11 ##===----------------------------------------------------------------------===##
12
13 LEVEL := ../../..
14 LIBRARYNAME := llvm_executionengine
15 DONT_BUILD_RELINKED := 1
16 UsedComponents := executionengine jit interpreter native
17 UsedOcamlInterfaces := llvm
18
19 include ../Makefile.ocaml
0 /*===-- analysis_ocaml.c - LLVM Ocaml Glue ----------------------*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file was developed by Gordon Henriksen and is distributed under the *|
5 |* University of Illinois Open Source License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file glues LLVM's ocaml interface to its C interface. These functions *|
10 |* are by and large transparent wrappers to the corresponding C functions. *|
11 |* *|
12 |* Note that these functions intentionally take liberties with the CAMLparamX *|
13 |* macros, since most of the parameters are not GC heap objects. *|
14 |* *|
15 \*===----------------------------------------------------------------------===*/
16
17 #include "llvm-c/ExecutionEngine.h"
18 #include "caml/alloc.h"
19 #include "caml/custom.h"
20 #include "caml/fail.h"
21 #include "caml/memory.h"
22 #include
23 #include
24
25
26 /* Can't use the recommended caml_named_value mechanism for backwards
27 compatibility reasons. This is largely equivalent. */
28 static value llvm_ee_error_exn;
29
30 CAMLprim value llvm_register_ee_exns(value Error) {
31 llvm_ee_error_exn = Field(Error, 0);
32 register_global_root(&llvm_ee_error_exn);
33 return Val_unit;
34 }
35
36 static void llvm_raise(value Prototype, char *Message) {
37 CAMLparam1(Prototype);
38 CAMLlocal1(CamlMessage);
39
40 CamlMessage = copy_string(Message);
41 LLVMDisposeMessage(Message);
42
43 raise_with_arg(Prototype, CamlMessage);
44 abort(); /* NOTREACHED */
45 CAMLnoreturn;
46 }
47
48
49 /*--... Operations on generic values .......................................--*/
50
51 #define Genericvalue_val(v) (*(LLVMGenericValueRef *)(Data_custom_val(v)))
52
53 static void llvm_finalize_generic_value(value GenVal) {
54 LLVMDisposeGenericValue(Genericvalue_val(GenVal));
55 }
56
57 static struct custom_operations generic_value_ops = {
58 (char *) "LLVMGenericValue",
59 llvm_finalize_generic_value,
60 custom_compare_default,
61 custom_hash_default,
62 custom_serialize_default,
63 custom_deserialize_default
64 };
65
66 static value alloc_generic_value(LLVMGenericValueRef Ref) {
67 value Val = alloc_custom(&generic_value_ops, sizeof(LLVMGenericValueRef), 0, 1);
68 Genericvalue_val(Val) = Ref;
69 return Val;
70 }
71
72 /* Llvm.lltype -> float -> t */
73 CAMLprim value llvm_genericvalue_of_float(LLVMTypeRef Ty, value N) {
74 return alloc_generic_value(LLVMCreateGenericValueOfFloat(Ty, Double_val(N)));
75 }
76
77 /* 'a -> t */
78 CAMLprim value llvm_genericvalue_of_value(value V) {
79 return alloc_generic_value(LLVMCreateGenericValueOfPointer(Op_val(V)));
80 }
81
82 /* Llvm.lltype -> int -> t */
83 CAMLprim value llvm_genericvalue_of_int(LLVMTypeRef Ty, value Int) {
84 return alloc_generic_value(LLVMCreateGenericValueOfInt(Ty, Int_val(Int), 1));
85 }
86
87 /* Llvm.lltype -> int32 -> t */
88 CAMLprim value llvm_genericvalue_of_int32(LLVMTypeRef Ty, value Int32) {
89 return alloc_generic_value(LLVMCreateGenericValueOfInt(Ty, Int32_val(Int32),
90 1));
91 }
92
93 /* Llvm.lltype -> nativeint -> t */
94 CAMLprim value llvm_genericvalue_of_nativeint(LLVMTypeRef Ty, value NatInt) {
95 return alloc_generic_value(LLVMCreateGenericValueOfInt(Ty,
96 Nativeint_val(NatInt),
97 1));
98 }
99
100 /* Llvm.lltype -> int64 -> t */
101 CAMLprim value llvm_genericvalue_of_int64(LLVMTypeRef Ty, value Int64) {
102 return alloc_generic_value(LLVMCreateGenericValueOfInt(Ty, Int64_val(Int64),
103 1));
104 }
105
106 /* Llvm.lltype -> t -> float */
107 CAMLprim value llvm_genericvalue_as_float(LLVMTypeRef Ty, value GenVal) {
108 return copy_double(LLVMGenericValueToFloat(Ty, Genericvalue_val(GenVal)));
109 }
110
111 /* t -> 'a */
112 CAMLprim value llvm_genericvalue_as_value(value GenVal) {
113 return Val_op(LLVMGenericValueToPointer(Genericvalue_val(GenVal)));
114 }
115
116 /* t -> int */
117 CAMLprim value llvm_genericvalue_as_int(value GenVal) {
118 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 8 * sizeof(value)
119 && "Generic value too wide to treat as an int!");
120 return Val_int(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1));
121 }
122
123 /* t -> int32 */
124 CAMLprim value llvm_genericvalue_as_int32(value GenVal) {
125 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 32
126 && "Generic value too wide to treat as an int32!");
127 return copy_int32(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1));
128 }
129
130 /* t -> int64 */
131 CAMLprim value llvm_genericvalue_as_int64(value GenVal) {
132 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 64
133 && "Generic value too wide to treat as an int64!");
134 return copy_int64(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1));
135 }
136
137 /* t -> nativeint */
138 CAMLprim value llvm_genericvalue_as_nativeint(value GenVal) {
139 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 8 * sizeof(value)
140 && "Generic value too wide to treat as a nativeint!");
141 return copy_nativeint(LLVMGenericValueToInt(Genericvalue_val(GenVal),1));
142 }
143
144
145 /*--... Operations on execution engines ....................................--*/
146
147 /* llmoduleprovider -> ExecutionEngine.t */
148 CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleProviderRef MP) {
149 LLVMExecutionEngineRef Interp;
150 char *Error;
151 if (LLVMCreateExecutionEngine(&Interp, MP, &Error))
152 llvm_raise(llvm_ee_error_exn, Error);
153 return Interp;
154 }
155
156 /* llmoduleprovider -> ExecutionEngine.t */
157 CAMLprim LLVMExecutionEngineRef
158 llvm_ee_create_interpreter(LLVMModuleProviderRef MP) {
159 LLVMExecutionEngineRef Interp;
160 char *Error;
161 if (LLVMCreateInterpreter(&Interp, MP, &Error))
162 llvm_raise(llvm_ee_error_exn, Error);
163 return Interp;
164 }
165
166 /* llmoduleprovider -> ExecutionEngine.t */
167 CAMLprim LLVMExecutionEngineRef
168 llvm_ee_create_jit(LLVMModuleProviderRef MP) {
169 LLVMExecutionEngineRef JIT;
170 char *Error;
171 if (LLVMCreateJITCompiler(&JIT, MP, &Error))
172 llvm_raise(llvm_ee_error_exn, Error);
173 return JIT;
174 }
175
176 /* ExecutionEngine.t -> unit */
177 CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
178 LLVMDisposeExecutionEngine(EE);
179 return Val_unit;
180 }
181
182 /* llmoduleprovider -> ExecutionEngine.t -> unit */
183 CAMLprim value llvm_ee_add_mp(LLVMModuleProviderRef MP,
184 LLVMExecutionEngineRef EE) {
185 LLVMAddModuleProvider(EE, MP);
186 return Val_unit;
187 }
188
189 /* llmoduleprovider -> ExecutionEngine.t -> llmodule */
190 CAMLprim LLVMModuleRef llvm_ee_remove_mp(LLVMModuleProviderRef MP,
191 LLVMExecutionEngineRef EE) {
192 LLVMModuleRef RemovedModule;
193 char *Error;
194 if (LLVMRemoveModuleProvider(EE, MP, &RemovedModule, &Error))
195 llvm_raise(llvm_ee_error_exn, Error);
196 return RemovedModule;
197 }
198
199 /* string -> ExecutionEngine.t -> llvalue option */
200 CAMLprim value llvm_ee_find_function(value Name, LLVMExecutionEngineRef EE) {
201 CAMLparam1(Name);
202 CAMLlocal1(Option);
203 LLVMValueRef Found;
204 if (LLVMFindFunction(EE, String_val(Name), &Found))
205 CAMLreturn(Val_unit);
206 Option = alloc(1, 1);
207 Field(Option, 0) = Val_op(Found);
208 CAMLreturn(Option);
209 }
210
211 /* llvalue -> GenericValue.t array -> ExecutionEngine.t -> GenericValue.t */
212 CAMLprim value llvm_ee_run_function(LLVMValueRef F, value Args,
213 LLVMExecutionEngineRef EE) {
214 unsigned NumArgs;
215 LLVMGenericValueRef Result, *GVArgs;
216 unsigned I;
217
218 NumArgs = Wosize_val(Args);
219 GVArgs = (LLVMGenericValueRef*) malloc(NumArgs * sizeof(LLVMGenericValueRef));
220 for (I = 0; I != NumArgs; ++I)
221 GVArgs[I] = Genericvalue_val(Field(Args, I));
222
223 Result = LLVMRunFunction(EE, F, NumArgs, GVArgs);
224
225 free(GVArgs);
226 return alloc_generic_value(Result);
227 }
228
229 /* ExecutionEngine.t -> unit */
230 CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
231 LLVMRunStaticConstructors(EE);
232 return Val_unit;
233 }
234
235 /* ExecutionEngine.t -> unit */
236 CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
237 LLVMRunStaticDestructors(EE);
238 return Val_unit;
239 }
240
241 /* llvalue -> string array -> (string * string) array -> ExecutionEngine.t ->
242 int */
243 CAMLprim value llvm_ee_run_function_as_main(LLVMValueRef F,
244 value Args, value Env,
245 LLVMExecutionEngineRef EE) {
246 CAMLparam2(Args, Env);
247 int I, NumArgs, NumEnv, EnvSize, Result;
248 const char **CArgs, **CEnv;
249 char *CEnvBuf, *Pos;
250
251 NumArgs = Wosize_val(Args);
252 NumEnv = Wosize_val(Env);
253
254 /* Build the environment. */
255 CArgs = (const char **) malloc(NumArgs * sizeof(char*));
256 for (I = 0; I != NumArgs; ++I)
257 CArgs[I] = String_val(Field(Args, I));
258
259 /* Compute the size of the environment string buffer. */
260 for (I = 0, EnvSize = 0; I != NumEnv; ++I) {
261 EnvSize += strlen(String_val(Field(Field(Env, I), 0))) + 1;
262 EnvSize += strlen(String_val(Field(Field(Env, I), 1))) + 1;
263 }
264
265 /* Build the environment. */
266 CEnv = (const char **) malloc((NumEnv + 1) * sizeof(char*));
267 CEnvBuf = (char*) malloc(EnvSize);
268 Pos = CEnvBuf;
269 for (I = 0; I != NumEnv; ++I) {
270 char *Name = String_val(Field(Field(Env, I), 0)),
271 *Value = String_val(Field(Field(Env, I), 1));
272 int NameLen = strlen(Name),
273 ValueLen = strlen(Value);
274
275 CEnv[I] = Pos;
276 memcpy(Pos, Name, NameLen);
277 Pos += NameLen;
278 *Pos++ = '=';
279 memcpy(Pos, Value, ValueLen);
280 Pos += ValueLen;
281 *Pos++ = '\0';
282 }
283 CEnv[NumEnv] = NULL;
284
285 Result = LLVMRunFunctionAsMain(EE, F, NumArgs, CArgs, CEnv);
286
287 free(CArgs);
288 free(CEnv);
289 free(CEnvBuf);
290
291 CAMLreturn(Val_int(Result));
292 }
293
294 /* llvalue -> ExecutionEngine.t -> unit */
295 CAMLprim value llvm_ee_free_machine_code(LLVMValueRef F,
296 LLVMExecutionEngineRef EE) {
297 LLVMFreeMachineCodeForFunction(EE, F);
298 return Val_unit;
299 }
300
0 (*===-- llvm_executionengine.ml - LLVM Ocaml Interface ----------*- C++ -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file was developed by Gordon Henriksen and is distributed under the
5 * University of Illinois Open Source License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===*)
8
9
10 exception Error of string
11
12 external register_exns: exn -> unit
13 = "llvm_register_ee_exns"
14
15
16 module GenericValue = struct
17 type t
18
19 external of_float: Llvm.lltype -> float -> t
20 = "llvm_genericvalue_of_float"
21 external of_pointer: 'a -> t
22 = "llvm_genericvalue_of_value"
23 external of_int32: Llvm.lltype -> int32 -> t
24 = "llvm_genericvalue_of_int32"
25 external of_int: Llvm.lltype -> int -> t
26 = "llvm_genericvalue_of_int"
27 external of_nativeint: Llvm.lltype -> nativeint -> t
28 = "llvm_genericvalue_of_nativeint"
29 external of_int64: Llvm.lltype -> int64 -> t
30 = "llvm_genericvalue_of_int64"
31
32 external as_float: Llvm.lltype -> t -> float
33 = "llvm_genericvalue_as_float"
34 external as_pointer: t -> 'a
35 = "llvm_genericvalue_as_value"
36 external as_int32: t -> int32
37 = "llvm_genericvalue_as_int32"
38 external as_int: t -> int
39 = "llvm_genericvalue_as_int"
40 external as_nativeint: t -> nativeint
41 = "llvm_genericvalue_as_nativeint"
42 external as_int64: t -> int64
43 = "llvm_genericvalue_as_int64"
44 end
45
46
47 module ExecutionEngine = struct
48 type t
49
50 (* FIXME: Ocaml is not running this setup code unless we use 'val' in the
51 interface, which causes the emission of a stub for each function;
52 using 'external' in the module allows direct calls into
53 ocaml_executionengine.c. This is hardly fatal, but it is unnecessary
54 overhead on top of the two stubs that are already invoked for each
55 call into LLVM. *)
56 let _ = register_exns (Error "")
57
58 external create: Llvm.llmoduleprovider -> t
59 = "llvm_ee_create"
60 external create_interpreter: Llvm.llmoduleprovider -> t
61 = "llvm_ee_create_interpreter"
62 external create_jit: Llvm.llmoduleprovider -> t
63 = "llvm_ee_create_jit"
64 external dispose: t -> unit
65 = "llvm_ee_dispose"
66 external add_module_provider: Llvm.llmoduleprovider -> t -> unit
67 = "llvm_ee_add_mp"
68 external remove_module_provider: Llvm.llmoduleprovider -> t -> Llvm.llmodule
69 = "llvm_ee_remove_mp"
70 external find_function: string -> t -> Llvm.llvalue option
71 = "llvm_ee_find_function"
72 external run_function: Llvm.llvalue -> GenericValue.t array -> t ->
73 GenericValue.t
74 = "llvm_ee_run_function"
75 external run_static_ctors: t -> unit
76 = "llvm_ee_run_static_ctors"
77 external run_static_dtors: t -> unit
78 = "llvm_ee_run_static_dtors"
79 external run_function_as_main: Llvm.llvalue -> string array ->
80 (string * string) array -> t -> int
81 = "llvm_ee_run_function_as_main"
82 external free_machine_code: Llvm.llvalue -> t -> unit
83 = "llvm_ee_free_machine_code"
84
85 (* The following are not bound. Patches are welcome.
86
87 get_target_data: t -> lltargetdata
88 add_global_mapping: llvalue -> llgenericvalue -> t -> unit
89 clear_all_global_mappings: t -> unit
90 update_global_mapping: llvalue -> llgenericvalue -> t -> unit
91 get_pointer_to_global_if_available: llvalue -> t -> llgenericvalue
92 get_pointer_to_global: llvalue -> t -> llgenericvalue
93 get_pointer_to_function: llvalue -> t -> llgenericvalue
94 get_pointer_to_function_or_stub: llvalue -> t -> llgenericvalue
95 get_global_value_at_address: llgenericvalue -> t -> llvalue option
96 store_value_to_memory: llgenericvalue -> llgenericvalue -> lltype -> unit
97 initialize_memory: llvalue -> llgenericvalue -> t -> unit
98 recompile_and_relink_function: llvalue -> t -> llgenericvalue
99 get_or_emit_global_variable: llvalue -> t -> llgenericvalue
100 disable_lazy_compilation: t -> unit
101 lazy_compilation_enabled: t -> bool
102 install_lazy_function_creator: (string -> llgenericvalue) -> t -> unit
103
104 *)
105 end
0 (*===-- llvm_executionengine.mli - LLVM Ocaml Interface ---------*- C++ -*-===*
1 *
2 * The LLVM Compiler Infrastructure
3 *
4 * This file was developed by Gordon Henriksen and is distributed under the
5 * University of Illinois Open Source License. See LICENSE.TXT for details.
6 *
7 *===----------------------------------------------------------------------===
8 *
9 * This interface provides an ocaml API for LLVM execution engine (JIT/
10 * interpreter), the classes in the ExecutionEngine library.
11 *
12 *===----------------------------------------------------------------------===*)
13
14
15 exception Error of string
16
17
18 module GenericValue: sig
19 (** [GenericValue.t] is a boxed union type used to portably pass arguments to
20 and receive values from the execution engine. It supports only a limited
21 selection of types; for more complex argument types, it is necessary to
22 generate a stub function by hand or to pass parameters by reference.
23 See the struct [llvm::GenericValue]. **)
24 type t
25
26 (** [of_float fpty n] boxes the float [n] in a float-valued generic value
27 according to the floating point type [fpty]. See the fields
28 [llvm::GenericValue::DoubleVal] and [llvm::GenericValue::FloatVal]. **)
29 val of_float: Llvm.lltype -> float -> t
30
31 (** [of_pointer v] boxes the pointer value [v] in a generic value. See the
32 field [llvm::GenericValue::PointerVal]. **)
33 val of_pointer: 'a -> t
34
35 (** [of_int32 n w] boxes the int32 [i] in a generic value with the bitwidth
36 [w]. See the field [llvm::GenericValue::IntVal]. **)
37 val of_int32: Llvm.lltype -> int32 -> t
38
39 (** [of_int n w] boxes the int [i] in a generic value with the bitwidth
40 [w]. See the field [llvm::GenericValue::IntVal]. **)
41 val of_int: Llvm.lltype -> int -> t
42
43 (** [of_natint n w] boxes the native int [i] in a generic value with the
44 bitwidth [w]. See the field [llvm::GenericValue::IntVal]. **)
45 val of_nativeint: Llvm.lltype -> nativeint -> t
46
47 (** [of_int64 n w] boxes the int64 [i] in a generic value with the bitwidth
48 [w]. See the field [llvm::GenericValue::IntVal]. **)
49 val of_int64: Llvm.lltype -> int64 -> t
50
51 (** [as_float fpty gv] unboxes the floating point-valued generic value [gv] of
52 floating point type [fpty]. See the fields [llvm::GenericValue::DoubleVal]
53 and [llvm::GenericValue::FloatVal]. **)
54 val as_float: Llvm.lltype -> t -> float
55
56 (** [as_pointer gv] unboxes the pointer-valued generic value [gv]. See the
57 field [llvm::GenericValue::PointerVal]. **)
58 val as_pointer: t -> 'a
59
60 (** [as_int32 gv] unboxes the integer-valued generic value [gv] as an [int32].
61 Is invalid if [gv] has a bitwidth greater than 32 bits. See the field
62 [llvm::GenericValue::IntVal]. **)
63 val as_int32: t -> int32
64
65 (** [as_int gv] unboxes the integer-valued generic value [gv] as an [int].
66 Is invalid if [gv] has a bitwidth greater than the host bit width (but the
67 most significant bit may be lost). See the field
68 [llvm::GenericValue::IntVal]. **)
69 val as_int: t -> int
70
71 (** [as_natint gv] unboxes the integer-valued generic value [gv] as a
72 [nativeint]. Is invalid if [gv] has a bitwidth greater than
73 [nativeint]. See the field [llvm::GenericValue::IntVal]. **)
74 val as_nativeint: t -> nativeint
75
76 (** [as_int64 gv] returns the integer-valued generic value [gv] as an [int64].
77 Is invalid if [gv] has a bitwidth greater than [int64]. See the field
78 [llvm::GenericValue::IntVal]. **)
79 val as_int64: t -> int64
80 end
81
82
83 module ExecutionEngine: sig
84 (** An execution engine is either a JIT compiler or an interpreter, capable of
85 directly loading an LLVM module and executing its functions without first
86 invoking a static compiler and generating a native executable. **)
87 type t
88
89 (** [create mp] creates a new execution engine, taking ownership of the
90 module provider [mp] if successful. Creates a JIT if possible, else falls
91 back to an interpreter. Raises [Error msg] if an error occurrs. The
92 execution engine is not garbage collected and must be destroyed with
93 [dispose ee]. See the function [llvm::ExecutionEngine::create]. **)
94 val create: Llvm.llmoduleprovider -> t
95
96 (** [create_interpreter mp] creates a new interpreter, taking ownership of the
97 module provider [mp] if successful. Raises [Error msg] if an error
98 occurrs. The execution engine is not garbage collected and must be
99 destroyed with [dispose ee].
100 See the function [llvm::ExecutionEngine::create]. **)
101 val create_interpreter: Llvm.llmoduleprovider -> t
102
103 (** [create_jit mp] creates a new JIT (just-in-time compiler), taking
104 ownership of the module provider [mp] if successful. Raises [Error msg] if
105 an error occurrs. The execution engine is not garbage collected and must
106 be destroyed with [dispose ee].
107 See the function [llvm::ExecutionEngine::create]. **)
108 val create_jit: Llvm.llmoduleprovider -> t
109
110 (** [dispose ee] releases the memory used by the execution engine and must be
111 invoked to avoid memory leaks. **)
112 val dispose: t -> unit
113
114 (** [add_module_provider mp ee] adds the module provider [mp] to the execution
115 engine [ee]. **)
116 val add_module_provider: Llvm.llmoduleprovider -> t -> unit
117
118 (** [remove_module_provider mp ee] removes the module provider [mp] from the
119 execution engine [ee], disposing of [mp] and the module referenced by
120 [mp]. Raises [Error msg] if an error occurs. **)
121 val remove_module_provider: Llvm.llmoduleprovider -> t -> Llvm.llmodule
122
123 (** [find_function n ee] finds the function named [n] defined in any of the
124 modules owned by the execution engine [ee]. Returns [None] if the function
125 is not found and [Some f] otherwise. **)
126 val find_function: string -> t -> Llvm.llvalue option
127
128 (** [run_function f args ee] synchronously executes the function [f] with the
129 arguments [args], which must be compatible with the parameter types. **)
130 val run_function: Llvm.llvalue -> GenericValue.t array -> t ->
131 GenericValue.t
132
133 (** [run_static_ctors ee] executes the static constructors of each module in
134 the execution engine [ee]. **)
135 val run_static_ctors: t -> unit
136
137 (** [run_static_dtors ee] executes the static destructors of each module in
138 the execution engine [ee]. **)
139 val run_static_dtors: t -> unit
140
141 (** [run_function_as_main f args env ee] executes the function [f] as a main
142 function, passing it [argv] and [argc] according to the string array
143 [args], and [envp] as specified by the array [env]. Returns the integer
144 return value of the function. **)
145 val run_function_as_main: Llvm.llvalue -> string array ->
146 (string * string) array -> t -> int
147
148 (** [free_machine_code f ee] releases the memory in the execution engine [ee]
149 used to store the machine code for the function [f]. **)
150 val free_machine_code: Llvm.llvalue -> t -> unit
151 end
1717 #include "llvm-c/Core.h"
1818 #include "caml/alloc.h"
1919 #include "caml/custom.h"
20 #include "caml/mlvalues.h"
2120 #include "caml/memory.h"
2221 #include "caml/fail.h"
2322 #include "caml/callback.h"
3635 return Val_unit;
3736 }
3837
39 void llvm_raise(value Prototype, char *Message) {
38 static void llvm_raise(value Prototype, char *Message) {
4039 CAMLparam1(Prototype);
4140 CAMLlocal1(CamlMessage);
4241
4443 LLVMDisposeMessage(Message);
4544
4645 raise_with_arg(Prototype, CamlMessage);
46 abort(); /* NOTREACHED */
4747 CAMLnoreturn;
4848 }
4949
233233
234234 #define Typehandle_val(v) (*(LLVMTypeHandleRef *)(Data_custom_val(v)))
235235
236 void llvm_finalize_handle(value TH) {
236 static void llvm_finalize_handle(value TH) {
237237 LLVMDisposeTypeHandle(Typehandle_val(TH));
238238 }
239239
0 /*===-- llvm-c/ExecutionEngine.h - ExecutionEngine Lib C Iface --*- C++ -*-===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file was developed by Gordon Henriksen and is distributed under the *|
5 |* University of Illinois Open Source License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This header declares the C interface to libLLVMExecutionEngine.o, which *|
10 |* implements various analyses of the LLVM IR. *|
11 |* *|
12 |* Many exotic languages can interoperate with C code but have a harder time *|
13 |* with C++ due to name mangling. So in addition to C, this interface enables *|
14 |* tools written in such languages. *|
15 |* *|
16 \*===----------------------------------------------------------------------===*/
17
18 #ifndef LLVM_C_EXECUTIONENGINE_H
19 #define LLVM_C_EXECUTIONENGINE_H
20
21 #include "llvm-c/Core.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 typedef struct LLVMOpaqueGenericValue *LLVMGenericValueRef;
28 typedef struct LLVMOpaqueExecutionEngine *LLVMExecutionEngineRef;
29
30 /*===-- Operations on generic values --------------------------------------===*/
31
32 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
33 unsigned long long N,
34 int IsSigned);
35
36 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P);
37
38 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef Ty, double N);
39
40 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef);
41
42 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenVal,
43 int IsSigned);
44
45 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal);
46
47 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal);
48
49 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal);
50
51 /*===-- Operations on execution engines -----------------------------------===*/
52
53 int LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
54 LLVMModuleProviderRef MP,
55 char **OutError);
56
57 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
58 LLVMModuleProviderRef MP,
59 char **OutError);
60
61 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
62 LLVMModuleProviderRef MP,
63 char **OutError);
64
65 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE);
66
67 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE);
68
69 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE);
70
71 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
72 unsigned ArgC, const char * const *ArgV,
73 const char * const *EnvP);
74
75 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
76 unsigned NumArgs,
77 LLVMGenericValueRef *Args);
78
79 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F);
80
81 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP);
82
83 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
84 LLVMModuleProviderRef MP,
85 LLVMModuleRef *OutMod, char **OutError);
86
87 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
88 LLVMValueRef *OutFn);
89
90 #ifdef __cplusplus
91 }
92
93 namespace llvm {
94 class GenericValue;
95 class ExecutionEngine;
96
97 #define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref) \
98 inline ty *unwrap(ref P) { \
99 return reinterpret_cast(P); \
100 } \
101 \
102 inline ref wrap(const ty *P) { \
103 return reinterpret_cast(const_cast(P)); \
104 }
105
106 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef )
107 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef)
108
109 #undef DEFINE_SIMPLE_CONVERSION_FUNCTIONS
110 }
111
112 #endif /* defined(__cplusplus) */
113
114 #endif
0 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by Gordon Henriksen and is distributed under the
5 // University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the C bindings for the ExecutionEngine library.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #define DEBUG_TYPE "jit"
14 #include "llvm-c/ExecutionEngine.h"
15 #include "llvm/ExecutionEngine/GenericValue.h"
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
17
18 using namespace llvm;
19
20 /*===-- Operations on generic values --------------------------------------===*/
21
22 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
23 unsigned long long N,
24 int IsSigned) {
25 GenericValue *GenVal = new GenericValue();
26 GenVal->IntVal = APInt(unwrap(Ty)->getBitWidth(), N, IsSigned);
27 return wrap(GenVal);
28 }
29
30 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
31 GenericValue *GenVal = new GenericValue();
32 GenVal->PointerVal = P;
33 return wrap(GenVal);
34 }
35
36 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
37 GenericValue *GenVal = new GenericValue();
38 switch (unwrap(TyRef)->getTypeID()) {
39 case Type::FloatTyID:
40 GenVal->FloatVal = N;
41 break;
42 case Type::DoubleTyID:
43 GenVal->DoubleVal = N;
44 break;
45 default:
46 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
47 break;
48 }
49 return wrap(GenVal);
50 }
51
52 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
53 return unwrap(GenValRef)->IntVal.getBitWidth();
54 }
55
56 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
57 int IsSigned) {
58 GenericValue *GenVal = unwrap(GenValRef);
59 if (IsSigned)
60 return GenVal->IntVal.getSExtValue();
61 else
62 return GenVal->IntVal.getZExtValue();
63 }
64
65 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
66 return unwrap(GenVal)->PointerVal;
67 }
68
69 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
70 switch (unwrap(TyRef)->getTypeID()) {
71 case Type::FloatTyID:
72 return unwrap(GenVal)->FloatVal;
73 case Type::DoubleTyID:
74 return unwrap(GenVal)->DoubleVal;
75 default:
76 assert(0 && "LLVMGenericValueToFloat supports only float and double.");
77 break;
78 }
79 }
80
81 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
82 delete unwrap(GenVal);
83 }
84
85 /*===-- Operations on execution engines -----------------------------------===*/
86
87 int LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
88 LLVMModuleProviderRef MP,
89 char **OutError) {
90 std::string Error;
91 if (ExecutionEngine *EE = ExecutionEngine::create(unwrap(MP), false, &Error)){
92 *OutEE = wrap(EE);
93 return 0;
94 }
95 *OutError = strdup(Error.c_str());
96 return 1;
97 }
98
99 int LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
100 LLVMModuleProviderRef MP,
101 char **OutError) {
102 std::string Error;
103 if (ExecutionEngine *Interp = ExecutionEngine::create(unwrap(MP), &Error)) {
104 *OutInterp = wrap(Interp);
105 return 0;
106 }
107 *OutError = strdup(Error.c_str());
108 return 1;
109 }
110
111 int LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
112 LLVMModuleProviderRef MP,
113 char **OutError) {
114 std::string Error;
115 if (ExecutionEngine *JIT = ExecutionEngine::createJIT(unwrap(MP), &Error)) {
116 *OutJIT = wrap(JIT);
117 return 0;
118 }
119 *OutError = strdup(Error.c_str());
120 return 1;
121 }
122
123 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
124 delete unwrap(EE);
125 }
126
127 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
128 unwrap(EE)->runStaticConstructorsDestructors(false);
129 }
130
131 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
132 unwrap(EE)->runStaticConstructorsDestructors(true);
133 }
134
135 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
136 unsigned ArgC, const char * const *ArgV,
137 const char * const *EnvP) {
138 std::vector ArgVec;
139 for (unsigned I = 0; I != ArgC; ++I)
140 ArgVec.push_back(ArgV[I]);
141
142 return unwrap(EE)->runFunctionAsMain(unwrap(F), ArgVec, EnvP);
143 }
144
145 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
146 unsigned NumArgs,
147 LLVMGenericValueRef *Args) {
148 std::vector ArgVec;
149 ArgVec.reserve(NumArgs);
150 for (unsigned I = 0; I != NumArgs; ++I)
151 ArgVec.push_back(*unwrap(Args[I]));
152
153 GenericValue *Result = new GenericValue();
154 *Result = unwrap(EE)->runFunction(unwrap(F), ArgVec);
155 return wrap(Result);
156 }
157
158 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
159 unwrap(EE)->freeMachineCodeForFunction(unwrap(F));
160 }
161
162 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
163 unwrap(EE)->addModuleProvider(unwrap(MP));
164 }
165
166 int LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
167 LLVMModuleProviderRef MP,
168 LLVMModuleRef *OutMod, char **OutError) {
169 std::string Error;
170 if (Module *Gone = unwrap(EE)->removeModuleProvider(unwrap(MP), &Error)) {
171 *OutMod = wrap(Gone);
172 return 0;
173 }
174 if (OutError)
175 *OutError = strdup(Error.c_str());
176 return 1;
177 }
178
179 int LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
180 LLVMValueRef *OutFn) {
181 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
182 *OutFn = wrap(F);
183 return 0;
184 }
185 return 1;
186 }
0 (* RUN: %ocamlc -warn-error A llvm.cma llvm_executionengine.cma %s -o %t
1 * RUN: ./%t %t.bc
2 *)
3
4 open Llvm
5 open Llvm_executionengine
6
7 (* Note that this takes a moment to link, so it's best to keep the number of
8 individual tests low. *)
9
10 let bomb msg =
11 prerr_endline msg;
12 exit 2
13
14 let define_main_fn m retval =
15 let fn =
16 let str_arr_type = pointer_type (pointer_type i8_type) in
17 define_function "main" (function_type i32_type [| i32_type;
18 str_arr_type;
19 str_arr_type |]) m in
20 let b = builder_at_end (entry_block fn) in
21 ignore (build_ret (const_int i32_type retval) b);
22 fn
23
24 let define_plus m =
25 let fn = define_function "plus" (function_type i32_type [| i32_type;
26 i32_type |]) m in
27 let b = builder_at_end (entry_block fn) in
28 let add = build_add (param fn 0) (param fn 1) "sum" b in
29 ignore (build_ret add b)
30
31 let test_genericvalue () =
32 let tu = (1, 2) in
33 let ptrgv = GenericValue.of_pointer tu in
34 assert (tu = GenericValue.as_pointer ptrgv);
35
36 let fpgv = GenericValue.of_float double_type 2. in
37 assert (2. = GenericValue.as_float double_type fpgv);
38
39 let intgv = GenericValue.of_int i32_type 3 in
40 assert (3 = GenericValue.as_int intgv);
41
42 let i32gv = GenericValue.of_int32 i32_type 4l in
43 assert (4l = GenericValue.as_int32 i32gv);
44
45 let nigv = GenericValue.of_nativeint i32_type 5n in
46 assert (5n = GenericValue.as_nativeint nigv);
47
48 let i64gv = GenericValue.of_int64 i64_type 6L in
49 assert (6L = GenericValue.as_int64 i64gv)
50
51 let test_executionengine () =
52 (* create *)
53 let m = create_module "test_module" in
54 let main = define_main_fn m 42 in
55
56 let m2 = create_module "test_module2" in
57 define_plus m2;
58
59 let ee = ExecutionEngine.create (ModuleProvider.create m) in
60 let mp2 = ModuleProvider.create m2 in
61 ExecutionEngine.add_module_provider mp2 ee;
62
63 (* run_static_ctors *)
64 ExecutionEngine.run_static_ctors ee;
65
66 (* run_function_as_main *)
67 let res = ExecutionEngine.run_function_as_main main [|"test"|] [||] ee in
68 if 42 != res then bomb "main did not return 42";
69
70 (* free_machine_code *)
71 ExecutionEngine.free_machine_code main ee;
72
73 (* find_function *)
74 match ExecutionEngine.find_function "dne" ee with
75 | Some _ -> raise (Failure "find_function 'dne' failed")
76 | None ->
77
78 match ExecutionEngine.find_function "plus" ee with
79 | None -> raise (Failure "find_function 'plus' failed")
80 | Some plus ->
81
82 (* run_function *)
83 let res = ExecutionEngine.run_function plus
84 [| GenericValue.of_int i32_type 2;
85 GenericValue.of_int i32_type 2 |]
86 ee in
87 if 4 != GenericValue.as_int res then bomb "plus did not work";
88
89 (* remove_module_provider *)
90 Llvm.dispose_module (ExecutionEngine.remove_module_provider mp2 ee);
91
92 (* run_static_dtors *)
93 ExecutionEngine.run_static_dtors ee;
94
95 (* dispose *)
96 ExecutionEngine.dispose ee
97
98 let _ =
99 test_genericvalue ();
100 test_executionengine ()