1 /* tc-nds32.h -- Header file for tc-nds32.c.
2    Copyright (C) 2012-2021 Free Software Foundation, Inc.
3    Contributed by Andes Technology Corporation.
4 
5    This file is part of GAS.
6 
7    GAS 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, or (at your option)
10    any later version.
11 
12    GAS 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.
16 
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21 
22 #ifndef TC_NDS32
23 #define TC_NDS32
24 
25 #include <stdint.h>
26 
27 /* Enum mapping symbol.  */
28 enum mstate
29 {
30   MAP_UNDEFINED = 0,	/* Must be zero, for seginfo in new sections.  */
31   MAP_DATA,
32   MAP_CODE,
33 };
34 #define TC_SEGMENT_INFO_TYPE struct nds32_segment_info_type
35 
36 /* For mapping symbol.  */
37 struct nds32_segment_info_type
38 {
39   enum mstate mapstate;
40 };
41 
42 #define LISTING_HEADER \
43   (target_big_endian ? "NDS32 GAS" : "NDS32 GAS Little Endian")
44 
45 /* The target BFD architecture.  */
46 #define TARGET_ARCH		bfd_arch_nds32
47 
48 /* mapping to mach_table[5] */
49 #define ISA_V1      bfd_mach_n1h
50 #define ISA_V2      bfd_mach_n1h_v2
51 #define ISA_V3      bfd_mach_n1h_v3
52 #define ISA_V3M     bfd_mach_n1h_v3m
53 
54 /* Default to big endian. Please note that for Andes architecture,
55    instructions are always in big-endian format.  */
56 #ifndef TARGET_BYTES_BIG_ENDIAN
57 #define TARGET_BYTES_BIG_ENDIAN	1
58 #endif
59 
60 /* as.c.  */
61 /* Extend GAS command line option handling capability.  */
62 extern int nds32_parse_option (int, const char *);
63 extern void nds32_after_parse_args (void);
64 /* The endianness of the target format may change based on command
65    line arguments.  */
66 extern const char * nds32_target_format (void);
67 
68 #define md_parse_option(optc, optarg)	nds32_parse_option (optc, optarg)
69 #define md_after_parse_args()		nds32_after_parse_args ()
70 #define TARGET_FORMAT nds32_target_format()
71 
72 /* expr.c */
73 extern int nds32_parse_name (char const *, expressionS *, enum expr_mode, char *);
74 extern bool nds32_allow_local_subtract (expressionS *, expressionS *, segT);
75 #define md_parse_name(name, exprP, mode, nextcharP) \
76 	nds32_parse_name (name, exprP, mode, nextcharP)
77 #define md_allow_local_subtract(lhs,rhs,sect)	nds32_allow_local_subtract (lhs, rhs, sect)
78 
79 /* dwarf2dbg.c.  */
80 #define DWARF2_USE_FIXED_ADVANCE_PC		1
81 
82 /* write.c.  */
83 extern long nds32_pcrel_from_section (struct fix *, segT);
84 extern bool nds32_fix_adjustable (struct fix *);
85 extern void nds32_frob_file (void);
86 extern void nds32_post_relax_hook (void);
87 extern void nds32_frob_file_before_fix (void);
88 extern void elf_nds32_final_processing (void);
89 extern int nds32_validate_fix_sub (struct fix *, segT);
90 extern int nds32_force_relocation (struct fix *);
91 extern void nds32_set_section_relocs (asection *, arelent ** , unsigned int);
92 
93 /* Fill in rs_align_code fragments.  TODO: Review this.  */
94 extern void nds32_handle_align (fragS *);
95 extern int nds32_relax_frag (segT, fragS *, long);
96 extern int tc_nds32_regname_to_dw2regnum (char *);
97 extern void tc_nds32_frame_initial_instructions (void);
98 
99 #define MD_PCREL_FROM_SECTION(fix, sect)	nds32_pcrel_from_section (fix, sect)
100 #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG	0
101 #define tc_fix_adjustable(FIX)			nds32_fix_adjustable (FIX)
102 #define md_apply_fix(fixP, addn, seg)		nds32_apply_fix (fixP, addn, seg)
103 #define md_post_relax_hook			nds32_post_relax_hook ()
104 #define tc_frob_file_before_fix()		nds32_frob_file_before_fix ()
105 #define elf_tc_final_processing()		elf_nds32_final_processing ()
106 /* For DIFF relocations.  The default behavior is inconsistent with the
107    asm internal document.  */
108 #define TC_FORCE_RELOCATION_SUB_SAME(FIX, SEC)		\
109   (GENERIC_FORCE_RELOCATION_SUB_SAME (FIX, SEC)		\
110    || TC_FORCE_RELOCATION (FIX))
111 #define TC_FORCE_RELOCATION(fix)		nds32_force_relocation (fix)
112 #define TC_VALIDATE_FIX_SUB(FIX,SEG)		nds32_validate_fix_sub (FIX,SEG)
113 #define SET_SECTION_RELOCS(sec, relocs, n)	nds32_set_section_relocs (sec, relocs, n)
114 /* Values passed to md_apply_fix don't include the symbol value.  */
115 #define MD_APPLY_SYM_VALUE(FIX)			0
116 #define HANDLE_ALIGN(f)				nds32_handle_align (f)
117 #undef DIFF_EXPR_OK				/* They should be fixed in linker.  */
118 #define md_relax_frag(segment, fragP, stretch)	nds32_relax_frag (segment, fragP, stretch)
119 #define WORKING_DOT_WORD			/* We don't need to handle .word strangely.  */
120 /* Using to chain fixup with previous fixup.  */
121 #define TC_FIX_TYPE struct fix *
122 #define TC_INIT_FIX_DATA(fixP)		\
123   do					\
124     {					\
125       fixP->tc_fix_data = NULL;		\
126     }					\
127   while (0)
128 
129 /* read.c.  */
130 /* Extend GAS macro handling capability.  */
131 extern void nds32_macro_start (void);
132 extern void nds32_macro_end (void);
133 extern void nds32_macro_info (void *);
134 extern void nds32_start_line_hook (void);
135 extern void nds32_elf_section_change_hook (void);
136 extern void md_begin (void);
137 extern void md_end (void);
138 extern int nds32_start_label (int, int);
139 extern void nds32_cleanup (void);
140 extern void nds32_flush_pending_output (void);
141 extern void nds32_cons_align (int);
142 extern void nds32_check_label (symbolS *);
143 extern void nds32_frob_label (symbolS *);
144 extern void nds32_pre_do_align (int, char *, int, int);
145 extern void nds32_do_align (int);
146 
147 #define md_macro_start()			nds32_macro_start ()
148 #define md_macro_end()				nds32_macro_end ()
149 #define md_macro_info(args)			nds32_macro_info (args)
150 #define TC_START_LABEL(STR, NUL_CHAR, NEXT_CHAR)	\
151   (NEXT_CHAR == ':' && nds32_start_label (0, 0))
152 #define tc_check_label(label)			nds32_check_label (label)
153 #define tc_frob_label(label)			nds32_frob_label (label)
154 #define md_end					md_end
155 #define md_start_line_hook()			nds32_start_line_hook ()
156 #define md_cons_align(n)			nds32_cons_align (n)
157 /* COLE: TODO: Review md_do_align.  */
158 #define md_do_align(N, FILL, LEN, MAX, LABEL)	\
159   nds32_pre_do_align (N, FILL, LEN, MAX);	\
160   if ((N) > 1 && (subseg_text_p (now_seg)	\
161       || startswith (now_seg->name, ".gcc_except_table"))) \
162     nds32_do_align (N);				\
163   goto LABEL;
164 #define md_elf_section_change_hook()		nds32_elf_section_change_hook ()
165 #define md_flush_pending_output()		nds32_flush_pending_output ()
166 #define md_cleanup()				nds32_cleanup ()
167 #define LOCAL_LABELS_FB				1 /* Permit temporary numeric labels.  */
168 
169 /* frags.c.  */
170 
171 enum FRAG_ATTR
172 {
173   NDS32_FRAG_RELAXABLE  = 0x1,
174   NDS32_FRAG_RELAXED = 0x2,
175   NDS32_FRAG_BRANCH = 0x4,
176   NDS32_FRAG_LABEL = 0x8,
177   NDS32_FRAG_FINAL = 0x10,
178   NDS32_FRAG_RELAXABLE_BRANCH = 0x20,
179   NDS32_FRAG_ALIGN = 0x40
180 };
181 
182 struct nds32_frag_type
183 {
184   relax_substateT flag;
185   struct nds32_opcode *opcode;
186   uint32_t insn;
187   /* To Save previous label fixup if existence.  */
188   struct fix *fixup;
189 };
190 
191 extern void nds32_frag_init (fragS *);
192 
193 #define TC_FRAG_TYPE				struct nds32_frag_type
194 #define TC_FRAG_INIT(fragP, max_bytes)		nds32_frag_init (fragP)
195 
196 /* CFI directive.  */
197 extern void nds32_elf_frame_initial_instructions (void);
198 extern int tc_nds32_regname_to_dw2regnum (char *);
199 
200 #define TARGET_USE_CFIPOP			1
201 #define DWARF2_DEFAULT_RETURN_COLUMN		30
202 #define DWARF2_CIE_DATA_ALIGNMENT		-4
203 #define DWARF2_LINE_MIN_INSN_LENGTH		2
204 
205 #define tc_regname_to_dw2regnum			tc_nds32_regname_to_dw2regnum
206 #define tc_cfi_frame_initial_instructions	tc_nds32_frame_initial_instructions
207 
208 /* COLE: TODO: Review These. They seem to be obsoleted.  */
209 #if 1
210 #define TC_RELOC_RTSYM_LOC_FIXUP(FIX)				\
211    ((FIX)->fx_addsy == NULL					\
212     || (! S_IS_EXTERNAL ((FIX)->fx_addsy)			\
213 	&& ! S_IS_WEAK ((FIX)->fx_addsy)			\
214 	&& S_IS_DEFINED ((FIX)->fx_addsy)			\
215 	&& ! S_IS_COMMON ((FIX)->fx_addsy)))
216 #define TC_HANDLES_FX_DONE
217 #endif
218 
219 /* Because linker may relax the code, assemble-time expression
220    optimization is not allowed.  */
221 #define md_allow_eh_opt 0
222 
223 /* For nds32 relax.  */
224 enum nds32_br_range
225 {
226   BR_RANGE_S256 = 0,
227   BR_RANGE_S16K,
228   BR_RANGE_S64K,
229   BR_RANGE_S16M,
230   BR_RANGE_U4G,
231   BR_RANGE_NUM
232 };
233 
234 enum nds32_ramp
235 {
236   NDS32_CREATE_LABEL = 1,
237   NDS32_RELAX = (1 << 1), /* Obsolete in the future.  */
238   NDS32_ORIGIN = (1 << 2),
239   NDS32_INSN16 = (1 << 3),
240   NDS32_PTR = (1 << 4),
241   NDS32_ABS = (1 << 5),
242   NDS32_HINT = (1 << 6),
243   NDS32_FIX = (1 << 7),
244   NDS32_ADDEND = (1 << 8),
245   NDS32_SYM = (1 << 9),
246   NDS32_PCREL = (1 << 10),
247   NDS32_PTR_PATTERN = (1 << 11),
248   NDS32_PTR_MULTIPLE = (1 << 12),
249   NDS32_GROUP = (1 << 13),
250   NDS32_SYM_DESC_MEM = (1 << 14)
251 };
252 
253 typedef struct nds32_relax_fixup_info
254 {
255   int offset;
256   int size;
257   /* Reverse branch has to jump to the end of instruction pattern.  */
258   int ramp;
259   enum bfd_reloc_code_real r_type;
260 } nds32_relax_fixup_info_t;
261 
262 typedef struct nds32_cond_field
263 {
264   int offset;
265   int bitpos; /* Register position.  */
266   int bitmask; /* Number of register bits.  */
267   bool signed_extend;
268 } nds32_cond_field_t;
269 
270 /* The max relaxation pattern is 20-bytes including the nop.  */
271 #define NDS32_MAXCHAR 20
272 /* In current, the max extended number of instruction for one pseudo instruction
273    is 6, but its number of relocation may be 12.  */
274 #define MAX_RELAX_NUM 6
275 #define MAX_RELAX_FIX 12
276 
277 typedef struct nds32_relax_info
278 {
279   /* Opcode for the instruction.  */
280   const char *opcode;
281   enum nds32_br_range br_range;
282   nds32_cond_field_t cond_field[MAX_RELAX_NUM]; /* TODO: Reuse nds32_field?  */
283   /* Code sequences for different branch range.  */
284   uint32_t relax_code_seq[BR_RANGE_NUM][MAX_RELAX_NUM];
285   nds32_cond_field_t relax_code_condition[BR_RANGE_NUM][MAX_RELAX_NUM];
286   unsigned int relax_code_size[BR_RANGE_NUM];
287   int relax_branch_isize[BR_RANGE_NUM];
288   nds32_relax_fixup_info_t relax_fixup[BR_RANGE_NUM][MAX_RELAX_FIX];
289 } relax_info_t;
290 
291 enum nds32_relax_hint_type
292 {
293   NDS32_RELAX_HINT_NONE = 0,
294   NDS32_RELAX_HINT_LA_FLSI,
295   NDS32_RELAX_HINT_LALS,
296   NDS32_RELAX_HINT_LA_PLT,
297   NDS32_RELAX_HINT_LA_GOT,
298   NDS32_RELAX_HINT_LA_GOTOFF,
299   NDS32_RELAX_HINT_TLS_START = 0x100,
300   NDS32_RELAX_HINT_TLS_LE_LS,
301   NDS32_RELAX_HINT_TLS_IE_LS,
302   NDS32_RELAX_HINT_TLS_IE_LA,
303   NDS32_RELAX_HINT_TLS_IEGP_LA,
304   NDS32_RELAX_HINT_TLS_DESC_LS,
305 };
306 
307 struct nds32_relax_hint_table
308 {
309   enum nds32_relax_hint_type main_type;
310   unsigned int relax_code_size;
311   uint32_t relax_code_seq[MAX_RELAX_NUM];
312   nds32_relax_fixup_info_t relax_fixup[MAX_RELAX_FIX];
313 };
314 
315 #endif /* TC_NDS32 */
316