RISC-V: Add description for RISC-V Modifiers to as doc.
[deliverable/binutils-gdb.git] / gas / doc / c-riscv.texi
CommitLineData
b3adc24a 1@c Copyright (C) 2016-2020 Free Software Foundation, Inc.
e23eba97
NC
2@c This is part of the GAS anual.
3@c For copying conditions, see the file as.texinfo
4@c man end
5
6@ifset GENERIC
7@page
8@node RISC-V-Dependent
9@chapter RISC-V Dependent Features
10@end ifset
11@ifclear GENERIC
12@node Machine Dependencies
13@chapter RISC-V Dependent Features
14@end ifclear
15
16@cindex RISC-V support
17@menu
b57e49f7
JW
18* RISC-V-Options:: RISC-V Options
19* RISC-V-Directives:: RISC-V Directives
de48783e 20* RISC-V-Modifiers:: RISC-V Assembler Modifiers
0e35537d 21* RISC-V-Formats:: RISC-V Instruction Formats
2dc8dd17 22* RISC-V-ATTRIBUTE:: RISC-V Object Attribute
e23eba97
NC
23@end menu
24
b57e49f7
JW
25@node RISC-V-Options
26@section RISC-V Options
e23eba97 27
b57e49f7 28The following table lists all available RISC-V specific options.
e23eba97
NC
29
30@c man begin OPTIONS
31@table @gcctabopt
e23eba97 32
19683c04
PD
33@cindex @samp{-fpic} option, RISC-V
34@item -fpic
b57e49f7 35@itemx -fPIC
19683c04
PD
36Generate position-independent code
37
38@cindex @samp{-fno-pic} option, RISC-V
39@item -fno-pic
40Don't generate position-independent code (default)
41
4f7eddc4
PD
42@cindex @samp{-march=ISA} option, RISC-V
43@item -march=ISA
2922d21d
AW
44Select the base isa, as specified by ISA. For example -march=rv32ima.
45
46@cindex @samp{-mabi=ABI} option, RISC-V
47@item -mabi=ABI
48Selects the ABI, which is either "ilp32" or "lp64", optionally followed
49by "f", "d", or "q" to indicate single-precision, double-precision, or
50quad-precision floating-point calling convention, or none to indicate
7f999549
JW
51the soft-float calling convention. Also, "ilp32" can optionally be followed
52by "e" to indicate the RVE ABI, which is always soft-float.
e23eba97 53
71060565
JW
54@cindex @samp{-mrelax} option, RISC-V
55@item -mrelax
56Take advantage of linker relaxations to reduce the number of instructions
57required to materialize symbol addresses. (default)
58
59@cindex @samp{-mno-relax} option, RISC-V
60@item -mno-relax
61Don't do linker relaxations.
62
bd3380bc
NC
63@cindex @samp{-march-attr} option, RISC-V
64@item -march-attr
65Generate the default contents for the riscv elf attribute section if the
66.attribute directives are not set. This section is used to record the
67information that a linker or runtime loader needs to check compatibility.
68This information includes ISA string, stack alignment requirement, unaligned
69memory accesses, and the major, minor and revision version of privileged
70specification.
71
72@cindex @samp{-mno-arch-attr} option, RISC-V
73@item -mno-arch-attr
74Don't generate the default riscv elf attribute section if the .attribute
75directives are not set.
76
2ca89224
NC
77@cindex @samp{-mcsr-check} option, RISC-V
78@item -mcsr-check
79Enable the CSR checking for the ISA-dependent CRS and the read-only CSR.
80The ISA-dependent CSR are only valid when the specific ISA is set. The
81read-only CSR can not be written by the CSR instructions.
82
83@cindex @samp{-mno-csr-check} option, RISC-V
84@item -mno-csr-check
85Don't do CSR cheching.
e23eba97
NC
86@end table
87@c man end
b57e49f7
JW
88
89@node RISC-V-Directives
fed44c60 90@section RISC-V Directives
b57e49f7
JW
91@cindex machine directives, RISC-V
92@cindex RISC-V machine directives
93
b57e49f7
JW
94The following table lists all available RISC-V specific directives.
95
96@table @code
97
98@cindex @code{align} directive
99@item .align @var{size-log-2}
100Align to the given boundary, with the size given as log2 the number of bytes to
101align to.
102
103@cindex Data directives
104@item .half @var{value}
105@itemx .word @var{value}
106@itemx .dword @var{value}
107Emits a half-word, word, or double-word value at the current position.
108
109@cindex DTP-relative data directives
110@item .dtprelword @var{value}
111@itemx .dtpreldword @var{value}
112Emits a DTP-relative word (or double-word) at the current position. This is
113meant to be used by the compiler in shared libraries for DWARF debug info for
114thread local variables.
115
116@cindex BSS directive
117@item .bss
118Sets the current section to the BSS section.
119
120@cindex LEB128 directives
121@item .uleb128 @var{value}
122@itemx .sleb128 @var{value}
123Emits a signed or unsigned LEB128 value at the current position. This only
124accepts constant expressions, because symbol addresses can change with
125relaxation, and we don't support relocations to modify LEB128 values at link
126time.
127
128@cindex Option directive
129@cindex @code{option} directive
130@item .option @var{argument}
131Modifies RISC-V specific assembler options inline with the assembly code.
132This is used when particular instruction sequences must be assembled with a
133specific set of options. For example, since we relax addressing sequences to
134shorter GP-relative sequences when possible the initial load of GP must not be
135relaxed and should be emitted as something like
136
137@smallexample
138 .option push
139 .option norelax
140 la gp, __global_pointer$
141 .option pop
142@end smallexample
143
144in order to produce after linker relaxation the expected
145
146@smallexample
147 auipc gp, %pcrel_hi(__global_pointer$)
148 addi gp, gp, %pcrel_lo(__global_pointer$)
149@end smallexample
150
151instead of just
152
153@smallexample
154 addi gp, gp, 0
155@end smallexample
156
157It's not expected that options are changed in this manner during regular use,
158but there are a handful of esoteric cases like the one above where users need
159to disable particular features of the assembler for particular code sequences.
160The complete list of option arguments is shown below:
161
162@table @code
163@item push
164@itemx pop
165Pushes or pops the current option stack. These should be used whenever
166changing an option in line with assembly code in order to ensure the user's
167command-line options are respected for the bulk of the file being assembled.
168
169@item rvc
170@itemx norvc
171Enables or disables the generation of compressed instructions. Instructions
172are opportunistically compressed by the RISC-V assembler when possible, but
173sometimes this behavior is not desirable.
174
175@item pic
176@itemx nopic
177Enables or disables position-independent code generation. Unless you really
178know what you're doing, this should only be at the top of a file.
179
180@item relax
181@itemx norelax
182Enables or disables relaxation. The RISC-V assembler and linker
183opportunistically relax some code sequences, but sometimes this behavior is not
184desirable.
185@end table
186
2ca89224
NC
187@item csr-check
188@itemx no-csr-check
189Enables or disables the CSR checking.
190
0e35537d
JW
191@cindex INSN directives
192@item .insn @var{value}
193@itemx .insn @var{value}
194This directive permits the numeric representation of an instructions
195and makes the assembler insert the operands according to one of the
196instruction formats for @samp{.insn} (@ref{RISC-V-Formats}).
197For example, the instruction @samp{add a0, a1, a2} could be written as
198@samp{.insn r 0x33, 0, 0, a0, a1, a2}.
199
2dc8dd17
JW
200@cindex @code{.attribute} directive, RISC-V
201@item .attribute @var{tag}, @var{value}
202Set the object attribute @var{tag} to @var{value}.
203
204The @var{tag} is either an attribute number, or one of the following:
205@code{Tag_RISCV_arch}, @code{Tag_RISCV_stack_align},
206@code{Tag_RISCV_unaligned_access}, @code{Tag_RISCV_priv_spec},
207@code{Tag_RISCV_priv_spec_minor}, @code{Tag_RISCV_priv_spec_revision}.
208
b57e49f7 209@end table
0e35537d 210
de48783e
NC
211@node RISC-V-Modifiers
212@section RISC-V Assembler Modifiers
213
214The RISC-V assembler supports following modifiers for relocatable addresses
215used in RISC-V instruction operands. However, we also support some pseudo
216instructions that are easier to use than these modifiers.
217
218@table @code
219@item %lo(@var{symbol})
220The low 12 bits of absolute address for @var{symbol}.
221
222@item %hi(@var{symbol})
223The high 20 bits of absolute address for @var{symbol}. This is usually
224used with the %lo modifier to represent a 32-bit absolute address.
225
226@smallexample
227 lui a0, %hi(@var{symbol}) // R_RISCV_HI20
228 addi a0, a0, %lo(@var{symbol}) // R_RISCV_LO12_I
229
230 lui a0, %hi(@var{symbol}) // R_RISCV_HI20
231 load/store a0, %lo(@var{symbol})(a0) // R_RISCV_LO12_I/S
232@end smallexample
233
234@item %pcrel_lo(@var{label})
235The low 12 bits of relative address between pc and @var{symbol}.
236The @var{symbol} is related to the high part instruction which is marked
237by @var{label}.
238
239@item %pcrel_hi(@var{symbol})
240The high 20 bits of relative address between pc and @var{symbol}.
241This is usually used with the %pcrel_lo modifier to represent a +/-2GB
242pc-relative range.
243
244@smallexample
245@var{label}:
246 auipc a0, %pcrel_hi(@var{symbol}) // R_RISCV_PCREL_HI20
247 addi a0, a0, %pcrel_lo(@var{label}) // R_RISCV_PCREL_LO12_I
248
249@var{label}:
250 auipc a0, %pcrel_hi(@var{symbol}) // R_RISCV_PCREL_HI20
251 load/store a0, %pcrel_lo(@var{label})(a0) // R_RISCV_PCREL_LO12_I/S
252@end smallexample
253
254Or you can use the pseudo lla/lw/sw/... instruction to do this.
255
256@smallexample
257 lla a0, @var{symbol}
258@end smallexample
259
260@item %tprel_add(@var{symbol})
261This is used purely to associate the R_RISCV_TPREL_ADD relocation for
262TLS relaxation. This one is only valid as the fourth operand to the normally
2633 operand add instruction.
264
265@item %tprel_lo(@var{symbol})
266The low 12 bits of relative address between tp and @var{symbol}.
267
268@item %tprel_hi(@var{symbol})
269The high 20 bits of relative address between tp and @var{symbol}. This is
270usually used with the %tprel_lo and %tprel_add modifiers to access the thread
271local variable @var{symbol} in TLS Local Exec.
272
273@smallexample
274 lui a5, %tprel_hi(@var{symbol}) // R_RISCV_TPREL_HI20
275 add a5, a5, tp, %tprel_add(@var{symbol}) // R_RISCV_TPREL_ADD
276 load/store t0, %tprel_lo(@var{symbol})(a5) // R_RISCV_TPREL_LO12_I/S
277@end smallexample
278
279@item %tls_ie_pcrel_hi(@var{symbol})
280The high 20 bits of relative address between pc and GOT entry. It is
281usually used with the %pcrel_lo modifier to access the thread local
282variable @var{symbol} in TLS Initial Exec.
283
284@smallexample
285 la.tls.ie a5, @var{symbol}
286 add a5, a5, tp
287 load/store t0, 0(a5)
288@end smallexample
289
290The pseudo la.tls.ie instruction can be expended to
291
292@smallexample
293@var{label}:
294 auipc a5, %tls_ie_pcrel_hi(@var{symbol}) // R_RISCV_TLS_GOT_HI20
295 load a5, %pcrel_lo(@var{label})(a5) // R_RISCV_PCREL_LO12_I
296@end smallexample
297
298@item %tls_gd_pcrel_hi(@var{symbol})
299The high 20 bits of relative address between pc and GOT entry. It is
300usually used with the %pcrel_lo modifier to access the thread local variable
301@var{symbol} in TLS Global Dynamic.
302
303@smallexample
304 la.tls.gd a0, @var{symbol}
305 call __tls_get_addr@@plt
306 mv a5, a0
307 load/store t0, 0(a5)
308@end smallexample
309
310The pseudo la.tls.gd instruction can be expended to
311
312@smallexample
313@var{label}:
314 auipc a0, %tls_gd_pcrel_hi(@var{symbol}) // R_RISCV_TLS_GD_HI20
315 addi a0, a0, %pcrel_lo(@var{label}) // R_RISCV_PCREL_LO12_I
316@end smallexample
317
318@end table
319
0e35537d 320@node RISC-V-Formats
de48783e 321@section RISC-V Instruction Formats
0e35537d
JW
322@cindex instruction formats, risc-v
323@cindex RISC-V instruction formats
324
325The RISC-V Instruction Set Manual Volume I: User-Level ISA lists 12
326instruction formats where some of the formats have multiple variants.
327For the @samp{.insn} pseudo directive the assembler recognizes some
328of the formats.
329Typically, the most general variant of the instruction format is used
330by the @samp{.insn} directive.
331
332The following table lists the abbreviations used in the table of
333instruction formats:
334
335@display
336@multitable @columnfractions .15 .40
337@item opcode @tab Unsigned immediate or opcode name for 7-bits opcode.
338@item opcode2 @tab Unsigned immediate or opcode name for 2-bits opcode.
339@item func7 @tab Unsigned immediate for 7-bits function code.
4765cd61 340@item func6 @tab Unsigned immediate for 6-bits function code.
0e35537d
JW
341@item func4 @tab Unsigned immediate for 4-bits function code.
342@item func3 @tab Unsigned immediate for 3-bits function code.
343@item func2 @tab Unsigned immediate for 2-bits function code.
344@item rd @tab Destination register number for operand x, can be GPR or FPR.
345@item rd' @tab Destination register number for operand x,
346only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
347@item rs1 @tab First source register number for operand x, can be GPR or FPR.
348@item rs1' @tab First source register number for operand x,
349only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
350@item rs2 @tab Second source register number for operand x, can be GPR or FPR.
351@item rs2' @tab Second source register number for operand x,
352only accept s0-s1, a0-a5, fs0-fs1 and fa0-fa5.
353@item simm12 @tab Sign-extended 12-bit immediate for operand x.
354@item simm20 @tab Sign-extended 20-bit immediate for operand x.
355@item simm6 @tab Sign-extended 6-bit immediate for operand x.
356@item uimm8 @tab Unsigned 8-bit immediate for operand x.
357@item symbol @tab Symbol or lable reference for operand x.
358@end multitable
359@end display
360
361The following table lists all available opcode name:
362
363@table @code
364@item C0
365@item C1
366@item C2
367Opcode space for compressed instructions.
368
369@item LOAD
370Opcode space for load instructions.
371
372@item LOAD_FP
373Opcode space for floating-point load instructions.
374
375@item STORE
376Opcode space for store instructions.
377
378@item STORE_FP
379Opcode space for floating-point store instructions.
380
381@item AUIPC
382Opcode space for auipc instruction.
383
384@item LUI
385Opcode space for lui instruction.
386
387@item BRANCH
388Opcode space for branch instructions.
389
390@item JAL
391Opcode space for jal instruction.
392
393@item JALR
394Opcode space for jalr instruction.
395
396@item OP
397Opcode space for ALU instructions.
398
399@item OP_32
400Opcode space for 32-bits ALU instructions.
401
402@item OP_IMM
403Opcode space for ALU with immediate instructions.
404
405@item OP_IMM_32
406Opcode space for 32-bits ALU with immediate instructions.
407
408@item OP_FP
409Opcode space for floating-point operation instructions.
410
411@item MADD
412Opcode space for madd instruction.
413
414@item MSUB
415Opcode space for msub instruction.
416
417@item NMADD
418Opcode space for nmadd instruction.
419
420@item NMSUB
421Opcode space for msub instruction.
422
423@item AMO
424Opcode space for atomic memory operation instructions.
425
98602811 426@item MISC_MEM
0e35537d
JW
427Opcode space for misc instructions.
428
429@item SYSTEM
430Opcode space for system instructions.
431
432@item CUSTOM_0
433@item CUSTOM_1
434@item CUSTOM_2
435@item CUSTOM_3
436Opcode space for customize instructions.
437
438@end table
439
440An instruction is two or four bytes in length and must be aligned
441on a 2 byte boundary. The first two bits of the instruction specify the
442length of the instruction, 00, 01 and 10 indicates a two byte instruction,
44311 indicates a four byte instruction.
444
445The following table lists the RISC-V instruction formats that are available
446with the @samp{.insn} pseudo directive:
447
448@table @code
449@item R type: .insn r opcode, func3, func7, rd, rs1, rs2
450@verbatim
451+-------+-----+-----+-------+----+-------------+
452| func7 | rs2 | rs1 | func3 | rd | opcode |
453+-------+-----+-----+-------+----+-------------+
45431 25 20 15 12 7 0
455@end verbatim
456
457@item R type with 4 register operands: .insn r opcode, func3, func2, rd, rs1, rs2, rs3
98602811 458@itemx R4 type: .insn r4 opcode, func3, func2, rd, rs1, rs2, rs3
0e35537d
JW
459@verbatim
460+-----+-------+-----+-----+-------+----+-------------+
461| rs3 | func2 | rs2 | rs1 | func3 | rd | opcode |
462+-----+-------+-----+-----+-------+----+-------------+
46331 27 25 20 15 12 7 0
464@end verbatim
465
466@item I type: .insn i opcode, func3, rd, rs1, simm12
467@verbatim
468+-------------+-----+-------+----+-------------+
469| simm12 | rs1 | func3 | rd | opcode |
470+-------------+-----+-------+----+-------------+
47131 20 15 12 7 0
472@end verbatim
473
474@item S type: .insn s opcode, func3, rd, rs1, simm12
475@verbatim
476+--------------+-----+-----+-------+-------------+-------------+
477| simm12[11:5] | rs2 | rs1 | func3 | simm12[4:0] | opcode |
478+--------------+-----+-----+-------+-------------+-------------+
47931 25 20 15 12 7 0
480@end verbatim
481
482@item SB type: .insn sb opcode, func3, rd, rs1, symbol
483@itemx SB type: .insn sb opcode, func3, rd, simm12(rs1)
98602811
JW
484@itemx B type: .insn s opcode, func3, rd, rs1, symbol
485@itemx B type: .insn s opcode, func3, rd, simm12(rs1)
0e35537d 486@verbatim
98602811
JW
487+------------+--------------+-----+-----+-------+-------------+-------------+--------+
488| simm12[12] | simm12[10:5] | rs2 | rs1 | func3 | simm12[4:1] | simm12[11]] | opcode |
489+------------+--------------+-----+-----+-------+-------------+-------------+--------+
49031 30 25 20 15 12 7 0
0e35537d
JW
491@end verbatim
492
493@item U type: .insn u opcode, rd, simm20
494@verbatim
495+---------------------------+----+-------------+
496| simm20 | rd | opcode |
497+---------------------------+----+-------------+
49831 12 7 0
499@end verbatim
500
501@item UJ type: .insn uj opcode, rd, symbol
98602811 502@itemx J type: .insn j opcode, rd, symbol
0e35537d
JW
503@verbatim
504+------------+--------------+------------+---------------+----+-------------+
505| simm20[20] | simm20[10:1] | simm20[11] | simm20[19:12] | rd | opcode |
506+------------+--------------+------------+---------------+----+-------------+
50731 30 21 20 12 7 0
508@end verbatim
509
4765cd61 510@item CR type: .insn cr opcode2, func4, rd, rs2
0e35537d
JW
511@verbatim
512+---------+--------+-----+---------+
513| func4 | rd/rs1 | rs2 | opcode2 |
514+---------+--------+-----+---------+
51515 12 7 2 0
516@end verbatim
517
518@item CI type: .insn ci opcode2, func3, rd, simm6
519@verbatim
520+---------+-----+--------+-----+---------+
521| func3 | imm | rd/rs1 | imm | opcode2 |
522+---------+-----+--------+-----+---------+
52315 13 12 7 2 0
524@end verbatim
525
526@item CIW type: .insn ciw opcode2, func3, rd, uimm8
527@verbatim
528+---------+--------------+-----+---------+
529| func3 | imm | rd' | opcode2 |
530+---------+--------------+-----+---------+
53115 13 7 2 0
532@end verbatim
533
4765cd61
JW
534@item CA type: .insn ca opcode2, func6, func2, rd, rs2
535@verbatim
536+---------+----------+-------+------+--------+
537| func6 | rd'/rs1' | func2 | rs2' | opcode |
538+---------+----------+-------+------+--------+
53915 10 7 5 2 0
540@end verbatim
541
0e35537d
JW
542@item CB type: .insn cb opcode2, func3, rs1, symbol
543@verbatim
544+---------+--------+------+--------+---------+
545| func3 | offset | rs1' | offset | opcode2 |
546+---------+--------+------+--------+---------+
54715 13 10 7 2 0
548@end verbatim
549
550@item CJ type: .insn cj opcode2, symbol
551@verbatim
552+---------+--------------------+---------+
553| func3 | jump target | opcode2 |
554+---------+--------------------+---------+
55515 13 7 2 0
556@end verbatim
557
558
559@end table
560
561For the complete list of all instruction format variants see
562The RISC-V Instruction Set Manual Volume I: User-Level ISA.
2dc8dd17
JW
563
564@node RISC-V-ATTRIBUTE
565@section RISC-V Object Attribute
566@cindex Object Attribute, RISC-V
567
568RISC-V attributes have a string value if the tag number is odd and an integer
569value if the tag number is even.
570
571@table @r
572@item Tag_RISCV_stack_align (4)
573Tag_RISCV_strict_align records the N-byte stack alignment for this object. The
574default value is 16 for RV32I or RV64I, and 4 for RV32E.
575
576The smallest value will be used if object files with different
577Tag_RISCV_stack_align values are merged.
578
579@item Tag_RISCV_arch (5)
580Tag_RISCV_arch contains a string for the target architecture taken from the
581option @option{-march}. Different architectures will be integrated into a
582superset when object files are merged.
583
584Note that the version information of the target architecture must be presented
585explicitly in the attribute and abbreviations must be expanded. The version
586information, if not given by @option{-march}, must be in accordance with the
587default specified by the tool. For example, the architecture @code{RV32I} has
588to be recorded in the attribute as @code{RV32I2P0} in which @code{2P0} stands
589for the default version of its base ISA. On the other hand, the architecture
590@code{RV32G} has to be presented as @code{RV32I2P0_M2P0_A2P0_F2P0_D2P0} in
591which the abbreviation @code{G} is expanded to the @code{IMAFD} combination
592with default versions of the standard extensions.
593
594@item Tag_RISCV_unaligned_access (6)
595Tag_RISCV_unaligned_access is 0 for files that do not allow any unaligned
596memory accesses, and 1 for files that do allow unaligned memory accesses.
597
598@item Tag_RISCV_priv_spec (8)
599@item Tag_RISCV_priv_spec_minor (10)
600@item Tag_RISCV_priv_spec_revision (12)
601Tag_RISCV_priv_spec contains the major/minor/revision version information of
602the privileged specification. It will report errors if object files of
603different privileged specification versions are merged.
604
605@end table
This page took 0.187595 seconds and 4 git commands to generate.