-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathData.scala
More file actions
1311 lines (1177 loc) · 54.4 KB
/
Data.scala
File metadata and controls
1311 lines (1177 loc) · 54.4 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
package chisel3
import chisel3.experimental.dataview.reify
import chisel3.experimental.{requireIsChiselType, requireIsHardware, Analog, BaseModule}
import chisel3.experimental.{prefix, SourceInfo, UnlocatableSourceInfo}
import chisel3.experimental.dataview.{reifyIdentityView, reifySingleTarget, DataViewable}
import chisel3.internal.Builder.pushCommand
import chisel3.internal._
import chisel3.internal.binding._
import chisel3.internal.firrtl.ir._
import chisel3.properties.Property
import chisel3.reflect.DataMirror
import chisel3.util.simpleClassName
import scala.reflect.ClassTag
import scala.util.Try
import scala.util.control.NonFatal
/** User-specified directions.
*/
sealed abstract class SpecifiedDirection(private[chisel3] val value: Byte)
object SpecifiedDirection {
/** Default user direction, also meaning 'not-flipped'
*/
case object Unspecified extends SpecifiedDirection(0)
/** Node and its children are forced as output
*/
case object Output extends SpecifiedDirection(1)
/** Node and its children are forced as inputs
*/
case object Input extends SpecifiedDirection(2)
/** Mainly for containers, children are flipped.
*/
case object Flip extends SpecifiedDirection(3)
private[chisel3] def fromByte(b: Byte): SpecifiedDirection = b match {
case Unspecified.value => Unspecified
case Output.value => Output
case Input.value => Input
case Flip.value => Flip
case _ => throw new RuntimeException(s"Unexpected SpecifiedDirection value $b")
}
def flip(dir: SpecifiedDirection): SpecifiedDirection = dir match {
case Unspecified => Flip
case Flip => Unspecified
case Output => Input
case Input => Output
}
/** Returns the effective SpecifiedDirection of this node given the parent's effective SpecifiedDirection
* and the user-specified SpecifiedDirection of this node.
*/
def fromParent(parentDirection: SpecifiedDirection, thisDirection: SpecifiedDirection): SpecifiedDirection =
(parentDirection, thisDirection) match {
case (SpecifiedDirection.Output, _) => SpecifiedDirection.Output
case (SpecifiedDirection.Input, _) => SpecifiedDirection.Input
case (SpecifiedDirection.Unspecified, thisDirection) => thisDirection
case (SpecifiedDirection.Flip, thisDirection) => SpecifiedDirection.flip(thisDirection)
}
private[chisel3] def specifiedDirection[T <: Data](
source: => T
)(dir: T => SpecifiedDirection): T = {
val prevId = Builder.idGen.value
val data = source // evaluate source once (passed by name)
requireIsChiselType(data)
val out = if (!data.mustClone(prevId)) data else data.cloneTypeFull.asInstanceOf[T]
out.specifiedDirection = dir(data) // Must use original data, specified direction of clone is cleared
out
}
}
/** Resolved directions for both leaf and container nodes, only visible after
* a node is bound (since higher-level specifications like Input and Output
* can override directions).
*/
sealed abstract class ActualDirection(private[chisel3] val value: Byte)
object ActualDirection {
// 0 is reserved for unset, no case object added because that would be an unnecessary API breakage.
private[chisel3] val Unset: Byte = 0
/** The object does not exist / is empty and hence has no direction
*/
case object Empty extends ActualDirection(1)
/** Undirectioned, struct-like
*/
case object Unspecified extends ActualDirection(2)
/** Output element, or container with all outputs (even if forced)
*/
case object Output extends ActualDirection(3)
/** Input element, or container with all inputs (even if forced)
*/
case object Input extends ActualDirection(4)
// BidirectionalDirection is effectively an extension of ActualDirection, see its use in Bidirectional below.
// Thus, the numbering here is a continuation of the numbering in ActualDirection.
// Bidirectional.Default and Bidirectional.Flipped wrap these objects.
sealed abstract class BidirectionalDirection(private[chisel3] val value: Byte)
case object Default extends BidirectionalDirection(5)
case object Flipped extends BidirectionalDirection(6)
// This constructor has 2 arguments (which need to be in sync) only to distinguish it from the other constructor
// Once that one is removed, delete _value
case class Bidirectional private[chisel3] (dir: BidirectionalDirection, _value: Byte)
extends ActualDirection(_value) {
@deprecated("Use companion object factory apply method", "Chisel 6.5")
def this(dir: BidirectionalDirection) = this(dir, dir.value)
private[chisel3] def copy(dir: BidirectionalDirection = this.dir, _value: Byte = this._value) =
new Bidirectional(dir, _value)
}
object Bidirectional {
val Default = new Bidirectional(ActualDirection.Default, ActualDirection.Default.value)
val Flipped = new Bidirectional(ActualDirection.Flipped, ActualDirection.Flipped.value)
def apply(dir: BidirectionalDirection): ActualDirection = dir match {
case ActualDirection.Default => Default
case ActualDirection.Flipped => Flipped
}
@deprecated("Match on Bidirectional.Default and Bidirectional.Flipped directly instead", "Chisel 6.5")
def unapply(dir: Bidirectional): Option[BidirectionalDirection] = Some(dir.dir)
private[chisel3] def apply(dir: BidirectionalDirection, _value: Byte) = new Bidirectional(dir, _value)
}
private[chisel3] def fromByte(b: Byte): ActualDirection = b match {
case Empty.value => Empty
case Unspecified.value => Unspecified
case Output.value => Output
case Input.value => Input
case Bidirectional.Default.value => Bidirectional.Default
case Bidirectional.Flipped.value => Bidirectional.Flipped
case _ => throwException(s"Unexpected ActualDirection value $b")
}
/** Converts a `SpecifiedDirection` to an `ActualDirection`
*
* Implements the Chisel convention that Flip is Input and unspecified is Output.
*/
def fromSpecified(direction: SpecifiedDirection): ActualDirection = direction match {
case SpecifiedDirection.Output | SpecifiedDirection.Unspecified => ActualDirection.Output
case SpecifiedDirection.Input | SpecifiedDirection.Flip => ActualDirection.Input
}
/** Determine the actual binding of a container given directions of its children.
* Returns None in the case of mixed specified / unspecified directionality.
*/
def fromChildren(
childDirections: Set[ActualDirection],
containerDirection: SpecifiedDirection
): Option[ActualDirection] = {
if (childDirections == Set()) { // Sadly, Scala can't do set matching
ActualDirection.fromSpecified(containerDirection) match {
case ActualDirection.Unspecified => Some(ActualDirection.Empty) // empty direction if relative / no direction
case dir => Some(dir) // use assigned direction if specified
}
} else if (childDirections == Set(ActualDirection.Unspecified)) {
Some(ActualDirection.Unspecified)
} else if (childDirections == Set(ActualDirection.Input)) {
Some(ActualDirection.Input)
} else if (childDirections == Set(ActualDirection.Output)) {
Some(ActualDirection.Output)
} else if (
childDirections.subsetOf(
Set(
ActualDirection.Output,
ActualDirection.Input,
ActualDirection.Bidirectional(ActualDirection.Default),
ActualDirection.Bidirectional(ActualDirection.Flipped)
)
)
) {
containerDirection match {
case SpecifiedDirection.Unspecified => Some(ActualDirection.Bidirectional(ActualDirection.Default))
case SpecifiedDirection.Flip => Some(ActualDirection.Bidirectional(ActualDirection.Flipped))
case _ => throw new RuntimeException("Unexpected forced Input / Output")
}
} else {
None
}
}
}
/** Creates a clone of the super-type of the input elements. Super-type is defined as:
* - for Bits type of the same class: the cloned type of the largest width
* - Bools are treated as UInts
* - For other types of the same class are are the same: clone of any of the elements
* - Otherwise: fail
*/
private[chisel3] object cloneSupertype {
def apply[T <: Data](
elts: Seq[T],
createdType: String
)(
implicit sourceInfo: SourceInfo
): T = {
require(!elts.isEmpty, s"can't create $createdType with no inputs")
val filteredElts = elts.filter(_ != DontCare)
require(!filteredElts.isEmpty, s"can't create $createdType with only DontCare inputs")
if (filteredElts.head.isInstanceOf[Bits]) {
val model: T = filteredElts.reduce { (elt1: T, elt2: T) =>
((elt1, elt2) match {
case (elt1: Bool, elt2: Bool) => elt1
case (elt1: Bool, elt2: UInt) => elt2 // TODO: what happens with zero width UInts?
case (elt1: UInt, elt2: Bool) => elt1 // TODO: what happens with zero width UInts?
case (elt1: UInt, elt2: UInt) =>
// TODO: perhaps redefine Widths to allow >= op?
if (elt1.width == (elt1.width.max(elt2.width))) elt1 else elt2
case (elt1: SInt, elt2: SInt) => if (elt1.width == (elt1.width.max(elt2.width))) elt1 else elt2
case (elt1, elt2) =>
throw new AssertionError(
s"can't create $createdType with heterogeneous types ${elt1.getClass} and ${elt2.getClass}"
)
}).asInstanceOf[T]
}
model.cloneTypeFull
} else {
for (elt <- filteredElts.tail) {
require(
elt.getClass == filteredElts.head.getClass,
s"can't create $createdType with heterogeneous types ${filteredElts.head.getClass} and ${elt.getClass}"
)
val mismatch =
elt.findFirstTypeMismatch(filteredElts.head, strictTypes = true, strictWidths = true, strictProbeInfo = true)
require(
mismatch.isEmpty,
s"can't create $createdType with non-equivalent types _${mismatch.get}"
)
}
filteredElts.head.cloneTypeFull
}
}
}
// Returns pairs of all fields, element-level and containers, in a Record and their path names
private[chisel3] object getRecursiveFields {
def noPath(data: Data): Seq[Data] = lazilyNoPath(data).toVector
def lazilyNoPath(data: Data): Iterable[Data] = DataMirror.collectMembers(data) { case x => x }
def apply(data: Data, path: String): Seq[(Data, String)] = lazily(data, path).toVector
def lazily(data: Data, path: String): Iterable[(Data, String)] = DataMirror.collectMembersAndPaths(data, path) {
case x => x
}
}
// Returns pairs of corresponding fields between two Records of the same type
// TODO it seems wrong that Elements are checked for typeEquivalence in Bundle and Vec lit creation
private[chisel3] object getMatchedFields {
def apply(x: Data, y: Data): Seq[(Data, Data)] = (x, y) match {
case (x: Element, y: Element) =>
x.requireTypeEquivalent(y)
Seq(x -> y)
case (_, _) if DataMirror.hasProbeTypeModifier(x) || DataMirror.hasProbeTypeModifier(y) => {
x.requireTypeEquivalent(y)
Seq(x -> y)
}
case (x: Record, y: Record) =>
(x._elements
.zip(y._elements))
.map { case ((xName, xElt), (yName, yElt)) =>
require(
xName == yName,
s"$xName != $yName, ${x._elements}, ${y._elements}, $x, $y"
) // assume fields returned in same, deterministic order
getMatchedFields(xElt, yElt)
}
.fold(Seq(x -> y)) {
_ ++ _
}
case (x: Vec[_], y: Vec[_]) =>
(x.elementsIterator
.zip(y.elementsIterator))
.map { case (xElt, yElt) =>
getMatchedFields(xElt, yElt)
}
.fold(Seq(x -> y)) {
_ ++ _
}
}
}
/** Returns the chisel type of a hardware object, allowing other hardware to be constructed from it.
*/
object chiselTypeOf {
def apply[T <: Data](target: T): T = {
requireIsHardware(target)
target.cloneTypeFull.asInstanceOf[T]
}
}
/**
* Input, Output, and Flipped are used to define the directions of Module IOs.
*
* Note that they currently clone their source argument, including its bindings.
*
* Thus, an error will be thrown if these are used on bound Data
*/
object Input {
def apply[T <: Data](source: => T): T = {
SpecifiedDirection.specifiedDirection(source)(_ => SpecifiedDirection.Input)
}
}
object Output {
def apply[T <: Data](source: => T): T = {
SpecifiedDirection.specifiedDirection(source)(_ => SpecifiedDirection.Output)
}
}
object Flipped {
def apply[T <: Data](source: => T): T = {
SpecifiedDirection.specifiedDirection(source)(x => SpecifiedDirection.flip(x.specifiedDirection))
}
}
/** This forms the root of the type system for wire data types. The data value
* must be representable as some number (need not be known at Chisel compile
* time) of bits, and must have methods to pack / unpack structured data to /
* from bits.
*
* @groupdesc Connect Utilities for connecting hardware components
* @define coll data
*/
abstract class Data extends HasId with NamedComponent with DataIntf {
import Data.ProbeInfo
// This is a bad API that punches through object boundaries.
private[chisel3] def flatten: IndexedSeq[Element] = {
this match {
case elt: Aggregate => elt.elementsIterator.toIndexedSeq.flatMap { _.flatten }
case elt: Element => IndexedSeq(elt)
case elt => throwException(s"Cannot flatten type ${elt.getClass}")
}
}
// Whether this node'e element(s) possess a specified flipped direction, ignoring coercion via Input/Output
private[chisel3] def containsAFlipped: Boolean = false
// Must clone a Data if any of the following are true:
// * It has a binding
// * Its id is older than prevId (not "freshly created")
// * It is a Bundle or Record that contains a member older than prevId
private[chisel3] def mustClone(prevId: Long): Boolean = {
this.hasBinding || this._minId <= prevId
}
/** The minimum (aka "oldest") id that is part of this Data
*
* @note This is usually just _id except for some Records and Bundles
*/
private[chisel3] def _minId: Long = this._id
override def autoSeed(name: String): this.type = {
topBindingOpt match {
// Ports are special in that the autoSeed will keep the first name, not the last name
case Some(PortBinding(m)) if hasSeed && Builder.currentModule.contains(m) => this
case _ => super.autoSeed(name)
}
}
// probeInfo only exists if this is a probe type
private var _probeInfoVar: ProbeInfo = null
private[chisel3] def probeInfo: Option[ProbeInfo] = Option(_probeInfoVar)
private[chisel3] def probeInfo_=(probeInfo: Option[ProbeInfo]) = _probeInfoVar = probeInfo.getOrElse(null)
// If this Data is constant, it must hold a constant value
private var _isConst: Boolean = false
private[chisel3] def isConst: Boolean = _isConst
private[chisel3] def isConst_=(isConst: Boolean) = _isConst = isConst
// Both _direction and _resolvedUserDirection are saved versions of computed variables (for
// efficiency, avoid expensive recomputation of frequent operations).
// Both are only valid after binding is set.
// User-specified direction, local at this node only.
// Note that the actual direction of this node can differ from child and parent specifiedDirection.
private var _specifiedDirection: Byte = SpecifiedDirection.Unspecified.value
private[chisel3] def specifiedDirection: SpecifiedDirection = SpecifiedDirection.fromByte(_specifiedDirection)
private[chisel3] def specifiedDirection_=(direction: SpecifiedDirection) = {
_specifiedDirection = direction.value
}
// Direction of this node, accounting for parents (force Input / Output) and children.
private var _directionVar: Byte = ActualDirection.Unset
private def _direction: Option[ActualDirection] =
Option.when(_directionVar != ActualDirection.Unset)(ActualDirection.fromByte(_directionVar))
private[chisel3] def direction: ActualDirection = _direction.get
private[chisel3] def direction_=(actualDirection: ActualDirection): Unit = {
if (_direction.isDefined) {
throw RebindingException(s"Attempted reassignment of resolved direction to $this")
}
_directionVar = actualDirection.value
}
/** This overwrites a relative SpecifiedDirection with an explicit one, and is used to implement
* the compatibility layer where, at the elements, Flip is Input and unspecified is Output.
* DO NOT USE OUTSIDE THIS PURPOSE. THIS OPERATION IS DANGEROUS!
*/
private[chisel3] def _assignCompatibilityExplicitDirection: Unit = {
(this, specifiedDirection) match {
case (_: Analog, _) => // nothing to do
case (_, SpecifiedDirection.Unspecified) => specifiedDirection = SpecifiedDirection.Output
case (_, SpecifiedDirection.Flip) => specifiedDirection = SpecifiedDirection.Input
case (_, SpecifiedDirection.Input | SpecifiedDirection.Output) => // nothing to do
}
}
// Binding stores information about this node's position in the hardware graph.
// This information is supplemental (more than is necessary to generate FIRRTL) and is used to
// perform checks in Chisel, where more informative error messages are possible.
private var _bindingVar: Binding = null // using nullable var for better memory usage
private def _binding: Option[Binding] = Option(_bindingVar)
// Only valid after node is bound (synthesizable), crashes otherwise
protected[chisel3] def binding: Option[Binding] = _binding
protected def binding_=(target: Binding): Unit = {
if (_binding.isDefined) {
throw RebindingException(s"Attempted reassignment of binding to $this, from: ${target}")
}
_bindingVar = target
}
private[chisel3] def hasBinding: Boolean = _binding.isDefined
// Similar to topBindingOpt except it explicitly excludes SampleElements which are bound but not
// hardware
private[chisel3] final def isSynthesizable: Boolean = _binding.map {
case ChildBinding(parent) => parent.isSynthesizable
case _: TopBinding => true
case (_: SampleElementBinding[_] | _: MemTypeBinding[_] | _: FirrtlMemTypeBinding) => false
}.getOrElse(false)
private[chisel3] def topBindingOpt: Option[TopBinding] = _binding.flatMap {
case ChildBinding(parent) => parent.topBindingOpt
case bindingVal: TopBinding => Some(bindingVal)
case SampleElementBinding(parent) => parent.topBindingOpt
case (_: MemTypeBinding[_] | _: FirrtlMemTypeBinding) => None
}
private[chisel3] def topBinding: TopBinding = topBindingOpt.get
/** Binds this node to the hardware graph.
* parentDirection is the direction of the parent node, or Unspecified (default) if the target
* node is the top-level.
* binding and direction are valid after this call completes.
*/
private[chisel3] def bind(target: Binding, parentDirection: SpecifiedDirection = SpecifiedDirection.Unspecified): Unit
/** Adds this `Data` to its parents _ids if it should be added */
private[chisel3] def maybeAddToParentIds(target: Binding): Unit = {
// ConstrainedBinding means the thing actually corresponds to a Module, no need to add to _ids otherwise
target match {
case c: SecretPortBinding => // secret ports are handled differently, parent's don't need to know about that
case c: ConstrainedBinding => _parent.foreach(_.addId(this))
case _ =>
}
}
// Specializes the .toString method of a [[Data]] for conditions such as
// DataView, Probe modifiers, a DontCare, and whether it is bound or a pure chisel type
private[chisel3] def stringAccessor(chiselType: String): String = {
// Add probe and layer color (if they exist) to the returned String
val chiselTypeWithModifier =
probeInfo match {
case None => chiselType
case Some(ProbeInfo(writeable, layer)) =>
val layerString = layer.map(x => s"[${x.fullName}]").getOrElse("")
(if (writeable) "RWProbe" else "Probe") + s"$layerString<$chiselType>"
}
// Trace views to give better error messages
// Reifying involves checking against ViewParent which requires being in a Builder context
// Since we're just printing a String, suppress such errors and use this object
// Use a guard to prevent infinite recursion when reifySingleTarget triggers toString
val thiz = if (Data.avoidReifyingViews.get()) {
this
} else {
Data.avoidReifyingViews.set(true)
try {
Try(reifySingleTarget(this)).toOption.flatten.getOrElse(this)
} finally {
Data.avoidReifyingViews.set(false)
}
}
thiz.topBindingOpt match {
case None => chiselTypeWithModifier
// Handle DontCares specially as they are "literal-like" but not actually literals
case Some(DontCareBinding()) => s"$chiselType(DontCare)"
case Some(topBinding) =>
val binding: String = thiz._bindingToString(topBinding)
val name = thiz.earlyName
val mod = thiz.parentNameOpt.map(_ + ".").getOrElse("")
s"$mod$name: $binding[$chiselTypeWithModifier]"
}
}
// User-friendly representation of the binding as a helper function for toString.
// Provides a unhelpful fallback for literals, which should have custom rendering per
// Data-subtype.
private[chisel3] def _bindingToString(topBindingOpt: TopBinding): String =
topBindingOpt match {
case OpBinding(_, _) => "OpResult"
case MemoryPortBinding(_, _) => "MemPort"
case PortBinding(_) => "IO"
case SecretPortBinding(_) => "IO"
case RegBinding(_, _) => "Reg"
case WireBinding(_, _) => "Wire"
case DontCareBinding() => "(DontCare)"
case ElementLitBinding(litArg) => "(unhandled literal)"
case BundleLitBinding(litMap) => "(unhandled bundle literal)"
case VecLitBinding(litMap) => "(unhandled vec literal)"
case DynamicIndexBinding(vec) => _bindingToString(vec.topBinding)
case _ => ""
}
private[chisel3] def earlyName: String = Arg.earlyLocalName(this)
// Only used in error messages, this is not allowed to fail
private[chisel3] def parentNameOpt: Option[String] = try {
this._parent.map(_.name)
} catch {
case NonFatal(_) => Some("<unknown>")
}
/** Useful information for recoverable errors that will allow the error to deduplicate */
private[chisel3] def _localErrorContext: String = {
if (this.binding.exists(_.isInstanceOf[ChildBinding])) {
val n = Arg.earlyLocalName(this, includeRoot = false)
s"Field '$n' of type ${this.typeName}"
} else {
this.typeName
}
}
// Return ALL elements at root of this type.
// Contasts with flatten, which returns just Bits
// TODO: refactor away this, this is outside the scope of Data
private[chisel3] def allElements: Seq[Element]
private[chisel3] def badConnect(that: Data)(implicit sourceInfo: SourceInfo): Unit =
throwException(s"cannot connect ${this} and ${that}")
private[chisel3] def connect(
that: Data
)(
implicit sourceInfo: SourceInfo
): Unit = {
requireIsHardware(this, "data to be connected")
requireIsHardware(that, "data to be connected")
this.topBinding match {
case _: ReadOnlyBinding => throwException(s"Cannot reassign to read-only $this")
case _ => // fine
}
try {
MonoConnect.connect(sourceInfo, this, that, Builder.referenceUserContainer)
} catch {
case MonoConnectException(message) =>
throwException(
s"Connection between sink ($this) and source ($that) failed @: $message"
)
}
}
private[chisel3] def bulkConnect(
that: Data
)(
implicit sourceInfo: SourceInfo
): Unit = {
requireIsHardware(this, s"data to be bulk-connected")
requireIsHardware(that, s"data to be bulk-connected")
(this.topBinding, that.topBinding) match {
case (_: ReadOnlyBinding, _: ReadOnlyBinding) => throwException(s"Both $this and $that are read-only")
// DontCare cannot be a sink (LHS)
case (_: DontCareBinding, _) => throw BiConnect.DontCareCantBeSink
case _ => // fine
}
try {
BiConnect.connect(sourceInfo, this, that, Builder.referenceUserModule)
} catch {
case BiConnectException(message) =>
throwException(
s"Connection between left ($this) and source ($that) failed @$message"
)
}
}
/** Whether this Data has the same model ("data type") as that Data.
* Data subtypes should overload this with checks against their own type.
* @param that the Data to check for type equivalence against.
* @param strictProbeInfo whether probe info (including its RW-ness and Color) must match
*/
private[chisel3] final def typeEquivalent(
that: Data,
strictProbeInfo: Boolean = true
): Boolean =
findFirstTypeMismatch(that, strictTypes = true, strictWidths = true, strictProbeInfo = strictProbeInfo).isEmpty
/** Find and report any type mismatches
*
* @param that Data being compared to this
* @param strictTypes Does class of Bundles or Records need to match? Inverse of "structural".
* @param strictWidths do widths need to match?
* @param strictProbeInfo does probe info need to match (includes RW and Color)
* @return None if types are equivalent, Some String reporting the first mismatch if not
*/
private[chisel3] final def findFirstTypeMismatch(
that: Data,
strictTypes: Boolean,
strictWidths: Boolean,
strictProbeInfo: Boolean
): Option[String] = {
def checkProbeInfo(left: Data, right: Data): Option[String] =
Option.when(strictProbeInfo && (left.probeInfo != right.probeInfo)) {
def probeInfoStr(info: Option[ProbeInfo]) = info.map { info =>
s"Some(writeable=${info.writable}, color=${info.color})"
}.getOrElse("None")
s": Left ($left with probeInfo: ${probeInfoStr(left.probeInfo)}) and Right ($right with probeInfo: ${probeInfoStr(right.probeInfo)}) have different probeInfo."
}
def rec(left: Data, right: Data): Option[String] =
checkProbeInfo(left, right).orElse {
(left, right) match {
// Careful, EnumTypes are Element and if we don't implement this, then they are all always equal
case (e1: EnumType, e2: EnumType) =>
// TODO, should we implement a form of structural equality for enums?
if (e1.factory == e2.factory) None
else Some(s": Left ($e1) and Right ($e2) have different types.")
// Properties should be considered equal when getPropertyType is equal, not when getClass is equal.
case (p1: Property[_], p2: Property[_]) =>
if (p1.getPropertyType != p2.getPropertyType) {
Some(s": Left ($p1) and Right ($p2) have different types")
} else {
None
}
case (e1: Element, e2: Element) if e1.getClass == e2.getClass =>
if (strictWidths && e1.width != e2.width) {
Some(s": Left ($e1) and Right ($e2) have different widths.")
} else {
None
}
case (r1: Record, r2: Record) if !strictTypes || r1.getClass == r2.getClass =>
val (larger, smaller, msg) =
if (r1._elements.size >= r2._elements.size) (r1, r2, "Left") else (r2, r1, "Right")
larger._elements.flatMap { case (name, data) =>
val recurse = smaller._elements.get(name) match {
case None => Some(s": Dangling field on $msg")
case Some(data2) => rec(data, data2)
}
recurse.map("." + name + _)
}.headOption
case (v1: Vec[_], v2: Vec[_]) =>
if (v1.size != v2.size) {
Some(s": Left (size ${v1.size}) and Right (size ${v2.size}) have different lengths.")
} else {
val recurse = rec(v1.sample_element, v2.sample_element)
recurse.map("[_]" + _)
}
case _ => Some(s": Left ($left) and Right ($right) have different types.")
}
}
rec(this, that)
}
/** Require that two things are type equivalent, and if they are not, print a helpful error message as
* to why not.
*
* @param that the Data to compare to for type equivalence
* @param message if they are not type equivalent, contextual message to add to the exception thrown
*/
private[chisel3] def requireTypeEquivalent(that: Data, message: String = ""): Unit = {
require(
this.typeEquivalent(that), {
val reason = this
.findFirstTypeMismatch(that, strictTypes = true, strictWidths = true, strictProbeInfo = true)
.map(s => s"\nbecause $s")
.getOrElse("")
s"$message$this is not typeEquivalent to $that$reason"
}
)
}
private[chisel3] def isVisible: Boolean = isVisibleFromModule && visibleFromBlock.isEmpty
private[chisel3] def isVisibleFromModule: Boolean = {
val topBindingOpt = this.topBindingOpt // Only call the function once
val mod = topBindingOpt.flatMap(_.location)
topBindingOpt match {
case Some(tb: TopBinding) if (mod == Builder.currentModule) => true
case Some(pb: PortBinding)
if mod.flatMap(Builder.retrieveParent(_, Builder.currentModule.get)) == Builder.currentModule =>
true
case Some(ViewBinding(target, _)) => target.isVisibleFromModule
case Some(AggregateViewBinding(mapping, _)) => mapping.values.forall(_.isVisibleFromModule)
case Some(DynamicIndexBinding(vec)) => vec.isVisibleFromModule // Use underlying Vec visibility for dynamic index
case Some(pb: SecretPortBinding) => true // Ignore secret to not require visibility
case Some(_: UnconstrainedBinding) => true
case _ => false
}
}
private[chisel3] def visibleFromBlock: Option[SourceInfo] = MonoConnect.checkBlockVisibility(this)
private[chisel3] def requireVisible()(implicit info: SourceInfo): Unit = {
this.checkVisible.foreach(err => Builder.error(err))
}
// Some is an error message, None means no error
private[chisel3] def checkVisible(implicit info: SourceInfo): Option[String] = {
if (!isVisibleFromModule) {
Some(s"operand '$this' is not visible from the current module ${Builder.currentModule.get.name}")
} else {
visibleFromBlock.map(MonoConnect.escapedScopeErrorMsg(this, _))
}
}
// Internal API: returns a ref that can be assigned to, if consistent with the binding
private[chisel3] def lref(implicit info: SourceInfo): Node = {
requireIsHardware(this)
requireVisible()
topBindingOpt match {
case Some(binding: ReadOnlyBinding) =>
throwException(s"internal error: attempted to generate LHS ref to ReadOnlyBinding $binding")
case Some(ViewBinding(target1, wr1)) =>
val (target2, wr2) = reify(target1)
val writability = wr1.combine(wr2)
writability.reportIfReadOnly(target2.lref)(Wire(chiselTypeOf(target2)).lref)
case Some(binding: TopBinding) => Node(this)
case opt => throwException(s"internal error: unknown binding $opt in generating LHS ref")
}
}
// Internal API: returns a ref, if bound
private[chisel3] def ref(implicit info: SourceInfo): Arg = {
def materializeWire(makeConst: Boolean = false): Arg = {
if (!Builder.currentModule.isDefined) throwException(s"internal error: cannot materialize ref for $this")
implicit val sourceInfo = UnlocatableSourceInfo
if (makeConst) {
WireDefault(Const(chiselTypeOf(this)), this).ref
} else {
WireDefault(this).ref
}
}
requireIsHardware(this)
topBindingOpt match {
// DataView
case Some(ViewBinding(target, _)) => reify(target)._1.ref
case Some(_: AggregateViewBinding) =>
reifyIdentityView(this) match {
// If this is an identity view (a view of something of the same type), return ref of target
case Some((target, _)) => target.ref
// Otherwise, we need to materialize hardware of the correct type
case _ => materializeWire()
}
// Literals
case Some(ElementLitBinding(litArg)) => litArg
case Some(BundleLitBinding(litMap)) =>
litMap.get(this) match {
case Some(litArg) => litArg
case _ => materializeWire(true) // FIXME FIRRTL doesn't have Bundle literal expressions
}
case Some(VecLitBinding(litMap)) =>
litMap.get(this) match {
case Some(litArg) => litArg
case _ => materializeWire(true) // FIXME FIRRTL doesn't have Vec literal expressions
}
case Some(DontCareBinding()) =>
materializeWire() // FIXME FIRRTL doesn't have a DontCare expression so materialize a Wire
// Non-literals
case Some(binding: TopBinding) =>
if (Builder.currentModule.isDefined) {
// This is allowed (among other cases) for evaluating args of Printf / Assert / Printable, which are
// partially resolved *after* elaboration completes. If this is resolved, the check should be unconditional.
requireVisible()
}
Node(this)
case opt => throwException(s"internal error: unknown binding $opt in generating LHS ref")
}
}
// Recursively set the parent of the start Data and any children (eg. in an Aggregate)
private[chisel3] def setAllParents(parent: Option[BaseModule]): Unit =
DataMirror.collectAllMembers(this).foreach { x => x._parent = parent }
private[chisel3] def width: Width
private[chisel3] def firrtlConnect(that: Data)(implicit sourceInfo: SourceInfo): Unit
/** Internal API; Chisel users should look at chisel3.chiselTypeOf(...).
*
* cloneType must be defined for any Chisel object extending Data.
* It is responsible for constructing a basic copy of the object being cloned.
*
* @return a copy of the object.
*/
def cloneType: this.type
/** Internal API; Chisel users should look at chisel3.chiselTypeOf(...).
*
* Returns a copy of this data type, with hardware bindings (if any) removed.
* Directionality data and probe information is still preserved.
*/
private[chisel3] def cloneTypeFull: this.type = {
val clone: this.type = this.cloneType // get a fresh object, without bindings
// Only the top-level direction needs to be fixed up, cloneType should do the rest
clone.specifiedDirection = specifiedDirection
probe.setProbeModifier(clone, probeInfo)
clone.isConst = isConst
clone
}
/** The "strong connect" operator.
*
* For chisel3._, this operator is mono-directioned; all sub-elements of `this` will be driven by sub-elements of `that`.
* - Equivalent to `this :#= that`
*
* For Chisel._, this operator connections bi-directionally via emitting the FIRRTL.<=
* - Equivalent to `this :<>= that`
*
* @param that the Data to connect from
* @group connection
*/
final def :=(that: => Data)(implicit sourceInfo: SourceInfo): Unit = {
prefix(this) {
this.connect(that)(sourceInfo)
}
}
/** The "bulk connect operator", assigning elements in this Vec from elements in a Vec.
*
* For chisel3._, uses the `chisel3.internal.BiConnect` algorithm; sub-elements of that` may end up driving sub-elements of `this`
* - Complicated semantics, hard to write quickly, will likely be deprecated in the future
*
* For Chisel._, emits the FIRRTL.<- operator
* - Equivalent to `this :<>= that` without the restrictions that bundle field names and vector sizes must match
*
* @param that the Data to connect from
* @group connection
*/
final def <>(that: => Data)(implicit sourceInfo: SourceInfo): Unit = {
prefix(this) {
this.bulkConnect(that)(sourceInfo)
}
}
def isLit: Boolean = litOption.isDefined
/**
* If this is a literal that is representable as bits, returns the value as a BigInt.
* If not a literal, or not representable as bits (for example, is or contains Analog), returns None.
*/
def litOption: Option[BigInt]
/**
* Returns the literal value if this is a literal that is representable as bits, otherwise crashes.
*/
def litValue: BigInt = litOption.get
/** Returns the width, in bits, if currently known. */
final def getWidth: Int =
if (isWidthKnown) width.get else throwException(s"Width of $this is unknown!")
/** Returns whether the width is currently known. */
final def isWidthKnown: Boolean = width.known
/** Returns Some(width) if the width is known, else None. */
final def widthOption: Option[Int] = if (isWidthKnown) Some(getWidth) else None
private[chisel3] def _asTypeOfImpl[T <: Data](that: T)(implicit sourceInfo: SourceInfo): T = {
that._fromUInt(this.asUInt).asInstanceOf[T].viewAsReadOnly { _ =>
"Return values of asTypeOf are now read-only"
}
}
/** Return a value of this type from a UInt type. Internal implementation for asTypeOf.
*
* Protected so that it can be implemented by the external FixedPoint library
*/
protected def _fromUInt(that: UInt)(implicit sourceInfo: SourceInfo): Data
// Package private alias for _fromUInt so we can call it elsewhere in chisel3
private[chisel3] final def _fromUIntPrivate(that: UInt)(implicit sourceInfo: SourceInfo): Data = _fromUInt(that)
// The actual implementation of do_asUInt
// @param first exists because of awkward behavior in Aggregate that requires changing 0.U to be zero-width to fix
private[chisel3] def _asUIntImpl(first: Boolean)(implicit sourceInfo: SourceInfo): UInt
protected def _asUIntImpl(implicit sourceInfo: SourceInfo): UInt = this._asUIntImpl(true)
/** Default pretty printing */
def toPrintable: Printable
/** A non-ambiguous name of this `Data` for use in generated Verilog names */
def typeName: String = simpleClassName(this.getClass)
}
object Data {
// Needed for the `implicit def toConnectableDefault`
import scala.language.implicitConversions
// ThreadLocal to prevent infinite recursion in stringAccessor when reifySingleTarget triggers toString
private[chisel3] val avoidReifyingViews: ThreadLocal[Boolean] = ThreadLocal.withInitial(() => false)
private[chisel3] case class ProbeInfo(val writable: Boolean, color: Option[layer.Layer])
/** Provides :<=, :>=, :<>=, and :#= between consumer and producer of the same T <: Data */
implicit class ConnectableDefault[T <: Data](consumer: T) extends connectable.ConnectableOperators[T](consumer)
/** Provides :<>=, :<=, :>=, and :#= between a (consumer: Vec) and (producer: Seq) */
implicit class ConnectableVecDefault[T <: Data](consumer: Vec[T])
extends connectable.ConnectableVecOperators[T](consumer)
/** Can implicitly convert a Data to a Connectable
*
* Originally this was done with an implicit class, but all functions we want to
* add to Data we also want on Connectable, so an implicit conversion makes the most sense
* so the ScalaDoc can be shared.
*/
implicit def toConnectableDefault[T <: Data](d: T): Connectable[T] = makeConnectableDefault(d)
/** Create the default [[Connectable]] used for all instances of a [[Data]] of type T.
*
* This uses the default [[connectable.Connectable.apply]] as a starting point.
*
* Users can extend the [[HasCustomConnectable]] trait on any [[Data]] to further customize the [[Connectable]]. This
* is checked for in any potentially nested [[Data]] and any customizations are applied on top of the default
* [[Connectable]].
*/
private[chisel3] def makeConnectableDefault[T <: Data](d: T): Connectable[T] = {
val base = Connectable.apply(d)
DataMirror
.collectMembers(d) { case hasCustom: HasCustomConnectable =>
hasCustom
}
.foldLeft(base)((connectable, hasCustom) => hasCustom.customConnectable(connectable))
}
/** Typeclass implementation of HasMatchingZipOfChildren for Data
*
* The canonical API to iterate through two Chisel types or components, where
* matching children are provided together, while non-matching members are provided
* separately
*
* Only zips immediate children (vs members, which are all children/grandchildren etc.)
*/
implicit val dataMatchingZipOfChildren: DataMirror.HasMatchingZipOfChildren[Data] =
new DataMirror.HasMatchingZipOfChildren[Data] {
implicit class VecOptOps(vOpt: Option[Vec[Data]]) {
// Like .get, but its already defined on Option
def grab(i: Int): Option[Data] = vOpt.flatMap { _.lift(i) }
def size = vOpt.map(_.size).getOrElse(0)
}
implicit class RecordOptGet(rOpt: Option[Record]) {
// Like .get, but its already defined on Option
def grab(k: String): Option[Data] = rOpt.flatMap { _._elements.get(k) }
def keys: Iterable[String] = rOpt.map { r => r._elements.map(_._1) }.getOrElse(Seq.empty[String])
}
// TODO(azidar): Rewrite this to be more clear, probably not the cleanest way to express this
private def isDifferent(l: Option[Data], r: Option[Data]): Boolean =
l.nonEmpty && r.nonEmpty && !isRecord(l, r) && !isVec(l, r) && !isElement(l, r) && !isProbe(l, r)
private def isRecord(l: Option[Data], r: Option[Data]): Boolean =
l.orElse(r).map { _.isInstanceOf[Record] }.getOrElse(false)
private def isVec(l: Option[Data], r: Option[Data]): Boolean =
l.orElse(r).map { _.isInstanceOf[Vec[_]] }.getOrElse(false)
private def isElement(l: Option[Data], r: Option[Data]): Boolean =
l.orElse(r).map { _.isInstanceOf[Element] }.getOrElse(false)
private def isProbe(l: Option[Data], r: Option[Data]): Boolean =
l.orElse(r).map { x => x.isInstanceOf[Data] && DataMirror.hasProbeTypeModifier(x) }.getOrElse(false)
/** Zips matching children of `left` and `right`; returns Nil if both are empty
*
* The canonical API to iterate through two Chisel types or components, where
* matching children are provided together, while non-matching members are provided
* separately
*
* Only zips immediate children (vs members, which are all children/grandchildren etc.)
*
* Returns Nil if both are different types
*/
def matchingZipOfChildren(left: Option[Data], right: Option[Data]): Seq[(Option[Data], Option[Data])] =
(left, right) match {
case (None, None) => Nil
case (lOpt, rOpt) if isDifferent(lOpt, rOpt) => Nil
case (lOpt, rOpt) if isProbe(lOpt, rOpt) => Nil
case (lOpt: Option[Vec[Data] @unchecked], rOpt: Option[Vec[Data] @unchecked]) if isVec(lOpt, rOpt) =>
(0 until (lOpt.size.max(rOpt.size))).map { i => (lOpt.grab(i), rOpt.grab(i)) }
case (lOpt: Option[Record @unchecked], rOpt: Option[Record @unchecked]) if isRecord(lOpt, rOpt) =>
(lOpt.keys ++ rOpt.keys).toList.distinct.map { k => (lOpt.grab(k), rOpt.grab(k)) }
case (lOpt: Option[Element @unchecked], rOpt: Option[Element @unchecked]) if isElement(lOpt, rOpt) => Nil
case _ =>
throw new InternalErrorException(s"Match Error: left=$left, right=$right")