|
| 1 | +// Copyright 2026 syzkaller project authors. All rights reserved. |
| 2 | +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. |
| 3 | + |
| 4 | +// gen generates riscv64 instruction tables from riscv-unified-db YAML. |
| 5 | +// https://github.com/riscv-software-src/riscv-unified-db/tree/main/spec/std/isa/inst |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "fmt" |
| 11 | + "io/fs" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "gopkg.in/yaml.v3" |
| 17 | + |
| 18 | + "github.com/google/syzkaller/pkg/ifuzz/riscv64" |
| 19 | + "github.com/google/syzkaller/pkg/osutil" |
| 20 | + "github.com/google/syzkaller/pkg/serializer" |
| 21 | + "github.com/google/syzkaller/pkg/tool" |
| 22 | +) |
| 23 | + |
| 24 | +type instYAML struct { |
| 25 | + Kind string `yaml:"kind"` |
| 26 | + Name string `yaml:"name"` |
| 27 | + Encoding struct { |
| 28 | + Match string `yaml:"match"` |
| 29 | + Variables []struct { |
| 30 | + Name string `yaml:"name"` |
| 31 | + Location string `yaml:"location"` // e.g. "24-20" |
| 32 | + } `yaml:"variables"` |
| 33 | + } `yaml:"encoding"` |
| 34 | + Access struct { |
| 35 | + U string `yaml:"u"` |
| 36 | + VU string `yaml:"vu"` |
| 37 | + } `yaml:"access"` |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + if len(os.Args) != 3 { |
| 42 | + tool.Failf("usage: go run gen.go <riscv-unified-db/spec/std/isa/inst> <output.go>") |
| 43 | + } |
| 44 | + root := os.Args[1] |
| 45 | + outFile := os.Args[2] |
| 46 | + |
| 47 | + insns := []*riscv64.Insn{} |
| 48 | + |
| 49 | + err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + if d.IsDir() || !strings.HasSuffix(path, ".yaml") { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + data, err := os.ReadFile(path) |
| 58 | + if err != nil { |
| 59 | + return nil |
| 60 | + } |
| 61 | + |
| 62 | + var inst instYAML |
| 63 | + if err := yaml.Unmarshal(data, &inst); err != nil { |
| 64 | + return nil |
| 65 | + } |
| 66 | + if inst.Kind != "instruction" { |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + match := inst.Encoding.Match |
| 71 | + if len(match) != 32 { |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + insn, ok := buildInsn(inst) |
| 76 | + if ok { |
| 77 | + insns = append(insns, insn) |
| 78 | + } |
| 79 | + return nil |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + tool.Fail(err) |
| 83 | + } |
| 84 | + |
| 85 | + var out bytes.Buffer |
| 86 | + fmt.Fprintf(&out, `// Code generated by pkg/ifuzz/riscv64/gen. DO NOT EDIT. |
| 87 | +
|
| 88 | +// go:build !codeanalysis |
| 89 | +
|
| 90 | +package generated |
| 91 | +
|
| 92 | +import ( |
| 93 | + . "github.com/google/syzkaller/pkg/ifuzz/riscv64" |
| 94 | +) |
| 95 | +
|
| 96 | +func init() { |
| 97 | + Register(insns_riscv64) |
| 98 | +} |
| 99 | +
|
| 100 | +var insns_riscv64 = |
| 101 | +`) |
| 102 | + serializer.Write(&out, insns) |
| 103 | + |
| 104 | + if err := osutil.WriteFileAtomically(outFile, out.Bytes()); err != nil { |
| 105 | + tool.Fail(err) |
| 106 | + } |
| 107 | + |
| 108 | + fmt.Fprintf(os.Stderr, "generated %d instructions\n", len(insns)) |
| 109 | +} |
| 110 | + |
| 111 | +func buildInsn(inst instYAML) (*riscv64.Insn, bool) { |
| 112 | + match := inst.Encoding.Match |
| 113 | + |
| 114 | + var opcode uint32 |
| 115 | + var mask uint32 |
| 116 | + |
| 117 | + for i, ch := range match { |
| 118 | + bit := uint(31 - i) |
| 119 | + switch ch { |
| 120 | + case '0': |
| 121 | + mask |= 1 << bit |
| 122 | + case '1': |
| 123 | + mask |= 1 << bit |
| 124 | + opcode |= 1 << bit |
| 125 | + case '-': |
| 126 | + default: |
| 127 | + return nil, false |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fields := []riscv64.InsnField{} |
| 132 | + for _, v := range inst.Encoding.Variables { |
| 133 | + subFields, ok := parseLocations(v.Name, v.Location) |
| 134 | + if !ok { |
| 135 | + return nil, false |
| 136 | + } |
| 137 | + fields = append(fields, subFields...) |
| 138 | + } |
| 139 | + |
| 140 | + priv := inst.Access.U == "never" || inst.Access.VU == "never" |
| 141 | + |
| 142 | + return &riscv64.Insn{ |
| 143 | + Name: inst.Name, |
| 144 | + OpcodeMask: mask, |
| 145 | + Opcode: opcode, |
| 146 | + Fields: fields, |
| 147 | + AsUInt32: opcode, |
| 148 | + Generator: nil, |
| 149 | + Priv: priv, |
| 150 | + }, true |
| 151 | +} |
| 152 | + |
| 153 | +func parseLocations(name, loc string) ([]riscv64.InsnField, bool) { |
| 154 | + // Support multiple ranges separated by '|', e.g.: |
| 155 | + // "31-25|11-7" |
| 156 | + ranges := strings.Split(loc, "|") |
| 157 | + fields := make([]riscv64.InsnField, 0, len(ranges)) |
| 158 | + |
| 159 | + for _, r := range ranges { |
| 160 | + start, length, hi, lo, ok := parseRange(r) |
| 161 | + if !ok { |
| 162 | + return nil, false |
| 163 | + } |
| 164 | + |
| 165 | + fieldName := name |
| 166 | + if len(ranges) > 1 { |
| 167 | + fieldName = fmt.Sprintf("%s_%d_%d", name, hi, lo) |
| 168 | + } |
| 169 | + |
| 170 | + fields = append(fields, riscv64.InsnField{ |
| 171 | + Name: fieldName, |
| 172 | + Start: start, |
| 173 | + Length: length, |
| 174 | + }) |
| 175 | + } |
| 176 | + |
| 177 | + return fields, true |
| 178 | +} |
| 179 | + |
| 180 | +func parseRange(r string) (start, length, hi, lo uint, ok bool) { |
| 181 | + parts := strings.Split(r, "-") |
| 182 | + if len(parts) != 2 { |
| 183 | + return 0, 0, 0, 0, false |
| 184 | + } |
| 185 | + |
| 186 | + if _, err := fmt.Sscanf(parts[0], "%d", &hi); err != nil { |
| 187 | + return 0, 0, 0, 0, false |
| 188 | + } |
| 189 | + if _, err := fmt.Sscanf(parts[1], "%d", &lo); err != nil { |
| 190 | + return 0, 0, 0, 0, false |
| 191 | + } |
| 192 | + if hi < lo { |
| 193 | + return 0, 0, 0, 0, false |
| 194 | + } |
| 195 | + |
| 196 | + start = hi |
| 197 | + length = hi - lo + 1 |
| 198 | + return start, length, hi, lo, true |
| 199 | +} |
0 commit comments