Skip to content

Commit 1379610

Browse files
committed
[STASH]
1 parent 1b6bd3d commit 1379610

7 files changed

Lines changed: 71 additions & 84 deletions

File tree

src/base.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
826826
if operand.layout().size.bytes() == 0 {
827827
// Do nothing for ZST's
828828
} else if fx.clif_type(operand.layout().ty) == Some(types::I8) {
829-
let times = fx.bcx.ins().iconst(fx.pointer_type, times as i64);
829+
let times = fx.bcx.ins().iconst(fx.pointer_type, times.cast_signed());
830830
// FIXME use emit_small_memset where possible
831831
let addr = lval.to_ptr().get_addr(fx);
832832
let val = operand.load_scalar(fx);
@@ -955,8 +955,8 @@ fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx
955955
let len = fx
956956
.monomorphize(len)
957957
.try_to_target_usize(fx.tcx)
958-
.expect("expected monomorphic const in codegen") as i64;
959-
fx.bcx.ins().iconst(fx.pointer_type, len)
958+
.expect("expected monomorphic const in codegen");
959+
fx.bcx.ins().iconst(fx.pointer_type, len.cast_signed())
960960
}
961961
ty::Slice(_elem_ty) => place.to_ptr_unsized().1,
962962
_ => bug!("Rvalue::Len({:?})", place),
@@ -988,7 +988,7 @@ pub(crate) fn codegen_place<'tcx>(
988988
PlaceElem::ConstantIndex { offset, min_length: _, from_end } => {
989989
let offset: u64 = offset;
990990
let index = if !from_end {
991-
fx.bcx.ins().iconst(fx.pointer_type, offset as i64)
991+
fx.bcx.ins().iconst(fx.pointer_type, offset.cast_signed())
992992
} else {
993993
let len = codegen_array_len(fx, cplace);
994994
fx.bcx.ins().iadd_imm(len, -(offset as i64))

src/common.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,23 @@ fn clif_pair_type_from_ty<'tcx>(
101101
})
102102
}
103103

