Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

purego: SyscallN r2 return float #67

Merged
merged 2 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion func_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package purego

import (
"math"
"reflect"
"runtime"
"unsafe"
Expand Down Expand Up @@ -110,7 +111,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
panic("purego: unsupported kind: " + v.Kind().String())
}
}
r1, _, _ := SyscallN(cfn, sysargs...) //TODO: handle float32/64 and struct types
r1, r2, _ := SyscallN(cfn, sysargs...) //TODO: handle float32/64 and struct types
if ty.NumOut() == 0 {
return nil
}
Expand All @@ -134,6 +135,10 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
RegisterFunc(v.Interface(), r1)
case reflect.String:
v.SetString(strings.GoString(r1))
case reflect.Float32, reflect.Float64:
// NOTE: r2 is only the floating return value on 64bit platforms.
// On 32bit platforms r2 is the upper part of a 64bit return.
v.SetFloat(math.Float64frombits(uint64(r2)))
default:
panic("purego: unsupported return kind: " + outType.Kind().String())
}
Expand Down
2 changes: 1 addition & 1 deletion sys_darwin_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ TEXT syscall9X(SB), NOSPLIT, $0

MOVQ 24(BP), DI // get the pointer back
MOVQ AX, syscall9Args_r1(DI) // r1
MOVQ DX, syscall9Args_r2(DI) // r2
MOVQ X0, syscall9Args_r2(DI) // r2

XORL AX, AX // no error (it's ignored anyway)
ADDQ $32, SP
Expand Down
8 changes: 4 additions & 4 deletions sys_darwin_arm64.s
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ TEXT syscall9X(SB), NOSPLIT, $0

BL (R12)

MOVD 8(RSP), R2 // pop structure pointer
ADD $16, RSP
MOVD R0, syscall9Args_r1(R2) // save r1
MOVD R1, syscall9Args_r2(R2) // save r2
MOVD 8(RSP), R2 // pop structure pointer
ADD $16, RSP
MOVD R0, syscall9Args_r1(R2) // save r1
FMOVD F0, syscall9Args_r2(R2) // save r2
RET

// runtime·cgocallback expects a call to the ABIInternal function
Expand Down