]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/tc-riscv.c
RISC-V: Add bfd/cpu-riscv.h to support all spec versions controlling.
[thirdparty/binutils-gdb.git] / gas / config / tc-riscv.c
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2021 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/cpu-riscv.h"
33 #include "bfd/elfxx-riscv.h"
34 #include "elf/riscv.h"
35 #include "opcode/riscv.h"
36
37 #include <stdint.h>
38
39 /* Information about an instruction, including its format, operands
40 and fixups. */
41 struct riscv_cl_insn
42 {
43 /* The opcode's entry in riscv_opcodes. */
44 const struct riscv_opcode *insn_mo;
45
46 /* The encoded instruction bits. */
47 insn_t insn_opcode;
48
49 /* The frag that contains the instruction. */
50 struct frag *frag;
51
52 /* The offset into FRAG of the first instruction byte. */
53 long where;
54
55 /* The relocs associated with the instruction, if any. */
56 fixS *fixp;
57 };
58
59 /* All RISC-V CSR belong to one of these classes. */
60 enum riscv_csr_class
61 {
62 CSR_CLASS_NONE,
63
64 CSR_CLASS_I,
65 CSR_CLASS_I_32, /* rv32 only */
66 CSR_CLASS_F, /* f-ext only */
67 CSR_CLASS_DEBUG /* debug CSR */
68 };
69
70 /* This structure holds all restricted conditions for a CSR. */
71 struct riscv_csr_extra
72 {
73 /* Class to which this CSR belongs. Used to decide whether or
74 not this CSR is legal in the current -march context. */
75 enum riscv_csr_class csr_class;
76
77 /* CSR may have differnet numbers in the previous priv spec. */
78 unsigned address;
79
80 /* Record the CSR is defined/valid in which versions. */
81 enum riscv_spec_class define_version;
82
83 /* Record the CSR is aborted/invalid from which versions. If it isn't
84 aborted in the current version, then it should be CSR_CLASS_VDRAFT. */
85 enum riscv_spec_class abort_version;
86
87 /* The CSR may have more than one setting. */
88 struct riscv_csr_extra *next;
89 };
90
91 /* All standard/Z* extensions defined in all supported ISA spec. */
92 struct riscv_ext_version
93 {
94 const char *name;
95 enum riscv_spec_class isa_spec_class;
96 int major_version;
97 int minor_version;
98 };
99
100 static const struct riscv_ext_version ext_version_table[] =
101 {
102 {"e", ISA_SPEC_CLASS_20191213, 1, 9},
103 {"e", ISA_SPEC_CLASS_20190608, 1, 9},
104 {"e", ISA_SPEC_CLASS_2P2, 1, 9},
105
106 {"i", ISA_SPEC_CLASS_20191213, 2, 1},
107 {"i", ISA_SPEC_CLASS_20190608, 2, 1},
108 {"i", ISA_SPEC_CLASS_2P2, 2, 0},
109
110 {"m", ISA_SPEC_CLASS_20191213, 2, 0},
111 {"m", ISA_SPEC_CLASS_20190608, 2, 0},
112 {"m", ISA_SPEC_CLASS_2P2, 2, 0},
113
114 {"a", ISA_SPEC_CLASS_20191213, 2, 1},
115 {"a", ISA_SPEC_CLASS_20190608, 2, 0},
116 {"a", ISA_SPEC_CLASS_2P2, 2, 0},
117
118 {"f", ISA_SPEC_CLASS_20191213, 2, 2},
119 {"f", ISA_SPEC_CLASS_20190608, 2, 2},
120 {"f", ISA_SPEC_CLASS_2P2, 2, 0},
121
122 {"d", ISA_SPEC_CLASS_20191213, 2, 2},
123 {"d", ISA_SPEC_CLASS_20190608, 2, 2},
124 {"d", ISA_SPEC_CLASS_2P2, 2, 0},
125
126 {"q", ISA_SPEC_CLASS_20191213, 2, 2},
127 {"q", ISA_SPEC_CLASS_20190608, 2, 2},
128 {"q", ISA_SPEC_CLASS_2P2, 2, 0},
129
130 {"c", ISA_SPEC_CLASS_20191213, 2, 0},
131 {"c", ISA_SPEC_CLASS_20190608, 2, 0},
132 {"c", ISA_SPEC_CLASS_2P2, 2, 0},
133
134 {"zicsr", ISA_SPEC_CLASS_20191213, 2, 0},
135 {"zicsr", ISA_SPEC_CLASS_20190608, 2, 0},
136
137 {"zifencei", ISA_SPEC_CLASS_20191213, 2, 0},
138 {"zifencei", ISA_SPEC_CLASS_20190608, 2, 0},
139
140 {"zihintpause", ISA_SPEC_CLASS_DRAFT, 1, 0},
141
142 /* Terminate the list. */
143 {NULL, 0, 0, 0}
144 };
145
146 #ifndef DEFAULT_ARCH
147 #define DEFAULT_ARCH "riscv64"
148 #endif
149
150 #ifndef DEFAULT_RISCV_ATTR
151 #define DEFAULT_RISCV_ATTR 0
152 #endif
153
154 /* Let riscv_after_parse_args set the default value according to xlen. */
155 #ifndef DEFAULT_RISCV_ARCH_WITH_EXT
156 #define DEFAULT_RISCV_ARCH_WITH_EXT NULL
157 #endif
158
159 /* Need to sync the version with RISC-V compiler. */
160 #ifndef DEFAULT_RISCV_ISA_SPEC
161 #define DEFAULT_RISCV_ISA_SPEC "2.2"
162 #endif
163
164 #ifndef DEFAULT_RISCV_PRIV_SPEC
165 #define DEFAULT_RISCV_PRIV_SPEC "1.11"
166 #endif
167
168 static const char default_arch[] = DEFAULT_ARCH;
169 static const char *default_arch_with_ext = DEFAULT_RISCV_ARCH_WITH_EXT;
170 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_NONE;
171 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
172
173 static unsigned xlen = 0; /* The width of an x-register. */
174 static unsigned abi_xlen = 0; /* The width of a pointer in the ABI. */
175 static bfd_boolean rve_abi = FALSE;
176 enum float_abi
177 {
178 FLOAT_ABI_DEFAULT = -1,
179 FLOAT_ABI_SOFT,
180 FLOAT_ABI_SINGLE,
181 FLOAT_ABI_DOUBLE,
182 FLOAT_ABI_QUAD
183 };
184 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
185
186 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
187 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
188
189 static unsigned elf_flags = 0;
190
191 /* Set the default_isa_spec. Return 0 if the spec isn't supported.
192 Otherwise, return 1. */
193
194 static int
195 riscv_set_default_isa_spec (const char *s)
196 {
197 enum riscv_spec_class class = ISA_SPEC_CLASS_NONE;
198 RISCV_GET_ISA_SPEC_CLASS (s, class);
199 if (class == ISA_SPEC_CLASS_NONE)
200 {
201 as_bad ("unknown default ISA spec `%s' set by "
202 "-misa-spec or --with-isa-spec", s);
203 return 0;
204 }
205 else
206 default_isa_spec = class;
207 return 1;
208 }
209
210 /* Set the default_priv_spec. Find the privileged elf attributes when
211 the input string is NULL. Return 0 if the spec isn't supported.
212 Otherwise, return 1. */
213
214 static int
215 riscv_set_default_priv_spec (const char *s)
216 {
217 enum riscv_spec_class class = PRIV_SPEC_CLASS_NONE;
218 unsigned major, minor, revision;
219 obj_attribute *attr;
220
221 RISCV_GET_PRIV_SPEC_CLASS (s, class);
222 if (class != PRIV_SPEC_CLASS_NONE)
223 {
224 default_priv_spec = class;
225 return 1;
226 }
227
228 if (s != NULL)
229 {
230 as_bad (_("unknown default privileged spec `%s' set by "
231 "-mpriv-spec or --with-priv-spec"), s);
232 return 0;
233 }
234
235 /* Set the default_priv_spec by the privileged elf attributes. */
236 attr = elf_known_obj_attributes_proc (stdoutput);
237 major = (unsigned) attr[Tag_RISCV_priv_spec].i;
238 minor = (unsigned) attr[Tag_RISCV_priv_spec_minor].i;
239 revision = (unsigned) attr[Tag_RISCV_priv_spec_revision].i;
240 /* Version 0.0.0 is the default value and meningless. */
241 if (major == 0 && minor == 0 && revision == 0)
242 return 1;
243
244 riscv_get_priv_spec_class_from_numbers (major, minor, revision, &class);
245 if (class != PRIV_SPEC_CLASS_NONE)
246 {
247 default_priv_spec = class;
248 return 1;
249 }
250
251 /* Still can not find the privileged spec class. */
252 as_bad (_("unknown default privileged spec `%d.%d.%d' set by "
253 "privileged elf attributes"), major, minor, revision);
254 return 0;
255 }
256
257 /* This is the set of options which the .option pseudo-op may modify. */
258 struct riscv_set_options
259 {
260 int pic; /* Generate position-independent code. */
261 int rvc; /* Generate RVC code. */
262 int rve; /* Generate RVE code. */
263 int relax; /* Emit relocs the linker is allowed to relax. */
264 int arch_attr; /* Emit architecture and privileged elf attributes. */
265 int csr_check; /* Enable the CSR checking. */
266 };
267
268 static struct riscv_set_options riscv_opts =
269 {
270 0, /* pic */
271 0, /* rvc */
272 0, /* rve */
273 1, /* relax */
274 DEFAULT_RISCV_ATTR, /* arch_attr */
275 0, /* csr_check */
276 };
277
278 static void
279 riscv_set_rvc (bfd_boolean rvc_value)
280 {
281 if (rvc_value)
282 elf_flags |= EF_RISCV_RVC;
283
284 riscv_opts.rvc = rvc_value;
285 }
286
287 static void
288 riscv_set_rve (bfd_boolean rve_value)
289 {
290 riscv_opts.rve = rve_value;
291 }
292
293 static riscv_subset_list_t riscv_subsets;
294
295 static bfd_boolean
296 riscv_subset_supports (const char *feature)
297 {
298 struct riscv_subset_t *subset;
299
300 if (riscv_opts.rvc && (strcasecmp (feature, "c") == 0))
301 return TRUE;
302
303 return riscv_lookup_subset (&riscv_subsets, feature, &subset);
304 }
305
306 static bfd_boolean
307 riscv_multi_subset_supports (enum riscv_insn_class insn_class)
308 {
309 switch (insn_class)
310 {
311 case INSN_CLASS_I: return riscv_subset_supports ("i");
312 case INSN_CLASS_C: return riscv_subset_supports ("c");
313 case INSN_CLASS_A: return riscv_subset_supports ("a");
314 case INSN_CLASS_M: return riscv_subset_supports ("m");
315 case INSN_CLASS_F: return riscv_subset_supports ("f");
316 case INSN_CLASS_D: return riscv_subset_supports ("d");
317 case INSN_CLASS_Q: return riscv_subset_supports ("q");
318
319 case INSN_CLASS_F_AND_C:
320 return (riscv_subset_supports ("f")
321 && riscv_subset_supports ("c"));
322 case INSN_CLASS_D_AND_C:
323 return (riscv_subset_supports ("d")
324 && riscv_subset_supports ("c"));
325
326 case INSN_CLASS_ZICSR:
327 return riscv_subset_supports ("zicsr");
328 case INSN_CLASS_ZIFENCEI:
329 return riscv_subset_supports ("zifencei");
330 case INSN_CLASS_ZIHINTPAUSE:
331 return riscv_subset_supports ("zihintpause");
332
333 default:
334 as_fatal ("internal: unreachable");
335 return FALSE;
336 }
337 }
338
339 /* Handle of the extension with version hash table. */
340 static htab_t ext_version_hash = NULL;
341
342 static htab_t
343 init_ext_version_hash (void)
344 {
345 const struct riscv_ext_version *table = ext_version_table;
346 htab_t hash = str_htab_create ();
347 int i = 0;
348
349 while (table[i].name)
350 {
351 const char *name = table[i].name;
352 if (str_hash_insert (hash, name, &table[i], 0) != NULL)
353 as_fatal (_("internal: duplicate %s"), name);
354
355 i++;
356 while (table[i].name
357 && strcmp (table[i].name, name) == 0)
358 i++;
359 }
360
361 return hash;
362 }
363
364 static void
365 riscv_get_default_ext_version (const char *name,
366 int *major_version,
367 int *minor_version)
368 {
369 struct riscv_ext_version *ext;
370
371 if (name == NULL || default_isa_spec == ISA_SPEC_CLASS_NONE)
372 return;
373
374 ext = (struct riscv_ext_version *) str_hash_find (ext_version_hash, name);
375 while (ext
376 && ext->name
377 && strcmp (ext->name, name) == 0)
378 {
379 if (ext->isa_spec_class == ISA_SPEC_CLASS_DRAFT
380 || ext->isa_spec_class == default_isa_spec)
381 {
382 *major_version = ext->major_version;
383 *minor_version = ext->minor_version;
384 return;
385 }
386 ext++;
387 }
388 }
389
390 /* Set which ISA and extensions are available. */
391
392 static void
393 riscv_set_arch (const char *s)
394 {
395 riscv_parse_subset_t rps;
396 rps.subset_list = &riscv_subsets;
397 rps.error_handler = as_bad;
398 rps.xlen = &xlen;
399 rps.get_default_version = riscv_get_default_ext_version;
400
401 if (s == NULL)
402 return;
403
404 riscv_release_subset_list (&riscv_subsets);
405 riscv_parse_subset (&rps, s);
406 }
407
408 /* Indicate -mabi option is explictly set. */
409 static bfd_boolean explicit_mabi = FALSE;
410
411 static void
412 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bfd_boolean rve)
413 {
414 abi_xlen = new_xlen;
415 float_abi = new_float_abi;
416 rve_abi = rve;
417 }
418
419 /* If the -mabi option isn't set, then set the abi according to the
420 ISA string. Otherwise, check if there is any conflict. */
421
422 static void
423 riscv_set_abi_by_arch (void)
424 {
425 if (!explicit_mabi)
426 {
427 if (riscv_subset_supports ("q"))
428 riscv_set_abi (xlen, FLOAT_ABI_QUAD, FALSE);
429 else if (riscv_subset_supports ("d"))
430 riscv_set_abi (xlen, FLOAT_ABI_DOUBLE, FALSE);
431 else
432 riscv_set_abi (xlen, FLOAT_ABI_SOFT, FALSE);
433 }
434 else
435 {
436 gas_assert (abi_xlen != 0 && xlen != 0 && float_abi != FLOAT_ABI_DEFAULT);
437 if (abi_xlen > xlen)
438 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
439 else if (abi_xlen < xlen)
440 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
441 }
442
443 /* Update the EF_RISCV_FLOAT_ABI field of elf_flags. */
444 elf_flags &= ~EF_RISCV_FLOAT_ABI;
445 elf_flags |= float_abi << 1;
446
447 if (rve_abi)
448 elf_flags |= EF_RISCV_RVE;
449 }
450
451 /* Handle of the OPCODE hash table. */
452 static htab_t op_hash = NULL;
453
454 /* Handle of the type of .insn hash table. */
455 static htab_t insn_type_hash = NULL;
456
457 /* This array holds the chars that always start a comment. If the
458 pre-processor is disabled, these aren't very useful. */
459 const char comment_chars[] = "#";
460
461 /* This array holds the chars that only start a comment at the beginning of
462 a line. If the line seems to have the form '# 123 filename'
463 .line and .file directives will appear in the pre-processed output
464
465 Note that input_file.c hand checks for '#' at the beginning of the
466 first line of the input file. This is because the compiler outputs
467 #NO_APP at the beginning of its output.
468
469 Also note that C style comments are always supported. */
470 const char line_comment_chars[] = "#";
471
472 /* This array holds machine specific line separator characters. */
473 const char line_separator_chars[] = ";";
474
475 /* Chars that can be used to separate mant from exp in floating point nums. */
476 const char EXP_CHARS[] = "eE";
477
478 /* Chars that mean this number is a floating point constant.
479 As in 0f12.456 or 0d1.2345e12. */
480 const char FLT_CHARS[] = "rRsSfFdDxXpP";
481
482 /* Indicate we are already assemble any instructions or not. */
483 static bfd_boolean start_assemble = FALSE;
484
485 /* Indicate ELF attributes are explicitly set. */
486 static bfd_boolean explicit_attr = FALSE;
487
488 /* Indicate CSR or priv instructions are explicitly used. */
489 static bfd_boolean explicit_priv_attr = FALSE;
490
491 /* Macros for encoding relaxation state for RVC branches and far jumps. */
492 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
493 ((relax_substateT) \
494 (0xc0000000 \
495 | ((uncond) ? 1 : 0) \
496 | ((rvc) ? 2 : 0) \
497 | ((length) << 2)))
498 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
499 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
500 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
501 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
502
503 /* Is the given value a sign-extended 32-bit value? */
504 #define IS_SEXT_32BIT_NUM(x) \
505 (((x) &~ (offsetT) 0x7fffffff) == 0 \
506 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
507
508 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
509 #define IS_ZEXT_32BIT_NUM(x) \
510 (((x) &~ (offsetT) 0xffffffff) == 0 \
511 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
512
513 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
514 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
515 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
516 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
517
518 /* Determine if an instruction matches an opcode. */
519 #define OPCODE_MATCHES(OPCODE, OP) \
520 (((OPCODE) & MASK_##OP) == MATCH_##OP)
521
522 static char *expr_end;
523
524 /* The default target format to use. */
525
526 const char *
527 riscv_target_format (void)
528 {
529 if (target_big_endian)
530 return xlen == 64 ? "elf64-bigriscv" : "elf32-bigriscv";
531 else
532 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
533 }
534
535 /* Return the length of instruction INSN. */
536
537 static inline unsigned int
538 insn_length (const struct riscv_cl_insn *insn)
539 {
540 return riscv_insn_length (insn->insn_opcode);
541 }
542
543 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
544
545 static void
546 create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
547 {
548 insn->insn_mo = mo;
549 insn->insn_opcode = mo->match;
550 insn->frag = NULL;
551 insn->where = 0;
552 insn->fixp = NULL;
553 }
554
555 /* Install INSN at the location specified by its "frag" and "where" fields. */
556
557 static void
558 install_insn (const struct riscv_cl_insn *insn)
559 {
560 char *f = insn->frag->fr_literal + insn->where;
561 number_to_chars_littleendian (f, insn->insn_opcode, insn_length (insn));
562 }
563
564 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
565 and install the opcode in the new location. */
566
567 static void
568 move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
569 {
570 insn->frag = frag;
571 insn->where = where;
572 if (insn->fixp != NULL)
573 {
574 insn->fixp->fx_frag = frag;
575 insn->fixp->fx_where = where;
576 }
577 install_insn (insn);
578 }
579
580 /* Add INSN to the end of the output. */
581
582 static void
583 add_fixed_insn (struct riscv_cl_insn *insn)
584 {
585 char *f = frag_more (insn_length (insn));
586 move_insn (insn, frag_now, f - frag_now->fr_literal);
587 }
588
589 static void
590 add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
591 relax_substateT subtype, symbolS *symbol, offsetT offset)
592 {
593 frag_grow (max_chars);
594 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
595 frag_var (rs_machine_dependent, max_chars, var,
596 subtype, symbol, offset, NULL);
597 }
598
599 /* Compute the length of a branch sequence, and adjust the stored length
600 accordingly. If FRAGP is NULL, the worst-case length is returned. */
601
602 static unsigned
603 relaxed_branch_length (fragS *fragp, asection *sec, int update)
604 {
605 int jump, rvc, length = 8;
606
607 if (!fragp)
608 return length;
609
610 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
611 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
612 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
613
614 /* Assume jumps are in range; the linker will catch any that aren't. */
615 length = jump ? 4 : 8;
616
617 if (fragp->fr_symbol != NULL
618 && S_IS_DEFINED (fragp->fr_symbol)
619 && !S_IS_WEAK (fragp->fr_symbol)
620 && sec == S_GET_SEGMENT (fragp->fr_symbol))
621 {
622 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
623 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
624 val -= fragp->fr_address + fragp->fr_fix;
625
626 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
627 length = 2;
628 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
629 length = 4;
630 else if (!jump && rvc)
631 length = 6;
632 }
633
634 if (update)
635 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
636
637 return length;
638 }
639
640 /* Information about an opcode name, mnemonics and its value. */
641 struct opcode_name_t
642 {
643 const char *name;
644 unsigned int val;
645 };
646
647 /* List for all supported opcode name. */
648 static const struct opcode_name_t opcode_name_list[] =
649 {
650 {"C0", 0x0},
651 {"C1", 0x1},
652 {"C2", 0x2},
653
654 {"LOAD", 0x03},
655 {"LOAD_FP", 0x07},
656 {"CUSTOM_0", 0x0b},
657 {"MISC_MEM", 0x0f},
658 {"OP_IMM", 0x13},
659 {"AUIPC", 0x17},
660 {"OP_IMM_32", 0x1b},
661 /* 48b 0x1f. */
662
663 {"STORE", 0x23},
664 {"STORE_FP", 0x27},
665 {"CUSTOM_1", 0x2b},
666 {"AMO", 0x2f},
667 {"OP", 0x33},
668 {"LUI", 0x37},
669 {"OP_32", 0x3b},
670 /* 64b 0x3f. */
671
672 {"MADD", 0x43},
673 {"MSUB", 0x47},
674 {"NMADD", 0x4f},
675 {"NMSUB", 0x4b},
676 {"OP_FP", 0x53},
677 /*reserved 0x57. */
678 {"CUSTOM_2", 0x5b},
679 /* 48b 0x5f. */
680
681 {"BRANCH", 0x63},
682 {"JALR", 0x67},
683 /*reserved 0x5b. */
684 {"JAL", 0x6f},
685 {"SYSTEM", 0x73},
686 /*reserved 0x77. */
687 {"CUSTOM_3", 0x7b},
688 /* >80b 0x7f. */
689
690 {NULL, 0}
691 };
692
693 /* Hash table for lookup opcode name. */
694 static htab_t opcode_names_hash = NULL;
695
696 /* Initialization for hash table of opcode name. */
697
698 static void
699 init_opcode_names_hash (void)
700 {
701 const struct opcode_name_t *opcode;
702
703 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
704 if (str_hash_insert (opcode_names_hash, opcode->name, opcode, 0) != NULL)
705 as_fatal (_("internal: duplicate %s"), opcode->name);
706 }
707
708 /* Find `s` is a valid opcode name or not, return the opcode name info
709 if found. */
710
711 static const struct opcode_name_t *
712 opcode_name_lookup (char **s)
713 {
714 char *e;
715 char save_c;
716 struct opcode_name_t *o;
717
718 /* Find end of name. */
719 e = *s;
720 if (is_name_beginner (*e))
721 ++e;
722 while (is_part_of_name (*e))
723 ++e;
724
725 /* Terminate name. */
726 save_c = *e;
727 *e = '\0';
728
729 o = (struct opcode_name_t *) str_hash_find (opcode_names_hash, *s);
730
731 /* Advance to next token if one was recognized. */
732 if (o)
733 *s = e;
734
735 *e = save_c;
736 expr_end = e;
737
738 return o;
739 }
740
741 enum reg_class
742 {
743 RCLASS_GPR,
744 RCLASS_FPR,
745 RCLASS_MAX,
746
747 RCLASS_CSR
748 };
749
750 static htab_t reg_names_hash = NULL;
751 static htab_t csr_extra_hash = NULL;
752
753 #define ENCODE_REG_HASH(cls, n) \
754 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
755 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
756 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
757
758 static void
759 hash_reg_name (enum reg_class class, const char *name, unsigned n)
760 {
761 void *hash = ENCODE_REG_HASH (class, n);
762 if (str_hash_insert (reg_names_hash, name, hash, 0) != NULL)
763 as_fatal (_("internal: duplicate %s"), name);
764 }
765
766 static void
767 hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
768 {
769 unsigned i;
770
771 for (i = 0; i < n; i++)
772 hash_reg_name (class, names[i], i);
773 }
774
775 /* Init hash table csr_extra_hash to handle CSR. */
776
777 static void
778 riscv_init_csr_hash (const char *name,
779 unsigned address,
780 enum riscv_csr_class class,
781 enum riscv_spec_class define_version,
782 enum riscv_spec_class abort_version)
783 {
784 struct riscv_csr_extra *entry, *pre_entry;
785 bfd_boolean need_enrty = TRUE;
786
787 pre_entry = NULL;
788 entry = (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, name);
789 while (need_enrty && entry != NULL)
790 {
791 if (entry->csr_class == class
792 && entry->address == address
793 && entry->define_version == define_version
794 && entry->abort_version == abort_version)
795 need_enrty = FALSE;
796 pre_entry = entry;
797 entry = entry->next;
798 }
799
800 /* Duplicate CSR. */
801 if (!need_enrty)
802 return;
803
804 entry = XNEW (struct riscv_csr_extra);
805 entry->csr_class = class;
806 entry->address = address;
807 entry->define_version = define_version;
808 entry->abort_version = abort_version;
809 entry->next = NULL;
810
811 if (pre_entry == NULL)
812 str_hash_insert (csr_extra_hash, name, entry, 0);
813 else
814 pre_entry->next = entry;
815 }
816
817 /* Return the CSR address after checking the ISA dependency and
818 the privileged spec version.
819
820 There are one warning and two errors for CSR,
821
822 Invalid CSR: the CSR was defined, but isn't allowed for the current ISA
823 or the privileged spec, report warning only if -mcsr-check is set.
824 Unknown CSR: the CSR has never been defined, report error.
825 Improper CSR: the CSR number over the range (> 0xfff), report error. */
826
827 static unsigned int
828 riscv_csr_address (const char *csr_name,
829 struct riscv_csr_extra *entry)
830 {
831 struct riscv_csr_extra *saved_entry = entry;
832 enum riscv_csr_class csr_class = entry->csr_class;
833 bfd_boolean need_check_version = TRUE;
834 bfd_boolean result = TRUE;
835
836 switch (csr_class)
837 {
838 case CSR_CLASS_I:
839 result = riscv_subset_supports ("i");
840 break;
841 case CSR_CLASS_I_32:
842 result = (xlen == 32 && riscv_subset_supports ("i"));
843 break;
844 case CSR_CLASS_F:
845 result = riscv_subset_supports ("f");
846 need_check_version = FALSE;
847 break;
848 case CSR_CLASS_DEBUG:
849 need_check_version = FALSE;
850 break;
851 default:
852 as_bad (_("internal: bad RISC-V CSR class (0x%x)"), csr_class);
853 }
854
855 if (riscv_opts.csr_check && !result)
856 as_warn (_("invalid CSR `%s' for the current ISA"), csr_name);
857
858 while (entry != NULL)
859 {
860 if (!need_check_version
861 || (default_priv_spec >= entry->define_version
862 && default_priv_spec < entry->abort_version))
863 {
864 /* Find the CSR according to the specific version. */
865 return entry->address;
866 }
867 entry = entry->next;
868 }
869
870 /* Can not find the CSR address from the chosen privileged version,
871 so use the newly defined value. */
872 if (riscv_opts.csr_check)
873 {
874 const char *priv_name = NULL;
875 RISCV_GET_PRIV_SPEC_NAME (priv_name, default_priv_spec);
876 if (priv_name != NULL)
877 as_warn (_("invalid CSR `%s' for the privileged spec `%s'"),
878 csr_name, priv_name);
879 }
880
881 return saved_entry->address;
882 }
883
884 /* Return -1 if the CSR has never been defined. Otherwise, return
885 the address. */
886
887 static unsigned int
888 reg_csr_lookup_internal (const char *s)
889 {
890 struct riscv_csr_extra *r =
891 (struct riscv_csr_extra *) str_hash_find (csr_extra_hash, s);
892
893 if (r == NULL)
894 return -1U;
895
896 return riscv_csr_address (s, r);
897 }
898
899 static unsigned int
900 reg_lookup_internal (const char *s, enum reg_class class)
901 {
902 void *r;
903
904 if (class == RCLASS_CSR)
905 return reg_csr_lookup_internal (s);
906
907 r = str_hash_find (reg_names_hash, s);
908 if (r == NULL || DECODE_REG_CLASS (r) != class)
909 return -1;
910
911 if (riscv_opts.rve && class == RCLASS_GPR && DECODE_REG_NUM (r) > 15)
912 return -1;
913
914 return DECODE_REG_NUM (r);
915 }
916
917 static bfd_boolean
918 reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
919 {
920 char *e;
921 char save_c;
922 int reg = -1;
923
924 /* Find end of name. */
925 e = *s;
926 if (is_name_beginner (*e))
927 ++e;
928 while (is_part_of_name (*e))
929 ++e;
930
931 /* Terminate name. */
932 save_c = *e;
933 *e = '\0';
934
935 /* Look for the register. Advance to next token if one was recognized. */
936 if ((reg = reg_lookup_internal (*s, class)) >= 0)
937 *s = e;
938
939 *e = save_c;
940 if (regnop)
941 *regnop = reg;
942 return reg >= 0;
943 }
944
945 static bfd_boolean
946 arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
947 {
948 const char *p = strchr (*s, ',');
949 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
950
951 if (len == 0)
952 return FALSE;
953
954 for (i = 0; i < size; i++)
955 if (array[i] != NULL && strncmp (array[i], *s, len) == 0)
956 {
957 *regnop = i;
958 *s += len;
959 return TRUE;
960 }
961
962 return FALSE;
963 }
964
965 /* For consistency checking, verify that all bits are specified either
966 by the match/mask part of the instruction definition, or by the
967 operand list. The `length` could be 0, 4 or 8, 0 for auto detection. */
968
969 static bfd_boolean
970 validate_riscv_insn (const struct riscv_opcode *opc, int length)
971 {
972 const char *p = opc->args;
973 char c;
974 insn_t used_bits = opc->mask;
975 int insn_width;
976 insn_t required_bits;
977
978 if (length == 0)
979 insn_width = 8 * riscv_insn_length (opc->match);
980 else
981 insn_width = 8 * length;
982
983 required_bits = ~0ULL >> (64 - insn_width);
984
985 if ((used_bits & opc->match) != (opc->match & required_bits))
986 {
987 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
988 opc->name, opc->args);
989 return FALSE;
990 }
991
992 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
993 while (*p)
994 switch (c = *p++)
995 {
996 case 'C': /* RVC */
997 switch (c = *p++)
998 {
999 case 'a': used_bits |= ENCODE_RVC_J_IMM (-1U); break;
1000 case 'c': break; /* RS1, constrained to equal sp. */
1001 case 'i': used_bits |= ENCODE_RVC_SIMM3(-1U); break;
1002 case 'j': used_bits |= ENCODE_RVC_IMM (-1U); break;
1003 case 'o': used_bits |= ENCODE_RVC_IMM (-1U); break;
1004 case 'k': used_bits |= ENCODE_RVC_LW_IMM (-1U); break;
1005 case 'l': used_bits |= ENCODE_RVC_LD_IMM (-1U); break;
1006 case 'm': used_bits |= ENCODE_RVC_LWSP_IMM (-1U); break;
1007 case 'n': used_bits |= ENCODE_RVC_LDSP_IMM (-1U); break;
1008 case 'p': used_bits |= ENCODE_RVC_B_IMM (-1U); break;
1009 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1010 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1011 case 'u': used_bits |= ENCODE_RVC_IMM (-1U); break;
1012 case 'v': used_bits |= ENCODE_RVC_IMM (-1U); break;
1013 case 'w': break; /* RS1S, constrained to equal RD. */
1014 case 'x': break; /* RS2S, constrained to equal RD. */
1015 case 'z': break; /* RS2S, constrained to be x0. */
1016 case 'K': used_bits |= ENCODE_RVC_ADDI4SPN_IMM (-1U); break;
1017 case 'L': used_bits |= ENCODE_RVC_ADDI16SP_IMM (-1U); break;
1018 case 'M': used_bits |= ENCODE_RVC_SWSP_IMM (-1U); break;
1019 case 'N': used_bits |= ENCODE_RVC_SDSP_IMM (-1U); break;
1020 case 'U': break; /* RS1, constrained to equal RD. */
1021 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1022 case '<': used_bits |= ENCODE_RVC_IMM (-1U); break;
1023 case '>': used_bits |= ENCODE_RVC_IMM (-1U); break;
1024 case '8': used_bits |= ENCODE_RVC_UIMM8 (-1U); break;
1025 case 'S': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
1026 case 'T': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1027 case 'D': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1028 case 'F': /* RVC funct for .insn directive. */
1029 switch (c = *p++)
1030 {
1031 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
1032 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
1033 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
1034 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
1035 default:
1036 as_bad (_("internal: bad RISC-V opcode "
1037 "(unknown operand type `CF%c'): %s %s"),
1038 c, opc->name, opc->args);
1039 return FALSE;
1040 }
1041 break;
1042 default:
1043 as_bad (_("internal: bad RISC-V opcode "
1044 "(unknown operand type `C%c'): %s %s"),
1045 c, opc->name, opc->args);
1046 return FALSE;
1047 }
1048 break;
1049 case ',': break;
1050 case '(': break;
1051 case ')': break;
1052 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
1053 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
1054 case 'A': break;
1055 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1056 case 'Z': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1057 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
1058 case 'I': break;
1059 case 'R': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1060 case 'S': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1061 case 'U': USE_BITS (OP_MASK_RS1, OP_SH_RS1);
1062 /* Fall through. */
1063 case 'T': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1064 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
1065 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
1066 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
1067 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
1068 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
1069 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
1070 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
1071 case 'o':
1072 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
1073 case 'a': used_bits |= ENCODE_UJTYPE_IMM (-1U); break;
1074 case 'p': used_bits |= ENCODE_SBTYPE_IMM (-1U); break;
1075 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
1076 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
1077 case 'z': break;
1078 case '[': break;
1079 case ']': break;
1080 case '0': break;
1081 case '1': break;
1082 case 'F': /* Funct for .insn directive. */
1083 switch (c = *p++)
1084 {
1085 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
1086 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
1087 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
1088 default:
1089 as_bad (_("internal: bad RISC-V opcode "
1090 "(unknown operand type `F%c'): %s %s"),
1091 c, opc->name, opc->args);
1092 return FALSE;
1093 }
1094 break;
1095 case 'O': /* Opcode for .insn directive. */
1096 switch (c = *p++)
1097 {
1098 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
1099 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
1100 default:
1101 as_bad (_("internal: bad RISC-V opcode "
1102 "(unknown operand type `F%c'): %s %s"),
1103 c, opc->name, opc->args);
1104 return FALSE;
1105 }
1106 break;
1107 default:
1108 as_bad (_("internal: bad RISC-V opcode "
1109 "(unknown operand type `%c'): %s %s"),
1110 c, opc->name, opc->args);
1111 return FALSE;
1112 }
1113 #undef USE_BITS
1114 if (used_bits != required_bits)
1115 {
1116 as_bad (_("internal: bad RISC-V opcode "
1117 "(bits 0x%lx undefined): %s %s"),
1118 ~(unsigned long)(used_bits & required_bits),
1119 opc->name, opc->args);
1120 return FALSE;
1121 }
1122 return TRUE;
1123 }
1124
1125 struct percent_op_match
1126 {
1127 const char *str;
1128 bfd_reloc_code_real_type reloc;
1129 };
1130
1131 /* Common hash table initialization function for instruction and .insn
1132 directive. */
1133
1134 static htab_t
1135 init_opcode_hash (const struct riscv_opcode *opcodes,
1136 bfd_boolean insn_directive_p)
1137 {
1138 int i = 0;
1139 int length;
1140 htab_t hash = str_htab_create ();
1141 while (opcodes[i].name)
1142 {
1143 const char *name = opcodes[i].name;
1144 if (str_hash_insert (hash, name, &opcodes[i], 0) != NULL)
1145 as_fatal (_("internal: duplicate %s"), name);
1146
1147 do
1148 {
1149 if (opcodes[i].pinfo != INSN_MACRO)
1150 {
1151 if (insn_directive_p)
1152 length = ((name[0] == 'c') ? 2 : 4);
1153 else
1154 length = 0; /* Let assembler determine the length. */
1155 if (!validate_riscv_insn (&opcodes[i], length))
1156 as_fatal (_("internal: broken assembler. "
1157 "No assembly attempted"));
1158 }
1159 else
1160 gas_assert (!insn_directive_p);
1161 ++i;
1162 }
1163 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
1164 }
1165
1166 return hash;
1167 }
1168
1169 /* This function is called once, at assembler startup time. It should set up
1170 all the tables, etc. that the MD part of the assembler will need. */
1171
1172 void
1173 md_begin (void)
1174 {
1175 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
1176
1177 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
1178 as_warn (_("could not set architecture and machine"));
1179
1180 op_hash = init_opcode_hash (riscv_opcodes, FALSE);
1181 insn_type_hash = init_opcode_hash (riscv_insn_types, TRUE);
1182
1183 reg_names_hash = str_htab_create ();
1184 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
1185 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
1186 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
1187 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
1188 /* Add "fp" as an alias for "s0". */
1189 hash_reg_name (RCLASS_GPR, "fp", 8);
1190
1191 /* Create and insert CSR hash tables. */
1192 csr_extra_hash = str_htab_create ();
1193 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
1194 riscv_init_csr_hash (#name, num, class, define_version, abort_version);
1195 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
1196 DECLARE_CSR(name, num, class, define_version, abort_version);
1197 #include "opcode/riscv-opc.h"
1198 #undef DECLARE_CSR
1199
1200 opcode_names_hash = str_htab_create ();
1201 init_opcode_names_hash ();
1202
1203 /* Set the default alignment for the text section. */
1204 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
1205 }
1206
1207 static insn_t
1208 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
1209 {
1210 switch (reloc_type)
1211 {
1212 case BFD_RELOC_32:
1213 return value;
1214
1215 case BFD_RELOC_RISCV_HI20:
1216 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1217
1218 case BFD_RELOC_RISCV_LO12_S:
1219 return ENCODE_STYPE_IMM (value);
1220
1221 case BFD_RELOC_RISCV_LO12_I:
1222 return ENCODE_ITYPE_IMM (value);
1223
1224 default:
1225 abort ();
1226 }
1227 }
1228
1229 /* Output an instruction. IP is the instruction information.
1230 ADDRESS_EXPR is an operand of the instruction to be used with
1231 RELOC_TYPE. */
1232
1233 static void
1234 append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
1235 bfd_reloc_code_real_type reloc_type)
1236 {
1237 dwarf2_emit_insn (0);
1238
1239 if (reloc_type != BFD_RELOC_UNUSED)
1240 {
1241 reloc_howto_type *howto;
1242
1243 gas_assert (address_expr);
1244 if (reloc_type == BFD_RELOC_12_PCREL
1245 || reloc_type == BFD_RELOC_RISCV_JMP)
1246 {
1247 int j = reloc_type == BFD_RELOC_RISCV_JMP;
1248 int best_case = riscv_insn_length (ip->insn_opcode);
1249 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
1250
1251 if (now_seg == absolute_section)
1252 {
1253 as_bad (_("relaxable branches not supported in absolute section"));
1254 return;
1255 }
1256
1257 add_relaxed_insn (ip, worst_case, best_case,
1258 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
1259 address_expr->X_add_symbol,
1260 address_expr->X_add_number);
1261 return;
1262 }
1263 else
1264 {
1265 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
1266 if (howto == NULL)
1267 as_bad (_("internal: usupported RISC-V relocation number %d"),
1268 reloc_type);
1269
1270 ip->fixp = fix_new_exp (ip->frag, ip->where,
1271 bfd_get_reloc_size (howto),
1272 address_expr, FALSE, reloc_type);
1273
1274 ip->fixp->fx_tcbit = riscv_opts.relax;
1275 }
1276 }
1277
1278 add_fixed_insn (ip);
1279 install_insn (ip);
1280
1281 /* We need to start a new frag after any instruction that can be
1282 optimized away or compressed by the linker during relaxation, to prevent
1283 the assembler from computing static offsets across such an instruction.
1284 This is necessary to get correct EH info. */
1285 if (reloc_type == BFD_RELOC_RISCV_HI20
1286 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
1287 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
1288 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
1289 {
1290 frag_wane (frag_now);
1291 frag_new (0);
1292 }
1293 }
1294
1295 /* Build an instruction created by a macro expansion. This is passed
1296 a pointer to the count of instructions created so far, an expression,
1297 the name of the instruction to build, an operand format string, and
1298 corresponding arguments. */
1299
1300 static void
1301 macro_build (expressionS *ep, const char *name, const char *fmt, ...)
1302 {
1303 const struct riscv_opcode *mo;
1304 struct riscv_cl_insn insn;
1305 bfd_reloc_code_real_type r;
1306 va_list args;
1307
1308 va_start (args, fmt);
1309
1310 r = BFD_RELOC_UNUSED;
1311 mo = (struct riscv_opcode *) str_hash_find (op_hash, name);
1312 gas_assert (mo);
1313
1314 /* Find a non-RVC variant of the instruction. append_insn will compress
1315 it if possible. */
1316 while (riscv_insn_length (mo->match) < 4)
1317 mo++;
1318 gas_assert (strcmp (name, mo->name) == 0);
1319
1320 create_insn (&insn, mo);
1321 for (;;)
1322 {
1323 switch (*fmt++)
1324 {
1325 case 'd':
1326 INSERT_OPERAND (RD, insn, va_arg (args, int));
1327 continue;
1328
1329 case 's':
1330 INSERT_OPERAND (RS1, insn, va_arg (args, int));
1331 continue;
1332
1333 case 't':
1334 INSERT_OPERAND (RS2, insn, va_arg (args, int));
1335 continue;
1336
1337 case 'j':
1338 case 'u':
1339 case 'q':
1340 gas_assert (ep != NULL);
1341 r = va_arg (args, int);
1342 continue;
1343
1344 case '\0':
1345 break;
1346 case ',':
1347 continue;
1348 default:
1349 as_fatal (_("internal: invalid macro"));
1350 }
1351 break;
1352 }
1353 va_end (args);
1354 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
1355
1356 append_insn (&insn, ep, r);
1357 }
1358
1359 /* Build an instruction created by a macro expansion. Like md_assemble but
1360 accept a printf-style format string and arguments. */
1361
1362 static void
1363 md_assemblef (const char *format, ...)
1364 {
1365 char *buf = NULL;
1366 va_list ap;
1367 int r;
1368
1369 va_start (ap, format);
1370
1371 r = vasprintf (&buf, format, ap);
1372
1373 if (r < 0)
1374 as_fatal (_("internal: vasprintf failed"));
1375
1376 md_assemble (buf);
1377 free(buf);
1378
1379 va_end (ap);
1380 }
1381
1382 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
1383 unset. */
1384
1385 static void
1386 normalize_constant_expr (expressionS *ex)
1387 {
1388 if (xlen > 32)
1389 return;
1390 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
1391 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
1392 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
1393 - 0x80000000);
1394 }
1395
1396 /* Fail if an expression EX is not a constant. IP is the instruction using EX.
1397 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
1398
1399 static void
1400 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1401 bfd_boolean maybe_csr)
1402 {
1403 if (ex->X_op == O_big)
1404 as_bad (_("unsupported large constant"));
1405 else if (maybe_csr && ex->X_op == O_symbol)
1406 as_bad (_("unknown CSR `%s'"),
1407 S_GET_NAME (ex->X_add_symbol));
1408 else if (ex->X_op != O_constant)
1409 as_bad (_("instruction %s requires absolute expression"),
1410 ip->insn_mo->name);
1411 normalize_constant_expr (ex);
1412 }
1413
1414 static symbolS *
1415 make_internal_label (void)
1416 {
1417 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg, frag_now,
1418 frag_now_fix ());
1419 }
1420
1421 /* Load an entry from the GOT. */
1422
1423 static void
1424 pcrel_access (int destreg, int tempreg, expressionS *ep,
1425 const char *lo_insn, const char *lo_pattern,
1426 bfd_reloc_code_real_type hi_reloc,
1427 bfd_reloc_code_real_type lo_reloc)
1428 {
1429 expressionS ep2;
1430 ep2.X_op = O_symbol;
1431 ep2.X_add_symbol = make_internal_label ();
1432 ep2.X_add_number = 0;
1433
1434 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1435 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1436 }
1437
1438 static void
1439 pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1440 bfd_reloc_code_real_type hi_reloc,
1441 bfd_reloc_code_real_type lo_reloc)
1442 {
1443 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1444 }
1445
1446 static void
1447 pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1448 bfd_reloc_code_real_type hi_reloc,
1449 bfd_reloc_code_real_type lo_reloc)
1450 {
1451 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1452 }
1453
1454 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1455
1456 static void
1457 riscv_call (int destreg, int tempreg, expressionS *ep,
1458 bfd_reloc_code_real_type reloc)
1459 {
1460 /* Ensure the jalr is emitted to the same frag as the auipc. */
1461 frag_grow (8);
1462 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1463 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1464 /* See comment at end of append_insn. */
1465 frag_wane (frag_now);
1466 frag_new (0);
1467 }
1468
1469 /* Load an integer constant into a register. */
1470
1471 static void
1472 load_const (int reg, expressionS *ep)
1473 {
1474 int shift = RISCV_IMM_BITS;
1475 bfd_vma upper_imm, sign = (bfd_vma) 1 << (RISCV_IMM_BITS - 1);
1476 expressionS upper = *ep, lower = *ep;
1477 lower.X_add_number = ((ep->X_add_number & (sign + sign - 1)) ^ sign) - sign;
1478 upper.X_add_number -= lower.X_add_number;
1479
1480 if (ep->X_op != O_constant)
1481 {
1482 as_bad (_("unsupported large constant"));
1483 return;
1484 }
1485
1486 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
1487 {
1488 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1489 while (((upper.X_add_number >> shift) & 1) == 0)
1490 shift++;
1491
1492 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1493 load_const (reg, &upper);
1494
1495 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1496 if (lower.X_add_number != 0)
1497 md_assemblef ("addi x%d, x%d, %" BFD_VMA_FMT "d", reg, reg,
1498 lower.X_add_number);
1499 }
1500 else
1501 {
1502 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1503 int hi_reg = 0;
1504
1505 if (upper.X_add_number != 0)
1506 {
1507 /* Discard low part and zero-extend upper immediate. */
1508 upper_imm = ((uint32_t)upper.X_add_number >> shift);
1509
1510 md_assemblef ("lui x%d, 0x%" BFD_VMA_FMT "x", reg, upper_imm);
1511 hi_reg = reg;
1512 }
1513
1514 if (lower.X_add_number != 0 || hi_reg == 0)
1515 md_assemblef ("%s x%d, x%d, %" BFD_VMA_FMT "d", ADD32_INSN, reg, hi_reg,
1516 lower.X_add_number);
1517 }
1518 }
1519
1520 /* Zero extend and sign extend byte/half-word/word. */
1521
1522 static void
1523 riscv_ext (int destreg, int srcreg, unsigned shift, bfd_boolean sign)
1524 {
1525 if (sign)
1526 {
1527 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1528 md_assemblef ("srai x%d, x%d, 0x%x", destreg, destreg, shift);
1529 }
1530 else
1531 {
1532 md_assemblef ("slli x%d, x%d, 0x%x", destreg, srcreg, shift);
1533 md_assemblef ("srli x%d, x%d, 0x%x", destreg, destreg, shift);
1534 }
1535 }
1536
1537 /* Expand RISC-V assembly macros into one or more instructions. */
1538
1539 static void
1540 macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1541 bfd_reloc_code_real_type *imm_reloc)
1542 {
1543 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
1544 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
1545 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
1546 int mask = ip->insn_mo->mask;
1547
1548 switch (mask)
1549 {
1550 case M_LI:
1551 load_const (rd, imm_expr);
1552 break;
1553
1554 case M_LA:
1555 case M_LLA:
1556 /* Load the address of a symbol into a register. */
1557 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
1558 as_bad (_("offset too large"));
1559
1560 if (imm_expr->X_op == O_constant)
1561 load_const (rd, imm_expr);
1562 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol. */
1563 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1564 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1565 else /* Local PIC symbol, or any non-PIC symbol. */
1566 pcrel_load (rd, rd, imm_expr, "addi",
1567 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1568 break;
1569
1570 case M_LA_TLS_GD:
1571 pcrel_load (rd, rd, imm_expr, "addi",
1572 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1573 break;
1574
1575 case M_LA_TLS_IE:
1576 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1577 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1578 break;
1579
1580 case M_LB:
1581 pcrel_load (rd, rd, imm_expr, "lb",
1582 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1583 break;
1584
1585 case M_LBU:
1586 pcrel_load (rd, rd, imm_expr, "lbu",
1587 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1588 break;
1589
1590 case M_LH:
1591 pcrel_load (rd, rd, imm_expr, "lh",
1592 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1593 break;
1594
1595 case M_LHU:
1596 pcrel_load (rd, rd, imm_expr, "lhu",
1597 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1598 break;
1599
1600 case M_LW:
1601 pcrel_load (rd, rd, imm_expr, "lw",
1602 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1603 break;
1604
1605 case M_LWU:
1606 pcrel_load (rd, rd, imm_expr, "lwu",
1607 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1608 break;
1609
1610 case M_LD:
1611 pcrel_load (rd, rd, imm_expr, "ld",
1612 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1613 break;
1614
1615 case M_FLW:
1616 pcrel_load (rd, rs1, imm_expr, "flw",
1617 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1618 break;
1619
1620 case M_FLD:
1621 pcrel_load (rd, rs1, imm_expr, "fld",
1622 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1623 break;
1624
1625 case M_SB:
1626 pcrel_store (rs2, rs1, imm_expr, "sb",
1627 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1628 break;
1629
1630 case M_SH:
1631 pcrel_store (rs2, rs1, imm_expr, "sh",
1632 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1633 break;
1634
1635 case M_SW:
1636 pcrel_store (rs2, rs1, imm_expr, "sw",
1637 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1638 break;
1639
1640 case M_SD:
1641 pcrel_store (rs2, rs1, imm_expr, "sd",
1642 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1643 break;
1644
1645 case M_FSW:
1646 pcrel_store (rs2, rs1, imm_expr, "fsw",
1647 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1648 break;
1649
1650 case M_FSD:
1651 pcrel_store (rs2, rs1, imm_expr, "fsd",
1652 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1653 break;
1654
1655 case M_CALL:
1656 riscv_call (rd, rs1, imm_expr, *imm_reloc);
1657 break;
1658
1659 case M_ZEXTH:
1660 riscv_ext (rd, rs1, xlen - 16, FALSE);
1661 break;
1662
1663 case M_ZEXTW:
1664 riscv_ext (rd, rs1, xlen - 32, FALSE);
1665 break;
1666
1667 case M_SEXTB:
1668 riscv_ext (rd, rs1, xlen - 8, TRUE);
1669 break;
1670
1671 case M_SEXTH:
1672 riscv_ext (rd, rs1, xlen - 16, TRUE);
1673 break;
1674
1675 default:
1676 as_bad (_("internal: macro %s not implemented"), ip->insn_mo->name);
1677 break;
1678 }
1679 }
1680
1681 static const struct percent_op_match percent_op_utype[] =
1682 {
1683 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
1684 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
1685 {"%got_pcrel_hi", BFD_RELOC_RISCV_GOT_HI20},
1686 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
1687 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
1688 {"%hi", BFD_RELOC_RISCV_HI20},
1689 {0, 0}
1690 };
1691
1692 static const struct percent_op_match percent_op_itype[] =
1693 {
1694 {"%lo", BFD_RELOC_RISCV_LO12_I},
1695 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
1696 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
1697 {0, 0}
1698 };
1699
1700 static const struct percent_op_match percent_op_stype[] =
1701 {
1702 {"%lo", BFD_RELOC_RISCV_LO12_S},
1703 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
1704 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
1705 {0, 0}
1706 };
1707
1708 static const struct percent_op_match percent_op_rtype[] =
1709 {
1710 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1711 {0, 0}
1712 };
1713
1714 static const struct percent_op_match percent_op_null[] =
1715 {
1716 {0, 0}
1717 };
1718
1719 /* Return true if *STR points to a relocation operator. When returning true,
1720 move *STR over the operator and store its relocation code in *RELOC.
1721 Leave both *STR and *RELOC alone when returning false. */
1722
1723 static bfd_boolean
1724 parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
1725 const struct percent_op_match *percent_op)
1726 {
1727 for ( ; percent_op->str; percent_op++)
1728 if (strncasecmp (*str, percent_op->str, strlen (percent_op->str)) == 0)
1729 {
1730 int len = strlen (percent_op->str);
1731
1732 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
1733 continue;
1734
1735 *str += strlen (percent_op->str);
1736 *reloc = percent_op->reloc;
1737
1738 /* Check whether the output BFD supports this relocation.
1739 If not, issue an error and fall back on something safe. */
1740 if (*reloc != BFD_RELOC_UNUSED
1741 && !bfd_reloc_type_lookup (stdoutput, *reloc))
1742 {
1743 as_bad ("internal: relocation %s isn't supported by the "
1744 "current ABI", percent_op->str);
1745 *reloc = BFD_RELOC_UNUSED;
1746 }
1747 return TRUE;
1748 }
1749 return FALSE;
1750 }
1751
1752 static void
1753 my_getExpression (expressionS *ep, char *str)
1754 {
1755 char *save_in;
1756
1757 save_in = input_line_pointer;
1758 input_line_pointer = str;
1759 expression (ep);
1760 expr_end = input_line_pointer;
1761 input_line_pointer = save_in;
1762 }
1763
1764 /* Parse string STR as a 16-bit relocatable operand. Store the
1765 expression in *EP and the relocation, if any, in RELOC.
1766 Return the number of relocation operators used (0 or 1).
1767
1768 On exit, EXPR_END points to the first character after the expression. */
1769
1770 static size_t
1771 my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1772 char *str, const struct percent_op_match *percent_op)
1773 {
1774 size_t reloc_index;
1775 unsigned crux_depth, str_depth, regno;
1776 char *crux;
1777
1778 /* First, check for integer registers. No callers can accept a reg, but
1779 we need to avoid accidentally creating a useless undefined symbol below,
1780 if this is an instruction pattern that can't match. A glibc build fails
1781 if this is removed. */
1782 if (reg_lookup (&str, RCLASS_GPR, &regno))
1783 {
1784 ep->X_op = O_register;
1785 ep->X_add_number = regno;
1786 expr_end = str;
1787 return 0;
1788 }
1789
1790 /* Search for the start of the main expression.
1791
1792 End the loop with CRUX pointing to the start of the main expression and
1793 with CRUX_DEPTH containing the number of open brackets at that point. */
1794 reloc_index = -1;
1795 str_depth = 0;
1796 do
1797 {
1798 reloc_index++;
1799 crux = str;
1800 crux_depth = str_depth;
1801
1802 /* Skip over whitespace and brackets, keeping count of the number
1803 of brackets. */
1804 while (*str == ' ' || *str == '\t' || *str == '(')
1805 if (*str++ == '(')
1806 str_depth++;
1807 }
1808 while (*str == '%'
1809 && reloc_index < 1
1810 && parse_relocation (&str, reloc, percent_op));
1811
1812 my_getExpression (ep, crux);
1813 str = expr_end;
1814
1815 /* Match every open bracket. */
1816 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
1817 if (*str++ == ')')
1818 crux_depth--;
1819
1820 if (crux_depth > 0)
1821 as_bad ("unclosed '('");
1822
1823 expr_end = str;
1824
1825 return reloc_index;
1826 }
1827
1828 /* Parse opcode name, could be an mnemonics or number. */
1829
1830 static size_t
1831 my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1832 char *str, const struct percent_op_match *percent_op)
1833 {
1834 const struct opcode_name_t *o = opcode_name_lookup (&str);
1835
1836 if (o != NULL)
1837 {
1838 ep->X_op = O_constant;
1839 ep->X_add_number = o->val;
1840 return 0;
1841 }
1842
1843 return my_getSmallExpression (ep, reloc, str, percent_op);
1844 }
1845
1846 /* Detect and handle implicitly zero load-store offsets. For example,
1847 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return TRUE iff such
1848 an implicit offset was detected. */
1849
1850 static bfd_boolean
1851 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
1852 {
1853 /* Check whether there is only a single bracketed expression left.
1854 If so, it must be the base register and the constant must be zero. */
1855 if (*s == '(' && strchr (s + 1, '(') == 0)
1856 {
1857 ep->X_op = O_constant;
1858 ep->X_add_number = 0;
1859 return TRUE;
1860 }
1861
1862 return FALSE;
1863 }
1864
1865 /* All RISC-V CSR instructions belong to one of these classes. */
1866 enum csr_insn_type
1867 {
1868 INSN_NOT_CSR,
1869 INSN_CSRRW,
1870 INSN_CSRRS,
1871 INSN_CSRRC
1872 };
1873
1874 /* Return which CSR instruction is checking. */
1875
1876 static enum csr_insn_type
1877 riscv_csr_insn_type (insn_t insn)
1878 {
1879 if (((insn ^ MATCH_CSRRW) & MASK_CSRRW) == 0
1880 || ((insn ^ MATCH_CSRRWI) & MASK_CSRRWI) == 0)
1881 return INSN_CSRRW;
1882 else if (((insn ^ MATCH_CSRRS) & MASK_CSRRS) == 0
1883 || ((insn ^ MATCH_CSRRSI) & MASK_CSRRSI) == 0)
1884 return INSN_CSRRS;
1885 else if (((insn ^ MATCH_CSRRC) & MASK_CSRRC) == 0
1886 || ((insn ^ MATCH_CSRRCI) & MASK_CSRRCI) == 0)
1887 return INSN_CSRRC;
1888 else
1889 return INSN_NOT_CSR;
1890 }
1891
1892 /* CSRRW and CSRRWI always write CSR. CSRRS, CSRRC, CSRRSI and CSRRCI write
1893 CSR when RS1 isn't zero. The CSR is read only if the [11:10] bits of
1894 CSR address is 0x3. */
1895
1896 static bfd_boolean
1897 riscv_csr_read_only_check (insn_t insn)
1898 {
1899 int csr = (insn & (OP_MASK_CSR << OP_SH_CSR)) >> OP_SH_CSR;
1900 int rs1 = (insn & (OP_MASK_RS1 << OP_SH_RS1)) >> OP_SH_RS1;
1901 int readonly = (((csr & (0x3 << 10)) >> 10) == 0x3);
1902 enum csr_insn_type csr_insn = riscv_csr_insn_type (insn);
1903
1904 if (readonly
1905 && (((csr_insn == INSN_CSRRS
1906 || csr_insn == INSN_CSRRC)
1907 && rs1 != 0)
1908 || csr_insn == INSN_CSRRW))
1909 return FALSE;
1910
1911 return TRUE;
1912 }
1913
1914 /* Return True if it is a privileged instruction. Otherwise, return FALSE.
1915
1916 uret is actually a N-ext instruction. So it is better to regard it as
1917 an user instruction rather than the priv instruction.
1918
1919 hret is used to return from traps in H-mode. H-mode is removed since
1920 the v1.10 priv spec, but probably be added in the new hypervisor spec.
1921 Therefore, hret should be controlled by the hypervisor spec rather than
1922 priv spec in the future.
1923
1924 dret is defined in the debug spec, so it should be checked in the future,
1925 too. */
1926
1927 static bfd_boolean
1928 riscv_is_priv_insn (insn_t insn)
1929 {
1930 return (((insn ^ MATCH_SRET) & MASK_SRET) == 0
1931 || ((insn ^ MATCH_MRET) & MASK_MRET) == 0
1932 || ((insn ^ MATCH_SFENCE_VMA) & MASK_SFENCE_VMA) == 0
1933 || ((insn ^ MATCH_WFI) & MASK_WFI) == 0
1934 /* The sfence.vm is dropped in the v1.10 priv specs, but we still need to
1935 check it here to keep the compatible. */
1936 || ((insn ^ MATCH_SFENCE_VM) & MASK_SFENCE_VM) == 0);
1937 }
1938
1939 /* This routine assembles an instruction into its binary format. As a
1940 side effect, it sets the global variable imm_reloc to the type of
1941 relocation to do if one of the operands is an address expression. */
1942
1943 static const char *
1944 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
1945 bfd_reloc_code_real_type *imm_reloc, htab_t hash)
1946 {
1947 char *s;
1948 const char *args;
1949 char c = 0;
1950 struct riscv_opcode *insn;
1951 char *argsStart;
1952 unsigned int regno;
1953 char save_c = 0;
1954 int argnum;
1955 const struct percent_op_match *p;
1956 const char *error = "unrecognized opcode";
1957 /* Indicate we are assembling instruction with CSR. */
1958 bfd_boolean insn_with_csr = FALSE;
1959
1960 /* Parse the name of the instruction. Terminate the string if whitespace
1961 is found so that str_hash_find only sees the name part of the string. */
1962 for (s = str; *s != '\0'; ++s)
1963 if (ISSPACE (*s))
1964 {
1965 save_c = *s;
1966 *s++ = '\0';
1967 break;
1968 }
1969
1970 insn = (struct riscv_opcode *) str_hash_find (hash, str);
1971
1972 argsStart = s;
1973 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
1974 {
1975 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
1976 continue;
1977
1978 if (!riscv_multi_subset_supports (insn->insn_class))
1979 continue;
1980
1981 create_insn (ip, insn);
1982 argnum = 1;
1983
1984 imm_expr->X_op = O_absent;
1985 *imm_reloc = BFD_RELOC_UNUSED;
1986 p = percent_op_itype;
1987
1988 for (args = insn->args;; ++args)
1989 {
1990 s += strspn (s, " \t");
1991 switch (*args)
1992 {
1993 case '\0': /* End of args. */
1994 if (insn->pinfo != INSN_MACRO)
1995 {
1996 if (!insn->match_func (insn, ip->insn_opcode))
1997 break;
1998
1999 /* For .insn, insn->match and insn->mask are 0. */
2000 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
2001 ? ip->insn_opcode
2002 : insn->match) == 2
2003 && !riscv_opts.rvc)
2004 break;
2005
2006 if (riscv_is_priv_insn (ip->insn_opcode))
2007 explicit_priv_attr = TRUE;
2008
2009 /* Check if we write a read-only CSR by the CSR
2010 instruction. */
2011 if (insn_with_csr
2012 && riscv_opts.csr_check
2013 && !riscv_csr_read_only_check (ip->insn_opcode))
2014 {
2015 /* Restore the character in advance, since we want to
2016 report the detailed warning message here. */
2017 if (save_c)
2018 *(argsStart - 1) = save_c;
2019 as_warn (_("read-only CSR is written `%s'"), str);
2020 insn_with_csr = FALSE;
2021 }
2022 }
2023 if (*s != '\0')
2024 break;
2025 /* Successful assembly. */
2026 error = NULL;
2027 insn_with_csr = FALSE;
2028 goto out;
2029
2030 case 'C': /* RVC */
2031 switch (*++args)
2032 {
2033 case 's': /* RS1 x8-x15. */
2034 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2035 || !(regno >= 8 && regno <= 15))
2036 break;
2037 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2038 continue;
2039 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
2040 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2041 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
2042 break;
2043 continue;
2044 case 't': /* RS2 x8-x15. */
2045 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2046 || !(regno >= 8 && regno <= 15))
2047 break;
2048 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2049 continue;
2050 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
2051 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2052 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
2053 break;
2054 continue;
2055 case 'U': /* RS1, constrained to equal RD. */
2056 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2057 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
2058 break;
2059 continue;
2060 case 'V': /* RS2 */
2061 if (!reg_lookup (&s, RCLASS_GPR, &regno))
2062 break;
2063 INSERT_OPERAND (CRS2, *ip, regno);
2064 continue;
2065 case 'c': /* RS1, constrained to equal sp. */
2066 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2067 || regno != X_SP)
2068 break;
2069 continue;
2070 case 'z': /* RS2, constrained to equal x0. */
2071 if (!reg_lookup (&s, RCLASS_GPR, &regno)
2072 || regno != 0)
2073 break;
2074 continue;
2075 case '>':
2076 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2077 || imm_expr->X_op != O_constant
2078 || imm_expr->X_add_number <= 0
2079 || imm_expr->X_add_number >= 64)
2080 break;
2081 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
2082 rvc_imm_done:
2083 s = expr_end;
2084 imm_expr->X_op = O_absent;
2085 continue;
2086 case '<':
2087 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2088 || imm_expr->X_op != O_constant
2089 || imm_expr->X_add_number <= 0
2090 || imm_expr->X_add_number >= 32
2091 || !VALID_RVC_IMM ((valueT) imm_expr->X_add_number))
2092 break;
2093 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
2094 goto rvc_imm_done;
2095 case '8':
2096 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2097 || imm_expr->X_op != O_constant
2098 || imm_expr->X_add_number < 0
2099 || imm_expr->X_add_number >= 256
2100 || !VALID_RVC_UIMM8 ((valueT) imm_expr->X_add_number))
2101 break;
2102 ip->insn_opcode |= ENCODE_RVC_UIMM8 (imm_expr->X_add_number);
2103 goto rvc_imm_done;
2104 case 'i':
2105 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2106 || imm_expr->X_op != O_constant
2107 || imm_expr->X_add_number == 0
2108 || !VALID_RVC_SIMM3 ((valueT) imm_expr->X_add_number))
2109 break;
2110 ip->insn_opcode |= ENCODE_RVC_SIMM3 (imm_expr->X_add_number);
2111 goto rvc_imm_done;
2112 case 'j':
2113 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2114 || imm_expr->X_op != O_constant
2115 || imm_expr->X_add_number == 0
2116 || !VALID_RVC_IMM ((valueT) imm_expr->X_add_number))
2117 break;
2118 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
2119 goto rvc_imm_done;
2120 case 'k':
2121 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2122 continue;
2123 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2124 || imm_expr->X_op != O_constant
2125 || !VALID_RVC_LW_IMM ((valueT) imm_expr->X_add_number))
2126 break;
2127 ip->insn_opcode |= ENCODE_RVC_LW_IMM (imm_expr->X_add_number);
2128 goto rvc_imm_done;
2129 case 'l':
2130 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2131 continue;
2132 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2133 || imm_expr->X_op != O_constant
2134 || !VALID_RVC_LD_IMM ((valueT) imm_expr->X_add_number))
2135 break;
2136 ip->insn_opcode |= ENCODE_RVC_LD_IMM (imm_expr->X_add_number);
2137 goto rvc_imm_done;
2138 case 'm':
2139 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2140 continue;
2141 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2142 || imm_expr->X_op != O_constant
2143 || !VALID_RVC_LWSP_IMM ((valueT) imm_expr->X_add_number))
2144 break;
2145 ip->insn_opcode |=
2146 ENCODE_RVC_LWSP_IMM (imm_expr->X_add_number);
2147 goto rvc_imm_done;
2148 case 'n':
2149 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2150 continue;
2151 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2152 || imm_expr->X_op != O_constant
2153 || !VALID_RVC_LDSP_IMM ((valueT) imm_expr->X_add_number))
2154 break;
2155 ip->insn_opcode |=
2156 ENCODE_RVC_LDSP_IMM (imm_expr->X_add_number);
2157 goto rvc_imm_done;
2158 case 'o':
2159 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2160 || imm_expr->X_op != O_constant
2161 /* C.addiw, c.li, and c.andi allow zero immediate.
2162 C.addi allows zero immediate as hint. Otherwise this
2163 is same as 'j'. */
2164 || !VALID_RVC_IMM ((valueT) imm_expr->X_add_number))
2165 break;
2166 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
2167 goto rvc_imm_done;
2168 case 'K':
2169 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2170 || imm_expr->X_op != O_constant
2171 || imm_expr->X_add_number == 0
2172 || !VALID_RVC_ADDI4SPN_IMM ((valueT) imm_expr->X_add_number))
2173 break;
2174 ip->insn_opcode |=
2175 ENCODE_RVC_ADDI4SPN_IMM (imm_expr->X_add_number);
2176 goto rvc_imm_done;
2177 case 'L':
2178 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2179 || imm_expr->X_op != O_constant
2180 || imm_expr->X_add_number == 0
2181 || !VALID_RVC_ADDI16SP_IMM ((valueT) imm_expr->X_add_number))
2182 break;
2183 ip->insn_opcode |=
2184 ENCODE_RVC_ADDI16SP_IMM (imm_expr->X_add_number);
2185 goto rvc_imm_done;
2186 case 'M':
2187 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2188 continue;
2189 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2190 || imm_expr->X_op != O_constant
2191 || !VALID_RVC_SWSP_IMM ((valueT) imm_expr->X_add_number))
2192 break;
2193 ip->insn_opcode |=
2194 ENCODE_RVC_SWSP_IMM (imm_expr->X_add_number);
2195 goto rvc_imm_done;
2196 case 'N':
2197 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2198 continue;
2199 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2200 || imm_expr->X_op != O_constant
2201 || !VALID_RVC_SDSP_IMM ((valueT) imm_expr->X_add_number))
2202 break;
2203 ip->insn_opcode |=
2204 ENCODE_RVC_SDSP_IMM (imm_expr->X_add_number);
2205 goto rvc_imm_done;
2206 case 'u':
2207 p = percent_op_utype;
2208 if (my_getSmallExpression (imm_expr, imm_reloc, s, p))
2209 break;
2210 rvc_lui:
2211 if (imm_expr->X_op != O_constant
2212 || imm_expr->X_add_number <= 0
2213 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
2214 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
2215 && (imm_expr->X_add_number <
2216 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
2217 break;
2218 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
2219 goto rvc_imm_done;
2220 case 'v':
2221 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2222 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
2223 || ((int32_t)imm_expr->X_add_number
2224 != imm_expr->X_add_number))
2225 break;
2226 imm_expr->X_add_number =
2227 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
2228 goto rvc_lui;
2229 case 'p':
2230 goto branch;
2231 case 'a':
2232 goto jump;
2233 case 'S': /* Floating-point RS1 x8-x15. */
2234 if (!reg_lookup (&s, RCLASS_FPR, &regno)
2235 || !(regno >= 8 && regno <= 15))
2236 break;
2237 INSERT_OPERAND (CRS1S, *ip, regno % 8);
2238 continue;
2239 case 'D': /* Floating-point RS2 x8-x15. */
2240 if (!reg_lookup (&s, RCLASS_FPR, &regno)
2241 || !(regno >= 8 && regno <= 15))
2242 break;
2243 INSERT_OPERAND (CRS2S, *ip, regno % 8);
2244 continue;
2245 case 'T': /* Floating-point RS2. */
2246 if (!reg_lookup (&s, RCLASS_FPR, &regno))
2247 break;
2248 INSERT_OPERAND (CRS2, *ip, regno);
2249 continue;
2250 case 'F':
2251 switch (*++args)
2252 {
2253 case '6':
2254 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2255 || imm_expr->X_op != O_constant
2256 || imm_expr->X_add_number < 0
2257 || imm_expr->X_add_number >= 64)
2258 {
2259 as_bad (_("bad value for compressed funct6 "
2260 "field, value must be 0...64"));
2261 break;
2262 }
2263 INSERT_OPERAND (CFUNCT6, *ip, imm_expr->X_add_number);
2264 imm_expr->X_op = O_absent;
2265 s = expr_end;
2266 continue;
2267
2268 case '4':
2269 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2270 || imm_expr->X_op != O_constant
2271 || imm_expr->X_add_number < 0
2272 || imm_expr->X_add_number >= 16)
2273 {
2274 as_bad (_("bad value for compressed funct4 "
2275 "field, value must be 0...15"));
2276 break;
2277 }
2278 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
2279 imm_expr->X_op = O_absent;
2280 s = expr_end;
2281 continue;
2282
2283 case '3':
2284 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2285 || imm_expr->X_op != O_constant
2286 || imm_expr->X_add_number < 0
2287 || imm_expr->X_add_number >= 8)
2288 {
2289 as_bad (_("bad value for compressed funct3 "
2290 "field, value must be 0...7"));
2291 break;
2292 }
2293 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
2294 imm_expr->X_op = O_absent;
2295 s = expr_end;
2296 continue;
2297
2298 case '2':
2299 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2300 || imm_expr->X_op != O_constant
2301 || imm_expr->X_add_number < 0
2302 || imm_expr->X_add_number >= 4)
2303 {
2304 as_bad (_("bad value for compressed funct2 "
2305 "field, value must be 0...3"));
2306 break;
2307 }
2308 INSERT_OPERAND (CFUNCT2, *ip, imm_expr->X_add_number);
2309 imm_expr->X_op = O_absent;
2310 s = expr_end;
2311 continue;
2312
2313 default:
2314 as_bad (_("internal: unknown compressed funct "
2315 "field specifier `CF%c'"), *args);
2316 }
2317 break;
2318
2319 default:
2320 as_bad (_("internal: unknown compressed field "
2321 "specifier `C%c'"), *args);
2322 }
2323 break;
2324
2325 case ',':
2326 ++argnum;
2327 if (*s++ == *args)
2328 continue;
2329 s--;
2330 break;
2331
2332 case '(':
2333 case ')':
2334 case '[':
2335 case ']':
2336 if (*s++ == *args)
2337 continue;
2338 break;
2339
2340 case '<': /* Shift amount, 0 - 31. */
2341 my_getExpression (imm_expr, s);
2342 check_absolute_expr (ip, imm_expr, FALSE);
2343 if ((unsigned long) imm_expr->X_add_number > 31)
2344 as_bad (_("improper shift amount (%lu)"),
2345 (unsigned long) imm_expr->X_add_number);
2346 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
2347 imm_expr->X_op = O_absent;
2348 s = expr_end;
2349 continue;
2350
2351 case '>': /* Shift amount, 0 - (XLEN-1). */
2352 my_getExpression (imm_expr, s);
2353 check_absolute_expr (ip, imm_expr, FALSE);
2354 if ((unsigned long) imm_expr->X_add_number >= xlen)
2355 as_bad (_("improper shift amount (%lu)"),
2356 (unsigned long) imm_expr->X_add_number);
2357 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
2358 imm_expr->X_op = O_absent;
2359 s = expr_end;
2360 continue;
2361
2362 case 'Z': /* CSRRxI immediate. */
2363 my_getExpression (imm_expr, s);
2364 check_absolute_expr (ip, imm_expr, FALSE);
2365 if ((unsigned long) imm_expr->X_add_number > 31)
2366 as_bad (_("improper CSRxI immediate (%lu)"),
2367 (unsigned long) imm_expr->X_add_number);
2368 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
2369 imm_expr->X_op = O_absent;
2370 s = expr_end;
2371 continue;
2372
2373 case 'E': /* Control register. */
2374 insn_with_csr = TRUE;
2375 explicit_priv_attr = TRUE;
2376 if (reg_lookup (&s, RCLASS_CSR, &regno))
2377 INSERT_OPERAND (CSR, *ip, regno);
2378 else
2379 {
2380 my_getExpression (imm_expr, s);
2381 check_absolute_expr (ip, imm_expr, TRUE);
2382 if ((unsigned long) imm_expr->X_add_number > 0xfff)
2383 as_bad (_("improper CSR address (%lu)"),
2384 (unsigned long) imm_expr->X_add_number);
2385 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
2386 imm_expr->X_op = O_absent;
2387 s = expr_end;
2388 }
2389 continue;
2390
2391 case 'm': /* Rounding mode. */
2392 if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), &regno))
2393 {
2394 INSERT_OPERAND (RM, *ip, regno);
2395 continue;
2396 }
2397 break;
2398
2399 case 'P':
2400 case 'Q': /* Fence predecessor/successor. */
2401 if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ),
2402 &regno))
2403 {
2404 if (*args == 'P')
2405 INSERT_OPERAND (PRED, *ip, regno);
2406 else
2407 INSERT_OPERAND (SUCC, *ip, regno);
2408 continue;
2409 }
2410 break;
2411
2412 case 'd': /* Destination register. */
2413 case 's': /* Source register. */
2414 case 't': /* Target register. */
2415 case 'r': /* RS3 */
2416 if (reg_lookup (&s, RCLASS_GPR, &regno))
2417 {
2418 c = *args;
2419 if (*s == ' ')
2420 ++s;
2421
2422 /* Now that we have assembled one operand, we use the args
2423 string to figure out where it goes in the instruction. */
2424 switch (c)
2425 {
2426 case 's':
2427 INSERT_OPERAND (RS1, *ip, regno);
2428 break;
2429 case 'd':
2430 INSERT_OPERAND (RD, *ip, regno);
2431 break;
2432 case 't':
2433 INSERT_OPERAND (RS2, *ip, regno);
2434 break;
2435 case 'r':
2436 INSERT_OPERAND (RS3, *ip, regno);
2437 break;
2438 }
2439 continue;
2440 }
2441 break;
2442
2443 case 'D': /* Floating point RD. */
2444 case 'S': /* Floating point RS1. */
2445 case 'T': /* Floating point RS2. */
2446 case 'U': /* Floating point RS1 and RS2. */
2447 case 'R': /* Floating point RS3. */
2448 if (reg_lookup (&s, RCLASS_FPR, &regno))
2449 {
2450 c = *args;
2451 if (*s == ' ')
2452 ++s;
2453 switch (c)
2454 {
2455 case 'D':
2456 INSERT_OPERAND (RD, *ip, regno);
2457 break;
2458 case 'S':
2459 INSERT_OPERAND (RS1, *ip, regno);
2460 break;
2461 case 'U':
2462 INSERT_OPERAND (RS1, *ip, regno);
2463 /* Fall through. */
2464 case 'T':
2465 INSERT_OPERAND (RS2, *ip, regno);
2466 break;
2467 case 'R':
2468 INSERT_OPERAND (RS3, *ip, regno);
2469 break;
2470 }
2471 continue;
2472 }
2473 break;
2474
2475 case 'I':
2476 my_getExpression (imm_expr, s);
2477 if (imm_expr->X_op != O_big
2478 && imm_expr->X_op != O_constant)
2479 break;
2480 normalize_constant_expr (imm_expr);
2481 s = expr_end;
2482 continue;
2483
2484 case 'A':
2485 my_getExpression (imm_expr, s);
2486 normalize_constant_expr (imm_expr);
2487 /* The 'A' format specifier must be a symbol. */
2488 if (imm_expr->X_op != O_symbol)
2489 break;
2490 *imm_reloc = BFD_RELOC_32;
2491 s = expr_end;
2492 continue;
2493
2494 case 'B':
2495 my_getExpression (imm_expr, s);
2496 normalize_constant_expr (imm_expr);
2497 /* The 'B' format specifier must be a symbol or a constant. */
2498 if (imm_expr->X_op != O_symbol && imm_expr->X_op != O_constant)
2499 break;
2500 if (imm_expr->X_op == O_symbol)
2501 *imm_reloc = BFD_RELOC_32;
2502 s = expr_end;
2503 continue;
2504
2505 case 'j': /* Sign-extended immediate. */
2506 p = percent_op_itype;
2507 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
2508 goto alu_op;
2509 case 'q': /* Store displacement. */
2510 p = percent_op_stype;
2511 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
2512 goto load_store;
2513 case 'o': /* Load displacement. */
2514 p = percent_op_itype;
2515 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
2516 goto load_store;
2517 case '1':
2518 /* This is used for TLS, where the fourth operand is
2519 %tprel_add, to get a relocation applied to an add
2520 instruction, for relaxation to use. */
2521 p = percent_op_rtype;
2522 goto alu_op;
2523 case '0': /* AMO displacement, which must be zero. */
2524 p = percent_op_null;
2525 load_store:
2526 if (riscv_handle_implicit_zero_offset (imm_expr, s))
2527 continue;
2528 alu_op:
2529 /* If this value won't fit into a 16 bit offset, then go
2530 find a macro that will generate the 32 bit offset
2531 code pattern. */
2532 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
2533 {
2534 normalize_constant_expr (imm_expr);
2535 if (imm_expr->X_op != O_constant
2536 || (*args == '0' && imm_expr->X_add_number != 0)
2537 || (*args == '1')
2538 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
2539 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
2540 break;
2541 }
2542 s = expr_end;
2543 continue;
2544
2545 case 'p': /* PC-relative offset. */
2546 branch:
2547 *imm_reloc = BFD_RELOC_12_PCREL;
2548 my_getExpression (imm_expr, s);
2549 s = expr_end;
2550 continue;
2551
2552 case 'u': /* Upper 20 bits. */
2553 p = percent_op_utype;
2554 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
2555 {
2556 if (imm_expr->X_op != O_constant)
2557 break;
2558
2559 if (imm_expr->X_add_number < 0
2560 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
2561 as_bad (_("lui expression not in range 0..1048575"));
2562
2563 *imm_reloc = BFD_RELOC_RISCV_HI20;
2564 imm_expr->X_add_number <<= RISCV_IMM_BITS;
2565 }
2566 s = expr_end;
2567 continue;
2568
2569 case 'a': /* 20-bit PC-relative offset. */
2570 jump:
2571 my_getExpression (imm_expr, s);
2572 s = expr_end;
2573 *imm_reloc = BFD_RELOC_RISCV_JMP;
2574 continue;
2575
2576 case 'c':
2577 my_getExpression (imm_expr, s);
2578 s = expr_end;
2579 if (strcmp (s, "@plt") == 0)
2580 {
2581 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
2582 s += 4;
2583 }
2584 else
2585 *imm_reloc = BFD_RELOC_RISCV_CALL;
2586 continue;
2587
2588 case 'O':
2589 switch (*++args)
2590 {
2591 case '4':
2592 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
2593 || imm_expr->X_op != O_constant
2594 || imm_expr->X_add_number < 0
2595 || imm_expr->X_add_number >= 128
2596 || (imm_expr->X_add_number & 0x3) != 3)
2597 {
2598 as_bad (_("bad value for opcode field, "
2599 "value must be 0...127 and "
2600 "lower 2 bits must be 0x3"));
2601 break;
2602 }
2603 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
2604 imm_expr->X_op = O_absent;
2605 s = expr_end;
2606 continue;
2607
2608 case '2':
2609 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
2610 || imm_expr->X_op != O_constant
2611 || imm_expr->X_add_number < 0
2612 || imm_expr->X_add_number >= 3)
2613 {
2614 as_bad (_("bad value for opcode field, "
2615 "value must be 0...2"));
2616 break;
2617 }
2618 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
2619 imm_expr->X_op = O_absent;
2620 s = expr_end;
2621 continue;
2622
2623 default:
2624 as_bad (_("internal: unknown opcode field "
2625 "specifier `O%c'"), *args);
2626 }
2627 break;
2628
2629 case 'F':
2630 switch (*++args)
2631 {
2632 case '7':
2633 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2634 || imm_expr->X_op != O_constant
2635 || imm_expr->X_add_number < 0
2636 || imm_expr->X_add_number >= 128)
2637 {
2638 as_bad (_("bad value for funct7 field, "
2639 "value must be 0...127"));
2640 break;
2641 }
2642 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
2643 imm_expr->X_op = O_absent;
2644 s = expr_end;
2645 continue;
2646
2647 case '3':
2648 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2649 || imm_expr->X_op != O_constant
2650 || imm_expr->X_add_number < 0
2651 || imm_expr->X_add_number >= 8)
2652 {
2653 as_bad (_("bad value for funct3 field, "
2654 "value must be 0...7"));
2655 break;
2656 }
2657 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
2658 imm_expr->X_op = O_absent;
2659 s = expr_end;
2660 continue;
2661
2662 case '2':
2663 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2664 || imm_expr->X_op != O_constant
2665 || imm_expr->X_add_number < 0
2666 || imm_expr->X_add_number >= 4)
2667 {
2668 as_bad (_("bad value for funct2 field, "
2669 "value must be 0...3"));
2670 break;
2671 }
2672 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
2673 imm_expr->X_op = O_absent;
2674 s = expr_end;
2675 continue;
2676
2677 default:
2678 as_bad (_("internal: unknown funct field "
2679 "specifier `F%c'\n"), *args);
2680 }
2681 break;
2682
2683 case 'z':
2684 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2685 || imm_expr->X_op != O_constant
2686 || imm_expr->X_add_number != 0)
2687 break;
2688 s = expr_end;
2689 imm_expr->X_op = O_absent;
2690 continue;
2691
2692 default:
2693 as_fatal (_("internal: unknown argument type `%c'"), *args);
2694 }
2695 break;
2696 }
2697 s = argsStart;
2698 error = _("illegal operands");
2699 insn_with_csr = FALSE;
2700 }
2701
2702 out:
2703 /* Restore the character we might have clobbered above. */
2704 if (save_c)
2705 *(argsStart - 1) = save_c;
2706
2707 return error;
2708 }
2709
2710 void
2711 md_assemble (char *str)
2712 {
2713 struct riscv_cl_insn insn;
2714 expressionS imm_expr;
2715 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2716
2717 /* The architecture and privileged elf attributes should be set
2718 before assembling. */
2719 if (!start_assemble)
2720 {
2721 start_assemble = TRUE;
2722
2723 riscv_set_abi_by_arch ();
2724 if (!riscv_set_default_priv_spec (NULL))
2725 return;
2726 }
2727
2728 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc, op_hash);
2729
2730 if (error)
2731 {
2732 as_bad ("%s `%s'", error, str);
2733 return;
2734 }
2735
2736 if (insn.insn_mo->pinfo == INSN_MACRO)
2737 macro (&insn, &imm_expr, &imm_reloc);
2738 else
2739 append_insn (&insn, &imm_expr, imm_reloc);
2740 }
2741
2742 const char *
2743 md_atof (int type, char *litP, int *sizeP)
2744 {
2745 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
2746 }
2747
2748 void
2749 md_number_to_chars (char *buf, valueT val, int n)
2750 {
2751 if (target_big_endian)
2752 number_to_chars_bigendian (buf, val, n);
2753 else
2754 number_to_chars_littleendian (buf, val, n);
2755 }
2756
2757 const char *md_shortopts = "O::g::G:";
2758
2759 enum options
2760 {
2761 OPTION_MARCH = OPTION_MD_BASE,
2762 OPTION_PIC,
2763 OPTION_NO_PIC,
2764 OPTION_MABI,
2765 OPTION_RELAX,
2766 OPTION_NO_RELAX,
2767 OPTION_ARCH_ATTR,
2768 OPTION_NO_ARCH_ATTR,
2769 OPTION_CSR_CHECK,
2770 OPTION_NO_CSR_CHECK,
2771 OPTION_MISA_SPEC,
2772 OPTION_MPRIV_SPEC,
2773 OPTION_BIG_ENDIAN,
2774 OPTION_LITTLE_ENDIAN,
2775 OPTION_END_OF_ENUM
2776 };
2777
2778 struct option md_longopts[] =
2779 {
2780 {"march", required_argument, NULL, OPTION_MARCH},
2781 {"fPIC", no_argument, NULL, OPTION_PIC},
2782 {"fpic", no_argument, NULL, OPTION_PIC},
2783 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2784 {"mabi", required_argument, NULL, OPTION_MABI},
2785 {"mrelax", no_argument, NULL, OPTION_RELAX},
2786 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
2787 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
2788 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
2789 {"mcsr-check", no_argument, NULL, OPTION_CSR_CHECK},
2790 {"mno-csr-check", no_argument, NULL, OPTION_NO_CSR_CHECK},
2791 {"misa-spec", required_argument, NULL, OPTION_MISA_SPEC},
2792 {"mpriv-spec", required_argument, NULL, OPTION_MPRIV_SPEC},
2793 {"mbig-endian", no_argument, NULL, OPTION_BIG_ENDIAN},
2794 {"mlittle-endian", no_argument, NULL, OPTION_LITTLE_ENDIAN},
2795
2796 {NULL, no_argument, NULL, 0}
2797 };
2798 size_t md_longopts_size = sizeof (md_longopts);
2799
2800 int
2801 md_parse_option (int c, const char *arg)
2802 {
2803 switch (c)
2804 {
2805 case OPTION_MARCH:
2806 default_arch_with_ext = arg;
2807 break;
2808
2809 case OPTION_NO_PIC:
2810 riscv_opts.pic = FALSE;
2811 break;
2812
2813 case OPTION_PIC:
2814 riscv_opts.pic = TRUE;
2815 break;
2816
2817 case OPTION_MABI:
2818 if (strcmp (arg, "ilp32") == 0)
2819 riscv_set_abi (32, FLOAT_ABI_SOFT, FALSE);
2820 else if (strcmp (arg, "ilp32e") == 0)
2821 riscv_set_abi (32, FLOAT_ABI_SOFT, TRUE);
2822 else if (strcmp (arg, "ilp32f") == 0)
2823 riscv_set_abi (32, FLOAT_ABI_SINGLE, FALSE);
2824 else if (strcmp (arg, "ilp32d") == 0)
2825 riscv_set_abi (32, FLOAT_ABI_DOUBLE, FALSE);
2826 else if (strcmp (arg, "ilp32q") == 0)
2827 riscv_set_abi (32, FLOAT_ABI_QUAD, FALSE);
2828 else if (strcmp (arg, "lp64") == 0)
2829 riscv_set_abi (64, FLOAT_ABI_SOFT, FALSE);
2830 else if (strcmp (arg, "lp64f") == 0)
2831 riscv_set_abi (64, FLOAT_ABI_SINGLE, FALSE);
2832 else if (strcmp (arg, "lp64d") == 0)
2833 riscv_set_abi (64, FLOAT_ABI_DOUBLE, FALSE);
2834 else if (strcmp (arg, "lp64q") == 0)
2835 riscv_set_abi (64, FLOAT_ABI_QUAD, FALSE);
2836 else
2837 return 0;
2838 explicit_mabi = TRUE;
2839 break;
2840
2841 case OPTION_RELAX:
2842 riscv_opts.relax = TRUE;
2843 break;
2844
2845 case OPTION_NO_RELAX:
2846 riscv_opts.relax = FALSE;
2847 break;
2848
2849 case OPTION_ARCH_ATTR:
2850 riscv_opts.arch_attr = TRUE;
2851 break;
2852
2853 case OPTION_NO_ARCH_ATTR:
2854 riscv_opts.arch_attr = FALSE;
2855 break;
2856
2857 case OPTION_CSR_CHECK:
2858 riscv_opts.csr_check = TRUE;
2859 break;
2860
2861 case OPTION_NO_CSR_CHECK:
2862 riscv_opts.csr_check = FALSE;
2863 break;
2864
2865 case OPTION_MISA_SPEC:
2866 return riscv_set_default_isa_spec (arg);
2867
2868 case OPTION_MPRIV_SPEC:
2869 return riscv_set_default_priv_spec (arg);
2870
2871 case OPTION_BIG_ENDIAN:
2872 target_big_endian = 1;
2873 break;
2874
2875 case OPTION_LITTLE_ENDIAN:
2876 target_big_endian = 0;
2877 break;
2878
2879 default:
2880 return 0;
2881 }
2882
2883 return 1;
2884 }
2885
2886 void
2887 riscv_after_parse_args (void)
2888 {
2889 /* The --with-arch is optional for now, so we still need to set the xlen
2890 according to the default_arch, which is set by the --target. */
2891 if (xlen == 0)
2892 {
2893 if (strcmp (default_arch, "riscv32") == 0)
2894 xlen = 32;
2895 else if (strcmp (default_arch, "riscv64") == 0)
2896 xlen = 64;
2897 else
2898 as_bad ("unknown default architecture `%s'", default_arch);
2899 }
2900 if (default_arch_with_ext == NULL)
2901 default_arch_with_ext = xlen == 64 ? "rv64g" : "rv32g";
2902
2903 /* Initialize the hash table for extensions with default version. */
2904 ext_version_hash = init_ext_version_hash ();
2905
2906 /* Set default specs. */
2907 if (default_isa_spec == ISA_SPEC_CLASS_NONE)
2908 riscv_set_default_isa_spec (DEFAULT_RISCV_ISA_SPEC);
2909 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
2910 riscv_set_default_priv_spec (DEFAULT_RISCV_PRIV_SPEC);
2911
2912 riscv_set_arch (default_arch_with_ext);
2913
2914 /* Add the RVC extension, regardless of -march, to support .option rvc. */
2915 riscv_set_rvc (FALSE);
2916 if (riscv_subset_supports ("c"))
2917 riscv_set_rvc (TRUE);
2918
2919 /* Enable RVE if specified by the -march option. */
2920 riscv_set_rve (FALSE);
2921 if (riscv_subset_supports ("e"))
2922 riscv_set_rve (TRUE);
2923
2924 /* If the CIE to be produced has not been overridden on the command line,
2925 then produce version 3 by default. This allows us to use the full
2926 range of registers in a .cfi_return_column directive. */
2927 if (flag_dwarf_cie_version == -1)
2928 flag_dwarf_cie_version = 3;
2929 }
2930
2931 long
2932 md_pcrel_from (fixS *fixP)
2933 {
2934 return fixP->fx_where + fixP->fx_frag->fr_address;
2935 }
2936
2937 /* Apply a fixup to the object file. */
2938
2939 void
2940 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2941 {
2942 unsigned int subtype;
2943 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
2944 bfd_boolean relaxable = FALSE;
2945 offsetT loc;
2946 segT sub_segment;
2947
2948 /* Remember value for tc_gen_reloc. */
2949 fixP->fx_addnumber = *valP;
2950
2951 switch (fixP->fx_r_type)
2952 {
2953 case BFD_RELOC_RISCV_HI20:
2954 case BFD_RELOC_RISCV_LO12_I:
2955 case BFD_RELOC_RISCV_LO12_S:
2956 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2957 | bfd_getl32 (buf), buf);
2958 if (fixP->fx_addsy == NULL)
2959 fixP->fx_done = TRUE;
2960 relaxable = TRUE;
2961 break;
2962
2963 case BFD_RELOC_RISCV_GOT_HI20:
2964 case BFD_RELOC_RISCV_ADD8:
2965 case BFD_RELOC_RISCV_ADD16:
2966 case BFD_RELOC_RISCV_ADD32:
2967 case BFD_RELOC_RISCV_ADD64:
2968 case BFD_RELOC_RISCV_SUB6:
2969 case BFD_RELOC_RISCV_SUB8:
2970 case BFD_RELOC_RISCV_SUB16:
2971 case BFD_RELOC_RISCV_SUB32:
2972 case BFD_RELOC_RISCV_SUB64:
2973 case BFD_RELOC_RISCV_RELAX:
2974 break;
2975
2976 case BFD_RELOC_RISCV_TPREL_HI20:
2977 case BFD_RELOC_RISCV_TPREL_LO12_I:
2978 case BFD_RELOC_RISCV_TPREL_LO12_S:
2979 case BFD_RELOC_RISCV_TPREL_ADD:
2980 relaxable = TRUE;
2981 /* Fall through. */
2982
2983 case BFD_RELOC_RISCV_TLS_GOT_HI20:
2984 case BFD_RELOC_RISCV_TLS_GD_HI20:
2985 case BFD_RELOC_RISCV_TLS_DTPREL32:
2986 case BFD_RELOC_RISCV_TLS_DTPREL64:
2987 if (fixP->fx_addsy != NULL)
2988 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2989 else
2990 as_bad_where (fixP->fx_file, fixP->fx_line,
2991 _("TLS relocation against a constant"));
2992 break;
2993
2994 case BFD_RELOC_32:
2995 /* Use pc-relative relocation for FDE initial location.
2996 The symbol address in .eh_frame may be adjusted in
2997 _bfd_elf_discard_section_eh_frame, and the content of
2998 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
2999 Therefore, we cannot insert a relocation whose addend symbol is
3000 in .eh_frame. Othrewise, the value may be adjusted twice. */
3001 if (fixP->fx_addsy && fixP->fx_subsy
3002 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
3003 && strcmp (sub_segment->name, ".eh_frame") == 0
3004 && S_GET_VALUE (fixP->fx_subsy)
3005 == fixP->fx_frag->fr_address + fixP->fx_where)
3006 {
3007 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
3008 fixP->fx_subsy = NULL;
3009 break;
3010 }
3011 /* Fall through. */
3012 case BFD_RELOC_64:
3013 case BFD_RELOC_16:
3014 case BFD_RELOC_8:
3015 case BFD_RELOC_RISCV_CFA:
3016 if (fixP->fx_addsy && fixP->fx_subsy)
3017 {
3018 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3019 fixP->fx_next->fx_addsy = fixP->fx_subsy;
3020 fixP->fx_next->fx_subsy = NULL;
3021 fixP->fx_next->fx_offset = 0;
3022 fixP->fx_subsy = NULL;
3023
3024 switch (fixP->fx_r_type)
3025 {
3026 case BFD_RELOC_64:
3027 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
3028 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
3029 break;
3030
3031 case BFD_RELOC_32:
3032 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
3033 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3034 break;
3035
3036 case BFD_RELOC_16:
3037 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
3038 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3039 break;
3040
3041 case BFD_RELOC_8:
3042 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
3043 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3044 break;
3045
3046 case BFD_RELOC_RISCV_CFA:
3047 /* Load the byte to get the subtype. */
3048 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
3049 loc = fixP->fx_frag->fr_fix - (subtype & 7);
3050 switch (subtype)
3051 {
3052 case DW_CFA_advance_loc1:
3053 fixP->fx_where = loc + 1;
3054 fixP->fx_next->fx_where = loc + 1;
3055 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
3056 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
3057 break;
3058
3059 case DW_CFA_advance_loc2:
3060 fixP->fx_size = 2;
3061 fixP->fx_next->fx_size = 2;
3062 fixP->fx_where = loc + 1;
3063 fixP->fx_next->fx_where = loc + 1;
3064 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
3065 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
3066 break;
3067
3068 case DW_CFA_advance_loc4:
3069 fixP->fx_size = 4;
3070 fixP->fx_next->fx_size = 4;
3071 fixP->fx_where = loc;
3072 fixP->fx_next->fx_where = loc;
3073 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
3074 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
3075 break;
3076
3077 default:
3078 if (subtype < 0x80 && (subtype & 0x40))
3079 {
3080 /* DW_CFA_advance_loc */
3081 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
3082 fixP->fx_next->fx_frag = fixP->fx_frag;
3083 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
3084 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
3085 }
3086 else
3087 as_fatal (_("internal: bad CFA value #%d"), subtype);
3088 break;
3089 }
3090 break;
3091
3092 default:
3093 /* This case is unreachable. */
3094 abort ();
3095 }
3096 }
3097 /* Fall through. */
3098
3099 case BFD_RELOC_RVA:
3100 /* If we are deleting this reloc entry, we must fill in the
3101 value now. This can happen if we have a .word which is not
3102 resolved when it appears but is later defined. */
3103 if (fixP->fx_addsy == NULL)
3104 {
3105 gas_assert (fixP->fx_size <= sizeof (valueT));
3106 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
3107 fixP->fx_done = 1;
3108 }
3109 break;
3110
3111 case BFD_RELOC_RISCV_JMP:
3112 if (fixP->fx_addsy)
3113 {
3114 /* Fill in a tentative value to improve objdump readability. */
3115 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3116 bfd_vma delta = target - md_pcrel_from (fixP);
3117 bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf);
3118 }
3119 break;
3120
3121 case BFD_RELOC_12_PCREL:
3122 if (fixP->fx_addsy)
3123 {
3124 /* Fill in a tentative value to improve objdump readability. */
3125 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3126 bfd_vma delta = target - md_pcrel_from (fixP);
3127 bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf);
3128 }
3129 break;
3130
3131 case BFD_RELOC_RISCV_RVC_BRANCH:
3132 if (fixP->fx_addsy)
3133 {
3134 /* Fill in a tentative value to improve objdump readability. */
3135 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3136 bfd_vma delta = target - md_pcrel_from (fixP);
3137 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf);
3138 }
3139 break;
3140
3141 case BFD_RELOC_RISCV_RVC_JUMP:
3142 if (fixP->fx_addsy)
3143 {
3144 /* Fill in a tentative value to improve objdump readability. */
3145 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
3146 bfd_vma delta = target - md_pcrel_from (fixP);
3147 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf);
3148 }
3149 break;
3150
3151 case BFD_RELOC_RISCV_CALL:
3152 case BFD_RELOC_RISCV_CALL_PLT:
3153 relaxable = TRUE;
3154 break;
3155
3156 case BFD_RELOC_RISCV_PCREL_HI20:
3157 case BFD_RELOC_RISCV_PCREL_LO12_S:
3158 case BFD_RELOC_RISCV_PCREL_LO12_I:
3159 relaxable = riscv_opts.relax;
3160 break;
3161
3162 case BFD_RELOC_RISCV_ALIGN:
3163 break;
3164
3165 default:
3166 /* We ignore generic BFD relocations we don't know about. */
3167 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
3168 as_fatal (_("internal: bad relocation #%d"), fixP->fx_r_type);
3169 }
3170
3171 if (fixP->fx_subsy != NULL)
3172 as_bad_where (fixP->fx_file, fixP->fx_line,
3173 _("unsupported symbol subtraction"));
3174
3175 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
3176 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
3177 {
3178 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
3179 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
3180 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
3181 fixP->fx_next->fx_size = 0;
3182 }
3183 }
3184
3185 /* Because the value of .cfi_remember_state may changed after relaxation,
3186 we insert a fix to relocate it again in link-time. */
3187
3188 void
3189 riscv_pre_output_hook (void)
3190 {
3191 const frchainS *frch;
3192 segT s;
3193
3194 /* Save the current segment info. */
3195 segT seg = now_seg;
3196 subsegT subseg = now_subseg;
3197
3198 for (s = stdoutput->sections; s; s = s->next)
3199 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
3200 {
3201 fragS *frag;
3202
3203 for (frag = frch->frch_root; frag; frag = frag->fr_next)
3204 {
3205 if (frag->fr_type == rs_cfa)
3206 {
3207 expressionS exp;
3208 expressionS *symval;
3209
3210 symval = symbol_get_value_expression (frag->fr_symbol);
3211 exp.X_op = O_subtract;
3212 exp.X_add_symbol = symval->X_add_symbol;
3213 exp.X_add_number = 0;
3214 exp.X_op_symbol = symval->X_op_symbol;
3215
3216 /* We must set the segment before creating a frag after all
3217 frag chains have been chained together. */
3218 subseg_set (s, frch->frch_subseg);
3219
3220 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
3221 BFD_RELOC_RISCV_CFA);
3222 }
3223 }
3224 }
3225
3226 /* Restore the original segment info. */
3227 subseg_set (seg, subseg);
3228 }
3229
3230 /* This structure is used to hold a stack of .option values. */
3231 struct riscv_option_stack
3232 {
3233 struct riscv_option_stack *next;
3234 struct riscv_set_options options;
3235 };
3236
3237 static struct riscv_option_stack *riscv_opts_stack;
3238
3239 /* Handle the .option pseudo-op. */
3240
3241 static void
3242 s_riscv_option (int x ATTRIBUTE_UNUSED)
3243 {
3244 char *name = input_line_pointer, ch;
3245
3246 while (!is_end_of_line[(unsigned char) *input_line_pointer])
3247 ++input_line_pointer;
3248 ch = *input_line_pointer;
3249 *input_line_pointer = '\0';
3250
3251 if (strcmp (name, "rvc") == 0)
3252 riscv_set_rvc (TRUE);
3253 else if (strcmp (name, "norvc") == 0)
3254 riscv_set_rvc (FALSE);
3255 else if (strcmp (name, "pic") == 0)
3256 riscv_opts.pic = TRUE;
3257 else if (strcmp (name, "nopic") == 0)
3258 riscv_opts.pic = FALSE;
3259 else if (strcmp (name, "relax") == 0)
3260 riscv_opts.relax = TRUE;
3261 else if (strcmp (name, "norelax") == 0)
3262 riscv_opts.relax = FALSE;
3263 else if (strcmp (name, "csr-check") == 0)
3264 riscv_opts.csr_check = TRUE;
3265 else if (strcmp (name, "no-csr-check") == 0)
3266 riscv_opts.csr_check = FALSE;
3267 else if (strcmp (name, "push") == 0)
3268 {
3269 struct riscv_option_stack *s;
3270
3271 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
3272 s->next = riscv_opts_stack;
3273 s->options = riscv_opts;
3274 riscv_opts_stack = s;
3275 }
3276 else if (strcmp (name, "pop") == 0)
3277 {
3278 struct riscv_option_stack *s;
3279
3280 s = riscv_opts_stack;
3281 if (s == NULL)
3282 as_bad (_(".option pop with no .option push"));
3283 else
3284 {
3285 riscv_opts = s->options;
3286 riscv_opts_stack = s->next;
3287 free (s);
3288 }
3289 }
3290 else
3291 {
3292 as_warn (_("unrecognized .option directive: %s\n"), name);
3293 }
3294 *input_line_pointer = ch;
3295 demand_empty_rest_of_line ();
3296 }
3297
3298 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
3299 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
3300 use in DWARF debug information. */
3301
3302 static void
3303 s_dtprel (int bytes)
3304 {
3305 expressionS ex;
3306 char *p;
3307
3308 expression (&ex);
3309
3310 if (ex.X_op != O_symbol)
3311 {
3312 as_bad (_("unsupported use of %s"), (bytes == 8
3313 ? ".dtpreldword"
3314 : ".dtprelword"));
3315 ignore_rest_of_line ();
3316 }
3317
3318 p = frag_more (bytes);
3319 md_number_to_chars (p, 0, bytes);
3320 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
3321 (bytes == 8
3322 ? BFD_RELOC_RISCV_TLS_DTPREL64
3323 : BFD_RELOC_RISCV_TLS_DTPREL32));
3324
3325 demand_empty_rest_of_line ();
3326 }
3327
3328 /* Handle the .bss pseudo-op. */
3329
3330 static void
3331 s_bss (int ignore ATTRIBUTE_UNUSED)
3332 {
3333 subseg_set (bss_section, 0);
3334 demand_empty_rest_of_line ();
3335 }
3336
3337 static void
3338 riscv_make_nops (char *buf, bfd_vma bytes)
3339 {
3340 bfd_vma i = 0;
3341
3342 /* RISC-V instructions cannot begin or end on odd addresses, so this case
3343 means we are not within a valid instruction sequence. It is thus safe
3344 to use a zero byte, even though that is not a valid instruction. */
3345 if (bytes % 2 == 1)
3346 buf[i++] = 0;
3347
3348 /* Use at most one 2-byte NOP. */
3349 if ((bytes - i) % 4 == 2)
3350 {
3351 number_to_chars_littleendian (buf + i, RVC_NOP, 2);
3352 i += 2;
3353 }
3354
3355 /* Fill the remainder with 4-byte NOPs. */
3356 for ( ; i < bytes; i += 4)
3357 number_to_chars_littleendian (buf + i, RISCV_NOP, 4);
3358 }
3359
3360 /* Called from md_do_align. Used to create an alignment frag in a
3361 code section by emitting a worst-case NOP sequence that the linker
3362 will later relax to the correct number of NOPs. We can't compute
3363 the correct alignment now because of other linker relaxations. */
3364
3365 bfd_boolean
3366 riscv_frag_align_code (int n)
3367 {
3368 bfd_vma bytes = (bfd_vma) 1 << n;
3369 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
3370 bfd_vma worst_case_bytes = bytes - insn_alignment;
3371 char *nops;
3372 expressionS ex;
3373
3374 /* If we are moving to a smaller alignment than the instruction size, then no
3375 alignment is required. */
3376 if (bytes <= insn_alignment)
3377 return TRUE;
3378
3379 /* When not relaxing, riscv_handle_align handles code alignment. */
3380 if (!riscv_opts.relax)
3381 return FALSE;
3382
3383 nops = frag_more (worst_case_bytes);
3384
3385 ex.X_op = O_constant;
3386 ex.X_add_number = worst_case_bytes;
3387
3388 riscv_make_nops (nops, worst_case_bytes);
3389
3390 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
3391 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
3392
3393 return TRUE;
3394 }
3395
3396 /* Implement HANDLE_ALIGN. */
3397
3398 void
3399 riscv_handle_align (fragS *fragP)
3400 {
3401 switch (fragP->fr_type)
3402 {
3403 case rs_align_code:
3404 /* When relaxing, riscv_frag_align_code handles code alignment. */
3405 if (!riscv_opts.relax)
3406 {
3407 bfd_signed_vma bytes = (fragP->fr_next->fr_address
3408 - fragP->fr_address - fragP->fr_fix);
3409 /* We have 4 byte uncompressed nops. */
3410 bfd_signed_vma size = 4;
3411 bfd_signed_vma excess = bytes % size;
3412 char *p = fragP->fr_literal + fragP->fr_fix;
3413
3414 if (bytes <= 0)
3415 break;
3416
3417 /* Insert zeros or compressed nops to get 4 byte alignment. */
3418 if (excess)
3419 {
3420 riscv_make_nops (p, excess);
3421 fragP->fr_fix += excess;
3422 p += excess;
3423 }
3424
3425 /* Insert variable number of 4 byte uncompressed nops. */
3426 riscv_make_nops (p, size);
3427 fragP->fr_var = size;
3428 }
3429 break;
3430
3431 default:
3432 break;
3433 }
3434 }
3435
3436 int
3437 md_estimate_size_before_relax (fragS *fragp, asection *segtype)
3438 {
3439 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE));
3440 }
3441
3442 /* Translate internal representation of relocation info to BFD target
3443 format. */
3444
3445 arelent *
3446 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
3447 {
3448 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
3449
3450 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
3451 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3452 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3453 reloc->addend = fixp->fx_addnumber;
3454
3455 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
3456 if (reloc->howto == NULL)
3457 {
3458 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
3459 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
3460 {
3461 /* We don't have R_RISCV_8/16, but for this special case,
3462 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
3463 return reloc;
3464 }
3465
3466 as_bad_where (fixp->fx_file, fixp->fx_line,
3467 _("cannot represent %s relocation in object file"),
3468 bfd_get_reloc_code_name (fixp->fx_r_type));
3469 return NULL;
3470 }
3471
3472 return reloc;
3473 }
3474
3475 int
3476 riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
3477 {
3478 if (RELAX_BRANCH_P (fragp->fr_subtype))
3479 {
3480 offsetT old_var = fragp->fr_var;
3481 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
3482 return fragp->fr_var - old_var;
3483 }
3484
3485 return 0;
3486 }
3487
3488 /* Expand far branches to multi-instruction sequences. */
3489
3490 static void
3491 md_convert_frag_branch (fragS *fragp)
3492 {
3493 bfd_byte *buf;
3494 expressionS exp;
3495 fixS *fixp;
3496 insn_t insn;
3497 int rs1, reloc;
3498
3499 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
3500
3501 exp.X_op = O_symbol;
3502 exp.X_add_symbol = fragp->fr_symbol;
3503 exp.X_add_number = fragp->fr_offset;
3504
3505 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
3506
3507 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
3508 {
3509 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
3510 {
3511 case 8:
3512 case 4:
3513 /* Expand the RVC branch into a RISC-V one. */
3514 insn = bfd_getl16 (buf);
3515 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
3516 if ((insn & MASK_C_J) == MATCH_C_J)
3517 insn = MATCH_JAL;
3518 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
3519 insn = MATCH_JAL | (X_RA << OP_SH_RD);
3520 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
3521 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
3522 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
3523 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
3524 else
3525 abort ();
3526 bfd_putl32 (insn, buf);
3527 break;
3528
3529 case 6:
3530 /* Invert the branch condition. Branch over the jump. */
3531 insn = bfd_getl16 (buf);
3532 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
3533 insn |= ENCODE_RVC_B_IMM (6);
3534 bfd_putl16 (insn, buf);
3535 buf += 2;
3536 goto jump;
3537
3538 case 2:
3539 /* Just keep the RVC branch. */
3540 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
3541 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
3542 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3543 2, &exp, FALSE, reloc);
3544 buf += 2;
3545 goto done;
3546
3547 default:
3548 abort ();
3549 }
3550 }
3551
3552 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
3553 {
3554 case 8:
3555 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
3556
3557 /* Invert the branch condition. Branch over the jump. */
3558 insn = bfd_getl32 (buf);
3559 insn ^= MATCH_BEQ ^ MATCH_BNE;
3560 insn |= ENCODE_SBTYPE_IMM (8);
3561 bfd_putl32 (insn, buf);
3562 buf += 4;
3563
3564 jump:
3565 /* Jump to the target. */
3566 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3567 4, &exp, FALSE, BFD_RELOC_RISCV_JMP);
3568 bfd_putl32 (MATCH_JAL, buf);
3569 buf += 4;
3570 break;
3571
3572 case 4:
3573 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
3574 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
3575 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3576 4, &exp, FALSE, reloc);
3577 buf += 4;
3578 break;
3579
3580 default:
3581 abort ();
3582 }
3583
3584 done:
3585 fixp->fx_file = fragp->fr_file;
3586 fixp->fx_line = fragp->fr_line;
3587
3588 gas_assert (buf == (bfd_byte *)fragp->fr_literal
3589 + fragp->fr_fix + fragp->fr_var);
3590
3591 fragp->fr_fix += fragp->fr_var;
3592 }
3593
3594 /* Relax a machine dependent frag. This returns the amount by which
3595 the current size of the frag should change. */
3596
3597 void
3598 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
3599 fragS *fragp)
3600 {
3601 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
3602 md_convert_frag_branch (fragp);
3603 }
3604
3605 void
3606 md_show_usage (FILE *stream)
3607 {
3608 fprintf (stream, _("\
3609 RISC-V options:\n\
3610 -fpic generate position-independent code\n\
3611 -fno-pic don't generate position-independent code (default)\n\
3612 -march=ISA set the RISC-V architecture\n\
3613 -misa-spec=ISAspec set the RISC-V ISA spec (2.2, 20190608, 20191213)\n\
3614 -mpriv-spec=PRIVspec set the RISC-V privilege spec (1.9, 1.9.1, 1.10, 1.11)\n\
3615 -mabi=ABI set the RISC-V ABI\n\
3616 -mrelax enable relax (default)\n\
3617 -mno-relax disable relax\n\
3618 -march-attr generate RISC-V arch attribute\n\
3619 -mno-arch-attr don't generate RISC-V arch attribute\n\
3620 "));
3621 }
3622
3623 /* Standard calling conventions leave the CFA at SP on entry. */
3624
3625 void
3626 riscv_cfi_frame_initial_instructions (void)
3627 {
3628 cfi_add_CFA_def_cfa_register (X_SP);
3629 }
3630
3631 int
3632 tc_riscv_regname_to_dw2regnum (char *regname)
3633 {
3634 int reg;
3635
3636 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
3637 return reg;
3638
3639 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
3640 return reg + 32;
3641
3642 /* CSRs are numbered 4096 -> 8191. */
3643 if ((reg = reg_lookup_internal (regname, RCLASS_CSR)) >= 0)
3644 return reg + 4096;
3645
3646 as_bad (_("unknown register `%s'"), regname);
3647 return -1;
3648 }
3649
3650 void
3651 riscv_elf_final_processing (void)
3652 {
3653 riscv_set_abi_by_arch ();
3654 elf_elfheader (stdoutput)->e_flags |= elf_flags;
3655 }
3656
3657 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
3658 since these directives break relaxation when used with symbol deltas. */
3659
3660 static void
3661 s_riscv_leb128 (int sign)
3662 {
3663 expressionS exp;
3664 char *save_in = input_line_pointer;
3665
3666 expression (&exp);
3667 if (exp.X_op != O_constant)
3668 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
3669 demand_empty_rest_of_line ();
3670
3671 input_line_pointer = save_in;
3672 return s_leb128 (sign);
3673 }
3674
3675 /* Parse the .insn directive. */
3676
3677 static void
3678 s_riscv_insn (int x ATTRIBUTE_UNUSED)
3679 {
3680 char *str = input_line_pointer;
3681 struct riscv_cl_insn insn;
3682 expressionS imm_expr;
3683 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
3684 char save_c;
3685
3686 while (!is_end_of_line[(unsigned char) *input_line_pointer])
3687 ++input_line_pointer;
3688
3689 save_c = *input_line_pointer;
3690 *input_line_pointer = '\0';
3691
3692 const char *error = riscv_ip (str, &insn, &imm_expr,
3693 &imm_reloc, insn_type_hash);
3694
3695 if (error)
3696 {
3697 as_bad ("%s `%s'", error, str);
3698 }
3699 else
3700 {
3701 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
3702 append_insn (&insn, &imm_expr, imm_reloc);
3703 }
3704
3705 *input_line_pointer = save_c;
3706 demand_empty_rest_of_line ();
3707 }
3708
3709 /* Update architecture and privileged elf attributes. If we don't set
3710 them, then try to output the default ones. */
3711
3712 static void
3713 riscv_write_out_attrs (void)
3714 {
3715 const char *arch_str, *priv_str, *p;
3716 /* versions[0]: major version.
3717 versions[1]: minor version.
3718 versions[2]: revision version. */
3719 unsigned versions[3] = {0}, number = 0;
3720 unsigned int i;
3721
3722 /* Re-write architecture elf attribute. */
3723 arch_str = riscv_arch_str (xlen, &riscv_subsets);
3724 bfd_elf_add_proc_attr_string (stdoutput, Tag_RISCV_arch, arch_str);
3725 xfree ((void *) arch_str);
3726
3727 /* For the file without any instruction, we don't set the default_priv_spec
3728 according to the privileged elf attributes since the md_assemble isn't
3729 called. */
3730 if (!start_assemble
3731 && !riscv_set_default_priv_spec (NULL))
3732 return;
3733
3734 /* If we already have set privileged elf attributes, then no need to do
3735 anything. Otherwise, don't generate or update them when no CSR and
3736 privileged instructions are used. */
3737 if (!explicit_priv_attr)
3738 return;
3739
3740 RISCV_GET_PRIV_SPEC_NAME (priv_str, default_priv_spec);
3741 p = priv_str;
3742 for (i = 0; *p; ++p)
3743 {
3744 if (*p == '.' && i < 3)
3745 {
3746 versions[i++] = number;
3747 number = 0;
3748 }
3749 else if (ISDIGIT (*p))
3750 number = (number * 10) + (*p - '0');
3751 else
3752 {
3753 as_bad (_("internal: bad RISC-V privileged spec (%s)"), priv_str);
3754 return;
3755 }
3756 }
3757 versions[i] = number;
3758
3759 /* Re-write privileged elf attributes. */
3760 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec, versions[0]);
3761 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_minor, versions[1]);
3762 bfd_elf_add_proc_attr_int (stdoutput, Tag_RISCV_priv_spec_revision, versions[2]);
3763 }
3764
3765 /* Add the default contents for the .riscv.attributes section. */
3766
3767 static void
3768 riscv_set_public_attributes (void)
3769 {
3770 if (riscv_opts.arch_attr || explicit_attr)
3771 riscv_write_out_attrs ();
3772 }
3773
3774 /* Called after all assembly has been done. */
3775
3776 void
3777 riscv_md_end (void)
3778 {
3779 riscv_set_public_attributes ();
3780 }
3781
3782 /* Given a symbolic attribute NAME, return the proper integer value.
3783 Returns -1 if the attribute is not known. */
3784
3785 int
3786 riscv_convert_symbolic_attribute (const char *name)
3787 {
3788 static const struct
3789 {
3790 const char *name;
3791 const int tag;
3792 }
3793 attribute_table[] =
3794 {
3795 /* When you modify this table you should
3796 also modify the list in doc/c-riscv.texi. */
3797 #define T(tag) {#tag, Tag_RISCV_##tag}, {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
3798 T(arch),
3799 T(priv_spec),
3800 T(priv_spec_minor),
3801 T(priv_spec_revision),
3802 T(unaligned_access),
3803 T(stack_align),
3804 #undef T
3805 };
3806
3807 if (name == NULL)
3808 return -1;
3809
3810 unsigned int i;
3811 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
3812 if (strcmp (name, attribute_table[i].name) == 0)
3813 return attribute_table[i].tag;
3814
3815 return -1;
3816 }
3817
3818 /* Parse a .attribute directive. */
3819
3820 static void
3821 s_riscv_attribute (int ignored ATTRIBUTE_UNUSED)
3822 {
3823 int tag = obj_elf_vendor_attribute (OBJ_ATTR_PROC);
3824 unsigned old_xlen;
3825 obj_attribute *attr;
3826
3827 explicit_attr = TRUE;
3828 switch (tag)
3829 {
3830 case Tag_RISCV_arch:
3831 old_xlen = xlen;
3832 attr = elf_known_obj_attributes_proc (stdoutput);
3833 if (!start_assemble)
3834 riscv_set_arch (attr[Tag_RISCV_arch].s);
3835 else
3836 as_fatal (_("architecture elf attributes must set before "
3837 "any instructions"));
3838
3839 if (old_xlen != xlen)
3840 {
3841 /* We must re-init bfd again if xlen is changed. */
3842 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
3843 bfd_find_target (riscv_target_format (), stdoutput);
3844
3845 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
3846 as_warn (_("could not set architecture and machine"));
3847 }
3848 break;
3849
3850 case Tag_RISCV_priv_spec:
3851 case Tag_RISCV_priv_spec_minor:
3852 case Tag_RISCV_priv_spec_revision:
3853 if (start_assemble)
3854 as_fatal (_("privileged elf attributes must set before "
3855 "any instructions"));
3856 break;
3857
3858 default:
3859 break;
3860 }
3861 }
3862
3863 /* RISC-V pseudo-ops table. */
3864 static const pseudo_typeS riscv_pseudo_table[] =
3865 {
3866 {"option", s_riscv_option, 0},
3867 {"half", cons, 2},
3868 {"word", cons, 4},
3869 {"dword", cons, 8},
3870 {"dtprelword", s_dtprel, 4},
3871 {"dtpreldword", s_dtprel, 8},
3872 {"bss", s_bss, 0},
3873 {"uleb128", s_riscv_leb128, 0},
3874 {"sleb128", s_riscv_leb128, 1},
3875 {"insn", s_riscv_insn, 0},
3876 {"attribute", s_riscv_attribute, 0},
3877
3878 { NULL, NULL, 0 },
3879 };
3880
3881 void
3882 riscv_pop_insert (void)
3883 {
3884 extern void pop_insert (const pseudo_typeS *);
3885
3886 pop_insert (riscv_pseudo_table);
3887 }