]> git.proxmox.com Git - rustc.git/blame - src/test/codegen/packed.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / codegen / packed.rs
CommitLineData
54a0048b 1// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
32a655c1 11// compile-flags: -C no-prepopulate-passes
9e0c209e 12
32a655c1 13#![crate_type = "lib"]
c30ab7b3 14
32a655c1
SL
15#[repr(packed)]
16pub struct Packed {
17 dealign: u8,
18 data: u32
c30ab7b3 19}
223e47cc 20
32a655c1
SL
21// CHECK-LABEL: @write_pkd
22#[no_mangle]
23pub fn write_pkd(pkd: &mut Packed) -> u32 {
24// CHECK: %{{.*}} = load i32, i32* %{{.*}}, align 1
25// CHECK: store i32 42, i32* %{{.*}}, align 1
26 let result = pkd.data;
27 pkd.data = 42;
28 result
223e47cc 29}
8bb4bdeb
XL
30
31pub struct Array([i32; 8]);
32#[repr(packed)]
33pub struct BigPacked {
34 dealign: u8,
35 data: Array
36}
37
38// CHECK-LABEL: @call_pkd
39#[no_mangle]
40pub fn call_pkd(f: fn() -> Array) -> BigPacked {
41// CHECK: [[ALLOCA:%[_a-z0-9]+]] = alloca %Array
42// CHECK: call void %{{.*}}(%Array* noalias nocapture sret dereferenceable(32) [[ALLOCA]])
43// CHECK: call void @llvm.memcpy.{{.*}}(i8* %{{.*}}, i8* %{{.*}}, i{{[0-9]+}} 32, i32 1, i1 false)
44 // check that calls whose destination is a field of a packed struct
45 // go through an alloca rather than calling the function with an
46 // unaligned destination.
47 BigPacked { dealign: 0, data: f() }
48}
49
50#[repr(packed)]
51#[derive(Copy, Clone)]
52pub struct PackedPair(u8, u32);
53
54// CHECK-LABEL: @pkd_pair
55#[no_mangle]
56pub fn pkd_pair(pair1: &mut PackedPair, pair2: &mut PackedPair) {
57 // CHECK: [[V1:%[a-z0-9]+]] = load i8, i8* %{{.*}}, align 1
58 // CHECK: [[V2:%[a-z0-9]+]] = load i32, i32* %{{.*}}, align 1
59 // CHECK: store i8 [[V1]], i8* {{.*}}, align 1
60 // CHECK: store i32 [[V2]], i32* {{.*}}, align 1
61 *pair2 = *pair1;
62}
abe05a73
XL
63
64#[repr(packed)]
65#[derive(Copy, Clone)]
66pub struct PackedNestedPair((u32, u32));
67
68// CHECK-LABEL: @pkd_nested_pair
69#[no_mangle]
70pub fn pkd_nested_pair(pair1: &mut PackedNestedPair, pair2: &mut PackedNestedPair) {
71// CHECK: call void @llvm.memcpy.{{.*}}(i8* %{{.*}}, i8* %{{.*}}, i{{[0-9]+}} 8, i32 1, i1 false)
72 *pair2 = *pair1;
73}