use crosscall2 from runtime/cgo (#80)
- commit
- 2a13cf524ca4c18c2cc55ecaffc89f1edeb6f75f
- author
- TotallyGamerJet · · 2022-12-14 15:09 UTC
- committer
- GitHub · · 2022-12-14 15:09 UTC
- parent
- 2c9e36d
- signature
- GPG Unverified
4AEE18F83AFDEB23The key is unknown
5 files · +120 -29
M README.md +1 -0
| @@ -11,6 +11,7 @@ This is a list of the copied files: | |||
| 11 | 11 | ||
| 12 | 12 | * `zcallback_darwin_*.s` from package `runtime` | |
| 13 | 13 | * `internal/abi/abi_*.h` from package `runtime/cgo` | |
| 14 | + | * `internal/fakecgo/asm_GOARCH.s` from package `runtime/cgo` | |
| 14 | 15 | * `internal/fakecgo/callbacks.go` from package `runtime/cgo` | |
| 15 | 16 | * `internal/fakecgo/go_GOOS_GOARCH.go` from package `runtime/cgo` | |
| 16 | 17 | * `internal/fakecgo/iscgo.go` from package `runtime/cgo` | |
| @@ -11,6 +11,7 @@ This is a list of the copied files: | |||||
| 11 | 11 | ||||
| 12 | * `zcallback_darwin_*.s` from package `runtime` | 12 | * `zcallback_darwin_*.s` from package `runtime` | ||
| 13 | * `internal/abi/abi_*.h` from package `runtime/cgo` | 13 | * `internal/abi/abi_*.h` from package `runtime/cgo` | ||
| 14 | + | * `internal/fakecgo/asm_GOARCH.s` from package `runtime/cgo` | |||
| 14 | * `internal/fakecgo/callbacks.go` from package `runtime/cgo` | 15 | * `internal/fakecgo/callbacks.go` from package `runtime/cgo` | ||
| 15 | * `internal/fakecgo/go_GOOS_GOARCH.go` from package `runtime/cgo` | 16 | * `internal/fakecgo/go_GOOS_GOARCH.go` from package `runtime/cgo` | ||
| 16 | * `internal/fakecgo/iscgo.go` from package `runtime/cgo` | 17 | * `internal/fakecgo/iscgo.go` from package `runtime/cgo` | ||
A internal/fakecgo/asm_amd64.s +76 -0
| @@ -0,0 +1,76 @@ | |||
| 1 | + | // Copyright 2009 The Go Authors. All rights reserved. | |
| 2 | + | // Use of this source code is governed by a BSD-style | |
| 3 | + | // license that can be found in the LICENSE file. | |
| 4 | + | ||
| 5 | + | #include "textflag.h" | |
| 6 | + | ||
| 7 | + | // Called by C code generated by cmd/cgo. | |
| 8 | + | // func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr) | |
| 9 | + | // Saves C callee-saved registers and calls cgocallback with three arguments. | |
| 10 | + | // fn is the PC of a func(a unsafe.Pointer) function. | |
| 11 | + | // This signature is known to SWIG, so we can't change it. | |
| 12 | + | #ifndef GOOS_windows | |
| 13 | + | TEXT crosscall2(SB), NOSPLIT, $0x50-0 // keeps stack pointer 32-byte aligned | |
| 14 | + | #else | |
| 15 | + | TEXT crosscall2(SB), NOSPLIT, $0x110-0 // also need to save xmm6 - xmm15 | |
| 16 | + | #endif | |
| 17 | + | MOVQ BX, 0x18(SP) | |
| 18 | + | MOVQ R12, 0x28(SP) | |
| 19 | + | MOVQ R13, 0x30(SP) | |
| 20 | + | MOVQ R14, 0x38(SP) | |
| 21 | + | MOVQ R15, 0x40(SP) | |
| 22 | + | ||
| 23 | + | #ifdef GOOS_windows | |
| 24 | + | // Win64 save RBX, RBP, RDI, RSI, RSP, R12, R13, R14, R15 and XMM6 -- XMM15. | |
| 25 | + | MOVQ DI, 0x48(SP) | |
| 26 | + | MOVQ SI, 0x50(SP) | |
| 27 | + | MOVUPS X6, 0x60(SP) | |
| 28 | + | MOVUPS X7, 0x70(SP) | |
| 29 | + | MOVUPS X8, 0x80(SP) | |
| 30 | + | MOVUPS X9, 0x90(SP) | |
| 31 | + | MOVUPS X10, 0xa0(SP) | |
| 32 | + | MOVUPS X11, 0xb0(SP) | |
| 33 | + | MOVUPS X12, 0xc0(SP) | |
| 34 | + | MOVUPS X13, 0xd0(SP) | |
| 35 | + | MOVUPS X14, 0xe0(SP) | |
| 36 | + | MOVUPS X15, 0xf0(SP) | |
| 37 | + | ||
| 38 | + | MOVQ CX, 0x0(SP) // fn | |
| 39 | + | MOVQ DX, 0x8(SP) // arg | |
| 40 | + | ||
| 41 | + | // Skip n in R8. | |
| 42 | + | MOVQ R9, 0x10(SP) // ctxt | |
| 43 | + | ||
| 44 | + | CALL runtime·cgocallback(SB) | |
| 45 | + | ||
| 46 | + | MOVQ 0x48(SP), DI | |
| 47 | + | MOVQ 0x50(SP), SI | |
| 48 | + | MOVUPS 0x60(SP), X6 | |
| 49 | + | MOVUPS 0x70(SP), X7 | |
| 50 | + | MOVUPS 0x80(SP), X8 | |
| 51 | + | MOVUPS 0x90(SP), X9 | |
| 52 | + | MOVUPS 0xa0(SP), X10 | |
| 53 | + | MOVUPS 0xb0(SP), X11 | |
| 54 | + | MOVUPS 0xc0(SP), X12 | |
| 55 | + | MOVUPS 0xd0(SP), X13 | |
| 56 | + | MOVUPS 0xe0(SP), X14 | |
| 57 | + | MOVUPS 0xf0(SP), X15 | |
| 58 | + | ||
| 59 | + | #else | |
| 60 | + | MOVQ DI, 0x0(SP) // fn | |
| 61 | + | MOVQ SI, 0x8(SP) // arg | |
| 62 | + | ||
| 63 | + | // Skip n in DX. | |
| 64 | + | MOVQ CX, 0x10(SP) // ctxt | |
| 65 | + | ||
| 66 | + | CALL runtime·cgocallback(SB) | |
| 67 | + | ||
| 68 | + | #endif | |
| 69 | + | ||
| 70 | + | MOVQ 0x18(SP), BX | |
| 71 | + | MOVQ 0x28(SP), R12 | |
| 72 | + | MOVQ 0x30(SP), R13 | |
| 73 | + | MOVQ 0x38(SP), R14 | |
| 74 | + | MOVQ 0x40(SP), R15 | |
| 75 | + | ||
| 76 | + | RET | |
| @@ -0,0 +1,76 @@ | |||||
| 1 | + | // Copyright 2009 The Go Authors. All rights reserved. | |||
| 2 | + | // Use of this source code is governed by a BSD-style | |||
| 3 | + | // license that can be found in the LICENSE file. | |||
| 4 | + | ||||
| 5 | + | #include "textflag.h" | |||
| 6 | + | ||||
| 7 | + | // Called by C code generated by cmd/cgo. | |||
| 8 | + | // func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr) | |||
| 9 | + | // Saves C callee-saved registers and calls cgocallback with three arguments. | |||
| 10 | + | // fn is the PC of a func(a unsafe.Pointer) function. | |||
| 11 | + | // This signature is known to SWIG, so we can't change it. | |||
| 12 | + | #ifndef GOOS_windows | |||
| 13 | + | TEXT crosscall2(SB), NOSPLIT, $0x50-0 // keeps stack pointer 32-byte aligned | |||
| 14 | + | #else | |||
| 15 | + | TEXT crosscall2(SB), NOSPLIT, $0x110-0 // also need to save xmm6 - xmm15 | |||
| 16 | + | #endif | |||
| 17 | + | MOVQ BX, 0x18(SP) | |||
| 18 | + | MOVQ R12, 0x28(SP) | |||
| 19 | + | MOVQ R13, 0x30(SP) | |||
| 20 | + | MOVQ R14, 0x38(SP) | |||
| 21 | + | MOVQ R15, 0x40(SP) | |||
| 22 | + | ||||
| 23 | + | #ifdef GOOS_windows | |||
| 24 | + | // Win64 save RBX, RBP, RDI, RSI, RSP, R12, R13, R14, R15 and XMM6 -- XMM15. | |||
| 25 | + | MOVQ DI, 0x48(SP) | |||
| 26 | + | MOVQ SI, 0x50(SP) | |||
| 27 | + | MOVUPS X6, 0x60(SP) | |||
| 28 | + | MOVUPS X7, 0x70(SP) | |||
| 29 | + | MOVUPS X8, 0x80(SP) | |||
| 30 | + | MOVUPS X9, 0x90(SP) | |||
| 31 | + | MOVUPS X10, 0xa0(SP) | |||
| 32 | + | MOVUPS X11, 0xb0(SP) | |||
| 33 | + | MOVUPS X12, 0xc0(SP) | |||
| 34 | + | MOVUPS X13, 0xd0(SP) | |||
| 35 | + | MOVUPS X14, 0xe0(SP) | |||
| 36 | + | MOVUPS X15, 0xf0(SP) | |||
| 37 | + | ||||
| 38 | + | MOVQ CX, 0x0(SP) // fn | |||
| 39 | + | MOVQ DX, 0x8(SP) // arg | |||
| 40 | + | ||||
| 41 | + | // Skip n in R8. | |||
| 42 | + | MOVQ R9, 0x10(SP) // ctxt | |||
| 43 | + | ||||
| 44 | + | CALL runtime·cgocallback(SB) | |||
| 45 | + | ||||
| 46 | + | MOVQ 0x48(SP), DI | |||
| 47 | + | MOVQ 0x50(SP), SI | |||
| 48 | + | MOVUPS 0x60(SP), X6 | |||
| 49 | + | MOVUPS 0x70(SP), X7 | |||
| 50 | + | MOVUPS 0x80(SP), X8 | |||
| 51 | + | MOVUPS 0x90(SP), X9 | |||
| 52 | + | MOVUPS 0xa0(SP), X10 | |||
| 53 | + | MOVUPS 0xb0(SP), X11 | |||
| 54 | + | MOVUPS 0xc0(SP), X12 | |||
| 55 | + | MOVUPS 0xd0(SP), X13 | |||
| 56 | + | MOVUPS 0xe0(SP), X14 | |||
| 57 | + | MOVUPS 0xf0(SP), X15 | |||
| 58 | + | ||||
| 59 | + | #else | |||
| 60 | + | MOVQ DI, 0x0(SP) // fn | |||
| 61 | + | MOVQ SI, 0x8(SP) // arg | |||
| 62 | + | ||||
| 63 | + | // Skip n in DX. | |||
| 64 | + | MOVQ CX, 0x10(SP) // ctxt | |||
| 65 | + | ||||
| 66 | + | CALL runtime·cgocallback(SB) | |||
| 67 | + | ||||
| 68 | + | #endif | |||
| 69 | + | ||||
| 70 | + | MOVQ 0x18(SP), BX | |||
| 71 | + | MOVQ 0x28(SP), R12 | |||
| 72 | + | MOVQ 0x30(SP), R13 | |||
| 73 | + | MOVQ 0x38(SP), R14 | |||
| 74 | + | MOVQ 0x40(SP), R15 | |||
| 75 | + | ||||
| 76 | + | RET | |||
A internal/fakecgo/asm_arm64.s +36 -0
| @@ -0,0 +1,36 @@ | |||
| 1 | + | // Copyright 2015 The Go Authors. All rights reserved. | |
| 2 | + | // Use of this source code is governed by a BSD-style | |
| 3 | + | // license that can be found in the LICENSE file. | |
| 4 | + | ||
| 5 | + | #include "textflag.h" | |
| 6 | + | #include "../abi/abi_arm64.h" | |
| 7 | + | ||
| 8 | + | // Called by C code generated by cmd/cgo. | |
| 9 | + | // func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr) | |
| 10 | + | // Saves C callee-saved registers and calls cgocallback with three arguments. | |
| 11 | + | // fn is the PC of a func(a unsafe.Pointer) function. | |
| 12 | + | TEXT crosscall2(SB), NOSPLIT|NOFRAME, $0 | |
| 13 | + | /* | |
| 14 | + | * We still need to save all callee save register as before, and then | |
| 15 | + | * push 3 args for fn (R0, R1, R3), skipping R2. | |
| 16 | + | * Also note that at procedure entry in gc world, 8(RSP) will be the | |
| 17 | + | * first arg. | |
| 18 | + | */ | |
| 19 | + | SUB $(8*24), RSP | |
| 20 | + | STP (R0, R1), (8*1)(RSP) | |
| 21 | + | MOVD R3, (8*3)(RSP) | |
| 22 | + | ||
| 23 | + | SAVE_R19_TO_R28(8*4) | |
| 24 | + | SAVE_F8_TO_F15(8*14) | |
| 25 | + | STP (R29, R30), (8*22)(RSP) | |
| 26 | + | ||
| 27 | + | // Initialize Go ABI environment | |
| 28 | + | BL runtime·load_g(SB) | |
| 29 | + | BL runtime·cgocallback(SB) | |
| 30 | + | ||
| 31 | + | RESTORE_R19_TO_R28(8*4) | |
| 32 | + | RESTORE_F8_TO_F15(8*14) | |
| 33 | + | LDP (8*22)(RSP), (R29, R30) | |
| 34 | + | ||
| 35 | + | ADD $(8*24), RSP | |
| 36 | + | RET | |
| @@ -0,0 +1,36 @@ | |||||
| 1 | + | // Copyright 2015 The Go Authors. All rights reserved. | |||
| 2 | + | // Use of this source code is governed by a BSD-style | |||
| 3 | + | // license that can be found in the LICENSE file. | |||
| 4 | + | ||||
| 5 | + | #include "textflag.h" | |||
| 6 | + | #include "../abi/abi_arm64.h" | |||
| 7 | + | ||||
| 8 | + | // Called by C code generated by cmd/cgo. | |||
| 9 | + | // func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr) | |||
| 10 | + | // Saves C callee-saved registers and calls cgocallback with three arguments. | |||
| 11 | + | // fn is the PC of a func(a unsafe.Pointer) function. | |||
| 12 | + | TEXT crosscall2(SB), NOSPLIT|NOFRAME, $0 | |||
| 13 | + | /* | |||
| 14 | + | * We still need to save all callee save register as before, and then | |||
| 15 | + | * push 3 args for fn (R0, R1, R3), skipping R2. | |||
| 16 | + | * Also note that at procedure entry in gc world, 8(RSP) will be the | |||
| 17 | + | * first arg. | |||
| 18 | + | */ | |||
| 19 | + | SUB $(8*24), RSP | |||
| 20 | + | STP (R0, R1), (8*1)(RSP) | |||
| 21 | + | MOVD R3, (8*3)(RSP) | |||
| 22 | + | ||||
| 23 | + | SAVE_R19_TO_R28(8*4) | |||
| 24 | + | SAVE_F8_TO_F15(8*14) | |||
| 25 | + | STP (R29, R30), (8*22)(RSP) | |||
| 26 | + | ||||
| 27 | + | // Initialize Go ABI environment | |||
| 28 | + | BL runtime·load_g(SB) | |||
| 29 | + | BL runtime·cgocallback(SB) | |||
| 30 | + | ||||
| 31 | + | RESTORE_R19_TO_R28(8*4) | |||
| 32 | + | RESTORE_F8_TO_F15(8*14) | |||
| 33 | + | LDP (8*22)(RSP), (R29, R30) | |||
| 34 | + | ||||
| 35 | + | ADD $(8*24), RSP | |||
| 36 | + | RET | |||
M sys_darwin_amd64.s +6 -6
| @@ -111,12 +111,12 @@ TEXT callbackasm1(SB), NOSPLIT, $0 | |||
| 111 | 111 | LEAQ 24(SP), AX // take the address of callbackArgs | |
| 112 | 112 | ||
| 113 | 113 | // Call cgocallback, which will call callbackWrap(frame). | |
| 114 | - | MOVQ $0, 16(SP) // context | |
| 115 | - | MOVQ AX, 8(SP) // frame (address of callbackArgs) | |
| 116 | - | MOVQ ·callbackWrap_call(SB), DX // Get the ABIInternal function pointer | |
| 117 | - | MOVQ (DX), CX // without <ABIInternal> by using a closure. | |
| 118 | - | MOVQ CX, 0(SP) // PC of function value to call (callbackWrap) | |
| 119 | - | CALL runtime·cgocallback(SB) // runtime.cgocallback(fn, frame, ctxt uintptr) | |
| 114 | + | MOVQ ·callbackWrap_call(SB), DI // Get the ABIInternal function pointer | |
| 115 | + | MOVQ (DI), DI // without <ABIInternal> by using a closure. | |
| 116 | + | MOVQ AX, SI // frame (address of callbackArgs) | |
| 117 | + | MOVQ $0, CX // context | |
| 118 | + | ||
| 119 | + | CALL crosscall2(SB) // runtime.cgocallback(fn, frame, ctxt uintptr) | |
| 120 | 120 | ||
| 121 | 121 | // Get callback result. | |
| 122 | 122 | MOVQ (24+callbackArgs_result)(SP), AX | |
| @@ -111,12 +111,12 @@ TEXT callbackasm1(SB), NOSPLIT, $0 | |||||
| 111 | LEAQ 24(SP), AX // take the address of callbackArgs | 111 | LEAQ 24(SP), AX // take the address of callbackArgs | ||
| 112 | 112 | ||||
| 113 | // Call cgocallback, which will call callbackWrap(frame). | 113 | // Call cgocallback, which will call callbackWrap(frame). | ||
| 114 | - | MOVQ $0, 16(SP) // context | 114 | + | MOVQ ·callbackWrap_call(SB), DI // Get the ABIInternal function pointer |
| 115 | - | MOVQ AX, 8(SP) // frame (address of callbackArgs) | 115 | + | MOVQ (DI), DI // without <ABIInternal> by using a closure. |
| 116 | - | MOVQ ·callbackWrap_call(SB), DX // Get the ABIInternal function pointer | 116 | + | MOVQ AX, SI // frame (address of callbackArgs) |
| 117 | - | MOVQ (DX), CX // without <ABIInternal> by using a closure. | 117 | + | MOVQ $0, CX // context |
| 118 | - | MOVQ CX, 0(SP) // PC of function value to call (callbackWrap) | 118 | + | |
| 119 | - | CALL runtime·cgocallback(SB) // runtime.cgocallback(fn, frame, ctxt uintptr) | 119 | + | CALL crosscall2(SB) // runtime.cgocallback(fn, frame, ctxt uintptr) |
| 120 | 120 | ||||
| 121 | // Get callback result. | 121 | // Get callback result. | ||
| 122 | MOVQ (24+callbackArgs_result)(SP), AX | 122 | MOVQ (24+callbackArgs_result)(SP), AX | ||
M sys_darwin_arm64.s +1 -23
| @@ -95,29 +95,7 @@ TEXT callbackasm1(SB), NOSPLIT, $208-0 | |||
| 95 | 95 | MOVD R13, R1 // frame (&callbackArgs{...}) | |
| 96 | 96 | MOVD $0, R3 // ctxt uintptr | |
| 97 | 97 | ||
| 98 | - | // We still need to save all callee save register as before, and then | |
| 99 | - | // push 3 args for fn (R0, R1, R3), skipping R2. | |
| 100 | - | // Also note that at procedure entry in gc world, 8(RSP) will be the | |
| 101 | - | // first arg. | |
| 102 | - | SUB $(8*24), RSP | |
| 103 | - | STP (R0, R1), (8*1)(RSP) | |
| 104 | - | MOVD R3, (8*3)(RSP) | |
| 105 | - | ||
| 106 | - | // Push C callee-save registers R19-R28. | |
| 107 | - | // LR, FP already saved. | |
| 108 | - | SAVE_R19_TO_R28(8*4) | |
| 109 | - | SAVE_F8_TO_F15(8*14) | |
| 110 | - | STP (R29, R30), (8*22)(RSP) | |
| 111 | - | ||
| 112 | - | // Initialize Go ABI environment | |
| 113 | - | BL runtime·load_g(SB) | |
| 114 | - | BL runtime·cgocallback(SB) | |
| 115 | - | ||
| 116 | - | RESTORE_R19_TO_R28(8*4) | |
| 117 | - | RESTORE_F8_TO_F15(8*14) | |
| 118 | - | LDP (8*22)(RSP), (R29, R30) | |
| 119 | - | ||
| 120 | - | ADD $(8*24), RSP | |
| 98 | + | BL crosscall2(SB) | |
| 121 | 99 | ||
| 122 | 100 | // Get callback result. | |
| 123 | 101 | MOVD $cbargs-(18*8+callbackArgs__size)(SP), R13 | |
| @@ -95,29 +95,7 @@ TEXT callbackasm1(SB), NOSPLIT, $208-0 | |||||
| 95 | MOVD R13, R1 // frame (&callbackArgs{...}) | 95 | MOVD R13, R1 // frame (&callbackArgs{...}) | ||
| 96 | MOVD $0, R3 // ctxt uintptr | 96 | MOVD $0, R3 // ctxt uintptr | ||
| 97 | 97 | ||||
| 98 | - | // We still need to save all callee save register as before, and then | 98 | + | BL crosscall2(SB) |
| 99 | - | // push 3 args for fn (R0, R1, R3), skipping R2. | |||
| 100 | - | // Also note that at procedure entry in gc world, 8(RSP) will be the | |||
| 101 | - | // first arg. | |||
| 102 | - | SUB $(8*24), RSP | |||
| 103 | - | STP (R0, R1), (8*1)(RSP) | |||
| 104 | - | MOVD R3, (8*3)(RSP) | |||
| 105 | - | ||||
| 106 | - | // Push C callee-save registers R19-R28. | |||
| 107 | - | // LR, FP already saved. | |||
| 108 | - | SAVE_R19_TO_R28(8*4) | |||
| 109 | - | SAVE_F8_TO_F15(8*14) | |||
| 110 | - | STP (R29, R30), (8*22)(RSP) | |||
| 111 | - | ||||
| 112 | - | // Initialize Go ABI environment | |||
| 113 | - | BL runtime·load_g(SB) | |||
| 114 | - | BL runtime·cgocallback(SB) | |||
| 115 | - | ||||
| 116 | - | RESTORE_R19_TO_R28(8*4) | |||
| 117 | - | RESTORE_F8_TO_F15(8*14) | |||
| 118 | - | LDP (8*22)(RSP), (R29, R30) | |||
| 119 | - | ||||
| 120 | - | ADD $(8*24), RSP | |||
| 121 | 99 | ||||
| 122 | // Get callback result. | 100 | // Get callback result. | ||
| 123 | MOVD $cbargs-(18*8+callbackArgs__size)(SP), R13 | 101 | MOVD $cbargs-(18*8+callbackArgs__size)(SP), R13 | ||