Minimal app now compiles

This commit is contained in:
Emil Fresk 2021-07-06 22:47:48 +02:00
parent 3f85cb5caf
commit ef5307d83a
14 changed files with 289 additions and 353 deletions

View file

@ -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) = let (mod_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks) =
hardware_tasks::codegen(app, analysis, extra); hardware_tasks::codegen(app, analysis, extra);
@ -186,7 +187,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
#(#root)* #(#root)*
#mod_resources #mod_shared_resources
#mod_local_resources
#(#root_hardware_tasks)* #(#root_hardware_tasks)*
@ -195,7 +198,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
/// app module /// app module
#(#mod_app)* #(#mod_app)*
#(#mod_app_resources)* #(#mod_app_shared_resources)*
#(#mod_app_local_resources)*
#(#mod_app_hardware_tasks)* #(#mod_app_hardware_tasks)*

View file

@ -76,11 +76,12 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
let inputs = util::mark_internal_ident(&inputs); let inputs = util::mark_internal_ident(&inputs);
let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs); let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs);
let locals_new = if task.args.local_resources.is_empty() { // TODO Fix me
quote!() // let locals_new = if task.args.local_resources.is_empty() {
} else { // quote!()
quote!(#name::Locals::new(),) // } else {
}; // quote!(#name::Locals::new(),)
// };
quote!( quote!(
#(#cfgs)* #(#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); #fq.get_mut_unchecked().split().0.enqueue_unchecked(index);
let priority = &rtic::export::Priority::new(PRIORITY); let priority = &rtic::export::Priority::new(PRIORITY);
#name( #name(
#locals_new
#name::Context::new(priority) #name::Context::new(priority)
#(,#pats)* #(,#pats)*
) )

View file

@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
analyze::Analysis, analyze::Analysis,
check::Extra, 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) /// Generate support code for hardware tasks (`#[exception]`s and `#[interrupt]`s)
@ -29,11 +29,12 @@ pub fn codegen(
let mut user_tasks = vec![]; let mut user_tasks = vec![];
for (name, task) in &app.hardware_tasks { for (name, task) in &app.hardware_tasks {
let locals_new = if task.args.local_resources.is_empty() { // TODO: Fix locals
quote!() // let locals_new = if task.args.local_resources.is_empty() {
} else { // quote!()
quote!(#name::Locals::new(),) // } else {
}; // quote!(#name::Locals::new(),)
// };
let symbol = task.args.binds.clone(); let symbol = task.args.binds.clone();
let priority = task.args.priority; let priority = task.args.priority;
@ -50,7 +51,6 @@ pub fn codegen(
rtic::export::run(PRIORITY, || { rtic::export::run(PRIORITY, || {
#name( #name(
#locals_new
#name::Context::new(&rtic::export::Priority::new(PRIORITY)) #name::Context::new(&rtic::export::Priority::new(PRIORITY))
) )
}); });
@ -59,10 +59,21 @@ pub fn codegen(
let mut needs_lt = false; 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` // `${task}Resources`
if !task.args.shared_resources.is_empty() { if !task.args.shared_resources.is_empty() {
let (item, constructor) = 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); root.push(item);
@ -77,24 +88,25 @@ pub fn codegen(
extra, extra,
)); ));
// `${task}Locals` // TODO: Fix locals
let mut locals_pat = None; // // `${task}Locals`
if !task.locals.is_empty() { // let mut locals_pat = None;
let (struct_, pat) = locals::codegen(Context::HardwareTask(name), &task.locals, app); // if !task.locals.is_empty() {
// let (struct_, pat) =
// local_resources_struct::codegen(Context::HardwareTask(name), &task.locals, app);
root.push(struct_); // root.push(struct_);
locals_pat = Some(pat); // locals_pat = Some(pat);
} // }
if !&task.is_extern { if !task.is_extern {
let attrs = &task.attrs; let attrs = &task.attrs;
let context = &task.context; let context = &task.context;
let stmts = &task.stmts; let stmts = &task.stmts;
let locals_pat = locals_pat.iter();
user_tasks.push(quote!( user_tasks.push(quote!(
#(#attrs)* #(#attrs)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn #name(#(#locals_pat,)* #context: #name::Context) { fn #name(#context: #name::Context) {
use rtic::Mutex as _; use rtic::Mutex as _;
use rtic::mutex_prelude::*; use rtic::mutex_prelude::*;

View file

@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
analyze::Analysis, analyze::Analysis,
check::Extra, check::Extra,
codegen::{locals, module, resources_struct}, codegen::{local_resources_struct, module, shared_resources_struct},
}; };
/// Generates support code for `#[idle]` functions /// Generates support code for `#[idle]` functions
@ -26,30 +26,28 @@ pub fn codegen(
// call_idle // call_idle
TokenStream2, TokenStream2,
) { ) {
if !app.idles.is_empty() { if let Some(idle) = &app.idle {
let idle = &app.idles.first().unwrap();
let mut needs_lt = false; let mut needs_lt = false;
let mut mod_app = None; let mut mod_app = None;
let mut root_idle = vec![]; let mut root_idle = vec![];
let mut locals_pat = None;
let mut locals_new = None;
let name = &idle.name; let name = &idle.name;
if !idle.args.resources.is_empty() { if !idle.args.shared_resources.is_empty() {
let (item, constructor) = resources_struct::codegen(Context::Idle, &mut needs_lt, app); let (item, constructor) = shared_resources_struct::codegen(Context::Idle, &mut needs_lt, app);
root_idle.push(item); root_idle.push(item);
mod_app = Some(constructor); mod_app = Some(constructor);
} }
if !idle.locals.is_empty() { // TODO: Fix locals
let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app); // if !idle.locals.is_empty() {
// let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);
locals_new = Some(quote!(#name::Locals::new())); // locals_new = Some(quote!(#name::Locals::new()));
locals_pat = Some(pat); // locals_pat = Some(pat);
root_idle.push(locals); // root_idle.push(locals);
} // }
root_idle.push(module::codegen( root_idle.push(module::codegen(
Context::Idle, Context::Idle,
@ -62,11 +60,10 @@ pub fn codegen(
let attrs = &idle.attrs; let attrs = &idle.attrs;
let context = &idle.context; let context = &idle.context;
let stmts = &idle.stmts; let stmts = &idle.stmts;
let locals_pat = locals_pat.iter();
let user_idle = Some(quote!( let user_idle = Some(quote!(
#(#attrs)* #(#attrs)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn #name(#(#locals_pat,)* #context: #name::Context) -> ! { fn #name(#context: #name::Context) -> ! {
use rtic::Mutex as _; use rtic::Mutex as _;
use rtic::mutex_prelude::*; use rtic::mutex_prelude::*;
@ -74,9 +71,7 @@ pub fn codegen(
} }
)); ));
let locals_new = locals_new.iter();
let call_idle = quote!(#name( let call_idle = quote!(#name(
#(#locals_new,)*
#name::Context::new(&rtic::export::Priority::new(0)) #name::Context::new(&rtic::export::Priority::new(0))
)); ));

View file

@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
analyze::Analysis, analyze::Analysis,
check::Extra, check::Extra,
codegen::{locals, module, resources_struct}, codegen::{local_resources_struct, module},
}; };
type CodegenResult = ( type CodegenResult = (
@ -18,57 +18,88 @@ type CodegenResult = (
// - the `${init}` module, which contains types like `${init}::Context` // - the `${init}` module, which contains types like `${init}::Context`
Vec<TokenStream2>, Vec<TokenStream2>,
// user_init -- the `#[init]` function written by the user // user_init -- the `#[init]` function written by the user
Option<TokenStream2>, TokenStream2,
// call_init -- the call to the user `#[init]` if there's one // call_init -- the call to the user `#[init]`
Option<TokenStream2>, TokenStream2,
); );
/// Generates support code for `#[init]` functions /// Generates support code for `#[init]` functions
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult {
if !app.inits.is_empty() { let init = &app.init;
let init = &app.inits.first().unwrap();
let mut needs_lt = false; let mut needs_lt = false;
let name = &init.name; let name = &init.name;
let mut root_init = vec![]; let mut root_init = vec![];
let mut locals_pat = None; // TODO: Fix locals
let mut locals_new = None; // let mut locals_pat = None;
if !init.locals.is_empty() { // let mut locals_new = None;
let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); // if !init.locals.is_empty() {
// let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app);
locals_new = Some(quote!(#name::Locals::new())); // locals_new = Some(quote!(#name::Locals::new()));
locals_pat = Some(pat); // locals_pat = Some(pat);
root_init.push(struct_); // root_init.push(struct_);
} // }
let context = &init.context; let context = &init.context;
let attrs = &init.attrs; let attrs = &init.attrs;
let stmts = &init.stmts; let stmts = &init.stmts;
let locals_pat = locals_pat.iter(); let shared = &init.user_shared_struct;
let local = &init.user_local_struct;
let user_init_return = quote! {#name::LateResources, #name::Monotonics}; 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 user_init = Some(quote!( struct #local {
#(#local_resources)*
}
});
// let locals_pat = locals_pat.iter();
let user_init_return = quote! {#shared, #local, #name::Monotonics};
let user_init = quote!(
#(#attrs)* #(#attrs)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
fn #name(#(#locals_pat,)* #context: #name::Context) -> (#user_init_return) { fn #name(#context: #name::Context) -> (#user_init_return) {
#(#stmts)* #(#stmts)*
} }
)); );
let mut mod_app = None; 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); // let locals_new = locals_new.iter();
mod_app = Some(constructor); let call_init = quote! {
} let (shared_resources, local_resources, mut monotonics) = #name(#name::Context::new(core.into()));
};
let locals_new = locals_new.iter();
let call_init = Some(
quote!(let (late, mut monotonics) = #name(#(#locals_new,)* #name::Context::new(core.into()));),
);
root_init.push(module::codegen( root_init.push(module::codegen(
Context::Init, Context::Init,
@ -79,7 +110,4 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult {
)); ));
(mod_app, root_init, user_init, call_init) (mod_app, root_init, user_init, call_init)
} else {
(None, vec![], None, None)
}
} }

View file

@ -1,10 +1,12 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::quote;
use rtic_syntax::{analyze::Ownership, ast::App}; use rtic_syntax::ast::App;
use crate::{analyze::Analysis, check::Extra, codegen::util}; use crate::{analyze::Analysis, check::Extra, codegen::util};
/// Generates `local` variables and local resource proxies /// Generates `local` variables and local resource proxies
///
/// I.e. the `static` variables and theirs proxies.
pub fn codegen( pub fn codegen(
app: &App, app: &App,
analysis: &Analysis, analysis: &Analysis,
@ -16,38 +18,21 @@ pub fn codegen(
TokenStream2, TokenStream2,
) { ) {
let mut mod_app = vec![]; 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 expr = &res.expr; // TODO: Extract from tasks???...
let cfgs = &res.cfgs; let cfgs = &res.cfgs;
let ty = &res.ty; 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));
{ let ty = quote!(rtic::RacyCell<core::mem::MaybeUninit<#ty>>);
// late resources in `util::link_section_uninit` let expr = quote!(rtic::RacyCell::new(core::mem::MaybeUninit::uninit()));
let section = if expr.is_none() {
util::link_section_uninit(true)
} else {
None
};
// 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; let attrs = &res.attrs;
// late resources in `util::link_section_uninit`
let section = util::link_section_uninit(true);
// For future use // For future use
// let doc = format!(" RTIC internal: {}:{}", file!(), line!()); // let doc = format!(" RTIC internal: {}:{}", file!(), line!());
@ -62,85 +47,13 @@ pub fn codegen(
)); ));
} }
let r_prop = &res.properties; // let mod_resources = if mod_resources.is_empty() {
// For future use // quote!()
// let doc = format!(" RTIC internal: {}:{}", file!(), line!()); // } else {
// quote!(mod local_resources {
// #(#mod_resources)*
// })
// };
if !r_prop.task_local && !r_prop.lock_free { (mod_app, TokenStream2::new())
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,
));
}
}
let mod_resources = if mod_resources.is_empty() {
quote!()
} else {
quote!(mod resources {
use rtic::export::Priority;
#(#mod_resources)*
})
};
(mod_app, mod_resources)
} }

