| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"go/format" |
| 14 |
"log" |
| 15 |
"os" |
| 16 |
"regexp" |
| 17 |
"sort" |
| 18 |
"strings" |
| 19 |
|
| 20 |
"golang.org/x/arch/x86/xeddata" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
db *xeddata.Database |
| 25 |
xedPath string |
| 26 |
outFile string |
| 27 |
) |
| 28 |
|
| 29 |
func main() { |
| 30 |
log.SetPrefix("genavx: ") |
| 31 |
log.SetFlags(0) |
| 32 |
|
| 33 |
flag.StringVar(&xedPath, "xedPath", "", "XED datafiles location") |
| 34 |
flag.StringVar(&outFile, "o", "", "output file (stdout if empty)") |
| 35 |
flag.Parse() |
| 36 |
|
| 37 |
if xedPath == "" { |
| 38 |
xedPath = os.Getenv("XEDPATH") |
| 39 |
} |
| 40 |
if xedPath == "" { |
| 41 |
log.Fatalf("XEDPATH not set and -xedPath not provided") |
| 42 |
} |
| 43 |
|
| 44 |
var err error |
| 45 |
db, err = xeddata.NewDatabase(xedPath) |
| 46 |
if err != nil { |
| 47 |
log.Fatalf("open database: %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
generate() |
| 51 |
} |
| 52 |
|
| 53 |
type instruction struct { |
| 54 |
iclass string |
| 55 |
opcode string |
| 56 |
opbyte string |
| 57 |
opdigit string |
| 58 |
vexL string |
| 59 |
vexW string |
| 60 |
vexP string |
| 61 |
mapSelect string |
| 62 |
evex bool |
| 63 |
args []string |
| 64 |
ismem int |
| 65 |
dispScale int |
| 66 |
bcstScale int |
| 67 |
memBytes uint8 |
| 68 |
vsib bool |
| 69 |
} |
| 70 |
|
| 71 |
var registerArgs = map[string]string{ |
| 72 |
"GPR32_R()": "argGPR32_R", |
| 73 |
"GPR64_R()": "argGPR64_R", |
| 74 |
"VGPR32_R()": "argGPR32_R", |
| 75 |
"VGPR64_R()": "argGPR64_R", |
| 76 |
"VGPR32_N()": "argGPR32_N", |
| 77 |
"VGPR64_N()": "argGPR64_N", |
| 78 |
"GPR32_B()": "argGPR32_B", |
| 79 |
"GPR64_B()": "argGPR64_B", |
| 80 |
"VGPR32_B()": "argGPR32_B", |
| 81 |
"VGPR64_B()": "argGPR64_B", |
| 82 |
|
| 83 |
"XMM_R()": "argXmm_R", |
| 84 |
"XMM_R3()": "argXmmEvex_R", |
| 85 |
"XMM_N()": "argXmm_N", |
| 86 |
"XMM_N3()": "argXmmEvex_N", |
| 87 |
"XMM_B()": "argXmm_B", |
| 88 |
"XMM_B3()": "argXmmEvex_B", |
| 89 |
"XMM_SE()": "argXmm_SE", |
| 90 |
|
| 91 |
"YMM_R()": "argYmm_R", |
| 92 |
"YMM_R3()": "argYmmEvex_R", |
| 93 |
"YMM_N()": "argYmm_N", |
| 94 |
"YMM_N3()": "argYmmEvex_N", |
| 95 |
"YMM_B()": "argYmm_B", |
| 96 |
"YMM_B3()": "argYmmEvex_B", |
| 97 |
"YMM_SE()": "argYmm_SE", |
| 98 |
|
| 99 |
"ZMM_R3()": "argZmm_R", |
| 100 |
"ZMM_N3()": "argZmm_N", |
| 101 |
"ZMM_B3()": "argZmm_B", |
| 102 |
|
| 103 |
"MASK_R()": "argK_R", |
| 104 |
"MASK_N()": "argK_N", |
| 105 |
"MASK_B()": "argK_B", |
| 106 |
|
| 107 |
"MASKNOT0()": "argKnot0", |
| 108 |
} |
| 109 |
|
| 110 |
func generate() { |
| 111 |
var insts []*instruction |
| 112 |
|
| 113 |
err := xeddata.WalkInsts(xedPath, func(inst *xeddata.Inst) { |
| 114 |
inst.Pattern = xeddata.ExpandStates(db, inst.Pattern) |
| 115 |
pset := xeddata.NewPatternSet(inst.Pattern) |
| 116 |
|
| 117 |
if inst.HasAttribute("AMDONLY") || inst.Extension == "XOP" { |
| 118 |
return |
| 119 |
} |
| 120 |
if !pset.Is("VEX") && !pset.Is("EVEX") { |
| 121 |
return |
| 122 |
} |
| 123 |
if !strings.HasPrefix(inst.Iclass, "V") && !strings.HasPrefix(inst.Iclass, "K") { |
| 124 |
|
| 125 |
return |
| 126 |
} |
| 127 |
if inst.RealOpcode == "N" { |
| 128 |
return |
| 129 |
} |
| 130 |
|
| 131 |
dec := &instruction{ |
| 132 |
iclass: inst.Iclass, |
| 133 |
evex: pset.Is("EVEX"), |
| 134 |
vsib: strings.Contains(inst.Iclass, "GATHER") || strings.Contains(inst.Iclass, "SCATTER"), |
| 135 |
} |
| 136 |
|
| 137 |
dec.opdigit = findOpdigit(pset) |
| 138 |
dec.opbyte = findOpbyte(pset) |
| 139 |
|
| 140 |
|
| 141 |
for _, f := range strings.Fields(inst.Operands) { |
| 142 |
xarg, err := xeddata.NewOperand(db, f) |
| 143 |
if err != nil { |
| 144 |
continue |
| 145 |
} |
| 146 |
if xarg.Action == "" || !xarg.IsVisible() { |
| 147 |
continue |
| 148 |
} |
| 149 |
|
| 150 |
name := xarg.NameLHS() |
| 151 |
switch name { |
| 152 |
case "IMM0": |
| 153 |
if pset["IMM0SIGNED=1"] { |
| 154 |
dec.args = append(dec.args, "argImm8") |
| 155 |
} else { |
| 156 |
dec.args = append(dec.args, "argImm8u") |
| 157 |
} |
| 158 |
case "REG0", "REG1", "REG2", "REG3": |
| 159 |
rhs := xarg.NameRHS() |
| 160 |
if rhs == "MASK1()" { |
| 161 |
dec.args = append(dec.args, "argKmask") |
| 162 |
continue |
| 163 |
} |
| 164 |
arg := registerArgs[rhs] |
| 165 |
if arg == "" { |
| 166 |
log.Printf("unknown reg: %s", rhs) |
| 167 |
return |
| 168 |
} |
| 169 |
dec.args = append(dec.args, arg) |
| 170 |
case "MEM0": |
| 171 |
dec.args = append(dec.args, "argM") |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
dec.vexP = pset.Match( |
| 176 |
"VEX_PREFIX=1", "1", |
| 177 |
"VEX_PREFIX=2", "2", |
| 178 |
"VEX_PREFIX=3", "3") |
| 179 |
if dec.vexP == "" { |
| 180 |
dec.vexP = "0" |
| 181 |
} |
| 182 |
|
| 183 |
dec.mapSelect = pset.Match( |
| 184 |
"MAP=1", "1", |
| 185 |
"MAP=2", "2", |
| 186 |
"MAP=3", "3") |
| 187 |
|
| 188 |
dec.vexL = pset.Match( |
| 189 |
"VL=0", "0", |
| 190 |
"VL=1", "1", |
| 191 |
"VL=2", "2") |
| 192 |
if dec.vexL == "" { |
| 193 |
dec.vexL = "0" |
| 194 |
} |
| 195 |
|
| 196 |
dec.vexW = pset.Match( |
| 197 |
"REXW=1", "1") |
| 198 |
if dec.vexW == "" { |
| 199 |
dec.vexW = "0" |
| 200 |
} |
| 201 |
|
| 202 |
dec.ismem = -1 |
| 203 |
if pset.Is("MemOnly") { |
| 204 |
dec.ismem = 1 |
| 205 |
} else if pset.Is("RegOnly") { |
| 206 |
dec.ismem = 0 |
| 207 |
} |
| 208 |
|
| 209 |
if strings.Contains(inst.Attributes, "DISP8_") { |
| 210 |
dec.dispScale = evexScale(pset) |
| 211 |
dec.bcstScale = evexBcstScale(pset, inst) |
| 212 |
} |
| 213 |
|
| 214 |
for _, op := range strings.Fields(inst.Operands) { |
| 215 |
if strings.HasPrefix(op, "MEM") { |
| 216 |
parts := strings.Split(op, ":") |
| 217 |
if len(parts) >= 3 { |
| 218 |
wcode := parts[2] |
| 219 |
sizeStr := db.WidthSize(wcode, xeddata.OpSize64) |
| 220 |
if sizeStr != "" { |
| 221 |
var size int |
| 222 |
fmt.Sscanf(sizeStr, "%d", &size) |
| 223 |
if strings.HasSuffix(sizeStr, "bits") { |
| 224 |
size /= 8 |
| 225 |
} |
| 226 |
dec.memBytes = uint8(size) |
| 227 |
} |
| 228 |
} |
| 229 |
break |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
insts = append(insts, dec) |
| 234 |
}) |
| 235 |
if err != nil { |
| 236 |
log.Fatalf("walk: %v", err) |
| 237 |
} |
| 238 |
|
| 239 |
printTables(outFile, insts) |
| 240 |
} |
| 241 |
|
| 242 |
func findOpdigit(pset xeddata.PatternSet) string { |
| 243 |
reg := pset.Index( |
| 244 |
"REG[0b000]", "REG[0b001]", "REG[0b010]", "REG[0b011]", |
| 245 |
"REG[0b100]", "REG[0b101]", "REG[0b110]", "REG[0b111]", |
| 246 |
) |
| 247 |
if reg != -1 { |
| 248 |
return fmt.Sprintf("%d", reg) |
| 249 |
} |
| 250 |
return "" |
| 251 |
} |
| 252 |
|
| 253 |
var opbyteRE = regexp.MustCompile(`0x[0-9A-F]{2}`) |
| 254 |
|
| 255 |
func findOpbyte(pset xeddata.PatternSet) string { |
| 256 |
for k := range pset { |
| 257 |
if opbyteRE.MatchString(k) { |
| 258 |
return k |
| 259 |
} |
| 260 |
} |
| 261 |
return "" |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
func evexScale(pset xeddata.PatternSet) int { |
| 270 |
scaleStr := "" |
| 271 |
switch { |
| 272 |
case pset["NELEM_FULL()"], pset["NELEM_FULLMEM()"]: |
| 273 |
scaleStr = pset.Match("VL=0", "N16", "VL=1", "N32", "VL=2", "N64") |
| 274 |
case pset["NELEM_MOVDDUP()"]: |
| 275 |
scaleStr = pset.Match("VL=0", "N8", "VL=1", "N32", "VL=2", "N64") |
| 276 |
case pset["NELEM_HALF()"], pset["NELEM_HALFMEM()"]: |
| 277 |
scaleStr = pset.Match("VL=0", "N8", "VL=1", "N16", "VL=2", "N32") |
| 278 |
case pset["NELEM_QUARTERMEM()"]: |
| 279 |
scaleStr = pset.Match("VL=0", "N4", "VL=1", "N8", "VL=2", "N16") |
| 280 |
case pset["NELEM_EIGHTHMEM()"]: |
| 281 |
scaleStr = pset.Match("VL=0", "N2", "VL=1", "N4", "VL=2", "N8") |
| 282 |
case pset["NELEM_TUPLE2()"]: |
| 283 |
scaleStr = pset.Match("ESIZE_32_BITS()", "N8", "ESIZE_64_BITS()", "N16") |
| 284 |
case pset["NELEM_TUPLE4()"]: |
| 285 |
scaleStr = pset.Match("ESIZE_32_BITS()", "N16", "ESIZE_64_BITS()", "N32") |
| 286 |
case pset["NELEM_TUPLE8()"]: |
| 287 |
scaleStr = "N32" |
| 288 |
case pset["NELEM_MEM128()"], pset["NELEM_TUPLE1_4X()"]: |
| 289 |
scaleStr = "N16" |
| 290 |
} |
| 291 |
|
| 292 |
if scaleStr == "" { |
| 293 |
scalars := [...]string{ |
| 294 |
"NELEM_SCALAR()", "NELEM_GSCAT()", "NELEM_GPR_READER()", |
| 295 |
"NELEM_GPR_READER_BYTE()", "NELEM_GPR_READER_WORD()", |
| 296 |
"NELEM_GPR_WRITER_STORE()", "NELEM_GPR_WRITER_STORE_BYTE()", |
| 297 |
"NELEM_GPR_WRITER_STORE_WORD()", "NELEM_GPR_WRITER_LDOP_D()", |
| 298 |
"NELEM_GPR_WRITER_LDOP_Q()", "NELEM_TUPLE1()", "NELEM_TUPLE1_BYTE()", |
| 299 |
"NELEM_TUPLE1_WORD()", "NELEM_ONE()", |
| 300 |
} |
| 301 |
for _, scalar := range scalars { |
| 302 |
if pset[scalar] { |
| 303 |
scaleStr = pset.Match( |
| 304 |
"ESIZE_8_BITS()", "N1", |
| 305 |
"ESIZE_16_BITS()", "N2", |
| 306 |
"ESIZE_32_BITS()", "N4", |
| 307 |
"ESIZE_64_BITS()", "N8") |
| 308 |
break |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
switch scaleStr { |
| 314 |
case "N1": |
| 315 |
return 1 |
| 316 |
case "N2": |
| 317 |
return 2 |
| 318 |
case "N4": |
| 319 |
return 4 |
| 320 |
case "N8": |
| 321 |
return 8 |
| 322 |
case "N16": |
| 323 |
return 16 |
| 324 |
case "N32": |
| 325 |
return 32 |
| 326 |
case "N64": |
| 327 |
return 64 |
| 328 |
} |
| 329 |
return 0 |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
func evexBcstScale(pset xeddata.PatternSet, inst *xeddata.Inst) int { |
| 339 |
scaleStr := "" |
| 340 |
switch { |
| 341 |
case pset["NELEM_FULL()"]: |
| 342 |
scaleStr = pset.Match( |
| 343 |
"ESIZE_32_BITS()", "BcstN4", |
| 344 |
"ESIZE_64_BITS()", "BcstN8") |
| 345 |
case pset["NELEM_HALF()"]: |
| 346 |
scaleStr = "BcstN4" |
| 347 |
default: |
| 348 |
if inst.HasAttribute("BROADCAST_ENABLED") { |
| 349 |
scaleStr = pset.Match( |
| 350 |
"ESIZE_32_BITS()", "BcstN4", |
| 351 |
"ESIZE_64_BITS()", "BcstN8") |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
switch scaleStr { |
| 356 |
case "BcstN4": |
| 357 |
return 4 |
| 358 |
case "BcstN8": |
| 359 |
return 8 |
| 360 |
} |
| 361 |
return 0 |
| 362 |
} |
| 363 |
|
| 364 |
func printTables(outFile string, insts []*instruction) { |
| 365 |
|
| 366 |
|
| 367 |
groups := make(map[string]map[uint8][]*instruction) |
| 368 |
groups["1"] = make(map[uint8][]*instruction) |
| 369 |
groups["2"] = make(map[uint8][]*instruction) |
| 370 |
groups["3"] = make(map[uint8][]*instruction) |
| 371 |
|
| 372 |
for _, inst := range insts { |
| 373 |
if inst.opbyte == "" { |
| 374 |
continue |
| 375 |
} |
| 376 |
var opbyte uint8 |
| 377 |
fmt.Sscanf(inst.opbyte, "0x%02X", &opbyte) |
| 378 |
m := inst.mapSelect |
| 379 |
if m == "" { |
| 380 |
log.Printf("missing map for %s", inst.iclass) |
| 381 |
continue |
| 382 |
} |
| 383 |
groups[m][opbyte] = append(groups[m][opbyte], inst) |
| 384 |
} |
| 385 |
|
| 386 |
var buf bytes.Buffer |
| 387 |
fmt.Fprintf(&buf, `// Code generated by genavx. DO NOT EDIT. |
| 388 |
|
| 389 |
package x86asm |
| 390 |
|
| 391 |
`) |
| 392 |
|
| 393 |
opsMap := make(map[string]bool) |
| 394 |
for _, inst := range insts { |
| 395 |
opsMap[inst.iclass] = true |
| 396 |
} |
| 397 |
var ops []string |
| 398 |
for op := range opsMap { |
| 399 |
ops = append(ops, op) |
| 400 |
} |
| 401 |
sort.Strings(ops) |
| 402 |
|
| 403 |
fmt.Fprintf(&buf, "const (\n") |
| 404 |
fmt.Fprintf(&buf, "\t_ Op = iota + maxNonAVXOp\n") |
| 405 |
for _, op := range ops { |
| 406 |
fmt.Fprintf(&buf, "\t%s\n", op) |
| 407 |
} |
| 408 |
fmt.Fprintf(&buf, ")\n\n") |
| 409 |
|
| 410 |
fmt.Fprintf(&buf, "var avxOpNames = []string{\n") |
| 411 |
for _, op := range ops { |
| 412 |
fmt.Fprintf(&buf, "\t%s: %q,\n", op, op) |
| 413 |
} |
| 414 |
fmt.Fprintf(&buf, "}\n\n") |
| 415 |
|
| 416 |
if len(ops) > 0 { |
| 417 |
fmt.Fprintf(&buf, "const maxOp = %s\n\n", ops[len(ops)-1]) |
| 418 |
} |
| 419 |
|
| 420 |
vsibOps := make(map[string]bool) |
| 421 |
for _, inst := range insts { |
| 422 |
if inst.vsib { |
| 423 |
vsibOps[inst.iclass] = true |
| 424 |
} |
| 425 |
} |
| 426 |
var vsibOpsList []string |
| 427 |
for op := range vsibOps { |
| 428 |
vsibOpsList = append(vsibOpsList, op) |
| 429 |
} |
| 430 |
sort.Strings(vsibOpsList) |
| 431 |
|
| 432 |
fmt.Fprintf(&buf, "func isVSIB(op Op) bool {\n") |
| 433 |
fmt.Fprintf(&buf, "\tswitch op {\n") |
| 434 |
if len(vsibOpsList) > 0 { |
| 435 |
fmt.Fprintf(&buf, "\tcase ") |
| 436 |
for i, op := range vsibOpsList { |
| 437 |
if i > 0 { |
| 438 |
fmt.Fprintf(&buf, ", ") |
| 439 |
} |
| 440 |
fmt.Fprintf(&buf, "%s", op) |
| 441 |
} |
| 442 |
fmt.Fprintf(&buf, ":\n\t\treturn true\n") |
| 443 |
} |
| 444 |
fmt.Fprintf(&buf, "\t}\n") |
| 445 |
fmt.Fprintf(&buf, "\treturn false\n") |
| 446 |
fmt.Fprintf(&buf, "}\n\n") |
| 447 |
fmt.Fprintf(&buf, `type avxOptab struct { |
| 448 |
op Op |
| 449 |
args [6]argType |
| 450 |
vexP uint8 // 0=any, 1=none, 2=66, 3=F2, 4=F3 |
| 451 |
vexL uint8 // 0=128, 1=256, 2=512 |
| 452 |
vexW uint8 // 0=W0, 1=W1 |
| 453 |
opdigit int8 // -1 if none |
| 454 |
ismem int8 // -1 = any, 0 = reg, 1 = mem |
| 455 |
evex bool |
| 456 |
dispScale int8 |
| 457 |
bcstScale int8 |
| 458 |
memBytes uint8 |
| 459 |
vsib bool |
| 460 |
} |
| 461 |
|
| 462 |
`) |
| 463 |
|
| 464 |
emitTable(&buf, "avxMap0F", groups["1"]) |
| 465 |
emitTable(&buf, "avxMap0F38", groups["2"]) |
| 466 |
emitTable(&buf, "avxMap0F3A", groups["3"]) |
| 467 |
|
| 468 |
src, err := format.Source(buf.Bytes()) |
| 469 |
if err != nil { |
| 470 |
log.Fatalf("gofmt failed: %v\nsource:\n%s", err, buf.Bytes()) |
| 471 |
} |
| 472 |
|
| 473 |
w := os.Stdout |
| 474 |
if outFile != "" { |
| 475 |
f, err := os.Create(outFile) |
| 476 |
if err != nil { |
| 477 |
log.Fatalf("create: %v", err) |
| 478 |
} |
| 479 |
defer f.Close() |
| 480 |
w = f |
| 481 |
} |
| 482 |
if _, err := w.Write(src); err != nil { |
| 483 |
log.Fatalf("write: %v", err) |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
func emitTable(buf *bytes.Buffer, name string, m map[uint8][]*instruction) { |
| 488 |
fmt.Fprintf(buf, "var %s = [256][]*avxOptab{\n", name) |
| 489 |
for i := 0; i < 256; i++ { |
| 490 |
list := m[uint8(i)] |
| 491 |
if len(list) == 0 { |
| 492 |
continue |
| 493 |
} |
| 494 |
fmt.Fprintf(buf, "\t%d: {\n", i) |
| 495 |
for _, inst := range list { |
| 496 |
var fields []string |
| 497 |
fields = append(fields, fmt.Sprintf("op: %s", inst.iclass)) |
| 498 |
|
| 499 |
if len(inst.args) > 0 { |
| 500 |
fields = append(fields, fmt.Sprintf("args: [6]argType{%s}", strings.Join(inst.args, ", "))) |
| 501 |
} |
| 502 |
if inst.vexP != "0" { |
| 503 |
fields = append(fields, fmt.Sprintf("vexP: %s", inst.vexP)) |
| 504 |
} |
| 505 |
if inst.vexL != "0" { |
| 506 |
fields = append(fields, fmt.Sprintf("vexL: %s", inst.vexL)) |
| 507 |
} |
| 508 |
if inst.vexW != "0" { |
| 509 |
fields = append(fields, fmt.Sprintf("vexW: %s", inst.vexW)) |
| 510 |
} |
| 511 |
if inst.opdigit == "" { |
| 512 |
fields = append(fields, "opdigit: -1") |
| 513 |
} else if inst.opdigit != "0" { |
| 514 |
fields = append(fields, fmt.Sprintf("opdigit: %s", inst.opdigit)) |
| 515 |
} |
| 516 |
if inst.ismem != 0 { |
| 517 |
fields = append(fields, fmt.Sprintf("ismem: %d", inst.ismem)) |
| 518 |
} |
| 519 |
if inst.evex { |
| 520 |
fields = append(fields, "evex: true") |
| 521 |
} |
| 522 |
if inst.dispScale != 0 { |
| 523 |
fields = append(fields, fmt.Sprintf("dispScale: %d", inst.dispScale)) |
| 524 |
} |
| 525 |
if inst.bcstScale != 0 { |
| 526 |
fields = append(fields, fmt.Sprintf("bcstScale: %d", inst.bcstScale)) |
| 527 |
} |
| 528 |
if inst.memBytes != 0 { |
| 529 |
fields = append(fields, fmt.Sprintf("memBytes: %d", inst.memBytes)) |
| 530 |
} |
| 531 |
if inst.vsib { |
| 532 |
fields = append(fields, "vsib: true") |
| 533 |
} |
| 534 |
|
| 535 |
fmt.Fprintf(buf, "\t\t{%s},\n", strings.Join(fields, ", ")) |
| 536 |
} |
| 537 |
fmt.Fprintf(buf, "\t},\n") |
| 538 |
} |
| 539 |
fmt.Fprintf(buf, "}\n\n") |
| 540 |
} |