Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 040fc58

Browse files
committedFeb 10, 2025
Differentiate "assembly code" and "asm block".
The latter now only means the entire block, while the former means the assembly code specified within the block (thus excluding `label` blocks).
1 parent 1f3607e commit 040fc58

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed
 

‎src/inline-assembly.md

+33-33
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ assert_eq!(y, [3, 2, 0, 1]);
356356

357357
r[asm.operand-type.supported-operands.label]
358358
* `label <block>`
359-
- The address of the block is substituted into the asm template string. The assembly block may jump to the substituted address.
359+
- The address of the block is substituted into the asm template string. The assembly code may jump to the substituted address.
360360
- After execution of the block, the `asm!` expression returns.
361361
- The type of the block must be unit or `!` (never).
362362
- The block starts a new safety context; despite the outer `unsafe` block needed for `asm!`, unsafe operations within the `label` block must be wrapped in an inner `unsafe` block.
@@ -749,7 +749,7 @@ Some registers cannot be used for input or output operands:
749749

750750
| Architecture | Unsupported register | Reason |
751751
| ------------ | -------------------- | ------ |
752-
| All | `sp`, `r15` (s390x) | The stack pointer must be restored to its original value at the end of an asm code block. |
752+
| All | `sp`, `r15` (s390x) | The stack pointer must be restored to its original value at the end of an assembly code. |
753753
| All | `bp` (x86), `x29` (AArch64 and Arm64EC), `x8` (RISC-V), `$fp` (LoongArch), `r11` (s390x) | The frame pointer cannot be used as an input or output. |
754754
| ARM | `r7` or `r11` | On ARM the frame pointer can be either `r7` or `r11` depending on the target. The frame pointer cannot be used as an input or output. |
755755
| All | `si` (x86-32), `bx` (x86-64), `r6` (ARM), `x19` (AArch64 and Arm64EC), `x9` (RISC-V), `$s8` (LoongArch) | This is used internally by LLVM as a "base pointer" for functions with complex stack frames. |
@@ -873,7 +873,7 @@ r[asm.abi-clobbers]
873873
## ABI clobbers
874874

