diff options
author | whitequark <whitequark@whitequark.org> | 2017-06-05 11:49:52 +0000 |
---|---|---|
committer | whitequark <whitequark@whitequark.org> | 2017-06-05 11:49:52 +0000 |
commit | f6059fdc547f1f07ef1c22b48da6514a581c3665 (patch) | |
tree | b5754f0c702d23dd67eff0cc4e46695a46dac054 | |
parent | b3398936ab26d7426bd5419da6c57334c3cfc043 (diff) | |
download | bcm5719-llvm-f6059fdc547f1f07ef1c22b48da6514a581c3665.tar.gz bcm5719-llvm-f6059fdc547f1f07ef1c22b48da6514a581c3665.zip |
[LLVM-C] [OCaml] Expose Type::subtypes.
The C functions added are LLVMGetNumContainedTypes and
LLVMGetSubtypes.
The OCaml function added is Llvm.subtypes.
Patch by Ekaterina Vaartis.
Differential Revision: https://reviews.llvm.org/D33677
llvm-svn: 304709
-rw-r--r-- | llvm/bindings/ocaml/llvm/llvm.ml | 2 | ||||
-rw-r--r-- | llvm/bindings/ocaml/llvm/llvm.mli | 3 | ||||
-rw-r--r-- | llvm/bindings/ocaml/llvm/llvm_ocaml.c | 14 | ||||
-rw-r--r-- | llvm/include/llvm-c/Core.h | 14 | ||||
-rw-r--r-- | llvm/lib/IR/Core.cpp | 12 | ||||
-rw-r--r-- | llvm/test/Bindings/OCaml/core.ml | 11 |
6 files changed, 56 insertions, 0 deletions
diff --git a/llvm/bindings/ocaml/llvm/llvm.ml b/llvm/bindings/ocaml/llvm/llvm.ml index 399fd2d27c2..6e8ca662ef6 100644 --- a/llvm/bindings/ocaml/llvm/llvm.ml +++ b/llvm/bindings/ocaml/llvm/llvm.ml @@ -459,6 +459,8 @@ external is_packed : lltype -> bool = "llvm_is_packed" external is_opaque : lltype -> bool = "llvm_is_opaque" (*--... Operations on pointer, vector, and array types .....................--*) + +external subtypes : lltype -> lltype array = "llvm_subtypes" external array_type : lltype -> int -> lltype = "llvm_array_type" external pointer_type : lltype -> lltype = "llvm_pointer_type" external qualified_pointer_type : lltype -> int -> lltype diff --git a/llvm/bindings/ocaml/llvm/llvm.mli b/llvm/bindings/ocaml/llvm/llvm.mli index 4068126e2cb..c422e78f5d2 100644 --- a/llvm/bindings/ocaml/llvm/llvm.mli +++ b/llvm/bindings/ocaml/llvm/llvm.mli @@ -658,6 +658,9 @@ val is_opaque : lltype -> bool (** {7 Operations on pointer, vector, and array types} *) +(** [subtypes ty] returns [ty]'s subtypes *) +val subtypes : lltype -> lltype array + (** [array_type ty n] returns the array type containing [n] elements of type [ty]. See the method [llvm::ArrayType::get]. *) val array_type : lltype -> int -> lltype diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c index af04ea25c8a..4b6d1c5072b 100644 --- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c +++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c @@ -506,6 +506,20 @@ CAMLprim value llvm_is_opaque(LLVMTypeRef StructTy) { /*--... Operations on array, pointer, and vector types .....................--*/ +/* lltype -> lltype array */ +CAMLprim value llvm_subtypes(LLVMTypeRef Ty) { + CAMLparam0(); + CAMLlocal1(Arr); + + unsigned Size = LLVMGetNumContainedTypes(Ty); + + Arr = caml_alloc(Size, 0); + + LLVMGetSubtypes(Ty, (LLVMTypeRef *) Arr); + + CAMLreturn(Arr); +} + /* lltype -> int -> lltype */ CAMLprim LLVMTypeRef llvm_array_type(LLVMTypeRef ElementTy, value Count) { return LLVMArrayType(ElementTy, Int_val(Count)); diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 0a1d8faf99b..22cef23007c 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -1040,6 +1040,20 @@ LLVMBool LLVMIsOpaqueStruct(LLVMTypeRef StructTy); LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty); /** + * Returns type's subtypes + * + * @see llvm::Type::subtypes() + */ +void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr); + +/** + * Return the number of types in the derived type. + * + * @see llvm::Type::getNumContainedTypes() + */ +unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp); + +/** * Create a fixed size array type that refers to a specific type. * * The created type will exist in the context that its element type diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 50292b6e20b..4ff0261a7f0 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -568,6 +568,14 @@ LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) { /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ +void LLVMGetSubtypes(LLVMTypeRef Tp, LLVMTypeRef *Arr) { + int i = 0; + for (auto *T : unwrap(Tp)->subtypes()) { + Arr[i] = wrap(T); + i++; + } +} + LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); } @@ -587,6 +595,10 @@ LLVMTypeRef LLVMGetElementType(LLVMTypeRef WrappedTy) { return wrap(cast<SequentialType>(Ty)->getElementType()); } +unsigned LLVMGetNumContainedTypes(LLVMTypeRef Tp) { + return unwrap(Tp)->getNumContainedTypes(); +} + unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { return unwrap<ArrayType>(ArrayTy)->getNumElements(); } diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml index 105f1bc4f73..802baa0b80b 100644 --- a/llvm/test/Bindings/OCaml/core.ml +++ b/llvm/test/Bindings/OCaml/core.ml @@ -66,6 +66,16 @@ let suite name f = let filename = Sys.argv.(1) let m = create_module context filename +(*===-- Contained types --------------------------------------------------===*) + +let test_contained_types () = + let pointer_i32 = pointer_type i32_type in + insist (i32_type = (Array.get (subtypes pointer_i32) 0)); + + let ar = struct_type context [| i32_type; i8_type |] in + insist (i32_type = (Array.get (subtypes ar)) 0); + insist (i8_type = (Array.get (subtypes ar)) 1) + (*===-- Conversion --------------------------------------------------------===*) @@ -1533,6 +1543,7 @@ let test_writer () = (*===-- Driver ------------------------------------------------------------===*) let _ = + suite "contained types" test_contained_types; suite "conversion" test_conversion; suite "target" test_target; suite "constants" test_constants; |