]> git.lizzy.rs Git - rust.git/commitdiff
rustllvm: Add LLVMRustArrayType
authorklutzy <klutzytheklutzy@gmail.com>
Sat, 10 May 2014 08:30:55 +0000 (17:30 +0900)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 14 May 2014 00:24:08 +0000 (17:24 -0700)
LLVM internally uses `uint64_t` for array size, but the corresponding
C API (`LLVMArrayType`) uses `unsigned int` so ths value is truncated.
Therefore rustc generates wrong type for fixed-sized large vector e.g.
`[0 x i8]` for `[0u8, ..(1 << 32)]`.

This patch adds `LLVMRustArrayType` function for `uint64_t` support.

src/librustc/lib/llvm.rs
src/librustc/middle/trans/type_.rs
src/rustllvm/RustWrapper.cpp
src/test/run-pass/vec-fixed-length.rs

index e3d0e78e93873e335c18db29f8dbc6136d4e3ee6..0c874bd776ed11d1fd8a7e162d4741d3354197b3 100644 (file)
@@ -398,8 +398,7 @@ pub fn LLVMGetStructElementTypes(StructTy: TypeRef,
         pub fn LLVMIsPackedStruct(StructTy: TypeRef) -> Bool;
 
         /* Operations on array, pointer, and vector types (sequence types) */
-        pub fn LLVMArrayType(ElementType: TypeRef, ElementCount: c_uint)
-                             -> TypeRef;
+        pub fn LLVMRustArrayType(ElementType: TypeRef, ElementCount: u64) -> TypeRef;
         pub fn LLVMPointerType(ElementType: TypeRef, AddressSpace: c_uint)
                                -> TypeRef;
         pub fn LLVMVectorType(ElementType: TypeRef, ElementCount: c_uint)
index a0744037dc0081650abd545b2a1cbee3fabbad0d..d5a80edfaedd15d24dee9ade3ceb9e1ec1db09c0 100644 (file)
@@ -207,7 +207,7 @@ pub fn tydesc(ccx: &CrateContext) -> Type {
     }
 
     pub fn array(ty: &Type, len: u64) -> Type {
-        ty!(llvm::LLVMArrayType(ty.to_ref(), len as c_uint))
+        ty!(llvm::LLVMRustArrayType(ty.to_ref(), len))
     }
 
     pub fn vector(ty: &Type, len: u64) -> Type {
index ec33b750358bbb857f963157322136a7d8234cbb..717cd333a79ce800e41dee9ee23fb20871f5b2a1 100644 (file)
@@ -754,3 +754,9 @@ LLVMRustGetSectionName(LLVMSectionIteratorRef SI, const char **ptr) {
     *ptr = ret.data();
     return ret.size();
 }
+
+// LLVMArrayType function does not support 64-bit ElementCount
+extern "C" LLVMTypeRef
+LLVMRustArrayType(LLVMTypeRef ElementType, uint64_t ElementCount) {
+    return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
+}
index 8e22be1b4325c5325af76f7fc3c5225643249ace..5116b4e746a05cd066837b497e3fbed7a4f51be6 100644 (file)
@@ -8,7 +8,19 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::mem::size_of;
+
 pub fn main() {
     let x: [int, ..4] = [1, 2, 3, 4];
-    println!("{}", x[0]);
+    assert_eq!(x[0], 1);
+    assert_eq!(x[1], 2);
+    assert_eq!(x[2], 3);
+    assert_eq!(x[3], 4);
+
+    assert_eq!(size_of::<[u8, ..4]>(), 4u);
+
+    // FIXME #10183
+    if cfg!(target_word_size = "64") {
+        assert_eq!(size_of::<[u8, ..(1 << 32)]>(), (1u << 32));
+    }
 }