From 7fa1a195a0d2e7b5306987f2b359aca1fcbca5a2 Mon Sep 17 00:00:00 2001 From: Connor Horman Date: Thu, 26 Sep 2024 15:04:25 -0400 Subject: [PATCH] Adjust identifiers in inline-assembly.md to be more consistent with other chapters --- src/inline-assembly.md | 157 +++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 78 deletions(-) diff --git a/src/inline-assembly.md b/src/inline-assembly.md index 3ad16a600..6add42333 100644 --- a/src/inline-assembly.md +++ b/src/inline-assembly.md @@ -9,7 +9,7 @@ It can be used to embed handwritten assembly in the assembly output generated by [`asm!`]: core::arch::asm [`global_asm!`]: core::arch::global_asm -r[asm.stable-targets] +r[asm.targets] Support for inline assembly is stable on the following architectures: - x86 and x86-64 - ARM @@ -17,12 +17,11 @@ Support for inline assembly is stable on the following architectures: - RISC-V - LoongArch -The compiler will emit an error if `asm!` is used on an unsupported target. +> [!NOTE] +> The compiler will emit an error if `asm!` is used on an unsupported target. ## Example -r[asm.example] - ```rust # #[cfg(target_arch = "x86_64")] { use std::arch::asm; @@ -81,37 +80,37 @@ This can be used to hand-write entire functions using assembly code, and general ## Template string arguments -r[asm.ts-args] +r[asm.argument] -r[asm.ts-args.syntax] +r[asm.argument.format] The assembler template uses the same syntax as [format strings][format-syntax] (i.e. placeholders are specified by curly braces). -r[asm.ts-args.order] +r[asm.argument.order] The corresponding arguments are accessed in order, by index, or by name. -r[asm.ts-args.no-implicit] +r[asm.argument.implicit] However, implicit named arguments (introduced by [RFC #2795][rfc-2795]) are not supported. -r[asm.ts-args.one-or-more] +r[asm.argument.multiple] An `asm!` invocation may have one or more template string arguments; an `asm!` with multiple template string arguments is treated as if all the strings were concatenated with a `\n` between them. The expected usage is for each template string argument to correspond to a line of assembly code. -r[asm.ts-args.before-other-args] +r[asm.argument.string-location] All template string arguments must appear before any other arguments. -r[asm.ts-args.positional-first] +r[asm.argument.positional] As with format strings, positional arguments must appear before named arguments and explicit [register operands](#register-operands). -r[asm.ts-args.register-operands] +r[asm.argument.register] Explicit register operands cannot be used by placeholders in the template string. -r[asm.ts-args.at-least-once] +r[asm.argument.argument-usage] All other named and positional operands must appear at least once in the template string, otherwise a compiler error is generated. -r[asm.ts-args.opaque] +r[asm.argument.asm-syntax] The exact assembly code syntax is target-specific and opaque to the compiler except for the way operands are substituted into the template string to form the code passed to the assembler. -r[asm.ts-args.llvm-syntax] +r[asm.argument.syntax-form] Currently, all supported targets follow the assembly code syntax used by LLVM's internal assembler which usually corresponds to that of the GNU assembler (GAS). On x86, the `.intel_syntax noprefix` mode of GAS is used by default. On ARM, the `.syntax unified` mode is used. @@ -126,17 +125,17 @@ Further constraints on the directives used by inline assembly are indicated by [ r[asm.operand-type] -r[asm.operand-type.supported-operands] +r[asm.operand-type.form] Several types of operands are supported: -r[asm.operand-type.supported-operands.in] +r[asm.operand-type.in] * `in() ` - `` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. - The allocated register will contain the value of `` at the start of the asm code. - The allocated register must contain the same value at the end of the asm code (except if a `lateout` is allocated to the same register). -r[asm.operand-type.supported-operands.out] +r[asm.operand-type.out] * `out() ` - `` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. @@ -144,67 +143,69 @@ r[asm.operand-type.supported-operands.out] - `` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the asm code. - 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). -r[asm.operand-type.supported-operands.lateout] +r[asm.operand-type.lateout] * `lateout() ` - Identical to `out` except that the register allocator can reuse a register allocated to an `in`. - You should only write to the register after all inputs are read, otherwise you may clobber an input. -r[asm.operand-type.supported-operands.inout] +r[asm.operand-type.inout] * `inout() ` - `` can refer to a register class or an explicit register. The allocated register name is substituted into the asm template string. - The allocated register will contain the value of `` at the start of the asm code. - `` must be a mutable initialized place expression, to which the contents of the allocated register are written at the end of the asm code. -r[asm.operand-type.supported-operands.inout-arrow] +r[asm.operand-type.inout-split] * `inout() => ` - Same as `inout` except that the initial value of the register is taken from the value of ``. - `` must be a (possibly uninitialized) place expression, to which the contents of the allocated register are written at the end of the asm code. - An underscore (`_`) may be specified instead of an expression for ``, which will cause the contents of the register to be discarded at the end of the asm code (effectively acting as a clobber). - `` and `` may have different types. -r[asm.operand-type.supported-operands.inlateout] +r[asm.operand-type.inlateout] * `inlateout() ` / `inlateout() => ` - Identical to `inout` except that the register allocator can reuse a register allocated to an `in` (this can happen if the compiler knows the `in` has the same initial value as the `inlateout`). - You should only write to the register after all inputs are read, otherwise you may clobber an input. -r[asm.operand-type.supported-operands.sym] +r[asm.operand-type.sym] * `sym ` - `` must refer to a `fn` or `static`. - A mangled symbol name referring to the item is substituted into the asm template string. - The substituted string does not include any modifiers (e.g. GOT, PLT, relocations, etc). - `` 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. + +r[asm.operand-type.const] * `const ` - `` must be an integer constant expression. This expression follows the same rules as inline `const` blocks. - The type of the expression may be any integer type, but defaults to `i32` just like integer literals. - The value of the expression is formatted as a string and substituted directly into the asm template string. -r[asm.operand-type.left-to-right] +r[asm.operand-type.order] Operand expressions are evaluated from left to right, just like function call arguments. After the `asm!` has executed, outputs are written to in left to right order. This is significant if two outputs point to the same place: that place will contain the value of the rightmost output. -r[asm.operand-type.global_asm-restriction] +r[asm.operand-type.global_asm] Since `global_asm!` exists outside a function, it can only use `sym` and `const` operands. ## Register operands -r[asm.register-operands] +r[asm.register] -r[asm.register-operands.register-or-class] +r[asm.register.name] Input and output operands can be specified either as an explicit register or as a register class from which the register allocator can select a register. Explicit registers are specified as string literals (e.g. `"eax"`) while register classes are specified as identifiers (e.g. `reg`). -r[asm.register-operands.equivalence-to-base-register] +r[asm.register.alias] Note that explicit registers treat register aliases (e.g. `r14` vs `lr` on ARM) and smaller views of a register (e.g. `eax` vs `rax`) as equivalent to the base register. -r[asm.register-operands.error-two-operands] +r[asm.register.duplicate] It is a compile-time error to use the same explicit register for two input operands or two output operands. -r[asm.register-operands.error-overlapping] +r[asm.register.overlapping] Additionally, it is also a compile-time error to use overlapping registers (e.g. ARM VFP) in input operands or in output operands. -r[asm.register-operands.allowed-types] +r[asm.register.types] Only the following types are allowed as operands for inline assembly: - Integers (signed and unsigned) - Floating-point numbers @@ -213,7 +214,7 @@ Only the following types are allowed as operands for inline assembly: - SIMD vectors (structs defined with `#[repr(simd)]` and which implement `Copy`). This includes architecture-specific vector types defined in `std::arch` such as `__m128` (x86) or `int8x16_t` (ARM). -r[asm.register-operands.supported-register-classes] +r[asm.register.classes] Here is the list of currently supported register classes: | Architecture | Register class | Registers | LLVM constraint code | @@ -255,7 +256,7 @@ Here is the list of currently supported register classes: > - On x86-64 the high byte registers (e.g. `ah`) are not available in the `reg_byte` register class. > - Some register classes are marked as "Only clobbers" which means that registers in these classes cannot be used for inputs or outputs, only clobbers of the form `out() _` or `lateout() _`. -r[asm.register-operands.value-type-constraints] +r[asm.register.class-types] Each register class has constraints on which value types they can be used with. This is necessary because the way a value is loaded into a register depends on its type. For example, on big-endian systems, loading a `i32x4` and a `i8x16` into a SIMD register may result in different register contents even if the byte-wise memory representation of both values is identical. @@ -291,11 +292,11 @@ The availability of supported types for a particular register class may depend o > **Note**: For the purposes of the above table pointers, function pointers and `isize`/`usize` are treated as the equivalent integer type (`i16`/`i32`/`i64` depending on the target). -r[asm.register-operands.smaller-value] +r[asm.register.widening] If a value is of a smaller size than the register it is allocated in then the upper bits of that register will have an undefined value for inputs and will be ignored for outputs. The only exception is the `freg` register class on RISC-V where `f32` values are NaN-boxed in a `f64` as required by the RISC-V architecture. -r[asm.register-operands.separate-input-output] +r[asm.register.inout-type] When separate input and output expressions are specified for an `inout` operand, both expressions must have the same type. The only exception is if both operands are pointers or integers, in which case they are only required to have the same size. This restriction exists because the register allocators in LLVM and GCC sometimes cannot handle tied operands with different types. @@ -304,7 +305,7 @@ This restriction exists because the register allocators in LLVM and GCC sometime r[asm.register-names] -r[asm.register-names.supported-register-aliases] +r[asm.register-names.alias] Some registers have multiple names. These are all treated by the compiler as identical to the base register name. Here is the list of all supported register aliases: @@ -367,7 +368,7 @@ Here is the list of all supported register aliases: | LoongArch | `$f[8-23]` | `$ft[0-15]` | | LoongArch | `$f[24-31]` | `$fs[0-7]` | -r[asm.register-names.not-for-io] +r[asm.register-names.reserved] Some registers cannot be used for input or output operands: | Architecture | Unsupported register | Reason | @@ -387,21 +388,21 @@ Some registers cannot be used for input or output operands: | LoongArch | `$r2` or `$tp` | This is reserved for TLS. | | LoongArch | `$r21` | This is reserved by the ABI. | -r[asm.register-names.fp-bp-reserved] +r[asm.register-names.base-pointer] The frame pointer and base pointer registers are reserved for internal use by LLVM. While `asm!` statements cannot explicitly specify the use of reserved registers, in some cases LLVM will allocate one of these reserved registers for `reg` operands. Assembly code making use of reserved registers should be careful since `reg` operands may use the same registers. ## Template modifiers -r[asm.template-modifiers] +r[asm.modifier] -r[asm.template-modifiers.intro] +r[asm.modifier.intro] The placeholders can be augmented by modifiers which are specified after the `:` in the curly braces. These modifiers do not affect register allocation, but change the way operands are formatted when inserted into the template string. -r[asm.template-modifiers.only-one] +r[asm.modifier.limit] Only one modifier is allowed per template placeholder. -r[asm.template-modifiers.supported-modifiers] +r[asm.modifier.list] The supported modifiers are a subset of LLVM's (and GCC's) [asm template argument modifiers][llvm-argmod], but do not use the same letter codes. | Architecture | Register class | Modifier | Example output | LLVM modifier | @@ -448,7 +449,7 @@ The supported modifiers are a subset of LLVM's (and GCC's) [asm template argumen > GCC will infer the modifier based on the operand value type, while we default to the full register size. > - on x86 `xmm_reg`: the `x`, `t` and `g` LLVM modifiers are not yet implemented in LLVM (they are supported by GCC only), but this should be a simple change. -r[asm.template-modifiers.smaller-value] +r[asm.modifier.widening] 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. 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`). Since this an easy pitfall, the compiler will suggest a template modifier to use where appropriate given the input type. @@ -458,22 +459,22 @@ If all references to an operand already have modifiers then the warning is suppr ## ABI clobbers -r[asm.abi-clobbers] +r[asm.clobber_abi] -r[asm.abi-clobbers.intro] +r[asm.clobber_abi.intro] The `clobber_abi` keyword can be used to apply a default set of clobbers to an `asm!` block. 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). -r[asm.abi-clobbers.many] +r[asm.clobber_abi.usage] `clobber_abi` may be specified any number of times. It will insert a clobber for all unique registers in the union of all specified calling conventions. -r[asm.abi-clobbers.must-specify] +r[asm.clobber_abi.constraint-class] Generic register class outputs are disallowed by the compiler when `clobber_abi` is used: all outputs must specify an explicit register. -r[asm.abi-clobbers.explicit-have-precedence] +r[asm.clobber_abi.explicit] Explicit register outputs have precedence over the implicit clobbers inserted by `clobber_abi`: a clobber will only be inserted for a register if that register is not used as an output. -r[asm.abi-clobbers.supported-abis] +r[asm.clobber_abi.list] The following ABIs can be used with `clobber_abi`: | Architecture | ABI name | Clobbered registers | @@ -495,59 +496,59 @@ The list of clobbered registers for each ABI is updated in rustc as architecture r[asm.options] -r[asm.options.supported-options] +r[asm.options.list] Flags are used to further influence the behavior of the inline assembly block. Currently the following options are defined: -r[asm.options.supported-options.pure] +r[asm.options.pure] - `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). 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. The `pure` option must be combined with either the `nomem` or `readonly` options, otherwise a compile-time error is emitted. -r[asm.options.supported-options.nomem] +r[asm.options.nomem] - `nomem`: The `asm!` block does not read from or write to any memory accessible outside of the `asm!` block. 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!`. The compiler also assumes that this `asm!` block does not perform any kind of synchronization with other threads, e.g. via fences. -r[asm.options.supported-options.readonly] +r[asm.options.readonly] - `readonly`: The `asm!` block does not write to any memory accessible outside of the `asm!` block. 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!`. The compiler also assumes that this `asm!` block does not perform any kind of synchronization with other threads, e.g. via fences. -r[asm.options.supported-options.preserves_flags] +r[asm.options.preserves_flags] - `preserves_flags`: The `asm!` block does not modify the flags register (defined in the rules below). This allows the compiler to avoid recomputing the condition flags after the `asm!` block. -r[asm.options.supported-options.noreturn] +r[asm.options.noreturn] - `noreturn`: The `asm!` block never returns, and its return type is defined as `!` (never). Behavior is undefined if execution falls through past the end of the asm code. 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. -r[asm.options.supported-options.nostack] +r[asm.options.nostack] - `nostack`: The `asm!` block does not push data to the stack, or write to the stack red-zone (if supported by the target). 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. -r[asm.options.supported-options.att_syntax] +r[asm.options.att_syntax] - `att_syntax`: This option is only valid on x86, and causes the assembler to use the `.att_syntax prefix` mode of the GNU assembler. Register operands are substituted in with a leading `%`. -r[asm.options.supported-options.raw] +r[asm.options.raw] - `raw`: This causes the template string to be parsed as a raw assembly string, with no special handling for `{` and `}`. This is primarily useful when including raw assembly code from an external file using `include_str!`. r[asm.options.checks] The compiler performs some additional checks on options: -r[asm.options.checks.mutually-exclusive] +r[asm.options.checks-exclusive] - The `nomem` and `readonly` options are mutually exclusive: it is a compile-time error to specify both. -r[asm.options.checks.pure] +r[asm.options.checks-pure] - It is a compile-time error to specify `pure` on an asm block with no outputs or only discarded outputs (`_`). -r[asm.options.checks.noreturn] +r[asm.options.checks-noreturn] - It is a compile-time error to specify `noreturn` on an asm block with outputs. -r[asm.options.global_asm-restriction] +r[asm.options.global_asm] `global_asm!` only supports the `att_syntax` and `raw` options. The remaining options are not meaningful for global-scope inline assembly @@ -558,12 +559,12 @@ r[asm.rules] r[asm.rules.intro] To avoid undefined behavior, these rules must be followed when using function-scope inline assembly (`asm!`): -r[asm.rules.reg-not-input] +r[asm.rules.input] - Any registers not specified as inputs will contain an undefined value on entry to the asm block. - 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. 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). -r[asm.rules.reg-not-output] +r[asm.rules.output] - 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. - This only applies to registers which can be specified as an input or output. Other registers follow target-specific rules. @@ -574,7 +575,7 @@ r[asm.rules.unwind] - Behavior is undefined if execution unwinds out of an asm block. - This also applies if the assembly code calls a function which then unwinds. -r[asm.rules.mem-same-as-ffi] +r[asm.rules.memory] - The set of memory locations that assembly code is allowed to read and write are the same as those allowed for an FFI function. - Refer to the unsafe code guidelines for the exact rules. - If the `readonly` option is set, then only memory reads are allowed. @@ -587,7 +588,7 @@ r[asm.rules.black-box] - Runtime code patching is allowed, via target-specific mechanisms. - 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. -r[asm.rules.stack-below-sp] +r[asm.rules.stack] - Unless the `nostack` option is set, asm code is allowed to use stack space below the stack pointer. - On entry to the asm block the stack pointer is guaranteed to be suitably aligned (according to the target ABI) for a function call. - You are responsible for making sure you don't overflow the stack (e.g. use stack probing to ensure you hit a guard page). @@ -633,7 +634,7 @@ r[asm.rules.x86-x87] - 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)") _, ...`. - 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. -r[asm.rules.only-on-exit] +r[asm.rules.divergent] - The requirement of restoring the stack pointer and non-output registers to their original value only applies when exiting an `asm!` block. - This means that `asm!` blocks that never return (even if not marked `noreturn`) don't need to preserve these registers. - 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*. @@ -643,10 +644,10 @@ r[asm.rules.only-on-exit] - 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. - The set of memory locations that you may access is the intersection of those allowed by the `asm!` blocks you entered and exited. -r[asm.rules.not-successive] +r[asm.rules.adjacency] - You cannot assume that two `asm!` blocks adjacent in source code, even without any other code between them, will end up in successive addresses in the binary without any other instructions between them. -r[asm.rules.not-exactly-once] +r[asm.rules.duplication] - You cannot assume that an `asm!` block will appear exactly once in the output binary. The compiler is allowed to instantiate multiple copies of the `asm!` block, for example when the function containing it is inlined in multiple places. @@ -661,7 +662,7 @@ r[asm.rules.preserves_flags] r[asm.validity] -r[asm.validity.necessary-but-not-sufficient] +r[asm.validity.limit] In addition to all of the previous rules, the string argument to `asm!` must ultimately become--- after all other arguments are evaluated, formatting is performed, and operands are translated--- assembly that is both syntactically correct and semantically valid for the target architecture. @@ -688,14 +689,14 @@ assuming the responsibility of not violating rules of both the compiler or the a r[asm.directives] -r[asm.directives.subset-supported] +r[asm.directives.intro] Inline assembly supports a subset of the directives supported by both GNU AS and LLVM's internal assembler, given as follows. The result of using other directives is assembler-specific (and may cause an error, or may be accepted as-is). -r[asm.directives.stateful] +r[asm.directives.state] 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. -r[asm.directives.supported-directives] +r[asm.directives.supported] The following directives are guaranteed to be supported by the assembler: - `.2byte` @@ -751,11 +752,11 @@ The following directives are guaranteed to be supported by the assembler: #### Target Specific Directive Support -r[asm.target-specific-directives] +r[asm.directives.target-specific] ##### Dwarf Unwinding -r[asm.target-specific-directives.dwarf-unwinding] +r[asm.directives.target-specific.dwarf] The following directives are supported on ELF targets that support DWARF unwind info: @@ -784,7 +785,7 @@ The following directives are supported on ELF targets that support DWARF unwind ##### Structured Exception Handling -r[asm.target-specific-directives.structured-exception-handling] +r[asm.directives.target-specific.structured-exception-handling] On targets with structured exception Handling, the following additional directives are guaranteed to be supported: - `.seh_endproc` @@ -798,7 +799,7 @@ On targets with structured exception Handling, the following additional directiv ##### x86 (32-bit and 64-bit) -r[asm.target-specific-directives.x86] +r[asm.directives.target-specific.x86] On x86 targets, both 32-bit and 64-bit, the following additional directives are guaranteed to be supported: - `.nops` - `.code16` @@ -813,7 +814,7 @@ Use of `.code16`, `.code32`, and `.code64` directives are only supported if the ##### ARM (32-bit) -r[asm.target-specific-directives.arm-32-bit] +r[asm.directives.target-specific.arm-32-bit] On ARM, the following additional directives are guaranteed to be supported: - `.even`