fayalite 0.2.0

Hardware Description Language embedded in Rust, using FIRRTL's semantics
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
// SPDX-License-Identifier: LGPL-3.0-or-later
// See Notices.txt for copyright information
use crate::{
    array::Array,
    bundle::{Bundle, BundleField},
    expr::Flow,
    intern::{Intern, Interned},
    memory::{DynPortType, MemPort},
    module::{Instance, ModuleIO, TargetName},
    reg::Reg,
    source_location::SourceLocation,
    ty::{CanonicalType, Type},
    wire::Wire,
};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TargetPathBundleField {
    pub name: Interned<str>,
}

impl fmt::Display for TargetPathBundleField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, ".{}", self.name)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TargetPathArrayElement {
    pub index: usize,
}

impl fmt::Display for TargetPathArrayElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}]", self.index)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TargetPathDynArrayElement {}

impl fmt::Display for TargetPathDynArrayElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[<dyn>]")
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TargetPathElement {
    BundleField(TargetPathBundleField),
    ArrayElement(TargetPathArrayElement),
    DynArrayElement(TargetPathDynArrayElement),
}

impl From<TargetPathBundleField> for TargetPathElement {
    fn from(value: TargetPathBundleField) -> Self {
        Self::BundleField(value)
    }
}

impl From<TargetPathArrayElement> for TargetPathElement {
    fn from(value: TargetPathArrayElement) -> Self {
        Self::ArrayElement(value)
    }
}

impl From<TargetPathDynArrayElement> for TargetPathElement {
    fn from(value: TargetPathDynArrayElement) -> Self {
        Self::DynArrayElement(value)
    }
}

impl fmt::Display for TargetPathElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BundleField(v) => v.fmt(f),
            Self::ArrayElement(v) => v.fmt(f),
            Self::DynArrayElement(v) => v.fmt(f),
        }
    }
}

impl TargetPathElement {
    pub fn canonical_ty(&self, parent: Interned<Target>) -> CanonicalType {
        match self {
            &Self::BundleField(TargetPathBundleField { name }) => {
                let parent_ty = Bundle::from_canonical(parent.canonical_ty());
                let field = parent_ty
                    .field_by_name(name)
                    .expect("field name is known to be a valid field of parent type");
                field.ty
            }
            &Self::ArrayElement(TargetPathArrayElement { index }) => {
                let parent_ty = Array::<CanonicalType>::from_canonical(parent.canonical_ty());
                assert!(index < parent_ty.len());
                parent_ty.element()
            }
            Self::DynArrayElement(_) => {
                let parent_ty = Array::<CanonicalType>::from_canonical(parent.canonical_ty());
                parent_ty.element()
            }
        }
    }
    pub fn flow(&self, parent: Interned<Target>) -> Flow {
        match self {
            Self::BundleField(v) => {
                let parent_ty = Bundle::from_canonical(parent.canonical_ty());
                let field = parent_ty
                    .field_by_name(v.name)
                    .expect("field name is known to be a valid field of parent type");
                parent.flow().flip_if(field.flipped)
            }
            Self::ArrayElement(_) => parent.flow(),
            Self::DynArrayElement(_) => parent.flow(),
        }
    }
    pub fn is_static(&self) -> bool {
        match self {
            Self::BundleField(_) | Self::ArrayElement(_) => true,
            Self::DynArrayElement(_) => false,
        }
    }
}

macro_rules! impl_target_base {
    (
        $(#[$enum_meta:meta])*
        $enum_vis:vis enum $TargetBase:ident {
            $(
                #[is = $is_fn:ident]
                #[to = $to_fn:ident]
                $(#[$variant_meta:meta])*
                $Variant:ident($VariantTy:ty),
            )*
        }
    ) => {
        $(#[$enum_meta])*
        $enum_vis enum $TargetBase {
            $(
                $(#[$variant_meta])*
                $Variant($VariantTy),
            )*
        }

        impl fmt::Debug for $TargetBase {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self {
                    $(Self::$Variant(v) => v.fmt(f),)*
                }
            }
        }

        $(
        impl From<$VariantTy> for $TargetBase {
            fn from(value: $VariantTy) -> Self {
                Self::$Variant(value)
            }
        }

        impl From<$VariantTy> for Target {
            fn from(value: $VariantTy) -> Self {
                $TargetBase::$Variant(value).into()
            }
        }
        )*

        impl $TargetBase {
            $(
            $enum_vis fn $is_fn(&self) -> bool {
                self.$to_fn().is_some()
            }
            $enum_vis fn $to_fn(&self) -> Option<&$VariantTy> {
                if let Self::$Variant(retval) = self {
                    Some(retval)
                } else {
                    None
                }
            }
            )*
            $enum_vis fn must_connect_to(&self) -> bool {
                match self {
                    $(Self::$Variant(v) => v.must_connect_to(),)*
                }
            }
            $enum_vis fn flow(&self) -> Flow {
                match self {
                    $(Self::$Variant(v) => v.flow(),)*
                }
            }
            $enum_vis fn source_location(&self) -> SourceLocation {
                match self {
                    $(Self::$Variant(v) => v.source_location(),)*
                }
            }
        }
    };
}

impl_target_base! {
    #[derive(Clone, PartialEq, Eq, Hash)]
    pub enum TargetBase {
        #[is = is_module_io]
        #[to = module_io]
        ModuleIO(ModuleIO<CanonicalType>),
        #[is = is_mem_port]
        #[to = mem_port]
        MemPort(MemPort<DynPortType>),
        #[is = is_reg]
        #[to = reg]
        Reg(Reg<CanonicalType>),
        #[is = is_wire]
        #[to = wire]
        Wire(Wire<CanonicalType>),
        #[is = is_instance]
        #[to = instance]
        Instance(Instance<Bundle>),
    }
}

impl fmt::Display for TargetBase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.target_name())
    }
}

