|
1 | 1 | use crate::common; |
2 | | -use crate::mir::FunctionCx; |
3 | 2 | use crate::traits::{AsmMethods, BuilderMethods, GlobalAsmOperandRef, MiscMethods}; |
4 | 3 | use rustc_attr::InstructionSetAttr; |
5 | 4 | use rustc_middle::bug; |
6 | 5 | use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility}; |
| 6 | +use rustc_middle::mir::Body; |
7 | 7 | use rustc_middle::mir::InlineAsmOperand; |
8 | 8 | use rustc_middle::ty; |
9 | 9 | use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf}; |
10 | 10 | use rustc_middle::ty::{Instance, TyCtxt}; |
11 | 11 | use rustc_target::asm::InlineAsmArch; |
12 | 12 |
|
13 | | -impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { |
14 | | - pub fn codegen_naked_asm(&self, instance: Instance<'tcx>) { |
15 | | - let cx = &self.cx; |
16 | | - |
17 | | - let rustc_middle::mir::TerminatorKind::InlineAsm { |
18 | | - template, |
19 | | - ref operands, |
20 | | - options, |
21 | | - line_spans, |
22 | | - targets: _, |
23 | | - unwind: _, |
24 | | - } = self.mir.basic_blocks.iter().next().unwrap().terminator().kind |
25 | | - else { |
26 | | - bug!("#[naked] functions should always terminate with an asm! block") |
27 | | - }; |
28 | | - |
29 | | - let operands: Vec<_> = |
30 | | - operands.iter().map(|op| self.inline_to_global_operand(op)).collect(); |
31 | | - |
32 | | - let item_data = cx.codegen_unit().items().get(&MonoItem::Fn(instance)).unwrap(); |
33 | | - let (begin, end) = crate::mir::naked_asm::prefix_and_suffix(cx.tcx(), instance, item_data); |
34 | | - |
35 | | - let mut template_vec = Vec::new(); |
36 | | - template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(begin)); |
37 | | - template_vec.extend(template.iter().cloned()); |
38 | | - template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(end)); |
39 | | - |
40 | | - cx.codegen_global_asm(&template_vec, &operands, options, line_spans); |
41 | | - } |
| 13 | +pub fn codegen_naked_asm<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( |
| 14 | + cx: &'a Bx::CodegenCx, |
| 15 | + mir: &Body<'tcx>, |
| 16 | + instance: Instance<'tcx>, |
| 17 | +) { |
| 18 | + let rustc_middle::mir::TerminatorKind::InlineAsm { |
| 19 | + template, |
| 20 | + ref operands, |
| 21 | + options, |
| 22 | + line_spans, |
| 23 | + targets: _, |
| 24 | + unwind: _, |
| 25 | + } = mir.basic_blocks.iter().next().unwrap().terminator().kind |
| 26 | + else { |
| 27 | + bug!("#[naked] functions should always terminate with an asm! block") |
| 28 | + }; |
42 | 29 |
|
43 | | - fn inline_to_global_operand(&self, op: &InlineAsmOperand<'tcx>) -> GlobalAsmOperandRef<'tcx> { |
44 | | - match op { |
45 | | - InlineAsmOperand::Const { value } => { |
46 | | - let const_value = self.eval_mir_constant(value); |
47 | | - let string = common::asm_const_to_str( |
48 | | - self.cx.tcx(), |
49 | | - value.span, |
50 | | - const_value, |
51 | | - self.cx.layout_of(value.ty()), |
52 | | - ); |
53 | | - GlobalAsmOperandRef::Const { string } |
54 | | - } |
55 | | - InlineAsmOperand::SymFn { value } => { |
56 | | - let instance = match value.ty().kind() { |
57 | | - &ty::FnDef(def_id, args) => Instance::new(def_id, args), |
58 | | - _ => bug!("asm sym is not a function"), |
59 | | - }; |
| 30 | + let operands: Vec<_> = |
| 31 | + operands.iter().map(|op| inline_to_global_operand::<Bx>(cx, instance, op)).collect(); |
60 | 32 |
|
61 | | - GlobalAsmOperandRef::SymFn { instance } |
62 | | - } |
63 | | - InlineAsmOperand::SymStatic { def_id } => { |
64 | | - GlobalAsmOperandRef::SymStatic { def_id: *def_id } |
65 | | - } |
66 | | - InlineAsmOperand::In { .. } |
67 | | - | InlineAsmOperand::Out { .. } |
68 | | - | InlineAsmOperand::InOut { .. } |
69 | | - | InlineAsmOperand::Label { .. } => { |
70 | | - bug!("invalid operand type for naked_asm!") |
71 | | - } |
| 33 | + let item_data = cx.codegen_unit().items().get(&MonoItem::Fn(instance)).unwrap(); |
| 34 | + let (begin, end) = crate::mir::naked_asm::prefix_and_suffix(cx.tcx(), instance, item_data); |
| 35 | + |
| 36 | + let mut template_vec = Vec::new(); |
| 37 | + template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(begin.into())); |
| 38 | + template_vec.extend(template.iter().cloned()); |
| 39 | + template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(end.into())); |
| 40 | + |
| 41 | + cx.codegen_global_asm(&template_vec, &operands, options, line_spans); |
| 42 | +} |
| 43 | + |
| 44 | +fn inline_to_global_operand<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( |
| 45 | + cx: &'a Bx::CodegenCx, |
| 46 | + instance: Instance<'tcx>, |
| 47 | + op: &InlineAsmOperand<'tcx>, |
| 48 | +) -> GlobalAsmOperandRef<'tcx> { |
| 49 | + match op { |
| 50 | + InlineAsmOperand::Const { value } => { |
| 51 | + let const_value = instance |
| 52 | + .instantiate_mir_and_normalize_erasing_regions( |
| 53 | + cx.tcx(), |
| 54 | + ty::ParamEnv::reveal_all(), |
| 55 | + ty::EarlyBinder::bind(value.const_), |
| 56 | + ) |
| 57 | + .eval(cx.tcx(), ty::ParamEnv::reveal_all(), value.span) |
| 58 | + .expect("erroneous constant missed by mono item collection"); |
| 59 | + let string = common::asm_const_to_str( |
| 60 | + cx.tcx(), |
| 61 | + value.span, |
| 62 | + const_value, |
| 63 | + cx.layout_of(value.ty()), |
| 64 | + ); |
| 65 | + GlobalAsmOperandRef::Const { string } |
| 66 | + } |
| 67 | + InlineAsmOperand::SymFn { value } => { |
| 68 | + let instance = match value.ty().kind() { |
| 69 | + &ty::FnDef(def_id, args) => Instance::new(def_id, args), |
| 70 | + _ => bug!("asm sym is not a function"), |
| 71 | + }; |
| 72 | + |
| 73 | + GlobalAsmOperandRef::SymFn { instance } |
| 74 | + } |
| 75 | + InlineAsmOperand::SymStatic { def_id } => { |
| 76 | + GlobalAsmOperandRef::SymStatic { def_id: *def_id } |
| 77 | + } |
| 78 | + InlineAsmOperand::In { .. } |
| 79 | + | InlineAsmOperand::Out { .. } |
| 80 | + | InlineAsmOperand::InOut { .. } |
| 81 | + | InlineAsmOperand::Label { .. } => { |
| 82 | + bug!("invalid operand type for naked_asm!") |
72 | 83 | } |
73 | 84 | } |
74 | 85 | } |
|
0 commit comments