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