]>
Commit | Line | Data |
---|---|---|
9fa01778 | 1 | use crate::base; |
17df50a5 | 2 | use crate::common; |
9fa01778 | 3 | use crate::traits::*; |
dfeec247 | 4 | use rustc_hir as hir; |
17df50a5 | 5 | use rustc_middle::mir::mono::MonoItem; |
ba9703b0 | 6 | use rustc_middle::mir::mono::{Linkage, Visibility}; |
c295e0f8 | 7 | use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf}; |
a1dfa0c6 | 8 | |
dc9dc135 XL |
9 | pub trait MonoItemExt<'a, 'tcx> { |
10 | fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx); | |
11 | fn predefine<Bx: BuilderMethods<'a, 'tcx>>( | |
12 | &self, | |
13 | cx: &'a Bx::CodegenCx, | |
14 | linkage: Linkage, | |
dfeec247 | 15 | visibility: Visibility, |
dc9dc135 XL |
16 | ); |
17 | fn to_raw_string(&self) -> String; | |
18 | } | |
a1dfa0c6 | 19 | |
dc9dc135 | 20 | impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> { |
a1dfa0c6 | 21 | fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) { |
dfeec247 XL |
22 | debug!( |
23 | "BEGIN IMPLEMENTING '{} ({})' in cgu {}", | |
1b1a35ee | 24 | self, |
dfeec247 XL |
25 | self.to_raw_string(), |
26 | cx.codegen_unit().name() | |
27 | ); | |
a1dfa0c6 | 28 | |
dc9dc135 | 29 | match *self { |
a1dfa0c6 | 30 | MonoItem::Static(def_id) => { |
48663c56 | 31 | cx.codegen_static(def_id, cx.tcx().is_mutable_static(def_id)); |
a1dfa0c6 | 32 | } |
6a06907d XL |
33 | MonoItem::GlobalAsm(item_id) => { |
34 | let item = cx.tcx().hir().item(item_id); | |
17df50a5 XL |
35 | if let hir::ItemKind::GlobalAsm(ref asm) = item.kind { |
36 | let operands: Vec<_> = asm | |
37 | .operands | |
38 | .iter() | |
39 | .map(|(op, op_sp)| match *op { | |
40 | hir::InlineAsmOperand::Const { ref anon_const } => { | |
41 | let anon_const_def_id = | |
42 | cx.tcx().hir().local_def_id(anon_const.hir_id).to_def_id(); | |
43 | let const_value = | |
44 | cx.tcx().const_eval_poly(anon_const_def_id).unwrap_or_else( | |
45 | |_| span_bug!(*op_sp, "asm const cannot be resolved"), | |
46 | ); | |
47 | let ty = cx | |
48 | .tcx() | |
49 | .typeck_body(anon_const.body) | |
50 | .node_type(anon_const.hir_id); | |
51 | let string = common::asm_const_to_str( | |
52 | cx.tcx(), | |
53 | *op_sp, | |
54 | const_value, | |
55 | cx.layout_of(ty), | |
56 | ); | |
57 | GlobalAsmOperandRef::Const { string } | |
58 | } | |
59 | _ => span_bug!(*op_sp, "invalid operand type for global_asm!"), | |
60 | }) | |
61 | .collect(); | |
62 | ||
63 | cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans); | |
a1dfa0c6 XL |
64 | } else { |
65 | span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type") | |
66 | } | |
67 | } | |
68 | MonoItem::Fn(instance) => { | |
69 | base::codegen_instance::<Bx>(&cx, instance); | |
70 | } | |
71 | } | |
72 | ||
dfeec247 XL |
73 | debug!( |
74 | "END IMPLEMENTING '{} ({})' in cgu {}", | |
1b1a35ee | 75 | self, |
dfeec247 XL |
76 | self.to_raw_string(), |
77 | cx.codegen_unit().name() | |
78 | ); | |
a1dfa0c6 XL |
79 | } |
80 | ||
81 | fn predefine<Bx: BuilderMethods<'a, 'tcx>>( | |
82 | &self, | |
83 | cx: &'a Bx::CodegenCx, | |
84 | linkage: Linkage, | |
dfeec247 | 85 | visibility: Visibility, |
a1dfa0c6 | 86 | ) { |
dfeec247 XL |
87 | debug!( |
88 | "BEGIN PREDEFINING '{} ({})' in cgu {}", | |
1b1a35ee | 89 | self, |
dfeec247 XL |
90 | self.to_raw_string(), |
91 | cx.codegen_unit().name() | |
92 | ); | |
a1dfa0c6 | 93 | |
3dfed10e | 94 | let symbol_name = self.symbol_name(cx.tcx()).name; |
a1dfa0c6 XL |
95 | |
96 | debug!("symbol {}", &symbol_name); | |
97 | ||
dc9dc135 | 98 | match *self { |
a1dfa0c6 XL |
99 | MonoItem::Static(def_id) => { |
100 | cx.predefine_static(def_id, linkage, visibility, &symbol_name); | |
101 | } | |
102 | MonoItem::Fn(instance) => { | |
103 | cx.predefine_fn(instance, linkage, visibility, &symbol_name); | |
104 | } | |
105 | MonoItem::GlobalAsm(..) => {} | |
106 | } | |
107 | ||
dfeec247 XL |
108 | debug!( |
109 | "END PREDEFINING '{} ({})' in cgu {}", | |
1b1a35ee | 110 | self, |
dfeec247 XL |
111 | self.to_raw_string(), |
112 | cx.codegen_unit().name() | |
113 | ); | |
a1dfa0c6 XL |
114 | } |
115 | ||
116 | fn to_raw_string(&self) -> String { | |
dc9dc135 | 117 | match *self { |
a1dfa0c6 | 118 | MonoItem::Fn(instance) => { |
5e7ed085 | 119 | format!("Fn({:?}, {})", instance.def, instance.substs.as_ptr().addr()) |
a1dfa0c6 | 120 | } |
dfeec247 XL |
121 | MonoItem::Static(id) => format!("Static({:?})", id), |
122 | MonoItem::GlobalAsm(id) => format!("GlobalAsm({:?})", id), | |
a1dfa0c6 XL |
123 | } |
124 | } | |
125 | } |