1use crate::{analyze::Analysis, codegen::util, syntax::ast::App};
2use proc_macro2::TokenStream as TokenStream2;
3use quote::quote;
45/// Generates code that runs after `#[init]` returns
6pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
7let mut stmts = vec![];
89// Initialize shared resources
10for (name, res) in &app.shared_resources {
11let mangled_name = util::static_shared_resource_ident(name);
12// If it's live
13let cfgs = res.cfgs.clone();
14if analysis.shared_resources.get(name).is_some() {
15 stmts.push(quote!(
16// We include the cfgs
17#(#cfgs)*
18// Resource is a RacyCell<MaybeUninit<T>>
19 // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
20 // - `write` the defined value for the late resource T
21#mangled_name.get_mut().write(core::mem::MaybeUninit::new(shared_resources.#name));
22 ));
23 }
24 }
2526// Initialize local resources
27for (name, res) in &app.local_resources {
28let mangled_name = util::static_local_resource_ident(name);
29// If it's live
30let cfgs = res.cfgs.clone();
31if analysis.local_resources.get(name).is_some() {
32 stmts.push(quote!(
33// We include the cfgs
34#(#cfgs)*
35// Resource is a RacyCell<MaybeUninit<T>>
36 // - `get_mut` to obtain a raw pointer to `MaybeUninit<T>`
37 // - `write` the defined value for the late resource T
38#mangled_name.get_mut().write(core::mem::MaybeUninit::new(local_resources.#name));
39 ));
40 }
41 }
4243// Enable the interrupts -- this completes the `init`-ialization phase
44stmts.push(quote!(rtic::export::interrupt::enable();));
4546 stmts
47}