prjunnamed_pattern/
simple.rs

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use prjunnamed_netlist::{Cell, Const, Net, Trit, Value};

use crate::{DesignDyn, NetOrValue, Pattern};

pub struct PAny;

impl PAny {
    pub fn new() -> PAny {
        PAny
    }
}

impl<T: Clone> Pattern<T> for PAny {
    type Capture = (T,);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        Some((target.clone(),))
    }
}

pub struct PBind<P>(P);

impl<P> PBind<P> {
    pub fn new(pat: P) -> PBind<P> {
        PBind(pat)
    }
}

impl<T: Clone, P: Pattern<T>> Pattern<T> for PBind<P> {
    type Capture = (T, P::Capture);

    #[inline]
    fn execute(&self, design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        self.0.execute(design, target).and_then(|capture| Some((target.clone(), capture)))
    }
}

pub struct PConst;

impl PConst {
    pub fn new() -> PConst {
        PConst
    }
}

impl Pattern<Net> for PConst {
    type Capture = (Trit,);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &Net) -> Option<Self::Capture> {
        Net::as_const(*target).map(|value| (value,))
    }
}

impl Pattern<Value> for PConst {
    type Capture = (Const,);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &Value) -> Option<Self::Capture> {
        Value::as_const(&target).map(|value| (value,))
    }
}

pub struct PZero;

impl PZero {
    pub fn new() -> PZero {
        PZero
    }
}

impl Pattern<u32> for PZero {
    type Capture = ((),);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &u32) -> Option<Self::Capture> {
        if *target == 0 {
            Some(((),))
        } else {
            None
        }
    }
}

impl<T: NetOrValue> Pattern<T> for PZero {
    type Capture = ((),);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        match target.as_const() {
            Some(value) if value.iter().all(|net| net == Trit::Zero) => Some(((),)),
            _ => None,
        }
    }
}

pub struct POnes;

impl POnes {
    pub fn new() -> POnes {
        POnes
    }
}

impl<T: NetOrValue> Pattern<T> for POnes {
    type Capture = ((),);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        match target.as_const() {
            Some(value) if value.is_empty() => None,
            Some(value) if value.iter().all(|net| net == Trit::One) => Some(((),)),
            _ => None,
        }
    }
}

pub struct PUndef;

impl PUndef {
    pub fn new() -> PUndef {
        PUndef
    }
}

impl<T: NetOrValue> Pattern<T> for PUndef {
    type Capture = ((),);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        match target.as_const() {
            Some(value) if value.is_empty() => None,
            Some(value) if value.iter().all(|net| net == Trit::Undef) => Some(((),)),
            _ => None,
        }
    }
}

pub struct PHasX;

impl PHasX {
    pub fn new() -> PHasX {
        PHasX
    }
}

impl<T: NetOrValue> Pattern<T> for PHasX {
    type Capture = ((),);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        match target.as_const() {
            Some(value) if value.has_undef() => Some(((),)),
            _ => None,
        }
    }
}

pub struct PPow2;

impl PPow2 {
    pub fn new() -> PPow2 {
        PPow2
    }
}

impl<T: NetOrValue> Pattern<T> for PPow2 {
    type Capture = (u32,);

    #[inline]
    fn execute(&self, _design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        target.as_const().and_then(|value| value.as_power_of_two().map(|exp| (exp,)))
    }
}

pub type POneHot = PPow2;

pub struct PInput(&'static str);

impl PInput {
    pub fn new(name: &'static str) -> PInput {
        PInput(name)
    }
}

impl<T: NetOrValue> Pattern<T> for PInput {
    type Capture = (T,);

    #[inline]
    fn execute(&self, design: &dyn DesignDyn, target: &T) -> Option<Self::Capture> {
        if let Some(net) = target.iter().next() {
            if let Ok((cell_ref, 0)) = design.find_cell(net) {
                if let Cell::Input(name, _size) = &*cell_ref.get() {
                    if target.as_value() == cell_ref.output() && name == self.0 {
                        return Some((target.clone(),));
                    }
                }
            }
        }
        None
    }
}

pub struct PZExt<P>(P);

impl<P> PZExt<P> {
    pub fn new(pat: P) -> PZExt<P> {
        PZExt(pat)
    }
}

impl<P: Pattern<Value>> Pattern<Value> for PZExt<P> {
    // The amount of extension bits can be found using: [PZExt@y [PAny@a]] => y.len() - a.len()
    type Capture = (Value, P::Capture);

    #[inline]
    fn execute(&self, design: &dyn DesignDyn, target: &Value) -> Option<Self::Capture> {
        let zext_count = target.iter().rev().take_while(|net| *net == Net::ZERO).count();
        self.0.execute(design, &target.slice(..target.len() - zext_count)).map(|capture| (target.clone(), capture))
    }
}

pub struct PSExt<P>(P);

impl<P> PSExt<P> {
    pub fn new(pat: P) -> PSExt<P> {
        PSExt(pat)
    }
}

impl<P: Pattern<Value>> Pattern<Value> for PSExt<P> {
    // The amount of extension bits can be found using: [PZExt@y [PAny@a]] => y.len() - a.len()
    type Capture = (Value, P::Capture);

    #[inline]
    fn execute(&self, design: &dyn DesignDyn, target: &Value) -> Option<Self::Capture> {
        if target.is_empty() {
            return None;
        }
        let sext_count = target.iter().rev().take_while(|net| *net == target.msb()).count() - 1;
        self.0.execute(design, &target.slice(..target.len() - sext_count)).map(|capture| (target.clone(), capture))
    }
}