From 57cebeec7ba08b55f29f5bf98ad0a3a17e9d0c71 Mon Sep 17 00:00:00 2001 From: Gordon Henriksen Date: Mon, 17 Dec 2007 16:08:32 +0000 Subject: [PATCH] C and Ocaml bindings for address spaces, for that burgeoning market for Ocaml-based compilers targeting embedded devices. :) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45096 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/ocaml/llvm/llvm.ml | 5 ++++- bindings/ocaml/llvm/llvm.mli | 15 +++++++++++++-- bindings/ocaml/llvm/llvm_ocaml.c | 20 ++++++++++++++++++-- include/llvm-c/Core.h | 3 ++- lib/VMCore/Core.cpp | 13 ++++++++----- test/Bindings/Ocaml/vmcore.ml | 25 ++++++++++++++++++------- 6 files changed, 63 insertions(+), 18 deletions(-) diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 6ede17978bf..5a5f4e7cfe1 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -144,11 +144,14 @@ external is_packed : lltype -> bool = "llvm_is_packed" (*--... Operations on pointer, vector, and array types .....................--*) external array_type : lltype -> int -> lltype = "llvm_array_type" -external pointer_type : lltype -> lltype = "LLVMPointerType" +external pointer_type : lltype -> lltype = "llvm_pointer_type" +external qualified_pointer_type : lltype -> int -> lltype + = "llvm_qualified_pointer_type" external vector_type : lltype -> int -> lltype = "llvm_vector_type" external element_type : lltype -> lltype = "LLVMGetElementType" external array_length : lltype -> int = "llvm_array_length" +external address_space : lltype -> int = "llvm_address_space" external vector_size : lltype -> int = "llvm_vector_size" (*--... Operations on other types ..........................................--*) diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 2bc3e1a27c8..4646c576152 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -258,8 +258,15 @@ external is_packed : lltype -> bool = "llvm_is_packed" external array_type : lltype -> int -> lltype = "llvm_array_type" (** [pointer_type ty] returns the pointer type referencing objects of type - [ty]. See the method [llvm::PointerType::get]. **) -external pointer_type : lltype -> lltype = "LLVMPointerType" + [ty] in the default address space (0). + See the method [llvm::PointerType::getUnqual]. **) +external pointer_type : lltype -> lltype = "llvm_pointer_type" + +(** [qualified_pointer_type ty as] returns the pointer type referencing objects + of type [ty] in address space [as]. + See the method [llvm::PointerType::get]. **) +external qualified_pointer_type : lltype -> int -> lltype + = "llvm_qualified_pointer_type" (** [vector_type ty n] returns the array type containing [n] elements of the primitive type [ty]. See the method [llvm::ArrayType::get]. **) @@ -273,6 +280,10 @@ external element_type : lltype -> lltype = "LLVMGetElementType" See the method [llvm::ArrayType::getNumElements]. **) external array_length : lltype -> int = "llvm_array_length" +(** [address_space pty] returns the address space qualifier of the pointer type + [pty]. See the method [llvm::PointerType::getAddressSpace]. **) +external address_space : lltype -> int = "llvm_address_space" + (** [element_type ty] returns the element count of the vector type [ty]. See the method [llvm::VectorType::getNumElements]. **) external vector_size : lltype -> int = "llvm_vector_size" diff --git a/bindings/ocaml/llvm/llvm_ocaml.c b/bindings/ocaml/llvm/llvm_ocaml.c index f9d7e6f4780..43b6167bdc0 100644 --- a/bindings/ocaml/llvm/llvm_ocaml.c +++ b/bindings/ocaml/llvm/llvm_ocaml.c @@ -163,6 +163,17 @@ CAMLprim LLVMTypeRef llvm_array_type(LLVMTypeRef ElementTy, value Count) { return LLVMArrayType(ElementTy, Int_val(Count)); } +/* lltype -> lltype */ +CAMLprim LLVMTypeRef llvm_pointer_type(LLVMTypeRef ElementTy) { + return LLVMPointerType(ElementTy, 0); +} + +/* lltype -> int -> lltype */ +CAMLprim LLVMTypeRef llvm_qualified_pointer_type(LLVMTypeRef ElementTy, + value AddressSpace) { + return LLVMPointerType(ElementTy, Int_val(AddressSpace)); +} + /* lltype -> int -> lltype */ CAMLprim LLVMTypeRef llvm_vector_type(LLVMTypeRef ElementTy, value Count) { return LLVMVectorType(ElementTy, Int_val(Count)); @@ -173,6 +184,11 @@ CAMLprim value llvm_array_length(LLVMTypeRef ArrayTy) { return Val_int(LLVMGetArrayLength(ArrayTy)); } +/* lltype -> int */ +CAMLprim value llvm_address_space(LLVMTypeRef PtrTy) { + return Val_int(LLVMGetPointerAddressSpace(PtrTy)); +} + /* lltype -> int */ CAMLprim value llvm_vector_size(LLVMTypeRef VectorTy) { return Val_int(LLVMGetVectorSize(VectorTy)); @@ -399,7 +415,7 @@ CAMLprim LLVMValueRef llvm_declare_global(LLVMTypeRef Ty, value Name, LLVMValueRef GlobalVar; if ((GlobalVar = LLVMGetNamedGlobal(M, String_val(Name)))) { if (LLVMGetElementType(LLVMTypeOf(GlobalVar)) != Ty) - return LLVMConstBitCast(GlobalVar, LLVMPointerType(Ty)); + return LLVMConstBitCast(GlobalVar, LLVMPointerType(Ty, 0)); return GlobalVar; } return LLVMAddGlobal(M, Ty, String_val(Name)); @@ -476,7 +492,7 @@ CAMLprim LLVMValueRef llvm_declare_function(value Name, LLVMTypeRef Ty, LLVMValueRef Fn; if ((Fn = LLVMGetNamedFunction(M, String_val(Name)))) { if (LLVMGetElementType(LLVMTypeOf(Fn)) != Ty) - return LLVMConstBitCast(Fn, LLVMPointerType(Ty)); + return LLVMConstBitCast(Fn, LLVMPointerType(Ty, 0)); return Fn; } return LLVMAddFunction(M, String_val(Name), Ty); diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 696ef6a168b..4ed2bd0c60d 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -194,11 +194,12 @@ int LLVMIsPackedStruct(LLVMTypeRef StructTy); /* Operations on array, pointer, and vector types (sequence types) */ LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount); -LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType); +LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace); LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount); LLVMTypeRef LLVMGetElementType(LLVMTypeRef Ty); unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy); +unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy); unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy); /* Operations on other types */ diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index 9a7c7909e81..4c56e556eb3 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -145,16 +145,15 @@ int LLVMIsPackedStruct(LLVMTypeRef StructTy) { /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/ -LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount){ +LLVMTypeRef LLVMArrayType(LLVMTypeRef ElementType, unsigned ElementCount) { return wrap(ArrayType::get(unwrap(ElementType), ElementCount)); } -LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType) { - // FIXME: Needst to handle address spaces - return wrap(PointerType::getUnqual(unwrap(ElementType))); +LLVMTypeRef LLVMPointerType(LLVMTypeRef ElementType, unsigned AddressSpace) { + return wrap(PointerType::get(unwrap(ElementType), AddressSpace)); } -LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType,unsigned ElementCount){ +LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount) { return wrap(VectorType::get(unwrap(ElementType), ElementCount)); } @@ -166,6 +165,10 @@ unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) { return unwrap(ArrayTy)->getNumElements(); } +unsigned LLVMGetPointerAddressSpace(LLVMTypeRef PointerTy) { + return unwrap(PointerTy)->getAddressSpace(); +} + unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) { return unwrap(VectorTy)->getNumElements(); } diff --git a/test/Bindings/Ocaml/vmcore.ml b/test/Bindings/Ocaml/vmcore.ml index 8bea6d23a9c..65d8a316e9c 100644 --- a/test/Bindings/Ocaml/vmcore.ml +++ b/test/Bindings/Ocaml/vmcore.ml @@ -103,13 +103,24 @@ let test_types () = insist (i8_type == element_type ty); insist (Array_type == classify_type ty); - (* RUN: grep {Ty10.*float\*} < %t.ll - *) - group "pointer"; - let ty = pointer_type float_type in - insist (define_type_name "Ty10" ty m); - insist (float_type == element_type ty); - insist (Pointer_type == classify_type ty); + begin group "pointer"; + (* RUN: grep {UnqualPtrTy.*float\*} < %t.ll + *) + let ty = pointer_type float_type in + insist (define_type_name "UnqualPtrTy" ty m); + insist (float_type == element_type ty); + insist (0 == address_space ty); + insist (Pointer_type == classify_type ty) + end; + + begin group "qualified_pointer"; + (* RUN: grep {QualPtrTy.*i8.*3.*\*} < %t.ll + *) + let ty = qualified_pointer_type i8_type 3 in + insist (define_type_name "QualPtrTy" ty m); + insist (i8_type == element_type ty); + insist (3 == address_space ty) + end; (* RUN: grep {Ty11.*\<4 x i16\>} < %t.ll *)