xref: /aosp_15_r20/external/llvm/bindings/ocaml/executionengine/executionengine_ocaml.c (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker /*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
2*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
3*9880d681SAndroid Build Coastguard Worker |*                     The LLVM Compiler Infrastructure                       *|
4*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
5*9880d681SAndroid Build Coastguard Worker |* This file is distributed under the University of Illinois Open Source      *|
6*9880d681SAndroid Build Coastguard Worker |* License. See LICENSE.TXT for details.                                      *|
7*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
8*9880d681SAndroid Build Coastguard Worker |*===----------------------------------------------------------------------===*|
9*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
10*9880d681SAndroid Build Coastguard Worker |* This file glues LLVM's OCaml interface to its C interface. These functions *|
11*9880d681SAndroid Build Coastguard Worker |* are by and large transparent wrappers to the corresponding C functions.    *|
12*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
13*9880d681SAndroid Build Coastguard Worker |* Note that these functions intentionally take liberties with the CAMLparamX *|
14*9880d681SAndroid Build Coastguard Worker |* macros, since most of the parameters are not GC heap objects.              *|
15*9880d681SAndroid Build Coastguard Worker |*                                                                            *|
16*9880d681SAndroid Build Coastguard Worker \*===----------------------------------------------------------------------===*/
17*9880d681SAndroid Build Coastguard Worker 
18*9880d681SAndroid Build Coastguard Worker #include <string.h>
19*9880d681SAndroid Build Coastguard Worker #include <assert.h>
20*9880d681SAndroid Build Coastguard Worker #include "llvm-c/Core.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm-c/ExecutionEngine.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm-c/Target.h"
23*9880d681SAndroid Build Coastguard Worker #include "caml/alloc.h"
24*9880d681SAndroid Build Coastguard Worker #include "caml/custom.h"
25*9880d681SAndroid Build Coastguard Worker #include "caml/fail.h"
26*9880d681SAndroid Build Coastguard Worker #include "caml/memory.h"
27*9880d681SAndroid Build Coastguard Worker #include "caml/callback.h"
28*9880d681SAndroid Build Coastguard Worker 
29*9880d681SAndroid Build Coastguard Worker void llvm_raise(value Prototype, char *Message);
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker /* unit -> bool */
llvm_ee_initialize(value Unit)32*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_initialize(value Unit) {
33*9880d681SAndroid Build Coastguard Worker   LLVMLinkInMCJIT();
34*9880d681SAndroid Build Coastguard Worker 
35*9880d681SAndroid Build Coastguard Worker   return Val_bool(!LLVMInitializeNativeTarget() &&
36*9880d681SAndroid Build Coastguard Worker                   !LLVMInitializeNativeAsmParser() &&
37*9880d681SAndroid Build Coastguard Worker                   !LLVMInitializeNativeAsmPrinter());
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker /* llmodule -> llcompileroption -> ExecutionEngine.t */
llvm_ee_create(value OptRecordOpt,LLVMModuleRef M)41*9880d681SAndroid Build Coastguard Worker CAMLprim LLVMExecutionEngineRef llvm_ee_create(value OptRecordOpt, LLVMModuleRef M) {
42*9880d681SAndroid Build Coastguard Worker   value OptRecord;
43*9880d681SAndroid Build Coastguard Worker   LLVMExecutionEngineRef MCJIT;
44*9880d681SAndroid Build Coastguard Worker   char *Error;
45*9880d681SAndroid Build Coastguard Worker   struct LLVMMCJITCompilerOptions Options;
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker   LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
48*9880d681SAndroid Build Coastguard Worker   if (OptRecordOpt != Val_int(0)) {
49*9880d681SAndroid Build Coastguard Worker     OptRecord = Field(OptRecordOpt, 0);
50*9880d681SAndroid Build Coastguard Worker     Options.OptLevel = Int_val(Field(OptRecord, 0));
51*9880d681SAndroid Build Coastguard Worker     Options.CodeModel = Int_val(Field(OptRecord, 1));
52*9880d681SAndroid Build Coastguard Worker     Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
53*9880d681SAndroid Build Coastguard Worker     Options.EnableFastISel = Int_val(Field(OptRecord, 3));
54*9880d681SAndroid Build Coastguard Worker     Options.MCJMM = NULL;
55*9880d681SAndroid Build Coastguard Worker   }
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker   if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
58*9880d681SAndroid Build Coastguard Worker                                       sizeof(Options), &Error))
59*9880d681SAndroid Build Coastguard Worker     llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
60*9880d681SAndroid Build Coastguard Worker   return MCJIT;
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker 
63*9880d681SAndroid Build Coastguard Worker /* ExecutionEngine.t -> unit */
llvm_ee_dispose(LLVMExecutionEngineRef EE)64*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
65*9880d681SAndroid Build Coastguard Worker   LLVMDisposeExecutionEngine(EE);
66*9880d681SAndroid Build Coastguard Worker   return Val_unit;
67*9880d681SAndroid Build Coastguard Worker }
68*9880d681SAndroid Build Coastguard Worker 
69*9880d681SAndroid Build Coastguard Worker /* llmodule -> ExecutionEngine.t -> unit */
llvm_ee_add_module(LLVMModuleRef M,LLVMExecutionEngineRef EE)70*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
71*9880d681SAndroid Build Coastguard Worker   LLVMAddModule(EE, M);
72*9880d681SAndroid Build Coastguard Worker   return Val_unit;
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker /* llmodule -> ExecutionEngine.t -> llmodule */
llvm_ee_remove_module(LLVMModuleRef M,LLVMExecutionEngineRef EE)76*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_remove_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
77*9880d681SAndroid Build Coastguard Worker   LLVMModuleRef RemovedModule;
78*9880d681SAndroid Build Coastguard Worker   char *Error;
79*9880d681SAndroid Build Coastguard Worker   if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
80*9880d681SAndroid Build Coastguard Worker     llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
81*9880d681SAndroid Build Coastguard Worker   return Val_unit;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker /* ExecutionEngine.t -> unit */
llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE)85*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
86*9880d681SAndroid Build Coastguard Worker   LLVMRunStaticConstructors(EE);
87*9880d681SAndroid Build Coastguard Worker   return Val_unit;
88*9880d681SAndroid Build Coastguard Worker }
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker /* ExecutionEngine.t -> unit */
llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE)91*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
92*9880d681SAndroid Build Coastguard Worker   LLVMRunStaticDestructors(EE);
93*9880d681SAndroid Build Coastguard Worker   return Val_unit;
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker 
96*9880d681SAndroid Build Coastguard Worker extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
97*9880d681SAndroid Build Coastguard Worker 
98*9880d681SAndroid Build Coastguard Worker /* ExecutionEngine.t -> Llvm_target.DataLayout.t */
llvm_ee_get_data_layout(LLVMExecutionEngineRef EE)99*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
100*9880d681SAndroid Build Coastguard Worker   value DataLayout;
101*9880d681SAndroid Build Coastguard Worker   LLVMTargetDataRef OrigDataLayout;
102*9880d681SAndroid Build Coastguard Worker   char* TargetDataCStr;
103*9880d681SAndroid Build Coastguard Worker 
104*9880d681SAndroid Build Coastguard Worker   OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
105*9880d681SAndroid Build Coastguard Worker   TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
106*9880d681SAndroid Build Coastguard Worker   DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
107*9880d681SAndroid Build Coastguard Worker   LLVMDisposeMessage(TargetDataCStr);
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   return DataLayout;
110*9880d681SAndroid Build Coastguard Worker }
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker /* Llvm.llvalue -> int64 -> llexecutionengine -> unit */
llvm_ee_add_global_mapping(LLVMValueRef Global,value Ptr,LLVMExecutionEngineRef EE)113*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr,
114*9880d681SAndroid Build Coastguard Worker                                           LLVMExecutionEngineRef EE) {
115*9880d681SAndroid Build Coastguard Worker   LLVMAddGlobalMapping(EE, Global, (void*) (Int64_val(Ptr)));
116*9880d681SAndroid Build Coastguard Worker   return Val_unit;
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker 
llvm_ee_get_global_value_address(value Name,LLVMExecutionEngineRef EE)119*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_get_global_value_address(value Name,
120*9880d681SAndroid Build Coastguard Worker 						LLVMExecutionEngineRef EE) {
121*9880d681SAndroid Build Coastguard Worker   return caml_copy_int64((int64_t) LLVMGetGlobalValueAddress(EE, String_val(Name)));
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker 
llvm_ee_get_function_address(value Name,LLVMExecutionEngineRef EE)124*9880d681SAndroid Build Coastguard Worker CAMLprim value llvm_ee_get_function_address(value Name,
125*9880d681SAndroid Build Coastguard Worker 					    LLVMExecutionEngineRef EE) {
126*9880d681SAndroid Build Coastguard Worker   return caml_copy_int64((int64_t) LLVMGetFunctionAddress(EE, String_val(Name)));
127*9880d681SAndroid Build Coastguard Worker }
128