From ef5307d83a1d62df0569d78db75d4006147c927d Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Tue, 6 Jul 2021 22:47:48 +0200 Subject: Minimal app now compiles --- macros/src/codegen/init.rs | 130 +++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 51 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index fe8a126..e3f7408 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -5,7 +5,7 @@ use rtic_syntax::{ast::App, Context}; use crate::{ analyze::Analysis, check::Extra, - codegen::{locals, module, resources_struct}, + codegen::{local_resources_struct, module}, }; type CodegenResult = ( @@ -18,68 +18,96 @@ type CodegenResult = ( // - the `${init}` module, which contains types like `${init}::Context` Vec, // user_init -- the `#[init]` function written by the user - Option, - // call_init -- the call to the user `#[init]` if there's one - Option, + TokenStream2, + // call_init -- the call to the user `#[init]` + TokenStream2, ); /// Generates support code for `#[init]` functions pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { - if !app.inits.is_empty() { - let init = &app.inits.first().unwrap(); - let mut needs_lt = false; - let name = &init.name; + let init = &app.init; + let mut needs_lt = false; + let name = &init.name; - let mut root_init = vec![]; + let mut root_init = vec![]; - let mut locals_pat = None; - let mut locals_new = None; - if !init.locals.is_empty() { - let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); + // TODO: Fix locals + // let mut locals_pat = None; + // let mut locals_new = None; + // if !init.locals.is_empty() { + // let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); - locals_new = Some(quote!(#name::Locals::new())); - locals_pat = Some(pat); - root_init.push(struct_); - } + // locals_new = Some(quote!(#name::Locals::new())); + // locals_pat = Some(pat); + // root_init.push(struct_); + // } - let context = &init.context; - let attrs = &init.attrs; - let stmts = &init.stmts; - let locals_pat = locals_pat.iter(); + let context = &init.context; + let attrs = &init.attrs; + let stmts = &init.stmts; + let shared = &init.user_shared_struct; + let local = &init.user_local_struct; - let user_init_return = quote! {#name::LateResources, #name::Monotonics}; + let shared_resources: Vec<_> = app + .shared_resources + .iter() + .map(|(k, v)| { + let ty = &v.ty; + let cfgs = &v.cfgs; + quote!( + #(#cfgs)* + #k: #ty, + ) + }) + .collect(); + let local_resources: Vec<_> = app + .local_resources + .iter() + .map(|(k, v)| { + let ty = &v.ty; + let cfgs = &v.cfgs; + quote!( + #(#cfgs)* + #k: #ty, + ) + }) + .collect(); + root_init.push(quote! { + struct #shared { + #(#shared_resources)* + } - let user_init = Some(quote!( - #(#attrs)* - #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context) -> (#user_init_return) { - #(#stmts)* - } - )); + struct #local { + #(#local_resources)* + } + }); - let mut mod_app = None; - if !init.args.resources.is_empty() { - let (item, constructor) = resources_struct::codegen(Context::Init, &mut needs_lt, app); + // let locals_pat = locals_pat.iter(); - root_init.push(item); - mod_app = Some(constructor); + let user_init_return = quote! {#shared, #local, #name::Monotonics}; + + let user_init = quote!( + #(#attrs)* + #[allow(non_snake_case)] + fn #name(#context: #name::Context) -> (#user_init_return) { + #(#stmts)* } + ); + + let mut mod_app = None; + + // let locals_new = locals_new.iter(); + let call_init = quote! { + let (shared_resources, local_resources, mut monotonics) = #name(#name::Context::new(core.into())); + }; + + root_init.push(module::codegen( + Context::Init, + needs_lt, + app, + analysis, + extra, + )); - let locals_new = locals_new.iter(); - let call_init = Some( - quote!(let (late, mut monotonics) = #name(#(#locals_new,)* #name::Context::new(core.into()));), - ); - - root_init.push(module::codegen( - Context::Init, - needs_lt, - app, - analysis, - extra, - )); - - (mod_app, root_init, user_init, call_init) - } else { - (None, vec![], None, None) - } + (mod_app, root_init, user_init, call_init) } -- cgit v1.2.3 From d7393c5b27fc95f3569d12137ee0c4d03ff7e2ba Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 7 Jul 2021 21:03:56 +0200 Subject: Full local resource syntax working --- macros/src/codegen/init.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index e3f7408..1bea7b7 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -26,7 +26,7 @@ type CodegenResult = ( /// Generates support code for `#[init]` functions pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { let init = &app.init; - let mut needs_lt = false; + let mut local_needs_lt = false; let name = &init.name; let mut root_init = vec![]; @@ -96,6 +96,16 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { let mut mod_app = None; + // `${task}Locals` + if !init.args.local_resources.is_empty() { + let (item, constructor) = + local_resources_struct::codegen(Context::Init, &mut local_needs_lt, app); + + root_init.push(item); + + mod_app = Some(constructor); + } + // let locals_new = locals_new.iter(); let call_init = quote! { let (shared_resources, local_resources, mut monotonics) = #name(#name::Context::new(core.into())); @@ -103,7 +113,8 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { root_init.push(module::codegen( Context::Init, - needs_lt, + false, + local_needs_lt, app, analysis, extra, -- cgit v1.2.3 From 98d2af9d73da56910c8bb6cb662fbc4d609a704a Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 7 Jul 2021 22:50:59 +0200 Subject: Fixing tests --- macros/src/codegen/init.rs | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 1bea7b7..b6d3f72 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -31,17 +31,6 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { let mut root_init = vec![]; - // TODO: Fix locals - // let mut locals_pat = None; - // let mut locals_new = None; - // if !init.locals.is_empty() { - // let (struct_, pat) = locals::codegen(Context::Init, &init.locals, app); - - // locals_new = Some(quote!(#name::Locals::new())); - // locals_pat = Some(pat); - // root_init.push(struct_); - // } - let context = &init.context; let attrs = &init.attrs; let stmts = &init.stmts; -- cgit v1.2.3