Skip to content

Commit 22b1094

Browse files
heycamemilio
authored andcommitted
Fix BitfieldUnit constructor to handle 64 bit wide bitfields on 32 bit.
Fixes #1639.
1 parent 807fa1e commit 22b1094

File tree

3 files changed

+160
-7
lines changed

3 files changed

+160
-7
lines changed

src/codegen/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,12 +1200,8 @@ impl BitfieldUnit {
12001200

12011201
impl Bitfield {
12021202
/// Extend an under construction bitfield unit constructor with this
1203-
/// bitfield. This involves two things:
1204-
///
1205-
/// 1. Adding a parameter with this bitfield's name and its type.
1206-
///
1207-
/// 2. Setting the relevant bits on the `__bindgen_bitfield_unit` variable
1208-
/// that's being constructed.
1203+
/// bitfield. This sets the relevant bits on the `__bindgen_bitfield_unit`
1204+
/// variable that's being constructed.
12091205
fn extend_ctor_impl(
12101206
&self,
12111207
ctx: &BindgenContext,
@@ -1216,7 +1212,9 @@ impl Bitfield {
12161212
let bitfield_ty_layout = bitfield_ty
12171213
.layout(ctx)
12181214
.expect("Bitfield without layout? Gah!");
1219-
let bitfield_int_ty = helpers::blob(ctx, bitfield_ty_layout);
1215+
let bitfield_int_ty = helpers::integer_type(ctx, bitfield_ty_layout)
1216+
.expect("Should already have verified that the bitfield is \
1217+
representable as an int");
12201218

12211219
let offset = self.offset_into_unit();
12221220
let width = self.width() as u8;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C)]
11+
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
12+
pub struct __BindgenBitfieldUnit<Storage, Align> {
13+
storage: Storage,
14+
align: [Align; 0],
15+
}
16+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> {
17+
#[inline]
18+
pub const fn new(storage: Storage) -> Self {
19+
Self { storage, align: [] }
20+
}
21+
}
22+
impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align>
23+
where
24+
Storage: AsRef<[u8]> + AsMut<[u8]>,
25+
{
26+
#[inline]
27+
pub fn get_bit(&self, index: usize) -> bool {
28+
debug_assert!(index / 8 < self.storage.as_ref().len());
29+
let byte_index = index / 8;
30+
let byte = self.storage.as_ref()[byte_index];
31+
let bit_index = if cfg!(target_endian = "big") {
32+
7 - (index % 8)
33+
} else {
34+
index % 8
35+
};
36+
let mask = 1 << bit_index;
37+
byte & mask == mask
38+
}
39+
#[inline]
40+
pub fn set_bit(&mut self, index: usize, val: bool) {
41+
debug_assert!(index / 8 < self.storage.as_ref().len());
42+
let byte_index = index / 8;
43+
let byte = &mut self.storage.as_mut()[byte_index];
44+
let bit_index = if cfg!(target_endian = "big") {
45+
7 - (index % 8)
46+
} else {
47+
index % 8
48+
};
49+
let mask = 1 << bit_index;
50+
if val {
51+
*byte |= mask;
52+
} else {
53+
*byte &= !mask;
54+
}
55+
}
56+
#[inline]
57+
pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
58+
debug_assert!(bit_width <= 64);
59+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
60+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
61+
let mut val = 0;
62+
for i in 0..(bit_width as usize) {
63+
if self.get_bit(i + bit_offset) {
64+
let index = if cfg!(target_endian = "big") {
65+
bit_width as usize - 1 - i
66+
} else {
67+
i
68+
};
69+
val |= 1 << index;
70+
}
71+
}
72+
val
73+
}
74+
#[inline]
75+
pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
76+
debug_assert!(bit_width <= 64);
77+
debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
78+
debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
79+
for i in 0..(bit_width as usize) {
80+
let mask = 1 << i;
81+
let val_bit_is_set = val & mask == mask;
82+
let index = if cfg!(target_endian = "big") {
83+
bit_width as usize - 1 - i
84+
} else {
85+
i
86+
};
87+
self.set_bit(index + bit_offset, val_bit_is_set);
88+
}
89+
}
90+
}
91+
#[repr(C, packed(4))]
92+
#[repr(align(4))]
93+
#[derive(Debug, Default, Copy, Clone)]
94+
pub struct Test {
95+
pub _bitfield_1: __BindgenBitfieldUnit<[u8; 8usize], u64>,
96+
}
97+
#[test]
98+
fn bindgen_test_layout_Test() {
99+
assert_eq!(
100+
::std::mem::size_of::<Test>(),
101+
8usize,
102+
concat!("Size of: ", stringify!(Test))
103+
);
104+
assert_eq!(
105+
::std::mem::align_of::<Test>(),
106+
4usize,
107+
concat!("Alignment of ", stringify!(Test))
108+
);
109+
}
110+
impl Test {
111+
#[inline]
112+
pub fn x(&self) -> u64 {
113+
unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 56u8) as u64) }
114+
}
115+
#[inline]
116+
pub fn set_x(&mut self, val: u64) {
117+
unsafe {
118+
let val: u64 = ::std::mem::transmute(val);
119+
self._bitfield_1.set(0usize, 56u8, val as u64)
120+
}
121+
}
122+
#[inline]
123+
pub fn y(&self) -> u64 {
124+
unsafe { ::std::mem::transmute(self._bitfield_1.get(56usize, 8u8) as u64) }
125+
}
126+
#[inline]
127+
pub fn set_y(&mut self, val: u64) {
128+
unsafe {
129+
let val: u64 = ::std::mem::transmute(val);
130+
self._bitfield_1.set(56usize, 8u8, val as u64)
131+
}
132+
}
133+
#[inline]
134+
pub fn new_bitfield_1(x: u64, y: u64) -> __BindgenBitfieldUnit<[u8; 8usize], u64> {
135+
let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 8usize], u64> =
136+
Default::default();
137+
__bindgen_bitfield_unit.set(0usize, 56u8, {
138+
let x: u64 = unsafe { ::std::mem::transmute(x) };
139+
x as u64
140+
});
141+
__bindgen_bitfield_unit.set(56usize, 8u8, {
142+
let y: u64 = unsafe { ::std::mem::transmute(y) };
143+
y as u64
144+
});
145+
__bindgen_bitfield_unit
146+
}
147+
}

tests/headers/bitfield-linux-32.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// bindgen-flags: -- --target=i586-unknown-linux
2+
3+
typedef unsigned long long uint64_t;
4+
5+
struct Test {
6+
uint64_t x : 56;
7+
uint64_t y : 8;
8+
};

0 commit comments

Comments
 (0)