2019-06-13 23:56:59 +02:00
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2020-09-01 16:39:05 +02:00
|
|
|
use rtic_syntax::{analyze::Ownership, ast::App};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
use crate::{analyze::Analysis, check::Extra, codegen::util};
|
|
|
|
|
2021-04-08 18:25:09 +02:00
|
|
|
/// Generates `static` variables and resource proxies
|
|
|
|
/// Early resources are stored in `RacyCell<T>`
|
|
|
|
/// Late resource are stored in `RacyCell<MaybeUninit<T>>`
|
|
|
|
///
|
|
|
|
/// Safety:
|
|
|
|
/// - RacyCell<T> access is `unsafe`.
|
|
|
|
/// - RacyCell<MaybeUninit> is always written to before user access, thus
|
|
|
|
// the generated code for user access can safely `assume_init`.
|
2019-06-13 23:56:59 +02:00
|
|
|
pub fn codegen(
|
|
|
|
app: &App,
|
|
|
|
analysis: &Analysis,
|
|
|
|
extra: &Extra,
|
|
|
|
) -> (
|
2021-04-08 18:25:09 +02:00
|
|
|
// mod_app -- the `static` variables behind the proxies
|
2019-06-13 23:56:59 +02:00
|
|
|
Vec<TokenStream2>,
|
|
|
|
// mod_resources -- the `resources` module
|
|
|
|
TokenStream2,
|
|
|
|
) {
|
2020-10-01 18:17:15 +02:00
|
|
|
let mut mod_app = vec![];
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut mod_resources = vec![];
|
|
|
|
|
2020-08-27 13:21:56 +02:00
|
|
|
for (name, res, expr, _) in app.resources(analysis) {
|
2019-06-13 23:56:59 +02:00
|
|
|
let cfgs = &res.cfgs;
|
|
|
|
let ty = &res.ty;
|
2021-02-25 19:05:39 +01:00
|
|
|
let mangled_name = util::mark_internal_ident(&name);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
{
|
2021-04-08 18:25:09 +02:00
|
|
|
// late resources in `util::link_section_uninit`
|
2020-08-27 13:21:56 +02:00
|
|
|
let section = if expr.is_none() {
|
2020-09-01 16:39:05 +02:00
|
|
|
util::link_section_uninit(true)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2021-04-08 18:25:09 +02:00
|
|
|
// resource type and assigned value
|
2019-06-13 23:56:59 +02:00
|
|
|
let (ty, expr) = if let Some(expr) = expr {
|
2021-04-08 18:25:09 +02:00
|
|
|
// early resource
|
|
|
|
(
|
|
|
|
quote!(rtic::RacyCell<#ty>),
|
|
|
|
quote!(rtic::RacyCell::new(#expr)),
|
|
|
|
)
|
2019-06-13 23:56:59 +02:00
|
|
|
} else {
|
2021-04-08 18:25:09 +02:00
|
|
|
// late resource
|
2019-06-13 23:56:59 +02:00
|
|
|
(
|
2021-04-08 18:25:09 +02:00
|
|
|
quote!(rtic::RacyCell<core::mem::MaybeUninit<#ty>>),
|
|
|
|
quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())),
|
2019-06-13 23:56:59 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let attrs = &res.attrs;
|
2021-04-08 18:25:09 +02:00
|
|
|
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-04-08 18:25:09 +02:00
|
|
|
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
2020-10-01 18:17:15 +02:00
|
|
|
mod_app.push(quote!(
|
2019-07-10 22:42:44 +02:00
|
|
|
#[allow(non_upper_case_globals)]
|
2021-04-08 18:25:09 +02:00
|
|
|
// #[doc = #doc]
|
2021-02-25 19:05:39 +01:00
|
|
|
#[doc(hidden)]
|
2019-06-13 23:56:59 +02:00
|
|
|
#(#attrs)*
|
|
|
|
#(#cfgs)*
|
2019-06-29 09:11:42 +02:00
|
|
|
#section
|
2021-04-08 18:25:09 +02:00
|
|
|
static #mangled_name: #ty = #expr;
|
2019-06-13 23:56:59 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-10-22 21:36:32 +02:00
|
|
|
let r_prop = &res.properties;
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-04-08 18:25:09 +02:00
|
|
|
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
|
|
|
|
2020-10-22 21:36:32 +02:00
|
|
|
if !r_prop.task_local && !r_prop.lock_free {
|
2019-07-10 22:42:44 +02:00
|
|
|
mod_resources.push(quote!(
|
2021-04-08 18:25:09 +02:00
|
|
|
// #[doc = #doc]
|
|
|
|
#[doc(hidden)]
|
2019-07-10 22:42:44 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#(#cfgs)*
|
|
|
|
pub struct #name<'a> {
|
|
|
|
priority: &'a Priority,
|
|
|
|
}
|
|
|
|
|
|
|
|
#(#cfgs)*
|
|
|
|
impl<'a> #name<'a> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn new(priority: &'a Priority) -> Self {
|
|
|
|
#name { priority }
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 22:42:44 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn priority(&self) -> &Priority {
|
|
|
|
self.priority
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
2019-07-10 22:42:44 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2021-04-08 18:25:09 +02:00
|
|
|
let (ptr, _doc) = if expr.is_none() {
|
|
|
|
// late resource
|
|
|
|
(
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#mangled_name.get_mut_unchecked().as_mut_ptr()
|
|
|
|
),
|
|
|
|
"late",
|
2020-04-08 16:43:47 +02:00
|
|
|
)
|
2019-07-10 22:42:44 +02:00
|
|
|
} else {
|
2021-04-08 18:25:09 +02:00
|
|
|
// early resource
|
|
|
|
(
|
|
|
|
quote!(
|
|
|
|
#(#cfgs)*
|
|
|
|
#mangled_name.get_mut_unchecked()
|
|
|
|
),
|
|
|
|
"early",
|
2020-04-08 16:43:47 +02:00
|
|
|
)
|
2019-07-10 22:42:44 +02:00
|
|
|
};
|
|
|
|
|
2020-10-22 21:36:32 +02:00
|
|
|
let ceiling = match analysis.ownerships.get(name) {
|
|
|
|
Some(Ownership::Owned { priority }) => *priority,
|
|
|
|
Some(Ownership::CoOwned { priority }) => *priority,
|
|
|
|
Some(Ownership::Contended { ceiling }) => *ceiling,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
|
2021-04-22 18:38:42 +02:00
|
|
|
// For future use
|
2021-04-08 18:25:09 +02:00
|
|
|
// let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!());
|
|
|
|
|
2020-10-01 18:17:15 +02:00
|
|
|
mod_app.push(util::impl_mutex(
|
2019-07-10 22:42:44 +02:00
|
|
|
extra,
|
|
|
|
cfgs,
|
|
|
|
true,
|
|
|
|
name,
|
|
|
|
quote!(#ty),
|
2020-10-22 21:36:32 +02:00
|
|
|
ceiling,
|
2019-07-10 22:42:44 +02:00
|
|
|
ptr,
|
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mod_resources = if mod_resources.is_empty() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
|
|
|
quote!(mod resources {
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic::export::Priority;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
#(#mod_resources)*
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2020-10-21 20:20:26 +02:00
|
|
|
(mod_app, mod_resources)
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|