2019-06-13 23:56:59 +02:00
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2020-04-08 16:43:47 +02:00
|
|
|
use rtfm_syntax::ast::App;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
use crate::{analyze::Analysis, check::Extra, codegen::util};
|
|
|
|
|
|
|
|
/// Generates code that runs after `#[init]` returns
|
|
|
|
pub fn codegen(
|
|
|
|
core: u8,
|
2020-04-08 16:43:47 +02:00
|
|
|
app: &App,
|
2019-06-13 23:56:59 +02:00
|
|
|
analysis: &Analysis,
|
|
|
|
extra: &Extra,
|
|
|
|
) -> (Vec<TokenStream2>, Vec<TokenStream2>) {
|
|
|
|
let mut const_app = vec![];
|
|
|
|
let mut stmts = vec![];
|
|
|
|
|
|
|
|
// initialize late resources
|
|
|
|
if let Some(late_resources) = analysis.late_resources.get(&core) {
|
|
|
|
for name in late_resources {
|
|
|
|
// if it's live
|
2020-04-08 16:43:47 +02:00
|
|
|
let cfgs = app.late_resources[name].cfgs.clone();
|
2019-06-13 23:56:59 +02:00
|
|
|
if analysis.locations.get(name).is_some() {
|
2020-04-08 16:43:47 +02:00
|
|
|
// Need to also include the cfgs
|
|
|
|
stmts.push(quote!(
|
2020-06-03 12:34:01 +02:00
|
|
|
#(#cfgs)*
|
|
|
|
#name.as_mut_ptr().write(late.#name);
|
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if analysis.timer_queues.is_empty() {
|
|
|
|
// cross-initialization barriers -- notify *other* cores that their resources have been
|
|
|
|
// initialized
|
2019-07-03 20:36:52 +02:00
|
|
|
for (user, initializers) in &analysis.initialization_barriers {
|
|
|
|
if !initializers.contains(&core) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ib = util::init_barrier(*user);
|
2019-06-18 10:31:31 +02:00
|
|
|
let shared = if cfg!(feature = "heterogeneous") {
|
|
|
|
Some(quote!(
|
|
|
|
#[rtfm::export::shared]
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
const_app.push(quote!(
|
2019-06-18 10:31:31 +02:00
|
|
|
#shared
|
2019-06-13 23:56:59 +02:00
|
|
|
static #ib: rtfm::export::Barrier = rtfm::export::Barrier::new();
|
|
|
|
));
|
|
|
|
|
|
|
|
stmts.push(quote!(
|
|
|
|
#ib.release();
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// then wait until the other cores have initialized *our* resources
|
2019-07-03 20:36:52 +02:00
|
|
|
if analysis.initialization_barriers.contains_key(&core) {
|
|
|
|
let ib = util::init_barrier(core);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2019-07-03 20:36:52 +02:00
|
|
|
stmts.push(quote!(
|
|
|
|
#ib.wait();
|
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// cross-spawn barriers: wait until other cores are ready to receive messages
|
|
|
|
for (&receiver, senders) in &analysis.spawn_barriers {
|
|
|
|
if senders.get(&core) == Some(&false) {
|
|
|
|
let sb = util::spawn_barrier(receiver);
|
|
|
|
|
|
|
|
stmts.push(quote!(
|
|
|
|
#sb.wait();
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if the `schedule` API is used then we'll synchronize all cores to leave the
|
|
|
|
// `init`-ialization phase at the same time. In this case the rendezvous barrier makes the
|
|
|
|
// cross-initialization and spawn barriers unnecessary
|
|
|
|
|
|
|
|
let m = extra.monotonic();
|
|
|
|
|
|
|
|
if analysis.timer_queues.len() == 1 {
|
|
|
|
// reset the monotonic timer / counter
|
|
|
|
stmts.push(quote!(
|
|
|
|
<#m as rtfm::Monotonic>::reset();
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
// in the multi-core case we need a rendezvous (RV) barrier between *all* the cores that
|
|
|
|
// use the `schedule` API; otherwise one of the cores could observe the before-reset
|
|
|
|
// value of the monotonic counter
|
|
|
|
// (this may be easier to implement with `AtomicU8.fetch_sub` but that API is not
|
|
|
|
// available on ARMv6-M)
|
|
|
|
|
|
|
|
// this core will reset the monotonic counter
|
|
|
|
const FIRST: u8 = 0;
|
|
|
|
|
|
|
|
if core == FIRST {
|
|
|
|
for &i in analysis.timer_queues.keys() {
|
|
|
|
let rv = util::rendezvous_ident(i);
|
2019-06-18 10:31:31 +02:00
|
|
|
let shared = if cfg!(feature = "heterogeneous") {
|
|
|
|
Some(quote!(
|
|
|
|
#[rtfm::export::shared]
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
const_app.push(quote!(
|
2019-06-18 10:31:31 +02:00
|
|
|
#shared
|
2019-06-13 23:56:59 +02:00
|
|
|
static #rv: rtfm::export::Barrier = rtfm::export::Barrier::new();
|
|
|
|
));
|
|
|
|
|
|
|
|
// wait until all the other cores have reached the RV point
|
|
|
|
if i != FIRST {
|
|
|
|
stmts.push(quote!(
|
|
|
|
#rv.wait();
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rv = util::rendezvous_ident(core);
|
|
|
|
stmts.push(quote!(
|
|
|
|
// the compiler fences are used to prevent `reset` from being re-ordering wrt to
|
|
|
|
// the atomic operations -- we don't know if `reset` contains load or store
|
|
|
|
// operations
|
|
|
|
|
|
|
|
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
|
|
|
// reset the counter
|
|
|
|
<#m as rtfm::Monotonic>::reset();
|
|
|
|
|
|
|
|
core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
|
|
|
|
|
|
|
|
// now unblock all the other cores
|
|
|
|
#rv.release();
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
let rv = util::rendezvous_ident(core);
|
|
|
|
|
|
|
|
// let the first core know that we have reached the RV point
|
|
|
|
stmts.push(quote!(
|
|
|
|
#rv.release();
|
|
|
|
));
|
|
|
|
|
|
|
|
let rv = util::rendezvous_ident(FIRST);
|
|
|
|
|
|
|
|
// wait until the first core has reset the monotonic timer
|
|
|
|
stmts.push(quote!(
|
|
|
|
#rv.wait();
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable the interrupts -- this completes the `init`-ialization phase
|
|
|
|
stmts.push(quote!(rtfm::export::interrupt::enable();));
|
|
|
|
|
|
|
|
(const_app, stmts)
|
|
|
|
}
|