The 2.41 release!
[binutils-gdb.git] / gas / config / tc-riscv.c
blob297bb9b2a819b6de32574871ce314b148fa489e5
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_SMSTATEEN, /* Smstateen only */
78 CSR_CLASS_SMSTATEEN_32, /* Smstateen RV32 only */
79 CSR_CLASS_SSAIA, /* Ssaia */
80 CSR_CLASS_SSAIA_AND_H, /* Ssaia with H */
81 CSR_CLASS_SSAIA_32, /* Ssaia, rv32 only */
82 CSR_CLASS_SSAIA_AND_H_32, /* Ssaia with H, rv32 only */
83 CSR_CLASS_SSSTATEEN, /* S[ms]stateen only */
84 CSR_CLASS_SSSTATEEN_AND_H, /* S[ms]stateen only (with H) */
85 CSR_CLASS_SSSTATEEN_AND_H_32, /* S[ms]stateen RV32 only (with H) */
86 CSR_CLASS_SSCOFPMF, /* Sscofpmf only */
87 CSR_CLASS_SSCOFPMF_32, /* Sscofpmf RV32 only */
88 CSR_CLASS_SSTC, /* Sstc only */
89 CSR_CLASS_SSTC_AND_H, /* Sstc only (with H) */
90 CSR_CLASS_SSTC_32, /* Sstc RV32 only */
91 CSR_CLASS_SSTC_AND_H_32, /* Sstc RV32 only (with H) */
94 /* This structure holds all restricted conditions for a CSR. */
95 struct riscv_csr_extra
97 /* Class to which this CSR belongs. Used to decide whether or
98 not this CSR is legal in the current -march context. */
99 enum riscv_csr_class csr_class;
101 /* CSR may have differnet numbers in the previous priv spec. */
102 unsigned address;
104 /* Record the CSR is defined/valid in which versions. */
105 enum riscv_spec_class define_version;
107 /* Record the CSR is aborted/invalid from which versions. If it isn't
108 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
109 enum riscv_spec_class abort_version;
111 /* The CSR may have more than one setting. */
112 struct riscv_csr_extra *next;
115 /* This structure contains information about errors that occur within the
116 riscv_ip function */
117 struct riscv_ip_error
119 /* General error message */
120 const char* msg;
122 /* Statement that caused the error */
123 char* statement;
125 /* Missing extension that needs to be enabled */
126 const char* missing_ext;
129 #ifndef DEFAULT_ARCH
130 #define DEFAULT_ARCH "riscv64"
131 #endif
133 #ifndef DEFAULT_RISCV_ATTR
134 #define DEFAULT_RISCV_ATTR 0
135 #endif
137 /* Let riscv_after_parse_args set the default value according to xlen. */
138 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
139 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
140 #endif
142 /* Need to sync the version with RISC-V compiler. */
143 #ifndef DEFAULT_RISCV_ISA_SPEC
144 #define DEFAULT_RISCV_ISA_SPEC "20191213"
145 #endif
147 #ifndef DEFAULT_RISCV_PRIV_SPEC
148 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
149 #endif
151 static const char default_arch[] = DEFAULT_ARCH;
152 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
153 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
154 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
156 static unsigned xlen = 0; /* The width of an x-register. */
157 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
158 static bool rve_abi = false;
159 enum float_abi
161 FLOAT_ABI_DEFAULT = -1,
162 FLOAT_ABI_SOFT,
163 FLOAT_ABI_SINGLE,
164 FLOAT_ABI_DOUBLE,
165 FLOAT_ABI_QUAD
167 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
169 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
170 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
172 static unsigned elf_flags = 0;
174 static bool probing_insn_operands;
176 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
177 Otherwise, return 1. */
179 static int
180 riscv_set_default_isa_spec (const char *s)
182 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
183 RISCV_GET_ISA_SPEC_CLASS (s, class);
184 if (class == ISA_SPEC_CLASS_NONE)
186 as_bad ("unknown default ISA spec `%s' set by "
187 "-misa-spec or --with-isa-spec", s);
188 return 0;
190 else
191 default_isa_spec = class;
192 return 1;
195 /* Set the default_priv_spec. Find the privileged elf attributes when
196 the input string is NULL. Return 0 if the spec isn't supported.
197 Otherwise, return 1. */
199 static int
200 riscv_set_default_priv_spec (const char *s)
202 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
203 unsigned major, minor, revision;
204 obj_attribute *attr;
206 RISCV_GET_PRIV_SPEC_CLASS (s, class);
207 if (class != PRIV_SPEC_CLASS_NONE)
209 default_priv_spec = class;
210 return 1;
213 if (s != NULL)
215 as_bad (_("unknown default privileged spec `%s' set by "
216 "-mpriv-spec or --with-priv-spec"), s);
217 return 0;
220 /* Set the default_priv_spec by the privileged elf attributes. */
221 attr = elf_known_obj_attributes_proc (stdoutput);
222 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
223 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
224 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
225 /* Version 0.0.0 is the default value and meningless. */
226 if (major == 0 && minor == 0 && revision == 0)
227 return 1;
229 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
230 if (class != PRIV_SPEC_CLASS_NONE)
232 default_priv_spec = class;
233 return 1;
236 /* Still can not find the privileged spec class. */
237 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
238 "privileged elf attributes"), major, minor, revision);
239 return 0;
242 /* This is the set of options which the .option pseudo-op may modify. */
243 struct riscv_set_options
245 int pic; /* Generate position-independent code. */
246 int rvc; /* Generate RVC code. */
247 int relax; /* Emit relocs the linker is allowed to relax. */
248 int arch_attr; /* Emit architecture and privileged elf attributes. */
249 int csr_check; /* Enable the CSR checking. */
252 static struct riscv_set_options riscv_opts =
254 0, /* pic */
255 0, /* rvc */
256 1, /* relax */
257 DEFAULT_RISCV_ATTR, /* arch_attr */
258 0, /* csr_check */
261 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
262 for elf_flags once we have enabled c extension. */
264 static void
265 riscv_set_rvc (bool rvc_value)
267 if (rvc_value)
268 elf_flags |= EF_RISCV_RVC;
270 riscv_opts.rvc = rvc_value;
273 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
275 static void
276 riscv_set_tso (void)
278 elf_flags |= EF_RISCV_TSO;
281 /* The linked list hanging off of .subsets_list records all enabled extensions,
282 which are parsed from the architecture string. The architecture string can
283 be set by the -march option, the elf architecture attributes, and the
284 --with-arch configure option. */
285 static riscv_parse_subset_t riscv_rps_as =
287 NULL, /* subset_list, we will set it later once
288 riscv_opts_stack is created or updated. */
289 as_bad, /* error_handler. */
290 &xlen, /* xlen. */
291 &default_isa_spec, /* isa_spec. */
292 true, /* check_unknown_prefixed_ext. */
295 /* Update the architecture string in the subset_list. */
297 static void
298 riscv_reset_subsets_list_arch_str (void)
300 riscv_subset_list_t *subsets = riscv_rps_as.subset_list;
301 if (subsets->arch_str != NULL)
302 free ((void *) subsets->arch_str);
303 subsets->arch_str = riscv_arch_str (xlen, subsets);
306 /* This structure is used to hold a stack of .option values. */
307 struct riscv_option_stack
309 struct riscv_option_stack *next;
310 struct riscv_set_options options;
311 riscv_subset_list_t *subset_list;
314 static struct riscv_option_stack *riscv_opts_stack = NULL;
316 /* Set which ISA and extensions are available. */
318 static void
319 riscv_set_arch (const char *s)
321 if (s != NULL && strcmp (s, "") == 0)
323 as_bad (_("the architecture string of -march and elf architecture "
324 "attributes cannot be empty"));
325 return;
328 if (riscv_rps_as.subset_list == NULL)
330 riscv_rps_as.subset_list = XNEW (riscv_subset_list_t);
331 riscv_rps_as.subset_list->head = NULL;
332 riscv_rps_as.subset_list->tail = NULL;
333 riscv_rps_as.subset_list->arch_str = NULL;
335 riscv_release_subset_list (riscv_rps_as.subset_list);
336 riscv_parse_subset (&riscv_rps_as, s);
337 riscv_reset_subsets_list_arch_str ();
339 riscv_set_rvc (false);
340 if (riscv_subset_supports (&riscv_rps_as, "c"))
341 riscv_set_rvc (true);
343 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
344 riscv_set_tso ();
347 /* Indicate -mabi option is explictly set. */
348 static bool explicit_mabi = false;
350 /* Set the abi information. */
352 static void
353 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
355 abi_xlen = new_xlen;
356 float_abi = new_float_abi;
357 rve_abi = rve;
360 /* If the -mabi option isn't set, then set the abi according to the
361 ISA string. Otherwise, check if there is any conflict. */
363 static void
364 riscv_set_abi_by_arch (void)
366 if (!explicit_mabi)
368 if (riscv_subset_supports (&riscv_rps_as, "q"))
369 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
370 else if (riscv_subset_supports (&riscv_rps_as, "d"))
371 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
372 else if (riscv_subset_supports (&riscv_rps_as, "e"))
373 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
374 else
375 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
377 else
379 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
380 if (abi_xlen > xlen)
381 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
382 else if (abi_xlen < xlen)
383 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
385 if (riscv_subset_supports (&riscv_rps_as, "e") && !rve_abi)
386 as_bad ("only the ilp32e ABI is supported for e extension");
388 if (float_abi == FLOAT_ABI_SINGLE
389 && !riscv_subset_supports (&riscv_rps_as, "f"))
390 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
391 "isn't supported");
392 else if (float_abi == FLOAT_ABI_DOUBLE
393 && !riscv_subset_supports (&riscv_rps_as, "d"))
394 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
395 "isn't supported");
396 else if (float_abi == FLOAT_ABI_QUAD
397 && !riscv_subset_supports (&riscv_rps_as, "q"))
398 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
399 "isn't supported");
402 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
403 elf_flags &= ~EF_RISCV_FLOAT_ABI;
404 elf_flags |= float_abi << 1;
406 if (rve_abi)
407 elf_flags |= EF_RISCV_RVE;
410 /* Handle of the OPCODE hash table. */
411 static htab_t op_hash = NULL;
413 /* Handle of the type of .insn hash table. */
414 static htab_t insn_type_hash = NULL;
416 /* This array holds the chars that always start a comment. If the
417 pre-processor is disabled, these aren't very useful. */
418 const char comment_chars[] = "#";
420 /* This array holds the chars that only start a comment at the beginning of
421 a line. If the line seems to have the form '# 123 filename'
422 .line and .file directives will appear in the pre-processed output
424 Note that input_file.c hand checks for '#' at the beginning of the
425 first line of the input file. This is because the compiler outputs
426 #NO_APP at the beginning of its output.
428 Also note that C style comments are always supported. */
429 const char line_comment_chars[] = "#";
431 /* This array holds machine specific line separator characters. */
432 const char line_separator_chars[] = ";";
434 /* Chars that can be used to separate mant from exp in floating point nums. */
435 const char EXP_CHARS[] = "eE";
437 /* Chars that mean this number is a floating point constant.
438 As in 0f12.456 or 0d1.2345e12. */
439 const char FLT_CHARS[] = "rRsSfFdDxXpPhH";
441 /* Indicate we are already assemble any instructions or not. */
442 static bool start_assemble = false;
444 /* Indicate ELF attributes are explicitly set. */
445 static bool explicit_attr = false;
447 /* Indicate CSR or priv instructions are explicitly used. */
448 static bool explicit_priv_attr = false;
450 static char *expr_parse_end;
452 /* Macros for encoding relaxation state for RVC branches and far jumps. */
453 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
454 ((relax_substateT) \
455 (0xc0000000 \
456 | ((uncond) ? 1 : 0) \
457 | ((rvc) ? 2 : 0) \
458 | ((length) << 2)))
459 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
460 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
461 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
462 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
464 /* Is the given value a sign-extended 32-bit value? */
465 #define IS_SEXT_32BIT_NUM(x) \
466 (((x) &~ (offsetT) 0x7fffffff) == 0 \
467 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
469 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
470 #define IS_ZEXT_32BIT_NUM(x) \
471 (((x) &~ (offsetT) 0xffffffff) == 0 \
472 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
474 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
475 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
476 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
477 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
479 #define INSERT_IMM(n, s, INSN, VALUE) \
480 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
482 /* Determine if an instruction matches an opcode. */
483 #define OPCODE_MATCHES(OPCODE, OP) \
484 (((OPCODE) & MASK_##OP) == MATCH_##OP)
486 /* Create a new mapping symbol for the transition to STATE. */
488 static void
489 make_mapping_symbol (enum riscv_seg_mstate state,
490 valueT value,
491 fragS *frag,
492 const char *arch_str,
493 bool odd_data_padding)
495 const char *name;
496 char *buff = NULL;
497 switch (state)
499 case MAP_DATA:
500 name = "$d";
501 break;
502 case MAP_INSN:
503 if (arch_str != NULL)
505 size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */
506 buff = xmalloc (size);
507 snprintf (buff, size, "$x%s", arch_str);
508 name = buff;
510 else
511 name = "$x";
512 break;
513 default:
514 abort ();
517 symbolS *symbol = symbol_new (name, now_seg, frag, value);
518 symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL);
519 if (arch_str != NULL)
521 /* Store current $x+arch into tc_segment_info. */
522 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol;
523 xfree ((void *) buff);
526 /* If .fill or other data filling directive generates zero sized data,
527 then mapping symbol for the following code will have the same value.
529 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
530 and .text.zero.fill.last. */
531 symbolS *first = frag->tc_frag_data.first_map_symbol;
532 symbolS *last = frag->tc_frag_data.last_map_symbol;
533 symbolS *removed = NULL;
534 if (value == 0)
536 if (first != NULL)
538 know (S_GET_VALUE (first) == S_GET_VALUE (symbol)
539 && first == last);
540 /* Remove the old one. */
541 removed = first;
543 frag->tc_frag_data.first_map_symbol = symbol;
545 else if (last != NULL)
547 /* The mapping symbols should be added in offset order. */
548 know (S_GET_VALUE (last) <= S_GET_VALUE (symbol));
549 /* Remove the old one. */
550 if (S_GET_VALUE (last) == S_GET_VALUE (symbol))
551 removed = last;
553 frag->tc_frag_data.last_map_symbol = symbol;
555 if (removed == NULL)
556 return;
558 if (odd_data_padding)
560 /* If the removed mapping symbol is $x+arch, then add it back to
561 the next $x. */
562 const char *str = strncmp (S_GET_NAME (removed), "$xrv", 4) == 0
563 ? S_GET_NAME (removed) + 2 : NULL;
564 make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str,
565 false/* odd_data_padding */);
567 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
570 /* Set the mapping state for frag_now. */
572 void
573 riscv_mapping_state (enum riscv_seg_mstate to_state,
574 int max_chars,
575 bool fr_align_code)
577 enum riscv_seg_mstate from_state =
578 seg_info (now_seg)->tc_segment_info_data.map_state;
579 bool reset_seg_arch_str = false;
581 if (!SEG_NORMAL (now_seg)
582 /* For now we only add the mapping symbols to text sections.
583 Therefore, the dis-assembler only show the actual contents
584 distribution for text. Other sections will be shown as
585 data without the details. */
586 || !subseg_text_p (now_seg))
587 return;
589 /* The mapping symbol should be emitted if not in the right
590 mapping state. */
591 symbolS *seg_arch_symbol =
592 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol;
593 if (to_state == MAP_INSN && seg_arch_symbol == 0)
595 /* Always add $x+arch at the first instruction of section. */
596 reset_seg_arch_str = true;
598 else if (seg_arch_symbol != 0
599 && to_state == MAP_INSN
600 && !fr_align_code
601 && strcmp (riscv_rps_as.subset_list->arch_str,
602 S_GET_NAME (seg_arch_symbol) + 2) != 0)
604 reset_seg_arch_str = true;
606 else if (from_state == to_state)
607 return;
609 valueT value = (valueT) (frag_now_fix () - max_chars);
610 seg_info (now_seg)->tc_segment_info_data.map_state = to_state;
611 const char *arch_str = reset_seg_arch_str
612 ? riscv_rps_as.subset_list->arch_str : NULL;
613 make_mapping_symbol (to_state, value, frag_now, arch_str,
614 false/* odd_data_padding */);
617 /* Add the odd bytes of paddings for riscv_handle_align. */
619 static void
620 riscv_add_odd_padding_symbol (fragS *frag)
622 /* If there was already a mapping symbol, it should be
623 removed in the make_mapping_symbol.
625 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
626 make_mapping_symbol (MAP_DATA, frag->fr_fix, frag,
627 NULL/* arch_str */, true/* odd_data_padding */);
630 /* Remove any excess mapping symbols generated for alignment frags in
631 SEC. We may have created a mapping symbol before a zero byte
632 alignment; remove it if there's a mapping symbol after the
633 alignment. */
635 static void
636 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
637 asection *sec,
638 void *dummy ATTRIBUTE_UNUSED)
640 segment_info_type *seginfo = seg_info (sec);
641 fragS *fragp;
643 if (seginfo == NULL || seginfo->frchainP == NULL)
644 return;
646 for (fragp = seginfo->frchainP->frch_root;
647 fragp != NULL;
648 fragp = fragp->fr_next)
650 symbolS *last = fragp->tc_frag_data.last_map_symbol;
651 fragS *next = fragp->fr_next;
653 if (last == NULL || next == NULL)
654 continue;
656 /* Check the last mapping symbol if it is at the boundary of
657 fragment. */
658 if (S_GET_VALUE (last) < next->fr_address)
659 continue;
660 know (S_GET_VALUE (last) == next->fr_address);
664 symbolS *next_first = next->tc_frag_data.first_map_symbol;
665 if (next_first != NULL)
667 /* The last mapping symbol overlaps with another one
668 which at the start of the next frag.
670 Please see the gas/testsuite/gas/riscv/mapping.s:
671 .text.zero.fill.align.A and .text.zero.fill.align.B. */
672 know (S_GET_VALUE (last) == S_GET_VALUE (next_first));
673 symbolS *removed = last;
674 if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0
675 && strcmp (S_GET_NAME (next_first), "$x") == 0)
676 removed = next_first;
677 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
678 break;
681 if (next->fr_next == NULL)
683 /* The last mapping symbol is at the end of the section.
685 Please see the gas/testsuite/gas/riscv/mapping.s:
686 .text.last.section. */
687 know (next->fr_fix == 0 && next->fr_var == 0);
688 symbol_remove (last, &symbol_rootP, &symbol_lastP);
689 break;
692 /* Since we may have empty frags without any mapping symbols,
693 keep looking until the non-empty frag. */
694 if (next->fr_address != next->fr_next->fr_address)
695 break;
697 next = next->fr_next;
699 while (next != NULL);
703 /* The default target format to use. */
705 const char *
706 riscv_target_format (void)
708 if (target_big_endian)
709 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
710 else
711 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
714 /* Return the length of instruction INSN. */
716 static inline unsigned int
717 insn_length (const struct riscv_cl_insn *insn)
719 return riscv_insn_length (insn->insn_opcode);
722 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
724 static void
725 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
727 insn->insn_mo = mo;
728 insn->insn_opcode = mo->match;
729 insn->insn_long_opcode[0] = 0;
730 insn->frag = NULL;
731 insn->where = 0;
732 insn->fixp = NULL;
735 /* Install INSN at the location specified by its "frag" and "where" fields. */
737 static void
738 install_insn (const struct riscv_cl_insn *insn)
740 char *f = insn->frag->fr_literal + insn->where;
741 if (insn->insn_long_opcode[0] != 0)
742 memcpy (f, insn->insn_long_opcode, insn_length (insn));
743 else
744 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
747 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
748 and install the opcode in the new location. */
750 static void
751 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
753 insn->frag = frag;
754 insn->where = where;
755 if (insn->fixp != NULL)
757 insn->fixp->fx_frag = frag;
758 insn->fixp->fx_where = where;
760 install_insn (insn);
763 /* Add INSN to the end of the output. */
765 static void
766 add_fixed_insn (struct riscv_cl_insn *insn)
768 char *f = frag_more (insn_length (insn));
769 move_insn (insn, frag_now, f - frag_now->fr_literal);
772 static void
773 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
774 relax_substateT subtype, symbolS *symbol, offsetT offset)
776 frag_grow (max_chars);
777 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
778 frag_var (rs_machine_dependent, max_chars, var,
779 subtype, symbol, offset, NULL);
782 /* Compute the length of a branch sequence, and adjust the stored length
783 accordingly. If FRAGP is NULL, the worst-case length is returned. */
785 static unsigned
786 relaxed_branch_length (fragS *fragp, asection *sec, int update)
788 int jump, rvc, length = 8;
790 if (!fragp)
791 return length;
793 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
794 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
795 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
797 /* Assume jumps are in range; the linker will catch any that aren't. */
798 length = jump ? 4 : 8;
800 if (fragp->fr_symbol != NULL
801 && S_IS_DEFINED (fragp->fr_symbol)
802 && !S_IS_WEAK (fragp->fr_symbol)
803 && sec == S_GET_SEGMENT (fragp->fr_symbol))
805 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
806 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
807 val -= fragp->fr_address + fragp->fr_fix;
809 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
810 length = 2;
811 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
812 length = 4;
813 else if (!jump && rvc)
814 length = 6;
817 if (update)
818 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
820 return length;
823 /* Information about an opcode name, mnemonics and its value. */
824 struct opcode_name_t
826 const char *name;
827 unsigned int val;
830 /* List for all supported opcode name. */
831 static const struct opcode_name_t opcode_name_list[] =
833 {"C0", 0x0},
834 {"C1", 0x1},
835 {"C2", 0x2},
837 {"LOAD", 0x03},
838 {"LOAD_FP", 0x07},
839 {"CUSTOM_0", 0x0b},
840 {"MISC_MEM", 0x0f},
841 {"OP_IMM", 0x13},
842 {"AUIPC", 0x17},
843 {"OP_IMM_32", 0x1b},
844 /* 48b 0x1f. */
846 {"STORE", 0x23},
847 {"STORE_FP", 0x27},
848 {"CUSTOM_1", 0x2b},
849 {"AMO", 0x2f},
850 {"OP", 0x33},
851 {"LUI", 0x37},
852 {"OP_32", 0x3b},
853 /* 64b 0x3f. */
855 {"MADD", 0x43},
856 {"MSUB", 0x47},
857 {"NMADD", 0x4f},
858 {"NMSUB", 0x4b},
859 {"OP_FP", 0x53},
860 {"OP_V", 0x57},
861 {"CUSTOM_2", 0x5b},
862 /* 48b 0x5f. */
864 {"BRANCH", 0x63},
865 {"JALR", 0x67},
866 /*reserved 0x5b. */
867 {"JAL", 0x6f},
868 {"SYSTEM", 0x73},
869 /*reserved 0x77. */
870 {"CUSTOM_3", 0x7b},
871 /* >80b 0x7f. */
873 {NULL, 0}
876 /* Hash table for lookup opcode name. */
877 static htab_t opcode_names_hash = NULL;
879 /* Initialization for hash table of opcode name. */
881 static void
882 init_opcode_names_hash (void)
884 const struct opcode_name_t *opcode;
886 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
887 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
888 as_fatal (_("internal: duplicate %s"), opcode->name);
891 /* Find `s` is a valid opcode name or not, return the opcode name info
892 if found. */
894 static const struct opcode_name_t *
895 opcode_name_lookup (char **s)
897 char *e;
898 char save_c;
899 struct opcode_name_t *o;
901 /* Find end of name. */
902 e = *s;
903 if (is_name_beginner (*e))
904 ++e;
905 while (is_part_of_name (*e))
906 ++e;
908 /* Terminate name. */
909 save_c = *e;
910 *e = '\0';
912 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
914 /* Advance to next token if one was recognized. */
915 if (o)
916 *s = e;
918 *e = save_c;
919 expr_parse_end = e;
921 return o;
924 /* All RISC-V registers belong to one of these classes. */
925 enum reg_class
927 RCLASS_GPR,
928 RCLASS_FPR,
929 RCLASS_VECR,
930 RCLASS_VECM,
931 RCLASS_MAX,
933 RCLASS_CSR
936 static htab_t reg_names_hash = NULL;
937 static htab_t csr_extra_hash = NULL;
939 #define ENCODE_REG_HASH(cls, n) \
940 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
941 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
942 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
944 static void
945 hash_reg_name (enum reg_class class, const char *name, unsigned n)
947 void *hash = ENCODE_REG_HASH (class, n);
948 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
949 as_fatal (_("internal: duplicate %s"), name);
952 static void
953 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
955 unsigned i;
957 for (i = 0; i < n; i++)
958 hash_reg_name (class, names[i], i);
961 /* Init hash table csr_extra_hash to handle CSR. */
963 static void
964 riscv_init_csr_hash (const char *name,
965 unsigned address,
966 enum riscv_csr_class class,
967 enum riscv_spec_class define_version,
968 enum riscv_spec_class abort_version)
970 struct riscv_csr_extra *entry, *pre_entry;
971 bool need_enrty = true;
973 pre_entry = NULL;
974 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
975 while (need_enrty && entry != NULL)
977 if (entry->csr_class == class
978 && entry->address == address
979 && entry->define_version == define_version
980 && entry->abort_version == abort_version)
981 need_enrty = false;
982 pre_entry = entry;
983 entry = entry->next;
986 /* Duplicate CSR. */
987 if (!need_enrty)
988 return;
990 entry = notes_alloc (sizeof (*entry));
991 entry->csr_class = class;
992 entry->address = address;
993 entry->define_version = define_version;
994 entry->abort_version = abort_version;
995 entry->next = NULL;
997 if (pre_entry == NULL)
998 str_hash_insert (csr_extra_hash, name, entry, 0);
999 else
1000 pre_entry->next = entry;
1003 /* Return the CSR address after checking the ISA dependency and
1004 the privileged spec version.
1006 There are one warning and two errors for CSR,
1008 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1009 or the privileged spec, report warning only if -mcsr-check is set.
1010 Unknown CSR: the CSR has never been defined, report error.
1011 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1013 static unsigned int
1014 riscv_csr_address (const char *csr_name,
1015 struct riscv_csr_extra *entry)
1017 struct riscv_csr_extra *saved_entry = entry;
1018 enum riscv_csr_class csr_class = entry->csr_class;
1019 bool need_check_version = false;
1020 bool is_rv32_only = false;
1021 bool is_h_required = false;
1022 const char* extension = NULL;
1024 switch (csr_class)
1026 case CSR_CLASS_I_32:
1027 is_rv32_only = true;
1028 /* Fall through. */
1029 case CSR_CLASS_I:
1030 need_check_version = true;
1031 extension = "i";
1032 break;
1033 case CSR_CLASS_H_32:
1034 is_rv32_only = true;
1035 /* Fall through. */
1036 case CSR_CLASS_H:
1037 extension = "h";
1038 break;
1039 case CSR_CLASS_F:
1040 extension = "f";
1041 break;
1042 case CSR_CLASS_ZKR:
1043 extension = "zkr";
1044 break;
1045 case CSR_CLASS_V:
1046 extension = "zve32x";
1047 break;
1048 case CSR_CLASS_SMAIA_32:
1049 is_rv32_only = true;
1050 /* Fall through. */
1051 case CSR_CLASS_SMAIA:
1052 extension = "smaia";
1053 break;
1054 case CSR_CLASS_SMSTATEEN_32:
1055 is_rv32_only = true;
1056 /* Fall through. */
1057 case CSR_CLASS_SMSTATEEN:
1058 extension = "smstateen";
1059 break;
1060 case CSR_CLASS_SSAIA:
1061 case CSR_CLASS_SSAIA_AND_H:
1062 case CSR_CLASS_SSAIA_32:
1063 case CSR_CLASS_SSAIA_AND_H_32:
1064 is_rv32_only = (csr_class == CSR_CLASS_SSAIA_32
1065 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1066 is_h_required = (csr_class == CSR_CLASS_SSAIA_AND_H
1067 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1068 extension = "ssaia";
1069 break;
1070 case CSR_CLASS_SSSTATEEN_AND_H_32:
1071 is_rv32_only = true;
1072 /* Fall through. */
1073 case CSR_CLASS_SSSTATEEN_AND_H:
1074 is_h_required = true;
1075 /* Fall through. */
1076 case CSR_CLASS_SSSTATEEN:
1077 extension = "ssstateen";
1078 break;
1079 case CSR_CLASS_SSCOFPMF_32:
1080 is_rv32_only = true;
1081 /* Fall through. */
1082 case CSR_CLASS_SSCOFPMF:
1083 extension = "sscofpmf";
1084 break;
1085 case CSR_CLASS_SSTC:
1086 case CSR_CLASS_SSTC_AND_H:
1087 case CSR_CLASS_SSTC_32:
1088 case CSR_CLASS_SSTC_AND_H_32:
1089 is_rv32_only = (csr_class == CSR_CLASS_SSTC_32
1090 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1091 is_h_required = (csr_class == CSR_CLASS_SSTC_AND_H
1092 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1093 extension = "sstc";
1094 break;
1095 case CSR_CLASS_DEBUG:
1096 break;
1097 default:
1098 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
1101 if (riscv_opts.csr_check)
1103 if (is_rv32_only && xlen != 32)
1104 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name);
1105 if (is_h_required && !riscv_subset_supports (&riscv_rps_as, "h"))
1106 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name);
1108 if (extension != NULL
1109 && !riscv_subset_supports (&riscv_rps_as, extension))
1110 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1111 csr_name, extension);
1114 while (entry != NULL)
1116 if (!need_check_version
1117 || (default_priv_spec >= entry->define_version
1118 && default_priv_spec < entry->abort_version))
1120 /* Find the CSR according to the specific version. */
1121 return entry->address;
1123 entry = entry->next;
1126 /* Can not find the CSR address from the chosen privileged version,
1127 so use the newly defined value. */
1128 if (riscv_opts.csr_check)
1130 const char *priv_name = NULL;
1131 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
1132 if (priv_name != NULL)
1133 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1134 csr_name, priv_name);
1137 return saved_entry->address;
1140 /* Return -1 if the CSR has never been defined. Otherwise, return
1141 the address. */
1143 static unsigned int
1144 reg_csr_lookup_internal (const char *s)
1146 struct riscv_csr_extra *r =
1147 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
1149 if (r == NULL)
1150 return -1U;
1152 return riscv_csr_address (s, r);
1155 static unsigned int
1156 reg_lookup_internal (const char *s, enum reg_class class)
1158 void *r;
1160 if (class == RCLASS_CSR)
1161 return reg_csr_lookup_internal (s);
1163 r = str_hash_find (reg_names_hash, s);
1164 if (r == NULL || DECODE_REG_CLASS (r) != class)
1165 return -1;
1167 if (riscv_subset_supports (&riscv_rps_as, "e")
1168 && class == RCLASS_GPR
1169 && DECODE_REG_NUM (r) > 15)
1170 return -1;
1172 return DECODE_REG_NUM (r);
1175 static bool
1176 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1178 char *e;
1179 char save_c;
1180 int reg = -1;
1182 /* Find end of name. */
1183 e = *s;
1184 if (is_name_beginner (*e))
1185 ++e;
1186 while (is_part_of_name (*e))
1187 ++e;
1189 /* Terminate name. */
1190 save_c = *e;
1191 *e = '\0';
1193 /* Look for the register. Advance to next token if one was recognized. */
1194 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1195 *s = e;
1197 *e = save_c;
1198 if (regnop)
1199 *regnop = reg;
1200 return reg >= 0;
1203 static bool
1204 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
1206 const char *p = strchr (*s, ',');
1207 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
1209 if (len == 0)
1210 return false;
1212 for (i = 0; i < size; i++)
1213 if (array[i] != NULL && strncmp (array[i], *s, len) == 0
1214 && array[i][len] == '\0')
1216 *regnop = i;
1217 *s += len;
1218 return true;
1221 return false;
1224 static bool
1225 flt_lookup (float f, const float *array, size_t size, unsigned *regnop)
1227 size_t i;
1229 for (i = 0; i < size; i++)
1230 if (array[i] == f)
1232 *regnop = i;
1233 return true;
1236 return false;
1239 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1240 #define USE_IMM(n, s) \
1241 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1243 /* For consistency checking, verify that all bits are specified either
1244 by the match/mask part of the instruction definition, or by the
1245 operand list. The `length` could be the actual instruction length or
1246 0 for auto-detection. */
1248 static bool
1249 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1251 const char *oparg, *opargStart;
1252 insn_t used_bits = opc->mask;
1253 int insn_width;
1254 insn_t required_bits;
1256 if (length == 0)
1257 length = riscv_insn_length (opc->match);
1258 /* We don't support instructions longer than 64-bits yet. */
1259 if (length > 8)
1260 length = 8;
1261 insn_width = 8 * length;
1263 required_bits = ((insn_t)~0ULL) >> (64 - insn_width);
1265 if ((used_bits & opc->match) != (opc->match & required_bits))
1267 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1268 opc->name, opc->args);
1269 return false;
1272 for (oparg = opc->args; *oparg; ++oparg)
1274 opargStart = oparg;
1275 switch (*oparg)
1277 case 'C': /* RVC */
1278 switch (*++oparg)
1280 case 'U': break; /* CRS1, constrained to equal RD. */
1281 case 'c': break; /* CRS1, constrained to equal sp. */
1282 case 'T': /* CRS2, floating point. */
1283 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1284 case 'S': /* CRS1S, floating point. */
1285 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1286 case 'w': break; /* CRS1S, constrained to equal RD. */
1287 case 'D': /* CRS2S, floating point. */
1288 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1289 case 'x': break; /* CRS2S, constrained to equal RD. */
1290 case 'z': break; /* CRS2S, constrained to be x0. */
1291 case '>': /* CITYPE immediate, compressed shift. */
1292 case 'u': /* CITYPE immediate, compressed lui. */
1293 case 'v': /* CITYPE immediate, li to compressed lui. */
1294 case 'o': /* CITYPE immediate, allow zero. */
1295 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1296 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1297 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1298 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1299 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1300 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1301 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1302 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1303 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1304 /* CLTYPE and CSTYPE have the same immediate encoding. */
1305 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1306 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1307 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1308 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1309 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1310 case 'F': /* Compressed funct for .insn directive. */
1311 switch (*++oparg)
1313 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1314 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1315 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1316 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1317 default:
1318 goto unknown_validate_operand;
1320 break;
1321 default:
1322 goto unknown_validate_operand;
1324 break; /* end RVC */
1325 case 'V': /* RVV */
1326 switch (*++oparg)
1328 case 'd':
1329 case 'f': USE_BITS (OP_MASK_VD, OP_SH_VD); break;
1330 case 'e': USE_BITS (OP_MASK_VWD, OP_SH_VWD); break;
1331 case 's': USE_BITS (OP_MASK_VS1, OP_SH_VS1); break;
1332 case 't': USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1333 case 'u': USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1334 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1335 case 'v': USE_BITS (OP_MASK_VD, OP_SH_VD);
1336 USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1337 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1338 case '0': break;
1339 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1340 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1341 case 'i':
1342 case 'j':
1343 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1344 case 'l': used_bits |= ENCODE_RVV_VI_UIMM6 (-1U); break;
1345 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1346 case 'M': break; /* Macro operand, must be a mask register. */
1347 case 'T': break; /* Macro operand, must be a vector register. */
1348 default:
1349 goto unknown_validate_operand;
1351 break; /* end RVV */
1352 case ',': break;
1353 case '(': break;
1354 case ')': break;
1355 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1356 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1357 case 'A': break; /* Macro operand, must be symbol. */
1358 case 'B': break; /* Macro operand, must be symbol or constant. */
1359 case 'c': break; /* Macro operand, must be symbol or constant. */
1360 case 'I': break; /* Macro operand, must be constant. */
1361 case 'D': /* RD, floating point. */
1362 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1363 case 'y': USE_BITS (OP_MASK_BS, OP_SH_BS); break;
1364 case 'Y': USE_BITS (OP_MASK_RNUM, OP_SH_RNUM); break;
1365 case 'Z': /* RS1, CSR number. */
1366 case 'S': /* RS1, floating point. */
1367 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1368 case 'U': /* RS1 and RS2 are the same, floating point. */
1369 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1370 /* Fall through. */
1371 case 'T': /* RS2, floating point. */
1372 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1373 case 'R': /* RS3, floating point. */
1374 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1375 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1376 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1377 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1378 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1379 case 'o': /* ITYPE immediate, load displacement. */
1380 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1381 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1382 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1383 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1384 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1385 case 'z': break; /* Zero immediate. */
1386 case '[': break; /* Unused operand. */
1387 case ']': break; /* Unused operand. */
1388 case '0': break; /* AMO displacement, must to zero. */
1389 case '1': break; /* Relaxation operand. */
1390 case 'F': /* Funct for .insn directive. */
1391 switch (*++oparg)
1393 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1394 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1395 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1396 default:
1397 goto unknown_validate_operand;
1399 break;
1400 case 'O': /* Opcode for .insn directive. */
1401 switch (*++oparg)
1403 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1404 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1405 default:
1406 goto unknown_validate_operand;
1408 break;
1409 case 'W': /* Various operands. */
1410 switch (*++oparg)
1412 case 'i':
1413 switch (*++oparg)
1415 case 'f': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1416 default:
1417 goto unknown_validate_operand;
1419 break;
1420 case 'f':
1421 switch (*++oparg)
1423 case 'v': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1424 default:
1425 goto unknown_validate_operand;
1427 break;
1428 default:
1429 goto unknown_validate_operand;
1431 break;
1432 case 'X': /* Integer immediate. */
1434 size_t n;
1435 size_t s;
1437 switch (*++oparg)
1439 case 'l': /* Literal. */
1440 oparg += strcspn(oparg, ",") - 1;
1441 break;
1442 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
1443 goto use_imm;
1444 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
1445 goto use_imm;
1446 use_imm:
1447 n = strtol (oparg + 1, (char **)&oparg, 10);
1448 if (*oparg != '@')
1449 goto unknown_validate_operand;
1450 s = strtol (oparg + 1, (char **)&oparg, 10);
1451 oparg--;
1453 USE_IMM (n, s);
1454 break;
1455 default:
1456 goto unknown_validate_operand;
1459 break;
1460 default:
1461 unknown_validate_operand:
1462 as_bad (_("internal: bad RISC-V opcode "
1463 "(unknown operand type `%s'): %s %s"),
1464 opargStart, opc->name, opc->args);
1465 return false;
1469 if (used_bits != required_bits)
1471 as_bad (_("internal: bad RISC-V opcode "
1472 "(bits %#llx undefined or invalid): %s %s"),
1473 (unsigned long long)(used_bits ^ required_bits),
1474 opc->name, opc->args);
1475 return false;
1477 return true;
1480 #undef USE_BITS
1482 struct percent_op_match
1484 const char *str;
1485 bfd_reloc_code_real_type reloc;
1488 /* Common hash table initialization function for instruction and .insn
1489 directive. */
1491 static htab_t
1492 init_opcode_hash (const struct riscv_opcode *opcodes,
1493 bool insn_directive_p)
1495 int i = 0;
1496 int length;
1497 htab_t hash = str_htab_create ();
1498 while (opcodes[i].name)
1500 const char *name = opcodes[i].name;
1501 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1502 as_fatal (_("internal: duplicate %s"), name);
1506 if (opcodes[i].pinfo != INSN_MACRO)
1508 if (insn_directive_p)
1509 length = ((name[0] == 'c') ? 2 : 4);
1510 else
1511 length = 0; /* Let assembler determine the length. */
1512 if (!validate_riscv_insn (&opcodes[i], length))
1513 as_fatal (_("internal: broken assembler. "
1514 "No assembly attempted"));
1516 else
1517 gas_assert (!insn_directive_p);
1518 ++i;
1520 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1523 return hash;
1526 /* This function is called once, at assembler startup time. It should set up
1527 all the tables, etc. that the MD part of the assembler will need. */
1529 void
1530 md_begin (void)
1532 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1534 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1535 as_warn (_("could not set architecture and machine"));
1537 op_hash = init_opcode_hash (riscv_opcodes, false);
1538 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1540 reg_names_hash = str_htab_create ();
1541 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1542 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1543 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1544 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1545 hash_reg_names (RCLASS_VECR, riscv_vecr_names_numeric, NVECR);
1546 hash_reg_names (RCLASS_VECM, riscv_vecm_names_numeric, NVECM);
1547 /* Add "fp" as an alias for "s0". */
1548 hash_reg_name (RCLASS_GPR, "fp", 8);
1550 /* Create and insert CSR hash tables. */
1551 csr_extra_hash = str_htab_create ();
1552 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1553 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1554 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1555 DECLARE_CSR(name, num, class, define_version, abort_version);
1556 #include "opcode/riscv-opc.h"
1557 #undef DECLARE_CSR
1559 opcode_names_hash = str_htab_create ();
1560 init_opcode_names_hash ();
1562 /* Set the default alignment for the text section. */
1563 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1566 static insn_t
1567 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1569 switch (reloc_type)
1571 case BFD_RELOC_32:
1572 return value;
1574 case BFD_RELOC_RISCV_HI20:
1575 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1577 case BFD_RELOC_RISCV_LO12_S:
1578 return ENCODE_STYPE_IMM (value);
1580 case BFD_RELOC_RISCV_LO12_I:
1581 return ENCODE_ITYPE_IMM (value);
1583 default:
1584 abort ();
1588 /* Output an instruction. IP is the instruction information.
1589 ADDRESS_EXPR is an operand of the instruction to be used with
1590 RELOC_TYPE. */
1592 static void
1593 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1594 bfd_reloc_code_real_type reloc_type)
1596 dwarf2_emit_insn (0);
1598 if (reloc_type != BFD_RELOC_UNUSED)
1600 reloc_howto_type *howto;
1602 gas_assert (address_expr);
1603 if (reloc_type == BFD_RELOC_12_PCREL
1604 || reloc_type == BFD_RELOC_RISCV_JMP)
1606 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1607 int best_case = insn_length (ip);
1608 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1610 if (now_seg == absolute_section)
1612 as_bad (_("relaxable branches not supported in absolute section"));
1613 return;
1616 add_relaxed_insn (ip, worst_case, best_case,
1617 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1618 address_expr->X_add_symbol,
1619 address_expr->X_add_number);
1620 return;
1622 else
1624 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1625 if (howto == NULL)
1626 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1627 reloc_type);
1629 ip->fixp = fix_new_exp (ip->frag, ip->where,
1630 bfd_get_reloc_size (howto),
1631 address_expr, false, reloc_type);
1633 ip->fixp->fx_tcbit = riscv_opts.relax;
1637 add_fixed_insn (ip);
1639 /* We need to start a new frag after any instruction that can be
1640 optimized away or compressed by the linker during relaxation, to prevent
1641 the assembler from computing static offsets across such an instruction.
1642 This is necessary to get correct EH info. */
1643 if (reloc_type == BFD_RELOC_RISCV_HI20
1644 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1645 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1646 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1648 frag_wane (frag_now);
1649 frag_new (0);
1653 /* Build an instruction created by a macro expansion. This is passed
1654 a pointer to the count of instructions created so far, an expression,
1655 the name of the instruction to build, an operand format string, and
1656 corresponding arguments. */
1658 static void
1659 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1661 const struct riscv_opcode *mo;
1662 struct riscv_cl_insn insn;
1663 bfd_reloc_code_real_type r;
1664 va_list args;
1665 const char *fmtStart;
1667 va_start (args, fmt);
1669 r = BFD_RELOC_UNUSED;
1670 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1671 gas_assert (mo);
1673 /* Find a non-RVC variant of the instruction. append_insn will compress
1674 it if possible. */
1675 while (riscv_insn_length (mo->match) < 4)
1676 mo++;
1677 gas_assert (strcmp (name, mo->name) == 0);
1679 create_insn (&insn, mo);
1680 for (;; ++fmt)
1682 fmtStart = fmt;
1683 switch (*fmt)
1685 case 'V': /* RVV */
1686 switch (*++fmt)
1688 case 'd':
1689 INSERT_OPERAND (VD, insn, va_arg (args, int));
1690 continue;
1691 case 's':
1692 INSERT_OPERAND (VS1, insn, va_arg (args, int));
1693 continue;
1694 case 't':
1695 INSERT_OPERAND (VS2, insn, va_arg (args, int));
1696 continue;
1697 case 'm':
1699 int reg = va_arg (args, int);
1700 if (reg == -1)
1702 INSERT_OPERAND (VMASK, insn, 1);
1703 continue;
1705 else if (reg == 0)
1707 INSERT_OPERAND (VMASK, insn, 0);
1708 continue;
1710 else
1711 goto unknown_macro_argument;
1713 default:
1714 goto unknown_macro_argument;
1716 break;
1718 case 'd':
1719 INSERT_OPERAND (RD, insn, va_arg (args, int));
1720 continue;
1721 case 's':
1722 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1723 continue;
1724 case 't':
1725 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1726 continue;
1728 case 'j':
1729 case 'u':
1730 case 'q':
1731 gas_assert (ep != NULL);
1732 r = va_arg (args, int);
1733 continue;
1735 case '\0':
1736 break;
1737 case ',':
1738 continue;
1739 default:
1740 unknown_macro_argument:
1741 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
1743 break;
1745 va_end (args);
1746 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1748 append_insn (&insn, ep, r);
1751 /* Build an instruction created by a macro expansion. Like md_assemble but
1752 accept a printf-style format string and arguments. */
1754 static void
1755 md_assemblef (const char *format, ...)
1757 char *buf = NULL;
1758 va_list ap;
1759 int r;
1761 va_start (ap, format);
1763 r = vasprintf (&buf, format, ap);
1765 if (r < 0)
1766 as_fatal (_("internal: vasprintf failed"));
1768 md_assemble (buf);
1769 free(buf);
1771 va_end (ap);
1774 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1775 unset. */
1777 static void
1778 normalize_constant_expr (expressionS *ex)
1780 if (xlen > 32)
1781 return;
1782 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
1783 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
1784 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
1785 - 0x80000000);
1788 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1789 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1791 static void
1792 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1793 bool maybe_csr)
1795 if (ex->X_op == O_big)
1796 as_bad (_("unsupported large constant"));
1797 else if (maybe_csr && ex->X_op == O_symbol)
1798 as_bad (_("unknown CSR `%s'"),
1799 S_GET_NAME (ex->X_add_symbol));
1800 else if (ex->X_op != O_constant)
1801 as_bad (_("instruction %s requires absolute expression"),
1802 ip->insn_mo->name);
1803 normalize_constant_expr (ex);
1806 static symbolS *
1807 make_internal_label (void)
1809 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1810 frag_now_fix ());
1813 /* Load an entry from the GOT. */
1815 static void
1816 pcrel_access (int destreg, int tempreg, expressionS *ep,
1817 const char *lo_insn, const char *lo_pattern,
1818 bfd_reloc_code_real_type hi_reloc,
1819 bfd_reloc_code_real_type lo_reloc)
1821 expressionS ep2;
1822 ep2.X_op = O_symbol;
1823 ep2.X_add_symbol = make_internal_label ();
1824 ep2.X_add_number = 0;
1826 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1827 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1830 static void
1831 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1832 bfd_reloc_code_real_type hi_reloc,
1833 bfd_reloc_code_real_type lo_reloc)
1835 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1838 static void
1839 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1840 bfd_reloc_code_real_type hi_reloc,
1841 bfd_reloc_code_real_type lo_reloc)
1843 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1846 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1848 static void
1849 riscv_call (int destreg, int tempreg, expressionS *ep,
1850 bfd_reloc_code_real_type reloc)
1852 /* Ensure the jalr is emitted to the same frag as the auipc. */
1853 frag_grow (8);
1854 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1855 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1856 /* See comment at end of append_insn. */
1857 frag_wane (frag_now);
1858 frag_new (0);
1861 /* Load an integer constant into a register. */
1863 static void
1864 load_const (int reg, expressionS *ep)
1866 int shift = RISCV_IMM_BITS;
1867 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
1868 expressionS upper = *ep, lower = *ep;
1869 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
1870 upper.X_add_number -= lower.X_add_number;
1872 if (ep->X_op != O_constant)
1874 as_bad (_("unsupported large constant"));
1875 return;
1878 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
1880 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1881 while (((upper.X_add_number >> shift) & 1) == 0)
1882 shift++;
1884 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1885 load_const (reg, &upper);
1887 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1888 if (lower.X_add_number != 0)
1889 md_assemblef ("addi x%d, x%d, %" PRId64, reg, reg,
1890 (int64_t) lower.X_add_number);
1892 else
1894 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1895 int hi_reg = 0;
1897 if (upper.X_add_number != 0)
1899 /* Discard low part and zero-extend upper immediate. */
1900 upper_imm = ((uint32_t)upper.X_add_number >> shift);
1902 md_assemblef ("lui x%d, 0x%" PRIx64, reg, (uint64_t) upper_imm);
1903 hi_reg = reg;
1906 if (lower.X_add_number != 0 || hi_reg == 0)
1907 md_assemblef ("%s x%d, x%d, %" PRId64, ADD32_INSN, reg, hi_reg,
1908 (int64_t) lower.X_add_number);
1912 /* Zero extend and sign extend byte/half-word/word. */
1914 static void
1915 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
1917 if (sign)
1919 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1920 md_assemblef ("srai x%d, x%d, 0x%x", destreg, destreg, shift);
1922 else
1924 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1925 md_assemblef ("srli x%d, x%d, 0x%x", destreg, destreg, shift);
1929 /* Expand RISC-V Vector macros into one or more instructions. */
1931 static void
1932 vector_macro (struct riscv_cl_insn *ip)
1934 int vd = (ip->insn_opcode >> OP_SH_VD) & OP_MASK_VD;
1935 int vs1 = (ip->insn_opcode >> OP_SH_VS1) & OP_MASK_VS1;
1936 int vs2 = (ip->insn_opcode >> OP_SH_VS2) & OP_MASK_VS2;
1937 int vm = (ip->insn_opcode >> OP_SH_VMASK) & OP_MASK_VMASK;
1938 int vtemp = (ip->insn_opcode >> OP_SH_VFUNCT6) & OP_MASK_VFUNCT6;
1939 int mask = ip->insn_mo->mask;
1941 switch (mask)
1943 case M_VMSGE:
1944 if (vm)
1946 /* Unmasked. */
1947 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1948 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1949 break;
1951 if (vtemp != 0)
1953 /* Masked. Have vtemp to avoid overlap constraints. */
1954 if (vd == vm)
1956 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1957 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1959 else
1961 /* Preserve the value of vd if not updating by vm. */
1962 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1963 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1964 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
1965 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
1968 else if (vd != vm)
1970 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1971 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
1972 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
1974 else
1975 as_bad (_("must provide temp if destination overlaps mask"));
1976 break;
1978 case M_VMSGEU:
1979 if (vm)
1981 /* Unmasked. */
1982 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1983 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1984 break;
1986 if (vtemp != 0)
1988 /* Masked. Have vtemp to avoid overlap constraints. */
1989 if (vd == vm)
1991 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1992 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1994 else
1996 /* Preserve the value of vd if not updating by vm. */
1997 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1998 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1999 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
2000 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
2003 else if (vd != vm)
2005 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2006 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
2007 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
2009 else
2010 as_bad (_("must provide temp if destination overlaps mask"));
2011 break;
2013 default:
2014 break;
2018 /* Expand RISC-V assembly macros into one or more instructions. */
2020 static void
2021 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
2022 bfd_reloc_code_real_type *imm_reloc)
2024 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
2025 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
2026 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
2027 int mask = ip->insn_mo->mask;
2029 switch (mask)
2031 case M_LI:
2032 load_const (rd, imm_expr);
2033 break;
2035 case M_LA:
2036 case M_LLA:
2037 case M_LGA:
2038 /* Load the address of a symbol into a register. */
2039 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
2040 as_bad (_("offset too large"));
2042 if (imm_expr->X_op == O_constant)
2043 load_const (rd, imm_expr);
2044 /* Global PIC symbol. */
2045 else if ((riscv_opts.pic && mask == M_LA)
2046 || mask == M_LGA)
2047 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2048 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2049 /* Local PIC symbol, or any non-PIC symbol. */
2050 else
2051 pcrel_load (rd, rd, imm_expr, "addi",
2052 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2053 break;
2055 case M_LA_TLS_GD:
2056 pcrel_load (rd, rd, imm_expr, "addi",
2057 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2058 break;
2060 case M_LA_TLS_IE:
2061 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2062 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2063 break;
2065 case M_LB:
2066 pcrel_load (rd, rd, imm_expr, "lb",
2067 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2068 break;
2070 case M_LBU:
2071 pcrel_load (rd, rd, imm_expr, "lbu",
2072 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2073 break;
2075 case M_LH:
2076 pcrel_load (rd, rd, imm_expr, "lh",
2077 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2078 break;
2080 case M_LHU:
2081 pcrel_load (rd, rd, imm_expr, "lhu",
2082 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2083 break;
2085 case M_LW:
2086 pcrel_load (rd, rd, imm_expr, "lw",
2087 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2088 break;
2090 case M_LWU:
2091 pcrel_load (rd, rd, imm_expr, "lwu",
2092 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2093 break;
2095 case M_LD:
2096 pcrel_load (rd, rd, imm_expr, "ld",
2097 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2098 break;
2100 case M_FLW:
2101 pcrel_load (rd, rs1, imm_expr, "flw",
2102 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2103 break;
2105 case M_FLD:
2106 pcrel_load (rd, rs1, imm_expr, "fld",
2107 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2108 break;
2110 case M_SB:
2111 pcrel_store (rs2, rs1, imm_expr, "sb",
2112 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2113 break;
2115 case M_SH:
2116 pcrel_store (rs2, rs1, imm_expr, "sh",
2117 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2118 break;
2120 case M_SW:
2121 pcrel_store (rs2, rs1, imm_expr, "sw",
2122 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2123 break;
2125 case M_SD:
2126 pcrel_store (rs2, rs1, imm_expr, "sd",
2127 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2128 break;
2130 case M_FSW:
2131 pcrel_store (rs2, rs1, imm_expr, "fsw",
2132 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2133 break;
2135 case M_FSD:
2136 pcrel_store (rs2, rs1, imm_expr, "fsd",
2137 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2138 break;
2140 case M_CALL:
2141 riscv_call (rd, rs1, imm_expr, *imm_reloc);
2142 break;
2144 case M_ZEXTH:
2145 riscv_ext (rd, rs1, xlen - 16, false);
2146 break;
2148 case M_ZEXTW:
2149 riscv_ext (rd, rs1, xlen - 32, false);
2150 break;
2152 case M_SEXTB:
2153 riscv_ext (rd, rs1, xlen - 8, true);
2154 break;
2156 case M_SEXTH:
2157 riscv_ext (rd, rs1, xlen - 16, true);
2158 break;
2160 case M_VMSGE:
2161 case M_VMSGEU:
2162 vector_macro (ip);
2163 break;
2165 case M_FLH:
2166 pcrel_load (rd, rs1, imm_expr, "flh",
2167 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2168 break;
2169 case M_FSH:
2170 pcrel_store (rs2, rs1, imm_expr, "fsh",
2171 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2172 break;
2174 default:
2175 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
2176 break;
2180 static const struct percent_op_match percent_op_utype[] =
2182 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
2183 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
2184 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
2185 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
2186 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
2187 {"hi", BFD_RELOC_RISCV_HI20},
2188 {0, 0}
2191 static const struct percent_op_match percent_op_itype[] =
2193 {"lo", BFD_RELOC_RISCV_LO12_I},
2194 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
2195 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
2196 {0, 0}
2199 static const struct percent_op_match percent_op_stype[] =
2201 {"lo", BFD_RELOC_RISCV_LO12_S},
2202 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
2203 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
2204 {0, 0}
2207 static const struct percent_op_match percent_op_rtype[] =
2209 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
2210 {0, 0}
2213 static const struct percent_op_match percent_op_null[] =
2215 {0, 0}
2218 /* Return true if *STR points to a relocation operator. When returning true,
2219 move *STR over the operator and store its relocation code in *RELOC.
2220 Leave both *STR and *RELOC alone when returning false. */
2222 static bool
2223 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
2224 const struct percent_op_match *percent_op)
2226 for ( ; percent_op->str; percent_op++)
2227 if (strncasecmp (*str + 1, percent_op->str, strlen (percent_op->str)) == 0)
2229 size_t len = 1 + strlen (percent_op->str);
2231 while (ISSPACE ((*str)[len]))
2232 ++len;
2233 if ((*str)[len] != '(')
2234 continue;
2236 *str += len;
2237 *reloc = percent_op->reloc;
2239 /* Check whether the output BFD supports this relocation.
2240 If not, issue an error and fall back on something safe. */
2241 if (*reloc != BFD_RELOC_UNUSED
2242 && !bfd_reloc_type_lookup (stdoutput, *reloc))
2244 as_bad ("internal: relocation %s isn't supported by the "
2245 "current ABI", percent_op->str);
2246 *reloc = BFD_RELOC_UNUSED;
2248 return true;
2250 return false;
2253 static void
2254 my_getExpression (expressionS *ep, char *str)
2256 char *save_in;
2258 save_in = input_line_pointer;
2259 input_line_pointer = str;
2260 expression (ep);
2261 expr_parse_end = input_line_pointer;
2262 input_line_pointer = save_in;
2265 /* Parse string STR as a 16-bit relocatable operand. Store the
2266 expression in *EP and the relocation, if any, in RELOC.
2267 Return the number of relocation operators used (0 or 1).
2269 On exit, EXPR_PARSE_END points to the first character after the
2270 expression. */
2272 static size_t
2273 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2274 char *str, const struct percent_op_match *percent_op)
2276 size_t reloc_index;
2277 unsigned crux_depth, str_depth;
2278 bool orig_probing = probing_insn_operands;
2279 char *crux;
2281 /* Search for the start of the main expression.
2283 End the loop with CRUX pointing to the start of the main expression and
2284 with CRUX_DEPTH containing the number of open brackets at that point. */
2285 reloc_index = -1;
2286 str_depth = 0;
2289 reloc_index++;
2290 crux = str;
2291 crux_depth = str_depth;
2293 /* Skip over whitespace and brackets, keeping count of the number
2294 of brackets. */
2295 while (*str == ' ' || *str == '\t' || *str == '(')
2296 if (*str++ == '(')
2297 str_depth++;
2299 while (*str == '%'
2300 && reloc_index < 1
2301 && parse_relocation (&str, reloc, percent_op));
2303 if (*str == '%')
2305 /* expression() will choke on anything looking like an (unrecognized)
2306 relocation specifier. Don't even call it, avoiding multiple (and
2307 perhaps redundant) error messages; our caller will issue one. */
2308 ep->X_op = O_illegal;
2309 return 0;
2312 /* Anything inside parentheses or subject to a relocation operator cannot
2313 be a register and hence can be treated the same as operands to
2314 directives (other than .insn). */
2315 if (str_depth || reloc_index)
2316 probing_insn_operands = false;
2318 my_getExpression (ep, crux);
2319 str = expr_parse_end;
2321 probing_insn_operands = orig_probing;
2323 /* Match every open bracket. */
2324 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2325 if (*str++ == ')')
2326 crux_depth--;
2328 if (crux_depth > 0)
2329 as_bad ("unclosed '('");
2331 expr_parse_end = str;
2333 return reloc_index;
2336 /* Parse opcode name, could be an mnemonics or number. */
2338 static size_t
2339 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2340 char *str)
2342 const struct opcode_name_t *o = opcode_name_lookup (&str);
2344 if (o != NULL)
2346 ep->X_op = O_constant;
2347 ep->X_add_number = o->val;
2348 return 0;
2351 return my_getSmallExpression (ep, reloc, str, percent_op_null);
2354 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2355 On exit, EXPR_PARSE_END points to the first character after the
2356 expression. */
2358 static void
2359 my_getVsetvliExpression (expressionS *ep, char *str)
2361 unsigned int vsew_value = 0, vlmul_value = 0;
2362 unsigned int vta_value = 0, vma_value = 0;
2363 bfd_boolean vsew_found = FALSE, vlmul_found = FALSE;
2364 bfd_boolean vta_found = FALSE, vma_found = FALSE;
2366 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew), &vsew_value))
2368 if (*str == ',')
2369 ++str;
2370 if (vsew_found)
2371 as_bad (_("multiple vsew constants"));
2372 vsew_found = TRUE;
2374 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2376 if (*str == ',')
2377 ++str;
2378 if (vlmul_found)
2379 as_bad (_("multiple vlmul constants"));
2380 vlmul_found = TRUE;
2382 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2384 if (*str == ',')
2385 ++str;
2386 if (vta_found)
2387 as_bad (_("multiple vta constants"));
2388 vta_found = TRUE;
2390 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2392 if (*str == ',')
2393 ++str;
2394 if (vma_found)
2395 as_bad (_("multiple vma constants"));
2396 vma_found = TRUE;
2399 if (vsew_found || vlmul_found || vta_found || vma_found)
2401 ep->X_op = O_constant;
2402 ep->X_add_number = (vlmul_value << OP_SH_VLMUL)
2403 | (vsew_value << OP_SH_VSEW)
2404 | (vta_value << OP_SH_VTA)
2405 | (vma_value << OP_SH_VMA);
2406 expr_parse_end = str;
2408 else
2410 my_getExpression (ep, str);
2411 str = expr_parse_end;
2415 /* Detect and handle implicitly zero load-store offsets. For example,
2416 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2417 an implicit offset was detected. */
2419 static bool
2420 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
2422 /* Check whether there is only a single bracketed expression left.
2423 If so, it must be the base register and the constant must be zero. */
2424 if (*s == '(' && strchr (s + 1, '(') == 0)
2426 ep->X_op = O_constant;
2427 ep->X_add_number = 0;
2428 return true;
2431 return false;
2434 /* All RISC-V CSR instructions belong to one of these classes. */
2435 enum csr_insn_type
2437 INSN_NOT_CSR,
2438 INSN_CSRRW,
2439 INSN_CSRRS,
2440 INSN_CSRRC
2443 /* Return which CSR instruction is checking. */
2445 static enum csr_insn_type
2446 riscv_csr_insn_type (insn_t insn)
2448 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
2449 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
2450 return INSN_CSRRW;
2451 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2452 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2453 return INSN_CSRRS;
2454 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2455 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2456 return INSN_CSRRC;
2457 else
2458 return INSN_NOT_CSR;
2461 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2462 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2463 CSR address is 0x3. */
2465 static bool
2466 riscv_csr_read_only_check (insn_t insn)
2468 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
2469 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
2470 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
2471 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
2473 if (readonly
2474 && (((csr_insn == INSN_CSRRS
2475 || csr_insn == INSN_CSRRC)
2476 && rs1 != 0)
2477 || csr_insn == INSN_CSRRW))
2478 return false;
2480 return true;
2483 /* Return true if it is a privileged instruction. Otherwise, return false.
2485 uret is actually a N-ext instruction. So it is better to regard it as
2486 an user instruction rather than the priv instruction.
2488 hret is used to return from traps in H-mode. H-mode is removed since
2489 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2490 Therefore, hret should be controlled by the hypervisor spec rather than
2491 priv spec in the future.
2493 dret is defined in the debug spec, so it should be checked in the future,
2494 too. */
2496 static bool
2497 riscv_is_priv_insn (insn_t insn)
2499 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
2500 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
2501 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
2502 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
2503 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2504 check it here to keep the compatible. */
2505 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
2508 static symbolS *deferred_sym_rootP;
2509 static symbolS *deferred_sym_lastP;
2510 /* Since symbols can't easily be freed, try to recycle ones which weren't
2511 committed. */
2512 static symbolS *orphan_sym_rootP;
2513 static symbolS *orphan_sym_lastP;
2515 /* This routine assembles an instruction into its binary format. As a
2516 side effect, it sets the global variable imm_reloc to the type of
2517 relocation to do if one of the operands is an address expression. */
2519 static struct riscv_ip_error
2520 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
2521 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
2523 /* The operand string defined in the riscv_opcodes. */
2524 const char *oparg, *opargStart;
2525 /* The parsed operands from assembly. */
2526 char *asarg, *asargStart;
2527 char save_c = 0;
2528 struct riscv_opcode *insn;
2529 unsigned int regno;
2530 const struct percent_op_match *p;
2531 struct riscv_ip_error error;
2532 error.msg = "unrecognized opcode";
2533 error.statement = str;
2534 error.missing_ext = NULL;
2535 /* Indicate we are assembling instruction with CSR. */
2536 bool insn_with_csr = false;
2538 /* Parse the name of the instruction. Terminate the string if whitespace
2539 is found so that str_hash_find only sees the name part of the string. */
2540 for (asarg = str; *asarg!= '\0'; ++asarg)
2541 if (ISSPACE (*asarg))
2543 save_c = *asarg;
2544 *asarg++ = '\0';
2545 break;
2548 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2550 probing_insn_operands = true;
2552 asargStart = asarg;
2553 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2555 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2556 continue;
2558 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2560 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2561 insn->insn_class);
2562 continue;
2565 /* Reset error message of the previous round. */
2566 error.msg = _("illegal operands");
2567 error.missing_ext = NULL;
2569 /* Purge deferred symbols from the previous round, if any. */
2570 while (deferred_sym_rootP)
2572 symbolS *sym = deferred_sym_rootP;
2574 symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP);
2575 symbol_append (sym, orphan_sym_lastP, &orphan_sym_rootP,
2576 &orphan_sym_lastP);
2579 create_insn (ip, insn);
2581 imm_expr->X_op = O_absent;
2582 *imm_reloc = BFD_RELOC_UNUSED;
2583 p = percent_op_null;
2585 for (oparg = insn->args;; ++oparg)
2587 opargStart = oparg;
2588 asarg += strspn (asarg, " \t");
2589 switch (*oparg)
2591 case '\0': /* End of args. */
2592 if (insn->pinfo != INSN_MACRO)
2594 if (!insn->match_func (insn, ip->insn_opcode))
2595 break;
2597 /* For .insn, insn->match and insn->mask are 0. */
2598 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2599 ? ip->insn_opcode
2600 : insn->match) == 2
2601 && !riscv_opts.rvc)
2602 break;
2604 if (riscv_is_priv_insn (ip->insn_opcode))
2605 explicit_priv_attr = true;
2607 /* Check if we write a read-only CSR by the CSR
2608 instruction. */
2609 if (insn_with_csr
2610 && riscv_opts.csr_check
2611 && !riscv_csr_read_only_check (ip->insn_opcode))
2613 /* Restore the character in advance, since we want to
2614 report the detailed warning message here. */
2615 if (save_c)
2616 *(asargStart - 1) = save_c;
2617 as_warn (_("read-only CSR is written `%s'"), str);
2618 insn_with_csr = false;
2621 /* The (segmant) load and store with EEW 64 cannot be used
2622 when zve32x is enabled. */
2623 if (ip->insn_mo->pinfo & INSN_V_EEW64
2624 && riscv_subset_supports (&riscv_rps_as, "zve32x")
2625 && !riscv_subset_supports (&riscv_rps_as, "zve64x"))
2627 error.msg = _("illegal opcode for zve32x");
2628 break;
2631 if (*asarg != '\0')
2632 break;
2634 /* Successful assembly. */
2635 error.msg = NULL;
2636 insn_with_csr = false;
2638 /* Commit deferred symbols, if any. */
2639 while (deferred_sym_rootP)
2641 symbolS *sym = deferred_sym_rootP;
2643 symbol_remove (sym, &deferred_sym_rootP,
2644 &deferred_sym_lastP);
2645 symbol_append (sym, symbol_lastP, &symbol_rootP,
2646 &symbol_lastP);
2647 symbol_table_insert (sym);
2649 goto out;
2651 case 'C': /* RVC */
2652 switch (*++oparg)
2654 case 's': /* RS1 x8-x15. */
2655 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2656 || !(regno >= 8 && regno <= 15))
2657 break;
2658 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2659 continue;
2660 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2661 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2662 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2663 break;
2664 continue;
2665 case 't': /* RS2 x8-x15. */
2666 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2667 || !(regno >= 8 && regno <= 15))
2668 break;
2669 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2670 continue;
2671 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2672 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2673 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2674 break;
2675 continue;
2676 case 'U': /* RS1, constrained to equal RD. */
2677 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2678 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2679 break;
2680 continue;
2681 case 'V': /* RS2 */
2682 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2683 break;
2684 INSERT_OPERAND (CRS2, *ip, regno);
2685 continue;
2686 case 'c': /* RS1, constrained to equal sp. */
2687 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2688 || regno != X_SP)
2689 break;
2690 continue;
2691 case 'z': /* RS2, constrained to equal x0. */
2692 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2693 || regno != 0)
2694 break;
2695 continue;
2696 case '>': /* Shift amount, 0 - (XLEN-1). */
2697 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2698 || imm_expr->X_op != O_constant
2699 || (unsigned long) imm_expr->X_add_number >= xlen)
2700 break;
2701 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2702 rvc_imm_done:
2703 asarg = expr_parse_end;
2704 imm_expr->X_op = O_absent;
2705 continue;
2706 case '5':
2707 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2708 || imm_expr->X_op != O_constant
2709 || imm_expr->X_add_number < 0
2710 || imm_expr->X_add_number >= 32
2711 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
2712 break;
2713 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2714 goto rvc_imm_done;
2715 case '6':
2716 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2717 || imm_expr->X_op != O_constant
2718 || imm_expr->X_add_number < 0
2719 || imm_expr->X_add_number >= 64
2720 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
2721 break;
2722 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2723 goto rvc_imm_done;
2724 case '8':
2725 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2726 || imm_expr->X_op != O_constant
2727 || imm_expr->X_add_number < 0
2728 || imm_expr->X_add_number >= 256
2729 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
2730 break;
2731 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2732 goto rvc_imm_done;
2733 case 'j':
2734 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2735 || imm_expr->X_op != O_constant
2736 || imm_expr->X_add_number == 0
2737 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2738 break;
2739 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2740 goto rvc_imm_done;
2741 case 'k':
2742 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2743 continue;
2744 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2745 || imm_expr->X_op != O_constant
2746 || !VALID_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
2747 break;
2748 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2749 goto rvc_imm_done;
2750 case 'l':
2751 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2752 continue;
2753 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2754 || imm_expr->X_op != O_constant
2755 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
2756 break;
2757 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2758 goto rvc_imm_done;
2759 case 'm':
2760 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2761 continue;
2762 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2763 || imm_expr->X_op != O_constant
2764 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
2765 break;
2766 ip->insn_opcode |=
2767 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
2768 goto rvc_imm_done;
2769 case 'n':
2770 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2771 continue;
2772 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2773 || imm_expr->X_op != O_constant
2774 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
2775 break;
2776 ip->insn_opcode |=
2777 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
2778 goto rvc_imm_done;
2779 case 'o':
2780 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2781 || imm_expr->X_op != O_constant
2782 /* C.addiw, c.li, and c.andi allow zero immediate.
2783 C.addi allows zero immediate as hint. Otherwise this
2784 is same as 'j'. */
2785 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2786 break;
2787 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2788 goto rvc_imm_done;
2789 case 'K':
2790 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2791 || imm_expr->X_op != O_constant
2792 || imm_expr->X_add_number == 0
2793 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
2794 break;
2795 ip->insn_opcode |=
2796 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
2797 goto rvc_imm_done;
2798 case 'L':
2799 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2800 || imm_expr->X_op != O_constant
2801 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
2802 break;
2803 ip->insn_opcode |=
2804 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
2805 goto rvc_imm_done;
2806 case 'M':
2807 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2808 continue;
2809 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2810 || imm_expr->X_op != O_constant
2811 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
2812 break;
2813 ip->insn_opcode |=
2814 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
2815 goto rvc_imm_done;
2816 case 'N':
2817 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2818 continue;
2819 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2820 || imm_expr->X_op != O_constant
2821 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
2822 break;
2823 ip->insn_opcode |=
2824 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
2825 goto rvc_imm_done;
2826 case 'u':
2827 p = percent_op_utype;
2828 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
2829 break;
2830 rvc_lui:
2831 if (imm_expr->X_op != O_constant
2832 || imm_expr->X_add_number <= 0
2833 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
2834 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
2835 && (imm_expr->X_add_number <
2836 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
2837 break;
2838 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2839 goto rvc_imm_done;
2840 case 'v':
2841 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2842 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
2843 || ((int32_t)imm_expr->X_add_number
2844 != imm_expr->X_add_number))
2845 break;
2846 imm_expr->X_add_number =
2847 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2848 goto rvc_lui;
2849 case 'p':
2850 goto branch;
2851 case 'a':
2852 goto jump;
2853 case 'S': /* Floating-point RS1 x8-x15. */
2854 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2855 || !(regno >= 8 && regno <= 15))
2856 break;
2857 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2858 continue;
2859 case 'D': /* Floating-point RS2 x8-x15. */
2860 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2861 || !(regno >= 8 && regno <= 15))
2862 break;
2863 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2864 continue;
2865 case 'T': /* Floating-point RS2. */
2866 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
2867 break;
2868 INSERT_OPERAND (CRS2, *ip, regno);
2869 continue;
2870 case 'F':
2871 switch (*++oparg)
2873 case '6':
2874 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2875 || imm_expr->X_op != O_constant
2876 || imm_expr->X_add_number < 0
2877 || imm_expr->X_add_number >= 64)
2879 as_bad (_("bad value for compressed funct6 "
2880 "field, value must be 0...63"));
2881 break;
2883 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2884 imm_expr->X_op = O_absent;
2885 asarg = expr_parse_end;
2886 continue;
2888 case '4':
2889 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2890 || imm_expr->X_op != O_constant
2891 || imm_expr->X_add_number < 0
2892 || imm_expr->X_add_number >= 16)
2894 as_bad (_("bad value for compressed funct4 "
2895 "field, value must be 0...15"));
2896 break;
2898 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2899 imm_expr->X_op = O_absent;
2900 asarg = expr_parse_end;
2901 continue;
2903 case '3':
2904 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2905 || imm_expr->X_op != O_constant
2906 || imm_expr->X_add_number < 0
2907 || imm_expr->X_add_number >= 8)
2909 as_bad (_("bad value for compressed funct3 "
2910 "field, value must be 0...7"));
2911 break;
2913 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2914 imm_expr->X_op = O_absent;
2915 asarg = expr_parse_end;
2916 continue;
2918 case '2':
2919 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2920 || imm_expr->X_op != O_constant
2921 || imm_expr->X_add_number < 0
2922 || imm_expr->X_add_number >= 4)
2924 as_bad (_("bad value for compressed funct2 "
2925 "field, value must be 0...3"));
2926 break;
2928 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2929 imm_expr->X_op = O_absent;
2930 asarg = expr_parse_end;
2931 continue;
2933 default:
2934 goto unknown_riscv_ip_operand;
2936 break;
2938 default:
2939 goto unknown_riscv_ip_operand;
2941 break; /* end RVC */
2943 case 'V': /* RVV */
2944 switch (*++oparg)
2946 case 'd': /* VD */
2947 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2948 break;
2949 INSERT_OPERAND (VD, *ip, regno);
2950 continue;
2952 case 'e': /* AMO VD */
2953 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
2954 INSERT_OPERAND (VWD, *ip, 0);
2955 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
2957 INSERT_OPERAND (VWD, *ip, 1);
2958 INSERT_OPERAND (VD, *ip, regno);
2960 else
2961 break;
2962 continue;
2964 case 'f': /* AMO VS3 */
2965 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2966 break;
2967 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
2968 INSERT_OPERAND (VD, *ip, regno);
2969 else
2971 /* VS3 must match VD. */
2972 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
2973 break;
2975 continue;
2977 case 's': /* VS1 */
2978 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2979 break;
2980 INSERT_OPERAND (VS1, *ip, regno);
2981 continue;
2983 case 't': /* VS2 */
2984 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2985 break;
2986 INSERT_OPERAND (VS2, *ip, regno);
2987 continue;
2989 case 'u': /* VS1 == VS2 */
2990 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2991 break;
2992 INSERT_OPERAND (VS1, *ip, regno);
2993 INSERT_OPERAND (VS2, *ip, regno);
2994 continue;
2996 case 'v': /* VD == VS1 == VS2 */
2997 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2998 break;
2999 INSERT_OPERAND (VD, *ip, regno);
3000 INSERT_OPERAND (VS1, *ip, regno);
3001 INSERT_OPERAND (VS2, *ip, regno);
3002 continue;
3004 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
3005 and is used to choose vs1/rs1/frs1/imm or vs2 for
3006 v[f]merge. It use the same encoding as the vector mask
3007 register. */
3008 case '0':
3009 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
3010 continue;
3011 break;
3013 case 'b': /* vtypei for vsetivli */
3014 my_getVsetvliExpression (imm_expr, asarg);
3015 check_absolute_expr (ip, imm_expr, FALSE);
3016 if (!VALID_RVV_VB_IMM (imm_expr->X_add_number))
3017 as_bad (_("bad value for vsetivli immediate field, "
3018 "value must be 0..1023"));
3019 ip->insn_opcode
3020 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
3021 imm_expr->X_op = O_absent;
3022 asarg = expr_parse_end;
3023 continue;
3025 case 'c': /* vtypei for vsetvli */
3026 my_getVsetvliExpression (imm_expr, asarg);
3027 check_absolute_expr (ip, imm_expr, FALSE);
3028 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
3029 as_bad (_("bad value for vsetvli immediate field, "
3030 "value must be 0..2047"));
3031 ip->insn_opcode
3032 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3033 imm_expr->X_op = O_absent;
3034 asarg = expr_parse_end;
3035 continue;
3037 case 'i': /* vector arith signed immediate */
3038 my_getExpression (imm_expr, asarg);
3039 check_absolute_expr (ip, imm_expr, FALSE);
3040 if (imm_expr->X_add_number > 15
3041 || imm_expr->X_add_number < -16)
3042 as_bad (_("bad value for vector immediate field, "
3043 "value must be -16...15"));
3044 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3045 imm_expr->X_op = O_absent;
3046 asarg = expr_parse_end;
3047 continue;
3049 case 'j': /* vector arith unsigned immediate */
3050 my_getExpression (imm_expr, asarg);
3051 check_absolute_expr (ip, imm_expr, FALSE);
3052 if (imm_expr->X_add_number < 0
3053 || imm_expr->X_add_number >= 32)
3054 as_bad (_("bad value for vector immediate field, "
3055 "value must be 0...31"));
3056 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3057 imm_expr->X_op = O_absent;
3058 asarg = expr_parse_end;
3059 continue;
3061 case 'k': /* vector arith signed immediate, minus 1 */
3062 my_getExpression (imm_expr, asarg);
3063 check_absolute_expr (ip, imm_expr, FALSE);
3064 if (imm_expr->X_add_number > 16
3065 || imm_expr->X_add_number < -15)
3066 as_bad (_("bad value for vector immediate field, "
3067 "value must be -15...16"));
3068 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number - 1);
3069 imm_expr->X_op = O_absent;
3070 asarg = expr_parse_end;
3071 continue;
3073 case 'l': /* 6-bit vector arith unsigned immediate */
3074 my_getExpression (imm_expr, asarg);
3075 check_absolute_expr (ip, imm_expr, FALSE);
3076 if (imm_expr->X_add_number < 0
3077 || imm_expr->X_add_number >= 64)
3078 as_bad (_("bad value for vector immediate field, "
3079 "value must be 0...63"));
3080 ip->insn_opcode |= ENCODE_RVV_VI_UIMM6 (imm_expr->X_add_number);
3081 imm_expr->X_op = O_absent;
3082 asarg = expr_parse_end;
3083 continue;
3085 case 'm': /* optional vector mask */
3086 if (*asarg == '\0')
3088 INSERT_OPERAND (VMASK, *ip, 1);
3089 continue;
3091 else if (*asarg == ',' && asarg++
3092 && reg_lookup (&asarg, RCLASS_VECM, &regno)
3093 && regno == 0)
3095 INSERT_OPERAND (VMASK, *ip, 0);
3096 continue;
3098 break;
3100 case 'M': /* required vector mask */
3101 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
3103 INSERT_OPERAND (VMASK, *ip, 0);
3104 continue;
3106 break;
3108 case 'T': /* vector macro temporary register */
3109 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
3110 break;
3111 /* Store it in the FUNCT6 field as we don't have anyplace
3112 else to store it. */
3113 INSERT_OPERAND (VFUNCT6, *ip, regno);
3114 continue;
3116 default:
3117 goto unknown_riscv_ip_operand;
3119 break; /* end RVV */
3121 case ',':
3122 if (*asarg++ == *oparg)
3123 continue;
3124 asarg--;
3125 break;
3127 case '(':
3128 case ')':
3129 case '[':
3130 case ']':
3131 if (*asarg++ == *oparg)
3132 continue;
3133 break;
3135 case '<': /* Shift amount, 0 - 31. */
3136 my_getExpression (imm_expr, asarg);
3137 check_absolute_expr (ip, imm_expr, false);
3138 if ((unsigned long) imm_expr->X_add_number > 31)
3139 as_bad (_("improper shift amount (%"PRIu64")"),
3140 imm_expr->X_add_number);
3141 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
3142 imm_expr->X_op = O_absent;
3143 asarg = expr_parse_end;
3144 continue;
3146 case '>': /* Shift amount, 0 - (XLEN-1). */
3147 my_getExpression (imm_expr, asarg);
3148 check_absolute_expr (ip, imm_expr, false);
3149 if ((unsigned long) imm_expr->X_add_number >= xlen)
3150 as_bad (_("improper shift amount (%"PRIu64")"),
3151 imm_expr->X_add_number);
3152 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
3153 imm_expr->X_op = O_absent;
3154 asarg = expr_parse_end;
3155 continue;
3157 case 'Z': /* CSRRxI immediate. */
3158 my_getExpression (imm_expr, asarg);
3159 check_absolute_expr (ip, imm_expr, false);
3160 if ((unsigned long) imm_expr->X_add_number > 31)
3161 as_bad (_("improper CSRxI immediate (%"PRIu64")"),
3162 imm_expr->X_add_number);
3163 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
3164 imm_expr->X_op = O_absent;
3165 asarg = expr_parse_end;
3166 continue;
3168 case 'E': /* Control register. */
3169 insn_with_csr = true;
3170 explicit_priv_attr = true;
3171 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
3172 INSERT_OPERAND (CSR, *ip, regno);
3173 else
3175 my_getExpression (imm_expr, asarg);
3176 check_absolute_expr (ip, imm_expr, true);
3177 if ((unsigned long) imm_expr->X_add_number > 0xfff)
3178 as_bad (_("improper CSR address (%"PRIu64")"),
3179 imm_expr->X_add_number);
3180 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
3181 imm_expr->X_op = O_absent;
3182 asarg = expr_parse_end;
3184 continue;
3186 case 'm': /* Rounding mode. */
3187 if (arg_lookup (&asarg, riscv_rm,
3188 ARRAY_SIZE (riscv_rm), &regno))
3190 INSERT_OPERAND (RM, *ip, regno);
3191 continue;
3193 break;
3195 case 'P':
3196 case 'Q': /* Fence predecessor/successor. */
3197 if (arg_lookup (&asarg, riscv_pred_succ,
3198 ARRAY_SIZE (riscv_pred_succ), &regno))
3200 if (*oparg == 'P')
3201 INSERT_OPERAND (PRED, *ip, regno);
3202 else
3203 INSERT_OPERAND (SUCC, *ip, regno);
3204 continue;
3206 break;
3208 case 'd': /* Destination register. */
3209 case 's': /* Source register. */
3210 case 't': /* Target register. */
3211 case 'r': /* RS3 */
3212 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
3214 char c = *oparg;
3215 if (*asarg == ' ')
3216 ++asarg;
3218 /* Now that we have assembled one operand, we use the args
3219 string to figure out where it goes in the instruction. */
3220 switch (c)
3222 case 's':
3223 INSERT_OPERAND (RS1, *ip, regno);
3224 break;
3225 case 'd':
3226 INSERT_OPERAND (RD, *ip, regno);
3227 break;
3228 case 't':
3229 INSERT_OPERAND (RS2, *ip, regno);
3230 break;
3231 case 'r':
3232 INSERT_OPERAND (RS3, *ip, regno);
3233 break;
3235 continue;
3237 break;
3239 case 'D': /* Floating point RD. */
3240 case 'S': /* Floating point RS1. */
3241 case 'T': /* Floating point RS2. */
3242 case 'U': /* Floating point RS1 and RS2. */
3243 case 'R': /* Floating point RS3. */
3244 if (reg_lookup (&asarg,
3245 (riscv_subset_supports (&riscv_rps_as, "zfinx")
3246 ? RCLASS_GPR : RCLASS_FPR), &regno))
3248 char c = *oparg;
3249 if (*asarg == ' ')
3250 ++asarg;
3251 switch (c)
3253 case 'D':
3254 INSERT_OPERAND (RD, *ip, regno);
3255 break;
3256 case 'S':
3257 INSERT_OPERAND (RS1, *ip, regno);
3258 break;
3259 case 'U':
3260 INSERT_OPERAND (RS1, *ip, regno);
3261 /* Fall through. */
3262 case 'T':
3263 INSERT_OPERAND (RS2, *ip, regno);
3264 break;
3265 case 'R':
3266 INSERT_OPERAND (RS3, *ip, regno);
3267 break;
3269 continue;
3271 break;
3273 case 'I':
3274 my_getExpression (imm_expr, asarg);
3275 if (imm_expr->X_op != O_big
3276 && imm_expr->X_op != O_constant)
3277 break;
3278 normalize_constant_expr (imm_expr);
3279 asarg = expr_parse_end;
3280 continue;
3282 case 'A':
3283 my_getExpression (imm_expr, asarg);
3284 normalize_constant_expr (imm_expr);
3285 /* The 'A' format specifier must be a symbol. */
3286 if (imm_expr->X_op != O_symbol)
3287 break;
3288 *imm_reloc = BFD_RELOC_32;
3289 asarg = expr_parse_end;
3290 continue;
3292 case 'B':
3293 my_getExpression (imm_expr, asarg);
3294 normalize_constant_expr (imm_expr);
3295 /* The 'B' format specifier must be a symbol or a constant. */
3296 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
3297 break;
3298 if (imm_expr->X_op == O_symbol)
3299 *imm_reloc = BFD_RELOC_32;
3300 asarg = expr_parse_end;
3301 continue;
3303 case 'j': /* Sign-extended immediate. */
3304 p = percent_op_itype;
3305 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3306 goto alu_op;
3307 case 'q': /* Store displacement. */
3308 p = percent_op_stype;
3309 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3310 goto load_store;
3311 case 'o': /* Load displacement. */
3312 p = percent_op_itype;
3313 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3314 goto load_store;
3315 case '1':
3316 /* This is used for TLS, where the fourth operand is
3317 %tprel_add, to get a relocation applied to an add
3318 instruction, for relaxation to use. */
3319 p = percent_op_rtype;
3320 goto alu_op;
3321 case '0': /* AMO displacement, which must be zero. */
3322 load_store:
3323 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3324 continue;
3325 alu_op:
3326 /* If this value won't fit into a 16 bit offset, then go
3327 find a macro that will generate the 32 bit offset
3328 code pattern. */
3329 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3331 normalize_constant_expr (imm_expr);
3332 if (imm_expr->X_op != O_constant
3333 || (*oparg == '0' && imm_expr->X_add_number != 0)
3334 || (*oparg == '1')
3335 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3336 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3337 break;
3339 asarg = expr_parse_end;
3340 continue;
3342 case 'p': /* PC-relative offset. */
3343 branch:
3344 *imm_reloc = BFD_RELOC_12_PCREL;
3345 my_getExpression (imm_expr, asarg);
3346 asarg = expr_parse_end;
3347 continue;
3349 case 'u': /* Upper 20 bits. */
3350 p = percent_op_utype;
3351 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3353 if (imm_expr->X_op != O_constant)
3354 break;
3356 if (imm_expr->X_add_number < 0
3357 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
3358 as_bad (_("lui expression not in range 0..1048575"));
3360 *imm_reloc = BFD_RELOC_RISCV_HI20;
3361 imm_expr->X_add_number <<= RISCV_IMM_BITS;
3363 asarg = expr_parse_end;
3364 continue;
3366 case 'a': /* 20-bit PC-relative offset. */
3367 jump:
3368 my_getExpression (imm_expr, asarg);
3369 asarg = expr_parse_end;
3370 *imm_reloc = BFD_RELOC_RISCV_JMP;
3371 continue;
3373 case 'c':
3374 my_getExpression (imm_expr, asarg);
3375 asarg = expr_parse_end;
3376 if (strcmp (asarg, "@plt") == 0)
3377 asarg += 4;
3378 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3379 continue;
3381 case 'O':
3382 switch (*++oparg)
3384 case '4':
3385 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3386 || imm_expr->X_op != O_constant
3387 || imm_expr->X_add_number < 0
3388 || imm_expr->X_add_number >= 128
3389 || (imm_expr->X_add_number & 0x3) != 3)
3391 as_bad (_("bad value for opcode field, "
3392 "value must be 0...127 and "
3393 "lower 2 bits must be 0x3"));
3394 break;
3396 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3397 imm_expr->X_op = O_absent;
3398 asarg = expr_parse_end;
3399 continue;
3401 case '2':
3402 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3403 || imm_expr->X_op != O_constant
3404 || imm_expr->X_add_number < 0
3405 || imm_expr->X_add_number >= 3)
3407 as_bad (_("bad value for opcode field, "
3408 "value must be 0...2"));
3409 break;
3411 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3412 imm_expr->X_op = O_absent;
3413 asarg = expr_parse_end;
3414 continue;
3416 default:
3417 goto unknown_riscv_ip_operand;
3419 break;
3421 case 'F':
3422 switch (*++oparg)
3424 case '7':
3425 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3426 || imm_expr->X_op != O_constant
3427 || imm_expr->X_add_number < 0
3428 || imm_expr->X_add_number >= 128)
3430 as_bad (_("bad value for funct7 field, "
3431 "value must be 0...127"));
3432 break;
3434 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3435 imm_expr->X_op = O_absent;
3436 asarg = expr_parse_end;
3437 continue;
3439 case '3':
3440 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3441 || imm_expr->X_op != O_constant
3442 || imm_expr->X_add_number < 0
3443 || imm_expr->X_add_number >= 8)
3445 as_bad (_("bad value for funct3 field, "
3446 "value must be 0...7"));
3447 break;
3449 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3450 imm_expr->X_op = O_absent;
3451 asarg = expr_parse_end;
3452 continue;
3454 case '2':
3455 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3456 || imm_expr->X_op != O_constant
3457 || imm_expr->X_add_number < 0
3458 || imm_expr->X_add_number >= 4)
3460 as_bad (_("bad value for funct2 field, "
3461 "value must be 0...3"));
3462 break;
3464 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3465 imm_expr->X_op = O_absent;
3466 asarg = expr_parse_end;
3467 continue;
3469 default:
3470 goto unknown_riscv_ip_operand;
3472 break;
3474 case 'y': /* bs immediate */
3475 my_getExpression (imm_expr, asarg);
3476 check_absolute_expr (ip, imm_expr, FALSE);
3477 if ((unsigned long)imm_expr->X_add_number > 3)
3478 as_bad(_("Improper bs immediate (%lu)"),
3479 (unsigned long)imm_expr->X_add_number);
3480 INSERT_OPERAND(BS, *ip, imm_expr->X_add_number);
3481 imm_expr->X_op = O_absent;
3482 asarg = expr_parse_end;
3483 continue;
3485 case 'Y': /* rnum immediate */
3486 my_getExpression (imm_expr, asarg);
3487 check_absolute_expr (ip, imm_expr, FALSE);
3488 if ((unsigned long)imm_expr->X_add_number > 10)
3489 as_bad(_("Improper rnum immediate (%lu)"),
3490 (unsigned long)imm_expr->X_add_number);
3491 INSERT_OPERAND(RNUM, *ip, imm_expr->X_add_number);
3492 imm_expr->X_op = O_absent;
3493 asarg = expr_parse_end;
3494 continue;
3496 case 'z':
3497 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3498 || imm_expr->X_op != O_constant
3499 || imm_expr->X_add_number != 0)
3500 break;
3501 asarg = expr_parse_end;
3502 imm_expr->X_op = O_absent;
3503 continue;
3505 case 'W': /* Various operands. */
3506 switch (*++oparg)
3508 case 'i':
3509 switch (*++oparg)
3511 case 'f':
3512 /* Prefetch offset for 'Zicbop' extension.
3513 pseudo S-type but lower 5-bits zero. */
3514 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3515 continue;
3516 my_getExpression (imm_expr, asarg);
3517 check_absolute_expr (ip, imm_expr, false);
3518 if (((unsigned) (imm_expr->X_add_number) & 0x1fU)
3519 || imm_expr->X_add_number >= RISCV_IMM_REACH / 2
3520 || imm_expr->X_add_number < -RISCV_IMM_REACH / 2)
3521 as_bad (_ ("improper prefetch offset (%ld)"),
3522 (long) imm_expr->X_add_number);
3523 ip->insn_opcode |= ENCODE_STYPE_IMM (
3524 (unsigned) (imm_expr->X_add_number) & ~0x1fU);
3525 imm_expr->X_op = O_absent;
3526 asarg = expr_parse_end;
3527 continue;
3528 default:
3529 goto unknown_riscv_ip_operand;
3531 break;
3532 case 'f':
3533 switch (*++oparg)
3535 case 'v':
3536 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3537 if (!arg_lookup (&asarg, riscv_fli_symval,
3538 ARRAY_SIZE (riscv_fli_symval), &regno))
3540 /* 0.0 is not a valid entry in riscv_fli_numval. */
3541 errno = 0;
3542 float f = strtof (asarg, &asarg);
3543 if (errno != 0 || f == 0.0
3544 || !flt_lookup (f, riscv_fli_numval,
3545 ARRAY_SIZE(riscv_fli_numval),
3546 &regno))
3548 as_bad (_("bad fli constant operand, "
3549 "supported constants must be in "
3550 "decimal or hexadecimal floating-point "
3551 "literal form"));
3552 break;
3555 INSERT_OPERAND (RS1, *ip, regno);
3556 continue;
3557 default:
3558 goto unknown_riscv_ip_operand;
3560 break;
3561 default:
3562 goto unknown_riscv_ip_operand;
3564 break;
3566 case 'X': /* Integer immediate. */
3568 size_t n;
3569 size_t s;
3570 bool sign;
3572 switch (*++oparg)
3574 case 'l': /* Literal. */
3575 n = strcspn (++oparg, ",");
3576 if (strncmp (oparg, asarg, n))
3577 as_bad (_("unexpected literal (%s)"), asarg);
3578 oparg += n - 1;
3579 asarg += n;
3580 continue;
3581 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
3582 sign = true;
3583 goto parse_imm;
3584 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
3585 sign = false;
3586 goto parse_imm;
3587 parse_imm:
3588 n = strtol (oparg + 1, (char **)&oparg, 10);
3589 if (*oparg != '@')
3590 goto unknown_riscv_ip_operand;
3591 s = strtol (oparg + 1, (char **)&oparg, 10);
3592 oparg--;
3594 my_getExpression (imm_expr, asarg);
3595 check_absolute_expr (ip, imm_expr, false);
3596 if (!sign)
3598 if (!VALIDATE_U_IMM (imm_expr->X_add_number, n))
3599 as_bad (_("improper immediate value (%"PRIu64")"),
3600 imm_expr->X_add_number);
3602 else
3604 if (!VALIDATE_S_IMM (imm_expr->X_add_number, n))
3605 as_bad (_("improper immediate value (%"PRIi64")"),
3606 imm_expr->X_add_number);
3608 INSERT_IMM (n, s, *ip, imm_expr->X_add_number);
3609 imm_expr->X_op = O_absent;
3610 asarg = expr_parse_end;
3611 continue;
3612 default:
3613 goto unknown_riscv_ip_operand;
3616 break;
3618 default:
3619 unknown_riscv_ip_operand:
3620 as_fatal (_("internal: unknown argument type `%s'"),
3621 opargStart);
3623 break;
3625 asarg = asargStart;
3626 insn_with_csr = false;
3629 out:
3630 /* Restore the character we might have clobbered above. */
3631 if (save_c)
3632 *(asargStart - 1) = save_c;
3634 probing_insn_operands = false;
3636 return error;
3639 /* Similar to riscv_ip, but assembles an instruction according to the
3640 hardcode values of .insn directive. */
3642 static const char *
3643 riscv_ip_hardcode (char *str,
3644 struct riscv_cl_insn *ip,
3645 expressionS *imm_expr,
3646 const char *error)
3648 struct riscv_opcode *insn;
3649 insn_t values[2] = {0, 0};
3650 unsigned int num = 0;
3652 input_line_pointer = str;
3655 expression (imm_expr);
3656 switch (imm_expr->X_op)
3658 case O_constant:
3659 values[num++] = (insn_t) imm_expr->X_add_number;
3660 break;
3661 case O_big:
3662 /* Extract lower 32-bits of a big number.
3663 Assume that generic_bignum_to_int32 work on such number. */
3664 values[num++] = (insn_t) generic_bignum_to_int32 ();
3665 break;
3666 default:
3667 /* The first value isn't constant, so it should be
3668 .insn <type> <operands>. We have been parsed it
3669 in the riscv_ip. */
3670 if (num == 0)
3671 return error;
3672 return _("values must be constant");
3675 while (*input_line_pointer++ == ',' && num < 2 && imm_expr->X_op != O_big);
3677 input_line_pointer--;
3678 if (*input_line_pointer != '\0')
3679 return _("unrecognized values");
3681 insn = XNEW (struct riscv_opcode);
3682 insn->match = values[num - 1];
3683 create_insn (ip, insn);
3684 unsigned int bytes = riscv_insn_length (insn->match);
3686 if (num == 2 && values[0] != bytes)
3687 return _("value conflicts with instruction length");
3689 if (imm_expr->X_op == O_big)
3691 unsigned int llen = 0;
3692 for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
3693 lval != 0; llen++)
3694 lval >>= BITS_PER_CHAR;
3695 unsigned int repr_bytes
3696 = (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
3697 if (bytes < repr_bytes)
3698 return _("value conflicts with instruction length");
3699 for (num = 0; num < imm_expr->X_add_number - 1; ++num)
3700 number_to_chars_littleendian (
3701 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3702 generic_bignum[num],
3703 CHARS_PER_LITTLENUM);
3704 if (llen != 0)
3705 number_to_chars_littleendian (
3706 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3707 generic_bignum[num],
3708 llen);
3709 memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
3710 return NULL;
3713 if (bytes < sizeof(values[0]) && values[num - 1] >> (8 * bytes) != 0)
3714 return _("value conflicts with instruction length");
3716 return NULL;
3719 void
3720 md_assemble (char *str)
3722 struct riscv_cl_insn insn;
3723 expressionS imm_expr;
3724 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
3726 /* The architecture and privileged elf attributes should be set
3727 before assembling. */
3728 if (!start_assemble)
3730 start_assemble = true;
3732 riscv_set_abi_by_arch ();
3733 if (!riscv_set_default_priv_spec (NULL))
3734 return;
3737 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
3739 const struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
3740 &imm_reloc, op_hash);
3742 if (error.msg)
3744 if (error.missing_ext)
3745 as_bad ("%s `%s', extension `%s' required", error.msg,
3746 error.statement, error.missing_ext);
3747 else
3748 as_bad ("%s `%s'", error.msg, error.statement);
3749 return;
3752 if (insn.insn_mo->pinfo == INSN_MACRO)
3753 macro (&insn, &imm_expr, &imm_reloc);
3754 else
3755 append_insn (&insn, &imm_expr, imm_reloc);
3758 const char *
3759 md_atof (int type, char *litP, int *sizeP)
3761 return ieee_md_atof (type, litP, sizeP, target_big_endian);
3764 void
3765 md_number_to_chars (char *buf, valueT val, int n)
3767 if (target_big_endian)
3768 number_to_chars_bigendian (buf, val, n);
3769 else
3770 number_to_chars_littleendian (buf, val, n);
3773 const char *md_shortopts = "O::g::G:";
3775 enum options
3777 OPTION_MARCH = OPTION_MD_BASE,
3778 OPTION_PIC,
3779 OPTION_NO_PIC,
3780 OPTION_MABI,
3781 OPTION_RELAX,
3782 OPTION_NO_RELAX,
3783 OPTION_ARCH_ATTR,
3784 OPTION_NO_ARCH_ATTR,
3785 OPTION_CSR_CHECK,
3786 OPTION_NO_CSR_CHECK,
3787 OPTION_MISA_SPEC,
3788 OPTION_MPRIV_SPEC,
3789 OPTION_BIG_ENDIAN,
3790 OPTION_LITTLE_ENDIAN,
3791 OPTION_END_OF_ENUM
3794 struct option md_longopts[] =
3796 {"march", required_argument, NULL, OPTION_MARCH},
3797 {"fPIC", no_argument, NULL, OPTION_PIC},
3798 {"fpic", no_argument, NULL, OPTION_PIC},
3799 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
3800 {"mabi", required_argument, NULL, OPTION_MABI},
3801 {"mrelax", no_argument, NULL, OPTION_RELAX},
3802 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
3803 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
3804 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
3805 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
3806 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
3807 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
3808 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
3809 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
3810 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
3812 {NULL, no_argument, NULL, 0}
3814 size_t md_longopts_size = sizeof (md_longopts);
3817 md_parse_option (int c, const char *arg)
3819 switch (c)
3821 case OPTION_MARCH:
3822 default_arch_with_ext = arg;
3823 break;
3825 case OPTION_NO_PIC:
3826 riscv_opts.pic = false;
3827 break;
3829 case OPTION_PIC:
3830 riscv_opts.pic = true;
3831 break;
3833 case OPTION_MABI:
3834 if (strcmp (arg, "ilp32") == 0)
3835 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
3836 else if (strcmp (arg, "ilp32e") == 0)
3837 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
3838 else if (strcmp (arg, "ilp32f") == 0)
3839 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
3840 else if (strcmp (arg, "ilp32d") == 0)
3841 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
3842 else if (strcmp (arg, "ilp32q") == 0)
3843 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
3844 else if (strcmp (arg, "lp64") == 0)
3845 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
3846 else if (strcmp (arg, "lp64f") == 0)
3847 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
3848 else if (strcmp (arg, "lp64d") == 0)
3849 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
3850 else if (strcmp (arg, "lp64q") == 0)
3851 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
3852 else
3853 return 0;
3854 explicit_mabi = true;
3855 break;
3857 case OPTION_RELAX:
3858 riscv_opts.relax = true;
3859 break;
3861 case OPTION_NO_RELAX:
3862 riscv_opts.relax = false;
3863 break;
3865 case OPTION_ARCH_ATTR:
3866 riscv_opts.arch_attr = true;
3867 break;
3869 case OPTION_NO_ARCH_ATTR:
3870 riscv_opts.arch_attr = false;
3871 break;
3873 case OPTION_CSR_CHECK:
3874 riscv_opts.csr_check = true;
3875 break;
3877 case OPTION_NO_CSR_CHECK:
3878 riscv_opts.csr_check = false;
3879 break;
3881 case OPTION_MISA_SPEC:
3882 return riscv_set_default_isa_spec (arg);
3884 case OPTION_MPRIV_SPEC:
3885 return riscv_set_default_priv_spec (arg);
3887 case OPTION_BIG_ENDIAN:
3888 target_big_endian = 1;
3889 break;
3891 case OPTION_LITTLE_ENDIAN:
3892 target_big_endian = 0;
3893 break;
3895 default:
3896 return 0;
3899 return 1;
3902 void
3903 riscv_after_parse_args (void)
3905 /* The --with-arch is optional for now, so we still need to set the xlen
3906 according to the default_arch, which is set by the --target. */
3907 if (xlen == 0)
3909 if (strcmp (default_arch, "riscv32") == 0)
3910 xlen = 32;
3911 else if (strcmp (default_arch, "riscv64") == 0)
3912 xlen = 64;
3913 else
3914 as_bad ("unknown default architecture `%s'", default_arch);
3917 /* Set default specs. */
3918 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
3919 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
3920 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
3921 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
3923 riscv_set_arch (default_arch_with_ext);
3925 /* If the CIE to be produced has not been overridden on the command line,
3926 then produce version 3 by default. This allows us to use the full
3927 range of registers in a .cfi_return_column directive. */
3928 if (flag_dwarf_cie_version == -1)
3929 flag_dwarf_cie_version = 3;
3932 bool riscv_parse_name (const char *name, struct expressionS *ep,
3933 enum expr_mode mode)
3935 unsigned int regno;
3936 symbolS *sym;
3938 if (!probing_insn_operands)
3939 return false;
3941 gas_assert (mode == expr_normal);
3943 regno = reg_lookup_internal (name, RCLASS_GPR);
3944 if (regno == (unsigned int)-1)
3945 return false;
3947 if (symbol_find (name) != NULL)
3948 return false;
3950 /* Create a symbol without adding it to the symbol table yet.
3951 Insertion will happen only once we commit to using the insn
3952 we're probing operands for. */
3953 for (sym = deferred_sym_rootP; sym; sym = symbol_next (sym))
3954 if (strcmp (name, S_GET_NAME (sym)) == 0)
3955 break;
3956 if (!sym)
3958 for (sym = orphan_sym_rootP; sym; sym = symbol_next (sym))
3959 if (strcmp (name, S_GET_NAME (sym)) == 0)
3961 symbol_remove (sym, &orphan_sym_rootP, &orphan_sym_lastP);
3962 break;
3964 if (!sym)
3965 sym = symbol_create (name, undefined_section,
3966 &zero_address_frag, 0);
3968 symbol_append (sym, deferred_sym_lastP, &deferred_sym_rootP,
3969 &deferred_sym_lastP);
3972 ep->X_op = O_symbol;
3973 ep->X_add_symbol = sym;
3974 ep->X_add_number = 0;
3976 return true;
3979 long
3980 md_pcrel_from (fixS *fixP)
3982 return fixP->fx_where + fixP->fx_frag->fr_address;
3985 /* Apply a fixup to the object file. */
3987 void
3988 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3990 unsigned int subtype;
3991 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
3992 bool relaxable = false;
3993 offsetT loc;
3994 segT sub_segment;
3996 /* Remember value for tc_gen_reloc. */
3997 fixP->fx_addnumber = *valP;
3999 switch (fixP->fx_r_type)
4001 case BFD_RELOC_RISCV_HI20:
4002 case BFD_RELOC_RISCV_LO12_I:
4003 case BFD_RELOC_RISCV_LO12_S:
4004 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
4005 | bfd_getl32 (buf), buf);
4006 if (fixP->fx_addsy == NULL)
4007 fixP->fx_done = true;
4008 relaxable = true;
4009 break;
4011 case BFD_RELOC_RISCV_GOT_HI20:
4012 case BFD_RELOC_RISCV_ADD8:
4013 case BFD_RELOC_RISCV_ADD16:
4014 case BFD_RELOC_RISCV_ADD32:
4015 case BFD_RELOC_RISCV_ADD64:
4016 case BFD_RELOC_RISCV_SUB6:
4017 case BFD_RELOC_RISCV_SUB8:
4018 case BFD_RELOC_RISCV_SUB16:
4019 case BFD_RELOC_RISCV_SUB32:
4020 case BFD_RELOC_RISCV_SUB64:
4021 case BFD_RELOC_RISCV_RELAX:
4022 /* cvt_frag_to_fill () has called output_leb128 (). */
4023 case BFD_RELOC_RISCV_SET_ULEB128:
4024 case BFD_RELOC_RISCV_SUB_ULEB128:
4025 break;
4027 case BFD_RELOC_RISCV_TPREL_HI20:
4028 case BFD_RELOC_RISCV_TPREL_LO12_I:
4029 case BFD_RELOC_RISCV_TPREL_LO12_S:
4030 case BFD_RELOC_RISCV_TPREL_ADD:
4031 relaxable = true;
4032 /* Fall through. */
4034 case BFD_RELOC_RISCV_TLS_GOT_HI20:
4035 case BFD_RELOC_RISCV_TLS_GD_HI20:
4036 case BFD_RELOC_RISCV_TLS_DTPREL32:
4037 case BFD_RELOC_RISCV_TLS_DTPREL64:
4038 if (fixP->fx_addsy != NULL)
4039 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4040 else
4041 as_bad_where (fixP->fx_file, fixP->fx_line,
4042 _("TLS relocation against a constant"));
4043 break;
4045 case BFD_RELOC_32:
4046 /* Use pc-relative relocation for FDE initial location.
4047 The symbol address in .eh_frame may be adjusted in
4048 _bfd_elf_discard_section_eh_frame, and the content of
4049 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4050 Therefore, we cannot insert a relocation whose addend symbol is
4051 in .eh_frame. Othrewise, the value may be adjusted twice. */
4052 if (fixP->fx_addsy && fixP->fx_subsy
4053 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
4054 && strcmp (sub_segment->name, ".eh_frame") == 0
4055 && S_GET_VALUE (fixP->fx_subsy)
4056 == fixP->fx_frag->fr_address + fixP->fx_where)
4058 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
4059 fixP->fx_subsy = NULL;
4060 break;
4062 /* Fall through. */
4063 case BFD_RELOC_64:
4064 case BFD_RELOC_16:
4065 case BFD_RELOC_8:
4066 case BFD_RELOC_RISCV_CFA:
4067 if (fixP->fx_addsy && fixP->fx_subsy)
4069 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4070 fixP->fx_next->fx_addsy = fixP->fx_subsy;
4071 fixP->fx_next->fx_subsy = NULL;
4072 fixP->fx_next->fx_offset = 0;
4073 fixP->fx_subsy = NULL;
4075 switch (fixP->fx_r_type)
4077 case BFD_RELOC_64:
4078 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
4079 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
4080 break;
4082 case BFD_RELOC_32:
4083 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
4084 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4085 break;
4087 case BFD_RELOC_16:
4088 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
4089 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4090 break;
4092 case BFD_RELOC_8:
4093 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
4094 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4095 break;
4097 case BFD_RELOC_RISCV_CFA:
4098 /* Load the byte to get the subtype. */
4099 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
4100 loc = fixP->fx_frag->fr_fix - (subtype & 7);
4101 switch (subtype)
4103 case DW_CFA_advance_loc1:
4104 fixP->fx_where = loc + 1;
4105 fixP->fx_next->fx_where = loc + 1;
4106 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
4107 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4108 break;
4110 case DW_CFA_advance_loc2:
4111 fixP->fx_size = 2;
4112 fixP->fx_next->fx_size = 2;
4113 fixP->fx_where = loc + 1;
4114 fixP->fx_next->fx_where = loc + 1;
4115 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
4116 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4117 break;
4119 case DW_CFA_advance_loc4:
4120 fixP->fx_size = 4;
4121 fixP->fx_next->fx_size = 4;
4122 fixP->fx_where = loc;
4123 fixP->fx_next->fx_where = loc;
4124 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
4125 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4126 break;
4128 default:
4129 if (subtype < 0x80 && (subtype & 0x40))
4131 /* DW_CFA_advance_loc */
4132 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
4133 fixP->fx_next->fx_frag = fixP->fx_frag;
4134 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
4135 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
4137 else
4138 as_fatal (_("internal: bad CFA value #%d"), subtype);
4139 break;
4141 break;
4143 default:
4144 /* This case is unreachable. */
4145 abort ();
4148 /* Fall through. */
4150 case BFD_RELOC_RVA:
4151 /* If we are deleting this reloc entry, we must fill in the
4152 value now. This can happen if we have a .word which is not
4153 resolved when it appears but is later defined. */
4154 if (fixP->fx_addsy == NULL)
4156 gas_assert (fixP->fx_size <= sizeof (valueT));
4157 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
4158 fixP->fx_done = 1;
4160 break;
4162 case BFD_RELOC_RISCV_JMP:
4163 if (fixP->fx_addsy)
4165 /* Fill in a tentative value to improve objdump readability. */
4166 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4167 bfd_vma delta = target - md_pcrel_from (fixP);
4168 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
4170 break;
4172 case BFD_RELOC_12_PCREL:
4173 if (fixP->fx_addsy)
4175 /* Fill in a tentative value to improve objdump readability. */
4176 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4177 bfd_vma delta = target - md_pcrel_from (fixP);
4178 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
4180 break;
4182 case BFD_RELOC_RISCV_RVC_BRANCH:
4183 if (fixP->fx_addsy)
4185 /* Fill in a tentative value to improve objdump readability. */
4186 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4187 bfd_vma delta = target - md_pcrel_from (fixP);
4188 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
4190 break;
4192 case BFD_RELOC_RISCV_RVC_JUMP:
4193 if (fixP->fx_addsy)
4195 /* Fill in a tentative value to improve objdump readability. */
4196 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4197 bfd_vma delta = target - md_pcrel_from (fixP);
4198 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
4200 break;
4202 case BFD_RELOC_RISCV_CALL:
4203 case BFD_RELOC_RISCV_CALL_PLT:
4204 relaxable = true;
4205 break;
4207 case BFD_RELOC_RISCV_PCREL_HI20:
4208 case BFD_RELOC_RISCV_PCREL_LO12_S:
4209 case BFD_RELOC_RISCV_PCREL_LO12_I:
4210 relaxable = riscv_opts.relax;
4211 break;
4213 case BFD_RELOC_RISCV_ALIGN:
4214 break;
4216 default:
4217 /* We ignore generic BFD relocations we don't know about. */
4218 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
4219 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
4222 if (fixP->fx_subsy != NULL)
4223 as_bad_subtract (fixP);
4225 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4226 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
4228 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4229 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
4230 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
4231 fixP->fx_next->fx_size = 0;
4235 /* Because the value of .cfi_remember_state may changed after relaxation,
4236 we insert a fix to relocate it again in link-time. */
4238 void
4239 riscv_pre_output_hook (void)
4241 const frchainS *frch;
4242 segT s;
4244 /* Save the current segment info. */
4245 segT seg = now_seg;
4246 subsegT subseg = now_subseg;
4248 for (s = stdoutput->sections; s; s = s->next)
4249 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
4251 fragS *frag;
4253 for (frag = frch->frch_root; frag; frag = frag->fr_next)
4255 if (frag->fr_type == rs_cfa)
4257 expressionS exp;
4258 expressionS *symval;
4260 symval = symbol_get_value_expression (frag->fr_symbol);
4261 exp.X_op = O_subtract;
4262 exp.X_add_symbol = symval->X_add_symbol;
4263 exp.X_add_number = 0;
4264 exp.X_op_symbol = symval->X_op_symbol;
4266 /* We must set the segment before creating a frag after all
4267 frag chains have been chained together. */
4268 subseg_set (s, frch->frch_subseg);
4270 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
4271 BFD_RELOC_RISCV_CFA);
4276 /* Restore the original segment info. */
4277 subseg_set (seg, subseg);
4280 /* Handle the .option pseudo-op. */
4282 static void
4283 s_riscv_option (int x ATTRIBUTE_UNUSED)
4285 char *name = input_line_pointer, ch;
4287 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4288 ++input_line_pointer;
4289 ch = *input_line_pointer;
4290 *input_line_pointer = '\0';
4292 if (strcmp (name, "rvc") == 0)
4294 riscv_update_subset (&riscv_rps_as, "+c");
4295 riscv_reset_subsets_list_arch_str ();
4296 riscv_set_rvc (true);
4298 else if (strcmp (name, "norvc") == 0)
4300 riscv_update_subset (&riscv_rps_as, "-c");
4301 riscv_reset_subsets_list_arch_str ();
4302 riscv_set_rvc (false);
4304 else if (strcmp (name, "pic") == 0)
4305 riscv_opts.pic = true;
4306 else if (strcmp (name, "nopic") == 0)
4307 riscv_opts.pic = false;
4308 else if (strcmp (name, "relax") == 0)
4309 riscv_opts.relax = true;
4310 else if (strcmp (name, "norelax") == 0)
4311 riscv_opts.relax = false;
4312 else if (strcmp (name, "csr-check") == 0)
4313 riscv_opts.csr_check = true;
4314 else if (strcmp (name, "no-csr-check") == 0)
4315 riscv_opts.csr_check = false;
4316 else if (strncmp (name, "arch,", 5) == 0)
4318 name += 5;
4319 if (ISSPACE (*name) && *name != '\0')
4320 name++;
4321 riscv_update_subset (&riscv_rps_as, name);
4322 riscv_reset_subsets_list_arch_str ();
4324 riscv_set_rvc (false);
4325 if (riscv_subset_supports (&riscv_rps_as, "c"))
4326 riscv_set_rvc (true);
4328 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
4329 riscv_set_tso ();
4331 else if (strcmp (name, "push") == 0)
4333 struct riscv_option_stack *s;
4335 s = XNEW (struct riscv_option_stack);
4336 s->next = riscv_opts_stack;
4337 s->options = riscv_opts;
4338 s->subset_list = riscv_rps_as.subset_list;
4339 riscv_opts_stack = s;
4340 riscv_rps_as.subset_list = riscv_copy_subset_list (s->subset_list);
4342 else if (strcmp (name, "pop") == 0)
4344 struct riscv_option_stack *s;
4346 s = riscv_opts_stack;
4347 if (s == NULL)
4348 as_bad (_(".option pop with no .option push"));
4349 else
4351 riscv_subset_list_t *release_subsets = riscv_rps_as.subset_list;
4352 riscv_opts_stack = s->next;
4353 riscv_opts = s->options;
4354 riscv_rps_as.subset_list = s->subset_list;
4355 riscv_release_subset_list (release_subsets);
4356 free (s);
4359 else
4361 as_warn (_("unrecognized .option directive: %s"), name);
4363 *input_line_pointer = ch;
4364 demand_empty_rest_of_line ();
4367 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4368 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4369 use in DWARF debug information. */
4371 static void
4372 s_dtprel (int bytes)
4374 expressionS ex;
4375 char *p;
4377 expression (&ex);
4379 if (ex.X_op != O_symbol)
4381 as_bad (_("unsupported use of %s"), (bytes == 8
4382 ? ".dtpreldword"
4383 : ".dtprelword"));
4384 ignore_rest_of_line ();
4387 p = frag_more (bytes);
4388 md_number_to_chars (p, 0, bytes);
4389 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
4390 (bytes == 8
4391 ? BFD_RELOC_RISCV_TLS_DTPREL64
4392 : BFD_RELOC_RISCV_TLS_DTPREL32));
4394 demand_empty_rest_of_line ();
4397 /* Handle the .bss pseudo-op. */
4399 static void
4400 s_bss (int ignore ATTRIBUTE_UNUSED)
4402 subseg_set (bss_section, 0);
4403 demand_empty_rest_of_line ();
4406 static void
4407 riscv_make_nops (char *buf, bfd_vma bytes)
4409 bfd_vma i = 0;
4411 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4412 means we are not within a valid instruction sequence. It is thus safe
4413 to use a zero byte, even though that is not a valid instruction. */
4414 if (bytes % 2 == 1)
4415 buf[i++] = 0;
4417 /* Use at most one 2-byte NOP. */
4418 if ((bytes - i) % 4 == 2)
4420 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
4421 i += 2;
4424 /* Fill the remainder with 4-byte NOPs. */
4425 for ( ; i < bytes; i += 4)
4426 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
4429 /* Called from md_do_align. Used to create an alignment frag in a
4430 code section by emitting a worst-case NOP sequence that the linker
4431 will later relax to the correct number of NOPs. We can't compute
4432 the correct alignment now because of other linker relaxations. */
4434 bool
4435 riscv_frag_align_code (int n)
4437 bfd_vma bytes = (bfd_vma) 1 << n;
4438 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
4439 bfd_vma worst_case_bytes = bytes - insn_alignment;
4440 char *nops;
4441 expressionS ex;
4443 /* If we are moving to a smaller alignment than the instruction size, then no
4444 alignment is required. */
4445 if (bytes <= insn_alignment)
4446 return true;
4448 /* When not relaxing, riscv_handle_align handles code alignment. */
4449 if (!riscv_opts.relax)
4450 return false;
4452 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4453 rather than just use frag_more to handle an alignment here? So that we
4454 don't need to call riscv_mapping_state again later, and then only need
4455 to check frag->fr_type to see if it is frag_align_code. */
4456 nops = frag_more (worst_case_bytes);
4458 ex.X_op = O_constant;
4459 ex.X_add_number = worst_case_bytes;
4461 riscv_make_nops (nops, worst_case_bytes);
4463 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
4464 &ex, false, BFD_RELOC_RISCV_ALIGN);
4466 riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
4468 /* We need to start a new frag after the alignment which may be removed by
4469 the linker, to prevent the assembler from computing static offsets.
4470 This is necessary to get correct EH info. */
4471 frag_wane (frag_now);
4472 frag_new (0);
4474 return true;
4477 /* Implement HANDLE_ALIGN. */
4479 void
4480 riscv_handle_align (fragS *fragP)
4482 switch (fragP->fr_type)
4484 case rs_align_code:
4485 /* When relaxing, riscv_frag_align_code handles code alignment. */
4486 if (!riscv_opts.relax)
4488 bfd_signed_vma bytes = (fragP->fr_next->fr_address
4489 - fragP->fr_address - fragP->fr_fix);
4490 /* We have 4 byte uncompressed nops. */
4491 bfd_signed_vma size = 4;
4492 bfd_signed_vma excess = bytes % size;
4493 bfd_boolean odd_padding = (excess % 2 == 1);
4494 char *p = fragP->fr_literal + fragP->fr_fix;
4496 if (bytes <= 0)
4497 break;
4499 /* Insert zeros or compressed nops to get 4 byte alignment. */
4500 if (excess)
4502 if (odd_padding)
4503 riscv_add_odd_padding_symbol (fragP);
4504 riscv_make_nops (p, excess);
4505 fragP->fr_fix += excess;
4506 p += excess;
4509 /* The frag will be changed to `rs_fill` later. The function
4510 `write_contents` will try to fill the remaining spaces
4511 according to the patterns we give. In this case, we give
4512 a 4 byte uncompressed nop as the pattern, and set the size
4513 of the pattern into `fr_var`. The nop will be output to the
4514 file `fr_offset` times. However, `fr_offset` could be zero
4515 if we don't need to pad the boundary finally. */
4516 riscv_make_nops (p, size);
4517 fragP->fr_var = size;
4519 break;
4521 default:
4522 break;
4526 /* This usually called from frag_var. */
4528 void
4529 riscv_init_frag (fragS * fragP, int max_chars)
4531 /* Do not add mapping symbol to debug sections. */
4532 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
4533 return;
4535 switch (fragP->fr_type)
4537 case rs_fill:
4538 case rs_align:
4539 case rs_align_test:
4540 riscv_mapping_state (MAP_DATA, max_chars, false/* fr_align_code */);
4541 break;
4542 case rs_align_code:
4543 riscv_mapping_state (MAP_INSN, max_chars, true/* fr_align_code */);
4544 break;
4545 default:
4546 break;
4551 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
4553 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
4556 /* Translate internal representation of relocation info to BFD target
4557 format. */
4559 arelent *
4560 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4562 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
4564 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4565 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4566 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4567 reloc->addend = fixp->fx_addnumber;
4569 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
4570 if (reloc->howto == NULL)
4572 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
4573 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
4575 /* We don't have R_RISCV_8/16, but for this special case,
4576 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
4577 return reloc;
4580 as_bad_where (fixp->fx_file, fixp->fx_line,
4581 _("cannot represent %s relocation in object file"),
4582 bfd_get_reloc_code_name (fixp->fx_r_type));
4583 return NULL;
4586 return reloc;
4590 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
4592 if (RELAX_BRANCH_P (fragp->fr_subtype))
4594 offsetT old_var = fragp->fr_var;
4595 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
4596 return fragp->fr_var - old_var;
4599 return 0;
4602 /* Expand far branches to multi-instruction sequences. */
4604 static void
4605 md_convert_frag_branch (fragS *fragp)
4607 bfd_byte *buf;
4608 expressionS exp;
4609 fixS *fixp;
4610 insn_t insn;
4611 int rs1, reloc;
4613 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
4615 exp.X_op = O_symbol;
4616 exp.X_add_symbol = fragp->fr_symbol;
4617 exp.X_add_number = fragp->fr_offset;
4619 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
4621 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
4623 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4625 case 8:
4626 case 4:
4627 /* Expand the RVC branch into a RISC-V one. */
4628 insn = bfd_getl16 (buf);
4629 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
4630 if ((insn & MASK_C_J) == MATCH_C_J)
4631 insn = MATCH_JAL;
4632 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
4633 insn = MATCH_JAL | (X_RA << OP_SH_RD);
4634 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
4635 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
4636 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
4637 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
4638 else
4639 abort ();
4640 bfd_putl32 (insn, buf);
4641 break;
4643 case 6:
4644 /* Invert the branch condition. Branch over the jump. */
4645 insn = bfd_getl16 (buf);
4646 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
4647 insn |= ENCODE_CBTYPE_IMM (6);
4648 bfd_putl16 (insn, buf);
4649 buf += 2;
4650 goto jump;
4652 case 2:
4653 /* Just keep the RVC branch. */
4654 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4655 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
4656 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4657 2, &exp, false, reloc);
4658 buf += 2;
4659 goto done;
4661 default:
4662 abort ();
4666 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4668 case 8:
4669 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
4671 /* Invert the branch condition. Branch over the jump. */
4672 insn = bfd_getl32 (buf);
4673 insn ^= MATCH_BEQ ^ MATCH_BNE;
4674 insn |= ENCODE_BTYPE_IMM (8);
4675 bfd_putl32 (insn, buf);
4676 buf += 4;
4678 jump:
4679 /* Jump to the target. */
4680 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4681 4, &exp, false, BFD_RELOC_RISCV_JMP);
4682 bfd_putl32 (MATCH_JAL, buf);
4683 buf += 4;
4684 break;
4686 case 4:
4687 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4688 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
4689 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4690 4, &exp, false, reloc);
4691 buf += 4;
4692 break;
4694 default:
4695 abort ();
4698 done:
4699 fixp->fx_file = fragp->fr_file;
4700 fixp->fx_line = fragp->fr_line;
4702 gas_assert (buf == (bfd_byte *)fragp->fr_literal
4703 + fragp->fr_fix + fragp->fr_var);
4705 fragp->fr_fix += fragp->fr_var;
4708 /* Relax a machine dependent frag. This returns the amount by which
4709 the current size of the frag should change. */
4711 void
4712 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
4713 fragS *fragp)
4715 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
4716 md_convert_frag_branch (fragp);
4719 void
4720 md_show_usage (FILE *stream)
4722 fprintf (stream, _("\
4723 RISC-V options:\n\
4724 -fpic or -fPIC generate position-independent code\n\
4725 -fno-pic don't generate position-independent code (default)\n\
4726 -march=ISA set the RISC-V architecture\n\
4727 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
4728 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9.1, 1.10, 1.11, 1.12)\n\
4729 -mabi=ABI set the RISC-V ABI\n\
4730 -mrelax enable relax (default)\n\
4731 -mno-relax disable relax\n\
4732 -march-attr generate RISC-V arch attribute\n\
4733 -mno-arch-attr don't generate RISC-V arch attribute\n\
4734 -mcsr-check enable the csr ISA and privilege spec version checks\n\
4735 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
4736 -mbig-endian assemble for big-endian\n\
4737 -mlittle-endian assemble for little-endian\n\
4738 "));
4741 /* Standard calling conventions leave the CFA at SP on entry. */
4743 void
4744 riscv_cfi_frame_initial_instructions (void)
4746 cfi_add_CFA_def_cfa_register (X_SP);
4750 tc_riscv_regname_to_dw2regnum (char *regname)
4752 int reg;
4754 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
4755 return reg;
4757 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
4758 return reg + 32;
4760 if ((reg = reg_lookup_internal (regname, RCLASS_VECR)) >= 0)
4761 return reg + 96;
4763 /* CSRs are numbered 4096 -> 8191. */
4764 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
4765 return reg + 4096;
4767 as_bad (_("unknown register `%s'"), regname);
4768 return -1;
4771 void
4772 riscv_elf_final_processing (void)
4774 riscv_set_abi_by_arch ();
4775 riscv_release_subset_list (riscv_rps_as.subset_list);
4776 elf_elfheader (stdoutput)->e_flags |= elf_flags;
4779 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
4780 since these directives break relaxation when used with symbol deltas. */
4782 static void
4783 s_riscv_leb128 (int sign)
4785 expressionS exp;
4786 char *save_in = input_line_pointer;
4788 expression (&exp);
4789 if (sign && exp.X_op != O_constant)
4790 as_bad (_("non-constant .sleb128 is not supported"));
4791 else if (!sign && exp.X_op != O_constant && exp.X_op != O_subtract)
4792 as_bad (_(".uleb128 only supports constant or subtract expressions"));
4794 demand_empty_rest_of_line ();
4796 input_line_pointer = save_in;
4797 return s_leb128 (sign);
4800 /* Parse the .insn directive. There are three formats,
4801 Format 1: .insn <type> <operand1>, <operand2>, ...
4802 Format 2: .insn <length>, <value>
4803 Format 3: .insn <value>. */
4805 static void
4806 s_riscv_insn (int x ATTRIBUTE_UNUSED)
4808 char *str = input_line_pointer;
4809 struct riscv_cl_insn insn;
4810 expressionS imm_expr;
4811 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
4812 char save_c;
4814 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4815 ++input_line_pointer;
4817 save_c = *input_line_pointer;
4818 *input_line_pointer = '\0';
4820 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
4822 struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
4823 &imm_reloc, insn_type_hash);
4824 if (error.msg)
4826 char *save_in = input_line_pointer;
4827 error.msg = riscv_ip_hardcode (str, &insn, &imm_expr, error.msg);
4828 input_line_pointer = save_in;
4831 if (error.msg)
4833 if (error.missing_ext)
4834 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
4835 error.missing_ext);
4836 else
4837 as_bad ("%s `%s'", error.msg, error.statement);
4839 else
4841 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
4842 append_insn (&insn, &imm_expr, imm_reloc);
4845 *input_line_pointer = save_c;
4846 demand_empty_rest_of_line ();
4849 /* Update architecture and privileged elf attributes. If we don't set
4850 them, then try to output the default ones. */
4852 static void
4853 riscv_write_out_attrs (void)
4855 const char *arch_str, *priv_str, *p;
4856 /* versions[0]: major version.
4857 versions[1]: minor version.
4858 versions[2]: revision version. */
4859 unsigned versions[3] = {0}, number = 0;
4860 unsigned int i;
4862 /* Re-write architecture elf attribute. */
4863 arch_str = riscv_rps_as.subset_list->arch_str;
4864 bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
4866 /* For the file without any instruction, we don't set the default_priv_spec
4867 according to the privileged elf attributes since the md_assemble isn't
4868 called. */
4869 if (!start_assemble
4870 && !riscv_set_default_priv_spec (NULL))
4871 return;
4873 /* If we already have set privileged elf attributes, then no need to do
4874 anything. Otherwise, don't generate or update them when no CSR and
4875 privileged instructions are used. */
4876 if (!explicit_priv_attr)
4877 return;
4879 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
4880 p = priv_str;
4881 for (i = 0; *p; ++p)
4883 if (*p == '.' && i < 3)
4885 versions[i++] = number;
4886 number = 0;
4888 else if (ISDIGIT (*p))
4889 number = (number * 10) + (*p - '0');
4890 else
4892 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
4893 return;
4896 versions[i] = number;
4898 /* Re-write privileged elf attributes. */
4899 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
4900 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
4901 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
4904 /* Add the default contents for the .riscv.attributes section. */
4906 static void
4907 riscv_set_public_attributes (void)
4909 if (riscv_opts.arch_attr || explicit_attr)
4910 riscv_write_out_attrs ();
4913 /* Scan uleb128 subtraction expressions and insert fixups for them.
4914 e.g., .uleb128 .L1 - .L0
4915 Because relaxation may change the value of the subtraction, we
4916 must resolve them at link-time. */
4918 static void
4919 riscv_insert_uleb128_fixes (bfd *abfd ATTRIBUTE_UNUSED,
4920 asection *sec, void *xxx ATTRIBUTE_UNUSED)
4922 segment_info_type *seginfo = seg_info (sec);
4923 struct frag *fragP;
4925 subseg_set (sec, 0);
4927 for (fragP = seginfo->frchainP->frch_root;
4928 fragP; fragP = fragP->fr_next)
4930 expressionS *exp, *exp_dup;
4932 if (fragP->fr_type != rs_leb128 || fragP->fr_symbol == NULL)
4933 continue;
4935 exp = symbol_get_value_expression (fragP->fr_symbol);
4937 if (exp->X_op != O_subtract)
4938 continue;
4940 /* Only unsigned leb128 can be handled. */
4941 gas_assert (fragP->fr_subtype == 0);
4942 exp_dup = xmemdup (exp, sizeof (*exp), sizeof (*exp));
4943 exp_dup->X_op = O_symbol;
4944 exp_dup->X_op_symbol = NULL;
4946 /* Insert relocations to resolve the subtraction at link-time.
4947 Emit the SET relocation first in riscv. */
4948 exp_dup->X_add_symbol = exp->X_add_symbol;
4949 fix_new_exp (fragP, fragP->fr_fix, 0,
4950 exp_dup, 0, BFD_RELOC_RISCV_SET_ULEB128);
4951 exp_dup->X_add_symbol = exp->X_op_symbol;
4952 fix_new_exp (fragP, fragP->fr_fix, 0,
4953 exp_dup, 0, BFD_RELOC_RISCV_SUB_ULEB128);
4957 /* Called after all assembly has been done. */
4959 void
4960 riscv_md_finish (void)
4962 riscv_set_public_attributes ();
4963 if (riscv_opts.relax)
4964 bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL);
4967 /* Adjust the symbol table. */
4969 void
4970 riscv_adjust_symtab (void)
4972 bfd_map_over_sections (stdoutput, riscv_check_mapping_symbols, (char *) 0);
4973 elf_adjust_symtab ();
4976 /* Given a symbolic attribute NAME, return the proper integer value.
4977 Returns -1 if the attribute is not known. */
4980 riscv_convert_symbolic_attribute (const char *name)
4982 static const struct
4984 const char *name;
4985 const int tag;
4987 attribute_table[] =
4989 /* When you modify this table you should
4990 also modify the list in doc/c-riscv.texi. */
4991 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
4992 T(arch),
4993 T(priv_spec),
4994 T(priv_spec_minor),
4995 T(priv_spec_revision),
4996 T(unaligned_access),
4997 T(stack_align),
4998 #undef T
5001 if (name == NULL)
5002 return -1;
5004 unsigned int i;
5005 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
5006 if (strcmp (name, attribute_table[i].name) == 0)
5007 return attribute_table[i].tag;
5009 return -1;
5012 /* Parse a .attribute directive. */
5014 static void
5015 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
5017 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
5018 unsigned old_xlen;
5019 obj_attribute *attr;
5021 explicit_attr = true;
5022 switch (tag)
5024 case Tag_RISCV_arch:
5025 old_xlen = xlen;
5026 attr = elf_known_obj_attributes_proc (stdoutput);
5027 if (!start_assemble)
5028 riscv_set_arch (attr[Tag_RISCV_arch].s);
5029 else
5030 as_fatal (_("architecture elf attributes must set before "
5031 "any instructions"));
5033 if (old_xlen != xlen)
5035 /* We must re-init bfd again if xlen is changed. */
5036 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
5037 bfd_find_target (riscv_target_format (), stdoutput);
5039 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
5040 as_warn (_("could not set architecture and machine"));
5042 break;
5044 case Tag_RISCV_priv_spec:
5045 case Tag_RISCV_priv_spec_minor:
5046 case Tag_RISCV_priv_spec_revision:
5047 if (start_assemble)
5048 as_fatal (_("privileged elf attributes must set before "
5049 "any instructions"));
5050 break;
5052 default:
5053 break;
5057 /* Mark symbol that it follows a variant CC convention. */
5059 static void
5060 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
5062 char *name;
5063 char c;
5064 symbolS *sym;
5065 asymbol *bfdsym;
5066 elf_symbol_type *elfsym;
5068 c = get_symbol_name (&name);
5069 if (!*name)
5070 as_bad (_("missing symbol name for .variant_cc directive"));
5071 sym = symbol_find_or_make (name);
5072 restore_line_pointer (c);
5073 demand_empty_rest_of_line ();
5075 bfdsym = symbol_get_bfdsym (sym);
5076 elfsym = elf_symbol_from (bfdsym);
5077 gas_assert (elfsym);
5078 elfsym->internal_elf_sym.st_other |= STO_RISCV_VARIANT_CC;
5081 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5082 This is needed so RISC-V specific st_other values can be independently
5083 specified for an IFUNC resolver (that is called by the dynamic linker)
5084 and the symbol it resolves (aliased to the resolver). In particular,
5085 if a function symbol has special st_other value set via directives,
5086 then attaching an IFUNC resolver to that symbol should not override
5087 the st_other setting. Requiring the directive on the IFUNC resolver
5088 symbol would be unexpected and problematic in C code, where the two
5089 symbols appear as two independent function declarations. */
5091 void
5092 riscv_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
5094 struct elf_obj_sy *srcelf = symbol_get_obj (src);
5095 struct elf_obj_sy *destelf = symbol_get_obj (dest);
5096 /* If size is unset, copy size from src. Because we don't track whether
5097 .size has been used, we can't differentiate .size dest, 0 from the case
5098 where dest's size is unset. */
5099 if (!destelf->size && S_GET_SIZE (dest) == 0)
5101 if (srcelf->size)
5103 destelf->size = XNEW (expressionS);
5104 *destelf->size = *srcelf->size;
5106 S_SET_SIZE (dest, S_GET_SIZE (src));
5110 /* RISC-V pseudo-ops table. */
5111 static const pseudo_typeS riscv_pseudo_table[] =
5113 {"option", s_riscv_option, 0},
5114 {"half", cons, 2},
5115 {"word", cons, 4},
5116 {"dword", cons, 8},
5117 {"dtprelword", s_dtprel, 4},
5118 {"dtpreldword", s_dtprel, 8},
5119 {"bss", s_bss, 0},
5120 {"uleb128", s_riscv_leb128, 0},
5121 {"sleb128", s_riscv_leb128, 1},
5122 {"insn", s_riscv_insn, 0},
5123 {"attribute", s_riscv_attribute, 0},
5124 {"variant_cc", s_variant_cc, 0},
5125 {"float16", float_cons, 'h'},
5127 { NULL, NULL, 0 },
5130 void
5131 riscv_pop_insert (void)
5133 extern void pop_insert (const pseudo_typeS *);
5135 pop_insert (riscv_pseudo_table);