| |
| |
| |
|
|
| package main |
|
|
| import ( |
| "fmt" |
| "log" |
| "maps" |
| "reflect" |
| "regexp" |
| "slices" |
| "strconv" |
| "strings" |
|
|
| "simd/archsimd/_gen/unify" |
|
|
| "golang.org/x/arch/x86/xeddata" |
| "gopkg.in/yaml.v3" |
| ) |
|
|
| const ( |
| NOT_REG_CLASS = iota |
| VREG_CLASS |
| GREG_CLASS |
| ) |
|
|
| |
| |
| type instVariant uint8 |
|
|
| const ( |
| instVariantNone instVariant = 0 |
|
|
| |
| |
| instVariantMasked instVariant = 1 << iota |
| ) |
|
|
| var operandRemarks int |
|
|
| |
| func loadXED(xedPath string) []*unify.Value { |
| |
|
|
| db, err := xeddata.NewDatabase(xedPath) |
| if err != nil { |
| log.Fatalf("open database: %v", err) |
| } |
|
|
| var defs []*unify.Value |
| type opData struct { |
| inst *xeddata.Inst |
| ops []operand |
| mem string |
| } |
| |
| memOps := make(map[string][]opData, 0) |
| otherOps := make(map[string][]opData, 0) |
| appendDefs := func(inst *xeddata.Inst, ops []operand, addFields map[string]string) { |
| applyQuirks(inst, ops) |
|
|
| defsPos := len(defs) |
| defs = append(defs, instToUVal(inst, ops, addFields)...) |
|
|
| if *flagDebugXED { |
| for i := defsPos; i < len(defs); i++ { |
| y, _ := yaml.Marshal(defs[i]) |
| fmt.Printf("==>\n%s\n", y) |
| } |
| } |
| } |
| err = xeddata.WalkInsts(xedPath, func(inst *xeddata.Inst) { |
| inst.Pattern = xeddata.ExpandStates(db, inst.Pattern) |
|
|
| switch { |
| case inst.RealOpcode == "N": |
| return |
| case !(strings.HasPrefix(inst.Extension, "AVX") || strings.HasPrefix(inst.Extension, "SHA") || |
| inst.Extension == "FMA" || inst.Extension == "VAES"): |
| |
| return |
| } |
|
|
| if *flagDebugXED { |
| fmt.Printf("%s:\n%+v\n", inst.Pos, inst) |
| } |
|
|
| ops, err := decodeOperands(db, strings.Fields(inst.Operands)) |
| if err != nil { |
| operandRemarks++ |
| if *Verbose { |
| log.Printf("%s: [%s] %s", inst.Pos, inst.Opcode(), err) |
| } |
| return |
| } |
| var data map[string][]opData |
| mem := checkMem(ops) |
| if mem == "vbcst" { |
| |
| |
| data = memOps |
| } else { |
| data = otherOps |
| } |
| opcode := inst.Opcode() |
| if _, ok := data[opcode]; !ok { |
| s := make([]opData, 1) |
| s[0] = opData{inst, ops, mem} |
| data[opcode] = s |
| } else { |
| data[opcode] = append(data[opcode], opData{inst, ops, mem}) |
| } |
| }) |
| for _, s := range otherOps { |
| for _, o := range s { |
| addFields := map[string]string{} |
| if o.mem == "noMem" { |
| opcode := o.inst.Opcode() |
| |
| |
| |
| if ms, ok := memOps[opcode]; ok { |
| feat1, ok1 := decodeCPUFeature(o.inst) |
| |
| |
| var feat1Match, feat2Match string |
| matchIdx := -1 |
| var featMismatchCnt int |
| outer: |
| for i, m := range ms { |
| |
| var featMismatch bool |
| feat2, ok2 := decodeCPUFeature(m.inst) |
| if !ok1 || !ok2 { |
| continue |
| } |
| if feat1 != feat2 { |
| featMismatch = true |
| featMismatchCnt++ |
| } |
| if len(o.ops) == len(m.ops) { |
| for j := range o.ops { |
| if reflect.TypeOf(o.ops[j]) == reflect.TypeOf(m.ops[j]) { |
| v1, ok3 := o.ops[j].(operandVReg) |
| v2, _ := m.ops[j].(operandVReg) |
| if !ok3 { |
| continue |
| } |
| if v1.vecShape != v2.vecShape { |
| |
| continue outer |
| } |
| } else { |
| _, ok3 := o.ops[j].(operandVReg) |
| _, ok4 := m.ops[j].(operandMem) |
| |
| if !ok3 || !ok4 { |
| |
| continue outer |
| } |
| } |
| } |
| |
| matchIdx = i |
| feat1Match = feat1 |
| feat2Match = feat2 |
| if featMismatchCnt > 1 { |
| panic("multiple feature mismatch vbcst memops detected, simdgen failed to distinguish") |
| } |
| if !featMismatch { |
| |
| break |
| } |
| } |
| } |
| |
| if matchIdx != -1 { |
| memOps[opcode] = append(memOps[opcode][:matchIdx], memOps[opcode][matchIdx+1:]...) |
| |
| |
| addFields["memFeatures"] = "vbcst" |
| if feat1Match != feat2Match { |
| addFields["memFeaturesData"] = fmt.Sprintf("feat1=%s;feat2=%s", feat1Match, feat2Match) |
| } |
| } |
| } |
| } |
| appendDefs(o.inst, o.ops, addFields) |
| } |
| } |
| for _, ms := range memOps { |
| for _, m := range ms { |
| if *Verbose { |
| log.Printf("mem op not merged: %s, %v\n", m.inst.Opcode(), m) |
| } |
| appendDefs(m.inst, m.ops, nil) |
| } |
| } |
| if err != nil { |
| log.Fatalf("walk insts: %v", err) |
| } |
|
|
| if len(unknownFeatures) > 0 { |
| if !*Verbose { |
| nInst := 0 |
| for _, insts := range unknownFeatures { |
| nInst += len(insts) |
| } |
| log.Printf("%d unhandled CPU features for %d instructions (use -v for details)", len(unknownFeatures), nInst) |
| } else { |
| keys := slices.Sorted(maps.Keys(unknownFeatures)) |
| for _, key := range keys { |
| log.Printf("unhandled ISASet %s", key) |
| log.Printf(" opcodes: %s", slices.Sorted(maps.Keys(unknownFeatures[key]))) |
| } |
| } |
| } |
|
|
| return defs |
| } |
|
|
| var ( |
| maskRequiredRe = regexp.MustCompile(`VPCOMPRESS[BWDQ]|VCOMPRESSP[SD]|VPEXPAND[BWDQ]|VEXPANDP[SD]`) |
| maskOptionalRe = regexp.MustCompile(`VPCMP(EQ|GT|U)?[BWDQ]|VCMPP[SD]`) |
| ) |
|
|
| func applyQuirks(inst *xeddata.Inst, ops []operand) { |
| opc := inst.Opcode() |
| switch { |
| case maskRequiredRe.MatchString(opc): |
| |
| |
| for i, op := range ops { |
| if op, ok := op.(operandMask); ok { |
| op.optional = false |
| ops[i] = op |
| } |
| } |
|
|
| case maskOptionalRe.MatchString(opc): |
| |
| for i, op := range ops { |
| if op, ok := op.(operandMask); ok && op.action.r { |
| op.optional = true |
| ops[i] = op |
| } |
| } |
| } |
| } |
|
|
| type operandCommon struct { |
| action operandAction |
| } |
|
|
| |
| |
| |
| type operandAction struct { |
| r bool |
| w bool |
| cr bool |
| cw bool |
| } |
|
|
| type operandMem struct { |
| operandCommon |
| vecShape |
| elemBaseType scalarBaseType |
| |
| |
| |
| vbcst bool |
| unknown bool |
| } |
|
|
| type vecShape struct { |
| elemBits int |
| bits int |
| fixedName string |
| } |
|
|
| type operandVReg struct { |
| operandCommon |
| vecShape |
| elemBaseType scalarBaseType |
| } |
|
|
| type operandGReg struct { |
| operandCommon |
| vecShape |
| elemBaseType scalarBaseType |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| type operandMask struct { |
| operandCommon |
| vecShape |
| |
|
|
| allMasks bool |
|
|
| |
| optional bool |
| } |
|
|
| type operandImm struct { |
| operandCommon |
| bits int |
| } |
|
|
| type operand interface { |
| common() operandCommon |
| addToDef(b *unify.DefBuilder) |
| } |
|
|
| func strVal(s any) *unify.Value { |
| return unify.NewValue(unify.NewStringExact(fmt.Sprint(s))) |
| } |
|
|
| func (o operandCommon) common() operandCommon { |
| return o |
| } |
|
|
| func (o operandMem) addToDef(b *unify.DefBuilder) { |
| b.Add("class", strVal("memory")) |
| if o.unknown { |
| return |
| } |
| baseDomain, err := unify.NewStringRegex(o.elemBaseType.regex()) |
| if err != nil { |
| panic("parsing baseRe: " + err.Error()) |
| } |
| b.Add("base", unify.NewValue(baseDomain)) |
| b.Add("bits", strVal(o.bits)) |
| if o.elemBits != o.bits { |
| b.Add("elemBits", strVal(o.elemBits)) |
| } |
| } |
|
|
| func (o operandVReg) addToDef(b *unify.DefBuilder) { |
| baseDomain, err := unify.NewStringRegex(o.elemBaseType.regex()) |
| if err != nil { |
| panic("parsing baseRe: " + err.Error()) |
| } |
| b.Add("class", strVal("vreg")) |
| b.Add("bits", strVal(o.bits)) |
| b.Add("base", unify.NewValue(baseDomain)) |
| |
| |
| if o.elemBits != o.bits { |
| b.Add("elemBits", strVal(o.elemBits)) |
| } |
| if o.fixedName != "" { |
| b.Add("fixedReg", strVal(o.fixedName)) |
| } |
| } |
|
|
| func (o operandGReg) addToDef(b *unify.DefBuilder) { |
| baseDomain, err := unify.NewStringRegex(o.elemBaseType.regex()) |
| if err != nil { |
| panic("parsing baseRe: " + err.Error()) |
| } |
| b.Add("class", strVal("greg")) |
| b.Add("bits", strVal(o.bits)) |
| b.Add("base", unify.NewValue(baseDomain)) |
| if o.elemBits != o.bits { |
| b.Add("elemBits", strVal(o.elemBits)) |
| } |
| if o.fixedName != "" { |
| b.Add("fixedReg", strVal(o.fixedName)) |
| } |
| } |
|
|
| func (o operandMask) addToDef(b *unify.DefBuilder) { |
| b.Add("class", strVal("mask")) |
| if o.allMasks { |
| |
| return |
| } |
| b.Add("elemBits", strVal(o.elemBits)) |
| b.Add("bits", strVal(o.bits)) |
| if o.fixedName != "" { |
| b.Add("fixedReg", strVal(o.fixedName)) |
| } |
| } |
|
|
| func (o operandImm) addToDef(b *unify.DefBuilder) { |
| b.Add("class", strVal("immediate")) |
| b.Add("bits", strVal(o.bits)) |
| } |
|
|
| var actionEncoding = map[string]operandAction{ |
| "r": {r: true}, |
| "cr": {r: true, cr: true}, |
| "w": {w: true}, |
| "cw": {w: true, cw: true}, |
| "rw": {r: true, w: true}, |
| "crw": {r: true, w: true, cr: true}, |
| "rcw": {r: true, w: true, cw: true}, |
| } |
|
|
| func decodeOperand(db *xeddata.Database, operand string) (operand, error) { |
| op, err := xeddata.NewOperand(db, operand) |
| if err != nil { |
| log.Fatalf("parsing operand %q: %v", operand, err) |
| } |
| if *flagDebugXED { |
| fmt.Printf(" %+v\n", op) |
| } |
|
|
| if strings.HasPrefix(op.Name, "EMX_BROADCAST") { |
| |
| |
| |
| |
| return nil, nil |
| } |
|
|
| |
| |
| action, ok := actionEncoding[op.Action] |
| if !ok { |
| return nil, fmt.Errorf("unknown action %q", op.Action) |
| } |
| common := operandCommon{action: action} |
|
|
| lhs := op.NameLHS() |
| if strings.HasPrefix(lhs, "MEM") { |
| |
| |
| if op.Width == "vv" && (op.Attributes["TXT=BCASTSTR"] || |
| op.Attributes["TXT=VPBROADCASTD"]) { |
| baseType, elemBits, ok := decodeType(op) |
| if !ok { |
| return nil, fmt.Errorf("failed to decode memory width %q", operand) |
| } |
| |
| |
| |
| |
| |
| shape := vecShape{elemBits: elemBits, bits: elemBits} |
| return operandMem{ |
| operandCommon: common, |
| vecShape: shape, |
| elemBaseType: baseType, |
| vbcst: true, |
| unknown: false, |
| }, nil |
| } |
| |
| |
| return operandMem{ |
| operandCommon: common, |
| unknown: true, |
| }, nil |
| } else if strings.HasPrefix(lhs, "REG") { |
| if op.Width == "mskw" { |
| |
| |
| |
| |
| return operandMask{ |
| operandCommon: common, |
| optional: op.Attributes["TXT=ZEROSTR"], |
| }, nil |
| } else { |
| class, regBits, fixedReg := decodeReg(op) |
| if class == NOT_REG_CLASS { |
| return nil, fmt.Errorf("failed to decode register %q", operand) |
| } |
| baseType, elemBits, ok := decodeType(op) |
| if !ok { |
| return nil, fmt.Errorf("failed to decode register width %q", operand) |
| } |
| shape := vecShape{elemBits: elemBits, bits: regBits, fixedName: fixedReg} |
| if class == VREG_CLASS { |
| return operandVReg{ |
| operandCommon: common, |
| vecShape: shape, |
| elemBaseType: baseType, |
| }, nil |
| } |
| |
| m := min(shape.bits, shape.elemBits) |
| shape.bits, shape.elemBits = m, m |
| return operandGReg{ |
| operandCommon: common, |
| vecShape: shape, |
| elemBaseType: baseType, |
| }, nil |
|
|
| } |
| } else if strings.HasPrefix(lhs, "IMM") { |
| _, bits, ok := decodeType(op) |
| if !ok { |
| return nil, fmt.Errorf("failed to decode register width %q", operand) |
| } |
| return operandImm{ |
| operandCommon: common, |
| bits: bits, |
| }, nil |
| } |
|
|
| |
| return nil, fmt.Errorf("unknown operand LHS %q in %q", lhs, operand) |
| } |
|
|
| func decodeOperands(db *xeddata.Database, operands []string) (ops []operand, err error) { |
| |
| for _, o := range operands { |
| op, err := decodeOperand(db, o) |
| if err != nil { |
| return nil, err |
| } |
| if op != nil { |
| ops = append(ops, op) |
| } |
| } |
|
|
| |
| |
| if err := inferMaskSizes(ops); err != nil { |
| return nil, fmt.Errorf("%w in operands %+v", err, operands) |
| } |
|
|
| return ops, nil |
| } |
|
|
| func inferMaskSizes(ops []operand) error { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| inferMask := func(r, w bool) error { |
| var masks []int |
| var rSizes, wSizes, sizes []vecShape |
| allMasks := true |
| hasWMask := false |
| for i, op := range ops { |
| action := op.common().action |
| if _, ok := op.(operandMask); ok { |
| if action.r && action.w { |
| return fmt.Errorf("unexpected rw mask") |
| } |
| if action.r == r || action.w == w { |
| masks = append(masks, i) |
| } |
| if action.w { |
| hasWMask = true |
| } |
| } else { |
| allMasks = false |
| if reg, ok := op.(operandVReg); ok { |
| if action.r { |
| rSizes = append(rSizes, reg.vecShape) |
| } |
| if action.w { |
| wSizes = append(wSizes, reg.vecShape) |
| } |
| } |
| } |
| } |
| if len(masks) == 0 { |
| return nil |
| } |
|
|
| if r { |
| sizes = rSizes |
| if len(sizes) == 0 { |
| sizes = wSizes |
| } |
| } |
| if w { |
| sizes = wSizes |
| if len(sizes) == 0 { |
| sizes = rSizes |
| } |
| } |
|
|
| if len(sizes) == 0 { |
| |
| if allMasks { |
| for _, i := range masks { |
| m := ops[i].(operandMask) |
| m.allMasks = true |
| ops[i] = m |
| } |
| return nil |
| } |
| return fmt.Errorf("cannot infer mask size: no register operands") |
| } |
| shape, ok := singular(sizes) |
| if !ok { |
| if !hasWMask && len(wSizes) == 1 && len(masks) == 1 { |
| |
| |
| shape = wSizes[0] |
| } else { |
| return fmt.Errorf("cannot infer mask size: multiple register sizes %v", sizes) |
| } |
| } |
| for _, i := range masks { |
| m := ops[i].(operandMask) |
| m.vecShape = shape |
| ops[i] = m |
| } |
| return nil |
| } |
| if err := inferMask(true, false); err != nil { |
| return err |
| } |
| if err := inferMask(false, true); err != nil { |
| return err |
| } |
| return nil |
| } |
|
|
| |
| |
| |
| |
| func addOperandsToDef(ops []operand, instDB *unify.DefBuilder, variant instVariant) { |
| var inVals, inVar, outVals []*unify.Value |
| asmPos := 0 |
| for _, op := range ops { |
| var db unify.DefBuilder |
| op.addToDef(&db) |
| db.Add("asmPos", unify.NewValue(unify.NewStringExact(fmt.Sprint(asmPos)))) |
|
|
| action := op.common().action |
| asmCount := 1 |
| if action.r { |
| inVal := unify.NewValue(db.Build()) |
| |
| if mask, ok := op.(operandMask); ok && mask.optional { |
| if variant&instVariantMasked != 0 { |
| inVar = append(inVar, inVal) |
| } else { |
| |
| asmCount = 0 |
| } |
| } else { |
| |
| inVals = append(inVals, inVal) |
| } |
| } |
| if action.w { |
| outVal := unify.NewValue(db.Build()) |
| outVals = append(outVals, outVal) |
| } |
|
|
| asmPos += asmCount |
| } |
|
|
| instDB.Add("in", unify.NewValue(unify.NewTuple(inVals...))) |
| instDB.Add("inVariant", unify.NewValue(unify.NewTuple(inVar...))) |
| instDB.Add("out", unify.NewValue(unify.NewTuple(outVals...))) |
| memFeatures := checkMem(ops) |
| if memFeatures != "noMem" { |
| instDB.Add("memFeatures", unify.NewValue(unify.NewStringExact(memFeatures))) |
| } |
| } |
|
|
| |
| |
| func checkMem(ops []operand) string { |
| memState := "noMem" |
| var mem *operandMem |
| memCnt := 0 |
| for _, op := range ops { |
| if m, ok := op.(operandMem); ok { |
| mem = &m |
| memCnt++ |
| } |
| } |
| if mem != nil { |
| if mem.unknown { |
| memState = "unknown" |
| } else if memCnt > 1 { |
| memState = "tooManyMem" |
| } else { |
| |
| |
| |
| |
| memState = "vbcst" |
| } |
| } |
| return memState |
| } |
|
|
| func instToUVal(inst *xeddata.Inst, ops []operand, addFields map[string]string) []*unify.Value { |
| feature, ok := decodeCPUFeature(inst) |
| if !ok { |
| return nil |
| } |
|
|
| var vals []*unify.Value |
| vals = append(vals, instToUVal1(inst, ops, feature, instVariantNone, addFields)) |
| if hasOptionalMask(ops) { |
| vals = append(vals, instToUVal1(inst, ops, feature, instVariantMasked, addFields)) |
| } |
| return vals |
| } |
|
|
| func instToUVal1(inst *xeddata.Inst, ops []operand, feature string, variant instVariant, addFields map[string]string) *unify.Value { |
| var db unify.DefBuilder |
| db.Add("goarch", unify.NewValue(unify.NewStringExact("amd64"))) |
| db.Add("asm", unify.NewValue(unify.NewStringExact(inst.Opcode()))) |
| addOperandsToDef(ops, &db, variant) |
| db.Add("cpuFeature", unify.NewValue(unify.NewStringExact(feature))) |
| for k, v := range addFields { |
| db.Add(k, unify.NewValue(unify.NewStringExact(v))) |
| } |
|
|
| if strings.Contains(inst.Pattern, "ZEROING=0") { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| db.Add("zeroing", unify.NewValue(unify.NewStringExact("false"))) |
| } |
| pos := unify.Pos{Path: inst.Pos.Path, Line: inst.Pos.Line} |
| return unify.NewValuePos(db.Build(), pos) |
| } |
|
|
| |
| |
| func decodeCPUFeature(inst *xeddata.Inst) (string, bool) { |
| isaSet := inst.ISASet |
| if isaSet == "" { |
| |
| |
| isaSet = inst.Extension |
| } |
| |
| |
| if strings.HasPrefix(isaSet, "AVX512") { |
| isaSet = isaSetVL.ReplaceAllLiteralString(isaSet, "") |
| } |
|
|
| feat, ok := cpuFeatureMap[isaSet] |
| if !ok { |
| imap := unknownFeatures[isaSet] |
| if imap == nil { |
| imap = make(map[string]struct{}) |
| unknownFeatures[isaSet] = imap |
| } |
| imap[inst.Opcode()] = struct{}{} |
| return "", false |
| } |
| if feat == "ignore" { |
| return "", false |
| } |
| return feat, true |
| } |
|
|
| var isaSetVL = regexp.MustCompile("_(128N?|256N?|512)$") |
|
|
| |
| |
| |
| |
| var cpuFeatureMap = map[string]string{ |
| "AVX": "AVX", |
| "AVX_VNNI": "AVXVNNI", |
| "AVX2": "AVX2", |
| "AVXAES": "AVXAES", |
| "SHA": "SHA", |
| "FMA": "FMA", |
| "VAES": "VAES", |
|
|
| |
| "AVX512F": "AVX512", |
| "AVX512BW": "AVX512", |
| "AVX512CD": "AVX512", |
| "AVX512DQ": "AVX512", |
| |
| |
|
|
| |
| "AVX512_BITALG": "AVX512BITALG", |
| "AVX512_GFNI": "AVX512GFNI", |
| "AVX512_VBMI": "AVX512VBMI", |
| "AVX512_VBMI2": "AVX512VBMI2", |
| "AVX512_VNNI": "AVX512VNNI", |
| "AVX512_VPOPCNTDQ": "AVX512VPOPCNTDQ", |
| "AVX512_VAES": "AVX512VAES", |
| "AVX512_VPCLMULQDQ": "AVX512VPCLMULQDQ", |
|
|
| |
| "AVX10_2_RC": "ignore", |
| } |
|
|
| func init() { |
| |
| |
| |
| var features = map[string]featureInfo{ |
| "AVX2": {Implies: []string{"AVX"}}, |
| "AVX512": {Implies: []string{"AVX2"}}, |
|
|
| "AVXAES": {Virtual: true, Implies: []string{"AVX", "AES"}}, |
| "FMA": {Implies: []string{"AVX"}}, |
| "VAES": {Implies: []string{"AVX"}}, |
|
|
| |
| "AVX512BITALG": {Implies: []string{"AVX512"}}, |
| "AVX512GFNI": {Implies: []string{"AVX512"}}, |
| "AVX512VBMI": {Implies: []string{"AVX512"}}, |
| "AVX512VBMI2": {Implies: []string{"AVX512"}}, |
| "AVX512VNNI": {Implies: []string{"AVX512"}}, |
| "AVX512VPOPCNTDQ": {Implies: []string{"AVX512"}}, |
| "AVX512VAES": {Implies: []string{"AVX512"}}, |
|
|
| |
| |
| |
| |
| "AVXVNNI": {Implies: []string{"AVX2"}}, |
| "AVXIFMA": {Implies: []string{"AVX2"}}, |
| } |
| registerFeatureInfo("amd64", goarchFeatures{ |
| featureVar: "X86", |
| features: features, |
| }) |
| } |
|
|
| var unknownFeatures = map[string]map[string]struct{}{} |
|
|
| |
| func hasOptionalMask(ops []operand) bool { |
| for _, op := range ops { |
| if op, ok := op.(operandMask); ok && op.optional { |
| return true |
| } |
| } |
| return false |
| } |
|
|
| func singular[T comparable](xs []T) (T, bool) { |
| if len(xs) == 0 { |
| return *new(T), false |
| } |
| for _, x := range xs[1:] { |
| if x != xs[0] { |
| return *new(T), false |
| } |
| } |
| return xs[0], true |
| } |
|
|
| type fixedReg struct { |
| class int |
| name string |
| width int |
| } |
|
|
| var fixedRegMap = map[string]fixedReg{ |
| "XED_REG_XMM0": {VREG_CLASS, "x0", 128}, |
| } |
|
|
| |
| |
| |
| func decodeReg(op *xeddata.Operand) (class, width int, name string) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| if !strings.HasPrefix(op.NameLHS(), "REG") { |
| return NOT_REG_CLASS, 0, "" |
| } |
| |
| |
| rhs := op.NameRHS() |
| if !strings.HasSuffix(rhs, "()") { |
| if fixedReg, ok := fixedRegMap[rhs]; ok { |
| return fixedReg.class, fixedReg.width, fixedReg.name |
| } |
| return NOT_REG_CLASS, 0, "" |
| } |
| switch { |
| case strings.HasPrefix(rhs, "XMM_"): |
| return VREG_CLASS, 128, "" |
| case strings.HasPrefix(rhs, "YMM_"): |
| return VREG_CLASS, 256, "" |
| case strings.HasPrefix(rhs, "ZMM_"): |
| return VREG_CLASS, 512, "" |
| case strings.HasPrefix(rhs, "GPR64_"), strings.HasPrefix(rhs, "VGPR64_"): |
| return GREG_CLASS, 64, "" |
| case strings.HasPrefix(rhs, "GPR32_"), strings.HasPrefix(rhs, "VGPR32_"): |
| return GREG_CLASS, 32, "" |
| } |
| return NOT_REG_CLASS, 0, "" |
| } |
|
|
| var xtypeRe = regexp.MustCompile(`^([iuf])([0-9]+)$`) |
|
|
| |
| |
| |
| type scalarBaseType int |
|
|
| const ( |
| scalarBaseInt scalarBaseType = iota |
| scalarBaseUint |
| scalarBaseIntOrUint |
| scalarBaseFloat |
| scalarBaseComplex |
| scalarBaseBFloat |
| scalarBaseHFloat |
| ) |
|
|
| func (s scalarBaseType) regex() string { |
| switch s { |
| case scalarBaseInt: |
| return "int" |
| case scalarBaseUint: |
| return "uint" |
| case scalarBaseIntOrUint: |
| return "int|uint" |
| case scalarBaseFloat: |
| return "float" |
| case scalarBaseComplex: |
| return "complex" |
| case scalarBaseBFloat: |
| return "BFloat" |
| case scalarBaseHFloat: |
| return "HFloat" |
| } |
| panic(fmt.Sprintf("unknown scalar base type %d", s)) |
| } |
|
|
| func decodeType(op *xeddata.Operand) (base scalarBaseType, bits int, ok bool) { |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| switch op.Xtype { |
| |
| |
| case "bf8": |
| return scalarBaseBFloat, 8, true |
| case "hf8": |
| return scalarBaseHFloat, 8, true |
| case "bf16": |
| return scalarBaseBFloat, 16, true |
| case "2f16": |
| |
| |
| return scalarBaseComplex, 32, true |
| case "2i8", "2I8": |
| |
| |
| return scalarBaseInt, 8, true |
| case "2u16", "2U16": |
| |
| |
| return scalarBaseUint, 16, true |
| case "2i16", "2I16": |
| |
| return scalarBaseInt, 16, true |
| case "4u8", "4U8": |
| |
| return scalarBaseUint, 8, true |
| case "4i8", "4I8": |
| |
| return scalarBaseInt, 8, true |
| } |
|
|
| |
| m := xtypeRe.FindStringSubmatch(op.Xtype) |
| if m == nil { |
| |
| return 0, 0, false |
| } |
| bits, _ = strconv.Atoi(m[2]) |
| switch m[1] { |
| case "i", "u": |
| |
| |
| |
| return scalarBaseIntOrUint, bits, true |
| case "f": |
| return scalarBaseFloat, bits, true |
| default: |
| panic("unreachable") |
| } |
| } |
|
|