1 /*===-- analysis_ocaml.c - LLVM Ocaml Glue ----------------------*- C++ -*-===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
10 |* This file glues LLVM's ocaml interface to its C interface. These functions *|
11 |* are by and large transparent wrappers to the corresponding C functions. *|
13 |* Note that these functions intentionally take liberties with the CAMLparamX *|
14 |* macros, since most of the parameters are not GC heap objects. *|
16 \*===----------------------------------------------------------------------===*/
18 #include "llvm-c/ExecutionEngine.h"
19 #include "caml/alloc.h"
20 #include "caml/custom.h"
21 #include "caml/fail.h"
22 #include "caml/memory.h"
27 /* Can't use the recommended caml_named_value mechanism for backwards
28 compatibility reasons. This is largely equivalent. */
29 static value llvm_ee_error_exn;
31 CAMLprim value llvm_register_ee_exns(value Error) {
32 llvm_ee_error_exn = Field(Error, 0);
33 register_global_root(&llvm_ee_error_exn);
37 static void llvm_raise(value Prototype, char *Message) {
38 CAMLparam1(Prototype);
39 CAMLlocal1(CamlMessage);
41 CamlMessage = copy_string(Message);
42 LLVMDisposeMessage(Message);
44 raise_with_arg(Prototype, CamlMessage);
45 abort(); /* NOTREACHED */
50 /*--... Operations on generic values .......................................--*/
52 #define Genericvalue_val(v) (*(LLVMGenericValueRef *)(Data_custom_val(v)))
54 static void llvm_finalize_generic_value(value GenVal) {
55 LLVMDisposeGenericValue(Genericvalue_val(GenVal));
58 static struct custom_operations generic_value_ops = {
59 (char *) "LLVMGenericValue",
60 llvm_finalize_generic_value,
61 custom_compare_default,
63 custom_serialize_default,
64 custom_deserialize_default
67 static value alloc_generic_value(LLVMGenericValueRef Ref) {
68 value Val = alloc_custom(&generic_value_ops, sizeof(LLVMGenericValueRef), 0, 1);
69 Genericvalue_val(Val) = Ref;
73 /* Llvm.lltype -> float -> t */
74 CAMLprim value llvm_genericvalue_of_float(LLVMTypeRef Ty, value N) {
76 CAMLreturn(alloc_generic_value(
77 LLVMCreateGenericValueOfFloat(Ty, Double_val(N))));
81 CAMLprim value llvm_genericvalue_of_value(value V) {
83 CAMLreturn(alloc_generic_value(LLVMCreateGenericValueOfPointer(Op_val(V))));
86 /* Llvm.lltype -> int -> t */
87 CAMLprim value llvm_genericvalue_of_int(LLVMTypeRef Ty, value Int) {
88 return alloc_generic_value(LLVMCreateGenericValueOfInt(Ty, Int_val(Int), 1));
91 /* Llvm.lltype -> int32 -> t */
92 CAMLprim value llvm_genericvalue_of_int32(LLVMTypeRef Ty, value Int32) {
94 CAMLreturn(alloc_generic_value(
95 LLVMCreateGenericValueOfInt(Ty, Int32_val(Int32), 1)));
98 /* Llvm.lltype -> nativeint -> t */
99 CAMLprim value llvm_genericvalue_of_nativeint(LLVMTypeRef Ty, value NatInt) {
101 CAMLreturn(alloc_generic_value(
102 LLVMCreateGenericValueOfInt(Ty, Nativeint_val(NatInt), 1)));
105 /* Llvm.lltype -> int64 -> t */
106 CAMLprim value llvm_genericvalue_of_int64(LLVMTypeRef Ty, value Int64) {
108 CAMLreturn(alloc_generic_value(
109 LLVMCreateGenericValueOfInt(Ty, Int64_val(Int64), 1)));
112 /* Llvm.lltype -> t -> float */
113 CAMLprim value llvm_genericvalue_as_float(LLVMTypeRef Ty, value GenVal) {
115 CAMLreturn(copy_double(
116 LLVMGenericValueToFloat(Ty, Genericvalue_val(GenVal))));
120 CAMLprim value llvm_genericvalue_as_value(value GenVal) {
121 return Val_op(LLVMGenericValueToPointer(Genericvalue_val(GenVal)));
125 CAMLprim value llvm_genericvalue_as_int(value GenVal) {
126 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 8 * sizeof(value)
127 && "Generic value too wide to treat as an int!");
128 return Val_int(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1));
132 CAMLprim value llvm_genericvalue_as_int32(value GenVal) {
134 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 32
135 && "Generic value too wide to treat as an int32!");
136 CAMLreturn(copy_int32(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1)));
140 CAMLprim value llvm_genericvalue_as_int64(value GenVal) {
142 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 64
143 && "Generic value too wide to treat as an int64!");
144 CAMLreturn(copy_int64(LLVMGenericValueToInt(Genericvalue_val(GenVal), 1)));
148 CAMLprim value llvm_genericvalue_as_nativeint(value GenVal) {
150 assert(LLVMGenericValueIntWidth(Genericvalue_val(GenVal)) <= 8 * sizeof(value)
151 && "Generic value too wide to treat as a nativeint!");
152 CAMLreturn(copy_nativeint(LLVMGenericValueToInt(Genericvalue_val(GenVal),1)));
156 /*--... Operations on execution engines ....................................--*/
158 /* llmoduleprovider -> ExecutionEngine.t */
159 CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleProviderRef MP) {
160 LLVMExecutionEngineRef Interp;
162 if (LLVMCreateExecutionEngine(&Interp, MP, &Error))
163 llvm_raise(llvm_ee_error_exn, Error);
167 /* llmoduleprovider -> ExecutionEngine.t */
168 CAMLprim LLVMExecutionEngineRef
169 llvm_ee_create_interpreter(LLVMModuleProviderRef MP) {
170 LLVMExecutionEngineRef Interp;
172 if (LLVMCreateInterpreter(&Interp, MP, &Error))
173 llvm_raise(llvm_ee_error_exn, Error);
177 /* llmoduleprovider -> ExecutionEngine.t */
178 CAMLprim LLVMExecutionEngineRef
179 llvm_ee_create_jit(LLVMModuleProviderRef MP) {
180 LLVMExecutionEngineRef JIT;
182 if (LLVMCreateJITCompiler(&JIT, MP, &Error))
183 llvm_raise(llvm_ee_error_exn, Error);
187 /* ExecutionEngine.t -> unit */
188 CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
189 LLVMDisposeExecutionEngine(EE);
193 /* llmoduleprovider -> ExecutionEngine.t -> unit */
194 CAMLprim value llvm_ee_add_mp(LLVMModuleProviderRef MP,
195 LLVMExecutionEngineRef EE) {
196 LLVMAddModuleProvider(EE, MP);
200 /* llmoduleprovider -> ExecutionEngine.t -> llmodule */
201 CAMLprim LLVMModuleRef llvm_ee_remove_mp(LLVMModuleProviderRef MP,
202 LLVMExecutionEngineRef EE) {
203 LLVMModuleRef RemovedModule;
205 if (LLVMRemoveModuleProvider(EE, MP, &RemovedModule, &Error))
206 llvm_raise(llvm_ee_error_exn, Error);
207 return RemovedModule;
210 /* string -> ExecutionEngine.t -> llvalue option */
211 CAMLprim value llvm_ee_find_function(value Name, LLVMExecutionEngineRef EE) {
215 if (LLVMFindFunction(EE, String_val(Name), &Found))
216 CAMLreturn(Val_unit);
217 Option = alloc(1, 1);
218 Field(Option, 0) = Val_op(Found);
222 /* llvalue -> GenericValue.t array -> ExecutionEngine.t -> GenericValue.t */
223 CAMLprim value llvm_ee_run_function(LLVMValueRef F, value Args,
224 LLVMExecutionEngineRef EE) {
226 LLVMGenericValueRef Result, *GVArgs;
229 NumArgs = Wosize_val(Args);
230 GVArgs = (LLVMGenericValueRef*) malloc(NumArgs * sizeof(LLVMGenericValueRef));
231 for (I = 0; I != NumArgs; ++I)
232 GVArgs[I] = Genericvalue_val(Field(Args, I));
234 Result = LLVMRunFunction(EE, F, NumArgs, GVArgs);
237 return alloc_generic_value(Result);
240 /* ExecutionEngine.t -> unit */
241 CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
242 LLVMRunStaticConstructors(EE);
246 /* ExecutionEngine.t -> unit */
247 CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
248 LLVMRunStaticDestructors(EE);
252 /* llvalue -> string array -> (string * string) array -> ExecutionEngine.t ->
254 CAMLprim value llvm_ee_run_function_as_main(LLVMValueRef F,
255 value Args, value Env,
256 LLVMExecutionEngineRef EE) {
257 CAMLparam2(Args, Env);
258 int I, NumArgs, NumEnv, EnvSize, Result;
259 const char **CArgs, **CEnv;
262 NumArgs = Wosize_val(Args);
263 NumEnv = Wosize_val(Env);
265 /* Build the environment. */
266 CArgs = (const char **) malloc(NumArgs * sizeof(char*));
267 for (I = 0; I != NumArgs; ++I)
268 CArgs[I] = String_val(Field(Args, I));
270 /* Compute the size of the environment string buffer. */
271 for (I = 0, EnvSize = 0; I != NumEnv; ++I) {
272 EnvSize += strlen(String_val(Field(Field(Env, I), 0))) + 1;
273 EnvSize += strlen(String_val(Field(Field(Env, I), 1))) + 1;
276 /* Build the environment. */
277 CEnv = (const char **) malloc((NumEnv + 1) * sizeof(char*));
278 CEnvBuf = (char*) malloc(EnvSize);
280 for (I = 0; I != NumEnv; ++I) {
281 char *Name = String_val(Field(Field(Env, I), 0)),
282 *Value = String_val(Field(Field(Env, I), 1));
283 int NameLen = strlen(Name),
284 ValueLen = strlen(Value);
287 memcpy(Pos, Name, NameLen);
290 memcpy(Pos, Value, ValueLen);
296 Result = LLVMRunFunctionAsMain(EE, F, NumArgs, CArgs, CEnv);
302 CAMLreturn(Val_int(Result));
305 /* llvalue -> ExecutionEngine.t -> unit */
306 CAMLprim value llvm_ee_free_machine_code(LLVMValueRef F,
307 LLVMExecutionEngineRef EE) {
308 LLVMFreeMachineCodeForFunction(EE, F);