File: asm.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (78 lines) | stat: -rw-r--r-- 1,500 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
		}
	}
}