Mention PR testsuite/25016 in ChangeLog entry
[deliverable/binutils-gdb.git] / gas / config / tc-riscv.c
1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2019 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
32 #include "bfd/elfxx-riscv.h"
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 #ifndef DEFAULT_RISCV_ATTR
63 #define DEFAULT_RISCV_ATTR 0
64 #endif
65
66 static const char default_arch[] = DEFAULT_ARCH;
67
68 static unsigned xlen = 0; /* width of an x-register */
69 static unsigned abi_xlen = 0; /* width of a pointer in the ABI */
70 static bfd_boolean rve_abi = FALSE;
71
72 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
73 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
74
75 static unsigned elf_flags = 0;
76
77 /* This is the set of options which the .option pseudo-op may modify. */
78
79 struct riscv_set_options
80 {
81 int pic; /* Generate position-independent code. */
82 int rvc; /* Generate RVC code. */
83 int rve; /* Generate RVE code. */
84 int relax; /* Emit relocs the linker is allowed to relax. */
85 int arch_attr; /* Emit arch attribute. */
86 };
87
88 static struct riscv_set_options riscv_opts =
89 {
90 0, /* pic */
91 0, /* rvc */
92 0, /* rve */
93 1, /* relax */
94 DEFAULT_RISCV_ATTR, /* arch_attr */
95 };
96
97 static void
98 riscv_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
106 static void
107 riscv_set_rve (bfd_boolean rve_value)
108 {
109 riscv_opts.rve = rve_value;
110 }
111
112 static riscv_subset_list_t riscv_subsets;
113
114 static bfd_boolean
115 riscv_subset_supports (const char *feature)
116 {
117 if (riscv_opts.rvc && (strcasecmp (feature, "c") == 0))
118 return TRUE;
119
120 return riscv_lookup_subset (&riscv_subsets, feature) != NULL;
121 }
122
123 static bfd_boolean
124 riscv_multi_subset_supports (enum riscv_insn_class insn_class)
125 {
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");
139
140 case INSN_CLASS_Q: return riscv_subset_supports ("q");
141
142 default:
143 as_fatal ("Unreachable");
144 return FALSE;
145 }
146 }
147
148 /* Set which ISA and extensions are available. */
149
150 static void
151 riscv_set_arch (const char *s)
152 {
153 riscv_parse_subset_t rps;
154 rps.subset_list = &riscv_subsets;
155 rps.error_handler = as_fatal;
156 rps.xlen = &xlen;
157
158 riscv_release_subset_list (&riscv_subsets);
159 riscv_parse_subset (&rps, s);
160 }
161
162 /* Handle of the OPCODE hash table. */
163 static struct hash_control *op_hash = NULL;
164
165 /* Handle of the type of .insn hash table. */
166 static struct hash_control *insn_type_hash = NULL;
167
168 /* This array holds the chars that always start a comment. If the
169 pre-processor is disabled, these aren't very useful */
170 const 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. */
179 const char line_comment_chars[] = "#";
180
181 /* This array holds machine specific line separator characters. */
182 const char line_separator_chars[] = ";";
183
184 /* Chars that can be used to separate mant from exp in floating point nums */
185 const 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 */
190 const char FLT_CHARS[] = "rRsSfFdDxXpP";
191
192 /* Indicate we are already assemble any instructions or not. */
193 static bfd_boolean start_assemble = FALSE;
194
195 /* Indicate arch attribute is explictly set. */
196 static bfd_boolean explicit_arch_attr = FALSE;
197
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
229 static char *expr_end;
230
231 /* The default target format to use. */
232
233 const char *
234 riscv_target_format (void)
235 {
236 return xlen == 64 ? "elf64-littleriscv" : "elf32-littleriscv";
237 }
238
239 /* Return the length of instruction INSN. */
240
241 static inline unsigned int
242 insn_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
249 static void
250 create_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
261 static void
262 install_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
271 static void
272 move_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
286 static void
287 add_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
293 static void
294 add_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
306 static unsigned
307 relaxed_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)
323 && !S_IS_WEAK (fragp->fr_symbol)
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
344 /* Information about an opcode name, mnemonics and its value. */
345 struct opcode_name_t
346 {
347 const char *name;
348 unsigned int val;
349 };
350
351 /* List for all supported opcode name. */
352 static 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. */
398 static struct hash_control *opcode_names_hash = NULL;
399
400 /* Initialization for hash table of opcode name. */
401 static void
402 init_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. */
419 static const struct opcode_name_t *
420 opcode_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
449 struct regname
450 {
451 const char *name;
452 unsigned int num;
453 };
454
455 enum reg_class
456 {
457 RCLASS_GPR,
458 RCLASS_FPR,
459 RCLASS_CSR,
460 RCLASS_MAX
461 };
462
463 static 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
470 static void
471 hash_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
480 static void
481 hash_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
489 static unsigned int
490 reg_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;
496
497 if (riscv_opts.rve && class == RCLASS_GPR && DECODE_REG_NUM (r) > 15)
498 return -1;
499
500 return DECODE_REG_NUM (r);
501 }
502
503 static bfd_boolean
504 reg_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
531 static bfd_boolean
532 arg_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
537 if (len == 0)
538 return FALSE;
539
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
553 operand list.
554
555 `length` could be 0, 4 or 8, 0 for auto detection. */
556 static bfd_boolean
557 validate_riscv_insn (const struct riscv_opcode *opc, int length)
558 {
559 const char *p = opc->args;
560 char c;
561 insn_t used_bits = opc->mask;
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);
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 {
586 case 'a': used_bits |= ENCODE_RVC_J_IMM (-1U); break;
587 case 'c': break; /* RS1, constrained to equal sp */
588 case 'i': used_bits |= ENCODE_RVC_SIMM3(-1U); break;
589 case 'j': used_bits |= ENCODE_RVC_IMM (-1U); break;
590 case 'o': used_bits |= ENCODE_RVC_IMM (-1U); break;
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;
596 case 's': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
597 case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
598 case 'u': used_bits |= ENCODE_RVC_IMM (-1U); break;
599 case 'v': used_bits |= ENCODE_RVC_IMM (-1U); break;
600 case 'w': break; /* RS1S, constrained to equal RD */
601 case 'x': break; /* RS2S, constrained to equal RD */
602 case 'z': break; /* RS2S, contrained to be x0 */
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;
607 case 'U': break; /* RS1, constrained to equal RD */
608 case 'V': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
609 case '<': used_bits |= ENCODE_RVC_IMM (-1U); break;
610 case '>': used_bits |= ENCODE_RVC_IMM (-1U); break;
611 case '8': used_bits |= ENCODE_RVC_UIMM8 (-1U); break;
612 case 'S': USE_BITS (OP_MASK_CRS1S, OP_SH_CRS1S); break;
613 case 'T': USE_BITS (OP_MASK_CRS2, OP_SH_CRS2); break;
614 case 'D': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
615 case 'F': /* funct */
616 switch (c = *p++)
617 {
618 case '6': USE_BITS (OP_MASK_CFUNCT6, OP_SH_CFUNCT6); break;
619 case '4': USE_BITS (OP_MASK_CFUNCT4, OP_SH_CFUNCT4); break;
620 case '3': USE_BITS (OP_MASK_CFUNCT3, OP_SH_CFUNCT3); break;
621 case '2': USE_BITS (OP_MASK_CFUNCT2, OP_SH_CFUNCT2); break;
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;
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;
653 case 'r': USE_BITS (OP_MASK_RS3, OP_SH_RS3); break;
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':
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;
662 case 'z': break;
663 case '[': break;
664 case ']': break;
665 case '0': break;
666 case '1': break;
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;
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
709 struct percent_op_match
710 {
711 const char *str;
712 bfd_reloc_code_real_type reloc;
713 };
714
715 /* Common hash table initialization function for
716 instruction and .insn directive. */
717 static struct hash_control *
718 init_opcode_hash (const struct riscv_opcode *opcodes,
719 bfd_boolean insn_directive_p)
720 {
721 int i = 0;
722 int length;
723 struct hash_control *hash = hash_new ();
724 while (opcodes[i].name)
725 {
726 const char *name = opcodes[i].name;
727 const char *hash_error =
728 hash_insert (hash, name, (void *) &opcodes[i]);
729
730 if (hash_error)
731 {
732 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
733 opcodes[i].name, hash_error);
734 /* Probably a memory allocation problem? Give up now. */
735 as_fatal (_("Broken assembler. No assembly attempted."));
736 }
737
738 do
739 {
740 if (opcodes[i].pinfo != INSN_MACRO)
741 {
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))
747 as_fatal (_("Broken assembler. No assembly attempted."));
748 }
749 else
750 gas_assert (!insn_directive_p);
751 ++i;
752 }
753 while (opcodes[i].name && !strcmp (opcodes[i].name, name));
754 }
755
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
762 void
763 md_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
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
779 /* Add "fp" as an alias for "s0". */
780 hash_reg_name (RCLASS_GPR, "fp", 8);
781
782 opcode_names_hash = hash_new ();
783 init_opcode_names_hash ();
784
785 #define DECLARE_CSR(name, num) hash_reg_name (RCLASS_CSR, #name, num);
786 #define DECLARE_CSR_ALIAS(name, num) DECLARE_CSR(name, num);
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
794 static insn_t
795 riscv_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
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
820 static void
821 append_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
830 gas_assert (address_expr);
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 }
843 else
844 {
845 howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
846 if (howto == NULL)
847 as_bad (_("Unsupported RISC-V relocation number %d"), reloc_type);
848
849 ip->fixp = fix_new_exp (ip->frag, ip->where,
850 bfd_get_reloc_size (howto),
851 address_expr, FALSE, reloc_type);
852
853 ip->fixp->fx_tcbit = riscv_opts.relax;
854 }
855 }
856
857 add_fixed_insn (ip);
858 install_insn (ip);
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 }
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
881 static void
882 macro_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
944 /* Build an instruction created by a macro expansion. Like md_assemble but
945 accept a printf-style format string and arguments. */
946
947 static void
948 md_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
967 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
968 unset. */
969 static void
970 normalize_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
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. */
982
983 static void
984 check_absolute_expr (struct riscv_cl_insn *ip, expressionS *ex,
985 bfd_boolean maybe_csr)
986 {
987 if (ex->X_op == O_big)
988 as_bad (_("unsupported large constant"));
989 else if (maybe_csr && ex->X_op == O_symbol)
990 as_bad (_("unknown CSR `%s'"),
991 S_GET_NAME (ex->X_add_symbol));
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
998 static symbolS *
999 make_internal_label (void)
1000 {
1001 return (symbolS *) local_symbol_make (FAKE_LABEL_NAME, now_seg,
1002 (valueT) frag_now_fix (), frag_now);
1003 }
1004
1005 /* Load an entry from the GOT. */
1006 static void
1007 pcrel_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
1021 static void
1022 pcrel_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
1029 static void
1030 pcrel_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. */
1038 static void
1039 riscv_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
1048 static void
1049 load_const (int reg, expressionS *ep)
1050 {
1051 int shift = RISCV_IMM_BITS;
1052 bfd_vma upper_imm;
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
1063 if (xlen > 32 && !IS_SEXT_32BIT_NUM (ep->X_add_number))
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;
1070 load_const (reg, &upper);
1071
1072 md_assemblef ("slli x%d, x%d, 0x%x", reg, reg, shift);
1073 if (lower.X_add_number != 0)
1074 md_assemblef ("addi x%d, x%d, %" BFD_VMA_FMT "d", reg, reg,
1075 lower.X_add_number);
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 {
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);
1088 hi_reg = reg;
1089 }
1090
1091 if (lower.X_add_number != 0 || hi_reg == 0)
1092 md_assemblef ("%s x%d, x%d, %" BFD_VMA_FMT "d", ADD32_INSN, reg, hi_reg,
1093 lower.X_add_number);
1094 }
1095 }
1096
1097 /* Expand RISC-V assembly macros into one or more instructions. */
1098 static void
1099 macro (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
1224 static 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
1234 static 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
1242 static 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
1250 static const struct percent_op_match percent_op_rtype[] =
1251 {
1252 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD},
1253 {0, 0}
1254 };
1255
1256 static const struct percent_op_match percent_op_null[] =
1257 {
1258 {0, 0}
1259 };
1260
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
1265 static bfd_boolean
1266 parse_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. */
1282 if (*reloc != BFD_RELOC_UNUSED
1283 && !bfd_reloc_type_lookup (stdoutput, *reloc))
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
1294 static void
1295 my_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
1312 static size_t
1313 my_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
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. */
1324 if (reg_lookup (&str, RCLASS_GPR, &regno))
1325 {
1326 ep->X_op = O_register;
1327 ep->X_add_number = regno;
1328 expr_end = str;
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
1370 /* Parse opcode name, could be an mnemonics or number. */
1371 static size_t
1372 my_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
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
1391 static bfd_boolean
1392 riscv_handle_implicit_zero_offset (expressionS *ep, const char *s)
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 {
1398 ep->X_op = O_constant;
1399 ep->X_add_number = 0;
1400 return TRUE;
1401 }
1402
1403 return FALSE;
1404 }
1405
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
1410 static const char *
1411 riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
1412 bfd_reloc_code_real_type *imm_reloc, struct hash_control *hash)
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
1435 insn = (struct riscv_opcode *) hash_find (hash, str);
1436
1437 argsStart = s;
1438 for ( ; insn && insn->name && strcmp (insn->name, str) == 0; insn++)
1439 {
1440 if ((insn->xlen_requirement != 0) && (xlen != insn->xlen_requirement))
1441 continue;
1442
1443 if (!riscv_multi_subset_supports (insn->insn_class))
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;
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)
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;
1517 case 'z': /* RS2, contrained to equal x0. */
1518 if (!reg_lookup (&s, RCLASS_GPR, &regno)
1519 || regno != 0)
1520 break;
1521 continue;
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);
1529 rvc_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;
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;
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':
1568 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1569 continue;
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':
1577 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1578 continue;
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':
1586 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1587 continue;
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':
1596 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1597 continue;
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;
1605 case 'o':
1606 if (my_getSmallExpression (imm_expr, imm_reloc, s, p)
1607 || imm_expr->X_op != O_constant
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'. */
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;
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':
1634 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1635 continue;
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':
1644 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1645 continue;
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;
1657 rvc_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;
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;
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;
1697 case 'F':
1698 switch (*++args)
1699 {
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;
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;
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;
1758 default:
1759 as_bad (_("bad compressed FUNCT field"
1760 " specifier 'CF%c'\n"),
1761 *args);
1762 }
1763 break;
1764
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);
1787 check_absolute_expr (ip, imm_expr, FALSE);
1788 if ((unsigned long) imm_expr->X_add_number > 31)
1789 as_bad (_("Improper shift amount (%lu)"),
1790 (unsigned long) imm_expr->X_add_number);
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);
1798 check_absolute_expr (ip, imm_expr, FALSE);
1799 if ((unsigned long) imm_expr->X_add_number >= xlen)
1800 as_bad (_("Improper shift amount (%lu)"),
1801 (unsigned long) imm_expr->X_add_number);
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);
1809 check_absolute_expr (ip, imm_expr, FALSE);
1810 if ((unsigned long) imm_expr->X_add_number > 31)
1811 as_bad (_("Improper CSRxI immediate (%lu)"),
1812 (unsigned long) imm_expr->X_add_number);
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);
1824 check_absolute_expr (ip, imm_expr, TRUE);
1825 if ((unsigned long) imm_expr->X_add_number > 0xfff)
1826 as_bad (_("Improper CSR address (%lu)"),
1827 (unsigned long) imm_expr->X_add_number);
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. */
1858 case 'r': /* rs3. */
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;
1878 case 'r':
1879 INSERT_OPERAND (RS3, *ip, regno);
1880 break;
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
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
1949 case 'j': /* Sign-extended immediate. */
1950 p = percent_op_itype;
1951 *imm_reloc = BFD_RELOC_RISCV_LO12_I;
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;
1961 case '1': /* 4-operand add, must be %tprel_add. */
1962 p = percent_op_rtype;
1963 goto alu_op;
1964 case '0': /* AMO "displacement," which must be zero. */
1965 p = percent_op_null;
1966 load_store:
1967 if (riscv_handle_implicit_zero_offset (imm_expr, s))
1968 continue;
1969 alu_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)
1978 || (*args == '1')
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. */
1988 branch:
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;
1996 if (!my_getSmallExpression (imm_expr, imm_reloc, s, p))
1997 {
1998 if (imm_expr->X_op != O_constant)
1999 break;
2000
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. */
2012 jump:
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;
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;
2122
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
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
2141 out:
2142 /* Restore the character we might have clobbered above. */
2143 if (save_c)
2144 *(argsStart - 1) = save_c;
2145
2146 return error;
2147 }
2148
2149 void
2150 md_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
2156 const char *error = riscv_ip (str, &insn, &imm_expr, &imm_reloc, op_hash);
2157
2158 start_assemble = TRUE;
2159
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
2172 const char *
2173 md_atof (int type, char *litP, int *sizeP)
2174 {
2175 return ieee_md_atof (type, litP, sizeP, TARGET_BYTES_BIG_ENDIAN);
2176 }
2177
2178 void
2179 md_number_to_chars (char *buf, valueT val, int n)
2180 {
2181 number_to_chars_littleendian (buf, val, n);
2182 }
2183
2184 const char *md_shortopts = "O::g::G:";
2185
2186 enum options
2187 {
2188 OPTION_MARCH = OPTION_MD_BASE,
2189 OPTION_PIC,
2190 OPTION_NO_PIC,
2191 OPTION_MABI,
2192 OPTION_RELAX,
2193 OPTION_NO_RELAX,
2194 OPTION_ARCH_ATTR,
2195 OPTION_NO_ARCH_ATTR,
2196 OPTION_END_OF_ENUM
2197 };
2198
2199 struct option md_longopts[] =
2200 {
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},
2205 {"mabi", required_argument, NULL, OPTION_MABI},
2206 {"mrelax", no_argument, NULL, OPTION_RELAX},
2207 {"mno-relax", no_argument, NULL, OPTION_NO_RELAX},
2208 {"march-attr", no_argument, NULL, OPTION_ARCH_ATTR},
2209 {"mno-arch-attr", no_argument, NULL, OPTION_NO_ARCH_ATTR},
2210
2211 {NULL, no_argument, NULL, 0}
2212 };
2213 size_t md_longopts_size = sizeof (md_longopts);
2214
2215 enum float_abi {
2216 FLOAT_ABI_DEFAULT = -1,
2217 FLOAT_ABI_SOFT,
2218 FLOAT_ABI_SINGLE,
2219 FLOAT_ABI_DOUBLE,
2220 FLOAT_ABI_QUAD
2221 };
2222 static enum float_abi float_abi = FLOAT_ABI_DEFAULT;
2223
2224 static void
2225 riscv_set_abi (unsigned new_xlen, enum float_abi new_float_abi, bfd_boolean rve)
2226 {
2227 abi_xlen = new_xlen;
2228 float_abi = new_float_abi;
2229 rve_abi = rve;
2230 }
2231
2232 int
2233 md_parse_option (int c, const char *arg)
2234 {
2235 switch (c)
2236 {
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
2249 case OPTION_MABI:
2250 if (strcmp (arg, "ilp32") == 0)
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);
2254 else if (strcmp (arg, "ilp32f") == 0)
2255 riscv_set_abi (32, FLOAT_ABI_SINGLE, FALSE);
2256 else if (strcmp (arg, "ilp32d") == 0)
2257 riscv_set_abi (32, FLOAT_ABI_DOUBLE, FALSE);
2258 else if (strcmp (arg, "ilp32q") == 0)
2259 riscv_set_abi (32, FLOAT_ABI_QUAD, FALSE);
2260 else if (strcmp (arg, "lp64") == 0)
2261 riscv_set_abi (64, FLOAT_ABI_SOFT, FALSE);
2262 else if (strcmp (arg, "lp64f") == 0)
2263 riscv_set_abi (64, FLOAT_ABI_SINGLE, FALSE);
2264 else if (strcmp (arg, "lp64d") == 0)
2265 riscv_set_abi (64, FLOAT_ABI_DOUBLE, FALSE);
2266 else if (strcmp (arg, "lp64q") == 0)
2267 riscv_set_abi (64, FLOAT_ABI_QUAD, FALSE);
2268 else
2269 return 0;
2270 break;
2271
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
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
2288 default:
2289 return 0;
2290 }
2291
2292 return 1;
2293 }
2294
2295 void
2296 riscv_after_parse_args (void)
2297 {
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 }
2307
2308 if (riscv_subsets.head == NULL)
2309 riscv_set_arch (xlen == 64 ? "rv64g" : "rv32g");
2310
2311 /* Add the RVC extension, regardless of -march, to support .option rvc. */
2312 riscv_set_rvc (FALSE);
2313 if (riscv_subset_supports ("c"))
2314 riscv_set_rvc (TRUE);
2315
2316 /* Enable RVE if specified by the -march option. */
2317 riscv_set_rve (FALSE);
2318 if (riscv_subset_supports ("e"))
2319 riscv_set_rve (TRUE);
2320
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 {
2331 riscv_subset_t *subset;
2332
2333 /* Assume soft-float unless D extension is present. */
2334 float_abi = FLOAT_ABI_SOFT;
2335
2336 for (subset = riscv_subsets.head; subset != NULL; subset = subset->next)
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 }
2343 }
2344
2345 if (rve_abi)
2346 elf_flags |= EF_RISCV_RVE;
2347
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));
2350 }
2351
2352 long
2353 md_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
2360 void
2361 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2362 {
2363 unsigned int subtype;
2364 bfd_byte *buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
2365 bfd_boolean relaxable = FALSE;
2366 offsetT loc;
2367 segT sub_segment;
2368
2369 /* Remember value for tc_gen_reloc. */
2370 fixP->fx_addnumber = *valP;
2371
2372 switch (fixP->fx_r_type)
2373 {
2374 case BFD_RELOC_RISCV_HI20:
2375 case BFD_RELOC_RISCV_LO12_I:
2376 case BFD_RELOC_RISCV_LO12_S:
2377 bfd_putl32 (riscv_apply_const_reloc (fixP->fx_r_type, *valP)
2378 | bfd_getl32 (buf), buf);
2379 if (fixP->fx_addsy == NULL)
2380 fixP->fx_done = TRUE;
2381 relaxable = TRUE;
2382 break;
2383
2384 case BFD_RELOC_RISCV_GOT_HI20:
2385 case BFD_RELOC_RISCV_ADD8:
2386 case BFD_RELOC_RISCV_ADD16:
2387 case BFD_RELOC_RISCV_ADD32:
2388 case BFD_RELOC_RISCV_ADD64:
2389 case BFD_RELOC_RISCV_SUB6:
2390 case BFD_RELOC_RISCV_SUB8:
2391 case BFD_RELOC_RISCV_SUB16:
2392 case BFD_RELOC_RISCV_SUB32:
2393 case BFD_RELOC_RISCV_SUB64:
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:
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"));
2413 break;
2414
2415 case BFD_RELOC_32:
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:
2434 case BFD_RELOC_16:
2435 case BFD_RELOC_8:
2436 case BFD_RELOC_RISCV_CFA:
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;
2465 break;
2466
2467 case BFD_RELOC_RISCV_CFA:
2468 /* Load the byte to get the subtype. */
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);
2471 switch (subtype)
2472 {
2473 case DW_CFA_advance_loc1:
2474 fixP->fx_where = loc + 1;
2475 fixP->fx_next->fx_where = loc + 1;
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;
2482 fixP->fx_next->fx_size = 2;
2483 fixP->fx_where = loc + 1;
2484 fixP->fx_next->fx_where = loc + 1;
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;
2491 fixP->fx_next->fx_size = 4;
2492 fixP->fx_where = loc;
2493 fixP->fx_next->fx_where = loc;
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 */
2502 fixP->fx_frag = (fragS *) fixP->fx_frag->fr_opcode;
2503 fixP->fx_next->fx_frag = fixP->fx_frag;
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
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
2572 case BFD_RELOC_RISCV_CALL:
2573 case BFD_RELOC_RISCV_CALL_PLT:
2574 relaxable = TRUE;
2575 break;
2576
2577 case BFD_RELOC_RISCV_PCREL_HI20:
2578 case BFD_RELOC_RISCV_PCREL_LO12_S:
2579 case BFD_RELOC_RISCV_PCREL_LO12_I:
2580 relaxable = riscv_opts.relax;
2581 break;
2582
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 }
2591
2592 if (fixP->fx_subsy != NULL)
2593 as_bad_where (fixP->fx_file, fixP->fx_line,
2594 _("unsupported symbol subtraction"));
2595
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
2608 void
2609 riscv_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 {
2617 fragS *frag;
2618
2619 for (frag = frch->frch_root; frag; frag = frag->fr_next)
2620 {
2621 if (frag->fr_type == rs_cfa)
2622 {
2623 expressionS exp;
2624 expressionS *symval;
2625
2626 symval = symbol_get_value_expression (frag->fr_symbol);
2627 exp.X_op = O_subtract;
2628 exp.X_add_symbol = symval->X_add_symbol;
2629 exp.X_add_number = 0;
2630 exp.X_op_symbol = symval->X_op_symbol;
2631
2632 fix_new_exp (frag, (int) frag->fr_offset, 1, &exp, 0,
2633 BFD_RELOC_RISCV_CFA);
2634 }
2635 }
2636 }
2637 }
2638
2639
2640 /* This structure is used to hold a stack of .option values. */
2641
2642 struct riscv_option_stack
2643 {
2644 struct riscv_option_stack *next;
2645 struct riscv_set_options options;
2646 };
2647
2648 static struct riscv_option_stack *riscv_opts_stack;
2649
2650 /* Handle the .option pseudo-op. */
2651
2652 static void
2653 s_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;
2670 else if (strcmp (name, "relax") == 0)
2671 riscv_opts.relax = TRUE;
2672 else if (strcmp (name, "norelax") == 0)
2673 riscv_opts.relax = FALSE;
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
2709 static void
2710 s_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
2737 static void
2738 s_bss (int ignore ATTRIBUTE_UNUSED)
2739 {
2740 subseg_set (bss_section, 0);
2741 demand_empty_rest_of_line ();
2742 }
2743
2744 static void
2745 riscv_make_nops (char *buf, bfd_vma bytes)
2746 {
2747 bfd_vma i = 0;
2748
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)
2757 {
2758 md_number_to_chars (buf + i, RVC_NOP, 2);
2759 i += 2;
2760 }
2761
2762 /* Fill the remainder with 4-byte NOPs. */
2763 for ( ; i < bytes; i += 4)
2764 md_number_to_chars (buf + i, RISCV_NOP, 4);
2765 }
2766
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
2772 bfd_boolean
2773 riscv_frag_align_code (int n)
2774 {
2775 bfd_vma bytes = (bfd_vma) 1 << n;
2776 bfd_vma insn_alignment = riscv_opts.rvc ? 2 : 4;
2777 bfd_vma worst_case_bytes = bytes - insn_alignment;
2778 char *nops;
2779 expressionS ex;
2780
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
2786 /* When not relaxing, riscv_handle_align handles code alignment. */
2787 if (!riscv_opts.relax)
2788 return FALSE;
2789
2790 nops = frag_more (worst_case_bytes);
2791
2792 ex.X_op = O_constant;
2793 ex.X_add_number = worst_case_bytes;
2794
2795 riscv_make_nops (nops, worst_case_bytes);
2796
2797 fix_new_exp (frag_now, nops - frag_now->fr_literal, 0,
2798 &ex, FALSE, BFD_RELOC_RISCV_ALIGN);
2799
2800 return TRUE;
2801 }
2802
2803 /* Implement HANDLE_ALIGN. */
2804
2805 void
2806 riscv_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 {
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)
2822 break;
2823
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;
2835 }
2836 break;
2837
2838 default:
2839 break;
2840 }
2841 }
2842
2843 int
2844 md_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
2852 arelent *
2853 tc_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
2882 int
2883 riscv_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
2897 static void
2898 md_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:
2955 abort ();
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
2971 jump:
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
2991 done:
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
3004 void
3005 md_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
3012 void
3013 md_show_usage (FILE *stream)
3014 {
3015 fprintf (stream, _("\
3016 RISC-V options:\n\
3017 -fpic generate position-independent code\n\
3018 -fno-pic don't generate position-independent code (default)\n\
3019 -march=ISA set the RISC-V architecture\n\
3020 -mabi=ABI set the RISC-V ABI\n\
3021 -mrelax enable relax (default)\n\
3022 -mno-relax disable relax\n\
3023 -march-attr generate RISC-V arch attribute\n\
3024 -mno-arch-attr don't generate RISC-V arch attribute\n\
3025 "));
3026 }
3027
3028 /* Standard calling conventions leave the CFA at SP on entry. */
3029 void
3030 riscv_cfi_frame_initial_instructions (void)
3031 {
3032 cfi_add_CFA_def_cfa_register (X_SP);
3033 }
3034
3035 int
3036 tc_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
3050 void
3051 riscv_elf_final_processing (void)
3052 {
3053 elf_elfheader (stdoutput)->e_flags |= elf_flags;
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
3059 static void
3060 s_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
3074 /* Parse the .insn directive. */
3075
3076 static void
3077 s_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
3108 /* Update arch attributes. */
3109
3110 static void
3111 riscv_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
3122 static void
3123 riscv_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
3132 void
3133 riscv_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
3141 int
3142 riscv_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
3177 static void
3178 s_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
3206 /* Pseudo-op table. */
3207
3208 static 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},
3218 {"uleb128", s_riscv_leb128, 0},
3219 {"sleb128", s_riscv_leb128, 1},
3220 {"insn", s_riscv_insn, 0},
3221 {"attribute", s_riscv_attribute, 0},
3222
3223 { NULL, NULL, 0 },
3224 };
3225
3226 void
3227 riscv_pop_insert (void)
3228 {
3229 extern void pop_insert (const pseudo_typeS *);
3230
3231 pop_insert (riscv_pseudo_table);
3232 }
This page took 0.104057 seconds and 4 git commands to generate.