1 /* tc-riscv.c -- RISC-V assembler
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
7 This file is part of GAS.
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)
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.
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/>. */
26 #include "safe-ctype.h"
29 #include "dwarf2dbg.h"
30 #include "dw2gencfi.h"
31 #include "struc-symbol.h"
33 #include "elf/riscv.h"
34 #include "opcode/riscv.h"
38 /* Information about an instruction, including its format, operands
42 /* The opcode's entry in riscv_opcodes. */
43 const struct riscv_opcode
*insn_mo
;
45 /* The encoded instruction bits. */
48 /* The frag that contains the instruction. */
51 /* The offset into FRAG of the first instruction byte. */
54 /* The relocs associated with the instruction, if any. */
59 #define DEFAULT_ARCH "riscv64"
62 static const char default_arch
[] = DEFAULT_ARCH
;
64 static unsigned xlen
= 0; /* width of an x-register */
65 static unsigned abi_xlen
= 0; /* width of a pointer in the ABI */
67 #define LOAD_ADDRESS_INSN (abi_xlen == 64 ? "ld" : "lw")
68 #define ADD32_INSN (xlen == 64 ? "addiw" : "addi")
70 static unsigned elf_flags
= 0;
72 /* This is the set of options which the .option pseudo-op may modify. */
74 struct riscv_set_options
76 int pic
; /* Generate position-independent code. */
77 int rvc
; /* Generate RVC code. */
78 int relax
; /* Emit relocs the linker is allowed to relax. */
81 static struct riscv_set_options riscv_opts
=
89 riscv_set_rvc (bfd_boolean rvc_value
)
92 elf_flags
|= EF_RISCV_RVC
;
94 riscv_opts
.rvc
= rvc_value
;
101 struct riscv_subset
*next
;
104 static struct riscv_subset
*riscv_subsets
;
107 riscv_subset_supports (const char *feature
)
109 struct riscv_subset
*s
;
111 unsigned xlen_required
= strtoul (feature
, &p
, 10);
113 if (xlen_required
&& xlen
!= xlen_required
)
116 for (s
= riscv_subsets
; s
!= NULL
; s
= s
->next
)
117 if (strcasecmp (s
->name
, p
) == 0)
124 riscv_add_subset (const char *subset
)
126 struct riscv_subset
*s
= xmalloc (sizeof *s
);
128 s
->name
= xstrdup (subset
);
129 s
->next
= riscv_subsets
;
133 /* Set which ISA and extensions are available. */
136 riscv_set_arch (const char *s
)
138 const char *all_subsets
= "imafdc";
139 const char *extension
= NULL
;
142 if (strncmp (p
, "rv32", 4) == 0)
147 else if (strncmp (p
, "rv64", 4) == 0)
153 as_fatal ("-march=%s: ISA string must begin with rv32 or rv64", s
);
162 for ( ; *all_subsets
!= 'c'; all_subsets
++)
164 const char subset
[] = {*all_subsets
, '\0'};
165 riscv_add_subset (subset
);
170 as_fatal ("-march=%s: first ISA subset must be `i' or `g'", s
);
177 char *subset
= xstrdup (p
), *q
= subset
;
179 while (*++q
!= '\0' && *q
!= '_')
184 as_fatal ("-march=%s: only one non-standard extension is supported"
185 " (found `%s' and `%s')", s
, extension
, subset
);
187 riscv_add_subset (subset
);
188 p
+= strlen (subset
);
193 else if ((all_subsets
= strchr (all_subsets
, *p
)) != NULL
)
195 const char subset
[] = {*p
, 0};
196 riscv_add_subset (subset
);
202 const char subset
[] = {*p
, 0};
203 riscv_add_subset (subset
);
207 as_fatal ("-march=%s: unsupported ISA subset `%c'", s
, *p
);
211 /* Handle of the OPCODE hash table. */
212 static struct hash_control
*op_hash
= NULL
;
214 /* This array holds the chars that always start a comment. If the
215 pre-processor is disabled, these aren't very useful */
216 const char comment_chars
[] = "#";
218 /* This array holds the chars that only start a comment at the beginning of
219 a line. If the line seems to have the form '# 123 filename'
220 .line and .file directives will appear in the pre-processed output */
221 /* Note that input_file.c hand checks for '#' at the beginning of the
222 first line of the input file. This is because the compiler outputs
223 #NO_APP at the beginning of its output. */
224 /* Also note that C style comments are always supported. */
225 const char line_comment_chars
[] = "#";
227 /* This array holds machine specific line separator characters. */
228 const char line_separator_chars
[] = ";";
230 /* Chars that can be used to separate mant from exp in floating point nums */
231 const char EXP_CHARS
[] = "eE";
233 /* Chars that mean this number is a floating point constant */
236 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
238 /* Macros for encoding relaxation state for RVC branches and far jumps. */
239 #define RELAX_BRANCH_ENCODE(uncond, rvc, length) \
242 | ((uncond) ? 1 : 0) \
245 #define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
246 #define RELAX_BRANCH_LENGTH(i) (((i) >> 2) & 0xF)
247 #define RELAX_BRANCH_RVC(i) (((i) & 2) != 0)
248 #define RELAX_BRANCH_UNCOND(i) (((i) & 1) != 0)
250 /* Is the given value a sign-extended 32-bit value? */
251 #define IS_SEXT_32BIT_NUM(x) \
252 (((x) &~ (offsetT) 0x7fffffff) == 0 \
253 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
255 /* Is the given value a zero-extended 32-bit value? Or a negated one? */
256 #define IS_ZEXT_32BIT_NUM(x) \
257 (((x) &~ (offsetT) 0xffffffff) == 0 \
258 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
260 /* Change INSN's opcode so that the operand given by FIELD has value VALUE.
261 INSN is a riscv_cl_insn structure and VALUE is evaluated exactly once. */
262 #define INSERT_OPERAND(FIELD, INSN, VALUE) \
263 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
265 /* Determine if an instruction matches an opcode. */
266 #define OPCODE_MATCHES(OPCODE, OP) \
267 (((OPCODE) & MASK_##OP) == MATCH_##OP)
269 static char *expr_end
;
271 /* The default target format to use. */
274 riscv_target_format (void)
276 return xlen
== 64 ? "elf64-littleriscv" : "elf32-littleriscv";
279 /* Return the length of instruction INSN. */
281 static inline unsigned int
282 insn_length (const struct riscv_cl_insn
*insn
)
284 return riscv_insn_length (insn
->insn_opcode
);
287 /* Initialise INSN from opcode entry MO. Leave its position unspecified. */
290 create_insn (struct riscv_cl_insn
*insn
, const struct riscv_opcode
*mo
)
293 insn
->insn_opcode
= mo
->match
;
299 /* Install INSN at the location specified by its "frag" and "where" fields. */
302 install_insn (const struct riscv_cl_insn
*insn
)
304 char *f
= insn
->frag
->fr_literal
+ insn
->where
;
305 md_number_to_chars (f
, insn
->insn_opcode
, insn_length (insn
));
308 /* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
309 and install the opcode in the new location. */
312 move_insn (struct riscv_cl_insn
*insn
, fragS
*frag
, long where
)
316 if (insn
->fixp
!= NULL
)
318 insn
->fixp
->fx_frag
= frag
;
319 insn
->fixp
->fx_where
= where
;
324 /* Add INSN to the end of the output. */
327 add_fixed_insn (struct riscv_cl_insn
*insn
)
329 char *f
= frag_more (insn_length (insn
));
330 move_insn (insn
, frag_now
, f
- frag_now
->fr_literal
);
334 add_relaxed_insn (struct riscv_cl_insn
*insn
, int max_chars
, int var
,
335 relax_substateT subtype
, symbolS
*symbol
, offsetT offset
)
337 frag_grow (max_chars
);
338 move_insn (insn
, frag_now
, frag_more (0) - frag_now
->fr_literal
);
339 frag_var (rs_machine_dependent
, max_chars
, var
,
340 subtype
, symbol
, offset
, NULL
);
343 /* Compute the length of a branch sequence, and adjust the stored length
344 accordingly. If FRAGP is NULL, the worst-case length is returned. */
347 relaxed_branch_length (fragS
*fragp
, asection
*sec
, int update
)
349 int jump
, rvc
, length
= 8;
354 jump
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
);
355 rvc
= RELAX_BRANCH_RVC (fragp
->fr_subtype
);
356 length
= RELAX_BRANCH_LENGTH (fragp
->fr_subtype
);
358 /* Assume jumps are in range; the linker will catch any that aren't. */
359 length
= jump
? 4 : 8;
361 if (fragp
->fr_symbol
!= NULL
362 && S_IS_DEFINED (fragp
->fr_symbol
)
363 && !S_IS_WEAK (fragp
->fr_symbol
)
364 && sec
== S_GET_SEGMENT (fragp
->fr_symbol
))
366 offsetT val
= S_GET_VALUE (fragp
->fr_symbol
) + fragp
->fr_offset
;
367 bfd_vma rvc_range
= jump
? RVC_JUMP_REACH
: RVC_BRANCH_REACH
;
368 val
-= fragp
->fr_address
+ fragp
->fr_fix
;
370 if (rvc
&& (bfd_vma
)(val
+ rvc_range
/2) < rvc_range
)
372 else if ((bfd_vma
)(val
+ RISCV_BRANCH_REACH
/2) < RISCV_BRANCH_REACH
)
374 else if (!jump
&& rvc
)
379 fragp
->fr_subtype
= RELAX_BRANCH_ENCODE (jump
, rvc
, length
);
398 static struct hash_control
*reg_names_hash
= NULL
;
400 #define ENCODE_REG_HASH(cls, n) \
401 ((void *)(uintptr_t)((n) * RCLASS_MAX + (cls) + 1))
402 #define DECODE_REG_CLASS(hash) (((uintptr_t)(hash) - 1) % RCLASS_MAX)
403 #define DECODE_REG_NUM(hash) (((uintptr_t)(hash) - 1) / RCLASS_MAX)
406 hash_reg_name (enum reg_class
class, const char *name
, unsigned n
)
408 void *hash
= ENCODE_REG_HASH (class, n
);
409 const char *retval
= hash_insert (reg_names_hash
, name
, hash
);
412 as_fatal (_("internal error: can't hash `%s': %s"), name
, retval
);
416 hash_reg_names (enum reg_class
class, const char * const names
[], unsigned n
)
420 for (i
= 0; i
< n
; i
++)
421 hash_reg_name (class, names
[i
], i
);
425 reg_lookup_internal (const char *s
, enum reg_class
class)
427 struct regname
*r
= (struct regname
*) hash_find (reg_names_hash
, s
);
429 if (r
== NULL
|| DECODE_REG_CLASS (r
) != class)
431 return DECODE_REG_NUM (r
);
435 reg_lookup (char **s
, enum reg_class
class, unsigned int *regnop
)
441 /* Find end of name. */
443 if (is_name_beginner (*e
))
445 while (is_part_of_name (*e
))
448 /* Terminate name. */
452 /* Look for the register. Advance to next token if one was recognized. */
453 if ((reg
= reg_lookup_internal (*s
, class)) >= 0)
463 arg_lookup (char **s
, const char *const *array
, size_t size
, unsigned *regnop
)
465 const char *p
= strchr (*s
, ',');
466 size_t i
, len
= p
? (size_t)(p
- *s
) : strlen (*s
);
468 for (i
= 0; i
< size
; i
++)
469 if (array
[i
] != NULL
&& strncmp (array
[i
], *s
, len
) == 0)
479 /* For consistency checking, verify that all bits are specified either
480 by the match/mask part of the instruction definition, or by the
483 validate_riscv_insn (const struct riscv_opcode
*opc
)
485 const char *p
= opc
->args
;
487 insn_t used_bits
= opc
->mask
;
488 int insn_width
= 8 * riscv_insn_length (opc
->match
);
489 insn_t required_bits
= ~0ULL >> (64 - insn_width
);
491 if ((used_bits
& opc
->match
) != (opc
->match
& required_bits
))
493 as_bad (_("internal: bad RISC-V opcode (mask error): %s %s"),
494 opc
->name
, opc
->args
);
498 #define USE_BITS(mask,shift) (used_bits |= ((insn_t)(mask) << (shift)))
505 case 'a': used_bits
|= ENCODE_RVC_J_IMM (-1U); break;
506 case 'c': break; /* RS1, constrained to equal sp */
507 case 'i': used_bits
|= ENCODE_RVC_SIMM3(-1U); break;
508 case 'j': used_bits
|= ENCODE_RVC_IMM (-1U); break;
509 case 'k': used_bits
|= ENCODE_RVC_LW_IMM (-1U); break;
510 case 'l': used_bits
|= ENCODE_RVC_LD_IMM (-1U); break;
511 case 'm': used_bits
|= ENCODE_RVC_LWSP_IMM (-1U); break;
512 case 'n': used_bits
|= ENCODE_RVC_LDSP_IMM (-1U); break;
513 case 'p': used_bits
|= ENCODE_RVC_B_IMM (-1U); break;
514 case 's': USE_BITS (OP_MASK_CRS1S
, OP_SH_CRS1S
); break;
515 case 't': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
516 case 'u': used_bits
|= ENCODE_RVC_IMM (-1U); break;
517 case 'v': used_bits
|= ENCODE_RVC_IMM (-1U); break;
518 case 'w': break; /* RS1S, constrained to equal RD */
519 case 'x': break; /* RS2S, constrained to equal RD */
520 case 'K': used_bits
|= ENCODE_RVC_ADDI4SPN_IMM (-1U); break;
521 case 'L': used_bits
|= ENCODE_RVC_ADDI16SP_IMM (-1U); break;
522 case 'M': used_bits
|= ENCODE_RVC_SWSP_IMM (-1U); break;
523 case 'N': used_bits
|= ENCODE_RVC_SDSP_IMM (-1U); break;
524 case 'U': break; /* RS1, constrained to equal RD */
525 case 'V': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
526 case '<': used_bits
|= ENCODE_RVC_IMM (-1U); break;
527 case '>': used_bits
|= ENCODE_RVC_IMM (-1U); break;
528 case 'T': USE_BITS (OP_MASK_CRS2
, OP_SH_CRS2
); break;
529 case 'D': USE_BITS (OP_MASK_CRS2S
, OP_SH_CRS2S
); break;
531 as_bad (_("internal: bad RISC-V opcode (unknown operand type `C%c'): %s %s"),
532 c
, opc
->name
, opc
->args
);
539 case '<': USE_BITS (OP_MASK_SHAMTW
, OP_SH_SHAMTW
); break;
540 case '>': USE_BITS (OP_MASK_SHAMT
, OP_SH_SHAMT
); break;
542 case 'D': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
543 case 'Z': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
544 case 'E': USE_BITS (OP_MASK_CSR
, OP_SH_CSR
); break;
546 case 'R': USE_BITS (OP_MASK_RS3
, OP_SH_RS3
); break;
547 case 'S': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
548 case 'U': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); /* fallthru */
549 case 'T': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
550 case 'd': USE_BITS (OP_MASK_RD
, OP_SH_RD
); break;
551 case 'm': USE_BITS (OP_MASK_RM
, OP_SH_RM
); break;
552 case 's': USE_BITS (OP_MASK_RS1
, OP_SH_RS1
); break;
553 case 't': USE_BITS (OP_MASK_RS2
, OP_SH_RS2
); break;
554 case 'P': USE_BITS (OP_MASK_PRED
, OP_SH_PRED
); break;
555 case 'Q': USE_BITS (OP_MASK_SUCC
, OP_SH_SUCC
); break;
557 case 'j': used_bits
|= ENCODE_ITYPE_IMM (-1U); break;
558 case 'a': used_bits
|= ENCODE_UJTYPE_IMM (-1U); break;
559 case 'p': used_bits
|= ENCODE_SBTYPE_IMM (-1U); break;
560 case 'q': used_bits
|= ENCODE_STYPE_IMM (-1U); break;
561 case 'u': used_bits
|= ENCODE_UTYPE_IMM (-1U); break;
566 as_bad (_("internal: bad RISC-V opcode "
567 "(unknown operand type `%c'): %s %s"),
568 c
, opc
->name
, opc
->args
);
572 if (used_bits
!= required_bits
)
574 as_bad (_("internal: bad RISC-V opcode (bits 0x%lx undefined): %s %s"),
575 ~(unsigned long)(used_bits
& required_bits
),
576 opc
->name
, opc
->args
);
582 struct percent_op_match
585 bfd_reloc_code_real_type reloc
;
588 /* This function is called once, at assembler startup time. It should set up
589 all the tables, etc. that the MD part of the assembler will need. */
595 unsigned long mach
= xlen
== 64 ? bfd_mach_riscv64
: bfd_mach_riscv32
;
597 if (! bfd_set_arch_mach (stdoutput
, bfd_arch_riscv
, mach
))
598 as_warn (_("Could not set architecture and machine"));
600 op_hash
= hash_new ();
602 while (riscv_opcodes
[i
].name
)
604 const char *name
= riscv_opcodes
[i
].name
;
605 const char *hash_error
=
606 hash_insert (op_hash
, name
, (void *) &riscv_opcodes
[i
]);
610 fprintf (stderr
, _("internal error: can't hash `%s': %s\n"),
611 riscv_opcodes
[i
].name
, hash_error
);
612 /* Probably a memory allocation problem? Give up now. */
613 as_fatal (_("Broken assembler. No assembly attempted."));
618 if (riscv_opcodes
[i
].pinfo
!= INSN_MACRO
)
620 if (!validate_riscv_insn (&riscv_opcodes
[i
]))
621 as_fatal (_("Broken assembler. No assembly attempted."));
625 while (riscv_opcodes
[i
].name
&& !strcmp (riscv_opcodes
[i
].name
, name
));
628 reg_names_hash
= hash_new ();
629 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_numeric
, NGPR
);
630 hash_reg_names (RCLASS_GPR
, riscv_gpr_names_abi
, NGPR
);
631 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_numeric
, NFPR
);
632 hash_reg_names (RCLASS_FPR
, riscv_fpr_names_abi
, NFPR
);
634 #define DECLARE_CSR(name, num) hash_reg_name (RCLASS_CSR, #name, num);
635 #include "opcode/riscv-opc.h"
638 /* Set the default alignment for the text section. */
639 record_alignment (text_section
, riscv_opts
.rvc
? 1 : 2);
643 riscv_apply_const_reloc (bfd_reloc_code_real_type reloc_type
, bfd_vma value
)
650 case BFD_RELOC_RISCV_HI20
:
651 return ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
653 case BFD_RELOC_RISCV_LO12_S
:
654 return ENCODE_STYPE_IMM (value
);
656 case BFD_RELOC_RISCV_LO12_I
:
657 return ENCODE_ITYPE_IMM (value
);
664 /* Output an instruction. IP is the instruction information.
665 ADDRESS_EXPR is an operand of the instruction to be used with
669 append_insn (struct riscv_cl_insn
*ip
, expressionS
*address_expr
,
670 bfd_reloc_code_real_type reloc_type
)
672 dwarf2_emit_insn (0);
674 if (reloc_type
!= BFD_RELOC_UNUSED
)
676 reloc_howto_type
*howto
;
678 gas_assert (address_expr
);
679 if (reloc_type
== BFD_RELOC_12_PCREL
680 || reloc_type
== BFD_RELOC_RISCV_JMP
)
682 int j
= reloc_type
== BFD_RELOC_RISCV_JMP
;
683 int best_case
= riscv_insn_length (ip
->insn_opcode
);
684 unsigned worst_case
= relaxed_branch_length (NULL
, NULL
, 0);
685 add_relaxed_insn (ip
, worst_case
, best_case
,
686 RELAX_BRANCH_ENCODE (j
, best_case
== 2, worst_case
),
687 address_expr
->X_add_symbol
,
688 address_expr
->X_add_number
);
693 howto
= bfd_reloc_type_lookup (stdoutput
, reloc_type
);
695 as_bad (_("Unsupported RISC-V relocation number %d"), reloc_type
);
697 ip
->fixp
= fix_new_exp (ip
->frag
, ip
->where
,
698 bfd_get_reloc_size (howto
),
699 address_expr
, FALSE
, reloc_type
);
701 ip
->fixp
->fx_tcbit
= riscv_opts
.relax
;
709 /* Build an instruction created by a macro expansion. This is passed
710 a pointer to the count of instructions created so far, an
711 expression, the name of the instruction to build, an operand format
712 string, and corresponding arguments. */
715 macro_build (expressionS
*ep
, const char *name
, const char *fmt
, ...)
717 const struct riscv_opcode
*mo
;
718 struct riscv_cl_insn insn
;
719 bfd_reloc_code_real_type r
;
722 va_start (args
, fmt
);
724 r
= BFD_RELOC_UNUSED
;
725 mo
= (struct riscv_opcode
*) hash_find (op_hash
, name
);
728 /* Find a non-RVC variant of the instruction. append_insn will compress
730 while (riscv_insn_length (mo
->match
) < 4)
732 gas_assert (strcmp (name
, mo
->name
) == 0);
734 create_insn (&insn
, mo
);
740 INSERT_OPERAND (RD
, insn
, va_arg (args
, int));
744 INSERT_OPERAND (RS1
, insn
, va_arg (args
, int));
748 INSERT_OPERAND (RS2
, insn
, va_arg (args
, int));
752 INSERT_OPERAND (SHAMT
, insn
, va_arg (args
, int));
758 gas_assert (ep
!= NULL
);
759 r
= va_arg (args
, int);
767 as_fatal (_("internal error: invalid macro"));
772 gas_assert (r
== BFD_RELOC_UNUSED
? ep
== NULL
: ep
!= NULL
);
774 append_insn (&insn
, ep
, r
);
777 /* Sign-extend 32-bit mode constants that have bit 31 set and all higher bits
780 normalize_constant_expr (expressionS
*ex
)
784 if ((ex
->X_op
== O_constant
|| ex
->X_op
== O_symbol
)
785 && IS_ZEXT_32BIT_NUM (ex
->X_add_number
))
786 ex
->X_add_number
= (((ex
->X_add_number
& 0xffffffff) ^ 0x80000000)
790 /* Fail if an expression is not a constant. */
793 check_absolute_expr (struct riscv_cl_insn
*ip
, expressionS
*ex
)
795 if (ex
->X_op
== O_big
)
796 as_bad (_("unsupported large constant"));
797 else if (ex
->X_op
!= O_constant
)
798 as_bad (_("Instruction %s requires absolute expression"),
800 normalize_constant_expr (ex
);
804 make_internal_label (void)
806 return (symbolS
*) local_symbol_make (FAKE_LABEL_NAME
, now_seg
,
807 (valueT
) frag_now_fix (), frag_now
);
810 /* Load an entry from the GOT. */
812 pcrel_access (int destreg
, int tempreg
, expressionS
*ep
,
813 const char *lo_insn
, const char *lo_pattern
,
814 bfd_reloc_code_real_type hi_reloc
,
815 bfd_reloc_code_real_type lo_reloc
)
819 ep2
.X_add_symbol
= make_internal_label ();
820 ep2
.X_add_number
= 0;
822 macro_build (ep
, "auipc", "d,u", tempreg
, hi_reloc
);
823 macro_build (&ep2
, lo_insn
, lo_pattern
, destreg
, tempreg
, lo_reloc
);
827 pcrel_load (int destreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
828 bfd_reloc_code_real_type hi_reloc
,
829 bfd_reloc_code_real_type lo_reloc
)
831 pcrel_access (destreg
, tempreg
, ep
, lo_insn
, "d,s,j", hi_reloc
, lo_reloc
);
835 pcrel_store (int srcreg
, int tempreg
, expressionS
*ep
, const char *lo_insn
,
836 bfd_reloc_code_real_type hi_reloc
,
837 bfd_reloc_code_real_type lo_reloc
)
839 pcrel_access (srcreg
, tempreg
, ep
, lo_insn
, "t,s,q", hi_reloc
, lo_reloc
);
842 /* PC-relative function call using AUIPC/JALR, relaxed to JAL. */
844 riscv_call (int destreg
, int tempreg
, expressionS
*ep
,
845 bfd_reloc_code_real_type reloc
)
847 macro_build (ep
, "auipc", "d,u", tempreg
, reloc
);
848 macro_build (NULL
, "jalr", "d,s", destreg
, tempreg
);
851 /* Load an integer constant into a register. */
854 load_const (int reg
, expressionS
*ep
)
856 int shift
= RISCV_IMM_BITS
;
857 expressionS upper
= *ep
, lower
= *ep
;
858 lower
.X_add_number
= (int32_t) ep
->X_add_number
<< (32-shift
) >> (32-shift
);
859 upper
.X_add_number
-= lower
.X_add_number
;
861 if (ep
->X_op
!= O_constant
)
863 as_bad (_("unsupported large constant"));
867 if (xlen
> 32 && !IS_SEXT_32BIT_NUM (ep
->X_add_number
))
869 /* Reduce to a signed 32-bit constant using SLLI and ADDI. */
870 while (((upper
.X_add_number
>> shift
) & 1) == 0)
873 upper
.X_add_number
= (int64_t) upper
.X_add_number
>> shift
;
874 load_const (reg
, &upper
);
876 macro_build (NULL
, "slli", "d,s,>", reg
, reg
, shift
);
877 if (lower
.X_add_number
!= 0)
878 macro_build (&lower
, "addi", "d,s,j", reg
, reg
, BFD_RELOC_RISCV_LO12_I
);
882 /* Simply emit LUI and/or ADDI to build a 32-bit signed constant. */
885 if (upper
.X_add_number
!= 0)
887 macro_build (ep
, "lui", "d,u", reg
, BFD_RELOC_RISCV_HI20
);
891 if (lower
.X_add_number
!= 0 || hi_reg
== 0)
892 macro_build (ep
, ADD32_INSN
, "d,s,j", reg
, hi_reg
,
893 BFD_RELOC_RISCV_LO12_I
);
897 /* Expand RISC-V assembly macros into one or more instructions. */
899 macro (struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
900 bfd_reloc_code_real_type
*imm_reloc
)
902 int rd
= (ip
->insn_opcode
>> OP_SH_RD
) & OP_MASK_RD
;
903 int rs1
= (ip
->insn_opcode
>> OP_SH_RS1
) & OP_MASK_RS1
;
904 int rs2
= (ip
->insn_opcode
>> OP_SH_RS2
) & OP_MASK_RS2
;
905 int mask
= ip
->insn_mo
->mask
;
910 load_const (rd
, imm_expr
);
915 /* Load the address of a symbol into a register. */
916 if (!IS_SEXT_32BIT_NUM (imm_expr
->X_add_number
))
917 as_bad (_("offset too large"));
919 if (imm_expr
->X_op
== O_constant
)
920 load_const (rd
, imm_expr
);
921 else if (riscv_opts
.pic
&& mask
== M_LA
) /* Global PIC symbol */
922 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
923 BFD_RELOC_RISCV_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
924 else /* Local PIC symbol, or any non-PIC symbol */
925 pcrel_load (rd
, rd
, imm_expr
, "addi",
926 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
930 pcrel_load (rd
, rd
, imm_expr
, "addi",
931 BFD_RELOC_RISCV_TLS_GD_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
935 pcrel_load (rd
, rd
, imm_expr
, LOAD_ADDRESS_INSN
,
936 BFD_RELOC_RISCV_TLS_GOT_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
940 pcrel_load (rd
, rd
, imm_expr
, "lb",
941 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
945 pcrel_load (rd
, rd
, imm_expr
, "lbu",
946 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
950 pcrel_load (rd
, rd
, imm_expr
, "lh",
951 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
955 pcrel_load (rd
, rd
, imm_expr
, "lhu",
956 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
960 pcrel_load (rd
, rd
, imm_expr
, "lw",
961 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
965 pcrel_load (rd
, rd
, imm_expr
, "lwu",
966 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
970 pcrel_load (rd
, rd
, imm_expr
, "ld",
971 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
975 pcrel_load (rd
, rs1
, imm_expr
, "flw",
976 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
980 pcrel_load (rd
, rs1
, imm_expr
, "fld",
981 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_I
);
985 pcrel_store (rs2
, rs1
, imm_expr
, "sb",
986 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
990 pcrel_store (rs2
, rs1
, imm_expr
, "sh",
991 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
995 pcrel_store (rs2
, rs1
, imm_expr
, "sw",
996 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1000 pcrel_store (rs2
, rs1
, imm_expr
, "sd",
1001 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1005 pcrel_store (rs2
, rs1
, imm_expr
, "fsw",
1006 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1010 pcrel_store (rs2
, rs1
, imm_expr
, "fsd",
1011 BFD_RELOC_RISCV_PCREL_HI20
, BFD_RELOC_RISCV_PCREL_LO12_S
);
1015 riscv_call (rd
, rs1
, imm_expr
, *imm_reloc
);
1019 as_bad (_("Macro %s not implemented"), ip
->insn_mo
->name
);
1024 static const struct percent_op_match percent_op_utype
[] =
1026 {"%tprel_hi", BFD_RELOC_RISCV_TPREL_HI20
},
1027 {"%pcrel_hi", BFD_RELOC_RISCV_PCREL_HI20
},
1028 {"%tls_ie_pcrel_hi", BFD_RELOC_RISCV_TLS_GOT_HI20
},
1029 {"%tls_gd_pcrel_hi", BFD_RELOC_RISCV_TLS_GD_HI20
},
1030 {"%hi", BFD_RELOC_RISCV_HI20
},
1034 static const struct percent_op_match percent_op_itype
[] =
1036 {"%lo", BFD_RELOC_RISCV_LO12_I
},
1037 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_I
},
1038 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_I
},
1042 static const struct percent_op_match percent_op_stype
[] =
1044 {"%lo", BFD_RELOC_RISCV_LO12_S
},
1045 {"%tprel_lo", BFD_RELOC_RISCV_TPREL_LO12_S
},
1046 {"%pcrel_lo", BFD_RELOC_RISCV_PCREL_LO12_S
},
1050 static const struct percent_op_match percent_op_rtype
[] =
1052 {"%tprel_add", BFD_RELOC_RISCV_TPREL_ADD
},
1056 /* Return true if *STR points to a relocation operator. When returning true,
1057 move *STR over the operator and store its relocation code in *RELOC.
1058 Leave both *STR and *RELOC alone when returning false. */
1061 parse_relocation (char **str
, bfd_reloc_code_real_type
*reloc
,
1062 const struct percent_op_match
*percent_op
)
1064 for ( ; percent_op
->str
; percent_op
++)
1065 if (strncasecmp (*str
, percent_op
->str
, strlen (percent_op
->str
)) == 0)
1067 int len
= strlen (percent_op
->str
);
1069 if (!ISSPACE ((*str
)[len
]) && (*str
)[len
] != '(')
1072 *str
+= strlen (percent_op
->str
);
1073 *reloc
= percent_op
->reloc
;
1075 /* Check whether the output BFD supports this relocation.
1076 If not, issue an error and fall back on something safe. */
1077 if (*reloc
!= BFD_RELOC_UNUSED
1078 && !bfd_reloc_type_lookup (stdoutput
, *reloc
))
1080 as_bad ("relocation %s isn't supported by the current ABI",
1082 *reloc
= BFD_RELOC_UNUSED
;
1090 my_getExpression (expressionS
*ep
, char *str
)
1094 save_in
= input_line_pointer
;
1095 input_line_pointer
= str
;
1097 expr_end
= input_line_pointer
;
1098 input_line_pointer
= save_in
;
1101 /* Parse string STR as a 16-bit relocatable operand. Store the
1102 expression in *EP and the relocation, if any, in RELOC.
1103 Return the number of relocation operators used (0 or 1).
1105 On exit, EXPR_END points to the first character after the expression. */
1108 my_getSmallExpression (expressionS
*ep
, bfd_reloc_code_real_type
*reloc
,
1109 char *str
, const struct percent_op_match
*percent_op
)
1112 unsigned crux_depth
, str_depth
, regno
;
1115 /* First, check for integer registers. */
1116 if (reg_lookup (&str
, RCLASS_GPR
, ®no
))
1118 ep
->X_op
= O_register
;
1119 ep
->X_add_number
= regno
;
1123 /* Search for the start of the main expression.
1124 End the loop with CRUX pointing to the start
1125 of the main expression and with CRUX_DEPTH containing the number
1126 of open brackets at that point. */
1133 crux_depth
= str_depth
;
1135 /* Skip over whitespace and brackets, keeping count of the number
1137 while (*str
== ' ' || *str
== '\t' || *str
== '(')
1143 && parse_relocation (&str
, reloc
, percent_op
));
1145 my_getExpression (ep
, crux
);
1148 /* Match every open bracket. */
1149 while (crux_depth
> 0 && (*str
== ')' || *str
== ' ' || *str
== '\t'))
1154 as_bad ("unclosed '('");
1161 /* This routine assembles an instruction into its binary format. As a
1162 side effect, it sets the global variable imm_reloc to the type of
1163 relocation to do if one of the operands is an address expression. */
1166 riscv_ip (char *str
, struct riscv_cl_insn
*ip
, expressionS
*imm_expr
,
1167 bfd_reloc_code_real_type
*imm_reloc
)
1172 struct riscv_opcode
*insn
;
1177 const struct percent_op_match
*p
;
1178 const char *error
= "unrecognized opcode";
1180 /* Parse the name of the instruction. Terminate the string if whitespace
1181 is found so that hash_find only sees the name part of the string. */
1182 for (s
= str
; *s
!= '\0'; ++s
)
1190 insn
= (struct riscv_opcode
*) hash_find (op_hash
, str
);
1193 for ( ; insn
&& insn
->name
&& strcmp (insn
->name
, str
) == 0; insn
++)
1195 if (!riscv_subset_supports (insn
->subset
))
1198 create_insn (ip
, insn
);
1201 imm_expr
->X_op
= O_absent
;
1202 *imm_reloc
= BFD_RELOC_UNUSED
;
1203 p
= percent_op_itype
;
1205 for (args
= insn
->args
;; ++args
)
1207 s
+= strspn (s
, " \t");
1210 case '\0': /* End of args. */
1211 if (insn
->pinfo
!= INSN_MACRO
)
1213 if (!insn
->match_func (insn
, ip
->insn_opcode
))
1215 if (riscv_insn_length (insn
->match
) == 2 && !riscv_opts
.rvc
)
1220 /* Successful assembly. */
1227 case 's': /* RS1 x8-x15 */
1228 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1229 || !(regno
>= 8 && regno
<= 15))
1231 INSERT_OPERAND (CRS1S
, *ip
, regno
% 8);
1233 case 'w': /* RS1 x8-x15, constrained to equal RD x8-x15. */
1234 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1235 || EXTRACT_OPERAND (CRS1S
, ip
->insn_opcode
) + 8 != regno
)
1238 case 't': /* RS2 x8-x15 */
1239 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1240 || !(regno
>= 8 && regno
<= 15))
1242 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
1244 case 'x': /* RS2 x8-x15, constrained to equal RD x8-x15. */
1245 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1246 || EXTRACT_OPERAND (CRS2S
, ip
->insn_opcode
) + 8 != regno
)
1249 case 'U': /* RS1, constrained to equal RD. */
1250 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1251 || EXTRACT_OPERAND (RD
, ip
->insn_opcode
) != regno
)
1255 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
))
1257 INSERT_OPERAND (CRS2
, *ip
, regno
);
1259 case 'c': /* RS1, constrained to equal sp. */
1260 if (!reg_lookup (&s
, RCLASS_GPR
, ®no
)
1265 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1266 || imm_expr
->X_op
!= O_constant
1267 || imm_expr
->X_add_number
<= 0
1268 || imm_expr
->X_add_number
>= 64)
1270 ip
->insn_opcode
|= ENCODE_RVC_IMM (imm_expr
->X_add_number
);
1273 imm_expr
->X_op
= O_absent
;
1276 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1277 || imm_expr
->X_op
!= O_constant
1278 || !VALID_RVC_IMM (imm_expr
->X_add_number
)
1279 || imm_expr
->X_add_number
<= 0
1280 || imm_expr
->X_add_number
>= 32)
1282 ip
->insn_opcode
|= ENCODE_RVC_IMM (imm_expr
->X_add_number
);
1285 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1286 || imm_expr
->X_op
!= O_constant
1287 || imm_expr
->X_add_number
== 0
1288 || !VALID_RVC_SIMM3 (imm_expr
->X_add_number
))
1290 ip
->insn_opcode
|= ENCODE_RVC_SIMM3 (imm_expr
->X_add_number
);
1293 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1294 || imm_expr
->X_op
!= O_constant
1295 || imm_expr
->X_add_number
== 0
1296 || !VALID_RVC_IMM (imm_expr
->X_add_number
))
1298 ip
->insn_opcode
|= ENCODE_RVC_IMM (imm_expr
->X_add_number
);
1301 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1302 || imm_expr
->X_op
!= O_constant
1303 || !VALID_RVC_LW_IMM (imm_expr
->X_add_number
))
1305 ip
->insn_opcode
|= ENCODE_RVC_LW_IMM (imm_expr
->X_add_number
);
1308 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1309 || imm_expr
->X_op
!= O_constant
1310 || !VALID_RVC_LD_IMM (imm_expr
->X_add_number
))
1312 ip
->insn_opcode
|= ENCODE_RVC_LD_IMM (imm_expr
->X_add_number
);
1315 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1316 || imm_expr
->X_op
!= O_constant
1317 || !VALID_RVC_LWSP_IMM (imm_expr
->X_add_number
))
1320 ENCODE_RVC_LWSP_IMM (imm_expr
->X_add_number
);
1323 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1324 || imm_expr
->X_op
!= O_constant
1325 || !VALID_RVC_LDSP_IMM (imm_expr
->X_add_number
))
1328 ENCODE_RVC_LDSP_IMM (imm_expr
->X_add_number
);
1331 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1332 || imm_expr
->X_op
!= O_constant
1333 || !VALID_RVC_ADDI4SPN_IMM (imm_expr
->X_add_number
)
1334 || imm_expr
->X_add_number
== 0)
1337 ENCODE_RVC_ADDI4SPN_IMM (imm_expr
->X_add_number
);
1340 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1341 || imm_expr
->X_op
!= O_constant
1342 || !VALID_RVC_ADDI16SP_IMM (imm_expr
->X_add_number
)
1343 || imm_expr
->X_add_number
== 0)
1346 ENCODE_RVC_ADDI16SP_IMM (imm_expr
->X_add_number
);
1349 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1350 || imm_expr
->X_op
!= O_constant
1351 || !VALID_RVC_SWSP_IMM (imm_expr
->X_add_number
))
1354 ENCODE_RVC_SWSP_IMM (imm_expr
->X_add_number
);
1357 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1358 || imm_expr
->X_op
!= O_constant
1359 || !VALID_RVC_SDSP_IMM (imm_expr
->X_add_number
))
1362 ENCODE_RVC_SDSP_IMM (imm_expr
->X_add_number
);
1365 p
= percent_op_utype
;
1366 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
))
1369 if (imm_expr
->X_op
!= O_constant
1370 || imm_expr
->X_add_number
<= 0
1371 || imm_expr
->X_add_number
>= RISCV_BIGIMM_REACH
1372 || (imm_expr
->X_add_number
>= RISCV_RVC_IMM_REACH
/ 2
1373 && (imm_expr
->X_add_number
<
1374 RISCV_BIGIMM_REACH
- RISCV_RVC_IMM_REACH
/ 2)))
1376 ip
->insn_opcode
|= ENCODE_RVC_IMM (imm_expr
->X_add_number
);
1379 if (my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1380 || (imm_expr
->X_add_number
& (RISCV_IMM_REACH
- 1))
1381 || ((int32_t)imm_expr
->X_add_number
1382 != imm_expr
->X_add_number
))
1384 imm_expr
->X_add_number
=
1385 ((uint32_t) imm_expr
->X_add_number
) >> RISCV_IMM_BITS
;
1391 case 'D': /* Floating-point RS2 x8-x15. */
1392 if (!reg_lookup (&s
, RCLASS_FPR
, ®no
)
1393 || !(regno
>= 8 && regno
<= 15))
1395 INSERT_OPERAND (CRS2S
, *ip
, regno
% 8);
1397 case 'T': /* Floating-point RS2. */
1398 if (!reg_lookup (&s
, RCLASS_FPR
, ®no
))
1400 INSERT_OPERAND (CRS2
, *ip
, regno
);
1403 as_bad (_("bad RVC field specifier 'C%c'\n"), *args
);
1422 case '<': /* Shift amount, 0 - 31. */
1423 my_getExpression (imm_expr
, s
);
1424 check_absolute_expr (ip
, imm_expr
);
1425 if ((unsigned long) imm_expr
->X_add_number
> 31)
1426 as_warn (_("Improper shift amount (%lu)"),
1427 (unsigned long) imm_expr
->X_add_number
);
1428 INSERT_OPERAND (SHAMTW
, *ip
, imm_expr
->X_add_number
);
1429 imm_expr
->X_op
= O_absent
;
1433 case '>': /* Shift amount, 0 - (XLEN-1). */
1434 my_getExpression (imm_expr
, s
);
1435 check_absolute_expr (ip
, imm_expr
);
1436 if ((unsigned long) imm_expr
->X_add_number
>= xlen
)
1437 as_warn (_("Improper shift amount (%lu)"),
1438 (unsigned long) imm_expr
->X_add_number
);
1439 INSERT_OPERAND (SHAMT
, *ip
, imm_expr
->X_add_number
);
1440 imm_expr
->X_op
= O_absent
;
1444 case 'Z': /* CSRRxI immediate. */
1445 my_getExpression (imm_expr
, s
);
1446 check_absolute_expr (ip
, imm_expr
);
1447 if ((unsigned long) imm_expr
->X_add_number
> 31)
1448 as_warn (_("Improper CSRxI immediate (%lu)"),
1449 (unsigned long) imm_expr
->X_add_number
);
1450 INSERT_OPERAND (RS1
, *ip
, imm_expr
->X_add_number
);
1451 imm_expr
->X_op
= O_absent
;
1455 case 'E': /* Control register. */
1456 if (reg_lookup (&s
, RCLASS_CSR
, ®no
))
1457 INSERT_OPERAND (CSR
, *ip
, regno
);
1460 my_getExpression (imm_expr
, s
);
1461 check_absolute_expr (ip
, imm_expr
);
1462 if ((unsigned long) imm_expr
->X_add_number
> 0xfff)
1463 as_warn (_("Improper CSR address (%lu)"),
1464 (unsigned long) imm_expr
->X_add_number
);
1465 INSERT_OPERAND (CSR
, *ip
, imm_expr
->X_add_number
);
1466 imm_expr
->X_op
= O_absent
;
1471 case 'm': /* Rounding mode. */
1472 if (arg_lookup (&s
, riscv_rm
, ARRAY_SIZE (riscv_rm
), ®no
))
1474 INSERT_OPERAND (RM
, *ip
, regno
);
1480 case 'Q': /* Fence predecessor/successor. */
1481 if (arg_lookup (&s
, riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
),
1485 INSERT_OPERAND (PRED
, *ip
, regno
);
1487 INSERT_OPERAND (SUCC
, *ip
, regno
);
1492 case 'd': /* Destination register. */
1493 case 's': /* Source register. */
1494 case 't': /* Target register. */
1495 if (reg_lookup (&s
, RCLASS_GPR
, ®no
))
1501 /* Now that we have assembled one operand, we use the args
1502 string to figure out where it goes in the instruction. */
1506 INSERT_OPERAND (RS1
, *ip
, regno
);
1509 INSERT_OPERAND (RD
, *ip
, regno
);
1512 INSERT_OPERAND (RS2
, *ip
, regno
);
1519 case 'D': /* Floating point rd. */
1520 case 'S': /* Floating point rs1. */
1521 case 'T': /* Floating point rs2. */
1522 case 'U': /* Floating point rs1 and rs2. */
1523 case 'R': /* Floating point rs3. */
1524 if (reg_lookup (&s
, RCLASS_FPR
, ®no
))
1532 INSERT_OPERAND (RD
, *ip
, regno
);
1535 INSERT_OPERAND (RS1
, *ip
, regno
);
1538 INSERT_OPERAND (RS1
, *ip
, regno
);
1541 INSERT_OPERAND (RS2
, *ip
, regno
);
1544 INSERT_OPERAND (RS3
, *ip
, regno
);
1553 my_getExpression (imm_expr
, s
);
1554 if (imm_expr
->X_op
!= O_big
1555 && imm_expr
->X_op
!= O_constant
)
1557 normalize_constant_expr (imm_expr
);
1562 my_getExpression (imm_expr
, s
);
1563 normalize_constant_expr (imm_expr
);
1564 /* The 'A' format specifier must be a symbol. */
1565 if (imm_expr
->X_op
!= O_symbol
)
1567 *imm_reloc
= BFD_RELOC_32
;
1571 case 'j': /* Sign-extended immediate. */
1572 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
1573 p
= percent_op_itype
;
1575 case 'q': /* Store displacement. */
1576 p
= percent_op_stype
;
1577 *imm_reloc
= BFD_RELOC_RISCV_LO12_S
;
1579 case 'o': /* Load displacement. */
1580 p
= percent_op_itype
;
1581 *imm_reloc
= BFD_RELOC_RISCV_LO12_I
;
1583 case '0': /* AMO "displacement," which must be zero. */
1584 p
= percent_op_rtype
;
1585 *imm_reloc
= BFD_RELOC_UNUSED
;
1587 /* Check whether there is only a single bracketed expression
1588 left. If so, it must be the base register and the
1589 constant must be zero. */
1590 imm_expr
->X_op
= O_constant
;
1591 imm_expr
->X_add_number
= 0;
1592 if (*s
== '(' && strchr (s
+ 1, '(') == 0)
1595 /* If this value won't fit into a 16 bit offset, then go
1596 find a macro that will generate the 32 bit offset
1598 if (!my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
))
1600 normalize_constant_expr (imm_expr
);
1601 if (imm_expr
->X_op
!= O_constant
1602 || (*args
== '0' && imm_expr
->X_add_number
!= 0)
1603 || imm_expr
->X_add_number
>= (signed)RISCV_IMM_REACH
/2
1604 || imm_expr
->X_add_number
< -(signed)RISCV_IMM_REACH
/2)
1611 case 'p': /* PC-relative offset. */
1613 *imm_reloc
= BFD_RELOC_12_PCREL
;
1614 my_getExpression (imm_expr
, s
);
1618 case 'u': /* Upper 20 bits. */
1619 p
= percent_op_utype
;
1620 if (!my_getSmallExpression (imm_expr
, imm_reloc
, s
, p
)
1621 && imm_expr
->X_op
== O_constant
)
1623 if (imm_expr
->X_add_number
< 0
1624 || imm_expr
->X_add_number
>= (signed)RISCV_BIGIMM_REACH
)
1625 as_bad (_("lui expression not in range 0..1048575"));
1627 *imm_reloc
= BFD_RELOC_RISCV_HI20
;
1628 imm_expr
->X_add_number
<<= RISCV_IMM_BITS
;
1633 case 'a': /* 20-bit PC-relative offset. */
1635 my_getExpression (imm_expr
, s
);
1637 *imm_reloc
= BFD_RELOC_RISCV_JMP
;
1641 my_getExpression (imm_expr
, s
);
1643 if (strcmp (s
, "@plt") == 0)
1645 *imm_reloc
= BFD_RELOC_RISCV_CALL_PLT
;
1649 *imm_reloc
= BFD_RELOC_RISCV_CALL
;
1653 as_fatal (_("internal error: bad argument type %c"), *args
);
1658 error
= _("illegal operands");
1662 /* Restore the character we might have clobbered above. */
1664 *(argsStart
- 1) = save_c
;
1670 md_assemble (char *str
)
1672 struct riscv_cl_insn insn
;
1673 expressionS imm_expr
;
1674 bfd_reloc_code_real_type imm_reloc
= BFD_RELOC_UNUSED
;
1676 const char *error
= riscv_ip (str
, &insn
, &imm_expr
, &imm_reloc
);
1680 as_bad ("%s `%s'", error
, str
);
1684 if (insn
.insn_mo
->pinfo
== INSN_MACRO
)
1685 macro (&insn
, &imm_expr
, &imm_reloc
);
1687 append_insn (&insn
, &imm_expr
, imm_reloc
);
1691 md_atof (int type
, char *litP
, int *sizeP
)
1693 return ieee_md_atof (type
, litP
, sizeP
, TARGET_BYTES_BIG_ENDIAN
);
1697 md_number_to_chars (char *buf
, valueT val
, int n
)
1699 number_to_chars_littleendian (buf
, val
, n
);
1702 const char *md_shortopts
= "O::g::G:";
1706 OPTION_MARCH
= OPTION_MD_BASE
,
1713 struct option md_longopts
[] =
1715 {"march", required_argument
, NULL
, OPTION_MARCH
},
1716 {"fPIC", no_argument
, NULL
, OPTION_PIC
},
1717 {"fpic", no_argument
, NULL
, OPTION_PIC
},
1718 {"fno-pic", no_argument
, NULL
, OPTION_NO_PIC
},
1719 {"mabi", required_argument
, NULL
, OPTION_MABI
},
1721 {NULL
, no_argument
, NULL
, 0}
1723 size_t md_longopts_size
= sizeof (md_longopts
);
1726 FLOAT_ABI_DEFAULT
= -1,
1732 static enum float_abi float_abi
= FLOAT_ABI_DEFAULT
;
1735 riscv_set_abi (unsigned new_xlen
, enum float_abi new_float_abi
)
1737 abi_xlen
= new_xlen
;
1738 float_abi
= new_float_abi
;
1742 md_parse_option (int c
, const char *arg
)
1747 riscv_set_arch (arg
);
1751 riscv_opts
.pic
= FALSE
;
1755 riscv_opts
.pic
= TRUE
;
1759 if (strcmp (arg
, "ilp32") == 0)
1760 riscv_set_abi (32, FLOAT_ABI_SOFT
);
1761 else if (strcmp (arg
, "ilp32f") == 0)
1762 riscv_set_abi (32, FLOAT_ABI_SINGLE
);
1763 else if (strcmp (arg
, "ilp32d") == 0)
1764 riscv_set_abi (32, FLOAT_ABI_DOUBLE
);
1765 else if (strcmp (arg
, "ilp32q") == 0)
1766 riscv_set_abi (32, FLOAT_ABI_QUAD
);
1767 else if (strcmp (arg
, "lp64") == 0)
1768 riscv_set_abi (64, FLOAT_ABI_SOFT
);
1769 else if (strcmp (arg
, "lp64f") == 0)
1770 riscv_set_abi (64, FLOAT_ABI_SINGLE
);
1771 else if (strcmp (arg
, "lp64d") == 0)
1772 riscv_set_abi (64, FLOAT_ABI_DOUBLE
);
1773 else if (strcmp (arg
, "lp64q") == 0)
1774 riscv_set_abi (64, FLOAT_ABI_QUAD
);
1787 riscv_after_parse_args (void)
1791 if (strcmp (default_arch
, "riscv32") == 0)
1793 else if (strcmp (default_arch
, "riscv64") == 0)
1796 as_bad ("unknown default architecture `%s'", default_arch
);
1799 if (riscv_subsets
== NULL
)
1800 riscv_set_arch (xlen
== 64 ? "rv64g" : "rv32g");
1802 /* Add the RVC extension, regardless of -march, to support .option rvc. */
1803 if (riscv_subset_supports ("c"))
1804 riscv_set_rvc (TRUE
);
1806 riscv_add_subset ("c");
1808 /* Infer ABI from ISA if not specified on command line. */
1811 else if (abi_xlen
> xlen
)
1812 as_bad ("can't have %d-bit ABI on %d-bit ISA", abi_xlen
, xlen
);
1813 else if (abi_xlen
< xlen
)
1814 as_bad ("%d-bit ABI not yet supported on %d-bit ISA", abi_xlen
, xlen
);
1816 if (float_abi
== FLOAT_ABI_DEFAULT
)
1818 struct riscv_subset
*subset
;
1820 /* Assume soft-float unless D extension is present. */
1821 float_abi
= FLOAT_ABI_SOFT
;
1823 for (subset
= riscv_subsets
; subset
!= NULL
; subset
= subset
->next
)
1825 if (strcasecmp (subset
->name
, "D") == 0)
1826 float_abi
= FLOAT_ABI_DOUBLE
;
1827 if (strcasecmp (subset
->name
, "Q") == 0)
1828 float_abi
= FLOAT_ABI_QUAD
;
1832 /* Insert float_abi into the EF_RISCV_FLOAT_ABI field of elf_flags. */
1833 elf_flags
|= float_abi
* (EF_RISCV_FLOAT_ABI
& ~(EF_RISCV_FLOAT_ABI
<< 1));
1837 md_pcrel_from (fixS
*fixP
)
1839 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
1842 /* Apply a fixup to the object file. */
1845 md_apply_fix (fixS
*fixP
, valueT
*valP
, segT seg ATTRIBUTE_UNUSED
)
1847 unsigned int subtype
;
1848 bfd_byte
*buf
= (bfd_byte
*) (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
);
1849 bfd_boolean relaxable
= FALSE
;
1852 /* Remember value for tc_gen_reloc. */
1853 fixP
->fx_addnumber
= *valP
;
1855 switch (fixP
->fx_r_type
)
1857 case BFD_RELOC_RISCV_HI20
:
1858 case BFD_RELOC_RISCV_LO12_I
:
1859 case BFD_RELOC_RISCV_LO12_S
:
1860 bfd_putl32 (riscv_apply_const_reloc (fixP
->fx_r_type
, *valP
)
1861 | bfd_getl32 (buf
), buf
);
1862 if (fixP
->fx_addsy
== NULL
)
1863 fixP
->fx_done
= TRUE
;
1867 case BFD_RELOC_RISCV_GOT_HI20
:
1868 case BFD_RELOC_RISCV_PCREL_HI20
:
1869 case BFD_RELOC_RISCV_ADD8
:
1870 case BFD_RELOC_RISCV_ADD16
:
1871 case BFD_RELOC_RISCV_ADD32
:
1872 case BFD_RELOC_RISCV_ADD64
:
1873 case BFD_RELOC_RISCV_SUB6
:
1874 case BFD_RELOC_RISCV_SUB8
:
1875 case BFD_RELOC_RISCV_SUB16
:
1876 case BFD_RELOC_RISCV_SUB32
:
1877 case BFD_RELOC_RISCV_SUB64
:
1878 case BFD_RELOC_RISCV_RELAX
:
1881 case BFD_RELOC_RISCV_TPREL_HI20
:
1882 case BFD_RELOC_RISCV_TPREL_LO12_I
:
1883 case BFD_RELOC_RISCV_TPREL_LO12_S
:
1884 case BFD_RELOC_RISCV_TPREL_ADD
:
1888 case BFD_RELOC_RISCV_TLS_GOT_HI20
:
1889 case BFD_RELOC_RISCV_TLS_GD_HI20
:
1890 case BFD_RELOC_RISCV_TLS_DTPREL32
:
1891 case BFD_RELOC_RISCV_TLS_DTPREL64
:
1892 if (fixP
->fx_addsy
!= NULL
)
1893 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1895 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1896 _("TLS relocation against a constant"));
1903 case BFD_RELOC_RISCV_CFA
:
1904 if (fixP
->fx_addsy
&& fixP
->fx_subsy
)
1906 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
1907 fixP
->fx_next
->fx_addsy
= fixP
->fx_subsy
;
1908 fixP
->fx_next
->fx_subsy
= NULL
;
1909 fixP
->fx_next
->fx_offset
= 0;
1910 fixP
->fx_subsy
= NULL
;
1912 switch (fixP
->fx_r_type
)
1915 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD64
;
1916 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB64
;
1920 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD32
;
1921 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
1925 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD16
;
1926 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
1930 fixP
->fx_r_type
= BFD_RELOC_RISCV_ADD8
;
1931 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
1934 case BFD_RELOC_RISCV_CFA
:
1935 /* Load the byte to get the subtype. */
1936 subtype
= bfd_get_8 (NULL
, &((fragS
*) (fixP
->fx_frag
->fr_opcode
))->fr_literal
[fixP
->fx_where
]);
1937 loc
= fixP
->fx_frag
->fr_fix
- (subtype
& 7);
1940 case DW_CFA_advance_loc1
:
1941 fixP
->fx_where
= loc
+ 1;
1942 fixP
->fx_next
->fx_where
= loc
+ 1;
1943 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET8
;
1944 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB8
;
1947 case DW_CFA_advance_loc2
:
1949 fixP
->fx_next
->fx_size
= 2;
1950 fixP
->fx_where
= loc
+ 1;
1951 fixP
->fx_next
->fx_where
= loc
+ 1;
1952 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET16
;
1953 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB16
;
1956 case DW_CFA_advance_loc4
:
1958 fixP
->fx_next
->fx_size
= 4;
1959 fixP
->fx_where
= loc
;
1960 fixP
->fx_next
->fx_where
= loc
;
1961 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET32
;
1962 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB32
;
1966 if (subtype
< 0x80 && (subtype
& 0x40))
1968 /* DW_CFA_advance_loc */
1969 fixP
->fx_frag
= (fragS
*) fixP
->fx_frag
->fr_opcode
;
1970 fixP
->fx_next
->fx_frag
= fixP
->fx_frag
;
1971 fixP
->fx_r_type
= BFD_RELOC_RISCV_SET6
;
1972 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_SUB6
;
1975 as_fatal (_("internal error: bad CFA value #%d"), subtype
);
1981 /* This case is unreachable. */
1988 /* If we are deleting this reloc entry, we must fill in the
1989 value now. This can happen if we have a .word which is not
1990 resolved when it appears but is later defined. */
1991 if (fixP
->fx_addsy
== NULL
)
1993 gas_assert (fixP
->fx_size
<= sizeof (valueT
));
1994 md_number_to_chars ((char *) buf
, *valP
, fixP
->fx_size
);
1999 case BFD_RELOC_RISCV_JMP
:
2002 /* Fill in a tentative value to improve objdump readability. */
2003 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
2004 bfd_vma delta
= target
- md_pcrel_from (fixP
);
2005 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_UJTYPE_IMM (delta
), buf
);
2009 case BFD_RELOC_12_PCREL
:
2012 /* Fill in a tentative value to improve objdump readability. */
2013 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
2014 bfd_vma delta
= target
- md_pcrel_from (fixP
);
2015 bfd_putl32 (bfd_getl32 (buf
) | ENCODE_SBTYPE_IMM (delta
), buf
);
2019 case BFD_RELOC_RISCV_RVC_BRANCH
:
2022 /* Fill in a tentative value to improve objdump readability. */
2023 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
2024 bfd_vma delta
= target
- md_pcrel_from (fixP
);
2025 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_RVC_B_IMM (delta
), buf
);
2029 case BFD_RELOC_RISCV_RVC_JUMP
:
2032 /* Fill in a tentative value to improve objdump readability. */
2033 bfd_vma target
= S_GET_VALUE (fixP
->fx_addsy
) + *valP
;
2034 bfd_vma delta
= target
- md_pcrel_from (fixP
);
2035 bfd_putl16 (bfd_getl16 (buf
) | ENCODE_RVC_J_IMM (delta
), buf
);
2039 case BFD_RELOC_RISCV_CALL
:
2040 case BFD_RELOC_RISCV_CALL_PLT
:
2044 case BFD_RELOC_RISCV_PCREL_LO12_S
:
2045 case BFD_RELOC_RISCV_PCREL_LO12_I
:
2046 case BFD_RELOC_RISCV_ALIGN
:
2050 /* We ignore generic BFD relocations we don't know about. */
2051 if (bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
) != NULL
)
2052 as_fatal (_("internal error: bad relocation #%d"), fixP
->fx_r_type
);
2055 if (fixP
->fx_subsy
!= NULL
)
2056 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2057 _("unsupported symbol subtraction"));
2059 /* Add an R_RISCV_RELAX reloc if the reloc is relaxable. */
2060 if (relaxable
&& fixP
->fx_tcbit
&& fixP
->fx_addsy
!= NULL
)
2062 fixP
->fx_next
= xmemdup (fixP
, sizeof (*fixP
), sizeof (*fixP
));
2063 fixP
->fx_next
->fx_addsy
= fixP
->fx_next
->fx_subsy
= NULL
;
2064 fixP
->fx_next
->fx_r_type
= BFD_RELOC_RISCV_RELAX
;
2068 /* Because the value of .cfi_remember_state may changed after relaxation,
2069 we insert a fix to relocate it again in link-time. */
2072 riscv_pre_output_hook (void)
2074 const frchainS
*frch
;
2077 for (s
= stdoutput
->sections
; s
; s
= s
->next
)
2078 for (frch
= seg_info (s
)->frchainP
; frch
; frch
= frch
->frch_next
)
2082 for (frag
= frch
->frch_root
; frag
; frag
= frag
->fr_next
)
2084 if (frag
->fr_type
== rs_cfa
)
2088 symbolS
*add_symbol
= frag
->fr_symbol
->sy_value
.X_add_symbol
;
2089 symbolS
*op_symbol
= frag
->fr_symbol
->sy_value
.X_op_symbol
;
2091 exp
.X_op
= O_subtract
;
2092 exp
.X_add_symbol
= add_symbol
;
2093 exp
.X_add_number
= 0;
2094 exp
.X_op_symbol
= op_symbol
;
2096 fix_new_exp (frag
, (int) frag
->fr_offset
, 1, &exp
, 0,
2097 BFD_RELOC_RISCV_CFA
);
2104 /* This structure is used to hold a stack of .option values. */
2106 struct riscv_option_stack
2108 struct riscv_option_stack
*next
;
2109 struct riscv_set_options options
;
2112 static struct riscv_option_stack
*riscv_opts_stack
;
2114 /* Handle the .option pseudo-op. */
2117 s_riscv_option (int x ATTRIBUTE_UNUSED
)
2119 char *name
= input_line_pointer
, ch
;
2121 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
2122 ++input_line_pointer
;
2123 ch
= *input_line_pointer
;
2124 *input_line_pointer
= '\0';
2126 if (strcmp (name
, "rvc") == 0)
2127 riscv_set_rvc (TRUE
);
2128 else if (strcmp (name
, "norvc") == 0)
2129 riscv_set_rvc (FALSE
);
2130 else if (strcmp (name
, "pic") == 0)
2131 riscv_opts
.pic
= TRUE
;
2132 else if (strcmp (name
, "nopic") == 0)
2133 riscv_opts
.pic
= FALSE
;
2134 else if (strcmp (name
, "relax") == 0)
2135 riscv_opts
.relax
= TRUE
;
2136 else if (strcmp (name
, "norelax") == 0)
2137 riscv_opts
.relax
= FALSE
;
2138 else if (strcmp (name
, "push") == 0)
2140 struct riscv_option_stack
*s
;
2142 s
= (struct riscv_option_stack
*) xmalloc (sizeof *s
);
2143 s
->next
= riscv_opts_stack
;
2144 s
->options
= riscv_opts
;
2145 riscv_opts_stack
= s
;
2147 else if (strcmp (name
, "pop") == 0)
2149 struct riscv_option_stack
*s
;
2151 s
= riscv_opts_stack
;
2153 as_bad (_(".option pop with no .option push"));
2156 riscv_opts
= s
->options
;
2157 riscv_opts_stack
= s
->next
;
2163 as_warn (_("Unrecognized .option directive: %s\n"), name
);
2165 *input_line_pointer
= ch
;
2166 demand_empty_rest_of_line ();
2169 /* Handle the .dtprelword and .dtpreldword pseudo-ops. They generate
2170 a 32-bit or 64-bit DTP-relative relocation (BYTES says which) for
2171 use in DWARF debug information. */
2174 s_dtprel (int bytes
)
2181 if (ex
.X_op
!= O_symbol
)
2183 as_bad (_("Unsupported use of %s"), (bytes
== 8
2186 ignore_rest_of_line ();
2189 p
= frag_more (bytes
);
2190 md_number_to_chars (p
, 0, bytes
);
2191 fix_new_exp (frag_now
, p
- frag_now
->fr_literal
, bytes
, &ex
, FALSE
,
2193 ? BFD_RELOC_RISCV_TLS_DTPREL64
2194 : BFD_RELOC_RISCV_TLS_DTPREL32
));
2196 demand_empty_rest_of_line ();
2199 /* Handle the .bss pseudo-op. */
2202 s_bss (int ignore ATTRIBUTE_UNUSED
)
2204 subseg_set (bss_section
, 0);
2205 demand_empty_rest_of_line ();
2209 riscv_make_nops (char *buf
, bfd_vma bytes
)
2213 /* RISC-V instructions cannot begin or end on odd addresses, so this case
2214 means we are not within a valid instruction sequence. It is thus safe
2215 to use a zero byte, even though that is not a valid instruction. */
2219 /* Use at most one 2-byte NOP. */
2220 if ((bytes
- i
) % 4 == 2)
2222 md_number_to_chars (buf
+ i
, RVC_NOP
, 2);
2226 /* Fill the remainder with 4-byte NOPs. */
2227 for ( ; i
< bytes
; i
+= 4)
2228 md_number_to_chars (buf
+ i
, RISCV_NOP
, 4);
2231 /* Called from md_do_align. Used to create an alignment frag in a
2232 code section by emitting a worst-case NOP sequence that the linker
2233 will later relax to the correct number of NOPs. We can't compute
2234 the correct alignment now because of other linker relaxations. */
2237 riscv_frag_align_code (int n
)
2239 bfd_vma bytes
= (bfd_vma
) 1 << n
;
2240 bfd_vma min_text_alignment_order
= riscv_opts
.rvc
? 1 : 2;
2241 bfd_vma min_text_alignment
= (bfd_vma
) 1 << min_text_alignment_order
;
2243 /* First, get back to minimal alignment. */
2244 frag_align_code (min_text_alignment_order
, 0);
2246 /* When not relaxing, riscv_handle_align handles code alignment. */
2247 if (!riscv_opts
.relax
)
2250 if (bytes
> min_text_alignment
)
2252 bfd_vma worst_case_bytes
= bytes
- min_text_alignment
;
2253 char *nops
= frag_more (worst_case_bytes
);
2256 ex
.X_op
= O_constant
;
2257 ex
.X_add_number
= worst_case_bytes
;
2259 riscv_make_nops (nops
, worst_case_bytes
);
2261 fix_new_exp (frag_now
, nops
- frag_now
->fr_literal
, 0,
2262 &ex
, FALSE
, BFD_RELOC_RISCV_ALIGN
);
2268 /* Implement HANDLE_ALIGN. */
2271 riscv_handle_align (fragS
*fragP
)
2273 switch (fragP
->fr_type
)
2276 /* When relaxing, riscv_frag_align_code handles code alignment. */
2277 if (!riscv_opts
.relax
)
2279 bfd_signed_vma count
= fragP
->fr_next
->fr_address
2280 - fragP
->fr_address
- fragP
->fr_fix
;
2285 count
&= MAX_MEM_FOR_RS_ALIGN_CODE
;
2286 riscv_make_nops (fragP
->fr_literal
+ fragP
->fr_fix
, count
);
2287 fragP
->fr_var
= count
;
2297 md_estimate_size_before_relax (fragS
*fragp
, asection
*segtype
)
2299 return (fragp
->fr_var
= relaxed_branch_length (fragp
, segtype
, FALSE
));
2302 /* Translate internal representation of relocation info to BFD target
2306 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixp
)
2308 arelent
*reloc
= (arelent
*) xmalloc (sizeof (arelent
));
2310 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
2311 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2312 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2313 reloc
->addend
= fixp
->fx_addnumber
;
2315 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2316 if (reloc
->howto
== NULL
)
2318 if ((fixp
->fx_r_type
== BFD_RELOC_16
|| fixp
->fx_r_type
== BFD_RELOC_8
)
2319 && fixp
->fx_addsy
!= NULL
&& fixp
->fx_subsy
!= NULL
)
2321 /* We don't have R_RISCV_8/16, but for this special case,
2322 we can use R_RISCV_ADD8/16 with R_RISCV_SUB8/16. */
2326 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2327 _("cannot represent %s relocation in object file"),
2328 bfd_get_reloc_code_name (fixp
->fx_r_type
));
2336 riscv_relax_frag (asection
*sec
, fragS
*fragp
, long stretch ATTRIBUTE_UNUSED
)
2338 if (RELAX_BRANCH_P (fragp
->fr_subtype
))
2340 offsetT old_var
= fragp
->fr_var
;
2341 fragp
->fr_var
= relaxed_branch_length (fragp
, sec
, TRUE
);
2342 return fragp
->fr_var
- old_var
;
2348 /* Expand far branches to multi-instruction sequences. */
2351 md_convert_frag_branch (fragS
*fragp
)
2359 buf
= (bfd_byte
*)fragp
->fr_literal
+ fragp
->fr_fix
;
2361 exp
.X_op
= O_symbol
;
2362 exp
.X_add_symbol
= fragp
->fr_symbol
;
2363 exp
.X_add_number
= fragp
->fr_offset
;
2365 gas_assert (fragp
->fr_var
== RELAX_BRANCH_LENGTH (fragp
->fr_subtype
));
2367 if (RELAX_BRANCH_RVC (fragp
->fr_subtype
))
2369 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
2373 /* Expand the RVC branch into a RISC-V one. */
2374 insn
= bfd_getl16 (buf
);
2375 rs1
= 8 + ((insn
>> OP_SH_CRS1S
) & OP_MASK_CRS1S
);
2376 if ((insn
& MASK_C_J
) == MATCH_C_J
)
2378 else if ((insn
& MASK_C_JAL
) == MATCH_C_JAL
)
2379 insn
= MATCH_JAL
| (X_RA
<< OP_SH_RD
);
2380 else if ((insn
& MASK_C_BEQZ
) == MATCH_C_BEQZ
)
2381 insn
= MATCH_BEQ
| (rs1
<< OP_SH_RS1
);
2382 else if ((insn
& MASK_C_BNEZ
) == MATCH_C_BNEZ
)
2383 insn
= MATCH_BNE
| (rs1
<< OP_SH_RS1
);
2386 bfd_putl32 (insn
, buf
);
2390 /* Invert the branch condition. Branch over the jump. */
2391 insn
= bfd_getl16 (buf
);
2392 insn
^= MATCH_C_BEQZ
^ MATCH_C_BNEZ
;
2393 insn
|= ENCODE_RVC_B_IMM (6);
2394 bfd_putl16 (insn
, buf
);
2399 /* Just keep the RVC branch. */
2400 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
2401 ? BFD_RELOC_RISCV_RVC_JUMP
: BFD_RELOC_RISCV_RVC_BRANCH
;
2402 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
2403 2, &exp
, FALSE
, reloc
);
2412 switch (RELAX_BRANCH_LENGTH (fragp
->fr_subtype
))
2415 gas_assert (!RELAX_BRANCH_UNCOND (fragp
->fr_subtype
));
2417 /* Invert the branch condition. Branch over the jump. */
2418 insn
= bfd_getl32 (buf
);
2419 insn
^= MATCH_BEQ
^ MATCH_BNE
;
2420 insn
|= ENCODE_SBTYPE_IMM (8);
2421 md_number_to_chars ((char *) buf
, insn
, 4);
2425 /* Jump to the target. */
2426 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
2427 4, &exp
, FALSE
, BFD_RELOC_RISCV_JMP
);
2428 md_number_to_chars ((char *) buf
, MATCH_JAL
, 4);
2433 reloc
= RELAX_BRANCH_UNCOND (fragp
->fr_subtype
)
2434 ? BFD_RELOC_RISCV_JMP
: BFD_RELOC_12_PCREL
;
2435 fixp
= fix_new_exp (fragp
, buf
- (bfd_byte
*)fragp
->fr_literal
,
2436 4, &exp
, FALSE
, reloc
);
2445 fixp
->fx_file
= fragp
->fr_file
;
2446 fixp
->fx_line
= fragp
->fr_line
;
2448 gas_assert (buf
== (bfd_byte
*)fragp
->fr_literal
2449 + fragp
->fr_fix
+ fragp
->fr_var
);
2451 fragp
->fr_fix
+= fragp
->fr_var
;
2454 /* Relax a machine dependent frag. This returns the amount by which
2455 the current size of the frag should change. */
2458 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
, segT asec ATTRIBUTE_UNUSED
,
2461 gas_assert (RELAX_BRANCH_P (fragp
->fr_subtype
));
2462 md_convert_frag_branch (fragp
);
2466 md_show_usage (FILE *stream
)
2468 fprintf (stream
, _("\
2470 -m32 assemble RV32 code\n\
2471 -m64 assemble RV64 code (default)\n\
2472 -fpic generate position-independent code\n\
2473 -fno-pic don't generate position-independent code (default)\n\
2474 -msoft-float don't use F registers for floating-point values\n\
2475 -mhard-float use F registers for floating-point values (default)\n\
2476 -mno-rvc disable the C extension for compressed instructions (default)\n\
2477 -mrvc enable the C extension for compressed instructions\n\
2478 -march=ISA set the RISC-V architecture, RV64IMAFD by default\n\
2482 /* Standard calling conventions leave the CFA at SP on entry. */
2484 riscv_cfi_frame_initial_instructions (void)
2486 cfi_add_CFA_def_cfa_register (X_SP
);
2490 tc_riscv_regname_to_dw2regnum (char *regname
)
2494 if ((reg
= reg_lookup_internal (regname
, RCLASS_GPR
)) >= 0)
2497 if ((reg
= reg_lookup_internal (regname
, RCLASS_FPR
)) >= 0)
2500 as_bad (_("unknown register `%s'"), regname
);
2505 riscv_elf_final_processing (void)
2507 elf_elfheader (stdoutput
)->e_flags
|= elf_flags
;
2510 /* Parse the .sleb128 and .uleb128 pseudos. Only allow constant expressions,
2511 since these directives break relaxation when used with symbol deltas. */
2514 s_riscv_leb128 (int sign
)
2517 char *save_in
= input_line_pointer
;
2520 if (exp
.X_op
!= O_constant
)
2521 as_bad (_("non-constant .%cleb128 is not supported"), sign
? 's' : 'u');
2522 demand_empty_rest_of_line ();
2524 input_line_pointer
= save_in
;
2525 return s_leb128 (sign
);
2528 /* Pseudo-op table. */
2530 static const pseudo_typeS riscv_pseudo_table
[] =
2532 /* RISC-V-specific pseudo-ops. */
2533 {"option", s_riscv_option
, 0},
2537 {"dtprelword", s_dtprel
, 4},
2538 {"dtpreldword", s_dtprel
, 8},
2540 {"uleb128", s_riscv_leb128
, 0},
2541 {"sleb128", s_riscv_leb128
, 1},
2547 riscv_pop_insert (void)
2549 extern void pop_insert (const pseudo_typeS
*);
2551 pop_insert (riscv_pseudo_table
);