| 1 | package inst |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // Instruction represents an x86 instruction. |
| 9 | type Instruction struct { |
| 10 | Opcode string // Golang assembly mnemonic |
| 11 | AliasOf string // Opcode of instruction that this is an alias for |
| 12 | Summary string // Description of the instruction |
| 13 | Forms // Accepted operand forms |
| 14 | } |
| 15 | |
| 16 | // IsTerminal reports whether the instruction exits a function. |
| 17 | func (i Instruction) IsTerminal() bool { |
| 18 | // TODO(mbm): how about the RETF* instructions |
| 19 | return i.Opcode == "RET" |
| 20 | } |
| 21 | |
| 22 | // IsBranch reports whether the instruction is a branch; that is, if it can |
| 23 | // cause control flow to jump to another location. |
| 24 | func (i Instruction) IsBranch() bool { |
| 25 | if i.Opcode == "CALL" { |
| 26 | return false |
| 27 | } |
| 28 | for _, f := range i.Forms { |
| 29 | for _, op := range f.Operands { |
| 30 | if strings.HasPrefix(op.Type, "rel") { |
| 31 | return true |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return false |
| 36 | } |
| 37 | |
| 38 | // IsConditionalBranch reports whether the instruction branches dependent on some condition. |
| 39 | func (i Instruction) IsConditionalBranch() bool { |
| 40 | return i.IsBranch() && i.Opcode != "JMP" |
| 41 | } |
| 42 | |
| 43 | // Clone the instruction. |
| 44 | func (i Instruction) Clone() Instruction { |
| 45 | c := i |
| 46 | c.Forms = i.Forms.Clone() |
| 47 | return c |
| 48 | } |
| 49 | |
| 50 | // Forms is a collection of instruction forms. |
| 51 | type Forms []Form |
| 52 | |
| 53 | // Arities returns the unique arities among the instruction forms. |
| 54 | func (fs Forms) Arities() []int { |
| 55 | s := map[int]bool{} |
| 56 | for _, f := range fs { |
| 57 | s[f.Arity()] = true |
| 58 | } |
| 59 | a := make([]int, 0, len(s)) |
| 60 | for n := range s { |
| 61 | a = append(a, n) |
| 62 | } |
| 63 | sort.Ints(a) |
| 64 | return a |
| 65 | } |
| 66 | |
| 67 | // Arity is a convenience for returning the unique instruction arity when you |
| 68 | // know it is not variadic. Panics for a variadic instruction. |
| 69 | func (fs Forms) Arity() int { |
| 70 | if fs.IsVariadic() { |
| 71 | panic("variadic") |
| 72 | } |
| 73 | a := fs.Arities() |
| 74 | return a[0] |
| 75 | } |
| 76 | |
| 77 | // IsVariadic reports whether the instruction has more than one arity. |
| 78 | func (fs Forms) IsVariadic() bool { |
| 79 | return len(fs.Arities()) > 1 |
| 80 | } |
| 81 | |
| 82 | // IsNiladic reports whether the instruction takes no operands. |
| 83 | func (fs Forms) IsNiladic() bool { |
| 84 | a := fs.Arities() |
| 85 | return len(a) == 1 && a[0] == 0 |
| 86 | } |
| 87 | |
| 88 | // Clone the instruction forms. |
| 89 | func (fs Forms) Clone() Forms { |
| 90 | cs := make(Forms, 0, len(fs)) |
| 91 | for _, f := range fs { |
| 92 | cs = append(cs, f.Clone()) |
| 93 | } |
| 94 | return cs |
| 95 | } |
| 96 | |
| 97 | // Form specifies one accepted set of operands for an instruction. |
| 98 | type Form struct { |
| 99 | // Instruction sets this instruction form requires. |
| 100 | ISA []string |
| 101 | |
| 102 | // Operands required for this form. |
| 103 | Operands []Operand |
| 104 | |
| 105 | // Registers read or written but not explicitly passed to the instruction. |
| 106 | ImplicitOperands []ImplicitOperand |
| 107 | |
| 108 | // Encoding type required for this instruction form. |
| 109 | EncodingType EncodingType |
| 110 | |
| 111 | // CancellingInputs indicates this instruction form has no dependency on the |
| 112 | // input operands when they refer to the same register. The classic example of |
| 113 | // this is "XORQ RAX, RAX", in which case the output has no dependence on the |
| 114 | // value of RAX. Instruction forms with cancelling inputs have only two input |
| 115 | // operands, which have the same register type. |
| 116 | CancellingInputs bool |
| 117 | |
| 118 | // Zeroing indicates whether the instruction form uses AVX-512 zeroing. This |
| 119 | // is the .Z suffix in Go, usually indicated with {z} operand suffix in |
| 120 | // Intel manuals. |
| 121 | Zeroing bool |
| 122 | |
| 123 | // EmbeddedRounding indicates whether the instruction form uses AVX-512 |
| 124 | // embedded rounding. This is the RN_SAE, RZ_SAE, RD_SAE and RU_SAE suffixes |
| 125 | // in Go, usually indicated with {er} in Intel manuals. |
| 126 | EmbeddedRounding bool |
| 127 | |
| 128 | // SuppressAllExceptions indicates whether the instruction form uses AVX-512 |
| 129 | // "suppress all exceptions". This is the SAE suffix in Go, usually |
| 130 | // indicated with {sae} in Intel manuals. |
| 131 | SuppressAllExceptions bool |
| 132 | |
| 133 | // Broadcast indicates whether the instruction form uses AVX-512 |
| 134 | // broadcast. This is the BCST suffix in Go, usually indicated by operand |
| 135 | // types like "m64bcst" in Intel manuals. |
| 136 | Broadcast bool |
| 137 | } |
| 138 | |
| 139 | // Arity returns the number of operands this form expects. |
| 140 | func (f Form) Arity() int { |
| 141 | return len(f.Operands) |
| 142 | } |
| 143 | |
| 144 | // Signature returns the list of operand types. |
| 145 | func (f Form) Signature() []string { |
| 146 | s := make([]string, f.Arity()) |
| 147 | for i, op := range f.Operands { |
| 148 | s[i] = op.Type |
| 149 | } |
| 150 | return s |
| 151 | } |
| 152 | |
| 153 | // Clone the instruction form. |
| 154 | func (f Form) Clone() Form { |
| 155 | c := f |
| 156 | c.ISA = append([]string(nil), f.ISA...) |
| 157 | c.Operands = append([]Operand(nil), f.Operands...) |
| 158 | c.ImplicitOperands = append([]ImplicitOperand(nil), f.ImplicitOperands...) |
| 159 | return c |
| 160 | } |
| 161 | |
| 162 | // AcceptsSuffixes reports whether this form takes any opcode suffixes. |
| 163 | func (f Form) AcceptsSuffixes() bool { |
| 164 | return f.Broadcast || f.EmbeddedRounding || f.SuppressAllExceptions || f.Zeroing |
| 165 | } |
| 166 | |
| 167 | // SuffixesClass returns a key representing the class of instruction suffixes it |
| 168 | // accepts. All instructions sharing a suffix class accept the same suffixes. |
| 169 | func (f Form) SuffixesClass() string { |
| 170 | if !f.AcceptsSuffixes() { |
| 171 | return "nil" |
| 172 | } |
| 173 | var parts []string |
| 174 | for _, flag := range []struct { |
| 175 | Name string |
| 176 | Enabled bool |
| 177 | }{ |
| 178 | {"er", f.EmbeddedRounding}, |
| 179 | {"sae", f.SuppressAllExceptions}, |
| 180 | {"bcst", f.Broadcast}, |
| 181 | {"z", f.Zeroing}, |
| 182 | } { |
| 183 | if flag.Enabled { |
| 184 | parts = append(parts, flag.Name) |
| 185 | } |
| 186 | } |
| 187 | return strings.Join(parts, "_") |
| 188 | } |
| 189 | |
| 190 | // SupportedSuffixes returns the list of all possible suffix combinations |
| 191 | // supported by this instruction form. |
| 192 | func (f Form) SupportedSuffixes() []Suffixes { |
| 193 | suffixes := []Suffixes{ |
| 194 | {}, |
| 195 | } |
| 196 | |
| 197 | add := func(ss ...Suffix) { |
| 198 | var exts []Suffixes |
| 199 | for _, s := range ss { |
| 200 | for _, suffix := range suffixes { |
| 201 | ext := append(Suffixes(nil), suffix...) |
| 202 | ext = append(ext, s) |
| 203 | exts = append(exts, ext) |
| 204 | } |
| 205 | } |
| 206 | suffixes = exts |
| 207 | } |
| 208 | |
| 209 | if f.Broadcast { |
| 210 | add(BCST) |
| 211 | } |
| 212 | |
| 213 | if f.EmbeddedRounding { |
| 214 | add(RN_SAE, RZ_SAE, RD_SAE, RU_SAE) |
| 215 | } |
| 216 | |
| 217 | if f.SuppressAllExceptions { |
| 218 | add(SAE) |
| 219 | } |
| 220 | |
| 221 | if f.Zeroing { |
| 222 | add(Z) |
| 223 | } |
| 224 | |
| 225 | return suffixes |
| 226 | } |
| 227 | |
| 228 | // Suffix is an opcode suffix. |
| 229 | type Suffix string |
| 230 | |
| 231 | // Supported opcode suffixes in x86 assembly. |
| 232 | const ( |
| 233 | BCST Suffix = "BCST" |
| 234 | RN_SAE Suffix = "RN_SAE" |
| 235 | RZ_SAE Suffix = "RZ_SAE" |
| 236 | RD_SAE Suffix = "RD_SAE" |
| 237 | RU_SAE Suffix = "RU_SAE" |
| 238 | SAE Suffix = "SAE" |
| 239 | Z Suffix = "Z" |
| 240 | ) |
| 241 | |
| 242 | func (s Suffix) String() string { |
| 243 | return string(s) |
| 244 | } |
| 245 | |
| 246 | // Summary of the opcode suffix, for documentation purposes. |
| 247 | func (s Suffix) Summary() string { |
| 248 | return suffixsummary[s] |
| 249 | } |
| 250 | |
| 251 | var suffixsummary = map[Suffix]string{ |
| 252 | BCST: "Broadcast", |
| 253 | RN_SAE: "Round Towards Nearest", |
| 254 | RZ_SAE: "Round Towards Zero", |
| 255 | RD_SAE: "Round Towards Negative Infinity", |
| 256 | RU_SAE: "Round Towards Positive Infinity", |
| 257 | SAE: "Suppress All Exceptions", |
| 258 | Z: "Zeroing Masking", |
| 259 | } |
| 260 | |
| 261 | // Suffixes is a list of opcode suffixes. |
| 262 | type Suffixes []Suffix |
| 263 | |
| 264 | // String returns the dot-separated suffixes. |
| 265 | func (s Suffixes) String() string { return s.Join(".") } |
| 266 | |
| 267 | // Join suffixes with the given separator. |
| 268 | func (s Suffixes) Join(sep string) string { |
| 269 | return strings.Join(s.Strings(), sep) |
| 270 | } |
| 271 | |
| 272 | // Strings returns the suffixes as strings. |
| 273 | func (s Suffixes) Strings() []string { |
| 274 | var ss []string |
| 275 | for _, suffix := range s { |
| 276 | ss = append(ss, suffix.String()) |
| 277 | } |
| 278 | return ss |
| 279 | } |
| 280 | |
| 281 | // Summaries returns all the suffix summaries. |
| 282 | func (s Suffixes) Summaries() []string { |
| 283 | var summaries []string |
| 284 | for _, suffix := range s { |
| 285 | summaries = append(summaries, suffix.Summary()) |
| 286 | } |
| 287 | return summaries |
| 288 | } |
| 289 | |
| 290 | // Operand is an operand to an instruction, describing the expected type and read/write action. |
| 291 | type Operand struct { |
| 292 | Type string |
| 293 | Action Action |
| 294 | } |
| 295 | |
| 296 | // ImplicitOperand describes a register that is implicitly read/written by an instruction. |
| 297 | type ImplicitOperand struct { |
| 298 | Register string |
| 299 | Action Action |
| 300 | } |
| 301 | |
| 302 | // Action specifies the read/write operation of an instruction on an operand. |
| 303 | type Action uint8 |
| 304 | |
| 305 | // Possible Action types. |
| 306 | const ( |
| 307 | R Action = 1 << iota // Read |
| 308 | W // Write |
| 309 | |
| 310 | RW Action = R | W // Read-Write |
| 311 | ) |
| 312 | |
| 313 | // ActionFromReadWrite builds an Action from boolean flags. |
| 314 | func ActionFromReadWrite(r, w bool) Action { |
| 315 | var a Action |
| 316 | if r { |
| 317 | a |= R |
| 318 | } |
| 319 | if w { |
| 320 | a |= W |
| 321 | } |
| 322 | return a |
| 323 | } |
| 324 | |
| 325 | // ContainsAll reports whether a supports all actions in s. |
| 326 | func (a Action) ContainsAll(s Action) bool { |
| 327 | return (a & s) == s |
| 328 | } |
| 329 | |
| 330 | // ContainsAny reports whether a supports any actions in s. |
| 331 | func (a Action) ContainsAny(s Action) bool { |
| 332 | return (a & s) != 0 |
| 333 | } |
| 334 | |
| 335 | // Read reports whether a supports read. |
| 336 | func (a Action) Read() bool { |
| 337 | return a.ContainsAll(R) |
| 338 | } |
| 339 | |
| 340 | // Write reports whether a supports write. |
| 341 | func (a Action) Write() bool { |
| 342 | return a.ContainsAll(W) |
| 343 | } |
| 344 | |
| 345 | // String represents a as a human-readable string. |
| 346 | func (a Action) String() string { |
| 347 | s := "" |
| 348 | if a.Read() { |
| 349 | s += "r" |
| 350 | } |
| 351 | if a.Write() { |
| 352 | s += "w" |
| 353 | } |
| 354 | return s |
| 355 | } |
| 356 | |
| 357 | // EncodingType specifies a category of encoding types. |
| 358 | type EncodingType uint8 |
| 359 | |
| 360 | // Supported encoding types. |
| 361 | const ( |
| 362 | EncodingTypeLegacy EncodingType = 1 + iota |
| 363 | EncodingTypeREX |
| 364 | EncodingTypeVEX |
| 365 | EncodingTypeEVEX |
| 366 | ) |