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