cortex_m_rtic_macros/codegen/
software_tasks.rs
1use proc_macro2::TokenStream as TokenStream2;
2use quote::quote;
3use rtic_syntax::{ast::App, Context};
4
5use crate::{
6 analyze::Analysis,
7 check::Extra,
8 codegen::{local_resources_struct, module, shared_resources_struct, util},
9};
10
11pub fn codegen(
12 app: &App,
13 analysis: &Analysis,
14 extra: &Extra,
15) -> (
16 Vec<TokenStream2>,
18 Vec<TokenStream2>,
23 Vec<TokenStream2>,
25) {
26 let mut mod_app = vec![];
27 let mut root = vec![];
28 let mut user_tasks = vec![];
29
30 for (name, task) in &app.software_tasks {
31 let inputs = &task.inputs;
32 let cfgs = &task.cfgs;
33 let (_, _, _, input_ty) = util::regroup_inputs(inputs);
34
35 let cap = task.args.capacity;
36 let cap_lit = util::capacity_literal(cap as usize);
37 let cap_lit_p1 = util::capacity_literal(cap as usize + 1);
38
39 let fq = util::fq_ident(name);
41
42 #[allow(clippy::redundant_closure)]
43 let (fq_ty, fq_expr, mk_uninit): (_, _, Box<dyn Fn() -> Option<_>>) = {
44 (
45 quote!(rtic::export::SCFQ<#cap_lit_p1>),
46 quote!(rtic::export::Queue::new()),
47 Box::new(|| Some(util::link_section_uninit())),
48 )
49 };
50 mod_app.push(quote!(
51 #(#cfgs)*
54 #[allow(non_camel_case_types)]
55 #[allow(non_upper_case_globals)]
56 #[doc(hidden)]
57 static #fq: rtic::RacyCell<#fq_ty> = rtic::RacyCell::new(#fq_expr);
58 ));
59
60 let elems = &(0..cap)
61 .map(|_| quote!(core::mem::MaybeUninit::uninit()))
62 .collect::<Vec<_>>();
63
64 for (_, monotonic) in &app.monotonics {
65 let instants = util::monotonic_instants_ident(name, &monotonic.ident);
66 let mono_type = &monotonic.ty;
67 let cfgs = &monotonic.cfgs;
68
69 let uninit = mk_uninit();
70 mod_app.push(quote!(
73 #uninit
74 #[allow(non_camel_case_types)]
77 #[allow(non_upper_case_globals)]
78 #[doc(hidden)]
79 #(#cfgs)*
80 static #instants:
81 rtic::RacyCell<[core::mem::MaybeUninit<<#mono_type as rtic::Monotonic>::Instant>; #cap_lit]> =
82 rtic::RacyCell::new([#(#elems,)*]);
83 ));
84 }
85
86 let uninit = mk_uninit();
87 let inputs_ident = util::inputs_ident(name);
88 mod_app.push(quote!(
89 #uninit
90 #[allow(non_camel_case_types)]
92 #[allow(non_upper_case_globals)]
93 #[doc(hidden)]
94 #(#cfgs)*
95 static #inputs_ident: rtic::RacyCell<[core::mem::MaybeUninit<#input_ty>; #cap_lit]> =
96 rtic::RacyCell::new([#(#elems,)*]);
97 ));
98
99 let mut shared_needs_lt = false;
101 let mut local_needs_lt = false;
102
103 if !task.args.local_resources.is_empty() {
105 let (item, constructor) = local_resources_struct::codegen(
106 Context::SoftwareTask(name),
107 &mut local_needs_lt,
108 app,
109 );
110
111 root.push(item);
112
113 mod_app.push(constructor);
114 }
115
116 if !task.args.shared_resources.is_empty() {
117 let (item, constructor) = shared_resources_struct::codegen(
118 Context::SoftwareTask(name),
119 &mut shared_needs_lt,
120 app,
121 );
122
123 root.push(item);
124
125 mod_app.push(constructor);
126 }
127
128 if !&task.is_extern {
129 let context = &task.context;
130 let attrs = &task.attrs;
131 let cfgs = &task.cfgs;
132 let stmts = &task.stmts;
133 let user_task_doc = format!(" User SW task {name}");
134 user_tasks.push(quote!(
135 #[doc = #user_task_doc]
136 #(#attrs)*
137 #(#cfgs)*
138 #[allow(non_snake_case)]
139 fn #name(#context: #name::Context #(,#inputs)*) {
140 use rtic::Mutex as _;
141 use rtic::mutex::prelude::*;
142
143 #(#stmts)*
144 }
145 ));
146 }
147
148 root.push(module::codegen(
149 Context::SoftwareTask(name),
150 shared_needs_lt,
151 local_needs_lt,
152 app,
153 analysis,
154 extra,
155 ));
156 }
157
158 (mod_app, root, user_tasks)
159}