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" 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_ocaml.c =================================================================== --- bindings/ocaml/llvm/llvm_ocaml.c +++ bindings/ocaml/llvm/llvm_ocaml.c @@ -20,6 +20,7 @@ #include #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 */