1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2025 The Ebitengine Authors
3
4package purego
5
6import (
7 "reflect"
8 "unsafe"
9)
10
11func addStruct(v reflect.Value, numInts, numFloats, numStack *int, addInt, addFloat, addStack func(uintptr), keepAlive []any) []any {
12 size := v.Type().Size()
13 if size == 0 {
14 return keepAlive
15 }
16
17 // TODO: ARM EABI: small structs are passed in registers or on stack
18 // For simplicity, pass by pointer for now
19 ptr := v.Addr().UnsafePointer()
20 keepAlive = append(keepAlive, ptr)
21 if *numInts < 4 {
22 addInt(uintptr(ptr))
23 *numInts++
24 } else {
25 addStack(uintptr(ptr))
26 *numStack++
27 }
28 return keepAlive
29}
30
31// structReturnInMemory always reports false on arm: an indirect struct return
32// is recovered from the pointer the callee leaves in a1 (see getStruct) rather
33// than through a caller-allocated hidden first argument.
34func structReturnInMemory(reflect.Type) bool {
35 return false
36}
37
38func getStruct(outType reflect.Type, syscall syscallArgs) (v reflect.Value) {
39 outSize := outType.Size()
40 if outSize == 0 {
41 return reflect.New(outType).Elem()
42 }
43 if outSize <= 4 {
44 // Fits in one register
45 return reflect.NewAt(outType, unsafe.Pointer(&struct{ a uintptr }{syscall.a1})).Elem()
46 }
47 if outSize <= 8 {
48 // Fits in two registers
49 return reflect.NewAt(outType, unsafe.Pointer(&struct{ a, b uintptr }{syscall.a1, syscall.a2})).Elem()
50 }
51 // Larger structs returned via pointer in a1
52 return reflect.NewAt(outType, *(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1))).Elem()
53}
54
55func placeRegisters(v reflect.Value, addFloat func(uintptr), addInt func(uintptr)) {
56 // TODO: For ARM32, just pass the struct data directly
57 // This is a simplified implementation
58 size := v.Type().Size()
59 if size == 0 {
60 return
61 }
62 ptr := unsafe.Pointer(v.UnsafeAddr())
63 if size <= 4 {
64 addInt(*(*uintptr)(ptr))
65 } else if size <= 8 {
66 addInt(*(*uintptr)(ptr))
67 addInt(*(*uintptr)(unsafe.Add(ptr, 4)))
68 }
69}
70
71// shouldBundleStackArgs always returns false on arm
72// since C-style stack argument bundling is only needed on Darwin ARM64.
73func shouldBundleStackArgs(v reflect.Value, numInts, numFloats int) bool {
74 return false
75}
76
77// structFitsInRegisters is not used on arm.
78func structFitsInRegisters(val reflect.Value, tempNumInts, tempNumFloats int) (bool, int, int) {
79 panic("purego: structFitsInRegisters should not be called on arm")
80}
81
82// collectStackArgs is not used on arm.
83func collectStackArgs(args []reflect.Value, startIdx int, numInts, numFloats int,
84 keepAlive []any, addInt, addFloat, addStack func(uintptr),
85 pNumInts, pNumFloats, pNumStack *int) ([]reflect.Value, []any) {
86 panic("purego: collectStackArgs should not be called on arm")
87}
88
89// bundleStackArgs is not used on arm.
90func bundleStackArgs(stackArgs []reflect.Value, addStack func(uintptr)) {
91 panic("purego: bundleStackArgs should not be called on arm")
92}
93
94func getCallbackStruct(inType reflect.Type, frame unsafe.Pointer, floatsN *int, intsN *int, stackSlot *int, stackByteOffset *uintptr) reflect.Value {
95 panic("purego: struct callback arguments are not supported on arm")
96}
97
98func setStruct(a *callbackArgs, ret reflect.Value) {
99 panic("purego: struct returns are not supported on arm")
100}