evex.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package x86
  5. import (
  6. "github.com/twitchyliquid64/golang-asm/obj"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. )
  11. // evexBits stores EVEX prefix info that is used during instruction encoding.
  12. type evexBits struct {
  13. b1 byte // [W1mmLLpp]
  14. b2 byte // [NNNbbZRS]
  15. // Associated instruction opcode.
  16. opcode byte
  17. }
  18. // newEVEXBits creates evexBits object from enc bytes at z position.
  19. func newEVEXBits(z int, enc *opBytes) evexBits {
  20. return evexBits{
  21. b1: enc[z+0],
  22. b2: enc[z+1],
  23. opcode: enc[z+2],
  24. }
  25. }
  26. // P returns EVEX.pp value.
  27. func (evex evexBits) P() byte { return (evex.b1 & evexP) >> 0 }
  28. // L returns EVEX.L'L value.
  29. func (evex evexBits) L() byte { return (evex.b1 & evexL) >> 2 }
  30. // M returns EVEX.mm value.
  31. func (evex evexBits) M() byte { return (evex.b1 & evexM) >> 4 }
  32. // W returns EVEX.W value.
  33. func (evex evexBits) W() byte { return (evex.b1 & evexW) >> 7 }
  34. // BroadcastEnabled reports whether BCST suffix is permitted.
  35. func (evex evexBits) BroadcastEnabled() bool {
  36. return evex.b2&evexBcst != 0
  37. }
  38. // ZeroingEnabled reports whether Z suffix is permitted.
  39. func (evex evexBits) ZeroingEnabled() bool {
  40. return (evex.b2&evexZeroing)>>2 != 0
  41. }
  42. // RoundingEnabled reports whether RN_SAE, RZ_SAE, RD_SAE and RU_SAE suffixes
  43. // are permitted.
  44. func (evex evexBits) RoundingEnabled() bool {
  45. return (evex.b2&evexRounding)>>1 != 0
  46. }
  47. // SaeEnabled reports whether SAE suffix is permitted.
  48. func (evex evexBits) SaeEnabled() bool {
  49. return (evex.b2&evexSae)>>0 != 0
  50. }
  51. // DispMultiplier returns displacement multiplier that is calculated
  52. // based on tuple type, EVEX.W and input size.
  53. // If embedded broadcast is used, bcst should be true.
  54. func (evex evexBits) DispMultiplier(bcst bool) int32 {
  55. if bcst {
  56. switch evex.b2 & evexBcst {
  57. case evexBcstN4:
  58. return 4
  59. case evexBcstN8:
  60. return 8
  61. }
  62. return 1
  63. }
  64. switch evex.b2 & evexN {
  65. case evexN1:
  66. return 1
  67. case evexN2:
  68. return 2
  69. case evexN4:
  70. return 4
  71. case evexN8:
  72. return 8
  73. case evexN16:
  74. return 16
  75. case evexN32:
  76. return 32
  77. case evexN64:
  78. return 64
  79. case evexN128:
  80. return 128
  81. }
  82. return 1
  83. }
  84. // EVEX is described by using 2-byte sequence.
  85. // See evexBits for more details.
  86. const (
  87. evexW = 0x80 // b1[W... ....]
  88. evexWIG = 0 << 7
  89. evexW0 = 0 << 7
  90. evexW1 = 1 << 7
  91. evexM = 0x30 // b2[..mm ...]
  92. evex0F = 1 << 4
  93. evex0F38 = 2 << 4
  94. evex0F3A = 3 << 4
  95. evexL = 0x0C // b1[.... LL..]
  96. evexLIG = 0 << 2
  97. evex128 = 0 << 2
  98. evex256 = 1 << 2
  99. evex512 = 2 << 2
  100. evexP = 0x03 // b1[.... ..pp]
  101. evex66 = 1 << 0
  102. evexF3 = 2 << 0
  103. evexF2 = 3 << 0
  104. // Precalculated Disp8 N value.
  105. // N acts like a multiplier for 8bit displacement.
  106. // Note that some N are not used, but their bits are reserved.
  107. evexN = 0xE0 // b2[NNN. ....]
  108. evexN1 = 0 << 5
  109. evexN2 = 1 << 5
  110. evexN4 = 2 << 5
  111. evexN8 = 3 << 5
  112. evexN16 = 4 << 5
  113. evexN32 = 5 << 5
  114. evexN64 = 6 << 5
  115. evexN128 = 7 << 5
  116. // Disp8 for broadcasts.
  117. evexBcst = 0x18 // b2[...b b...]
  118. evexBcstN4 = 1 << 3
  119. evexBcstN8 = 2 << 3
  120. // Flags that permit certain AVX512 features.
  121. // It's semantically illegal to combine evexZeroing and evexSae.
  122. evexZeroing = 0x4 // b2[.... .Z..]
  123. evexZeroingEnabled = 1 << 2
  124. evexRounding = 0x2 // b2[.... ..R.]
  125. evexRoundingEnabled = 1 << 1
  126. evexSae = 0x1 // b2[.... ...S]
  127. evexSaeEnabled = 1 << 0
  128. )
  129. // compressedDisp8 calculates EVEX compressed displacement, if applicable.
  130. func compressedDisp8(disp, elemSize int32) (disp8 byte, ok bool) {
  131. if disp%elemSize == 0 {
  132. v := disp / elemSize
  133. if v >= -128 && v <= 127 {
  134. return byte(v), true
  135. }
  136. }
  137. return 0, false
  138. }
  139. // evexZcase reports whether given Z-case belongs to EVEX group.
  140. func evexZcase(zcase uint8) bool {
  141. return zcase > Zevex_first && zcase < Zevex_last
  142. }
  143. // evexSuffixBits carries instruction EVEX suffix set flags.
  144. //
  145. // Examples:
  146. // "RU_SAE.Z" => {rounding: 3, zeroing: true}
  147. // "Z" => {zeroing: true}
  148. // "BCST" => {broadcast: true}
  149. // "SAE.Z" => {sae: true, zeroing: true}
  150. type evexSuffix struct {
  151. rounding byte
  152. sae bool
  153. zeroing bool
  154. broadcast bool
  155. }
  156. // Rounding control values.
  157. // Match exact value for EVEX.L'L field (with exception of rcUnset).
  158. const (
  159. rcRNSAE = 0 // Round towards nearest
  160. rcRDSAE = 1 // Round towards -Inf
  161. rcRUSAE = 2 // Round towards +Inf
  162. rcRZSAE = 3 // Round towards zero
  163. rcUnset = 4
  164. )
  165. // newEVEXSuffix returns proper zero value for evexSuffix.
  166. func newEVEXSuffix() evexSuffix {
  167. return evexSuffix{rounding: rcUnset}
  168. }
  169. // evexSuffixMap maps obj.X86suffix to its decoded version.
  170. // Filled during init().
  171. var evexSuffixMap [255]evexSuffix
  172. func init() {
  173. // Decode all valid suffixes for later use.
  174. for i := range opSuffixTable {
  175. suffix := newEVEXSuffix()
  176. parts := strings.Split(opSuffixTable[i], ".")
  177. for j := range parts {
  178. switch parts[j] {
  179. case "Z":
  180. suffix.zeroing = true
  181. case "BCST":
  182. suffix.broadcast = true
  183. case "SAE":
  184. suffix.sae = true
  185. case "RN_SAE":
  186. suffix.rounding = rcRNSAE
  187. case "RD_SAE":
  188. suffix.rounding = rcRDSAE
  189. case "RU_SAE":
  190. suffix.rounding = rcRUSAE
  191. case "RZ_SAE":
  192. suffix.rounding = rcRZSAE
  193. }
  194. }
  195. evexSuffixMap[i] = suffix
  196. }
  197. }
  198. // toDisp8 tries to convert disp to proper 8-bit displacement value.
  199. func toDisp8(disp int32, p *obj.Prog, asmbuf *AsmBuf) (disp8 byte, ok bool) {
  200. if asmbuf.evexflag {
  201. bcst := evexSuffixMap[p.Scond].broadcast
  202. elemSize := asmbuf.evex.DispMultiplier(bcst)
  203. return compressedDisp8(disp, elemSize)
  204. }
  205. return byte(disp), disp >= -128 && disp < 128
  206. }
  207. // EncodeRegisterRange packs [reg0-reg1] list into 64-bit value that
  208. // is intended to be stored inside obj.Addr.Offset with TYPE_REGLIST.
  209. func EncodeRegisterRange(reg0, reg1 int16) int64 {
  210. return (int64(reg0) << 0) |
  211. (int64(reg1) << 16) |
  212. obj.RegListX86Lo
  213. }
  214. // decodeRegisterRange unpacks [reg0-reg1] list from 64-bit value created by EncodeRegisterRange.
  215. func decodeRegisterRange(list int64) (reg0, reg1 int) {
  216. return int((list >> 0) & 0xFFFF),
  217. int((list >> 16) & 0xFFFF)
  218. }
  219. // ParseSuffix handles the special suffix for the 386/AMD64.
  220. // Suffix bits are stored into p.Scond.
  221. //
  222. // Leading "." in cond is ignored.
  223. func ParseSuffix(p *obj.Prog, cond string) error {
  224. cond = strings.TrimPrefix(cond, ".")
  225. suffix := newOpSuffix(cond)
  226. if !suffix.IsValid() {
  227. return inferSuffixError(cond)
  228. }
  229. p.Scond = uint8(suffix)
  230. return nil
  231. }
  232. // inferSuffixError returns non-nil error that describes what could be
  233. // the cause of suffix parse failure.
  234. //
  235. // At the point this function is executed there is already assembly error,
  236. // so we can burn some clocks to construct good error message.
  237. //
  238. // Reported issues:
  239. // - duplicated suffixes
  240. // - illegal rounding/SAE+broadcast combinations
  241. // - unknown suffixes
  242. // - misplaced suffix (e.g. wrong Z suffix position)
  243. func inferSuffixError(cond string) error {
  244. suffixSet := make(map[string]bool) // Set for duplicates detection.
  245. unknownSet := make(map[string]bool) // Set of unknown suffixes.
  246. hasBcst := false
  247. hasRoundSae := false
  248. var msg []string // Error message parts
  249. suffixes := strings.Split(cond, ".")
  250. for i, suffix := range suffixes {
  251. switch suffix {
  252. case "Z":
  253. if i != len(suffixes)-1 {
  254. msg = append(msg, "Z suffix should be the last")
  255. }
  256. case "BCST":
  257. hasBcst = true
  258. case "SAE", "RN_SAE", "RZ_SAE", "RD_SAE", "RU_SAE":
  259. hasRoundSae = true
  260. default:
  261. if !unknownSet[suffix] {
  262. msg = append(msg, fmt.Sprintf("unknown suffix %q", suffix))
  263. }
  264. unknownSet[suffix] = true
  265. }
  266. if suffixSet[suffix] {
  267. msg = append(msg, fmt.Sprintf("duplicate suffix %q", suffix))
  268. }
  269. suffixSet[suffix] = true
  270. }
  271. if hasBcst && hasRoundSae {
  272. msg = append(msg, "can't combine rounding/SAE and broadcast")
  273. }
  274. if len(msg) == 0 {
  275. return errors.New("bad suffix combination")
  276. }
  277. return errors.New(strings.Join(msg, "; "))
  278. }
  279. // opSuffixTable is a complete list of possible opcode suffix combinations.
  280. // It "maps" uint8 suffix bits to their string representation.
  281. // With the exception of first and last elements, order is not important.
  282. var opSuffixTable = [...]string{
  283. "", // Map empty suffix to empty string.
  284. "Z",
  285. "SAE",
  286. "SAE.Z",
  287. "RN_SAE",
  288. "RZ_SAE",
  289. "RD_SAE",
  290. "RU_SAE",
  291. "RN_SAE.Z",
  292. "RZ_SAE.Z",
  293. "RD_SAE.Z",
  294. "RU_SAE.Z",
  295. "BCST",
  296. "BCST.Z",
  297. "<bad suffix>",
  298. }
  299. // opSuffix represents instruction opcode suffix.
  300. // Compound (multi-part) suffixes expressed with single opSuffix value.
  301. //
  302. // uint8 type is used to fit obj.Prog.Scond.
  303. type opSuffix uint8
  304. // badOpSuffix is used to represent all invalid suffix combinations.
  305. const badOpSuffix = opSuffix(len(opSuffixTable) - 1)
  306. // newOpSuffix returns opSuffix object that matches suffixes string.
  307. //
  308. // If no matching suffix is found, special "invalid" suffix is returned.
  309. // Use IsValid method to check against this case.
  310. func newOpSuffix(suffixes string) opSuffix {
  311. for i := range opSuffixTable {
  312. if opSuffixTable[i] == suffixes {
  313. return opSuffix(i)
  314. }
  315. }
  316. return badOpSuffix
  317. }
  318. // IsValid reports whether suffix is valid.
  319. // Empty suffixes are valid.
  320. func (suffix opSuffix) IsValid() bool {
  321. return suffix != badOpSuffix
  322. }
  323. // String returns suffix printed representation.
  324. //
  325. // It matches the string that was used to create suffix with NewX86Suffix()
  326. // for valid suffixes.
  327. // For all invalid suffixes, special marker is returned.
  328. func (suffix opSuffix) String() string {
  329. return opSuffixTable[suffix]
  330. }