Skip to content

Commit

Permalink
Return the last result through registers in the winch calling convention
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Mar 18, 2024
1 parent 648288c commit 30e21ba
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
23 changes: 14 additions & 9 deletions cranelift/codegen/src/isa/x64/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ impl ABIMachineSpec for X64ABIMachineSpec {
next_stack = 32;
}

for param in params {
for (ix, param) in params.iter().enumerate() {
let last_param = ix == params.len() - 1;

if let ir::ArgumentPurpose::StructArgument(size) = param.purpose {
let offset = next_stack as i64;
let size = size;
Expand Down Expand Up @@ -218,14 +220,18 @@ impl ABIMachineSpec for X64ABIMachineSpec {
ArgsOrRets::Args => {
get_intreg_for_arg(&call_conv, next_gpr, next_param_idx)
}
ArgsOrRets::Rets => get_intreg_for_retval(&call_conv, flags, next_gpr),
ArgsOrRets::Rets => {
get_intreg_for_retval(&call_conv, flags, next_gpr, last_param)
}
}
} else {
match args_or_rets {
ArgsOrRets::Args => {
get_fltreg_for_arg(&call_conv, next_vreg, next_param_idx)
}
ArgsOrRets::Rets => get_fltreg_for_retval(&call_conv, next_vreg),
ArgsOrRets::Rets => {
get_fltreg_for_retval(&call_conv, next_vreg, last_param)
}
}
};
next_param_idx += 1;
Expand Down Expand Up @@ -1037,6 +1043,7 @@ fn get_intreg_for_retval(
call_conv: &CallConv,
flags: &settings::Flags,
intreg_idx: usize,
is_last: bool,
) -> Option<Reg> {
match call_conv {
CallConv::Tail => match intreg_idx {
Expand Down Expand Up @@ -1067,16 +1074,13 @@ fn get_intreg_for_retval(
1 => Some(regs::rdx()), // The Rust ABI for i128s needs this.
_ => None,
},
CallConv::Winch => match intreg_idx {
0 => Some(regs::rax()),
_ => None,
},
CallConv::Winch => is_last.then(|| regs::rax()),
CallConv::Probestack => todo!(),
CallConv::WasmtimeSystemV | CallConv::AppleAarch64 => unreachable!(),
}
}

fn get_fltreg_for_retval(call_conv: &CallConv, fltreg_idx: usize) -> Option<Reg> {
fn get_fltreg_for_retval(call_conv: &CallConv, fltreg_idx: usize, is_last: bool) -> Option<Reg> {
match call_conv {
CallConv::Tail => match fltreg_idx {
0 => Some(regs::xmm0()),
Expand All @@ -1094,10 +1098,11 @@ fn get_fltreg_for_retval(call_conv: &CallConv, fltreg_idx: usize) -> Option<Reg>
1 => Some(regs::xmm1()),
_ => None,
},
CallConv::WindowsFastcall | CallConv::Winch => match fltreg_idx {
CallConv::WindowsFastcall => match fltreg_idx {
0 => Some(regs::xmm0()),
_ => None,
},
CallConv::Winch => is_last.then(|| regs::xmm0()),
CallConv::Probestack => todo!(),
CallConv::WasmtimeSystemV | CallConv::AppleAarch64 => unreachable!(),
}
Expand Down
71 changes: 71 additions & 0 deletions cranelift/filetests/filetests/isa/x64/winch.clif
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,74 @@ block0:
; popq %rbp
; retq

function %f6(i64) -> i32 {
sig0 = () -> i32, i32 winch
fn0 = %g sig0

block0(v0:i64):
v1, v2 = call fn0()
v3 = band.i32 v1, v2
return v3
}

; VCode:
; pushq %rbp
; movq %rsp, %rbp
; subq %rsp, $48, %rsp
; movq %rbx, 0(%rsp)
; movq %r12, 8(%rsp)
; movq %r13, 16(%rsp)
; movq %r14, 24(%rsp)
; movq %r15, 32(%rsp)
; block0:
; subq %rsp, $16, %rsp
; virtual_sp_offset_adjust 16
; lea 0(%rsp), %rdi
; load_ext_name %g+0, %r15
; call *%r15
; movq %rax, %r10
; movq 0(%rsp), %rax
; addq %rsp, $16, %rsp
; virtual_sp_offset_adjust -16
; movq %r10, %r9
; andl %eax, %r9d, %eax
; movq 0(%rsp), %rbx
; movq 8(%rsp), %r12
; movq 16(%rsp), %r13
; movq 24(%rsp), %r14
; movq 32(%rsp), %r15
; addq %rsp, $48, %rsp
; movq %rbp, %rsp
; popq %rbp
; ret
;
; Disassembled:
; block0: ; offset 0x0
; pushq %rbp
; movq %rsp, %rbp
; subq $0x30, %rsp
; movq %rbx, (%rsp)
; movq %r12, 8(%rsp)
; movq %r13, 0x10(%rsp)
; movq %r14, 0x18(%rsp)
; movq %r15, 0x20(%rsp)
; block1: ; offset 0x20
; subq $0x10, %rsp
; leaq (%rsp), %rdi
; movabsq $0, %r15 ; reloc_external Abs8 %g 0
; callq *%r15
; movq %rax, %r10
; movq (%rsp), %rax
; addq $0x10, %rsp
; movq %r10, %r9
; andl %r9d, %eax
; movq (%rsp), %rbx
; movq 8(%rsp), %r12
; movq 0x10(%rsp), %r13
; movq 0x18(%rsp), %r14
; movq 0x20(%rsp), %r15
; addq $0x30, %rsp
; movq %rbp, %rsp
; popq %rbp
; retq

0 comments on commit 30e21ba

Please sign in to comment.