1use proc_macro2::{Span, TokenStream as TokenStream2};
2use quote::quote;
3use rtic_syntax::ast::App;
4use syn::Index;
56use crate::{analyze::Analysis, codegen::util};
78/// Generates code that runs after `#[init]` returns
9pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
10let mut stmts = vec![];
1112// Initialize shared resources
13for (name, res) in &app.shared_resources {
14let mangled_name = util::static_shared_resource_ident(name);
15// If it's live
16let cfgs = res.cfgs.clone();
17if analysis.shared_resources.get(name).is_some() {
18 stmts.push(quote!(
19// We include the cfgs
20#(#cfgs)*
21// Resource is a RacyCell<MaybeUninit<T>>
22 // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
23 // - `write` the defined value for the late resource T
24#mangled_name.get_mut().write(core::mem::MaybeUninit::new(shared_resources.#name));
25 ));
26 }
27 }
2829// Initialize local resources
30for (name, res) in &app.local_resources {
31let mangled_name = util::static_local_resource_ident(name);
32// If it's live
33let cfgs = res.cfgs.clone();
34if analysis.local_resources.get(name).is_some() {
35 stmts.push(quote!(
36// We include the cfgs
37#(#cfgs)*
38// Resource is a RacyCell<MaybeUninit<T>>
39 // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
40 // - `write` the defined value for the late resource T
41#mangled_name.get_mut().write(core::mem::MaybeUninit::new(local_resources.#name));
42 ));
43 }
44 }
4546for (i, (monotonic_ident, monotonic)) in app.monotonics.iter().enumerate() {
47// For future use
48 // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
49 // stmts.push(quote!(#[doc = #doc]));
50let cfgs = &monotonic.cfgs;
5152#[allow(clippy::cast_possible_truncation)]
53let idx = Index {
54 index: i as u32,
55 span: Span::call_site(),
56 };
57 stmts.push(quote!(
58 #(#cfgs)*
59 monotonics.#idx.reset();
60 ));
6162// Store the monotonic
63let name = util::monotonic_ident(&monotonic_ident.to_string());
64 stmts.push(quote!(
65 #(#cfgs)*
66 #name.get_mut().write(Some(monotonics.#idx));
67 ));
68 }
6970// Enable the interrupts -- this completes the `init`-ialization phase
71stmts.push(quote!(rtic::export::interrupt::enable();));
7273 stmts
74}