mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-12-03 16:57:14 +01:00
Minimal app now compiles
This commit is contained in:
parent
3f85cb5caf
commit
ef5307d83a
14 changed files with 289 additions and 353 deletions
|
@ -84,7 +84,8 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
|
|||
}
|
||||
));
|
||||
|
||||
let (mod_app_resources, mod_resources) = resources::codegen(app, analysis, extra);
|
||||
let (mod_app_shared_resources, mod_shared_resources) = shared_resources::codegen(app, analysis, extra);
|
||||
let (mod_app_local_resources, mod_local_resources) = local_resources::codegen(app, analysis, extra);
|
||||
|
||||
let (mod_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks) =
|
||||
hardware_tasks::codegen(app, analysis, extra);
|
||||
|
@ -186,7 +187,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
|
|||
|
||||
#(#root)*
|
||||
|
||||
#mod_resources
|
||||
#mod_shared_resources
|
||||
|
||||
#mod_local_resources
|
||||
|
||||
#(#root_hardware_tasks)*
|
||||
|
||||
|
@ -195,7 +198,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
|
|||
/// app module
|
||||
#(#mod_app)*
|
||||
|
||||
#(#mod_app_resources)*
|
||||
#(#mod_app_shared_resources)*
|
||||
|
||||
#(#mod_app_local_resources)*
|
||||
|
||||
#(#mod_app_hardware_tasks)*
|
||||
|
||||
|
|
|
@ -76,11 +76,12 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
|
|||
let inputs = util::mark_internal_ident(&inputs);
|
||||
let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs);
|
||||
|
||||
let locals_new = if task.args.local_resources.is_empty() {
|
||||
quote!()
|
||||
} else {
|
||||
quote!(#name::Locals::new(),)
|
||||
};
|
||||
// TODO Fix me
|
||||
// let locals_new = if task.args.local_resources.is_empty() {
|
||||
// quote!()
|
||||
// } else {
|
||||
// quote!(#name::Locals::new(),)
|
||||
// };
|
||||
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
|
@ -94,7 +95,6 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
|
|||
#fq.get_mut_unchecked().split().0.enqueue_unchecked(index);
|
||||
let priority = &rtic::export::Priority::new(PRIORITY);
|
||||
#name(
|
||||
#locals_new
|
||||
#name::Context::new(priority)
|
||||
#(,#pats)*
|
||||
)
|
||||
|
|
|
@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
|
|||
use crate::{
|
||||
analyze::Analysis,
|
||||
check::Extra,
|
||||
codegen::{locals, module, resources_struct},
|
||||
codegen::{local_resources_struct, module, shared_resources_struct},
|
||||
};
|
||||
|
||||
/// Generate support code for hardware tasks (`#[exception]`s and `#[interrupt]`s)
|
||||
|
@ -29,11 +29,12 @@ pub fn codegen(
|
|||
let mut user_tasks = vec![];
|
||||
|
||||
for (name, task) in &app.hardware_tasks {
|
||||
let locals_new = if task.args.local_resources.is_empty() {
|
||||
quote!()
|
||||
} else {
|
||||
quote!(#name::Locals::new(),)
|
||||
};
|
||||
// TODO: Fix locals
|
||||
// let locals_new = if task.args.local_resources.is_empty() {
|
||||
// quote!()
|
||||
// } else {
|
||||
// quote!(#name::Locals::new(),)
|
||||
// };
|
||||
|
||||
let symbol = task.args.binds.clone();
|
||||
let priority = task.args.priority;
|
||||
|
@ -50,7 +51,6 @@ pub fn codegen(
|
|||
|
||||
rtic::export::run(PRIORITY, || {
|
||||
#name(
|
||||
#locals_new
|
||||
#name::Context::new(&rtic::export::Priority::new(PRIORITY))
|
||||
)
|
||||
});
|
||||
|
@ -59,10 +59,21 @@ pub fn codegen(
|
|||
|
||||
let mut needs_lt = false;
|
||||
|
||||
// TODO: Fix locals
|
||||
// `${task}Locals`
|
||||
if !task.args.local_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
local_resources_struct::codegen(Context::HardwareTask(name), &mut needs_lt, app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
// `${task}Resources`
|
||||
if !task.args.shared_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
resources_struct::codegen(Context::HardwareTask(name), &mut needs_lt, app);
|
||||
shared_resources_struct::codegen(Context::HardwareTask(name), &mut needs_lt, app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
|
@ -77,24 +88,25 @@ pub fn codegen(
|
|||
extra,
|
||||
));
|
||||
|
||||
// `${task}Locals`
|
||||
let mut locals_pat = None;
|
||||
if !task.locals.is_empty() {
|
||||
let (struct_, pat) = locals::codegen(Context::HardwareTask(name), &task.locals, app);
|
||||
// TODO: Fix locals
|
||||
// // `${task}Locals`
|
||||
// let mut locals_pat = None;
|
||||
// if !task.locals.is_empty() {
|
||||
// let (struct_, pat) =
|
||||
// local_resources_struct::codegen(Context::HardwareTask(name), &task.locals, app);
|
||||
|
||||
root.push(struct_);
|
||||
locals_pat = Some(pat);
|
||||
}
|
||||
// root.push(struct_);
|
||||
// locals_pat = Some(pat);
|
||||
// }
|
||||
|
||||
if !&task.is_extern {
|
||||
if !task.is_extern {
|
||||
let attrs = &task.attrs;
|
||||
let context = &task.context;
|
||||
let stmts = &task.stmts;
|
||||
let locals_pat = locals_pat.iter();
|
||||
user_tasks.push(quote!(
|
||||
#(#attrs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context) {
|
||||
fn #name(#context: #name::Context) {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
|
|||
use crate::{
|
||||
analyze::Analysis,
|
||||
check::Extra,
|
||||
codegen::{locals, module, resources_struct},
|
||||
codegen::{local_resources_struct, module, shared_resources_struct},
|
||||
};
|
||||
|
||||
/// Generates support code for `#[idle]` functions
|
||||
|
@ -26,30 +26,28 @@ pub fn codegen(
|
|||
// call_idle
|
||||
TokenStream2,
|
||||
) {
|
||||
if !app.idles.is_empty() {
|
||||
let idle = &app.idles.first().unwrap();
|
||||
if let Some(idle) = &app.idle {
|
||||
let mut needs_lt = false;
|
||||
let mut mod_app = None;
|
||||
let mut root_idle = vec![];
|
||||
let mut locals_pat = None;
|
||||
let mut locals_new = None;
|
||||
|
||||
let name = &idle.name;
|
||||
|
||||
if !idle.args.resources.is_empty() {
|
||||
let (item, constructor) = resources_struct::codegen(Context::Idle, &mut needs_lt, app);
|
||||
if !idle.args.shared_resources.is_empty() {
|
||||
let (item, constructor) = shared_resources_struct::codegen(Context::Idle, &mut needs_lt, app);
|
||||
|
||||
root_idle.push(item);
|
||||
mod_app = Some(constructor);
|
||||
}
|
||||
|
||||
if !idle.locals.is_empty() {
|
||||
let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);
|
||||
// TODO: Fix locals
|
||||
// if !idle.locals.is_empty() {
|
||||
// let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);
|
||||
|
||||
locals_new = Some(quote!(#name::Locals::new()));
|
||||
locals_pat = Some(pat);
|
||||
root_idle.push(locals);
|
||||
}
|
||||
// locals_new = Some(quote!(#name::Locals::new()));
|
||||
// locals_pat = Some(pat);
|
||||
// root_idle.push(locals);
|
||||
// }
|
||||
|
||||
root_idle.push(module::codegen(
|
||||
Context::Idle,
|
||||
|
@ -62,11 +60,10 @@ pub fn codegen(
|
|||
let attrs = &idle.attrs;
|
||||
let context = &idle.context;
|
||||
let stmts = &idle.stmts;
|
||||
let locals_pat = locals_pat.iter();
|
||||
let user_idle = Some(quote!(
|
||||
#(#attrs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context) -> ! {
|
||||
fn #name(#context: #name::Context) -> ! {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
|
@ -74,9 +71,7 @@ pub fn codegen(
|
|||
}
|
||||
));
|
||||
|
||||
let locals_new = locals_new.iter();
|
||||
let call_idle = quote!(#name(
|
||||
#(#locals_new,)*
|
||||
#name::Context::new(&rtic::export::Priority::new(0))
|
||||
));
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
|
|||
use crate::{
|
||||
analyze::Analysis,
|
||||
check::Extra,
|
||||
codegen::{locals, module, resources_struct},
|
||||
codegen::{local_resources_struct, module},
|
||||
};
|
||||
|
||||
type CodegenResult = (
|
||||
|
@ -18,68 +18,96 @@ type CodegenResult = (
|
|||
// - the `${init}` module, which contains types like `${init}::Context`
|
||||
Vec<TokenStream2>,
|
||||
// user_init -- the `#[init]` function written by the user
|
||||
Option<TokenStream2>,
|
||||
// call_init -- the call to the user `#[init]` if there's one
|
||||
Option<TokenStream2>,
|
||||
TokenStream2,
|
||||
// call_init -- the call to the user `#[init]`
|
||||
TokenStream2,
|
||||
);
|
||||
|
||||
/// Generates support code for `#[init]` functions
|
||||
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult {
|
||||
if !app.inits.is_empty() {
|
||||
let init = &app.inits.first().unwrap();
|
||||
let mut needs_lt = false;
|
||||
let name = &init.name;
|
||||
let init = &app.init;
|
||||
let mut needs_lt = false;
|
||||
let name = &init.name;
|
||||
|
||||
let mut root_init = vec![];
|
||||
let mut root_init = vec![];
|
||||
|
||||
let mut locals_pat = None;
|
||||
let mut locals_new = None;
|
||||
if !init.locals.is_empty() {
|
||||
let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app);
|
||||
// TODO: Fix locals
|
||||
// let mut locals_pat = None;
|
||||
// let mut locals_new = None;
|
||||
// if !init.locals.is_empty() {
|
||||
// let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app);
|
||||
|
||||
locals_new = Some(quote!(#name::Locals::new()));
|
||||
locals_pat = Some(pat);
|
||||
root_init.push(struct_);
|
||||
// locals_new = Some(quote!(#name::Locals::new()));
|
||||
// locals_pat = Some(pat);
|
||||
// root_init.push(struct_);
|
||||
// }
|
||||
|
||||
let context = &init.context;
|
||||
let attrs = &init.attrs;
|
||||
let stmts = &init.stmts;
|
||||
let shared = &init.user_shared_struct;
|
||||
let local = &init.user_local_struct;
|
||||
|
||||
let shared_resources: Vec<_> = app
|
||||
.shared_resources
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let ty = &v.ty;
|
||||
let cfgs = &v.cfgs;
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#k: #ty,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let local_resources: Vec<_> = app
|
||||
.local_resources
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let ty = &v.ty;
|
||||
let cfgs = &v.cfgs;
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#k: #ty,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
root_init.push(quote! {
|
||||
struct #shared {
|
||||
#(#shared_resources)*
|
||||
}
|
||||
|
||||
let context = &init.context;
|
||||
let attrs = &init.attrs;
|
||||
let stmts = &init.stmts;
|
||||
let locals_pat = locals_pat.iter();
|
||||
|
||||
let user_init_return = quote! {#name::LateResources, #name::Monotonics};
|
||||
|
||||
let user_init = Some(quote!(
|
||||
#(#attrs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context) -> (#user_init_return) {
|
||||
#(#stmts)*
|
||||
}
|
||||
));
|
||||
|
||||
let mut mod_app = None;
|
||||
if !init.args.resources.is_empty() {
|
||||
let (item, constructor) = resources_struct::codegen(Context::Init, &mut needs_lt, app);
|
||||
|
||||
root_init.push(item);
|
||||
mod_app = Some(constructor);
|
||||
struct #local {
|
||||
#(#local_resources)*
|
||||
}
|
||||
});
|
||||
|
||||
let locals_new = locals_new.iter();
|
||||
let call_init = Some(
|
||||
quote!(let (late, mut monotonics) = #name(#(#locals_new,)* #name::Context::new(core.into()));),
|
||||
);
|
||||
// let locals_pat = locals_pat.iter();
|
||||
|
||||
root_init.push(module::codegen(
|
||||
Context::Init,
|
||||
needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
extra,
|
||||
));
|
||||
let user_init_return = quote! {#shared, #local, #name::Monotonics};
|
||||
|
||||
(mod_app, root_init, user_init, call_init)
|
||||
} else {
|
||||
(None, vec![], None, None)
|
||||
}
|
||||
let user_init = quote!(
|
||||
#(#attrs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#context: #name::Context) -> (#user_init_return) {
|
||||
#(#stmts)*
|
||||
}
|
||||
);
|
||||
|
||||
let mut mod_app = None;
|
||||
|
||||
// let locals_new = locals_new.iter();
|
||||
let call_init = quote! {
|
||||
let (shared_resources, local_resources, mut monotonics) = #name(#name::Context::new(core.into()));
|
||||
};
|
||||
|
||||
root_init.push(module::codegen(
|
||||
Context::Init,
|
||||
needs_lt,
|
||||
app,
|
||||
analysis,
|
||||
extra,
|
||||
));
|
||||
|
||||
(mod_app, root_init, user_init, call_init)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
use proc_macro2::TokenStream as TokenStream2;
|
||||
use quote::quote;
|
||||
use rtic_syntax::{analyze::Ownership, ast::App};
|
||||
use rtic_syntax::ast::App;
|
||||
|
||||
use crate::{analyze::Analysis, check::Extra, codegen::util};
|
||||
|
||||
/// Generates `local` variables and local resource proxies
|
||||
///
|
||||
/// I.e. the `static` variables and theirs proxies.
|
||||
pub fn codegen(
|
||||
app: &App,
|
||||
analysis: &Analysis,
|
||||
|
@ -16,131 +18,42 @@ pub fn codegen(
|
|||
TokenStream2,
|
||||
) {
|
||||
let mut mod_app = vec![];
|
||||
let mut mod_resources = vec![];
|
||||
// let mut mod_resources: _ = vec![];
|
||||
|
||||
for (name, res) in app.local_resources {
|
||||
// All local resources declared in the `#[local]' struct
|
||||
for (name, res) in &app.local_resources {
|
||||
// let expr = &res.expr; // TODO: Extract from tasks???...
|
||||
let cfgs = &res.cfgs;
|
||||
let ty = &res.ty;
|
||||
let mangled_name = util::mark_internal_ident(&name);
|
||||
let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name));
|
||||
|
||||
{
|
||||
// late resources in `util::link_section_uninit`
|
||||
let section = if expr.is_none() {
|
||||
util::link_section_uninit(true)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let ty = quote!(rtic::RacyCell<core::mem::MaybeUninit<#ty>>);
|
||||
let expr = quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit()));
|
||||
|
||||
// resource type and assigned value
|
||||
let (ty, expr) = if let Some(expr) = expr {
|
||||
// early resource
|
||||
(
|
||||
quote!(rtic::RacyCell<#ty>),
|
||||
quote!(rtic::RacyCell::new(#expr)),
|
||||
)
|
||||
} else {
|
||||
// late resource
|
||||
(
|
||||
quote!(rtic::RacyCell<core::mem::MaybeUninit<#ty>>),
|
||||
quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())),
|
||||
)
|
||||
};
|
||||
let attrs = &res.attrs;
|
||||
// late resources in `util::link_section_uninit`
|
||||
let section = util::link_section_uninit(true);
|
||||
|
||||
let attrs = &res.attrs;
|
||||
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
||||
mod_app.push(quote!(
|
||||
#[allow(non_upper_case_globals)]
|
||||
// #[doc = #doc]
|
||||
#[doc(hidden)]
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#section
|
||||
static #mangled_name: #ty = #expr;
|
||||
));
|
||||
}
|
||||
|
||||
let r_prop = &res.properties;
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
||||
|
||||
if !r_prop.task_local && !r_prop.lock_free {
|
||||
mod_resources.push(quote!(
|
||||
// #[doc = #doc]
|
||||
#[doc(hidden)]
|
||||
#[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 }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn priority(&self) -> &Priority {
|
||||
self.priority
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
let (ptr, _doc) = if expr.is_none() {
|
||||
// late resource
|
||||
(
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr()
|
||||
),
|
||||
"late",
|
||||
)
|
||||
} else {
|
||||
// early resource
|
||||
(
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#mangled_name.get_mut_unchecked()
|
||||
),
|
||||
"early",
|
||||
)
|
||||
};
|
||||
|
||||
let ceiling = match analysis.ownerships.get(name) {
|
||||
Some(Ownership::Owned { priority }) => *priority,
|
||||
Some(Ownership::CoOwned { priority }) => *priority,
|
||||
Some(Ownership::Contended { ceiling }) => *ceiling,
|
||||
None => 0,
|
||||
};
|
||||
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal ({} resource): {}:{}", doc, file!(), line!());
|
||||
|
||||
mod_app.push(util::impl_mutex(
|
||||
extra,
|
||||
cfgs,
|
||||
true,
|
||||
name,
|
||||
quote!(#ty),
|
||||
ceiling,
|
||||
ptr,
|
||||
));
|
||||
}
|
||||
mod_app.push(quote!(
|
||||
#[allow(non_upper_case_globals)]
|
||||
// #[doc = #doc]
|
||||
#[doc(hidden)]
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#section
|
||||
static #mangled_name: #ty = #expr;
|
||||
));
|
||||
}
|
||||
|
||||
let mod_resources = if mod_resources.is_empty() {
|
||||
quote!()
|
||||
} else {
|
||||
quote!(mod resources {
|
||||
use rtic::export::Priority;
|
||||
// let mod_resources = if mod_resources.is_empty() {
|
||||
// quote!()
|
||||
// } else {
|
||||
// quote!(mod local_resources {
|
||||
// #(#mod_resources)*
|
||||
// })
|
||||
// };
|
||||
|
||||
#(#mod_resources)*
|
||||
})
|
||||
};
|
||||
|
||||
(mod_app, mod_resources)
|
||||
(mod_app, TokenStream2::new())
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
|
||||
let resources = match ctxt {
|
||||
Context::Init => &app.init.args.local_resources,
|
||||
Context::Idle => &app.idle.unwrap().args.local_resources,
|
||||
Context::Idle => &app.idle.as_ref().unwrap().args.local_resources,
|
||||
Context::HardwareTask(name) => &app.hardware_tasks[name].args.local_resources,
|
||||
Context::SoftwareTask(name) => &app.software_tasks[name].args.local_resources,
|
||||
};
|
||||
|
@ -33,7 +33,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
};
|
||||
|
||||
let ty = &res.ty;
|
||||
let mangled_name = util::mark_internal_ident(&name);
|
||||
let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name));
|
||||
|
||||
fields.push(quote!(
|
||||
#(#cfgs)*
|
||||
|
|
|
@ -56,16 +56,16 @@ pub fn codegen(
|
|||
Context::SoftwareTask(_) => {}
|
||||
}
|
||||
|
||||
if ctxt.has_locals(app) {
|
||||
let ident = util::locals_ident(ctxt, app);
|
||||
module_items.push(quote!(
|
||||
#[doc(inline)]
|
||||
pub use super::#ident as Locals;
|
||||
));
|
||||
}
|
||||
// if ctxt.has_locals(app) {
|
||||
// let ident = util::locals_ident(ctxt, app);
|
||||
// module_items.push(quote!(
|
||||
// #[doc(inline)]
|
||||
// pub use super::#ident as Locals;
|
||||
// ));
|
||||
// }
|
||||
|
||||
if ctxt.has_resources(app) {
|
||||
let ident = util::resources_ident(ctxt, app);
|
||||
if ctxt.has_local_resources(app) {
|
||||
let ident = util::local_resources_ident(ctxt, app);
|
||||
let ident = util::mark_internal_ident(&ident);
|
||||
let lt = if resources_tick {
|
||||
lt = Some(quote!('a));
|
||||
|
@ -76,12 +76,35 @@ pub fn codegen(
|
|||
|
||||
module_items.push(quote!(
|
||||
#[doc(inline)]
|
||||
pub use super::#ident as Resources;
|
||||
pub use super::#ident as LocalResources;
|
||||
));
|
||||
|
||||
fields.push(quote!(
|
||||
/// Resources this task has access to
|
||||
pub resources: #name::Resources<#lt>
|
||||
/// Local Resources this task has access to
|
||||
pub local: #name::LocalResources<#lt>
|
||||
));
|
||||
|
||||
values.push(quote!(local: #name::LocalResources::new()));
|
||||
}
|
||||
|
||||
if ctxt.has_shared_resources(app) {
|
||||
let ident = util::shared_resources_ident(ctxt, app);
|
||||
let ident = util::mark_internal_ident(&ident);
|
||||
let lt = if resources_tick {
|
||||
lt = Some(quote!('a));
|
||||
Some(quote!('a))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
module_items.push(quote!(
|
||||
#[doc(inline)]
|
||||
pub use super::#ident as SharedResources;
|
||||
));
|
||||
|
||||
fields.push(quote!(
|
||||
/// Shared Resources this task has access to
|
||||
pub shared: #name::SharedResources<#lt>
|
||||
));
|
||||
|
||||
let priority = if ctxt.is_init() {
|
||||
|
@ -89,38 +112,10 @@ pub fn codegen(
|
|||
} else {
|
||||
Some(quote!(priority))
|
||||
};
|
||||
values.push(quote!(resources: #name::Resources::new(#priority)));
|
||||
values.push(quote!(shared: #name::SharedResources::new(#priority)));
|
||||
}
|
||||
|
||||
if let Context::Init = ctxt {
|
||||
let late_fields = analysis
|
||||
.late_resources
|
||||
.iter()
|
||||
.flat_map(|resources| {
|
||||
resources.iter().map(|name| {
|
||||
let ty = &app.late_resources[name].ty;
|
||||
let cfgs = &app.late_resources[name].cfgs;
|
||||
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
pub #name: #ty
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let internal_late_ident = util::mark_internal_name("LateResources");
|
||||
items.push(quote!(
|
||||
/// Resources initialized at runtime
|
||||
#[allow(non_snake_case)]
|
||||
pub struct #internal_late_ident {
|
||||
#(#late_fields),*
|
||||
}
|
||||
));
|
||||
module_items.push(quote!(
|
||||
pub use super::#internal_late_ident as LateResources;
|
||||
));
|
||||
|
||||
let monotonic_types: Vec<_> = app
|
||||
.monotonics
|
||||
.iter()
|
||||
|
|
|
@ -9,24 +9,39 @@ use crate::{analyze::Analysis, codegen::util};
|
|||
pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
|
||||
let mut stmts = vec![];
|
||||
|
||||
// Initialize late resources
|
||||
if !analysis.late_resources.is_empty() {
|
||||
// BTreeSet wrapped in a vector
|
||||
for name in analysis.late_resources.first().unwrap() {
|
||||
let mangled_name = util::mark_internal_ident(&name);
|
||||
// If it's live
|
||||
let cfgs = app.late_resources[name].cfgs.clone();
|
||||
if analysis.locations.get(name).is_some() {
|
||||
stmts.push(quote!(
|
||||
// We include the cfgs
|
||||
#(#cfgs)*
|
||||
// Late resource is a RacyCell<MaybeUninit<T>>
|
||||
// - `get_mut_unchecked` to obtain `MaybeUninit<T>`
|
||||
// - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
|
||||
// - `write` the defined value for the late resource T
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr().write(late.#name);
|
||||
));
|
||||
}
|
||||
// Initialize shared resources
|
||||
for (name, res) in &app.shared_resources {
|
||||
let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(name));
|
||||
// If it's live
|
||||
let cfgs = res.cfgs.clone();
|
||||
if analysis.shared_resource_locations.get(name).is_some() {
|
||||
stmts.push(quote!(
|
||||
// We include the cfgs
|
||||
#(#cfgs)*
|
||||
// Resource is a RacyCell<MaybeUninit<T>>
|
||||
// - `get_mut_unchecked` to obtain `MaybeUninit<T>`
|
||||
// - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
|
||||
// - `write` the defined value for the late resource T
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr().write(shared_resources.#name);
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize local resources
|
||||
for (name, res) in &app.local_resources {
|
||||
let mangled_name = util::mark_internal_ident(&util::static_local_resource_ident(name));
|
||||
// If it's live
|
||||
let cfgs = res.cfgs.clone();
|
||||
if analysis.local_resource_locations.get(name).is_some() {
|
||||
stmts.push(quote!(
|
||||
// We include the cfgs
|
||||
#(#cfgs)*
|
||||
// Resource is a RacyCell<MaybeUninit<T>>
|
||||
// - `get_mut_unchecked` to obtain `MaybeUninit<T>`
|
||||
// - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
|
||||
// - `write` the defined value for the late resource T
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr().write(local_resources.#name);
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -126,7 +126,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
|
|||
}
|
||||
|
||||
// If there's no user `#[idle]` then optimize returning from interrupt handlers
|
||||
if app.idles.is_empty() {
|
||||
if app.idle.is_none() {
|
||||
// Set SLEEPONEXIT bit to enter sleep mode when returning from ISR
|
||||
stmts.push(quote!(core.SCB.scr.modify(|r| r | 1 << 1);));
|
||||
}
|
||||
|
|
|
@ -18,54 +18,31 @@ pub fn codegen(
|
|||
let mut mod_app = vec![];
|
||||
let mut mod_resources = vec![];
|
||||
|
||||
for (name, res) in app.shared_resources {
|
||||
for (name, res) in &app.shared_resources {
|
||||
let cfgs = &res.cfgs;
|
||||
let ty = &res.ty;
|
||||
let mangled_name = util::mark_internal_ident(&name);
|
||||
let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(&name));
|
||||
|
||||
{
|
||||
// late resources in `util::link_section_uninit`
|
||||
let section = if expr.is_none() {
|
||||
util::link_section_uninit(true)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// late resources in `util::link_section_uninit`
|
||||
let section = util::link_section_uninit(true);
|
||||
let attrs = &res.attrs;
|
||||
|
||||
// resource type and assigned value
|
||||
let (ty, expr) = if let Some(expr) = expr {
|
||||
// early resource
|
||||
(
|
||||
quote!(rtic::RacyCell<#ty>),
|
||||
quote!(rtic::RacyCell::new(#expr)),
|
||||
)
|
||||
} else {
|
||||
// late resource
|
||||
(
|
||||
quote!(rtic::RacyCell<core::mem::MaybeUninit<#ty>>),
|
||||
quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit())),
|
||||
)
|
||||
};
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
||||
mod_app.push(quote!(
|
||||
#[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());
|
||||
));
|
||||
|
||||
let attrs = &res.attrs;
|
||||
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
||||
mod_app.push(quote!(
|
||||
#[allow(non_upper_case_globals)]
|
||||
// #[doc = #doc]
|
||||
#[doc(hidden)]
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#section
|
||||
static #mangled_name: #ty = #expr;
|
||||
));
|
||||
}
|
||||
|
||||
let r_prop = &res.properties;
|
||||
// For future use
|
||||
// let doc = format!(" RTIC internal: {}:{}", file!(), line!());
|
||||
|
||||
if !r_prop.task_local && !r_prop.lock_free {
|
||||
if !res.properties.lock_free {
|
||||
mod_resources.push(quote!(
|
||||
// #[doc = #doc]
|
||||
#[doc(hidden)]
|
||||
|
@ -89,25 +66,10 @@ pub fn codegen(
|
|||
}
|
||||
));
|
||||
|
||||
let (ptr, _doc) = if expr.is_none() {
|
||||
// late resource
|
||||
(
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr()
|
||||
),
|
||||
"late",
|
||||
)
|
||||
} else {
|
||||
// early resource
|
||||
(
|
||||
quote!(
|
||||
#(#cfgs)*
|
||||
#mangled_name.get_mut_unchecked()
|
||||
),
|
||||
"early",
|
||||
)
|
||||
};
|
||||
let ptr = quote!(
|
||||
#(#cfgs)*
|
||||
#mangled_name.get_mut_unchecked().as_mut_ptr()
|
||||
);
|
||||
|
||||
let ceiling = match analysis.ownerships.get(name) {
|
||||
Some(Ownership::Owned { priority }) => *priority,
|
||||
|
@ -123,7 +85,7 @@ pub fn codegen(
|
|||
extra,
|
||||
cfgs,
|
||||
true,
|
||||
name,
|
||||
&name,
|
||||
quote!(#ty),
|
||||
ceiling,
|
||||
ptr,
|
||||
|
@ -134,7 +96,7 @@ pub fn codegen(
|
|||
let mod_resources = if mod_resources.is_empty() {
|
||||
quote!()
|
||||
} else {
|
||||
quote!(mod resources {
|
||||
quote!(mod shared_resources {
|
||||
use rtic::export::Priority;
|
||||
|
||||
#(#mod_resources)*
|
||||
|
|
|
@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
|
||||
let resources = match ctxt {
|
||||
Context::Init => unreachable!("Tried to generate shared resources struct for init"),
|
||||
Context::Idle => &app.idle.unwrap().args.shared_resources,
|
||||
Context::Idle => &app.idle.as_ref().unwrap().args.shared_resources,
|
||||
Context::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources,
|
||||
Context::SoftwareTask(name) => &app.software_tasks[name].args.shared_resources,
|
||||
};
|
||||
|
@ -48,12 +48,12 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
|
|||
|
||||
fields.push(quote!(
|
||||
#(#cfgs)*
|
||||
pub #name: resources::#name<'a>
|
||||
pub #name: shared_resources::#name<'a>
|
||||
));
|
||||
|
||||
values.push(quote!(
|
||||
#(#cfgs)*
|
||||
#name: resources::#name::new(priority)
|
||||
#name: shared_resources::#name::new(priority)
|
||||
|
||||
));
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
|
|||
use crate::{
|
||||
analyze::Analysis,
|
||||
check::Extra,
|
||||
codegen::{locals, module, resources_struct, util},
|
||||
codegen::{local_resources_struct, module, shared_resources_struct, util},
|
||||
};
|
||||
|
||||
pub fn codegen(
|
||||
|
@ -91,35 +91,38 @@ pub fn codegen(
|
|||
|
||||
// `${task}Resources`
|
||||
let mut needs_lt = false;
|
||||
if !task.args.resources.is_empty() {
|
||||
|
||||
// TODO: Fix locals
|
||||
// `${task}Locals`
|
||||
if !task.args.local_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app);
|
||||
local_resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app);
|
||||
|
||||
root.push(item);
|
||||
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
// `${task}Locals`
|
||||
let mut locals_pat = None;
|
||||
if !task.locals.is_empty() {
|
||||
let (struct_, pat) = locals::codegen(Context::SoftwareTask(name), &task.locals, app);
|
||||
if !task.args.shared_resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
shared_resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app);
|
||||
|
||||
locals_pat = Some(pat);
|
||||
root.push(struct_);
|
||||
root.push(item);
|
||||
|
||||
mod_app.push(constructor);
|
||||
}
|
||||
|
||||
|
||||
if !&task.is_extern {
|
||||
let context = &task.context;
|
||||
let attrs = &task.attrs;
|
||||
let cfgs = &task.cfgs;
|
||||
let stmts = &task.stmts;
|
||||
let locals_pat = locals_pat.iter();
|
||||
user_tasks.push(quote!(
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) {
|
||||
fn #name(#context: #name::Context #(,#inputs)*) {
|
||||
use rtic::Mutex as _;
|
||||
use rtic::mutex_prelude::*;
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ pub fn impl_mutex(
|
|||
ptr: TokenStream2,
|
||||
) -> TokenStream2 {
|
||||
let (path, priority) = if resources_prefix {
|
||||
(quote!(resources::#name), quote!(self.priority()))
|
||||
(quote!(shared_resources::#name), quote!(self.priority()))
|
||||
} else {
|
||||
(quote!(#name), quote!(self.priority))
|
||||
};
|
||||
|
@ -167,7 +167,7 @@ pub fn link_section_uninit(empty_expr: bool) -> Option<TokenStream2> {
|
|||
pub fn locals_ident(ctxt: Context, app: &App) -> Ident {
|
||||
let mut s = match ctxt {
|
||||
Context::Init => app.init.name.to_string(),
|
||||
Context::Idle => app.idle.unwrap().name.to_string(),
|
||||
Context::Idle => app.idle.as_ref().unwrap().name.to_string(),
|
||||
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
|
||||
};
|
||||
|
||||
|
@ -229,7 +229,7 @@ pub fn regroup_inputs(
|
|||
pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident {
|
||||
let mut s = match ctxt {
|
||||
Context::Init => app.init.name.to_string(),
|
||||
Context::Idle => app.idle.unwrap().name.to_string(),
|
||||
Context::Idle => app.idle.as_ref().unwrap().name.to_string(),
|
||||
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
|
||||
};
|
||||
|
||||
|
@ -242,7 +242,7 @@ pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident {
|
|||
pub fn local_resources_ident(ctxt: Context, app: &App) -> Ident {
|
||||
let mut s = match ctxt {
|
||||
Context::Init => app.init.name.to_string(),
|
||||
Context::Idle => app.idle.unwrap().name.to_string(),
|
||||
Context::Idle => app.idle.as_ref().unwrap().name.to_string(),
|
||||
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
|
||||
};
|
||||
|
||||
|
@ -288,6 +288,14 @@ pub fn monotonic_ident(name: &str) -> Ident {
|
|||
Ident::new(&format!("MONOTONIC_STORAGE_{}", name), Span::call_site())
|
||||
}
|
||||
|
||||
pub fn static_shared_resource_ident(name: &Ident) -> Ident {
|
||||
Ident::new(&format!("shared_{}", name.to_string()), Span::call_site())
|
||||
}
|
||||
|
||||
pub fn static_local_resource_ident(name: &Ident) -> Ident {
|
||||
Ident::new(&format!("local_{}", name.to_string()), Span::call_site())
|
||||
}
|
||||
|
||||
/// The name to get better RT flag errors
|
||||
pub fn rt_err_ident() -> Ident {
|
||||
Ident::new(
|
||||
|
|
Loading…
Reference in a new issue