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