1use crate::ir;
4use crate::ir::types::*;
5
6use crate::isa;
7
8use crate::isa::CallConv;
9use crate::isa::riscv64::inst::*;
10use crate::machinst::*;
11
12use crate::CodegenResult;
13use crate::ir::LibCall;
14use crate::ir::Signature;
15use crate::isa::riscv64::settings::Flags as RiscvFlags;
16use crate::isa::unwind::UnwindInst;
17use crate::settings;
18use alloc::boxed::Box;
19use alloc::vec::Vec;
20use regalloc2::{MachineEnv, PRegSet};
21
22use alloc::borrow::ToOwned;
23use smallvec::{SmallVec, smallvec};
24
25pub(crate) type Riscv64Callee = Callee<Riscv64MachineDeps>;
27
28pub struct Riscv64MachineDeps;
31
32impl IsaFlags for RiscvFlags {}
33
34impl RiscvFlags {
35 pub(crate) fn min_vec_reg_size(&self) -> u64 {
36 let entries = [
37 (self.has_zvl65536b(), 65536),
38 (self.has_zvl32768b(), 32768),
39 (self.has_zvl16384b(), 16384),
40 (self.has_zvl8192b(), 8192),
41 (self.has_zvl4096b(), 4096),
42 (self.has_zvl2048b(), 2048),
43 (self.has_zvl1024b(), 1024),
44 (self.has_zvl512b(), 512),
45 (self.has_zvl256b(), 256),
46 (self.has_v(), 128),
49 (self.has_zvl128b(), 128),
50 (self.has_zvl64b(), 64),
51 (self.has_zvl32b(), 32),
52 ];
53
54 for (has_flag, size) in entries.into_iter() {
55 if !has_flag {
56 continue;
57 }
58
59 return core::cmp::min(size, 1024);
62 }
63
64 return 0;
65 }
66}
67
68impl ABIMachineSpec for Riscv64MachineDeps {
69 type I = Inst;
70 type F = RiscvFlags;
71
72 const STACK_ARG_RET_SIZE_LIMIT: u32 = 128 * 1024 * 1024;
76
77 fn word_bits() -> u32 {
78 64
79 }
80
81 fn stack_align(_call_conv: isa::CallConv) -> u32 {
83 16
84 }
85
86 fn compute_arg_locs(
87 call_conv: isa::CallConv,
88 flags: &settings::Flags,
89 params: &[ir::AbiParam],
90 args_or_rets: ArgsOrRets,
91 add_ret_area_ptr: bool,
92 mut args: ArgsAccumulator,
93 ) -> CodegenResult<(u32, Option<usize>)> {
94 assert_ne!(
97 call_conv,
98 isa::CallConv::Winch,
99 "riscv64 does not support the 'winch' calling convention yet"
100 );
101
102 let (x_start, x_end, f_start, f_end) = match args_or_rets {
105 ArgsOrRets::Args => (10, 17, 10, 17),
106 ArgsOrRets::Rets => (10, 11, 10, 11),
107 };
108 let mut next_x_reg = x_start;
109 let mut next_f_reg = f_start;
110 let mut next_stack: u32 = 0;
112
113 let is_tail = call_conv == isa::CallConv::Tail;
114 let ret_area_ptr = if add_ret_area_ptr && !is_tail {
115 assert!(ArgsOrRets::Args == args_or_rets);
116 next_x_reg += 1;
117 Some(ABIArg::reg(
118 x_reg(x_start).to_real_reg().unwrap(),
119 I64,
120 ir::ArgumentExtension::None,
121 ir::ArgumentPurpose::Normal,
122 ))
123 } else {
124 None
125 };
126
127 for param in params {
128 if let ir::ArgumentPurpose::StructArgument(_) = param.purpose {
129 panic!(
130 "StructArgument parameters are not supported on riscv64. \
131 Use regular pointer arguments instead."
132 );
133 }
134
135 let (rcs, reg_tys) = Inst::rc_for_type(¶m.value_type)?;
137 let mut slots = ABIArgSlotVec::new();
138 for (rc, reg_ty) in rcs.iter().zip(reg_tys.iter()) {
139 let next_reg = if (next_x_reg <= x_end) && *rc == RegClass::Int {
140 let x = Some(x_reg(next_x_reg));
141 next_x_reg += 1;
142 x
143 } else if (next_f_reg <= f_end) && *rc == RegClass::Float {
144 let x = Some(f_reg(next_f_reg));
145 next_f_reg += 1;
146 x
147 } else {
148 None
149 };
150 if let Some(reg) = next_reg {
151 slots.push(ABIArgSlot::Reg {
152 reg: reg.to_real_reg().unwrap(),
153 ty: *reg_ty,
154 extension: param.extension,
155 });
156 } else {
157 if args_or_rets == ArgsOrRets::Rets && !flags.enable_multi_ret_implicit_sret() {
158 return Err(crate::CodegenError::Unsupported(
159 "Too many return values to fit in registers. \
160 Use a StructReturn argument instead. (#9510)"
161 .to_owned(),
162 ));
163 }
164
165 let size = reg_ty.bits() / 8;
168 let size = core::cmp::max(size, 8);
169 debug_assert!(size.is_power_of_two());
171 next_stack = align_to(next_stack, size);
172 slots.push(ABIArgSlot::Stack {
173 offset: next_stack as i64,
174 ty: *reg_ty,
175 extension: param.extension,
176 });
177 next_stack += size;
178 }
179 }
180 args.push(ABIArg::Slots {
181 slots,
182 purpose: param.purpose,
183 });
184 }
185
186 let ret_area_ptr = if add_ret_area_ptr && is_tail {
187 assert!(ArgsOrRets::Args == args_or_rets);
188 Some(if next_x_reg <= x_end {
189 ABIArg::reg(
190 x_reg(next_x_reg).to_real_reg().unwrap(),
191 I64,
192 ir::ArgumentExtension::None,
193 ir::ArgumentPurpose::Normal,
194 )
195 } else {
196 next_stack = align_to(next_stack, 8);
197 let offset = next_stack as i64;
198 next_stack += 8;
199 ABIArg::stack(
200 offset,
201 I64,
202 ir::ArgumentExtension::None,
203 ir::ArgumentPurpose::Normal,
204 )
205 })
206 } else {
207 ret_area_ptr
208 };
209 let pos = if let Some(ret_area_ptr) = ret_area_ptr {
210 args.push_non_formal(ret_area_ptr);
211 Some(args.args().len() - 1)
212 } else {
213 None
214 };
215
216 next_stack = align_to(next_stack, Self::stack_align(call_conv));
217
218 Ok((next_stack, pos))
219 }
220
221 fn gen_load_stack(mem: StackAMode, into_reg: Writable<Reg>, ty: Type) -> Inst {
222 Inst::gen_load(into_reg, mem.into(), ty, MemFlagsData::trusted())
223 }
224
225 fn gen_store_stack(mem: StackAMode, from_reg: Reg, ty: Type) -> Inst {
226 Inst::gen_store(mem.into(), from_reg, ty, MemFlagsData::trusted())
227 }
228
229 fn gen_move(to_reg: Writable<Reg>, from_reg: Reg, ty: Type) -> Inst {
230 Inst::gen_move(to_reg, from_reg, ty)
231 }
232
233 fn gen_extend(
234 to_reg: Writable<Reg>,
235 from_reg: Reg,
236 signed: bool,
237 from_bits: u8,
238 to_bits: u8,
239 ) -> Inst {
240 assert!(from_bits < to_bits);
241 Inst::Extend {
242 rd: to_reg,
243 rn: from_reg,
244 signed,
245 from_bits,
246 to_bits,
247 }
248 }
249
250 fn get_ext_mode(
251 _call_conv: isa::CallConv,
252 specified: ir::ArgumentExtension,
253 _location: ABIArgLocation,
254 ) -> ir::ArgumentExtension {
255 specified
256 }
257
258 fn gen_args(args: Box<[ArgPair]>) -> Inst {
259 Inst::Args { args }
260 }
261
262 fn gen_rets(rets: Box<[RetPair]>) -> Inst {
263 Inst::Rets { rets }
264 }
265
266 fn get_stacklimit_reg(_call_conv: isa::CallConv) -> Reg {
267 spilltmp_reg()
268 }
269
270 fn gen_add_imm(
271 _call_conv: isa::CallConv,
272 into_reg: Writable<Reg>,
273 from_reg: Reg,
274 imm: u32,
275 ) -> SmallInstVec<Inst> {
276 let mut insts = SmallInstVec::new();
277 if let Some(imm12) = Imm12::maybe_from_u64(imm as u64) {
278 insts.push(Inst::AluRRImm12 {
279 alu_op: AluOPRRI::Addi,
280 rd: into_reg,
281 rs: from_reg,
282 imm12,
283 });
284 } else {
285 insts.extend(Inst::load_constant_u32(
286 writable_spilltmp_reg2(),
287 imm as u64,
288 ));
289 insts.push(Inst::AluRRR {
290 alu_op: AluOPRRR::Add,
291 rd: into_reg,
292 rs1: spilltmp_reg2(),
293 rs2: from_reg,
294 });
295 }
296 insts
297 }
298
299 fn gen_stack_lower_bound_trap(limit_reg: Reg) -> SmallInstVec<Inst> {
300 let mut insts = SmallVec::new();
301 insts.push(Inst::TrapIf {
302 cmp: IntegerCompare {
303 kind: IntCC::UnsignedLessThan,
304 rs1: stack_reg(),
305 rs2: limit_reg,
306 },
307 trap_code: ir::TrapCode::STACK_OVERFLOW,
308 });
309 insts
310 }
311
312 fn gen_stack_lower_bound_jump(limit_reg: Reg, handler: &ExternalName) -> SmallInstVec<Inst> {
313 smallvec![Inst::StackLimitJump {
314 limit: limit_reg,
315 handler: Box::new(handler.clone()),
316 }]
317 }
318
319 fn gen_get_stack_addr(mem: StackAMode, into_reg: Writable<Reg>) -> Inst {
320 Inst::LoadAddr {
321 rd: into_reg,
322 mem: mem.into(),
323 }
324 }
325
326 fn gen_load_base_offset(into_reg: Writable<Reg>, base: Reg, offset: i32, ty: Type) -> Inst {
327 let mem = AMode::RegOffset(base, offset as i64);
328 Inst::gen_load(into_reg, mem, ty, MemFlagsData::trusted())
329 }
330
331 fn gen_store_base_offset(base: Reg, offset: i32, from_reg: Reg, ty: Type) -> Inst {
332 let mem = AMode::RegOffset(base, offset as i64);
333 Inst::gen_store(mem, from_reg, ty, MemFlagsData::trusted())
334 }
335
336 fn gen_sp_reg_adjust(amount: i32) -> SmallInstVec<Inst> {
337 let mut insts = SmallVec::new();
338
339 if amount == 0 {
340 return insts;
341 }
342
343 if let Some(imm) = Imm12::maybe_from_i64(amount as i64) {
344 insts.push(Inst::AluRRImm12 {
345 alu_op: AluOPRRI::Addi,
346 rd: writable_stack_reg(),
347 rs: stack_reg(),
348 imm12: imm,
349 })
350 } else {
351 let tmp = writable_spilltmp_reg();
352 insts.extend(Inst::load_constant_u64(tmp, amount as i64 as u64));
353 insts.push(Inst::AluRRR {
354 alu_op: AluOPRRR::Add,
355 rd: writable_stack_reg(),
356 rs1: stack_reg(),
357 rs2: tmp.to_reg(),
358 });
359 }
360
361 insts
362 }
363
364 fn gen_prologue_frame_setup(
365 _call_conv: isa::CallConv,
366 flags: &settings::Flags,
367 _isa_flags: &RiscvFlags,
368 frame_layout: &FrameLayout,
369 ) -> SmallInstVec<Inst> {
370 let mut insts = SmallVec::new();
371
372 if frame_layout.setup_area_size > 0 {
373 insts.extend(Self::gen_sp_reg_adjust(-16));
378 insts.push(Inst::gen_store(
379 AMode::SPOffset(8),
380 link_reg(),
381 I64,
382 MemFlagsData::trusted(),
383 ));
384 insts.push(Inst::gen_store(
385 AMode::SPOffset(0),
386 fp_reg(),
387 I64,
388 MemFlagsData::trusted(),
389 ));
390
391 if flags.unwind_info() {
392 insts.push(Inst::Unwind {
393 inst: UnwindInst::PushFrameRegs {
394 offset_upward_to_caller_sp: frame_layout.setup_area_size,
395 },
396 });
397 }
398 insts.push(Inst::Mov {
399 rd: writable_fp_reg(),
400 rm: stack_reg(),
401 ty: I64,
402 });
403 }
404
405 insts
406 }
407 fn gen_epilogue_frame_restore(
409 call_conv: isa::CallConv,
410 _flags: &settings::Flags,
411 _isa_flags: &RiscvFlags,
412 frame_layout: &FrameLayout,
413 ) -> SmallInstVec<Inst> {
414 let mut insts = SmallVec::new();
415
416 if frame_layout.setup_area_size > 0 {
417 insts.push(Inst::gen_load(
418 writable_link_reg(),
419 AMode::SPOffset(8),
420 I64,
421 MemFlagsData::trusted(),
422 ));
423 insts.push(Inst::gen_load(
424 writable_fp_reg(),
425 AMode::SPOffset(0),
426 I64,
427 MemFlagsData::trusted(),
428 ));
429 insts.extend(Self::gen_sp_reg_adjust(16));
430 }
431
432 if call_conv == isa::CallConv::Tail && frame_layout.tail_args_size > 0 {
433 insts.extend(Self::gen_sp_reg_adjust(
434 frame_layout.tail_args_size.try_into().unwrap(),
435 ));
436 }
437
438 insts
439 }
440
441 fn gen_return(
442 _call_conv: isa::CallConv,
443 _isa_flags: &RiscvFlags,
444 _frame_layout: &FrameLayout,
445 ) -> SmallInstVec<Inst> {
446 smallvec![Inst::Ret {}]
447 }
448
449 fn gen_probestack(insts: &mut SmallInstVec<Self::I>, frame_size: u32) {
450 insts.extend(Inst::load_constant_u32(writable_a0(), frame_size as u64));
451 let mut info = CallInfo::empty(
452 ExternalName::LibCall(LibCall::Probestack),
453 CallConv::SystemV,
454 );
455 info.uses.push(CallArgPair {
456 vreg: a0(),
457 preg: a0(),
458 });
459 insts.push(Inst::Call {
460 info: Box::new(info),
461 });
462 }
463
464 fn gen_clobber_save(
465 _call_conv: isa::CallConv,
466 flags: &settings::Flags,
467 frame_layout: &FrameLayout,
468 ) -> SmallVec<[Inst; 16]> {
469 let mut insts = SmallVec::new();
470 let setup_frame = frame_layout.setup_area_size > 0;
471
472 if flags.unwind_info() && setup_frame {
473 insts.push(Inst::Unwind {
481 inst: UnwindInst::DefineNewFrame {
482 offset_downward_to_clobbers: frame_layout.clobber_size,
483 offset_upward_to_caller_sp: frame_layout.setup_area_size,
484 },
485 });
486 }
487
488 let incoming_args_diff = frame_layout.tail_args_size - frame_layout.incoming_args_size;
489 if incoming_args_diff > 0 {
490 insts.extend(Self::gen_sp_reg_adjust(-(incoming_args_diff as i32)));
492
493 if setup_frame {
494 insts.push(Inst::gen_store(
497 AMode::SPOffset(8),
498 link_reg(),
499 I64,
500 MemFlagsData::trusted(),
501 ));
502 insts.push(Inst::gen_load(
506 writable_spilltmp_reg(),
507 AMode::SPOffset(i64::from(incoming_args_diff)),
508 I64,
509 MemFlagsData::trusted(),
510 ));
511 insts.push(Inst::gen_store(
512 AMode::SPOffset(0),
513 spilltmp_reg(),
514 I64,
515 MemFlagsData::trusted(),
516 ));
517
518 insts.push(Inst::gen_move(writable_fp_reg(), stack_reg(), I64));
520 }
521 }
522
523 let stack_size = frame_layout.clobber_size
526 + frame_layout.fixed_frame_storage_size
527 + frame_layout.outgoing_args_size;
528
529 if stack_size > 0 {
532 insts.extend(Self::gen_sp_reg_adjust(-(stack_size as i32)));
533
534 let mut cur_offset = 0;
535 for reg in &frame_layout.clobbered_callee_saves {
536 let r_reg = reg.to_reg();
537 let ty = match r_reg.class() {
538 RegClass::Int => I64,
539 RegClass::Float => F64,
540 RegClass::Vector => I8X16,
541 };
542 cur_offset = align_to(cur_offset, ty.bytes());
543 insts.push(Inst::gen_store(
544 AMode::SPOffset(i64::from(stack_size - cur_offset - ty.bytes())),
545 Reg::from(reg.to_reg()),
546 ty,
547 MemFlagsData::trusted(),
548 ));
549
550 if flags.unwind_info() {
551 insts.push(Inst::Unwind {
552 inst: UnwindInst::SaveReg {
553 clobber_offset: frame_layout.clobber_size - cur_offset - ty.bytes(),
554 reg: r_reg,
555 },
556 });
557 }
558
559 cur_offset += ty.bytes();
560 assert!(cur_offset <= stack_size);
561 }
562 }
563 insts
564 }
565
566 fn gen_clobber_restore(
567 _call_conv: isa::CallConv,
568 _flags: &settings::Flags,
569 frame_layout: &FrameLayout,
570 ) -> SmallVec<[Inst; 16]> {
571 let mut insts = SmallVec::new();
572
573 let stack_size = frame_layout.clobber_size
574 + frame_layout.fixed_frame_storage_size
575 + frame_layout.outgoing_args_size;
576 let mut cur_offset = 0;
577
578 for reg in &frame_layout.clobbered_callee_saves {
579 let rreg = reg.to_reg();
580 let ty = match rreg.class() {
581 RegClass::Int => I64,
582 RegClass::Float => F64,
583 RegClass::Vector => I8X16,
584 };
585 cur_offset = align_to(cur_offset, ty.bytes());
586 insts.push(Inst::gen_load(
587 reg.map(Reg::from),
588 AMode::SPOffset(i64::from(stack_size - cur_offset - ty.bytes())),
589 ty,
590 MemFlagsData::trusted(),
591 ));
592 cur_offset += ty.bytes();
593 }
594
595 if stack_size > 0 {
596 insts.extend(Self::gen_sp_reg_adjust(stack_size as i32));
597 }
598
599 insts
600 }
601
602 fn gen_memcpy<F: FnMut(Type) -> Writable<Reg>>(
603 call_conv: isa::CallConv,
604 dst: Reg,
605 src: Reg,
606 size: usize,
607 mut alloc_tmp: F,
608 ) -> SmallVec<[Self::I; 8]> {
609 let mut insts = SmallVec::new();
610 let arg0 = Writable::from_reg(x_reg(10));
611 let arg1 = Writable::from_reg(x_reg(11));
612 let arg2 = Writable::from_reg(x_reg(12));
613 let tmp = alloc_tmp(Self::word_type());
614 insts.extend(Inst::load_constant_u64(tmp, size as u64));
615 insts.push(Inst::Call {
616 info: Box::new(CallInfo {
617 dest: ExternalName::LibCall(LibCall::Memcpy),
618 uses: smallvec![
619 CallArgPair {
620 vreg: dst,
621 preg: arg0.to_reg()
622 },
623 CallArgPair {
624 vreg: src,
625 preg: arg1.to_reg()
626 },
627 CallArgPair {
628 vreg: tmp.to_reg(),
629 preg: arg2.to_reg()
630 }
631 ],
632 defs: smallvec![],
633 clobbers: Self::get_regs_clobbered_by_call(call_conv, false),
634 caller_conv: call_conv,
635 callee_conv: call_conv,
636 callee_pop_size: 0,
637 try_call_info: None,
638 patchable: false,
639 }),
640 });
641 insts
642 }
643
644 fn get_number_of_spillslots_for_value(
645 rc: RegClass,
646 _target_vector_bytes: u32,
647 isa_flags: &RiscvFlags,
648 ) -> u32 {
649 match rc {
651 RegClass::Int => 1,
652 RegClass::Float => 1,
653 RegClass::Vector => (isa_flags.min_vec_reg_size() / 8) as u32,
654 }
655 }
656
657 fn get_machine_env(_flags: &settings::Flags, _call_conv: isa::CallConv) -> &MachineEnv {
658 static MACHINE_ENV: MachineEnv = create_reg_environment();
659 &MACHINE_ENV
660 }
661
662 fn get_regs_clobbered_by_call(
663 call_conv_of_callee: isa::CallConv,
664 is_exception: bool,
665 ) -> PRegSet {
666 match call_conv_of_callee {
667 isa::CallConv::Tail if is_exception => ALL_CLOBBERS,
668 isa::CallConv::PreserveAll if is_exception => ALL_CLOBBERS,
674 isa::CallConv::PreserveAll => NO_CLOBBERS,
675 _ => DEFAULT_CLOBBERS,
676 }
677 }
678
679 fn compute_frame_layout(
680 call_conv: isa::CallConv,
681 flags: &settings::Flags,
682 _sig: &Signature,
683 regs: &[Writable<RealReg>],
684 function_calls: FunctionCalls,
685 incoming_args_size: u32,
686 tail_args_size: u32,
687 stackslots_size: u32,
688 fixed_frame_storage_size: u32,
689 outgoing_args_size: u32,
690 ) -> FrameLayout {
691 let is_callee_saved = |reg: &Writable<RealReg>| match call_conv {
692 isa::CallConv::PreserveAll => true,
693 _ => DEFAULT_CALLEE_SAVES.contains(reg.to_reg().into()),
694 };
695 let mut regs: Vec<Writable<RealReg>> =
696 regs.iter().cloned().filter(is_callee_saved).collect();
697
698 regs.sort_unstable();
699
700 let clobber_size = compute_clobber_size(®s);
702
703 let setup_area_size = if flags.preserve_frame_pointers()
705 || function_calls != FunctionCalls::None
706 || incoming_args_size > 0
709 || clobber_size > 0
710 || fixed_frame_storage_size > 0
711 {
712 16 } else {
714 0
715 };
716
717 FrameLayout {
719 word_bytes: 8,
720 incoming_args_size,
721 tail_args_size,
722 setup_area_size,
723 clobber_size,
724 fixed_frame_storage_size,
725 stackslots_size,
726 outgoing_args_size,
727 clobbered_callee_saves: regs,
728 function_calls,
729 }
730 }
731
732 fn gen_inline_probestack(
733 insts: &mut SmallInstVec<Self::I>,
734 call_conv: isa::CallConv,
735 frame_size: u32,
736 guard_size: u32,
737 ) {
738 const PROBE_MAX_UNROLL: u32 = 3;
740
741 let probe_count = frame_size / guard_size;
744 if probe_count == 0 {
745 return;
747 }
748
749 if call_conv == isa::CallConv::PreserveAll {
755 Self::gen_probestack_unroll(insts, writable_spilltmp_reg2(), guard_size, probe_count);
756 return;
757 }
758
759 let tmp = Writable::from_reg(x_reg(28)); if probe_count <= PROBE_MAX_UNROLL {
763 Self::gen_probestack_unroll(insts, tmp, guard_size, probe_count)
764 } else {
765 insts.push(Inst::StackProbeLoop {
766 guard_size,
767 probe_count,
768 tmp,
769 });
770 }
771 }
772
773 fn retval_temp_reg(_call_conv_of_callee: isa::CallConv) -> Writable<Reg> {
774 Writable::from_reg(regs::x_reg(12))
777 }
778
779 fn exception_payload_regs(call_conv: isa::CallConv) -> &'static [Reg] {
780 const PAYLOAD_REGS: &'static [Reg] = &[regs::a0(), regs::a1()];
781 match call_conv {
782 isa::CallConv::SystemV | isa::CallConv::Tail | isa::CallConv::PreserveAll => {
783 PAYLOAD_REGS
784 }
785 _ => &[],
786 }
787 }
788}
789
790const DEFAULT_CALLEE_SAVES: PRegSet = PRegSet::empty()
792 .with(px_reg(2))
794 .with(px_reg(8))
795 .with(px_reg(9))
796 .with(px_reg(18))
797 .with(px_reg(19))
798 .with(px_reg(20))
799 .with(px_reg(21))
800 .with(px_reg(22))
801 .with(px_reg(23))
802 .with(px_reg(24))
803 .with(px_reg(25))
804 .with(px_reg(26))
805 .with(px_reg(27))
806 .with(pf_reg(8))
808 .with(pf_reg(18))
809 .with(pf_reg(19))
810 .with(pf_reg(20))
811 .with(pf_reg(21))
812 .with(pf_reg(22))
813 .with(pf_reg(23))
814 .with(pf_reg(24))
815 .with(pf_reg(25))
816 .with(pf_reg(26))
817 .with(pf_reg(27));
818
819fn compute_clobber_size(clobbers: &[Writable<RealReg>]) -> u32 {
820 let mut clobbered_size = 0;
821 for reg in clobbers {
822 match reg.to_reg().class() {
823 RegClass::Int => {
824 clobbered_size += 8;
825 }
826 RegClass::Float => {
827 clobbered_size += 8;
828 }
829 RegClass::Vector => {
830 clobbered_size = align_to(clobbered_size, 16);
831 clobbered_size += 16;
832 }
833 }
834 }
835 align_to(clobbered_size, 16)
836}
837
838const DEFAULT_CLOBBERS: PRegSet = PRegSet::empty()
839 .with(px_reg(1))
840 .with(px_reg(5))
841 .with(px_reg(6))
842 .with(px_reg(7))
843 .with(px_reg(10))
844 .with(px_reg(11))
845 .with(px_reg(12))
846 .with(px_reg(13))
847 .with(px_reg(14))
848 .with(px_reg(15))
849 .with(px_reg(16))
850 .with(px_reg(17))
851 .with(px_reg(28))
852 .with(px_reg(29))
853 .with(px_reg(30))
854 .with(px_reg(31))
855 .with(pf_reg(0))
857 .with(pf_reg(1))
858 .with(pf_reg(2))
859 .with(pf_reg(3))
860 .with(pf_reg(4))
861 .with(pf_reg(5))
862 .with(pf_reg(6))
863 .with(pf_reg(7))
864 .with(pf_reg(9))
865 .with(pf_reg(10))
866 .with(pf_reg(11))
867 .with(pf_reg(12))
868 .with(pf_reg(13))
869 .with(pf_reg(14))
870 .with(pf_reg(15))
871 .with(pf_reg(16))
872 .with(pf_reg(17))
873 .with(pf_reg(28))
874 .with(pf_reg(29))
875 .with(pf_reg(30))
876 .with(pf_reg(31))
877 .with(pv_reg(0))
879 .with(pv_reg(1))
880 .with(pv_reg(2))
881 .with(pv_reg(3))
882 .with(pv_reg(4))
883 .with(pv_reg(5))
884 .with(pv_reg(6))
885 .with(pv_reg(7))
886 .with(pv_reg(8))
887 .with(pv_reg(9))
888 .with(pv_reg(10))
889 .with(pv_reg(11))
890 .with(pv_reg(12))
891 .with(pv_reg(13))
892 .with(pv_reg(14))
893 .with(pv_reg(15))
894 .with(pv_reg(16))
895 .with(pv_reg(17))
896 .with(pv_reg(18))
897 .with(pv_reg(19))
898 .with(pv_reg(20))
899 .with(pv_reg(21))
900 .with(pv_reg(22))
901 .with(pv_reg(23))
902 .with(pv_reg(24))
903 .with(pv_reg(25))
904 .with(pv_reg(26))
905 .with(pv_reg(27))
906 .with(pv_reg(28))
907 .with(pv_reg(29))
908 .with(pv_reg(30))
909 .with(pv_reg(31));
910
911const ALL_CLOBBERS: PRegSet = PRegSet::empty()
912 .with(px_reg(3))
914 .with(px_reg(4))
915 .with(px_reg(5))
916 .with(px_reg(6))
917 .with(px_reg(7))
918 .with(px_reg(8))
919 .with(px_reg(9))
920 .with(px_reg(10))
921 .with(px_reg(11))
922 .with(px_reg(12))
923 .with(px_reg(13))
924 .with(px_reg(14))
925 .with(px_reg(15))
926 .with(px_reg(16))
927 .with(px_reg(17))
928 .with(px_reg(18))
929 .with(px_reg(19))
930 .with(px_reg(20))
931 .with(px_reg(21))
932 .with(px_reg(22))
933 .with(px_reg(23))
934 .with(px_reg(24))
935 .with(px_reg(25))
936 .with(px_reg(26))
937 .with(px_reg(27))
938 .with(px_reg(28))
939 .with(px_reg(29))
940 .with(px_reg(30))
941 .with(px_reg(31))
942 .with(pf_reg(0))
944 .with(pf_reg(1))
945 .with(pf_reg(2))
946 .with(pf_reg(3))
947 .with(pf_reg(4))
948 .with(pf_reg(5))
949 .with(pf_reg(6))
950 .with(pf_reg(7))
951 .with(pf_reg(8))
952 .with(pf_reg(9))
953 .with(pf_reg(10))
954 .with(pf_reg(11))
955 .with(pf_reg(12))
956 .with(pf_reg(13))
957 .with(pf_reg(14))
958 .with(pf_reg(15))
959 .with(pf_reg(16))
960 .with(pf_reg(17))
961 .with(pf_reg(18))
962 .with(pf_reg(19))
963 .with(pf_reg(20))
964 .with(pf_reg(21))
965 .with(pf_reg(22))
966 .with(pf_reg(23))
967 .with(pf_reg(24))
968 .with(pf_reg(25))
969 .with(pf_reg(26))
970 .with(pf_reg(27))
971 .with(pf_reg(28))
972 .with(pf_reg(29))
973 .with(pf_reg(30))
974 .with(pf_reg(31))
975 .with(pv_reg(0))
977 .with(pv_reg(1))
978 .with(pv_reg(2))
979 .with(pv_reg(3))
980 .with(pv_reg(4))
981 .with(pv_reg(5))
982 .with(pv_reg(6))
983 .with(pv_reg(7))
984 .with(pv_reg(8))
985 .with(pv_reg(9))
986 .with(pv_reg(10))
987 .with(pv_reg(11))
988 .with(pv_reg(12))
989 .with(pv_reg(13))
990 .with(pv_reg(14))
991 .with(pv_reg(15))
992 .with(pv_reg(16))
993 .with(pv_reg(17))
994 .with(pv_reg(18))
995 .with(pv_reg(19))
996 .with(pv_reg(20))
997 .with(pv_reg(21))
998 .with(pv_reg(22))
999 .with(pv_reg(23))
1000 .with(pv_reg(24))
1001 .with(pv_reg(25))
1002 .with(pv_reg(26))
1003 .with(pv_reg(27))
1004 .with(pv_reg(28))
1005 .with(pv_reg(29))
1006 .with(pv_reg(30))
1007 .with(pv_reg(31));
1008
1009const NO_CLOBBERS: PRegSet = PRegSet::empty();
1010
1011const fn create_reg_environment() -> MachineEnv {
1012 let preferred_regs_by_class: [PRegSet; 3] = [
1023 PRegSet::empty()
1024 .with(px_reg(10))
1025 .with(px_reg(11))
1026 .with(px_reg(12))
1027 .with(px_reg(13))
1028 .with(px_reg(14))
1029 .with(px_reg(15)),
1030 PRegSet::empty()
1031 .with(pf_reg(10))
1032 .with(pf_reg(11))
1033 .with(pf_reg(12))
1034 .with(pf_reg(13))
1035 .with(pf_reg(14))
1036 .with(pf_reg(15)),
1037 PRegSet::empty()
1038 .with(pv_reg(8))
1039 .with(pv_reg(9))
1040 .with(pv_reg(10))
1041 .with(pv_reg(11))
1042 .with(pv_reg(12))
1043 .with(pv_reg(13))
1044 .with(pv_reg(14))
1045 .with(pv_reg(15)),
1046 ];
1047
1048 let non_preferred_regs_by_class: [PRegSet; 3] = [
1049 PRegSet::empty()
1052 .with(px_reg(5))
1053 .with(px_reg(6))
1054 .with(px_reg(7))
1055 .with(px_reg(16))
1057 .with(px_reg(17))
1058 .with(px_reg(28))
1059 .with(px_reg(29))
1060 .with(px_reg(9))
1063 .with(px_reg(18))
1065 .with(px_reg(19))
1066 .with(px_reg(20))
1067 .with(px_reg(21))
1068 .with(px_reg(22))
1069 .with(px_reg(23))
1070 .with(px_reg(24))
1071 .with(px_reg(25))
1072 .with(px_reg(26))
1073 .with(px_reg(27)),
1074 PRegSet::empty()
1076 .with(pf_reg(0))
1077 .with(pf_reg(1))
1078 .with(pf_reg(2))
1079 .with(pf_reg(3))
1080 .with(pf_reg(4))
1081 .with(pf_reg(5))
1082 .with(pf_reg(6))
1083 .with(pf_reg(7))
1084 .with(pf_reg(16))
1085 .with(pf_reg(17))
1086 .with(pf_reg(28))
1087 .with(pf_reg(29))
1088 .with(pf_reg(30))
1089 .with(pf_reg(31))
1090 .with(pf_reg(8))
1093 .with(pf_reg(9))
1094 .with(pf_reg(18))
1095 .with(pf_reg(19))
1096 .with(pf_reg(20))
1097 .with(pf_reg(21))
1098 .with(pf_reg(22))
1099 .with(pf_reg(23))
1100 .with(pf_reg(24))
1101 .with(pf_reg(25))
1102 .with(pf_reg(26))
1103 .with(pf_reg(27)),
1104 PRegSet::empty()
1105 .with(pv_reg(0))
1106 .with(pv_reg(1))
1107 .with(pv_reg(2))
1108 .with(pv_reg(3))
1109 .with(pv_reg(4))
1110 .with(pv_reg(5))
1111 .with(pv_reg(6))
1112 .with(pv_reg(7))
1113 .with(pv_reg(16))
1114 .with(pv_reg(17))
1115 .with(pv_reg(18))
1116 .with(pv_reg(19))
1117 .with(pv_reg(20))
1118 .with(pv_reg(21))
1119 .with(pv_reg(22))
1120 .with(pv_reg(23))
1121 .with(pv_reg(24))
1122 .with(pv_reg(25))
1123 .with(pv_reg(26))
1124 .with(pv_reg(27))
1125 .with(pv_reg(28))
1126 .with(pv_reg(29))
1127 .with(pv_reg(30))
1128 .with(pv_reg(31)),
1129 ];
1130
1131 MachineEnv {
1132 preferred_regs_by_class,
1133 non_preferred_regs_by_class,
1134 fixed_stack_slots: vec![],
1135 scratch_by_class: [None, None, None],
1136 }
1137}
1138
1139impl Riscv64MachineDeps {
1140 fn gen_probestack_unroll(
1141 insts: &mut SmallInstVec<Inst>,
1142 tmp: Writable<Reg>,
1143 guard_size: u32,
1144 probe_count: u32,
1145 ) {
1146 insts.extend(Inst::load_constant_u64(tmp, (-(guard_size as i64)) as u64));
1158
1159 for _ in 0..probe_count {
1160 insts.push(Inst::AluRRR {
1161 alu_op: AluOPRRR::Add,
1162 rd: writable_stack_reg(),
1163 rs1: stack_reg(),
1164 rs2: tmp.to_reg(),
1165 });
1166
1167 insts.push(Inst::gen_store(
1168 AMode::SPOffset(0),
1169 zero_reg(),
1170 I32,
1171 MemFlagsData::trusted(),
1172 ));
1173 }
1174
1175 insts.extend(Self::gen_sp_reg_adjust((guard_size * probe_count) as i32));
1177 }
1178}