2022-12-31 14:45:13 +01:00
|
|
|
use crate::syntax::{analyze::Ownership, ast::App};
|
|
|
|
use crate::{analyze::Analysis, codegen::util};
|
2019-06-13 23:56:59 +02:00
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2023-02-11 08:55:19 +01:00
|
|
|
|
|
|
|
use super::bindings::impl_mutex;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2021-07-05 21:40:01 +02:00
|
|
|
/// Generates `static` variables and shared resource proxies
|
2023-01-07 14:26:55 +01:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis) -> 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![];
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
for (name, res) in &app.shared_resources {
|
2019-06-13 23:56:59 +02:00
|
|
|
let cfgs = &res.cfgs;
|
|
|
|
let ty = &res.ty;
|
2021-12-25 13:17:16 +01:00
|
|
|
let mangled_name = &util::static_shared_resource_ident(name);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
let attrs = &res.attrs;
|
2021-04-08 18:25:09 +02:00
|
|
|
|
2022-07-06 17:43:38 +02:00
|
|
|
// late resources in `util::link_section_uninit`
|
|
|
|
// unless user specifies custom link section
|
|
|
|
let section = if attrs.iter().any(|attr| attr.path.is_ident("link_section")) {
|
|
|
|
None
|
2022-07-27 20:25:34 +02:00
|
|
|
} else {
|
2022-07-06 17:43:38 +02:00
|
|
|
Some(util::link_section_uninit())
|
|
|
|
};
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
// For future use
|
|
|
|
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
|
|
|
mod_app.push(quote!(
|
2021-08-19 08:21:18 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
2021-07-06 22:47:48 +02:00
|
|
|
#[allow(non_upper_case_globals)]
|
|
|
|
// #[doc = #doc]
|
|
|
|
#[doc(hidden)]
|
|
|
|
#(#attrs)*
|
|
|
|
#(#cfgs)*
|
|
|
|
#section
|
|
|
|
static #mangled_name: rtic::RacyCell<core::mem::MaybeUninit<#ty>> = rtic::RacyCell::new(core::mem::MaybeUninit::uninit());
|
|
|
|
));
|
2019-06-13 23:56:59 +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!());
|
|
|
|
|
2021-11-11 14:22:47 +01:00
|
|
|
let shared_name = util::need_to_lock_ident(name);
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
if !res.properties.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)*
|
2021-11-11 14:22:47 +01:00
|
|
|
pub struct #shared_name<'a> {
|
2023-01-07 11:24:13 +01:00
|
|
|
__rtic_internal_p: ::core::marker::PhantomData<&'a ()>,
|
2019-07-10 22:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#(#cfgs)*
|
2021-11-11 14:22:47 +01:00
|
|
|
impl<'a> #shared_name<'a> {
|
2019-07-10 22:42:44 +02:00
|
|
|
#[inline(always)]
|
2023-01-07 11:24:13 +01:00
|
|
|
pub unsafe fn new() -> Self {
|
|
|
|
#shared_name { __rtic_internal_p: ::core::marker::PhantomData }
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
2019-07-10 22:42:44 +02:00
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
let ptr = quote!(
|
|
|
|
#(#cfgs)*
|
2021-11-03 08:27:05 +01:00
|
|
|
#mangled_name.get_mut() as *mut _
|
2021-07-06 22:47:48 +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) {
|
2022-02-18 19:38:48 +01:00
|
|
|
Some(Ownership::Owned { priority } | Ownership::CoOwned { priority }) => *priority,
|
2020-10-22 21:36:32 +02:00
|
|
|
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!());
|
|
|
|
|
2023-02-11 08:55:19 +01:00
|
|
|
mod_app.push(impl_mutex(
|
2022-12-31 14:45:13 +01:00
|
|
|
app,
|
2023-02-11 08:55:19 +01:00
|
|
|
analysis,
|
2019-07-10 22:42:44 +02:00
|
|
|
cfgs,
|
|
|
|
true,
|
2021-11-11 14:22:47 +01:00
|
|
|
&shared_name,
|
2022-02-18 19:38:48 +01:00
|
|
|
"e!(#ty),
|
2020-10-22 21:36:32 +02:00
|
|
|
ceiling,
|
2022-02-18 19:38:48 +01:00
|
|
|
&ptr,
|
2019-07-10 22:42:44 +02:00
|
|
|
));
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mod_resources = if mod_resources.is_empty() {
|
|
|
|
quote!()
|
|
|
|
} else {
|
2021-07-06 22:47:48 +02:00
|
|
|
quote!(mod shared_resources {
|
2019-06-13 23:56:59 +02:00
|
|
|
#(#mod_resources)*
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-01-07 14:26:55 +01:00
|
|
|
quote!(
|
|
|
|
#(#mod_app)*
|
|
|
|
|
|
|
|
#mod_resources
|
|
|
|
)
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|