1 use super::LoweringContext;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_errors::struct_span_err;
7 use rustc_session::parse::feature_err;
8 use rustc_span::{sym, Span, Symbol};
10 use std::collections::hash_map::Entry;
13 impl<'a, 'hir> LoweringContext<'a, 'hir> {
14 crate fn lower_inline_asm(&mut self, sp: Span, asm: &InlineAsm) -> &'hir hir::InlineAsm<'hir> {
15 // Rustdoc needs to support asm! from foreign architectures: don't try
16 // lowering the register constraints in this case.
17 let asm_arch = if self.sess.opts.actually_rustdoc { None } else { self.sess.asm_arch };
18 if asm_arch.is_none() && !self.sess.opts.actually_rustdoc {
19 struct_span_err!(self.sess, sp, E0472, "inline assembly is unsupported on this target")
22 if let Some(asm_arch) = asm_arch {
23 // Inline assembly is currently only stable for these architectures.
24 let is_stable = matches!(
26 asm::InlineAsmArch::X86
27 | asm::InlineAsmArch::X86_64
28 | asm::InlineAsmArch::Arm
29 | asm::InlineAsmArch::AArch64
30 | asm::InlineAsmArch::RiscV32
31 | asm::InlineAsmArch::RiscV64
33 if !is_stable && !self.sess.features_untracked().asm_experimental_arch {
35 &self.sess.parse_sess,
36 sym::asm_experimental_arch,
38 "inline assembly is not stable yet on this architecture",
43 if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
44 && !matches!(asm_arch, Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64))
45 && !self.sess.opts.actually_rustdoc
48 .struct_span_err(sp, "the `att_syntax` option is only supported on x86")
52 let mut clobber_abi = None;
53 if let Some(asm_arch) = asm_arch {
54 if let Some((abi_name, abi_span)) = asm.clobber_abi {
55 match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, abi_name) {
56 Ok(abi) => clobber_abi = Some((abi, abi_span)),
61 "`clobber_abi` is not supported on this target",
65 Err(supported_abis) => {
67 self.sess.struct_span_err(abi_span, "invalid ABI for `clobber_abi`");
68 let mut abis = format!("`{}`", supported_abis[0]);
69 for m in &supported_abis[1..] {
70 let _ = write!(abis, ", `{}`", m);
73 "the following ABIs are supported on this target: {}",
82 // Lower operands to HIR. We use dummy register classes if an error
83 // occurs during lowering because we still need to be able to produce a
86 let mut operands: Vec<_> = asm
90 let lower_reg = |reg| match reg {
91 InlineAsmRegOrRegClass::Reg(s) => {
92 asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
93 asm::InlineAsmReg::parse(
95 |feature| sess.target_features.contains(&Symbol::intern(feature)),
100 let msg = format!("invalid register `{}`: {}", s.as_str(), e);
101 sess.struct_span_err(*op_sp, &msg).emit();
102 asm::InlineAsmReg::Err
105 asm::InlineAsmReg::Err
108 InlineAsmRegOrRegClass::RegClass(s) => {
109 asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
110 asm::InlineAsmRegClass::parse(asm_arch, s).unwrap_or_else(|e| {
111 let msg = format!("invalid register class `{}`: {}", s.as_str(), e);
112 sess.struct_span_err(*op_sp, &msg).emit();
113 asm::InlineAsmRegClass::Err
116 asm::InlineAsmRegClass::Err
122 InlineAsmOperand::In { reg, ref expr } => hir::InlineAsmOperand::In {
124 expr: self.lower_expr_mut(expr),
126 InlineAsmOperand::Out { reg, late, ref expr } => hir::InlineAsmOperand::Out {
129 expr: expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
131 InlineAsmOperand::InOut { reg, late, ref expr } => {
132 hir::InlineAsmOperand::InOut {
135 expr: self.lower_expr_mut(expr),
138 InlineAsmOperand::SplitInOut { reg, late, ref in_expr, ref out_expr } => {
139 hir::InlineAsmOperand::SplitInOut {
142 in_expr: self.lower_expr_mut(in_expr),
143 out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
146 InlineAsmOperand::Const { ref anon_const } => {
147 if !self.sess.features_untracked().asm_const {
149 &self.sess.parse_sess,
152 "const operands for inline assembly are unstable",
156 hir::InlineAsmOperand::Const {
157 anon_const: self.lower_anon_const(anon_const),
160 InlineAsmOperand::Sym { ref expr } => {
161 if !self.sess.features_untracked().asm_sym {
163 &self.sess.parse_sess,
166 "sym operands for inline assembly are unstable",
170 hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
173 (op, self.lower_span(*op_sp))
177 // Validate template modifiers against the register classes for the operands
178 for p in &asm.template {
179 if let InlineAsmTemplatePiece::Placeholder {
181 modifier: Some(modifier),
182 span: placeholder_span,
185 let op_sp = asm.operands[operand_idx].1;
186 match &operands[operand_idx].0 {
187 hir::InlineAsmOperand::In { reg, .. }
188 | hir::InlineAsmOperand::Out { reg, .. }
189 | hir::InlineAsmOperand::InOut { reg, .. }
190 | hir::InlineAsmOperand::SplitInOut { reg, .. } => {
191 let class = reg.reg_class();
192 if class == asm::InlineAsmRegClass::Err {
195 let valid_modifiers = class.valid_modifiers(asm_arch.unwrap());
196 if !valid_modifiers.contains(&modifier) {
197 let mut err = sess.struct_span_err(
199 "invalid asm template modifier for this register class",
201 err.span_label(placeholder_span, "template modifier");
202 err.span_label(op_sp, "argument");
203 if !valid_modifiers.is_empty() {
204 let mut mods = format!("`{}`", valid_modifiers[0]);
205 for m in &valid_modifiers[1..] {
206 let _ = write!(mods, ", `{}`", m);
209 "the `{}` register class supports \
210 the following template modifiers: {}",
216 "the `{}` register class does not support template modifiers",
223 hir::InlineAsmOperand::Const { .. } => {
224 let mut err = sess.struct_span_err(
226 "asm template modifiers are not allowed for `const` arguments",
228 err.span_label(placeholder_span, "template modifier");
229 err.span_label(op_sp, "argument");
232 hir::InlineAsmOperand::Sym { .. } => {
233 let mut err = sess.struct_span_err(
235 "asm template modifiers are not allowed for `sym` arguments",
237 err.span_label(placeholder_span, "template modifier");
238 err.span_label(op_sp, "argument");
245 let mut used_input_regs = FxHashMap::default();
246 let mut used_output_regs = FxHashMap::default();
248 for (idx, &(ref op, op_sp)) in operands.iter().enumerate() {
249 if let Some(reg) = op.reg() {
250 let reg_class = reg.reg_class();
251 if reg_class == asm::InlineAsmRegClass::Err {
255 // Some register classes can only be used as clobbers. This
256 // means that we disallow passing a value in/out of the asm and
257 // require that the operand name an explicit register, not a
259 if reg_class.is_clobber_only(asm_arch.unwrap()) && !op.is_clobber() {
261 "register class `{}` can only be used as a clobber, \
262 not as an input or output",
265 sess.struct_span_err(op_sp, &msg).emit();
269 // Check for conflicts between explicit register operands.
270 if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
271 let (input, output) = match op {
272 hir::InlineAsmOperand::In { .. } => (true, false),
274 // Late output do not conflict with inputs, but normal outputs do
275 hir::InlineAsmOperand::Out { late, .. } => (!late, true),
277 hir::InlineAsmOperand::InOut { .. }
278 | hir::InlineAsmOperand::SplitInOut { .. } => (true, true),
280 hir::InlineAsmOperand::Const { .. } | hir::InlineAsmOperand::Sym { .. } => {
285 // Flag to output the error only once per operand
286 let mut skip = false;
287 reg.overlapping_regs(|r| {
288 let mut check = |used_regs: &mut FxHashMap<asm::InlineAsmReg, usize>,
290 match used_regs.entry(r) {
291 Entry::Occupied(o) => {
298 let &(ref op2, op_sp2) = &operands[idx2];
299 let reg2 = match op2.reg() {
300 Some(asm::InlineAsmRegOrRegClass::Reg(r)) => r,
305 "register `{}` conflicts with register `{}`",
309 let mut err = sess.struct_span_err(op_sp, &msg);
310 err.span_label(op_sp, &format!("register `{}`", reg.name()));
311 err.span_label(op_sp2, &format!("register `{}`", reg2.name()));
315 hir::InlineAsmOperand::In { .. },
316 hir::InlineAsmOperand::Out { late, .. },
319 hir::InlineAsmOperand::Out { late, .. },
320 hir::InlineAsmOperand::In { .. },
323 let out_op_sp = if input { op_sp2 } else { op_sp };
324 let msg = "use `lateout` instead of \
325 `out` to avoid conflict";
326 err.span_help(out_op_sp, msg);
333 Entry::Vacant(v) => {
339 check(&mut used_input_regs, true);
342 check(&mut used_output_regs, false);
349 // If a clobber_abi is specified, add the necessary clobbers to the
351 if let Some((abi, abi_span)) = clobber_abi {
352 for &clobber in abi.clobbered_regs() {
353 let mut output_used = false;
354 clobber.overlapping_regs(|reg| {
355 if used_output_regs.contains_key(®) {
362 hir::InlineAsmOperand::Out {
363 reg: asm::InlineAsmRegOrRegClass::Reg(clobber),
367 self.lower_span(abi_span),
373 let operands = self.arena.alloc_from_iter(operands);
374 let template = self.arena.alloc_from_iter(asm.template.iter().cloned());
375 let template_strs = self.arena.alloc_from_iter(
378 .map(|(sym, snippet, span)| (*sym, *snippet, self.lower_span(*span))),
381 self.arena.alloc_from_iter(asm.line_spans.iter().map(|span| self.lower_span(*span)));
383 hir::InlineAsm { template, template_strs, operands, options: asm.options, line_spans };
384 self.arena.alloc(hir_asm)