From 4933072f08ae1767e76d7faa03f5d9aabb136b44 Mon Sep 17 00:00:00 2001 From: Vladimir Pouzanov Date: Tue, 29 Apr 2014 13:11:19 +0100 Subject: [PATCH] RFC on low level features --- active/0000-better-low-level-handling.md | 85 ++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 active/0000-better-low-level-handling.md diff --git a/active/0000-better-low-level-handling.md b/active/0000-better-low-level-handling.md new file mode 100644 index 00000000000..cda4f90fbd4 --- /dev/null +++ b/active/0000-better-low-level-handling.md @@ -0,0 +1,85 @@ +- Start Date: 2014-04-29 +- RFC PR #: (leave this empty) +- Rust Issue #: (leave this empty) + +# Summary + +Add a few features that would allow to write better system-level code: weak +symbols, naked functions, pure assembly functions. + +# Motivation + +Those features are available in C and C++ and are often used in embedded +development. + +# Detailed design + +### Weak symbols + +`linkage` attribute should be extended to be allowed in function context +(alternatively a new attribute should be provided): + +```rust +#[no_mangle] +#[linkage(weak)] +pub fn isr_handler() { + ... +} +``` + +This would allow to provide default implementations that could be overriden by +specific code. + +### Naked functions + +Provide an attribute that would allow to generate a naked function, without +stack guard prologue, common function prologue and epilogue. The only statement +that could be safely used in naked function is `asm!` that doesn't modify +operands (alternatively, naked functions are always considered _unsafe_). + +```rust +#[naked] +pub fn isr_handler { + asm!("mrs r0, psp + stmdb r0!, {r4-r11} + msr psp, r0" :::: "volatile"); + switch_stack(); +} +``` + +would generate the following machine code: + +``` +isr_handler: + // asm! block + mrs r0, psp + stmdb r0!, {r4-r11} + msr psp, r0 + + // function call + bl switch_stack + + // the only generated instruction + bx lr +``` + +Note how in this example the provided code is incorrect, as `bl` will overwrite +the contents of `lr` register, so that the final `bx lr` instruction will do +something unexpected. It might be useful to disallow function calls from naked +function unless those are done explicity from `asm!` (which is quite hard, +unless the called function has `#[no_mangle]`). + +### Pure assembly functions + +This the extension of naked funcations above, but no code would be generated by +compiler at all. Those functions could be written in an external .s file, but +including them in source code would allow better management, including `#[cfg]` +usage to pick the correct implementation. + +# Alternatives + +N/A + +# Unresolved questions + +N/A