875875
r[asm.abi-clobbers.intro]
876-
The `clobber_abi` keyword can be used to apply a default set of clobbers to an `asm!` block.
876+
The `clobber_abi` keyword can be used to apply a default set of clobbers to the assembly code.
877877
This will automatically insert the necessary clobber constraints as needed for calling a function with a particular calling convention: if the calling convention does not fully preserve the value of a register across a call then `lateout("...") _` is implicitly added to the operands list (where the `...` is replaced by the register's name).
878878

879879
```rust
@@ -966,12 +966,12 @@ r[asm.options]
966966
## Options
967967

968968
r[asm.options.supported-options]
969-
Flags are used to further influence the behavior of the inline assembly block.
969+
Flags are used to further influence the behavior of the inline assembly code.
970970
Currently the following options are defined:
971971

972972
r[asm.options.supported-options.pure]
973-
- `pure`: The `asm!` block has no side effects, must eventually return, and its outputs depend only on its direct inputs (i.e. the values themselves, not what they point to) or values read from memory (unless the `nomem` options is also set).
974-
This allows the compiler to execute the `asm!` block fewer times than specified in the program (e.g. by hoisting it out of a loop) or even eliminate it entirely if the outputs are not used.
973+
- `pure`: The assembly code has no side effects, must eventually return, and its outputs depend only on its direct inputs (i.e. the values themselves, not what they point to) or values read from memory (unless the `nomem` options is also set).
974+
This allows the compiler to execute the assembly code fewer times than specified in the program (e.g. by hoisting it out of a loop) or even eliminate it entirely if the outputs are not used.
975975
The `pure` option must be combined with either the `nomem` or `readonly` options, otherwise a compile-time error is emitted.
976976

977977
```rust
@@ -998,9 +998,9 @@ assert_eq!(z, 0);
998998
```
999999

10001000
r[asm.options.supported-options.nomem]
1001-
- `nomem`: The `asm!` block does not read from or write to any memory accessible outside of the `asm!` block.
1002-
This allows the compiler to cache the values of modified global variables in registers across the `asm!` block since it knows that they are not read or written to by the `asm!`.
1003-
The compiler also assumes that this `asm!` block does not perform any kind of synchronization with other threads, e.g. via fences.
1001+
- `nomem`: The assembly code does not read from or write to any memory accessible outside of the assembly code.
1002+
This allows the compiler to cache the values of modified global variables in registers across the assembly code since it knows that they are not read or written to by the `asm!`.
1003+
The compiler also assumes that the assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
10041004

10051005
<!-- no_run: This test has unpredictable or undefined behavior at runtime -->
10061006
```rust,no_run
@@ -1044,9 +1044,9 @@ assert_eq!(z, 1);
10441044
```
10451045

10461046
r[asm.options.supported-options.readonly]
1047-
- `readonly`: The `asm!` block does not write to any memory accessible outside of the `asm!` block.
1048-
This allows the compiler to cache the values of unmodified global variables in registers across the `asm!` block since it knows that they are not written to by the `asm!`.
1049-
The compiler also assumes that this `asm!` block does not perform any kind of synchronization with other threads, e.g. via fences.
1047+
- `readonly`: The assembly code does not write to any memory accessible outside of the assembly code.
1048+
This allows the compiler to cache the values of unmodified global variables in registers across the assembly code since it knows that they are not written to by the `asm!`.
1049+
The compiler also assumes that this assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
10501050

10511051
<!-- no_run: This test has undefined behaviour at runtime -->
10521052
```rust,no_run
@@ -1090,14 +1090,14 @@ assert_eq!(z, 1);
10901090
```
10911091

10921092
r[asm.options.supported-options.preserves_flags]
1093-
- `preserves_flags`: The `asm!` block does not modify the flags register (defined in the rules below).
1094-
This allows the compiler to avoid recomputing the condition flags after the `asm!` block.
1093+
- `preserves_flags`: The assembly code does not modify the flags register (defined in the rules below).
1094+
This allows the compiler to avoid recomputing the condition flags after the assembly code.
10951095

10961096
r[asm.options.supported-options.noreturn]
1097-
- `noreturn`: The `asm!` block never returns, and its return type is defined as `!` (never).
1097+
- `noreturn`: The assembly code never returns, and its return type is defined as `!` (never).
10981098
Behavior is undefined if execution falls through past the end of the asm code.
10991099
A `noreturn` asm block behaves just like a function which doesn't return; notably, local variables in scope are not dropped before it is invoked.
1100-
- When any `label` blocks are present, `noreturn` means the execution of the `asm!` block never falls through; the asm block may only exit by jumping to one of the specified blocks.
1100+
- When any `label` blocks are present, `noreturn` means the execution of the assembly code never falls through; the assembly code may only exit by jumping to one of the specified blocks.
11011101
The entire `asm!` block will have unit type in this case, unless all `label` blocks diverge, in which case the return type is `!`.
11021102

11031103
<!-- no_run: This test aborts at runtime -->
@@ -1120,7 +1120,7 @@ unsafe { core::arch::asm!("", options(noreturn)); }
11201120
```
11211121

11221122
r[asm.options.supported-options.nostack]
1123-
- `nostack`: The `asm!` block does not push data to the stack, or write to the stack red-zone (if supported by the target).
1123+
- `nostack`: The assembly code does not push data to the stack, or write to the stack red-zone (if supported by the target).
11241124
If this option is *not* used then the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
11251125

11261126
<!-- no_run: Test has undefined behavior at runtime -->
@@ -1217,27 +1217,27 @@ r[asm.rules.intro]
12171217
To avoid undefined behavior, these rules must be followed when using function-scope inline assembly (`asm!`):
12181218

12191219
r[asm.rules.reg-not-input]
1220-
- Any registers not specified as inputs will contain an undefined value on entry to the asm block.
1220+
- Any registers not specified as inputs will contain an undefined value on entry to the assembly code.
12211221
- An "undefined value" in the context of inline assembly means that the register can (non-deterministically) have any one of the possible values allowed by the architecture.
12221222
Notably it is not the same as an LLVM `undef` which can have a different value every time you read it (since such a concept does not exist in assembly code).
12231223

12241224
r[asm.rules.reg-not-output]
1225-
- Any registers not specified as outputs must have the same value upon exiting the asm block as they had on entry, otherwise behavior is undefined.
1225+
- Any registers not specified as outputs must have the same value upon exiting the assembly code as they had on entry, otherwise behavior is undefined.
12261226
- This only applies to registers which can be specified as an input or output.
12271227
Other registers follow target-specific rules.
12281228
- Note that a `lateout` may be allocated to the same register as an `in`, in which case this rule does not apply.
12291229
Code should not rely on this however since it depends on the results of register allocation.
12301230

12311231
r[asm.rules.unwind]
1232-
- Behavior is undefined if execution unwinds out of an asm block.
1232+
- Behavior is undefined if execution unwinds out of the assembly code.
12331233
- This also applies if the assembly code calls a function which then unwinds.
12341234

12351235
r[asm.rules.mem-same-as-ffi]
12361236
- The set of memory locations that assembly code is allowed to read and write are the same as those allowed for an FFI function.
12371237
- Refer to the unsafe code guidelines for the exact rules.
12381238
- If the `readonly` option is set, then only memory reads are allowed.
12391239
- If the `nomem` option is set then no reads or writes to memory are allowed.
1240-
- These rules do not apply to memory which is private to the asm code, such as stack space allocated within the asm block.
1240+
- These rules do not apply to memory which is private to the asm code, such as stack space allocated within the assembly code.
12411241

12421242
r[asm.rules.black-box]
12431243
- The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed.
@@ -1247,22 +1247,22 @@ r[asm.rules.black-box]
12471247

12481248
r[asm.rules.stack-below-sp]
12491249
- Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer.
1250-
- On entry to the asm block the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
1250+
- On entry to the assembly code the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
12511251
- You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page).
12521252
- You should adjust the stack pointer when allocating stack memory as required by the target ABI.
1253-
- The stack pointer must be restored to its original value before leaving the asm block.
1253+
- The stack pointer must be restored to its original value before leaving the assembly code.
12541254

