2021-12-20 17:56:35 +00:00
|
|
|
use cranelift_codegen::isa::TargetFrontendConfig;
|
2024-04-21 17:35:09 +00:00
|
|
|
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::IndexVec;
|
2021-09-01 21:09:34 +00:00
|
|
|
use rustc_middle::ty::layout::{
|
2024-04-21 17:35:09 +00:00
|
|
|
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
|
2021-09-01 21:09:34 +00:00
|
|
|
};
|
2024-04-21 17:35:09 +00:00
|
|
|
use rustc_middle::ty::TypeFoldable;
|
2023-05-17 10:30:14 +00:00
|
|
|
use rustc_span::source_map::Spanned;
|
2021-02-01 09:11:46 +00:00
|
|
|
use rustc_target::abi::call::FnAbi;
|
2024-05-06 12:27:40 +00:00
|
|
|
use rustc_target::abi::{Float, Integer, Primitive};
|
2018-11-07 12:29:38 +00:00
|
|
|
use rustc_target::spec::{HasTargetSpec, Target};
|
2018-06-22 17:18:53 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
use crate::constant::ConstantCx;
|
2022-08-24 16:40:58 +00:00
|
|
|
use crate::debuginfo::FunctionDebugContext;
|
2018-07-30 14:57:40 +00:00
|
|
|
use crate::prelude::*;
|
2018-06-22 17:18:53 +00:00
|
|
|
|
2020-04-05 11:48:26 +00:00
|
|
|
pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type {
|
2018-08-18 15:10:02 +00:00
|
|
|
match tcx.data_layout.pointer_size.bits() {
|
|
|
|
16 => types::I16,
|
|
|
|
32 => types::I32,
|
|
|
|
64 => types::I64,
|
|
|
|
bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 11:48:26 +00:00
|
|
|
pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type {
|
2022-03-03 12:02:12 +00:00
|
|
|
match scalar.primitive() {
|
2019-08-30 09:58:52 +00:00
|
|
|
Primitive::Int(int, _sign) => match int {
|
|
|
|
Integer::I8 => types::I8,
|
|
|
|
Integer::I16 => types::I16,
|
|
|
|
Integer::I32 => types::I32,
|
|
|
|
Integer::I64 => types::I64,
|
|
|
|
Integer::I128 => types::I128,
|
|
|
|
},
|
2024-05-06 12:27:40 +00:00
|
|
|
Primitive::Float(float) => match float {
|
|
|
|
Float::F16 => unimplemented!("f16_f128"),
|
|
|
|
Float::F32 => types::F32,
|
|
|
|
Float::F64 => types::F64,
|
|
|
|
Float::F128 => unimplemented!("f16_f128"),
|
|
|
|
},
|
2023-01-23 04:03:58 +00:00
|
|
|
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
|
|
|
|
Primitive::Pointer(_) => pointer_ty(tcx),
|
2019-08-30 09:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:11:06 +00:00
|
|
|
fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
|
2020-09-05 08:38:49 +00:00
|
|
|
Some(match ty.kind() {
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::Bool => types::I8,
|
|
|
|
ty::Uint(size) => match size {
|
2018-07-31 10:25:16 +00:00
|
|
|
UintTy::U8 => types::I8,
|
|
|
|
UintTy::U16 => types::I16,
|
|
|
|
UintTy::U32 => types::I32,
|
|
|
|
UintTy::U64 => types::I64,
|
2019-06-12 18:54:38 +00:00
|
|
|
UintTy::U128 => types::I128,
|
2018-08-18 15:10:02 +00:00
|
|
|
UintTy::Usize => pointer_ty(tcx),
|
2018-07-31 10:25:16 +00:00
|
|
|
},
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::Int(size) => match size {
|
2018-07-31 10:25:16 +00:00
|
|
|
IntTy::I8 => types::I8,
|
|
|
|
IntTy::I16 => types::I16,
|
|
|
|
IntTy::I32 => types::I32,
|
|
|
|
IntTy::I64 => types::I64,
|
2019-06-12 18:54:38 +00:00
|
|
|
IntTy::I128 => types::I128,
|
2018-08-22 10:31:45 +00:00
|
|
|
IntTy::Isize => pointer_ty(tcx),
|
2018-07-31 10:25:16 +00:00
|
|
|
},
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::Char => types::I32,
|
|
|
|
ty::Float(size) => match size {
|
2024-02-28 08:44:23 +00:00
|
|
|
FloatTy::F16 => unimplemented!("f16_f128"),
|
2018-08-08 08:26:25 +00:00
|
|
|
FloatTy::F32 => types::F32,
|
|
|
|
FloatTy::F64 => types::F64,
|
2024-02-28 08:44:23 +00:00
|
|
|
FloatTy::F128 => unimplemented!("f16_f128"),
|
2018-07-31 10:25:16 +00:00
|
|
|
},
|
2018-08-24 12:51:02 +00:00
|
|
|
ty::FnPtr(_) => pointer_ty(tcx),
|
2024-03-21 21:11:06 +00:00
|
|
|
ty::RawPtr(pointee_ty, _) | ty::Ref(_, pointee_ty, _) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
if has_ptr_meta(tcx, *pointee_ty) {
|
2018-07-14 14:39:49 +00:00
|
|
|
return None;
|
2019-09-14 15:53:36 +00:00
|
|
|
} else {
|
|
|
|
pointer_ty(tcx)
|
2018-07-14 14:39:49 +00:00
|
|
|
}
|
2020-03-27 19:55:54 +00:00
|
|
|
}
|
2019-03-16 09:54:02 +00:00
|
|
|
ty::Param(_) => bug!("ty param {:?}", ty),
|
2018-06-22 17:18:53 +00:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
fn clif_pair_type_from_ty<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<(types::Type, types::Type)> {
|
2020-09-05 08:38:49 +00:00
|
|
|
Some(match ty.kind() {
|
2022-02-07 15:06:31 +00:00
|
|
|
ty::Tuple(types) if types.len() == 2 => {
|
2023-04-29 12:00:43 +00:00
|
|
|
(clif_type_from_ty(tcx, types[0])?, clif_type_from_ty(tcx, types[1])?)
|
2020-07-02 22:23:21 +00:00
|
|
|
}
|
2024-03-21 21:11:06 +00:00
|
|
|
ty::RawPtr(pointee_ty, _) | ty::Ref(_, pointee_ty, _) => {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
if has_ptr_meta(tcx, *pointee_ty) {
|
2020-07-03 12:48:19 +00:00
|
|
|
(pointer_ty(tcx), pointer_ty(tcx))
|
|
|
|
} else {
|
2020-08-28 10:10:48 +00:00
|
|
|
return None;
|
2020-07-03 12:48:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-02 22:23:21 +00:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:53:36 +00:00
|
|
|
/// Is a pointer to this type a fat ptr?
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
|
2023-12-19 12:46:39 +00:00
|
|
|
if ty.is_sized(tcx, ParamEnv::reveal_all()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tail = tcx.struct_tail_erasing_lifetimes(ty, ParamEnv::reveal_all());
|
|
|
|
match tail.kind() {
|
|
|
|
ty::Foreign(..) => false,
|
|
|
|
ty::Str | ty::Slice(..) | ty::Dynamic(..) => true,
|
|
|
|
_ => bug!("unexpected unsized tail: {:?}", tail),
|
2019-09-14 15:53:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn codegen_icmp_imm(
|
2021-03-05 18:12:59 +00:00
|
|
|
fx: &mut FunctionCx<'_, '_, '_>,
|
2019-08-15 09:36:06 +00:00
|
|
|
intcc: IntCC,
|
|
|
|
lhs: Value,
|
|
|
|
rhs: i128,
|
|
|
|
) -> Value {
|
|
|
|
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
|
|
|
|
if lhs_ty == types::I128 {
|
|
|
|
// FIXME legalize `icmp_imm.i128` in Cranelift
|
|
|
|
|
|
|
|
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
|
|
|
|
let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64);
|
|
|
|
|
|
|
|
match intcc {
|
|
|
|
IntCC::Equal => {
|
|
|
|
let lsb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().band(lsb_eq, msb_eq)
|
|
|
|
}
|
|
|
|
IntCC::NotEqual => {
|
|
|
|
let lsb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().bor(lsb_ne, msb_ne)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// if msb_eq {
|
|
|
|
// lsb_cc
|
|
|
|
// } else {
|
|
|
|
// msb_cc
|
|
|
|
// }
|
|
|
|
|
|
|
|
let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
let lsb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_msb, rhs_msb);
|
|
|
|
|
|
|
|
fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-14 18:30:46 +00:00
|
|
|
let rhs = rhs as i64; // Truncates on purpose in case rhs is actually an unsigned value
|
2019-08-15 09:36:06 +00:00
|
|
|
fx.bcx.ins().icmp_imm(intcc, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 17:56:42 +00:00
|
|
|
pub(crate) fn codegen_bitcast(fx: &mut FunctionCx<'_, '_, '_>, dst_ty: Type, val: Value) -> Value {
|
|
|
|
let mut flags = MemFlags::new();
|
|
|
|
flags.set_endianness(match fx.tcx.data_layout.endian {
|
|
|
|
rustc_target::abi::Endian::Big => cranelift_codegen::ir::Endianness::Big,
|
|
|
|
rustc_target::abi::Endian::Little => cranelift_codegen::ir::Endianness::Little,
|
|
|
|
});
|
|
|
|
fx.bcx.ins().bitcast(dst_ty, flags, val)
|
|
|
|
}
|
|
|
|
|
2022-12-14 18:30:46 +00:00
|
|
|
pub(crate) fn type_zero_value(bcx: &mut FunctionBuilder<'_>, ty: Type) -> Value {
|
|
|
|
if ty == types::I128 {
|
|
|
|
let zero = bcx.ins().iconst(types::I64, 0);
|
|
|
|
bcx.ins().iconcat(zero, zero)
|
|
|
|
} else {
|
|
|
|
bcx.ins().iconst(ty, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
pub(crate) fn type_min_max_value(
|
|
|
|
bcx: &mut FunctionBuilder<'_>,
|
|
|
|
ty: Type,
|
|
|
|
signed: bool,
|
|
|
|
) -> (Value, Value) {
|
2019-08-12 15:25:16 +00:00
|
|
|
assert!(ty.is_int());
|
2020-04-17 12:14:18 +00:00
|
|
|
|
|
|
|
if ty == types::I128 {
|
|
|
|
if signed {
|
|
|
|
let min = i128::MIN as u128;
|
|
|
|
let min_lsb = bcx.ins().iconst(types::I64, min as u64 as i64);
|
|
|
|
let min_msb = bcx.ins().iconst(types::I64, (min >> 64) as u64 as i64);
|
|
|
|
let min = bcx.ins().iconcat(min_lsb, min_msb);
|
|
|
|
|
2020-11-27 19:48:53 +00:00
|
|
|
let max = i128::MAX as u128;
|
2020-04-17 12:14:18 +00:00
|
|
|
let max_lsb = bcx.ins().iconst(types::I64, max as u64 as i64);
|
|
|
|
let max_msb = bcx.ins().iconst(types::I64, (max >> 64) as u64 as i64);
|
|
|
|
let max = bcx.ins().iconcat(max_lsb, max_msb);
|
|
|
|
|
|
|
|
return (min, max);
|
|
|
|
} else {
|
|
|
|
let min_half = bcx.ins().iconst(types::I64, 0);
|
|
|
|
let min = bcx.ins().iconcat(min_half, min_half);
|
|
|
|
|
|
|
|
let max_half = bcx.ins().iconst(types::I64, u64::MAX as i64);
|
|
|
|
let max = bcx.ins().iconcat(max_half, max_half);
|
|
|
|
|
|
|
|
return (min, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 15:25:16 +00:00
|
|
|
let min = match (ty, signed) {
|
2019-08-31 17:28:09 +00:00
|
|
|
(types::I8, false) | (types::I16, false) | (types::I32, false) | (types::I64, false) => {
|
|
|
|
0i64
|
|
|
|
}
|
2023-10-09 08:52:46 +00:00
|
|
|
(types::I8, true) => i64::from(i8::MIN as u8),
|
|
|
|
(types::I16, true) => i64::from(i16::MIN as u16),
|
|
|
|
(types::I32, true) => i64::from(i32::MIN as u32),
|
2020-03-20 15:29:05 +00:00
|
|
|
(types::I64, true) => i64::MIN,
|
2019-08-08 13:54:13 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2019-08-12 15:25:16 +00:00
|
|
|
let max = match (ty, signed) {
|
2020-07-14 14:38:50 +00:00
|
|
|
(types::I8, false) => i64::from(u8::MAX),
|
|
|
|
(types::I16, false) => i64::from(u16::MAX),
|
|
|
|
(types::I32, false) => i64::from(u32::MAX),
|
2020-03-20 15:29:05 +00:00
|
|
|
(types::I64, false) => u64::MAX as i64,
|
2023-10-09 08:52:46 +00:00
|
|
|
(types::I8, true) => i64::from(i8::MAX as u8),
|
|
|
|
(types::I16, true) => i64::from(i16::MAX as u16),
|
|
|
|
(types::I32, true) => i64::from(i32::MAX as u32),
|
2020-03-20 15:29:05 +00:00
|
|
|
(types::I64, true) => i64::MAX,
|
2019-08-08 13:54:13 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2020-04-17 12:14:18 +00:00
|
|
|
let (min, max) = (bcx.ins().iconst(ty, min), bcx.ins().iconst(ty, max));
|
|
|
|
|
2019-08-08 13:54:13 +00:00
|
|
|
(min, max)
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn type_sign(ty: Ty<'_>) -> bool {
|
2020-09-05 08:38:49 +00:00
|
|
|
match ty.kind() {
|
2019-08-12 15:25:16 +00:00
|
|
|
ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
|
|
|
|
ty::Int(..) => true,
|
|
|
|
ty::Float(..) => false, // `signed` is unused for floats
|
|
|
|
_ => panic!("{}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:38:16 +00:00
|
|
|
pub(crate) fn create_wrapper_function(
|
|
|
|
module: &mut dyn Module,
|
|
|
|
unwind_context: &mut UnwindContext,
|
|
|
|
sig: Signature,
|
|
|
|
wrapper_name: &str,
|
|
|
|
callee_name: &str,
|
|
|
|
) {
|
|
|
|
let wrapper_func_id = module.declare_function(wrapper_name, Linkage::Export, &sig).unwrap();
|
|
|
|
let callee_func_id = module.declare_function(callee_name, Linkage::Import, &sig).unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.func.signature = sig;
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
|
|
|
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
|
|
|
let func = &mut bcx.func.stencil;
|
|
|
|
let args = func
|
|
|
|
.signature
|
|
|
|
.params
|
|
|
|
.iter()
|
|
|
|
.map(|param| func.dfg.append_block_param(block, param.value_type))
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
|
|
|
|
let callee_func_ref = module.declare_func_in_func(callee_func_id, &mut bcx.func);
|
|
|
|
let call_inst = bcx.ins().call(callee_func_ref, &args);
|
|
|
|
let results = bcx.inst_results(call_inst).to_vec(); // Clone to prevent borrow error
|
|
|
|
|
|
|
|
bcx.ins().return_(&results);
|
|
|
|
bcx.seal_all_blocks();
|
|
|
|
bcx.finalize();
|
|
|
|
}
|
|
|
|
module.define_function(wrapper_func_id, &mut ctx).unwrap();
|
|
|
|
unwind_context.add_function(wrapper_func_id, &ctx, module.isa());
|
|
|
|
}
|
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
|
2022-08-24 16:40:58 +00:00
|
|
|
pub(crate) cx: &'clif mut crate::CodegenCx,
|
2021-04-30 12:49:58 +00:00
|
|
|
pub(crate) module: &'m mut dyn Module,
|
2020-08-22 14:45:50 +00:00
|
|
|
pub(crate) tcx: TyCtxt<'tcx>,
|
2021-12-20 17:56:35 +00:00
|
|
|
pub(crate) target_config: TargetFrontendConfig, // Cached from module
|
2021-12-30 13:53:41 +00:00
|
|
|
pub(crate) pointer_type: Type, // Cached from module
|
2021-04-30 12:49:58 +00:00
|
|
|
pub(crate) constants_cx: ConstantCx,
|
2022-08-24 16:40:58 +00:00
|
|
|
pub(crate) func_debug_cx: Option<FunctionDebugContext>,
|
2018-12-01 10:49:44 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) instance: Instance<'tcx>,
|
2022-08-24 16:40:58 +00:00
|
|
|
pub(crate) symbol_name: String,
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) mir: &'tcx Body<'tcx>,
|
2024-03-27 19:49:20 +00:00
|
|
|
pub(crate) fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
|
2018-12-01 10:49:44 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) bcx: FunctionBuilder<'clif>,
|
|
|
|
pub(crate) block_map: IndexVec<BasicBlock, Block>,
|
2020-09-16 16:45:19 +00:00
|
|
|
pub(crate) local_map: IndexVec<Local, CPlace<'tcx>>,
|
2018-12-01 10:49:44 +00:00
|
|
|
|
2020-01-11 15:49:42 +00:00
|
|
|
/// When `#[track_caller]` is used, the implicit caller location is stored in this variable.
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) caller_location: Option<CValue<'tcx>>,
|
2020-01-11 15:49:42 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) clif_comments: crate::pretty_clif::CommentWriter,
|
2022-08-24 16:40:58 +00:00
|
|
|
|
2020-06-27 09:58:44 +00:00
|
|
|
/// This should only be accessed by `CPlace::new_var`.
|
|
|
|
pub(crate) next_ssa_var: u32,
|
2018-06-22 17:18:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 17:37:36 +00:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> {
|
2021-08-30 14:38:27 +00:00
|
|
|
type LayoutOfResult = TyAndLayout<'tcx>;
|
2018-06-24 12:01:41 +00:00
|
|
|
|
2021-08-30 15:01:58 +00:00
|
|
|
#[inline]
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
|
|
|
|
RevealAllLayoutCx(self.tcx).handle_layout_err(err, span, ty)
|
2018-06-24 12:01:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:09:34 +00:00
|
|
|
impl<'tcx> FnAbiOfHelpers<'tcx> for FunctionCx<'_, '_, 'tcx> {
|
|
|
|
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn handle_fn_abi_err(
|
|
|
|
&self,
|
2022-02-01 17:44:45 +00:00
|
|
|
err: FnAbiError<'tcx>,
|
2021-09-01 21:09:34 +00:00
|
|
|
span: Span,
|
2021-09-01 21:29:15 +00:00
|
|
|
fn_abi_request: FnAbiRequest<'tcx>,
|
2021-09-01 21:09:34 +00:00
|
|
|
) -> ! {
|
|
|
|
RevealAllLayoutCx(self.tcx).handle_fn_abi_err(err, span, fn_abi_request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
impl<'tcx> layout::HasTyCtxt<'tcx> for FunctionCx<'_, '_, 'tcx> {
|
2019-06-16 09:13:49 +00:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2020-08-22 14:47:31 +00:00
|
|
|
self.tcx
|
2018-06-24 12:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
impl<'tcx> rustc_target::abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> {
|
2020-04-03 09:54:18 +00:00
|
|
|
fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
|
2020-08-22 14:47:31 +00:00
|
|
|
&self.tcx.data_layout
|
2018-06-24 12:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
impl<'tcx> layout::HasParamEnv<'tcx> for FunctionCx<'_, '_, 'tcx> {
|
2019-05-08 18:05:01 +00:00
|
|
|
fn param_env(&self) -> ParamEnv<'tcx> {
|
|
|
|
ParamEnv::reveal_all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
impl<'tcx> HasTargetSpec for FunctionCx<'_, '_, 'tcx> {
|
2018-06-24 12:25:29 +00:00
|
|
|
fn target_spec(&self) -> &Target {
|
2020-10-16 07:35:48 +00:00
|
|
|
&self.tcx.sess.target
|
2018-06-24 12:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:12:59 +00:00
|
|
|
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
|
2020-10-28 07:25:06 +00:00
|
|
|
pub(crate) fn monomorphize<T>(&self, value: T) -> T
|
2018-07-31 10:25:16 +00:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
|
2018-06-23 16:54:15 +00:00
|
|
|
{
|
2023-09-25 13:46:38 +00:00
|
|
|
self.instance.instantiate_mir_and_normalize_erasing_regions(
|
2020-11-06 00:00:00 +00:00
|
|
|
self.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
2023-05-29 11:46:10 +00:00
|
|
|
ty::EarlyBinder::bind(value),
|
2020-11-06 00:00:00 +00:00
|
|
|
)
|
2018-06-23 16:54:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
|
2020-08-22 14:47:31 +00:00
|
|
|
clif_type_from_ty(self.tcx, ty)
|
2018-06-24 12:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 22:23:21 +00:00
|
|
|
pub(crate) fn clif_pair_type(&self, ty: Ty<'tcx>) -> Option<(Type, Type)> {
|
2020-08-22 14:47:31 +00:00
|
|
|
clif_pair_type_from_ty(self.tcx, ty)
|
2020-07-02 22:23:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn get_block(&self, bb: BasicBlock) -> Block {
|
2020-02-14 17:23:29 +00:00
|
|
|
*self.block_map.get(bb).unwrap()
|
2018-06-22 17:18:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
|
2020-09-16 16:45:19 +00:00
|
|
|
*self.local_map.get(local).unwrap_or_else(|| {
|
2020-03-10 19:41:31 +00:00
|
|
|
panic!("Local {:?} doesn't exist", local);
|
|
|
|
})
|
2018-06-22 17:18:53 +00:00
|
|
|
}
|
2019-01-17 17:07:27 +00:00
|
|
|
|
2023-10-24 12:22:23 +00:00
|
|
|
pub(crate) fn create_stack_slot(&mut self, size: u32, align: u32) -> Pointer {
|
2024-03-08 20:41:29 +00:00
|
|
|
let abi_align = if self.tcx.sess.target.arch == "s390x" { 8 } else { 16 };
|
|
|
|
if align <= abi_align {
|
2023-10-24 12:22:23 +00:00
|
|
|
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
2024-03-08 20:41:29 +00:00
|
|
|
// FIXME Don't force the size to a multiple of <abi_align> bytes once Cranelift gets
|
|
|
|
// a way to specify stack slot alignment.
|
|
|
|
size: (size + abi_align - 1) / abi_align * abi_align,
|
2023-10-24 12:22:23 +00:00
|
|
|
});
|
|
|
|
Pointer::stack_slot(stack_slot)
|
|
|
|
} else {
|
|
|
|
// Alignment is too big to handle using the above hack. Dynamically realign a stack slot
|
|
|
|
// instead. This wastes some space for the realignment.
|
2024-03-08 20:41:29 +00:00
|
|
|
let stack_slot = self.bcx.create_sized_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
// FIXME Don't force the size to a multiple of <abi_align> bytes once Cranelift gets
|
|
|
|
// a way to specify stack slot alignment.
|
|
|
|
size: (size + align) / abi_align * abi_align,
|
|
|
|
});
|
|
|
|
let base_ptr = self.bcx.ins().stack_addr(self.pointer_type, stack_slot, 0);
|
2023-10-24 12:22:23 +00:00
|
|
|
let misalign_offset = self.bcx.ins().urem_imm(base_ptr, i64::from(align));
|
|
|
|
let realign_offset = self.bcx.ins().irsub_imm(misalign_offset, i64::from(align));
|
|
|
|
Pointer::new(self.bcx.ins().iadd(base_ptr, realign_offset))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
|
2022-08-24 16:40:58 +00:00
|
|
|
if let Some(debug_context) = &mut self.cx.debug_context {
|
2024-03-27 14:14:30 +00:00
|
|
|
let (file_id, line, column) =
|
|
|
|
debug_context.get_span_loc(self.tcx, self.mir.span, source_info.span);
|
2022-08-24 16:40:58 +00:00
|
|
|
|
|
|
|
let source_loc =
|
|
|
|
self.func_debug_cx.as_mut().unwrap().add_dbg_loc(file_id, line, column);
|
|
|
|
self.bcx.set_srcloc(source_loc);
|
|
|
|
}
|
2019-01-17 17:07:27 +00:00
|
|
|
}
|
2019-11-09 10:14:18 +00:00
|
|
|
|
2023-10-28 14:16:15 +00:00
|
|
|
pub(crate) fn get_caller_location(&mut self, source_info: mir::SourceInfo) -> CValue<'tcx> {
|
|
|
|
self.mir.caller_location_span(source_info, self.caller_location, self.tcx, |span| {
|
|
|
|
let const_loc = self.tcx.span_as_caller_location(span);
|
|
|
|
crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty())
|
|
|
|
})
|
2019-11-09 10:14:18 +00:00
|
|
|
}
|
2019-12-17 14:03:32 +00:00
|
|
|
|
2021-05-27 11:08:14 +00:00
|
|
|
pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value {
|
2023-06-15 17:56:01 +00:00
|
|
|
let mut data = DataDescription::new();
|
|
|
|
data.define(msg.as_bytes().to_vec().into_boxed_slice());
|
2021-05-27 11:08:14 +00:00
|
|
|
let msg_id = self.module.declare_anonymous_data(false, false).unwrap();
|
2020-04-25 17:07:25 +00:00
|
|
|
|
|
|
|
// Ignore DuplicateDefinition error, as the data will be the same
|
2023-06-15 17:56:01 +00:00
|
|
|
let _ = self.module.define_data(msg_id, &data);
|
2020-04-25 17:07:25 +00:00
|
|
|
|
2021-04-30 12:49:58 +00:00
|
|
|
let local_msg_id = self.module.declare_data_in_func(msg_id, self.bcx.func);
|
2021-03-29 08:45:09 +00:00
|
|
|
if self.clif_comments.enabled() {
|
2020-04-25 17:07:25 +00:00
|
|
|
self.add_comment(local_msg_id, msg);
|
|
|
|
}
|
|
|
|
self.bcx.ins().global_value(self.pointer_type, local_msg_id)
|
|
|
|
}
|
2018-06-22 17:18:53 +00:00
|
|
|
}
|
2021-02-01 09:11:46 +00:00
|
|
|
|
|
|
|
pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
|
|
|
|
|
2021-08-30 17:37:36 +00:00
|
|
|
impl<'tcx> LayoutOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
|
2021-08-30 14:38:27 +00:00
|
|
|
type LayoutOfResult = TyAndLayout<'tcx>;
|
2021-02-01 09:11:46 +00:00
|
|
|
|
2021-08-30 15:01:58 +00:00
|
|
|
#[inline]
|
|
|
|
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
|
2023-08-04 05:28:04 +00:00
|
|
|
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.0.sess.dcx().span_fatal(span, err.to_string())
|
2021-08-30 15:01:58 +00:00
|
|
|
} else {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.0
|
|
|
|
.sess
|
|
|
|
.dcx()
|
|
|
|
.span_fatal(span, format!("failed to get layout for `{}`: {}", ty, err))
|
2021-08-30 15:01:58 +00:00
|
|
|
}
|
2021-02-01 09:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 21:09:34 +00:00
|
|
|
impl<'tcx> FnAbiOfHelpers<'tcx> for RevealAllLayoutCx<'tcx> {
|
|
|
|
type FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn handle_fn_abi_err(
|
|
|
|
&self,
|
2022-02-01 17:44:45 +00:00
|
|
|
err: FnAbiError<'tcx>,
|
2021-09-01 21:09:34 +00:00
|
|
|
span: Span,
|
2021-09-01 21:29:15 +00:00
|
|
|
fn_abi_request: FnAbiRequest<'tcx>,
|
2021-09-01 21:09:34 +00:00
|
|
|
) -> ! {
|
|
|
|
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.0.sess.dcx().emit_fatal(Spanned { span, node: err })
|
2021-09-01 21:09:34 +00:00
|
|
|
} else {
|
|
|
|
match fn_abi_request {
|
|
|
|
FnAbiRequest::OfFnPtr { sig, extra_args } => {
|
2023-05-17 10:30:14 +00:00
|
|
|
span_bug!(span, "`fn_abi_of_fn_ptr({sig}, {extra_args:?})` failed: {err:?}");
|
2021-09-01 21:09:34 +00:00
|
|
|
}
|
|
|
|
FnAbiRequest::OfInstance { instance, extra_args } => {
|
|
|
|
span_bug!(
|
|
|
|
span,
|
2023-05-17 10:30:14 +00:00
|
|
|
"`fn_abi_of_instance({instance}, {extra_args:?})` failed: {err:?}"
|
2021-09-01 21:09:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 09:11:46 +00:00
|
|
|
impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> {
|
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> rustc_target::abi::HasDataLayout for RevealAllLayoutCx<'tcx> {
|
|
|
|
fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout {
|
|
|
|
&self.0.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> {
|
|
|
|
fn param_env(&self) -> ParamEnv<'tcx> {
|
|
|
|
ParamEnv::reveal_all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> {
|
|
|
|
fn target_spec(&self) -> &Target {
|
|
|
|
&self.0.sess.target
|
|
|
|
}
|
|
|
|
}
|