diff options
Diffstat (limited to 'macros')
-rw-r--r-- | macros/Cargo.toml | 6 | ||||
-rw-r--r-- | macros/src/analyze.rs | 46 | ||||
-rw-r--r-- | macros/src/trans.rs | 180 |
3 files changed, 147 insertions, 85 deletions
diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 81b788c5..cc893cd2 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -11,11 +11,11 @@ version = "0.3.1" [dependencies] failure = "0.1.1" -proc-macro2 = "0.3.6" -quote = "0.5.1" +proc-macro2 = "0.4.4" +quote = "0.6.3" # rtfm-syntax = "0.4.0" rtfm-syntax = { git = "https://github.com/japaric/rtfm-syntax", branch = "tq" } -syn = "0.13.1" +syn = "0.14.1" [dependencies.either] version = "1" diff --git a/macros/src/analyze.rs b/macros/src/analyze.rs index 3bfc72f3..365030ca 100644 --- a/macros/src/analyze.rs +++ b/macros/src/analyze.rs @@ -13,14 +13,19 @@ pub fn app(app: &App) -> Context { let mut tq = TimerQueue::new(); let mut free_interrupts = app.free_interrupts.iter().cloned().collect::<Vec<_>>(); - schedule_now.extend(&app.init.schedule_now); + schedule_now.extend(app.init.schedule_now.iter().cloned()); for task in &app.init.schedule_after { - schedule_after.insert(*task); + schedule_after.insert(task.clone()); // Timer queue - if let Entry::Vacant(entry) = tq.tasks.entry(*task) { - tq.capacity += app.tasks[task].interrupt_or_instances.right().unwrap(); + if let Entry::Vacant(entry) = tq.tasks.entry(task.clone()) { + tq.capacity += app.tasks[task] + .interrupt_or_instances + .as_ref() + .right() + .clone() + .unwrap(); entry.insert(app.tasks[task].priority); } } @@ -28,28 +33,33 @@ pub fn app(app: &App) -> Context { // compute dispatchers for (name, task) in &app.tasks { match task.interrupt_or_instances { - Either::Left(interrupt) => { - triggers.insert(interrupt, (*name, task.priority)); + Either::Left(ref interrupt) => { + triggers.insert(interrupt.clone(), (name.clone(), task.priority)); } Either::Right(instances) => { let dispatcher = dispatchers.entry(task.priority).or_insert_with(|| { Dispatcher::new(free_interrupts.pop().expect("not enough free interrupts")) }); - dispatcher.tasks.push(*name); + dispatcher.tasks.push(name.clone()); dispatcher.capacity += instances; } } for task in &task.schedule_now { - schedule_now.insert(*task); + schedule_now.insert(task.clone()); } for task in &task.schedule_after { - schedule_after.insert(*task); + schedule_after.insert(task.clone()); // Timer queue - if let Entry::Vacant(entry) = tq.tasks.entry(*task) { - tq.capacity += app.tasks[task].interrupt_or_instances.right().unwrap(); + if let Entry::Vacant(entry) = tq.tasks.entry(task.clone()) { + tq.capacity += app.tasks[task] + .interrupt_or_instances + .as_ref() + .right() + .clone() + .unwrap(); entry.insert(app.tasks[task].priority); } } @@ -83,7 +93,7 @@ pub fn app(app: &App) -> Context { })) { let ceiling = ceilings .resources - .entry(*resource) + .entry(resource.clone()) .or_insert(Ceiling::Owned(priority)); if priority > (*ceiling).into() { *ceiling = Ceiling::Shared(priority); @@ -101,7 +111,10 @@ pub fn app(app: &App) -> Context { }) { // schedule_now callers contend for the consumer end of the task slot queue (#task::SQ) and // .. - let ceiling = ceilings.slot_queues.entry(*task).or_insert(caller_priority); + let ceiling = ceilings + .slot_queues + .entry(task.clone()) + .or_insert(caller_priority); if caller_priority > *ceiling { *ceiling = caller_priority; @@ -128,7 +141,10 @@ pub fn app(app: &App) -> Context { }) { // schedule_after callers contend for the consumer end of the task slot queue (#task::SQ) // and .. - let ceiling = ceilings.slot_queues.entry(*task).or_insert(caller_priority); + let ceiling = ceilings + .slot_queues + .entry(task.clone()) + .or_insert(caller_priority); if caller_priority > *ceiling { *ceiling = caller_priority; @@ -209,7 +225,7 @@ impl Dispatcher { } pub fn interrupt(&self) -> Ident { - self.interrupt + self.interrupt.clone() } pub fn tasks(&self) -> &[Ident] { diff --git a/macros/src/trans.rs b/macros/src/trans.rs index d21a8a5b..d80eface 100644 --- a/macros/src/trans.rs +++ b/macros/src/trans.rs @@ -1,14 +1,14 @@ -use quote::Tokens; - use either::Either; +use proc_macro2::Span; +use proc_macro2::TokenStream; use syn::Ident; use syntax::check::App; use analyze::Context; -pub fn app(ctxt: &Context, app: &App) -> Tokens { +pub fn app(ctxt: &Context, app: &App) -> TokenStream { let mut root = vec![]; - let k = Ident::from("_rtfm"); + let k = Ident::new("_rtfm", Span::call_site()); let device = &app.device; let needs_tq = !ctxt.schedule_after.is_empty(); @@ -34,15 +34,18 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { .map(|e| quote!(#e)) .unwrap_or_else(|| quote!(unsafe { ::#k::_impl::uninitialized() })); - let ceiling = Ident::from(format!( - "U{}", - ctxt.ceilings - .resources() - .get(name) - .cloned() - .map(u8::from) - .unwrap_or(0) // 0 = resource owned by `init` - )); + let ceiling = Ident::new( + &format!( + "U{}", + ctxt.ceilings + .resources() + .get(name) + .cloned() + .map(u8::from) + .unwrap_or(0) // 0 = resource owned by `init` + ), + Span::call_site(), + ); root.push(quote! { #[allow(unsafe_code)] unsafe impl ::#k::Resource for _resource::#name { @@ -183,7 +186,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { for (name, task) in &app.tasks { let path = &task.path; - let lifetime = if task.resources + let lifetime = if task + .resources .iter() .any(|res| ctxt.ceilings.resources()[res].is_owned()) { @@ -192,11 +196,10 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { None }; - let _context = Ident::from(format!( - "_ZN{}{}7ContextE", - name.as_ref().as_bytes().len(), - name - )); + let _context = Ident::new( + &format!("_ZN{}{}7ContextE", name.to_string().as_bytes().len(), name), + Span::call_site(), + ); let mut mod_ = vec![]; @@ -206,7 +209,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { quote!(scheduled_time) }; - let input_ = task.input + let input_ = task + .input .as_ref() .map(|input| quote!(#input)) .unwrap_or(quote!(())); @@ -261,7 +265,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }); } - let res_fields = task.resources + let res_fields = task + .resources .iter() .map(|res| { if ctxt.ceilings.resources()[res].is_owned() { @@ -281,7 +286,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { } }); - let tasks_fields = task.schedule_now + let tasks_fields = task + .schedule_now .iter() .map(|task| quote!(pub #task: ::_schedule_now::#task)) .chain( @@ -291,7 +297,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { ) .collect::<Vec<_>>(); - let tasks_exprs = task.schedule_now + let tasks_exprs = task + .schedule_now .iter() .map(|task| { if cfg!(feature = "timer-queue") { @@ -307,7 +314,7 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { ) .collect::<Vec<_>>(); - let priority = Ident::from(format!("U{}", task.priority)); + let priority = Ident::new(&format!("U{}", task.priority), Span::call_site()); mod_.push(quote! { #[allow(unused_imports)] use ::#k::Resource; @@ -363,9 +370,9 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { } match task.interrupt_or_instances { - Either::Left(interrupt) => { - let export_name = interrupt.as_ref(); - let fn_name = Ident::from(format!("_{}", interrupt)); + Either::Left(ref interrupt) => { + let export_name = interrupt.to_string(); + let fn_name = Ident::new(&format!("_{}", interrupt), Span::call_site()); let bl = if cfg!(feature = "timer-queue") { Some(quote!(_now,)) @@ -387,7 +394,7 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }); } Either::Right(instances) => { - let ucapacity = Ident::from(format!("U{}", instances)); + let ucapacity = Ident::new(&format!("U{}", instances), Span::call_site()); let capacity = instances as usize; root.push(quote! { @@ -408,13 +415,19 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }); - let ceiling = Ident::from(format!( - "U{}", - ctxt.ceilings.slot_queues().get(name).cloned() // 0 = owned by init + let ceiling = Ident::new( + &format!( + "U{}", + ctxt.ceilings.slot_queues().get(name).cloned() // 0 = owned by init .unwrap_or(0) - )); + ), + Span::call_site(), + ); - let mangled = Ident::from(format!("_ZN{}{}6PAYLOADSE", name.as_ref().len(), name)); + let mangled = Ident::new( + &format!("_ZN{}{}6PAYLOADSE", name.to_string().len(), name), + Span::call_site(), + ); // NOTE must be in the root because of `#input` root.push(quote! { @@ -459,15 +472,17 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { } /* schedule_now */ - let schedule_now = ctxt.schedule_now + let schedule_now = ctxt + .schedule_now .iter() .map(|name| { let task = &app.tasks[name]; let priority = task.priority; - let _priority = Ident::from(format!("_{}", priority)); + let _priority = Ident::new(&format!("_{}", priority), Span::call_site()); let interrupt = ctxt.dispatchers[&priority].interrupt(); - let input_ = task.input + let input_ = task + .input .as_ref() .map(|input| quote!(#input)) .unwrap_or(quote!(())); @@ -477,12 +492,18 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { (quote!(), quote!(())) }; - let sqc = Ident::from(format!( - "U{}", - ctxt.ceilings.slot_queues().get(name).cloned() // 0 = owned by init + let sqc = Ident::new( + &format!( + "U{}", + ctxt.ceilings.slot_queues().get(name).cloned() // 0 = owned by init .unwrap_or(0) - )); - let qc = Ident::from(format!("U{}", ctxt.ceilings.dispatch_queues()[&priority])); + ), + Span::call_site(), + ); + let qc = Ident::new( + &format!("U{}", ctxt.ceilings.dispatch_queues()[&priority]), + Span::call_site(), + ); if cfg!(feature = "timer-queue") { root.push(quote! { @@ -620,18 +641,26 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }); /* schedule_after */ - let schedule_after = ctxt.schedule_after + let schedule_after = ctxt + .schedule_after .iter() .map(|name| { let task = &app.tasks[name]; - let sqc = Ident::from(format!( - "U{}", - ctxt.ceilings.slot_queues().get(name).unwrap_or(&0) // 0 = owned by init - )); - let tqc = Ident::from(format!("U{}", ctxt.ceilings.timer_queue())); - - let input_ = task.input + let sqc = Ident::new( + &format!( + "U{}", + ctxt.ceilings.slot_queues().get(name).unwrap_or(&0) // 0 = owned by init + ), + Span::call_site(), + ); + let tqc = Ident::new( + &format!("U{}", ctxt.ceilings.timer_queue()), + Span::call_site(), + ); + + let input_ = task + .input .as_ref() .map(|input| quote!(#input)) .unwrap_or(quote!(())); @@ -649,7 +678,7 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { #[inline] pub fn schedule_after<P>( &self, - p: &::#k::Priority<P>, + _p: &::#k::Priority<P>, after: u32, #payload_in ) -> Result<(), #input_> @@ -722,13 +751,17 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { /* Timer queue */ if needs_tq { - let capacity = Ident::from(format!("U{}", ctxt.timer_queue.capacity())); + let capacity = Ident::new( + &format!("U{}", ctxt.timer_queue.capacity()), + Span::call_site(), + ); let tasks = ctxt.timer_queue.tasks().keys(); - let arms = ctxt.timer_queue + let arms = ctxt + .timer_queue .tasks() .iter() .map(|(name, priority)| { - let _priority = Ident::from(format!("_{}", priority)); + let _priority = Ident::new(&format!("_{}", priority), Span::call_site()); let interrupt = ctxt.dispatchers[priority].interrupt(); quote! { @@ -743,8 +776,11 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }) .collect::<Vec<_>>(); - let ceiling = Ident::from(format!("U{}", ctxt.ceilings.timer_queue())); - let priority = Ident::from(format!("U{}", ctxt.sys_tick)); + let ceiling = Ident::new( + &format!("U{}", ctxt.ceilings.timer_queue()), + Span::call_site(), + ); + let priority = Ident::new(&format!("U{}", ctxt.sys_tick), Span::call_site()); root.push(quote! { mod _tq { #[allow(non_camel_case_types)] @@ -800,10 +836,13 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { /* Dispatchers */ for (priority, dispatcher) in &ctxt.dispatchers { - let _priority = Ident::from(format!("_{}", priority)); - let capacity = Ident::from(format!("U{}", dispatcher.capacity())); + let _priority = Ident::new(&format!("_{}", priority), Span::call_site()); + let capacity = Ident::new(&format!("U{}", dispatcher.capacity()), Span::call_site()); let tasks = dispatcher.tasks(); - let ceiling = Ident::from(format!("U{}", ctxt.ceilings.dispatch_queues()[priority])); + let ceiling = Ident::new( + &format!("U{}", ctxt.ceilings.dispatch_queues()[priority]), + Span::call_site(), + ); root.push(quote! { mod #_priority { @@ -873,8 +912,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { .collect::<Vec<_>>(); let interrupt = dispatcher.interrupt(); - let export_name = interrupt.as_ref(); - let fn_name = Ident::from(format!("_{}", export_name)); + let export_name = interrupt.to_string(); + let fn_name = Ident::new(&format!("_{}", export_name), Span::call_site()); root.push(quote! { #[allow(non_snake_case)] #[allow(unsafe_code)] @@ -956,7 +995,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }; /* init */ - let res_fields = app.init + let res_fields = app + .init .resources .iter() .map(|r| { @@ -968,13 +1008,15 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }) .collect::<Vec<_>>(); - let res_exprs = app.init + let res_exprs = app + .init .resources .iter() .map(|r| quote!(#r: #r::_new())) .collect::<Vec<_>>(); - let tasks_fields = app.init + let tasks_fields = app + .init .schedule_now .iter() .map(|task| quote!(pub #task: ::_schedule_now::#task)) @@ -986,7 +1028,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { ) .collect::<Vec<_>>(); - let tasks_exprs = app.init + let tasks_exprs = app + .init .schedule_now .iter() .map(|task| { @@ -1004,7 +1047,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { ) .collect::<Vec<_>>(); - let late_resources = app.resources + let late_resources = app + .resources .iter() .filter_map(|(name, res)| { if res.expr.is_none() && !app.init.resources.contains(name) { @@ -1158,7 +1202,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { /* idle */ if let Some(idle) = app.idle.as_ref() { - let res_fields = idle.resources + let res_fields = idle + .resources .iter() .map(|res| { if ctxt.ceilings.resources()[res].is_owned() { @@ -1171,7 +1216,8 @@ pub fn app(ctxt: &Context, app: &App) -> Tokens { }) .collect::<Vec<_>>(); - let res_exprs = idle.resources + let res_exprs = idle + .resources .iter() .map(|res| { if ctxt.ceilings.resources()[res].is_owned() { |