impl TargetBase {
    pub fn target_name(&self) -> TargetName {
        match self {
            TargetBase::ModuleIO(v) => TargetName(v.scoped_name(), None),
            TargetBase::MemPort(v) => TargetName(v.mem_name(), Some(v.port_name())),
            TargetBase::Reg(v) => TargetName(v.scoped_name(), None),
            TargetBase::Wire(v) => TargetName(v.scoped_name(), None),
            TargetBase::Instance(v) => TargetName(v.scoped_name(), None),
        }
    }
    pub fn canonical_ty(&self) -> CanonicalType {
        match self {
            TargetBase::ModuleIO(v) => v.ty(),
            TargetBase::MemPort(v) => v.ty().canonical(),
            TargetBase::Reg(v) => v.ty(),
            TargetBase::Wire(v) => v.ty(),
            TargetBase::Instance(v) => v.ty().canonical(),
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct TargetChild {
    parent: Interned<Target>,
    path_element: Interned<TargetPathElement>,
    canonical_ty: CanonicalType,
    flow: Flow,
}

impl fmt::Debug for TargetChild {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            parent,
            path_element,
            canonical_ty: _,
            flow: _,
        } = self;
        parent.fmt(f)?;
        fmt::Display::fmt(path_element, f)
    }
}

impl fmt::Display for TargetChild {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            parent,
            path_element,
            canonical_ty: _,
            flow: _,
        } = self;
        parent.fmt(f)?;
        path_element.fmt(f)
    }
}

impl TargetChild {
    pub fn new(parent: Interned<Target>, path_element: Interned<TargetPathElement>) -> Self {
        Self {
            parent,
            path_element,
            canonical_ty: path_element.canonical_ty(parent),
            flow: path_element.flow(parent),
        }
    }
    pub fn parent(self) -> Interned<Target> {
        self.parent
    }
    pub fn path_element(self) -> Interned<TargetPathElement> {
        self.path_element
    }
    pub fn canonical_ty(self) -> CanonicalType {
        self.canonical_ty
    }
    pub fn flow(self) -> Flow {
        self.flow
    }
    pub fn bundle_field(self) -> Option<BundleField> {
        if let TargetPathElement::BundleField(TargetPathBundleField { name }) = *self.path_element {
            let parent_ty = Bundle::from_canonical(self.parent.canonical_ty());
            Some(
                parent_ty
                    .field_by_name(name)
                    .expect("field name known to be a valid field of parent"),
            )
        } else {
            None
        }
    }
}

#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Target {
    Base(Interned<TargetBase>),
    Child(TargetChild),
}

impl From<TargetBase> for Target {
    fn from(value: TargetBase) -> Self {
        Self::Base(Intern::intern_sized(value))
    }
}

impl From<TargetChild> for Target {
    fn from(value: TargetChild) -> Self {
        Self::Child(value)
    }
}

impl From<Interned<TargetBase>> for Target {
    fn from(value: Interned<TargetBase>) -> Self {
        Self::Base(value)
    }
}

impl Target {
    pub fn base(&self) -> Interned<TargetBase> {
        let mut target = self;
        loop {
            match target {
                Self::Base(v) => break *v,
                Self::Child(v) => target = &v.parent,
            }
        }
    }
    pub fn child(&self) -> Option<TargetChild> {
        match *self {
            Target::Base(_) => None,
            Target::Child(v) => Some(v),
        }
    }
    pub fn is_static(&self) -> bool {
        let mut target = self;
        loop {
            match target {
                Self::Base(_) => return true,
                Self::Child(v) if !v.path_element().is_static() => return false,
                Self::Child(v) => target = &v.parent,
            }
        }
    }
    #[must_use]
    pub fn join(&self, path_element: Interned<TargetPathElement>) -> Self {
        TargetChild::new(self.intern(), path_element).into()
    }
    pub fn flow(&self) -> Flow {
        match self {
            Self::Base(v) => v.flow(),
            Self::Child(v) => v.flow(),
        }
    }
    pub fn canonical_ty(&self) -> CanonicalType {
        match self {
            Target::Base(v) => v.canonical_ty(),
            Target::Child(v) => v.canonical_ty(),
        }
    }
}

impl fmt::Display for Target {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Base(v) => v.fmt(f),
            Self::Child(v) => v.fmt(f),
        }
    }
}

impl fmt::Debug for Target {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Base(v) => v.fmt(f),
            Self::Child(v) => v.fmt(f),
        }
    }
}

pub trait GetTarget {
    fn target(&self) -> Option<Interned<Target>>;
}

impl GetTarget for bool {
    fn target(&self) -> Option<Interned<Target>> {
        None
    }
}

impl<T: ?Sized + GetTarget + Send + Sync + 'static> GetTarget for Interned<T> {
    fn target(&self) -> Option<Interned<Target>> {
        T::target(self)
    }
}

impl<T: ?Sized + GetTarget> GetTarget for &'_ T {
    fn target(&self) -> Option<Interned<Target>> {
        T::target(self)
    }
}

impl<T: ?Sized + GetTarget> GetTarget for &'_ mut T {
    fn target(&self) -> Option<Interned<Target>> {
        T::target(self)
    }
}

impl<T: ?Sized + GetTarget> GetTarget for Box<T> {
    fn target(&self) -> Option<Interned<Target>> {
        T::target(self)
    }
}