Started work

This commit is contained in:
Emil Fresk 2021-07-05 21:40:01 +02:00
parent 13dc3992e6
commit 3f85cb5caf
10 changed files with 283 additions and 146 deletions

View file

@ -22,4 +22,5 @@ proc-macro2 = "1"
proc-macro-error = "1"
quote = "1"
syn = "1"
rtic-syntax = "0.5.0-alpha.3"
# rtic-syntax = "0.5.0-alpha.3"
rtic-syntax = { path = "../../rtic-syntax" }

View file

@ -9,12 +9,13 @@ mod dispatchers;
mod hardware_tasks;
mod idle;
mod init;
mod locals;
mod local_resources;
mod shared_resources;
mod local_resources_struct;
mod shared_resources_struct;
mod module;
mod post_init;
mod pre_init;
mod resources;
mod resources_struct;
mod software_tasks;
mod timer_queue;
mod util;

View file

@ -76,7 +76,7 @@ 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.locals.is_empty() {
let locals_new = if task.args.local_resources.is_empty() {
quote!()
} else {
quote!(#name::Locals::new(),)

View file

@ -29,7 +29,7 @@ pub fn codegen(
let mut user_tasks = vec![];
for (name, task) in &app.hardware_tasks {
let locals_new = if task.locals.is_empty() {
let locals_new = if task.args.local_resources.is_empty() {
quote!()
} else {
quote!(#name::Locals::new(),)
@ -60,7 +60,7 @@ pub fn codegen(
let mut needs_lt = false;
// `${task}Resources`
if !task.args.resources.is_empty() {
if !task.args.shared_resources.is_empty() {
let (item, constructor) =
resources_struct::codegen(Context::HardwareTask(name), &mut needs_lt, app);

View file

@ -4,14 +4,7 @@ use rtic_syntax::{analyze::Ownership, ast::App};
use crate::{analyze::Analysis, check::Extra, codegen::util};
/// 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`.
/// Generates `local` variables and local resource proxies
pub fn codegen(
app: &App,
analysis: &Analysis,
@ -25,7 +18,8 @@ pub fn codegen(
let mut mod_app = vec![];
let mut mod_resources = vec![];
for (name, res, expr, _) in app.resources(analysis) {
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);

View file

@ -0,0 +1,88 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::{ast::App, Context};
use crate::codegen::util;
/// Generates local resources structs
pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) {
let mut lt = None;
let resources = match ctxt {
Context::Init => &app.init.args.local_resources,
Context::Idle => &app.idle.unwrap().args.local_resources,
Context::HardwareTask(name) => &app.hardware_tasks[name].args.local_resources,
Context::SoftwareTask(name) => &app.software_tasks[name].args.local_resources,
};
let mut fields = vec![];
let mut values = vec![];
let mut has_cfgs = false;
for (name, task_local) in resources {
let res = app.local_resources.get(name).expect("UNREACHABLE");
let cfgs = &res.cfgs;
has_cfgs |= !cfgs.is_empty();
let lt = if ctxt.runs_once() {
quote!('static)
} else {
lt = Some(quote!('a));
quote!('a)
};
let ty = &res.ty;
let mangled_name = util::mark_internal_ident(&name);
fields.push(quote!(
#(#cfgs)*
pub #name: &#lt mut #ty
));
let expr = quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr());
values.push(quote!(
#(#cfgs)*
#name: #expr
));
}
if lt.is_some() {
*needs_lt = true;
// The struct could end up empty due to `cfg`s leading to an error due to `'a` being unused
if has_cfgs {
fields.push(quote!(
#[doc(hidden)]
pub __marker__: core::marker::PhantomData<&'a ()>
));
values.push(quote!(__marker__: core::marker::PhantomData))
}
}
let doc = format!("Local resources `{}` has access to", ctxt.ident(app));
let ident = util::local_resources_ident(ctxt, app);
let ident = util::mark_internal_ident(&ident);
let item = quote!(
#[allow(non_snake_case)]
#[doc = #doc]
pub struct #ident<#lt> {
#(#fields,)*
}
);
let constructor = quote!(
impl<#lt> #ident<#lt> {
#[inline(always)]
pub unsafe fn new() -> Self {
#ident {
#(#values,)*
}
}
}
);
(item, constructor)
}

View file

@ -1,95 +0,0 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::{
ast::{App, Local},
Context, Map,
};
use crate::codegen::util;
pub fn codegen(
ctxt: Context,
locals: &Map<Local>,
app: &App,
) -> (
// locals
TokenStream2,
// pat
TokenStream2,
) {
assert!(!locals.is_empty());
let runs_once = ctxt.runs_once();
let ident = util::locals_ident(ctxt, app);
let mut lt = None;
let mut fields = vec![];
let mut items = vec![];
let mut names = vec![];
let mut values = vec![];
let mut pats = vec![];
let mut has_cfgs = false;
for (name, local) in locals {
let lt = if runs_once {
quote!('static)
} else {
lt = Some(quote!('a));
quote!('a)
};
let cfgs = &local.cfgs;
has_cfgs |= !cfgs.is_empty();
let expr = &local.expr;
let ty = &local.ty;
fields.push(quote!(
#(#cfgs)*
#name: &#lt mut #ty
));
items.push(quote!(
#(#cfgs)*
#[doc(hidden)]
static #name: rtic::RacyCell<#ty> = rtic::RacyCell::new(#expr)
));
values.push(quote!(
#(#cfgs)*
#name: #name.get_mut_unchecked()
));
names.push(name);
pats.push(quote!(
#(#cfgs)*
#name
));
}
if lt.is_some() && has_cfgs {
fields.push(quote!(__marker__: core::marker::PhantomData<&'a ()>));
values.push(quote!(__marker__: core::marker::PhantomData));
}
let locals = quote!(
#[allow(non_snake_case)]
#[doc(hidden)]
pub struct #ident<#lt> {
#(#fields),*
}
impl<#lt> #ident<#lt> {
#[inline(always)]
unsafe fn new() -> Self {
#(#items;)*
#ident {
#(#values),*
}
}
}
);
let ident = ctxt.ident(app);
(
locals,
quote!(#ident::Locals { #(#pats,)* .. }: #ident::Locals),
)
}

View file

@ -0,0 +1,145 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use rtic_syntax::{analyze::Ownership, ast::App};
use crate::{analyze::Analysis, check::Extra, codegen::util};
/// Generates `static` variables and shared resource proxies
pub fn codegen(
app: &App,
analysis: &Analysis,
extra: &Extra,
) -> (
// mod_app -- the `static` variables behind the proxies
Vec<TokenStream2>,
// mod_resources -- the `resources` module
TokenStream2,
) {
let mut mod_app = vec![];
let mut mod_resources = vec![];
for (name, res) in app.shared_resources {
let cfgs = &res.cfgs;
let ty = &res.ty;
let mangled_name = util::mark_internal_ident(&name);
{
// late resources in `util::link_section_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;
// 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,
));
}
}
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

@ -4,14 +4,15 @@ use rtic_syntax::{ast::App, Context};
use crate::codegen::util;
/// Generate shared resources structs
pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2, TokenStream2) {
let mut lt = None;
let resources = match ctxt {
Context::Init => &app.inits.first().unwrap().args.resources,
Context::Idle => &app.idles.first().unwrap().args.resources,
Context::HardwareTask(name) => &app.hardware_tasks[name].args.resources,
Context::SoftwareTask(name) => &app.software_tasks[name].args.resources,
Context::Init => unreachable!("Tried to generate shared resources struct for init"),
Context::Idle => &app.idle.unwrap().args.shared_resources,
Context::HardwareTask(name) => &app.hardware_tasks[name].args.shared_resources,
Context::SoftwareTask(name) => &app.software_tasks[name].args.shared_resources,
};
let mut fields = vec![];
@ -19,7 +20,7 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
let mut has_cfgs = false;
for (name, access) in resources {
let (res, expr) = app.resource(name).expect("UNREACHABLE");
let res = app.shared_resources.get(name).expect("UNREACHABLE");
let cfgs = &res.cfgs;
has_cfgs |= !cfgs.is_empty();
@ -33,10 +34,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 ownership = &analysis.ownerships[name];
let r_prop = &res.properties;
if !r_prop.task_local && !r_prop.lock_free {
if !res.properties.lock_free {
if access.is_shared() {
lt = Some(quote!('a));
@ -76,8 +74,6 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
));
}
let is_late = expr.is_none();
if is_late {
let expr = if access.is_exclusive() {
quote!(&mut *#mangled_name.get_mut_unchecked().as_mut_ptr())
} else {
@ -88,12 +84,6 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
#(#cfgs)*
#name: #expr
));
} else {
values.push(quote!(
#(#cfgs)*
#name: #mangled_name.get_mut_unchecked()
));
}
}
if lt.is_some() {
@ -110,8 +100,8 @@ pub fn codegen(ctxt: Context, needs_lt: &mut bool, app: &App) -> (TokenStream2,
}
}
let doc = format!("Resources `{}` has access to", ctxt.ident(app));
let ident = util::resources_ident(ctxt, app);
let doc = format!("Shared resources `{}` has access to", ctxt.ident(app));
let ident = util::shared_resources_ident(ctxt, app);
let ident = util::mark_internal_ident(&ident);
let item = quote!(
#[allow(non_snake_case)]

View file

@ -166,8 +166,8 @@ pub fn link_section_uninit(empty_expr: bool) -> Option<TokenStream2> {
/// Generates a pre-reexport identifier for the "locals" struct
pub fn locals_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt {
Context::Init => app.inits.first().unwrap().name.to_string(),
Context::Idle => app.idles.first().unwrap().name.to_string(),
Context::Init => app.init.name.to_string(),
Context::Idle => app.idle.unwrap().name.to_string(),
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
@ -225,15 +225,28 @@ pub fn regroup_inputs(
}
}
/// Generates a pre-reexport identifier for the "resources" struct
pub fn resources_ident(ctxt: Context, app: &App) -> Ident {
/// Generates a pre-reexport identifier for the "shared resources" struct
pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt {
Context::Init => app.inits.first().unwrap().name.to_string(),
Context::Idle => app.idles.first().unwrap().name.to_string(),
Context::Init => app.init.name.to_string(),
Context::Idle => app.idle.unwrap().name.to_string(),
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
s.push_str("Resources");
s.push_str("SharedResources");
Ident::new(&s, Span::call_site())
}
/// Generates a pre-reexport identifier for the "local resources" struct
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::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
s.push_str("LocalResources");
Ident::new(&s, Span::call_site())
}