]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-riscv.c
RISC-V: minor effort reduction in relocation specifier parsing
[thirdparty/binutils-gdb.git] / gas / config / tc-riscv.c
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
3
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
6
7 This file is part of GAS.
8
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.
13
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.
18
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/>. */
22
23 #include "as.h"
24 #include "config.h"
25 #include "subsegs.h"
26 #include "safe-ctype.h"
27
28 #include "itbl-ops.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
31
32 #include "bfd/elfxx-riscv.h"
33 #include "elf/riscv.h"
34 #include "opcode/riscv.h"
35
36 #include <stdint.h>
37
38 /* Information about an instruction, including its format, operands
39 and fixups. */
40 struct riscv_cl_insn
41 {
42 /* The opcode's entry in riscv_opcodes. */
43 const struct riscv_opcode *insn_mo;
44
45 /* The encoded instruction bits
46 (first bits enough to extract instruction length on a long opcode). */
47 insn_t insn_opcode;
48
49 /* The long encoded instruction bits ([0] is non-zero on a long opcode). */
50 char insn_long_opcode[RISCV_MAX_INSN_LEN];
51
52 /* The frag that contains the instruction. */
53 struct frag *frag;
54
55 /* The offset into FRAG of the first instruction byte. */
56 long where;
57
58 /* The relocs associated with the instruction, if any. */
59 fixS *fixp;
60 };
61
62 /* All RISC-V CSR belong to one of these classes. */
63 enum riscv_csr_class
64 {
65 CSR_CLASS_NONE,
66
67 CSR_CLASS_I,
68 CSR_CLASS_I_32, /* rv32 only */
69 CSR_CLASS_F, /* f-ext only */
70 CSR_CLASS_ZKR, /* zkr only */
71 CSR_CLASS_V, /* rvv only */
72 CSR_CLASS_DEBUG, /* debug CSR */
73 CSR_CLASS_H, /* hypervisor */
74 CSR_CLASS_H_32, /* hypervisor, rv32 only */
75 CSR_CLASS_SMAIA, /* Smaia */
76 CSR_CLASS_SMAIA_32, /* Smaia, rv32 only */
77 CSR_CLASS_SMSTATEEN, /* Smstateen only */
78 CSR_CLASS_SMSTATEEN_32, /* Smstateen RV32 only */
79 CSR_CLASS_SSAIA, /* Ssaia */
80 CSR_CLASS_SSAIA_AND_H, /* Ssaia with H */
81 CSR_CLASS_SSAIA_32, /* Ssaia, rv32 only */
82 CSR_CLASS_SSAIA_AND_H_32, /* Ssaia with H, rv32 only */
83 CSR_CLASS_SSSTATEEN, /* S[ms]stateen only */
84 CSR_CLASS_SSSTATEEN_AND_H, /* S[ms]stateen only (with H) */
85 CSR_CLASS_SSSTATEEN_AND_H_32, /* S[ms]stateen RV32 only (with H) */
86 CSR_CLASS_SSCOFPMF, /* Sscofpmf only */
87 CSR_CLASS_SSCOFPMF_32, /* Sscofpmf RV32 only */
88 CSR_CLASS_SSTC, /* Sstc only */
89 CSR_CLASS_SSTC_AND_H, /* Sstc only (with H) */
90 CSR_CLASS_SSTC_32, /* Sstc RV32 only */
91 CSR_CLASS_SSTC_AND_H_32, /* Sstc RV32 only (with H) */
92 };
93
94 /* This structure holds all restricted conditions for a CSR. */
95 struct riscv_csr_extra
96 {
97 /* Class to which this CSR belongs. Used to decide whether or
98 not this CSR is legal in the current -march context. */
99 enum riscv_csr_class csr_class;
100
101 /* CSR may have differnet numbers in the previous priv spec. */
102 unsigned address;
103
104 /* Record the CSR is defined/valid in which versions. */
105 enum riscv_spec_class define_version;
106
107 /* Record the CSR is aborted/invalid from which versions. If it isn't
108 aborted in the current version, then it should be PRIV_SPEC_CLASS_DRAFT. */
109 enum riscv_spec_class abort_version;
110
111 /* The CSR may have more than one setting. */
112 struct riscv_csr_extra *next;
113 };
114
115 /* This structure contains information about errors that occur within the
116 riscv_ip function */
117 struct riscv_ip_error
118 {
119 /* General error message */
120 const char* msg;
121
122 /* Statement that caused the error */
123 char* statement;
124
125 /* Missing extension that needs to be enabled */
126 const char* missing_ext;
127 };
128
129 #ifndef DEFAULT_ARCH
130 #define DEFAULT_ARCH "riscv64"
131 #endif
132
133 #ifndef DEFAULT_RISCV_ATTR
134 #define DEFAULT_RISCV_ATTR 0
135 #endif
136
137 /* Let riscv_after_parse_args set the default value according to xlen. */
138 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
139 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
140 #endif
141
142 /* Need to sync the version with RISC-V compiler. */
143 #ifndef DEFAULT_RISCV_ISA_SPEC
144 #define DEFAULT_RISCV_ISA_SPEC "20191213"
145 #endif
146
147 #ifndef DEFAULT_RISCV_PRIV_SPEC
148 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
149 #endif
150
151 static const char default_arch[] = DEFAULT_ARCH;
152 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
153 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
154 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
155
156 static unsigned xlen = 0; /* The width of an x-register. */
157 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
158 static bool rve_abi = false;
159 enum float_abi
160 {
161 FLOAT_ABI_DEFAULT = -1,
162 FLOAT_ABI_SOFT,
163 FLOAT_ABI_SINGLE,
164 FLOAT_ABI_DOUBLE,
165 FLOAT_ABI_QUAD
166 };
167 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
168
169 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
170 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
171
172 static unsigned elf_flags = 0;
173
174 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
175 Otherwise, return 1. */
176
177 static int
178 riscv_set_default_isa_spec (const char *s)
179 {
180 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
181 RISCV_GET_ISA_SPEC_CLASS (s, class);
182 if (class == ISA_SPEC_CLASS_NONE)
183 {
184 as_bad ("unknown default ISA spec `%s' set by "
185 "-misa-spec or --with-isa-spec", s);
186 return 0;
187 }
188 else
189 default_isa_spec = class;
190 return 1;
191 }
192
193 /* Set the default_priv_spec. Find the privileged elf attributes when
194 the input string is NULL. Return 0 if the spec isn't supported.
195 Otherwise, return 1. */
196
197 static int
198 riscv_set_default_priv_spec (const char *s)
199 {
200 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
201 unsigned major, minor, revision;
202 obj_attribute *attr;
203
204 RISCV_GET_PRIV_SPEC_CLASS (s, class);
205 if (class != PRIV_SPEC_CLASS_NONE)
206 {
207 default_priv_spec = class;
208 return 1;
209 }
210
211 if (s != NULL)
212 {
213 as_bad (_("unknown default privileged spec `%s' set by "
214 "-mpriv-spec or --with-priv-spec"), s);
215 return 0;
216 }
217
218 /* Set the default_priv_spec by the privileged elf attributes. */
219 attr = elf_known_obj_attributes_proc (stdoutput);
220 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
221 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
222 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
223 /* Version 0.0.0 is the default value and meningless. */
224 if (major == 0 && minor == 0 && revision == 0)
225 return 1;
226
227 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
228 if (class != PRIV_SPEC_CLASS_NONE)
229 {
230 default_priv_spec = class;
231 return 1;
232 }
233
234 /* Still can not find the privileged spec class. */
235 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
236 "privileged elf attributes"), major, minor, revision);
237 return 0;
238 }
239
240 /* This is the set of options which the .option pseudo-op may modify. */
241 struct riscv_set_options
242 {
243 int pic; /* Generate position-independent code. */
244 int rvc; /* Generate RVC code. */
245 int relax; /* Emit relocs the linker is allowed to relax. */
246 int arch_attr; /* Emit architecture and privileged elf attributes. */
247 int csr_check; /* Enable the CSR checking. */
248 };
249
250 static struct riscv_set_options riscv_opts =
251 {
252 0, /* pic */
253 0, /* rvc */
254 1, /* relax */
255 DEFAULT_RISCV_ATTR, /* arch_attr */
256 0, /* csr_check */
257 };
258
259 /* Enable or disable the rvc flags for riscv_opts. Turn on the rvc flag
260 for elf_flags once we have enabled c extension. */
261
262 static void
263 riscv_set_rvc (bool rvc_value)
264 {
265 if (rvc_value)
266 elf_flags |= EF_RISCV_RVC;
267
268 riscv_opts.rvc = rvc_value;
269 }
270
271 /* Turn on the tso flag for elf_flags once we have enabled ztso extension. */
272
273 static void
274 riscv_set_tso (void)
275 {
276 elf_flags |= EF_RISCV_TSO;
277 }
278
279 /* The linked list hanging off of .subsets_list records all enabled extensions,
280 which are parsed from the architecture string. The architecture string can
281 be set by the -march option, the elf architecture attributes, and the
282 --with-arch configure option. */
283 static riscv_parse_subset_t riscv_rps_as =
284 {
285 NULL, /* subset_list, we will set it later once
286 riscv_opts_stack is created or updated. */
287 as_bad, /* error_handler. */
288 &xlen, /* xlen. */
289 &default_isa_spec, /* isa_spec. */
290 true, /* check_unknown_prefixed_ext. */
291 };
292
293 /* Update the architecture string in the subset_list. */
294
295 static void
296 riscv_reset_subsets_list_arch_str (void)
297 {
298 riscv_subset_list_t *subsets = riscv_rps_as.subset_list;
299 if (subsets->arch_str != NULL)
300 free ((void *) subsets->arch_str);
301 subsets->arch_str = riscv_arch_str (xlen, subsets);
302 }
303
304 /* This structure is used to hold a stack of .option values. */
305 struct riscv_option_stack
306 {
307 struct riscv_option_stack *next;
308 struct riscv_set_options options;
309 riscv_subset_list_t *subset_list;
310 };
311
312 static struct riscv_option_stack *riscv_opts_stack = NULL;
313
314 /* Set which ISA and extensions are available. */
315
316 static void
317 riscv_set_arch (const char *s)
318 {
319 if (s != NULL && strcmp (s, "") == 0)
320 {
321 as_bad (_("the architecture string of -march and elf architecture "
322 "attributes cannot be empty"));
323 return;
324 }
325
326 if (riscv_rps_as.subset_list == NULL)
327 {
328 riscv_rps_as.subset_list = XNEW (riscv_subset_list_t);
329 riscv_rps_as.subset_list->head = NULL;
330 riscv_rps_as.subset_list->tail = NULL;
331 riscv_rps_as.subset_list->arch_str = NULL;
332 }
333 riscv_release_subset_list (riscv_rps_as.subset_list);
334 riscv_parse_subset (&riscv_rps_as, s);
335 riscv_reset_subsets_list_arch_str ();
336
337 riscv_set_rvc (false);
338 if (riscv_subset_supports (&riscv_rps_as, "c"))
339 riscv_set_rvc (true);
340
341 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
342 riscv_set_tso ();
343 }
344
345 /* Indicate -mabi option is explictly set. */
346 static bool explicit_mabi = false;
347
348 /* Set the abi information. */
349
350 static void
351 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bool rve)
352 {
353 abi_xlen = new_xlen;
354 float_abi = new_float_abi;
355 rve_abi = rve;
356 }
357
358 /* If the -mabi option isn't set, then set the abi according to the
359 ISA string. Otherwise, check if there is any conflict. */
360
361 static void
362 riscv_set_abi_by_arch (void)
363 {
364 if (!explicit_mabi)
365 {
366 if (riscv_subset_supports (&riscv_rps_as, "q"))
367 riscv_set_abi (xlen, FLOAT_ABI_QUAD, false);
368 else if (riscv_subset_supports (&riscv_rps_as, "d"))
369 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, false);
370 else if (riscv_subset_supports (&riscv_rps_as, "e"))
371 riscv_set_abi (xlen, FLOAT_ABI_SOFT, true);
372 else
373 riscv_set_abi (xlen, FLOAT_ABI_SOFT, false);
374 }
375 else
376 {
377 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
378 if (abi_xlen > xlen)
379 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
380 else if (abi_xlen < xlen)
381 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
382
383 if (riscv_subset_supports (&riscv_rps_as, "e") && !rve_abi)
384 as_bad ("only the ilp32e ABI is supported for e extension");
385
386 if (float_abi == FLOAT_ABI_SINGLE
387 && !riscv_subset_supports (&riscv_rps_as, "f"))
388 as_bad ("ilp32f/lp64f ABI can't be used when f extension "
389 "isn't supported");
390 else if (float_abi == FLOAT_ABI_DOUBLE
391 && !riscv_subset_supports (&riscv_rps_as, "d"))
392 as_bad ("ilp32d/lp64d ABI can't be used when d extension "
393 "isn't supported");
394 else if (float_abi == FLOAT_ABI_QUAD
395 && !riscv_subset_supports (&riscv_rps_as, "q"))
396 as_bad ("ilp32q/lp64q ABI can't be used when q extension "
397 "isn't supported");
398 }
399
400 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
401 elf_flags &= ~EF_RISCV_FLOAT_ABI;
402 elf_flags |= float_abi << 1;
403
404 if (rve_abi)
405 elf_flags |= EF_RISCV_RVE;
406 }
407
408 /* Handle of the OPCODE hash table. */
409 static htab_t op_hash = NULL;
410
411 /* Handle of the type of .insn hash table. */
412 static htab_t insn_type_hash = NULL;
413
414 /* This array holds the chars that always start a comment. If the
415 pre-processor is disabled, these aren't very useful. */
416 const char comment_chars[] = "#";
417
418 /* This array holds the chars that only start a comment at the beginning of
419 a line. If the line seems to have the form '# 123 filename'
420 .line and .file directives will appear in the pre-processed output
421
422 Note that input_file.c hand checks for '#' at the beginning of the
423 first line of the input file. This is because the compiler outputs
424 #NO_APP at the beginning of its output.
425
426 Also note that C style comments are always supported. */
427 const char line_comment_chars[] = "#";
428
429 /* This array holds machine specific line separator characters. */
430 const char line_separator_chars[] = ";";
431
432 /* Chars that can be used to separate mant from exp in floating point nums. */
433 const char EXP_CHARS[] = "eE";
434
435 /* Chars that mean this number is a floating point constant.
436 As in 0f12.456 or 0d1.2345e12. */
437 const char FLT_CHARS[] = "rRsSfFdDxXpPhH";
438
439 /* Indicate we are already assemble any instructions or not. */
440 static bool start_assemble = false;
441
442 /* Indicate ELF attributes are explicitly set. */
443 static bool explicit_attr = false;
444
445 /* Indicate CSR or priv instructions are explicitly used. */
446 static bool explicit_priv_attr = false;
447
448 static char *expr_parse_end;
449
450 /* Macros for encoding relaxation state for RVC branches and far jumps. */
451 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
452 ((relax_substateT) \
453 (0xc0000000 \
454 | ((uncond) ? 1 : 0) \
455 | ((rvc) ? 2 : 0) \
456 | ((length) << 2)))
457 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
458 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
459 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
460 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
461
462 /* Is the given value a sign-extended 32-bit value? */
463 #define IS_SEXT_32BIT_NUM(x) \
464 (((x) &~ (offsetT) 0x7fffffff) == 0 \
465 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
466
467 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
468 #define IS_ZEXT_32BIT_NUM(x) \
469 (((x) &~ (offsetT) 0xffffffff) == 0 \
470 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
471
472 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
473 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
474 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
475 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
476
477 #define INSERT_IMM(n, s, INSN, VALUE) \
478 INSERT_BITS ((INSN).insn_opcode, VALUE, (1ULL<<n) - 1, s)
479
480 /* Determine if an instruction matches an opcode. */
481 #define OPCODE_MATCHES(OPCODE, OP) \
482 (((OPCODE) & MASK_##OP) == MATCH_##OP)
483
484 /* Create a new mapping symbol for the transition to STATE. */
485
486 static void
487 make_mapping_symbol (enum riscv_seg_mstate state,
488 valueT value,
489 fragS *frag,
490 const char *arch_str,
491 bool odd_data_padding)
492 {
493 const char *name;
494 char *buff = NULL;
495 switch (state)
496 {
497 case MAP_DATA:
498 name = "$d";
499 break;
500 case MAP_INSN:
501 if (arch_str != NULL)
502 {
503 size_t size = strlen (arch_str) + 3; /* "$x" + '\0' */
504 buff = xmalloc (size);
505 snprintf (buff, size, "$x%s", arch_str);
506 name = buff;
507 }
508 else
509 name = "$x";
510 break;
511 default:
512 abort ();
513 }
514
515 symbolS *symbol = symbol_new (name, now_seg, frag, value);
516 symbol_get_bfdsym (symbol)->flags |= (BSF_NO_FLAGS | BSF_LOCAL);
517 if (arch_str != NULL)
518 {
519 /* Store current $x+arch into tc_segment_info. */
520 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol = symbol;
521 xfree ((void *) buff);
522 }
523
524 /* If .fill or other data filling directive generates zero sized data,
525 then mapping symbol for the following code will have the same value.
526
527 Please see gas/testsuite/gas/riscv/mapping.s: .text.zero.fill.first
528 and .text.zero.fill.last. */
529 symbolS *first = frag->tc_frag_data.first_map_symbol;
530 symbolS *last = frag->tc_frag_data.last_map_symbol;
531 symbolS *removed = NULL;
532 if (value == 0)
533 {
534 if (first != NULL)
535 {
536 know (S_GET_VALUE (first) == S_GET_VALUE (symbol)
537 && first == last);
538 /* Remove the old one. */
539 removed = first;
540 }
541 frag->tc_frag_data.first_map_symbol = symbol;
542 }
543 else if (last != NULL)
544 {
545 /* The mapping symbols should be added in offset order. */
546 know (S_GET_VALUE (last) <= S_GET_VALUE (symbol));
547 /* Remove the old one. */
548 if (S_GET_VALUE (last) == S_GET_VALUE (symbol))
549 removed = last;
550 }
551 frag->tc_frag_data.last_map_symbol = symbol;
552
553 if (removed == NULL)
554 return;
555
556 if (odd_data_padding)
557 {
558 /* If the removed mapping symbol is $x+arch, then add it back to
559 the next $x. */
560 const char *str = strncmp (S_GET_NAME (removed), "$xrv", 4) == 0
561 ? S_GET_NAME (removed) + 2 : NULL;
562 make_mapping_symbol (MAP_INSN, frag->fr_fix + 1, frag, str,
563 false/* odd_data_padding */);
564 }
565 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
566 }
567
568 /* Set the mapping state for frag_now. */
569
570 void
571 riscv_mapping_state (enum riscv_seg_mstate to_state,
572 int max_chars,
573 bool fr_align_code)
574 {
575 enum riscv_seg_mstate from_state =
576 seg_info (now_seg)->tc_segment_info_data.map_state;
577 bool reset_seg_arch_str = false;
578
579 if (!SEG_NORMAL (now_seg)
580 /* For now we only add the mapping symbols to text sections.
581 Therefore, the dis-assembler only show the actual contents
582 distribution for text. Other sections will be shown as
583 data without the details. */
584 || !subseg_text_p (now_seg))
585 return;
586
587 /* The mapping symbol should be emitted if not in the right
588 mapping state. */
589 symbolS *seg_arch_symbol =
590 seg_info (now_seg)->tc_segment_info_data.arch_map_symbol;
591 if (to_state == MAP_INSN && seg_arch_symbol == 0)
592 {
593 /* Always add $x+arch at the first instruction of section. */
594 reset_seg_arch_str = true;
595 }
596 else if (seg_arch_symbol != 0
597 && to_state == MAP_INSN
598 && !fr_align_code
599 && strcmp (riscv_rps_as.subset_list->arch_str,
600 S_GET_NAME (seg_arch_symbol) + 2) != 0)
601 {
602 reset_seg_arch_str = true;
603 }
604 else if (from_state == to_state)
605 return;
606
607 valueT value = (valueT) (frag_now_fix () - max_chars);
608 seg_info (now_seg)->tc_segment_info_data.map_state = to_state;
609 const char *arch_str = reset_seg_arch_str
610 ? riscv_rps_as.subset_list->arch_str : NULL;
611 make_mapping_symbol (to_state, value, frag_now, arch_str,
612 false/* odd_data_padding */);
613 }
614
615 /* Add the odd bytes of paddings for riscv_handle_align. */
616
617 static void
618 riscv_add_odd_padding_symbol (fragS *frag)
619 {
620 /* If there was already a mapping symbol, it should be
621 removed in the make_mapping_symbol.
622
623 Please see gas/testsuite/gas/riscv/mapping.s: .text.odd.align.*. */
624 make_mapping_symbol (MAP_DATA, frag->fr_fix, frag,
625 NULL/* arch_str */, true/* odd_data_padding */);
626 }
627
628 /* Remove any excess mapping symbols generated for alignment frags in
629 SEC. We may have created a mapping symbol before a zero byte
630 alignment; remove it if there's a mapping symbol after the
631 alignment. */
632
633 static void
634 riscv_check_mapping_symbols (bfd *abfd ATTRIBUTE_UNUSED,
635 asection *sec,
636 void *dummy ATTRIBUTE_UNUSED)
637 {
638 segment_info_type *seginfo = seg_info (sec);
639 fragS *fragp;
640
641 if (seginfo == NULL || seginfo->frchainP == NULL)
642 return;
643
644 for (fragp = seginfo->frchainP->frch_root;
645 fragp != NULL;
646 fragp = fragp->fr_next)
647 {
648 symbolS *last = fragp->tc_frag_data.last_map_symbol;
649 fragS *next = fragp->fr_next;
650
651 if (last == NULL || next == NULL)
652 continue;
653
654 /* Check the last mapping symbol if it is at the boundary of
655 fragment. */
656 if (S_GET_VALUE (last) < next->fr_address)
657 continue;
658 know (S_GET_VALUE (last) == next->fr_address);
659
660 do
661 {
662 symbolS *next_first = next->tc_frag_data.first_map_symbol;
663 if (next_first != NULL)
664 {
665 /* The last mapping symbol overlaps with another one
666 which at the start of the next frag.
667
668 Please see the gas/testsuite/gas/riscv/mapping.s:
669 .text.zero.fill.align.A and .text.zero.fill.align.B. */
670 know (S_GET_VALUE (last) == S_GET_VALUE (next_first));
671 symbolS *removed = last;
672 if (strncmp (S_GET_NAME (last), "$xrv", 4) == 0
673 && strcmp (S_GET_NAME (next_first), "$x") == 0)
674 removed = next_first;
675 symbol_remove (removed, &symbol_rootP, &symbol_lastP);
676 break;
677 }
678
679 if (next->fr_next == NULL)
680 {
681 /* The last mapping symbol is at the end of the section.
682
683 Please see the gas/testsuite/gas/riscv/mapping.s:
684 .text.last.section. */
685 know (next->fr_fix == 0 && next->fr_var == 0);
686 symbol_remove (last, &symbol_rootP, &symbol_lastP);
687 break;
688 }
689
690 /* Since we may have empty frags without any mapping symbols,
691 keep looking until the non-empty frag. */
692 if (next->fr_address != next->fr_next->fr_address)
693 break;
694
695 next = next->fr_next;
696 }
697 while (next != NULL);
698 }
699 }
700
701 /* The default target format to use. */
702
703 const char *
704 riscv_target_format (void)
705 {
706 if (target_big_endian)
707 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
708 else
709 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
710 }
711
712 /* Return the length of instruction INSN. */
713
714 static inline unsigned int
715 insn_length (const struct riscv_cl_insn *insn)
716 {
717 return riscv_insn_length (insn->insn_opcode);
718 }
719
720 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
721
722 static void
723 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
724 {
725 insn->insn_mo = mo;
726 insn->insn_opcode = mo->match;
727 insn->insn_long_opcode[0] = 0;
728 insn->frag = NULL;
729 insn->where = 0;
730 insn->fixp = NULL;
731 }
732
733 /* Install INSN at the location specified by its "frag" and "where" fields. */
734
735 static void
736 install_insn (const struct riscv_cl_insn *insn)
737 {
738 char *f = insn->frag->fr_literal + insn->where;
739 if (insn->insn_long_opcode[0] != 0)
740 memcpy (f, insn->insn_long_opcode, insn_length (insn));
741 else
742 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
743 }
744
745 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
746 and install the opcode in the new location. */
747
748 static void
749 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
750 {
751 insn->frag = frag;
752 insn->where = where;
753 if (insn->fixp != NULL)
754 {
755 insn->fixp->fx_frag = frag;
756 insn->fixp->fx_where = where;
757 }
758 install_insn (insn);
759 }
760
761 /* Add INSN to the end of the output. */
762
763 static void
764 add_fixed_insn (struct riscv_cl_insn *insn)
765 {
766 char *f = frag_more (insn_length (insn));
767 move_insn (insn, frag_now, f - frag_now->fr_literal);
768 }
769
770 static void
771 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
772 relax_substateT subtype, symbolS *symbol, offsetT offset)
773 {
774 frag_grow (max_chars);
775 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
776 frag_var (rs_machine_dependent, max_chars, var,
777 subtype, symbol, offset, NULL);
778 }
779
780 /* Compute the length of a branch sequence, and adjust the stored length
781 accordingly. If FRAGP is NULL, the worst-case length is returned. */
782
783 static unsigned
784 relaxed_branch_length (fragS *fragp, asection *sec, int update)
785 {
786 int jump, rvc, length = 8;
787
788 if (!fragp)
789 return length;
790
791 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
792 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
793 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
794
795 /* Assume jumps are in range; the linker will catch any that aren't. */
796 length = jump ? 4 : 8;
797
798 if (fragp->fr_symbol != NULL
799 && S_IS_DEFINED (fragp->fr_symbol)
800 && !S_IS_WEAK (fragp->fr_symbol)
801 && sec == S_GET_SEGMENT (fragp->fr_symbol))
802 {
803 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
804 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
805 val -= fragp->fr_address + fragp->fr_fix;
806
807 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
808 length = 2;
809 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
810 length = 4;
811 else if (!jump && rvc)
812 length = 6;
813 }
814
815 if (update)
816 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
817
818 return length;
819 }
820
821 /* Information about an opcode name, mnemonics and its value. */
822 struct opcode_name_t
823 {
824 const char *name;
825 unsigned int val;
826 };
827
828 /* List for all supported opcode name. */
829 static const struct opcode_name_t opcode_name_list[] =
830 {
831 {"C0", 0x0},
832 {"C1", 0x1},
833 {"C2", 0x2},
834
835 {"LOAD", 0x03},
836 {"LOAD_FP", 0x07},
837 {"CUSTOM_0", 0x0b},
838 {"MISC_MEM", 0x0f},
839 {"OP_IMM", 0x13},
840 {"AUIPC", 0x17},
841 {"OP_IMM_32", 0x1b},
842 /* 48b 0x1f. */
843
844 {"STORE", 0x23},
845 {"STORE_FP", 0x27},
846 {"CUSTOM_1", 0x2b},
847 {"AMO", 0x2f},
848 {"OP", 0x33},
849 {"LUI", 0x37},
850 {"OP_32", 0x3b},
851 /* 64b 0x3f. */
852
853 {"MADD", 0x43},
854 {"MSUB", 0x47},
855 {"NMADD", 0x4f},
856 {"NMSUB", 0x4b},
857 {"OP_FP", 0x53},
858 {"OP_V", 0x57},
859 {"CUSTOM_2", 0x5b},
860 /* 48b 0x5f. */
861
862 {"BRANCH", 0x63},
863 {"JALR", 0x67},
864 /*reserved 0x5b. */
865 {"JAL", 0x6f},
866 {"SYSTEM", 0x73},
867 /*reserved 0x77. */
868 {"CUSTOM_3", 0x7b},
869 /* >80b 0x7f. */
870
871 {NULL, 0}
872 };
873
874 /* Hash table for lookup opcode name. */
875 static htab_t opcode_names_hash = NULL;
876
877 /* Initialization for hash table of opcode name. */
878
879 static void
880 init_opcode_names_hash (void)
881 {
882 const struct opcode_name_t *opcode;
883
884 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
885 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
886 as_fatal (_("internal: duplicate %s"), opcode->name);
887 }
888
889 /* Find `s` is a valid opcode name or not, return the opcode name info
890 if found. */
891
892 static const struct opcode_name_t *
893 opcode_name_lookup (char **s)
894 {
895 char *e;
896 char save_c;
897 struct opcode_name_t *o;
898
899 /* Find end of name. */
900 e = *s;
901 if (is_name_beginner (*e))
902 ++e;
903 while (is_part_of_name (*e))
904 ++e;
905
906 /* Terminate name. */
907 save_c = *e;
908 *e = '\0';
909
910 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
911
912 /* Advance to next token if one was recognized. */
913 if (o)
914 *s = e;
915
916 *e = save_c;
917 expr_parse_end = e;
918
919 return o;
920 }
921
922 /* All RISC-V registers belong to one of these classes. */
923 enum reg_class
924 {
925 RCLASS_GPR,
926 RCLASS_FPR,
927 RCLASS_VECR,
928 RCLASS_VECM,
929 RCLASS_MAX,
930
931 RCLASS_CSR
932 };
933
934 static htab_t reg_names_hash = NULL;
935 static htab_t csr_extra_hash = NULL;
936
937 #define ENCODE_REG_HASH(cls, n) \
938 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
939 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
940 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
941
942 static void
943 hash_reg_name (enum reg_class class, const char *name, unsigned n)
944 {
945 void *hash = ENCODE_REG_HASH (class, n);
946 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
947 as_fatal (_("internal: duplicate %s"), name);
948 }
949
950 static void
951 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
952 {
953 unsigned i;
954
955 for (i = 0; i < n; i++)
956 hash_reg_name (class, names[i], i);
957 }
958
959 /* Init hash table csr_extra_hash to handle CSR. */
960
961 static void
962 riscv_init_csr_hash (const char *name,
963 unsigned address,
964 enum riscv_csr_class class,
965 enum riscv_spec_class define_version,
966 enum riscv_spec_class abort_version)
967 {
968 struct riscv_csr_extra *entry, *pre_entry;
969 bool need_enrty = true;
970
971 pre_entry = NULL;
972 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
973 while (need_enrty && entry != NULL)
974 {
975 if (entry->csr_class == class
976 && entry->address == address
977 && entry->define_version == define_version
978 && entry->abort_version == abort_version)
979 need_enrty = false;
980 pre_entry = entry;
981 entry = entry->next;
982 }
983
984 /* Duplicate CSR. */
985 if (!need_enrty)
986 return;
987
988 entry = notes_alloc (sizeof (*entry));
989 entry->csr_class = class;
990 entry->address = address;
991 entry->define_version = define_version;
992 entry->abort_version = abort_version;
993 entry->next = NULL;
994
995 if (pre_entry == NULL)
996 str_hash_insert (csr_extra_hash, name, entry, 0);
997 else
998 pre_entry->next = entry;
999 }
1000
1001 /* Return the CSR address after checking the ISA dependency and
1002 the privileged spec version.
1003
1004 There are one warning and two errors for CSR,
1005
1006 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
1007 or the privileged spec, report warning only if -mcsr-check is set.
1008 Unknown CSR: the CSR has never been defined, report error.
1009 Improper CSR: the CSR number over the range (> 0xfff), report error. */
1010
1011 static unsigned int
1012 riscv_csr_address (const char *csr_name,
1013 struct riscv_csr_extra *entry)
1014 {
1015 struct riscv_csr_extra *saved_entry = entry;
1016 enum riscv_csr_class csr_class = entry->csr_class;
1017 bool need_check_version = false;
1018 bool is_rv32_only = false;
1019 bool is_h_required = false;
1020 const char* extension = NULL;
1021
1022 switch (csr_class)
1023 {
1024 case CSR_CLASS_I_32:
1025 is_rv32_only = true;
1026 /* Fall through. */
1027 case CSR_CLASS_I:
1028 need_check_version = true;
1029 extension = "i";
1030 break;
1031 case CSR_CLASS_H_32:
1032 is_rv32_only = true;
1033 /* Fall through. */
1034 case CSR_CLASS_H:
1035 extension = "h";
1036 break;
1037 case CSR_CLASS_F:
1038 extension = "f";
1039 break;
1040 case CSR_CLASS_ZKR:
1041 extension = "zkr";
1042 break;
1043 case CSR_CLASS_V:
1044 extension = "zve32x";
1045 break;
1046 case CSR_CLASS_SMAIA_32:
1047 is_rv32_only = true;
1048 /* Fall through. */
1049 case CSR_CLASS_SMAIA:
1050 extension = "smaia";
1051 break;
1052 case CSR_CLASS_SMSTATEEN_32:
1053 is_rv32_only = true;
1054 /* Fall through. */
1055 case CSR_CLASS_SMSTATEEN:
1056 extension = "smstateen";
1057 break;
1058 case CSR_CLASS_SSAIA:
1059 case CSR_CLASS_SSAIA_AND_H:
1060 case CSR_CLASS_SSAIA_32:
1061 case CSR_CLASS_SSAIA_AND_H_32:
1062 is_rv32_only = (csr_class == CSR_CLASS_SSAIA_32
1063 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1064 is_h_required = (csr_class == CSR_CLASS_SSAIA_AND_H
1065 || csr_class == CSR_CLASS_SSAIA_AND_H_32);
1066 extension = "ssaia";
1067 break;
1068 case CSR_CLASS_SSSTATEEN_AND_H_32:
1069 is_rv32_only = true;
1070 /* Fall through. */
1071 case CSR_CLASS_SSSTATEEN_AND_H:
1072 is_h_required = true;
1073 /* Fall through. */
1074 case CSR_CLASS_SSSTATEEN:
1075 extension = "ssstateen";
1076 break;
1077 case CSR_CLASS_SSCOFPMF_32:
1078 is_rv32_only = true;
1079 /* Fall through. */
1080 case CSR_CLASS_SSCOFPMF:
1081 extension = "sscofpmf";
1082 break;
1083 case CSR_CLASS_SSTC:
1084 case CSR_CLASS_SSTC_AND_H:
1085 case CSR_CLASS_SSTC_32:
1086 case CSR_CLASS_SSTC_AND_H_32:
1087 is_rv32_only = (csr_class == CSR_CLASS_SSTC_32
1088 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1089 is_h_required = (csr_class == CSR_CLASS_SSTC_AND_H
1090 || csr_class == CSR_CLASS_SSTC_AND_H_32);
1091 extension = "sstc";
1092 break;
1093 case CSR_CLASS_DEBUG:
1094 break;
1095 default:
1096 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
1097 }
1098
1099 if (riscv_opts.csr_check)
1100 {
1101 if (is_rv32_only && xlen != 32)
1102 as_warn (_("invalid CSR `%s', needs rv32i extension"), csr_name);
1103 if (is_h_required && !riscv_subset_supports (&riscv_rps_as, "h"))
1104 as_warn (_("invalid CSR `%s', needs `h' extension"), csr_name);
1105
1106 if (extension != NULL
1107 && !riscv_subset_supports (&riscv_rps_as, extension))
1108 as_warn (_("invalid CSR `%s', needs `%s' extension"),
1109 csr_name, extension);
1110 }
1111
1112 while (entry != NULL)
1113 {
1114 if (!need_check_version
1115 || (default_priv_spec >= entry->define_version
1116 && default_priv_spec < entry->abort_version))
1117 {
1118 /* Find the CSR according to the specific version. */
1119 return entry->address;
1120 }
1121 entry = entry->next;
1122 }
1123
1124 /* Can not find the CSR address from the chosen privileged version,
1125 so use the newly defined value. */
1126 if (riscv_opts.csr_check)
1127 {
1128 const char *priv_name = NULL;
1129 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
1130 if (priv_name != NULL)
1131 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
1132 csr_name, priv_name);
1133 }
1134
1135 return saved_entry->address;
1136 }
1137
1138 /* Return -1 if the CSR has never been defined. Otherwise, return
1139 the address. */
1140
1141 static unsigned int
1142 reg_csr_lookup_internal (const char *s)
1143 {
1144 struct riscv_csr_extra *r =
1145 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
1146
1147 if (r == NULL)
1148 return -1U;
1149
1150 return riscv_csr_address (s, r);
1151 }
1152
1153 static unsigned int
1154 reg_lookup_internal (const char *s, enum reg_class class)
1155 {
1156 void *r;
1157
1158 if (class == RCLASS_CSR)
1159 return reg_csr_lookup_internal (s);
1160
1161 r = str_hash_find (reg_names_hash, s);
1162 if (r == NULL || DECODE_REG_CLASS (r) != class)
1163 return -1;
1164
1165 if (riscv_subset_supports (&riscv_rps_as, "e")
1166 && class == RCLASS_GPR
1167 && DECODE_REG_NUM (r) > 15)
1168 return -1;
1169
1170 return DECODE_REG_NUM (r);
1171 }
1172
1173 static bool
1174 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
1175 {
1176 char *e;
1177 char save_c;
1178 int reg = -1;
1179
1180 /* Find end of name. */
1181 e = *s;
1182 if (is_name_beginner (*e))
1183 ++e;
1184 while (is_part_of_name (*e))
1185 ++e;
1186
1187 /* Terminate name. */
1188 save_c = *e;
1189 *e = '\0';
1190
1191 /* Look for the register. Advance to next token if one was recognized. */
1192 if ((reg = reg_lookup_internal (*s, class)) >= 0)
1193 *s = e;
1194
1195 *e = save_c;
1196 if (regnop)
1197 *regnop = reg;
1198 return reg >= 0;
1199 }
1200
1201 static bool
1202 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
1203 {
1204 const char *p = strchr (*s, ',');
1205 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
1206
1207 if (len == 0)
1208 return false;
1209
1210 for (i = 0; i < size; i++)
1211 if (array[i] != NULL && strncmp (array[i], *s, len) == 0
1212 && array[i][len] == '\0')
1213 {
1214 *regnop = i;
1215 *s += len;
1216 return true;
1217 }
1218
1219 return false;
1220 }
1221
1222 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
1223 #define USE_IMM(n, s) \
1224 (used_bits |= ((insn_t)((1ull<<n)-1) << (s)))
1225
1226 /* For consistency checking, verify that all bits are specified either
1227 by the match/mask part of the instruction definition, or by the
1228 operand list. The `length` could be the actual instruction length or
1229 0 for auto-detection. */
1230
1231 static bool
1232 validate_riscv_insn (const struct riscv_opcode *opc, int length)
1233 {
1234 const char *oparg, *opargStart;
1235 insn_t used_bits = opc->mask;
1236 int insn_width;
1237 insn_t required_bits;
1238
1239 if (length == 0)
1240 length = riscv_insn_length (opc->match);
1241 /* We don't support instructions longer than 64-bits yet. */
1242 if (length > 8)
1243 length = 8;
1244 insn_width = 8 * length;
1245
1246 required_bits = ((insn_t)~0ULL) >> (64 - insn_width);
1247
1248 if ((used_bits & opc->match) != (opc->match & required_bits))
1249 {
1250 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
1251 opc->name, opc->args);
1252 return false;
1253 }
1254
1255 for (oparg = opc->args; *oparg; ++oparg)
1256 {
1257 opargStart = oparg;
1258 switch (*oparg)
1259 {
1260 case 'C': /* RVC */
1261 switch (*++oparg)
1262 {
1263 case 'U': break; /* CRS1, constrained to equal RD. */
1264 case 'c': break; /* CRS1, constrained to equal sp. */
1265 case 'T': /* CRS2, floating point. */
1266 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1267 case 'S': /* CRS1S, floating point. */
1268 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1269 case 'w': break; /* CRS1S, constrained to equal RD. */
1270 case 'D': /* CRS2S, floating point. */
1271 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1272 case 'x': break; /* CRS2S, constrained to equal RD. */
1273 case 'z': break; /* CRS2S, constrained to be x0. */
1274 case '>': /* CITYPE immediate, compressed shift. */
1275 case 'u': /* CITYPE immediate, compressed lui. */
1276 case 'v': /* CITYPE immediate, li to compressed lui. */
1277 case 'o': /* CITYPE immediate, allow zero. */
1278 case 'j': used_bits |= ENCODE_CITYPE_IMM (-1U); break;
1279 case 'L': used_bits |= ENCODE_CITYPE_ADDI16SP_IMM (-1U); break;
1280 case 'm': used_bits |= ENCODE_CITYPE_LWSP_IMM (-1U); break;
1281 case 'n': used_bits |= ENCODE_CITYPE_LDSP_IMM (-1U); break;
1282 case '6': used_bits |= ENCODE_CSSTYPE_IMM (-1U); break;
1283 case 'M': used_bits |= ENCODE_CSSTYPE_SWSP_IMM (-1U); break;
1284 case 'N': used_bits |= ENCODE_CSSTYPE_SDSP_IMM (-1U); break;
1285 case '8': used_bits |= ENCODE_CIWTYPE_IMM (-1U); break;
1286 case 'K': used_bits |= ENCODE_CIWTYPE_ADDI4SPN_IMM (-1U); break;
1287 /* CLTYPE and CSTYPE have the same immediate encoding. */
1288 case '5': used_bits |= ENCODE_CLTYPE_IMM (-1U); break;
1289 case 'k': used_bits |= ENCODE_CLTYPE_LW_IMM (-1U); break;
1290 case 'l': used_bits |= ENCODE_CLTYPE_LD_IMM (-1U); break;
1291 case 'p': used_bits |= ENCODE_CBTYPE_IMM (-1U); break;
1292 case 'a': used_bits |= ENCODE_CJTYPE_IMM (-1U); break;
1293 case 'F': /* Compressed funct for .insn directive. */
1294 switch (*++oparg)
1295 {
1296 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1297 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1298 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1299 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1300 default:
1301 goto unknown_validate_operand;
1302 }
1303 break;
1304 default:
1305 goto unknown_validate_operand;
1306 }
1307 break; /* end RVC */
1308 case 'V': /* RVV */
1309 switch (*++oparg)
1310 {
1311 case 'd':
1312 case 'f': USE_BITS (OP_MASK_VD, OP_SH_VD); break;
1313 case 'e': USE_BITS (OP_MASK_VWD, OP_SH_VWD); break;
1314 case 's': USE_BITS (OP_MASK_VS1, OP_SH_VS1); break;
1315 case 't': USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1316 case 'u': USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1317 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1318 case 'v': USE_BITS (OP_MASK_VD, OP_SH_VD);
1319 USE_BITS (OP_MASK_VS1, OP_SH_VS1);
1320 USE_BITS (OP_MASK_VS2, OP_SH_VS2); break;
1321 case '0': break;
1322 case 'b': used_bits |= ENCODE_RVV_VB_IMM (-1U); break;
1323 case 'c': used_bits |= ENCODE_RVV_VC_IMM (-1U); break;
1324 case 'i':
1325 case 'j':
1326 case 'k': USE_BITS (OP_MASK_VIMM, OP_SH_VIMM); break;
1327 case 'm': USE_BITS (OP_MASK_VMASK, OP_SH_VMASK); break;
1328 case 'M': break; /* Macro operand, must be a mask register. */
1329 case 'T': break; /* Macro operand, must be a vector register. */
1330 default:
1331 goto unknown_validate_operand;
1332 }
1333 break; /* end RVV */
1334 case ',': break;
1335 case '(': break;
1336 case ')': break;
1337 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1338 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1339 case 'A': break; /* Macro operand, must be symbol. */
1340 case 'B': break; /* Macro operand, must be symbol or constant. */
1341 case 'c': break; /* Macro operand, must be symbol or constant. */
1342 case 'I': break; /* Macro operand, must be constant. */
1343 case 'D': /* RD, floating point. */
1344 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1345 case 'y': USE_BITS (OP_MASK_BS, OP_SH_BS); break;
1346 case 'Y': USE_BITS (OP_MASK_RNUM, OP_SH_RNUM); break;
1347 case 'Z': /* RS1, CSR number. */
1348 case 'S': /* RS1, floating point. */
1349 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1350 case 'U': /* RS1 and RS2 are the same, floating point. */
1351 USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1352 /* Fall through. */
1353 case 'T': /* RS2, floating point. */
1354 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1355 case 'R': /* RS3, floating point. */
1356 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1357 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1358 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1359 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1360 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1361 case 'o': /* ITYPE immediate, load displacement. */
1362 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1363 case 'a': used_bits |= ENCODE_JTYPE_IMM (-1U); break;
1364 case 'p': used_bits |= ENCODE_BTYPE_IMM (-1U); break;
1365 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1366 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1367 case 'z': break; /* Zero immediate. */
1368 case '[': break; /* Unused operand. */
1369 case ']': break; /* Unused operand. */
1370 case '0': break; /* AMO displacement, must to zero. */
1371 case '1': break; /* Relaxation operand. */
1372 case 'F': /* Funct for .insn directive. */
1373 switch (*++oparg)
1374 {
1375 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1376 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1377 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1378 default:
1379 goto unknown_validate_operand;
1380 }
1381 break;
1382 case 'O': /* Opcode for .insn directive. */
1383 switch (*++oparg)
1384 {
1385 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1386 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1387 default:
1388 goto unknown_validate_operand;
1389 }
1390 break;
1391 case 'W': /* Various operands. */
1392 switch (*++oparg)
1393 {
1394 case 'i':
1395 switch (*++oparg)
1396 {
1397 case 'f': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1398 default:
1399 goto unknown_validate_operand;
1400 }
1401 break;
1402 default:
1403 goto unknown_validate_operand;
1404 }
1405 break;
1406 case 'X': /* Integer immediate. */
1407 {
1408 size_t n;
1409 size_t s;
1410
1411 switch (*++oparg)
1412 {
1413 case 'l': /* Literal. */
1414 oparg += strcspn(oparg, ",") - 1;
1415 break;
1416 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
1417 goto use_imm;
1418 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
1419 goto use_imm;
1420 use_imm:
1421 n = strtol (oparg + 1, (char **)&oparg, 10);
1422 if (*oparg != '@')
1423 goto unknown_validate_operand;
1424 s = strtol (oparg + 1, (char **)&oparg, 10);
1425 oparg--;
1426
1427 USE_IMM (n, s);
1428 break;
1429 default:
1430 goto unknown_validate_operand;
1431 }
1432 }
1433 break;
1434 default:
1435 unknown_validate_operand:
1436 as_bad (_("internal: bad RISC-V opcode "
1437 "(unknown operand type `%s'): %s %s"),
1438 opargStart, opc->name, opc->args);
1439 return false;
1440 }
1441 }
1442
1443 if (used_bits != required_bits)
1444 {
1445 as_bad (_("internal: bad RISC-V opcode "
1446 "(bits %#llx undefined or invalid): %s %s"),
1447 (unsigned long long)(used_bits ^ required_bits),
1448 opc->name, opc->args);
1449 return false;
1450 }
1451 return true;
1452 }
1453
1454 #undef USE_BITS
1455
1456 struct percent_op_match
1457 {
1458 const char *str;
1459 bfd_reloc_code_real_type reloc;
1460 };
1461
1462 /* Common hash table initialization function for instruction and .insn
1463 directive. */
1464
1465 static htab_t
1466 init_opcode_hash (const struct riscv_opcode *opcodes,
1467 bool insn_directive_p)
1468 {
1469 int i = 0;
1470 int length;
1471 htab_t hash = str_htab_create ();
1472 while (opcodes[i].name)
1473 {
1474 const char *name = opcodes[i].name;
1475 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1476 as_fatal (_("internal: duplicate %s"), name);
1477
1478 do
1479 {
1480 if (opcodes[i].pinfo != INSN_MACRO)
1481 {
1482 if (insn_directive_p)
1483 length = ((name[0] == 'c') ? 2 : 4);
1484 else
1485 length = 0; /* Let assembler determine the length. */
1486 if (!validate_riscv_insn (&opcodes[i], length))
1487 as_fatal (_("internal: broken assembler. "
1488 "No assembly attempted"));
1489 }
1490 else
1491 gas_assert (!insn_directive_p);
1492 ++i;
1493 }
1494 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1495 }
1496
1497 return hash;
1498 }
1499
1500 /* This function is called once, at assembler startup time. It should set up
1501 all the tables, etc. that the MD part of the assembler will need. */
1502
1503 void
1504 md_begin (void)
1505 {
1506 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1507
1508 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1509 as_warn (_("could not set architecture and machine"));
1510
1511 op_hash = init_opcode_hash (riscv_opcodes, false);
1512 insn_type_hash = init_opcode_hash (riscv_insn_types, true);
1513
1514 reg_names_hash = str_htab_create ();
1515 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1516 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1517 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1518 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1519 hash_reg_names (RCLASS_VECR, riscv_vecr_names_numeric, NVECR);
1520 hash_reg_names (RCLASS_VECM, riscv_vecm_names_numeric, NVECM);
1521 /* Add "fp" as an alias for "s0". */
1522 hash_reg_name (RCLASS_GPR, "fp", 8);
1523
1524 /* Create and insert CSR hash tables. */
1525 csr_extra_hash = str_htab_create ();
1526 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1527 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1528 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1529 DECLARE_CSR(name, num, class, define_version, abort_version);
1530 #include "opcode/riscv-opc.h"
1531 #undef DECLARE_CSR
1532
1533 opcode_names_hash = str_htab_create ();
1534 init_opcode_names_hash ();
1535
1536 /* Set the default alignment for the text section. */
1537 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1538 }
1539
1540 static insn_t
1541 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1542 {
1543 switch (reloc_type)
1544 {
1545 case BFD_RELOC_32:
1546 return value;
1547
1548 case BFD_RELOC_RISCV_HI20:
1549 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1550
1551 case BFD_RELOC_RISCV_LO12_S:
1552 return ENCODE_STYPE_IMM (value);
1553
1554 case BFD_RELOC_RISCV_LO12_I:
1555 return ENCODE_ITYPE_IMM (value);
1556
1557 default:
1558 abort ();
1559 }
1560 }
1561
1562 /* Output an instruction. IP is the instruction information.
1563 ADDRESS_EXPR is an operand of the instruction to be used with
1564 RELOC_TYPE. */
1565
1566 static void
1567 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1568 bfd_reloc_code_real_type reloc_type)
1569 {
1570 dwarf2_emit_insn (0);
1571
1572 if (reloc_type != BFD_RELOC_UNUSED)
1573 {
1574 reloc_howto_type *howto;
1575
1576 gas_assert (address_expr);
1577 if (reloc_type == BFD_RELOC_12_PCREL
1578 || reloc_type == BFD_RELOC_RISCV_JMP)
1579 {
1580 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1581 int best_case = insn_length (ip);
1582 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1583
1584 if (now_seg == absolute_section)
1585 {
1586 as_bad (_("relaxable branches not supported in absolute section"));
1587 return;
1588 }
1589
1590 add_relaxed_insn (ip, worst_case, best_case,
1591 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1592 address_expr->X_add_symbol,
1593 address_expr->X_add_number);
1594 return;
1595 }
1596 else
1597 {
1598 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1599 if (howto == NULL)
1600 as_bad (_("internal: unsupported RISC-V relocation number %d"),
1601 reloc_type);
1602
1603 ip->fixp = fix_new_exp (ip->frag, ip->where,
1604 bfd_get_reloc_size (howto),
1605 address_expr, false, reloc_type);
1606
1607 ip->fixp->fx_tcbit = riscv_opts.relax;
1608 }
1609 }
1610
1611 add_fixed_insn (ip);
1612
1613 /* We need to start a new frag after any instruction that can be
1614 optimized away or compressed by the linker during relaxation, to prevent
1615 the assembler from computing static offsets across such an instruction.
1616 This is necessary to get correct EH info. */
1617 if (reloc_type == BFD_RELOC_RISCV_HI20
1618 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1619 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1620 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1621 {
1622 frag_wane (frag_now);
1623 frag_new (0);
1624 }
1625 }
1626
1627 /* Build an instruction created by a macro expansion. This is passed
1628 a pointer to the count of instructions created so far, an expression,
1629 the name of the instruction to build, an operand format string, and
1630 corresponding arguments. */
1631
1632 static void
1633 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1634 {
1635 const struct riscv_opcode *mo;
1636 struct riscv_cl_insn insn;
1637 bfd_reloc_code_real_type r;
1638 va_list args;
1639 const char *fmtStart;
1640
1641 va_start (args, fmt);
1642
1643 r = BFD_RELOC_UNUSED;
1644 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1645 gas_assert (mo);
1646
1647 /* Find a non-RVC variant of the instruction. append_insn will compress
1648 it if possible. */
1649 while (riscv_insn_length (mo->match) < 4)
1650 mo++;
1651 gas_assert (strcmp (name, mo->name) == 0);
1652
1653 create_insn (&insn, mo);
1654 for (;; ++fmt)
1655 {
1656 fmtStart = fmt;
1657 switch (*fmt)
1658 {
1659 case 'V': /* RVV */
1660 switch (*++fmt)
1661 {
1662 case 'd':
1663 INSERT_OPERAND (VD, insn, va_arg (args, int));
1664 continue;
1665 case 's':
1666 INSERT_OPERAND (VS1, insn, va_arg (args, int));
1667 continue;
1668 case 't':
1669 INSERT_OPERAND (VS2, insn, va_arg (args, int));
1670 continue;
1671 case 'm':
1672 {
1673 int reg = va_arg (args, int);
1674 if (reg == -1)
1675 {
1676 INSERT_OPERAND (VMASK, insn, 1);
1677 continue;
1678 }
1679 else if (reg == 0)
1680 {
1681 INSERT_OPERAND (VMASK, insn, 0);
1682 continue;
1683 }
1684 else
1685 goto unknown_macro_argument;
1686 }
1687 default:
1688 goto unknown_macro_argument;
1689 }
1690 break;
1691
1692 case 'd':
1693 INSERT_OPERAND (RD, insn, va_arg (args, int));
1694 continue;
1695 case 's':
1696 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1697 continue;
1698 case 't':
1699 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1700 continue;
1701
1702 case 'j':
1703 case 'u':
1704 case 'q':
1705 gas_assert (ep != NULL);
1706 r = va_arg (args, int);
1707 continue;
1708
1709 case '\0':
1710 break;
1711 case ',':
1712 continue;
1713 default:
1714 unknown_macro_argument:
1715 as_fatal (_("internal: invalid macro argument `%s'"), fmtStart);
1716 }
1717 break;
1718 }
1719 va_end (args);
1720 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1721
1722 append_insn (&insn, ep, r);
1723 }
1724
1725 /* Build an instruction created by a macro expansion. Like md_assemble but
1726 accept a printf-style format string and arguments. */
1727
1728 static void
1729 md_assemblef (const char *format, ...)
1730 {
1731 char *buf = NULL;
1732 va_list ap;
1733 int r;
1734
1735 va_start (ap, format);
1736
1737 r = vasprintf (&buf, format, ap);
1738
1739 if (r < 0)
1740 as_fatal (_("internal: vasprintf failed"));
1741
1742 md_assemble (buf);
1743 free(buf);
1744
1745 va_end (ap);
1746 }
1747
1748 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1749 unset. */
1750
1751 static void
1752 normalize_constant_expr (expressionS *ex)
1753 {
1754 if (xlen > 32)
1755 return;
1756 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
1757 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
1758 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
1759 - 0x80000000);
1760 }
1761
1762 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1763 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1764
1765 static void
1766 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1767 bool maybe_csr)
1768 {
1769 if (ex->X_op == O_big)
1770 as_bad (_("unsupported large constant"));
1771 else if (maybe_csr && ex->X_op == O_symbol)
1772 as_bad (_("unknown CSR `%s'"),
1773 S_GET_NAME (ex->X_add_symbol));
1774 else if (ex->X_op != O_constant)
1775 as_bad (_("instruction %s requires absolute expression"),
1776 ip->insn_mo->name);
1777 normalize_constant_expr (ex);
1778 }
1779
1780 static symbolS *
1781 make_internal_label (void)
1782 {
1783 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1784 frag_now_fix ());
1785 }
1786
1787 /* Load an entry from the GOT. */
1788
1789 static void
1790 pcrel_access (int destreg, int tempreg, expressionS *ep,
1791 const char *lo_insn, const char *lo_pattern,
1792 bfd_reloc_code_real_type hi_reloc,
1793 bfd_reloc_code_real_type lo_reloc)
1794 {
1795 expressionS ep2;
1796 ep2.X_op = O_symbol;
1797 ep2.X_add_symbol = make_internal_label ();
1798 ep2.X_add_number = 0;
1799
1800 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1801 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1802 }
1803
1804 static void
1805 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1806 bfd_reloc_code_real_type hi_reloc,
1807 bfd_reloc_code_real_type lo_reloc)
1808 {
1809 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1810 }
1811
1812 static void
1813 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1814 bfd_reloc_code_real_type hi_reloc,
1815 bfd_reloc_code_real_type lo_reloc)
1816 {
1817 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1818 }
1819
1820 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1821
1822 static void
1823 riscv_call (int destreg, int tempreg, expressionS *ep,
1824 bfd_reloc_code_real_type reloc)
1825 {
1826 /* Ensure the jalr is emitted to the same frag as the auipc. */
1827 frag_grow (8);
1828 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1829 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1830 /* See comment at end of append_insn. */
1831 frag_wane (frag_now);
1832 frag_new (0);
1833 }
1834
1835 /* Load an integer constant into a register. */
1836
1837 static void
1838 load_const (int reg, expressionS *ep)
1839 {
1840 int shift = RISCV_IMM_BITS;
1841 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
1842 expressionS upper = *ep, lower = *ep;
1843 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
1844 upper.X_add_number -= lower.X_add_number;
1845
1846 if (ep->X_op != O_constant)
1847 {
1848 as_bad (_("unsupported large constant"));
1849 return;
1850 }
1851
1852 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
1853 {
1854 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1855 while (((upper.X_add_number >> shift) & 1) == 0)
1856 shift++;
1857
1858 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1859 load_const (reg, &upper);
1860
1861 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1862 if (lower.X_add_number != 0)
1863 md_assemblef ("addi x%d, x%d, %" PRId64, reg, reg,
1864 (int64_t) lower.X_add_number);
1865 }
1866 else
1867 {
1868 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1869 int hi_reg = 0;
1870
1871 if (upper.X_add_number != 0)
1872 {
1873 /* Discard low part and zero-extend upper immediate. */
1874 upper_imm = ((uint32_t)upper.X_add_number >> shift);
1875
1876 md_assemblef ("lui x%d, 0x%" PRIx64, reg, (uint64_t) upper_imm);
1877 hi_reg = reg;
1878 }
1879
1880 if (lower.X_add_number != 0 || hi_reg == 0)
1881 md_assemblef ("%s x%d, x%d, %" PRId64, ADD32_INSN, reg, hi_reg,
1882 (int64_t) lower.X_add_number);
1883 }
1884 }
1885
1886 /* Zero extend and sign extend byte/half-word/word. */
1887
1888 static void
1889 riscv_ext (int destreg, int srcreg, unsigned shift, bool sign)
1890 {
1891 if (sign)
1892 {
1893 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1894 md_assemblef ("srai x%d, x%d, 0x%x", destreg, destreg, shift);
1895 }
1896 else
1897 {
1898 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1899 md_assemblef ("srli x%d, x%d, 0x%x", destreg, destreg, shift);
1900 }
1901 }
1902
1903 /* Expand RISC-V Vector macros into one or more instructions. */
1904
1905 static void
1906 vector_macro (struct riscv_cl_insn *ip)
1907 {
1908 int vd = (ip->insn_opcode >> OP_SH_VD) & OP_MASK_VD;
1909 int vs1 = (ip->insn_opcode >> OP_SH_VS1) & OP_MASK_VS1;
1910 int vs2 = (ip->insn_opcode >> OP_SH_VS2) & OP_MASK_VS2;
1911 int vm = (ip->insn_opcode >> OP_SH_VMASK) & OP_MASK_VMASK;
1912 int vtemp = (ip->insn_opcode >> OP_SH_VFUNCT6) & OP_MASK_VFUNCT6;
1913 int mask = ip->insn_mo->mask;
1914
1915 switch (mask)
1916 {
1917 case M_VMSGE:
1918 if (vm)
1919 {
1920 /* Unmasked. */
1921 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1922 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1923 break;
1924 }
1925 if (vtemp != 0)
1926 {
1927 /* Masked. Have vtemp to avoid overlap constraints. */
1928 if (vd == vm)
1929 {
1930 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1931 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1932 }
1933 else
1934 {
1935 /* Preserve the value of vd if not updating by vm. */
1936 macro_build (NULL, "vmslt.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1937 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1938 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
1939 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
1940 }
1941 }
1942 else if (vd != vm)
1943 {
1944 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1945 macro_build (NULL, "vmslt.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
1946 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
1947 }
1948 else
1949 as_bad (_("must provide temp if destination overlaps mask"));
1950 break;
1951
1952 case M_VMSGEU:
1953 if (vm)
1954 {
1955 /* Unmasked. */
1956 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, -1);
1957 macro_build (NULL, "vmnand.mm", "Vd,Vt,Vs", vd, vd, vd);
1958 break;
1959 }
1960 if (vtemp != 0)
1961 {
1962 /* Masked. Have vtemp to avoid overlap constraints. */
1963 if (vd == vm)
1964 {
1965 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1966 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vm, vtemp);
1967 }
1968 else
1969 {
1970 /* Preserve the value of vd if not updating by vm. */
1971 macro_build (NULL, "vmsltu.vx", "Vd,Vt,s", vtemp, vs2, vs1);
1972 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vtemp, vm, vtemp);
1973 macro_build (NULL, "vmandnot.mm", "Vd,Vt,Vs", vd, vd, vm);
1974 macro_build (NULL, "vmor.mm", "Vd,Vt,Vs", vd, vtemp, vd);
1975 }
1976 }
1977 else if (vd != vm)
1978 {
1979 /* Masked. This may cause the vd overlaps vs2, when LMUL > 1. */
1980 macro_build (NULL, "vmsltu.vx", "Vd,Vt,sVm", vd, vs2, vs1, vm);
1981 macro_build (NULL, "vmxor.mm", "Vd,Vt,Vs", vd, vd, vm);
1982 }
1983 else
1984 as_bad (_("must provide temp if destination overlaps mask"));
1985 break;
1986
1987 default:
1988 break;
1989 }
1990 }
1991
1992 /* Expand RISC-V assembly macros into one or more instructions. */
1993
1994 static void
1995 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1996 bfd_reloc_code_real_type *imm_reloc)
1997 {
1998 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
1999 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
2000 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
2001 int mask = ip->insn_mo->mask;
2002
2003 switch (mask)
2004 {
2005 case M_LI:
2006 load_const (rd, imm_expr);
2007 break;
2008
2009 case M_LA:
2010 case M_LLA:
2011 /* Load the address of a symbol into a register. */
2012 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
2013 as_bad (_("offset too large"));
2014
2015 if (imm_expr->X_op == O_constant)
2016 load_const (rd, imm_expr);
2017 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol. */
2018 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2019 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2020 else /* Local PIC symbol, or any non-PIC symbol. */
2021 pcrel_load (rd, rd, imm_expr, "addi",
2022 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2023 break;
2024
2025 case M_LA_TLS_GD:
2026 pcrel_load (rd, rd, imm_expr, "addi",
2027 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2028 break;
2029
2030 case M_LA_TLS_IE:
2031 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
2032 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2033 break;
2034
2035 case M_LB:
2036 pcrel_load (rd, rd, imm_expr, "lb",
2037 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2038 break;
2039
2040 case M_LBU:
2041 pcrel_load (rd, rd, imm_expr, "lbu",
2042 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2043 break;
2044
2045 case M_LH:
2046 pcrel_load (rd, rd, imm_expr, "lh",
2047 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2048 break;
2049
2050 case M_LHU:
2051 pcrel_load (rd, rd, imm_expr, "lhu",
2052 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2053 break;
2054
2055 case M_LW:
2056 pcrel_load (rd, rd, imm_expr, "lw",
2057 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2058 break;
2059
2060 case M_LWU:
2061 pcrel_load (rd, rd, imm_expr, "lwu",
2062 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2063 break;
2064
2065 case M_LD:
2066 pcrel_load (rd, rd, imm_expr, "ld",
2067 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2068 break;
2069
2070 case M_FLW:
2071 pcrel_load (rd, rs1, imm_expr, "flw",
2072 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2073 break;
2074
2075 case M_FLD:
2076 pcrel_load (rd, rs1, imm_expr, "fld",
2077 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2078 break;
2079
2080 case M_SB:
2081 pcrel_store (rs2, rs1, imm_expr, "sb",
2082 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2083 break;
2084
2085 case M_SH:
2086 pcrel_store (rs2, rs1, imm_expr, "sh",
2087 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2088 break;
2089
2090 case M_SW:
2091 pcrel_store (rs2, rs1, imm_expr, "sw",
2092 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2093 break;
2094
2095 case M_SD:
2096 pcrel_store (rs2, rs1, imm_expr, "sd",
2097 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2098 break;
2099
2100 case M_FSW:
2101 pcrel_store (rs2, rs1, imm_expr, "fsw",
2102 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2103 break;
2104
2105 case M_FSD:
2106 pcrel_store (rs2, rs1, imm_expr, "fsd",
2107 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2108 break;
2109
2110 case M_CALL:
2111 riscv_call (rd, rs1, imm_expr, *imm_reloc);
2112 break;
2113
2114 case M_ZEXTH:
2115 riscv_ext (rd, rs1, xlen - 16, false);
2116 break;
2117
2118 case M_ZEXTW:
2119 riscv_ext (rd, rs1, xlen - 32, false);
2120 break;
2121
2122 case M_SEXTB:
2123 riscv_ext (rd, rs1, xlen - 8, true);
2124 break;
2125
2126 case M_SEXTH:
2127 riscv_ext (rd, rs1, xlen - 16, true);
2128 break;
2129
2130 case M_VMSGE:
2131 case M_VMSGEU:
2132 vector_macro (ip);
2133 break;
2134
2135 case M_FLH:
2136 pcrel_load (rd, rs1, imm_expr, "flh",
2137 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
2138 break;
2139 case M_FSH:
2140 pcrel_store (rs2, rs1, imm_expr, "fsh",
2141 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
2142 break;
2143
2144 default:
2145 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
2146 break;
2147 }
2148 }
2149
2150 static const struct percent_op_match percent_op_utype[] =
2151 {
2152 {"tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
2153 {"pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
2154 {"got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
2155 {"tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
2156 {"tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
2157 {"hi", BFD_RELOC_RISCV_HI20},
2158 {0, 0}
2159 };
2160
2161 static const struct percent_op_match percent_op_itype[] =
2162 {
2163 {"lo", BFD_RELOC_RISCV_LO12_I},
2164 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
2165 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
2166 {0, 0}
2167 };
2168
2169 static const struct percent_op_match percent_op_stype[] =
2170 {
2171 {"lo", BFD_RELOC_RISCV_LO12_S},
2172 {"tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
2173 {"pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
2174 {0, 0}
2175 };
2176
2177 static const struct percent_op_match percent_op_rtype[] =
2178 {
2179 {"tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
2180 {0, 0}
2181 };
2182
2183 static const struct percent_op_match percent_op_null[] =
2184 {
2185 {0, 0}
2186 };
2187
2188 /* Return true if *STR points to a relocation operator. When returning true,
2189 move *STR over the operator and store its relocation code in *RELOC.
2190 Leave both *STR and *RELOC alone when returning false. */
2191
2192 static bool
2193 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
2194 const struct percent_op_match *percent_op)
2195 {
2196 for ( ; percent_op->str; percent_op++)
2197 if (strncasecmp (*str + 1, percent_op->str, strlen (percent_op->str)) == 0)
2198 {
2199 size_t len = 1 + strlen (percent_op->str);
2200
2201 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
2202 continue;
2203
2204 *str += len;
2205 *reloc = percent_op->reloc;
2206
2207 /* Check whether the output BFD supports this relocation.
2208 If not, issue an error and fall back on something safe. */
2209 if (*reloc != BFD_RELOC_UNUSED
2210 && !bfd_reloc_type_lookup (stdoutput, *reloc))
2211 {
2212 as_bad ("internal: relocation %s isn't supported by the "
2213 "current ABI", percent_op->str);
2214 *reloc = BFD_RELOC_UNUSED;
2215 }
2216 return true;
2217 }
2218 return false;
2219 }
2220
2221 static void
2222 my_getExpression (expressionS *ep, char *str)
2223 {
2224 char *save_in;
2225
2226 save_in = input_line_pointer;
2227 input_line_pointer = str;
2228 expression (ep);
2229 expr_parse_end = input_line_pointer;
2230 input_line_pointer = save_in;
2231 }
2232
2233 /* Parse string STR as a 16-bit relocatable operand. Store the
2234 expression in *EP and the relocation, if any, in RELOC.
2235 Return the number of relocation operators used (0 or 1).
2236
2237 On exit, EXPR_PARSE_END points to the first character after the
2238 expression. */
2239
2240 static size_t
2241 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2242 char *str, const struct percent_op_match *percent_op)
2243 {
2244 size_t reloc_index;
2245 unsigned crux_depth, str_depth, regno;
2246 char *crux;
2247
2248 /* First, check for integer registers. No callers can accept a reg, but
2249 we need to avoid accidentally creating a useless undefined symbol below,
2250 if this is an instruction pattern that can't match. A glibc build fails
2251 if this is removed. */
2252 if (reg_lookup (&str, RCLASS_GPR, &regno))
2253 {
2254 ep->X_op = O_register;
2255 ep->X_add_number = regno;
2256 expr_parse_end = str;
2257 return 0;
2258 }
2259
2260 /* Search for the start of the main expression.
2261
2262 End the loop with CRUX pointing to the start of the main expression and
2263 with CRUX_DEPTH containing the number of open brackets at that point. */
2264 reloc_index = -1;
2265 str_depth = 0;
2266 do
2267 {
2268 reloc_index++;
2269 crux = str;
2270 crux_depth = str_depth;
2271
2272 /* Skip over whitespace and brackets, keeping count of the number
2273 of brackets. */
2274 while (*str == ' ' || *str == '\t' || *str == '(')
2275 if (*str++ == '(')
2276 str_depth++;
2277 }
2278 while (*str == '%'
2279 && reloc_index < 1
2280 && parse_relocation (&str, reloc, percent_op));
2281
2282 my_getExpression (ep, crux);
2283 str = expr_parse_end;
2284
2285 /* Match every open bracket. */
2286 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
2287 if (*str++ == ')')
2288 crux_depth--;
2289
2290 if (crux_depth > 0)
2291 as_bad ("unclosed '('");
2292
2293 expr_parse_end = str;
2294
2295 return reloc_index;
2296 }
2297
2298 /* Parse opcode name, could be an mnemonics or number. */
2299
2300 static size_t
2301 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
2302 char *str, const struct percent_op_match *percent_op)
2303 {
2304 const struct opcode_name_t *o = opcode_name_lookup (&str);
2305
2306 if (o != NULL)
2307 {
2308 ep->X_op = O_constant;
2309 ep->X_add_number = o->val;
2310 return 0;
2311 }
2312
2313 return my_getSmallExpression (ep, reloc, str, percent_op);
2314 }
2315
2316 /* Parse string STR as a vsetvli operand. Store the expression in *EP.
2317 On exit, EXPR_PARSE_END points to the first character after the
2318 expression. */
2319
2320 static void
2321 my_getVsetvliExpression (expressionS *ep, char *str)
2322 {
2323 unsigned int vsew_value = 0, vlmul_value = 0;
2324 unsigned int vta_value = 0, vma_value = 0;
2325 bfd_boolean vsew_found = FALSE, vlmul_found = FALSE;
2326 bfd_boolean vta_found = FALSE, vma_found = FALSE;
2327
2328 if (arg_lookup (&str, riscv_vsew, ARRAY_SIZE (riscv_vsew), &vsew_value))
2329 {
2330 if (*str == ',')
2331 ++str;
2332 if (vsew_found)
2333 as_bad (_("multiple vsew constants"));
2334 vsew_found = TRUE;
2335 }
2336 if (arg_lookup (&str, riscv_vlmul, ARRAY_SIZE (riscv_vlmul), &vlmul_value))
2337 {
2338 if (*str == ',')
2339 ++str;
2340 if (vlmul_found)
2341 as_bad (_("multiple vlmul constants"));
2342 vlmul_found = TRUE;
2343 }
2344 if (arg_lookup (&str, riscv_vta, ARRAY_SIZE (riscv_vta), &vta_value))
2345 {
2346 if (*str == ',')
2347 ++str;
2348 if (vta_found)
2349 as_bad (_("multiple vta constants"));
2350 vta_found = TRUE;
2351 }
2352 if (arg_lookup (&str, riscv_vma, ARRAY_SIZE (riscv_vma), &vma_value))
2353 {
2354 if (*str == ',')
2355 ++str;
2356 if (vma_found)
2357 as_bad (_("multiple vma constants"));
2358 vma_found = TRUE;
2359 }
2360
2361 if (vsew_found || vlmul_found || vta_found || vma_found)
2362 {
2363 ep->X_op = O_constant;
2364 ep->X_add_number = (vlmul_value << OP_SH_VLMUL)
2365 | (vsew_value << OP_SH_VSEW)
2366 | (vta_value << OP_SH_VTA)
2367 | (vma_value << OP_SH_VMA);
2368 expr_parse_end = str;
2369 }
2370 else
2371 {
2372 my_getExpression (ep, str);
2373 str = expr_parse_end;
2374 }
2375 }
2376
2377 /* Detect and handle implicitly zero load-store offsets. For example,
2378 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return true if such
2379 an implicit offset was detected. */
2380
2381 static bool
2382 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
2383 {
2384 /* Check whether there is only a single bracketed expression left.
2385 If so, it must be the base register and the constant must be zero. */
2386 if (*s == '(' && strchr (s + 1, '(') == 0)
2387 {
2388 ep->X_op = O_constant;
2389 ep->X_add_number = 0;
2390 return true;
2391 }
2392
2393 return false;
2394 }
2395
2396 /* All RISC-V CSR instructions belong to one of these classes. */
2397 enum csr_insn_type
2398 {
2399 INSN_NOT_CSR,
2400 INSN_CSRRW,
2401 INSN_CSRRS,
2402 INSN_CSRRC
2403 };
2404
2405 /* Return which CSR instruction is checking. */
2406
2407 static enum csr_insn_type
2408 riscv_csr_insn_type (insn_t insn)
2409 {
2410 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
2411 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
2412 return INSN_CSRRW;
2413 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
2414 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
2415 return INSN_CSRRS;
2416 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
2417 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
2418 return INSN_CSRRC;
2419 else
2420 return INSN_NOT_CSR;
2421 }
2422
2423 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
2424 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
2425 CSR address is 0x3. */
2426
2427 static bool
2428 riscv_csr_read_only_check (insn_t insn)
2429 {
2430 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
2431 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
2432 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
2433 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
2434
2435 if (readonly
2436 && (((csr_insn == INSN_CSRRS
2437 || csr_insn == INSN_CSRRC)
2438 && rs1 != 0)
2439 || csr_insn == INSN_CSRRW))
2440 return false;
2441
2442 return true;
2443 }
2444
2445 /* Return true if it is a privileged instruction. Otherwise, return false.
2446
2447 uret is actually a N-ext instruction. So it is better to regard it as
2448 an user instruction rather than the priv instruction.
2449
2450 hret is used to return from traps in H-mode. H-mode is removed since
2451 the v1.10 priv spec, but probably be added in the new hypervisor spec.
2452 Therefore, hret should be controlled by the hypervisor spec rather than
2453 priv spec in the future.
2454
2455 dret is defined in the debug spec, so it should be checked in the future,
2456 too. */
2457
2458 static bool
2459 riscv_is_priv_insn (insn_t insn)
2460 {
2461 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
2462 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
2463 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
2464 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
2465 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
2466 check it here to keep the compatible. */
2467 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
2468 }
2469
2470 /* This routine assembles an instruction into its binary format. As a
2471 side effect, it sets the global variable imm_reloc to the type of
2472 relocation to do if one of the operands is an address expression. */
2473
2474 static struct riscv_ip_error
2475 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
2476 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
2477 {
2478 /* The operand string defined in the riscv_opcodes. */
2479 const char *oparg, *opargStart;
2480 /* The parsed operands from assembly. */
2481 char *asarg, *asargStart;
2482 char save_c = 0;
2483 struct riscv_opcode *insn;
2484 unsigned int regno;
2485 const struct percent_op_match *p;
2486 struct riscv_ip_error error;
2487 error.msg = "unrecognized opcode";
2488 error.statement = str;
2489 error.missing_ext = NULL;
2490 /* Indicate we are assembling instruction with CSR. */
2491 bool insn_with_csr = false;
2492
2493 /* Parse the name of the instruction. Terminate the string if whitespace
2494 is found so that str_hash_find only sees the name part of the string. */
2495 for (asarg = str; *asarg!= '\0'; ++asarg)
2496 if (ISSPACE (*asarg))
2497 {
2498 save_c = *asarg;
2499 *asarg++ = '\0';
2500 break;
2501 }
2502
2503 insn = (struct riscv_opcode *) str_hash_find (hash, str);
2504
2505 asargStart = asarg;
2506 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
2507 {
2508 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
2509 continue;
2510
2511 if (!riscv_multi_subset_supports (&riscv_rps_as, insn->insn_class))
2512 {
2513 error.missing_ext = riscv_multi_subset_supports_ext (&riscv_rps_as,
2514 insn->insn_class);
2515 continue;
2516 }
2517
2518 /* Reset error message of the previous round. */
2519 error.msg = _("illegal operands");
2520 error.missing_ext = NULL;
2521 create_insn (ip, insn);
2522
2523 imm_expr->X_op = O_absent;
2524 *imm_reloc = BFD_RELOC_UNUSED;
2525 p = percent_op_itype;
2526
2527 for (oparg = insn->args;; ++oparg)
2528 {
2529 opargStart = oparg;
2530 asarg += strspn (asarg, " \t");
2531 switch (*oparg)
2532 {
2533 case '\0': /* End of args. */
2534 if (insn->pinfo != INSN_MACRO)
2535 {
2536 if (!insn->match_func (insn, ip->insn_opcode))
2537 break;
2538
2539 /* For .insn, insn->match and insn->mask are 0. */
2540 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2541 ? ip->insn_opcode
2542 : insn->match) == 2
2543 && !riscv_opts.rvc)
2544 break;
2545
2546 if (riscv_is_priv_insn (ip->insn_opcode))
2547 explicit_priv_attr = true;
2548
2549 /* Check if we write a read-only CSR by the CSR
2550 instruction. */
2551 if (insn_with_csr
2552 && riscv_opts.csr_check
2553 && !riscv_csr_read_only_check (ip->insn_opcode))
2554 {
2555 /* Restore the character in advance, since we want to
2556 report the detailed warning message here. */
2557 if (save_c)
2558 *(asargStart - 1) = save_c;
2559 as_warn (_("read-only CSR is written `%s'"), str);
2560 insn_with_csr = false;
2561 }
2562
2563 /* The (segmant) load and store with EEW 64 cannot be used
2564 when zve32x is enabled. */
2565 if (ip->insn_mo->pinfo & INSN_V_EEW64
2566 && riscv_subset_supports (&riscv_rps_as, "zve32x")
2567 && !riscv_subset_supports (&riscv_rps_as, "zve64x"))
2568 {
2569 error.msg = _("illegal opcode for zve32x");
2570 break;
2571 }
2572 }
2573 if (*asarg != '\0')
2574 break;
2575 /* Successful assembly. */
2576 error.msg = NULL;
2577 insn_with_csr = false;
2578 goto out;
2579
2580 case 'C': /* RVC */
2581 switch (*++oparg)
2582 {
2583 case 's': /* RS1 x8-x15. */
2584 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2585 || !(regno >= 8 && regno <= 15))
2586 break;
2587 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2588 continue;
2589 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2590 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2591 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2592 break;
2593 continue;
2594 case 't': /* RS2 x8-x15. */
2595 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2596 || !(regno >= 8 && regno <= 15))
2597 break;
2598 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2599 continue;
2600 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2601 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2602 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2603 break;
2604 continue;
2605 case 'U': /* RS1, constrained to equal RD. */
2606 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2607 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2608 break;
2609 continue;
2610 case 'V': /* RS2 */
2611 if (!reg_lookup (&asarg, RCLASS_GPR, &regno))
2612 break;
2613 INSERT_OPERAND (CRS2, *ip, regno);
2614 continue;
2615 case 'c': /* RS1, constrained to equal sp. */
2616 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2617 || regno != X_SP)
2618 break;
2619 continue;
2620 case 'z': /* RS2, constrained to equal x0. */
2621 if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
2622 || regno != 0)
2623 break;
2624 continue;
2625 case '>': /* Shift amount, 0 - (XLEN-1). */
2626 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2627 || imm_expr->X_op != O_constant
2628 || (unsigned long) imm_expr->X_add_number >= xlen)
2629 break;
2630 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2631 rvc_imm_done:
2632 asarg = expr_parse_end;
2633 imm_expr->X_op = O_absent;
2634 continue;
2635 case '5':
2636 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2637 || imm_expr->X_op != O_constant
2638 || imm_expr->X_add_number < 0
2639 || imm_expr->X_add_number >= 32
2640 || !VALID_CLTYPE_IMM ((valueT) imm_expr->X_add_number))
2641 break;
2642 ip->insn_opcode |= ENCODE_CLTYPE_IMM (imm_expr->X_add_number);
2643 goto rvc_imm_done;
2644 case '6':
2645 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2646 || imm_expr->X_op != O_constant
2647 || imm_expr->X_add_number < 0
2648 || imm_expr->X_add_number >= 64
2649 || !VALID_CSSTYPE_IMM ((valueT) imm_expr->X_add_number))
2650 break;
2651 ip->insn_opcode |= ENCODE_CSSTYPE_IMM (imm_expr->X_add_number);
2652 goto rvc_imm_done;
2653 case '8':
2654 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2655 || imm_expr->X_op != O_constant
2656 || imm_expr->X_add_number < 0
2657 || imm_expr->X_add_number >= 256
2658 || !VALID_CIWTYPE_IMM ((valueT) imm_expr->X_add_number))
2659 break;
2660 ip->insn_opcode |= ENCODE_CIWTYPE_IMM (imm_expr->X_add_number);
2661 goto rvc_imm_done;
2662 case 'j':
2663 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2664 || imm_expr->X_op != O_constant
2665 || imm_expr->X_add_number == 0
2666 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2667 break;
2668 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2669 goto rvc_imm_done;
2670 case 'k':
2671 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2672 continue;
2673 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2674 || imm_expr->X_op != O_constant
2675 || !VALID_CLTYPE_LW_IMM ((valueT) imm_expr->X_add_number))
2676 break;
2677 ip->insn_opcode |= ENCODE_CLTYPE_LW_IMM (imm_expr->X_add_number);
2678 goto rvc_imm_done;
2679 case 'l':
2680 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2681 continue;
2682 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2683 || imm_expr->X_op != O_constant
2684 || !VALID_CLTYPE_LD_IMM ((valueT) imm_expr->X_add_number))
2685 break;
2686 ip->insn_opcode |= ENCODE_CLTYPE_LD_IMM (imm_expr->X_add_number);
2687 goto rvc_imm_done;
2688 case 'm':
2689 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2690 continue;
2691 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2692 || imm_expr->X_op != O_constant
2693 || !VALID_CITYPE_LWSP_IMM ((valueT) imm_expr->X_add_number))
2694 break;
2695 ip->insn_opcode |=
2696 ENCODE_CITYPE_LWSP_IMM (imm_expr->X_add_number);
2697 goto rvc_imm_done;
2698 case 'n':
2699 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2700 continue;
2701 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2702 || imm_expr->X_op != O_constant
2703 || !VALID_CITYPE_LDSP_IMM ((valueT) imm_expr->X_add_number))
2704 break;
2705 ip->insn_opcode |=
2706 ENCODE_CITYPE_LDSP_IMM (imm_expr->X_add_number);
2707 goto rvc_imm_done;
2708 case 'o':
2709 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2710 || imm_expr->X_op != O_constant
2711 /* C.addiw, c.li, and c.andi allow zero immediate.
2712 C.addi allows zero immediate as hint. Otherwise this
2713 is same as 'j'. */
2714 || !VALID_CITYPE_IMM ((valueT) imm_expr->X_add_number))
2715 break;
2716 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2717 goto rvc_imm_done;
2718 case 'K':
2719 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2720 || imm_expr->X_op != O_constant
2721 || imm_expr->X_add_number == 0
2722 || !VALID_CIWTYPE_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
2723 break;
2724 ip->insn_opcode |=
2725 ENCODE_CIWTYPE_ADDI4SPN_IMM (imm_expr->X_add_number);
2726 goto rvc_imm_done;
2727 case 'L':
2728 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2729 || imm_expr->X_op != O_constant
2730 || !VALID_CITYPE_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
2731 break;
2732 ip->insn_opcode |=
2733 ENCODE_CITYPE_ADDI16SP_IMM (imm_expr->X_add_number);
2734 goto rvc_imm_done;
2735 case 'M':
2736 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2737 continue;
2738 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2739 || imm_expr->X_op != O_constant
2740 || !VALID_CSSTYPE_SWSP_IMM ((valueT) imm_expr->X_add_number))
2741 break;
2742 ip->insn_opcode |=
2743 ENCODE_CSSTYPE_SWSP_IMM (imm_expr->X_add_number);
2744 goto rvc_imm_done;
2745 case 'N':
2746 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
2747 continue;
2748 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2749 || imm_expr->X_op != O_constant
2750 || !VALID_CSSTYPE_SDSP_IMM ((valueT) imm_expr->X_add_number))
2751 break;
2752 ip->insn_opcode |=
2753 ENCODE_CSSTYPE_SDSP_IMM (imm_expr->X_add_number);
2754 goto rvc_imm_done;
2755 case 'u':
2756 p = percent_op_utype;
2757 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
2758 break;
2759 rvc_lui:
2760 if (imm_expr->X_op != O_constant
2761 || imm_expr->X_add_number <= 0
2762 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
2763 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
2764 && (imm_expr->X_add_number <
2765 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
2766 break;
2767 ip->insn_opcode |= ENCODE_CITYPE_IMM (imm_expr->X_add_number);
2768 goto rvc_imm_done;
2769 case 'v':
2770 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2771 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
2772 || ((int32_t)imm_expr->X_add_number
2773 != imm_expr->X_add_number))
2774 break;
2775 imm_expr->X_add_number =
2776 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2777 goto rvc_lui;
2778 case 'p':
2779 goto branch;
2780 case 'a':
2781 if (oparg == insn->args + 1)
2782 goto jump_check_gpr;
2783 goto jump;
2784 case 'S': /* Floating-point RS1 x8-x15. */
2785 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2786 || !(regno >= 8 && regno <= 15))
2787 break;
2788 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2789 continue;
2790 case 'D': /* Floating-point RS2 x8-x15. */
2791 if (!reg_lookup (&asarg, RCLASS_FPR, &regno)
2792 || !(regno >= 8 && regno <= 15))
2793 break;
2794 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2795 continue;
2796 case 'T': /* Floating-point RS2. */
2797 if (!reg_lookup (&asarg, RCLASS_FPR, &regno))
2798 break;
2799 INSERT_OPERAND (CRS2, *ip, regno);
2800 continue;
2801 case 'F':
2802 switch (*++oparg)
2803 {
2804 case '6':
2805 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2806 || imm_expr->X_op != O_constant
2807 || imm_expr->X_add_number < 0
2808 || imm_expr->X_add_number >= 64)
2809 {
2810 as_bad (_("bad value for compressed funct6 "
2811 "field, value must be 0...63"));
2812 break;
2813 }
2814 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2815 imm_expr->X_op = O_absent;
2816 asarg = expr_parse_end;
2817 continue;
2818
2819 case '4':
2820 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2821 || imm_expr->X_op != O_constant
2822 || imm_expr->X_add_number < 0
2823 || imm_expr->X_add_number >= 16)
2824 {
2825 as_bad (_("bad value for compressed funct4 "
2826 "field, value must be 0...15"));
2827 break;
2828 }
2829 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2830 imm_expr->X_op = O_absent;
2831 asarg = expr_parse_end;
2832 continue;
2833
2834 case '3':
2835 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2836 || imm_expr->X_op != O_constant
2837 || imm_expr->X_add_number < 0
2838 || imm_expr->X_add_number >= 8)
2839 {
2840 as_bad (_("bad value for compressed funct3 "
2841 "field, value must be 0...7"));
2842 break;
2843 }
2844 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2845 imm_expr->X_op = O_absent;
2846 asarg = expr_parse_end;
2847 continue;
2848
2849 case '2':
2850 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
2851 || imm_expr->X_op != O_constant
2852 || imm_expr->X_add_number < 0
2853 || imm_expr->X_add_number >= 4)
2854 {
2855 as_bad (_("bad value for compressed funct2 "
2856 "field, value must be 0...3"));
2857 break;
2858 }
2859 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2860 imm_expr->X_op = O_absent;
2861 asarg = expr_parse_end;
2862 continue;
2863
2864 default:
2865 goto unknown_riscv_ip_operand;
2866 }
2867 break;
2868
2869 default:
2870 goto unknown_riscv_ip_operand;
2871 }
2872 break; /* end RVC */
2873
2874 case 'V': /* RVV */
2875 switch (*++oparg)
2876 {
2877 case 'd': /* VD */
2878 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2879 break;
2880 INSERT_OPERAND (VD, *ip, regno);
2881 continue;
2882
2883 case 'e': /* AMO VD */
2884 if (reg_lookup (&asarg, RCLASS_GPR, &regno) && regno == 0)
2885 INSERT_OPERAND (VWD, *ip, 0);
2886 else if (reg_lookup (&asarg, RCLASS_VECR, &regno))
2887 {
2888 INSERT_OPERAND (VWD, *ip, 1);
2889 INSERT_OPERAND (VD, *ip, regno);
2890 }
2891 else
2892 break;
2893 continue;
2894
2895 case 'f': /* AMO VS3 */
2896 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2897 break;
2898 if (!EXTRACT_OPERAND (VWD, ip->insn_opcode))
2899 INSERT_OPERAND (VD, *ip, regno);
2900 else
2901 {
2902 /* VS3 must match VD. */
2903 if (EXTRACT_OPERAND (VD, ip->insn_opcode) != regno)
2904 break;
2905 }
2906 continue;
2907
2908 case 's': /* VS1 */
2909 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2910 break;
2911 INSERT_OPERAND (VS1, *ip, regno);
2912 continue;
2913
2914 case 't': /* VS2 */
2915 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2916 break;
2917 INSERT_OPERAND (VS2, *ip, regno);
2918 continue;
2919
2920 case 'u': /* VS1 == VS2 */
2921 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2922 break;
2923 INSERT_OPERAND (VS1, *ip, regno);
2924 INSERT_OPERAND (VS2, *ip, regno);
2925 continue;
2926
2927 case 'v': /* VD == VS1 == VS2 */
2928 if (!reg_lookup (&asarg, RCLASS_VECR, &regno))
2929 break;
2930 INSERT_OPERAND (VD, *ip, regno);
2931 INSERT_OPERAND (VS1, *ip, regno);
2932 INSERT_OPERAND (VS2, *ip, regno);
2933 continue;
2934
2935 /* The `V0` is carry-in register for v[m]adc and v[m]sbc,
2936 and is used to choose vs1/rs1/frs1/imm or vs2 for
2937 v[f]merge. It use the same encoding as the vector mask
2938 register. */
2939 case '0':
2940 if (reg_lookup (&asarg, RCLASS_VECR, &regno) && regno == 0)
2941 continue;
2942 break;
2943
2944 case 'b': /* vtypei for vsetivli */
2945 my_getVsetvliExpression (imm_expr, asarg);
2946 check_absolute_expr (ip, imm_expr, FALSE);
2947 if (!VALID_RVV_VB_IMM (imm_expr->X_add_number))
2948 as_bad (_("bad value for vsetivli immediate field, "
2949 "value must be 0..1023"));
2950 ip->insn_opcode
2951 |= ENCODE_RVV_VB_IMM (imm_expr->X_add_number);
2952 imm_expr->X_op = O_absent;
2953 asarg = expr_parse_end;
2954 continue;
2955
2956 case 'c': /* vtypei for vsetvli */
2957 my_getVsetvliExpression (imm_expr, asarg);
2958 check_absolute_expr (ip, imm_expr, FALSE);
2959 if (!VALID_RVV_VC_IMM (imm_expr->X_add_number))
2960 as_bad (_("bad value for vsetvli immediate field, "
2961 "value must be 0..2047"));
2962 ip->insn_opcode
2963 |= ENCODE_RVV_VC_IMM (imm_expr->X_add_number);
2964 imm_expr->X_op = O_absent;
2965 asarg = expr_parse_end;
2966 continue;
2967
2968 case 'i': /* vector arith signed immediate */
2969 my_getExpression (imm_expr, asarg);
2970 check_absolute_expr (ip, imm_expr, FALSE);
2971 if (imm_expr->X_add_number > 15
2972 || imm_expr->X_add_number < -16)
2973 as_bad (_("bad value for vector immediate field, "
2974 "value must be -16...15"));
2975 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
2976 imm_expr->X_op = O_absent;
2977 asarg = expr_parse_end;
2978 continue;
2979
2980 case 'j': /* vector arith unsigned immediate */
2981 my_getExpression (imm_expr, asarg);
2982 check_absolute_expr (ip, imm_expr, FALSE);
2983 if (imm_expr->X_add_number < 0
2984 || imm_expr->X_add_number >= 32)
2985 as_bad (_("bad value for vector immediate field, "
2986 "value must be 0...31"));
2987 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number);
2988 imm_expr->X_op = O_absent;
2989 asarg = expr_parse_end;
2990 continue;
2991
2992 case 'k': /* vector arith signed immediate, minus 1 */
2993 my_getExpression (imm_expr, asarg);
2994 check_absolute_expr (ip, imm_expr, FALSE);
2995 if (imm_expr->X_add_number > 16
2996 || imm_expr->X_add_number < -15)
2997 as_bad (_("bad value for vector immediate field, "
2998 "value must be -15...16"));
2999 INSERT_OPERAND (VIMM, *ip, imm_expr->X_add_number - 1);
3000 imm_expr->X_op = O_absent;
3001 asarg = expr_parse_end;
3002 continue;
3003
3004 case 'm': /* optional vector mask */
3005 if (*asarg == '\0')
3006 {
3007 INSERT_OPERAND (VMASK, *ip, 1);
3008 continue;
3009 }
3010 else if (*asarg == ',' && asarg++
3011 && reg_lookup (&asarg, RCLASS_VECM, &regno)
3012 && regno == 0)
3013 {
3014 INSERT_OPERAND (VMASK, *ip, 0);
3015 continue;
3016 }
3017 break;
3018
3019 case 'M': /* required vector mask */
3020 if (reg_lookup (&asarg, RCLASS_VECM, &regno) && regno == 0)
3021 {
3022 INSERT_OPERAND (VMASK, *ip, 0);
3023 continue;
3024 }
3025 break;
3026
3027 case 'T': /* vector macro temporary register */
3028 if (!reg_lookup (&asarg, RCLASS_VECR, &regno) || regno == 0)
3029 break;
3030 /* Store it in the FUNCT6 field as we don't have anyplace
3031 else to store it. */
3032 INSERT_OPERAND (VFUNCT6, *ip, regno);
3033 continue;
3034
3035 default:
3036 goto unknown_riscv_ip_operand;
3037 }
3038 break; /* end RVV */
3039
3040 case ',':
3041 if (*asarg++ == *oparg)
3042 continue;
3043 asarg--;
3044 break;
3045
3046 case '(':
3047 case ')':
3048 case '[':
3049 case ']':
3050 if (*asarg++ == *oparg)
3051 continue;
3052 break;
3053
3054 case '<': /* Shift amount, 0 - 31. */
3055 my_getExpression (imm_expr, asarg);
3056 check_absolute_expr (ip, imm_expr, false);
3057 if ((unsigned long) imm_expr->X_add_number > 31)
3058 as_bad (_("improper shift amount (%"PRIu64")"),
3059 imm_expr->X_add_number);
3060 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
3061 imm_expr->X_op = O_absent;
3062 asarg = expr_parse_end;
3063 continue;
3064
3065 case '>': /* Shift amount, 0 - (XLEN-1). */
3066 my_getExpression (imm_expr, asarg);
3067 check_absolute_expr (ip, imm_expr, false);
3068 if ((unsigned long) imm_expr->X_add_number >= xlen)
3069 as_bad (_("improper shift amount (%"PRIu64")"),
3070 imm_expr->X_add_number);
3071 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
3072 imm_expr->X_op = O_absent;
3073 asarg = expr_parse_end;
3074 continue;
3075
3076 case 'Z': /* CSRRxI immediate. */
3077 my_getExpression (imm_expr, asarg);
3078 check_absolute_expr (ip, imm_expr, false);
3079 if ((unsigned long) imm_expr->X_add_number > 31)
3080 as_bad (_("improper CSRxI immediate (%"PRIu64")"),
3081 imm_expr->X_add_number);
3082 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
3083 imm_expr->X_op = O_absent;
3084 asarg = expr_parse_end;
3085 continue;
3086
3087 case 'E': /* Control register. */
3088 insn_with_csr = true;
3089 explicit_priv_attr = true;
3090 if (reg_lookup (&asarg, RCLASS_CSR, &regno))
3091 INSERT_OPERAND (CSR, *ip, regno);
3092 else
3093 {
3094 my_getExpression (imm_expr, asarg);
3095 check_absolute_expr (ip, imm_expr, true);
3096 if ((unsigned long) imm_expr->X_add_number > 0xfff)
3097 as_bad (_("improper CSR address (%"PRIu64")"),
3098 imm_expr->X_add_number);
3099 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
3100 imm_expr->X_op = O_absent;
3101 asarg = expr_parse_end;
3102 }
3103 continue;
3104
3105 case 'm': /* Rounding mode. */
3106 if (arg_lookup (&asarg, riscv_rm,
3107 ARRAY_SIZE (riscv_rm), &regno))
3108 {
3109 INSERT_OPERAND (RM, *ip, regno);
3110 continue;
3111 }
3112 break;
3113
3114 case 'P':
3115 case 'Q': /* Fence predecessor/successor. */
3116 if (arg_lookup (&asarg, riscv_pred_succ,
3117 ARRAY_SIZE (riscv_pred_succ), &regno))
3118 {
3119 if (*oparg == 'P')
3120 INSERT_OPERAND (PRED, *ip, regno);
3121 else
3122 INSERT_OPERAND (SUCC, *ip, regno);
3123 continue;
3124 }
3125 break;
3126
3127 case 'd': /* Destination register. */
3128 case 's': /* Source register. */
3129 case 't': /* Target register. */
3130 case 'r': /* RS3 */
3131 if (reg_lookup (&asarg, RCLASS_GPR, &regno))
3132 {
3133 char c = *oparg;
3134 if (*asarg == ' ')
3135 ++asarg;
3136
3137 /* Now that we have assembled one operand, we use the args
3138 string to figure out where it goes in the instruction. */
3139 switch (c)
3140 {
3141 case 's':
3142 INSERT_OPERAND (RS1, *ip, regno);
3143 break;
3144 case 'd':
3145 INSERT_OPERAND (RD, *ip, regno);
3146 break;
3147 case 't':
3148 INSERT_OPERAND (RS2, *ip, regno);
3149 break;
3150 case 'r':
3151 INSERT_OPERAND (RS3, *ip, regno);
3152 break;
3153 }
3154 continue;
3155 }
3156 break;
3157
3158 case 'D': /* Floating point RD. */
3159 case 'S': /* Floating point RS1. */
3160 case 'T': /* Floating point RS2. */
3161 case 'U': /* Floating point RS1 and RS2. */
3162 case 'R': /* Floating point RS3. */
3163 if (reg_lookup (&asarg,
3164 (riscv_subset_supports (&riscv_rps_as, "zfinx")
3165 ? RCLASS_GPR : RCLASS_FPR), &regno))
3166 {
3167 char c = *oparg;
3168 if (*asarg == ' ')
3169 ++asarg;
3170 switch (c)
3171 {
3172 case 'D':
3173 INSERT_OPERAND (RD, *ip, regno);
3174 break;
3175 case 'S':
3176 INSERT_OPERAND (RS1, *ip, regno);
3177 break;
3178 case 'U':
3179 INSERT_OPERAND (RS1, *ip, regno);
3180 /* Fall through. */
3181 case 'T':
3182 INSERT_OPERAND (RS2, *ip, regno);
3183 break;
3184 case 'R':
3185 INSERT_OPERAND (RS3, *ip, regno);
3186 break;
3187 }
3188 continue;
3189 }
3190 break;
3191
3192 case 'I':
3193 my_getExpression (imm_expr, asarg);
3194 if (imm_expr->X_op != O_big
3195 && imm_expr->X_op != O_constant)
3196 break;
3197 normalize_constant_expr (imm_expr);
3198 asarg = expr_parse_end;
3199 continue;
3200
3201 case 'A':
3202 my_getExpression (imm_expr, asarg);
3203 normalize_constant_expr (imm_expr);
3204 /* The 'A' format specifier must be a symbol. */
3205 if (imm_expr->X_op != O_symbol)
3206 break;
3207 *imm_reloc = BFD_RELOC_32;
3208 asarg = expr_parse_end;
3209 continue;
3210
3211 case 'B':
3212 my_getExpression (imm_expr, asarg);
3213 normalize_constant_expr (imm_expr);
3214 /* The 'B' format specifier must be a symbol or a constant. */
3215 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
3216 break;
3217 if (imm_expr->X_op == O_symbol)
3218 *imm_reloc = BFD_RELOC_32;
3219 asarg = expr_parse_end;
3220 continue;
3221
3222 case 'j': /* Sign-extended immediate. */
3223 p = percent_op_itype;
3224 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3225 goto alu_op;
3226 case 'q': /* Store displacement. */
3227 p = percent_op_stype;
3228 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
3229 goto load_store;
3230 case 'o': /* Load displacement. */
3231 p = percent_op_itype;
3232 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
3233 goto load_store;
3234 case '1':
3235 /* This is used for TLS, where the fourth operand is
3236 %tprel_add, to get a relocation applied to an add
3237 instruction, for relaxation to use. */
3238 p = percent_op_rtype;
3239 goto alu_op;
3240 case '0': /* AMO displacement, which must be zero. */
3241 p = percent_op_null;
3242 load_store:
3243 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3244 continue;
3245 alu_op:
3246 /* If this value won't fit into a 16 bit offset, then go
3247 find a macro that will generate the 32 bit offset
3248 code pattern. */
3249 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3250 {
3251 normalize_constant_expr (imm_expr);
3252 if (imm_expr->X_op != O_constant
3253 || (*oparg == '0' && imm_expr->X_add_number != 0)
3254 || (*oparg == '1')
3255 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
3256 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
3257 break;
3258 }
3259 asarg = expr_parse_end;
3260 continue;
3261
3262 case 'p': /* PC-relative offset. */
3263 branch:
3264 *imm_reloc = BFD_RELOC_12_PCREL;
3265 my_getExpression (imm_expr, asarg);
3266 asarg = expr_parse_end;
3267 continue;
3268
3269 case 'u': /* Upper 20 bits. */
3270 p = percent_op_utype;
3271 if (!my_getSmallExpression (imm_expr, imm_reloc, asarg, p))
3272 {
3273 if (imm_expr->X_op != O_constant)
3274 break;
3275
3276 if (imm_expr->X_add_number < 0
3277 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
3278 as_bad (_("lui expression not in range 0..1048575"));
3279
3280 *imm_reloc = BFD_RELOC_RISCV_HI20;
3281 imm_expr->X_add_number <<= RISCV_IMM_BITS;
3282 }
3283 asarg = expr_parse_end;
3284 continue;
3285
3286 case 'a': /* 20-bit PC-relative offset. */
3287 /* Like in my_getSmallExpression() we need to avoid emitting
3288 a stray undefined symbol if the 1st JAL entry doesn't match,
3289 but the 2nd (with 2 operands) might. */
3290 if (oparg == insn->args)
3291 {
3292 jump_check_gpr:
3293 asargStart = asarg;
3294 if (reg_lookup (&asarg, RCLASS_GPR, NULL)
3295 && (*asarg == ',' || (ISSPACE (*asarg) && asarg[1] == ',')))
3296 break;
3297 asarg = asargStart;
3298 }
3299 jump:
3300 my_getExpression (imm_expr, asarg);
3301 asarg = expr_parse_end;
3302 *imm_reloc = BFD_RELOC_RISCV_JMP;
3303 continue;
3304
3305 case 'c':
3306 my_getExpression (imm_expr, asarg);
3307 asarg = expr_parse_end;
3308 if (strcmp (asarg, "@plt") == 0)
3309 asarg += 4;
3310 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
3311 continue;
3312
3313 case 'O':
3314 switch (*++oparg)
3315 {
3316 case '4':
3317 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg, p)
3318 || imm_expr->X_op != O_constant
3319 || imm_expr->X_add_number < 0
3320 || imm_expr->X_add_number >= 128
3321 || (imm_expr->X_add_number & 0x3) != 3)
3322 {
3323 as_bad (_("bad value for opcode field, "
3324 "value must be 0...127 and "
3325 "lower 2 bits must be 0x3"));
3326 break;
3327 }
3328 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
3329 imm_expr->X_op = O_absent;
3330 asarg = expr_parse_end;
3331 continue;
3332
3333 case '2':
3334 if (my_getOpcodeExpression (imm_expr, imm_reloc, asarg, p)
3335 || imm_expr->X_op != O_constant
3336 || imm_expr->X_add_number < 0
3337 || imm_expr->X_add_number >= 3)
3338 {
3339 as_bad (_("bad value for opcode field, "
3340 "value must be 0...2"));
3341 break;
3342 }
3343 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
3344 imm_expr->X_op = O_absent;
3345 asarg = expr_parse_end;
3346 continue;
3347
3348 default:
3349 goto unknown_riscv_ip_operand;
3350 }
3351 break;
3352
3353 case 'F':
3354 switch (*++oparg)
3355 {
3356 case '7':
3357 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3358 || imm_expr->X_op != O_constant
3359 || imm_expr->X_add_number < 0
3360 || imm_expr->X_add_number >= 128)
3361 {
3362 as_bad (_("bad value for funct7 field, "
3363 "value must be 0...127"));
3364 break;
3365 }
3366 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
3367 imm_expr->X_op = O_absent;
3368 asarg = expr_parse_end;
3369 continue;
3370
3371 case '3':
3372 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3373 || imm_expr->X_op != O_constant
3374 || imm_expr->X_add_number < 0
3375 || imm_expr->X_add_number >= 8)
3376 {
3377 as_bad (_("bad value for funct3 field, "
3378 "value must be 0...7"));
3379 break;
3380 }
3381 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
3382 imm_expr->X_op = O_absent;
3383 asarg = expr_parse_end;
3384 continue;
3385
3386 case '2':
3387 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3388 || imm_expr->X_op != O_constant
3389 || imm_expr->X_add_number < 0
3390 || imm_expr->X_add_number >= 4)
3391 {
3392 as_bad (_("bad value for funct2 field, "
3393 "value must be 0...3"));
3394 break;
3395 }
3396 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
3397 imm_expr->X_op = O_absent;
3398 asarg = expr_parse_end;
3399 continue;
3400
3401 default:
3402 goto unknown_riscv_ip_operand;
3403 }
3404 break;
3405
3406 case 'y': /* bs immediate */
3407 my_getExpression (imm_expr, asarg);
3408 check_absolute_expr (ip, imm_expr, FALSE);
3409 if ((unsigned long)imm_expr->X_add_number > 3)
3410 as_bad(_("Improper bs immediate (%lu)"),
3411 (unsigned long)imm_expr->X_add_number);
3412 INSERT_OPERAND(BS, *ip, imm_expr->X_add_number);
3413 imm_expr->X_op = O_absent;
3414 asarg = expr_parse_end;
3415 continue;
3416
3417 case 'Y': /* rnum immediate */
3418 my_getExpression (imm_expr, asarg);
3419 check_absolute_expr (ip, imm_expr, FALSE);
3420 if ((unsigned long)imm_expr->X_add_number > 10)
3421 as_bad(_("Improper rnum immediate (%lu)"),
3422 (unsigned long)imm_expr->X_add_number);
3423 INSERT_OPERAND(RNUM, *ip, imm_expr->X_add_number);
3424 imm_expr->X_op = O_absent;
3425 asarg = expr_parse_end;
3426 continue;
3427
3428 case 'z':
3429 if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
3430 || imm_expr->X_op != O_constant
3431 || imm_expr->X_add_number != 0)
3432 break;
3433 asarg = expr_parse_end;
3434 imm_expr->X_op = O_absent;
3435 continue;
3436
3437 case 'W': /* Various operands. */
3438 switch (*++oparg)
3439 {
3440 case 'i':
3441 switch (*++oparg)
3442 {
3443 case 'f':
3444 /* Prefetch offset for 'Zicbop' extension.
3445 pseudo S-type but lower 5-bits zero. */
3446 if (riscv_handle_implicit_zero_offset (imm_expr, asarg))
3447 continue;
3448 my_getExpression (imm_expr, asarg);
3449 check_absolute_expr (ip, imm_expr, false);
3450 if (((unsigned) (imm_expr->X_add_number) & 0x1fU)
3451 || imm_expr->X_add_number >= RISCV_IMM_REACH / 2
3452 || imm_expr->X_add_number < -RISCV_IMM_REACH / 2)
3453 as_bad (_ ("improper prefetch offset (%ld)"),
3454 (long) imm_expr->X_add_number);
3455 ip->insn_opcode |= ENCODE_STYPE_IMM (
3456 (unsigned) (imm_expr->X_add_number) & ~0x1fU);
3457 imm_expr->X_op = O_absent;
3458 asarg = expr_parse_end;
3459 continue;
3460 default:
3461 goto unknown_riscv_ip_operand;
3462 }
3463 break;
3464 default:
3465 goto unknown_riscv_ip_operand;
3466 }
3467 break;
3468
3469 case 'X': /* Integer immediate. */
3470 {
3471 size_t n;
3472 size_t s;
3473 bool sign;
3474
3475 switch (*++oparg)
3476 {
3477 case 'l': /* Literal. */
3478 n = strcspn (++oparg, ",");
3479 if (strncmp (oparg, asarg, n))
3480 as_bad (_("unexpected literal (%s)"), asarg);
3481 oparg += n - 1;
3482 asarg += n;
3483 continue;
3484 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
3485 sign = true;
3486 goto parse_imm;
3487 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
3488 sign = false;
3489 goto parse_imm;
3490 parse_imm:
3491 n = strtol (oparg + 1, (char **)&oparg, 10);
3492 if (*oparg != '@')
3493 goto unknown_riscv_ip_operand;
3494 s = strtol (oparg + 1, (char **)&oparg, 10);
3495 oparg--;
3496
3497 my_getExpression (imm_expr, asarg);
3498 check_absolute_expr (ip, imm_expr, false);
3499 if (!sign)
3500 {
3501 if (!VALIDATE_U_IMM (imm_expr->X_add_number, n))
3502 as_bad (_("improper immediate value (%"PRIu64")"),
3503 imm_expr->X_add_number);
3504 }
3505 else
3506 {
3507 if (!VALIDATE_S_IMM (imm_expr->X_add_number, n))
3508 as_bad (_("improper immediate value (%"PRIi64")"),
3509 imm_expr->X_add_number);
3510 }
3511 INSERT_IMM (n, s, *ip, imm_expr->X_add_number);
3512 imm_expr->X_op = O_absent;
3513 asarg = expr_parse_end;
3514 continue;
3515 default:
3516 goto unknown_riscv_ip_operand;
3517 }
3518 }
3519 break;
3520
3521 default:
3522 unknown_riscv_ip_operand:
3523 as_fatal (_("internal: unknown argument type `%s'"),
3524 opargStart);
3525 }
3526 break;
3527 }
3528 asarg = asargStart;
3529 insn_with_csr = false;
3530 }
3531
3532 out:
3533 /* Restore the character we might have clobbered above. */
3534 if (save_c)
3535 *(asargStart - 1) = save_c;
3536
3537 return error;
3538 }
3539
3540 /* Similar to riscv_ip, but assembles an instruction according to the
3541 hardcode values of .insn directive. */
3542
3543 static const char *
3544 riscv_ip_hardcode (char *str,
3545 struct riscv_cl_insn *ip,
3546 expressionS *imm_expr,
3547 const char *error)
3548 {
3549 struct riscv_opcode *insn;
3550 insn_t values[2] = {0, 0};
3551 unsigned int num = 0;
3552
3553 input_line_pointer = str;
3554 do
3555 {
3556 expression (imm_expr);
3557 switch (imm_expr->X_op)
3558 {
3559 case O_constant:
3560 values[num++] = (insn_t) imm_expr->X_add_number;
3561 break;
3562 case O_big:
3563 /* Extract lower 32-bits of a big number.
3564 Assume that generic_bignum_to_int32 work on such number. */
3565 values[num++] = (insn_t) generic_bignum_to_int32 ();
3566 break;
3567 default:
3568 /* The first value isn't constant, so it should be
3569 .insn <type> <operands>. We have been parsed it
3570 in the riscv_ip. */
3571 if (num == 0)
3572 return error;
3573 return _("values must be constant");
3574 }
3575 }
3576 while (*input_line_pointer++ == ',' && num < 2 && imm_expr->X_op != O_big);
3577
3578 input_line_pointer--;
3579 if (*input_line_pointer != '\0')
3580 return _("unrecognized values");
3581
3582 insn = XNEW (struct riscv_opcode);
3583 insn->match = values[num - 1];
3584 create_insn (ip, insn);
3585 unsigned int bytes = riscv_insn_length (insn->match);
3586
3587 if (num == 2 && values[0] != bytes)
3588 return _("value conflicts with instruction length");
3589
3590 if (imm_expr->X_op == O_big)
3591 {
3592 unsigned int llen = 0;
3593 for (LITTLENUM_TYPE lval = generic_bignum[imm_expr->X_add_number - 1];
3594 lval != 0; llen++)
3595 lval >>= BITS_PER_CHAR;
3596 unsigned int repr_bytes
3597 = (imm_expr->X_add_number - 1) * CHARS_PER_LITTLENUM + llen;
3598 if (bytes < repr_bytes)
3599 return _("value conflicts with instruction length");
3600 for (num = 0; num < imm_expr->X_add_number - 1; ++num)
3601 number_to_chars_littleendian (
3602 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3603 generic_bignum[num],
3604 CHARS_PER_LITTLENUM);
3605 if (llen != 0)
3606 number_to_chars_littleendian (
3607 ip->insn_long_opcode + num * CHARS_PER_LITTLENUM,
3608 generic_bignum[num],
3609 llen);
3610 memset(ip->insn_long_opcode + repr_bytes, 0, bytes - repr_bytes);
3611 return NULL;
3612 }
3613
3614 if (bytes < sizeof(values[0]) && values[num - 1] >> (8 * bytes) != 0)
3615 return _("value conflicts with instruction length");
3616
3617 return NULL;
3618 }
3619
3620 void
3621 md_assemble (char *str)
3622 {
3623 struct riscv_cl_insn insn;
3624 expressionS imm_expr;
3625 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
3626
3627 /* The architecture and privileged elf attributes should be set
3628 before assembling. */
3629 if (!start_assemble)
3630 {
3631 start_assemble = true;
3632
3633 riscv_set_abi_by_arch ();
3634 if (!riscv_set_default_priv_spec (NULL))
3635 return;
3636 }
3637
3638 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
3639
3640 const struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
3641 &imm_reloc, op_hash);
3642
3643 if (error.msg)
3644 {
3645 if (error.missing_ext)
3646 as_bad ("%s `%s', extension `%s' required", error.msg,
3647 error.statement, error.missing_ext);
3648 else
3649 as_bad ("%s `%s'", error.msg, error.statement);
3650 return;
3651 }
3652
3653 if (insn.insn_mo->pinfo == INSN_MACRO)
3654 macro (&insn, &imm_expr, &imm_reloc);
3655 else
3656 append_insn (&insn, &imm_expr, imm_reloc);
3657 }
3658
3659 const char *
3660 md_atof (int type, char *litP, int *sizeP)
3661 {
3662 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
3663 }
3664
3665 void
3666 md_number_to_chars (char *buf, valueT val, int n)
3667 {
3668 if (target_big_endian)
3669 number_to_chars_bigendian (buf, val, n);
3670 else
3671 number_to_chars_littleendian (buf, val, n);
3672 }
3673
3674 const char *md_shortopts = "O::g::G:";
3675
3676 enum options
3677 {
3678 OPTION_MARCH = OPTION_MD_BASE,
3679 OPTION_PIC,
3680 OPTION_NO_PIC,
3681 OPTION_MABI,
3682 OPTION_RELAX,
3683 OPTION_NO_RELAX,
3684 OPTION_ARCH_ATTR,
3685 OPTION_NO_ARCH_ATTR,
3686 OPTION_CSR_CHECK,
3687 OPTION_NO_CSR_CHECK,
3688 OPTION_MISA_SPEC,
3689 OPTION_MPRIV_SPEC,
3690 OPTION_BIG_ENDIAN,
3691 OPTION_LITTLE_ENDIAN,
3692 OPTION_END_OF_ENUM
3693 };
3694
3695 struct option md_longopts[] =
3696 {
3697 {"march", required_argument, NULL, OPTION_MARCH},
3698 {"fPIC", no_argument, NULL, OPTION_PIC},
3699 {"fpic", no_argument, NULL, OPTION_PIC},
3700 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
3701 {"mabi", required_argument, NULL, OPTION_MABI},
3702 {"mrelax", no_argument, NULL, OPTION_RELAX},
3703 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
3704 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
3705 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
3706 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
3707 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
3708 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
3709 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
3710 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
3711 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
3712
3713 {NULL, no_argument, NULL, 0}
3714 };
3715 size_t md_longopts_size = sizeof (md_longopts);
3716
3717 int
3718 md_parse_option (int c, const char *arg)
3719 {
3720 switch (c)
3721 {
3722 case OPTION_MARCH:
3723 default_arch_with_ext = arg;
3724 break;
3725
3726 case OPTION_NO_PIC:
3727 riscv_opts.pic = false;
3728 break;
3729
3730 case OPTION_PIC:
3731 riscv_opts.pic = true;
3732 break;
3733
3734 case OPTION_MABI:
3735 if (strcmp (arg, "ilp32") == 0)
3736 riscv_set_abi (32, FLOAT_ABI_SOFT, false);
3737 else if (strcmp (arg, "ilp32e") == 0)
3738 riscv_set_abi (32, FLOAT_ABI_SOFT, true);
3739 else if (strcmp (arg, "ilp32f") == 0)
3740 riscv_set_abi (32, FLOAT_ABI_SINGLE, false);
3741 else if (strcmp (arg, "ilp32d") == 0)
3742 riscv_set_abi (32, FLOAT_ABI_DOUBLE, false);
3743 else if (strcmp (arg, "ilp32q") == 0)
3744 riscv_set_abi (32, FLOAT_ABI_QUAD, false);
3745 else if (strcmp (arg, "lp64") == 0)
3746 riscv_set_abi (64, FLOAT_ABI_SOFT, false);
3747 else if (strcmp (arg, "lp64f") == 0)
3748 riscv_set_abi (64, FLOAT_ABI_SINGLE, false);
3749 else if (strcmp (arg, "lp64d") == 0)
3750 riscv_set_abi (64, FLOAT_ABI_DOUBLE, false);
3751 else if (strcmp (arg, "lp64q") == 0)
3752 riscv_set_abi (64, FLOAT_ABI_QUAD, false);
3753 else
3754 return 0;
3755 explicit_mabi = true;
3756 break;
3757
3758 case OPTION_RELAX:
3759 riscv_opts.relax = true;
3760 break;
3761
3762 case OPTION_NO_RELAX:
3763 riscv_opts.relax = false;
3764 break;
3765
3766 case OPTION_ARCH_ATTR:
3767 riscv_opts.arch_attr = true;
3768 break;
3769
3770 case OPTION_NO_ARCH_ATTR:
3771 riscv_opts.arch_attr = false;
3772 break;
3773
3774 case OPTION_CSR_CHECK:
3775 riscv_opts.csr_check = true;
3776 break;
3777
3778 case OPTION_NO_CSR_CHECK:
3779 riscv_opts.csr_check = false;
3780 break;
3781
3782 case OPTION_MISA_SPEC:
3783 return riscv_set_default_isa_spec (arg);
3784
3785 case OPTION_MPRIV_SPEC:
3786 return riscv_set_default_priv_spec (arg);
3787
3788 case OPTION_BIG_ENDIAN:
3789 target_big_endian = 1;
3790 break;
3791
3792 case OPTION_LITTLE_ENDIAN:
3793 target_big_endian = 0;
3794 break;
3795
3796 default:
3797 return 0;
3798 }
3799
3800 return 1;
3801 }
3802
3803 void
3804 riscv_after_parse_args (void)
3805 {
3806 /* The --with-arch is optional for now, so we still need to set the xlen
3807 according to the default_arch, which is set by the --target. */
3808 if (xlen == 0)
3809 {
3810 if (strcmp (default_arch, "riscv32") == 0)
3811 xlen = 32;
3812 else if (strcmp (default_arch, "riscv64") == 0)
3813 xlen = 64;
3814 else
3815 as_bad ("unknown default architecture `%s'", default_arch);
3816 }
3817
3818 /* Set default specs. */
3819 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
3820 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
3821 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
3822 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
3823
3824 riscv_set_arch (default_arch_with_ext);
3825
3826 /* If the CIE to be produced has not been overridden on the command line,
3827 then produce version 3 by default. This allows us to use the full
3828 range of registers in a .cfi_return_column directive. */
3829 if (flag_dwarf_cie_version == -1)
3830 flag_dwarf_cie_version = 3;
3831 }
3832
3833 long
3834 md_pcrel_from (fixS *fixP)
3835 {
3836 return fixP->fx_where + fixP->fx_frag->fr_address;
3837 }
3838
3839 /* Apply a fixup to the object file. */
3840
3841 void
3842 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
3843 {
3844 unsigned int subtype;
3845 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
3846 bool relaxable = false;
3847 offsetT loc;
3848 segT sub_segment;
3849
3850 /* Remember value for tc_gen_reloc. */
3851 fixP->fx_addnumber = *valP;
3852
3853 switch (fixP->fx_r_type)
3854 {
3855 case BFD_RELOC_RISCV_HI20:
3856 case BFD_RELOC_RISCV_LO12_I:
3857 case BFD_RELOC_RISCV_LO12_S:
3858 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
3859 | bfd_getl32 (buf), buf);
3860 if (fixP->fx_addsy == NULL)
3861 fixP->fx_done = true;
3862 relaxable = true;
3863 break;
3864
3865 case BFD_RELOC_RISCV_GOT_HI20:
3866 case BFD_RELOC_RISCV_ADD8:
3867 case BFD_RELOC_RISCV_ADD16:
3868 case BFD_RELOC_RISCV_ADD32:
3869 case BFD_RELOC_RISCV_ADD64:
3870 case BFD_RELOC_RISCV_SUB6:
3871 case BFD_RELOC_RISCV_SUB8:
3872 case BFD_RELOC_RISCV_SUB16:
3873 case BFD_RELOC_RISCV_SUB32:
3874 case BFD_RELOC_RISCV_SUB64:
3875 case BFD_RELOC_RISCV_RELAX:
3876 break;
3877
3878 case BFD_RELOC_RISCV_TPREL_HI20:
3879 case BFD_RELOC_RISCV_TPREL_LO12_I:
3880 case BFD_RELOC_RISCV_TPREL_LO12_S:
3881 case BFD_RELOC_RISCV_TPREL_ADD:
3882 relaxable = true;
3883 /* Fall through. */
3884
3885 case BFD_RELOC_RISCV_TLS_GOT_HI20:
3886 case BFD_RELOC_RISCV_TLS_GD_HI20:
3887 case BFD_RELOC_RISCV_TLS_DTPREL32:
3888 case BFD_RELOC_RISCV_TLS_DTPREL64:
3889 if (fixP->fx_addsy != NULL)
3890 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3891 else
3892 as_bad_where (fixP->fx_file, fixP->fx_line,
3893 _("TLS relocation against a constant"));
3894 break;
3895
3896 case BFD_RELOC_32:
3897 /* Use pc-relative relocation for FDE initial location.
3898 The symbol address in .eh_frame may be adjusted in
3899 _bfd_elf_discard_section_eh_frame, and the content of
3900 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
3901 Therefore, we cannot insert a relocation whose addend symbol is
3902 in .eh_frame. Othrewise, the value may be adjusted twice. */
3903 if (fixP->fx_addsy && fixP->fx_subsy
3904 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
3905 && strcmp (sub_segment->name, ".eh_frame") == 0
3906 && S_GET_VALUE (fixP->fx_subsy)
3907 == fixP->fx_frag->fr_address + fixP->fx_where)
3908 {
3909 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
3910 fixP->fx_subsy = NULL;
3911 break;
3912 }
3913 /* Fall through. */
3914 case BFD_RELOC_64:
3915 case BFD_RELOC_16:
3916 case BFD_RELOC_8:
3917 case BFD_RELOC_RISCV_CFA:
3918 if (fixP->fx_addsy && fixP->fx_subsy)
3919 {
3920 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3921 fixP->fx_next->fx_addsy = fixP->fx_subsy;
3922 fixP->fx_next->fx_subsy = NULL;
3923 fixP->fx_next->fx_offset = 0;
3924 fixP->fx_subsy = NULL;
3925
3926 switch (fixP->fx_r_type)
3927 {
3928 case BFD_RELOC_64:
3929 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
3930 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
3931 break;
3932
3933 case BFD_RELOC_32:
3934 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
3935 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3936 break;
3937
3938 case BFD_RELOC_16:
3939 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
3940 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3941 break;
3942
3943 case BFD_RELOC_8:
3944 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
3945 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3946 break;
3947
3948 case BFD_RELOC_RISCV_CFA:
3949 /* Load the byte to get the subtype. */
3950 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
3951 loc = fixP->fx_frag->fr_fix - (subtype & 7);
3952 switch (subtype)
3953 {
3954 case DW_CFA_advance_loc1:
3955 fixP->fx_where = loc + 1;
3956 fixP->fx_next->fx_where = loc + 1;
3957 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
3958 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3959 break;
3960
3961 case DW_CFA_advance_loc2:
3962 fixP->fx_size = 2;
3963 fixP->fx_next->fx_size = 2;
3964 fixP->fx_where = loc + 1;
3965 fixP->fx_next->fx_where = loc + 1;
3966 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
3967 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3968 break;
3969
3970 case DW_CFA_advance_loc4:
3971 fixP->fx_size = 4;
3972 fixP->fx_next->fx_size = 4;
3973 fixP->fx_where = loc;
3974 fixP->fx_next->fx_where = loc;
3975 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
3976 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3977 break;
3978
3979 default:
3980 if (subtype < 0x80 && (subtype & 0x40))
3981 {
3982 /* DW_CFA_advance_loc */
3983 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
3984 fixP->fx_next->fx_frag = fixP->fx_frag;
3985 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
3986 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
3987 }
3988 else
3989 as_fatal (_("internal: bad CFA value #%d"), subtype);
3990 break;
3991 }
3992 break;
3993
3994 default:
3995 /* This case is unreachable. */
3996 abort ();
3997 }
3998 }
3999 /* Fall through. */
4000
4001 case BFD_RELOC_RVA:
4002 /* If we are deleting this reloc entry, we must fill in the
4003 value now. This can happen if we have a .word which is not
4004 resolved when it appears but is later defined. */
4005 if (fixP->fx_addsy == NULL)
4006 {
4007 gas_assert (fixP->fx_size <= sizeof (valueT));
4008 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
4009 fixP->fx_done = 1;
4010 }
4011 break;
4012
4013 case BFD_RELOC_RISCV_JMP:
4014 if (fixP->fx_addsy)
4015 {
4016 /* Fill in a tentative value to improve objdump readability. */
4017 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4018 bfd_vma delta = target - md_pcrel_from (fixP);
4019 bfd_putl32 (bfd_getl32 (buf) | ENCODE_JTYPE_IMM (delta), buf);
4020 }
4021 break;
4022
4023 case BFD_RELOC_12_PCREL:
4024 if (fixP->fx_addsy)
4025 {
4026 /* Fill in a tentative value to improve objdump readability. */
4027 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4028 bfd_vma delta = target - md_pcrel_from (fixP);
4029 bfd_putl32 (bfd_getl32 (buf) | ENCODE_BTYPE_IMM (delta), buf);
4030 }
4031 break;
4032
4033 case BFD_RELOC_RISCV_RVC_BRANCH:
4034 if (fixP->fx_addsy)
4035 {
4036 /* Fill in a tentative value to improve objdump readability. */
4037 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4038 bfd_vma delta = target - md_pcrel_from (fixP);
4039 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CBTYPE_IMM (delta), buf);
4040 }
4041 break;
4042
4043 case BFD_RELOC_RISCV_RVC_JUMP:
4044 if (fixP->fx_addsy)
4045 {
4046 /* Fill in a tentative value to improve objdump readability. */
4047 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
4048 bfd_vma delta = target - md_pcrel_from (fixP);
4049 bfd_putl16 (bfd_getl16 (buf) | ENCODE_CJTYPE_IMM (delta), buf);
4050 }
4051 break;
4052
4053 case BFD_RELOC_RISCV_CALL:
4054 case BFD_RELOC_RISCV_CALL_PLT:
4055 relaxable = true;
4056 break;
4057
4058 case BFD_RELOC_RISCV_PCREL_HI20:
4059 case BFD_RELOC_RISCV_PCREL_LO12_S:
4060 case BFD_RELOC_RISCV_PCREL_LO12_I:
4061 relaxable = riscv_opts.relax;
4062 break;
4063
4064 case BFD_RELOC_RISCV_ALIGN:
4065 break;
4066
4067 default:
4068 /* We ignore generic BFD relocations we don't know about. */
4069 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
4070 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
4071 }
4072
4073 if (fixP->fx_subsy != NULL)
4074 as_bad_subtract (fixP);
4075
4076 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
4077 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
4078 {
4079 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
4080 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
4081 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
4082 fixP->fx_next->fx_size = 0;
4083 }
4084 }
4085
4086 /* Because the value of .cfi_remember_state may changed after relaxation,
4087 we insert a fix to relocate it again in link-time. */
4088
4089 void
4090 riscv_pre_output_hook (void)
4091 {
4092 const frchainS *frch;
4093 segT s;
4094
4095 /* Save the current segment info. */
4096 segT seg = now_seg;
4097 subsegT subseg = now_subseg;
4098
4099 for (s = stdoutput->sections; s; s = s->next)
4100 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
4101 {
4102 fragS *frag;
4103
4104 for (frag = frch->frch_root; frag; frag = frag->fr_next)
4105 {
4106 if (frag->fr_type == rs_cfa)
4107 {
4108 expressionS exp;
4109 expressionS *symval;
4110
4111 symval = symbol_get_value_expression (frag->fr_symbol);
4112 exp.X_op = O_subtract;
4113 exp.X_add_symbol = symval->X_add_symbol;
4114 exp.X_add_number = 0;
4115 exp.X_op_symbol = symval->X_op_symbol;
4116
4117 /* We must set the segment before creating a frag after all
4118 frag chains have been chained together. */
4119 subseg_set (s, frch->frch_subseg);
4120
4121 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
4122 BFD_RELOC_RISCV_CFA);
4123 }
4124 }
4125 }
4126
4127 /* Restore the original segment info. */
4128 subseg_set (seg, subseg);
4129 }
4130
4131 /* Handle the .option pseudo-op. */
4132
4133 static void
4134 s_riscv_option (int x ATTRIBUTE_UNUSED)
4135 {
4136 char *name = input_line_pointer, ch;
4137
4138 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4139 ++input_line_pointer;
4140 ch = *input_line_pointer;
4141 *input_line_pointer = '\0';
4142
4143 if (strcmp (name, "rvc") == 0)
4144 {
4145 riscv_update_subset (&riscv_rps_as, "+c");
4146 riscv_reset_subsets_list_arch_str ();
4147 riscv_set_rvc (true);
4148 }
4149 else if (strcmp (name, "norvc") == 0)
4150 {
4151 riscv_update_subset (&riscv_rps_as, "-c");
4152 riscv_reset_subsets_list_arch_str ();
4153 riscv_set_rvc (false);
4154 }
4155 else if (strcmp (name, "pic") == 0)
4156 riscv_opts.pic = true;
4157 else if (strcmp (name, "nopic") == 0)
4158 riscv_opts.pic = false;
4159 else if (strcmp (name, "relax") == 0)
4160 riscv_opts.relax = true;
4161 else if (strcmp (name, "norelax") == 0)
4162 riscv_opts.relax = false;
4163 else if (strcmp (name, "csr-check") == 0)
4164 riscv_opts.csr_check = true;
4165 else if (strcmp (name, "no-csr-check") == 0)
4166 riscv_opts.csr_check = false;
4167 else if (strncmp (name, "arch,", 5) == 0)
4168 {
4169 name += 5;
4170 if (ISSPACE (*name) && *name != '\0')
4171 name++;
4172 riscv_update_subset (&riscv_rps_as, name);
4173 riscv_reset_subsets_list_arch_str ();
4174
4175 riscv_set_rvc (false);
4176 if (riscv_subset_supports (&riscv_rps_as, "c"))
4177 riscv_set_rvc (true);
4178
4179 if (riscv_subset_supports (&riscv_rps_as, "ztso"))
4180 riscv_set_tso ();
4181 }
4182 else if (strcmp (name, "push") == 0)
4183 {
4184 struct riscv_option_stack *s;
4185
4186 s = XNEW (struct riscv_option_stack);
4187 s->next = riscv_opts_stack;
4188 s->options = riscv_opts;
4189 s->subset_list = riscv_rps_as.subset_list;
4190 riscv_opts_stack = s;
4191 riscv_rps_as.subset_list = riscv_copy_subset_list (s->subset_list);
4192 }
4193 else if (strcmp (name, "pop") == 0)
4194 {
4195 struct riscv_option_stack *s;
4196
4197 s = riscv_opts_stack;
4198 if (s == NULL)
4199 as_bad (_(".option pop with no .option push"));
4200 else
4201 {
4202 riscv_subset_list_t *release_subsets = riscv_rps_as.subset_list;
4203 riscv_opts_stack = s->next;
4204 riscv_opts = s->options;
4205 riscv_rps_as.subset_list = s->subset_list;
4206 riscv_release_subset_list (release_subsets);
4207 free (s);
4208 }
4209 }
4210 else
4211 {
4212 as_warn (_("unrecognized .option directive: %s"), name);
4213 }
4214 *input_line_pointer = ch;
4215 demand_empty_rest_of_line ();
4216 }
4217
4218 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
4219 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
4220 use in DWARF debug information. */
4221
4222 static void
4223 s_dtprel (int bytes)
4224 {
4225 expressionS ex;
4226 char *p;
4227
4228 expression (&ex);
4229
4230 if (ex.X_op != O_symbol)
4231 {
4232 as_bad (_("unsupported use of %s"), (bytes == 8
4233 ? ".dtpreldword"
4234 : ".dtprelword"));
4235 ignore_rest_of_line ();
4236 }
4237
4238 p = frag_more (bytes);
4239 md_number_to_chars (p, 0, bytes);
4240 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false,
4241 (bytes == 8
4242 ? BFD_RELOC_RISCV_TLS_DTPREL64
4243 : BFD_RELOC_RISCV_TLS_DTPREL32));
4244
4245 demand_empty_rest_of_line ();
4246 }
4247
4248 /* Handle the .bss pseudo-op. */
4249
4250 static void
4251 s_bss (int ignore ATTRIBUTE_UNUSED)
4252 {
4253 subseg_set (bss_section, 0);
4254 demand_empty_rest_of_line ();
4255 }
4256
4257 static void
4258 riscv_make_nops (char *buf, bfd_vma bytes)
4259 {
4260 bfd_vma i = 0;
4261
4262 /* RISC-V instructions cannot begin or end on odd addresses, so this case
4263 means we are not within a valid instruction sequence. It is thus safe
4264 to use a zero byte, even though that is not a valid instruction. */
4265 if (bytes % 2 == 1)
4266 buf[i++] = 0;
4267
4268 /* Use at most one 2-byte NOP. */
4269 if ((bytes - i) % 4 == 2)
4270 {
4271 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
4272 i += 2;
4273 }
4274
4275 /* Fill the remainder with 4-byte NOPs. */
4276 for ( ; i < bytes; i += 4)
4277 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
4278 }
4279
4280 /* Called from md_do_align. Used to create an alignment frag in a
4281 code section by emitting a worst-case NOP sequence that the linker
4282 will later relax to the correct number of NOPs. We can't compute
4283 the correct alignment now because of other linker relaxations. */
4284
4285 bool
4286 riscv_frag_align_code (int n)
4287 {
4288 bfd_vma bytes = (bfd_vma) 1 << n;
4289 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
4290 bfd_vma worst_case_bytes = bytes - insn_alignment;
4291 char *nops;
4292 expressionS ex;
4293
4294 /* If we are moving to a smaller alignment than the instruction size, then no
4295 alignment is required. */
4296 if (bytes <= insn_alignment)
4297 return true;
4298
4299 /* When not relaxing, riscv_handle_align handles code alignment. */
4300 if (!riscv_opts.relax)
4301 return false;
4302
4303 /* Maybe we should use frag_var to create a new rs_align_code fragment,
4304 rather than just use frag_more to handle an alignment here? So that we
4305 don't need to call riscv_mapping_state again later, and then only need
4306 to check frag->fr_type to see if it is frag_align_code. */
4307 nops = frag_more (worst_case_bytes);
4308
4309 ex.X_op = O_constant;
4310 ex.X_add_number = worst_case_bytes;
4311
4312 riscv_make_nops (nops, worst_case_bytes);
4313
4314 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
4315 &ex, false, BFD_RELOC_RISCV_ALIGN);
4316
4317 riscv_mapping_state (MAP_INSN, worst_case_bytes, true/* fr_align_code */);
4318
4319 /* We need to start a new frag after the alignment which may be removed by
4320 the linker, to prevent the assembler from computing static offsets.
4321 This is necessary to get correct EH info. */
4322 frag_wane (frag_now);
4323 frag_new (0);
4324
4325 return true;
4326 }
4327
4328 /* Implement HANDLE_ALIGN. */
4329
4330 void
4331 riscv_handle_align (fragS *fragP)
4332 {
4333 switch (fragP->fr_type)
4334 {
4335 case rs_align_code:
4336 /* When relaxing, riscv_frag_align_code handles code alignment. */
4337 if (!riscv_opts.relax)
4338 {
4339 bfd_signed_vma bytes = (fragP->fr_next->fr_address
4340 - fragP->fr_address - fragP->fr_fix);
4341 /* We have 4 byte uncompressed nops. */
4342 bfd_signed_vma size = 4;
4343 bfd_signed_vma excess = bytes % size;
4344 bfd_boolean odd_padding = (excess % 2 == 1);
4345 char *p = fragP->fr_literal + fragP->fr_fix;
4346
4347 if (bytes <= 0)
4348 break;
4349
4350 /* Insert zeros or compressed nops to get 4 byte alignment. */
4351 if (excess)
4352 {
4353 if (odd_padding)
4354 riscv_add_odd_padding_symbol (fragP);
4355 riscv_make_nops (p, excess);
4356 fragP->fr_fix += excess;
4357 p += excess;
4358 }
4359
4360 /* The frag will be changed to `rs_fill` later. The function
4361 `write_contents` will try to fill the remaining spaces
4362 according to the patterns we give. In this case, we give
4363 a 4 byte uncompressed nop as the pattern, and set the size
4364 of the pattern into `fr_var`. The nop will be output to the
4365 file `fr_offset` times. However, `fr_offset` could be zero
4366 if we don't need to pad the boundary finally. */
4367 riscv_make_nops (p, size);
4368 fragP->fr_var = size;
4369 }
4370 break;
4371
4372 default:
4373 break;
4374 }
4375 }
4376
4377 /* This usually called from frag_var. */
4378
4379 void
4380 riscv_init_frag (fragS * fragP, int max_chars)
4381 {
4382 /* Do not add mapping symbol to debug sections. */
4383 if (bfd_section_flags (now_seg) & SEC_DEBUGGING)
4384 return;
4385
4386 switch (fragP->fr_type)
4387 {
4388 case rs_fill:
4389 case rs_align:
4390 case rs_align_test:
4391 riscv_mapping_state (MAP_DATA, max_chars, false/* fr_align_code */);
4392 break;
4393 case rs_align_code:
4394 riscv_mapping_state (MAP_INSN, max_chars, true/* fr_align_code */);
4395 break;
4396 default:
4397 break;
4398 }
4399 }
4400
4401 int
4402 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
4403 {
4404 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, false));
4405 }
4406
4407 /* Translate internal representation of relocation info to BFD target
4408 format. */
4409
4410 arelent *
4411 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
4412 {
4413 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
4414
4415 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
4416 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
4417 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4418 reloc->addend = fixp->fx_addnumber;
4419
4420 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
4421 if (reloc->howto == NULL)
4422 {
4423 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
4424 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
4425 {
4426 /* We don't have R_RISCV_8/16, but for this special case,
4427 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
4428 return reloc;
4429 }
4430
4431 as_bad_where (fixp->fx_file, fixp->fx_line,
4432 _("cannot represent %s relocation in object file"),
4433 bfd_get_reloc_code_name (fixp->fx_r_type));
4434 return NULL;
4435 }
4436
4437 return reloc;
4438 }
4439
4440 int
4441 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
4442 {
4443 if (RELAX_BRANCH_P (fragp->fr_subtype))
4444 {
4445 offsetT old_var = fragp->fr_var;
4446 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
4447 return fragp->fr_var - old_var;
4448 }
4449
4450 return 0;
4451 }
4452
4453 /* Expand far branches to multi-instruction sequences. */
4454
4455 static void
4456 md_convert_frag_branch (fragS *fragp)
4457 {
4458 bfd_byte *buf;
4459 expressionS exp;
4460 fixS *fixp;
4461 insn_t insn;
4462 int rs1, reloc;
4463
4464 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
4465
4466 exp.X_op = O_symbol;
4467 exp.X_add_symbol = fragp->fr_symbol;
4468 exp.X_add_number = fragp->fr_offset;
4469
4470 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
4471
4472 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
4473 {
4474 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4475 {
4476 case 8:
4477 case 4:
4478 /* Expand the RVC branch into a RISC-V one. */
4479 insn = bfd_getl16 (buf);
4480 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
4481 if ((insn & MASK_C_J) == MATCH_C_J)
4482 insn = MATCH_JAL;
4483 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
4484 insn = MATCH_JAL | (X_RA << OP_SH_RD);
4485 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
4486 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
4487 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
4488 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
4489 else
4490 abort ();
4491 bfd_putl32 (insn, buf);
4492 break;
4493
4494 case 6:
4495 /* Invert the branch condition. Branch over the jump. */
4496 insn = bfd_getl16 (buf);
4497 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
4498 insn |= ENCODE_CBTYPE_IMM (6);
4499 bfd_putl16 (insn, buf);
4500 buf += 2;
4501 goto jump;
4502
4503 case 2:
4504 /* Just keep the RVC branch. */
4505 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4506 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
4507 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4508 2, &exp, false, reloc);
4509 buf += 2;
4510 goto done;
4511
4512 default:
4513 abort ();
4514 }
4515 }
4516
4517 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
4518 {
4519 case 8:
4520 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
4521
4522 /* Invert the branch condition. Branch over the jump. */
4523 insn = bfd_getl32 (buf);
4524 insn ^= MATCH_BEQ ^ MATCH_BNE;
4525 insn |= ENCODE_BTYPE_IMM (8);
4526 bfd_putl32 (insn, buf);
4527 buf += 4;
4528
4529 jump:
4530 /* Jump to the target. */
4531 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4532 4, &exp, false, BFD_RELOC_RISCV_JMP);
4533 bfd_putl32 (MATCH_JAL, buf);
4534 buf += 4;
4535 break;
4536
4537 case 4:
4538 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
4539 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
4540 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
4541 4, &exp, false, reloc);
4542 buf += 4;
4543 break;
4544
4545 default:
4546 abort ();
4547 }
4548
4549 done:
4550 fixp->fx_file = fragp->fr_file;
4551 fixp->fx_line = fragp->fr_line;
4552
4553 gas_assert (buf == (bfd_byte *)fragp->fr_literal
4554 + fragp->fr_fix + fragp->fr_var);
4555
4556 fragp->fr_fix += fragp->fr_var;
4557 }
4558
4559 /* Relax a machine dependent frag. This returns the amount by which
4560 the current size of the frag should change. */
4561
4562 void
4563 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
4564 fragS *fragp)
4565 {
4566 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
4567 md_convert_frag_branch (fragp);
4568 }
4569
4570 void
4571 md_show_usage (FILE *stream)
4572 {
4573 fprintf (stream, _("\
4574 RISC-V options:\n\
4575 -fpic or -fPIC generate position-independent code\n\
4576 -fno-pic don't generate position-independent code (default)\n\
4577 -march=ISA set the RISC-V architecture\n\
4578 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
4579 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9.1, 1.10, 1.11, 1.12)\n\
4580 -mabi=ABI set the RISC-V ABI\n\
4581 -mrelax enable relax (default)\n\
4582 -mno-relax disable relax\n\
4583 -march-attr generate RISC-V arch attribute\n\
4584 -mno-arch-attr don't generate RISC-V arch attribute\n\
4585 -mcsr-check enable the csr ISA and privilege spec version checks\n\
4586 -mno-csr-check disable the csr ISA and privilege spec version checks (default)\n\
4587 -mbig-endian assemble for big-endian\n\
4588 -mlittle-endian assemble for little-endian\n\
4589 "));
4590 }
4591
4592 /* Standard calling conventions leave the CFA at SP on entry. */
4593
4594 void
4595 riscv_cfi_frame_initial_instructions (void)
4596 {
4597 cfi_add_CFA_def_cfa_register (X_SP);
4598 }
4599
4600 int
4601 tc_riscv_regname_to_dw2regnum (char *regname)
4602 {
4603 int reg;
4604
4605 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
4606 return reg;
4607
4608 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
4609 return reg + 32;
4610
4611 if ((reg = reg_lookup_internal (regname, RCLASS_VECR)) >= 0)
4612 return reg + 96;
4613
4614 /* CSRs are numbered 4096 -> 8191. */
4615 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
4616 return reg + 4096;
4617
4618 as_bad (_("unknown register `%s'"), regname);
4619 return -1;
4620 }
4621
4622 void
4623 riscv_elf_final_processing (void)
4624 {
4625 riscv_set_abi_by_arch ();
4626 riscv_release_subset_list (riscv_rps_as.subset_list);
4627 elf_elfheader (stdoutput)->e_flags |= elf_flags;
4628 }
4629
4630 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
4631 since these directives break relaxation when used with symbol deltas. */
4632
4633 static void
4634 s_riscv_leb128 (int sign)
4635 {
4636 expressionS exp;
4637 char *save_in = input_line_pointer;
4638
4639 expression (&exp);
4640 if (exp.X_op != O_constant)
4641 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
4642 demand_empty_rest_of_line ();
4643
4644 input_line_pointer = save_in;
4645 return s_leb128 (sign);
4646 }
4647
4648 /* Parse the .insn directive. There are three formats,
4649 Format 1: .insn <type> <operand1>, <operand2>, ...
4650 Format 2: .insn <length>, <value>
4651 Format 3: .insn <value>. */
4652
4653 static void
4654 s_riscv_insn (int x ATTRIBUTE_UNUSED)
4655 {
4656 char *str = input_line_pointer;
4657 struct riscv_cl_insn insn;
4658 expressionS imm_expr;
4659 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
4660 char save_c;
4661
4662 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4663 ++input_line_pointer;
4664
4665 save_c = *input_line_pointer;
4666 *input_line_pointer = '\0';
4667
4668 riscv_mapping_state (MAP_INSN, 0, false/* fr_align_code */);
4669
4670 struct riscv_ip_error error = riscv_ip (str, &insn, &imm_expr,
4671 &imm_reloc, insn_type_hash);
4672 if (error.msg)
4673 {
4674 char *save_in = input_line_pointer;
4675 error.msg = riscv_ip_hardcode (str, &insn, &imm_expr, error.msg);
4676 input_line_pointer = save_in;
4677 }
4678
4679 if (error.msg)
4680 {
4681 if (error.missing_ext)
4682 as_bad ("%s `%s', extension `%s' required", error.msg, error.statement,
4683 error.missing_ext);
4684 else
4685 as_bad ("%s `%s'", error.msg, error.statement);
4686 }
4687 else
4688 {
4689 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
4690 append_insn (&insn, &imm_expr, imm_reloc);
4691 }
4692
4693 *input_line_pointer = save_c;
4694 demand_empty_rest_of_line ();
4695 }
4696
4697 /* Update architecture and privileged elf attributes. If we don't set
4698 them, then try to output the default ones. */
4699
4700 static void
4701 riscv_write_out_attrs (void)
4702 {
4703 const char *arch_str, *priv_str, *p;
4704 /* versions[0]: major version.
4705 versions[1]: minor version.
4706 versions[2]: revision version. */
4707 unsigned versions[3] = {0}, number = 0;
4708 unsigned int i;
4709
4710 /* Re-write architecture elf attribute. */
4711 arch_str = riscv_rps_as.subset_list->arch_str;
4712 bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
4713
4714 /* For the file without any instruction, we don't set the default_priv_spec
4715 according to the privileged elf attributes since the md_assemble isn't
4716 called. */
4717 if (!start_assemble
4718 && !riscv_set_default_priv_spec (NULL))
4719 return;
4720
4721 /* If we already have set privileged elf attributes, then no need to do
4722 anything. Otherwise, don't generate or update them when no CSR and
4723 privileged instructions are used. */
4724 if (!explicit_priv_attr)
4725 return;
4726
4727 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
4728 p = priv_str;
4729 for (i = 0; *p; ++p)
4730 {
4731 if (*p == '.' && i < 3)
4732 {
4733 versions[i++] = number;
4734 number = 0;
4735 }
4736 else if (ISDIGIT (*p))
4737 number = (number * 10) + (*p - '0');
4738 else
4739 {
4740 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
4741 return;
4742 }
4743 }
4744 versions[i] = number;
4745
4746 /* Re-write privileged elf attributes. */
4747 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
4748 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
4749 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
4750 }
4751
4752 /* Add the default contents for the .riscv.attributes section. */
4753
4754 static void
4755 riscv_set_public_attributes (void)
4756 {
4757 if (riscv_opts.arch_attr || explicit_attr)
4758 riscv_write_out_attrs ();
4759 }
4760
4761 /* Called after all assembly has been done. */
4762
4763 void
4764 riscv_md_finish (void)
4765 {
4766 riscv_set_public_attributes ();
4767 }
4768
4769 /* Adjust the symbol table. */
4770
4771 void
4772 riscv_adjust_symtab (void)
4773 {
4774 bfd_map_over_sections (stdoutput, riscv_check_mapping_symbols, (char *) 0);
4775 elf_adjust_symtab ();
4776 }
4777
4778 /* Given a symbolic attribute NAME, return the proper integer value.
4779 Returns -1 if the attribute is not known. */
4780
4781 int
4782 riscv_convert_symbolic_attribute (const char *name)
4783 {
4784 static const struct
4785 {
4786 const char *name;
4787 const int tag;
4788 }
4789 attribute_table[] =
4790 {
4791 /* When you modify this table you should
4792 also modify the list in doc/c-riscv.texi. */
4793 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
4794 T(arch),
4795 T(priv_spec),
4796 T(priv_spec_minor),
4797 T(priv_spec_revision),
4798 T(unaligned_access),
4799 T(stack_align),
4800 #undef T
4801 };
4802
4803 if (name == NULL)
4804 return -1;
4805
4806 unsigned int i;
4807 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
4808 if (strcmp (name, attribute_table[i].name) == 0)
4809 return attribute_table[i].tag;
4810
4811 return -1;
4812 }
4813
4814 /* Parse a .attribute directive. */
4815
4816 static void
4817 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
4818 {
4819 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
4820 unsigned old_xlen;
4821 obj_attribute *attr;
4822
4823 explicit_attr = true;
4824 switch (tag)
4825 {
4826 case Tag_RISCV_arch:
4827 old_xlen = xlen;
4828 attr = elf_known_obj_attributes_proc (stdoutput);
4829 if (!start_assemble)
4830 riscv_set_arch (attr[Tag_RISCV_arch].s);
4831 else
4832 as_fatal (_("architecture elf attributes must set before "
4833 "any instructions"));
4834
4835 if (old_xlen != xlen)
4836 {
4837 /* We must re-init bfd again if xlen is changed. */
4838 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
4839 bfd_find_target (riscv_target_format (), stdoutput);
4840
4841 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
4842 as_warn (_("could not set architecture and machine"));
4843 }
4844 break;
4845
4846 case Tag_RISCV_priv_spec:
4847 case Tag_RISCV_priv_spec_minor:
4848 case Tag_RISCV_priv_spec_revision:
4849 if (start_assemble)
4850 as_fatal (_("privileged elf attributes must set before "
4851 "any instructions"));
4852 break;
4853
4854 default:
4855 break;
4856 }
4857 }
4858
4859 /* Mark symbol that it follows a variant CC convention. */
4860
4861 static void
4862 s_variant_cc (int ignored ATTRIBUTE_UNUSED)
4863 {
4864 char *name;
4865 char c;
4866 symbolS *sym;
4867 asymbol *bfdsym;
4868 elf_symbol_type *elfsym;
4869
4870 c = get_symbol_name (&name);
4871 if (!*name)
4872 as_bad (_("missing symbol name for .variant_cc directive"));
4873 sym = symbol_find_or_make (name);
4874 restore_line_pointer (c);
4875 demand_empty_rest_of_line ();
4876
4877 bfdsym = symbol_get_bfdsym (sym);
4878 elfsym = elf_symbol_from (bfdsym);
4879 gas_assert (elfsym);
4880 elfsym->internal_elf_sym.st_other |= STO_RISCV_VARIANT_CC;
4881 }
4882
4883 /* Same as elf_copy_symbol_attributes, but without copying st_other.
4884 This is needed so RISC-V specific st_other values can be independently
4885 specified for an IFUNC resolver (that is called by the dynamic linker)
4886 and the symbol it resolves (aliased to the resolver). In particular,
4887 if a function symbol has special st_other value set via directives,
4888 then attaching an IFUNC resolver to that symbol should not override
4889 the st_other setting. Requiring the directive on the IFUNC resolver
4890 symbol would be unexpected and problematic in C code, where the two
4891 symbols appear as two independent function declarations. */
4892
4893 void
4894 riscv_elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
4895 {
4896 struct elf_obj_sy *srcelf = symbol_get_obj (src);
4897 struct elf_obj_sy *destelf = symbol_get_obj (dest);
4898 /* If size is unset, copy size from src. Because we don't track whether
4899 .size has been used, we can't differentiate .size dest, 0 from the case
4900 where dest's size is unset. */
4901 if (!destelf->size && S_GET_SIZE (dest) == 0)
4902 {
4903 if (srcelf->size)
4904 {
4905 destelf->size = XNEW (expressionS);
4906 *destelf->size = *srcelf->size;
4907 }
4908 S_SET_SIZE (dest, S_GET_SIZE (src));
4909 }
4910 }
4911
4912 /* RISC-V pseudo-ops table. */
4913 static const pseudo_typeS riscv_pseudo_table[] =
4914 {
4915 {"option", s_riscv_option, 0},
4916 {"half", cons, 2},
4917 {"word", cons, 4},
4918 {"dword", cons, 8},
4919 {"dtprelword", s_dtprel, 4},
4920 {"dtpreldword", s_dtprel, 8},
4921 {"bss", s_bss, 0},
4922 {"uleb128", s_riscv_leb128, 0},
4923 {"sleb128", s_riscv_leb128, 1},
4924 {"insn", s_riscv_insn, 0},
4925 {"attribute", s_riscv_attribute, 0},
4926 {"variant_cc", s_variant_cc, 0},
4927 {"float16", float_cons, 'h'},
4928
4929 { NULL, NULL, 0 },
4930 };
4931
4932 void
4933 riscv_pop_insert (void)
4934 {
4935 extern void pop_insert (const pseudo_typeS *);
4936
4937 pop_insert (riscv_pseudo_table);
4938 }