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    // mod_app_software_tasks -- free queues, buffers and `${task}Resources` constructors
17    Vec<TokenStream2>,
18    // root_software_tasks -- items that must be placed in the root of the crate:
19    // - `${task}Locals` structs
20    // - `${task}Resources` structs
21    // - `${task}` modules
22    Vec<TokenStream2>,
23    // user_software_tasks -- the `#[task]` functions written by the user
24    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        // Create free queues and inputs / instants buffers
40        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            // /// Queue version of a free-list that keeps track of empty slots in
52            // /// the following buffers
53            #(#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            // For future use
71            // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
72            mod_app.push(quote!(
73                #uninit
74                // /// Buffer that holds the instants associated to the inputs of a task
75                // #[doc = #doc]
76                #[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            // /// Buffer that holds the inputs of a task
91            #[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        // `${task}Resources`
100        let mut shared_needs_lt = false;
101        let mut local_needs_lt = false;
102
103        // `${task}Locals`
104        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}