File: make_attrtest.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 (89 lines) | stat: -rw-r--r-- 1,752 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
79
80
81
82
83
84
85
86
87
88
89
//go:build ignore
// +build ignore

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"log"
	"math/rand"
	"os"
	"path/filepath"
	"runtime"

	"github.com/mmcloughlin/avo/attr"
)

var (
	output = flag.String("output", "", "path to output file (default stdout)")
	seed   = flag.Int64("seed", 0, "random seed")
	num    = flag.Int("num", 32, "number of attributes to generate")
)

func GenerateAttributes(n int) []attr.Attribute {
	as := make([]attr.Attribute, 0, n)

	// Include each bitlevel.
	for i := 0; i < 16 && i < n; i++ {
		a := attr.Attribute(1 << uint(i))
		as = append(as, a)
	}

	// Add randomly generated attributes.
	for len(as) < n {
		a := attr.Attribute(rand.Uint32())
		as = append(as, a)
	}

	return as
}

func PrintAttributesTest(w io.Writer, as []attr.Attribute) {
	_, self, _, _ := runtime.Caller(0)
	fmt.Fprintf(w, "// Code generated by %s. DO NOT EDIT.\n\n", filepath.Base(self))
	fmt.Fprintf(w, "#include \"textflag.h\"\n\n")
	fmt.Fprintf(w, "TEXT ·attrtest(SB), $0-1\n")
	fmt.Fprintf(w, "\tMOVB $0, ret+0(FP)\n")

	for i, a := range as {
		fmt.Fprintf(w, "\tMOVW $(%d), R8\n", a)
		fmt.Fprintf(w, "\tMOVW $(%s), R9\n", a.Asm())
		fmt.Fprintf(w, "\tCMPW R8, R9\n")

		cont := fmt.Sprintf("cont%d", i)
		fmt.Fprintf(w, "\tJE   %s\n", cont)
		fmt.Fprintf(w, "\tRET\n")

		fmt.Fprintf(w, "\n%s:\n", cont)
	}

	fmt.Fprintf(w, "\tMOVB $1, ret+0(FP)\n")
	fmt.Fprintf(w, "\tRET\n")
}

func main() {
	flag.Parse()

	w := os.Stdout
	if *output != "" {
		f, err := os.Create(*output)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		w = f
	}

	rand.Seed(*seed)
	as := GenerateAttributes(*num)

	buf := bytes.NewBuffer(nil)
	PrintAttributesTest(buf, as)

	if _, err := w.Write(buf.Bytes()); err != nil {
		log.Fatal(err)
	}
}