Automatic date update in version.in
[binutils-gdb.git] / gas / config / tc-riscv.c
blob1757ff6e9e2846fd3e5d01d1e6b8a989bcb118dd
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2024 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 /* The identifier of the assembler macro we are expanding, if any. */
63 static int source_macro = -1;
65 /* All RISC-V CSR belong to one of these classes. */
66 enum riscv_csr_class
68 CSR_CLASS_NONE,
70 CSR_CLASS_I,
71 CSR_CLASS_I_32, /* rv32 only */
72 CSR_CLASS_F, /* f-ext only */
73 CSR_CLASS_ZKR, /* zkr only */
74 CSR_CLASS_V, /* rvv only */
75 CSR_CLASS_DEBUG, /* debug CSR */
76 CSR_CLASS_H, /* hypervisor */
77 CSR_CLASS_H_32, /* hypervisor, rv32 only */
78 CSR_CLASS_SMAIA, /* Smaia */
79 CSR_CLASS_SMAIA_32, /* Smaia, rv32 only */
80 CSR_CLASS_SMCNTRPMF, /* Smcntrpmf */
81 CSR_CLASS_SMCNTRPMF_32, /* Smcntrpmf, rv32 only */
82 CSR_CLASS_SMSTATEEN, /* Smstateen only */
83 CSR_CLASS_SMSTATEEN_32, /* Smstateen RV32 only */
84 CSR_CLASS_SSAIA, /* Ssaia */
85 CSR_CLASS_SSAIA_AND_H, /* Ssaia with H */
86 CSR_CLASS_SSAIA_32, /* Ssaia, rv32 only */
87 CSR_CLASS_SSAIA_AND_H_32, /* Ssaia with H, rv32 only */
88 CSR_CLASS_SSSTATEEN, /* S[ms]stateen only */
89 CSR_CLASS_SSSTATEEN_AND_H, /* S[ms]stateen only (with H) */
90 CSR_CLASS_SSSTATEEN_AND_H_32, /* S[ms]stateen RV32 only (with H) */
91 CSR_CLASS_SSCOFPMF, /* Sscofpmf only */
92 CSR_CLASS_SSCOFPMF_32, /* Sscofpmf RV32 only */
93 CSR_CLASS_SSTC, /* Sstc only */
94 CSR_CLASS_SSTC_AND_H, /* Sstc only (with H) */
95 CSR_CLASS_SSTC_32, /* Sstc RV32 only */
96 CSR_CLASS_SSTC_AND_H_32, /* Sstc RV32 only (with H) */
97 CSR_CLASS_XTHEADVECTOR, /* xtheadvector only */
100 /* This structure holds all restricted conditions for a CSR. */
101 struct riscv_csr_extra
103 /* Class to which this CSR belongs. Used to decide whether or
104 not this CSR is legal in the current -march context. */
105 enum riscv_csr_class csr_class;
107 /* CSR may have differnet numbers in the previous priv spec. */
108 unsigned address;
110 /* Record the CSR is defined/valid in which versions. */
111 enum riscv_spec_class define_version;
113 /* Record the CSR is aborted/invalid from which versions. If it isn't
114 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
115 enum riscv_spec_class abort_version;
117 /* The CSR may have more than one setting. */
118 struct riscv_csr_extra *next;
121 /* This structure contains information about errors that occur within the
122 riscv_ip function */
123 struct riscv_ip_error
125 /* General error message */
126 const char* msg;
128 /* Statement that caused the error */
129 char* statement;
131 /* Missing extension that needs to be enabled */
132 const char* missing_ext;
135 #ifndef DEFAULT_ARCH
136 #define DEFAULT_ARCH "riscv64"
137 #endif
139 #ifndef DEFAULT_RISCV_ATTR
140 #define DEFAULT_RISCV_ATTR 0
141 #endif
143 /* Let riscv_after_parse_args set the default value according to xlen. */
144 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
145 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
146 #endif
148 /* Need to sync the version with RISC-V compiler. */
149 #ifndef DEFAULT_RISCV_ISA_SPEC
150 #define DEFAULT_RISCV_ISA_SPEC "20191213"
151 #endif
153 #ifndef DEFAULT_RISCV_PRIV_SPEC
154 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
155 #endif
157 static const char default_arch[] = DEFAULT_ARCH;
158 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
159 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
160 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
162 static unsigned xlen = 0; /* The width of an x-register. */
163 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
164 static bool rve_abi = false;
165 enum float_abi
167 FLOAT_ABI_DEFAULT = -1,
168 FLOAT_ABI_SOFT,
169 FLOAT_ABI_SINGLE,
170 FLOAT_ABI_DOUBLE,
171 FLOAT_ABI_QUAD
173 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
175 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
176 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
178 static unsigned elf_flags = 0;
180 static bool probing_insn_operands;
182 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
183 Otherwise, return 1. */
185 static int
186 riscv_set_default_isa_spec (const char *s)
188 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
189 RISCV_GET_ISA_SPEC_CLASS (s, class);
190 if (class == ISA_SPEC_CLASS_NONE)
192 as_bad ("unknown default ISA spec `%s' set by "
193 "-misa-spec or --with-isa-spec", s);
194 return 0;
196 else
197 default_isa_spec = class;
198 return 1;
201 /* Set the default_priv_spec. Find the privileged elf attributes when
202 the input string is NULL. Return 0 if the spec isn't supported.
203 Otherwise, return 1. */
205 static int
206 riscv_set_default_priv_spec (const char *s)
208 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
209 unsigned major, minor, revision;
210 obj_attribute *attr;
212 RISCV_GET_PRIV_SPEC_CLASS (s, class);
213 if (class != PRIV_SPEC_CLASS_NONE
214 && class != PRIV_SPEC_CLASS_1P9P1)
216 default_priv_spec = class;
217 return 1;
220 if (s != NULL)
222 as_bad (_("unknown default privileged spec `%s' set by "
223 "-mpriv-spec or --with-priv-spec"), s);
224 return 0;
227 /* Set the default_priv_spec by the privileged elf attributes. */
228 attr = elf_known_obj_attributes_proc (stdoutput);
229 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
230 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
231 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
232 /* Version 0.0.0 is the default value and meningless. */
233 if (major == 0 && minor == 0 && revision == 0)
234 return 1;
236 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
237 if (class != PRIV_SPEC_CLASS_NONE)
239 default_priv_spec = class;
240 return 1;
243 /* Still can not find the privileged spec class. */
244 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
245 "privileged elf attributes"), major, minor, revision);
246 return 0;
249 /* This is the set of options which the .option pseudo-op may modify. */
250 struct riscv_set_options
252 int pic; /* Generate position-independent code. */
253 int rvc; /* Generate RVC code. */
254 int relax; /* Emit relocs the linker is allowed to relax. */
255 int arch_attr; /* Emit architecture and privileged elf attributes. */
256 int csr_check; /* Enable the CSR checking. */
259 static struct riscv_set_options riscv_opts =
261 0, /* pic */
262 0, /* rvc */
263 1, /* relax */
264 DEFAULT_RISCV_ATTR, /* arch_attr */
265 0, /* csr_check */
268 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
269 for elf_flags once we have enabled c extension. */
271 static void
272 riscv_set_rvc (bool rvc_value)
274 if (rvc_value)
275 elf_flags |= EF_RISCV_RVC;
277 riscv_opts.rvc = rvc_value;
280 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
282 static void
283 riscv_set_tso (void)
285 elf_flags |= EF_RISCV_TSO;
288 /* The linked list hanging off of .subsets_list records all enabled extensions,
289 which are parsed from the architecture string. The architecture string can
290 be set by the -march option, the elf architecture attributes, and the
291 --with-arch configure option. */
292 static riscv_parse_subset_t riscv_rps_as =
294 NULL, /* subset_list, we will set it later once
295 riscv_opts_stack is created or updated. */
296 as_bad, /* error_handler. */
297 &xlen, /* xlen. */
298 &default_isa_spec, /* isa_spec. */
299 true, /* check_unknown_prefixed_ext. */
302 /* Update the architecture string in the subset_list. */
304 static void
305 riscv_reset_subsets_list_arch_str (void)
307 riscv_subset_list_t *subsets = riscv_rps_as.subset_list;
308 if (subsets->arch_str != NULL)
309 free ((void *) subsets->arch_str);
310 subsets->arch_str = riscv_arch_str (xlen, subsets);
313 /* This structure is used to hold a stack of .option values. */
314 struct riscv_option_stack
316 struct riscv_option_stack *next;
317 struct riscv_set_options options;
318 riscv_subset_list_t *subset_list;
321 static struct riscv_option_stack *riscv_opts_stack = NULL;
323 /* Set which ISA and extensions are available. */
325 static void
326 riscv_set_arch (const char *s)
328 if (s != NULL && strcmp (s, "") == 0)
330 as_bad (_("the architecture string of -march and elf architecture "
331 "attributes cannot be empty"));
332 return;
335 if (riscv_rps_as.subset_list == NULL)
337 riscv_rps_as.subset_list = XNEW (riscv_subset_list_t);
338 riscv_rps_as.subset_list->head = NULL;
339 riscv_rps_as.subset_list->tail = NULL;
340 riscv_rps_as.subset_list->arch_str = NULL;
342 riscv_release_subset_list (riscv_rps_as.subset_list);
343 riscv_parse_subset (&riscv_rps_as, s);
344 riscv_reset_subsets_list_arch_str ();
346 riscv_set_rvc (false);
347 if (riscv_subset_supports (&riscv_rps_as, "c")
348 || riscv_subset_supports (&riscv_rps_as, "zca"))
349 riscv_set_rvc (true);
351 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
352 riscv_set_tso ();
355 /* Indicate -mabi option is explictly set. */
356 static bool explicit_mabi = false;
358 /* Set the abi information. */
360 static void
361 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
363 abi_xlen = new_xlen;
364 float_abi = new_float_abi;
365 rve_abi = rve;
368 /* If the -mabi option isn't set, then set the abi according to the
369 ISA string. Otherwise, check if there is any conflict. */
371 static void
372 riscv_set_abi_by_arch (void)
374 if (!explicit_mabi)
376 if (riscv_subset_supports (&riscv_rps_as, "q"))
377 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
378 else if (riscv_subset_supports (&riscv_rps_as, "d"))
379 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
380 else if (riscv_subset_supports (&riscv_rps_as, "e"))
381 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
382 else
383 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
385 else
387 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
388 if (abi_xlen > xlen)
389 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
390 else if (abi_xlen < xlen)
391 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
393 if (riscv_subset_supports (&riscv_rps_as, "e") && !rve_abi)
394 as_bad ("only ilp32e/lp64e ABI are supported for e extension");
396 if (float_abi == FLOAT_ABI_SINGLE
397 && !riscv_subset_supports (&riscv_rps_as, "f"))
398 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
399 "isn't supported");
400 else if (float_abi == FLOAT_ABI_DOUBLE
401 && !riscv_subset_supports (&riscv_rps_as, "d"))
402 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
403 "isn't supported");
404 else if (float_abi == FLOAT_ABI_QUAD
405 && !riscv_subset_supports (&riscv_rps_as, "q"))
406 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
407 "isn't supported");
410 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
411 elf_flags &= ~EF_RISCV_FLOAT_ABI;
412 elf_flags |= float_abi << 1;
414 if (rve_abi)
415 elf_flags |= EF_RISCV_RVE;
418 /* Handle of the OPCODE hash table. */
419 static htab_t op_hash = NULL;
421 /* Handle of the type of .insn hash table. */
422 static htab_t insn_type_hash = NULL;
424 /* This array holds the chars that always start a comment. If the
425 pre-processor is disabled, these aren't very useful. */
426 const char comment_chars[] = "#";
428 /* This array holds the chars that only start a comment at the beginning of
429 a line. If the line seems to have the form '# 123 filename'
430 .line and .file directives will appear in the pre-processed output
432 Note that input_file.c hand checks for '#' at the beginning of the
433 first line of the input file. This is because the compiler outputs
434 #NO_APP at the beginning of its output.
436 Also note that C style comments are always supported. */
437 const char line_comment_chars[] = "#";
439 /* This array holds machine specific line separator characters. */
440 const char line_separator_chars[] = ";";
442 /* Chars that can be used to separate mant from exp in floating point nums. */
443 const char EXP_CHARS[] = "eE";
445 /* Chars that mean this number is a floating point constant.
446 As in 0f12.456 or 0d1.2345e12. */
447 const char FLT_CHARS[] = "rRsSfFdDxXpPhH";
449 /* Indicate we are already assemble any instructions or not. */
450 static bool start_assemble = false;
452 /* Indicate ELF attributes are explicitly set. */
453 static bool explicit_attr = false;
455 /* Indicate CSR or priv instructions are explicitly used. */
456 static bool explicit_priv_attr = false;
458 static char *expr_parse_end;
460 /* Macros for encoding relaxation state for RVC branches and far jumps. */
461 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
462 ((relax_substateT) \
463 (0xc0000000 \
464 | ((uncond) ? 1 : 0) \
465 | ((rvc) ? 2 : 0) \
466 | ((length) << 2)))
467 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
468 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
469 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
470 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
472 /* Is the given value a sign-extended 32-bit value? */
473 #define IS_SEXT_32BIT_NUM(x) \
474 (((x) &~ (offsetT) 0x7fffffff) == 0 \
475 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
477 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
478 #define IS_ZEXT_32BIT_NUM(x) \
479 (((x) &~ (offsetT) 0xffffffff) == 0 \
480 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
482 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
483 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
484 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
485 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
487 #define INSERT_IMM(n, s, INSN, VALUE) \
488 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
490 /* Determine if an instruction matches an opcode. */
491 #define OPCODE_MATCHES(OPCODE, OP) \
492 (((OPCODE) & MASK_##OP) == MATCH_##OP)
494 /* Create a new mapping symbol for the transition to STATE. */
496 static void
497 make_mapping_symbol (enum riscv_seg_mstate state,
498 valueT value,
499 fragS *frag,
500 const char *arch_str,
501 bool odd_data_padding)
503 const char *name;
504 char *buff = NULL;
505 switch (state)
507 case MAP_DATA:
508 name = "$d";
509 break;
510 case MAP_INSN:
511 if (arch_str != NULL)
513 size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */
514 buff = xmalloc (size);
515 snprintf (buff, size, "$x%s", arch_str);
516 name = buff;
518 else
519 name = "$x";
520 break;
521 default:
522 abort ();
525 symbolS *symbol = symbol_new (name, now_seg, frag, value);
526 symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL);
527 if (arch_str != NULL)
529 /* Store current $x+arch into tc_segment_info. */
530 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol;
531 xfree ((void *) buff);
534 /* If .fill or other data filling directive generates zero sized data,
535 then mapping symbol for the following code will have the same value.
537 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
538 and .text.zero.fill.last. */
539 symbolS *first = frag->tc_frag_data.first_map_symbol;
540 symbolS *last = frag->tc_frag_data.last_map_symbol;
541 symbolS *removed = NULL;
542 if (value == 0)
544 if (first != NULL)
546 know (S_GET_VALUE (first) == S_GET_VALUE (symbol)
547 && first == last);
548 /* Remove the old one. */
549 removed = first;
551 frag->tc_frag_data.first_map_symbol = symbol;
553 else if (last != NULL)
555 /* The mapping symbols should be added in offset order. */
556 know (S_GET_VALUE (last) <= S_GET_VALUE (symbol));
557 /* Remove the old one. */
558 if (S_GET_VALUE (last) == S_GET_VALUE (symbol))
559 removed = last;
561 frag->tc_frag_data.last_map_symbol = symbol;
563 if (removed == NULL)
564 return;
566 if (odd_data_padding)
568 /* If the removed mapping symbol is $x+arch, then add it back to
569 the next $x. */
570 const char *str = strncmp (S_GET_NAME (removed), "$xrv", 4) == 0
571 ? S_GET_NAME (removed) + 2 : NULL;
572 make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str,
573 false/* odd_data_padding */);
575 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
578 /* Set the mapping state for frag_now. */
580 void
581 riscv_mapping_state (enum riscv_seg_mstate to_state,
582 int max_chars,
583 bool fr_align_code)
585 enum riscv_seg_mstate from_state =
586 seg_info (now_seg)->tc_segment_info_data.map_state;
587 bool reset_seg_arch_str = false;
589 if (!SEG_NORMAL (now_seg)
590 /* For now we only add the mapping symbols to text sections.
591 Therefore, the dis-assembler only show the actual contents
592 distribution for text. Other sections will be shown as
593 data without the details. */
594 || !subseg_text_p (now_seg))
595 return;
597 /* The mapping symbol should be emitted if not in the right
598 mapping state. */
599 symbolS *seg_arch_symbol =
600 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol;
601 if (to_state == MAP_INSN && seg_arch_symbol == 0)
603 /* Always add $x+arch at the first instruction of section. */
604 reset_seg_arch_str = true;
606 else if (seg_arch_symbol != 0
607 && to_state == MAP_INSN
608 && !fr_align_code
609 && strcmp (riscv_rps_as.subset_list->arch_str,
610 S_GET_NAME (seg_arch_symbol) + 2) != 0)
612 reset_seg_arch_str = true;
614 else if (from_state == to_state)
615 return;
617 valueT value = (valueT) (frag_now_fix () - max_chars);
618 seg_info (now_seg)->tc_segment_info_data.map_state = to_state;
619 const char *arch_str = reset_seg_arch_str
620 ? riscv_rps_as.subset_list->arch_str : NULL;
621 make_mapping_symbol (to_state, value, frag_now, arch_str,
622 false/* odd_data_padding */);
625 /* Add the odd bytes of paddings for riscv_handle_align. */
627 static void
628 riscv_add_odd_padding_symbol (fragS *frag)
630 /* If there was already a mapping symbol, it should be
631 removed in the make_mapping_symbol.
633 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
634 make_mapping_symbol (MAP_DATA, frag->fr_fix, frag,
635 NULL/* arch_str */, true/* odd_data_padding */);
638 /* Remove any excess mapping symbols generated for alignment frags in
639 SEC. We may have created a mapping symbol before a zero byte
640 alignment; remove it if there's a mapping symbol after the
641 alignment. */
643 static void
644 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
645 asection *sec,
646 void *dummy ATTRIBUTE_UNUSED)
648 segment_info_type *seginfo = seg_info (sec);
649 fragS *fragp;
651 if (seginfo == NULL || seginfo->frchainP == NULL)
652 return;
654 for (fragp = seginfo->frchainP->frch_root;
655 fragp != NULL;
656 fragp = fragp->fr_next)
658 symbolS *last = fragp->tc_frag_data.last_map_symbol;
659 fragS *next = fragp->fr_next;
661 if (last == NULL || next == NULL)
662 continue;
664 /* Check the last mapping symbol if it is at the boundary of
665 fragment. */
666 if (S_GET_VALUE (last) < next->fr_address)
667 continue;
668 know (S_GET_VALUE (last) == next->fr_address);
672 symbolS *next_first = next->tc_frag_data.first_map_symbol;
673 if (next_first != NULL)
675 /* The last mapping symbol overlaps with another one
676 which at the start of the next frag.
678 Please see the gas/testsuite/gas/riscv/mapping.s:
679 .text.zero.fill.align.A and .text.zero.fill.align.B. */
680 know (S_GET_VALUE (last) == S_GET_VALUE (next_first));
681 symbolS *removed = last;
682 if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0
683 && strcmp (S_GET_NAME (next_first), "$x") == 0)
684 removed = next_first;
685 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
686 break;
689 if (next->fr_next == NULL)
691 /* The last mapping symbol is at the end of the section.
693 Please see the gas/testsuite/gas/riscv/mapping.s:
694 .text.last.section. */
695 know (next->fr_fix == 0 && next->fr_var == 0);
696 symbol_remove (last, &symbol_rootP, &symbol_lastP);
697 break;
700 /* Since we may have empty frags without any mapping symbols,
701 keep looking until the non-empty frag. */
702 if (next->fr_address != next->fr_next->fr_address)
703 break;
705 next = next->fr_next;
707 while (next != NULL);
711 /* The default target format to use. */
713 const char *
714 riscv_target_format (void)
716 if (target_big_endian)
717 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
718 else
719 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
722 /* Return the length of instruction INSN. */
724 static inline unsigned int
725 insn_length (const struct riscv_cl_insn *insn)
727 return riscv_insn_length (insn->insn_opcode);
730 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
732 static void
733 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
735 insn->insn_mo = mo;
736 insn->insn_opcode = mo->match;
737 insn->insn_long_opcode[0] = 0;
738 insn->frag = NULL;
739 insn->where = 0;
740 insn->fixp = NULL;
743 /* Install INSN at the location specified by its "frag" and "where" fields. */
745 static void
746 install_insn (const struct riscv_cl_insn *insn)
748 char *f = insn->frag->fr_literal + insn->where;
749 if (insn->insn_long_opcode[0] != 0)
750 memcpy (f, insn->insn_long_opcode, insn_length (insn));
751 else
752 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
755 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
756 and install the opcode in the new location. */
758 static void
759 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
761 insn->frag = frag;
762 insn->where = where;
763 if (insn->fixp != NULL)
765 insn->fixp->fx_frag = frag;
766 insn->fixp->fx_where = where;
768 install_insn (insn);
771 /* Add INSN to the end of the output. */
773 static void
774 add_fixed_insn (struct riscv_cl_insn *insn)
776 char *f = frag_more (insn_length (insn));
777 move_insn (insn, frag_now, f - frag_now->fr_literal);
780 static void
781 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
782 relax_substateT subtype, symbolS *symbol, offsetT offset)
784 frag_grow (max_chars);
785 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
786 frag_var (rs_machine_dependent, max_chars, var,
787 subtype, symbol, offset, NULL);
790 /* Compute the length of a branch sequence, and adjust the stored length
791 accordingly. If FRAGP is NULL, the worst-case length is returned. */
793 static unsigned
794 relaxed_branch_length (fragS *fragp, asection *sec, int update)
796 int jump, rvc, length = 8;
798 if (!fragp)
799 return length;
801 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
802 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
803 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
805 /* Assume jumps are in range; the linker will catch any that aren't. */
806 length = jump ? 4 : 8;
808 if (fragp->fr_symbol != NULL
809 && S_IS_DEFINED (fragp->fr_symbol)
810 && !S_IS_WEAK (fragp->fr_symbol)
811 && sec == S_GET_SEGMENT (fragp->fr_symbol))
813 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
814 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
815 val -= fragp->fr_address + fragp->fr_fix;
817 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
818 length = 2;
819 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
820 length = 4;
821 else if (!jump && rvc)
822 length = 6;
825 if (update)
826 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
828 return length;
831 /* Information about an opcode name, mnemonics and its value. */
832 struct opcode_name_t
834 const char *name;
835 unsigned int val;
838 /* List for all supported opcode name. */
839 static const struct opcode_name_t opcode_name_list[] =
841 {"C0", 0x0},
842 {"C1", 0x1},
843 {"C2", 0x2},
845 {"LOAD", 0x03},
846 {"LOAD_FP", 0x07},
847 {"CUSTOM_0", 0x0b},
848 {"MISC_MEM", 0x0f},
849 {"OP_IMM", 0x13},
850 {"AUIPC", 0x17},
851 {"OP_IMM_32", 0x1b},
852 /* 48b 0x1f. */
854 {"STORE", 0x23},
855 {"STORE_FP", 0x27},
856 {"CUSTOM_1", 0x2b},
857 {"AMO", 0x2f},
858 {"OP", 0x33},
859 {"LUI", 0x37},
860 {"OP_32", 0x3b},
861 /* 64b 0x3f. */
863 {"MADD", 0x43},
864 {"MSUB", 0x47},
865 {"NMADD", 0x4f},
866 {"NMSUB", 0x4b},
867 {"OP_FP", 0x53},
868 {"OP_V", 0x57},
869 {"CUSTOM_2", 0x5b},
870 /* 48b 0x5f. */
872 {"BRANCH", 0x63},
873 {"JALR", 0x67},
874 /*reserved 0x5b. */
875 {"JAL", 0x6f},
876 {"SYSTEM", 0x73},
877 /*reserved 0x77. */
878 {"CUSTOM_3", 0x7b},
879 /* >80b 0x7f. */
881 {NULL, 0}
884 /* Hash table for lookup opcode name. */
885 static htab_t opcode_names_hash = NULL;
887 /* Initialization for hash table of opcode name. */
889 static void
890 init_opcode_names_hash (void)
892 const struct opcode_name_t *opcode;
894 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
895 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
896 as_fatal (_("internal: duplicate %s"), opcode->name);
899 /* Find `s` is a valid opcode name or not, return the opcode name info
900 if found. */
902 static const struct opcode_name_t *
903 opcode_name_lookup (char **s)
905 char *e;
906 char save_c;
907 struct opcode_name_t *o;
909 /* Find end of name. */
910 e = *s;
911 if (is_name_beginner (*e))
912 ++e;
913 while (is_part_of_name (*e))
914 ++e;
916 /* Terminate name. */
917 save_c = *e;
918 *e = '\0';
920 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
922 /* Advance to next token if one was recognized. */
923 if (o)
924 *s = e;
926 *e = save_c;
927 expr_parse_end = e;
929 return o;
932 /* All RISC-V registers belong to one of these classes. */
933 enum reg_class
935 RCLASS_GPR,
936 RCLASS_FPR,
937 RCLASS_VECR,
938 RCLASS_VECM,
939 RCLASS_MAX,
941 RCLASS_CSR
944 static htab_t reg_names_hash = NULL;
945 static htab_t csr_extra_hash = NULL;
947 #define ENCODE_REG_HASH(cls, n) \
948 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
949 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
950 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
952 static void
953 hash_reg_name (enum reg_class class, const char *name, unsigned n)
955 void *hash = ENCODE_REG_HASH (class, n);
956 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
957 as_fatal (_("internal: duplicate %s"), name);
960 static void
961 hash_reg_names (enum reg_class class, const char names[][NRC], unsigned n)
963 unsigned i;
965 for (i = 0; i < n; i++)
966 hash_reg_name (class, names[i], i);
969 /* Init hash table csr_extra_hash to handle CSR. */
971 static void
972 riscv_init_csr_hash (const char *name,
973 unsigned address,
974 enum riscv_csr_class class,
975 enum riscv_spec_class define_version,
976 enum riscv_spec_class abort_version)
978 struct riscv_csr_extra *entry, *pre_entry;
979 bool need_enrty = true;
981 pre_entry = NULL;
982 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
983 while (need_enrty && entry != NULL)
985 if (entry->csr_class == class
986 && entry->address == address
987 && entry->define_version == define_version
988 && entry->abort_version == abort_version)
989 need_enrty = false;
990 pre_entry = entry;
991 entry = entry->next;
994 /* Duplicate CSR. */
995 if (!need_enrty)
996 return;
998 entry = notes_alloc (sizeof (*entry));
999 entry->csr_class = class;
1000 entry->address = address;
1001 entry->define_version = define_version;
1002 entry->abort_version = abort_version;
1003 entry->next = NULL;
1005 if (pre_entry == NULL)
1006 str_hash_insert (csr_extra_hash, name, entry, 0);
1007 else
1008 pre_entry->next = entry;
1011 /* Return the CSR address after checking the ISA dependency and
1012 the privileged spec version.
1014 There are one warning and two errors for CSR,
1016 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1017 or the privileged spec, report warning only if -mcsr-check is set.
1018 Unknown CSR: the CSR has never been defined, report error.
1019 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1021 static unsigned int
1022 riscv_csr_address (const char *csr_name,
1023 struct riscv_csr_extra *entry)
1025 struct riscv_csr_extra *saved_entry = entry;
1026 enum riscv_csr_class csr_class = entry->csr_class;
1027 bool need_check_version = false;
1028 bool is_rv32_only = false;
1029 bool is_h_required = false;
1030 const char* extension = NULL;
1032 switch (csr_class)
1034 case CSR_CLASS_I_32:
1035 is_rv32_only = true;
1036 /* Fall through. */
1037 case CSR_CLASS_I:
1038 need_check_version = true;
1039 extension = "i";
1040 break;
1041 case CSR_CLASS_H_32:
1042 is_rv32_only = true;
1043 /* Fall through. */
1044 case CSR_CLASS_H:
1045 extension = "h";
1046 break;
1047 case CSR_CLASS_F:
1048 extension = "f";
1049 break;
1050 case CSR_CLASS_ZKR:
1051 extension = "zkr";
1052 break;
1053 case CSR_CLASS_V:
1054 extension = "zve32x";
1055 break;
1056 case CSR_CLASS_SMAIA_32:
1057 is_rv32_only = true;
1058 /* Fall through. */
1059 case CSR_CLASS_SMAIA:
1060 extension = "smaia";
1061 break;
1062 case CSR_CLASS_SMCNTRPMF_32:
1063 is_rv32_only = true;
1064 /* Fall through. */
1065 case CSR_CLASS_SMCNTRPMF:
1066 need_check_version = true;
1067 extension = "smcntrpmf";
1068 break;
1069 case CSR_CLASS_SMSTATEEN_32:
1070 is_rv32_only = true;
1071 /* Fall through. */
1072 case CSR_CLASS_SMSTATEEN:
1073 extension = "smstateen";
1074 break;
1075 case CSR_CLASS_SSAIA:
1076 case CSR_CLASS_SSAIA_AND_H:
1077 case CSR_CLASS_SSAIA_32:
1078 case CSR_CLASS_SSAIA_AND_H_32:
1079 is_rv32_only = (csr_class == CSR_CLASS_SSAIA_32
1080 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1081 is_h_required = (csr_class == CSR_CLASS_SSAIA_AND_H
1082 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1083 extension = "ssaia";
1084 break;
1085 case CSR_CLASS_SSSTATEEN_AND_H_32:
1086 is_rv32_only = true;
1087 /* Fall through. */
1088 case CSR_CLASS_SSSTATEEN_AND_H:
1089 is_h_required = true;
1090 /* Fall through. */
1091 case CSR_CLASS_SSSTATEEN:
1092 extension = "ssstateen";
1093 break;
1094 case CSR_CLASS_SSCOFPMF_32:
1095 is_rv32_only = true;
1096 /* Fall through. */
1097 case CSR_CLASS_SSCOFPMF:
1098 extension = "sscofpmf";
1099 break;
1100 case CSR_CLASS_SSTC:
1101 case CSR_CLASS_SSTC_AND_H:
1102 case CSR_CLASS_SSTC_32:
1103 case CSR_CLASS_SSTC_AND_H_32:
1104 is_rv32_only = (csr_class == CSR_CLASS_SSTC_32
1105 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1106 is_h_required = (csr_class == CSR_CLASS_SSTC_AND_H
1107 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1108 extension = "sstc";
1109 break;
1110 case CSR_CLASS_DEBUG:
1111 break;
1112 case CSR_CLASS_XTHEADVECTOR:
1113 extension = "xtheadvector";
1114 break;
1115 default:
1116 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
1119 if (riscv_opts.csr_check)
1121 if (is_rv32_only && xlen != 32)
1122 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name);
1123 if (is_h_required && !riscv_subset_supports (&riscv_rps_as, "h"))
1124 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name);
1126 if (extension != NULL
1127 && !riscv_subset_supports (&riscv_rps_as, extension))
1128 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1129 csr_name, extension);
1132 while (entry != NULL)
1134 if (!need_check_version
1135 || (default_priv_spec >= entry->define_version
1136 && default_priv_spec < entry->abort_version))
1138 /* Find the CSR according to the specific version. */
1139 return entry->address;
1141 entry = entry->next;
1144 /* Can not find the CSR address from the chosen privileged version,
1145 so use the newly defined value. */
1146 if (riscv_opts.csr_check)
1148 const char *priv_name = NULL;
1149 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
1150 if (priv_name != NULL)
1151 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1152 csr_name, priv_name);
1155 return saved_entry->address;
1158 /* Return -1 if the CSR has never been defined. Otherwise, return
1159 the address. */
1161 static unsigned int
1162 reg_csr_lookup_internal (const char *s)
1164 struct riscv_csr_extra *r =
1165 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
1167 if (r == NULL)
1168 return -1U;
1170 return riscv_csr_address (s, r);
1173 static unsigned int
1174 reg_lookup_internal (const char *s, enum reg_class class)
1176 void *r;
1178 if (class == RCLASS_CSR)
1179 return reg_csr_lookup_internal (s);
1181 r = str_hash_find (reg_names_hash, s);
1182 if (r == NULL || DECODE_REG_CLASS (r) != class)
1183 return -1;
1185 if (riscv_subset_supports (&riscv_rps_as, "e")
1186 && class == RCLASS_GPR
1187 && DECODE_REG_NUM (r) > 15)
1188 return -1;
1190 return DECODE_REG_NUM (r);
1193 static bool
1194 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1196 char *e;
1197 char save_c;
1198 int reg = -1;
1200 /* Find end of name. */
1201 e = *s;
1202 if (is_name_beginner (*e))
1203 ++e;
1204 while (is_part_of_name (*e))
1205 ++e;
1207 /* Terminate name. */
1208 save_c = *e;
1209 *e = '\0';
1211 /* Look for the register. Advance to next token if one was recognized. */
1212 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1213 *s = e;
1215 *e = save_c;
1216 if (regnop)
1217 *regnop = reg;
1218 return reg >= 0;
1221 static bool
1222 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
1224 const char *p = strchr (*s, ',');
1225 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
1227 if (len == 0)
1228 return false;
1230 for (i = 0; i < size; i++)
1231 if (array[i] != NULL && strncmp (array[i], *s, len) == 0
1232 && array[i][len] == '\0')
1234 *regnop = i;
1235 *s += len;
1236 return true;
1239 return false;
1242 static bool
1243 flt_lookup (float f, const float *array, size_t size, unsigned *regnop)
1245 size_t i;
1247 for (i = 0; i < size; i++)
1248 if (array[i] == f)
1250 *regnop = i;
1251 return true;
1254 return false;
1257 /* Map ra and s-register to [4,15], so that we can check if the
1258 reg2 in register list reg1-reg2 or single reg2 is valid or not,
1259 and obtain the corresponding reg_list value.
1261 ra - 4
1262 s0 - 5
1263 s1 - 6
1264 ....
1265 s10 - 0 (invalid)
1266 s11 - 15. */
1268 static int
1269 regno_to_reg_list (unsigned regno)
1271 if (regno == X_RA)
1272 return 4;
1273 else if (regno == X_S0 || regno == X_S1)
1274 return 5 + regno - X_S0;
1275 else if (regno >= X_S2 && regno < X_S10)
1276 return 7 + regno - X_S2;
1277 else if (regno == X_S11)
1278 return 15;
1280 /* Invalid symbol. */
1281 return 0;
1284 /* Parse register list, and return the last register by regno_to_reg_list.
1286 If ABI register names are used (e.g. ra and s0), the register
1287 list could be "{ra}", "{ra, s0}", "{ra, s0-sN}", where 0 < N < 10 or
1288 N == 11.
1290 If numeric register names are used (e.g. x1 and x8), the register list
1291 could be "{x1}", "{x1,x8}", "{x1,x8-x9}", "{x1,x8-x9,x18}" and
1292 "{x1,x8-x9,x18-xN}", where 19 < N < 25 or N == 27.
1294 The numeric and ABI register names cannot be used at the same time.
1296 TODO: Report errors for the following cases,
1297 1. Too many registers in the list.
1298 2. Cases which return 0.
1299 3. Illegal formats, for example, {x1,x8-NULL,x18-x24/x18}, {x1-x2,x8}. */
1301 static unsigned
1302 reglist_lookup_internal (char *reglist)
1304 unsigned regno = 0;
1305 unsigned reg_list = 0;
1306 char *regname[3][2] = {{NULL}};
1307 char *save_tok, *save_subtok;
1308 unsigned i, j;
1310 char *token = strtok_r (reglist, ",", &save_tok);
1311 for (i = 0; i < 3 && token != NULL;
1312 token = strtok_r (NULL, ",", &save_tok), i++)
1314 char *subtoken = strtok_r (token, "-", &save_subtok);
1315 for (j = 0; j < 2 && subtoken != NULL;
1316 subtoken = strtok_r (NULL, "-", &save_subtok), j++)
1317 regname[i][j] = subtoken;
1320 bool reg1_numeric = false;
1321 for (i = 0; i < 3; i++)
1323 if (regname[i][0] == NULL)
1324 continue;
1325 #define REG_TO_REG_LIST(NAME, NUM, LIST) \
1326 (reg_lookup (&NAME, RCLASS_GPR, &NUM) && (LIST = regno_to_reg_list (NUM)))
1327 #define REG_NUMERIC(NAME) (NAME[0] == 'x')
1328 #define REG_CONFLICT(NAME, REG_NUMERIC) \
1329 ((NAME[0] == 'x' && !REG_NUMERIC) || (NAME[0] != 'x' && REG_NUMERIC))
1330 switch (i)
1332 case 0:
1333 reg1_numeric = REG_NUMERIC (regname[i][0]);
1334 if (!REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1335 || regno != X_RA)
1336 return 0;
1337 break;
1338 case 1:
1339 if (REG_CONFLICT (regname[i][0], reg1_numeric)
1340 /* The second register should be s0 or its numeric names x8. */
1341 || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1342 || regno != X_S0)
1343 return 0;
1344 else if (regname[i][1] == NULL)
1345 return reg_list;
1347 if (REG_CONFLICT (regname[i][1], reg1_numeric)
1348 /* The third register is x9 if the numeric name is used.
1349 Otherwise, it could be any other sN register, where N > 0. */
1350 || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
1351 || regno <= X_S0
1352 || (reg1_numeric && regno != X_S1))
1353 return 0;
1354 break;
1355 case 2:
1356 /* Must use register numeric names. */
1357 if (!reg1_numeric
1358 || !REG_NUMERIC (regname[i][0])
1359 /* The fourth register should be s2. */
1360 || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1361 || regno != X_S2)
1362 return 0;
1363 else if (regname[i][1] == NULL)
1364 return reg_list;
1366 if (!reg1_numeric
1367 || !REG_NUMERIC (regname[i][1])
1368 /* The fifth register could be any other sN register, where N > 1. */
1369 || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
1370 || regno <= X_S2)
1371 return 0;
1372 break;
1373 default:
1374 return 0;
1376 #undef REG_TO_REG_LIST
1377 #undef REG_NUMERIC
1378 #undef REG_CONFLICT
1380 return reg_list;
1383 /* Parse register list. Return false if REG_LIST is zero, which is an
1384 invalid value. */
1386 static bool
1387 reglist_lookup (char **s, unsigned *reg_list)
1389 *reg_list = 0;
1390 char *reglist = strdup (*s);
1391 if (reglist != NULL)
1393 char *token = strtok (reglist, "}");
1394 if (token != NULL)
1396 *s += strlen (token);
1397 *reg_list = reglist_lookup_internal (reglist);
1399 else
1401 as_bad (_("cannot find `}' for cm.push/cm.pop"));
1402 *reg_list = 0;
1405 free (reglist);
1406 return *reg_list == 0 ? false : true;
1409 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1410 #define USE_IMM(n, s) \
1411 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1413 /* For consistency checking, verify that all bits are specified either
1414 by the match/mask part of the instruction definition, or by the
1415 operand list. The `length` could be the actual instruction length or
1416 0 for auto-detection. */
1418 static bool
1419 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1421 const char *oparg, *opargStart;
1422 insn_t used_bits = opc->mask;
1423 int insn_width;
1424 insn_t required_bits;
1426 if (length == 0)
1427 length = riscv_insn_length (opc->match);
1428 /* We don't support instructions longer than 64-bits yet. */
1429 if (length > 8)
1430 length = 8;
1431 insn_width = 8 * length;
1433 required_bits = ((insn_t)~0ULL) >> (64 - insn_width);
1435 if ((used_bits & opc->match) != (opc->match & required_bits))
1437 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1438 opc->name, opc->args);
1439 return false;
1442 for (oparg = opc->args; *oparg; ++oparg)
1444 opargStart = oparg;
1445 switch (*oparg)
1447 case 'C': /* RVC */
1448 switch (*++oparg)
1450 case 'U': break; /* CRS1, constrained to equal RD. */
1451 case 'c': break; /* CRS1, constrained to equal sp. */
1452 case 'T': /* CRS2, floating point. */
1453 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1454 case 'S': /* CRS1S, floating point. */
1455 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1456 case 'w': break; /* CRS1S, constrained to equal RD. */
1457 case 'D': /* CRS2S, floating point. */
1458 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1459 case 'x': break; /* CRS2S, constrained to equal RD. */
1460 case 'z': break; /* CRS2S, constrained to be x0. */
1461 case '>': /* CITYPE immediate, compressed shift. */
1462 case 'u': /* CITYPE immediate, compressed lui. */
1463 case 'v': /* CITYPE immediate, li to compressed lui. */
1464 case 'o': /* CITYPE immediate, allow zero. */
1465 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1466 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1467 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1468 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1469 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1470 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1471 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1472 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1473 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1474 /* CLTYPE and CSTYPE have the same immediate encoding. */
1475 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1476 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1477 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1478 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1479 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1480 case 'F': /* Compressed funct for .insn directive. */
1481 switch (*++oparg)
1483 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1484 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1485 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1486 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1487 default:
1488 goto unknown_validate_operand;
1490 break;
1491 default:
1492 goto unknown_validate_operand;
1494 break; /* end RVC */
1495 case 'V': /* RVV */
1496 switch (*++oparg)
1498 case 'd':
1499 case 'f': USE_BITS (OP_MASK_VD, OP_SH_VD); break;
1500 case 'e': USE_BITS (OP_MASK_VWD, OP_SH_VWD); break;
1501 case 's': USE_BITS (OP_MASK_VS1, OP_SH_VS1); break;
1502 case 't': USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1503 case 'u': USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1504 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1505 case 'v': USE_BITS (OP_MASK_VD, OP_SH_VD);
1506 USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1507 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1508 case '0': break;
1509 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1510 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1511 case 'i':
1512 case 'j':
1513 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1514 case 'l': used_bits |= ENCODE_RVV_VI_UIMM6 (-1U); break;
1515 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1516 case 'M': break; /* Macro operand, must be a mask register. */
1517 case 'T': break; /* Macro operand, must be a vector register. */
1518 default:
1519 goto unknown_validate_operand;
1521 break; /* end RVV */
1522 case ',': break;
1523 case '(': break;
1524 case ')': break;
1525 case '{': break;
1526 case '}': break;
1527 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1528 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1529 case 'A': break; /* Macro operand, must be symbol. */
1530 case 'B': break; /* Macro operand, must be symbol or constant. */
1531 case 'c': break; /* Macro operand, must be symbol or constant. */
1532 case 'I': break; /* Macro operand, must be constant. */
1533 case 'D': /* RD, floating point. */
1534 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1535 case 'y': USE_BITS (OP_MASK_BS, OP_SH_BS); break;
1536 case 'Y': USE_BITS (OP_MASK_RNUM, OP_SH_RNUM); break;
1537 case 'Z': /* RS1, CSR number. */
1538 case 'S': /* RS1, floating point. */
1539 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1540 case 'U': /* RS1 and RS2 are the same, floating point. */
1541 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1542 /* Fall through. */
1543 case 'T': /* RS2, floating point. */
1544 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1545 case 'R': /* RS3, floating point. */
1546 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1547 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1548 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1549 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1550 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1551 case 'o': /* ITYPE immediate, load displacement. */
1552 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1553 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1554 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1555 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1556 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1557 case 'z': break; /* Zero immediate. */
1558 case '[': break; /* Unused operand. */
1559 case ']': break; /* Unused operand. */
1560 case '0': break; /* AMO displacement, must to zero. */
1561 case '1': break; /* Relaxation operand. */
1562 case 'F': /* Funct for .insn directive. */
1563 switch (*++oparg)
1565 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1566 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1567 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1568 default:
1569 goto unknown_validate_operand;
1571 break;
1572 case 'O': /* Opcode for .insn directive. */
1573 switch (*++oparg)
1575 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1576 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1577 default:
1578 goto unknown_validate_operand;
1580 break;
1581 case 'W': /* Various operands for standard z extensions. */
1582 switch (*++oparg)
1584 case 'i':
1585 switch (*++oparg)
1587 case 'f': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1588 default:
1589 goto unknown_validate_operand;
1591 break;
1592 case 'f':
1593 switch (*++oparg)
1595 case 'v': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1596 default:
1597 goto unknown_validate_operand;
1599 break;
1600 case 'c':
1601 switch (*++oparg)
1603 /* byte immediate operators, load/store byte insns. */
1604 case 'h': used_bits |= ENCODE_ZCB_HALFWORD_UIMM (-1U); break;
1605 /* halfword immediate operators, load/store halfword insns. */
1606 case 'b': used_bits |= ENCODE_ZCB_BYTE_UIMM (-1U); break;
1607 /* Immediate offset operand for cm.push and cm.pop. */
1608 case 'p': used_bits |= ENCODE_ZCMP_SPIMM (-1U); break;
1609 /* Register list operand for cm.push and cm.pop. */
1610 case 'r': USE_BITS (OP_MASK_REG_LIST, OP_SH_REG_LIST); break;
1611 case 'f': break;
1612 default:
1613 goto unknown_validate_operand;
1615 break;
1616 default:
1617 goto unknown_validate_operand;
1619 break;
1620 case 'X': /* Vendor-specific operands. */
1621 switch (*++oparg)
1623 case 't': /* Vendor-specific (T-head) operands. */
1625 size_t n;
1626 size_t s;
1627 switch (*++oparg)
1629 case 'V':
1630 switch (*++oparg)
1632 case 'c': /* Vtypei for th.vsetvli. */
1633 used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1634 default:
1635 goto unknown_validate_operand;
1637 break;
1638 case 'l': /* Integer immediate, literal. */
1639 oparg += strcspn(oparg, ",") - 1;
1640 break;
1641 case 's': /* Integer immediate, 'XtsN@S' ... N-bit signed immediate at bit S. */
1642 goto use_imm;
1643 case 'u': /* Integer immediate, 'XtuN@S' ... N-bit unsigned immediate at bit S. */
1644 goto use_imm;
1645 use_imm:
1646 n = strtol (oparg + 1, (char **)&oparg, 10);
1647 if (*oparg != '@')
1648 goto unknown_validate_operand;
1649 s = strtol (oparg + 1, (char **)&oparg, 10);
1650 oparg--;
1652 USE_IMM (n, s);
1653 break;
1654 default:
1655 goto unknown_validate_operand;
1658 break;
1659 case 'c': /* Vendor-specific (CORE-V) operands. */
1660 switch (*++oparg)
1662 case '2':
1663 /* ls2[4:0] */
1664 used_bits |= ENCODE_CV_IS2_UIMM5 (-1U);
1665 break;
1666 case '3':
1667 used_bits |= ENCODE_CV_IS3_UIMM5 (-1U);
1668 break;
1669 default:
1670 goto unknown_validate_operand;
1672 break;
1673 case 's': /* Vendor-specific (SiFive) operands. */
1674 switch (*++oparg)
1676 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1677 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1678 case 'O':
1679 switch (*++oparg)
1681 case '2': USE_BITS (OP_MASK_XSO2, OP_SH_XSO2); break;
1682 case '1': USE_BITS (OP_MASK_XSO1, OP_SH_XSO1); break;
1683 default:
1684 goto unknown_validate_operand;
1686 break;
1687 default:
1688 goto unknown_validate_operand;
1690 break;
1691 default:
1692 goto unknown_validate_operand;
1694 break;
1695 default:
1696 unknown_validate_operand:
1697 as_bad (_("internal: bad RISC-V opcode "
1698 "(unknown operand type `%s'): %s %s"),
1699 opargStart, opc->name, opc->args);
1700 return false;
1704 if (used_bits != required_bits)
1706 as_bad (_("internal: bad RISC-V opcode "
1707 "(bits %#llx undefined or invalid): %s %s"),
1708 (unsigned long long)(used_bits ^ required_bits),
1709 opc->name, opc->args);
1710 return false;
1712 return true;
1715 #undef USE_BITS
1717 struct percent_op_match
1719 const char *str;
1720 bfd_reloc_code_real_type reloc;
1723 /* Common hash table initialization function for instruction and .insn
1724 directive. */
1726 static htab_t
1727 init_opcode_hash (const struct riscv_opcode *opcodes,
1728 bool insn_directive_p)
1730 int i = 0;
1731 int length;
1732 htab_t hash = str_htab_create ();
1733 while (opcodes[i].name)
1735 const char *name = opcodes[i].name;
1736 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1737 as_fatal (_("internal: duplicate %s"), name);
1741 if (opcodes[i].pinfo != INSN_MACRO)
1743 if (insn_directive_p)
1744 length = ((name[0] == 'c') ? 2 : 4);
1745 else
1746 length = 0; /* Let assembler determine the length. */
1747 if (!validate_riscv_insn (&opcodes[i], length))
1748 as_fatal (_("internal: broken assembler. "
1749 "No assembly attempted"));
1751 else
1752 gas_assert (!insn_directive_p);
1753 ++i;
1755 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1758 return hash;
1761 /* Record all PC-relative high-part relocation that we have encountered to
1762 help us resolve the corresponding low-part relocation later. */
1763 typedef struct
1765 bfd_vma address;
1766 symbolS *symbol;
1767 bfd_vma target;
1768 } riscv_pcrel_hi_fixup;
1770 /* Handle of the pcrel_hi hash table. */
1771 static htab_t riscv_pcrel_hi_fixup_hash;
1773 /* Get the key of a entry from the pcrel_hi hash table. */
1775 static hashval_t
1776 riscv_pcrel_fixup_hash (const void *entry)
1778 const riscv_pcrel_hi_fixup *e = entry;
1779 return (hashval_t) (e->address);
1782 /* Compare the keys between two entries fo the pcrel_hi hash table. */
1784 static int
1785 riscv_pcrel_fixup_eq (const void *entry1, const void *entry2)
1787 const riscv_pcrel_hi_fixup *e1 = entry1, *e2 = entry2;
1788 return e1->address == e2->address;
1791 /* Record the pcrel_hi relocation. */
1793 static bool
1794 riscv_record_pcrel_fixup (htab_t p, bfd_vma address, symbolS *symbol,
1795 bfd_vma target)
1797 riscv_pcrel_hi_fixup entry = {address, symbol, target};
1798 riscv_pcrel_hi_fixup **slot =
1799 (riscv_pcrel_hi_fixup **) htab_find_slot (p, &entry, INSERT);
1800 if (slot == NULL)
1801 return false;
1803 *slot = (riscv_pcrel_hi_fixup *) xmalloc (sizeof (riscv_pcrel_hi_fixup));
1804 if (*slot == NULL)
1805 return false;
1806 **slot = entry;
1807 return true;
1810 /* This function is called once, at assembler startup time. It should set up
1811 all the tables, etc. that the MD part of the assembler will need. */
1813 void
1814 md_begin (void)
1816 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1818 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1819 as_warn (_("could not set architecture and machine"));
1821 op_hash = init_opcode_hash (riscv_opcodes, false);
1822 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1824 reg_names_hash = str_htab_create ();
1825 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1826 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1827 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1828 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1829 hash_reg_names (RCLASS_VECR, riscv_vecr_names_numeric, NVECR);
1830 hash_reg_names (RCLASS_VECM, riscv_vecm_names_numeric, NVECM);
1831 /* Add "fp" as an alias for "s0". */
1832 hash_reg_name (RCLASS_GPR, "fp", 8);
1834 /* Create and insert CSR hash tables. */
1835 csr_extra_hash = str_htab_create ();
1836 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1837 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1838 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1839 DECLARE_CSR(name, num, class, define_version, abort_version);
1840 #include "opcode/riscv-opc.h"
1841 #undef DECLARE_CSR
1843 opcode_names_hash = str_htab_create ();
1844 init_opcode_names_hash ();
1846 /* Create pcrel_hi hash table to resolve the relocation while with
1847 -mno-relax. */
1848 riscv_pcrel_hi_fixup_hash = htab_create (1024, riscv_pcrel_fixup_hash,
1849 riscv_pcrel_fixup_eq, free);
1851 /* Set the default alignment for the text section. */
1852 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1855 static insn_t
1856 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1858 switch (reloc_type)
1860 case BFD_RELOC_32:
1861 return value;
1863 case BFD_RELOC_RISCV_HI20:
1864 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1866 case BFD_RELOC_RISCV_LO12_S:
1867 return ENCODE_STYPE_IMM (value);
1869 case BFD_RELOC_RISCV_LO12_I:
1870 return ENCODE_ITYPE_IMM (value);
1872 default:
1873 abort ();
1877 /* Output an instruction. IP is the instruction information.
1878 ADDRESS_EXPR is an operand of the instruction to be used with
1879 RELOC_TYPE. */
1881 static void
1882 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1883 bfd_reloc_code_real_type reloc_type)
1885 dwarf2_emit_insn (0);
1887 if (reloc_type != BFD_RELOC_UNUSED)
1889 reloc_howto_type *howto;
1891 gas_assert (address_expr);
1892 if (reloc_type == BFD_RELOC_12_PCREL
1893 || reloc_type == BFD_RELOC_RISCV_JMP)
1895 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1896 int best_case = insn_length (ip);
1897 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1899 if (now_seg == absolute_section)
1901 as_bad (_("relaxable branches not supported in absolute section"));
1902 return;
1905 add_relaxed_insn (ip, worst_case, best_case,
1906 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1907 address_expr->X_add_symbol,
1908 address_expr->X_add_number);
1909 return;
1911 else
1913 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1914 if (howto == NULL)
1915 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1916 reloc_type);
1918 ip->fixp = fix_new_exp (ip->frag, ip->where,
1919 bfd_get_reloc_size (howto),
1920 address_expr, false, reloc_type);
1922 ip->fixp->fx_tcbit = riscv_opts.relax;
1923 ip->fixp->tc_fix_data.source_macro = source_macro;
1927 add_fixed_insn (ip);
1929 /* We need to start a new frag after any instruction that can be
1930 optimized away or compressed by the linker during relaxation, to prevent
1931 the assembler from computing static offsets across such an instruction.
1932 This is necessary to get correct EH info. */
1933 if (reloc_type == BFD_RELOC_RISCV_HI20
1934 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1935 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1936 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1938 frag_wane (frag_now);
1939 frag_new (0);
1943 /* Build an instruction created by a macro expansion. This is passed
1944 a pointer to the count of instructions created so far, an expression,
1945 the name of the instruction to build, an operand format string, and
1946 corresponding arguments. */
1948 static void
1949 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1951 const struct riscv_opcode *mo;
1952 struct riscv_cl_insn insn;
1953 bfd_reloc_code_real_type r;
1954 va_list args;
1955 const char *fmtStart;
1957 va_start (args, fmt);
1959 r = BFD_RELOC_UNUSED;
1960 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1961 gas_assert (mo);
1963 /* Find a non-RVC variant of the instruction. append_insn will compress
1964 it if possible. */
1965 while (riscv_insn_length (mo->match) < 4)
1966 mo++;
1967 gas_assert (strcmp (name, mo->name) == 0);
1969 create_insn (&insn, mo);
1970 for (;; ++fmt)
1972 fmtStart = fmt;
1973 switch (*fmt)
1975 case 'V': /* RVV */
1976 switch (*++fmt)
1978 case 'd':
1979 INSERT_OPERAND (VD, insn, va_arg (args, int));
1980 continue;
1981 case 's':
1982 INSERT_OPERAND (VS1, insn, va_arg (args, int));
1983 continue;
1984 case 't':
1985 INSERT_OPERAND (VS2, insn, va_arg (args, int));
1986 continue;
1987 case 'm':
1989 int reg = va_arg (args, int);
1990 if (reg == -1)
1992 INSERT_OPERAND (VMASK, insn, 1);
1993 continue;
1995 else if (reg == 0)
1997 INSERT_OPERAND (VMASK, insn, 0);
1998 continue;
2000 else
2001 goto unknown_macro_argument;
2003 default:
2004 goto unknown_macro_argument;
2006 break;
2008 case 'd':
2009 INSERT_OPERAND (RD, insn, va_arg (args, int));
2010 continue;
2011 case 's':
2012 INSERT_OPERAND (RS1, insn, va_arg (args, int));
2013 continue;
2014 case 't':
2015 INSERT_OPERAND (RS2, insn, va_arg (args, int));
2016 continue;
2018 case 'j':
2019 case 'u':
2020 case 'q':
2021 gas_assert (ep != NULL);
2022 r = va_arg (args, int);
2023 continue;
2025 case '\0':
2026 break;
2027 case ',':
2028 continue;
2029 default:
2030 unknown_macro_argument:
2031 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
2033 break;
2035 va_end (args);
2036 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2038 append_insn (&insn, ep, r);
2041 /* Build an instruction created by a macro expansion. Like md_assemble but
2042 accept a printf-style format string and arguments. */
2044 static void
2045 md_assemblef (const char *format, ...)
2047 char *buf = NULL;
2048 va_list ap;
2049 int r;
2051 va_start (ap, format);
2053 r = vasprintf (&buf, format, ap);
2055 if (r < 0)
2056 as_fatal (_("internal: vasprintf failed"));
2058 md_assemble (buf);
2059 free(buf);
2061 va_end (ap);
2064 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
2065 unset. */
2067 static void
2068 normalize_constant_expr (expressionS *ex)
2070 if (xlen > 32)
2071 return;
2072 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
2073 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
2074 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
2075 - 0x80000000);
2078 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
2079 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
2081 static void
2082 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
2083 bool maybe_csr)
2085 if (ex->X_op == O_big)
2086 as_bad (_("unsupported large constant"));
2087 else if (maybe_csr && ex->X_op == O_symbol)
2088 as_bad (_("unknown CSR `%s'"),
2089 S_GET_NAME (ex->X_add_symbol));
2090 else if (ex->X_op != O_constant)
2091 as_bad (_("instruction %s requires absolute expression"),
2092 ip->insn_mo->name);
2093 normalize_constant_expr (ex);
2096 static symbolS *
2097 make_internal_label (void)
2099 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
2100 frag_now_fix ());
2103 /* Load an entry from the GOT. */
2105 static void
2106 pcrel_access (int destreg, int tempreg, expressionS *ep,
2107 const char *lo_insn, const char *lo_pattern,
2108 bfd_reloc_code_real_type hi_reloc,
2109 bfd_reloc_code_real_type lo_reloc)
2111 expressionS ep2;
2112 ep2.X_op = O_symbol;
2113 ep2.X_add_symbol = make_internal_label ();
2114 ep2.X_add_number = 0;
2116 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
2117 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
2120 static void
2121 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
2122 bfd_reloc_code_real_type hi_reloc,
2123 bfd_reloc_code_real_type lo_reloc)
2125 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
2128 static void
2129 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
2130 bfd_reloc_code_real_type hi_reloc,
2131 bfd_reloc_code_real_type lo_reloc)
2133 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
2136 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
2138 static void
2139 riscv_call (int destreg, int tempreg, expressionS *ep,
2140 bfd_reloc_code_real_type reloc)
2142 /* Ensure the jalr is emitted to the same frag as the auipc. */
2143 frag_grow (8);
2144 macro_build (ep, "auipc", "d,u", tempreg, reloc);
2145 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
2146 /* See comment at end of append_insn. */
2147 frag_wane (frag_now);
2148 frag_new (0);
2151 /* Load an integer constant into a register. */
2153 static void
2154 load_const (int reg, expressionS *ep)
2156 int shift = RISCV_IMM_BITS;
2157 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
2158 expressionS upper = *ep, lower = *ep;
2159 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
2160 upper.X_add_number -= lower.X_add_number;
2162 if (ep->X_op != O_constant)
2164 as_bad (_("unsupported large constant"));
2165 return;
2168 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
2170 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
2171 while (((upper.X_add_number >> shift) & 1) == 0)
2172 shift++;
2174 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
2175 load_const (reg, &upper);
2177 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
2178 if (lower.X_add_number != 0)
2179 md_assemblef ("addi x%d, x%d, %" PRId64, reg, reg,
2180 (int64_t) lower.X_add_number);
2182 else
2184 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
2185 int hi_reg = 0;
2187 if (upper.X_add_number != 0)
2189 /* Discard low part and zero-extend upper immediate. */
2190 upper_imm = ((uint32_t)upper.X_add_number >> shift);
2192 md_assemblef ("lui x%d, 0x%" PRIx64, reg, (uint64_t) upper_imm);
2193 hi_reg = reg;
2196 if (lower.X_add_number != 0 || hi_reg == 0)
2197 md_assemblef ("%s x%d, x%d, %" PRId64, ADD32_INSN, reg, hi_reg,
2198 (int64_t) lower.X_add_number);
2202 /* Zero extend and sign extend byte/half-word/word. */
2204 static void
2205 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
2207 md_assemblef ("slli x%d, x%d, %#x", destreg, srcreg, shift);
2208 md_assemblef ("sr%ci x%d, x%d, %#x",
2209 sign ? 'a' : 'l', destreg, destreg, shift);
2212 /* Expand RISC-V Vector macros into one or more instructions. */
2214 static void
2215 vector_macro (struct riscv_cl_insn *ip)
2217 int vd = (ip->insn_opcode >> OP_SH_VD) & OP_MASK_VD;
2218 int vs1 = (ip->insn_opcode >> OP_SH_VS1) & OP_MASK_VS1;
2219 int vs2 = (ip->insn_opcode >> OP_SH_VS2) & OP_MASK_VS2;
2220 int vm = (ip->insn_opcode >> OP_SH_VMASK) & OP_MASK_VMASK;
2221 int vtemp = (ip->insn_opcode >> OP_SH_VFUNCT6) & OP_MASK_VFUNCT6;
2222 const char *vmslt_vx = ip->insn_mo->match ? "vmsltu.vx" : "vmslt.vx";
2223 int mask = ip->insn_mo->mask;
2225 switch (mask)
2227 case M_VMSGE:
2228 if (vm)
2230 /* Unmasked. */
2231 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vd, vs2, vs1, -1);
2232 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
2233 break;
2235 if (vtemp != 0)
2237 /* Masked. Have vtemp to avoid overlap constraints. */
2238 if (vd == vm)
2240 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vtemp, vs2, vs1, -1);
2241 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
2243 else
2245 /* Preserve the value of vd if not updating by vm. */
2246 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vtemp, vs2, vs1, -1);
2247 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
2248 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
2249 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
2252 else if (vd != vm)
2254 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2255 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vd, vs2, vs1, vm);
2256 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
2258 else
2259 as_bad (_("must provide temp if destination overlaps mask"));
2260 break;
2262 default:
2263 break;
2267 /* Expand RISC-V assembly macros into one or more instructions. */
2269 static void
2270 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
2271 bfd_reloc_code_real_type *imm_reloc)
2273 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
2274 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
2275 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
2276 int mask = ip->insn_mo->mask;
2278 source_macro = mask;
2280 switch (mask)
2282 case M_LI:
2283 load_const (rd, imm_expr);
2284 break;
2286 case M_LA:
2287 case M_LLA:
2288 case M_LGA:
2289 /* Load the address of a symbol into a register. */
2290 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
2291 as_bad (_("offset too large"));
2293 if (imm_expr->X_op == O_constant)
2294 load_const (rd, imm_expr);
2295 /* Global PIC symbol. */
2296 else if ((riscv_opts.pic && mask == M_LA)
2297 || mask == M_LGA)
2298 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2299 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2300 /* Local PIC symbol, or any non-PIC symbol. */
2301 else
2302 pcrel_load (rd, rd, imm_expr, "addi",
2303 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2304 break;
2306 case M_LA_TLS_GD:
2307 pcrel_load (rd, rd, imm_expr, "addi",
2308 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2309 break;
2311 case M_LA_TLS_IE:
2312 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2313 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2314 break;
2316 case M_Lx:
2317 pcrel_load (rd, rd, imm_expr, ip->insn_mo->name,
2318 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2319 break;
2321 case M_FLx:
2322 pcrel_load (rd, rs1, imm_expr, ip->insn_mo->name,
2323 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2324 break;
2326 case M_Sx_FSx:
2327 pcrel_store (rs2, rs1, imm_expr, ip->insn_mo->name,
2328 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2329 break;
2331 case M_CALL:
2332 riscv_call (rd, rs1, imm_expr, *imm_reloc);
2333 break;
2335 case M_EXTH:
2336 riscv_ext (rd, rs1, xlen - 16, *ip->insn_mo->name == 's');
2337 break;
2339 case M_ZEXTW:
2340 riscv_ext (rd, rs1, xlen - 32, false);
2341 break;
2343 case M_SEXTB:
2344 riscv_ext (rd, rs1, xlen - 8, true);
2345 break;
2347 case M_VMSGE:
2348 vector_macro (ip);
2349 break;
2351 default:
2352 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
2353 break;
2356 source_macro = -1;
2359 static const struct percent_op_match percent_op_utype[] =
2361 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
2362 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
2363 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
2364 {"tlsdesc_hi", BFD_RELOC_RISCV_TLSDESC_HI20},
2365 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
2366 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
2367 {"hi", BFD_RELOC_RISCV_HI20},
2368 {0, 0}
2371 static const struct percent_op_match percent_op_itype[] =
2373 {"lo", BFD_RELOC_RISCV_LO12_I},
2374 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
2375 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
2376 {"tlsdesc_load_lo", BFD_RELOC_RISCV_TLSDESC_LOAD_LO12},
2377 {"tlsdesc_add_lo", BFD_RELOC_RISCV_TLSDESC_ADD_LO12},
2378 {0, 0}
2381 static const struct percent_op_match percent_op_stype[] =
2383 {"lo", BFD_RELOC_RISCV_LO12_S},
2384 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
2385 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
2386 {0, 0}
2389 static const struct percent_op_match percent_op_relax_only[] =
2391 {"tlsdesc_call", BFD_RELOC_RISCV_TLSDESC_CALL},
2392 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
2393 {0, 0}
2396 static const struct percent_op_match percent_op_null[] =
2398 {0, 0}
2401 /* Return true if *STR points to a relocation operator. When returning true,
2402 move *STR over the operator and store its relocation code in *RELOC.
2403 Leave both *STR and *RELOC alone when returning false. */
2405 static bool
2406 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
2407 const struct percent_op_match *percent_op)
2409 for ( ; percent_op->str; percent_op++)
2410 if (strncasecmp (*str + 1, percent_op->str, strlen (percent_op->str)) == 0)
2412 size_t len = 1 + strlen (percent_op->str);
2414 while (ISSPACE ((*str)[len]))
2415 ++len;
2416 if ((*str)[len] != '(')
2417 continue;
2419 *str += len;
2420 *reloc = percent_op->reloc;
2422 /* Check whether the output BFD supports this relocation.
2423 If not, issue an error and fall back on something safe. */
2424 if (*reloc != BFD_RELOC_UNUSED
2425 && !bfd_reloc_type_lookup (stdoutput, *reloc))
2427 as_bad ("internal: relocation %s isn't supported by the "
2428 "current ABI", percent_op->str);
2429 *reloc = BFD_RELOC_UNUSED;
2431 return true;
2433 return false;
2436 static void
2437 my_getExpression (expressionS *ep, char *str)
2439 char *save_in;
2441 save_in = input_line_pointer;
2442 input_line_pointer = str;
2443 expression (ep);
2444 expr_parse_end = input_line_pointer;
2445 input_line_pointer = save_in;
2448 /* Parse string STR as a 16-bit relocatable operand. Store the
2449 expression in *EP and the relocation, if any, in RELOC.
2450 Return the number of relocation operators used (0 or 1).
2452 On exit, EXPR_PARSE_END points to the first character after the
2453 expression. */
2455 static size_t
2456 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2457 char *str, const struct percent_op_match *percent_op)
2459 size_t reloc_index;
2460 unsigned crux_depth, str_depth;
2461 bool orig_probing = probing_insn_operands;
2462 char *crux;
2464 /* Search for the start of the main expression.
2466 End the loop with CRUX pointing to the start of the main expression and
2467 with CRUX_DEPTH containing the number of open brackets at that point. */
2468 reloc_index = -1;
2469 str_depth = 0;
2472 reloc_index++;
2473 crux = str;
2474 crux_depth = str_depth;
2476 /* Skip over whitespace and brackets, keeping count of the number
2477 of brackets. */
2478 while (*str == ' ' || *str == '\t' || *str == '(')
2479 if (*str++ == '(')
2480 str_depth++;
2482 while (*str == '%'
2483 && reloc_index < 1
2484 && parse_relocation (&str, reloc, percent_op));
2486 if (*str == '%')
2488 /* expression() will choke on anything looking like an (unrecognized)
2489 relocation specifier. Don't even call it, avoiding multiple (and
2490 perhaps redundant) error messages; our caller will issue one. */
2491 ep->X_op = O_illegal;
2492 return 0;
2495 /* Anything inside parentheses or subject to a relocation operator cannot
2496 be a register and hence can be treated the same as operands to
2497 directives (other than .insn). */
2498 if (str_depth || reloc_index)
2499 probing_insn_operands = false;
2501 my_getExpression (ep, crux);
2502 str = expr_parse_end;
2504 probing_insn_operands = orig_probing;
2506 /* Match every open bracket. */
2507 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2508 if (*str++ == ')')
2509 crux_depth--;
2511 if (crux_depth > 0)
2512 as_bad ("unclosed '('");
2514 expr_parse_end = str;
2516 return reloc_index;
2519 /* Parse opcode name, could be an mnemonics or number. */
2521 static size_t
2522 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2523 char *str)
2525 const struct opcode_name_t *o = opcode_name_lookup (&str);
2527 if (o != NULL)
2529 ep->X_op = O_constant;
2530 ep->X_add_number = o->val;
2531 return 0;
2534 return my_getSmallExpression (ep, reloc, str, percent_op_null);
2537 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2538 On exit, EXPR_PARSE_END points to the first character after the
2539 expression. */
2541 static void
2542 my_getVsetvliExpression (expressionS *ep, char *str)
2544 unsigned int vsew_value = 0, vlmul_value = 0;
2545 unsigned int vta_value = 0, vma_value = 0;
2546 bfd_boolean vsew_found = FALSE, vlmul_found = FALSE;
2547 bfd_boolean vta_found = FALSE, vma_found = FALSE;
2549 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew), &vsew_value))
2551 if (*str == ',')
2552 ++str;
2553 if (vsew_found)
2554 as_bad (_("multiple vsew constants"));
2555 vsew_found = TRUE;
2557 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2559 if (*str == ',')
2560 ++str;
2561 if (vlmul_found)
2562 as_bad (_("multiple vlmul constants"));
2563 vlmul_found = TRUE;
2565 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2567 if (*str == ',')
2568 ++str;
2569 if (vta_found)
2570 as_bad (_("multiple vta constants"));
2571 vta_found = TRUE;
2573 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2575 if (*str == ',')
2576 ++str;
2577 if (vma_found)
2578 as_bad (_("multiple vma constants"));
2579 vma_found = TRUE;
2582 if (vsew_found || vlmul_found || vta_found || vma_found)
2584 ep->X_op = O_constant;
2585 ep->X_add_number = (vlmul_value << OP_SH_VLMUL)
2586 | (vsew_value << OP_SH_VSEW)
2587 | (vta_value << OP_SH_VTA)
2588 | (vma_value << OP_SH_VMA);
2589 expr_parse_end = str;
2591 else
2593 my_getExpression (ep, str);
2594 str = expr_parse_end;
2598 /* Parse string STR as a th.vsetvli operand. Store the expression in *EP.
2599 On exit, EXPR_PARSE_END points to the first character after the
2600 expression. */
2602 static void
2603 my_getThVsetvliExpression (expressionS *ep, char *str)
2605 unsigned int vsew_value = 0, vlen_value = 0, vediv_value = 0;
2606 bfd_boolean vsew_found = FALSE, vlen_found = FALSE, vediv_found = FALSE;
2608 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew),
2609 &vsew_value))
2611 if (*str == ',')
2612 ++str;
2613 if (vsew_found)
2614 as_bad (_("multiple vsew constants"));
2615 vsew_found = TRUE;
2618 if (arg_lookup (&str, riscv_th_vlen, ARRAY_SIZE (riscv_th_vlen),
2619 &vlen_value))
2621 if (*str == ',')
2622 ++str;
2623 if (vlen_found)
2624 as_bad (_("multiple vlen constants"));
2625 vlen_found = TRUE;
2627 if (arg_lookup (&str, riscv_th_vediv, ARRAY_SIZE (riscv_th_vediv),
2628 &vediv_value))
2630 if (*str == ',')
2631 ++str;
2632 if (vediv_found)
2633 as_bad (_("multiple vediv constants"));
2634 vediv_found = TRUE;
2637 if (vlen_found || vediv_found || vsew_found)
2639 ep->X_op = O_constant;
2640 ep->X_add_number
2641 = (vediv_value << 5) | (vsew_value << 2) | (vlen_value);
2642 expr_parse_end = str;
2644 else
2646 my_getExpression (ep, str);
2647 str = expr_parse_end;
2651 /* Detect and handle implicitly zero load-store offsets. For example,
2652 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2653 an implicit offset was detected. */
2655 static bool
2656 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
2658 /* Check whether there is only a single bracketed expression left.
2659 If so, it must be the base register and the constant must be zero. */
2660 if (*s == '(' && strchr (s + 1, '(') == 0)
2662 ep->X_op = O_constant;
2663 ep->X_add_number = 0;
2664 return true;
2667 return false;
2670 /* All RISC-V CSR instructions belong to one of these classes. */
2671 enum csr_insn_type
2673 INSN_NOT_CSR,
2674 INSN_CSRRW,
2675 INSN_CSRRS,
2676 INSN_CSRRC
2679 /* Return which CSR instruction is checking. */
2681 static enum csr_insn_type
2682 riscv_csr_insn_type (insn_t insn)
2684 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
2685 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
2686 return INSN_CSRRW;
2687 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2688 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2689 return INSN_CSRRS;
2690 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2691 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2692 return INSN_CSRRC;
2693 else
2694 return INSN_NOT_CSR;
2697 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2698 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2699 CSR address is 0x3. */
2701 static bool
2702 riscv_csr_read_only_check (insn_t insn)
2704 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
2705 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
2706 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
2707 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
2709 if (readonly
2710 && (((csr_insn == INSN_CSRRS
2711 || csr_insn == INSN_CSRRC)
2712 && rs1 != 0)
2713 || csr_insn == INSN_CSRRW))
2714 return false;
2716 return true;
2719 /* Return true if it is a privileged instruction. Otherwise, return false.
2721 uret is actually a N-ext instruction. So it is better to regard it as
2722 an user instruction rather than the priv instruction.
2724 hret is used to return from traps in H-mode. H-mode is removed since
2725 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2726 Therefore, hret should be controlled by the hypervisor spec rather than
2727 priv spec in the future.
2729 dret is defined in the debug spec, so it should be checked in the future,
2730 too. */
2732 static bool
2733 riscv_is_priv_insn (insn_t insn)
2735 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
2736 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
2737 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
2738 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
2739 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2740 check it here to keep the compatible. */
2741 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
2744 static symbolS *deferred_sym_rootP;
2745 static symbolS *deferred_sym_lastP;
2746 /* Since symbols can't easily be freed, try to recycle ones which weren't
2747 committed. */
2748 static symbolS *orphan_sym_rootP;
2749 static symbolS *orphan_sym_lastP;
2751 /* This routine assembles an instruction into its binary format. As a
2752 side effect, it sets the global variable imm_reloc to the type of
2753 relocation to do if one of the operands is an address expression. */
2755 static struct riscv_ip_error
2756 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
2757 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
2759 /* The operand string defined in the riscv_opcodes. */
2760 const char *oparg, *opargStart;
2761 /* The parsed operands from assembly. */
2762 char *asarg, *asargStart;
2763 char save_c = 0;
2764 struct riscv_opcode *insn;
2765 unsigned int regno;
2766 const struct percent_op_match *p;
2767 struct riscv_ip_error error;
2768 error.msg = "unrecognized opcode";
2769 error.statement = str;
2770 error.missing_ext = NULL;
2771 /* Indicate we are assembling instruction with CSR. */
2772 bool insn_with_csr = false;
2774 /* Parse the name of the instruction. Terminate the string if whitespace
2775 is found so that str_hash_find only sees the name part of the string. */
2776 for (asarg = str; *asarg!= '\0'; ++asarg)
2777 if (ISSPACE (*asarg))
2779 save_c = *asarg;
2780 *asarg++ = '\0';
2781 break;
2784 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2786 probing_insn_operands = true;
2788 asargStart = asarg;
2789 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2791 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2792 continue;
2794 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2796 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2797 insn->insn_class);
2798 continue;
2801 /* Reset error message of the previous round. */
2802 error.msg = _("illegal operands");
2803 error.missing_ext = NULL;
2805 /* Purge deferred symbols from the previous round, if any. */
2806 while (deferred_sym_rootP)
2808 symbolS *sym = deferred_sym_rootP;
2810 symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP);
2811 symbol_append (sym, orphan_sym_lastP, &orphan_sym_rootP,
2812 &orphan_sym_lastP);
2815 create_insn (ip, insn);
2817 imm_expr->X_op = O_absent;
2818 *imm_reloc = BFD_RELOC_UNUSED;
2819 p = percent_op_null;
2821 for (oparg = insn->args;; ++oparg)
2823 opargStart = oparg;
2824 asarg += strspn (asarg, " \t");
2825 switch (*oparg)
2827 case '\0': /* End of args. */
2828 if (insn->match_func && !insn->match_func (insn, ip->insn_opcode))
2829 break;
2831 if (insn->pinfo != INSN_MACRO)
2833 /* For .insn, insn->match and insn->mask are 0. */
2834 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2835 ? ip->insn_opcode
2836 : insn->match) == 2
2837 && !riscv_opts.rvc)
2838 break;
2840 if (riscv_is_priv_insn (ip->insn_opcode))
2841 explicit_priv_attr = true;
2843 /* Check if we write a read-only CSR by the CSR
2844 instruction. */
2845 if (insn_with_csr
2846 && riscv_opts.csr_check
2847 && !riscv_csr_read_only_check (ip->insn_opcode))
2849 /* Restore the character in advance, since we want to
2850 report the detailed warning message here. */
2851 if (save_c)
2852 *(asargStart - 1) = save_c;
2853 as_warn (_("read-only CSR is written `%s'"), str);
2854 insn_with_csr = false;
2857 /* The (segmant) load and store with EEW 64 cannot be used
2858 when zve32x is enabled. */
2859 if (ip->insn_mo->pinfo & INSN_V_EEW64
2860 && riscv_subset_supports (&riscv_rps_as, "zve32x")
2861 && !riscv_subset_supports (&riscv_rps_as, "zve64x"))
2863 error.msg = _("illegal opcode for zve32x");
2864 break;
2867 if (*asarg != '\0')
2868 break;
2870 /* Successful assembly. */
2871 error.msg = NULL;
2872 insn_with_csr = false;
2874 /* Commit deferred symbols, if any. */
2875 while (deferred_sym_rootP)
2877 symbolS *sym = deferred_sym_rootP;
2879 symbol_remove (sym, &deferred_sym_rootP,
2880 &deferred_sym_lastP);
2881 symbol_append (sym, symbol_lastP, &symbol_rootP,
2882 &symbol_lastP);
2883 symbol_table_insert (sym);
2885 goto out;
2887 case 'C': /* RVC */
2888 switch (*++oparg)
2890 case 's': /* RS1 x8-x15. */
2891 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2892 || !(regno >= 8 && regno <= 15))
2893 break;
2894 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2895 continue;
2896 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2897 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2898 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2899 break;
2900 continue;
2901 case 't': /* RS2 x8-x15. */
2902 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2903 || !(regno >= 8 && regno <= 15))
2904 break;
2905 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2906 continue;
2907 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2908 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2909 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2910 break;
2911 continue;
2912 case 'U': /* RS1, constrained to equal RD. */
2913 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2914 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2915 break;
2916 continue;
2917 case 'V': /* RS2 */
2918 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2919 break;
2920 INSERT_OPERAND (CRS2, *ip, regno);
2921 continue;
2922 case 'c': /* RS1, constrained to equal sp. */
2923 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2924 || regno != X_SP)
2925 break;
2926 continue;
2927 case 'z': /* RS2, constrained to equal x0. */
2928 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2929 || regno != 0)
2930 break;
2931 continue;
2932 case '>': /* Shift amount, 0 - (XLEN-1). */
2933 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2934 || imm_expr->X_op != O_constant
2935 || (unsigned long) imm_expr->X_add_number >= xlen)
2936 break;
2937 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2938 rvc_imm_done:
2939 asarg = expr_parse_end;
2940 imm_expr->X_op = O_absent;
2941 continue;
2942 case '5':
2943 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2944 || imm_expr->X_op != O_constant
2945 || imm_expr->X_add_number < 0
2946 || imm_expr->X_add_number >= 32
2947 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
2948 break;
2949 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2950 goto rvc_imm_done;
2951 case '6':
2952 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2953 || imm_expr->X_op != O_constant
2954 || imm_expr->X_add_number < 0
2955 || imm_expr->X_add_number >= 64
2956 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
2957 break;
2958 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2959 goto rvc_imm_done;
2960 case '8':
2961 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2962 || imm_expr->X_op != O_constant
2963 || imm_expr->X_add_number < 0
2964 || imm_expr->X_add_number >= 256
2965 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
2966 break;
2967 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2968 goto rvc_imm_done;
2969 case 'j':
2970 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2971 || imm_expr->X_op != O_constant
2972 || imm_expr->X_add_number == 0
2973 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2974 break;
2975 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2976 goto rvc_imm_done;
2977 case 'k':
2978 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2979 continue;
2980 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2981 || imm_expr->X_op != O_constant
2982 || !VALID_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
2983 break;
2984 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2985 goto rvc_imm_done;
2986 case 'l':
2987 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2988 continue;
2989 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2990 || imm_expr->X_op != O_constant
2991 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
2992 break;
2993 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2994 goto rvc_imm_done;
2995 case 'm':
2996 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2997 continue;
2998 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2999 || imm_expr->X_op != O_constant
3000 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
3001 break;
3002 ip->insn_opcode |=
3003 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
3004 goto rvc_imm_done;
3005 case 'n':
3006 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3007 continue;
3008 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3009 || imm_expr->X_op != O_constant
3010 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
3011 break;
3012 ip->insn_opcode |=
3013 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
3014 goto rvc_imm_done;
3015 case 'o':
3016 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3017 || imm_expr->X_op != O_constant
3018 /* C.addiw, c.li, and c.andi allow zero immediate.
3019 C.addi allows zero immediate as hint. Otherwise this
3020 is same as 'j'. */
3021 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
3022 break;
3023 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3024 goto rvc_imm_done;
3025 case 'K':
3026 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3027 || imm_expr->X_op != O_constant
3028 || imm_expr->X_add_number == 0
3029 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
3030 break;
3031 ip->insn_opcode |=
3032 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
3033 goto rvc_imm_done;
3034 case 'L':
3035 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3036 || imm_expr->X_op != O_constant
3037 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
3038 break;
3039 ip->insn_opcode |=
3040 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
3041 goto rvc_imm_done;
3042 case 'M':
3043 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3044 continue;
3045 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3046 || imm_expr->X_op != O_constant
3047 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
3048 break;
3049 ip->insn_opcode |=
3050 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
3051 goto rvc_imm_done;
3052 case 'N':
3053 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3054 continue;
3055 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3056 || imm_expr->X_op != O_constant
3057 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
3058 break;
3059 ip->insn_opcode |=
3060 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
3061 goto rvc_imm_done;
3062 case 'u':
3063 p = percent_op_utype;
3064 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3065 break;
3066 rvc_lui:
3067 if (imm_expr->X_op != O_constant
3068 || imm_expr->X_add_number <= 0
3069 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
3070 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
3071 && (imm_expr->X_add_number <
3072 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
3073 break;
3074 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3075 goto rvc_imm_done;
3076 case 'v':
3077 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3078 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
3079 || ((int32_t)imm_expr->X_add_number
3080 != imm_expr->X_add_number))
3081 break;
3082 imm_expr->X_add_number =
3083 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
3084 goto rvc_lui;
3085 case 'p':
3086 goto branch;
3087 case 'a':
3088 goto jump;
3089 case 'S': /* Floating-point RS1 x8-x15. */
3090 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
3091 || !(regno >= 8 && regno <= 15))
3092 break;
3093 INSERT_OPERAND (CRS1S, *ip, regno % 8);
3094 continue;
3095 case 'D': /* Floating-point RS2 x8-x15. */
3096 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
3097 || !(regno >= 8 && regno <= 15))
3098 break;
3099 INSERT_OPERAND (CRS2S, *ip, regno % 8);
3100 continue;
3101 case 'T': /* Floating-point RS2. */
3102 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
3103 break;
3104 INSERT_OPERAND (CRS2, *ip, regno);
3105 continue;
3106 case 'F':
3107 switch (*++oparg)
3109 case '6':
3110 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3111 || imm_expr->X_op != O_constant
3112 || imm_expr->X_add_number < 0
3113 || imm_expr->X_add_number >= 64)
3115 as_bad (_("bad value for compressed funct6 "
3116 "field, value must be 0...63"));
3117 break;
3119 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
3120 imm_expr->X_op = O_absent;
3121 asarg = expr_parse_end;
3122 continue;
3124 case '4':
3125 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3126 || imm_expr->X_op != O_constant
3127 || imm_expr->X_add_number < 0
3128 || imm_expr->X_add_number >= 16)
3130 as_bad (_("bad value for compressed funct4 "
3131 "field, value must be 0...15"));
3132 break;
3134 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
3135 imm_expr->X_op = O_absent;
3136 asarg = expr_parse_end;
3137 continue;
3139 case '3':
3140 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3141 || imm_expr->X_op != O_constant
3142 || imm_expr->X_add_number < 0
3143 || imm_expr->X_add_number >= 8)
3145 as_bad (_("bad value for compressed funct3 "
3146 "field, value must be 0...7"));
3147 break;
3149 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
3150 imm_expr->X_op = O_absent;
3151 asarg = expr_parse_end;
3152 continue;
3154 case '2':
3155 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3156 || imm_expr->X_op != O_constant
3157 || imm_expr->X_add_number < 0
3158 || imm_expr->X_add_number >= 4)
3160 as_bad (_("bad value for compressed funct2 "
3161 "field, value must be 0...3"));
3162 break;
3164 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
3165 imm_expr->X_op = O_absent;
3166 asarg = expr_parse_end;
3167 continue;
3169 default:
3170 goto unknown_riscv_ip_operand;
3172 break;
3174 default:
3175 goto unknown_riscv_ip_operand;
3177 break; /* end RVC */
3179 case 'V': /* RVV */
3180 switch (*++oparg)
3182 case 'd': /* VD */
3183 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3184 break;
3185 INSERT_OPERAND (VD, *ip, regno);
3186 continue;
3188 case 'e': /* AMO VD */
3189 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
3190 INSERT_OPERAND (VWD, *ip, 0);
3191 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
3193 INSERT_OPERAND (VWD, *ip, 1);
3194 INSERT_OPERAND (VD, *ip, regno);
3196 else
3197 break;
3198 continue;
3200 case 'f': /* AMO VS3 */
3201 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3202 break;
3203 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
3204 INSERT_OPERAND (VD, *ip, regno);
3205 else
3207 /* VS3 must match VD. */
3208 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
3209 break;
3211 continue;
3213 case 's': /* VS1 */
3214 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3215 break;
3216 INSERT_OPERAND (VS1, *ip, regno);
3217 continue;
3219 case 't': /* VS2 */
3220 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3221 break;
3222 INSERT_OPERAND (VS2, *ip, regno);
3223 continue;
3225 case 'u': /* VS1 == VS2 */
3226 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3227 break;
3228 INSERT_OPERAND (VS1, *ip, regno);
3229 INSERT_OPERAND (VS2, *ip, regno);
3230 continue;
3232 case 'v': /* VD == VS1 == VS2 */
3233 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3234 break;
3235 INSERT_OPERAND (VD, *ip, regno);
3236 INSERT_OPERAND (VS1, *ip, regno);
3237 INSERT_OPERAND (VS2, *ip, regno);
3238 continue;
3240 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
3241 and is used to choose vs1/rs1/frs1/imm or vs2 for
3242 v[f]merge. It use the same encoding as the vector mask
3243 register. */
3244 case '0':
3245 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
3246 continue;
3247 break;
3249 case 'b': /* vtypei for vsetivli */
3250 my_getVsetvliExpression (imm_expr, asarg);
3251 check_absolute_expr (ip, imm_expr, FALSE);
3252 if (!VALID_RVV_VB_IMM (imm_expr->X_add_number))
3253 as_bad (_("bad value for vsetivli immediate field, "
3254 "value must be 0..1023"));
3255 ip->insn_opcode
3256 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
3257 imm_expr->X_op = O_absent;
3258 asarg = expr_parse_end;
3259 continue;
3261 case 'c': /* vtypei for vsetvli */
3262 my_getVsetvliExpression (imm_expr, asarg);
3263 check_absolute_expr (ip, imm_expr, FALSE);
3264 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
3265 as_bad (_("bad value for vsetvli immediate field, "
3266 "value must be 0..2047"));
3267 ip->insn_opcode
3268 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3269 imm_expr->X_op = O_absent;
3270 asarg = expr_parse_end;
3271 continue;
3273 case 'i': /* vector arith signed immediate */
3274 my_getExpression (imm_expr, asarg);
3275 check_absolute_expr (ip, imm_expr, FALSE);
3276 if (imm_expr->X_add_number > 15
3277 || imm_expr->X_add_number < -16)
3278 as_bad (_("bad value for vector immediate field, "
3279 "value must be -16...15"));
3280 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3281 imm_expr->X_op = O_absent;
3282 asarg = expr_parse_end;
3283 continue;
3285 case 'j': /* vector arith unsigned immediate */
3286 my_getExpression (imm_expr, asarg);
3287 check_absolute_expr (ip, imm_expr, FALSE);
3288 if (imm_expr->X_add_number < 0
3289 || imm_expr->X_add_number >= 32)
3290 as_bad (_("bad value for vector immediate field, "
3291 "value must be 0...31"));
3292 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3293 imm_expr->X_op = O_absent;
3294 asarg = expr_parse_end;
3295 continue;
3297 case 'k': /* vector arith signed immediate, minus 1 */
3298 my_getExpression (imm_expr, asarg);
3299 check_absolute_expr (ip, imm_expr, FALSE);
3300 if (imm_expr->X_add_number > 16
3301 || imm_expr->X_add_number < -15)
3302 as_bad (_("bad value for vector immediate field, "
3303 "value must be -15...16"));
3304 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number - 1);
3305 imm_expr->X_op = O_absent;
3306 asarg = expr_parse_end;
3307 continue;
3309 case 'l': /* 6-bit vector arith unsigned immediate */
3310 my_getExpression (imm_expr, asarg);
3311 check_absolute_expr (ip, imm_expr, FALSE);
3312 if (imm_expr->X_add_number < 0
3313 || imm_expr->X_add_number >= 64)
3314 as_bad (_("bad value for vector immediate field, "
3315 "value must be 0...63"));
3316 ip->insn_opcode |= ENCODE_RVV_VI_UIMM6 (imm_expr->X_add_number);
3317 imm_expr->X_op = O_absent;
3318 asarg = expr_parse_end;
3319 continue;
3321 case 'm': /* optional vector mask */
3322 if (*asarg == '\0')
3324 INSERT_OPERAND (VMASK, *ip, 1);
3325 continue;
3327 else if (*asarg == ',' && asarg++
3328 && reg_lookup (&asarg, RCLASS_VECM, &regno)
3329 && regno == 0)
3331 INSERT_OPERAND (VMASK, *ip, 0);
3332 continue;
3334 break;
3336 case 'M': /* required vector mask */
3337 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
3339 INSERT_OPERAND (VMASK, *ip, 0);
3340 continue;
3342 break;
3344 case 'T': /* vector macro temporary register */
3345 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
3346 break;
3347 /* Store it in the FUNCT6 field as we don't have anyplace
3348 else to store it. */
3349 INSERT_OPERAND (VFUNCT6, *ip, regno);
3350 continue;
3352 default:
3353 goto unknown_riscv_ip_operand;
3355 break; /* end RVV */
3357 case ',':
3358 if (*asarg++ == *oparg)
3359 continue;
3360 asarg--;
3361 break;
3363 case '(':
3364 case ')':
3365 case '[':
3366 case ']':
3367 case '{':
3368 case '}':
3369 if (*asarg++ == *oparg)
3370 continue;
3371 break;
3373 case '<': /* Shift amount, 0 - 31. */
3374 my_getExpression (imm_expr, asarg);
3375 check_absolute_expr (ip, imm_expr, false);
3376 if ((unsigned long) imm_expr->X_add_number > 31)
3377 as_bad (_("improper shift amount (%"PRIu64")"),
3378 imm_expr->X_add_number);
3379 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
3380 imm_expr->X_op = O_absent;
3381 asarg = expr_parse_end;
3382 continue;
3384 case '>': /* Shift amount, 0 - (XLEN-1). */
3385 my_getExpression (imm_expr, asarg);
3386 check_absolute_expr (ip, imm_expr, false);
3387 if ((unsigned long) imm_expr->X_add_number >= xlen)
3388 as_bad (_("improper shift amount (%"PRIu64")"),
3389 imm_expr->X_add_number);
3390 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
3391 imm_expr->X_op = O_absent;
3392 asarg = expr_parse_end;
3393 continue;
3395 case 'Z': /* CSRRxI immediate. */
3396 my_getExpression (imm_expr, asarg);
3397 check_absolute_expr (ip, imm_expr, false);
3398 if ((unsigned long) imm_expr->X_add_number > 31)
3399 as_bad (_("improper CSRxI immediate (%"PRIu64")"),
3400 imm_expr->X_add_number);
3401 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
3402 imm_expr->X_op = O_absent;
3403 asarg = expr_parse_end;
3404 continue;
3406 case 'E': /* Control register. */
3407 insn_with_csr = true;
3408 explicit_priv_attr = true;
3409 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
3410 INSERT_OPERAND (CSR, *ip, regno);
3411 else
3413 my_getExpression (imm_expr, asarg);
3414 check_absolute_expr (ip, imm_expr, true);
3415 if ((unsigned long) imm_expr->X_add_number > 0xfff)
3416 as_bad (_("improper CSR address (%"PRIu64")"),
3417 imm_expr->X_add_number);
3418 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
3419 imm_expr->X_op = O_absent;
3420 asarg = expr_parse_end;
3422 continue;
3424 case 'm': /* Rounding mode. */
3425 if (arg_lookup (&asarg, riscv_rm,
3426 ARRAY_SIZE (riscv_rm), &regno))
3428 INSERT_OPERAND (RM, *ip, regno);
3429 continue;
3431 break;
3433 case 'P':
3434 case 'Q': /* Fence predecessor/successor. */
3435 if (arg_lookup (&asarg, riscv_pred_succ,
3436 ARRAY_SIZE (riscv_pred_succ), &regno))
3438 if (*oparg == 'P')
3439 INSERT_OPERAND (PRED, *ip, regno);
3440 else
3441 INSERT_OPERAND (SUCC, *ip, regno);
3442 continue;
3444 break;
3446 case 'd': /* Destination register. */
3447 case 's': /* Source register. */
3448 case 't': /* Target register. */
3449 case 'r': /* RS3 */
3450 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
3452 char c = *oparg;
3453 if (*asarg == ' ')
3454 ++asarg;
3456 /* Now that we have assembled one operand, we use the args
3457 string to figure out where it goes in the instruction. */
3458 switch (c)
3460 case 's':
3461 INSERT_OPERAND (RS1, *ip, regno);
3462 break;
3463 case 'd':
3464 INSERT_OPERAND (RD, *ip, regno);
3465 break;
3466 case 't':
3467 INSERT_OPERAND (RS2, *ip, regno);
3468 break;
3469 case 'r':
3470 INSERT_OPERAND (RS3, *ip, regno);
3471 break;
3473 continue;
3475 break;
3477 case 'D': /* Floating point RD. */
3478 case 'S': /* Floating point RS1. */
3479 case 'T': /* Floating point RS2. */
3480 case 'U': /* Floating point RS1 and RS2. */
3481 case 'R': /* Floating point RS3. */
3482 if (reg_lookup (&asarg,
3483 (riscv_subset_supports (&riscv_rps_as, "zfinx")
3484 ? RCLASS_GPR : RCLASS_FPR), &regno))
3486 char c = *oparg;
3487 if (*asarg == ' ')
3488 ++asarg;
3489 switch (c)
3491 case 'D':
3492 INSERT_OPERAND (RD, *ip, regno);
3493 break;
3494 case 'S':
3495 INSERT_OPERAND (RS1, *ip, regno);
3496 break;
3497 case 'U':
3498 INSERT_OPERAND (RS1, *ip, regno);
3499 /* Fall through. */
3500 case 'T':
3501 INSERT_OPERAND (RS2, *ip, regno);
3502 break;
3503 case 'R':
3504 INSERT_OPERAND (RS3, *ip, regno);
3505 break;
3507 continue;
3509 break;
3511 case 'I':
3512 my_getExpression (imm_expr, asarg);
3513 if (imm_expr->X_op != O_big
3514 && imm_expr->X_op != O_constant)
3515 break;
3516 normalize_constant_expr (imm_expr);
3517 asarg = expr_parse_end;
3518 continue;
3520 case 'A':
3521 my_getExpression (imm_expr, asarg);
3522 normalize_constant_expr (imm_expr);
3523 /* The 'A' format specifier must be a symbol. */
3524 if (imm_expr->X_op != O_symbol)
3525 break;
3526 *imm_reloc = BFD_RELOC_32;
3527 asarg = expr_parse_end;
3528 continue;
3530 case 'B':
3531 my_getExpression (imm_expr, asarg);
3532 normalize_constant_expr (imm_expr);
3533 /* The 'B' format specifier must be a symbol or a constant. */
3534 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
3535 break;
3536 if (imm_expr->X_op == O_symbol)
3537 *imm_reloc = BFD_RELOC_32;
3538 asarg = expr_parse_end;
3539 continue;
3541 case 'j': /* Sign-extended immediate. */
3542 p = percent_op_itype;
3543 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3544 goto alu_op;
3545 case 'q': /* Store displacement. */
3546 p = percent_op_stype;
3547 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3548 goto load_store;
3549 case 'o': /* Load displacement. */
3550 p = percent_op_itype;
3551 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3552 goto load_store;
3553 case '1':
3554 /* This is used for TLS relocations that acts as relaxation
3555 markers and do not change the instruction encoding,
3556 i.e. %tprel_add and %tlsdesc_call. */
3557 p = percent_op_relax_only;
3558 goto alu_op;
3559 case '0': /* AMO displacement, which must be zero. */
3560 load_store:
3561 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3562 continue;
3563 alu_op:
3564 /* If this value won't fit into a 16 bit offset, then go
3565 find a macro that will generate the 32 bit offset
3566 code pattern. */
3567 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3569 normalize_constant_expr (imm_expr);
3570 if (imm_expr->X_op != O_constant
3571 || (*oparg == '0' && imm_expr->X_add_number != 0)
3572 || (*oparg == '1')
3573 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3574 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3575 break;
3577 asarg = expr_parse_end;
3578 continue;
3580 case 'p': /* PC-relative offset. */
3581 branch:
3582 *imm_reloc = BFD_RELOC_12_PCREL;
3583 my_getExpression (imm_expr, asarg);
3584 asarg = expr_parse_end;
3585 continue;
3587 case 'u': /* Upper 20 bits. */
3588 p = percent_op_utype;
3589 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3591 if (imm_expr->X_op != O_constant)
3592 break;
3594 if (imm_expr->X_add_number < 0
3595 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
3596 as_bad (_("lui expression not in range 0..1048575"));
3598 *imm_reloc = BFD_RELOC_RISCV_HI20;
3599 imm_expr->X_add_number <<= RISCV_IMM_BITS;
3601 asarg = expr_parse_end;
3602 continue;
3604 case 'a': /* 20-bit PC-relative offset. */
3605 jump:
3606 my_getExpression (imm_expr, asarg);
3607 asarg = expr_parse_end;
3608 *imm_reloc = BFD_RELOC_RISCV_JMP;
3609 continue;
3611 case 'c':
3612 my_getExpression (imm_expr, asarg);
3613 asarg = expr_parse_end;
3614 if (strcmp (asarg, "@plt") == 0)
3615 asarg += 4;
3616 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3617 continue;
3619 case 'O':
3620 switch (*++oparg)
3622 case '4':
3623 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3624 || imm_expr->X_op != O_constant
3625 || imm_expr->X_add_number < 0
3626 || imm_expr->X_add_number >= 128
3627 || (imm_expr->X_add_number & 0x3) != 3)
3629 as_bad (_("bad value for opcode field, "
3630 "value must be 0...127 and "
3631 "lower 2 bits must be 0x3"));
3632 break;
3634 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3635 imm_expr->X_op = O_absent;
3636 asarg = expr_parse_end;
3637 continue;
3639 case '2':
3640 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3641 || imm_expr->X_op != O_constant
3642 || imm_expr->X_add_number < 0
3643 || imm_expr->X_add_number >= 3)
3645 as_bad (_("bad value for opcode field, "
3646 "value must be 0...2"));
3647 break;
3649 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3650 imm_expr->X_op = O_absent;
3651 asarg = expr_parse_end;
3652 continue;
3654 default:
3655 goto unknown_riscv_ip_operand;
3657 break;
3659 case 'F':
3660 switch (*++oparg)
3662 case '7':
3663 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3664 || imm_expr->X_op != O_constant
3665 || imm_expr->X_add_number < 0
3666 || imm_expr->X_add_number >= 128)
3668 as_bad (_("bad value for funct7 field, "
3669 "value must be 0...127"));
3670 break;
3672 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3673 imm_expr->X_op = O_absent;
3674 asarg = expr_parse_end;
3675 continue;
3677 case '3':
3678 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3679 || imm_expr->X_op != O_constant
3680 || imm_expr->X_add_number < 0
3681 || imm_expr->X_add_number >= 8)
3683 as_bad (_("bad value for funct3 field, "
3684 "value must be 0...7"));
3685 break;
3687 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3688 imm_expr->X_op = O_absent;
3689 asarg = expr_parse_end;
3690 continue;
3692 case '2':
3693 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3694 || imm_expr->X_op != O_constant
3695 || imm_expr->X_add_number < 0
3696 || imm_expr->X_add_number >= 4)
3698 as_bad (_("bad value for funct2 field, "
3699 "value must be 0...3"));
3700 break;
3702 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3703 imm_expr->X_op = O_absent;
3704 asarg = expr_parse_end;
3705 continue;
3707 default:
3708 goto unknown_riscv_ip_operand;
3710 break;
3712 case 'y': /* bs immediate */
3713 my_getExpression (imm_expr, asarg);
3714 check_absolute_expr (ip, imm_expr, FALSE);
3715 if ((unsigned long)imm_expr->X_add_number > 3)
3716 as_bad(_("Improper bs immediate (%lu)"),
3717 (unsigned long)imm_expr->X_add_number);
3718 INSERT_OPERAND(BS, *ip, imm_expr->X_add_number);
3719 imm_expr->X_op = O_absent;
3720 asarg = expr_parse_end;
3721 continue;
3723 case 'Y': /* rnum immediate */
3724 my_getExpression (imm_expr, asarg);
3725 check_absolute_expr (ip, imm_expr, FALSE);
3726 if ((unsigned long)imm_expr->X_add_number > 10)
3727 as_bad(_("Improper rnum immediate (%lu)"),
3728 (unsigned long)imm_expr->X_add_number);
3729 INSERT_OPERAND(RNUM, *ip, imm_expr->X_add_number);
3730 imm_expr->X_op = O_absent;
3731 asarg = expr_parse_end;
3732 continue;
3734 case 'z':
3735 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3736 || imm_expr->X_op != O_constant
3737 || imm_expr->X_add_number != 0)
3738 break;
3739 asarg = expr_parse_end;
3740 imm_expr->X_op = O_absent;
3741 continue;
3743 case 'W': /* Various operands for standard z extensions. */
3744 switch (*++oparg)
3746 case 'i':
3747 switch (*++oparg)
3749 case 'f':
3750 /* Prefetch offset for 'Zicbop' extension.
3751 pseudo S-type but lower 5-bits zero. */
3752 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3753 continue;
3754 my_getExpression (imm_expr, asarg);
3755 check_absolute_expr (ip, imm_expr, false);
3756 if (((unsigned) (imm_expr->X_add_number) & 0x1fU)
3757 || imm_expr->X_add_number >= RISCV_IMM_REACH / 2
3758 || imm_expr->X_add_number < -RISCV_IMM_REACH / 2)
3759 as_bad (_ ("improper prefetch offset (%ld)"),
3760 (long) imm_expr->X_add_number);
3761 ip->insn_opcode |= ENCODE_STYPE_IMM (
3762 (unsigned) (imm_expr->X_add_number) & ~0x1fU);
3763 imm_expr->X_op = O_absent;
3764 asarg = expr_parse_end;
3765 continue;
3766 default:
3767 goto unknown_riscv_ip_operand;
3769 break;
3771 case 'f':
3772 switch (*++oparg)
3774 case 'v':
3775 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3776 if (!arg_lookup (&asarg, riscv_fli_symval,
3777 ARRAY_SIZE (riscv_fli_symval), &regno))
3779 /* 0.0 is not a valid entry in riscv_fli_numval. */
3780 errno = 0;
3781 float f = strtof (asarg, &asarg);
3782 if (errno != 0 || f == 0.0
3783 || !flt_lookup (f, riscv_fli_numval,
3784 ARRAY_SIZE(riscv_fli_numval),
3785 &regno))
3787 as_bad (_("bad fli constant operand, "
3788 "supported constants must be in "
3789 "decimal or hexadecimal floating-point "
3790 "literal form"));
3791 break;
3794 INSERT_OPERAND (RS1, *ip, regno);
3795 continue;
3796 default:
3797 goto unknown_riscv_ip_operand;
3799 break;
3801 case 'c':
3802 switch (*++oparg)
3804 case 'h': /* Immediate field for c.lh/c.lhu/c.sh. */
3805 /* Handle cases, such as c.sh rs2', (rs1'). */
3806 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3807 continue;
3808 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3809 || imm_expr->X_op != O_constant
3810 || !VALID_ZCB_HALFWORD_UIMM ((valueT) imm_expr->X_add_number))
3811 break;
3812 ip->insn_opcode |= ENCODE_ZCB_HALFWORD_UIMM (imm_expr->X_add_number);
3813 goto rvc_imm_done;
3814 case 'b': /* Immediate field for c.lbu/c.sb. */
3815 /* Handle cases, such as c.lbu rd', (rs1'). */
3816 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3817 continue;
3818 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3819 || imm_expr->X_op != O_constant
3820 || !VALID_ZCB_BYTE_UIMM ((valueT) imm_expr->X_add_number))
3821 break;
3822 ip->insn_opcode |= ENCODE_ZCB_BYTE_UIMM (imm_expr->X_add_number);
3823 goto rvc_imm_done;
3824 case 'r':
3825 if (!reglist_lookup (&asarg, &regno))
3826 break;
3827 INSERT_OPERAND (REG_LIST, *ip, regno);
3828 continue;
3829 case 'p':
3830 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3831 || imm_expr->X_op != O_constant)
3832 break;
3833 /* Convert stack adjustment of cm.push to a positive
3834 offset. */
3835 if (ip->insn_mo->match == MATCH_CM_PUSH)
3836 imm_expr->X_add_number *= -1;
3837 /* Subtract base stack adjustment and get spimm. */
3838 imm_expr->X_add_number -=
3839 riscv_get_sp_base (ip->insn_opcode, *riscv_rps_as.xlen);
3840 if (!VALID_ZCMP_SPIMM (imm_expr->X_add_number))
3841 break;
3842 ip->insn_opcode |=
3843 ENCODE_ZCMP_SPIMM (imm_expr->X_add_number);
3844 goto rvc_imm_done;
3845 case 'f': /* Operand for matching immediate 255. */
3846 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3847 || imm_expr->X_op != O_constant
3848 || imm_expr->X_add_number != 255)
3849 break;
3850 /* This operand is used for matching immediate 255, and
3851 we do not write anything to encoding by this operand. */
3852 asarg = expr_parse_end;
3853 imm_expr->X_op = O_absent;
3854 continue;
3855 default:
3856 goto unknown_riscv_ip_operand;
3858 break;
3860 default:
3861 goto unknown_riscv_ip_operand;
3863 break;
3865 case 'X': /* Vendor-specific operands. */
3866 switch (*++oparg)
3868 case 't': /* Vendor-specific (T-head) operands. */
3870 size_t n;
3871 size_t s;
3872 bool sign;
3873 switch (*++oparg)
3875 case 'V':
3876 /* Vtypei for th.vsetvli. */
3877 ++oparg;
3878 if (*oparg != 'c')
3879 goto unknown_riscv_ip_operand;
3881 my_getThVsetvliExpression (imm_expr, asarg);
3882 check_absolute_expr (ip, imm_expr, FALSE);
3883 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
3884 as_bad (_("bad value for th.vsetvli immediate field, "
3885 "value must be 0..2047"));
3886 ip->insn_opcode
3887 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3888 imm_expr->X_op = O_absent;
3889 asarg = expr_parse_end;
3890 continue;
3892 case 'l': /* Integer immediate, literal. */
3893 n = strcspn (++oparg, ",");
3894 if (strncmp (oparg, asarg, n))
3895 as_bad (_("unexpected literal (%s)"), asarg);
3896 oparg += n - 1;
3897 asarg += n;
3898 continue;
3899 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
3900 sign = true;
3901 goto parse_imm;
3902 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
3903 sign = false;
3904 goto parse_imm;
3905 parse_imm:
3906 n = strtol (oparg + 1, (char **)&oparg, 10);
3907 if (*oparg != '@')
3908 goto unknown_riscv_ip_operand;
3909 s = strtol (oparg + 1, (char **)&oparg, 10);
3910 oparg--;
3912 my_getExpression (imm_expr, asarg);
3913 check_absolute_expr (ip, imm_expr, false);
3914 if (!sign)
3916 if (!VALIDATE_U_IMM (imm_expr->X_add_number, n))
3917 as_bad (_("improper immediate value (%"PRIu64")"),
3918 imm_expr->X_add_number);
3920 else
3922 if (!VALIDATE_S_IMM (imm_expr->X_add_number, n))
3923 as_bad (_("improper immediate value (%"PRIi64")"),
3924 imm_expr->X_add_number);
3926 INSERT_IMM (n, s, *ip, imm_expr->X_add_number);
3927 imm_expr->X_op = O_absent;
3928 asarg = expr_parse_end;
3929 continue;
3930 default:
3931 goto unknown_riscv_ip_operand;
3934 break;
3936 case 'c': /* Vendor-specific (CORE-V) operands. */
3937 switch (*++oparg)
3939 case '2':
3940 my_getExpression (imm_expr, asarg);
3941 check_absolute_expr (ip, imm_expr, FALSE);
3942 asarg = expr_parse_end;
3943 if (imm_expr->X_add_number<0
3944 || imm_expr->X_add_number>31)
3945 break;
3946 ip->insn_opcode
3947 |= ENCODE_CV_IS2_UIMM5 (imm_expr->X_add_number);
3948 continue;
3949 case '3':
3950 my_getExpression (imm_expr, asarg);
3951 check_absolute_expr (ip, imm_expr, FALSE);
3952 asarg = expr_parse_end;
3953 if (imm_expr->X_add_number < 0
3954 || imm_expr->X_add_number > 31)
3955 break;
3956 ip->insn_opcode
3957 |= ENCODE_CV_IS3_UIMM5 (imm_expr->X_add_number);
3958 continue;
3959 default:
3960 goto unknown_riscv_ip_operand;
3962 break;
3964 case 's': /* Vendor-specific (SiFive) operands. */
3965 #define UIMM_BITFIELD_VAL(S, E) (1 << ((E) - (S) + 1))
3966 #define ENCODE_UIMM_BIT_FIELD(NAME, IP, EXPR, RELOC, ASARG, \
3967 START, END) \
3968 do \
3970 if (my_getOpcodeExpression (EXPR, RELOC, ASARG) \
3971 || EXPR->X_op != O_constant \
3972 || EXPR->X_add_number < 0 \
3973 || EXPR->X_add_number >= UIMM_BITFIELD_VAL (START, END)) \
3975 as_bad (_("bad value for <bit-%s-%s> " \
3976 "field, value must be 0...%d"), \
3977 #START, #END, UIMM_BITFIELD_VAL (START, END)); \
3978 break; \
3980 INSERT_OPERAND (NAME, *IP, EXPR->X_add_number); \
3981 EXPR->X_op = O_absent; \
3982 ASARG = expr_parse_end; \
3984 while (0);
3985 switch (*++oparg)
3987 case 'd': /* Xsd */
3988 ENCODE_UIMM_BIT_FIELD
3989 (RD, ip, imm_expr, imm_reloc, asarg, 7, 11);
3990 continue;
3991 case 't': /* Xst */
3992 ENCODE_UIMM_BIT_FIELD
3993 (RS2, ip, imm_expr, imm_reloc, asarg, 20, 24)
3994 continue;
3995 case 'O':
3996 switch (*++oparg)
3998 case '2': /* XsO2 */
3999 ENCODE_UIMM_BIT_FIELD
4000 (XSO2, ip, imm_expr, imm_reloc, asarg, 26, 27);
4001 continue;
4002 case '1': /* XsO1 */
4003 ENCODE_UIMM_BIT_FIELD
4004 (XSO1, ip, imm_expr, imm_reloc, asarg, 26, 26);
4005 continue;
4007 default:
4008 goto unknown_riscv_ip_operand;
4010 #undef UIMM_BITFIELD_VAL
4011 #undef ENCODE_UIMM_BIT_FIELD
4012 break;
4014 default:
4015 goto unknown_riscv_ip_operand;
4017 break;
4019 default:
4020 unknown_riscv_ip_operand:
4021 as_fatal (_("internal: unknown argument type `%s'"),
4022 opargStart);
4024 break;
4026 asarg = asargStart;
4027 insn_with_csr = false;
4030 out:
4031 /* Restore the character we might have clobbered above. */
4032 if (save_c)
4033 *(asargStart - 1) = save_c;
4035 probing_insn_operands = false;
4037 return error;
4040 /* Similar to riscv_ip, but assembles an instruction according to the
4041 hardcode values of .insn directive. */
4043 static const char *
4044 riscv_ip_hardcode (char *str,
4045 struct riscv_cl_insn *ip,
4046 expressionS *imm_expr,
4047 const char *error)
4049 struct riscv_opcode *insn;
4050 insn_t values[2] = {0, 0};
4051 unsigned int num = 0;
4053 input_line_pointer = str;
4056 expression (imm_expr);
4057 switch (imm_expr->X_op)
4059 case O_constant:
4060 values[num++] = (insn_t) imm_expr->X_add_number;
4061 break;
4062 case O_big:
4063 /* Extract lower 32-bits of a big number.
4064 Assume that generic_bignum_to_int32 work on such number. */
4065 values[num++] = (insn_t) generic_bignum_to_int32 ();
4066 break;
4067 default:
4068 /* The first value isn't constant, so it should be
4069 .insn <type> <operands>. We have been parsed it
4070 in the riscv_ip. */
4071 if (num == 0)
4072 return error;
4073 return _("values must be constant");
4076 while (*input_line_pointer++ == ',' && num < 2 && imm_expr->X_op != O_big);
4078 input_line_pointer--;
4079 if (*input_line_pointer != '\0')
4080 return _("unrecognized values");
4082 insn = XCNEW (struct riscv_opcode);
4083 insn->match = values[num - 1];
4084 create_insn (ip, insn);
4085 unsigned int bytes = riscv_insn_length (insn->match);
4087 if (num == 2 && values[0] != bytes)
4088 return _("value conflicts with instruction length");
4090 if (imm_expr->X_op == O_big)
4092 unsigned int llen = 0;
4093 for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
4094 lval != 0; llen++)
4095 lval >>= BITS_PER_CHAR;
4096 unsigned int repr_bytes
4097 = (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
4098 if (bytes < repr_bytes)
4099 return _("value conflicts with instruction length");
4100 for (num = 0; num < imm_expr->X_add_number - 1; ++num)
4101 number_to_chars_littleendian (
4102 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
4103 generic_bignum[num],
4104 CHARS_PER_LITTLENUM);
4105 if (llen != 0)
4106 number_to_chars_littleendian (
4107 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
4108 generic_bignum[num],
4109 llen);
4110 memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
4111 return NULL;
4114 if (bytes < sizeof(values[0]) && values[num - 1] >> (8 * bytes) != 0)
4115 return _("value conflicts with instruction length");
4117 return NULL;
4120 void
4121 md_assemble (char *str)
4123 struct riscv_cl_insn insn;
4124 expressionS imm_expr;
4125 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
4127 /* The architecture and privileged elf attributes should be set
4128 before assembling. */
4129 if (!start_assemble)
4131 start_assemble = true;
4133 riscv_set_abi_by_arch ();
4134 if (!riscv_set_default_priv_spec (NULL))
4135 return;
4138 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
4140 const struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
4141 &imm_reloc, op_hash);
4143 if (error.msg)
4145 if (error.missing_ext)
4146 as_bad ("%s `%s', extension `%s' required", error.msg,
4147 error.statement, error.missing_ext);
4148 else
4149 as_bad ("%s `%s'", error.msg, error.statement);
4150 return;
4153 if (insn.insn_mo->pinfo == INSN_MACRO)
4154 macro (&insn, &imm_expr, &imm_reloc);
4155 else
4156 append_insn (&insn, &imm_expr, imm_reloc);
4159 const char *
4160 md_atof (int type, char *litP, int *sizeP)
4162 return ieee_md_atof (type, litP, sizeP, target_big_endian);
4165 void
4166 md_number_to_chars (char *buf, valueT val, int n)
4168 if (target_big_endian)
4169 number_to_chars_bigendian (buf, val, n);
4170 else
4171 number_to_chars_littleendian (buf, val, n);
4174 const char *md_shortopts = "O::g::G:";
4176 enum options
4178 OPTION_MARCH = OPTION_MD_BASE,
4179 OPTION_PIC,
4180 OPTION_NO_PIC,
4181 OPTION_MABI,
4182 OPTION_RELAX,
4183 OPTION_NO_RELAX,
4184 OPTION_ARCH_ATTR,
4185 OPTION_NO_ARCH_ATTR,
4186 OPTION_CSR_CHECK,
4187 OPTION_NO_CSR_CHECK,
4188 OPTION_MISA_SPEC,
4189 OPTION_MPRIV_SPEC,
4190 OPTION_BIG_ENDIAN,
4191 OPTION_LITTLE_ENDIAN,
4192 OPTION_END_OF_ENUM
4195 struct option md_longopts[] =
4197 {"march", required_argument, NULL, OPTION_MARCH},
4198 {"fPIC", no_argument, NULL, OPTION_PIC},
4199 {"fpic", no_argument, NULL, OPTION_PIC},
4200 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
4201 {"mabi", required_argument, NULL, OPTION_MABI},
4202 {"mrelax", no_argument, NULL, OPTION_RELAX},
4203 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
4204 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
4205 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
4206 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
4207 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
4208 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
4209 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
4210 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
4211 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
4213 {NULL, no_argument, NULL, 0}
4215 size_t md_longopts_size = sizeof (md_longopts);
4218 md_parse_option (int c, const char *arg)
4220 switch (c)
4222 case OPTION_MARCH:
4223 /* List all avaiable extensions. */
4224 if (strcmp (arg, "help") == 0)
4226 riscv_print_extensions ();
4227 exit (EXIT_SUCCESS);
4229 default_arch_with_ext = arg;
4230 break;
4232 case OPTION_NO_PIC:
4233 riscv_opts.pic = false;
4234 break;
4236 case OPTION_PIC:
4237 riscv_opts.pic = true;
4238 break;
4240 case OPTION_MABI:
4241 if (strcmp (arg, "ilp32") == 0)
4242 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
4243 else if (strcmp (arg, "ilp32e") == 0)
4244 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
4245 else if (strcmp (arg, "ilp32f") == 0)
4246 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
4247 else if (strcmp (arg, "ilp32d") == 0)
4248 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
4249 else if (strcmp (arg, "ilp32q") == 0)
4250 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
4251 else if (strcmp (arg, "lp64") == 0)
4252 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
4253 else if (strcmp (arg, "lp64e") == 0)
4254 riscv_set_abi (64, FLOAT_ABI_SOFT, true);
4255 else if (strcmp (arg, "lp64f") == 0)
4256 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
4257 else if (strcmp (arg, "lp64d") == 0)
4258 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
4259 else if (strcmp (arg, "lp64q") == 0)
4260 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
4261 else
4262 return 0;
4263 explicit_mabi = true;
4264 break;
4266 case OPTION_RELAX:
4267 riscv_opts.relax = true;
4268 break;
4270 case OPTION_NO_RELAX:
4271 riscv_opts.relax = false;
4272 break;
4274 case OPTION_ARCH_ATTR:
4275 riscv_opts.arch_attr = true;
4276 break;
4278 case OPTION_NO_ARCH_ATTR:
4279 riscv_opts.arch_attr = false;
4280 break;
4282 case OPTION_CSR_CHECK:
4283 riscv_opts.csr_check = true;
4284 break;
4286 case OPTION_NO_CSR_CHECK:
4287 riscv_opts.csr_check = false;
4288 break;
4290 case OPTION_MISA_SPEC:
4291 return riscv_set_default_isa_spec (arg);
4293 case OPTION_MPRIV_SPEC:
4294 return riscv_set_default_priv_spec (arg);
4296 case OPTION_BIG_ENDIAN:
4297 target_big_endian = 1;
4298 break;
4300 case OPTION_LITTLE_ENDIAN:
4301 target_big_endian = 0;
4302 break;
4304 default:
4305 return 0;
4308 return 1;
4311 void
4312 riscv_after_parse_args (void)
4314 /* The --with-arch is optional for now, so we still need to set the xlen
4315 according to the default_arch, which is set by the --target. */
4316 if (xlen == 0)
4318 if (strcmp (default_arch, "riscv32") == 0)
4319 xlen = 32;
4320 else if (strcmp (default_arch, "riscv64") == 0)
4321 xlen = 64;
4322 else
4323 as_bad ("unknown default architecture `%s'", default_arch);
4326 /* Set default specs. */
4327 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
4328 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
4329 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
4330 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
4332 riscv_set_arch (default_arch_with_ext);
4334 /* If the CIE to be produced has not been overridden on the command line,
4335 then produce version 3 by default. This allows us to use the full
4336 range of registers in a .cfi_return_column directive. */
4337 if (flag_dwarf_cie_version == -1)
4338 flag_dwarf_cie_version = 3;
4341 bool riscv_parse_name (const char *name, struct expressionS *ep,
4342 enum expr_mode mode)
4344 unsigned int regno;
4345 symbolS *sym;
4347 if (!probing_insn_operands)
4348 return false;
4350 gas_assert (mode == expr_normal);
4352 regno = reg_lookup_internal (name, RCLASS_GPR);
4353 if (regno == (unsigned int)-1)
4354 return false;
4356 if (symbol_find (name) != NULL)
4357 return false;
4359 /* Create a symbol without adding it to the symbol table yet.
4360 Insertion will happen only once we commit to using the insn
4361 we're probing operands for. */
4362 for (sym = deferred_sym_rootP; sym; sym = symbol_next (sym))
4363 if (strcmp (name, S_GET_NAME (sym)) == 0)
4364 break;
4365 if (!sym)
4367 for (sym = orphan_sym_rootP; sym; sym = symbol_next (sym))
4368 if (strcmp (name, S_GET_NAME (sym)) == 0)
4370 symbol_remove (sym, &orphan_sym_rootP, &orphan_sym_lastP);
4371 break;
4373 if (!sym)
4374 sym = symbol_create (name, undefined_section,
4375 &zero_address_frag, 0);
4377 symbol_append (sym, deferred_sym_lastP, &deferred_sym_rootP,
4378 &deferred_sym_lastP);
4381 ep->X_op = O_symbol;
4382 ep->X_add_symbol = sym;
4383 ep->X_add_number = 0;
4385 return true;
4388 long
4389 md_pcrel_from (fixS *fixP)
4391 return fixP->fx_where + fixP->fx_frag->fr_address;
4394 /* Apply a fixup to the object file. */
4396 void
4397 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
4399 unsigned int subtype;
4400 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
4401 bool relaxable = false;
4402 offsetT loc;
4403 segT sub_segment;
4405 /* Remember value for tc_gen_reloc. */
4406 fixP->fx_addnumber = *valP;
4408 switch (fixP->fx_r_type)
4410 case BFD_RELOC_RISCV_HI20:
4411 case BFD_RELOC_RISCV_LO12_I:
4412 case BFD_RELOC_RISCV_LO12_S:
4413 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
4414 | bfd_getl32 (buf), buf);
4415 if (fixP->fx_addsy == NULL)
4416 fixP->fx_done = true;
4417 relaxable = true;
4418 break;
4420 case BFD_RELOC_RISCV_GOT_HI20:
4421 /* R_RISCV_GOT_HI20 and the following R_RISCV_LO12_I are relaxable
4422 only if it is created as a result of la or lga assembler macros. */
4423 if (fixP->tc_fix_data.source_macro == M_LA
4424 || fixP->tc_fix_data.source_macro == M_LGA)
4425 relaxable = true;
4426 break;
4428 case BFD_RELOC_RISCV_ADD8:
4429 case BFD_RELOC_RISCV_ADD16:
4430 case BFD_RELOC_RISCV_ADD32:
4431 case BFD_RELOC_RISCV_ADD64:
4432 case BFD_RELOC_RISCV_SUB6:
4433 case BFD_RELOC_RISCV_SUB8:
4434 case BFD_RELOC_RISCV_SUB16:
4435 case BFD_RELOC_RISCV_SUB32:
4436 case BFD_RELOC_RISCV_SUB64:
4437 case BFD_RELOC_RISCV_RELAX:
4438 /* cvt_frag_to_fill () has called output_leb128 (). */
4439 case BFD_RELOC_RISCV_SET_ULEB128:
4440 case BFD_RELOC_RISCV_SUB_ULEB128:
4441 break;
4443 case BFD_RELOC_RISCV_TPREL_HI20:
4444 case BFD_RELOC_RISCV_TPREL_LO12_I:
4445 case BFD_RELOC_RISCV_TPREL_LO12_S:
4446 case BFD_RELOC_RISCV_TPREL_ADD:
4447 case BFD_RELOC_RISCV_TLSDESC_HI20:
4448 relaxable = true;
4449 /* Fall through. */
4451 case BFD_RELOC_RISCV_TLS_GOT_HI20:
4452 case BFD_RELOC_RISCV_TLS_GD_HI20:
4453 case BFD_RELOC_RISCV_TLS_DTPREL32:
4454 case BFD_RELOC_RISCV_TLS_DTPREL64:
4455 if (fixP->fx_addsy != NULL)
4456 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4457 else
4458 as_bad_where (fixP->fx_file, fixP->fx_line,
4459 _("TLS relocation against a constant"));
4460 break;
4462 case BFD_RELOC_32:
4463 /* Use pc-relative relocation for FDE initial location.
4464 The symbol address in .eh_frame may be adjusted in
4465 _bfd_elf_discard_section_eh_frame, and the content of
4466 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4467 Therefore, we cannot insert a relocation whose addend symbol is
4468 in .eh_frame. Othrewise, the value may be adjusted twice. */
4469 if (fixP->fx_addsy && fixP->fx_subsy
4470 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
4471 && strcmp (sub_segment->name, ".eh_frame") == 0
4472 && S_GET_VALUE (fixP->fx_subsy)
4473 == fixP->fx_frag->fr_address + fixP->fx_where)
4475 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
4476 fixP->fx_subsy = NULL;
4477 break;
4479 /* Fall through. */
4480 case BFD_RELOC_64:
4481 case BFD_RELOC_16:
4482 case BFD_RELOC_8:
4483 case BFD_RELOC_RISCV_CFA:
4484 if (fixP->fx_addsy && fixP->fx_subsy)
4486 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4487 fixP->fx_next->fx_addsy = fixP->fx_subsy;
4488 fixP->fx_next->fx_subsy = NULL;
4489 fixP->fx_next->fx_offset = 0;
4490 fixP->fx_subsy = NULL;
4492 switch (fixP->fx_r_type)
4494 case BFD_RELOC_64:
4495 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
4496 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
4497 break;
4499 case BFD_RELOC_32:
4500 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
4501 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4502 break;
4504 case BFD_RELOC_16:
4505 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
4506 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4507 break;
4509 case BFD_RELOC_8:
4510 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
4511 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4512 break;
4514 case BFD_RELOC_RISCV_CFA:
4515 /* Load the byte to get the subtype. */
4516 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
4517 loc = fixP->fx_frag->fr_fix - (subtype & 7);
4518 switch (subtype)
4520 case DW_CFA_advance_loc1:
4521 fixP->fx_where = loc + 1;
4522 fixP->fx_next->fx_where = loc + 1;
4523 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
4524 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4525 break;
4527 case DW_CFA_advance_loc2:
4528 fixP->fx_size = 2;
4529 fixP->fx_next->fx_size = 2;
4530 fixP->fx_where = loc + 1;
4531 fixP->fx_next->fx_where = loc + 1;
4532 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
4533 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4534 break;
4536 case DW_CFA_advance_loc4:
4537 fixP->fx_size = 4;
4538 fixP->fx_next->fx_size = 4;
4539 fixP->fx_where = loc;
4540 fixP->fx_next->fx_where = loc;
4541 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
4542 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4543 break;
4545 default:
4546 if (subtype < 0x80 && (subtype & 0x40))
4548 /* DW_CFA_advance_loc */
4549 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
4550 fixP->fx_next->fx_frag = fixP->fx_frag;
4551 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
4552 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
4554 else
4555 as_fatal (_("internal: bad CFA value #%d"), subtype);
4556 break;
4558 break;
4560 default:
4561 /* This case is unreachable. */
4562 abort ();
4565 /* Fall through. */
4567 case BFD_RELOC_RVA:
4568 /* If we are deleting this reloc entry, we must fill in the
4569 value now. This can happen if we have a .word which is not
4570 resolved when it appears but is later defined. */
4571 if (fixP->fx_addsy == NULL)
4573 gas_assert (fixP->fx_size <= sizeof (valueT));
4574 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
4575 fixP->fx_done = 1;
4577 break;
4579 case BFD_RELOC_RISCV_JMP:
4580 if (fixP->fx_addsy)
4582 /* Fill in a tentative value to improve objdump readability. */
4583 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4584 bfd_vma delta = target - md_pcrel_from (fixP);
4585 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
4586 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4587 fixP->fx_done = 1;
4589 break;
4591 case BFD_RELOC_12_PCREL:
4592 if (fixP->fx_addsy)
4594 /* Fill in a tentative value to improve objdump readability. */
4595 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4596 bfd_vma delta = target - md_pcrel_from (fixP);
4597 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
4598 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4599 fixP->fx_done = 1;
4601 break;
4603 case BFD_RELOC_RISCV_RVC_BRANCH:
4604 if (fixP->fx_addsy)
4606 /* Fill in a tentative value to improve objdump readability. */
4607 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4608 bfd_vma delta = target - md_pcrel_from (fixP);
4609 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
4610 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4611 fixP->fx_done = 1;
4613 break;
4615 case BFD_RELOC_RISCV_RVC_JUMP:
4616 if (fixP->fx_addsy)
4618 /* Fill in a tentative value to improve objdump readability. */
4619 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4620 bfd_vma delta = target - md_pcrel_from (fixP);
4621 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
4622 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4623 fixP->fx_done = 1;
4625 break;
4627 case BFD_RELOC_RISCV_CALL:
4628 case BFD_RELOC_RISCV_CALL_PLT:
4629 case BFD_RELOC_RISCV_TLSDESC_LOAD_LO12:
4630 case BFD_RELOC_RISCV_TLSDESC_ADD_LO12:
4631 case BFD_RELOC_RISCV_TLSDESC_CALL:
4632 relaxable = true;
4633 break;
4635 case BFD_RELOC_RISCV_PCREL_HI20:
4636 /* Record and evaluate the pcrel_hi relocation with local symbol.
4637 Fill in a tentative value to improve objdump readability for -mrelax,
4638 and set fx_done for -mno-relax. */
4639 if (fixP->fx_addsy
4640 && S_IS_LOCAL (fixP->fx_addsy)
4641 && S_GET_SEGMENT (fixP->fx_addsy) == seg)
4643 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4644 bfd_vma value = target - md_pcrel_from (fixP);
4646 /* Record PCREL_HI20. */
4647 if (!riscv_record_pcrel_fixup (riscv_pcrel_hi_fixup_hash,
4648 md_pcrel_from (fixP),
4649 fixP->fx_addsy,
4650 target))
4651 as_warn (_("too many pcrel_hi"));
4653 bfd_putl32 (bfd_getl32 (buf)
4654 | ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)),
4655 buf);
4656 if (!riscv_opts.relax)
4657 fixP->fx_done = 1;
4659 relaxable = true;
4660 break;
4662 case BFD_RELOC_RISCV_PCREL_LO12_S:
4663 case BFD_RELOC_RISCV_PCREL_LO12_I:
4664 /* Resolve the pcrel_lo relocation with local symbol.
4665 Fill in a tentative value to improve objdump readability for -mrelax,
4666 and set fx_done for -mno-relax. */
4668 bfd_vma location_pcrel_hi = S_GET_VALUE (fixP->fx_addsy) + *valP;
4669 riscv_pcrel_hi_fixup search = {location_pcrel_hi, 0, 0};
4670 riscv_pcrel_hi_fixup *entry = htab_find (riscv_pcrel_hi_fixup_hash,
4671 &search);
4672 if (entry && entry->symbol
4673 && S_IS_LOCAL (entry->symbol)
4674 && S_GET_SEGMENT (entry->symbol) == seg)
4676 bfd_vma target = entry->target;
4677 bfd_vma value = target - entry->address;
4678 bfd_putl32 (bfd_getl32 (buf) | ENCODE_ITYPE_IMM (value), buf);
4679 /* Relaxations should never be enabled by `.option relax'. */
4680 if (!riscv_opts.relax)
4681 fixP->fx_done = 1;
4684 relaxable = true;
4685 break;
4687 case BFD_RELOC_RISCV_ALIGN:
4688 break;
4690 default:
4691 /* We ignore generic BFD relocations we don't know about. */
4692 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
4693 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
4696 if (fixP->fx_subsy != NULL)
4697 as_bad_subtract (fixP);
4699 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4700 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
4702 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4703 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
4704 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
4705 fixP->fx_next->fx_size = 0;
4709 /* Because the value of .cfi_remember_state may changed after relaxation,
4710 we insert a fix to relocate it again in link-time. */
4712 void
4713 riscv_pre_output_hook (void)
4715 const frchainS *frch;
4716 segT s;
4718 /* Save the current segment info. */
4719 segT seg = now_seg;
4720 subsegT subseg = now_subseg;
4722 for (s = stdoutput->sections; s; s = s->next)
4723 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
4725 fragS *frag;
4727 for (frag = frch->frch_root; frag; frag = frag->fr_next)
4729 if (frag->fr_type == rs_cfa)
4731 expressionS exp;
4732 expressionS *symval;
4734 symval = symbol_get_value_expression (frag->fr_symbol);
4735 exp.X_op = O_subtract;
4736 exp.X_add_symbol = symval->X_add_symbol;
4737 exp.X_add_number = 0;
4738 exp.X_op_symbol = symval->X_op_symbol;
4740 /* We must set the segment before creating a frag after all
4741 frag chains have been chained together. */
4742 subseg_set (s, frch->frch_subseg);
4744 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
4745 BFD_RELOC_RISCV_CFA);
4750 /* Restore the original segment info. */
4751 subseg_set (seg, subseg);
4754 /* Handle the .option pseudo-op. */
4756 static void
4757 s_riscv_option (int x ATTRIBUTE_UNUSED)
4759 char *name = input_line_pointer, ch;
4761 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4762 ++input_line_pointer;
4763 ch = *input_line_pointer;
4764 *input_line_pointer = '\0';
4766 if (strcmp (name, "rvc") == 0)
4768 riscv_update_subset (&riscv_rps_as, "+c");
4769 riscv_reset_subsets_list_arch_str ();
4770 riscv_set_rvc (true);
4772 else if (strcmp (name, "norvc") == 0)
4774 riscv_update_subset (&riscv_rps_as, "-c");
4775 riscv_reset_subsets_list_arch_str ();
4776 riscv_set_rvc (false);
4778 else if (strcmp (name, "pic") == 0)
4779 riscv_opts.pic = true;
4780 else if (strcmp (name, "nopic") == 0)
4781 riscv_opts.pic = false;
4782 else if (strcmp (name, "relax") == 0)
4783 riscv_opts.relax = true;
4784 else if (strcmp (name, "norelax") == 0)
4785 riscv_opts.relax = false;
4786 else if (strcmp (name, "csr-check") == 0)
4787 riscv_opts.csr_check = true;
4788 else if (strcmp (name, "no-csr-check") == 0)
4789 riscv_opts.csr_check = false;
4790 else if (strncmp (name, "arch,", 5) == 0)
4792 name += 5;
4793 if (ISSPACE (*name) && *name != '\0')
4794 name++;
4795 riscv_update_subset (&riscv_rps_as, name);
4796 riscv_reset_subsets_list_arch_str ();
4798 riscv_set_rvc (false);
4799 if (riscv_subset_supports (&riscv_rps_as, "c")
4800 || riscv_subset_supports (&riscv_rps_as, "zca"))
4801 riscv_set_rvc (true);
4803 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
4804 riscv_set_tso ();
4806 else if (strcmp (name, "push") == 0)
4808 struct riscv_option_stack *s;
4810 s = XNEW (struct riscv_option_stack);
4811 s->next = riscv_opts_stack;
4812 s->options = riscv_opts;
4813 s->subset_list = riscv_rps_as.subset_list;
4814 riscv_opts_stack = s;
4815 riscv_rps_as.subset_list = riscv_copy_subset_list (s->subset_list);
4817 else if (strcmp (name, "pop") == 0)
4819 struct riscv_option_stack *s;
4821 s = riscv_opts_stack;
4822 if (s == NULL)
4823 as_bad (_(".option pop with no .option push"));
4824 else
4826 riscv_subset_list_t *release_subsets = riscv_rps_as.subset_list;
4827 riscv_opts_stack = s->next;
4828 riscv_opts = s->options;
4829 riscv_rps_as.subset_list = s->subset_list;
4830 riscv_release_subset_list (release_subsets);
4831 free (s);
4834 else
4836 as_warn (_("unrecognized .option directive: %s"), name);
4838 *input_line_pointer = ch;
4839 demand_empty_rest_of_line ();
4842 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4843 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4844 use in DWARF debug information. */
4846 static void
4847 s_dtprel (int bytes)
4849 expressionS ex;
4850 char *p;
4852 expression (&ex);
4854 if (ex.X_op != O_symbol)
4856 as_bad (_("unsupported use of %s"), (bytes == 8
4857 ? ".dtpreldword"
4858 : ".dtprelword"));
4859 ignore_rest_of_line ();
4862 p = frag_more (bytes);
4863 md_number_to_chars (p, 0, bytes);
4864 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
4865 (bytes == 8
4866 ? BFD_RELOC_RISCV_TLS_DTPREL64
4867 : BFD_RELOC_RISCV_TLS_DTPREL32));
4869 demand_empty_rest_of_line ();
4872 static void
4873 riscv_make_nops (char *buf, bfd_vma bytes)
4875 bfd_vma i = 0;
4877 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4878 means we are not within a valid instruction sequence. It is thus safe
4879 to use a zero byte, even though that is not a valid instruction. */
4880 if (bytes % 2 == 1)
4881 buf[i++] = 0;
4883 /* Use at most one 2-byte NOP. */
4884 if ((bytes - i) % 4 == 2)
4886 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
4887 i += 2;
4890 /* Fill the remainder with 4-byte NOPs. */
4891 for ( ; i < bytes; i += 4)
4892 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
4895 /* Called from md_do_align. Used to create an alignment frag in a
4896 code section by emitting a worst-case NOP sequence that the linker
4897 will later relax to the correct number of NOPs. We can't compute
4898 the correct alignment now because of other linker relaxations. */
4900 bool
4901 riscv_frag_align_code (int n)
4903 bfd_vma bytes = (bfd_vma) 1 << n;
4904 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
4905 bfd_vma worst_case_bytes = bytes - insn_alignment;
4906 char *nops;
4907 expressionS ex;
4909 /* If we are moving to a smaller alignment than the instruction size, then no
4910 alignment is required. */
4911 if (bytes <= insn_alignment)
4912 return true;
4914 /* When not relaxing, riscv_handle_align handles code alignment. */
4915 if (!riscv_opts.relax)
4916 return false;
4918 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4919 rather than just use frag_more to handle an alignment here? So that we
4920 don't need to call riscv_mapping_state again later, and then only need
4921 to check frag->fr_type to see if it is frag_align_code. */
4922 nops = frag_more (worst_case_bytes);
4924 ex.X_op = O_constant;
4925 ex.X_add_number = worst_case_bytes;
4927 riscv_make_nops (nops, worst_case_bytes);
4929 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
4930 &ex, false, BFD_RELOC_RISCV_ALIGN);
4932 riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
4934 /* We need to start a new frag after the alignment which may be removed by
4935 the linker, to prevent the assembler from computing static offsets.
4936 This is necessary to get correct EH info. */
4937 frag_wane (frag_now);
4938 frag_new (0);
4940 return true;
4943 /* Implement HANDLE_ALIGN. */
4945 void
4946 riscv_handle_align (fragS *fragP)
4948 switch (fragP->fr_type)
4950 case rs_align_code:
4951 /* When relaxing, riscv_frag_align_code handles code alignment. */
4952 if (!riscv_opts.relax)
4954 bfd_signed_vma bytes = (fragP->fr_next->fr_address
4955 - fragP->fr_address - fragP->fr_fix);
4956 /* We have 4 byte uncompressed nops. */
4957 bfd_signed_vma size = 4;
4958 bfd_signed_vma excess = bytes % size;
4959 bfd_boolean odd_padding = (excess % 2 == 1);
4960 char *p = fragP->fr_literal + fragP->fr_fix;
4962 if (bytes <= 0)
4963 break;
4965 /* Insert zeros or compressed nops to get 4 byte alignment. */
4966 if (excess)
4968 if (odd_padding)
4969 riscv_add_odd_padding_symbol (fragP);
4970 riscv_make_nops (p, excess);
4971 fragP->fr_fix += excess;
4972 p += excess;
4975 /* The frag will be changed to `rs_fill` later. The function
4976 `write_contents` will try to fill the remaining spaces
4977 according to the patterns we give. In this case, we give
4978 a 4 byte uncompressed nop as the pattern, and set the size
4979 of the pattern into `fr_var`. The nop will be output to the
4980 file `fr_offset` times. However, `fr_offset` could be zero
4981 if we don't need to pad the boundary finally. */
4982 riscv_make_nops (p, size);
4983 fragP->fr_var = size;
4985 break;
4987 default:
4988 break;
4992 /* This usually called from frag_var. */
4994 void
4995 riscv_init_frag (fragS * fragP, int max_chars)
4997 /* Do not add mapping symbol to debug sections. */
4998 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
4999 return;
5001 switch (fragP->fr_type)
5003 case rs_fill:
5004 case rs_align:
5005 case rs_align_test:
5006 riscv_mapping_state (MAP_DATA, max_chars, false/* fr_align_code */);
5007 break;
5008 case rs_align_code:
5009 riscv_mapping_state (MAP_INSN, max_chars, true/* fr_align_code */);
5010 break;
5011 default:
5012 break;
5017 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
5019 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
5022 /* Translate internal representation of relocation info to BFD target
5023 format. */
5025 arelent *
5026 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
5028 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
5030 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5031 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5032 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5033 reloc->addend = fixp->fx_addnumber;
5035 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5036 if (reloc->howto == NULL)
5038 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
5039 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
5041 /* We don't have R_RISCV_8/16, but for this special case,
5042 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
5043 return reloc;
5046 as_bad_where (fixp->fx_file, fixp->fx_line,
5047 _("cannot represent %s relocation in object file"),
5048 bfd_get_reloc_code_name (fixp->fx_r_type));
5049 return NULL;
5052 return reloc;
5056 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
5058 if (RELAX_BRANCH_P (fragp->fr_subtype))
5060 offsetT old_var = fragp->fr_var;
5061 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
5062 return fragp->fr_var - old_var;
5065 return 0;
5068 /* Expand far branches to multi-instruction sequences. */
5070 static void
5071 md_convert_frag_branch (fragS *fragp)
5073 bfd_byte *buf;
5074 expressionS exp;
5075 fixS *fixp;
5076 insn_t insn;
5077 int rs1, reloc;
5079 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
5081 exp.X_op = O_symbol;
5082 exp.X_add_symbol = fragp->fr_symbol;
5083 exp.X_add_number = fragp->fr_offset;
5085 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
5087 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
5089 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
5091 case 8:
5092 case 4:
5093 /* Expand the RVC branch into a RISC-V one. */
5094 insn = bfd_getl16 (buf);
5095 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
5096 if ((insn & MASK_C_J) == MATCH_C_J)
5097 insn = MATCH_JAL;
5098 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
5099 insn = MATCH_JAL | (X_RA << OP_SH_RD);
5100 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
5101 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
5102 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
5103 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
5104 else
5105 abort ();
5106 bfd_putl32 (insn, buf);
5107 break;
5109 case 6:
5110 /* Invert the branch condition. Branch over the jump. */
5111 insn = bfd_getl16 (buf);
5112 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
5113 insn |= ENCODE_CBTYPE_IMM (6);
5114 bfd_putl16 (insn, buf);
5115 buf += 2;
5116 goto jump;
5118 case 2:
5119 /* Just keep the RVC branch. */
5120 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
5121 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
5122 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5123 2, &exp, false, reloc);
5124 buf += 2;
5125 goto done;
5127 default:
5128 abort ();
5132 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
5134 case 8:
5135 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
5137 /* Invert the branch condition. Branch over the jump. */
5138 insn = bfd_getl32 (buf);
5139 insn ^= MATCH_BEQ ^ MATCH_BNE;
5140 insn |= ENCODE_BTYPE_IMM (8);
5141 bfd_putl32 (insn, buf);
5142 buf += 4;
5144 jump:
5145 /* Jump to the target. */
5146 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5147 4, &exp, false, BFD_RELOC_RISCV_JMP);
5148 bfd_putl32 (MATCH_JAL, buf);
5149 buf += 4;
5150 break;
5152 case 4:
5153 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
5154 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
5155 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5156 4, &exp, false, reloc);
5157 buf += 4;
5158 break;
5160 default:
5161 abort ();
5164 done:
5165 fixp->fx_file = fragp->fr_file;
5166 fixp->fx_line = fragp->fr_line;
5168 gas_assert (buf == (bfd_byte *)fragp->fr_literal
5169 + fragp->fr_fix + fragp->fr_var);
5171 fragp->fr_fix += fragp->fr_var;
5174 /* Relax a machine dependent frag. This returns the amount by which
5175 the current size of the frag should change. */
5177 void
5178 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
5179 fragS *fragp)
5181 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
5182 md_convert_frag_branch (fragp);
5185 void
5186 md_show_usage (FILE *stream)
5188 fprintf (stream, _("\
5189 RISC-V options:\n\
5190 -fpic or -fPIC generate position-independent code\n\
5191 -fno-pic don't generate position-independent code (default)\n\
5192 -march=ISA set the RISC-V architecture\n\
5193 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
5194 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.10, 1.11, 1.12)\n\
5195 -mabi=ABI set the RISC-V ABI\n\
5196 -mrelax enable relax (default)\n\
5197 -mno-relax disable relax\n\
5198 -march-attr generate RISC-V arch attribute\n\
5199 -mno-arch-attr don't generate RISC-V arch attribute\n\
5200 -mcsr-check enable the csr ISA and privilege spec version checks\n\
5201 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
5202 -mbig-endian assemble for big-endian\n\
5203 -mlittle-endian assemble for little-endian\n\
5204 "));
5207 /* Standard calling conventions leave the CFA at SP on entry. */
5209 void
5210 riscv_cfi_frame_initial_instructions (void)
5212 cfi_add_CFA_def_cfa_register (X_SP);
5216 tc_riscv_regname_to_dw2regnum (char *regname)
5218 int reg;
5220 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
5221 return reg;
5223 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
5224 return reg + 32;
5226 if ((reg = reg_lookup_internal (regname, RCLASS_VECR)) >= 0)
5227 return reg + 96;
5229 /* CSRs are numbered 4096 -> 8191. */
5230 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
5231 return reg + 4096;
5233 as_bad (_("unknown register `%s'"), regname);
5234 return -1;
5237 void
5238 riscv_elf_final_processing (void)
5240 riscv_set_abi_by_arch ();
5241 riscv_release_subset_list (riscv_rps_as.subset_list);
5242 elf_elfheader (stdoutput)->e_flags |= elf_flags;
5245 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
5246 since these directives break relaxation when used with symbol deltas. */
5248 static void
5249 s_riscv_leb128 (int sign)
5251 expressionS exp;
5252 char *save_in = input_line_pointer;
5254 expression (&exp);
5255 if (sign && exp.X_op != O_constant)
5256 as_bad (_("non-constant .sleb128 is not supported"));
5257 else if (!sign && exp.X_op != O_constant && exp.X_op != O_subtract)
5258 as_bad (_(".uleb128 only supports constant or subtract expressions"));
5260 demand_empty_rest_of_line ();
5262 input_line_pointer = save_in;
5263 return s_leb128 (sign);
5266 /* Parse the .insn directive. There are three formats,
5267 Format 1: .insn <type> <operand1>, <operand2>, ...
5268 Format 2: .insn <length>, <value>
5269 Format 3: .insn <value>. */
5271 static void
5272 s_riscv_insn (int x ATTRIBUTE_UNUSED)
5274 char *str = input_line_pointer;
5275 struct riscv_cl_insn insn;
5276 expressionS imm_expr;
5277 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
5278 char save_c;
5280 while (!is_end_of_line[(unsigned char) *input_line_pointer])
5281 ++input_line_pointer;
5283 save_c = *input_line_pointer;
5284 *input_line_pointer = '\0';
5286 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
5288 struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
5289 &imm_reloc, insn_type_hash);
5290 if (error.msg)
5292 char *save_in = input_line_pointer;
5293 error.msg = riscv_ip_hardcode (str, &insn, &imm_expr, error.msg);
5294 input_line_pointer = save_in;
5297 if (error.msg)
5299 if (error.missing_ext)
5300 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
5301 error.missing_ext);
5302 else
5303 as_bad ("%s `%s'", error.msg, error.statement);
5305 else
5307 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
5308 append_insn (&insn, &imm_expr, imm_reloc);
5311 *input_line_pointer = save_c;
5312 demand_empty_rest_of_line ();
5315 /* Update architecture and privileged elf attributes. If we don't set
5316 them, then try to output the default ones. */
5318 static void
5319 riscv_write_out_attrs (void)
5321 const char *arch_str, *priv_str, *p;
5322 /* versions[0]: major version.
5323 versions[1]: minor version.
5324 versions[2]: revision version. */
5325 unsigned versions[3] = {0}, number = 0;
5326 unsigned int i;
5328 /* Re-write architecture elf attribute. */
5329 arch_str = riscv_rps_as.subset_list->arch_str;
5330 if (!bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str))
5331 as_fatal (_("error adding attribute: %s"),
5332 bfd_errmsg (bfd_get_error ()));
5334 /* For the file without any instruction, we don't set the default_priv_spec
5335 according to the privileged elf attributes since the md_assemble isn't
5336 called. */
5337 if (!start_assemble
5338 && !riscv_set_default_priv_spec (NULL))
5339 return;
5341 /* If we already have set privileged elf attributes, then no need to do
5342 anything. Otherwise, don't generate or update them when no CSR and
5343 privileged instructions are used. */
5344 if (!explicit_priv_attr)
5345 return;
5347 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
5348 p = priv_str;
5349 for (i = 0; *p; ++p)
5351 if (*p == '.' && i < 3)
5353 versions[i++] = number;
5354 number = 0;
5356 else if (ISDIGIT (*p))
5357 number = (number * 10) + (*p - '0');
5358 else
5360 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
5361 return;
5364 versions[i] = number;
5366 /* Re-write privileged elf attributes. */
5367 if (!bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec,
5368 versions[0])
5369 || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor,
5370 versions[1])
5371 || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision,
5372 versions[2]))
5373 as_fatal (_("error adding attribute: %s"),
5374 bfd_errmsg (bfd_get_error ()));
5377 /* Add the default contents for the .riscv.attributes section. */
5379 static void
5380 riscv_set_public_attributes (void)
5382 if (riscv_opts.arch_attr || explicit_attr)
5383 riscv_write_out_attrs ();
5386 /* Scan uleb128 subtraction expressions and insert fixups for them.
5387 e.g., .uleb128 .L1 - .L0
5388 Because relaxation may change the value of the subtraction, we
5389 must resolve them at link-time. */
5391 static void
5392 riscv_insert_uleb128_fixes (bfd *abfd ATTRIBUTE_UNUSED,
5393 asection *sec, void *xxx ATTRIBUTE_UNUSED)
5395 segment_info_type *seginfo = seg_info (sec);
5396 struct frag *fragP;
5398 subseg_set (sec, 0);
5400 for (fragP = seginfo->frchainP->frch_root;
5401 fragP; fragP = fragP->fr_next)
5403 expressionS *exp, *exp_dup;
5405 if (fragP->fr_type != rs_leb128 || fragP->fr_symbol == NULL)
5406 continue;
5408 exp = symbol_get_value_expression (fragP->fr_symbol);
5410 if (exp->X_op != O_subtract)
5411 continue;
5413 /* Only unsigned leb128 can be handled. */
5414 gas_assert (fragP->fr_subtype == 0);
5415 exp_dup = xmemdup (exp, sizeof (*exp), sizeof (*exp));
5416 exp_dup->X_op = O_symbol;
5417 exp_dup->X_op_symbol = NULL;
5419 /* Insert relocations to resolve the subtraction at link-time.
5420 Emit the SET relocation first in riscv. */
5421 exp_dup->X_add_symbol = exp->X_add_symbol;
5422 fix_new_exp (fragP, fragP->fr_fix, 0,
5423 exp_dup, 0, BFD_RELOC_RISCV_SET_ULEB128);
5424 exp_dup->X_add_symbol = exp->X_op_symbol;
5425 exp_dup->X_add_number = 0; /* Set addend of SUB_ULEB128 to zero. */
5426 fix_new_exp (fragP, fragP->fr_fix, 0,
5427 exp_dup, 0, BFD_RELOC_RISCV_SUB_ULEB128);
5428 free ((void *) exp_dup);
5432 /* Called after all assembly has been done. */
5434 void
5435 riscv_md_finish (void)
5437 riscv_set_public_attributes ();
5438 if (riscv_opts.relax)
5439 bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL);
5442 /* Called just before the assembler exits. */
5444 void
5445 riscv_md_end (void)
5447 htab_delete (riscv_pcrel_hi_fixup_hash);
5450 /* Adjust the symbol table. */
5452 void
5453 riscv_adjust_symtab (void)
5455 bfd_map_over_sections (stdoutput, riscv_check_mapping_symbols, (char *) 0);
5456 elf_adjust_symtab ();
5459 /* Given a symbolic attribute NAME, return the proper integer value.
5460 Returns -1 if the attribute is not known. */
5463 riscv_convert_symbolic_attribute (const char *name)
5465 static const struct
5467 const char *name;
5468 const int tag;
5470 attribute_table[] =
5472 /* When you modify this table you should
5473 also modify the list in doc/c-riscv.texi. */
5474 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
5475 T(arch),
5476 T(priv_spec),
5477 T(priv_spec_minor),
5478 T(priv_spec_revision),
5479 T(unaligned_access),
5480 T(stack_align),
5481 #undef T
5484 if (name == NULL)
5485 return -1;
5487 unsigned int i;
5488 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
5489 if (strcmp (name, attribute_table[i].name) == 0)
5490 return attribute_table[i].tag;
5492 return -1;
5495 /* Parse a .attribute directive. */
5497 static void
5498 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
5500 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
5501 unsigned old_xlen;
5502 obj_attribute *attr;
5504 explicit_attr = true;
5505 switch (tag)
5507 case Tag_RISCV_arch:
5508 old_xlen = xlen;
5509 attr = elf_known_obj_attributes_proc (stdoutput);
5510 if (!start_assemble)
5511 riscv_set_arch (attr[Tag_RISCV_arch].s);
5512 else
5513 as_fatal (_("architecture elf attributes must set before "
5514 "any instructions"));
5516 if (old_xlen != xlen)
5518 /* We must re-init bfd again if xlen is changed. */
5519 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
5520 bfd_find_target (riscv_target_format (), stdoutput);
5522 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
5523 as_warn (_("could not set architecture and machine"));
5525 break;
5527 case Tag_RISCV_priv_spec:
5528 case Tag_RISCV_priv_spec_minor:
5529 case Tag_RISCV_priv_spec_revision:
5530 if (start_assemble)
5531 as_fatal (_("privileged elf attributes must set before "
5532 "any instructions"));
5533 break;
5535 default:
5536 break;
5540 /* Mark symbol that it follows a variant CC convention. */
5542 static void
5543 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
5545 char *name;
5546 char c;
5547 symbolS *sym;
5548 asymbol *bfdsym;
5549 elf_symbol_type *elfsym;
5551 c = get_symbol_name (&name);
5552 if (!*name)
5553 as_bad (_("missing symbol name for .variant_cc directive"));
5554 sym = symbol_find_or_make (name);
5555 restore_line_pointer (c);
5556 demand_empty_rest_of_line ();
5558 bfdsym = symbol_get_bfdsym (sym);
5559 elfsym = elf_symbol_from (bfdsym);
5560 gas_assert (elfsym);
5561 elfsym->internal_elf_sym.st_other |= STO_RISCV_VARIANT_CC;
5564 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5565 This is needed so RISC-V specific st_other values can be independently
5566 specified for an IFUNC resolver (that is called by the dynamic linker)
5567 and the symbol it resolves (aliased to the resolver). In particular,
5568 if a function symbol has special st_other value set via directives,
5569 then attaching an IFUNC resolver to that symbol should not override
5570 the st_other setting. Requiring the directive on the IFUNC resolver
5571 symbol would be unexpected and problematic in C code, where the two
5572 symbols appear as two independent function declarations. */
5574 void
5575 riscv_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
5577 struct elf_obj_sy *srcelf = symbol_get_obj (src);
5578 struct elf_obj_sy *destelf = symbol_get_obj (dest);
5579 /* If size is unset, copy size from src. Because we don't track whether
5580 .size has been used, we can't differentiate .size dest, 0 from the case
5581 where dest's size is unset. */
5582 if (!destelf->size && S_GET_SIZE (dest) == 0)
5584 if (srcelf->size)
5586 destelf->size = XNEW (expressionS);
5587 *destelf->size = *srcelf->size;
5589 S_SET_SIZE (dest, S_GET_SIZE (src));
5593 /* RISC-V pseudo-ops table. */
5594 static const pseudo_typeS riscv_pseudo_table[] =
5596 {"option", s_riscv_option, 0},
5597 {"half", cons, 2},
5598 {"word", cons, 4},
5599 {"dword", cons, 8},
5600 {"dtprelword", s_dtprel, 4},
5601 {"dtpreldword", s_dtprel, 8},
5602 {"uleb128", s_riscv_leb128, 0},
5603 {"sleb128", s_riscv_leb128, 1},
5604 {"insn", s_riscv_insn, 0},
5605 {"attribute", s_riscv_attribute, 0},
5606 {"variant_cc", s_variant_cc, 0},
5607 {"float16", float_cons, 'h'},
5609 { NULL, NULL, 0 },
5612 void
5613 riscv_pop_insert (void)
5615 extern void pop_insert (const pseudo_typeS *);
5617 pop_insert (riscv_pseudo_table);