12551255
r[asm.rules.noreturn]
1256-
- If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the asm block.
1256+
- If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the assembly code.
12571257

12581258
r[asm.rules.pure]
12591259
- If the `pure` option is set then behavior is undefined if the `asm!` has side-effects other than its direct outputs.
12601260
Behavior is also undefined if two executions of the `asm!` code with the same inputs result in different outputs.
12611261
- When used with the `nomem` option, "inputs" are just the direct inputs of the `asm!`.
1262-
- When used with the `readonly` option, "inputs" comprise the direct inputs of the `asm!` and any memory that the `asm!` block is allowed to read.
1262+
- When used with the `readonly` option, "inputs" comprise the direct inputs of the `asm!` and any memory that the assembly code is allowed to read.
12631263

12641264
r[asm.rules.preserved-registers]
1265-
- These flags registers must be restored upon exiting the asm block if the `preserves_flags` option is set:
1265+
- These flags registers must be restored upon exiting the assembly code if the `preserves_flags` option is set:
12661266
- x86
12671267
- Status flags in `EFLAGS` (CF, PF, AF, ZF, SF, OF).
12681268
- Floating-point status word (all).
@@ -1286,12 +1286,12 @@ r[asm.rules.preserved-registers]
12861286
- The condition code register `cc`.
12871287

12881288
r[asm.rules.x86-df]
1289-
- On x86, the direction flag (DF in `EFLAGS`) is clear on entry to an asm block and must be clear on exit.
1290-
- Behavior is undefined if the direction flag is set on exiting an asm block.
1289+
- On x86, the direction flag (DF in `EFLAGS`) is clear on entry to the assembly code and must be clear on exit.
1290+
- Behavior is undefined if the direction flag is set on exiting the assembly code.
12911291

12921292
r[asm.rules.x86-x87]
12931293
- On x86, the x87 floating-point register stack must remain unchanged unless all of the `st([0-7])` registers have been marked as clobbered with `out("st(0)") _, out("st(1)") _, ...`.
1294-
- If all x87 registers are clobbered then the x87 register stack is guaranteed to be empty upon entering an `asm` block. Assembly code must ensure that the x87 register stack is also empty when exiting the asm block.
1294+
- If all x87 registers are clobbered then the x87 register stack is guaranteed to be empty upon entering the assembly code. Assembly code must ensure that the x87 register stack is also empty when exiting the asssembly code.
12951295

12961296
```rust
12971297
# #[cfg(target_arch = "x86_64")]
@@ -1330,8 +1330,8 @@ r[asm.rules.arm64ec]
13301330
- On arm64ec, [call checkers with appropriate thunks](https://learn.microsoft.com/en-us/windows/arm/arm64ec-abi#authoring-arm64ec-in-assembly) are mandatory when calling functions.
13311331

13321332
r[asm.rules.only-on-exit]
1333-
- The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting an `asm!` block.
1334-
- This means that `asm!` blocks that never return (even if not marked `noreturn`) don't need to preserve these registers.
1333+
- The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting the assembly code.
1334+
- This means that assembly code that never return (even if not marked `noreturn`) don't need to preserve these registers.
13351335
- When returning to a different `asm!` block than you entered (e.g. for context switching), these registers must contain the value they had upon entering the `asm!` block that you are *exiting*.
13361336
- You cannot exit an `asm!` block that has not been entered.
13371337
Neither can you exit an `asm!` block that has already been exited (without first entering it again).
@@ -1387,7 +1387,7 @@ Inline assembly supports a subset of the directives supported by both GNU AS and
13871387
The result of using other directives is assembler-specific (and may cause an error, or may be accepted as-is).
13881388

13891389
r[asm.directives.stateful]
1390-
If inline assembly includes any "stateful" directive that modifies how subsequent assembly is processed, the block must undo the effects of any such directives before the inline assembly ends.
1390+
If inline assembly includes any "stateful" directive that modifies how subsequent assembly is processed, the assembly code must undo the effects of any such directives before the inline assembly ends.
13911391

13921392
r[asm.directives.supported-directives]
13931393
The following directives are guaranteed to be supported by the assembler:
@@ -1514,7 +1514,7 @@ On x86 targets, both 32-bit and 64-bit, the following additional directives are
15141514
- `.code32`
15151515
- `.code64`
15161516

1517-
Use of `.code16`, `.code32`, and `.code64` directives are only supported if the state is reset to the default before exiting the assembly block.
1517+
Use of `.code16`, `.code32`, and `.code64` directives are only supported if the state is reset to the default before exiting the assembly code.
15181518
32-bit x86 uses `.code32` by default, and x86_64 uses `.code64` by default.
15191519

15201520
r[asm.target-specific-directives.arm-32-bit]

0 commit comments

Comments
 (0)
Failed to load comments.