[PATCH] D52208: [OCaml] Add OCaml APIs for LLVMGetModuleDataLayout and LLVMABISizeOfType

Josh Berdine via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 17 18:26:06 PDT 2018


jberdine created this revision.
jberdine added a reviewer: whitequark.
Herald added a subscriber: llvm-commits.

This patch adds a shim around LLVMABISizeOfType to the OCaml API. To
make this usable, a shim around LLVMGetModuleDataLayout and a minimal
implementation of the DataLayout type is also added.


Repository:
  rL LLVM

https://reviews.llvm.org/D52208

Files:
  bindings/ocaml/llvm/llvm.ml
  bindings/ocaml/llvm/llvm.mli
  bindings/ocaml/llvm/llvm_ocaml.c


Index: bindings/ocaml/llvm/llvm_ocaml.c
===================================================================
--- bindings/ocaml/llvm/llvm_ocaml.c
+++ bindings/ocaml/llvm/llvm_ocaml.c
@@ -20,6 +20,7 @@
 #include <string.h>
 #include "llvm-c/Core.h"
 #include "llvm-c/Support.h"
+#include "llvm-c/Target.h"
 #include "llvm/Config/llvm-config.h"
 #include "caml/alloc.h"
 #include "caml/custom.h"
@@ -319,6 +320,19 @@
   return Val_unit;
 }
 
+/*===-- Data Layout -------------------------------------------------------===*/
+
+/* llmodule -> lldatalayout */
+CAMLprim LLVMTargetDataRef llvm_module_data_layout(LLVMModuleRef M) {
+  return LLVMGetModuleDataLayout(M);
+}
+
+/* lldatalayout -> lltype -> int64 */
+CAMLprim value llvm_type_alloc_size(LLVMTargetDataRef TD, LLVMTypeRef Ty) {
+  return caml_copy_int64(LLVMABISizeOfType(TD, Ty));
+}
+
+
 /*===-- Types -------------------------------------------------------------===*/
 
 /* lltype -> TypeKind.t */
Index: bindings/ocaml/llvm/llvm.mli
===================================================================
--- bindings/ocaml/llvm/llvm.mli
+++ bindings/ocaml/llvm/llvm.mli
@@ -25,6 +25,10 @@
     objects. See the [llvm::Module] class. *)
 type llmodule
 
+(** A parsed version of the target data layout string. See the
+    [llvm::DataLayout] class. *)
+type lldatalayout
+
 (** Each value in the LLVM IR has a type, an instance of [lltype]. See the
     [llvm::Type] class. *)
 type lltype
@@ -524,6 +528,17 @@
 val module_context : llmodule -> llcontext
 
 
+(** {6 Data Layout} *)
+
+(** Obtain the data layout for a module.
+    See the method [llvm::Module::getDataLayout]. *)
+val module_data_layout : llmodule -> lldatalayout
+
+(** Computes the ABI size of a type in bytes for a target.
+    See the method [llvm::DataLayout::getTypeAllocSize]. *)
+val type_alloc_size : lldatalayout -> lltype -> int64
+
+
 (** {6 Types} *)
 
 (** [classify_type ty] returns the {!TypeKind.t} corresponding to the type [ty].
Index: bindings/ocaml/llvm/llvm.ml
===================================================================
--- bindings/ocaml/llvm/llvm.ml
+++ bindings/ocaml/llvm/llvm.ml
@@ -10,6 +10,7 @@
 
 type llcontext
 type llmodule
+type lldatalayout
 type lltype
 type llvalue
 type lluse
@@ -424,6 +425,13 @@
                                = "llvm_set_module_inline_asm"
 external module_context : llmodule -> llcontext = "LLVMGetModuleContext"
 
+(*===-- Data Layout -------------------------------------------------------===*)
+external module_data_layout : llmodule -> lldatalayout
+                            = "llvm_module_data_layout"
+
+external type_alloc_size : lldatalayout -> lltype -> int64
+                         = "llvm_type_alloc_size"
+
 (*===-- Types -------------------------------------------------------------===*)
 external classify_type : lltype -> TypeKind.t = "llvm_classify_type"
 external type_context : lltype -> llcontext = "llvm_type_context"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52208.165866.patch
Type: text/x-patch
Size: 2938 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180918/82eb593d/attachment.bin>


More information about the llvm-commits mailing list