Add new AArch64 FP16 FM{A|S} instructions.
[deliverable/binutils-gdb.git] / gas / config / tc-riscv.c
CommitLineData
e23eba97 1/* tc-riscv.c -- RISC-V assembler
2571583a 2 Copyright (C) 2011-2017 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;
e23eba97
NC
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
593struct 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
602void
603md_begin (void)
604{
605 int i = 0;
2922d21d 606 unsigned long mach = xlen == 64 ? bfd_mach_riscv64 : bfd_mach_riscv32;
e23eba97 607
2922d21d 608 if (! bfd_set_arch_mach (stdoutput, bfd_arch_riscv, mach))
e23eba97
NC
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);
1270b047 646#define DECLARE_CSR_ALIAS(name, num) DECLARE_CSR(name, num);
e23eba97
NC
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
45f76423
AW
654static insn_t
655riscv_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
e23eba97
NC
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
680static void
681append_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
1d65abb5 690 gas_assert (address_expr);
e23eba97
NC
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 }
45f76423 703 else
e23eba97 704 {
45f76423
AW
705 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
706 if (howto == NULL)
707 as_bad (_("Unsupported RISC-V relocation number %d"), reloc_type);
e23eba97 708
45f76423
AW
709 ip->fixp = fix_new_exp (ip->frag, ip->where,
710 bfd_get_reloc_size (howto),
711 address_expr, FALSE, reloc_type);
e23eba97 712
45f76423 713 ip->fixp->fx_tcbit = riscv_opts.relax;
e23eba97 714 }
e23eba97
NC
715 }
716
e23eba97
NC
717 add_fixed_insn (ip);
718 install_insn (ip);
f77bb6c5
JW
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 }
e23eba97
NC
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
741static void
742macro_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. */
806static void
807normalize_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
819static void
820check_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
830static symbolS *
831make_internal_label (void)
832{
833 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg,
1d65abb5 834 (valueT) frag_now_fix (), frag_now);
e23eba97
NC
835}
836
837/* Load an entry from the GOT. */
838static void
839pcrel_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
853static void
854pcrel_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
861static void
862pcrel_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. */
870static void
871riscv_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
880static void
881load_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
1d65abb5 894 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
e23eba97
NC
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;
1d65abb5 901 load_const (reg, &upper);
e23eba97
NC
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. */
925static void
926macro (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
1051static 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
1061static 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
1069static 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
1077static 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
1087static bfd_boolean
1088parse_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. */
45f76423
AW
1104 if (*reloc != BFD_RELOC_UNUSED
1105 && !bfd_reloc_type_lookup (stdoutput, *reloc))
e23eba97
NC
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
1116static void
1117my_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
1134static size_t
1135my_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/* This routine assembles an instruction into its binary format. As a
1189 side effect, it sets the global variable imm_reloc to the type of
1190 relocation to do if one of the operands is an address expression. */
1191
1192static const char *
1193riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
1194 bfd_reloc_code_real_type *imm_reloc)
1195{
1196 char *s;
1197 const char *args;
1198 char c = 0;
1199 struct riscv_opcode *insn;
1200 char *argsStart;
1201 unsigned int regno;
1202 char save_c = 0;
1203 int argnum;
1204 const struct percent_op_match *p;
1205 const char *error = "unrecognized opcode";
1206
1207 /* Parse the name of the instruction. Terminate the string if whitespace
1208 is found so that hash_find only sees the name part of the string. */
1209 for (s = str; *s != '\0'; ++s)
1210 if (ISSPACE (*s))
1211 {
1212 save_c = *s;
1213 *s++ = '\0';
1214 break;
1215 }
1216
1217 insn = (struct riscv_opcode *) hash_find (op_hash, str);
1218
1219 argsStart = s;
1220 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
1221 {
1222 if (!riscv_subset_supports (insn->subset))
1223 continue;
1224
1225 create_insn (ip, insn);
1226 argnum = 1;
1227
1228 imm_expr->X_op = O_absent;
1229 *imm_reloc = BFD_RELOC_UNUSED;
1230 p = percent_op_itype;
1231
1232 for (args = insn->args;; ++args)
1233 {
1234 s += strspn (s, " \t");
1235 switch (*args)
1236 {
1237 case '\0': /* End of args. */
1238 if (insn->pinfo != INSN_MACRO)
1239 {
1240 if (!insn->match_func (insn, ip->insn_opcode))
1241 break;
1242 if (riscv_insn_length (insn->match) == 2 && !riscv_opts.rvc)
1243 break;
1244 }
1245 if (*s != '\0')
1246 break;
1247 /* Successful assembly. */
1248 error = NULL;
1249 goto out;
1250
1251 case 'C': /* RVC */
1252 switch (*++args)
1253 {
1254 case 's': /* RS1 x8-x15 */
1255 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1256 || !(regno >= 8 && regno <= 15))
1257 break;
1258 INSERT_OPERAND (CRS1S, *ip, regno % 8);
1259 continue;
1260 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
1261 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1262 || EXTRACT_OPERAND (CRS1S, ip->insn_opcode) + 8 != regno)
1263 break;
1264 continue;
1265 case 't': /* RS2 x8-x15 */
1266 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1267 || !(regno >= 8 && regno <= 15))
1268 break;
1269 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1270 continue;
1271 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
1272 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1273 || EXTRACT_OPERAND (CRS2S, ip->insn_opcode) + 8 != regno)
1274 break;
1275 continue;
1276 case 'U': /* RS1, constrained to equal RD. */
1277 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1278 || EXTRACT_OPERAND (RD, ip->insn_opcode) != regno)
1279 break;
1280 continue;
1281 case 'V': /* RS2 */
1282 if (!reg_lookup (&s, RCLASS_GPR, &regno))
1283 break;
1284 INSERT_OPERAND (CRS2, *ip, regno);
1285 continue;
1286 case 'c': /* RS1, constrained to equal sp. */
1287 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1288 || regno != X_SP)
1289 break;
1290 continue;
1291 case '>':
1292 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1293 || imm_expr->X_op != O_constant
1294 || imm_expr->X_add_number <= 0
1295 || imm_expr->X_add_number >= 64)
1296 break;
1297 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1298rvc_imm_done:
1299 s = expr_end;
1300 imm_expr->X_op = O_absent;
1301 continue;
1302 case '<':
1303 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1304 || imm_expr->X_op != O_constant
1305 || !VALID_RVC_IMM (imm_expr->X_add_number)
1306 || imm_expr->X_add_number <= 0
1307 || imm_expr->X_add_number >= 32)
1308 break;
1309 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1310 goto rvc_imm_done;
1311 case 'i':
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 || !VALID_RVC_SIMM3 (imm_expr->X_add_number))
1316 break;
1317 ip->insn_opcode |= ENCODE_RVC_SIMM3 (imm_expr->X_add_number);
1318 goto rvc_imm_done;
1319 case 'j':
1320 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1321 || imm_expr->X_op != O_constant
1322 || imm_expr->X_add_number == 0
1323 || !VALID_RVC_IMM (imm_expr->X_add_number))
1324 break;
1325 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1326 goto rvc_imm_done;
1327 case 'k':
1328 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1329 || imm_expr->X_op != O_constant
1330 || !VALID_RVC_LW_IMM (imm_expr->X_add_number))
1331 break;
1332 ip->insn_opcode |= ENCODE_RVC_LW_IMM (imm_expr->X_add_number);
1333 goto rvc_imm_done;
1334 case 'l':
1335 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1336 || imm_expr->X_op != O_constant
1337 || !VALID_RVC_LD_IMM (imm_expr->X_add_number))
1338 break;
1339 ip->insn_opcode |= ENCODE_RVC_LD_IMM (imm_expr->X_add_number);
1340 goto rvc_imm_done;
1341 case 'm':
1342 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1343 || imm_expr->X_op != O_constant
1344 || !VALID_RVC_LWSP_IMM (imm_expr->X_add_number))
1345 break;
1346 ip->insn_opcode |=
1347 ENCODE_RVC_LWSP_IMM (imm_expr->X_add_number);
1348 goto rvc_imm_done;
1349 case 'n':
1350 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1351 || imm_expr->X_op != O_constant
1352 || !VALID_RVC_LDSP_IMM (imm_expr->X_add_number))
1353 break;
1354 ip->insn_opcode |=
1355 ENCODE_RVC_LDSP_IMM (imm_expr->X_add_number);
1356 goto rvc_imm_done;
b416fe87
KC
1357 case 'o':
1358 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1359 || imm_expr->X_op != O_constant
1360 || !VALID_RVC_IMM (imm_expr->X_add_number))
1361 break;
1362 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1363 goto rvc_imm_done;
e23eba97
NC
1364 case 'K':
1365 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1366 || imm_expr->X_op != O_constant
1367 || !VALID_RVC_ADDI4SPN_IMM (imm_expr->X_add_number)
1368 || imm_expr->X_add_number == 0)
1369 break;
1370 ip->insn_opcode |=
1371 ENCODE_RVC_ADDI4SPN_IMM (imm_expr->X_add_number);
1372 goto rvc_imm_done;
1373 case 'L':
1374 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1375 || imm_expr->X_op != O_constant
1376 || !VALID_RVC_ADDI16SP_IMM (imm_expr->X_add_number)
1377 || imm_expr->X_add_number == 0)
1378 break;
1379 ip->insn_opcode |=
1380 ENCODE_RVC_ADDI16SP_IMM (imm_expr->X_add_number);
1381 goto rvc_imm_done;
1382 case 'M':
1383 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1384 || imm_expr->X_op != O_constant
1385 || !VALID_RVC_SWSP_IMM (imm_expr->X_add_number))
1386 break;
1387 ip->insn_opcode |=
1388 ENCODE_RVC_SWSP_IMM (imm_expr->X_add_number);
1389 goto rvc_imm_done;
1390 case 'N':
1391 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1392 || imm_expr->X_op != O_constant
1393 || !VALID_RVC_SDSP_IMM (imm_expr->X_add_number))
1394 break;
1395 ip->insn_opcode |=
1396 ENCODE_RVC_SDSP_IMM (imm_expr->X_add_number);
1397 goto rvc_imm_done;
1398 case 'u':
1399 p = percent_op_utype;
1400 if (my_getSmallExpression (imm_expr, imm_reloc, s, p))
1401 break;
1402rvc_lui:
1403 if (imm_expr->X_op != O_constant
1404 || imm_expr->X_add_number <= 0
1405 || imm_expr->X_add_number >= RISCV_BIGIMM_REACH
1406 || (imm_expr->X_add_number >= RISCV_RVC_IMM_REACH / 2
1407 && (imm_expr->X_add_number <
1408 RISCV_BIGIMM_REACH - RISCV_RVC_IMM_REACH / 2)))
1409 break;
1410 ip->insn_opcode |= ENCODE_RVC_IMM (imm_expr->X_add_number);
1411 goto rvc_imm_done;
1412 case 'v':
1413 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1414 || (imm_expr->X_add_number & (RISCV_IMM_REACH - 1))
1415 || ((int32_t)imm_expr->X_add_number
1416 != imm_expr->X_add_number))
1417 break;
1418 imm_expr->X_add_number =
1419 ((uint32_t) imm_expr->X_add_number) >> RISCV_IMM_BITS;
1420 goto rvc_lui;
1421 case 'p':
1422 goto branch;
1423 case 'a':
1424 goto jump;
1425 case 'D': /* Floating-point RS2 x8-x15. */
1426 if (!reg_lookup (&s, RCLASS_FPR, &regno)
1427 || !(regno >= 8 && regno <= 15))
1428 break;
1429 INSERT_OPERAND (CRS2S, *ip, regno % 8);
1430 continue;
1431 case 'T': /* Floating-point RS2. */
1432 if (!reg_lookup (&s, RCLASS_FPR, &regno))
1433 break;
1434 INSERT_OPERAND (CRS2, *ip, regno);
1435 continue;
1436 default:
1437 as_bad (_("bad RVC field specifier 'C%c'\n"), *args);
1438 }
1439 break;
1440
1441 case ',':
1442 ++argnum;
1443 if (*s++ == *args)
1444 continue;
1445 s--;
1446 break;
1447
1448 case '(':
1449 case ')':
1450 case '[':
1451 case ']':
1452 if (*s++ == *args)
1453 continue;
1454 break;
1455
1456 case '<': /* Shift amount, 0 - 31. */
1457 my_getExpression (imm_expr, s);
1458 check_absolute_expr (ip, imm_expr);
1459 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1460 as_bad (_("Improper shift amount (%lu)"),
1461 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1462 INSERT_OPERAND (SHAMTW, *ip, imm_expr->X_add_number);
1463 imm_expr->X_op = O_absent;
1464 s = expr_end;
1465 continue;
1466
1467 case '>': /* Shift amount, 0 - (XLEN-1). */
1468 my_getExpression (imm_expr, s);
1469 check_absolute_expr (ip, imm_expr);
1470 if ((unsigned long) imm_expr->X_add_number >= xlen)
94f78a77
AW
1471 as_bad (_("Improper shift amount (%lu)"),
1472 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1473 INSERT_OPERAND (SHAMT, *ip, imm_expr->X_add_number);
1474 imm_expr->X_op = O_absent;
1475 s = expr_end;
1476 continue;
1477
1478 case 'Z': /* CSRRxI immediate. */
1479 my_getExpression (imm_expr, s);
1480 check_absolute_expr (ip, imm_expr);
1481 if ((unsigned long) imm_expr->X_add_number > 31)
94f78a77
AW
1482 as_bad (_("Improper CSRxI immediate (%lu)"),
1483 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1484 INSERT_OPERAND (RS1, *ip, imm_expr->X_add_number);
1485 imm_expr->X_op = O_absent;
1486 s = expr_end;
1487 continue;
1488
1489 case 'E': /* Control register. */
1490 if (reg_lookup (&s, RCLASS_CSR, &regno))
1491 INSERT_OPERAND (CSR, *ip, regno);
1492 else
1493 {
1494 my_getExpression (imm_expr, s);
1495 check_absolute_expr (ip, imm_expr);
1496 if ((unsigned long) imm_expr->X_add_number > 0xfff)
94f78a77
AW
1497 as_bad (_("Improper CSR address (%lu)"),
1498 (unsigned long) imm_expr->X_add_number);
e23eba97
NC
1499 INSERT_OPERAND (CSR, *ip, imm_expr->X_add_number);
1500 imm_expr->X_op = O_absent;
1501 s = expr_end;
1502 }
1503 continue;
1504
1505 case 'm': /* Rounding mode. */
1506 if (arg_lookup (&s, riscv_rm, ARRAY_SIZE (riscv_rm), &regno))
1507 {
1508 INSERT_OPERAND (RM, *ip, regno);
1509 continue;
1510 }
1511 break;
1512
1513 case 'P':
1514 case 'Q': /* Fence predecessor/successor. */
1515 if (arg_lookup (&s, riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ),
1516 &regno))
1517 {
1518 if (*args == 'P')
1519 INSERT_OPERAND (PRED, *ip, regno);
1520 else
1521 INSERT_OPERAND (SUCC, *ip, regno);
1522 continue;
1523 }
1524 break;
1525
1526 case 'd': /* Destination register. */
1527 case 's': /* Source register. */
1528 case 't': /* Target register. */
1529 if (reg_lookup (&s, RCLASS_GPR, &regno))
1530 {
1531 c = *args;
1532 if (*s == ' ')
1533 ++s;
1534
1535 /* Now that we have assembled one operand, we use the args
1536 string to figure out where it goes in the instruction. */
1537 switch (c)
1538 {
1539 case 's':
1540 INSERT_OPERAND (RS1, *ip, regno);
1541 break;
1542 case 'd':
1543 INSERT_OPERAND (RD, *ip, regno);
1544 break;
1545 case 't':
1546 INSERT_OPERAND (RS2, *ip, regno);
1547 break;
1548 }
1549 continue;
1550 }
1551 break;
1552
1553 case 'D': /* Floating point rd. */
1554 case 'S': /* Floating point rs1. */
1555 case 'T': /* Floating point rs2. */
1556 case 'U': /* Floating point rs1 and rs2. */
1557 case 'R': /* Floating point rs3. */
1558 if (reg_lookup (&s, RCLASS_FPR, &regno))
1559 {
1560 c = *args;
1561 if (*s == ' ')
1562 ++s;
1563 switch (c)
1564 {
1565 case 'D':
1566 INSERT_OPERAND (RD, *ip, regno);
1567 break;
1568 case 'S':
1569 INSERT_OPERAND (RS1, *ip, regno);
1570 break;
1571 case 'U':
1572 INSERT_OPERAND (RS1, *ip, regno);
1573 /* fallthru */
1574 case 'T':
1575 INSERT_OPERAND (RS2, *ip, regno);
1576 break;
1577 case 'R':
1578 INSERT_OPERAND (RS3, *ip, regno);
1579 break;
1580 }
1581 continue;
1582 }
1583
1584 break;
1585
1586 case 'I':
1587 my_getExpression (imm_expr, s);
1588 if (imm_expr->X_op != O_big
1589 && imm_expr->X_op != O_constant)
1590 break;
1591 normalize_constant_expr (imm_expr);
1592 s = expr_end;
1593 continue;
1594
1595 case 'A':
1596 my_getExpression (imm_expr, s);
1597 normalize_constant_expr (imm_expr);
1598 /* The 'A' format specifier must be a symbol. */
1599 if (imm_expr->X_op != O_symbol)
1600 break;
1601 *imm_reloc = BFD_RELOC_32;
1602 s = expr_end;
1603 continue;
1604
1605 case 'j': /* Sign-extended immediate. */
1606 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1607 p = percent_op_itype;
1608 goto alu_op;
1609 case 'q': /* Store displacement. */
1610 p = percent_op_stype;
1611 *imm_reloc = BFD_RELOC_RISCV_LO12_S;
1612 goto load_store;
1613 case 'o': /* Load displacement. */
1614 p = percent_op_itype;
1615 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
1616 goto load_store;
1617 case '0': /* AMO "displacement," which must be zero. */
1618 p = percent_op_rtype;
1619 *imm_reloc = BFD_RELOC_UNUSED;
1620load_store:
1621 /* Check whether there is only a single bracketed expression
1622 left. If so, it must be the base register and the
1623 constant must be zero. */
1624 imm_expr->X_op = O_constant;
1625 imm_expr->X_add_number = 0;
1626 if (*s == '(' && strchr (s + 1, '(') == 0)
1627 continue;
1628alu_op:
1629 /* If this value won't fit into a 16 bit offset, then go
1630 find a macro that will generate the 32 bit offset
1631 code pattern. */
1632 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
1633 {
1634 normalize_constant_expr (imm_expr);
1635 if (imm_expr->X_op != O_constant
1636 || (*args == '0' && imm_expr->X_add_number != 0)
1637 || imm_expr->X_add_number >= (signed)RISCV_IMM_REACH/2
1638 || imm_expr->X_add_number < -(signed)RISCV_IMM_REACH/2)
1639 break;
1640 }
1641
1642 s = expr_end;
1643 continue;
1644
1645 case 'p': /* PC-relative offset. */
1646branch:
1647 *imm_reloc = BFD_RELOC_12_PCREL;
1648 my_getExpression (imm_expr, s);
1649 s = expr_end;
1650 continue;
1651
1652 case 'u': /* Upper 20 bits. */
1653 p = percent_op_utype;
1654 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p)
1655 && imm_expr->X_op == O_constant)
1656 {
1657 if (imm_expr->X_add_number < 0
1658 || imm_expr->X_add_number >= (signed)RISCV_BIGIMM_REACH)
1659 as_bad (_("lui expression not in range 0..1048575"));
1660
1661 *imm_reloc = BFD_RELOC_RISCV_HI20;
1662 imm_expr->X_add_number <<= RISCV_IMM_BITS;
1663 }
1664 s = expr_end;
1665 continue;
1666
1667 case 'a': /* 20-bit PC-relative offset. */
1668jump:
1669 my_getExpression (imm_expr, s);
1670 s = expr_end;
1671 *imm_reloc = BFD_RELOC_RISCV_JMP;
1672 continue;
1673
1674 case 'c':
1675 my_getExpression (imm_expr, s);
1676 s = expr_end;
1677 if (strcmp (s, "@plt") == 0)
1678 {
1679 *imm_reloc = BFD_RELOC_RISCV_CALL_PLT;
1680 s += 4;
1681 }
1682 else
1683 *imm_reloc = BFD_RELOC_RISCV_CALL;
1684 continue;
1685
1686 default:
1687 as_fatal (_("internal error: bad argument type %c"), *args);
1688 }
1689 break;
1690 }
1691 s = argsStart;
1692 error = _("illegal operands");
1693 }
1694
1695out:
1696 /* Restore the character we might have clobbered above. */
1697 if (save_c)
1698 *(argsStart - 1) = save_c;
1699
1700 return error;
1701}
1702
1703void
1704md_assemble (char *str)
1705{
1706 struct riscv_cl_insn insn;
1707 expressionS imm_expr;
1708 bfd_reloc_code_real_type imm_reloc = BFD_RELOC_UNUSED;
1709
1710 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc);
1711
1712 if (error)
1713 {
1714 as_bad ("%s `%s'", error, str);
1715 return;
1716 }
1717
1718 if (insn.insn_mo->pinfo == INSN_MACRO)
1719 macro (&insn, &imm_expr, &imm_reloc);
1720 else
1721 append_insn (&insn, &imm_expr, imm_reloc);
1722}
1723
1724const char *
1725md_atof (int type, char *litP, int *sizeP)
1726{
1727 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
1728}
1729
1730void
1731md_number_to_chars (char *buf, valueT val, int n)
1732{
1733 number_to_chars_littleendian (buf, val, n);
1734}
1735
1736const char *md_shortopts = "O::g::G:";
1737
1738enum options
1739{
2922d21d 1740 OPTION_MARCH = OPTION_MD_BASE,
e23eba97
NC
1741 OPTION_PIC,
1742 OPTION_NO_PIC,
2922d21d 1743 OPTION_MABI,
e23eba97
NC
1744 OPTION_END_OF_ENUM
1745};
1746
1747struct option md_longopts[] =
1748{
e23eba97
NC
1749 {"march", required_argument, NULL, OPTION_MARCH},
1750 {"fPIC", no_argument, NULL, OPTION_PIC},
1751 {"fpic", no_argument, NULL, OPTION_PIC},
1752 {"fno-pic", no_argument, NULL, OPTION_NO_PIC},
2922d21d 1753 {"mabi", required_argument, NULL, OPTION_MABI},
e23eba97
NC
1754
1755 {NULL, no_argument, NULL, 0}
1756};
1757size_t md_longopts_size = sizeof (md_longopts);
1758
2922d21d
AW
1759enum float_abi {
1760 FLOAT_ABI_DEFAULT = -1,
1761 FLOAT_ABI_SOFT,
1762 FLOAT_ABI_SINGLE,
1763 FLOAT_ABI_DOUBLE,
1764 FLOAT_ABI_QUAD
e23eba97 1765};
2922d21d
AW
1766static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
1767
1768static void
1769riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi)
1770{
1771 abi_xlen = new_xlen;
1772 float_abi = new_float_abi;
1773}
e23eba97
NC
1774
1775int
1776md_parse_option (int c, const char *arg)
1777{
1778 switch (c)
1779 {
e23eba97
NC
1780 case OPTION_MARCH:
1781 riscv_set_arch (arg);
1782 break;
1783
1784 case OPTION_NO_PIC:
1785 riscv_opts.pic = FALSE;
1786 break;
1787
1788 case OPTION_PIC:
1789 riscv_opts.pic = TRUE;
1790 break;
1791
2922d21d
AW
1792 case OPTION_MABI:
1793 if (strcmp (arg, "ilp32") == 0)
1794 riscv_set_abi (32, FLOAT_ABI_SOFT);
1795 else if (strcmp (arg, "ilp32f") == 0)
1796 riscv_set_abi (32, FLOAT_ABI_SINGLE);
1797 else if (strcmp (arg, "ilp32d") == 0)
1798 riscv_set_abi (32, FLOAT_ABI_DOUBLE);
1799 else if (strcmp (arg, "ilp32q") == 0)
1800 riscv_set_abi (32, FLOAT_ABI_QUAD);
1801 else if (strcmp (arg, "lp64") == 0)
1802 riscv_set_abi (64, FLOAT_ABI_SOFT);
1803 else if (strcmp (arg, "lp64f") == 0)
1804 riscv_set_abi (64, FLOAT_ABI_SINGLE);
1805 else if (strcmp (arg, "lp64d") == 0)
1806 riscv_set_abi (64, FLOAT_ABI_DOUBLE);
1807 else if (strcmp (arg, "lp64q") == 0)
1808 riscv_set_abi (64, FLOAT_ABI_QUAD);
1809 else
1810 return 0;
1811 break;
1812
e23eba97
NC
1813 default:
1814 return 0;
1815 }
1816
1817 return 1;
1818}
1819
1820void
1821riscv_after_parse_args (void)
1822{
e23eba97
NC
1823 if (xlen == 0)
1824 {
1825 if (strcmp (default_arch, "riscv32") == 0)
1826 xlen = 32;
1827 else if (strcmp (default_arch, "riscv64") == 0)
1828 xlen = 64;
1829 else
1830 as_bad ("unknown default architecture `%s'", default_arch);
1831 }
2922d21d
AW
1832
1833 if (riscv_subsets == NULL)
1834 riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g");
1835
1836 /* Add the RVC extension, regardless of -march, to support .option rvc. */
fecb9c46 1837 riscv_set_rvc (FALSE);
2922d21d
AW
1838 if (riscv_subset_supports ("c"))
1839 riscv_set_rvc (TRUE);
1840 else
1841 riscv_add_subset ("c");
1842
1843 /* Infer ABI from ISA if not specified on command line. */
1844 if (abi_xlen == 0)
1845 abi_xlen = xlen;
1846 else if (abi_xlen > xlen)
1847 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen, xlen);
1848 else if (abi_xlen < xlen)
1849 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen, xlen);
1850
1851 if (float_abi == FLOAT_ABI_DEFAULT)
1852 {
1853 struct riscv_subset *subset;
1854
1855 /* Assume soft-float unless D extension is present. */
1856 float_abi = FLOAT_ABI_SOFT;
1857
1858 for (subset = riscv_subsets; subset != NULL; subset = subset->next)
cc917fd9
KC
1859 {
1860 if (strcasecmp (subset->name, "D") == 0)
1861 float_abi = FLOAT_ABI_DOUBLE;
1862 if (strcasecmp (subset->name, "Q") == 0)
1863 float_abi = FLOAT_ABI_QUAD;
1864 }
2922d21d
AW
1865 }
1866
1867 /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */
1868 elf_flags |= float_abi * (EF_RISCV_FLOAT_ABI & ~(EF_RISCV_FLOAT_ABI << 1));
e23eba97
NC
1869}
1870
1871long
1872md_pcrel_from (fixS *fixP)
1873{
1874 return fixP->fx_where + fixP->fx_frag->fr_address;
1875}
1876
1877/* Apply a fixup to the object file. */
1878
1879void
1880md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
1881{
45f76423 1882 unsigned int subtype;
e23eba97 1883 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
45f76423 1884 bfd_boolean relaxable = FALSE;
c1b465c9 1885 offsetT loc;
a6cbf936 1886 segT sub_segment;
e23eba97
NC
1887
1888 /* Remember value for tc_gen_reloc. */
1889 fixP->fx_addnumber = *valP;
1890
1891 switch (fixP->fx_r_type)
1892 {
e23eba97
NC
1893 case BFD_RELOC_RISCV_HI20:
1894 case BFD_RELOC_RISCV_LO12_I:
1895 case BFD_RELOC_RISCV_LO12_S:
45f76423
AW
1896 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
1897 | bfd_getl32 (buf), buf);
a5ec5e3f
AW
1898 if (fixP->fx_addsy == NULL)
1899 fixP->fx_done = TRUE;
45f76423
AW
1900 relaxable = TRUE;
1901 break;
1902
1903 case BFD_RELOC_RISCV_GOT_HI20:
e23eba97
NC
1904 case BFD_RELOC_RISCV_ADD8:
1905 case BFD_RELOC_RISCV_ADD16:
1906 case BFD_RELOC_RISCV_ADD32:
1907 case BFD_RELOC_RISCV_ADD64:
45f76423 1908 case BFD_RELOC_RISCV_SUB6:
e23eba97
NC
1909 case BFD_RELOC_RISCV_SUB8:
1910 case BFD_RELOC_RISCV_SUB16:
1911 case BFD_RELOC_RISCV_SUB32:
1912 case BFD_RELOC_RISCV_SUB64:
45f76423
AW
1913 case BFD_RELOC_RISCV_RELAX:
1914 break;
1915
1916 case BFD_RELOC_RISCV_TPREL_HI20:
1917 case BFD_RELOC_RISCV_TPREL_LO12_I:
1918 case BFD_RELOC_RISCV_TPREL_LO12_S:
1919 case BFD_RELOC_RISCV_TPREL_ADD:
1920 relaxable = TRUE;
1921 /* Fall through. */
1922
1923 case BFD_RELOC_RISCV_TLS_GOT_HI20:
1924 case BFD_RELOC_RISCV_TLS_GD_HI20:
1925 case BFD_RELOC_RISCV_TLS_DTPREL32:
1926 case BFD_RELOC_RISCV_TLS_DTPREL64:
e294484e
AW
1927 if (fixP->fx_addsy != NULL)
1928 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1929 else
1930 as_bad_where (fixP->fx_file, fixP->fx_line,
1931 _("TLS relocation against a constant"));
e23eba97
NC
1932 break;
1933
e23eba97 1934 case BFD_RELOC_32:
a6cbf936
KLC
1935 /* Use pc-relative relocation for FDE initial location.
1936 The symbol address in .eh_frame may be adjusted in
1937 _bfd_elf_discard_section_eh_frame, and the content of
1938 .eh_frame will be adjusted in _bfd_elf_write_section_eh_frame.
1939 Therefore, we cannot insert a relocation whose addend symbol is
1940 in .eh_frame. Othrewise, the value may be adjusted twice.*/
1941 if (fixP->fx_addsy && fixP->fx_subsy
1942 && (sub_segment = S_GET_SEGMENT (fixP->fx_subsy))
1943 && strcmp (sub_segment->name, ".eh_frame") == 0
1944 && S_GET_VALUE (fixP->fx_subsy)
1945 == fixP->fx_frag->fr_address + fixP->fx_where)
1946 {
1947 fixP->fx_r_type = BFD_RELOC_RISCV_32_PCREL;
1948 fixP->fx_subsy = NULL;
1949 break;
1950 }
1951 /* Fall through. */
1952 case BFD_RELOC_64:
e23eba97
NC
1953 case BFD_RELOC_16:
1954 case BFD_RELOC_8:
45f76423 1955 case BFD_RELOC_RISCV_CFA:
e23eba97
NC
1956 if (fixP->fx_addsy && fixP->fx_subsy)
1957 {
1958 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
1959 fixP->fx_next->fx_addsy = fixP->fx_subsy;
1960 fixP->fx_next->fx_subsy = NULL;
1961 fixP->fx_next->fx_offset = 0;
1962 fixP->fx_subsy = NULL;
1963
1964 switch (fixP->fx_r_type)
1965 {
1966 case BFD_RELOC_64:
1967 fixP->fx_r_type = BFD_RELOC_RISCV_ADD64;
1968 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB64;
1969 break;
1970
1971 case BFD_RELOC_32:
1972 fixP->fx_r_type = BFD_RELOC_RISCV_ADD32;
1973 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
1974 break;
1975
1976 case BFD_RELOC_16:
1977 fixP->fx_r_type = BFD_RELOC_RISCV_ADD16;
1978 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
1979 break;
1980
1981 case BFD_RELOC_8:
1982 fixP->fx_r_type = BFD_RELOC_RISCV_ADD8;
1983 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
073808ed 1984 break;
e23eba97 1985
45f76423
AW
1986 case BFD_RELOC_RISCV_CFA:
1987 /* Load the byte to get the subtype. */
c1b465c9
KLC
1988 subtype = bfd_get_8 (NULL, &((fragS *) (fixP->fx_frag->fr_opcode))->fr_literal[fixP->fx_where]);
1989 loc = fixP->fx_frag->fr_fix - (subtype & 7);
45f76423
AW
1990 switch (subtype)
1991 {
1992 case DW_CFA_advance_loc1:
c1b465c9
KLC
1993 fixP->fx_where = loc + 1;
1994 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
1995 fixP->fx_r_type = BFD_RELOC_RISCV_SET8;
1996 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB8;
1997 break;
1998
1999 case DW_CFA_advance_loc2:
2000 fixP->fx_size = 2;
45f76423 2001 fixP->fx_next->fx_size = 2;
c1b465c9
KLC
2002 fixP->fx_where = loc + 1;
2003 fixP->fx_next->fx_where = loc + 1;
45f76423
AW
2004 fixP->fx_r_type = BFD_RELOC_RISCV_SET16;
2005 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB16;
2006 break;
2007
2008 case DW_CFA_advance_loc4:
2009 fixP->fx_size = 4;
45f76423 2010 fixP->fx_next->fx_size = 4;
c1b465c9
KLC
2011 fixP->fx_where = loc;
2012 fixP->fx_next->fx_where = loc;
45f76423
AW
2013 fixP->fx_r_type = BFD_RELOC_RISCV_SET32;
2014 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB32;
2015 break;
2016
2017 default:
2018 if (subtype < 0x80 && (subtype & 0x40))
2019 {
2020 /* DW_CFA_advance_loc */
2aece2ba
KLC
2021 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
2022 fixP->fx_next->fx_frag = fixP->fx_frag;
45f76423
AW
2023 fixP->fx_r_type = BFD_RELOC_RISCV_SET6;
2024 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_SUB6;
2025 }
2026 else
2027 as_fatal (_("internal error: bad CFA value #%d"), subtype);
2028 break;
2029 }
2030 break;
2031
e23eba97
NC
2032 default:
2033 /* This case is unreachable. */
2034 abort ();
2035 }
2036 }
2037 /* Fall through. */
2038
2039 case BFD_RELOC_RVA:
2040 /* If we are deleting this reloc entry, we must fill in the
2041 value now. This can happen if we have a .word which is not
2042 resolved when it appears but is later defined. */
2043 if (fixP->fx_addsy == NULL)
2044 {
2045 gas_assert (fixP->fx_size <= sizeof (valueT));
2046 md_number_to_chars ((char *) buf, *valP, fixP->fx_size);
2047 fixP->fx_done = 1;
2048 }
2049 break;
2050
2051 case BFD_RELOC_RISCV_JMP:
2052 if (fixP->fx_addsy)
2053 {
2054 /* Fill in a tentative value to improve objdump readability. */
2055 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2056 bfd_vma delta = target - md_pcrel_from (fixP);
2057 bfd_putl32 (bfd_getl32 (buf) | ENCODE_UJTYPE_IMM (delta), buf);
2058 }
2059 break;
2060
2061 case BFD_RELOC_12_PCREL:
2062 if (fixP->fx_addsy)
2063 {
2064 /* Fill in a tentative value to improve objdump readability. */
2065 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2066 bfd_vma delta = target - md_pcrel_from (fixP);
2067 bfd_putl32 (bfd_getl32 (buf) | ENCODE_SBTYPE_IMM (delta), buf);
2068 }
2069 break;
2070
2071 case BFD_RELOC_RISCV_RVC_BRANCH:
2072 if (fixP->fx_addsy)
2073 {
2074 /* Fill in a tentative value to improve objdump readability. */
2075 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2076 bfd_vma delta = target - md_pcrel_from (fixP);
2077 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_B_IMM (delta), buf);
2078 }
2079 break;
2080
2081 case BFD_RELOC_RISCV_RVC_JUMP:
2082 if (fixP->fx_addsy)
2083 {
2084 /* Fill in a tentative value to improve objdump readability. */
2085 bfd_vma target = S_GET_VALUE (fixP->fx_addsy) + *valP;
2086 bfd_vma delta = target - md_pcrel_from (fixP);
2087 bfd_putl16 (bfd_getl16 (buf) | ENCODE_RVC_J_IMM (delta), buf);
2088 }
2089 break;
2090
e23eba97
NC
2091 case BFD_RELOC_RISCV_CALL:
2092 case BFD_RELOC_RISCV_CALL_PLT:
45f76423
AW
2093 relaxable = TRUE;
2094 break;
2095
9d06997a 2096 case BFD_RELOC_RISCV_PCREL_HI20:
45f76423
AW
2097 case BFD_RELOC_RISCV_PCREL_LO12_S:
2098 case BFD_RELOC_RISCV_PCREL_LO12_I:
9d06997a
PD
2099 relaxable = riscv_opts.relax;
2100 break;
2101
e23eba97
NC
2102 case BFD_RELOC_RISCV_ALIGN:
2103 break;
2104
2105 default:
2106 /* We ignore generic BFD relocations we don't know about. */
2107 if (bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type) != NULL)
2108 as_fatal (_("internal error: bad relocation #%d"), fixP->fx_r_type);
2109 }
45f76423 2110
e294484e
AW
2111 if (fixP->fx_subsy != NULL)
2112 as_bad_where (fixP->fx_file, fixP->fx_line,
2113 _("unsupported symbol subtraction"));
2114
45f76423
AW
2115 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
2116 if (relaxable && fixP->fx_tcbit && fixP->fx_addsy != NULL)
2117 {
2118 fixP->fx_next = xmemdup (fixP, sizeof (*fixP), sizeof (*fixP));
2119 fixP->fx_next->fx_addsy = fixP->fx_next->fx_subsy = NULL;
2120 fixP->fx_next->fx_r_type = BFD_RELOC_RISCV_RELAX;
2121 }
2122}
2123
2124/* Because the value of .cfi_remember_state may changed after relaxation,
2125 we insert a fix to relocate it again in link-time. */
2126
2127void
2128riscv_pre_output_hook (void)
2129{
2130 const frchainS *frch;
2131 const asection *s;
2132
2133 for (s = stdoutput->sections; s; s = s->next)
2134 for (frch = seg_info (s)->frchainP; frch; frch = frch->frch_next)
2135 {
7cb7b948 2136 fragS *frag;
45f76423
AW
2137
2138 for (frag = frch->frch_root; frag; frag = frag->fr_next)
2139 {
2140 if (frag->fr_type == rs_cfa)
2141 {
45f76423
AW
2142 expressionS exp;
2143
2144 symbolS *add_symbol = frag->fr_symbol->sy_value.X_add_symbol;
2145 symbolS *op_symbol = frag->fr_symbol->sy_value.X_op_symbol;
2146
2147 exp.X_op = O_subtract;
2148 exp.X_add_symbol = add_symbol;
2149 exp.X_add_number = 0;
2150 exp.X_op_symbol = op_symbol;
2151
c1b465c9 2152 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
45f76423
AW
2153 BFD_RELOC_RISCV_CFA);
2154 }
2155 }
2156 }
e23eba97
NC
2157}
2158
45f76423 2159
e23eba97
NC
2160/* This structure is used to hold a stack of .option values. */
2161
2162struct riscv_option_stack
2163{
2164 struct riscv_option_stack *next;
2165 struct riscv_set_options options;
2166};
2167
2168static struct riscv_option_stack *riscv_opts_stack;
2169
2170/* Handle the .option pseudo-op. */
2171
2172static void
2173s_riscv_option (int x ATTRIBUTE_UNUSED)
2174{
2175 char *name = input_line_pointer, ch;
2176
2177 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2178 ++input_line_pointer;
2179 ch = *input_line_pointer;
2180 *input_line_pointer = '\0';
2181
2182 if (strcmp (name, "rvc") == 0)
2183 riscv_set_rvc (TRUE);
2184 else if (strcmp (name, "norvc") == 0)
2185 riscv_set_rvc (FALSE);
2186 else if (strcmp (name, "pic") == 0)
2187 riscv_opts.pic = TRUE;
2188 else if (strcmp (name, "nopic") == 0)
2189 riscv_opts.pic = FALSE;
45f76423
AW
2190 else if (strcmp (name, "relax") == 0)
2191 riscv_opts.relax = TRUE;
2192 else if (strcmp (name, "norelax") == 0)
2193 riscv_opts.relax = FALSE;
e23eba97
NC
2194 else if (strcmp (name, "push") == 0)
2195 {
2196 struct riscv_option_stack *s;
2197
2198 s = (struct riscv_option_stack *) xmalloc (sizeof *s);
2199 s->next = riscv_opts_stack;
2200 s->options = riscv_opts;
2201 riscv_opts_stack = s;
2202 }
2203 else if (strcmp (name, "pop") == 0)
2204 {
2205 struct riscv_option_stack *s;
2206
2207 s = riscv_opts_stack;
2208 if (s == NULL)
2209 as_bad (_(".option pop with no .option push"));
2210 else
2211 {
2212 riscv_opts = s->options;
2213 riscv_opts_stack = s->next;
2214 free (s);
2215 }
2216 }
2217 else
2218 {
2219 as_warn (_("Unrecognized .option directive: %s\n"), name);
2220 }
2221 *input_line_pointer = ch;
2222 demand_empty_rest_of_line ();
2223}
2224
2225/* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
2226 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
2227 use in DWARF debug information. */
2228
2229static void
2230s_dtprel (int bytes)
2231{
2232 expressionS ex;
2233 char *p;
2234
2235 expression (&ex);
2236
2237 if (ex.X_op != O_symbol)
2238 {
2239 as_bad (_("Unsupported use of %s"), (bytes == 8
2240 ? ".dtpreldword"
2241 : ".dtprelword"));
2242 ignore_rest_of_line ();
2243 }
2244
2245 p = frag_more (bytes);
2246 md_number_to_chars (p, 0, bytes);
2247 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE,
2248 (bytes == 8
2249 ? BFD_RELOC_RISCV_TLS_DTPREL64
2250 : BFD_RELOC_RISCV_TLS_DTPREL32));
2251
2252 demand_empty_rest_of_line ();
2253}
2254
2255/* Handle the .bss pseudo-op. */
2256
2257static void
2258s_bss (int ignore ATTRIBUTE_UNUSED)
2259{
2260 subseg_set (bss_section, 0);
2261 demand_empty_rest_of_line ();
2262}
2263
e23eba97 2264static void
d115ab8e 2265riscv_make_nops (char *buf, bfd_vma bytes)
e23eba97 2266{
d115ab8e 2267 bfd_vma i = 0;
e23eba97 2268
e5b737de
AW
2269 /* RISC-V instructions cannot begin or end on odd addresses, so this case
2270 means we are not within a valid instruction sequence. It is thus safe
2271 to use a zero byte, even though that is not a valid instruction. */
2272 if (bytes % 2 == 1)
2273 buf[i++] = 0;
2274
2275 /* Use at most one 2-byte NOP. */
2276 if ((bytes - i) % 4 == 2)
e23eba97 2277 {
e5b737de 2278 md_number_to_chars (buf + i, RVC_NOP, 2);
d115ab8e 2279 i += 2;
e23eba97
NC
2280 }
2281
e5b737de 2282 /* Fill the remainder with 4-byte NOPs. */
d115ab8e
AW
2283 for ( ; i < bytes; i += 4)
2284 md_number_to_chars (buf + i, RISCV_NOP, 4);
2285}
e23eba97 2286
d115ab8e
AW
2287/* Called from md_do_align. Used to create an alignment frag in a
2288 code section by emitting a worst-case NOP sequence that the linker
2289 will later relax to the correct number of NOPs. We can't compute
2290 the correct alignment now because of other linker relaxations. */
2291
2292bfd_boolean
2293riscv_frag_align_code (int n)
2294{
e5b737de 2295 bfd_vma bytes = (bfd_vma) 1 << n;
2c3f27ed 2296 bfd_vma worst_case_bytes = bytes - (riscv_opts.rvc ? 2 : 4);
ed0816bd
PD
2297 char *nops = frag_more (worst_case_bytes);
2298 expressionS ex;
d115ab8e
AW
2299
2300 /* When not relaxing, riscv_handle_align handles code alignment. */
2301 if (!riscv_opts.relax)
2302 return FALSE;
e23eba97 2303
ed0816bd
PD
2304 ex.X_op = O_constant;
2305 ex.X_add_number = worst_case_bytes;
d115ab8e 2306
ed0816bd 2307 riscv_make_nops (nops, worst_case_bytes);
e23eba97 2308
ed0816bd
PD
2309 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
2310 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
e23eba97 2311
d115ab8e
AW
2312 return TRUE;
2313}
e23eba97 2314
d115ab8e
AW
2315/* Implement HANDLE_ALIGN. */
2316
2317void
2318riscv_handle_align (fragS *fragP)
2319{
2320 switch (fragP->fr_type)
2321 {
2322 case rs_align_code:
2323 /* When relaxing, riscv_frag_align_code handles code alignment. */
2324 if (!riscv_opts.relax)
2325 {
2326 bfd_signed_vma count = fragP->fr_next->fr_address
2327 - fragP->fr_address - fragP->fr_fix;
2328
2329 if (count <= 0)
2330 break;
2331
2332 count &= MAX_MEM_FOR_RS_ALIGN_CODE;
2333 riscv_make_nops (fragP->fr_literal + fragP->fr_fix, count);
2334 fragP->fr_var = count;
2335 }
2336 break;
2337
2338 default:
2339 break;
2340 }
e23eba97
NC
2341}
2342
2343int
2344md_estimate_size_before_relax (fragS *fragp, asection *segtype)
2345{
2346 return (fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE));
2347}
2348
2349/* Translate internal representation of relocation info to BFD target
2350 format. */
2351
2352arelent *
2353tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
2354{
2355 arelent *reloc = (arelent *) xmalloc (sizeof (arelent));
2356
2357 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2358 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2359 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2360 reloc->addend = fixp->fx_addnumber;
2361
2362 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2363 if (reloc->howto == NULL)
2364 {
2365 if ((fixp->fx_r_type == BFD_RELOC_16 || fixp->fx_r_type == BFD_RELOC_8)
2366 && fixp->fx_addsy != NULL && fixp->fx_subsy != NULL)
2367 {
2368 /* We don't have R_RISCV_8/16, but for this special case,
2369 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
2370 return reloc;
2371 }
2372
2373 as_bad_where (fixp->fx_file, fixp->fx_line,
2374 _("cannot represent %s relocation in object file"),
2375 bfd_get_reloc_code_name (fixp->fx_r_type));
2376 return NULL;
2377 }
2378
2379 return reloc;
2380}
2381
2382int
2383riscv_relax_frag (asection *sec, fragS *fragp, long stretch ATTRIBUTE_UNUSED)
2384{
2385 if (RELAX_BRANCH_P (fragp->fr_subtype))
2386 {
2387 offsetT old_var = fragp->fr_var;
2388 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
2389 return fragp->fr_var - old_var;
2390 }
2391
2392 return 0;
2393}
2394
2395/* Expand far branches to multi-instruction sequences. */
2396
2397static void
2398md_convert_frag_branch (fragS *fragp)
2399{
2400 bfd_byte *buf;
2401 expressionS exp;
2402 fixS *fixp;
2403 insn_t insn;
2404 int rs1, reloc;
2405
2406 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
2407
2408 exp.X_op = O_symbol;
2409 exp.X_add_symbol = fragp->fr_symbol;
2410 exp.X_add_number = fragp->fr_offset;
2411
2412 gas_assert (fragp->fr_var == RELAX_BRANCH_LENGTH (fragp->fr_subtype));
2413
2414 if (RELAX_BRANCH_RVC (fragp->fr_subtype))
2415 {
2416 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2417 {
2418 case 8:
2419 case 4:
2420 /* Expand the RVC branch into a RISC-V one. */
2421 insn = bfd_getl16 (buf);
2422 rs1 = 8 + ((insn >> OP_SH_CRS1S) & OP_MASK_CRS1S);
2423 if ((insn & MASK_C_J) == MATCH_C_J)
2424 insn = MATCH_JAL;
2425 else if ((insn & MASK_C_JAL) == MATCH_C_JAL)
2426 insn = MATCH_JAL | (X_RA << OP_SH_RD);
2427 else if ((insn & MASK_C_BEQZ) == MATCH_C_BEQZ)
2428 insn = MATCH_BEQ | (rs1 << OP_SH_RS1);
2429 else if ((insn & MASK_C_BNEZ) == MATCH_C_BNEZ)
2430 insn = MATCH_BNE | (rs1 << OP_SH_RS1);
2431 else
2432 abort ();
2433 bfd_putl32 (insn, buf);
2434 break;
2435
2436 case 6:
2437 /* Invert the branch condition. Branch over the jump. */
2438 insn = bfd_getl16 (buf);
2439 insn ^= MATCH_C_BEQZ ^ MATCH_C_BNEZ;
2440 insn |= ENCODE_RVC_B_IMM (6);
2441 bfd_putl16 (insn, buf);
2442 buf += 2;
2443 goto jump;
2444
2445 case 2:
2446 /* Just keep the RVC branch. */
2447 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2448 ? BFD_RELOC_RISCV_RVC_JUMP : BFD_RELOC_RISCV_RVC_BRANCH;
2449 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2450 2, &exp, FALSE, reloc);
2451 buf += 2;
2452 goto done;
2453
2454 default:
1d65abb5 2455 abort ();
e23eba97
NC
2456 }
2457 }
2458
2459 switch (RELAX_BRANCH_LENGTH (fragp->fr_subtype))
2460 {
2461 case 8:
2462 gas_assert (!RELAX_BRANCH_UNCOND (fragp->fr_subtype));
2463
2464 /* Invert the branch condition. Branch over the jump. */
2465 insn = bfd_getl32 (buf);
2466 insn ^= MATCH_BEQ ^ MATCH_BNE;
2467 insn |= ENCODE_SBTYPE_IMM (8);
2468 md_number_to_chars ((char *) buf, insn, 4);
2469 buf += 4;
2470
2471jump:
2472 /* Jump to the target. */
2473 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2474 4, &exp, FALSE, BFD_RELOC_RISCV_JMP);
2475 md_number_to_chars ((char *) buf, MATCH_JAL, 4);
2476 buf += 4;
2477 break;
2478
2479 case 4:
2480 reloc = RELAX_BRANCH_UNCOND (fragp->fr_subtype)
2481 ? BFD_RELOC_RISCV_JMP : BFD_RELOC_12_PCREL;
2482 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
2483 4, &exp, FALSE, reloc);
2484 buf += 4;
2485 break;
2486
2487 default:
2488 abort ();
2489 }
2490
2491done:
2492 fixp->fx_file = fragp->fr_file;
2493 fixp->fx_line = fragp->fr_line;
2494
2495 gas_assert (buf == (bfd_byte *)fragp->fr_literal
2496 + fragp->fr_fix + fragp->fr_var);
2497
2498 fragp->fr_fix += fragp->fr_var;
2499}
2500
2501/* Relax a machine dependent frag. This returns the amount by which
2502 the current size of the frag should change. */
2503
2504void
2505md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec ATTRIBUTE_UNUSED,
2506 fragS *fragp)
2507{
2508 gas_assert (RELAX_BRANCH_P (fragp->fr_subtype));
2509 md_convert_frag_branch (fragp);
2510}
2511
2512void
2513md_show_usage (FILE *stream)
2514{
2515 fprintf (stream, _("\
2516RISC-V options:\n\
e23eba97
NC
2517 -fpic generate position-independent code\n\
2518 -fno-pic don't generate position-independent code (default)\n\
19683c04
PD
2519 -march=ISA set the RISC-V architecture\n\
2520 -mabi=ABI set the RISC-V ABI\n\
e23eba97
NC
2521"));
2522}
2523
2524/* Standard calling conventions leave the CFA at SP on entry. */
2525void
2526riscv_cfi_frame_initial_instructions (void)
2527{
2528 cfi_add_CFA_def_cfa_register (X_SP);
2529}
2530
2531int
2532tc_riscv_regname_to_dw2regnum (char *regname)
2533{
2534 int reg;
2535
2536 if ((reg = reg_lookup_internal (regname, RCLASS_GPR)) >= 0)
2537 return reg;
2538
2539 if ((reg = reg_lookup_internal (regname, RCLASS_FPR)) >= 0)
2540 return reg + 32;
2541
2542 as_bad (_("unknown register `%s'"), regname);
2543 return -1;
2544}
2545
2546void
2547riscv_elf_final_processing (void)
2548{
e23eba97 2549 elf_elfheader (stdoutput)->e_flags |= elf_flags;
e23eba97
NC
2550}
2551
2552/* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
2553 since these directives break relaxation when used with symbol deltas. */
2554
2555static void
2556s_riscv_leb128 (int sign)
2557{
2558 expressionS exp;
2559 char *save_in = input_line_pointer;
2560
2561 expression (&exp);
2562 if (exp.X_op != O_constant)
2563 as_bad (_("non-constant .%cleb128 is not supported"), sign ? 's' : 'u');
2564 demand_empty_rest_of_line ();
2565
2566 input_line_pointer = save_in;
2567 return s_leb128 (sign);
2568}
2569
2570/* Pseudo-op table. */
2571
2572static const pseudo_typeS riscv_pseudo_table[] =
2573{
2574 /* RISC-V-specific pseudo-ops. */
2575 {"option", s_riscv_option, 0},
2576 {"half", cons, 2},
2577 {"word", cons, 4},
2578 {"dword", cons, 8},
2579 {"dtprelword", s_dtprel, 4},
2580 {"dtpreldword", s_dtprel, 8},
2581 {"bss", s_bss, 0},
e23eba97
NC
2582 {"uleb128", s_riscv_leb128, 0},
2583 {"sleb128", s_riscv_leb128, 1},
2584
2585 { NULL, NULL, 0 },
2586};
2587
2588void
2589riscv_pop_insert (void)
2590{
2591 extern void pop_insert (const pseudo_typeS *);
2592
2593 pop_insert (riscv_pseudo_table);
2594}
This page took 0.183924 seconds and 4 git commands to generate.