arm64.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright 2015 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. // This file encapsulates some of the odd characteristics of the ARM64
  5. // instruction set, to minimize its interaction with the core of the
  6. // assembler.
  7. package arch
  8. import (
  9. "github.com/twitchyliquid64/golang-asm/obj"
  10. "github.com/twitchyliquid64/golang-asm/obj/arm64"
  11. "errors"
  12. )
  13. var arm64LS = map[string]uint8{
  14. "P": arm64.C_XPOST,
  15. "W": arm64.C_XPRE,
  16. }
  17. var arm64Jump = map[string]bool{
  18. "B": true,
  19. "BL": true,
  20. "BEQ": true,
  21. "BNE": true,
  22. "BCS": true,
  23. "BHS": true,
  24. "BCC": true,
  25. "BLO": true,
  26. "BMI": true,
  27. "BPL": true,
  28. "BVS": true,
  29. "BVC": true,
  30. "BHI": true,
  31. "BLS": true,
  32. "BGE": true,
  33. "BLT": true,
  34. "BGT": true,
  35. "BLE": true,
  36. "CALL": true,
  37. "CBZ": true,
  38. "CBZW": true,
  39. "CBNZ": true,
  40. "CBNZW": true,
  41. "JMP": true,
  42. "TBNZ": true,
  43. "TBZ": true,
  44. }
  45. func jumpArm64(word string) bool {
  46. return arm64Jump[word]
  47. }
  48. // IsARM64CMP reports whether the op (as defined by an arm.A* constant) is
  49. // one of the comparison instructions that require special handling.
  50. func IsARM64CMP(op obj.As) bool {
  51. switch op {
  52. case arm64.ACMN, arm64.ACMP, arm64.ATST,
  53. arm64.ACMNW, arm64.ACMPW, arm64.ATSTW,
  54. arm64.AFCMPS, arm64.AFCMPD,
  55. arm64.AFCMPES, arm64.AFCMPED:
  56. return true
  57. }
  58. return false
  59. }
  60. // IsARM64STLXR reports whether the op (as defined by an arm64.A*
  61. // constant) is one of the STLXR-like instructions that require special
  62. // handling.
  63. func IsARM64STLXR(op obj.As) bool {
  64. switch op {
  65. case arm64.ASTLXRB, arm64.ASTLXRH, arm64.ASTLXRW, arm64.ASTLXR,
  66. arm64.ASTXRB, arm64.ASTXRH, arm64.ASTXRW, arm64.ASTXR,
  67. arm64.ASTXP, arm64.ASTXPW, arm64.ASTLXP, arm64.ASTLXPW:
  68. return true
  69. }
  70. // atomic instructions
  71. if arm64.IsAtomicInstruction(op) {
  72. return true
  73. }
  74. return false
  75. }
  76. // ARM64Suffix handles the special suffix for the ARM64.
  77. // It returns a boolean to indicate success; failure means
  78. // cond was unrecognized.
  79. func ARM64Suffix(prog *obj.Prog, cond string) bool {
  80. if cond == "" {
  81. return true
  82. }
  83. bits, ok := parseARM64Suffix(cond)
  84. if !ok {
  85. return false
  86. }
  87. prog.Scond = bits
  88. return true
  89. }
  90. // parseARM64Suffix parses the suffix attached to an ARM64 instruction.
  91. // The input is a single string consisting of period-separated condition
  92. // codes, such as ".P.W". An initial period is ignored.
  93. func parseARM64Suffix(cond string) (uint8, bool) {
  94. if cond == "" {
  95. return 0, true
  96. }
  97. return parseARMCondition(cond, arm64LS, nil)
  98. }
  99. func arm64RegisterNumber(name string, n int16) (int16, bool) {
  100. switch name {
  101. case "F":
  102. if 0 <= n && n <= 31 {
  103. return arm64.REG_F0 + n, true
  104. }
  105. case "R":
  106. if 0 <= n && n <= 30 { // not 31
  107. return arm64.REG_R0 + n, true
  108. }
  109. case "V":
  110. if 0 <= n && n <= 31 {
  111. return arm64.REG_V0 + n, true
  112. }
  113. }
  114. return 0, false
  115. }
  116. // IsARM64TBL reports whether the op (as defined by an arm64.A*
  117. // constant) is one of the table lookup instructions that require special
  118. // handling.
  119. func IsARM64TBL(op obj.As) bool {
  120. return op == arm64.AVTBL
  121. }
  122. // ARM64RegisterExtension parses an ARM64 register with extension or arrangement.
  123. func ARM64RegisterExtension(a *obj.Addr, ext string, reg, num int16, isAmount, isIndex bool) error {
  124. Rnum := (reg & 31) + int16(num<<5)
  125. if isAmount {
  126. if num < 0 || num > 7 {
  127. return errors.New("index shift amount is out of range")
  128. }
  129. }
  130. switch ext {
  131. case "UXTB":
  132. if !isAmount {
  133. return errors.New("invalid register extension")
  134. }
  135. if a.Type == obj.TYPE_MEM {
  136. return errors.New("invalid shift for the register offset addressing mode")
  137. }
  138. a.Reg = arm64.REG_UXTB + Rnum
  139. case "UXTH":
  140. if !isAmount {
  141. return errors.New("invalid register extension")
  142. }
  143. if a.Type == obj.TYPE_MEM {
  144. return errors.New("invalid shift for the register offset addressing mode")
  145. }
  146. a.Reg = arm64.REG_UXTH + Rnum
  147. case "UXTW":
  148. if !isAmount {
  149. return errors.New("invalid register extension")
  150. }
  151. // effective address of memory is a base register value and an offset register value.
  152. if a.Type == obj.TYPE_MEM {
  153. a.Index = arm64.REG_UXTW + Rnum
  154. } else {
  155. a.Reg = arm64.REG_UXTW + Rnum
  156. }
  157. case "UXTX":
  158. if !isAmount {
  159. return errors.New("invalid register extension")
  160. }
  161. if a.Type == obj.TYPE_MEM {
  162. return errors.New("invalid shift for the register offset addressing mode")
  163. }
  164. a.Reg = arm64.REG_UXTX + Rnum
  165. case "SXTB":
  166. if !isAmount {
  167. return errors.New("invalid register extension")
  168. }
  169. a.Reg = arm64.REG_SXTB + Rnum
  170. case "SXTH":
  171. if !isAmount {
  172. return errors.New("invalid register extension")
  173. }
  174. if a.Type == obj.TYPE_MEM {
  175. return errors.New("invalid shift for the register offset addressing mode")
  176. }
  177. a.Reg = arm64.REG_SXTH + Rnum
  178. case "SXTW":
  179. if !isAmount {
  180. return errors.New("invalid register extension")
  181. }
  182. if a.Type == obj.TYPE_MEM {
  183. a.Index = arm64.REG_SXTW + Rnum
  184. } else {
  185. a.Reg = arm64.REG_SXTW + Rnum
  186. }
  187. case "SXTX":
  188. if !isAmount {
  189. return errors.New("invalid register extension")
  190. }
  191. if a.Type == obj.TYPE_MEM {
  192. a.Index = arm64.REG_SXTX + Rnum
  193. } else {
  194. a.Reg = arm64.REG_SXTX + Rnum
  195. }
  196. case "LSL":
  197. if !isAmount {
  198. return errors.New("invalid register extension")
  199. }
  200. a.Index = arm64.REG_LSL + Rnum
  201. case "B8":
  202. if isIndex {
  203. return errors.New("invalid register extension")
  204. }
  205. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_8B & 15) << 5)
  206. case "B16":
  207. if isIndex {
  208. return errors.New("invalid register extension")
  209. }
  210. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_16B & 15) << 5)
  211. case "H4":
  212. if isIndex {
  213. return errors.New("invalid register extension")
  214. }
  215. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_4H & 15) << 5)
  216. case "H8":
  217. if isIndex {
  218. return errors.New("invalid register extension")
  219. }
  220. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_8H & 15) << 5)
  221. case "S2":
  222. if isIndex {
  223. return errors.New("invalid register extension")
  224. }
  225. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_2S & 15) << 5)
  226. case "S4":
  227. if isIndex {
  228. return errors.New("invalid register extension")
  229. }
  230. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_4S & 15) << 5)
  231. case "D1":
  232. if isIndex {
  233. return errors.New("invalid register extension")
  234. }
  235. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_1D & 15) << 5)
  236. case "D2":
  237. if isIndex {
  238. return errors.New("invalid register extension")
  239. }
  240. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_2D & 15) << 5)
  241. case "Q1":
  242. if isIndex {
  243. return errors.New("invalid register extension")
  244. }
  245. a.Reg = arm64.REG_ARNG + (reg & 31) + ((arm64.ARNG_1Q & 15) << 5)
  246. case "B":
  247. if !isIndex {
  248. return nil
  249. }
  250. a.Reg = arm64.REG_ELEM + (reg & 31) + ((arm64.ARNG_B & 15) << 5)
  251. a.Index = num
  252. case "H":
  253. if !isIndex {
  254. return nil
  255. }
  256. a.Reg = arm64.REG_ELEM + (reg & 31) + ((arm64.ARNG_H & 15) << 5)
  257. a.Index = num
  258. case "S":
  259. if !isIndex {
  260. return nil
  261. }
  262. a.Reg = arm64.REG_ELEM + (reg & 31) + ((arm64.ARNG_S & 15) << 5)
  263. a.Index = num
  264. case "D":
  265. if !isIndex {
  266. return nil
  267. }
  268. a.Reg = arm64.REG_ELEM + (reg & 31) + ((arm64.ARNG_D & 15) << 5)
  269. a.Index = num
  270. default:
  271. return errors.New("unsupported register extension type: " + ext)
  272. }
  273. return nil
  274. }
  275. // ARM64RegisterArrangement parses an ARM64 vector register arrangement.
  276. func ARM64RegisterArrangement(reg int16, name, arng string) (int64, error) {
  277. var curQ, curSize uint16
  278. if name[0] != 'V' {
  279. return 0, errors.New("expect V0 through V31; found: " + name)
  280. }
  281. if reg < 0 {
  282. return 0, errors.New("invalid register number: " + name)
  283. }
  284. switch arng {
  285. case "B8":
  286. curSize = 0
  287. curQ = 0
  288. case "B16":
  289. curSize = 0
  290. curQ = 1
  291. case "H4":
  292. curSize = 1
  293. curQ = 0
  294. case "H8":
  295. curSize = 1
  296. curQ = 1
  297. case "S2":
  298. curSize = 2
  299. curQ = 0
  300. case "S4":
  301. curSize = 2
  302. curQ = 1
  303. case "D1":
  304. curSize = 3
  305. curQ = 0
  306. case "D2":
  307. curSize = 3
  308. curQ = 1
  309. default:
  310. return 0, errors.New("invalid arrangement in ARM64 register list")
  311. }
  312. return (int64(curQ) & 1 << 30) | (int64(curSize&3) << 10), nil
  313. }
  314. // ARM64RegisterListOffset generates offset encoding according to AArch64 specification.
  315. func ARM64RegisterListOffset(firstReg, regCnt int, arrangement int64) (int64, error) {
  316. offset := int64(firstReg)
  317. switch regCnt {
  318. case 1:
  319. offset |= 0x7 << 12
  320. case 2:
  321. offset |= 0xa << 12
  322. case 3:
  323. offset |= 0x6 << 12
  324. case 4:
  325. offset |= 0x2 << 12
  326. default:
  327. return 0, errors.New("invalid register numbers in ARM64 register list")
  328. }
  329. offset |= arrangement
  330. // arm64 uses the 60th bit to differentiate from other archs
  331. // For more details, refer to: obj/arm64/list7.go
  332. offset |= 1 << 60
  333. return offset, nil
  334. }