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
With the `naked_asm!` macro, the assembly code is emitted in a function scope and constitutes the full assembly code of a function.
85
+
The `naked_asm!` macro is only allowed in [naked functions](../attributes/codegen.md#the-naked-attribute).
86
+
81
87
r[asm.scope.global_asm]
82
88
With the `global_asm!` macro, the assembly code is emitted in a global scope, outside a function.
83
89
This can be used to hand-write entire functions using assembly code, and generally provides much more freedom to use arbitrary registers and assembler directives.
@@ -384,8 +390,11 @@ assert_eq!(y, 1);
384
390
# }
385
391
```
386
392
393
+
r[asm.operand-type.naked_asm-restriction]
394
+
Because `naked_asm!` defines a whole function body, it can only use `sym` and `const` operands.
395
+
387
396
r[asm.operand-type.global_asm-restriction]
388
-
Since`global_asm!` exists outside a function, it can only use `sym` and `const` operands.
397
+
Because`global_asm!` exists outside a function, it can only use `sym` and `const` operands.
> As a general rule, the flags covered by `preserves_flags` are those which are *not* preserved when performing a function call.
1364
1377
1378
+
r[asm.naked-rules]
1379
+
## Rules for naked inline assembly
1380
+
1381
+
r[asm.naked-rules.intro]
1382
+
To avoid undefined behavior, these rules must be followed when using function-scope inline assembly in naked functions (`naked_asm!`):
1383
+
1384
+
r[asm.naked-rules.reg-not-input]
1385
+
- Any registers not used for function inputs according to the calling convention and function signature will contain an undefined value on entry to the asm block.
1386
+
- 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.
1387
+
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).
1388
+
1389
+
r[asm.naked-rules.reg-not-output]
1390
+
- Any callee-saved registers must have the same value upon exiting the asm block as they had on entry, otherwise behavior is undefined.
1391
+
- Caller-saved registes may be used freely, even if they are not used for the return value.
1392
+
1393
+
r[asm.naked-rules.unwind]
1394
+
- Behavior is undefined if execution unwinds out of an asm block.
1395
+
- This also applies if the assembly code calls a function which then unwinds.
1396
+
1397
+
r[asm.naked-rules.noreturn]
1398
+
- Behavior is undefined if execution falls through to the end of the asm block.
1399
+
1400
+
r[asm.naked-rules.mem-same-as-ffi]
1401
+
- The set of memory locations that assembly code is allowed to read and write are the same as those allowed for an FFI function.
1402
+
- Refer to the unsafe code guidelines for the exact rules.
1403
+
- These rules do not apply to memory which is private to the asm code, such as stack space allocated within the asm block.
1404
+
1405
+
r[asm.naked-rules.black-box]
1406
+
- The compiler cannot assume that the instructions in the asm are the ones that will actually end up executed.
1407
+
- This effectively means that the compiler must treat the `naked_asm!` as a black box and only take the interface specification into account, not the instructions themselves.
1408
+
- Runtime code patching is allowed, via target-specific mechanisms.
1409
+
- However there is no guarantee that each `naked_asm!` directly corresponds to a single instance of instructions in the object file: the compiler is free to duplicate or deduplicate `naked_asm!` blocks.
1410
+
1411
+
r[asm.naked-rules.not-exactly-once]
1412
+
- You cannot assume that an `naked_asm!` block will appear exactly once in the output binary.
1413
+
The compiler is allowed to instantiate multiple copies of the `naked_asm!` block, for example when the function containing it is inlined in multiple places.
0 commit comments