1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30 /* Constant values for assembler. */
33 /* Error code for assembling an instruction. */
38 NASM_ERR_OUT_OF_RANGE,
42 /* Results of parse_operand. */
47 /* Flags for open description. */
48 NASM_OPEN_ARCH_V1 = 0x0,
49 NASM_OPEN_ARCH_V2 = 0x1,
50 NASM_OPEN_ARCH_V3 = 0x2,
51 NASM_OPEN_ARCH_V3M = 0x3,
52 NASM_OPEN_ARCH_MASK = 0xf,
53 NASM_OPEN_REDUCED_REG = 0x10,
55 /* Common attributes. */
56 NASM_ATTR_ISA_V1 = 0x01,
57 NASM_ATTR_ISA_V2 = 0x02,
58 NASM_ATTR_ISA_V3 = 0x04,
59 NASM_ATTR_ISA_V3M = 0x08,
60 NASM_ATTR_ISA_ALL = 0x0f,
62 /* Attributes for instructions. */
63 NASM_ATTR_MAC = 0x0000100,
64 NASM_ATTR_DIV = 0x0000200,
65 NASM_ATTR_FPU = 0x0000400,
66 NASM_ATTR_FPU_SP_EXT = 0x0000800,
67 NASM_ATTR_FPU_DP_EXT = 0x0001000,
68 NASM_ATTR_STR_EXT = 0x0002000,
69 NASM_ATTR_PERF_EXT = 0x0004000,
70 NASM_ATTR_PERF2_EXT = 0x0008000,
71 NASM_ATTR_AUDIO_ISAEXT = 0x0010000,
72 NASM_ATTR_IFC_EXT = 0x0020000,
73 NASM_ATTR_EX9_EXT = 0x0040000,
74 NASM_ATTR_FPU_FMA = 0x0080000,
75 NASM_ATTR_DXREG = 0x0100000,
76 NASM_ATTR_BRANCH = 0x0200000,
77 NASM_ATTR_SATURATION_EXT = 0x0400000,
78 NASM_ATTR_PCREL = 0x0800000,
79 NASM_ATTR_GPREL = 0x1000000,
81 /* Attributes for relocations. */
82 NASM_ATTR_HI20 = 0x10000000,
83 NASM_ATTR_LO12 = 0x20000000,
84 NASM_ATTR_LO20 = 0x40000000,
86 /* Attributes for registers. */
87 NASM_ATTR_RDREG = 0x000100
92 /* This is a field (operand) of just a separator char. */
95 /* This operand is used for input or output. (define or use) */
101 /* Hardware resources. */
108 HW_CP, /* Co-processor ID. */
109 HW_CPR, /* Co-processor registers. */
110 HW_ABDIM, /* [ab][di]m? flag for LSMWA?. */
111 HW_ABM, /* [ab]m? flag for LSMWZB. */
132 /* TODO: Maybe we should add a new type to distinguish address and
133 const int. Only the former allows symbols and relocations. */
138 /* for audio-extension. */
151 N32_AEXT_AMAWBS = 0x0c,
165 /* Macro for instruction attribute. */
166 #define ATTR(attr) NASM_ATTR_ ## attr
168 #define ATTR_PCREL (ATTR (PCREL) | ATTR (BRANCH))
170 #define ATTR_ALL (ATTR (ISA_ALL))
171 #define ATTR_V2UP (ATTR_ALL & ~(ATTR (ISA_V1)))
172 #define ATTR_V3MUP (ATTR (ISA_V3) | ATTR (ISA_V3M))
173 #define ATTR_V3 (ATTR (ISA_V3))
174 #define ATTR_V3MEX_V1 (ATTR_ALL & ~(ATTR (ISA_V3M)))
175 #define ATTR_V3MEX_V2 (ATTR_V2UP & ~(ATTR (ISA_V3M)))
177 /* Lexical element in parsed syntax. */
180 /* Common header for hash entries. */
181 struct nds32_hash_entry
186 typedef struct nds32_keyword
193 typedef struct nds32_opcode
195 /* Opcode for the instruction. */
197 /* Human readable string of this instruction. */
198 const char *instruction;
199 /* Base value of this instruction. */
201 /* The byte-size of the instruction. */
203 /* Attributes of this instruction. */
205 /* Implicit define/use. */
207 /* Parsed string for assembling. */
209 /* Number of variant. */
211 /* Next form of the same mnemonic. */
212 struct nds32_opcode *next;
214 /* TODO: Extra constrains and verification.
215 For example, `mov55 $sp, $sp' is not allowed in v3. */
218 typedef struct nds32_asm_insn
220 /* Assembled instruction bytes. */
222 /* The opcode structure for this instruction. */
223 struct nds32_opcode *opcode;
224 /* The field need special fix-up, used for relocation. */
225 const struct nds32_field *field;
226 /* Attributes for relocation. */
228 /* Application-dependent data, e.g., expression. */
230 /* Input/output registers. */
234 typedef struct nds32_asm_desc
236 /* The callback provided by assembler user for parse an operand,
237 e.g., parse integer. */
238 int (*parse_operand) (struct nds32_asm_desc *,
239 struct nds32_asm_insn *,
242 /* Result of assembling. */
245 /* The mach for this assembling. */
251 /* The field information for an operand. */
252 typedef struct nds32_field
254 /* Name of the field. */
262 int (*parse) (struct nds32_asm_desc *,
263 struct nds32_asm_insn *,
267 extern void nds32_assemble (nds32_asm_desc_t *, nds32_asm_insn_t *, char *);
268 extern void nds32_asm_init (nds32_asm_desc_t *, int);
270 #define OP6(op6) (N32_OP6_ ## op6 << 25)
272 #define LSMW(sub) (OP6 (LSMW) | N32_LSMW_ ## sub)
273 #define JREG(sub) (OP6 (JREG) | N32_JREG_ ## sub)
274 #define JREG_RET (1 << 5)
275 #define JREG_IFC (1 << 6)
276 #define BR2(sub) (OP6 (BR2) | (N32_BR2_ ## sub << 16))
277 #define SIMD(sub) (OP6 (SIMD) | N32_SIMD_ ## sub)
278 #define ALU1(sub) (OP6 (ALU1) | N32_ALU1_ ## sub)
279 #define ALU2(sub) (OP6 (ALU2) | N32_ALU2_ ## sub)
280 #define MISC(sub) (OP6 (MISC) | N32_MISC_ ## sub)
281 #define MEM(sub) (OP6 (MEM) | N32_MEM_ ## sub)
282 #define FPU_RA_IMMBI(sub) (OP6 (sub) | N32_BIT (12))
283 #define FS1(sub) (OP6 (COP) | N32_FPU_FS1 | (N32_FPU_FS1_ ## sub << 6))
284 #define FS1_F2OP(sub) (OP6 (COP) | N32_FPU_FS1 | (N32_FPU_FS1_F2OP << 6) \
285 | (N32_FPU_FS1_F2OP_ ## sub << 10))
286 #define FS2(sub) (OP6 (COP) | N32_FPU_FS2 | (N32_FPU_FS2_ ## sub << 6))
287 #define FD1(sub) (OP6 (COP) | N32_FPU_FD1 | (N32_FPU_FD1_ ## sub << 6))
288 #define FD1_F2OP(sub) (OP6 (COP) | N32_FPU_FD1 | (N32_FPU_FD1_F2OP << 6) \
289 | (N32_FPU_FD1_F2OP_ ## sub << 10))
290 #define FD2(sub) (OP6 (COP) | N32_FPU_FD2 | (N32_FPU_FD2_ ## sub << 6))
291 #define MFCP(sub) (OP6 (COP) | N32_FPU_MFCP | (N32_FPU_MFCP_ ## sub << 6))
292 #define MFCP_XR(sub) (OP6 (COP) | N32_FPU_MFCP | (N32_FPU_MFCP_XR << 6) \
293 | (N32_FPU_MFCP_XR_ ## sub << 10))
294 #define MTCP(sub) (OP6 (COP) | N32_FPU_MTCP | (N32_FPU_MTCP_ ## sub << 6))
295 #define MTCP_XR(sub) (OP6 (COP) | N32_FPU_MTCP | (N32_FPU_MTCP_XR << 6) \
296 | (N32_FPU_MTCP_XR_ ## sub << 10))
297 #define FPU_MEM(sub) (OP6 (COP) | N32_FPU_ ## sub)
298 #define FPU_MEMBI(sub) (OP6 (COP) | N32_FPU_ ## sub | 0x1 << 7)
299 #define AUDIO(sub) (OP6 (AEXT) | (N32_AEXT_ ## sub << 20))