-
Notifications
You must be signed in to change notification settings - Fork 103
purego: add support for s390x #413
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
Merged
+4,561
−20
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2016 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. | ||
|
|
||
| #include "textflag.h" | ||
|
|
||
| // Called by C code generated by cmd/cgo. | ||
| // func crosscall2(fn, a unsafe.Pointer, n int32, ctxt uintptr) | ||
| // Saves C callee-saved registers and calls cgocallback with three arguments. | ||
| // fn is the PC of a func(a unsafe.Pointer) function. | ||
| TEXT crosscall2(SB), NOSPLIT|NOFRAME, $0 | ||
| // Start with standard C stack frame layout and linkage. | ||
|
|
||
| // Save R6-R15 in the register save area of the calling function. | ||
| STMG R6, R15, 48(R15) | ||
|
|
||
| // Allocate 96 bytes on the stack. | ||
| MOVD $-96(R15), R15 | ||
|
|
||
| // Save F8-F15 in our stack frame. | ||
| FMOVD F8, 32(R15) | ||
| FMOVD F9, 40(R15) | ||
| FMOVD F10, 48(R15) | ||
| FMOVD F11, 56(R15) | ||
| FMOVD F12, 64(R15) | ||
| FMOVD F13, 72(R15) | ||
| FMOVD F14, 80(R15) | ||
| FMOVD F15, 88(R15) | ||
|
|
||
| // Initialize Go ABI environment. | ||
| BL runtime·load_g(SB) | ||
|
|
||
| MOVD R2, 8(R15) // fn unsafe.Pointer | ||
| MOVD R3, 16(R15) // a unsafe.Pointer | ||
|
|
||
| // Skip R4 = n uint32 | ||
| MOVD R5, 24(R15) // ctxt uintptr | ||
| BL runtime·cgocallback(SB) | ||
|
|
||
| FMOVD 32(R15), F8 | ||
| FMOVD 40(R15), F9 | ||
| FMOVD 48(R15), F10 | ||
| FMOVD 56(R15), F11 | ||
| FMOVD 64(R15), F12 | ||
| FMOVD 72(R15), F13 | ||
| FMOVD 80(R15), F14 | ||
| FMOVD 88(R15), F15 | ||
|
|
||
| // De-allocate stack frame. | ||
| MOVD $96(R15), R15 | ||
|
|
||
| // Restore R6-R15. | ||
| LMG 48(R15), R6, R15 | ||
|
|
||
| RET |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | ||
|
|
||
| package purego | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| func getStruct(outType reflect.Type, syscall syscall15Args) reflect.Value { | ||
| outSize := outType.Size() | ||
|
|
||
| switch { | ||
| case outSize == 0: | ||
| return reflect.New(outType).Elem() | ||
|
|
||
| case outSize <= 16: | ||
| // Reconstruct from registers by copying raw bytes | ||
| var buf [16]byte | ||
|
|
||
| // Integer registers | ||
| *(*uintptr)(unsafe.Pointer(&buf[0])) = syscall.a1 | ||
| if outSize > 8 { | ||
| *(*uintptr)(unsafe.Pointer(&buf[8])) = syscall.a2 | ||
| } | ||
|
|
||
| // Homogeneous float aggregates override integer regs | ||
| if isAllFloats, numFields := isAllSameFloat(outType); isAllFloats { | ||
| if outType.Field(0).Type.Kind() == reflect.Float32 { | ||
| // float32 values in FP regs | ||
| f := []uintptr{syscall.f1, syscall.f2, syscall.f3, syscall.f4} | ||
| for i := 0; i < numFields; i++ { | ||
| *(*uint32)(unsafe.Pointer(&buf[i*4])) = uint32(f[i]) | ||
| } | ||
| } else { | ||
| // float64: whole register value is valid | ||
| *(*uintptr)(unsafe.Pointer(&buf[0])) = syscall.f1 | ||
| if outSize > 8 { | ||
| *(*uintptr)(unsafe.Pointer(&buf[8])) = syscall.f2 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return reflect.NewAt(outType, unsafe.Pointer(&buf[0])).Elem() | ||
|
|
||
| default: | ||
| // Returned indirectly via pointer in a1 | ||
| ptr := *(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1)) | ||
| return reflect.NewAt(outType, ptr).Elem() | ||
| } | ||
| } | ||
|
|
||
| func addStruct( | ||
| v reflect.Value, | ||
| numInts, numFloats, numStack *int, | ||
| addInt, addFloat, addStack func(uintptr), | ||
| keepAlive []any, | ||
| ) []any { | ||
| size := v.Type().Size() | ||
| if size == 0 { | ||
| return keepAlive | ||
| } | ||
|
|
||
| if size <= 16 { | ||
| return placeSmallAggregateS390X(v, addFloat, addInt, keepAlive) | ||
| } | ||
|
|
||
| return placeStack(v, keepAlive, addInt) | ||
| } | ||
|
|
||
| func placeSmallAggregateS390X( | ||
| v reflect.Value, | ||
| addFloat, addInt func(uintptr), | ||
| keepAlive []any, | ||
| ) []any { | ||
| size := v.Type().Size() | ||
|
|
||
| var ptr unsafe.Pointer | ||
| if v.CanAddr() { | ||
| ptr = v.Addr().UnsafePointer() | ||
| } else { | ||
| tmp := reflect.New(v.Type()) | ||
| tmp.Elem().Set(v) | ||
| ptr = tmp.UnsafePointer() | ||
| keepAlive = append(keepAlive, tmp.Interface()) | ||
| } | ||
|
|
||
| var buf [16]byte | ||
| src := unsafe.Slice((*byte)(ptr), size) | ||
| copy(buf[:], src) | ||
|
|
||
| w0 := *(*uintptr)(unsafe.Pointer(&buf[0])) | ||
| w1 := uintptr(0) | ||
| if size > 8 { | ||
| w1 = *(*uintptr)(unsafe.Pointer(&buf[8])) | ||
| } | ||
|
|
||
| if isFloats, _ := isAllSameFloat(v.Type()); isFloats { | ||
| addFloat(w0) | ||
| if size > 8 { | ||
| addFloat(w1) | ||
| } | ||
| } else { | ||
| addInt(w0) | ||
| if size > 8 { | ||
| addInt(w1) | ||
| } | ||
| } | ||
|
|
||
| return keepAlive | ||
| } | ||
|
|
||
| // placeStack is a fallback for structs that are too large to fit in registers | ||
| func placeStack(v reflect.Value, keepAlive []any, addInt func(uintptr)) []any { | ||
| if v.CanAddr() { | ||
| addInt(v.Addr().Pointer()) | ||
| return keepAlive | ||
| } | ||
| ptr := reflect.New(v.Type()) | ||
| ptr.Elem().Set(v) | ||
| addInt(ptr.Pointer()) | ||
| return append(keepAlive, ptr.Interface()) | ||
| } | ||
|
|
||
| func shouldBundleStackArgs(v reflect.Value, numInts, numFloats int) bool { | ||
| // S390X does not bundle stack args | ||
| return false | ||
| } | ||
|
|
||
| func collectStackArgs( | ||
| args []reflect.Value, | ||
| i, numInts, numFloats int, | ||
| keepAlive []any, | ||
| addInt, addFloat, addStack func(uintptr), | ||
| numIntsPtr, numFloatsPtr, numStackPtr *int, | ||
| ) ([]reflect.Value, []any) { | ||
| return nil, keepAlive | ||
| } | ||
|
|
||
| func bundleStackArgs(stackArgs []reflect.Value, addStack func(uintptr)) { | ||
| panic("bundleStackArgs not supported on S390X") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.