Skip to content

Commit f5f517e

Browse files
authored
winch: Small clean-up for x64 (#5691)
This commit contains a small set of clean up items for x64. Notably: * Adds filetests * Documents why 16 for the arg base offset abi implementation, for clarity. * Fixes a bug in the spill implementation caught while anlyzing the filetests results. The fix consists of emitting a load instead of a store into the scratch register before spiiling its value. * Remove dead code for pretty printing registers which is not needed anymore since we now have proper disassembly.
1 parent 446337c commit f5f517e

File tree

8 files changed

+82
-102
lines changed

8 files changed

+82
-102
lines changed

winch/codegen/src/isa/reg.rs

-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ impl Reg {
2727
Self::new(PReg::new(enc, RegClass::Float))
2828
}
2929

30-
/// Get the class of the underlying register.
31-
pub fn class(self) -> RegClass {
32-
self.0.class()
33-
}
34-
3530
/// Get the encoding of the underlying register.
3631
pub fn hw_enc(self) -> u8 {
3732
self.0.hw_enc() as u8

winch/codegen/src/isa/x64/abi.rs

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ impl ABI for X64ABI {
4040
}
4141

4242
fn arg_base_offset(&self) -> u8 {
43+
// Two 8-byte slots, one for the return address and another
44+
// one for the frame pointer.
45+
// ┌──────────┬───────── Argument base
46+
// │ Ret │
47+
// │ Addr │
48+
// ├──────────┼
49+
// │ │
50+
// │ FP │
51+
// └──────────┴
4352
16
4453
}
4554

winch/codegen/src/isa/x64/masm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ impl Masm for MacroAssembler {
124124
}
125125

126126
fn epilogue(&mut self, locals_size: u32) {
127+
assert!(self.sp_offset == locals_size);
128+
127129
let rsp = rsp();
128130
if locals_size > 0 {
129131
self.asm.add_ir(locals_size as i32, rsp, OperandSize::S64);

winch/codegen/src/isa/x64/regs.rs

-94
Original file line numberDiff line numberDiff line change
@@ -142,97 +142,3 @@ const NON_ALLOCATABLE_GPR: u32 = (1 << ENC_RBP) | (1 << ENC_RSP) | (1 << ENC_R11
142142

143143
/// Bitmask to represent the available general purpose registers.
144144
pub(crate) const ALL_GPR: u32 = ALLOCATABLE_GPR & !NON_ALLOCATABLE_GPR;
145-
146-
// Temporarily removing the % from the register name
147-
// for debugging purposes only until winch gets disasm
148-
// support.
149-
pub(crate) fn reg_name(reg: Reg, size: u8) -> &'static str {
150-
match reg.class() {
151-
RegClass::Int => match (reg.hw_enc() as u8, size) {
152-
(ENC_RAX, 8) => "rax",
153-
(ENC_RAX, 4) => "eax",
154-
(ENC_RAX, 2) => "ax",
155-
(ENC_RAX, 1) => "al",
156-
(ENC_RBX, 8) => "rbx",
157-
(ENC_RBX, 4) => "ebx",
158-
(ENC_RBX, 2) => "bx",
159-
(ENC_RBX, 1) => "bl",
160-
(ENC_RCX, 8) => "rcx",
161-
(ENC_RCX, 4) => "ecx",
162-
(ENC_RCX, 2) => "cx",
163-
(ENC_RCX, 1) => "cl",
164-
(ENC_RDX, 8) => "rdx",
165-
(ENC_RDX, 4) => "edx",
166-
(ENC_RDX, 2) => "dx",
167-
(ENC_RDX, 1) => "dl",
168-
(ENC_RSI, 8) => "rsi",
169-
(ENC_RSI, 4) => "esi",
170-
(ENC_RSI, 2) => "si",
171-
(ENC_RSI, 1) => "sil",
172-
(ENC_RDI, 8) => "rdi",
173-
(ENC_RDI, 4) => "edi",
174-
(ENC_RDI, 2) => "di",
175-
(ENC_RDI, 1) => "dil",
176-
(ENC_RBP, 8) => "rbp",
177-
(ENC_RBP, 4) => "ebp",
178-
(ENC_RBP, 2) => "bp",
179-
(ENC_RBP, 1) => "bpl",
180-
(ENC_RSP, 8) => "rsp",
181-
(ENC_RSP, 4) => "esp",
182-
(ENC_RSP, 2) => "sp",
183-
(ENC_RSP, 1) => "spl",
184-
(ENC_R8, 8) => "r8",
185-
(ENC_R8, 4) => "r8d",
186-
(ENC_R8, 2) => "r8w",
187-
(ENC_R8, 1) => "r8b",
188-
(ENC_R9, 8) => "r9",
189-
(ENC_R9, 4) => "r9d",
190-
(ENC_R9, 2) => "r9w",
191-
(ENC_R9, 1) => "r9b",
192-
(ENC_R10, 8) => "r10",
193-
(ENC_R10, 4) => "r10d",
194-
(ENC_R10, 2) => "r10w",
195-
(ENC_R10, 1) => "r10b",
196-
(ENC_R11, 8) => "r11",
197-
(ENC_R11, 4) => "r11d",
198-
(ENC_R11, 2) => "r11w",
199-
(ENC_R11, 1) => "r11b",
200-
(ENC_R12, 8) => "r12",
201-
(ENC_R12, 4) => "r12d",
202-
(ENC_R12, 2) => "r12w",
203-
(ENC_R12, 1) => "r12b",
204-
(ENC_R13, 8) => "r13",
205-
(ENC_R13, 4) => "r13d",
206-
(ENC_R13, 2) => "r13w",
207-
(ENC_R13, 1) => "r13b",
208-
(ENC_R14, 8) => "r14",
209-
(ENC_R14, 4) => "r14d",
210-
(ENC_R14, 2) => "r14w",
211-
(ENC_R14, 1) => "r14b",
212-
(ENC_R15, 8) => "r15",
213-
(ENC_R15, 4) => "r15d",
214-
(ENC_R15, 2) => "r15w",
215-
(ENC_R15, 1) => "r15b",
216-
_ => panic!("Invalid Reg: {:?}", reg),
217-
},
218-
RegClass::Float => match reg.hw_enc() {
219-
0 => "xmm0",
220-
1 => "xmm1",
221-
2 => "xmm2",
222-
3 => "xmm3",
223-
4 => "xmm4",
224-
5 => "xmm5",
225-
6 => "xmm6",
226-
7 => "xmm7",
227-
8 => "xmm8",
228-
9 => "xmm9",
229-
10 => "xmm10",
230-
11 => "xmm11",
231-
12 => "xmm12",
232-
13 => "xmm13",
233-
14 => "xmm14",
234-
15 => "xmm15",
235-
_ => panic!("Invalid Reg: {:?}", reg),
236-
},
237-
}
238-
}

winch/codegen/src/regalloc.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ impl RegAlloc {
134134
.get_local(*index)
135135
.expect("valid local at slot");
136136
let addr = context.masm.local_address(&slot);
137-
context
138-
.masm
139-
.store(RegImm::reg(self.scratch), addr, slot.ty.into());
137+
context.masm.load(addr, self.scratch, slot.ty.into());
140138
let offset = context.masm.push(self.scratch);
141139
*v = Val::Memory(offset);
142140
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
;;! target = "x86_64"
2+
3+
(module
4+
(export "main" (func $main))
5+
6+
(func $main (result i32)
7+
(i32.const 10)
8+
(i32.const 20)
9+
i32.add)
10+
)
11+
;; 0: 55 push rbp
12+
;; 1: 4889e5 mov rbp, rsp
13+
;; 4: b80a000000 mov eax, 0xa
14+
;; 9: 83c014 add eax, 0x14
15+
;; c: 5d pop rbp
16+
;; d: c3 ret
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
;;! target = "x86_64"
2+
3+
(module
4+
(export "main" (func $main))
5+
6+
(func $main (result i32)
7+
(local $foo i32)
8+
(local $bar i32)
9+
(i32.const 10)
10+
(local.set $foo)
11+
(i32.const 20)
12+
(local.set $bar)
13+
14+
(local.get $foo)
15+
(local.get $bar)
16+
i32.add)
17+
)
18+
;; 0: 55 push rbp
19+
;; 1: 4889e5 mov rbp, rsp
20+
;; 4: 4883ec08 sub rsp, 8
21+
;; 8: 48c7042400000000 mov qword ptr [rsp], 0
22+
;; 10: b80a000000 mov eax, 0xa
23+
;; 15: 89442404 mov dword ptr [rsp + 4], eax
24+
;; 19: b814000000 mov eax, 0x14
25+
;; 1e: 890424 mov dword ptr [rsp], eax
26+
;; 21: 8b0424 mov eax, dword ptr [rsp]
27+
;; 24: 8b4c2404 mov ecx, dword ptr [rsp + 4]
28+
;; 28: 01c1 add ecx, eax
29+
;; 2a: 4889c8 mov rax, rcx
30+
;; 2d: 4883c408 add rsp, 8
31+
;; 31: 5d pop rbp
32+
;; 32: c3 ret
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
;;! target = "x86_64"
2+
3+
(module
4+
(export "main" (func $main))
5+
6+
(func $main (param i32) (param i32) (result i32)
7+
(local.get 0)
8+
(local.get 1)
9+
i32.add)
10+
)
11+
;; 0: 55 push rbp
12+
;; 1: 4889e5 mov rbp, rsp
13+
;; 4: 4883ec08 sub rsp, 8
14+
;; 8: 897c2404 mov dword ptr [rsp + 4], edi
15+
;; c: 893424 mov dword ptr [rsp], esi
16+
;; f: 8b0424 mov eax, dword ptr [rsp]
17+
;; 12: 8b4c2404 mov ecx, dword ptr [rsp + 4]
18+
;; 16: 01c1 add ecx, eax
19+
;; 18: 4889c8 mov rax, rcx
20+
;; 1b: 4883c408 add rsp, 8
21+
;; 1f: 5d pop rbp
22+
;; 20: c3 ret

0 commit comments

Comments
 (0)