| 1 | package inst |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | //go:generate avogen -bootstrap -data ../data -output ztable.go godata |
| 9 | |
| 10 | // Lookup returns the instruction with the given opcode. Boolean return value |
| 11 | // indicates whether the instruction was found. |
| 12 | func Lookup(opcode string) (Instruction, bool) { |
| 13 | for _, i := range Instructions { |
| 14 | if i.Opcode == opcode { |
| 15 | return i, true |
| 16 | } |
| 17 | } |
| 18 | return Instruction{}, false |
| 19 | } |
| 20 | |
| 21 | // OperandTypes returns all the operand types that appear in the provided |
| 22 | // instructions. |
| 23 | func OperandTypes(is []Instruction) []string { |
| 24 | set := map[string]bool{} |
| 25 | for _, i := range is { |
| 26 | for _, f := range i.Forms { |
| 27 | for _, op := range f.Operands { |
| 28 | set[op.Type] = true |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | return sortedslice(set) |
| 33 | } |
| 34 | |
| 35 | // ImplicitRegisters returns all the registers that appear as implicit operands |
| 36 | // in the provided instructions. |
| 37 | func ImplicitRegisters(is []Instruction) []string { |
| 38 | set := map[string]bool{} |
| 39 | for _, i := range is { |
| 40 | for _, f := range i.Forms { |
| 41 | for _, op := range f.ImplicitOperands { |
| 42 | set[op.Register] = true |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | return sortedslice(set) |
| 47 | } |
| 48 | |
| 49 | // UniqueSuffixes returns all the non-empty suffixes that appear in the provided |
| 50 | // instructions. |
| 51 | func UniqueSuffixes(is []Instruction) []Suffix { |
| 52 | // Collect set. |
| 53 | set := map[Suffix]bool{} |
| 54 | for _, i := range is { |
| 55 | for _, f := range i.Forms { |
| 56 | for _, suffixes := range f.SupportedSuffixes() { |
| 57 | for _, suffix := range suffixes { |
| 58 | set[suffix] = true |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Convert to sorted slice. |
| 65 | suffixes := make([]Suffix, 0, len(set)) |
| 66 | for suffix := range set { |
| 67 | suffixes = append(suffixes, suffix) |
| 68 | } |
| 69 | |
| 70 | sort.Slice(suffixes, func(i, j int) bool { |
| 71 | return suffixes[i] < suffixes[j] |
| 72 | }) |
| 73 | |
| 74 | return suffixes |
| 75 | } |
| 76 | |
| 77 | // SuffixesClasses returns all possible classes of suffix combinations. |
| 78 | func SuffixesClasses(is []Instruction) map[string][]Suffixes { |
| 79 | classes := map[string][]Suffixes{} |
| 80 | for _, i := range is { |
| 81 | for _, f := range i.Forms { |
| 82 | class := f.SuffixesClass() |
| 83 | if _, ok := classes[class]; ok { |
| 84 | continue |
| 85 | } |
| 86 | classes[class] = f.SupportedSuffixes() |
| 87 | } |
| 88 | } |
| 89 | return classes |
| 90 | } |
| 91 | |
| 92 | // ISAs returns all the unique ISAs seen in the given instructions. |
| 93 | func ISAs(is []Instruction) []string { |
| 94 | set := map[string]bool{} |
| 95 | for _, i := range is { |
| 96 | for _, f := range i.Forms { |
| 97 | for _, isa := range f.ISA { |
| 98 | set[isa] = true |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | return sortedslice(set) |
| 103 | } |
| 104 | |
| 105 | // ISACombinations returns all the unique combinations of ISAs seen in the given |
| 106 | // instructions. |
| 107 | func ISACombinations(is []Instruction) [][]string { |
| 108 | var combinations [][]string |
| 109 | seen := map[string]bool{} |
| 110 | for _, i := range is { |
| 111 | for _, f := range i.Forms { |
| 112 | isas := append([]string(nil), f.ISA...) |
| 113 | sort.Strings(isas) |
| 114 | key := strings.Join(isas, ",") |
| 115 | |
| 116 | if !seen[key] { |
| 117 | combinations = append(combinations, isas) |
| 118 | seen[key] = true |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | return combinations |
| 123 | } |
| 124 | |
| 125 | // sortedslice builds a sorted slice of strings from a set. |
| 126 | func sortedslice(set map[string]bool) []string { |
| 127 | ss := make([]string, 0, len(set)) |
| 128 | for s := range set { |
| 129 | ss = append(ss, s) |
| 130 | } |
| 131 | |
| 132 | sort.Strings(ss) |
| 133 | |
| 134 | return ss |
| 135 | } |