diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 748637c16971..0173f7c48565 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -12,7 +12,8 @@ version = "0.2.0" [dependencies] error-chain = "0.10.0" quote = "0.3.15" -rtfm-syntax = "0.2.0" +# rtfm-syntax = "0.2.0" +rtfm-syntax = { git = "https://github.com/japaric/rtfm-syntax", branch = "root" } syn = "0.11.11" [lib] diff --git a/macros/src/check.rs b/macros/src/check.rs index 3cd112acfd17..3f6c74388fed 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -11,6 +11,7 @@ pub struct App { pub idle: Idle, pub init: Init, pub resources: Statics, + pub root: Option, pub tasks: Tasks, } @@ -60,6 +61,7 @@ pub fn app(app: check::App) -> Result { idle: app.idle, init: app.init, resources: app.resources, + root: app.root, tasks: app.tasks .into_iter() .map(|(k, v)| { diff --git a/macros/src/trans.rs b/macros/src/trans.rs index 96ff770b64f2..0e886d80e3a8 100644 --- a/macros/src/trans.rs +++ b/macros/src/trans.rs @@ -12,6 +12,7 @@ pub fn app(app: &App, ownerships: &Ownerships) -> Tokens { let mut root = vec![]; let mut main = vec![]; + ::trans::root(app, &mut main); ::trans::init(app, &mut main, &mut root); ::trans::idle(app, ownerships, &mut main, &mut root); ::trans::resources(app, ownerships, &mut root); @@ -309,18 +310,34 @@ fn init(app: &App, main: &mut Vec, root: &mut Vec) { } let init = &app.init.path; - main.push(quote! { - // type check - let init: fn(#(#tys,)*) #ret = #init; + // TODO DRY + if app.root.is_some() { + main.push(quote! { + // type check + let init: fn(#(#tys,)* &'static mut _) #ret = #init; + + #krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe { + let _late_resources = init(#(#exprs,)* &mut *(&mut root as *mut _)); + #(#late_resource_init)* + + #(#exceptions)* + #(#interrupts)* + }); + }); + } else { + main.push(quote! { + // type check + let init: fn(#(#tys,)*) #ret = #init; - #krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe { - let _late_resources = init(#(#exprs,)*); - #(#late_resource_init)* + #krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe { + let _late_resources = init(#(#exprs,)*); + #(#late_resource_init)* - #(#exceptions)* - #(#interrupts)* + #(#exceptions)* + #(#interrupts)* + }); }); - }); + } } fn resources(app: &App, ownerships: &Ownerships, root: &mut Vec) { @@ -539,6 +556,15 @@ fn resources(app: &App, ownerships: &Ownerships, root: &mut Vec) { }); } +fn root(app: &App, main: &mut Vec) { + if let Some(root) = app.root.as_ref() { + main.push(quote! { + let root: fn() -> _ = #root; + let mut root = root(); + }); + } +} + fn tasks(app: &App, ownerships: &Ownerships, root: &mut Vec) { let device = &app.device; let krate = krate();