Skip to content

Commit

Permalink
feat(macros): add additional constants to processes and channels
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenautumns committed Oct 16, 2024
1 parent af33993 commit 7e597e5
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 47 deletions.
74 changes: 56 additions & 18 deletions macros/src/generate/channel.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::time::Duration;

use convert_case::{Case, Casing};
use quote::format_ident;
use syn::{
parse_quote, Expr, ExprCall, Ident, ItemConst, ItemImpl, ItemMod, ItemStatic, LitByteStr,
LitStr, Path,
parse_quote, ExprCall, Ident, ItemConst, ItemImpl, ItemMod, ItemStatic, LitByteStr, LitStr,
Path,
};

use crate::parse::channel::Channel;
Expand Down Expand Up @@ -50,24 +48,33 @@ impl Channel {
Channel::SamplingOut(_, _) => {
parse_quote!(create_const_sampling_port_source(NAME))
}
Channel::SamplingIn(_, ch) => {
let dur: Duration = ch.refresh_period.into();
let dur = dur.as_nanos() as u64;
let dur: Expr = parse_quote!(core::time::Duration::from_nanos(#dur));
parse_quote!(create_const_sampling_port_destination(NAME, #dur))
Channel::SamplingIn(_, _) => {
parse_quote!(create_const_sampling_port_destination(NAME, REFRESH_PERIOD))
}
Channel::QueuingOut(_, ch) => {
let disc: Path = ch.discipline.into();
parse_quote!(create_const_queuing_port_sender(NAME, #disc))
Channel::QueuingOut(_, _) => {
parse_quote!(create_const_queuing_port_sender(NAME, DISCIPLINE))
}
Channel::QueuingIn(_, ch) => {
let disc: Path = ch.discipline.into();
parse_quote!(create_const_queuing_port_receiver(NAME, #disc))
Channel::QueuingIn(_, _) => {
parse_quote!(create_const_queuing_port_receiver(NAME, DISCIPLINE))
}
}
}

pub fn gen_static_name(&self) -> syn::Result<ItemConst> {
pub fn gen_consts(&self) -> syn::Result<Vec<ItemConst>> {
let mut consts = vec![self.gen_const_name()?, self.gen_const_msg_size()];
if let Some(msg_count) = self.gen_const_msg_count() {
consts.push(msg_count);
}
if let Some(discipline) = self.gen_const_discipline() {
consts.push(discipline);
}
if let Some(refresh_period) = self.gen_const_refresh_period() {
consts.push(refresh_period);
}
Ok(consts)
}

pub fn gen_const_name(&self) -> syn::Result<ItemConst> {
const LEN: usize = 32;
let name = self.name().to_string();
let len = name.bytes().len();
Expand All @@ -87,9 +94,40 @@ impl Channel {
})
}

pub fn gen_const_msg_size(&self) -> ItemConst {
let msg_size = self.msg_size() as u32;
parse_quote! {
pub(super) const MSG_SIZE: MessageSize = #msg_size;
}
}

pub fn gen_const_msg_count(&self) -> Option<ItemConst> {
let msg_count = self.msg_count()?;
Some(parse_quote! {
pub(super) const NB_MSGS: MessageRange = #msg_count;
})
}

pub fn gen_const_discipline(&self) -> Option<ItemConst> {
let discipline: Path = self.discipline()?.into();
Some(parse_quote! {
pub(super) const DISCIPLINE: QueuingDiscipline = #discipline;
})
}

pub fn gen_const_refresh_period(&self) -> Option<ItemConst> {
let refresh_period = self.refresh_period()?;
let secs = refresh_period.as_secs();
let nanos = refresh_period.subsec_nanos();
Some(parse_quote! {
pub(super) const REFRESH_PERIOD: core::time::Duration =
core::time::Duration::new( #secs , #nanos );
})
}

pub fn gen_channel_mod(&self) -> syn::Result<ItemMod> {
let name = self.gen_snake_ident();
let static_name = self.gen_static_name()?;
let consts = self.gen_consts()?;
let static_value = self.gen_static_value();
let create_fn = self.gen_create_fn();
Ok(parse_quote! {
Expand All @@ -98,7 +136,7 @@ impl Channel {
use a653rs::prelude::*;

#create_fn
#static_name
#(#consts)*
#static_value
}
})
Expand Down
70 changes: 55 additions & 15 deletions macros/src/generate/process.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use proc_macro2::TokenStream;
use quote::format_ident;
use syn::{
parse_quote, ExprPath, ItemConst, ItemFn, ItemImpl, ItemMod, ItemStatic, LitByteStr, LitStr,
parse_quote, Expr, ItemConst, ItemFn, ItemImpl, ItemMod, ItemStatic, LitByteStr, LitStr, Path,
};

use super::context::Context;
Expand All @@ -24,22 +23,17 @@ impl Process {

pub fn gen_create_fn(&self) -> ItemImpl {
let create_ident = format_ident!("create_{}", self.ident);
let deadline: ExprPath = self.deadline.into();
let priority = self.base_priority;
let stack_size = self.stack_size.as_u64() as u32;
let time_capacity: TokenStream = self.time_capacity.clone().into();
let period: TokenStream = self.period.clone().into();
parse_quote! {
impl<'a> super:: StartContext<'a, Hypervisor> {
pub fn #create_ident<'b>(&'b mut self) -> Result<&'b Process::<Hypervisor>, Error>{
use core::str::FromStr;
let attr = ProcessAttribute {
period: #period,
time_capacity: #time_capacity,
period: PERIOD,
time_capacity: TIME_CAPACITY,
entry_point: wrapper,
stack_size: #stack_size,
base_priority: #priority,
deadline: #deadline,
stack_size: STACK_SIZE,
base_priority: BASE_PRIORITY,
deadline: DEADLINE,
name: NAME,
};
let process = self.ctx.create_process(attr)?;
Expand All @@ -59,7 +53,18 @@ impl Process {
}
}

pub fn gen_static_name(&self) -> syn::Result<ItemConst> {
pub fn gen_consts(&self) -> syn::Result<Vec<ItemConst>> {
Ok(vec![
self.gen_const_name()?,
self.gen_const_time_capacity(),
self.gen_const_period(),
self.gen_const_stack_size(),
self.gen_const_base_priority(),
self.gen_const_deadline(),
])
}

pub fn gen_const_name(&self) -> syn::Result<ItemConst> {
const LEN: usize = 32;
let name = self.name.to_string();
let len = name.bytes().len();
Expand All @@ -79,10 +84,45 @@ impl Process {
})
}

pub fn gen_const_time_capacity(&self) -> ItemConst {
let time_capacity: Expr = self.time_capacity.into();
parse_quote! {
pub(super) const TIME_CAPACITY: SystemTime = #time_capacity ;
}
}

pub fn gen_const_period(&self) -> ItemConst {
let period: Expr = self.period.into();
parse_quote! {
pub(super) const PERIOD: SystemTime = #period ;
}
}

pub fn gen_const_stack_size(&self) -> ItemConst {
let stack_size = self.stack_size.as_u64() as u32;
parse_quote! {
pub(super) const STACK_SIZE: StackSize = #stack_size ;
}
}

pub fn gen_const_base_priority(&self) -> ItemConst {
let base_priority = self.base_priority;
parse_quote! {
pub(super) const BASE_PRIORITY: Priority = #base_priority ;
}
}

pub fn gen_const_deadline(&self) -> ItemConst {
let deadline: Path = self.deadline.into();
parse_quote! {
pub(super) const DEADLINE: Deadline = #deadline ;
}
}

pub fn gen_process_mod(&self) -> syn::Result<ItemMod> {
let ident = &self.ident;
let wrapper = self.gen_wrapper_fn();
let static_name = self.gen_static_name()?;
let consts = self.gen_consts()?;
let static_value = self.gen_static_value();
let create_fn = self.gen_create_fn();
let context_ident = Context::from_process(self).get_context_ident();
Expand All @@ -95,7 +135,7 @@ impl Process {

#wrapper
#create_fn
#static_name
#(#consts)*
#static_value
}
})
Expand Down
16 changes: 7 additions & 9 deletions macros/src/generate/util.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::time::Duration;

use proc_macro2::TokenStream;
use quote::format_ident;
use syn::{parse_quote, ExprPath, Path};
use syn::{parse_quote, Expr, Path};

use crate::parse::channel::QueuingDiscipline;
use crate::parse::process::{Deadline, SystemTime};

impl From<Deadline> for ExprPath {
impl From<Deadline> for Path {
fn from(d: Deadline) -> Self {
let deadline = format_ident!("{}", d.to_string());
parse_quote! {
Expand All @@ -16,16 +15,15 @@ impl From<Deadline> for ExprPath {
}
}

impl From<SystemTime> for TokenStream {
fn from(time: SystemTime) -> TokenStream {
impl From<SystemTime> for Expr {
fn from(time: SystemTime) -> Expr {
match time {
SystemTime::Infinite => parse_quote!(SystemTime::Infinite),
SystemTime::Normal(dur) => {
let dur: Duration = dur.into();
let dur = dur.as_nanos() as u64;
parse_quote!(SystemTime::Normal(core::time::Duration::from_nanos(
#dur
)))
let secs = dur.as_secs();
let nanos = dur.subsec_nanos();
parse_quote!(SystemTime::Normal(core::time::Duration::new( #secs , #nanos )))
}
}
}
Expand Down
41 changes: 37 additions & 4 deletions macros/src/parse/channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;
use std::str::FromStr;
use std::time::Duration;

use darling::{FromAttributes, FromMeta};
use proc_macro2::Ident;
Expand Down Expand Up @@ -95,7 +96,7 @@ pub struct QueuingOutProc {
#[darling(default = "ApexName::default")]
pub name: ApexName,
pub msg_size: WrappedByteSize,
pub msg_count: usize,
pub msg_count: u32,
pub discipline: QueuingDiscipline,
}

Expand All @@ -116,7 +117,7 @@ pub struct QueuingInProc {
#[darling(default = "ApexName::default")]
pub name: ApexName,
pub msg_size: WrappedByteSize,
pub msg_count: usize,
pub msg_count: u32,
pub discipline: QueuingDiscipline,
}

Expand Down Expand Up @@ -161,6 +162,38 @@ impl Channel {
.clone()
}

pub fn msg_size(&self) -> u64 {
match self {
Channel::SamplingOut(_ident, ch) => ch.msg_size.bytes(),
Channel::SamplingIn(_ident, ch) => ch.msg_size.bytes(),
Channel::QueuingOut(_ident, ch) => ch.msg_size.bytes(),
Channel::QueuingIn(_ident, ch) => ch.msg_size.bytes(),
}
}

pub fn msg_count(&self) -> Option<u32> {
match self {
Channel::QueuingOut(_ident, ch) => Some(ch.msg_count),
Channel::QueuingIn(_ident, ch) => Some(ch.msg_count),
_ => None,
}
}

pub fn discipline(&self) -> Option<QueuingDiscipline> {
match self {
Channel::QueuingOut(_ident, ch) => Some(ch.discipline),
Channel::QueuingIn(_ident, ch) => Some(ch.discipline),
_ => None,
}
}

pub fn refresh_period(&self) -> Option<Duration> {
if let Channel::SamplingIn(_ident, ch) = self {
return Some(ch.refresh_period.into());
}
None
}

pub fn typ(&self) -> Type {
match self {
Channel::SamplingOut(_, s) => {
Expand All @@ -173,12 +206,12 @@ impl Channel {
}
Channel::QueuingOut(_, q) => {
let size = q.msg_size.bytes() as u32;
let count = q.msg_count as u32;
let count = q.msg_count;
parse_quote!(ConstQueuingPortSender::< #size , #count , Hypervisor>)
}
Channel::QueuingIn(_, q) => {
let size = q.msg_size.bytes() as u32;
let count = q.msg_count as u32;
let count = q.msg_count;
parse_quote!(ConstQueuingPortReceiver::< #size , #count , Hypervisor>)
}
}
Expand Down
2 changes: 1 addition & 1 deletion macros/src/parse/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::parse::util::{
MayFromAttributes, WrappedByteSize, WrappedDuration,
};

#[derive(Debug, Clone, Display)]
#[derive(Debug, Copy, Clone, Display)]
pub enum SystemTime {
Infinite,
Normal(WrappedDuration),
Expand Down

0 comments on commit 7e597e5

Please sign in to comment.