diff options
author | 2023-03-04 21:10:24 +0000 | |
---|---|---|
committer | 2023-03-04 21:10:24 +0000 | |
commit | 7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b (patch) | |
tree | 80a47f0dc40059014e9448c4c2eb34c54dff45fe /rtic-macros/src/codegen/main.rs | |
parent | 1c5db277e4161470136dbd2a11e914ff1d383581 (diff) | |
parent | 98c5490d94950608d31cd5ad9dd260f2f853735c (diff) | |
download | rtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.tar.gz rtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.tar.zst rtic-7c7d6558f6d9c50fbb4d2487c98c9a5be15f2f7b.zip |
Merge #694
694: RTIC 2 r=AfoHT a=korken89
Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
Co-authored-by: Per Lindgren <per.lindgren@ltu.se>
Diffstat (limited to 'rtic-macros/src/codegen/main.rs')
-rw-r--r-- | rtic-macros/src/codegen/main.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/rtic-macros/src/codegen/main.rs b/rtic-macros/src/codegen/main.rs new file mode 100644 index 00000000..2775d259 --- /dev/null +++ b/rtic-macros/src/codegen/main.rs @@ -0,0 +1,52 @@ +use crate::{analyze::Analysis, codegen::util, syntax::ast::App}; +use proc_macro2::TokenStream as TokenStream2; +use quote::quote; + +use super::{assertions, post_init, pre_init}; + +/// Generates code for `fn main` +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { + let assertion_stmts = assertions::codegen(app, analysis); + + let pre_init_stmts = pre_init::codegen(app, analysis); + + let post_init_stmts = post_init::codegen(app, analysis); + + let call_idle = if let Some(idle) = &app.idle { + let name = &idle.name; + quote!(#name(#name::Context::new())) + } else if analysis.channels.get(&0).is_some() { + let dispatcher = util::zero_prio_dispatcher_ident(); + quote!(#dispatcher();) + } else { + quote!(loop { + rtic::export::nop() + }) + }; + + let main = util::suffixed("main"); + let init_name = &app.init.name; + quote!( + #[doc(hidden)] + #[no_mangle] + unsafe extern "C" fn #main() -> ! { + #(#assertion_stmts)* + + #(#pre_init_stmts)* + + #[inline(never)] + fn __rtic_init_resources<F>(f: F) where F: FnOnce() { + f(); + } + + // Wrap late_init_stmts in a function to ensure that stack space is reclaimed. + __rtic_init_resources(||{ + let (shared_resources, local_resources) = #init_name(#init_name::Context::new(core.into())); + + #(#post_init_stmts)* + }); + + #call_idle + } + ) +} |