Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
matrix:
os: [ubuntu-latest, ubuntu-24.04-arm, macos-latest, windows-latest]
go: ['1.18.x', '1.19.x', '1.20.x', '1.21.x', '1.22.x', '1.23.x', '1.24.x', '1.25.x', '1.26.0-rc.2']
fail-fast: false
name: Test with Go ${{ matrix.go }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
defaults:
Expand Down Expand Up @@ -140,6 +141,20 @@ jobs:
go env -u CC
go env -u CXX

- name: go test (Linux ppc64le)
if: ${{ runner.os == 'Linux' && runner.arch != 'ARM64' && !startsWith(matrix.go, '1.18.') && !startsWith(matrix.go, '1.19.') && !startsWith(matrix.go, '1.20.') && !startsWith(matrix.go, '1.21.') }}
run: |
sudo apt-get update
sudo apt-get install -y gcc-powerpc64le-linux-gnu g++-powerpc64le-linux-gnu qemu-user
go env -w CC=powerpc64le-linux-gnu-gcc
go env -w CXX=powerpc64le-linux-gnu-g++
env GOOS=linux GOARCH=ppc64le CGO_ENABLED=0 go test -c -o=purego-test-nocgo .
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-nocgo -test.shuffle=on -test.v -test.count=10
env GOOS=linux GOARCH=ppc64le CGO_ENABLED=1 go test -c -o=purego-test-cgo .
env QEMU_LD_PREFIX=/usr/powerpc64le-linux-gnu qemu-ppc64le ./purego-test-cgo -test.shuffle=on -test.v -test.count=10
go env -u CC
go env -u CXX

- name: go test (Linux riscv64)
if: ${{ runner.os == 'Linux' && runner.arch != 'ARM64' && !startsWith(matrix.go, '1.18.') && !startsWith(matrix.go, '1.19.') && !startsWith(matrix.go, '1.20.') && !startsWith(matrix.go, '1.21.') }}
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Tier 2 platforms are supported by PureGo on a best-effort basis. Critical bugs o

- **Android**: 386<sup>1</sup>, arm<sup>1</sup>
- **FreeBSD**: amd64<sup>2</sup>, arm64<sup>2</sup>
- **Linux**: 386, arm, loong64, riscv64
- **Linux**: 386, arm, loong64, ppc64le, riscv64
- **Windows**: 386<sup>3</sup>, arm<sup>3,4</sup>

#### Support Notes
Expand Down
195 changes: 195 additions & 0 deletions abi_ppc64x.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Macros for transitioning from the host ABI to Go ABI
//
// On PPC64/ELFv2 targets, the following registers are callee
// saved when called from C. They must be preserved before
// calling into Go which does not preserve any of them.
//
// R14-R31
// CR2-4
// VR20-31
// F14-F31
//
// xcoff(aix) and ELFv1 are similar, but may only require a
// subset of these.
//
// These macros assume a 16 byte aligned stack pointer. This
// is required by ELFv1, ELFv2, and AIX PPC64.

#define SAVE_GPR_SIZE (18*8)
#define SAVE_GPR(offset) \
MOVD R14, (offset+8*0)(R1) \
MOVD R15, (offset+8*1)(R1) \
MOVD R16, (offset+8*2)(R1) \
MOVD R17, (offset+8*3)(R1) \
MOVD R18, (offset+8*4)(R1) \
MOVD R19, (offset+8*5)(R1) \
MOVD R20, (offset+8*6)(R1) \
MOVD R21, (offset+8*7)(R1) \
MOVD R22, (offset+8*8)(R1) \
MOVD R23, (offset+8*9)(R1) \
MOVD R24, (offset+8*10)(R1) \
MOVD R25, (offset+8*11)(R1) \
MOVD R26, (offset+8*12)(R1) \
MOVD R27, (offset+8*13)(R1) \
MOVD R28, (offset+8*14)(R1) \
MOVD R29, (offset+8*15)(R1) \
MOVD g, (offset+8*16)(R1) \
MOVD R31, (offset+8*17)(R1)

#define RESTORE_GPR(offset) \
MOVD (offset+8*0)(R1), R14 \
MOVD (offset+8*1)(R1), R15 \
MOVD (offset+8*2)(R1), R16 \
MOVD (offset+8*3)(R1), R17 \
MOVD (offset+8*4)(R1), R18 \
MOVD (offset+8*5)(R1), R19 \
MOVD (offset+8*6)(R1), R20 \
MOVD (offset+8*7)(R1), R21 \
MOVD (offset+8*8)(R1), R22 \
MOVD (offset+8*9)(R1), R23 \
MOVD (offset+8*10)(R1), R24 \
MOVD (offset+8*11)(R1), R25 \
MOVD (offset+8*12)(R1), R26 \
MOVD (offset+8*13)(R1), R27 \
MOVD (offset+8*14)(R1), R28 \
MOVD (offset+8*15)(R1), R29 \
MOVD (offset+8*16)(R1), g \
MOVD (offset+8*17)(R1), R31

#define SAVE_FPR_SIZE (18*8)
#define SAVE_FPR(offset) \
FMOVD F14, (offset+8*0)(R1) \
FMOVD F15, (offset+8*1)(R1) \
FMOVD F16, (offset+8*2)(R1) \
FMOVD F17, (offset+8*3)(R1) \
FMOVD F18, (offset+8*4)(R1) \
FMOVD F19, (offset+8*5)(R1) \
FMOVD F20, (offset+8*6)(R1) \
FMOVD F21, (offset+8*7)(R1) \
FMOVD F22, (offset+8*8)(R1) \
FMOVD F23, (offset+8*9)(R1) \
FMOVD F24, (offset+8*10)(R1) \
FMOVD F25, (offset+8*11)(R1) \
FMOVD F26, (offset+8*12)(R1) \
FMOVD F27, (offset+8*13)(R1) \
FMOVD F28, (offset+8*14)(R1) \
FMOVD F29, (offset+8*15)(R1) \
FMOVD F30, (offset+8*16)(R1) \
FMOVD F31, (offset+8*17)(R1)

#define RESTORE_FPR(offset) \
FMOVD (offset+8*0)(R1), F14 \
FMOVD (offset+8*1)(R1), F15 \
FMOVD (offset+8*2)(R1), F16 \
FMOVD (offset+8*3)(R1), F17 \
FMOVD (offset+8*4)(R1), F18 \
FMOVD (offset+8*5)(R1), F19 \
FMOVD (offset+8*6)(R1), F20 \
FMOVD (offset+8*7)(R1), F21 \
FMOVD (offset+8*8)(R1), F22 \
FMOVD (offset+8*9)(R1), F23 \
FMOVD (offset+8*10)(R1), F24 \
FMOVD (offset+8*11)(R1), F25 \
FMOVD (offset+8*12)(R1), F26 \
FMOVD (offset+8*13)(R1), F27 \
FMOVD (offset+8*14)(R1), F28 \
FMOVD (offset+8*15)(R1), F29 \
FMOVD (offset+8*16)(R1), F30 \
FMOVD (offset+8*17)(R1), F31

// Save and restore VR20-31 (aka VSR56-63). These
// macros must point to a 16B aligned offset.
#define SAVE_VR_SIZE (12*16)
#define SAVE_VR(offset, rtmp) \
MOVD $(offset+16*0), rtmp \
STVX V20, (rtmp)(R1) \
MOVD $(offset+16*1), rtmp \
STVX V21, (rtmp)(R1) \
MOVD $(offset+16*2), rtmp \
STVX V22, (rtmp)(R1) \
MOVD $(offset+16*3), rtmp \
STVX V23, (rtmp)(R1) \
MOVD $(offset+16*4), rtmp \
STVX V24, (rtmp)(R1) \
MOVD $(offset+16*5), rtmp \
STVX V25, (rtmp)(R1) \
MOVD $(offset+16*6), rtmp \
STVX V26, (rtmp)(R1) \
MOVD $(offset+16*7), rtmp \
STVX V27, (rtmp)(R1) \
MOVD $(offset+16*8), rtmp \
STVX V28, (rtmp)(R1) \
MOVD $(offset+16*9), rtmp \
STVX V29, (rtmp)(R1) \
MOVD $(offset+16*10), rtmp \
STVX V30, (rtmp)(R1) \
MOVD $(offset+16*11), rtmp \
STVX V31, (rtmp)(R1)

#define RESTORE_VR(offset, rtmp) \
MOVD $(offset+16*0), rtmp \
LVX (rtmp)(R1), V20 \
MOVD $(offset+16*1), rtmp \
LVX (rtmp)(R1), V21 \
MOVD $(offset+16*2), rtmp \
LVX (rtmp)(R1), V22 \
MOVD $(offset+16*3), rtmp \
LVX (rtmp)(R1), V23 \
MOVD $(offset+16*4), rtmp \
LVX (rtmp)(R1), V24 \
MOVD $(offset+16*5), rtmp \
LVX (rtmp)(R1), V25 \
MOVD $(offset+16*6), rtmp \
LVX (rtmp)(R1), V26 \
MOVD $(offset+16*7), rtmp \
LVX (rtmp)(R1), V27 \
MOVD $(offset+16*8), rtmp \
LVX (rtmp)(R1), V28 \
MOVD $(offset+16*9), rtmp \
LVX (rtmp)(R1), V29 \
MOVD $(offset+16*10), rtmp \
LVX (rtmp)(R1), V30 \
MOVD $(offset+16*11), rtmp \
LVX (rtmp)(R1), V31

// LR and CR are saved in the caller's frame. The callee must
// make space for all other callee-save registers.
#define SAVE_ALL_REG_SIZE (SAVE_GPR_SIZE+SAVE_FPR_SIZE+SAVE_VR_SIZE)

// Stack a frame and save all callee-save registers following the
// host OS's ABI. Fortunately, this is identical for AIX, ELFv1, and
// ELFv2. All host ABIs require the stack pointer to maintain 16 byte
// alignment, and save the callee-save registers in the same places.
//
// To restate, R1 is assumed to be aligned when this macro is used.
// This assumes the caller's frame is compliant with the host ABI.
// CR and LR are saved into the caller's frame per the host ABI.
// R0 is initialized to $0 as expected by Go.
#define STACK_AND_SAVE_HOST_TO_GO_ABI(extra) \
MOVD LR, R0 \
MOVD R0, 16(R1) \
MOVW CR, R0 \
MOVD R0, 8(R1) \
MOVDU R1, -(extra)-FIXED_FRAME-SAVE_ALL_REG_SIZE(R1) \
SAVE_GPR(extra+FIXED_FRAME) \
SAVE_FPR(extra+FIXED_FRAME+SAVE_GPR_SIZE) \
SAVE_VR(extra+FIXED_FRAME+SAVE_GPR_SIZE+SAVE_FPR_SIZE, R0) \
MOVD $0, R0

// This unstacks the frame, restoring all callee-save registers
// as saved by STACK_AND_SAVE_HOST_TO_GO_ABI.
//
// R0 is not guaranteed to contain $0 after this macro.
#define UNSTACK_AND_RESTORE_GO_TO_HOST_ABI(extra) \
RESTORE_GPR(extra+FIXED_FRAME) \
RESTORE_FPR(extra+FIXED_FRAME+SAVE_GPR_SIZE) \
RESTORE_VR(extra+FIXED_FRAME+SAVE_GPR_SIZE+SAVE_FPR_SIZE, R0) \
ADD $(extra+FIXED_FRAME+SAVE_ALL_REG_SIZE), R1 \
MOVD 16(R1), R0 \
MOVD R0, LR \
MOVD 8(R1), R0 \
MOVW R0, CR
2 changes: 1 addition & 1 deletion callback_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors

//go:build darwin || (linux && (386 || amd64 || arm || arm64 || loong64 || riscv64))
//go:build darwin || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64))

package purego_test

Expand Down
18 changes: 11 additions & 7 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
panic("purego: cfn is nil")
}
if ty.NumOut() == 1 && (ty.Out(0).Kind() == reflect.Float32 || ty.Out(0).Kind() == reflect.Float64) &&
runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "riscv64" {
runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "riscv64" {
panic("purego: float returns are not supported")
}
{
Expand Down Expand Up @@ -277,7 +277,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
var arm64_r8 uintptr
if ty.NumOut() == 1 && ty.Out(0).Kind() == reflect.Struct {
outType := ty.Out(0)
if (runtime.GOARCH == "amd64" || runtime.GOARCH == "loong64" || runtime.GOARCH == "riscv64") && outType.Size() > maxRegAllocStructSize {
if (runtime.GOARCH == "amd64" || runtime.GOARCH == "loong64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "riscv64") && outType.Size() > maxRegAllocStructSize {
val := reflect.New(outType)
keepAlive = append(keepAlive, val)
addInt(val.Pointer())
Expand Down Expand Up @@ -317,7 +317,7 @@ func RegisterFunc(fptr any, cfn uintptr) {
syscall := thePool.Get().(*syscall15Args)
defer thePool.Put(syscall)

if runtime.GOARCH == "loong64" || runtime.GOARCH == "riscv64" {
if runtime.GOARCH == "loong64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "riscv64" {
syscall.Set(cfn, sysargs[:], floats[:], 0)
runtime_cgocall(syscall15XABI0, unsafe.Pointer(syscall))
} else if runtime.GOARCH == "arm64" || runtime.GOOS != "windows" {
Expand Down Expand Up @@ -359,9 +359,13 @@ func RegisterFunc(fptr any, cfn uintptr) {
// NOTE: syscall.r2 is only the floating return value on 64bit platforms.
// On 32bit platforms syscall.r2 is the upper part of a 64bit return.
// On 386, x87 FPU returns floats as float64 in ST(0), so we read as float64 and convert.
if runtime.GOARCH == "386" {
// On PPC64LE, C ABI converts float32 to double in FPR, so we read as float64.
switch runtime.GOARCH {
case "386":
v.SetFloat(math.Float64frombits(uint64(syscall.f1) | (uint64(syscall.f2) << 32)))
} else {
case "ppc64le":
v.SetFloat(math.Float64frombits(uint64(syscall.f1)))
default:
v.SetFloat(float64(math.Float32frombits(uint32(syscall.f1))))
}
case reflect.Float64:
Expand Down Expand Up @@ -486,7 +490,7 @@ func roundUpTo8(val uintptr) uintptr {

func numOfFloatRegisters() int {
switch runtime.GOARCH {
case "amd64", "arm64", "loong64", "riscv64":
case "amd64", "arm64", "loong64", "ppc64le", "riscv64":
return 8
case "arm":
return 16
Expand All @@ -502,7 +506,7 @@ func numOfFloatRegisters() int {

func numOfIntegerRegisters() int {
switch runtime.GOARCH {
case "arm64", "loong64", "riscv64":
case "arm64", "loong64", "ppc64le", "riscv64":
return 8
case "amd64":
return 6
Expand Down
8 changes: 4 additions & 4 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestRegisterFunc(t *testing.T) {
}

func Test_qsort(t *testing.T) {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "riscv64" {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "riscv64" {
t.Skip("Platform doesn't support Floats")
return
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func Test_qsort(t *testing.T) {
}

func TestRegisterFunc_Floats(t *testing.T) {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "riscv64" {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "riscv64" {
t.Skip("Platform doesn't support Floats")
return
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestRegisterFunc_Floats(t *testing.T) {
}

func TestRegisterLibFunc_Bool(t *testing.T) {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "riscv64" {
if runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "riscv64" {
t.Skip("Platform doesn't support callbacks")
return
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestABI_ArgumentPassing(t *testing.T) {
if tt.name == "20_int32" && (runtime.GOOS != "darwin" || runtime.GOARCH != "arm64") {
t.Skip("20 int32 arguments only supported on Darwin ARM64 with smart stack checking")
}
if tt.name == "10_float32" && (runtime.GOARCH == "loong64" || runtime.GOARCH == "riscv64") {
if tt.name == "10_float32" && (runtime.GOARCH == "loong64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "riscv64") {
t.Skip("float32 stack arguments not yet supported on this platform")
}
// Struct tests require Darwin ARM64 or AMD64
Expand Down
2 changes: 1 addition & 1 deletion internal/cgo/syscall_cgo_unix.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors

//go:build freebsd || (linux && !(386 || amd64 || arm || arm64 || loong64 || riscv64)) || netbsd
//go:build freebsd || (linux && !(386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64)) || netbsd

package cgo

Expand Down
Loading