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