]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-riscv.c
RISC-V: Emit better warning for unknown CSR.
[thirdparty/binutils-gdb.git] / gas / config / tc-riscv.c
CommitLineData
e23eba97 1/* tc-riscv.c -- RISC-V assembler
219d1afa 2 Copyright (C) 2011-2018 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"
1d65abb5 31#include "struc-symbol.h"
e23eba97
NC
32
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
62static const char default_arch[] = DEFAULT_ARCH;
63
2922d21d
AW
64static unsigned xlen = 0; /* width of an x-register */
65static unsigned abi_xlen = 0; /* width of a pointer in the ABI */
e23eba97 66
2922d21d 67#define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
e23eba97
NC
68#define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
69
70static unsigned elf_flags = 0;
71
72/* This is the set of options which the .option pseudo-op may modify. */
73
74struct riscv_set_options
75{
76 int pic; /* Generate position-independent code. */
77 int rvc; /* Generate RVC code. */
45f76423 78 int relax; /* Emit relocs the linker is allowed to relax. */
e23eba97
NC
79};
80
81static struct riscv_set_options riscv_opts =
82{
83 0, /* pic */
84 0, /* rvc */
45f76423 85 1, /* relax */
e23eba97
NC
86};
87
88static void
89riscv_set_rvc (bfd_boolean rvc_value)
90{
91 if (rvc_value)
92 elf_flags |= EF_RISCV_RVC;
93
94 riscv_opts.rvc = rvc_value;
95}
96
97struct riscv_subset
98{
99 const char *name;
100
101 struct riscv_subset *next;
102};
103
104static struct riscv_subset *riscv_subsets;
105
106static bfd_boolean
107riscv_subset_supports (const char *feature)
108{
109 struct riscv_subset *s;
110 char *p;
111 unsigned xlen_required = strtoul (feature, &p, 10);
112
113 if (xlen_required && xlen != xlen_required)
114 return FALSE;
115
116 for (s = riscv_subsets; s != NULL; s = s->next)
117 if (strcasecmp (s->name, p) == 0)
118 return TRUE;
119
120 return FALSE;
121}
122
fecb9c46
PD
123static void
124riscv_clear_subsets (void)
125{
126 while (riscv_subsets != NULL)
127 {
128 struct riscv_subset *next = riscv_subsets->next;
c41cf6fd 129 free ((void *) riscv_subsets->name);
fecb9c46
PD
130 free (riscv_subsets);
131 riscv_subsets = next;
132 }
133}
134
e23eba97
NC
135static void
136riscv_add_subset (const char *subset)
137{
138 struct riscv_subset *s = xmalloc (sizeof *s);
139
140 s->name = xstrdup (subset);
141 s->next = riscv_subsets;
142 riscv_subsets = s;
143}
144
2922d21d 145/* Set which ISA and extensions are available. */
e23eba97 146
e23eba97 147static void
2922d21d 148riscv_set_arch (const char *s)
e23eba97 149{
a8086704
AW
150 const char *all_subsets = "imafdqc";
151 char *extension = NULL;
2922d21d 152 const char *p = s;
e23eba97 153
fecb9c46
PD
154 riscv_clear_subsets();
155
2922d21d 156 if (strncmp (p, "rv32", 4) == 0)
e23eba97
NC
157 {
158 xlen = 32;
159 p += 4;
160 }
2922d21d 161 else if (strncmp (p, "rv64", 4) == 0)
e23eba97
NC
162 {
163 xlen = 64;
164 p += 4;
165 }
2922d21d
AW
166 else
167 as_fatal ("-march=%s: ISA string must begin with rv32 or rv64", s);
e23eba97 168
2922d21d 169 switch (*p)
e23eba97 170 {
2922d21d 171 case 'i':
e23eba97
NC
172 break;
173
2922d21d 174 case 'g':
e23eba97 175 p++;
a8086704 176 for ( ; *all_subsets != 'q'; all_subsets++)
e23eba97 177 {
2922d21d 178 const char subset[] = {*all_subsets, '\0'};
e23eba97
NC
179 riscv_add_subset (subset);
180 }
181 break;
182
183 default:
2922d21d 184 as_fatal ("-march=%s: first ISA subset must be `i' or `g'", s);
e23eba97
NC
185 }
186
187 while (*p)
188 {
2922d21d 189 if (*p == 'x')
e23eba97 190 {
a8086704
AW
191 char *subset = xstrdup (p);
192 char *q = subset;
e23eba97
NC
193
194 while (*++q != '\0' && *q != '_')
195 ;
196 *q = '\0';
197
198 if (extension)
2922d21d
AW
199 as_fatal ("-march=%s: only one non-standard extension is supported"
200 " (found `%s' and `%s')", s, extension, subset);
e23eba97
NC
201 extension = subset;
202 riscv_add_subset (subset);
203 p += strlen (subset);
e23eba97
NC
204 }
205 else if (*p == '_')
206 p++;
207 else if ((all_subsets = strchr (all_subsets, *p)) != NULL)
208 {
209 const char subset[] = {*p, 0};
210 riscv_add_subset (subset);
e23eba97
NC
211 all_subsets++;
212 p++;
213 }
214 else
2922d21d 215 as_fatal ("-march=%s: unsupported ISA subset `%c'", s, *p);
e23eba97 216 }
a8086704
AW
217
218 free (extension);
e23eba97
NC
219}
220
221/* Handle of the OPCODE hash table. */
222static struct hash_control *op_hash = NULL;
223
0e35537d
JW
224/* Handle of the type of .insn hash table. */
225static struct hash_control *insn_type_hash = NULL;
226
e23eba97
NC
227/* This array holds the chars that always start a comment. If the
228 pre-processor is disabled, these aren't very useful */
229const char comment_chars[] = "#";
230
231/* This array holds the chars that only start a comment at the beginning of
232 a line. If the line seems to have the form '# 123 filename'
233 .line and .file directives will appear in the pre-processed output */
234/* Note that input_file.c hand checks for '#' at the beginning of the
235 first line of the input file. This is because the compiler outputs
236 #NO_APP at the beginning of its output. */
237/* Also note that C style comments are always supported. */
238const char line_comment_chars[] = "#";
239
240/* This array holds machine specific line separator characters. */
241const char line_separator_chars[] = ";";
242
243/* Chars that can be used to separate mant from exp in floating point nums */
244const char EXP_CHARS[] = "eE";
245
246/* Chars that mean this number is a floating point constant */
247/* As in 0f12.456 */
248/* or 0d1.2345e12 */
249const char FLT_CHARS[] = "rRsSfFdDxXpP";
250
251/* Macros for encoding relaxation state for RVC branches and far jumps. */
252#define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
253 ((relax_substateT) \
254 (0xc0000000 \
255 | ((uncond) ? 1 : 0) \
256 | ((rvc) ? 2 : 0) \
257 | ((length) << 2)))
258#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
259#define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
260#define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
261#define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
262
263/* Is the given value a sign-extended 32-bit value? */
264#define IS_SEXT_32BIT_NUM(x) \
265 (((x) &~ (offsetT) 0x7fffffff) == 0 \
266 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
267
268/* Is the given value a zero-extended 32-bit value? Or a negated one? */
269#define IS_ZEXT_32BIT_NUM(x) \
270 (((x) &~ (offsetT) 0xffffffff) == 0 \
271 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
272
273/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
274 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
275#define INSERT_OPERAND(FIELD, INSN, VALUE) \
276 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
277
278/* Determine if an instruction matches an opcode. */
279#define OPCODE_MATCHES(OPCODE, OP) \
280 (((OPCODE) & MASK_##OP) == MATCH_##OP)
281
282static char *expr_end;
283
284/* The default target format to use. */
285
286const char *
287riscv_target_format (void)
288{
289 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
290}
291
292/* Return the length of instruction INSN. */
293
294static inline unsigned int
295insn_length (const struct riscv_cl_insn *insn)
296{
297 return riscv_insn_length (insn->insn_opcode);
298}
299
300/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
301
302static void
303create_insn (struct riscv_cl_insn *insn, const struct riscv_opcode *mo)
304{
305 insn->insn_mo = mo;
306 insn->insn_opcode = mo->match;
307 insn->frag = NULL;
308 insn->where = 0;
309 insn->fixp = NULL;
310}
311
312/* Install INSN at the location specified by its "frag" and "where" fields. */
313
314static void
315install_insn (const struct riscv_cl_insn *insn)
316{
317 char *f = insn->frag->fr_literal + insn->where;
318 md_number_to_chars (f, insn->insn_opcode, insn_length (insn));
319}
320
321/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
322 and install the opcode in the new location. */
323
324static void
325move_insn (struct riscv_cl_insn *insn, fragS *frag, long where)
326{
327 insn->frag = frag;
328 insn->where = where;
329 if (insn->fixp != NULL)
330 {
331 insn->fixp->fx_frag = frag;
332 insn->fixp->fx_where = where;
333 }
334 install_insn (insn);
335}
336
337/* Add INSN to the end of the output. */
338
339static void
340add_fixed_insn (struct riscv_cl_insn *insn)
341{
342 char *f = frag_more (insn_length (insn));
343 move_insn (insn, frag_now, f - frag_now->fr_literal);
344}
345
346static void
347add_relaxed_insn (struct riscv_cl_insn *insn, int max_chars, int var,
348 relax_substateT subtype, symbolS *symbol, offsetT offset)
349{
350 frag_grow (max_chars);
351 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
352 frag_var (rs_machine_dependent, max_chars, var,
353 subtype, symbol, offset, NULL);
354}
355
356/* Compute the length of a branch sequence, and adjust the stored length
357 accordingly. If FRAGP is NULL, the worst-case length is returned. */
358
359static unsigned
360relaxed_branch_length (fragS *fragp, asection *sec, int update)
361{
362 int jump, rvc, length = 8;
363
364 if (!fragp)
365 return length;
366
367 jump = RELAX_BRANCH_UNCOND (fragp->fr_subtype);
368 rvc = RELAX_BRANCH_RVC (fragp->fr_subtype);
369 length = RELAX_BRANCH_LENGTH (fragp->fr_subtype);
370
371 /* Assume jumps are in range; the linker will catch any that aren't. */
372 length = jump ? 4 : 8;
373
374 if (fragp->fr_symbol != NULL
375 && S_IS_DEFINED (fragp->fr_symbol)
01156111 376 && !S_IS_WEAK (fragp->fr_symbol)
e23eba97
NC
377 && sec == S_GET_SEGMENT (fragp->fr_symbol))
378 {
379 offsetT val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
380 bfd_vma rvc_range = jump ? RVC_JUMP_REACH : RVC_BRANCH_REACH;
381 val -= fragp->fr_address + fragp->fr_fix;
382
383 if (rvc && (bfd_vma)(val + rvc_range/2) < rvc_range)
384 length = 2;
385 else if ((bfd_vma)(val + RISCV_BRANCH_REACH/2) < RISCV_BRANCH_REACH)
386 length = 4;
387 else if (!jump && rvc)
388 length = 6;
389 }
390
391 if (update)
392 fragp->fr_subtype = RELAX_BRANCH_ENCODE (jump, rvc, length);
393
394 return length;
395}
396
0e35537d
JW
397/* Information about an opcode name, mnemonics and its value. */
398struct opcode_name_t
399{
400 const char *name;
401 unsigned int val;
402};
403
404/* List for all supported opcode name. */
405static const struct opcode_name_t opcode_name_list[] =
406{
407 {"C0", 0x0},
408 {"C1", 0x1},
409 {"C2", 0x2},
410
411 {"LOAD", 0x03},
412 {"LOAD_FP", 0x07},
413 {"CUSTOM_0", 0x0b},
414 {"MISC_MEM", 0x0f},
415 {"OP_IMM", 0x13},
416 {"AUIPC", 0x17},
417 {"OP_IMM_32", 0x1b},
418 /* 48b 0x1f. */
419
420 {"STORE", 0x23},
421 {"STORE_FP", 0x27},
422 {"CUSTOM_1", 0x2b},
423 {"AMO", 0x2f},
424 {"OP", 0x33},
425 {"LUI", 0x37},
426 {"OP_32", 0x3b},
427 /* 64b 0x3f. */
428
429 {"MADD", 0x43},
430 {"MSUB", 0x47},
431 {"NMADD", 0x4f},
432 {"NMSUB", 0x4b},
433 {"OP_FP", 0x53},
434 /*reserved 0x57. */
435 {"CUSTOM_2", 0x5b},
436 /* 48b 0x5f. */
437
438 {"BRANCH", 0x63},
439 {"JALR", 0x67},
440 /*reserved 0x5b. */
441 {"JAL", 0x6f},
442 {"SYSTEM", 0x73},
443 /*reserved 0x77. */
444 {"CUSTOM_3", 0x7b},
445 /* >80b 0x7f. */
446
447 {NULL, 0}
448};
449
450/* Hash table for lookup opcode name. */
451static struct hash_control *opcode_names_hash = NULL;
452
453/* Initialization for hash table of opcode name. */
454static void
455init_opcode_names_hash (void)
456{
457 const char *retval;
458 const struct opcode_name_t *opcode;
459
460 for (opcode = &opcode_name_list[0]; opcode->name != NULL; ++opcode)
461 {
462 retval = hash_insert (opcode_names_hash, opcode->name, (void *)opcode);
463
464 if (retval != NULL)
465 as_fatal (_("internal error: can't hash `%s': %s"),
466 opcode->name, retval);
467 }
468}
469
470/* Find `s` is a valid opcode name or not,
471 return the opcode name info if found. */
472static const struct opcode_name_t *
473opcode_name_lookup (char **s)
474{
475 char *e;
476 char save_c;
477 struct opcode_name_t *o;
478
479 /* Find end of name. */
480 e = *s;
481 if (is_name_beginner (*e))
482 ++e;
483 while (is_part_of_name (*e))
484 ++e;
485
486 /* Terminate name. */
487 save_c = *e;
488 *e = '\0';
489
490 o = (struct opcode_name_t *) hash_find (opcode_names_hash, *s);
491
492 /* Advance to next token if one was recognized. */
493 if (o)
494 *s = e;
495
496 *e = save_c;
497 expr_end = e;
498
499 return o;
500}
501
e23eba97
NC
502struct regname
503{
504 const char *name;
505 unsigned int num;
506};
507
508enum reg_class
509{
510 RCLASS_GPR,
511 RCLASS_FPR,
512 RCLASS_CSR,
513 RCLASS_MAX
514};
515
516static struct hash_control *reg_names_hash = NULL;
517
518#define ENCODE_REG_HASH(cls, n) \
519 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
520#define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
521#define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
522
523static void
524hash_reg_name (enum reg_class class, const char *name, unsigned n)
525{
526 void *hash = ENCODE_REG_HASH (class, n);
527 const char *retval = hash_insert (reg_names_hash, name, hash);
528
529 if (retval != NULL)
530 as_fatal (_("internal error: can't hash `%s': %s"), name, retval);
531}
532
533static void
534hash_reg_names (enum reg_class class, const char * const names[], unsigned n)
535{
536 unsigned i;
537
538 for (i = 0; i < n; i++)
539 hash_reg_name (class, names[i], i);
540}
541
542static unsigned int
543reg_lookup_internal (const char *s, enum reg_class class)
544{
545 struct regname *r = (struct regname *) hash_find (reg_names_hash, s);
546
547 if (r == NULL || DECODE_REG_CLASS (r) != class)
548 return -1;
549 return DECODE_REG_NUM (r);
550}
551
552static bfd_boolean
553reg_lookup (char **s, enum reg_class class, unsigned int *regnop)
554{
555 char *e;
556 char save_c;
557 int reg = -1;
558
559 /* Find end of name. */
560 e = *s;
561 if (is_name_beginner (*e))
562 ++e;
563 while (is_part_of_name (*e))
564 ++e;
565
566 /* Terminate name. */
567 save_c = *e;
568 *e = '\0';
569
570 /* Look for the register. Advance to next token if one was recognized. */
571 if ((reg = reg_lookup_internal (*s, class)) >= 0)
572 *s = e;
573
574 *e = save_c;
575 if (regnop)
576 *regnop = reg;
577 return reg >= 0;
578}
579
580static bfd_boolean
581arg_lookup (char **s, const char *const *array, size_t size, unsigned *regnop)
582{
583 const char *p = strchr (*s, ',');
584 size_t i, len = p ? (size_t)(p - *s) : strlen (*s);
585
586 for (i = 0; i < size; i++)
587 if (array[i] != NULL && strncmp (array[i], *s, len) == 0)
588 {
589 *regnop = i;
590 *s += len;
591 return TRUE;
592 }
593
594 return FALSE;
595}
596
597/* For consistency checking, verify that all bits are specified either
598 by the match/mask part of the instruction definition, or by the
0e35537d
JW
599 operand list.
600
601 `length` could be 0, 4 or 8, 0 for auto detection. */
e23eba97 602static bfd_boolean
0e35537d 603validate_riscv_insn (const struct riscv_opcode *opc, int length)
e23eba97
NC
604{
605 const char *p = opc->args;
606 char c;
607 insn_t used_bits = opc->mask;
0e35537d
JW
608 int insn_width;
609 insn_t required_bits;
610
611 if (length == 0)
612 insn_width = 8 * riscv_insn_length (opc->match);
613 else
614 insn_width = 8 * length;
615
616 required_bits = ~0ULL >> (64 - insn_width);
e23eba97
NC
617
618 if ((used_bits & opc->match) != (opc->match & required_bits))
619 {
620 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
621 opc->name, opc->args);
622 return FALSE;
623 }
624
625#define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
626 while (*p)
627 switch (c = *p++)
628 {
629 case 'C': /* RVC */
630 switch (c = *p++)
631 {
1d65abb5 632 case 'a': used_bits |= ENCODE_RVC_J_IMM (-1U); break;
e23eba97
NC
633 case 'c': break; /* RS1, constrained to equal sp */
634 case 'i': used_bits |= ENCODE_RVC_SIMM3(-1U); break;
1d65abb5 635 case 'j': used_bits |= ENCODE_RVC_IMM (-1U); break;
b416fe87 636 case 'o': used_bits |= ENCODE_RVC_IMM (-1U); break;
1d65abb5
AW
637 case 'k': used_bits |= ENCODE_RVC_LW_IMM (-1U); break;
638 case 'l': used_bits |= ENCODE_RVC_LD_IMM (-1U); break;
639 case 'm': used_bits |= ENCODE_RVC_LWSP_IMM (-1U); break;
640 case 'n': used_bits |= ENCODE_RVC_LDSP_IMM (-1U); break;
641 case 'p': used_bits |= ENCODE_RVC_B_IMM (-1U); break;
e23eba97
NC
642 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
643 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
1d65abb5
AW
644 case 'u': used_bits |= ENCODE_RVC_IMM (-1U); break;
645 case 'v': used_bits |= ENCODE_RVC_IMM (-1U); break;
e23eba97
NC
646 case 'w': break; /* RS1S, constrained to equal RD */
647 case 'x': break; /* RS2S, constrained to equal RD */
1d65abb5
AW
648 case 'K': used_bits |= ENCODE_RVC_ADDI4SPN_IMM (-1U); break;
649 case 'L': used_bits |= ENCODE_RVC_ADDI16SP_IMM (-1U); break;
650 case 'M': used_bits |= ENCODE_RVC_SWSP_IMM (-1U); break;
651 case 'N': used_bits |= ENCODE_RVC_SDSP_IMM (-1U); break;
e23eba97
NC
652 case 'U': break; /* RS1, constrained to equal RD */
653 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
1d65abb5
AW
654 case '<': used_bits |= ENCODE_RVC_IMM (-1U); break;
655 case '>': used_bits |= ENCODE_RVC_IMM (-1U); break;
0e35537d
JW
656 case '8': used_bits |= ENCODE_RVC_UIMM8 (-1U); break;
657 case 'S': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
e23eba97
NC
658 case 'T': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
659 case 'D': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
0e35537d
JW
660 case 'F': /* funct */
661 switch (c = *p++)
662 {
663 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
664 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
665 default:
666 as_bad (_("internal: bad RISC-V opcode"
667 " (unknown operand type `CF%c'): %s %s"),
668 c, opc->name, opc->args);
669 return FALSE;
670 }
671 break;
e23eba97
NC
672 default:
673 as_bad (_("internal: bad RISC-V opcode (unknown operand type `C%c'): %s %s"),
674 c, opc->name, opc->args);
675 return FALSE;
676 }
677 break;
678 case ',': break;
679 case '(': break;
680 case ')': break;
681 case '<': USE_BITS (OP_MASK_SHAMTW, OP_SH_SHAMTW); break;
682 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
683 case 'A': break;
684 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
685 case 'Z': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
686 case 'E': USE_BITS (OP_MASK_CSR, OP_SH_CSR); break;
687 case 'I': break;
688 case 'R': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
689 case 'S': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
690 case 'U': USE_BITS (OP_MASK_RS1, OP_SH_RS1); /* fallthru */
691 case 'T': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
692 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
693 case 'm': USE_BITS (OP_MASK_RM, OP_SH_RM); break;
694 case 's': USE_BITS (OP_MASK_RS1, OP_SH_RS1); break;
695 case 't': USE_BITS (OP_MASK_RS2, OP_SH_RS2); break;
0e35537d 696 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
e23eba97
NC
697 case 'P': USE_BITS (OP_MASK_PRED, OP_SH_PRED); break;
698 case 'Q': USE_BITS (OP_MASK_SUCC, OP_SH_SUCC); break;
699 case 'o':
1d65abb5
AW
700 case 'j': used_bits |= ENCODE_ITYPE_IMM (-1U); break;
701 case 'a': used_bits |= ENCODE_UJTYPE_IMM (-1U); break;
702 case 'p': used_bits |= ENCODE_SBTYPE_IMM (-1U); break;
703 case 'q': used_bits |= ENCODE_STYPE_IMM (-1U); break;
704 case 'u': used_bits |= ENCODE_UTYPE_IMM (-1U); break;
e925c834 705 case 'z': break;
e23eba97
NC
706 case '[': break;
707 case ']': break;
708 case '0': break;
0e35537d
JW
709 case 'F': /* funct */
710 switch (c = *p++)
711 {
712 case '7': USE_BITS (OP_MASK_FUNCT7, OP_SH_FUNCT7); break;
713 case '3': USE_BITS (OP_MASK_FUNCT3, OP_SH_FUNCT3); break;
714 case '2': USE_BITS (OP_MASK_FUNCT2, OP_SH_FUNCT2); break;
715 default:
716 as_bad (_("internal: bad RISC-V opcode"
717 " (unknown operand type `F%c'): %s %s"),
718 c, opc->name, opc->args);
719 return FALSE;
720 }
721 break;
722 case 'O': /* opcode */
723 switch (c = *p++)
724 {
725 case '4': USE_BITS (OP_MASK_OP, OP_SH_OP); break;
726 case '2': USE_BITS (OP_MASK_OP2, OP_SH_OP2); break;
727 default:
728 as_bad (_("internal: bad RISC-V opcode"
729 " (unknown operand type `F%c'): %s %s"),
730 c, opc->name, opc->args);
731 return FALSE;
732 }
733 break;
e23eba97
NC
734 default:
735 as_bad (_("internal: bad RISC-V opcode "
736 "(unknown operand type `%c'): %s %s"),
737 c, opc->name, opc->args);
738 return FALSE;
739 }
740#undef USE_BITS
741 if (used_bits != required_bits)
742 {
743 as_bad (_("internal: bad RISC-V opcode (bits 0x%lx undefined): %s %s"),
744 ~(unsigned long)(used_bits & required_bits),
745 opc->name, opc->args);
746 return FALSE;
747 }
748 return TRUE;
749}
750
751struct percent_op_match
752{
753 const char *str;
754 bfd_reloc_code_real_type reloc;
755};
756
0e35537d
JW
757/* Common hash table initialization function for
758 instruction and .insn directive. */
759static struct hash_control *
760init_opcode_hash (const struct riscv_opcode *opcodes,
761 bfd_boolean insn_directive_p)
e23eba97
NC
762{
763 int i = 0;
0e35537d
JW
764 int length;
765 struct hash_control *hash = hash_new ();
766 while (opcodes[i].name)
e23eba97 767 {
0e35537d 768 const char *name = opcodes[i].name;
e23eba97 769 const char *hash_error =
0e35537d 770 hash_insert (hash, name, (void *) &opcodes[i]);
e23eba97
NC
771
772 if (hash_error)
773 {
774 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
0e35537d 775 opcodes[i].name, hash_error);
e23eba97
NC
776 /* Probably a memory allocation problem? Give up now. */
777 as_fatal (_("Broken assembler. No assembly attempted."));
778 }
779
780 do
781 {
0e35537d 782 if (opcodes[i].pinfo != INSN_MACRO)
e23eba97 783 {
0e35537d
JW
784 if (insn_directive_p)
785 length = ((name[0] == 'c') ? 2 : 4);
786 else
787 length = 0; /* Let assembler determine the length. */
788 if (!validate_riscv_insn (&opcodes[i], length))
e23eba97
NC
789 as_fatal (_("Broken assembler. No assembly attempted."));
790 }
0e35537d
JW
791 else
792 gas_assert (!insn_directive_p);
e23eba97
NC
793 ++i;
794 }
0e35537d 795 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
e23eba97
NC
796 }
797
0e35537d
JW
798 return hash;
799}
800
801/* This function is called once, at assembler startup time. It should set up
802 all the tables, etc. that the MD part of the assembler will need. */
803
804void
805md_begin (void)
806{
807 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
808
809 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
810 as_warn (_("Could not set architecture and machine"));
811
812 op_hash = init_opcode_hash (riscv_opcodes, FALSE);
813 insn_type_hash = init_opcode_hash (riscv_insn_types, TRUE);
814
e23eba97
NC
815 reg_names_hash = hash_new ();
816 hash_reg_names (RCLASS_GPR, riscv_gpr_names_numeric, NGPR);
817 hash_reg_names (RCLASS_GPR, riscv_gpr_names_abi, NGPR);
818 hash_reg_names (RCLASS_FPR, riscv_fpr_names_numeric, NFPR);
819 hash_reg_names (RCLASS_FPR, riscv_fpr_names_abi, NFPR);
820
0e35537d
JW
821 opcode_names_hash = hash_new ();
822 init_opcode_names_hash ();
823
e23eba97 824#define DECLARE_CSR(name, num) hash_reg_name (RCLASS_CSR, #name, num);
1270b047 825#define DECLARE_CSR_ALIAS(name, num) DECLARE_CSR(name, num);
e23eba97
NC
826#include "opcode/riscv-opc.h"
827#undef DECLARE_CSR
828
829 /* Set the default alignment for the text section. */
830 record_alignment (text_section, riscv_opts.rvc ? 1 : 2);
831}
832
45f76423
AW
833static insn_t
834riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type, bfd_vma value)
835{
836 switch (reloc_type)
837 {
838 case BFD_RELOC_32:
839 return value;
840
841 case BFD_RELOC_RISCV_HI20:
842 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
843
844 case BFD_RELOC_RISCV_LO12_S:
845 return ENCODE_STYPE_IMM (value);
846
847 case BFD_RELOC_RISCV_LO12_I:
848 return ENCODE_ITYPE_IMM (value);
849
850 default:
851 abort ();
852 }
853}
854
e23eba97
NC
855/* Output an instruction. IP is the instruction information.
856 ADDRESS_EXPR is an operand of the instruction to be used with
857 RELOC_TYPE. */
858
859static void
860append_insn (struct riscv_cl_insn *ip, expressionS *address_expr,
861 bfd_reloc_code_real_type reloc_type)
862{
863 dwarf2_emit_insn (0);
864
865 if (reloc_type != BFD_RELOC_UNUSED)
866 {
867 reloc_howto_type *howto;
868
1d65abb5 869 gas_assert (address_expr);
e23eba97
NC
870 if (reloc_type == BFD_RELOC_12_PCREL
871 || reloc_type == BFD_RELOC_RISCV_JMP)
872 {
873 int j = reloc_type == BFD_RELOC_RISCV_JMP;
874 int best_case = riscv_insn_length (ip->insn_opcode);
875 unsigned worst_case = relaxed_branch_length (NULL, NULL, 0);
876 add_relaxed_insn (ip, worst_case, best_case,
877 RELAX_BRANCH_ENCODE (j, best_case == 2, worst_case),
878 address_expr->X_add_symbol,
879 address_expr->X_add_number);
880 return;
881 }
45f76423 882 else
e23eba97 883 {
45f76423
AW
884 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
885 if (howto == NULL)
886 as_bad (_("Unsupported RISC-V relocation number %d"), reloc_type);
e23eba97 887
45f76423
AW
888 ip->fixp = fix_new_exp (ip->frag, ip->where,
889 bfd_get_reloc_size (howto),
890 address_expr, FALSE, reloc_type);
e23eba97 891
45f76423 892 ip->fixp->fx_tcbit = riscv_opts.relax;
e23eba97 893 }
e23eba97
NC
894 }
895
e23eba97
NC
896 add_fixed_insn (ip);
897 install_insn (ip);
f77bb6c5
JW
898
899 /* We need to start a new frag after any instruction that can be
900 optimized away or compressed by the linker during relaxation, to prevent
901 the assembler from computing static offsets across such an instruction.
902 This is necessary to get correct EH info. */
903 if (reloc_type == BFD_RELOC_RISCV_CALL
904 || reloc_type == BFD_RELOC_RISCV_CALL_PLT
905 || reloc_type == BFD_RELOC_RISCV_HI20
906 || reloc_type == BFD_RELOC_RISCV_PCREL_HI20
907 || reloc_type == BFD_RELOC_RISCV_TPREL_HI20
908 || reloc_type == BFD_RELOC_RISCV_TPREL_ADD)
909 {
910 frag_wane (frag_now);
911 frag_new (0);
912 }
e23eba97
NC
913}
914
915/* Build an instruction created by a macro expansion. This is passed
916 a pointer to the count of instructions created so far, an
917 expression, the name of the instruction to build, an operand format
918 string, and corresponding arguments. */
919
920static void
921macro_build (expressionS *ep, const char *name, const char *fmt, ...)
922{
923 const struct riscv_opcode *mo;
924 struct riscv_cl_insn insn;
925 bfd_reloc_code_real_type r;
926 va_list args;
927
928 va_start (args, fmt);
929
930 r = BFD_RELOC_UNUSED;
931 mo = (struct riscv_opcode *) hash_find (op_hash, name);
932 gas_assert (mo);
933
934 /* Find a non-RVC variant of the instruction. append_insn will compress
935 it if possible. */
936 while (riscv_insn_length (mo->match) < 4)
937 mo++;
938 gas_assert (strcmp (name, mo->name) == 0);
939
940 create_insn (&insn, mo);
941 for (;;)
942 {
943 switch (*fmt++)
944 {
945 case 'd':
946 INSERT_OPERAND (RD, insn, va_arg (args, int));
947 continue;
948
949 case 's':
950 INSERT_OPERAND (RS1, insn, va_arg (args, int));
951 continue;
952
953 case 't':
954 INSERT_OPERAND (RS2, insn, va_arg (args, int));
955 continue;
956
957 case '>':
958 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
959 continue;
960
961 case 'j':
962 case 'u':
963 case 'q':
964 gas_assert (ep != NULL);
965 r = va_arg (args, int);
966 continue;
967
968 case '\0':
969 break;
970 case ',':
971 continue;
972 default:
973 as_fatal (_("internal error: invalid macro"));
974 }
975 break;
976 }
977 va_end (args);
978 gas_assert (r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
979
980 append_insn (&insn, ep, r);
981}
982
983/* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
984 unset. */
985static void
986normalize_constant_expr (expressionS *ex)
987{
988 if (xlen > 32)
989 return;
990 if ((ex->X_op == O_constant || ex->X_op == O_symbol)
991 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
992 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
993 - 0x80000000);
994}
995
ca2fd32c
JW
996/* Fail if an expression EX is not a constant. IP is the instruction using EX.
997 MAYBE_CSR is true if the symbol may be an unrecognized CSR name. */
e23eba97
NC
998
999static void
ca2fd32c
JW
1000check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
1001 bfd_boolean maybe_csr)
e23eba97
NC
1002{
1003 if (ex->X_op == O_big)
1004 as_bad (_("unsupported large constant"));
ca2fd32c
JW
1005 else if (maybe_csr && ex->X_op == O_symbol)
1006 as_bad (_("unknown CSR `%s'"),
1007 S_GET_NAME (ex->X_add_symbol));
e23eba97
NC
1008 else if (ex->X_op != O_constant)
1009 as_bad (_("Instruction %s requires absolute expression"),
1010 ip->insn_mo->name);
1011 normalize_constant_expr (ex);
1012}
1013
1014static symbolS *
1015make_internal_label (void)
1016{
1017 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg,
1d65abb5 1018 (valueT) frag_now_fix (), frag_now);
e23eba97
NC
1019}
1020
1021/* Load an entry from the GOT. */
1022static void
1023pcrel_access (int destreg, int tempreg, expressionS *ep,
1024 const char *lo_insn, const char *lo_pattern,
1025 bfd_reloc_code_real_type hi_reloc,
1026 bfd_reloc_code_real_type lo_reloc)
1027{
1028 expressionS ep2;
1029 ep2.X_op = O_symbol;
1030 ep2.X_add_symbol = make_internal_label ();
1031 ep2.X_add_number = 0;
1032
1033 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1034 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1035}
1036
1037static void
1038pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1039 bfd_reloc_code_real_type hi_reloc,
1040 bfd_reloc_code_real_type lo_reloc)
1041{
1042 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1043}
1044
1045static void
1046pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1047 bfd_reloc_code_real_type hi_reloc,
1048 bfd_reloc_code_real_type lo_reloc)
1049{
1050 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1051}
1052
1053/* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1054static void
1055riscv_call (int destreg, int tempreg, expressionS *ep,
1056 bfd_reloc_code_real_type reloc)
1057{
1058 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1059 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1060}
1061
1062/* Load an integer constant into a register. */
1063
1064static void
1065load_const (int reg, expressionS *ep)
1066{
1067 int shift = RISCV_IMM_BITS;
1068 expressionS upper = *ep, lower = *ep;
1069 lower.X_add_number = (int32_t) ep->X_add_number << (32-shift) >> (32-shift);
1070 upper.X_add_number -= lower.X_add_number;
1071
1072 if (ep->X_op != O_constant)
1073 {
1074 as_bad (_("unsupported large constant"));
1075 return;
1076 }
1077
1d65abb5 1078 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
e23eba97
NC
1079 {
1080 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1081 while (((upper.X_add_number >> shift) & 1) == 0)
1082 shift++;
1083
1084 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1d65abb5 1085 load_const (reg, &upper);
e23eba97
NC
1086
1087 macro_build (NULL, "slli", "d,s,>", reg, reg, shift);
1088 if (lower.X_add_number != 0)
1089 macro_build (&lower, "addi", "d,s,j", reg, reg, BFD_RELOC_RISCV_LO12_I);
1090 }
1091 else
1092 {
1093 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1094 int hi_reg = 0;
1095
1096 if (upper.X_add_number != 0)
1097 {
1098 macro_build (ep, "lui", "d,u", reg, BFD_RELOC_RISCV_HI20);
1099 hi_reg = reg;
1100 }
1101
1102 if (lower.X_add_number != 0 || hi_reg == 0)
1103 macro_build (ep, ADD32_INSN, "d,s,j", reg, hi_reg,
1104 BFD_RELOC_RISCV_LO12_I);
1105 }
1106}
1107
1108/* Expand RISC-V assembly macros into one or more instructions. */
1109static void
1110macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1111 bfd_reloc_code_real_type *imm_reloc)
1112{
1113 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
1114 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
1115 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
1116 int mask = ip->insn_mo->mask;
1117
1118 switch (mask)
1119 {
1120 case M_LI:
1121 load_const (rd, imm_expr);
1122 break;
1123
1124 case M_LA:
1125 case M_LLA:
1126 /* Load the address of a symbol into a register. */
1127 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
1128 as_bad (_("offset too large"));
1129
1130 if (imm_expr->X_op == O_constant)
1131 load_const (rd, imm_expr);
1132 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol */
1133 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1134 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1135 else /* Local PIC symbol, or any non-PIC symbol */
1136 pcrel_load (rd, rd, imm_expr, "addi",
1137 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1138 break;
1139
1140 case M_LA_TLS_GD:
1141 pcrel_load (rd, rd, imm_expr, "addi",
1142 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1143 break;
1144
1145 case M_LA_TLS_IE:
1146 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1147 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1148 break;
1149
1150 case M_LB:
1151 pcrel_load (rd, rd, imm_expr, "lb",
1152 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1153 break;
1154
1155 case M_LBU:
1156 pcrel_load (rd, rd, imm_expr, "lbu",
1157 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1158 break;
1159
1160 case M_LH:
1161 pcrel_load (rd, rd, imm_expr, "lh",
1162 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1163 break;
1164
1165 case M_LHU:
1166 pcrel_load (rd, rd, imm_expr, "lhu",
1167 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1168 break;
1169
1170 case M_LW:
1171 pcrel_load (rd, rd, imm_expr, "lw",
1172 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1173 break;
1174
1175 case M_LWU:
1176 pcrel_load (rd, rd, imm_expr, "lwu",
1177 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1178 break;
1179
1180 case M_LD:
1181 pcrel_load (rd, rd, imm_expr, "ld",
1182 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1183 break;
1184
1185 case M_FLW:
1186 pcrel_load (rd, rs1, imm_expr, "flw",
1187 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1188 break;
1189
1190 case M_FLD:
1191 pcrel_load (rd, rs1, imm_expr, "fld",
1192 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1193 break;
1194
1195 case M_SB:
1196 pcrel_store (rs2, rs1, imm_expr, "sb",
1197 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1198 break;
1199
1200 case M_SH:
1201 pcrel_store (rs2, rs1, imm_expr, "sh",
1202 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1203 break;
1204
1205 case M_SW:
1206 pcrel_store (rs2, rs1, imm_expr, "sw",
1207 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1208 break;
1209
1210 case M_SD:
1211 pcrel_store (rs2, rs1, imm_expr, "sd",
1212 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1213 break;
1214
1215 case M_FSW:
1216 pcrel_store (rs2, rs1, imm_expr, "fsw",
1217 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1218 break;
1219
1220 case M_FSD:
1221 pcrel_store (rs2, rs1, imm_expr, "fsd",
1222 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1223 break;
1224
1225 case M_CALL:
1226 riscv_call (rd, rs1, imm_expr, *imm_reloc);
1227 break;
1228
1229 default:
1230 as_bad (_("Macro %s not implemented"), ip->insn_mo->name);
1231 break;
1232 }
1233}
1234
1235static const struct percent_op_match percent_op_utype[] =
1236{
1237 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
1238 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
1239 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
1240 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
1241 {"%hi", BFD_RELOC_RISCV_HI20},
1242 {0, 0}
1243};
1244
1245static const struct percent_op_match percent_op_itype[] =
1246{
1247 {"%lo", BFD_RELOC_RISCV_LO12_I},
1248 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
1249 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
1250 {0, 0}
1251};
1252
1253static const struct percent_op_match percent_op_stype[] =
1254{
1255 {"%lo", BFD_RELOC_RISCV_LO12_S},
1256 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
1257 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
1258 {0, 0}
1259};
1260
1261static const struct percent_op_match percent_op_rtype[] =
1262{
1263 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1264 {0, 0}
1265};
1266
1267/* Return true if *STR points to a relocation operator. When returning true,
1268 move *STR over the operator and store its relocation code in *RELOC.
1269 Leave both *STR and *RELOC alone when returning false. */
1270
1271static bfd_boolean
1272parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
1273 const struct percent_op_match *percent_op)
1274{
1275 for ( ; percent_op->str; percent_op++)
1276 if (strncasecmp (*str, percent_op->str, strlen (percent_op->str)) == 0)
1277 {
1278 int len = strlen (percent_op->str);
1279
1280 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
1281 continue;
1282
1283 *str += strlen (percent_op->str);
1284 *reloc = percent_op->reloc;
1285
1286 /* Check whether the output BFD supports this relocation.
1287 If not, issue an error and fall back on something safe. */
45f76423
AW
1288 if (*reloc != BFD_RELOC_UNUSED
1289 && !bfd_reloc_type_lookup (stdoutput, *reloc))
e23eba97
NC
1290 {
1291 as_bad ("relocation %s isn't supported by the current ABI",
1292 percent_op->str);
1293 *reloc = BFD_RELOC_UNUSED;
1294 }
1295 return TRUE;
1296 }
1297 return FALSE;
1298}
1299
1300static void
1301my_getExpression (expressionS *ep, char *str)
1302{
1303 char *save_in;
1304
1305 save_in = input_line_pointer;
1306 input_line_pointer = str;
1307 expression (ep);
1308 expr_end = input_line_pointer;
1309 input_line_pointer = save_in;
1310}
1311
1312/* Parse string STR as a 16-bit relocatable operand. Store the
1313 expression in *EP and the relocation, if any, in RELOC.
1314 Return the number of relocation operators used (0 or 1).
1315
1316 On exit, EXPR_END points to the first character after the expression. */
1317
1318static size_t
1319my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1320 char *str, const struct percent_op_match *percent_op)
1321{
1322 size_t reloc_index;
1323 unsigned crux_depth, str_depth, regno;
1324 char *crux;
1325
1326 /* First, check for integer registers. */
1327 if (reg_lookup (&str, RCLASS_GPR, &regno))
1328 {
1329 ep->X_op = O_register;
1330 ep->X_add_number = regno;
1331 return 0;
1332 }
1333
1334 /* Search for the start of the main expression.
1335 End the loop with CRUX pointing to the start
1336 of the main expression and with CRUX_DEPTH containing the number
1337 of open brackets at that point. */
1338 reloc_index = -1;
1339 str_depth = 0;
1340 do
1341 {
1342 reloc_index++;
1343 crux = str;
1344 crux_depth = str_depth;
1345
1346 /* Skip over whitespace and brackets, keeping count of the number
1347 of brackets. */
1348 while (*str == ' ' || *str == '\t' || *str == '(')
1349 if (*str++ == '(')
1350 str_depth++;
1351 }
1352 while (*str == '%'
1353 && reloc_index < 1
1354 && parse_relocation (&str, reloc, percent_op));
1355
1356 my_getExpression (ep, crux);
1357 str = expr_end;
1358
1359 /* Match every open bracket. */
1360 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
1361 if (*str++ == ')')
1362 crux_depth--;
1363
1364 if (crux_depth > 0)
1365 as_bad ("unclosed '('");
1366
1367 expr_end = str;
1368
1369 return reloc_index;
1370}
1371
0e35537d
JW
1372/* Parse opcode name, could be an mnemonics or number. */
1373static size_t
1374my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1375 char *str, const struct percent_op_match *percent_op)
1376{
1377 const struct opcode_name_t *o = opcode_name_lookup (&str);
1378
1379 if (o != NULL)
1380 {
1381 ep->X_op = O_constant;
1382 ep->X_add_number = o->val;
1383 return 0;
1384 }
1385
1386 return my_getSmallExpression (ep, reloc, str, percent_op);
1387}
1388
f0531ed6
JW
1389/* Detect and handle implicitly zero load-store offsets. For example,
1390 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return TRUE iff such
1391 an implicit offset was detected. */
1392
1393static bfd_boolean
89424b1d 1394riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
f0531ed6
JW
1395{
1396 /* Check whether there is only a single bracketed expression left.
1397 If so, it must be the base register and the constant must be zero. */
1398 if (*s == '(' && strchr (s + 1, '(') == 0)
1399 {
89424b1d
MR
1400 ep->X_op = O_constant;
1401 ep->X_add_number = 0;
f0531ed6
JW
1402 return TRUE;
1403 }
1404
1405 return FALSE;
1406}
1407
e23eba97
NC
1408/* This routine assembles an instruction into its binary format. As a
1409 side effect, it sets the global variable imm_reloc to the type of
1410 relocation to do if one of the operands is an address expression. */
1411
1412static const char *
1413riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
0e35537d 1414 bfd_reloc_code_real_type *imm_reloc, struct hash_control *hash)
e23eba97
NC
1415{
1416 char *s;
1417 const char *args;
1418 char c = 0;
1419 struct riscv_opcode *insn;
1420 char *argsStart;
1421 unsigned int regno;
1422 char save_c = 0;
1423 int argnum;
1424 const struct percent_op_match *p;
1425 const char *error = "unrecognized opcode";
1426
1427 /* Parse the name of the instruction. Terminate the string if whitespace
1428 is found so that hash_find only sees the name part of the string. */
1429 for (s = str; *s != '\0'; ++s)
1430 if (ISSPACE (*s))
1431 {
1432 save_c = *s;
1433 *s++ = '\0';
1434 break;
1435 }
1436
0e35537d 1437 insn = (struct riscv_opcode *) hash_find (hash, str);
e23eba97
NC
1438
1439 argsStart = s;
1440 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
1441 {
1442 if (!riscv_subset_supports (insn->subset))
1443 continue;
1444
1445 create_insn (ip, insn);
1446 argnum = 1;
1447
1448 imm_expr->X_op = O_absent;
1449 *imm_reloc = BFD_RELOC_UNUSED;
1450 p = percent_op_itype;
1451
1452 for (args = insn->args;; ++args)
1453 {
1454 s += strspn (s, " \t");
1455 switch (*args)
1456 {
1457 case '\0': /* End of args. */
1458 if (insn->pinfo != INSN_MACRO)
1459 {
1460 if (!insn->match_func (insn, ip->insn_opcode))
1461 break;
0e35537d
JW
1462
1463 /* For .insn, insn->match and insn->mask are 0. */
1464 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
1465 ? ip->insn_opcode
1466 : insn->match) == 2
1467 && !riscv_opts.rvc)
e23eba97
NC
1468 break;
1469 }
1470 if (*s != '\0')
1471 break;
1472 /* Successful assembly. */
1473 error = NULL;
1474 goto out;
1475
1476 case 'C': /* RVC */
1477 switch (*++args)
1478 {
1479 case 's': /* RS1 x8-x15 */
1480 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1481 || !(regno >= 8 && regno <= 15))
1482 break;
1483 INSERT_OPERAND (CRS1S, *ip, regno % 8);
1484 continue;
1485 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
1486 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1487 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
1488 break;
1489 continue;
1490 case 't': /* RS2 x8-x15 */
1491 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1492 || !(regno >= 8 && regno <= 15))
1493 break;
1494 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1495 continue;
1496 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
1497 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1498 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
1499 break;
1500 continue;
1501 case 'U': /* RS1, constrained to equal RD. */
1502 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1503 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
1504 break;
1505 continue;
1506 case 'V': /* RS2 */
1507 if (!reg_lookup (&s, RCLASS_GPR, &regno))
1508 break;
1509 INSERT_OPERAND (CRS2, *ip, regno);
1510 continue;
1511 case 'c': /* RS1, constrained to equal sp. */
1512 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1513 || regno != X_SP)
1514 break;
1515 continue;
1516 case '>':
1517 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1518 || imm_expr->X_op != O_constant
1519 || imm_expr->X_add_number <= 0
1520 || imm_expr->X_add_number >= 64)
1521 break;
1522 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1523rvc_imm_done:
1524 s = expr_end;
1525 imm_expr->X_op = O_absent;
1526 continue;
1527 case '<':
1528 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1529 || imm_expr->X_op != O_constant
1530 || !VALID_RVC_IMM (imm_expr->X_add_number)
1531 || imm_expr->X_add_number <= 0
1532 || imm_expr->X_add_number >= 32)
1533 break;
1534 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1535 goto rvc_imm_done;
0e35537d
JW
1536 case '8':
1537 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1538 || imm_expr->X_op != O_constant
1539 || !VALID_RVC_UIMM8 (imm_expr->X_add_number)
1540 || imm_expr->X_add_number < 0
1541 || imm_expr->X_add_number >= 256)
1542 break;
1543 ip->insn_opcode |= ENCODE_RVC_UIMM8 (imm_expr->X_add_number);
1544 goto rvc_imm_done;
e23eba97
NC
1545 case 'i':
1546 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1547 || imm_expr->X_op != O_constant
1548 || imm_expr->X_add_number == 0
1549 || !VALID_RVC_SIMM3 (imm_expr->X_add_number))
1550 break;
1551 ip->insn_opcode |= ENCODE_RVC_SIMM3 (imm_expr->X_add_number);
1552 goto rvc_imm_done;
1553 case 'j':
1554 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1555 || imm_expr->X_op != O_constant
1556 || imm_expr->X_add_number == 0
1557 || !VALID_RVC_IMM (imm_expr->X_add_number))
1558 break;
1559 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1560 goto rvc_imm_done;
1561 case 'k':
f0531ed6
JW
1562 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1563 continue;
e23eba97
NC
1564 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1565 || imm_expr->X_op != O_constant
1566 || !VALID_RVC_LW_IMM (imm_expr->X_add_number))
1567 break;
1568 ip->insn_opcode |= ENCODE_RVC_LW_IMM (imm_expr->X_add_number);
1569 goto rvc_imm_done;
1570 case 'l':
f0531ed6
JW
1571 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1572 continue;
e23eba97
NC
1573 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1574 || imm_expr->X_op != O_constant
1575 || !VALID_RVC_LD_IMM (imm_expr->X_add_number))
1576 break;
1577 ip->insn_opcode |= ENCODE_RVC_LD_IMM (imm_expr->X_add_number);
1578 goto rvc_imm_done;
1579 case 'm':
f0531ed6
JW
1580 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1581 continue;
e23eba97
NC
1582 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1583 || imm_expr->X_op != O_constant
1584 || !VALID_RVC_LWSP_IMM (imm_expr->X_add_number))
1585 break;
1586 ip->insn_opcode |=
1587 ENCODE_RVC_LWSP_IMM (imm_expr->X_add_number);
1588 goto rvc_imm_done;
1589 case 'n':
f0531ed6
JW
1590 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1591 continue;
e23eba97
NC
1592 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1593 || imm_expr->X_op != O_constant
1594 || !VALID_RVC_LDSP_IMM (imm_expr->X_add_number))
1595 break;
1596 ip->insn_opcode |=
1597 ENCODE_RVC_LDSP_IMM (imm_expr->X_add_number);
1598 goto rvc_imm_done;
b416fe87
KC
1599 case 'o':
1600 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1601 || imm_expr->X_op != O_constant
21a186f2
JW
1602 /* C.addiw, c.li, and c.andi allow zero immediate.
1603 C.addi allows zero immediate as hint. Otherwise this
1604 is same as 'j'. */
b416fe87
KC
1605 || !VALID_RVC_IMM (imm_expr->X_add_number))
1606 break;
1607 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1608 goto rvc_imm_done;
e23eba97
NC
1609 case 'K':
1610 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1611 || imm_expr->X_op != O_constant
1612 || !VALID_RVC_ADDI4SPN_IMM (imm_expr->X_add_number)
1613 || imm_expr->X_add_number == 0)
1614 break;
1615 ip->insn_opcode |=
1616 ENCODE_RVC_ADDI4SPN_IMM (imm_expr->X_add_number);
1617 goto rvc_imm_done;
1618 case 'L':
1619 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1620 || imm_expr->X_op != O_constant
1621 || !VALID_RVC_ADDI16SP_IMM (imm_expr->X_add_number)
1622 || imm_expr->X_add_number == 0)
1623 break;
1624 ip->insn_opcode |=
1625 ENCODE_RVC_ADDI16SP_IMM (imm_expr->X_add_number);
1626 goto rvc_imm_done;
1627 case 'M':
f0531ed6
JW
1628 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1629 continue;
e23eba97
NC
1630 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1631 || imm_expr->X_op != O_constant
1632 || !VALID_RVC_SWSP_IMM (imm_expr->X_add_number))
1633 break;
1634 ip->insn_opcode |=
1635 ENCODE_RVC_SWSP_IMM (imm_expr->X_add_number);
1636 goto rvc_imm_done;
1637 case 'N':
f0531ed6
JW
1638 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1639 continue;
e23eba97
NC
1640 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1641 || imm_expr->X_op != O_constant
1642 || !VALID_RVC_SDSP_IMM (imm_expr->X_add_number))
1643 break;
1644 ip->insn_opcode |=
1645 ENCODE_RVC_SDSP_IMM (imm_expr->X_add_number);
1646 goto rvc_imm_done;
1647 case 'u':
1648 p = percent_op_utype;
1649 if (my_getSmallExpression (imm_expr, imm_reloc, s, p))
1650 break;
1651rvc_lui:
1652 if (imm_expr->X_op != O_constant
1653 || imm_expr->X_add_number <= 0
1654 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
1655 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
1656 && (imm_expr->X_add_number <
1657 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
1658 break;
1659 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1660 goto rvc_imm_done;
1661 case 'v':
1662 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1663 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
1664 || ((int32_t)imm_expr->X_add_number
1665 != imm_expr->X_add_number))
1666 break;
1667 imm_expr->X_add_number =
1668 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
1669 goto rvc_lui;
1670 case 'p':
1671 goto branch;
1672 case 'a':
1673 goto jump;
0e35537d
JW
1674 case 'S': /* Floating-point RS1 x8-x15. */
1675 if (!reg_lookup (&s, RCLASS_FPR, &regno)
1676 || !(regno >= 8 && regno <= 15))
1677 break;
1678 INSERT_OPERAND (CRS1S, *ip, regno % 8);
1679 continue;
e23eba97
NC
1680 case 'D': /* Floating-point RS2 x8-x15. */
1681 if (!reg_lookup (&s, RCLASS_FPR, &regno)
1682 || !(regno >= 8 && regno <= 15))
1683 break;
1684 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1685 continue;
1686 case 'T': /* Floating-point RS2. */
1687 if (!reg_lookup (&s, RCLASS_FPR, &regno))
1688 break;
1689 INSERT_OPERAND (CRS2, *ip, regno);
1690 continue;
0e35537d
JW
1691 case 'F':
1692 switch (*++args)
1693 {
1694 case '4':
1695 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1696 || imm_expr->X_op != O_constant
1697 || imm_expr->X_add_number < 0
1698 || imm_expr->X_add_number >= 16)
1699 {
1700 as_bad (_("bad value for funct4 field, "
1701 "value must be 0...15"));
1702 break;
1703 }
1704
1705 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
1706 imm_expr->X_op = O_absent;
1707 s = expr_end;
1708 continue;
1709 case '3':
1710 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1711 || imm_expr->X_op != O_constant
1712 || imm_expr->X_add_number < 0
1713 || imm_expr->X_add_number >= 8)
1714 {
1715 as_bad (_("bad value for funct3 field, "
1716 "value must be 0...7"));
1717 break;
1718 }
1719 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
1720 imm_expr->X_op = O_absent;
1721 s = expr_end;
1722 continue;
1723 default:
1724 as_bad (_("bad compressed FUNCT field"
1725 " specifier 'CF%c'\n"),
1726 *args);
1727 }
1728 break;
1729
e23eba97
NC
1730 default:
1731 as_bad (_("bad RVC field specifier 'C%c'\n"), *args);
1732 }
1733 break;
1734
1735 case ',':
1736 ++argnum;
1737 if (*s++ == *args)
1738 continue;
1739 s--;
1740 break;
1741
1742 case '(':
1743 case ')':
1744 case '[':
1745 case ']':
1746 if (*s++ == *args)
1747 continue;
1748 break;
1749
1750 case '<': /* Shift amount, 0 - 31. */
1751 my_getExpression (imm_expr, s);
ca2fd32c 1752 check_absolute_expr (ip, imm_expr, FALSE);
e23eba97 1753 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1754 as_bad (_("Improper shift amount (%lu)"),
1755 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1756 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
1757 imm_expr->X_op = O_absent;
1758 s = expr_end;
1759 continue;
1760
1761 case '>': /* Shift amount, 0 - (XLEN-1). */
1762 my_getExpression (imm_expr, s);
ca2fd32c 1763 check_absolute_expr (ip, imm_expr, FALSE);
e23eba97 1764 if ((unsigned long) imm_expr->X_add_number >= xlen)
94f78a77
AW
1765 as_bad (_("Improper shift amount (%lu)"),
1766 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1767 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
1768 imm_expr->X_op = O_absent;
1769 s = expr_end;
1770 continue;
1771
1772 case 'Z': /* CSRRxI immediate. */
1773 my_getExpression (imm_expr, s);
ca2fd32c 1774 check_absolute_expr (ip, imm_expr, FALSE);
e23eba97 1775 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1776 as_bad (_("Improper CSRxI immediate (%lu)"),
1777 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1778 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
1779 imm_expr->X_op = O_absent;
1780 s = expr_end;
1781 continue;
1782
1783 case 'E': /* Control register. */
1784 if (reg_lookup (&s, RCLASS_CSR, &regno))
1785 INSERT_OPERAND (CSR, *ip, regno);
1786 else
1787 {
1788 my_getExpression (imm_expr, s);
ca2fd32c 1789 check_absolute_expr (ip, imm_expr, TRUE);
e23eba97 1790 if ((unsigned long) imm_expr->X_add_number > 0xfff)
94f78a77
AW
1791 as_bad (_("Improper CSR address (%lu)"),
1792 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1793 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
1794 imm_expr->X_op = O_absent;
1795 s = expr_end;
1796 }
1797 continue;
1798
1799 case 'm': /* Rounding mode. */
1800 if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), &regno))
1801 {
1802 INSERT_OPERAND (RM, *ip, regno);
1803 continue;
1804 }
1805 break;
1806
1807 case 'P':
1808 case 'Q': /* Fence predecessor/successor. */
1809 if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ),
1810 &regno))
1811 {
1812 if (*args == 'P')
1813 INSERT_OPERAND (PRED, *ip, regno);
1814 else
1815 INSERT_OPERAND (SUCC, *ip, regno);
1816 continue;
1817 }
1818 break;
1819
1820 case 'd': /* Destination register. */
1821 case 's': /* Source register. */
1822 case 't': /* Target register. */
0e35537d 1823 case 'r': /* rs3. */
e23eba97
NC
1824 if (reg_lookup (&s, RCLASS_GPR, &regno))
1825 {
1826 c = *args;
1827 if (*s == ' ')
1828 ++s;
1829
1830 /* Now that we have assembled one operand, we use the args
1831 string to figure out where it goes in the instruction. */
1832 switch (c)
1833 {
1834 case 's':
1835 INSERT_OPERAND (RS1, *ip, regno);
1836 break;
1837 case 'd':
1838 INSERT_OPERAND (RD, *ip, regno);
1839 break;
1840 case 't':
1841 INSERT_OPERAND (RS2, *ip, regno);
1842 break;
0e35537d
JW
1843 case 'r':
1844 INSERT_OPERAND (RS3, *ip, regno);
1845 break;
e23eba97
NC
1846 }
1847 continue;
1848 }
1849 break;
1850
1851 case 'D': /* Floating point rd. */
1852 case 'S': /* Floating point rs1. */
1853 case 'T': /* Floating point rs2. */
1854 case 'U': /* Floating point rs1 and rs2. */
1855 case 'R': /* Floating point rs3. */
1856 if (reg_lookup (&s, RCLASS_FPR, &regno))
1857 {
1858 c = *args;
1859 if (*s == ' ')
1860 ++s;
1861 switch (c)
1862 {
1863 case 'D':
1864 INSERT_OPERAND (RD, *ip, regno);
1865 break;
1866 case 'S':
1867 INSERT_OPERAND (RS1, *ip, regno);
1868 break;
1869 case 'U':
1870 INSERT_OPERAND (RS1, *ip, regno);
1871 /* fallthru */
1872 case 'T':
1873 INSERT_OPERAND (RS2, *ip, regno);
1874 break;
1875 case 'R':
1876 INSERT_OPERAND (RS3, *ip, regno);
1877 break;
1878 }
1879 continue;
1880 }
1881
1882 break;
1883
1884 case 'I':
1885 my_getExpression (imm_expr, s);
1886 if (imm_expr->X_op != O_big
1887 && imm_expr->X_op != O_constant)
1888 break;
1889 normalize_constant_expr (imm_expr);
1890 s = expr_end;
1891 continue;
1892
1893 case 'A':
1894 my_getExpression (imm_expr, s);
1895 normalize_constant_expr (imm_expr);
1896 /* The 'A' format specifier must be a symbol. */
1897 if (imm_expr->X_op != O_symbol)
1898 break;
1899 *imm_reloc = BFD_RELOC_32;
1900 s = expr_end;
1901 continue;
1902
1903 case 'j': /* Sign-extended immediate. */
1904 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1905 p = percent_op_itype;
1906 goto alu_op;
1907 case 'q': /* Store displacement. */
1908 p = percent_op_stype;
1909 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
1910 goto load_store;
1911 case 'o': /* Load displacement. */
1912 p = percent_op_itype;
1913 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1914 goto load_store;
1915 case '0': /* AMO "displacement," which must be zero. */
1916 p = percent_op_rtype;
1917 *imm_reloc = BFD_RELOC_UNUSED;
1918load_store:
f0531ed6 1919 if (riscv_handle_implicit_zero_offset (imm_expr, s))
e23eba97
NC
1920 continue;
1921alu_op:
1922 /* If this value won't fit into a 16 bit offset, then go
1923 find a macro that will generate the 32 bit offset
1924 code pattern. */
1925 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
1926 {
1927 normalize_constant_expr (imm_expr);
1928 if (imm_expr->X_op != O_constant
1929 || (*args == '0' && imm_expr->X_add_number != 0)
1930 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
1931 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
1932 break;
1933 }
1934
1935 s = expr_end;
1936 continue;
1937
1938 case 'p': /* PC-relative offset. */
1939branch:
1940 *imm_reloc = BFD_RELOC_12_PCREL;
1941 my_getExpression (imm_expr, s);
1942 s = expr_end;
1943 continue;
1944
1945 case 'u': /* Upper 20 bits. */
1946 p = percent_op_utype;
1947 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p)
1948 && imm_expr->X_op == O_constant)
1949 {
1950 if (imm_expr->X_add_number < 0
1951 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
1952 as_bad (_("lui expression not in range 0..1048575"));
1953
1954 *imm_reloc = BFD_RELOC_RISCV_HI20;
1955 imm_expr->X_add_number <<= RISCV_IMM_BITS;
1956 }
1957 s = expr_end;
1958 continue;
1959
1960 case 'a': /* 20-bit PC-relative offset. */
1961jump:
1962 my_getExpression (imm_expr, s);
1963 s = expr_end;
1964 *imm_reloc = BFD_RELOC_RISCV_JMP;
1965 continue;
1966
1967 case 'c':
1968 my_getExpression (imm_expr, s);
1969 s = expr_end;
1970 if (strcmp (s, "@plt") == 0)
1971 {
1972 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
1973 s += 4;
1974 }
1975 else
1976 *imm_reloc = BFD_RELOC_RISCV_CALL;
1977 continue;
0e35537d
JW
1978 case 'O':
1979 switch (*++args)
1980 {
1981 case '4':
1982 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
1983 || imm_expr->X_op != O_constant
1984 || imm_expr->X_add_number < 0
1985 || imm_expr->X_add_number >= 128
1986 || (imm_expr->X_add_number & 0x3) != 3)
1987 {
1988 as_bad (_("bad value for opcode field, "
1989 "value must be 0...127 and "
1990 "lower 2 bits must be 0x3"));
1991 break;
1992 }
1993
1994 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
1995 imm_expr->X_op = O_absent;
1996 s = expr_end;
1997 continue;
1998 case '2':
1999 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
2000 || imm_expr->X_op != O_constant
2001 || imm_expr->X_add_number < 0
2002 || imm_expr->X_add_number >= 3)
2003 {
2004 as_bad (_("bad value for opcode field, "
2005 "value must be 0...2"));
2006 break;
2007 }
2008
2009 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
2010 imm_expr->X_op = O_absent;
2011 s = expr_end;
2012 continue;
2013 default:
2014 as_bad (_("bad Opcode field specifier 'O%c'\n"), *args);
2015 }
2016 break;
2017
2018 case 'F':
2019 switch (*++args)
2020 {
2021 case '7':
2022 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2023 || imm_expr->X_op != O_constant
2024 || imm_expr->X_add_number < 0
2025 || imm_expr->X_add_number >= 128)
2026 {
2027 as_bad (_("bad value for funct7 field, "
2028 "value must be 0...127"));
2029 break;
2030 }
2031
2032 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
2033 imm_expr->X_op = O_absent;
2034 s = expr_end;
2035 continue;
2036 case '3':
2037 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2038 || imm_expr->X_op != O_constant
2039 || imm_expr->X_add_number < 0
2040 || imm_expr->X_add_number >= 8)
2041 {
2042 as_bad (_("bad value for funct3 field, "
2043 "value must be 0...7"));
2044 break;
2045 }
2046
2047 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
2048 imm_expr->X_op = O_absent;
2049 s = expr_end;
2050 continue;
2051 case '2':
2052 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2053 || imm_expr->X_op != O_constant
2054 || imm_expr->X_add_number < 0
2055 || imm_expr->X_add_number >= 4)
2056 {
2057 as_bad (_("bad value for funct2 field, "
2058 "value must be 0...3"));
2059 break;
2060 }
2061
2062 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
2063 imm_expr->X_op = O_absent;
2064 s = expr_end;
2065 continue;
2066
2067 default:
2068 as_bad (_("bad FUNCT field specifier 'F%c'\n"), *args);
2069 }
2070 break;
e23eba97 2071
e925c834
JW
2072 case 'z':
2073 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2074 || imm_expr->X_op != O_constant
2075 || imm_expr->X_add_number != 0)
2076 break;
2077 s = expr_end;
2078 imm_expr->X_op = O_absent;
2079 continue;
2080
e23eba97
NC
2081 default:
2082 as_fatal (_("internal error: bad argument type %c"), *args);
2083 }
2084 break;
2085 }
2086 s = argsStart;
2087 error = _("illegal operands");
2088 }
2089
2090out:
2091 /* Restore the character we might have clobbered above. */
2092 if (save_c)
2093 *(argsStart - 1) = save_c;
2094
2095 return error;
2096}
2097
2098void
2099md_assemble (char *str)
2100{
2101 struct riscv_cl_insn insn;
2102 expressionS imm_expr;
2103 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2104
0e35537d 2105 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc, op_hash);
e23eba97
NC
2106
2107 if (error)
2108 {
2109 as_bad ("%s `%s'", error, str);
2110 return;
2111 }
2112
2113 if (insn.insn_mo->pinfo == INSN_MACRO)
2114 macro (&insn, &imm_expr, &imm_reloc);
2115 else
2116 append_insn (&insn, &imm_expr, imm_reloc);
2117}
2118
2119const char *
2120md_atof (int type, char *litP, int *sizeP)
2121{
2122 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
2123}
2124
2125void
2126md_number_to_chars (char *buf, valueT val, int n)
2127{
2128 number_to_chars_littleendian (buf, val, n);
2129}
2130
2131const char *md_shortopts = "O::g::G:";
2132
2133enum options
2134{
2922d21d 2135 OPTION_MARCH = OPTION_MD_BASE,
e23eba97
NC
2136 OPTION_PIC,
2137 OPTION_NO_PIC,
2922d21d 2138 OPTION_MABI,
e23eba97
NC
2139 OPTION_END_OF_ENUM
2140};
2141
2142struct option md_longopts[] =
2143{
e23eba97
NC
2144 {"march", required_argument, NULL, OPTION_MARCH},
2145 {"fPIC", no_argument, NULL, OPTION_PIC},
2146 {"fpic", no_argument, NULL, OPTION_PIC},
2147 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2922d21d 2148 {"mabi", required_argument, NULL, OPTION_MABI},
e23eba97
NC
2149
2150 {NULL, no_argument, NULL, 0}
2151};
2152size_t md_longopts_size = sizeof (md_longopts);
2153
2922d21d
AW
2154enum float_abi {
2155 FLOAT_ABI_DEFAULT = -1,
2156 FLOAT_ABI_SOFT,
2157 FLOAT_ABI_SINGLE,
2158 FLOAT_ABI_DOUBLE,
2159 FLOAT_ABI_QUAD
e23eba97 2160};
2922d21d
AW
2161static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
2162
2163static void
2164riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi)
2165{
2166 abi_xlen = new_xlen;
2167 float_abi = new_float_abi;
2168}
e23eba97
NC
2169
2170int
2171md_parse_option (int c, const char *arg)
2172{
2173 switch (c)
2174 {
e23eba97
NC
2175 case OPTION_MARCH:
2176 riscv_set_arch (arg);
2177 break;
2178
2179 case OPTION_NO_PIC:
2180 riscv_opts.pic = FALSE;
2181 break;
2182
2183 case OPTION_PIC:
2184 riscv_opts.pic = TRUE;
2185 break;
2186
2922d21d
AW
2187 case OPTION_MABI:
2188 if (strcmp (arg, "ilp32") == 0)
2189 riscv_set_abi (32, FLOAT_ABI_SOFT);
2190 else if (strcmp (arg, "ilp32f") == 0)
2191 riscv_set_abi (32, FLOAT_ABI_SINGLE);
2192 else if (strcmp (arg, "ilp32d") == 0)
2193 riscv_set_abi (32, FLOAT_ABI_DOUBLE);
2194 else if (strcmp (arg, "ilp32q") == 0)
2195 riscv_set_abi (32, FLOAT_ABI_QUAD);
2196 else if (strcmp (arg, "lp64") == 0)
2197 riscv_set_abi (64, FLOAT_ABI_SOFT);
2198 else if (strcmp (arg, "lp64f") == 0)
2199 riscv_set_abi (64, FLOAT_ABI_SINGLE);
2200 else if (strcmp (arg, "lp64d") == 0)
2201 riscv_set_abi (64, FLOAT_ABI_DOUBLE);
2202 else if (strcmp (arg, "lp64q") == 0)
2203 riscv_set_abi (64, FLOAT_ABI_QUAD);
2204 else
2205 return 0;
2206 break;
2207
e23eba97
NC
2208 default:
2209 return 0;
2210 }
2211
2212 return 1;
2213}
2214
2215void
2216riscv_after_parse_args (void)
2217{
e23eba97
NC
2218 if (xlen == 0)
2219 {
2220 if (strcmp (default_arch, "riscv32") == 0)
2221 xlen = 32;
2222 else if (strcmp (default_arch, "riscv64") == 0)
2223 xlen = 64;
2224 else
2225 as_bad ("unknown default architecture `%s'", default_arch);
2226 }
2922d21d
AW
2227
2228 if (riscv_subsets == NULL)
2229 riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g");
2230
2231 /* Add the RVC extension, regardless of -march, to support .option rvc. */
fecb9c46 2232 riscv_set_rvc (FALSE);
2922d21d
AW
2233 if (riscv_subset_supports ("c"))
2234 riscv_set_rvc (TRUE);
2235 else
2236 riscv_add_subset ("c");
2237
2238 /* Infer ABI from ISA if not specified on command line. */
2239 if (abi_xlen == 0)
2240 abi_xlen = xlen;
2241 else if (abi_xlen > xlen)
2242 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
2243 else if (abi_xlen < xlen)
2244 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
2245
2246 if (float_abi == FLOAT_ABI_DEFAULT)
2247 {
2248 struct riscv_subset *subset;
2249
2250 /* Assume soft-float unless D extension is present. */
2251 float_abi = FLOAT_ABI_SOFT;
2252
2253 for (subset = riscv_subsets; subset != NULL; subset = subset->next)
cc917fd9
KC
2254 {
2255 if (strcasecmp (subset->name, "D") == 0)
2256 float_abi = FLOAT_ABI_DOUBLE;
2257 if (strcasecmp (subset->name, "Q") == 0)
2258 float_abi = FLOAT_ABI_QUAD;
2259 }
2922d21d
AW
2260 }
2261
2262 /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */
2263 elf_flags |= float_abi * (EF_RISCV_FLOAT_ABI & ~(EF_RISCV_FLOAT_ABI << 1));
e23eba97
NC
2264}
2265
2266long
2267md_pcrel_from (fixS *fixP)
2268{
2269 return fixP->fx_where + fixP->fx_frag->fr_address;
2270}
2271
2272/* Apply a fixup to the object file. */
2273
2274void
2275md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2276{
45f76423 2277 unsigned int subtype;
e23eba97 2278 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
45f76423 2279 bfd_boolean relaxable = FALSE;
c1b465c9 2280 offsetT loc;
a6cbf936 2281 segT sub_segment;
e23eba97
NC
2282
2283 /* Remember value for tc_gen_reloc. */
2284 fixP->fx_addnumber = *valP;
2285
2286 switch (fixP->fx_r_type)
2287 {
e23eba97
NC
2288 case BFD_RELOC_RISCV_HI20:
2289 case BFD_RELOC_RISCV_LO12_I:
2290 case BFD_RELOC_RISCV_LO12_S:
45f76423
AW
2291 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2292 | bfd_getl32 (buf), buf);
a5ec5e3f
AW
2293 if (fixP->fx_addsy == NULL)
2294 fixP->fx_done = TRUE;
45f76423
AW
2295 relaxable = TRUE;
2296 break;
2297
2298 case BFD_RELOC_RISCV_GOT_HI20:
e23eba97
NC
2299 case BFD_RELOC_RISCV_ADD8:
2300 case BFD_RELOC_RISCV_ADD16:
2301 case BFD_RELOC_RISCV_ADD32:
2302 case BFD_RELOC_RISCV_ADD64:
45f76423 2303 case BFD_RELOC_RISCV_SUB6:
e23eba97
NC
2304 case BFD_RELOC_RISCV_SUB8:
2305 case BFD_RELOC_RISCV_SUB16:
2306 case BFD_RELOC_RISCV_SUB32:
2307 case BFD_RELOC_RISCV_SUB64:
45f76423
AW
2308 case BFD_RELOC_RISCV_RELAX:
2309 break;
2310
2311 case BFD_RELOC_RISCV_TPREL_HI20:
2312 case BFD_RELOC_RISCV_TPREL_LO12_I:
2313 case BFD_RELOC_RISCV_TPREL_LO12_S:
2314 case BFD_RELOC_RISCV_TPREL_ADD:
2315 relaxable = TRUE;
2316 /* Fall through. */
2317
2318 case BFD_RELOC_RISCV_TLS_GOT_HI20:
2319 case BFD_RELOC_RISCV_TLS_GD_HI20:
2320 case BFD_RELOC_RISCV_TLS_DTPREL32:
2321 case BFD_RELOC_RISCV_TLS_DTPREL64:
e294484e
AW
2322 if (fixP->fx_addsy != NULL)
2323 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2324 else
2325 as_bad_where (fixP->fx_file, fixP->fx_line,
2326 _("TLS relocation against a constant"));
e23eba97
NC
2327 break;
2328
e23eba97 2329 case BFD_RELOC_32:
a6cbf936
KLC
2330 /* Use pc-relative relocation for FDE initial location.
2331 The symbol address in .eh_frame may be adjusted in
2332 _bfd_elf_discard_section_eh_frame, and the content of
2333 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
2334 Therefore, we cannot insert a relocation whose addend symbol is
2335 in .eh_frame. Othrewise, the value may be adjusted twice.*/
2336 if (fixP->fx_addsy && fixP->fx_subsy
2337 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
2338 && strcmp (sub_segment->name, ".eh_frame") == 0
2339 && S_GET_VALUE (fixP->fx_subsy)
2340 == fixP->fx_frag->fr_address + fixP->fx_where)
2341 {
2342 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
2343 fixP->fx_subsy = NULL;
2344 break;
2345 }
2346 /* Fall through. */
2347 case BFD_RELOC_64:
e23eba97
NC
2348 case BFD_RELOC_16:
2349 case BFD_RELOC_8:
45f76423 2350 case BFD_RELOC_RISCV_CFA:
e23eba97
NC
2351 if (fixP->fx_addsy && fixP->fx_subsy)
2352 {
2353 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2354 fixP->fx_next->fx_addsy = fixP->fx_subsy;
2355 fixP->fx_next->fx_subsy = NULL;
2356 fixP->fx_next->fx_offset = 0;
2357 fixP->fx_subsy = NULL;
2358
2359 switch (fixP->fx_r_type)
2360 {
2361 case BFD_RELOC_64:
2362 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
2363 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
2364 break;
2365
2366 case BFD_RELOC_32:
2367 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
2368 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2369 break;
2370
2371 case BFD_RELOC_16:
2372 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
2373 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2374 break;
2375
2376 case BFD_RELOC_8:
2377 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
2378 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
073808ed 2379 break;
e23eba97 2380
45f76423
AW
2381 case BFD_RELOC_RISCV_CFA:
2382 /* Load the byte to get the subtype. */
c1b465c9
KLC
2383 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
2384 loc = fixP->fx_frag->fr_fix - (subtype & 7);
45f76423
AW
2385 switch (subtype)
2386 {
2387 case DW_CFA_advance_loc1:
c1b465c9
KLC
2388 fixP->fx_where = loc + 1;
2389 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2390 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
2391 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
2392 break;
2393
2394 case DW_CFA_advance_loc2:
2395 fixP->fx_size = 2;
45f76423 2396 fixP->fx_next->fx_size = 2;
c1b465c9
KLC
2397 fixP->fx_where = loc + 1;
2398 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2399 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
2400 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2401 break;
2402
2403 case DW_CFA_advance_loc4:
2404 fixP->fx_size = 4;
45f76423 2405 fixP->fx_next->fx_size = 4;
c1b465c9
KLC
2406 fixP->fx_where = loc;
2407 fixP->fx_next->fx_where = loc;
45f76423
AW
2408 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
2409 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2410 break;
2411
2412 default:
2413 if (subtype < 0x80 && (subtype & 0x40))
2414 {
2415 /* DW_CFA_advance_loc */
2aece2ba
KLC
2416 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
2417 fixP->fx_next->fx_frag = fixP->fx_frag;
45f76423
AW
2418 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
2419 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
2420 }
2421 else
2422 as_fatal (_("internal error: bad CFA value #%d"), subtype);
2423 break;
2424 }
2425 break;
2426
e23eba97
NC
2427 default:
2428 /* This case is unreachable. */
2429 abort ();
2430 }
2431 }
2432 /* Fall through. */
2433
2434 case BFD_RELOC_RVA:
2435 /* If we are deleting this reloc entry, we must fill in the
2436 value now. This can happen if we have a .word which is not
2437 resolved when it appears but is later defined. */
2438 if (fixP->fx_addsy == NULL)
2439 {
2440 gas_assert (fixP->fx_size <= sizeof (valueT));
2441 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
2442 fixP->fx_done = 1;
2443 }
2444 break;
2445
2446 case BFD_RELOC_RISCV_JMP:
2447 if (fixP->fx_addsy)
2448 {
2449 /* Fill in a tentative value to improve objdump readability. */
2450 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2451 bfd_vma delta = target - md_pcrel_from (fixP);
2452 bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf);
2453 }
2454 break;
2455
2456 case BFD_RELOC_12_PCREL:
2457 if (fixP->fx_addsy)
2458 {
2459 /* Fill in a tentative value to improve objdump readability. */
2460 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2461 bfd_vma delta = target - md_pcrel_from (fixP);
2462 bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf);
2463 }
2464 break;
2465
2466 case BFD_RELOC_RISCV_RVC_BRANCH:
2467 if (fixP->fx_addsy)
2468 {
2469 /* Fill in a tentative value to improve objdump readability. */
2470 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2471 bfd_vma delta = target - md_pcrel_from (fixP);
2472 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf);
2473 }
2474 break;
2475
2476 case BFD_RELOC_RISCV_RVC_JUMP:
2477 if (fixP->fx_addsy)
2478 {
2479 /* Fill in a tentative value to improve objdump readability. */
2480 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2481 bfd_vma delta = target - md_pcrel_from (fixP);
2482 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf);
2483 }
2484 break;
2485
e23eba97
NC
2486 case BFD_RELOC_RISCV_CALL:
2487 case BFD_RELOC_RISCV_CALL_PLT:
45f76423
AW
2488 relaxable = TRUE;
2489 break;
2490
9d06997a 2491 case BFD_RELOC_RISCV_PCREL_HI20:
45f76423
AW
2492 case BFD_RELOC_RISCV_PCREL_LO12_S:
2493 case BFD_RELOC_RISCV_PCREL_LO12_I:
9d06997a
PD
2494 relaxable = riscv_opts.relax;
2495 break;
2496
e23eba97
NC
2497 case BFD_RELOC_RISCV_ALIGN:
2498 break;
2499
2500 default:
2501 /* We ignore generic BFD relocations we don't know about. */
2502 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
2503 as_fatal (_("internal error: bad relocation #%d"), fixP->fx_r_type);
2504 }
45f76423 2505
e294484e
AW
2506 if (fixP->fx_subsy != NULL)
2507 as_bad_where (fixP->fx_file, fixP->fx_line,
2508 _("unsupported symbol subtraction"));
2509
45f76423
AW
2510 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
2511 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
2512 {
2513 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2514 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
2515 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
2516 }
2517}
2518
2519/* Because the value of .cfi_remember_state may changed after relaxation,
2520 we insert a fix to relocate it again in link-time. */
2521
2522void
2523riscv_pre_output_hook (void)
2524{
2525 const frchainS *frch;
2526 const asection *s;
2527
2528 for (s = stdoutput->sections; s; s = s->next)
2529 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
2530 {
7cb7b948 2531 fragS *frag;
45f76423
AW
2532
2533 for (frag = frch->frch_root; frag; frag = frag->fr_next)
2534 {
2535 if (frag->fr_type == rs_cfa)
2536 {
45f76423
AW
2537 expressionS exp;
2538
2539 symbolS *add_symbol = frag->fr_symbol->sy_value.X_add_symbol;
2540 symbolS *op_symbol = frag->fr_symbol->sy_value.X_op_symbol;
2541
2542 exp.X_op = O_subtract;
2543 exp.X_add_symbol = add_symbol;
2544 exp.X_add_number = 0;
2545 exp.X_op_symbol = op_symbol;
2546
c1b465c9 2547 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
45f76423
AW
2548 BFD_RELOC_RISCV_CFA);
2549 }
2550 }
2551 }
e23eba97
NC
2552}
2553
45f76423 2554
e23eba97
NC
2555/* This structure is used to hold a stack of .option values. */
2556
2557struct riscv_option_stack
2558{
2559 struct riscv_option_stack *next;
2560 struct riscv_set_options options;
2561};
2562
2563static struct riscv_option_stack *riscv_opts_stack;
2564
2565/* Handle the .option pseudo-op. */
2566
2567static void
2568s_riscv_option (int x ATTRIBUTE_UNUSED)
2569{
2570 char *name = input_line_pointer, ch;
2571
2572 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2573 ++input_line_pointer;
2574 ch = *input_line_pointer;
2575 *input_line_pointer = '\0';
2576
2577 if (strcmp (name, "rvc") == 0)
2578 riscv_set_rvc (TRUE);
2579 else if (strcmp (name, "norvc") == 0)
2580 riscv_set_rvc (FALSE);
2581 else if (strcmp (name, "pic") == 0)
2582 riscv_opts.pic = TRUE;
2583 else if (strcmp (name, "nopic") == 0)
2584 riscv_opts.pic = FALSE;
45f76423
AW
2585 else if (strcmp (name, "relax") == 0)
2586 riscv_opts.relax = TRUE;
2587 else if (strcmp (name, "norelax") == 0)
2588 riscv_opts.relax = FALSE;
e23eba97
NC
2589 else if (strcmp (name, "push") == 0)
2590 {
2591 struct riscv_option_stack *s;
2592
2593 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
2594 s->next = riscv_opts_stack;
2595 s->options = riscv_opts;
2596 riscv_opts_stack = s;
2597 }
2598 else if (strcmp (name, "pop") == 0)
2599 {
2600 struct riscv_option_stack *s;
2601
2602 s = riscv_opts_stack;
2603 if (s == NULL)
2604 as_bad (_(".option pop with no .option push"));
2605 else
2606 {
2607 riscv_opts = s->options;
2608 riscv_opts_stack = s->next;
2609 free (s);
2610 }
2611 }
2612 else
2613 {
2614 as_warn (_("Unrecognized .option directive: %s\n"), name);
2615 }
2616 *input_line_pointer = ch;
2617 demand_empty_rest_of_line ();
2618}
2619
2620/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
2621 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
2622 use in DWARF debug information. */
2623
2624static void
2625s_dtprel (int bytes)
2626{
2627 expressionS ex;
2628 char *p;
2629
2630 expression (&ex);
2631
2632 if (ex.X_op != O_symbol)
2633 {
2634 as_bad (_("Unsupported use of %s"), (bytes == 8
2635 ? ".dtpreldword"
2636 : ".dtprelword"));
2637 ignore_rest_of_line ();
2638 }
2639
2640 p = frag_more (bytes);
2641 md_number_to_chars (p, 0, bytes);
2642 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
2643 (bytes == 8
2644 ? BFD_RELOC_RISCV_TLS_DTPREL64
2645 : BFD_RELOC_RISCV_TLS_DTPREL32));
2646
2647 demand_empty_rest_of_line ();
2648}
2649
2650/* Handle the .bss pseudo-op. */
2651
2652static void
2653s_bss (int ignore ATTRIBUTE_UNUSED)
2654{
2655 subseg_set (bss_section, 0);
2656 demand_empty_rest_of_line ();
2657}
2658
e23eba97 2659static void
d115ab8e 2660riscv_make_nops (char *buf, bfd_vma bytes)
e23eba97 2661{
d115ab8e 2662 bfd_vma i = 0;
e23eba97 2663
e5b737de
AW
2664 /* RISC-V instructions cannot begin or end on odd addresses, so this case
2665 means we are not within a valid instruction sequence. It is thus safe
2666 to use a zero byte, even though that is not a valid instruction. */
2667 if (bytes % 2 == 1)
2668 buf[i++] = 0;
2669
2670 /* Use at most one 2-byte NOP. */
2671 if ((bytes - i) % 4 == 2)
e23eba97 2672 {
e5b737de 2673 md_number_to_chars (buf + i, RVC_NOP, 2);
d115ab8e 2674 i += 2;
e23eba97
NC
2675 }
2676
e5b737de 2677 /* Fill the remainder with 4-byte NOPs. */
d115ab8e
AW
2678 for ( ; i < bytes; i += 4)
2679 md_number_to_chars (buf + i, RISCV_NOP, 4);
2680}
e23eba97 2681
d115ab8e
AW
2682/* Called from md_do_align. Used to create an alignment frag in a
2683 code section by emitting a worst-case NOP sequence that the linker
2684 will later relax to the correct number of NOPs. We can't compute
2685 the correct alignment now because of other linker relaxations. */
2686
2687bfd_boolean
2688riscv_frag_align_code (int n)
2689{
e5b737de 2690 bfd_vma bytes = (bfd_vma) 1 << n;
36877bfb
JW
2691 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
2692 bfd_vma worst_case_bytes = bytes - insn_alignment;
2693 char *nops;
ed0816bd 2694 expressionS ex;
d115ab8e 2695
36877bfb
JW
2696 /* If we are moving to a smaller alignment than the instruction size, then no
2697 alignment is required. */
2698 if (bytes <= insn_alignment)
2699 return TRUE;
2700
2701 nops = frag_more (worst_case_bytes);
2702
d115ab8e
AW
2703 /* When not relaxing, riscv_handle_align handles code alignment. */
2704 if (!riscv_opts.relax)
2705 return FALSE;
e23eba97 2706
ed0816bd
PD
2707 ex.X_op = O_constant;
2708 ex.X_add_number = worst_case_bytes;
d115ab8e 2709
ed0816bd 2710 riscv_make_nops (nops, worst_case_bytes);
e23eba97 2711
ed0816bd
PD
2712 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
2713 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
e23eba97 2714
d115ab8e
AW
2715 return TRUE;
2716}
e23eba97 2717
d115ab8e
AW
2718/* Implement HANDLE_ALIGN. */
2719
2720void
2721riscv_handle_align (fragS *fragP)
2722{
2723 switch (fragP->fr_type)
2724 {
2725 case rs_align_code:
2726 /* When relaxing, riscv_frag_align_code handles code alignment. */
2727 if (!riscv_opts.relax)
2728 {
2729 bfd_signed_vma count = fragP->fr_next->fr_address
2730 - fragP->fr_address - fragP->fr_fix;
2731
2732 if (count <= 0)
2733 break;
2734
2735 count &= MAX_MEM_FOR_RS_ALIGN_CODE;
2736 riscv_make_nops (fragP->fr_literal + fragP->fr_fix, count);
2737 fragP->fr_var = count;
2738 }
2739 break;
2740
2741 default:
2742 break;
2743 }
e23eba97
NC
2744}
2745
2746int
2747md_estimate_size_before_relax (fragS *fragp, asection *segtype)
2748{
2749 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE));
2750}
2751
2752/* Translate internal representation of relocation info to BFD target
2753 format. */
2754
2755arelent *
2756tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2757{
2758 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
2759
2760 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2761 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2762 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2763 reloc->addend = fixp->fx_addnumber;
2764
2765 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2766 if (reloc->howto == NULL)
2767 {
2768 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
2769 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
2770 {
2771 /* We don't have R_RISCV_8/16, but for this special case,
2772 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
2773 return reloc;
2774 }
2775
2776 as_bad_where (fixp->fx_file, fixp->fx_line,
2777 _("cannot represent %s relocation in object file"),
2778 bfd_get_reloc_code_name (fixp->fx_r_type));
2779 return NULL;
2780 }
2781
2782 return reloc;
2783}
2784
2785int
2786riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
2787{
2788 if (RELAX_BRANCH_P (fragp->fr_subtype))
2789 {
2790 offsetT old_var = fragp->fr_var;
2791 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
2792 return fragp->fr_var - old_var;
2793 }
2794
2795 return 0;
2796}
2797
2798/* Expand far branches to multi-instruction sequences. */
2799
2800static void
2801md_convert_frag_branch (fragS *fragp)
2802{
2803 bfd_byte *buf;
2804 expressionS exp;
2805 fixS *fixp;
2806 insn_t insn;
2807 int rs1, reloc;
2808
2809 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
2810
2811 exp.X_op = O_symbol;
2812 exp.X_add_symbol = fragp->fr_symbol;
2813 exp.X_add_number = fragp->fr_offset;
2814
2815 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
2816
2817 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
2818 {
2819 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2820 {
2821 case 8:
2822 case 4:
2823 /* Expand the RVC branch into a RISC-V one. */
2824 insn = bfd_getl16 (buf);
2825 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
2826 if ((insn & MASK_C_J) == MATCH_C_J)
2827 insn = MATCH_JAL;
2828 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
2829 insn = MATCH_JAL | (X_RA << OP_SH_RD);
2830 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
2831 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
2832 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
2833 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
2834 else
2835 abort ();
2836 bfd_putl32 (insn, buf);
2837 break;
2838
2839 case 6:
2840 /* Invert the branch condition. Branch over the jump. */
2841 insn = bfd_getl16 (buf);
2842 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
2843 insn |= ENCODE_RVC_B_IMM (6);
2844 bfd_putl16 (insn, buf);
2845 buf += 2;
2846 goto jump;
2847
2848 case 2:
2849 /* Just keep the RVC branch. */
2850 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2851 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
2852 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2853 2, &exp, FALSE, reloc);
2854 buf += 2;
2855 goto done;
2856
2857 default:
1d65abb5 2858 abort ();
e23eba97
NC
2859 }
2860 }
2861
2862 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2863 {
2864 case 8:
2865 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
2866
2867 /* Invert the branch condition. Branch over the jump. */
2868 insn = bfd_getl32 (buf);
2869 insn ^= MATCH_BEQ ^ MATCH_BNE;
2870 insn |= ENCODE_SBTYPE_IMM (8);
2871 md_number_to_chars ((char *) buf, insn, 4);
2872 buf += 4;
2873
2874jump:
2875 /* Jump to the target. */
2876 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2877 4, &exp, FALSE, BFD_RELOC_RISCV_JMP);
2878 md_number_to_chars ((char *) buf, MATCH_JAL, 4);
2879 buf += 4;
2880 break;
2881
2882 case 4:
2883 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2884 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
2885 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2886 4, &exp, FALSE, reloc);
2887 buf += 4;
2888 break;
2889
2890 default:
2891 abort ();
2892 }
2893
2894done:
2895 fixp->fx_file = fragp->fr_file;
2896 fixp->fx_line = fragp->fr_line;
2897
2898 gas_assert (buf == (bfd_byte *)fragp->fr_literal
2899 + fragp->fr_fix + fragp->fr_var);
2900
2901 fragp->fr_fix += fragp->fr_var;
2902}
2903
2904/* Relax a machine dependent frag. This returns the amount by which
2905 the current size of the frag should change. */
2906
2907void
2908md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
2909 fragS *fragp)
2910{
2911 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
2912 md_convert_frag_branch (fragp);
2913}
2914
2915void
2916md_show_usage (FILE *stream)
2917{
2918 fprintf (stream, _("\
2919RISC-V options:\n\
e23eba97
NC
2920 -fpic generate position-independent code\n\
2921 -fno-pic don't generate position-independent code (default)\n\
19683c04
PD
2922 -march=ISA set the RISC-V architecture\n\
2923 -mabi=ABI set the RISC-V ABI\n\
e23eba97
NC
2924"));
2925}
2926
2927/* Standard calling conventions leave the CFA at SP on entry. */
2928void
2929riscv_cfi_frame_initial_instructions (void)
2930{
2931 cfi_add_CFA_def_cfa_register (X_SP);
2932}
2933
2934int
2935tc_riscv_regname_to_dw2regnum (char *regname)
2936{
2937 int reg;
2938
2939 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
2940 return reg;
2941
2942 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
2943 return reg + 32;
2944
2945 as_bad (_("unknown register `%s'"), regname);
2946 return -1;
2947}
2948
2949void
2950riscv_elf_final_processing (void)
2951{
e23eba97 2952 elf_elfheader (stdoutput)->e_flags |= elf_flags;
e23eba97
NC
2953}
2954
2955/* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
2956 since these directives break relaxation when used with symbol deltas. */
2957
2958static void
2959s_riscv_leb128 (int sign)
2960{
2961 expressionS exp;
2962 char *save_in = input_line_pointer;
2963
2964 expression (&exp);
2965 if (exp.X_op != O_constant)
2966 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
2967 demand_empty_rest_of_line ();
2968
2969 input_line_pointer = save_in;
2970 return s_leb128 (sign);
2971}
2972
0e35537d
JW
2973/* Parse the .insn directive. */
2974
2975static void
2976s_riscv_insn (int x ATTRIBUTE_UNUSED)
2977{
2978 char *str = input_line_pointer;
2979 struct riscv_cl_insn insn;
2980 expressionS imm_expr;
2981 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2982 char save_c;
2983
2984 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2985 ++input_line_pointer;
2986
2987 save_c = *input_line_pointer;
2988 *input_line_pointer = '\0';
2989
2990 const char *error = riscv_ip (str, &insn, &imm_expr,
2991 &imm_reloc, insn_type_hash);
2992
2993 if (error)
2994 {
2995 as_bad ("%s `%s'", error, str);
2996 }
2997 else
2998 {
2999 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
3000 append_insn (&insn, &imm_expr, imm_reloc);
3001 }
3002
3003 *input_line_pointer = save_c;
3004 demand_empty_rest_of_line ();
3005}
3006
e23eba97
NC
3007/* Pseudo-op table. */
3008
3009static const pseudo_typeS riscv_pseudo_table[] =
3010{
3011 /* RISC-V-specific pseudo-ops. */
3012 {"option", s_riscv_option, 0},
3013 {"half", cons, 2},
3014 {"word", cons, 4},
3015 {"dword", cons, 8},
3016 {"dtprelword", s_dtprel, 4},
3017 {"dtpreldword", s_dtprel, 8},
3018 {"bss", s_bss, 0},
e23eba97
NC
3019 {"uleb128", s_riscv_leb128, 0},
3020 {"sleb128", s_riscv_leb128, 1},
0e35537d 3021 {"insn", s_riscv_insn, 0},
e23eba97
NC
3022
3023 { NULL, NULL, 0 },
3024};
3025
3026void
3027riscv_pop_insert (void)
3028{
3029 extern void pop_insert (const pseudo_typeS *);
3030
3031 pop_insert (riscv_pseudo_table);
3032}