executionengine_ocaml.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
  2. |* *|
  3. |* The LLVM Compiler Infrastructure *|
  4. |* *|
  5. |* This file is distributed under the University of Illinois Open Source *|
  6. |* License. See LICENSE.TXT for details. *|
  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/ExecutionEngine.h"
  20. #include "llvm-c/Target.h"
  21. #include "caml/alloc.h"
  22. #include "caml/custom.h"
  23. #include "caml/fail.h"
  24. #include "caml/memory.h"
  25. #include "caml/callback.h"
  26. void llvm_raise(value Prototype, char *Message);
  27. /* unit -> bool */
  28. CAMLprim value llvm_ee_initialize(value Unit) {
  29. LLVMLinkInMCJIT();
  30. return Val_bool(!LLVMInitializeNativeTarget() &&
  31. !LLVMInitializeNativeAsmParser() &&
  32. !LLVMInitializeNativeAsmPrinter());
  33. }
  34. /* llmodule -> llcompileroption -> ExecutionEngine.t */
  35. CAMLprim LLVMExecutionEngineRef llvm_ee_create(value OptRecordOpt, LLVMModuleRef M) {
  36. value OptRecord;
  37. LLVMExecutionEngineRef MCJIT;
  38. char *Error;
  39. struct LLVMMCJITCompilerOptions Options;
  40. LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
  41. if (OptRecordOpt != Val_int(0)) {
  42. OptRecord = Field(OptRecordOpt, 0);
  43. Options.OptLevel = Int_val(Field(OptRecord, 0));
  44. Options.CodeModel = Int_val(Field(OptRecord, 1));
  45. Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
  46. Options.EnableFastISel = Int_val(Field(OptRecord, 3));
  47. Options.MCJMM = NULL;
  48. }
  49. if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
  50. sizeof(Options), &Error))
  51. llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
  52. return MCJIT;
  53. }
  54. /* ExecutionEngine.t -> unit */
  55. CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
  56. LLVMDisposeExecutionEngine(EE);
  57. return Val_unit;
  58. }
  59. /* llmodule -> ExecutionEngine.t -> unit */
  60. CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
  61. LLVMAddModule(EE, M);
  62. return Val_unit;
  63. }
  64. /* llmodule -> ExecutionEngine.t -> llmodule */
  65. CAMLprim value llvm_ee_remove_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
  66. LLVMModuleRef RemovedModule;
  67. char *Error;
  68. if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
  69. llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
  70. return Val_unit;
  71. }
  72. /* ExecutionEngine.t -> unit */
  73. CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
  74. LLVMRunStaticConstructors(EE);
  75. return Val_unit;
  76. }
  77. /* ExecutionEngine.t -> unit */
  78. CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
  79. LLVMRunStaticDestructors(EE);
  80. return Val_unit;
  81. }
  82. extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
  83. /* ExecutionEngine.t -> Llvm_target.DataLayout.t */
  84. CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
  85. value DataLayout;
  86. LLVMTargetDataRef OrigDataLayout;
  87. char* TargetDataCStr;
  88. OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
  89. TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
  90. DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
  91. LLVMDisposeMessage(TargetDataCStr);
  92. return DataLayout;
  93. }
  94. /* Llvm.llvalue -> int64 -> llexecutionengine -> unit */
  95. CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr,
  96. LLVMExecutionEngineRef EE) {
  97. LLVMAddGlobalMapping(EE, Global, (void*) (Int64_val(Ptr)));
  98. return Val_unit;
  99. }
  100. /* Llvm.llvalue -> llexecutionengine -> int64 */
  101. CAMLprim value llvm_ee_get_pointer_to_global(LLVMValueRef Global,
  102. LLVMExecutionEngineRef EE) {
  103. return caml_copy_int64((int64_t) LLVMGetPointerToGlobal(EE, Global));
  104. }