]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-riscv.c
RISC-V: adjust logic to avoid register name symbols
[thirdparty/binutils-gdb.git] / gas / config / tc-riscv.c
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
3
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
6
7 This file is part of GAS.
8
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
22
23 #include "as.h"
24 #include "config.h"
25 #include "subsegs.h"
26 #include "safe-ctype.h"
27
28 #include "itbl-ops.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
31
32 #include "bfd/elfxx-riscv.h"
33 #include "elf/riscv.h"
34 #include "opcode/riscv.h"
35
36 #include <stdint.h>
37
38 /* Information about an instruction, including its format, operands
39 and fixups. */
40 struct riscv_cl_insn
41 {
42 /* The opcode's entry in riscv_opcodes. */
43 const struct riscv_opcode *insn_mo;
44
45 /* The encoded instruction bits
46 (first bits enough to extract instruction length on a long opcode). */
47 insn_t insn_opcode;
48
49 /* The long encoded instruction bits ([0] is non-zero on a long opcode). */
50 char insn_long_opcode[RISCV_MAX_INSN_LEN];
51
52 /* The frag that contains the instruction. */
53 struct frag *frag;
54
55 /* The offset into FRAG of the first instruction byte. */
56 long where;
57
58 /* The relocs associated with the instruction, if any. */
59 fixS *fixp;
60 };
61
62 /* All RISC-V CSR belong to one of these classes. */
63 enum riscv_csr_class
64 {
65 CSR_CLASS_NONE,
66
67 CSR_CLASS_I,
68 CSR_CLASS_I_32, /* rv32 only */
69 CSR_CLASS_F, /* f-ext only */
70 CSR_CLASS_ZKR, /* zkr only */
71 CSR_CLASS_V, /* rvv only */
72 CSR_CLASS_DEBUG, /* debug CSR */
73 CSR_CLASS_H, /* hypervisor */
74 CSR_CLASS_H_32, /* hypervisor, rv32 only */
75 CSR_CLASS_SMAIA, /* Smaia */
76 CSR_CLASS_SMAIA_32, /* Smaia, rv32 only */
77 CSR_CLASS_SMSTATEEN, /* Smstateen only */
78 CSR_CLASS_SMSTATEEN_32, /* Smstateen RV32 only */
79 CSR_CLASS_SSAIA, /* Ssaia */
80 CSR_CLASS_SSAIA_AND_H, /* Ssaia with H */
81 CSR_CLASS_SSAIA_32, /* Ssaia, rv32 only */
82 CSR_CLASS_SSAIA_AND_H_32, /* Ssaia with H, rv32 only */
83 CSR_CLASS_SSSTATEEN, /* S[ms]stateen only */
84 CSR_CLASS_SSSTATEEN_AND_H, /* S[ms]stateen only (with H) */
85 CSR_CLASS_SSSTATEEN_AND_H_32, /* S[ms]stateen RV32 only (with H) */
86 CSR_CLASS_SSCOFPMF, /* Sscofpmf only */
87 CSR_CLASS_SSCOFPMF_32, /* Sscofpmf RV32 only */
88 CSR_CLASS_SSTC, /* Sstc only */
89 CSR_CLASS_SSTC_AND_H, /* Sstc only (with H) */
90 CSR_CLASS_SSTC_32, /* Sstc RV32 only */
91 CSR_CLASS_SSTC_AND_H_32, /* Sstc RV32 only (with H) */
92 };
93
94 /* This structure holds all restricted conditions for a CSR. */
95 struct riscv_csr_extra
96 {
97 /* Class to which this CSR belongs. Used to decide whether or
98 not this CSR is legal in the current -march context. */
99 enum riscv_csr_class csr_class;
100
101 /* CSR may have differnet numbers in the previous priv spec. */
102 unsigned address;
103
104 /* Record the CSR is defined/valid in which versions. */
105 enum riscv_spec_class define_version;
106
107 /* Record the CSR is aborted/invalid from which versions. If it isn't
108 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
109 enum riscv_spec_class abort_version;
110
111 /* The CSR may have more than one setting. */
112 struct riscv_csr_extra *next;
113 };
114
115 /* This structure contains information about errors that occur within the
116 riscv_ip function */
117 struct riscv_ip_error
118 {
119 /* General error message */
120 const char* msg;
121
122 /* Statement that caused the error */
123 char* statement;
124
125 /* Missing extension that needs to be enabled */
126 const char* missing_ext;
127 };
128
129 #ifndef DEFAULT_ARCH
130 #define DEFAULT_ARCH "riscv64"
131 #endif
132
133 #ifndef DEFAULT_RISCV_ATTR
134 #define DEFAULT_RISCV_ATTR 0
135 #endif
136
137 /* Let riscv_after_parse_args set the default value according to xlen. */
138 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
139 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
140 #endif
141
142 /* Need to sync the version with RISC-V compiler. */
143 #ifndef DEFAULT_RISCV_ISA_SPEC
144 #define DEFAULT_RISCV_ISA_SPEC "20191213"
145 #endif
146
147 #ifndef DEFAULT_RISCV_PRIV_SPEC
148 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
149 #endif
150
151 static const char default_arch[] = DEFAULT_ARCH;
152 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
153 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
154 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
155
156 static unsigned xlen = 0; /* The width of an x-register. */
157 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
158 static bool rve_abi = false;
159 enum float_abi
160 {
161 FLOAT_ABI_DEFAULT = -1,
162 FLOAT_ABI_SOFT,
163 FLOAT_ABI_SINGLE,
164 FLOAT_ABI_DOUBLE,
165 FLOAT_ABI_QUAD
166 };
167 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
168
169 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
170 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
171
172 static unsigned elf_flags = 0;
173
174 static bool probing_insn_operands;
175
176 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
177 Otherwise, return 1. */
178
179 static int
180 riscv_set_default_isa_spec (const char *s)
181 {
182 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
183 RISCV_GET_ISA_SPEC_CLASS (s, class);
184 if (class == ISA_SPEC_CLASS_NONE)
185 {
186 as_bad ("unknown default ISA spec `%s' set by "
187 "-misa-spec or --with-isa-spec", s);
188 return 0;
189 }
190 else
191 default_isa_spec = class;
192 return 1;
193 }
194
195 /* Set the default_priv_spec. Find the privileged elf attributes when
196 the input string is NULL. Return 0 if the spec isn't supported.
197 Otherwise, return 1. */
198
199 static int
200 riscv_set_default_priv_spec (const char *s)
201 {
202 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
203 unsigned major, minor, revision;
204 obj_attribute *attr;
205
206 RISCV_GET_PRIV_SPEC_CLASS (s, class);
207 if (class != PRIV_SPEC_CLASS_NONE)
208 {
209 default_priv_spec = class;
210 return 1;
211 }
212
213 if (s != NULL)
214 {
215 as_bad (_("unknown default privileged spec `%s' set by "
216 "-mpriv-spec or --with-priv-spec"), s);
217 return 0;
218 }
219
220 /* Set the default_priv_spec by the privileged elf attributes. */
221 attr = elf_known_obj_attributes_proc (stdoutput);
222 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
223 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
224 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
225 /* Version 0.0.0 is the default value and meningless. */
226 if (major == 0 && minor == 0 && revision == 0)
227 return 1;
228
229 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
230 if (class != PRIV_SPEC_CLASS_NONE)
231 {
232 default_priv_spec = class;
233 return 1;
234 }
235
236 /* Still can not find the privileged spec class. */
237 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
238 "privileged elf attributes"), major, minor, revision);
239 return 0;
240 }
241
242 /* This is the set of options which the .option pseudo-op may modify. */
243 struct riscv_set_options
244 {
245 int pic; /* Generate position-independent code. */
246 int rvc; /* Generate RVC code. */
247 int relax; /* Emit relocs the linker is allowed to relax. */
248 int arch_attr; /* Emit architecture and privileged elf attributes. */
249 int csr_check; /* Enable the CSR checking. */
250 };
251
252 static struct riscv_set_options riscv_opts =
253 {
254 0, /* pic */
255 0, /* rvc */
256 1, /* relax */
257 DEFAULT_RISCV_ATTR, /* arch_attr */
258 0, /* csr_check */
259 };
260
261 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
262 for elf_flags once we have enabled c extension. */
263
264 static void
265 riscv_set_rvc (bool rvc_value)
266 {
267 if (rvc_value)
268 elf_flags |= EF_RISCV_RVC;
269
270 riscv_opts.rvc = rvc_value;
271 }
272
273 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
274
275 static void
276 riscv_set_tso (void)
277 {
278 elf_flags |= EF_RISCV_TSO;
279 }
280
281 /* The linked list hanging off of .subsets_list records all enabled extensions,
282 which are parsed from the architecture string. The architecture string can
283 be set by the -march option, the elf architecture attributes, and the
284 --with-arch configure option. */
285 static riscv_parse_subset_t riscv_rps_as =
286 {
287 NULL, /* subset_list, we will set it later once
288 riscv_opts_stack is created or updated. */
289 as_bad, /* error_handler. */
290 &xlen, /* xlen. */
291 &default_isa_spec, /* isa_spec. */
292 true, /* check_unknown_prefixed_ext. */
293 };
294
295 /* Update the architecture string in the subset_list. */
296
297 static void
298 riscv_reset_subsets_list_arch_str (void)
299 {
300 riscv_subset_list_t *subsets = riscv_rps_as.subset_list;
301 if (subsets->arch_str != NULL)
302 free ((void *) subsets->arch_str);
303 subsets->arch_str = riscv_arch_str (xlen, subsets);
304 }
305
306 /* This structure is used to hold a stack of .option values. */
307 struct riscv_option_stack
308 {
309 struct riscv_option_stack *next;
310 struct riscv_set_options options;
311 riscv_subset_list_t *subset_list;
312 };
313
314 static struct riscv_option_stack *riscv_opts_stack = NULL;
315
316 /* Set which ISA and extensions are available. */
317
318 static void
319 riscv_set_arch (const char *s)
320 {
321 if (s != NULL && strcmp (s, "") == 0)
322 {
323 as_bad (_("the architecture string of -march and elf architecture "
324 "attributes cannot be empty"));
325 return;
326 }
327
328 if (riscv_rps_as.subset_list == NULL)
329 {
330 riscv_rps_as.subset_list = XNEW (riscv_subset_list_t);
331 riscv_rps_as.subset_list->head = NULL;
332 riscv_rps_as.subset_list->tail = NULL;
333 riscv_rps_as.subset_list->arch_str = NULL;
334 }
335 riscv_release_subset_list (riscv_rps_as.subset_list);
336 riscv_parse_subset (&riscv_rps_as, s);
337 riscv_reset_subsets_list_arch_str ();
338
339 riscv_set_rvc (false);
340 if (riscv_subset_supports (&riscv_rps_as, "c"))
341 riscv_set_rvc (true);
342
343 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
344 riscv_set_tso ();
345 }
346
347 /* Indicate -mabi option is explictly set. */
348 static bool explicit_mabi = false;
349
350 /* Set the abi information. */
351
352 static void
353 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
354 {
355 abi_xlen = new_xlen;
356 float_abi = new_float_abi;
357 rve_abi = rve;
358 }
359
360 /* If the -mabi option isn't set, then set the abi according to the
361 ISA string. Otherwise, check if there is any conflict. */
362
363 static void
364 riscv_set_abi_by_arch (void)
365 {
366 if (!explicit_mabi)
367 {
368 if (riscv_subset_supports (&riscv_rps_as, "q"))
369 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
370 else if (riscv_subset_supports (&riscv_rps_as, "d"))
371 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
372 else if (riscv_subset_supports (&riscv_rps_as, "e"))
373 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
374 else
375 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
376 }
377 else
378 {
379 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
380 if (abi_xlen > xlen)
381 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
382 else if (abi_xlen < xlen)
383 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
384
385 if (riscv_subset_supports (&riscv_rps_as, "e") && !rve_abi)
386 as_bad ("only the ilp32e ABI is supported for e extension");
387
388 if (float_abi == FLOAT_ABI_SINGLE
389 && !riscv_subset_supports (&riscv_rps_as, "f"))
390 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
391 "isn't supported");
392 else if (float_abi == FLOAT_ABI_DOUBLE
393 && !riscv_subset_supports (&riscv_rps_as, "d"))
394 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
395 "isn't supported");
396 else if (float_abi == FLOAT_ABI_QUAD
397 && !riscv_subset_supports (&riscv_rps_as, "q"))
398 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
399 "isn't supported");
400 }
401
402 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
403 elf_flags &= ~EF_RISCV_FLOAT_ABI;
404 elf_flags |= float_abi << 1;
405
406 if (rve_abi)
407 elf_flags |= EF_RISCV_RVE;
408 }
409
410 /* Handle of the OPCODE hash table. */
411 static htab_t op_hash = NULL;
412
413 /* Handle of the type of .insn hash table. */
414 static htab_t insn_type_hash = NULL;
415
416 /* This array holds the chars that always start a comment. If the
417 pre-processor is disabled, these aren't very useful. */
418 const char comment_chars[] = "#";
419
420 /* This array holds the chars that only start a comment at the beginning of
421 a line. If the line seems to have the form '# 123 filename'
422 .line and .file directives will appear in the pre-processed output
423
424 Note that input_file.c hand checks for '#' at the beginning of the
425 first line of the input file. This is because the compiler outputs
426 #NO_APP at the beginning of its output.
427
428 Also note that C style comments are always supported. */
429 const char line_comment_chars[] = "#";
430
431 /* This array holds machine specific line separator characters. */
432 const char line_separator_chars[] = ";";
433
434 /* Chars that can be used to separate mant from exp in floating point nums. */
435 const char EXP_CHARS[] = "eE";
436
437 /* Chars that mean this number is a floating point constant.
438 As in 0f12.456 or 0d1.2345e12. */
439 const char FLT_CHARS[] = "rRsSfFdDxXpPhH";
440
441 /* Indicate we are already assemble any instructions or not. */
442 static bool start_assemble = false;
443
444 /* Indicate ELF attributes are explicitly set. */
445 static bool explicit_attr = false;
446
447 /* Indicate CSR or priv instructions are explicitly used. */
448 static bool explicit_priv_attr = false;
449
450 static char *expr_parse_end;
451
452 /* Macros for encoding relaxation state for RVC branches and far jumps. */
453 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
454 ((relax_substateT) \
455 (0xc0000000 \
456 | ((uncond) ? 1 : 0) \
457 | ((rvc) ? 2 : 0) \
458 | ((length) << 2)))
459 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
460 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
461 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
462 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
463
464 /* Is the given value a sign-extended 32-bit value? */
465 #define IS_SEXT_32BIT_NUM(x) \
466 (((x) &~ (offsetT) 0x7fffffff) == 0 \
467 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
468
469 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
470 #define IS_ZEXT_32BIT_NUM(x) \
471 (((x) &~ (offsetT) 0xffffffff) == 0 \
472 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
473
474 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
475 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
476 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
477 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
478
479 #define INSERT_IMM(n, s, INSN, VALUE) \
480 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
481
482 /* Determine if an instruction matches an opcode. */
483 #define OPCODE_MATCHES(OPCODE, OP) \
484 (((OPCODE) & MASK_##OP) == MATCH_##OP)
485
486 /* Create a new mapping symbol for the transition to STATE. */
487
488 static void
489 make_mapping_symbol (enum riscv_seg_mstate state,
490 valueT value,
491 fragS *frag,
492 const char *arch_str,
493 bool odd_data_padding)
494 {
495 const char *name;
496 char *buff = NULL;
497 switch (state)
498 {
499 case MAP_DATA:
500 name = "$d";
501 break;
502 case MAP_INSN:
503 if (arch_str != NULL)
504 {
505 size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */
506 buff = xmalloc (size);
507 snprintf (buff, size, "$x%s", arch_str);
508 name = buff;
509 }
510 else
511 name = "$x";
512 break;
513 default:
514 abort ();
515 }
516
517 symbolS *symbol = symbol_new (name, now_seg, frag, value);
518 symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL);
519 if (arch_str != NULL)
520 {
521 /* Store current $x+arch into tc_segment_info. */
522 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol;
523 xfree ((void *) buff);
524 }
525
526 /* If .fill or other data filling directive generates zero sized data,
527 then mapping symbol for the following code will have the same value.
528
529 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
530 and .text.zero.fill.last. */
531 symbolS *first = frag->tc_frag_data.first_map_symbol;
532 symbolS *last = frag->tc_frag_data.last_map_symbol;
533 symbolS *removed = NULL;
534 if (value == 0)
535 {
536 if (first != NULL)
537 {
538 know (S_GET_VALUE (first) == S_GET_VALUE (symbol)
539 && first == last);
540 /* Remove the old one. */
541 removed = first;
542 }
543 frag->tc_frag_data.first_map_symbol = symbol;
544 }
545 else if (last != NULL)
546 {
547 /* The mapping symbols should be added in offset order. */
548 know (S_GET_VALUE (last) <= S_GET_VALUE (symbol));
549 /* Remove the old one. */
550 if (S_GET_VALUE (last) == S_GET_VALUE (symbol))
551 removed = last;
552 }
553 frag->tc_frag_data.last_map_symbol = symbol;
554
555 if (removed == NULL)
556 return;
557
558 if (odd_data_padding)
559 {
560 /* If the removed mapping symbol is $x+arch, then add it back to
561 the next $x. */
562 const char *str = strncmp (S_GET_NAME (removed), "$xrv", 4) == 0
563 ? S_GET_NAME (removed) + 2 : NULL;
564 make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str,
565 false/* odd_data_padding */);
566 }
567 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
568 }
569
570 /* Set the mapping state for frag_now. */
571
572 void
573 riscv_mapping_state (enum riscv_seg_mstate to_state,
574 int max_chars,
575 bool fr_align_code)
576 {
577 enum riscv_seg_mstate from_state =
578 seg_info (now_seg)->tc_segment_info_data.map_state;
579 bool reset_seg_arch_str = false;
580
581 if (!SEG_NORMAL (now_seg)
582 /* For now we only add the mapping symbols to text sections.
583 Therefore, the dis-assembler only show the actual contents
584 distribution for text. Other sections will be shown as
585 data without the details. */
586 || !subseg_text_p (now_seg))
587 return;
588
589 /* The mapping symbol should be emitted if not in the right
590 mapping state. */
591 symbolS *seg_arch_symbol =
592 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol;
593 if (to_state == MAP_INSN && seg_arch_symbol == 0)
594 {
595 /* Always add $x+arch at the first instruction of section. */
596 reset_seg_arch_str = true;
597 }
598 else if (seg_arch_symbol != 0
599 && to_state == MAP_INSN
600 && !fr_align_code
601 && strcmp (riscv_rps_as.subset_list->arch_str,
602 S_GET_NAME (seg_arch_symbol) + 2) != 0)
603 {
604 reset_seg_arch_str = true;
605 }
606 else if (from_state == to_state)
607 return;
608
609 valueT value = (valueT) (frag_now_fix () - max_chars);
610 seg_info (now_seg)->tc_segment_info_data.map_state = to_state;
611 const char *arch_str = reset_seg_arch_str
612 ? riscv_rps_as.subset_list->arch_str : NULL;
613 make_mapping_symbol (to_state, value, frag_now, arch_str,
614 false/* odd_data_padding */);
615 }
616
617 /* Add the odd bytes of paddings for riscv_handle_align. */
618
619 static void
620 riscv_add_odd_padding_symbol (fragS *frag)
621 {
622 /* If there was already a mapping symbol, it should be
623 removed in the make_mapping_symbol.
624
625 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
626 make_mapping_symbol (MAP_DATA, frag->fr_fix, frag,
627 NULL/* arch_str */, true/* odd_data_padding */);
628 }
629
630 /* Remove any excess mapping symbols generated for alignment frags in
631 SEC. We may have created a mapping symbol before a zero byte
632 alignment; remove it if there's a mapping symbol after the
633 alignment. */
634
635 static void
636 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
637 asection *sec,
638 void *dummy ATTRIBUTE_UNUSED)
639 {
640 segment_info_type *seginfo = seg_info (sec);
641 fragS *fragp;
642
643 if (seginfo == NULL || seginfo->frchainP == NULL)
644 return;
645
646 for (fragp = seginfo->frchainP->frch_root;
647 fragp != NULL;
648 fragp = fragp->fr_next)
649 {
650 symbolS *last = fragp->tc_frag_data.last_map_symbol;
651 fragS *next = fragp->fr_next;
652
653 if (last == NULL || next == NULL)
654 continue;
655
656 /* Check the last mapping symbol if it is at the boundary of
657 fragment. */
658 if (S_GET_VALUE (last) < next->fr_address)
659 continue;
660 know (S_GET_VALUE (last) == next->fr_address);
661
662 do
663 {
664 symbolS *next_first = next->tc_frag_data.first_map_symbol;
665 if (next_first != NULL)
666 {
667 /* The last mapping symbol overlaps with another one
668 which at the start of the next frag.
669
670 Please see the gas/testsuite/gas/riscv/mapping.s:
671 .text.zero.fill.align.A and .text.zero.fill.align.B. */
672 know (S_GET_VALUE (last) == S_GET_VALUE (next_first));
673 symbolS *removed = last;
674 if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0
675 && strcmp (S_GET_NAME (next_first), "$x") == 0)
676 removed = next_first;
677 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
678 break;
679 }
680
681 if (next->fr_next == NULL)
682 {
683 /* The last mapping symbol is at the end of the section.
684
685 Please see the gas/testsuite/gas/riscv/mapping.s:
686 .text.last.section. */
687 know (next->fr_fix == 0 && next->fr_var == 0);
688 symbol_remove (last, &symbol_rootP, &symbol_lastP);
689 break;
690 }
691
692 /* Since we may have empty frags without any mapping symbols,
693 keep looking until the non-empty frag. */
694 if (next->fr_address != next->fr_next->fr_address)
695 break;
696
697 next = next->fr_next;
698 }
699 while (next != NULL);
700 }
701 }
702
703 /* The default target format to use. */
704
705 const char *
706 riscv_target_format (void)
707 {
708 if (target_big_endian)
709 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
710 else
711 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
712 }
713
714 /* Return the length of instruction INSN. */
715
716 static inline unsigned int
717 insn_length (const struct riscv_cl_insn *insn)
718 {
719 return riscv_insn_length (insn->insn_opcode);
720 }
721
722 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
723
724 static void
725 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
726 {
727 insn->insn_mo = mo;
728 insn->insn_opcode = mo->match;
729 insn->insn_long_opcode[0] = 0;
730 insn->frag = NULL;
731 insn->where = 0;
732 insn->fixp = NULL;
733 }
734
735 /* Install INSN at the location specified by its "frag" and "where" fields. */
736
737 static void
738 install_insn (const struct riscv_cl_insn *insn)
739 {
740 char *f = insn->frag->fr_literal + insn->where;
741 if (insn->insn_long_opcode[0] != 0)
742 memcpy (f, insn->insn_long_opcode, insn_length (insn));
743 else
744 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
745 }
746
747 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
748 and install the opcode in the new location. */
749
750 static void
751 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
752 {
753 insn->frag = frag;
754 insn->where = where;
755 if (insn->fixp != NULL)
756 {
757 insn->fixp->fx_frag = frag;
758 insn->fixp->fx_where = where;
759 }
760 install_insn (insn);
761 }
762
763 /* Add INSN to the end of the output. */
764
765 static void
766 add_fixed_insn (struct riscv_cl_insn *insn)
767 {
768 char *f = frag_more (insn_length (insn));
769 move_insn (insn, frag_now, f - frag_now->fr_literal);
770 }
771
772 static void
773 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
774 relax_substateT subtype, symbolS *symbol, offsetT offset)
775 {
776 frag_grow (max_chars);
777 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
778 frag_var (rs_machine_dependent, max_chars, var,
779 subtype, symbol, offset, NULL);
780 }
781
782 /* Compute the length of a branch sequence, and adjust the stored length
783 accordingly. If FRAGP is NULL, the worst-case length is returned. */
784
785 static unsigned
786 relaxed_branch_length (fragS *fragp, asection *sec, int update)
787 {
788 int jump, rvc, length = 8;
789
790 if (!fragp)
791 return length;
792
793 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
794 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
795 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
796
797 /* Assume jumps are in range; the linker will catch any that aren't. */
798 length = jump ? 4 : 8;
799
800 if (fragp->fr_symbol != NULL
801 && S_IS_DEFINED (fragp->fr_symbol)
802 && !S_IS_WEAK (fragp->fr_symbol)
803 && sec == S_GET_SEGMENT (fragp->fr_symbol))
804 {
805 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
806 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
807 val -= fragp->fr_address + fragp->fr_fix;
808
809 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
810 length = 2;
811 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
812 length = 4;
813 else if (!jump && rvc)
814 length = 6;
815 }
816
817 if (update)
818 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
819
820 return length;
821 }
822
823 /* Information about an opcode name, mnemonics and its value. */
824 struct opcode_name_t
825 {
826 const char *name;
827 unsigned int val;
828 };
829
830 /* List for all supported opcode name. */
831 static const struct opcode_name_t opcode_name_list[] =
832 {
833 {"C0", 0x0},
834 {"C1", 0x1},
835 {"C2", 0x2},
836
837 {"LOAD", 0x03},
838 {"LOAD_FP", 0x07},
839 {"CUSTOM_0", 0x0b},
840 {"MISC_MEM", 0x0f},
841 {"OP_IMM", 0x13},
842 {"AUIPC", 0x17},
843 {"OP_IMM_32", 0x1b},
844 /* 48b 0x1f. */
845
846 {"STORE", 0x23},
847 {"STORE_FP", 0x27},
848 {"CUSTOM_1", 0x2b},
849 {"AMO", 0x2f},
850 {"OP", 0x33},
851 {"LUI", 0x37},
852 {"OP_32", 0x3b},
853 /* 64b 0x3f. */
854
855 {"MADD", 0x43},
856 {"MSUB", 0x47},
857 {"NMADD", 0x4f},
858 {"NMSUB", 0x4b},
859 {"OP_FP", 0x53},
860 {"OP_V", 0x57},
861 {"CUSTOM_2", 0x5b},
862 /* 48b 0x5f. */
863
864 {"BRANCH", 0x63},
865 {"JALR", 0x67},
866 /*reserved 0x5b. */
867 {"JAL", 0x6f},
868 {"SYSTEM", 0x73},
869 /*reserved 0x77. */
870 {"CUSTOM_3", 0x7b},
871 /* >80b 0x7f. */
872
873 {NULL, 0}
874 };
875
876 /* Hash table for lookup opcode name. */
877 static htab_t opcode_names_hash = NULL;
878
879 /* Initialization for hash table of opcode name. */
880
881 static void
882 init_opcode_names_hash (void)
883 {
884 const struct opcode_name_t *opcode;
885
886 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
887 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
888 as_fatal (_("internal: duplicate %s"), opcode->name);
889 }
890
891 /* Find `s` is a valid opcode name or not, return the opcode name info
892 if found. */
893
894 static const struct opcode_name_t *
895 opcode_name_lookup (char **s)
896 {
897 char *e;
898 char save_c;
899 struct opcode_name_t *o;
900
901 /* Find end of name. */
902 e = *s;
903 if (is_name_beginner (*e))
904 ++e;
905 while (is_part_of_name (*e))
906 ++e;
907
908 /* Terminate name. */
909 save_c = *e;
910 *e = '\0';
911
912 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
913
914 /* Advance to next token if one was recognized. */
915 if (o)
916 *s = e;
917
918 *e = save_c;
919 expr_parse_end = e;
920
921 return o;
922 }
923
924 /* All RISC-V registers belong to one of these classes. */
925 enum reg_class
926 {
927 RCLASS_GPR,
928 RCLASS_FPR,
929 RCLASS_VECR,
930 RCLASS_VECM,
931 RCLASS_MAX,
932
933 RCLASS_CSR
934 };
935
936 static htab_t reg_names_hash = NULL;
937 static htab_t csr_extra_hash = NULL;
938
939 #define ENCODE_REG_HASH(cls, n) \
940 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
941 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
942 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
943
944 static void
945 hash_reg_name (enum reg_class class, const char *name, unsigned n)
946 {
947 void *hash = ENCODE_REG_HASH (class, n);
948 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
949 as_fatal (_("internal: duplicate %s"), name);
950 }
951
952 static void
953 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
954 {
955 unsigned i;
956
957 for (i = 0; i < n; i++)
958 hash_reg_name (class, names[i], i);
959 }
960
961 /* Init hash table csr_extra_hash to handle CSR. */
962
963 static void
964 riscv_init_csr_hash (const char *name,
965 unsigned address,
966 enum riscv_csr_class class,
967 enum riscv_spec_class define_version,
968 enum riscv_spec_class abort_version)
969 {
970 struct riscv_csr_extra *entry, *pre_entry;
971 bool need_enrty = true;
972
973 pre_entry = NULL;
974 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
975 while (need_enrty && entry != NULL)
976 {
977 if (entry->csr_class == class
978 && entry->address == address
979 && entry->define_version == define_version
980 && entry->abort_version == abort_version)
981 need_enrty = false;
982 pre_entry = entry;
983 entry = entry->next;
984 }
985
986 /* Duplicate CSR. */
987 if (!need_enrty)
988 return;
989
990 entry = notes_alloc (sizeof (*entry));
991 entry->csr_class = class;
992 entry->address = address;
993 entry->define_version = define_version;
994 entry->abort_version = abort_version;
995 entry->next = NULL;
996
997 if (pre_entry == NULL)
998 str_hash_insert (csr_extra_hash, name, entry, 0);
999 else
1000 pre_entry->next = entry;
1001 }
1002
1003 /* Return the CSR address after checking the ISA dependency and
1004 the privileged spec version.
1005
1006 There are one warning and two errors for CSR,
1007
1008 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1009 or the privileged spec, report warning only if -mcsr-check is set.
1010 Unknown CSR: the CSR has never been defined, report error.
1011 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1012
1013 static unsigned int
1014 riscv_csr_address (const char *csr_name,
1015 struct riscv_csr_extra *entry)
1016 {
1017 struct riscv_csr_extra *saved_entry = entry;
1018 enum riscv_csr_class csr_class = entry->csr_class;
1019 bool need_check_version = false;
1020 bool is_rv32_only = false;
1021 bool is_h_required = false;
1022 const char* extension = NULL;
1023
1024 switch (csr_class)
1025 {
1026 case CSR_CLASS_I_32:
1027 is_rv32_only = true;
1028 /* Fall through. */
1029 case CSR_CLASS_I:
1030 need_check_version = true;
1031 extension = "i";
1032 break;
1033 case CSR_CLASS_H_32:
1034 is_rv32_only = true;
1035 /* Fall through. */
1036 case CSR_CLASS_H:
1037 extension = "h";
1038 break;
1039 case CSR_CLASS_F:
1040 extension = "f";
1041 break;
1042 case CSR_CLASS_ZKR:
1043 extension = "zkr";
1044 break;
1045 case CSR_CLASS_V:
1046 extension = "zve32x";
1047 break;
1048 case CSR_CLASS_SMAIA_32:
1049 is_rv32_only = true;
1050 /* Fall through. */
1051 case CSR_CLASS_SMAIA:
1052 extension = "smaia";
1053 break;
1054 case CSR_CLASS_SMSTATEEN_32:
1055 is_rv32_only = true;
1056 /* Fall through. */
1057 case CSR_CLASS_SMSTATEEN:
1058 extension = "smstateen";
1059 break;
1060 case CSR_CLASS_SSAIA:
1061 case CSR_CLASS_SSAIA_AND_H:
1062 case CSR_CLASS_SSAIA_32:
1063 case CSR_CLASS_SSAIA_AND_H_32:
1064 is_rv32_only = (csr_class == CSR_CLASS_SSAIA_32
1065 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1066 is_h_required = (csr_class == CSR_CLASS_SSAIA_AND_H
1067 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1068 extension = "ssaia";
1069 break;
1070 case CSR_CLASS_SSSTATEEN_AND_H_32:
1071 is_rv32_only = true;
1072 /* Fall through. */
1073 case CSR_CLASS_SSSTATEEN_AND_H:
1074 is_h_required = true;
1075 /* Fall through. */
1076 case CSR_CLASS_SSSTATEEN:
1077 extension = "ssstateen";
1078 break;
1079 case CSR_CLASS_SSCOFPMF_32:
1080 is_rv32_only = true;
1081 /* Fall through. */
1082 case CSR_CLASS_SSCOFPMF:
1083 extension = "sscofpmf";
1084 break;
1085 case CSR_CLASS_SSTC:
1086 case CSR_CLASS_SSTC_AND_H:
1087 case CSR_CLASS_SSTC_32:
1088 case CSR_CLASS_SSTC_AND_H_32:
1089 is_rv32_only = (csr_class == CSR_CLASS_SSTC_32
1090 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1091 is_h_required = (csr_class == CSR_CLASS_SSTC_AND_H
1092 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1093 extension = "sstc";
1094 break;
1095 case CSR_CLASS_DEBUG:
1096 break;
1097 default:
1098 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
1099 }
1100
1101 if (riscv_opts.csr_check)
1102 {
1103 if (is_rv32_only && xlen != 32)
1104 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name);
1105 if (is_h_required && !riscv_subset_supports (&riscv_rps_as, "h"))
1106 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name);
1107
1108 if (extension != NULL
1109 && !riscv_subset_supports (&riscv_rps_as, extension))
1110 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1111 csr_name, extension);
1112 }
1113
1114 while (entry != NULL)
1115 {
1116 if (!need_check_version
1117 || (default_priv_spec >= entry->define_version
1118 && default_priv_spec < entry->abort_version))
1119 {
1120 /* Find the CSR according to the specific version. */
1121 return entry->address;
1122 }
1123 entry = entry->next;
1124 }
1125
1126 /* Can not find the CSR address from the chosen privileged version,
1127 so use the newly defined value. */
1128 if (riscv_opts.csr_check)
1129 {
1130 const char *priv_name = NULL;
1131 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
1132 if (priv_name != NULL)
1133 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1134 csr_name, priv_name);
1135 }
1136
1137 return saved_entry->address;
1138 }
1139
1140 /* Return -1 if the CSR has never been defined. Otherwise, return
1141 the address. */
1142
1143 static unsigned int
1144 reg_csr_lookup_internal (const char *s)
1145 {
1146 struct riscv_csr_extra *r =
1147 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
1148
1149 if (r == NULL)
1150 return -1U;
1151
1152 return riscv_csr_address (s, r);
1153 }
1154
1155 static unsigned int
1156 reg_lookup_internal (const char *s, enum reg_class class)
1157 {
1158 void *r;
1159
1160 if (class == RCLASS_CSR)
1161 return reg_csr_lookup_internal (s);
1162
1163 r = str_hash_find (reg_names_hash, s);
1164 if (r == NULL || DECODE_REG_CLASS (r) != class)
1165 return -1;
1166
1167 if (riscv_subset_supports (&riscv_rps_as, "e")
1168 && class == RCLASS_GPR
1169 && DECODE_REG_NUM (r) > 15)
1170 return -1;
1171
1172 return DECODE_REG_NUM (r);
1173 }
1174
1175 static bool
1176 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1177 {
1178 char *e;
1179 char save_c;
1180 int reg = -1;
1181
1182 /* Find end of name. */
1183 e = *s;
1184 if (is_name_beginner (*e))
1185 ++e;
1186 while (is_part_of_name (*e))
1187 ++e;
1188
1189 /* Terminate name. */
1190 save_c = *e;
1191 *e = '\0';
1192
1193 /* Look for the register. Advance to next token if one was recognized. */
1194 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1195 *s = e;
1196
1197 *e = save_c;
1198 if (regnop)
1199 *regnop = reg;
1200 return reg >= 0;
1201 }
1202
1203 static bool
1204 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
1205 {
1206 const char *p = strchr (*s, ',');
1207 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
1208
1209 if (len == 0)
1210 return false;
1211
1212 for (i = 0; i < size; i++)
1213 if (array[i] != NULL && strncmp (array[i], *s, len) == 0
1214 && array[i][len] == '\0')
1215 {
1216 *regnop = i;
1217 *s += len;
1218 return true;
1219 }
1220
1221 return false;
1222 }
1223
1224 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1225 #define USE_IMM(n, s) \
1226 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1227
1228 /* For consistency checking, verify that all bits are specified either
1229 by the match/mask part of the instruction definition, or by the
1230 operand list. The `length` could be the actual instruction length or
1231 0 for auto-detection. */
1232
1233 static bool
1234 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1235 {
1236 const char *oparg, *opargStart;
1237 insn_t used_bits = opc->mask;
1238 int insn_width;
1239 insn_t required_bits;
1240
1241 if (length == 0)
1242 length = riscv_insn_length (opc->match);
1243 /* We don't support instructions longer than 64-bits yet. */
1244 if (length > 8)
1245 length = 8;
1246 insn_width = 8 * length;
1247
1248 required_bits = ((insn_t)~0ULL) >> (64 - insn_width);
1249
1250 if ((used_bits & opc->match) != (opc->match & required_bits))
1251 {
1252 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1253 opc->name, opc->args);
1254 return false;
1255 }
1256
1257 for (oparg = opc->args; *oparg; ++oparg)
1258 {
1259 opargStart = oparg;
1260 switch (*oparg)
1261 {
1262 case 'C': /* RVC */
1263 switch (*++oparg)
1264 {
1265 case 'U': break; /* CRS1, constrained to equal RD. */
1266 case 'c': break; /* CRS1, constrained to equal sp. */
1267 case 'T': /* CRS2, floating point. */
1268 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1269 case 'S': /* CRS1S, floating point. */
1270 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1271 case 'w': break; /* CRS1S, constrained to equal RD. */
1272 case 'D': /* CRS2S, floating point. */
1273 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1274 case 'x': break; /* CRS2S, constrained to equal RD. */
1275 case 'z': break; /* CRS2S, constrained to be x0. */
1276 case '>': /* CITYPE immediate, compressed shift. */
1277 case 'u': /* CITYPE immediate, compressed lui. */
1278 case 'v': /* CITYPE immediate, li to compressed lui. */
1279 case 'o': /* CITYPE immediate, allow zero. */
1280 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1281 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1282 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1283 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1284 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1285 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1286 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1287 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1288 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1289 /* CLTYPE and CSTYPE have the same immediate encoding. */
1290 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1291 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1292 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1293 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1294 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1295 case 'F': /* Compressed funct for .insn directive. */
1296 switch (*++oparg)
1297 {
1298 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1299 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1300 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1301 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1302 default:
1303 goto unknown_validate_operand;
1304 }
1305 break;
1306 default:
1307 goto unknown_validate_operand;
1308 }
1309 break; /* end RVC */
1310 case 'V': /* RVV */
1311 switch (*++oparg)
1312 {
1313 case 'd':
1314 case 'f': USE_BITS (OP_MASK_VD, OP_SH_VD); break;
1315 case 'e': USE_BITS (OP_MASK_VWD, OP_SH_VWD); break;
1316 case 's': USE_BITS (OP_MASK_VS1, OP_SH_VS1); break;
1317 case 't': USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1318 case 'u': USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1319 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1320 case 'v': USE_BITS (OP_MASK_VD, OP_SH_VD);
1321 USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1322 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1323 case '0': break;
1324 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1325 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1326 case 'i':
1327 case 'j':
1328 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1329 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1330 case 'M': break; /* Macro operand, must be a mask register. */
1331 case 'T': break; /* Macro operand, must be a vector register. */
1332 default:
1333 goto unknown_validate_operand;
1334 }
1335 break; /* end RVV */
1336 case ',': break;
1337 case '(': break;
1338 case ')': break;
1339 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1340 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1341 case 'A': break; /* Macro operand, must be symbol. */
1342 case 'B': break; /* Macro operand, must be symbol or constant. */
1343 case 'c': break; /* Macro operand, must be symbol or constant. */
1344 case 'I': break; /* Macro operand, must be constant. */
1345 case 'D': /* RD, floating point. */
1346 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1347 case 'y': USE_BITS (OP_MASK_BS, OP_SH_BS); break;
1348 case 'Y': USE_BITS (OP_MASK_RNUM, OP_SH_RNUM); break;
1349 case 'Z': /* RS1, CSR number. */
1350 case 'S': /* RS1, floating point. */
1351 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1352 case 'U': /* RS1 and RS2 are the same, floating point. */
1353 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1354 /* Fall through. */
1355 case 'T': /* RS2, floating point. */
1356 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1357 case 'R': /* RS3, floating point. */
1358 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1359 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1360 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1361 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1362 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1363 case 'o': /* ITYPE immediate, load displacement. */
1364 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1365 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1366 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1367 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1368 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1369 case 'z': break; /* Zero immediate. */
1370 case '[': break; /* Unused operand. */
1371 case ']': break; /* Unused operand. */
1372 case '0': break; /* AMO displacement, must to zero. */
1373 case '1': break; /* Relaxation operand. */
1374 case 'F': /* Funct for .insn directive. */
1375 switch (*++oparg)
1376 {
1377 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1378 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1379 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1380 default:
1381 goto unknown_validate_operand;
1382 }
1383 break;
1384 case 'O': /* Opcode for .insn directive. */
1385 switch (*++oparg)
1386 {
1387 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1388 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1389 default:
1390 goto unknown_validate_operand;
1391 }
1392 break;
1393 case 'W': /* Various operands. */
1394 switch (*++oparg)
1395 {
1396 case 'i':
1397 switch (*++oparg)
1398 {
1399 case 'f': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1400 default:
1401 goto unknown_validate_operand;
1402 }
1403 break;
1404 default:
1405 goto unknown_validate_operand;
1406 }
1407 break;
1408 case 'X': /* Integer immediate. */
1409 {
1410 size_t n;
1411 size_t s;
1412
1413 switch (*++oparg)
1414 {
1415 case 'l': /* Literal. */
1416 oparg += strcspn(oparg, ",") - 1;
1417 break;
1418 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
1419 goto use_imm;
1420 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
1421 goto use_imm;
1422 use_imm:
1423 n = strtol (oparg + 1, (char **)&oparg, 10);
1424 if (*oparg != '@')
1425 goto unknown_validate_operand;
1426 s = strtol (oparg + 1, (char **)&oparg, 10);
1427 oparg--;
1428
1429 USE_IMM (n, s);
1430 break;
1431 default:
1432 goto unknown_validate_operand;
1433 }
1434 }
1435 break;
1436 default:
1437 unknown_validate_operand:
1438 as_bad (_("internal: bad RISC-V opcode "
1439 "(unknown operand type `%s'): %s %s"),
1440 opargStart, opc->name, opc->args);
1441 return false;
1442 }
1443 }
1444
1445 if (used_bits != required_bits)
1446 {
1447 as_bad (_("internal: bad RISC-V opcode "
1448 "(bits %#llx undefined or invalid): %s %s"),
1449 (unsigned long long)(used_bits ^ required_bits),
1450 opc->name, opc->args);
1451 return false;
1452 }
1453 return true;
1454 }
1455
1456 #undef USE_BITS
1457
1458 struct percent_op_match
1459 {
1460 const char *str;
1461 bfd_reloc_code_real_type reloc;
1462 };
1463
1464 /* Common hash table initialization function for instruction and .insn
1465 directive. */
1466
1467 static htab_t
1468 init_opcode_hash (const struct riscv_opcode *opcodes,
1469 bool insn_directive_p)
1470 {
1471 int i = 0;
1472 int length;
1473 htab_t hash = str_htab_create ();
1474 while (opcodes[i].name)
1475 {
1476 const char *name = opcodes[i].name;
1477 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1478 as_fatal (_("internal: duplicate %s"), name);
1479
1480 do
1481 {
1482 if (opcodes[i].pinfo != INSN_MACRO)
1483 {
1484 if (insn_directive_p)
1485 length = ((name[0] == 'c') ? 2 : 4);
1486 else
1487 length = 0; /* Let assembler determine the length. */
1488 if (!validate_riscv_insn (&opcodes[i], length))
1489 as_fatal (_("internal: broken assembler. "
1490 "No assembly attempted"));
1491 }
1492 else
1493 gas_assert (!insn_directive_p);
1494 ++i;
1495 }
1496 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1497 }
1498
1499 return hash;
1500 }
1501
1502 /* This function is called once, at assembler startup time. It should set up
1503 all the tables, etc. that the MD part of the assembler will need. */
1504
1505 void
1506 md_begin (void)
1507 {
1508 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1509
1510 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1511 as_warn (_("could not set architecture and machine"));
1512
1513 op_hash = init_opcode_hash (riscv_opcodes, false);
1514 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1515
1516 reg_names_hash = str_htab_create ();
1517 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1518 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1519 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1520 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1521 hash_reg_names (RCLASS_VECR, riscv_vecr_names_numeric, NVECR);
1522 hash_reg_names (RCLASS_VECM, riscv_vecm_names_numeric, NVECM);
1523 /* Add "fp" as an alias for "s0". */
1524 hash_reg_name (RCLASS_GPR, "fp", 8);
1525
1526 /* Create and insert CSR hash tables. */
1527 csr_extra_hash = str_htab_create ();
1528 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1529 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1530 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1531 DECLARE_CSR(name, num, class, define_version, abort_version);
1532 #include "opcode/riscv-opc.h"
1533 #undef DECLARE_CSR
1534
1535 opcode_names_hash = str_htab_create ();
1536 init_opcode_names_hash ();
1537
1538 /* Set the default alignment for the text section. */
1539 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1540 }
1541
1542 static insn_t
1543 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1544 {
1545 switch (reloc_type)
1546 {
1547 case BFD_RELOC_32:
1548 return value;
1549
1550 case BFD_RELOC_RISCV_HI20:
1551 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1552
1553 case BFD_RELOC_RISCV_LO12_S:
1554 return ENCODE_STYPE_IMM (value);
1555
1556 case BFD_RELOC_RISCV_LO12_I:
1557 return ENCODE_ITYPE_IMM (value);
1558
1559 default:
1560 abort ();
1561 }
1562 }
1563
1564 /* Output an instruction. IP is the instruction information.
1565 ADDRESS_EXPR is an operand of the instruction to be used with
1566 RELOC_TYPE. */
1567
1568 static void
1569 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1570 bfd_reloc_code_real_type reloc_type)
1571 {
1572 dwarf2_emit_insn (0);
1573
1574 if (reloc_type != BFD_RELOC_UNUSED)
1575 {
1576 reloc_howto_type *howto;
1577
1578 gas_assert (address_expr);
1579 if (reloc_type == BFD_RELOC_12_PCREL
1580 || reloc_type == BFD_RELOC_RISCV_JMP)
1581 {
1582 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1583 int best_case = insn_length (ip);
1584 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1585
1586 if (now_seg == absolute_section)
1587 {
1588 as_bad (_("relaxable branches not supported in absolute section"));
1589 return;
1590 }
1591
1592 add_relaxed_insn (ip, worst_case, best_case,
1593 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1594 address_expr->X_add_symbol,
1595 address_expr->X_add_number);
1596 return;
1597 }
1598 else
1599 {
1600 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1601 if (howto == NULL)
1602 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1603 reloc_type);
1604
1605 ip->fixp = fix_new_exp (ip->frag, ip->where,
1606 bfd_get_reloc_size (howto),
1607 address_expr, false, reloc_type);
1608
1609 ip->fixp->fx_tcbit = riscv_opts.relax;
1610 }
1611 }
1612
1613 add_fixed_insn (ip);
1614
1615 /* We need to start a new frag after any instruction that can be
1616 optimized away or compressed by the linker during relaxation, to prevent
1617 the assembler from computing static offsets across such an instruction.
1618 This is necessary to get correct EH info. */
1619 if (reloc_type == BFD_RELOC_RISCV_HI20
1620 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1621 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1622 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1623 {
1624 frag_wane (frag_now);
1625 frag_new (0);
1626 }
1627 }
1628
1629 /* Build an instruction created by a macro expansion. This is passed
1630 a pointer to the count of instructions created so far, an expression,
1631 the name of the instruction to build, an operand format string, and
1632 corresponding arguments. */
1633
1634 static void
1635 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1636 {
1637 const struct riscv_opcode *mo;
1638 struct riscv_cl_insn insn;
1639 bfd_reloc_code_real_type r;
1640 va_list args;
1641 const char *fmtStart;
1642
1643 va_start (args, fmt);
1644
1645 r = BFD_RELOC_UNUSED;
1646 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1647 gas_assert (mo);
1648
1649 /* Find a non-RVC variant of the instruction. append_insn will compress
1650 it if possible. */
1651 while (riscv_insn_length (mo->match) < 4)
1652 mo++;
1653 gas_assert (strcmp (name, mo->name) == 0);
1654
1655 create_insn (&insn, mo);
1656 for (;; ++fmt)
1657 {
1658 fmtStart = fmt;
1659 switch (*fmt)
1660 {
1661 case 'V': /* RVV */
1662 switch (*++fmt)
1663 {
1664 case 'd':
1665 INSERT_OPERAND (VD, insn, va_arg (args, int));
1666 continue;
1667 case 's':
1668 INSERT_OPERAND (VS1, insn, va_arg (args, int));
1669 continue;
1670 case 't':
1671 INSERT_OPERAND (VS2, insn, va_arg (args, int));
1672 continue;
1673 case 'm':
1674 {
1675 int reg = va_arg (args, int);
1676 if (reg == -1)
1677 {
1678 INSERT_OPERAND (VMASK, insn, 1);
1679 continue;
1680 }
1681 else if (reg == 0)
1682 {
1683 INSERT_OPERAND (VMASK, insn, 0);
1684 continue;
1685 }
1686 else
1687 goto unknown_macro_argument;
1688 }
1689 default:
1690 goto unknown_macro_argument;
1691 }
1692 break;
1693
1694 case 'd':
1695 INSERT_OPERAND (RD, insn, va_arg (args, int));
1696 continue;
1697 case 's':
1698 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1699 continue;
1700 case 't':
1701 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1702 continue;
1703
1704 case 'j':
1705 case 'u':
1706 case 'q':
1707 gas_assert (ep != NULL);
1708 r = va_arg (args, int);
1709 continue;
1710
1711 case '\0':
1712 break;
1713 case ',':
1714 continue;
1715 default:
1716 unknown_macro_argument:
1717 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
1718 }
1719 break;
1720 }
1721 va_end (args);
1722 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1723
1724 append_insn (&insn, ep, r);
1725 }
1726
1727 /* Build an instruction created by a macro expansion. Like md_assemble but
1728 accept a printf-style format string and arguments. */
1729
1730 static void
1731 md_assemblef (const char *format, ...)
1732 {
1733 char *buf = NULL;
1734 va_list ap;
1735 int r;
1736
1737 va_start (ap, format);
1738
1739 r = vasprintf (&buf, format, ap);
1740
1741 if (r < 0)
1742 as_fatal (_("internal: vasprintf failed"));
1743
1744 md_assemble (buf);
1745 free(buf);
1746
1747 va_end (ap);
1748 }
1749
1750 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1751 unset. */
1752
1753 static void
1754 normalize_constant_expr (expressionS *ex)
1755 {
1756 if (xlen > 32)
1757 return;
1758 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
1759 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
1760 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
1761 - 0x80000000);
1762 }
1763
1764 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1765 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1766
1767 static void
1768 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1769 bool maybe_csr)
1770 {
1771 if (ex->X_op == O_big)
1772 as_bad (_("unsupported large constant"));
1773 else if (maybe_csr && ex->X_op == O_symbol)
1774 as_bad (_("unknown CSR `%s'"),
1775 S_GET_NAME (ex->X_add_symbol));
1776 else if (ex->X_op != O_constant)
1777 as_bad (_("instruction %s requires absolute expression"),
1778 ip->insn_mo->name);
1779 normalize_constant_expr (ex);
1780 }
1781
1782 static symbolS *
1783 make_internal_label (void)
1784 {
1785 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1786 frag_now_fix ());
1787 }
1788
1789 /* Load an entry from the GOT. */
1790
1791 static void
1792 pcrel_access (int destreg, int tempreg, expressionS *ep,
1793 const char *lo_insn, const char *lo_pattern,
1794 bfd_reloc_code_real_type hi_reloc,
1795 bfd_reloc_code_real_type lo_reloc)
1796 {
1797 expressionS ep2;
1798 ep2.X_op = O_symbol;
1799 ep2.X_add_symbol = make_internal_label ();
1800 ep2.X_add_number = 0;
1801
1802 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1803 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1804 }
1805
1806 static void
1807 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1808 bfd_reloc_code_real_type hi_reloc,
1809 bfd_reloc_code_real_type lo_reloc)
1810 {
1811 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1812 }
1813
1814 static void
1815 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1816 bfd_reloc_code_real_type hi_reloc,
1817 bfd_reloc_code_real_type lo_reloc)
1818 {
1819 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1820 }
1821
1822 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1823
1824 static void
1825 riscv_call (int destreg, int tempreg, expressionS *ep,
1826 bfd_reloc_code_real_type reloc)
1827 {
1828 /* Ensure the jalr is emitted to the same frag as the auipc. */
1829 frag_grow (8);
1830 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1831 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1832 /* See comment at end of append_insn. */
1833 frag_wane (frag_now);
1834 frag_new (0);
1835 }
1836
1837 /* Load an integer constant into a register. */
1838
1839 static void
1840 load_const (int reg, expressionS *ep)
1841 {
1842 int shift = RISCV_IMM_BITS;
1843 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
1844 expressionS upper = *ep, lower = *ep;
1845 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
1846 upper.X_add_number -= lower.X_add_number;
1847
1848 if (ep->X_op != O_constant)
1849 {
1850 as_bad (_("unsupported large constant"));
1851 return;
1852 }
1853
1854 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
1855 {
1856 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1857 while (((upper.X_add_number >> shift) & 1) == 0)
1858 shift++;
1859
1860 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1861 load_const (reg, &upper);
1862
1863 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1864 if (lower.X_add_number != 0)
1865 md_assemblef ("addi x%d, x%d, %" PRId64, reg, reg,
1866 (int64_t) lower.X_add_number);
1867 }
1868 else
1869 {
1870 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1871 int hi_reg = 0;
1872
1873 if (upper.X_add_number != 0)
1874 {
1875 /* Discard low part and zero-extend upper immediate. */
1876 upper_imm = ((uint32_t)upper.X_add_number >> shift);
1877
1878 md_assemblef ("lui x%d, 0x%" PRIx64, reg, (uint64_t) upper_imm);
1879 hi_reg = reg;
1880 }
1881
1882 if (lower.X_add_number != 0 || hi_reg == 0)
1883 md_assemblef ("%s x%d, x%d, %" PRId64, ADD32_INSN, reg, hi_reg,
1884 (int64_t) lower.X_add_number);
1885 }
1886 }
1887
1888 /* Zero extend and sign extend byte/half-word/word. */
1889
1890 static void
1891 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
1892 {
1893 if (sign)
1894 {
1895 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1896 md_assemblef ("srai x%d, x%d, 0x%x", destreg, destreg, shift);
1897 }
1898 else
1899 {
1900 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1901 md_assemblef ("srli x%d, x%d, 0x%x", destreg, destreg, shift);
1902 }
1903 }
1904
1905 /* Expand RISC-V Vector macros into one or more instructions. */
1906
1907 static void
1908 vector_macro (struct riscv_cl_insn *ip)
1909 {
1910 int vd = (ip->insn_opcode >> OP_SH_VD) & OP_MASK_VD;
1911 int vs1 = (ip->insn_opcode >> OP_SH_VS1) & OP_MASK_VS1;
1912 int vs2 = (ip->insn_opcode >> OP_SH_VS2) & OP_MASK_VS2;
1913 int vm = (ip->insn_opcode >> OP_SH_VMASK) & OP_MASK_VMASK;
1914 int vtemp = (ip->insn_opcode >> OP_SH_VFUNCT6) & OP_MASK_VFUNCT6;
1915 int mask = ip->insn_mo->mask;
1916
1917 switch (mask)
1918 {
1919 case M_VMSGE:
1920 if (vm)
1921 {
1922 /* Unmasked. */
1923 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1924 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1925 break;
1926 }
1927 if (vtemp != 0)
1928 {
1929 /* Masked. Have vtemp to avoid overlap constraints. */
1930 if (vd == vm)
1931 {
1932 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1933 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1934 }
1935 else
1936 {
1937 /* Preserve the value of vd if not updating by vm. */
1938 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1939 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1940 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
1941 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
1942 }
1943 }
1944 else if (vd != vm)
1945 {
1946 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1947 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
1948 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
1949 }
1950 else
1951 as_bad (_("must provide temp if destination overlaps mask"));
1952 break;
1953
1954 case M_VMSGEU:
1955 if (vm)
1956 {
1957 /* Unmasked. */
1958 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1959 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1960 break;
1961 }
1962 if (vtemp != 0)
1963 {
1964 /* Masked. Have vtemp to avoid overlap constraints. */
1965 if (vd == vm)
1966 {
1967 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1968 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1969 }
1970 else
1971 {
1972 /* Preserve the value of vd if not updating by vm. */
1973 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1974 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1975 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
1976 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
1977 }
1978 }
1979 else if (vd != vm)
1980 {
1981 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1982 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
1983 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
1984 }
1985 else
1986 as_bad (_("must provide temp if destination overlaps mask"));
1987 break;
1988
1989 default:
1990 break;
1991 }
1992 }
1993
1994 /* Expand RISC-V assembly macros into one or more instructions. */
1995
1996 static void
1997 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1998 bfd_reloc_code_real_type *imm_reloc)
1999 {
2000 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
2001 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
2002 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
2003 int mask = ip->insn_mo->mask;
2004
2005 switch (mask)
2006 {
2007 case M_LI:
2008 load_const (rd, imm_expr);
2009 break;
2010
2011 case M_LA:
2012 case M_LLA:
2013 /* Load the address of a symbol into a register. */
2014 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
2015 as_bad (_("offset too large"));
2016
2017 if (imm_expr->X_op == O_constant)
2018 load_const (rd, imm_expr);
2019 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol. */
2020 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2021 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2022 else /* Local PIC symbol, or any non-PIC symbol. */
2023 pcrel_load (rd, rd, imm_expr, "addi",
2024 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2025 break;
2026
2027 case M_LA_TLS_GD:
2028 pcrel_load (rd, rd, imm_expr, "addi",
2029 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2030 break;
2031
2032 case M_LA_TLS_IE:
2033 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2034 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2035 break;
2036
2037 case M_LB:
2038 pcrel_load (rd, rd, imm_expr, "lb",
2039 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2040 break;
2041
2042 case M_LBU:
2043 pcrel_load (rd, rd, imm_expr, "lbu",
2044 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2045 break;
2046
2047 case M_LH:
2048 pcrel_load (rd, rd, imm_expr, "lh",
2049 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2050 break;
2051
2052 case M_LHU:
2053 pcrel_load (rd, rd, imm_expr, "lhu",
2054 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2055 break;
2056
2057 case M_LW:
2058 pcrel_load (rd, rd, imm_expr, "lw",
2059 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2060 break;
2061
2062 case M_LWU:
2063 pcrel_load (rd, rd, imm_expr, "lwu",
2064 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2065 break;
2066
2067 case M_LD:
2068 pcrel_load (rd, rd, imm_expr, "ld",
2069 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2070 break;
2071
2072 case M_FLW:
2073 pcrel_load (rd, rs1, imm_expr, "flw",
2074 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2075 break;
2076
2077 case M_FLD:
2078 pcrel_load (rd, rs1, imm_expr, "fld",
2079 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2080 break;
2081
2082 case M_SB:
2083 pcrel_store (rs2, rs1, imm_expr, "sb",
2084 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2085 break;
2086
2087 case M_SH:
2088 pcrel_store (rs2, rs1, imm_expr, "sh",
2089 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2090 break;
2091
2092 case M_SW:
2093 pcrel_store (rs2, rs1, imm_expr, "sw",
2094 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2095 break;
2096
2097 case M_SD:
2098 pcrel_store (rs2, rs1, imm_expr, "sd",
2099 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2100 break;
2101
2102 case M_FSW:
2103 pcrel_store (rs2, rs1, imm_expr, "fsw",
2104 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2105 break;
2106
2107 case M_FSD:
2108 pcrel_store (rs2, rs1, imm_expr, "fsd",
2109 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2110 break;
2111
2112 case M_CALL:
2113 riscv_call (rd, rs1, imm_expr, *imm_reloc);
2114 break;
2115
2116 case M_ZEXTH:
2117 riscv_ext (rd, rs1, xlen - 16, false);
2118 break;
2119
2120 case M_ZEXTW:
2121 riscv_ext (rd, rs1, xlen - 32, false);
2122 break;
2123
2124 case M_SEXTB:
2125 riscv_ext (rd, rs1, xlen - 8, true);
2126 break;
2127
2128 case M_SEXTH:
2129 riscv_ext (rd, rs1, xlen - 16, true);
2130 break;
2131
2132 case M_VMSGE:
2133 case M_VMSGEU:
2134 vector_macro (ip);
2135 break;
2136
2137 case M_FLH:
2138 pcrel_load (rd, rs1, imm_expr, "flh",
2139 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2140 break;
2141 case M_FSH:
2142 pcrel_store (rs2, rs1, imm_expr, "fsh",
2143 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2144 break;
2145
2146 default:
2147 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
2148 break;
2149 }
2150 }
2151
2152 static const struct percent_op_match percent_op_utype[] =
2153 {
2154 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
2155 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
2156 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
2157 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
2158 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
2159 {"hi", BFD_RELOC_RISCV_HI20},
2160 {0, 0}
2161 };
2162
2163 static const struct percent_op_match percent_op_itype[] =
2164 {
2165 {"lo", BFD_RELOC_RISCV_LO12_I},
2166 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
2167 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
2168 {0, 0}
2169 };
2170
2171 static const struct percent_op_match percent_op_stype[] =
2172 {
2173 {"lo", BFD_RELOC_RISCV_LO12_S},
2174 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
2175 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
2176 {0, 0}
2177 };
2178
2179 static const struct percent_op_match percent_op_rtype[] =
2180 {
2181 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
2182 {0, 0}
2183 };
2184
2185 static const struct percent_op_match percent_op_null[] =
2186 {
2187 {0, 0}
2188 };
2189
2190 /* Return true if *STR points to a relocation operator. When returning true,
2191 move *STR over the operator and store its relocation code in *RELOC.
2192 Leave both *STR and *RELOC alone when returning false. */
2193
2194 static bool
2195 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
2196 const struct percent_op_match *percent_op)
2197 {
2198 for ( ; percent_op->str; percent_op++)
2199 if (strncasecmp (*str + 1, percent_op->str, strlen (percent_op->str)) == 0)
2200 {
2201 size_t len = 1 + strlen (percent_op->str);
2202
2203 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
2204 continue;
2205
2206 *str += len;
2207 *reloc = percent_op->reloc;
2208
2209 /* Check whether the output BFD supports this relocation.
2210 If not, issue an error and fall back on something safe. */
2211 if (*reloc != BFD_RELOC_UNUSED
2212 && !bfd_reloc_type_lookup (stdoutput, *reloc))
2213 {
2214 as_bad ("internal: relocation %s isn't supported by the "
2215 "current ABI", percent_op->str);
2216 *reloc = BFD_RELOC_UNUSED;
2217 }
2218 return true;
2219 }
2220 return false;
2221 }
2222
2223 static void
2224 my_getExpression (expressionS *ep, char *str)
2225 {
2226 char *save_in;
2227
2228 save_in = input_line_pointer;
2229 input_line_pointer = str;
2230 expression (ep);
2231 expr_parse_end = input_line_pointer;
2232 input_line_pointer = save_in;
2233 }
2234
2235 /* Parse string STR as a 16-bit relocatable operand. Store the
2236 expression in *EP and the relocation, if any, in RELOC.
2237 Return the number of relocation operators used (0 or 1).
2238
2239 On exit, EXPR_PARSE_END points to the first character after the
2240 expression. */
2241
2242 static size_t
2243 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2244 char *str, const struct percent_op_match *percent_op)
2245 {
2246 size_t reloc_index;
2247 unsigned crux_depth, str_depth;
2248 bool orig_probing = probing_insn_operands;
2249 char *crux;
2250
2251 /* Search for the start of the main expression.
2252
2253 End the loop with CRUX pointing to the start of the main expression and
2254 with CRUX_DEPTH containing the number of open brackets at that point. */
2255 reloc_index = -1;
2256 str_depth = 0;
2257 do
2258 {
2259 reloc_index++;
2260 crux = str;
2261 crux_depth = str_depth;
2262
2263 /* Skip over whitespace and brackets, keeping count of the number
2264 of brackets. */
2265 while (*str == ' ' || *str == '\t' || *str == '(')
2266 if (*str++ == '(')
2267 str_depth++;
2268 }
2269 while (*str == '%'
2270 && reloc_index < 1
2271 && parse_relocation (&str, reloc, percent_op));
2272
2273 if (*str == '%')
2274 {
2275 /* expression() will choke on anything looking like an (unrecognized)
2276 relocation specifier. Don't even call it, avoiding multiple (and
2277 perhaps redundant) error messages; our caller will issue one. */
2278 ep->X_op = O_illegal;
2279 return 0;
2280 }
2281
2282 /* Anything inside parentheses or subject to a relocation operator cannot
2283 be a register and hence can be treated the same as operands to
2284 directives (other than .insn). */
2285 if (str_depth || reloc_index)
2286 probing_insn_operands = false;
2287
2288 my_getExpression (ep, crux);
2289 str = expr_parse_end;
2290
2291 probing_insn_operands = orig_probing;
2292
2293 /* Match every open bracket. */
2294 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2295 if (*str++ == ')')
2296 crux_depth--;
2297
2298 if (crux_depth > 0)
2299 as_bad ("unclosed '('");
2300
2301 expr_parse_end = str;
2302
2303 return reloc_index;
2304 }
2305
2306 /* Parse opcode name, could be an mnemonics or number. */
2307
2308 static size_t
2309 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2310 char *str)
2311 {
2312 const struct opcode_name_t *o = opcode_name_lookup (&str);
2313
2314 if (o != NULL)
2315 {
2316 ep->X_op = O_constant;
2317 ep->X_add_number = o->val;
2318 return 0;
2319 }
2320
2321 return my_getSmallExpression (ep, reloc, str, percent_op_null);
2322 }
2323
2324 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2325 On exit, EXPR_PARSE_END points to the first character after the
2326 expression. */
2327
2328 static void
2329 my_getVsetvliExpression (expressionS *ep, char *str)
2330 {
2331 unsigned int vsew_value = 0, vlmul_value = 0;
2332 unsigned int vta_value = 0, vma_value = 0;
2333 bfd_boolean vsew_found = FALSE, vlmul_found = FALSE;
2334 bfd_boolean vta_found = FALSE, vma_found = FALSE;
2335
2336 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew), &vsew_value))
2337 {
2338 if (*str == ',')
2339 ++str;
2340 if (vsew_found)
2341 as_bad (_("multiple vsew constants"));
2342 vsew_found = TRUE;
2343 }
2344 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2345 {
2346 if (*str == ',')
2347 ++str;
2348 if (vlmul_found)
2349 as_bad (_("multiple vlmul constants"));
2350 vlmul_found = TRUE;
2351 }
2352 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2353 {
2354 if (*str == ',')
2355 ++str;
2356 if (vta_found)
2357 as_bad (_("multiple vta constants"));
2358 vta_found = TRUE;
2359 }
2360 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2361 {
2362 if (*str == ',')
2363 ++str;
2364 if (vma_found)
2365 as_bad (_("multiple vma constants"));
2366 vma_found = TRUE;
2367 }
2368
2369 if (vsew_found || vlmul_found || vta_found || vma_found)
2370 {
2371 ep->X_op = O_constant;
2372 ep->X_add_number = (vlmul_value << OP_SH_VLMUL)
2373 | (vsew_value << OP_SH_VSEW)
2374 | (vta_value << OP_SH_VTA)
2375 | (vma_value << OP_SH_VMA);
2376 expr_parse_end = str;
2377 }
2378 else
2379 {
2380 my_getExpression (ep, str);
2381 str = expr_parse_end;
2382 }
2383 }
2384
2385 /* Detect and handle implicitly zero load-store offsets. For example,
2386 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2387 an implicit offset was detected. */
2388
2389 static bool
2390 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
2391 {
2392 /* Check whether there is only a single bracketed expression left.
2393 If so, it must be the base register and the constant must be zero. */
2394 if (*s == '(' && strchr (s + 1, '(') == 0)
2395 {
2396 ep->X_op = O_constant;
2397 ep->X_add_number = 0;
2398 return true;
2399 }
2400
2401 return false;
2402 }
2403
2404 /* All RISC-V CSR instructions belong to one of these classes. */
2405 enum csr_insn_type
2406 {
2407 INSN_NOT_CSR,
2408 INSN_CSRRW,
2409 INSN_CSRRS,
2410 INSN_CSRRC
2411 };
2412
2413 /* Return which CSR instruction is checking. */
2414
2415 static enum csr_insn_type
2416 riscv_csr_insn_type (insn_t insn)
2417 {
2418 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
2419 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
2420 return INSN_CSRRW;
2421 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2422 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2423 return INSN_CSRRS;
2424 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2425 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2426 return INSN_CSRRC;
2427 else
2428 return INSN_NOT_CSR;
2429 }
2430
2431 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2432 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2433 CSR address is 0x3. */
2434
2435 static bool
2436 riscv_csr_read_only_check (insn_t insn)
2437 {
2438 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
2439 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
2440 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
2441 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
2442
2443 if (readonly
2444 && (((csr_insn == INSN_CSRRS
2445 || csr_insn == INSN_CSRRC)
2446 && rs1 != 0)
2447 || csr_insn == INSN_CSRRW))
2448 return false;
2449
2450 return true;
2451 }
2452
2453 /* Return true if it is a privileged instruction. Otherwise, return false.
2454
2455 uret is actually a N-ext instruction. So it is better to regard it as
2456 an user instruction rather than the priv instruction.
2457
2458 hret is used to return from traps in H-mode. H-mode is removed since
2459 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2460 Therefore, hret should be controlled by the hypervisor spec rather than
2461 priv spec in the future.
2462
2463 dret is defined in the debug spec, so it should be checked in the future,
2464 too. */
2465
2466 static bool
2467 riscv_is_priv_insn (insn_t insn)
2468 {
2469 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
2470 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
2471 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
2472 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
2473 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2474 check it here to keep the compatible. */
2475 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
2476 }
2477
2478 static symbolS *deferred_sym_rootP;
2479 static symbolS *deferred_sym_lastP;
2480 /* Since symbols can't easily be freed, try to recycle ones which weren't
2481 committed. */
2482 static symbolS *orphan_sym_rootP;
2483 static symbolS *orphan_sym_lastP;
2484
2485 /* This routine assembles an instruction into its binary format. As a
2486 side effect, it sets the global variable imm_reloc to the type of
2487 relocation to do if one of the operands is an address expression. */
2488
2489 static struct riscv_ip_error
2490 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
2491 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
2492 {
2493 /* The operand string defined in the riscv_opcodes. */
2494 const char *oparg, *opargStart;
2495 /* The parsed operands from assembly. */
2496 char *asarg, *asargStart;
2497 char save_c = 0;
2498 struct riscv_opcode *insn;
2499 unsigned int regno;
2500 const struct percent_op_match *p;
2501 struct riscv_ip_error error;
2502 error.msg = "unrecognized opcode";
2503 error.statement = str;
2504 error.missing_ext = NULL;
2505 /* Indicate we are assembling instruction with CSR. */
2506 bool insn_with_csr = false;
2507
2508 /* Parse the name of the instruction. Terminate the string if whitespace
2509 is found so that str_hash_find only sees the name part of the string. */
2510 for (asarg = str; *asarg!= '\0'; ++asarg)
2511 if (ISSPACE (*asarg))
2512 {
2513 save_c = *asarg;
2514 *asarg++ = '\0';
2515 break;
2516 }
2517
2518 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2519
2520 probing_insn_operands = true;
2521
2522 asargStart = asarg;
2523 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2524 {
2525 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2526 continue;
2527
2528 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2529 {
2530 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2531 insn->insn_class);
2532 continue;
2533 }
2534
2535 /* Reset error message of the previous round. */
2536 error.msg = _("illegal operands");
2537 error.missing_ext = NULL;
2538
2539 /* Purge deferred symbols from the previous round, if any. */
2540 while (deferred_sym_rootP)
2541 {
2542 symbolS *sym = deferred_sym_rootP;
2543
2544 symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP);
2545 symbol_append (sym, orphan_sym_lastP, &orphan_sym_rootP,
2546 &orphan_sym_lastP);
2547 }
2548
2549 create_insn (ip, insn);
2550
2551 imm_expr->X_op = O_absent;
2552 *imm_reloc = BFD_RELOC_UNUSED;
2553 p = percent_op_null;
2554
2555 for (oparg = insn->args;; ++oparg)
2556 {
2557 opargStart = oparg;
2558 asarg += strspn (asarg, " \t");
2559 switch (*oparg)
2560 {
2561 case '\0': /* End of args. */
2562 if (insn->pinfo != INSN_MACRO)
2563 {
2564 if (!insn->match_func (insn, ip->insn_opcode))
2565 break;
2566
2567 /* For .insn, insn->match and insn->mask are 0. */
2568 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2569 ? ip->insn_opcode
2570 : insn->match) == 2
2571 && !riscv_opts.rvc)
2572 break;
2573
2574 if (riscv_is_priv_insn (ip->insn_opcode))
2575 explicit_priv_attr = true;
2576
2577 /* Check if we write a read-only CSR by the CSR
2578 instruction. */
2579 if (insn_with_csr
2580 && riscv_opts.csr_check
2581 && !riscv_csr_read_only_check (ip->insn_opcode))
2582 {
2583 /* Restore the character in advance, since we want to
2584 report the detailed warning message here. */
2585 if (save_c)
2586 *(asargStart - 1) = save_c;
2587 as_warn (_("read-only CSR is written `%s'"), str);
2588 insn_with_csr = false;
2589 }
2590
2591 /* The (segmant) load and store with EEW 64 cannot be used
2592 when zve32x is enabled. */
2593 if (ip->insn_mo->pinfo & INSN_V_EEW64
2594 && riscv_subset_supports (&riscv_rps_as, "zve32x")
2595 && !riscv_subset_supports (&riscv_rps_as, "zve64x"))
2596 {
2597 error.msg = _("illegal opcode for zve32x");
2598 break;
2599 }
2600 }
2601 if (*asarg != '\0')
2602 break;
2603
2604 /* Successful assembly. */
2605 error.msg = NULL;
2606 insn_with_csr = false;
2607
2608 /* Commit deferred symbols, if any. */
2609 while (deferred_sym_rootP)
2610 {
2611 symbolS *sym = deferred_sym_rootP;
2612
2613 symbol_remove (sym, &deferred_sym_rootP,
2614 &deferred_sym_lastP);
2615 symbol_append (sym, symbol_lastP, &symbol_rootP,
2616 &symbol_lastP);
2617 symbol_table_insert (sym);
2618 }
2619 goto out;
2620
2621 case 'C': /* RVC */
2622 switch (*++oparg)
2623 {
2624 case 's': /* RS1 x8-x15. */
2625 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2626 || !(regno >= 8 && regno <= 15))
2627 break;
2628 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2629 continue;
2630 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2631 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2632 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2633 break;
2634 continue;
2635 case 't': /* RS2 x8-x15. */
2636 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2637 || !(regno >= 8 && regno <= 15))
2638 break;
2639 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2640 continue;
2641 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2642 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2643 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2644 break;
2645 continue;
2646 case 'U': /* RS1, constrained to equal RD. */
2647 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2648 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2649 break;
2650 continue;
2651 case 'V': /* RS2 */
2652 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2653 break;
2654 INSERT_OPERAND (CRS2, *ip, regno);
2655 continue;
2656 case 'c': /* RS1, constrained to equal sp. */
2657 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2658 || regno != X_SP)
2659 break;
2660 continue;
2661 case 'z': /* RS2, constrained to equal x0. */
2662 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2663 || regno != 0)
2664 break;
2665 continue;
2666 case '>': /* Shift amount, 0 - (XLEN-1). */
2667 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2668 || imm_expr->X_op != O_constant
2669 || (unsigned long) imm_expr->X_add_number >= xlen)
2670 break;
2671 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2672 rvc_imm_done:
2673 asarg = expr_parse_end;
2674 imm_expr->X_op = O_absent;
2675 continue;
2676 case '5':
2677 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2678 || imm_expr->X_op != O_constant
2679 || imm_expr->X_add_number < 0
2680 || imm_expr->X_add_number >= 32
2681 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
2682 break;
2683 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2684 goto rvc_imm_done;
2685 case '6':
2686 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2687 || imm_expr->X_op != O_constant
2688 || imm_expr->X_add_number < 0
2689 || imm_expr->X_add_number >= 64
2690 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
2691 break;
2692 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2693 goto rvc_imm_done;
2694 case '8':
2695 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2696 || imm_expr->X_op != O_constant
2697 || imm_expr->X_add_number < 0
2698 || imm_expr->X_add_number >= 256
2699 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
2700 break;
2701 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2702 goto rvc_imm_done;
2703 case 'j':
2704 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2705 || imm_expr->X_op != O_constant
2706 || imm_expr->X_add_number == 0
2707 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2708 break;
2709 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2710 goto rvc_imm_done;
2711 case 'k':
2712 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2713 continue;
2714 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2715 || imm_expr->X_op != O_constant
2716 || !VALID_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
2717 break;
2718 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2719 goto rvc_imm_done;
2720 case 'l':
2721 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2722 continue;
2723 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2724 || imm_expr->X_op != O_constant
2725 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
2726 break;
2727 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2728 goto rvc_imm_done;
2729 case 'm':
2730 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2731 continue;
2732 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2733 || imm_expr->X_op != O_constant
2734 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
2735 break;
2736 ip->insn_opcode |=
2737 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
2738 goto rvc_imm_done;
2739 case 'n':
2740 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2741 continue;
2742 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2743 || imm_expr->X_op != O_constant
2744 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
2745 break;
2746 ip->insn_opcode |=
2747 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
2748 goto rvc_imm_done;
2749 case 'o':
2750 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2751 || imm_expr->X_op != O_constant
2752 /* C.addiw, c.li, and c.andi allow zero immediate.
2753 C.addi allows zero immediate as hint. Otherwise this
2754 is same as 'j'. */
2755 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2756 break;
2757 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2758 goto rvc_imm_done;
2759 case 'K':
2760 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2761 || imm_expr->X_op != O_constant
2762 || imm_expr->X_add_number == 0
2763 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
2764 break;
2765 ip->insn_opcode |=
2766 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
2767 goto rvc_imm_done;
2768 case 'L':
2769 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2770 || imm_expr->X_op != O_constant
2771 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
2772 break;
2773 ip->insn_opcode |=
2774 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
2775 goto rvc_imm_done;
2776 case 'M':
2777 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2778 continue;
2779 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2780 || imm_expr->X_op != O_constant
2781 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
2782 break;
2783 ip->insn_opcode |=
2784 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
2785 goto rvc_imm_done;
2786 case 'N':
2787 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2788 continue;
2789 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2790 || imm_expr->X_op != O_constant
2791 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
2792 break;
2793 ip->insn_opcode |=
2794 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
2795 goto rvc_imm_done;
2796 case 'u':
2797 p = percent_op_utype;
2798 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
2799 break;
2800 rvc_lui:
2801 if (imm_expr->X_op != O_constant
2802 || imm_expr->X_add_number <= 0
2803 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
2804 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
2805 && (imm_expr->X_add_number <
2806 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
2807 break;
2808 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2809 goto rvc_imm_done;
2810 case 'v':
2811 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2812 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
2813 || ((int32_t)imm_expr->X_add_number
2814 != imm_expr->X_add_number))
2815 break;
2816 imm_expr->X_add_number =
2817 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2818 goto rvc_lui;
2819 case 'p':
2820 goto branch;
2821 case 'a':
2822 goto jump;
2823 case 'S': /* Floating-point RS1 x8-x15. */
2824 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2825 || !(regno >= 8 && regno <= 15))
2826 break;
2827 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2828 continue;
2829 case 'D': /* Floating-point RS2 x8-x15. */
2830 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2831 || !(regno >= 8 && regno <= 15))
2832 break;
2833 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2834 continue;
2835 case 'T': /* Floating-point RS2. */
2836 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
2837 break;
2838 INSERT_OPERAND (CRS2, *ip, regno);
2839 continue;
2840 case 'F':
2841 switch (*++oparg)
2842 {
2843 case '6':
2844 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2845 || imm_expr->X_op != O_constant
2846 || imm_expr->X_add_number < 0
2847 || imm_expr->X_add_number >= 64)
2848 {
2849 as_bad (_("bad value for compressed funct6 "
2850 "field, value must be 0...63"));
2851 break;
2852 }
2853 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2854 imm_expr->X_op = O_absent;
2855 asarg = expr_parse_end;
2856 continue;
2857
2858 case '4':
2859 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2860 || imm_expr->X_op != O_constant
2861 || imm_expr->X_add_number < 0
2862 || imm_expr->X_add_number >= 16)
2863 {
2864 as_bad (_("bad value for compressed funct4 "
2865 "field, value must be 0...15"));
2866 break;
2867 }
2868 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2869 imm_expr->X_op = O_absent;
2870 asarg = expr_parse_end;
2871 continue;
2872
2873 case '3':
2874 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2875 || imm_expr->X_op != O_constant
2876 || imm_expr->X_add_number < 0
2877 || imm_expr->X_add_number >= 8)
2878 {
2879 as_bad (_("bad value for compressed funct3 "
2880 "field, value must be 0...7"));
2881 break;
2882 }
2883 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2884 imm_expr->X_op = O_absent;
2885 asarg = expr_parse_end;
2886 continue;
2887
2888 case '2':
2889 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2890 || imm_expr->X_op != O_constant
2891 || imm_expr->X_add_number < 0
2892 || imm_expr->X_add_number >= 4)
2893 {
2894 as_bad (_("bad value for compressed funct2 "
2895 "field, value must be 0...3"));
2896 break;
2897 }
2898 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2899 imm_expr->X_op = O_absent;
2900 asarg = expr_parse_end;
2901 continue;
2902
2903 default:
2904 goto unknown_riscv_ip_operand;
2905 }
2906 break;
2907
2908 default:
2909 goto unknown_riscv_ip_operand;
2910 }
2911 break; /* end RVC */
2912
2913 case 'V': /* RVV */
2914 switch (*++oparg)
2915 {
2916 case 'd': /* VD */
2917 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2918 break;
2919 INSERT_OPERAND (VD, *ip, regno);
2920 continue;
2921
2922 case 'e': /* AMO VD */
2923 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
2924 INSERT_OPERAND (VWD, *ip, 0);
2925 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
2926 {
2927 INSERT_OPERAND (VWD, *ip, 1);
2928 INSERT_OPERAND (VD, *ip, regno);
2929 }
2930 else
2931 break;
2932 continue;
2933
2934 case 'f': /* AMO VS3 */
2935 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2936 break;
2937 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
2938 INSERT_OPERAND (VD, *ip, regno);
2939 else
2940 {
2941 /* VS3 must match VD. */
2942 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
2943 break;
2944 }
2945 continue;
2946
2947 case 's': /* VS1 */
2948 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2949 break;
2950 INSERT_OPERAND (VS1, *ip, regno);
2951 continue;
2952
2953 case 't': /* VS2 */
2954 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2955 break;
2956 INSERT_OPERAND (VS2, *ip, regno);
2957 continue;
2958
2959 case 'u': /* VS1 == VS2 */
2960 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2961 break;
2962 INSERT_OPERAND (VS1, *ip, regno);
2963 INSERT_OPERAND (VS2, *ip, regno);
2964 continue;
2965
2966 case 'v': /* VD == VS1 == VS2 */
2967 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2968 break;
2969 INSERT_OPERAND (VD, *ip, regno);
2970 INSERT_OPERAND (VS1, *ip, regno);
2971 INSERT_OPERAND (VS2, *ip, regno);
2972 continue;
2973
2974 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
2975 and is used to choose vs1/rs1/frs1/imm or vs2 for
2976 v[f]merge. It use the same encoding as the vector mask
2977 register. */
2978 case '0':
2979 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
2980 continue;
2981 break;
2982
2983 case 'b': /* vtypei for vsetivli */
2984 my_getVsetvliExpression (imm_expr, asarg);
2985 check_absolute_expr (ip, imm_expr, FALSE);
2986 if (!VALID_RVV_VB_IMM (imm_expr->X_add_number))
2987 as_bad (_("bad value for vsetivli immediate field, "
2988 "value must be 0..1023"));
2989 ip->insn_opcode
2990 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
2991 imm_expr->X_op = O_absent;
2992 asarg = expr_parse_end;
2993 continue;
2994
2995 case 'c': /* vtypei for vsetvli */
2996 my_getVsetvliExpression (imm_expr, asarg);
2997 check_absolute_expr (ip, imm_expr, FALSE);
2998 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
2999 as_bad (_("bad value for vsetvli immediate field, "
3000 "value must be 0..2047"));
3001 ip->insn_opcode
3002 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3003 imm_expr->X_op = O_absent;
3004 asarg = expr_parse_end;
3005 continue;
3006
3007 case 'i': /* vector arith signed immediate */
3008 my_getExpression (imm_expr, asarg);
3009 check_absolute_expr (ip, imm_expr, FALSE);
3010 if (imm_expr->X_add_number > 15
3011 || imm_expr->X_add_number < -16)
3012 as_bad (_("bad value for vector immediate field, "
3013 "value must be -16...15"));
3014 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3015 imm_expr->X_op = O_absent;
3016 asarg = expr_parse_end;
3017 continue;
3018
3019 case 'j': /* vector arith unsigned immediate */
3020 my_getExpression (imm_expr, asarg);
3021 check_absolute_expr (ip, imm_expr, FALSE);
3022 if (imm_expr->X_add_number < 0
3023 || imm_expr->X_add_number >= 32)
3024 as_bad (_("bad value for vector immediate field, "
3025 "value must be 0...31"));
3026 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3027 imm_expr->X_op = O_absent;
3028 asarg = expr_parse_end;
3029 continue;
3030
3031 case 'k': /* vector arith signed immediate, minus 1 */
3032 my_getExpression (imm_expr, asarg);
3033 check_absolute_expr (ip, imm_expr, FALSE);
3034 if (imm_expr->X_add_number > 16
3035 || imm_expr->X_add_number < -15)
3036 as_bad (_("bad value for vector immediate field, "
3037 "value must be -15...16"));
3038 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number - 1);
3039 imm_expr->X_op = O_absent;
3040 asarg = expr_parse_end;
3041 continue;
3042
3043 case 'm': /* optional vector mask */
3044 if (*asarg == '\0')
3045 {
3046 INSERT_OPERAND (VMASK, *ip, 1);
3047 continue;
3048 }
3049 else if (*asarg == ',' && asarg++
3050 && reg_lookup (&asarg, RCLASS_VECM, &regno)
3051 && regno == 0)
3052 {
3053 INSERT_OPERAND (VMASK, *ip, 0);
3054 continue;
3055 }
3056 break;
3057
3058 case 'M': /* required vector mask */
3059 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
3060 {
3061 INSERT_OPERAND (VMASK, *ip, 0);
3062 continue;
3063 }
3064 break;
3065
3066 case 'T': /* vector macro temporary register */
3067 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
3068 break;
3069 /* Store it in the FUNCT6 field as we don't have anyplace
3070 else to store it. */
3071 INSERT_OPERAND (VFUNCT6, *ip, regno);
3072 continue;
3073
3074 default:
3075 goto unknown_riscv_ip_operand;
3076 }
3077 break; /* end RVV */
3078
3079 case ',':
3080 if (*asarg++ == *oparg)
3081 continue;
3082 asarg--;
3083 break;
3084
3085 case '(':
3086 case ')':
3087 case '[':
3088 case ']':
3089 if (*asarg++ == *oparg)
3090 continue;
3091 break;
3092
3093 case '<': /* Shift amount, 0 - 31. */
3094 my_getExpression (imm_expr, asarg);
3095 check_absolute_expr (ip, imm_expr, false);
3096 if ((unsigned long) imm_expr->X_add_number > 31)
3097 as_bad (_("improper shift amount (%"PRIu64")"),
3098 imm_expr->X_add_number);
3099 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
3100 imm_expr->X_op = O_absent;
3101 asarg = expr_parse_end;
3102 continue;
3103
3104 case '>': /* Shift amount, 0 - (XLEN-1). */
3105 my_getExpression (imm_expr, asarg);
3106 check_absolute_expr (ip, imm_expr, false);
3107 if ((unsigned long) imm_expr->X_add_number >= xlen)
3108 as_bad (_("improper shift amount (%"PRIu64")"),
3109 imm_expr->X_add_number);
3110 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
3111 imm_expr->X_op = O_absent;
3112 asarg = expr_parse_end;
3113 continue;
3114
3115 case 'Z': /* CSRRxI immediate. */
3116 my_getExpression (imm_expr, asarg);
3117 check_absolute_expr (ip, imm_expr, false);
3118 if ((unsigned long) imm_expr->X_add_number > 31)
3119 as_bad (_("improper CSRxI immediate (%"PRIu64")"),
3120 imm_expr->X_add_number);
3121 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
3122 imm_expr->X_op = O_absent;
3123 asarg = expr_parse_end;
3124 continue;
3125
3126 case 'E': /* Control register. */
3127 insn_with_csr = true;
3128 explicit_priv_attr = true;
3129 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
3130 INSERT_OPERAND (CSR, *ip, regno);
3131 else
3132 {
3133 my_getExpression (imm_expr, asarg);
3134 check_absolute_expr (ip, imm_expr, true);
3135 if ((unsigned long) imm_expr->X_add_number > 0xfff)
3136 as_bad (_("improper CSR address (%"PRIu64")"),
3137 imm_expr->X_add_number);
3138 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
3139 imm_expr->X_op = O_absent;
3140 asarg = expr_parse_end;
3141 }
3142 continue;
3143
3144 case 'm': /* Rounding mode. */
3145 if (arg_lookup (&asarg, riscv_rm,
3146 ARRAY_SIZE (riscv_rm), &regno))
3147 {
3148 INSERT_OPERAND (RM, *ip, regno);
3149 continue;
3150 }
3151 break;
3152
3153 case 'P':
3154 case 'Q': /* Fence predecessor/successor. */
3155 if (arg_lookup (&asarg, riscv_pred_succ,
3156 ARRAY_SIZE (riscv_pred_succ), &regno))
3157 {
3158 if (*oparg == 'P')
3159 INSERT_OPERAND (PRED, *ip, regno);
3160 else
3161 INSERT_OPERAND (SUCC, *ip, regno);
3162 continue;
3163 }
3164 break;
3165
3166 case 'd': /* Destination register. */
3167 case 's': /* Source register. */
3168 case 't': /* Target register. */
3169 case 'r': /* RS3 */
3170 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
3171 {
3172 char c = *oparg;
3173 if (*asarg == ' ')
3174 ++asarg;
3175
3176 /* Now that we have assembled one operand, we use the args
3177 string to figure out where it goes in the instruction. */
3178 switch (c)
3179 {
3180 case 's':
3181 INSERT_OPERAND (RS1, *ip, regno);
3182 break;
3183 case 'd':
3184 INSERT_OPERAND (RD, *ip, regno);
3185 break;
3186 case 't':
3187 INSERT_OPERAND (RS2, *ip, regno);
3188 break;
3189 case 'r':
3190 INSERT_OPERAND (RS3, *ip, regno);
3191 break;
3192 }
3193 continue;
3194 }
3195 break;
3196
3197 case 'D': /* Floating point RD. */
3198 case 'S': /* Floating point RS1. */
3199 case 'T': /* Floating point RS2. */
3200 case 'U': /* Floating point RS1 and RS2. */
3201 case 'R': /* Floating point RS3. */
3202 if (reg_lookup (&asarg,
3203 (riscv_subset_supports (&riscv_rps_as, "zfinx")
3204 ? RCLASS_GPR : RCLASS_FPR), &regno))
3205 {
3206 char c = *oparg;
3207 if (*asarg == ' ')
3208 ++asarg;
3209 switch (c)
3210 {
3211 case 'D':
3212 INSERT_OPERAND (RD, *ip, regno);
3213 break;
3214 case 'S':
3215 INSERT_OPERAND (RS1, *ip, regno);
3216 break;
3217 case 'U':
3218 INSERT_OPERAND (RS1, *ip, regno);
3219 /* Fall through. */
3220 case 'T':
3221 INSERT_OPERAND (RS2, *ip, regno);
3222 break;
3223 case 'R':
3224 INSERT_OPERAND (RS3, *ip, regno);
3225 break;
3226 }
3227 continue;
3228 }
3229 break;
3230
3231 case 'I':
3232 my_getExpression (imm_expr, asarg);
3233 if (imm_expr->X_op != O_big
3234 && imm_expr->X_op != O_constant)
3235 break;
3236 normalize_constant_expr (imm_expr);
3237 asarg = expr_parse_end;
3238 continue;
3239
3240 case 'A':
3241 my_getExpression (imm_expr, asarg);
3242 normalize_constant_expr (imm_expr);
3243 /* The 'A' format specifier must be a symbol. */
3244 if (imm_expr->X_op != O_symbol)
3245 break;
3246 *imm_reloc = BFD_RELOC_32;
3247 asarg = expr_parse_end;
3248 continue;
3249
3250 case 'B':
3251 my_getExpression (imm_expr, asarg);
3252 normalize_constant_expr (imm_expr);
3253 /* The 'B' format specifier must be a symbol or a constant. */
3254 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
3255 break;
3256 if (imm_expr->X_op == O_symbol)
3257 *imm_reloc = BFD_RELOC_32;
3258 asarg = expr_parse_end;
3259 continue;
3260
3261 case 'j': /* Sign-extended immediate. */
3262 p = percent_op_itype;
3263 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3264 goto alu_op;
3265 case 'q': /* Store displacement. */
3266 p = percent_op_stype;
3267 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3268 goto load_store;
3269 case 'o': /* Load displacement. */
3270 p = percent_op_itype;
3271 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3272 goto load_store;
3273 case '1':
3274 /* This is used for TLS, where the fourth operand is
3275 %tprel_add, to get a relocation applied to an add
3276 instruction, for relaxation to use. */
3277 p = percent_op_rtype;
3278 goto alu_op;
3279 case '0': /* AMO displacement, which must be zero. */
3280 load_store:
3281 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3282 continue;
3283 alu_op:
3284 /* If this value won't fit into a 16 bit offset, then go
3285 find a macro that will generate the 32 bit offset
3286 code pattern. */
3287 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3288 {
3289 normalize_constant_expr (imm_expr);
3290 if (imm_expr->X_op != O_constant
3291 || (*oparg == '0' && imm_expr->X_add_number != 0)
3292 || (*oparg == '1')
3293 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3294 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3295 break;
3296 }
3297 asarg = expr_parse_end;
3298 continue;
3299
3300 case 'p': /* PC-relative offset. */
3301 branch:
3302 *imm_reloc = BFD_RELOC_12_PCREL;
3303 my_getExpression (imm_expr, asarg);
3304 asarg = expr_parse_end;
3305 continue;
3306
3307 case 'u': /* Upper 20 bits. */
3308 p = percent_op_utype;
3309 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3310 {
3311 if (imm_expr->X_op != O_constant)
3312 break;
3313
3314 if (imm_expr->X_add_number < 0
3315 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
3316 as_bad (_("lui expression not in range 0..1048575"));
3317
3318 *imm_reloc = BFD_RELOC_RISCV_HI20;
3319 imm_expr->X_add_number <<= RISCV_IMM_BITS;
3320 }
3321 asarg = expr_parse_end;
3322 continue;
3323
3324 case 'a': /* 20-bit PC-relative offset. */
3325 jump:
3326 my_getExpression (imm_expr, asarg);
3327 asarg = expr_parse_end;
3328 *imm_reloc = BFD_RELOC_RISCV_JMP;
3329 continue;
3330
3331 case 'c':
3332 my_getExpression (imm_expr, asarg);
3333 asarg = expr_parse_end;
3334 if (strcmp (asarg, "@plt") == 0)
3335 asarg += 4;
3336 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3337 continue;
3338
3339 case 'O':
3340 switch (*++oparg)
3341 {
3342 case '4':
3343 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3344 || imm_expr->X_op != O_constant
3345 || imm_expr->X_add_number < 0
3346 || imm_expr->X_add_number >= 128
3347 || (imm_expr->X_add_number & 0x3) != 3)
3348 {
3349 as_bad (_("bad value for opcode field, "
3350 "value must be 0...127 and "
3351 "lower 2 bits must be 0x3"));
3352 break;
3353 }
3354 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3355 imm_expr->X_op = O_absent;
3356 asarg = expr_parse_end;
3357 continue;
3358
3359 case '2':
3360 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3361 || imm_expr->X_op != O_constant
3362 || imm_expr->X_add_number < 0
3363 || imm_expr->X_add_number >= 3)
3364 {
3365 as_bad (_("bad value for opcode field, "
3366 "value must be 0...2"));
3367 break;
3368 }
3369 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3370 imm_expr->X_op = O_absent;
3371 asarg = expr_parse_end;
3372 continue;
3373
3374 default:
3375 goto unknown_riscv_ip_operand;
3376 }
3377 break;
3378
3379 case 'F':
3380 switch (*++oparg)
3381 {
3382 case '7':
3383 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3384 || imm_expr->X_op != O_constant
3385 || imm_expr->X_add_number < 0
3386 || imm_expr->X_add_number >= 128)
3387 {
3388 as_bad (_("bad value for funct7 field, "
3389 "value must be 0...127"));
3390 break;
3391 }
3392 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3393 imm_expr->X_op = O_absent;
3394 asarg = expr_parse_end;
3395 continue;
3396
3397 case '3':
3398 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3399 || imm_expr->X_op != O_constant
3400 || imm_expr->X_add_number < 0
3401 || imm_expr->X_add_number >= 8)
3402 {
3403 as_bad (_("bad value for funct3 field, "
3404 "value must be 0...7"));
3405 break;
3406 }
3407 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3408 imm_expr->X_op = O_absent;
3409 asarg = expr_parse_end;
3410 continue;
3411
3412 case '2':
3413 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3414 || imm_expr->X_op != O_constant
3415 || imm_expr->X_add_number < 0
3416 || imm_expr->X_add_number >= 4)
3417 {
3418 as_bad (_("bad value for funct2 field, "
3419 "value must be 0...3"));
3420 break;
3421 }
3422 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3423 imm_expr->X_op = O_absent;
3424 asarg = expr_parse_end;
3425 continue;
3426
3427 default:
3428 goto unknown_riscv_ip_operand;
3429 }
3430 break;
3431
3432 case 'y': /* bs immediate */
3433 my_getExpression (imm_expr, asarg);
3434 check_absolute_expr (ip, imm_expr, FALSE);
3435 if ((unsigned long)imm_expr->X_add_number > 3)
3436 as_bad(_("Improper bs immediate (%lu)"),
3437 (unsigned long)imm_expr->X_add_number);
3438 INSERT_OPERAND(BS, *ip, imm_expr->X_add_number);
3439 imm_expr->X_op = O_absent;
3440 asarg = expr_parse_end;
3441 continue;
3442
3443 case 'Y': /* rnum immediate */
3444 my_getExpression (imm_expr, asarg);
3445 check_absolute_expr (ip, imm_expr, FALSE);
3446 if ((unsigned long)imm_expr->X_add_number > 10)
3447 as_bad(_("Improper rnum immediate (%lu)"),
3448 (unsigned long)imm_expr->X_add_number);
3449 INSERT_OPERAND(RNUM, *ip, imm_expr->X_add_number);
3450 imm_expr->X_op = O_absent;
3451 asarg = expr_parse_end;
3452 continue;
3453
3454 case 'z':
3455 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3456 || imm_expr->X_op != O_constant
3457 || imm_expr->X_add_number != 0)
3458 break;
3459 asarg = expr_parse_end;
3460 imm_expr->X_op = O_absent;
3461 continue;
3462
3463 case 'W': /* Various operands. */
3464 switch (*++oparg)
3465 {
3466 case 'i':
3467 switch (*++oparg)
3468 {
3469 case 'f':
3470 /* Prefetch offset for 'Zicbop' extension.
3471 pseudo S-type but lower 5-bits zero. */
3472 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3473 continue;
3474 my_getExpression (imm_expr, asarg);
3475 check_absolute_expr (ip, imm_expr, false);
3476 if (((unsigned) (imm_expr->X_add_number) & 0x1fU)
3477 || imm_expr->X_add_number >= RISCV_IMM_REACH / 2
3478 || imm_expr->X_add_number < -RISCV_IMM_REACH / 2)
3479 as_bad (_ ("improper prefetch offset (%ld)"),
3480 (long) imm_expr->X_add_number);
3481 ip->insn_opcode |= ENCODE_STYPE_IMM (
3482 (unsigned) (imm_expr->X_add_number) & ~0x1fU);
3483 imm_expr->X_op = O_absent;
3484 asarg = expr_parse_end;
3485 continue;
3486 default:
3487 goto unknown_riscv_ip_operand;
3488 }
3489 break;
3490 default:
3491 goto unknown_riscv_ip_operand;
3492 }
3493 break;
3494
3495 case 'X': /* Integer immediate. */
3496 {
3497 size_t n;
3498 size_t s;
3499 bool sign;
3500
3501 switch (*++oparg)
3502 {
3503 case 'l': /* Literal. */
3504 n = strcspn (++oparg, ",");
3505 if (strncmp (oparg, asarg, n))
3506 as_bad (_("unexpected literal (%s)"), asarg);
3507 oparg += n - 1;
3508 asarg += n;
3509 continue;
3510 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
3511 sign = true;
3512 goto parse_imm;
3513 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
3514 sign = false;
3515 goto parse_imm;
3516 parse_imm:
3517 n = strtol (oparg + 1, (char **)&oparg, 10);
3518 if (*oparg != '@')
3519 goto unknown_riscv_ip_operand;
3520 s = strtol (oparg + 1, (char **)&oparg, 10);
3521 oparg--;
3522
3523 my_getExpression (imm_expr, asarg);
3524 check_absolute_expr (ip, imm_expr, false);
3525 if (!sign)
3526 {
3527 if (!VALIDATE_U_IMM (imm_expr->X_add_number, n))
3528 as_bad (_("improper immediate value (%"PRIu64")"),
3529 imm_expr->X_add_number);
3530 }
3531 else
3532 {
3533 if (!VALIDATE_S_IMM (imm_expr->X_add_number, n))
3534 as_bad (_("improper immediate value (%"PRIi64")"),
3535 imm_expr->X_add_number);
3536 }
3537 INSERT_IMM (n, s, *ip, imm_expr->X_add_number);
3538 imm_expr->X_op = O_absent;
3539 asarg = expr_parse_end;
3540 continue;
3541 default:
3542 goto unknown_riscv_ip_operand;
3543 }
3544 }
3545 break;
3546
3547 default:
3548 unknown_riscv_ip_operand:
3549 as_fatal (_("internal: unknown argument type `%s'"),
3550 opargStart);
3551 }
3552 break;
3553 }
3554 asarg = asargStart;
3555 insn_with_csr = false;
3556 }
3557
3558 out:
3559 /* Restore the character we might have clobbered above. */
3560 if (save_c)
3561 *(asargStart - 1) = save_c;
3562
3563 probing_insn_operands = false;
3564
3565 return error;
3566 }
3567
3568 /* Similar to riscv_ip, but assembles an instruction according to the
3569 hardcode values of .insn directive. */
3570
3571 static const char *
3572 riscv_ip_hardcode (char *str,
3573 struct riscv_cl_insn *ip,
3574 expressionS *imm_expr,
3575 const char *error)
3576 {
3577 struct riscv_opcode *insn;
3578 insn_t values[2] = {0, 0};
3579 unsigned int num = 0;
3580
3581 input_line_pointer = str;
3582 do
3583 {
3584 expression (imm_expr);
3585 switch (imm_expr->X_op)
3586 {
3587 case O_constant:
3588 values[num++] = (insn_t) imm_expr->X_add_number;
3589 break;
3590 case O_big:
3591 /* Extract lower 32-bits of a big number.
3592 Assume that generic_bignum_to_int32 work on such number. */
3593 values[num++] = (insn_t) generic_bignum_to_int32 ();
3594 break;
3595 default:
3596 /* The first value isn't constant, so it should be
3597 .insn <type> <operands>. We have been parsed it
3598 in the riscv_ip. */
3599 if (num == 0)
3600 return error;
3601 return _("values must be constant");
3602 }
3603 }
3604 while (*input_line_pointer++ == ',' && num < 2 && imm_expr->X_op != O_big);
3605
3606 input_line_pointer--;
3607 if (*input_line_pointer != '\0')
3608 return _("unrecognized values");
3609
3610 insn = XNEW (struct riscv_opcode);
3611 insn->match = values[num - 1];
3612 create_insn (ip, insn);
3613 unsigned int bytes = riscv_insn_length (insn->match);
3614
3615 if (num == 2 && values[0] != bytes)
3616 return _("value conflicts with instruction length");
3617
3618 if (imm_expr->X_op == O_big)
3619 {
3620 unsigned int llen = 0;
3621 for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
3622 lval != 0; llen++)
3623 lval >>= BITS_PER_CHAR;
3624 unsigned int repr_bytes
3625 = (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
3626 if (bytes < repr_bytes)
3627 return _("value conflicts with instruction length");
3628 for (num = 0; num < imm_expr->X_add_number - 1; ++num)
3629 number_to_chars_littleendian (
3630 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3631 generic_bignum[num],
3632 CHARS_PER_LITTLENUM);
3633 if (llen != 0)
3634 number_to_chars_littleendian (
3635 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3636 generic_bignum[num],
3637 llen);
3638 memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
3639 return NULL;
3640 }
3641
3642 if (bytes < sizeof(values[0]) && values[num - 1] >> (8 * bytes) != 0)
3643 return _("value conflicts with instruction length");
3644
3645 return NULL;
3646 }
3647
3648 void
3649 md_assemble (char *str)
3650 {
3651 struct riscv_cl_insn insn;
3652 expressionS imm_expr;
3653 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
3654
3655 /* The architecture and privileged elf attributes should be set
3656 before assembling. */
3657 if (!start_assemble)
3658 {
3659 start_assemble = true;
3660
3661 riscv_set_abi_by_arch ();
3662 if (!riscv_set_default_priv_spec (NULL))
3663 return;
3664 }
3665
3666 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
3667
3668 const struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
3669 &imm_reloc, op_hash);
3670
3671 if (error.msg)
3672 {
3673 if (error.missing_ext)
3674 as_bad ("%s `%s', extension `%s' required", error.msg,
3675 error.statement, error.missing_ext);
3676 else
3677 as_bad ("%s `%s'", error.msg, error.statement);
3678 return;
3679 }
3680
3681 if (insn.insn_mo->pinfo == INSN_MACRO)
3682 macro (&insn, &imm_expr, &imm_reloc);
3683 else
3684 append_insn (&insn, &imm_expr, imm_reloc);
3685 }
3686
3687 const char *
3688 md_atof (int type, char *litP, int *sizeP)
3689 {
3690 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
3691 }
3692
3693 void
3694 md_number_to_chars (char *buf, valueT val, int n)
3695 {
3696 if (target_big_endian)
3697 number_to_chars_bigendian (buf, val, n);
3698 else
3699 number_to_chars_littleendian (buf, val, n);
3700 }
3701
3702 const char *md_shortopts = "O::g::G:";
3703
3704 enum options
3705 {
3706 OPTION_MARCH = OPTION_MD_BASE,
3707 OPTION_PIC,
3708 OPTION_NO_PIC,
3709 OPTION_MABI,
3710 OPTION_RELAX,
3711 OPTION_NO_RELAX,
3712 OPTION_ARCH_ATTR,
3713 OPTION_NO_ARCH_ATTR,
3714 OPTION_CSR_CHECK,
3715 OPTION_NO_CSR_CHECK,
3716 OPTION_MISA_SPEC,
3717 OPTION_MPRIV_SPEC,
3718 OPTION_BIG_ENDIAN,
3719 OPTION_LITTLE_ENDIAN,
3720 OPTION_END_OF_ENUM
3721 };
3722
3723 struct option md_longopts[] =
3724 {
3725 {"march", required_argument, NULL, OPTION_MARCH},
3726 {"fPIC", no_argument, NULL, OPTION_PIC},
3727 {"fpic", no_argument, NULL, OPTION_PIC},
3728 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
3729 {"mabi", required_argument, NULL, OPTION_MABI},
3730 {"mrelax", no_argument, NULL, OPTION_RELAX},
3731 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
3732 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
3733 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
3734 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
3735 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
3736 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
3737 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
3738 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
3739 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
3740
3741 {NULL, no_argument, NULL, 0}
3742 };
3743 size_t md_longopts_size = sizeof (md_longopts);
3744
3745 int
3746 md_parse_option (int c, const char *arg)
3747 {
3748 switch (c)
3749 {
3750 case OPTION_MARCH:
3751 default_arch_with_ext = arg;
3752 break;
3753
3754 case OPTION_NO_PIC:
3755 riscv_opts.pic = false;
3756 break;
3757
3758 case OPTION_PIC:
3759 riscv_opts.pic = true;
3760 break;
3761
3762 case OPTION_MABI:
3763 if (strcmp (arg, "ilp32") == 0)
3764 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
3765 else if (strcmp (arg, "ilp32e") == 0)
3766 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
3767 else if (strcmp (arg, "ilp32f") == 0)
3768 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
3769 else if (strcmp (arg, "ilp32d") == 0)
3770 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
3771 else if (strcmp (arg, "ilp32q") == 0)
3772 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
3773 else if (strcmp (arg, "lp64") == 0)
3774 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
3775 else if (strcmp (arg, "lp64f") == 0)
3776 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
3777 else if (strcmp (arg, "lp64d") == 0)
3778 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
3779 else if (strcmp (arg, "lp64q") == 0)
3780 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
3781 else
3782 return 0;
3783 explicit_mabi = true;
3784 break;
3785
3786 case OPTION_RELAX:
3787 riscv_opts.relax = true;
3788 break;
3789
3790 case OPTION_NO_RELAX:
3791 riscv_opts.relax = false;
3792 break;
3793
3794 case OPTION_ARCH_ATTR:
3795 riscv_opts.arch_attr = true;
3796 break;
3797
3798 case OPTION_NO_ARCH_ATTR:
3799 riscv_opts.arch_attr = false;
3800 break;
3801
3802 case OPTION_CSR_CHECK:
3803 riscv_opts.csr_check = true;
3804 break;
3805
3806 case OPTION_NO_CSR_CHECK:
3807 riscv_opts.csr_check = false;
3808 break;
3809
3810 case OPTION_MISA_SPEC:
3811 return riscv_set_default_isa_spec (arg);
3812
3813 case OPTION_MPRIV_SPEC:
3814 return riscv_set_default_priv_spec (arg);
3815
3816 case OPTION_BIG_ENDIAN:
3817 target_big_endian = 1;
3818 break;
3819
3820 case OPTION_LITTLE_ENDIAN:
3821 target_big_endian = 0;
3822 break;
3823
3824 default:
3825 return 0;
3826 }
3827
3828 return 1;
3829 }
3830
3831 void
3832 riscv_after_parse_args (void)
3833 {
3834 /* The --with-arch is optional for now, so we still need to set the xlen
3835 according to the default_arch, which is set by the --target. */
3836 if (xlen == 0)
3837 {
3838 if (strcmp (default_arch, "riscv32") == 0)
3839 xlen = 32;
3840 else if (strcmp (default_arch, "riscv64") == 0)
3841 xlen = 64;
3842 else
3843 as_bad ("unknown default architecture `%s'", default_arch);
3844 }
3845
3846 /* Set default specs. */
3847 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
3848 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
3849 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
3850 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
3851
3852 riscv_set_arch (default_arch_with_ext);
3853
3854 /* If the CIE to be produced has not been overridden on the command line,
3855 then produce version 3 by default. This allows us to use the full
3856 range of registers in a .cfi_return_column directive. */
3857 if (flag_dwarf_cie_version == -1)
3858 flag_dwarf_cie_version = 3;
3859 }
3860
3861 bool riscv_parse_name (const char *name, struct expressionS *ep,
3862 enum expr_mode mode)
3863 {
3864 unsigned int regno;
3865 symbolS *sym;
3866
3867 if (!probing_insn_operands)
3868 return false;
3869
3870 gas_assert (mode == expr_normal);
3871
3872 regno = reg_lookup_internal (name, RCLASS_GPR);
3873 if (regno == (unsigned int)-1)
3874 return false;
3875
3876 if (symbol_find (name) != NULL)
3877 return false;
3878
3879 /* Create a symbol without adding it to the symbol table yet.
3880 Insertion will happen only once we commit to using the insn
3881 we're probing operands for. */
3882 for (sym = deferred_sym_rootP; sym; sym = symbol_next (sym))
3883 if (strcmp (name, S_GET_NAME (sym)) == 0)
3884 break;
3885 if (!sym)
3886 {
3887 for (sym = orphan_sym_rootP; sym; sym = symbol_next (sym))
3888 if (strcmp (name, S_GET_NAME (sym)) == 0)
3889 {
3890 symbol_remove (sym, &orphan_sym_rootP, &orphan_sym_lastP);
3891 break;
3892 }
3893 if (!sym)
3894 sym = symbol_create (name, undefined_section,
3895 &zero_address_frag, 0);
3896
3897 symbol_append (sym, deferred_sym_lastP, &deferred_sym_rootP,
3898 &deferred_sym_lastP);
3899 }
3900
3901 ep->X_op = O_symbol;
3902 ep->X_add_symbol = sym;
3903 ep->X_add_number = 0;
3904
3905 return true;
3906 }
3907
3908 long
3909 md_pcrel_from (fixS *fixP)
3910 {
3911 return fixP->fx_where + fixP->fx_frag->fr_address;
3912 }
3913
3914 /* Apply a fixup to the object file. */
3915
3916 void
3917 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3918 {
3919 unsigned int subtype;
3920 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
3921 bool relaxable = false;
3922 offsetT loc;
3923 segT sub_segment;
3924
3925 /* Remember value for tc_gen_reloc. */
3926 fixP->fx_addnumber = *valP;
3927
3928 switch (fixP->fx_r_type)
3929 {
3930 case BFD_RELOC_RISCV_HI20:
3931 case BFD_RELOC_RISCV_LO12_I:
3932 case BFD_RELOC_RISCV_LO12_S:
3933 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
3934 | bfd_getl32 (buf), buf);
3935 if (fixP->fx_addsy == NULL)
3936 fixP->fx_done = true;
3937 relaxable = true;
3938 break;
3939
3940 case BFD_RELOC_RISCV_GOT_HI20:
3941 case BFD_RELOC_RISCV_ADD8:
3942 case BFD_RELOC_RISCV_ADD16:
3943 case BFD_RELOC_RISCV_ADD32:
3944 case BFD_RELOC_RISCV_ADD64:
3945 case BFD_RELOC_RISCV_SUB6:
3946 case BFD_RELOC_RISCV_SUB8:
3947 case BFD_RELOC_RISCV_SUB16:
3948 case BFD_RELOC_RISCV_SUB32:
3949 case BFD_RELOC_RISCV_SUB64:
3950 case BFD_RELOC_RISCV_RELAX:
3951 break;
3952
3953 case BFD_RELOC_RISCV_TPREL_HI20:
3954 case BFD_RELOC_RISCV_TPREL_LO12_I:
3955 case BFD_RELOC_RISCV_TPREL_LO12_S:
3956 case BFD_RELOC_RISCV_TPREL_ADD:
3957 relaxable = true;
3958 /* Fall through. */
3959
3960 case BFD_RELOC_RISCV_TLS_GOT_HI20:
3961 case BFD_RELOC_RISCV_TLS_GD_HI20:
3962 case BFD_RELOC_RISCV_TLS_DTPREL32:
3963 case BFD_RELOC_RISCV_TLS_DTPREL64:
3964 if (fixP->fx_addsy != NULL)
3965 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3966 else
3967 as_bad_where (fixP->fx_file, fixP->fx_line,
3968 _("TLS relocation against a constant"));
3969 break;
3970
3971 case BFD_RELOC_32:
3972 /* Use pc-relative relocation for FDE initial location.
3973 The symbol address in .eh_frame may be adjusted in
3974 _bfd_elf_discard_section_eh_frame, and the content of
3975 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
3976 Therefore, we cannot insert a relocation whose addend symbol is
3977 in .eh_frame. Othrewise, the value may be adjusted twice. */
3978 if (fixP->fx_addsy && fixP->fx_subsy
3979 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
3980 && strcmp (sub_segment->name, ".eh_frame") == 0
3981 && S_GET_VALUE (fixP->fx_subsy)
3982 == fixP->fx_frag->fr_address + fixP->fx_where)
3983 {
3984 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
3985 fixP->fx_subsy = NULL;
3986 break;
3987 }
3988 /* Fall through. */
3989 case BFD_RELOC_64:
3990 case BFD_RELOC_16:
3991 case BFD_RELOC_8:
3992 case BFD_RELOC_RISCV_CFA:
3993 if (fixP->fx_addsy && fixP->fx_subsy)
3994 {
3995 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3996 fixP->fx_next->fx_addsy = fixP->fx_subsy;
3997 fixP->fx_next->fx_subsy = NULL;
3998 fixP->fx_next->fx_offset = 0;
3999 fixP->fx_subsy = NULL;
4000
4001 switch (fixP->fx_r_type)
4002 {
4003 case BFD_RELOC_64:
4004 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
4005 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
4006 break;
4007
4008 case BFD_RELOC_32:
4009 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
4010 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4011 break;
4012
4013 case BFD_RELOC_16:
4014 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
4015 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4016 break;
4017
4018 case BFD_RELOC_8:
4019 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
4020 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4021 break;
4022
4023 case BFD_RELOC_RISCV_CFA:
4024 /* Load the byte to get the subtype. */
4025 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
4026 loc = fixP->fx_frag->fr_fix - (subtype & 7);
4027 switch (subtype)
4028 {
4029 case DW_CFA_advance_loc1:
4030 fixP->fx_where = loc + 1;
4031 fixP->fx_next->fx_where = loc + 1;
4032 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
4033 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4034 break;
4035
4036 case DW_CFA_advance_loc2:
4037 fixP->fx_size = 2;
4038 fixP->fx_next->fx_size = 2;
4039 fixP->fx_where = loc + 1;
4040 fixP->fx_next->fx_where = loc + 1;
4041 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
4042 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4043 break;
4044
4045 case DW_CFA_advance_loc4:
4046 fixP->fx_size = 4;
4047 fixP->fx_next->fx_size = 4;
4048 fixP->fx_where = loc;
4049 fixP->fx_next->fx_where = loc;
4050 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
4051 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4052 break;
4053
4054 default:
4055 if (subtype < 0x80 && (subtype & 0x40))
4056 {
4057 /* DW_CFA_advance_loc */
4058 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
4059 fixP->fx_next->fx_frag = fixP->fx_frag;
4060 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
4061 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
4062 }
4063 else
4064 as_fatal (_("internal: bad CFA value #%d"), subtype);
4065 break;
4066 }
4067 break;
4068
4069 default:
4070 /* This case is unreachable. */
4071 abort ();
4072 }
4073 }
4074 /* Fall through. */
4075
4076 case BFD_RELOC_RVA:
4077 /* If we are deleting this reloc entry, we must fill in the
4078 value now. This can happen if we have a .word which is not
4079 resolved when it appears but is later defined. */
4080 if (fixP->fx_addsy == NULL)
4081 {
4082 gas_assert (fixP->fx_size <= sizeof (valueT));
4083 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
4084 fixP->fx_done = 1;
4085 }
4086 break;
4087
4088 case BFD_RELOC_RISCV_JMP:
4089 if (fixP->fx_addsy)
4090 {
4091 /* Fill in a tentative value to improve objdump readability. */
4092 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4093 bfd_vma delta = target - md_pcrel_from (fixP);
4094 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
4095 }
4096 break;
4097
4098 case BFD_RELOC_12_PCREL:
4099 if (fixP->fx_addsy)
4100 {
4101 /* Fill in a tentative value to improve objdump readability. */
4102 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4103 bfd_vma delta = target - md_pcrel_from (fixP);
4104 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
4105 }
4106 break;
4107
4108 case BFD_RELOC_RISCV_RVC_BRANCH:
4109 if (fixP->fx_addsy)
4110 {
4111 /* Fill in a tentative value to improve objdump readability. */
4112 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4113 bfd_vma delta = target - md_pcrel_from (fixP);
4114 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
4115 }
4116 break;
4117
4118 case BFD_RELOC_RISCV_RVC_JUMP:
4119 if (fixP->fx_addsy)
4120 {
4121 /* Fill in a tentative value to improve objdump readability. */
4122 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4123 bfd_vma delta = target - md_pcrel_from (fixP);
4124 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
4125 }
4126 break;
4127
4128 case BFD_RELOC_RISCV_CALL:
4129 case BFD_RELOC_RISCV_CALL_PLT:
4130 relaxable = true;
4131 break;
4132
4133 case BFD_RELOC_RISCV_PCREL_HI20:
4134 case BFD_RELOC_RISCV_PCREL_LO12_S:
4135 case BFD_RELOC_RISCV_PCREL_LO12_I:
4136 relaxable = riscv_opts.relax;
4137 break;
4138
4139 case BFD_RELOC_RISCV_ALIGN:
4140 break;
4141
4142 default:
4143 /* We ignore generic BFD relocations we don't know about. */
4144 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
4145 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
4146 }
4147
4148 if (fixP->fx_subsy != NULL)
4149 as_bad_subtract (fixP);
4150
4151 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4152 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
4153 {
4154 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4155 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
4156 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
4157 fixP->fx_next->fx_size = 0;
4158 }
4159 }
4160
4161 /* Because the value of .cfi_remember_state may changed after relaxation,
4162 we insert a fix to relocate it again in link-time. */
4163
4164 void
4165 riscv_pre_output_hook (void)
4166 {
4167 const frchainS *frch;
4168 segT s;
4169
4170 /* Save the current segment info. */
4171 segT seg = now_seg;
4172 subsegT subseg = now_subseg;
4173
4174 for (s = stdoutput->sections; s; s = s->next)
4175 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
4176 {
4177 fragS *frag;
4178
4179 for (frag = frch->frch_root; frag; frag = frag->fr_next)
4180 {
4181 if (frag->fr_type == rs_cfa)
4182 {
4183 expressionS exp;
4184 expressionS *symval;
4185
4186 symval = symbol_get_value_expression (frag->fr_symbol);
4187 exp.X_op = O_subtract;
4188 exp.X_add_symbol = symval->X_add_symbol;
4189 exp.X_add_number = 0;
4190 exp.X_op_symbol = symval->X_op_symbol;
4191
4192 /* We must set the segment before creating a frag after all
4193 frag chains have been chained together. */
4194 subseg_set (s, frch->frch_subseg);
4195
4196 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
4197 BFD_RELOC_RISCV_CFA);
4198 }
4199 }
4200 }
4201
4202 /* Restore the original segment info. */
4203 subseg_set (seg, subseg);
4204 }
4205
4206 /* Handle the .option pseudo-op. */
4207
4208 static void
4209 s_riscv_option (int x ATTRIBUTE_UNUSED)
4210 {
4211 char *name = input_line_pointer, ch;
4212
4213 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4214 ++input_line_pointer;
4215 ch = *input_line_pointer;
4216 *input_line_pointer = '\0';
4217
4218 if (strcmp (name, "rvc") == 0)
4219 {
4220 riscv_update_subset (&riscv_rps_as, "+c");
4221 riscv_reset_subsets_list_arch_str ();
4222 riscv_set_rvc (true);
4223 }
4224 else if (strcmp (name, "norvc") == 0)
4225 {
4226 riscv_update_subset (&riscv_rps_as, "-c");
4227 riscv_reset_subsets_list_arch_str ();
4228 riscv_set_rvc (false);
4229 }
4230 else if (strcmp (name, "pic") == 0)
4231 riscv_opts.pic = true;
4232 else if (strcmp (name, "nopic") == 0)
4233 riscv_opts.pic = false;
4234 else if (strcmp (name, "relax") == 0)
4235 riscv_opts.relax = true;
4236 else if (strcmp (name, "norelax") == 0)
4237 riscv_opts.relax = false;
4238 else if (strcmp (name, "csr-check") == 0)
4239 riscv_opts.csr_check = true;
4240 else if (strcmp (name, "no-csr-check") == 0)
4241 riscv_opts.csr_check = false;
4242 else if (strncmp (name, "arch,", 5) == 0)
4243 {
4244 name += 5;
4245 if (ISSPACE (*name) && *name != '\0')
4246 name++;
4247 riscv_update_subset (&riscv_rps_as, name);
4248 riscv_reset_subsets_list_arch_str ();
4249
4250 riscv_set_rvc (false);
4251 if (riscv_subset_supports (&riscv_rps_as, "c"))
4252 riscv_set_rvc (true);
4253
4254 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
4255 riscv_set_tso ();
4256 }
4257 else if (strcmp (name, "push") == 0)
4258 {
4259 struct riscv_option_stack *s;
4260
4261 s = XNEW (struct riscv_option_stack);
4262 s->next = riscv_opts_stack;
4263 s->options = riscv_opts;
4264 s->subset_list = riscv_rps_as.subset_list;
4265 riscv_opts_stack = s;
4266 riscv_rps_as.subset_list = riscv_copy_subset_list (s->subset_list);
4267 }
4268 else if (strcmp (name, "pop") == 0)
4269 {
4270 struct riscv_option_stack *s;
4271
4272 s = riscv_opts_stack;
4273 if (s == NULL)
4274 as_bad (_(".option pop with no .option push"));
4275 else
4276 {
4277 riscv_subset_list_t *release_subsets = riscv_rps_as.subset_list;
4278 riscv_opts_stack = s->next;
4279 riscv_opts = s->options;
4280 riscv_rps_as.subset_list = s->subset_list;
4281 riscv_release_subset_list (release_subsets);
4282 free (s);
4283 }
4284 }
4285 else
4286 {
4287 as_warn (_("unrecognized .option directive: %s"), name);
4288 }
4289 *input_line_pointer = ch;
4290 demand_empty_rest_of_line ();
4291 }
4292
4293 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4294 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4295 use in DWARF debug information. */
4296
4297 static void
4298 s_dtprel (int bytes)
4299 {
4300 expressionS ex;
4301 char *p;
4302
4303 expression (&ex);
4304
4305 if (ex.X_op != O_symbol)
4306 {
4307 as_bad (_("unsupported use of %s"), (bytes == 8
4308 ? ".dtpreldword"
4309 : ".dtprelword"));
4310 ignore_rest_of_line ();
4311 }
4312
4313 p = frag_more (bytes);
4314 md_number_to_chars (p, 0, bytes);
4315 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
4316 (bytes == 8
4317 ? BFD_RELOC_RISCV_TLS_DTPREL64
4318 : BFD_RELOC_RISCV_TLS_DTPREL32));
4319
4320 demand_empty_rest_of_line ();
4321 }
4322
4323 /* Handle the .bss pseudo-op. */
4324
4325 static void
4326 s_bss (int ignore ATTRIBUTE_UNUSED)
4327 {
4328 subseg_set (bss_section, 0);
4329 demand_empty_rest_of_line ();
4330 }
4331
4332 static void
4333 riscv_make_nops (char *buf, bfd_vma bytes)
4334 {
4335 bfd_vma i = 0;
4336
4337 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4338 means we are not within a valid instruction sequence. It is thus safe
4339 to use a zero byte, even though that is not a valid instruction. */
4340 if (bytes % 2 == 1)
4341 buf[i++] = 0;
4342
4343 /* Use at most one 2-byte NOP. */
4344 if ((bytes - i) % 4 == 2)
4345 {
4346 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
4347 i += 2;
4348 }
4349
4350 /* Fill the remainder with 4-byte NOPs. */
4351 for ( ; i < bytes; i += 4)
4352 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
4353 }
4354
4355 /* Called from md_do_align. Used to create an alignment frag in a
4356 code section by emitting a worst-case NOP sequence that the linker
4357 will later relax to the correct number of NOPs. We can't compute
4358 the correct alignment now because of other linker relaxations. */
4359
4360 bool
4361 riscv_frag_align_code (int n)
4362 {
4363 bfd_vma bytes = (bfd_vma) 1 << n;
4364 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
4365 bfd_vma worst_case_bytes = bytes - insn_alignment;
4366 char *nops;
4367 expressionS ex;
4368
4369 /* If we are moving to a smaller alignment than the instruction size, then no
4370 alignment is required. */
4371 if (bytes <= insn_alignment)
4372 return true;
4373
4374 /* When not relaxing, riscv_handle_align handles code alignment. */
4375 if (!riscv_opts.relax)
4376 return false;
4377
4378 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4379 rather than just use frag_more to handle an alignment here? So that we
4380 don't need to call riscv_mapping_state again later, and then only need
4381 to check frag->fr_type to see if it is frag_align_code. */
4382 nops = frag_more (worst_case_bytes);
4383
4384 ex.X_op = O_constant;
4385 ex.X_add_number = worst_case_bytes;
4386
4387 riscv_make_nops (nops, worst_case_bytes);
4388
4389 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
4390 &ex, false, BFD_RELOC_RISCV_ALIGN);
4391
4392 riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
4393
4394 /* We need to start a new frag after the alignment which may be removed by
4395 the linker, to prevent the assembler from computing static offsets.
4396 This is necessary to get correct EH info. */
4397 frag_wane (frag_now);
4398 frag_new (0);
4399
4400 return true;
4401 }
4402
4403 /* Implement HANDLE_ALIGN. */
4404
4405 void
4406 riscv_handle_align (fragS *fragP)
4407 {
4408 switch (fragP->fr_type)
4409 {
4410 case rs_align_code:
4411 /* When relaxing, riscv_frag_align_code handles code alignment. */
4412 if (!riscv_opts.relax)
4413 {
4414 bfd_signed_vma bytes = (fragP->fr_next->fr_address
4415 - fragP->fr_address - fragP->fr_fix);
4416 /* We have 4 byte uncompressed nops. */
4417 bfd_signed_vma size = 4;
4418 bfd_signed_vma excess = bytes % size;
4419 bfd_boolean odd_padding = (excess % 2 == 1);
4420 char *p = fragP->fr_literal + fragP->fr_fix;
4421
4422 if (bytes <= 0)
4423 break;
4424
4425 /* Insert zeros or compressed nops to get 4 byte alignment. */
4426 if (excess)
4427 {
4428 if (odd_padding)
4429 riscv_add_odd_padding_symbol (fragP);
4430 riscv_make_nops (p, excess);
4431 fragP->fr_fix += excess;
4432 p += excess;
4433 }
4434
4435 /* The frag will be changed to `rs_fill` later. The function
4436 `write_contents` will try to fill the remaining spaces
4437 according to the patterns we give. In this case, we give
4438 a 4 byte uncompressed nop as the pattern, and set the size
4439 of the pattern into `fr_var`. The nop will be output to the
4440 file `fr_offset` times. However, `fr_offset` could be zero
4441 if we don't need to pad the boundary finally. */
4442 riscv_make_nops (p, size);
4443 fragP->fr_var = size;
4444 }
4445 break;
4446
4447 default:
4448 break;
4449 }
4450 }
4451
4452 /* This usually called from frag_var. */
4453
4454 void
4455 riscv_init_frag (fragS * fragP, int max_chars)
4456 {
4457 /* Do not add mapping symbol to debug sections. */
4458 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
4459 return;
4460
4461 switch (fragP->fr_type)
4462 {
4463 case rs_fill:
4464 case rs_align:
4465 case rs_align_test:
4466 riscv_mapping_state (MAP_DATA, max_chars, false/* fr_align_code */);
4467 break;
4468 case rs_align_code:
4469 riscv_mapping_state (MAP_INSN, max_chars, true/* fr_align_code */);
4470 break;
4471 default:
4472 break;
4473 }
4474 }
4475
4476 int
4477 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
4478 {
4479 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
4480 }
4481
4482 /* Translate internal representation of relocation info to BFD target
4483 format. */
4484
4485 arelent *
4486 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4487 {
4488 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
4489
4490 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4491 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4492 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4493 reloc->addend = fixp->fx_addnumber;
4494
4495 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
4496 if (reloc->howto == NULL)
4497 {
4498 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
4499 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
4500 {
4501 /* We don't have R_RISCV_8/16, but for this special case,
4502 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
4503 return reloc;
4504 }
4505
4506 as_bad_where (fixp->fx_file, fixp->fx_line,
4507 _("cannot represent %s relocation in object file"),
4508 bfd_get_reloc_code_name (fixp->fx_r_type));
4509 return NULL;
4510 }
4511
4512 return reloc;
4513 }
4514
4515 int
4516 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
4517 {
4518 if (RELAX_BRANCH_P (fragp->fr_subtype))
4519 {
4520 offsetT old_var = fragp->fr_var;
4521 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
4522 return fragp->fr_var - old_var;
4523 }
4524
4525 return 0;
4526 }
4527
4528 /* Expand far branches to multi-instruction sequences. */
4529
4530 static void
4531 md_convert_frag_branch (fragS *fragp)
4532 {
4533 bfd_byte *buf;
4534 expressionS exp;
4535 fixS *fixp;
4536 insn_t insn;
4537 int rs1, reloc;
4538
4539 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
4540
4541 exp.X_op = O_symbol;
4542 exp.X_add_symbol = fragp->fr_symbol;
4543 exp.X_add_number = fragp->fr_offset;
4544
4545 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
4546
4547 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
4548 {
4549 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4550 {
4551 case 8:
4552 case 4:
4553 /* Expand the RVC branch into a RISC-V one. */
4554 insn = bfd_getl16 (buf);
4555 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
4556 if ((insn & MASK_C_J) == MATCH_C_J)
4557 insn = MATCH_JAL;
4558 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
4559 insn = MATCH_JAL | (X_RA << OP_SH_RD);
4560 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
4561 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
4562 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
4563 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
4564 else
4565 abort ();
4566 bfd_putl32 (insn, buf);
4567 break;
4568
4569 case 6:
4570 /* Invert the branch condition. Branch over the jump. */
4571 insn = bfd_getl16 (buf);
4572 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
4573 insn |= ENCODE_CBTYPE_IMM (6);
4574 bfd_putl16 (insn, buf);
4575 buf += 2;
4576 goto jump;
4577
4578 case 2:
4579 /* Just keep the RVC branch. */
4580 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4581 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
4582 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4583 2, &exp, false, reloc);
4584 buf += 2;
4585 goto done;
4586
4587 default:
4588 abort ();
4589 }
4590 }
4591
4592 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4593 {
4594 case 8:
4595 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
4596
4597 /* Invert the branch condition. Branch over the jump. */
4598 insn = bfd_getl32 (buf);
4599 insn ^= MATCH_BEQ ^ MATCH_BNE;
4600 insn |= ENCODE_BTYPE_IMM (8);
4601 bfd_putl32 (insn, buf);
4602 buf += 4;
4603
4604 jump:
4605 /* Jump to the target. */
4606 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4607 4, &exp, false, BFD_RELOC_RISCV_JMP);
4608 bfd_putl32 (MATCH_JAL, buf);
4609 buf += 4;
4610 break;
4611
4612 case 4:
4613 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4614 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
4615 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4616 4, &exp, false, reloc);
4617 buf += 4;
4618 break;
4619
4620 default:
4621 abort ();
4622 }
4623
4624 done:
4625 fixp->fx_file = fragp->fr_file;
4626 fixp->fx_line = fragp->fr_line;
4627
4628 gas_assert (buf == (bfd_byte *)fragp->fr_literal
4629 + fragp->fr_fix + fragp->fr_var);
4630
4631 fragp->fr_fix += fragp->fr_var;
4632 }
4633
4634 /* Relax a machine dependent frag. This returns the amount by which
4635 the current size of the frag should change. */
4636
4637 void
4638 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
4639 fragS *fragp)
4640 {
4641 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
4642 md_convert_frag_branch (fragp);
4643 }
4644
4645 void
4646 md_show_usage (FILE *stream)
4647 {
4648 fprintf (stream, _("\
4649 RISC-V options:\n\
4650 -fpic or -fPIC generate position-independent code\n\
4651 -fno-pic don't generate position-independent code (default)\n\
4652 -march=ISA set the RISC-V architecture\n\
4653 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
4654 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9.1, 1.10, 1.11, 1.12)\n\
4655 -mabi=ABI set the RISC-V ABI\n\
4656 -mrelax enable relax (default)\n\
4657 -mno-relax disable relax\n\
4658 -march-attr generate RISC-V arch attribute\n\
4659 -mno-arch-attr don't generate RISC-V arch attribute\n\
4660 -mcsr-check enable the csr ISA and privilege spec version checks\n\
4661 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
4662 -mbig-endian assemble for big-endian\n\
4663 -mlittle-endian assemble for little-endian\n\
4664 "));
4665 }
4666
4667 /* Standard calling conventions leave the CFA at SP on entry. */
4668
4669 void
4670 riscv_cfi_frame_initial_instructions (void)
4671 {
4672 cfi_add_CFA_def_cfa_register (X_SP);
4673 }
4674
4675 int
4676 tc_riscv_regname_to_dw2regnum (char *regname)
4677 {
4678 int reg;
4679
4680 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
4681 return reg;
4682
4683 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
4684 return reg + 32;
4685
4686 if ((reg = reg_lookup_internal (regname, RCLASS_VECR)) >= 0)
4687 return reg + 96;
4688
4689 /* CSRs are numbered 4096 -> 8191. */
4690 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
4691 return reg + 4096;
4692
4693 as_bad (_("unknown register `%s'"), regname);
4694 return -1;
4695 }
4696
4697 void
4698 riscv_elf_final_processing (void)
4699 {
4700 riscv_set_abi_by_arch ();
4701 riscv_release_subset_list (riscv_rps_as.subset_list);
4702 elf_elfheader (stdoutput)->e_flags |= elf_flags;
4703 }
4704
4705 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
4706 since these directives break relaxation when used with symbol deltas. */
4707
4708 static void
4709 s_riscv_leb128 (int sign)
4710 {
4711 expressionS exp;
4712 char *save_in = input_line_pointer;
4713
4714 expression (&exp);
4715 if (exp.X_op != O_constant)
4716 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
4717 demand_empty_rest_of_line ();
4718
4719 input_line_pointer = save_in;
4720 return s_leb128 (sign);
4721 }
4722
4723 /* Parse the .insn directive. There are three formats,
4724 Format 1: .insn <type> <operand1>, <operand2>, ...
4725 Format 2: .insn <length>, <value>
4726 Format 3: .insn <value>. */
4727
4728 static void
4729 s_riscv_insn (int x ATTRIBUTE_UNUSED)
4730 {
4731 char *str = input_line_pointer;
4732 struct riscv_cl_insn insn;
4733 expressionS imm_expr;
4734 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
4735 char save_c;
4736
4737 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4738 ++input_line_pointer;
4739
4740 save_c = *input_line_pointer;
4741 *input_line_pointer = '\0';
4742
4743 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
4744
4745 struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
4746 &imm_reloc, insn_type_hash);
4747 if (error.msg)
4748 {
4749 char *save_in = input_line_pointer;
4750 error.msg = riscv_ip_hardcode (str, &insn, &imm_expr, error.msg);
4751 input_line_pointer = save_in;
4752 }
4753
4754 if (error.msg)
4755 {
4756 if (error.missing_ext)
4757 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
4758 error.missing_ext);
4759 else
4760 as_bad ("%s `%s'", error.msg, error.statement);
4761 }
4762 else
4763 {
4764 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
4765 append_insn (&insn, &imm_expr, imm_reloc);
4766 }
4767
4768 *input_line_pointer = save_c;
4769 demand_empty_rest_of_line ();
4770 }
4771
4772 /* Update architecture and privileged elf attributes. If we don't set
4773 them, then try to output the default ones. */
4774
4775 static void
4776 riscv_write_out_attrs (void)
4777 {
4778 const char *arch_str, *priv_str, *p;
4779 /* versions[0]: major version.
4780 versions[1]: minor version.
4781 versions[2]: revision version. */
4782 unsigned versions[3] = {0}, number = 0;
4783 unsigned int i;
4784
4785 /* Re-write architecture elf attribute. */
4786 arch_str = riscv_rps_as.subset_list->arch_str;
4787 bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
4788
4789 /* For the file without any instruction, we don't set the default_priv_spec
4790 according to the privileged elf attributes since the md_assemble isn't
4791 called. */
4792 if (!start_assemble
4793 && !riscv_set_default_priv_spec (NULL))
4794 return;
4795
4796 /* If we already have set privileged elf attributes, then no need to do
4797 anything. Otherwise, don't generate or update them when no CSR and
4798 privileged instructions are used. */
4799 if (!explicit_priv_attr)
4800 return;
4801
4802 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
4803 p = priv_str;
4804 for (i = 0; *p; ++p)
4805 {
4806 if (*p == '.' && i < 3)
4807 {
4808 versions[i++] = number;
4809 number = 0;
4810 }
4811 else if (ISDIGIT (*p))
4812 number = (number * 10) + (*p - '0');
4813 else
4814 {
4815 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
4816 return;
4817 }
4818 }
4819 versions[i] = number;
4820
4821 /* Re-write privileged elf attributes. */
4822 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
4823 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
4824 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
4825 }
4826
4827 /* Add the default contents for the .riscv.attributes section. */
4828
4829 static void
4830 riscv_set_public_attributes (void)
4831 {
4832 if (riscv_opts.arch_attr || explicit_attr)
4833 riscv_write_out_attrs ();
4834 }
4835
4836 /* Called after all assembly has been done. */
4837
4838 void
4839 riscv_md_finish (void)
4840 {
4841 riscv_set_public_attributes ();
4842 }
4843
4844 /* Adjust the symbol table. */
4845
4846 void
4847 riscv_adjust_symtab (void)
4848 {
4849 bfd_map_over_sections (stdoutput, riscv_check_mapping_symbols, (char *) 0);
4850 elf_adjust_symtab ();
4851 }
4852
4853 /* Given a symbolic attribute NAME, return the proper integer value.
4854 Returns -1 if the attribute is not known. */
4855
4856 int
4857 riscv_convert_symbolic_attribute (const char *name)
4858 {
4859 static const struct
4860 {
4861 const char *name;
4862 const int tag;
4863 }
4864 attribute_table[] =
4865 {
4866 /* When you modify this table you should
4867 also modify the list in doc/c-riscv.texi. */
4868 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
4869 T(arch),
4870 T(priv_spec),
4871 T(priv_spec_minor),
4872 T(priv_spec_revision),
4873 T(unaligned_access),
4874 T(stack_align),
4875 #undef T
4876 };
4877
4878 if (name == NULL)
4879 return -1;
4880
4881 unsigned int i;
4882 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
4883 if (strcmp (name, attribute_table[i].name) == 0)
4884 return attribute_table[i].tag;
4885
4886 return -1;
4887 }
4888
4889 /* Parse a .attribute directive. */
4890
4891 static void
4892 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
4893 {
4894 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4895 unsigned old_xlen;
4896 obj_attribute *attr;
4897
4898 explicit_attr = true;
4899 switch (tag)
4900 {
4901 case Tag_RISCV_arch:
4902 old_xlen = xlen;
4903 attr = elf_known_obj_attributes_proc (stdoutput);
4904 if (!start_assemble)
4905 riscv_set_arch (attr[Tag_RISCV_arch].s);
4906 else
4907 as_fatal (_("architecture elf attributes must set before "
4908 "any instructions"));
4909
4910 if (old_xlen != xlen)
4911 {
4912 /* We must re-init bfd again if xlen is changed. */
4913 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
4914 bfd_find_target (riscv_target_format (), stdoutput);
4915
4916 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
4917 as_warn (_("could not set architecture and machine"));
4918 }
4919 break;
4920
4921 case Tag_RISCV_priv_spec:
4922 case Tag_RISCV_priv_spec_minor:
4923 case Tag_RISCV_priv_spec_revision:
4924 if (start_assemble)
4925 as_fatal (_("privileged elf attributes must set before "
4926 "any instructions"));
4927 break;
4928
4929 default:
4930 break;
4931 }
4932 }
4933
4934 /* Mark symbol that it follows a variant CC convention. */
4935
4936 static void
4937 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
4938 {
4939 char *name;
4940 char c;
4941 symbolS *sym;
4942 asymbol *bfdsym;
4943 elf_symbol_type *elfsym;
4944
4945 c = get_symbol_name (&name);
4946 if (!*name)
4947 as_bad (_("missing symbol name for .variant_cc directive"));
4948 sym = symbol_find_or_make (name);
4949 restore_line_pointer (c);
4950 demand_empty_rest_of_line ();
4951
4952 bfdsym = symbol_get_bfdsym (sym);
4953 elfsym = elf_symbol_from (bfdsym);
4954 gas_assert (elfsym);
4955 elfsym->internal_elf_sym.st_other |= STO_RISCV_VARIANT_CC;
4956 }
4957
4958 /* Same as elf_copy_symbol_attributes, but without copying st_other.
4959 This is needed so RISC-V specific st_other values can be independently
4960 specified for an IFUNC resolver (that is called by the dynamic linker)
4961 and the symbol it resolves (aliased to the resolver). In particular,
4962 if a function symbol has special st_other value set via directives,
4963 then attaching an IFUNC resolver to that symbol should not override
4964 the st_other setting. Requiring the directive on the IFUNC resolver
4965 symbol would be unexpected and problematic in C code, where the two
4966 symbols appear as two independent function declarations. */
4967
4968 void
4969 riscv_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
4970 {
4971 struct elf_obj_sy *srcelf = symbol_get_obj (src);
4972 struct elf_obj_sy *destelf = symbol_get_obj (dest);
4973 /* If size is unset, copy size from src. Because we don't track whether
4974 .size has been used, we can't differentiate .size dest, 0 from the case
4975 where dest's size is unset. */
4976 if (!destelf->size && S_GET_SIZE (dest) == 0)
4977 {
4978 if (srcelf->size)
4979 {
4980 destelf->size = XNEW (expressionS);
4981 *destelf->size = *srcelf->size;
4982 }
4983 S_SET_SIZE (dest, S_GET_SIZE (src));
4984 }
4985 }
4986
4987 /* RISC-V pseudo-ops table. */
4988 static const pseudo_typeS riscv_pseudo_table[] =
4989 {
4990 {"option", s_riscv_option, 0},
4991 {"half", cons, 2},
4992 {"word", cons, 4},
4993 {"dword", cons, 8},
4994 {"dtprelword", s_dtprel, 4},
4995 {"dtpreldword", s_dtprel, 8},
4996 {"bss", s_bss, 0},
4997 {"uleb128", s_riscv_leb128, 0},
4998 {"sleb128", s_riscv_leb128, 1},
4999 {"insn", s_riscv_insn, 0},
5000 {"attribute", s_riscv_attribute, 0},
5001 {"variant_cc", s_variant_cc, 0},
5002 {"float16", float_cons, 'h'},
5003
5004 { NULL, NULL, 0 },
5005 };
5006
5007 void
5008 riscv_pop_insert (void)
5009 {
5010 extern void pop_insert (const pseudo_typeS *);
5011
5012 pop_insert (riscv_pseudo_table);
5013 }