executionengine_ocaml.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
  2. |* *|
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
  4. |* Exceptions. *|
  5. |* See https://llvm.org/LICENSE.txt for license information. *|
  6. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
  7. |* *|
  8. |*===----------------------------------------------------------------------===*|
  9. |* *|
  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. *|
  12. |* *|
  13. |* Note that these functions intentionally take liberties with the CAMLparamX *|
  14. |* macros, since most of the parameters are not GC heap objects. *|
  15. |* *|
  16. \*===----------------------------------------------------------------------===*/
  17. #include <string.h>
  18. #include <assert.h>
  19. #include "llvm-c/Core.h"
  20. #include "llvm-c/ExecutionEngine.h"
  21. #include "llvm-c/Target.h"
  22. #include "caml/alloc.h"
  23. #include "caml/custom.h"
  24. #include "caml/fail.h"
  25. #include "caml/memory.h"
  26. #include "caml/callback.h"
  27. void llvm_raise(value Prototype, char *Message);
  28. /* unit -> bool */
  29. CAMLprim value llvm_ee_initialize(value Unit) {
  30. LLVMLinkInMCJIT();
  31. return Val_bool(!LLVMInitializeNativeTarget() &&
  32. !LLVMInitializeNativeAsmParser() &&
  33. !LLVMInitializeNativeAsmPrinter());
  34. }
  35. /* llmodule -> llcompileroption -> ExecutionEngine.t */
  36. CAMLprim LLVMExecutionEngineRef llvm_ee_create(value OptRecordOpt, LLVMModuleRef M) {
  37. value OptRecord;
  38. LLVMExecutionEngineRef MCJIT;
  39. char *Error;
  40. struct LLVMMCJITCompilerOptions Options;
  41. LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
  42. if (OptRecordOpt != Val_int(0)) {
  43. OptRecord = Field(OptRecordOpt, 0);
  44. Options.OptLevel = Int_val(Field(OptRecord, 0));
  45. Options.CodeModel = Int_val(Field(OptRecord, 1));
  46. Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
  47. Options.EnableFastISel = Int_val(Field(OptRecord, 3));
  48. Options.MCJMM = NULL;
  49. }
  50. if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
  51. sizeof(Options), &Error))
  52. llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
  53. return MCJIT;
  54. }
  55. /* ExecutionEngine.t -> unit */
  56. CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
  57. LLVMDisposeExecutionEngine(EE);
  58. return Val_unit;
  59. }
  60. /* llmodule -> ExecutionEngine.t -> unit */
  61. CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
  62. LLVMAddModule(EE, M);
  63. return Val_unit;
  64. }
  65. /* llmodule -> ExecutionEngine.t -> llmodule */
  66. CAMLprim value llvm_ee_remove_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
  67. LLVMModuleRef RemovedModule;
  68. char *Error;
  69. if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
  70. llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
  71. return Val_unit;
  72. }
  73. /* ExecutionEngine.t -> unit */
  74. CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
  75. LLVMRunStaticConstructors(EE);
  76. return Val_unit;
  77. }
  78. /* ExecutionEngine.t -> unit */
  79. CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
  80. LLVMRunStaticDestructors(EE);
  81. return Val_unit;
  82. }
  83. extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
  84. /* ExecutionEngine.t -> Llvm_target.DataLayout.t */
  85. CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
  86. value DataLayout;
  87. LLVMTargetDataRef OrigDataLayout;
  88. char* TargetDataCStr;
  89. OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
  90. TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
  91. DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
  92. LLVMDisposeMessage(TargetDataCStr);
  93. return DataLayout;
  94. }
  95. /* Llvm.llvalue -> int64 -> llexecutionengine -> unit */
  96. CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr,
  97. LLVMExecutionEngineRef EE) {
  98. LLVMAddGlobalMapping(EE, Global, (void*) (Int64_val(Ptr)));
  99. return Val_unit;
  100. }
  101. CAMLprim value llvm_ee_get_global_value_address(value Name,
  102. LLVMExecutionEngineRef EE) {
  103. return caml_copy_int64((int64_t) LLVMGetGlobalValueAddress(EE, String_val(Name)));
  104. }
  105. CAMLprim value llvm_ee_get_function_address(value Name,
  106. LLVMExecutionEngineRef EE) {
  107. return caml_copy_int64((int64_t) LLVMGetFunctionAddress(EE, String_val(Name)));
  108. }