2020-12-12 15:32:30 +01:00
|
|
|
use rustc_ast::InlineAsmTemplatePiece;
|
2023-02-21 15:15:16 +01:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2024-06-14 14:46:32 -04:00
|
|
|
use rustc_hir::{self as hir, LangItem};
|
2024-05-08 16:40:46 +10:00
|
|
|
use rustc_middle::bug;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
|
2020-02-13 11:00:55 +00:00
|
|
|
use rustc_session::lint;
|
2022-11-05 15:33:58 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2024-01-04 12:54:23 +11:00
|
|
|
use rustc_span::Symbol;
|
2023-03-28 23:32:25 -07:00
|
|
|
use rustc_target::abi::FieldIdx;
|
2024-03-03 09:34:26 -05:00
|
|
|
use rustc_target::asm::{
|
|
|
|
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
|
|
|
|
};
|
2014-06-12 14:08:44 -07:00
|
|
|
|
2022-08-13 19:57:22 +02:00
|
|
|
pub struct InlineAsmCtxt<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
|
|
|
pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
|
|
|
|
InlineAsmCtxt {
|
|
|
|
tcx,
|
|
|
|
param_env: ty::ParamEnv::empty(),
|
|
|
|
get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_in_fn(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
|
|
|
|
) -> Self {
|
|
|
|
InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
|
|
|
|
}
|
2020-02-13 11:00:55 +00:00
|
|
|
|
2022-07-10 18:55:38 +00:00
|
|
|
// FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
|
2020-02-13 11:00:55 +00:00
|
|
|
fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
|
2022-07-10 18:55:38 +00:00
|
|
|
// Type still may have region variables, but `Sized` does not depend
|
|
|
|
// on those, so just erase them before querying.
|
2022-10-27 14:45:02 +04:00
|
|
|
if ty.is_sized(self.tcx, self.param_env) {
|
2020-02-13 11:00:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-03 00:49:11 +02:00
|
|
|
if let ty::Foreign(..) = ty.kind() {
|
2020-02-13 11:00:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-08-23 21:57:18 +09:00
|
|
|
fn get_asm_ty(&self, ty: Ty<'tcx>) -> Option<InlineAsmType> {
|
2020-10-15 11:44:00 +02:00
|
|
|
let asm_ty_isize = match self.tcx.sess.target.pointer_width {
|
2020-02-13 11:00:55 +00:00
|
|
|
16 => InlineAsmType::I16,
|
|
|
|
32 => InlineAsmType::I32,
|
|
|
|
64 => InlineAsmType::I64,
|
2023-12-15 03:19:46 +00:00
|
|
|
width => bug!("unsupported pointer width: {width}"),
|
2020-02-13 11:00:55 +00:00
|
|
|
};
|
2022-07-10 18:55:38 +00:00
|
|
|
|
2023-08-23 21:57:18 +09:00
|
|
|
match *ty.kind() {
|
2020-02-13 11:00:55 +00:00
|
|
|
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
|
|
|
|
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
|
|
|
|
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
|
|
|
|
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
|
|
|
|
ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
|
|
|
|
ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
|
2024-06-13 16:09:45 +01:00
|
|
|
ty::Float(FloatTy::F16) => Some(InlineAsmType::F16),
|
2020-02-13 11:00:55 +00:00
|
|
|
ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
|
|
|
|
ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
|
2024-06-13 16:09:45 +01:00
|
|
|
ty::Float(FloatTy::F128) => Some(InlineAsmType::F128),
|
2020-02-13 11:00:55 +00:00
|
|
|
ty::FnPtr(_) => Some(asm_ty_isize),
|
2024-03-21 17:11:06 -04:00
|
|
|
ty::RawPtr(ty, _) if self.is_thin_ptr_ty(ty) => Some(asm_ty_isize),
|
2023-07-11 22:35:29 +01:00
|
|
|
ty::Adt(adt, args) if adt.repr().simd() => {
|
2020-02-13 11:00:55 +00:00
|
|
|
let fields = &adt.non_enum_variant().fields;
|
2024-04-03 17:49:59 +03:00
|
|
|
let elem_ty = fields[FieldIdx::ZERO].ty(self.tcx, args);
|
2023-04-22 16:55:59 +12:00
|
|
|
|
|
|
|
let (size, ty) = match elem_ty.kind() {
|
|
|
|
ty::Array(ty, len) => {
|
|
|
|
if let Some(len) =
|
|
|
|
len.try_eval_target_usize(self.tcx, self.tcx.param_env(adt.did()))
|
|
|
|
{
|
|
|
|
(len, *ty)
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
2023-04-22 16:55:59 +12:00
|
|
|
_ => (fields.len() as u64, elem_ty),
|
|
|
|
};
|
|
|
|
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::VecI8(size)),
|
2020-02-13 11:00:55 +00:00
|
|
|
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
|
2023-04-22 16:55:59 +12:00
|
|
|
Some(InlineAsmType::VecI16(size))
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
|
2023-04-22 16:55:59 +12:00
|
|
|
Some(InlineAsmType::VecI32(size))
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
|
2023-04-22 16:55:59 +12:00
|
|
|
Some(InlineAsmType::VecI64(size))
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
|
2023-04-22 16:55:59 +12:00
|
|
|
Some(InlineAsmType::VecI128(size))
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
|
2020-10-15 11:44:00 +02:00
|
|
|
Some(match self.tcx.sess.target.pointer_width {
|
2023-04-22 16:55:59 +12:00
|
|
|
16 => InlineAsmType::VecI16(size),
|
|
|
|
32 => InlineAsmType::VecI32(size),
|
|
|
|
64 => InlineAsmType::VecI64(size),
|
2023-12-15 03:19:46 +00:00
|
|
|
width => bug!("unsupported pointer width: {width}"),
|
2020-02-13 11:00:55 +00:00
|
|
|
})
|
|
|
|
}
|
2024-06-13 16:09:45 +01:00
|
|
|
ty::Float(FloatTy::F16) => Some(InlineAsmType::VecF16(size)),
|
2023-04-22 16:55:59 +12:00
|
|
|
ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(size)),
|
|
|
|
ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(size)),
|
2024-06-13 16:09:45 +01:00
|
|
|
ty::Float(FloatTy::F128) => Some(InlineAsmType::VecF128(size)),
|
2020-02-13 11:00:55 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 03:19:46 +00:00
|
|
|
ty::Infer(_) => bug!("unexpected infer ty in asm operand"),
|
2020-02-13 11:00:55 +00:00
|
|
|
_ => None,
|
2023-08-23 21:57:18 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_asm_operand_type(
|
|
|
|
&self,
|
|
|
|
idx: usize,
|
|
|
|
reg: InlineAsmRegOrRegClass,
|
|
|
|
expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
template: &[InlineAsmTemplatePiece],
|
|
|
|
is_input: bool,
|
|
|
|
tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
|
|
|
|
target_features: &FxIndexSet<Symbol>,
|
|
|
|
) -> Option<InlineAsmType> {
|
|
|
|
let ty = (self.get_operand_ty)(expr);
|
|
|
|
if ty.has_non_region_infer() {
|
|
|
|
bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let asm_ty = match *ty.kind() {
|
|
|
|
// `!` is allowed for input but not for output (issue #87802)
|
|
|
|
ty::Never if is_input => return None,
|
|
|
|
_ if ty.references_error() => return None,
|
2024-06-14 14:46:32 -04:00
|
|
|
ty::Adt(adt, args) if self.tcx.is_lang_item(adt.did(), LangItem::MaybeUninit) => {
|
2023-08-23 21:57:18 +09:00
|
|
|
let fields = &adt.non_enum_variant().fields;
|
|
|
|
let ty = fields[FieldIdx::from_u32(1)].ty(self.tcx, args);
|
2023-12-15 03:19:46 +00:00
|
|
|
// FIXME: Are we just trying to map to the `T` in `MaybeUninit<T>`?
|
|
|
|
// If so, just get it from the args.
|
|
|
|
let ty::Adt(ty, args) = ty.kind() else {
|
|
|
|
unreachable!("expected first field of `MaybeUninit` to be an ADT")
|
|
|
|
};
|
|
|
|
assert!(
|
|
|
|
ty.is_manually_drop(),
|
2024-04-11 15:38:24 +09:00
|
|
|
"expected first field of `MaybeUninit` to be `ManuallyDrop`"
|
2023-12-15 03:19:46 +00:00
|
|
|
);
|
2023-08-23 21:57:18 +09:00
|
|
|
let fields = &ty.non_enum_variant().fields;
|
2024-04-03 17:49:59 +03:00
|
|
|
let ty = fields[FieldIdx::ZERO].ty(self.tcx, args);
|
2023-08-23 21:57:18 +09:00
|
|
|
self.get_asm_ty(ty)
|
|
|
|
}
|
|
|
|
_ => self.get_asm_ty(ty),
|
2020-02-13 11:00:55 +00:00
|
|
|
};
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(asm_ty) = asm_ty else {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
let msg = format!("cannot use value of type `{ty}` for inline assembly");
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(expr.span, msg)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_note(
|
2024-01-03 17:03:10 +11:00
|
|
|
"only integers, floats, SIMD vectors, pointers and function pointers \
|
|
|
|
can be used as arguments for inline assembly",
|
|
|
|
)
|
|
|
|
.emit();
|
2022-02-19 00:48:49 +01:00
|
|
|
return None;
|
2020-02-13 11:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check that the type implements Copy. The only case where this can
|
|
|
|
// possibly fail is for SIMD types which don't #[derive(Copy)].
|
2022-10-27 14:45:02 +04:00
|
|
|
if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
|
2020-02-13 11:00:55 +00:00
|
|
|
let msg = "arguments for inline assembly must be copyable";
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(expr.span, msg)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_note(format!("`{ty}` does not implement the Copy trait"))
|
2024-01-03 17:03:10 +11:00
|
|
|
.emit();
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ideally we wouldn't need to do this, but LLVM's register allocator
|
|
|
|
// really doesn't like it when tied operands have different types.
|
|
|
|
//
|
|
|
|
// This is purely an LLVM limitation, but we have to live with it since
|
|
|
|
// there is no way to hide this with implicit conversions.
|
|
|
|
//
|
|
|
|
// For the purposes of this check we only look at the `InlineAsmType`,
|
|
|
|
// which means that pointers and integers are treated as identical (modulo
|
|
|
|
// size).
|
|
|
|
if let Some((in_expr, Some(in_asm_ty))) = tied_input {
|
|
|
|
if in_asm_ty != asm_ty {
|
2020-06-09 15:57:08 +02:00
|
|
|
let msg = "incompatible types for asm inout argument";
|
2022-08-13 19:57:22 +02:00
|
|
|
let in_expr_ty = (self.get_operand_ty)(in_expr);
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(vec![in_expr.span, expr.span], msg)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_span_label(in_expr.span, format!("type `{in_expr_ty}`"))
|
|
|
|
.with_span_label(expr.span, format!("type `{ty}`"))
|
|
|
|
.with_note(
|
2024-01-03 17:03:10 +11:00
|
|
|
"asm inout arguments must have the same type, \
|
2024-01-09 09:08:49 +11:00
|
|
|
unless they are both pointers or integers of the same size",
|
2024-01-03 17:03:10 +11:00
|
|
|
)
|
|
|
|
.emit();
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All of the later checks have already been done on the input, so
|
|
|
|
// let's not emit errors and warnings twice.
|
|
|
|
return Some(asm_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the type against the list of types supported by the selected
|
|
|
|
// register class.
|
|
|
|
let asm_arch = self.tcx.sess.asm_arch.unwrap();
|
|
|
|
let reg_class = reg.reg_class();
|
|
|
|
let supported_tys = reg_class.supported_types(asm_arch);
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
let msg = format!("type `{ty}` cannot be used with this register class");
|
2023-12-18 22:21:37 +11:00
|
|
|
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
|
2022-02-19 00:48:49 +01:00
|
|
|
let supported_tys: Vec<_> = supported_tys.iter().map(|(t, _)| t.to_string()).collect();
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
err.note(format!(
|
2022-02-19 00:48:49 +01:00
|
|
|
"register class `{}` supports these types: {}",
|
|
|
|
reg_class.name(),
|
|
|
|
supported_tys.join(", "),
|
|
|
|
));
|
|
|
|
if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
|
|
|
|
err.help(format!("consider using the `{}` register class instead", suggest.name()));
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
2022-02-19 00:48:49 +01:00
|
|
|
err.emit();
|
|
|
|
return Some(asm_ty);
|
2020-02-13 11:00:55 +00:00
|
|
|
};
|
|
|
|
|
2020-03-19 07:41:43 +00:00
|
|
|
// Check whether the selected type requires a target feature. Note that
|
2021-10-07 15:42:18 -04:00
|
|
|
// this is different from the feature check we did earlier. While the
|
|
|
|
// previous check checked that this register class is usable at all
|
|
|
|
// with the currently enabled features, some types may only be usable
|
|
|
|
// with a register class when a certain feature is enabled. We check
|
|
|
|
// this here since it depends on the results of typeck.
|
2020-03-19 07:41:43 +00:00
|
|
|
//
|
|
|
|
// Also note that this check isn't run when the operand type is never
|
2021-10-07 15:42:18 -04:00
|
|
|
// (!). In that case we still need the earlier check to verify that the
|
|
|
|
// register class is usable at all.
|
2020-02-13 11:00:55 +00:00
|
|
|
if let Some(feature) = feature {
|
2023-02-21 15:15:16 +01:00
|
|
|
if !target_features.contains(feature) {
|
2023-07-25 23:17:39 +02:00
|
|
|
let msg = format!("`{feature}` target feature is not enabled");
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(expr.span, msg)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_note(format!(
|
2024-01-03 17:03:10 +11:00
|
|
|
"this is required to use type `{}` with register class `{}`",
|
|
|
|
ty,
|
|
|
|
reg_class.name(),
|
|
|
|
))
|
|
|
|
.emit();
|
2020-02-13 11:00:55 +00:00
|
|
|
return Some(asm_ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether a modifier is suggested for using this type.
|
2024-03-03 09:34:26 -05:00
|
|
|
if let Some(ModifierInfo {
|
|
|
|
modifier: suggested_modifier,
|
|
|
|
result: suggested_result,
|
|
|
|
size: suggested_size,
|
|
|
|
}) = reg_class.suggest_modifier(asm_arch, asm_ty)
|
2020-02-13 11:00:55 +00:00
|
|
|
{
|
|
|
|
// Search for any use of this operand without a modifier and emit
|
|
|
|
// the suggestion for them.
|
|
|
|
let mut spans = vec![];
|
|
|
|
for piece in template {
|
|
|
|
if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
|
|
|
|
{
|
|
|
|
if operand_idx == idx && modifier.is_none() {
|
|
|
|
spans.push(span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !spans.is_empty() {
|
2024-03-03 09:34:26 -05:00
|
|
|
let ModifierInfo {
|
|
|
|
modifier: default_modifier,
|
|
|
|
result: default_result,
|
|
|
|
size: default_size,
|
|
|
|
} = reg_class.default_modifier(asm_arch).unwrap();
|
2024-01-16 16:14:33 +11:00
|
|
|
self.tcx.node_span_lint(
|
2020-02-13 11:00:55 +00:00
|
|
|
lint::builtin::ASM_SUB_REGISTER,
|
|
|
|
expr.hir_id,
|
|
|
|
spans,
|
|
|
|
|lint| {
|
2024-05-22 16:46:05 +02:00
|
|
|
lint.primary_message("formatting may not be suitable for sub-register argument");
|
2022-09-16 11:01:02 +04:00
|
|
|
lint.span_label(expr.span, "for this argument");
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
lint.help(format!(
|
2024-03-03 09:34:26 -05:00
|
|
|
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
|
2020-04-29 00:45:58 +01:00
|
|
|
));
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
lint.help(format!(
|
2024-03-03 09:34:26 -05:00
|
|
|
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
|
2020-02-13 11:00:55 +00:00
|
|
|
));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(asm_ty)
|
|
|
|
}
|
|
|
|
|
2022-11-05 15:33:58 +00:00
|
|
|
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
|
|
|
|
let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
|
2022-04-14 12:07:36 +00:00
|
|
|
let Some(asm_arch) = self.tcx.sess.asm_arch else {
|
2024-01-04 12:54:23 +11:00
|
|
|
self.tcx.dcx().delayed_bug("target architecture does not support asm");
|
2022-04-14 12:07:36 +00:00
|
|
|
return;
|
|
|
|
};
|
2021-10-07 15:42:18 -04:00
|
|
|
for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
|
|
|
|
// Validate register classes against currently enabled target
|
|
|
|
// features. We check that at least one type is available for
|
|
|
|
// the enabled features.
|
|
|
|
//
|
|
|
|
// We ignore target feature requirements for clobbers: if the
|
|
|
|
// feature is disabled then the compiler doesn't care what we
|
|
|
|
// do with the registers.
|
|
|
|
//
|
|
|
|
// Note that this is only possible for explicit register
|
|
|
|
// operands, which cannot be used in the asm string.
|
|
|
|
if let Some(reg) = op.reg() {
|
2022-02-17 18:16:04 +00:00
|
|
|
// Some explicit registers cannot be used depending on the
|
|
|
|
// target. Reject those here.
|
|
|
|
if let InlineAsmRegOrRegClass::Reg(reg) = reg {
|
2022-04-14 12:07:36 +00:00
|
|
|
if let InlineAsmReg::Err = reg {
|
2022-05-25 14:01:06 +00:00
|
|
|
// `validate` will panic on `Err`, as an error must
|
|
|
|
// already have been reported.
|
|
|
|
continue;
|
2022-04-14 12:07:36 +00:00
|
|
|
}
|
2022-02-17 18:16:04 +00:00
|
|
|
if let Err(msg) = reg.validate(
|
|
|
|
asm_arch,
|
|
|
|
self.tcx.sess.relocation_model(),
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2022-02-17 18:16:04 +00:00
|
|
|
&self.tcx.sess.target,
|
|
|
|
op.is_clobber(),
|
|
|
|
) {
|
|
|
|
let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
|
2024-01-04 10:38:10 +11:00
|
|
|
self.tcx.dcx().span_err(*op_sp, msg);
|
2022-02-17 18:16:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:42:18 -04:00
|
|
|
if !op.is_clobber() {
|
|
|
|
let mut missing_required_features = vec![];
|
|
|
|
let reg_class = reg.reg_class();
|
2022-04-14 12:07:36 +00:00
|
|
|
if let InlineAsmRegClass::Err = reg_class {
|
2022-05-25 14:01:06 +00:00
|
|
|
continue;
|
2022-04-14 12:07:36 +00:00
|
|
|
}
|
2022-02-17 18:16:04 +00:00
|
|
|
for &(_, feature) in reg_class.supported_types(asm_arch) {
|
2021-10-07 15:42:18 -04:00
|
|
|
match feature {
|
|
|
|
Some(feature) => {
|
2022-02-17 18:16:04 +00:00
|
|
|
if target_features.contains(&feature) {
|
2021-10-07 15:42:18 -04:00
|
|
|
missing_required_features.clear();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
missing_required_features.push(feature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
missing_required_features.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are sorting primitive strs here and can use unstable sort here
|
|
|
|
missing_required_features.sort_unstable();
|
|
|
|
missing_required_features.dedup();
|
|
|
|
match &missing_required_features[..] {
|
|
|
|
[] => {}
|
|
|
|
[feature] => {
|
|
|
|
let msg = format!(
|
|
|
|
"register class `{}` requires the `{}` target feature",
|
|
|
|
reg_class.name(),
|
|
|
|
feature
|
|
|
|
);
|
2024-01-04 10:38:10 +11:00
|
|
|
self.tcx.dcx().span_err(*op_sp, msg);
|
2021-10-07 15:42:18 -04:00
|
|
|
// register isn't enabled, don't do more checks
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
features => {
|
|
|
|
let msg = format!(
|
|
|
|
"register class `{}` requires at least one of the following target features: {}",
|
|
|
|
reg_class.name(),
|
2022-01-15 19:57:47 +01:00
|
|
|
features
|
|
|
|
.iter()
|
|
|
|
.map(|f| f.as_str())
|
|
|
|
.intersperse(", ")
|
|
|
|
.collect::<String>(),
|
2021-10-07 15:42:18 -04:00
|
|
|
);
|
2024-01-04 10:38:10 +11:00
|
|
|
self.tcx.dcx().span_err(*op_sp, msg);
|
2021-10-07 15:42:18 -04:00
|
|
|
// register isn't enabled, don't do more checks
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 11:00:55 +00:00
|
|
|
match *op {
|
2023-01-09 16:30:40 +00:00
|
|
|
hir::InlineAsmOperand::In { reg, expr } => {
|
2021-10-07 15:42:18 -04:00
|
|
|
self.check_asm_operand_type(
|
|
|
|
idx,
|
|
|
|
reg,
|
|
|
|
expr,
|
|
|
|
asm.template,
|
|
|
|
true,
|
|
|
|
None,
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2021-10-07 15:42:18 -04:00
|
|
|
);
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
hir::InlineAsmOperand::Out { reg, late: _, expr } => {
|
2020-02-13 11:00:55 +00:00
|
|
|
if let Some(expr) = expr {
|
2021-10-07 15:42:18 -04:00
|
|
|
self.check_asm_operand_type(
|
|
|
|
idx,
|
|
|
|
reg,
|
|
|
|
expr,
|
|
|
|
asm.template,
|
|
|
|
false,
|
|
|
|
None,
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2021-10-07 15:42:18 -04:00
|
|
|
);
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
hir::InlineAsmOperand::InOut { reg, late: _, expr } => {
|
2021-10-07 15:42:18 -04:00
|
|
|
self.check_asm_operand_type(
|
|
|
|
idx,
|
|
|
|
reg,
|
|
|
|
expr,
|
|
|
|
asm.template,
|
|
|
|
false,
|
|
|
|
None,
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2021-10-07 15:42:18 -04:00
|
|
|
);
|
2020-02-13 11:00:55 +00:00
|
|
|
}
|
2023-01-09 16:30:40 +00:00
|
|
|
hir::InlineAsmOperand::SplitInOut { reg, late: _, in_expr, out_expr } => {
|
2021-10-07 15:42:18 -04:00
|
|
|
let in_ty = self.check_asm_operand_type(
|
|
|
|
idx,
|
|
|
|
reg,
|
|
|
|
in_expr,
|
|
|
|
asm.template,
|
|
|
|
true,
|
|
|
|
None,
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2021-10-07 15:42:18 -04:00
|
|
|
);
|
2020-02-13 11:00:55 +00:00
|
|
|
if let Some(out_expr) = out_expr {
|
|
|
|
self.check_asm_operand_type(
|
|
|
|
idx,
|
|
|
|
reg,
|
|
|
|
out_expr,
|
|
|
|
asm.template,
|
2021-08-12 20:23:34 +01:00
|
|
|
false,
|
2020-02-13 11:00:55 +00:00
|
|
|
Some((in_expr, in_ty)),
|
2023-11-21 20:07:32 +01:00
|
|
|
target_features,
|
2020-02-13 11:00:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-05-26 00:11:32 +01:00
|
|
|
hir::InlineAsmOperand::Const { anon_const } => {
|
|
|
|
let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity();
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Error(_) => {}
|
|
|
|
ty::Int(_) | ty::Uint(_) => {}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(*op_sp, "invalid type for `const` operand")
|
|
|
|
.with_span_label(
|
|
|
|
self.tcx.def_span(anon_const.def_id),
|
|
|
|
format!("is {} `{}`", ty.kind().article(), ty),
|
|
|
|
)
|
|
|
|
.with_help("`const` operands must be of an integer type")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// AST lowering guarantees that SymStatic points to a static.
|
|
|
|
hir::InlineAsmOperand::SymStatic { .. } => {}
|
2022-03-01 00:50:56 +00:00
|
|
|
// Check that sym actually points to a function. Later passes
|
|
|
|
// depend on this.
|
|
|
|
hir::InlineAsmOperand::SymFn { anon_const } => {
|
2023-07-11 22:35:29 +01:00
|
|
|
let ty = self.tcx.type_of(anon_const.def_id).instantiate_identity();
|
2022-03-01 00:50:56 +00:00
|
|
|
match ty.kind() {
|
|
|
|
ty::Never | ty::Error(_) => {}
|
|
|
|
ty::FnDef(..) => {}
|
|
|
|
_ => {
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx
|
|
|
|
.dcx()
|
|
|
|
.struct_span_err(*op_sp, "invalid `sym` operand")
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_span_label(
|
2024-01-03 17:03:10 +11:00
|
|
|
self.tcx.def_span(anon_const.def_id),
|
|
|
|
format!("is {} `{}`", ty.kind().article(), ty),
|
|
|
|
)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_help(
|
2024-01-03 17:03:10 +11:00
|
|
|
"`sym` operands must refer to either a function or a static",
|
|
|
|
)
|
|
|
|
.emit();
|
2022-03-01 00:50:56 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-12-25 20:53:01 +00:00
|
|
|
// No special checking is needed for labels.
|
|
|
|
hir::InlineAsmOperand::Label { .. } => {}
|
2022-03-01 00:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-12 14:08:44 -07:00
|
|
|
}
|
|
|
|
}
|