From 59afbf02aa06d976dfd22df4cb87fadf6027a0fb Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Tue, 11 Jul 2017 23:44:54 -0500 Subject: [PATCH] compiler plugin -> proc macro --- Cargo.toml | 1 + macros/Cargo.toml | 2 +- macros/src/lib.rs | 50 ++++++--------------------------------------- macros/src/trans.rs | 2 +- src/lib.rs | 3 +++ 5 files changed, 12 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f37449d797..09acc71f86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,4 +14,5 @@ version = "0.2.0" [dependencies] cortex-m = "0.3.0" +cortex-m-rtfm-macros = { path = "macros" } static-ref = "0.2.0" \ No newline at end of file diff --git a/macros/Cargo.toml b/macros/Cargo.toml index babc2e2808..3aece07954 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -8,4 +8,4 @@ quote = "0.3.15" syn = "0.11.11" [lib] -plugin = true +proc-macro = true diff --git a/macros/src/lib.rs b/macros/src/lib.rs index a5fdf96de9..10839eebce 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,64 +1,26 @@ #![deny(warnings)] -#![feature(plugin_registrar)] -#![feature(proc_macro_internals)] -#![feature(rustc_private)] +#![feature(proc_macro)] #![recursion_limit = "128"] extern crate proc_macro; #[macro_use] extern crate quote; -extern crate rustc_errors; -extern crate rustc_plugin; extern crate syn; -extern crate syntax as rustc_syntax; - -use proc_macro::TokenStream; -use rustc_errors::Handler; -use rustc_errors::emitter::ColorConfig; -use rustc_plugin::Registry; -use rustc_syntax::codemap::{CodeMap, FilePathMapping}; -use rustc_syntax::ext::base::SyntaxExtension; -use rustc_syntax::parse::ParseSess; -use rustc_syntax::symbol::Symbol; -use rustc_syntax::tokenstream::TokenStream as TokenStream_; -use std::rc::Rc; -use std::str::FromStr; mod check; mod syntax; mod trans; mod util; -fn expand_rtfm(ts: TokenStream_) -> TokenStream_ { +use proc_macro::TokenStream; + +#[proc_macro] +pub fn rtfm(ts: TokenStream) -> TokenStream { let input = format!("{}", ts); let app = syntax::parse::app(&input); let ceilings = util::compute_ceilings(&app); check::resources(&app.resources, &ceilings); - let output = format!("{}", trans::app(&app, &ceilings)); - - let mapping = FilePathMapping::empty(); - let codemap = Rc::new(CodeMap::new(mapping)); - - let tty_handler = Handler::with_tty_emitter( - ColorConfig::Auto, - true, - false, - Some(codemap.clone()), - ); - - let sess = ParseSess::with_span_handler(tty_handler, codemap.clone()); - proc_macro::__internal::set_parse_sess(&sess, || { - let ts = TokenStream::from_str(&output).unwrap(); - proc_macro::__internal::token_stream_inner(ts) - }) -} - -#[plugin_registrar] -pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension( - Symbol::intern("rtfm"), - SyntaxExtension::ProcMacro(Box::new(expand_rtfm)), - ); + format!("{}", trans::app(&app, &ceilings)).parse().unwrap() } diff --git a/macros/src/trans.rs b/macros/src/trans.rs index 790ffb8eb5..13dc51d186 100644 --- a/macros/src/trans.rs +++ b/macros/src/trans.rs @@ -100,7 +100,7 @@ fn init(app: &App, main: &mut Vec, root: &mut Vec) { exceptions.push(quote! { let prio_bits = #device::NVIC_PRIO_BITS; let hw = ((1 << prio_bits) - #priority) << (8 - prio_bits); - scb.shpr[rtfm::Exception::#name.nr() - 4].write(hw); + scb.shpr[#krate::Exception::#name.nr() - 4].write(hw); }); } Kind::Interrupt { enabled } => { diff --git a/src/lib.rs b/src/lib.rs index 5d49af17f2..23f3abd1c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,16 @@ #![feature(asm)] #![feature(const_fn)] #![feature(optin_builtin_traits)] +#![feature(proc_macro)] #![no_std] extern crate cortex_m; +extern crate cortex_m_rtfm_macros; extern crate static_ref; use core::cell::UnsafeCell; +pub use cortex_m_rtfm_macros::rtfm; pub use cortex_m::asm::{bkpt, wfi}; pub use cortex_m::interrupt::CriticalSection; pub use cortex_m::interrupt::free as atomic;