RISC-V: Add .insn support.
[deliverable/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
996/* Fail if an expression is not a constant. */
997
998static void
999check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex)
1000{
1001 if (ex->X_op == O_big)
1002 as_bad (_("unsupported large constant"));
1003 else if (ex->X_op != O_constant)
1004 as_bad (_("Instruction %s requires absolute expression"),
1005 ip->insn_mo->name);
1006 normalize_constant_expr (ex);
1007}
1008
1009static symbolS *
1010make_internal_label (void)
1011{
1012 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg,
1d65abb5 1013 (valueT) frag_now_fix (), frag_now);
e23eba97
NC
1014}
1015
1016/* Load an entry from the GOT. */
1017static void
1018pcrel_access (int destreg, int tempreg, expressionS *ep,
1019 const char *lo_insn, const char *lo_pattern,
1020 bfd_reloc_code_real_type hi_reloc,
1021 bfd_reloc_code_real_type lo_reloc)
1022{
1023 expressionS ep2;
1024 ep2.X_op = O_symbol;
1025 ep2.X_add_symbol = make_internal_label ();
1026 ep2.X_add_number = 0;
1027
1028 macro_build (ep, "auipc", "d,u", tempreg, hi_reloc);
1029 macro_build (&ep2, lo_insn, lo_pattern, destreg, tempreg, lo_reloc);
1030}
1031
1032static void
1033pcrel_load (int destreg, int tempreg, expressionS *ep, const char *lo_insn,
1034 bfd_reloc_code_real_type hi_reloc,
1035 bfd_reloc_code_real_type lo_reloc)
1036{
1037 pcrel_access (destreg, tempreg, ep, lo_insn, "d,s,j", hi_reloc, lo_reloc);
1038}
1039
1040static void
1041pcrel_store (int srcreg, int tempreg, expressionS *ep, const char *lo_insn,
1042 bfd_reloc_code_real_type hi_reloc,
1043 bfd_reloc_code_real_type lo_reloc)
1044{
1045 pcrel_access (srcreg, tempreg, ep, lo_insn, "t,s,q", hi_reloc, lo_reloc);
1046}
1047
1048/* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
1049static void
1050riscv_call (int destreg, int tempreg, expressionS *ep,
1051 bfd_reloc_code_real_type reloc)
1052{
1053 macro_build (ep, "auipc", "d,u", tempreg, reloc);
1054 macro_build (NULL, "jalr", "d,s", destreg, tempreg);
1055}
1056
1057/* Load an integer constant into a register. */
1058
1059static void
1060load_const (int reg, expressionS *ep)
1061{
1062 int shift = RISCV_IMM_BITS;
1063 expressionS upper = *ep, lower = *ep;
1064 lower.X_add_number = (int32_t) ep->X_add_number << (32-shift) >> (32-shift);
1065 upper.X_add_number -= lower.X_add_number;
1066
1067 if (ep->X_op != O_constant)
1068 {
1069 as_bad (_("unsupported large constant"));
1070 return;
1071 }
1072
1d65abb5 1073 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
e23eba97
NC
1074 {
1075 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
1076 while (((upper.X_add_number >> shift) & 1) == 0)
1077 shift++;
1078
1079 upper.X_add_number = (int64_t) upper.X_add_number >> shift;
1d65abb5 1080 load_const (reg, &upper);
e23eba97
NC
1081
1082 macro_build (NULL, "slli", "d,s,>", reg, reg, shift);
1083 if (lower.X_add_number != 0)
1084 macro_build (&lower, "addi", "d,s,j", reg, reg, BFD_RELOC_RISCV_LO12_I);
1085 }
1086 else
1087 {
1088 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
1089 int hi_reg = 0;
1090
1091 if (upper.X_add_number != 0)
1092 {
1093 macro_build (ep, "lui", "d,u", reg, BFD_RELOC_RISCV_HI20);
1094 hi_reg = reg;
1095 }
1096
1097 if (lower.X_add_number != 0 || hi_reg == 0)
1098 macro_build (ep, ADD32_INSN, "d,s,j", reg, hi_reg,
1099 BFD_RELOC_RISCV_LO12_I);
1100 }
1101}
1102
1103/* Expand RISC-V assembly macros into one or more instructions. */
1104static void
1105macro (struct riscv_cl_insn *ip, expressionS *imm_expr,
1106 bfd_reloc_code_real_type *imm_reloc)
1107{
1108 int rd = (ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD;
1109 int rs1 = (ip->insn_opcode >> OP_SH_RS1) & OP_MASK_RS1;
1110 int rs2 = (ip->insn_opcode >> OP_SH_RS2) & OP_MASK_RS2;
1111 int mask = ip->insn_mo->mask;
1112
1113 switch (mask)
1114 {
1115 case M_LI:
1116 load_const (rd, imm_expr);
1117 break;
1118
1119 case M_LA:
1120 case M_LLA:
1121 /* Load the address of a symbol into a register. */
1122 if (!IS_SEXT_32BIT_NUM (imm_expr->X_add_number))
1123 as_bad (_("offset too large"));
1124
1125 if (imm_expr->X_op == O_constant)
1126 load_const (rd, imm_expr);
1127 else if (riscv_opts.pic && mask == M_LA) /* Global PIC symbol */
1128 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1129 BFD_RELOC_RISCV_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1130 else /* Local PIC symbol, or any non-PIC symbol */
1131 pcrel_load (rd, rd, imm_expr, "addi",
1132 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1133 break;
1134
1135 case M_LA_TLS_GD:
1136 pcrel_load (rd, rd, imm_expr, "addi",
1137 BFD_RELOC_RISCV_TLS_GD_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1138 break;
1139
1140 case M_LA_TLS_IE:
1141 pcrel_load (rd, rd, imm_expr, LOAD_ADDRESS_INSN,
1142 BFD_RELOC_RISCV_TLS_GOT_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1143 break;
1144
1145 case M_LB:
1146 pcrel_load (rd, rd, imm_expr, "lb",
1147 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1148 break;
1149
1150 case M_LBU:
1151 pcrel_load (rd, rd, imm_expr, "lbu",
1152 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1153 break;
1154
1155 case M_LH:
1156 pcrel_load (rd, rd, imm_expr, "lh",
1157 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1158 break;
1159
1160 case M_LHU:
1161 pcrel_load (rd, rd, imm_expr, "lhu",
1162 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1163 break;
1164
1165 case M_LW:
1166 pcrel_load (rd, rd, imm_expr, "lw",
1167 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1168 break;
1169
1170 case M_LWU:
1171 pcrel_load (rd, rd, imm_expr, "lwu",
1172 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1173 break;
1174
1175 case M_LD:
1176 pcrel_load (rd, rd, imm_expr, "ld",
1177 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1178 break;
1179
1180 case M_FLW:
1181 pcrel_load (rd, rs1, imm_expr, "flw",
1182 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1183 break;
1184
1185 case M_FLD:
1186 pcrel_load (rd, rs1, imm_expr, "fld",
1187 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_I);
1188 break;
1189
1190 case M_SB:
1191 pcrel_store (rs2, rs1, imm_expr, "sb",
1192 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1193 break;
1194
1195 case M_SH:
1196 pcrel_store (rs2, rs1, imm_expr, "sh",
1197 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1198 break;
1199
1200 case M_SW:
1201 pcrel_store (rs2, rs1, imm_expr, "sw",
1202 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1203 break;
1204
1205 case M_SD:
1206 pcrel_store (rs2, rs1, imm_expr, "sd",
1207 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1208 break;
1209
1210 case M_FSW:
1211 pcrel_store (rs2, rs1, imm_expr, "fsw",
1212 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1213 break;
1214
1215 case M_FSD:
1216 pcrel_store (rs2, rs1, imm_expr, "fsd",
1217 BFD_RELOC_RISCV_PCREL_HI20, BFD_RELOC_RISCV_PCREL_LO12_S);
1218 break;
1219
1220 case M_CALL:
1221 riscv_call (rd, rs1, imm_expr, *imm_reloc);
1222 break;
1223
1224 default:
1225 as_bad (_("Macro %s not implemented"), ip->insn_mo->name);
1226 break;
1227 }
1228}
1229
1230static const struct percent_op_match percent_op_utype[] =
1231{
1232 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20},
1233 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20},
1234 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20},
1235 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20},
1236 {"%hi", BFD_RELOC_RISCV_HI20},
1237 {0, 0}
1238};
1239
1240static const struct percent_op_match percent_op_itype[] =
1241{
1242 {"%lo", BFD_RELOC_RISCV_LO12_I},
1243 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I},
1244 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I},
1245 {0, 0}
1246};
1247
1248static const struct percent_op_match percent_op_stype[] =
1249{
1250 {"%lo", BFD_RELOC_RISCV_LO12_S},
1251 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S},
1252 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S},
1253 {0, 0}
1254};
1255
1256static const struct percent_op_match percent_op_rtype[] =
1257{
1258 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1259 {0, 0}
1260};
1261
1262/* Return true if *STR points to a relocation operator. When returning true,
1263 move *STR over the operator and store its relocation code in *RELOC.
1264 Leave both *STR and *RELOC alone when returning false. */
1265
1266static bfd_boolean
1267parse_relocation (char **str, bfd_reloc_code_real_type *reloc,
1268 const struct percent_op_match *percent_op)
1269{
1270 for ( ; percent_op->str; percent_op++)
1271 if (strncasecmp (*str, percent_op->str, strlen (percent_op->str)) == 0)
1272 {
1273 int len = strlen (percent_op->str);
1274
1275 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
1276 continue;
1277
1278 *str += strlen (percent_op->str);
1279 *reloc = percent_op->reloc;
1280
1281 /* Check whether the output BFD supports this relocation.
1282 If not, issue an error and fall back on something safe. */
45f76423
AW
1283 if (*reloc != BFD_RELOC_UNUSED
1284 && !bfd_reloc_type_lookup (stdoutput, *reloc))
e23eba97
NC
1285 {
1286 as_bad ("relocation %s isn't supported by the current ABI",
1287 percent_op->str);
1288 *reloc = BFD_RELOC_UNUSED;
1289 }
1290 return TRUE;
1291 }
1292 return FALSE;
1293}
1294
1295static void
1296my_getExpression (expressionS *ep, char *str)
1297{
1298 char *save_in;
1299
1300 save_in = input_line_pointer;
1301 input_line_pointer = str;
1302 expression (ep);
1303 expr_end = input_line_pointer;
1304 input_line_pointer = save_in;
1305}
1306
1307/* Parse string STR as a 16-bit relocatable operand. Store the
1308 expression in *EP and the relocation, if any, in RELOC.
1309 Return the number of relocation operators used (0 or 1).
1310
1311 On exit, EXPR_END points to the first character after the expression. */
1312
1313static size_t
1314my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1315 char *str, const struct percent_op_match *percent_op)
1316{
1317 size_t reloc_index;
1318 unsigned crux_depth, str_depth, regno;
1319 char *crux;
1320
1321 /* First, check for integer registers. */
1322 if (reg_lookup (&str, RCLASS_GPR, &regno))
1323 {
1324 ep->X_op = O_register;
1325 ep->X_add_number = regno;
1326 return 0;
1327 }
1328
1329 /* Search for the start of the main expression.
1330 End the loop with CRUX pointing to the start
1331 of the main expression and with CRUX_DEPTH containing the number
1332 of open brackets at that point. */
1333 reloc_index = -1;
1334 str_depth = 0;
1335 do
1336 {
1337 reloc_index++;
1338 crux = str;
1339 crux_depth = str_depth;
1340
1341 /* Skip over whitespace and brackets, keeping count of the number
1342 of brackets. */
1343 while (*str == ' ' || *str == '\t' || *str == '(')
1344 if (*str++ == '(')
1345 str_depth++;
1346 }
1347 while (*str == '%'
1348 && reloc_index < 1
1349 && parse_relocation (&str, reloc, percent_op));
1350
1351 my_getExpression (ep, crux);
1352 str = expr_end;
1353
1354 /* Match every open bracket. */
1355 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
1356 if (*str++ == ')')
1357 crux_depth--;
1358
1359 if (crux_depth > 0)
1360 as_bad ("unclosed '('");
1361
1362 expr_end = str;
1363
1364 return reloc_index;
1365}
1366
0e35537d
JW
1367/* Parse opcode name, could be an mnemonics or number. */
1368static size_t
1369my_getOpcodeExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
1370 char *str, const struct percent_op_match *percent_op)
1371{
1372 const struct opcode_name_t *o = opcode_name_lookup (&str);
1373
1374 if (o != NULL)
1375 {
1376 ep->X_op = O_constant;
1377 ep->X_add_number = o->val;
1378 return 0;
1379 }
1380
1381 return my_getSmallExpression (ep, reloc, str, percent_op);
1382}
1383
f0531ed6
JW
1384/* Detect and handle implicitly zero load-store offsets. For example,
1385 "lw t0, (t1)" is shorthand for "lw t0, 0(t1)". Return TRUE iff such
1386 an implicit offset was detected. */
1387
1388static bfd_boolean
89424b1d 1389riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
f0531ed6
JW
1390{
1391 /* Check whether there is only a single bracketed expression left.
1392 If so, it must be the base register and the constant must be zero. */
1393 if (*s == '(' && strchr (s + 1, '(') == 0)
1394 {
89424b1d
MR
1395 ep->X_op = O_constant;
1396 ep->X_add_number = 0;
f0531ed6
JW
1397 return TRUE;
1398 }
1399
1400 return FALSE;
1401}
1402
e23eba97
NC
1403/* This routine assembles an instruction into its binary format. As a
1404 side effect, it sets the global variable imm_reloc to the type of
1405 relocation to do if one of the operands is an address expression. */
1406
1407static const char *
1408riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
0e35537d 1409 bfd_reloc_code_real_type *imm_reloc, struct hash_control *hash)
e23eba97
NC
1410{
1411 char *s;
1412 const char *args;
1413 char c = 0;
1414 struct riscv_opcode *insn;
1415 char *argsStart;
1416 unsigned int regno;
1417 char save_c = 0;
1418 int argnum;
1419 const struct percent_op_match *p;
1420 const char *error = "unrecognized opcode";
1421
1422 /* Parse the name of the instruction. Terminate the string if whitespace
1423 is found so that hash_find only sees the name part of the string. */
1424 for (s = str; *s != '\0'; ++s)
1425 if (ISSPACE (*s))
1426 {
1427 save_c = *s;
1428 *s++ = '\0';
1429 break;
1430 }
1431
0e35537d 1432 insn = (struct riscv_opcode *) hash_find (hash, str);
e23eba97
NC
1433
1434 argsStart = s;
1435 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
1436 {
1437 if (!riscv_subset_supports (insn->subset))
1438 continue;
1439
1440 create_insn (ip, insn);
1441 argnum = 1;
1442
1443 imm_expr->X_op = O_absent;
1444 *imm_reloc = BFD_RELOC_UNUSED;
1445 p = percent_op_itype;
1446
1447 for (args = insn->args;; ++args)
1448 {
1449 s += strspn (s, " \t");
1450 switch (*args)
1451 {
1452 case '\0': /* End of args. */
1453 if (insn->pinfo != INSN_MACRO)
1454 {
1455 if (!insn->match_func (insn, ip->insn_opcode))
1456 break;
0e35537d
JW
1457
1458 /* For .insn, insn->match and insn->mask are 0. */
1459 if (riscv_insn_length ((insn->match == 0 && insn->mask == 0)
1460 ? ip->insn_opcode
1461 : insn->match) == 2
1462 && !riscv_opts.rvc)
e23eba97
NC
1463 break;
1464 }
1465 if (*s != '\0')
1466 break;
1467 /* Successful assembly. */
1468 error = NULL;
1469 goto out;
1470
1471 case 'C': /* RVC */
1472 switch (*++args)
1473 {
1474 case 's': /* RS1 x8-x15 */
1475 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1476 || !(regno >= 8 && regno <= 15))
1477 break;
1478 INSERT_OPERAND (CRS1S, *ip, regno % 8);
1479 continue;
1480 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
1481 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1482 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
1483 break;
1484 continue;
1485 case 't': /* RS2 x8-x15 */
1486 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1487 || !(regno >= 8 && regno <= 15))
1488 break;
1489 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1490 continue;
1491 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
1492 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1493 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
1494 break;
1495 continue;
1496 case 'U': /* RS1, constrained to equal RD. */
1497 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1498 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
1499 break;
1500 continue;
1501 case 'V': /* RS2 */
1502 if (!reg_lookup (&s, RCLASS_GPR, &regno))
1503 break;
1504 INSERT_OPERAND (CRS2, *ip, regno);
1505 continue;
1506 case 'c': /* RS1, constrained to equal sp. */
1507 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1508 || regno != X_SP)
1509 break;
1510 continue;
1511 case '>':
1512 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1513 || imm_expr->X_op != O_constant
1514 || imm_expr->X_add_number <= 0
1515 || imm_expr->X_add_number >= 64)
1516 break;
1517 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1518rvc_imm_done:
1519 s = expr_end;
1520 imm_expr->X_op = O_absent;
1521 continue;
1522 case '<':
1523 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1524 || imm_expr->X_op != O_constant
1525 || !VALID_RVC_IMM (imm_expr->X_add_number)
1526 || imm_expr->X_add_number <= 0
1527 || imm_expr->X_add_number >= 32)
1528 break;
1529 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1530 goto rvc_imm_done;
0e35537d
JW
1531 case '8':
1532 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1533 || imm_expr->X_op != O_constant
1534 || !VALID_RVC_UIMM8 (imm_expr->X_add_number)
1535 || imm_expr->X_add_number < 0
1536 || imm_expr->X_add_number >= 256)
1537 break;
1538 ip->insn_opcode |= ENCODE_RVC_UIMM8 (imm_expr->X_add_number);
1539 goto rvc_imm_done;
e23eba97
NC
1540 case 'i':
1541 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1542 || imm_expr->X_op != O_constant
1543 || imm_expr->X_add_number == 0
1544 || !VALID_RVC_SIMM3 (imm_expr->X_add_number))
1545 break;
1546 ip->insn_opcode |= ENCODE_RVC_SIMM3 (imm_expr->X_add_number);
1547 goto rvc_imm_done;
1548 case 'j':
1549 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1550 || imm_expr->X_op != O_constant
1551 || imm_expr->X_add_number == 0
1552 || !VALID_RVC_IMM (imm_expr->X_add_number))
1553 break;
1554 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1555 goto rvc_imm_done;
1556 case 'k':
f0531ed6
JW
1557 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1558 continue;
e23eba97
NC
1559 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1560 || imm_expr->X_op != O_constant
1561 || !VALID_RVC_LW_IMM (imm_expr->X_add_number))
1562 break;
1563 ip->insn_opcode |= ENCODE_RVC_LW_IMM (imm_expr->X_add_number);
1564 goto rvc_imm_done;
1565 case 'l':
f0531ed6
JW
1566 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1567 continue;
e23eba97
NC
1568 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1569 || imm_expr->X_op != O_constant
1570 || !VALID_RVC_LD_IMM (imm_expr->X_add_number))
1571 break;
1572 ip->insn_opcode |= ENCODE_RVC_LD_IMM (imm_expr->X_add_number);
1573 goto rvc_imm_done;
1574 case 'm':
f0531ed6
JW
1575 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1576 continue;
e23eba97
NC
1577 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1578 || imm_expr->X_op != O_constant
1579 || !VALID_RVC_LWSP_IMM (imm_expr->X_add_number))
1580 break;
1581 ip->insn_opcode |=
1582 ENCODE_RVC_LWSP_IMM (imm_expr->X_add_number);
1583 goto rvc_imm_done;
1584 case 'n':
f0531ed6
JW
1585 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1586 continue;
e23eba97
NC
1587 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1588 || imm_expr->X_op != O_constant
1589 || !VALID_RVC_LDSP_IMM (imm_expr->X_add_number))
1590 break;
1591 ip->insn_opcode |=
1592 ENCODE_RVC_LDSP_IMM (imm_expr->X_add_number);
1593 goto rvc_imm_done;
b416fe87
KC
1594 case 'o':
1595 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1596 || imm_expr->X_op != O_constant
21a186f2
JW
1597 /* C.addiw, c.li, and c.andi allow zero immediate.
1598 C.addi allows zero immediate as hint. Otherwise this
1599 is same as 'j'. */
b416fe87
KC
1600 || !VALID_RVC_IMM (imm_expr->X_add_number))
1601 break;
1602 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1603 goto rvc_imm_done;
e23eba97
NC
1604 case 'K':
1605 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1606 || imm_expr->X_op != O_constant
1607 || !VALID_RVC_ADDI4SPN_IMM (imm_expr->X_add_number)
1608 || imm_expr->X_add_number == 0)
1609 break;
1610 ip->insn_opcode |=
1611 ENCODE_RVC_ADDI4SPN_IMM (imm_expr->X_add_number);
1612 goto rvc_imm_done;
1613 case 'L':
1614 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1615 || imm_expr->X_op != O_constant
1616 || !VALID_RVC_ADDI16SP_IMM (imm_expr->X_add_number)
1617 || imm_expr->X_add_number == 0)
1618 break;
1619 ip->insn_opcode |=
1620 ENCODE_RVC_ADDI16SP_IMM (imm_expr->X_add_number);
1621 goto rvc_imm_done;
1622 case 'M':
f0531ed6
JW
1623 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1624 continue;
e23eba97
NC
1625 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1626 || imm_expr->X_op != O_constant
1627 || !VALID_RVC_SWSP_IMM (imm_expr->X_add_number))
1628 break;
1629 ip->insn_opcode |=
1630 ENCODE_RVC_SWSP_IMM (imm_expr->X_add_number);
1631 goto rvc_imm_done;
1632 case 'N':
f0531ed6
JW
1633 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1634 continue;
e23eba97
NC
1635 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1636 || imm_expr->X_op != O_constant
1637 || !VALID_RVC_SDSP_IMM (imm_expr->X_add_number))
1638 break;
1639 ip->insn_opcode |=
1640 ENCODE_RVC_SDSP_IMM (imm_expr->X_add_number);
1641 goto rvc_imm_done;
1642 case 'u':
1643 p = percent_op_utype;
1644 if (my_getSmallExpression (imm_expr, imm_reloc, s, p))
1645 break;
1646rvc_lui:
1647 if (imm_expr->X_op != O_constant
1648 || imm_expr->X_add_number <= 0
1649 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
1650 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
1651 && (imm_expr->X_add_number <
1652 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
1653 break;
1654 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1655 goto rvc_imm_done;
1656 case 'v':
1657 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1658 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
1659 || ((int32_t)imm_expr->X_add_number
1660 != imm_expr->X_add_number))
1661 break;
1662 imm_expr->X_add_number =
1663 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
1664 goto rvc_lui;
1665 case 'p':
1666 goto branch;
1667 case 'a':
1668 goto jump;
0e35537d
JW
1669 case 'S': /* Floating-point RS1 x8-x15. */
1670 if (!reg_lookup (&s, RCLASS_FPR, &regno)
1671 || !(regno >= 8 && regno <= 15))
1672 break;
1673 INSERT_OPERAND (CRS1S, *ip, regno % 8);
1674 continue;
e23eba97
NC
1675 case 'D': /* Floating-point RS2 x8-x15. */
1676 if (!reg_lookup (&s, RCLASS_FPR, &regno)
1677 || !(regno >= 8 && regno <= 15))
1678 break;
1679 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1680 continue;
1681 case 'T': /* Floating-point RS2. */
1682 if (!reg_lookup (&s, RCLASS_FPR, &regno))
1683 break;
1684 INSERT_OPERAND (CRS2, *ip, regno);
1685 continue;
0e35537d
JW
1686 case 'F':
1687 switch (*++args)
1688 {
1689 case '4':
1690 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1691 || imm_expr->X_op != O_constant
1692 || imm_expr->X_add_number < 0
1693 || imm_expr->X_add_number >= 16)
1694 {
1695 as_bad (_("bad value for funct4 field, "
1696 "value must be 0...15"));
1697 break;
1698 }
1699
1700 INSERT_OPERAND (CFUNCT4, *ip, imm_expr->X_add_number);
1701 imm_expr->X_op = O_absent;
1702 s = expr_end;
1703 continue;
1704 case '3':
1705 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1706 || imm_expr->X_op != O_constant
1707 || imm_expr->X_add_number < 0
1708 || imm_expr->X_add_number >= 8)
1709 {
1710 as_bad (_("bad value for funct3 field, "
1711 "value must be 0...7"));
1712 break;
1713 }
1714 INSERT_OPERAND (CFUNCT3, *ip, imm_expr->X_add_number);
1715 imm_expr->X_op = O_absent;
1716 s = expr_end;
1717 continue;
1718 default:
1719 as_bad (_("bad compressed FUNCT field"
1720 " specifier 'CF%c'\n"),
1721 *args);
1722 }
1723 break;
1724
e23eba97
NC
1725 default:
1726 as_bad (_("bad RVC field specifier 'C%c'\n"), *args);
1727 }
1728 break;
1729
1730 case ',':
1731 ++argnum;
1732 if (*s++ == *args)
1733 continue;
1734 s--;
1735 break;
1736
1737 case '(':
1738 case ')':
1739 case '[':
1740 case ']':
1741 if (*s++ == *args)
1742 continue;
1743 break;
1744
1745 case '<': /* Shift amount, 0 - 31. */
1746 my_getExpression (imm_expr, s);
1747 check_absolute_expr (ip, imm_expr);
1748 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1749 as_bad (_("Improper shift amount (%lu)"),
1750 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1751 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
1752 imm_expr->X_op = O_absent;
1753 s = expr_end;
1754 continue;
1755
1756 case '>': /* Shift amount, 0 - (XLEN-1). */
1757 my_getExpression (imm_expr, s);
1758 check_absolute_expr (ip, imm_expr);
1759 if ((unsigned long) imm_expr->X_add_number >= xlen)
94f78a77
AW
1760 as_bad (_("Improper shift amount (%lu)"),
1761 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1762 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
1763 imm_expr->X_op = O_absent;
1764 s = expr_end;
1765 continue;
1766
1767 case 'Z': /* CSRRxI immediate. */
1768 my_getExpression (imm_expr, s);
1769 check_absolute_expr (ip, imm_expr);
1770 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1771 as_bad (_("Improper CSRxI immediate (%lu)"),
1772 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1773 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
1774 imm_expr->X_op = O_absent;
1775 s = expr_end;
1776 continue;
1777
1778 case 'E': /* Control register. */
1779 if (reg_lookup (&s, RCLASS_CSR, &regno))
1780 INSERT_OPERAND (CSR, *ip, regno);
1781 else
1782 {
1783 my_getExpression (imm_expr, s);
1784 check_absolute_expr (ip, imm_expr);
1785 if ((unsigned long) imm_expr->X_add_number > 0xfff)
94f78a77
AW
1786 as_bad (_("Improper CSR address (%lu)"),
1787 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1788 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
1789 imm_expr->X_op = O_absent;
1790 s = expr_end;
1791 }
1792 continue;
1793
1794 case 'm': /* Rounding mode. */
1795 if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), &regno))
1796 {
1797 INSERT_OPERAND (RM, *ip, regno);
1798 continue;
1799 }
1800 break;
1801
1802 case 'P':
1803 case 'Q': /* Fence predecessor/successor. */
1804 if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ),
1805 &regno))
1806 {
1807 if (*args == 'P')
1808 INSERT_OPERAND (PRED, *ip, regno);
1809 else
1810 INSERT_OPERAND (SUCC, *ip, regno);
1811 continue;
1812 }
1813 break;
1814
1815 case 'd': /* Destination register. */
1816 case 's': /* Source register. */
1817 case 't': /* Target register. */
0e35537d 1818 case 'r': /* rs3. */
e23eba97
NC
1819 if (reg_lookup (&s, RCLASS_GPR, &regno))
1820 {
1821 c = *args;
1822 if (*s == ' ')
1823 ++s;
1824
1825 /* Now that we have assembled one operand, we use the args
1826 string to figure out where it goes in the instruction. */
1827 switch (c)
1828 {
1829 case 's':
1830 INSERT_OPERAND (RS1, *ip, regno);
1831 break;
1832 case 'd':
1833 INSERT_OPERAND (RD, *ip, regno);
1834 break;
1835 case 't':
1836 INSERT_OPERAND (RS2, *ip, regno);
1837 break;
0e35537d
JW
1838 case 'r':
1839 INSERT_OPERAND (RS3, *ip, regno);
1840 break;
e23eba97
NC
1841 }
1842 continue;
1843 }
1844 break;
1845
1846 case 'D': /* Floating point rd. */
1847 case 'S': /* Floating point rs1. */
1848 case 'T': /* Floating point rs2. */
1849 case 'U': /* Floating point rs1 and rs2. */
1850 case 'R': /* Floating point rs3. */
1851 if (reg_lookup (&s, RCLASS_FPR, &regno))
1852 {
1853 c = *args;
1854 if (*s == ' ')
1855 ++s;
1856 switch (c)
1857 {
1858 case 'D':
1859 INSERT_OPERAND (RD, *ip, regno);
1860 break;
1861 case 'S':
1862 INSERT_OPERAND (RS1, *ip, regno);
1863 break;
1864 case 'U':
1865 INSERT_OPERAND (RS1, *ip, regno);
1866 /* fallthru */
1867 case 'T':
1868 INSERT_OPERAND (RS2, *ip, regno);
1869 break;
1870 case 'R':
1871 INSERT_OPERAND (RS3, *ip, regno);
1872 break;
1873 }
1874 continue;
1875 }
1876
1877 break;
1878
1879 case 'I':
1880 my_getExpression (imm_expr, s);
1881 if (imm_expr->X_op != O_big
1882 && imm_expr->X_op != O_constant)
1883 break;
1884 normalize_constant_expr (imm_expr);
1885 s = expr_end;
1886 continue;
1887
1888 case 'A':
1889 my_getExpression (imm_expr, s);
1890 normalize_constant_expr (imm_expr);
1891 /* The 'A' format specifier must be a symbol. */
1892 if (imm_expr->X_op != O_symbol)
1893 break;
1894 *imm_reloc = BFD_RELOC_32;
1895 s = expr_end;
1896 continue;
1897
1898 case 'j': /* Sign-extended immediate. */
1899 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1900 p = percent_op_itype;
1901 goto alu_op;
1902 case 'q': /* Store displacement. */
1903 p = percent_op_stype;
1904 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
1905 goto load_store;
1906 case 'o': /* Load displacement. */
1907 p = percent_op_itype;
1908 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1909 goto load_store;
1910 case '0': /* AMO "displacement," which must be zero. */
1911 p = percent_op_rtype;
1912 *imm_reloc = BFD_RELOC_UNUSED;
1913load_store:
f0531ed6 1914 if (riscv_handle_implicit_zero_offset (imm_expr, s))
e23eba97
NC
1915 continue;
1916alu_op:
1917 /* If this value won't fit into a 16 bit offset, then go
1918 find a macro that will generate the 32 bit offset
1919 code pattern. */
1920 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
1921 {
1922 normalize_constant_expr (imm_expr);
1923 if (imm_expr->X_op != O_constant
1924 || (*args == '0' && imm_expr->X_add_number != 0)
1925 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
1926 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
1927 break;
1928 }
1929
1930 s = expr_end;
1931 continue;
1932
1933 case 'p': /* PC-relative offset. */
1934branch:
1935 *imm_reloc = BFD_RELOC_12_PCREL;
1936 my_getExpression (imm_expr, s);
1937 s = expr_end;
1938 continue;
1939
1940 case 'u': /* Upper 20 bits. */
1941 p = percent_op_utype;
1942 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p)
1943 && imm_expr->X_op == O_constant)
1944 {
1945 if (imm_expr->X_add_number < 0
1946 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
1947 as_bad (_("lui expression not in range 0..1048575"));
1948
1949 *imm_reloc = BFD_RELOC_RISCV_HI20;
1950 imm_expr->X_add_number <<= RISCV_IMM_BITS;
1951 }
1952 s = expr_end;
1953 continue;
1954
1955 case 'a': /* 20-bit PC-relative offset. */
1956jump:
1957 my_getExpression (imm_expr, s);
1958 s = expr_end;
1959 *imm_reloc = BFD_RELOC_RISCV_JMP;
1960 continue;
1961
1962 case 'c':
1963 my_getExpression (imm_expr, s);
1964 s = expr_end;
1965 if (strcmp (s, "@plt") == 0)
1966 {
1967 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
1968 s += 4;
1969 }
1970 else
1971 *imm_reloc = BFD_RELOC_RISCV_CALL;
1972 continue;
0e35537d
JW
1973 case 'O':
1974 switch (*++args)
1975 {
1976 case '4':
1977 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
1978 || imm_expr->X_op != O_constant
1979 || imm_expr->X_add_number < 0
1980 || imm_expr->X_add_number >= 128
1981 || (imm_expr->X_add_number & 0x3) != 3)
1982 {
1983 as_bad (_("bad value for opcode field, "
1984 "value must be 0...127 and "
1985 "lower 2 bits must be 0x3"));
1986 break;
1987 }
1988
1989 INSERT_OPERAND (OP, *ip, imm_expr->X_add_number);
1990 imm_expr->X_op = O_absent;
1991 s = expr_end;
1992 continue;
1993 case '2':
1994 if (my_getOpcodeExpression (imm_expr, imm_reloc, s, p)
1995 || imm_expr->X_op != O_constant
1996 || imm_expr->X_add_number < 0
1997 || imm_expr->X_add_number >= 3)
1998 {
1999 as_bad (_("bad value for opcode field, "
2000 "value must be 0...2"));
2001 break;
2002 }
2003
2004 INSERT_OPERAND (OP2, *ip, imm_expr->X_add_number);
2005 imm_expr->X_op = O_absent;
2006 s = expr_end;
2007 continue;
2008 default:
2009 as_bad (_("bad Opcode field specifier 'O%c'\n"), *args);
2010 }
2011 break;
2012
2013 case 'F':
2014 switch (*++args)
2015 {
2016 case '7':
2017 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2018 || imm_expr->X_op != O_constant
2019 || imm_expr->X_add_number < 0
2020 || imm_expr->X_add_number >= 128)
2021 {
2022 as_bad (_("bad value for funct7 field, "
2023 "value must be 0...127"));
2024 break;
2025 }
2026
2027 INSERT_OPERAND (FUNCT7, *ip, imm_expr->X_add_number);
2028 imm_expr->X_op = O_absent;
2029 s = expr_end;
2030 continue;
2031 case '3':
2032 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2033 || imm_expr->X_op != O_constant
2034 || imm_expr->X_add_number < 0
2035 || imm_expr->X_add_number >= 8)
2036 {
2037 as_bad (_("bad value for funct3 field, "
2038 "value must be 0...7"));
2039 break;
2040 }
2041
2042 INSERT_OPERAND (FUNCT3, *ip, imm_expr->X_add_number);
2043 imm_expr->X_op = O_absent;
2044 s = expr_end;
2045 continue;
2046 case '2':
2047 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2048 || imm_expr->X_op != O_constant
2049 || imm_expr->X_add_number < 0
2050 || imm_expr->X_add_number >= 4)
2051 {
2052 as_bad (_("bad value for funct2 field, "
2053 "value must be 0...3"));
2054 break;
2055 }
2056
2057 INSERT_OPERAND (FUNCT2, *ip, imm_expr->X_add_number);
2058 imm_expr->X_op = O_absent;
2059 s = expr_end;
2060 continue;
2061
2062 default:
2063 as_bad (_("bad FUNCT field specifier 'F%c'\n"), *args);
2064 }
2065 break;
e23eba97 2066
e925c834
JW
2067 case 'z':
2068 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
2069 || imm_expr->X_op != O_constant
2070 || imm_expr->X_add_number != 0)
2071 break;
2072 s = expr_end;
2073 imm_expr->X_op = O_absent;
2074 continue;
2075
e23eba97
NC
2076 default:
2077 as_fatal (_("internal error: bad argument type %c"), *args);
2078 }
2079 break;
2080 }
2081 s = argsStart;
2082 error = _("illegal operands");
2083 }
2084
2085out:
2086 /* Restore the character we might have clobbered above. */
2087 if (save_c)
2088 *(argsStart - 1) = save_c;
2089
2090 return error;
2091}
2092
2093void
2094md_assemble (char *str)
2095{
2096 struct riscv_cl_insn insn;
2097 expressionS imm_expr;
2098 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2099
0e35537d 2100 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc, op_hash);
e23eba97
NC
2101
2102 if (error)
2103 {
2104 as_bad ("%s `%s'", error, str);
2105 return;
2106 }
2107
2108 if (insn.insn_mo->pinfo == INSN_MACRO)
2109 macro (&insn, &imm_expr, &imm_reloc);
2110 else
2111 append_insn (&insn, &imm_expr, imm_reloc);
2112}
2113
2114const char *
2115md_atof (int type, char *litP, int *sizeP)
2116{
2117 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
2118}
2119
2120void
2121md_number_to_chars (char *buf, valueT val, int n)
2122{
2123 number_to_chars_littleendian (buf, val, n);
2124}
2125
2126const char *md_shortopts = "O::g::G:";
2127
2128enum options
2129{
2922d21d 2130 OPTION_MARCH = OPTION_MD_BASE,
e23eba97
NC
2131 OPTION_PIC,
2132 OPTION_NO_PIC,
2922d21d 2133 OPTION_MABI,
e23eba97
NC
2134 OPTION_END_OF_ENUM
2135};
2136
2137struct option md_longopts[] =
2138{
e23eba97
NC
2139 {"march", required_argument, NULL, OPTION_MARCH},
2140 {"fPIC", no_argument, NULL, OPTION_PIC},
2141 {"fpic", no_argument, NULL, OPTION_PIC},
2142 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2922d21d 2143 {"mabi", required_argument, NULL, OPTION_MABI},
e23eba97
NC
2144
2145 {NULL, no_argument, NULL, 0}
2146};
2147size_t md_longopts_size = sizeof (md_longopts);
2148
2922d21d
AW
2149enum float_abi {
2150 FLOAT_ABI_DEFAULT = -1,
2151 FLOAT_ABI_SOFT,
2152 FLOAT_ABI_SINGLE,
2153 FLOAT_ABI_DOUBLE,
2154 FLOAT_ABI_QUAD
e23eba97 2155};
2922d21d
AW
2156static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
2157
2158static void
2159riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi)
2160{
2161 abi_xlen = new_xlen;
2162 float_abi = new_float_abi;
2163}
e23eba97
NC
2164
2165int
2166md_parse_option (int c, const char *arg)
2167{
2168 switch (c)
2169 {
e23eba97
NC
2170 case OPTION_MARCH:
2171 riscv_set_arch (arg);
2172 break;
2173
2174 case OPTION_NO_PIC:
2175 riscv_opts.pic = FALSE;
2176 break;
2177
2178 case OPTION_PIC:
2179 riscv_opts.pic = TRUE;
2180 break;
2181
2922d21d
AW
2182 case OPTION_MABI:
2183 if (strcmp (arg, "ilp32") == 0)
2184 riscv_set_abi (32, FLOAT_ABI_SOFT);
2185 else if (strcmp (arg, "ilp32f") == 0)
2186 riscv_set_abi (32, FLOAT_ABI_SINGLE);
2187 else if (strcmp (arg, "ilp32d") == 0)
2188 riscv_set_abi (32, FLOAT_ABI_DOUBLE);
2189 else if (strcmp (arg, "ilp32q") == 0)
2190 riscv_set_abi (32, FLOAT_ABI_QUAD);
2191 else if (strcmp (arg, "lp64") == 0)
2192 riscv_set_abi (64, FLOAT_ABI_SOFT);
2193 else if (strcmp (arg, "lp64f") == 0)
2194 riscv_set_abi (64, FLOAT_ABI_SINGLE);
2195 else if (strcmp (arg, "lp64d") == 0)
2196 riscv_set_abi (64, FLOAT_ABI_DOUBLE);
2197 else if (strcmp (arg, "lp64q") == 0)
2198 riscv_set_abi (64, FLOAT_ABI_QUAD);
2199 else
2200 return 0;
2201 break;
2202
e23eba97
NC
2203 default:
2204 return 0;
2205 }
2206
2207 return 1;
2208}
2209
2210void
2211riscv_after_parse_args (void)
2212{
e23eba97
NC
2213 if (xlen == 0)
2214 {
2215 if (strcmp (default_arch, "riscv32") == 0)
2216 xlen = 32;
2217 else if (strcmp (default_arch, "riscv64") == 0)
2218 xlen = 64;
2219 else
2220 as_bad ("unknown default architecture `%s'", default_arch);
2221 }
2922d21d
AW
2222
2223 if (riscv_subsets == NULL)
2224 riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g");
2225
2226 /* Add the RVC extension, regardless of -march, to support .option rvc. */
fecb9c46 2227 riscv_set_rvc (FALSE);
2922d21d
AW
2228 if (riscv_subset_supports ("c"))
2229 riscv_set_rvc (TRUE);
2230 else
2231 riscv_add_subset ("c");
2232
2233 /* Infer ABI from ISA if not specified on command line. */
2234 if (abi_xlen == 0)
2235 abi_xlen = xlen;
2236 else if (abi_xlen > xlen)
2237 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
2238 else if (abi_xlen < xlen)
2239 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
2240
2241 if (float_abi == FLOAT_ABI_DEFAULT)
2242 {
2243 struct riscv_subset *subset;
2244
2245 /* Assume soft-float unless D extension is present. */
2246 float_abi = FLOAT_ABI_SOFT;
2247
2248 for (subset = riscv_subsets; subset != NULL; subset = subset->next)
cc917fd9
KC
2249 {
2250 if (strcasecmp (subset->name, "D") == 0)
2251 float_abi = FLOAT_ABI_DOUBLE;
2252 if (strcasecmp (subset->name, "Q") == 0)
2253 float_abi = FLOAT_ABI_QUAD;
2254 }
2922d21d
AW
2255 }
2256
2257 /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */
2258 elf_flags |= float_abi * (EF_RISCV_FLOAT_ABI & ~(EF_RISCV_FLOAT_ABI << 1));
e23eba97
NC
2259}
2260
2261long
2262md_pcrel_from (fixS *fixP)
2263{
2264 return fixP->fx_where + fixP->fx_frag->fr_address;
2265}
2266
2267/* Apply a fixup to the object file. */
2268
2269void
2270md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2271{
45f76423 2272 unsigned int subtype;
e23eba97 2273 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
45f76423 2274 bfd_boolean relaxable = FALSE;
c1b465c9 2275 offsetT loc;
a6cbf936 2276 segT sub_segment;
e23eba97
NC
2277
2278 /* Remember value for tc_gen_reloc. */
2279 fixP->fx_addnumber = *valP;
2280
2281 switch (fixP->fx_r_type)
2282 {
e23eba97
NC
2283 case BFD_RELOC_RISCV_HI20:
2284 case BFD_RELOC_RISCV_LO12_I:
2285 case BFD_RELOC_RISCV_LO12_S:
45f76423
AW
2286 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2287 | bfd_getl32 (buf), buf);
a5ec5e3f
AW
2288 if (fixP->fx_addsy == NULL)
2289 fixP->fx_done = TRUE;
45f76423
AW
2290 relaxable = TRUE;
2291 break;
2292
2293 case BFD_RELOC_RISCV_GOT_HI20:
e23eba97
NC
2294 case BFD_RELOC_RISCV_ADD8:
2295 case BFD_RELOC_RISCV_ADD16:
2296 case BFD_RELOC_RISCV_ADD32:
2297 case BFD_RELOC_RISCV_ADD64:
45f76423 2298 case BFD_RELOC_RISCV_SUB6:
e23eba97
NC
2299 case BFD_RELOC_RISCV_SUB8:
2300 case BFD_RELOC_RISCV_SUB16:
2301 case BFD_RELOC_RISCV_SUB32:
2302 case BFD_RELOC_RISCV_SUB64:
45f76423
AW
2303 case BFD_RELOC_RISCV_RELAX:
2304 break;
2305
2306 case BFD_RELOC_RISCV_TPREL_HI20:
2307 case BFD_RELOC_RISCV_TPREL_LO12_I:
2308 case BFD_RELOC_RISCV_TPREL_LO12_S:
2309 case BFD_RELOC_RISCV_TPREL_ADD:
2310 relaxable = TRUE;
2311 /* Fall through. */
2312
2313 case BFD_RELOC_RISCV_TLS_GOT_HI20:
2314 case BFD_RELOC_RISCV_TLS_GD_HI20:
2315 case BFD_RELOC_RISCV_TLS_DTPREL32:
2316 case BFD_RELOC_RISCV_TLS_DTPREL64:
e294484e
AW
2317 if (fixP->fx_addsy != NULL)
2318 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2319 else
2320 as_bad_where (fixP->fx_file, fixP->fx_line,
2321 _("TLS relocation against a constant"));
e23eba97
NC
2322 break;
2323
e23eba97 2324 case BFD_RELOC_32:
a6cbf936
KLC
2325 /* Use pc-relative relocation for FDE initial location.
2326 The symbol address in .eh_frame may be adjusted in
2327 _bfd_elf_discard_section_eh_frame, and the content of
2328 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
2329 Therefore, we cannot insert a relocation whose addend symbol is
2330 in .eh_frame. Othrewise, the value may be adjusted twice.*/
2331 if (fixP->fx_addsy && fixP->fx_subsy
2332 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
2333 && strcmp (sub_segment->name, ".eh_frame") == 0
2334 && S_GET_VALUE (fixP->fx_subsy)
2335 == fixP->fx_frag->fr_address + fixP->fx_where)
2336 {
2337 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
2338 fixP->fx_subsy = NULL;
2339 break;
2340 }
2341 /* Fall through. */
2342 case BFD_RELOC_64:
e23eba97
NC
2343 case BFD_RELOC_16:
2344 case BFD_RELOC_8:
45f76423 2345 case BFD_RELOC_RISCV_CFA:
e23eba97
NC
2346 if (fixP->fx_addsy && fixP->fx_subsy)
2347 {
2348 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2349 fixP->fx_next->fx_addsy = fixP->fx_subsy;
2350 fixP->fx_next->fx_subsy = NULL;
2351 fixP->fx_next->fx_offset = 0;
2352 fixP->fx_subsy = NULL;
2353
2354 switch (fixP->fx_r_type)
2355 {
2356 case BFD_RELOC_64:
2357 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
2358 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
2359 break;
2360
2361 case BFD_RELOC_32:
2362 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
2363 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2364 break;
2365
2366 case BFD_RELOC_16:
2367 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
2368 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2369 break;
2370
2371 case BFD_RELOC_8:
2372 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
2373 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
073808ed 2374 break;
e23eba97 2375
45f76423
AW
2376 case BFD_RELOC_RISCV_CFA:
2377 /* Load the byte to get the subtype. */
c1b465c9
KLC
2378 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
2379 loc = fixP->fx_frag->fr_fix - (subtype & 7);
45f76423
AW
2380 switch (subtype)
2381 {
2382 case DW_CFA_advance_loc1:
c1b465c9
KLC
2383 fixP->fx_where = loc + 1;
2384 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2385 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
2386 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
2387 break;
2388
2389 case DW_CFA_advance_loc2:
2390 fixP->fx_size = 2;
45f76423 2391 fixP->fx_next->fx_size = 2;
c1b465c9
KLC
2392 fixP->fx_where = loc + 1;
2393 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2394 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
2395 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2396 break;
2397
2398 case DW_CFA_advance_loc4:
2399 fixP->fx_size = 4;
45f76423 2400 fixP->fx_next->fx_size = 4;
c1b465c9
KLC
2401 fixP->fx_where = loc;
2402 fixP->fx_next->fx_where = loc;
45f76423
AW
2403 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
2404 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2405 break;
2406
2407 default:
2408 if (subtype < 0x80 && (subtype & 0x40))
2409 {
2410 /* DW_CFA_advance_loc */
2aece2ba
KLC
2411 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
2412 fixP->fx_next->fx_frag = fixP->fx_frag;
45f76423
AW
2413 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
2414 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
2415 }
2416 else
2417 as_fatal (_("internal error: bad CFA value #%d"), subtype);
2418 break;
2419 }
2420 break;
2421
e23eba97
NC
2422 default:
2423 /* This case is unreachable. */
2424 abort ();
2425 }
2426 }
2427 /* Fall through. */
2428
2429 case BFD_RELOC_RVA:
2430 /* If we are deleting this reloc entry, we must fill in the
2431 value now. This can happen if we have a .word which is not
2432 resolved when it appears but is later defined. */
2433 if (fixP->fx_addsy == NULL)
2434 {
2435 gas_assert (fixP->fx_size <= sizeof (valueT));
2436 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
2437 fixP->fx_done = 1;
2438 }
2439 break;
2440
2441 case BFD_RELOC_RISCV_JMP:
2442 if (fixP->fx_addsy)
2443 {
2444 /* Fill in a tentative value to improve objdump readability. */
2445 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2446 bfd_vma delta = target - md_pcrel_from (fixP);
2447 bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf);
2448 }
2449 break;
2450
2451 case BFD_RELOC_12_PCREL:
2452 if (fixP->fx_addsy)
2453 {
2454 /* Fill in a tentative value to improve objdump readability. */
2455 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2456 bfd_vma delta = target - md_pcrel_from (fixP);
2457 bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf);
2458 }
2459 break;
2460
2461 case BFD_RELOC_RISCV_RVC_BRANCH:
2462 if (fixP->fx_addsy)
2463 {
2464 /* Fill in a tentative value to improve objdump readability. */
2465 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2466 bfd_vma delta = target - md_pcrel_from (fixP);
2467 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf);
2468 }
2469 break;
2470
2471 case BFD_RELOC_RISCV_RVC_JUMP:
2472 if (fixP->fx_addsy)
2473 {
2474 /* Fill in a tentative value to improve objdump readability. */
2475 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2476 bfd_vma delta = target - md_pcrel_from (fixP);
2477 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf);
2478 }
2479 break;
2480
e23eba97
NC
2481 case BFD_RELOC_RISCV_CALL:
2482 case BFD_RELOC_RISCV_CALL_PLT:
45f76423
AW
2483 relaxable = TRUE;
2484 break;
2485
9d06997a 2486 case BFD_RELOC_RISCV_PCREL_HI20:
45f76423
AW
2487 case BFD_RELOC_RISCV_PCREL_LO12_S:
2488 case BFD_RELOC_RISCV_PCREL_LO12_I:
9d06997a
PD
2489 relaxable = riscv_opts.relax;
2490 break;
2491
e23eba97
NC
2492 case BFD_RELOC_RISCV_ALIGN:
2493 break;
2494
2495 default:
2496 /* We ignore generic BFD relocations we don't know about. */
2497 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
2498 as_fatal (_("internal error: bad relocation #%d"), fixP->fx_r_type);
2499 }
45f76423 2500
e294484e
AW
2501 if (fixP->fx_subsy != NULL)
2502 as_bad_where (fixP->fx_file, fixP->fx_line,
2503 _("unsupported symbol subtraction"));
2504
45f76423
AW
2505 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
2506 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
2507 {
2508 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2509 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
2510 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
2511 }
2512}
2513
2514/* Because the value of .cfi_remember_state may changed after relaxation,
2515 we insert a fix to relocate it again in link-time. */
2516
2517void
2518riscv_pre_output_hook (void)
2519{
2520 const frchainS *frch;
2521 const asection *s;
2522
2523 for (s = stdoutput->sections; s; s = s->next)
2524 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
2525 {
7cb7b948 2526 fragS *frag;
45f76423
AW
2527
2528 for (frag = frch->frch_root; frag; frag = frag->fr_next)
2529 {
2530 if (frag->fr_type == rs_cfa)
2531 {
45f76423
AW
2532 expressionS exp;
2533
2534 symbolS *add_symbol = frag->fr_symbol->sy_value.X_add_symbol;
2535 symbolS *op_symbol = frag->fr_symbol->sy_value.X_op_symbol;
2536
2537 exp.X_op = O_subtract;
2538 exp.X_add_symbol = add_symbol;
2539 exp.X_add_number = 0;
2540 exp.X_op_symbol = op_symbol;
2541
c1b465c9 2542 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
45f76423
AW
2543 BFD_RELOC_RISCV_CFA);
2544 }
2545 }
2546 }
e23eba97
NC
2547}
2548
45f76423 2549
e23eba97
NC
2550/* This structure is used to hold a stack of .option values. */
2551
2552struct riscv_option_stack
2553{
2554 struct riscv_option_stack *next;
2555 struct riscv_set_options options;
2556};
2557
2558static struct riscv_option_stack *riscv_opts_stack;
2559
2560/* Handle the .option pseudo-op. */
2561
2562static void
2563s_riscv_option (int x ATTRIBUTE_UNUSED)
2564{
2565 char *name = input_line_pointer, ch;
2566
2567 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2568 ++input_line_pointer;
2569 ch = *input_line_pointer;
2570 *input_line_pointer = '\0';
2571
2572 if (strcmp (name, "rvc") == 0)
2573 riscv_set_rvc (TRUE);
2574 else if (strcmp (name, "norvc") == 0)
2575 riscv_set_rvc (FALSE);
2576 else if (strcmp (name, "pic") == 0)
2577 riscv_opts.pic = TRUE;
2578 else if (strcmp (name, "nopic") == 0)
2579 riscv_opts.pic = FALSE;
45f76423
AW
2580 else if (strcmp (name, "relax") == 0)
2581 riscv_opts.relax = TRUE;
2582 else if (strcmp (name, "norelax") == 0)
2583 riscv_opts.relax = FALSE;
e23eba97
NC
2584 else if (strcmp (name, "push") == 0)
2585 {
2586 struct riscv_option_stack *s;
2587
2588 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
2589 s->next = riscv_opts_stack;
2590 s->options = riscv_opts;
2591 riscv_opts_stack = s;
2592 }
2593 else if (strcmp (name, "pop") == 0)
2594 {
2595 struct riscv_option_stack *s;
2596
2597 s = riscv_opts_stack;
2598 if (s == NULL)
2599 as_bad (_(".option pop with no .option push"));
2600 else
2601 {
2602 riscv_opts = s->options;
2603 riscv_opts_stack = s->next;
2604 free (s);
2605 }
2606 }
2607 else
2608 {
2609 as_warn (_("Unrecognized .option directive: %s\n"), name);
2610 }
2611 *input_line_pointer = ch;
2612 demand_empty_rest_of_line ();
2613}
2614
2615/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
2616 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
2617 use in DWARF debug information. */
2618
2619static void
2620s_dtprel (int bytes)
2621{
2622 expressionS ex;
2623 char *p;
2624
2625 expression (&ex);
2626
2627 if (ex.X_op != O_symbol)
2628 {
2629 as_bad (_("Unsupported use of %s"), (bytes == 8
2630 ? ".dtpreldword"
2631 : ".dtprelword"));
2632 ignore_rest_of_line ();
2633 }
2634
2635 p = frag_more (bytes);
2636 md_number_to_chars (p, 0, bytes);
2637 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
2638 (bytes == 8
2639 ? BFD_RELOC_RISCV_TLS_DTPREL64
2640 : BFD_RELOC_RISCV_TLS_DTPREL32));
2641
2642 demand_empty_rest_of_line ();
2643}
2644
2645/* Handle the .bss pseudo-op. */
2646
2647static void
2648s_bss (int ignore ATTRIBUTE_UNUSED)
2649{
2650 subseg_set (bss_section, 0);
2651 demand_empty_rest_of_line ();
2652}
2653
e23eba97 2654static void
d115ab8e 2655riscv_make_nops (char *buf, bfd_vma bytes)
e23eba97 2656{
d115ab8e 2657 bfd_vma i = 0;
e23eba97 2658
e5b737de
AW
2659 /* RISC-V instructions cannot begin or end on odd addresses, so this case
2660 means we are not within a valid instruction sequence. It is thus safe
2661 to use a zero byte, even though that is not a valid instruction. */
2662 if (bytes % 2 == 1)
2663 buf[i++] = 0;
2664
2665 /* Use at most one 2-byte NOP. */
2666 if ((bytes - i) % 4 == 2)
e23eba97 2667 {
e5b737de 2668 md_number_to_chars (buf + i, RVC_NOP, 2);
d115ab8e 2669 i += 2;
e23eba97
NC
2670 }
2671
e5b737de 2672 /* Fill the remainder with 4-byte NOPs. */
d115ab8e
AW
2673 for ( ; i < bytes; i += 4)
2674 md_number_to_chars (buf + i, RISCV_NOP, 4);
2675}
e23eba97 2676
d115ab8e
AW
2677/* Called from md_do_align. Used to create an alignment frag in a
2678 code section by emitting a worst-case NOP sequence that the linker
2679 will later relax to the correct number of NOPs. We can't compute
2680 the correct alignment now because of other linker relaxations. */
2681
2682bfd_boolean
2683riscv_frag_align_code (int n)
2684{
e5b737de 2685 bfd_vma bytes = (bfd_vma) 1 << n;
36877bfb
JW
2686 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
2687 bfd_vma worst_case_bytes = bytes - insn_alignment;
2688 char *nops;
ed0816bd 2689 expressionS ex;
d115ab8e 2690
36877bfb
JW
2691 /* If we are moving to a smaller alignment than the instruction size, then no
2692 alignment is required. */
2693 if (bytes <= insn_alignment)
2694 return TRUE;
2695
2696 nops = frag_more (worst_case_bytes);
2697
d115ab8e
AW
2698 /* When not relaxing, riscv_handle_align handles code alignment. */
2699 if (!riscv_opts.relax)
2700 return FALSE;
e23eba97 2701
ed0816bd
PD
2702 ex.X_op = O_constant;
2703 ex.X_add_number = worst_case_bytes;
d115ab8e 2704
ed0816bd 2705 riscv_make_nops (nops, worst_case_bytes);
e23eba97 2706
ed0816bd
PD
2707 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
2708 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
e23eba97 2709
d115ab8e
AW
2710 return TRUE;
2711}
e23eba97 2712
d115ab8e
AW
2713/* Implement HANDLE_ALIGN. */
2714
2715void
2716riscv_handle_align (fragS *fragP)
2717{
2718 switch (fragP->fr_type)
2719 {
2720 case rs_align_code:
2721 /* When relaxing, riscv_frag_align_code handles code alignment. */
2722 if (!riscv_opts.relax)
2723 {
2724 bfd_signed_vma count = fragP->fr_next->fr_address
2725 - fragP->fr_address - fragP->fr_fix;
2726
2727 if (count <= 0)
2728 break;
2729
2730 count &= MAX_MEM_FOR_RS_ALIGN_CODE;
2731 riscv_make_nops (fragP->fr_literal + fragP->fr_fix, count);
2732 fragP->fr_var = count;
2733 }
2734 break;
2735
2736 default:
2737 break;
2738 }
e23eba97
NC
2739}
2740
2741int
2742md_estimate_size_before_relax (fragS *fragp, asection *segtype)
2743{
2744 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE));
2745}
2746
2747/* Translate internal representation of relocation info to BFD target
2748 format. */
2749
2750arelent *
2751tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2752{
2753 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
2754
2755 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2756 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2757 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2758 reloc->addend = fixp->fx_addnumber;
2759
2760 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2761 if (reloc->howto == NULL)
2762 {
2763 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
2764 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
2765 {
2766 /* We don't have R_RISCV_8/16, but for this special case,
2767 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
2768 return reloc;
2769 }
2770
2771 as_bad_where (fixp->fx_file, fixp->fx_line,
2772 _("cannot represent %s relocation in object file"),
2773 bfd_get_reloc_code_name (fixp->fx_r_type));
2774 return NULL;
2775 }
2776
2777 return reloc;
2778}
2779
2780int
2781riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
2782{
2783 if (RELAX_BRANCH_P (fragp->fr_subtype))
2784 {
2785 offsetT old_var = fragp->fr_var;
2786 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
2787 return fragp->fr_var - old_var;
2788 }
2789
2790 return 0;
2791}
2792
2793/* Expand far branches to multi-instruction sequences. */
2794
2795static void
2796md_convert_frag_branch (fragS *fragp)
2797{
2798 bfd_byte *buf;
2799 expressionS exp;
2800 fixS *fixp;
2801 insn_t insn;
2802 int rs1, reloc;
2803
2804 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
2805
2806 exp.X_op = O_symbol;
2807 exp.X_add_symbol = fragp->fr_symbol;
2808 exp.X_add_number = fragp->fr_offset;
2809
2810 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
2811
2812 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
2813 {
2814 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2815 {
2816 case 8:
2817 case 4:
2818 /* Expand the RVC branch into a RISC-V one. */
2819 insn = bfd_getl16 (buf);
2820 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
2821 if ((insn & MASK_C_J) == MATCH_C_J)
2822 insn = MATCH_JAL;
2823 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
2824 insn = MATCH_JAL | (X_RA << OP_SH_RD);
2825 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
2826 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
2827 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
2828 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
2829 else
2830 abort ();
2831 bfd_putl32 (insn, buf);
2832 break;
2833
2834 case 6:
2835 /* Invert the branch condition. Branch over the jump. */
2836 insn = bfd_getl16 (buf);
2837 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
2838 insn |= ENCODE_RVC_B_IMM (6);
2839 bfd_putl16 (insn, buf);
2840 buf += 2;
2841 goto jump;
2842
2843 case 2:
2844 /* Just keep the RVC branch. */
2845 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2846 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
2847 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2848 2, &exp, FALSE, reloc);
2849 buf += 2;
2850 goto done;
2851
2852 default:
1d65abb5 2853 abort ();
e23eba97
NC
2854 }
2855 }
2856
2857 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2858 {
2859 case 8:
2860 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
2861
2862 /* Invert the branch condition. Branch over the jump. */
2863 insn = bfd_getl32 (buf);
2864 insn ^= MATCH_BEQ ^ MATCH_BNE;
2865 insn |= ENCODE_SBTYPE_IMM (8);
2866 md_number_to_chars ((char *) buf, insn, 4);
2867 buf += 4;
2868
2869jump:
2870 /* Jump to the target. */
2871 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2872 4, &exp, FALSE, BFD_RELOC_RISCV_JMP);
2873 md_number_to_chars ((char *) buf, MATCH_JAL, 4);
2874 buf += 4;
2875 break;
2876
2877 case 4:
2878 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2879 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
2880 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2881 4, &exp, FALSE, reloc);
2882 buf += 4;
2883 break;
2884
2885 default:
2886 abort ();
2887 }
2888
2889done:
2890 fixp->fx_file = fragp->fr_file;
2891 fixp->fx_line = fragp->fr_line;
2892
2893 gas_assert (buf == (bfd_byte *)fragp->fr_literal
2894 + fragp->fr_fix + fragp->fr_var);
2895
2896 fragp->fr_fix += fragp->fr_var;
2897}
2898
2899/* Relax a machine dependent frag. This returns the amount by which
2900 the current size of the frag should change. */
2901
2902void
2903md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
2904 fragS *fragp)
2905{
2906 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
2907 md_convert_frag_branch (fragp);
2908}
2909
2910void
2911md_show_usage (FILE *stream)
2912{
2913 fprintf (stream, _("\
2914RISC-V options:\n\
e23eba97
NC
2915 -fpic generate position-independent code\n\
2916 -fno-pic don't generate position-independent code (default)\n\
19683c04
PD
2917 -march=ISA set the RISC-V architecture\n\
2918 -mabi=ABI set the RISC-V ABI\n\
e23eba97
NC
2919"));
2920}
2921
2922/* Standard calling conventions leave the CFA at SP on entry. */
2923void
2924riscv_cfi_frame_initial_instructions (void)
2925{
2926 cfi_add_CFA_def_cfa_register (X_SP);
2927}
2928
2929int
2930tc_riscv_regname_to_dw2regnum (char *regname)
2931{
2932 int reg;
2933
2934 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
2935 return reg;
2936
2937 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
2938 return reg + 32;
2939
2940 as_bad (_("unknown register `%s'"), regname);
2941 return -1;
2942}
2943
2944void
2945riscv_elf_final_processing (void)
2946{
e23eba97 2947 elf_elfheader (stdoutput)->e_flags |= elf_flags;
e23eba97
NC
2948}
2949
2950/* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
2951 since these directives break relaxation when used with symbol deltas. */
2952
2953static void
2954s_riscv_leb128 (int sign)
2955{
2956 expressionS exp;
2957 char *save_in = input_line_pointer;
2958
2959 expression (&exp);
2960 if (exp.X_op != O_constant)
2961 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
2962 demand_empty_rest_of_line ();
2963
2964 input_line_pointer = save_in;
2965 return s_leb128 (sign);
2966}
2967
0e35537d
JW
2968/* Parse the .insn directive. */
2969
2970static void
2971s_riscv_insn (int x ATTRIBUTE_UNUSED)
2972{
2973 char *str = input_line_pointer;
2974 struct riscv_cl_insn insn;
2975 expressionS imm_expr;
2976 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2977 char save_c;
2978
2979 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2980 ++input_line_pointer;
2981
2982 save_c = *input_line_pointer;
2983 *input_line_pointer = '\0';
2984
2985 const char *error = riscv_ip (str, &insn, &imm_expr,
2986 &imm_reloc, insn_type_hash);
2987
2988 if (error)
2989 {
2990 as_bad ("%s `%s'", error, str);
2991 }
2992 else
2993 {
2994 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
2995 append_insn (&insn, &imm_expr, imm_reloc);
2996 }
2997
2998 *input_line_pointer = save_c;
2999 demand_empty_rest_of_line ();
3000}
3001
e23eba97
NC
3002/* Pseudo-op table. */
3003
3004static const pseudo_typeS riscv_pseudo_table[] =
3005{
3006 /* RISC-V-specific pseudo-ops. */
3007 {"option", s_riscv_option, 0},
3008 {"half", cons, 2},
3009 {"word", cons, 4},
3010 {"dword", cons, 8},
3011 {"dtprelword", s_dtprel, 4},
3012 {"dtpreldword", s_dtprel, 8},
3013 {"bss", s_bss, 0},
e23eba97
NC
3014 {"uleb128", s_riscv_leb128, 0},
3015 {"sleb128", s_riscv_leb128, 1},
0e35537d 3016 {"insn", s_riscv_insn, 0},
e23eba97
NC
3017
3018 { NULL, NULL, 0 },
3019};
3020
3021void
3022riscv_pop_insert (void)
3023{
3024 extern void pop_insert (const pseudo_typeS *);
3025
3026 pop_insert (riscv_pseudo_table);
3027}
This page took 0.289634 seconds and 4 git commands to generate.