compiler plugin -> proc macro

This commit is contained in:
Jorge Aparicio 2017-07-11 23:44:54 -05:00
parent 8485a24d36
commit 59afbf02aa
5 changed files with 12 additions and 46 deletions

View file

@ -14,4 +14,5 @@ version = "0.2.0"
[dependencies] [dependencies]
cortex-m = "0.3.0" cortex-m = "0.3.0"
cortex-m-rtfm-macros = { path = "macros" }
static-ref = "0.2.0" static-ref = "0.2.0"

View file

@ -8,4 +8,4 @@ quote = "0.3.15"
syn = "0.11.11" syn = "0.11.11"
[lib] [lib]
plugin = true proc-macro = true

View file

@ -1,64 +1,26 @@
#![deny(warnings)] #![deny(warnings)]
#![feature(plugin_registrar)] #![feature(proc_macro)]
#![feature(proc_macro_internals)]
#![feature(rustc_private)]
#![recursion_limit = "128"] #![recursion_limit = "128"]
extern crate proc_macro; extern crate proc_macro;
#[macro_use] #[macro_use]
extern crate quote; extern crate quote;
extern crate rustc_errors;
extern crate rustc_plugin;
extern crate syn; 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 check;
mod syntax; mod syntax;
mod trans; mod trans;
mod util; 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 input = format!("{}", ts);
let app = syntax::parse::app(&input); let app = syntax::parse::app(&input);
let ceilings = util::compute_ceilings(&app); let ceilings = util::compute_ceilings(&app);
check::resources(&app.resources, &ceilings); check::resources(&app.resources, &ceilings);
let output = format!("{}", trans::app(&app, &ceilings)); format!("{}", trans::app(&app, &ceilings)).parse().unwrap()
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)),
);
} }

View file

@ -100,7 +100,7 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
exceptions.push(quote! { exceptions.push(quote! {
let prio_bits = #device::NVIC_PRIO_BITS; let prio_bits = #device::NVIC_PRIO_BITS;
let hw = ((1 << prio_bits) - #priority) << (8 - 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 } => { Kind::Interrupt { enabled } => {

View file

@ -1,13 +1,16 @@
#![feature(asm)] #![feature(asm)]
#![feature(const_fn)] #![feature(const_fn)]
#![feature(optin_builtin_traits)] #![feature(optin_builtin_traits)]
#![feature(proc_macro)]
#![no_std] #![no_std]
extern crate cortex_m; extern crate cortex_m;
extern crate cortex_m_rtfm_macros;
extern crate static_ref; extern crate static_ref;
use core::cell::UnsafeCell; use core::cell::UnsafeCell;
pub use cortex_m_rtfm_macros::rtfm;
pub use cortex_m::asm::{bkpt, wfi}; pub use cortex_m::asm::{bkpt, wfi};
pub use cortex_m::interrupt::CriticalSection; pub use cortex_m::interrupt::CriticalSection;
pub use cortex_m::interrupt::free as atomic; pub use cortex_m::interrupt::free as atomic;