Skip to content

Commit 428446c

Browse files
committed
Add global_asm! for module-level inline assembly
1 parent 1dd5f1d commit 428446c

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

text/0000-global-asm.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
- Feature Name: global_asm
2+
- Start Date: 2016-03-18
3+
- RFC PR: (leave this empty)
4+
- Rust Issue: (leave this empty)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
This RFC exposes LLVM's support for [module-level inline assembly](http://llvm.org/docs/LangRef.html#module-level-inline-assembly) by adding a `global_asm!` macro. The syntax is very simple: it just takes a string literal containing the assembly code.
10+
11+
Example:
12+
```rust
13+
global_asm!(r#"
14+
.globl my_asm_func
15+
my_asm_func:
16+
ret
17+
"#);
18+
19+
extern {
20+
fn my_asm_func();
21+
}
22+
```
23+
24+
# Motivation
25+
[motivation]: #motivation
26+
27+
There are two main use cases for this feature. The first is that it allows functions to be written completely in assembly, which mostly eliminates the need for a `naked` attribute. This is mainly useful for function that use a custom calling convention, such as interrupt handlers.
28+
29+
Another important use case is that it allows external assembly files to be used in a Rust module without needing hacks in the build system:
30+
31+
```rust
32+
global_asm!(include_str!("my_asm_file.s"));
33+
```
34+
35+
Assembly files can also be preprocessed or generated by `build.rs` (for example using the C preprocessor), which will produce output files in the Cargo output directory:
36+
37+
```rust
38+
global_asm!(include_str!(concat!(env!("OUT_DIR"), "/preprocessed_asm.s")));
39+
```
40+
41+
# Detailed design
42+
[design]: #detailed-design
43+
44+
See description above, not much to add. The macro will map directly to LLVM's `module asm`.
45+
46+
# Drawbacks
47+
[drawbacks]: #drawbacks
48+
49+
Like `asm!`, this feature depends on LLVM's integrated assembler.
50+
51+
# Alternatives
52+
[alternatives]: #alternatives
53+
54+
The current way of including external assembly is to compile the assembly files using gcc in `build.rs` and link them into the Rust program as a static library.
55+
56+
An alternative for functions written entirely in assembly is to add a [`#[naked]` function attribute](https://github.com/rust-lang/rfcs/pull/1201).
57+
58+
# Unresolved questions
59+
[unresolved]: #unresolved-questions
60+
61+
None

0 commit comments

Comments
 (0)