Skip to content

Commit

Permalink
tests: test functions with random signatures (#216)
Browse files Browse the repository at this point in the history
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
mmcloughlin committed Oct 31, 2021
1 parent bcbebd5 commit 6acd6a0
Show file tree
Hide file tree
Showing 4 changed files with 2,131 additions and 0 deletions.
78 changes: 78 additions & 0 deletions tests/signature/asm.go
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
}
}
}
4 changes: 4 additions & 0 deletions tests/signature/gen.go
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

0 comments on commit 6acd6a0

Please sign in to comment.