1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2024 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 /* The identifier of the assembler macro we are expanding, if any. */
63 static int source_macro
= -1;
65 /* All RISC-V CSR belong to one of these classes. */
71 CSR_CLASS_I_32
, /* rv32 only */
72 CSR_CLASS_F
, /* f-ext only */
73 CSR_CLASS_ZKR
, /* zkr only */
74 CSR_CLASS_ZCMT
, /* zcmt only */
75 CSR_CLASS_V
, /* rvv only */
76 CSR_CLASS_DEBUG
, /* debug CSR */
77 CSR_CLASS_H
, /* hypervisor */
78 CSR_CLASS_H_32
, /* hypervisor, rv32 only */
79 CSR_CLASS_SMAIA
, /* Smaia */
80 CSR_CLASS_SMAIA_32
, /* Smaia, rv32 only */
81 CSR_CLASS_SMAIA_OR_SMCSRIND
, /* Smaia/Smcsrind */
82 CSR_CLASS_SMCSRIND
, /* Smcsrind */
83 CSR_CLASS_SMCNTRPMF
, /* Smcntrpmf */
84 CSR_CLASS_SMCNTRPMF_32
, /* Smcntrpmf, rv32 only */
85 CSR_CLASS_SMRNMI
, /* Smrnmi */
86 CSR_CLASS_SMSTATEEN
, /* Smstateen only */
87 CSR_CLASS_SMSTATEEN_32
, /* Smstateen RV32 only */
88 CSR_CLASS_SSAIA
, /* Ssaia */
89 CSR_CLASS_SSAIA_AND_H
, /* Ssaia with H */
90 CSR_CLASS_SSAIA_32
, /* Ssaia, rv32 only */
91 CSR_CLASS_SSAIA_AND_H_32
, /* Ssaia with H, rv32 only */
92 CSR_CLASS_SSAIA_OR_SSCSRIND
, /* Ssaia/Smcsrind */
93 CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H
, /* Ssaia/Smcsrind with H */
94 CSR_CLASS_SSCSRIND
, /* Sscsrind */
95 CSR_CLASS_SSCSRIND_AND_H
, /* Sscsrind with H */
96 CSR_CLASS_SSSTATEEN
, /* S[ms]stateen only */
97 CSR_CLASS_SSSTATEEN_AND_H
, /* S[ms]stateen only (with H) */
98 CSR_CLASS_SSSTATEEN_AND_H_32
, /* S[ms]stateen RV32 only (with H) */
99 CSR_CLASS_SSCOFPMF
, /* Sscofpmf only */
100 CSR_CLASS_SSCOFPMF_32
, /* Sscofpmf RV32 only */
101 CSR_CLASS_SSTC
, /* Sstc only */
102 CSR_CLASS_SSTC_AND_H
, /* Sstc only (with H) */
103 CSR_CLASS_SSTC_32
, /* Sstc RV32 only */
104 CSR_CLASS_SSTC_AND_H_32
, /* Sstc RV32 only (with H) */
105 CSR_CLASS_XTHEADVECTOR
, /* xtheadvector only */
108 /* This structure holds all restricted conditions for a CSR. */
109 struct riscv_csr_extra
111 /* Class to which this CSR belongs. Used to decide whether or
112 not this CSR is legal in the current -march context. */
113 enum riscv_csr_class csr_class
;
115 /* CSR may have differnet numbers in the previous priv spec. */
118 /* Record the CSR is defined/valid in which versions. */
119 enum riscv_spec_class define_version
;
121 /* Record the CSR is aborted/invalid from which versions. If it isn't
122 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
123 enum riscv_spec_class abort_version
;
125 /* The CSR may have more than one setting. */
126 struct riscv_csr_extra
*next
;
129 /* This structure contains information about errors that occur within the
131 struct riscv_ip_error
133 /* General error message */
136 /* Statement that caused the error */
139 /* Missing extension that needs to be enabled */
140 const char* missing_ext
;
144 #define DEFAULT_ARCH "riscv64"
147 #ifndef DEFAULT_RISCV_ATTR
148 #define DEFAULT_RISCV_ATTR 0
151 /* Let riscv_after_parse_args set the default value according to xlen. */
152 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
153 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
156 /* Need to sync the version with RISC-V compiler. */
157 #ifndef DEFAULT_RISCV_ISA_SPEC
158 #define DEFAULT_RISCV_ISA_SPEC "20191213"
161 #ifndef DEFAULT_RISCV_PRIV_SPEC
162 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
165 static const char default_arch
[] = DEFAULT_ARCH
;
166 static const char *default_arch_with_ext
= DEFAULT_RISCV_ARCH_WITH_EXT
;
167 static const char *file_arch_str
= NULL
;
168 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_NONE
;
169 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
171 static unsigned xlen
= 0; /* The width of an x-register. */
172 static unsigned abi_xlen
= 0; /* The width of a pointer in the ABI. */
173 static bool rve_abi
= false;
176 FLOAT_ABI_DEFAULT
= -1,
182 static enum float_abi float_abi
= FLOAT_ABI_DEFAULT
;
184 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
185 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
187 static unsigned elf_flags
= 0;
189 /* Indicate whether we are already assembling any instructions. */
190 static bool start_assemble
= false;
192 static bool probing_insn_operands
;
194 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
195 Otherwise, return 1. */
198 riscv_set_default_isa_spec (const char *s
)
200 enum riscv_spec_class
class = ISA_SPEC_CLASS_NONE
;
201 RISCV_GET_ISA_SPEC_CLASS (s
, class);
202 if (class == ISA_SPEC_CLASS_NONE
)
204 as_bad ("unknown default ISA spec `%s' set by "
205 "-misa-spec or --with-isa-spec", s
);
209 default_isa_spec
= class;
213 /* Set the default_priv_spec. Find the privileged elf attributes when
214 the input string is NULL. Return 0 if the spec isn't supported.
215 Otherwise, return 1. */
218 riscv_set_default_priv_spec (const char *s
)
220 enum riscv_spec_class
class = PRIV_SPEC_CLASS_NONE
;
221 unsigned major
, minor
, revision
;
224 RISCV_GET_PRIV_SPEC_CLASS (s
, class);
225 if (class != PRIV_SPEC_CLASS_NONE
226 && class != PRIV_SPEC_CLASS_1P9P1
)
228 default_priv_spec
= class;
234 as_bad (_("unknown default privileged spec `%s' set by "
235 "-mpriv-spec or --with-priv-spec"), s
);
239 /* Set the default_priv_spec by the privileged elf attributes. */
240 attr
= elf_known_obj_attributes_proc (stdoutput
);
241 major
= (unsigned) attr
[Tag_RISCV_priv_spec
].i
;
242 minor
= (unsigned) attr
[Tag_RISCV_priv_spec_minor
].i
;
243 revision
= (unsigned) attr
[Tag_RISCV_priv_spec_revision
].i
;
244 /* Version 0.0.0 is the default value and meningless. */
245 if (major
== 0 && minor
== 0 && revision
== 0)
248 riscv_get_priv_spec_class_from_numbers (major
, minor
, revision
, &class);
249 if (class != PRIV_SPEC_CLASS_NONE
)
251 default_priv_spec
= class;
255 /* Still can not find the privileged spec class. */
256 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
257 "privileged elf attributes"), major
, minor
, revision
);
261 /* This is the set of options which the .option pseudo-op may modify. */
262 struct riscv_set_options
264 int pic
; /* Generate position-independent code. */
265 int rvc
; /* Generate RVC code. */
266 int relax
; /* Emit relocs the linker is allowed to relax. */
267 int arch_attr
; /* Emit architecture and privileged elf attributes. */
268 int csr_check
; /* Enable the CSR checking. */
271 static struct riscv_set_options riscv_opts
=
276 DEFAULT_RISCV_ATTR
, /* arch_attr */
280 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
281 for elf_flags once we have enabled c extension. */
284 riscv_set_rvc (bool rvc_value
)
287 elf_flags
|= EF_RISCV_RVC
;
289 if (start_assemble
&& subseg_text_p (now_seg
)
290 && riscv_opts
.rvc
&& !rvc_value
)
292 struct riscv_segment_info_type
*info
293 = &seg_info(now_seg
)->tc_segment_info_data
;
295 info
->last_insn16
= true;
296 info
->rvc
= rvc_value
;
299 riscv_opts
.rvc
= rvc_value
;
302 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
307 elf_flags
|= EF_RISCV_TSO
;
310 /* The linked list hanging off of .subsets_list records all enabled extensions,
311 which are parsed from the architecture string. The architecture string can
312 be set by the -march option, the elf architecture attributes, and the
313 --with-arch configure option. */
314 static riscv_parse_subset_t riscv_rps_as
=
316 NULL
, /* subset_list, we will set it later once
317 riscv_opts_stack is created or updated. */
318 as_bad
, /* error_handler. */
320 &default_isa_spec
, /* isa_spec. */
321 true, /* check_unknown_prefixed_ext. */
324 /* Update file/function-level architecture string according to the
328 riscv_set_arch_str (const char **arch_str_p
)
330 riscv_subset_list_t
*subsets
= riscv_rps_as
.subset_list
;
331 const char *arch_str
= *arch_str_p
;
332 if (arch_str
!= NULL
)
333 free ((void *) arch_str
);
334 *arch_str_p
= riscv_arch_str (xlen
, subsets
);
337 /* This structure is used to hold a stack of .option values. */
338 struct riscv_option_stack
340 struct riscv_option_stack
*next
;
341 struct riscv_set_options options
;
342 riscv_subset_list_t
*subset_list
;
345 static struct riscv_option_stack
*riscv_opts_stack
= NULL
;
347 /* Set which ISA and extensions are available. */
350 riscv_set_arch (const char *s
)
352 if (s
!= NULL
&& strcmp (s
, "") == 0)
354 as_bad (_("the architecture string of -march and elf architecture "
355 "attributes cannot be empty"));
359 if (riscv_rps_as
.subset_list
== NULL
)
361 riscv_rps_as
.subset_list
= XNEW (riscv_subset_list_t
);
362 riscv_rps_as
.subset_list
->head
= NULL
;
363 riscv_rps_as
.subset_list
->tail
= NULL
;
364 riscv_rps_as
.subset_list
->arch_str
= NULL
;
366 riscv_release_subset_list (riscv_rps_as
.subset_list
);
367 riscv_parse_subset (&riscv_rps_as
, s
);
368 riscv_set_arch_str (&file_arch_str
);
369 riscv_set_arch_str (&riscv_rps_as
.subset_list
->arch_str
);
371 riscv_set_rvc (riscv_subset_supports (&riscv_rps_as
, "c")
372 || riscv_subset_supports (&riscv_rps_as
, "zca"));
374 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
378 /* Indicate -mabi option is explictly set. */
379 static bool explicit_mabi
= false;
381 /* Set the abi information. */
384 riscv_set_abi (unsigned new_xlen
, enum float_abi new_float_abi
, bool rve
)
387 float_abi
= new_float_abi
;
391 /* If the -mabi option isn't set, then set the abi according to the
392 ISA string. Otherwise, check if there is any conflict. */
395 riscv_set_abi_by_arch (void)
399 if (riscv_subset_supports (&riscv_rps_as
, "q"))
400 riscv_set_abi (xlen
, FLOAT_ABI_QUAD
, false);
401 else if (riscv_subset_supports (&riscv_rps_as
, "d"))
402 riscv_set_abi (xlen
, FLOAT_ABI_DOUBLE
, false);
403 else if (riscv_subset_supports (&riscv_rps_as
, "e"))
404 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, true);
406 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, false);
410 gas_assert (abi_xlen
!= 0 && xlen
!= 0 && float_abi
!= FLOAT_ABI_DEFAULT
);
412 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen
, xlen
);
413 else if (abi_xlen
< xlen
)
414 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen
, xlen
);
416 if (riscv_subset_supports (&riscv_rps_as
, "e") && !rve_abi
)
417 as_bad ("only ilp32e/lp64e ABI are supported for e extension");
419 if (float_abi
== FLOAT_ABI_SINGLE
420 && !riscv_subset_supports (&riscv_rps_as
, "f"))
421 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
423 else if (float_abi
== FLOAT_ABI_DOUBLE
424 && !riscv_subset_supports (&riscv_rps_as
, "d"))
425 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
427 else if (float_abi
== FLOAT_ABI_QUAD
428 && !riscv_subset_supports (&riscv_rps_as
, "q"))
429 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
433 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
434 elf_flags
&= ~EF_RISCV_FLOAT_ABI
;
435 elf_flags
|= float_abi
<< 1;
438 elf_flags
|= EF_RISCV_RVE
;
441 /* Handle of the OPCODE hash table. */
442 static htab_t op_hash
= NULL
;
444 /* Handle of the type of .insn hash table. */
445 static htab_t insn_type_hash
= NULL
;
447 /* This array holds the chars that always start a comment. If the
448 pre-processor is disabled, these aren't very useful. */
449 const char comment_chars
[] = "#";
451 /* This array holds the chars that only start a comment at the beginning of
452 a line. If the line seems to have the form '# 123 filename'
453 .line and .file directives will appear in the pre-processed output
455 Note that input_file.c hand checks for '#' at the beginning of the
456 first line of the input file. This is because the compiler outputs
457 #NO_APP at the beginning of its output.
459 Also note that C style comments are always supported. */
460 const char line_comment_chars
[] = "#";
462 /* This array holds machine specific line separator characters. */
463 const char line_separator_chars
[] = ";";
465 /* Chars that can be used to separate mant from exp in floating point nums. */
466 const char EXP_CHARS
[] = "eE";
468 /* Chars that mean this number is a floating point constant.
469 As in 0f12.456 or 0d1.2345e12. */
470 const char FLT_CHARS
[] = "rRsSfFdDxXpPhH";
472 /* Indicate ELF attributes are explicitly set. */
473 static bool explicit_attr
= false;
475 /* Indicate CSR or priv instructions are explicitly used. */
476 static bool explicit_priv_attr
= false;
478 static char *expr_parse_end
;
480 /* Macros for encoding relaxation state for RVC branches and far jumps. */
481 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
484 | ((uncond) ? 1 : 0) \
487 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
488 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
489 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
490 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
492 /* Is the given value a sign-extended 32-bit value? */
493 #define IS_SEXT_32BIT_NUM(x) \
494 (((x) &~ (offsetT) 0x7fffffff) == 0 \
495 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
497 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
498 #define IS_ZEXT_32BIT_NUM(x) \
499 (((x) &~ (offsetT) 0xffffffff) == 0 \
500 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
502 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
503 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
504 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
505 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
507 #define INSERT_IMM(n, s, INSN, VALUE) \
508 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
510 /* Determine if an instruction matches an opcode. */
511 #define OPCODE_MATCHES(OPCODE, OP) \
512 (((OPCODE) & MASK_##OP) == MATCH_##OP)
514 /* Create a new mapping symbol for the transition to STATE. */
517 make_mapping_symbol (enum riscv_seg_mstate state
,
520 const char *arch_str
,
521 bool odd_data_padding
)
531 if (arch_str
!= NULL
)
533 size_t size
= strlen (arch_str
) + 3; /* "$x" + '\0' */
534 buff
= xmalloc (size
);
535 snprintf (buff
, size
, "$x%s", arch_str
);
545 symbolS
*symbol
= symbol_new (name
, now_seg
, frag
, value
);
546 symbol_get_bfdsym (symbol
)->flags
|= (BSF_NO_FLAGS
| BSF_LOCAL
);
547 if (arch_str
!= NULL
)
549 /* Store current $x+arch into tc_segment_info. */
550 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
= symbol
;
551 xfree ((void *) buff
);
554 /* If .fill or other data filling directive generates zero sized data,
555 then mapping symbol for the following code will have the same value.
557 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
558 and .text.zero.fill.last. */
559 symbolS
*first
= frag
->tc_frag_data
.first_map_symbol
;
560 symbolS
*last
= frag
->tc_frag_data
.last_map_symbol
;
561 symbolS
*removed
= NULL
;
566 know (S_GET_VALUE (first
) == S_GET_VALUE (symbol
)
568 /* Remove the old one. */
571 frag
->tc_frag_data
.first_map_symbol
= symbol
;
573 else if (last
!= NULL
)
575 /* The mapping symbols should be added in offset order. */
576 know (S_GET_VALUE (last
) <= S_GET_VALUE (symbol
));
577 /* Remove the old one. */
578 if (S_GET_VALUE (last
) == S_GET_VALUE (symbol
))
581 frag
->tc_frag_data
.last_map_symbol
= symbol
;
583 if (odd_data_padding
)
585 /* If the removed mapping symbol is $x+arch, then add it back to
587 const char *str
= removed
!= NULL
588 && strncmp (S_GET_NAME (removed
), "$xrv", 4) == 0
589 ? S_GET_NAME (removed
) + 2 : NULL
;
590 make_mapping_symbol (MAP_INSN
, frag
->fr_fix
+ 1, frag
, str
,
591 false/* odd_data_padding */);
595 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
598 /* Set the mapping state for frag_now. */
601 riscv_mapping_state (enum riscv_seg_mstate to_state
,
605 enum riscv_seg_mstate from_state
=
606 seg_info (now_seg
)->tc_segment_info_data
.map_state
;
607 bool reset_seg_arch_str
= false;
609 if (!SEG_NORMAL (now_seg
)
610 /* For now we only add the mapping symbols to text sections.
611 Therefore, the dis-assembler only show the actual contents
612 distribution for text. Other sections will be shown as
613 data without the details. */
614 || !subseg_text_p (now_seg
))
617 /* The mapping symbol should be emitted if not in the right
619 symbolS
*seg_arch_symbol
=
620 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
;
621 if (to_state
== MAP_INSN
&& seg_arch_symbol
== 0)
623 /* Always add $x+arch at the first instruction of section. */
624 reset_seg_arch_str
= true;
626 else if (seg_arch_symbol
!= 0
627 && to_state
== MAP_INSN
629 && strcmp (riscv_rps_as
.subset_list
->arch_str
,
630 S_GET_NAME (seg_arch_symbol
) + 2) != 0)
632 reset_seg_arch_str
= true;
634 else if (from_state
== to_state
)
637 valueT value
= (valueT
) (frag_now_fix () - max_chars
);
638 seg_info (now_seg
)->tc_segment_info_data
.map_state
= to_state
;
639 seg_info (now_seg
)->tc_segment_info_data
.last_insn16
= false;
640 const char *arch_str
= reset_seg_arch_str
641 ? riscv_rps_as
.subset_list
->arch_str
: NULL
;
642 make_mapping_symbol (to_state
, value
, frag_now
, arch_str
,
643 false/* odd_data_padding */);
646 /* Add the odd bytes of paddings for riscv_handle_align. */
649 riscv_add_odd_padding_symbol (fragS
*frag
)
651 /* If there was already a mapping symbol, it should be
652 removed in the make_mapping_symbol.
654 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
655 make_mapping_symbol (MAP_DATA
, frag
->fr_fix
, frag
,
656 NULL
/* arch_str */, true/* odd_data_padding */);
659 /* Remove any excess mapping symbols generated for alignment frags in
660 SEC. We may have created a mapping symbol before a zero byte
661 alignment; remove it if there's a mapping symbol after the
665 riscv_check_mapping_symbols (bfd
*abfd ATTRIBUTE_UNUSED
,
667 void *dummy ATTRIBUTE_UNUSED
)
669 segment_info_type
*seginfo
= seg_info (sec
);
672 if (seginfo
== NULL
|| seginfo
->frchainP
== NULL
)
675 for (fragp
= seginfo
->frchainP
->frch_root
;
677 fragp
= fragp
->fr_next
)
679 symbolS
*last
= fragp
->tc_frag_data
.last_map_symbol
;
680 fragS
*next
= fragp
->fr_next
;
682 if (last
== NULL
|| next
== NULL
)
685 /* Check the last mapping symbol if it is at the boundary of
687 if (S_GET_VALUE (last
) < next
->fr_address
)
689 know (S_GET_VALUE (last
) == next
->fr_address
);
693 symbolS
*next_first
= next
->tc_frag_data
.first_map_symbol
;
694 if (next_first
!= NULL
)
696 /* The last mapping symbol overlaps with another one
697 which at the start of the next frag.
699 Please see the gas/testsuite/gas/riscv/mapping.s:
700 .text.zero.fill.align.A and .text.zero.fill.align.B. */
701 know (S_GET_VALUE (last
) == S_GET_VALUE (next_first
));
702 symbolS
*removed
= last
;
703 if (strncmp (S_GET_NAME (last
), "$xrv", 4) == 0
704 && strcmp (S_GET_NAME (next_first
), "$x") == 0)
705 removed
= next_first
;
706 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
710 if (next
->fr_next
== NULL
)
712 /* The last mapping symbol is at the end of the section.
714 Please see the gas/testsuite/gas/riscv/mapping.s:
715 .text.last.section. */
716 know (next
->fr_fix
== 0 && next
->fr_var
== 0);
717 symbol_remove (last
, &symbol_rootP
, &symbol_lastP
);
721 /* Since we may have empty frags without any mapping symbols,
722 keep looking until the non-empty frag. */
723 if (next
->fr_address
!= next
->fr_next
->fr_address
)
726 next
= next
->fr_next
;
728 while (next
!= NULL
);
732 /* The default target format to use. */
735 riscv_target_format (void)
737 if (target_big_endian
)
738 return xlen
== 64 ? "elf64-bigriscv" : "elf32-bigriscv";
740 return xlen
== 64 ? "elf64-littleriscv" : "elf32-littleriscv";
743 /* Return the length of instruction INSN. */
745 static inline unsigned int
746 insn_length (const struct riscv_cl_insn
*insn
)
748 return riscv_insn_length (insn
->insn_opcode
);
751 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
754 create_insn (struct riscv_cl_insn
*insn
, const struct riscv_opcode
*mo
)
757 insn
->insn_opcode
= mo
->match
;
758 insn
->insn_long_opcode
[0] = 0;
764 /* Install INSN at the location specified by its "frag" and "where" fields. */
767 install_insn (const struct riscv_cl_insn
*insn
)
769 char *f
= insn
->frag
->fr_literal
+ insn
->where
;
770 if (insn
->insn_long_opcode
[0] != 0)
771 memcpy (f
, insn
->insn_long_opcode
, insn_length (insn
));
773 number_to_chars_littleendian (f
, insn
->insn_opcode
, insn_length (insn
));
776 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
777 and install the opcode in the new location. */
780 move_insn (struct riscv_cl_insn
*insn
, fragS
*frag
, long where
)
784 if (insn
->fixp
!= NULL
)
786 insn
->fixp
->fx_frag
= frag
;
787 insn
->fixp
->fx_where
= where
;
792 /* Add INSN to the end of the output. */
795 add_fixed_insn (struct riscv_cl_insn
*insn
)
797 char *f
= frag_more (insn_length (insn
));
798 move_insn (insn
, frag_now
, f
- frag_now
->fr_literal
);
802 add_relaxed_insn (struct riscv_cl_insn
*insn
, int max_chars
, int var
,
803 relax_substateT subtype
, symbolS
*symbol
, offsetT offset
)
805 frag_grow (max_chars
);
806 move_insn (insn
, frag_now
, frag_more (0) - frag_now
->fr_literal
);
807 frag_var (rs_machine_dependent
, max_chars
, var
,
808 subtype
, symbol
, offset
, NULL
);
811 /* Compute the length of a branch sequence, and adjust the stored length
812 accordingly. If FRAGP is NULL, the worst-case length is returned. */
815 relaxed_branch_length (fragS
*fragp
, asection
*sec
, int update
)
817 int jump
, rvc
, length
= 8;
822 jump
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
);
823 rvc
= RELAX_BRANCH_RVC (fragp
->fr_subtype
);
824 length
= RELAX_BRANCH_LENGTH (fragp
->fr_subtype
);
826 /* Assume jumps are in range; the linker will catch any that aren't. */
827 length
= jump
? 4 : 8;
829 if (fragp
->fr_symbol
!= NULL
830 && S_IS_DEFINED (fragp
->fr_symbol
)
831 && !S_IS_WEAK (fragp
->fr_symbol
)
832 && sec
== S_GET_SEGMENT (fragp
->fr_symbol
))
834 offsetT val
= S_GET_VALUE (fragp
->fr_symbol
) + fragp
->fr_offset
;
835 bfd_vma rvc_range
= jump
? RVC_JUMP_REACH
: RVC_BRANCH_REACH
;
836 val
-= fragp
->fr_address
+ fragp
->fr_fix
;
838 if (rvc
&& (bfd_vma
)(val
+ rvc_range
/2) < rvc_range
)
840 else if ((bfd_vma
)(val
+ RISCV_BRANCH_REACH
/2) < RISCV_BRANCH_REACH
)
842 else if (!jump
&& rvc
)
847 fragp
->fr_subtype
= RELAX_BRANCH_ENCODE (jump
, rvc
, length
);
852 /* Information about an opcode name, mnemonics and its value. */
859 /* List for all supported opcode name. */
860 static const struct opcode_name_t opcode_name_list
[] =
905 /* Hash table for lookup opcode name. */
906 static htab_t opcode_names_hash
= NULL
;
908 /* Initialization for hash table of opcode name. */
911 init_opcode_names_hash (void)
913 const struct opcode_name_t
*opcode
;
915 for (opcode
= &opcode_name_list
[0]; opcode
->name
!= NULL
; ++opcode
)
916 if (str_hash_insert (opcode_names_hash
, opcode
->name
, opcode
, 0) != NULL
)
917 as_fatal (_("internal: duplicate %s"), opcode
->name
);
920 /* Find `s` is a valid opcode name or not, return the opcode name info
923 static const struct opcode_name_t
*
924 opcode_name_lookup (char **s
)
928 struct opcode_name_t
*o
;
930 /* Find end of name. */
932 if (is_name_beginner (*e
))
934 while (is_part_of_name (*e
))
937 /* Terminate name. */
941 o
= (struct opcode_name_t
*) str_hash_find (opcode_names_hash
, *s
);
943 /* Advance to next token if one was recognized. */
953 /* All RISC-V registers belong to one of these classes. */
965 static htab_t reg_names_hash
= NULL
;
966 static htab_t csr_extra_hash
= NULL
;
968 #define ENCODE_REG_HASH(cls, n) \
969 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
970 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
971 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
974 hash_reg_name (enum reg_class
class, const char *name
, unsigned n
)
976 void *hash
= ENCODE_REG_HASH (class, n
);
977 if (str_hash_insert (reg_names_hash
, name
, hash
, 0) != NULL
)
978 as_fatal (_("internal: duplicate %s"), name
);
982 hash_reg_names (enum reg_class
class, const char names
[][NRC
], unsigned n
)
986 for (i
= 0; i
< n
; i
++)
987 hash_reg_name (class, names
[i
], i
);
990 /* Init hash table csr_extra_hash to handle CSR. */
993 riscv_init_csr_hash (const char *name
,
995 enum riscv_csr_class
class,
996 enum riscv_spec_class define_version
,
997 enum riscv_spec_class abort_version
)
999 struct riscv_csr_extra
*entry
, *pre_entry
;
1000 bool need_enrty
= true;
1003 entry
= (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, name
);
1004 while (need_enrty
&& entry
!= NULL
)
1006 if (entry
->csr_class
== class
1007 && entry
->address
== address
1008 && entry
->define_version
== define_version
1009 && entry
->abort_version
== abort_version
)
1012 entry
= entry
->next
;
1015 /* Duplicate CSR. */
1019 entry
= notes_alloc (sizeof (*entry
));
1020 entry
->csr_class
= class;
1021 entry
->address
= address
;
1022 entry
->define_version
= define_version
;
1023 entry
->abort_version
= abort_version
;
1026 if (pre_entry
== NULL
)
1027 str_hash_insert (csr_extra_hash
, name
, entry
, 0);
1029 pre_entry
->next
= entry
;
1032 /* Return the CSR address after checking the ISA dependency and
1033 the privileged spec version.
1035 There are one warning and two errors for CSR,
1037 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1038 or the privileged spec, report warning only if -mcsr-check is set.
1039 Unknown CSR: the CSR has never been defined, report error.
1040 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1043 riscv_csr_address (const char *csr_name
,
1044 struct riscv_csr_extra
*entry
)
1046 struct riscv_csr_extra
*saved_entry
= entry
;
1047 enum riscv_csr_class csr_class
= entry
->csr_class
;
1048 bool need_check_version
= false;
1049 bool is_rv32_only
= false;
1050 bool is_h_required
= false;
1051 const char* extension
= NULL
;
1055 case CSR_CLASS_I_32
:
1056 is_rv32_only
= true;
1059 need_check_version
= true;
1062 case CSR_CLASS_H_32
:
1063 is_rv32_only
= true;
1074 case CSR_CLASS_ZCMT
:
1078 extension
= "zve32x";
1080 case CSR_CLASS_SMAIA_32
:
1081 is_rv32_only
= true;
1083 case CSR_CLASS_SMAIA
:
1084 extension
= "smaia";
1086 case CSR_CLASS_SMAIA_OR_SMCSRIND
:
1087 extension
= "smaia or smcsrind";
1089 case CSR_CLASS_SMCSRIND
:
1090 extension
= "smcsrind";
1092 case CSR_CLASS_SMCNTRPMF_32
:
1093 is_rv32_only
= true;
1095 case CSR_CLASS_SMCNTRPMF
:
1096 need_check_version
= true;
1097 extension
= "smcntrpmf";
1099 case CSR_CLASS_SMRNMI
:
1100 extension
= "smrnmi";
1102 case CSR_CLASS_SMSTATEEN_32
:
1103 is_rv32_only
= true;
1105 case CSR_CLASS_SMSTATEEN
:
1106 extension
= "smstateen";
1108 case CSR_CLASS_SSAIA
:
1109 case CSR_CLASS_SSAIA_AND_H
:
1110 case CSR_CLASS_SSAIA_32
:
1111 case CSR_CLASS_SSAIA_AND_H_32
:
1112 is_rv32_only
= (csr_class
== CSR_CLASS_SSAIA_32
1113 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1114 is_h_required
= (csr_class
== CSR_CLASS_SSAIA_AND_H
1115 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1116 extension
= "ssaia";
1118 case CSR_CLASS_SSAIA_OR_SSCSRIND
:
1119 case CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H
:
1120 is_h_required
= (csr_class
== CSR_CLASS_SSAIA_OR_SSCSRIND_AND_H
);
1121 extension
= "ssaia or sscsrind";
1123 case CSR_CLASS_SSCSRIND
:
1124 case CSR_CLASS_SSCSRIND_AND_H
:
1125 is_h_required
= (csr_class
== CSR_CLASS_SSCSRIND_AND_H
);
1126 extension
= "sscsrind";
1128 case CSR_CLASS_SSSTATEEN_AND_H_32
:
1129 is_rv32_only
= true;
1131 case CSR_CLASS_SSSTATEEN_AND_H
:
1132 is_h_required
= true;
1134 case CSR_CLASS_SSSTATEEN
:
1135 extension
= "ssstateen";
1137 case CSR_CLASS_SSCOFPMF_32
:
1138 is_rv32_only
= true;
1140 case CSR_CLASS_SSCOFPMF
:
1141 extension
= "sscofpmf";
1143 case CSR_CLASS_SSTC
:
1144 case CSR_CLASS_SSTC_AND_H
:
1145 case CSR_CLASS_SSTC_32
:
1146 case CSR_CLASS_SSTC_AND_H_32
:
1147 is_rv32_only
= (csr_class
== CSR_CLASS_SSTC_32
1148 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1149 is_h_required
= (csr_class
== CSR_CLASS_SSTC_AND_H
1150 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1153 case CSR_CLASS_DEBUG
:
1155 case CSR_CLASS_XTHEADVECTOR
:
1156 extension
= "xtheadvector";
1159 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class
);
1162 if (riscv_opts
.csr_check
)
1164 if (is_rv32_only
&& xlen
!= 32)
1165 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name
);
1166 if (is_h_required
&& !riscv_subset_supports (&riscv_rps_as
, "h"))
1167 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name
);
1169 if (extension
!= NULL
1170 && !riscv_subset_supports (&riscv_rps_as
, extension
))
1171 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1172 csr_name
, extension
);
1175 while (entry
!= NULL
)
1177 if (!need_check_version
1178 || (default_priv_spec
>= entry
->define_version
1179 && default_priv_spec
< entry
->abort_version
))
1181 /* Find the CSR according to the specific version. */
1182 return entry
->address
;
1184 entry
= entry
->next
;
1187 /* Can not find the CSR address from the chosen privileged version,
1188 so use the newly defined value. */
1189 if (riscv_opts
.csr_check
)
1191 const char *priv_name
= NULL
;
1192 RISCV_GET_PRIV_SPEC_NAME (priv_name
, default_priv_spec
);
1193 if (priv_name
!= NULL
)
1194 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1195 csr_name
, priv_name
);
1198 return saved_entry
->address
;
1201 /* Return -1 if the CSR has never been defined. Otherwise, return
1205 reg_csr_lookup_internal (const char *s
)
1207 struct riscv_csr_extra
*r
=
1208 (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, s
);
1213 return riscv_csr_address (s
, r
);
1217 reg_lookup_internal (const char *s
, enum reg_class
class)
1221 if (class == RCLASS_CSR
)
1222 return reg_csr_lookup_internal (s
);
1224 r
= str_hash_find (reg_names_hash
, s
);
1225 if (r
== NULL
|| DECODE_REG_CLASS (r
) != class)
1228 if (riscv_subset_supports (&riscv_rps_as
, "e")
1229 && class == RCLASS_GPR
1230 && DECODE_REG_NUM (r
) > 15)
1233 return DECODE_REG_NUM (r
);
1237 reg_lookup (char **s
, enum reg_class
class, unsigned int *regnop
)
1243 /* Find end of name. */
1245 if (is_name_beginner (*e
))
1247 while (is_part_of_name (*e
))
1250 /* Terminate name. */
1254 /* Look for the register. Advance to next token if one was recognized. */
1255 if ((reg
= reg_lookup_internal (*s
, class)) >= 0)
1265 arg_lookup (char **s
, const char *const *array
, size_t size
, unsigned *regnop
)
1267 const char *p
= strchr (*s
, ',');
1268 size_t i
, len
= p
? (size_t)(p
- *s
) : strlen (*s
);
1273 for (i
= 0; i
< size
; i
++)
1274 if (array
[i
] != NULL
&& strncmp (array
[i
], *s
, len
) == 0
1275 && array
[i
][len
] == '\0')
1286 flt_lookup (float f
, const float *array
, size_t size
, unsigned *regnop
)
1290 for (i
= 0; i
< size
; i
++)
1300 /* Map ra and s-register to [4,15], so that we can check if the
1301 reg2 in register list reg1-reg2 or single reg2 is valid or not,
1302 and obtain the corresponding reg_list value.
1312 regno_to_reg_list (unsigned regno
)
1316 else if (regno
== X_S0
|| regno
== X_S1
)
1317 return 5 + regno
- X_S0
;
1318 else if (regno
>= X_S2
&& regno
< X_S10
)
1319 return 7 + regno
- X_S2
;
1320 else if (regno
== X_S11
)
1323 /* Invalid symbol. */
1327 /* Parse register list, and return the last register by regno_to_reg_list.
1329 If ABI register names are used (e.g. ra and s0), the register
1330 list could be "{ra}", "{ra, s0}", "{ra, s0-sN}", where 0 < N < 10 or
1333 If numeric register names are used (e.g. x1 and x8), the register list
1334 could be "{x1}", "{x1,x8}", "{x1,x8-x9}", "{x1,x8-x9,x18}" and
1335 "{x1,x8-x9,x18-xN}", where 19 < N < 25 or N == 27.
1337 The numeric and ABI register names cannot be used at the same time.
1339 TODO: Report errors for the following cases,
1340 1. Too many registers in the list.
1341 2. Cases which return 0.
1342 3. Illegal formats, for example, {x1,x8-NULL,x18-x24/x18}, {x1-x2,x8}. */
1345 reglist_lookup_internal (char *reglist
)
1348 unsigned reg_list
= 0;
1349 char *regname
[3][2] = {{NULL
}};
1350 char *save_tok
, *save_subtok
;
1353 char *token
= strtok_r (reglist
, ",", &save_tok
);
1354 for (i
= 0; i
< 3 && token
!= NULL
;
1355 token
= strtok_r (NULL
, ",", &save_tok
), i
++)
1357 char *subtoken
= strtok_r (token
, "-", &save_subtok
);
1358 for (j
= 0; j
< 2 && subtoken
!= NULL
;
1359 subtoken
= strtok_r (NULL
, "-", &save_subtok
), j
++)
1360 regname
[i
][j
] = subtoken
;
1363 bool reg1_numeric
= false;
1364 for (i
= 0; i
< 3; i
++)
1366 if (regname
[i
][0] == NULL
)
1368 #define REG_TO_REG_LIST(NAME, NUM, LIST) \
1369 (reg_lookup (&NAME, RCLASS_GPR, &NUM) && (LIST = regno_to_reg_list (NUM)))
1370 #define REG_NUMERIC(NAME) (NAME[0] == 'x')
1371 #define REG_CONFLICT(NAME, REG_NUMERIC) \
1372 ((NAME[0] == 'x' && !REG_NUMERIC) || (NAME[0] != 'x' && REG_NUMERIC))
1376 reg1_numeric
= REG_NUMERIC (regname
[i
][0]);
1377 if (!REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1382 if (REG_CONFLICT (regname
[i
][0], reg1_numeric
)
1383 /* The second register should be s0 or its numeric names x8. */
1384 || !REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1387 else if (regname
[i
][1] == NULL
)
1390 if (REG_CONFLICT (regname
[i
][1], reg1_numeric
)
1391 /* The third register is x9 if the numeric name is used.
1392 Otherwise, it could be any other sN register, where N > 0. */
1393 || !REG_TO_REG_LIST (regname
[i
][1], regno
, reg_list
)
1395 || (reg1_numeric
&& regno
!= X_S1
))
1399 /* Must use register numeric names. */
1401 || !REG_NUMERIC (regname
[i
][0])
1402 /* The fourth register should be s2. */
1403 || !REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1406 else if (regname
[i
][1] == NULL
)
1410 || !REG_NUMERIC (regname
[i
][1])
1411 /* The fifth register could be any other sN register, where N > 1. */
1412 || !REG_TO_REG_LIST (regname
[i
][1], regno
, reg_list
)
1419 #undef REG_TO_REG_LIST
1426 /* Parse register list. Return false if REG_LIST is zero, which is an
1430 reglist_lookup (char **s
, unsigned *reg_list
)
1433 char *reglist
= strdup (*s
);
1434 if (reglist
!= NULL
)
1436 char *token
= strtok (reglist
, "}");
1439 *s
+= strlen (token
);
1440 *reg_list
= reglist_lookup_internal (reglist
);
1444 as_bad (_("cannot find `}' for cm.push/cm.pop"));
1449 return *reg_list
== 0 ? false : true;
1452 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1453 #define USE_IMM(n, s) \
1454 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1456 /* For consistency checking, verify that all bits are specified either
1457 by the match/mask part of the instruction definition, or by the
1458 operand list. The `length` could be the actual instruction length or
1459 0 for auto-detection. */
1462 validate_riscv_insn (const struct riscv_opcode
*opc
, int length
)
1464 const char *oparg
, *opargStart
;
1465 insn_t used_bits
= opc
->mask
;
1467 insn_t required_bits
;
1470 length
= riscv_insn_length (opc
->match
);
1471 /* We don't support instructions longer than 64-bits yet. */
1474 insn_width
= 8 * length
;
1476 required_bits
= ((insn_t
)~0ULL) >> (64 - insn_width
);
1478 if ((used_bits
& opc
->match
) != (opc
->match
& required_bits
))
1480 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1481 opc
->name
, opc
->args
);
1485 for (oparg
= opc
->args
; *oparg
; ++oparg
)
1493 case 'U': break; /* CRS1, constrained to equal RD. */
1494 case 'c': break; /* CRS1, constrained to equal sp. */
1495 case 'T': /* CRS2, floating point. */
1496 case 'V': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
1497 case 'S': /* CRS1S, floating point. */
1498 case 's': USE_BITS (OP_MASK_CRS1S
, OP_SH_CRS1S
); break;
1499 case 'w': break; /* CRS1S, constrained to equal RD. */
1500 case 'D': /* CRS2S, floating point. */
1501 case 't': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
1502 case 'x': break; /* CRS2S, constrained to equal RD. */
1503 case 'z': break; /* CRS2S, constrained to be x0. */
1504 case '>': /* CITYPE immediate, compressed shift. */
1505 case 'u': /* CITYPE immediate, compressed lui. */
1506 case 'v': /* CITYPE immediate, li to compressed lui. */
1507 case 'o': /* CITYPE immediate, allow zero. */
1508 case 'j': used_bits
|= ENCODE_CITYPE_IMM (-1U); break;
1509 case 'L': used_bits
|= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1510 case 'm': used_bits
|= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1511 case 'n': used_bits
|= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1512 case '6': used_bits
|= ENCODE_CSSTYPE_IMM (-1U); break;
1513 case 'M': used_bits
|= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1514 case 'N': used_bits
|= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1515 case '8': used_bits
|= ENCODE_CIWTYPE_IMM (-1U); break;
1516 case 'K': used_bits
|= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1517 /* CLTYPE and CSTYPE have the same immediate encoding. */
1518 case '5': used_bits
|= ENCODE_CLTYPE_IMM (-1U); break;
1519 case 'k': used_bits
|= ENCODE_CLTYPE_LW_IMM (-1U); break;
1520 case 'l': used_bits
|= ENCODE_CLTYPE_LD_IMM (-1U); break;
1521 case 'p': used_bits
|= ENCODE_CBTYPE_IMM (-1U); break;
1522 case 'a': used_bits
|= ENCODE_CJTYPE_IMM (-1U); break;
1523 case 'F': /* Compressed funct for .insn directive. */
1526 case '6': USE_BITS (OP_MASK_CFUNCT6
, OP_SH_CFUNCT6
); break;
1527 case '4': USE_BITS (OP_MASK_CFUNCT4
, OP_SH_CFUNCT4
); break;
1528 case '3': USE_BITS (OP_MASK_CFUNCT3
, OP_SH_CFUNCT3
); break;
1529 case '2': USE_BITS (OP_MASK_CFUNCT2
, OP_SH_CFUNCT2
); break;
1531 goto unknown_validate_operand
;
1535 goto unknown_validate_operand
;
1537 break; /* end RVC */
1542 case 'f': USE_BITS (OP_MASK_VD
, OP_SH_VD
); break;
1543 case 'e': USE_BITS (OP_MASK_VWD
, OP_SH_VWD
); break;
1544 case 's': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
); break;
1545 case 't': USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1546 case 'u': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1547 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1548 case 'v': USE_BITS (OP_MASK_VD
, OP_SH_VD
);
1549 USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1550 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1552 case 'b': used_bits
|= ENCODE_RVV_VB_IMM (-1U); break;
1553 case 'c': used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1556 case 'k': USE_BITS (OP_MASK_VIMM
, OP_SH_VIMM
); break;
1557 case 'l': used_bits
|= ENCODE_RVV_VI_UIMM6 (-1U); break;
1558 case 'm': USE_BITS (OP_MASK_VMASK
, OP_SH_VMASK
); break;
1559 case 'M': break; /* Macro operand, must be a mask register. */
1560 case 'T': break; /* Macro operand, must be a vector register. */
1562 goto unknown_validate_operand
;
1564 break; /* end RVV */
1570 case '<': USE_BITS (OP_MASK_SHAMTW
, OP_SH_SHAMTW
); break;
1571 case '>': USE_BITS (OP_MASK_SHAMT
, OP_SH_SHAMT
); break;
1572 case 'A': break; /* Macro operand, must be symbol. */
1573 case 'B': break; /* Macro operand, must be symbol or constant. */
1574 case 'c': break; /* Macro operand, must be symbol or constant. */
1575 case 'I': break; /* Macro operand, must be constant. */
1576 case 'D': /* RD, floating point. */
1577 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1578 case 'y': USE_BITS (OP_MASK_BS
, OP_SH_BS
); break;
1579 case 'Y': USE_BITS (OP_MASK_RNUM
, OP_SH_RNUM
); break;
1580 case 'Z': /* RS1, CSR number. */
1581 case 'S': /* RS1, floating point. */
1582 case 's': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1583 case 'U': /* RS1 and RS2 are the same, floating point. */
1584 USE_BITS (OP_MASK_RS1
, OP_SH_RS1
);
1586 case 'T': /* RS2, floating point. */
1587 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1588 case 'R': /* RS3, floating point. */
1589 case 'r': USE_BITS (OP_MASK_RS3
, OP_SH_RS3
); break;
1590 case 'm': USE_BITS (OP_MASK_RM
, OP_SH_RM
); break;
1591 case 'E': USE_BITS (OP_MASK_CSR
, OP_SH_CSR
); break;
1592 case 'P': USE_BITS (OP_MASK_PRED
, OP_SH_PRED
); break;
1593 case 'Q': USE_BITS (OP_MASK_SUCC
, OP_SH_SUCC
); break;
1594 case 'o': /* ITYPE immediate, load displacement. */
1595 case 'j': used_bits
|= ENCODE_ITYPE_IMM (-1U); break;
1596 case 'a': used_bits
|= ENCODE_JTYPE_IMM (-1U); break;
1597 case 'p': used_bits
|= ENCODE_BTYPE_IMM (-1U); break;
1598 case 'q': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1599 case 'u': used_bits
|= ENCODE_UTYPE_IMM (-1U); break;
1600 case 'z': break; /* Zero immediate. */
1601 case '[': break; /* Unused operand. */
1602 case ']': break; /* Unused operand. */
1603 case '0': break; /* AMO displacement, must to zero. */
1604 case '1': break; /* Relaxation operand. */
1605 case 'F': /* Funct for .insn directive. */
1608 case '7': USE_BITS (OP_MASK_FUNCT7
, OP_SH_FUNCT7
); break;
1609 case '3': USE_BITS (OP_MASK_FUNCT3
, OP_SH_FUNCT3
); break;
1610 case '2': USE_BITS (OP_MASK_FUNCT2
, OP_SH_FUNCT2
); break;
1612 goto unknown_validate_operand
;
1615 case 'O': /* Opcode for .insn directive. */
1618 case '4': USE_BITS (OP_MASK_OP
, OP_SH_OP
); break;
1619 case '2': USE_BITS (OP_MASK_OP2
, OP_SH_OP2
); break;
1621 goto unknown_validate_operand
;
1624 case 'W': /* Various operands for standard z extensions. */
1630 case 'f': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1632 goto unknown_validate_operand
;
1638 case 'v': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1640 goto unknown_validate_operand
;
1646 /* sreg operators in cm.mvsa01 and cm.mva01s. */
1647 case '1': USE_BITS (OP_MASK_SREG1
, OP_SH_SREG1
); break;
1648 case '2': USE_BITS (OP_MASK_SREG2
, OP_SH_SREG2
); break;
1649 /* byte immediate operators, load/store byte insns. */
1650 case 'h': used_bits
|= ENCODE_ZCB_HALFWORD_UIMM (-1U); break;
1651 /* halfword immediate operators, load/store halfword insns. */
1652 case 'b': used_bits
|= ENCODE_ZCB_BYTE_UIMM (-1U); break;
1653 /* Immediate offset operand for cm.push and cm.pop. */
1654 case 'p': used_bits
|= ENCODE_ZCMP_SPIMM (-1U); break;
1655 /* Register list operand for cm.push and cm.pop. */
1656 case 'r': USE_BITS (OP_MASK_REG_LIST
, OP_SH_REG_LIST
); break;
1657 /* Table jump used by cm.jt or cm.jalt. */
1659 case 'I': used_bits
|= ENCODE_ZCMT_INDEX (-1U); break;
1662 goto unknown_validate_operand
;
1666 goto unknown_validate_operand
;
1669 case 'X': /* Vendor-specific operands. */
1672 case 't': /* Vendor-specific (T-head) operands. */
1681 case 'c': /* Vtypei for th.vsetvli. */
1682 used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1684 goto unknown_validate_operand
;
1687 case 'l': /* Integer immediate, literal. */
1688 oparg
+= strcspn(oparg
, ",") - 1;
1690 case 's': /* Integer immediate, 'XtsN@S' ... N-bit signed immediate at bit S. */
1692 case 'u': /* Integer immediate, 'XtuN@S' ... N-bit unsigned immediate at bit S. */
1695 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1697 goto unknown_validate_operand
;
1698 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1704 goto unknown_validate_operand
;
1708 case 'c': /* Vendor-specific (CORE-V) operands. */
1713 used_bits
|= ENCODE_CV_IS2_UIMM5 (-1U);
1716 used_bits
|= ENCODE_CV_IS3_UIMM5 (-1U);
1719 used_bits
|= ENCODE_CV_SIMD_IMM6(-1U);
1722 used_bits
|= ENCODE_CV_BITMANIP_UIMM5(-1U);
1725 used_bits
|= ENCODE_CV_BITMANIP_UIMM2(-1U);
1728 used_bits
|= ENCODE_CV_SIMD_UIMM6(-1U);
1732 goto unknown_validate_operand
;
1735 case 's': /* Vendor-specific (SiFive) operands. */
1738 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1739 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1743 case '2': USE_BITS (OP_MASK_XSO2
, OP_SH_XSO2
); break;
1744 case '1': USE_BITS (OP_MASK_XSO1
, OP_SH_XSO1
); break;
1746 goto unknown_validate_operand
;
1750 goto unknown_validate_operand
;
1754 goto unknown_validate_operand
;
1758 unknown_validate_operand
:
1759 as_bad (_("internal: bad RISC-V opcode "
1760 "(unknown operand type `%s'): %s %s"),
1761 opargStart
, opc
->name
, opc
->args
);
1766 if (used_bits
!= required_bits
)
1768 as_bad (_("internal: bad RISC-V opcode "
1769 "(bits %#llx undefined or invalid): %s %s"),
1770 (unsigned long long)(used_bits
^ required_bits
),
1771 opc
->name
, opc
->args
);
1779 struct percent_op_match
1782 bfd_reloc_code_real_type reloc
;
1785 /* Common hash table initialization function for instruction and .insn
1789 init_opcode_hash (const struct riscv_opcode
*opcodes
,
1790 bool insn_directive_p
)
1794 htab_t hash
= str_htab_create ();
1795 while (opcodes
[i
].name
)
1797 const char *name
= opcodes
[i
].name
;
1798 if (str_hash_insert (hash
, name
, &opcodes
[i
], 0) != NULL
)
1799 as_fatal (_("internal: duplicate %s"), name
);
1803 if (opcodes
[i
].pinfo
!= INSN_MACRO
)
1805 if (insn_directive_p
)
1806 length
= ((name
[0] == 'c') ? 2 : 4);
1808 length
= 0; /* Let assembler determine the length. */
1809 if (!validate_riscv_insn (&opcodes
[i
], length
))
1810 as_fatal (_("internal: broken assembler. "
1811 "No assembly attempted"));
1814 gas_assert (!insn_directive_p
);
1817 while (opcodes
[i
].name
&& !strcmp (opcodes
[i
].name
, name
));
1823 /* Record all PC-relative high-part relocation that we have encountered to
1824 help us resolve the corresponding low-part relocation later. */
1827 const asection
*sec
;
1831 } riscv_pcrel_hi_fixup
;
1833 /* Handle of the pcrel_hi hash table. */
1834 static htab_t riscv_pcrel_hi_fixup_hash
;
1836 /* Get the key of a entry from the pcrel_hi hash table. */
1839 riscv_pcrel_fixup_hash (const void *entry
)
1841 const riscv_pcrel_hi_fixup
*e
= entry
;
1843 /* the pcrel_hi with same address may reside in different segments,
1844 to ensure uniqueness, the segment ID needs to be included in the
1845 hash key calculation.
1846 Temporarily using the prime number 499 as a multiplier, but it
1847 might not be large enough. */
1848 return e
->address
+ 499 * e
->sec
->id
;
1851 /* Compare the keys between two entries fo the pcrel_hi hash table. */
1854 riscv_pcrel_fixup_eq (const void *entry1
, const void *entry2
)
1856 const riscv_pcrel_hi_fixup
*e1
= entry1
, *e2
= entry2
;
1857 return e1
->sec
->id
== e2
->sec
->id
1858 && e1
->address
== e2
->address
;
1861 /* Record the pcrel_hi relocation. */
1864 riscv_record_pcrel_fixup (htab_t p
, const asection
*sec
, bfd_vma address
,
1865 symbolS
*symbol
, bfd_vma target
)
1867 riscv_pcrel_hi_fixup entry
= {sec
, address
, symbol
, target
};
1868 riscv_pcrel_hi_fixup
**slot
=
1869 (riscv_pcrel_hi_fixup
**) htab_find_slot (p
, &entry
, INSERT
);
1873 *slot
= (riscv_pcrel_hi_fixup
*) xmalloc (sizeof (riscv_pcrel_hi_fixup
));
1880 /* This function is called once, at assembler startup time. It should set up
1881 all the tables, etc. that the MD part of the assembler will need. */
1886 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
1888 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
1889 as_warn (_("could not set architecture and machine"));
1891 op_hash
= init_opcode_hash (riscv_opcodes
, false);
1892 insn_type_hash
= init_opcode_hash (riscv_insn_types
, true);
1894 reg_names_hash
= str_htab_create ();
1895 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_numeric
, NGPR
);
1896 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_abi
, NGPR
);
1897 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_numeric
, NFPR
);
1898 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_abi
, NFPR
);
1899 hash_reg_names (RCLASS_VECR
, riscv_vecr_names_numeric
, NVECR
);
1900 hash_reg_names (RCLASS_VECM
, riscv_vecm_names_numeric
, NVECM
);
1901 /* Add "fp" as an alias for "s0". */
1902 hash_reg_name (RCLASS_GPR
, "fp", 8);
1904 /* Create and insert CSR hash tables. */
1905 csr_extra_hash
= str_htab_create ();
1906 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1907 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1908 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1909 DECLARE_CSR(name, num, class, define_version, abort_version);
1910 #include "opcode/riscv-opc.h"
1913 opcode_names_hash
= str_htab_create ();
1914 init_opcode_names_hash ();
1916 /* Create pcrel_hi hash table to resolve the relocation while with
1918 riscv_pcrel_hi_fixup_hash
= htab_create (1024, riscv_pcrel_fixup_hash
,
1919 riscv_pcrel_fixup_eq
, free
);
1921 /* Set the default alignment for the text section. */
1922 record_alignment (text_section
, riscv_opts
.rvc
? 1 : 2);
1926 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type
, bfd_vma value
)
1933 case BFD_RELOC_RISCV_HI20
:
1934 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1936 case BFD_RELOC_RISCV_LO12_S
:
1937 return ENCODE_STYPE_IMM (value
);
1939 case BFD_RELOC_RISCV_LO12_I
:
1940 return ENCODE_ITYPE_IMM (value
);
1947 /* Output an instruction. IP is the instruction information.
1948 ADDRESS_EXPR is an operand of the instruction to be used with
1952 append_insn (struct riscv_cl_insn
*ip
, expressionS
*address_expr
,
1953 bfd_reloc_code_real_type reloc_type
)
1955 dwarf2_emit_insn (0);
1957 if (reloc_type
!= BFD_RELOC_UNUSED
)
1959 reloc_howto_type
*howto
;
1961 gas_assert (address_expr
);
1962 if (reloc_type
== BFD_RELOC_12_PCREL
1963 || reloc_type
== BFD_RELOC_RISCV_JMP
)
1965 int j
= reloc_type
== BFD_RELOC_RISCV_JMP
;
1966 int best_case
= insn_length (ip
);
1967 unsigned worst_case
= relaxed_branch_length (NULL
, NULL
, 0);
1969 if (now_seg
== absolute_section
)
1971 as_bad (_("relaxable branches not supported in absolute section"));
1975 add_relaxed_insn (ip
, worst_case
, best_case
,
1976 RELAX_BRANCH_ENCODE (j
, best_case
== 2, worst_case
),
1977 address_expr
->X_add_symbol
,
1978 address_expr
->X_add_number
);
1983 howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1985 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1988 ip
->fixp
= fix_new_exp (ip
->frag
, ip
->where
,
1989 bfd_get_reloc_size (howto
),
1990 address_expr
, false, reloc_type
);
1992 ip
->fixp
->fx_tcbit
= riscv_opts
.relax
;
1993 ip
->fixp
->tc_fix_data
.source_macro
= source_macro
;
1997 add_fixed_insn (ip
);
1999 /* We need to start a new frag after any instruction that can be
2000 optimized away or compressed by the linker during relaxation, to prevent
2001 the assembler from computing static offsets across such an instruction.
2002 This is necessary to get correct EH info. */
2003 if (reloc_type
== BFD_RELOC_RISCV_HI20
2004 || reloc_type
== BFD_RELOC_RISCV_PCREL_HI20
2005 || reloc_type
== BFD_RELOC_RISCV_TPREL_HI20
2006 || reloc_type
== BFD_RELOC_RISCV_TPREL_ADD
)
2008 frag_wane (frag_now
);
2013 /* Build an instruction created by a macro expansion. This is passed
2014 a pointer to the count of instructions created so far, an expression,
2015 the name of the instruction to build, an operand format string, and
2016 corresponding arguments. */
2019 macro_build (expressionS
*ep
, const char *name
, const char *fmt
, ...)
2021 const struct riscv_opcode
*mo
;
2022 struct riscv_cl_insn insn
;
2023 bfd_reloc_code_real_type r
;
2025 const char *fmtStart
;
2027 va_start (args
, fmt
);
2029 r
= BFD_RELOC_UNUSED
;
2030 mo
= (struct riscv_opcode
*) str_hash_find (op_hash
, name
);
2033 /* Find a non-RVC variant of the instruction. append_insn will compress
2035 while (riscv_insn_length (mo
->match
) < 4)
2037 gas_assert (strcmp (name
, mo
->name
) == 0);
2039 create_insn (&insn
, mo
);
2049 INSERT_OPERAND (VD
, insn
, va_arg (args
, int));
2052 INSERT_OPERAND (VS1
, insn
, va_arg (args
, int));
2055 INSERT_OPERAND (VS2
, insn
, va_arg (args
, int));
2059 int reg
= va_arg (args
, int);
2062 INSERT_OPERAND (VMASK
, insn
, 1);
2067 INSERT_OPERAND (VMASK
, insn
, 0);
2071 goto unknown_macro_argument
;
2074 goto unknown_macro_argument
;
2079 INSERT_OPERAND (RD
, insn
, va_arg (args
, int));
2082 INSERT_OPERAND (RS1
, insn
, va_arg (args
, int));
2085 INSERT_OPERAND (RS2
, insn
, va_arg (args
, int));
2091 gas_assert (ep
!= NULL
);
2092 r
= va_arg (args
, int);
2100 unknown_macro_argument
:
2101 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart
);
2106 gas_assert (r
== BFD_RELOC_UNUSED
? ep
== NULL
: ep
!= NULL
);
2108 append_insn (&insn
, ep
, r
);
2111 /* Build an instruction created by a macro expansion. Like md_assemble but
2112 accept a printf-style format string and arguments. */
2115 md_assemblef (const char *format
, ...)
2120 va_start (ap
, format
);
2122 buf
= xvasprintf (format
, ap
);
2129 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
2133 normalize_constant_expr (expressionS
*ex
)
2137 if ((ex
->X_op
== O_constant
|| ex
->X_op
== O_symbol
)
2138 && IS_ZEXT_32BIT_NUM (ex
->X_add_number
))
2139 ex
->X_add_number
= (((ex
->X_add_number
& 0xffffffff) ^ 0x80000000)
2143 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
2144 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
2147 check_absolute_expr (struct riscv_cl_insn
*ip
, expressionS
*ex
,
2150 if (ex
->X_op
== O_big
)
2151 as_bad (_("unsupported large constant"));
2152 else if (maybe_csr
&& ex
->X_op
== O_symbol
)
2153 as_bad (_("unknown CSR `%s'"),
2154 S_GET_NAME (ex
->X_add_symbol
));
2155 else if (ex
->X_op
!= O_constant
)
2156 as_bad (_("instruction %s requires absolute expression"),
2158 normalize_constant_expr (ex
);
2162 make_internal_label (void)
2164 return (symbolS
*) local_symbol_make (FAKE_LABEL_NAME
, now_seg
, frag_now
,
2168 /* Load an entry from the GOT. */
2171 pcrel_access (int destreg
, int tempreg
, expressionS
*ep
,
2172 const char *lo_insn
, const char *lo_pattern
,
2173 bfd_reloc_code_real_type hi_reloc
,
2174 bfd_reloc_code_real_type lo_reloc
)
2177 ep2
.X_op
= O_symbol
;
2178 ep2
.X_add_symbol
= make_internal_label ();
2179 ep2
.X_add_number
= 0;
2181 macro_build (ep
, "auipc", "d,u", tempreg
, hi_reloc
);
2182 macro_build (&ep2
, lo_insn
, lo_pattern
, destreg
, tempreg
, lo_reloc
);
2186 pcrel_load (int destreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
2187 bfd_reloc_code_real_type hi_reloc
,
2188 bfd_reloc_code_real_type lo_reloc
)
2190 pcrel_access (destreg
, tempreg
, ep
, lo_insn
, "d,s,j", hi_reloc
, lo_reloc
);
2194 pcrel_store (int srcreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
2195 bfd_reloc_code_real_type hi_reloc
,
2196 bfd_reloc_code_real_type lo_reloc
)
2198 pcrel_access (srcreg
, tempreg
, ep
, lo_insn
, "t,s,q", hi_reloc
, lo_reloc
);
2201 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
2204 riscv_call (int destreg
, int tempreg
, expressionS
*ep
,
2205 bfd_reloc_code_real_type reloc
)
2207 /* Ensure the jalr is emitted to the same frag as the auipc. */
2209 macro_build (ep
, "auipc", "d,u", tempreg
, reloc
);
2210 macro_build (NULL
, "jalr", "d,s", destreg
, tempreg
);
2211 /* See comment at end of append_insn. */
2212 frag_wane (frag_now
);
2216 /* Load an integer constant into a register. */
2219 load_const (int reg
, expressionS
*ep
)
2221 int shift
= RISCV_IMM_BITS
;
2222 bfd_vma upper_imm
, sign
= (bfd_vma
) 1 << (RISCV_IMM_BITS
- 1);
2223 expressionS upper
= *ep
, lower
= *ep
;
2224 lower
.X_add_number
= ((ep
->X_add_number
& (sign
+ sign
- 1)) ^ sign
) - sign
;
2225 upper
.X_add_number
-= lower
.X_add_number
;
2227 if (ep
->X_op
!= O_constant
)
2229 as_bad (_("unsupported large constant"));
2233 if (xlen
> 32 && !IS_SEXT_32BIT_NUM (ep
->X_add_number
))
2235 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
2236 while (((upper
.X_add_number
>> shift
) & 1) == 0)
2239 upper
.X_add_number
= (int64_t) upper
.X_add_number
>> shift
;
2240 load_const (reg
, &upper
);
2242 md_assemblef ("slli x%d, x%d, 0x%x", reg
, reg
, shift
);
2243 if (lower
.X_add_number
!= 0)
2244 md_assemblef ("addi x%d, x%d, %" PRId64
, reg
, reg
,
2245 (int64_t) lower
.X_add_number
);
2249 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
2252 if (upper
.X_add_number
!= 0)
2254 /* Discard low part and zero-extend upper immediate. */
2255 upper_imm
= ((uint32_t)upper
.X_add_number
>> shift
);
2257 md_assemblef ("lui x%d, 0x%" PRIx64
, reg
, (uint64_t) upper_imm
);
2261 if (lower
.X_add_number
!= 0 || hi_reg
== 0)
2262 md_assemblef ("%s x%d, x%d, %" PRId64
, ADD32_INSN
, reg
, hi_reg
,
2263 (int64_t) lower
.X_add_number
);
2267 /* Zero extend and sign extend byte/half-word/word. */
2270 riscv_ext (int destreg
, int srcreg
, unsigned shift
, bool sign
)
2272 md_assemblef ("slli x%d, x%d, %#x", destreg
, srcreg
, shift
);
2273 md_assemblef ("sr%ci x%d, x%d, %#x",
2274 sign
? 'a' : 'l', destreg
, destreg
, shift
);
2277 /* Expand RISC-V Vector macros into one or more instructions. */
2280 vector_macro (struct riscv_cl_insn
*ip
)
2282 int vd
= (ip
->insn_opcode
>> OP_SH_VD
) & OP_MASK_VD
;
2283 int vs1
= (ip
->insn_opcode
>> OP_SH_VS1
) & OP_MASK_VS1
;
2284 int vs2
= (ip
->insn_opcode
>> OP_SH_VS2
) & OP_MASK_VS2
;
2285 int vm
= (ip
->insn_opcode
>> OP_SH_VMASK
) & OP_MASK_VMASK
;
2286 int vtemp
= (ip
->insn_opcode
>> OP_SH_VFUNCT6
) & OP_MASK_VFUNCT6
;
2287 const char *vmslt_vx
= ip
->insn_mo
->match
? "vmsltu.vx" : "vmslt.vx";
2288 int mask
= ip
->insn_mo
->mask
;
2296 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, -1);
2297 macro_build (NULL
, "vmnand.mm", "Vd,Vt,Vs", vd
, vd
, vd
);
2302 /* Masked. Have vtemp to avoid overlap constraints. */
2305 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
2306 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vm
, vtemp
);
2310 /* Preserve the value of vd if not updating by vm. */
2311 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
2312 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vtemp
, vm
, vtemp
);
2313 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
2314 macro_build (NULL
, "vmor.mm", "Vd,Vt,Vs", vd
, vtemp
, vd
);
2319 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2320 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, vm
);
2321 macro_build (NULL
, "vmxor.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
2324 as_bad (_("must provide temp if destination overlaps mask"));
2332 /* Expand RISC-V assembly macros into one or more instructions. */
2335 macro (struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2336 bfd_reloc_code_real_type
*imm_reloc
)
2338 int rd
= (ip
->insn_opcode
>> OP_SH_RD
) & OP_MASK_RD
;
2339 int rs1
= (ip
->insn_opcode
>> OP_SH_RS1
) & OP_MASK_RS1
;
2340 int rs2
= (ip
->insn_opcode
>> OP_SH_RS2
) & OP_MASK_RS2
;
2341 int mask
= ip
->insn_mo
->mask
;
2343 source_macro
= mask
;
2348 load_const (rd
, imm_expr
);
2354 /* Load the address of a symbol into a register. */
2355 if (!IS_SEXT_32BIT_NUM (imm_expr
->X_add_number
))
2356 as_bad (_("offset too large"));
2358 if (imm_expr
->X_op
== O_constant
)
2359 load_const (rd
, imm_expr
);
2360 /* Global PIC symbol. */
2361 else if ((riscv_opts
.pic
&& mask
== M_LA
)
2363 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2364 BFD_RELOC_RISCV_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2365 /* Local PIC symbol, or any non-PIC symbol. */
2367 pcrel_load (rd
, rd
, imm_expr
, "addi",
2368 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2372 pcrel_load (rd
, rd
, imm_expr
, "addi",
2373 BFD_RELOC_RISCV_TLS_GD_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2377 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2378 BFD_RELOC_RISCV_TLS_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2382 pcrel_load (rd
, rd
, imm_expr
, ip
->insn_mo
->name
,
2383 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2387 pcrel_load (rd
, rs1
, imm_expr
, ip
->insn_mo
->name
,
2388 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2392 pcrel_store (rs2
, rs1
, imm_expr
, ip
->insn_mo
->name
,
2393 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2397 riscv_call (rd
, rs1
, imm_expr
, *imm_reloc
);
2401 riscv_ext (rd
, rs1
, xlen
- 16, *ip
->insn_mo
->name
== 's');
2405 riscv_ext (rd
, rs1
, xlen
- 32, false);
2409 riscv_ext (rd
, rs1
, xlen
- 8, true);
2417 as_bad (_("internal: macro %s not implemented"), ip
->insn_mo
->name
);
2424 static const struct percent_op_match percent_op_utype
[] =
2426 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20
},
2427 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20
},
2428 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20
},
2429 {"tlsdesc_hi", BFD_RELOC_RISCV_TLSDESC_HI20
},
2430 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20
},
2431 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20
},
2432 {"hi", BFD_RELOC_RISCV_HI20
},
2436 static const struct percent_op_match percent_op_itype
[] =
2438 {"lo", BFD_RELOC_RISCV_LO12_I
},
2439 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I
},
2440 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I
},
2441 {"tlsdesc_load_lo", BFD_RELOC_RISCV_TLSDESC_LOAD_LO12
},
2442 {"tlsdesc_add_lo", BFD_RELOC_RISCV_TLSDESC_ADD_LO12
},
2446 static const struct percent_op_match percent_op_stype
[] =
2448 {"lo", BFD_RELOC_RISCV_LO12_S
},
2449 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S
},
2450 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S
},
2454 static const struct percent_op_match percent_op_relax_only
[] =
2456 {"tlsdesc_call", BFD_RELOC_RISCV_TLSDESC_CALL
},
2457 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD
},
2461 static const struct percent_op_match percent_op_null
[] =
2466 /* Return true if *STR points to a relocation operator. When returning true,
2467 move *STR over the operator and store its relocation code in *RELOC.
2468 Leave both *STR and *RELOC alone when returning false. */
2471 parse_relocation (char **str
, bfd_reloc_code_real_type
*reloc
,
2472 const struct percent_op_match
*percent_op
)
2474 for ( ; percent_op
->str
; percent_op
++)
2475 if (strncasecmp (*str
+ 1, percent_op
->str
, strlen (percent_op
->str
)) == 0)
2477 size_t len
= 1 + strlen (percent_op
->str
);
2479 while (ISSPACE ((*str
)[len
]))
2481 if ((*str
)[len
] != '(')
2485 *reloc
= percent_op
->reloc
;
2487 /* Check whether the output BFD supports this relocation.
2488 If not, issue an error and fall back on something safe. */
2489 if (*reloc
!= BFD_RELOC_UNUSED
2490 && !bfd_reloc_type_lookup (stdoutput
, *reloc
))
2492 as_bad ("internal: relocation %s isn't supported by the "
2493 "current ABI", percent_op
->str
);
2494 *reloc
= BFD_RELOC_UNUSED
;
2502 my_getExpression (expressionS
*ep
, char *str
)
2506 save_in
= input_line_pointer
;
2507 input_line_pointer
= str
;
2509 expr_parse_end
= input_line_pointer
;
2510 input_line_pointer
= save_in
;
2513 /* Parse string STR as a 16-bit relocatable operand. Store the
2514 expression in *EP and the relocation, if any, in RELOC.
2515 Return the number of relocation operators used (0 or 1).
2517 On exit, EXPR_PARSE_END points to the first character after the
2521 my_getSmallExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2522 char *str
, const struct percent_op_match
*percent_op
)
2525 unsigned crux_depth
, str_depth
;
2526 bool orig_probing
= probing_insn_operands
;
2529 /* Search for the start of the main expression.
2531 End the loop with CRUX pointing to the start of the main expression and
2532 with CRUX_DEPTH containing the number of open brackets at that point. */
2539 crux_depth
= str_depth
;
2541 /* Skip over whitespace and brackets, keeping count of the number
2543 while (*str
== ' ' || *str
== '\t' || *str
== '(')
2549 && parse_relocation (&str
, reloc
, percent_op
));
2553 /* expression() will choke on anything looking like an (unrecognized)
2554 relocation specifier. Don't even call it, avoiding multiple (and
2555 perhaps redundant) error messages; our caller will issue one. */
2556 ep
->X_op
= O_illegal
;
2560 /* Anything inside parentheses or subject to a relocation operator cannot
2561 be a register and hence can be treated the same as operands to
2562 directives (other than .insn). */
2563 if (str_depth
|| reloc_index
)
2564 probing_insn_operands
= false;
2566 my_getExpression (ep
, crux
);
2567 str
= expr_parse_end
;
2569 probing_insn_operands
= orig_probing
;
2571 /* Match every open bracket. */
2572 while (crux_depth
> 0 && (*str
== ')' || *str
== ' ' || *str
== '\t'))
2577 as_bad ("unclosed '('");
2579 expr_parse_end
= str
;
2584 /* Parse opcode name, could be an mnemonics or number. */
2587 my_getOpcodeExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2590 const struct opcode_name_t
*o
= opcode_name_lookup (&str
);
2594 ep
->X_op
= O_constant
;
2595 ep
->X_add_number
= o
->val
;
2599 return my_getSmallExpression (ep
, reloc
, str
, percent_op_null
);
2602 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2603 On exit, EXPR_PARSE_END points to the first character after the
2607 my_getVsetvliExpression (expressionS
*ep
, char *str
)
2609 unsigned int vsew_value
= 0, vlmul_value
= 0;
2610 unsigned int vta_value
= 0, vma_value
= 0;
2611 bfd_boolean vsew_found
= FALSE
, vlmul_found
= FALSE
;
2612 bfd_boolean vta_found
= FALSE
, vma_found
= FALSE
;
2614 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
), &vsew_value
))
2619 as_bad (_("multiple vsew constants"));
2622 if (arg_lookup (&str
, riscv_vlmul
, ARRAY_SIZE (riscv_vlmul
), &vlmul_value
))
2627 as_bad (_("multiple vlmul constants"));
2630 if (arg_lookup (&str
, riscv_vta
, ARRAY_SIZE (riscv_vta
), &vta_value
))
2635 as_bad (_("multiple vta constants"));
2638 if (arg_lookup (&str
, riscv_vma
, ARRAY_SIZE (riscv_vma
), &vma_value
))
2643 as_bad (_("multiple vma constants"));
2647 if (vsew_found
|| vlmul_found
|| vta_found
|| vma_found
)
2649 ep
->X_op
= O_constant
;
2650 ep
->X_add_number
= (vlmul_value
<< OP_SH_VLMUL
)
2651 | (vsew_value
<< OP_SH_VSEW
)
2652 | (vta_value
<< OP_SH_VTA
)
2653 | (vma_value
<< OP_SH_VMA
);
2654 expr_parse_end
= str
;
2658 my_getExpression (ep
, str
);
2659 str
= expr_parse_end
;
2663 /* Parse string STR as a th.vsetvli operand. Store the expression in *EP.
2664 On exit, EXPR_PARSE_END points to the first character after the
2668 my_getThVsetvliExpression (expressionS
*ep
, char *str
)
2670 unsigned int vsew_value
= 0, vlen_value
= 0, vediv_value
= 0;
2671 bfd_boolean vsew_found
= FALSE
, vlen_found
= FALSE
, vediv_found
= FALSE
;
2673 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
),
2679 as_bad (_("multiple vsew constants"));
2683 if (arg_lookup (&str
, riscv_th_vlen
, ARRAY_SIZE (riscv_th_vlen
),
2689 as_bad (_("multiple vlen constants"));
2692 if (arg_lookup (&str
, riscv_th_vediv
, ARRAY_SIZE (riscv_th_vediv
),
2698 as_bad (_("multiple vediv constants"));
2702 if (vlen_found
|| vediv_found
|| vsew_found
)
2704 ep
->X_op
= O_constant
;
2706 = (vediv_value
<< 5) | (vsew_value
<< 2) | (vlen_value
);
2707 expr_parse_end
= str
;
2711 my_getExpression (ep
, str
);
2712 str
= expr_parse_end
;
2716 /* Detect and handle implicitly zero load-store offsets. For example,
2717 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2718 an implicit offset was detected. */
2721 riscv_handle_implicit_zero_offset (expressionS
*ep
, const char *s
)
2723 /* Check whether there is only a single bracketed expression left.
2724 If so, it must be the base register and the constant must be zero. */
2725 if (*s
== '(' && strchr (s
+ 1, '(') == 0)
2727 ep
->X_op
= O_constant
;
2728 ep
->X_add_number
= 0;
2735 /* All RISC-V CSR instructions belong to one of these classes. */
2744 /* Return which CSR instruction is checking. */
2746 static enum csr_insn_type
2747 riscv_csr_insn_type (insn_t insn
)
2749 if (((insn
^ MATCH_CSRRW
) & MASK_CSRRW
) == 0
2750 || ((insn
^ MATCH_CSRRWI
) & MASK_CSRRWI
) == 0)
2752 else if (((insn
^ MATCH_CSRRS
) & MASK_CSRRS
) == 0
2753 || ((insn
^ MATCH_CSRRSI
) & MASK_CSRRSI
) == 0)
2755 else if (((insn
^ MATCH_CSRRC
) & MASK_CSRRC
) == 0
2756 || ((insn
^ MATCH_CSRRCI
) & MASK_CSRRCI
) == 0)
2759 return INSN_NOT_CSR
;
2762 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2763 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2764 CSR address is 0x3. */
2767 riscv_csr_read_only_check (insn_t insn
)
2769 int csr
= (insn
& (OP_MASK_CSR
<< OP_SH_CSR
)) >> OP_SH_CSR
;
2770 int rs1
= (insn
& (OP_MASK_RS1
<< OP_SH_RS1
)) >> OP_SH_RS1
;
2771 int readonly
= (((csr
& (0x3 << 10)) >> 10) == 0x3);
2772 enum csr_insn_type csr_insn
= riscv_csr_insn_type (insn
);
2775 && (((csr_insn
== INSN_CSRRS
2776 || csr_insn
== INSN_CSRRC
)
2778 || csr_insn
== INSN_CSRRW
))
2784 /* Return true if it is a privileged instruction. Otherwise, return false.
2786 uret is actually a N-ext instruction. So it is better to regard it as
2787 an user instruction rather than the priv instruction.
2789 hret is used to return from traps in H-mode. H-mode is removed since
2790 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2791 Therefore, hret should be controlled by the hypervisor spec rather than
2792 priv spec in the future.
2794 dret is defined in the debug spec, so it should be checked in the future,
2798 riscv_is_priv_insn (insn_t insn
)
2800 return (((insn
^ MATCH_SRET
) & MASK_SRET
) == 0
2801 || ((insn
^ MATCH_MRET
) & MASK_MRET
) == 0
2802 || ((insn
^ MATCH_SFENCE_VMA
) & MASK_SFENCE_VMA
) == 0
2803 || ((insn
^ MATCH_WFI
) & MASK_WFI
) == 0
2804 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2805 check it here to keep the compatible. */
2806 || ((insn
^ MATCH_SFENCE_VM
) & MASK_SFENCE_VM
) == 0);
2809 static symbolS
*deferred_sym_rootP
;
2810 static symbolS
*deferred_sym_lastP
;
2811 /* Since symbols can't easily be freed, try to recycle ones which weren't
2813 static symbolS
*orphan_sym_rootP
;
2814 static symbolS
*orphan_sym_lastP
;
2816 /* This routine assembles an instruction into its binary format. As a
2817 side effect, it sets the global variable imm_reloc to the type of
2818 relocation to do if one of the operands is an address expression. */
2820 static struct riscv_ip_error
2821 riscv_ip (char *str
, struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2822 bfd_reloc_code_real_type
*imm_reloc
, htab_t hash
)
2824 /* The operand string defined in the riscv_opcodes. */
2825 const char *oparg
, *opargStart
;
2826 /* The parsed operands from assembly. */
2827 char *asarg
, *asargStart
;
2829 struct riscv_opcode
*insn
;
2831 const struct percent_op_match
*p
;
2832 struct riscv_ip_error error
;
2833 error
.msg
= "unrecognized opcode";
2834 error
.statement
= str
;
2835 error
.missing_ext
= NULL
;
2836 /* Indicate we are assembling instruction with CSR. */
2837 bool insn_with_csr
= false;
2839 /* Parse the name of the instruction. Terminate the string if whitespace
2840 is found so that str_hash_find only sees the name part of the string. */
2841 for (asarg
= str
; *asarg
!= '\0'; ++asarg
)
2842 if (ISSPACE (*asarg
))
2849 insn
= (struct riscv_opcode
*) str_hash_find (hash
, str
);
2851 probing_insn_operands
= true;
2854 for ( ; insn
&& insn
->name
&& strcmp (insn
->name
, str
) == 0; insn
++)
2856 if ((insn
->xlen_requirement
!= 0) && (xlen
!= insn
->xlen_requirement
))
2859 if (!riscv_multi_subset_supports (&riscv_rps_as
, insn
->insn_class
))
2861 error
.missing_ext
= riscv_multi_subset_supports_ext (&riscv_rps_as
,
2866 /* Reset error message of the previous round. */
2867 error
.msg
= _("illegal operands");
2868 error
.missing_ext
= NULL
;
2870 /* Purge deferred symbols from the previous round, if any. */
2871 while (deferred_sym_rootP
)
2873 symbolS
*sym
= deferred_sym_rootP
;
2875 symbol_remove (sym
, &deferred_sym_rootP
, &deferred_sym_lastP
);
2876 symbol_append (sym
, orphan_sym_lastP
, &orphan_sym_rootP
,
2880 create_insn (ip
, insn
);
2882 imm_expr
->X_op
= O_absent
;
2883 *imm_reloc
= BFD_RELOC_UNUSED
;
2884 p
= percent_op_null
;
2886 for (oparg
= insn
->args
;; ++oparg
)
2889 asarg
+= strspn (asarg
, " \t");
2892 case '\0': /* End of args. */
2893 if (insn
->match_func
&& !insn
->match_func (insn
, ip
->insn_opcode
))
2896 if (insn
->pinfo
!= INSN_MACRO
)
2898 /* For .insn, insn->match and insn->mask are 0. */
2899 if (riscv_insn_length ((insn
->match
== 0 && insn
->mask
== 0)
2905 if (riscv_is_priv_insn (ip
->insn_opcode
))
2906 explicit_priv_attr
= true;
2908 /* Check if we write a read-only CSR by the CSR
2911 && riscv_opts
.csr_check
2912 && !riscv_csr_read_only_check (ip
->insn_opcode
))
2914 /* Restore the character in advance, since we want to
2915 report the detailed warning message here. */
2917 *(asargStart
- 1) = save_c
;
2918 as_warn (_("read-only CSR is written `%s'"), str
);
2919 insn_with_csr
= false;
2922 /* The (segmant) load and store with EEW 64 cannot be used
2923 when zve32x is enabled. */
2924 if (ip
->insn_mo
->pinfo
& INSN_V_EEW64
2925 && riscv_subset_supports (&riscv_rps_as
, "zve32x")
2926 && !riscv_subset_supports (&riscv_rps_as
, "zve64x"))
2928 error
.msg
= _("illegal opcode for zve32x");
2935 /* Successful assembly. */
2937 insn_with_csr
= false;
2939 /* Commit deferred symbols, if any. */
2940 while (deferred_sym_rootP
)
2942 symbolS
*sym
= deferred_sym_rootP
;
2944 symbol_remove (sym
, &deferred_sym_rootP
,
2945 &deferred_sym_lastP
);
2946 symbol_append (sym
, symbol_lastP
, &symbol_rootP
,
2948 symbol_table_insert (sym
);
2955 case 's': /* RS1 x8-x15. */
2956 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2957 || !(regno
>= 8 && regno
<= 15))
2959 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2961 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2962 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2963 || EXTRACT_OPERAND (CRS1S
, ip
->insn_opcode
) + 8 != regno
)
2966 case 't': /* RS2 x8-x15. */
2967 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2968 || !(regno
>= 8 && regno
<= 15))
2970 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2972 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2973 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2974 || EXTRACT_OPERAND (CRS2S
, ip
->insn_opcode
) + 8 != regno
)
2977 case 'U': /* RS1, constrained to equal RD. */
2978 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2979 || EXTRACT_OPERAND (RD
, ip
->insn_opcode
) != regno
)
2983 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
2985 INSERT_OPERAND (CRS2
, *ip
, regno
);
2987 case 'c': /* RS1, constrained to equal sp. */
2988 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2992 case 'z': /* RS2, constrained to equal x0. */
2993 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2997 case '>': /* Shift amount, 0 - (XLEN-1). */
2998 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2999 || imm_expr
->X_op
!= O_constant
3000 || (unsigned long) imm_expr
->X_add_number
>= xlen
)
3002 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
3004 asarg
= expr_parse_end
;
3005 imm_expr
->X_op
= O_absent
;
3008 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3009 || imm_expr
->X_op
!= O_constant
3010 || imm_expr
->X_add_number
< 0
3011 || imm_expr
->X_add_number
>= 32
3012 || !VALID_CLTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3014 ip
->insn_opcode
|= ENCODE_CLTYPE_IMM (imm_expr
->X_add_number
);
3017 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3018 || imm_expr
->X_op
!= O_constant
3019 || imm_expr
->X_add_number
< 0
3020 || imm_expr
->X_add_number
>= 64
3021 || !VALID_CSSTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3023 ip
->insn_opcode
|= ENCODE_CSSTYPE_IMM (imm_expr
->X_add_number
);
3026 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3027 || imm_expr
->X_op
!= O_constant
3028 || imm_expr
->X_add_number
< 0
3029 || imm_expr
->X_add_number
>= 256
3030 || !VALID_CIWTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3032 ip
->insn_opcode
|= ENCODE_CIWTYPE_IMM (imm_expr
->X_add_number
);
3035 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3036 || imm_expr
->X_op
!= O_constant
3037 || imm_expr
->X_add_number
== 0
3038 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3040 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
3043 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3045 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3046 || imm_expr
->X_op
!= O_constant
3047 || !VALID_CLTYPE_LW_IMM ((valueT
) imm_expr
->X_add_number
))
3049 ip
->insn_opcode
|= ENCODE_CLTYPE_LW_IMM (imm_expr
->X_add_number
);
3052 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3054 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3055 || imm_expr
->X_op
!= O_constant
3056 || !VALID_CLTYPE_LD_IMM ((valueT
) imm_expr
->X_add_number
))
3058 ip
->insn_opcode
|= ENCODE_CLTYPE_LD_IMM (imm_expr
->X_add_number
);
3061 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3063 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3064 || imm_expr
->X_op
!= O_constant
3065 || !VALID_CITYPE_LWSP_IMM ((valueT
) imm_expr
->X_add_number
))
3068 ENCODE_CITYPE_LWSP_IMM (imm_expr
->X_add_number
);
3071 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3073 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3074 || imm_expr
->X_op
!= O_constant
3075 || !VALID_CITYPE_LDSP_IMM ((valueT
) imm_expr
->X_add_number
))
3078 ENCODE_CITYPE_LDSP_IMM (imm_expr
->X_add_number
);
3081 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3082 || imm_expr
->X_op
!= O_constant
3083 /* C.addiw, c.li, and c.andi allow zero immediate.
3084 C.addi allows zero immediate as hint. Otherwise this
3086 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3088 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
3091 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3092 || imm_expr
->X_op
!= O_constant
3093 || imm_expr
->X_add_number
== 0
3094 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT
) imm_expr
->X_add_number
))
3097 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr
->X_add_number
);
3100 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3101 || imm_expr
->X_op
!= O_constant
3102 || !VALID_CITYPE_ADDI16SP_IMM ((valueT
) imm_expr
->X_add_number
))
3105 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr
->X_add_number
);
3108 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3110 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3111 || imm_expr
->X_op
!= O_constant
3112 || !VALID_CSSTYPE_SWSP_IMM ((valueT
) imm_expr
->X_add_number
))
3115 ENCODE_CSSTYPE_SWSP_IMM (imm_expr
->X_add_number
);
3118 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3120 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3121 || imm_expr
->X_op
!= O_constant
3122 || !VALID_CSSTYPE_SDSP_IMM ((valueT
) imm_expr
->X_add_number
))
3125 ENCODE_CSSTYPE_SDSP_IMM (imm_expr
->X_add_number
);
3128 p
= percent_op_utype
;
3129 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3132 if (imm_expr
->X_op
!= O_constant
3133 || imm_expr
->X_add_number
<= 0
3134 || imm_expr
->X_add_number
>= RISCV_BIGIMM_REACH
3135 || (imm_expr
->X_add_number
>= RISCV_RVC_IMM_REACH
/ 2
3136 && (imm_expr
->X_add_number
<
3137 RISCV_BIGIMM_REACH
- RISCV_RVC_IMM_REACH
/ 2)))
3139 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
3142 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3143 || (imm_expr
->X_add_number
& (RISCV_IMM_REACH
- 1))
3144 || ((int32_t)imm_expr
->X_add_number
3145 != imm_expr
->X_add_number
))
3147 imm_expr
->X_add_number
=
3148 ((uint32_t) imm_expr
->X_add_number
) >> RISCV_IMM_BITS
;
3154 case 'S': /* Floating-point RS1 x8-x15. */
3155 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
3156 || !(regno
>= 8 && regno
<= 15))
3158 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
3160 case 'D': /* Floating-point RS2 x8-x15. */
3161 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
3162 || !(regno
>= 8 && regno
<= 15))
3164 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
3166 case 'T': /* Floating-point RS2. */
3167 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
))
3169 INSERT_OPERAND (CRS2
, *ip
, regno
);
3175 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3176 || imm_expr
->X_op
!= O_constant
3177 || imm_expr
->X_add_number
< 0
3178 || imm_expr
->X_add_number
>= 64)
3180 as_bad (_("bad value for compressed funct6 "
3181 "field, value must be 0...63"));
3184 INSERT_OPERAND (CFUNCT6
, *ip
, imm_expr
->X_add_number
);
3185 imm_expr
->X_op
= O_absent
;
3186 asarg
= expr_parse_end
;
3190 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3191 || imm_expr
->X_op
!= O_constant
3192 || imm_expr
->X_add_number
< 0
3193 || imm_expr
->X_add_number
>= 16)
3195 as_bad (_("bad value for compressed funct4 "
3196 "field, value must be 0...15"));
3199 INSERT_OPERAND (CFUNCT4
, *ip
, imm_expr
->X_add_number
);
3200 imm_expr
->X_op
= O_absent
;
3201 asarg
= expr_parse_end
;
3205 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3206 || imm_expr
->X_op
!= O_constant
3207 || imm_expr
->X_add_number
< 0
3208 || imm_expr
->X_add_number
>= 8)
3210 as_bad (_("bad value for compressed funct3 "
3211 "field, value must be 0...7"));
3214 INSERT_OPERAND (CFUNCT3
, *ip
, imm_expr
->X_add_number
);
3215 imm_expr
->X_op
= O_absent
;
3216 asarg
= expr_parse_end
;
3220 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3221 || imm_expr
->X_op
!= O_constant
3222 || imm_expr
->X_add_number
< 0
3223 || imm_expr
->X_add_number
>= 4)
3225 as_bad (_("bad value for compressed funct2 "
3226 "field, value must be 0...3"));
3229 INSERT_OPERAND (CFUNCT2
, *ip
, imm_expr
->X_add_number
);
3230 imm_expr
->X_op
= O_absent
;
3231 asarg
= expr_parse_end
;
3235 goto unknown_riscv_ip_operand
;
3240 goto unknown_riscv_ip_operand
;
3242 break; /* end RVC */
3248 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3250 INSERT_OPERAND (VD
, *ip
, regno
);
3253 case 'e': /* AMO VD */
3254 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
) && regno
== 0)
3255 INSERT_OPERAND (VWD
, *ip
, 0);
3256 else if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3258 INSERT_OPERAND (VWD
, *ip
, 1);
3259 INSERT_OPERAND (VD
, *ip
, regno
);
3265 case 'f': /* AMO VS3 */
3266 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3268 if (!EXTRACT_OPERAND (VWD
, ip
->insn_opcode
))
3269 INSERT_OPERAND (VD
, *ip
, regno
);
3272 /* VS3 must match VD. */
3273 if (EXTRACT_OPERAND (VD
, ip
->insn_opcode
) != regno
)
3279 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3281 INSERT_OPERAND (VS1
, *ip
, regno
);
3285 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3287 INSERT_OPERAND (VS2
, *ip
, regno
);
3290 case 'u': /* VS1 == VS2 */
3291 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3293 INSERT_OPERAND (VS1
, *ip
, regno
);
3294 INSERT_OPERAND (VS2
, *ip
, regno
);
3297 case 'v': /* VD == VS1 == VS2 */
3298 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3300 INSERT_OPERAND (VD
, *ip
, regno
);
3301 INSERT_OPERAND (VS1
, *ip
, regno
);
3302 INSERT_OPERAND (VS2
, *ip
, regno
);
3305 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
3306 and is used to choose vs1/rs1/frs1/imm or vs2 for
3307 v[f]merge. It use the same encoding as the vector mask
3310 if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
) && regno
== 0)
3314 case 'b': /* vtypei for vsetivli */
3315 my_getVsetvliExpression (imm_expr
, asarg
);
3316 check_absolute_expr (ip
, imm_expr
, FALSE
);
3317 if (!VALID_RVV_VB_IMM (imm_expr
->X_add_number
))
3318 as_bad (_("bad value for vsetivli immediate field, "
3319 "value must be 0..1023"));
3321 |= ENCODE_RVV_VB_IMM (imm_expr
->X_add_number
);
3322 imm_expr
->X_op
= O_absent
;
3323 asarg
= expr_parse_end
;
3326 case 'c': /* vtypei for vsetvli */
3327 my_getVsetvliExpression (imm_expr
, asarg
);
3328 check_absolute_expr (ip
, imm_expr
, FALSE
);
3329 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
3330 as_bad (_("bad value for vsetvli immediate field, "
3331 "value must be 0..2047"));
3333 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
3334 imm_expr
->X_op
= O_absent
;
3335 asarg
= expr_parse_end
;
3338 case 'i': /* vector arith signed immediate */
3339 my_getExpression (imm_expr
, asarg
);
3340 check_absolute_expr (ip
, imm_expr
, FALSE
);
3341 if (imm_expr
->X_add_number
> 15
3342 || imm_expr
->X_add_number
< -16)
3343 as_bad (_("bad value for vector immediate field, "
3344 "value must be -16...15"));
3345 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3346 imm_expr
->X_op
= O_absent
;
3347 asarg
= expr_parse_end
;
3350 case 'j': /* vector arith unsigned immediate */
3351 my_getExpression (imm_expr
, asarg
);
3352 check_absolute_expr (ip
, imm_expr
, FALSE
);
3353 if (imm_expr
->X_add_number
< 0
3354 || imm_expr
->X_add_number
>= 32)
3355 as_bad (_("bad value for vector immediate field, "
3356 "value must be 0...31"));
3357 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3358 imm_expr
->X_op
= O_absent
;
3359 asarg
= expr_parse_end
;
3362 case 'k': /* vector arith signed immediate, minus 1 */
3363 my_getExpression (imm_expr
, asarg
);
3364 check_absolute_expr (ip
, imm_expr
, FALSE
);
3365 if (imm_expr
->X_add_number
> 16
3366 || imm_expr
->X_add_number
< -15)
3367 as_bad (_("bad value for vector immediate field, "
3368 "value must be -15...16"));
3369 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
- 1);
3370 imm_expr
->X_op
= O_absent
;
3371 asarg
= expr_parse_end
;
3374 case 'l': /* 6-bit vector arith unsigned immediate */
3375 my_getExpression (imm_expr
, asarg
);
3376 check_absolute_expr (ip
, imm_expr
, FALSE
);
3377 if (imm_expr
->X_add_number
< 0
3378 || imm_expr
->X_add_number
>= 64)
3379 as_bad (_("bad value for vector immediate field, "
3380 "value must be 0...63"));
3381 ip
->insn_opcode
|= ENCODE_RVV_VI_UIMM6 (imm_expr
->X_add_number
);
3382 imm_expr
->X_op
= O_absent
;
3383 asarg
= expr_parse_end
;
3386 case 'm': /* optional vector mask */
3389 INSERT_OPERAND (VMASK
, *ip
, 1);
3392 else if (*asarg
== ',' && asarg
++
3393 && reg_lookup (&asarg
, RCLASS_VECM
, ®no
)
3396 INSERT_OPERAND (VMASK
, *ip
, 0);
3401 case 'M': /* required vector mask */
3402 if (reg_lookup (&asarg
, RCLASS_VECM
, ®no
) && regno
== 0)
3404 INSERT_OPERAND (VMASK
, *ip
, 0);
3409 case 'T': /* vector macro temporary register */
3410 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
) || regno
== 0)
3412 /* Store it in the FUNCT6 field as we don't have anyplace
3413 else to store it. */
3414 INSERT_OPERAND (VFUNCT6
, *ip
, regno
);
3418 goto unknown_riscv_ip_operand
;
3420 break; /* end RVV */
3423 if (*asarg
++ == *oparg
)
3434 if (*asarg
++ == *oparg
)
3438 case '<': /* Shift amount, 0 - 31. */
3439 my_getExpression (imm_expr
, asarg
);
3440 check_absolute_expr (ip
, imm_expr
, false);
3441 if ((unsigned long) imm_expr
->X_add_number
> 31)
3442 as_bad (_("improper shift amount (%"PRIu64
")"),
3443 imm_expr
->X_add_number
);
3444 INSERT_OPERAND (SHAMTW
, *ip
, imm_expr
->X_add_number
);
3445 imm_expr
->X_op
= O_absent
;
3446 asarg
= expr_parse_end
;
3449 case '>': /* Shift amount, 0 - (XLEN-1). */
3450 my_getExpression (imm_expr
, asarg
);
3451 check_absolute_expr (ip
, imm_expr
, false);
3452 if ((unsigned long) imm_expr
->X_add_number
>= xlen
)
3453 as_bad (_("improper shift amount (%"PRIu64
")"),
3454 imm_expr
->X_add_number
);
3455 INSERT_OPERAND (SHAMT
, *ip
, imm_expr
->X_add_number
);
3456 imm_expr
->X_op
= O_absent
;
3457 asarg
= expr_parse_end
;
3460 case 'Z': /* CSRRxI immediate. */
3461 my_getExpression (imm_expr
, asarg
);
3462 check_absolute_expr (ip
, imm_expr
, false);
3463 if ((unsigned long) imm_expr
->X_add_number
> 31)
3464 as_bad (_("improper CSRxI immediate (%"PRIu64
")"),
3465 imm_expr
->X_add_number
);
3466 INSERT_OPERAND (RS1
, *ip
, imm_expr
->X_add_number
);
3467 imm_expr
->X_op
= O_absent
;
3468 asarg
= expr_parse_end
;
3471 case 'E': /* Control register. */
3472 insn_with_csr
= true;
3473 explicit_priv_attr
= true;
3474 if (reg_lookup (&asarg
, RCLASS_CSR
, ®no
))
3475 INSERT_OPERAND (CSR
, *ip
, regno
);
3478 my_getExpression (imm_expr
, asarg
);
3479 check_absolute_expr (ip
, imm_expr
, true);
3480 if ((unsigned long) imm_expr
->X_add_number
> 0xfff)
3481 as_bad (_("improper CSR address (%"PRIu64
")"),
3482 imm_expr
->X_add_number
);
3483 INSERT_OPERAND (CSR
, *ip
, imm_expr
->X_add_number
);
3484 imm_expr
->X_op
= O_absent
;
3485 asarg
= expr_parse_end
;
3489 case 'm': /* Rounding mode. */
3490 if (arg_lookup (&asarg
, riscv_rm
,
3491 ARRAY_SIZE (riscv_rm
), ®no
))
3493 INSERT_OPERAND (RM
, *ip
, regno
);
3499 case 'Q': /* Fence predecessor/successor. */
3500 if (arg_lookup (&asarg
, riscv_pred_succ
,
3501 ARRAY_SIZE (riscv_pred_succ
), ®no
))
3504 INSERT_OPERAND (PRED
, *ip
, regno
);
3506 INSERT_OPERAND (SUCC
, *ip
, regno
);
3511 case 'd': /* Destination register. */
3512 case 's': /* Source register. */
3513 case 't': /* Target register. */
3515 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
3521 /* Now that we have assembled one operand, we use the args
3522 string to figure out where it goes in the instruction. */
3526 INSERT_OPERAND (RS1
, *ip
, regno
);
3529 INSERT_OPERAND (RD
, *ip
, regno
);
3532 INSERT_OPERAND (RS2
, *ip
, regno
);
3535 INSERT_OPERAND (RS3
, *ip
, regno
);
3542 case 'D': /* Floating point RD. */
3543 case 'S': /* Floating point RS1. */
3544 case 'T': /* Floating point RS2. */
3545 case 'U': /* Floating point RS1 and RS2. */
3546 case 'R': /* Floating point RS3. */
3547 if (reg_lookup (&asarg
,
3548 (riscv_subset_supports (&riscv_rps_as
, "zfinx")
3549 ? RCLASS_GPR
: RCLASS_FPR
), ®no
))
3557 INSERT_OPERAND (RD
, *ip
, regno
);
3560 INSERT_OPERAND (RS1
, *ip
, regno
);
3563 INSERT_OPERAND (RS1
, *ip
, regno
);
3566 INSERT_OPERAND (RS2
, *ip
, regno
);
3569 INSERT_OPERAND (RS3
, *ip
, regno
);
3577 my_getExpression (imm_expr
, asarg
);
3578 if (imm_expr
->X_op
!= O_big
3579 && imm_expr
->X_op
!= O_constant
)
3581 normalize_constant_expr (imm_expr
);
3582 asarg
= expr_parse_end
;
3586 my_getExpression (imm_expr
, asarg
);
3587 normalize_constant_expr (imm_expr
);
3588 /* The 'A' format specifier must be a symbol. */
3589 if (imm_expr
->X_op
!= O_symbol
)
3591 *imm_reloc
= BFD_RELOC_32
;
3592 asarg
= expr_parse_end
;
3596 my_getExpression (imm_expr
, asarg
);
3597 normalize_constant_expr (imm_expr
);
3598 /* The 'B' format specifier must be a symbol or a constant. */
3599 if (imm_expr
->X_op
!= O_symbol
&& imm_expr
->X_op
!= O_constant
)
3601 if (imm_expr
->X_op
== O_symbol
)
3602 *imm_reloc
= BFD_RELOC_32
;
3603 asarg
= expr_parse_end
;
3606 case 'j': /* Sign-extended immediate. */
3607 p
= percent_op_itype
;
3608 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3610 case 'q': /* Store displacement. */
3611 p
= percent_op_stype
;
3612 *imm_reloc
= BFD_RELOC_RISCV_LO12_S
;
3614 case 'o': /* Load displacement. */
3615 p
= percent_op_itype
;
3616 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3619 /* This is used for TLS relocations that acts as relaxation
3620 markers and do not change the instruction encoding,
3621 i.e. %tprel_add and %tlsdesc_call. */
3622 p
= percent_op_relax_only
;
3624 case '0': /* AMO displacement, which must be zero. */
3626 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3629 /* If this value won't fit into a 16 bit offset, then go
3630 find a macro that will generate the 32 bit offset
3632 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3634 normalize_constant_expr (imm_expr
);
3635 if (imm_expr
->X_op
!= O_constant
3636 || (*oparg
== '0' && imm_expr
->X_add_number
!= 0)
3638 || imm_expr
->X_add_number
>= (signed)RISCV_IMM_REACH
/2
3639 || imm_expr
->X_add_number
< -(signed)RISCV_IMM_REACH
/2)
3642 asarg
= expr_parse_end
;
3645 case 'p': /* PC-relative offset. */
3647 *imm_reloc
= BFD_RELOC_12_PCREL
;
3648 my_getExpression (imm_expr
, asarg
);
3649 asarg
= expr_parse_end
;
3652 case 'u': /* Upper 20 bits. */
3653 p
= percent_op_utype
;
3654 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3656 if (imm_expr
->X_op
!= O_constant
)
3659 if (imm_expr
->X_add_number
< 0
3660 || imm_expr
->X_add_number
>= (signed)RISCV_BIGIMM_REACH
)
3661 as_bad (_("lui expression not in range 0..1048575"));
3663 *imm_reloc
= BFD_RELOC_RISCV_HI20
;
3664 imm_expr
->X_add_number
<<= RISCV_IMM_BITS
;
3666 asarg
= expr_parse_end
;
3669 case 'a': /* 20-bit PC-relative offset. */
3671 my_getExpression (imm_expr
, asarg
);
3672 asarg
= expr_parse_end
;
3673 *imm_reloc
= BFD_RELOC_RISCV_JMP
;
3677 my_getExpression (imm_expr
, asarg
);
3678 asarg
= expr_parse_end
;
3679 if (strcmp (asarg
, "@plt") == 0)
3681 *imm_reloc
= BFD_RELOC_RISCV_CALL_PLT
;
3688 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3689 || imm_expr
->X_op
!= O_constant
3690 || imm_expr
->X_add_number
< 0
3691 || imm_expr
->X_add_number
>= 128
3692 || (imm_expr
->X_add_number
& 0x3) != 3)
3694 as_bad (_("bad value for opcode field, "
3695 "value must be 0...127 and "
3696 "lower 2 bits must be 0x3"));
3699 INSERT_OPERAND (OP
, *ip
, imm_expr
->X_add_number
);
3700 imm_expr
->X_op
= O_absent
;
3701 asarg
= expr_parse_end
;
3705 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3706 || imm_expr
->X_op
!= O_constant
3707 || imm_expr
->X_add_number
< 0
3708 || imm_expr
->X_add_number
>= 3)
3710 as_bad (_("bad value for opcode field, "
3711 "value must be 0...2"));
3714 INSERT_OPERAND (OP2
, *ip
, imm_expr
->X_add_number
);
3715 imm_expr
->X_op
= O_absent
;
3716 asarg
= expr_parse_end
;
3720 goto unknown_riscv_ip_operand
;
3728 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3729 || imm_expr
->X_op
!= O_constant
3730 || imm_expr
->X_add_number
< 0
3731 || imm_expr
->X_add_number
>= 128)
3733 as_bad (_("bad value for funct7 field, "
3734 "value must be 0...127"));
3737 INSERT_OPERAND (FUNCT7
, *ip
, imm_expr
->X_add_number
);
3738 imm_expr
->X_op
= O_absent
;
3739 asarg
= expr_parse_end
;
3743 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3744 || imm_expr
->X_op
!= O_constant
3745 || imm_expr
->X_add_number
< 0
3746 || imm_expr
->X_add_number
>= 8)
3748 as_bad (_("bad value for funct3 field, "
3749 "value must be 0...7"));
3752 INSERT_OPERAND (FUNCT3
, *ip
, imm_expr
->X_add_number
);
3753 imm_expr
->X_op
= O_absent
;
3754 asarg
= expr_parse_end
;
3758 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3759 || imm_expr
->X_op
!= O_constant
3760 || imm_expr
->X_add_number
< 0
3761 || imm_expr
->X_add_number
>= 4)
3763 as_bad (_("bad value for funct2 field, "
3764 "value must be 0...3"));
3767 INSERT_OPERAND (FUNCT2
, *ip
, imm_expr
->X_add_number
);
3768 imm_expr
->X_op
= O_absent
;
3769 asarg
= expr_parse_end
;
3773 goto unknown_riscv_ip_operand
;
3777 case 'y': /* bs immediate */
3778 my_getExpression (imm_expr
, asarg
);
3779 check_absolute_expr (ip
, imm_expr
, FALSE
);
3780 if ((unsigned long)imm_expr
->X_add_number
> 3)
3781 as_bad(_("Improper bs immediate (%lu)"),
3782 (unsigned long)imm_expr
->X_add_number
);
3783 INSERT_OPERAND(BS
, *ip
, imm_expr
->X_add_number
);
3784 imm_expr
->X_op
= O_absent
;
3785 asarg
= expr_parse_end
;
3788 case 'Y': /* rnum immediate */
3789 my_getExpression (imm_expr
, asarg
);
3790 check_absolute_expr (ip
, imm_expr
, FALSE
);
3791 if ((unsigned long)imm_expr
->X_add_number
> 10)
3792 as_bad(_("Improper rnum immediate (%lu)"),
3793 (unsigned long)imm_expr
->X_add_number
);
3794 INSERT_OPERAND(RNUM
, *ip
, imm_expr
->X_add_number
);
3795 imm_expr
->X_op
= O_absent
;
3796 asarg
= expr_parse_end
;
3800 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3801 || imm_expr
->X_op
!= O_constant
3802 || imm_expr
->X_add_number
!= 0)
3804 asarg
= expr_parse_end
;
3805 imm_expr
->X_op
= O_absent
;
3808 case 'W': /* Various operands for standard z extensions. */
3815 /* Prefetch offset for 'Zicbop' extension.
3816 pseudo S-type but lower 5-bits zero. */
3817 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3819 my_getExpression (imm_expr
, asarg
);
3820 check_absolute_expr (ip
, imm_expr
, false);
3821 if (((unsigned) (imm_expr
->X_add_number
) & 0x1fU
)
3822 || imm_expr
->X_add_number
>= RISCV_IMM_REACH
/ 2
3823 || imm_expr
->X_add_number
< -RISCV_IMM_REACH
/ 2)
3824 as_bad (_ ("improper prefetch offset (%ld)"),
3825 (long) imm_expr
->X_add_number
);
3826 ip
->insn_opcode
|= ENCODE_STYPE_IMM (
3827 (unsigned) (imm_expr
->X_add_number
) & ~0x1fU
);
3828 imm_expr
->X_op
= O_absent
;
3829 asarg
= expr_parse_end
;
3832 goto unknown_riscv_ip_operand
;
3840 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3841 if (!arg_lookup (&asarg
, riscv_fli_symval
,
3842 ARRAY_SIZE (riscv_fli_symval
), ®no
))
3844 /* 0.0 is not a valid entry in riscv_fli_numval. */
3846 float f
= strtof (asarg
, &asarg
);
3847 if (errno
!= 0 || f
== 0.0
3848 || !flt_lookup (f
, riscv_fli_numval
,
3849 ARRAY_SIZE(riscv_fli_numval
),
3852 as_bad (_("bad fli constant operand, "
3853 "supported constants must be in "
3854 "decimal or hexadecimal floating-point "
3859 INSERT_OPERAND (RS1
, *ip
, regno
);
3862 goto unknown_riscv_ip_operand
;
3869 case 'h': /* Immediate field for c.lh/c.lhu/c.sh. */
3870 /* Handle cases, such as c.sh rs2', (rs1'). */
3871 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3873 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3874 || imm_expr
->X_op
!= O_constant
3875 || !VALID_ZCB_HALFWORD_UIMM ((valueT
) imm_expr
->X_add_number
))
3877 ip
->insn_opcode
|= ENCODE_ZCB_HALFWORD_UIMM (imm_expr
->X_add_number
);
3879 case 'b': /* Immediate field for c.lbu/c.sb. */
3880 /* Handle cases, such as c.lbu rd', (rs1'). */
3881 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3883 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3884 || imm_expr
->X_op
!= O_constant
3885 || !VALID_ZCB_BYTE_UIMM ((valueT
) imm_expr
->X_add_number
))
3887 ip
->insn_opcode
|= ENCODE_ZCB_BYTE_UIMM (imm_expr
->X_add_number
);
3890 if (!reglist_lookup (&asarg
, ®no
))
3892 INSERT_OPERAND (REG_LIST
, *ip
, regno
);
3895 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3896 || imm_expr
->X_op
!= O_constant
)
3898 /* Convert stack adjustment of cm.push to a positive
3900 if (ip
->insn_mo
->match
== MATCH_CM_PUSH
)
3901 imm_expr
->X_add_number
*= -1;
3902 /* Subtract base stack adjustment and get spimm. */
3903 imm_expr
->X_add_number
-=
3904 riscv_get_sp_base (ip
->insn_opcode
, *riscv_rps_as
.xlen
);
3905 if (!VALID_ZCMP_SPIMM (imm_expr
->X_add_number
))
3908 ENCODE_ZCMP_SPIMM (imm_expr
->X_add_number
);
3910 case 'f': /* Operand for matching immediate 255. */
3911 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3912 || imm_expr
->X_op
!= O_constant
3913 || imm_expr
->X_add_number
!= 255)
3915 /* This operand is used for matching immediate 255, and
3916 we do not write anything to encoding by this operand. */
3917 asarg
= expr_parse_end
;
3918 imm_expr
->X_op
= O_absent
;
3921 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
3922 || !RISCV_SREG_0_7 (regno
))
3924 INSERT_OPERAND (SREG1
, *ip
, regno
% 8);
3927 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
3928 || !RISCV_SREG_0_7 (regno
))
3930 INSERT_OPERAND (SREG2
, *ip
, regno
% 8);
3932 case 'I': /* index operand of cm.jt. The range is from 0 to 31. */
3933 my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
);
3934 if (imm_expr
->X_op
!= O_constant
3935 || imm_expr
->X_add_number
< 0
3936 || imm_expr
->X_add_number
> 31)
3938 as_bad ("bad index value for cm.jt, range: [0, 31]");
3941 ip
->insn_opcode
|= ENCODE_ZCMT_INDEX (imm_expr
->X_add_number
);
3943 case 'i': /* index operand of cm.jalt. The range is from 32 to 255. */
3944 my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
);
3945 if (imm_expr
->X_op
!= O_constant
3946 || imm_expr
->X_add_number
< 32
3947 || imm_expr
->X_add_number
> 255)
3949 as_bad ("bad index value for cm.jalt, range: [32, 255]");
3952 ip
->insn_opcode
|= ENCODE_ZCMT_INDEX (imm_expr
->X_add_number
);
3955 goto unknown_riscv_ip_operand
;
3960 goto unknown_riscv_ip_operand
;
3964 case 'X': /* Vendor-specific operands. */
3967 case 't': /* Vendor-specific (T-head) operands. */
3975 /* Vtypei for th.vsetvli. */
3978 goto unknown_riscv_ip_operand
;
3980 my_getThVsetvliExpression (imm_expr
, asarg
);
3981 check_absolute_expr (ip
, imm_expr
, FALSE
);
3982 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
3983 as_bad (_("bad value for th.vsetvli immediate field, "
3984 "value must be 0..2047"));
3986 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
3987 imm_expr
->X_op
= O_absent
;
3988 asarg
= expr_parse_end
;
3991 case 'l': /* Integer immediate, literal. */
3992 n
= strcspn (++oparg
, ",");
3993 if (strncmp (oparg
, asarg
, n
))
3994 as_bad (_("unexpected literal (%s)"), asarg
);
3998 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
4001 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
4005 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
4007 goto unknown_riscv_ip_operand
;
4008 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
4011 my_getExpression (imm_expr
, asarg
);
4012 check_absolute_expr (ip
, imm_expr
, false);
4015 if (!VALIDATE_U_IMM (imm_expr
->X_add_number
, n
))
4016 as_bad (_("improper immediate value (%"PRIu64
")"),
4017 imm_expr
->X_add_number
);
4021 if (!VALIDATE_S_IMM (imm_expr
->X_add_number
, n
))
4022 as_bad (_("improper immediate value (%"PRIi64
")"),
4023 imm_expr
->X_add_number
);
4025 INSERT_IMM (n
, s
, *ip
, imm_expr
->X_add_number
);
4026 imm_expr
->X_op
= O_absent
;
4027 asarg
= expr_parse_end
;
4030 goto unknown_riscv_ip_operand
;
4035 case 'c': /* Vendor-specific (CORE-V) operands. */
4039 my_getExpression (imm_expr
, asarg
);
4040 check_absolute_expr (ip
, imm_expr
, FALSE
);
4041 asarg
= expr_parse_end
;
4042 if (imm_expr
->X_add_number
<0
4043 || imm_expr
->X_add_number
>31)
4046 |= ENCODE_CV_IS2_UIMM5 (imm_expr
->X_add_number
);
4049 my_getExpression (imm_expr
, asarg
);
4050 check_absolute_expr (ip
, imm_expr
, FALSE
);
4051 asarg
= expr_parse_end
;
4052 if (imm_expr
->X_add_number
< 0
4053 || imm_expr
->X_add_number
> 31)
4056 |= ENCODE_CV_IS3_UIMM5 (imm_expr
->X_add_number
);
4059 my_getExpression (imm_expr
, asarg
);
4060 check_absolute_expr (ip
, imm_expr
, FALSE
);
4061 asarg
= expr_parse_end
;
4062 if (imm_expr
->X_add_number
< -16
4063 || imm_expr
->X_add_number
> 15)
4066 |= ENCODE_CV_IS2_UIMM5 (imm_expr
->X_add_number
);
4069 my_getExpression (imm_expr
, asarg
);
4070 check_absolute_expr (ip
, imm_expr
, FALSE
);
4071 asarg
= expr_parse_end
;
4072 if (imm_expr
->X_add_number
< -32
4073 || imm_expr
->X_add_number
> 31)
4076 |= ENCODE_CV_SIMD_IMM6 (imm_expr
->X_add_number
);
4079 my_getExpression (imm_expr
, asarg
);
4080 check_absolute_expr (ip
, imm_expr
, FALSE
);
4081 asarg
= expr_parse_end
;
4082 if (imm_expr
->X_add_number
< 0
4083 || imm_expr
->X_add_number
> 31)
4086 |= ENCODE_CV_BITMANIP_UIMM5 (imm_expr
->X_add_number
);
4089 my_getExpression (imm_expr
, asarg
);
4090 check_absolute_expr (ip
, imm_expr
, FALSE
);
4091 asarg
= expr_parse_end
;
4092 if (imm_expr
->X_add_number
< 0
4093 || imm_expr
->X_add_number
> 3)
4096 |= ENCODE_CV_BITMANIP_UIMM2 (imm_expr
->X_add_number
);
4099 my_getExpression (imm_expr
, asarg
);
4100 check_absolute_expr (ip
, imm_expr
, FALSE
);
4101 asarg
= expr_parse_end
;
4103 if (imm_expr
->X_add_number
< 0
4104 || imm_expr
->X_add_number
> 63)
4106 else if (*oparg
== '1'
4107 && imm_expr
->X_add_number
> 1)
4109 else if (*oparg
== '2'
4110 && imm_expr
->X_add_number
> 3)
4112 else if (*oparg
== '3'
4113 && imm_expr
->X_add_number
> 7)
4115 else if (*oparg
== '4'
4116 && imm_expr
->X_add_number
> 15)
4119 |= ENCODE_CV_SIMD_UIMM6 (imm_expr
->X_add_number
);
4122 goto unknown_riscv_ip_operand
;
4126 case 's': /* Vendor-specific (SiFive) operands. */
4127 #define UIMM_BITFIELD_VAL(S, E) (1 << ((E) - (S) + 1))
4128 #define ENCODE_UIMM_BIT_FIELD(NAME, IP, EXPR, RELOC, ASARG, \
4132 if (my_getOpcodeExpression (EXPR, RELOC, ASARG) \
4133 || EXPR->X_op != O_constant \
4134 || EXPR->X_add_number < 0 \
4135 || EXPR->X_add_number >= UIMM_BITFIELD_VAL (START, END)) \
4137 as_bad (_("bad value for <bit-%s-%s> " \
4138 "field, value must be 0...%d"), \
4139 #START, #END, UIMM_BITFIELD_VAL (START, END)); \
4142 INSERT_OPERAND (NAME, *IP, EXPR->X_add_number); \
4143 EXPR->X_op = O_absent; \
4144 ASARG = expr_parse_end; \
4150 ENCODE_UIMM_BIT_FIELD
4151 (RD
, ip
, imm_expr
, imm_reloc
, asarg
, 7, 11);
4154 ENCODE_UIMM_BIT_FIELD
4155 (RS2
, ip
, imm_expr
, imm_reloc
, asarg
, 20, 24)
4160 case '2': /* XsO2 */
4161 ENCODE_UIMM_BIT_FIELD
4162 (XSO2
, ip
, imm_expr
, imm_reloc
, asarg
, 26, 27);
4164 case '1': /* XsO1 */
4165 ENCODE_UIMM_BIT_FIELD
4166 (XSO1
, ip
, imm_expr
, imm_reloc
, asarg
, 26, 26);
4170 goto unknown_riscv_ip_operand
;
4172 #undef UIMM_BITFIELD_VAL
4173 #undef ENCODE_UIMM_BIT_FIELD
4177 goto unknown_riscv_ip_operand
;
4182 unknown_riscv_ip_operand
:
4183 as_fatal (_("internal: unknown argument type `%s'"),
4189 insn_with_csr
= false;
4193 /* Restore the character we might have clobbered above. */
4195 *(asargStart
- 1) = save_c
;
4197 probing_insn_operands
= false;
4202 /* Similar to riscv_ip, but assembles an instruction according to the
4203 hardcode values of .insn directive. */
4206 riscv_ip_hardcode (char *str
,
4207 struct riscv_cl_insn
*ip
,
4208 expressionS
*imm_expr
,
4211 struct riscv_opcode
*insn
;
4212 insn_t values
[2] = {0, 0};
4213 unsigned int num
= 0;
4215 input_line_pointer
= str
;
4218 expression (imm_expr
);
4219 switch (imm_expr
->X_op
)
4222 values
[num
++] = (insn_t
) imm_expr
->X_add_number
;
4225 /* Extract lower 32-bits of a big number.
4226 Assume that generic_bignum_to_int32 work on such number. */
4227 values
[num
++] = (insn_t
) generic_bignum_to_int32 ();
4230 /* The first value isn't constant, so it should be
4231 .insn <type> <operands>. We have been parsed it
4235 return _("values must be constant");
4238 while (*input_line_pointer
++ == ',' && num
< 2 && imm_expr
->X_op
!= O_big
);
4240 input_line_pointer
--;
4241 if (*input_line_pointer
!= '\0')
4242 return _("unrecognized values");
4244 insn
= XCNEW (struct riscv_opcode
);
4245 insn
->match
= values
[num
- 1];
4246 create_insn (ip
, insn
);
4247 unsigned int bytes
= riscv_insn_length (insn
->match
);
4249 if (num
== 2 && values
[0] != bytes
)
4250 return _("value conflicts with instruction length");
4252 if (imm_expr
->X_op
== O_big
)
4254 unsigned int llen
= 0;
4255 for (LITTLENUM_TYPE lval
= generic_bignum
[imm_expr
->X_add_number
- 1];
4257 lval
>>= BITS_PER_CHAR
;
4258 unsigned int repr_bytes
4259 = (imm_expr
->X_add_number
- 1) * CHARS_PER_LITTLENUM
+ llen
;
4260 if (bytes
< repr_bytes
)
4261 return _("value conflicts with instruction length");
4262 for (num
= 0; num
< imm_expr
->X_add_number
- 1; ++num
)
4263 number_to_chars_littleendian (
4264 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
4265 generic_bignum
[num
],
4266 CHARS_PER_LITTLENUM
);
4268 number_to_chars_littleendian (
4269 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
4270 generic_bignum
[num
],
4272 memset(ip
->insn_long_opcode
+ repr_bytes
, 0, bytes
- repr_bytes
);
4274 else if (bytes
< sizeof(values
[0]) && values
[num
- 1] >> (8 * bytes
) != 0)
4275 return _("value conflicts with instruction length");
4277 if (!riscv_opts
.rvc
&& (bytes
& 2))
4278 seg_info (now_seg
)->tc_segment_info_data
.last_insn16
= true;
4284 md_assemble (char *str
)
4286 struct riscv_cl_insn insn
;
4287 expressionS imm_expr
;
4288 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
4290 /* The architecture and privileged elf attributes should be set
4291 before assembling. */
4292 if (!start_assemble
)
4294 start_assemble
= true;
4296 riscv_set_abi_by_arch ();
4297 if (!riscv_set_default_priv_spec (NULL
))
4301 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
4303 const struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
4304 &imm_reloc
, op_hash
);
4308 if (error
.missing_ext
)
4309 as_bad ("%s `%s', extension `%s' required", error
.msg
,
4310 error
.statement
, error
.missing_ext
);
4312 as_bad ("%s `%s'", error
.msg
, error
.statement
);
4316 if (insn
.insn_mo
->pinfo
== INSN_MACRO
)
4317 macro (&insn
, &imm_expr
, &imm_reloc
);
4319 append_insn (&insn
, &imm_expr
, imm_reloc
);
4323 md_atof (int type
, char *litP
, int *sizeP
)
4325 return ieee_md_atof (type
, litP
, sizeP
, target_big_endian
);
4329 md_number_to_chars (char *buf
, valueT val
, int n
)
4331 if (target_big_endian
)
4332 number_to_chars_bigendian (buf
, val
, n
);
4334 number_to_chars_littleendian (buf
, val
, n
);
4337 const char md_shortopts
[] = "O::g::G:";
4341 OPTION_MARCH
= OPTION_MD_BASE
,
4348 OPTION_NO_ARCH_ATTR
,
4350 OPTION_NO_CSR_CHECK
,
4354 OPTION_LITTLE_ENDIAN
,
4358 const struct option md_longopts
[] =
4360 {"march", required_argument
, NULL
, OPTION_MARCH
},
4361 {"fPIC", no_argument
, NULL
, OPTION_PIC
},
4362 {"fpic", no_argument
, NULL
, OPTION_PIC
},
4363 {"fno-pic", no_argument
, NULL
, OPTION_NO_PIC
},
4364 {"mabi", required_argument
, NULL
, OPTION_MABI
},
4365 {"mrelax", no_argument
, NULL
, OPTION_RELAX
},
4366 {"mno-relax", no_argument
, NULL
, OPTION_NO_RELAX
},
4367 {"march-attr", no_argument
, NULL
, OPTION_ARCH_ATTR
},
4368 {"mno-arch-attr", no_argument
, NULL
, OPTION_NO_ARCH_ATTR
},
4369 {"mcsr-check", no_argument
, NULL
, OPTION_CSR_CHECK
},
4370 {"mno-csr-check", no_argument
, NULL
, OPTION_NO_CSR_CHECK
},
4371 {"misa-spec", required_argument
, NULL
, OPTION_MISA_SPEC
},
4372 {"mpriv-spec", required_argument
, NULL
, OPTION_MPRIV_SPEC
},
4373 {"mbig-endian", no_argument
, NULL
, OPTION_BIG_ENDIAN
},
4374 {"mlittle-endian", no_argument
, NULL
, OPTION_LITTLE_ENDIAN
},
4376 {NULL
, no_argument
, NULL
, 0}
4378 const size_t md_longopts_size
= sizeof (md_longopts
);
4381 md_parse_option (int c
, const char *arg
)
4386 /* List all avaiable extensions. */
4387 if (strcmp (arg
, "help") == 0)
4389 riscv_print_extensions ();
4390 exit (EXIT_SUCCESS
);
4392 default_arch_with_ext
= arg
;
4396 riscv_opts
.pic
= false;
4400 riscv_opts
.pic
= true;
4404 if (strcmp (arg
, "ilp32") == 0)
4405 riscv_set_abi (32, FLOAT_ABI_SOFT
, false);
4406 else if (strcmp (arg
, "ilp32e") == 0)
4407 riscv_set_abi (32, FLOAT_ABI_SOFT
, true);
4408 else if (strcmp (arg
, "ilp32f") == 0)
4409 riscv_set_abi (32, FLOAT_ABI_SINGLE
, false);
4410 else if (strcmp (arg
, "ilp32d") == 0)
4411 riscv_set_abi (32, FLOAT_ABI_DOUBLE
, false);
4412 else if (strcmp (arg
, "ilp32q") == 0)
4413 riscv_set_abi (32, FLOAT_ABI_QUAD
, false);
4414 else if (strcmp (arg
, "lp64") == 0)
4415 riscv_set_abi (64, FLOAT_ABI_SOFT
, false);
4416 else if (strcmp (arg
, "lp64e") == 0)
4417 riscv_set_abi (64, FLOAT_ABI_SOFT
, true);
4418 else if (strcmp (arg
, "lp64f") == 0)
4419 riscv_set_abi (64, FLOAT_ABI_SINGLE
, false);
4420 else if (strcmp (arg
, "lp64d") == 0)
4421 riscv_set_abi (64, FLOAT_ABI_DOUBLE
, false);
4422 else if (strcmp (arg
, "lp64q") == 0)
4423 riscv_set_abi (64, FLOAT_ABI_QUAD
, false);
4426 explicit_mabi
= true;
4430 riscv_opts
.relax
= true;
4433 case OPTION_NO_RELAX
:
4434 riscv_opts
.relax
= false;
4437 case OPTION_ARCH_ATTR
:
4438 riscv_opts
.arch_attr
= true;
4441 case OPTION_NO_ARCH_ATTR
:
4442 riscv_opts
.arch_attr
= false;
4445 case OPTION_CSR_CHECK
:
4446 riscv_opts
.csr_check
= true;
4449 case OPTION_NO_CSR_CHECK
:
4450 riscv_opts
.csr_check
= false;
4453 case OPTION_MISA_SPEC
:
4454 return riscv_set_default_isa_spec (arg
);
4456 case OPTION_MPRIV_SPEC
:
4457 return riscv_set_default_priv_spec (arg
);
4459 case OPTION_BIG_ENDIAN
:
4460 target_big_endian
= 1;
4463 case OPTION_LITTLE_ENDIAN
:
4464 target_big_endian
= 0;
4475 riscv_after_parse_args (void)
4477 /* The --with-arch is optional for now, so we still need to set the xlen
4478 according to the default_arch, which is set by the --target. */
4481 if (strcmp (default_arch
, "riscv32") == 0)
4483 else if (strcmp (default_arch
, "riscv64") == 0)
4486 as_bad ("unknown default architecture `%s'", default_arch
);
4489 /* Set default specs. */
4490 if (default_isa_spec
== ISA_SPEC_CLASS_NONE
)
4491 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC
);
4492 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
4493 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC
);
4495 riscv_set_arch (default_arch_with_ext
);
4497 /* If the CIE to be produced has not been overridden on the command line,
4498 then produce version 3 by default. This allows us to use the full
4499 range of registers in a .cfi_return_column directive. */
4500 if (flag_dwarf_cie_version
== -1)
4501 flag_dwarf_cie_version
= 3;
4504 bool riscv_parse_name (const char *name
, struct expressionS
*ep
,
4505 enum expr_mode mode
)
4510 if (!probing_insn_operands
)
4513 gas_assert (mode
== expr_normal
);
4515 regno
= reg_lookup_internal (name
, RCLASS_GPR
);
4516 if (regno
== (unsigned int)-1)
4519 if (symbol_find (name
) != NULL
)
4522 /* Create a symbol without adding it to the symbol table yet.
4523 Insertion will happen only once we commit to using the insn
4524 we're probing operands for. */
4525 for (sym
= deferred_sym_rootP
; sym
; sym
= symbol_next (sym
))
4526 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
4530 for (sym
= orphan_sym_rootP
; sym
; sym
= symbol_next (sym
))
4531 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
4533 symbol_remove (sym
, &orphan_sym_rootP
, &orphan_sym_lastP
);
4537 sym
= symbol_create (name
, undefined_section
,
4538 &zero_address_frag
, 0);
4540 symbol_append (sym
, deferred_sym_lastP
, &deferred_sym_rootP
,
4541 &deferred_sym_lastP
);
4544 ep
->X_op
= O_symbol
;
4545 ep
->X_add_symbol
= sym
;
4546 ep
->X_add_number
= 0;
4552 md_pcrel_from (fixS
*fixP
)
4554 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4557 /* Apply a fixup to the object file. */
4560 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg
)
4562 unsigned int subtype
;
4563 bfd_byte
*buf
= (bfd_byte
*) (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
);
4564 bool relaxable
= false;
4568 /* Remember value for tc_gen_reloc. */
4569 fixP
->fx_addnumber
= *valP
;
4571 switch (fixP
->fx_r_type
)
4573 case BFD_RELOC_RISCV_HI20
:
4574 case BFD_RELOC_RISCV_LO12_I
:
4575 case BFD_RELOC_RISCV_LO12_S
:
4576 bfd_putl32 (riscv_apply_const_reloc (fixP
->fx_r_type
, *valP
)
4577 | bfd_getl32 (buf
), buf
);
4578 if (fixP
->fx_addsy
== NULL
)
4579 fixP
->fx_done
= true;
4583 case BFD_RELOC_RISCV_GOT_HI20
:
4584 /* R_RISCV_GOT_HI20 and the following R_RISCV_LO12_I are relaxable
4585 only if it is created as a result of la or lga assembler macros. */
4586 if (fixP
->tc_fix_data
.source_macro
== M_LA
4587 || fixP
->tc_fix_data
.source_macro
== M_LGA
)
4591 case BFD_RELOC_RISCV_ADD8
:
4592 case BFD_RELOC_RISCV_ADD16
:
4593 case BFD_RELOC_RISCV_ADD32
:
4594 case BFD_RELOC_RISCV_ADD64
:
4595 case BFD_RELOC_RISCV_SUB6
:
4596 case BFD_RELOC_RISCV_SUB8
:
4597 case BFD_RELOC_RISCV_SUB16
:
4598 case BFD_RELOC_RISCV_SUB32
:
4599 case BFD_RELOC_RISCV_SUB64
:
4600 case BFD_RELOC_RISCV_RELAX
:
4601 /* cvt_frag_to_fill () has called output_leb128 (). */
4602 case BFD_RELOC_RISCV_SET_ULEB128
:
4603 case BFD_RELOC_RISCV_SUB_ULEB128
:
4606 case BFD_RELOC_RISCV_TPREL_HI20
:
4607 case BFD_RELOC_RISCV_TPREL_LO12_I
:
4608 case BFD_RELOC_RISCV_TPREL_LO12_S
:
4609 case BFD_RELOC_RISCV_TPREL_ADD
:
4610 case BFD_RELOC_RISCV_TLSDESC_HI20
:
4614 case BFD_RELOC_RISCV_TLS_GOT_HI20
:
4615 case BFD_RELOC_RISCV_TLS_GD_HI20
:
4616 case BFD_RELOC_RISCV_TLS_DTPREL32
:
4617 case BFD_RELOC_RISCV_TLS_DTPREL64
:
4618 if (fixP
->fx_addsy
!= NULL
)
4619 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
4621 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
4622 _("TLS relocation against a constant"));
4626 /* Use pc-relative relocation for FDE initial location.
4627 The symbol address in .eh_frame may be adjusted in
4628 _bfd_elf_discard_section_eh_frame, and the content of
4629 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4630 Therefore, we cannot insert a relocation whose addend symbol is
4631 in .eh_frame. Othrewise, the value may be adjusted twice. */
4632 if (fixP
->fx_addsy
&& fixP
->fx_subsy
4633 && (sub_segment
= S_GET_SEGMENT (fixP
->fx_subsy
))
4634 && strcmp (sub_segment
->name
, ".eh_frame") == 0
4635 && S_GET_VALUE (fixP
->fx_subsy
)
4636 == fixP
->fx_frag
->fr_address
+ fixP
->fx_where
)
4638 fixP
->fx_r_type
= BFD_RELOC_RISCV_32_PCREL
;
4639 fixP
->fx_subsy
= NULL
;
4646 case BFD_RELOC_RISCV_CFA
:
4647 if (fixP
->fx_addsy
&& fixP
->fx_subsy
)
4649 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4650 fixP
->fx_next
->fx_addsy
= fixP
->fx_subsy
;
4651 fixP
->fx_next
->fx_subsy
= NULL
;
4652 fixP
->fx_next
->fx_offset
= 0;
4653 fixP
->fx_subsy
= NULL
;
4655 switch (fixP
->fx_r_type
)
4658 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD64
;
4659 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB64
;
4663 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD32
;
4664 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4668 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD16
;
4669 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4673 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD8
;
4674 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4677 case BFD_RELOC_RISCV_CFA
:
4678 /* Load the byte to get the subtype. */
4679 subtype
= bfd_get_8 (NULL
, &((fragS
*) (fixP
->fx_frag
->fr_opcode
))->fr_literal
[fixP
->fx_where
]);
4680 loc
= fixP
->fx_frag
->fr_fix
- (subtype
& 7);
4683 case DW_CFA_advance_loc1
:
4684 fixP
->fx_where
= loc
+ 1;
4685 fixP
->fx_next
->fx_where
= loc
+ 1;
4686 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET8
;
4687 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4690 case DW_CFA_advance_loc2
:
4692 fixP
->fx_next
->fx_size
= 2;
4693 fixP
->fx_where
= loc
+ 1;
4694 fixP
->fx_next
->fx_where
= loc
+ 1;
4695 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET16
;
4696 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4699 case DW_CFA_advance_loc4
:
4701 fixP
->fx_next
->fx_size
= 4;
4702 fixP
->fx_where
= loc
;
4703 fixP
->fx_next
->fx_where
= loc
;
4704 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET32
;
4705 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4709 if (subtype
< 0x80 && (subtype
& 0x40))
4711 /* DW_CFA_advance_loc */
4712 fixP
->fx_frag
= (fragS
*) fixP
->fx_frag
->fr_opcode
;
4713 fixP
->fx_next
->fx_frag
= fixP
->fx_frag
;
4714 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET6
;
4715 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB6
;
4718 as_fatal (_("internal: bad CFA value #%d"), subtype
);
4724 /* This case is unreachable. */
4731 /* If we are deleting this reloc entry, we must fill in the
4732 value now. This can happen if we have a .word which is not
4733 resolved when it appears but is later defined. */
4734 if (fixP
->fx_addsy
== NULL
)
4736 gas_assert (fixP
->fx_size
<= sizeof (valueT
));
4737 md_number_to_chars ((char *) buf
, *valP
, fixP
->fx_size
);
4742 case BFD_RELOC_RISCV_JMP
:
4745 /* Fill in a tentative value to improve objdump readability. */
4746 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4747 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4748 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_JTYPE_IMM (delta
), buf
);
4749 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4754 case BFD_RELOC_12_PCREL
:
4757 /* Fill in a tentative value to improve objdump readability. */
4758 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4759 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4760 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_BTYPE_IMM (delta
), buf
);
4761 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4766 case BFD_RELOC_RISCV_RVC_BRANCH
:
4769 /* Fill in a tentative value to improve objdump readability. */
4770 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4771 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4772 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CBTYPE_IMM (delta
), buf
);
4773 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4778 case BFD_RELOC_RISCV_RVC_JUMP
:
4781 /* Fill in a tentative value to improve objdump readability. */
4782 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4783 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4784 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CJTYPE_IMM (delta
), buf
);
4785 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4790 case BFD_RELOC_RISCV_CALL
:
4791 case BFD_RELOC_RISCV_CALL_PLT
:
4792 case BFD_RELOC_RISCV_TLSDESC_LOAD_LO12
:
4793 case BFD_RELOC_RISCV_TLSDESC_ADD_LO12
:
4794 case BFD_RELOC_RISCV_TLSDESC_CALL
:
4798 case BFD_RELOC_RISCV_PCREL_HI20
:
4799 /* Record and evaluate the pcrel_hi relocation with local symbol.
4800 Fill in a tentative value to improve objdump readability for -mrelax,
4801 and set fx_done for -mno-relax. */
4803 && S_IS_LOCAL (fixP
->fx_addsy
)
4804 && S_GET_SEGMENT (fixP
->fx_addsy
) == seg
)
4806 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4807 bfd_vma value
= target
- md_pcrel_from (fixP
);
4809 /* Record PCREL_HI20. */
4810 if (!riscv_record_pcrel_fixup (riscv_pcrel_hi_fixup_hash
,
4811 (const asection
*) seg
,
4812 md_pcrel_from (fixP
),
4815 as_warn (_("too many pcrel_hi"));
4817 bfd_putl32 (bfd_getl32 (buf
)
4818 | ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)),
4820 if (!riscv_opts
.relax
)
4826 case BFD_RELOC_RISCV_PCREL_LO12_S
:
4827 case BFD_RELOC_RISCV_PCREL_LO12_I
:
4828 /* Resolve the pcrel_lo relocation with local symbol.
4829 Fill in a tentative value to improve objdump readability for -mrelax,
4830 and set fx_done for -mno-relax. */
4832 bfd_vma location_pcrel_hi
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4833 riscv_pcrel_hi_fixup search
=
4834 {(const asection
*) seg
, location_pcrel_hi
, 0, 0};
4835 riscv_pcrel_hi_fixup
*entry
= htab_find (riscv_pcrel_hi_fixup_hash
,
4837 if (entry
&& entry
->symbol
4838 && S_IS_LOCAL (entry
->symbol
)
4839 && S_GET_SEGMENT (entry
->symbol
) == seg
)
4841 bfd_vma target
= entry
->target
;
4842 bfd_vma value
= target
- entry
->address
;
4843 if (fixP
->fx_r_type
== BFD_RELOC_RISCV_PCREL_LO12_S
)
4844 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_STYPE_IMM (value
), buf
);
4846 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_ITYPE_IMM (value
), buf
);
4847 /* Relaxations should never be enabled by `.option relax'. */
4848 if (!riscv_opts
.relax
)
4855 case BFD_RELOC_RISCV_ALIGN
:
4859 /* We ignore generic BFD relocations we don't know about. */
4860 if (bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
) != NULL
)
4861 as_fatal (_("internal: bad relocation #%d"), fixP
->fx_r_type
);
4864 if (fixP
->fx_subsy
!= NULL
)
4865 as_bad_subtract (fixP
);
4867 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4868 if (relaxable
&& fixP
->fx_tcbit
&& fixP
->fx_addsy
!= NULL
)
4870 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4871 fixP
->fx_next
->fx_addsy
= fixP
->fx_next
->fx_subsy
= NULL
;
4872 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_RELAX
;
4873 fixP
->fx_next
->fx_size
= 0;
4877 /* Because the value of .cfi_remember_state may changed after relaxation,
4878 we insert a fix to relocate it again in link-time. */
4881 riscv_pre_output_hook (void)
4883 const frchainS
*frch
;
4886 /* Save the current segment info. */
4888 subsegT subseg
= now_subseg
;
4890 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
4891 for (frch
= seg_info (s
)->frchainP
; frch
; frch
= frch
->frch_next
)
4895 for (frag
= frch
->frch_root
; frag
; frag
= frag
->fr_next
)
4897 if (frag
->fr_type
== rs_cfa
)
4900 expressionS
*symval
;
4902 symval
= symbol_get_value_expression (frag
->fr_symbol
);
4903 exp
.X_op
= O_subtract
;
4904 exp
.X_add_symbol
= symval
->X_add_symbol
;
4905 exp
.X_add_number
= 0;
4906 exp
.X_op_symbol
= symval
->X_op_symbol
;
4908 /* We must set the segment before creating a frag after all
4909 frag chains have been chained together. */
4910 subseg_set (s
, frch
->frch_subseg
);
4912 fix_new_exp (frag
, (int) frag
->fr_offset
, 1, &exp
, 0,
4913 BFD_RELOC_RISCV_CFA
);
4918 /* Restore the original segment info. */
4919 subseg_set (seg
, subseg
);
4922 /* Handle the .option pseudo-op. */
4925 s_riscv_option (int x ATTRIBUTE_UNUSED
)
4927 char *name
= input_line_pointer
, ch
;
4929 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
4930 ++input_line_pointer
;
4931 ch
= *input_line_pointer
;
4932 *input_line_pointer
= '\0';
4934 if (strcmp (name
, "rvc") == 0)
4936 riscv_update_subset (&riscv_rps_as
, "+c");
4937 riscv_set_arch_str (&riscv_rps_as
.subset_list
->arch_str
);
4938 riscv_set_rvc (true);
4940 else if (strcmp (name
, "norvc") == 0)
4942 riscv_update_subset (&riscv_rps_as
, "-c");
4943 riscv_set_arch_str (&riscv_rps_as
.subset_list
->arch_str
);
4944 riscv_set_rvc (false);
4946 else if (strcmp (name
, "pic") == 0)
4947 riscv_opts
.pic
= true;
4948 else if (strcmp (name
, "nopic") == 0)
4949 riscv_opts
.pic
= false;
4950 else if (strcmp (name
, "relax") == 0)
4951 riscv_opts
.relax
= true;
4952 else if (strcmp (name
, "norelax") == 0)
4953 riscv_opts
.relax
= false;
4954 else if (strcmp (name
, "csr-check") == 0)
4955 riscv_opts
.csr_check
= true;
4956 else if (strcmp (name
, "no-csr-check") == 0)
4957 riscv_opts
.csr_check
= false;
4958 else if (strncmp (name
, "arch,", 5) == 0)
4961 if (ISSPACE (*name
) && *name
!= '\0')
4963 riscv_update_subset (&riscv_rps_as
, name
);
4964 riscv_set_arch_str (&riscv_rps_as
.subset_list
->arch_str
);
4966 riscv_set_rvc (riscv_subset_supports (&riscv_rps_as
, "c")
4967 || riscv_subset_supports (&riscv_rps_as
, "zca"));
4969 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
4972 else if (strcmp (name
, "push") == 0)
4974 struct riscv_option_stack
*s
;
4976 s
= XNEW (struct riscv_option_stack
);
4977 s
->next
= riscv_opts_stack
;
4978 s
->options
= riscv_opts
;
4979 s
->subset_list
= riscv_rps_as
.subset_list
;
4980 riscv_opts_stack
= s
;
4981 riscv_rps_as
.subset_list
= riscv_copy_subset_list (s
->subset_list
);
4983 else if (strcmp (name
, "pop") == 0)
4985 struct riscv_option_stack
*s
;
4987 s
= riscv_opts_stack
;
4989 as_bad (_(".option pop with no .option push"));
4992 riscv_subset_list_t
*release_subsets
= riscv_rps_as
.subset_list
;
4993 riscv_opts_stack
= s
->next
;
4994 riscv_opts
= s
->options
;
4995 riscv_rps_as
.subset_list
= s
->subset_list
;
4996 riscv_release_subset_list (release_subsets
);
5002 as_warn (_("unrecognized .option directive: %s"), name
);
5004 *input_line_pointer
= ch
;
5005 demand_empty_rest_of_line ();
5008 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
5009 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
5010 use in DWARF debug information. */
5013 s_dtprel (int bytes
)
5020 if (ex
.X_op
!= O_symbol
)
5022 as_bad (_("unsupported use of %s"), (bytes
== 8
5025 ignore_rest_of_line ();
5028 p
= frag_more (bytes
);
5029 md_number_to_chars (p
, 0, bytes
);
5030 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, bytes
, &ex
, false,
5032 ? BFD_RELOC_RISCV_TLS_DTPREL64
5033 : BFD_RELOC_RISCV_TLS_DTPREL32
));
5035 demand_empty_rest_of_line ();
5039 riscv_make_nops (char *buf
, bfd_vma bytes
)
5043 /* RISC-V instructions cannot begin or end on odd addresses, so this case
5044 means we are not within a valid instruction sequence. It is thus safe
5045 to use a zero byte, even though that is not a valid instruction. */
5049 /* Use at most one 2-byte NOP. */
5050 if ((bytes
- i
) % 4 == 2)
5052 number_to_chars_littleendian (buf
+ i
, RVC_NOP
, 2);
5056 /* Fill the remainder with 4-byte NOPs. */
5057 for ( ; i
< bytes
; i
+= 4)
5058 number_to_chars_littleendian (buf
+ i
, RISCV_NOP
, 4);
5061 /* Called from md_do_align. Used to create an alignment frag in a
5062 code section by emitting a worst-case NOP sequence that the linker
5063 will later relax to the correct number of NOPs. We can't compute
5064 the correct alignment now because of other linker relaxations. */
5067 riscv_frag_align_code (int n
)
5069 bfd_vma bytes
= (bfd_vma
) 1 << n
;
5070 bfd_vma insn_alignment
= riscv_opts
.rvc
? 2 : 4;
5071 bfd_vma worst_case_bytes
= bytes
- insn_alignment
;
5075 /* If we are moving to alignment no larger than the instruction size, then
5076 no special alignment handling is required. */
5077 if (bytes
<= insn_alignment
)
5079 if (bytes
== insn_alignment
)
5080 seg_info (now_seg
)->tc_segment_info_data
.last_insn16
= false;
5084 /* When not relaxing, riscv_handle_align handles code alignment. */
5085 if (!riscv_opts
.relax
)
5088 /* If the last item emitted was not an ordinary insn, first align back to
5089 insn granularity. Don't do this unconditionally, to avoid altering frags
5090 when that's not actually needed. */
5091 if (seg_info (now_seg
)->tc_segment_info_data
.map_state
!= MAP_INSN
5092 || seg_info (now_seg
)->tc_segment_info_data
.last_insn16
)
5093 frag_align_code (riscv_opts
.rvc
? 1 : 2, 0);
5094 seg_info (now_seg
)->tc_segment_info_data
.last_insn16
= false;
5096 /* Maybe we should use frag_var to create a new rs_align_code fragment,
5097 rather than just use frag_more to handle an alignment here? So that we
5098 don't need to call riscv_mapping_state again later, and then only need
5099 to check frag->fr_type to see if it is frag_align_code. */
5100 nops
= frag_more (worst_case_bytes
);
5102 ex
.X_op
= O_constant
;
5103 ex
.X_add_number
= worst_case_bytes
;
5105 riscv_make_nops (nops
, worst_case_bytes
);
5107 fix_new_exp (frag_now
, nops
- frag_now
->fr_literal
, 0,
5108 &ex
, false, BFD_RELOC_RISCV_ALIGN
);
5110 riscv_mapping_state (MAP_INSN
, worst_case_bytes
, true/* fr_align_code */);
5112 /* We need to start a new frag after the alignment which may be removed by
5113 the linker, to prevent the assembler from computing static offsets.
5114 This is necessary to get correct EH info. */
5115 frag_wane (frag_now
);
5121 /* Implement HANDLE_ALIGN. */
5124 riscv_handle_align (fragS
*fragP
)
5126 switch (fragP
->fr_type
)
5130 bfd_signed_vma bytes
= (fragP
->fr_next
->fr_address
5131 - fragP
->fr_address
- fragP
->fr_fix
);
5132 /* We have 4 byte uncompressed nops. */
5133 bfd_signed_vma size
= 4;
5134 bfd_signed_vma excess
= bytes
% size
;
5135 char *p
= fragP
->fr_literal
+ fragP
->fr_fix
;
5140 /* Insert zeros or compressed nops to get 4 byte alignment. */
5144 riscv_add_odd_padding_symbol (fragP
);
5145 riscv_make_nops (p
, excess
);
5146 fragP
->fr_fix
+= excess
;
5150 /* The frag will be changed to `rs_fill` later. The function
5151 `write_contents` will try to fill the remaining spaces
5152 according to the patterns we give. In this case, we give
5153 a 4 byte uncompressed nop as the pattern, and set the size
5154 of the pattern into `fr_var`. The nop will be output to the
5155 file `fr_offset` times. However, `fr_offset` could be zero
5156 if we don't need to pad the boundary finally. */
5157 riscv_make_nops (p
, size
);
5158 fragP
->fr_var
= size
;
5167 /* This usually called from frag_var. */
5170 riscv_init_frag (fragS
* fragP
, int max_chars
)
5172 /* Do not add mapping symbol to debug sections. */
5173 if (bfd_section_flags (now_seg
) & SEC_DEBUGGING
)
5176 switch (fragP
->fr_type
)
5181 riscv_mapping_state (MAP_DATA
, max_chars
, false/* fr_align_code */);
5184 riscv_mapping_state (MAP_INSN
, max_chars
, true/* fr_align_code */);
5192 md_estimate_size_before_relax (fragS
*fragp
, asection
*segtype
)
5194 return (fragp
->fr_var
= relaxed_branch_length (fragp
, segtype
, false));
5197 /* Translate internal representation of relocation info to BFD target
5201 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
5203 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
5205 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
5206 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
5207 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
5208 reloc
->addend
= fixp
->fx_addnumber
;
5210 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
5211 if (reloc
->howto
== NULL
)
5213 if ((fixp
->fx_r_type
== BFD_RELOC_16
|| fixp
->fx_r_type
== BFD_RELOC_8
)
5214 && fixp
->fx_addsy
!= NULL
&& fixp
->fx_subsy
!= NULL
)
5216 /* We don't have R_RISCV_8/16, but for this special case,
5217 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
5221 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
5222 _("cannot represent %s relocation in object file"),
5223 bfd_get_reloc_code_name (fixp
->fx_r_type
));
5231 riscv_relax_frag (asection
*sec
, fragS
*fragp
, long stretch ATTRIBUTE_UNUSED
)
5233 if (RELAX_BRANCH_P (fragp
->fr_subtype
))
5235 offsetT old_var
= fragp
->fr_var
;
5236 fragp
->fr_var
= relaxed_branch_length (fragp
, sec
, true);
5237 return fragp
->fr_var
- old_var
;
5243 /* Expand far branches to multi-instruction sequences. */
5246 md_convert_frag_branch (fragS
*fragp
)
5254 buf
= (bfd_byte
*)fragp
->fr_literal
+ fragp
->fr_fix
;
5256 exp
.X_op
= O_symbol
;
5257 exp
.X_add_symbol
= fragp
->fr_symbol
;
5258 exp
.X_add_number
= fragp
->fr_offset
;
5260 gas_assert (fragp
->fr_var
== RELAX_BRANCH_LENGTH (fragp
->fr_subtype
));
5262 if (RELAX_BRANCH_RVC (fragp
->fr_subtype
))
5264 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
5268 /* Expand the RVC branch into a RISC-V one. */
5269 insn
= bfd_getl16 (buf
);
5270 rs1
= 8 + ((insn
>> OP_SH_CRS1S
) & OP_MASK_CRS1S
);
5271 if ((insn
& MASK_C_J
) == MATCH_C_J
)
5273 else if ((insn
& MASK_C_JAL
) == MATCH_C_JAL
)
5274 insn
= MATCH_JAL
| (X_RA
<< OP_SH_RD
);
5275 else if ((insn
& MASK_C_BEQZ
) == MATCH_C_BEQZ
)
5276 insn
= MATCH_BEQ
| (rs1
<< OP_SH_RS1
);
5277 else if ((insn
& MASK_C_BNEZ
) == MATCH_C_BNEZ
)
5278 insn
= MATCH_BNE
| (rs1
<< OP_SH_RS1
);
5281 bfd_putl32 (insn
, buf
);
5285 /* Invert the branch condition. Branch over the jump. */
5286 insn
= bfd_getl16 (buf
);
5287 insn
^= MATCH_C_BEQZ
^ MATCH_C_BNEZ
;
5288 insn
|= ENCODE_CBTYPE_IMM (6);
5289 bfd_putl16 (insn
, buf
);
5294 /* Just keep the RVC branch. */
5295 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
5296 ? BFD_RELOC_RISCV_RVC_JUMP
: BFD_RELOC_RISCV_RVC_BRANCH
;
5297 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5298 2, &exp
, false, reloc
);
5307 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
5310 gas_assert (!RELAX_BRANCH_UNCOND (fragp
->fr_subtype
));
5312 /* Invert the branch condition. Branch over the jump. */
5313 insn
= bfd_getl32 (buf
);
5314 insn
^= MATCH_BEQ
^ MATCH_BNE
;
5315 insn
|= ENCODE_BTYPE_IMM (8);
5316 bfd_putl32 (insn
, buf
);
5320 /* Jump to the target. */
5321 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5322 4, &exp
, false, BFD_RELOC_RISCV_JMP
);
5323 bfd_putl32 (MATCH_JAL
, buf
);
5328 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
5329 ? BFD_RELOC_RISCV_JMP
: BFD_RELOC_12_PCREL
;
5330 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5331 4, &exp
, false, reloc
);
5340 fixp
->fx_file
= fragp
->fr_file
;
5341 fixp
->fx_line
= fragp
->fr_line
;
5343 gas_assert (buf
== (bfd_byte
*)fragp
->fr_literal
5344 + fragp
->fr_fix
+ fragp
->fr_var
);
5346 fragp
->fr_fix
+= fragp
->fr_var
;
5349 /* Relax a machine dependent frag. This returns the amount by which
5350 the current size of the frag should change. */
5353 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT asec ATTRIBUTE_UNUSED
,
5356 gas_assert (RELAX_BRANCH_P (fragp
->fr_subtype
));
5357 md_convert_frag_branch (fragp
);
5361 md_show_usage (FILE *stream
)
5363 fprintf (stream
, _("\
5365 -fpic or -fPIC generate position-independent code\n\
5366 -fno-pic don't generate position-independent code (default)\n\
5367 -march=ISA set the RISC-V architecture\n\
5368 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
5369 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.10, 1.11, 1.12)\n\
5370 -mabi=ABI set the RISC-V ABI\n\
5371 -mrelax enable relax (default)\n\
5372 -mno-relax disable relax\n\
5373 -march-attr generate RISC-V arch attribute\n\
5374 -mno-arch-attr don't generate RISC-V arch attribute\n\
5375 -mcsr-check enable the csr ISA and privilege spec version checks\n\
5376 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
5377 -mbig-endian assemble for big-endian\n\
5378 -mlittle-endian assemble for little-endian\n\
5382 /* Standard calling conventions leave the CFA at SP on entry. */
5385 riscv_cfi_frame_initial_instructions (void)
5387 cfi_add_CFA_def_cfa (X_SP
, 0);
5391 tc_riscv_regname_to_dw2regnum (char *regname
)
5395 if ((reg
= reg_lookup_internal (regname
, RCLASS_GPR
)) >= 0)
5398 if ((reg
= reg_lookup_internal (regname
, RCLASS_FPR
)) >= 0)
5401 if ((reg
= reg_lookup_internal (regname
, RCLASS_VECR
)) >= 0)
5404 /* CSRs are numbered 4096 -> 8191. */
5405 if ((reg
= reg_lookup_internal (regname
, RCLASS_CSR
)) >= 0)
5408 as_bad (_("unknown register `%s'"), regname
);
5413 riscv_elf_section_change_hook (void)
5415 struct riscv_segment_info_type
*info
5416 = &seg_info(now_seg
)->tc_segment_info_data
;
5418 if (info
->rvc
&& !riscv_opts
.rvc
)
5419 info
->last_insn16
= true;
5421 info
->rvc
= riscv_opts
.rvc
;
5425 riscv_elf_final_processing (void)
5427 riscv_set_abi_by_arch ();
5428 riscv_release_subset_list (riscv_rps_as
.subset_list
);
5429 elf_elfheader (stdoutput
)->e_flags
|= elf_flags
;
5432 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
5433 since these directives break relaxation when used with symbol deltas. */
5436 s_riscv_leb128 (int sign
)
5439 char *save_in
= input_line_pointer
;
5442 if (sign
&& exp
.X_op
!= O_constant
)
5443 as_bad (_("non-constant .sleb128 is not supported"));
5444 else if (!sign
&& exp
.X_op
!= O_constant
&& exp
.X_op
!= O_subtract
)
5445 as_bad (_(".uleb128 only supports constant or subtract expressions"));
5447 demand_empty_rest_of_line ();
5449 input_line_pointer
= save_in
;
5450 return s_leb128 (sign
);
5453 /* Parse the .insn directive. There are three formats,
5454 Format 1: .insn <type> <operand1>, <operand2>, ...
5455 Format 2: .insn <length>, <value>
5456 Format 3: .insn <value>. */
5459 s_riscv_insn (int x ATTRIBUTE_UNUSED
)
5461 char *str
= input_line_pointer
;
5462 struct riscv_cl_insn insn
;
5463 expressionS imm_expr
;
5464 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
5467 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
5468 ++input_line_pointer
;
5470 save_c
= *input_line_pointer
;
5471 *input_line_pointer
= '\0';
5473 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
5475 struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
5476 &imm_reloc
, insn_type_hash
);
5479 char *save_in
= input_line_pointer
;
5480 error
.msg
= riscv_ip_hardcode (str
, &insn
, &imm_expr
, error
.msg
);
5481 input_line_pointer
= save_in
;
5486 if (error
.missing_ext
)
5487 as_bad ("%s `%s', extension `%s' required", error
.msg
, error
.statement
,
5490 as_bad ("%s `%s'", error
.msg
, error
.statement
);
5494 gas_assert (insn
.insn_mo
->pinfo
!= INSN_MACRO
);
5495 append_insn (&insn
, &imm_expr
, imm_reloc
);
5498 *input_line_pointer
= save_c
;
5499 demand_empty_rest_of_line ();
5502 /* Update architecture and privileged elf attributes. If we don't set
5503 them, then try to output the default ones. */
5506 riscv_write_out_attrs (void)
5508 const char *priv_str
, *p
;
5509 /* versions[0]: major version.
5510 versions[1]: minor version.
5511 versions[2]: revision version. */
5512 unsigned versions
[3] = {0}, number
= 0;
5515 /* Re-write architecture elf attribute. */
5516 if (!bfd_elf_add_proc_attr_string (stdoutput
, Tag_RISCV_arch
, file_arch_str
))
5517 as_fatal (_("error adding attribute: %s"),
5518 bfd_errmsg (bfd_get_error ()));
5519 free ((void *) file_arch_str
);
5521 /* For the file without any instruction, we don't set the default_priv_spec
5522 according to the privileged elf attributes since the md_assemble isn't
5525 && !riscv_set_default_priv_spec (NULL
))
5528 /* If we already have set privileged elf attributes, then no need to do
5529 anything. Otherwise, don't generate or update them when no CSR and
5530 privileged instructions are used. */
5531 if (!explicit_priv_attr
)
5534 RISCV_GET_PRIV_SPEC_NAME (priv_str
, default_priv_spec
);
5536 for (i
= 0; *p
; ++p
)
5538 if (*p
== '.' && i
< 3)
5540 versions
[i
++] = number
;
5543 else if (ISDIGIT (*p
))
5544 number
= (number
* 10) + (*p
- '0');
5547 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str
);
5551 versions
[i
] = number
;
5553 /* Re-write privileged elf attributes. */
5554 if (!bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec
,
5556 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_minor
,
5558 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_revision
,
5560 as_fatal (_("error adding attribute: %s"),
5561 bfd_errmsg (bfd_get_error ()));
5564 /* Add the default contents for the .riscv.attributes section. */
5567 riscv_set_public_attributes (void)
5569 if (riscv_opts
.arch_attr
|| explicit_attr
)
5570 riscv_write_out_attrs ();
5573 /* Scan uleb128 subtraction expressions and insert fixups for them.
5574 e.g., .uleb128 .L1 - .L0
5575 Because relaxation may change the value of the subtraction, we
5576 must resolve them at link-time. */
5579 riscv_insert_uleb128_fixes (bfd
*abfd ATTRIBUTE_UNUSED
,
5580 asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
5582 segment_info_type
*seginfo
= seg_info (sec
);
5585 subseg_set (sec
, 0);
5587 for (fragP
= seginfo
->frchainP
->frch_root
;
5588 fragP
; fragP
= fragP
->fr_next
)
5590 expressionS
*exp
, *exp_dup
;
5592 if (fragP
->fr_type
!= rs_leb128
|| fragP
->fr_symbol
== NULL
)
5595 exp
= symbol_get_value_expression (fragP
->fr_symbol
);
5597 if (exp
->X_op
!= O_subtract
)
5600 /* Only unsigned leb128 can be handled. */
5601 gas_assert (fragP
->fr_subtype
== 0);
5602 exp_dup
= xmemdup (exp
, sizeof (*exp
), sizeof (*exp
));
5603 exp_dup
->X_op
= O_symbol
;
5604 exp_dup
->X_op_symbol
= NULL
;
5606 /* Insert relocations to resolve the subtraction at link-time.
5607 Emit the SET relocation first in riscv. */
5608 exp_dup
->X_add_symbol
= exp
->X_add_symbol
;
5609 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
5610 exp_dup
, 0, BFD_RELOC_RISCV_SET_ULEB128
);
5611 exp_dup
->X_add_symbol
= exp
->X_op_symbol
;
5612 exp_dup
->X_add_number
= 0; /* Set addend of SUB_ULEB128 to zero. */
5613 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
5614 exp_dup
, 0, BFD_RELOC_RISCV_SUB_ULEB128
);
5615 free ((void *) exp_dup
);
5619 /* Called after all assembly has been done. */
5622 riscv_md_finish (void)
5624 riscv_set_public_attributes ();
5625 if (riscv_opts
.relax
)
5626 bfd_map_over_sections (stdoutput
, riscv_insert_uleb128_fixes
, NULL
);
5629 /* Called just before the assembler exits. */
5634 htab_delete (riscv_pcrel_hi_fixup_hash
);
5637 /* Adjust the symbol table. */
5640 riscv_adjust_symtab (void)
5642 bfd_map_over_sections (stdoutput
, riscv_check_mapping_symbols
, (char *) 0);
5643 elf_adjust_symtab ();
5646 /* Given a symbolic attribute NAME, return the proper integer value.
5647 Returns -1 if the attribute is not known. */
5650 riscv_convert_symbolic_attribute (const char *name
)
5659 /* When you modify this table you should
5660 also modify the list in doc/c-riscv.texi. */
5661 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
5665 T(priv_spec_revision
),
5666 T(unaligned_access
),
5675 for (i
= 0; i
< ARRAY_SIZE (attribute_table
); i
++)
5676 if (strcmp (name
, attribute_table
[i
].name
) == 0)
5677 return attribute_table
[i
].tag
;
5682 /* Parse a .attribute directive. */
5685 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED
)
5687 int tag
= obj_elf_vendor_attribute (OBJ_ATTR_PROC
);
5689 obj_attribute
*attr
;
5691 explicit_attr
= true;
5694 case Tag_RISCV_arch
:
5696 attr
= elf_known_obj_attributes_proc (stdoutput
);
5697 if (!start_assemble
)
5698 riscv_set_arch (attr
[Tag_RISCV_arch
].s
);
5700 as_fatal (_("architecture elf attributes must set before "
5701 "any instructions"));
5703 if (old_xlen
!= xlen
)
5705 /* We must re-init bfd again if xlen is changed. */
5706 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
5707 bfd_find_target (riscv_target_format (), stdoutput
);
5709 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
5710 as_warn (_("could not set architecture and machine"));
5714 case Tag_RISCV_priv_spec
:
5715 case Tag_RISCV_priv_spec_minor
:
5716 case Tag_RISCV_priv_spec_revision
:
5718 as_fatal (_("privileged elf attributes must set before "
5719 "any instructions"));
5727 /* Mark symbol that it follows a variant CC convention. */
5730 s_variant_cc (int ignored ATTRIBUTE_UNUSED
)
5736 elf_symbol_type
*elfsym
;
5738 c
= get_symbol_name (&name
);
5740 as_bad (_("missing symbol name for .variant_cc directive"));
5741 sym
= symbol_find_or_make (name
);
5742 restore_line_pointer (c
);
5743 demand_empty_rest_of_line ();
5745 bfdsym
= symbol_get_bfdsym (sym
);
5746 elfsym
= elf_symbol_from (bfdsym
);
5747 gas_assert (elfsym
);
5748 elfsym
->internal_elf_sym
.st_other
|= STO_RISCV_VARIANT_CC
;
5751 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5752 This is needed so RISC-V specific st_other values can be independently
5753 specified for an IFUNC resolver (that is called by the dynamic linker)
5754 and the symbol it resolves (aliased to the resolver). In particular,
5755 if a function symbol has special st_other value set via directives,
5756 then attaching an IFUNC resolver to that symbol should not override
5757 the st_other setting. Requiring the directive on the IFUNC resolver
5758 symbol would be unexpected and problematic in C code, where the two
5759 symbols appear as two independent function declarations. */
5762 riscv_elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
5764 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
5765 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
5766 /* If size is unset, copy size from src. Because we don't track whether
5767 .size has been used, we can't differentiate .size dest, 0 from the case
5768 where dest's size is unset. */
5769 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
5773 destelf
->size
= XNEW (expressionS
);
5774 *destelf
->size
= *srcelf
->size
;
5776 S_SET_SIZE (dest
, S_GET_SIZE (src
));
5780 /* RISC-V pseudo-ops table. */
5781 static const pseudo_typeS riscv_pseudo_table
[] =
5783 {"option", s_riscv_option
, 0},
5787 {"dtprelword", s_dtprel
, 4},
5788 {"dtpreldword", s_dtprel
, 8},
5789 {"uleb128", s_riscv_leb128
, 0},
5790 {"sleb128", s_riscv_leb128
, 1},
5791 {"insn", s_riscv_insn
, 0},
5792 {"attribute", s_riscv_attribute
, 0},
5793 {"variant_cc", s_variant_cc
, 0},
5794 {"float16", float_cons
, 'h'},
5800 riscv_pop_insert (void)
5802 extern void pop_insert (const pseudo_typeS
*);
5804 pop_insert (riscv_pseudo_table
);