You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's make some clarifications of substance and editorial tweaks to
the changes for `asm_goto`. We add an example, we change various
wording to be more clear -- in particular, we significantly revise the
section on `noreturn` to unify the statement of the rules -- and we
replace remaining uses of "asm code" with "assembly code" to match our
verbiage elsewhere.
The main clarification of substance is that for requirements that must
be upheld when falling through the assembly, such as he requirement to
restore the stack pointer, we must also note that the requirement must
be satisfied before jumping to any `label` blocks.
-`<reg>` can refer to a register class or an explicit register.
241
241
The allocated register name is substituted into the asm template string.
242
-
- The allocated register will contain an undefined value at the start of the asm code.
243
-
-`<expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the asm code.
244
-
- An underscore (`_`) may be specified instead of an expression, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber).
242
+
- The allocated register will contain an undefined value at the start of the assembly code.
243
+
-`<expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the assembly code.
244
+
- An underscore (`_`) may be specified instead of an expression, which will cause the contents of the register to be discarded at the end of the assembly code (effectively acting as a clobber).
- Same as `inout` except that the initial value of the register is taken from the value of `<in expr>`.
289
-
-`<out expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the asm code.
290
-
- An underscore (`_`) may be specified instead of an expression for `<out expr>`, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber).
289
+
-`<out expr>` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the assembly code.
290
+
- An underscore (`_`) may be specified instead of an expression for `<out expr>`, which will cause the contents of the register to be discarded at the end of the assembly code (effectively acting as a clobber).
291
291
-`<in expr>` and `<out expr>` may have different types.
- A mangled symbol name referring to the item is substituted into the asm template string.
320
320
- The substituted string does not include any modifiers (e.g. GOT, PLT, relocations, etc).
321
-
-`<path>` is allowed to point to a `#[thread_local]` static, in which case the asm code can combine the symbol with relocations (e.g. `@plt`, `@TPOFF`) to read from thread-local data.
321
+
-`<path>` is allowed to point to a `#[thread_local]` static, in which case the assembly code can combine the symbol with relocations (e.g. `@plt`, `@TPOFF`) to read from thread-local data.
- The address of the block is substituted into the asm template string. The assembly code may jump to the substituted address.
360
360
- After execution of the block, the `asm!` expression returns.
361
361
- The type of the block must be unit or `!` (never).
362
-
- The block starts a new safety context: unsafe operations within the `label` block must be wrapped in an inner `unsafe` block, even though the entire `asm!`statement is already wrapped in `unsafe`.
362
+
- The block starts a new safety context; unsafe operations within the `label` block must be wrapped in an inner `unsafe` block, even though the entire `asm!`expression is already wrapped in `unsafe`.
363
363
364
364
```rust
365
365
# #[cfg(target_arch ="x86_64")]
@@ -370,7 +370,6 @@ unsafe {
370
370
}
371
371
```
372
372
373
-
374
373
r[asm.operand-type.left-to-right]
375
374
Operand expressions are evaluated from left to right, just like function call arguments.
376
375
After the `asm!` has executed, outputs are written to in left to right order.
@@ -749,7 +748,7 @@ Some registers cannot be used for input or output operands:
749
748
750
749
| Architecture | Unsupported register | Reason |
751
750
| ------------ | -------------------- | ------ |
752
-
| All |`sp`, `r15` (s390x) | The stack pointer must be restored to its original value at the end of an assembly code. |
751
+
| All |`sp`, `r15` (s390x) | The stack pointer must be restored to its original value at the end of the assembly code or before jumping to a `label` block. |
753
752
| 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. |
754
753
| 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. |
755
754
| 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. |
@@ -863,7 +862,7 @@ assert_eq!(x, 0x1000u16);
863
862
864
863
r[asm.template-modifiers.smaller-value]
865
864
As stated in the previous section, passing an input value smaller than the register width will result in the upper bits of the register containing undefined values.
866
-
This is not a problem if the inline asm only accesses the lower bits of the register, which can be done by using a template modifier to use a subregister name in the asm code (e.g. `ax` instead of `rax`).
865
+
This is not a problem if the inline asm only accesses the lower bits of the register, which can be done by using a template modifier to use a subregister name in the assembly code (e.g. `ax` instead of `rax`).
867
866
Since this an easy pitfall, the compiler will suggest a template modifier to use where appropriate given the input type.
868
867
If all references to an operand already have modifiers then the warning is suppressed for that operand.
869
868
@@ -999,15 +998,16 @@ assert_eq!(z, 0);
999
998
1000
999
r[asm.options.supported-options.nomem]
1001
1000
-`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!`.
1001
+
This allows the compiler to cache the values of modified global variables in registers across execution of the assembly code since it knows that they are not read from or written to by it.
1003
1002
The compiler also assumes that the assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
1004
1003
1005
1004
<!-- no_run: This test has unpredictable or undefined behavior at runtime -->
1006
1005
```rust,no_run
1007
1006
# #[cfg(target_arch = "x86_64")] {
1008
1007
let mut x = 0i32;
1009
1008
let z: i32;
1010
-
// Accessing memory from assembly in a nomem asm block is disallowed
1009
+
// Accessing outside memory from assembly when `nomem` is
-`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
+
This allows the compiler to cache the values of unmodified global variables in registers across execution of the assembly code since it knows that they are not written to by it.
1049
1050
The compiler also assumes that this assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
1050
1051
1051
1052
<!-- no_run: This test has undefined behaviour at runtime -->
1052
1053
```rust,no_run
1053
1054
# #[cfg(target_arch = "x86_64")] {
1054
1055
let mut x = 0;
1055
-
// We cannot modify memory in readonly
1056
+
// We cannot modify outside memory when `readonly` is specified
-`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.
1095
+
This allows the compiler to avoid recomputing the condition flags after execution of the assembly code.
1095
1096
1096
1097
r[asm.options.supported-options.noreturn]
1097
-
-`noreturn`: The assembly code never returns, and its return type is defined as `!` (never).
1098
-
Behavior is undefined if execution falls through past the end of the asm code.
1099
-
A `noreturn` asm block with no `label` blocks 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 assembly code never falls through; the assembly code may only exit by jumping to one of the specified blocks.
1101
-
The entire `asm!` block will have unit type in this case, unless all `label` blocks diverge, in which case the return type is `!`.
1098
+
-`noreturn`: The assembly code does not fall through; behavior is undefined if it does. It may still jump to `label` blocks. If any `label` blocks return unit, the `asm!` block will return unit. Otherwise it will return `!` (never). As with a call to a function that does not return, local variables in scope are not dropped before execution of the assembly code.
- Refer to the unsafe code guidelines for the exact rules.
1238
1245
- If the `readonly` option is set, then only memory reads are allowed.
1239
1246
- 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 assembly code.
1247
+
- These rules do not apply to memory which is private to the assembly code, such as stack space allocated within it.
1241
1248
1242
1249
r[asm.rules.black-box]
1243
-
- The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed.
1244
-
- This effectively means that the compiler must treat the `asm!` as a black box and only take the interface specification into account, not the instructions themselves.
1250
+
- The compiler cannot assume that the instructions in the assembly code are the ones that will actually end up executed.
1251
+
- This effectively means that the compiler must treat the assembly code as a black box and only take the interface specification into account, not the instructions themselves.
1245
1252
- Runtime code patching is allowed, via target-specific mechanisms.
1246
-
- However there is no guarantee that each `asm!`directly corresponds to a single instance of instructions in the object file: the compiler is free to duplicate or deduplicate the assembly code in `asm!` blocks.
1253
+
- However there is no guarantee that each block of assembly code in the source directly corresponds to a single instance of instructions in the object file; the compiler is free to duplicate or deduplicate the assembly code in `asm!` blocks.
1247
1254
1248
1255
r[asm.rules.stack-below-sp]
1249
-
- Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer.
1256
+
- Unless the `nostack` option is set, assembly code is allowed to use stack space below the stack pointer.
1250
1257
- On entry to the assembly code the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
1251
1258
- You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page).
1252
1259
- You should adjust the stack pointer when allocating stack memory as required by the target ABI.
1253
1260
- The stack pointer must be restored to its original value before leaving the assembly code.
1254
1261
1255
1262
r[asm.rules.noreturn]
1256
-
- If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the assembly code.
1263
+
- If the `noreturn` option is set then behavior is undefined if execution falls through the end of the assembly code.
1257
1264
1258
1265
r[asm.rules.pure]
1259
1266
- If the `pure` option is set then behavior is undefined if the `asm!` has side-effects other than its direct outputs.
1260
1267
Behavior is also undefined if two executions of the `asm!` code with the same inputs result in different outputs.
1261
1268
- 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 assembly code is allowed to read.
1269
+
- When used with the `readonly` option, "inputs" comprise the direct inputs of the assembly code and any memory that it is allowed to read.
1263
1270
1264
1271
r[asm.rules.preserved-registers]
1265
1272
- These flags registers must be restored upon exiting the assembly code if the `preserves_flags` option is set:
@@ -1331,7 +1338,7 @@ r[asm.rules.arm64ec]
1331
1338
1332
1339
r[asm.rules.only-on-exit]
1333
1340
- 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 returns (even if not marked `noreturn`) doesn't need to preserve these registers.
1341
+
- This means that assembly code that does not fall through and does not jump to any `label` blocks, even if not marked `noreturn`, doesn't need to preserve these registers.
1335
1342
- When returning to the assembly code of 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*.
1336
1343
- You cannot exit the assembly code of an `asm!` block that has not been entered.
1337
1344
Neither can you exit the assembly code of an `asm!` block whose assembly code has already been exited (without first entering it again).
0 commit comments