gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gas / config / tc-riscv.c
blob1b730b4be36afc08729cdcf8ec9cb12719e42f40
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
7 This file is part of GAS.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 #include "as.h"
24 #include "config.h"
25 #include "subsegs.h"
26 #include "safe-ctype.h"
28 #include "itbl-ops.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
32 #include "bfd/elfxx-riscv.h"
33 #include "elf/riscv.h"
34 #include "opcode/riscv.h"
36 #include <stdint.h>
38 /* Information about an instruction, including its format, operands
39 and fixups. */
40 struct riscv_cl_insn
42 /* The opcode's entry in riscv_opcodes. */
43 const struct riscv_opcode *insn_mo;
45 /* The encoded instruction bits. */
46 insn_t insn_opcode;
48 /* The frag that contains the instruction. */
49 struct frag *frag;
51 /* The offset into FRAG of the first instruction byte. */
52 long where;
54 /* The relocs associated with the instruction, if any. */
55 fixS *fixp;
58 /* All RISC-V CSR belong to one of these classes. */
59 enum riscv_csr_class
61 CSR_CLASS_NONE,
63 CSR_CLASS_I,
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. */
79 unsigned address;
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
93 riscv_ip function */
94 struct riscv_ip_error
96 /* General error message */
97 const char* msg;
99 /* Statement that caused the error */
100 char* statement;
102 /* Missing extension that needs to be enabled */
103 const char* missing_ext;
106 #ifndef DEFAULT_ARCH
107 #define DEFAULT_ARCH "riscv64"
108 #endif
110 #ifndef DEFAULT_RISCV_ATTR
111 #define DEFAULT_RISCV_ATTR 0
112 #endif
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
117 #endif
119 /* Need to sync the version with RISC-V compiler. */
120 #ifndef DEFAULT_RISCV_ISA_SPEC
121 #define DEFAULT_RISCV_ISA_SPEC "20191213"
122 #endif
124 #ifndef DEFAULT_RISCV_PRIV_SPEC
125 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
126 #endif
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;
136 enum float_abi
138 FLOAT_ABI_DEFAULT = -1,
139 FLOAT_ABI_SOFT,
140 FLOAT_ABI_SINGLE,
141 FLOAT_ABI_DOUBLE,
142 FLOAT_ABI_QUAD
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. */
154 static int
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);
163 return 0;
165 else
166 default_isa_spec = class;
167 return 1;
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. */
174 static int
175 riscv_set_default_priv_spec (const char *s)
177 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
178 unsigned major, minor, revision;
179 obj_attribute *attr;
181 RISCV_GET_PRIV_SPEC_CLASS (s, class);
182 if (class != PRIV_SPEC_CLASS_NONE)
184 default_priv_spec = class;
185 return 1;
188 if (s != NULL)
190 as_bad (_("unknown default privileged spec `%s' set by "
191 "-mpriv-spec or --with-priv-spec"), s);
192 return 0;
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)
202 return 1;
204 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
205 if (class != PRIV_SPEC_CLASS_NONE)
207 default_priv_spec = class;
208 return 1;
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);
214 return 0;
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 =
229 0, /* pic */
230 0, /* rvc */
231 1, /* relax */
232 DEFAULT_RISCV_ATTR, /* arch_attr */
233 0, /* csr_check */
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. */
239 static void
240 riscv_set_rvc (bool rvc_value)
242 if (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
251 configure option. */
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. */
258 &xlen, /* xlen. */
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. */
275 static void
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"));
282 return;
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. */
305 static void
306 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
308 abi_xlen = new_xlen;
309 float_abi = new_float_abi;
310 rve_abi = rve;
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. */
316 static void
317 riscv_set_abi_by_arch (void)
319 if (!explicit_mabi)
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);
327 else
328 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
330 else
332 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
333 if (abi_xlen > xlen)
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 "
344 "isn't supported");
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 "
348 "isn't supported");
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 "
352 "isn't supported");
355 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
356 elf_flags &= ~EF_RISCV_FLOAT_ABI;
357 elf_flags |= float_abi << 1;
359 if (rve_abi)
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) \
407 ((relax_substateT) \
408 (0xc0000000 \
409 | ((uncond) ? 1 : 0) \
410 | ((rvc) ? 2 : 0) \
411 | ((length) << 2)))
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. */
438 static void
439 make_mapping_symbol (enum riscv_seg_mstate state,
440 valueT value,
441 fragS *frag)
443 const char *name;
444 switch (state)
446 case MAP_DATA:
447 name = "$d";
448 break;
449 case MAP_INSN:
450 name = "$x";
451 break;
452 default:
453 abort ();
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. */
462 if (value == 0)
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. */
490 void
491 riscv_mapping_state (enum riscv_seg_mstate to_state,
492 int max_chars)
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))
503 return;
505 /* The mapping symbol should be emitted if not in the right
506 mapping state */
507 if (from_state == to_state)
508 return;
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. */
517 static void
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
529 alignment. */
531 static void
532 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
533 asection *sec,
534 void *dummy ATTRIBUTE_UNUSED)
536 segment_info_type *seginfo = seg_info (sec);
537 fragS *fragp;
539 if (seginfo == NULL || seginfo->frchainP == NULL)
540 return;
542 for (fragp = seginfo->frchainP->frch_root;
543 fragp != NULL;
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)
550 continue;
552 /* Check the last mapping symbol if it is at the boundary of
553 fragment. */
554 if (S_GET_VALUE (last) < next->fr_address)
555 continue;
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);
565 break;
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);
573 break;
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)
579 break;
581 next = next->fr_next;
583 while (next != NULL);
587 /* The default target format to use. */
589 const char *
590 riscv_target_format (void)
592 if (target_big_endian)
593 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
594 else
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. */
608 static void
609 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
611 insn->insn_mo = mo;
612 insn->insn_opcode = mo->match;
613 insn->frag = NULL;
614 insn->where = 0;
615 insn->fixp = NULL;
618 /* Install INSN at the location specified by its "frag" and "where" fields. */
620 static void
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. */
630 static void
631 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
633 insn->frag = frag;
634 insn->where = where;
635 if (insn->fixp != NULL)
637 insn->fixp->fx_frag = frag;
638 insn->fixp->fx_where = where;
640 install_insn (insn);
643 /* Add INSN to the end of the output. */
645 static void
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);
652 static void
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. */
665 static unsigned
666 relaxed_branch_length (fragS *fragp, asection *sec, int update)
668 int jump, rvc, length = 8;
670 if (!fragp)
671 return length;
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)
690 length = 2;
691 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
692 length = 4;
693 else if (!jump && rvc)
694 length = 6;
697 if (update)
698 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
700 return length;
703 /* Information about an opcode name, mnemonics and its value. */
704 struct opcode_name_t
706 const char *name;
707 unsigned int val;
710 /* List for all supported opcode name. */
711 static const struct opcode_name_t opcode_name_list[] =
713 {"C0", 0x0},
714 {"C1", 0x1},
715 {"C2", 0x2},
717 {"LOAD", 0x03},
718 {"LOAD_FP", 0x07},
719 {"CUSTOM_0", 0x0b},
720 {"MISC_MEM", 0x0f},
721 {"OP_IMM", 0x13},
722 {"AUIPC", 0x17},
723 {"OP_IMM_32", 0x1b},
724 /* 48b 0x1f. */
726 {"STORE", 0x23},
727 {"STORE_FP", 0x27},
728 {"CUSTOM_1", 0x2b},
729 {"AMO", 0x2f},
730 {"OP", 0x33},
731 {"LUI", 0x37},
732 {"OP_32", 0x3b},
733 /* 64b 0x3f. */
735 {"MADD", 0x43},
736 {"MSUB", 0x47},
737 {"NMADD", 0x4f},
738 {"NMSUB", 0x4b},
739 {"OP_FP", 0x53},
740 /*reserved 0x57. */
741 {"CUSTOM_2", 0x5b},
742 /* 48b 0x5f. */
744 {"BRANCH", 0x63},
745 {"JALR", 0x67},
746 /*reserved 0x5b. */
747 {"JAL", 0x6f},
748 {"SYSTEM", 0x73},
749 /*reserved 0x77. */
750 {"CUSTOM_3", 0x7b},
751 /* >80b 0x7f. */
753 {NULL, 0}
756 /* Hash table for lookup opcode name. */
757 static htab_t opcode_names_hash = NULL;
759 /* Initialization for hash table of opcode name. */
761 static void
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
772 if found. */
774 static const struct opcode_name_t *
775 opcode_name_lookup (char **s)
777 char *e;
778 char save_c;
779 struct opcode_name_t *o;
781 /* Find end of name. */
782 e = *s;
783 if (is_name_beginner (*e))
784 ++e;
785 while (is_part_of_name (*e))
786 ++e;
788 /* Terminate name. */
789 save_c = *e;
790 *e = '\0';
792 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
794 /* Advance to next token if one was recognized. */
795 if (o)
796 *s = e;
798 *e = save_c;
799 expr_end = e;
801 return o;
804 /* All RISC-V registers belong to one of these classes. */
805 enum reg_class
807 RCLASS_GPR,
808 RCLASS_FPR,
809 RCLASS_VECR,
810 RCLASS_VECM,
811 RCLASS_MAX,
813 RCLASS_CSR
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)
824 static void
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);
832 static void
833 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
835 unsigned i;
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. */
843 static void
844 riscv_init_csr_hash (const char *name,
845 unsigned address,
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;
853 pre_entry = NULL;
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)
861 need_enrty = false;
862 pre_entry = entry;
863 entry = entry->next;
866 /* Duplicate CSR. */
867 if (!need_enrty)
868 return;
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;
875 entry->next = NULL;
877 if (pre_entry == NULL)
878 str_hash_insert (csr_extra_hash, name, entry, 0);
879 else
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. */
893 static unsigned int
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;
903 switch (csr_class)
905 case CSR_CLASS_I_32:
906 rv32_only = (xlen == 32);
907 /* Fall through. */
908 case CSR_CLASS_I:
909 need_check_version = true;
910 extension = "i";
911 break;
912 case CSR_CLASS_F:
913 extension = "f";
914 break;
915 case CSR_CLASS_ZKR:
916 extension = "zkr";
917 break;
918 case CSR_CLASS_V:
919 extension = "v";
920 break;
921 case CSR_CLASS_DEBUG:
922 break;
923 default:
924 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
927 if (riscv_opts.csr_check)
929 if (!rv32_only)
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;
947 entry = entry->next;
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
965 the address. */
967 static unsigned int
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);
973 if (r == NULL)
974 return -1U;
976 return riscv_csr_address (s, r);
979 static unsigned int
980 reg_lookup_internal (const char *s, enum reg_class class)
982 void *r;
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)
989 return -1;
991 if (riscv_subset_supports (&riscv_rps_as, "e")
992 && class == RCLASS_GPR
993 && DECODE_REG_NUM (r) > 15)
994 return -1;
996 return DECODE_REG_NUM (r);
999 static bool
1000 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1002 char *e;
1003 char save_c;
1004 int reg = -1;
1006 /* Find end of name. */
1007 e = *s;
1008 if (is_name_beginner (*e))
1009 ++e;
1010 while (is_part_of_name (*e))
1011 ++e;
1013 /* Terminate name. */
1014 save_c = *e;
1015 *e = '\0';
1017 /* Look for the register. Advance to next token if one was recognized. */
1018 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1019 *s = e;
1021 *e = save_c;
1022 if (regnop)
1023 *regnop = reg;
1024 return reg >= 0;
1027 static bool
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);
1033 if (len == 0)
1034 return false;
1036 for (i = 0; i < size; i++)
1037 if (array[i] != NULL && strncmp (array[i], *s, len) == 0)
1039 *regnop = i;
1040 *s += len;
1041 return true;
1044 return false;
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. */
1053 static bool
1054 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1056 const char *oparg, *opargStart;
1057 insn_t used_bits = opc->mask;
1058 int insn_width;
1059 insn_t required_bits;
1061 if (length == 0)
1062 insn_width = 8 * riscv_insn_length (opc->match);
1063 else
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);
1072 return false;
1075 for (oparg = opc->args; *oparg; ++oparg)
1077 opargStart = oparg;
1078 switch (*oparg)
1080 case 'C': /* RVC */
1081 switch (*++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. */
1114 switch (*++oparg)
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;
1120 default:
1121 goto unknown_validate_operand;
1123 break;
1124 default:
1125 goto unknown_validate_operand;
1127 break; /* end RVC */
1128 case 'V': /* RVV */
1129 switch (*++oparg)
1131 case 'd':
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;
1141 case '0': break;
1142 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1143 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1144 case 'i':
1145 case 'j':
1146 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1147 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1148 default:
1149 goto unknown_validate_operand;
1151 break; /* end RVV */
1152 case ',': break;
1153 case '(': break;
1154 case ')': break;
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);
1169 /* Fall through. */
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. */
1191 switch (*++oparg)
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;
1196 default:
1197 goto unknown_validate_operand;
1199 break;
1200 case 'O': /* Opcode for .insn directive. */
1201 switch (*++oparg)
1203 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1204 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1205 default:
1206 goto unknown_validate_operand;
1208 break;
1209 default:
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);
1214 return false;
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);
1224 return false;
1226 return true;
1229 #undef USE_BITS
1231 struct percent_op_match
1233 const char *str;
1234 bfd_reloc_code_real_type reloc;
1237 /* Common hash table initialization function for instruction and .insn
1238 directive. */
1240 static htab_t
1241 init_opcode_hash (const struct riscv_opcode *opcodes,
1242 bool insn_directive_p)
1244 int i = 0;
1245 int length;
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);
1259 else
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"));
1265 else
1266 gas_assert (!insn_directive_p);
1267 ++i;
1269 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1272 return hash;
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. */
1278 void
1279 md_begin (void)
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"
1306 #undef DECLARE_CSR
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);
1315 static insn_t
1316 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1318 switch (reloc_type)
1320 case BFD_RELOC_32:
1321 return 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);
1332 default:
1333 abort ();
1337 /* Output an instruction. IP is the instruction information.
1338 ADDRESS_EXPR is an operand of the instruction to be used with
1339 RELOC_TYPE. */
1341 static void
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"));
1362 return;
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);
1369 return;
1371 else
1373 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1374 if (howto == NULL)
1375 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1376 reloc_type);
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);
1387 install_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);
1399 frag_new (0);
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. */
1408 static void
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;
1414 va_list args;
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);
1421 gas_assert (mo);
1423 /* Find a non-RVC variant of the instruction. append_insn will compress
1424 it if possible. */
1425 while (riscv_insn_length (mo->match) < 4)
1426 mo++;
1427 gas_assert (strcmp (name, mo->name) == 0);
1429 create_insn (&insn, mo);
1430 for (;; ++fmt)
1432 fmtStart = fmt;
1433 switch (*fmt)
1435 case 'V': /* RVV */
1436 switch (*++fmt)
1438 case 'd':
1439 INSERT_OPERAND (VD, insn, va_arg (args, int));
1440 continue;
1441 case 's':
1442 INSERT_OPERAND (VS1, insn, va_arg (args, int));
1443 continue;
1444 case 't':
1445 INSERT_OPERAND (VS2, insn, va_arg (args, int));
1446 continue;
1447 case 'm':
1449 int reg = va_arg (args, int);
1450 if (reg == -1)
1452 INSERT_OPERAND (VMASK, insn, 1);
1453 continue;
1455 else if (reg == 0)
1457 INSERT_OPERAND (VMASK, insn, 0);
1458 continue;
1460 else
1461 goto unknown_macro_argument;
1463 default:
1464 goto unknown_macro_argument;
1466 break;
1468 case 'd':
1469 INSERT_OPERAND (RD, insn, va_arg (args, int));
1470 continue;
1471 case 's':
1472 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1473 continue;
1474 case 't':
1475 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1476 continue;
1478 case 'j':
1479 case 'u':
1480 case 'q':
1481 gas_assert (ep != NULL);
1482 r = va_arg (args, int);
1483 continue;
1485 case '\0':
1486 break;
1487 case ',':
1488 continue;
1489 default:
1490 unknown_macro_argument:
1491 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
1493 break;
1495 va_end (args);
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. */
1504 static void
1505 md_assemblef (const char *format, ...)
1507 char *buf = NULL;
1508 va_list ap;
1509 int r;
1511 va_start (ap, format);
1513 r = vasprintf (&buf, format, ap);
1515 if (r < 0)
1516 as_fatal (_("internal: vasprintf failed"));
1518 md_assemble (buf);
1519 free(buf);
1521 va_end (ap);
1524 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1525 unset. */
1527 static void
1528 normalize_constant_expr (expressionS *ex)
1530 if (xlen > 32)
1531 return;
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)
1535 - 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. */
1541 static void
1542 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1543 bool maybe_csr)
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"),
1552 ip->insn_mo->name);
1553 normalize_constant_expr (ex);
1556 static symbolS *
1557 make_internal_label (void)
1559 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1560 frag_now_fix ());
1563 /* Load an entry from the GOT. */
1565 static void
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)
1571 expressionS ep2;
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);
1580 static void
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);
1588 static void
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. */
1598 static void
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. */
1603 frag_grow (8);
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);
1608 frag_new (0);
1611 /* Load an integer constant into a register. */
1613 static void
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"));
1625 return;
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)
1632 shift++;
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);
1642 else
1644 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1645 int hi_reg = 0;
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);
1653 hi_reg = reg;
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. */
1664 static void
1665 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
1667 if (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);
1672 else
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. */
1681 static void
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;
1691 switch (mask)
1693 case M_VMSGE:
1694 if (vm)
1696 /* Unmasked. */
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);
1699 break;
1701 if (vtemp != 0)
1703 /* Masked. Have vtemp to avoid overlap constraints. */
1704 if (vd == vm)
1706 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1707 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1709 else
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);
1718 else if (vd != vm)
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);
1724 else
1725 as_bad (_("must provide temp if destination overlaps mask"));
1726 break;
1728 case M_VMSGEU:
1729 if (vm)
1731 /* Unmasked. */
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);
1734 break;
1736 if (vtemp != 0)
1738 /* Masked. Have vtemp to avoid overlap constraints. */
1739 if (vd == vm)
1741 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1742 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1744 else
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);
1753 else if (vd != vm)
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);
1759 else
1760 as_bad (_("must provide temp if destination overlaps mask"));
1761 break;
1763 default:
1764 break;
1768 /* Expand RISC-V assembly macros into one or more instructions. */
1770 static void
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;
1779 switch (mask)
1781 case M_LI:
1782 load_const (rd, imm_expr);
1783 break;
1785 case M_LA:
1786 case M_LLA:
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);
1799 break;
1801 case M_LA_TLS_GD:
1802 pcrel_load (rd, rd, imm_expr, "addi",
1803 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1804 break;
1806 case M_LA_TLS_IE:
1807 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1808 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1809 break;
1811 case M_LB:
1812 pcrel_load (rd, rd, imm_expr, "lb",
1813 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1814 break;
1816 case M_LBU:
1817 pcrel_load (rd, rd, imm_expr, "lbu",
1818 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1819 break;
1821 case M_LH:
1822 pcrel_load (rd, rd, imm_expr, "lh",
1823 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1824 break;
1826 case M_LHU:
1827 pcrel_load (rd, rd, imm_expr, "lhu",
1828 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1829 break;
1831 case M_LW:
1832 pcrel_load (rd, rd, imm_expr, "lw",
1833 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1834 break;
1836 case M_LWU:
1837 pcrel_load (rd, rd, imm_expr, "lwu",
1838 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1839 break;
1841 case M_LD:
1842 pcrel_load (rd, rd, imm_expr, "ld",
1843 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1844 break;
1846 case M_FLW:
1847 pcrel_load (rd, rs1, imm_expr, "flw",
1848 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1849 break;
1851 case M_FLD:
1852 pcrel_load (rd, rs1, imm_expr, "fld",
1853 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1854 break;
1856 case M_SB:
1857 pcrel_store (rs2, rs1, imm_expr, "sb",
1858 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1859 break;
1861 case M_SH:
1862 pcrel_store (rs2, rs1, imm_expr, "sh",
1863 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1864 break;
1866 case M_SW:
1867 pcrel_store (rs2, rs1, imm_expr, "sw",
1868 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1869 break;
1871 case M_SD:
1872 pcrel_store (rs2, rs1, imm_expr, "sd",
1873 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1874 break;
1876 case M_FSW:
1877 pcrel_store (rs2, rs1, imm_expr, "fsw",
1878 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1879 break;
1881 case M_FSD:
1882 pcrel_store (rs2, rs1, imm_expr, "fsd",
1883 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1884 break;
1886 case M_CALL:
1887 riscv_call (rd, rs1, imm_expr, *imm_reloc);
1888 break;
1890 case M_ZEXTH:
1891 riscv_ext (rd, rs1, xlen - 16, false);
1892 break;
1894 case M_ZEXTW:
1895 riscv_ext (rd, rs1, xlen - 32, false);
1896 break;
1898 case M_SEXTB:
1899 riscv_ext (rd, rs1, xlen - 8, true);
1900 break;
1902 case M_SEXTH:
1903 riscv_ext (rd, rs1, xlen - 16, true);
1904 break;
1906 case M_VMSGE:
1907 case M_VMSGEU:
1908 vector_macro (ip);
1909 break;
1911 case M_FLH:
1912 pcrel_load (rd, rs1, imm_expr, "flh",
1913 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1914 break;
1915 case M_FSH:
1916 pcrel_store (rs2, rs1, imm_expr, "fsh",
1917 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1918 break;
1920 default:
1921 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
1922 break;
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},
1934 {0, 0}
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},
1942 {0, 0}
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},
1950 {0, 0}
1953 static const struct percent_op_match percent_op_rtype[] =
1955 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1956 {0, 0}
1959 static const struct percent_op_match percent_op_null[] =
1961 {0, 0}
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. */
1968 static bool
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] != '(')
1978 continue;
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;
1992 return true;
1994 return false;
1997 static void
1998 my_getExpression (expressionS *ep, char *str)
2000 char *save_in;
2002 save_in = input_line_pointer;
2003 input_line_pointer = str;
2004 expression (ep);
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. */
2015 static size_t
2016 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2017 char *str, const struct percent_op_match *percent_op)
2019 size_t reloc_index;
2020 unsigned crux_depth, str_depth, regno;
2021 char *crux;
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, &regno))
2029 ep->X_op = O_register;
2030 ep->X_add_number = regno;
2031 expr_end = str;
2032 return 0;
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. */
2039 reloc_index = -1;
2040 str_depth = 0;
2043 reloc_index++;
2044 crux = str;
2045 crux_depth = str_depth;
2047 /* Skip over whitespace and brackets, keeping count of the number
2048 of brackets. */
2049 while (*str == ' ' || *str == '\t' || *str == '(')
2050 if (*str++ == '(')
2051 str_depth++;
2053 while (*str == '%'
2054 && reloc_index < 1
2055 && parse_relocation (&str, reloc, percent_op));
2057 my_getExpression (ep, crux);
2058 str = expr_end;
2060 /* Match every open bracket. */
2061 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2062 if (*str++ == ')')
2063 crux_depth--;
2065 if (crux_depth > 0)
2066 as_bad ("unclosed '('");
2068 expr_end = str;
2070 return reloc_index;
2073 /* Parse opcode name, could be an mnemonics or number. */
2075 static size_t
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);
2081 if (o != NULL)
2083 ep->X_op = O_constant;
2084 ep->X_add_number = o->val;
2085 return 0;
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. */
2094 static void
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))
2104 if (*str == ',')
2105 ++str;
2106 if (vsew_found)
2107 as_bad (_("multiple vsew constants"));
2108 vsew_found = TRUE;
2110 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2112 if (*str == ',')
2113 ++str;
2114 if (vlmul_found)
2115 as_bad (_("multiple vlmul constants"));
2116 vlmul_found = TRUE;
2118 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2120 if (*str == ',')
2121 ++str;
2122 if (vta_found)
2123 as_bad (_("multiple vta constants"));
2124 vta_found = TRUE;
2126 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2128 if (*str == ',')
2129 ++str;
2130 if (vma_found)
2131 as_bad (_("multiple vma constants"));
2132 vma_found = TRUE;
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);
2142 expr_end = str;
2144 else
2146 my_getExpression (ep, str);
2147 str = expr_end;
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. */
2155 static bool
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;
2164 return true;
2167 return false;
2170 /* All RISC-V CSR instructions belong to one of these classes. */
2171 enum csr_insn_type
2173 INSN_NOT_CSR,
2174 INSN_CSRRW,
2175 INSN_CSRRS,
2176 INSN_CSRRC
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)
2186 return INSN_CSRRW;
2187 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2188 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2189 return INSN_CSRRS;
2190 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2191 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2192 return INSN_CSRRC;
2193 else
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. */
2201 static bool
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);
2209 if (readonly
2210 && (((csr_insn == INSN_CSRRS
2211 || csr_insn == INSN_CSRRC)
2212 && rs1 != 0)
2213 || csr_insn == INSN_CSRRW))
2214 return false;
2216 return true;
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,
2230 too. */
2232 static bool
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;
2256 char save_c = 0;
2257 struct riscv_opcode *insn;
2258 unsigned int regno;
2259 int argnum;
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))
2273 save_c = *asarg;
2274 *asarg++ = '\0';
2275 break;
2278 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2280 asargStart = asarg;
2281 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2283 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2284 continue;
2286 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2288 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2289 insn->insn_class);
2290 continue;
2293 /* Reset error message of the previous round. */
2294 error.msg = _("illegal operands");
2295 error.missing_ext = NULL;
2296 create_insn (ip, insn);
2297 argnum = 1;
2299 imm_expr->X_op = O_absent;
2300 *imm_reloc = BFD_RELOC_UNUSED;
2301 p = percent_op_itype;
2303 for (oparg = insn->args;; ++oparg)
2305 opargStart = oparg;
2306 asarg += strspn (asarg, " \t");
2307 switch (*oparg)
2309 case '\0': /* End of args. */
2310 if (insn->pinfo != INSN_MACRO)
2312 if (!insn->match_func (insn, ip->insn_opcode))
2313 break;
2315 /* For .insn, insn->match and insn->mask are 0. */
2316 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2317 ? ip->insn_opcode
2318 : insn->match) == 2
2319 && !riscv_opts.rvc)
2320 break;
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
2326 instruction. */
2327 if (insn_with_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. */
2333 if (save_c)
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");
2346 break;
2349 if (*asarg != '\0')
2350 break;
2351 /* Successful assembly. */
2352 error.msg = NULL;
2353 insn_with_csr = false;
2354 goto out;
2356 case 'C': /* RVC */
2357 switch (*++oparg)
2359 case 's': /* RS1 x8-x15. */
2360 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2361 || !(regno >= 8 && regno <= 15))
2362 break;
2363 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2364 continue;
2365 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2366 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2367 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2368 break;
2369 continue;
2370 case 't': /* RS2 x8-x15. */
2371 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2372 || !(regno >= 8 && regno <= 15))
2373 break;
2374 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2375 continue;
2376 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2377 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2378 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2379 break;
2380 continue;
2381 case 'U': /* RS1, constrained to equal RD. */
2382 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2383 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2384 break;
2385 continue;
2386 case 'V': /* RS2 */
2387 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2388 break;
2389 INSERT_OPERAND (CRS2, *ip, regno);
2390 continue;
2391 case 'c': /* RS1, constrained to equal sp. */
2392 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2393 || regno != X_SP)
2394 break;
2395 continue;
2396 case 'z': /* RS2, constrained to equal x0. */
2397 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2398 || regno != 0)
2399 break;
2400 continue;
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)
2405 break;
2406 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2407 rvc_imm_done:
2408 asarg = expr_end;
2409 imm_expr->X_op = O_absent;
2410 continue;
2411 case '5':
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))
2417 break;
2418 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2419 goto rvc_imm_done;
2420 case '6':
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))
2426 break;
2427 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2428 goto rvc_imm_done;
2429 case '8':
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))
2435 break;
2436 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2437 goto rvc_imm_done;
2438 case 'j':
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))
2443 break;
2444 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2445 goto rvc_imm_done;
2446 case 'k':
2447 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2448 continue;
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))
2452 break;
2453 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2454 goto rvc_imm_done;
2455 case 'l':
2456 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2457 continue;
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))
2461 break;
2462 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2463 goto rvc_imm_done;
2464 case 'm':
2465 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2466 continue;
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))
2470 break;
2471 ip->insn_opcode |=
2472 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
2473 goto rvc_imm_done;
2474 case 'n':
2475 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2476 continue;
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))
2480 break;
2481 ip->insn_opcode |=
2482 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
2483 goto rvc_imm_done;
2484 case 'o':
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
2489 is same as 'j'. */
2490 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2491 break;
2492 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2493 goto rvc_imm_done;
2494 case 'K':
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))
2499 break;
2500 ip->insn_opcode |=
2501 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
2502 goto rvc_imm_done;
2503 case 'L':
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))
2507 break;
2508 ip->insn_opcode |=
2509 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
2510 goto rvc_imm_done;
2511 case 'M':
2512 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2513 continue;
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))
2517 break;
2518 ip->insn_opcode |=
2519 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
2520 goto rvc_imm_done;
2521 case 'N':
2522 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2523 continue;
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))
2527 break;
2528 ip->insn_opcode |=
2529 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
2530 goto rvc_imm_done;
2531 case 'u':
2532 p = percent_op_utype;
2533 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
2534 break;
2535 rvc_lui:
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)))
2542 break;
2543 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2544 goto rvc_imm_done;
2545 case 'v':
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))
2550 break;
2551 imm_expr->X_add_number =
2552 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2553 goto rvc_lui;
2554 case 'p':
2555 goto branch;
2556 case 'a':
2557 goto jump;
2558 case 'S': /* Floating-point RS1 x8-x15. */
2559 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2560 || !(regno >= 8 && regno <= 15))
2561 break;
2562 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2563 continue;
2564 case 'D': /* Floating-point RS2 x8-x15. */
2565 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2566 || !(regno >= 8 && regno <= 15))
2567 break;
2568 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2569 continue;
2570 case 'T': /* Floating-point RS2. */
2571 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
2572 break;
2573 INSERT_OPERAND (CRS2, *ip, regno);
2574 continue;
2575 case 'F':
2576 switch (*++oparg)
2578 case '6':
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"));
2586 break;
2588 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2589 imm_expr->X_op = O_absent;
2590 asarg = expr_end;
2591 continue;
2593 case '4':
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"));
2601 break;
2603 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2604 imm_expr->X_op = O_absent;
2605 asarg = expr_end;
2606 continue;
2608 case '3':
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"));
2616 break;
2618 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2619 imm_expr->X_op = O_absent;
2620 asarg = expr_end;
2621 continue;
2623 case '2':
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"));
2631 break;
2633 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2634 imm_expr->X_op = O_absent;
2635 asarg = expr_end;
2636 continue;
2638 default:
2639 goto unknown_riscv_ip_operand;
2641 break;
2643 default:
2644 goto unknown_riscv_ip_operand;
2646 break; /* end RVC */
2648 case 'V': /* RVV */
2649 switch (*++oparg)
2651 case 'd': /* VD */
2652 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2653 break;
2654 INSERT_OPERAND (VD, *ip, regno);
2655 continue;
2657 case 'e': /* AMO VD */
2658 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
2659 INSERT_OPERAND (VWD, *ip, 0);
2660 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
2662 INSERT_OPERAND (VWD, *ip, 1);
2663 INSERT_OPERAND (VD, *ip, regno);
2665 else
2666 break;
2667 continue;
2669 case 'f': /* AMO VS3 */
2670 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2671 break;
2672 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
2673 INSERT_OPERAND (VD, *ip, regno);
2674 else
2676 /* VS3 must match VD. */
2677 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
2678 break;
2680 continue;
2682 case 's': /* VS1 */
2683 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2684 break;
2685 INSERT_OPERAND (VS1, *ip, regno);
2686 continue;
2688 case 't': /* VS2 */
2689 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2690 break;
2691 INSERT_OPERAND (VS2, *ip, regno);
2692 continue;
2694 case 'u': /* VS1 == VS2 */
2695 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2696 break;
2697 INSERT_OPERAND (VS1, *ip, regno);
2698 INSERT_OPERAND (VS2, *ip, regno);
2699 continue;
2701 case 'v': /* VD == VS1 == VS2 */
2702 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2703 break;
2704 INSERT_OPERAND (VD, *ip, regno);
2705 INSERT_OPERAND (VS1, *ip, regno);
2706 INSERT_OPERAND (VS2, *ip, regno);
2707 continue;
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
2712 register. */
2713 case '0':
2714 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
2715 continue;
2716 break;
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"));
2724 ip->insn_opcode
2725 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
2726 imm_expr->X_op = O_absent;
2727 asarg = expr_end;
2728 continue;
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"));
2736 ip->insn_opcode
2737 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
2738 imm_expr->X_op = O_absent;
2739 asarg = expr_end;
2740 continue;
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;
2751 asarg = expr_end;
2752 continue;
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;
2763 asarg = expr_end;
2764 continue;
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;
2775 asarg = expr_end;
2776 continue;
2778 case 'm': /* optional vector mask */
2779 if (*asarg == '\0')
2781 INSERT_OPERAND (VMASK, *ip, 1);
2782 continue;
2784 else if (*asarg == ',' && asarg++
2785 && reg_lookup (&asarg, RCLASS_VECM, &regno)
2786 && regno == 0)
2788 INSERT_OPERAND (VMASK, *ip, 0);
2789 continue;
2791 break;
2793 case 'M': /* required vector mask */
2794 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
2796 INSERT_OPERAND (VMASK, *ip, 0);
2797 continue;
2799 break;
2801 case 'T': /* vector macro temporary register */
2802 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
2803 break;
2804 /* Store it in the FUNCT6 field as we don't have anyplace
2805 else to store it. */
2806 INSERT_OPERAND (VFUNCT6, *ip, regno);
2807 continue;
2809 default:
2810 goto unknown_riscv_ip_operand;
2812 break; /* end RVV */
2814 case ',':
2815 ++argnum;
2816 if (*asarg++ == *oparg)
2817 continue;
2818 asarg--;
2819 break;
2821 case '(':
2822 case ')':
2823 case '[':
2824 case ']':
2825 if (*asarg++ == *oparg)
2826 continue;
2827 break;
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;
2837 asarg = expr_end;
2838 continue;
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;
2848 asarg = expr_end;
2849 continue;
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;
2859 asarg = expr_end;
2860 continue;
2862 case 'E': /* Control register. */
2863 insn_with_csr = true;
2864 explicit_priv_attr = true;
2865 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
2866 INSERT_OPERAND (CSR, *ip, regno);
2867 else
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;
2876 asarg = expr_end;
2878 continue;
2880 case 'm': /* Rounding mode. */
2881 if (arg_lookup (&asarg, riscv_rm,
2882 ARRAY_SIZE (riscv_rm), &regno))
2884 INSERT_OPERAND (RM, *ip, regno);
2885 continue;
2887 break;
2889 case 'P':
2890 case 'Q': /* Fence predecessor/successor. */
2891 if (arg_lookup (&asarg, riscv_pred_succ,
2892 ARRAY_SIZE (riscv_pred_succ), &regno))
2894 if (*oparg == 'P')
2895 INSERT_OPERAND (PRED, *ip, regno);
2896 else
2897 INSERT_OPERAND (SUCC, *ip, regno);
2898 continue;
2900 break;
2902 case 'd': /* Destination register. */
2903 case 's': /* Source register. */
2904 case 't': /* Target register. */
2905 case 'r': /* RS3 */
2906 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
2908 char c = *oparg;
2909 if (*asarg == ' ')
2910 ++asarg;
2912 /* Now that we have assembled one operand, we use the args
2913 string to figure out where it goes in the instruction. */
2914 switch (c)
2916 case 's':
2917 INSERT_OPERAND (RS1, *ip, regno);
2918 break;
2919 case 'd':
2920 INSERT_OPERAND (RD, *ip, regno);
2921 break;
2922 case 't':
2923 INSERT_OPERAND (RS2, *ip, regno);
2924 break;
2925 case 'r':
2926 INSERT_OPERAND (RS3, *ip, regno);
2927 break;
2929 continue;
2931 break;
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), &regno))
2942 char c = *oparg;
2943 if (*asarg == ' ')
2944 ++asarg;
2945 switch (c)
2947 case 'D':
2948 INSERT_OPERAND (RD, *ip, regno);
2949 break;
2950 case 'S':
2951 INSERT_OPERAND (RS1, *ip, regno);
2952 break;
2953 case 'U':
2954 INSERT_OPERAND (RS1, *ip, regno);
2955 /* Fall through. */
2956 case 'T':
2957 INSERT_OPERAND (RS2, *ip, regno);
2958 break;
2959 case 'R':
2960 INSERT_OPERAND (RS3, *ip, regno);
2961 break;
2963 continue;
2965 break;
2967 case 'I':
2968 my_getExpression (imm_expr, asarg);
2969 if (imm_expr->X_op != O_big
2970 && imm_expr->X_op != O_constant)
2971 break;
2972 normalize_constant_expr (imm_expr);
2973 asarg = expr_end;
2974 continue;
2976 case 'A':
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)
2981 break;
2982 *imm_reloc = BFD_RELOC_32;
2983 asarg = expr_end;
2984 continue;
2986 case 'B':
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)
2991 break;
2992 if (imm_expr->X_op == O_symbol)
2993 *imm_reloc = BFD_RELOC_32;
2994 asarg = expr_end;
2995 continue;
2997 case 'j': /* Sign-extended immediate. */
2998 p = percent_op_itype;
2999 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3000 goto alu_op;
3001 case 'q': /* Store displacement. */
3002 p = percent_op_stype;
3003 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3004 goto load_store;
3005 case 'o': /* Load displacement. */
3006 p = percent_op_itype;
3007 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3008 goto load_store;
3009 case '1':
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;
3014 goto alu_op;
3015 case '0': /* AMO displacement, which must be zero. */
3016 p = percent_op_null;
3017 load_store:
3018 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3019 continue;
3020 alu_op:
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
3023 code pattern. */
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)
3029 || (*oparg == '1')
3030 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3031 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3032 break;
3034 asarg = expr_end;
3035 continue;
3037 case 'p': /* PC-relative offset. */
3038 branch:
3039 *imm_reloc = BFD_RELOC_12_PCREL;
3040 my_getExpression (imm_expr, asarg);
3041 asarg = expr_end;
3042 continue;
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)
3049 break;
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;
3058 asarg = expr_end;
3059 continue;
3061 case 'a': /* 20-bit PC-relative offset. */
3062 jump:
3063 my_getExpression (imm_expr, asarg);
3064 asarg = expr_end;
3065 *imm_reloc = BFD_RELOC_RISCV_JMP;
3066 continue;
3068 case 'c':
3069 my_getExpression (imm_expr, asarg);
3070 asarg = expr_end;
3071 if (strcmp (asarg, "@plt") == 0)
3073 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3074 asarg += 4;
3076 else
3077 *imm_reloc = BFD_RELOC_RISCV_CALL;
3078 continue;
3080 case 'O':
3081 switch (*++oparg)
3083 case '4':
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"));
3093 break;
3095 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3096 imm_expr->X_op = O_absent;
3097 asarg = expr_end;
3098 continue;
3100 case '2':
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"));
3108 break;
3110 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3111 imm_expr->X_op = O_absent;
3112 asarg = expr_end;
3113 continue;
3115 default:
3116 goto unknown_riscv_ip_operand;
3118 break;
3120 case 'F':
3121 switch (*++oparg)
3123 case '7':
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"));
3131 break;
3133 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3134 imm_expr->X_op = O_absent;
3135 asarg = expr_end;
3136 continue;
3138 case '3':
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"));
3146 break;
3148 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3149 imm_expr->X_op = O_absent;
3150 asarg = expr_end;
3151 continue;
3153 case '2':
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"));
3161 break;
3163 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3164 imm_expr->X_op = O_absent;
3165 asarg = expr_end;
3166 continue;
3168 default:
3169 goto unknown_riscv_ip_operand;
3171 break;
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;
3181 asarg = expr_end;
3182 continue;
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;
3192 asarg = expr_end;
3193 continue;
3195 case 'z':
3196 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3197 || imm_expr->X_op != O_constant
3198 || imm_expr->X_add_number != 0)
3199 break;
3200 asarg = expr_end;
3201 imm_expr->X_op = O_absent;
3202 continue;
3204 case 'f': /* Prefetch offset, pseudo S-type but lower 5-bits zero. */
3205 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3206 continue;
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);
3214 ip->insn_opcode |=
3215 ENCODE_STYPE_IMM ((unsigned) (imm_expr->X_add_number) &
3216 ~ 0x1fU);
3217 imm_expr->X_op = O_absent;
3218 asarg = expr_end;
3219 continue;
3221 default:
3222 unknown_riscv_ip_operand:
3223 as_fatal (_("internal: unknown argument type `%s'"),
3224 opargStart);
3226 break;
3228 asarg = asargStart;
3229 insn_with_csr = false;
3232 out:
3233 /* Restore the character we might have clobbered above. */
3234 if (save_c)
3235 *(asargStart - 1) = save_c;
3237 return error;
3240 /* Similar to riscv_ip, but assembles an instruction according to the
3241 hardcode values of .insn directive. */
3243 static const char *
3244 riscv_ip_hardcode (char *str,
3245 struct riscv_cl_insn *ip,
3246 expressionS *imm_expr,
3247 const char *error)
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
3261 in the riscv_ip. */
3262 if (num == 0)
3263 return error;
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");
3282 return NULL;
3285 void
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))
3300 return;
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);
3308 if (error.msg)
3310 if (error.missing_ext)
3311 as_bad ("%s `%s', extension `%s' required", error.msg,
3312 error.statement, error.missing_ext);
3313 else
3314 as_bad ("%s `%s'", error.msg, error.statement);
3315 return;
3318 if (insn.insn_mo->pinfo == INSN_MACRO)
3319 macro (&insn, &imm_expr, &imm_reloc);
3320 else
3321 append_insn (&insn, &imm_expr, imm_reloc);
3324 const char *
3325 md_atof (int type, char *litP, int *sizeP)
3327 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
3330 void
3331 md_number_to_chars (char *buf, valueT val, int n)
3333 if (target_big_endian)
3334 number_to_chars_bigendian (buf, val, n);
3335 else
3336 number_to_chars_littleendian (buf, val, n);
3339 const char *md_shortopts = "O::g::G:";
3341 enum options
3343 OPTION_MARCH = OPTION_MD_BASE,
3344 OPTION_PIC,
3345 OPTION_NO_PIC,
3346 OPTION_MABI,
3347 OPTION_RELAX,
3348 OPTION_NO_RELAX,
3349 OPTION_ARCH_ATTR,
3350 OPTION_NO_ARCH_ATTR,
3351 OPTION_CSR_CHECK,
3352 OPTION_NO_CSR_CHECK,
3353 OPTION_MISA_SPEC,
3354 OPTION_MPRIV_SPEC,
3355 OPTION_BIG_ENDIAN,
3356 OPTION_LITTLE_ENDIAN,
3357 OPTION_END_OF_ENUM
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)
3385 switch (c)
3387 case OPTION_MARCH:
3388 default_arch_with_ext = arg;
3389 break;
3391 case OPTION_NO_PIC:
3392 riscv_opts.pic = false;
3393 break;
3395 case OPTION_PIC:
3396 riscv_opts.pic = true;
3397 break;
3399 case OPTION_MABI:
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);
3418 else
3419 return 0;
3420 explicit_mabi = true;
3421 break;
3423 case OPTION_RELAX:
3424 riscv_opts.relax = true;
3425 break;
3427 case OPTION_NO_RELAX:
3428 riscv_opts.relax = false;
3429 break;
3431 case OPTION_ARCH_ATTR:
3432 riscv_opts.arch_attr = true;
3433 break;
3435 case OPTION_NO_ARCH_ATTR:
3436 riscv_opts.arch_attr = false;
3437 break;
3439 case OPTION_CSR_CHECK:
3440 riscv_opts.csr_check = true;
3441 break;
3443 case OPTION_NO_CSR_CHECK:
3444 riscv_opts.csr_check = false;
3445 break;
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;
3455 break;
3457 case OPTION_LITTLE_ENDIAN:
3458 target_big_endian = 0;
3459 break;
3461 default:
3462 return 0;
3465 return 1;
3468 void
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. */
3473 if (xlen == 0)
3475 if (strcmp (default_arch, "riscv32") == 0)
3476 xlen = 32;
3477 else if (strcmp (default_arch, "riscv64") == 0)
3478 xlen = 64;
3479 else
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;
3498 long
3499 md_pcrel_from (fixS *fixP)
3501 return fixP->fx_where + fixP->fx_frag->fr_address;
3504 /* Apply a fixup to the object file. */
3506 void
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;
3512 offsetT loc;
3513 segT sub_segment;
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;
3527 relaxable = true;
3528 break;
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:
3541 break;
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:
3547 relaxable = true;
3548 /* Fall through. */
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);
3556 else
3557 as_bad_where (fixP->fx_file, fixP->fx_line,
3558 _("TLS relocation against a constant"));
3559 break;
3561 case BFD_RELOC_32:
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;
3576 break;
3578 /* Fall through. */
3579 case BFD_RELOC_64:
3580 case BFD_RELOC_16:
3581 case BFD_RELOC_8:
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)
3593 case BFD_RELOC_64:
3594 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
3595 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
3596 break;
3598 case BFD_RELOC_32:
3599 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
3600 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3601 break;
3603 case BFD_RELOC_16:
3604 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
3605 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3606 break;
3608 case BFD_RELOC_8:
3609 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
3610 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3611 break;
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);
3617 switch (subtype)
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;
3624 break;
3626 case DW_CFA_advance_loc2:
3627 fixP->fx_size = 2;
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;
3633 break;
3635 case DW_CFA_advance_loc4:
3636 fixP->fx_size = 4;
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;
3642 break;
3644 default:
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;
3653 else
3654 as_fatal (_("internal: bad CFA value #%d"), subtype);
3655 break;
3657 break;
3659 default:
3660 /* This case is unreachable. */
3661 abort ();
3664 /* Fall through. */
3666 case BFD_RELOC_RVA:
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);
3674 fixP->fx_done = 1;
3676 break;
3678 case BFD_RELOC_RISCV_JMP:
3679 if (fixP->fx_addsy)
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);
3686 break;
3688 case BFD_RELOC_12_PCREL:
3689 if (fixP->fx_addsy)
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);
3696 break;
3698 case BFD_RELOC_RISCV_RVC_BRANCH:
3699 if (fixP->fx_addsy)
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);
3706 break;
3708 case BFD_RELOC_RISCV_RVC_JUMP:
3709 if (fixP->fx_addsy)
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);
3716 break;
3718 case BFD_RELOC_RISCV_CALL:
3719 case BFD_RELOC_RISCV_CALL_PLT:
3720 relaxable = true;
3721 break;
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;
3727 break;
3729 case BFD_RELOC_RISCV_ALIGN:
3730 break;
3732 default:
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. */
3754 void
3755 riscv_pre_output_hook (void)
3757 const frchainS *frch;
3758 segT s;
3760 /* Save the current segment info. */
3761 segT seg = now_seg;
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)
3767 fragS *frag;
3769 for (frag = frch->frch_root; frag; frag = frag->fr_next)
3771 if (frag->fr_type == rs_cfa)
3773 expressionS exp;
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. */
3798 static void
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)
3832 name += 5;
3833 if (ISSPACE (*name) && *name != '\0')
3834 name++;
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;
3858 if (s == NULL)
3859 as_bad (_(".option pop with no .option push"));
3860 else
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);
3868 free (s);
3871 else
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. */
3883 static void
3884 s_dtprel (int bytes)
3886 expressionS ex;
3887 char *p;
3889 expression (&ex);
3891 if (ex.X_op != O_symbol)
3893 as_bad (_("unsupported use of %s"), (bytes == 8
3894 ? ".dtpreldword"
3895 : ".dtprelword"));
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,
3902 (bytes == 8
3903 ? BFD_RELOC_RISCV_TLS_DTPREL64
3904 : BFD_RELOC_RISCV_TLS_DTPREL32));
3906 demand_empty_rest_of_line ();
3909 /* Handle the .bss pseudo-op. */
3911 static void
3912 s_bss (int ignore ATTRIBUTE_UNUSED)
3914 subseg_set (bss_section, 0);
3915 demand_empty_rest_of_line ();
3918 static void
3919 riscv_make_nops (char *buf, bfd_vma bytes)
3921 bfd_vma i = 0;
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. */
3926 if (bytes % 2 == 1)
3927 buf[i++] = 0;
3929 /* Use at most one 2-byte NOP. */
3930 if ((bytes - i) % 4 == 2)
3932 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
3933 i += 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. */
3946 bool
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;
3952 char *nops;
3953 expressionS ex;
3955 /* If we are moving to a smaller alignment than the instruction size, then no
3956 alignment is required. */
3957 if (bytes <= insn_alignment)
3958 return true;
3960 /* When not relaxing, riscv_handle_align handles code alignment. */
3961 if (!riscv_opts.relax)
3962 return false;
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);
3980 frag_new (0);
3982 return true;
3985 /* Implement HANDLE_ALIGN. */
3987 void
3988 riscv_handle_align (fragS *fragP)
3990 switch (fragP->fr_type)
3992 case rs_align_code:
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;
4004 if (bytes <= 0)
4005 break;
4007 /* Insert zeros or compressed nops to get 4 byte alignment. */
4008 if (excess)
4010 if (odd_padding)
4011 riscv_add_odd_padding_symbol (fragP);
4012 riscv_make_nops (p, excess);
4013 fragP->fr_fix += excess;
4014 p += 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;
4027 break;
4029 default:
4030 break;
4034 /* This usually called from frag_var. */
4036 void
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)
4041 return;
4043 switch (fragP->fr_type)
4045 case rs_fill:
4046 case rs_align:
4047 case rs_align_test:
4048 riscv_mapping_state (MAP_DATA, max_chars);
4049 break;
4050 case rs_align_code:
4051 riscv_mapping_state (MAP_INSN, max_chars);
4052 break;
4053 default:
4054 break;
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
4065 format. */
4067 arelent *
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. */
4085 return reloc;
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));
4091 return NULL;
4094 return reloc;
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;
4107 return 0;
4110 /* Expand far branches to multi-instruction sequences. */
4112 static void
4113 md_convert_frag_branch (fragS *fragp)
4115 bfd_byte *buf;
4116 expressionS exp;
4117 fixS *fixp;
4118 insn_t insn;
4119 int rs1, reloc;
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))
4133 case 8:
4134 case 4:
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)
4139 insn = MATCH_JAL;
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);
4146 else
4147 abort ();
4148 bfd_putl32 (insn, buf);
4149 break;
4151 case 6:
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);
4157 buf += 2;
4158 goto jump;
4160 case 2:
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);
4166 buf += 2;
4167 goto done;
4169 default:
4170 abort ();
4174 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4176 case 8:
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);
4184 buf += 4;
4186 jump:
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);
4191 buf += 4;
4192 break;
4194 case 4:
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);
4199 buf += 4;
4200 break;
4202 default:
4203 abort ();
4206 done:
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. */
4219 void
4220 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
4221 fragS *fragp)
4223 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
4224 md_convert_frag_branch (fragp);
4227 void
4228 md_show_usage (FILE *stream)
4230 fprintf (stream, _("\
4231 RISC-V options:\n\
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\
4246 "));
4249 /* Standard calling conventions leave the CFA at SP on entry. */
4251 void
4252 riscv_cfi_frame_initial_instructions (void)
4254 cfi_add_CFA_def_cfa_register (X_SP);
4258 tc_riscv_regname_to_dw2regnum (char *regname)
4260 int reg;
4262 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
4263 return reg;
4265 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
4266 return reg + 32;
4268 /* CSRs are numbered 4096 -> 8191. */
4269 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
4270 return reg + 4096;
4272 as_bad (_("unknown register `%s'"), regname);
4273 return -1;
4276 void
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. */
4286 static void
4287 s_riscv_leb128 (int sign)
4289 expressionS exp;
4290 char *save_in = input_line_pointer;
4292 expression (&exp);
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>. */
4306 static void
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;
4313 char save_c;
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);
4325 if (error.msg)
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;
4332 if (error.msg)
4334 if (error.missing_ext)
4335 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
4336 error.missing_ext);
4337 else
4338 as_bad ("%s `%s'", error.msg, error.statement);
4340 else
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. */
4353 static void
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;
4361 unsigned int i;
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
4370 called. */
4371 if (!start_assemble
4372 && !riscv_set_default_priv_spec (NULL))
4373 return;
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)
4379 return;
4381 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
4382 p = priv_str;
4383 for (i = 0; *p; ++p)
4385 if (*p == '.' && i < 3)
4387 versions[i++] = number;
4388 number = 0;
4390 else if (ISDIGIT (*p))
4391 number = (number * 10) + (*p - '0');
4392 else
4394 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
4395 return;
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. */
4408 static void
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. */
4417 void
4418 riscv_md_end (void)
4420 riscv_set_public_attributes ();
4423 /* Adjust the symbol table. */
4425 void
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)
4438 static const struct
4440 const char *name;
4441 const int tag;
4443 attribute_table[] =
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}
4448 T(arch),
4449 T(priv_spec),
4450 T(priv_spec_minor),
4451 T(priv_spec_revision),
4452 T(unaligned_access),
4453 T(stack_align),
4454 #undef T
4457 if (name == NULL)
4458 return -1;
4460 unsigned int i;
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;
4465 return -1;
4468 /* Parse a .attribute directive. */
4470 static void
4471 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
4473 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4474 unsigned old_xlen;
4475 obj_attribute *attr;
4477 explicit_attr = true;
4478 switch (tag)
4480 case Tag_RISCV_arch:
4481 old_xlen = xlen;
4482 attr = elf_known_obj_attributes_proc (stdoutput);
4483 if (!start_assemble)
4484 riscv_set_arch (attr[Tag_RISCV_arch].s);
4485 else
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"));
4498 break;
4500 case Tag_RISCV_priv_spec:
4501 case Tag_RISCV_priv_spec_minor:
4502 case Tag_RISCV_priv_spec_revision:
4503 if (start_assemble)
4504 as_fatal (_("privileged elf attributes must set before "
4505 "any instructions"));
4506 break;
4508 default:
4509 break;
4513 /* Mark symbol that it follows a variant CC convention. */
4515 static void
4516 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
4518 char *name;
4519 char c;
4520 symbolS *sym;
4521 asymbol *bfdsym;
4522 elf_symbol_type *elfsym;
4524 c = get_symbol_name (&name);
4525 if (!*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. */
4547 void
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)
4557 if (srcelf->size)
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},
4570 {"half", cons, 2},
4571 {"word", cons, 4},
4572 {"dword", cons, 8},
4573 {"dtprelword", s_dtprel, 4},
4574 {"dtpreldword", s_dtprel, 8},
4575 {"bss", s_bss, 0},
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'},
4583 { NULL, NULL, 0 },
4586 void
4587 riscv_pop_insert (void)
4589 extern void pop_insert (const pseudo_typeS *);
4591 pop_insert (riscv_pseudo_table);