| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"log" |
| 11 |
"math/rand" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"runtime" |
| 15 |
|
| 16 |
"github.com/mmcloughlin/avo/attr" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
output = flag.String("output", "", "path to output file (default stdout)") |
| 21 |
seed = flag.Int64("seed", 0, "random seed") |
| 22 |
num = flag.Int("num", 32, "number of attributes to generate") |
| 23 |
) |
| 24 |
|
| 25 |
func GenerateAttributes(n int) []attr.Attribute { |
| 26 |
as := make([]attr.Attribute, 0, n) |
| 27 |
|
| 28 |
|
| 29 |
for i := 0; i < 16 && i < n; i++ { |
| 30 |
a := attr.Attribute(1 << uint(i)) |
| 31 |
as = append(as, a) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
for len(as) < n { |
| 36 |
a := attr.Attribute(rand.Uint32()) |
| 37 |
as = append(as, a) |
| 38 |
} |
| 39 |
|
| 40 |
return as |
| 41 |
} |
| 42 |
|
| 43 |
func PrintAttributesTest(w io.Writer, as []attr.Attribute) { |
| 44 |
_, self, _, _ := runtime.Caller(0) |
| 45 |
fmt.Fprintf(w, "// Code generated by %s. DO NOT EDIT.\n\n", filepath.Base(self)) |
| 46 |
fmt.Fprintf(w, "#include \"textflag.h\"\n\n") |
| 47 |
fmt.Fprintf(w, "TEXT ·attrtest(SB), $0-1\n") |
| 48 |
fmt.Fprintf(w, "\tMOVB $0, ret+0(FP)\n") |
| 49 |
|
| 50 |
for i, a := range as { |
| 51 |
fmt.Fprintf(w, "\tMOVW $(%d), R8\n", a) |
| 52 |
fmt.Fprintf(w, "\tMOVW $(%s), R9\n", a.Asm()) |
| 53 |
fmt.Fprintf(w, "\tCMPW R8, R9\n") |
| 54 |
|
| 55 |
cont := fmt.Sprintf("cont%d", i) |
| 56 |
fmt.Fprintf(w, "\tJE %s\n", cont) |
| 57 |
fmt.Fprintf(w, "\tRET\n") |
| 58 |
|
| 59 |
fmt.Fprintf(w, "\n%s:\n", cont) |
| 60 |
} |
| 61 |
|
| 62 |
fmt.Fprintf(w, "\tMOVB $1, ret+0(FP)\n") |
| 63 |
fmt.Fprintf(w, "\tRET\n") |
| 64 |
} |
| 65 |
|
| 66 |
func main() { |
| 67 |
flag.Parse() |
| 68 |
|
| 69 |
w := os.Stdout |
| 70 |
if *output != "" { |
| 71 |
f, err := os.Create(*output) |
| 72 |
if err != nil { |
| 73 |
log.Fatal(err) |
| 74 |
} |
| 75 |
defer f.Close() |
| 76 |
w = f |
| 77 |
} |
| 78 |
|
| 79 |
rand.Seed(*seed) |
| 80 |
as := GenerateAttributes(*num) |
| 81 |
|
| 82 |
buf := bytes.NewBuffer(nil) |
| 83 |
PrintAttributesTest(buf, as) |
| 84 |
|
| 85 |
if _, err := w.Write(buf.Bytes()); err != nil { |
| 86 |
log.Fatal(err) |
| 87 |
} |
| 88 |
} |