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