x86: properly force / avoid forcing EVEX encoding
[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
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,
71060565
JW
2139 OPTION_RELAX,
2140 OPTION_NO_RELAX,
e23eba97
NC
2141 OPTION_END_OF_ENUM
2142};
2143
2144struct option md_longopts[] =
2145{
e23eba97
NC
2146 {"march", required_argument, NULL, OPTION_MARCH},
2147 {"fPIC", no_argument, NULL, OPTION_PIC},
2148 {"fpic", no_argument, NULL, OPTION_PIC},
2149 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2922d21d 2150 {"mabi", required_argument, NULL, OPTION_MABI},
71060565
JW
2151 {"mrelax", no_argument, NULL, OPTION_RELAX},
2152 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
e23eba97
NC
2153
2154 {NULL, no_argument, NULL, 0}
2155};
2156size_t md_longopts_size = sizeof (md_longopts);
2157
2922d21d
AW
2158enum float_abi {
2159 FLOAT_ABI_DEFAULT = -1,
2160 FLOAT_ABI_SOFT,
2161 FLOAT_ABI_SINGLE,
2162 FLOAT_ABI_DOUBLE,
2163 FLOAT_ABI_QUAD
e23eba97 2164};
2922d21d
AW
2165static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
2166
2167static void
2168riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi)
2169{
2170 abi_xlen = new_xlen;
2171 float_abi = new_float_abi;
2172}
e23eba97
NC
2173
2174int
2175md_parse_option (int c, const char *arg)
2176{
2177 switch (c)
2178 {
e23eba97
NC
2179 case OPTION_MARCH:
2180 riscv_set_arch (arg);
2181 break;
2182
2183 case OPTION_NO_PIC:
2184 riscv_opts.pic = FALSE;
2185 break;
2186
2187 case OPTION_PIC:
2188 riscv_opts.pic = TRUE;
2189 break;
2190
2922d21d
AW
2191 case OPTION_MABI:
2192 if (strcmp (arg, "ilp32") == 0)
2193 riscv_set_abi (32, FLOAT_ABI_SOFT);
2194 else if (strcmp (arg, "ilp32f") == 0)
2195 riscv_set_abi (32, FLOAT_ABI_SINGLE);
2196 else if (strcmp (arg, "ilp32d") == 0)
2197 riscv_set_abi (32, FLOAT_ABI_DOUBLE);
2198 else if (strcmp (arg, "ilp32q") == 0)
2199 riscv_set_abi (32, FLOAT_ABI_QUAD);
2200 else if (strcmp (arg, "lp64") == 0)
2201 riscv_set_abi (64, FLOAT_ABI_SOFT);
2202 else if (strcmp (arg, "lp64f") == 0)
2203 riscv_set_abi (64, FLOAT_ABI_SINGLE);
2204 else if (strcmp (arg, "lp64d") == 0)
2205 riscv_set_abi (64, FLOAT_ABI_DOUBLE);
2206 else if (strcmp (arg, "lp64q") == 0)
2207 riscv_set_abi (64, FLOAT_ABI_QUAD);
2208 else
2209 return 0;
2210 break;
2211
71060565
JW
2212 case OPTION_RELAX:
2213 riscv_opts.relax = TRUE;
2214 break;
2215
2216 case OPTION_NO_RELAX:
2217 riscv_opts.relax = FALSE;
2218 break;
2219
e23eba97
NC
2220 default:
2221 return 0;
2222 }
2223
2224 return 1;
2225}
2226
2227void
2228riscv_after_parse_args (void)
2229{
e23eba97
NC
2230 if (xlen == 0)
2231 {
2232 if (strcmp (default_arch, "riscv32") == 0)
2233 xlen = 32;
2234 else if (strcmp (default_arch, "riscv64") == 0)
2235 xlen = 64;
2236 else
2237 as_bad ("unknown default architecture `%s'", default_arch);
2238 }
2922d21d
AW
2239
2240 if (riscv_subsets == NULL)
2241 riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g");
2242
2243 /* Add the RVC extension, regardless of -march, to support .option rvc. */
fecb9c46 2244 riscv_set_rvc (FALSE);
2922d21d
AW
2245 if (riscv_subset_supports ("c"))
2246 riscv_set_rvc (TRUE);
2247 else
2248 riscv_add_subset ("c");
2249
2250 /* Infer ABI from ISA if not specified on command line. */
2251 if (abi_xlen == 0)
2252 abi_xlen = xlen;
2253 else if (abi_xlen > xlen)
2254 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
2255 else if (abi_xlen < xlen)
2256 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
2257
2258 if (float_abi == FLOAT_ABI_DEFAULT)
2259 {
2260 struct riscv_subset *subset;
2261
2262 /* Assume soft-float unless D extension is present. */
2263 float_abi = FLOAT_ABI_SOFT;
2264
2265 for (subset = riscv_subsets; subset != NULL; subset = subset->next)
cc917fd9
KC
2266 {
2267 if (strcasecmp (subset->name, "D") == 0)
2268 float_abi = FLOAT_ABI_DOUBLE;
2269 if (strcasecmp (subset->name, "Q") == 0)
2270 float_abi = FLOAT_ABI_QUAD;
2271 }
2922d21d
AW
2272 }
2273
2274 /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */
2275 elf_flags |= float_abi * (EF_RISCV_FLOAT_ABI & ~(EF_RISCV_FLOAT_ABI << 1));
e23eba97
NC
2276}
2277
2278long
2279md_pcrel_from (fixS *fixP)
2280{
2281 return fixP->fx_where + fixP->fx_frag->fr_address;
2282}
2283
2284/* Apply a fixup to the object file. */
2285
2286void
2287md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2288{
45f76423 2289 unsigned int subtype;
e23eba97 2290 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
45f76423 2291 bfd_boolean relaxable = FALSE;
c1b465c9 2292 offsetT loc;
a6cbf936 2293 segT sub_segment;
e23eba97
NC
2294
2295 /* Remember value for tc_gen_reloc. */
2296 fixP->fx_addnumber = *valP;
2297
2298 switch (fixP->fx_r_type)
2299 {
e23eba97
NC
2300 case BFD_RELOC_RISCV_HI20:
2301 case BFD_RELOC_RISCV_LO12_I:
2302 case BFD_RELOC_RISCV_LO12_S:
45f76423
AW
2303 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2304 | bfd_getl32 (buf), buf);
a5ec5e3f
AW
2305 if (fixP->fx_addsy == NULL)
2306 fixP->fx_done = TRUE;
45f76423
AW
2307 relaxable = TRUE;
2308 break;
2309
2310 case BFD_RELOC_RISCV_GOT_HI20:
e23eba97
NC
2311 case BFD_RELOC_RISCV_ADD8:
2312 case BFD_RELOC_RISCV_ADD16:
2313 case BFD_RELOC_RISCV_ADD32:
2314 case BFD_RELOC_RISCV_ADD64:
45f76423 2315 case BFD_RELOC_RISCV_SUB6:
e23eba97
NC
2316 case BFD_RELOC_RISCV_SUB8:
2317 case BFD_RELOC_RISCV_SUB16:
2318 case BFD_RELOC_RISCV_SUB32:
2319 case BFD_RELOC_RISCV_SUB64:
45f76423
AW
2320 case BFD_RELOC_RISCV_RELAX:
2321 break;
2322
2323 case BFD_RELOC_RISCV_TPREL_HI20:
2324 case BFD_RELOC_RISCV_TPREL_LO12_I:
2325 case BFD_RELOC_RISCV_TPREL_LO12_S:
2326 case BFD_RELOC_RISCV_TPREL_ADD:
2327 relaxable = TRUE;
2328 /* Fall through. */
2329
2330 case BFD_RELOC_RISCV_TLS_GOT_HI20:
2331 case BFD_RELOC_RISCV_TLS_GD_HI20:
2332 case BFD_RELOC_RISCV_TLS_DTPREL32:
2333 case BFD_RELOC_RISCV_TLS_DTPREL64:
e294484e
AW
2334 if (fixP->fx_addsy != NULL)
2335 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2336 else
2337 as_bad_where (fixP->fx_file, fixP->fx_line,
2338 _("TLS relocation against a constant"));
e23eba97
NC
2339 break;
2340
e23eba97 2341 case BFD_RELOC_32:
a6cbf936
KLC
2342 /* Use pc-relative relocation for FDE initial location.
2343 The symbol address in .eh_frame may be adjusted in
2344 _bfd_elf_discard_section_eh_frame, and the content of
2345 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
2346 Therefore, we cannot insert a relocation whose addend symbol is
2347 in .eh_frame. Othrewise, the value may be adjusted twice.*/
2348 if (fixP->fx_addsy && fixP->fx_subsy
2349 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
2350 && strcmp (sub_segment->name, ".eh_frame") == 0
2351 && S_GET_VALUE (fixP->fx_subsy)
2352 == fixP->fx_frag->fr_address + fixP->fx_where)
2353 {
2354 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
2355 fixP->fx_subsy = NULL;
2356 break;
2357 }
2358 /* Fall through. */
2359 case BFD_RELOC_64:
e23eba97
NC
2360 case BFD_RELOC_16:
2361 case BFD_RELOC_8:
45f76423 2362 case BFD_RELOC_RISCV_CFA:
e23eba97
NC
2363 if (fixP->fx_addsy && fixP->fx_subsy)
2364 {
2365 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2366 fixP->fx_next->fx_addsy = fixP->fx_subsy;
2367 fixP->fx_next->fx_subsy = NULL;
2368 fixP->fx_next->fx_offset = 0;
2369 fixP->fx_subsy = NULL;
2370
2371 switch (fixP->fx_r_type)
2372 {
2373 case BFD_RELOC_64:
2374 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
2375 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
2376 break;
2377
2378 case BFD_RELOC_32:
2379 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
2380 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2381 break;
2382
2383 case BFD_RELOC_16:
2384 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
2385 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2386 break;
2387
2388 case BFD_RELOC_8:
2389 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
2390 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
073808ed 2391 break;
e23eba97 2392
45f76423
AW
2393 case BFD_RELOC_RISCV_CFA:
2394 /* Load the byte to get the subtype. */
c1b465c9
KLC
2395 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
2396 loc = fixP->fx_frag->fr_fix - (subtype & 7);
45f76423
AW
2397 switch (subtype)
2398 {
2399 case DW_CFA_advance_loc1:
c1b465c9
KLC
2400 fixP->fx_where = loc + 1;
2401 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2402 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
2403 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
2404 break;
2405
2406 case DW_CFA_advance_loc2:
2407 fixP->fx_size = 2;
45f76423 2408 fixP->fx_next->fx_size = 2;
c1b465c9
KLC
2409 fixP->fx_where = loc + 1;
2410 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2411 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
2412 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2413 break;
2414
2415 case DW_CFA_advance_loc4:
2416 fixP->fx_size = 4;
45f76423 2417 fixP->fx_next->fx_size = 4;
c1b465c9
KLC
2418 fixP->fx_where = loc;
2419 fixP->fx_next->fx_where = loc;
45f76423
AW
2420 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
2421 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2422 break;
2423
2424 default:
2425 if (subtype < 0x80 && (subtype & 0x40))
2426 {
2427 /* DW_CFA_advance_loc */
2aece2ba
KLC
2428 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
2429 fixP->fx_next->fx_frag = fixP->fx_frag;
45f76423
AW
2430 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
2431 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
2432 }
2433 else
2434 as_fatal (_("internal error: bad CFA value #%d"), subtype);
2435 break;
2436 }
2437 break;
2438
e23eba97
NC
2439 default:
2440 /* This case is unreachable. */
2441 abort ();
2442 }
2443 }
2444 /* Fall through. */
2445
2446 case BFD_RELOC_RVA:
2447 /* If we are deleting this reloc entry, we must fill in the
2448 value now. This can happen if we have a .word which is not
2449 resolved when it appears but is later defined. */
2450 if (fixP->fx_addsy == NULL)
2451 {
2452 gas_assert (fixP->fx_size <= sizeof (valueT));
2453 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
2454 fixP->fx_done = 1;
2455 }
2456 break;
2457
2458 case BFD_RELOC_RISCV_JMP:
2459 if (fixP->fx_addsy)
2460 {
2461 /* Fill in a tentative value to improve objdump readability. */
2462 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2463 bfd_vma delta = target - md_pcrel_from (fixP);
2464 bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf);
2465 }
2466 break;
2467
2468 case BFD_RELOC_12_PCREL:
2469 if (fixP->fx_addsy)
2470 {
2471 /* Fill in a tentative value to improve objdump readability. */
2472 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2473 bfd_vma delta = target - md_pcrel_from (fixP);
2474 bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf);
2475 }
2476 break;
2477
2478 case BFD_RELOC_RISCV_RVC_BRANCH:
2479 if (fixP->fx_addsy)
2480 {
2481 /* Fill in a tentative value to improve objdump readability. */
2482 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2483 bfd_vma delta = target - md_pcrel_from (fixP);
2484 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf);
2485 }
2486 break;
2487
2488 case BFD_RELOC_RISCV_RVC_JUMP:
2489 if (fixP->fx_addsy)
2490 {
2491 /* Fill in a tentative value to improve objdump readability. */
2492 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2493 bfd_vma delta = target - md_pcrel_from (fixP);
2494 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf);
2495 }
2496 break;
2497
e23eba97
NC
2498 case BFD_RELOC_RISCV_CALL:
2499 case BFD_RELOC_RISCV_CALL_PLT:
45f76423
AW
2500 relaxable = TRUE;
2501 break;
2502
9d06997a 2503 case BFD_RELOC_RISCV_PCREL_HI20:
45f76423
AW
2504 case BFD_RELOC_RISCV_PCREL_LO12_S:
2505 case BFD_RELOC_RISCV_PCREL_LO12_I:
9d06997a
PD
2506 relaxable = riscv_opts.relax;
2507 break;
2508
e23eba97
NC
2509 case BFD_RELOC_RISCV_ALIGN:
2510 break;
2511
2512 default:
2513 /* We ignore generic BFD relocations we don't know about. */
2514 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
2515 as_fatal (_("internal error: bad relocation #%d"), fixP->fx_r_type);
2516 }
45f76423 2517
e294484e
AW
2518 if (fixP->fx_subsy != NULL)
2519 as_bad_where (fixP->fx_file, fixP->fx_line,
2520 _("unsupported symbol subtraction"));
2521
45f76423
AW
2522 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
2523 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
2524 {
2525 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2526 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
2527 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
2528 }
2529}
2530
2531/* Because the value of .cfi_remember_state may changed after relaxation,
2532 we insert a fix to relocate it again in link-time. */
2533
2534void
2535riscv_pre_output_hook (void)
2536{
2537 const frchainS *frch;
2538 const asection *s;
2539
2540 for (s = stdoutput->sections; s; s = s->next)
2541 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
2542 {
7cb7b948 2543 fragS *frag;
45f76423
AW
2544
2545 for (frag = frch->frch_root; frag; frag = frag->fr_next)
2546 {
2547 if (frag->fr_type == rs_cfa)
2548 {
45f76423
AW
2549 expressionS exp;
2550
2551 symbolS *add_symbol = frag->fr_symbol->sy_value.X_add_symbol;
2552 symbolS *op_symbol = frag->fr_symbol->sy_value.X_op_symbol;
2553
2554 exp.X_op = O_subtract;
2555 exp.X_add_symbol = add_symbol;
2556 exp.X_add_number = 0;
2557 exp.X_op_symbol = op_symbol;
2558
c1b465c9 2559 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
45f76423
AW
2560 BFD_RELOC_RISCV_CFA);
2561 }
2562 }
2563 }
e23eba97
NC
2564}
2565
45f76423 2566
e23eba97
NC
2567/* This structure is used to hold a stack of .option values. */
2568
2569struct riscv_option_stack
2570{
2571 struct riscv_option_stack *next;
2572 struct riscv_set_options options;
2573};
2574
2575static struct riscv_option_stack *riscv_opts_stack;
2576
2577/* Handle the .option pseudo-op. */
2578
2579static void
2580s_riscv_option (int x ATTRIBUTE_UNUSED)
2581{
2582 char *name = input_line_pointer, ch;
2583
2584 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2585 ++input_line_pointer;
2586 ch = *input_line_pointer;
2587 *input_line_pointer = '\0';
2588
2589 if (strcmp (name, "rvc") == 0)
2590 riscv_set_rvc (TRUE);
2591 else if (strcmp (name, "norvc") == 0)
2592 riscv_set_rvc (FALSE);
2593 else if (strcmp (name, "pic") == 0)
2594 riscv_opts.pic = TRUE;
2595 else if (strcmp (name, "nopic") == 0)
2596 riscv_opts.pic = FALSE;
45f76423
AW
2597 else if (strcmp (name, "relax") == 0)
2598 riscv_opts.relax = TRUE;
2599 else if (strcmp (name, "norelax") == 0)
2600 riscv_opts.relax = FALSE;
e23eba97
NC
2601 else if (strcmp (name, "push") == 0)
2602 {
2603 struct riscv_option_stack *s;
2604
2605 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
2606 s->next = riscv_opts_stack;
2607 s->options = riscv_opts;
2608 riscv_opts_stack = s;
2609 }
2610 else if (strcmp (name, "pop") == 0)
2611 {
2612 struct riscv_option_stack *s;
2613
2614 s = riscv_opts_stack;
2615 if (s == NULL)
2616 as_bad (_(".option pop with no .option push"));
2617 else
2618 {
2619 riscv_opts = s->options;
2620 riscv_opts_stack = s->next;
2621 free (s);
2622 }
2623 }
2624 else
2625 {
2626 as_warn (_("Unrecognized .option directive: %s\n"), name);
2627 }
2628 *input_line_pointer = ch;
2629 demand_empty_rest_of_line ();
2630}
2631
2632/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
2633 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
2634 use in DWARF debug information. */
2635
2636static void
2637s_dtprel (int bytes)
2638{
2639 expressionS ex;
2640 char *p;
2641
2642 expression (&ex);
2643
2644 if (ex.X_op != O_symbol)
2645 {
2646 as_bad (_("Unsupported use of %s"), (bytes == 8
2647 ? ".dtpreldword"
2648 : ".dtprelword"));
2649 ignore_rest_of_line ();
2650 }
2651
2652 p = frag_more (bytes);
2653 md_number_to_chars (p, 0, bytes);
2654 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
2655 (bytes == 8
2656 ? BFD_RELOC_RISCV_TLS_DTPREL64
2657 : BFD_RELOC_RISCV_TLS_DTPREL32));
2658
2659 demand_empty_rest_of_line ();
2660}
2661
2662/* Handle the .bss pseudo-op. */
2663
2664static void
2665s_bss (int ignore ATTRIBUTE_UNUSED)
2666{
2667 subseg_set (bss_section, 0);
2668 demand_empty_rest_of_line ();
2669}
2670
e23eba97 2671static void
d115ab8e 2672riscv_make_nops (char *buf, bfd_vma bytes)
e23eba97 2673{
d115ab8e 2674 bfd_vma i = 0;
e23eba97 2675
e5b737de
AW
2676 /* RISC-V instructions cannot begin or end on odd addresses, so this case
2677 means we are not within a valid instruction sequence. It is thus safe
2678 to use a zero byte, even though that is not a valid instruction. */
2679 if (bytes % 2 == 1)
2680 buf[i++] = 0;
2681
2682 /* Use at most one 2-byte NOP. */
2683 if ((bytes - i) % 4 == 2)
e23eba97 2684 {
e5b737de 2685 md_number_to_chars (buf + i, RVC_NOP, 2);
d115ab8e 2686 i += 2;
e23eba97
NC
2687 }
2688
e5b737de 2689 /* Fill the remainder with 4-byte NOPs. */
d115ab8e
AW
2690 for ( ; i < bytes; i += 4)
2691 md_number_to_chars (buf + i, RISCV_NOP, 4);
2692}
e23eba97 2693
d115ab8e
AW
2694/* Called from md_do_align. Used to create an alignment frag in a
2695 code section by emitting a worst-case NOP sequence that the linker
2696 will later relax to the correct number of NOPs. We can't compute
2697 the correct alignment now because of other linker relaxations. */
2698
2699bfd_boolean
2700riscv_frag_align_code (int n)
2701{
e5b737de 2702 bfd_vma bytes = (bfd_vma) 1 << n;
36877bfb
JW
2703 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
2704 bfd_vma worst_case_bytes = bytes - insn_alignment;
2705 char *nops;
ed0816bd 2706 expressionS ex;
d115ab8e 2707
36877bfb
JW
2708 /* If we are moving to a smaller alignment than the instruction size, then no
2709 alignment is required. */
2710 if (bytes <= insn_alignment)
2711 return TRUE;
2712
2713 nops = frag_more (worst_case_bytes);
2714
d115ab8e
AW
2715 /* When not relaxing, riscv_handle_align handles code alignment. */
2716 if (!riscv_opts.relax)
2717 return FALSE;
e23eba97 2718
ed0816bd
PD
2719 ex.X_op = O_constant;
2720 ex.X_add_number = worst_case_bytes;
d115ab8e 2721
ed0816bd 2722 riscv_make_nops (nops, worst_case_bytes);
e23eba97 2723
ed0816bd
PD
2724 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
2725 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
e23eba97 2726
d115ab8e
AW
2727 return TRUE;
2728}
e23eba97 2729
d115ab8e
AW
2730/* Implement HANDLE_ALIGN. */
2731
2732void
2733riscv_handle_align (fragS *fragP)
2734{
2735 switch (fragP->fr_type)
2736 {
2737 case rs_align_code:
2738 /* When relaxing, riscv_frag_align_code handles code alignment. */
2739 if (!riscv_opts.relax)
2740 {
2741 bfd_signed_vma count = fragP->fr_next->fr_address
2742 - fragP->fr_address - fragP->fr_fix;
2743
2744 if (count <= 0)
2745 break;
2746
2747 count &= MAX_MEM_FOR_RS_ALIGN_CODE;
2748 riscv_make_nops (fragP->fr_literal + fragP->fr_fix, count);
2749 fragP->fr_var = count;
2750 }
2751 break;
2752
2753 default:
2754 break;
2755 }
e23eba97
NC
2756}
2757
2758int
2759md_estimate_size_before_relax (fragS *fragp, asection *segtype)
2760{
2761 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE));
2762}
2763
2764/* Translate internal representation of relocation info to BFD target
2765 format. */
2766
2767arelent *
2768tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2769{
2770 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
2771
2772 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2773 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2774 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2775 reloc->addend = fixp->fx_addnumber;
2776
2777 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2778 if (reloc->howto == NULL)
2779 {
2780 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
2781 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
2782 {
2783 /* We don't have R_RISCV_8/16, but for this special case,
2784 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
2785 return reloc;
2786 }
2787
2788 as_bad_where (fixp->fx_file, fixp->fx_line,
2789 _("cannot represent %s relocation in object file"),
2790 bfd_get_reloc_code_name (fixp->fx_r_type));
2791 return NULL;
2792 }
2793
2794 return reloc;
2795}
2796
2797int
2798riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
2799{
2800 if (RELAX_BRANCH_P (fragp->fr_subtype))
2801 {
2802 offsetT old_var = fragp->fr_var;
2803 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
2804 return fragp->fr_var - old_var;
2805 }
2806
2807 return 0;
2808}
2809
2810/* Expand far branches to multi-instruction sequences. */
2811
2812static void
2813md_convert_frag_branch (fragS *fragp)
2814{
2815 bfd_byte *buf;
2816 expressionS exp;
2817 fixS *fixp;
2818 insn_t insn;
2819 int rs1, reloc;
2820
2821 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
2822
2823 exp.X_op = O_symbol;
2824 exp.X_add_symbol = fragp->fr_symbol;
2825 exp.X_add_number = fragp->fr_offset;
2826
2827 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
2828
2829 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
2830 {
2831 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2832 {
2833 case 8:
2834 case 4:
2835 /* Expand the RVC branch into a RISC-V one. */
2836 insn = bfd_getl16 (buf);
2837 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
2838 if ((insn & MASK_C_J) == MATCH_C_J)
2839 insn = MATCH_JAL;
2840 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
2841 insn = MATCH_JAL | (X_RA << OP_SH_RD);
2842 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
2843 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
2844 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
2845 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
2846 else
2847 abort ();
2848 bfd_putl32 (insn, buf);
2849 break;
2850
2851 case 6:
2852 /* Invert the branch condition. Branch over the jump. */
2853 insn = bfd_getl16 (buf);
2854 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
2855 insn |= ENCODE_RVC_B_IMM (6);
2856 bfd_putl16 (insn, buf);
2857 buf += 2;
2858 goto jump;
2859
2860 case 2:
2861 /* Just keep the RVC branch. */
2862 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2863 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
2864 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2865 2, &exp, FALSE, reloc);
2866 buf += 2;
2867 goto done;
2868
2869 default:
1d65abb5 2870 abort ();
e23eba97
NC
2871 }
2872 }
2873
2874 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2875 {
2876 case 8:
2877 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
2878
2879 /* Invert the branch condition. Branch over the jump. */
2880 insn = bfd_getl32 (buf);
2881 insn ^= MATCH_BEQ ^ MATCH_BNE;
2882 insn |= ENCODE_SBTYPE_IMM (8);
2883 md_number_to_chars ((char *) buf, insn, 4);
2884 buf += 4;
2885
2886jump:
2887 /* Jump to the target. */
2888 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2889 4, &exp, FALSE, BFD_RELOC_RISCV_JMP);
2890 md_number_to_chars ((char *) buf, MATCH_JAL, 4);
2891 buf += 4;
2892 break;
2893
2894 case 4:
2895 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2896 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
2897 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2898 4, &exp, FALSE, reloc);
2899 buf += 4;
2900 break;
2901
2902 default:
2903 abort ();
2904 }
2905
2906done:
2907 fixp->fx_file = fragp->fr_file;
2908 fixp->fx_line = fragp->fr_line;
2909
2910 gas_assert (buf == (bfd_byte *)fragp->fr_literal
2911 + fragp->fr_fix + fragp->fr_var);
2912
2913 fragp->fr_fix += fragp->fr_var;
2914}
2915
2916/* Relax a machine dependent frag. This returns the amount by which
2917 the current size of the frag should change. */
2918
2919void
2920md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
2921 fragS *fragp)
2922{
2923 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
2924 md_convert_frag_branch (fragp);
2925}
2926
2927void
2928md_show_usage (FILE *stream)
2929{
2930 fprintf (stream, _("\
2931RISC-V options:\n\
e23eba97
NC
2932 -fpic generate position-independent code\n\
2933 -fno-pic don't generate position-independent code (default)\n\
19683c04
PD
2934 -march=ISA set the RISC-V architecture\n\
2935 -mabi=ABI set the RISC-V ABI\n\
71060565
JW
2936 -mrelax enable relax (default)\n\
2937 -mno-relax disable relax\n\
e23eba97
NC
2938"));
2939}
2940
2941/* Standard calling conventions leave the CFA at SP on entry. */
2942void
2943riscv_cfi_frame_initial_instructions (void)
2944{
2945 cfi_add_CFA_def_cfa_register (X_SP);
2946}
2947
2948int
2949tc_riscv_regname_to_dw2regnum (char *regname)
2950{
2951 int reg;
2952
2953 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
2954 return reg;
2955
2956 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
2957 return reg + 32;
2958
2959 as_bad (_("unknown register `%s'"), regname);
2960 return -1;
2961}
2962
2963void
2964riscv_elf_final_processing (void)
2965{
e23eba97 2966 elf_elfheader (stdoutput)->e_flags |= elf_flags;
e23eba97
NC
2967}
2968
2969/* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
2970 since these directives break relaxation when used with symbol deltas. */
2971
2972static void
2973s_riscv_leb128 (int sign)
2974{
2975 expressionS exp;
2976 char *save_in = input_line_pointer;
2977
2978 expression (&exp);
2979 if (exp.X_op != O_constant)
2980 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
2981 demand_empty_rest_of_line ();
2982
2983 input_line_pointer = save_in;
2984 return s_leb128 (sign);
2985}
2986
0e35537d
JW
2987/* Parse the .insn directive. */
2988
2989static void
2990s_riscv_insn (int x ATTRIBUTE_UNUSED)
2991{
2992 char *str = input_line_pointer;
2993 struct riscv_cl_insn insn;
2994 expressionS imm_expr;
2995 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
2996 char save_c;
2997
2998 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2999 ++input_line_pointer;
3000
3001 save_c = *input_line_pointer;
3002 *input_line_pointer = '\0';
3003
3004 const char *error = riscv_ip (str, &insn, &imm_expr,
3005 &imm_reloc, insn_type_hash);
3006
3007 if (error)
3008 {
3009 as_bad ("%s `%s'", error, str);
3010 }
3011 else
3012 {
3013 gas_assert (insn.insn_mo->pinfo != INSN_MACRO);
3014 append_insn (&insn, &imm_expr, imm_reloc);
3015 }
3016
3017 *input_line_pointer = save_c;
3018 demand_empty_rest_of_line ();
3019}
3020
e23eba97
NC
3021/* Pseudo-op table. */
3022
3023static const pseudo_typeS riscv_pseudo_table[] =
3024{
3025 /* RISC-V-specific pseudo-ops. */
3026 {"option", s_riscv_option, 0},
3027 {"half", cons, 2},
3028 {"word", cons, 4},
3029 {"dword", cons, 8},
3030 {"dtprelword", s_dtprel, 4},
3031 {"dtpreldword", s_dtprel, 8},
3032 {"bss", s_bss, 0},
e23eba97
NC
3033 {"uleb128", s_riscv_leb128, 0},
3034 {"sleb128", s_riscv_leb128, 1},
0e35537d 3035 {"insn", s_riscv_insn, 0},
e23eba97
NC
3036
3037 { NULL, NULL, 0 },
3038};
3039
3040void
3041riscv_pop_insert (void)
3042{
3043 extern void pop_insert (const pseudo_typeS *);
3044
3045 pop_insert (riscv_pseudo_table);
3046}
This page took 0.22234 seconds and 4 git commands to generate.