Skip to content

Commit

Permalink
purego: use syscall.Syscall6X for functions with 6 or less arguments (#3
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TotallyGamerJet authored May 25, 2022
1 parent 36e492b commit 0f6873f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
3 changes: 3 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"unsafe"
)

//go:linkname syscall_syscall6X syscall.syscall6X
func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) // from runtime/sys_darwin_64.s

//go:linkname runtime_libcCall runtime.libcCall
//go:linkname runtime_entersyscall runtime.entersyscall
//go:linkname runtime_exitsyscall runtime.exitsyscall
Expand Down
2 changes: 1 addition & 1 deletion sys_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ TEXT ·syscall9X(SB), NOSPLIT, $0
MOVQ R11, X6 // a7
MOVQ R12, X7 // a8

// push the remainding paramters onto the stack
// push the remaining paramters onto the stack
MOVQ R11, 0(SP) // push a7
MOVQ R12, 8(SP) // push a8
MOVQ R13, 16(SP) // push a9
Expand Down
9 changes: 5 additions & 4 deletions sys_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TEXT ·syscall9X(SB), NOSPLIT, $0
MOVD 56(R0), R6 // a7
MOVD 64(R0), R7 // a8
MOVD 72(R0), R8 // a9
MOVD 8(R0), R0 // a1

// these may be float arguments
// so we put them also where C expects floats
Expand All @@ -46,12 +47,12 @@ TEXT ·syscall9X(SB), NOSPLIT, $0
FMOVD R3, F3 // a4
FMOVD R4, F4 // a5
FMOVD R5, F5 // a6
FMOVD R6, F6 // a4
FMOVD R7, F7 // a5
FMOVD R6, F6 // a7
FMOVD R7, F7 // a8

MOVD R8, (RSP) // push a9 onto stack
MOVD 8(R0), R0 // a1
BL (R12)

BL (R12)

MOVD 8(RSP), R2 // pop structure pointer
ADD $16, RSP
Expand Down
18 changes: 12 additions & 6 deletions syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ const maxArgs = 9
// NOTE: SyscallN does not properly call functions that have both integer and float parameters.
// See discussion comment https://github.com/ebiten/purego/pull/1#issuecomment-1128057607
// for an explanation of why that is.
//
// On amd64, if there are more than 8 floats the 9th and so on will be placed incorrectly on the
// stack.
func SyscallN(fn uintptr, args ...uintptr) (r1, r2, err uintptr) {
if len(args) > maxArgs {
panic("too many arguments to SyscallN")
}
if len(args) < maxArgs {
// add padding so there is no out-of-bounds slicing
var tmp = make([]uintptr, maxArgs)
copy(tmp, args)
args = tmp
// add padding so there is no out-of-bounds slicing
var tmp [maxArgs]uintptr
copy(tmp[:], args)
if len(args) <= 6 {
// use the 6 argument version because
// gl.GenFramebuffersEXT would fail with the 9 version
// See https://github.com/hajimehoshi/ebiten/issues/2102#issuecomment-1134679352
return syscall_syscall6X(fn, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5])
}
return syscall_syscall9X(fn, args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8])
return syscall_syscall9X(fn, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4], tmp[5], tmp[6], tmp[7], tmp[8])
}

func callc(fn uintptr, args unsafe.Pointer) {
Expand Down

0 comments on commit 0f6873f

Please sign in to comment.