-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: test functions with random signatures (#216)
Adds a test for function signature memory layout by generating functions with random signatures. This confirms that the size and offsets computed by `gotypes` agree with `asmdecl`. Updates #191 #195
- Loading branch information
1 parent
bcbebd5
commit 6acd6a0
Showing
4 changed files
with
2,131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,78 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"go/token" | ||
"go/types" | ||
"math/rand" | ||
|
||
. "github.com/mmcloughlin/avo/build" | ||
. "github.com/mmcloughlin/avo/gotypes" | ||
. "github.com/mmcloughlin/avo/reg" | ||
) | ||
|
||
var ( | ||
seed = flag.Int64("seed", 0, "random seed") | ||
num = flag.Int("num", 32, "number of test functions to generate") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
rand.Seed(*seed) | ||
|
||
for i := 0; i < *num; i++ { | ||
name := fmt.Sprintf("Signature%d", i) | ||
sig := RandomSignature() | ||
SignatureFunction(name, sig) | ||
} | ||
|
||
Generate() | ||
} | ||
|
||
func SignatureFunction(name string, sig *types.Signature) { | ||
// Declare the function. | ||
TEXT(name, 0, sig.String()) | ||
|
||
// Write to the results. Otherwise, asmdecl would warn us. | ||
regsize := map[int64]Virtual{1: GP8(), 2: GP16(), 4: GP32(), 8: GP64()} | ||
rs := sig.Results() | ||
for i := 0; i < rs.Len(); i++ { | ||
r := rs.At(i) | ||
size := Sizes.Sizeof(r.Type()) | ||
Store(regsize[size], ReturnIndex(i)) | ||
} | ||
|
||
RET() | ||
} | ||
|
||
func RandomSignature() *types.Signature { | ||
p := RandomTuple() | ||
r := RandomTuple() | ||
return types.NewSignature(nil, p, r, false) | ||
} | ||
|
||
func RandomTuple() *types.Tuple { | ||
n := rand.Intn(5) | ||
vs := make([]*types.Var, n) | ||
for i := 0; i < n; i++ { | ||
t := RandomType() | ||
vs[i] = types.NewVar(token.NoPos, nil, "", t) | ||
} | ||
return types.NewTuple(vs...) | ||
} | ||
|
||
func RandomType() types.Type { | ||
accept := types.IsInteger | types.IsUnsigned | ||
for { | ||
t := types.Typ[rand.Intn(len(types.Typ))] | ||
info := t.Info() | ||
if info != 0 && (info&^accept) == 0 { | ||
return t | ||
} | ||
} | ||
} |
This file contains 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,4 @@ | ||
// Package signature tests handling of random function signatures. | ||
package signature | ||
|
||
//go:generate go run asm.go -out signature.s -stubs stub.go -seed 42 -num 256 |
Oops, something went wrong.