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_V
, /* rvv only */
75 CSR_CLASS_DEBUG
, /* debug CSR */
76 CSR_CLASS_H
, /* hypervisor */
77 CSR_CLASS_H_32
, /* hypervisor, rv32 only */
78 CSR_CLASS_SMAIA
, /* Smaia */
79 CSR_CLASS_SMAIA_32
, /* Smaia, rv32 only */
80 CSR_CLASS_SMCNTRPMF
, /* Smcntrpmf */
81 CSR_CLASS_SMCNTRPMF_32
, /* Smcntrpmf, rv32 only */
82 CSR_CLASS_SMSTATEEN
, /* Smstateen only */
83 CSR_CLASS_SMSTATEEN_32
, /* Smstateen RV32 only */
84 CSR_CLASS_SSAIA
, /* Ssaia */
85 CSR_CLASS_SSAIA_AND_H
, /* Ssaia with H */
86 CSR_CLASS_SSAIA_32
, /* Ssaia, rv32 only */
87 CSR_CLASS_SSAIA_AND_H_32
, /* Ssaia with H, rv32 only */
88 CSR_CLASS_SSSTATEEN
, /* S[ms]stateen only */
89 CSR_CLASS_SSSTATEEN_AND_H
, /* S[ms]stateen only (with H) */
90 CSR_CLASS_SSSTATEEN_AND_H_32
, /* S[ms]stateen RV32 only (with H) */
91 CSR_CLASS_SSCOFPMF
, /* Sscofpmf only */
92 CSR_CLASS_SSCOFPMF_32
, /* Sscofpmf RV32 only */
93 CSR_CLASS_SSTC
, /* Sstc only */
94 CSR_CLASS_SSTC_AND_H
, /* Sstc only (with H) */
95 CSR_CLASS_SSTC_32
, /* Sstc RV32 only */
96 CSR_CLASS_SSTC_AND_H_32
, /* Sstc RV32 only (with H) */
97 CSR_CLASS_XTHEADVECTOR
, /* xtheadvector only */
100 /* This structure holds all restricted conditions for a CSR. */
101 struct riscv_csr_extra
103 /* Class to which this CSR belongs. Used to decide whether or
104 not this CSR is legal in the current -march context. */
105 enum riscv_csr_class csr_class
;
107 /* CSR may have differnet numbers in the previous priv spec. */
110 /* Record the CSR is defined/valid in which versions. */
111 enum riscv_spec_class define_version
;
113 /* Record the CSR is aborted/invalid from which versions. If it isn't
114 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
115 enum riscv_spec_class abort_version
;
117 /* The CSR may have more than one setting. */
118 struct riscv_csr_extra
*next
;
121 /* This structure contains information about errors that occur within the
123 struct riscv_ip_error
125 /* General error message */
128 /* Statement that caused the error */
131 /* Missing extension that needs to be enabled */
132 const char* missing_ext
;
136 #define DEFAULT_ARCH "riscv64"
139 #ifndef DEFAULT_RISCV_ATTR
140 #define DEFAULT_RISCV_ATTR 0
143 /* Let riscv_after_parse_args set the default value according to xlen. */
144 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
145 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
148 /* Need to sync the version with RISC-V compiler. */
149 #ifndef DEFAULT_RISCV_ISA_SPEC
150 #define DEFAULT_RISCV_ISA_SPEC "20191213"
153 #ifndef DEFAULT_RISCV_PRIV_SPEC
154 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
157 static const char default_arch
[] = DEFAULT_ARCH
;
158 static const char *default_arch_with_ext
= DEFAULT_RISCV_ARCH_WITH_EXT
;
159 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_NONE
;
160 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
162 static unsigned xlen
= 0; /* The width of an x-register. */
163 static unsigned abi_xlen
= 0; /* The width of a pointer in the ABI. */
164 static bool rve_abi
= false;
167 FLOAT_ABI_DEFAULT
= -1,
173 static enum float_abi float_abi
= FLOAT_ABI_DEFAULT
;
175 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
176 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
178 static unsigned elf_flags
= 0;
180 static bool probing_insn_operands
;
182 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
183 Otherwise, return 1. */
186 riscv_set_default_isa_spec (const char *s
)
188 enum riscv_spec_class
class = ISA_SPEC_CLASS_NONE
;
189 RISCV_GET_ISA_SPEC_CLASS (s
, class);
190 if (class == ISA_SPEC_CLASS_NONE
)
192 as_bad ("unknown default ISA spec `%s' set by "
193 "-misa-spec or --with-isa-spec", s
);
197 default_isa_spec
= class;
201 /* Set the default_priv_spec. Find the privileged elf attributes when
202 the input string is NULL. Return 0 if the spec isn't supported.
203 Otherwise, return 1. */
206 riscv_set_default_priv_spec (const char *s
)
208 enum riscv_spec_class
class = PRIV_SPEC_CLASS_NONE
;
209 unsigned major
, minor
, revision
;
212 RISCV_GET_PRIV_SPEC_CLASS (s
, class);
213 if (class != PRIV_SPEC_CLASS_NONE
214 && class != PRIV_SPEC_CLASS_1P9P1
)
216 default_priv_spec
= class;
222 as_bad (_("unknown default privileged spec `%s' set by "
223 "-mpriv-spec or --with-priv-spec"), s
);
227 /* Set the default_priv_spec by the privileged elf attributes. */
228 attr
= elf_known_obj_attributes_proc (stdoutput
);
229 major
= (unsigned) attr
[Tag_RISCV_priv_spec
].i
;
230 minor
= (unsigned) attr
[Tag_RISCV_priv_spec_minor
].i
;
231 revision
= (unsigned) attr
[Tag_RISCV_priv_spec_revision
].i
;
232 /* Version 0.0.0 is the default value and meningless. */
233 if (major
== 0 && minor
== 0 && revision
== 0)
236 riscv_get_priv_spec_class_from_numbers (major
, minor
, revision
, &class);
237 if (class != PRIV_SPEC_CLASS_NONE
)
239 default_priv_spec
= class;
243 /* Still can not find the privileged spec class. */
244 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
245 "privileged elf attributes"), major
, minor
, revision
);
249 /* This is the set of options which the .option pseudo-op may modify. */
250 struct riscv_set_options
252 int pic
; /* Generate position-independent code. */
253 int rvc
; /* Generate RVC code. */
254 int relax
; /* Emit relocs the linker is allowed to relax. */
255 int arch_attr
; /* Emit architecture and privileged elf attributes. */
256 int csr_check
; /* Enable the CSR checking. */
259 static struct riscv_set_options riscv_opts
=
264 DEFAULT_RISCV_ATTR
, /* arch_attr */
268 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
269 for elf_flags once we have enabled c extension. */
272 riscv_set_rvc (bool rvc_value
)
275 elf_flags
|= EF_RISCV_RVC
;
277 riscv_opts
.rvc
= rvc_value
;
280 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
285 elf_flags
|= EF_RISCV_TSO
;
288 /* The linked list hanging off of .subsets_list records all enabled extensions,
289 which are parsed from the architecture string. The architecture string can
290 be set by the -march option, the elf architecture attributes, and the
291 --with-arch configure option. */
292 static riscv_parse_subset_t riscv_rps_as
=
294 NULL
, /* subset_list, we will set it later once
295 riscv_opts_stack is created or updated. */
296 as_bad
, /* error_handler. */
298 &default_isa_spec
, /* isa_spec. */
299 true, /* check_unknown_prefixed_ext. */
302 /* Update the architecture string in the subset_list. */
305 riscv_reset_subsets_list_arch_str (void)
307 riscv_subset_list_t
*subsets
= riscv_rps_as
.subset_list
;
308 if (subsets
->arch_str
!= NULL
)
309 free ((void *) subsets
->arch_str
);
310 subsets
->arch_str
= riscv_arch_str (xlen
, subsets
);
313 /* This structure is used to hold a stack of .option values. */
314 struct riscv_option_stack
316 struct riscv_option_stack
*next
;
317 struct riscv_set_options options
;
318 riscv_subset_list_t
*subset_list
;
321 static struct riscv_option_stack
*riscv_opts_stack
= NULL
;
323 /* Set which ISA and extensions are available. */
326 riscv_set_arch (const char *s
)
328 if (s
!= NULL
&& strcmp (s
, "") == 0)
330 as_bad (_("the architecture string of -march and elf architecture "
331 "attributes cannot be empty"));
335 if (riscv_rps_as
.subset_list
== NULL
)
337 riscv_rps_as
.subset_list
= XNEW (riscv_subset_list_t
);
338 riscv_rps_as
.subset_list
->head
= NULL
;
339 riscv_rps_as
.subset_list
->tail
= NULL
;
340 riscv_rps_as
.subset_list
->arch_str
= NULL
;
342 riscv_release_subset_list (riscv_rps_as
.subset_list
);
343 riscv_parse_subset (&riscv_rps_as
, s
);
344 riscv_reset_subsets_list_arch_str ();
346 riscv_set_rvc (false);
347 if (riscv_subset_supports (&riscv_rps_as
, "c")
348 || riscv_subset_supports (&riscv_rps_as
, "zca"))
349 riscv_set_rvc (true);
351 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
355 /* Indicate -mabi option is explictly set. */
356 static bool explicit_mabi
= false;
358 /* Set the abi information. */
361 riscv_set_abi (unsigned new_xlen
, enum float_abi new_float_abi
, bool rve
)
364 float_abi
= new_float_abi
;
368 /* If the -mabi option isn't set, then set the abi according to the
369 ISA string. Otherwise, check if there is any conflict. */
372 riscv_set_abi_by_arch (void)
376 if (riscv_subset_supports (&riscv_rps_as
, "q"))
377 riscv_set_abi (xlen
, FLOAT_ABI_QUAD
, false);
378 else if (riscv_subset_supports (&riscv_rps_as
, "d"))
379 riscv_set_abi (xlen
, FLOAT_ABI_DOUBLE
, false);
380 else if (riscv_subset_supports (&riscv_rps_as
, "e"))
381 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, true);
383 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, false);
387 gas_assert (abi_xlen
!= 0 && xlen
!= 0 && float_abi
!= FLOAT_ABI_DEFAULT
);
389 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen
, xlen
);
390 else if (abi_xlen
< xlen
)
391 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen
, xlen
);
393 if (riscv_subset_supports (&riscv_rps_as
, "e") && !rve_abi
)
394 as_bad ("only ilp32e/lp64e ABI are supported for e extension");
396 if (float_abi
== FLOAT_ABI_SINGLE
397 && !riscv_subset_supports (&riscv_rps_as
, "f"))
398 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
400 else if (float_abi
== FLOAT_ABI_DOUBLE
401 && !riscv_subset_supports (&riscv_rps_as
, "d"))
402 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
404 else if (float_abi
== FLOAT_ABI_QUAD
405 && !riscv_subset_supports (&riscv_rps_as
, "q"))
406 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
410 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
411 elf_flags
&= ~EF_RISCV_FLOAT_ABI
;
412 elf_flags
|= float_abi
<< 1;
415 elf_flags
|= EF_RISCV_RVE
;
418 /* Handle of the OPCODE hash table. */
419 static htab_t op_hash
= NULL
;
421 /* Handle of the type of .insn hash table. */
422 static htab_t insn_type_hash
= NULL
;
424 /* This array holds the chars that always start a comment. If the
425 pre-processor is disabled, these aren't very useful. */
426 const char comment_chars
[] = "#";
428 /* This array holds the chars that only start a comment at the beginning of
429 a line. If the line seems to have the form '# 123 filename'
430 .line and .file directives will appear in the pre-processed output
432 Note that input_file.c hand checks for '#' at the beginning of the
433 first line of the input file. This is because the compiler outputs
434 #NO_APP at the beginning of its output.
436 Also note that C style comments are always supported. */
437 const char line_comment_chars
[] = "#";
439 /* This array holds machine specific line separator characters. */
440 const char line_separator_chars
[] = ";";
442 /* Chars that can be used to separate mant from exp in floating point nums. */
443 const char EXP_CHARS
[] = "eE";
445 /* Chars that mean this number is a floating point constant.
446 As in 0f12.456 or 0d1.2345e12. */
447 const char FLT_CHARS
[] = "rRsSfFdDxXpPhH";
449 /* Indicate we are already assemble any instructions or not. */
450 static bool start_assemble
= false;
452 /* Indicate ELF attributes are explicitly set. */
453 static bool explicit_attr
= false;
455 /* Indicate CSR or priv instructions are explicitly used. */
456 static bool explicit_priv_attr
= false;
458 static char *expr_parse_end
;
460 /* Macros for encoding relaxation state for RVC branches and far jumps. */
461 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
464 | ((uncond) ? 1 : 0) \
467 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
468 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
469 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
470 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
472 /* Is the given value a sign-extended 32-bit value? */
473 #define IS_SEXT_32BIT_NUM(x) \
474 (((x) &~ (offsetT) 0x7fffffff) == 0 \
475 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
477 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
478 #define IS_ZEXT_32BIT_NUM(x) \
479 (((x) &~ (offsetT) 0xffffffff) == 0 \
480 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
482 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
483 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
484 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
485 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
487 #define INSERT_IMM(n, s, INSN, VALUE) \
488 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
490 /* Determine if an instruction matches an opcode. */
491 #define OPCODE_MATCHES(OPCODE, OP) \
492 (((OPCODE) & MASK_##OP) == MATCH_##OP)
494 /* Create a new mapping symbol for the transition to STATE. */
497 make_mapping_symbol (enum riscv_seg_mstate state
,
500 const char *arch_str
,
501 bool odd_data_padding
)
511 if (arch_str
!= NULL
)
513 size_t size
= strlen (arch_str
) + 3; /* "$x" + '\0' */
514 buff
= xmalloc (size
);
515 snprintf (buff
, size
, "$x%s", arch_str
);
525 symbolS
*symbol
= symbol_new (name
, now_seg
, frag
, value
);
526 symbol_get_bfdsym (symbol
)->flags
|= (BSF_NO_FLAGS
| BSF_LOCAL
);
527 if (arch_str
!= NULL
)
529 /* Store current $x+arch into tc_segment_info. */
530 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
= symbol
;
531 xfree ((void *) buff
);
534 /* If .fill or other data filling directive generates zero sized data,
535 then mapping symbol for the following code will have the same value.
537 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
538 and .text.zero.fill.last. */
539 symbolS
*first
= frag
->tc_frag_data
.first_map_symbol
;
540 symbolS
*last
= frag
->tc_frag_data
.last_map_symbol
;
541 symbolS
*removed
= NULL
;
546 know (S_GET_VALUE (first
) == S_GET_VALUE (symbol
)
548 /* Remove the old one. */
551 frag
->tc_frag_data
.first_map_symbol
= symbol
;
553 else if (last
!= NULL
)
555 /* The mapping symbols should be added in offset order. */
556 know (S_GET_VALUE (last
) <= S_GET_VALUE (symbol
));
557 /* Remove the old one. */
558 if (S_GET_VALUE (last
) == S_GET_VALUE (symbol
))
561 frag
->tc_frag_data
.last_map_symbol
= symbol
;
566 if (odd_data_padding
)
568 /* If the removed mapping symbol is $x+arch, then add it back to
570 const char *str
= strncmp (S_GET_NAME (removed
), "$xrv", 4) == 0
571 ? S_GET_NAME (removed
) + 2 : NULL
;
572 make_mapping_symbol (MAP_INSN
, frag
->fr_fix
+ 1, frag
, str
,
573 false/* odd_data_padding */);
575 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
578 /* Set the mapping state for frag_now. */
581 riscv_mapping_state (enum riscv_seg_mstate to_state
,
585 enum riscv_seg_mstate from_state
=
586 seg_info (now_seg
)->tc_segment_info_data
.map_state
;
587 bool reset_seg_arch_str
= false;
589 if (!SEG_NORMAL (now_seg
)
590 /* For now we only add the mapping symbols to text sections.
591 Therefore, the dis-assembler only show the actual contents
592 distribution for text. Other sections will be shown as
593 data without the details. */
594 || !subseg_text_p (now_seg
))
597 /* The mapping symbol should be emitted if not in the right
599 symbolS
*seg_arch_symbol
=
600 seg_info (now_seg
)->tc_segment_info_data
.arch_map_symbol
;
601 if (to_state
== MAP_INSN
&& seg_arch_symbol
== 0)
603 /* Always add $x+arch at the first instruction of section. */
604 reset_seg_arch_str
= true;
606 else if (seg_arch_symbol
!= 0
607 && to_state
== MAP_INSN
609 && strcmp (riscv_rps_as
.subset_list
->arch_str
,
610 S_GET_NAME (seg_arch_symbol
) + 2) != 0)
612 reset_seg_arch_str
= true;
614 else if (from_state
== to_state
)
617 valueT value
= (valueT
) (frag_now_fix () - max_chars
);
618 seg_info (now_seg
)->tc_segment_info_data
.map_state
= to_state
;
619 const char *arch_str
= reset_seg_arch_str
620 ? riscv_rps_as
.subset_list
->arch_str
: NULL
;
621 make_mapping_symbol (to_state
, value
, frag_now
, arch_str
,
622 false/* odd_data_padding */);
625 /* Add the odd bytes of paddings for riscv_handle_align. */
628 riscv_add_odd_padding_symbol (fragS
*frag
)
630 /* If there was already a mapping symbol, it should be
631 removed in the make_mapping_symbol.
633 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
634 make_mapping_symbol (MAP_DATA
, frag
->fr_fix
, frag
,
635 NULL
/* arch_str */, true/* odd_data_padding */);
638 /* Remove any excess mapping symbols generated for alignment frags in
639 SEC. We may have created a mapping symbol before a zero byte
640 alignment; remove it if there's a mapping symbol after the
644 riscv_check_mapping_symbols (bfd
*abfd ATTRIBUTE_UNUSED
,
646 void *dummy ATTRIBUTE_UNUSED
)
648 segment_info_type
*seginfo
= seg_info (sec
);
651 if (seginfo
== NULL
|| seginfo
->frchainP
== NULL
)
654 for (fragp
= seginfo
->frchainP
->frch_root
;
656 fragp
= fragp
->fr_next
)
658 symbolS
*last
= fragp
->tc_frag_data
.last_map_symbol
;
659 fragS
*next
= fragp
->fr_next
;
661 if (last
== NULL
|| next
== NULL
)
664 /* Check the last mapping symbol if it is at the boundary of
666 if (S_GET_VALUE (last
) < next
->fr_address
)
668 know (S_GET_VALUE (last
) == next
->fr_address
);
672 symbolS
*next_first
= next
->tc_frag_data
.first_map_symbol
;
673 if (next_first
!= NULL
)
675 /* The last mapping symbol overlaps with another one
676 which at the start of the next frag.
678 Please see the gas/testsuite/gas/riscv/mapping.s:
679 .text.zero.fill.align.A and .text.zero.fill.align.B. */
680 know (S_GET_VALUE (last
) == S_GET_VALUE (next_first
));
681 symbolS
*removed
= last
;
682 if (strncmp (S_GET_NAME (last
), "$xrv", 4) == 0
683 && strcmp (S_GET_NAME (next_first
), "$x") == 0)
684 removed
= next_first
;
685 symbol_remove (removed
, &symbol_rootP
, &symbol_lastP
);
689 if (next
->fr_next
== NULL
)
691 /* The last mapping symbol is at the end of the section.
693 Please see the gas/testsuite/gas/riscv/mapping.s:
694 .text.last.section. */
695 know (next
->fr_fix
== 0 && next
->fr_var
== 0);
696 symbol_remove (last
, &symbol_rootP
, &symbol_lastP
);
700 /* Since we may have empty frags without any mapping symbols,
701 keep looking until the non-empty frag. */
702 if (next
->fr_address
!= next
->fr_next
->fr_address
)
705 next
= next
->fr_next
;
707 while (next
!= NULL
);
711 /* The default target format to use. */
714 riscv_target_format (void)
716 if (target_big_endian
)
717 return xlen
== 64 ? "elf64-bigriscv" : "elf32-bigriscv";
719 return xlen
== 64 ? "elf64-littleriscv" : "elf32-littleriscv";
722 /* Return the length of instruction INSN. */
724 static inline unsigned int
725 insn_length (const struct riscv_cl_insn
*insn
)
727 return riscv_insn_length (insn
->insn_opcode
);
730 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
733 create_insn (struct riscv_cl_insn
*insn
, const struct riscv_opcode
*mo
)
736 insn
->insn_opcode
= mo
->match
;
737 insn
->insn_long_opcode
[0] = 0;
743 /* Install INSN at the location specified by its "frag" and "where" fields. */
746 install_insn (const struct riscv_cl_insn
*insn
)
748 char *f
= insn
->frag
->fr_literal
+ insn
->where
;
749 if (insn
->insn_long_opcode
[0] != 0)
750 memcpy (f
, insn
->insn_long_opcode
, insn_length (insn
));
752 number_to_chars_littleendian (f
, insn
->insn_opcode
, insn_length (insn
));
755 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
756 and install the opcode in the new location. */
759 move_insn (struct riscv_cl_insn
*insn
, fragS
*frag
, long where
)
763 if (insn
->fixp
!= NULL
)
765 insn
->fixp
->fx_frag
= frag
;
766 insn
->fixp
->fx_where
= where
;
771 /* Add INSN to the end of the output. */
774 add_fixed_insn (struct riscv_cl_insn
*insn
)
776 char *f
= frag_more (insn_length (insn
));
777 move_insn (insn
, frag_now
, f
- frag_now
->fr_literal
);
781 add_relaxed_insn (struct riscv_cl_insn
*insn
, int max_chars
, int var
,
782 relax_substateT subtype
, symbolS
*symbol
, offsetT offset
)
784 frag_grow (max_chars
);
785 move_insn (insn
, frag_now
, frag_more (0) - frag_now
->fr_literal
);
786 frag_var (rs_machine_dependent
, max_chars
, var
,
787 subtype
, symbol
, offset
, NULL
);
790 /* Compute the length of a branch sequence, and adjust the stored length
791 accordingly. If FRAGP is NULL, the worst-case length is returned. */
794 relaxed_branch_length (fragS
*fragp
, asection
*sec
, int update
)
796 int jump
, rvc
, length
= 8;
801 jump
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
);
802 rvc
= RELAX_BRANCH_RVC (fragp
->fr_subtype
);
803 length
= RELAX_BRANCH_LENGTH (fragp
->fr_subtype
);
805 /* Assume jumps are in range; the linker will catch any that aren't. */
806 length
= jump
? 4 : 8;
808 if (fragp
->fr_symbol
!= NULL
809 && S_IS_DEFINED (fragp
->fr_symbol
)
810 && !S_IS_WEAK (fragp
->fr_symbol
)
811 && sec
== S_GET_SEGMENT (fragp
->fr_symbol
))
813 offsetT val
= S_GET_VALUE (fragp
->fr_symbol
) + fragp
->fr_offset
;
814 bfd_vma rvc_range
= jump
? RVC_JUMP_REACH
: RVC_BRANCH_REACH
;
815 val
-= fragp
->fr_address
+ fragp
->fr_fix
;
817 if (rvc
&& (bfd_vma
)(val
+ rvc_range
/2) < rvc_range
)
819 else if ((bfd_vma
)(val
+ RISCV_BRANCH_REACH
/2) < RISCV_BRANCH_REACH
)
821 else if (!jump
&& rvc
)
826 fragp
->fr_subtype
= RELAX_BRANCH_ENCODE (jump
, rvc
, length
);
831 /* Information about an opcode name, mnemonics and its value. */
838 /* List for all supported opcode name. */
839 static const struct opcode_name_t opcode_name_list
[] =
884 /* Hash table for lookup opcode name. */
885 static htab_t opcode_names_hash
= NULL
;
887 /* Initialization for hash table of opcode name. */
890 init_opcode_names_hash (void)
892 const struct opcode_name_t
*opcode
;
894 for (opcode
= &opcode_name_list
[0]; opcode
->name
!= NULL
; ++opcode
)
895 if (str_hash_insert (opcode_names_hash
, opcode
->name
, opcode
, 0) != NULL
)
896 as_fatal (_("internal: duplicate %s"), opcode
->name
);
899 /* Find `s` is a valid opcode name or not, return the opcode name info
902 static const struct opcode_name_t
*
903 opcode_name_lookup (char **s
)
907 struct opcode_name_t
*o
;
909 /* Find end of name. */
911 if (is_name_beginner (*e
))
913 while (is_part_of_name (*e
))
916 /* Terminate name. */
920 o
= (struct opcode_name_t
*) str_hash_find (opcode_names_hash
, *s
);
922 /* Advance to next token if one was recognized. */
932 /* All RISC-V registers belong to one of these classes. */
944 static htab_t reg_names_hash
= NULL
;
945 static htab_t csr_extra_hash
= NULL
;
947 #define ENCODE_REG_HASH(cls, n) \
948 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
949 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
950 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
953 hash_reg_name (enum reg_class
class, const char *name
, unsigned n
)
955 void *hash
= ENCODE_REG_HASH (class, n
);
956 if (str_hash_insert (reg_names_hash
, name
, hash
, 0) != NULL
)
957 as_fatal (_("internal: duplicate %s"), name
);
961 hash_reg_names (enum reg_class
class, const char names
[][NRC
], unsigned n
)
965 for (i
= 0; i
< n
; i
++)
966 hash_reg_name (class, names
[i
], i
);
969 /* Init hash table csr_extra_hash to handle CSR. */
972 riscv_init_csr_hash (const char *name
,
974 enum riscv_csr_class
class,
975 enum riscv_spec_class define_version
,
976 enum riscv_spec_class abort_version
)
978 struct riscv_csr_extra
*entry
, *pre_entry
;
979 bool need_enrty
= true;
982 entry
= (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, name
);
983 while (need_enrty
&& entry
!= NULL
)
985 if (entry
->csr_class
== class
986 && entry
->address
== address
987 && entry
->define_version
== define_version
988 && entry
->abort_version
== abort_version
)
998 entry
= notes_alloc (sizeof (*entry
));
999 entry
->csr_class
= class;
1000 entry
->address
= address
;
1001 entry
->define_version
= define_version
;
1002 entry
->abort_version
= abort_version
;
1005 if (pre_entry
== NULL
)
1006 str_hash_insert (csr_extra_hash
, name
, entry
, 0);
1008 pre_entry
->next
= entry
;
1011 /* Return the CSR address after checking the ISA dependency and
1012 the privileged spec version.
1014 There are one warning and two errors for CSR,
1016 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1017 or the privileged spec, report warning only if -mcsr-check is set.
1018 Unknown CSR: the CSR has never been defined, report error.
1019 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1022 riscv_csr_address (const char *csr_name
,
1023 struct riscv_csr_extra
*entry
)
1025 struct riscv_csr_extra
*saved_entry
= entry
;
1026 enum riscv_csr_class csr_class
= entry
->csr_class
;
1027 bool need_check_version
= false;
1028 bool is_rv32_only
= false;
1029 bool is_h_required
= false;
1030 const char* extension
= NULL
;
1034 case CSR_CLASS_I_32
:
1035 is_rv32_only
= true;
1038 need_check_version
= true;
1041 case CSR_CLASS_H_32
:
1042 is_rv32_only
= true;
1054 extension
= "zve32x";
1056 case CSR_CLASS_SMAIA_32
:
1057 is_rv32_only
= true;
1059 case CSR_CLASS_SMAIA
:
1060 extension
= "smaia";
1062 case CSR_CLASS_SMCNTRPMF_32
:
1063 is_rv32_only
= true;
1065 case CSR_CLASS_SMCNTRPMF
:
1066 need_check_version
= true;
1067 extension
= "smcntrpmf";
1069 case CSR_CLASS_SMSTATEEN_32
:
1070 is_rv32_only
= true;
1072 case CSR_CLASS_SMSTATEEN
:
1073 extension
= "smstateen";
1075 case CSR_CLASS_SSAIA
:
1076 case CSR_CLASS_SSAIA_AND_H
:
1077 case CSR_CLASS_SSAIA_32
:
1078 case CSR_CLASS_SSAIA_AND_H_32
:
1079 is_rv32_only
= (csr_class
== CSR_CLASS_SSAIA_32
1080 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1081 is_h_required
= (csr_class
== CSR_CLASS_SSAIA_AND_H
1082 || csr_class
== CSR_CLASS_SSAIA_AND_H_32
);
1083 extension
= "ssaia";
1085 case CSR_CLASS_SSSTATEEN_AND_H_32
:
1086 is_rv32_only
= true;
1088 case CSR_CLASS_SSSTATEEN_AND_H
:
1089 is_h_required
= true;
1091 case CSR_CLASS_SSSTATEEN
:
1092 extension
= "ssstateen";
1094 case CSR_CLASS_SSCOFPMF_32
:
1095 is_rv32_only
= true;
1097 case CSR_CLASS_SSCOFPMF
:
1098 extension
= "sscofpmf";
1100 case CSR_CLASS_SSTC
:
1101 case CSR_CLASS_SSTC_AND_H
:
1102 case CSR_CLASS_SSTC_32
:
1103 case CSR_CLASS_SSTC_AND_H_32
:
1104 is_rv32_only
= (csr_class
== CSR_CLASS_SSTC_32
1105 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1106 is_h_required
= (csr_class
== CSR_CLASS_SSTC_AND_H
1107 || csr_class
== CSR_CLASS_SSTC_AND_H_32
);
1110 case CSR_CLASS_DEBUG
:
1112 case CSR_CLASS_XTHEADVECTOR
:
1113 extension
= "xtheadvector";
1116 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class
);
1119 if (riscv_opts
.csr_check
)
1121 if (is_rv32_only
&& xlen
!= 32)
1122 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name
);
1123 if (is_h_required
&& !riscv_subset_supports (&riscv_rps_as
, "h"))
1124 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name
);
1126 if (extension
!= NULL
1127 && !riscv_subset_supports (&riscv_rps_as
, extension
))
1128 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1129 csr_name
, extension
);
1132 while (entry
!= NULL
)
1134 if (!need_check_version
1135 || (default_priv_spec
>= entry
->define_version
1136 && default_priv_spec
< entry
->abort_version
))
1138 /* Find the CSR according to the specific version. */
1139 return entry
->address
;
1141 entry
= entry
->next
;
1144 /* Can not find the CSR address from the chosen privileged version,
1145 so use the newly defined value. */
1146 if (riscv_opts
.csr_check
)
1148 const char *priv_name
= NULL
;
1149 RISCV_GET_PRIV_SPEC_NAME (priv_name
, default_priv_spec
);
1150 if (priv_name
!= NULL
)
1151 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1152 csr_name
, priv_name
);
1155 return saved_entry
->address
;
1158 /* Return -1 if the CSR has never been defined. Otherwise, return
1162 reg_csr_lookup_internal (const char *s
)
1164 struct riscv_csr_extra
*r
=
1165 (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, s
);
1170 return riscv_csr_address (s
, r
);
1174 reg_lookup_internal (const char *s
, enum reg_class
class)
1178 if (class == RCLASS_CSR
)
1179 return reg_csr_lookup_internal (s
);
1181 r
= str_hash_find (reg_names_hash
, s
);
1182 if (r
== NULL
|| DECODE_REG_CLASS (r
) != class)
1185 if (riscv_subset_supports (&riscv_rps_as
, "e")
1186 && class == RCLASS_GPR
1187 && DECODE_REG_NUM (r
) > 15)
1190 return DECODE_REG_NUM (r
);
1194 reg_lookup (char **s
, enum reg_class
class, unsigned int *regnop
)
1200 /* Find end of name. */
1202 if (is_name_beginner (*e
))
1204 while (is_part_of_name (*e
))
1207 /* Terminate name. */
1211 /* Look for the register. Advance to next token if one was recognized. */
1212 if ((reg
= reg_lookup_internal (*s
, class)) >= 0)
1222 arg_lookup (char **s
, const char *const *array
, size_t size
, unsigned *regnop
)
1224 const char *p
= strchr (*s
, ',');
1225 size_t i
, len
= p
? (size_t)(p
- *s
) : strlen (*s
);
1230 for (i
= 0; i
< size
; i
++)
1231 if (array
[i
] != NULL
&& strncmp (array
[i
], *s
, len
) == 0
1232 && array
[i
][len
] == '\0')
1243 flt_lookup (float f
, const float *array
, size_t size
, unsigned *regnop
)
1247 for (i
= 0; i
< size
; i
++)
1257 /* Map ra and s-register to [4,15], so that we can check if the
1258 reg2 in register list reg1-reg2 or single reg2 is valid or not,
1259 and obtain the corresponding reg_list value.
1269 regno_to_reg_list (unsigned regno
)
1273 else if (regno
== X_S0
|| regno
== X_S1
)
1274 return 5 + regno
- X_S0
;
1275 else if (regno
>= X_S2
&& regno
< X_S10
)
1276 return 7 + regno
- X_S2
;
1277 else if (regno
== X_S11
)
1280 /* Invalid symbol. */
1284 /* Parse register list, and return the last register by regno_to_reg_list.
1286 If ABI register names are used (e.g. ra and s0), the register
1287 list could be "{ra}", "{ra, s0}", "{ra, s0-sN}", where 0 < N < 10 or
1290 If numeric register names are used (e.g. x1 and x8), the register list
1291 could be "{x1}", "{x1,x8}", "{x1,x8-x9}", "{x1,x8-x9,x18}" and
1292 "{x1,x8-x9,x18-xN}", where 19 < N < 25 or N == 27.
1294 The numeric and ABI register names cannot be used at the same time.
1296 TODO: Report errors for the following cases,
1297 1. Too many registers in the list.
1298 2. Cases which return 0.
1299 3. Illegal formats, for example, {x1,x8-NULL,x18-x24/x18}, {x1-x2,x8}. */
1302 reglist_lookup_internal (char *reglist
)
1305 unsigned reg_list
= 0;
1306 char *regname
[3][2] = {{NULL
}};
1307 char *save_tok
, *save_subtok
;
1310 char *token
= strtok_r (reglist
, ",", &save_tok
);
1311 for (i
= 0; i
< 3 && token
!= NULL
;
1312 token
= strtok_r (NULL
, ",", &save_tok
), i
++)
1314 char *subtoken
= strtok_r (token
, "-", &save_subtok
);
1315 for (j
= 0; j
< 2 && subtoken
!= NULL
;
1316 subtoken
= strtok_r (NULL
, "-", &save_subtok
), j
++)
1317 regname
[i
][j
] = subtoken
;
1320 bool reg1_numeric
= false;
1321 for (i
= 0; i
< 3; i
++)
1323 if (regname
[i
][0] == NULL
)
1325 #define REG_TO_REG_LIST(NAME, NUM, LIST) \
1326 (reg_lookup (&NAME, RCLASS_GPR, &NUM) && (LIST = regno_to_reg_list (NUM)))
1327 #define REG_NUMERIC(NAME) (NAME[0] == 'x')
1328 #define REG_CONFLICT(NAME, REG_NUMERIC) \
1329 ((NAME[0] == 'x' && !REG_NUMERIC) || (NAME[0] != 'x' && REG_NUMERIC))
1333 reg1_numeric
= REG_NUMERIC (regname
[i
][0]);
1334 if (!REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1339 if (REG_CONFLICT (regname
[i
][0], reg1_numeric
)
1340 /* The second register should be s0 or its numeric names x8. */
1341 || !REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1344 else if (regname
[i
][1] == NULL
)
1347 if (REG_CONFLICT (regname
[i
][1], reg1_numeric
)
1348 /* The third register is x9 if the numeric name is used.
1349 Otherwise, it could be any other sN register, where N > 0. */
1350 || !REG_TO_REG_LIST (regname
[i
][1], regno
, reg_list
)
1352 || (reg1_numeric
&& regno
!= X_S1
))
1356 /* Must use register numeric names. */
1358 || !REG_NUMERIC (regname
[i
][0])
1359 /* The fourth register should be s2. */
1360 || !REG_TO_REG_LIST (regname
[i
][0], regno
, reg_list
)
1363 else if (regname
[i
][1] == NULL
)
1367 || !REG_NUMERIC (regname
[i
][1])
1368 /* The fifth register could be any other sN register, where N > 1. */
1369 || !REG_TO_REG_LIST (regname
[i
][1], regno
, reg_list
)
1376 #undef REG_TO_REG_LIST
1383 /* Parse register list. Return false if REG_LIST is zero, which is an
1387 reglist_lookup (char **s
, unsigned *reg_list
)
1390 char *reglist
= strdup (*s
);
1391 if (reglist
!= NULL
)
1393 char *token
= strtok (reglist
, "}");
1396 *s
+= strlen (token
);
1397 *reg_list
= reglist_lookup_internal (reglist
);
1401 as_bad (_("cannot find `}' for cm.push/cm.pop"));
1406 return *reg_list
== 0 ? false : true;
1409 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1410 #define USE_IMM(n, s) \
1411 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1413 /* For consistency checking, verify that all bits are specified either
1414 by the match/mask part of the instruction definition, or by the
1415 operand list. The `length` could be the actual instruction length or
1416 0 for auto-detection. */
1419 validate_riscv_insn (const struct riscv_opcode
*opc
, int length
)
1421 const char *oparg
, *opargStart
;
1422 insn_t used_bits
= opc
->mask
;
1424 insn_t required_bits
;
1427 length
= riscv_insn_length (opc
->match
);
1428 /* We don't support instructions longer than 64-bits yet. */
1431 insn_width
= 8 * length
;
1433 required_bits
= ((insn_t
)~0ULL) >> (64 - insn_width
);
1435 if ((used_bits
& opc
->match
) != (opc
->match
& required_bits
))
1437 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1438 opc
->name
, opc
->args
);
1442 for (oparg
= opc
->args
; *oparg
; ++oparg
)
1450 case 'U': break; /* CRS1, constrained to equal RD. */
1451 case 'c': break; /* CRS1, constrained to equal sp. */
1452 case 'T': /* CRS2, floating point. */
1453 case 'V': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
1454 case 'S': /* CRS1S, floating point. */
1455 case 's': USE_BITS (OP_MASK_CRS1S
, OP_SH_CRS1S
); break;
1456 case 'w': break; /* CRS1S, constrained to equal RD. */
1457 case 'D': /* CRS2S, floating point. */
1458 case 't': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
1459 case 'x': break; /* CRS2S, constrained to equal RD. */
1460 case 'z': break; /* CRS2S, constrained to be x0. */
1461 case '>': /* CITYPE immediate, compressed shift. */
1462 case 'u': /* CITYPE immediate, compressed lui. */
1463 case 'v': /* CITYPE immediate, li to compressed lui. */
1464 case 'o': /* CITYPE immediate, allow zero. */
1465 case 'j': used_bits
|= ENCODE_CITYPE_IMM (-1U); break;
1466 case 'L': used_bits
|= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1467 case 'm': used_bits
|= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1468 case 'n': used_bits
|= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1469 case '6': used_bits
|= ENCODE_CSSTYPE_IMM (-1U); break;
1470 case 'M': used_bits
|= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1471 case 'N': used_bits
|= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1472 case '8': used_bits
|= ENCODE_CIWTYPE_IMM (-1U); break;
1473 case 'K': used_bits
|= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1474 /* CLTYPE and CSTYPE have the same immediate encoding. */
1475 case '5': used_bits
|= ENCODE_CLTYPE_IMM (-1U); break;
1476 case 'k': used_bits
|= ENCODE_CLTYPE_LW_IMM (-1U); break;
1477 case 'l': used_bits
|= ENCODE_CLTYPE_LD_IMM (-1U); break;
1478 case 'p': used_bits
|= ENCODE_CBTYPE_IMM (-1U); break;
1479 case 'a': used_bits
|= ENCODE_CJTYPE_IMM (-1U); break;
1480 case 'F': /* Compressed funct for .insn directive. */
1483 case '6': USE_BITS (OP_MASK_CFUNCT6
, OP_SH_CFUNCT6
); break;
1484 case '4': USE_BITS (OP_MASK_CFUNCT4
, OP_SH_CFUNCT4
); break;
1485 case '3': USE_BITS (OP_MASK_CFUNCT3
, OP_SH_CFUNCT3
); break;
1486 case '2': USE_BITS (OP_MASK_CFUNCT2
, OP_SH_CFUNCT2
); break;
1488 goto unknown_validate_operand
;
1492 goto unknown_validate_operand
;
1494 break; /* end RVC */
1499 case 'f': USE_BITS (OP_MASK_VD
, OP_SH_VD
); break;
1500 case 'e': USE_BITS (OP_MASK_VWD
, OP_SH_VWD
); break;
1501 case 's': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
); break;
1502 case 't': USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1503 case 'u': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1504 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1505 case 'v': USE_BITS (OP_MASK_VD
, OP_SH_VD
);
1506 USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1507 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1509 case 'b': used_bits
|= ENCODE_RVV_VB_IMM (-1U); break;
1510 case 'c': used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1513 case 'k': USE_BITS (OP_MASK_VIMM
, OP_SH_VIMM
); break;
1514 case 'l': used_bits
|= ENCODE_RVV_VI_UIMM6 (-1U); break;
1515 case 'm': USE_BITS (OP_MASK_VMASK
, OP_SH_VMASK
); break;
1516 case 'M': break; /* Macro operand, must be a mask register. */
1517 case 'T': break; /* Macro operand, must be a vector register. */
1519 goto unknown_validate_operand
;
1521 break; /* end RVV */
1527 case '<': USE_BITS (OP_MASK_SHAMTW
, OP_SH_SHAMTW
); break;
1528 case '>': USE_BITS (OP_MASK_SHAMT
, OP_SH_SHAMT
); break;
1529 case 'A': break; /* Macro operand, must be symbol. */
1530 case 'B': break; /* Macro operand, must be symbol or constant. */
1531 case 'c': break; /* Macro operand, must be symbol or constant. */
1532 case 'I': break; /* Macro operand, must be constant. */
1533 case 'D': /* RD, floating point. */
1534 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1535 case 'y': USE_BITS (OP_MASK_BS
, OP_SH_BS
); break;
1536 case 'Y': USE_BITS (OP_MASK_RNUM
, OP_SH_RNUM
); break;
1537 case 'Z': /* RS1, CSR number. */
1538 case 'S': /* RS1, floating point. */
1539 case 's': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1540 case 'U': /* RS1 and RS2 are the same, floating point. */
1541 USE_BITS (OP_MASK_RS1
, OP_SH_RS1
);
1543 case 'T': /* RS2, floating point. */
1544 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1545 case 'R': /* RS3, floating point. */
1546 case 'r': USE_BITS (OP_MASK_RS3
, OP_SH_RS3
); break;
1547 case 'm': USE_BITS (OP_MASK_RM
, OP_SH_RM
); break;
1548 case 'E': USE_BITS (OP_MASK_CSR
, OP_SH_CSR
); break;
1549 case 'P': USE_BITS (OP_MASK_PRED
, OP_SH_PRED
); break;
1550 case 'Q': USE_BITS (OP_MASK_SUCC
, OP_SH_SUCC
); break;
1551 case 'o': /* ITYPE immediate, load displacement. */
1552 case 'j': used_bits
|= ENCODE_ITYPE_IMM (-1U); break;
1553 case 'a': used_bits
|= ENCODE_JTYPE_IMM (-1U); break;
1554 case 'p': used_bits
|= ENCODE_BTYPE_IMM (-1U); break;
1555 case 'q': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1556 case 'u': used_bits
|= ENCODE_UTYPE_IMM (-1U); break;
1557 case 'z': break; /* Zero immediate. */
1558 case '[': break; /* Unused operand. */
1559 case ']': break; /* Unused operand. */
1560 case '0': break; /* AMO displacement, must to zero. */
1561 case '1': break; /* Relaxation operand. */
1562 case 'F': /* Funct for .insn directive. */
1565 case '7': USE_BITS (OP_MASK_FUNCT7
, OP_SH_FUNCT7
); break;
1566 case '3': USE_BITS (OP_MASK_FUNCT3
, OP_SH_FUNCT3
); break;
1567 case '2': USE_BITS (OP_MASK_FUNCT2
, OP_SH_FUNCT2
); break;
1569 goto unknown_validate_operand
;
1572 case 'O': /* Opcode for .insn directive. */
1575 case '4': USE_BITS (OP_MASK_OP
, OP_SH_OP
); break;
1576 case '2': USE_BITS (OP_MASK_OP2
, OP_SH_OP2
); break;
1578 goto unknown_validate_operand
;
1581 case 'W': /* Various operands for standard z extensions. */
1587 case 'f': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1589 goto unknown_validate_operand
;
1595 case 'v': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1597 goto unknown_validate_operand
;
1603 /* byte immediate operators, load/store byte insns. */
1604 case 'h': used_bits
|= ENCODE_ZCB_HALFWORD_UIMM (-1U); break;
1605 /* halfword immediate operators, load/store halfword insns. */
1606 case 'b': used_bits
|= ENCODE_ZCB_BYTE_UIMM (-1U); break;
1607 /* Immediate offset operand for cm.push and cm.pop. */
1608 case 'p': used_bits
|= ENCODE_ZCMP_SPIMM (-1U); break;
1609 /* Register list operand for cm.push and cm.pop. */
1610 case 'r': USE_BITS (OP_MASK_REG_LIST
, OP_SH_REG_LIST
); break;
1613 goto unknown_validate_operand
;
1617 goto unknown_validate_operand
;
1620 case 'X': /* Vendor-specific operands. */
1623 case 't': /* Vendor-specific (T-head) operands. */
1632 case 'c': /* Vtypei for th.vsetvli. */
1633 used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1635 goto unknown_validate_operand
;
1638 case 'l': /* Integer immediate, literal. */
1639 oparg
+= strcspn(oparg
, ",") - 1;
1641 case 's': /* Integer immediate, 'XtsN@S' ... N-bit signed immediate at bit S. */
1643 case 'u': /* Integer immediate, 'XtuN@S' ... N-bit unsigned immediate at bit S. */
1646 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1648 goto unknown_validate_operand
;
1649 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
1655 goto unknown_validate_operand
;
1659 case 'c': /* Vendor-specific (CORE-V) operands. */
1664 used_bits
|= ENCODE_CV_IS2_UIMM5 (-1U);
1667 used_bits
|= ENCODE_CV_IS3_UIMM5 (-1U);
1670 goto unknown_validate_operand
;
1673 case 's': /* Vendor-specific (SiFive) operands. */
1676 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1677 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1681 case '2': USE_BITS (OP_MASK_XSO2
, OP_SH_XSO2
); break;
1682 case '1': USE_BITS (OP_MASK_XSO1
, OP_SH_XSO1
); break;
1684 goto unknown_validate_operand
;
1688 goto unknown_validate_operand
;
1692 goto unknown_validate_operand
;
1696 unknown_validate_operand
:
1697 as_bad (_("internal: bad RISC-V opcode "
1698 "(unknown operand type `%s'): %s %s"),
1699 opargStart
, opc
->name
, opc
->args
);
1704 if (used_bits
!= required_bits
)
1706 as_bad (_("internal: bad RISC-V opcode "
1707 "(bits %#llx undefined or invalid): %s %s"),
1708 (unsigned long long)(used_bits
^ required_bits
),
1709 opc
->name
, opc
->args
);
1717 struct percent_op_match
1720 bfd_reloc_code_real_type reloc
;
1723 /* Common hash table initialization function for instruction and .insn
1727 init_opcode_hash (const struct riscv_opcode
*opcodes
,
1728 bool insn_directive_p
)
1732 htab_t hash
= str_htab_create ();
1733 while (opcodes
[i
].name
)
1735 const char *name
= opcodes
[i
].name
;
1736 if (str_hash_insert (hash
, name
, &opcodes
[i
], 0) != NULL
)
1737 as_fatal (_("internal: duplicate %s"), name
);
1741 if (opcodes
[i
].pinfo
!= INSN_MACRO
)
1743 if (insn_directive_p
)
1744 length
= ((name
[0] == 'c') ? 2 : 4);
1746 length
= 0; /* Let assembler determine the length. */
1747 if (!validate_riscv_insn (&opcodes
[i
], length
))
1748 as_fatal (_("internal: broken assembler. "
1749 "No assembly attempted"));
1752 gas_assert (!insn_directive_p
);
1755 while (opcodes
[i
].name
&& !strcmp (opcodes
[i
].name
, name
));
1761 /* Record all PC-relative high-part relocation that we have encountered to
1762 help us resolve the corresponding low-part relocation later. */
1768 } riscv_pcrel_hi_fixup
;
1770 /* Handle of the pcrel_hi hash table. */
1771 static htab_t riscv_pcrel_hi_fixup_hash
;
1773 /* Get the key of a entry from the pcrel_hi hash table. */
1776 riscv_pcrel_fixup_hash (const void *entry
)
1778 const riscv_pcrel_hi_fixup
*e
= entry
;
1779 return (hashval_t
) (e
->address
);
1782 /* Compare the keys between two entries fo the pcrel_hi hash table. */
1785 riscv_pcrel_fixup_eq (const void *entry1
, const void *entry2
)
1787 const riscv_pcrel_hi_fixup
*e1
= entry1
, *e2
= entry2
;
1788 return e1
->address
== e2
->address
;
1791 /* Record the pcrel_hi relocation. */
1794 riscv_record_pcrel_fixup (htab_t p
, bfd_vma address
, symbolS
*symbol
,
1797 riscv_pcrel_hi_fixup entry
= {address
, symbol
, target
};
1798 riscv_pcrel_hi_fixup
**slot
=
1799 (riscv_pcrel_hi_fixup
**) htab_find_slot (p
, &entry
, INSERT
);
1803 *slot
= (riscv_pcrel_hi_fixup
*) xmalloc (sizeof (riscv_pcrel_hi_fixup
));
1810 /* This function is called once, at assembler startup time. It should set up
1811 all the tables, etc. that the MD part of the assembler will need. */
1816 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
1818 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
1819 as_warn (_("could not set architecture and machine"));
1821 op_hash
= init_opcode_hash (riscv_opcodes
, false);
1822 insn_type_hash
= init_opcode_hash (riscv_insn_types
, true);
1824 reg_names_hash
= str_htab_create ();
1825 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_numeric
, NGPR
);
1826 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_abi
, NGPR
);
1827 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_numeric
, NFPR
);
1828 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_abi
, NFPR
);
1829 hash_reg_names (RCLASS_VECR
, riscv_vecr_names_numeric
, NVECR
);
1830 hash_reg_names (RCLASS_VECM
, riscv_vecm_names_numeric
, NVECM
);
1831 /* Add "fp" as an alias for "s0". */
1832 hash_reg_name (RCLASS_GPR
, "fp", 8);
1834 /* Create and insert CSR hash tables. */
1835 csr_extra_hash
= str_htab_create ();
1836 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1837 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1838 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1839 DECLARE_CSR(name, num, class, define_version, abort_version);
1840 #include "opcode/riscv-opc.h"
1843 opcode_names_hash
= str_htab_create ();
1844 init_opcode_names_hash ();
1846 /* Create pcrel_hi hash table to resolve the relocation while with
1848 riscv_pcrel_hi_fixup_hash
= htab_create (1024, riscv_pcrel_fixup_hash
,
1849 riscv_pcrel_fixup_eq
, free
);
1851 /* Set the default alignment for the text section. */
1852 record_alignment (text_section
, riscv_opts
.rvc
? 1 : 2);
1856 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type
, bfd_vma value
)
1863 case BFD_RELOC_RISCV_HI20
:
1864 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1866 case BFD_RELOC_RISCV_LO12_S
:
1867 return ENCODE_STYPE_IMM (value
);
1869 case BFD_RELOC_RISCV_LO12_I
:
1870 return ENCODE_ITYPE_IMM (value
);
1877 /* Output an instruction. IP is the instruction information.
1878 ADDRESS_EXPR is an operand of the instruction to be used with
1882 append_insn (struct riscv_cl_insn
*ip
, expressionS
*address_expr
,
1883 bfd_reloc_code_real_type reloc_type
)
1885 dwarf2_emit_insn (0);
1887 if (reloc_type
!= BFD_RELOC_UNUSED
)
1889 reloc_howto_type
*howto
;
1891 gas_assert (address_expr
);
1892 if (reloc_type
== BFD_RELOC_12_PCREL
1893 || reloc_type
== BFD_RELOC_RISCV_JMP
)
1895 int j
= reloc_type
== BFD_RELOC_RISCV_JMP
;
1896 int best_case
= insn_length (ip
);
1897 unsigned worst_case
= relaxed_branch_length (NULL
, NULL
, 0);
1899 if (now_seg
== absolute_section
)
1901 as_bad (_("relaxable branches not supported in absolute section"));
1905 add_relaxed_insn (ip
, worst_case
, best_case
,
1906 RELAX_BRANCH_ENCODE (j
, best_case
== 2, worst_case
),
1907 address_expr
->X_add_symbol
,
1908 address_expr
->X_add_number
);
1913 howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1915 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1918 ip
->fixp
= fix_new_exp (ip
->frag
, ip
->where
,
1919 bfd_get_reloc_size (howto
),
1920 address_expr
, false, reloc_type
);
1922 ip
->fixp
->fx_tcbit
= riscv_opts
.relax
;
1923 ip
->fixp
->tc_fix_data
.source_macro
= source_macro
;
1927 add_fixed_insn (ip
);
1929 /* We need to start a new frag after any instruction that can be
1930 optimized away or compressed by the linker during relaxation, to prevent
1931 the assembler from computing static offsets across such an instruction.
1932 This is necessary to get correct EH info. */
1933 if (reloc_type
== BFD_RELOC_RISCV_HI20
1934 || reloc_type
== BFD_RELOC_RISCV_PCREL_HI20
1935 || reloc_type
== BFD_RELOC_RISCV_TPREL_HI20
1936 || reloc_type
== BFD_RELOC_RISCV_TPREL_ADD
)
1938 frag_wane (frag_now
);
1943 /* Build an instruction created by a macro expansion. This is passed
1944 a pointer to the count of instructions created so far, an expression,
1945 the name of the instruction to build, an operand format string, and
1946 corresponding arguments. */
1949 macro_build (expressionS
*ep
, const char *name
, const char *fmt
, ...)
1951 const struct riscv_opcode
*mo
;
1952 struct riscv_cl_insn insn
;
1953 bfd_reloc_code_real_type r
;
1955 const char *fmtStart
;
1957 va_start (args
, fmt
);
1959 r
= BFD_RELOC_UNUSED
;
1960 mo
= (struct riscv_opcode
*) str_hash_find (op_hash
, name
);
1963 /* Find a non-RVC variant of the instruction. append_insn will compress
1965 while (riscv_insn_length (mo
->match
) < 4)
1967 gas_assert (strcmp (name
, mo
->name
) == 0);
1969 create_insn (&insn
, mo
);
1979 INSERT_OPERAND (VD
, insn
, va_arg (args
, int));
1982 INSERT_OPERAND (VS1
, insn
, va_arg (args
, int));
1985 INSERT_OPERAND (VS2
, insn
, va_arg (args
, int));
1989 int reg
= va_arg (args
, int);
1992 INSERT_OPERAND (VMASK
, insn
, 1);
1997 INSERT_OPERAND (VMASK
, insn
, 0);
2001 goto unknown_macro_argument
;
2004 goto unknown_macro_argument
;
2009 INSERT_OPERAND (RD
, insn
, va_arg (args
, int));
2012 INSERT_OPERAND (RS1
, insn
, va_arg (args
, int));
2015 INSERT_OPERAND (RS2
, insn
, va_arg (args
, int));
2021 gas_assert (ep
!= NULL
);
2022 r
= va_arg (args
, int);
2030 unknown_macro_argument
:
2031 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart
);
2036 gas_assert (r
== BFD_RELOC_UNUSED
? ep
== NULL
: ep
!= NULL
);
2038 append_insn (&insn
, ep
, r
);
2041 /* Build an instruction created by a macro expansion. Like md_assemble but
2042 accept a printf-style format string and arguments. */
2045 md_assemblef (const char *format
, ...)
2051 va_start (ap
, format
);
2053 r
= vasprintf (&buf
, format
, ap
);
2056 as_fatal (_("internal: vasprintf failed"));
2064 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
2068 normalize_constant_expr (expressionS
*ex
)
2072 if ((ex
->X_op
== O_constant
|| ex
->X_op
== O_symbol
)
2073 && IS_ZEXT_32BIT_NUM (ex
->X_add_number
))
2074 ex
->X_add_number
= (((ex
->X_add_number
& 0xffffffff) ^ 0x80000000)
2078 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
2079 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
2082 check_absolute_expr (struct riscv_cl_insn
*ip
, expressionS
*ex
,
2085 if (ex
->X_op
== O_big
)
2086 as_bad (_("unsupported large constant"));
2087 else if (maybe_csr
&& ex
->X_op
== O_symbol
)
2088 as_bad (_("unknown CSR `%s'"),
2089 S_GET_NAME (ex
->X_add_symbol
));
2090 else if (ex
->X_op
!= O_constant
)
2091 as_bad (_("instruction %s requires absolute expression"),
2093 normalize_constant_expr (ex
);
2097 make_internal_label (void)
2099 return (symbolS
*) local_symbol_make (FAKE_LABEL_NAME
, now_seg
, frag_now
,
2103 /* Load an entry from the GOT. */
2106 pcrel_access (int destreg
, int tempreg
, expressionS
*ep
,
2107 const char *lo_insn
, const char *lo_pattern
,
2108 bfd_reloc_code_real_type hi_reloc
,
2109 bfd_reloc_code_real_type lo_reloc
)
2112 ep2
.X_op
= O_symbol
;
2113 ep2
.X_add_symbol
= make_internal_label ();
2114 ep2
.X_add_number
= 0;
2116 macro_build (ep
, "auipc", "d,u", tempreg
, hi_reloc
);
2117 macro_build (&ep2
, lo_insn
, lo_pattern
, destreg
, tempreg
, lo_reloc
);
2121 pcrel_load (int destreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
2122 bfd_reloc_code_real_type hi_reloc
,
2123 bfd_reloc_code_real_type lo_reloc
)
2125 pcrel_access (destreg
, tempreg
, ep
, lo_insn
, "d,s,j", hi_reloc
, lo_reloc
);
2129 pcrel_store (int srcreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
2130 bfd_reloc_code_real_type hi_reloc
,
2131 bfd_reloc_code_real_type lo_reloc
)
2133 pcrel_access (srcreg
, tempreg
, ep
, lo_insn
, "t,s,q", hi_reloc
, lo_reloc
);
2136 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
2139 riscv_call (int destreg
, int tempreg
, expressionS
*ep
,
2140 bfd_reloc_code_real_type reloc
)
2142 /* Ensure the jalr is emitted to the same frag as the auipc. */
2144 macro_build (ep
, "auipc", "d,u", tempreg
, reloc
);
2145 macro_build (NULL
, "jalr", "d,s", destreg
, tempreg
);
2146 /* See comment at end of append_insn. */
2147 frag_wane (frag_now
);
2151 /* Load an integer constant into a register. */
2154 load_const (int reg
, expressionS
*ep
)
2156 int shift
= RISCV_IMM_BITS
;
2157 bfd_vma upper_imm
, sign
= (bfd_vma
) 1 << (RISCV_IMM_BITS
- 1);
2158 expressionS upper
= *ep
, lower
= *ep
;
2159 lower
.X_add_number
= ((ep
->X_add_number
& (sign
+ sign
- 1)) ^ sign
) - sign
;
2160 upper
.X_add_number
-= lower
.X_add_number
;
2162 if (ep
->X_op
!= O_constant
)
2164 as_bad (_("unsupported large constant"));
2168 if (xlen
> 32 && !IS_SEXT_32BIT_NUM (ep
->X_add_number
))
2170 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
2171 while (((upper
.X_add_number
>> shift
) & 1) == 0)
2174 upper
.X_add_number
= (int64_t) upper
.X_add_number
>> shift
;
2175 load_const (reg
, &upper
);
2177 md_assemblef ("slli x%d, x%d, 0x%x", reg
, reg
, shift
);
2178 if (lower
.X_add_number
!= 0)
2179 md_assemblef ("addi x%d, x%d, %" PRId64
, reg
, reg
,
2180 (int64_t) lower
.X_add_number
);
2184 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
2187 if (upper
.X_add_number
!= 0)
2189 /* Discard low part and zero-extend upper immediate. */
2190 upper_imm
= ((uint32_t)upper
.X_add_number
>> shift
);
2192 md_assemblef ("lui x%d, 0x%" PRIx64
, reg
, (uint64_t) upper_imm
);
2196 if (lower
.X_add_number
!= 0 || hi_reg
== 0)
2197 md_assemblef ("%s x%d, x%d, %" PRId64
, ADD32_INSN
, reg
, hi_reg
,
2198 (int64_t) lower
.X_add_number
);
2202 /* Zero extend and sign extend byte/half-word/word. */
2205 riscv_ext (int destreg
, int srcreg
, unsigned shift
, bool sign
)
2207 md_assemblef ("slli x%d, x%d, %#x", destreg
, srcreg
, shift
);
2208 md_assemblef ("sr%ci x%d, x%d, %#x",
2209 sign
? 'a' : 'l', destreg
, destreg
, shift
);
2212 /* Expand RISC-V Vector macros into one or more instructions. */
2215 vector_macro (struct riscv_cl_insn
*ip
)
2217 int vd
= (ip
->insn_opcode
>> OP_SH_VD
) & OP_MASK_VD
;
2218 int vs1
= (ip
->insn_opcode
>> OP_SH_VS1
) & OP_MASK_VS1
;
2219 int vs2
= (ip
->insn_opcode
>> OP_SH_VS2
) & OP_MASK_VS2
;
2220 int vm
= (ip
->insn_opcode
>> OP_SH_VMASK
) & OP_MASK_VMASK
;
2221 int vtemp
= (ip
->insn_opcode
>> OP_SH_VFUNCT6
) & OP_MASK_VFUNCT6
;
2222 const char *vmslt_vx
= ip
->insn_mo
->match
? "vmsltu.vx" : "vmslt.vx";
2223 int mask
= ip
->insn_mo
->mask
;
2231 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, -1);
2232 macro_build (NULL
, "vmnand.mm", "Vd,Vt,Vs", vd
, vd
, vd
);
2237 /* Masked. Have vtemp to avoid overlap constraints. */
2240 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
2241 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vm
, vtemp
);
2245 /* Preserve the value of vd if not updating by vm. */
2246 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vtemp
, vs2
, vs1
, -1);
2247 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vtemp
, vm
, vtemp
);
2248 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
2249 macro_build (NULL
, "vmor.mm", "Vd,Vt,Vs", vd
, vtemp
, vd
);
2254 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
2255 macro_build (NULL
, vmslt_vx
, "Vd,Vt,sVm", vd
, vs2
, vs1
, vm
);
2256 macro_build (NULL
, "vmxor.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
2259 as_bad (_("must provide temp if destination overlaps mask"));
2267 /* Expand RISC-V assembly macros into one or more instructions. */
2270 macro (struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2271 bfd_reloc_code_real_type
*imm_reloc
)
2273 int rd
= (ip
->insn_opcode
>> OP_SH_RD
) & OP_MASK_RD
;
2274 int rs1
= (ip
->insn_opcode
>> OP_SH_RS1
) & OP_MASK_RS1
;
2275 int rs2
= (ip
->insn_opcode
>> OP_SH_RS2
) & OP_MASK_RS2
;
2276 int mask
= ip
->insn_mo
->mask
;
2278 source_macro
= mask
;
2283 load_const (rd
, imm_expr
);
2289 /* Load the address of a symbol into a register. */
2290 if (!IS_SEXT_32BIT_NUM (imm_expr
->X_add_number
))
2291 as_bad (_("offset too large"));
2293 if (imm_expr
->X_op
== O_constant
)
2294 load_const (rd
, imm_expr
);
2295 /* Global PIC symbol. */
2296 else if ((riscv_opts
.pic
&& mask
== M_LA
)
2298 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2299 BFD_RELOC_RISCV_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2300 /* Local PIC symbol, or any non-PIC symbol. */
2302 pcrel_load (rd
, rd
, imm_expr
, "addi",
2303 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2307 pcrel_load (rd
, rd
, imm_expr
, "addi",
2308 BFD_RELOC_RISCV_TLS_GD_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2312 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
2313 BFD_RELOC_RISCV_TLS_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2317 pcrel_load (rd
, rd
, imm_expr
, ip
->insn_mo
->name
,
2318 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2322 pcrel_load (rd
, rs1
, imm_expr
, ip
->insn_mo
->name
,
2323 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
2327 pcrel_store (rs2
, rs1
, imm_expr
, ip
->insn_mo
->name
,
2328 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
2332 riscv_call (rd
, rs1
, imm_expr
, *imm_reloc
);
2336 riscv_ext (rd
, rs1
, xlen
- 16, *ip
->insn_mo
->name
== 's');
2340 riscv_ext (rd
, rs1
, xlen
- 32, false);
2344 riscv_ext (rd
, rs1
, xlen
- 8, true);
2352 as_bad (_("internal: macro %s not implemented"), ip
->insn_mo
->name
);
2359 static const struct percent_op_match percent_op_utype
[] =
2361 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20
},
2362 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20
},
2363 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20
},
2364 {"tlsdesc_hi", BFD_RELOC_RISCV_TLSDESC_HI20
},
2365 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20
},
2366 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20
},
2367 {"hi", BFD_RELOC_RISCV_HI20
},
2371 static const struct percent_op_match percent_op_itype
[] =
2373 {"lo", BFD_RELOC_RISCV_LO12_I
},
2374 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I
},
2375 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I
},
2376 {"tlsdesc_load_lo", BFD_RELOC_RISCV_TLSDESC_LOAD_LO12
},
2377 {"tlsdesc_add_lo", BFD_RELOC_RISCV_TLSDESC_ADD_LO12
},
2381 static const struct percent_op_match percent_op_stype
[] =
2383 {"lo", BFD_RELOC_RISCV_LO12_S
},
2384 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S
},
2385 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S
},
2389 static const struct percent_op_match percent_op_relax_only
[] =
2391 {"tlsdesc_call", BFD_RELOC_RISCV_TLSDESC_CALL
},
2392 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD
},
2396 static const struct percent_op_match percent_op_null
[] =
2401 /* Return true if *STR points to a relocation operator. When returning true,
2402 move *STR over the operator and store its relocation code in *RELOC.
2403 Leave both *STR and *RELOC alone when returning false. */
2406 parse_relocation (char **str
, bfd_reloc_code_real_type
*reloc
,
2407 const struct percent_op_match
*percent_op
)
2409 for ( ; percent_op
->str
; percent_op
++)
2410 if (strncasecmp (*str
+ 1, percent_op
->str
, strlen (percent_op
->str
)) == 0)
2412 size_t len
= 1 + strlen (percent_op
->str
);
2414 while (ISSPACE ((*str
)[len
]))
2416 if ((*str
)[len
] != '(')
2420 *reloc
= percent_op
->reloc
;
2422 /* Check whether the output BFD supports this relocation.
2423 If not, issue an error and fall back on something safe. */
2424 if (*reloc
!= BFD_RELOC_UNUSED
2425 && !bfd_reloc_type_lookup (stdoutput
, *reloc
))
2427 as_bad ("internal: relocation %s isn't supported by the "
2428 "current ABI", percent_op
->str
);
2429 *reloc
= BFD_RELOC_UNUSED
;
2437 my_getExpression (expressionS
*ep
, char *str
)
2441 save_in
= input_line_pointer
;
2442 input_line_pointer
= str
;
2444 expr_parse_end
= input_line_pointer
;
2445 input_line_pointer
= save_in
;
2448 /* Parse string STR as a 16-bit relocatable operand. Store the
2449 expression in *EP and the relocation, if any, in RELOC.
2450 Return the number of relocation operators used (0 or 1).
2452 On exit, EXPR_PARSE_END points to the first character after the
2456 my_getSmallExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2457 char *str
, const struct percent_op_match
*percent_op
)
2460 unsigned crux_depth
, str_depth
;
2461 bool orig_probing
= probing_insn_operands
;
2464 /* Search for the start of the main expression.
2466 End the loop with CRUX pointing to the start of the main expression and
2467 with CRUX_DEPTH containing the number of open brackets at that point. */
2474 crux_depth
= str_depth
;
2476 /* Skip over whitespace and brackets, keeping count of the number
2478 while (*str
== ' ' || *str
== '\t' || *str
== '(')
2484 && parse_relocation (&str
, reloc
, percent_op
));
2488 /* expression() will choke on anything looking like an (unrecognized)
2489 relocation specifier. Don't even call it, avoiding multiple (and
2490 perhaps redundant) error messages; our caller will issue one. */
2491 ep
->X_op
= O_illegal
;
2495 /* Anything inside parentheses or subject to a relocation operator cannot
2496 be a register and hence can be treated the same as operands to
2497 directives (other than .insn). */
2498 if (str_depth
|| reloc_index
)
2499 probing_insn_operands
= false;
2501 my_getExpression (ep
, crux
);
2502 str
= expr_parse_end
;
2504 probing_insn_operands
= orig_probing
;
2506 /* Match every open bracket. */
2507 while (crux_depth
> 0 && (*str
== ')' || *str
== ' ' || *str
== '\t'))
2512 as_bad ("unclosed '('");
2514 expr_parse_end
= str
;
2519 /* Parse opcode name, could be an mnemonics or number. */
2522 my_getOpcodeExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2525 const struct opcode_name_t
*o
= opcode_name_lookup (&str
);
2529 ep
->X_op
= O_constant
;
2530 ep
->X_add_number
= o
->val
;
2534 return my_getSmallExpression (ep
, reloc
, str
, percent_op_null
);
2537 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2538 On exit, EXPR_PARSE_END points to the first character after the
2542 my_getVsetvliExpression (expressionS
*ep
, char *str
)
2544 unsigned int vsew_value
= 0, vlmul_value
= 0;
2545 unsigned int vta_value
= 0, vma_value
= 0;
2546 bfd_boolean vsew_found
= FALSE
, vlmul_found
= FALSE
;
2547 bfd_boolean vta_found
= FALSE
, vma_found
= FALSE
;
2549 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
), &vsew_value
))
2554 as_bad (_("multiple vsew constants"));
2557 if (arg_lookup (&str
, riscv_vlmul
, ARRAY_SIZE (riscv_vlmul
), &vlmul_value
))
2562 as_bad (_("multiple vlmul constants"));
2565 if (arg_lookup (&str
, riscv_vta
, ARRAY_SIZE (riscv_vta
), &vta_value
))
2570 as_bad (_("multiple vta constants"));
2573 if (arg_lookup (&str
, riscv_vma
, ARRAY_SIZE (riscv_vma
), &vma_value
))
2578 as_bad (_("multiple vma constants"));
2582 if (vsew_found
|| vlmul_found
|| vta_found
|| vma_found
)
2584 ep
->X_op
= O_constant
;
2585 ep
->X_add_number
= (vlmul_value
<< OP_SH_VLMUL
)
2586 | (vsew_value
<< OP_SH_VSEW
)
2587 | (vta_value
<< OP_SH_VTA
)
2588 | (vma_value
<< OP_SH_VMA
);
2589 expr_parse_end
= str
;
2593 my_getExpression (ep
, str
);
2594 str
= expr_parse_end
;
2598 /* Parse string STR as a th.vsetvli operand. Store the expression in *EP.
2599 On exit, EXPR_PARSE_END points to the first character after the
2603 my_getThVsetvliExpression (expressionS
*ep
, char *str
)
2605 unsigned int vsew_value
= 0, vlen_value
= 0, vediv_value
= 0;
2606 bfd_boolean vsew_found
= FALSE
, vlen_found
= FALSE
, vediv_found
= FALSE
;
2608 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
),
2614 as_bad (_("multiple vsew constants"));
2618 if (arg_lookup (&str
, riscv_th_vlen
, ARRAY_SIZE (riscv_th_vlen
),
2624 as_bad (_("multiple vlen constants"));
2627 if (arg_lookup (&str
, riscv_th_vediv
, ARRAY_SIZE (riscv_th_vediv
),
2633 as_bad (_("multiple vediv constants"));
2637 if (vlen_found
|| vediv_found
|| vsew_found
)
2639 ep
->X_op
= O_constant
;
2641 = (vediv_value
<< 5) | (vsew_value
<< 2) | (vlen_value
);
2642 expr_parse_end
= str
;
2646 my_getExpression (ep
, str
);
2647 str
= expr_parse_end
;
2651 /* Detect and handle implicitly zero load-store offsets. For example,
2652 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2653 an implicit offset was detected. */
2656 riscv_handle_implicit_zero_offset (expressionS
*ep
, const char *s
)
2658 /* Check whether there is only a single bracketed expression left.
2659 If so, it must be the base register and the constant must be zero. */
2660 if (*s
== '(' && strchr (s
+ 1, '(') == 0)
2662 ep
->X_op
= O_constant
;
2663 ep
->X_add_number
= 0;
2670 /* All RISC-V CSR instructions belong to one of these classes. */
2679 /* Return which CSR instruction is checking. */
2681 static enum csr_insn_type
2682 riscv_csr_insn_type (insn_t insn
)
2684 if (((insn
^ MATCH_CSRRW
) & MASK_CSRRW
) == 0
2685 || ((insn
^ MATCH_CSRRWI
) & MASK_CSRRWI
) == 0)
2687 else if (((insn
^ MATCH_CSRRS
) & MASK_CSRRS
) == 0
2688 || ((insn
^ MATCH_CSRRSI
) & MASK_CSRRSI
) == 0)
2690 else if (((insn
^ MATCH_CSRRC
) & MASK_CSRRC
) == 0
2691 || ((insn
^ MATCH_CSRRCI
) & MASK_CSRRCI
) == 0)
2694 return INSN_NOT_CSR
;
2697 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2698 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2699 CSR address is 0x3. */
2702 riscv_csr_read_only_check (insn_t insn
)
2704 int csr
= (insn
& (OP_MASK_CSR
<< OP_SH_CSR
)) >> OP_SH_CSR
;
2705 int rs1
= (insn
& (OP_MASK_RS1
<< OP_SH_RS1
)) >> OP_SH_RS1
;
2706 int readonly
= (((csr
& (0x3 << 10)) >> 10) == 0x3);
2707 enum csr_insn_type csr_insn
= riscv_csr_insn_type (insn
);
2710 && (((csr_insn
== INSN_CSRRS
2711 || csr_insn
== INSN_CSRRC
)
2713 || csr_insn
== INSN_CSRRW
))
2719 /* Return true if it is a privileged instruction. Otherwise, return false.
2721 uret is actually a N-ext instruction. So it is better to regard it as
2722 an user instruction rather than the priv instruction.
2724 hret is used to return from traps in H-mode. H-mode is removed since
2725 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2726 Therefore, hret should be controlled by the hypervisor spec rather than
2727 priv spec in the future.
2729 dret is defined in the debug spec, so it should be checked in the future,
2733 riscv_is_priv_insn (insn_t insn
)
2735 return (((insn
^ MATCH_SRET
) & MASK_SRET
) == 0
2736 || ((insn
^ MATCH_MRET
) & MASK_MRET
) == 0
2737 || ((insn
^ MATCH_SFENCE_VMA
) & MASK_SFENCE_VMA
) == 0
2738 || ((insn
^ MATCH_WFI
) & MASK_WFI
) == 0
2739 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2740 check it here to keep the compatible. */
2741 || ((insn
^ MATCH_SFENCE_VM
) & MASK_SFENCE_VM
) == 0);
2744 static symbolS
*deferred_sym_rootP
;
2745 static symbolS
*deferred_sym_lastP
;
2746 /* Since symbols can't easily be freed, try to recycle ones which weren't
2748 static symbolS
*orphan_sym_rootP
;
2749 static symbolS
*orphan_sym_lastP
;
2751 /* This routine assembles an instruction into its binary format. As a
2752 side effect, it sets the global variable imm_reloc to the type of
2753 relocation to do if one of the operands is an address expression. */
2755 static struct riscv_ip_error
2756 riscv_ip (char *str
, struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2757 bfd_reloc_code_real_type
*imm_reloc
, htab_t hash
)
2759 /* The operand string defined in the riscv_opcodes. */
2760 const char *oparg
, *opargStart
;
2761 /* The parsed operands from assembly. */
2762 char *asarg
, *asargStart
;
2764 struct riscv_opcode
*insn
;
2766 const struct percent_op_match
*p
;
2767 struct riscv_ip_error error
;
2768 error
.msg
= "unrecognized opcode";
2769 error
.statement
= str
;
2770 error
.missing_ext
= NULL
;
2771 /* Indicate we are assembling instruction with CSR. */
2772 bool insn_with_csr
= false;
2774 /* Parse the name of the instruction. Terminate the string if whitespace
2775 is found so that str_hash_find only sees the name part of the string. */
2776 for (asarg
= str
; *asarg
!= '\0'; ++asarg
)
2777 if (ISSPACE (*asarg
))
2784 insn
= (struct riscv_opcode
*) str_hash_find (hash
, str
);
2786 probing_insn_operands
= true;
2789 for ( ; insn
&& insn
->name
&& strcmp (insn
->name
, str
) == 0; insn
++)
2791 if ((insn
->xlen_requirement
!= 0) && (xlen
!= insn
->xlen_requirement
))
2794 if (!riscv_multi_subset_supports (&riscv_rps_as
, insn
->insn_class
))
2796 error
.missing_ext
= riscv_multi_subset_supports_ext (&riscv_rps_as
,
2801 /* Reset error message of the previous round. */
2802 error
.msg
= _("illegal operands");
2803 error
.missing_ext
= NULL
;
2805 /* Purge deferred symbols from the previous round, if any. */
2806 while (deferred_sym_rootP
)
2808 symbolS
*sym
= deferred_sym_rootP
;
2810 symbol_remove (sym
, &deferred_sym_rootP
, &deferred_sym_lastP
);
2811 symbol_append (sym
, orphan_sym_lastP
, &orphan_sym_rootP
,
2815 create_insn (ip
, insn
);
2817 imm_expr
->X_op
= O_absent
;
2818 *imm_reloc
= BFD_RELOC_UNUSED
;
2819 p
= percent_op_null
;
2821 for (oparg
= insn
->args
;; ++oparg
)
2824 asarg
+= strspn (asarg
, " \t");
2827 case '\0': /* End of args. */
2828 if (insn
->match_func
&& !insn
->match_func (insn
, ip
->insn_opcode
))
2831 if (insn
->pinfo
!= INSN_MACRO
)
2833 /* For .insn, insn->match and insn->mask are 0. */
2834 if (riscv_insn_length ((insn
->match
== 0 && insn
->mask
== 0)
2840 if (riscv_is_priv_insn (ip
->insn_opcode
))
2841 explicit_priv_attr
= true;
2843 /* Check if we write a read-only CSR by the CSR
2846 && riscv_opts
.csr_check
2847 && !riscv_csr_read_only_check (ip
->insn_opcode
))
2849 /* Restore the character in advance, since we want to
2850 report the detailed warning message here. */
2852 *(asargStart
- 1) = save_c
;
2853 as_warn (_("read-only CSR is written `%s'"), str
);
2854 insn_with_csr
= false;
2857 /* The (segmant) load and store with EEW 64 cannot be used
2858 when zve32x is enabled. */
2859 if (ip
->insn_mo
->pinfo
& INSN_V_EEW64
2860 && riscv_subset_supports (&riscv_rps_as
, "zve32x")
2861 && !riscv_subset_supports (&riscv_rps_as
, "zve64x"))
2863 error
.msg
= _("illegal opcode for zve32x");
2870 /* Successful assembly. */
2872 insn_with_csr
= false;
2874 /* Commit deferred symbols, if any. */
2875 while (deferred_sym_rootP
)
2877 symbolS
*sym
= deferred_sym_rootP
;
2879 symbol_remove (sym
, &deferred_sym_rootP
,
2880 &deferred_sym_lastP
);
2881 symbol_append (sym
, symbol_lastP
, &symbol_rootP
,
2883 symbol_table_insert (sym
);
2890 case 's': /* RS1 x8-x15. */
2891 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2892 || !(regno
>= 8 && regno
<= 15))
2894 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2896 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2897 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2898 || EXTRACT_OPERAND (CRS1S
, ip
->insn_opcode
) + 8 != regno
)
2901 case 't': /* RS2 x8-x15. */
2902 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2903 || !(regno
>= 8 && regno
<= 15))
2905 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2907 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2908 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2909 || EXTRACT_OPERAND (CRS2S
, ip
->insn_opcode
) + 8 != regno
)
2912 case 'U': /* RS1, constrained to equal RD. */
2913 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2914 || EXTRACT_OPERAND (RD
, ip
->insn_opcode
) != regno
)
2918 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
2920 INSERT_OPERAND (CRS2
, *ip
, regno
);
2922 case 'c': /* RS1, constrained to equal sp. */
2923 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2927 case 'z': /* RS2, constrained to equal x0. */
2928 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2932 case '>': /* Shift amount, 0 - (XLEN-1). */
2933 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2934 || imm_expr
->X_op
!= O_constant
2935 || (unsigned long) imm_expr
->X_add_number
>= xlen
)
2937 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2939 asarg
= expr_parse_end
;
2940 imm_expr
->X_op
= O_absent
;
2943 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2944 || imm_expr
->X_op
!= O_constant
2945 || imm_expr
->X_add_number
< 0
2946 || imm_expr
->X_add_number
>= 32
2947 || !VALID_CLTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2949 ip
->insn_opcode
|= ENCODE_CLTYPE_IMM (imm_expr
->X_add_number
);
2952 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2953 || imm_expr
->X_op
!= O_constant
2954 || imm_expr
->X_add_number
< 0
2955 || imm_expr
->X_add_number
>= 64
2956 || !VALID_CSSTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2958 ip
->insn_opcode
|= ENCODE_CSSTYPE_IMM (imm_expr
->X_add_number
);
2961 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2962 || imm_expr
->X_op
!= O_constant
2963 || imm_expr
->X_add_number
< 0
2964 || imm_expr
->X_add_number
>= 256
2965 || !VALID_CIWTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2967 ip
->insn_opcode
|= ENCODE_CIWTYPE_IMM (imm_expr
->X_add_number
);
2970 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2971 || imm_expr
->X_op
!= O_constant
2972 || imm_expr
->X_add_number
== 0
2973 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2975 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2978 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2980 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2981 || imm_expr
->X_op
!= O_constant
2982 || !VALID_CLTYPE_LW_IMM ((valueT
) imm_expr
->X_add_number
))
2984 ip
->insn_opcode
|= ENCODE_CLTYPE_LW_IMM (imm_expr
->X_add_number
);
2987 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2989 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2990 || imm_expr
->X_op
!= O_constant
2991 || !VALID_CLTYPE_LD_IMM ((valueT
) imm_expr
->X_add_number
))
2993 ip
->insn_opcode
|= ENCODE_CLTYPE_LD_IMM (imm_expr
->X_add_number
);
2996 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2998 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2999 || imm_expr
->X_op
!= O_constant
3000 || !VALID_CITYPE_LWSP_IMM ((valueT
) imm_expr
->X_add_number
))
3003 ENCODE_CITYPE_LWSP_IMM (imm_expr
->X_add_number
);
3006 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3008 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3009 || imm_expr
->X_op
!= O_constant
3010 || !VALID_CITYPE_LDSP_IMM ((valueT
) imm_expr
->X_add_number
))
3013 ENCODE_CITYPE_LDSP_IMM (imm_expr
->X_add_number
);
3016 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3017 || imm_expr
->X_op
!= O_constant
3018 /* C.addiw, c.li, and c.andi allow zero immediate.
3019 C.addi allows zero immediate as hint. Otherwise this
3021 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
3023 ip
->insn_opcode
|= ENCODE_CITYPE_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 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT
) imm_expr
->X_add_number
))
3032 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr
->X_add_number
);
3035 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3036 || imm_expr
->X_op
!= O_constant
3037 || !VALID_CITYPE_ADDI16SP_IMM ((valueT
) imm_expr
->X_add_number
))
3040 ENCODE_CITYPE_ADDI16SP_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_CSSTYPE_SWSP_IMM ((valueT
) imm_expr
->X_add_number
))
3050 ENCODE_CSSTYPE_SWSP_IMM (imm_expr
->X_add_number
);
3053 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3055 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3056 || imm_expr
->X_op
!= O_constant
3057 || !VALID_CSSTYPE_SDSP_IMM ((valueT
) imm_expr
->X_add_number
))
3060 ENCODE_CSSTYPE_SDSP_IMM (imm_expr
->X_add_number
);
3063 p
= percent_op_utype
;
3064 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3067 if (imm_expr
->X_op
!= O_constant
3068 || imm_expr
->X_add_number
<= 0
3069 || imm_expr
->X_add_number
>= RISCV_BIGIMM_REACH
3070 || (imm_expr
->X_add_number
>= RISCV_RVC_IMM_REACH
/ 2
3071 && (imm_expr
->X_add_number
<
3072 RISCV_BIGIMM_REACH
- RISCV_RVC_IMM_REACH
/ 2)))
3074 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
3077 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3078 || (imm_expr
->X_add_number
& (RISCV_IMM_REACH
- 1))
3079 || ((int32_t)imm_expr
->X_add_number
3080 != imm_expr
->X_add_number
))
3082 imm_expr
->X_add_number
=
3083 ((uint32_t) imm_expr
->X_add_number
) >> RISCV_IMM_BITS
;
3089 case 'S': /* Floating-point RS1 x8-x15. */
3090 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
3091 || !(regno
>= 8 && regno
<= 15))
3093 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
3095 case 'D': /* Floating-point RS2 x8-x15. */
3096 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
3097 || !(regno
>= 8 && regno
<= 15))
3099 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
3101 case 'T': /* Floating-point RS2. */
3102 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
))
3104 INSERT_OPERAND (CRS2
, *ip
, regno
);
3110 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3111 || imm_expr
->X_op
!= O_constant
3112 || imm_expr
->X_add_number
< 0
3113 || imm_expr
->X_add_number
>= 64)
3115 as_bad (_("bad value for compressed funct6 "
3116 "field, value must be 0...63"));
3119 INSERT_OPERAND (CFUNCT6
, *ip
, imm_expr
->X_add_number
);
3120 imm_expr
->X_op
= O_absent
;
3121 asarg
= expr_parse_end
;
3125 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3126 || imm_expr
->X_op
!= O_constant
3127 || imm_expr
->X_add_number
< 0
3128 || imm_expr
->X_add_number
>= 16)
3130 as_bad (_("bad value for compressed funct4 "
3131 "field, value must be 0...15"));
3134 INSERT_OPERAND (CFUNCT4
, *ip
, imm_expr
->X_add_number
);
3135 imm_expr
->X_op
= O_absent
;
3136 asarg
= expr_parse_end
;
3140 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3141 || imm_expr
->X_op
!= O_constant
3142 || imm_expr
->X_add_number
< 0
3143 || imm_expr
->X_add_number
>= 8)
3145 as_bad (_("bad value for compressed funct3 "
3146 "field, value must be 0...7"));
3149 INSERT_OPERAND (CFUNCT3
, *ip
, imm_expr
->X_add_number
);
3150 imm_expr
->X_op
= O_absent
;
3151 asarg
= expr_parse_end
;
3155 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3156 || imm_expr
->X_op
!= O_constant
3157 || imm_expr
->X_add_number
< 0
3158 || imm_expr
->X_add_number
>= 4)
3160 as_bad (_("bad value for compressed funct2 "
3161 "field, value must be 0...3"));
3164 INSERT_OPERAND (CFUNCT2
, *ip
, imm_expr
->X_add_number
);
3165 imm_expr
->X_op
= O_absent
;
3166 asarg
= expr_parse_end
;
3170 goto unknown_riscv_ip_operand
;
3175 goto unknown_riscv_ip_operand
;
3177 break; /* end RVC */
3183 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3185 INSERT_OPERAND (VD
, *ip
, regno
);
3188 case 'e': /* AMO VD */
3189 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
) && regno
== 0)
3190 INSERT_OPERAND (VWD
, *ip
, 0);
3191 else if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3193 INSERT_OPERAND (VWD
, *ip
, 1);
3194 INSERT_OPERAND (VD
, *ip
, regno
);
3200 case 'f': /* AMO VS3 */
3201 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3203 if (!EXTRACT_OPERAND (VWD
, ip
->insn_opcode
))
3204 INSERT_OPERAND (VD
, *ip
, regno
);
3207 /* VS3 must match VD. */
3208 if (EXTRACT_OPERAND (VD
, ip
->insn_opcode
) != regno
)
3214 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3216 INSERT_OPERAND (VS1
, *ip
, regno
);
3220 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3222 INSERT_OPERAND (VS2
, *ip
, regno
);
3225 case 'u': /* VS1 == VS2 */
3226 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3228 INSERT_OPERAND (VS1
, *ip
, regno
);
3229 INSERT_OPERAND (VS2
, *ip
, regno
);
3232 case 'v': /* VD == VS1 == VS2 */
3233 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
3235 INSERT_OPERAND (VD
, *ip
, regno
);
3236 INSERT_OPERAND (VS1
, *ip
, regno
);
3237 INSERT_OPERAND (VS2
, *ip
, regno
);
3240 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
3241 and is used to choose vs1/rs1/frs1/imm or vs2 for
3242 v[f]merge. It use the same encoding as the vector mask
3245 if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
) && regno
== 0)
3249 case 'b': /* vtypei for vsetivli */
3250 my_getVsetvliExpression (imm_expr
, asarg
);
3251 check_absolute_expr (ip
, imm_expr
, FALSE
);
3252 if (!VALID_RVV_VB_IMM (imm_expr
->X_add_number
))
3253 as_bad (_("bad value for vsetivli immediate field, "
3254 "value must be 0..1023"));
3256 |= ENCODE_RVV_VB_IMM (imm_expr
->X_add_number
);
3257 imm_expr
->X_op
= O_absent
;
3258 asarg
= expr_parse_end
;
3261 case 'c': /* vtypei for vsetvli */
3262 my_getVsetvliExpression (imm_expr
, asarg
);
3263 check_absolute_expr (ip
, imm_expr
, FALSE
);
3264 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
3265 as_bad (_("bad value for vsetvli immediate field, "
3266 "value must be 0..2047"));
3268 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
3269 imm_expr
->X_op
= O_absent
;
3270 asarg
= expr_parse_end
;
3273 case 'i': /* vector arith signed immediate */
3274 my_getExpression (imm_expr
, asarg
);
3275 check_absolute_expr (ip
, imm_expr
, FALSE
);
3276 if (imm_expr
->X_add_number
> 15
3277 || imm_expr
->X_add_number
< -16)
3278 as_bad (_("bad value for vector immediate field, "
3279 "value must be -16...15"));
3280 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3281 imm_expr
->X_op
= O_absent
;
3282 asarg
= expr_parse_end
;
3285 case 'j': /* vector arith unsigned immediate */
3286 my_getExpression (imm_expr
, asarg
);
3287 check_absolute_expr (ip
, imm_expr
, FALSE
);
3288 if (imm_expr
->X_add_number
< 0
3289 || imm_expr
->X_add_number
>= 32)
3290 as_bad (_("bad value for vector immediate field, "
3291 "value must be 0...31"));
3292 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
3293 imm_expr
->X_op
= O_absent
;
3294 asarg
= expr_parse_end
;
3297 case 'k': /* vector arith signed immediate, minus 1 */
3298 my_getExpression (imm_expr
, asarg
);
3299 check_absolute_expr (ip
, imm_expr
, FALSE
);
3300 if (imm_expr
->X_add_number
> 16
3301 || imm_expr
->X_add_number
< -15)
3302 as_bad (_("bad value for vector immediate field, "
3303 "value must be -15...16"));
3304 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
- 1);
3305 imm_expr
->X_op
= O_absent
;
3306 asarg
= expr_parse_end
;
3309 case 'l': /* 6-bit vector arith unsigned immediate */
3310 my_getExpression (imm_expr
, asarg
);
3311 check_absolute_expr (ip
, imm_expr
, FALSE
);
3312 if (imm_expr
->X_add_number
< 0
3313 || imm_expr
->X_add_number
>= 64)
3314 as_bad (_("bad value for vector immediate field, "
3315 "value must be 0...63"));
3316 ip
->insn_opcode
|= ENCODE_RVV_VI_UIMM6 (imm_expr
->X_add_number
);
3317 imm_expr
->X_op
= O_absent
;
3318 asarg
= expr_parse_end
;
3321 case 'm': /* optional vector mask */
3324 INSERT_OPERAND (VMASK
, *ip
, 1);
3327 else if (*asarg
== ',' && asarg
++
3328 && reg_lookup (&asarg
, RCLASS_VECM
, ®no
)
3331 INSERT_OPERAND (VMASK
, *ip
, 0);
3336 case 'M': /* required vector mask */
3337 if (reg_lookup (&asarg
, RCLASS_VECM
, ®no
) && regno
== 0)
3339 INSERT_OPERAND (VMASK
, *ip
, 0);
3344 case 'T': /* vector macro temporary register */
3345 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
) || regno
== 0)
3347 /* Store it in the FUNCT6 field as we don't have anyplace
3348 else to store it. */
3349 INSERT_OPERAND (VFUNCT6
, *ip
, regno
);
3353 goto unknown_riscv_ip_operand
;
3355 break; /* end RVV */
3358 if (*asarg
++ == *oparg
)
3369 if (*asarg
++ == *oparg
)
3373 case '<': /* Shift amount, 0 - 31. */
3374 my_getExpression (imm_expr
, asarg
);
3375 check_absolute_expr (ip
, imm_expr
, false);
3376 if ((unsigned long) imm_expr
->X_add_number
> 31)
3377 as_bad (_("improper shift amount (%"PRIu64
")"),
3378 imm_expr
->X_add_number
);
3379 INSERT_OPERAND (SHAMTW
, *ip
, imm_expr
->X_add_number
);
3380 imm_expr
->X_op
= O_absent
;
3381 asarg
= expr_parse_end
;
3384 case '>': /* Shift amount, 0 - (XLEN-1). */
3385 my_getExpression (imm_expr
, asarg
);
3386 check_absolute_expr (ip
, imm_expr
, false);
3387 if ((unsigned long) imm_expr
->X_add_number
>= xlen
)
3388 as_bad (_("improper shift amount (%"PRIu64
")"),
3389 imm_expr
->X_add_number
);
3390 INSERT_OPERAND (SHAMT
, *ip
, imm_expr
->X_add_number
);
3391 imm_expr
->X_op
= O_absent
;
3392 asarg
= expr_parse_end
;
3395 case 'Z': /* CSRRxI immediate. */
3396 my_getExpression (imm_expr
, asarg
);
3397 check_absolute_expr (ip
, imm_expr
, false);
3398 if ((unsigned long) imm_expr
->X_add_number
> 31)
3399 as_bad (_("improper CSRxI immediate (%"PRIu64
")"),
3400 imm_expr
->X_add_number
);
3401 INSERT_OPERAND (RS1
, *ip
, imm_expr
->X_add_number
);
3402 imm_expr
->X_op
= O_absent
;
3403 asarg
= expr_parse_end
;
3406 case 'E': /* Control register. */
3407 insn_with_csr
= true;
3408 explicit_priv_attr
= true;
3409 if (reg_lookup (&asarg
, RCLASS_CSR
, ®no
))
3410 INSERT_OPERAND (CSR
, *ip
, regno
);
3413 my_getExpression (imm_expr
, asarg
);
3414 check_absolute_expr (ip
, imm_expr
, true);
3415 if ((unsigned long) imm_expr
->X_add_number
> 0xfff)
3416 as_bad (_("improper CSR address (%"PRIu64
")"),
3417 imm_expr
->X_add_number
);
3418 INSERT_OPERAND (CSR
, *ip
, imm_expr
->X_add_number
);
3419 imm_expr
->X_op
= O_absent
;
3420 asarg
= expr_parse_end
;
3424 case 'm': /* Rounding mode. */
3425 if (arg_lookup (&asarg
, riscv_rm
,
3426 ARRAY_SIZE (riscv_rm
), ®no
))
3428 INSERT_OPERAND (RM
, *ip
, regno
);
3434 case 'Q': /* Fence predecessor/successor. */
3435 if (arg_lookup (&asarg
, riscv_pred_succ
,
3436 ARRAY_SIZE (riscv_pred_succ
), ®no
))
3439 INSERT_OPERAND (PRED
, *ip
, regno
);
3441 INSERT_OPERAND (SUCC
, *ip
, regno
);
3446 case 'd': /* Destination register. */
3447 case 's': /* Source register. */
3448 case 't': /* Target register. */
3450 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
3456 /* Now that we have assembled one operand, we use the args
3457 string to figure out where it goes in the instruction. */
3461 INSERT_OPERAND (RS1
, *ip
, regno
);
3464 INSERT_OPERAND (RD
, *ip
, regno
);
3467 INSERT_OPERAND (RS2
, *ip
, regno
);
3470 INSERT_OPERAND (RS3
, *ip
, regno
);
3477 case 'D': /* Floating point RD. */
3478 case 'S': /* Floating point RS1. */
3479 case 'T': /* Floating point RS2. */
3480 case 'U': /* Floating point RS1 and RS2. */
3481 case 'R': /* Floating point RS3. */
3482 if (reg_lookup (&asarg
,
3483 (riscv_subset_supports (&riscv_rps_as
, "zfinx")
3484 ? RCLASS_GPR
: RCLASS_FPR
), ®no
))
3492 INSERT_OPERAND (RD
, *ip
, regno
);
3495 INSERT_OPERAND (RS1
, *ip
, regno
);
3498 INSERT_OPERAND (RS1
, *ip
, regno
);
3501 INSERT_OPERAND (RS2
, *ip
, regno
);
3504 INSERT_OPERAND (RS3
, *ip
, regno
);
3512 my_getExpression (imm_expr
, asarg
);
3513 if (imm_expr
->X_op
!= O_big
3514 && imm_expr
->X_op
!= O_constant
)
3516 normalize_constant_expr (imm_expr
);
3517 asarg
= expr_parse_end
;
3521 my_getExpression (imm_expr
, asarg
);
3522 normalize_constant_expr (imm_expr
);
3523 /* The 'A' format specifier must be a symbol. */
3524 if (imm_expr
->X_op
!= O_symbol
)
3526 *imm_reloc
= BFD_RELOC_32
;
3527 asarg
= expr_parse_end
;
3531 my_getExpression (imm_expr
, asarg
);
3532 normalize_constant_expr (imm_expr
);
3533 /* The 'B' format specifier must be a symbol or a constant. */
3534 if (imm_expr
->X_op
!= O_symbol
&& imm_expr
->X_op
!= O_constant
)
3536 if (imm_expr
->X_op
== O_symbol
)
3537 *imm_reloc
= BFD_RELOC_32
;
3538 asarg
= expr_parse_end
;
3541 case 'j': /* Sign-extended immediate. */
3542 p
= percent_op_itype
;
3543 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3545 case 'q': /* Store displacement. */
3546 p
= percent_op_stype
;
3547 *imm_reloc
= BFD_RELOC_RISCV_LO12_S
;
3549 case 'o': /* Load displacement. */
3550 p
= percent_op_itype
;
3551 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3554 /* This is used for TLS relocations that acts as relaxation
3555 markers and do not change the instruction encoding,
3556 i.e. %tprel_add and %tlsdesc_call. */
3557 p
= percent_op_relax_only
;
3559 case '0': /* AMO displacement, which must be zero. */
3561 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3564 /* If this value won't fit into a 16 bit offset, then go
3565 find a macro that will generate the 32 bit offset
3567 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3569 normalize_constant_expr (imm_expr
);
3570 if (imm_expr
->X_op
!= O_constant
3571 || (*oparg
== '0' && imm_expr
->X_add_number
!= 0)
3573 || imm_expr
->X_add_number
>= (signed)RISCV_IMM_REACH
/2
3574 || imm_expr
->X_add_number
< -(signed)RISCV_IMM_REACH
/2)
3577 asarg
= expr_parse_end
;
3580 case 'p': /* PC-relative offset. */
3582 *imm_reloc
= BFD_RELOC_12_PCREL
;
3583 my_getExpression (imm_expr
, asarg
);
3584 asarg
= expr_parse_end
;
3587 case 'u': /* Upper 20 bits. */
3588 p
= percent_op_utype
;
3589 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3591 if (imm_expr
->X_op
!= O_constant
)
3594 if (imm_expr
->X_add_number
< 0
3595 || imm_expr
->X_add_number
>= (signed)RISCV_BIGIMM_REACH
)
3596 as_bad (_("lui expression not in range 0..1048575"));
3598 *imm_reloc
= BFD_RELOC_RISCV_HI20
;
3599 imm_expr
->X_add_number
<<= RISCV_IMM_BITS
;
3601 asarg
= expr_parse_end
;
3604 case 'a': /* 20-bit PC-relative offset. */
3606 my_getExpression (imm_expr
, asarg
);
3607 asarg
= expr_parse_end
;
3608 *imm_reloc
= BFD_RELOC_RISCV_JMP
;
3612 my_getExpression (imm_expr
, asarg
);
3613 asarg
= expr_parse_end
;
3614 if (strcmp (asarg
, "@plt") == 0)
3616 *imm_reloc
= BFD_RELOC_RISCV_CALL_PLT
;
3623 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3624 || imm_expr
->X_op
!= O_constant
3625 || imm_expr
->X_add_number
< 0
3626 || imm_expr
->X_add_number
>= 128
3627 || (imm_expr
->X_add_number
& 0x3) != 3)
3629 as_bad (_("bad value for opcode field, "
3630 "value must be 0...127 and "
3631 "lower 2 bits must be 0x3"));
3634 INSERT_OPERAND (OP
, *ip
, imm_expr
->X_add_number
);
3635 imm_expr
->X_op
= O_absent
;
3636 asarg
= expr_parse_end
;
3640 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
)
3641 || imm_expr
->X_op
!= O_constant
3642 || imm_expr
->X_add_number
< 0
3643 || imm_expr
->X_add_number
>= 3)
3645 as_bad (_("bad value for opcode field, "
3646 "value must be 0...2"));
3649 INSERT_OPERAND (OP2
, *ip
, imm_expr
->X_add_number
);
3650 imm_expr
->X_op
= O_absent
;
3651 asarg
= expr_parse_end
;
3655 goto unknown_riscv_ip_operand
;
3663 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3664 || imm_expr
->X_op
!= O_constant
3665 || imm_expr
->X_add_number
< 0
3666 || imm_expr
->X_add_number
>= 128)
3668 as_bad (_("bad value for funct7 field, "
3669 "value must be 0...127"));
3672 INSERT_OPERAND (FUNCT7
, *ip
, imm_expr
->X_add_number
);
3673 imm_expr
->X_op
= O_absent
;
3674 asarg
= expr_parse_end
;
3678 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3679 || imm_expr
->X_op
!= O_constant
3680 || imm_expr
->X_add_number
< 0
3681 || imm_expr
->X_add_number
>= 8)
3683 as_bad (_("bad value for funct3 field, "
3684 "value must be 0...7"));
3687 INSERT_OPERAND (FUNCT3
, *ip
, imm_expr
->X_add_number
);
3688 imm_expr
->X_op
= O_absent
;
3689 asarg
= expr_parse_end
;
3693 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3694 || imm_expr
->X_op
!= O_constant
3695 || imm_expr
->X_add_number
< 0
3696 || imm_expr
->X_add_number
>= 4)
3698 as_bad (_("bad value for funct2 field, "
3699 "value must be 0...3"));
3702 INSERT_OPERAND (FUNCT2
, *ip
, imm_expr
->X_add_number
);
3703 imm_expr
->X_op
= O_absent
;
3704 asarg
= expr_parse_end
;
3708 goto unknown_riscv_ip_operand
;
3712 case 'y': /* bs immediate */
3713 my_getExpression (imm_expr
, asarg
);
3714 check_absolute_expr (ip
, imm_expr
, FALSE
);
3715 if ((unsigned long)imm_expr
->X_add_number
> 3)
3716 as_bad(_("Improper bs immediate (%lu)"),
3717 (unsigned long)imm_expr
->X_add_number
);
3718 INSERT_OPERAND(BS
, *ip
, imm_expr
->X_add_number
);
3719 imm_expr
->X_op
= O_absent
;
3720 asarg
= expr_parse_end
;
3723 case 'Y': /* rnum immediate */
3724 my_getExpression (imm_expr
, asarg
);
3725 check_absolute_expr (ip
, imm_expr
, FALSE
);
3726 if ((unsigned long)imm_expr
->X_add_number
> 10)
3727 as_bad(_("Improper rnum immediate (%lu)"),
3728 (unsigned long)imm_expr
->X_add_number
);
3729 INSERT_OPERAND(RNUM
, *ip
, imm_expr
->X_add_number
);
3730 imm_expr
->X_op
= O_absent
;
3731 asarg
= expr_parse_end
;
3735 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3736 || imm_expr
->X_op
!= O_constant
3737 || imm_expr
->X_add_number
!= 0)
3739 asarg
= expr_parse_end
;
3740 imm_expr
->X_op
= O_absent
;
3743 case 'W': /* Various operands for standard z extensions. */
3750 /* Prefetch offset for 'Zicbop' extension.
3751 pseudo S-type but lower 5-bits zero. */
3752 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3754 my_getExpression (imm_expr
, asarg
);
3755 check_absolute_expr (ip
, imm_expr
, false);
3756 if (((unsigned) (imm_expr
->X_add_number
) & 0x1fU
)
3757 || imm_expr
->X_add_number
>= RISCV_IMM_REACH
/ 2
3758 || imm_expr
->X_add_number
< -RISCV_IMM_REACH
/ 2)
3759 as_bad (_ ("improper prefetch offset (%ld)"),
3760 (long) imm_expr
->X_add_number
);
3761 ip
->insn_opcode
|= ENCODE_STYPE_IMM (
3762 (unsigned) (imm_expr
->X_add_number
) & ~0x1fU
);
3763 imm_expr
->X_op
= O_absent
;
3764 asarg
= expr_parse_end
;
3767 goto unknown_riscv_ip_operand
;
3775 /* FLI.[HSDQ] value field for 'Zfa' extension. */
3776 if (!arg_lookup (&asarg
, riscv_fli_symval
,
3777 ARRAY_SIZE (riscv_fli_symval
), ®no
))
3779 /* 0.0 is not a valid entry in riscv_fli_numval. */
3781 float f
= strtof (asarg
, &asarg
);
3782 if (errno
!= 0 || f
== 0.0
3783 || !flt_lookup (f
, riscv_fli_numval
,
3784 ARRAY_SIZE(riscv_fli_numval
),
3787 as_bad (_("bad fli constant operand, "
3788 "supported constants must be in "
3789 "decimal or hexadecimal floating-point "
3794 INSERT_OPERAND (RS1
, *ip
, regno
);
3797 goto unknown_riscv_ip_operand
;
3804 case 'h': /* Immediate field for c.lh/c.lhu/c.sh. */
3805 /* Handle cases, such as c.sh rs2', (rs1'). */
3806 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3808 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3809 || imm_expr
->X_op
!= O_constant
3810 || !VALID_ZCB_HALFWORD_UIMM ((valueT
) imm_expr
->X_add_number
))
3812 ip
->insn_opcode
|= ENCODE_ZCB_HALFWORD_UIMM (imm_expr
->X_add_number
);
3814 case 'b': /* Immediate field for c.lbu/c.sb. */
3815 /* Handle cases, such as c.lbu rd', (rs1'). */
3816 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3818 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3819 || imm_expr
->X_op
!= O_constant
3820 || !VALID_ZCB_BYTE_UIMM ((valueT
) imm_expr
->X_add_number
))
3822 ip
->insn_opcode
|= ENCODE_ZCB_BYTE_UIMM (imm_expr
->X_add_number
);
3825 if (!reglist_lookup (&asarg
, ®no
))
3827 INSERT_OPERAND (REG_LIST
, *ip
, regno
);
3830 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3831 || imm_expr
->X_op
!= O_constant
)
3833 /* Convert stack adjustment of cm.push to a positive
3835 if (ip
->insn_mo
->match
== MATCH_CM_PUSH
)
3836 imm_expr
->X_add_number
*= -1;
3837 /* Subtract base stack adjustment and get spimm. */
3838 imm_expr
->X_add_number
-=
3839 riscv_get_sp_base (ip
->insn_opcode
, *riscv_rps_as
.xlen
);
3840 if (!VALID_ZCMP_SPIMM (imm_expr
->X_add_number
))
3843 ENCODE_ZCMP_SPIMM (imm_expr
->X_add_number
);
3845 case 'f': /* Operand for matching immediate 255. */
3846 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3847 || imm_expr
->X_op
!= O_constant
3848 || imm_expr
->X_add_number
!= 255)
3850 /* This operand is used for matching immediate 255, and
3851 we do not write anything to encoding by this operand. */
3852 asarg
= expr_parse_end
;
3853 imm_expr
->X_op
= O_absent
;
3856 goto unknown_riscv_ip_operand
;
3861 goto unknown_riscv_ip_operand
;
3865 case 'X': /* Vendor-specific operands. */
3868 case 't': /* Vendor-specific (T-head) operands. */
3876 /* Vtypei for th.vsetvli. */
3879 goto unknown_riscv_ip_operand
;
3881 my_getThVsetvliExpression (imm_expr
, asarg
);
3882 check_absolute_expr (ip
, imm_expr
, FALSE
);
3883 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
3884 as_bad (_("bad value for th.vsetvli immediate field, "
3885 "value must be 0..2047"));
3887 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
3888 imm_expr
->X_op
= O_absent
;
3889 asarg
= expr_parse_end
;
3892 case 'l': /* Integer immediate, literal. */
3893 n
= strcspn (++oparg
, ",");
3894 if (strncmp (oparg
, asarg
, n
))
3895 as_bad (_("unexpected literal (%s)"), asarg
);
3899 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
3902 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
3906 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
3908 goto unknown_riscv_ip_operand
;
3909 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
3912 my_getExpression (imm_expr
, asarg
);
3913 check_absolute_expr (ip
, imm_expr
, false);
3916 if (!VALIDATE_U_IMM (imm_expr
->X_add_number
, n
))
3917 as_bad (_("improper immediate value (%"PRIu64
")"),
3918 imm_expr
->X_add_number
);
3922 if (!VALIDATE_S_IMM (imm_expr
->X_add_number
, n
))
3923 as_bad (_("improper immediate value (%"PRIi64
")"),
3924 imm_expr
->X_add_number
);
3926 INSERT_IMM (n
, s
, *ip
, imm_expr
->X_add_number
);
3927 imm_expr
->X_op
= O_absent
;
3928 asarg
= expr_parse_end
;
3931 goto unknown_riscv_ip_operand
;
3936 case 'c': /* Vendor-specific (CORE-V) operands. */
3940 my_getExpression (imm_expr
, asarg
);
3941 check_absolute_expr (ip
, imm_expr
, FALSE
);
3942 asarg
= expr_parse_end
;
3943 if (imm_expr
->X_add_number
<0
3944 || imm_expr
->X_add_number
>31)
3947 |= ENCODE_CV_IS2_UIMM5 (imm_expr
->X_add_number
);
3950 my_getExpression (imm_expr
, asarg
);
3951 check_absolute_expr (ip
, imm_expr
, FALSE
);
3952 asarg
= expr_parse_end
;
3953 if (imm_expr
->X_add_number
< 0
3954 || imm_expr
->X_add_number
> 31)
3957 |= ENCODE_CV_IS3_UIMM5 (imm_expr
->X_add_number
);
3960 goto unknown_riscv_ip_operand
;
3964 case 's': /* Vendor-specific (SiFive) operands. */
3965 #define UIMM_BITFIELD_VAL(S, E) (1 << ((E) - (S) + 1))
3966 #define ENCODE_UIMM_BIT_FIELD(NAME, IP, EXPR, RELOC, ASARG, \
3970 if (my_getOpcodeExpression (EXPR, RELOC, ASARG) \
3971 || EXPR->X_op != O_constant \
3972 || EXPR->X_add_number < 0 \
3973 || EXPR->X_add_number >= UIMM_BITFIELD_VAL (START, END)) \
3975 as_bad (_("bad value for <bit-%s-%s> " \
3976 "field, value must be 0...%d"), \
3977 #START, #END, UIMM_BITFIELD_VAL (START, END)); \
3980 INSERT_OPERAND (NAME, *IP, EXPR->X_add_number); \
3981 EXPR->X_op = O_absent; \
3982 ASARG = expr_parse_end; \
3988 ENCODE_UIMM_BIT_FIELD
3989 (RD
, ip
, imm_expr
, imm_reloc
, asarg
, 7, 11);
3992 ENCODE_UIMM_BIT_FIELD
3993 (RS2
, ip
, imm_expr
, imm_reloc
, asarg
, 20, 24)
3998 case '2': /* XsO2 */
3999 ENCODE_UIMM_BIT_FIELD
4000 (XSO2
, ip
, imm_expr
, imm_reloc
, asarg
, 26, 27);
4002 case '1': /* XsO1 */
4003 ENCODE_UIMM_BIT_FIELD
4004 (XSO1
, ip
, imm_expr
, imm_reloc
, asarg
, 26, 26);
4008 goto unknown_riscv_ip_operand
;
4010 #undef UIMM_BITFIELD_VAL
4011 #undef ENCODE_UIMM_BIT_FIELD
4015 goto unknown_riscv_ip_operand
;
4020 unknown_riscv_ip_operand
:
4021 as_fatal (_("internal: unknown argument type `%s'"),
4027 insn_with_csr
= false;
4031 /* Restore the character we might have clobbered above. */
4033 *(asargStart
- 1) = save_c
;
4035 probing_insn_operands
= false;
4040 /* Similar to riscv_ip, but assembles an instruction according to the
4041 hardcode values of .insn directive. */
4044 riscv_ip_hardcode (char *str
,
4045 struct riscv_cl_insn
*ip
,
4046 expressionS
*imm_expr
,
4049 struct riscv_opcode
*insn
;
4050 insn_t values
[2] = {0, 0};
4051 unsigned int num
= 0;
4053 input_line_pointer
= str
;
4056 expression (imm_expr
);
4057 switch (imm_expr
->X_op
)
4060 values
[num
++] = (insn_t
) imm_expr
->X_add_number
;
4063 /* Extract lower 32-bits of a big number.
4064 Assume that generic_bignum_to_int32 work on such number. */
4065 values
[num
++] = (insn_t
) generic_bignum_to_int32 ();
4068 /* The first value isn't constant, so it should be
4069 .insn <type> <operands>. We have been parsed it
4073 return _("values must be constant");
4076 while (*input_line_pointer
++ == ',' && num
< 2 && imm_expr
->X_op
!= O_big
);
4078 input_line_pointer
--;
4079 if (*input_line_pointer
!= '\0')
4080 return _("unrecognized values");
4082 insn
= XCNEW (struct riscv_opcode
);
4083 insn
->match
= values
[num
- 1];
4084 create_insn (ip
, insn
);
4085 unsigned int bytes
= riscv_insn_length (insn
->match
);
4087 if (num
== 2 && values
[0] != bytes
)
4088 return _("value conflicts with instruction length");
4090 if (imm_expr
->X_op
== O_big
)
4092 unsigned int llen
= 0;
4093 for (LITTLENUM_TYPE lval
= generic_bignum
[imm_expr
->X_add_number
- 1];
4095 lval
>>= BITS_PER_CHAR
;
4096 unsigned int repr_bytes
4097 = (imm_expr
->X_add_number
- 1) * CHARS_PER_LITTLENUM
+ llen
;
4098 if (bytes
< repr_bytes
)
4099 return _("value conflicts with instruction length");
4100 for (num
= 0; num
< imm_expr
->X_add_number
- 1; ++num
)
4101 number_to_chars_littleendian (
4102 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
4103 generic_bignum
[num
],
4104 CHARS_PER_LITTLENUM
);
4106 number_to_chars_littleendian (
4107 ip
->insn_long_opcode
+ num
* CHARS_PER_LITTLENUM
,
4108 generic_bignum
[num
],
4110 memset(ip
->insn_long_opcode
+ repr_bytes
, 0, bytes
- repr_bytes
);
4114 if (bytes
< sizeof(values
[0]) && values
[num
- 1] >> (8 * bytes
) != 0)
4115 return _("value conflicts with instruction length");
4121 md_assemble (char *str
)
4123 struct riscv_cl_insn insn
;
4124 expressionS imm_expr
;
4125 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
4127 /* The architecture and privileged elf attributes should be set
4128 before assembling. */
4129 if (!start_assemble
)
4131 start_assemble
= true;
4133 riscv_set_abi_by_arch ();
4134 if (!riscv_set_default_priv_spec (NULL
))
4138 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
4140 const struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
4141 &imm_reloc
, op_hash
);
4145 if (error
.missing_ext
)
4146 as_bad ("%s `%s', extension `%s' required", error
.msg
,
4147 error
.statement
, error
.missing_ext
);
4149 as_bad ("%s `%s'", error
.msg
, error
.statement
);
4153 if (insn
.insn_mo
->pinfo
== INSN_MACRO
)
4154 macro (&insn
, &imm_expr
, &imm_reloc
);
4156 append_insn (&insn
, &imm_expr
, imm_reloc
);
4160 md_atof (int type
, char *litP
, int *sizeP
)
4162 return ieee_md_atof (type
, litP
, sizeP
, target_big_endian
);
4166 md_number_to_chars (char *buf
, valueT val
, int n
)
4168 if (target_big_endian
)
4169 number_to_chars_bigendian (buf
, val
, n
);
4171 number_to_chars_littleendian (buf
, val
, n
);
4174 const char *md_shortopts
= "O::g::G:";
4178 OPTION_MARCH
= OPTION_MD_BASE
,
4185 OPTION_NO_ARCH_ATTR
,
4187 OPTION_NO_CSR_CHECK
,
4191 OPTION_LITTLE_ENDIAN
,
4195 struct option md_longopts
[] =
4197 {"march", required_argument
, NULL
, OPTION_MARCH
},
4198 {"fPIC", no_argument
, NULL
, OPTION_PIC
},
4199 {"fpic", no_argument
, NULL
, OPTION_PIC
},
4200 {"fno-pic", no_argument
, NULL
, OPTION_NO_PIC
},
4201 {"mabi", required_argument
, NULL
, OPTION_MABI
},
4202 {"mrelax", no_argument
, NULL
, OPTION_RELAX
},
4203 {"mno-relax", no_argument
, NULL
, OPTION_NO_RELAX
},
4204 {"march-attr", no_argument
, NULL
, OPTION_ARCH_ATTR
},
4205 {"mno-arch-attr", no_argument
, NULL
, OPTION_NO_ARCH_ATTR
},
4206 {"mcsr-check", no_argument
, NULL
, OPTION_CSR_CHECK
},
4207 {"mno-csr-check", no_argument
, NULL
, OPTION_NO_CSR_CHECK
},
4208 {"misa-spec", required_argument
, NULL
, OPTION_MISA_SPEC
},
4209 {"mpriv-spec", required_argument
, NULL
, OPTION_MPRIV_SPEC
},
4210 {"mbig-endian", no_argument
, NULL
, OPTION_BIG_ENDIAN
},
4211 {"mlittle-endian", no_argument
, NULL
, OPTION_LITTLE_ENDIAN
},
4213 {NULL
, no_argument
, NULL
, 0}
4215 size_t md_longopts_size
= sizeof (md_longopts
);
4218 md_parse_option (int c
, const char *arg
)
4223 /* List all avaiable extensions. */
4224 if (strcmp (arg
, "help") == 0)
4226 riscv_print_extensions ();
4227 exit (EXIT_SUCCESS
);
4229 default_arch_with_ext
= arg
;
4233 riscv_opts
.pic
= false;
4237 riscv_opts
.pic
= true;
4241 if (strcmp (arg
, "ilp32") == 0)
4242 riscv_set_abi (32, FLOAT_ABI_SOFT
, false);
4243 else if (strcmp (arg
, "ilp32e") == 0)
4244 riscv_set_abi (32, FLOAT_ABI_SOFT
, true);
4245 else if (strcmp (arg
, "ilp32f") == 0)
4246 riscv_set_abi (32, FLOAT_ABI_SINGLE
, false);
4247 else if (strcmp (arg
, "ilp32d") == 0)
4248 riscv_set_abi (32, FLOAT_ABI_DOUBLE
, false);
4249 else if (strcmp (arg
, "ilp32q") == 0)
4250 riscv_set_abi (32, FLOAT_ABI_QUAD
, false);
4251 else if (strcmp (arg
, "lp64") == 0)
4252 riscv_set_abi (64, FLOAT_ABI_SOFT
, false);
4253 else if (strcmp (arg
, "lp64e") == 0)
4254 riscv_set_abi (64, FLOAT_ABI_SOFT
, true);
4255 else if (strcmp (arg
, "lp64f") == 0)
4256 riscv_set_abi (64, FLOAT_ABI_SINGLE
, false);
4257 else if (strcmp (arg
, "lp64d") == 0)
4258 riscv_set_abi (64, FLOAT_ABI_DOUBLE
, false);
4259 else if (strcmp (arg
, "lp64q") == 0)
4260 riscv_set_abi (64, FLOAT_ABI_QUAD
, false);
4263 explicit_mabi
= true;
4267 riscv_opts
.relax
= true;
4270 case OPTION_NO_RELAX
:
4271 riscv_opts
.relax
= false;
4274 case OPTION_ARCH_ATTR
:
4275 riscv_opts
.arch_attr
= true;
4278 case OPTION_NO_ARCH_ATTR
:
4279 riscv_opts
.arch_attr
= false;
4282 case OPTION_CSR_CHECK
:
4283 riscv_opts
.csr_check
= true;
4286 case OPTION_NO_CSR_CHECK
:
4287 riscv_opts
.csr_check
= false;
4290 case OPTION_MISA_SPEC
:
4291 return riscv_set_default_isa_spec (arg
);
4293 case OPTION_MPRIV_SPEC
:
4294 return riscv_set_default_priv_spec (arg
);
4296 case OPTION_BIG_ENDIAN
:
4297 target_big_endian
= 1;
4300 case OPTION_LITTLE_ENDIAN
:
4301 target_big_endian
= 0;
4312 riscv_after_parse_args (void)
4314 /* The --with-arch is optional for now, so we still need to set the xlen
4315 according to the default_arch, which is set by the --target. */
4318 if (strcmp (default_arch
, "riscv32") == 0)
4320 else if (strcmp (default_arch
, "riscv64") == 0)
4323 as_bad ("unknown default architecture `%s'", default_arch
);
4326 /* Set default specs. */
4327 if (default_isa_spec
== ISA_SPEC_CLASS_NONE
)
4328 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC
);
4329 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
4330 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC
);
4332 riscv_set_arch (default_arch_with_ext
);
4334 /* If the CIE to be produced has not been overridden on the command line,
4335 then produce version 3 by default. This allows us to use the full
4336 range of registers in a .cfi_return_column directive. */
4337 if (flag_dwarf_cie_version
== -1)
4338 flag_dwarf_cie_version
= 3;
4341 bool riscv_parse_name (const char *name
, struct expressionS
*ep
,
4342 enum expr_mode mode
)
4347 if (!probing_insn_operands
)
4350 gas_assert (mode
== expr_normal
);
4352 regno
= reg_lookup_internal (name
, RCLASS_GPR
);
4353 if (regno
== (unsigned int)-1)
4356 if (symbol_find (name
) != NULL
)
4359 /* Create a symbol without adding it to the symbol table yet.
4360 Insertion will happen only once we commit to using the insn
4361 we're probing operands for. */
4362 for (sym
= deferred_sym_rootP
; sym
; sym
= symbol_next (sym
))
4363 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
4367 for (sym
= orphan_sym_rootP
; sym
; sym
= symbol_next (sym
))
4368 if (strcmp (name
, S_GET_NAME (sym
)) == 0)
4370 symbol_remove (sym
, &orphan_sym_rootP
, &orphan_sym_lastP
);
4374 sym
= symbol_create (name
, undefined_section
,
4375 &zero_address_frag
, 0);
4377 symbol_append (sym
, deferred_sym_lastP
, &deferred_sym_rootP
,
4378 &deferred_sym_lastP
);
4381 ep
->X_op
= O_symbol
;
4382 ep
->X_add_symbol
= sym
;
4383 ep
->X_add_number
= 0;
4389 md_pcrel_from (fixS
*fixP
)
4391 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
4394 /* Apply a fixup to the object file. */
4397 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
4399 unsigned int subtype
;
4400 bfd_byte
*buf
= (bfd_byte
*) (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
);
4401 bool relaxable
= false;
4405 /* Remember value for tc_gen_reloc. */
4406 fixP
->fx_addnumber
= *valP
;
4408 switch (fixP
->fx_r_type
)
4410 case BFD_RELOC_RISCV_HI20
:
4411 case BFD_RELOC_RISCV_LO12_I
:
4412 case BFD_RELOC_RISCV_LO12_S
:
4413 bfd_putl32 (riscv_apply_const_reloc (fixP
->fx_r_type
, *valP
)
4414 | bfd_getl32 (buf
), buf
);
4415 if (fixP
->fx_addsy
== NULL
)
4416 fixP
->fx_done
= true;
4420 case BFD_RELOC_RISCV_GOT_HI20
:
4421 /* R_RISCV_GOT_HI20 and the following R_RISCV_LO12_I are relaxable
4422 only if it is created as a result of la or lga assembler macros. */
4423 if (fixP
->tc_fix_data
.source_macro
== M_LA
4424 || fixP
->tc_fix_data
.source_macro
== M_LGA
)
4428 case BFD_RELOC_RISCV_ADD8
:
4429 case BFD_RELOC_RISCV_ADD16
:
4430 case BFD_RELOC_RISCV_ADD32
:
4431 case BFD_RELOC_RISCV_ADD64
:
4432 case BFD_RELOC_RISCV_SUB6
:
4433 case BFD_RELOC_RISCV_SUB8
:
4434 case BFD_RELOC_RISCV_SUB16
:
4435 case BFD_RELOC_RISCV_SUB32
:
4436 case BFD_RELOC_RISCV_SUB64
:
4437 case BFD_RELOC_RISCV_RELAX
:
4438 /* cvt_frag_to_fill () has called output_leb128 (). */
4439 case BFD_RELOC_RISCV_SET_ULEB128
:
4440 case BFD_RELOC_RISCV_SUB_ULEB128
:
4443 case BFD_RELOC_RISCV_TPREL_HI20
:
4444 case BFD_RELOC_RISCV_TPREL_LO12_I
:
4445 case BFD_RELOC_RISCV_TPREL_LO12_S
:
4446 case BFD_RELOC_RISCV_TPREL_ADD
:
4447 case BFD_RELOC_RISCV_TLSDESC_HI20
:
4451 case BFD_RELOC_RISCV_TLS_GOT_HI20
:
4452 case BFD_RELOC_RISCV_TLS_GD_HI20
:
4453 case BFD_RELOC_RISCV_TLS_DTPREL32
:
4454 case BFD_RELOC_RISCV_TLS_DTPREL64
:
4455 if (fixP
->fx_addsy
!= NULL
)
4456 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
4458 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
4459 _("TLS relocation against a constant"));
4463 /* Use pc-relative relocation for FDE initial location.
4464 The symbol address in .eh_frame may be adjusted in
4465 _bfd_elf_discard_section_eh_frame, and the content of
4466 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
4467 Therefore, we cannot insert a relocation whose addend symbol is
4468 in .eh_frame. Othrewise, the value may be adjusted twice. */
4469 if (fixP
->fx_addsy
&& fixP
->fx_subsy
4470 && (sub_segment
= S_GET_SEGMENT (fixP
->fx_subsy
))
4471 && strcmp (sub_segment
->name
, ".eh_frame") == 0
4472 && S_GET_VALUE (fixP
->fx_subsy
)
4473 == fixP
->fx_frag
->fr_address
+ fixP
->fx_where
)
4475 fixP
->fx_r_type
= BFD_RELOC_RISCV_32_PCREL
;
4476 fixP
->fx_subsy
= NULL
;
4483 case BFD_RELOC_RISCV_CFA
:
4484 if (fixP
->fx_addsy
&& fixP
->fx_subsy
)
4486 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4487 fixP
->fx_next
->fx_addsy
= fixP
->fx_subsy
;
4488 fixP
->fx_next
->fx_subsy
= NULL
;
4489 fixP
->fx_next
->fx_offset
= 0;
4490 fixP
->fx_subsy
= NULL
;
4492 switch (fixP
->fx_r_type
)
4495 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD64
;
4496 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB64
;
4500 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD32
;
4501 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4505 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD16
;
4506 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4510 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD8
;
4511 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4514 case BFD_RELOC_RISCV_CFA
:
4515 /* Load the byte to get the subtype. */
4516 subtype
= bfd_get_8 (NULL
, &((fragS
*) (fixP
->fx_frag
->fr_opcode
))->fr_literal
[fixP
->fx_where
]);
4517 loc
= fixP
->fx_frag
->fr_fix
- (subtype
& 7);
4520 case DW_CFA_advance_loc1
:
4521 fixP
->fx_where
= loc
+ 1;
4522 fixP
->fx_next
->fx_where
= loc
+ 1;
4523 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET8
;
4524 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
4527 case DW_CFA_advance_loc2
:
4529 fixP
->fx_next
->fx_size
= 2;
4530 fixP
->fx_where
= loc
+ 1;
4531 fixP
->fx_next
->fx_where
= loc
+ 1;
4532 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET16
;
4533 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
4536 case DW_CFA_advance_loc4
:
4538 fixP
->fx_next
->fx_size
= 4;
4539 fixP
->fx_where
= loc
;
4540 fixP
->fx_next
->fx_where
= loc
;
4541 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET32
;
4542 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
4546 if (subtype
< 0x80 && (subtype
& 0x40))
4548 /* DW_CFA_advance_loc */
4549 fixP
->fx_frag
= (fragS
*) fixP
->fx_frag
->fr_opcode
;
4550 fixP
->fx_next
->fx_frag
= fixP
->fx_frag
;
4551 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET6
;
4552 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB6
;
4555 as_fatal (_("internal: bad CFA value #%d"), subtype
);
4561 /* This case is unreachable. */
4568 /* If we are deleting this reloc entry, we must fill in the
4569 value now. This can happen if we have a .word which is not
4570 resolved when it appears but is later defined. */
4571 if (fixP
->fx_addsy
== NULL
)
4573 gas_assert (fixP
->fx_size
<= sizeof (valueT
));
4574 md_number_to_chars ((char *) buf
, *valP
, fixP
->fx_size
);
4579 case BFD_RELOC_RISCV_JMP
:
4582 /* Fill in a tentative value to improve objdump readability. */
4583 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4584 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4585 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_JTYPE_IMM (delta
), buf
);
4586 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4591 case BFD_RELOC_12_PCREL
:
4594 /* Fill in a tentative value to improve objdump readability. */
4595 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4596 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4597 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_BTYPE_IMM (delta
), buf
);
4598 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4603 case BFD_RELOC_RISCV_RVC_BRANCH
:
4606 /* Fill in a tentative value to improve objdump readability. */
4607 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4608 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4609 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CBTYPE_IMM (delta
), buf
);
4610 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4615 case BFD_RELOC_RISCV_RVC_JUMP
:
4618 /* Fill in a tentative value to improve objdump readability. */
4619 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4620 bfd_vma delta
= target
- md_pcrel_from (fixP
);
4621 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CJTYPE_IMM (delta
), buf
);
4622 if (!riscv_opts
.relax
&& S_IS_LOCAL (fixP
->fx_addsy
))
4627 case BFD_RELOC_RISCV_CALL
:
4628 case BFD_RELOC_RISCV_CALL_PLT
:
4629 case BFD_RELOC_RISCV_TLSDESC_LOAD_LO12
:
4630 case BFD_RELOC_RISCV_TLSDESC_ADD_LO12
:
4631 case BFD_RELOC_RISCV_TLSDESC_CALL
:
4635 case BFD_RELOC_RISCV_PCREL_HI20
:
4636 /* Record and evaluate the pcrel_hi relocation with local symbol.
4637 Fill in a tentative value to improve objdump readability for -mrelax,
4638 and set fx_done for -mno-relax. */
4640 && S_IS_LOCAL (fixP
->fx_addsy
)
4641 && S_GET_SEGMENT (fixP
->fx_addsy
) == seg
)
4643 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4644 bfd_vma value
= target
- md_pcrel_from (fixP
);
4646 /* Record PCREL_HI20. */
4647 if (!riscv_record_pcrel_fixup (riscv_pcrel_hi_fixup_hash
,
4648 md_pcrel_from (fixP
),
4651 as_warn (_("too many pcrel_hi"));
4653 bfd_putl32 (bfd_getl32 (buf
)
4654 | ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)),
4656 if (!riscv_opts
.relax
)
4662 case BFD_RELOC_RISCV_PCREL_LO12_S
:
4663 case BFD_RELOC_RISCV_PCREL_LO12_I
:
4664 /* Resolve the pcrel_lo relocation with local symbol.
4665 Fill in a tentative value to improve objdump readability for -mrelax,
4666 and set fx_done for -mno-relax. */
4668 bfd_vma location_pcrel_hi
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
4669 riscv_pcrel_hi_fixup search
= {location_pcrel_hi
, 0, 0};
4670 riscv_pcrel_hi_fixup
*entry
= htab_find (riscv_pcrel_hi_fixup_hash
,
4672 if (entry
&& entry
->symbol
4673 && S_IS_LOCAL (entry
->symbol
)
4674 && S_GET_SEGMENT (entry
->symbol
) == seg
)
4676 bfd_vma target
= entry
->target
;
4677 bfd_vma value
= target
- entry
->address
;
4678 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_ITYPE_IMM (value
), buf
);
4679 /* Relaxations should never be enabled by `.option relax'. */
4680 if (!riscv_opts
.relax
)
4687 case BFD_RELOC_RISCV_ALIGN
:
4691 /* We ignore generic BFD relocations we don't know about. */
4692 if (bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
) != NULL
)
4693 as_fatal (_("internal: bad relocation #%d"), fixP
->fx_r_type
);
4696 if (fixP
->fx_subsy
!= NULL
)
4697 as_bad_subtract (fixP
);
4699 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4700 if (relaxable
&& fixP
->fx_tcbit
&& fixP
->fx_addsy
!= NULL
)
4702 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
4703 fixP
->fx_next
->fx_addsy
= fixP
->fx_next
->fx_subsy
= NULL
;
4704 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_RELAX
;
4705 fixP
->fx_next
->fx_size
= 0;
4709 /* Because the value of .cfi_remember_state may changed after relaxation,
4710 we insert a fix to relocate it again in link-time. */
4713 riscv_pre_output_hook (void)
4715 const frchainS
*frch
;
4718 /* Save the current segment info. */
4720 subsegT subseg
= now_subseg
;
4722 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
4723 for (frch
= seg_info (s
)->frchainP
; frch
; frch
= frch
->frch_next
)
4727 for (frag
= frch
->frch_root
; frag
; frag
= frag
->fr_next
)
4729 if (frag
->fr_type
== rs_cfa
)
4732 expressionS
*symval
;
4734 symval
= symbol_get_value_expression (frag
->fr_symbol
);
4735 exp
.X_op
= O_subtract
;
4736 exp
.X_add_symbol
= symval
->X_add_symbol
;
4737 exp
.X_add_number
= 0;
4738 exp
.X_op_symbol
= symval
->X_op_symbol
;
4740 /* We must set the segment before creating a frag after all
4741 frag chains have been chained together. */
4742 subseg_set (s
, frch
->frch_subseg
);
4744 fix_new_exp (frag
, (int) frag
->fr_offset
, 1, &exp
, 0,
4745 BFD_RELOC_RISCV_CFA
);
4750 /* Restore the original segment info. */
4751 subseg_set (seg
, subseg
);
4754 /* Handle the .option pseudo-op. */
4757 s_riscv_option (int x ATTRIBUTE_UNUSED
)
4759 char *name
= input_line_pointer
, ch
;
4761 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
4762 ++input_line_pointer
;
4763 ch
= *input_line_pointer
;
4764 *input_line_pointer
= '\0';
4766 if (strcmp (name
, "rvc") == 0)
4768 riscv_update_subset (&riscv_rps_as
, "+c");
4769 riscv_reset_subsets_list_arch_str ();
4770 riscv_set_rvc (true);
4772 else if (strcmp (name
, "norvc") == 0)
4774 riscv_update_subset (&riscv_rps_as
, "-c");
4775 riscv_reset_subsets_list_arch_str ();
4776 riscv_set_rvc (false);
4778 else if (strcmp (name
, "pic") == 0)
4779 riscv_opts
.pic
= true;
4780 else if (strcmp (name
, "nopic") == 0)
4781 riscv_opts
.pic
= false;
4782 else if (strcmp (name
, "relax") == 0)
4783 riscv_opts
.relax
= true;
4784 else if (strcmp (name
, "norelax") == 0)
4785 riscv_opts
.relax
= false;
4786 else if (strcmp (name
, "csr-check") == 0)
4787 riscv_opts
.csr_check
= true;
4788 else if (strcmp (name
, "no-csr-check") == 0)
4789 riscv_opts
.csr_check
= false;
4790 else if (strncmp (name
, "arch,", 5) == 0)
4793 if (ISSPACE (*name
) && *name
!= '\0')
4795 riscv_update_subset (&riscv_rps_as
, name
);
4796 riscv_reset_subsets_list_arch_str ();
4798 riscv_set_rvc (false);
4799 if (riscv_subset_supports (&riscv_rps_as
, "c")
4800 || riscv_subset_supports (&riscv_rps_as
, "zca"))
4801 riscv_set_rvc (true);
4803 if (riscv_subset_supports (&riscv_rps_as
, "ztso"))
4806 else if (strcmp (name
, "push") == 0)
4808 struct riscv_option_stack
*s
;
4810 s
= XNEW (struct riscv_option_stack
);
4811 s
->next
= riscv_opts_stack
;
4812 s
->options
= riscv_opts
;
4813 s
->subset_list
= riscv_rps_as
.subset_list
;
4814 riscv_opts_stack
= s
;
4815 riscv_rps_as
.subset_list
= riscv_copy_subset_list (s
->subset_list
);
4817 else if (strcmp (name
, "pop") == 0)
4819 struct riscv_option_stack
*s
;
4821 s
= riscv_opts_stack
;
4823 as_bad (_(".option pop with no .option push"));
4826 riscv_subset_list_t
*release_subsets
= riscv_rps_as
.subset_list
;
4827 riscv_opts_stack
= s
->next
;
4828 riscv_opts
= s
->options
;
4829 riscv_rps_as
.subset_list
= s
->subset_list
;
4830 riscv_release_subset_list (release_subsets
);
4836 as_warn (_("unrecognized .option directive: %s"), name
);
4838 *input_line_pointer
= ch
;
4839 demand_empty_rest_of_line ();
4842 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4843 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4844 use in DWARF debug information. */
4847 s_dtprel (int bytes
)
4854 if (ex
.X_op
!= O_symbol
)
4856 as_bad (_("unsupported use of %s"), (bytes
== 8
4859 ignore_rest_of_line ();
4862 p
= frag_more (bytes
);
4863 md_number_to_chars (p
, 0, bytes
);
4864 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, bytes
, &ex
, false,
4866 ? BFD_RELOC_RISCV_TLS_DTPREL64
4867 : BFD_RELOC_RISCV_TLS_DTPREL32
));
4869 demand_empty_rest_of_line ();
4873 riscv_make_nops (char *buf
, bfd_vma bytes
)
4877 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4878 means we are not within a valid instruction sequence. It is thus safe
4879 to use a zero byte, even though that is not a valid instruction. */
4883 /* Use at most one 2-byte NOP. */
4884 if ((bytes
- i
) % 4 == 2)
4886 number_to_chars_littleendian (buf
+ i
, RVC_NOP
, 2);
4890 /* Fill the remainder with 4-byte NOPs. */
4891 for ( ; i
< bytes
; i
+= 4)
4892 number_to_chars_littleendian (buf
+ i
, RISCV_NOP
, 4);
4895 /* Called from md_do_align. Used to create an alignment frag in a
4896 code section by emitting a worst-case NOP sequence that the linker
4897 will later relax to the correct number of NOPs. We can't compute
4898 the correct alignment now because of other linker relaxations. */
4901 riscv_frag_align_code (int n
)
4903 bfd_vma bytes
= (bfd_vma
) 1 << n
;
4904 bfd_vma insn_alignment
= riscv_opts
.rvc
? 2 : 4;
4905 bfd_vma worst_case_bytes
= bytes
- insn_alignment
;
4909 /* If we are moving to a smaller alignment than the instruction size, then no
4910 alignment is required. */
4911 if (bytes
<= insn_alignment
)
4914 /* When not relaxing, riscv_handle_align handles code alignment. */
4915 if (!riscv_opts
.relax
)
4918 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4919 rather than just use frag_more to handle an alignment here? So that we
4920 don't need to call riscv_mapping_state again later, and then only need
4921 to check frag->fr_type to see if it is frag_align_code. */
4922 nops
= frag_more (worst_case_bytes
);
4924 ex
.X_op
= O_constant
;
4925 ex
.X_add_number
= worst_case_bytes
;
4927 riscv_make_nops (nops
, worst_case_bytes
);
4929 fix_new_exp (frag_now
, nops
- frag_now
->fr_literal
, 0,
4930 &ex
, false, BFD_RELOC_RISCV_ALIGN
);
4932 riscv_mapping_state (MAP_INSN
, worst_case_bytes
, true/* fr_align_code */);
4934 /* We need to start a new frag after the alignment which may be removed by
4935 the linker, to prevent the assembler from computing static offsets.
4936 This is necessary to get correct EH info. */
4937 frag_wane (frag_now
);
4943 /* Implement HANDLE_ALIGN. */
4946 riscv_handle_align (fragS
*fragP
)
4948 switch (fragP
->fr_type
)
4951 /* When relaxing, riscv_frag_align_code handles code alignment. */
4952 if (!riscv_opts
.relax
)
4954 bfd_signed_vma bytes
= (fragP
->fr_next
->fr_address
4955 - fragP
->fr_address
- fragP
->fr_fix
);
4956 /* We have 4 byte uncompressed nops. */
4957 bfd_signed_vma size
= 4;
4958 bfd_signed_vma excess
= bytes
% size
;
4959 bfd_boolean odd_padding
= (excess
% 2 == 1);
4960 char *p
= fragP
->fr_literal
+ fragP
->fr_fix
;
4965 /* Insert zeros or compressed nops to get 4 byte alignment. */
4969 riscv_add_odd_padding_symbol (fragP
);
4970 riscv_make_nops (p
, excess
);
4971 fragP
->fr_fix
+= excess
;
4975 /* The frag will be changed to `rs_fill` later. The function
4976 `write_contents` will try to fill the remaining spaces
4977 according to the patterns we give. In this case, we give
4978 a 4 byte uncompressed nop as the pattern, and set the size
4979 of the pattern into `fr_var`. The nop will be output to the
4980 file `fr_offset` times. However, `fr_offset` could be zero
4981 if we don't need to pad the boundary finally. */
4982 riscv_make_nops (p
, size
);
4983 fragP
->fr_var
= size
;
4992 /* This usually called from frag_var. */
4995 riscv_init_frag (fragS
* fragP
, int max_chars
)
4997 /* Do not add mapping symbol to debug sections. */
4998 if (bfd_section_flags (now_seg
) & SEC_DEBUGGING
)
5001 switch (fragP
->fr_type
)
5006 riscv_mapping_state (MAP_DATA
, max_chars
, false/* fr_align_code */);
5009 riscv_mapping_state (MAP_INSN
, max_chars
, true/* fr_align_code */);
5017 md_estimate_size_before_relax (fragS
*fragp
, asection
*segtype
)
5019 return (fragp
->fr_var
= relaxed_branch_length (fragp
, segtype
, false));
5022 /* Translate internal representation of relocation info to BFD target
5026 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
5028 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
5030 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
5031 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
5032 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
5033 reloc
->addend
= fixp
->fx_addnumber
;
5035 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
5036 if (reloc
->howto
== NULL
)
5038 if ((fixp
->fx_r_type
== BFD_RELOC_16
|| fixp
->fx_r_type
== BFD_RELOC_8
)
5039 && fixp
->fx_addsy
!= NULL
&& fixp
->fx_subsy
!= NULL
)
5041 /* We don't have R_RISCV_8/16, but for this special case,
5042 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
5046 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
5047 _("cannot represent %s relocation in object file"),
5048 bfd_get_reloc_code_name (fixp
->fx_r_type
));
5056 riscv_relax_frag (asection
*sec
, fragS
*fragp
, long stretch ATTRIBUTE_UNUSED
)
5058 if (RELAX_BRANCH_P (fragp
->fr_subtype
))
5060 offsetT old_var
= fragp
->fr_var
;
5061 fragp
->fr_var
= relaxed_branch_length (fragp
, sec
, true);
5062 return fragp
->fr_var
- old_var
;
5068 /* Expand far branches to multi-instruction sequences. */
5071 md_convert_frag_branch (fragS
*fragp
)
5079 buf
= (bfd_byte
*)fragp
->fr_literal
+ fragp
->fr_fix
;
5081 exp
.X_op
= O_symbol
;
5082 exp
.X_add_symbol
= fragp
->fr_symbol
;
5083 exp
.X_add_number
= fragp
->fr_offset
;
5085 gas_assert (fragp
->fr_var
== RELAX_BRANCH_LENGTH (fragp
->fr_subtype
));
5087 if (RELAX_BRANCH_RVC (fragp
->fr_subtype
))
5089 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
5093 /* Expand the RVC branch into a RISC-V one. */
5094 insn
= bfd_getl16 (buf
);
5095 rs1
= 8 + ((insn
>> OP_SH_CRS1S
) & OP_MASK_CRS1S
);
5096 if ((insn
& MASK_C_J
) == MATCH_C_J
)
5098 else if ((insn
& MASK_C_JAL
) == MATCH_C_JAL
)
5099 insn
= MATCH_JAL
| (X_RA
<< OP_SH_RD
);
5100 else if ((insn
& MASK_C_BEQZ
) == MATCH_C_BEQZ
)
5101 insn
= MATCH_BEQ
| (rs1
<< OP_SH_RS1
);
5102 else if ((insn
& MASK_C_BNEZ
) == MATCH_C_BNEZ
)
5103 insn
= MATCH_BNE
| (rs1
<< OP_SH_RS1
);
5106 bfd_putl32 (insn
, buf
);
5110 /* Invert the branch condition. Branch over the jump. */
5111 insn
= bfd_getl16 (buf
);
5112 insn
^= MATCH_C_BEQZ
^ MATCH_C_BNEZ
;
5113 insn
|= ENCODE_CBTYPE_IMM (6);
5114 bfd_putl16 (insn
, buf
);
5119 /* Just keep the RVC branch. */
5120 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
5121 ? BFD_RELOC_RISCV_RVC_JUMP
: BFD_RELOC_RISCV_RVC_BRANCH
;
5122 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5123 2, &exp
, false, reloc
);
5132 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
5135 gas_assert (!RELAX_BRANCH_UNCOND (fragp
->fr_subtype
));
5137 /* Invert the branch condition. Branch over the jump. */
5138 insn
= bfd_getl32 (buf
);
5139 insn
^= MATCH_BEQ
^ MATCH_BNE
;
5140 insn
|= ENCODE_BTYPE_IMM (8);
5141 bfd_putl32 (insn
, buf
);
5145 /* Jump to the target. */
5146 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5147 4, &exp
, false, BFD_RELOC_RISCV_JMP
);
5148 bfd_putl32 (MATCH_JAL
, buf
);
5153 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
5154 ? BFD_RELOC_RISCV_JMP
: BFD_RELOC_12_PCREL
;
5155 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
5156 4, &exp
, false, reloc
);
5165 fixp
->fx_file
= fragp
->fr_file
;
5166 fixp
->fx_line
= fragp
->fr_line
;
5168 gas_assert (buf
== (bfd_byte
*)fragp
->fr_literal
5169 + fragp
->fr_fix
+ fragp
->fr_var
);
5171 fragp
->fr_fix
+= fragp
->fr_var
;
5174 /* Relax a machine dependent frag. This returns the amount by which
5175 the current size of the frag should change. */
5178 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT asec ATTRIBUTE_UNUSED
,
5181 gas_assert (RELAX_BRANCH_P (fragp
->fr_subtype
));
5182 md_convert_frag_branch (fragp
);
5186 md_show_usage (FILE *stream
)
5188 fprintf (stream
, _("\
5190 -fpic or -fPIC generate position-independent code\n\
5191 -fno-pic don't generate position-independent code (default)\n\
5192 -march=ISA set the RISC-V architecture\n\
5193 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
5194 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.10, 1.11, 1.12)\n\
5195 -mabi=ABI set the RISC-V ABI\n\
5196 -mrelax enable relax (default)\n\
5197 -mno-relax disable relax\n\
5198 -march-attr generate RISC-V arch attribute\n\
5199 -mno-arch-attr don't generate RISC-V arch attribute\n\
5200 -mcsr-check enable the csr ISA and privilege spec version checks\n\
5201 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
5202 -mbig-endian assemble for big-endian\n\
5203 -mlittle-endian assemble for little-endian\n\
5207 /* Standard calling conventions leave the CFA at SP on entry. */
5210 riscv_cfi_frame_initial_instructions (void)
5212 cfi_add_CFA_def_cfa_register (X_SP
);
5216 tc_riscv_regname_to_dw2regnum (char *regname
)
5220 if ((reg
= reg_lookup_internal (regname
, RCLASS_GPR
)) >= 0)
5223 if ((reg
= reg_lookup_internal (regname
, RCLASS_FPR
)) >= 0)
5226 if ((reg
= reg_lookup_internal (regname
, RCLASS_VECR
)) >= 0)
5229 /* CSRs are numbered 4096 -> 8191. */
5230 if ((reg
= reg_lookup_internal (regname
, RCLASS_CSR
)) >= 0)
5233 as_bad (_("unknown register `%s'"), regname
);
5238 riscv_elf_final_processing (void)
5240 riscv_set_abi_by_arch ();
5241 riscv_release_subset_list (riscv_rps_as
.subset_list
);
5242 elf_elfheader (stdoutput
)->e_flags
|= elf_flags
;
5245 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
5246 since these directives break relaxation when used with symbol deltas. */
5249 s_riscv_leb128 (int sign
)
5252 char *save_in
= input_line_pointer
;
5255 if (sign
&& exp
.X_op
!= O_constant
)
5256 as_bad (_("non-constant .sleb128 is not supported"));
5257 else if (!sign
&& exp
.X_op
!= O_constant
&& exp
.X_op
!= O_subtract
)
5258 as_bad (_(".uleb128 only supports constant or subtract expressions"));
5260 demand_empty_rest_of_line ();
5262 input_line_pointer
= save_in
;
5263 return s_leb128 (sign
);
5266 /* Parse the .insn directive. There are three formats,
5267 Format 1: .insn <type> <operand1>, <operand2>, ...
5268 Format 2: .insn <length>, <value>
5269 Format 3: .insn <value>. */
5272 s_riscv_insn (int x ATTRIBUTE_UNUSED
)
5274 char *str
= input_line_pointer
;
5275 struct riscv_cl_insn insn
;
5276 expressionS imm_expr
;
5277 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
5280 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
5281 ++input_line_pointer
;
5283 save_c
= *input_line_pointer
;
5284 *input_line_pointer
= '\0';
5286 riscv_mapping_state (MAP_INSN
, 0, false/* fr_align_code */);
5288 struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
5289 &imm_reloc
, insn_type_hash
);
5292 char *save_in
= input_line_pointer
;
5293 error
.msg
= riscv_ip_hardcode (str
, &insn
, &imm_expr
, error
.msg
);
5294 input_line_pointer
= save_in
;
5299 if (error
.missing_ext
)
5300 as_bad ("%s `%s', extension `%s' required", error
.msg
, error
.statement
,
5303 as_bad ("%s `%s'", error
.msg
, error
.statement
);
5307 gas_assert (insn
.insn_mo
->pinfo
!= INSN_MACRO
);
5308 append_insn (&insn
, &imm_expr
, imm_reloc
);
5311 *input_line_pointer
= save_c
;
5312 demand_empty_rest_of_line ();
5315 /* Update architecture and privileged elf attributes. If we don't set
5316 them, then try to output the default ones. */
5319 riscv_write_out_attrs (void)
5321 const char *arch_str
, *priv_str
, *p
;
5322 /* versions[0]: major version.
5323 versions[1]: minor version.
5324 versions[2]: revision version. */
5325 unsigned versions
[3] = {0}, number
= 0;
5328 /* Re-write architecture elf attribute. */
5329 arch_str
= riscv_rps_as
.subset_list
->arch_str
;
5330 if (!bfd_elf_add_proc_attr_string (stdoutput
, Tag_RISCV_arch
, arch_str
))
5331 as_fatal (_("error adding attribute: %s"),
5332 bfd_errmsg (bfd_get_error ()));
5334 /* For the file without any instruction, we don't set the default_priv_spec
5335 according to the privileged elf attributes since the md_assemble isn't
5338 && !riscv_set_default_priv_spec (NULL
))
5341 /* If we already have set privileged elf attributes, then no need to do
5342 anything. Otherwise, don't generate or update them when no CSR and
5343 privileged instructions are used. */
5344 if (!explicit_priv_attr
)
5347 RISCV_GET_PRIV_SPEC_NAME (priv_str
, default_priv_spec
);
5349 for (i
= 0; *p
; ++p
)
5351 if (*p
== '.' && i
< 3)
5353 versions
[i
++] = number
;
5356 else if (ISDIGIT (*p
))
5357 number
= (number
* 10) + (*p
- '0');
5360 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str
);
5364 versions
[i
] = number
;
5366 /* Re-write privileged elf attributes. */
5367 if (!bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec
,
5369 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_minor
,
5371 || !bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_revision
,
5373 as_fatal (_("error adding attribute: %s"),
5374 bfd_errmsg (bfd_get_error ()));
5377 /* Add the default contents for the .riscv.attributes section. */
5380 riscv_set_public_attributes (void)
5382 if (riscv_opts
.arch_attr
|| explicit_attr
)
5383 riscv_write_out_attrs ();
5386 /* Scan uleb128 subtraction expressions and insert fixups for them.
5387 e.g., .uleb128 .L1 - .L0
5388 Because relaxation may change the value of the subtraction, we
5389 must resolve them at link-time. */
5392 riscv_insert_uleb128_fixes (bfd
*abfd ATTRIBUTE_UNUSED
,
5393 asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
5395 segment_info_type
*seginfo
= seg_info (sec
);
5398 subseg_set (sec
, 0);
5400 for (fragP
= seginfo
->frchainP
->frch_root
;
5401 fragP
; fragP
= fragP
->fr_next
)
5403 expressionS
*exp
, *exp_dup
;
5405 if (fragP
->fr_type
!= rs_leb128
|| fragP
->fr_symbol
== NULL
)
5408 exp
= symbol_get_value_expression (fragP
->fr_symbol
);
5410 if (exp
->X_op
!= O_subtract
)
5413 /* Only unsigned leb128 can be handled. */
5414 gas_assert (fragP
->fr_subtype
== 0);
5415 exp_dup
= xmemdup (exp
, sizeof (*exp
), sizeof (*exp
));
5416 exp_dup
->X_op
= O_symbol
;
5417 exp_dup
->X_op_symbol
= NULL
;
5419 /* Insert relocations to resolve the subtraction at link-time.
5420 Emit the SET relocation first in riscv. */
5421 exp_dup
->X_add_symbol
= exp
->X_add_symbol
;
5422 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
5423 exp_dup
, 0, BFD_RELOC_RISCV_SET_ULEB128
);
5424 exp_dup
->X_add_symbol
= exp
->X_op_symbol
;
5425 exp_dup
->X_add_number
= 0; /* Set addend of SUB_ULEB128 to zero. */
5426 fix_new_exp (fragP
, fragP
->fr_fix
, 0,
5427 exp_dup
, 0, BFD_RELOC_RISCV_SUB_ULEB128
);
5428 free ((void *) exp_dup
);
5432 /* Called after all assembly has been done. */
5435 riscv_md_finish (void)
5437 riscv_set_public_attributes ();
5438 if (riscv_opts
.relax
)
5439 bfd_map_over_sections (stdoutput
, riscv_insert_uleb128_fixes
, NULL
);
5442 /* Called just before the assembler exits. */
5447 htab_delete (riscv_pcrel_hi_fixup_hash
);
5450 /* Adjust the symbol table. */
5453 riscv_adjust_symtab (void)
5455 bfd_map_over_sections (stdoutput
, riscv_check_mapping_symbols
, (char *) 0);
5456 elf_adjust_symtab ();
5459 /* Given a symbolic attribute NAME, return the proper integer value.
5460 Returns -1 if the attribute is not known. */
5463 riscv_convert_symbolic_attribute (const char *name
)
5472 /* When you modify this table you should
5473 also modify the list in doc/c-riscv.texi. */
5474 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
5478 T(priv_spec_revision
),
5479 T(unaligned_access
),
5488 for (i
= 0; i
< ARRAY_SIZE (attribute_table
); i
++)
5489 if (strcmp (name
, attribute_table
[i
].name
) == 0)
5490 return attribute_table
[i
].tag
;
5495 /* Parse a .attribute directive. */
5498 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED
)
5500 int tag
= obj_elf_vendor_attribute (OBJ_ATTR_PROC
);
5502 obj_attribute
*attr
;
5504 explicit_attr
= true;
5507 case Tag_RISCV_arch
:
5509 attr
= elf_known_obj_attributes_proc (stdoutput
);
5510 if (!start_assemble
)
5511 riscv_set_arch (attr
[Tag_RISCV_arch
].s
);
5513 as_fatal (_("architecture elf attributes must set before "
5514 "any instructions"));
5516 if (old_xlen
!= xlen
)
5518 /* We must re-init bfd again if xlen is changed. */
5519 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
5520 bfd_find_target (riscv_target_format (), stdoutput
);
5522 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
5523 as_warn (_("could not set architecture and machine"));
5527 case Tag_RISCV_priv_spec
:
5528 case Tag_RISCV_priv_spec_minor
:
5529 case Tag_RISCV_priv_spec_revision
:
5531 as_fatal (_("privileged elf attributes must set before "
5532 "any instructions"));
5540 /* Mark symbol that it follows a variant CC convention. */
5543 s_variant_cc (int ignored ATTRIBUTE_UNUSED
)
5549 elf_symbol_type
*elfsym
;
5551 c
= get_symbol_name (&name
);
5553 as_bad (_("missing symbol name for .variant_cc directive"));
5554 sym
= symbol_find_or_make (name
);
5555 restore_line_pointer (c
);
5556 demand_empty_rest_of_line ();
5558 bfdsym
= symbol_get_bfdsym (sym
);
5559 elfsym
= elf_symbol_from (bfdsym
);
5560 gas_assert (elfsym
);
5561 elfsym
->internal_elf_sym
.st_other
|= STO_RISCV_VARIANT_CC
;
5564 /* Same as elf_copy_symbol_attributes, but without copying st_other.
5565 This is needed so RISC-V specific st_other values can be independently
5566 specified for an IFUNC resolver (that is called by the dynamic linker)
5567 and the symbol it resolves (aliased to the resolver). In particular,
5568 if a function symbol has special st_other value set via directives,
5569 then attaching an IFUNC resolver to that symbol should not override
5570 the st_other setting. Requiring the directive on the IFUNC resolver
5571 symbol would be unexpected and problematic in C code, where the two
5572 symbols appear as two independent function declarations. */
5575 riscv_elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
5577 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
5578 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
5579 /* If size is unset, copy size from src. Because we don't track whether
5580 .size has been used, we can't differentiate .size dest, 0 from the case
5581 where dest's size is unset. */
5582 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
5586 destelf
->size
= XNEW (expressionS
);
5587 *destelf
->size
= *srcelf
->size
;
5589 S_SET_SIZE (dest
, S_GET_SIZE (src
));
5593 /* RISC-V pseudo-ops table. */
5594 static const pseudo_typeS riscv_pseudo_table
[] =
5596 {"option", s_riscv_option
, 0},
5600 {"dtprelword", s_dtprel
, 4},
5601 {"dtpreldword", s_dtprel
, 8},
5602 {"uleb128", s_riscv_leb128
, 0},
5603 {"sleb128", s_riscv_leb128
, 1},
5604 {"insn", s_riscv_insn
, 0},
5605 {"attribute", s_riscv_attribute
, 0},
5606 {"variant_cc", s_variant_cc
, 0},
5607 {"float16", float_cons
, 'h'},
5613 riscv_pop_insert (void)
5615 extern void pop_insert (const pseudo_typeS
*);
5617 pop_insert (riscv_pseudo_table
);