nds32-asm.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* NDS32-specific support for 32-bit ELF.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Andes Technology Corporation.
  4. This file is part of BFD, the Binary File Descriptor library.
  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, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #ifndef NDS32_ASM_H
  18. #define NDS32_ASM_H
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /* Constant values for assembler. */
  23. enum
  24. {
  25. /* Error code for assembling an instruction. */
  26. NASM_OK = 0,
  27. NASM_ERR_UNKNOWN_OP,
  28. NASM_ERR_SYNTAX,
  29. NASM_ERR_OPERAND,
  30. NASM_ERR_OUT_OF_RANGE,
  31. NASM_ERR_REG_REDUCED,
  32. NASM_ERR_JUNK_EOL,
  33. /* Results of parse_operand. */
  34. NASM_R_CONST,
  35. NASM_R_SYMBOL,
  36. NASM_R_ILLEGAL,
  37. /* Flags for open description. */
  38. NASM_OPEN_ARCH_V1 = 0x0,
  39. NASM_OPEN_ARCH_V2 = 0x1,
  40. NASM_OPEN_ARCH_V3 = 0x2,
  41. NASM_OPEN_ARCH_V3M = 0x3,
  42. NASM_OPEN_ARCH_MASK = 0xf,
  43. NASM_OPEN_REDUCED_REG = 0x10,
  44. /* Common attributes. */
  45. NASM_ATTR_ISA_V1 = 0x01,
  46. NASM_ATTR_ISA_V2 = 0x02,
  47. NASM_ATTR_ISA_V3 = 0x04,
  48. NASM_ATTR_ISA_V3M = 0x08,
  49. NASM_ATTR_ISA_ALL = 0x0f,
  50. /* Attributes for instructions. */
  51. NASM_ATTR_MAC = 0x0000100,
  52. NASM_ATTR_DIV = 0x0000200,
  53. NASM_ATTR_FPU = 0x0000400,
  54. NASM_ATTR_FPU_SP_EXT = 0x0000800,
  55. NASM_ATTR_FPU_DP_EXT = 0x0001000,
  56. NASM_ATTR_STR_EXT = 0x0002000,
  57. NASM_ATTR_PERF_EXT = 0x0004000,
  58. NASM_ATTR_PERF2_EXT = 0x0008000,
  59. NASM_ATTR_AUDIO_ISAEXT = 0x0010000,
  60. NASM_ATTR_IFC_EXT = 0x0020000,
  61. NASM_ATTR_EX9_EXT = 0x0040000,
  62. NASM_ATTR_FPU_FMA = 0x0080000,
  63. NASM_ATTR_DXREG = 0x0100000,
  64. NASM_ATTR_BRANCH = 0x0200000,
  65. NASM_ATTR_SATURATION_EXT = 0x0400000,
  66. NASM_ATTR_PCREL = 0x0800000,
  67. NASM_ATTR_GPREL = 0x1000000,
  68. NASM_ATTR_DSP_ISAEXT = 0x2000000,
  69. NASM_ATTR_ZOL = (1 << 26),
  70. /* Attributes for relocations. */
  71. NASM_ATTR_HI20 = 0x10000000,
  72. NASM_ATTR_LO12 = 0x20000000,
  73. NASM_ATTR_LO20 = 0x40000000,
  74. /* Attributes for registers. */
  75. NASM_ATTR_RDREG = 0x000100
  76. };
  77. /* We only support one core for now. */
  78. #define NDS32_CORE_COUNT 1
  79. #define NDS32_MAIN_CORE 0
  80. enum
  81. {
  82. /* This operand is used for input or output. (define or use) */
  83. SYN_INPUT = 0x10000,
  84. SYN_OUTPUT = 0x20000,
  85. SYN_LOPT = 0x40000,
  86. SYN_ROPT = 0x80000,
  87. /* Hardware resources:
  88. Current set up allows up to 256 resources for each class
  89. defined above. */
  90. HW_GPR = NDS32_MAIN_CORE << 8,
  91. HW_USR,
  92. HW_DXR,
  93. HW_SR,
  94. HW_FSR,
  95. HW_FDR,
  96. HW_CP, /* Co-processor ID. */
  97. HW_CPR, /* Co-processor registers. */
  98. HW_ABDIM, /* [ab][di]m? flag for LSMWA?. */
  99. HW_ABM, /* [ab]m? flag for LSMWZB. */
  100. HW_DTITON,
  101. HW_DTITOFF,
  102. HW_DPREF_ST,
  103. HW_CCTL_ST0,
  104. HW_CCTL_ST1,
  105. HW_CCTL_ST2,
  106. HW_CCTL_ST3,
  107. HW_CCTL_ST4,
  108. HW_CCTL_ST5,
  109. HW_CCTL_LV,
  110. HW_TLBOP_ST,
  111. HW_STANDBY_ST,
  112. HW_MSYNC_ST,
  113. HW_AEXT_IM_I,
  114. HW_AEXT_IM_M,
  115. HW_AEXT_ACC,
  116. HW_AEXT_ARIDX,
  117. HW_AEXT_ARIDX2,
  118. HW_AEXT_ARIDXI,
  119. HW_AEXT_ARIDXI_MX,
  120. _HW_LAST,
  121. HW_INT = 0x1000,
  122. HW_UINT
  123. };
  124. /* for audio-extension. */
  125. enum
  126. {
  127. N32_AEXT_AMADD = 0,
  128. N32_AEXT_AMSUB,
  129. N32_AEXT_AMULT,
  130. N32_AEXT_AMFAR,
  131. N32_AEXT_AMADDS,
  132. N32_AEXT_AMSUBS,
  133. N32_AEXT_AMULTS,
  134. N32_AEXT_AMNEGS,
  135. N32_AEXT_AADDL,
  136. N32_AEXT_AMTARI,
  137. N32_AEXT_AMAWBS = 0x0c,
  138. N32_AEXT_AMAWTS,
  139. N32_AEXT_AMWBS,
  140. N32_AEXT_AMWTS,
  141. N32_AEXT_AMABBS,
  142. N32_AEXT_AMABTS,
  143. N32_AEXT_AMATBS,
  144. N32_AEXT_AMATTS,
  145. N32_AEXT_AMBBS,
  146. N32_AEXT_AMBTS,
  147. N32_AEXT_AMTBS,
  148. N32_AEXT_AMTTS
  149. };
  150. /* Macro for instruction attribute. */
  151. #define ATTR(attr) NASM_ATTR_ ## attr
  152. #define ATTR_NONE 0
  153. #define ATTR_PCREL (ATTR (PCREL) | ATTR (BRANCH))
  154. #define ATTR_ALL (ATTR (ISA_ALL))
  155. #define ATTR_V2UP (ATTR_ALL & ~(ATTR (ISA_V1)))
  156. #define ATTR_V3MUP (ATTR (ISA_V3) | ATTR (ISA_V3M))
  157. #define ATTR_V3 (ATTR (ISA_V3))
  158. #define ATTR_V3MEX_V1 (ATTR_ALL & ~(ATTR (ISA_V3M)))
  159. #define ATTR_V3MEX_V2 (ATTR_V2UP & ~(ATTR (ISA_V3M)))
  160. /* Lexical element in parsed syntax. */
  161. typedef int lex_t;
  162. /* Common header for hash entries. */
  163. struct nds32_hash_entry
  164. {
  165. const char *name;
  166. };
  167. typedef struct nds32_keyword
  168. {
  169. const char *name;
  170. int value;
  171. uint64_t attr;
  172. } keyword_t;
  173. typedef struct nds32_opcode
  174. {
  175. /* Opcode for the instruction. */
  176. const char *opcode;
  177. /* Human readable string of this instruction. */
  178. const char *instruction;
  179. /* Base value of this instruction. */
  180. uint32_t value;
  181. /* The byte-size of the instruction. */
  182. int isize;
  183. /* Attributes of this instruction. */
  184. uint64_t attr;
  185. /* Implicit define/use. */
  186. uint64_t defuse;
  187. /* Parsed string for assembling. */
  188. lex_t *syntax;
  189. /* Number of variant. */
  190. int variant;
  191. /* Next form of the same mnemonic. */
  192. struct nds32_opcode *next;
  193. /* TODO: Extra constrains and verification.
  194. For example, `mov55 $sp, $sp' is not allowed in v3. */
  195. } opcode_t;
  196. typedef struct nds32_asm_insn
  197. {
  198. /* Assembled instruction bytes. */
  199. uint32_t insn;
  200. /* The opcode structure for this instruction. */
  201. struct nds32_opcode *opcode;
  202. /* The field need special fix-up, used for relocation. */
  203. const struct nds32_field *field;
  204. /* Attributes for relocation. */
  205. uint64_t attr;
  206. /* Application-dependent data, e.g., expression. */
  207. void *info;
  208. /* Input/output registers. */
  209. uint64_t defuse;
  210. } nds32_asm_insn_t;
  211. typedef struct nds32_asm_desc
  212. {
  213. /* The callback provided by assembler user for parse an operand,
  214. e.g., parse integer. */
  215. int (*parse_operand) (struct nds32_asm_desc *,
  216. struct nds32_asm_insn *,
  217. char **, int64_t *);
  218. /* Result of assembling. */
  219. int result;
  220. /* The mach for this assembling. */
  221. int mach;
  222. int flags;
  223. } nds32_asm_desc_t;
  224. /* The field information for an operand. */
  225. typedef struct nds32_field
  226. {
  227. /* Name of the field. */
  228. const char *name;
  229. int bitpos;
  230. int bitsize;
  231. int shift;
  232. int hw_res;
  233. int (*parse) (struct nds32_asm_desc *,
  234. struct nds32_asm_insn *,
  235. char **, int64_t *);
  236. } field_t;
  237. extern const field_t *nds32_field_table[NDS32_CORE_COUNT];
  238. extern opcode_t *nds32_opcode_table[NDS32_CORE_COUNT];
  239. extern const keyword_t **nds32_keyword_table[NDS32_CORE_COUNT];
  240. extern struct nds32_opcode nds32_opcodes[];
  241. extern const field_t nds32_operand_fields[];
  242. extern const keyword_t *nds32_keywords[];
  243. extern const keyword_t nds32_keyword_gpr[];
  244. extern void nds32_assemble (nds32_asm_desc_t *, nds32_asm_insn_t *, char *);
  245. extern void nds32_asm_init (nds32_asm_desc_t *, int);
  246. #define OP6(op6) (N32_OP6_ ## op6 << 25)
  247. #define LSMW(sub) (OP6 (LSMW) | N32_LSMW_ ## sub)
  248. #define JREG(sub) (OP6 (JREG) | N32_JREG_ ## sub)
  249. #define JREG_RET (1 << 5)
  250. #define JREG_IFC (1 << 6)
  251. #define BR2(sub) (OP6 (BR2) | (N32_BR2_ ## sub << 16))
  252. #define SIMD(sub) (OP6 (SIMD) | N32_SIMD_ ## sub)
  253. #define ALU1(sub) (OP6 (ALU1) | N32_ALU1_ ## sub)
  254. #define ALU2(sub) (OP6 (ALU2) | N32_ALU2_ ## sub)
  255. #define ALU2_1(sub) (OP6 (ALU2) | N32_BIT (6) | N32_ALU2_ ## sub)
  256. #define ALU2_2(sub) (OP6 (ALU2) | N32_BIT (7) | N32_ALU2_ ## sub)
  257. #define ALU2_3(sub) (OP6 (ALU2) | N32_BIT (6) | N32_BIT (7) | N32_ALU2_ ## sub)
  258. #define MISC(sub) (OP6 (MISC) | N32_MISC_ ## sub)
  259. #define MEM(sub) (OP6 (MEM) | N32_MEM_ ## sub)
  260. #define FPU_RA_IMMBI(sub) (OP6 (sub) | N32_BIT (12))
  261. #define FS1(sub) (OP6 (COP) | N32_FPU_FS1 | (N32_FPU_FS1_ ## sub << 6))
  262. #define FS1_F2OP(sub) (OP6 (COP) | N32_FPU_FS1 | (N32_FPU_FS1_F2OP << 6) \
  263. | (N32_FPU_FS1_F2OP_ ## sub << 10))
  264. #define FS2(sub) (OP6 (COP) | N32_FPU_FS2 | (N32_FPU_FS2_ ## sub << 6))
  265. #define FD1(sub) (OP6 (COP) | N32_FPU_FD1 | (N32_FPU_FD1_ ## sub << 6))
  266. #define FD1_F2OP(sub) (OP6 (COP) | N32_FPU_FD1 | (N32_FPU_FD1_F2OP << 6) \
  267. | (N32_FPU_FD1_F2OP_ ## sub << 10))
  268. #define FD2(sub) (OP6 (COP) | N32_FPU_FD2 | (N32_FPU_FD2_ ## sub << 6))
  269. #define MFCP(sub) (OP6 (COP) | N32_FPU_MFCP | (N32_FPU_MFCP_ ## sub << 6))
  270. #define MFCP_XR(sub) (OP6 (COP) | N32_FPU_MFCP | (N32_FPU_MFCP_XR << 6) \
  271. | (N32_FPU_MFCP_XR_ ## sub << 10))
  272. #define MTCP(sub) (OP6 (COP) | N32_FPU_MTCP | (N32_FPU_MTCP_ ## sub << 6))
  273. #define MTCP_XR(sub) (OP6 (COP) | N32_FPU_MTCP | (N32_FPU_MTCP_XR << 6) \
  274. | (N32_FPU_MTCP_XR_ ## sub << 10))
  275. #define FPU_MEM(sub) (OP6 (COP) | N32_FPU_ ## sub)
  276. #define FPU_MEMBI(sub) (OP6 (COP) | N32_FPU_ ## sub | 0x1 << 7)
  277. #define AUDIO(sub) (OP6 (AEXT) | (N32_AEXT_ ## sub << 20))
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif