Skip to content

Commit

Permalink
Reorganize new code additions + simplify register func first block
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyko0 committed Oct 8, 2023
1 parent b101fcd commit 183ce1f
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,33 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
fn.Set(v)
}

func numOfIntegerRegisters() int {
switch runtime.GOARCH {
case "arm64":
return 8
case "amd64":
return 6
// TODO: figure out why 386 tests are not working
/*case "386":
return 0
case "arm":
return 4*/
default:
panic("purego: unknown GOARCH (" + runtime.GOARCH + ")")
}
}

// WIP: Less reflection below

type syscallStack interface {
SysArgs() []uintptr
Floats() []uintptr

addStack(x uintptr)
addInt(x uintptr)
addFloat(x uintptr)
}

type syscallStackArm64NoWin [1 + maxArgs + numOfFloats]uintptr

func (ss *syscallStackArm64NoWin) numStack() uintptr {
Expand Down Expand Up @@ -378,15 +403,6 @@ func (ss *syscallStackAmd64OrWin) Floats() []uintptr {
return ss[1+maxArgs:]
}

type syscallStack interface {
SysArgs() []uintptr
Floats() []uintptr

addStack(x uintptr)
addInt(x uintptr)
addFloat(x uintptr)
}

func newSyscallStack() syscallStack {
if runtime.GOARCH == "arm64" || runtime.GOOS != "windows" {
return &syscallStackArm64NoWin{}
Expand Down Expand Up @@ -562,13 +578,15 @@ func getReturnFunc[T any]() func(r1, r2 uintptr) T {
return nil
}

func argsCheck(ty reflect.Type, cfn uintptr) {
func argsCheck(fptr any, cfn uintptr) {
if cfn == 0 {
panic("purego: cfn is nil")
}
// this code checks how many registers and stack this function will use
// to avoid crashing with too many arguments
var ints, floats, stack int

ty := reflect.ValueOf(fptr).Elem().Type()
for i := 0; i < ty.NumIn(); i++ {
arg := ty.In(i)
switch arg.Kind() {
Expand Down Expand Up @@ -621,9 +639,8 @@ func runtime_call(ss syscallStack, cfn uintptr) (uintptr, uintptr) {
// No return value

func RegisterFunc1_0[I0 any](fptr *func(I0), cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
func0 := getAddFunc[I0]()
// Create new function
*fptr = func(i0 I0) {
Expand All @@ -637,9 +654,8 @@ func RegisterFunc1_0[I0 any](fptr *func(I0), cfn uintptr) {
}

func RegisterFunc2_0[I0, I1 any](fptr *func(I0, I1), cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
// Create new function
Expand All @@ -655,9 +671,8 @@ func RegisterFunc2_0[I0, I1 any](fptr *func(I0, I1), cfn uintptr) {
}

func RegisterFunc3_0[I0, I1, I2 any](fptr *func(I0, I1, I2), cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
func2 := getAddFunc[I2]()
Expand All @@ -675,9 +690,8 @@ func RegisterFunc3_0[I0, I1, I2 any](fptr *func(I0, I1, I2), cfn uintptr) {
}

func RegisterFunc4_0[I0, I1, I2, I3 any](fptr *func(I0, I1, I2, I3), cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
func2 := getAddFunc[I2]()
Expand All @@ -697,9 +711,8 @@ func RegisterFunc4_0[I0, I1, I2, I3 any](fptr *func(I0, I1, I2, I3), cfn uintptr
}

func RegisterFunc5_0[I0, I1, I2, I3, I4 any](fptr *func(I0, I1, I2, I3, I4), cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
func2 := getAddFunc[I2]()
Expand All @@ -725,9 +738,8 @@ func RegisterFunc5_0[I0, I1, I2, I3, I4 any](fptr *func(I0, I1, I2, I3, I4), cfn
// 1 return value

func RegisterFunc1_1[I0, O any](fptr *func(I0) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
// Create new function
Expand All @@ -744,9 +756,8 @@ func RegisterFunc1_1[I0, O any](fptr *func(I0) O, cfn uintptr) {
}

func RegisterFunc2_1[I0, I1, O any](fptr *func(I0, I1) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
Expand All @@ -765,9 +776,8 @@ func RegisterFunc2_1[I0, I1, O any](fptr *func(I0, I1) O, cfn uintptr) {
}

func RegisterFunc3_1[I0, I1, I2, O any](fptr *func(I0, I1, I2) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
Expand All @@ -788,9 +798,8 @@ func RegisterFunc3_1[I0, I1, I2, O any](fptr *func(I0, I1, I2) O, cfn uintptr) {
}

func RegisterFunc4_1[I0, I1, I2, I3, O any](fptr *func(I0, I1, I2, I3) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
Expand All @@ -813,9 +822,8 @@ func RegisterFunc4_1[I0, I1, I2, I3, O any](fptr *func(I0, I1, I2, I3) O, cfn ui
}

func RegisterFunc5_1[I0, I1, I2, I3, I4, O any](fptr *func(I0, I1, I2, I3, I4) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
Expand All @@ -842,9 +850,8 @@ func RegisterFunc5_1[I0, I1, I2, I3, I4, O any](fptr *func(I0, I1, I2, I3, I4) O
// TODO: missing 6-8

func RegisterFunc9_1[I0, I1, I2, I3, I4, I5, I6, I7, I8, O any](fptr *func(I0, I1, I2, I3, I4, I5, I6, I7, I8) O, cfn uintptr) {
ty := reflect.ValueOf(fptr).Elem().Type()
// Prevent too many registers and check func address is okay
argsCheck(ty, cfn)
argsCheck(fptr, cfn)
returnFunc := getReturnFunc[O]()
func0 := getAddFunc[I0]()
func1 := getAddFunc[I1]()
Expand Down Expand Up @@ -875,19 +882,3 @@ func RegisterFunc9_1[I0, I1, I2, I3, I4, I5, I6, I7, I8, O any](fptr *func(I0, I
return returnFunc(r1, r2)
}
}

func numOfIntegerRegisters() int {
switch runtime.GOARCH {
case "arm64":
return 8
case "amd64":
return 6
// TODO: figure out why 386 tests are not working
/*case "386":
return 0
case "arm":
return 4*/
default:
panic("purego: unknown GOARCH (" + runtime.GOARCH + ")")
}
}

0 comments on commit 183ce1f

Please sign in to comment.