View file

@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
let resources = match ctxt { let resources = match ctxt {
Context::Init => &app.init.args.local_resources, 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::HardwareTask(name) => &app.hardware_tasks[name].args.local_resources,
Context::SoftwareTask(name) => &app.software_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 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!( fields.push(quote!(
#(#cfgs)* #(#cfgs)*

View file

@ -56,16 +56,16 @@ pub fn codegen(
Context::SoftwareTask(_) => {} Context::SoftwareTask(_) => {}
} }
if ctxt.has_locals(app) { // if ctxt.has_locals(app) {
let ident = util::locals_ident(ctxt, app); // let ident = util::locals_ident(ctxt, app);
module_items.push(quote!( // module_items.push(quote!(
#[doc(inline)] // #[doc(inline)]
pub use super::#ident as Locals; // pub use super::#ident as Locals;
)); // ));
} // }
if ctxt.has_resources(app) { if ctxt.has_local_resources(app) {
let ident = util::resources_ident(ctxt, app); let ident = util::local_resources_ident(ctxt, app);
let ident = util::mark_internal_ident(&ident); let ident = util::mark_internal_ident(&ident);
let lt = if resources_tick { let lt = if resources_tick {
lt = Some(quote!('a)); lt = Some(quote!('a));
@ -76,12 +76,35 @@ pub fn codegen(
module_items.push(quote!( module_items.push(quote!(
#[doc(inline)] #[doc(inline)]
pub use super::#ident as Resources; pub use super::#ident as LocalResources;
)); ));
fields.push(quote!( fields.push(quote!(
/// Resources this task has access to /// Local Resources this task has access to
pub resources: #name::Resources<#lt> 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() { let priority = if ctxt.is_init() {
@ -89,38 +112,10 @@ pub fn codegen(
} else { } else {
Some(quote!(priority)) Some(quote!(priority))
}; };
values.push(quote!(resources: #name::Resources::new(#priority))); values.push(quote!(shared: #name::SharedResources::new(#priority)));
} }
if let Context::Init = ctxt { 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 let monotonic_types: Vec<_> = app
.monotonics .monotonics
.iter() .iter()

View file

@ -9,25 +9,40 @@ use crate::{analyze::Analysis, codegen::util};
pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> { pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
let mut stmts = vec![]; let mut stmts = vec![];
// Initialize late resources // Initialize shared resources
if !analysis.late_resources.is_empty() { for (name, res) in &app.shared_resources {
// BTreeSet wrapped in a vector let mangled_name = util::mark_internal_ident(&util::static_shared_resource_ident(name));
for name in analysis.late_resources.first().unwrap() {
let mangled_name = util::mark_internal_ident(&name);
// If it's live // If it's live
let cfgs = app.late_resources[name].cfgs.clone(); let cfgs = res.cfgs.clone();
if analysis.locations.get(name).is_some() { if analysis.shared_resource_locations.get(name).is_some() {
stmts.push(quote!( stmts.push(quote!(
// We include the cfgs // We include the cfgs
#(#cfgs)* #(#cfgs)*
// Late resource is a RacyCell<MaybeUninit<T>> // Resource is a RacyCell<MaybeUninit<T>>
// - `get_mut_unchecked` to obtain `MaybeUninit<T>` // - `get_mut_unchecked` to obtain `MaybeUninit<T>`
// - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>` // - `as_mut_ptr` to obtain a raw pointer to `MaybeUninit<T>`
// - `write` the defined value for the late resource T // - `write` the defined value for the late resource T
#mangled_name.get_mut_unchecked().as_mut_ptr().write(late.#name); #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);
));
}
} }
for (i, (monotonic, _)) in app.monotonics.iter().enumerate() { for (i, (monotonic, _)) in app.monotonics.iter().enumerate() {

View file

@ -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 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 // Set SLEEPONEXIT bit to enter sleep mode when returning from ISR
stmts.push(quote!(core.SCB.scr.modify(|r| r | 1 << 1);)); stmts.push(quote!(core.SCB.scr.modify(|r| r | 1 << 1);));
} }

View file

@ -18,34 +18,13 @@ pub fn codegen(
let mut mod_app = vec![]; let mut mod_app = vec![];
let mut mod_resources = 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 cfgs = &res.cfgs;
let ty = &res.ty; 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` // late resources in `util::link_section_uninit`
let section = if expr.is_none() { let section = util::link_section_uninit(true);
util::link_section_uninit(true)
} else {
None
};
// 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; let attrs = &res.attrs;
// For future use // For future use
@ -57,15 +36,13 @@ pub fn codegen(
#(#attrs)* #(#attrs)*
#(#cfgs)* #(#cfgs)*
#section #section
static #mangled_name: #ty = #expr; static #mangled_name: rtic::RacyCell<core::mem::MaybeUninit<#ty>> = rtic::RacyCell::new(core::mem::MaybeUninit::uninit());
)); ));
}
let r_prop = &res.properties;
// For future use // For future use
// let doc = format!(" RTIC internal: {}:{}", file!(), line!()); // 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!( mod_resources.push(quote!(
// #[doc = #doc] // #[doc = #doc]
#[doc(hidden)] #[doc(hidden)]
@ -89,25 +66,10 @@ pub fn codegen(
} }
)); ));
let (ptr, _doc) = if expr.is_none() { let ptr = quote!(
// late resource
(
quote!(
#(#cfgs)* #(#cfgs)*
#mangled_name.get_mut_unchecked().as_mut_ptr() #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) { let ceiling = match analysis.ownerships.get(name) {
Some(Ownership::Owned { priority }) => *priority, Some(Ownership::Owned { priority }) => *priority,
@ -123,7 +85,7 @@ pub fn codegen(
extra, extra,
cfgs, cfgs,
true, true,
name, &name,
quote!(#ty), quote!(#ty),
ceiling, ceiling,
ptr, ptr,
@ -134,7 +96,7 @@ pub fn codegen(
let mod_resources = if mod_resources.is_empty() { let mod_resources = if mod_resources.is_empty() {
quote!() quote!()
} else { } else {
quote!(mod resources { quote!(mod shared_resources {
use rtic::export::Priority; use rtic::export::Priority;
#(#mod_resources)* #(#mod_resources)*

View file

@ -10,7 +10,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
let resources = match ctxt { let resources = match ctxt {
Context::Init => unreachable!("Tried to generate shared resources struct for init"), 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::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources,
Context::SoftwareTask(name) => &app.software_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!( fields.push(quote!(
#(#cfgs)* #(#cfgs)*
pub #name: resources::#name<'a> pub #name: shared_resources::#name<'a>
)); ));
values.push(quote!( values.push(quote!(
#(#cfgs)* #(#cfgs)*
#name: resources::#name::new(priority) #name: shared_resources::#name::new(priority)
)); ));

View file

@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
analyze::Analysis, analyze::Analysis,
check::Extra, check::Extra,
codegen::{locals, module, resources_struct, util}, codegen::{local_resources_struct, module, shared_resources_struct, util},
}; };
pub fn codegen( pub fn codegen(
@ -91,35 +91,38 @@ pub fn codegen(
// `${task}Resources` // `${task}Resources`
let mut needs_lt = false; 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) = 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); root.push(item);
mod_app.push(constructor); mod_app.push(constructor);
} }
// `${task}Locals` if !task.args.shared_resources.is_empty() {
let mut locals_pat = None; let (item, constructor) =
if !task.locals.is_empty() { shared_resources_struct::codegen(Context::SoftwareTask(name), &mut needs_lt, app);
let (struct_, pat) = locals::codegen(Context::SoftwareTask(name), &task.locals, app);
locals_pat = Some(pat); root.push(item);
root.push(struct_);
mod_app.push(constructor);
} }
if !&task.is_extern { if !&task.is_extern {
let context = &task.context; let context = &task.context;
let attrs = &task.attrs; let attrs = &task.attrs;
let cfgs = &task.cfgs; let cfgs = &task.cfgs;
let stmts = &task.stmts; let stmts = &task.stmts;
let locals_pat = locals_pat.iter();
user_tasks.push(quote!( user_tasks.push(quote!(
#(#attrs)* #(#attrs)*
#(#cfgs)* #(#cfgs)*
#[allow(non_snake_case)] #[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 as _;
use rtic::mutex_prelude::*; use rtic::mutex_prelude::*;

View file

@ -41,7 +41,7 @@ pub fn impl_mutex(
ptr: TokenStream2, ptr: TokenStream2,
) -> TokenStream2 { ) -> TokenStream2 {
let (path, priority) = if resources_prefix { let (path, priority) = if resources_prefix {
(quote!(resources::#name), quote!(self.priority())) (quote!(shared_resources::#name), quote!(self.priority()))
} else { } else {
(quote!(#name), quote!(self.priority)) (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 { pub fn locals_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt { let mut s = match ctxt {
Context::Init => app.init.name.to_string(), 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(), 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 { pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt { let mut s = match ctxt {
Context::Init => app.init.name.to_string(), 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(), 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 { pub fn local_resources_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt { let mut s = match ctxt {
Context::Init => app.init.name.to_string(), 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(), 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()) 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 /// The name to get better RT flag errors
pub fn rt_err_ident() -> Ident { pub fn rt_err_ident() -> Ident {
Ident::new( Ident::new(