Skip to content

Commit

Permalink
Let data section contain only necessary data (for #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksco committed Sep 28, 2023
1 parent 5db19ef commit 3c35245
Show file tree
Hide file tree
Showing 41 changed files with 170 additions and 147 deletions.
2 changes: 1 addition & 1 deletion env/riscv-test-env
Submodule riscv-test-env updated 1 files
+6 −2 v/riscv_test.h
118 changes: 66 additions & 52 deletions generator/insn.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,84 +140,84 @@ var formats = map[insnFormat]struct{}{
insnFormatVsetivli: {},
}

func (i *Insn) genCodeCombinations() []string {
func (i *Insn) genCodeCombinations(pos int) []string {
switch i.Format {
case insnFormatVdRs1mVm:
return i.genCodeVdRs1mVm()
return i.genCodeVdRs1mVm(pos)
case insnFormatVs3Rs1mVm:
return i.genCodeVs3Rs1mVm()
return i.genCodeVs3Rs1mVm(pos)
case insnFormatVdRs1m:
return i.genCodeVdRs1m()
return i.genCodeVdRs1m(pos)
case insnFormatVs3Rs1m:
return i.genCodeVs3Rs1m()
return i.genCodeVs3Rs1m(pos)
case insnFormatVdRs1mRs2Vm:
return i.genCodeVdRs1mRs2Vm()
return i.genCodeVdRs1mRs2Vm(pos)
case insnFormatVs3Rs1mRs2Vm:
return i.genCodeVs3Rs1mRs2Vm()
return i.genCodeVs3Rs1mRs2Vm(pos)
case insnFormatVdRs1mVs2Vm:
return i.genCodeVdRs1mVs2Vm()
return i.genCodeVdRs1mVs2Vm(pos)
case insnFormatVs3Rs1mVs2Vm:
return i.genCodeVs3Rs1mVs2Vm()
return i.genCodeVs3Rs1mVs2Vm(pos)
case insnFormatVdVs2Vs1:
return i.genCodeVdVs2Vs1()
return i.genCodeVdVs2Vs1(pos)
case insnFormatVdVs2Vs1V0:
return i.genCodeVdVs2Vs1V0()
return i.genCodeVdVs2Vs1V0(pos)
case insnFormatVdVs2Rs1:
return i.genCodeVdVs2Rs1()
return i.genCodeVdVs2Rs1(pos)
case insnFormatVdVs2Rs1V0:
return i.genCodeVdVs2Rs1V0()
return i.genCodeVdVs2Rs1V0(pos)
case insnFormatVdVs2Fs1V0:
return i.genCodeVdVs2Fs1V0()
return i.genCodeVdVs2Fs1V0(pos)
case insnFormatVdVs2Vs1Vm:
return i.genCodeVdVs2Vs1Vm()
return i.genCodeVdVs2Vs1Vm(pos)
case insnFormatVdVs2Rs1Vm:
return i.genCodeVdVs2Rs1Vm()
return i.genCodeVdVs2Rs1Vm(pos)
case insnFormatVdVs1Vs2Vm:
return i.genCodeVdVs1Vs2Vm()
return i.genCodeVdVs1Vs2Vm(pos)
case insnFormatVdRs1Vs2Vm:
return i.genCodeVdRs1Vs2Vm()
return i.genCodeVdRs1Vs2Vm(pos)
case insnFormatVdVs2Fs1Vm:
return i.genCodeVdVs2Fs1Vm()
return i.genCodeVdVs2Fs1Vm(pos)
case insnFormatVdVs2Imm:
return i.genCodeVdVs2Imm()
return i.genCodeVdVs2Imm(pos)
case insnFormatVdVs2ImmV0:
return i.genCodeVdVs2ImmV0()
return i.genCodeVdVs2ImmV0(pos)
case insnFormatVdVs2ImmVm:
return i.genCodeVdVs2ImmVm()
return i.genCodeVdVs2ImmVm(pos)
case insnFormatVdVs2UimmVm:
return i.genCodeVdVs2UimmVm()
return i.genCodeVdVs2UimmVm(pos)
case insnFormatVdFs1Vs2Vm:
return i.genCodeVdFs1Vs2Vm()
return i.genCodeVdFs1Vs2Vm(pos)
case insnFormatVdVs1:
return i.genCodeVdVs1()
return i.genCodeVdVs1(pos)
case insnFormatVdRs1:
return i.genCodeVdRs1()
return i.genCodeVdRs1(pos)
case insnFormatVdFs1:
return i.genCodeVdFs1()
return i.genCodeVdFs1(pos)
case insnFormatVdImm:
return i.genCodeVdImm()
return i.genCodeVdImm(pos)
case insnFormatVdVs2:
return i.genCodeVdVs2()
return i.genCodeVdVs2(pos)
case insnFormatVdVs2Vm:
return i.genCodeVdVs2Vm()
return i.genCodeVdVs2Vm(pos)
case insnFormatVdVs2VmP2:
return i.genCodeVdVs2VmP2()
return i.genCodeVdVs2VmP2(pos)
case insnFormatVdVs2VmP3:
return i.genCodeVdVs2VmP3()
return i.genCodeVdVs2VmP3(pos)
case insnFormatRdVs2Vm:
return i.genCodeRdVs2Vm()
return i.genCodeRdVs2Vm(pos)
case insnFormatRdVs2:
return i.genCodeRdVs2()
return i.genCodeRdVs2(pos)
case insnFormatFdVs2:
return i.genCodeFdVs2()
return i.genCodeFdVs2(pos)
case insnFormatVdVm:
return i.genCodeVdVm()
return i.genCodeVdVm(pos)
case insnFormatVsetvli:
return i.genCodevsetvli()
return i.genCodevsetvli(pos)
case insnFormatVsetvl:
return i.genCodevsetvl()
return i.genCodevsetvl(pos)
case insnFormatVsetivli:
return i.genCodevsetivli()
return i.genCodevsetivli(pos)
default:
log.Fatalln("unreachable")
return nil
Expand Down Expand Up @@ -263,11 +263,12 @@ func (i *Insn) check() error {
func (i *Insn) Generate(splitPerLines int) []string {
res := make([]string, 0)

for _, code := range i.genMergedCodeCombinations(splitPerLines) {
codes, data := i.genMergedCodeCombinations(splitPerLines)
for idx, code := range codes {
builder := strings.Builder{}
builder.WriteString(i.genHeader())
builder.WriteString(code)
builder.WriteString(i.genData())
builder.WriteString(data[idx])
res = append(res, builder.String())
}
return res
Expand All @@ -285,26 +286,39 @@ RVTEST_RV%dUV
`, i.Name, i.Option.XLEN)
}

func (i *Insn) genMergedCodeCombinations(splitPerLines int) []string {
res := make([]string, 0)
func (i *Insn) genMergedCodeCombinations(splitPerLines int) ([]string, []string) {
codeRes := make([]string, 0)
dataRes := make([]string, 0)
builder := strings.Builder{}
cs := i.genCodeCombinations()
for idx, c := range cs {
builder.WriteString(c)
if (splitPerLines > 0 && strings.Count(builder.String(), "\n") > splitPerLines) ||
idx == len(cs)-1 {
buf := fmt.Sprintf(`
done := false
pos := 0
for !done {
cs := i.genCodeCombinations(pos)
for idx, c := range cs {
builder.WriteString(c)
if (splitPerLines > 0 && strings.Count(builder.String(), "\n") > splitPerLines) ||
idx == len(cs)-1 {
buf := fmt.Sprintf(`
RVTEST_CODE_BEGIN
%s
TEST_CASE(2, x0, 0x0)
TEST_PASSFAIL
RVTEST_CODE_END
`, builder.String())
res = append(res, buf)
builder.Reset()
codeRes = append(codeRes, buf)
dataRes = append(dataRes, i.genData())
i.TestData = &TestData{}
builder.Reset()
pos += idx
if idx == len(cs)-1 {
done = true
}
break
}
}
}
return res

return codeRes, dataRes
}

func (i *Insn) genData() string {
Expand Down
4 changes: 2 additions & 2 deletions generator/insn_fdvs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
)

func (i *Insn) genCodeFdVs2() []string {
func (i *Insn) genCodeFdVs2(pos int) []string {
combinations := i.combinations([]LMUL{1}, floatSEWs, []bool{false})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
18 changes: 10 additions & 8 deletions generator/insn_g.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/binary"
"fmt"
"math"
"strings"
)

Expand All @@ -23,24 +24,25 @@ func (i *Insn) gWriteRandomData(lmul LMUL) string {
return builder.String()
}

func (i *Insn) gWriteIndexData(lmul1 LMUL, n int, width SEW, sew SEW) string {
func (i *Insn) gWriteIndexData(dataLmul1 LMUL, offsetLmul1 LMUL, n int, dataSew SEW, offsetSew SEW) string {
if n <= 0 {
n = 1
}
nBytes := i.vlenb() * int(lmul1)
nBytes := i.vlenb() * int(offsetLmul1)
builder := strings.Builder{}
buf := &bytes.Buffer{}
n = int(math.Min(float64(n), float64(int(dataLmul1)*i.vlenb()/(int(dataSew)/8))))
s := genShuffledSlice(n)
for a := 0; a < nBytes/(int(sew)/8); a++ {
switch sew {
for a := 0; a < nBytes/(int(offsetSew)/8); a++ {
switch offsetSew {
case 8:
_ = binary.Write(buf, binary.LittleEndian, uint8(s[a%n]*int(width)/8))
_ = binary.Write(buf, binary.LittleEndian, uint8(s[a%n]*int(dataSew)/8))
case 16:
_ = binary.Write(buf, binary.LittleEndian, uint16(s[a%n]*int(width)/8))
_ = binary.Write(buf, binary.LittleEndian, uint16(s[a%n]*int(dataSew)/8))
case 32:
_ = binary.Write(buf, binary.LittleEndian, uint32(s[a%n]*int(width)/8))
_ = binary.Write(buf, binary.LittleEndian, uint32(s[a%n]*int(dataSew)/8))
case 64:
_ = binary.Write(buf, binary.LittleEndian, uint64(s[a%n]*int(width)/8))
_ = binary.Write(buf, binary.LittleEndian, uint64(s[a%n]*int(dataSew)/8))
}
}
off := i.TestData.Append(buf.Bytes())
Expand Down
4 changes: 2 additions & 2 deletions generator/insn_rdvs2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
)

func (i *Insn) genCodeRdVs2() []string {
func (i *Insn) genCodeRdVs2(pos int) []string {
combinations := i.combinations([]LMUL{1}, allSEWs, []bool{false})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_rdvs2vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
)

func (i *Insn) genCodeRdVs2Vm() []string {
func (i *Insn) genCodeRdVs2Vm(pos int) []string {
combinations := i.combinations([]LMUL{1}, []SEW{8}, []bool{false, true})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_vdfs1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"
)

func (i *Insn) genCodeVdFs1() []string {
func (i *Insn) genCodeVdFs1(pos int) []string {
lmuls := iff(strings.HasSuffix(i.Name, ".s.f"), []LMUL{1}, allLMULs)
combinations := i.combinations(lmuls, floatSEWs, []bool{false})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
5 changes: 3 additions & 2 deletions generator/insn_vdfs1vs2vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func (i *Insn) genCodeVdFs1Vs2Vm() []string {
func (i *Insn) genCodeVdFs1Vs2Vm(pos int) []string {
vdWidening := strings.HasPrefix(i.Name, "vfw")
vdSize := iff(vdWidening, 2, 1)

Expand All @@ -18,7 +18,7 @@ func (i *Insn) genCodeVdFs1Vs2Vm() []string {
)
res := make([]string, 0, len(combinations))

for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand All @@ -28,6 +28,7 @@ func (i *Insn) genCodeVdFs1Vs2Vm() []string {
vdEMUL1 := LMUL(math.Max(float64(int(c.LMUL)*vdSize), 1))
vdEEW := c.SEW * SEW(vdSize)
if vdEEW > SEW(i.Option.XLEN) {
res = append(res, "")
continue
}

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_vdimm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"strings"
)

func (i *Insn) genCodeVdImm() []string {
func (i *Insn) genCodeVdImm(pos int) []string {
combinations := i.combinations(allLMULs, allSEWs, []bool{false})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_vdrs1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"strings"
)

func (i *Insn) genCodeVdRs1() []string {
func (i *Insn) genCodeVdRs1(pos int) []string {
lmuls := iff(strings.HasSuffix(i.Name, ".s.x"), []LMUL{1}, allLMULs)
combinations := i.combinations(lmuls, allSEWs, []bool{false})

res := make([]string, 0, len(combinations))
for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_vdrs1m.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func (i *Insn) genCodeVdRs1m() []string {
func (i *Insn) genCodeVdRs1m(pos int) []string {
s := regexp.MustCompile(`vl(\d)re(\d+)\.v`)
subs := s.FindStringSubmatch(i.Name)

Expand All @@ -23,7 +23,7 @@ func (i *Insn) genCodeVdRs1m() []string {

res := make([]string, 0, len(combinations))

for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
4 changes: 2 additions & 2 deletions generator/insn_vdrs1mrs2vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"strings"
)

func (i *Insn) genCodeVdRs1mRs2Vm() []string {
func (i *Insn) genCodeVdRs1mRs2Vm(pos int) []string {
nfields := getNfieldsRoundedUp(i.Name)
combinations := i.combinations(
nfieldsLMULs(nfields),
[]SEW{getEEW(i.Name)},
[]bool{false, true})
res := make([]string, 0, len(combinations))

for _, c := range combinations {
for _, c := range combinations[pos:] {
builder := strings.Builder{}
builder.WriteString(c.comment())

Expand Down
Loading

0 comments on commit 3c35245

Please sign in to comment.