From 76595b7aedd2a14aea8569b75fabe62120f93230 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sat, 7 Jan 2023 14:26:55 +0100 Subject: [PATCH] All codegen is now explicit --- macros/src/codegen.rs | 56 +++++++------------------ macros/src/codegen/async_dispatchers.rs | 4 +- macros/src/codegen/hardware_tasks.rs | 23 ++++------ macros/src/codegen/idle.rs | 27 ++++-------- macros/src/codegen/init.rs | 23 ++++------ macros/src/codegen/local_resources.rs | 13 +----- macros/src/codegen/shared_resources.rs | 16 +++---- macros/src/codegen/software_tasks.rs | 23 ++++------ 8 files changed, 57 insertions(+), 128 deletions(-) diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index bb1028f764..24e98ce90a 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -29,21 +29,14 @@ mod main; pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 { // Generate the `main` function let main = main::codegen(app, analysis); + let init_codegen = init::codegen(app, analysis); + let idle_codegen = idle::codegen(app, analysis); + let shared_resources_codegen = shared_resources::codegen(app, analysis); + let local_resources_codegen = local_resources::codegen(app, analysis); + let hardware_tasks_codegen = hardware_tasks::codegen(app, analysis); + let software_tasks_codegen = software_tasks::codegen(app, analysis); + let async_dispatchers_codegen = async_dispatchers::codegen(app, analysis); - let (mod_app_init, root_init, user_init) = init::codegen(app, analysis); - - let (mod_app_idle, root_idle, user_idle) = idle::codegen(app, analysis); - - let (mod_app_shared_resources, mod_shared_resources) = shared_resources::codegen(app, analysis); - let (mod_app_local_resources, mod_local_resources) = local_resources::codegen(app, analysis); - - let (mod_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks) = - hardware_tasks::codegen(app, analysis); - - let (mod_app_software_tasks, root_software_tasks, user_software_tasks) = - software_tasks::codegen(app, analysis); - - let mod_app_async_dispatchers = async_dispatchers::codegen(app, analysis); let user_imports = &app.user_imports; let user_code = &app.user_code; let name = &app.name; @@ -59,43 +52,22 @@ pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 { #(#user_imports)* - /// User code from within the module #(#user_code)* /// User code end - #(#user_hardware_tasks)* + #init_codegen - #(#user_software_tasks)* + #idle_codegen - #mod_app_init + #hardware_tasks_codegen - #(#root_init)* + #software_tasks_codegen - #user_init + #shared_resources_codegen - #(#mod_app_idle)* + #local_resources_codegen - #(#root_idle)* - - #user_idle - - #mod_shared_resources - - #mod_local_resources - - #(#root_hardware_tasks)* - - #(#root_software_tasks)* - - #(#mod_app_shared_resources)* - - #(#mod_app_local_resources)* - - #(#mod_app_hardware_tasks)* - - #(#mod_app_software_tasks)* - - #(#mod_app_async_dispatchers)* + #async_dispatchers_codegen #main } diff --git a/macros/src/codegen/async_dispatchers.rs b/macros/src/codegen/async_dispatchers.rs index d53d7b5e0c..62b17fee4a 100644 --- a/macros/src/codegen/async_dispatchers.rs +++ b/macros/src/codegen/async_dispatchers.rs @@ -4,7 +4,7 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; /// Generates task dispatchers -pub fn codegen(app: &App, analysis: &Analysis) -> Vec { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let mut items = vec![]; let interrupts = &analysis.interrupts; @@ -96,5 +96,5 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec { )); } - items + quote!(#(#items)*) } diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index 9ea5825b3d..8a5a8f6cf4 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -7,20 +7,7 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; /// Generate support code for hardware tasks (`#[exception]`s and `#[interrupt]`s) -pub fn codegen( - app: &App, - analysis: &Analysis, -) -> ( - // mod_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors - Vec, - // root_hardware_tasks -- items that must be placed in the root of the crate: - // - `${task}Locals` structs - // - `${task}Resources` structs - // - `${task}` modules - Vec, - // user_hardware_tasks -- the `#[task]` functions written by the user - Vec, -) { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; @@ -90,5 +77,11 @@ pub fn codegen( } } - (mod_app, root, user_tasks) + quote!( + #(#mod_app)* + + #(#root)* + + #(#user_tasks)* + ) } diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index 1f05d12901..0c833ef391 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -7,20 +7,7 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; /// Generates support code for `#[idle]` functions -pub fn codegen( - app: &App, - analysis: &Analysis, -) -> ( - // mod_app_idle -- the `${idle}Resources` constructor - Vec, - // root_idle -- items that must be placed in the root of the crate: - // - the `${idle}Locals` struct - // - the `${idle}Resources` struct - // - the `${idle}` module, which contains types like `${idle}::Context` - Vec, - // user_idle - Option, -) { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { if let Some(idle) = &app.idle { let mut mod_app = vec![]; let mut root_idle = vec![]; @@ -58,10 +45,14 @@ pub fn codegen( } )); - (mod_app, root_idle, user_idle) - } else { - // TODO: No idle defined, check for 0-priority tasks and generate an executor if needed + quote!( + #(#mod_app)* - (vec![], vec![], None) + #(#root_idle)* + + #user_idle + ) + } else { + quote!() } } diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 3b2bcd471b..6e1059f7eb 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -7,21 +7,8 @@ use crate::{ syntax::{ast::App, Context}, }; -type CodegenResult = ( - // mod_app_idle -- the `${init}Resources` constructor - Option, - // root_init -- items that must be placed in the root of the crate: - // - the `${init}Locals` struct - // - the `${init}Resources` struct - // - the `${init}LateResources` struct - // - the `${init}` module, which contains types like `${init}::Context` - Vec, - // user_init -- the `#[init]` function written by the user - TokenStream2, -); - /// Generates support code for `#[init]` functions -pub fn codegen(app: &App, analysis: &Analysis) -> CodegenResult { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let init = &app.init; let name = &init.name; @@ -98,5 +85,11 @@ pub fn codegen(app: &App, analysis: &Analysis) -> CodegenResult { root_init.push(module::codegen(Context::Init, app, analysis)); - (mod_app, root_init, user_init) + quote!( + #mod_app + + #(#root_init)* + + #user_init + ) } diff --git a/macros/src/codegen/local_resources.rs b/macros/src/codegen/local_resources.rs index 6fc63cd93e..e6d15533b8 100644 --- a/macros/src/codegen/local_resources.rs +++ b/macros/src/codegen/local_resources.rs @@ -6,17 +6,8 @@ use quote::quote; /// Generates `local` variables and local resource proxies /// /// I.e. the `static` variables and theirs proxies. -pub fn codegen( - app: &App, - _analysis: &Analysis, -) -> ( - // mod_app -- the `static` variables behind the proxies - Vec, - // mod_resources -- the `resources` module - TokenStream2, -) { +pub fn codegen(app: &App, _analysis: &Analysis) -> TokenStream2 { let mut mod_app = vec![]; - // let mut mod_resources: _ = vec![]; // All local resources declared in the `#[local]' struct for (name, res) in &app.local_resources { @@ -70,5 +61,5 @@ pub fn codegen( )); } - (mod_app, TokenStream2::new()) + quote!(#(#mod_app)*) } diff --git a/macros/src/codegen/shared_resources.rs b/macros/src/codegen/shared_resources.rs index 5c54fb99b2..19fd13fecc 100644 --- a/macros/src/codegen/shared_resources.rs +++ b/macros/src/codegen/shared_resources.rs @@ -5,15 +5,7 @@ use quote::quote; use std::collections::HashMap; /// Generates `static` variables and shared resource proxies -pub fn codegen( - app: &App, - analysis: &Analysis, -) -> ( - // mod_app -- the `static` variables behind the proxies - Vec, - // mod_resources -- the `resources` module - TokenStream2, -) { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let mut mod_app = vec![]; let mut mod_resources = vec![]; @@ -183,5 +175,9 @@ pub fn codegen( )); } - (mod_app, mod_resources) + quote!( + #(#mod_app)* + + #mod_resources + ) } diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 350c1e6009..4cb1fa95f6 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -6,20 +6,7 @@ use crate::{ use proc_macro2::TokenStream as TokenStream2; use quote::quote; -pub fn codegen( - app: &App, - analysis: &Analysis, -) -> ( - // mod_app_software_tasks -- free queues, buffers and `${task}Resources` constructors - Vec, - // root_software_tasks -- items that must be placed in the root of the crate: - // - `${task}Locals` structs - // - `${task}Resources` structs - // - `${task}` modules - Vec, - // user_software_tasks -- the `#[task]` functions written by the user - Vec, -) { +pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; @@ -78,5 +65,11 @@ pub fn codegen( root.push(module::codegen(Context::SoftwareTask(name), app, analysis)); } - (mod_app, root, user_tasks) + quote!( + #(#mod_app)* + + #(#root)* + + #(#user_tasks)* + ) }