2021-02-18 19:30:59 +01:00
|
|
|
use proc_macro2::{Span, TokenStream as TokenStream2};
|
2019-06-13 23:56:59 +02:00
|
|
|
use quote::quote;
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic_syntax::ast::App;
|
2021-02-18 19:30:59 +01:00
|
|
|
use syn::Index;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-10-21 20:20:26 +02:00
|
|
|
use crate::{analyze::Analysis, codegen::util};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
/// Generates code that runs after `#[init]` returns
|
2020-09-01 19:04:55 +02:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut stmts = vec![];
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
// Initialize shared resources
|
|
|
|
for (name, res) in &app.shared_resources {
|
2021-08-19 21:32:12 +02:00
|
|
|
let mangled_name = util::static_shared_resource_ident(name);
|
2021-07-06 22:47:48 +02:00
|
|
|
// If it's live
|
|
|
|
let cfgs = res.cfgs.clone();
|
2021-11-07 00:42:57 +01:00
|
|
|
if analysis.shared_resources.get(name).is_some() {
|
2021-07-06 22:47:48 +02:00
|
|
|
stmts.push(quote!(
|
|
|
|
// We include the cfgs
|
|
|
|
#(#cfgs)*
|
|
|
|
// Resource is a RacyCell<MaybeUninit<T>>
|
2021-11-02 13:41:12 +01:00
|
|
|
// - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
|
2021-07-06 22:47:48 +02:00
|
|
|
// - `write` the defined value for the late resource T
|
2021-11-03 08:26:45 +01:00
|
|
|
#mangled_name.get_mut().write(core::mem::MaybeUninit::new(shared_resources.#name));
|
2021-07-06 22:47:48 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize local resources
|
|
|
|
for (name, res) in &app.local_resources {
|
2021-08-19 21:32:12 +02:00
|
|
|
let mangled_name = util::static_local_resource_ident(name);
|
2021-07-06 22:47:48 +02:00
|
|
|
// If it's live
|
|
|
|
let cfgs = res.cfgs.clone();
|
2021-11-07 00:42:57 +01:00
|
|
|
if analysis.local_resources.get(name).is_some() {
|
2021-07-06 22:47:48 +02:00
|
|
|
stmts.push(quote!(
|
|
|
|
// We include the cfgs
|
|
|
|
#(#cfgs)*
|
|
|
|
// Resource is a RacyCell<MaybeUninit<T>>
|
2021-11-02 13:41:12 +01:00
|
|
|
// - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
|
2021-07-06 22:47:48 +02:00
|
|
|
// - `write` the defined value for the late resource T
|
2021-11-03 08:26:45 +01:00
|
|
|
#mangled_name.get_mut().write(core::mem::MaybeUninit::new(local_resources.#name));
|
2021-07-06 22:47:48 +02:00
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:30:59 +01:00
|
|
|
for (i, (monotonic, _)) in app.monotonics.iter().enumerate() {
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-04-08 18:25:09 +02:00
|
|
|
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
|
|
|
// stmts.push(quote!(#[doc = #doc]));
|
|
|
|
|
2021-02-18 19:30:59 +01:00
|
|
|
let idx = Index {
|
|
|
|
index: i as u32,
|
|
|
|
span: Span::call_site(),
|
|
|
|
};
|
|
|
|
stmts.push(quote!(monotonics.#idx.reset();));
|
2021-02-04 20:22:02 +01:00
|
|
|
|
2021-02-18 19:30:59 +01:00
|
|
|
// Store the monotonic
|
|
|
|
let name = util::monotonic_ident(&monotonic.to_string());
|
2021-11-02 13:41:12 +01:00
|
|
|
stmts.push(quote!(#name.get_mut().write(Some(monotonics.#idx));));
|
2021-02-18 19:30:59 +01:00
|
|
|
}
|
2020-12-03 21:04:06 +01:00
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
// Enable the interrupts -- this completes the `init`-ialization phase
|
2020-06-11 19:18:29 +02:00
|
|
|
stmts.push(quote!(rtic::export::interrupt::enable();));
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2020-09-01 19:04:55 +02:00
|
|
|
stmts
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|