2020-08-27 13:21:56 +02:00
|
|
|
use std::collections::HashSet;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic_syntax::ast::App;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
analyze::Analysis,
|
|
|
|
check::Extra,
|
|
|
|
codegen::{spawn_body, util},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Generates all `${ctxt}::Spawn` methods
|
|
|
|
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
|
|
|
|
let mut items = vec![];
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let mut seen = HashSet::<_>::new();
|
2019-06-13 23:56:59 +02:00
|
|
|
for (spawner, spawnees) in app.spawn_callers() {
|
|
|
|
let mut methods = vec![];
|
|
|
|
|
|
|
|
for name in spawnees {
|
|
|
|
let spawnee = &app.software_tasks[name];
|
2020-08-27 13:21:56 +02:00
|
|
|
//let receiver = spawnee.args.core;
|
2019-06-13 23:56:59 +02:00
|
|
|
let cfgs = &spawnee.cfgs;
|
|
|
|
let (args, _, untupled, ty) = util::regroup_inputs(&spawnee.inputs);
|
|
|
|
let args = &args;
|
|
|
|
|
|
|
|
if spawner.is_init() {
|
|
|
|
// `init` uses a special spawn implementation; it doesn't use the `spawn_${name}`
|
|
|
|
// functions which are shared by other contexts
|
|
|
|
|
|
|
|
let body = spawn_body::codegen(spawner, &name, app, analysis, extra);
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let let_instant = if app.uses_schedule() {
|
2019-06-13 23:56:59 +02:00
|
|
|
let m = extra.monotonic();
|
|
|
|
|
2020-06-11 19:18:29 +02:00
|
|
|
Some(quote!(let instant = unsafe { <#m as rtic::Monotonic>::zero() };))
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let section = util::link_section("text");
|
2019-06-13 23:56:59 +02:00
|
|
|
methods.push(quote!(
|
|
|
|
#(#cfgs)*
|
2019-06-29 09:11:42 +02:00
|
|
|
#section
|
2019-06-13 23:56:59 +02:00
|
|
|
fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
|
|
|
#let_instant
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
));
|
|
|
|
} else {
|
2020-08-27 13:21:56 +02:00
|
|
|
let spawn = util::spawn_ident(name);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
if !seen.contains(name) {
|
|
|
|
// generate a `spawn_${name}_S${sender}` function
|
|
|
|
seen.insert(name);
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let instant = if app.uses_schedule() {
|
2019-06-13 23:56:59 +02:00
|
|
|
let m = extra.monotonic();
|
|
|
|
|
2020-06-11 19:18:29 +02:00
|
|
|
Some(quote!(, instant: <#m as rtic::Monotonic>::Instant))
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let body = spawn_body::codegen(spawner, &name, app, analysis, extra);
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let section = util::link_section("text");
|
2019-06-13 23:56:59 +02:00
|
|
|
items.push(quote!(
|
2020-08-27 13:21:56 +02:00
|
|
|
//#cfg_sender
|
2019-06-13 23:56:59 +02:00
|
|
|
#(#cfgs)*
|
2019-06-29 09:11:42 +02:00
|
|
|
#section
|
2019-06-13 23:56:59 +02:00
|
|
|
unsafe fn #spawn(
|
2020-06-11 19:18:29 +02:00
|
|
|
priority: &rtic::export::Priority
|
2019-06-13 23:56:59 +02:00
|
|
|
#instant
|
|
|
|
#(,#args)*
|
|
|
|
) -> Result<(), #ty> {
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
let (let_instant, instant) = if app.uses_schedule() {
|
2019-06-13 23:56:59 +02:00
|
|
|
let m = extra.monotonic();
|
|
|
|
|
|
|
|
(
|
|
|
|
Some(if spawner.is_idle() {
|
2020-06-11 19:18:29 +02:00
|
|
|
quote!(let instant = <#m as rtic::Monotonic>::now();)
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
|
|
|
quote!(let instant = self.instant();)
|
|
|
|
}),
|
|
|
|
Some(quote!(, instant)),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(None, None)
|
|
|
|
};
|
|
|
|
|
|
|
|
methods.push(quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#[inline(always)]
|
|
|
|
fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
|
|
|
unsafe {
|
|
|
|
#let_instant
|
|
|
|
#spawn(self.priority() #instant #(,#untupled)*)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let lt = if spawner.is_init() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(quote!('a))
|
|
|
|
};
|
|
|
|
|
|
|
|
let spawner = spawner.ident(app);
|
|
|
|
debug_assert!(!methods.is_empty());
|
|
|
|
items.push(quote!(
|
2020-08-27 13:21:56 +02:00
|
|
|
//#cfg_sender
|
2019-06-13 23:56:59 +02:00
|
|
|
impl<#lt> #spawner::Spawn<#lt> {
|
|
|
|
#(#methods)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
items
|
|
|
|
}
|