| 1 | package opcodescsv |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "golang.org/x/arch/x86/x86csv" |
| 9 | ) |
| 10 | |
| 11 | // Alias records another possible name for an instruction configuration. |
| 12 | type Alias struct { |
| 13 | Opcode string |
| 14 | DataSize int |
| 15 | NumOperands int |
| 16 | } |
| 17 | |
| 18 | // BuildAliasMap constructs a map from AT&T/GNU/Intel to Go syntax. |
| 19 | func BuildAliasMap(is []*x86csv.Inst) (map[Alias]string, error) { |
| 20 | m := map[Alias]string{} |
| 21 | for _, i := range is { |
| 22 | if skip(i) { |
| 23 | continue |
| 24 | } |
| 25 | |
| 26 | s, err := strconv.Atoi("0" + i.DataSize) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | for _, alt := range []string{i.IntelOpcode(), i.GNUOpcode()} { |
| 32 | if strings.ToUpper(alt) != i.GoOpcode() { |
| 33 | a := Alias{ |
| 34 | Opcode: strings.ToLower(alt), |
| 35 | DataSize: s, |
| 36 | NumOperands: len(i.GoArgs()), |
| 37 | } |
| 38 | m[a] = i.GoOpcode() |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | return m, nil |
| 43 | } |
| 44 | |
| 45 | // OperandOrder describes the order an instruction takes its operands. |
| 46 | type OperandOrder uint8 |
| 47 | |
| 48 | // Possible operand orders. |
| 49 | const ( |
| 50 | UnknownOrder = iota |
| 51 | IntelOrder |
| 52 | ReverseIntelOrder |
| 53 | CMP3Order |
| 54 | ) |
| 55 | |
| 56 | // BuildOrderMap collects operand order information from the instruction list. |
| 57 | func BuildOrderMap(is []*x86csv.Inst) map[string]OperandOrder { |
| 58 | s := map[string]OperandOrder{} |
| 59 | for _, i := range is { |
| 60 | if skip(i) { |
| 61 | continue |
| 62 | } |
| 63 | s[i.GoOpcode()] = order(i) |
| 64 | } |
| 65 | return s |
| 66 | } |
| 67 | |
| 68 | // order categorizes the operand order of an instruction. |
| 69 | func order(i *x86csv.Inst) OperandOrder { |
| 70 | // Is it Intel order already? |
| 71 | intel := i.IntelArgs() |
| 72 | if reflect.DeepEqual(i.GoArgs(), intel) { |
| 73 | return IntelOrder |
| 74 | } |
| 75 | |
| 76 | // Check if it's reverse Intel. |
| 77 | for l, r := 0, len(intel)-1; l < r; l, r = l+1, r-1 { |
| 78 | intel[l], intel[r] = intel[r], intel[l] |
| 79 | } |
| 80 | if reflect.DeepEqual(i.GoArgs(), intel) { |
| 81 | return ReverseIntelOrder |
| 82 | } |
| 83 | |
| 84 | // Otherwise we could be in the bizarre special-case of 3-argument CMP instructions. |
| 85 | // |
| 86 | // Reference: https://github.com/golang/arch/blob/b19384d3c130858bb31a343ea8fce26be71b5998/x86/x86spec/format.go#L138-L144 |
| 87 | // |
| 88 | // case "CMPPD", "CMPPS", "CMPSD", "CMPSS": |
| 89 | // // rotate destination to end but don't swap comparison operands |
| 90 | // if len(args) == 3 { |
| 91 | // args[0], args[1], args[2] = args[2], args[0], args[1] |
| 92 | // break |
| 93 | // } |
| 94 | // fallthrough |
| 95 | // |
| 96 | switch i.GoOpcode() { |
| 97 | case "CMPPD", "CMPPS", "CMPSD", "CMPSS": |
| 98 | if len(i.GoArgs()) == 3 { |
| 99 | return CMP3Order |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return UnknownOrder |
| 104 | } |
| 105 | |
| 106 | // skip decides whether to ignore the instruction for analysis purposes. |
| 107 | func skip(i *x86csv.Inst) bool { |
| 108 | switch { |
| 109 | case strings.ContainsAny(i.GoOpcode(), "/*"): |
| 110 | return true |
| 111 | case i.Mode64 == "I": // Invalid in 64-bit mode. |
| 112 | return true |
| 113 | } |
| 114 | return false |
| 115 | } |