| 1 | package load |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "path/filepath" |
| 7 | "reflect" |
| 8 | "regexp" |
| 9 | "sort" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/mmcloughlin/avo/internal/inst" |
| 14 | "github.com/mmcloughlin/avo/internal/opcodescsv" |
| 15 | "github.com/mmcloughlin/avo/internal/opcodesextra" |
| 16 | "github.com/mmcloughlin/avo/internal/opcodesxml" |
| 17 | ) |
| 18 | |
| 19 | // This file is a mess. Some of this complexity is unavoidable, since the state |
| 20 | // of x86 instruction databases is also a mess, especially when it comes to |
| 21 | // idiosyncrasies of the Go assembler implementation. Some of the complexity is |
| 22 | // probably avoidable by migrating to using Intel XED |
| 23 | // (https://github.com/mmcloughlin/avo/issues/23), but for now this is an unholy |
| 24 | // mix of PeachPy's Opcodes database and Go's x86 CSV file. |
| 25 | // |
| 26 | // The goal is simply to keep as much of the uglyness in this file as possible, |
| 27 | // producing a clean instruction database for the rest of avo to use. Any nasty |
| 28 | // logic here should be backed up with a test somewhere to ensure the result is |
| 29 | // correct, even if the code that produced it is awful. |
| 30 | |
| 31 | // Expected data source filenames. |
| 32 | const ( |
| 33 | DefaultCSVName = "x86.v0.2.csv" |
| 34 | DefaultOpcodesXMLName = "x86_64.xml" |
| 35 | ) |
| 36 | |
| 37 | // Loader builds an instruction database from underlying datasources. |
| 38 | type Loader struct { |
| 39 | X86CSVPath string |
| 40 | OpcodesXMLPath string |
| 41 | |
| 42 | alias map[opcodescsv.Alias]string |
| 43 | order map[string]opcodescsv.OperandOrder |
| 44 | } |
| 45 | |
| 46 | // NewLoaderFromDataDir constructs an instruction loader from datafiles in the |
| 47 | // given directory. The files themselves are expected to have the standard |
| 48 | // names: see Default*Name constants. |
| 49 | func NewLoaderFromDataDir(dir string) *Loader { |
| 50 | return &Loader{ |
| 51 | X86CSVPath: filepath.Join(dir, DefaultCSVName), |
| 52 | OpcodesXMLPath: filepath.Join(dir, DefaultOpcodesXMLName), |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Load performs instruction loading. |
| 57 | func (l *Loader) Load() ([]inst.Instruction, error) { |
| 58 | if err := l.init(); err != nil { |
| 59 | return nil, err |
| 60 | } |
| 61 | |
| 62 | // Load Opcodes XML file. |
| 63 | iset, err := opcodesxml.ReadFile(l.OpcodesXMLPath) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | // Load opcodes XML data, grouped by Go opcode. |
| 69 | im := map[string]*inst.Instruction{} |
| 70 | for _, i := range iset.Instructions { |
| 71 | for _, f := range i.Forms { |
| 72 | if !l.include(f) { |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | for _, opcode := range l.gonames(f) { |
| 77 | if im[opcode] == nil { |
| 78 | im[opcode] = &inst.Instruction{ |
| 79 | Opcode: opcode, |
| 80 | Summary: i.Summary, |
| 81 | } |
| 82 | } |
| 83 | im[opcode].Forms = append(im[opcode].Forms, l.form(opcode, f)) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Add extras to our list. |
| 89 | for _, e := range opcodesextra.Instructions() { |
| 90 | im[e.Opcode] = e |
| 91 | } |
| 92 | |
| 93 | // Generate additional AVX-512 forms. |
| 94 | for _, i := range im { |
| 95 | i.Forms = avx512forms(i.Opcode, i.Forms) |
| 96 | } |
| 97 | |
| 98 | // Merge aliased forms. This is primarily for MOVQ (issue #50). |
| 99 | for _, a := range aliases { |
| 100 | if existing, found := im[a.From]; found { |
| 101 | im[a.To].Forms = append(im[a.To].Forms, existing.Forms...) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Apply list of aliases. |
| 106 | for _, a := range aliases { |
| 107 | cpy := *im[a.To] |
| 108 | cpy.Opcode = a.From |
| 109 | cpy.AliasOf = a.To |
| 110 | im[a.From] = &cpy |
| 111 | } |
| 112 | |
| 113 | // Dedupe forms. |
| 114 | for _, i := range im { |
| 115 | i.Forms = dedupe(i.Forms) |
| 116 | } |
| 117 | |
| 118 | // Resolve forms that have VEX and EVEX encoded forms. |
| 119 | for _, i := range im { |
| 120 | i.Forms, err = vexevex(i.Forms) |
| 121 | if err != nil { |
| 122 | return nil, err |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Convert to a slice. Sort instructions and forms for reproducibility. |
| 127 | is := make([]inst.Instruction, 0, len(im)) |
| 128 | for _, i := range im { |
| 129 | sortforms(i.Forms) |
| 130 | is = append(is, *i) |
| 131 | } |
| 132 | |
| 133 | sort.Slice(is, func(i, j int) bool { |
| 134 | return is[i].Opcode < is[j].Opcode |
| 135 | }) |
| 136 | |
| 137 | return is, nil |
| 138 | } |
| 139 | |
| 140 | func (l *Loader) init() error { |
| 141 | icsv, err := opcodescsv.ReadFile(l.X86CSVPath) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | |
| 146 | l.alias, err = opcodescsv.BuildAliasMap(icsv) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | |
| 151 | l.order = opcodescsv.BuildOrderMap(icsv) |
| 152 | |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | // include decides whether to include the instruction form in the avo listing. |
| 157 | // This discards some opcodes that are not supported in Go. |
| 158 | func (l Loader) include(f opcodesxml.Form) bool { |
| 159 | // Exclude certain ISAs simply not present in Go |
| 160 | for _, isa := range f.ISA { |
| 161 | switch isa.ID { |
| 162 | // AMD-only. |
| 163 | case "TBM", "CLZERO", "FMA4", "XOP", "SSE4A", "3dnow!", "3dnow!+": |
| 164 | return false |
| 165 | // AVX512PF doesn't work without some special case handling, and is only on Knights Landing/Knights Mill. |
| 166 | case "AVX512PF": |
| 167 | return false |
| 168 | // Incomplete support for some prefetching instructions. |
| 169 | case "PREFETCH", "PREFETCHW", "PREFETCHWT1", "CLWB": |
| 170 | return false |
| 171 | // Remaining oddities. |
| 172 | case "MONITORX", "FEMMS": |
| 173 | return false |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // Go appears to have skeleton support for MMX instructions. See the many TODO lines in the testcases: |
| 178 | // Reference: https://github.com/golang/go/blob/649b89377e91ad6dbe710784f9e662082d31a1ff/src/cmd/asm/internal/asm/testdata/amd64enc.s#L3310-L3312 |
| 179 | // |
| 180 | // //TODO: PALIGNR $7, (BX), M2 // 0f3a0f1307 |
| 181 | // //TODO: PALIGNR $7, (R11), M2 // 410f3a0f1307 |
| 182 | // //TODO: PALIGNR $7, M2, M2 // 0f3a0fd207 |
| 183 | // |
| 184 | if f.MMXMode == "MMX" { |
| 185 | return false |
| 186 | } |
| 187 | |
| 188 | // x86 csv contains a number of CMOV* instructions which are actually not valid |
| 189 | // Go instructions. The valid Go forms should have different opcodes from GNU. |
| 190 | // Therefore a decent "heuristic" is CMOV* instructions that do not have |
| 191 | // aliases. |
| 192 | if strings.HasPrefix(f.GASName, "cmov") && l.lookupAlias(f) == "" { |
| 193 | return false |
| 194 | } |
| 195 | |
| 196 | // Some specific exclusions. |
| 197 | switch f.GASName { |
| 198 | // Certain branch instructions appear to not be supported. |
| 199 | // |
| 200 | // Reference: https://github.com/golang/go/blob/649b89377e91ad6dbe710784f9e662082d31a1ff/src/cmd/asm/internal/asm/testdata/amd64enc.s#L757 |
| 201 | // |
| 202 | // //TODO: CALLQ* (BX) // ff13 |
| 203 | // |
| 204 | // Reference: https://github.com/golang/go/blob/649b89377e91ad6dbe710784f9e662082d31a1ff/src/cmd/asm/internal/asm/testdata/amd64enc.s#L2108 |
| 205 | // |
| 206 | // //TODO: LJMPL* (R11) // 41ff2b |
| 207 | // |
| 208 | case "callq", "jmpl": |
| 209 | return false |
| 210 | // MOVABS doesn't seem to be supported either. |
| 211 | // |
| 212 | // Reference: https://github.com/golang/go/blob/1ac84999b93876bb06887e483ae45b27e03d7423/src/cmd/asm/internal/asm/testdata/amd64enc.s#L2354 |
| 213 | // |
| 214 | // //TODO: MOVABSB 0x123456789abcdef1, AL // a0f1debc9a78563412 |
| 215 | // |
| 216 | case "movabs": |
| 217 | return false |
| 218 | // Only one XLAT form is supported. |
| 219 | // |
| 220 | // Reference: https://github.com/golang/arch/blob/b19384d3c130858bb31a343ea8fce26be71b5998/x86/x86.v0.2.csv#L2221-L2222 |
| 221 | // |
| 222 | // "XLATB","XLAT","xlat","D7","V","V","","ignoreREXW","","","" |
| 223 | // "XLATB","XLAT","xlat","REX.W D7","N.E.","V","","pseudo","","","" |
| 224 | // |
| 225 | // Reference: https://github.com/golang/go/blob/b3294d9491b898396e134bad5412d85337c37b64/src/cmd/internal/obj/x86/asm6.go#L1519 |
| 226 | // |
| 227 | // {AXLAT, ynone, Px, opBytes{0xd7}}, |
| 228 | // |
| 229 | // TODO(mbm): confirm the Px prefix means non REX mode |
| 230 | case "xlatb": |
| 231 | return f.Encoding.REX == nil |
| 232 | } |
| 233 | |
| 234 | return true |
| 235 | } |
| 236 | |
| 237 | func (l Loader) lookupAlias(f opcodesxml.Form) string { |
| 238 | // Attempt lookup with datasize. |
| 239 | k := opcodescsv.Alias{ |
| 240 | Opcode: f.GASName, |
| 241 | DataSize: datasize(f), |
| 242 | NumOperands: len(f.Operands), |
| 243 | } |
| 244 | if a := l.alias[k]; a != "" { |
| 245 | return a |
| 246 | } |
| 247 | |
| 248 | // Fallback to unknown datasize. |
| 249 | k.DataSize = 0 |
| 250 | return l.alias[k] |
| 251 | } |
| 252 | |
| 253 | func (l Loader) gonames(f opcodesxml.Form) []string { |
| 254 | s := datasize(f) |
| 255 | |
| 256 | // Suspect a "bug" in x86 CSV for the CVTTSD2SQ instruction, as CVTTSD2SL appears twice. |
| 257 | // |
| 258 | // Reference: https://github.com/golang/arch/blob/b19384d3c130858bb31a343ea8fce26be71b5998/x86/x86.v0.2.csv#L345-L346 |
| 259 | // |
| 260 | // "CVTTSD2SI r32, xmm2/m64","CVTTSD2SL xmm2/m64, r32","cvttsd2si xmm2/m64, r32","F2 0F 2C /r","V","V","SSE2","operand16,operand32","w,r","Y","32" |
| 261 | // "CVTTSD2SI r64, xmm2/m64","CVTTSD2SL xmm2/m64, r64","cvttsd2siq xmm2/m64, r64","F2 REX.W 0F 2C /r","N.E.","V","SSE2","","w,r","Y","64" |
| 262 | // |
| 263 | // Reference: https://github.com/golang/go/blob/048c9164a0c5572df18325e377473e7893dbfb07/src/cmd/internal/obj/x86/asm6.go#L1073-L1074 |
| 264 | // |
| 265 | // {ACVTTSD2SL, yxcvfl, Pf2, opBytes{0x2c}}, |
| 266 | // {ACVTTSD2SQ, yxcvfq, Pw, opBytes{Pf2, 0x2c}}, |
| 267 | // |
| 268 | if f.GASName == "cvttsd2si" && s == 64 { |
| 269 | return []string{"CVTTSD2SQ"} |
| 270 | } |
| 271 | |
| 272 | // Return alias if available. |
| 273 | if a := l.lookupAlias(f); a != "" { |
| 274 | return []string{a} |
| 275 | } |
| 276 | |
| 277 | // Some odd special cases. |
| 278 | // TODO(mbm): can this be handled by processing csv entries with slashes / |
| 279 | if f.GoName == "RET" && len(f.Operands) == 1 { |
| 280 | return []string{"RETFW", "RETFL", "RETFQ"} |
| 281 | } |
| 282 | |
| 283 | // IMUL 3-operand forms are not recorded correctly in either x86 CSV or opcodes. They are supposed to be called IMUL3{W,L,Q} |
| 284 | // |
| 285 | // Reference: https://github.com/golang/go/blob/649b89377e91ad6dbe710784f9e662082d31a1ff/src/cmd/internal/obj/x86/asm6.go#L1112-L1114 |
| 286 | // |
| 287 | // {AIMUL3W, yimul3, Pe, opBytes{0x6b, 00, 0x69, 00}}, |
| 288 | // {AIMUL3L, yimul3, Px, opBytes{0x6b, 00, 0x69, 00}}, |
| 289 | // {AIMUL3Q, yimul3, Pw, opBytes{0x6b, 00, 0x69, 00}}, |
| 290 | // |
| 291 | // Reference: https://github.com/golang/arch/blob/b19384d3c130858bb31a343ea8fce26be71b5998/x86/x86.v0.2.csv#L549 |
| 292 | // |
| 293 | // "IMUL r32, r/m32, imm32","IMULL imm32, r/m32, r32","imull imm32, r/m32, r32","69 /r id","V","V","","operand32","rw,r,r","Y","32" |
| 294 | // |
| 295 | if strings.HasPrefix(f.GASName, "imul") && len(f.Operands) == 3 { |
| 296 | return []string{strings.ToUpper(f.GASName[:4] + "3" + f.GASName[4:])} |
| 297 | } |
| 298 | |
| 299 | // Use go opcode from Opcodes XML where available. |
| 300 | if f.GoName != "" { |
| 301 | return []string{f.GoName} |
| 302 | } |
| 303 | |
| 304 | // Fallback to GAS name. |
| 305 | n := strings.ToUpper(f.GASName) |
| 306 | |
| 307 | // Some need data sizes added to them. |
| 308 | n += sizesuffix(n, f) |
| 309 | |
| 310 | return []string{n} |
| 311 | } |
| 312 | |
| 313 | func (l Loader) form(opcode string, f opcodesxml.Form) inst.Form { |
| 314 | // Map operands to avo format and ensure correct order. |
| 315 | ops := operands(f.Operands) |
| 316 | |
| 317 | switch l.order[opcode] { |
| 318 | case opcodescsv.IntelOrder: |
| 319 | // Nothing to do. |
| 320 | case opcodescsv.CMP3Order: |
| 321 | ops[0], ops[1] = ops[1], ops[0] |
| 322 | case opcodescsv.UnknownOrder: |
| 323 | // Instructions not in x86 CSV are assumed to have reverse intel order. |
| 324 | fallthrough |
| 325 | case opcodescsv.ReverseIntelOrder: |
| 326 | for l, r := 0, len(ops)-1; l < r; l, r = l+1, r-1 { |
| 327 | ops[l], ops[r] = ops[r], ops[l] |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Handle some exceptions. |
| 332 | // TODO(mbm): consider if there's some nicer way to handle the list of special cases. |
| 333 | switch opcode { |
| 334 | // Go assembler has an internal Yu2 operand type for unsigned 2-bit immediates. |
| 335 | // |
| 336 | // Reference: https://github.com/golang/go/blob/6d5caf38e37bf9aeba3291f1f0b0081f934b1187/src/cmd/internal/obj/x86/asm6.go#L109 |
| 337 | // |
| 338 | // Yu2 // $x, x fits in uint2 |
| 339 | // |
| 340 | // Reference: https://github.com/golang/go/blob/6d5caf38e37bf9aeba3291f1f0b0081f934b1187/src/cmd/internal/obj/x86/asm6.go#L858-L864 |
| 341 | // |
| 342 | // var yextractps = []ytab{ |
| 343 | // {Zibr_m, 2, argList{Yu2, Yxr, Yml}}, |
| 344 | // } |
| 345 | // |
| 346 | // var ysha1rnds4 = []ytab{ |
| 347 | // {Zibm_r, 2, argList{Yu2, Yxm, Yxr}}, |
| 348 | // } |
| 349 | // |
| 350 | case "SHA1RNDS4", "EXTRACTPS": |
| 351 | ops[0].Type = "imm2u" |
| 352 | } |
| 353 | |
| 354 | // Extract implicit operands. |
| 355 | var implicits []inst.ImplicitOperand |
| 356 | for _, implicit := range f.ImplicitOperands { |
| 357 | implicits = append(implicits, inst.ImplicitOperand{ |
| 358 | Register: implicit.ID, |
| 359 | Action: inst.ActionFromReadWrite(implicit.Input, implicit.Output), |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | // Extract ISA flags. |
| 364 | var isas []string |
| 365 | for _, isa := range f.ISA { |
| 366 | isas = append(isas, isa.ID) |
| 367 | } |
| 368 | sort.Strings(isas) |
| 369 | |
| 370 | // Build form. |
| 371 | return inst.Form{ |
| 372 | ISA: isas, |
| 373 | Operands: ops, |
| 374 | ImplicitOperands: implicits, |
| 375 | EncodingType: enctype(f), |
| 376 | CancellingInputs: f.CancellingInputs, |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // operands maps Opcodes XML operands to avo format. Returned in Intel order. |
| 381 | func operands(ops []opcodesxml.Operand) []inst.Operand { |
| 382 | n := len(ops) |
| 383 | r := make([]inst.Operand, n) |
| 384 | for i, op := range ops { |
| 385 | r[i] = operand(op) |
| 386 | } |
| 387 | return r |
| 388 | } |
| 389 | |
| 390 | // operand maps an Opcodes XML operand to avo format. |
| 391 | func operand(op opcodesxml.Operand) inst.Operand { |
| 392 | return inst.Operand{ |
| 393 | Type: op.Type, |
| 394 | Action: inst.ActionFromReadWrite(op.Input, op.Output), |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // avx512forms processes AVX-512 operands and expands them into additional |
| 399 | // instruction forms as expected by the Go assembler. |
| 400 | // |
| 401 | // See: https://go.dev/wiki/AVX512 |
| 402 | func avx512forms(opcode string, forms []inst.Form) []inst.Form { |
| 403 | // Apply modification stages to produce final list of forms. |
| 404 | stages := []func(string, inst.Form) []inst.Form{ |
| 405 | avx512rounding, |
| 406 | avx512sae, |
| 407 | avx512bcst, |
| 408 | avx512masking, |
| 409 | avx512zeroing, |
| 410 | } |
| 411 | |
| 412 | for _, stage := range stages { |
| 413 | var next []inst.Form |
| 414 | for _, f := range forms { |
| 415 | next = append(next, stage(opcode, f)...) |
| 416 | } |
| 417 | forms = next |
| 418 | } |
| 419 | |
| 420 | return forms |
| 421 | } |
| 422 | |
| 423 | // avx512rounding handles AVX-512 embedded rounding. Opcodes database represents |
| 424 | // these as {er} operands, whereas Go uses instruction suffixes. Remove the |
| 425 | // operand if present and set the corresponding flag. |
| 426 | func avx512rounding(opcode string, f inst.Form) []inst.Form { |
| 427 | i, found := findoperand(f.Operands, "{er}") |
| 428 | if !found { |
| 429 | return []inst.Form{f} |
| 430 | } |
| 431 | |
| 432 | // Delete the {er} operand. |
| 433 | f.Operands = append(f.Operands[:i], f.Operands[i+1:]...) |
| 434 | |
| 435 | // Create a second form with the rounding flag. |
| 436 | er := f.Clone() |
| 437 | er.EmbeddedRounding = true |
| 438 | |
| 439 | return []inst.Form{f, er} |
| 440 | } |
| 441 | |
| 442 | // avx512sae handles AVX-512 "suppress all exceptions". Opcodes database |
| 443 | // represents these as {sae} operands, whereas Go uses instruction suffixes. |
| 444 | // Remove the operand if present and set the corresponding flag. |
| 445 | func avx512sae(opcode string, f inst.Form) []inst.Form { |
| 446 | i, found := findoperand(f.Operands, "{sae}") |
| 447 | if !found { |
| 448 | return []inst.Form{f} |
| 449 | } |
| 450 | |
| 451 | // Delete the {sae} operand. |
| 452 | f.Operands = append(f.Operands[:i], f.Operands[i+1:]...) |
| 453 | |
| 454 | // Create a second form with the rounding flag. |
| 455 | sae := f.Clone() |
| 456 | sae.SuppressAllExceptions = true |
| 457 | |
| 458 | return []inst.Form{f, sae} |
| 459 | } |
| 460 | |
| 461 | // avx512bcst handles AVX-512 broadcast. Opcodes database uses operands like |
| 462 | // "m512/m64bcst" to indicate broadcast. Go uses the BCST suffix to enable it. |
| 463 | // Split the form into two, the regular and broadcast versions. |
| 464 | func avx512bcst(opcode string, f inst.Form) []inst.Form { |
| 465 | // Look for broadcast operand. |
| 466 | idx := -1 |
| 467 | for i, op := range f.Operands { |
| 468 | if bcstrx.MatchString(op.Type) { |
| 469 | idx = i |
| 470 | break |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if idx < 0 { |
| 475 | return []inst.Form{f} |
| 476 | } |
| 477 | |
| 478 | // Create two forms. |
| 479 | match := bcstrx.FindStringSubmatch(f.Operands[idx].Type) |
| 480 | |
| 481 | mem := f.Clone() |
| 482 | mem.Operands[idx].Type = match[1] |
| 483 | |
| 484 | bcst := f.Clone() |
| 485 | bcst.Broadcast = true |
| 486 | bcst.Operands[idx].Type = match[2] |
| 487 | |
| 488 | return []inst.Form{mem, bcst} |
| 489 | } |
| 490 | |
| 491 | var bcstrx = regexp.MustCompile(`^(m\d+)/(m\d+)bcst$`) |
| 492 | |
| 493 | // avx512masking handles AVX-512 masking forms. |
| 494 | func avx512masking(opcode string, f inst.Form) []inst.Form { |
| 495 | // In order to support implicit masking (with K0), Go has two instruction |
| 496 | // forms, one with the mask and one without. The mask register precedes the |
| 497 | // output register. The Opcodes database (similar to Intel manuals) |
| 498 | // represents masking with the {k} operand suffix, possibly with {z} for |
| 499 | // zeroing. |
| 500 | |
| 501 | // Look for masking with possible zeroing. Zeroing is handled by a later |
| 502 | // processing stage, but we need to be sure to notice and preserve it here. |
| 503 | masking := false |
| 504 | zeroing := false |
| 505 | idx := -1 |
| 506 | for i := range f.Operands { |
| 507 | op := &f.Operands[i] |
| 508 | if strings.HasSuffix(op.Type, "{z}") { |
| 509 | zeroing = true |
| 510 | op.Type = strings.TrimSuffix(op.Type, "{z}") |
| 511 | } |
| 512 | if strings.HasSuffix(op.Type, "{k}") { |
| 513 | masking = true |
| 514 | idx = i |
| 515 | op.Type = strings.TrimSuffix(op.Type, "{k}") |
| 516 | break |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Bail if no masking. |
| 521 | if !masking { |
| 522 | return []inst.Form{f} |
| 523 | } |
| 524 | |
| 525 | // Unmasked variant. |
| 526 | unmasked := f.Clone() |
| 527 | |
| 528 | // Masked form has "k" operand inserted. |
| 529 | masked := f.Clone() |
| 530 | mask := inst.Operand{Type: "k", Action: inst.R} |
| 531 | ops := append([]inst.Operand(nil), masked.Operands[:idx]...) |
| 532 | ops = append(ops, mask) |
| 533 | ops = append(ops, masked.Operands[idx:]...) |
| 534 | masked.Operands = ops |
| 535 | |
| 536 | // Restore zeroing suffix, so it can he handled later. |
| 537 | if zeroing { |
| 538 | masked.Operands[idx+1].Type += "{z}" |
| 539 | } |
| 540 | |
| 541 | // Almost all instructions take an optional mask, apart from a few |
| 542 | // special cases. |
| 543 | if maskrequired[opcode] { |
| 544 | return []inst.Form{masked} |
| 545 | } |
| 546 | return []inst.Form{unmasked, masked} |
| 547 | } |
| 548 | |
| 549 | // avx512zeroing handles AVX-512 zeroing forms. |
| 550 | func avx512zeroing(opcode string, f inst.Form) []inst.Form { |
| 551 | // Zeroing in Go is handled with the Z opcode suffix. Note that zeroing has |
| 552 | // an important effect on the instruction form, since the merge masking form |
| 553 | // has an input dependency for the output register, and the zeroing form |
| 554 | // does not. |
| 555 | |
| 556 | // Look for zeroing operand. |
| 557 | idx := -1 |
| 558 | for i := range f.Operands { |
| 559 | op := &f.Operands[i] |
| 560 | if strings.HasSuffix(op.Type, "{z}") { |
| 561 | idx = i |
| 562 | op.Type = strings.TrimSuffix(op.Type, "{z}") |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if idx < 0 { |
| 567 | return []inst.Form{f} |
| 568 | } |
| 569 | |
| 570 | // Duplicate into two forms for merging and zeroing. |
| 571 | merging := f.Clone() |
| 572 | merging.Operands[idx].Action |= inst.R |
| 573 | |
| 574 | zeroing := f.Clone() |
| 575 | zeroing.Zeroing = true |
| 576 | |
| 577 | return []inst.Form{merging, zeroing} |
| 578 | } |
| 579 | |
| 580 | // findoperand looks for an operand type and returns its index, if found. |
| 581 | func findoperand(ops []inst.Operand, t string) (int, bool) { |
| 582 | for i, op := range ops { |
| 583 | if op.Type == t { |
| 584 | return i, true |
| 585 | } |
| 586 | } |
| 587 | return 0, false |
| 588 | } |
| 589 | |
| 590 | // enctype selects the encoding type for the instruction form. |
| 591 | func enctype(f opcodesxml.Form) inst.EncodingType { |
| 592 | switch { |
| 593 | case f.Encoding.EVEX != nil: |
| 594 | return inst.EncodingTypeEVEX |
| 595 | case f.Encoding.VEX != nil: |
| 596 | return inst.EncodingTypeVEX |
| 597 | case f.Encoding.REX != nil: |
| 598 | return inst.EncodingTypeREX |
| 599 | default: |
| 600 | return inst.EncodingTypeLegacy |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | // datasize (intelligently) guesses the datasize of an instruction form. |
| 605 | func datasize(f opcodesxml.Form) int { |
| 606 | // Determine from encoding bits. |
| 607 | e := f.Encoding |
| 608 | if e.VEX != nil && e.VEX.W == nil { |
| 609 | return 128 << e.VEX.L |
| 610 | } |
| 611 | |
| 612 | // Guess from operand types. |
| 613 | size := 0 |
| 614 | for _, op := range f.Operands { |
| 615 | s := operandsize(op) |
| 616 | if s != 0 && (size == 0 || op.Output) { |
| 617 | size = s |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return size |
| 622 | } |
| 623 | |
| 624 | func operandsize(op opcodesxml.Operand) int { |
| 625 | for s := 256; s >= 8; s /= 2 { |
| 626 | if strings.HasSuffix(op.Type, strconv.Itoa(s)) { |
| 627 | return s |
| 628 | } |
| 629 | } |
| 630 | return 0 |
| 631 | } |
| 632 | |
| 633 | // sizesuffix returns an optional size suffix to be added to the opcode name. |
| 634 | func sizesuffix(n string, f opcodesxml.Form) string { |
| 635 | // Reference: https://github.com/golang/arch/blob/5de9028c2478e6cb4e1c1b1f4386f3f0a93e383a/x86/x86avxgen/main.go#L275-L322 |
| 636 | // |
| 637 | // func addGoSuffixes(ctx *context) { |
| 638 | // var opcodeSuffixMatchers map[string][]string |
| 639 | // { |
| 640 | // opXY := []string{"VL=0", "X", "VL=1", "Y"} |
| 641 | // opXYZ := []string{"VL=0", "X", "VL=1", "Y", "VL=2", "Z"} |
| 642 | // opQ := []string{"REXW=1", "Q"} |
| 643 | // opLQ := []string{"REXW=0", "L", "REXW=1", "Q"} |
| 644 | // |
| 645 | // opcodeSuffixMatchers = map[string][]string{ |
| 646 | // "VCVTPD2DQ": opXY, |
| 647 | // "VCVTPD2PS": opXY, |
| 648 | // "VCVTTPD2DQ": opXY, |
| 649 | // "VCVTQQ2PS": opXY, |
| 650 | // "VCVTUQQ2PS": opXY, |
| 651 | // "VCVTPD2UDQ": opXY, |
| 652 | // "VCVTTPD2UDQ": opXY, |
| 653 | // |
| 654 | // "VFPCLASSPD": opXYZ, |
| 655 | // "VFPCLASSPS": opXYZ, |
| 656 | // |
| 657 | // "VCVTSD2SI": opQ, |
| 658 | // "VCVTTSD2SI": opQ, |
| 659 | // "VCVTTSS2SI": opQ, |
| 660 | // "VCVTSS2SI": opQ, |
| 661 | // |
| 662 | // "VCVTSD2USI": opLQ, |
| 663 | // "VCVTSS2USI": opLQ, |
| 664 | // "VCVTTSD2USI": opLQ, |
| 665 | // "VCVTTSS2USI": opLQ, |
| 666 | // "VCVTUSI2SD": opLQ, |
| 667 | // "VCVTUSI2SS": opLQ, |
| 668 | // "VCVTSI2SD": opLQ, |
| 669 | // "VCVTSI2SS": opLQ, |
| 670 | // "ANDN": opLQ, |
| 671 | // "BEXTR": opLQ, |
| 672 | // "BLSI": opLQ, |
| 673 | // "BLSMSK": opLQ, |
| 674 | // "BLSR": opLQ, |
| 675 | // "BZHI": opLQ, |
| 676 | // "MULX": opLQ, |
| 677 | // "PDEP": opLQ, |
| 678 | // "PEXT": opLQ, |
| 679 | // "RORX": opLQ, |
| 680 | // "SARX": opLQ, |
| 681 | // "SHLX": opLQ, |
| 682 | // "SHRX": opLQ, |
| 683 | // } |
| 684 | // } |
| 685 | // |
| 686 | |
| 687 | type rule struct { |
| 688 | Size func(opcodesxml.Form) int |
| 689 | Suffix map[int]string |
| 690 | } |
| 691 | |
| 692 | var ( |
| 693 | XY = rule{evexLLsize, map[int]string{128: "X", 256: "Y"}} |
| 694 | XYZ = rule{evexLLsize, map[int]string{128: "X", 256: "Y", 512: "Z"}} |
| 695 | Q = rule{rexWsize, map[int]string{64: "Q"}} |
| 696 | LQ = rule{rexWsize, map[int]string{32: "L", 64: "Q"}} |
| 697 | WLQ = rule{datasize, map[int]string{16: "W", 32: "L", 64: "Q"}} |
| 698 | ) |
| 699 | |
| 700 | rules := map[string]rule{ |
| 701 | "VCVTPD2DQ": XY, |
| 702 | "VCVTPD2PS": XY, |
| 703 | "VCVTTPD2DQ": XY, |
| 704 | "VCVTQQ2PS": XY, |
| 705 | "VCVTUQQ2PS": XY, |
| 706 | "VCVTPD2UDQ": XY, |
| 707 | "VCVTTPD2UDQ": XY, |
| 708 | |
| 709 | "VFPCLASSPD": XYZ, |
| 710 | "VFPCLASSPS": XYZ, |
| 711 | |
| 712 | "VCVTSD2SI": Q, |
| 713 | "VCVTTSD2SI": Q, |
| 714 | "VCVTTSS2SI": Q, |
| 715 | "VCVTSS2SI": Q, |
| 716 | |
| 717 | "VCVTSD2USI": LQ, |
| 718 | "VCVTSS2USI": LQ, |
| 719 | "VCVTTSD2USI": LQ, |
| 720 | "VCVTTSS2USI": LQ, |
| 721 | "VCVTUSI2SD": LQ, |
| 722 | "VCVTUSI2SS": LQ, |
| 723 | "VCVTSI2SD": LQ, |
| 724 | "VCVTSI2SS": LQ, |
| 725 | "ANDN": LQ, |
| 726 | "BEXTR": LQ, |
| 727 | "BLSI": LQ, |
| 728 | "BLSMSK": LQ, |
| 729 | "BLSR": LQ, |
| 730 | "BZHI": LQ, |
| 731 | "MULX": LQ, |
| 732 | "PDEP": LQ, |
| 733 | "PEXT": LQ, |
| 734 | "RORX": LQ, |
| 735 | "SARX": LQ, |
| 736 | "SHLX": LQ, |
| 737 | "SHRX": LQ, |
| 738 | |
| 739 | "RDRAND": LQ, |
| 740 | "RDSEED": LQ, |
| 741 | |
| 742 | // MOVEBE* instructions seem to be inconsistent with x86 CSV. |
| 743 | // |
| 744 | // Reference: https://github.com/golang/arch/blob/b19384d3c130858bb31a343ea8fce26be71b5998/x86/x86spec/format.go#L282-L287 |
| 745 | // |
| 746 | // "MOVBE r16, m16": "movbeww", |
| 747 | // "MOVBE m16, r16": "movbeww", |
| 748 | // "MOVBE m32, r32": "movbell", |
| 749 | // "MOVBE r32, m32": "movbell", |
| 750 | // "MOVBE m64, r64": "movbeqq", |
| 751 | // "MOVBE r64, m64": "movbeqq", |
| 752 | // |
| 753 | "MOVBEW": WLQ, |
| 754 | "MOVBEL": WLQ, |
| 755 | "MOVBEQ": WLQ, |
| 756 | } |
| 757 | |
| 758 | r, ok := rules[n] |
| 759 | if !ok { |
| 760 | return "" |
| 761 | } |
| 762 | |
| 763 | s := r.Size(f) |
| 764 | return r.Suffix[s] |
| 765 | } |
| 766 | |
| 767 | func rexWsize(f opcodesxml.Form) int { |
| 768 | e := f.Encoding |
| 769 | switch { |
| 770 | case e.EVEX != nil && e.EVEX.W != nil: |
| 771 | return 32 << *e.EVEX.W |
| 772 | default: |
| 773 | return 32 |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | func evexLLsize(f opcodesxml.Form) int { |
| 778 | e := f.Encoding |
| 779 | if e.EVEX == nil { |
| 780 | return 0 |
| 781 | } |
| 782 | size := map[string]int{"00": 128, "01": 256, "10": 512} |
| 783 | return size[e.EVEX.LL] |
| 784 | } |
| 785 | |
| 786 | // vexevex fixes instructions that have both VEX and EVEX encoded forms with the |
| 787 | // same operand types. Go uses the VEX encoded form unless EVEX-only features |
| 788 | // are used. This function will only keep the VEX encoded version in the case |
| 789 | // where both exist. |
| 790 | // |
| 791 | // Note this is somewhat of a hack. There are real reasons to use the EVEX |
| 792 | // encoded version even when both exist. The main reason to use the EVEX version |
| 793 | // rather than VEX is to use the registers Z16, Z17, ... and up. However, avo |
| 794 | // does not implement the logic to distinguish between the two halfs of the |
| 795 | // vector registers. So in its current state the only reason to need the EVEX |
| 796 | // version is to encode suffixes, and these are represented by other instruction |
| 797 | // forms. |
| 798 | // |
| 799 | // TODO(mbm): restrict use of vector registers https://github.com/mmcloughlin/avo/issues/146 |
| 800 | func vexevex(fs []inst.Form) ([]inst.Form, error) { |
| 801 | // Group forms by deduping ID. |
| 802 | byid := map[string][]inst.Form{} |
| 803 | for _, f := range fs { |
| 804 | id := fmt.Sprintf( |
| 805 | "%s {%t,%t,%t,%t}", |
| 806 | strings.Join(f.Signature(), "_"), |
| 807 | f.Zeroing, |
| 808 | f.EmbeddedRounding, |
| 809 | f.SuppressAllExceptions, |
| 810 | f.Broadcast, |
| 811 | ) |
| 812 | byid[id] = append(byid[id], f) |
| 813 | } |
| 814 | |
| 815 | // Resolve overlaps. |
| 816 | var results []inst.Form |
| 817 | for id, group := range byid { |
| 818 | if len(group) < 2 { |
| 819 | results = append(results, group...) |
| 820 | continue |
| 821 | } |
| 822 | |
| 823 | // We expect these conflicts are caused by VEX/EVEX pairs. Bail if it's |
| 824 | // something else. |
| 825 | if len(group) > 2 { |
| 826 | return nil, fmt.Errorf("more than two forms of type %q", id) |
| 827 | } |
| 828 | |
| 829 | if group[0].EncodingType == inst.EncodingTypeEVEX { |
| 830 | group[0], group[1] = group[1], group[0] |
| 831 | } |
| 832 | |
| 833 | if group[0].EncodingType != inst.EncodingTypeVEX || group[1].EncodingType != inst.EncodingTypeEVEX { |
| 834 | return nil, errors.New("expected pair of VEX/EVEX encoded forms") |
| 835 | } |
| 836 | |
| 837 | vex := group[0] |
| 838 | |
| 839 | // In this case we only keep the VEX encoded form. |
| 840 | results = append(results, vex) |
| 841 | } |
| 842 | |
| 843 | return results, nil |
| 844 | } |
| 845 | |
| 846 | // dedupe a list of forms. |
| 847 | func dedupe(fs []inst.Form) []inst.Form { |
| 848 | uniq := make([]inst.Form, 0, len(fs)) |
| 849 | for _, f := range fs { |
| 850 | have := false |
| 851 | for _, u := range uniq { |
| 852 | if reflect.DeepEqual(u, f) { |
| 853 | have = true |
| 854 | break |
| 855 | } |
| 856 | } |
| 857 | if !have { |
| 858 | uniq = append(uniq, f) |
| 859 | } |
| 860 | } |
| 861 | return uniq |
| 862 | } |
| 863 | |
| 864 | // sortforms sorts a list of forms. |
| 865 | func sortforms(fs []inst.Form) { |
| 866 | sort.Slice(fs, func(i, j int) bool { |
| 867 | return sortkey(fs[i]) < sortkey(fs[j]) |
| 868 | }) |
| 869 | } |
| 870 | |
| 871 | func sortkey(f inst.Form) string { |
| 872 | return fmt.Sprintf("%d %v %v", f.EncodingType, f.ISA, f) |
| 873 | } |