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