Skip to content

Commit 662450c

Browse files
committed
simplify function header + reorganize new code in the file
1 parent b101fcd commit 662450c

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

func.go

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,22 @@ func RegisterFunc(fptr interface{}, cfn uintptr) {
292292
fn.Set(v)
293293
}
294294

295+
func numOfIntegerRegisters() int {
296+
switch runtime.GOARCH {
297+
case "arm64":
298+
return 8
299+
case "amd64":
300+
return 6
301+
// TODO: figure out why 386 tests are not working
302+
/*case "386":
303+
return 0
304+
case "arm":
305+
return 4*/
306+
default:
307+
panic("purego: unknown GOARCH (" + runtime.GOARCH + ")")
308+
}
309+
}
310+
295311
// WIP: Less reflection below
296312

297313
type syscallStackArm64NoWin [1 + maxArgs + numOfFloats]uintptr
@@ -562,13 +578,15 @@ func getReturnFunc[T any]() func(r1, r2 uintptr) T {
562578
return nil
563579
}
564580

565-
func argsCheck(ty reflect.Type, cfn uintptr) {
581+
func argsCheck(fptr any, cfn uintptr) {
566582
if cfn == 0 {
567583
panic("purego: cfn is nil")
568584
}
569585
// this code checks how many registers and stack this function will use
570586
// to avoid crashing with too many arguments
571587
var ints, floats, stack int
588+
589+
ty := reflect.ValueOf(fptr).Elem().Type()
572590
for i := 0; i < ty.NumIn(); i++ {
573591
arg := ty.In(i)
574592
switch arg.Kind() {
@@ -621,9 +639,8 @@ func runtime_call(ss syscallStack, cfn uintptr) (uintptr, uintptr) {
621639
// No return value
622640

623641
func RegisterFunc1_0[I0 any](fptr *func(I0), cfn uintptr) {
624-
ty := reflect.ValueOf(fptr).Elem().Type()
625642
// Prevent too many registers and check func address is okay
626-
argsCheck(ty, cfn)
643+
argsCheck(fptr, cfn)
627644
func0 := getAddFunc[I0]()
628645
// Create new function
629646
*fptr = func(i0 I0) {
@@ -637,9 +654,8 @@ func RegisterFunc1_0[I0 any](fptr *func(I0), cfn uintptr) {
637654
}
638655

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

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

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

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

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

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

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

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

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

844852
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) {
845-
ty := reflect.ValueOf(fptr).Elem().Type()
846853
// Prevent too many registers and check func address is okay
847-
argsCheck(ty, cfn)
854+
argsCheck(fptr, cfn)
848855
returnFunc := getReturnFunc[O]()
849856
func0 := getAddFunc[I0]()
850857
func1 := getAddFunc[I1]()
@@ -875,19 +882,3 @@ func RegisterFunc9_1[I0, I1, I2, I3, I4, I5, I6, I7, I8, O any](fptr *func(I0, I
875882
return returnFunc(r1, r2)
876883
}
877884
}
878-
879-
func numOfIntegerRegisters() int {
880-
switch runtime.GOARCH {
881-
case "arm64":
882-
return 8
883-
case "amd64":
884-
return 6
885-
// TODO: figure out why 386 tests are not working
886-
/*case "386":
887-
return 0
888-
case "arm":
889-
return 4*/
890-
default:
891-
panic("purego: unknown GOARCH (" + runtime.GOARCH + ")")
892-
}
893-
}

0 commit comments

Comments
 (0)