rustc_codegen_ssa/
mono_item.rs
1use rustc_hir as hir;
2use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
3use rustc_middle::mir::interpret::ErrorHandled;
4use rustc_middle::mir::mono::{Linkage, MonoItem, Visibility};
5use rustc_middle::ty::Instance;
6use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
7use rustc_middle::{span_bug, ty};
8use tracing::debug;
9
10use crate::traits::*;
11use crate::{base, common};
12
13pub trait MonoItemExt<'a, 'tcx> {
14 fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx);
15 fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
16 &self,
17 cx: &'a Bx::CodegenCx,
18 linkage: Linkage,
19 visibility: Visibility,
20 );
21 fn to_raw_string(&self) -> String;
22}
23
24impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
25 fn define<Bx: BuilderMethods<'a, 'tcx>>(&self, cx: &'a Bx::CodegenCx) {
26 debug!(
27 "BEGIN IMPLEMENTING '{} ({})' in cgu {}",
28 self,
29 self.to_raw_string(),
30 cx.codegen_unit().name()
31 );
32
33 match *self {
34 MonoItem::Static(def_id) => {
35 cx.codegen_static(def_id);
36 }
37 MonoItem::GlobalAsm(item_id) => {
38 let item = cx.tcx().hir().item(item_id);
39 if let hir::ItemKind::GlobalAsm(asm) = item.kind {
40 let operands: Vec<_> = asm
41 .operands
42 .iter()
43 .map(|(op, op_sp)| match *op {
44 hir::InlineAsmOperand::Const { ref anon_const } => {
45 match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
46 Ok(const_value) => {
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 Err(ErrorHandled::Reported { .. }) => {
60 GlobalAsmOperandRef::Const { string: String::new() }
65 }
66 Err(ErrorHandled::TooGeneric(_)) => {
67 span_bug!(
68 *op_sp,
69 "asm const cannot be resolved; too generic"
70 )
71 }
72 }
73 }
74 hir::InlineAsmOperand::SymFn { ref anon_const } => {
75 let ty = cx
76 .tcx()
77 .typeck_body(anon_const.body)
78 .node_type(anon_const.hir_id);
79 let instance = match ty.kind() {
80 &ty::FnDef(def_id, args) => Instance::new(def_id, args),
81 _ => span_bug!(*op_sp, "asm sym is not a function"),
82 };
83
84 GlobalAsmOperandRef::SymFn { instance }
85 }
86 hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
87 GlobalAsmOperandRef::SymStatic { def_id }
88 }
89 hir::InlineAsmOperand::In { .. }
90 | hir::InlineAsmOperand::Out { .. }
91 | hir::InlineAsmOperand::InOut { .. }
92 | hir::InlineAsmOperand::SplitInOut { .. }
93 | hir::InlineAsmOperand::Label { .. } => {
94 span_bug!(*op_sp, "invalid operand type for global_asm!")
95 }
96 })
97 .collect();
98
99 cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans);
100 } else {
101 span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
102 }
103 }
104 MonoItem::Fn(instance) => {
105 base::codegen_instance::<Bx>(cx, instance);
106 }
107 }
108
109 debug!(
110 "END IMPLEMENTING '{} ({})' in cgu {}",
111 self,
112 self.to_raw_string(),
113 cx.codegen_unit().name()
114 );
115 }
116
117 fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
118 &self,
119 cx: &'a Bx::CodegenCx,
120 linkage: Linkage,
121 visibility: Visibility,
122 ) {
123 debug!(
124 "BEGIN PREDEFINING '{} ({})' in cgu {}",
125 self,
126 self.to_raw_string(),
127 cx.codegen_unit().name()
128 );
129
130 let symbol_name = self.symbol_name(cx.tcx()).name;
131
132 debug!("symbol {symbol_name}");
133
134 match *self {
135 MonoItem::Static(def_id) => {
136 cx.predefine_static(def_id, linkage, visibility, symbol_name);
137 }
138 MonoItem::Fn(instance) => {
139 let attrs = cx.tcx().codegen_fn_attrs(instance.def_id());
140
141 if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
142 } else {
144 cx.predefine_fn(instance, linkage, visibility, symbol_name);
145 };
146 }
147 MonoItem::GlobalAsm(..) => {}
148 }
149
150 debug!(
151 "END PREDEFINING '{} ({})' in cgu {}",
152 self,
153 self.to_raw_string(),
154 cx.codegen_unit().name()
155 );
156 }
157
158 fn to_raw_string(&self) -> String {
159 match *self {
160 MonoItem::Fn(instance) => {
161 format!("Fn({:?}, {})", instance.def, instance.args.as_ptr().addr())
162 }
163 MonoItem::Static(id) => format!("Static({id:?})"),
164 MonoItem::GlobalAsm(id) => format!("GlobalAsm({id:?})"),
165 }
166 }
167}