1 @c Copyright (C) 2016-2022 Free Software Foundation, Inc.
2 @c This is part of the GAS anual.
3 @c For copying conditions, see the file as.texinfo
9 @chapter RISC-V Dependent Features
12 @node Machine Dependencies
13 @chapter RISC-V Dependent Features
16 @cindex RISC-V support
18 * RISC-V-Options:: RISC-V Options
19 * RISC-V-Directives:: RISC-V Directives
20 * RISC-V-Modifiers:: RISC-V Assembler Modifiers
21 * RISC-V-Formats:: RISC-V Instruction Formats
22 * RISC-V-ATTRIBUTE:: RISC-V Object Attribute
26 @section RISC-V Options
28 The following table lists all available RISC-V specific options.
33 @cindex @samp{-fpic} option, RISC-V
36 Generate position-independent code
38 @cindex @samp{-fno-pic} option, RISC-V
40 Don't generate position-independent code (default)
42 @cindex @samp{-march=ISA} option, RISC-V
44 Select the base isa, as specified by ISA. For example -march=rv32ima.
45 If this option and the architecture attributes aren't set, then assembler
46 will check the default configure setting --with-arch=ISA.
48 @cindex @samp{-misa-spec=ISAspec} option, RISC-V
49 @item -misa-spec=ISAspec
50 Select the default isa spec version. If the version of ISA isn't set
51 by -march, then assembler helps to set the version according to
52 the default chosen spec. If this option isn't set, then assembler will
53 check the default configure setting --with-isa-spec=ISAspec.
55 @cindex @samp{-mpriv-spec=PRIVspec} option, RISC-V
56 @item -mpriv-spec=PRIVspec
57 Select the privileged spec version. We can decide whether the CSR is valid or
58 not according to the chosen spec. If this option and the privilege attributes
59 aren't set, then assembler will check the default configure setting
60 --with-priv-spec=PRIVspec.
62 @cindex @samp{-mabi=ABI} option, RISC-V
64 Selects the ABI, which is either "ilp32" or "lp64", optionally followed
65 by "f", "d", or "q" to indicate single-precision, double-precision, or
66 quad-precision floating-point calling convention, or none to indicate
67 the soft-float calling convention. Also, "ilp32" can optionally be followed
68 by "e" to indicate the RVE ABI, which is always soft-float.
70 @cindex @samp{-mrelax} option, RISC-V
72 Take advantage of linker relaxations to reduce the number of instructions
73 required to materialize symbol addresses. (default)
75 @cindex @samp{-mno-relax} option, RISC-V
77 Don't do linker relaxations.
79 @cindex @samp{-march-attr} option, RISC-V
81 Generate the default contents for the riscv elf attribute section if the
82 .attribute directives are not set. This section is used to record the
83 information that a linker or runtime loader needs to check compatibility.
84 This information includes ISA string, stack alignment requirement, unaligned
85 memory accesses, and the major, minor and revision version of privileged
88 @cindex @samp{-mno-arch-attr} option, RISC-V
90 Don't generate the default riscv elf attribute section if the .attribute
91 directives are not set.
93 @cindex @samp{-mcsr-check} option, RISC-V
95 Enable the CSR checking for the ISA-dependent CRS and the read-only CSR.
96 The ISA-dependent CSR are only valid when the specific ISA is set. The
97 read-only CSR can not be written by the CSR instructions.
99 @cindex @samp{-mno-csr-check} option, RISC-V
101 Don't do CSR checking.
103 @cindex @samp{-mlittle-endian} option, RISC-V
104 @item -mlittle-endian
105 Generate code for a little endian machine.
107 @cindex @samp{-mbig-endian} option, RISC-V
109 Generate code for a big endian machine.
113 @node RISC-V-Directives
114 @section RISC-V Directives
115 @cindex machine directives, RISC-V
116 @cindex RISC-V machine directives
118 The following table lists all available RISC-V specific directives.
122 @cindex @code{align} directive
123 @item .align @var{size-log-2}
124 Align to the given boundary, with the size given as log2 the number of bytes to
127 @cindex Data directives
128 @item .half @var{value}
129 @itemx .word @var{value}
130 @itemx .dword @var{value}
131 Emits a half-word, word, or double-word value at the current position.
133 @cindex DTP-relative data directives
134 @item .dtprelword @var{value}
135 @itemx .dtpreldword @var{value}
136 Emits a DTP-relative word (or double-word) at the current position. This is
137 meant to be used by the compiler in shared libraries for DWARF debug info for
138 thread local variables.
140 @cindex BSS directive
142 Sets the current section to the BSS section.
144 @cindex LEB128 directives
145 @item .uleb128 @var{value}
146 @itemx .sleb128 @var{value}
147 Emits a signed or unsigned LEB128 value at the current position. This only
148 accepts constant expressions, because symbol addresses can change with
149 relaxation, and we don't support relocations to modify LEB128 values at link
152 @cindex Option directive
153 @cindex @code{option} directive
154 @item .option @var{argument}
155 Modifies RISC-V specific assembler options inline with the assembly code.
156 This is used when particular instruction sequences must be assembled with a
157 specific set of options. For example, since we relax addressing sequences to
158 shorter GP-relative sequences when possible the initial load of GP must not be
159 relaxed and should be emitted as something like
164 la gp, __global_pointer$
168 in order to produce after linker relaxation the expected
171 auipc gp, %pcrel_hi(__global_pointer$)
172 addi gp, gp, %pcrel_lo(__global_pointer$)
181 It's not expected that options are changed in this manner during regular use,
182 but there are a handful of esoteric cases like the one above where users need
183 to disable particular features of the assembler for particular code sequences.
184 The complete list of option arguments is shown below:
189 Pushes or pops the current option stack. These should be used whenever
190 changing an option in line with assembly code in order to ensure the user's
191 command-line options are respected for the bulk of the file being assembled.
195 Enables or disables the generation of compressed instructions. Instructions
196 are opportunistically compressed by the RISC-V assembler when possible, but
197 sometimes this behavior is not desirable, especially when handling alignments.
201 Enables or disables position-independent code generation. Unless you really
202 know what you're doing, this should only be at the top of a file.
206 Enables or disables relaxation. The RISC-V assembler and linker
207 opportunistically relax some code sequences, but sometimes this behavior is not
212 Enables or disables the CSR checking.
214 @item arch, @var{+extension[version]} [,...,@var{+extension_n[version_n]}]
215 @itemx arch, @var{-extension} [,...,@var{-extension_n}]
216 @itemx arch, @var{=ISA}
217 Enables or disables the extensions for specific code region. For example,
218 @samp{.option arch, +m2p0} means add m extension with version 2.0, and
219 @samp{.option arch, -f, -d} means remove extensions, f and d, from the
220 architecture string. Note that, @samp{.option arch, +c, -c} have the same
221 behavior as @samp{.option rvc, norvc}. However, they are also undesirable
222 sometimes. Besides, @samp{.option arch, -i} is illegal, since we cannot
223 remove the base i extension anytime. If you want to reset the whole ISA
224 string, you can also use @samp{.option arch, =rv32imac} to overwrite the
228 @cindex INSN directives
229 @item .insn @var{type}, @var{operand} [,...,@var{operand_n}]
230 @itemx .insn @var{insn_length}, @var{value}
231 @itemx .insn @var{value}
232 This directive permits the numeric representation of an instructions
233 and makes the assembler insert the operands according to one of the
234 instruction formats for @samp{.insn} (@ref{RISC-V-Formats}).
235 For example, the instruction @samp{add a0, a1, a2} could be written as
236 @samp{.insn r 0x33, 0, 0, a0, a1, a2}. But in fact, the instruction
237 formats are difficult to use for some users, so most of them are using
238 @samp{.word} to encode the instruction directly, rather than using
239 @samp{.insn}. It is fine for now, but will be wrong when the mapping
240 symbols are supported, since @samp{.word} will not be shown as an
241 instruction, it should be shown as data. Therefore, we also support
242 two more formats of the @samp{.insn}, the instruction @samp{add a0, a1, a2}
243 could also be written as @samp{.insn 0x4, 0xc58533} or @samp{.insn 0xc58533}.
244 When the @var{insn_length} is set, then assembler will check if the
245 @var{value} is a valid @var{insn_length} bytes instruction.
247 @cindex @code{.attribute} directive, RISC-V
248 @item .attribute @var{tag}, @var{value}
249 Set the object attribute @var{tag} to @var{value}.
251 The @var{tag} is either an attribute number, or one of the following:
252 @code{Tag_RISCV_arch}, @code{Tag_RISCV_stack_align},
253 @code{Tag_RISCV_unaligned_access}, @code{Tag_RISCV_priv_spec},
254 @code{Tag_RISCV_priv_spec_minor}, @code{Tag_RISCV_priv_spec_revision}.
258 @node RISC-V-Modifiers
259 @section RISC-V Assembler Modifiers
261 The RISC-V assembler supports following modifiers for relocatable addresses
262 used in RISC-V instruction operands. However, we also support some pseudo
263 instructions that are easier to use than these modifiers.
266 @item %lo(@var{symbol})
267 The low 12 bits of absolute address for @var{symbol}.
269 @item %hi(@var{symbol})
270 The high 20 bits of absolute address for @var{symbol}. This is usually
271 used with the %lo modifier to represent a 32-bit absolute address.
274 lui a0, %hi(@var{symbol}) // R_RISCV_HI20
275 addi a0, a0, %lo(@var{symbol}) // R_RISCV_LO12_I
277 lui a0, %hi(@var{symbol}) // R_RISCV_HI20
278 load/store a0, %lo(@var{symbol})(a0) // R_RISCV_LO12_I/S
281 @item %pcrel_lo(@var{label})
282 The low 12 bits of relative address between pc and @var{symbol}.
283 The @var{symbol} is related to the high part instruction which is marked
286 @item %pcrel_hi(@var{symbol})
287 The high 20 bits of relative address between pc and @var{symbol}.
288 This is usually used with the %pcrel_lo modifier to represent a +/-2GB
293 auipc a0, %pcrel_hi(@var{symbol}) // R_RISCV_PCREL_HI20
294 addi a0, a0, %pcrel_lo(@var{label}) // R_RISCV_PCREL_LO12_I
297 auipc a0, %pcrel_hi(@var{symbol}) // R_RISCV_PCREL_HI20
298 load/store a0, %pcrel_lo(@var{label})(a0) // R_RISCV_PCREL_LO12_I/S
301 Or you can use the pseudo lla/lw/sw/... instruction to do this.
307 @item %got_pcrel_hi(@var{symbol})
308 The high 20 bits of relative address between pc and the GOT entry of
309 @var{symbol}. This is usually used with the %pcrel_lo modifier to access
314 auipc a0, %got_pcrel_hi(@var{symbol}) // R_RISCV_GOT_HI20
315 addi a0, a0, %pcrel_lo(@var{label}) // R_RISCV_PCREL_LO12_I
318 auipc a0, %got_pcrel_hi(@var{symbol}) // R_RISCV_GOT_HI20
319 load/store a0, %pcrel_lo(@var{label})(a0) // R_RISCV_PCREL_LO12_I/S
322 Also, the pseudo la instruction with PIC has similar behavior.
324 @item %tprel_add(@var{symbol})
325 This is used purely to associate the R_RISCV_TPREL_ADD relocation for
326 TLS relaxation. This one is only valid as the fourth operand to the normally
327 3 operand add instruction.
329 @item %tprel_lo(@var{symbol})
330 The low 12 bits of relative address between tp and @var{symbol}.
332 @item %tprel_hi(@var{symbol})
333 The high 20 bits of relative address between tp and @var{symbol}. This is
334 usually used with the %tprel_lo and %tprel_add modifiers to access the thread
335 local variable @var{symbol} in TLS Local Exec.
338 lui a5, %tprel_hi(@var{symbol}) // R_RISCV_TPREL_HI20
339 add a5, a5, tp, %tprel_add(@var{symbol}) // R_RISCV_TPREL_ADD
340 load/store t0, %tprel_lo(@var{symbol})(a5) // R_RISCV_TPREL_LO12_I/S
343 @item %tls_ie_pcrel_hi(@var{symbol})
344 The high 20 bits of relative address between pc and GOT entry. It is
345 usually used with the %pcrel_lo modifier to access the thread local
346 variable @var{symbol} in TLS Initial Exec.
349 la.tls.ie a5, @var{symbol}
354 The pseudo la.tls.ie instruction can be expended to
358 auipc a5, %tls_ie_pcrel_hi(@var{symbol}) // R_RISCV_TLS_GOT_HI20
359 load a5, %pcrel_lo(@var{label})(a5) // R_RISCV_PCREL_LO12_I
362 @item %tls_gd_pcrel_hi(@var{symbol})
363 The high 20 bits of relative address between pc and GOT entry. It is
364 usually used with the %pcrel_lo modifier to access the thread local variable
365 @var{symbol} in TLS Global Dynamic.
368 la.tls.gd a0, @var{symbol}
369 call __tls_get_addr@@plt
374 The pseudo la.tls.gd instruction can be expended to
378 auipc a0, %tls_gd_pcrel_hi(@var{symbol}) // R_RISCV_TLS_GD_HI20
379 addi a0, a0, %pcrel_lo(@var{label}) // R_RISCV_PCREL_LO12_I
385 @section RISC-V Instruction Formats
386 @cindex instruction formats, risc-v
387 @cindex RISC-V instruction formats
389 The RISC-V Instruction Set Manual Volume I: User-Level ISA lists 15
390 instruction formats where some of the formats have multiple variants.
391 For the @samp{.insn} pseudo directive the assembler recognizes some
393 Typically, the most general variant of the instruction format is used
394 by the @samp{.insn} directive.
396 The following table lists the abbreviations used in the table of
400 @multitable @columnfractions .15 .40
401 @item opcode @tab Unsigned immediate or opcode name for 7-bits opcode.
402 @item opcode2 @tab Unsigned immediate or opcode name for 2-bits opcode.
403 @item func7 @tab Unsigned immediate for 7-bits function code.
404 @item func6 @tab Unsigned immediate for 6-bits function code.
405 @item func4 @tab Unsigned immediate for 4-bits function code.
406 @item func3 @tab Unsigned immediate for 3-bits function code.
407 @item func2 @tab Unsigned immediate for 2-bits function code.
408 @item rd @tab Destination register number for operand x, can be GPR or FPR.
409 @item rd' @tab Destination register number for operand x,
410 only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
411 @item rs1 @tab First source register number for operand x, can be GPR or FPR.
412 @item rs1' @tab First source register number for operand x,
413 only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
414 @item rs2 @tab Second source register number for operand x, can be GPR or FPR.
415 @item rs2' @tab Second source register number for operand x,
416 only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
417 @item simm12 @tab Sign-extended 12-bit immediate for operand x.
418 @item simm20 @tab Sign-extended 20-bit immediate for operand x.
419 @item simm6 @tab Sign-extended 6-bit immediate for operand x.
420 @item uimm5 @tab Unsigned 5-bit immediate for operand x.
421 @item uimm6 @tab Unsigned 6-bit immediate for operand x.
422 @item uimm8 @tab Unsigned 8-bit immediate for operand x.
423 @item symbol @tab Symbol or lable reference for operand x.
427 The following table lists all available opcode name:
433 Opcode space for compressed instructions.
436 Opcode space for load instructions.
439 Opcode space for floating-point load instructions.
442 Opcode space for store instructions.
445 Opcode space for floating-point store instructions.
448 Opcode space for auipc instruction.
451 Opcode space for lui instruction.
454 Opcode space for branch instructions.
457 Opcode space for jal instruction.
460 Opcode space for jalr instruction.
463 Opcode space for ALU instructions.
466 Opcode space for 32-bits ALU instructions.
469 Opcode space for ALU with immediate instructions.
472 Opcode space for 32-bits ALU with immediate instructions.
475 Opcode space for floating-point operation instructions.
478 Opcode space for madd instruction.
481 Opcode space for msub instruction.
484 Opcode space for nmadd instruction.
487 Opcode space for msub instruction.
490 Opcode space for atomic memory operation instructions.
493 Opcode space for misc instructions.
496 Opcode space for system instructions.
502 Opcode space for customize instructions.
506 An instruction is two or four bytes in length and must be aligned
507 on a 2 byte boundary. The first two bits of the instruction specify the
508 length of the instruction, 00, 01 and 10 indicates a two byte instruction,
509 11 indicates a four byte instruction.
511 The following table lists the RISC-V instruction formats that are available
512 with the @samp{.insn} pseudo directive:
515 @item R type: .insn r opcode6, func3, func7, rd, rs1, rs2
517 +-------+-----+-----+-------+----+---------+
518 | func7 | rs2 | rs1 | func3 | rd | opcode6 |
519 +-------+-----+-----+-------+----+---------+
523 @item R type with 4 register operands: .insn r opcode6, func3, func2, rd, rs1, rs2, rs3
524 @itemx R4 type: .insn r4 opcode6, func3, func2, rd, rs1, rs2, rs3
526 +-----+-------+-----+-----+-------+----+---------+
527 | rs3 | func2 | rs2 | rs1 | func3 | rd | opcode6 |
528 +-----+-------+-----+-----+-------+----+---------+
529 31 27 25 20 15 12 7 0
532 @item I type: .insn i opcode6, func3, rd, rs1, simm12
533 @itemx I type: .insn i opcode6, func3, rd, simm12(rs1)
535 +--------------+-----+-------+----+---------+
536 | simm12[11:0] | rs1 | func3 | rd | opcode6 |
537 +--------------+-----+-------+----+---------+
541 @item S type: .insn s opcode6, func3, rs2, simm12(rs1)
543 +--------------+-----+-----+-------+-------------+---------+
544 | simm12[11:5] | rs2 | rs1 | func3 | simm12[4:0] | opcode6 |
545 +--------------+-----+-----+-------+-------------+---------+
549 @item B type: .insn s opcode6, func3, rs1, rs2, symbol
550 @itemx SB type: .insn sb opcode6, func3, rs1, rs2, symbol
552 +-----------------+-----+-----+-------+----------------+---------+
553 | simm12[12|10:5] | rs2 | rs1 | func3 | simm12[4:1|11] | opcode6 |
554 +-----------------+-----+-----+-------+----------------+---------+
558 @item U type: .insn u opcode6, rd, simm20
560 +--------------------------+----+---------+
561 | simm20[20|10:1|11|19:12] | rd | opcode6 |
562 +--------------------------+----+---------+
566 @item J type: .insn j opcode6, rd, symbol
567 @itemx UJ type: .insn uj opcode6, rd, symbol
569 +------------+--------------+------------+---------------+----+---------+
570 | simm20[20] | simm20[10:1] | simm20[11] | simm20[19:12] | rd | opcode6 |
571 +------------+--------------+------------+---------------+----+---------+
575 @item CR type: .insn cr opcode2, func4, rd, rs2
577 +-------+--------+-----+---------+
578 | func4 | rd/rs1 | rs2 | opcode2 |
579 +-------+--------+-----+---------+
583 @item CI type: .insn ci opcode2, func3, rd, simm6
585 +-------+----------+--------+------------+---------+
586 | func3 | simm6[5] | rd/rs1 | simm6[4:0] | opcode2 |
587 +-------+----------+--------+------------+---------+
591 @item CIW type: .insn ciw opcode2, func3, rd', uimm8
593 +-------+------------+-----+---------+
594 | func3 | uimm8[7:0] | rd' | opcode2 |
595 +-------+-------- ---+-----+---------+
599 @item CSS type: .insn css opcode2, func3, rd, uimm6
601 +-------+------------+----+---------+
602 | func3 | uimm6[5:0] | rd | opcode2 |
603 +-------+------------+----+---------+
607 @item CL type: .insn cl opcode2, func3, rd', uimm5(rs1')
609 +-------+------------+------+------------+------+---------+
610 | func3 | uimm5[4:2] | rs1' | uimm5[1:0] | rd' | opcode2 |
611 +-------+------------+------+------------+------+---------+
615 @item CS type: .insn cs opcode2, func3, rs2', uimm5(rs1')
617 +-------+------------+------+------------+------+---------+
618 | func3 | uimm5[4:2] | rs1' | uimm5[1:0] | rs2' | opcode2 |
619 +-------+------------+------+------------+------+---------+
623 @item CA type: .insn ca opcode2, func6, func2, rd', rs2'
625 +-- ----+----------+-------+------+---------+
626 | func6 | rd'/rs1' | func2 | rs2' | opcode2 |
627 +-------+----------+-------+------+---------+
631 @item CB type: .insn cb opcode2, func3, rs1', symbol
633 +-------+--------------+------+------------------+---------+
634 | func3 | simm8[8|4:3] | rs1' | simm8[7:6|2:1|5] | opcode2 |
635 +-------+--------------+------+------------------+---------+
639 @item CJ type: .insn cj opcode2, symbol
641 +-------+-------------------------------+---------+
642 | func3 | simm11[11|4|9:8|10|6|7|3:1|5] | opcode2 |
643 +-------+-------------------------------+---------+
650 For the complete list of all instruction format variants see
651 The RISC-V Instruction Set Manual Volume I: User-Level ISA.
653 @node RISC-V-ATTRIBUTE
654 @section RISC-V Object Attribute
655 @cindex Object Attribute, RISC-V
657 RISC-V attributes have a string value if the tag number is odd and an integer
658 value if the tag number is even.
661 @item Tag_RISCV_stack_align (4)
662 Tag_RISCV_strict_align records the N-byte stack alignment for this object. The
663 default value is 16 for RV32I or RV64I, and 4 for RV32E.
665 The smallest value will be used if object files with different
666 Tag_RISCV_stack_align values are merged.
668 @item Tag_RISCV_arch (5)
669 Tag_RISCV_arch contains a string for the target architecture taken from the
670 option @option{-march}. Different architectures will be integrated into a
671 superset when object files are merged.
673 Note that the version information of the target architecture must be presented
674 explicitly in the attribute and abbreviations must be expanded. The version
675 information, if not given by @option{-march}, must be in accordance with the
676 default specified by the tool. For example, the architecture @code{RV32I} has
677 to be recorded in the attribute as @code{RV32I2P0} in which @code{2P0} stands
678 for the default version of its base ISA. On the other hand, the architecture
679 @code{RV32G} has to be presented as @code{RV32I2P0_M2P0_A2P0_F2P0_D2P0} in
680 which the abbreviation @code{G} is expanded to the @code{IMAFD} combination
681 with default versions of the standard extensions.
683 @item Tag_RISCV_unaligned_access (6)
684 Tag_RISCV_unaligned_access is 0 for files that do not allow any unaligned
685 memory accesses, and 1 for files that do allow unaligned memory accesses.
687 @item Tag_RISCV_priv_spec (8)
688 @item Tag_RISCV_priv_spec_minor (10)
689 @item Tag_RISCV_priv_spec_revision (12)
690 Tag_RISCV_priv_spec contains the major/minor/revision version information of
691 the privileged specification. It will report errors if object files of
692 different privileged specification versions are merged.