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
-`<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.
322
322
323
323
```rust
324
324
# #[cfg(target_arch ="x86_64")] {
@@ -354,6 +354,22 @@ assert_eq!(y, [3, 2, 0, 1]);
354
354
# }
355
355
```
356
356
357
+
r[asm.operand-type.supported-operands.label]
358
+
*`label <block>`
359
+
- The address of the block is substituted into the asm template string. The assembly code may jump to the substituted address.
360
+
- After execution of the block, the `asm!` expression returns.
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!` expression is already wrapped in `unsafe`.
363
+
364
+
```rust
365
+
# #[cfg(target_arch ="x86_64")]
366
+
unsafe {
367
+
core::arch::asm!("jmp {}", label {
368
+
println!("Hello from inline assembly label");
369
+
});
370
+
}
371
+
```
372
+
357
373
r[asm.operand-type.left-to-right]
358
374
Operand expressions are evaluated from left to right, just like function call arguments.
359
375
After the `asm!` has executed, outputs are written to in left to right order.
@@ -733,7 +749,7 @@ Some registers cannot be used for input or output operands:
733
749
734
750
| Architecture | Unsupported register | Reason |
735
751
| ------------ | -------------------- | ------ |
736
-
| 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 the assembly code or before jumping to a `label` block. |
737
753
| 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. |
738
754
| 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. |
739
755
| 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. |
@@ -847,7 +863,7 @@ assert_eq!(x, 0x1000u16);
847
863
848
864
r[asm.template-modifiers.smaller-value]
849
865
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.
850
-
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`).
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 assembly code (e.g. `ax` instead of `rax`).
851
867
Since this an easy pitfall, the compiler will suggest a template modifier to use where appropriate given the input type.
852
868
If all references to an operand already have modifiers then the warning is suppressed for that operand.
853
869
@@ -857,7 +873,7 @@ r[asm.abi-clobbers]
857
873
## ABI clobbers
858
874
859
875
r[asm.abi-clobbers.intro]
860
-
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.
861
877
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).
862
878
863
879
```rust
@@ -950,12 +966,12 @@ r[asm.options]
950
966
## Options
951
967
952
968
r[asm.options.supported-options]
953
-
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.
954
970
Currently the following options are defined:
955
971
956
972
r[asm.options.supported-options.pure]
957
-
-`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).
958
-
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.
959
975
The `pure` option must be combined with either the `nomem` or `readonly` options, otherwise a compile-time error is emitted.
960
976
961
977
```rust
@@ -982,16 +998,17 @@ assert_eq!(z, 0);
982
998
```
983
999
984
1000
r[asm.options.supported-options.nomem]
985
-
-`nomem`: The `asm!` block does not read from or write to any memory accessible outside of the `asm!` block.
986
-
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!`.
987
-
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 execution of the assembly code since it knows that they are not read from or written to by it.
1003
+
The compiler also assumes that the assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
988
1004
989
1005
<!-- no_run: This test has unpredictable or undefined behavior at runtime -->
990
1006
```rust,no_run
991
1007
# #[cfg(target_arch = "x86_64")] {
992
1008
let mut x = 0i32;
993
1009
let z: i32;
994
-
// Accessing memory from a nomem asm block is disallowed
1010
+
// Accessing outside memory from assembly when `nomem` is
-`readonly`: The `asm!` block does not write to any memory accessible outside of the `asm!` block.
1032
-
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!`.
1033
-
The compiler also assumes that this `asm!` block does not perform any kind of synchronization with other threads, e.g. via fences.
1049
+
-`readonly`: The assembly code does not write to any memory accessible outside of the assembly code.
1050
+
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.
1051
+
The compiler also assumes that this assembly code does not perform any kind of synchronization with other threads, e.g. via fences.
1034
1052
1035
1053
<!-- no_run: This test has undefined behaviour at runtime -->
1036
1054
```rust,no_run
1037
1055
# #[cfg(target_arch = "x86_64")] {
1038
1056
let mut x = 0;
1039
-
// We cannot modify memory in readonly
1057
+
// We cannot modify outside memory when `readonly` is specified
-`preserves_flags`: The `asm!` block does not modify the flags register (defined in the rules below).
1078
-
This allows the compiler to avoid recomputing the condition flags after the `asm!` block.
1095
+
-`preserves_flags`: The assembly code does not modify the flags register (defined in the rules below).
1096
+
This allows the compiler to avoid recomputing the condition flags after execution of the assembly code.
1079
1097
1080
1098
r[asm.options.supported-options.noreturn]
1081
-
-`noreturn`: The `asm!` block never returns, and its return type is defined as `!` (never).
1082
-
Behavior is undefined if execution falls through past the end of the asm code.
1083
-
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.
1099
+
-`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.
- It is a compile-time error to specify `noreturn` on an asm block with outputs.
1194
+
- It is a compile-time error to specify `noreturn` on an asm block with outputs and without labels.
1195
+
1196
+
r[asm.options.checks.label-with-outputs]
1197
+
- It is a compile-time error to have any `label` blocks in an asm block with outputs.
1169
1198
1170
1199
```rust,compile_fail
1171
1200
# #[cfg(target_arch = "x86_64")] {
@@ -1196,52 +1225,52 @@ r[asm.rules.intro]
1196
1225
To avoid undefined behavior, these rules must be followed when using function-scope inline assembly (`asm!`):
1197
1226
1198
1227
r[asm.rules.reg-not-input]
1199
-
- Any registers not specified as inputs will contain an undefined value on entry to the asm block.
1228
+
- Any registers not specified as inputs will contain an undefined value on entry to the assembly code.
1200
1229
- 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.
1201
1230
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).
1202
1231
1203
1232
r[asm.rules.reg-not-output]
1204
-
- 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.
1233
+
- 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.
1205
1234
- This only applies to registers which can be specified as an input or output.
1206
1235
Other registers follow target-specific rules.
1207
1236
- Note that a `lateout` may be allocated to the same register as an `in`, in which case this rule does not apply.
1208
1237
Code should not rely on this however since it depends on the results of register allocation.
1209
1238
1210
1239
r[asm.rules.unwind]
1211
-
- Behavior is undefined if execution unwinds out of an asm block.
1240
+
- Behavior is undefined if execution unwinds out of the assembly code.
1212
1241
- This also applies if the assembly code calls a function which then unwinds.
1213
1242
1214
1243
r[asm.rules.mem-same-as-ffi]
1215
1244
- The set of memory locations that assembly code is allowed to read and write are the same as those allowed for an FFI function.
1216
1245
- Refer to the unsafe code guidelines for the exact rules.
1217
1246
- If the `readonly` option is set, then only memory reads are allowed.
1218
1247
- If the `nomem` option is set then no reads or writes to memory are allowed.
1219
-
- These rules do not apply to memory which is private to the asm code, such as stack space allocated within the asm block.
1248
+
- These rules do not apply to memory which is private to the assembly code, such as stack space allocated within it.
1220
1249
1221
1250
r[asm.rules.black-box]
1222
-
- The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed.
1223
-
- 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.
1251
+
- The compiler cannot assume that the instructions in the assembly code are the ones that will actually end up executed.
1252
+
- 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.
1224
1253
- Runtime code patching is allowed, via target-specific mechanisms.
1225
-
- 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 `asm!` blocks.
1254
+
- 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.
1226
1255
1227
1256
r[asm.rules.stack-below-sp]
1228
-
- Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer.
1229
-
- On entry to the asm block the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
1257
+
- Unless the `nostack` option is set, assembly code is allowed to use stack space below the stack pointer.
1258
+
- On entry to the assembly code the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call.
1230
1259
- You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page).
1231
1260
- You should adjust the stack pointer when allocating stack memory as required by the target ABI.
1232
-
- The stack pointer must be restored to its original value before leaving the asm block.
1261
+
- The stack pointer must be restored to its original value before leaving the assembly code.
1233
1262
1234
1263
r[asm.rules.noreturn]
1235
-
- If the `noreturn` option is set then behavior is undefined if execution falls through to the end of the asm block.
1264
+
- If the `noreturn` option is set then behavior is undefined if execution falls through the end of the assembly code.
1236
1265
1237
1266
r[asm.rules.pure]
1238
1267
- If the `pure` option is set then behavior is undefined if the `asm!` has side-effects other than its direct outputs.
1239
1268
Behavior is also undefined if two executions of the `asm!` code with the same inputs result in different outputs.
1240
1269
- When used with the `nomem` option, "inputs" are just the direct inputs of the `asm!`.
1241
-
- 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.
1270
+
- When used with the `readonly` option, "inputs" comprise the direct inputs of the assembly code and any memory that it is allowed to read.
1242
1271
1243
1272
r[asm.rules.preserved-registers]
1244
-
- These flags registers must be restored upon exiting the asm block if the `preserves_flags` option is set:
1273
+
- These flags registers must be restored upon exiting the assembly code if the `preserves_flags` option is set:
1245
1274
- x86
1246
1275
- Status flags in `EFLAGS` (CF, PF, AF, ZF, SF, OF).
- On x86, the direction flag (DF in `EFLAGS`) is clear on entry to an asm block and must be clear on exit.
1269
-
- Behavior is undefined if the direction flag is set on exiting an asm block.
1297
+
- On x86, the direction flag (DF in `EFLAGS`) is clear on entry to the assembly code and must be clear on exit.
1298
+
- Behavior is undefined if the direction flag is set on exiting the assembly code.
1270
1299
1271
1300
r[asm.rules.x86-x87]
1272
1301
- 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)") _, ...`.
1273
-
- 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.
1302
+
- 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.
1274
1303
1275
1304
```rust
1276
1305
# #[cfg(target_arch ="x86_64")]
@@ -1309,11 +1338,11 @@ r[asm.rules.arm64ec]
1309
1338
- 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.
1310
1339
1311
1340
r[asm.rules.only-on-exit]
1312
-
- The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting an `asm!` block.
1313
-
- This means that `asm!` blocks that never return (even if not marked `noreturn`) don't need to preserve these registers.
1314
-
- 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*.
1315
-
- You cannot exit an `asm!` block that has not been entered.
1316
-
Neither can you exit an `asm!` block that has already been exited (without first entering it again).
1341
+
- The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting the assembly code.
1342
+
- 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.
1343
+
- 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*.
1344
+
- You cannot exit the assembly code of an `asm!` block that has not been entered.
1345
+
Neither can you exit the assembly code of an `asm!` block whose assembly code has already been exited (without first entering it again).
1317
1346
- You are responsible for switching any target-specific state (e.g. thread-local storage, stack bounds).
1318
1347
- You cannot jump from an address in one `asm!` block to an address in another, even within the same function or block, without treating their contexts as potentially different and requiring context switching. You cannot assume that any particular value in those contexts (e.g. current stack pointer or temporary values below the stack pointer) will remain unchanged between the two `asm!` blocks.
1319
1348
- The set of memory locations that you may access is the intersection of those allowed by the `asm!` blocks you entered and exited.
@@ -1341,7 +1370,7 @@ In addition to all of the previous rules, the string argument to `asm!` must ult
1341
1370
after all other arguments are evaluated, formatting is performed, and operands are translated---
1342
1371
assembly that is both syntactically correct and semantically valid for the target architecture.
1343
1372
The formatting rules allow the compiler to generate assembly with correct syntax.
1344
-
Rules concerning operands permit valid translation of Rust operands into and out of `asm!`.
1373
+
Rules concerning operands permit valid translation of Rust operands into and out of the assembly code.
1345
1374
Adherence to these rules is necessary, but not sufficient, for the final expanded assembly to be
1346
1375
both correct and valid. For instance:
1347
1376
@@ -1367,7 +1396,7 @@ Inline assembly supports a subset of the directives supported by both GNU AS and
1367
1396
The result of using other directives is assembler-specific (and may cause an error, or may be accepted as-is).
1368
1397
1369
1398
r[asm.directives.stateful]
1370
-
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.
1399
+
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.
1371
1400
1372
1401
r[asm.directives.supported-directives]
1373
1402
The following directives are guaranteed to be supported by the assembler:
@@ -1494,7 +1523,7 @@ On x86 targets, both 32-bit and 64-bit, the following additional directives are
1494
1523
-`.code32`
1495
1524
-`.code64`
1496
1525
1497
-
Use of `.code16`, `.code32`, and `.code64` directives are only supported if the state is reset to the default before exiting the assembly block.
1526
+
Use of `.code16`, `.code32`, and `.code64` directives are only supported if the state is reset to the default before exiting the assembly code.
1498
1527
32-bit x86 uses `.code32` by default, and x86_64 uses `.code64` by default.
0 commit comments