rtic/macros/src/codegen/post_init.rs

54 lines
1.9 KiB
Rust
Raw Normal View History

2021-02-18 19:30:59 +01:00
use proc_macro2::{Span, TokenStream as TokenStream2};
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;
2020-10-21 20:20:26 +02:00
use crate::{analyze::Analysis, codegen::util};
/// Generates code that runs after `#[init]` returns
2020-09-01 19:04:55 +02:00
pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
let mut stmts = vec![];
2020-09-01 19:04:55 +02:00
// Initialize late resources
2020-10-13 16:16:33 +02:00
if !analysis.late_resources.is_empty() {
2020-08-27 13:21:56 +02:00
// BTreeSet wrapped in a vector
2020-09-01 19:04:55 +02:00
for name in analysis.late_resources.first().unwrap() {
let mangled_name = util::mark_internal_ident(&name);
2020-09-01 19:04:55 +02:00
// If it's live
2020-04-08 16:43:47 +02:00
let cfgs = app.late_resources[name].cfgs.clone();
if analysis.locations.get(name).is_some() {
2020-04-08 16:43:47 +02:00
stmts.push(quote!(
2021-04-08 18:25:09 +02:00
// We include the cfgs
#(#cfgs)*
// Late resource is a RacyCell<MaybeUninit<T>>
// - `get_mut_unchecked` to obtain `MaybeUninit<T>`
// - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
// - `write` the defined value for the late resource T
#mangled_name.get_mut_unchecked().as_mut_ptr().write(late.#name);
2020-06-03 12:34:01 +02:00
));
}
}
}
2021-02-18 19:30:59 +01:00
for (i, (monotonic, _)) in app.monotonics.iter().enumerate() {
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());
let name = util::mark_internal_ident(&name);
2021-04-08 18:25:09 +02:00
stmts.push(quote!(*#name.get_mut_unchecked() = 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();));
2020-09-01 19:04:55 +02:00
stmts
}