104-
pub(crate) fn codegen_iconst_u128(bcx: &mut FunctionBuilder<'_>, val: u128) -> Value {
105-
let lsb = bcx.ins().iconst(types::I64, (val as u64).cast_signed());
106-
let msb = bcx.ins().iconst(types::I64, ((val >> 64) as u64).cast_signed());
107-
bcx.ins().iconcat(lsb, msb)
104+
pub(crate) fn codegen_iconst_unsigned(bcx: &mut FunctionBuilder<'_>, ty: Type, val: u128) -> Value {
105+
if ty == types::I128 {
106+
if val == 0 {
107+
let zero = bcx.ins().iconst(types::I64, 0);
108+
return bcx.ins().iconcat(zero, zero);
109+
}
110+
111+
let lsb = bcx.ins().iconst(types::I64, (val as u64).cast_signed());
112+
let msb = bcx.ins().iconst(types::I64, ((val >> 64) as u64).cast_signed());
113+
bcx.ins().iconcat(lsb, msb)
114+
} else if ty == types::I64 {
115+
bcx.ins().iconst(ty, val as i64)
116+
} else {
117+
let shift = 128 - ty.bits();
118+
let val = (val << shift) >> shift;
119+
bcx.ins().iconst(ty, val as i64)
120+
}
108121
}
109122

110123
pub(crate) fn codegen_icmp_imm(
@@ -118,7 +131,10 @@ pub(crate) fn codegen_icmp_imm(
118131
// FIXME legalize `icmp_imm.i128` in Cranelift
119132

120133
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
121-
let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64);
134+
let (rhs_lsb, rhs_msb) = (
135+
(rhs.cast_unsigned() as u64).cast_signed(),
136+
((rhs.cast_unsigned() >> 64) as u64).cast_signed(),
137+
);
122138

123139
match intcc {
124140
IntCC::Equal => {
@@ -161,12 +177,7 @@ pub(crate) fn codegen_bitcast(fx: &mut FunctionCx<'_, '_, '_>, dst_ty: Type, val
161177
}
162178

163179
pub(crate) fn type_zero_value(bcx: &mut FunctionBuilder<'_>, ty: Type) -> Value {
164-
if ty == types::I128 {
165-
let zero = bcx.ins().iconst(types::I64, 0);
166-
bcx.ins().iconcat(zero, zero)
167-
} else {
168-
bcx.ins().iconst(ty, 0)
169-
}
180+
codegen_iconst_unsigned(bcx, ty, 0)
170181
}
171182

172183
pub(crate) fn type_min_max_value(
@@ -178,8 +189,8 @@ pub(crate) fn type_min_max_value(
178189

179190
if ty == types::I128 {
180191
if signed {
181-
let min = codegen_iconst_u128(bcx, i128::MIN.cast_unsigned());
182-
let max = codegen_iconst_u128(bcx, i128::MAX.cast_unsigned());
192+
let min = codegen_iconst_unsigned(bcx, types::I128, i128::MIN.cast_unsigned());
193+
let max = codegen_iconst_unsigned(bcx, types::I128, i128::MAX.cast_unsigned());
183194
return (min, max);
184195
} else {
185196
let min = type_zero_value(bcx, types::I128);
@@ -206,10 +217,10 @@ pub(crate) fn type_min_max_value(
206217
(types::I8, false) => i64::from(u8::MAX),
207218
(types::I16, false) => i64::from(u16::MAX),
208219
(types::I32, false) => i64::from(u32::MAX),
209-
(types::I64, false) => u64::MAX as i64,
210-
(types::I8, true) => i64::from(i8::MAX as u8),
211-
(types::I16, true) => i64::from(i16::MAX as u16),
212-
(types::I32, true) => i64::from(i32::MAX as u32),
220+
(types::I64, false) => u64::MAX.cast_signed(),
221+
(types::I8, true) => i64::from(i8::MAX.cast_unsigned()),
222+
(types::I16, true) => i64::from(i16::MAX.cast_unsigned()),
223+
(types::I32, true) => i64::from(i32::MAX.cast_unsigned()),
213224
(types::I64, true) => i64::MAX,
214225
_ => unreachable!(),
215226
};

src/constant.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,11 @@ pub(crate) fn codegen_const_value<'tcx>(
113113
if fx.clif_type(layout.ty).is_some() {
114114
CValue::const_val(fx, layout, int)
115115
} else {
116-
let raw_val = int.size().truncate(int.to_bits(int.size()));
117-
let val = match int.size().bytes() {
118-
1 => fx.bcx.ins().iconst(types::I8, raw_val as i64),
119-
2 => fx.bcx.ins().iconst(types::I16, raw_val as i64),
120-
4 => fx.bcx.ins().iconst(types::I32, raw_val as i64),
121-
8 => fx.bcx.ins().iconst(types::I64, raw_val as i64),
122-
16 => codegen_iconst_u128(&mut fx.bcx, raw_val),
123-
_ => unreachable!(),
124-
};
116+
let val = codegen_iconst_unsigned(
117+
&mut fx.bcx,
118+
Type::int_with_byte_size(int.size().bytes() as u16).unwrap(),
119+
int.to_bits(int.size()),
120+
);
125121

126122
// FIXME avoid this extra copy to the stack and directly write to the final
127123
// destination
@@ -136,7 +132,9 @@ pub(crate) fn codegen_const_value<'tcx>(
136132
let base_addr = match fx.tcx.global_alloc(alloc_id) {
137133
GlobalAlloc::Memory(alloc) => {
138134
if alloc.inner().len() == 0 {
139-
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes() as i64)
135+
fx.bcx
136+
.ins()
137+
.iconst(fx.pointer_type, alloc.inner().align.bytes().cast_signed())
140138
} else {
141139
let data_id = data_id_for_alloc_id(
142140
&mut fx.constants_cx,
@@ -217,7 +215,7 @@ pub(crate) fn codegen_const_value<'tcx>(
217215
),
218216
ConstValue::Slice { alloc_id, meta } => {
219217
let ptr = pointer_for_allocation(fx, alloc_id);
220-
let len = fx.bcx.ins().iconst(fx.pointer_type, meta as i64);
218+
let len = fx.bcx.ins().iconst(fx.pointer_type, meta.cast_signed());
221219
CValue::by_val_pair(ptr, len, layout)
222220
}
223221
}

src/discriminant.rs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,8 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
3030
} => {
3131
let ptr = place.place_field(fx, tag_field);
3232
let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val;
33-
let to = match ptr.layout().ty.kind() {
34-
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
35-
codegen_iconst_u128(&mut fx.bcx, to)
36-
}
37-
ty::Uint(_) | ty::Int(_) => {
38-
let clif_ty = fx.clif_type(ptr.layout().ty).unwrap();
39-
let raw_val = ptr.layout().size.truncate(to);
40-
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
41-
}
42-
_ => unreachable!(),
43-
};
33+
let clif_ty = fx.clif_type(ptr.layout().ty).unwrap();
34+
let to = codegen_iconst_unsigned(&mut fx.bcx, clif_ty, to);
4435
let discr = CValue::by_val(to, ptr.layout());
4536
ptr.write_cvalue(fx, discr);
4637
}
@@ -55,10 +46,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
5546
let niche_type = fx.clif_type(niche.layout().ty).unwrap();
5647
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
5748
let niche_value = (niche_value as u128).wrapping_add(niche_start);
58-
let niche_value = match niche_type {
59-
types::I128 => codegen_iconst_u128(&mut fx.bcx, niche_value),
60-
ty => fx.bcx.ins().iconst(ty, niche_value as i64),
61-
};
49+
let niche_value = codegen_iconst_unsigned(&mut fx.bcx, niche_type, niche_value);
6250
let niche_llval = CValue::by_val(niche_value, niche.layout());
6351
niche.write_cvalue(fx, niche_llval);
6452
}
@@ -86,17 +74,8 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
8674
.discriminant_for_variant(fx.tcx, *index)
8775
.map_or(u128::from(index.as_u32()), |discr| discr.val);
8876

89-
let val = match dest_layout.ty.kind() {
90-
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
91-
codegen_iconst_u128(&mut fx.bcx, discr_val)
92-
}
93-
ty::Uint(_) | ty::Int(_) => {
94-
let clif_ty = fx.clif_type(dest_layout.ty).unwrap();
95-
let raw_val = dest_layout.size.truncate(discr_val);
96-
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
97-
}
98-
_ => unreachable!(),
99-
};
77+
let clif_ty = fx.clif_type(dest_layout.ty).unwrap();
78+
let val = codegen_iconst_unsigned(&mut fx.bcx, clif_ty, discr_val);
10079
let res = CValue::by_val(val, dest_layout);
10180
dest.write_cvalue(fx, res);
10281
return;
@@ -151,16 +130,16 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
151130
// } else {
152131
// untagged_variant
153132
// }
154-
let is_niche = codegen_icmp_imm(fx, IntCC::Equal, tag, niche_start as i128);
133+
let is_niche = codegen_icmp_imm(fx, IntCC::Equal, tag, niche_start.cast_signed());
155134
let tagged_discr =
156-
fx.bcx.ins().iconst(cast_to, niche_variants.start().as_u32() as i64);
135+
fx.bcx.ins().iconst(cast_to, i64::from(niche_variants.start().as_u32()));
157136
(is_niche, tagged_discr, 0)
158137
} else {
159138
// The special cases don't apply, so we'll have to go with
160139
// the general algorithm.
161140
let niche_start = match fx.bcx.func.dfg.value_type(tag) {
162-
types::I128 => codegen_iconst_u128(&mut fx.bcx, niche_start),
163-
ty => fx.bcx.ins().iconst(ty, niche_start as i64),
141+
// FIXME remove match
142+
ty => codegen_iconst_unsigned(&mut fx.bcx, ty, niche_start),
164143
};
165144
let relative_discr = fx.bcx.ins().isub(tag, niche_start);
166145
let cast_tag = clif_intcast(fx, relative_discr, cast_to, false);
@@ -177,17 +156,17 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
177156
tagged_discr
178157
} else {
179158
let delta = match cast_to {
180-
types::I128 => codegen_iconst_u128(&mut fx.bcx, delta),
181-
ty => fx.bcx.ins().iconst(ty, delta as i64),
159+
// FIXME remove match
160+
ty => codegen_iconst_unsigned(&mut fx.bcx, ty, delta),
182161
};
183162
fx.bcx.ins().iadd(tagged_discr, delta)
184163
};
185164

186-
let untagged_variant = if cast_to == types::I128 {
187-
codegen_iconst_u128(&mut fx.bcx, u128::from(untagged_variant.as_u32()))
188-
} else {
189-
fx.bcx.ins().iconst(cast_to, i64::from(untagged_variant.as_u32()))
190-
};
165+
let untagged_variant = codegen_iconst_unsigned(
166+
&mut fx.bcx,
167+
cast_to,
168+
u128::from(untagged_variant.as_u32()),
169+
);
191170
let discr = fx.bcx.ins().select(is_niche, tagged_discr, untagged_variant);
192171
let res = CValue::by_val(discr, dest_layout);
193172
dest.write_cvalue(fx, res);

src/main_shim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub(crate) fn maybe_create_entry_wrapper(
9191
bcx.switch_to_block(block);
9292
let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type());
9393
let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type());
94-
let arg_sigpipe = bcx.ins().iconst(types::I8, sigpipe as i64);
94+
let arg_sigpipe = bcx.ins().iconst(types::I8, i64::from(sigpipe));
9595

9696
let main_func_ref = m.declare_func_in_func(main_func_id, bcx.func);
9797

src/unsize.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ pub(crate) fn unsized_info<'tcx>(
2828
match (&source.kind(), &target.kind()) {
2929
(&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst(
3030
fx.pointer_type,
31-
len.try_to_target_usize(fx.tcx).expect("expected monomorphic const in codegen") as i64,
31+
len.try_to_target_usize(fx.tcx)
32+
.expect("expected monomorphic const in codegen")
33+
.cast_signed(),
3234
),
3335
(&ty::Dynamic(data_a, _), &ty::Dynamic(data_b, _)) => {
3436
let old_info =
@@ -171,8 +173,8 @@ pub(crate) fn size_and_align_of<'tcx>(
171173
) -> (Value, Value) {
172174
if layout.is_sized() {
173175
return (
174-
fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64),
175-
fx.bcx.ins().iconst(fx.pointer_type, layout.align.bytes() as i64),
176+
fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes().cast_signed()),
177+
fx.bcx.ins().iconst(fx.pointer_type, layout.align.bytes().cast_signed()),
176178
);
177179
}
178180

@@ -190,8 +192,8 @@ pub(crate) fn size_and_align_of<'tcx>(
190192
// The info in this case is the length of the str, so the size is that
191193
// times the unit size.
192194
(
193-
fx.bcx.ins().imul_imm(info.unwrap(), unit.size.bytes() as i64),
194-
fx.bcx.ins().iconst(fx.pointer_type, unit.align.bytes() as i64),
195+
fx.bcx.ins().imul_imm(info.unwrap(), unit.size.bytes().cast_signed()),
196+
fx.bcx.ins().iconst(fx.pointer_type, unit.align.bytes().cast_signed()),
195197
)
196198
}
197199
ty::Foreign(_) => {
@@ -228,9 +230,9 @@ pub(crate) fn size_and_align_of<'tcx>(
228230
let i = layout.fields.count() - 1;
229231
let unsized_offset_unadjusted = layout.fields.offset(i).bytes();
230232
let unsized_offset_unadjusted =
231-
fx.bcx.ins().iconst(fx.pointer_type, unsized_offset_unadjusted as i64);
233+
fx.bcx.ins().iconst(fx.pointer_type, unsized_offset_unadjusted.cast_signed());
232234
let sized_align = layout.align.bytes();
233-
let sized_align = fx.bcx.ins().iconst(fx.pointer_type, sized_align as i64);
235+
let sized_align = fx.bcx.ins().iconst(fx.pointer_type, sized_align.cast_signed());
234236

235237
// Recurse to get the size of the dynamically sized field (must be
236238
// the last field).
@@ -247,7 +249,8 @@ pub(crate) fn size_and_align_of<'tcx>(
247249
unsized_align = fx.bcx.ins().iconst(fx.pointer_type, 1);
248250
} else {
249251
// We have to dynamically compute `min(unsized_align, packed)`.
250-
let packed = fx.bcx.ins().iconst(fx.pointer_type, packed.bytes() as i64);
252+
let packed =
253+
fx.bcx.ins().iconst(fx.pointer_type, packed.bytes().cast_signed());
251254
let cmp = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, unsized_align, packed);
252255
unsized_align = fx.bcx.ins().select(cmp, unsized_align, packed);
253256
}

src/value_and_place.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn codegen_field<'tcx>(
3636
// For packed types, we need to cap alignment.
3737
if let ty::Adt(def, _) = layout.ty.kind() {
3838
if let Some(packed) = def.repr().pack {
39-
let packed = fx.bcx.ins().iconst(fx.pointer_type, packed.bytes() as i64);
39+
let packed = fx.bcx.ins().iconst(fx.pointer_type, packed.bytes().cast_signed());
4040
let cmp = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, unsized_align, packed);
4141
unsized_align = fx.bcx.ins().select(cmp, unsized_align, packed);
4242
}
@@ -295,18 +295,14 @@ impl<'tcx> CValue<'tcx> {
295295
let clif_ty = fx.clif_type(layout.ty).unwrap();
296296

297297
let val = match layout.ty.kind() {
298-
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
299-
codegen_iconst_u128(&mut fx.bcx, const_val.to_bits(layout.size))
300-
}
301298
ty::Bool
302299
| ty::Char
303300
| ty::Uint(_)
304301
| ty::Int(_)
305302
| ty::Ref(..)
306303
| ty::RawPtr(..)
307304
| ty::FnPtr(..) => {
308-
let raw_val = const_val.size().truncate(const_val.to_bits(layout.size));
309-
fx.bcx.ins().iconst(clif_ty, raw_val as i64)
305+
codegen_iconst_unsigned(&mut fx.bcx, clif_ty, const_val.to_bits(layout.size))
310306
}
311307
ty::Float(FloatTy::F16) => {
312308
fx.bcx.ins().f16const(Ieee16::with_bits(u16::from(const_val)))

0 commit comments

Comments
 (0)