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