aboutsummaryrefslogtreecommitdiff
path: root/macros/src/analyze.rs
blob: ec12cfb4dcec5c272b7af05bb96bcaf6d272c116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use core::ops;
use std::collections::{BTreeMap, BTreeSet};

use crate::syntax::{
    analyze::{self, Priority},
    ast::{App, Dispatcher},
};
use syn::Ident;

/// Extend the upstream `Analysis` struct with our field
pub struct Analysis {
    parent: analyze::Analysis,
    pub interrupts_normal: BTreeMap<Priority, (Ident, Dispatcher)>,
    pub interrupts_async: BTreeMap<Priority, (Ident, Dispatcher)>,
}

impl ops::Deref for Analysis {
    type Target = analyze::Analysis;

    fn deref(&self) -> &Self::Target {
        &self.parent
    }
}

// Assign an interrupt to each priority level
pub fn app(analysis: analyze::Analysis, app: &App) -> Analysis {
    let mut available_interrupt = app.args.dispatchers.clone();

    // the set of priorities (each priority only once)
    let priorities = app
        .software_tasks
        .values()
        .filter(|task| !task.is_async)
        .map(|task| task.args.priority)
        .collect::<BTreeSet<_>>();

    let priorities_async = app
        .software_tasks
        .values()
        .filter(|task| task.is_async)
        .map(|task| task.args.priority)
        .collect::<BTreeSet<_>>();

    // map from priorities to interrupts (holding name and attributes)

    let interrupts_normal: BTreeMap<Priority, _> = priorities
        .iter()
        .copied()
        .rev()
        .map(|p| (p, available_interrupt.pop().expect("UNREACHABLE")))
        .collect();

    let interrupts_async: BTreeMap<Priority, _> = priorities_async
        .iter()
        .copied()
        .rev()
        .map(|p| (p, available_interrupt.pop().expect("UNREACHABLE")))
        .collect();

    Analysis {
        parent: analysis,
        interrupts_normal,
        interrupts_async,
    }
}