Sylvestre Ledru | 493cd8c | 2013-11-01 00:26:01 +0000 | [diff] [blame] | 1 | /*===-- target_ocaml.c - LLVM OCaml Glue ------------------------*- C++ -*-===*\ |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 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 | |* *| |
Sylvestre Ledru | 493cd8c | 2013-11-01 00:26:01 +0000 | [diff] [blame] | 10 | |* This file glues LLVM's OCaml interface to its C interface. These functions *| |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 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 | |
| 18 | #include "llvm-c/Target.h" |
| 19 | #include "caml/alloc.h" |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 20 | #include "caml/custom.h" |
| 21 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 22 | #define DataLayout_val(v) (*(LLVMTargetDataRef *)(Data_custom_val(v))) |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 23 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 24 | static void llvm_finalize_data_layout(value DataLayout) { |
| 25 | LLVMDisposeTargetData(DataLayout_val(DataLayout)); |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 28 | static struct custom_operations llvm_data_layout_ops = { |
| 29 | (char *) "LLVMDataLayout", |
| 30 | llvm_finalize_data_layout, |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 31 | custom_compare_default, |
| 32 | custom_hash_default, |
| 33 | custom_serialize_default, |
| 34 | custom_deserialize_default |
| 35 | #ifdef custom_compare_ext_default |
| 36 | , custom_compare_ext_default |
| 37 | #endif |
| 38 | }; |
| 39 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 40 | value llvm_alloc_data_layout(LLVMTargetDataRef DataLayout) { |
| 41 | value V = alloc_custom(&llvm_data_layout_ops, sizeof(LLVMTargetDataRef), 0, 1); |
| 42 | DataLayout_val(V) = DataLayout; |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 43 | return V; |
| 44 | } |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 45 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 46 | /* string -> DataLayout.t */ |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 47 | CAMLprim value llvm_datalayout_of_string(value StringRep) { |
| 48 | return llvm_alloc_data_layout(LLVMCreateTargetData(String_val(StringRep))); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 51 | /* DataLayout.t -> string */ |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 52 | CAMLprim value llvm_datalayout_as_string(value TD) { |
| 53 | char *StringRep = LLVMCopyStringRepOfTargetData(DataLayout_val(TD)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 54 | value Copy = copy_string(StringRep); |
| 55 | LLVMDisposeMessage(StringRep); |
| 56 | return Copy; |
| 57 | } |
| 58 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 59 | /* [<Llvm.PassManager.any] Llvm.PassManager.t -> DataLayout.t -> unit */ |
| 60 | CAMLprim value llvm_datalayout_add_to_pass_manager(LLVMPassManagerRef PM, |
| 61 | value DL) { |
| 62 | LLVMAddTargetData(DataLayout_val(DL), PM); |
| 63 | return Val_unit; |
| 64 | } |
| 65 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 66 | /* DataLayout.t -> Endian.t */ |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 67 | CAMLprim value llvm_datalayout_byte_order(value DL) { |
| 68 | return Val_int(LLVMByteOrder(DataLayout_val(DL))); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Micah Villmow | 9cfc13d | 2012-10-08 16:39:34 +0000 | [diff] [blame] | 71 | /* DataLayout.t -> int */ |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 72 | CAMLprim value llvm_datalayout_pointer_size(value DL) { |
| 73 | return Val_int(LLVMPointerSize(DataLayout_val(DL))); |
Peter Zotov | d52cf17 | 2013-11-11 14:47:11 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 76 | /* Llvm.llcontext -> DataLayout.t -> Llvm.lltype */ |
| 77 | CAMLprim LLVMTypeRef llvm_datalayout_intptr_type(LLVMContextRef C, value DL) { |
| 78 | return LLVMIntPtrTypeInContext(C, DataLayout_val(DL));; |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 81 | /* int -> DataLayout.t -> int */ |
| 82 | CAMLprim value llvm_datalayout_qualified_pointer_size(value AS, value DL) { |
| 83 | return Val_int(LLVMPointerSizeForAS(DataLayout_val(DL), Int_val(AS))); |
Peter Zotov | 18636a8 | 2013-11-11 14:47:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 86 | /* Llvm.llcontext -> int -> DataLayout.t -> Llvm.lltype */ |
| 87 | CAMLprim LLVMTypeRef llvm_datalayout_qualified_intptr_type(LLVMContextRef C, |
| 88 | value AS, |
| 89 | value DL) { |
| 90 | return LLVMIntPtrTypeForASInContext(C, DataLayout_val(DL), Int_val(AS)); |
Peter Zotov | 18636a8 | 2013-11-11 14:47:28 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 93 | /* Llvm.lltype -> DataLayout.t -> Int64.t */ |
| 94 | CAMLprim value llvm_datalayout_size_in_bits(LLVMTypeRef Ty, value DL) { |
| 95 | return caml_copy_int64(LLVMSizeOfTypeInBits(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 98 | /* Llvm.lltype -> DataLayout.t -> Int64.t */ |
| 99 | CAMLprim value llvm_datalayout_store_size(LLVMTypeRef Ty, value DL) { |
| 100 | return caml_copy_int64(LLVMStoreSizeOfType(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 103 | /* Llvm.lltype -> DataLayout.t -> Int64.t */ |
| 104 | CAMLprim value llvm_datalayout_abi_size(LLVMTypeRef Ty, value DL) { |
| 105 | return caml_copy_int64(LLVMABISizeOfType(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 108 | /* Llvm.lltype -> DataLayout.t -> int */ |
| 109 | CAMLprim value llvm_datalayout_abi_align(LLVMTypeRef Ty, value DL) { |
| 110 | return Val_int(LLVMABIAlignmentOfType(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 113 | /* Llvm.lltype -> DataLayout.t -> int */ |
| 114 | CAMLprim value llvm_datalayout_stack_align(LLVMTypeRef Ty, value DL) { |
| 115 | return Val_int(LLVMCallFrameAlignmentOfType(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 118 | /* Llvm.lltype -> DataLayout.t -> int */ |
| 119 | CAMLprim value llvm_datalayout_preferred_align(LLVMTypeRef Ty, value DL) { |
| 120 | return Val_int(LLVMPreferredAlignmentOfType(DataLayout_val(DL), Ty)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 123 | /* Llvm.llvalue -> DataLayout.t -> int */ |
| 124 | CAMLprim value llvm_datalayout_preferred_align_of_global(LLVMValueRef GlobalVar, |
| 125 | value DL) { |
| 126 | return Val_int(LLVMPreferredAlignmentOfGlobal(DataLayout_val(DL), GlobalVar)); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 129 | /* Llvm.lltype -> Int64.t -> DataLayout.t -> int */ |
| 130 | CAMLprim value llvm_datalayout_element_at_offset(LLVMTypeRef Ty, value Offset, |
| 131 | value DL) { |
| 132 | return Val_int(LLVMElementAtOffset(DataLayout_val(DL), Ty, |
| 133 | Int64_val(Offset))); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Peter Zotov | 8a1a3bf | 2013-11-15 02:51:44 +0000 | [diff] [blame^] | 136 | /* Llvm.lltype -> int -> DataLayout.t -> Int64.t */ |
| 137 | CAMLprim value llvm_datalayout_offset_of_element(LLVMTypeRef Ty, value Index, |
| 138 | value DL) { |
| 139 | return caml_copy_int64(LLVMOffsetOfElement(DataLayout_val(DL), Ty, |
| 140 | Int_val(Index))); |
Gordon Henriksen | ab4b7d3 | 2008-03-16 20:08:03 +0000 | [diff] [blame] | 141 | } |