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 { expr } => {
75 let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr);
76 let instance = match ty.kind() {
77 &ty::FnDef(def_id, args) => Instance::new(def_id, args),
78 _ => span_bug!(*op_sp, "asm sym is not a function"),
79 };
80
81 GlobalAsmOperandRef::SymFn { instance }
82 }
83 hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
84 GlobalAsmOperandRef::SymStatic { def_id }
85 }
86 hir::InlineAsmOperand::In { .. }
87 | hir::InlineAsmOperand::Out { .. }
88 | hir::InlineAsmOperand::InOut { .. }
89 | hir::InlineAsmOperand::SplitInOut { .. }
90 | hir::InlineAsmOperand::Label { .. } => {
91 span_bug!(*op_sp, "invalid operand type for global_asm!")
92 }
93 })
94 .collect();
95
96 cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans);
97 } else {
98 span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
99 }
100 }
101 MonoItem::Fn(instance) => {
102 base::codegen_instance::<Bx>(cx, instance);
103 }
104 }
105
106 debug!(
107 "END IMPLEMENTING '{} ({})' in cgu {}",
108 self,
109 self.to_raw_string(),
110 cx.codegen_unit().name()
111 );
112 }
113
114 fn predefine<Bx: BuilderMethods<'a, 'tcx>>(
115 &self,
116 cx: &'a Bx::CodegenCx,
117 linkage: Linkage,
118 visibility: Visibility,
119 ) {
120 debug!(
121 "BEGIN PREDEFINING '{} ({})' in cgu {}",
122 self,
123 self.to_raw_string(),
124 cx.codegen_unit().name()
125 );
126
127 let symbol_name = self.symbol_name(cx.tcx()).name;
128
129 debug!("symbol {symbol_name}");
130
131 match *self {
132 MonoItem::Static(def_id) => {
133 cx.predefine_static(def_id, linkage, visibility, symbol_name);
134 }
135 MonoItem::Fn(instance) => {
136 let attrs = cx.tcx().codegen_fn_attrs(instance.def_id());
137
138 if attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
139 } else {
141 cx.predefine_fn(instance, linkage, visibility, symbol_name);
142 };
143 }
144 MonoItem::GlobalAsm(..) => {}
145 }
146
147 debug!(
148 "END PREDEFINING '{} ({})' in cgu {}",
149 self,
150 self.to_raw_string(),
151 cx.codegen_unit().name()
152 );
153 }
154
155 fn to_raw_string(&self) -> String {
156 match *self {
157 MonoItem::Fn(instance) => {
158 format!("Fn({:?}, {})", instance.def, instance.args.as_ptr().addr())
159 }
160 MonoItem::Static(id) => format!("Static({id:?})"),
161 MonoItem::GlobalAsm(id) => format!("GlobalAsm({id:?})"),
162 }
163 }
164}