decode.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* decode.h -- Prototypes for AArch64 simulator decoder functions.
  2. Copyright (C) 2015-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifndef _DECODE_H
  16. #define _DECODE_H
  17. #include <sys/types.h>
  18. #include "cpustate.h"
  19. /* Codes used in conditional instructions
  20. These are passed to conditional operations to identify which
  21. condition to test for. */
  22. typedef enum CondCode
  23. {
  24. EQ = 0x0, /* meaning Z == 1 */
  25. NE = 0x1, /* meaning Z == 0 */
  26. HS = 0x2, /* meaning C == 1 */
  27. CS = HS,
  28. LO = 0x3, /* meaning C == 0 */
  29. CC = LO,
  30. MI = 0x4, /* meaning N == 1 */
  31. PL = 0x5, /* meaning N == 0 */
  32. VS = 0x6, /* meaning V == 1 */
  33. VC = 0x7, /* meaning V == 0 */
  34. HI = 0x8, /* meaning C == 1 && Z == 0 */
  35. LS = 0x9, /* meaning !(C == 1 && Z == 0) */
  36. GE = 0xa, /* meaning N == V */
  37. LT = 0xb, /* meaning N != V */
  38. GT = 0xc, /* meaning Z == 0 && N == V */
  39. LE = 0xd, /* meaning !(Z == 0 && N == V) */
  40. AL = 0xe, /* meaning ANY */
  41. NV = 0xf /* ditto */
  42. } CondCode;
  43. /* Certain addressing modes for load require pre or post writeback of
  44. the computed address to a base register. */
  45. typedef enum WriteBack
  46. {
  47. Post = 0,
  48. Pre = 1,
  49. NoWriteBack = -1
  50. } WriteBack;
  51. /* Certain addressing modes for load require an offset to
  52. be optionally scaled so the decode needs to pass that
  53. through to the execute routine. */
  54. typedef enum Scaling
  55. {
  56. Unscaled = 0,
  57. Scaled = 1,
  58. NoScaling = -1
  59. } Scaling;
  60. /* When we do have to scale we do so by shifting using
  61. log(bytes in data element - 1) as the shift count.
  62. so we don't have to scale offsets when loading
  63. bytes. */
  64. typedef enum ScaleShift
  65. {
  66. ScaleShift16 = 1,
  67. ScaleShift32 = 2,
  68. ScaleShift64 = 3,
  69. ScaleShift128 = 4
  70. } ScaleShift;
  71. /* One of the addressing modes for load requires a 32-bit register
  72. value to be either zero- or sign-extended for these instructions
  73. UXTW or SXTW should be passed.
  74. Arithmetic register data processing operations can optionally
  75. extend a portion of the second register value for these
  76. instructions the value supplied must identify the portion of the
  77. register which is to be zero- or sign-exended. */
  78. typedef enum Extension
  79. {
  80. UXTB = 0,
  81. UXTH = 1,
  82. UXTW = 2,
  83. UXTX = 3,
  84. SXTB = 4,
  85. SXTH = 5,
  86. SXTW = 6,
  87. SXTX = 7,
  88. NoExtension = -1
  89. } Extension;
  90. /* Arithmetic and logical register data processing operations
  91. optionally perform a shift on the second register value. */
  92. typedef enum Shift
  93. {
  94. LSL = 0,
  95. LSR = 1,
  96. ASR = 2,
  97. ROR = 3
  98. } Shift;
  99. /* Bit twiddling helpers for instruction decode. */
  100. /* 32 bit mask with bits [hi,...,lo] set. */
  101. static inline uint32_t
  102. mask32 (int hi, int lo)
  103. {
  104. int nbits = (hi + 1) - lo;
  105. return ((1 << nbits) - 1) << lo;
  106. }
  107. /* 64 bit mask with bits [hi,...,lo] set. */
  108. static inline uint64_t
  109. mask64 (int hi, int lo)
  110. {
  111. int nbits = (hi + 1) - lo;
  112. return ((1L << nbits) - 1) << lo;
  113. }
  114. /* Pick bits [hi,...,lo] from val. */
  115. static inline uint32_t
  116. pick32 (uint32_t val, int hi, int lo)
  117. {
  118. return val & mask32 (hi, lo);
  119. }
  120. /* Pick bits [hi,...,lo] from val. */
  121. static inline uint64_t
  122. pick64 (uint64_t val, int hi, int lo)
  123. {
  124. return val & mask64 (hi, lo);
  125. }
  126. /* Pick bits [hi,...,lo] from val and shift to [(hi-(newlo - lo)),newlo]. */
  127. static inline uint32_t
  128. pickshift32 (uint32_t val, int hi, int lo, int newlo)
  129. {
  130. uint32_t bits = pick32 (val, hi, lo);
  131. if (lo < newlo)
  132. return bits << (newlo - lo);
  133. return bits >> (lo - newlo);
  134. }
  135. /* Mask [hi,lo] and shift down to start at bit 0. */
  136. static inline uint32_t
  137. pickbits32 (uint32_t val, int hi, int lo)
  138. {
  139. return pick32 (val, hi, lo) >> lo;
  140. }
  141. /* Mask [hi,lo] and shift down to start at bit 0. */
  142. static inline uint64_t
  143. pickbits64 (uint64_t val, int hi, int lo)
  144. {
  145. return pick64 (val, hi, lo) >> lo;
  146. }
  147. static inline uint32_t
  148. uimm (uint32_t val, int hi, int lo)
  149. {
  150. return pickbits32 (val, hi, lo);
  151. }
  152. static inline int32_t
  153. simm32 (uint32_t val, int hi, int lo)
  154. {
  155. union
  156. {
  157. uint32_t u;
  158. int32_t n;
  159. } x;
  160. x.u = val << (31 - hi);
  161. return x.n >> (31 - hi + lo);
  162. }
  163. static inline int64_t
  164. simm64 (uint64_t val, int hi, int lo)
  165. {
  166. union
  167. {
  168. uint64_t u;
  169. int64_t n;
  170. } x;
  171. x.u = val << (63 - hi);
  172. return x.n >> (63 - hi + lo);
  173. }
  174. /* Operation decode.
  175. Bits [28,24] are the primary dispatch vector. */
  176. static inline uint32_t
  177. dispatchGroup (uint32_t val)
  178. {
  179. return pickshift32 (val, 28, 25, 0);
  180. }
  181. /* The 16 possible values for bits [28,25] identified by tags which
  182. map them to the 5 main instruction groups LDST, DPREG, ADVSIMD,
  183. BREXSYS and DPIMM.
  184. An extra group PSEUDO is included in one of the unallocated ranges
  185. for simulator-specific pseudo-instructions. */
  186. enum DispatchGroup
  187. {
  188. GROUP_PSEUDO_0000,
  189. GROUP_UNALLOC_0001,
  190. GROUP_UNALLOC_0010,
  191. GROUP_UNALLOC_0011,
  192. GROUP_LDST_0100,
  193. GROUP_DPREG_0101,
  194. GROUP_LDST_0110,
  195. GROUP_ADVSIMD_0111,
  196. GROUP_DPIMM_1000,
  197. GROUP_DPIMM_1001,
  198. GROUP_BREXSYS_1010,
  199. GROUP_BREXSYS_1011,
  200. GROUP_LDST_1100,
  201. GROUP_DPREG_1101,
  202. GROUP_LDST_1110,
  203. GROUP_ADVSIMD_1111
  204. };
  205. /* Bits [31, 29] of a Pseudo are the secondary dispatch vector. */
  206. static inline uint32_t
  207. dispatchPseudo (uint32_t val)
  208. {
  209. return pickshift32 (val, 31, 29, 0);
  210. }
  211. /* The 8 possible values for bits [31,29] in a Pseudo Instruction.
  212. Bits [28,25] are always 0000. */
  213. enum DispatchPseudo
  214. {
  215. PSEUDO_UNALLOC_000, /* Unallocated. */
  216. PSEUDO_UNALLOC_001, /* Ditto. */
  217. PSEUDO_UNALLOC_010, /* Ditto. */
  218. PSEUDO_UNALLOC_011, /* Ditto. */
  219. PSEUDO_UNALLOC_100, /* Ditto. */
  220. PSEUDO_UNALLOC_101, /* Ditto. */
  221. PSEUDO_CALLOUT_110, /* CALLOUT -- bits [24,0] identify call/ret sig. */
  222. PSEUDO_HALT_111 /* HALT -- bits [24, 0] identify halt code. */
  223. };
  224. /* Bits [25, 23] of a DPImm are the secondary dispatch vector. */
  225. static inline uint32_t
  226. dispatchDPImm (uint32_t instr)
  227. {
  228. return pickshift32 (instr, 25, 23, 0);
  229. }
  230. /* The 8 possible values for bits [25,23] in a Data Processing Immediate
  231. Instruction. Bits [28,25] are always 100_. */
  232. enum DispatchDPImm
  233. {
  234. DPIMM_PCADR_000, /* PC-rel-addressing. */
  235. DPIMM_PCADR_001, /* Ditto. */
  236. DPIMM_ADDSUB_010, /* Add/Subtract (immediate). */
  237. DPIMM_ADDSUB_011, /* Ditto. */
  238. DPIMM_LOG_100, /* Logical (immediate). */
  239. DPIMM_MOV_101, /* Move Wide (immediate). */
  240. DPIMM_BITF_110, /* Bitfield. */
  241. DPIMM_EXTR_111 /* Extract. */
  242. };
  243. /* Bits [29,28:26] of a LS are the secondary dispatch vector. */
  244. static inline uint32_t
  245. dispatchLS (uint32_t instr)
  246. {
  247. return ( pickshift32 (instr, 29, 28, 1)
  248. | pickshift32 (instr, 26, 26, 0));
  249. }
  250. /* The 8 possible values for bits [29,28:26] in a Load/Store
  251. Instruction. Bits [28,25] are always _1_0. */
  252. enum DispatchLS
  253. {
  254. LS_EXCL_000, /* Load/store exclusive (includes some unallocated). */
  255. LS_ADVSIMD_001, /* AdvSIMD load/store (various -- includes some unallocated). */
  256. LS_LIT_010, /* Load register literal (includes some unallocated). */
  257. LS_LIT_011, /* Ditto. */
  258. LS_PAIR_100, /* Load/store register pair (various). */
  259. LS_PAIR_101, /* Ditto. */
  260. LS_OTHER_110, /* Other load/store formats. */
  261. LS_OTHER_111 /* Ditto. */
  262. };
  263. /* Bits [28:24:21] of a DPReg are the secondary dispatch vector. */
  264. static inline uint32_t
  265. dispatchDPReg (uint32_t instr)
  266. {
  267. return ( pickshift32 (instr, 28, 28, 2)
  268. | pickshift32 (instr, 24, 24, 1)
  269. | pickshift32 (instr, 21, 21, 0));
  270. }
  271. /* The 8 possible values for bits [28:24:21] in a Data Processing
  272. Register Instruction. Bits [28,25] are always _101. */
  273. enum DispatchDPReg
  274. {
  275. DPREG_LOG_000, /* Logical (shifted register). */
  276. DPREG_LOG_001, /* Ditto. */
  277. DPREG_ADDSHF_010, /* Add/subtract (shifted register). */
  278. DPREG_ADDEXT_011, /* Add/subtract (extended register). */
  279. DPREG_ADDCOND_100, /* Add/subtract (with carry) AND
  280. Cond compare/select AND
  281. Data Processing (1/2 source). */
  282. DPREG_UNALLOC_101, /* Unallocated. */
  283. DPREG_3SRC_110, /* Data Processing (3 source). */
  284. DPREG_3SRC_111 /* Data Processing (3 source). */
  285. };
  286. /* bits [31,29] of a BrExSys are the secondary dispatch vector. */
  287. static inline uint32_t
  288. dispatchBrExSys (uint32_t instr)
  289. {
  290. return pickbits32 (instr, 31, 29);
  291. }
  292. /* The 8 possible values for bits [31,29] in a Branch/Exception/System
  293. Instruction. Bits [28,25] are always 101_. */
  294. enum DispatchBr
  295. {
  296. BR_IMM_000, /* Unconditional branch (immediate). */
  297. BR_IMMCMP_001, /* Compare & branch (immediate) AND
  298. Test & branch (immediate). */
  299. BR_IMMCOND_010, /* Conditional branch (immediate) AND Unallocated. */
  300. BR_UNALLOC_011, /* Unallocated. */
  301. BR_IMM_100, /* Unconditional branch (immediate). */
  302. BR_IMMCMP_101, /* Compare & branch (immediate) AND
  303. Test & branch (immediate). */
  304. BR_REG_110, /* Unconditional branch (register) AND System AND
  305. Excn gen AND Unallocated. */
  306. BR_UNALLOC_111 /* Unallocated. */
  307. };
  308. /* TODO still need to provide secondary decode and dispatch for
  309. AdvSIMD Insructions with instr[28,25] = 0111 or 1111. */
  310. #endif /* _DECODE_H */