1 use rustc_ast::InlineAsmTemplatePiece;
2 use rustc_data_structures::fx::FxHashSet;
4 use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
5 use rustc_session::lint;
6 use rustc_span::def_id::LocalDefId;
7 use rustc_span::{Symbol, DUMMY_SP};
8 use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
10 pub struct InlineAsmCtxt<'a, 'tcx> {
12 param_env: ty::ParamEnv<'tcx>,
13 get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
16 impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
17 pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
20 param_env: ty::ParamEnv::empty(),
21 get_operand_ty: Box::new(|e| bug!("asm operand in global asm: {e:?}")),
27 param_env: ty::ParamEnv<'tcx>,
28 get_operand_ty: impl Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a,
30 InlineAsmCtxt { tcx, param_env, get_operand_ty: Box::new(get_operand_ty) }
33 // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
34 fn is_thin_ptr_ty(&self, ty: Ty<'tcx>) -> bool {
35 // Type still may have region variables, but `Sized` does not depend
36 // on those, so just erase them before querying.
37 if ty.is_sized(self.tcx, self.param_env) {
40 if let ty::Foreign(..) = ty.kind() {
46 fn check_asm_operand_type(
49 reg: InlineAsmRegOrRegClass,
50 expr: &'tcx hir::Expr<'tcx>,
51 template: &[InlineAsmTemplatePiece],
53 tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
54 target_features: &FxHashSet<Symbol>,
55 ) -> Option<InlineAsmType> {
56 let ty = (self.get_operand_ty)(expr);
57 if ty.has_non_region_infer() {
58 bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
60 let asm_ty_isize = match self.tcx.sess.target.pointer_width {
61 16 => InlineAsmType::I16,
62 32 => InlineAsmType::I32,
63 64 => InlineAsmType::I64,
67 let asm_ty = match *ty.kind() {
68 // `!` is allowed for input but not for output (issue #87802)
69 ty::Never if is_input => return None,
70 ty::Error(_) => return None,
71 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
72 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
73 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
74 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
75 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
76 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
77 ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
78 ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
79 ty::FnPtr(_) => Some(asm_ty_isize),
80 ty::RawPtr(ty::TypeAndMut { ty, mutbl: _ }) if self.is_thin_ptr_ty(ty) => {
83 ty::Adt(adt, substs) if adt.repr().simd() => {
84 let fields = &adt.non_enum_variant().fields;
85 let elem_ty = fields[0].ty(self.tcx, substs);
86 match elem_ty.kind() {
87 ty::Never | ty::Error(_) => return None,
88 ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => {
89 Some(InlineAsmType::VecI8(fields.len() as u64))
91 ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
92 Some(InlineAsmType::VecI16(fields.len() as u64))
94 ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
95 Some(InlineAsmType::VecI32(fields.len() as u64))
97 ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
98 Some(InlineAsmType::VecI64(fields.len() as u64))
100 ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
101 Some(InlineAsmType::VecI128(fields.len() as u64))
103 ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
104 Some(match self.tcx.sess.target.pointer_width {
105 16 => InlineAsmType::VecI16(fields.len() as u64),
106 32 => InlineAsmType::VecI32(fields.len() as u64),
107 64 => InlineAsmType::VecI64(fields.len() as u64),
111 ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(fields.len() as u64)),
112 ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(fields.len() as u64)),
116 ty::Infer(_) => unreachable!(),
119 let Some(asm_ty) = asm_ty else {
120 let msg = &format!("cannot use value of type `{ty}` for inline assembly");
121 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
123 "only integers, floats, SIMD vectors, pointers and function pointers \
124 can be used as arguments for inline assembly",
130 // Check that the type implements Copy. The only case where this can
131 // possibly fail is for SIMD types which don't #[derive(Copy)].
132 if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
133 let msg = "arguments for inline assembly must be copyable";
134 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
135 err.note(&format!("`{ty}` does not implement the Copy trait"));
139 // Ideally we wouldn't need to do this, but LLVM's register allocator
140 // really doesn't like it when tied operands have different types.
142 // This is purely an LLVM limitation, but we have to live with it since
143 // there is no way to hide this with implicit conversions.
145 // For the purposes of this check we only look at the `InlineAsmType`,
146 // which means that pointers and integers are treated as identical (modulo
148 if let Some((in_expr, Some(in_asm_ty))) = tied_input {
149 if in_asm_ty != asm_ty {
150 let msg = "incompatible types for asm inout argument";
151 let mut err = self.tcx.sess.struct_span_err(vec![in_expr.span, expr.span], msg);
153 let in_expr_ty = (self.get_operand_ty)(in_expr);
154 err.span_label(in_expr.span, &format!("type `{in_expr_ty}`"));
155 err.span_label(expr.span, &format!("type `{ty}`"));
157 "asm inout arguments must have the same type, \
158 unless they are both pointers or integers of the same size",
163 // All of the later checks have already been done on the input, so
164 // let's not emit errors and warnings twice.
168 // Check the type against the list of types supported by the selected
170 let asm_arch = self.tcx.sess.asm_arch.unwrap();
171 let reg_class = reg.reg_class();
172 let supported_tys = reg_class.supported_types(asm_arch);
173 let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
174 let msg = &format!("type `{ty}` cannot be used with this register class");
175 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
176 let supported_tys: Vec<_> =
177 supported_tys.iter().map(|(t, _)| t.to_string()).collect();
179 "register class `{}` supports these types: {}",
181 supported_tys.join(", "),
183 if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
185 "consider using the `{}` register class instead",
193 // Check whether the selected type requires a target feature. Note that
194 // this is different from the feature check we did earlier. While the
195 // previous check checked that this register class is usable at all
196 // with the currently enabled features, some types may only be usable
197 // with a register class when a certain feature is enabled. We check
198 // this here since it depends on the results of typeck.
200 // Also note that this check isn't run when the operand type is never
201 // (!). In that case we still need the earlier check to verify that the
202 // register class is usable at all.
203 if let Some(feature) = feature {
204 if !target_features.contains(&feature) {
205 let msg = &format!("`{}` target feature is not enabled", feature);
206 let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
208 "this is required to use type `{}` with register class `{}`",
217 // Check whether a modifier is suggested for using this type.
218 if let Some((suggested_modifier, suggested_result)) =
219 reg_class.suggest_modifier(asm_arch, asm_ty)
221 // Search for any use of this operand without a modifier and emit
222 // the suggestion for them.
223 let mut spans = vec![];
224 for piece in template {
225 if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
227 if operand_idx == idx && modifier.is_none() {
232 if !spans.is_empty() {
233 let (default_modifier, default_result) =
234 reg_class.default_modifier(asm_arch).unwrap();
235 self.tcx.struct_span_lint_hir(
236 lint::builtin::ASM_SUB_REGISTER,
239 "formatting may not be suitable for sub-register argument",
241 lint.span_label(expr.span, "for this argument");
243 "use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}`",
246 "or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}`",
257 pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
258 let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
259 let Some(asm_arch) = self.tcx.sess.asm_arch else {
260 self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
263 for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
264 // Validate register classes against currently enabled target
265 // features. We check that at least one type is available for
266 // the enabled features.
268 // We ignore target feature requirements for clobbers: if the
269 // feature is disabled then the compiler doesn't care what we
270 // do with the registers.
272 // Note that this is only possible for explicit register
273 // operands, which cannot be used in the asm string.
274 if let Some(reg) = op.reg() {
275 // Some explicit registers cannot be used depending on the
276 // target. Reject those here.
277 if let InlineAsmRegOrRegClass::Reg(reg) = reg {
278 if let InlineAsmReg::Err = reg {
279 // `validate` will panic on `Err`, as an error must
280 // already have been reported.
283 if let Err(msg) = reg.validate(
285 self.tcx.sess.relocation_model(),
287 &self.tcx.sess.target,
290 let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
291 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
296 if !op.is_clobber() {
297 let mut missing_required_features = vec![];
298 let reg_class = reg.reg_class();
299 if let InlineAsmRegClass::Err = reg_class {
302 for &(_, feature) in reg_class.supported_types(asm_arch) {
305 if target_features.contains(&feature) {
306 missing_required_features.clear();
309 missing_required_features.push(feature);
313 missing_required_features.clear();
319 // We are sorting primitive strs here and can use unstable sort here
320 missing_required_features.sort_unstable();
321 missing_required_features.dedup();
322 match &missing_required_features[..] {
326 "register class `{}` requires the `{}` target feature",
330 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
331 // register isn't enabled, don't do more checks
336 "register class `{}` requires at least one of the following target features: {}",
342 .collect::<String>(),
344 self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
345 // register isn't enabled, don't do more checks
353 hir::InlineAsmOperand::In { reg, expr } => {
354 self.check_asm_operand_type(
364 hir::InlineAsmOperand::Out { reg, late: _, expr } => {
365 if let Some(expr) = expr {
366 self.check_asm_operand_type(
377 hir::InlineAsmOperand::InOut { reg, late: _, expr } => {
378 self.check_asm_operand_type(
388 hir::InlineAsmOperand::SplitInOut { reg, late: _, in_expr, out_expr } => {
389 let in_ty = self.check_asm_operand_type(
398 if let Some(out_expr) = out_expr {
399 self.check_asm_operand_type(
405 Some((in_expr, in_ty)),
410 // No special checking is needed for these:
411 // - Typeck has checked that Const operands are integers.
412 // - AST lowering guarantees that SymStatic points to a static.
413 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::SymStatic { .. } => {}
414 // Check that sym actually points to a function. Later passes
416 hir::InlineAsmOperand::SymFn { anon_const } => {
417 let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
419 ty::Never | ty::Error(_) => {}
423 self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
425 self.tcx.hir().span(anon_const.body.hir_id),
426 &format!("is {} `{}`", ty.kind().article(), ty),
428 err.help("`sym` operands must refer to either a function or a static");