Skip to content

Commit 73e0b2a

Browse files
committed
Change pre-init to use an empty function that is overwridden instead of a sentinel
Using a sentinel creates a conditional branch that cannot be optimized out whereas an empty default function keeps the codepath linear and can be optimized out in the best case. Signed-off-by: Gabriel Smith <[email protected]>
1 parent 743b8fc commit 73e0b2a

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

cortex-m-rt/link.x.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ EXTERN(__INTERRUPTS); /* `static` variable similar to `__EXCEPTIONS` */
5454
/* # Pre-initialization function */
5555
/* If the user overrides this using the `pre_init!` macro or by creating a `__pre_init` function,
5656
then the function this points to will be called before the RAM is initialized. */
57-
PROVIDE(__pre_init = 1);
57+
PROVIDE(__pre_init = DefaultPreInit);
5858

5959
/* # Sections */
6060
SECTIONS

cortex-m-rt/src/lib.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@
237237
//! have a size of 32 vectors (on ARMv6-M) or 240 vectors (on ARMv7-M). This array is located after
238238
//! `__EXCEPTIONS` in the `.vector_table` section.
239239
//!
240-
//! - `__pre_init`. This is a function to be run before RAM is initialized. It defaults pointing at
241-
//! `1` and if not changed to point to another address, usually by calling the `pre_init!` macro,
242-
//! the `_pre_init` function is skipped. The function cannot default to `0` as the compiler
243-
//! optimizes out the check for `0` under the assumption that a function pointer cannot point to
244-
//! `0`.
240+
//! - `__pre_init`. This is a function to be run before RAM is initialized. It defaults to an empty
241+
//! function. The function called can be changed by calling the `pre_init!` macro. The empty
242+
//! function is not optimized out by default, but if an empty function is passed to `pre_init!` the
243+
//! function call will be optimized out.
245244
//!
246245
//! If you override any exception handler you'll find it as an unmangled symbol, e.g. `SysTick` or
247246
//! `SVCall`, in the output of `objdump`,
@@ -493,9 +492,7 @@ pub unsafe extern "C" fn Reset() -> ! {
493492
}
494493

495494
let pre_init: unsafe extern "C" fn() = __pre_init;
496-
if pre_init as usize != 1 {
497-
pre_init();
498-
}
495+
pre_init();
499496

500497
// Initialize RAM
501498
r0::zero_bss(&mut __sbss, &mut __ebss);
@@ -552,6 +549,10 @@ pub unsafe extern "C" fn DefaultUserHardFault() {
552549
}
553550
}
554551

552+
#[doc(hidden)]
553+
#[no_mangle]
554+
pub unsafe extern "C" fn DefaultPreInit() {}
555+
555556
/// Macro to define the entry point of the program
556557
///
557558
/// **NOTE** This macro must be invoked once and must be invoked from an accessible module, ideally

0 commit comments

Comments
 (0)