Skip to content

Commit 0df83f8

Browse files
committed
Update global_asm! documentation
1 parent 5a229e0 commit 0df83f8

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/doc/unstable-book/src/library-features/global-asm.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@ The tracking issue for this feature is: [#35119]
88

99
The `global_asm!` macro allows the programmer to write arbitrary
1010
assembly outside the scope of a function body, passing it through
11-
`rustc` and `llvm` to the assembler. The macro is a no-frills
12-
interface to LLVM's concept of [module-level inline assembly]. That is,
13-
all caveats applicable to LLVM's module-level inline assembly apply
14-
to `global_asm!`.
15-
16-
[module-level inline assembly]: http://llvm.org/docs/LangRef.html#module-level-inline-assembly
11+
`rustc` and `llvm` to the assembler. That is to say, `global_asm!` is
12+
equivalent to assembling the asm with an external assembler and then
13+
linking the resulting object file with the current crate.
1714

1815
`global_asm!` fills a role not currently satisfied by either `asm!`
1916
or `#[naked]` functions. The programmer has _all_ features of the
@@ -69,8 +66,44 @@ pub mod harry {
6966
```
7067

7168
You may use `global_asm!` multiple times, anywhere in your crate, in
72-
whatever way suits you. The effect is as if you concatenated all
73-
usages and placed the larger, single usage in the crate root.
69+
whatever way suits you. However, you should not rely on assembler state
70+
(e.g. assembler macros) defined in one `global_asm!` to be available in
71+
another one. It is implementation-defined whether the multiple usages
72+
are concatenated into one or assembled separately.
73+
74+
`global_asm!` also supports `const` operands like `asm!`, which allows
75+
constants defined in Rust to be used in assembly code:
76+
77+
```rust,no_run
78+
#![feature(global_asm)]
79+
# #[cfg(any(target_arch="x86", target_arch="x86_64"))]
80+
# mod x86 {
81+
const C: i32 = 1234;
82+
global_asm!(
83+
".global bar",
84+
"bar: .word {c}",
85+
c = const C,
86+
);
87+
# }
88+
```
89+
90+
The syntax for passing operands is the same as `asm!` except that only
91+
`const` operands are allowed. Refer to the [asm](asm.md) documentation
92+
for more details.
93+
94+
On x86, the assembly code will use intel syntax by default. You can
95+
override this by adding `options(att_syntax)` at the end of the macro
96+
arguments list:
97+
98+
```rust,no_run
99+
#![feature(global_asm)]
100+
# #[cfg(any(target_arch="x86", target_arch="x86_64"))]
101+
# mod x86 {
102+
global_asm!("movl ${}, %ecx", const 5, options(att_syntax));
103+
// is equivalent to
104+
global_asm!("mov ecx, {}", const 5);
105+
# }
106+
```
74107

75108
------------------------
76109

0 commit comments

Comments
 (0)