1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2022 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. */
48 /* The frag that contains the instruction. */
51 /* The offset into FRAG of the first instruction byte. */
54 /* The relocs associated with the instruction, if any. */
58 /* All RISC-V CSR belong to one of these classes. */
64 CSR_CLASS_I_32
, /* rv32 only */
65 CSR_CLASS_F
, /* f-ext only */
66 CSR_CLASS_ZKR
, /* zkr only */
67 CSR_CLASS_V
, /* rvv only */
68 CSR_CLASS_DEBUG
/* debug CSR */
71 /* This structure holds all restricted conditions for a CSR. */
72 struct riscv_csr_extra
74 /* Class to which this CSR belongs. Used to decide whether or
75 not this CSR is legal in the current -march context. */
76 enum riscv_csr_class csr_class
;
78 /* CSR may have differnet numbers in the previous priv spec. */
81 /* Record the CSR is defined/valid in which versions. */
82 enum riscv_spec_class define_version
;
84 /* Record the CSR is aborted/invalid from which versions. If it isn't
85 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
86 enum riscv_spec_class abort_version
;
88 /* The CSR may have more than one setting. */
89 struct riscv_csr_extra
*next
;
92 /* This structure contains information about errors that occur within the
96 /* General error message */
99 /* Statement that caused the error */
102 /* Missing extension that needs to be enabled */
103 const char* missing_ext
;
107 #define DEFAULT_ARCH "riscv64"
110 #ifndef DEFAULT_RISCV_ATTR
111 #define DEFAULT_RISCV_ATTR 0
114 /* Let riscv_after_parse_args set the default value according to xlen. */
115 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
116 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
119 /* Need to sync the version with RISC-V compiler. */
120 #ifndef DEFAULT_RISCV_ISA_SPEC
121 #define DEFAULT_RISCV_ISA_SPEC "20191213"
124 #ifndef DEFAULT_RISCV_PRIV_SPEC
125 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
128 static const char default_arch
[] = DEFAULT_ARCH
;
129 static const char *default_arch_with_ext
= DEFAULT_RISCV_ARCH_WITH_EXT
;
130 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_NONE
;
131 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
133 static unsigned xlen
= 0; /* The width of an x-register. */
134 static unsigned abi_xlen
= 0; /* The width of a pointer in the ABI. */
135 static bool rve_abi
= false;
138 FLOAT_ABI_DEFAULT
= -1,
144 static enum float_abi float_abi
= FLOAT_ABI_DEFAULT
;
146 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
147 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
149 static unsigned elf_flags
= 0;
151 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
152 Otherwise, return 1. */
155 riscv_set_default_isa_spec (const char *s
)
157 enum riscv_spec_class
class = ISA_SPEC_CLASS_NONE
;
158 RISCV_GET_ISA_SPEC_CLASS (s
, class);
159 if (class == ISA_SPEC_CLASS_NONE
)
161 as_bad ("unknown default ISA spec `%s' set by "
162 "-misa-spec or --with-isa-spec", s
);
166 default_isa_spec
= class;
170 /* Set the default_priv_spec. Find the privileged elf attributes when
171 the input string is NULL. Return 0 if the spec isn't supported.
172 Otherwise, return 1. */
175 riscv_set_default_priv_spec (const char *s
)
177 enum riscv_spec_class
class = PRIV_SPEC_CLASS_NONE
;
178 unsigned major
, minor
, revision
;
181 RISCV_GET_PRIV_SPEC_CLASS (s
, class);
182 if (class != PRIV_SPEC_CLASS_NONE
)
184 default_priv_spec
= class;
190 as_bad (_("unknown default privileged spec `%s' set by "
191 "-mpriv-spec or --with-priv-spec"), s
);
195 /* Set the default_priv_spec by the privileged elf attributes. */
196 attr
= elf_known_obj_attributes_proc (stdoutput
);
197 major
= (unsigned) attr
[Tag_RISCV_priv_spec
].i
;
198 minor
= (unsigned) attr
[Tag_RISCV_priv_spec_minor
].i
;
199 revision
= (unsigned) attr
[Tag_RISCV_priv_spec_revision
].i
;
200 /* Version 0.0.0 is the default value and meningless. */
201 if (major
== 0 && minor
== 0 && revision
== 0)
204 riscv_get_priv_spec_class_from_numbers (major
, minor
, revision
, &class);
205 if (class != PRIV_SPEC_CLASS_NONE
)
207 default_priv_spec
= class;
211 /* Still can not find the privileged spec class. */
212 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
213 "privileged elf attributes"), major
, minor
, revision
);
217 /* This is the set of options which the .option pseudo-op may modify. */
218 struct riscv_set_options
220 int pic
; /* Generate position-independent code. */
221 int rvc
; /* Generate RVC code. */
222 int relax
; /* Emit relocs the linker is allowed to relax. */
223 int arch_attr
; /* Emit architecture and privileged elf attributes. */
224 int csr_check
; /* Enable the CSR checking. */
227 static struct riscv_set_options riscv_opts
=
232 DEFAULT_RISCV_ATTR
, /* arch_attr */
236 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
237 for elf_flags once we have enabled c extension. */
240 riscv_set_rvc (bool rvc_value
)
243 elf_flags
|= EF_RISCV_RVC
;
245 riscv_opts
.rvc
= rvc_value
;
248 /* This linked list records all enabled extensions, which are parsed from
249 the architecture string. The architecture string can be set by the
250 -march option, the elf architecture attributes, and the --with-arch
252 static riscv_subset_list_t
*riscv_subsets
= NULL
;
253 static riscv_parse_subset_t riscv_rps_as
=
255 NULL
, /* subset_list, we will set it later once
256 riscv_opts_stack is created or updated. */
257 as_bad
, /* error_handler. */
259 &default_isa_spec
, /* isa_spec. */
260 true, /* check_unknown_prefixed_ext. */
263 /* This structure is used to hold a stack of .option values. */
264 struct riscv_option_stack
266 struct riscv_option_stack
*next
;
267 struct riscv_set_options options
;
268 riscv_subset_list_t
*subset_list
;
271 static struct riscv_option_stack
*riscv_opts_stack
= NULL
;
273 /* Set which ISA and extensions are available. */
276 riscv_set_arch (const char *s
)
278 if (s
!= NULL
&& strcmp (s
, "") == 0)
280 as_bad (_("the architecture string of -march and elf architecture "
281 "attributes cannot be empty"));
285 if (riscv_subsets
== NULL
)
287 riscv_subsets
= XNEW (riscv_subset_list_t
);
288 riscv_subsets
->head
= NULL
;
289 riscv_subsets
->tail
= NULL
;
290 riscv_rps_as
.subset_list
= riscv_subsets
;
292 riscv_release_subset_list (riscv_subsets
);
293 riscv_parse_subset (&riscv_rps_as
, s
);
295 riscv_set_rvc (false);
296 if (riscv_subset_supports (&riscv_rps_as
, "c"))
297 riscv_set_rvc (true);
300 /* Indicate -mabi option is explictly set. */
301 static bool explicit_mabi
= false;
303 /* Set the abi information. */
306 riscv_set_abi (unsigned new_xlen
, enum float_abi new_float_abi
, bool rve
)
309 float_abi
= new_float_abi
;
313 /* If the -mabi option isn't set, then set the abi according to the
314 ISA string. Otherwise, check if there is any conflict. */
317 riscv_set_abi_by_arch (void)
321 if (riscv_subset_supports (&riscv_rps_as
, "q"))
322 riscv_set_abi (xlen
, FLOAT_ABI_QUAD
, false);
323 else if (riscv_subset_supports (&riscv_rps_as
, "d"))
324 riscv_set_abi (xlen
, FLOAT_ABI_DOUBLE
, false);
325 else if (riscv_subset_supports (&riscv_rps_as
, "e"))
326 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, true);
328 riscv_set_abi (xlen
, FLOAT_ABI_SOFT
, false);
332 gas_assert (abi_xlen
!= 0 && xlen
!= 0 && float_abi
!= FLOAT_ABI_DEFAULT
);
334 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen
, xlen
);
335 else if (abi_xlen
< xlen
)
336 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen
, xlen
);
338 if (riscv_subset_supports (&riscv_rps_as
, "e") && !rve_abi
)
339 as_bad ("only the ilp32e ABI is supported for e extension");
341 if (float_abi
== FLOAT_ABI_SINGLE
342 && !riscv_subset_supports (&riscv_rps_as
, "f"))
343 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
345 else if (float_abi
== FLOAT_ABI_DOUBLE
346 && !riscv_subset_supports (&riscv_rps_as
, "d"))
347 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
349 else if (float_abi
== FLOAT_ABI_QUAD
350 && !riscv_subset_supports (&riscv_rps_as
, "q"))
351 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
355 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
356 elf_flags
&= ~EF_RISCV_FLOAT_ABI
;
357 elf_flags
|= float_abi
<< 1;
360 elf_flags
|= EF_RISCV_RVE
;
363 /* Handle of the OPCODE hash table. */
364 static htab_t op_hash
= NULL
;
366 /* Handle of the type of .insn hash table. */
367 static htab_t insn_type_hash
= NULL
;
369 /* This array holds the chars that always start a comment. If the
370 pre-processor is disabled, these aren't very useful. */
371 const char comment_chars
[] = "#";
373 /* This array holds the chars that only start a comment at the beginning of
374 a line. If the line seems to have the form '# 123 filename'
375 .line and .file directives will appear in the pre-processed output
377 Note that input_file.c hand checks for '#' at the beginning of the
378 first line of the input file. This is because the compiler outputs
379 #NO_APP at the beginning of its output.
381 Also note that C style comments are always supported. */
382 const char line_comment_chars
[] = "#";
384 /* This array holds machine specific line separator characters. */
385 const char line_separator_chars
[] = ";";
387 /* Chars that can be used to separate mant from exp in floating point nums. */
388 const char EXP_CHARS
[] = "eE";
390 /* Chars that mean this number is a floating point constant.
391 As in 0f12.456 or 0d1.2345e12. */
392 const char FLT_CHARS
[] = "rRsSfFdDxXpPhH";
394 /* Indicate we are already assemble any instructions or not. */
395 static bool start_assemble
= false;
397 /* Indicate ELF attributes are explicitly set. */
398 static bool explicit_attr
= false;
400 /* Indicate CSR or priv instructions are explicitly used. */
401 static bool explicit_priv_attr
= false;
403 static char *expr_end
;
405 /* Macros for encoding relaxation state for RVC branches and far jumps. */
406 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
409 | ((uncond) ? 1 : 0) \
412 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
413 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
414 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
415 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
417 /* Is the given value a sign-extended 32-bit value? */
418 #define IS_SEXT_32BIT_NUM(x) \
419 (((x) &~ (offsetT) 0x7fffffff) == 0 \
420 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
422 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
423 #define IS_ZEXT_32BIT_NUM(x) \
424 (((x) &~ (offsetT) 0xffffffff) == 0 \
425 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
427 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
428 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
429 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
430 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
432 /* Determine if an instruction matches an opcode. */
433 #define OPCODE_MATCHES(OPCODE, OP) \
434 (((OPCODE) & MASK_##OP) == MATCH_##OP)
436 /* Create a new mapping symbol for the transition to STATE. */
439 make_mapping_symbol (enum riscv_seg_mstate state
,
456 symbolS
*symbol
= symbol_new (name
, now_seg
, frag
, value
);
457 symbol_get_bfdsym (symbol
)->flags
|= (BSF_NO_FLAGS
| BSF_LOCAL
);
459 /* If .fill or other data filling directive generates zero sized data,
460 or we are adding odd alignemnts, then the mapping symbol for the
461 following code will have the same value. */
464 if (frag
->tc_frag_data
.first_map_symbol
!= NULL
)
466 know (S_GET_VALUE (frag
->tc_frag_data
.first_map_symbol
)
467 == S_GET_VALUE (symbol
));
468 /* Remove the old one. */
469 symbol_remove (frag
->tc_frag_data
.first_map_symbol
,
470 &symbol_rootP
, &symbol_lastP
);
472 frag
->tc_frag_data
.first_map_symbol
= symbol
;
474 if (frag
->tc_frag_data
.last_map_symbol
!= NULL
)
476 /* The mapping symbols should be added in offset order. */
477 know (S_GET_VALUE (frag
->tc_frag_data
.last_map_symbol
)
478 <= S_GET_VALUE (symbol
));
479 /* Remove the old one. */
480 if (S_GET_VALUE (frag
->tc_frag_data
.last_map_symbol
)
481 == S_GET_VALUE (symbol
))
482 symbol_remove (frag
->tc_frag_data
.last_map_symbol
,
483 &symbol_rootP
, &symbol_lastP
);
485 frag
->tc_frag_data
.last_map_symbol
= symbol
;
488 /* Set the mapping state for frag_now. */
491 riscv_mapping_state (enum riscv_seg_mstate to_state
,
494 enum riscv_seg_mstate from_state
=
495 seg_info (now_seg
)->tc_segment_info_data
.map_state
;
497 if (!SEG_NORMAL (now_seg
)
498 /* For now I only add the mapping symbols to text sections.
499 Therefore, the dis-assembler only show the actual contents
500 distribution for text. Other sections will be shown as
501 data without the details. */
502 || !subseg_text_p (now_seg
))
505 /* The mapping symbol should be emitted if not in the right
507 if (from_state
== to_state
)
510 valueT value
= (valueT
) (frag_now_fix () - max_chars
);
511 seg_info (now_seg
)->tc_segment_info_data
.map_state
= to_state
;
512 make_mapping_symbol (to_state
, value
, frag_now
);
515 /* Add the odd bytes of paddings for riscv_handle_align. */
518 riscv_add_odd_padding_symbol (fragS
*frag
)
520 /* If there was already a mapping symbol, it should be
521 removed in the make_mapping_symbol. */
522 make_mapping_symbol (MAP_DATA
, frag
->fr_fix
, frag
);
523 make_mapping_symbol (MAP_INSN
, frag
->fr_fix
+ 1, frag
);
526 /* Remove any excess mapping symbols generated for alignment frags in
527 SEC. We may have created a mapping symbol before a zero byte
528 alignment; remove it if there's a mapping symbol after the
532 riscv_check_mapping_symbols (bfd
*abfd ATTRIBUTE_UNUSED
,
534 void *dummy ATTRIBUTE_UNUSED
)
536 segment_info_type
*seginfo
= seg_info (sec
);
539 if (seginfo
== NULL
|| seginfo
->frchainP
== NULL
)
542 for (fragp
= seginfo
->frchainP
->frch_root
;
544 fragp
= fragp
->fr_next
)
546 symbolS
*last
= fragp
->tc_frag_data
.last_map_symbol
;
547 fragS
*next
= fragp
->fr_next
;
549 if (last
== NULL
|| next
== NULL
)
552 /* Check the last mapping symbol if it is at the boundary of
554 if (S_GET_VALUE (last
) < next
->fr_address
)
556 know (S_GET_VALUE (last
) == next
->fr_address
);
560 if (next
->tc_frag_data
.first_map_symbol
!= NULL
)
562 /* The last mapping symbol overlaps with another one
563 which at the start of the next frag. */
564 symbol_remove (last
, &symbol_rootP
, &symbol_lastP
);
568 if (next
->fr_next
== NULL
)
570 /* The last mapping symbol is at the end of the section. */
571 know (next
->fr_fix
== 0 && next
->fr_var
== 0);
572 symbol_remove (last
, &symbol_rootP
, &symbol_lastP
);
576 /* Since we may have empty frags without any mapping symbols,
577 keep looking until the non-empty frag. */
578 if (next
->fr_address
!= next
->fr_next
->fr_address
)
581 next
= next
->fr_next
;
583 while (next
!= NULL
);
587 /* The default target format to use. */
590 riscv_target_format (void)
592 if (target_big_endian
)
593 return xlen
== 64 ? "elf64-bigriscv" : "elf32-bigriscv";
595 return xlen
== 64 ? "elf64-littleriscv" : "elf32-littleriscv";
598 /* Return the length of instruction INSN. */
600 static inline unsigned int
601 insn_length (const struct riscv_cl_insn
*insn
)
603 return riscv_insn_length (insn
->insn_opcode
);
606 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
609 create_insn (struct riscv_cl_insn
*insn
, const struct riscv_opcode
*mo
)
612 insn
->insn_opcode
= mo
->match
;
618 /* Install INSN at the location specified by its "frag" and "where" fields. */
621 install_insn (const struct riscv_cl_insn
*insn
)
623 char *f
= insn
->frag
->fr_literal
+ insn
->where
;
624 number_to_chars_littleendian (f
, insn
->insn_opcode
, insn_length (insn
));
627 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
628 and install the opcode in the new location. */
631 move_insn (struct riscv_cl_insn
*insn
, fragS
*frag
, long where
)
635 if (insn
->fixp
!= NULL
)
637 insn
->fixp
->fx_frag
= frag
;
638 insn
->fixp
->fx_where
= where
;
643 /* Add INSN to the end of the output. */
646 add_fixed_insn (struct riscv_cl_insn
*insn
)
648 char *f
= frag_more (insn_length (insn
));
649 move_insn (insn
, frag_now
, f
- frag_now
->fr_literal
);
653 add_relaxed_insn (struct riscv_cl_insn
*insn
, int max_chars
, int var
,
654 relax_substateT subtype
, symbolS
*symbol
, offsetT offset
)
656 frag_grow (max_chars
);
657 move_insn (insn
, frag_now
, frag_more (0) - frag_now
->fr_literal
);
658 frag_var (rs_machine_dependent
, max_chars
, var
,
659 subtype
, symbol
, offset
, NULL
);
662 /* Compute the length of a branch sequence, and adjust the stored length
663 accordingly. If FRAGP is NULL, the worst-case length is returned. */
666 relaxed_branch_length (fragS
*fragp
, asection
*sec
, int update
)
668 int jump
, rvc
, length
= 8;
673 jump
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
);
674 rvc
= RELAX_BRANCH_RVC (fragp
->fr_subtype
);
675 length
= RELAX_BRANCH_LENGTH (fragp
->fr_subtype
);
677 /* Assume jumps are in range; the linker will catch any that aren't. */
678 length
= jump
? 4 : 8;
680 if (fragp
->fr_symbol
!= NULL
681 && S_IS_DEFINED (fragp
->fr_symbol
)
682 && !S_IS_WEAK (fragp
->fr_symbol
)
683 && sec
== S_GET_SEGMENT (fragp
->fr_symbol
))
685 offsetT val
= S_GET_VALUE (fragp
->fr_symbol
) + fragp
->fr_offset
;
686 bfd_vma rvc_range
= jump
? RVC_JUMP_REACH
: RVC_BRANCH_REACH
;
687 val
-= fragp
->fr_address
+ fragp
->fr_fix
;
689 if (rvc
&& (bfd_vma
)(val
+ rvc_range
/2) < rvc_range
)
691 else if ((bfd_vma
)(val
+ RISCV_BRANCH_REACH
/2) < RISCV_BRANCH_REACH
)
693 else if (!jump
&& rvc
)
698 fragp
->fr_subtype
= RELAX_BRANCH_ENCODE (jump
, rvc
, length
);
703 /* Information about an opcode name, mnemonics and its value. */
710 /* List for all supported opcode name. */
711 static const struct opcode_name_t opcode_name_list
[] =
756 /* Hash table for lookup opcode name. */
757 static htab_t opcode_names_hash
= NULL
;
759 /* Initialization for hash table of opcode name. */
762 init_opcode_names_hash (void)
764 const struct opcode_name_t
*opcode
;
766 for (opcode
= &opcode_name_list
[0]; opcode
->name
!= NULL
; ++opcode
)
767 if (str_hash_insert (opcode_names_hash
, opcode
->name
, opcode
, 0) != NULL
)
768 as_fatal (_("internal: duplicate %s"), opcode
->name
);
771 /* Find `s` is a valid opcode name or not, return the opcode name info
774 static const struct opcode_name_t
*
775 opcode_name_lookup (char **s
)
779 struct opcode_name_t
*o
;
781 /* Find end of name. */
783 if (is_name_beginner (*e
))
785 while (is_part_of_name (*e
))
788 /* Terminate name. */
792 o
= (struct opcode_name_t
*) str_hash_find (opcode_names_hash
, *s
);
794 /* Advance to next token if one was recognized. */
804 /* All RISC-V registers belong to one of these classes. */
816 static htab_t reg_names_hash
= NULL
;
817 static htab_t csr_extra_hash
= NULL
;
819 #define ENCODE_REG_HASH(cls, n) \
820 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
821 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
822 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
825 hash_reg_name (enum reg_class
class, const char *name
, unsigned n
)
827 void *hash
= ENCODE_REG_HASH (class, n
);
828 if (str_hash_insert (reg_names_hash
, name
, hash
, 0) != NULL
)
829 as_fatal (_("internal: duplicate %s"), name
);
833 hash_reg_names (enum reg_class
class, const char * const names
[], unsigned n
)
837 for (i
= 0; i
< n
; i
++)
838 hash_reg_name (class, names
[i
], i
);
841 /* Init hash table csr_extra_hash to handle CSR. */
844 riscv_init_csr_hash (const char *name
,
846 enum riscv_csr_class
class,
847 enum riscv_spec_class define_version
,
848 enum riscv_spec_class abort_version
)
850 struct riscv_csr_extra
*entry
, *pre_entry
;
851 bool need_enrty
= true;
854 entry
= (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, name
);
855 while (need_enrty
&& entry
!= NULL
)
857 if (entry
->csr_class
== class
858 && entry
->address
== address
859 && entry
->define_version
== define_version
860 && entry
->abort_version
== abort_version
)
870 entry
= XNEW (struct riscv_csr_extra
);
871 entry
->csr_class
= class;
872 entry
->address
= address
;
873 entry
->define_version
= define_version
;
874 entry
->abort_version
= abort_version
;
877 if (pre_entry
== NULL
)
878 str_hash_insert (csr_extra_hash
, name
, entry
, 0);
880 pre_entry
->next
= entry
;
883 /* Return the CSR address after checking the ISA dependency and
884 the privileged spec version.
886 There are one warning and two errors for CSR,
888 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
889 or the privileged spec, report warning only if -mcsr-check is set.
890 Unknown CSR: the CSR has never been defined, report error.
891 Improper CSR: the CSR number over the range (> 0xfff), report error. */
894 riscv_csr_address (const char *csr_name
,
895 struct riscv_csr_extra
*entry
)
897 struct riscv_csr_extra
*saved_entry
= entry
;
898 enum riscv_csr_class csr_class
= entry
->csr_class
;
899 bool need_check_version
= false;
900 bool rv32_only
= true;
901 const char* extension
= NULL
;
906 rv32_only
= (xlen
== 32);
909 need_check_version
= true;
921 case CSR_CLASS_DEBUG
:
924 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class
);
927 if (riscv_opts
.csr_check
)
930 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name
);
932 if (extension
!= NULL
933 && !riscv_subset_supports (&riscv_rps_as
, extension
))
934 as_warn (_("invalid CSR `%s', needs `%s' extension"),
935 csr_name
, extension
);
938 while (entry
!= NULL
)
940 if (!need_check_version
941 || (default_priv_spec
>= entry
->define_version
942 && default_priv_spec
< entry
->abort_version
))
944 /* Find the CSR according to the specific version. */
945 return entry
->address
;
950 /* Can not find the CSR address from the chosen privileged version,
951 so use the newly defined value. */
952 if (riscv_opts
.csr_check
)
954 const char *priv_name
= NULL
;
955 RISCV_GET_PRIV_SPEC_NAME (priv_name
, default_priv_spec
);
956 if (priv_name
!= NULL
)
957 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
958 csr_name
, priv_name
);
961 return saved_entry
->address
;
964 /* Return -1 if the CSR has never been defined. Otherwise, return
968 reg_csr_lookup_internal (const char *s
)
970 struct riscv_csr_extra
*r
=
971 (struct riscv_csr_extra
*) str_hash_find (csr_extra_hash
, s
);
976 return riscv_csr_address (s
, r
);
980 reg_lookup_internal (const char *s
, enum reg_class
class)
984 if (class == RCLASS_CSR
)
985 return reg_csr_lookup_internal (s
);
987 r
= str_hash_find (reg_names_hash
, s
);
988 if (r
== NULL
|| DECODE_REG_CLASS (r
) != class)
991 if (riscv_subset_supports (&riscv_rps_as
, "e")
992 && class == RCLASS_GPR
993 && DECODE_REG_NUM (r
) > 15)
996 return DECODE_REG_NUM (r
);
1000 reg_lookup (char **s
, enum reg_class
class, unsigned int *regnop
)
1006 /* Find end of name. */
1008 if (is_name_beginner (*e
))
1010 while (is_part_of_name (*e
))
1013 /* Terminate name. */
1017 /* Look for the register. Advance to next token if one was recognized. */
1018 if ((reg
= reg_lookup_internal (*s
, class)) >= 0)
1028 arg_lookup (char **s
, const char *const *array
, size_t size
, unsigned *regnop
)
1030 const char *p
= strchr (*s
, ',');
1031 size_t i
, len
= p
? (size_t)(p
- *s
) : strlen (*s
);
1036 for (i
= 0; i
< size
; i
++)
1037 if (array
[i
] != NULL
&& strncmp (array
[i
], *s
, len
) == 0)
1047 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1049 /* For consistency checking, verify that all bits are specified either
1050 by the match/mask part of the instruction definition, or by the
1051 operand list. The `length` could be 0, 4 or 8, 0 for auto detection. */
1054 validate_riscv_insn (const struct riscv_opcode
*opc
, int length
)
1056 const char *oparg
, *opargStart
;
1057 insn_t used_bits
= opc
->mask
;
1059 insn_t required_bits
;
1062 insn_width
= 8 * riscv_insn_length (opc
->match
);
1064 insn_width
= 8 * length
;
1066 required_bits
= ~0ULL >> (64 - insn_width
);
1068 if ((used_bits
& opc
->match
) != (opc
->match
& required_bits
))
1070 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1071 opc
->name
, opc
->args
);
1075 for (oparg
= opc
->args
; *oparg
; ++oparg
)
1083 case 'U': break; /* CRS1, constrained to equal RD. */
1084 case 'c': break; /* CRS1, constrained to equal sp. */
1085 case 'T': /* CRS2, floating point. */
1086 case 'V': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
1087 case 'S': /* CRS1S, floating point. */
1088 case 's': USE_BITS (OP_MASK_CRS1S
, OP_SH_CRS1S
); break;
1089 case 'w': break; /* CRS1S, constrained to equal RD. */
1090 case 'D': /* CRS2S, floating point. */
1091 case 't': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
1092 case 'x': break; /* CRS2S, constrained to equal RD. */
1093 case 'z': break; /* CRS2S, constrained to be x0. */
1094 case '>': /* CITYPE immediate, compressed shift. */
1095 case 'u': /* CITYPE immediate, compressed lui. */
1096 case 'v': /* CITYPE immediate, li to compressed lui. */
1097 case 'o': /* CITYPE immediate, allow zero. */
1098 case 'j': used_bits
|= ENCODE_CITYPE_IMM (-1U); break;
1099 case 'L': used_bits
|= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1100 case 'm': used_bits
|= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1101 case 'n': used_bits
|= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1102 case '6': used_bits
|= ENCODE_CSSTYPE_IMM (-1U); break;
1103 case 'M': used_bits
|= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1104 case 'N': used_bits
|= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1105 case '8': used_bits
|= ENCODE_CIWTYPE_IMM (-1U); break;
1106 case 'K': used_bits
|= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1107 /* CLTYPE and CSTYPE have the same immediate encoding. */
1108 case '5': used_bits
|= ENCODE_CLTYPE_IMM (-1U); break;
1109 case 'k': used_bits
|= ENCODE_CLTYPE_LW_IMM (-1U); break;
1110 case 'l': used_bits
|= ENCODE_CLTYPE_LD_IMM (-1U); break;
1111 case 'p': used_bits
|= ENCODE_CBTYPE_IMM (-1U); break;
1112 case 'a': used_bits
|= ENCODE_CJTYPE_IMM (-1U); break;
1113 case 'F': /* Compressed funct for .insn directive. */
1116 case '6': USE_BITS (OP_MASK_CFUNCT6
, OP_SH_CFUNCT6
); break;
1117 case '4': USE_BITS (OP_MASK_CFUNCT4
, OP_SH_CFUNCT4
); break;
1118 case '3': USE_BITS (OP_MASK_CFUNCT3
, OP_SH_CFUNCT3
); break;
1119 case '2': USE_BITS (OP_MASK_CFUNCT2
, OP_SH_CFUNCT2
); break;
1121 goto unknown_validate_operand
;
1125 goto unknown_validate_operand
;
1127 break; /* end RVC */
1132 case 'f': USE_BITS (OP_MASK_VD
, OP_SH_VD
); break;
1133 case 'e': USE_BITS (OP_MASK_VWD
, OP_SH_VWD
); break;
1134 case 's': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
); break;
1135 case 't': USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1136 case 'u': USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1137 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1138 case 'v': USE_BITS (OP_MASK_VD
, OP_SH_VD
);
1139 USE_BITS (OP_MASK_VS1
, OP_SH_VS1
);
1140 USE_BITS (OP_MASK_VS2
, OP_SH_VS2
); break;
1142 case 'b': used_bits
|= ENCODE_RVV_VB_IMM (-1U); break;
1143 case 'c': used_bits
|= ENCODE_RVV_VC_IMM (-1U); break;
1146 case 'k': USE_BITS (OP_MASK_VIMM
, OP_SH_VIMM
); break;
1147 case 'm': USE_BITS (OP_MASK_VMASK
, OP_SH_VMASK
); break;
1149 goto unknown_validate_operand
;
1151 break; /* end RVV */
1155 case '<': USE_BITS (OP_MASK_SHAMTW
, OP_SH_SHAMTW
); break;
1156 case '>': USE_BITS (OP_MASK_SHAMT
, OP_SH_SHAMT
); break;
1157 case 'A': break; /* Macro operand, must be symbol. */
1158 case 'B': break; /* Macro operand, must be symbol or constant. */
1159 case 'I': break; /* Macro operand, must be constant. */
1160 case 'D': /* RD, floating point. */
1161 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
1162 case 'y': USE_BITS (OP_MASK_BS
, OP_SH_BS
); break;
1163 case 'Y': USE_BITS (OP_MASK_RNUM
, OP_SH_RNUM
); break;
1164 case 'Z': /* RS1, CSR number. */
1165 case 'S': /* RS1, floating point. */
1166 case 's': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
1167 case 'U': /* RS1 and RS2 are the same, floating point. */
1168 USE_BITS (OP_MASK_RS1
, OP_SH_RS1
);
1170 case 'T': /* RS2, floating point. */
1171 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
1172 case 'R': /* RS3, floating point. */
1173 case 'r': USE_BITS (OP_MASK_RS3
, OP_SH_RS3
); break;
1174 case 'm': USE_BITS (OP_MASK_RM
, OP_SH_RM
); break;
1175 case 'E': USE_BITS (OP_MASK_CSR
, OP_SH_CSR
); break;
1176 case 'P': USE_BITS (OP_MASK_PRED
, OP_SH_PRED
); break;
1177 case 'Q': USE_BITS (OP_MASK_SUCC
, OP_SH_SUCC
); break;
1178 case 'o': /* ITYPE immediate, load displacement. */
1179 case 'j': used_bits
|= ENCODE_ITYPE_IMM (-1U); break;
1180 case 'a': used_bits
|= ENCODE_JTYPE_IMM (-1U); break;
1181 case 'p': used_bits
|= ENCODE_BTYPE_IMM (-1U); break;
1182 case 'f': /* Fall through. */
1183 case 'q': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
1184 case 'u': used_bits
|= ENCODE_UTYPE_IMM (-1U); break;
1185 case 'z': break; /* Zero immediate. */
1186 case '[': break; /* Unused operand. */
1187 case ']': break; /* Unused operand. */
1188 case '0': break; /* AMO displacement, must to zero. */
1189 case '1': break; /* Relaxation operand. */
1190 case 'F': /* Funct for .insn directive. */
1193 case '7': USE_BITS (OP_MASK_FUNCT7
, OP_SH_FUNCT7
); break;
1194 case '3': USE_BITS (OP_MASK_FUNCT3
, OP_SH_FUNCT3
); break;
1195 case '2': USE_BITS (OP_MASK_FUNCT2
, OP_SH_FUNCT2
); break;
1197 goto unknown_validate_operand
;
1200 case 'O': /* Opcode for .insn directive. */
1203 case '4': USE_BITS (OP_MASK_OP
, OP_SH_OP
); break;
1204 case '2': USE_BITS (OP_MASK_OP2
, OP_SH_OP2
); break;
1206 goto unknown_validate_operand
;
1210 unknown_validate_operand
:
1211 as_bad (_("internal: bad RISC-V opcode "
1212 "(unknown operand type `%s'): %s %s"),
1213 opargStart
, opc
->name
, opc
->args
);
1218 if (used_bits
!= required_bits
)
1220 as_bad (_("internal: bad RISC-V opcode "
1221 "(bits 0x%lx undefined): %s %s"),
1222 ~(unsigned long)(used_bits
& required_bits
),
1223 opc
->name
, opc
->args
);
1231 struct percent_op_match
1234 bfd_reloc_code_real_type reloc
;
1237 /* Common hash table initialization function for instruction and .insn
1241 init_opcode_hash (const struct riscv_opcode
*opcodes
,
1242 bool insn_directive_p
)
1246 htab_t hash
= str_htab_create ();
1247 while (opcodes
[i
].name
)
1249 const char *name
= opcodes
[i
].name
;
1250 if (str_hash_insert (hash
, name
, &opcodes
[i
], 0) != NULL
)
1251 as_fatal (_("internal: duplicate %s"), name
);
1255 if (opcodes
[i
].pinfo
!= INSN_MACRO
)
1257 if (insn_directive_p
)
1258 length
= ((name
[0] == 'c') ? 2 : 4);
1260 length
= 0; /* Let assembler determine the length. */
1261 if (!validate_riscv_insn (&opcodes
[i
], length
))
1262 as_fatal (_("internal: broken assembler. "
1263 "No assembly attempted"));
1266 gas_assert (!insn_directive_p
);
1269 while (opcodes
[i
].name
&& !strcmp (opcodes
[i
].name
, name
));
1275 /* This function is called once, at assembler startup time. It should set up
1276 all the tables, etc. that the MD part of the assembler will need. */
1281 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
1283 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
1284 as_warn (_("could not set architecture and machine"));
1286 op_hash
= init_opcode_hash (riscv_opcodes
, false);
1287 insn_type_hash
= init_opcode_hash (riscv_insn_types
, true);
1289 reg_names_hash
= str_htab_create ();
1290 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_numeric
, NGPR
);
1291 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_abi
, NGPR
);
1292 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_numeric
, NFPR
);
1293 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_abi
, NFPR
);
1294 hash_reg_names (RCLASS_VECR
, riscv_vecr_names_numeric
, NVECR
);
1295 hash_reg_names (RCLASS_VECM
, riscv_vecm_names_numeric
, NVECM
);
1296 /* Add "fp" as an alias for "s0". */
1297 hash_reg_name (RCLASS_GPR
, "fp", 8);
1299 /* Create and insert CSR hash tables. */
1300 csr_extra_hash
= str_htab_create ();
1301 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1302 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1303 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1304 DECLARE_CSR(name, num, class, define_version, abort_version);
1305 #include "opcode/riscv-opc.h"
1308 opcode_names_hash
= str_htab_create ();
1309 init_opcode_names_hash ();
1311 /* Set the default alignment for the text section. */
1312 record_alignment (text_section
, riscv_opts
.rvc
? 1 : 2);
1316 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type
, bfd_vma value
)
1323 case BFD_RELOC_RISCV_HI20
:
1324 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1326 case BFD_RELOC_RISCV_LO12_S
:
1327 return ENCODE_STYPE_IMM (value
);
1329 case BFD_RELOC_RISCV_LO12_I
:
1330 return ENCODE_ITYPE_IMM (value
);
1337 /* Output an instruction. IP is the instruction information.
1338 ADDRESS_EXPR is an operand of the instruction to be used with
1342 append_insn (struct riscv_cl_insn
*ip
, expressionS
*address_expr
,
1343 bfd_reloc_code_real_type reloc_type
)
1345 dwarf2_emit_insn (0);
1347 if (reloc_type
!= BFD_RELOC_UNUSED
)
1349 reloc_howto_type
*howto
;
1351 gas_assert (address_expr
);
1352 if (reloc_type
== BFD_RELOC_12_PCREL
1353 || reloc_type
== BFD_RELOC_RISCV_JMP
)
1355 int j
= reloc_type
== BFD_RELOC_RISCV_JMP
;
1356 int best_case
= riscv_insn_length (ip
->insn_opcode
);
1357 unsigned worst_case
= relaxed_branch_length (NULL
, NULL
, 0);
1359 if (now_seg
== absolute_section
)
1361 as_bad (_("relaxable branches not supported in absolute section"));
1365 add_relaxed_insn (ip
, worst_case
, best_case
,
1366 RELAX_BRANCH_ENCODE (j
, best_case
== 2, worst_case
),
1367 address_expr
->X_add_symbol
,
1368 address_expr
->X_add_number
);
1373 howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
1375 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1378 ip
->fixp
= fix_new_exp (ip
->frag
, ip
->where
,
1379 bfd_get_reloc_size (howto
),
1380 address_expr
, false, reloc_type
);
1382 ip
->fixp
->fx_tcbit
= riscv_opts
.relax
;
1386 add_fixed_insn (ip
);
1389 /* We need to start a new frag after any instruction that can be
1390 optimized away or compressed by the linker during relaxation, to prevent
1391 the assembler from computing static offsets across such an instruction.
1392 This is necessary to get correct EH info. */
1393 if (reloc_type
== BFD_RELOC_RISCV_HI20
1394 || reloc_type
== BFD_RELOC_RISCV_PCREL_HI20
1395 || reloc_type
== BFD_RELOC_RISCV_TPREL_HI20
1396 || reloc_type
== BFD_RELOC_RISCV_TPREL_ADD
)
1398 frag_wane (frag_now
);
1403 /* Build an instruction created by a macro expansion. This is passed
1404 a pointer to the count of instructions created so far, an expression,
1405 the name of the instruction to build, an operand format string, and
1406 corresponding arguments. */
1409 macro_build (expressionS
*ep
, const char *name
, const char *fmt
, ...)
1411 const struct riscv_opcode
*mo
;
1412 struct riscv_cl_insn insn
;
1413 bfd_reloc_code_real_type r
;
1415 const char *fmtStart
;
1417 va_start (args
, fmt
);
1419 r
= BFD_RELOC_UNUSED
;
1420 mo
= (struct riscv_opcode
*) str_hash_find (op_hash
, name
);
1423 /* Find a non-RVC variant of the instruction. append_insn will compress
1425 while (riscv_insn_length (mo
->match
) < 4)
1427 gas_assert (strcmp (name
, mo
->name
) == 0);
1429 create_insn (&insn
, mo
);
1439 INSERT_OPERAND (VD
, insn
, va_arg (args
, int));
1442 INSERT_OPERAND (VS1
, insn
, va_arg (args
, int));
1445 INSERT_OPERAND (VS2
, insn
, va_arg (args
, int));
1449 int reg
= va_arg (args
, int);
1452 INSERT_OPERAND (VMASK
, insn
, 1);
1457 INSERT_OPERAND (VMASK
, insn
, 0);
1461 goto unknown_macro_argument
;
1464 goto unknown_macro_argument
;
1469 INSERT_OPERAND (RD
, insn
, va_arg (args
, int));
1472 INSERT_OPERAND (RS1
, insn
, va_arg (args
, int));
1475 INSERT_OPERAND (RS2
, insn
, va_arg (args
, int));
1481 gas_assert (ep
!= NULL
);
1482 r
= va_arg (args
, int);
1490 unknown_macro_argument
:
1491 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart
);
1496 gas_assert (r
== BFD_RELOC_UNUSED
? ep
== NULL
: ep
!= NULL
);
1498 append_insn (&insn
, ep
, r
);
1501 /* Build an instruction created by a macro expansion. Like md_assemble but
1502 accept a printf-style format string and arguments. */
1505 md_assemblef (const char *format
, ...)
1511 va_start (ap
, format
);
1513 r
= vasprintf (&buf
, format
, ap
);
1516 as_fatal (_("internal: vasprintf failed"));
1524 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1528 normalize_constant_expr (expressionS
*ex
)
1532 if ((ex
->X_op
== O_constant
|| ex
->X_op
== O_symbol
)
1533 && IS_ZEXT_32BIT_NUM (ex
->X_add_number
))
1534 ex
->X_add_number
= (((ex
->X_add_number
& 0xffffffff) ^ 0x80000000)
1538 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1539 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1542 check_absolute_expr (struct riscv_cl_insn
*ip
, expressionS
*ex
,
1545 if (ex
->X_op
== O_big
)
1546 as_bad (_("unsupported large constant"));
1547 else if (maybe_csr
&& ex
->X_op
== O_symbol
)
1548 as_bad (_("unknown CSR `%s'"),
1549 S_GET_NAME (ex
->X_add_symbol
));
1550 else if (ex
->X_op
!= O_constant
)
1551 as_bad (_("instruction %s requires absolute expression"),
1553 normalize_constant_expr (ex
);
1557 make_internal_label (void)
1559 return (symbolS
*) local_symbol_make (FAKE_LABEL_NAME
, now_seg
, frag_now
,
1563 /* Load an entry from the GOT. */
1566 pcrel_access (int destreg
, int tempreg
, expressionS
*ep
,
1567 const char *lo_insn
, const char *lo_pattern
,
1568 bfd_reloc_code_real_type hi_reloc
,
1569 bfd_reloc_code_real_type lo_reloc
)
1572 ep2
.X_op
= O_symbol
;
1573 ep2
.X_add_symbol
= make_internal_label ();
1574 ep2
.X_add_number
= 0;
1576 macro_build (ep
, "auipc", "d,u", tempreg
, hi_reloc
);
1577 macro_build (&ep2
, lo_insn
, lo_pattern
, destreg
, tempreg
, lo_reloc
);
1581 pcrel_load (int destreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
1582 bfd_reloc_code_real_type hi_reloc
,
1583 bfd_reloc_code_real_type lo_reloc
)
1585 pcrel_access (destreg
, tempreg
, ep
, lo_insn
, "d,s,j", hi_reloc
, lo_reloc
);
1589 pcrel_store (int srcreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
1590 bfd_reloc_code_real_type hi_reloc
,
1591 bfd_reloc_code_real_type lo_reloc
)
1593 pcrel_access (srcreg
, tempreg
, ep
, lo_insn
, "t,s,q", hi_reloc
, lo_reloc
);
1596 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1599 riscv_call (int destreg
, int tempreg
, expressionS
*ep
,
1600 bfd_reloc_code_real_type reloc
)
1602 /* Ensure the jalr is emitted to the same frag as the auipc. */
1604 macro_build (ep
, "auipc", "d,u", tempreg
, reloc
);
1605 macro_build (NULL
, "jalr", "d,s", destreg
, tempreg
);
1606 /* See comment at end of append_insn. */
1607 frag_wane (frag_now
);
1611 /* Load an integer constant into a register. */
1614 load_const (int reg
, expressionS
*ep
)
1616 int shift
= RISCV_IMM_BITS
;
1617 bfd_vma upper_imm
, sign
= (bfd_vma
) 1 << (RISCV_IMM_BITS
- 1);
1618 expressionS upper
= *ep
, lower
= *ep
;
1619 lower
.X_add_number
= ((ep
->X_add_number
& (sign
+ sign
- 1)) ^ sign
) - sign
;
1620 upper
.X_add_number
-= lower
.X_add_number
;
1622 if (ep
->X_op
!= O_constant
)
1624 as_bad (_("unsupported large constant"));
1628 if (xlen
> 32 && !IS_SEXT_32BIT_NUM (ep
->X_add_number
))
1630 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1631 while (((upper
.X_add_number
>> shift
) & 1) == 0)
1634 upper
.X_add_number
= (int64_t) upper
.X_add_number
>> shift
;
1635 load_const (reg
, &upper
);
1637 md_assemblef ("slli x%d, x%d, 0x%x", reg
, reg
, shift
);
1638 if (lower
.X_add_number
!= 0)
1639 md_assemblef ("addi x%d, x%d, %" BFD_VMA_FMT
"d", reg
, reg
,
1640 lower
.X_add_number
);
1644 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1647 if (upper
.X_add_number
!= 0)
1649 /* Discard low part and zero-extend upper immediate. */
1650 upper_imm
= ((uint32_t)upper
.X_add_number
>> shift
);
1652 md_assemblef ("lui x%d, 0x%" BFD_VMA_FMT
"x", reg
, upper_imm
);
1656 if (lower
.X_add_number
!= 0 || hi_reg
== 0)
1657 md_assemblef ("%s x%d, x%d, %" BFD_VMA_FMT
"d", ADD32_INSN
, reg
, hi_reg
,
1658 lower
.X_add_number
);
1662 /* Zero extend and sign extend byte/half-word/word. */
1665 riscv_ext (int destreg
, int srcreg
, unsigned shift
, bool sign
)
1669 md_assemblef ("slli x%d, x%d, 0x%x", destreg
, srcreg
, shift
);
1670 md_assemblef ("srai x%d, x%d, 0x%x", destreg
, destreg
, shift
);
1674 md_assemblef ("slli x%d, x%d, 0x%x", destreg
, srcreg
, shift
);
1675 md_assemblef ("srli x%d, x%d, 0x%x", destreg
, destreg
, shift
);
1679 /* Expand RISC-V Vector macros into one or more instructions. */
1682 vector_macro (struct riscv_cl_insn
*ip
)
1684 int vd
= (ip
->insn_opcode
>> OP_SH_VD
) & OP_MASK_VD
;
1685 int vs1
= (ip
->insn_opcode
>> OP_SH_VS1
) & OP_MASK_VS1
;
1686 int vs2
= (ip
->insn_opcode
>> OP_SH_VS2
) & OP_MASK_VS2
;
1687 int vm
= (ip
->insn_opcode
>> OP_SH_VMASK
) & OP_MASK_VMASK
;
1688 int vtemp
= (ip
->insn_opcode
>> OP_SH_VFUNCT6
) & OP_MASK_VFUNCT6
;
1689 int mask
= ip
->insn_mo
->mask
;
1697 macro_build (NULL
, "vmslt.vx", "Vd,Vt,sVm", vd
, vs2
, vs1
, -1);
1698 macro_build (NULL
, "vmnand.mm", "Vd,Vt,Vs", vd
, vd
, vd
);
1703 /* Masked. Have vtemp to avoid overlap constraints. */
1706 macro_build (NULL
, "vmslt.vx", "Vd,Vt,s", vtemp
, vs2
, vs1
);
1707 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vm
, vtemp
);
1711 /* Preserve the value of vd if not updating by vm. */
1712 macro_build (NULL
, "vmslt.vx", "Vd,Vt,s", vtemp
, vs2
, vs1
);
1713 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vtemp
, vm
, vtemp
);
1714 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
1715 macro_build (NULL
, "vmor.mm", "Vd,Vt,Vs", vd
, vtemp
, vd
);
1720 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1721 macro_build (NULL
, "vmslt.vx", "Vd,Vt,sVm", vd
, vs2
, vs1
, vm
);
1722 macro_build (NULL
, "vmxor.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
1725 as_bad (_("must provide temp if destination overlaps mask"));
1732 macro_build (NULL
, "vmsltu.vx", "Vd,Vt,sVm", vd
, vs2
, vs1
, -1);
1733 macro_build (NULL
, "vmnand.mm", "Vd,Vt,Vs", vd
, vd
, vd
);
1738 /* Masked. Have vtemp to avoid overlap constraints. */
1741 macro_build (NULL
, "vmsltu.vx", "Vd,Vt,s", vtemp
, vs2
, vs1
);
1742 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vm
, vtemp
);
1746 /* Preserve the value of vd if not updating by vm. */
1747 macro_build (NULL
, "vmsltu.vx", "Vd,Vt,s", vtemp
, vs2
, vs1
);
1748 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vtemp
, vm
, vtemp
);
1749 macro_build (NULL
, "vmandnot.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
1750 macro_build (NULL
, "vmor.mm", "Vd,Vt,Vs", vd
, vtemp
, vd
);
1755 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1756 macro_build (NULL
, "vmsltu.vx", "Vd,Vt,sVm", vd
, vs2
, vs1
, vm
);
1757 macro_build (NULL
, "vmxor.mm", "Vd,Vt,Vs", vd
, vd
, vm
);
1760 as_bad (_("must provide temp if destination overlaps mask"));
1768 /* Expand RISC-V assembly macros into one or more instructions. */
1771 macro (struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
1772 bfd_reloc_code_real_type
*imm_reloc
)
1774 int rd
= (ip
->insn_opcode
>> OP_SH_RD
) & OP_MASK_RD
;
1775 int rs1
= (ip
->insn_opcode
>> OP_SH_RS1
) & OP_MASK_RS1
;
1776 int rs2
= (ip
->insn_opcode
>> OP_SH_RS2
) & OP_MASK_RS2
;
1777 int mask
= ip
->insn_mo
->mask
;
1782 load_const (rd
, imm_expr
);
1787 /* Load the address of a symbol into a register. */
1788 if (!IS_SEXT_32BIT_NUM (imm_expr
->X_add_number
))
1789 as_bad (_("offset too large"));
1791 if (imm_expr
->X_op
== O_constant
)
1792 load_const (rd
, imm_expr
);
1793 else if (riscv_opts
.pic
&& mask
== M_LA
) /* Global PIC symbol. */
1794 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
1795 BFD_RELOC_RISCV_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1796 else /* Local PIC symbol, or any non-PIC symbol. */
1797 pcrel_load (rd
, rd
, imm_expr
, "addi",
1798 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1802 pcrel_load (rd
, rd
, imm_expr
, "addi",
1803 BFD_RELOC_RISCV_TLS_GD_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1807 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
1808 BFD_RELOC_RISCV_TLS_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1812 pcrel_load (rd
, rd
, imm_expr
, "lb",
1813 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1817 pcrel_load (rd
, rd
, imm_expr
, "lbu",
1818 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1822 pcrel_load (rd
, rd
, imm_expr
, "lh",
1823 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1827 pcrel_load (rd
, rd
, imm_expr
, "lhu",
1828 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1832 pcrel_load (rd
, rd
, imm_expr
, "lw",
1833 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1837 pcrel_load (rd
, rd
, imm_expr
, "lwu",
1838 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1842 pcrel_load (rd
, rd
, imm_expr
, "ld",
1843 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1847 pcrel_load (rd
, rs1
, imm_expr
, "flw",
1848 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1852 pcrel_load (rd
, rs1
, imm_expr
, "fld",
1853 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1857 pcrel_store (rs2
, rs1
, imm_expr
, "sb",
1858 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1862 pcrel_store (rs2
, rs1
, imm_expr
, "sh",
1863 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1867 pcrel_store (rs2
, rs1
, imm_expr
, "sw",
1868 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1872 pcrel_store (rs2
, rs1
, imm_expr
, "sd",
1873 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1877 pcrel_store (rs2
, rs1
, imm_expr
, "fsw",
1878 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1882 pcrel_store (rs2
, rs1
, imm_expr
, "fsd",
1883 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1887 riscv_call (rd
, rs1
, imm_expr
, *imm_reloc
);
1891 riscv_ext (rd
, rs1
, xlen
- 16, false);
1895 riscv_ext (rd
, rs1
, xlen
- 32, false);
1899 riscv_ext (rd
, rs1
, xlen
- 8, true);
1903 riscv_ext (rd
, rs1
, xlen
- 16, true);
1912 pcrel_load (rd
, rs1
, imm_expr
, "flh",
1913 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
1916 pcrel_store (rs2
, rs1
, imm_expr
, "fsh",
1917 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1921 as_bad (_("internal: macro %s not implemented"), ip
->insn_mo
->name
);
1926 static const struct percent_op_match percent_op_utype
[] =
1928 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20
},
1929 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20
},
1930 {"%got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20
},
1931 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20
},
1932 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20
},
1933 {"%hi", BFD_RELOC_RISCV_HI20
},
1937 static const struct percent_op_match percent_op_itype
[] =
1939 {"%lo", BFD_RELOC_RISCV_LO12_I
},
1940 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I
},
1941 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I
},
1945 static const struct percent_op_match percent_op_stype
[] =
1947 {"%lo", BFD_RELOC_RISCV_LO12_S
},
1948 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S
},
1949 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S
},
1953 static const struct percent_op_match percent_op_rtype
[] =
1955 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD
},
1959 static const struct percent_op_match percent_op_null
[] =
1964 /* Return true if *STR points to a relocation operator. When returning true,
1965 move *STR over the operator and store its relocation code in *RELOC.
1966 Leave both *STR and *RELOC alone when returning false. */
1969 parse_relocation (char **str
, bfd_reloc_code_real_type
*reloc
,
1970 const struct percent_op_match
*percent_op
)
1972 for ( ; percent_op
->str
; percent_op
++)
1973 if (strncasecmp (*str
, percent_op
->str
, strlen (percent_op
->str
)) == 0)
1975 int len
= strlen (percent_op
->str
);
1977 if (!ISSPACE ((*str
)[len
]) && (*str
)[len
] != '(')
1980 *str
+= strlen (percent_op
->str
);
1981 *reloc
= percent_op
->reloc
;
1983 /* Check whether the output BFD supports this relocation.
1984 If not, issue an error and fall back on something safe. */
1985 if (*reloc
!= BFD_RELOC_UNUSED
1986 && !bfd_reloc_type_lookup (stdoutput
, *reloc
))
1988 as_bad ("internal: relocation %s isn't supported by the "
1989 "current ABI", percent_op
->str
);
1990 *reloc
= BFD_RELOC_UNUSED
;
1998 my_getExpression (expressionS
*ep
, char *str
)
2002 save_in
= input_line_pointer
;
2003 input_line_pointer
= str
;
2005 expr_end
= input_line_pointer
;
2006 input_line_pointer
= save_in
;
2009 /* Parse string STR as a 16-bit relocatable operand. Store the
2010 expression in *EP and the relocation, if any, in RELOC.
2011 Return the number of relocation operators used (0 or 1).
2013 On exit, EXPR_END points to the first character after the expression. */
2016 my_getSmallExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2017 char *str
, const struct percent_op_match
*percent_op
)
2020 unsigned crux_depth
, str_depth
, regno
;
2023 /* First, check for integer registers. No callers can accept a reg, but
2024 we need to avoid accidentally creating a useless undefined symbol below,
2025 if this is an instruction pattern that can't match. A glibc build fails
2026 if this is removed. */
2027 if (reg_lookup (&str
, RCLASS_GPR
, ®no
))
2029 ep
->X_op
= O_register
;
2030 ep
->X_add_number
= regno
;
2035 /* Search for the start of the main expression.
2037 End the loop with CRUX pointing to the start of the main expression and
2038 with CRUX_DEPTH containing the number of open brackets at that point. */
2045 crux_depth
= str_depth
;
2047 /* Skip over whitespace and brackets, keeping count of the number
2049 while (*str
== ' ' || *str
== '\t' || *str
== '(')
2055 && parse_relocation (&str
, reloc
, percent_op
));
2057 my_getExpression (ep
, crux
);
2060 /* Match every open bracket. */
2061 while (crux_depth
> 0 && (*str
== ')' || *str
== ' ' || *str
== '\t'))
2066 as_bad ("unclosed '('");
2073 /* Parse opcode name, could be an mnemonics or number. */
2076 my_getOpcodeExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
2077 char *str
, const struct percent_op_match
*percent_op
)
2079 const struct opcode_name_t
*o
= opcode_name_lookup (&str
);
2083 ep
->X_op
= O_constant
;
2084 ep
->X_add_number
= o
->val
;
2088 return my_getSmallExpression (ep
, reloc
, str
, percent_op
);
2091 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2092 On exit, EXPR_END points to the first character after the expression. */
2095 my_getVsetvliExpression (expressionS
*ep
, char *str
)
2097 unsigned int vsew_value
= 0, vlmul_value
= 0;
2098 unsigned int vta_value
= 0, vma_value
= 0;
2099 bfd_boolean vsew_found
= FALSE
, vlmul_found
= FALSE
;
2100 bfd_boolean vta_found
= FALSE
, vma_found
= FALSE
;
2102 if (arg_lookup (&str
, riscv_vsew
, ARRAY_SIZE (riscv_vsew
), &vsew_value
))
2107 as_bad (_("multiple vsew constants"));
2110 if (arg_lookup (&str
, riscv_vlmul
, ARRAY_SIZE (riscv_vlmul
), &vlmul_value
))
2115 as_bad (_("multiple vlmul constants"));
2118 if (arg_lookup (&str
, riscv_vta
, ARRAY_SIZE (riscv_vta
), &vta_value
))
2123 as_bad (_("multiple vta constants"));
2126 if (arg_lookup (&str
, riscv_vma
, ARRAY_SIZE (riscv_vma
), &vma_value
))
2131 as_bad (_("multiple vma constants"));
2135 if (vsew_found
|| vlmul_found
|| vta_found
|| vma_found
)
2137 ep
->X_op
= O_constant
;
2138 ep
->X_add_number
= (vlmul_value
<< OP_SH_VLMUL
)
2139 | (vsew_value
<< OP_SH_VSEW
)
2140 | (vta_value
<< OP_SH_VTA
)
2141 | (vma_value
<< OP_SH_VMA
);
2146 my_getExpression (ep
, str
);
2151 /* Detect and handle implicitly zero load-store offsets. For example,
2152 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2153 an implicit offset was detected. */
2156 riscv_handle_implicit_zero_offset (expressionS
*ep
, const char *s
)
2158 /* Check whether there is only a single bracketed expression left.
2159 If so, it must be the base register and the constant must be zero. */
2160 if (*s
== '(' && strchr (s
+ 1, '(') == 0)
2162 ep
->X_op
= O_constant
;
2163 ep
->X_add_number
= 0;
2170 /* All RISC-V CSR instructions belong to one of these classes. */
2179 /* Return which CSR instruction is checking. */
2181 static enum csr_insn_type
2182 riscv_csr_insn_type (insn_t insn
)
2184 if (((insn
^ MATCH_CSRRW
) & MASK_CSRRW
) == 0
2185 || ((insn
^ MATCH_CSRRWI
) & MASK_CSRRWI
) == 0)
2187 else if (((insn
^ MATCH_CSRRS
) & MASK_CSRRS
) == 0
2188 || ((insn
^ MATCH_CSRRSI
) & MASK_CSRRSI
) == 0)
2190 else if (((insn
^ MATCH_CSRRC
) & MASK_CSRRC
) == 0
2191 || ((insn
^ MATCH_CSRRCI
) & MASK_CSRRCI
) == 0)
2194 return INSN_NOT_CSR
;
2197 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2198 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2199 CSR address is 0x3. */
2202 riscv_csr_read_only_check (insn_t insn
)
2204 int csr
= (insn
& (OP_MASK_CSR
<< OP_SH_CSR
)) >> OP_SH_CSR
;
2205 int rs1
= (insn
& (OP_MASK_RS1
<< OP_SH_RS1
)) >> OP_SH_RS1
;
2206 int readonly
= (((csr
& (0x3 << 10)) >> 10) == 0x3);
2207 enum csr_insn_type csr_insn
= riscv_csr_insn_type (insn
);
2210 && (((csr_insn
== INSN_CSRRS
2211 || csr_insn
== INSN_CSRRC
)
2213 || csr_insn
== INSN_CSRRW
))
2219 /* Return true if it is a privileged instruction. Otherwise, return false.
2221 uret is actually a N-ext instruction. So it is better to regard it as
2222 an user instruction rather than the priv instruction.
2224 hret is used to return from traps in H-mode. H-mode is removed since
2225 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2226 Therefore, hret should be controlled by the hypervisor spec rather than
2227 priv spec in the future.
2229 dret is defined in the debug spec, so it should be checked in the future,
2233 riscv_is_priv_insn (insn_t insn
)
2235 return (((insn
^ MATCH_SRET
) & MASK_SRET
) == 0
2236 || ((insn
^ MATCH_MRET
) & MASK_MRET
) == 0
2237 || ((insn
^ MATCH_SFENCE_VMA
) & MASK_SFENCE_VMA
) == 0
2238 || ((insn
^ MATCH_WFI
) & MASK_WFI
) == 0
2239 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2240 check it here to keep the compatible. */
2241 || ((insn
^ MATCH_SFENCE_VM
) & MASK_SFENCE_VM
) == 0);
2244 /* This routine assembles an instruction into its binary format. As a
2245 side effect, it sets the global variable imm_reloc to the type of
2246 relocation to do if one of the operands is an address expression. */
2248 static struct riscv_ip_error
2249 riscv_ip (char *str
, struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
2250 bfd_reloc_code_real_type
*imm_reloc
, htab_t hash
)
2252 /* The operand string defined in the riscv_opcodes. */
2253 const char *oparg
, *opargStart
;
2254 /* The parsed operands from assembly. */
2255 char *asarg
, *asargStart
;
2257 struct riscv_opcode
*insn
;
2260 const struct percent_op_match
*p
;
2261 struct riscv_ip_error error
;
2262 error
.msg
= "unrecognized opcode";
2263 error
.statement
= str
;
2264 error
.missing_ext
= NULL
;
2265 /* Indicate we are assembling instruction with CSR. */
2266 bool insn_with_csr
= false;
2268 /* Parse the name of the instruction. Terminate the string if whitespace
2269 is found so that str_hash_find only sees the name part of the string. */
2270 for (asarg
= str
; *asarg
!= '\0'; ++asarg
)
2271 if (ISSPACE (*asarg
))
2278 insn
= (struct riscv_opcode
*) str_hash_find (hash
, str
);
2281 for ( ; insn
&& insn
->name
&& strcmp (insn
->name
, str
) == 0; insn
++)
2283 if ((insn
->xlen_requirement
!= 0) && (xlen
!= insn
->xlen_requirement
))
2286 if (!riscv_multi_subset_supports (&riscv_rps_as
, insn
->insn_class
))
2288 error
.missing_ext
= riscv_multi_subset_supports_ext (&riscv_rps_as
,
2293 /* Reset error message of the previous round. */
2294 error
.msg
= _("illegal operands");
2295 error
.missing_ext
= NULL
;
2296 create_insn (ip
, insn
);
2299 imm_expr
->X_op
= O_absent
;
2300 *imm_reloc
= BFD_RELOC_UNUSED
;
2301 p
= percent_op_itype
;
2303 for (oparg
= insn
->args
;; ++oparg
)
2306 asarg
+= strspn (asarg
, " \t");
2309 case '\0': /* End of args. */
2310 if (insn
->pinfo
!= INSN_MACRO
)
2312 if (!insn
->match_func (insn
, ip
->insn_opcode
))
2315 /* For .insn, insn->match and insn->mask are 0. */
2316 if (riscv_insn_length ((insn
->match
== 0 && insn
->mask
== 0)
2322 if (riscv_is_priv_insn (ip
->insn_opcode
))
2323 explicit_priv_attr
= true;
2325 /* Check if we write a read-only CSR by the CSR
2328 && riscv_opts
.csr_check
2329 && !riscv_csr_read_only_check (ip
->insn_opcode
))
2331 /* Restore the character in advance, since we want to
2332 report the detailed warning message here. */
2334 *(asargStart
- 1) = save_c
;
2335 as_warn (_("read-only CSR is written `%s'"), str
);
2336 insn_with_csr
= false;
2339 /* The (segmant) load and store with EEW 64 cannot be used
2340 when zve32x is enabled. */
2341 if (ip
->insn_mo
->pinfo
& INSN_V_EEW64
2342 && riscv_subset_supports (&riscv_rps_as
, "zve32x")
2343 && !riscv_subset_supports (&riscv_rps_as
, "zve64x"))
2345 error
.msg
= _("illegal opcode for zve32x");
2351 /* Successful assembly. */
2353 insn_with_csr
= false;
2359 case 's': /* RS1 x8-x15. */
2360 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2361 || !(regno
>= 8 && regno
<= 15))
2363 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2365 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2366 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2367 || EXTRACT_OPERAND (CRS1S
, ip
->insn_opcode
) + 8 != regno
)
2370 case 't': /* RS2 x8-x15. */
2371 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2372 || !(regno
>= 8 && regno
<= 15))
2374 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2376 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2377 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2378 || EXTRACT_OPERAND (CRS2S
, ip
->insn_opcode
) + 8 != regno
)
2381 case 'U': /* RS1, constrained to equal RD. */
2382 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2383 || EXTRACT_OPERAND (RD
, ip
->insn_opcode
) != regno
)
2387 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
2389 INSERT_OPERAND (CRS2
, *ip
, regno
);
2391 case 'c': /* RS1, constrained to equal sp. */
2392 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2396 case 'z': /* RS2, constrained to equal x0. */
2397 if (!reg_lookup (&asarg
, RCLASS_GPR
, ®no
)
2401 case '>': /* Shift amount, 0 - (XLEN-1). */
2402 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2403 || imm_expr
->X_op
!= O_constant
2404 || (unsigned long) imm_expr
->X_add_number
>= xlen
)
2406 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2409 imm_expr
->X_op
= O_absent
;
2412 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2413 || imm_expr
->X_op
!= O_constant
2414 || imm_expr
->X_add_number
< 0
2415 || imm_expr
->X_add_number
>= 32
2416 || !VALID_CLTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2418 ip
->insn_opcode
|= ENCODE_CLTYPE_IMM (imm_expr
->X_add_number
);
2421 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2422 || imm_expr
->X_op
!= O_constant
2423 || imm_expr
->X_add_number
< 0
2424 || imm_expr
->X_add_number
>= 64
2425 || !VALID_CSSTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2427 ip
->insn_opcode
|= ENCODE_CSSTYPE_IMM (imm_expr
->X_add_number
);
2430 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2431 || imm_expr
->X_op
!= O_constant
2432 || imm_expr
->X_add_number
< 0
2433 || imm_expr
->X_add_number
>= 256
2434 || !VALID_CIWTYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2436 ip
->insn_opcode
|= ENCODE_CIWTYPE_IMM (imm_expr
->X_add_number
);
2439 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2440 || imm_expr
->X_op
!= O_constant
2441 || imm_expr
->X_add_number
== 0
2442 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2444 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2447 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2449 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2450 || imm_expr
->X_op
!= O_constant
2451 || !VALID_CLTYPE_LW_IMM ((valueT
) imm_expr
->X_add_number
))
2453 ip
->insn_opcode
|= ENCODE_CLTYPE_LW_IMM (imm_expr
->X_add_number
);
2456 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2458 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2459 || imm_expr
->X_op
!= O_constant
2460 || !VALID_CLTYPE_LD_IMM ((valueT
) imm_expr
->X_add_number
))
2462 ip
->insn_opcode
|= ENCODE_CLTYPE_LD_IMM (imm_expr
->X_add_number
);
2465 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2467 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2468 || imm_expr
->X_op
!= O_constant
2469 || !VALID_CITYPE_LWSP_IMM ((valueT
) imm_expr
->X_add_number
))
2472 ENCODE_CITYPE_LWSP_IMM (imm_expr
->X_add_number
);
2475 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2477 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2478 || imm_expr
->X_op
!= O_constant
2479 || !VALID_CITYPE_LDSP_IMM ((valueT
) imm_expr
->X_add_number
))
2482 ENCODE_CITYPE_LDSP_IMM (imm_expr
->X_add_number
);
2485 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2486 || imm_expr
->X_op
!= O_constant
2487 /* C.addiw, c.li, and c.andi allow zero immediate.
2488 C.addi allows zero immediate as hint. Otherwise this
2490 || !VALID_CITYPE_IMM ((valueT
) imm_expr
->X_add_number
))
2492 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2495 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2496 || imm_expr
->X_op
!= O_constant
2497 || imm_expr
->X_add_number
== 0
2498 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT
) imm_expr
->X_add_number
))
2501 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr
->X_add_number
);
2504 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2505 || imm_expr
->X_op
!= O_constant
2506 || !VALID_CITYPE_ADDI16SP_IMM ((valueT
) imm_expr
->X_add_number
))
2509 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr
->X_add_number
);
2512 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2514 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2515 || imm_expr
->X_op
!= O_constant
2516 || !VALID_CSSTYPE_SWSP_IMM ((valueT
) imm_expr
->X_add_number
))
2519 ENCODE_CSSTYPE_SWSP_IMM (imm_expr
->X_add_number
);
2522 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
2524 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2525 || imm_expr
->X_op
!= O_constant
2526 || !VALID_CSSTYPE_SDSP_IMM ((valueT
) imm_expr
->X_add_number
))
2529 ENCODE_CSSTYPE_SDSP_IMM (imm_expr
->X_add_number
);
2532 p
= percent_op_utype
;
2533 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
2536 if (imm_expr
->X_op
!= O_constant
2537 || imm_expr
->X_add_number
<= 0
2538 || imm_expr
->X_add_number
>= RISCV_BIGIMM_REACH
2539 || (imm_expr
->X_add_number
>= RISCV_RVC_IMM_REACH
/ 2
2540 && (imm_expr
->X_add_number
<
2541 RISCV_BIGIMM_REACH
- RISCV_RVC_IMM_REACH
/ 2)))
2543 ip
->insn_opcode
|= ENCODE_CITYPE_IMM (imm_expr
->X_add_number
);
2546 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2547 || (imm_expr
->X_add_number
& (RISCV_IMM_REACH
- 1))
2548 || ((int32_t)imm_expr
->X_add_number
2549 != imm_expr
->X_add_number
))
2551 imm_expr
->X_add_number
=
2552 ((uint32_t) imm_expr
->X_add_number
) >> RISCV_IMM_BITS
;
2558 case 'S': /* Floating-point RS1 x8-x15. */
2559 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
2560 || !(regno
>= 8 && regno
<= 15))
2562 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
2564 case 'D': /* Floating-point RS2 x8-x15. */
2565 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
)
2566 || !(regno
>= 8 && regno
<= 15))
2568 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
2570 case 'T': /* Floating-point RS2. */
2571 if (!reg_lookup (&asarg
, RCLASS_FPR
, ®no
))
2573 INSERT_OPERAND (CRS2
, *ip
, regno
);
2579 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2580 || imm_expr
->X_op
!= O_constant
2581 || imm_expr
->X_add_number
< 0
2582 || imm_expr
->X_add_number
>= 64)
2584 as_bad (_("bad value for compressed funct6 "
2585 "field, value must be 0...64"));
2588 INSERT_OPERAND (CFUNCT6
, *ip
, imm_expr
->X_add_number
);
2589 imm_expr
->X_op
= O_absent
;
2594 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2595 || imm_expr
->X_op
!= O_constant
2596 || imm_expr
->X_add_number
< 0
2597 || imm_expr
->X_add_number
>= 16)
2599 as_bad (_("bad value for compressed funct4 "
2600 "field, value must be 0...15"));
2603 INSERT_OPERAND (CFUNCT4
, *ip
, imm_expr
->X_add_number
);
2604 imm_expr
->X_op
= O_absent
;
2609 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2610 || imm_expr
->X_op
!= O_constant
2611 || imm_expr
->X_add_number
< 0
2612 || imm_expr
->X_add_number
>= 8)
2614 as_bad (_("bad value for compressed funct3 "
2615 "field, value must be 0...7"));
2618 INSERT_OPERAND (CFUNCT3
, *ip
, imm_expr
->X_add_number
);
2619 imm_expr
->X_op
= O_absent
;
2624 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
2625 || imm_expr
->X_op
!= O_constant
2626 || imm_expr
->X_add_number
< 0
2627 || imm_expr
->X_add_number
>= 4)
2629 as_bad (_("bad value for compressed funct2 "
2630 "field, value must be 0...3"));
2633 INSERT_OPERAND (CFUNCT2
, *ip
, imm_expr
->X_add_number
);
2634 imm_expr
->X_op
= O_absent
;
2639 goto unknown_riscv_ip_operand
;
2644 goto unknown_riscv_ip_operand
;
2646 break; /* end RVC */
2652 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2654 INSERT_OPERAND (VD
, *ip
, regno
);
2657 case 'e': /* AMO VD */
2658 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
) && regno
== 0)
2659 INSERT_OPERAND (VWD
, *ip
, 0);
2660 else if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2662 INSERT_OPERAND (VWD
, *ip
, 1);
2663 INSERT_OPERAND (VD
, *ip
, regno
);
2669 case 'f': /* AMO VS3 */
2670 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2672 if (!EXTRACT_OPERAND (VWD
, ip
->insn_opcode
))
2673 INSERT_OPERAND (VD
, *ip
, regno
);
2676 /* VS3 must match VD. */
2677 if (EXTRACT_OPERAND (VD
, ip
->insn_opcode
) != regno
)
2683 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2685 INSERT_OPERAND (VS1
, *ip
, regno
);
2689 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2691 INSERT_OPERAND (VS2
, *ip
, regno
);
2694 case 'u': /* VS1 == VS2 */
2695 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2697 INSERT_OPERAND (VS1
, *ip
, regno
);
2698 INSERT_OPERAND (VS2
, *ip
, regno
);
2701 case 'v': /* VD == VS1 == VS2 */
2702 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
))
2704 INSERT_OPERAND (VD
, *ip
, regno
);
2705 INSERT_OPERAND (VS1
, *ip
, regno
);
2706 INSERT_OPERAND (VS2
, *ip
, regno
);
2709 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
2710 and is used to choose vs1/rs1/frs1/imm or vs2 for
2711 v[f]merge. It use the same encoding as the vector mask
2714 if (reg_lookup (&asarg
, RCLASS_VECR
, ®no
) && regno
== 0)
2718 case 'b': /* vtypei for vsetivli */
2719 my_getVsetvliExpression (imm_expr
, asarg
);
2720 check_absolute_expr (ip
, imm_expr
, FALSE
);
2721 if (!VALID_RVV_VB_IMM (imm_expr
->X_add_number
))
2722 as_bad (_("bad value for vsetivli immediate field, "
2723 "value must be 0..1023"));
2725 |= ENCODE_RVV_VB_IMM (imm_expr
->X_add_number
);
2726 imm_expr
->X_op
= O_absent
;
2730 case 'c': /* vtypei for vsetvli */
2731 my_getVsetvliExpression (imm_expr
, asarg
);
2732 check_absolute_expr (ip
, imm_expr
, FALSE
);
2733 if (!VALID_RVV_VC_IMM (imm_expr
->X_add_number
))
2734 as_bad (_("bad value for vsetvli immediate field, "
2735 "value must be 0..2047"));
2737 |= ENCODE_RVV_VC_IMM (imm_expr
->X_add_number
);
2738 imm_expr
->X_op
= O_absent
;
2742 case 'i': /* vector arith signed immediate */
2743 my_getExpression (imm_expr
, asarg
);
2744 check_absolute_expr (ip
, imm_expr
, FALSE
);
2745 if (imm_expr
->X_add_number
> 15
2746 || imm_expr
->X_add_number
< -16)
2747 as_bad (_("bad value for vector immediate field, "
2748 "value must be -16...15"));
2749 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
2750 imm_expr
->X_op
= O_absent
;
2754 case 'j': /* vector arith unsigned immediate */
2755 my_getExpression (imm_expr
, asarg
);
2756 check_absolute_expr (ip
, imm_expr
, FALSE
);
2757 if (imm_expr
->X_add_number
< 0
2758 || imm_expr
->X_add_number
>= 32)
2759 as_bad (_("bad value for vector immediate field, "
2760 "value must be 0...31"));
2761 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
);
2762 imm_expr
->X_op
= O_absent
;
2766 case 'k': /* vector arith signed immediate, minus 1 */
2767 my_getExpression (imm_expr
, asarg
);
2768 check_absolute_expr (ip
, imm_expr
, FALSE
);
2769 if (imm_expr
->X_add_number
> 16
2770 || imm_expr
->X_add_number
< -15)
2771 as_bad (_("bad value for vector immediate field, "
2772 "value must be -15...16"));
2773 INSERT_OPERAND (VIMM
, *ip
, imm_expr
->X_add_number
- 1);
2774 imm_expr
->X_op
= O_absent
;
2778 case 'm': /* optional vector mask */
2781 INSERT_OPERAND (VMASK
, *ip
, 1);
2784 else if (*asarg
== ',' && asarg
++
2785 && reg_lookup (&asarg
, RCLASS_VECM
, ®no
)
2788 INSERT_OPERAND (VMASK
, *ip
, 0);
2793 case 'M': /* required vector mask */
2794 if (reg_lookup (&asarg
, RCLASS_VECM
, ®no
) && regno
== 0)
2796 INSERT_OPERAND (VMASK
, *ip
, 0);
2801 case 'T': /* vector macro temporary register */
2802 if (!reg_lookup (&asarg
, RCLASS_VECR
, ®no
) || regno
== 0)
2804 /* Store it in the FUNCT6 field as we don't have anyplace
2805 else to store it. */
2806 INSERT_OPERAND (VFUNCT6
, *ip
, regno
);
2810 goto unknown_riscv_ip_operand
;
2812 break; /* end RVV */
2816 if (*asarg
++ == *oparg
)
2825 if (*asarg
++ == *oparg
)
2829 case '<': /* Shift amount, 0 - 31. */
2830 my_getExpression (imm_expr
, asarg
);
2831 check_absolute_expr (ip
, imm_expr
, false);
2832 if ((unsigned long) imm_expr
->X_add_number
> 31)
2833 as_bad (_("improper shift amount (%lu)"),
2834 (unsigned long) imm_expr
->X_add_number
);
2835 INSERT_OPERAND (SHAMTW
, *ip
, imm_expr
->X_add_number
);
2836 imm_expr
->X_op
= O_absent
;
2840 case '>': /* Shift amount, 0 - (XLEN-1). */
2841 my_getExpression (imm_expr
, asarg
);
2842 check_absolute_expr (ip
, imm_expr
, false);
2843 if ((unsigned long) imm_expr
->X_add_number
>= xlen
)
2844 as_bad (_("improper shift amount (%lu)"),
2845 (unsigned long) imm_expr
->X_add_number
);
2846 INSERT_OPERAND (SHAMT
, *ip
, imm_expr
->X_add_number
);
2847 imm_expr
->X_op
= O_absent
;
2851 case 'Z': /* CSRRxI immediate. */
2852 my_getExpression (imm_expr
, asarg
);
2853 check_absolute_expr (ip
, imm_expr
, false);
2854 if ((unsigned long) imm_expr
->X_add_number
> 31)
2855 as_bad (_("improper CSRxI immediate (%lu)"),
2856 (unsigned long) imm_expr
->X_add_number
);
2857 INSERT_OPERAND (RS1
, *ip
, imm_expr
->X_add_number
);
2858 imm_expr
->X_op
= O_absent
;
2862 case 'E': /* Control register. */
2863 insn_with_csr
= true;
2864 explicit_priv_attr
= true;
2865 if (reg_lookup (&asarg
, RCLASS_CSR
, ®no
))
2866 INSERT_OPERAND (CSR
, *ip
, regno
);
2869 my_getExpression (imm_expr
, asarg
);
2870 check_absolute_expr (ip
, imm_expr
, true);
2871 if ((unsigned long) imm_expr
->X_add_number
> 0xfff)
2872 as_bad (_("improper CSR address (%lu)"),
2873 (unsigned long) imm_expr
->X_add_number
);
2874 INSERT_OPERAND (CSR
, *ip
, imm_expr
->X_add_number
);
2875 imm_expr
->X_op
= O_absent
;
2880 case 'm': /* Rounding mode. */
2881 if (arg_lookup (&asarg
, riscv_rm
,
2882 ARRAY_SIZE (riscv_rm
), ®no
))
2884 INSERT_OPERAND (RM
, *ip
, regno
);
2890 case 'Q': /* Fence predecessor/successor. */
2891 if (arg_lookup (&asarg
, riscv_pred_succ
,
2892 ARRAY_SIZE (riscv_pred_succ
), ®no
))
2895 INSERT_OPERAND (PRED
, *ip
, regno
);
2897 INSERT_OPERAND (SUCC
, *ip
, regno
);
2902 case 'd': /* Destination register. */
2903 case 's': /* Source register. */
2904 case 't': /* Target register. */
2906 if (reg_lookup (&asarg
, RCLASS_GPR
, ®no
))
2912 /* Now that we have assembled one operand, we use the args
2913 string to figure out where it goes in the instruction. */
2917 INSERT_OPERAND (RS1
, *ip
, regno
);
2920 INSERT_OPERAND (RD
, *ip
, regno
);
2923 INSERT_OPERAND (RS2
, *ip
, regno
);
2926 INSERT_OPERAND (RS3
, *ip
, regno
);
2933 case 'D': /* Floating point RD. */
2934 case 'S': /* Floating point RS1. */
2935 case 'T': /* Floating point RS2. */
2936 case 'U': /* Floating point RS1 and RS2. */
2937 case 'R': /* Floating point RS3. */
2938 if (reg_lookup (&asarg
,
2939 (riscv_subset_supports (&riscv_rps_as
, "zfinx")
2940 ? RCLASS_GPR
: RCLASS_FPR
), ®no
))
2948 INSERT_OPERAND (RD
, *ip
, regno
);
2951 INSERT_OPERAND (RS1
, *ip
, regno
);
2954 INSERT_OPERAND (RS1
, *ip
, regno
);
2957 INSERT_OPERAND (RS2
, *ip
, regno
);
2960 INSERT_OPERAND (RS3
, *ip
, regno
);
2968 my_getExpression (imm_expr
, asarg
);
2969 if (imm_expr
->X_op
!= O_big
2970 && imm_expr
->X_op
!= O_constant
)
2972 normalize_constant_expr (imm_expr
);
2977 my_getExpression (imm_expr
, asarg
);
2978 normalize_constant_expr (imm_expr
);
2979 /* The 'A' format specifier must be a symbol. */
2980 if (imm_expr
->X_op
!= O_symbol
)
2982 *imm_reloc
= BFD_RELOC_32
;
2987 my_getExpression (imm_expr
, asarg
);
2988 normalize_constant_expr (imm_expr
);
2989 /* The 'B' format specifier must be a symbol or a constant. */
2990 if (imm_expr
->X_op
!= O_symbol
&& imm_expr
->X_op
!= O_constant
)
2992 if (imm_expr
->X_op
== O_symbol
)
2993 *imm_reloc
= BFD_RELOC_32
;
2997 case 'j': /* Sign-extended immediate. */
2998 p
= percent_op_itype
;
2999 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3001 case 'q': /* Store displacement. */
3002 p
= percent_op_stype
;
3003 *imm_reloc
= BFD_RELOC_RISCV_LO12_S
;
3005 case 'o': /* Load displacement. */
3006 p
= percent_op_itype
;
3007 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
3010 /* This is used for TLS, where the fourth operand is
3011 %tprel_add, to get a relocation applied to an add
3012 instruction, for relaxation to use. */
3013 p
= percent_op_rtype
;
3015 case '0': /* AMO displacement, which must be zero. */
3016 p
= percent_op_null
;
3018 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3021 /* If this value won't fit into a 16 bit offset, then go
3022 find a macro that will generate the 32 bit offset
3024 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3026 normalize_constant_expr (imm_expr
);
3027 if (imm_expr
->X_op
!= O_constant
3028 || (*oparg
== '0' && imm_expr
->X_add_number
!= 0)
3030 || imm_expr
->X_add_number
>= (signed)RISCV_IMM_REACH
/2
3031 || imm_expr
->X_add_number
< -(signed)RISCV_IMM_REACH
/2)
3037 case 'p': /* PC-relative offset. */
3039 *imm_reloc
= BFD_RELOC_12_PCREL
;
3040 my_getExpression (imm_expr
, asarg
);
3044 case 'u': /* Upper 20 bits. */
3045 p
= percent_op_utype
;
3046 if (!my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
))
3048 if (imm_expr
->X_op
!= O_constant
)
3051 if (imm_expr
->X_add_number
< 0
3052 || imm_expr
->X_add_number
>= (signed)RISCV_BIGIMM_REACH
)
3053 as_bad (_("lui expression not in range 0..1048575"));
3055 *imm_reloc
= BFD_RELOC_RISCV_HI20
;
3056 imm_expr
->X_add_number
<<= RISCV_IMM_BITS
;
3061 case 'a': /* 20-bit PC-relative offset. */
3063 my_getExpression (imm_expr
, asarg
);
3065 *imm_reloc
= BFD_RELOC_RISCV_JMP
;
3069 my_getExpression (imm_expr
, asarg
);
3071 if (strcmp (asarg
, "@plt") == 0)
3073 *imm_reloc
= BFD_RELOC_RISCV_CALL_PLT
;
3077 *imm_reloc
= BFD_RELOC_RISCV_CALL
;
3084 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
, p
)
3085 || imm_expr
->X_op
!= O_constant
3086 || imm_expr
->X_add_number
< 0
3087 || imm_expr
->X_add_number
>= 128
3088 || (imm_expr
->X_add_number
& 0x3) != 3)
3090 as_bad (_("bad value for opcode field, "
3091 "value must be 0...127 and "
3092 "lower 2 bits must be 0x3"));
3095 INSERT_OPERAND (OP
, *ip
, imm_expr
->X_add_number
);
3096 imm_expr
->X_op
= O_absent
;
3101 if (my_getOpcodeExpression (imm_expr
, imm_reloc
, asarg
, p
)
3102 || imm_expr
->X_op
!= O_constant
3103 || imm_expr
->X_add_number
< 0
3104 || imm_expr
->X_add_number
>= 3)
3106 as_bad (_("bad value for opcode field, "
3107 "value must be 0...2"));
3110 INSERT_OPERAND (OP2
, *ip
, imm_expr
->X_add_number
);
3111 imm_expr
->X_op
= O_absent
;
3116 goto unknown_riscv_ip_operand
;
3124 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3125 || imm_expr
->X_op
!= O_constant
3126 || imm_expr
->X_add_number
< 0
3127 || imm_expr
->X_add_number
>= 128)
3129 as_bad (_("bad value for funct7 field, "
3130 "value must be 0...127"));
3133 INSERT_OPERAND (FUNCT7
, *ip
, imm_expr
->X_add_number
);
3134 imm_expr
->X_op
= O_absent
;
3139 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3140 || imm_expr
->X_op
!= O_constant
3141 || imm_expr
->X_add_number
< 0
3142 || imm_expr
->X_add_number
>= 8)
3144 as_bad (_("bad value for funct3 field, "
3145 "value must be 0...7"));
3148 INSERT_OPERAND (FUNCT3
, *ip
, imm_expr
->X_add_number
);
3149 imm_expr
->X_op
= O_absent
;
3154 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3155 || imm_expr
->X_op
!= O_constant
3156 || imm_expr
->X_add_number
< 0
3157 || imm_expr
->X_add_number
>= 4)
3159 as_bad (_("bad value for funct2 field, "
3160 "value must be 0...3"));
3163 INSERT_OPERAND (FUNCT2
, *ip
, imm_expr
->X_add_number
);
3164 imm_expr
->X_op
= O_absent
;
3169 goto unknown_riscv_ip_operand
;
3173 case 'y': /* bs immediate */
3174 my_getExpression (imm_expr
, asarg
);
3175 check_absolute_expr (ip
, imm_expr
, FALSE
);
3176 if ((unsigned long)imm_expr
->X_add_number
> 3)
3177 as_bad(_("Improper bs immediate (%lu)"),
3178 (unsigned long)imm_expr
->X_add_number
);
3179 INSERT_OPERAND(BS
, *ip
, imm_expr
->X_add_number
);
3180 imm_expr
->X_op
= O_absent
;
3184 case 'Y': /* rnum immediate */
3185 my_getExpression (imm_expr
, asarg
);
3186 check_absolute_expr (ip
, imm_expr
, FALSE
);
3187 if ((unsigned long)imm_expr
->X_add_number
> 10)
3188 as_bad(_("Improper rnum immediate (%lu)"),
3189 (unsigned long)imm_expr
->X_add_number
);
3190 INSERT_OPERAND(RNUM
, *ip
, imm_expr
->X_add_number
);
3191 imm_expr
->X_op
= O_absent
;
3196 if (my_getSmallExpression (imm_expr
, imm_reloc
, asarg
, p
)
3197 || imm_expr
->X_op
!= O_constant
3198 || imm_expr
->X_add_number
!= 0)
3201 imm_expr
->X_op
= O_absent
;
3204 case 'f': /* Prefetch offset, pseudo S-type but lower 5-bits zero. */
3205 if (riscv_handle_implicit_zero_offset (imm_expr
, asarg
))
3207 my_getExpression (imm_expr
, asarg
);
3208 check_absolute_expr (ip
, imm_expr
, false);
3209 if (((unsigned) (imm_expr
->X_add_number
) & 0x1fU
)
3210 || imm_expr
->X_add_number
>= (signed) RISCV_IMM_REACH
/ 2
3211 || imm_expr
->X_add_number
< -(signed) RISCV_IMM_REACH
/ 2)
3212 as_bad (_("improper prefetch offset (%ld)"),
3213 (long) imm_expr
->X_add_number
);
3215 ENCODE_STYPE_IMM ((unsigned) (imm_expr
->X_add_number
) &
3217 imm_expr
->X_op
= O_absent
;
3222 unknown_riscv_ip_operand
:
3223 as_fatal (_("internal: unknown argument type `%s'"),
3229 insn_with_csr
= false;
3233 /* Restore the character we might have clobbered above. */
3235 *(asargStart
- 1) = save_c
;
3240 /* Similar to riscv_ip, but assembles an instruction according to the
3241 hardcode values of .insn directive. */
3244 riscv_ip_hardcode (char *str
,
3245 struct riscv_cl_insn
*ip
,
3246 expressionS
*imm_expr
,
3249 struct riscv_opcode
*insn
;
3250 insn_t values
[2] = {0, 0};
3251 unsigned int num
= 0;
3253 input_line_pointer
= str
;
3256 expression (imm_expr
);
3257 if (imm_expr
->X_op
!= O_constant
)
3259 /* The first value isn't constant, so it should be
3260 .insn <type> <operands>. We have been parsed it
3264 return _("values must be constant");
3266 values
[num
++] = (insn_t
) imm_expr
->X_add_number
;
3268 while (*input_line_pointer
++ == ',' && num
< 2);
3270 input_line_pointer
--;
3271 if (*input_line_pointer
!= '\0')
3272 return _("unrecognized values");
3274 insn
= XNEW (struct riscv_opcode
);
3275 insn
->match
= values
[num
- 1];
3276 create_insn (ip
, insn
);
3277 unsigned int bytes
= riscv_insn_length (insn
->match
);
3278 if ((bytes
< sizeof(values
[0]) && values
[num
- 1] >> (8 * bytes
) != 0)
3279 || (num
== 2 && values
[0] != bytes
))
3280 return _("value conflicts with instruction length");
3286 md_assemble (char *str
)
3288 struct riscv_cl_insn insn
;
3289 expressionS imm_expr
;
3290 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
3292 /* The architecture and privileged elf attributes should be set
3293 before assembling. */
3294 if (!start_assemble
)
3296 start_assemble
= true;
3298 riscv_set_abi_by_arch ();
3299 if (!riscv_set_default_priv_spec (NULL
))
3303 riscv_mapping_state (MAP_INSN
, 0);
3305 const struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
3306 &imm_reloc
, op_hash
);
3310 if (error
.missing_ext
)
3311 as_bad ("%s `%s', extension `%s' required", error
.msg
,
3312 error
.statement
, error
.missing_ext
);
3314 as_bad ("%s `%s'", error
.msg
, error
.statement
);
3318 if (insn
.insn_mo
->pinfo
== INSN_MACRO
)
3319 macro (&insn
, &imm_expr
, &imm_reloc
);
3321 append_insn (&insn
, &imm_expr
, imm_reloc
);
3325 md_atof (int type
, char *litP
, int *sizeP
)
3327 return ieee_md_atof (type
, litP
, sizeP
, TARGET_BYTES_BIG_ENDIAN
);
3331 md_number_to_chars (char *buf
, valueT val
, int n
)
3333 if (target_big_endian
)
3334 number_to_chars_bigendian (buf
, val
, n
);
3336 number_to_chars_littleendian (buf
, val
, n
);
3339 const char *md_shortopts
= "O::g::G:";
3343 OPTION_MARCH
= OPTION_MD_BASE
,
3350 OPTION_NO_ARCH_ATTR
,
3352 OPTION_NO_CSR_CHECK
,
3356 OPTION_LITTLE_ENDIAN
,
3360 struct option md_longopts
[] =
3362 {"march", required_argument
, NULL
, OPTION_MARCH
},
3363 {"fPIC", no_argument
, NULL
, OPTION_PIC
},
3364 {"fpic", no_argument
, NULL
, OPTION_PIC
},
3365 {"fno-pic", no_argument
, NULL
, OPTION_NO_PIC
},
3366 {"mabi", required_argument
, NULL
, OPTION_MABI
},
3367 {"mrelax", no_argument
, NULL
, OPTION_RELAX
},
3368 {"mno-relax", no_argument
, NULL
, OPTION_NO_RELAX
},
3369 {"march-attr", no_argument
, NULL
, OPTION_ARCH_ATTR
},
3370 {"mno-arch-attr", no_argument
, NULL
, OPTION_NO_ARCH_ATTR
},
3371 {"mcsr-check", no_argument
, NULL
, OPTION_CSR_CHECK
},
3372 {"mno-csr-check", no_argument
, NULL
, OPTION_NO_CSR_CHECK
},
3373 {"misa-spec", required_argument
, NULL
, OPTION_MISA_SPEC
},
3374 {"mpriv-spec", required_argument
, NULL
, OPTION_MPRIV_SPEC
},
3375 {"mbig-endian", no_argument
, NULL
, OPTION_BIG_ENDIAN
},
3376 {"mlittle-endian", no_argument
, NULL
, OPTION_LITTLE_ENDIAN
},
3378 {NULL
, no_argument
, NULL
, 0}
3380 size_t md_longopts_size
= sizeof (md_longopts
);
3383 md_parse_option (int c
, const char *arg
)
3388 default_arch_with_ext
= arg
;
3392 riscv_opts
.pic
= false;
3396 riscv_opts
.pic
= true;
3400 if (strcmp (arg
, "ilp32") == 0)
3401 riscv_set_abi (32, FLOAT_ABI_SOFT
, false);
3402 else if (strcmp (arg
, "ilp32e") == 0)
3403 riscv_set_abi (32, FLOAT_ABI_SOFT
, true);
3404 else if (strcmp (arg
, "ilp32f") == 0)
3405 riscv_set_abi (32, FLOAT_ABI_SINGLE
, false);
3406 else if (strcmp (arg
, "ilp32d") == 0)
3407 riscv_set_abi (32, FLOAT_ABI_DOUBLE
, false);
3408 else if (strcmp (arg
, "ilp32q") == 0)
3409 riscv_set_abi (32, FLOAT_ABI_QUAD
, false);
3410 else if (strcmp (arg
, "lp64") == 0)
3411 riscv_set_abi (64, FLOAT_ABI_SOFT
, false);
3412 else if (strcmp (arg
, "lp64f") == 0)
3413 riscv_set_abi (64, FLOAT_ABI_SINGLE
, false);
3414 else if (strcmp (arg
, "lp64d") == 0)
3415 riscv_set_abi (64, FLOAT_ABI_DOUBLE
, false);
3416 else if (strcmp (arg
, "lp64q") == 0)
3417 riscv_set_abi (64, FLOAT_ABI_QUAD
, false);
3420 explicit_mabi
= true;
3424 riscv_opts
.relax
= true;
3427 case OPTION_NO_RELAX
:
3428 riscv_opts
.relax
= false;
3431 case OPTION_ARCH_ATTR
:
3432 riscv_opts
.arch_attr
= true;
3435 case OPTION_NO_ARCH_ATTR
:
3436 riscv_opts
.arch_attr
= false;
3439 case OPTION_CSR_CHECK
:
3440 riscv_opts
.csr_check
= true;
3443 case OPTION_NO_CSR_CHECK
:
3444 riscv_opts
.csr_check
= false;
3447 case OPTION_MISA_SPEC
:
3448 return riscv_set_default_isa_spec (arg
);
3450 case OPTION_MPRIV_SPEC
:
3451 return riscv_set_default_priv_spec (arg
);
3453 case OPTION_BIG_ENDIAN
:
3454 target_big_endian
= 1;
3457 case OPTION_LITTLE_ENDIAN
:
3458 target_big_endian
= 0;
3469 riscv_after_parse_args (void)
3471 /* The --with-arch is optional for now, so we still need to set the xlen
3472 according to the default_arch, which is set by the --target. */
3475 if (strcmp (default_arch
, "riscv32") == 0)
3477 else if (strcmp (default_arch
, "riscv64") == 0)
3480 as_bad ("unknown default architecture `%s'", default_arch
);
3483 /* Set default specs. */
3484 if (default_isa_spec
== ISA_SPEC_CLASS_NONE
)
3485 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC
);
3486 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3487 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC
);
3489 riscv_set_arch (default_arch_with_ext
);
3491 /* If the CIE to be produced has not been overridden on the command line,
3492 then produce version 3 by default. This allows us to use the full
3493 range of registers in a .cfi_return_column directive. */
3494 if (flag_dwarf_cie_version
== -1)
3495 flag_dwarf_cie_version
= 3;
3499 md_pcrel_from (fixS
*fixP
)
3501 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
3504 /* Apply a fixup to the object file. */
3507 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
3509 unsigned int subtype
;
3510 bfd_byte
*buf
= (bfd_byte
*) (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
);
3511 bool relaxable
= false;
3515 /* Remember value for tc_gen_reloc. */
3516 fixP
->fx_addnumber
= *valP
;
3518 switch (fixP
->fx_r_type
)
3520 case BFD_RELOC_RISCV_HI20
:
3521 case BFD_RELOC_RISCV_LO12_I
:
3522 case BFD_RELOC_RISCV_LO12_S
:
3523 bfd_putl32 (riscv_apply_const_reloc (fixP
->fx_r_type
, *valP
)
3524 | bfd_getl32 (buf
), buf
);
3525 if (fixP
->fx_addsy
== NULL
)
3526 fixP
->fx_done
= true;
3530 case BFD_RELOC_RISCV_GOT_HI20
:
3531 case BFD_RELOC_RISCV_ADD8
:
3532 case BFD_RELOC_RISCV_ADD16
:
3533 case BFD_RELOC_RISCV_ADD32
:
3534 case BFD_RELOC_RISCV_ADD64
:
3535 case BFD_RELOC_RISCV_SUB6
:
3536 case BFD_RELOC_RISCV_SUB8
:
3537 case BFD_RELOC_RISCV_SUB16
:
3538 case BFD_RELOC_RISCV_SUB32
:
3539 case BFD_RELOC_RISCV_SUB64
:
3540 case BFD_RELOC_RISCV_RELAX
:
3543 case BFD_RELOC_RISCV_TPREL_HI20
:
3544 case BFD_RELOC_RISCV_TPREL_LO12_I
:
3545 case BFD_RELOC_RISCV_TPREL_LO12_S
:
3546 case BFD_RELOC_RISCV_TPREL_ADD
:
3550 case BFD_RELOC_RISCV_TLS_GOT_HI20
:
3551 case BFD_RELOC_RISCV_TLS_GD_HI20
:
3552 case BFD_RELOC_RISCV_TLS_DTPREL32
:
3553 case BFD_RELOC_RISCV_TLS_DTPREL64
:
3554 if (fixP
->fx_addsy
!= NULL
)
3555 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
3557 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
3558 _("TLS relocation against a constant"));
3562 /* Use pc-relative relocation for FDE initial location.
3563 The symbol address in .eh_frame may be adjusted in
3564 _bfd_elf_discard_section_eh_frame, and the content of
3565 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
3566 Therefore, we cannot insert a relocation whose addend symbol is
3567 in .eh_frame. Othrewise, the value may be adjusted twice. */
3568 if (fixP
->fx_addsy
&& fixP
->fx_subsy
3569 && (sub_segment
= S_GET_SEGMENT (fixP
->fx_subsy
))
3570 && strcmp (sub_segment
->name
, ".eh_frame") == 0
3571 && S_GET_VALUE (fixP
->fx_subsy
)
3572 == fixP
->fx_frag
->fr_address
+ fixP
->fx_where
)
3574 fixP
->fx_r_type
= BFD_RELOC_RISCV_32_PCREL
;
3575 fixP
->fx_subsy
= NULL
;
3582 case BFD_RELOC_RISCV_CFA
:
3583 if (fixP
->fx_addsy
&& fixP
->fx_subsy
)
3585 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
3586 fixP
->fx_next
->fx_addsy
= fixP
->fx_subsy
;
3587 fixP
->fx_next
->fx_subsy
= NULL
;
3588 fixP
->fx_next
->fx_offset
= 0;
3589 fixP
->fx_subsy
= NULL
;
3591 switch (fixP
->fx_r_type
)
3594 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD64
;
3595 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB64
;
3599 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD32
;
3600 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
3604 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD16
;
3605 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
3609 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD8
;
3610 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
3613 case BFD_RELOC_RISCV_CFA
:
3614 /* Load the byte to get the subtype. */
3615 subtype
= bfd_get_8 (NULL
, &((fragS
*) (fixP
->fx_frag
->fr_opcode
))->fr_literal
[fixP
->fx_where
]);
3616 loc
= fixP
->fx_frag
->fr_fix
- (subtype
& 7);
3619 case DW_CFA_advance_loc1
:
3620 fixP
->fx_where
= loc
+ 1;
3621 fixP
->fx_next
->fx_where
= loc
+ 1;
3622 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET8
;
3623 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
3626 case DW_CFA_advance_loc2
:
3628 fixP
->fx_next
->fx_size
= 2;
3629 fixP
->fx_where
= loc
+ 1;
3630 fixP
->fx_next
->fx_where
= loc
+ 1;
3631 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET16
;
3632 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
3635 case DW_CFA_advance_loc4
:
3637 fixP
->fx_next
->fx_size
= 4;
3638 fixP
->fx_where
= loc
;
3639 fixP
->fx_next
->fx_where
= loc
;
3640 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET32
;
3641 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
3645 if (subtype
< 0x80 && (subtype
& 0x40))
3647 /* DW_CFA_advance_loc */
3648 fixP
->fx_frag
= (fragS
*) fixP
->fx_frag
->fr_opcode
;
3649 fixP
->fx_next
->fx_frag
= fixP
->fx_frag
;
3650 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET6
;
3651 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB6
;
3654 as_fatal (_("internal: bad CFA value #%d"), subtype
);
3660 /* This case is unreachable. */
3667 /* If we are deleting this reloc entry, we must fill in the
3668 value now. This can happen if we have a .word which is not
3669 resolved when it appears but is later defined. */
3670 if (fixP
->fx_addsy
== NULL
)
3672 gas_assert (fixP
->fx_size
<= sizeof (valueT
));
3673 md_number_to_chars ((char *) buf
, *valP
, fixP
->fx_size
);
3678 case BFD_RELOC_RISCV_JMP
:
3681 /* Fill in a tentative value to improve objdump readability. */
3682 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
3683 bfd_vma delta
= target
- md_pcrel_from (fixP
);
3684 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_JTYPE_IMM (delta
), buf
);
3688 case BFD_RELOC_12_PCREL
:
3691 /* Fill in a tentative value to improve objdump readability. */
3692 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
3693 bfd_vma delta
= target
- md_pcrel_from (fixP
);
3694 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_BTYPE_IMM (delta
), buf
);
3698 case BFD_RELOC_RISCV_RVC_BRANCH
:
3701 /* Fill in a tentative value to improve objdump readability. */
3702 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
3703 bfd_vma delta
= target
- md_pcrel_from (fixP
);
3704 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CBTYPE_IMM (delta
), buf
);
3708 case BFD_RELOC_RISCV_RVC_JUMP
:
3711 /* Fill in a tentative value to improve objdump readability. */
3712 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
3713 bfd_vma delta
= target
- md_pcrel_from (fixP
);
3714 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_CJTYPE_IMM (delta
), buf
);
3718 case BFD_RELOC_RISCV_CALL
:
3719 case BFD_RELOC_RISCV_CALL_PLT
:
3723 case BFD_RELOC_RISCV_PCREL_HI20
:
3724 case BFD_RELOC_RISCV_PCREL_LO12_S
:
3725 case BFD_RELOC_RISCV_PCREL_LO12_I
:
3726 relaxable
= riscv_opts
.relax
;
3729 case BFD_RELOC_RISCV_ALIGN
:
3733 /* We ignore generic BFD relocations we don't know about. */
3734 if (bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
) != NULL
)
3735 as_fatal (_("internal: bad relocation #%d"), fixP
->fx_r_type
);
3738 if (fixP
->fx_subsy
!= NULL
)
3739 as_bad_subtract (fixP
);
3741 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
3742 if (relaxable
&& fixP
->fx_tcbit
&& fixP
->fx_addsy
!= NULL
)
3744 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
3745 fixP
->fx_next
->fx_addsy
= fixP
->fx_next
->fx_subsy
= NULL
;
3746 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_RELAX
;
3747 fixP
->fx_next
->fx_size
= 0;
3751 /* Because the value of .cfi_remember_state may changed after relaxation,
3752 we insert a fix to relocate it again in link-time. */
3755 riscv_pre_output_hook (void)
3757 const frchainS
*frch
;
3760 /* Save the current segment info. */
3762 subsegT subseg
= now_subseg
;
3764 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
3765 for (frch
= seg_info (s
)->frchainP
; frch
; frch
= frch
->frch_next
)
3769 for (frag
= frch
->frch_root
; frag
; frag
= frag
->fr_next
)
3771 if (frag
->fr_type
== rs_cfa
)
3774 expressionS
*symval
;
3776 symval
= symbol_get_value_expression (frag
->fr_symbol
);
3777 exp
.X_op
= O_subtract
;
3778 exp
.X_add_symbol
= symval
->X_add_symbol
;
3779 exp
.X_add_number
= 0;
3780 exp
.X_op_symbol
= symval
->X_op_symbol
;
3782 /* We must set the segment before creating a frag after all
3783 frag chains have been chained together. */
3784 subseg_set (s
, frch
->frch_subseg
);
3786 fix_new_exp (frag
, (int) frag
->fr_offset
, 1, &exp
, 0,
3787 BFD_RELOC_RISCV_CFA
);
3792 /* Restore the original segment info. */
3793 subseg_set (seg
, subseg
);
3796 /* Handle the .option pseudo-op. */
3799 s_riscv_option (int x ATTRIBUTE_UNUSED
)
3801 char *name
= input_line_pointer
, ch
;
3803 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
3804 ++input_line_pointer
;
3805 ch
= *input_line_pointer
;
3806 *input_line_pointer
= '\0';
3808 if (strcmp (name
, "rvc") == 0)
3810 riscv_update_subset (&riscv_rps_as
, "+c");
3811 riscv_set_rvc (true);
3813 else if (strcmp (name
, "norvc") == 0)
3815 riscv_update_subset (&riscv_rps_as
, "-c");
3816 riscv_set_rvc (false);
3818 else if (strcmp (name
, "pic") == 0)
3819 riscv_opts
.pic
= true;
3820 else if (strcmp (name
, "nopic") == 0)
3821 riscv_opts
.pic
= false;
3822 else if (strcmp (name
, "relax") == 0)
3823 riscv_opts
.relax
= true;
3824 else if (strcmp (name
, "norelax") == 0)
3825 riscv_opts
.relax
= false;
3826 else if (strcmp (name
, "csr-check") == 0)
3827 riscv_opts
.csr_check
= true;
3828 else if (strcmp (name
, "no-csr-check") == 0)
3829 riscv_opts
.csr_check
= false;
3830 else if (strncmp (name
, "arch,", 5) == 0)
3833 if (ISSPACE (*name
) && *name
!= '\0')
3835 riscv_update_subset (&riscv_rps_as
, name
);
3837 riscv_set_rvc (false);
3838 if (riscv_subset_supports (&riscv_rps_as
, "c"))
3839 riscv_set_rvc (true);
3841 else if (strcmp (name
, "push") == 0)
3843 struct riscv_option_stack
*s
;
3845 s
= XNEW (struct riscv_option_stack
);
3846 s
->next
= riscv_opts_stack
;
3847 s
->options
= riscv_opts
;
3848 s
->subset_list
= riscv_subsets
;
3849 riscv_opts_stack
= s
;
3850 riscv_subsets
= riscv_copy_subset_list (s
->subset_list
);
3851 riscv_rps_as
.subset_list
= riscv_subsets
;
3853 else if (strcmp (name
, "pop") == 0)
3855 struct riscv_option_stack
*s
;
3857 s
= riscv_opts_stack
;
3859 as_bad (_(".option pop with no .option push"));
3862 riscv_subset_list_t
*release_subsets
= riscv_subsets
;
3863 riscv_opts_stack
= s
->next
;
3864 riscv_opts
= s
->options
;
3865 riscv_subsets
= s
->subset_list
;
3866 riscv_rps_as
.subset_list
= riscv_subsets
;
3867 riscv_release_subset_list (release_subsets
);
3873 as_warn (_("unrecognized .option directive: %s\n"), name
);
3875 *input_line_pointer
= ch
;
3876 demand_empty_rest_of_line ();
3879 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
3880 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
3881 use in DWARF debug information. */
3884 s_dtprel (int bytes
)
3891 if (ex
.X_op
!= O_symbol
)
3893 as_bad (_("unsupported use of %s"), (bytes
== 8
3896 ignore_rest_of_line ();
3899 p
= frag_more (bytes
);
3900 md_number_to_chars (p
, 0, bytes
);
3901 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, bytes
, &ex
, false,
3903 ? BFD_RELOC_RISCV_TLS_DTPREL64
3904 : BFD_RELOC_RISCV_TLS_DTPREL32
));
3906 demand_empty_rest_of_line ();
3909 /* Handle the .bss pseudo-op. */
3912 s_bss (int ignore ATTRIBUTE_UNUSED
)
3914 subseg_set (bss_section
, 0);
3915 demand_empty_rest_of_line ();
3919 riscv_make_nops (char *buf
, bfd_vma bytes
)
3923 /* RISC-V instructions cannot begin or end on odd addresses, so this case
3924 means we are not within a valid instruction sequence. It is thus safe
3925 to use a zero byte, even though that is not a valid instruction. */
3929 /* Use at most one 2-byte NOP. */
3930 if ((bytes
- i
) % 4 == 2)
3932 number_to_chars_littleendian (buf
+ i
, RVC_NOP
, 2);
3936 /* Fill the remainder with 4-byte NOPs. */
3937 for ( ; i
< bytes
; i
+= 4)
3938 number_to_chars_littleendian (buf
+ i
, RISCV_NOP
, 4);
3941 /* Called from md_do_align. Used to create an alignment frag in a
3942 code section by emitting a worst-case NOP sequence that the linker
3943 will later relax to the correct number of NOPs. We can't compute
3944 the correct alignment now because of other linker relaxations. */
3947 riscv_frag_align_code (int n
)
3949 bfd_vma bytes
= (bfd_vma
) 1 << n
;
3950 bfd_vma insn_alignment
= riscv_opts
.rvc
? 2 : 4;
3951 bfd_vma worst_case_bytes
= bytes
- insn_alignment
;
3955 /* If we are moving to a smaller alignment than the instruction size, then no
3956 alignment is required. */
3957 if (bytes
<= insn_alignment
)
3960 /* When not relaxing, riscv_handle_align handles code alignment. */
3961 if (!riscv_opts
.relax
)
3964 nops
= frag_more (worst_case_bytes
);
3966 ex
.X_op
= O_constant
;
3967 ex
.X_add_number
= worst_case_bytes
;
3969 riscv_make_nops (nops
, worst_case_bytes
);
3971 fix_new_exp (frag_now
, nops
- frag_now
->fr_literal
, 0,
3972 &ex
, false, BFD_RELOC_RISCV_ALIGN
);
3974 riscv_mapping_state (MAP_INSN
, worst_case_bytes
);
3976 /* We need to start a new frag after the alignment which may be removed by
3977 the linker, to prevent the assembler from computing static offsets.
3978 This is necessary to get correct EH info. */
3979 frag_wane (frag_now
);
3985 /* Implement HANDLE_ALIGN. */
3988 riscv_handle_align (fragS
*fragP
)
3990 switch (fragP
->fr_type
)
3993 /* When relaxing, riscv_frag_align_code handles code alignment. */
3994 if (!riscv_opts
.relax
)
3996 bfd_signed_vma bytes
= (fragP
->fr_next
->fr_address
3997 - fragP
->fr_address
- fragP
->fr_fix
);
3998 /* We have 4 byte uncompressed nops. */
3999 bfd_signed_vma size
= 4;
4000 bfd_signed_vma excess
= bytes
% size
;
4001 bfd_boolean odd_padding
= (excess
% 2 == 1);
4002 char *p
= fragP
->fr_literal
+ fragP
->fr_fix
;
4007 /* Insert zeros or compressed nops to get 4 byte alignment. */
4011 riscv_add_odd_padding_symbol (fragP
);
4012 riscv_make_nops (p
, excess
);
4013 fragP
->fr_fix
+= excess
;
4017 /* The frag will be changed to `rs_fill` later. The function
4018 `write_contents` will try to fill the remaining spaces
4019 according to the patterns we give. In this case, we give
4020 a 4 byte uncompressed nop as the pattern, and set the size
4021 of the pattern into `fr_var`. The nop will be output to the
4022 file `fr_offset` times. However, `fr_offset` could be zero
4023 if we don't need to pad the boundary finally. */
4024 riscv_make_nops (p
, size
);
4025 fragP
->fr_var
= size
;
4034 /* This usually called from frag_var. */
4037 riscv_init_frag (fragS
* fragP
, int max_chars
)
4039 /* Do not add mapping symbol to debug sections. */
4040 if (bfd_section_flags (now_seg
) & SEC_DEBUGGING
)
4043 switch (fragP
->fr_type
)
4048 riscv_mapping_state (MAP_DATA
, max_chars
);
4051 riscv_mapping_state (MAP_INSN
, max_chars
);
4059 md_estimate_size_before_relax (fragS
*fragp
, asection
*segtype
)
4061 return (fragp
->fr_var
= relaxed_branch_length (fragp
, segtype
, false));
4064 /* Translate internal representation of relocation info to BFD target
4068 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
4070 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
4072 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
4073 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
4074 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
4075 reloc
->addend
= fixp
->fx_addnumber
;
4077 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
4078 if (reloc
->howto
== NULL
)
4080 if ((fixp
->fx_r_type
== BFD_RELOC_16
|| fixp
->fx_r_type
== BFD_RELOC_8
)
4081 && fixp
->fx_addsy
!= NULL
&& fixp
->fx_subsy
!= NULL
)
4083 /* We don't have R_RISCV_8/16, but for this special case,
4084 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
4088 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
4089 _("cannot represent %s relocation in object file"),
4090 bfd_get_reloc_code_name (fixp
->fx_r_type
));
4098 riscv_relax_frag (asection
*sec
, fragS
*fragp
, long stretch ATTRIBUTE_UNUSED
)
4100 if (RELAX_BRANCH_P (fragp
->fr_subtype
))
4102 offsetT old_var
= fragp
->fr_var
;
4103 fragp
->fr_var
= relaxed_branch_length (fragp
, sec
, true);
4104 return fragp
->fr_var
- old_var
;
4110 /* Expand far branches to multi-instruction sequences. */
4113 md_convert_frag_branch (fragS
*fragp
)
4121 buf
= (bfd_byte
*)fragp
->fr_literal
+ fragp
->fr_fix
;
4123 exp
.X_op
= O_symbol
;
4124 exp
.X_add_symbol
= fragp
->fr_symbol
;
4125 exp
.X_add_number
= fragp
->fr_offset
;
4127 gas_assert (fragp
->fr_var
== RELAX_BRANCH_LENGTH (fragp
->fr_subtype
));
4129 if (RELAX_BRANCH_RVC (fragp
->fr_subtype
))
4131 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
4135 /* Expand the RVC branch into a RISC-V one. */
4136 insn
= bfd_getl16 (buf
);
4137 rs1
= 8 + ((insn
>> OP_SH_CRS1S
) & OP_MASK_CRS1S
);
4138 if ((insn
& MASK_C_J
) == MATCH_C_J
)
4140 else if ((insn
& MASK_C_JAL
) == MATCH_C_JAL
)
4141 insn
= MATCH_JAL
| (X_RA
<< OP_SH_RD
);
4142 else if ((insn
& MASK_C_BEQZ
) == MATCH_C_BEQZ
)
4143 insn
= MATCH_BEQ
| (rs1
<< OP_SH_RS1
);
4144 else if ((insn
& MASK_C_BNEZ
) == MATCH_C_BNEZ
)
4145 insn
= MATCH_BNE
| (rs1
<< OP_SH_RS1
);
4148 bfd_putl32 (insn
, buf
);
4152 /* Invert the branch condition. Branch over the jump. */
4153 insn
= bfd_getl16 (buf
);
4154 insn
^= MATCH_C_BEQZ
^ MATCH_C_BNEZ
;
4155 insn
|= ENCODE_CBTYPE_IMM (6);
4156 bfd_putl16 (insn
, buf
);
4161 /* Just keep the RVC branch. */
4162 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
4163 ? BFD_RELOC_RISCV_RVC_JUMP
: BFD_RELOC_RISCV_RVC_BRANCH
;
4164 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4165 2, &exp
, false, reloc
);
4174 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
4177 gas_assert (!RELAX_BRANCH_UNCOND (fragp
->fr_subtype
));
4179 /* Invert the branch condition. Branch over the jump. */
4180 insn
= bfd_getl32 (buf
);
4181 insn
^= MATCH_BEQ
^ MATCH_BNE
;
4182 insn
|= ENCODE_BTYPE_IMM (8);
4183 bfd_putl32 (insn
, buf
);
4187 /* Jump to the target. */
4188 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4189 4, &exp
, false, BFD_RELOC_RISCV_JMP
);
4190 bfd_putl32 (MATCH_JAL
, buf
);
4195 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
4196 ? BFD_RELOC_RISCV_JMP
: BFD_RELOC_12_PCREL
;
4197 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
4198 4, &exp
, false, reloc
);
4207 fixp
->fx_file
= fragp
->fr_file
;
4208 fixp
->fx_line
= fragp
->fr_line
;
4210 gas_assert (buf
== (bfd_byte
*)fragp
->fr_literal
4211 + fragp
->fr_fix
+ fragp
->fr_var
);
4213 fragp
->fr_fix
+= fragp
->fr_var
;
4216 /* Relax a machine dependent frag. This returns the amount by which
4217 the current size of the frag should change. */
4220 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT asec ATTRIBUTE_UNUSED
,
4223 gas_assert (RELAX_BRANCH_P (fragp
->fr_subtype
));
4224 md_convert_frag_branch (fragp
);
4228 md_show_usage (FILE *stream
)
4230 fprintf (stream
, _("\
4232 -fpic or -fPIC generate position-independent code\n\
4233 -fno-pic don't generate position-independent code (default)\n\
4234 -march=ISA set the RISC-V architecture\n\
4235 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
4236 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9.1, 1.10, 1.11, 1.12)\n\
4237 -mabi=ABI set the RISC-V ABI\n\
4238 -mrelax enable relax (default)\n\
4239 -mno-relax disable relax\n\
4240 -march-attr generate RISC-V arch attribute\n\
4241 -mno-arch-attr don't generate RISC-V arch attribute\n\
4242 -mcsr-check enable the csr ISA and privilege spec version checks\n\
4243 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
4244 -mbig-endian assemble for big-endian\n\
4245 -mlittle-endian assemble for little-endian\n\
4249 /* Standard calling conventions leave the CFA at SP on entry. */
4252 riscv_cfi_frame_initial_instructions (void)
4254 cfi_add_CFA_def_cfa_register (X_SP
);
4258 tc_riscv_regname_to_dw2regnum (char *regname
)
4262 if ((reg
= reg_lookup_internal (regname
, RCLASS_GPR
)) >= 0)
4265 if ((reg
= reg_lookup_internal (regname
, RCLASS_FPR
)) >= 0)
4268 /* CSRs are numbered 4096 -> 8191. */
4269 if ((reg
= reg_lookup_internal (regname
, RCLASS_CSR
)) >= 0)
4272 as_bad (_("unknown register `%s'"), regname
);
4277 riscv_elf_final_processing (void)
4279 riscv_set_abi_by_arch ();
4280 elf_elfheader (stdoutput
)->e_flags
|= elf_flags
;
4283 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
4284 since these directives break relaxation when used with symbol deltas. */
4287 s_riscv_leb128 (int sign
)
4290 char *save_in
= input_line_pointer
;
4293 if (exp
.X_op
!= O_constant
)
4294 as_bad (_("non-constant .%cleb128 is not supported"), sign
? 's' : 'u');
4295 demand_empty_rest_of_line ();
4297 input_line_pointer
= save_in
;
4298 return s_leb128 (sign
);
4301 /* Parse the .insn directive. There are three formats,
4302 Format 1: .insn <type> <operand1>, <operand2>, ...
4303 Format 2: .insn <length>, <value>
4304 Format 3: .insn <value>. */
4307 s_riscv_insn (int x ATTRIBUTE_UNUSED
)
4309 char *str
= input_line_pointer
;
4310 struct riscv_cl_insn insn
;
4311 expressionS imm_expr
;
4312 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
4315 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
4316 ++input_line_pointer
;
4318 save_c
= *input_line_pointer
;
4319 *input_line_pointer
= '\0';
4321 riscv_mapping_state (MAP_INSN
, 0);
4323 struct riscv_ip_error error
= riscv_ip (str
, &insn
, &imm_expr
,
4324 &imm_reloc
, insn_type_hash
);
4327 char *save_in
= input_line_pointer
;
4328 error
.msg
= riscv_ip_hardcode (str
, &insn
, &imm_expr
, error
.msg
);
4329 input_line_pointer
= save_in
;
4334 if (error
.missing_ext
)
4335 as_bad ("%s `%s', extension `%s' required", error
.msg
, error
.statement
,
4338 as_bad ("%s `%s'", error
.msg
, error
.statement
);
4342 gas_assert (insn
.insn_mo
->pinfo
!= INSN_MACRO
);
4343 append_insn (&insn
, &imm_expr
, imm_reloc
);
4346 *input_line_pointer
= save_c
;
4347 demand_empty_rest_of_line ();
4350 /* Update architecture and privileged elf attributes. If we don't set
4351 them, then try to output the default ones. */
4354 riscv_write_out_attrs (void)
4356 const char *arch_str
, *priv_str
, *p
;
4357 /* versions[0]: major version.
4358 versions[1]: minor version.
4359 versions[2]: revision version. */
4360 unsigned versions
[3] = {0}, number
= 0;
4363 /* Re-write architecture elf attribute. */
4364 arch_str
= riscv_arch_str (xlen
, riscv_subsets
);
4365 bfd_elf_add_proc_attr_string (stdoutput
, Tag_RISCV_arch
, arch_str
);
4366 xfree ((void *) arch_str
);
4368 /* For the file without any instruction, we don't set the default_priv_spec
4369 according to the privileged elf attributes since the md_assemble isn't
4372 && !riscv_set_default_priv_spec (NULL
))
4375 /* If we already have set privileged elf attributes, then no need to do
4376 anything. Otherwise, don't generate or update them when no CSR and
4377 privileged instructions are used. */
4378 if (!explicit_priv_attr
)
4381 RISCV_GET_PRIV_SPEC_NAME (priv_str
, default_priv_spec
);
4383 for (i
= 0; *p
; ++p
)
4385 if (*p
== '.' && i
< 3)
4387 versions
[i
++] = number
;
4390 else if (ISDIGIT (*p
))
4391 number
= (number
* 10) + (*p
- '0');
4394 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str
);
4398 versions
[i
] = number
;
4400 /* Re-write privileged elf attributes. */
4401 bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec
, versions
[0]);
4402 bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_minor
, versions
[1]);
4403 bfd_elf_add_proc_attr_int (stdoutput
, Tag_RISCV_priv_spec_revision
, versions
[2]);
4406 /* Add the default contents for the .riscv.attributes section. */
4409 riscv_set_public_attributes (void)
4411 if (riscv_opts
.arch_attr
|| explicit_attr
)
4412 riscv_write_out_attrs ();
4415 /* Called after all assembly has been done. */
4420 riscv_set_public_attributes ();
4423 /* Adjust the symbol table. */
4426 riscv_adjust_symtab (void)
4428 bfd_map_over_sections (stdoutput
, riscv_check_mapping_symbols
, (char *) 0);
4429 elf_adjust_symtab ();
4432 /* Given a symbolic attribute NAME, return the proper integer value.
4433 Returns -1 if the attribute is not known. */
4436 riscv_convert_symbolic_attribute (const char *name
)
4445 /* When you modify this table you should
4446 also modify the list in doc/c-riscv.texi. */
4447 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
4451 T(priv_spec_revision
),
4452 T(unaligned_access
),
4461 for (i
= 0; i
< ARRAY_SIZE (attribute_table
); i
++)
4462 if (strcmp (name
, attribute_table
[i
].name
) == 0)
4463 return attribute_table
[i
].tag
;
4468 /* Parse a .attribute directive. */
4471 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED
)
4473 int tag
= obj_elf_vendor_attribute (OBJ_ATTR_PROC
);
4475 obj_attribute
*attr
;
4477 explicit_attr
= true;
4480 case Tag_RISCV_arch
:
4482 attr
= elf_known_obj_attributes_proc (stdoutput
);
4483 if (!start_assemble
)
4484 riscv_set_arch (attr
[Tag_RISCV_arch
].s
);
4486 as_fatal (_("architecture elf attributes must set before "
4487 "any instructions"));
4489 if (old_xlen
!= xlen
)
4491 /* We must re-init bfd again if xlen is changed. */
4492 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
4493 bfd_find_target (riscv_target_format (), stdoutput
);
4495 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
4496 as_warn (_("could not set architecture and machine"));
4500 case Tag_RISCV_priv_spec
:
4501 case Tag_RISCV_priv_spec_minor
:
4502 case Tag_RISCV_priv_spec_revision
:
4504 as_fatal (_("privileged elf attributes must set before "
4505 "any instructions"));
4513 /* Mark symbol that it follows a variant CC convention. */
4516 s_variant_cc (int ignored ATTRIBUTE_UNUSED
)
4522 elf_symbol_type
*elfsym
;
4524 c
= get_symbol_name (&name
);
4526 as_bad (_("missing symbol name for .variant_cc directive"));
4527 sym
= symbol_find_or_make (name
);
4528 restore_line_pointer (c
);
4529 demand_empty_rest_of_line ();
4531 bfdsym
= symbol_get_bfdsym (sym
);
4532 elfsym
= elf_symbol_from (bfdsym
);
4533 gas_assert (elfsym
);
4534 elfsym
->internal_elf_sym
.st_other
|= STO_RISCV_VARIANT_CC
;
4537 /* Same as elf_copy_symbol_attributes, but without copying st_other.
4538 This is needed so RISC-V specific st_other values can be independently
4539 specified for an IFUNC resolver (that is called by the dynamic linker)
4540 and the symbol it resolves (aliased to the resolver). In particular,
4541 if a function symbol has special st_other value set via directives,
4542 then attaching an IFUNC resolver to that symbol should not override
4543 the st_other setting. Requiring the directive on the IFUNC resolver
4544 symbol would be unexpected and problematic in C code, where the two
4545 symbols appear as two independent function declarations. */
4548 riscv_elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
4550 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
4551 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
4552 /* If size is unset, copy size from src. Because we don't track whether
4553 .size has been used, we can't differentiate .size dest, 0 from the case
4554 where dest's size is unset. */
4555 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
4559 destelf
->size
= XNEW (expressionS
);
4560 *destelf
->size
= *srcelf
->size
;
4562 S_SET_SIZE (dest
, S_GET_SIZE (src
));
4566 /* RISC-V pseudo-ops table. */
4567 static const pseudo_typeS riscv_pseudo_table
[] =
4569 {"option", s_riscv_option
, 0},
4573 {"dtprelword", s_dtprel
, 4},
4574 {"dtpreldword", s_dtprel
, 8},
4576 {"uleb128", s_riscv_leb128
, 0},
4577 {"sleb128", s_riscv_leb128
, 1},
4578 {"insn", s_riscv_insn
, 0},
4579 {"attribute", s_riscv_attribute
, 0},
4580 {"variant_cc", s_variant_cc
, 0},
4581 {"float16", float_cons
, 'h'},
4587 riscv_pop_insert (void)
4589 extern void pop_insert (const pseudo_typeS
*);
4591 pop_insert (riscv_pseudo_table
);