-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
140 lines (114 loc) · 3.68 KB
/
main.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"github.com/ksco/riscv-vector-tests/generator"
"github.com/ksco/riscv-vector-tests/testfloat3"
)
func fatalIf(err error) {
if err == nil {
return
}
fmt.Printf("\033[0;1;31mfatal:\033[0m %s\n", err.Error())
os.Exit(1)
}
var vlenF = flag.Int("VLEN", 256, "")
var xlenF = flag.Int("XLEN", 64, "we do not support specifying ELEN yet, ELEN is consistent with XLEN.")
var splitF = flag.Int("split", 10000, "split per lines.")
var integerF = flag.Bool("integer", false, "only generate integer tests.")
var float16F = flag.Bool("float16", true, "generate float16 tests")
var patternF = flag.String("pattern", ".*", "regex to filter out tests.")
var stage1OutputDirF = flag.String("stage1output", "", "stage1 output directory.")
var configsDirF = flag.String("configs", "configs/", "config files directory.")
var testfloat3LevelF = flag.Int("testfloat3level", 2, "testfloat3 testing level (1 or 2).")
var repeatF = flag.Int("repeat", 1, "repeat same V instruction n times for a better coverage (only valid for float instructions).")
func main() {
flag.Parse()
pattern, err := regexp.Compile(*patternF)
fatalIf(err)
if stage1OutputDirF == nil || *stage1OutputDirF == "" {
fatalIf(errors.New("-stage1output is required"))
}
if !(*testfloat3LevelF == 1 || *testfloat3LevelF == 2) {
fatalIf(errors.New("-testfloat3level must be 1 or 2"))
}
if *repeatF <= 0 {
fatalIf(errors.New("-repeat must greater than 0"))
}
testfloat3.SetLevel(*testfloat3LevelF)
files, err := os.ReadDir(*configsDirF)
fatalIf(err)
println("Generating...")
makefrag := make([]string, 0)
lk := sync.Mutex{}
wg := sync.WaitGroup{}
for _, file := range files {
if *integerF && (strings.HasPrefix(file.Name(), "vf") || strings.HasPrefix(file.Name(), "vmf")) && !strings.HasPrefix(file.Name(), "vfirst") {
continue
}
if !pattern.MatchString(strings.TrimSuffix(file.Name(), ".toml")) {
continue
}
wg.Add(1)
go func(file os.DirEntry) {
name := file.Name()
fp := filepath.Join(*configsDirF, name)
if file.IsDir() ||
!strings.HasPrefix(name, "v") ||
!strings.HasSuffix(name, ".toml") {
lk.Lock()
fmt.Printf("\033[0;1;31mskipping:\033[0m %s, unrecognized filename\n", fp)
lk.Unlock()
return
}
name = strings.TrimSuffix(name, ".toml")
name = strings.Replace(name, ".", "_", -1)
contents, err := os.ReadFile(fp)
fatalIf(err)
option := generator.Option{
VLEN: generator.VLEN(*vlenF),
XLEN: generator.XLEN(*xlenF),
Repeat: *repeatF,
Float16: *float16F,
}
if (!strings.HasPrefix(file.Name(), "vf") && !strings.HasPrefix(file.Name(), "vmf")) || strings.HasPrefix(file.Name(), "vfirst") {
option.Repeat = 1
} else {
option.Fp = true
}
insn, err := generator.ReadInsnFromToml(contents, option)
fatalIf(err)
if insn.Name != strings.Replace(file.Name(), ".toml", "", -1) {
fatalIf(errors.New("filename and instruction name unmatched"))
}
for idx, testContent := range insn.Generate(*splitF) {
asmFilename := name + "-" + strconv.Itoa(idx)
writeTo(*stage1OutputDirF, asmFilename+".S", testContent)
lk.Lock()
makefrag = append(makefrag, fmt.Sprintf(" %s \\\n", asmFilename))
lk.Unlock()
}
wg.Done()
}(file)
}
wg.Wait()
sort.Slice(makefrag, func(i, j int) bool {
return makefrag[i] < makefrag[j]
})
writeTo("./", "Makefrag", "tests = \\\n"+strings.Join(makefrag, ""))
println("\033[32mOK\033[0m")
}
func writeTo(path string, name string, contents string) {
err := os.MkdirAll(path, 0777)
fatalIf(err)
err = os.WriteFile(filepath.Join(path, name), []byte(contents), 0644)
fatalIf(err)
}