gdb/dap: allow some requests when the process is running
[binutils-gdb.git] / gas / config / tc-riscv.c
blob3fb7727c250e64a5663b6651a1f2da918e7fb93b
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_ZCMT, /* zcmt only */
75 CSR_CLASS_V, /* rvv only */
76 CSR_CLASS_DEBUG, /* debug CSR */
77 CSR_CLASS_H, /* hypervisor */
78 CSR_CLASS_H_32, /* hypervisor, rv32 only */
79 CSR_CLASS_SMAIA, /* Smaia */
80 CSR_CLASS_SMAIA_32, /* Smaia, rv32 only */
81 CSR_CLASS_SMAIA_OR_SMCSRIND, /* Smaia/Smcsrind */
82 CSR_CLASS_SMCSRIND, /* Smcsrind */
83 CSR_CLASS_SMCNTRPMF, /* Smcntrpmf */
84 CSR_CLASS_SMCNTRPMF_32, /* Smcntrpmf, rv32 only */
85 CSR_CLASS_SMRNMI, /* Smrnmi */
86 CSR_CLASS_SMSTATEEN, /* Smstateen only */
87 CSR_CLASS_SMSTATEEN_32, /* Smstateen RV32 only */
88 CSR_CLASS_SSAIA, /* Ssaia */
89 CSR_CLASS_SSAIA_AND_H, /* Ssaia with H */
90 CSR_CLASS_SSAIA_32, /* Ssaia, rv32 only */
91 CSR_CLASS_SSAIA_AND_H_32, /* Ssaia with H, rv32 only */
92 CSR_CLASS_SSAIA_OR_SSCSRIND, /* Ssaia/Smcsrind */
93 CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H, /* Ssaia/Smcsrind with H */
94 CSR_CLASS_SSCSRIND, /* Sscsrind */
95 CSR_CLASS_SSCSRIND_AND_H, /* Sscsrind with H */
96 CSR_CLASS_SSSTATEEN, /* S[ms]stateen only */
97 CSR_CLASS_SSSTATEEN_AND_H, /* S[ms]stateen only (with H) */
98 CSR_CLASS_SSSTATEEN_AND_H_32, /* S[ms]stateen RV32 only (with H) */
99 CSR_CLASS_SSCOFPMF, /* Sscofpmf only */
100 CSR_CLASS_SSCOFPMF_32, /* Sscofpmf RV32 only */
101 CSR_CLASS_SSTC, /* Sstc only */
102 CSR_CLASS_SSTC_AND_H, /* Sstc only (with H) */
103 CSR_CLASS_SSTC_32, /* Sstc RV32 only */
104 CSR_CLASS_SSTC_AND_H_32, /* Sstc RV32 only (with H) */
105 CSR_CLASS_XTHEADVECTOR, /* xtheadvector only */
108 /* This structure holds all restricted conditions for a CSR. */
109 struct riscv_csr_extra
111 /* Class to which this CSR belongs. Used to decide whether or
112 not this CSR is legal in the current -march context. */
113 enum riscv_csr_class csr_class;
115 /* CSR may have differnet numbers in the previous priv spec. */
116 unsigned address;
118 /* Record the CSR is defined/valid in which versions. */
119 enum riscv_spec_class define_version;
121 /* Record the CSR is aborted/invalid from which versions. If it isn't
122 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
123 enum riscv_spec_class abort_version;
125 /* The CSR may have more than one setting. */
126 struct riscv_csr_extra *next;
129 /* This structure contains information about errors that occur within the
130 riscv_ip function */
131 struct riscv_ip_error
133 /* General error message */
134 const char* msg;
136 /* Statement that caused the error */
137 char* statement;
139 /* Missing extension that needs to be enabled */
140 const char* missing_ext;
143 #ifndef DEFAULT_ARCH
144 #define DEFAULT_ARCH "riscv64"
145 #endif
147 #ifndef DEFAULT_RISCV_ATTR
148 #define DEFAULT_RISCV_ATTR 0
149 #endif
151 /* Let riscv_after_parse_args set the default value according to xlen. */
152 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
153 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
154 #endif
156 /* Need to sync the version with RISC-V compiler. */
157 #ifndef DEFAULT_RISCV_ISA_SPEC
158 #define DEFAULT_RISCV_ISA_SPEC "20191213"
159 #endif
161 #ifndef DEFAULT_RISCV_PRIV_SPEC
162 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
163 #endif
165 static const char default_arch[] = DEFAULT_ARCH;
166 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
167 static const char *file_arch_str = NULL;
168 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
169 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
171 static unsigned xlen = 0; /* The width of an x-register. */
172 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
173 static bool rve_abi = false;
174 enum float_abi
176 FLOAT_ABI_DEFAULT = -1,
177 FLOAT_ABI_SOFT,
178 FLOAT_ABI_SINGLE,
179 FLOAT_ABI_DOUBLE,
180 FLOAT_ABI_QUAD
182 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
184 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
185 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
187 static unsigned elf_flags = 0;
189 /* Indicate whether we are already assembling any instructions. */
190 static bool start_assemble = false;
192 static bool probing_insn_operands;
194 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
195 Otherwise, return 1. */
197 static int
198 riscv_set_default_isa_spec (const char *s)
200 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
201 RISCV_GET_ISA_SPEC_CLASS (s, class);
202 if (class == ISA_SPEC_CLASS_NONE)
204 as_bad ("unknown default ISA spec `%s' set by "
205 "-misa-spec or --with-isa-spec", s);
206 return 0;
208 else
209 default_isa_spec = class;
210 return 1;
213 /* Set the default_priv_spec. Find the privileged elf attributes when
214 the input string is NULL. Return 0 if the spec isn't supported.
215 Otherwise, return 1. */
217 static int
218 riscv_set_default_priv_spec (const char *s)
220 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
221 unsigned major, minor, revision;
222 obj_attribute *attr;
224 RISCV_GET_PRIV_SPEC_CLASS (s, class);
225 if (class != PRIV_SPEC_CLASS_NONE
226 && class != PRIV_SPEC_CLASS_1P9P1)
228 default_priv_spec = class;
229 return 1;
232 if (s != NULL)
234 as_bad (_("unknown default privileged spec `%s' set by "
235 "-mpriv-spec or --with-priv-spec"), s);
236 return 0;
239 /* Set the default_priv_spec by the privileged elf attributes. */
240 attr = elf_known_obj_attributes_proc (stdoutput);
241 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
242 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
243 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
244 /* Version 0.0.0 is the default value and meningless. */
245 if (major == 0 && minor == 0 && revision == 0)
246 return 1;
248 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
249 if (class != PRIV_SPEC_CLASS_NONE)
251 default_priv_spec = class;
252 return 1;
255 /* Still can not find the privileged spec class. */
256 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
257 "privileged elf attributes"), major, minor, revision);
258 return 0;
261 /* This is the set of options which the .option pseudo-op may modify. */
262 struct riscv_set_options
264 int pic; /* Generate position-independent code. */
265 int rvc; /* Generate RVC code. */
266 int relax; /* Emit relocs the linker is allowed to relax. */
267 int arch_attr; /* Emit architecture and privileged elf attributes. */
268 int csr_check; /* Enable the CSR checking. */
271 static struct riscv_set_options riscv_opts =
273 0, /* pic */
274 0, /* rvc */
275 1, /* relax */
276 DEFAULT_RISCV_ATTR, /* arch_attr */
277 0, /* csr_check */
280 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
281 for elf_flags once we have enabled c extension. */
283 static void
284 riscv_set_rvc (bool rvc_value)
286 if (rvc_value)
287 elf_flags |= EF_RISCV_RVC;
289 if (start_assemble && subseg_text_p (now_seg)
290 && riscv_opts.rvc && !rvc_value)
292 struct riscv_segment_info_type *info
293 = &seg_info(now_seg)->tc_segment_info_data;
295 info->last_insn16 = true;
296 info->rvc = rvc_value;
299 riscv_opts.rvc = rvc_value;
302 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
304 static void
305 riscv_set_tso (void)
307 elf_flags |= EF_RISCV_TSO;
310 /* The linked list hanging off of .subsets_list records all enabled extensions,
311 which are parsed from the architecture string. The architecture string can
312 be set by the -march option, the elf architecture attributes, and the
313 --with-arch configure option. */
314 static riscv_parse_subset_t riscv_rps_as =
316 NULL, /* subset_list, we will set it later once
317 riscv_opts_stack is created or updated. */
318 as_bad, /* error_handler. */
319 &xlen, /* xlen. */
320 &default_isa_spec, /* isa_spec. */
321 true, /* check_unknown_prefixed_ext. */
324 /* Update file/function-level architecture string according to the
325 subset_list. */
327 static void
328 riscv_set_arch_str (const char **arch_str_p)
330 riscv_subset_list_t *subsets = riscv_rps_as.subset_list;
331 const char *arch_str = *arch_str_p;
332 if (arch_str != NULL)
333 free ((void *) arch_str);
334 *arch_str_p = riscv_arch_str (xlen, subsets);
337 /* This structure is used to hold a stack of .option values. */
338 struct riscv_option_stack
340 struct riscv_option_stack *next;
341 struct riscv_set_options options;
342 riscv_subset_list_t *subset_list;
345 static struct riscv_option_stack *riscv_opts_stack = NULL;
347 /* Set which ISA and extensions are available. */
349 static void
350 riscv_set_arch (const char *s)
352 if (s != NULL && strcmp (s, "") == 0)
354 as_bad (_("the architecture string of -march and elf architecture "
355 "attributes cannot be empty"));
356 return;
359 if (riscv_rps_as.subset_list == NULL)
361 riscv_rps_as.subset_list = XNEW (riscv_subset_list_t);
362 riscv_rps_as.subset_list->head = NULL;
363 riscv_rps_as.subset_list->tail = NULL;
364 riscv_rps_as.subset_list->arch_str = NULL;
366 riscv_release_subset_list (riscv_rps_as.subset_list);
367 riscv_parse_subset (&riscv_rps_as, s);
368 riscv_set_arch_str (&file_arch_str);
369 riscv_set_arch_str (&riscv_rps_as.subset_list->arch_str);
371 riscv_set_rvc (riscv_subset_supports (&riscv_rps_as, "c")
372 || riscv_subset_supports (&riscv_rps_as, "zca"));
374 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
375 riscv_set_tso ();
378 /* Indicate -mabi option is explictly set. */
379 static bool explicit_mabi = false;
381 /* Set the abi information. */
383 static void
384 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
386 abi_xlen = new_xlen;
387 float_abi = new_float_abi;
388 rve_abi = rve;
391 /* If the -mabi option isn't set, then set the abi according to the
392 ISA string. Otherwise, check if there is any conflict. */
394 static void
395 riscv_set_abi_by_arch (void)
397 if (!explicit_mabi)
399 if (riscv_subset_supports (&riscv_rps_as, "q"))
400 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
401 else if (riscv_subset_supports (&riscv_rps_as, "d"))
402 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
403 else if (riscv_subset_supports (&riscv_rps_as, "e"))
404 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
405 else
406 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
408 else
410 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
411 if (abi_xlen > xlen)
412 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
413 else if (abi_xlen < xlen)
414 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
416 if (riscv_subset_supports (&riscv_rps_as, "e") && !rve_abi)
417 as_bad ("only ilp32e/lp64e ABI are supported for e extension");
419 if (float_abi == FLOAT_ABI_SINGLE
420 && !riscv_subset_supports (&riscv_rps_as, "f"))
421 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
422 "isn't supported");
423 else if (float_abi == FLOAT_ABI_DOUBLE
424 && !riscv_subset_supports (&riscv_rps_as, "d"))
425 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
426 "isn't supported");
427 else if (float_abi == FLOAT_ABI_QUAD
428 && !riscv_subset_supports (&riscv_rps_as, "q"))
429 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
430 "isn't supported");
433 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
434 elf_flags &= ~EF_RISCV_FLOAT_ABI;
435 elf_flags |= float_abi << 1;
437 if (rve_abi)
438 elf_flags |= EF_RISCV_RVE;
441 /* Handle of the OPCODE hash table. */
442 static htab_t op_hash = NULL;
444 /* Handle of the type of .insn hash table. */
445 static htab_t insn_type_hash = NULL;
447 /* This array holds the chars that always start a comment. If the
448 pre-processor is disabled, these aren't very useful. */
449 const char comment_chars[] = "#";
451 /* This array holds the chars that only start a comment at the beginning of
452 a line. If the line seems to have the form '# 123 filename'
453 .line and .file directives will appear in the pre-processed output
455 Note that input_file.c hand checks for '#' at the beginning of the
456 first line of the input file. This is because the compiler outputs
457 #NO_APP at the beginning of its output.
459 Also note that C style comments are always supported. */
460 const char line_comment_chars[] = "#";
462 /* This array holds machine specific line separator characters. */
463 const char line_separator_chars[] = ";";
465 /* Chars that can be used to separate mant from exp in floating point nums. */
466 const char EXP_CHARS[] = "eE";
468 /* Chars that mean this number is a floating point constant.
469 As in 0f12.456 or 0d1.2345e12. */
470 const char FLT_CHARS[] = "rRsSfFdDxXpPhH";
472 /* Indicate ELF attributes are explicitly set. */
473 static bool explicit_attr = false;
475 /* Indicate CSR or priv instructions are explicitly used. */
476 static bool explicit_priv_attr = false;
478 static char *expr_parse_end;
480 /* Macros for encoding relaxation state for RVC branches and far jumps. */
481 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
482 ((relax_substateT) \
483 (0xc0000000 \
484 | ((uncond) ? 1 : 0) \
485 | ((rvc) ? 2 : 0) \
486 | ((length) << 2)))
487 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
488 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
489 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
490 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
492 /* Is the given value a sign-extended 32-bit value? */
493 #define IS_SEXT_32BIT_NUM(x) \
494 (((x) &~ (offsetT) 0x7fffffff) == 0 \
495 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
497 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
498 #define IS_ZEXT_32BIT_NUM(x) \
499 (((x) &~ (offsetT) 0xffffffff) == 0 \
500 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
502 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
503 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
504 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
505 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
507 #define INSERT_IMM(n, s, INSN, VALUE) \
508 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
510 /* Determine if an instruction matches an opcode. */
511 #define OPCODE_MATCHES(OPCODE, OP) \
512 (((OPCODE) & MASK_##OP) == MATCH_##OP)
514 /* Create a new mapping symbol for the transition to STATE. */
516 static void
517 make_mapping_symbol (enum riscv_seg_mstate state,
518 valueT value,
519 fragS *frag,
520 const char *arch_str,
521 bool odd_data_padding)
523 const char *name;
524 char *buff = NULL;
525 switch (state)
527 case MAP_DATA:
528 name = "$d";
529 break;
530 case MAP_INSN:
531 if (arch_str != NULL)
533 size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */
534 buff = xmalloc (size);
535 snprintf (buff, size, "$x%s", arch_str);
536 name = buff;
538 else
539 name = "$x";
540 break;
541 default:
542 abort ();
545 symbolS *symbol = symbol_new (name, now_seg, frag, value);
546 symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL);
547 if (arch_str != NULL)
549 /* Store current $x+arch into tc_segment_info. */
550 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol;
551 xfree ((void *) buff);
554 /* If .fill or other data filling directive generates zero sized data,
555 then mapping symbol for the following code will have the same value.
557 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
558 and .text.zero.fill.last. */
559 symbolS *first = frag->tc_frag_data.first_map_symbol;
560 symbolS *last = frag->tc_frag_data.last_map_symbol;
561 symbolS *removed = NULL;
562 if (value == 0)
564 if (first != NULL)
566 know (S_GET_VALUE (first) == S_GET_VALUE (symbol)
567 && first == last);
568 /* Remove the old one. */
569 removed = first;
571 frag->tc_frag_data.first_map_symbol = symbol;
573 else if (last != NULL)
575 /* The mapping symbols should be added in offset order. */
576 know (S_GET_VALUE (last) <= S_GET_VALUE (symbol));
577 /* Remove the old one. */
578 if (S_GET_VALUE (last) == S_GET_VALUE (symbol))
579 removed = last;
581 frag->tc_frag_data.last_map_symbol = symbol;
583 if (odd_data_padding)
585 /* If the removed mapping symbol is $x+arch, then add it back to
586 the next $x. */
587 const char *str = removed != NULL
588 && strncmp (S_GET_NAME (removed), "$xrv", 4) == 0
589 ? S_GET_NAME (removed) + 2 : NULL;
590 make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str,
591 false/* odd_data_padding */);
594 if (removed != NULL)
595 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
598 /* Set the mapping state for frag_now. */
600 void
601 riscv_mapping_state (enum riscv_seg_mstate to_state,
602 int max_chars,
603 bool fr_align_code)
605 enum riscv_seg_mstate from_state =
606 seg_info (now_seg)->tc_segment_info_data.map_state;
607 bool reset_seg_arch_str = false;
609 if (!SEG_NORMAL (now_seg)
610 /* For now we only add the mapping symbols to text sections.
611 Therefore, the dis-assembler only show the actual contents
612 distribution for text. Other sections will be shown as
613 data without the details. */
614 || !subseg_text_p (now_seg))
615 return;
617 /* The mapping symbol should be emitted if not in the right
618 mapping state. */
619 symbolS *seg_arch_symbol =
620 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol;
621 if (to_state == MAP_INSN && seg_arch_symbol == 0)
623 /* Always add $x+arch at the first instruction of section. */
624 reset_seg_arch_str = true;
626 else if (seg_arch_symbol != 0
627 && to_state == MAP_INSN
628 && !fr_align_code
629 && strcmp (riscv_rps_as.subset_list->arch_str,
630 S_GET_NAME (seg_arch_symbol) + 2) != 0)
632 reset_seg_arch_str = true;
634 else if (from_state == to_state)
635 return;
637 valueT value = (valueT) (frag_now_fix () - max_chars);
638 seg_info (now_seg)->tc_segment_info_data.map_state = to_state;
639 seg_info (now_seg)->tc_segment_info_data.last_insn16 = false;
640 const char *arch_str = reset_seg_arch_str
641 ? riscv_rps_as.subset_list->arch_str : NULL;
642 make_mapping_symbol (to_state, value, frag_now, arch_str,
643 false/* odd_data_padding */);
646 /* Add the odd bytes of paddings for riscv_handle_align. */
648 static void
649 riscv_add_odd_padding_symbol (fragS *frag)
651 /* If there was already a mapping symbol, it should be
652 removed in the make_mapping_symbol.
654 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
655 make_mapping_symbol (MAP_DATA, frag->fr_fix, frag,
656 NULL/* arch_str */, true/* odd_data_padding */);
659 /* Remove any excess mapping symbols generated for alignment frags in
660 SEC. We may have created a mapping symbol before a zero byte
661 alignment; remove it if there's a mapping symbol after the
662 alignment. */
664 static void
665 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
666 asection *sec,
667 void *dummy ATTRIBUTE_UNUSED)
669 segment_info_type *seginfo = seg_info (sec);
670 fragS *fragp;
672 if (seginfo == NULL || seginfo->frchainP == NULL)
673 return;
675 for (fragp = seginfo->frchainP->frch_root;
676 fragp != NULL;
677 fragp = fragp->fr_next)
679 symbolS *last = fragp->tc_frag_data.last_map_symbol;
680 fragS *next = fragp->fr_next;
682 if (last == NULL || next == NULL)
683 continue;
685 /* Check the last mapping symbol if it is at the boundary of
686 fragment. */
687 if (S_GET_VALUE (last) < next->fr_address)
688 continue;
689 know (S_GET_VALUE (last) == next->fr_address);
693 symbolS *next_first = next->tc_frag_data.first_map_symbol;
694 if (next_first != NULL)
696 /* The last mapping symbol overlaps with another one
697 which at the start of the next frag.
699 Please see the gas/testsuite/gas/riscv/mapping.s:
700 .text.zero.fill.align.A and .text.zero.fill.align.B. */
701 know (S_GET_VALUE (last) == S_GET_VALUE (next_first));
702 symbolS *removed = last;
703 if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0
704 && strcmp (S_GET_NAME (next_first), "$x") == 0)
705 removed = next_first;
706 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
707 break;
710 if (next->fr_next == NULL)
712 /* The last mapping symbol is at the end of the section.
714 Please see the gas/testsuite/gas/riscv/mapping.s:
715 .text.last.section. */
716 know (next->fr_fix == 0 && next->fr_var == 0);
717 symbol_remove (last, &symbol_rootP, &symbol_lastP);
718 break;
721 /* Since we may have empty frags without any mapping symbols,
722 keep looking until the non-empty frag. */
723 if (next->fr_address != next->fr_next->fr_address)
724 break;
726 next = next->fr_next;
728 while (next != NULL);
732 /* The default target format to use. */
734 const char *
735 riscv_target_format (void)
737 if (target_big_endian)
738 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
739 else
740 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
743 /* Return the length of instruction INSN. */
745 static inline unsigned int
746 insn_length (const struct riscv_cl_insn *insn)
748 return riscv_insn_length (insn->insn_opcode);
751 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
753 static void
754 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
756 insn->insn_mo = mo;
757 insn->insn_opcode = mo->match;
758 insn->insn_long_opcode[0] = 0;
759 insn->frag = NULL;
760 insn->where = 0;
761 insn->fixp = NULL;
764 /* Install INSN at the location specified by its "frag" and "where" fields. */
766 static void
767 install_insn (const struct riscv_cl_insn *insn)
769 char *f = insn->frag->fr_literal + insn->where;
770 if (insn->insn_long_opcode[0] != 0)
771 memcpy (f, insn->insn_long_opcode, insn_length (insn));
772 else
773 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
776 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
777 and install the opcode in the new location. */
779 static void
780 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
782 insn->frag = frag;
783 insn->where = where;
784 if (insn->fixp != NULL)
786 insn->fixp->fx_frag = frag;
787 insn->fixp->fx_where = where;
789 install_insn (insn);
792 /* Add INSN to the end of the output. */
794 static void
795 add_fixed_insn (struct riscv_cl_insn *insn)
797 char *f = frag_more (insn_length (insn));
798 move_insn (insn, frag_now, f - frag_now->fr_literal);
801 static void
802 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
803 relax_substateT subtype, symbolS *symbol, offsetT offset)
805 frag_grow (max_chars);
806 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
807 frag_var (rs_machine_dependent, max_chars, var,
808 subtype, symbol, offset, NULL);
811 /* Compute the length of a branch sequence, and adjust the stored length
812 accordingly. If FRAGP is NULL, the worst-case length is returned. */
814 static unsigned
815 relaxed_branch_length (fragS *fragp, asection *sec, int update)
817 int jump, rvc, length = 8;
819 if (!fragp)
820 return length;
822 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
823 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
824 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
826 /* Assume jumps are in range; the linker will catch any that aren't. */
827 length = jump ? 4 : 8;
829 if (fragp->fr_symbol != NULL
830 && S_IS_DEFINED (fragp->fr_symbol)
831 && !S_IS_WEAK (fragp->fr_symbol)
832 && sec == S_GET_SEGMENT (fragp->fr_symbol))
834 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
835 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
836 val -= fragp->fr_address + fragp->fr_fix;
838 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
839 length = 2;
840 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
841 length = 4;
842 else if (!jump && rvc)
843 length = 6;
846 if (update)
847 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
849 return length;
852 /* Information about an opcode name, mnemonics and its value. */
853 struct opcode_name_t
855 const char *name;
856 unsigned int val;
859 /* List for all supported opcode name. */
860 static const struct opcode_name_t opcode_name_list[] =
862 {"C0", 0x0},
863 {"C1", 0x1},
864 {"C2", 0x2},
866 {"LOAD", 0x03},
867 {"LOAD_FP", 0x07},
868 {"CUSTOM_0", 0x0b},
869 {"MISC_MEM", 0x0f},
870 {"OP_IMM", 0x13},
871 {"AUIPC", 0x17},
872 {"OP_IMM_32", 0x1b},
873 /* 48b 0x1f. */
875 {"STORE", 0x23},
876 {"STORE_FP", 0x27},
877 {"CUSTOM_1", 0x2b},
878 {"AMO", 0x2f},
879 {"OP", 0x33},
880 {"LUI", 0x37},
881 {"OP_32", 0x3b},
882 /* 64b 0x3f. */
884 {"MADD", 0x43},
885 {"MSUB", 0x47},
886 {"NMADD", 0x4f},
887 {"NMSUB", 0x4b},
888 {"OP_FP", 0x53},
889 {"OP_V", 0x57},
890 {"CUSTOM_2", 0x5b},
891 /* 48b 0x5f. */
893 {"BRANCH", 0x63},
894 {"JALR", 0x67},
895 /*reserved 0x5b. */
896 {"JAL", 0x6f},
897 {"SYSTEM", 0x73},
898 /*reserved 0x77. */
899 {"CUSTOM_3", 0x7b},
900 /* >80b 0x7f. */
902 {NULL, 0}
905 /* Hash table for lookup opcode name. */
906 static htab_t opcode_names_hash = NULL;
908 /* Initialization for hash table of opcode name. */
910 static void
911 init_opcode_names_hash (void)
913 const struct opcode_name_t *opcode;
915 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
916 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
917 as_fatal (_("internal: duplicate %s"), opcode->name);
920 /* Find `s` is a valid opcode name or not, return the opcode name info
921 if found. */
923 static const struct opcode_name_t *
924 opcode_name_lookup (char **s)
926 char *e;
927 char save_c;
928 struct opcode_name_t *o;
930 /* Find end of name. */
931 e = *s;
932 if (is_name_beginner (*e))
933 ++e;
934 while (is_part_of_name (*e))
935 ++e;
937 /* Terminate name. */
938 save_c = *e;
939 *e = '\0';
941 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
943 /* Advance to next token if one was recognized. */
944 if (o)
945 *s = e;
947 *e = save_c;
948 expr_parse_end = e;
950 return o;
953 /* All RISC-V registers belong to one of these classes. */
954 enum reg_class
956 RCLASS_GPR,
957 RCLASS_FPR,
958 RCLASS_VECR,
959 RCLASS_VECM,
960 RCLASS_MAX,
962 RCLASS_CSR
965 static htab_t reg_names_hash = NULL;
966 static htab_t csr_extra_hash = NULL;
968 #define ENCODE_REG_HASH(cls, n) \
969 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
970 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
971 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
973 static void
974 hash_reg_name (enum reg_class class, const char *name, unsigned n)
976 void *hash = ENCODE_REG_HASH (class, n);
977 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
978 as_fatal (_("internal: duplicate %s"), name);
981 static void
982 hash_reg_names (enum reg_class class, const char names[][NRC], unsigned n)
984 unsigned i;
986 for (i = 0; i < n; i++)
987 hash_reg_name (class, names[i], i);
990 /* Init hash table csr_extra_hash to handle CSR. */
992 static void
993 riscv_init_csr_hash (const char *name,
994 unsigned address,
995 enum riscv_csr_class class,
996 enum riscv_spec_class define_version,
997 enum riscv_spec_class abort_version)
999 struct riscv_csr_extra *entry, *pre_entry;
1000 bool need_enrty = true;
1002 pre_entry = NULL;
1003 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
1004 while (need_enrty && entry != NULL)
1006 if (entry->csr_class == class
1007 && entry->address == address
1008 && entry->define_version == define_version
1009 && entry->abort_version == abort_version)
1010 need_enrty = false;
1011 pre_entry = entry;
1012 entry = entry->next;
1015 /* Duplicate CSR. */
1016 if (!need_enrty)
1017 return;
1019 entry = notes_alloc (sizeof (*entry));
1020 entry->csr_class = class;
1021 entry->address = address;
1022 entry->define_version = define_version;
1023 entry->abort_version = abort_version;
1024 entry->next = NULL;
1026 if (pre_entry == NULL)
1027 str_hash_insert (csr_extra_hash, name, entry, 0);
1028 else
1029 pre_entry->next = entry;
1032 /* Return the CSR address after checking the ISA dependency and
1033 the privileged spec version.
1035 There are one warning and two errors for CSR,
1037 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1038 or the privileged spec, report warning only if -mcsr-check is set.
1039 Unknown CSR: the CSR has never been defined, report error.
1040 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1042 static unsigned int
1043 riscv_csr_address (const char *csr_name,
1044 struct riscv_csr_extra *entry)
1046 struct riscv_csr_extra *saved_entry = entry;
1047 enum riscv_csr_class csr_class = entry->csr_class;
1048 bool need_check_version = false;
1049 bool is_rv32_only = false;
1050 bool is_h_required = false;
1051 const char* extension = NULL;
1053 switch (csr_class)
1055 case CSR_CLASS_I_32:
1056 is_rv32_only = true;
1057 /* Fall through. */
1058 case CSR_CLASS_I:
1059 need_check_version = true;
1060 extension = "i";
1061 break;
1062 case CSR_CLASS_H_32:
1063 is_rv32_only = true;
1064 /* Fall through. */
1065 case CSR_CLASS_H:
1066 extension = "h";
1067 break;
1068 case CSR_CLASS_F:
1069 extension = "f";
1070 break;
1071 case CSR_CLASS_ZKR:
1072 extension = "zkr";
1073 break;
1074 case CSR_CLASS_ZCMT:
1075 extension = "zcmt";
1076 break;
1077 case CSR_CLASS_V:
1078 extension = "zve32x";
1079 break;
1080 case CSR_CLASS_SMAIA_32:
1081 is_rv32_only = true;
1082 /* Fall through. */
1083 case CSR_CLASS_SMAIA:
1084 extension = "smaia";
1085 break;
1086 case CSR_CLASS_SMAIA_OR_SMCSRIND:
1087 extension = "smaia or smcsrind";
1088 break;
1089 case CSR_CLASS_SMCSRIND:
1090 extension = "smcsrind";
1091 break;
1092 case CSR_CLASS_SMCNTRPMF_32:
1093 is_rv32_only = true;
1094 /* Fall through. */
1095 case CSR_CLASS_SMCNTRPMF:
1096 need_check_version = true;
1097 extension = "smcntrpmf";
1098 break;
1099 case CSR_CLASS_SMRNMI:
1100 extension = "smrnmi";
1101 break;
1102 case CSR_CLASS_SMSTATEEN_32:
1103 is_rv32_only = true;
1104 /* Fall through. */
1105 case CSR_CLASS_SMSTATEEN:
1106 extension = "smstateen";
1107 break;
1108 case CSR_CLASS_SSAIA:
1109 case CSR_CLASS_SSAIA_AND_H:
1110 case CSR_CLASS_SSAIA_32:
1111 case CSR_CLASS_SSAIA_AND_H_32:
1112 is_rv32_only = (csr_class == CSR_CLASS_SSAIA_32
1113 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1114 is_h_required = (csr_class == CSR_CLASS_SSAIA_AND_H
1115 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1116 extension = "ssaia";
1117 break;
1118 case CSR_CLASS_SSAIA_OR_SSCSRIND:
1119 case CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H:
1120 is_h_required = (csr_class == CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H);
1121 extension = "ssaia or sscsrind";
1122 break;
1123 case CSR_CLASS_SSCSRIND:
1124 case CSR_CLASS_SSCSRIND_AND_H:
1125 is_h_required = (csr_class == CSR_CLASS_SSCSRIND_AND_H);
1126 extension = "sscsrind";
1127 break;
1128 case CSR_CLASS_SSSTATEEN_AND_H_32:
1129 is_rv32_only = true;
1130 /* Fall through. */
1131 case CSR_CLASS_SSSTATEEN_AND_H:
1132 is_h_required = true;
1133 /* Fall through. */
1134 case CSR_CLASS_SSSTATEEN:
1135 extension = "ssstateen";
1136 break;
1137 case CSR_CLASS_SSCOFPMF_32:
1138 is_rv32_only = true;
1139 /* Fall through. */
1140 case CSR_CLASS_SSCOFPMF:
1141 extension = "sscofpmf";
1142 break;
1143 case CSR_CLASS_SSTC:
1144 case CSR_CLASS_SSTC_AND_H:
1145 case CSR_CLASS_SSTC_32:
1146 case CSR_CLASS_SSTC_AND_H_32:
1147 is_rv32_only = (csr_class == CSR_CLASS_SSTC_32
1148 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1149 is_h_required = (csr_class == CSR_CLASS_SSTC_AND_H
1150 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1151 extension = "sstc";
1152 break;
1153 case CSR_CLASS_DEBUG:
1154 break;
1155 case CSR_CLASS_XTHEADVECTOR:
1156 extension = "xtheadvector";
1157 break;
1158 default:
1159 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
1162 if (riscv_opts.csr_check)
1164 if (is_rv32_only && xlen != 32)
1165 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name);
1166 if (is_h_required && !riscv_subset_supports (&riscv_rps_as, "h"))
1167 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name);
1169 if (extension != NULL
1170 && !riscv_subset_supports (&riscv_rps_as, extension))
1171 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1172 csr_name, extension);
1175 while (entry != NULL)
1177 if (!need_check_version
1178 || (default_priv_spec >= entry->define_version
1179 && default_priv_spec < entry->abort_version))
1181 /* Find the CSR according to the specific version. */
1182 return entry->address;
1184 entry = entry->next;
1187 /* Can not find the CSR address from the chosen privileged version,
1188 so use the newly defined value. */
1189 if (riscv_opts.csr_check)
1191 const char *priv_name = NULL;
1192 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
1193 if (priv_name != NULL)
1194 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1195 csr_name, priv_name);
1198 return saved_entry->address;
1201 /* Return -1 if the CSR has never been defined. Otherwise, return
1202 the address. */
1204 static unsigned int
1205 reg_csr_lookup_internal (const char *s)
1207 struct riscv_csr_extra *r =
1208 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
1210 if (r == NULL)
1211 return -1U;
1213 return riscv_csr_address (s, r);
1216 static unsigned int
1217 reg_lookup_internal (const char *s, enum reg_class class)
1219 void *r;
1221 if (class == RCLASS_CSR)
1222 return reg_csr_lookup_internal (s);
1224 r = str_hash_find (reg_names_hash, s);
1225 if (r == NULL || DECODE_REG_CLASS (r) != class)
1226 return -1;
1228 if (riscv_subset_supports (&riscv_rps_as, "e")
1229 && class == RCLASS_GPR
1230 && DECODE_REG_NUM (r) > 15)
1231 return -1;
1233 return DECODE_REG_NUM (r);
1236 static bool
1237 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1239 char *e;
1240 char save_c;
1241 int reg = -1;
1243 /* Find end of name. */
1244 e = *s;
1245 if (is_name_beginner (*e))
1246 ++e;
1247 while (is_part_of_name (*e))
1248 ++e;
1250 /* Terminate name. */
1251 save_c = *e;
1252 *e = '\0';
1254 /* Look for the register. Advance to next token if one was recognized. */
1255 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1256 *s = e;
1258 *e = save_c;
1259 if (regnop)
1260 *regnop = reg;
1261 return reg >= 0;
1264 static bool
1265 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
1267 const char *p = strchr (*s, ',');
1268 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
1270 if (len == 0)
1271 return false;
1273 for (i = 0; i < size; i++)
1274 if (array[i] != NULL && strncmp (array[i], *s, len) == 0
1275 && array[i][len] == '\0')
1277 *regnop = i;
1278 *s += len;
1279 return true;
1282 return false;
1285 static bool
1286 flt_lookup (float f, const float *array, size_t size, unsigned *regnop)
1288 size_t i;
1290 for (i = 0; i < size; i++)
1291 if (array[i] == f)
1293 *regnop = i;
1294 return true;
1297 return false;
1300 /* Map ra and s-register to [4,15], so that we can check if the
1301 reg2 in register list reg1-reg2 or single reg2 is valid or not,
1302 and obtain the corresponding reg_list value.
1304 ra - 4
1305 s0 - 5
1306 s1 - 6
1307 ....
1308 s10 - 0 (invalid)
1309 s11 - 15. */
1311 static int
1312 regno_to_reg_list (unsigned regno)
1314 if (regno == X_RA)
1315 return 4;
1316 else if (regno == X_S0 || regno == X_S1)
1317 return 5 + regno - X_S0;
1318 else if (regno >= X_S2 && regno < X_S10)
1319 return 7 + regno - X_S2;
1320 else if (regno == X_S11)
1321 return 15;
1323 /* Invalid symbol. */
1324 return 0;
1327 /* Parse register list, and return the last register by regno_to_reg_list.
1329 If ABI register names are used (e.g. ra and s0), the register
1330 list could be "{ra}", "{ra, s0}", "{ra, s0-sN}", where 0 < N < 10 or
1331 N == 11.
1333 If numeric register names are used (e.g. x1 and x8), the register list
1334 could be "{x1}", "{x1,x8}", "{x1,x8-x9}", "{x1,x8-x9,x18}" and
1335 "{x1,x8-x9,x18-xN}", where 19 < N < 25 or N == 27.
1337 The numeric and ABI register names cannot be used at the same time.
1339 TODO: Report errors for the following cases,
1340 1. Too many registers in the list.
1341 2. Cases which return 0.
1342 3. Illegal formats, for example, {x1,x8-NULL,x18-x24/x18}, {x1-x2,x8}. */
1344 static unsigned
1345 reglist_lookup_internal (char *reglist)
1347 unsigned regno = 0;
1348 unsigned reg_list = 0;
1349 char *regname[3][2] = {{NULL}};
1350 char *save_tok, *save_subtok;
1351 unsigned i, j;
1353 char *token = strtok_r (reglist, ",", &save_tok);
1354 for (i = 0; i < 3 && token != NULL;
1355 token = strtok_r (NULL, ",", &save_tok), i++)
1357 char *subtoken = strtok_r (token, "-", &save_subtok);
1358 for (j = 0; j < 2 && subtoken != NULL;
1359 subtoken = strtok_r (NULL, "-", &save_subtok), j++)
1360 regname[i][j] = subtoken;
1363 bool reg1_numeric = false;
1364 for (i = 0; i < 3; i++)
1366 if (regname[i][0] == NULL)
1367 continue;
1368 #define REG_TO_REG_LIST(NAME, NUM, LIST) \
1369 (reg_lookup (&NAME, RCLASS_GPR, &NUM) && (LIST = regno_to_reg_list (NUM)))
1370 #define REG_NUMERIC(NAME) (NAME[0] == 'x')
1371 #define REG_CONFLICT(NAME, REG_NUMERIC) \
1372 ((NAME[0] == 'x' && !REG_NUMERIC) || (NAME[0] != 'x' && REG_NUMERIC))
1373 switch (i)
1375 case 0:
1376 reg1_numeric = REG_NUMERIC (regname[i][0]);
1377 if (!REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1378 || regno != X_RA)
1379 return 0;
1380 break;
1381 case 1:
1382 if (REG_CONFLICT (regname[i][0], reg1_numeric)
1383 /* The second register should be s0 or its numeric names x8. */
1384 || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1385 || regno != X_S0)
1386 return 0;
1387 else if (regname[i][1] == NULL)
1388 return reg_list;
1390 if (REG_CONFLICT (regname[i][1], reg1_numeric)
1391 /* The third register is x9 if the numeric name is used.
1392 Otherwise, it could be any other sN register, where N > 0. */
1393 || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
1394 || regno <= X_S0
1395 || (reg1_numeric && regno != X_S1))
1396 return 0;
1397 break;
1398 case 2:
1399 /* Must use register numeric names. */
1400 if (!reg1_numeric
1401 || !REG_NUMERIC (regname[i][0])
1402 /* The fourth register should be s2. */
1403 || !REG_TO_REG_LIST (regname[i][0], regno, reg_list)
1404 || regno != X_S2)
1405 return 0;
1406 else if (regname[i][1] == NULL)
1407 return reg_list;
1409 if (!reg1_numeric
1410 || !REG_NUMERIC (regname[i][1])
1411 /* The fifth register could be any other sN register, where N > 1. */
1412 || !REG_TO_REG_LIST (regname[i][1], regno, reg_list)
1413 || regno <= X_S2)
1414 return 0;
1415 break;
1416 default:
1417 return 0;
1419 #undef REG_TO_REG_LIST
1420 #undef REG_NUMERIC
1421 #undef REG_CONFLICT
1423 return reg_list;
1426 /* Parse register list. Return false if REG_LIST is zero, which is an
1427 invalid value. */
1429 static bool
1430 reglist_lookup (char **s, unsigned *reg_list)
1432 *reg_list = 0;
1433 char *reglist = strdup (*s);
1434 if (reglist != NULL)
1436 char *token = strtok (reglist, "}");
1437 if (token != NULL)
1439 *s += strlen (token);
1440 *reg_list = reglist_lookup_internal (reglist);
1442 else
1444 as_bad (_("cannot find `}' for cm.push/cm.pop"));
1445 *reg_list = 0;
1448 free (reglist);
1449 return *reg_list == 0 ? false : true;
1452 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1453 #define USE_IMM(n, s) \
1454 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1456 /* For consistency checking, verify that all bits are specified either
1457 by the match/mask part of the instruction definition, or by the
1458 operand list. The `length` could be the actual instruction length or
1459 0 for auto-detection. */
1461 static bool
1462 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1464 const char *oparg, *opargStart;
1465 insn_t used_bits = opc->mask;
1466 int insn_width;
1467 insn_t required_bits;
1469 if (length == 0)
1470 length = riscv_insn_length (opc->match);
1471 /* We don't support instructions longer than 64-bits yet. */
1472 if (length > 8)
1473 length = 8;
1474 insn_width = 8 * length;
1476 required_bits = ((insn_t)~0ULL) >> (64 - insn_width);
1478 if ((used_bits & opc->match) != (opc->match & required_bits))
1480 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1481 opc->name, opc->args);
1482 return false;
1485 for (oparg = opc->args; *oparg; ++oparg)
1487 opargStart = oparg;
1488 switch (*oparg)
1490 case 'C': /* RVC */
1491 switch (*++oparg)
1493 case 'U': break; /* CRS1, constrained to equal RD. */
1494 case 'c': break; /* CRS1, constrained to equal sp. */
1495 case 'T': /* CRS2, floating point. */
1496 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1497 case 'S': /* CRS1S, floating point. */
1498 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1499 case 'w': break; /* CRS1S, constrained to equal RD. */
1500 case 'D': /* CRS2S, floating point. */
1501 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1502 case 'x': break; /* CRS2S, constrained to equal RD. */
1503 case 'z': break; /* CRS2S, constrained to be x0. */
1504 case '>': /* CITYPE immediate, compressed shift. */
1505 case 'u': /* CITYPE immediate, compressed lui. */
1506 case 'v': /* CITYPE immediate, li to compressed lui. */
1507 case 'o': /* CITYPE immediate, allow zero. */
1508 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1509 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1510 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1511 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1512 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1513 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1514 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1515 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1516 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1517 /* CLTYPE and CSTYPE have the same immediate encoding. */
1518 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1519 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1520 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1521 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1522 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1523 case 'F': /* Compressed funct for .insn directive. */
1524 switch (*++oparg)
1526 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1527 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1528 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1529 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1530 default:
1531 goto unknown_validate_operand;
1533 break;
1534 default:
1535 goto unknown_validate_operand;
1537 break; /* end RVC */
1538 case 'V': /* RVV */
1539 switch (*++oparg)
1541 case 'd':
1542 case 'f': USE_BITS (OP_MASK_VD, OP_SH_VD); break;
1543 case 'e': USE_BITS (OP_MASK_VWD, OP_SH_VWD); break;
1544 case 's': USE_BITS (OP_MASK_VS1, OP_SH_VS1); break;
1545 case 't': USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1546 case 'u': USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1547 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1548 case 'v': USE_BITS (OP_MASK_VD, OP_SH_VD);
1549 USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1550 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1551 case '0': break;
1552 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1553 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1554 case 'i':
1555 case 'j':
1556 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1557 case 'l': used_bits |= ENCODE_RVV_VI_UIMM6 (-1U); break;
1558 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1559 case 'M': break; /* Macro operand, must be a mask register. */
1560 case 'T': break; /* Macro operand, must be a vector register. */
1561 default:
1562 goto unknown_validate_operand;
1564 break; /* end RVV */
1565 case ',': break;
1566 case '(': break;
1567 case ')': break;
1568 case '{': break;
1569 case '}': break;
1570 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1571 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1572 case 'A': break; /* Macro operand, must be symbol. */
1573 case 'B': break; /* Macro operand, must be symbol or constant. */
1574 case 'c': break; /* Macro operand, must be symbol or constant. */
1575 case 'I': break; /* Macro operand, must be constant. */
1576 case 'D': /* RD, floating point. */
1577 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1578 case 'y': USE_BITS (OP_MASK_BS, OP_SH_BS); break;
1579 case 'Y': USE_BITS (OP_MASK_RNUM, OP_SH_RNUM); break;
1580 case 'Z': /* RS1, CSR number. */
1581 case 'S': /* RS1, floating point. */
1582 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1583 case 'U': /* RS1 and RS2 are the same, floating point. */
1584 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1585 /* Fall through. */
1586 case 'T': /* RS2, floating point. */
1587 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1588 case 'R': /* RS3, floating point. */
1589 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1590 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1591 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1592 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1593 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1594 case 'o': /* ITYPE immediate, load displacement. */
1595 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1596 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1597 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1598 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1599 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1600 case 'z': break; /* Zero immediate. */
1601 case '[': break; /* Unused operand. */
1602 case ']': break; /* Unused operand. */
1603 case '0': break; /* AMO displacement, must to zero. */
1604 case '1': break; /* Relaxation operand. */
1605 case 'F': /* Funct for .insn directive. */
1606 switch (*++oparg)
1608 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1609 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1610 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1611 default:
1612 goto unknown_validate_operand;
1614 break;
1615 case 'O': /* Opcode for .insn directive. */
1616 switch (*++oparg)
1618 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1619 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1620 default:
1621 goto unknown_validate_operand;
1623 break;
1624 case 'W': /* Various operands for standard z extensions. */
1625 switch (*++oparg)
1627 case 'i':
1628 switch (*++oparg)
1630 case 'f': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1631 default:
1632 goto unknown_validate_operand;
1634 break;
1635 case 'f':
1636 switch (*++oparg)
1638 case 'v': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1639 default:
1640 goto unknown_validate_operand;
1642 break;
1643 case 'c':
1644 switch (*++oparg)
1646 /* sreg operators in cm.mvsa01 and cm.mva01s. */
1647 case '1': USE_BITS (OP_MASK_SREG1, OP_SH_SREG1); break;
1648 case '2': USE_BITS (OP_MASK_SREG2, OP_SH_SREG2); break;
1649 /* byte immediate operators, load/store byte insns. */
1650 case 'h': used_bits |= ENCODE_ZCB_HALFWORD_UIMM (-1U); break;
1651 /* halfword immediate operators, load/store halfword insns. */
1652 case 'b': used_bits |= ENCODE_ZCB_BYTE_UIMM (-1U); break;
1653 /* Immediate offset operand for cm.push and cm.pop. */
1654 case 'p': used_bits |= ENCODE_ZCMP_SPIMM (-1U); break;
1655 /* Register list operand for cm.push and cm.pop. */
1656 case 'r': USE_BITS (OP_MASK_REG_LIST, OP_SH_REG_LIST); break;
1657 /* Table jump used by cm.jt or cm.jalt. */
1658 case 'i':
1659 case 'I': used_bits |= ENCODE_ZCMT_INDEX (-1U); break;
1660 case 'f': break;
1661 default:
1662 goto unknown_validate_operand;
1664 break;
1665 default:
1666 goto unknown_validate_operand;
1668 break;
1669 case 'X': /* Vendor-specific operands. */
1670 switch (*++oparg)
1672 case 't': /* Vendor-specific (T-head) operands. */
1674 size_t n;
1675 size_t s;
1676 switch (*++oparg)
1678 case 'V':
1679 switch (*++oparg)
1681 case 'c': /* Vtypei for th.vsetvli. */
1682 used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1683 default:
1684 goto unknown_validate_operand;
1686 break;
1687 case 'l': /* Integer immediate, literal. */
1688 oparg += strcspn(oparg, ",") - 1;
1689 break;
1690 case 's': /* Integer immediate, 'XtsN@S' ... N-bit signed immediate at bit S. */
1691 goto use_imm;
1692 case 'u': /* Integer immediate, 'XtuN@S' ... N-bit unsigned immediate at bit S. */
1693 goto use_imm;
1694 use_imm:
1695 n = strtol (oparg + 1, (char **)&oparg, 10);
1696 if (*oparg != '@')
1697 goto unknown_validate_operand;
1698 s = strtol (oparg + 1, (char **)&oparg, 10);
1699 oparg--;
1701 USE_IMM (n, s);
1702 break;
1703 default:
1704 goto unknown_validate_operand;
1707 break;
1708 case 'c': /* Vendor-specific (CORE-V) operands. */
1709 switch (*++oparg)
1711 case '2':
1712 case '4':
1713 used_bits |= ENCODE_CV_IS2_UIMM5 (-1U);
1714 break;
1715 case '3':
1716 used_bits |= ENCODE_CV_IS3_UIMM5 (-1U);
1717 break;
1718 case '5':
1719 used_bits |= ENCODE_CV_SIMD_IMM6(-1U);
1720 break;
1721 case '6':
1722 used_bits |= ENCODE_CV_BITMANIP_UIMM5(-1U);
1723 break;
1724 case '7':
1725 used_bits |= ENCODE_CV_BITMANIP_UIMM2(-1U);
1726 break;
1727 case '8':
1728 used_bits |= ENCODE_CV_SIMD_UIMM6(-1U);
1729 ++oparg;
1730 break;
1731 default:
1732 goto unknown_validate_operand;
1734 break;
1735 case 's': /* Vendor-specific (SiFive) operands. */
1736 switch (*++oparg)
1738 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1739 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1740 case 'O':
1741 switch (*++oparg)
1743 case '2': USE_BITS (OP_MASK_XSO2, OP_SH_XSO2); break;
1744 case '1': USE_BITS (OP_MASK_XSO1, OP_SH_XSO1); break;
1745 default:
1746 goto unknown_validate_operand;
1748 break;
1749 default:
1750 goto unknown_validate_operand;
1752 break;
1753 default:
1754 goto unknown_validate_operand;
1756 break;
1757 default:
1758 unknown_validate_operand:
1759 as_bad (_("internal: bad RISC-V opcode "
1760 "(unknown operand type `%s'): %s %s"),
1761 opargStart, opc->name, opc->args);
1762 return false;
1766 if (used_bits != required_bits)
1768 as_bad (_("internal: bad RISC-V opcode "
1769 "(bits %#llx undefined or invalid): %s %s"),
1770 (unsigned long long)(used_bits ^ required_bits),
1771 opc->name, opc->args);
1772 return false;
1774 return true;
1777 #undef USE_BITS
1779 struct percent_op_match
1781 const char *str;
1782 bfd_reloc_code_real_type reloc;
1785 /* Common hash table initialization function for instruction and .insn
1786 directive. */
1788 static htab_t
1789 init_opcode_hash (const struct riscv_opcode *opcodes,
1790 bool insn_directive_p)
1792 int i = 0;
1793 int length;
1794 htab_t hash = str_htab_create ();
1795 while (opcodes[i].name)
1797 const char *name = opcodes[i].name;
1798 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1799 as_fatal (_("internal: duplicate %s"), name);
1803 if (opcodes[i].pinfo != INSN_MACRO)
1805 if (insn_directive_p)
1806 length = ((name[0] == 'c') ? 2 : 4);
1807 else
1808 length = 0; /* Let assembler determine the length. */
1809 if (!validate_riscv_insn (&opcodes[i], length))
1810 as_fatal (_("internal: broken assembler. "
1811 "No assembly attempted"));
1813 else
1814 gas_assert (!insn_directive_p);
1815 ++i;
1817 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1820 return hash;
1823 /* Record all PC-relative high-part relocation that we have encountered to
1824 help us resolve the corresponding low-part relocation later. */
1825 typedef struct
1827 const asection *sec;
1828 bfd_vma address;
1829 symbolS *symbol;
1830 bfd_vma target;
1831 } riscv_pcrel_hi_fixup;
1833 /* Handle of the pcrel_hi hash table. */
1834 static htab_t riscv_pcrel_hi_fixup_hash;
1836 /* Get the key of a entry from the pcrel_hi hash table. */
1838 static hashval_t
1839 riscv_pcrel_fixup_hash (const void *entry)
1841 const riscv_pcrel_hi_fixup *e = entry;
1843 /* the pcrel_hi with same address may reside in different segments,
1844 to ensure uniqueness, the segment ID needs to be included in the
1845 hash key calculation.
1846 Temporarily using the prime number 499 as a multiplier, but it
1847 might not be large enough. */
1848 return e->address + 499 * e->sec->id;
1851 /* Compare the keys between two entries fo the pcrel_hi hash table. */
1853 static int
1854 riscv_pcrel_fixup_eq (const void *entry1, const void *entry2)
1856 const riscv_pcrel_hi_fixup *e1 = entry1, *e2 = entry2;
1857 return e1->sec->id == e2->sec->id
1858 && e1->address == e2->address;
1861 /* Record the pcrel_hi relocation. */
1863 static bool
1864 riscv_record_pcrel_fixup (htab_t p, const asection *sec, bfd_vma address,
1865 symbolS *symbol, bfd_vma target)
1867 riscv_pcrel_hi_fixup entry = {sec, address, symbol, target};
1868 riscv_pcrel_hi_fixup **slot =
1869 (riscv_pcrel_hi_fixup **) htab_find_slot (p, &entry, INSERT);
1870 if (slot == NULL)
1871 return false;
1873 *slot = (riscv_pcrel_hi_fixup *) xmalloc (sizeof (riscv_pcrel_hi_fixup));
1874 if (*slot == NULL)
1875 return false;
1876 **slot = entry;
1877 return true;
1880 /* This function is called once, at assembler startup time. It should set up
1881 all the tables, etc. that the MD part of the assembler will need. */
1883 void
1884 md_begin (void)
1886 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1888 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1889 as_warn (_("could not set architecture and machine"));
1891 op_hash = init_opcode_hash (riscv_opcodes, false);
1892 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1894 reg_names_hash = str_htab_create ();
1895 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1896 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1897 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1898 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1899 hash_reg_names (RCLASS_VECR, riscv_vecr_names_numeric, NVECR);
1900 hash_reg_names (RCLASS_VECM, riscv_vecm_names_numeric, NVECM);
1901 /* Add "fp" as an alias for "s0". */
1902 hash_reg_name (RCLASS_GPR, "fp", 8);
1904 /* Create and insert CSR hash tables. */
1905 csr_extra_hash = str_htab_create ();
1906 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1907 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1908 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1909 DECLARE_CSR(name, num, class, define_version, abort_version);
1910 #include "opcode/riscv-opc.h"
1911 #undef DECLARE_CSR
1913 opcode_names_hash = str_htab_create ();
1914 init_opcode_names_hash ();
1916 /* Create pcrel_hi hash table to resolve the relocation while with
1917 -mno-relax. */
1918 riscv_pcrel_hi_fixup_hash = htab_create (1024, riscv_pcrel_fixup_hash,
1919 riscv_pcrel_fixup_eq, free);
1921 /* Set the default alignment for the text section. */
1922 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1925 static insn_t
1926 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1928 switch (reloc_type)
1930 case BFD_RELOC_32:
1931 return value;
1933 case BFD_RELOC_RISCV_HI20:
1934 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1936 case BFD_RELOC_RISCV_LO12_S:
1937 return ENCODE_STYPE_IMM (value);
1939 case BFD_RELOC_RISCV_LO12_I:
1940 return ENCODE_ITYPE_IMM (value);
1942 default:
1943 abort ();
1947 /* Output an instruction. IP is the instruction information.
1948 ADDRESS_EXPR is an operand of the instruction to be used with
1949 RELOC_TYPE. */
1951 static void
1952 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1953 bfd_reloc_code_real_type reloc_type)
1955 dwarf2_emit_insn (0);
1957 if (reloc_type != BFD_RELOC_UNUSED)
1959 reloc_howto_type *howto;
1961 gas_assert (address_expr);
1962 if (reloc_type == BFD_RELOC_12_PCREL
1963 || reloc_type == BFD_RELOC_RISCV_JMP)
1965 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1966 int best_case = insn_length (ip);
1967 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1969 if (now_seg == absolute_section)
1971 as_bad (_("relaxable branches not supported in absolute section"));
1972 return;
1975 add_relaxed_insn (ip, worst_case, best_case,
1976 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1977 address_expr->X_add_symbol,
1978 address_expr->X_add_number);
1979 return;
1981 else
1983 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1984 if (howto == NULL)
1985 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1986 reloc_type);
1988 ip->fixp = fix_new_exp (ip->frag, ip->where,
1989 bfd_get_reloc_size (howto),
1990 address_expr, false, reloc_type);
1992 ip->fixp->fx_tcbit = riscv_opts.relax;
1993 ip->fixp->tc_fix_data.source_macro = source_macro;
1997 add_fixed_insn (ip);
1999 /* We need to start a new frag after any instruction that can be
2000 optimized away or compressed by the linker during relaxation, to prevent
2001 the assembler from computing static offsets across such an instruction.
2002 This is necessary to get correct EH info. */
2003 if (reloc_type == BFD_RELOC_RISCV_HI20
2004 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
2005 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
2006 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
2008 frag_wane (frag_now);
2009 frag_new (0);
2013 /* Build an instruction created by a macro expansion. This is passed
2014 a pointer to the count of instructions created so far, an expression,
2015 the name of the instruction to build, an operand format string, and
2016 corresponding arguments. */
2018 static void
2019 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
2021 const struct riscv_opcode *mo;
2022 struct riscv_cl_insn insn;
2023 bfd_reloc_code_real_type r;
2024 va_list args;
2025 const char *fmtStart;
2027 va_start (args, fmt);
2029 r = BFD_RELOC_UNUSED;
2030 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
2031 gas_assert (mo);
2033 /* Find a non-RVC variant of the instruction. append_insn will compress
2034 it if possible. */
2035 while (riscv_insn_length (mo->match) < 4)
2036 mo++;
2037 gas_assert (strcmp (name, mo->name) == 0);
2039 create_insn (&insn, mo);
2040 for (;; ++fmt)
2042 fmtStart = fmt;
2043 switch (*fmt)
2045 case 'V': /* RVV */
2046 switch (*++fmt)
2048 case 'd':
2049 INSERT_OPERAND (VD, insn, va_arg (args, int));
2050 continue;
2051 case 's':
2052 INSERT_OPERAND (VS1, insn, va_arg (args, int));
2053 continue;
2054 case 't':
2055 INSERT_OPERAND (VS2, insn, va_arg (args, int));
2056 continue;
2057 case 'm':
2059 int reg = va_arg (args, int);
2060 if (reg == -1)
2062 INSERT_OPERAND (VMASK, insn, 1);
2063 continue;
2065 else if (reg == 0)
2067 INSERT_OPERAND (VMASK, insn, 0);
2068 continue;
2070 else
2071 goto unknown_macro_argument;
2073 default:
2074 goto unknown_macro_argument;
2076 break;
2078 case 'd':
2079 INSERT_OPERAND (RD, insn, va_arg (args, int));
2080 continue;
2081 case 's':
2082 INSERT_OPERAND (RS1, insn, va_arg (args, int));
2083 continue;
2084 case 't':
2085 INSERT_OPERAND (RS2, insn, va_arg (args, int));
2086 continue;
2088 case 'j':
2089 case 'u':
2090 case 'q':
2091 gas_assert (ep != NULL);
2092 r = va_arg (args, int);
2093 continue;
2095 case '\0':
2096 break;
2097 case ',':
2098 continue;
2099 default:
2100 unknown_macro_argument:
2101 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
2103 break;
2105 va_end (args);
2106 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
2108 append_insn (&insn, ep, r);
2111 /* Build an instruction created by a macro expansion. Like md_assemble but
2112 accept a printf-style format string and arguments. */
2114 static void
2115 md_assemblef (const char *format, ...)
2117 char *buf;
2118 va_list ap;
2120 va_start (ap, format);
2122 buf = xvasprintf (format, ap);
2123 md_assemble (buf);
2124 free(buf);
2126 va_end (ap);
2129 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
2130 unset. */
2132 static void
2133 normalize_constant_expr (expressionS *ex)
2135 if (xlen > 32)
2136 return;
2137 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
2138 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
2139 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
2140 - 0x80000000);
2143 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
2144 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
2146 static void
2147 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
2148 bool maybe_csr)
2150 if (ex->X_op == O_big)
2151 as_bad (_("unsupported large constant"));
2152 else if (maybe_csr && ex->X_op == O_symbol)
2153 as_bad (_("unknown CSR `%s'"),
2154 S_GET_NAME (ex->X_add_symbol));
2155 else if (ex->X_op != O_constant)
2156 as_bad (_("instruction %s requires absolute expression"),
2157 ip->insn_mo->name);
2158 normalize_constant_expr (ex);
2161 static symbolS *
2162 make_internal_label (void)
2164 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
2165 frag_now_fix ());
2168 /* Load an entry from the GOT. */
2170 static void
2171 pcrel_access (int destreg, int tempreg, expressionS *ep,
2172 const char *lo_insn, const char *lo_pattern,
2173 bfd_reloc_code_real_type hi_reloc,
2174 bfd_reloc_code_real_type lo_reloc)
2176 expressionS ep2;
2177 ep2.X_op = O_symbol;
2178 ep2.X_add_symbol = make_internal_label ();
2179 ep2.X_add_number = 0;
2181 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
2182 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
2185 static void
2186 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
2187 bfd_reloc_code_real_type hi_reloc,
2188 bfd_reloc_code_real_type lo_reloc)
2190 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
2193 static void
2194 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
2195 bfd_reloc_code_real_type hi_reloc,
2196 bfd_reloc_code_real_type lo_reloc)
2198 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
2201 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
2203 static void
2204 riscv_call (int destreg, int tempreg, expressionS *ep,
2205 bfd_reloc_code_real_type reloc)
2207 /* Ensure the jalr is emitted to the same frag as the auipc. */
2208 frag_grow (8);
2209 macro_build (ep, "auipc", "d,u", tempreg, reloc);
2210 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
2211 /* See comment at end of append_insn. */
2212 frag_wane (frag_now);
2213 frag_new (0);
2216 /* Load an integer constant into a register. */
2218 static void
2219 load_const (int reg, expressionS *ep)
2221 int shift = RISCV_IMM_BITS;
2222 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
2223 expressionS upper = *ep, lower = *ep;
2224 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
2225 upper.X_add_number -= lower.X_add_number;
2227 if (ep->X_op != O_constant)
2229 as_bad (_("unsupported large constant"));
2230 return;
2233 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
2235 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
2236 while (((upper.X_add_number >> shift) & 1) == 0)
2237 shift++;
2239 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
2240 load_const (reg, &upper);
2242 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
2243 if (lower.X_add_number != 0)
2244 md_assemblef ("addi x%d, x%d, %" PRId64, reg, reg,
2245 (int64_t) lower.X_add_number);
2247 else
2249 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
2250 int hi_reg = 0;
2252 if (upper.X_add_number != 0)
2254 /* Discard low part and zero-extend upper immediate. */
2255 upper_imm = ((uint32_t)upper.X_add_number >> shift);
2257 md_assemblef ("lui x%d, 0x%" PRIx64, reg, (uint64_t) upper_imm);
2258 hi_reg = reg;
2261 if (lower.X_add_number != 0 || hi_reg == 0)
2262 md_assemblef ("%s x%d, x%d, %" PRId64, ADD32_INSN, reg, hi_reg,
2263 (int64_t) lower.X_add_number);
2267 /* Zero extend and sign extend byte/half-word/word. */
2269 static void
2270 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
2272 md_assemblef ("slli x%d, x%d, %#x", destreg, srcreg, shift);
2273 md_assemblef ("sr%ci x%d, x%d, %#x",
2274 sign ? 'a' : 'l', destreg, destreg, shift);
2277 /* Expand RISC-V Vector macros into one or more instructions. */
2279 static void
2280 vector_macro (struct riscv_cl_insn *ip)
2282 int vd = (ip->insn_opcode >> OP_SH_VD) & OP_MASK_VD;
2283 int vs1 = (ip->insn_opcode >> OP_SH_VS1) & OP_MASK_VS1;
2284 int vs2 = (ip->insn_opcode >> OP_SH_VS2) & OP_MASK_VS2;
2285 int vm = (ip->insn_opcode >> OP_SH_VMASK) & OP_MASK_VMASK;
2286 int vtemp = (ip->insn_opcode >> OP_SH_VFUNCT6) & OP_MASK_VFUNCT6;
2287 const char *vmslt_vx = ip->insn_mo->match ? "vmsltu.vx" : "vmslt.vx";
2288 int mask = ip->insn_mo->mask;
2290 switch (mask)
2292 case M_VMSGE:
2293 if (vm)
2295 /* Unmasked. */
2296 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vd, vs2, vs1, -1);
2297 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
2298 break;
2300 if (vtemp != 0)
2302 /* Masked. Have vtemp to avoid overlap constraints. */
2303 if (vd == vm)
2305 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vtemp, vs2, vs1, -1);
2306 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
2308 else
2310 /* Preserve the value of vd if not updating by vm. */
2311 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vtemp, vs2, vs1, -1);
2312 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
2313 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
2314 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
2317 else if (vd != vm)
2319 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2320 macro_build (NULL, vmslt_vx, "Vd,Vt,sVm", vd, vs2, vs1, vm);
2321 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
2323 else
2324 as_bad (_("must provide temp if destination overlaps mask"));
2325 break;
2327 default:
2328 break;
2332 /* Expand RISC-V assembly macros into one or more instructions. */
2334 static void
2335 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
2336 bfd_reloc_code_real_type *imm_reloc)
2338 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
2339 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
2340 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
2341 int mask = ip->insn_mo->mask;
2343 source_macro = mask;
2345 switch (mask)
2347 case M_LI:
2348 load_const (rd, imm_expr);
2349 break;
2351 case M_LA:
2352 case M_LLA:
2353 case M_LGA:
2354 /* Load the address of a symbol into a register. */
2355 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
2356 as_bad (_("offset too large"));
2358 if (imm_expr->X_op == O_constant)
2359 load_const (rd, imm_expr);
2360 /* Global PIC symbol. */
2361 else if ((riscv_opts.pic && mask == M_LA)
2362 || mask == M_LGA)
2363 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2364 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2365 /* Local PIC symbol, or any non-PIC symbol. */
2366 else
2367 pcrel_load (rd, rd, imm_expr, "addi",
2368 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2369 break;
2371 case M_LA_TLS_GD:
2372 pcrel_load (rd, rd, imm_expr, "addi",
2373 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2374 break;
2376 case M_LA_TLS_IE:
2377 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2378 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2379 break;
2381 case M_Lx:
2382 pcrel_load (rd, rd, imm_expr, ip->insn_mo->name,
2383 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2384 break;
2386 case M_FLx:
2387 pcrel_load (rd, rs1, imm_expr, ip->insn_mo->name,
2388 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2389 break;
2391 case M_Sx_FSx:
2392 pcrel_store (rs2, rs1, imm_expr, ip->insn_mo->name,
2393 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2394 break;
2396 case M_CALL:
2397 riscv_call (rd, rs1, imm_expr, *imm_reloc);
2398 break;
2400 case M_EXTH:
2401 riscv_ext (rd, rs1, xlen - 16, *ip->insn_mo->name == 's');
2402 break;
2404 case M_ZEXTW:
2405 riscv_ext (rd, rs1, xlen - 32, false);
2406 break;
2408 case M_SEXTB:
2409 riscv_ext (rd, rs1, xlen - 8, true);
2410 break;
2412 case M_VMSGE:
2413 vector_macro (ip);
2414 break;
2416 default:
2417 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
2418 break;
2421 source_macro = -1;
2424 static const struct percent_op_match percent_op_utype[] =
2426 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
2427 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
2428 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
2429 {"tlsdesc_hi", BFD_RELOC_RISCV_TLSDESC_HI20},
2430 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
2431 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
2432 {"hi", BFD_RELOC_RISCV_HI20},
2433 {0, 0}
2436 static const struct percent_op_match percent_op_itype[] =
2438 {"lo", BFD_RELOC_RISCV_LO12_I},
2439 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
2440 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
2441 {"tlsdesc_load_lo", BFD_RELOC_RISCV_TLSDESC_LOAD_LO12},
2442 {"tlsdesc_add_lo", BFD_RELOC_RISCV_TLSDESC_ADD_LO12},
2443 {0, 0}
2446 static const struct percent_op_match percent_op_stype[] =
2448 {"lo", BFD_RELOC_RISCV_LO12_S},
2449 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
2450 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
2451 {0, 0}
2454 static const struct percent_op_match percent_op_relax_only[] =
2456 {"tlsdesc_call", BFD_RELOC_RISCV_TLSDESC_CALL},
2457 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
2458 {0, 0}
2461 static const struct percent_op_match percent_op_null[] =
2463 {0, 0}
2466 /* Return true if *STR points to a relocation operator. When returning true,
2467 move *STR over the operator and store its relocation code in *RELOC.
2468 Leave both *STR and *RELOC alone when returning false. */
2470 static bool
2471 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
2472 const struct percent_op_match *percent_op)
2474 for ( ; percent_op->str; percent_op++)
2475 if (strncasecmp (*str + 1, percent_op->str, strlen (percent_op->str)) == 0)
2477 size_t len = 1 + strlen (percent_op->str);
2479 while (ISSPACE ((*str)[len]))
2480 ++len;
2481 if ((*str)[len] != '(')
2482 continue;
2484 *str += len;
2485 *reloc = percent_op->reloc;
2487 /* Check whether the output BFD supports this relocation.
2488 If not, issue an error and fall back on something safe. */
2489 if (*reloc != BFD_RELOC_UNUSED
2490 && !bfd_reloc_type_lookup (stdoutput, *reloc))
2492 as_bad ("internal: relocation %s isn't supported by the "
2493 "current ABI", percent_op->str);
2494 *reloc = BFD_RELOC_UNUSED;
2496 return true;
2498 return false;
2501 static void
2502 my_getExpression (expressionS *ep, char *str)
2504 char *save_in;
2506 save_in = input_line_pointer;
2507 input_line_pointer = str;
2508 expression (ep);
2509 expr_parse_end = input_line_pointer;
2510 input_line_pointer = save_in;
2513 /* Parse string STR as a 16-bit relocatable operand. Store the
2514 expression in *EP and the relocation, if any, in RELOC.
2515 Return the number of relocation operators used (0 or 1).
2517 On exit, EXPR_PARSE_END points to the first character after the
2518 expression. */
2520 static size_t
2521 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2522 char *str, const struct percent_op_match *percent_op)
2524 size_t reloc_index;
2525 unsigned crux_depth, str_depth;
2526 bool orig_probing = probing_insn_operands;
2527 char *crux;
2529 /* Search for the start of the main expression.
2531 End the loop with CRUX pointing to the start of the main expression and
2532 with CRUX_DEPTH containing the number of open brackets at that point. */
2533 reloc_index = -1;
2534 str_depth = 0;
2537 reloc_index++;
2538 crux = str;
2539 crux_depth = str_depth;
2541 /* Skip over whitespace and brackets, keeping count of the number
2542 of brackets. */
2543 while (*str == ' ' || *str == '\t' || *str == '(')
2544 if (*str++ == '(')
2545 str_depth++;
2547 while (*str == '%'
2548 && reloc_index < 1
2549 && parse_relocation (&str, reloc, percent_op));
2551 if (*str == '%')
2553 /* expression() will choke on anything looking like an (unrecognized)
2554 relocation specifier. Don't even call it, avoiding multiple (and
2555 perhaps redundant) error messages; our caller will issue one. */
2556 ep->X_op = O_illegal;
2557 return 0;
2560 /* Anything inside parentheses or subject to a relocation operator cannot
2561 be a register and hence can be treated the same as operands to
2562 directives (other than .insn). */
2563 if (str_depth || reloc_index)
2564 probing_insn_operands = false;
2566 my_getExpression (ep, crux);
2567 str = expr_parse_end;
2569 probing_insn_operands = orig_probing;
2571 /* Match every open bracket. */
2572 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2573 if (*str++ == ')')
2574 crux_depth--;
2576 if (crux_depth > 0)
2577 as_bad ("unclosed '('");
2579 expr_parse_end = str;
2581 return reloc_index;
2584 /* Parse opcode name, could be an mnemonics or number. */
2586 static size_t
2587 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2588 char *str)
2590 const struct opcode_name_t *o = opcode_name_lookup (&str);
2592 if (o != NULL)
2594 ep->X_op = O_constant;
2595 ep->X_add_number = o->val;
2596 return 0;
2599 return my_getSmallExpression (ep, reloc, str, percent_op_null);
2602 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2603 On exit, EXPR_PARSE_END points to the first character after the
2604 expression. */
2606 static void
2607 my_getVsetvliExpression (expressionS *ep, char *str)
2609 unsigned int vsew_value = 0, vlmul_value = 0;
2610 unsigned int vta_value = 0, vma_value = 0;
2611 bfd_boolean vsew_found = FALSE, vlmul_found = FALSE;
2612 bfd_boolean vta_found = FALSE, vma_found = FALSE;
2614 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew), &vsew_value))
2616 if (*str == ',')
2617 ++str;
2618 if (vsew_found)
2619 as_bad (_("multiple vsew constants"));
2620 vsew_found = TRUE;
2622 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2624 if (*str == ',')
2625 ++str;
2626 if (vlmul_found)
2627 as_bad (_("multiple vlmul constants"));
2628 vlmul_found = TRUE;
2630 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2632 if (*str == ',')
2633 ++str;
2634 if (vta_found)
2635 as_bad (_("multiple vta constants"));
2636 vta_found = TRUE;
2638 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2640 if (*str == ',')
2641 ++str;
2642 if (vma_found)
2643 as_bad (_("multiple vma constants"));
2644 vma_found = TRUE;
2647 if (vsew_found || vlmul_found || vta_found || vma_found)
2649 ep->X_op = O_constant;
2650 ep->X_add_number = (vlmul_value << OP_SH_VLMUL)
2651 | (vsew_value << OP_SH_VSEW)
2652 | (vta_value << OP_SH_VTA)
2653 | (vma_value << OP_SH_VMA);
2654 expr_parse_end = str;
2656 else
2658 my_getExpression (ep, str);
2659 str = expr_parse_end;
2663 /* Parse string STR as a th.vsetvli operand. Store the expression in *EP.
2664 On exit, EXPR_PARSE_END points to the first character after the
2665 expression. */
2667 static void
2668 my_getThVsetvliExpression (expressionS *ep, char *str)
2670 unsigned int vsew_value = 0, vlen_value = 0, vediv_value = 0;
2671 bfd_boolean vsew_found = FALSE, vlen_found = FALSE, vediv_found = FALSE;
2673 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew),
2674 &vsew_value))
2676 if (*str == ',')
2677 ++str;
2678 if (vsew_found)
2679 as_bad (_("multiple vsew constants"));
2680 vsew_found = TRUE;
2683 if (arg_lookup (&str, riscv_th_vlen, ARRAY_SIZE (riscv_th_vlen),
2684 &vlen_value))
2686 if (*str == ',')
2687 ++str;
2688 if (vlen_found)
2689 as_bad (_("multiple vlen constants"));
2690 vlen_found = TRUE;
2692 if (arg_lookup (&str, riscv_th_vediv, ARRAY_SIZE (riscv_th_vediv),
2693 &vediv_value))
2695 if (*str == ',')
2696 ++str;
2697 if (vediv_found)
2698 as_bad (_("multiple vediv constants"));
2699 vediv_found = TRUE;
2702 if (vlen_found || vediv_found || vsew_found)
2704 ep->X_op = O_constant;
2705 ep->X_add_number
2706 = (vediv_value << 5) | (vsew_value << 2) | (vlen_value);
2707 expr_parse_end = str;
2709 else
2711 my_getExpression (ep, str);
2712 str = expr_parse_end;
2716 /* Detect and handle implicitly zero load-store offsets. For example,
2717 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2718 an implicit offset was detected. */
2720 static bool
2721 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
2723 /* Check whether there is only a single bracketed expression left.
2724 If so, it must be the base register and the constant must be zero. */
2725 if (*s == '(' && strchr (s + 1, '(') == 0)
2727 ep->X_op = O_constant;
2728 ep->X_add_number = 0;
2729 return true;
2732 return false;
2735 /* All RISC-V CSR instructions belong to one of these classes. */
2736 enum csr_insn_type
2738 INSN_NOT_CSR,
2739 INSN_CSRRW,
2740 INSN_CSRRS,
2741 INSN_CSRRC
2744 /* Return which CSR instruction is checking. */
2746 static enum csr_insn_type
2747 riscv_csr_insn_type (insn_t insn)
2749 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
2750 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
2751 return INSN_CSRRW;
2752 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2753 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2754 return INSN_CSRRS;
2755 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2756 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2757 return INSN_CSRRC;
2758 else
2759 return INSN_NOT_CSR;
2762 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2763 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2764 CSR address is 0x3. */
2766 static bool
2767 riscv_csr_read_only_check (insn_t insn)
2769 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
2770 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
2771 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
2772 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
2774 if (readonly
2775 && (((csr_insn == INSN_CSRRS
2776 || csr_insn == INSN_CSRRC)
2777 && rs1 != 0)
2778 || csr_insn == INSN_CSRRW))
2779 return false;
2781 return true;
2784 /* Return true if it is a privileged instruction. Otherwise, return false.
2786 uret is actually a N-ext instruction. So it is better to regard it as
2787 an user instruction rather than the priv instruction.
2789 hret is used to return from traps in H-mode. H-mode is removed since
2790 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2791 Therefore, hret should be controlled by the hypervisor spec rather than
2792 priv spec in the future.
2794 dret is defined in the debug spec, so it should be checked in the future,
2795 too. */
2797 static bool
2798 riscv_is_priv_insn (insn_t insn)
2800 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
2801 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
2802 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
2803 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
2804 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2805 check it here to keep the compatible. */
2806 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
2809 static symbolS *deferred_sym_rootP;
2810 static symbolS *deferred_sym_lastP;
2811 /* Since symbols can't easily be freed, try to recycle ones which weren't
2812 committed. */
2813 static symbolS *orphan_sym_rootP;
2814 static symbolS *orphan_sym_lastP;
2816 /* This routine assembles an instruction into its binary format. As a
2817 side effect, it sets the global variable imm_reloc to the type of
2818 relocation to do if one of the operands is an address expression. */
2820 static struct riscv_ip_error
2821 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
2822 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
2824 /* The operand string defined in the riscv_opcodes. */
2825 const char *oparg, *opargStart;
2826 /* The parsed operands from assembly. */
2827 char *asarg, *asargStart;
2828 char save_c = 0;
2829 struct riscv_opcode *insn;
2830 unsigned int regno;
2831 const struct percent_op_match *p;
2832 struct riscv_ip_error error;
2833 error.msg = "unrecognized opcode";
2834 error.statement = str;
2835 error.missing_ext = NULL;
2836 /* Indicate we are assembling instruction with CSR. */
2837 bool insn_with_csr = false;
2839 /* Parse the name of the instruction. Terminate the string if whitespace
2840 is found so that str_hash_find only sees the name part of the string. */
2841 for (asarg = str; *asarg!= '\0'; ++asarg)
2842 if (ISSPACE (*asarg))
2844 save_c = *asarg;
2845 *asarg++ = '\0';
2846 break;
2849 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2851 probing_insn_operands = true;
2853 asargStart = asarg;
2854 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2856 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2857 continue;
2859 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2861 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2862 insn->insn_class);
2863 continue;
2866 /* Reset error message of the previous round. */
2867 error.msg = _("illegal operands");
2868 error.missing_ext = NULL;
2870 /* Purge deferred symbols from the previous round, if any. */
2871 while (deferred_sym_rootP)
2873 symbolS *sym = deferred_sym_rootP;
2875 symbol_remove (sym, &deferred_sym_rootP, &deferred_sym_lastP);
2876 symbol_append (sym, orphan_sym_lastP, &orphan_sym_rootP,
2877 &orphan_sym_lastP);
2880 create_insn (ip, insn);
2882 imm_expr->X_op = O_absent;
2883 *imm_reloc = BFD_RELOC_UNUSED;
2884 p = percent_op_null;
2886 for (oparg = insn->args;; ++oparg)
2888 opargStart = oparg;
2889 asarg += strspn (asarg, " \t");
2890 switch (*oparg)
2892 case '\0': /* End of args. */
2893 if (insn->match_func && !insn->match_func (insn, ip->insn_opcode))
2894 break;
2896 if (insn->pinfo != INSN_MACRO)
2898 /* For .insn, insn->match and insn->mask are 0. */
2899 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2900 ? ip->insn_opcode
2901 : insn->match) == 2
2902 && !riscv_opts.rvc)
2903 break;
2905 if (riscv_is_priv_insn (ip->insn_opcode))
2906 explicit_priv_attr = true;
2908 /* Check if we write a read-only CSR by the CSR
2909 instruction. */
2910 if (insn_with_csr
2911 && riscv_opts.csr_check
2912 && !riscv_csr_read_only_check (ip->insn_opcode))
2914 /* Restore the character in advance, since we want to
2915 report the detailed warning message here. */
2916 if (save_c)
2917 *(asargStart - 1) = save_c;
2918 as_warn (_("read-only CSR is written `%s'"), str);
2919 insn_with_csr = false;
2922 /* The (segmant) load and store with EEW 64 cannot be used
2923 when zve32x is enabled. */
2924 if (ip->insn_mo->pinfo & INSN_V_EEW64
2925 && riscv_subset_supports (&riscv_rps_as, "zve32x")
2926 && !riscv_subset_supports (&riscv_rps_as, "zve64x"))
2928 error.msg = _("illegal opcode for zve32x");
2929 break;
2932 if (*asarg != '\0')
2933 break;
2935 /* Successful assembly. */
2936 error.msg = NULL;
2937 insn_with_csr = false;
2939 /* Commit deferred symbols, if any. */
2940 while (deferred_sym_rootP)
2942 symbolS *sym = deferred_sym_rootP;
2944 symbol_remove (sym, &deferred_sym_rootP,
2945 &deferred_sym_lastP);
2946 symbol_append (sym, symbol_lastP, &symbol_rootP,
2947 &symbol_lastP);
2948 symbol_table_insert (sym);
2950 goto out;
2952 case 'C': /* RVC */
2953 switch (*++oparg)
2955 case 's': /* RS1 x8-x15. */
2956 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2957 || !(regno >= 8 && regno <= 15))
2958 break;
2959 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2960 continue;
2961 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2962 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2963 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2964 break;
2965 continue;
2966 case 't': /* RS2 x8-x15. */
2967 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2968 || !(regno >= 8 && regno <= 15))
2969 break;
2970 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2971 continue;
2972 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2973 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2974 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2975 break;
2976 continue;
2977 case 'U': /* RS1, constrained to equal RD. */
2978 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2979 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2980 break;
2981 continue;
2982 case 'V': /* RS2 */
2983 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2984 break;
2985 INSERT_OPERAND (CRS2, *ip, regno);
2986 continue;
2987 case 'c': /* RS1, constrained to equal sp. */
2988 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2989 || regno != X_SP)
2990 break;
2991 continue;
2992 case 'z': /* RS2, constrained to equal x0. */
2993 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2994 || regno != 0)
2995 break;
2996 continue;
2997 case '>': /* Shift amount, 0 - (XLEN-1). */
2998 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2999 || imm_expr->X_op != O_constant
3000 || (unsigned long) imm_expr->X_add_number >= xlen)
3001 break;
3002 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3003 rvc_imm_done:
3004 asarg = expr_parse_end;
3005 imm_expr->X_op = O_absent;
3006 continue;
3007 case '5':
3008 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3009 || imm_expr->X_op != O_constant
3010 || imm_expr->X_add_number < 0
3011 || imm_expr->X_add_number >= 32
3012 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
3013 break;
3014 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
3015 goto rvc_imm_done;
3016 case '6':
3017 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3018 || imm_expr->X_op != O_constant
3019 || imm_expr->X_add_number < 0
3020 || imm_expr->X_add_number >= 64
3021 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
3022 break;
3023 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
3024 goto rvc_imm_done;
3025 case '8':
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 || imm_expr->X_add_number >= 256
3030 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
3031 break;
3032 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
3033 goto rvc_imm_done;
3034 case 'j':
3035 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3036 || imm_expr->X_op != O_constant
3037 || imm_expr->X_add_number == 0
3038 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
3039 break;
3040 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3041 goto rvc_imm_done;
3042 case 'k':
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_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
3048 break;
3049 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
3050 goto rvc_imm_done;
3051 case 'l':
3052 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3053 continue;
3054 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3055 || imm_expr->X_op != O_constant
3056 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
3057 break;
3058 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
3059 goto rvc_imm_done;
3060 case 'm':
3061 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3062 continue;
3063 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3064 || imm_expr->X_op != O_constant
3065 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
3066 break;
3067 ip->insn_opcode |=
3068 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
3069 goto rvc_imm_done;
3070 case 'n':
3071 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3072 continue;
3073 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3074 || imm_expr->X_op != O_constant
3075 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
3076 break;
3077 ip->insn_opcode |=
3078 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
3079 goto rvc_imm_done;
3080 case 'o':
3081 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3082 || imm_expr->X_op != O_constant
3083 /* C.addiw, c.li, and c.andi allow zero immediate.
3084 C.addi allows zero immediate as hint. Otherwise this
3085 is same as 'j'. */
3086 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
3087 break;
3088 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3089 goto rvc_imm_done;
3090 case 'K':
3091 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3092 || imm_expr->X_op != O_constant
3093 || imm_expr->X_add_number == 0
3094 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
3095 break;
3096 ip->insn_opcode |=
3097 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
3098 goto rvc_imm_done;
3099 case 'L':
3100 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3101 || imm_expr->X_op != O_constant
3102 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
3103 break;
3104 ip->insn_opcode |=
3105 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
3106 goto rvc_imm_done;
3107 case 'M':
3108 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3109 continue;
3110 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3111 || imm_expr->X_op != O_constant
3112 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
3113 break;
3114 ip->insn_opcode |=
3115 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
3116 goto rvc_imm_done;
3117 case 'N':
3118 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3119 continue;
3120 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3121 || imm_expr->X_op != O_constant
3122 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
3123 break;
3124 ip->insn_opcode |=
3125 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
3126 goto rvc_imm_done;
3127 case 'u':
3128 p = percent_op_utype;
3129 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3130 break;
3131 rvc_lui:
3132 if (imm_expr->X_op != O_constant
3133 || imm_expr->X_add_number <= 0
3134 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
3135 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
3136 && (imm_expr->X_add_number <
3137 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
3138 break;
3139 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
3140 goto rvc_imm_done;
3141 case 'v':
3142 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3143 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
3144 || ((int32_t)imm_expr->X_add_number
3145 != imm_expr->X_add_number))
3146 break;
3147 imm_expr->X_add_number =
3148 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
3149 goto rvc_lui;
3150 case 'p':
3151 goto branch;
3152 case 'a':
3153 goto jump;
3154 case 'S': /* Floating-point RS1 x8-x15. */
3155 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
3156 || !(regno >= 8 && regno <= 15))
3157 break;
3158 INSERT_OPERAND (CRS1S, *ip, regno % 8);
3159 continue;
3160 case 'D': /* Floating-point RS2 x8-x15. */
3161 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
3162 || !(regno >= 8 && regno <= 15))
3163 break;
3164 INSERT_OPERAND (CRS2S, *ip, regno % 8);
3165 continue;
3166 case 'T': /* Floating-point RS2. */
3167 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
3168 break;
3169 INSERT_OPERAND (CRS2, *ip, regno);
3170 continue;
3171 case 'F':
3172 switch (*++oparg)
3174 case '6':
3175 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3176 || imm_expr->X_op != O_constant
3177 || imm_expr->X_add_number < 0
3178 || imm_expr->X_add_number >= 64)
3180 as_bad (_("bad value for compressed funct6 "
3181 "field, value must be 0...63"));
3182 break;
3184 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
3185 imm_expr->X_op = O_absent;
3186 asarg = expr_parse_end;
3187 continue;
3189 case '4':
3190 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3191 || imm_expr->X_op != O_constant
3192 || imm_expr->X_add_number < 0
3193 || imm_expr->X_add_number >= 16)
3195 as_bad (_("bad value for compressed funct4 "
3196 "field, value must be 0...15"));
3197 break;
3199 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
3200 imm_expr->X_op = O_absent;
3201 asarg = expr_parse_end;
3202 continue;
3204 case '3':
3205 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3206 || imm_expr->X_op != O_constant
3207 || imm_expr->X_add_number < 0
3208 || imm_expr->X_add_number >= 8)
3210 as_bad (_("bad value for compressed funct3 "
3211 "field, value must be 0...7"));
3212 break;
3214 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
3215 imm_expr->X_op = O_absent;
3216 asarg = expr_parse_end;
3217 continue;
3219 case '2':
3220 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3221 || imm_expr->X_op != O_constant
3222 || imm_expr->X_add_number < 0
3223 || imm_expr->X_add_number >= 4)
3225 as_bad (_("bad value for compressed funct2 "
3226 "field, value must be 0...3"));
3227 break;
3229 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
3230 imm_expr->X_op = O_absent;
3231 asarg = expr_parse_end;
3232 continue;
3234 default:
3235 goto unknown_riscv_ip_operand;
3237 break;
3239 default:
3240 goto unknown_riscv_ip_operand;
3242 break; /* end RVC */
3244 case 'V': /* RVV */
3245 switch (*++oparg)
3247 case 'd': /* VD */
3248 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3249 break;
3250 INSERT_OPERAND (VD, *ip, regno);
3251 continue;
3253 case 'e': /* AMO VD */
3254 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
3255 INSERT_OPERAND (VWD, *ip, 0);
3256 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
3258 INSERT_OPERAND (VWD, *ip, 1);
3259 INSERT_OPERAND (VD, *ip, regno);
3261 else
3262 break;
3263 continue;
3265 case 'f': /* AMO VS3 */
3266 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3267 break;
3268 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
3269 INSERT_OPERAND (VD, *ip, regno);
3270 else
3272 /* VS3 must match VD. */
3273 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
3274 break;
3276 continue;
3278 case 's': /* VS1 */
3279 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3280 break;
3281 INSERT_OPERAND (VS1, *ip, regno);
3282 continue;
3284 case 't': /* VS2 */
3285 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3286 break;
3287 INSERT_OPERAND (VS2, *ip, regno);
3288 continue;
3290 case 'u': /* VS1 == VS2 */
3291 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3292 break;
3293 INSERT_OPERAND (VS1, *ip, regno);
3294 INSERT_OPERAND (VS2, *ip, regno);
3295 continue;
3297 case 'v': /* VD == VS1 == VS2 */
3298 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
3299 break;
3300 INSERT_OPERAND (VD, *ip, regno);
3301 INSERT_OPERAND (VS1, *ip, regno);
3302 INSERT_OPERAND (VS2, *ip, regno);
3303 continue;
3305 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
3306 and is used to choose vs1/rs1/frs1/imm or vs2 for
3307 v[f]merge. It use the same encoding as the vector mask
3308 register. */
3309 case '0':
3310 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
3311 continue;
3312 break;
3314 case 'b': /* vtypei for vsetivli */
3315 my_getVsetvliExpression (imm_expr, asarg);
3316 check_absolute_expr (ip, imm_expr, FALSE);
3317 if (!VALID_RVV_VB_IMM (imm_expr->X_add_number))
3318 as_bad (_("bad value for vsetivli immediate field, "
3319 "value must be 0..1023"));
3320 ip->insn_opcode
3321 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
3322 imm_expr->X_op = O_absent;
3323 asarg = expr_parse_end;
3324 continue;
3326 case 'c': /* vtypei for vsetvli */
3327 my_getVsetvliExpression (imm_expr, asarg);
3328 check_absolute_expr (ip, imm_expr, FALSE);
3329 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
3330 as_bad (_("bad value for vsetvli immediate field, "
3331 "value must be 0..2047"));
3332 ip->insn_opcode
3333 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3334 imm_expr->X_op = O_absent;
3335 asarg = expr_parse_end;
3336 continue;
3338 case 'i': /* vector arith signed immediate */
3339 my_getExpression (imm_expr, asarg);
3340 check_absolute_expr (ip, imm_expr, FALSE);
3341 if (imm_expr->X_add_number > 15
3342 || imm_expr->X_add_number < -16)
3343 as_bad (_("bad value for vector immediate field, "
3344 "value must be -16...15"));
3345 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3346 imm_expr->X_op = O_absent;
3347 asarg = expr_parse_end;
3348 continue;
3350 case 'j': /* vector arith unsigned immediate */
3351 my_getExpression (imm_expr, asarg);
3352 check_absolute_expr (ip, imm_expr, FALSE);
3353 if (imm_expr->X_add_number < 0
3354 || imm_expr->X_add_number >= 32)
3355 as_bad (_("bad value for vector immediate field, "
3356 "value must be 0...31"));
3357 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
3358 imm_expr->X_op = O_absent;
3359 asarg = expr_parse_end;
3360 continue;
3362 case 'k': /* vector arith signed immediate, minus 1 */
3363 my_getExpression (imm_expr, asarg);
3364 check_absolute_expr (ip, imm_expr, FALSE);
3365 if (imm_expr->X_add_number > 16
3366 || imm_expr->X_add_number < -15)
3367 as_bad (_("bad value for vector immediate field, "
3368 "value must be -15...16"));
3369 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number - 1);
3370 imm_expr->X_op = O_absent;
3371 asarg = expr_parse_end;
3372 continue;
3374 case 'l': /* 6-bit vector arith unsigned immediate */
3375 my_getExpression (imm_expr, asarg);
3376 check_absolute_expr (ip, imm_expr, FALSE);
3377 if (imm_expr->X_add_number < 0
3378 || imm_expr->X_add_number >= 64)
3379 as_bad (_("bad value for vector immediate field, "
3380 "value must be 0...63"));
3381 ip->insn_opcode |= ENCODE_RVV_VI_UIMM6 (imm_expr->X_add_number);
3382 imm_expr->X_op = O_absent;
3383 asarg = expr_parse_end;
3384 continue;
3386 case 'm': /* optional vector mask */
3387 if (*asarg == '\0')
3389 INSERT_OPERAND (VMASK, *ip, 1);
3390 continue;
3392 else if (*asarg == ',' && asarg++
3393 && reg_lookup (&asarg, RCLASS_VECM, &regno)
3394 && regno == 0)
3396 INSERT_OPERAND (VMASK, *ip, 0);
3397 continue;
3399 break;
3401 case 'M': /* required vector mask */
3402 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
3404 INSERT_OPERAND (VMASK, *ip, 0);
3405 continue;
3407 break;
3409 case 'T': /* vector macro temporary register */
3410 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
3411 break;
3412 /* Store it in the FUNCT6 field as we don't have anyplace
3413 else to store it. */
3414 INSERT_OPERAND (VFUNCT6, *ip, regno);
3415 continue;
3417 default:
3418 goto unknown_riscv_ip_operand;
3420 break; /* end RVV */
3422 case ',':
3423 if (*asarg++ == *oparg)
3424 continue;
3425 asarg--;
3426 break;
3428 case '(':
3429 case ')':
3430 case '[':
3431 case ']':
3432 case '{':
3433 case '}':
3434 if (*asarg++ == *oparg)
3435 continue;
3436 break;
3438 case '<': /* Shift amount, 0 - 31. */
3439 my_getExpression (imm_expr, asarg);
3440 check_absolute_expr (ip, imm_expr, false);
3441 if ((unsigned long) imm_expr->X_add_number > 31)
3442 as_bad (_("improper shift amount (%"PRIu64")"),
3443 imm_expr->X_add_number);
3444 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
3445 imm_expr->X_op = O_absent;
3446 asarg = expr_parse_end;
3447 continue;
3449 case '>': /* Shift amount, 0 - (XLEN-1). */
3450 my_getExpression (imm_expr, asarg);
3451 check_absolute_expr (ip, imm_expr, false);
3452 if ((unsigned long) imm_expr->X_add_number >= xlen)
3453 as_bad (_("improper shift amount (%"PRIu64")"),
3454 imm_expr->X_add_number);
3455 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
3456 imm_expr->X_op = O_absent;
3457 asarg = expr_parse_end;
3458 continue;
3460 case 'Z': /* CSRRxI immediate. */
3461 my_getExpression (imm_expr, asarg);
3462 check_absolute_expr (ip, imm_expr, false);
3463 if ((unsigned long) imm_expr->X_add_number > 31)
3464 as_bad (_("improper CSRxI immediate (%"PRIu64")"),
3465 imm_expr->X_add_number);
3466 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
3467 imm_expr->X_op = O_absent;
3468 asarg = expr_parse_end;
3469 continue;
3471 case 'E': /* Control register. */
3472 insn_with_csr = true;
3473 explicit_priv_attr = true;
3474 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
3475 INSERT_OPERAND (CSR, *ip, regno);
3476 else
3478 my_getExpression (imm_expr, asarg);
3479 check_absolute_expr (ip, imm_expr, true);
3480 if ((unsigned long) imm_expr->X_add_number > 0xfff)
3481 as_bad (_("improper CSR address (%"PRIu64")"),
3482 imm_expr->X_add_number);
3483 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
3484 imm_expr->X_op = O_absent;
3485 asarg = expr_parse_end;
3487 continue;
3489 case 'm': /* Rounding mode. */
3490 if (arg_lookup (&asarg, riscv_rm,
3491 ARRAY_SIZE (riscv_rm), &regno))
3493 INSERT_OPERAND (RM, *ip, regno);
3494 continue;
3496 break;
3498 case 'P':
3499 case 'Q': /* Fence predecessor/successor. */
3500 if (arg_lookup (&asarg, riscv_pred_succ,
3501 ARRAY_SIZE (riscv_pred_succ), &regno))
3503 if (*oparg == 'P')
3504 INSERT_OPERAND (PRED, *ip, regno);
3505 else
3506 INSERT_OPERAND (SUCC, *ip, regno);
3507 continue;
3509 break;
3511 case 'd': /* Destination register. */
3512 case 's': /* Source register. */
3513 case 't': /* Target register. */
3514 case 'r': /* RS3 */
3515 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
3517 char c = *oparg;
3518 if (*asarg == ' ')
3519 ++asarg;
3521 /* Now that we have assembled one operand, we use the args
3522 string to figure out where it goes in the instruction. */
3523 switch (c)
3525 case 's':
3526 INSERT_OPERAND (RS1, *ip, regno);
3527 break;
3528 case 'd':
3529 INSERT_OPERAND (RD, *ip, regno);
3530 break;
3531 case 't':
3532 INSERT_OPERAND (RS2, *ip, regno);
3533 break;
3534 case 'r':
3535 INSERT_OPERAND (RS3, *ip, regno);
3536 break;
3538 continue;
3540 break;
3542 case 'D': /* Floating point RD. */
3543 case 'S': /* Floating point RS1. */
3544 case 'T': /* Floating point RS2. */
3545 case 'U': /* Floating point RS1 and RS2. */
3546 case 'R': /* Floating point RS3. */
3547 if (reg_lookup (&asarg,
3548 (riscv_subset_supports (&riscv_rps_as, "zfinx")
3549 ? RCLASS_GPR : RCLASS_FPR), &regno))
3551 char c = *oparg;
3552 if (*asarg == ' ')
3553 ++asarg;
3554 switch (c)
3556 case 'D':
3557 INSERT_OPERAND (RD, *ip, regno);
3558 break;
3559 case 'S':
3560 INSERT_OPERAND (RS1, *ip, regno);
3561 break;
3562 case 'U':
3563 INSERT_OPERAND (RS1, *ip, regno);
3564 /* Fall through. */
3565 case 'T':
3566 INSERT_OPERAND (RS2, *ip, regno);
3567 break;
3568 case 'R':
3569 INSERT_OPERAND (RS3, *ip, regno);
3570 break;
3572 continue;
3574 break;
3576 case 'I':
3577 my_getExpression (imm_expr, asarg);
3578 if (imm_expr->X_op != O_big
3579 && imm_expr->X_op != O_constant)
3580 break;
3581 normalize_constant_expr (imm_expr);
3582 asarg = expr_parse_end;
3583 continue;
3585 case 'A':
3586 my_getExpression (imm_expr, asarg);
3587 normalize_constant_expr (imm_expr);
3588 /* The 'A' format specifier must be a symbol. */
3589 if (imm_expr->X_op != O_symbol)
3590 break;
3591 *imm_reloc = BFD_RELOC_32;
3592 asarg = expr_parse_end;
3593 continue;
3595 case 'B':
3596 my_getExpression (imm_expr, asarg);
3597 normalize_constant_expr (imm_expr);
3598 /* The 'B' format specifier must be a symbol or a constant. */
3599 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
3600 break;
3601 if (imm_expr->X_op == O_symbol)
3602 *imm_reloc = BFD_RELOC_32;
3603 asarg = expr_parse_end;
3604 continue;
3606 case 'j': /* Sign-extended immediate. */
3607 p = percent_op_itype;
3608 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3609 goto alu_op;
3610 case 'q': /* Store displacement. */
3611 p = percent_op_stype;
3612 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3613 goto load_store;
3614 case 'o': /* Load displacement. */
3615 p = percent_op_itype;
3616 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3617 goto load_store;
3618 case '1':
3619 /* This is used for TLS relocations that acts as relaxation
3620 markers and do not change the instruction encoding,
3621 i.e. %tprel_add and %tlsdesc_call. */
3622 p = percent_op_relax_only;
3623 goto alu_op;
3624 case '0': /* AMO displacement, which must be zero. */
3625 load_store:
3626 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3627 continue;
3628 alu_op:
3629 /* If this value won't fit into a 16 bit offset, then go
3630 find a macro that will generate the 32 bit offset
3631 code pattern. */
3632 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3634 normalize_constant_expr (imm_expr);
3635 if (imm_expr->X_op != O_constant
3636 || (*oparg == '0' && imm_expr->X_add_number != 0)
3637 || (*oparg == '1')
3638 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3639 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3640 break;
3642 asarg = expr_parse_end;
3643 continue;
3645 case 'p': /* PC-relative offset. */
3646 branch:
3647 *imm_reloc = BFD_RELOC_12_PCREL;
3648 my_getExpression (imm_expr, asarg);
3649 asarg = expr_parse_end;
3650 continue;
3652 case 'u': /* Upper 20 bits. */
3653 p = percent_op_utype;
3654 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3656 if (imm_expr->X_op != O_constant)
3657 break;
3659 if (imm_expr->X_add_number < 0
3660 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
3661 as_bad (_("lui expression not in range 0..1048575"));
3663 *imm_reloc = BFD_RELOC_RISCV_HI20;
3664 imm_expr->X_add_number <<= RISCV_IMM_BITS;
3666 asarg = expr_parse_end;
3667 continue;
3669 case 'a': /* 20-bit PC-relative offset. */
3670 jump:
3671 my_getExpression (imm_expr, asarg);
3672 asarg = expr_parse_end;
3673 *imm_reloc = BFD_RELOC_RISCV_JMP;
3674 continue;
3676 case 'c':
3677 my_getExpression (imm_expr, asarg);
3678 asarg = expr_parse_end;
3679 if (strcmp (asarg, "@plt") == 0)
3680 asarg += 4;
3681 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3682 continue;
3684 case 'O':
3685 switch (*++oparg)
3687 case '4':
3688 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3689 || imm_expr->X_op != O_constant
3690 || imm_expr->X_add_number < 0
3691 || imm_expr->X_add_number >= 128
3692 || (imm_expr->X_add_number & 0x3) != 3)
3694 as_bad (_("bad value for opcode field, "
3695 "value must be 0...127 and "
3696 "lower 2 bits must be 0x3"));
3697 break;
3699 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3700 imm_expr->X_op = O_absent;
3701 asarg = expr_parse_end;
3702 continue;
3704 case '2':
3705 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg)
3706 || imm_expr->X_op != O_constant
3707 || imm_expr->X_add_number < 0
3708 || imm_expr->X_add_number >= 3)
3710 as_bad (_("bad value for opcode field, "
3711 "value must be 0...2"));
3712 break;
3714 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3715 imm_expr->X_op = O_absent;
3716 asarg = expr_parse_end;
3717 continue;
3719 default:
3720 goto unknown_riscv_ip_operand;
3722 break;
3724 case 'F':
3725 switch (*++oparg)
3727 case '7':
3728 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3729 || imm_expr->X_op != O_constant
3730 || imm_expr->X_add_number < 0
3731 || imm_expr->X_add_number >= 128)
3733 as_bad (_("bad value for funct7 field, "
3734 "value must be 0...127"));
3735 break;
3737 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3738 imm_expr->X_op = O_absent;
3739 asarg = expr_parse_end;
3740 continue;
3742 case '3':
3743 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3744 || imm_expr->X_op != O_constant
3745 || imm_expr->X_add_number < 0
3746 || imm_expr->X_add_number >= 8)
3748 as_bad (_("bad value for funct3 field, "
3749 "value must be 0...7"));
3750 break;
3752 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3753 imm_expr->X_op = O_absent;
3754 asarg = expr_parse_end;
3755 continue;
3757 case '2':
3758 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3759 || imm_expr->X_op != O_constant
3760 || imm_expr->X_add_number < 0
3761 || imm_expr->X_add_number >= 4)
3763 as_bad (_("bad value for funct2 field, "
3764 "value must be 0...3"));
3765 break;
3767 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3768 imm_expr->X_op = O_absent;
3769 asarg = expr_parse_end;
3770 continue;
3772 default:
3773 goto unknown_riscv_ip_operand;
3775 break;
3777 case 'y': /* bs immediate */
3778 my_getExpression (imm_expr, asarg);
3779 check_absolute_expr (ip, imm_expr, FALSE);
3780 if ((unsigned long)imm_expr->X_add_number > 3)
3781 as_bad(_("Improper bs immediate (%lu)"),
3782 (unsigned long)imm_expr->X_add_number);
3783 INSERT_OPERAND(BS, *ip, imm_expr->X_add_number);
3784 imm_expr->X_op = O_absent;
3785 asarg = expr_parse_end;
3786 continue;
3788 case 'Y': /* rnum immediate */
3789 my_getExpression (imm_expr, asarg);
3790 check_absolute_expr (ip, imm_expr, FALSE);
3791 if ((unsigned long)imm_expr->X_add_number > 10)
3792 as_bad(_("Improper rnum immediate (%lu)"),
3793 (unsigned long)imm_expr->X_add_number);
3794 INSERT_OPERAND(RNUM, *ip, imm_expr->X_add_number);
3795 imm_expr->X_op = O_absent;
3796 asarg = expr_parse_end;
3797 continue;
3799 case 'z':
3800 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3801 || imm_expr->X_op != O_constant
3802 || imm_expr->X_add_number != 0)
3803 break;
3804 asarg = expr_parse_end;
3805 imm_expr->X_op = O_absent;
3806 continue;
3808 case 'W': /* Various operands for standard z extensions. */
3809 switch (*++oparg)
3811 case 'i':
3812 switch (*++oparg)
3814 case 'f':
3815 /* Prefetch offset for 'Zicbop' extension.
3816 pseudo S-type but lower 5-bits zero. */
3817 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3818 continue;
3819 my_getExpression (imm_expr, asarg);
3820 check_absolute_expr (ip, imm_expr, false);
3821 if (((unsigned) (imm_expr->X_add_number) & 0x1fU)
3822 || imm_expr->X_add_number >= RISCV_IMM_REACH / 2
3823 || imm_expr->X_add_number < -RISCV_IMM_REACH / 2)
3824 as_bad (_ ("improper prefetch offset (%ld)"),
3825 (long) imm_expr->X_add_number);
3826 ip->insn_opcode |= ENCODE_STYPE_IMM (
3827 (unsigned) (imm_expr->X_add_number) & ~0x1fU);
3828 imm_expr->X_op = O_absent;
3829 asarg = expr_parse_end;
3830 continue;
3831 default:
3832 goto unknown_riscv_ip_operand;
3834 break;
3836 case 'f':
3837 switch (*++oparg)
3839 case 'v':
3840 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3841 if (!arg_lookup (&asarg, riscv_fli_symval,
3842 ARRAY_SIZE (riscv_fli_symval), &regno))
3844 /* 0.0 is not a valid entry in riscv_fli_numval. */
3845 errno = 0;
3846 float f = strtof (asarg, &asarg);
3847 if (errno != 0 || f == 0.0
3848 || !flt_lookup (f, riscv_fli_numval,
3849 ARRAY_SIZE(riscv_fli_numval),
3850 &regno))
3852 as_bad (_("bad fli constant operand, "
3853 "supported constants must be in "
3854 "decimal or hexadecimal floating-point "
3855 "literal form"));
3856 break;
3859 INSERT_OPERAND (RS1, *ip, regno);
3860 continue;
3861 default:
3862 goto unknown_riscv_ip_operand;
3864 break;
3866 case 'c':
3867 switch (*++oparg)
3869 case 'h': /* Immediate field for c.lh/c.lhu/c.sh. */
3870 /* Handle cases, such as c.sh rs2', (rs1'). */
3871 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3872 continue;
3873 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3874 || imm_expr->X_op != O_constant
3875 || !VALID_ZCB_HALFWORD_UIMM ((valueT) imm_expr->X_add_number))
3876 break;
3877 ip->insn_opcode |= ENCODE_ZCB_HALFWORD_UIMM (imm_expr->X_add_number);
3878 goto rvc_imm_done;
3879 case 'b': /* Immediate field for c.lbu/c.sb. */
3880 /* Handle cases, such as c.lbu rd', (rs1'). */
3881 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3882 continue;
3883 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3884 || imm_expr->X_op != O_constant
3885 || !VALID_ZCB_BYTE_UIMM ((valueT) imm_expr->X_add_number))
3886 break;
3887 ip->insn_opcode |= ENCODE_ZCB_BYTE_UIMM (imm_expr->X_add_number);
3888 goto rvc_imm_done;
3889 case 'r':
3890 if (!reglist_lookup (&asarg, &regno))
3891 break;
3892 INSERT_OPERAND (REG_LIST, *ip, regno);
3893 continue;
3894 case 'p':
3895 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3896 || imm_expr->X_op != O_constant)
3897 break;
3898 /* Convert stack adjustment of cm.push to a positive
3899 offset. */
3900 if (ip->insn_mo->match == MATCH_CM_PUSH)
3901 imm_expr->X_add_number *= -1;
3902 /* Subtract base stack adjustment and get spimm. */
3903 imm_expr->X_add_number -=
3904 riscv_get_sp_base (ip->insn_opcode, *riscv_rps_as.xlen);
3905 if (!VALID_ZCMP_SPIMM (imm_expr->X_add_number))
3906 break;
3907 ip->insn_opcode |=
3908 ENCODE_ZCMP_SPIMM (imm_expr->X_add_number);
3909 goto rvc_imm_done;
3910 case 'f': /* Operand for matching immediate 255. */
3911 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3912 || imm_expr->X_op != O_constant
3913 || imm_expr->X_add_number != 255)
3914 break;
3915 /* This operand is used for matching immediate 255, and
3916 we do not write anything to encoding by this operand. */
3917 asarg = expr_parse_end;
3918 imm_expr->X_op = O_absent;
3919 continue;
3920 case '1':
3921 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
3922 || !RISCV_SREG_0_7 (regno))
3923 break;
3924 INSERT_OPERAND (SREG1, *ip, regno % 8);
3925 continue;
3926 case '2':
3927 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
3928 || !RISCV_SREG_0_7 (regno))
3929 break;
3930 INSERT_OPERAND (SREG2, *ip, regno % 8);
3931 continue;
3932 case 'I': /* index operand of cm.jt. The range is from 0 to 31. */
3933 my_getSmallExpression (imm_expr, imm_reloc, asarg, p);
3934 if (imm_expr->X_op != O_constant
3935 || imm_expr->X_add_number < 0
3936 || imm_expr->X_add_number > 31)
3938 as_bad ("bad index value for cm.jt, range: [0, 31]");
3939 break;
3941 ip->insn_opcode |= ENCODE_ZCMT_INDEX (imm_expr->X_add_number);
3942 goto rvc_imm_done;
3943 case 'i': /* index operand of cm.jalt. The range is from 32 to 255. */
3944 my_getSmallExpression (imm_expr, imm_reloc, asarg, p);
3945 if (imm_expr->X_op != O_constant
3946 || imm_expr->X_add_number < 32
3947 || imm_expr->X_add_number > 255)
3949 as_bad ("bad index value for cm.jalt, range: [32, 255]");
3950 break;
3952 ip->insn_opcode |= ENCODE_ZCMT_INDEX (imm_expr->X_add_number);
3953 goto rvc_imm_done;
3954 default:
3955 goto unknown_riscv_ip_operand;
3957 break;
3959 default:
3960 goto unknown_riscv_ip_operand;
3962 break;
3964 case 'X': /* Vendor-specific operands. */
3965 switch (*++oparg)
3967 case 't': /* Vendor-specific (T-head) operands. */
3969 size_t n;
3970 size_t s;
3971 bool sign;
3972 switch (*++oparg)
3974 case 'V':
3975 /* Vtypei for th.vsetvli. */
3976 ++oparg;
3977 if (*oparg != 'c')
3978 goto unknown_riscv_ip_operand;
3980 my_getThVsetvliExpression (imm_expr, asarg);
3981 check_absolute_expr (ip, imm_expr, FALSE);
3982 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
3983 as_bad (_("bad value for th.vsetvli immediate field, "
3984 "value must be 0..2047"));
3985 ip->insn_opcode
3986 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
3987 imm_expr->X_op = O_absent;
3988 asarg = expr_parse_end;
3989 continue;
3991 case 'l': /* Integer immediate, literal. */
3992 n = strcspn (++oparg, ",");
3993 if (strncmp (oparg, asarg, n))
3994 as_bad (_("unexpected literal (%s)"), asarg);
3995 oparg += n - 1;
3996 asarg += n;
3997 continue;
3998 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
3999 sign = true;
4000 goto parse_imm;
4001 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
4002 sign = false;
4003 goto parse_imm;
4004 parse_imm:
4005 n = strtol (oparg + 1, (char **)&oparg, 10);
4006 if (*oparg != '@')
4007 goto unknown_riscv_ip_operand;
4008 s = strtol (oparg + 1, (char **)&oparg, 10);
4009 oparg--;
4011 my_getExpression (imm_expr, asarg);
4012 check_absolute_expr (ip, imm_expr, false);
4013 if (!sign)
4015 if (!VALIDATE_U_IMM (imm_expr->X_add_number, n))
4016 as_bad (_("improper immediate value (%"PRIu64")"),
4017 imm_expr->X_add_number);
4019 else
4021 if (!VALIDATE_S_IMM (imm_expr->X_add_number, n))
4022 as_bad (_("improper immediate value (%"PRIi64")"),
4023 imm_expr->X_add_number);
4025 INSERT_IMM (n, s, *ip, imm_expr->X_add_number);
4026 imm_expr->X_op = O_absent;
4027 asarg = expr_parse_end;
4028 continue;
4029 default:
4030 goto unknown_riscv_ip_operand;
4033 break;
4035 case 'c': /* Vendor-specific (CORE-V) operands. */
4036 switch (*++oparg)
4038 case '2':
4039 my_getExpression (imm_expr, asarg);
4040 check_absolute_expr (ip, imm_expr, FALSE);
4041 asarg = expr_parse_end;
4042 if (imm_expr->X_add_number<0
4043 || imm_expr->X_add_number>31)
4044 break;
4045 ip->insn_opcode
4046 |= ENCODE_CV_IS2_UIMM5 (imm_expr->X_add_number);
4047 continue;
4048 case '3':
4049 my_getExpression (imm_expr, asarg);
4050 check_absolute_expr (ip, imm_expr, FALSE);
4051 asarg = expr_parse_end;
4052 if (imm_expr->X_add_number < 0
4053 || imm_expr->X_add_number > 31)
4054 break;
4055 ip->insn_opcode
4056 |= ENCODE_CV_IS3_UIMM5 (imm_expr->X_add_number);
4057 continue;
4058 case '4':
4059 my_getExpression (imm_expr, asarg);
4060 check_absolute_expr (ip, imm_expr, FALSE);
4061 asarg = expr_parse_end;
4062 if (imm_expr->X_add_number < -16
4063 || imm_expr->X_add_number > 15)
4064 break;
4065 ip->insn_opcode
4066 |= ENCODE_CV_IS2_UIMM5 (imm_expr->X_add_number);
4067 continue;
4068 case '5':
4069 my_getExpression (imm_expr, asarg);
4070 check_absolute_expr (ip, imm_expr, FALSE);
4071 asarg = expr_parse_end;
4072 if (imm_expr->X_add_number < -32
4073 || imm_expr->X_add_number > 31)
4074 break;
4075 ip->insn_opcode
4076 |= ENCODE_CV_SIMD_IMM6 (imm_expr->X_add_number);
4077 continue;
4078 case '6':
4079 my_getExpression (imm_expr, asarg);
4080 check_absolute_expr (ip, imm_expr, FALSE);
4081 asarg = expr_parse_end;
4082 if (imm_expr->X_add_number < 0
4083 || imm_expr->X_add_number > 31)
4084 break;
4085 ip->insn_opcode
4086 |= ENCODE_CV_BITMANIP_UIMM5 (imm_expr->X_add_number);
4087 continue;
4088 case '7':
4089 my_getExpression (imm_expr, asarg);
4090 check_absolute_expr (ip, imm_expr, FALSE);
4091 asarg = expr_parse_end;
4092 if (imm_expr->X_add_number < 0
4093 || imm_expr->X_add_number > 3)
4094 break;
4095 ip->insn_opcode
4096 |= ENCODE_CV_BITMANIP_UIMM2 (imm_expr->X_add_number);
4097 continue;
4098 case '8':
4099 my_getExpression (imm_expr, asarg);
4100 check_absolute_expr (ip, imm_expr, FALSE);
4101 asarg = expr_parse_end;
4102 ++oparg;
4103 if (imm_expr->X_add_number < 0
4104 || imm_expr->X_add_number > 63)
4105 break;
4106 else if (*oparg == '1'
4107 && imm_expr->X_add_number > 1)
4108 break;
4109 else if (*oparg == '2'
4110 && imm_expr->X_add_number > 3)
4111 break;
4112 else if (*oparg == '3'
4113 && imm_expr->X_add_number > 7)
4114 break;
4115 else if (*oparg == '4'
4116 && imm_expr->X_add_number > 15)
4117 break;
4118 ip->insn_opcode
4119 |= ENCODE_CV_SIMD_UIMM6 (imm_expr->X_add_number);
4120 continue;
4121 default:
4122 goto unknown_riscv_ip_operand;
4124 break;
4126 case 's': /* Vendor-specific (SiFive) operands. */
4127 #define UIMM_BITFIELD_VAL(S, E) (1 << ((E) - (S) + 1))
4128 #define ENCODE_UIMM_BIT_FIELD(NAME, IP, EXPR, RELOC, ASARG, \
4129 START, END) \
4130 do \
4132 if (my_getOpcodeExpression (EXPR, RELOC, ASARG) \
4133 || EXPR->X_op != O_constant \
4134 || EXPR->X_add_number < 0 \
4135 || EXPR->X_add_number >= UIMM_BITFIELD_VAL (START, END)) \
4137 as_bad (_("bad value for <bit-%s-%s> " \
4138 "field, value must be 0...%d"), \
4139 #START, #END, UIMM_BITFIELD_VAL (START, END)); \
4140 break; \
4142 INSERT_OPERAND (NAME, *IP, EXPR->X_add_number); \
4143 EXPR->X_op = O_absent; \
4144 ASARG = expr_parse_end; \
4146 while (0);
4147 switch (*++oparg)
4149 case 'd': /* Xsd */
4150 ENCODE_UIMM_BIT_FIELD
4151 (RD, ip, imm_expr, imm_reloc, asarg, 7, 11);
4152 continue;
4153 case 't': /* Xst */
4154 ENCODE_UIMM_BIT_FIELD
4155 (RS2, ip, imm_expr, imm_reloc, asarg, 20, 24)
4156 continue;
4157 case 'O':
4158 switch (*++oparg)
4160 case '2': /* XsO2 */
4161 ENCODE_UIMM_BIT_FIELD
4162 (XSO2, ip, imm_expr, imm_reloc, asarg, 26, 27);
4163 continue;
4164 case '1': /* XsO1 */
4165 ENCODE_UIMM_BIT_FIELD
4166 (XSO1, ip, imm_expr, imm_reloc, asarg, 26, 26);
4167 continue;
4169 default:
4170 goto unknown_riscv_ip_operand;
4172 #undef UIMM_BITFIELD_VAL
4173 #undef ENCODE_UIMM_BIT_FIELD
4174 break;
4176 default:
4177 goto unknown_riscv_ip_operand;
4179 break;
4181 default:
4182 unknown_riscv_ip_operand:
4183 as_fatal (_("internal: unknown argument type `%s'"),
4184 opargStart);
4186 break;
4188 asarg = asargStart;
4189 insn_with_csr = false;
4192 out:
4193 /* Restore the character we might have clobbered above. */
4194 if (save_c)
4195 *(asargStart - 1) = save_c;
4197 probing_insn_operands = false;
4199 return error;
4202 /* Similar to riscv_ip, but assembles an instruction according to the
4203 hardcode values of .insn directive. */
4205 static const char *
4206 riscv_ip_hardcode (char *str,
4207 struct riscv_cl_insn *ip,
4208 expressionS *imm_expr,
4209 const char *error)
4211 struct riscv_opcode *insn;
4212 insn_t values[2] = {0, 0};
4213 unsigned int num = 0;
4215 input_line_pointer = str;
4218 expression (imm_expr);
4219 switch (imm_expr->X_op)
4221 case O_constant:
4222 values[num++] = (insn_t) imm_expr->X_add_number;
4223 break;
4224 case O_big:
4225 /* Extract lower 32-bits of a big number.
4226 Assume that generic_bignum_to_int32 work on such number. */
4227 values[num++] = (insn_t) generic_bignum_to_int32 ();
4228 break;
4229 default:
4230 /* The first value isn't constant, so it should be
4231 .insn <type> <operands>. We have been parsed it
4232 in the riscv_ip. */
4233 if (num == 0)
4234 return error;
4235 return _("values must be constant");
4238 while (*input_line_pointer++ == ',' && num < 2 && imm_expr->X_op != O_big);
4240 input_line_pointer--;
4241 if (*input_line_pointer != '\0')
4242 return _("unrecognized values");
4244 insn = XCNEW (struct riscv_opcode);
4245 insn->match = values[num - 1];
4246 create_insn (ip, insn);
4247 unsigned int bytes = riscv_insn_length (insn->match);
4249 if (num == 2 && values[0] != bytes)
4250 return _("value conflicts with instruction length");
4252 if (imm_expr->X_op == O_big)
4254 unsigned int llen = 0;
4255 for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
4256 lval != 0; llen++)
4257 lval >>= BITS_PER_CHAR;
4258 unsigned int repr_bytes
4259 = (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
4260 if (bytes < repr_bytes)
4261 return _("value conflicts with instruction length");
4262 for (num = 0; num < imm_expr->X_add_number - 1; ++num)
4263 number_to_chars_littleendian (
4264 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
4265 generic_bignum[num],
4266 CHARS_PER_LITTLENUM);
4267 if (llen != 0)
4268 number_to_chars_littleendian (
4269 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
4270 generic_bignum[num],
4271 llen);
4272 memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
4274 else if (bytes < sizeof(values[0]) && values[num - 1] >> (8 * bytes) != 0)
4275 return _("value conflicts with instruction length");
4277 if (!riscv_opts.rvc && (bytes & 2))
4278 seg_info (now_seg)->tc_segment_info_data.last_insn16 = true;
4280 return NULL;
4283 void
4284 md_assemble (char *str)
4286 struct riscv_cl_insn insn;
4287 expressionS imm_expr;
4288 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
4290 /* The architecture and privileged elf attributes should be set
4291 before assembling. */
4292 if (!start_assemble)
4294 start_assemble = true;
4296 riscv_set_abi_by_arch ();
4297 if (!riscv_set_default_priv_spec (NULL))
4298 return;
4301 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
4303 const struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
4304 &imm_reloc, op_hash);
4306 if (error.msg)
4308 if (error.missing_ext)
4309 as_bad ("%s `%s', extension `%s' required", error.msg,
4310 error.statement, error.missing_ext);
4311 else
4312 as_bad ("%s `%s'", error.msg, error.statement);
4313 return;
4316 if (insn.insn_mo->pinfo == INSN_MACRO)
4317 macro (&insn, &imm_expr, &imm_reloc);
4318 else
4319 append_insn (&insn, &imm_expr, imm_reloc);
4322 const char *
4323 md_atof (int type, char *litP, int *sizeP)
4325 return ieee_md_atof (type, litP, sizeP, target_big_endian);
4328 void
4329 md_number_to_chars (char *buf, valueT val, int n)
4331 if (target_big_endian)
4332 number_to_chars_bigendian (buf, val, n);
4333 else
4334 number_to_chars_littleendian (buf, val, n);
4337 const char md_shortopts[] = "O::g::G:";
4339 enum options
4341 OPTION_MARCH = OPTION_MD_BASE,
4342 OPTION_PIC,
4343 OPTION_NO_PIC,
4344 OPTION_MABI,
4345 OPTION_RELAX,
4346 OPTION_NO_RELAX,
4347 OPTION_ARCH_ATTR,
4348 OPTION_NO_ARCH_ATTR,
4349 OPTION_CSR_CHECK,
4350 OPTION_NO_CSR_CHECK,
4351 OPTION_MISA_SPEC,
4352 OPTION_MPRIV_SPEC,
4353 OPTION_BIG_ENDIAN,
4354 OPTION_LITTLE_ENDIAN,
4355 OPTION_END_OF_ENUM
4358 const struct option md_longopts[] =
4360 {"march", required_argument, NULL, OPTION_MARCH},
4361 {"fPIC", no_argument, NULL, OPTION_PIC},
4362 {"fpic", no_argument, NULL, OPTION_PIC},
4363 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
4364 {"mabi", required_argument, NULL, OPTION_MABI},
4365 {"mrelax", no_argument, NULL, OPTION_RELAX},
4366 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
4367 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
4368 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
4369 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
4370 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
4371 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
4372 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
4373 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
4374 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
4376 {NULL, no_argument, NULL, 0}
4378 const size_t md_longopts_size = sizeof (md_longopts);
4381 md_parse_option (int c, const char *arg)
4383 switch (c)
4385 case OPTION_MARCH:
4386 /* List all avaiable extensions. */
4387 if (strcmp (arg, "help") == 0)
4389 riscv_print_extensions ();
4390 exit (EXIT_SUCCESS);
4392 default_arch_with_ext = arg;
4393 break;
4395 case OPTION_NO_PIC:
4396 riscv_opts.pic = false;
4397 break;
4399 case OPTION_PIC:
4400 riscv_opts.pic = true;
4401 break;
4403 case OPTION_MABI:
4404 if (strcmp (arg, "ilp32") == 0)
4405 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
4406 else if (strcmp (arg, "ilp32e") == 0)
4407 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
4408 else if (strcmp (arg, "ilp32f") == 0)
4409 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
4410 else if (strcmp (arg, "ilp32d") == 0)
4411 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
4412 else if (strcmp (arg, "ilp32q") == 0)
4413 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
4414 else if (strcmp (arg, "lp64") == 0)
4415 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
4416 else if (strcmp (arg, "lp64e") == 0)
4417 riscv_set_abi (64, FLOAT_ABI_SOFT, true);
4418 else if (strcmp (arg, "lp64f") == 0)
4419 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
4420 else if (strcmp (arg, "lp64d") == 0)
4421 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
4422 else if (strcmp (arg, "lp64q") == 0)
4423 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
4424 else
4425 return 0;
4426 explicit_mabi = true;
4427 break;
4429 case OPTION_RELAX:
4430 riscv_opts.relax = true;
4431 break;
4433 case OPTION_NO_RELAX:
4434 riscv_opts.relax = false;
4435 break;
4437 case OPTION_ARCH_ATTR:
4438 riscv_opts.arch_attr = true;
4439 break;
4441 case OPTION_NO_ARCH_ATTR:
4442 riscv_opts.arch_attr = false;
4443 break;
4445 case OPTION_CSR_CHECK:
4446 riscv_opts.csr_check = true;
4447 break;
4449 case OPTION_NO_CSR_CHECK:
4450 riscv_opts.csr_check = false;
4451 break;
4453 case OPTION_MISA_SPEC:
4454 return riscv_set_default_isa_spec (arg);
4456 case OPTION_MPRIV_SPEC:
4457 return riscv_set_default_priv_spec (arg);
4459 case OPTION_BIG_ENDIAN:
4460 target_big_endian = 1;
4461 break;
4463 case OPTION_LITTLE_ENDIAN:
4464 target_big_endian = 0;
4465 break;
4467 default:
4468 return 0;
4471 return 1;
4474 void
4475 riscv_after_parse_args (void)
4477 /* The --with-arch is optional for now, so we still need to set the xlen
4478 according to the default_arch, which is set by the --target. */
4479 if (xlen == 0)
4481 if (strcmp (default_arch, "riscv32") == 0)
4482 xlen = 32;
4483 else if (strcmp (default_arch, "riscv64") == 0)
4484 xlen = 64;
4485 else
4486 as_bad ("unknown default architecture `%s'", default_arch);
4489 /* Set default specs. */
4490 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
4491 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
4492 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
4493 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
4495 riscv_set_arch (default_arch_with_ext);
4497 /* If the CIE to be produced has not been overridden on the command line,
4498 then produce version 3 by default. This allows us to use the full
4499 range of registers in a .cfi_return_column directive. */
4500 if (flag_dwarf_cie_version == -1)
4501 flag_dwarf_cie_version = 3;
4504 bool riscv_parse_name (const char *name, struct expressionS *ep,
4505 enum expr_mode mode)
4507 unsigned int regno;
4508 symbolS *sym;
4510 if (!probing_insn_operands)
4511 return false;
4513 gas_assert (mode == expr_normal);
4515 regno = reg_lookup_internal (name, RCLASS_GPR);
4516 if (regno == (unsigned int)-1)
4517 return false;
4519 if (symbol_find (name) != NULL)
4520 return false;
4522 /* Create a symbol without adding it to the symbol table yet.
4523 Insertion will happen only once we commit to using the insn
4524 we're probing operands for. */
4525 for (sym = deferred_sym_rootP; sym; sym = symbol_next (sym))
4526 if (strcmp (name, S_GET_NAME (sym)) == 0)
4527 break;
4528 if (!sym)
4530 for (sym = orphan_sym_rootP; sym; sym = symbol_next (sym))
4531 if (strcmp (name, S_GET_NAME (sym)) == 0)
4533 symbol_remove (sym, &orphan_sym_rootP, &orphan_sym_lastP);
4534 break;
4536 if (!sym)
4537 sym = symbol_create (name, undefined_section,
4538 &zero_address_frag, 0);
4540 symbol_append (sym, deferred_sym_lastP, &deferred_sym_rootP,
4541 &deferred_sym_lastP);
4544 ep->X_op = O_symbol;
4545 ep->X_add_symbol = sym;
4546 ep->X_add_number = 0;
4548 return true;
4551 long
4552 md_pcrel_from (fixS *fixP)
4554 return fixP->fx_where + fixP->fx_frag->fr_address;
4557 /* Apply a fixup to the object file. */
4559 void
4560 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
4562 unsigned int subtype;
4563 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
4564 bool relaxable = false;
4565 offsetT loc;
4566 segT sub_segment;
4568 /* Remember value for tc_gen_reloc. */
4569 fixP->fx_addnumber = *valP;
4571 switch (fixP->fx_r_type)
4573 case BFD_RELOC_RISCV_HI20:
4574 case BFD_RELOC_RISCV_LO12_I:
4575 case BFD_RELOC_RISCV_LO12_S:
4576 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
4577 | bfd_getl32 (buf), buf);
4578 if (fixP->fx_addsy == NULL)
4579 fixP->fx_done = true;
4580 relaxable = true;
4581 break;
4583 case BFD_RELOC_RISCV_GOT_HI20:
4584 /* R_RISCV_GOT_HI20 and the following R_RISCV_LO12_I are relaxable
4585 only if it is created as a result of la or lga assembler macros. */
4586 if (fixP->tc_fix_data.source_macro == M_LA
4587 || fixP->tc_fix_data.source_macro == M_LGA)
4588 relaxable = true;
4589 break;
4591 case BFD_RELOC_RISCV_ADD8:
4592 case BFD_RELOC_RISCV_ADD16:
4593 case BFD_RELOC_RISCV_ADD32:
4594 case BFD_RELOC_RISCV_ADD64:
4595 case BFD_RELOC_RISCV_SUB6:
4596 case BFD_RELOC_RISCV_SUB8:
4597 case BFD_RELOC_RISCV_SUB16:
4598 case BFD_RELOC_RISCV_SUB32:
4599 case BFD_RELOC_RISCV_SUB64:
4600 case BFD_RELOC_RISCV_RELAX:
4601 /* cvt_frag_to_fill () has called output_leb128 (). */
4602 case BFD_RELOC_RISCV_SET_ULEB128:
4603 case BFD_RELOC_RISCV_SUB_ULEB128:
4604 break;
4606 case BFD_RELOC_RISCV_TPREL_HI20:
4607 case BFD_RELOC_RISCV_TPREL_LO12_I:
4608 case BFD_RELOC_RISCV_TPREL_LO12_S:
4609 case BFD_RELOC_RISCV_TPREL_ADD:
4610 case BFD_RELOC_RISCV_TLSDESC_HI20:
4611 relaxable = true;
4612 /* Fall through. */
4614 case BFD_RELOC_RISCV_TLS_GOT_HI20:
4615 case BFD_RELOC_RISCV_TLS_GD_HI20:
4616 case BFD_RELOC_RISCV_TLS_DTPREL32:
4617 case BFD_RELOC_RISCV_TLS_DTPREL64:
4618 if (fixP->fx_addsy != NULL)
4619 S_SET_THREAD_LOCAL (fixP->fx_addsy);
4620 else
4621 as_bad_where (fixP->fx_file, fixP->fx_line,
4622 _("TLS relocation against a constant"));
4623 break;
4625 case BFD_RELOC_32:
4626 /* Use pc-relative relocation for FDE initial location.
4627 The symbol address in .eh_frame may be adjusted in
4628 _bfd_elf_discard_section_eh_frame, and the content of
4629 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4630 Therefore, we cannot insert a relocation whose addend symbol is
4631 in .eh_frame. Othrewise, the value may be adjusted twice. */
4632 if (fixP->fx_addsy && fixP->fx_subsy
4633 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
4634 && strcmp (sub_segment->name, ".eh_frame") == 0
4635 && S_GET_VALUE (fixP->fx_subsy)
4636 == fixP->fx_frag->fr_address + fixP->fx_where)
4638 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
4639 fixP->fx_subsy = NULL;
4640 break;
4642 /* Fall through. */
4643 case BFD_RELOC_64:
4644 case BFD_RELOC_16:
4645 case BFD_RELOC_8:
4646 case BFD_RELOC_RISCV_CFA:
4647 if (fixP->fx_addsy && fixP->fx_subsy)
4649 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4650 fixP->fx_next->fx_addsy = fixP->fx_subsy;
4651 fixP->fx_next->fx_subsy = NULL;
4652 fixP->fx_next->fx_offset = 0;
4653 fixP->fx_subsy = NULL;
4655 switch (fixP->fx_r_type)
4657 case BFD_RELOC_64:
4658 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
4659 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
4660 break;
4662 case BFD_RELOC_32:
4663 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
4664 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4665 break;
4667 case BFD_RELOC_16:
4668 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
4669 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4670 break;
4672 case BFD_RELOC_8:
4673 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
4674 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4675 break;
4677 case BFD_RELOC_RISCV_CFA:
4678 /* Load the byte to get the subtype. */
4679 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
4680 loc = fixP->fx_frag->fr_fix - (subtype & 7);
4681 switch (subtype)
4683 case DW_CFA_advance_loc1:
4684 fixP->fx_where = loc + 1;
4685 fixP->fx_next->fx_where = loc + 1;
4686 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
4687 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
4688 break;
4690 case DW_CFA_advance_loc2:
4691 fixP->fx_size = 2;
4692 fixP->fx_next->fx_size = 2;
4693 fixP->fx_where = loc + 1;
4694 fixP->fx_next->fx_where = loc + 1;
4695 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
4696 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
4697 break;
4699 case DW_CFA_advance_loc4:
4700 fixP->fx_size = 4;
4701 fixP->fx_next->fx_size = 4;
4702 fixP->fx_where = loc;
4703 fixP->fx_next->fx_where = loc;
4704 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
4705 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
4706 break;
4708 default:
4709 if (subtype < 0x80 && (subtype & 0x40))
4711 /* DW_CFA_advance_loc */
4712 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
4713 fixP->fx_next->fx_frag = fixP->fx_frag;
4714 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
4715 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
4717 else
4718 as_fatal (_("internal: bad CFA value #%d"), subtype);
4719 break;
4721 break;
4723 default:
4724 /* This case is unreachable. */
4725 abort ();
4728 /* Fall through. */
4730 case BFD_RELOC_RVA:
4731 /* If we are deleting this reloc entry, we must fill in the
4732 value now. This can happen if we have a .word which is not
4733 resolved when it appears but is later defined. */
4734 if (fixP->fx_addsy == NULL)
4736 gas_assert (fixP->fx_size <= sizeof (valueT));
4737 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
4738 fixP->fx_done = 1;
4740 break;
4742 case BFD_RELOC_RISCV_JMP:
4743 if (fixP->fx_addsy)
4745 /* Fill in a tentative value to improve objdump readability. */
4746 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4747 bfd_vma delta = target - md_pcrel_from (fixP);
4748 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
4749 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4750 fixP->fx_done = 1;
4752 break;
4754 case BFD_RELOC_12_PCREL:
4755 if (fixP->fx_addsy)
4757 /* Fill in a tentative value to improve objdump readability. */
4758 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4759 bfd_vma delta = target - md_pcrel_from (fixP);
4760 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
4761 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4762 fixP->fx_done = 1;
4764 break;
4766 case BFD_RELOC_RISCV_RVC_BRANCH:
4767 if (fixP->fx_addsy)
4769 /* Fill in a tentative value to improve objdump readability. */
4770 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4771 bfd_vma delta = target - md_pcrel_from (fixP);
4772 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
4773 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4774 fixP->fx_done = 1;
4776 break;
4778 case BFD_RELOC_RISCV_RVC_JUMP:
4779 if (fixP->fx_addsy)
4781 /* Fill in a tentative value to improve objdump readability. */
4782 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4783 bfd_vma delta = target - md_pcrel_from (fixP);
4784 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
4785 if (!riscv_opts.relax && S_IS_LOCAL (fixP->fx_addsy))
4786 fixP->fx_done = 1;
4788 break;
4790 case BFD_RELOC_RISCV_CALL:
4791 case BFD_RELOC_RISCV_CALL_PLT:
4792 case BFD_RELOC_RISCV_TLSDESC_LOAD_LO12:
4793 case BFD_RELOC_RISCV_TLSDESC_ADD_LO12:
4794 case BFD_RELOC_RISCV_TLSDESC_CALL:
4795 relaxable = true;
4796 break;
4798 case BFD_RELOC_RISCV_PCREL_HI20:
4799 /* Record and evaluate the pcrel_hi relocation with local symbol.
4800 Fill in a tentative value to improve objdump readability for -mrelax,
4801 and set fx_done for -mno-relax. */
4802 if (fixP->fx_addsy
4803 && S_IS_LOCAL (fixP->fx_addsy)
4804 && S_GET_SEGMENT (fixP->fx_addsy) == seg)
4806 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4807 bfd_vma value = target - md_pcrel_from (fixP);
4809 /* Record PCREL_HI20. */
4810 if (!riscv_record_pcrel_fixup (riscv_pcrel_hi_fixup_hash,
4811 (const asection *) seg,
4812 md_pcrel_from (fixP),
4813 fixP->fx_addsy,
4814 target))
4815 as_warn (_("too many pcrel_hi"));
4817 bfd_putl32 (bfd_getl32 (buf)
4818 | ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)),
4819 buf);
4820 if (!riscv_opts.relax)
4821 fixP->fx_done = 1;
4823 relaxable = true;
4824 break;
4826 case BFD_RELOC_RISCV_PCREL_LO12_S:
4827 case BFD_RELOC_RISCV_PCREL_LO12_I:
4828 /* Resolve the pcrel_lo relocation with local symbol.
4829 Fill in a tentative value to improve objdump readability for -mrelax,
4830 and set fx_done for -mno-relax. */
4832 bfd_vma location_pcrel_hi = S_GET_VALUE (fixP->fx_addsy) + *valP;
4833 riscv_pcrel_hi_fixup search =
4834 {(const asection *) seg, location_pcrel_hi, 0, 0};
4835 riscv_pcrel_hi_fixup *entry = htab_find (riscv_pcrel_hi_fixup_hash,
4836 &search);
4837 if (entry && entry->symbol
4838 && S_IS_LOCAL (entry->symbol)
4839 && S_GET_SEGMENT (entry->symbol) == seg)
4841 bfd_vma target = entry->target;
4842 bfd_vma value = target - entry->address;
4843 if (fixP->fx_r_type == BFD_RELOC_RISCV_PCREL_LO12_S)
4844 bfd_putl32 (bfd_getl32 (buf) | ENCODE_STYPE_IMM (value), buf);
4845 else
4846 bfd_putl32 (bfd_getl32 (buf) | ENCODE_ITYPE_IMM (value), buf);
4847 /* Relaxations should never be enabled by `.option relax'. */
4848 if (!riscv_opts.relax)
4849 fixP->fx_done = 1;
4852 relaxable = true;
4853 break;
4855 case BFD_RELOC_RISCV_ALIGN:
4856 break;
4858 default:
4859 /* We ignore generic BFD relocations we don't know about. */
4860 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
4861 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
4864 if (fixP->fx_subsy != NULL)
4865 as_bad_subtract (fixP);
4867 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4868 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
4870 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4871 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
4872 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
4873 fixP->fx_next->fx_size = 0;
4877 /* Because the value of .cfi_remember_state may changed after relaxation,
4878 we insert a fix to relocate it again in link-time. */
4880 void
4881 riscv_pre_output_hook (void)
4883 const frchainS *frch;
4884 segT s;
4886 /* Save the current segment info. */
4887 segT seg = now_seg;
4888 subsegT subseg = now_subseg;
4890 for (s = stdoutput->sections; s; s = s->next)
4891 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
4893 fragS *frag;
4895 for (frag = frch->frch_root; frag; frag = frag->fr_next)
4897 if (frag->fr_type == rs_cfa)
4899 expressionS exp;
4900 expressionS *symval;
4902 symval = symbol_get_value_expression (frag->fr_symbol);
4903 exp.X_op = O_subtract;
4904 exp.X_add_symbol = symval->X_add_symbol;
4905 exp.X_add_number = 0;
4906 exp.X_op_symbol = symval->X_op_symbol;
4908 /* We must set the segment before creating a frag after all
4909 frag chains have been chained together. */
4910 subseg_set (s, frch->frch_subseg);
4912 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
4913 BFD_RELOC_RISCV_CFA);
4918 /* Restore the original segment info. */
4919 subseg_set (seg, subseg);
4922 /* Handle the .option pseudo-op. */
4924 static void
4925 s_riscv_option (int x ATTRIBUTE_UNUSED)
4927 char *name = input_line_pointer, ch;
4929 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4930 ++input_line_pointer;
4931 ch = *input_line_pointer;
4932 *input_line_pointer = '\0';
4934 if (strcmp (name, "rvc") == 0)
4936 riscv_update_subset (&riscv_rps_as, "+c");
4937 riscv_set_arch_str (&riscv_rps_as.subset_list->arch_str);
4938 riscv_set_rvc (true);
4940 else if (strcmp (name, "norvc") == 0)
4942 riscv_update_subset (&riscv_rps_as, "-c");
4943 riscv_set_arch_str (&riscv_rps_as.subset_list->arch_str);
4944 riscv_set_rvc (false);
4946 else if (strcmp (name, "pic") == 0)
4947 riscv_opts.pic = true;
4948 else if (strcmp (name, "nopic") == 0)
4949 riscv_opts.pic = false;
4950 else if (strcmp (name, "relax") == 0)
4951 riscv_opts.relax = true;
4952 else if (strcmp (name, "norelax") == 0)
4953 riscv_opts.relax = false;
4954 else if (strcmp (name, "csr-check") == 0)
4955 riscv_opts.csr_check = true;
4956 else if (strcmp (name, "no-csr-check") == 0)
4957 riscv_opts.csr_check = false;
4958 else if (strncmp (name, "arch,", 5) == 0)
4960 name += 5;
4961 if (ISSPACE (*name) && *name != '\0')
4962 name++;
4963 riscv_update_subset (&riscv_rps_as, name);
4964 riscv_set_arch_str (&riscv_rps_as.subset_list->arch_str);
4966 riscv_set_rvc (riscv_subset_supports (&riscv_rps_as, "c")
4967 || riscv_subset_supports (&riscv_rps_as, "zca"));
4969 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
4970 riscv_set_tso ();
4972 else if (strcmp (name, "push") == 0)
4974 struct riscv_option_stack *s;
4976 s = XNEW (struct riscv_option_stack);
4977 s->next = riscv_opts_stack;
4978 s->options = riscv_opts;
4979 s->subset_list = riscv_rps_as.subset_list;
4980 riscv_opts_stack = s;
4981 riscv_rps_as.subset_list = riscv_copy_subset_list (s->subset_list);
4983 else if (strcmp (name, "pop") == 0)
4985 struct riscv_option_stack *s;
4987 s = riscv_opts_stack;
4988 if (s == NULL)
4989 as_bad (_(".option pop with no .option push"));
4990 else
4992 riscv_subset_list_t *release_subsets = riscv_rps_as.subset_list;
4993 riscv_opts_stack = s->next;
4994 riscv_opts = s->options;
4995 riscv_rps_as.subset_list = s->subset_list;
4996 riscv_release_subset_list (release_subsets);
4997 free (s);
5000 else
5002 as_warn (_("unrecognized .option directive: %s"), name);
5004 *input_line_pointer = ch;
5005 demand_empty_rest_of_line ();
5008 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
5009 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
5010 use in DWARF debug information. */
5012 static void
5013 s_dtprel (int bytes)
5015 expressionS ex;
5016 char *p;
5018 expression (&ex);
5020 if (ex.X_op != O_symbol)
5022 as_bad (_("unsupported use of %s"), (bytes == 8
5023 ? ".dtpreldword"
5024 : ".dtprelword"));
5025 ignore_rest_of_line ();
5028 p = frag_more (bytes);
5029 md_number_to_chars (p, 0, bytes);
5030 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
5031 (bytes == 8
5032 ? BFD_RELOC_RISCV_TLS_DTPREL64
5033 : BFD_RELOC_RISCV_TLS_DTPREL32));
5035 demand_empty_rest_of_line ();
5038 static void
5039 riscv_make_nops (char *buf, bfd_vma bytes)
5041 bfd_vma i = 0;
5043 /* RISC-V instructions cannot begin or end on odd addresses, so this case
5044 means we are not within a valid instruction sequence. It is thus safe
5045 to use a zero byte, even though that is not a valid instruction. */
5046 if (bytes % 2 == 1)
5047 buf[i++] = 0;
5049 /* Use at most one 2-byte NOP. */
5050 if ((bytes - i) % 4 == 2)
5052 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
5053 i += 2;
5056 /* Fill the remainder with 4-byte NOPs. */
5057 for ( ; i < bytes; i += 4)
5058 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
5061 /* Called from md_do_align. Used to create an alignment frag in a
5062 code section by emitting a worst-case NOP sequence that the linker
5063 will later relax to the correct number of NOPs. We can't compute
5064 the correct alignment now because of other linker relaxations. */
5066 bool
5067 riscv_frag_align_code (int n)
5069 bfd_vma bytes = (bfd_vma) 1 << n;
5070 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
5071 bfd_vma worst_case_bytes = bytes - insn_alignment;
5072 char *nops;
5073 expressionS ex;
5075 /* If we are moving to alignment no larger than the instruction size, then
5076 no special alignment handling is required. */
5077 if (bytes <= insn_alignment)
5079 if (bytes == insn_alignment)
5080 seg_info (now_seg)->tc_segment_info_data.last_insn16 = false;
5081 return false;
5084 /* When not relaxing, riscv_handle_align handles code alignment. */
5085 if (!riscv_opts.relax)
5086 return false;
5088 /* If the last item emitted was not an ordinary insn, first align back to
5089 insn granularity. Don't do this unconditionally, to avoid altering frags
5090 when that's not actually needed. */
5091 if (seg_info (now_seg)->tc_segment_info_data.map_state != MAP_INSN
5092 || seg_info (now_seg)->tc_segment_info_data.last_insn16)
5093 frag_align_code (riscv_opts.rvc ? 1 : 2, 0);
5094 seg_info (now_seg)->tc_segment_info_data.last_insn16 = false;
5096 /* Maybe we should use frag_var to create a new rs_align_code fragment,
5097 rather than just use frag_more to handle an alignment here? So that we
5098 don't need to call riscv_mapping_state again later, and then only need
5099 to check frag->fr_type to see if it is frag_align_code. */
5100 nops = frag_more (worst_case_bytes);
5102 ex.X_op = O_constant;
5103 ex.X_add_number = worst_case_bytes;
5105 riscv_make_nops (nops, worst_case_bytes);
5107 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
5108 &ex, false, BFD_RELOC_RISCV_ALIGN);
5110 riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
5112 /* We need to start a new frag after the alignment which may be removed by
5113 the linker, to prevent the assembler from computing static offsets.
5114 This is necessary to get correct EH info. */
5115 frag_wane (frag_now);
5116 frag_new (0);
5118 return true;
5121 /* Implement HANDLE_ALIGN. */
5123 void
5124 riscv_handle_align (fragS *fragP)
5126 switch (fragP->fr_type)
5128 case rs_align_code:
5130 bfd_signed_vma bytes = (fragP->fr_next->fr_address
5131 - fragP->fr_address - fragP->fr_fix);
5132 /* We have 4 byte uncompressed nops. */
5133 bfd_signed_vma size = 4;
5134 bfd_signed_vma excess = bytes % size;
5135 char *p = fragP->fr_literal + fragP->fr_fix;
5137 if (bytes <= 0)
5138 break;
5140 /* Insert zeros or compressed nops to get 4 byte alignment. */
5141 if (excess)
5143 if (excess % 2)
5144 riscv_add_odd_padding_symbol (fragP);
5145 riscv_make_nops (p, excess);
5146 fragP->fr_fix += excess;
5147 p += excess;
5150 /* The frag will be changed to `rs_fill` later. The function
5151 `write_contents` will try to fill the remaining spaces
5152 according to the patterns we give. In this case, we give
5153 a 4 byte uncompressed nop as the pattern, and set the size
5154 of the pattern into `fr_var`. The nop will be output to the
5155 file `fr_offset` times. However, `fr_offset` could be zero
5156 if we don't need to pad the boundary finally. */
5157 riscv_make_nops (p, size);
5158 fragP->fr_var = size;
5160 break;
5162 default:
5163 break;
5167 /* This usually called from frag_var. */
5169 void
5170 riscv_init_frag (fragS * fragP, int max_chars)
5172 /* Do not add mapping symbol to debug sections. */
5173 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
5174 return;
5176 switch (fragP->fr_type)
5178 case rs_fill:
5179 case rs_align:
5180 case rs_align_test:
5181 riscv_mapping_state (MAP_DATA, max_chars, false/* fr_align_code */);
5182 break;
5183 case rs_align_code:
5184 riscv_mapping_state (MAP_INSN, max_chars, true/* fr_align_code */);
5185 break;
5186 default:
5187 break;
5192 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
5194 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
5197 /* Translate internal representation of relocation info to BFD target
5198 format. */
5200 arelent *
5201 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
5203 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
5205 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5206 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5207 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5208 reloc->addend = fixp->fx_addnumber;
5210 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5211 if (reloc->howto == NULL)
5213 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
5214 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
5216 /* We don't have R_RISCV_8/16, but for this special case,
5217 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
5218 return reloc;
5221 as_bad_where (fixp->fx_file, fixp->fx_line,
5222 _("cannot represent %s relocation in object file"),
5223 bfd_get_reloc_code_name (fixp->fx_r_type));
5224 return NULL;
5227 return reloc;
5231 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
5233 if (RELAX_BRANCH_P (fragp->fr_subtype))
5235 offsetT old_var = fragp->fr_var;
5236 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
5237 return fragp->fr_var - old_var;
5240 return 0;
5243 /* Expand far branches to multi-instruction sequences. */
5245 static void
5246 md_convert_frag_branch (fragS *fragp)
5248 bfd_byte *buf;
5249 expressionS exp;
5250 fixS *fixp;
5251 insn_t insn;
5252 int rs1, reloc;
5254 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
5256 exp.X_op = O_symbol;
5257 exp.X_add_symbol = fragp->fr_symbol;
5258 exp.X_add_number = fragp->fr_offset;
5260 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
5262 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
5264 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
5266 case 8:
5267 case 4:
5268 /* Expand the RVC branch into a RISC-V one. */
5269 insn = bfd_getl16 (buf);
5270 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
5271 if ((insn & MASK_C_J) == MATCH_C_J)
5272 insn = MATCH_JAL;
5273 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
5274 insn = MATCH_JAL | (X_RA << OP_SH_RD);
5275 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
5276 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
5277 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
5278 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
5279 else
5280 abort ();
5281 bfd_putl32 (insn, buf);
5282 break;
5284 case 6:
5285 /* Invert the branch condition. Branch over the jump. */
5286 insn = bfd_getl16 (buf);
5287 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
5288 insn |= ENCODE_CBTYPE_IMM (6);
5289 bfd_putl16 (insn, buf);
5290 buf += 2;
5291 goto jump;
5293 case 2:
5294 /* Just keep the RVC branch. */
5295 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
5296 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
5297 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5298 2, &exp, false, reloc);
5299 buf += 2;
5300 goto done;
5302 default:
5303 abort ();
5307 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
5309 case 8:
5310 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
5312 /* Invert the branch condition. Branch over the jump. */
5313 insn = bfd_getl32 (buf);
5314 insn ^= MATCH_BEQ ^ MATCH_BNE;
5315 insn |= ENCODE_BTYPE_IMM (8);
5316 bfd_putl32 (insn, buf);
5317 buf += 4;
5319 jump:
5320 /* Jump to the target. */
5321 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5322 4, &exp, false, BFD_RELOC_RISCV_JMP);
5323 bfd_putl32 (MATCH_JAL, buf);
5324 buf += 4;
5325 break;
5327 case 4:
5328 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
5329 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
5330 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
5331 4, &exp, false, reloc);
5332 buf += 4;
5333 break;
5335 default:
5336 abort ();
5339 done:
5340 fixp->fx_file = fragp->fr_file;
5341 fixp->fx_line = fragp->fr_line;
5343 gas_assert (buf == (bfd_byte *)fragp->fr_literal
5344 + fragp->fr_fix + fragp->fr_var);
5346 fragp->fr_fix += fragp->fr_var;
5349 /* Relax a machine dependent frag. This returns the amount by which
5350 the current size of the frag should change. */
5352 void
5353 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
5354 fragS *fragp)
5356 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
5357 md_convert_frag_branch (fragp);
5360 void
5361 md_show_usage (FILE *stream)
5363 fprintf (stream, _("\
5364 RISC-V options:\n\
5365 -fpic or -fPIC generate position-independent code\n\
5366 -fno-pic don't generate position-independent code (default)\n\
5367 -march=ISA set the RISC-V architecture\n\
5368 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
5369 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.10, 1.11, 1.12)\n\
5370 -mabi=ABI set the RISC-V ABI\n\
5371 -mrelax enable relax (default)\n\
5372 -mno-relax disable relax\n\
5373 -march-attr generate RISC-V arch attribute\n\
5374 -mno-arch-attr don't generate RISC-V arch attribute\n\
5375 -mcsr-check enable the csr ISA and privilege spec version checks\n\
5376 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
5377 -mbig-endian assemble for big-endian\n\
5378 -mlittle-endian assemble for little-endian\n\
5379 "));
5382 /* Standard calling conventions leave the CFA at SP on entry. */
5384 void
5385 riscv_cfi_frame_initial_instructions (void)
5387 cfi_add_CFA_def_cfa (X_SP, 0);
5391 tc_riscv_regname_to_dw2regnum (char *regname)
5393 int reg;
5395 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
5396 return reg;
5398 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
5399 return reg + 32;
5401 if ((reg = reg_lookup_internal (regname, RCLASS_VECR)) >= 0)
5402 return reg + 96;
5404 /* CSRs are numbered 4096 -> 8191. */
5405 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
5406 return reg + 4096;
5408 as_bad (_("unknown register `%s'"), regname);
5409 return -1;
5412 void
5413 riscv_elf_section_change_hook (void)
5415 struct riscv_segment_info_type *info
5416 = &seg_info(now_seg)->tc_segment_info_data;
5418 if (info->rvc && !riscv_opts.rvc)
5419 info->last_insn16 = true;
5421 info->rvc = riscv_opts.rvc;
5424 void
5425 riscv_elf_final_processing (void)
5427 riscv_set_abi_by_arch ();
5428 riscv_release_subset_list (riscv_rps_as.subset_list);
5429 elf_elfheader (stdoutput)->e_flags |= elf_flags;
5432 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
5433 since these directives break relaxation when used with symbol deltas. */
5435 static void
5436 s_riscv_leb128 (int sign)
5438 expressionS exp;
5439 char *save_in = input_line_pointer;
5441 expression (&exp);
5442 if (sign && exp.X_op != O_constant)
5443 as_bad (_("non-constant .sleb128 is not supported"));
5444 else if (!sign && exp.X_op != O_constant && exp.X_op != O_subtract)
5445 as_bad (_(".uleb128 only supports constant or subtract expressions"));
5447 demand_empty_rest_of_line ();
5449 input_line_pointer = save_in;
5450 return s_leb128 (sign);
5453 /* Parse the .insn directive. There are three formats,
5454 Format 1: .insn <type> <operand1>, <operand2>, ...
5455 Format 2: .insn <length>, <value>
5456 Format 3: .insn <value>. */
5458 static void
5459 s_riscv_insn (int x ATTRIBUTE_UNUSED)
5461 char *str = input_line_pointer;
5462 struct riscv_cl_insn insn;
5463 expressionS imm_expr;
5464 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
5465 char save_c;
5467 while (!is_end_of_line[(unsigned char) *input_line_pointer])
5468 ++input_line_pointer;
5470 save_c = *input_line_pointer;
5471 *input_line_pointer = '\0';
5473 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
5475 struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
5476 &imm_reloc, insn_type_hash);
5477 if (error.msg)
5479 char *save_in = input_line_pointer;
5480 error.msg = riscv_ip_hardcode (str, &insn, &imm_expr, error.msg);
5481 input_line_pointer = save_in;
5484 if (error.msg)
5486 if (error.missing_ext)
5487 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
5488 error.missing_ext);
5489 else
5490 as_bad ("%s `%s'", error.msg, error.statement);
5492 else
5494 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
5495 append_insn (&insn, &imm_expr, imm_reloc);
5498 *input_line_pointer = save_c;
5499 demand_empty_rest_of_line ();
5502 /* Update architecture and privileged elf attributes. If we don't set
5503 them, then try to output the default ones. */
5505 static void
5506 riscv_write_out_attrs (void)
5508 const char *priv_str, *p;
5509 /* versions[0]: major version.
5510 versions[1]: minor version.
5511 versions[2]: revision version. */
5512 unsigned versions[3] = {0}, number = 0;
5513 unsigned int i;
5515 /* Re-write architecture elf attribute. */
5516 if (!bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, file_arch_str))
5517 as_fatal (_("error adding attribute: %s"),
5518 bfd_errmsg (bfd_get_error ()));
5519 free ((void *) file_arch_str);
5521 /* For the file without any instruction, we don't set the default_priv_spec
5522 according to the privileged elf attributes since the md_assemble isn't
5523 called. */
5524 if (!start_assemble
5525 && !riscv_set_default_priv_spec (NULL))
5526 return;
5528 /* If we already have set privileged elf attributes, then no need to do
5529 anything. Otherwise, don't generate or update them when no CSR and
5530 privileged instructions are used. */
5531 if (!explicit_priv_attr)
5532 return;
5534 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
5535 p = priv_str;
5536 for (i = 0; *p; ++p)
5538 if (*p == '.' && i < 3)
5540 versions[i++] = number;
5541 number = 0;
5543 else if (ISDIGIT (*p))
5544 number = (number * 10) + (*p - '0');
5545 else
5547 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
5548 return;
5551 versions[i] = number;
5553 /* Re-write privileged elf attributes. */
5554 if (!bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec,
5555 versions[0])
5556 || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor,
5557 versions[1])
5558 || !bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision,
5559 versions[2]))
5560 as_fatal (_("error adding attribute: %s"),
5561 bfd_errmsg (bfd_get_error ()));
5564 /* Add the default contents for the .riscv.attributes section. */
5566 static void
5567 riscv_set_public_attributes (void)
5569 if (riscv_opts.arch_attr || explicit_attr)
5570 riscv_write_out_attrs ();
5573 /* Scan uleb128 subtraction expressions and insert fixups for them.
5574 e.g., .uleb128 .L1 - .L0
5575 Because relaxation may change the value of the subtraction, we
5576 must resolve them at link-time. */
5578 static void
5579 riscv_insert_uleb128_fixes (bfd *abfd ATTRIBUTE_UNUSED,
5580 asection *sec, void *xxx ATTRIBUTE_UNUSED)
5582 segment_info_type *seginfo = seg_info (sec);
5583 struct frag *fragP;
5585 subseg_set (sec, 0);
5587 for (fragP = seginfo->frchainP->frch_root;
5588 fragP; fragP = fragP->fr_next)
5590 expressionS *exp, *exp_dup;
5592 if (fragP->fr_type != rs_leb128 || fragP->fr_symbol == NULL)
5593 continue;
5595 exp = symbol_get_value_expression (fragP->fr_symbol);
5597 if (exp->X_op != O_subtract)
5598 continue;
5600 /* Only unsigned leb128 can be handled. */
5601 gas_assert (fragP->fr_subtype == 0);
5602 exp_dup = xmemdup (exp, sizeof (*exp), sizeof (*exp));
5603 exp_dup->X_op = O_symbol;
5604 exp_dup->X_op_symbol = NULL;
5606 /* Insert relocations to resolve the subtraction at link-time.
5607 Emit the SET relocation first in riscv. */
5608 exp_dup->X_add_symbol = exp->X_add_symbol;
5609 fix_new_exp (fragP, fragP->fr_fix, 0,
5610 exp_dup, 0, BFD_RELOC_RISCV_SET_ULEB128);
5611 exp_dup->X_add_symbol = exp->X_op_symbol;
5612 exp_dup->X_add_number = 0; /* Set addend of SUB_ULEB128 to zero. */
5613 fix_new_exp (fragP, fragP->fr_fix, 0,
5614 exp_dup, 0, BFD_RELOC_RISCV_SUB_ULEB128);
5615 free ((void *) exp_dup);
5619 /* Called after all assembly has been done. */
5621 void
5622 riscv_md_finish (void)
5624 riscv_set_public_attributes ();
5625 if (riscv_opts.relax)
5626 bfd_map_over_sections (stdoutput, riscv_insert_uleb128_fixes, NULL);
5629 /* Called just before the assembler exits. */
5631 void
5632 riscv_md_end (void)
5634 htab_delete (riscv_pcrel_hi_fixup_hash);
5637 /* Adjust the symbol table. */
5639 void
5640 riscv_adjust_symtab (void)
5642 bfd_map_over_sections (stdoutput, riscv_check_mapping_symbols, (char *) 0);
5643 elf_adjust_symtab ();
5646 /* Given a symbolic attribute NAME, return the proper integer value.
5647 Returns -1 if the attribute is not known. */
5650 riscv_convert_symbolic_attribute (const char *name)
5652 static const struct
5654 const char *name;
5655 const int tag;
5657 attribute_table[] =
5659 /* When you modify this table you should
5660 also modify the list in doc/c-riscv.texi. */
5661 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
5662 T(arch),
5663 T(priv_spec),
5664 T(priv_spec_minor),
5665 T(priv_spec_revision),
5666 T(unaligned_access),
5667 T(stack_align),
5668 #undef T
5671 if (name == NULL)
5672 return -1;
5674 unsigned int i;
5675 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
5676 if (strcmp (name, attribute_table[i].name) == 0)
5677 return attribute_table[i].tag;
5679 return -1;
5682 /* Parse a .attribute directive. */
5684 static void
5685 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
5687 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
5688 unsigned old_xlen;
5689 obj_attribute *attr;
5691 explicit_attr = true;
5692 switch (tag)
5694 case Tag_RISCV_arch:
5695 old_xlen = xlen;
5696 attr = elf_known_obj_attributes_proc (stdoutput);
5697 if (!start_assemble)
5698 riscv_set_arch (attr[Tag_RISCV_arch].s);
5699 else
5700 as_fatal (_("architecture elf attributes must set before "
5701 "any instructions"));
5703 if (old_xlen != xlen)
5705 /* We must re-init bfd again if xlen is changed. */
5706 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
5707 bfd_find_target (riscv_target_format (), stdoutput);
5709 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
5710 as_warn (_("could not set architecture and machine"));
5712 break;
5714 case Tag_RISCV_priv_spec:
5715 case Tag_RISCV_priv_spec_minor:
5716 case Tag_RISCV_priv_spec_revision:
5717 if (start_assemble)
5718 as_fatal (_("privileged elf attributes must set before "
5719 "any instructions"));
5720 break;
5722 default:
5723 break;
5727 /* Mark symbol that it follows a variant CC convention. */
5729 static void
5730 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
5732 char *name;
5733 char c;
5734 symbolS *sym;
5735 asymbol *bfdsym;
5736 elf_symbol_type *elfsym;
5738 c = get_symbol_name (&name);
5739 if (!*name)
5740 as_bad (_("missing symbol name for .variant_cc directive"));
5741 sym = symbol_find_or_make (name);
5742 restore_line_pointer (c);
5743 demand_empty_rest_of_line ();
5745 bfdsym = symbol_get_bfdsym (sym);
5746 elfsym = elf_symbol_from (bfdsym);
5747 gas_assert (elfsym);
5748 elfsym->internal_elf_sym.st_other |= STO_RISCV_VARIANT_CC;
5751 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5752 This is needed so RISC-V specific st_other values can be independently
5753 specified for an IFUNC resolver (that is called by the dynamic linker)
5754 and the symbol it resolves (aliased to the resolver). In particular,
5755 if a function symbol has special st_other value set via directives,
5756 then attaching an IFUNC resolver to that symbol should not override
5757 the st_other setting. Requiring the directive on the IFUNC resolver
5758 symbol would be unexpected and problematic in C code, where the two
5759 symbols appear as two independent function declarations. */
5761 void
5762 riscv_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
5764 struct elf_obj_sy *srcelf = symbol_get_obj (src);
5765 struct elf_obj_sy *destelf = symbol_get_obj (dest);
5766 /* If size is unset, copy size from src. Because we don't track whether
5767 .size has been used, we can't differentiate .size dest, 0 from the case
5768 where dest's size is unset. */
5769 if (!destelf->size && S_GET_SIZE (dest) == 0)
5771 if (srcelf->size)
5773 destelf->size = XNEW (expressionS);
5774 *destelf->size = *srcelf->size;
5776 S_SET_SIZE (dest, S_GET_SIZE (src));
5780 /* RISC-V pseudo-ops table. */
5781 static const pseudo_typeS riscv_pseudo_table[] =
5783 {"option", s_riscv_option, 0},
5784 {"half", cons, 2},
5785 {"word", cons, 4},
5786 {"dword", cons, 8},
5787 {"dtprelword", s_dtprel, 4},
5788 {"dtpreldword", s_dtprel, 8},
5789 {"uleb128", s_riscv_leb128, 0},
5790 {"sleb128", s_riscv_leb128, 1},
5791 {"insn", s_riscv_insn, 0},
5792 {"attribute", s_riscv_attribute, 0},
5793 {"variant_cc", s_variant_cc, 0},
5794 {"float16", float_cons, 'h'},
5796 { NULL, NULL, 0 },
5799 void
5800 riscv_pop_insert (void)
5802 extern void pop_insert (const pseudo_typeS *);
5804 pop_insert (riscv_pseudo_table);