1use rustc_abi::{BackendRepr, Float, Integer, Primitive, RegKind};
2use rustc_hir::attrs::{InstructionSetAttr, Linkage};
3use rustc_hir::def_id::LOCAL_CRATE;
4use rustc_middle::mir::{InlineAsmOperand, START_BLOCK};
5use rustc_middle::mono::{MonoItemData, Visibility};
6use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, TyAndLayout};
7use rustc_middle::ty::{Instance, Ty, TyCtxt, TypeVisitableExt};
8use rustc_middle::{bug, ty};
9use rustc_span::sym;
10use rustc_target::callconv::{ArgAbi, FnAbi, PassMode};
11use rustc_target::spec::{Arch, BinaryFormat, Env, Os};
12
13use crate::common;
14use crate::mir::AsmCodegenMethods;
15use crate::traits::GlobalAsmOperandRef;
16
17pub fn codegen_naked_asm<
18 'a,
19 'tcx,
20 Cx: LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>
21 + FnAbiOf<'tcx, FnAbiOfResult = &'tcx FnAbi<'tcx, Ty<'tcx>>>
22 + AsmCodegenMethods<'tcx>,
23>(
24 cx: &'a mut Cx,
25 instance: Instance<'tcx>,
26 item_data: MonoItemData,
27) {
28 if !!instance.args.has_infer() {
::core::panicking::panic("assertion failed: !instance.args.has_infer()")
};assert!(!instance.args.has_infer());
29 let mir = cx.tcx().instance_mir(instance.def);
30
31 let rustc_middle::mir::TerminatorKind::InlineAsm {
32 asm_macro: _,
33 template,
34 ref operands,
35 options,
36 line_spans,
37 targets: _,
38 unwind: _,
39 } = mir.basic_blocks[START_BLOCK].terminator().kind
40 else {
41 ::rustc_middle::util::bug::bug_fmt(format_args!("#[naked] functions should always terminate with an asm! block"))bug!("#[naked] functions should always terminate with an asm! block")
42 };
43
44 let operands: Vec<_> =
45 operands.iter().map(|op| inline_to_global_operand::<Cx>(cx, instance, op)).collect();
46
47 let name = cx.mangled_name(instance);
48 let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
49 let (begin, end) = prefix_and_suffix(cx.tcx(), instance, &name, item_data, fn_abi);
50
51 let mut template_vec = Vec::new();
52 template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(begin.into()));
53 template_vec.extend(template.iter().cloned());
54 template_vec.push(rustc_ast::ast::InlineAsmTemplatePiece::String(end.into()));
55
56 cx.codegen_global_asm(&template_vec, &operands, options, line_spans);
57}
58
59fn inline_to_global_operand<'a, 'tcx, Cx: LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>>>(
60 cx: &'a Cx,
61 instance: Instance<'tcx>,
62 op: &InlineAsmOperand<'tcx>,
63) -> GlobalAsmOperandRef<'tcx> {
64 match op {
65 InlineAsmOperand::Const { value } => {
66 let const_value = instance
67 .instantiate_mir_and_normalize_erasing_regions(
68 cx.tcx(),
69 cx.typing_env(),
70 ty::EarlyBinder::bind(value.const_),
71 )
72 .eval(cx.tcx(), cx.typing_env(), value.span)
73 .expect("erroneous constant missed by mono item collection");
74
75 let mono_type = instance.instantiate_mir_and_normalize_erasing_regions(
76 cx.tcx(),
77 cx.typing_env(),
78 ty::EarlyBinder::bind(value.ty()),
79 );
80
81 let string = common::asm_const_to_str(
82 cx.tcx(),
83 value.span,
84 const_value,
85 cx.layout_of(mono_type),
86 );
87
88 GlobalAsmOperandRef::Const { string }
89 }
90 InlineAsmOperand::SymFn { value } => {
91 let mono_type = instance.instantiate_mir_and_normalize_erasing_regions(
92 cx.tcx(),
93 cx.typing_env(),
94 ty::EarlyBinder::bind(value.ty()),
95 );
96
97 let instance = match mono_type.kind() {
98 &ty::FnDef(def_id, args) => {
99 Instance::expect_resolve(cx.tcx(), cx.typing_env(), def_id, args, value.span)
100 }
101 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("asm sym is not a function"))bug!("asm sym is not a function"),
102 };
103
104 GlobalAsmOperandRef::SymFn { instance }
105 }
106 InlineAsmOperand::SymStatic { def_id } => {
107 GlobalAsmOperandRef::SymStatic { def_id: *def_id }
108 }
109 InlineAsmOperand::In { .. }
110 | InlineAsmOperand::Out { .. }
111 | InlineAsmOperand::InOut { .. }
112 | InlineAsmOperand::Label { .. } => {
113 ::rustc_middle::util::bug::bug_fmt(format_args!("invalid operand type for naked_asm!"))bug!("invalid operand type for naked_asm!")
114 }
115 }
116}
117
118fn prefix_and_suffix<'tcx>(
119 tcx: TyCtxt<'tcx>,
120 instance: Instance<'tcx>,
121 asm_name: &str,
122 item_data: MonoItemData,
123 fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
124) -> (String, String) {
125 use std::fmt::Write;
126
127 let asm_binary_format = &tcx.sess.target.binary_format;
128
129 let is_arm = tcx.sess.target.arch == Arch::Arm;
130 let is_thumb = tcx.sess.unstable_target_features.contains(&sym::thumb_mode);
131 let function_sections =
132 tcx.sess.opts.unstable_opts.function_sections.unwrap_or(tcx.sess.target.function_sections);
133
134 let mut visibility = item_data.visibility;
139 if item_data.linkage != Linkage::Internal && tcx.is_compiler_builtins(LOCAL_CRATE) {
140 visibility = Visibility::Hidden;
141 }
142
143 let attrs = tcx.codegen_instance_attrs(instance.def);
144 let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());
145
146 let align_bytes = match attrs.alignment {
148 Some(align) => align.bytes(),
149 None => match asm_binary_format {
150 BinaryFormat::Coff => 16,
151 _ => 4,
152 },
153 };
154
155 let (arch_prefix, arch_suffix) = if is_arm {
157 (
158 match attrs.instruction_set {
159 None => match is_thumb {
160 true => ".thumb\n.thumb_func",
161 false => ".arm",
162 },
163 Some(InstructionSetAttr::ArmT32) => ".thumb\n.thumb_func",
164 Some(InstructionSetAttr::ArmA32) => ".arm",
165 },
166 match is_thumb {
167 true => ".thumb",
168 false => ".arm",
169 },
170 )
171 } else {
172 ("", "")
173 };
174
175 let emit_fatal = |msg| tcx.dcx().span_fatal(tcx.def_span(instance.def_id()), msg);
176
177 let write_linkage = |w: &mut String| -> std::fmt::Result {
179 match item_data.linkage {
180 Linkage::External => {
181 w.write_fmt(format_args!(".globl {0}\n", asm_name))writeln!(w, ".globl {asm_name}")?;
182 }
183 Linkage::LinkOnceAny | Linkage::LinkOnceODR | Linkage::WeakAny | Linkage::WeakODR => {
184 match asm_binary_format {
185 BinaryFormat::Elf | BinaryFormat::Coff | BinaryFormat::Wasm => {
186 w.write_fmt(format_args!(".weak {0}\n", asm_name))writeln!(w, ".weak {asm_name}")?;
187 }
188 BinaryFormat::Xcoff => {
189 emit_fatal(
192 "cannot create weak symbols from inline assembly for this target",
193 )
194 }
195 BinaryFormat::MachO => {
196 w.write_fmt(format_args!(".globl {0}\n", asm_name))writeln!(w, ".globl {asm_name}")?;
197 w.write_fmt(format_args!(".weak_definition {0}\n", asm_name))writeln!(w, ".weak_definition {asm_name}")?;
198 }
199 }
200 }
201 Linkage::Internal => {
202 emit_fatal("naked functions may not have internal linkage")
204 }
205 Linkage::Common => emit_fatal("Functions may not have common linkage"),
206 Linkage::AvailableExternally => {
207 emit_fatal("Functions may not have available_externally linkage")
209 }
210 Linkage::ExternalWeak => {
211 emit_fatal("Functions may not have external weak linkage")
213 }
214 }
215
216 Ok(())
217 };
218
219 let mut begin = String::new();
220 let mut end = String::new();
221 match asm_binary_format {
222 BinaryFormat::Elf => {
223 let progbits = match is_arm {
224 true => "%progbits",
225 false => "@progbits",
226 };
227
228 let function = match is_arm {
229 true => "%function",
230 false => "@function",
231 };
232
233 if let Some(section) = &link_section {
234 begin.write_fmt(format_args!(".pushsection {0},\"ax\", {1}\n", section,
progbits))writeln!(begin, ".pushsection {section},\"ax\", {progbits}").unwrap();
235 } else if function_sections {
236 begin.write_fmt(format_args!(".pushsection .text.{0},\"ax\", {1}\n", asm_name,
progbits))writeln!(begin, ".pushsection .text.{asm_name},\"ax\", {progbits}").unwrap();
237 } else {
238 begin.write_fmt(format_args!(".text\n"))writeln!(begin, ".text").unwrap();
239 }
240 begin.write_fmt(format_args!(".balign {0}\n", align_bytes))writeln!(begin, ".balign {align_bytes}").unwrap();
241 write_linkage(&mut begin).unwrap();
242 match visibility {
243 Visibility::Default => {}
244 Visibility::Protected => begin.write_fmt(format_args!(".protected {0}\n", asm_name))writeln!(begin, ".protected {asm_name}").unwrap(),
245 Visibility::Hidden => begin.write_fmt(format_args!(".hidden {0}\n", asm_name))writeln!(begin, ".hidden {asm_name}").unwrap(),
246 }
247 begin.write_fmt(format_args!(".type {0}, {1}\n", asm_name, function))writeln!(begin, ".type {asm_name}, {function}").unwrap();
248 if !arch_prefix.is_empty() {
249 begin.write_fmt(format_args!("{0}\n", arch_prefix))writeln!(begin, "{}", arch_prefix).unwrap();
250 }
251 begin.write_fmt(format_args!("{0}:\n", asm_name))writeln!(begin, "{asm_name}:").unwrap();
252
253 end.write_fmt(format_args!("\n"))writeln!(end).unwrap();
254 end.write_fmt(format_args!(".Lfunc_end_{0}:\n", asm_name))writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
257 end.write_fmt(format_args!(".size {0}, . - {0}\n", asm_name))writeln!(end, ".size {asm_name}, . - {asm_name}").unwrap();
258 if link_section.is_some() || function_sections {
259 end.write_fmt(format_args!(".popsection\n"))writeln!(end, ".popsection").unwrap();
260 }
261 if !arch_suffix.is_empty() {
262 end.write_fmt(format_args!("{0}\n", arch_suffix))writeln!(end, "{}", arch_suffix).unwrap();
263 }
264 }
265 BinaryFormat::MachO => {
266 if let Some(section) = &link_section {
270 begin.write_fmt(format_args!(".pushsection {0},regular,pure_instructions\n",
section))writeln!(begin, ".pushsection {section},regular,pure_instructions").unwrap();
271 } else {
272 begin.write_fmt(format_args!(".section __TEXT,__text,regular,pure_instructions\n"))writeln!(begin, ".section __TEXT,__text,regular,pure_instructions").unwrap();
273 }
274 begin.write_fmt(format_args!(".balign {0}\n", align_bytes))writeln!(begin, ".balign {align_bytes}").unwrap();
275 write_linkage(&mut begin).unwrap();
276 match visibility {
277 Visibility::Default | Visibility::Protected => {}
278 Visibility::Hidden => begin.write_fmt(format_args!(".private_extern {0}\n", asm_name))writeln!(begin, ".private_extern {asm_name}").unwrap(),
279 }
280 begin.write_fmt(format_args!("{0}:\n", asm_name))writeln!(begin, "{asm_name}:").unwrap();
281
282 end.write_fmt(format_args!("\n"))writeln!(end).unwrap();
283 end.write_fmt(format_args!(".Lfunc_end_{0}:\n", asm_name))writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
284 if link_section.is_some() {
285 end.write_fmt(format_args!(".popsection\n"))writeln!(end, ".popsection").unwrap();
286 }
287 if !arch_suffix.is_empty() {
288 end.write_fmt(format_args!("{0}\n", arch_suffix))writeln!(end, "{}", arch_suffix).unwrap();
289 }
290 }
291 BinaryFormat::Coff => {
292 begin.write_fmt(format_args!(".def {0}\n", asm_name))writeln!(begin, ".def {asm_name}").unwrap();
293 begin.write_fmt(format_args!(".scl 2\n"))writeln!(begin, ".scl 2").unwrap();
294 begin.write_fmt(format_args!(".type 32\n"))writeln!(begin, ".type 32").unwrap();
295 begin.write_fmt(format_args!(".endef\n"))writeln!(begin, ".endef").unwrap();
296
297 if let Some(section) = &link_section {
298 begin.write_fmt(format_args!(".section {0},\"xr\"\n", section))writeln!(begin, ".section {section},\"xr\"").unwrap()
299 } else if !function_sections {
300 begin.write_fmt(format_args!(".text\n"))writeln!(begin, ".text").unwrap();
303 } else {
304 match &tcx.sess.target.options.env {
311 Env::Gnu => {
312 begin.write_fmt(format_args!(".section .text${0},\"xr\",one_only,{0}\n",
asm_name))writeln!(begin, ".section .text${asm_name},\"xr\",one_only,{asm_name}")
313 .unwrap();
314 }
315 Env::Msvc => {
316 begin.write_fmt(format_args!(".section .text,\"xr\",one_only,{0}\n",
asm_name))writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
317 }
318 Env::Unspecified => match &tcx.sess.target.options.os {
319 Os::Uefi => {
320 begin.write_fmt(format_args!(".section .text,\"xr\",one_only,{0}\n",
asm_name))writeln!(begin, ".section .text,\"xr\",one_only,{asm_name}").unwrap();
321 }
322 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected coff target {0}",
tcx.sess.target.llvm_target))bug!("unexpected coff target {}", tcx.sess.target.llvm_target),
323 },
324 other => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected coff env {0:?}",
other))bug!("unexpected coff env {other:?}"),
325 }
326 }
327 write_linkage(&mut begin).unwrap();
328 begin.write_fmt(format_args!(".balign {0}\n", align_bytes))writeln!(begin, ".balign {align_bytes}").unwrap();
329 begin.write_fmt(format_args!("{0}:\n", asm_name))writeln!(begin, "{asm_name}:").unwrap();
330
331 end.write_fmt(format_args!("\n"))writeln!(end).unwrap();
332 if !arch_suffix.is_empty() {
333 end.write_fmt(format_args!("{0}\n", arch_suffix))writeln!(end, "{}", arch_suffix).unwrap();
334 }
335 }
336 BinaryFormat::Wasm => {
337 let section = link_section.unwrap_or_else(|| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(".text.{0}", asm_name))
})format!(".text.{asm_name}"));
338
339 begin.write_fmt(format_args!(".section {0},\"\",@\n", section))writeln!(begin, ".section {section},\"\",@").unwrap();
340 write_linkage(&mut begin).unwrap();
342 if let Visibility::Hidden = visibility {
343 begin.write_fmt(format_args!(".hidden {0}\n", asm_name))writeln!(begin, ".hidden {asm_name}").unwrap();
344 }
345 begin.write_fmt(format_args!(".type {0}, @function\n", asm_name))writeln!(begin, ".type {asm_name}, @function").unwrap();
346 if !arch_prefix.is_empty() {
347 begin.write_fmt(format_args!("{0}\n", arch_prefix))writeln!(begin, "{}", arch_prefix).unwrap();
348 }
349 begin.write_fmt(format_args!("{0}:\n", asm_name))writeln!(begin, "{asm_name}:").unwrap();
350 begin.write_fmt(format_args!(".functype {1} {0}\n",
wasm_functype(tcx, fn_abi), asm_name))writeln!(begin, ".functype {asm_name} {}", wasm_functype(tcx, fn_abi)).unwrap();
351
352 end.write_fmt(format_args!("\n"))writeln!(end).unwrap();
353 end.write_fmt(format_args!("end_function\n"))writeln!(end, "end_function").unwrap();
355 end.write_fmt(format_args!(".Lfunc_end_{0}:\n", asm_name))writeln!(end, ".Lfunc_end_{asm_name}:").unwrap();
356 }
357 BinaryFormat::Xcoff => {
358 begin.write_fmt(format_args!(".align {0}\n", align_bytes))writeln!(begin, ".align {}", align_bytes).unwrap();
373
374 write_linkage(&mut begin).unwrap();
375 if let Visibility::Hidden = visibility {
376 }
379 begin.write_fmt(format_args!("{0}:\n", asm_name))writeln!(begin, "{asm_name}:").unwrap();
380
381 end.write_fmt(format_args!("\n"))writeln!(end).unwrap();
382 }
384 }
385
386 (begin, end)
387}
388
389fn wasm_functype<'tcx>(tcx: TyCtxt<'tcx>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> String {
393 let mut signature = String::with_capacity(64);
394
395 let ptr_type = match tcx.data_layout.pointer_size().bits() {
396 32 => "i32",
397 64 => "i64",
398 other => ::rustc_middle::util::bug::bug_fmt(format_args!("wasm pointer size cannot be {0} bits",
other))bug!("wasm pointer size cannot be {other} bits"),
399 };
400
401 let hidden_return = #[allow(non_exhaustive_omitted_patterns)] match fn_abi.ret.mode {
PassMode::Indirect { .. } => true,
_ => false,
}matches!(fn_abi.ret.mode, PassMode::Indirect { .. });
402
403 signature.push('(');
404
405 if hidden_return {
406 signature.push_str(ptr_type);
407 if !fn_abi.args.is_empty() {
408 signature.push_str(", ");
409 }
410 }
411
412 let mut it = fn_abi.args.iter().peekable();
413 while let Some(arg_abi) = it.next() {
414 wasm_type(&mut signature, arg_abi, ptr_type);
415 if it.peek().is_some() {
416 signature.push_str(", ");
417 }
418 }
419
420 signature.push_str(") -> (");
421
422 if !hidden_return {
423 wasm_type(&mut signature, &fn_abi.ret, ptr_type);
424 }
425
426 signature.push(')');
427
428 signature
429}
430
431fn wasm_type<'tcx>(signature: &mut String, arg_abi: &ArgAbi<'_, Ty<'tcx>>, ptr_type: &'static str) {
432 match arg_abi.mode {
433 PassMode::Ignore => { }
434 PassMode::Direct(_) => {
435 let direct_type = match arg_abi.layout.backend_repr {
436 BackendRepr::Scalar(scalar) => wasm_primitive(scalar.primitive(), ptr_type),
437 BackendRepr::SimdVector { .. } => "v128",
438 other => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unexpected BackendRepr: {0:?}", other)));
}unreachable!("unexpected BackendRepr: {:?}", other),
439 };
440
441 signature.push_str(direct_type);
442 }
443 PassMode::Pair(_, _) => match arg_abi.layout.backend_repr {
444 BackendRepr::ScalarPair(a, b) => {
445 signature.push_str(wasm_primitive(a.primitive(), ptr_type));
446 signature.push_str(", ");
447 signature.push_str(wasm_primitive(b.primitive(), ptr_type));
448 }
449 other => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("{0:?}", other)));
}unreachable!("{other:?}"),
450 },
451 PassMode::Cast { pad_i32, ref cast } => {
452 if !!pad_i32 {
{
::core::panicking::panic_fmt(format_args!("not currently used by wasm calling convention"));
}
};assert!(!pad_i32, "not currently used by wasm calling convention");
454 if !cast.prefix[0].is_none() {
{ ::core::panicking::panic_fmt(format_args!("no prefix")); }
};assert!(cast.prefix[0].is_none(), "no prefix");
455 match (&cast.rest.total, &arg_abi.layout.size) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("single item")));
}
}
};assert_eq!(cast.rest.total, arg_abi.layout.size, "single item");
456
457 let wrapped_wasm_type = match cast.rest.unit.kind {
458 RegKind::Integer => match cast.rest.unit.size.bytes() {
459 ..=4 => "i32",
460 ..=8 => "i64",
461 _ => ptr_type,
462 },
463 RegKind::Float => match cast.rest.unit.size.bytes() {
464 ..=4 => "f32",
465 ..=8 => "f64",
466 _ => ptr_type,
467 },
468 RegKind::Vector { .. } => "v128",
469 };
470
471 signature.push_str(wrapped_wasm_type);
472 }
473 PassMode::Indirect { .. } => signature.push_str(ptr_type),
474 }
475}
476
477fn wasm_primitive(primitive: Primitive, ptr_type: &'static str) -> &'static str {
478 match primitive {
479 Primitive::Int(integer, _) => match integer {
480 Integer::I8 | Integer::I16 | Integer::I32 => "i32",
481 Integer::I64 => "i64",
482 Integer::I128 => "i64, i64",
483 },
484 Primitive::Float(float) => match float {
485 Float::F16 | Float::F32 => "f32",
486 Float::F64 => "f64",
487 Float::F128 => "i64, i64",
488 },
489 Primitive::Pointer(_) => ptr_type,
490 }
491}