winch: Add support for the <i32|i64>.div_* instructions (#5807)

* Refactor the structure and responsibilities of `CodeGenContext`

This commit refactors how the `CodeGenContext` is used throughout the code
generation process, making it easier to pass it around when more flexibility is
desired in the MacroAssembler to perform the lowering of certain instructions.

As of this change, the responsibility of the `CodeGenContext` is to provide an
interface for operations that require an orchestration between the register
allocator, the value stack and function's frame. The MacroAssembler is removed
from the CodeGenContext as is passed as a dependency where needed, effectly
using it as an independent code generation interface only.

By giving more responsibilities to the `CodeGenContext` we can clearly separate
the concerns of the register allocator, which previously did more than it
should (e.g. popping values and spilling).

This change ultimately allows passing in the `CodeGenContext` to the
`MacroAssembler` when a given instruction cannot be generically described
through a common interface. Allowing each implementation to decide the best way
to lower a particular instruction.

* winch: Add support for the WebAssembly `<i32|i64>.div_*` instructions

Given that some architectures have very specific requirements on how to handle
division, this change uses `CodeGenContext` as a dependency to the `div`
MacroAssembler instruction to ensure that each implementation can decide on how to lower the
division. This approach also allows -- in architectures where division can be
expressed as an ordinary binary operation -- to rely on the
`CodeGenContext::i32_binop` or `CodeGenContext::i64_binop` helpers.
This commit is contained in:
Saúl Cabrera
2023-02-17 17:42:03 -05:00
committed by GitHub
parent 853ff787f3
commit 7ec925122d
30 changed files with 851 additions and 220 deletions

View File

@@ -0,0 +1,25 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 20)
(i32.const 10)
(i32.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b90a000000 mov ecx, 0xa
;; 9: b814000000 mov eax, 0x14
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 83f9ff cmp ecx, -1
;; 1c: 0f850e000000 jne 0x30
;; 22: 81f800000080 cmp eax, 0x80000000
;; 28: 0f8502000000 jne 0x30
;; 2e: 0f0b ud2
;; 30: 99 cdq
;; 31: f7f9 idiv ecx
;; 33: 5d pop rbp
;; 34: c3 ret

View File

@@ -0,0 +1,25 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 1)
(i32.const 0)
(i32.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b900000000 mov ecx, 0
;; 9: b801000000 mov eax, 1
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 83f9ff cmp ecx, -1
;; 1c: 0f850e000000 jne 0x30
;; 22: 81f800000080 cmp eax, 0x80000000
;; 28: 0f8502000000 jne 0x30
;; 2e: 0f0b ud2
;; 30: 99 cdq
;; 31: f7f9 idiv ecx
;; 33: 5d pop rbp
;; 34: c3 ret

View File

@@ -0,0 +1,25 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 0x80000000)
(i32.const -1)
(i32.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b9ffffffff mov ecx, 0xffffffff
;; 9: b800000080 mov eax, 0x80000000
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 83f9ff cmp ecx, -1
;; 1c: 0f850e000000 jne 0x30
;; 22: 81f800000080 cmp eax, 0x80000000
;; 28: 0f8502000000 jne 0x30
;; 2e: 0f0b ud2
;; 30: 99 cdq
;; 31: f7f9 idiv ecx
;; 33: 5d pop rbp
;; 34: c3 ret

View File

@@ -0,0 +1,29 @@
;;! target = "x86_64"
(module
(func (param i32) (param i32) (result i32)
(local.get 0)
(local.get 1)
(i32.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 4883ec08 sub rsp, 8
;; 8: 897c2404 mov dword ptr [rsp + 4], edi
;; c: 893424 mov dword ptr [rsp], esi
;; f: 8b0c24 mov ecx, dword ptr [rsp]
;; 12: 8b442404 mov eax, dword ptr [rsp + 4]
;; 16: 83f900 cmp ecx, 0
;; 19: 0f8502000000 jne 0x21
;; 1f: 0f0b ud2
;; 21: 83f9ff cmp ecx, -1
;; 24: 0f850e000000 jne 0x38
;; 2a: 81f800000080 cmp eax, 0x80000000
;; 30: 0f8502000000 jne 0x38
;; 36: 0f0b ud2
;; 38: 99 cdq
;; 39: f7f9 idiv ecx
;; 3b: 4883c408 add rsp, 8
;; 3f: 5d pop rbp
;; 40: c3 ret

View File

@@ -0,0 +1,25 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 0)
(i32.const 0)
(i32.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b900000000 mov ecx, 0
;; 9: b800000000 mov eax, 0
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: 83f9ff cmp ecx, -1
;; 1c: 0f850e000000 jne 0x30
;; 22: 81f800000080 cmp eax, 0x80000000
;; 28: 0f8502000000 jne 0x30
;; 2e: 0f0b ud2
;; 30: 99 cdq
;; 31: f7f9 idiv ecx
;; 33: 5d pop rbp
;; 34: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 20)
(i32.const 10)
(i32.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b90a000000 mov ecx, 0xa
;; 9: b814000000 mov eax, 0x14
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: ba00000000 mov edx, 0
;; 1e: f7f1 div ecx
;; 20: 5d pop rbp
;; 21: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 1)
(i32.const 0)
(i32.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b900000000 mov ecx, 0
;; 9: b801000000 mov eax, 1
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: ba00000000 mov edx, 0
;; 1e: f7f1 div ecx
;; 20: 5d pop rbp
;; 21: c3 ret

View File

@@ -0,0 +1,24 @@
;;! target = "x86_64"
(module
(func (param i32) (param i32) (result i32)
(local.get 0)
(local.get 1)
(i32.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 4883ec08 sub rsp, 8
;; 8: 897c2404 mov dword ptr [rsp + 4], edi
;; c: 893424 mov dword ptr [rsp], esi
;; f: 8b0c24 mov ecx, dword ptr [rsp]
;; 12: 8b442404 mov eax, dword ptr [rsp + 4]
;; 16: 83f900 cmp ecx, 0
;; 19: 0f8502000000 jne 0x21
;; 1f: 0f0b ud2
;; 21: ba00000000 mov edx, 0
;; 26: f7f1 div ecx
;; 28: 4883c408 add rsp, 8
;; 2c: 5d pop rbp
;; 2d: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const -1)
(i32.const -1)
(i32.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b9ffffffff mov ecx, 0xffffffff
;; 9: b8ffffffff mov eax, 0xffffffff
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: ba00000000 mov edx, 0
;; 1e: f7f1 div ecx
;; 20: 5d pop rbp
;; 21: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i32)
(i32.const 0)
(i32.const 0)
(i32.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: b900000000 mov ecx, 0
;; 9: b800000000 mov eax, 0
;; e: 83f900 cmp ecx, 0
;; 11: 0f8502000000 jne 0x19
;; 17: 0f0b ud2
;; 19: ba00000000 mov edx, 0
;; 1e: f7f1 div ecx
;; 20: 5d pop rbp
;; 21: c3 ret

View File

@@ -0,0 +1,27 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 20)
(i64.const 10)
(i64.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c10a000000 mov rcx, 0xa
;; b: 48c7c014000000 mov rax, 0x14
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4883f9ff cmp rcx, -1
;; 22: 0f8515000000 jne 0x3d
;; 28: 49bb0000000000000080
;; movabs r11, 0x8000000000000000
;; 32: 4c39d8 cmp rax, r11
;; 35: 0f8502000000 jne 0x3d
;; 3b: 0f0b ud2
;; 3d: 4899 cqo
;; 3f: 48f7f9 idiv rcx
;; 42: 5d pop rbp
;; 43: c3 ret

View File

@@ -0,0 +1,27 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 1)
(i64.const 0)
(i64.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c001000000 mov rax, 1
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4883f9ff cmp rcx, -1
;; 22: 0f8515000000 jne 0x3d
;; 28: 49bb0000000000000080
;; movabs r11, 0x8000000000000000
;; 32: 4c39d8 cmp rax, r11
;; 35: 0f8502000000 jne 0x3d
;; 3b: 0f0b ud2
;; 3d: 4899 cqo
;; 3f: 48f7f9 idiv rcx
;; 42: 5d pop rbp
;; 43: c3 ret

View File

@@ -0,0 +1,28 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 0x8000000000000000)
(i64.const -1)
(i64.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c1ffffffff mov rcx, 0xffffffffffffffff
;; b: 48b80000000000000080
;; movabs rax, 0x8000000000000000
;; 15: 4883f900 cmp rcx, 0
;; 19: 0f8502000000 jne 0x21
;; 1f: 0f0b ud2
;; 21: 4883f9ff cmp rcx, -1
;; 25: 0f8515000000 jne 0x40
;; 2b: 49bb0000000000000080
;; movabs r11, 0x8000000000000000
;; 35: 4c39d8 cmp rax, r11
;; 38: 0f8502000000 jne 0x40
;; 3e: 0f0b ud2
;; 40: 4899 cqo
;; 42: 48f7f9 idiv rcx
;; 45: 5d pop rbp
;; 46: c3 ret

View File

@@ -0,0 +1,31 @@
;;! target = "x86_64"
(module
(func (param i64) (param i64) (result i64)
(local.get 0)
(local.get 1)
(i64.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 4883ec10 sub rsp, 0x10
;; 8: 48897c2408 mov qword ptr [rsp + 8], rdi
;; d: 48893424 mov qword ptr [rsp], rsi
;; 11: 488b0c24 mov rcx, qword ptr [rsp]
;; 15: 488b442408 mov rax, qword ptr [rsp + 8]
;; 1a: 4883f900 cmp rcx, 0
;; 1e: 0f8502000000 jne 0x26
;; 24: 0f0b ud2
;; 26: 4883f9ff cmp rcx, -1
;; 2a: 0f8515000000 jne 0x45
;; 30: 49bb0000000000000080
;; movabs r11, 0x8000000000000000
;; 3a: 4c39d8 cmp rax, r11
;; 3d: 0f8502000000 jne 0x45
;; 43: 0f0b ud2
;; 45: 4899 cqo
;; 47: 48f7f9 idiv rcx
;; 4a: 4883c410 add rsp, 0x10
;; 4e: 5d pop rbp
;; 4f: c3 ret

View File

@@ -0,0 +1,27 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 0)
(i64.const 0)
(i64.div_s)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c000000000 mov rax, 0
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: 4883f9ff cmp rcx, -1
;; 22: 0f8515000000 jne 0x3d
;; 28: 49bb0000000000000080
;; movabs r11, 0x8000000000000000
;; 32: 4c39d8 cmp rax, r11
;; 35: 0f8502000000 jne 0x3d
;; 3b: 0f0b ud2
;; 3d: 4899 cqo
;; 3f: 48f7f9 idiv rcx
;; 42: 5d pop rbp
;; 43: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 20)
(i64.const 10)
(i64.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c10a000000 mov rcx, 0xa
;; b: 48c7c014000000 mov rax, 0x14
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: ba00000000 mov edx, 0
;; 23: 48f7f1 div rcx
;; 26: 5d pop rbp
;; 27: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 1)
(i64.const 0)
(i64.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c001000000 mov rax, 1
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: ba00000000 mov edx, 0
;; 23: 48f7f1 div rcx
;; 26: 5d pop rbp
;; 27: c3 ret

View File

@@ -0,0 +1,24 @@
;;! target = "x86_64"
(module
(func (param i64) (param i64) (result i64)
(local.get 0)
(local.get 1)
(i64.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 4883ec10 sub rsp, 0x10
;; 8: 48897c2408 mov qword ptr [rsp + 8], rdi
;; d: 48893424 mov qword ptr [rsp], rsi
;; 11: 488b0c24 mov rcx, qword ptr [rsp]
;; 15: 488b442408 mov rax, qword ptr [rsp + 8]
;; 1a: 4883f900 cmp rcx, 0
;; 1e: 0f8502000000 jne 0x26
;; 24: 0f0b ud2
;; 26: ba00000000 mov edx, 0
;; 2b: 48f7f1 div rcx
;; 2e: 4883c410 add rsp, 0x10
;; 32: 5d pop rbp
;; 33: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const -1)
(i64.const -1)
(i64.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c1ffffffff mov rcx, 0xffffffffffffffff
;; b: 48c7c0ffffffff mov rax, 0xffffffffffffffff
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: ba00000000 mov edx, 0
;; 23: 48f7f1 div rcx
;; 26: 5d pop rbp
;; 27: c3 ret

View File

@@ -0,0 +1,20 @@
;;! target = "x86_64"
(module
(func (result i64)
(i64.const 0)
(i64.const 0)
(i64.div_u)
)
)
;; 0: 55 push rbp
;; 1: 4889e5 mov rbp, rsp
;; 4: 48c7c100000000 mov rcx, 0
;; b: 48c7c000000000 mov rax, 0
;; 12: 4883f900 cmp rcx, 0
;; 16: 0f8502000000 jne 0x1e
;; 1c: 0f0b ud2
;; 1e: ba00000000 mov edx, 0
;; 23: 48f7f1 div rcx
;; 26: 5d pop rbp
;; 27: c3 ret