1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
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)
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/>. */
26 #include "safe-ctype.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"
38 /* Information about an instruction, including its format, operands
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). */
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. */
55 /* The offset into FRAG of the first instruction byte. */
58 /* The relocs associated with the instruction, if any. */
62 /* All RISC-V CSR belong to one of these classes. */
68 CSR_CLASS_I_32
, /* rv32 only */
69 CSR_CLASS_F
, /* f-ext only */
70 CSR_CLASS_ZKR
, /* zkr only */
71 CSR_CLASS_V
, /* rvv only */
72 CSR_CLASS_DEBUG
, /* debug CSR */
73 CSR_CLASS_H
, /* hypervisor */
74 CSR_CLASS_H_32
, /* hypervisor, rv32 only */
75 CSR_CLASS_SMAIA
, /* Smaia */
76 CSR_CLASS_SMAIA_32
, /* Smaia, rv32 only */
77 CSR_CLASS_SMCNTRPMF
, /* Smcntrpmf */
78 CSR_CLASS_SMCNTRPMF_32
, /* Smcntrpmf, rv32 only */
79 CSR_CLASS_SMSTATEEN
, /* Smstateen only */
80 CSR_CLASS_SMSTATEEN_32
, /* Smstateen RV32 only */
81 CSR_CLASS_SSAIA
, /* Ssaia */
82 CSR_CLASS_SSAIA_AND_H
, /* Ssaia with H */
83 CSR_CLASS_SSAIA_32
, /* Ssaia, rv32 only */
84 CSR_CLASS_SSAIA_AND_H_32
, /* Ssaia with H, rv32 only */
85 CSR_CLASS_SSSTATEEN
, /* S[ms]stateen only */
86 CSR_CLASS_SSSTATEEN_AND_H
, /* S[ms]stateen only (with H) */
87 CSR_CLASS_SSSTATEEN_AND_H_32
, /* S[ms]stateen RV32 only (with H) */
88 CSR_CLASS_SSCOFPMF
, /* Sscofpmf only */
89 CSR_CLASS_SSCOFPMF_32
, /* Sscofpmf RV32 only */
90 CSR_CLASS_SSTC
, /* Sstc only */
91 CSR_CLASS_SSTC_AND_H
, /* Sstc only (with H) */
92 CSR_CLASS_SSTC_32
, /* Sstc RV32 only */
93 CSR_CLASS_SSTC_AND_H_32
, /* Sstc RV32 only (with H) */
96 /* This structure holds all restricted conditions for a CSR. */
97 struct riscv_csr_extra
99 /* Class to which this CSR belongs. Used to decide whether or
100 not this CSR is legal in the current -march context. */
101 enum riscv_csr_class csr_class
;
103 /* CSR may have differnet numbers in the previous priv spec. */
106 /* Record the CSR is defined/valid in which versions. */
107 enum riscv_spec_class define_version
;
109 /* Record the CSR is aborted/invalid from which versions. If it isn't
110 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
111 enum riscv_spec_class abort_version
;
113 /* The CSR may have more than one setting. */
114 struct riscv_csr_extra
*next
;
117 /* This structure contains information about errors that occur within the
119 struct riscv_ip_error
121 /* General error message */
124 /* Statement that caused the error */
127 /* Missing extension that needs to be enabled */
128 const char* missing_ext
;
132 #define DEFAULT_ARCH "riscv64"
135 #ifndef DEFAULT_RISCV_ATTR
136 #define DEFAULT_RISCV_ATTR 0
139 /* Let riscv_after_parse_args set the default value according to xlen. */
140 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
141 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
144 /* Need to sync the version with RISC-V compiler. */
145 #ifndef DEFAULT_RISCV_ISA_SPEC
146 #define DEFAULT_RISCV_ISA_SPEC "20191213"
149 #ifndef DEFAULT_RISCV_PRIV_SPEC
150 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
153 static const char default_arch
[] = DEFAULT_ARCH
;
154 static const char *default_arch_with_ext
= DEFAULT_RISCV_ARCH_WITH_EXT
;
155 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_NONE
;
156 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
158 static unsigned xlen
= 0; /* The width of an x-register. */
159 static unsigned abi_xlen
= 0; /* The width of a pointer in the ABI. */
160 static bool rve_abi
= false;
163 FLOAT_ABI_DEFAULT
= -1,
169 static enum float_abi float_abi
= FLOAT_ABI_DEFAULT
;
171 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
172 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
174 static unsigned elf_flags
= 0;
176 static bool probing_insn_operands
;
178 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
179 Otherwise, return 1. */
182 riscv_set_default_isa_spec (const char *s
)
184 enum riscv_spec_class
class = ISA_SPEC_CLASS_NONE
;
185 RISCV_GET_ISA_SPEC_CLASS (s
, class);
186 if (class == ISA_SPEC_CLASS_NONE
)
188 as_bad ("unknown default ISA spec `%s' set by "
189 "-misa-spec or --with-isa-spec", s
);
193 default_isa_spec
= class;
197 /* Set the default_priv_spec. Find the privileged elf attributes when
198 the input string is NULL. Return 0 if the spec isn't supported.
199 Otherwise, return 1. */
202 riscv_set_default_priv_spec (const char *s
)
204 enum riscv_spec_class
class = PRIV_SPEC_CLASS_NONE
;
205 unsigned major
, minor
, revision
;
208 RISCV_GET_PRIV_SPEC_CLASS (s
, class);
209 if (class != PRIV_SPEC_CLASS_NONE
)
211 default_priv_spec
= class;
217 as_bad (_("unknown default privileged spec `%s' set by "
218 "-mpriv-spec or --with-priv-spec"), s
);
222 /* Set the default_priv_spec by the privileged elf attributes. */
223 attr
= elf_known_obj_attributes_proc (stdoutput
);
224 major
= (unsigned) attr
[Tag_RISCV_priv_spec
].i
;
225 minor
= (unsigned) attr
[Tag_RISCV_priv_spec_minor
].i
;
226 revision
= (unsigned) attr
[Tag_RISCV_priv_spec_revision
].i
;
227 /* Version 0.0.0 is the default value and meningless. */
228 if (major
== 0 && minor
== 0 && revision
== 0)
231 riscv_get_priv_spec_class_from_numbers (major
, minor
, revision
, &class);
232 if (class != PRIV_SPEC_CLASS_NONE
)
234 default_priv_spec
= class;
238 /* Still can not find the privileged spec class. */
239 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
240 "privileged elf attributes"), major
, minor
, revision
);
244 /* This is the set of options which the .option pseudo-op may modify. */
245 struct riscv_set_options
247 int pic
; /* Generate position-independent code. */
248 int rvc
; /* Generate RVC code. */
249 int relax
; /* Emit relocs the linker is allowed to relax. */
250 int arch_attr
; /* Emit architecture and privileged elf attributes. */
251 int csr_check
; /* Enable the CSR checking. */
254 static struct riscv_set_options riscv_opts
=
259 DEFAULT_RISCV_ATTR
, /* arch_attr */
263 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
264 for elf_flags once we have enabled c extension. */
267 riscv_set_rvc (bool rvc_value
)
270 elf_flags
|= EF_RISCV_RVC
;
272 riscv_opts
.rvc
= rvc_value
;
275 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
280 elf_flags
|= EF_RISCV_TSO
;
283 /* The linked list hanging off of .subsets_list records all enabled extensions,
284 which are parsed from the architecture string. The architecture string can
285 be set by the -march option, the elf architecture attributes, and the
286 --with-arch configure option. */
287 static riscv_parse_subset_t riscv_rps_as
=
289 NULL
, /* subset_list, we will set it later once
290 riscv_opts_stack is created or updated. */
291 as_bad
, /* error_handler. */
293 &default_isa_spec
, /* isa_spec. */
294 true, /* check_unknown_prefixed_ext. */
297 /* Update the architecture string in the subset_list. */
300 riscv_reset_subsets_list_arch_str (void)
302 riscv_subset_list_t
*subsets
= riscv_rps_as
.subset_list
;
303 if (subsets
->arch_str
!= NULL
)
304 free ((void *) subsets
->arch_str
);
305 subsets
->arch_str
= riscv_arch_str (xlen
, subsets
);
308 /* This structure is used to hold a stack of .option values. */
309 struct riscv_option_stack
311 struct riscv_option_stack
*next
;
312 struct riscv_set_options options
;
313 riscv_subset_list_t
*subset_list
;
316 static struct riscv_option_stack
*riscv_opts_stack
= NULL
;
318 /* Set which ISA and extensions are available. */
321 riscv_set_arch (const char *s
)
323 if (s
!= NULL
&& strcmp (s
, "") == 0)
325 as_bad (_("the architecture string of -march and elf architecture "
326 "attributes cannot be empty"));
330 if (riscv_rps_as
.subset_list
== NULL
)
332 riscv_rps_as
.subset_list
= XNEW (riscv_subset_list_t
);
333 riscv_rps_as
.subset_list
->head
= NULL
;
334 riscv_rps_as
.subset_list
->tail
= NULL
;
335 riscv_rps_as
.subset_list
->arch_str
= NULL
;
337 riscv_release_subset_list (riscv_rps_as
.subset_list
);
338 riscv_parse_subset (&riscv_rps_as
, s
);
339 riscv_reset_subsets_list_arch_str ();
341 riscv_set_rvc (false);
342 if (riscv_subset_supports (&riscv_rps_as
, "c")
343 || riscv_subset_supports (&riscv_rps_as
, "zca"))
344 riscv_set_rvc (true);
346 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
350 /* Indicate -mabi option is explictly set. */
351 static bool explicit_mabi
= false;
353 /* Set the abi information. */
356 riscv_set_abi (unsigned new_xlen
, enum float_abi new_float_abi
, bool rve
)
359 float_abi
= new_float_abi
;
363 /* If the -mabi option isn't set, then set the abi according to the
364 ISA string. Otherwise, check if there is any conflict. */
367 riscv_set_abi_by_arch (void)
371 if (riscv_subset_supports (&riscv_rps_as
, "q"))
372 riscv_set_abi (xlen
, FLOAT_ABI_QUAD
, false);
373 else if (riscv_subset_supports (&riscv_rps_as
, "d"))
374 riscv_set_abi (xlen
, FLOAT_ABI_DOUBLE
, false);
375 else if (riscv_subset_supports (&riscv_rps_as
, "e"))
376 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, true);
378 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, false);
382 gas_assert (abi_xlen
!= 0 && xlen
!= 0 && float_abi
!= FLOAT_ABI_DEFAULT
);
384 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen
, xlen
);
385 else if (abi_xlen
< xlen
)
386 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen
, xlen
);
388 if (riscv_subset_supports (&riscv_rps_as
, "e") && !rve_abi
)
389 as_bad ("only the ilp32e ABI is supported for e extension");
391 if (float_abi
== FLOAT_ABI_SINGLE
392 && !riscv_subset_supports (&riscv_rps_as
, "f"))
393 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
395 else if (float_abi
== FLOAT_ABI_DOUBLE
396 && !riscv_subset_supports (&riscv_rps_as
, "d"))
397 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
399 else if (float_abi
== FLOAT_ABI_QUAD
400 && !riscv_subset_supports (&riscv_rps_as
, "q"))
401 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
405 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
406 elf_flags
&= ~EF_RISCV_FLOAT_ABI
;
407 elf_flags
|= float_abi
<< 1;
410 elf_flags
|= EF_RISCV_RVE
;
413 /* Handle of the OPCODE hash table. */
414 static htab_t op_hash
= NULL
;
416 /* Handle of the type of .insn hash table. */
417 static htab_t insn_type_hash
= NULL
;
419 /* This array holds the chars that always start a comment. If the
420 pre-processor is disabled, these aren't very useful. */
421 const char comment_chars
[] = "#";
423 /* This array holds the chars that only start a comment at the beginning of
424 a line. If the line seems to have the form '# 123 filename'
425 .line and .file directives will appear in the pre-processed output
427 Note that input_file.c hand checks for '#' at the beginning of the
428 first line of the input file. This is because the compiler outputs
429 #NO_APP at the beginning of its output.
431 Also note that C style comments are always supported. */
432 const char line_comment_chars
[] = "#";
434 /* This array holds machine specific line separator characters. */
435 const char line_separator_chars
[] = ";";
437 /* Chars that can be used to separate mant from exp in floating point nums. */
438 const char EXP_CHARS
[] = "eE";
440 /* Chars that mean this number is a floating point constant.
441 As in 0f12.456 or 0d1.2345e12. */
442 const char FLT_CHARS
[] = "rRsSfFdDxXpPhH";
444 /* Indicate we are already assemble any instructions or not. */
445 static bool start_assemble
= false;
447 /* Indicate ELF attributes are explicitly set. */
448 static bool explicit_attr
= false;
450 /* Indicate CSR or priv instructions are explicitly used. */
451 static bool explicit_priv_attr
= false;
453 static char *expr_parse_end
;
455 /* Macros for encoding relaxation state for RVC branches and far jumps. */
456 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
459 | ((uncond) ? 1 : 0) \
462 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
463 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
464 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
465 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
467 /* Is the given value a sign-extended 32-bit value? */
468 #define IS_SEXT_32BIT_NUM(x) \
469 (((x) &~ (offsetT) 0x7fffffff) == 0 \
470 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
472 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
473 #define IS_ZEXT_32BIT_NUM(x) \
474 (((x) &~ (offsetT) 0xffffffff) == 0 \
475 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
477 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
478 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
479 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
480 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
482 #define INSERT_IMM(n, s, INSN, VALUE) \
483 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
485 /* Determine if an instruction matches an opcode. */
486 #define OPCODE_MATCHES(OPCODE, OP) \
487 (((OPCODE) & MASK_##OP) == MATCH_##OP)
489 /* Create a new mapping symbol for the transition to STATE. */
492 make_mapping_symbol (enum riscv_seg_mstate state
,
495 const char *arch_str
,
496 bool odd_data_padding
)
506 if (arch_str
!= NULL
)
508 size_t size
= strlen (arch_str
) + 3; /* "$x" + '\0' */
509 buff
= xmalloc (size
);
510 snprintf (buff
, size
, "$x%s", arch_str
);
520 symbolS
*symbol
= symbol_new (name
, now_seg
, frag
, value
);
521 symbol_get_bfdsym (symbol
)->flags
|= (BSF_NO_FLAGS
| BSF_LOCAL
);
522 if (arch_str
!= NULL
)
524 /* Store current $x+arch into tc_segment_info. */
525 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
= symbol
;
526 xfree ((void *) buff
);
529 /* If .fill or other data filling directive generates zero sized data,
530 then mapping symbol for the following code will have the same value.
532 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
533 and .text.zero.fill.last. */
534 symbolS
*first
= frag
->tc_frag_data
.first_map_symbol
;
535 symbolS
*last
= frag
->tc_frag_data
.last_map_symbol
;
536 symbolS
*removed
= NULL
;
541 know (S_GET_VALUE (first
) == S_GET_VALUE (symbol
)
543 /* Remove the old one. */
546 frag
->tc_frag_data
.first_map_symbol
= symbol
;
548 else if (last
!= NULL
)
550 /* The mapping symbols should be added in offset order. */
551 know (S_GET_VALUE (last
) <= S_GET_VALUE (symbol
));
552 /* Remove the old one. */
553 if (S_GET_VALUE (last
) == S_GET_VALUE (symbol
))
556 frag
->tc_frag_data
.last_map_symbol
= symbol
;
561 if (odd_data_padding
)
563 /* If the removed mapping symbol is $x+arch, then add it back to
565 const char *str
= strncmp (S_GET_NAME (removed
), "$xrv", 4) == 0
566 ? S_GET_NAME (removed
) + 2 : NULL
;
567 make_mapping_symbol (MAP_INSN
, frag
->fr_fix
+ 1, frag
, str
,
568 false/* odd_data_padding */);
570 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
573 /* Set the mapping state for frag_now. */
576 riscv_mapping_state (enum riscv_seg_mstate to_state
,
580 enum riscv_seg_mstate from_state
=
581 seg_info (now_seg
)->tc_segment_info_data
.map_state
;
582 bool reset_seg_arch_str
= false;
584 if (!SEG_NORMAL (now_seg
)
585 /* For now we only add the mapping symbols to text sections.
586 Therefore, the dis-assembler only show the actual contents
587 distribution for text. Other sections will be shown as
588 data without the details. */
589 || !subseg_text_p (now_seg
))
592 /* The mapping symbol should be emitted if not in the right
594 symbolS
*seg_arch_symbol
=
595 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
;
596 if (to_state
== MAP_INSN
&& seg_arch_symbol
== 0)
598 /* Always add $x+arch at the first instruction of section. */
599 reset_seg_arch_str
= true;
601 else if (seg_arch_symbol
!= 0
602 && to_state
== MAP_INSN
604 && strcmp (riscv_rps_as
.subset_list
->arch_str
,
605 S_GET_NAME (seg_arch_symbol
) + 2) != 0)
607 reset_seg_arch_str
= true;
609 else if (from_state
== to_state
)
612 valueT value
= (valueT
) (frag_now_fix () - max_chars
);
613 seg_info (now_seg
)->tc_segment_info_data
.map_state
= to_state
;
614 const char *arch_str
= reset_seg_arch_str
615 ? riscv_rps_as
.subset_list
->arch_str
: NULL
;
616 make_mapping_symbol (to_state
, value
, frag_now
, arch_str
,
617 false/* odd_data_padding */);
620 /* Add the odd bytes of paddings for riscv_handle_align. */
623 riscv_add_odd_padding_symbol (fragS
*frag
)
625 /* If there was already a mapping symbol, it should be
626 removed in the make_mapping_symbol.
628 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
629 make_mapping_symbol (MAP_DATA
, frag
->fr_fix
, frag
,
630 NULL
/* arch_str */, true/* odd_data_padding */);
633 /* Remove any excess mapping symbols generated for alignment frags in
634 SEC. We may have created a mapping symbol before a zero byte
635 alignment; remove it if there's a mapping symbol after the
639 riscv_check_mapping_symbols (bfd
*abfd ATTRIBUTE_UNUSED
,
641 void *dummy ATTRIBUTE_UNUSED
)
643 segment_info_type
*seginfo
= seg_info (sec
);
646 if (seginfo
== NULL
|| seginfo
->frchainP
== NULL
)
649 for (fragp
= seginfo
->frchainP
->frch_root
;
651 fragp
= fragp
->fr_next
)
653 symbolS
*last
= fragp
->tc_frag_data
.last_map_symbol
;
654 fragS
*next
= fragp
->fr_next
;
656 if (last
== NULL
|| next
== NULL
)
659 /* Check the last mapping symbol if it is at the boundary of
661 if (S_GET_VALUE (last
) < next
->fr_address
)
663 know (S_GET_VALUE (last
) == next
->fr_address
);
667 symbolS
*next_first
= next
->tc_frag_data
.first_map_symbol
;
668 if (next_first
!= NULL
)
670 /* The last mapping symbol overlaps with another one
671 which at the start of the next frag.
673 Please see the gas/testsuite/gas/riscv/mapping.s:
674 .text.zero.fill.align.A and .text.zero.fill.align.B. */
675 know (S_GET_VALUE (last
) == S_GET_VALUE (next_first
));
676 symbolS
*removed
= last
;
677 if (strncmp (S_GET_NAME (last
), "$xrv", 4) == 0
678 && strcmp (S_GET_NAME (next_first
), "$x") == 0)
679 removed
= next_first
;
680 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
684 if (next
->fr_next
== NULL
)
686 /* The last mapping symbol is at the end of the section.
688 Please see the gas/testsuite/gas/riscv/mapping.s:
689 .text.last.section. */
690 know (next
->fr_fix
== 0 && next
->fr_var
== 0);
691 symbol_remove (last
, &symbol_rootP
, &symbol_lastP
);
695 /* Since we may have empty frags without any mapping symbols,
696 keep looking until the non-empty frag. */
697 if (next
->fr_address
!= next
->fr_next
->fr_address
)
700 next
= next
->fr_next
;
702 while (next
!= NULL
);
706 /* The default target format to use. */
709 riscv_target_format (void)
711 if (target_big_endian
)
712 return xlen
== 64 ? "elf64-bigriscv" : "elf32-bigriscv";
714 return xlen
== 64 ? "elf64-littleriscv" : "elf32-littleriscv";
717 /* Return the length of instruction INSN. */
719 static inline unsigned int
720 insn_length (const struct riscv_cl_insn
*insn
)
722 return riscv_insn_length (insn
->insn_opcode
);
725 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
728 create_insn (struct riscv_cl_insn
*insn
, const struct riscv_opcode
*mo
)
731 insn
->insn_opcode
= mo
->match
;
732 insn
->insn_long_opcode
[0] = 0;
738 /* Install INSN at the location specified by its "frag" and "where" fields. */
741 install_insn (const struct riscv_cl_insn
*insn
)
743 char *f
= insn
->frag
->fr_literal
+ insn
->where
;
744 if (insn
->insn_long_opcode
[0] != 0)
745 memcpy (f
, insn
->insn_long_opcode
, insn_length (insn
));
747 number_to_chars_littleendian (f
, insn
->insn_opcode
, insn_length (insn
));
750 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
751 and install the opcode in the new location. */
754 move_insn (struct riscv_cl_insn
*insn
, fragS
*frag
, long where
)
758 if (insn
->fixp
!= NULL
)
760 insn
->fixp
->fx_frag
= frag
;
761 insn
->fixp
->fx_where
= where
;
766 /* Add INSN to the end of the output. */
769 add_fixed_insn (struct riscv_cl_insn
*insn
)
771 char *f
= frag_more (insn_length (insn
));
772 move_insn (insn
, frag_now
, f
- frag_now
->fr_literal
);
776 add_relaxed_insn (struct riscv_cl_insn
*insn
, int max_chars
, int var
,
777 relax_substateT subtype
, symbolS
*symbol
, offsetT offset
)
779 frag_grow (max_chars
);
780 move_insn (insn
, frag_now
, frag_more (0) - frag_now
->fr_literal
);
781 frag_var (rs_machine_dependent
, max_chars
, var
,
782 subtype
, symbol
, offset
, NULL
);
785 /* Compute the length of a branch sequence, and adjust the stored length
786 accordingly. If FRAGP is NULL, the worst-case length is returned. */
789 relaxed_branch_length (fragS
*fragp
, asection
*sec
, int update
)
791 int jump
, rvc
, length
= 8;
796 jump
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
);
797 rvc
= RELAX_BRANCH_RVC (fragp
->fr_subtype
);
798 length
= RELAX_BRANCH_LENGTH (fragp
->fr_subtype
);
800 /* Assume jumps are in range; the linker will catch any that aren't. */
801 length
= jump
? 4 : 8;
803 if (fragp
->fr_symbol
!= NULL
804 && S_IS_DEFINED (fragp
->fr_symbol
)
805 && !S_IS_WEAK (fragp
->fr_symbol
)
806 && sec
== S_GET_SEGMENT (fragp
->fr_symbol
))
808 offsetT val
= S_GET_VALUE (fragp
->fr_symbol
) + fragp
->fr_offset
;
809 bfd_vma rvc_range
= jump
? RVC_JUMP_REACH
: RVC_BRANCH_REACH
;
810 val
-= fragp
->fr_address
+ fragp
->fr_fix
;
812 if (rvc
&& (bfd_vma
)(val
+ rvc_range
/2) < rvc_range
)
814 else if ((bfd_vma
)(val
+ RISCV_BRANCH_REACH
/2) < RISCV_BRANCH_REACH
)
816 else if (!jump
&& rvc
)
821 fragp
->fr_subtype
= RELAX_BRANCH_ENCODE (jump
, rvc
, length
);
826 /* Information about an opcode name, mnemonics and its value. */
833 /* List for all supported opcode name. */
834 static const struct opcode_name_t opcode_name_list
[] =
879 /* Hash table for lookup opcode name. */
880 static htab_t opcode_names_hash
= NULL
;
882 /* Initialization for hash table of opcode name. */
885 init_opcode_names_hash (void)
887 const struct opcode_name_t
*opcode
;
889 for (opcode
= &opcode_name_list
[0]; opcode
->name
!= NULL
; ++opcode
)
890 if (str_hash_insert (opcode_names_hash
, opcode
->name
, opcode
, 0) != NULL
)
891 as_fatal (_("internal: duplicate %s"), opcode
->name
);
894 /* Find `s` is a valid opcode name or not, return the opcode name info
897 static const struct opcode_name_t
*
898 opcode_name_lookup (char **s
)
902 struct opcode_name_t
*o
;
904 /* Find end of name. */
906 if (is_name_beginner (*e
))
908 while (is_part_of_name (*e
))
911 /* Terminate name. */
915 o
= (struct opcode_name_t
*) str_hash_find (opcode_names_hash
, *s
);
917 /* Advance to next token if one was recognized. */
927 /* All RISC-V registers belong to one of these classes. */
939 static htab_t reg_names_hash
= NULL
;
940 static htab_t csr_extra_hash
= NULL
;
942 #define ENCODE_REG_HASH(cls, n) \
943 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
944 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
945 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
948 hash_reg_name (enum reg_class
class, const char *name
, unsigned n
)
950 void *hash
= ENCODE_REG_HASH (class, n
);
951 if (str_hash_insert (reg_names_hash
, name
, hash
, 0) != NULL
)
952 as_fatal (_("internal: duplicate %s"), name
);
956 hash_reg_names (enum reg_class
class, const char names
[][NRC
], unsigned n
)
960 for (i
= 0; i
< n
; i
++)
961 hash_reg_name (class, names
[i
], i
);
964 /* Init hash table csr_extra_hash to handle CSR. */
967 riscv_init_csr_hash (const char *name
,
969 enum riscv_csr_class
class,
970 enum riscv_spec_class define_version
,
971 enum riscv_spec_class abort_version
)
973 struct riscv_csr_extra
*entry
, *pre_entry
;
974 bool need_enrty
= true;
977 entry
= (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, name
);
978 while (need_enrty
&& entry
!= NULL
)
980 if (entry
->csr_class
== class
981 && entry
->address
== address
982 && entry
->define_version
== define_version
983 && entry
->abort_version
== abort_version
)
993 entry
= notes_alloc (sizeof (*entry
));
994 entry
->csr_class
= class;
995 entry
->address
= address
;
996 entry
->define_version
= define_version
;
997 entry
->abort_version
= abort_version
;
1000 if (pre_entry
== NULL
)
1001 str_hash_insert (csr_extra_hash
, name
, entry
, 0);
1003 pre_entry
->next
= entry
;
1006 /* Return the CSR address after checking the ISA dependency and
1007 the privileged spec version.
1009 There are one warning and two errors for CSR,
1011 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1012 or the privileged spec, report warning only if -mcsr-check is set.
1013 Unknown CSR: the CSR has never been defined, report error.
1014 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1017 riscv_csr_address (const char *csr_name
,
1018 struct riscv_csr_extra
*entry
)
1020 struct riscv_csr_extra
*saved_entry
= entry
;
1021 enum riscv_csr_class csr_class
= entry
->csr_class
;
1022 bool need_check_version
= false;
1023 bool is_rv32_only
= false;
1024 bool is_h_required
= false;
1025 const char* extension
= NULL
;
1029 case CSR_CLASS_I_32
:
1030 is_rv32_only
= true;
1033 need_check_version
= true;
1036 case CSR_CLASS_H_32
:
1037 is_rv32_only
= true;
1049 extension
= "zve32x";
1051 case CSR_CLASS_SMAIA_32
:
1052 is_rv32_only
= true;
1054 case CSR_CLASS_SMAIA
:
1055 extension
= "smaia";
1057 case CSR_CLASS_SMCNTRPMF_32
:
1058 is_rv32_only
= true;
1060 case CSR_CLASS_SMCNTRPMF
:
1061 need_check_version
= true;
1062 extension
= "smcntrpmf";
1064 case CSR_CLASS_SMSTATEEN_32
:
1065 is_rv32_only
= true;
1067 case CSR_CLASS_SMSTATEEN
:
1068 extension
= "smstateen";
1070 case CSR_CLASS_SSAIA
:
1071 case CSR_CLASS_SSAIA_AND_H
:
1072 case CSR_CLASS_SSAIA_32
:
1073 case CSR_CLASS_SSAIA_AND_H_32
:
1074 is_rv32_only
= (csr_class
== CSR_CLASS_SSAIA_32
1075 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1076 is_h_required
= (csr_class
== CSR_CLASS_SSAIA_AND_H
1077 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1078 extension
= "ssaia";
1080 case CSR_CLASS_SSSTATEEN_AND_H_32
:
1081 is_rv32_only
= true;
1083 case CSR_CLASS_SSSTATEEN_AND_H
:
1084 is_h_required
= true;
1086 case CSR_CLASS_SSSTATEEN
:
1087 extension
= "ssstateen";
1089 case CSR_CLASS_SSCOFPMF_32
:
1090 is_rv32_only
= true;
1092 case CSR_CLASS_SSCOFPMF
:
1093 extension
= "sscofpmf";
1095 case CSR_CLASS_SSTC
:
1096 case CSR_CLASS_SSTC_AND_H
:
1097 case CSR_CLASS_SSTC_32
:
1098 case CSR_CLASS_SSTC_AND_H_32
:
1099 is_rv32_only
= (csr_class
== CSR_CLASS_SSTC_32
1100 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1101 is_h_required
= (csr_class
== CSR_CLASS_SSTC_AND_H
1102 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1105 case CSR_CLASS_DEBUG
:
1108 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class
);
1111 if (riscv_opts
.csr_check
)
1113 if (is_rv32_only
&& xlen
!= 32)
1114 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name
);
1115 if (is_h_required
&& !riscv_subset_supports (&riscv_rps_as
, "h"))
1116 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name
);
1118 if (extension
!= NULL
1119 && !riscv_subset_supports (&riscv_rps_as
, extension
))
1120 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1121 csr_name
, extension
);
1124 while (entry
!= NULL
)
1126 if (!need_check_version
1127 || (default_priv_spec
>= entry
->define_version
1128 && default_priv_spec
< entry
->abort_version
))
1130 /* Find the CSR according to the specific version. */
1131 return entry
->address
;
1133 entry
= entry
->next
;
1136 /* Can not find the CSR address from the chosen privileged version,
1137 so use the newly defined value. */
1138 if (riscv_opts
.csr_check
)
1140 const char *priv_name
= NULL
;
1141 RISCV_GET_PRIV_SPEC_NAME (priv_name
, default_priv_spec
);
1142 if (priv_name
!= NULL
)
1143 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1144 csr_name
, priv_name
);
1147 return saved_entry
->address
;
1150 /* Return -1 if the CSR has never been defined. Otherwise, return
1154 reg_csr_lookup_internal (const char *s
)
1156 struct riscv_csr_extra
*r
=
1157 (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, s
);
1162 return riscv_csr_address (s
, r
);
1166 reg_lookup_internal (const char *s
, enum reg_class
class)
1170 if (class == RCLASS_CSR
)
1171 return reg_csr_lookup_internal (s
);
1173 r
= str_hash_find (reg_names_hash
, s
);
1174 if (r
== NULL
|| DECODE_REG_CLASS (r
) != class)
1177 if (riscv_subset_supports (&riscv_rps_as
, "e")
1178 && class == RCLASS_GPR
1179 && DECODE_REG_NUM (r
) > 15)
1182 return DECODE_REG_NUM (r
);
1186 reg_lookup (char **s
, enum reg_class
class, unsigned int *regnop
)
1192 /* Find end of name. */
1194 if (is_name_beginner (*e
))
1196 while (is_part_of_name (*e
))
1199 /* Terminate name. */
1203 /* Look for the register. Advance to next token if one was recognized. */
1204 if ((reg
= reg_lookup_internal (*s
, class)) >= 0)
1214 arg_lookup (char **s
, const char *const *array
, size_t size
, unsigned *regnop
)
1216 const char *p
= strchr (*s
, ',');
1217 size_t i
, len
= p
? (size_t)(p
- *s
) : strlen (*s
);
1222 for (i
= 0; i
< size
; i
++)
1223 if (array
[i
] != NULL
&& strncmp (array
[i
], *s
, len
) == 0
1224 && array
[i
][len
] == '\0')
1235 flt_lookup (float f
, const float *array
, size_t size
, unsigned *regnop
)
1239 for (i
= 0; i
< size
; i
++)
1249 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1250 #define USE_IMM(n, s) \
1251 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1253 /* For consistency checking, verify that all bits are specified either
1254 by the match/mask part of the instruction definition, or by the
1255 operand list. The `length` could be the actual instruction length or
1256 0 for auto-detection. */
1259 validate_riscv_insn (const struct riscv_opcode
*opc
, int length
)
1261 const char *oparg
, *opargStart
;
1262 insn_t used_bits
= opc
->mask
;
1264 insn_t required_bits
;
1267 length
= riscv_insn_length (opc
->match
);
1268 /* We don't support instructions longer than 64-bits yet. */
1271 insn_width
= 8 * length
;
1273 required_bits
= ((insn_t
)~0ULL) >> (64 - insn_width
);
1275 if ((used_bits
& opc
->match
) != (opc
->match
& required_bits
))
1277 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1278 opc
->name
, opc
->args
);
1282 for (oparg
= opc
->args
; *oparg
; ++oparg
)
1290 case 'U': break; /* CRS1, constrained to equal RD. */
1291 case 'c': break; /* CRS1, constrained to equal sp. */
1292 case 'T': /* CRS2, floating point. */
1293 case 'V': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
1294 case 'S': /* CRS1S, floating point. */
1295 case 's': USE_BITS (OP_MASK_CRS1S
, OP_SH_CRS1S
); break;
1296 case 'w': break; /* CRS1S, constrained to equal RD. */
1297 case 'D': /* CRS2S, floating point. */
1298 case 't': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
1299 case 'x': break; /* CRS2S, constrained to equal RD. */
1300 case 'z': break; /* CRS2S, constrained to be x0. */
1301 case '>': /* CITYPE immediate, compressed shift. */
1302 case 'u': /* CITYPE immediate, compressed lui. */
1303 case 'v': /* CITYPE immediate, li to compressed lui. */
1304 case 'o': /* CITYPE immediate, allow zero. */
1305 case 'j': used_bits
|= ENCODE_CITYPE_IMM (-1U); break;
1306 case 'L': used_bits
|= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1307 case 'm': used_bits
|= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1308 case 'n': used_bits
|= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1309 case '6': used_bits
|= ENCODE_CSSTYPE_IMM (-1U); break;
1310 case 'M': used_bits
|= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1311 case 'N': used_bits
|= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1312 case '8': used_bits
|= ENCODE_CIWTYPE_IMM (-1U); break;
1313 case 'K': used_bits
|= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1314 /* CLTYPE and CSTYPE have the same immediate encoding. */
1315 case '5': used_bits
|= ENCODE_CLTYPE_IMM (-1U); break;
1316 case 'k': used_bits
|= ENCODE_CLTYPE_LW_IMM (-1U); break;
1317 case 'l': used_bits
|= ENCODE_CLTYPE_LD_IMM (-1U); break;
1318 case 'p': used_bits
|= ENCODE_CBTYPE_IMM (-1U); break;
1319 case 'a': used_bits
|= ENCODE_CJTYPE_IMM (-1U); break;
1320 case 'F': /* Compressed funct for .insn directive. */
1323 case '6': USE_BITS (OP_MASK_CFUNCT6
, OP_SH_CFUNCT6
); break;
1324 case '4': USE_BITS (OP_MASK_CFUNCT4
, OP_SH_CFUNCT4
); break;
1325 case '3': USE_BITS (OP_MASK_CFUNCT3
, OP_SH_CFUNCT3
); break;
1326 case '2': USE_BITS (OP_MASK_CFUNCT2
, OP_SH_CFUNCT2
); break;
1328 goto unknown_validate_operand
;
1332 goto unknown_validate_operand
;
1334 break; /* end RVC */
1339 case 'f': USE_BITS (OP_MASK_VD
, OP_SH_VD
); break;
1340 case 'e': USE_BITS (OP_MASK_VWD
, OP_SH_VWD
); break;
1341 case 's': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
); break;
1342 case 't': USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1343 case 'u': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1344 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1345 case 'v': USE_BITS (OP_MASK_VD
, OP_SH_VD
);
1346 USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1347 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1349 case 'b': used_bits
|= ENCODE_RVV_VB_IMM (-1U); break;
1350 case 'c': used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1353 case 'k': USE_BITS (OP_MASK_VIMM
, OP_SH_VIMM
); break;
1354 case 'l': used_bits
|= ENCODE_RVV_VI_UIMM6 (-1U); break;
1355 case 'm': USE_BITS (OP_MASK_VMASK
, OP_SH_VMASK
); break;
1356 case 'M': break; /* Macro operand, must be a mask register. */
1357 case 'T': break; /* Macro operand, must be a vector register. */
1359 goto unknown_validate_operand
;
1361 break; /* end RVV */
1365 case '<': USE_BITS (OP_MASK_SHAMTW
, OP_SH_SHAMTW
); break;
1366 case '>': USE_BITS (OP_MASK_SHAMT
, OP_SH_SHAMT
); break;
1367 case 'A': break; /* Macro operand, must be symbol. */
1368 case 'B': break; /* Macro operand, must be symbol or constant. */
1369 case 'c': break; /* Macro operand, must be symbol or constant. */
1370 case 'I': break; /* Macro operand, must be constant. */
1371 case 'D': /* RD, floating point. */
1372 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1373 case 'y': USE_BITS (OP_MASK_BS
, OP_SH_BS
); break;
1374 case 'Y': USE_BITS (OP_MASK_RNUM
, OP_SH_RNUM
); break;
1375 case 'Z': /* RS1, CSR number. */
1376 case 'S': /* RS1, floating point. */
1377 case 's': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1378 case 'U': /* RS1 and RS2 are the same, floating point. */
1379 USE_BITS (OP_MASK_RS1
, OP_SH_RS1
);
1381 case 'T': /* RS2, floating point. */
1382 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1383 case 'R': /* RS3, floating point. */
1384 case 'r': USE_BITS (OP_MASK_RS3
, OP_SH_RS3
); break;
1385 case 'm': USE_BITS (OP_MASK_RM
, OP_SH_RM
); break;
1386 case 'E': USE_BITS (OP_MASK_CSR
, OP_SH_CSR
); break;
1387 case 'P': USE_BITS (OP_MASK_PRED
, OP_SH_PRED
); break;
1388 case 'Q': USE_BITS (OP_MASK_SUCC
, OP_SH_SUCC
); break;
1389 case 'o': /* ITYPE immediate, load displacement. */
1390 case 'j': used_bits
|= ENCODE_ITYPE_IMM (-1U); break;
1391 case 'a': used_bits
|= ENCODE_JTYPE_IMM (-1U); break;
1392 case 'p': used_bits
|= ENCODE_BTYPE_IMM (-1U); break;
1393 case 'q': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1394 case 'u': used_bits
|= ENCODE_UTYPE_IMM (-1U); break;
1395 case 'z': break; /* Zero immediate. */
1396 case '[': break; /* Unused operand. */
1397 case ']': break; /* Unused operand. */
1398 case '0': break; /* AMO displacement, must to zero. */
1399 case '1': break; /* Relaxation operand. */
1400 case 'F': /* Funct for .insn directive. */
1403 case '7': USE_BITS (OP_MASK_FUNCT7
, OP_SH_FUNCT7
); break;
1404 case '3': USE_BITS (OP_MASK_FUNCT3
, OP_SH_FUNCT3
); break;
1405 case '2': USE_BITS (OP_MASK_FUNCT2
, OP_SH_FUNCT2
); break;
1407 goto unknown_validate_operand
;
1410 case 'O': /* Opcode for .insn directive. */
1413 case '4': USE_BITS (OP_MASK_OP
, OP_SH_OP
); break;
1414 case '2': USE_BITS (OP_MASK_OP2
, OP_SH_OP2
); break;
1416 goto unknown_validate_operand
;
1419 case 'W': /* Various operands for standard z extensions. */
1425 case 'f': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1427 goto unknown_validate_operand
;
1433 case 'v': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1435 goto unknown_validate_operand
;
1441 /* byte immediate operators, load/store byte insns. */
1442 case 'h': used_bits
|= ENCODE_ZCB_HALFWORD_UIMM (-1U); break;
1443 /* halfword immediate operators, load/store halfword insns. */
1444 case 'b': used_bits
|= ENCODE_ZCB_BYTE_UIMM (-1U); break;
1447 goto unknown_validate_operand
;
1451 goto unknown_validate_operand
;
1454 case 'X': /* Vendor-specific operands. */
1457 case 't': /* Vendor-specific (T-head) operands. */
1463 case 'l': /* Integer immediate, literal. */
1464 oparg
+= strcspn(oparg
, ",") - 1;
1466 case 's': /* Integer immediate, 'XtsN@S' ... N-bit signed immediate at bit S. */
1468 case 'u': /* Integer immediate, 'XtuN@S' ... N-bit unsigned immediate at bit S. */
1471 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1473 goto unknown_validate_operand
;
1474 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1480 goto unknown_validate_operand
;
1485 goto unknown_validate_operand
;
1489 unknown_validate_operand
:
1490 as_bad (_("internal: bad RISC-V opcode "
1491 "(unknown operand type `%s'): %s %s"),
1492 opargStart
, opc
->name
, opc
->args
);
1497 if (used_bits
!= required_bits
)
1499 as_bad (_("internal: bad RISC-V opcode "
1500 "(bits %#llx undefined or invalid): %s %s"),
1501 (unsigned long long)(used_bits
^ required_bits
),
1502 opc
->name
, opc
->args
);
1510 struct percent_op_match
1513 bfd_reloc_code_real_type reloc
;
1516 /* Common hash table initialization function for instruction and .insn
1520 init_opcode_hash (const struct riscv_opcode
*opcodes
,
1521 bool insn_directive_p
)
1525 htab_t hash
= str_htab_create ();
1526 while (opcodes
[i
].name
)
1528 const char *name
= opcodes
[i
].name
;
1529 if (str_hash_insert (hash
, name
, &opcodes
[i
], 0) != NULL
)
1530 as_fatal (_("internal: duplicate %s"), name
);
1534 if (opcodes
[i
].pinfo
!= INSN_MACRO
)
1536 if (insn_directive_p
)
1537 length
= ((name
[0] == 'c') ? 2 : 4);
1539 length
= 0; /* Let assembler determine the length. */
1540 if (!validate_riscv_insn (&opcodes
[i
], length
))
1541 as_fatal (_("internal: broken assembler. "
1542 "No assembly attempted"));
1545 gas_assert (!insn_directive_p
);
1548 while (opcodes
[i
].name
&& !strcmp (opcodes
[i
].name
, name
));
1554 /* This function is called once, at assembler startup time. It should set up
1555 all the tables, etc. that the MD part of the assembler will need. */
1560 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
1562 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
1563 as_warn (_("could not set architecture and machine"));
1565 op_hash
= init_opcode_hash (riscv_opcodes
, false);
1566 insn_type_hash
= init_opcode_hash (riscv_insn_types
, true);
1568 reg_names_hash
= str_htab_create ();
1569 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_numeric
, NGPR
);
1570 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_abi
, NGPR
);
1571 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_numeric
, NFPR
);
1572 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_abi
, NFPR
);
1573 hash_reg_names (RCLASS_VECR
, riscv_vecr_names_numeric
, NVECR
);
1574 hash_reg_names (RCLASS_VECM
, riscv_vecm_names_numeric
, NVECM
);
1575 /* Add "fp" as an alias for "s0". */
1576 hash_reg_name (RCLASS_GPR
, "fp", 8);
1578 /* Create and insert CSR hash tables. */
1579 csr_extra_hash
= str_htab_create ();
1580 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1581 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1582 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1583 DECLARE_CSR(name, num, class, define_version, abort_version);
1584 #include "opcode/riscv-opc.h"
1587 opcode_names_hash
= str_htab_create ();
1588 init_opcode_names_hash ();
1590 /* Set the default alignment for the text section. */
1591 record_alignment (text_section
, riscv_opts
.rvc
? 1 : 2);
1595 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type
, bfd_vma value
)
1602 case BFD_RELOC_RISCV_HI20
:
1603 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1605 case BFD_RELOC_RISCV_LO12_S
:
1606 return ENCODE_STYPE_IMM (value
);
1608 case BFD_RELOC_RISCV_LO12_I
:
1609 return ENCODE_ITYPE_IMM (value
);
1616 /* Output an instruction. IP is the instruction information.
1617 ADDRESS_EXPR is an operand of the instruction to be used with
1621 append_insn (struct riscv_cl_insn
*ip
, expressionS
*address_expr
,
1622 bfd_reloc_code_real_type reloc_type
)
1624 dwarf2_emit_insn (0);
1626 if (reloc_type
!= BFD_RELOC_UNUSED
)
1628 reloc_howto_type
*howto
;
1630 gas_assert (address_expr
);
1631 if (reloc_type
== BFD_RELOC_12_PCREL
1632 || reloc_type
== BFD_RELOC_RISCV_JMP
)
1634 int j
= reloc_type
== BFD_RELOC_RISCV_JMP
;
1635 int best_case
= insn_length (ip
);
1636 unsigned worst_case
= relaxed_branch_length (NULL
, NULL
, 0);
1638 if (now_seg
== absolute_section
)
1640 as_bad (_("relaxable branches not supported in absolute section"));
1644 add_relaxed_insn (ip
, worst_case
, best_case
,
1645 RELAX_BRANCH_ENCODE (j
, best_case
== 2, worst_case
),
1646 address_expr
->X_add_symbol
,
1647 address_expr
->X_add_number
);
1652 howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1654 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1657 ip
->fixp
= fix_new_exp (ip
->frag
, ip
->where
,
1658 bfd_get_reloc_size (howto
),
1659 address_expr
, false, reloc_type
);
1661 ip
->fixp
->fx_tcbit
= riscv_opts
.relax
;
1665 add_fixed_insn (ip
);
1667 /* We need to start a new frag after any instruction that can be
1668 optimized away or compressed by the linker during relaxation, to prevent
1669 the assembler from computing static offsets across such an instruction.
1670 This is necessary to get correct EH info. */
1671 if (reloc_type
== BFD_RELOC_RISCV_HI20
1672 || reloc_type
== BFD_RELOC_RISCV_PCREL_HI20
1673 || reloc_type
== BFD_RELOC_RISCV_TPREL_HI20
1674 || reloc_type
== BFD_RELOC_RISCV_TPREL_ADD
)
1676 frag_wane (frag_now
);
1681 /* Build an instruction created by a macro expansion. This is passed
1682 a pointer to the count of instructions created so far, an expression,
1683 the name of the instruction to build, an operand format string, and
1684 corresponding arguments. */
1687 macro_build (expressionS
*ep
, const char *name
, const char *fmt
, ...)
1689 const struct riscv_opcode
*mo
;
1690 struct riscv_cl_insn insn
;
1691 bfd_reloc_code_real_type r
;
1693 const char *fmtStart
;
1695 va_start (args
, fmt
);
1697 r
= BFD_RELOC_UNUSED
;
1698 mo
= (struct riscv_opcode
*) str_hash_find (op_hash
, name
);
1701 /* Find a non-RVC variant of the instruction. append_insn will compress
1703 while (riscv_insn_length (mo
->match
) < 4)
1705 gas_assert (strcmp (name
, mo
->name
) == 0);
1707 create_insn (&insn
, mo
);
1717 INSERT_OPERAND (VD
, insn
, va_arg (args
, int));
1720 INSERT_OPERAND (VS1
, insn
, va_arg (args
, int));
1723 INSERT_OPERAND (VS2
, insn
, va_arg (args
, int));
1727 int reg
= va_arg (args
, int);
1730 INSERT_OPERAND (VMASK
, insn
, 1);
1735 INSERT_OPERAND (VMASK
, insn
, 0);
1739 goto unknown_macro_argument
;
1742 goto unknown_macro_argument
;
1747 INSERT_OPERAND (RD
, insn
, va_arg (args
, int));
1750 INSERT_OPERAND (RS1
, insn
, va_arg (args
, int));
1753 INSERT_OPERAND (RS2
, insn
, va_arg (args
, int));
1759 gas_assert (ep
!= NULL
);
1760 r
= va_arg (args
, int);
1768 unknown_macro_argument
:
1769 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart
);
1774 gas_assert (r
== BFD_RELOC_UNUSED
? ep
== NULL
: ep
!= NULL
);
1776 append_insn (&insn
, ep
, r
);
1779 /* Build an instruction created by a macro expansion. Like md_assemble but
1780 accept a printf-style format string and arguments. */
1783 md_assemblef (const char *format
, ...)
1789 va_start (ap
, format
);
1791 r
= vasprintf (&buf
, format
, ap
);
1794 as_fatal (_("internal: vasprintf failed"));
1802 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1806 normalize_constant_expr (expressionS
*ex
)
1810 if ((ex
->X_op
== O_constant
|| ex
->X_op
== O_symbol
)
1811 && IS_ZEXT_32BIT_NUM (ex
->X_add_number
))
1812 ex
->X_add_number
= (((ex
->X_add_number
& 0xffffffff) ^ 0x80000000)
1816 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1817 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1820 check_absolute_expr (struct riscv_cl_insn
*ip
, expressionS
*ex
,
1823 if (ex
->X_op
== O_big
)
1824 as_bad (_("unsupported large constant"));
1825 else if (maybe_csr
&& ex
->X_op
== O_symbol
)
1826 as_bad (_("unknown CSR `%s'"),
1827 S_GET_NAME (ex
->X_add_symbol
));
1828 else if (ex
->X_op
!= O_constant
)
1829 as_bad (_("instruction %s requires absolute expression"),
1831 normalize_constant_expr (ex
);
1835 make_internal_label (void)
1837 return (symbolS
*) local_symbol_make (FAKE_LABEL_NAME
, now_seg
, frag_now
,
1841 /* Load an entry from the GOT. */
1844 pcrel_access (int destreg
, int tempreg
, expressionS
*ep
,
1845 const char *lo_insn
, const char *lo_pattern
,
1846 bfd_reloc_code_real_type hi_reloc
,
1847 bfd_reloc_code_real_type lo_reloc
)
1850 ep2
.X_op
= O_symbol
;
1851 ep2
.X_add_symbol
= make_internal_label ();
1852 ep2
.X_add_number
= 0;
1854 macro_build (ep
, "auipc", "d,u", tempreg
, hi_reloc
);
1855 macro_build (&ep2
, lo_insn
, lo_pattern
, destreg
, tempreg
, lo_reloc
);
1859 pcrel_load (int destreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
1860 bfd_reloc_code_real_type hi_reloc
,
1861 bfd_reloc_code_real_type lo_reloc
)
1863 pcrel_access (destreg
, tempreg
, ep
, lo_insn
, "d,s,j", hi_reloc
, lo_reloc
);
1867 pcrel_store (int srcreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
1868 bfd_reloc_code_real_type hi_reloc
,
1869 bfd_reloc_code_real_type lo_reloc
)
1871 pcrel_access (srcreg
, tempreg
, ep
, lo_insn
, "t,s,q", hi_reloc
, lo_reloc
);
1874 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1877 riscv_call (int destreg
, int tempreg
, expressionS
*ep
,
1878 bfd_reloc_code_real_type reloc
)
1880 /* Ensure the jalr is emitted to the same frag as the auipc. */
1882 macro_build (ep
, "auipc", "d,u", tempreg
, reloc
);
1883 macro_build (NULL
, "jalr", "d,s", destreg
, tempreg
);
1884 /* See comment at end of append_insn. */
1885 frag_wane (frag_now
);
1889 /* Load an integer constant into a register. */
1892 load_const (int reg
, expressionS
*ep
)
1894 int shift
= RISCV_IMM_BITS
;
1895 bfd_vma upper_imm
, sign
= (bfd_vma
) 1 << (RISCV_IMM_BITS
- 1);
1896 expressionS upper
= *ep
, lower
= *ep
;
1897 lower
.X_add_number
= ((ep
->X_add_number
& (sign
+ sign
- 1)) ^ sign
) - sign
;
1898 upper
.X_add_number
-= lower
.X_add_number
;
1900 if (ep
->X_op
!= O_constant
)
1902 as_bad (_("unsupported large constant"));
1906 if (xlen
> 32 && !IS_SEXT_32BIT_NUM (ep
->X_add_number
))
1908 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1909 while (((upper
.X_add_number
>> shift
) & 1) == 0)
1912 upper
.X_add_number
= (int64_t) upper
.X_add_number
>> shift
;
1913 load_const (reg
, &upper
);
1915 md_assemblef ("slli x%d, x%d, 0x%x", reg
, reg
, shift
);
1916 if (lower
.X_add_number
!= 0)
1917 md_assemblef ("addi x%d, x%d, %" PRId64
, reg
, reg
,
1918 (int64_t) lower
.X_add_number
);
1922 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1925 if (upper
.X_add_number
!= 0)
1927 /* Discard low part and zero-extend upper immediate. */
1928 upper_imm
= ((uint32_t)upper
.X_add_number
>> shift
);
1930 md_assemblef ("lui x%d, 0x%" PRIx64
, reg
, (uint64_t) upper_imm
);
1934 if (lower
.X_add_number
!= 0 || hi_reg
== 0)
1935 md_assemblef ("%s x%d, x%d, %" PRId64
, ADD32_INSN
, reg
, hi_reg
,
1936 (int64_t) lower
.X_add_number
);
1940 /* Zero extend and sign extend byte/half-word/word. */
1943 riscv_ext (int destreg
, int srcreg
, unsigned shift
, bool sign
)
1947 md_assemblef ("slli x%d, x%d, 0x%x", destreg
, srcreg
, shift
);
1948 md_assemblef ("srai x%d, x%d, 0x%x", destreg
, destreg
, shift
);
1952 md_assemblef ("slli x%d, x%d, 0x%x", destreg
, srcreg
, shift
);
1953 md_assemblef ("srli x%d, x%d, 0x%x", destreg
, destreg
, shift
);
1957 /* Expand RISC-V Vector macros into one or more instructions. */
1960 vector_macro (struct riscv_cl_insn
*ip
)
1962 int vd
= (ip
->insn_opcode
>> OP_SH_VD
) & OP_MASK_VD
;
1963 int vs1
= (ip
->insn_opcode
>> OP_SH_VS1
) & OP_MASK_VS1
;
1964 int vs2
= (ip
->insn_opcode
>> OP_SH_VS2
) & OP_MASK_VS2
;
1965 int vm
= (ip
->insn_opcode
>> OP_SH_VMASK
) & OP_MASK_VMASK
;
1966 int vtemp
= (ip
->insn_opcode
>> OP_SH_VFUNCT6
) & OP_MASK_VFUNCT6
;
1967 const char *vmslt_vx
= ip
->insn_mo
->match
? "vmsltu.vx" : "vmslt.vx";
1968 int mask
= ip
->insn_mo
->mask
;
1976 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, -1);
1977 macro_build (NULL
, "vmnand.mm", "Vd,Vt,Vs", vd
, vd
, vd
);
1982 /* Masked. Have vtemp to avoid overlap constraints. */
1985 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
1986 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vm
, vtemp
);
1990 /* Preserve the value of vd if not updating by vm. */
1991 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
1992 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vtemp
, vm
, vtemp
);
1993 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
1994 macro_build (NULL
, "vmor.mm", "Vd,Vt,Vs", vd
, vtemp
, vd
);
1999 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2000 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, vm
);
2001 macro_build (NULL
, "vmxor.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
2004 as_bad (_("must provide temp if destination overlaps mask"));
2012 /* Expand RISC-V assembly macros into one or more instructions. */
2015 macro (struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2016 bfd_reloc_code_real_type
*imm_reloc
)
2018 int rd
= (ip
->insn_opcode
>> OP_SH_RD
) & OP_MASK_RD
;
2019 int rs1
= (ip
->insn_opcode
>> OP_SH_RS1
) & OP_MASK_RS1
;
2020 int rs2
= (ip
->insn_opcode
>> OP_SH_RS2
) & OP_MASK_RS2
;
2021 int mask
= ip
->insn_mo
->mask
;
2026 load_const (rd
, imm_expr
);
2032 /* Load the address of a symbol into a register. */
2033 if (!IS_SEXT_32BIT_NUM (imm_expr
->X_add_number
))
2034 as_bad (_("offset too large"));
2036 if (imm_expr
->X_op
== O_constant
)
2037 load_const (rd
, imm_expr
);
2038 /* Global PIC symbol. */
2039 else if ((riscv_opts
.pic
&& mask
== M_LA
)
2041 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2042 BFD_RELOC_RISCV_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2043 /* Local PIC symbol, or any non-PIC symbol. */
2045 pcrel_load (rd
, rd
, imm_expr
, "addi",
2046 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2050 pcrel_load (rd
, rd
, imm_expr
, "addi",
2051 BFD_RELOC_RISCV_TLS_GD_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2055 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2056 BFD_RELOC_RISCV_TLS_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2060 pcrel_load (rd
, rd
, imm_expr
, "lb",
2061 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2065 pcrel_load (rd
, rd
, imm_expr
, "lbu",
2066 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2070 pcrel_load (rd
, rd
, imm_expr
, "lh",
2071 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2075 pcrel_load (rd
, rd
, imm_expr
, "lhu",
2076 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2080 pcrel_load (rd
, rd
, imm_expr
, "lw",
2081 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2085 pcrel_load (rd
, rd
, imm_expr
, "lwu",
2086 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2090 pcrel_load (rd
, rd
, imm_expr
, "ld",
2091 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2095 pcrel_load (rd
, rs1
, imm_expr
, "flw",
2096 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2100 pcrel_load (rd
, rs1
, imm_expr
, "fld",
2101 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2105 pcrel_store (rs2
, rs1
, imm_expr
, "sb",
2106 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2110 pcrel_store (rs2
, rs1
, imm_expr
, "sh",
2111 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2115 pcrel_store (rs2
, rs1
, imm_expr
, "sw",
2116 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2120 pcrel_store (rs2
, rs1
, imm_expr
, "sd",
2121 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2125 pcrel_store (rs2
, rs1
, imm_expr
, "fsw",
2126 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2130 pcrel_store (rs2
, rs1
, imm_expr
, "fsd",
2131 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2135 riscv_call (rd
, rs1
, imm_expr
, *imm_reloc
);
2139 riscv_ext (rd
, rs1
, xlen
- 16, false);
2143 riscv_ext (rd
, rs1
, xlen
- 32, false);
2147 riscv_ext (rd
, rs1
, xlen
- 8, true);
2151 riscv_ext (rd
, rs1
, xlen
- 16, true);
2159 pcrel_load (rd
, rs1
, imm_expr
, "flh",
2160 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2163 pcrel_store (rs2
, rs1
, imm_expr
, "fsh",
2164 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2168 as_bad (_("internal: macro %s not implemented"), ip
->insn_mo
->name
);
2173 static const struct percent_op_match percent_op_utype
[] =
2175 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20
},
2176 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20
},
2177 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20
},
2178 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20
},
2179 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20
},
2180 {"hi", BFD_RELOC_RISCV_HI20
},
2184 static const struct percent_op_match percent_op_itype
[] =
2186 {"lo", BFD_RELOC_RISCV_LO12_I
},
2187 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I
},
2188 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I
},
2192 static const struct percent_op_match percent_op_stype
[] =
2194 {"lo", BFD_RELOC_RISCV_LO12_S
},
2195 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S
},
2196 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S
},
2200 static const struct percent_op_match percent_op_rtype
[] =
2202 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD
},
2206 static const struct percent_op_match percent_op_null
[] =
2211 /* Return true if *STR points to a relocation operator. When returning true,
2212 move *STR over the operator and store its relocation code in *RELOC.
2213 Leave both *STR and *RELOC alone when returning false. */
2216 parse_relocation (char **str
, bfd_reloc_code_real_type
*reloc
,
2217 const struct percent_op_match
*percent_op
)
2219 for ( ; percent_op
->str
; percent_op
++)
2220 if (strncasecmp (*str
+ 1, percent_op
->str
, strlen (percent_op
->str
)) == 0)
2222 size_t len
= 1 + strlen (percent_op
->str
);
2224 while (ISSPACE ((*str
)[len
]))
2226 if ((*str
)[len
] != '(')
2230 *reloc
= percent_op
->reloc
;
2232 /* Check whether the output BFD supports this relocation.
2233 If not, issue an error and fall back on something safe. */
2234 if (*reloc
!= BFD_RELOC_UNUSED
2235 && !bfd_reloc_type_lookup (stdoutput
, *reloc
))
2237 as_bad ("internal: relocation %s isn't supported by the "
2238 "current ABI", percent_op
->str
);
2239 *reloc
= BFD_RELOC_UNUSED
;
2247 my_getExpression (expressionS
*ep
, char *str
)
2251 save_in
= input_line_pointer
;
2252 input_line_pointer
= str
;
2254 expr_parse_end
= input_line_pointer
;
2255 input_line_pointer
= save_in
;
2258 /* Parse string STR as a 16-bit relocatable operand. Store the
2259 expression in *EP and the relocation, if any, in RELOC.
2260 Return the number of relocation operators used (0 or 1).
2262 On exit, EXPR_PARSE_END points to the first character after the
2266 my_getSmallExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2267 char *str
, const struct percent_op_match
*percent_op
)
2270 unsigned crux_depth
, str_depth
;
2271 bool orig_probing
= probing_insn_operands
;
2274 /* Search for the start of the main expression.
2276 End the loop with CRUX pointing to the start of the main expression and
2277 with CRUX_DEPTH containing the number of open brackets at that point. */
2284 crux_depth
= str_depth
;
2286 /* Skip over whitespace and brackets, keeping count of the number
2288 while (*str
== ' ' || *str
== '\t' || *str
== '(')
2294 && parse_relocation (&str
, reloc
, percent_op
));
2298 /* expression() will choke on anything looking like an (unrecognized)
2299 relocation specifier. Don't even call it, avoiding multiple (and
2300 perhaps redundant) error messages; our caller will issue one. */
2301 ep
->X_op
= O_illegal
;
2305 /* Anything inside parentheses or subject to a relocation operator cannot
2306 be a register and hence can be treated the same as operands to
2307 directives (other than .insn). */
2308 if (str_depth
|| reloc_index
)
2309 probing_insn_operands
= false;
2311 my_getExpression (ep
, crux
);
2312 str
= expr_parse_end
;
2314 probing_insn_operands
= orig_probing
;
2316 /* Match every open bracket. */
2317 while (crux_depth
> 0 && (*str
== ')' || *str
== ' ' || *str
== '\t'))
2322 as_bad ("unclosed '('");
2324 expr_parse_end
= str
;
2329 /* Parse opcode name, could be an mnemonics or number. */
2332 my_getOpcodeExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2335 const struct opcode_name_t
*o
= opcode_name_lookup (&str
);
2339 ep
->X_op
= O_constant
;
2340 ep
->X_add_number
= o
->val
;
2344 return my_getSmallExpression (ep
, reloc
, str
, percent_op_null
);
2347 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2348 On exit, EXPR_PARSE_END points to the first character after the
2352 my_getVsetvliExpression (expressionS
*ep
, char *str
)
2354 unsigned int vsew_value
= 0, vlmul_value
= 0;
2355 unsigned int vta_value
= 0, vma_value
= 0;
2356 bfd_boolean vsew_found
= FALSE
, vlmul_found
= FALSE
;
2357 bfd_boolean vta_found
= FALSE
, vma_found
= FALSE
;
2359 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
), &vsew_value
))
2364 as_bad (_("multiple vsew constants"));
2367 if (arg_lookup (&str
, riscv_vlmul
, ARRAY_SIZE (riscv_vlmul
), &vlmul_value
))
2372 as_bad (_("multiple vlmul constants"));
2375 if (arg_lookup (&str
, riscv_vta
, ARRAY_SIZE (riscv_vta
), &vta_value
))
2380 as_bad (_("multiple vta constants"));
2383 if (arg_lookup (&str
, riscv_vma
, ARRAY_SIZE (riscv_vma
), &vma_value
))
2388 as_bad (_("multiple vma constants"));
2392 if (vsew_found
|| vlmul_found
|| vta_found
|| vma_found
)
2394 ep
->X_op
= O_constant
;
2395 ep
->X_add_number
= (vlmul_value
<< OP_SH_VLMUL
)
2396 | (vsew_value
<< OP_SH_VSEW
)
2397 | (vta_value
<< OP_SH_VTA
)
2398 | (vma_value
<< OP_SH_VMA
);
2399 expr_parse_end
= str
;
2403 my_getExpression (ep
, str
);
2404 str
= expr_parse_end
;
2408 /* Detect and handle implicitly zero load-store offsets. For example,
2409 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2410 an implicit offset was detected. */
2413 riscv_handle_implicit_zero_offset (expressionS
*ep
, const char *s
)
2415 /* Check whether there is only a single bracketed expression left.
2416 If so, it must be the base register and the constant must be zero. */
2417 if (*s
== '(' && strchr (s
+ 1, '(') == 0)
2419 ep
->X_op
= O_constant
;
2420 ep
->X_add_number
= 0;
2427 /* All RISC-V CSR instructions belong to one of these classes. */
2436 /* Return which CSR instruction is checking. */
2438 static enum csr_insn_type
2439 riscv_csr_insn_type (insn_t insn
)
2441 if (((insn
^ MATCH_CSRRW
) & MASK_CSRRW
) == 0
2442 || ((insn
^ MATCH_CSRRWI
) & MASK_CSRRWI
) == 0)
2444 else if (((insn
^ MATCH_CSRRS
) & MASK_CSRRS
) == 0
2445 || ((insn
^ MATCH_CSRRSI
) & MASK_CSRRSI
) == 0)
2447 else if (((insn
^ MATCH_CSRRC
) & MASK_CSRRC
) == 0
2448 || ((insn
^ MATCH_CSRRCI
) & MASK_CSRRCI
) == 0)
2451 return INSN_NOT_CSR
;
2454 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2455 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2456 CSR address is 0x3. */
2459 riscv_csr_read_only_check (insn_t insn
)
2461 int csr
= (insn
& (OP_MASK_CSR
<< OP_SH_CSR
)) >> OP_SH_CSR
;
2462 int rs1
= (insn
& (OP_MASK_RS1
<< OP_SH_RS1
)) >> OP_SH_RS1
;
2463 int readonly
= (((csr
& (0x3 << 10)) >> 10) == 0x3);
2464 enum csr_insn_type csr_insn
= riscv_csr_insn_type (insn
);
2467 && (((csr_insn
== INSN_CSRRS
2468 || csr_insn
== INSN_CSRRC
)
2470 || csr_insn
== INSN_CSRRW
))
2476 /* Return true if it is a privileged instruction. Otherwise, return false.
2478 uret is actually a N-ext instruction. So it is better to regard it as
2479 an user instruction rather than the priv instruction.
2481 hret is used to return from traps in H-mode. H-mode is removed since
2482 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2483 Therefore, hret should be controlled by the hypervisor spec rather than
2484 priv spec in the future.
2486 dret is defined in the debug spec, so it should be checked in the future,
2490 riscv_is_priv_insn (insn_t insn
)
2492 return (((insn
^ MATCH_SRET
) & MASK_SRET
) == 0
2493 || ((insn
^ MATCH_MRET
) & MASK_MRET
) == 0
2494 || ((insn
^ MATCH_SFENCE_VMA
) & MASK_SFENCE_VMA
) == 0
2495 || ((insn
^ MATCH_WFI
) & MASK_WFI
) == 0
2496 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2497 check it here to keep the compatible. */
2498 || ((insn
^ MATCH_SFENCE_VM
) & MASK_SFENCE_VM
) == 0);
2501 static symbolS
*deferred_sym_rootP
;
2502 static symbolS
*deferred_sym_lastP
;
2503 /* Since symbols can't easily be freed, try to recycle ones which weren't
2505 static symbolS
*orphan_sym_rootP
;
2506 static symbolS
*orphan_sym_lastP
;
2508 /* This routine assembles an instruction into its binary format. As a
2509 side effect, it sets the global variable imm_reloc to the type of
2510 relocation to do if one of the operands is an address expression. */
2512 static struct riscv_ip_error
2513 riscv_ip (char *str
, struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2514 bfd_reloc_code_real_type
*imm_reloc
, htab_t hash
)
2516 /* The operand string defined in the riscv_opcodes. */
2517 const char *oparg
, *opargStart
;
2518 /* The parsed operands from assembly. */
2519 char *asarg
, *asargStart
;
2521 struct riscv_opcode
*insn
;
2523 const struct percent_op_match
*p
;
2524 struct riscv_ip_error error
;
2525 error
.msg
= "unrecognized opcode";
2526 error
.statement
= str
;
2527 error
.missing_ext
= NULL
;
2528 /* Indicate we are assembling instruction with CSR. */
2529 bool insn_with_csr
= false;
2531 /* Parse the name of the instruction. Terminate the string if whitespace
2532 is found so that str_hash_find only sees the name part of the string. */
2533 for (asarg
= str
; *asarg
!= '\0'; ++asarg
)
2534 if (ISSPACE (*asarg
))
2541 insn
= (struct riscv_opcode
*) str_hash_find (hash
, str
);
2543 probing_insn_operands
= true;
2546 for ( ; insn
&& insn
->name
&& strcmp (insn
->name
, str
) == 0; insn
++)
2548 if ((insn
->xlen_requirement
!= 0) && (xlen
!= insn
->xlen_requirement
))
2551 if (!riscv_multi_subset_supports (&riscv_rps_as
, insn
->insn_class
))
2553 error
.missing_ext
= riscv_multi_subset_supports_ext (&riscv_rps_as
,
2558 /* Reset error message of the previous round. */
2559 error
.msg
= _("illegal operands");
2560 error
.missing_ext
= NULL
;
2562 /* Purge deferred symbols from the previous round, if any. */
2563 while (deferred_sym_rootP
)
2565 symbolS
*sym
= deferred_sym_rootP
;
2567 symbol_remove (sym
, &deferred_sym_rootP
, &deferred_sym_lastP
);
2568 symbol_append (sym
, orphan_sym_lastP
, &orphan_sym_rootP
,
2572 create_insn (ip
, insn
);
2574 imm_expr
->X_op
= O_absent
;
2575 *imm_reloc
= BFD_RELOC_UNUSED
;
2576 p
= percent_op_null
;
2578 for (oparg
= insn
->args
;; ++oparg
)
2581 asarg
+= strspn (asarg
, " \t");
2584 case '\0': /* End of args. */
2585 if (insn
->pinfo
!= INSN_MACRO
)
2587 if (!insn
->match_func (insn
, ip
->insn_opcode
))
2590 /* For .insn, insn->match and insn->mask are 0. */
2591 if (riscv_insn_length ((insn
->match
== 0 && insn
->mask
== 0)
2597 if (riscv_is_priv_insn (ip
->insn_opcode
))
2598 explicit_priv_attr
= true;
2600 /* Check if we write a read-only CSR by the CSR
2603 && riscv_opts
.csr_check
2604 && !riscv_csr_read_only_check (ip
->insn_opcode
))
2606 /* Restore the character in advance, since we want to
2607 report the detailed warning message here. */
2609 *(asargStart
- 1) = save_c
;
2610 as_warn (_("read-only CSR is written `%s'"), str
);
2611 insn_with_csr
= false;
2614 /* The (segmant) load and store with EEW 64 cannot be used
2615 when zve32x is enabled. */
2616 if (ip
->insn_mo
->pinfo
& INSN_V_EEW64
2617 && riscv_subset_supports (&riscv_rps_as
, "zve32x")
2618 && !riscv_subset_supports (&riscv_rps_as
, "zve64x"))
2620 error
.msg
= _("illegal opcode for zve32x");
2627 /* Successful assembly. */
2629 insn_with_csr
= false;
2631 /* Commit deferred symbols, if any. */
2632 while (deferred_sym_rootP
)
2634 symbolS
*sym
= deferred_sym_rootP
;
2636 symbol_remove (sym
, &deferred_sym_rootP
,
2637 &deferred_sym_lastP
);
2638 symbol_append (sym
, symbol_lastP
, &symbol_rootP
,
2640 symbol_table_insert (sym
);
2647 case 's': /* RS1 x8-x15. */
2648 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2649 || !(regno
>= 8 && regno
<= 15))
2651 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2653 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2654 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2655 || EXTRACT_OPERAND (CRS1S
, ip
->insn_opcode
) + 8 != regno
)
2658 case 't': /* RS2 x8-x15. */
2659 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2660 || !(regno
>= 8 && regno
<= 15))
2662 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2664 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2665 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2666 || EXTRACT_OPERAND (CRS2S
, ip
->insn_opcode
) + 8 != regno
)
2669 case 'U': /* RS1, constrained to equal RD. */
2670 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2671 || EXTRACT_OPERAND (RD
, ip
->insn_opcode
) != regno
)
2675 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
2677 INSERT_OPERAND (CRS2
, *ip
, regno
);
2679 case 'c': /* RS1, constrained to equal sp. */
2680 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2684 case 'z': /* RS2, constrained to equal x0. */
2685 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2689 case '>': /* Shift amount, 0 - (XLEN-1). */
2690 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2691 || imm_expr
->X_op
!= O_constant
2692 || (unsigned long) imm_expr
->X_add_number
>= xlen
)
2694 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2696 asarg
= expr_parse_end
;
2697 imm_expr
->X_op
= O_absent
;
2700 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2701 || imm_expr
->X_op
!= O_constant
2702 || imm_expr
->X_add_number
< 0
2703 || imm_expr
->X_add_number
>= 32
2704 || !VALID_CLTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2706 ip
->insn_opcode
|= ENCODE_CLTYPE_IMM (imm_expr
->X_add_number
);
2709 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2710 || imm_expr
->X_op
!= O_constant
2711 || imm_expr
->X_add_number
< 0
2712 || imm_expr
->X_add_number
>= 64
2713 || !VALID_CSSTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2715 ip
->insn_opcode
|= ENCODE_CSSTYPE_IMM (imm_expr
->X_add_number
);
2718 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2719 || imm_expr
->X_op
!= O_constant
2720 || imm_expr
->X_add_number
< 0
2721 || imm_expr
->X_add_number
>= 256
2722 || !VALID_CIWTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2724 ip
->insn_opcode
|= ENCODE_CIWTYPE_IMM (imm_expr
->X_add_number
);
2727 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2728 || imm_expr
->X_op
!= O_constant
2729 || imm_expr
->X_add_number
== 0
2730 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2732 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2735 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2737 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2738 || imm_expr
->X_op
!= O_constant
2739 || !VALID_CLTYPE_LW_IMM ((valueT
) imm_expr
->X_add_number
))
2741 ip
->insn_opcode
|= ENCODE_CLTYPE_LW_IMM (imm_expr
->X_add_number
);
2744 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2746 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2747 || imm_expr
->X_op
!= O_constant
2748 || !VALID_CLTYPE_LD_IMM ((valueT
) imm_expr
->X_add_number
))
2750 ip
->insn_opcode
|= ENCODE_CLTYPE_LD_IMM (imm_expr
->X_add_number
);
2753 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2755 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2756 || imm_expr
->X_op
!= O_constant
2757 || !VALID_CITYPE_LWSP_IMM ((valueT
) imm_expr
->X_add_number
))
2760 ENCODE_CITYPE_LWSP_IMM (imm_expr
->X_add_number
);
2763 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2765 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2766 || imm_expr
->X_op
!= O_constant
2767 || !VALID_CITYPE_LDSP_IMM ((valueT
) imm_expr
->X_add_number
))
2770 ENCODE_CITYPE_LDSP_IMM (imm_expr
->X_add_number
);
2773 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2774 || imm_expr
->X_op
!= O_constant
2775 /* C.addiw, c.li, and c.andi allow zero immediate.
2776 C.addi allows zero immediate as hint. Otherwise this
2778 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2780 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2783 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2784 || imm_expr
->X_op
!= O_constant
2785 || imm_expr
->X_add_number
== 0
2786 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT
) imm_expr
->X_add_number
))
2789 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr
->X_add_number
);
2792 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2793 || imm_expr
->X_op
!= O_constant
2794 || !VALID_CITYPE_ADDI16SP_IMM ((valueT
) imm_expr
->X_add_number
))
2797 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr
->X_add_number
);
2800 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2802 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2803 || imm_expr
->X_op
!= O_constant
2804 || !VALID_CSSTYPE_SWSP_IMM ((valueT
) imm_expr
->X_add_number
))
2807 ENCODE_CSSTYPE_SWSP_IMM (imm_expr
->X_add_number
);
2810 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2812 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2813 || imm_expr
->X_op
!= O_constant
2814 || !VALID_CSSTYPE_SDSP_IMM ((valueT
) imm_expr
->X_add_number
))
2817 ENCODE_CSSTYPE_SDSP_IMM (imm_expr
->X_add_number
);
2820 p
= percent_op_utype
;
2821 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
2824 if (imm_expr
->X_op
!= O_constant
2825 || imm_expr
->X_add_number
<= 0
2826 || imm_expr
->X_add_number
>= RISCV_BIGIMM_REACH
2827 || (imm_expr
->X_add_number
>= RISCV_RVC_IMM_REACH
/ 2
2828 && (imm_expr
->X_add_number
<
2829 RISCV_BIGIMM_REACH
- RISCV_RVC_IMM_REACH
/ 2)))
2831 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2834 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2835 || (imm_expr
->X_add_number
& (RISCV_IMM_REACH
- 1))
2836 || ((int32_t)imm_expr
->X_add_number
2837 != imm_expr
->X_add_number
))
2839 imm_expr
->X_add_number
=
2840 ((uint32_t) imm_expr
->X_add_number
) >> RISCV_IMM_BITS
;
2846 case 'S': /* Floating-point RS1 x8-x15. */
2847 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
2848 || !(regno
>= 8 && regno
<= 15))
2850 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2852 case 'D': /* Floating-point RS2 x8-x15. */
2853 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
2854 || !(regno
>= 8 && regno
<= 15))
2856 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2858 case 'T': /* Floating-point RS2. */
2859 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
))
2861 INSERT_OPERAND (CRS2
, *ip
, regno
);
2867 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2868 || imm_expr
->X_op
!= O_constant
2869 || imm_expr
->X_add_number
< 0
2870 || imm_expr
->X_add_number
>= 64)
2872 as_bad (_("bad value for compressed funct6 "
2873 "field, value must be 0...63"));
2876 INSERT_OPERAND (CFUNCT6
, *ip
, imm_expr
->X_add_number
);
2877 imm_expr
->X_op
= O_absent
;
2878 asarg
= expr_parse_end
;
2882 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2883 || imm_expr
->X_op
!= O_constant
2884 || imm_expr
->X_add_number
< 0
2885 || imm_expr
->X_add_number
>= 16)
2887 as_bad (_("bad value for compressed funct4 "
2888 "field, value must be 0...15"));
2891 INSERT_OPERAND (CFUNCT4
, *ip
, imm_expr
->X_add_number
);
2892 imm_expr
->X_op
= O_absent
;
2893 asarg
= expr_parse_end
;
2897 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2898 || imm_expr
->X_op
!= O_constant
2899 || imm_expr
->X_add_number
< 0
2900 || imm_expr
->X_add_number
>= 8)
2902 as_bad (_("bad value for compressed funct3 "
2903 "field, value must be 0...7"));
2906 INSERT_OPERAND (CFUNCT3
, *ip
, imm_expr
->X_add_number
);
2907 imm_expr
->X_op
= O_absent
;
2908 asarg
= expr_parse_end
;
2912 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2913 || imm_expr
->X_op
!= O_constant
2914 || imm_expr
->X_add_number
< 0
2915 || imm_expr
->X_add_number
>= 4)
2917 as_bad (_("bad value for compressed funct2 "
2918 "field, value must be 0...3"));
2921 INSERT_OPERAND (CFUNCT2
, *ip
, imm_expr
->X_add_number
);
2922 imm_expr
->X_op
= O_absent
;
2923 asarg
= expr_parse_end
;
2927 goto unknown_riscv_ip_operand
;
2932 goto unknown_riscv_ip_operand
;
2934 break; /* end RVC */
2940 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2942 INSERT_OPERAND (VD
, *ip
, regno
);
2945 case 'e': /* AMO VD */
2946 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
) && regno
== 0)
2947 INSERT_OPERAND (VWD
, *ip
, 0);
2948 else if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2950 INSERT_OPERAND (VWD
, *ip
, 1);
2951 INSERT_OPERAND (VD
, *ip
, regno
);
2957 case 'f': /* AMO VS3 */
2958 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2960 if (!EXTRACT_OPERAND (VWD
, ip
->insn_opcode
))
2961 INSERT_OPERAND (VD
, *ip
, regno
);
2964 /* VS3 must match VD. */
2965 if (EXTRACT_OPERAND (VD
, ip
->insn_opcode
) != regno
)
2971 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2973 INSERT_OPERAND (VS1
, *ip
, regno
);
2977 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2979 INSERT_OPERAND (VS2
, *ip
, regno
);
2982 case 'u': /* VS1 == VS2 */
2983 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2985 INSERT_OPERAND (VS1
, *ip
, regno
);
2986 INSERT_OPERAND (VS2
, *ip
, regno
);
2989 case 'v': /* VD == VS1 == VS2 */
2990 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2992 INSERT_OPERAND (VD
, *ip
, regno
);
2993 INSERT_OPERAND (VS1
, *ip
, regno
);
2994 INSERT_OPERAND (VS2
, *ip
, regno
);
2997 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
2998 and is used to choose vs1/rs1/frs1/imm or vs2 for
2999 v[f]merge. It use the same encoding as the vector mask
3002 if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
) && regno
== 0)
3006 case 'b': /* vtypei for vsetivli */
3007 my_getVsetvliExpression (imm_expr
, asarg
);
3008 check_absolute_expr (ip
, imm_expr
, FALSE
);
3009 if (!VALID_RVV_VB_IMM (imm_expr
->X_add_number
))
3010 as_bad (_("bad value for vsetivli immediate field, "
3011 "value must be 0..1023"));
3013 |= ENCODE_RVV_VB_IMM (imm_expr
->X_add_number
);
3014 imm_expr
->X_op
= O_absent
;
3015 asarg
= expr_parse_end
;
3018 case 'c': /* vtypei for vsetvli */
3019 my_getVsetvliExpression (imm_expr
, asarg
);
3020 check_absolute_expr (ip
, imm_expr
, FALSE
);
3021 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
3022 as_bad (_("bad value for vsetvli immediate field, "
3023 "value must be 0..2047"));
3025 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
3026 imm_expr
->X_op
= O_absent
;
3027 asarg
= expr_parse_end
;
3030 case 'i': /* vector arith signed immediate */
3031 my_getExpression (imm_expr
, asarg
);
3032 check_absolute_expr (ip
, imm_expr
, FALSE
);
3033 if (imm_expr
->X_add_number
> 15
3034 || imm_expr
->X_add_number
< -16)
3035 as_bad (_("bad value for vector immediate field, "
3036 "value must be -16...15"));
3037 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3038 imm_expr
->X_op
= O_absent
;
3039 asarg
= expr_parse_end
;
3042 case 'j': /* vector arith unsigned immediate */
3043 my_getExpression (imm_expr
, asarg
);
3044 check_absolute_expr (ip
, imm_expr
, FALSE
);
3045 if (imm_expr
->X_add_number
< 0
3046 || imm_expr
->X_add_number
>= 32)
3047 as_bad (_("bad value for vector immediate field, "
3048 "value must be 0...31"));
3049 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3050 imm_expr
->X_op
= O_absent
;
3051 asarg
= expr_parse_end
;
3054 case 'k': /* vector arith signed immediate, minus 1 */
3055 my_getExpression (imm_expr
, asarg
);
3056 check_absolute_expr (ip
, imm_expr
, FALSE
);
3057 if (imm_expr
->X_add_number
> 16
3058 || imm_expr
->X_add_number
< -15)
3059 as_bad (_("bad value for vector immediate field, "
3060 "value must be -15...16"));
3061 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
- 1);
3062 imm_expr
->X_op
= O_absent
;
3063 asarg
= expr_parse_end
;
3066 case 'l': /* 6-bit vector arith unsigned immediate */
3067 my_getExpression (imm_expr
, asarg
);
3068 check_absolute_expr (ip
, imm_expr
, FALSE
);
3069 if (imm_expr
->X_add_number
< 0
3070 || imm_expr
->X_add_number
>= 64)
3071 as_bad (_("bad value for vector immediate field, "
3072 "value must be 0...63"));
3073 ip
->insn_opcode
|= ENCODE_RVV_VI_UIMM6 (imm_expr
->X_add_number
);
3074 imm_expr
->X_op
= O_absent
;
3075 asarg
= expr_parse_end
;
3078 case 'm': /* optional vector mask */
3081 INSERT_OPERAND (VMASK
, *ip
, 1);
3084 else if (*asarg
== ',' && asarg
++
3085 && reg_lookup (&asarg
, RCLASS_VECM
, ®no
)
3088 INSERT_OPERAND (VMASK
, *ip
, 0);
3093 case 'M': /* required vector mask */
3094 if (reg_lookup (&asarg
, RCLASS_VECM
, ®no
) && regno
== 0)
3096 INSERT_OPERAND (VMASK
, *ip
, 0);
3101 case 'T': /* vector macro temporary register */
3102 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
) || regno
== 0)
3104 /* Store it in the FUNCT6 field as we don't have anyplace
3105 else to store it. */
3106 INSERT_OPERAND (VFUNCT6
, *ip
, regno
);
3110 goto unknown_riscv_ip_operand
;
3112 break; /* end RVV */
3115 if (*asarg
++ == *oparg
)
3124 if (*asarg
++ == *oparg
)
3128 case '<': /* Shift amount, 0 - 31. */
3129 my_getExpression (imm_expr
, asarg
);
3130 check_absolute_expr (ip
, imm_expr
, false);
3131 if ((unsigned long) imm_expr
->X_add_number
> 31)
3132 as_bad (_("improper shift amount (%"PRIu64
")"),
3133 imm_expr
->X_add_number
);
3134 INSERT_OPERAND (SHAMTW
, *ip
, imm_expr
->X_add_number
);
3135 imm_expr
->X_op
= O_absent
;
3136 asarg
= expr_parse_end
;
3139 case '>': /* Shift amount, 0 - (XLEN-1). */
3140 my_getExpression (imm_expr
, asarg
);
3141 check_absolute_expr (ip
, imm_expr
, false);
3142 if ((unsigned long) imm_expr
->X_add_number
>= xlen
)
3143 as_bad (_("improper shift amount (%"PRIu64
")"),
3144 imm_expr
->X_add_number
);
3145 INSERT_OPERAND (SHAMT
, *ip
, imm_expr
->X_add_number
);
3146 imm_expr
->X_op
= O_absent
;
3147 asarg
= expr_parse_end
;
3150 case 'Z': /* CSRRxI immediate. */
3151 my_getExpression (imm_expr
, asarg
);
3152 check_absolute_expr (ip
, imm_expr
, false);
3153 if ((unsigned long) imm_expr
->X_add_number
> 31)
3154 as_bad (_("improper CSRxI immediate (%"PRIu64
")"),
3155 imm_expr
->X_add_number
);
3156 INSERT_OPERAND (RS1
, *ip
, imm_expr
->X_add_number
);
3157 imm_expr
->X_op
= O_absent
;
3158 asarg
= expr_parse_end
;
3161 case 'E': /* Control register. */
3162 insn_with_csr
= true;
3163 explicit_priv_attr
= true;
3164 if (reg_lookup (&asarg
, RCLASS_CSR
, ®no
))
3165 INSERT_OPERAND (CSR
, *ip
, regno
);
3168 my_getExpression (imm_expr
, asarg
);
3169 check_absolute_expr (ip
, imm_expr
, true);
3170 if ((unsigned long) imm_expr
->X_add_number
> 0xfff)
3171 as_bad (_("improper CSR address (%"PRIu64
")"),
3172 imm_expr
->X_add_number
);
3173 INSERT_OPERAND (CSR
, *ip
, imm_expr
->X_add_number
);
3174 imm_expr
->X_op
= O_absent
;
3175 asarg
= expr_parse_end
;
3179 case 'm': /* Rounding mode. */
3180 if (arg_lookup (&asarg
, riscv_rm
,
3181 ARRAY_SIZE (riscv_rm
), ®no
))
3183 INSERT_OPERAND (RM
, *ip
, regno
);
3189 case 'Q': /* Fence predecessor/successor. */
3190 if (arg_lookup (&asarg
, riscv_pred_succ
,
3191 ARRAY_SIZE (riscv_pred_succ
), ®no
))
3194 INSERT_OPERAND (PRED
, *ip
, regno
);
3196 INSERT_OPERAND (SUCC
, *ip
, regno
);
3201 case 'd': /* Destination register. */
3202 case 's': /* Source register. */
3203 case 't': /* Target register. */
3205 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
3211 /* Now that we have assembled one operand, we use the args
3212 string to figure out where it goes in the instruction. */
3216 INSERT_OPERAND (RS1
, *ip
, regno
);
3219 INSERT_OPERAND (RD
, *ip
, regno
);
3222 INSERT_OPERAND (RS2
, *ip
, regno
);
3225 INSERT_OPERAND (RS3
, *ip
, regno
);
3232 case 'D': /* Floating point RD. */
3233 case 'S': /* Floating point RS1. */
3234 case 'T': /* Floating point RS2. */
3235 case 'U': /* Floating point RS1 and RS2. */
3236 case 'R': /* Floating point RS3. */
3237 if (reg_lookup (&asarg
,
3238 (riscv_subset_supports (&riscv_rps_as
, "zfinx")
3239 ? RCLASS_GPR
: RCLASS_FPR
), ®no
))
3247 INSERT_OPERAND (RD
, *ip
, regno
);
3250 INSERT_OPERAND (RS1
, *ip
, regno
);
3253 INSERT_OPERAND (RS1
, *ip
, regno
);
3256 INSERT_OPERAND (RS2
, *ip
, regno
);
3259 INSERT_OPERAND (RS3
, *ip
, regno
);
3267 my_getExpression (imm_expr
, asarg
);
3268 if (imm_expr
->X_op
!= O_big
3269 && imm_expr
->X_op
!= O_constant
)
3271 normalize_constant_expr (imm_expr
);
3272 asarg
= expr_parse_end
;
3276 my_getExpression (imm_expr
, asarg
);
3277 normalize_constant_expr (imm_expr
);
3278 /* The 'A' format specifier must be a symbol. */
3279 if (imm_expr
->X_op
!= O_symbol
)
3281 *imm_reloc
= BFD_RELOC_32
;
3282 asarg
= expr_parse_end
;
3286 my_getExpression (imm_expr
, asarg
);
3287 normalize_constant_expr (imm_expr
);
3288 /* The 'B' format specifier must be a symbol or a constant. */
3289 if (imm_expr
->X_op
!= O_symbol
&& imm_expr
->X_op
!= O_constant
)
3291 if (imm_expr
->X_op
== O_symbol
)
3292 *imm_reloc
= BFD_RELOC_32
;
3293 asarg
= expr_parse_end
;
3296 case 'j': /* Sign-extended immediate. */
3297 p
= percent_op_itype
;
3298 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3300 case 'q': /* Store displacement. */
3301 p
= percent_op_stype
;
3302 *imm_reloc
= BFD_RELOC_RISCV_LO12_S
;
3304 case 'o': /* Load displacement. */
3305 p
= percent_op_itype
;
3306 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3309 /* This is used for TLS, where the fourth operand is
3310 %tprel_add, to get a relocation applied to an add
3311 instruction, for relaxation to use. */
3312 p
= percent_op_rtype
;
3314 case '0': /* AMO displacement, which must be zero. */
3316 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3319 /* If this value won't fit into a 16 bit offset, then go
3320 find a macro that will generate the 32 bit offset
3322 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3324 normalize_constant_expr (imm_expr
);
3325 if (imm_expr
->X_op
!= O_constant
3326 || (*oparg
== '0' && imm_expr
->X_add_number
!= 0)
3328 || imm_expr
->X_add_number
>= (signed)RISCV_IMM_REACH
/2
3329 || imm_expr
->X_add_number
< -(signed)RISCV_IMM_REACH
/2)
3332 asarg
= expr_parse_end
;
3335 case 'p': /* PC-relative offset. */
3337 *imm_reloc
= BFD_RELOC_12_PCREL
;
3338 my_getExpression (imm_expr
, asarg
);
3339 asarg
= expr_parse_end
;
3342 case 'u': /* Upper 20 bits. */
3343 p
= percent_op_utype
;
3344 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3346 if (imm_expr
->X_op
!= O_constant
)
3349 if (imm_expr
->X_add_number
< 0
3350 || imm_expr
->X_add_number
>= (signed)RISCV_BIGIMM_REACH
)
3351 as_bad (_("lui expression not in range 0..1048575"));
3353 *imm_reloc
= BFD_RELOC_RISCV_HI20
;
3354 imm_expr
->X_add_number
<<= RISCV_IMM_BITS
;
3356 asarg
= expr_parse_end
;
3359 case 'a': /* 20-bit PC-relative offset. */
3361 my_getExpression (imm_expr
, asarg
);
3362 asarg
= expr_parse_end
;
3363 *imm_reloc
= BFD_RELOC_RISCV_JMP
;
3367 my_getExpression (imm_expr
, asarg
);
3368 asarg
= expr_parse_end
;
3369 if (strcmp (asarg
, "@plt") == 0)
3371 *imm_reloc
= BFD_RELOC_RISCV_CALL_PLT
;
3378 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3379 || imm_expr
->X_op
!= O_constant
3380 || imm_expr
->X_add_number
< 0
3381 || imm_expr
->X_add_number
>= 128
3382 || (imm_expr
->X_add_number
& 0x3) != 3)
3384 as_bad (_("bad value for opcode field, "
3385 "value must be 0...127 and "
3386 "lower 2 bits must be 0x3"));
3389 INSERT_OPERAND (OP
, *ip
, imm_expr
->X_add_number
);
3390 imm_expr
->X_op
= O_absent
;
3391 asarg
= expr_parse_end
;
3395 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3396 || imm_expr
->X_op
!= O_constant
3397 || imm_expr
->X_add_number
< 0
3398 || imm_expr
->X_add_number
>= 3)
3400 as_bad (_("bad value for opcode field, "
3401 "value must be 0...2"));
3404 INSERT_OPERAND (OP2
, *ip
, imm_expr
->X_add_number
);
3405 imm_expr
->X_op
= O_absent
;
3406 asarg
= expr_parse_end
;
3410 goto unknown_riscv_ip_operand
;
3418 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3419 || imm_expr
->X_op
!= O_constant
3420 || imm_expr
->X_add_number
< 0
3421 || imm_expr
->X_add_number
>= 128)
3423 as_bad (_("bad value for funct7 field, "
3424 "value must be 0...127"));
3427 INSERT_OPERAND (FUNCT7
, *ip
, imm_expr
->X_add_number
);
3428 imm_expr
->X_op
= O_absent
;
3429 asarg
= expr_parse_end
;
3433 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3434 || imm_expr
->X_op
!= O_constant
3435 || imm_expr
->X_add_number
< 0
3436 || imm_expr
->X_add_number
>= 8)
3438 as_bad (_("bad value for funct3 field, "
3439 "value must be 0...7"));
3442 INSERT_OPERAND (FUNCT3
, *ip
, imm_expr
->X_add_number
);
3443 imm_expr
->X_op
= O_absent
;
3444 asarg
= expr_parse_end
;
3448 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3449 || imm_expr
->X_op
!= O_constant
3450 || imm_expr
->X_add_number
< 0
3451 || imm_expr
->X_add_number
>= 4)
3453 as_bad (_("bad value for funct2 field, "
3454 "value must be 0...3"));
3457 INSERT_OPERAND (FUNCT2
, *ip
, imm_expr
->X_add_number
);
3458 imm_expr
->X_op
= O_absent
;
3459 asarg
= expr_parse_end
;
3463 goto unknown_riscv_ip_operand
;
3467 case 'y': /* bs immediate */
3468 my_getExpression (imm_expr
, asarg
);
3469 check_absolute_expr (ip
, imm_expr
, FALSE
);
3470 if ((unsigned long)imm_expr
->X_add_number
> 3)
3471 as_bad(_("Improper bs immediate (%lu)"),
3472 (unsigned long)imm_expr
->X_add_number
);
3473 INSERT_OPERAND(BS
, *ip
, imm_expr
->X_add_number
);
3474 imm_expr
->X_op
= O_absent
;
3475 asarg
= expr_parse_end
;
3478 case 'Y': /* rnum immediate */
3479 my_getExpression (imm_expr
, asarg
);
3480 check_absolute_expr (ip
, imm_expr
, FALSE
);
3481 if ((unsigned long)imm_expr
->X_add_number
> 10)
3482 as_bad(_("Improper rnum immediate (%lu)"),
3483 (unsigned long)imm_expr
->X_add_number
);
3484 INSERT_OPERAND(RNUM
, *ip
, imm_expr
->X_add_number
);
3485 imm_expr
->X_op
= O_absent
;
3486 asarg
= expr_parse_end
;
3490 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3491 || imm_expr
->X_op
!= O_constant
3492 || imm_expr
->X_add_number
!= 0)
3494 asarg
= expr_parse_end
;
3495 imm_expr
->X_op
= O_absent
;
3498 case 'W': /* Various operands for standard z extensions. */
3505 /* Prefetch offset for 'Zicbop' extension.
3506 pseudo S-type but lower 5-bits zero. */
3507 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3509 my_getExpression (imm_expr
, asarg
);
3510 check_absolute_expr (ip
, imm_expr
, false);
3511 if (((unsigned) (imm_expr
->X_add_number
) & 0x1fU
)
3512 || imm_expr
->X_add_number
>= RISCV_IMM_REACH
/ 2
3513 || imm_expr
->X_add_number
< -RISCV_IMM_REACH
/ 2)
3514 as_bad (_ ("improper prefetch offset (%ld)"),
3515 (long) imm_expr
->X_add_number
);
3516 ip
->insn_opcode
|= ENCODE_STYPE_IMM (
3517 (unsigned) (imm_expr
->X_add_number
) & ~0x1fU
);
3518 imm_expr
->X_op
= O_absent
;
3519 asarg
= expr_parse_end
;
3522 goto unknown_riscv_ip_operand
;
3530 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3531 if (!arg_lookup (&asarg
, riscv_fli_symval
,
3532 ARRAY_SIZE (riscv_fli_symval
), ®no
))
3534 /* 0.0 is not a valid entry in riscv_fli_numval. */
3536 float f
= strtof (asarg
, &asarg
);
3537 if (errno
!= 0 || f
== 0.0
3538 || !flt_lookup (f
, riscv_fli_numval
,
3539 ARRAY_SIZE(riscv_fli_numval
),
3542 as_bad (_("bad fli constant operand, "
3543 "supported constants must be in "
3544 "decimal or hexadecimal floating-point "
3549 INSERT_OPERAND (RS1
, *ip
, regno
);
3552 goto unknown_riscv_ip_operand
;
3559 case 'h': /* Immediate field for c.lh/c.lhu/c.sh. */
3560 /* Handle cases, such as c.sh rs2', (rs1'). */
3561 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3563 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3564 || imm_expr
->X_op
!= O_constant
3565 || !VALID_ZCB_HALFWORD_UIMM ((valueT
) imm_expr
->X_add_number
))
3567 ip
->insn_opcode
|= ENCODE_ZCB_HALFWORD_UIMM (imm_expr
->X_add_number
);
3569 case 'b': /* Immediate field for c.lbu/c.sb. */
3570 /* Handle cases, such as c.lbu rd', (rs1'). */
3571 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3573 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3574 || imm_expr
->X_op
!= O_constant
3575 || !VALID_ZCB_BYTE_UIMM ((valueT
) imm_expr
->X_add_number
))
3577 ip
->insn_opcode
|= ENCODE_ZCB_BYTE_UIMM (imm_expr
->X_add_number
);
3579 case 'f': /* Operand for matching immediate 255. */
3580 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3581 || imm_expr
->X_op
!= O_constant
3582 || imm_expr
->X_add_number
!= 255)
3584 /* This operand is used for matching immediate 255, and
3585 we do not write anything to encoding by this operand. */
3586 asarg
= expr_parse_end
;
3587 imm_expr
->X_op
= O_absent
;
3590 goto unknown_riscv_ip_operand
;
3595 goto unknown_riscv_ip_operand
;
3599 case 'X': /* Vendor-specific operands. */
3602 case 't': /* Vendor-specific (T-head) operands. */
3609 case 'l': /* Integer immediate, literal. */
3610 n
= strcspn (++oparg
, ",");
3611 if (strncmp (oparg
, asarg
, n
))
3612 as_bad (_("unexpected literal (%s)"), asarg
);
3616 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
3619 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
3623 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
3625 goto unknown_riscv_ip_operand
;
3626 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
3629 my_getExpression (imm_expr
, asarg
);
3630 check_absolute_expr (ip
, imm_expr
, false);
3633 if (!VALIDATE_U_IMM (imm_expr
->X_add_number
, n
))
3634 as_bad (_("improper immediate value (%"PRIu64
")"),
3635 imm_expr
->X_add_number
);
3639 if (!VALIDATE_S_IMM (imm_expr
->X_add_number
, n
))
3640 as_bad (_("improper immediate value (%"PRIi64
")"),
3641 imm_expr
->X_add_number
);
3643 INSERT_IMM (n
, s
, *ip
, imm_expr
->X_add_number
);
3644 imm_expr
->X_op
= O_absent
;
3645 asarg
= expr_parse_end
;
3648 goto unknown_riscv_ip_operand
;
3654 goto unknown_riscv_ip_operand
;
3659 unknown_riscv_ip_operand
:
3660 as_fatal (_("internal: unknown argument type `%s'"),
3666 insn_with_csr
= false;
3670 /* Restore the character we might have clobbered above. */
3672 *(asargStart
- 1) = save_c
;
3674 probing_insn_operands
= false;
3679 /* Similar to riscv_ip, but assembles an instruction according to the
3680 hardcode values of .insn directive. */
3683 riscv_ip_hardcode (char *str
,
3684 struct riscv_cl_insn
*ip
,
3685 expressionS
*imm_expr
,
3688 struct riscv_opcode
*insn
;
3689 insn_t values
[2] = {0, 0};
3690 unsigned int num
= 0;
3692 input_line_pointer
= str
;
3695 expression (imm_expr
);
3696 switch (imm_expr
->X_op
)
3699 values
[num
++] = (insn_t
) imm_expr
->X_add_number
;
3702 /* Extract lower 32-bits of a big number.
3703 Assume that generic_bignum_to_int32 work on such number. */
3704 values
[num
++] = (insn_t
) generic_bignum_to_int32 ();
3707 /* The first value isn't constant, so it should be
3708 .insn <type> <operands>. We have been parsed it
3712 return _("values must be constant");
3715 while (*input_line_pointer
++ == ',' && num
< 2 && imm_expr
->X_op
!= O_big
);
3717 input_line_pointer
--;
3718 if (*input_line_pointer
!= '\0')
3719 return _("unrecognized values");
3721 insn
= XCNEW (struct riscv_opcode
);
3722 insn
->match
= values
[num
- 1];
3723 create_insn (ip
, insn
);
3724 unsigned int bytes
= riscv_insn_length (insn
->match
);
3726 if (num
== 2 && values
[0] != bytes
)
3727 return _("value conflicts with instruction length");
3729 if (imm_expr
->X_op
== O_big
)
3731 unsigned int llen
= 0;
3732 for (LITTLENUM_TYPE lval
= generic_bignum
[imm_expr
->X_add_number
- 1];
3734 lval
>>= BITS_PER_CHAR
;
3735 unsigned int repr_bytes
3736 = (imm_expr
->X_add_number
- 1) * CHARS_PER_LITTLENUM
+ llen
;
3737 if (bytes
< repr_bytes
)
3738 return _("value conflicts with instruction length");
3739 for (num
= 0; num
< imm_expr
->X_add_number
- 1; ++num
)
3740 number_to_chars_littleendian (
3741 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
3742 generic_bignum
[num
],
3743 CHARS_PER_LITTLENUM
);
3745 number_to_chars_littleendian (
3746 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
3747 generic_bignum
[num
],
3749 memset(ip
->insn_long_opcode
+ repr_bytes
, 0, bytes
- repr_bytes
);
3753 if (bytes
< sizeof(values
[0]) && values
[num
- 1] >> (8 * bytes
) != 0)
3754 return _("value conflicts with instruction length");
3760 md_assemble (char *str
)
3762 struct riscv_cl_insn insn
;
3763 expressionS imm_expr
;
3764 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
3766 /* The architecture and privileged elf attributes should be set
3767 before assembling. */
3768 if (!start_assemble
)
3770 start_assemble
= true;
3772 riscv_set_abi_by_arch ();
3773 if (!riscv_set_default_priv_spec (NULL
))
3777 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
3779 const struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
3780 &imm_reloc
, op_hash
);
3784 if (error
.missing_ext
)
3785 as_bad ("%s `%s', extension `%s' required", error
.msg
,
3786 error
.statement
, error
.missing_ext
);
3788 as_bad ("%s `%s'", error
.msg
, error
.statement
);
3792 if (insn
.insn_mo
->pinfo
== INSN_MACRO
)
3793 macro (&insn
, &imm_expr
, &imm_reloc
);
3795 append_insn (&insn
, &imm_expr
, imm_reloc
);
3799 md_atof (int type
, char *litP
, int *sizeP
)
3801 return ieee_md_atof (type
, litP
, sizeP
, target_big_endian
);
3805 md_number_to_chars (char *buf
, valueT val
, int n
)
3807 if (target_big_endian
)
3808 number_to_chars_bigendian (buf
, val
, n
);
3810 number_to_chars_littleendian (buf
, val
, n
);
3813 const char *md_shortopts
= "O::g::G:";
3817 OPTION_MARCH
= OPTION_MD_BASE
,
3824 OPTION_NO_ARCH_ATTR
,
3826 OPTION_NO_CSR_CHECK
,
3830 OPTION_LITTLE_ENDIAN
,
3834 struct option md_longopts
[] =
3836 {"march", required_argument
, NULL
, OPTION_MARCH
},
3837 {"fPIC", no_argument
, NULL
, OPTION_PIC
},
3838 {"fpic", no_argument
, NULL
, OPTION_PIC
},
3839 {"fno-pic", no_argument
, NULL
, OPTION_NO_PIC
},
3840 {"mabi", required_argument
, NULL
, OPTION_MABI
},
3841 {"mrelax", no_argument
, NULL
, OPTION_RELAX
},
3842 {"mno-relax", no_argument
, NULL
, OPTION_NO_RELAX
},
3843 {"march-attr", no_argument
, NULL
, OPTION_ARCH_ATTR
},
3844 {"mno-arch-attr", no_argument
, NULL
, OPTION_NO_ARCH_ATTR
},
3845 {"mcsr-check", no_argument
, NULL
, OPTION_CSR_CHECK
},
3846 {"mno-csr-check", no_argument
, NULL
, OPTION_NO_CSR_CHECK
},
3847 {"misa-spec", required_argument
, NULL
, OPTION_MISA_SPEC
},
3848 {"mpriv-spec", required_argument
, NULL
, OPTION_MPRIV_SPEC
},
3849 {"mbig-endian", no_argument
, NULL
, OPTION_BIG_ENDIAN
},
3850 {"mlittle-endian", no_argument
, NULL
, OPTION_LITTLE_ENDIAN
},
3852 {NULL
, no_argument
, NULL
, 0}
3854 size_t md_longopts_size
= sizeof (md_longopts
);
3857 md_parse_option (int c
, const char *arg
)
3862 default_arch_with_ext
= arg
;
3866 riscv_opts
.pic
= false;
3870 riscv_opts
.pic
= true;
3874 if (strcmp (arg
, "ilp32") == 0)
3875 riscv_set_abi (32, FLOAT_ABI_SOFT
, false);
3876 else if (strcmp (arg
, "ilp32e") == 0)
3877 riscv_set_abi (32, FLOAT_ABI_SOFT
, true);
3878 else if (strcmp (arg
, "ilp32f") == 0)
3879 riscv_set_abi (32, FLOAT_ABI_SINGLE
, false);
3880 else if (strcmp (arg
, "ilp32d") == 0)
3881 riscv_set_abi (32, FLOAT_ABI_DOUBLE
, false);
3882 else if (strcmp (arg
, "ilp32q") == 0)
3883 riscv_set_abi (32, FLOAT_ABI_QUAD
, false);
3884 else if (strcmp (arg
, "lp64") == 0)
3885 riscv_set_abi (64, FLOAT_ABI_SOFT
, false);
3886 else if (strcmp (arg
, "lp64f") == 0)
3887 riscv_set_abi (64, FLOAT_ABI_SINGLE
, false);
3888 else if (strcmp (arg
, "lp64d") == 0)
3889 riscv_set_abi (64, FLOAT_ABI_DOUBLE
, false);
3890 else if (strcmp (arg
, "lp64q") == 0)
3891 riscv_set_abi (64, FLOAT_ABI_QUAD
, false);
3894 explicit_mabi
= true;
3898 riscv_opts
.relax
= true;
3901 case OPTION_NO_RELAX
:
3902 riscv_opts
.relax
= false;
3905 case OPTION_ARCH_ATTR
:
3906 riscv_opts
.arch_attr
= true;
3909 case OPTION_NO_ARCH_ATTR
:
3910 riscv_opts
.arch_attr
= false;
3913 case OPTION_CSR_CHECK
:
3914 riscv_opts
.csr_check
= true;
3917 case OPTION_NO_CSR_CHECK
:
3918 riscv_opts
.csr_check
= false;
3921 case OPTION_MISA_SPEC
:
3922 return riscv_set_default_isa_spec (arg
);
3924 case OPTION_MPRIV_SPEC
:
3925 return riscv_set_default_priv_spec (arg
);
3927 case OPTION_BIG_ENDIAN
:
3928 target_big_endian
= 1;
3931 case OPTION_LITTLE_ENDIAN
:
3932 target_big_endian
= 0;
3943 riscv_after_parse_args (void)
3945 /* The --with-arch is optional for now, so we still need to set the xlen
3946 according to the default_arch, which is set by the --target. */
3949 if (strcmp (default_arch
, "riscv32") == 0)
3951 else if (strcmp (default_arch
, "riscv64") == 0)
3954 as_bad ("unknown default architecture `%s'", default_arch
);
3957 /* Set default specs. */
3958 if (default_isa_spec
== ISA_SPEC_CLASS_NONE
)
3959 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC
);
3960 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3961 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC
);
3963 riscv_set_arch (default_arch_with_ext
);
3965 /* If the CIE to be produced has not been overridden on the command line,
3966 then produce version 3 by default. This allows us to use the full
3967 range of registers in a .cfi_return_column directive. */
3968 if (flag_dwarf_cie_version
== -1)
3969 flag_dwarf_cie_version
= 3;
3972 bool riscv_parse_name (const char *name
, struct expressionS
*ep
,
3973 enum expr_mode mode
)
3978 if (!probing_insn_operands
)
3981 gas_assert (mode
== expr_normal
);
3983 regno
= reg_lookup_internal (name
, RCLASS_GPR
);
3984 if (regno
== (unsigned int)-1)
3987 if (symbol_find (name
) != NULL
)
3990 /* Create a symbol without adding it to the symbol table yet.
3991 Insertion will happen only once we commit to using the insn
3992 we're probing operands for. */
3993 for (sym
= deferred_sym_rootP
; sym
; sym
= symbol_next (sym
))
3994 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
3998 for (sym
= orphan_sym_rootP
; sym
; sym
= symbol_next (sym
))
3999 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
4001 symbol_remove (sym
, &orphan_sym_rootP
, &orphan_sym_lastP
);
4005 sym
= symbol_create (name
, undefined_section
,
4006 &zero_address_frag
, 0);
4008 symbol_append (sym
, deferred_sym_lastP
, &deferred_sym_rootP
,
4009 &deferred_sym_lastP
);
4012 ep
->X_op
= O_symbol
;
4013 ep
->X_add_symbol
= sym
;
4014 ep
->X_add_number
= 0;
4020 md_pcrel_from (fixS
*fixP
)
4022 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4025 /* Apply a fixup to the object file. */
4028 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
4030 unsigned int subtype
;
4031 bfd_byte
*buf
= (bfd_byte
*) (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
);
4032 bool relaxable
= false;
4036 /* Remember value for tc_gen_reloc. */
4037 fixP
->fx_addnumber
= *valP
;
4039 switch (fixP
->fx_r_type
)
4041 case BFD_RELOC_RISCV_HI20
:
4042 case BFD_RELOC_RISCV_LO12_I
:
4043 case BFD_RELOC_RISCV_LO12_S
:
4044 bfd_putl32 (riscv_apply_const_reloc (fixP
->fx_r_type
, *valP
)
4045 | bfd_getl32 (buf
), buf
);
4046 if (fixP
->fx_addsy
== NULL
)
4047 fixP
->fx_done
= true;
4051 case BFD_RELOC_RISCV_GOT_HI20
:
4052 case BFD_RELOC_RISCV_ADD8
:
4053 case BFD_RELOC_RISCV_ADD16
:
4054 case BFD_RELOC_RISCV_ADD32
:
4055 case BFD_RELOC_RISCV_ADD64
:
4056 case BFD_RELOC_RISCV_SUB6
:
4057 case BFD_RELOC_RISCV_SUB8
:
4058 case BFD_RELOC_RISCV_SUB16
:
4059 case BFD_RELOC_RISCV_SUB32
:
4060 case BFD_RELOC_RISCV_SUB64
:
4061 case BFD_RELOC_RISCV_RELAX
:
4062 /* cvt_frag_to_fill () has called output_leb128 (). */
4063 case BFD_RELOC_RISCV_SET_ULEB128
:
4064 case BFD_RELOC_RISCV_SUB_ULEB128
:
4067 case BFD_RELOC_RISCV_TPREL_HI20
:
4068 case BFD_RELOC_RISCV_TPREL_LO12_I
:
4069 case BFD_RELOC_RISCV_TPREL_LO12_S
:
4070 case BFD_RELOC_RISCV_TPREL_ADD
:
4074 case BFD_RELOC_RISCV_TLS_GOT_HI20
:
4075 case BFD_RELOC_RISCV_TLS_GD_HI20
:
4076 case BFD_RELOC_RISCV_TLS_DTPREL32
:
4077 case BFD_RELOC_RISCV_TLS_DTPREL64
:
4078 if (fixP
->fx_addsy
!= NULL
)
4079 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
4081 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
4082 _("TLS relocation against a constant"));
4086 /* Use pc-relative relocation for FDE initial location.
4087 The symbol address in .eh_frame may be adjusted in
4088 _bfd_elf_discard_section_eh_frame, and the content of
4089 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4090 Therefore, we cannot insert a relocation whose addend symbol is
4091 in .eh_frame. Othrewise, the value may be adjusted twice. */
4092 if (fixP
->fx_addsy
&& fixP
->fx_subsy
4093 && (sub_segment
= S_GET_SEGMENT (fixP
->fx_subsy
))
4094 && strcmp (sub_segment
->name
, ".eh_frame") == 0
4095 && S_GET_VALUE (fixP
->fx_subsy
)
4096 == fixP
->fx_frag
->fr_address
+ fixP
->fx_where
)
4098 fixP
->fx_r_type
= BFD_RELOC_RISCV_32_PCREL
;
4099 fixP
->fx_subsy
= NULL
;
4106 case BFD_RELOC_RISCV_CFA
:
4107 if (fixP
->fx_addsy
&& fixP
->fx_subsy
)
4109 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4110 fixP
->fx_next
->fx_addsy
= fixP
->fx_subsy
;
4111 fixP
->fx_next
->fx_subsy
= NULL
;
4112 fixP
->fx_next
->fx_offset
= 0;
4113 fixP
->fx_subsy
= NULL
;
4115 switch (fixP
->fx_r_type
)
4118 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD64
;
4119 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB64
;
4123 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD32
;
4124 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4128 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD16
;
4129 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4133 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD8
;
4134 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4137 case BFD_RELOC_RISCV_CFA
:
4138 /* Load the byte to get the subtype. */
4139 subtype
= bfd_get_8 (NULL
, &((fragS
*) (fixP
->fx_frag
->fr_opcode
))->fr_literal
[fixP
->fx_where
]);
4140 loc
= fixP
->fx_frag
->fr_fix
- (subtype
& 7);
4143 case DW_CFA_advance_loc1
:
4144 fixP
->fx_where
= loc
+ 1;
4145 fixP
->fx_next
->fx_where
= loc
+ 1;
4146 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET8
;
4147 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4150 case DW_CFA_advance_loc2
:
4152 fixP
->fx_next
->fx_size
= 2;
4153 fixP
->fx_where
= loc
+ 1;
4154 fixP
->fx_next
->fx_where
= loc
+ 1;
4155 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET16
;
4156 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4159 case DW_CFA_advance_loc4
:
4161 fixP
->fx_next
->fx_size
= 4;
4162 fixP
->fx_where
= loc
;
4163 fixP
->fx_next
->fx_where
= loc
;
4164 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET32
;
4165 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4169 if (subtype
< 0x80 && (subtype
& 0x40))
4171 /* DW_CFA_advance_loc */
4172 fixP
->fx_frag
= (fragS
*) fixP
->fx_frag
->fr_opcode
;
4173 fixP
->fx_next
->fx_frag
= fixP
->fx_frag
;
4174 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET6
;
4175 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB6
;
4178 as_fatal (_("internal: bad CFA value #%d"), subtype
);
4184 /* This case is unreachable. */
4191 /* If we are deleting this reloc entry, we must fill in the
4192 value now. This can happen if we have a .word which is not
4193 resolved when it appears but is later defined. */
4194 if (fixP
->fx_addsy
== NULL
)
4196 gas_assert (fixP
->fx_size
<= sizeof (valueT
));
4197 md_number_to_chars ((char *) buf
, *valP
, fixP
->fx_size
);
4202 case BFD_RELOC_RISCV_JMP
:
4205 /* Fill in a tentative value to improve objdump readability. */
4206 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4207 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4208 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_JTYPE_IMM (delta
), buf
);
4212 case BFD_RELOC_12_PCREL
:
4215 /* Fill in a tentative value to improve objdump readability. */
4216 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4217 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4218 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_BTYPE_IMM (delta
), buf
);
4222 case BFD_RELOC_RISCV_RVC_BRANCH
:
4225 /* Fill in a tentative value to improve objdump readability. */
4226 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4227 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4228 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CBTYPE_IMM (delta
), buf
);
4232 case BFD_RELOC_RISCV_RVC_JUMP
:
4235 /* Fill in a tentative value to improve objdump readability. */
4236 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4237 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4238 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CJTYPE_IMM (delta
), buf
);
4242 case BFD_RELOC_RISCV_CALL
:
4243 case BFD_RELOC_RISCV_CALL_PLT
:
4247 case BFD_RELOC_RISCV_PCREL_HI20
:
4248 case BFD_RELOC_RISCV_PCREL_LO12_S
:
4249 case BFD_RELOC_RISCV_PCREL_LO12_I
:
4250 relaxable
= riscv_opts
.relax
;
4253 case BFD_RELOC_RISCV_ALIGN
:
4257 /* We ignore generic BFD relocations we don't know about. */
4258 if (bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
) != NULL
)
4259 as_fatal (_("internal: bad relocation #%d"), fixP
->fx_r_type
);
4262 if (fixP
->fx_subsy
!= NULL
)
4263 as_bad_subtract (fixP
);
4265 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4266 if (relaxable
&& fixP
->fx_tcbit
&& fixP
->fx_addsy
!= NULL
)
4268 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4269 fixP
->fx_next
->fx_addsy
= fixP
->fx_next
->fx_subsy
= NULL
;
4270 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_RELAX
;
4271 fixP
->fx_next
->fx_size
= 0;
4275 /* Because the value of .cfi_remember_state may changed after relaxation,
4276 we insert a fix to relocate it again in link-time. */
4279 riscv_pre_output_hook (void)
4281 const frchainS
*frch
;
4284 /* Save the current segment info. */
4286 subsegT subseg
= now_subseg
;
4288 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
4289 for (frch
= seg_info (s
)->frchainP
; frch
; frch
= frch
->frch_next
)
4293 for (frag
= frch
->frch_root
; frag
; frag
= frag
->fr_next
)
4295 if (frag
->fr_type
== rs_cfa
)
4298 expressionS
*symval
;
4300 symval
= symbol_get_value_expression (frag
->fr_symbol
);
4301 exp
.X_op
= O_subtract
;
4302 exp
.X_add_symbol
= symval
->X_add_symbol
;
4303 exp
.X_add_number
= 0;
4304 exp
.X_op_symbol
= symval
->X_op_symbol
;
4306 /* We must set the segment before creating a frag after all
4307 frag chains have been chained together. */
4308 subseg_set (s
, frch
->frch_subseg
);
4310 fix_new_exp (frag
, (int) frag
->fr_offset
, 1, &exp
, 0,
4311 BFD_RELOC_RISCV_CFA
);
4316 /* Restore the original segment info. */
4317 subseg_set (seg
, subseg
);
4320 /* Handle the .option pseudo-op. */
4323 s_riscv_option (int x ATTRIBUTE_UNUSED
)
4325 char *name
= input_line_pointer
, ch
;
4327 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
4328 ++input_line_pointer
;
4329 ch
= *input_line_pointer
;
4330 *input_line_pointer
= '\0';
4332 if (strcmp (name
, "rvc") == 0)
4334 riscv_update_subset (&riscv_rps_as
, "+c");
4335 riscv_reset_subsets_list_arch_str ();
4336 riscv_set_rvc (true);
4338 else if (strcmp (name
, "norvc") == 0)
4340 riscv_update_subset (&riscv_rps_as
, "-c");
4341 riscv_reset_subsets_list_arch_str ();
4342 riscv_set_rvc (false);
4344 else if (strcmp (name
, "pic") == 0)
4345 riscv_opts
.pic
= true;
4346 else if (strcmp (name
, "nopic") == 0)
4347 riscv_opts
.pic
= false;
4348 else if (strcmp (name
, "relax") == 0)
4349 riscv_opts
.relax
= true;
4350 else if (strcmp (name
, "norelax") == 0)
4351 riscv_opts
.relax
= false;
4352 else if (strcmp (name
, "csr-check") == 0)
4353 riscv_opts
.csr_check
= true;
4354 else if (strcmp (name
, "no-csr-check") == 0)
4355 riscv_opts
.csr_check
= false;
4356 else if (strncmp (name
, "arch,", 5) == 0)
4359 if (ISSPACE (*name
) && *name
!= '\0')
4361 riscv_update_subset (&riscv_rps_as
, name
);
4362 riscv_reset_subsets_list_arch_str ();
4364 riscv_set_rvc (false);
4365 if (riscv_subset_supports (&riscv_rps_as
, "c")
4366 || riscv_subset_supports (&riscv_rps_as
, "zca"))
4367 riscv_set_rvc (true);
4369 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
4372 else if (strcmp (name
, "push") == 0)
4374 struct riscv_option_stack
*s
;
4376 s
= XNEW (struct riscv_option_stack
);
4377 s
->next
= riscv_opts_stack
;
4378 s
->options
= riscv_opts
;
4379 s
->subset_list
= riscv_rps_as
.subset_list
;
4380 riscv_opts_stack
= s
;
4381 riscv_rps_as
.subset_list
= riscv_copy_subset_list (s
->subset_list
);
4383 else if (strcmp (name
, "pop") == 0)
4385 struct riscv_option_stack
*s
;
4387 s
= riscv_opts_stack
;
4389 as_bad (_(".option pop with no .option push"));
4392 riscv_subset_list_t
*release_subsets
= riscv_rps_as
.subset_list
;
4393 riscv_opts_stack
= s
->next
;
4394 riscv_opts
= s
->options
;
4395 riscv_rps_as
.subset_list
= s
->subset_list
;
4396 riscv_release_subset_list (release_subsets
);
4402 as_warn (_("unrecognized .option directive: %s"), name
);
4404 *input_line_pointer
= ch
;
4405 demand_empty_rest_of_line ();
4408 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4409 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4410 use in DWARF debug information. */
4413 s_dtprel (int bytes
)
4420 if (ex
.X_op
!= O_symbol
)
4422 as_bad (_("unsupported use of %s"), (bytes
== 8
4425 ignore_rest_of_line ();
4428 p
= frag_more (bytes
);
4429 md_number_to_chars (p
, 0, bytes
);
4430 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, bytes
, &ex
, false,
4432 ? BFD_RELOC_RISCV_TLS_DTPREL64
4433 : BFD_RELOC_RISCV_TLS_DTPREL32
));
4435 demand_empty_rest_of_line ();
4438 /* Handle the .bss pseudo-op. */
4441 s_bss (int ignore ATTRIBUTE_UNUSED
)
4443 subseg_set (bss_section
, 0);
4444 demand_empty_rest_of_line ();
4448 riscv_make_nops (char *buf
, bfd_vma bytes
)
4452 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4453 means we are not within a valid instruction sequence. It is thus safe
4454 to use a zero byte, even though that is not a valid instruction. */
4458 /* Use at most one 2-byte NOP. */
4459 if ((bytes
- i
) % 4 == 2)
4461 number_to_chars_littleendian (buf
+ i
, RVC_NOP
, 2);
4465 /* Fill the remainder with 4-byte NOPs. */
4466 for ( ; i
< bytes
; i
+= 4)
4467 number_to_chars_littleendian (buf
+ i
, RISCV_NOP
, 4);
4470 /* Called from md_do_align. Used to create an alignment frag in a
4471 code section by emitting a worst-case NOP sequence that the linker
4472 will later relax to the correct number of NOPs. We can't compute
4473 the correct alignment now because of other linker relaxations. */
4476 riscv_frag_align_code (int n
)
4478 bfd_vma bytes
= (bfd_vma
) 1 << n
;
4479 bfd_vma insn_alignment
= riscv_opts
.rvc
? 2 : 4;
4480 bfd_vma worst_case_bytes
= bytes
- insn_alignment
;
4484 /* If we are moving to a smaller alignment than the instruction size, then no
4485 alignment is required. */
4486 if (bytes
<= insn_alignment
)
4489 /* When not relaxing, riscv_handle_align handles code alignment. */
4490 if (!riscv_opts
.relax
)
4493 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4494 rather than just use frag_more to handle an alignment here? So that we
4495 don't need to call riscv_mapping_state again later, and then only need
4496 to check frag->fr_type to see if it is frag_align_code. */
4497 nops
= frag_more (worst_case_bytes
);
4499 ex
.X_op
= O_constant
;
4500 ex
.X_add_number
= worst_case_bytes
;
4502 riscv_make_nops (nops
, worst_case_bytes
);
4504 fix_new_exp (frag_now
, nops
- frag_now
->fr_literal
, 0,
4505 &ex
, false, BFD_RELOC_RISCV_ALIGN
);
4507 riscv_mapping_state (MAP_INSN
, worst_case_bytes
, true/* fr_align_code */);
4509 /* We need to start a new frag after the alignment which may be removed by
4510 the linker, to prevent the assembler from computing static offsets.
4511 This is necessary to get correct EH info. */
4512 frag_wane (frag_now
);
4518 /* Implement HANDLE_ALIGN. */
4521 riscv_handle_align (fragS
*fragP
)
4523 switch (fragP
->fr_type
)
4526 /* When relaxing, riscv_frag_align_code handles code alignment. */
4527 if (!riscv_opts
.relax
)
4529 bfd_signed_vma bytes
= (fragP
->fr_next
->fr_address
4530 - fragP
->fr_address
- fragP
->fr_fix
);
4531 /* We have 4 byte uncompressed nops. */
4532 bfd_signed_vma size
= 4;
4533 bfd_signed_vma excess
= bytes
% size
;
4534 bfd_boolean odd_padding
= (excess
% 2 == 1);
4535 char *p
= fragP
->fr_literal
+ fragP
->fr_fix
;
4540 /* Insert zeros or compressed nops to get 4 byte alignment. */
4544 riscv_add_odd_padding_symbol (fragP
);
4545 riscv_make_nops (p
, excess
);
4546 fragP
->fr_fix
+= excess
;
4550 /* The frag will be changed to `rs_fill` later. The function
4551 `write_contents` will try to fill the remaining spaces
4552 according to the patterns we give. In this case, we give
4553 a 4 byte uncompressed nop as the pattern, and set the size
4554 of the pattern into `fr_var`. The nop will be output to the
4555 file `fr_offset` times. However, `fr_offset` could be zero
4556 if we don't need to pad the boundary finally. */
4557 riscv_make_nops (p
, size
);
4558 fragP
->fr_var
= size
;
4567 /* This usually called from frag_var. */
4570 riscv_init_frag (fragS
* fragP
, int max_chars
)
4572 /* Do not add mapping symbol to debug sections. */
4573 if (bfd_section_flags (now_seg
) & SEC_DEBUGGING
)
4576 switch (fragP
->fr_type
)
4581 riscv_mapping_state (MAP_DATA
, max_chars
, false/* fr_align_code */);
4584 riscv_mapping_state (MAP_INSN
, max_chars
, true/* fr_align_code */);
4592 md_estimate_size_before_relax (fragS
*fragp
, asection
*segtype
)
4594 return (fragp
->fr_var
= relaxed_branch_length (fragp
, segtype
, false));
4597 /* Translate internal representation of relocation info to BFD target
4601 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
4603 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
4605 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
4606 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
4607 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
4608 reloc
->addend
= fixp
->fx_addnumber
;
4610 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
4611 if (reloc
->howto
== NULL
)
4613 if ((fixp
->fx_r_type
== BFD_RELOC_16
|| fixp
->fx_r_type
== BFD_RELOC_8
)
4614 && fixp
->fx_addsy
!= NULL
&& fixp
->fx_subsy
!= NULL
)
4616 /* We don't have R_RISCV_8/16, but for this special case,
4617 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
4621 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
4622 _("cannot represent %s relocation in object file"),
4623 bfd_get_reloc_code_name (fixp
->fx_r_type
));
4631 riscv_relax_frag (asection
*sec
, fragS
*fragp
, long stretch ATTRIBUTE_UNUSED
)
4633 if (RELAX_BRANCH_P (fragp
->fr_subtype
))
4635 offsetT old_var
= fragp
->fr_var
;
4636 fragp
->fr_var
= relaxed_branch_length (fragp
, sec
, true);
4637 return fragp
->fr_var
- old_var
;
4643 /* Expand far branches to multi-instruction sequences. */
4646 md_convert_frag_branch (fragS
*fragp
)
4654 buf
= (bfd_byte
*)fragp
->fr_literal
+ fragp
->fr_fix
;
4656 exp
.X_op
= O_symbol
;
4657 exp
.X_add_symbol
= fragp
->fr_symbol
;
4658 exp
.X_add_number
= fragp
->fr_offset
;
4660 gas_assert (fragp
->fr_var
== RELAX_BRANCH_LENGTH (fragp
->fr_subtype
));
4662 if (RELAX_BRANCH_RVC (fragp
->fr_subtype
))
4664 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
4668 /* Expand the RVC branch into a RISC-V one. */
4669 insn
= bfd_getl16 (buf
);
4670 rs1
= 8 + ((insn
>> OP_SH_CRS1S
) & OP_MASK_CRS1S
);
4671 if ((insn
& MASK_C_J
) == MATCH_C_J
)
4673 else if ((insn
& MASK_C_JAL
) == MATCH_C_JAL
)
4674 insn
= MATCH_JAL
| (X_RA
<< OP_SH_RD
);
4675 else if ((insn
& MASK_C_BEQZ
) == MATCH_C_BEQZ
)
4676 insn
= MATCH_BEQ
| (rs1
<< OP_SH_RS1
);
4677 else if ((insn
& MASK_C_BNEZ
) == MATCH_C_BNEZ
)
4678 insn
= MATCH_BNE
| (rs1
<< OP_SH_RS1
);
4681 bfd_putl32 (insn
, buf
);
4685 /* Invert the branch condition. Branch over the jump. */
4686 insn
= bfd_getl16 (buf
);
4687 insn
^= MATCH_C_BEQZ
^ MATCH_C_BNEZ
;
4688 insn
|= ENCODE_CBTYPE_IMM (6);
4689 bfd_putl16 (insn
, buf
);
4694 /* Just keep the RVC branch. */
4695 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
4696 ? BFD_RELOC_RISCV_RVC_JUMP
: BFD_RELOC_RISCV_RVC_BRANCH
;
4697 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4698 2, &exp
, false, reloc
);
4707 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
4710 gas_assert (!RELAX_BRANCH_UNCOND (fragp
->fr_subtype
));
4712 /* Invert the branch condition. Branch over the jump. */
4713 insn
= bfd_getl32 (buf
);
4714 insn
^= MATCH_BEQ
^ MATCH_BNE
;
4715 insn
|= ENCODE_BTYPE_IMM (8);
4716 bfd_putl32 (insn
, buf
);
4720 /* Jump to the target. */
4721 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4722 4, &exp
, false, BFD_RELOC_RISCV_JMP
);
4723 bfd_putl32 (MATCH_JAL
, buf
);
4728 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
4729 ? BFD_RELOC_RISCV_JMP
: BFD_RELOC_12_PCREL
;
4730 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4731 4, &exp
, false, reloc
);
4740 fixp
->fx_file
= fragp
->fr_file
;
4741 fixp
->fx_line
= fragp
->fr_line
;
4743 gas_assert (buf
== (bfd_byte
*)fragp
->fr_literal
4744 + fragp
->fr_fix
+ fragp
->fr_var
);
4746 fragp
->fr_fix
+= fragp
->fr_var
;
4749 /* Relax a machine dependent frag. This returns the amount by which
4750 the current size of the frag should change. */
4753 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT asec ATTRIBUTE_UNUSED
,
4756 gas_assert (RELAX_BRANCH_P (fragp
->fr_subtype
));
4757 md_convert_frag_branch (fragp
);
4761 md_show_usage (FILE *stream
)
4763 fprintf (stream
, _("\
4765 -fpic or -fPIC generate position-independent code\n\
4766 -fno-pic don't generate position-independent code (default)\n\
4767 -march=ISA set the RISC-V architecture\n\
4768 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
4769 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9.1, 1.10, 1.11, 1.12)\n\
4770 -mabi=ABI set the RISC-V ABI\n\
4771 -mrelax enable relax (default)\n\
4772 -mno-relax disable relax\n\
4773 -march-attr generate RISC-V arch attribute\n\
4774 -mno-arch-attr don't generate RISC-V arch attribute\n\
4775 -mcsr-check enable the csr ISA and privilege spec version checks\n\
4776 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
4777 -mbig-endian assemble for big-endian\n\
4778 -mlittle-endian assemble for little-endian\n\
4782 /* Standard calling conventions leave the CFA at SP on entry. */
4785 riscv_cfi_frame_initial_instructions (void)
4787 cfi_add_CFA_def_cfa_register (X_SP
);
4791 tc_riscv_regname_to_dw2regnum (char *regname
)
4795 if ((reg
= reg_lookup_internal (regname
, RCLASS_GPR
)) >= 0)
4798 if ((reg
= reg_lookup_internal (regname
, RCLASS_FPR
)) >= 0)
4801 if ((reg
= reg_lookup_internal (regname
, RCLASS_VECR
)) >= 0)
4804 /* CSRs are numbered 4096 -> 8191. */
4805 if ((reg
= reg_lookup_internal (regname
, RCLASS_CSR
)) >= 0)
4808 as_bad (_("unknown register `%s'"), regname
);
4813 riscv_elf_final_processing (void)
4815 riscv_set_abi_by_arch ();
4816 riscv_release_subset_list (riscv_rps_as
.subset_list
);
4817 elf_elfheader (stdoutput
)->e_flags
|= elf_flags
;
4820 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
4821 since these directives break relaxation when used with symbol deltas. */
4824 s_riscv_leb128 (int sign
)
4827 char *save_in
= input_line_pointer
;
4830 if (sign
&& exp
.X_op
!= O_constant
)
4831 as_bad (_("non-constant .sleb128 is not supported"));
4832 else if (!sign
&& exp
.X_op
!= O_constant
&& exp
.X_op
!= O_subtract
)
4833 as_bad (_(".uleb128 only supports constant or subtract expressions"));
4835 demand_empty_rest_of_line ();
4837 input_line_pointer
= save_in
;
4838 return s_leb128 (sign
);
4841 /* Parse the .insn directive. There are three formats,
4842 Format 1: .insn <type> <operand1>, <operand2>, ...
4843 Format 2: .insn <length>, <value>
4844 Format 3: .insn <value>. */
4847 s_riscv_insn (int x ATTRIBUTE_UNUSED
)
4849 char *str
= input_line_pointer
;
4850 struct riscv_cl_insn insn
;
4851 expressionS imm_expr
;
4852 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
4855 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
4856 ++input_line_pointer
;
4858 save_c
= *input_line_pointer
;
4859 *input_line_pointer
= '\0';
4861 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
4863 struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
4864 &imm_reloc
, insn_type_hash
);
4867 char *save_in
= input_line_pointer
;
4868 error
.msg
= riscv_ip_hardcode (str
, &insn
, &imm_expr
, error
.msg
);
4869 input_line_pointer
= save_in
;
4874 if (error
.missing_ext
)
4875 as_bad ("%s `%s', extension `%s' required", error
.msg
, error
.statement
,
4878 as_bad ("%s `%s'", error
.msg
, error
.statement
);
4882 gas_assert (insn
.insn_mo
->pinfo
!= INSN_MACRO
);
4883 append_insn (&insn
, &imm_expr
, imm_reloc
);
4886 *input_line_pointer
= save_c
;
4887 demand_empty_rest_of_line ();
4890 /* Update architecture and privileged elf attributes. If we don't set
4891 them, then try to output the default ones. */
4894 riscv_write_out_attrs (void)
4896 const char *arch_str
, *priv_str
, *p
;
4897 /* versions[0]: major version.
4898 versions[1]: minor version.
4899 versions[2]: revision version. */
4900 unsigned versions
[3] = {0}, number
= 0;
4903 /* Re-write architecture elf attribute. */
4904 arch_str
= riscv_rps_as
.subset_list
->arch_str
;
4905 if (!bfd_elf_add_proc_attr_string (stdoutput
, Tag_RISCV_arch
, arch_str
))
4906 as_fatal (_("error adding attribute: %s"),
4907 bfd_errmsg (bfd_get_error ()));
4909 /* For the file without any instruction, we don't set the default_priv_spec
4910 according to the privileged elf attributes since the md_assemble isn't
4913 && !riscv_set_default_priv_spec (NULL
))
4916 /* If we already have set privileged elf attributes, then no need to do
4917 anything. Otherwise, don't generate or update them when no CSR and
4918 privileged instructions are used. */
4919 if (!explicit_priv_attr
)
4922 RISCV_GET_PRIV_SPEC_NAME (priv_str
, default_priv_spec
);
4924 for (i
= 0; *p
; ++p
)
4926 if (*p
== '.' && i
< 3)
4928 versions
[i
++] = number
;
4931 else if (ISDIGIT (*p
))
4932 number
= (number
* 10) + (*p
- '0');
4935 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str
);
4939 versions
[i
] = number
;
4941 /* Re-write privileged elf attributes. */
4942 if (!bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec
,
4944 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_minor
,
4946 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_revision
,
4948 as_fatal (_("error adding attribute: %s"),
4949 bfd_errmsg (bfd_get_error ()));
4952 /* Add the default contents for the .riscv.attributes section. */
4955 riscv_set_public_attributes (void)
4957 if (riscv_opts
.arch_attr
|| explicit_attr
)
4958 riscv_write_out_attrs ();
4961 /* Scan uleb128 subtraction expressions and insert fixups for them.
4962 e.g., .uleb128 .L1 - .L0
4963 Because relaxation may change the value of the subtraction, we
4964 must resolve them at link-time. */
4967 riscv_insert_uleb128_fixes (bfd
*abfd ATTRIBUTE_UNUSED
,
4968 asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
4970 segment_info_type
*seginfo
= seg_info (sec
);
4973 subseg_set (sec
, 0);
4975 for (fragP
= seginfo
->frchainP
->frch_root
;
4976 fragP
; fragP
= fragP
->fr_next
)
4978 expressionS
*exp
, *exp_dup
;
4980 if (fragP
->fr_type
!= rs_leb128
|| fragP
->fr_symbol
== NULL
)
4983 exp
= symbol_get_value_expression (fragP
->fr_symbol
);
4985 if (exp
->X_op
!= O_subtract
)
4988 /* Only unsigned leb128 can be handled. */
4989 gas_assert (fragP
->fr_subtype
== 0);
4990 exp_dup
= xmemdup (exp
, sizeof (*exp
), sizeof (*exp
));
4991 exp_dup
->X_op
= O_symbol
;
4992 exp_dup
->X_op_symbol
= NULL
;
4994 /* Insert relocations to resolve the subtraction at link-time.
4995 Emit the SET relocation first in riscv. */
4996 exp_dup
->X_add_symbol
= exp
->X_add_symbol
;
4997 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
4998 exp_dup
, 0, BFD_RELOC_RISCV_SET_ULEB128
);
4999 exp_dup
->X_add_symbol
= exp
->X_op_symbol
;
5000 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
5001 exp_dup
, 0, BFD_RELOC_RISCV_SUB_ULEB128
);
5002 free ((void *) exp_dup
);
5006 /* Called after all assembly has been done. */
5009 riscv_md_finish (void)
5011 riscv_set_public_attributes ();
5012 if (riscv_opts
.relax
)
5013 bfd_map_over_sections (stdoutput
, riscv_insert_uleb128_fixes
, NULL
);
5016 /* Adjust the symbol table. */
5019 riscv_adjust_symtab (void)
5021 bfd_map_over_sections (stdoutput
, riscv_check_mapping_symbols
, (char *) 0);
5022 elf_adjust_symtab ();
5025 /* Given a symbolic attribute NAME, return the proper integer value.
5026 Returns -1 if the attribute is not known. */
5029 riscv_convert_symbolic_attribute (const char *name
)
5038 /* When you modify this table you should
5039 also modify the list in doc/c-riscv.texi. */
5040 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
5044 T(priv_spec_revision
),
5045 T(unaligned_access
),
5054 for (i
= 0; i
< ARRAY_SIZE (attribute_table
); i
++)
5055 if (strcmp (name
, attribute_table
[i
].name
) == 0)
5056 return attribute_table
[i
].tag
;
5061 /* Parse a .attribute directive. */
5064 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED
)
5066 int tag
= obj_elf_vendor_attribute (OBJ_ATTR_PROC
);
5068 obj_attribute
*attr
;
5070 explicit_attr
= true;
5073 case Tag_RISCV_arch
:
5075 attr
= elf_known_obj_attributes_proc (stdoutput
);
5076 if (!start_assemble
)
5077 riscv_set_arch (attr
[Tag_RISCV_arch
].s
);
5079 as_fatal (_("architecture elf attributes must set before "
5080 "any instructions"));
5082 if (old_xlen
!= xlen
)
5084 /* We must re-init bfd again if xlen is changed. */
5085 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
5086 bfd_find_target (riscv_target_format (), stdoutput
);
5088 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
5089 as_warn (_("could not set architecture and machine"));
5093 case Tag_RISCV_priv_spec
:
5094 case Tag_RISCV_priv_spec_minor
:
5095 case Tag_RISCV_priv_spec_revision
:
5097 as_fatal (_("privileged elf attributes must set before "
5098 "any instructions"));
5106 /* Mark symbol that it follows a variant CC convention. */
5109 s_variant_cc (int ignored ATTRIBUTE_UNUSED
)
5115 elf_symbol_type
*elfsym
;
5117 c
= get_symbol_name (&name
);
5119 as_bad (_("missing symbol name for .variant_cc directive"));
5120 sym
= symbol_find_or_make (name
);
5121 restore_line_pointer (c
);
5122 demand_empty_rest_of_line ();
5124 bfdsym
= symbol_get_bfdsym (sym
);
5125 elfsym
= elf_symbol_from (bfdsym
);
5126 gas_assert (elfsym
);
5127 elfsym
->internal_elf_sym
.st_other
|= STO_RISCV_VARIANT_CC
;
5130 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5131 This is needed so RISC-V specific st_other values can be independently
5132 specified for an IFUNC resolver (that is called by the dynamic linker)
5133 and the symbol it resolves (aliased to the resolver). In particular,
5134 if a function symbol has special st_other value set via directives,
5135 then attaching an IFUNC resolver to that symbol should not override
5136 the st_other setting. Requiring the directive on the IFUNC resolver
5137 symbol would be unexpected and problematic in C code, where the two
5138 symbols appear as two independent function declarations. */
5141 riscv_elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
5143 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
5144 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
5145 /* If size is unset, copy size from src. Because we don't track whether
5146 .size has been used, we can't differentiate .size dest, 0 from the case
5147 where dest's size is unset. */
5148 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
5152 destelf
->size
= XNEW (expressionS
);
5153 *destelf
->size
= *srcelf
->size
;
5155 S_SET_SIZE (dest
, S_GET_SIZE (src
));
5159 /* RISC-V pseudo-ops table. */
5160 static const pseudo_typeS riscv_pseudo_table
[] =
5162 {"option", s_riscv_option
, 0},
5166 {"dtprelword", s_dtprel
, 4},
5167 {"dtpreldword", s_dtprel
, 8},
5169 {"uleb128", s_riscv_leb128
, 0},
5170 {"sleb128", s_riscv_leb128
, 1},
5171 {"insn", s_riscv_insn
, 0},
5172 {"attribute", s_riscv_attribute
, 0},
5173 {"variant_cc", s_variant_cc
, 0},
5174 {"float16", float_cons
, 'h'},
5180 riscv_pop_insert (void)
5182 extern void pop_insert (const pseudo_typeS
*);
5184 pop_insert (riscv_pseudo_table
);