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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Copyright 2021 Fluence Labs Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use air_parser::AirPos;

use std::collections::HashMap;

/// Intended to track a number of executed instruction of each type. For instructions that
/// have a corresponding state in data, it tracks number of executed instructions on
/// current peer (executed) and overall number (seen) of met instructions of such type.
#[derive(Default, Debug, PartialEq, Eq)]
pub struct InstructionTracker {
    pub ap: ApTracker,
    pub call: CallTracker,
    pub fold: FoldTracker,
    pub match_count: u32,
    pub mismatch_count: u32,
    pub new_tracker: NewTracker,
    pub next_count: u32,
    pub null_count: u32,
    pub par: ParTracker,
    pub seq_count: u32,
    pub xor_count: u32,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct ApTracker {
    pub seen_count: u32,
    pub executed_count: u32,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct CallTracker {
    pub seen_count: u32,
    pub executed_count: u32,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct FoldTracker {
    pub seen_scalar_count: u32,
    pub seen_stream_count: u32,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct ParTracker {
    pub seen_count: u32,
    pub executed_count: u32,
}

#[derive(Default, Debug, PartialEq, Eq)]
pub struct NewTracker {
    /// Mapping from a new instruction position in a script
    /// to a number of their execution. This is needed to
    /// support private stream generation mappings.
    pub executed_count: HashMap<AirPos, u32>,
}

impl InstructionTracker {
    pub fn meet_ap(&mut self) {
        self.ap.seen_count += 1;
    }

    pub fn meet_executed_ap(&mut self) {
        self.ap.executed_count += 1;
    }

    pub fn meet_call(&mut self) {
        self.call.seen_count += 1;
    }

    pub fn meet_executed_call(&mut self) {
        self.call.executed_count += 1;
    }

    pub fn meet_fold_scalar(&mut self) {
        self.fold.seen_scalar_count += 1;
    }

    pub fn meet_fold_stream(&mut self) {
        self.fold.seen_stream_count += 1;
    }

    pub fn meet_match(&mut self) {
        self.match_count += 1;
    }

    pub fn meet_mismatch(&mut self) {
        self.mismatch_count += 1;
    }

    pub fn meet_next(&mut self) {
        self.next_count += 1;
    }

    pub fn meet_null(&mut self) {
        self.null_count += 1;
    }

    pub fn meet_par(&mut self) {
        self.par.seen_count += 1;
    }

    pub fn meet_executed_par(&mut self) {
        self.par.executed_count += 1;
    }

    pub fn meet_seq(&mut self) {
        self.seq_count += 1;
    }

    pub fn meet_xor(&mut self) {
        self.xor_count += 1;
    }

    pub fn meet_new(&mut self, position: AirPos) {
        use std::collections::hash_map::Entry::{Occupied, Vacant};

        match self.new_tracker.executed_count.entry(position) {
            Occupied(mut entry) => *entry.get_mut() += 1,
            Vacant(entry) => {
                entry.insert(1);
            }
        };
    }
}

impl NewTracker {
    pub fn get_iteration(&self, position: AirPos) -> u32 {
        self.executed_count
            .get(&position)
            .copied()
            .unwrap_or_default()
    }
}