]> git.lizzy.rs Git - rust.git/blob - src/inline_asm.rs
Ensure inline asm wrapper name never starts with a digit
[rust.git] / src / inline_asm.rs
1 //! Codegen of [`asm!`] invocations.
2
3 use crate::prelude::*;
4
5 use std::fmt::Write;
6
7 use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
8 use rustc_middle::mir::InlineAsmOperand;
9 use rustc_span::Symbol;
10 use rustc_target::asm::*;
11
12 pub(crate) fn codegen_inline_asm<'tcx>(
13     fx: &mut FunctionCx<'_, '_, 'tcx>,
14     _span: Span,
15     template: &[InlineAsmTemplatePiece],
16     operands: &[InlineAsmOperand<'tcx>],
17     options: InlineAsmOptions,
18 ) {
19     // FIXME add .eh_frame unwind info directives
20
21     if template.is_empty() {
22         // Black box
23         return;
24     } else if template[0] == InlineAsmTemplatePiece::String("int $$0x29".to_string()) {
25         let true_ = fx.bcx.ins().iconst(types::I32, 1);
26         fx.bcx.ins().trapnz(true_, TrapCode::User(1));
27         return;
28     } else if template[0] == InlineAsmTemplatePiece::String("movq %rbx, ".to_string())
29         && matches!(
30             template[1],
31             InlineAsmTemplatePiece::Placeholder { operand_idx: 0, modifier: Some('r'), span: _ }
32         )
33         && template[2] == InlineAsmTemplatePiece::String("\n".to_string())
34         && template[3] == InlineAsmTemplatePiece::String("cpuid".to_string())
35         && template[4] == InlineAsmTemplatePiece::String("\n".to_string())
36         && template[5] == InlineAsmTemplatePiece::String("xchgq %rbx, ".to_string())
37         && matches!(
38             template[6],
39             InlineAsmTemplatePiece::Placeholder { operand_idx: 0, modifier: Some('r'), span: _ }
40         )
41     {
42         assert_eq!(operands.len(), 4);
43         let (leaf, eax_place) = match operands[1] {
44             InlineAsmOperand::InOut { reg, late: true, ref in_value, out_place } => {
45                 assert_eq!(
46                     reg,
47                     InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::ax))
48                 );
49                 (
50                     crate::base::codegen_operand(fx, in_value).load_scalar(fx),
51                     crate::base::codegen_place(fx, out_place.unwrap()),
52                 )
53             }
54             _ => unreachable!(),
55         };
56         let ebx_place = match operands[0] {
57             InlineAsmOperand::Out { reg, late: true, place } => {
58                 assert_eq!(
59                     reg,
60                     InlineAsmRegOrRegClass::RegClass(InlineAsmRegClass::X86(
61                         X86InlineAsmRegClass::reg
62                     ))
63                 );
64                 crate::base::codegen_place(fx, place.unwrap())
65             }
66             _ => unreachable!(),
67         };
68         let (sub_leaf, ecx_place) = match operands[2] {
69             InlineAsmOperand::InOut { reg, late: true, ref in_value, out_place } => {
70                 assert_eq!(
71                     reg,
72                     InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::cx))
73                 );
74                 (
75                     crate::base::codegen_operand(fx, in_value).load_scalar(fx),
76                     crate::base::codegen_place(fx, out_place.unwrap()),
77                 )
78             }
79             _ => unreachable!(),
80         };
81         let edx_place = match operands[3] {
82             InlineAsmOperand::Out { reg, late: true, place } => {
83                 assert_eq!(
84                     reg,
85                     InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::dx))
86                 );
87                 crate::base::codegen_place(fx, place.unwrap())
88             }
89             _ => unreachable!(),
90         };
91
92         let (eax, ebx, ecx, edx) = crate::intrinsics::codegen_cpuid_call(fx, leaf, sub_leaf);
93
94         eax_place.write_cvalue(fx, CValue::by_val(eax, fx.layout_of(fx.tcx.types.u32)));
95         ebx_place.write_cvalue(fx, CValue::by_val(ebx, fx.layout_of(fx.tcx.types.u32)));
96         ecx_place.write_cvalue(fx, CValue::by_val(ecx, fx.layout_of(fx.tcx.types.u32)));
97         edx_place.write_cvalue(fx, CValue::by_val(edx, fx.layout_of(fx.tcx.types.u32)));
98         return;
99     } else if fx.tcx.symbol_name(fx.instance).name.starts_with("___chkstk") {
100         // ___chkstk, ___chkstk_ms and __alloca are only used on Windows
101         crate::trap::trap_unimplemented(fx, "Stack probes are not supported");
102     } else if fx.tcx.symbol_name(fx.instance).name == "__alloca" {
103         crate::trap::trap_unimplemented(fx, "Alloca is not supported");
104     }
105
106     let mut inputs = Vec::new();
107     let mut outputs = Vec::new();
108
109     let mut asm_gen = InlineAssemblyGenerator {
110         tcx: fx.tcx,
111         arch: fx.tcx.sess.asm_arch.unwrap(),
112         template,
113         operands,
114         options,
115         registers: Vec::new(),
116         stack_slots_clobber: Vec::new(),
117         stack_slots_input: Vec::new(),
118         stack_slots_output: Vec::new(),
119         stack_slot_size: Size::from_bytes(0),
120     };
121     asm_gen.allocate_registers();
122     asm_gen.allocate_stack_slots();
123
124     let inline_asm_index = fx.cx.inline_asm_index.get();
125     fx.cx.inline_asm_index.set(inline_asm_index + 1);
126     let asm_name =
127         format!("__inline_asm_{}_n{}", fx.cx.cgu_name.as_str().replace('.', "__").replace('-', "_"), inline_asm_index);
128
129     let generated_asm = asm_gen.generate_asm_wrapper(&asm_name);
130     fx.cx.global_asm.push_str(&generated_asm);
131
132     for (i, operand) in operands.iter().enumerate() {
133         match *operand {
134             InlineAsmOperand::In { reg: _, ref value } => {
135                 inputs.push((
136                     asm_gen.stack_slots_input[i].unwrap(),
137                     crate::base::codegen_operand(fx, value).load_scalar(fx),
138                 ));
139             }
140             InlineAsmOperand::Out { reg: _, late: _, place } => {
141                 if let Some(place) = place {
142                     outputs.push((
143                         asm_gen.stack_slots_output[i].unwrap(),
144                         crate::base::codegen_place(fx, place),
145                     ));
146                 }
147             }
148             InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
149                 inputs.push((
150                     asm_gen.stack_slots_input[i].unwrap(),
151                     crate::base::codegen_operand(fx, in_value).load_scalar(fx),
152                 ));
153                 if let Some(out_place) = out_place {
154                     outputs.push((
155                         asm_gen.stack_slots_output[i].unwrap(),
156                         crate::base::codegen_place(fx, out_place),
157                     ));
158                 }
159             }
160             InlineAsmOperand::Const { value: _ } => todo!(),
161             InlineAsmOperand::SymFn { value: _ } => todo!(),
162             InlineAsmOperand::SymStatic { def_id: _ } => todo!(),
163         }
164     }
165
166     call_inline_asm(fx, &asm_name, asm_gen.stack_slot_size, inputs, outputs);
167 }
168
169 struct InlineAssemblyGenerator<'a, 'tcx> {
170     tcx: TyCtxt<'tcx>,
171     arch: InlineAsmArch,
172     template: &'a [InlineAsmTemplatePiece],
173     operands: &'a [InlineAsmOperand<'tcx>],
174     options: InlineAsmOptions,
175     registers: Vec<Option<InlineAsmReg>>,
176     stack_slots_clobber: Vec<Option<Size>>,
177     stack_slots_input: Vec<Option<Size>>,
178     stack_slots_output: Vec<Option<Size>>,
179     stack_slot_size: Size,
180 }
181
182 impl<'tcx> InlineAssemblyGenerator<'_, 'tcx> {
183     fn allocate_registers(&mut self) {
184         let sess = self.tcx.sess;
185         let map = allocatable_registers(
186             self.arch,
187             |feature| sess.target_features.contains(&Symbol::intern(feature)),
188             &sess.target,
189         );
190         let mut allocated = FxHashMap::<_, (bool, bool)>::default();
191         let mut regs = vec![None; self.operands.len()];
192
193         // Add explicit registers to the allocated set.
194         for (i, operand) in self.operands.iter().enumerate() {
195             match *operand {
196                 InlineAsmOperand::In { reg: InlineAsmRegOrRegClass::Reg(reg), .. } => {
197                     regs[i] = Some(reg);
198                     allocated.entry(reg).or_default().0 = true;
199                 }
200                 InlineAsmOperand::Out {
201                     reg: InlineAsmRegOrRegClass::Reg(reg), late: true, ..
202                 } => {
203                     regs[i] = Some(reg);
204                     allocated.entry(reg).or_default().1 = true;
205                 }
206                 InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(reg), .. }
207                 | InlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(reg), .. } => {
208                     regs[i] = Some(reg);
209                     allocated.insert(reg, (true, true));
210                 }
211                 _ => (),
212             }
213         }
214
215         // Allocate out/inout/inlateout registers first because they are more constrained.
216         for (i, operand) in self.operands.iter().enumerate() {
217             match *operand {
218                 InlineAsmOperand::Out {
219                     reg: InlineAsmRegOrRegClass::RegClass(class),
220                     late: false,
221                     ..
222                 }
223                 | InlineAsmOperand::InOut {
224                     reg: InlineAsmRegOrRegClass::RegClass(class), ..
225                 } => {
226                     let mut alloc_reg = None;
227                     for &reg in &map[&class] {
228                         let mut used = false;
229                         reg.overlapping_regs(|r| {
230                             if allocated.contains_key(&r) {
231                                 used = true;
232                             }
233                         });
234
235                         if !used {
236                             alloc_reg = Some(reg);
237                             break;
238                         }
239                     }
240
241                     let reg = alloc_reg.expect("cannot allocate registers");
242                     regs[i] = Some(reg);
243                     allocated.insert(reg, (true, true));
244                 }
245                 _ => (),
246             }
247         }
248
249         // Allocate in/lateout.
250         for (i, operand) in self.operands.iter().enumerate() {
251             match *operand {
252                 InlineAsmOperand::In { reg: InlineAsmRegOrRegClass::RegClass(class), .. } => {
253                     let mut alloc_reg = None;
254                     for &reg in &map[&class] {
255                         let mut used = false;
256                         reg.overlapping_regs(|r| {
257                             if allocated.get(&r).copied().unwrap_or_default().0 {
258                                 used = true;
259                             }
260                         });
261
262                         if !used {
263                             alloc_reg = Some(reg);
264                             break;
265                         }
266                     }
267
268                     let reg = alloc_reg.expect("cannot allocate registers");
269                     regs[i] = Some(reg);
270                     allocated.entry(reg).or_default().0 = true;
271                 }
272                 InlineAsmOperand::Out {
273                     reg: InlineAsmRegOrRegClass::RegClass(class),
274                     late: true,
275                     ..
276                 } => {
277                     let mut alloc_reg = None;
278                     for &reg in &map[&class] {
279                         let mut used = false;
280                         reg.overlapping_regs(|r| {
281                             if allocated.get(&r).copied().unwrap_or_default().1 {
282                                 used = true;
283                             }
284                         });
285
286                         if !used {
287                             alloc_reg = Some(reg);
288                             break;
289                         }
290                     }
291
292                     let reg = alloc_reg.expect("cannot allocate registers");
293                     regs[i] = Some(reg);
294                     allocated.entry(reg).or_default().1 = true;
295                 }
296                 _ => (),
297             }
298         }
299
300         self.registers = regs;
301     }
302
303     fn allocate_stack_slots(&mut self) {
304         let mut slot_size = Size::from_bytes(0);
305         let mut slots_clobber = vec![None; self.operands.len()];
306         let mut slots_input = vec![None; self.operands.len()];
307         let mut slots_output = vec![None; self.operands.len()];
308
309         let new_slot_fn = |slot_size: &mut Size, reg_class: InlineAsmRegClass| {
310             let reg_size =
311                 reg_class.supported_types(self.arch).iter().map(|(ty, _)| ty.size()).max().unwrap();
312             let align = rustc_target::abi::Align::from_bytes(reg_size.bytes()).unwrap();
313             let offset = slot_size.align_to(align);
314             *slot_size = offset + reg_size;
315             offset
316         };
317         let mut new_slot = |x| new_slot_fn(&mut slot_size, x);
318
319         // Allocate stack slots for saving clobbered registers
320         let abi_clobber =
321             InlineAsmClobberAbi::parse(self.arch, &self.tcx.sess.target, Symbol::intern("C"))
322                 .unwrap()
323                 .clobbered_regs();
324         for (i, reg) in self.registers.iter().enumerate().filter_map(|(i, r)| r.map(|r| (i, r))) {
325             let mut need_save = true;
326             // If the register overlaps with a register clobbered by function call, then
327             // we don't need to save it.
328             for r in abi_clobber {
329                 r.overlapping_regs(|r| {
330                     if r == reg {
331                         need_save = false;
332                     }
333                 });
334
335                 if !need_save {
336                     break;
337                 }
338             }
339
340             if need_save {
341                 slots_clobber[i] = Some(new_slot(reg.reg_class()));
342             }
343         }
344
345         // Allocate stack slots for inout
346         for (i, operand) in self.operands.iter().enumerate() {
347             match *operand {
348                 InlineAsmOperand::InOut { reg, out_place: Some(_), .. } => {
349                     let slot = new_slot(reg.reg_class());
350                     slots_input[i] = Some(slot);
351                     slots_output[i] = Some(slot);
352                 }
353                 _ => (),
354             }
355         }
356
357         let slot_size_before_input = slot_size;
358         let mut new_slot = |x| new_slot_fn(&mut slot_size, x);
359
360         // Allocate stack slots for input
361         for (i, operand) in self.operands.iter().enumerate() {
362             match *operand {
363                 InlineAsmOperand::In { reg, .. }
364                 | InlineAsmOperand::InOut { reg, out_place: None, .. } => {
365                     slots_input[i] = Some(new_slot(reg.reg_class()));
366                 }
367                 _ => (),
368             }
369         }
370
371         // Reset slot size to before input so that input and output operands can overlap
372         // and save some memory.
373         let slot_size_after_input = slot_size;
374         slot_size = slot_size_before_input;
375         let mut new_slot = |x| new_slot_fn(&mut slot_size, x);
376
377         // Allocate stack slots for output
378         for (i, operand) in self.operands.iter().enumerate() {
379             match *operand {
380                 InlineAsmOperand::Out { reg, place: Some(_), .. } => {
381                     slots_output[i] = Some(new_slot(reg.reg_class()));
382                 }
383                 _ => (),
384             }
385         }
386
387         slot_size = slot_size.max(slot_size_after_input);
388
389         self.stack_slots_clobber = slots_clobber;
390         self.stack_slots_input = slots_input;
391         self.stack_slots_output = slots_output;
392         self.stack_slot_size = slot_size;
393     }
394
395     fn generate_asm_wrapper(&self, asm_name: &str) -> String {
396         let mut generated_asm = String::new();
397         writeln!(generated_asm, ".globl {}", asm_name).unwrap();
398         writeln!(generated_asm, ".type {},@function", asm_name).unwrap();
399         writeln!(generated_asm, ".section .text.{},\"ax\",@progbits", asm_name).unwrap();
400         writeln!(generated_asm, "{}:", asm_name).unwrap();
401
402         let is_x86 = matches!(self.arch, InlineAsmArch::X86 | InlineAsmArch::X86_64);
403
404         if is_x86 {
405             generated_asm.push_str(".intel_syntax noprefix\n");
406         }
407         Self::prologue(&mut generated_asm, self.arch);
408
409         // Save clobbered registers
410         if !self.options.contains(InlineAsmOptions::NORETURN) {
411             for (reg, slot) in self
412                 .registers
413                 .iter()
414                 .zip(self.stack_slots_clobber.iter().copied())
415                 .filter_map(|(r, s)| r.zip(s))
416             {
417                 Self::save_register(&mut generated_asm, self.arch, reg, slot);
418             }
419         }
420
421         // Write input registers
422         for (reg, slot) in self
423             .registers
424             .iter()
425             .zip(self.stack_slots_input.iter().copied())
426             .filter_map(|(r, s)| r.zip(s))
427         {
428             Self::restore_register(&mut generated_asm, self.arch, reg, slot);
429         }
430
431         if is_x86 && self.options.contains(InlineAsmOptions::ATT_SYNTAX) {
432             generated_asm.push_str(".att_syntax\n");
433         }
434
435         // The actual inline asm
436         for piece in self.template {
437             match piece {
438                 InlineAsmTemplatePiece::String(s) => {
439                     generated_asm.push_str(s);
440                 }
441                 InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span: _ } => {
442                     if self.options.contains(InlineAsmOptions::ATT_SYNTAX) {
443                         generated_asm.push('%');
444                     }
445                     self.registers[*operand_idx]
446                         .unwrap()
447                         .emit(&mut generated_asm, self.arch, *modifier)
448                         .unwrap();
449                 }
450             }
451         }
452         generated_asm.push('\n');
453
454         if is_x86 && self.options.contains(InlineAsmOptions::ATT_SYNTAX) {
455             generated_asm.push_str(".intel_syntax noprefix\n");
456         }
457
458         if !self.options.contains(InlineAsmOptions::NORETURN) {
459             // Read output registers
460             for (reg, slot) in self
461                 .registers
462                 .iter()
463                 .zip(self.stack_slots_output.iter().copied())
464                 .filter_map(|(r, s)| r.zip(s))
465             {
466                 Self::save_register(&mut generated_asm, self.arch, reg, slot);
467             }
468
469             // Restore clobbered registers
470             for (reg, slot) in self
471                 .registers
472                 .iter()
473                 .zip(self.stack_slots_clobber.iter().copied())
474                 .filter_map(|(r, s)| r.zip(s))
475             {
476                 Self::restore_register(&mut generated_asm, self.arch, reg, slot);
477             }
478
479             Self::epilogue(&mut generated_asm, self.arch);
480         } else {
481             Self::epilogue_noreturn(&mut generated_asm, self.arch);
482         }
483
484         if is_x86 {
485             generated_asm.push_str(".att_syntax\n");
486         }
487         writeln!(generated_asm, ".size {name}, .-{name}", name = asm_name).unwrap();
488         generated_asm.push_str(".text\n");
489         generated_asm.push_str("\n\n");
490
491         generated_asm
492     }
493
494     fn prologue(generated_asm: &mut String, arch: InlineAsmArch) {
495         match arch {
496             InlineAsmArch::X86 => {
497                 generated_asm.push_str("    push ebp\n");
498                 generated_asm.push_str("    mov ebp,[esp+8]\n");
499             }
500             InlineAsmArch::X86_64 => {
501                 generated_asm.push_str("    push rbp\n");
502                 generated_asm.push_str("    mov rbp,rdi\n");
503             }
504             InlineAsmArch::RiscV32 => {
505                 generated_asm.push_str("    addi sp, sp, -8\n");
506                 generated_asm.push_str("    sw ra, 4(sp)\n");
507                 generated_asm.push_str("    sw s0, 0(sp)\n");
508                 generated_asm.push_str("    mv s0, a0\n");
509             }
510             InlineAsmArch::RiscV64 => {
511                 generated_asm.push_str("    addi sp, sp, -16\n");
512                 generated_asm.push_str("    sd ra, 8(sp)\n");
513                 generated_asm.push_str("    sd s0, 0(sp)\n");
514                 generated_asm.push_str("    mv s0, a0\n");
515             }
516             _ => unimplemented!("prologue for {:?}", arch),
517         }
518     }
519
520     fn epilogue(generated_asm: &mut String, arch: InlineAsmArch) {
521         match arch {
522             InlineAsmArch::X86 => {
523                 generated_asm.push_str("    pop ebp\n");
524                 generated_asm.push_str("    ret\n");
525             }
526             InlineAsmArch::X86_64 => {
527                 generated_asm.push_str("    pop rbp\n");
528                 generated_asm.push_str("    ret\n");
529             }
530             InlineAsmArch::RiscV32 => {
531                 generated_asm.push_str("    lw s0, 0(sp)\n");
532                 generated_asm.push_str("    lw ra, 4(sp)\n");
533                 generated_asm.push_str("    addi sp, sp, 8\n");
534                 generated_asm.push_str("    ret\n");
535             }
536             InlineAsmArch::RiscV64 => {
537                 generated_asm.push_str("    ld s0, 0(sp)\n");
538                 generated_asm.push_str("    ld ra, 8(sp)\n");
539                 generated_asm.push_str("    addi sp, sp, 16\n");
540                 generated_asm.push_str("    ret\n");
541             }
542             _ => unimplemented!("epilogue for {:?}", arch),
543         }
544     }
545
546     fn epilogue_noreturn(generated_asm: &mut String, arch: InlineAsmArch) {
547         match arch {
548             InlineAsmArch::X86 | InlineAsmArch::X86_64 => {
549                 generated_asm.push_str("    ud2\n");
550             }
551             InlineAsmArch::RiscV32 | InlineAsmArch::RiscV64 => {
552                 generated_asm.push_str("    ebreak\n");
553             }
554             _ => unimplemented!("epilogue_noreturn for {:?}", arch),
555         }
556     }
557
558     fn save_register(
559         generated_asm: &mut String,
560         arch: InlineAsmArch,
561         reg: InlineAsmReg,
562         offset: Size,
563     ) {
564         match arch {
565             InlineAsmArch::X86 => {
566                 write!(generated_asm, "    mov [ebp+0x{:x}], ", offset.bytes()).unwrap();
567                 reg.emit(generated_asm, InlineAsmArch::X86, None).unwrap();
568                 generated_asm.push('\n');
569             }
570             InlineAsmArch::X86_64 => {
571                 write!(generated_asm, "    mov [rbp+0x{:x}], ", offset.bytes()).unwrap();
572                 reg.emit(generated_asm, InlineAsmArch::X86_64, None).unwrap();
573                 generated_asm.push('\n');
574             }
575             InlineAsmArch::RiscV32 => {
576                 generated_asm.push_str("    sw ");
577                 reg.emit(generated_asm, InlineAsmArch::RiscV32, None).unwrap();
578                 writeln!(generated_asm, ", 0x{:x}(s0)", offset.bytes()).unwrap();
579             }
580             InlineAsmArch::RiscV64 => {
581                 generated_asm.push_str("    sd ");
582                 reg.emit(generated_asm, InlineAsmArch::RiscV64, None).unwrap();
583                 writeln!(generated_asm, ", 0x{:x}(s0)", offset.bytes()).unwrap();
584             }
585             _ => unimplemented!("save_register for {:?}", arch),
586         }
587     }
588
589     fn restore_register(
590         generated_asm: &mut String,
591         arch: InlineAsmArch,
592         reg: InlineAsmReg,
593         offset: Size,
594     ) {
595         match arch {
596             InlineAsmArch::X86 => {
597                 generated_asm.push_str("    mov ");
598                 reg.emit(generated_asm, InlineAsmArch::X86, None).unwrap();
599                 writeln!(generated_asm, ", [ebp+0x{:x}]", offset.bytes()).unwrap();
600             }
601             InlineAsmArch::X86_64 => {
602                 generated_asm.push_str("    mov ");
603                 reg.emit(generated_asm, InlineAsmArch::X86_64, None).unwrap();
604                 writeln!(generated_asm, ", [rbp+0x{:x}]", offset.bytes()).unwrap();
605             }
606             InlineAsmArch::RiscV32 => {
607                 generated_asm.push_str("    lw ");
608                 reg.emit(generated_asm, InlineAsmArch::RiscV32, None).unwrap();
609                 writeln!(generated_asm, ", 0x{:x}(s0)", offset.bytes()).unwrap();
610             }
611             InlineAsmArch::RiscV64 => {
612                 generated_asm.push_str("    ld ");
613                 reg.emit(generated_asm, InlineAsmArch::RiscV64, None).unwrap();
614                 writeln!(generated_asm, ", 0x{:x}(s0)", offset.bytes()).unwrap();
615             }
616             _ => unimplemented!("restore_register for {:?}", arch),
617         }
618     }
619 }
620
621 fn call_inline_asm<'tcx>(
622     fx: &mut FunctionCx<'_, '_, 'tcx>,
623     asm_name: &str,
624     slot_size: Size,
625     inputs: Vec<(Size, Value)>,
626     outputs: Vec<(Size, CPlace<'tcx>)>,
627 ) {
628     let stack_slot = fx.bcx.func.create_stack_slot(StackSlotData {
629         kind: StackSlotKind::ExplicitSlot,
630         size: u32::try_from(slot_size.bytes()).unwrap(),
631     });
632     if fx.clif_comments.enabled() {
633         fx.add_comment(stack_slot, "inline asm scratch slot");
634     }
635
636     let inline_asm_func = fx
637         .module
638         .declare_function(
639             asm_name,
640             Linkage::Import,
641             &Signature {
642                 call_conv: CallConv::SystemV,
643                 params: vec![AbiParam::new(fx.pointer_type)],
644                 returns: vec![],
645             },
646         )
647         .unwrap();
648     let inline_asm_func = fx.module.declare_func_in_func(inline_asm_func, &mut fx.bcx.func);
649     if fx.clif_comments.enabled() {
650         fx.add_comment(inline_asm_func, asm_name);
651     }
652
653     for (offset, value) in inputs {
654         fx.bcx.ins().stack_store(value, stack_slot, i32::try_from(offset.bytes()).unwrap());
655     }
656
657     let stack_slot_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0);
658     fx.bcx.ins().call(inline_asm_func, &[stack_slot_addr]);
659
660     for (offset, place) in outputs {
661         let ty = fx.clif_type(place.layout().ty).unwrap();
662         let value = fx.bcx.ins().stack_load(ty, stack_slot, i32::try_from(offset.bytes()).unwrap());
663         place.write_cvalue(fx, CValue::by_val(value, place.layout()));
664     }
665 }