Skip to content

Commit bf904df

Browse files
committed
Use the correct alignment for integer types
1 parent 100dfce commit bf904df

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

src/common.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -424,35 +424,35 @@ impl<'gcc, 'tcx> TypeReflection<'gcc, 'tcx> for Type<'gcc> {
424424
}
425425

426426
fn is_i8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
427-
self.unqualified() == cx.i8_type
427+
self.is_compatible_with(cx.i8_type)
428428
}
429429

430430
fn is_u8(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
431-
self.unqualified() == cx.u8_type
431+
self.is_compatible_with(cx.u8_type)
432432
}
433433

434434
fn is_i16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
435-
self.unqualified() == cx.i16_type
435+
self.is_compatible_with(cx.i16_type)
436436
}
437437

438438
fn is_u16(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
439-
self.unqualified() == cx.u16_type
439+
self.is_compatible_with(cx.u16_type)
440440
}
441441

442442
fn is_i32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
443-
self.unqualified() == cx.i32_type
443+
self.is_compatible_with(cx.i32_type)
444444
}
445445

446446
fn is_u32(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
447-
self.unqualified() == cx.u32_type
447+
self.is_compatible_with(cx.u32_type)
448448
}
449449

450450
fn is_i64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
451-
self.unqualified() == cx.i64_type
451+
self.is_compatible_with(cx.i64_type)
452452
}
453453

454454
fn is_u64(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {
455-
self.unqualified() == cx.u64_type
455+
self.is_compatible_with(cx.u64_type)
456456
}
457457

458458
fn is_i128(&self, cx: &CodegenCx<'gcc, 'tcx>) -> bool {

src/context.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,25 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
129129
pub fn new(context: &'gcc Context<'gcc>, codegen_unit: &'tcx CodegenUnit<'tcx>, tcx: TyCtxt<'tcx>, supports_128bit_integers: bool) -> Self {
130130
let check_overflow = tcx.sess.overflow_checks();
131131

132-
let i8_type = context.new_c_type(CType::Int8t);
133-
let i16_type = context.new_c_type(CType::Int16t);
134-
let i32_type = context.new_c_type(CType::Int32t);
135-
let i64_type = context.new_c_type(CType::Int64t);
136-
let u8_type = context.new_c_type(CType::UInt8t);
137-
let u16_type = context.new_c_type(CType::UInt16t);
138-
let u32_type = context.new_c_type(CType::UInt32t);
139-
let u64_type = context.new_c_type(CType::UInt64t);
132+
let create_type = |ctype, rust_type| {
133+
let layout = tcx.layout_of(ParamEnv::reveal_all().and(rust_type)).unwrap();
134+
let align = layout.align.abi.bytes();
135+
context.new_c_type(ctype).get_aligned(align)
136+
};
137+
138+
let i8_type = create_type(CType::Int8t, tcx.types.i8);
139+
let i16_type = create_type(CType::Int16t, tcx.types.i16);
140+
let i32_type = create_type(CType::Int32t, tcx.types.i32);
141+
let i64_type = create_type(CType::Int64t, tcx.types.i64);
142+
let u8_type = create_type(CType::UInt8t, tcx.types.u8);
143+
let u16_type = create_type(CType::UInt16t, tcx.types.u16);
144+
let u32_type = create_type(CType::UInt32t, tcx.types.u32);
145+
let u64_type = create_type(CType::UInt64t, tcx.types.u64);
140146

141147
let (i128_type, u128_type) =
142148
if supports_128bit_integers {
143-
let i128_type = context.new_c_type(CType::Int128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
144-
let u128_type = context.new_c_type(CType::UInt128t).get_aligned(8); // TODO(antoyo): should the alignment be hard-coded?;
149+
let i128_type = create_type(CType::Int128t, tcx.types.i128);
150+
let u128_type = create_type(CType::UInt128t, tcx.types.u128);
145151
(i128_type, u128_type)
146152
}
147153
else {
@@ -265,15 +271,16 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
265271
}
266272

267273
pub fn is_native_int_type(&self, typ: Type<'gcc>) -> bool {
274+
// TODO: cache those types to not query libgccjit everytime this is called.
268275
let types = [
269-
self.u8_type,
270-
self.u16_type,
271-
self.u32_type,
272-
self.u64_type,
273-
self.i8_type,
274-
self.i16_type,
275-
self.i32_type,
276-
self.i64_type,
276+
self.context.new_c_type(CType::UInt8t),
277+
self.context.new_c_type(CType::UInt16t),
278+
self.context.new_c_type(CType::UInt32t),
279+
self.context.new_c_type(CType::UInt64t),
280+
self.context.new_c_type(CType::Int8t),
281+
self.context.new_c_type(CType::Int16t),
282+
self.context.new_c_type(CType::Int32t),
283+
self.context.new_c_type(CType::Int64t),
277284
];
278285

279286
for native_type in types {

0 commit comments

Comments
 (0)