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