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
This feature likely needs more design and constrained naked functions are
useful without it. I have also added a brief discussion of this feature to the
future possibilities section. If people feel strongly that we should keep the
custom calling convention, we can revert this commit.
This document attempts to increase the utility of [naked functions](https://github.com/rust-lang/rfcs/blob/master/text/1201-naked-fns.md) by constraining their use and increasing their defined invariants. This document also defines a new calling convention: `extern "custom"`.
7
+
This document attempts to increase the utility of [naked functions](https://github.com/rust-lang/rfcs/blob/master/text/1201-naked-fns.md) by constraining their use and increasing their defined invariants.
8
8
9
9
# Motivation
10
10
@@ -37,19 +37,6 @@ In exchange for the above constraints, the compiler commits to:
37
37
38
38
As a (weaker) correlary to the last compiler commitment, the initial state of all registers in the `asm!()` statement conform to the specified calling convention.
39
39
40
-
# Custom Calling Convention
41
-
42
-
In order to expand the usefulness of naked functions, this document also define a new calling convention, `extern "custom"`, that can be used only with naked functions. Functions with `extern "custom"` are not callable from Rust and must be marked as`unsafe`. The function definition must contain no arguments or return type. For example:
43
-
44
-
```rust
45
-
#[naked]
46
-
unsafeextern"custom"fncallback() {
47
-
asm!("mov rbx, rsi")
48
-
}
49
-
```
50
-
51
-
An `extern "custom"` function can be passed as a function pointer argument or passed to an `asm!()` block via `sym`.
52
-
53
40
# Explanation
54
41
55
42
Since a naked function has no prologue, any naive attempt to use the stack can produce invalid code. This certainly includes local variables. But this can also include attempts to reference function arguments which may be placed on the stack. This is why a naked function may only contain a single `asm!()` statement.
@@ -64,8 +51,6 @@ Because naked functions depend upon the calling convention so heavily, inlining
64
51
65
52
Since the `const` and `sym` operands modify neither the stack nor the registers, their use is permitted.
66
53
67
-
Because the compiler guarantees that no instructions not in the `asm!()` block of a naked function will be emitted to the function body, an `extern "custom"` function can implement whatever custom calling convention it wants. This is useful in a variety of scenarios where the calling convention is custom, such as in interrupt handlers or callbacks from assembly code.
68
-
69
54
## Examples
70
55
71
56
This function adds three to a number and returns the result:
@@ -140,4 +125,6 @@ Due to the definition of naked functions, the arguments will always be flagged a
140
125
141
126
It would be possible to define new calling conventions that can be used with naked functions.
142
127
128
+
A previous version of this document defined an `extern "custom"` calling convention. It was observed in conversation that calling conventions are really a *type* and that it could be useful to have calling conventions as part of the type system. In the interest of moving forward with constrained naked functions, it is best to limit the scope of this RFC and defer this (very good) conversation to a future RFC.
129
+
143
130
It may also be possible to loosen the definition of a naked function in a future RFC. For example, it might be possible to allow the use of some additional, possibly new, operands to the `asm!()` block.
0 commit comments