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