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