-
Notifications
You must be signed in to change notification settings - Fork 7
/
Compiler.scala
1348 lines (1193 loc) · 70.5 KB
/
Compiler.scala
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
package edg.compiler
import edg.EdgirUtils._
import edg.util.{DependencyGraph, Errorable, SingleWriteHashMap}
import edg.wir._
import edg.wir
import edgir.expr.expr
import edgir.ref.ref
import edgir.schema.schema
import scala.collection.{SeqMap, mutable}
class IllegalConstraintException(msg: String) extends Exception(msg)
class ElaboratingException(msg: String, wrapped: Exception) extends Exception(f"$msg:\n$wrapped")
sealed trait ElaborateRecord
object ElaborateRecord {
sealed trait ElaborateTask extends ElaborateRecord // an elaboration task that can be run
sealed trait ElaborateDependency extends ElaborateRecord // an elaboration dependency source
case class Block(blockPath: DesignPath) extends ElaborateTask // even when done, still may only be a generator
case class Link(linkPath: DesignPath) extends ElaborateTask
case class LinkArray(linkPath: DesignPath) extends ElaborateTask
// Connection to be elaborated, to set port parameter, IS_CONNECTED, and CONNECTED_LINK equivalences.
// Only elaborates the direct connect, and for bundles, creates sub-Connect tasks since it needs
// connectedLink and linkParams.
case class Connect(toLinkPortPath: DesignPath, toBlockPortPath: DesignPath)
extends ElaborateTask
// Elaborates the contents of a port array, based on the port array's ELEMENTS parameter.
// For recursive arrays, (eg, in link arrays), this is set when recursively elaborated.
// Only called for port arrays without defined elements (so excluding blocks that define their ports, including
// generator-defined port arrays which are structurally similar).
// Created but never run for abstract blocks with abstract port array.
case class ElaboratePortArray(path: DesignPath) extends ElaborateTask
case class ParamValue(paramPath: IndirectDesignPath) extends ElaborateDependency // when solved
// Set when the connection from the link's port to portPath have been elaborated, or for link ports
// when the link has been elaborated.
// When this is completed, connectedLink for the port and linkParams for the link will be set.
// Never set for port arrays.
case class ConnectedLink(portPath: DesignPath) extends ElaborateDependency
// The next tasks are a series for array connection
// Defines the ALLOCATED for the port array, by aggregating all the connected ports.
// Requires: ELEMENTS of all incoming connections defined.
case class ResolveArrayAllocated(parent: DesignPath, portPath: Seq[String], constraintNames: Seq[String],
arrayConstraintNames: Seq[String], portIsLink: Boolean)
extends ElaborateTask with ElaborateDependency
// For array connections (to link arrays) only, rewrites constraints to replace the ALLOCATE with a concrete
// port name, automatically allocated.
// Requires: array-connections expanded, port's ELEMENTS defined.
case class RewriteArrayAllocate(parent: DesignPath, portPath: Seq[String], constraintNames: Seq[String],
arrayConstraintNames: Seq[String], portIsLink: Boolean)
extends ElaborateTask with ElaborateDependency
// Expands ArrayConnect and ArrayExport connections to individual Connect and Export operations.
// ALLOCATEs are preserved as-is, meaning those will be allocated on an individual (instead of array) basis.
// Requires: port's ELEMENTS defined.
case class ExpandArrayConnections(parent: DesignPath, constraintName: String)
extends ElaborateTask with ElaborateDependency
// Once lowered to single connects, rewrites constraints to replace the ALLOCATE with a concrete port name,
// allocated from the port's ELEMENTS.
// Requires: array-connections expanded, port's ELEMENTS defined.
case class RewriteConnectAllocate(parent: DesignPath, portPath: Seq[String], constraintNames: Seq[String],
arrayConstraintNames: Seq[String], portIsLink: Boolean)
extends ElaborateTask with ElaborateDependency
// Sets a PortArray's IS_CONNECTED based off all the connected constraints.
// Requires: array-connections expanded, ALLOCATE replaced with concrete indices
case class ResolveArrayIsConnected(parent: DesignPath, portPath: Seq[String], constraintNames: Seq[String],
arrayConstraintNames: Seq[String], portIsLink: Boolean)
extends ElaborateTask
}
sealed trait CompilerError
object CompilerError {
case class Unelaborated(elaborateRecord: ElaborateRecord, missing: Set[ElaborateRecord]) extends CompilerError {
// These errors may be redundant with below, but provides dependency data
override def toString: String = s"Unelaborated missing dependencies $elaborateRecord:\n" +
s"${missing.map(x => s"- $x").mkString("\n")}"
}
case class LibraryElement(path: DesignPath, target: ref.LibraryPath) extends CompilerError {
override def toString: String = s"Unelaborated library element ${target.toSimpleString} @ $path"
}
case class UndefinedPortArray(path: DesignPath, portType: ref.LibraryPath) extends CompilerError {
override def toString: String = s"Undefined port array ${portType.toSimpleString} @ $path"
}
case class LibraryError(path: DesignPath, target: ref.LibraryPath, err: String) extends CompilerError {
override def toString: String = s"Library error ${target.toSimpleString} @ $path: $err"
}
case class GeneratorError(path: DesignPath, target: ref.LibraryPath, err: String) extends CompilerError {
override def toString: String = s"Generator error ${target.toSimpleString} @ $path: $err"
}
case class RefinementSubclassError(path: DesignPath, refinedLibrary: ref.LibraryPath, designLibrary: ref.LibraryPath)
extends CompilerError {
override def toString: String =
s"Invalid refinement ${refinedLibrary.toSimpleString} <- ${designLibrary.toSimpleString} @ $path"
}
case class OverAssign(target: IndirectDesignPath,
causes: Seq[OverAssignCause]) extends CompilerError {
override def toString: String = s"Overassign to $target:\n" +
s"${causes.map(x => s"- $x").mkString("\n")}"
}
case class BadRef(path: DesignPath, ref: IndirectDesignPath) extends CompilerError
case class AbstractBlock(path: DesignPath, blockType: ref.LibraryPath) extends CompilerError {
override def toString: String = s"Abstract block: $path (of type ${blockType.toSimpleString})"
}
case class FailedAssertion(root: DesignPath, constrName: String,
value: expr.ValueExpr, result: ExprValue) extends CompilerError {
override def toString: String =
s"Failed assertion: $root.$constrName, ${ExprToString.apply(value)} => $result"
}
case class MissingAssertion(root: DesignPath, constrName: String,
value: expr.ValueExpr, missing: Set[IndirectDesignPath]) extends CompilerError {
override def toString: String =
s"Unevaluated assertion: $root.$constrName (${ExprToString.apply(value)}), missing ${missing.mkString(", ")}"
}
// TODO should this be an error? Currently a debugging tool
case class EmptyRange(param: IndirectDesignPath, root: DesignPath, constrName: String,
value: expr.ValueExpr) extends CompilerError
sealed trait OverAssignCause
object OverAssignCause {
case class Assign(target: IndirectDesignPath, root: DesignPath, constrName: String, value: expr.ValueExpr)
extends OverAssignCause {
override def toString = s"Assign $target <- ${ExprToString(value)} @ $root.$constrName"
}
case class Equal(target: IndirectDesignPath, source: IndirectDesignPath) // TODO constraint info once we track that?
extends OverAssignCause {
override def toString = s"Equals $target = $source"
}
}
}
/** Compiler for a particular design, with an associated library to elaborate references from.
* TODO also needs a Python interface for generators, somewhere.
*
* During the compilation process, internal data structures are mutated.
*
* Port parameters are propagated by expanding connect and export statements between connected ports
* into equalities between all contained parameters.
* This expansion triggers when both ports are fully elaborated, and checks the structures of both ends
* for equivalence.
*
* CONNECTED_LINK parameters are propagated by expanding from the link's top-level port outward.
* Expansion triggers at the link-side top-level port (by ref matching), or when the towards-innermost-link
* (or towards-outermost-block) port is expanded.
* A list of link params is kept in a hashmap indexed by ports, as they are expanded.
*
* Alternative: fetch links from library (using the port type) to get params to expand.
* Problem: a bit more restrictive than what can be expressed in a block - but should be a common interface.
*
* It is intentional to allow a link-side port to access the CONNECTED_LINK, as a mechanism to access inner links.
*/
class Compiler(inputDesignPb: schema.Design, library: edg.wir.Library,
refinements: Refinements=Refinements()) {
def readableLibraryPath(path: ref.LibraryPath): String = { // TODO refactor to shared utils?
path.getTarget.getName
}
// Working design tree data structure
private val root = new wir.Block(inputDesignPb.getContents, None) // TODO refactor to unify root / non-root cases
def resolve(path: DesignPath): wir.Pathable = root.resolve(path.steps)
def resolveBlock(path: DesignPath): wir.BlockLike = root.resolve(path.steps).asInstanceOf[wir.BlockLike]
def resolveLink(path: DesignPath): wir.LinkLike = root.resolve(path.steps).asInstanceOf[wir.LinkLike]
def resolvePort(path: DesignPath): wir.PortLike = root.resolve(path.steps).asInstanceOf[wir.PortLike]
// Main data structure that tracks the next unit to elaborate
private val elaboratePending = DependencyGraph[ElaborateRecord, None.type]()
// Design parameters solving (constraint evaluation) and assertions
private val constProp = new ConstProp() {
override def onParamSolved(param: IndirectDesignPath, value: ExprValue): Unit = {
elaboratePending.setValue(ElaborateRecord.ParamValue(param), null)
}
}
for ((path, value) <- refinements.instanceValues) { // seed const prop with path assertions
constProp.setForcedValue(path.asIndirect, value, "path refinement")
}
// Primarily used for unit tests, TODO clean up this API?
private[edg] def getValue(path: IndirectDesignPath): Option[ExprValue] = constProp.getValue(path)
private val assertions = mutable.Buffer[(DesignPath, String, expr.ValueExpr, SourceLocator)]() // containing block, name, expr
// Supplemental elaboration data structures
private val linkParams = SingleWriteHashMap[DesignPath, Seq[IndirectStep]]() // link path -> list of params
linkParams.put(DesignPath(), Seq()) // empty path means disconnected
private val connectedLink = SingleWriteHashMap[DesignPath, DesignPath]() // port -> connected link path
private val expandedArrayConnectConstraints = SingleWriteHashMap[DesignPath, Seq[String]]() // constraint path -> new constraint names
private val errors = mutable.ListBuffer[CompilerError]()
// Returns all errors, by scanning the design tree for errors and adding errors accumulated through the compile
// process
def getErrors(): Seq[CompilerError] = {
val assertionErrors = assertions.flatMap { case (root, constrName, value, sourceLocator) =>
new ExprEvaluatePartial(constProp, root).map(value) match {
case ExprResult.Result(BooleanValue(true)) => None
case ExprResult.Result(result) =>
Some(CompilerError.FailedAssertion(root, constrName, value, result))
case ExprResult.Missing(missing) =>
Some(CompilerError.MissingAssertion(root, constrName, value, missing))
}
}.toSeq
val pendingErrors = elaboratePending.getMissing.map { missingNode =>
CompilerError.Unelaborated(missingNode, elaboratePending.nodeMissing(missingNode))
}.toSeq
errors.toSeq ++ constProp.getErrors ++ pendingErrors ++ assertionErrors
}
// Seed the elaboration record with the root design
//
elaboratePending.addNode(ElaborateRecord.Block(DesignPath()), Seq())
require(root.getPorts.isEmpty, "design top may not have ports") // also don't need to elaborate top ports
processParamDeclarations(DesignPath(), root)
// Hook method to be overridden, eg for status
//
def onElaborate(record: ElaborateRecord): Unit = { }
// Actual compilation methods
//
// Elaborate a connection (either a connect or export), by generating bidirectional equality constraints
// including link parameters (through CONNECTED_LINK) and IS_CONNECTED.
// Neither port can be an array (container) port (array connects should be expanded into individual element connects),
// but ports may be bundle or inner ports (in bundles or arrays).
// Only the link side port needs to have been elaborated.
protected def elaborateConnect(connect: ElaborateRecord.Connect): Unit = {
// Generate port-port parameter propagation
// All connected ports should have params
val toLinkPort = resolvePort(connect.toLinkPortPath).asInstanceOf[wir.HasParams]
val connectedParam = toLinkPort.getParams.keys.map(IndirectStep.Element(_))
for (connectedStep <- connectedParam) { // note: can't happen for top level connect!
constProp.addEquality(
connect.toLinkPortPath.asIndirect + connectedStep,
connect.toBlockPortPath.asIndirect + connectedStep
)
}
val linkPath = connectedLink(connect.toLinkPortPath) // must have ben set with ConnectedLink
connectedLink.put(connect.toBlockPortPath, linkPath) // propagate CONNECTED_LINK params
val allParams = linkParams(linkPath) :+ IndirectStep.Name
for (linkParam <- allParams) { // generate CONNECTED_LINK equalities
constProp.addEquality(
connect.toBlockPortPath.asIndirect + IndirectStep.ConnectedLink + linkParam,
linkPath.asIndirect + linkParam,
)
}
// Add sub-ports to the elaboration dependency graph, as appropriate
toLinkPort match {
case toLinkPort: wir.Bundle =>
for (portName <- toLinkPort.getPorts.keys) {
constProp.addEquality(connect.toLinkPortPath.asIndirect + IndirectStep.IsConnected,
connect.toLinkPortPath.asIndirect + portName + IndirectStep.IsConnected)
constProp.addEquality(connect.toBlockPortPath.asIndirect + IndirectStep.IsConnected,
connect.toBlockPortPath.asIndirect + portName + IndirectStep.IsConnected)
elaboratePending.addNode(
ElaborateRecord.Connect(connect.toLinkPortPath + portName, connect.toBlockPortPath + portName),
Seq(ElaborateRecord.ConnectedLink(connect.toLinkPortPath + portName))
)
}
case toLinkPort => // everything else ignored
}
// Register port as finished
elaboratePending.setValue(ElaborateRecord.ConnectedLink(connect.toBlockPortPath), None)
}
protected def resolvePortConnectivity(containerPath: DesignPath, portPostfix: Seq[String],
constraint: Option[(String, expr.ValueExpr)]): Unit = {
val port = resolvePort(containerPath ++ portPostfix)
val container = resolve(containerPath).asInstanceOf[wir.HasMutableConstraints] // block or link
val portBlock = resolve(containerPath + portPostfix.head).asInstanceOf[wir.HasMutableConstraints] // block or link
val constraintExpr = constraint.map { case (constrName, constr) => (constrName, constr.expr) }
def recursiveSetNotConnected(portPath: DesignPath, port: wir.PortLike): Unit = {
constProp.setValue(portPath.asIndirect + IndirectStep.IsConnected,
BooleanValue(false),
s"${containerPath ++ portPostfix}.(not connected)")
portBlock match {
case _: wir.Block =>
connectedLink.put(portPath, DesignPath())
elaboratePending.setValue(ElaborateRecord.ConnectedLink(portPath), None)
case _: wir.Link | _: wir.LinkArray => // links set these on all ports, so this is ignored here. TODO: unify code paths?
}
port match {
case port: wir.Bundle =>
port.getPorts.foreach { case (innerIndex, innerPort) =>
recursiveSetNotConnected(portPath + innerIndex, innerPort)
}
case _ => // no recursion at leaf
}
}
port match {
case _: wir.Bundle | _: wir.Port => constraintExpr match {
case Some((constrName, expr.ValueExpr.Expr.Connected(connected))) =>
require(container.isInstanceOf[wir.Block])
constProp.setValue(containerPath.asIndirect ++ portPostfix + IndirectStep.IsConnected,
BooleanValue(true),
s"$containerPath.$constrName")
case Some((constrName, expr.ValueExpr.Expr.Exported(exported))) =>
constProp.addDirectedEquality(containerPath.asIndirect ++ portPostfix + IndirectStep.IsConnected,
containerPath.asIndirect ++ exported.getExteriorPort.getRef + IndirectStep.IsConnected,
containerPath, s"$containerPath.$constrName")
case Some((constrName, expr.ValueExpr.Expr.ExportedTunnel(exported))) => // same as exported case
constProp.addDirectedEquality(containerPath.asIndirect ++ portPostfix + IndirectStep.IsConnected,
containerPath.asIndirect ++ exported.getExteriorPort.getRef + IndirectStep.IsConnected,
containerPath, s"$containerPath.$constrName")
case None =>
recursiveSetNotConnected(containerPath ++ portPostfix, port)
case Some((_, _)) => throw new IllegalArgumentException
}
case _: wir.PortLibrary => throw new IllegalArgumentException
case _: wir.PortArray => throw new IllegalArgumentException // must be lowered before
}
}
// Called for each param declaration, currently just registers the declaration and type signature.
protected def processParamDeclarations(path: DesignPath, hasParams: wir.HasParams): Unit = {
for ((paramName, param) <- hasParams.getParams) {
constProp.addDeclaration(path + paramName, param)
}
}
// Elaborates the port, mutating it in-place. Recursive.
protected def elaboratePort(path: DesignPath, container: wir.HasMutablePorts, port: wir.PortLike): Unit = {
// Instantiate as needed
val instantiated = port match {
case port: wir.PortLibrary =>
val libraryPath = port.target
val portPb = library.getPort(libraryPath) match {
case Errorable.Success(portPb) => portPb
case Errorable.Error(err) =>
import edg.IrPort
import edgir.elem.elem
errors += CompilerError.LibraryError(path, libraryPath, err)
IrPort.Port(elem.Port())
}
val newPort = wir.PortLike.fromIrPort(portPb)
container.elaborate(path.lastString, newPort)
newPort
case port: wir.PortArray => port // no instantiation needed
case port => throw new NotImplementedError(s"unknown unelaborated port $port")
}
// Process and recurse as needed
instantiated match {
case port: wir.Port =>
constProp.setValue(path.asIndirect + IndirectStep.Name, TextValue(path.toString))
processParamDeclarations(path, port)
case port: wir.Bundle =>
constProp.setValue(path.asIndirect + IndirectStep.Name, TextValue(path.toString))
processParamDeclarations(path, port)
for ((childPortName, childPort) <- port.getPorts) {
elaboratePort(path + childPortName, port, childPort)
}
case port: wir.PortArray =>
if (port.portsSet) { // set ELEMENTS if ports is defined by array, otherwise ports are dependent on ELEMENTS
constProp.setValue(path.asIndirect + IndirectStep.Elements,
ArrayValue(port.getPorts.keys.toSeq.map(TextValue(_))))
}
elaboratePending.addNode(ElaborateRecord.ElaboratePortArray(path), Seq( // does recursive elaboration + LENGTH
ElaborateRecord.ParamValue(path.asIndirect + IndirectStep.Elements)
))
case port => throw new NotImplementedError(s"unknown instantiated port $port")
}
}
// Attempts to process a parameter constraint, returning true if it is a matching constraint
def processParamConstraint(blockPath: DesignPath, constrName: String, constr: expr.ValueExpr,
constrValue: expr.ValueExpr): Boolean = constrValue.expr match {
case expr.ValueExpr.Expr.Assign(assign) =>
constProp.addAssignment(
blockPath.asIndirect ++ assign.dst.get,
blockPath, assign.src.get, constrName) // TODO add sourcelocators
true
case expr.ValueExpr.Expr.AssignTunnel(assign) =>
// same as normal assign case, but would not enforce locality of references
constProp.addAssignment(
blockPath.asIndirect ++ assign.dst.get,
blockPath, assign.src.get, constrName) // TODO add sourcelocators
true
case expr.ValueExpr.Expr.Binary(_) | expr.ValueExpr.Expr.BinarySet(_) |
expr.ValueExpr.Expr.Unary(_) | expr.ValueExpr.Expr.UnarySet(_) |
expr.ValueExpr.Expr.IfThenElse(_) => // raw ValueExprs interpreted as assertions
assertions += ((blockPath, constrName, constr, SourceLocator.empty)) // TODO add source locators
true
case expr.ValueExpr.Expr.Ref(target) // IsConnected also treated as assertion
if target.steps.last.step.isReservedParam
&& target.steps.last.getReservedParam == ref.Reserved.IS_CONNECTED =>
assertions += ((blockPath, constrName, constr, SourceLocator.empty)) // TODO add source locators
true
case _ => false
}
// Attempts to process a connected constraint, returning true if it is a matching constraint
def processConnectedConstraint(blockPath: DesignPath, constrName: String, constr: expr.ValueExpr,
isInLink: Boolean): Boolean = {
import edg.ExprBuilder.ValueExpr
constr.expr match {
case expr.ValueExpr.Expr.Connected(connected) => (connected.getBlockPort, connected.getLinkPort) match {
case (ValueExpr.Ref(blockPort), ValueExpr.Ref(linkPort)) =>
require(!isInLink)
elaboratePending.addNode(
ElaborateRecord.Connect(blockPath ++ linkPort, blockPath ++ blockPort),
Seq(ElaborateRecord.ConnectedLink(blockPath ++ linkPort))
)
true
case _ => false // anything with allocates is not processed
}
case expr.ValueExpr.Expr.Exported(exported) => (exported.getExteriorPort, exported.getInternalBlockPort) match {
case (ValueExpr.Ref(extPort), ValueExpr.Ref(intPort)) =>
if (!isInLink) {
elaboratePending.addNode(
ElaborateRecord.Connect(blockPath ++ extPort, blockPath ++ intPort),
Seq(ElaborateRecord.ConnectedLink(blockPath ++ extPort))
)
} else { // for links, the external port faces to the block, so args must be flipped
elaboratePending.addNode(
ElaborateRecord.Connect(blockPath ++ intPort, blockPath ++ extPort),
Seq(ElaborateRecord.ConnectedLink(blockPath ++ intPort))
)
}
true
case _ => false // anything with allocates is not processed
}
case expr.ValueExpr.Expr.ExportedTunnel(exported) => (exported.getExteriorPort, exported.getInternalBlockPort) match {
case (ValueExpr.Ref(extPort), ValueExpr.Ref(intPort)) =>
require(!isInLink)
elaboratePending.addNode(
ElaborateRecord.Connect(blockPath ++ extPort, blockPath ++ intPort),
Seq(ElaborateRecord.ConnectedLink(blockPath ++ extPort))
)
true
case _ => false // anything with allocates is not processed
}
case _ => false // not defined
}
}
// Given a block library at some path, expand it and return the instantiated block.
// Handles class type refinements and adds default parameters and class-based value refinements
// For the generator, this will be a skeleton block.
protected def expandBlock(path: DesignPath, block: wir.BlockLibrary): wir.Block = {
val libraryPath = block.target
val (refinedLibraryPath, unrefinedType) = refinements.instanceRefinements.get(path) match {
case Some(refinement) => (refinement, Some(libraryPath))
case None => refinements.classRefinements.get(libraryPath) match {
case Some(refinement) => (refinement, Some(libraryPath))
case None => (libraryPath, None)
}
}
val blockPb = library.getBlock(refinedLibraryPath) match {
case Errorable.Success(blockPb) =>
blockPb
case Errorable.Error(err) =>
import edgir.elem.elem
errors += CompilerError.LibraryError(path, refinedLibraryPath, err)
elem.HierarchyBlock()
}
// add class-based refinements - must be set before refinement params
// note that this operates on the post-refinement class
refinements.classValues.foreach { case (classPath, refinements) =>
if (library.isSubclassOf(refinedLibraryPath, classPath)) {
refinements.foreach{ case (subpath, value) =>
constProp.setForcedValue(path.asIndirect ++ subpath, value,
s"${refinedLibraryPath.getTarget.getName} class refinement")
}
}
}
// additional processing needed for the refinement case
if (unrefinedType.isDefined) {
if (!library.isSubclassOf(refinedLibraryPath, libraryPath)) { // check refinement validity
errors += CompilerError.RefinementSubclassError(path, refinedLibraryPath, libraryPath)
}
val unrefinedPb = library.getBlock(libraryPath) match { // add subclass (refinement) default params
case Errorable.Success(unrefinedPb) =>
unrefinedPb
case Errorable.Error(err) => // this doesn't stop elaboration, but does raise an error
import edgir.elem.elem
errors += CompilerError.LibraryError(path, libraryPath, err)
elem.HierarchyBlock()
}
val refinedNewParams = blockPb.params.keys.toSet -- unrefinedPb.params.keys
refinedNewParams.foreach { refinedNewParam =>
blockPb.paramDefaults.get(refinedNewParam).foreach { refinedDefault =>
constProp.addAssignment(path.asIndirect + refinedNewParam, path, refinedDefault,
s"(default)${refinedLibraryPath.toSimpleString}.$refinedNewParam")
}
}
}
val newBlock = if (blockPb.generator.isEmpty) {
new wir.Block(blockPb, unrefinedType)
} else {
new wir.Generator(blockPb, unrefinedType)
}
constProp.setValue(path.asIndirect + IndirectStep.Name, TextValue(path.toString))
processParamDeclarations(path, newBlock)
newBlock.getPorts.foreach { case (portName, port) => // all other cases, elaborate in place
elaboratePort(path + portName, newBlock, port)
}
val deps = newBlock match {
case newBlock: wir.Generator =>
newBlock.getDependencies.map { depPath =>
ElaborateRecord.ParamValue(path.asIndirect ++ depPath)
}
case _ => Seq()
}
elaboratePending.addNode(ElaborateRecord.Block(path), deps)
newBlock
}
// Given a link library at some path, expand it and return the instantiated block.
protected def expandLink(path: DesignPath, link: wir.LinkLibrary): wir.Link = {
val libraryPath = link.target
val linkPb = library.getLink(libraryPath) match {
case Errorable.Success(linkPb) => linkPb
case Errorable.Error(err) =>
import edgir.elem.elem
errors += CompilerError.LibraryError(path, libraryPath, err)
elem.Link()
}
val newLink = new wir.Link(linkPb)
// Elaborate ports and parameters
constProp.setValue(path.asIndirect + IndirectStep.Name, TextValue(path.toString))
processParamDeclarations(path, newLink)
linkParams.put(path, linkPb.params.keys.toSeq.map(IndirectStep.Element(_)))
newLink.getPorts.foreach { case (portName, port) =>
elaboratePort(path + portName, newLink, port)
}
// For link-side port arrays: set ALLOCATED -> ELEMENTS and allow it to expand later
newLink.getPorts.collect { case (portName, port: wir.PortArray) =>
require(!port.portsSet) // links can't have fixed array elts
constProp.addDirectedEquality(
path.asIndirect + portName + IndirectStep.Elements, path.asIndirect + portName + IndirectStep.Allocated,
path, s"$portName (link array-from-connects)")
}
// Links can only elaborate when their port arrays are ready
val arrayDeps = newLink.getPorts.collect {
case (portName, arr: wir.PortArray) => ElaborateRecord.ElaboratePortArray(path + portName)
}.toSeq
elaboratePending.addNode(ElaborateRecord.Link(path), arrayDeps)
newLink
}
// Expand an link array in-place.
protected def expandLinkArray(path: DesignPath, array: wir.LinkArray): Unit = {
val libraryPath = array.getModelLibrary
val modelPb = library.getLink(libraryPath) match {
case Errorable.Success(linkPb) =>
linkPb
case Errorable.Error(err) =>
import edgir.elem.elem
errors += CompilerError.LibraryError(path, libraryPath, err)
elem.Link()
}
val model = new wir.Link(modelPb)
array.createFrom(model)
// For all arrays, size ELEMENTS directly from ALLOCATED
model.getPorts.collect { case (portName, port: wir.PortArray) =>
constProp.addDirectedEquality(
path.asIndirect + portName + IndirectStep.Elements, path.asIndirect + portName + IndirectStep.Allocated,
path, s"$portName (link-array array-from-connects)")
}
val arrayPortDeps = model.getPorts.collect { case (portName, port: wir.PortArray) =>
ElaborateRecord.ParamValue(path.asIndirect + portName + IndirectStep.Elements)
}.toSeq
val elementDep = ElaborateRecord.ParamValue(path.asIndirect + IndirectStep.Elements)
elaboratePending.addNode(ElaborateRecord.LinkArray(path), arrayPortDeps :+ elementDep)
}
protected def runGenerator(path: DesignPath, generator: wir.Generator): Unit = {
val reqParamValues = generator.getDependencies.map { reqParam =>
reqParam -> constProp.getValue(path.asIndirect ++ reqParam).get
}
// Run generator and plug in
library.runGenerator(generator.getBlockClass, reqParamValues.toMap) match {
case Errorable.Success(generatedPb) =>
val generatedPorts = generator.applyGenerated(generatedPb)
generatedPorts.foreach { portName =>
val portArray = generator.getPorts(portName).asInstanceOf[wir.PortArray]
constProp.setValue(path.asIndirect + portName + IndirectStep.Elements,
ArrayValue(portArray.getPorts.keys.toSeq.map(TextValue(_))))
// the rest was already handled when elaboratePorts on the generator stub
}
case Errorable.Error(err) =>
errors += CompilerError.GeneratorError(path, generator.getBlockClass, err)
}
}
/** Elaborate a block, mainly processing its internal blocks, links, and connected and parameter constraints.
* The block should already have had its interface (ports and parameters) expanded.
*/
protected def elaborateBlock(path: DesignPath): Unit = {
val block = resolveBlock(path).asInstanceOf[wir.Block]
block match {
case block: wir.Generator => runGenerator(path, block)
case _ => // ignored
}
// Queue up sub-trees that need elaboration - needs to be post-generate for generators
block.getBlocks.foreach { case (innerBlockName, innerBlock) =>
val innerBlockElaborated = expandBlock(path + innerBlockName, innerBlock.asInstanceOf[wir.BlockLibrary])
block.elaborate(innerBlockName, innerBlockElaborated)
}
block.getLinks.foreach {
case (innerLinkName, innerLink: wir.LinkLibrary) =>
block.elaborate(innerLinkName, expandLink(path + innerLinkName, innerLink))
case (innerLinkName, innerLink: wir.LinkArray) =>
expandLinkArray(path + innerLinkName, innerLink)
case _ => throw new NotImplementedError()
}
val connectedConstraints = new ConnectedConstraintManager(block)
// Set IsConnected and generate constraint expansion records
import edg.ExprBuilder.ValueExpr
block.getBlocks.foreach { case (innerBlockName, innerBlock) =>
innerBlock.asInstanceOf[wir.Block].getPorts.foreach { case (portName, port) =>
val portPostfix = Seq(innerBlockName, portName)
port match {
case _: wir.PortArray => // array case: connectivity delayed to lowering
connectedConstraints.connectionsByBlockPort(portPostfix) match {
case PortConnections.ArrayConnect(constrName, constr) => constr.expr match {
case expr.ValueExpr.Expr.ConnectedArray(connected) =>
val linkPortPostfix = connected.getLinkPort match {
case ValueExpr.Ref(linkPortPostfix) => linkPortPostfix
case ValueExpr.RefAllocate(linkPortPostfix, _) => linkPortPostfix
case _ => throw new IllegalArgumentException
}
constProp.addEquality(path.asIndirect ++ portPostfix + IndirectStep.Elements,
path.asIndirect + linkPortPostfix.head + IndirectStep.Elements) // use link's ELEMENTS
constProp.addEquality(path.asIndirect ++ portPostfix + IndirectStep.Allocated,
path.asIndirect ++ portPostfix + IndirectStep.Elements) // TODO can this be directed?
val expandArrayTask = ElaborateRecord.ExpandArrayConnections(path, constrName)
// Note: actual expansion task set on the link side
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(constrName), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix),
expandArrayTask
))
case expr.ValueExpr.Expr.ExportedArray(exported) => // note internal port is portPostfix
val ValueExpr.Ref(extPostfix) = exported.getExteriorPort
constProp.addDirectedEquality(path.asIndirect ++ extPostfix + IndirectStep.Elements,
path.asIndirect ++ portPostfix + IndirectStep.Elements,
path, constrName)
constProp.addDirectedEquality(path.asIndirect ++ portPostfix + IndirectStep.Allocated,
path.asIndirect ++ extPostfix + IndirectStep.Allocated,
path, constrName)
val expandArrayTask = ElaborateRecord.ExpandArrayConnections(path, constrName)
elaboratePending.addNode(expandArrayTask,
Seq(
ElaborateRecord.ParamValue(path.asIndirect ++ portPostfix + IndirectStep.Elements),
// allocated must run first, it depends on constraints not being lowered
ElaborateRecord.ParamValue(path.asIndirect ++ portPostfix + IndirectStep.Allocated)
))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(constrName), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix),
expandArrayTask))
case _ => throw new IllegalArgumentException(s"invalid array connect to array $constr")
}
case PortConnections.AllocatedConnect(singleConnects, arrayConnects) =>
val arrayDeps = arrayConnects.map { case (allocated, constrName, constr) => // link elements required
val ValueExpr.RefAllocate(linkPostfix, _) = constr.getConnectedArray.getLinkPort
ElaborateRecord.ParamValue(path.asIndirect + linkPostfix.head + IndirectStep.Elements)
}
val setAllocatedTask = ElaborateRecord.ResolveArrayAllocated(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), false)
elaboratePending.addNode(setAllocatedTask, arrayDeps)
val expandArrayConnectTasks = arrayConnects.map { case (allocated, constrName, constr) =>
ElaborateRecord.ExpandArrayConnections(path, constrName)
}
val resolveAllocateTask = ElaborateRecord.RewriteConnectAllocate(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), false)
elaboratePending.addNode(resolveAllocateTask,
Seq(ElaborateRecord.ElaboratePortArray(path ++ portPostfix)) ++ expandArrayConnectTasks :+ setAllocatedTask)
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), false)
elaboratePending.addNode(resolveConnectedTask, Seq(resolveAllocateTask))
case PortConnections.AllocatedTunnelExport(connects) =>
// similar to AllocateDConnect case, except only with single-element connects
val setAllocatedTask = ElaborateRecord.ResolveArrayAllocated(path, portPostfix, connects.map(_._2), Seq(), false)
elaboratePending.addNode(setAllocatedTask, Seq())
val resolveAllocateTask = ElaborateRecord.RewriteConnectAllocate(path, portPostfix, connects.map(_._2), Seq(), false)
elaboratePending.addNode(resolveAllocateTask,
Seq(ElaborateRecord.ElaboratePortArray(path ++ portPostfix)) :+ setAllocatedTask)
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, connects.map(_._2), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(resolveAllocateTask))
case PortConnections.NotConnected =>
constProp.setValue(path.asIndirect ++ portPostfix + IndirectStep.Allocated, ArrayValue(Seq()))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
case connects => throw new IllegalArgumentException(s"invalid connections to array $connects")
}
case _ => // leaf only, no array support
connectedConstraints.connectionsByBlockPort(portPostfix) match {
case PortConnections.SingleConnect(constrName, constr) =>
resolvePortConnectivity(path, portPostfix, Some(constrName, constr))
case PortConnections.TunnelExport(constrName, constr) =>
resolvePortConnectivity(path, portPostfix, Some(constrName, constr))
case PortConnections.NotConnected =>
resolvePortConnectivity(path, portPostfix, None)
case connects => throw new IllegalArgumentException(s"invalid connections to element $connects")
}
}
}
}
block.getLinks.foreach {
case (innerLinkName, innerLink: wir.Link) => innerLink.getPorts.foreach { case (portName, port) =>
val portPostfix = Seq(innerLinkName, portName)
port match {
case _: wir.PortArray => // array case: connectivity delayed to lowering
connectedConstraints.connectionsByLinkPort(portPostfix, false) match {
case PortConnections.ArrayConnect(constrName, constr) =>
throw new NotImplementedError()
case PortConnections.AllocatedConnect(singleConnects, arrayConnects) =>
require(arrayConnects.isEmpty) // flattening (array-array w/o LinkArray) connections currently not used
val setAllocatedTask = ElaborateRecord.ResolveArrayAllocated(path, portPostfix, singleConnects.map(_._2), Seq(), false)
elaboratePending.addNode(setAllocatedTask, Seq())
val resolveAllocateTask = ElaborateRecord.RewriteConnectAllocate(path, portPostfix, singleConnects.map(_._2), Seq(), false)
elaboratePending.addNode(resolveAllocateTask,
Seq(ElaborateRecord.ElaboratePortArray(path ++ portPostfix)) :+ setAllocatedTask)
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, singleConnects.map(_._2), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
resolveAllocateTask))
case PortConnections.NotConnected =>
constProp.setValue(path.asIndirect ++ portPostfix + IndirectStep.Allocated, ArrayValue(Seq()))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
case connects => throw new IllegalArgumentException(s"invalid connections to array $connects")
}
case _ =>
connectedConstraints.connectionsByLinkPort(portPostfix, false) match {
case PortConnections.SingleConnect(constrName, constr) =>
resolvePortConnectivity(path, portPostfix, Some(constrName, constr))
case PortConnections.NotConnected =>
resolvePortConnectivity(path, portPostfix, None)
case connects => throw new IllegalArgumentException(s"invalid connections to element $connects")
}
}
}
case (innerLinkName, innerLink: wir.LinkArray) => innerLink.getModelPorts.foreach { case (portName, port) =>
val portPostfix = Seq(innerLinkName, portName)
port match {
case _: wir.PortArray => // array case: connectivity delayed to lowering
connectedConstraints.connectionsByLinkPort(portPostfix, false) match {
case PortConnections.AllocatedConnect(singleConnects, arrayConnects) =>
require(singleConnects.isEmpty) // link arrays cannot have single connects on ports
val setAllocatedTask = ElaborateRecord.ResolveArrayAllocated(path, portPostfix, Seq(), arrayConnects.map(_._2), false)
elaboratePending.addNode(setAllocatedTask, Seq(
ElaborateRecord.ParamValue(path.asIndirect + portPostfix.head + IndirectStep.Elements)))
val resolveAllocateTask = ElaborateRecord.RewriteArrayAllocate(path, portPostfix, Seq(), arrayConnects.map(_._2), false)
elaboratePending.addNode(resolveAllocateTask, Seq(setAllocatedTask))
val expandArrayTasks = arrayConnects.map { case (allocated, constrName, constr) =>
val expandArrayTask = ElaborateRecord.ExpandArrayConnections(path, constrName)
val blockPortPostfix = constr.getConnectedArray.getBlockPort match {
case ValueExpr.RefAllocate(blockPortPostfix, _) => blockPortPostfix
case ValueExpr.Ref(blockPortPostfix) => blockPortPostfix
case blockPort => throw new IllegalArgumentException(s"unknown block port $blockPort")
}
val blockArrayDep = // block allocated connect must run first since it must be pre-expansion
ElaborateRecord.ParamValue(path.asIndirect ++ blockPortPostfix + IndirectStep.Allocated)
elaboratePending.addNode(expandArrayTask, Seq(
ElaborateRecord.ParamValue(path.asIndirect + portPostfix.head + IndirectStep.Elements),
blockArrayDep,
resolveAllocateTask))
expandArrayTask
}
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), arrayConnects.map(_._2), false)
elaboratePending.addNode(resolveConnectedTask,
Seq(ElaborateRecord.ElaboratePortArray(path ++ portPostfix)) ++
expandArrayTasks)
case PortConnections.NotConnected => // TODO what are NC semantics for link array?
constProp.setValue(path.asIndirect ++ portPostfix + IndirectStep.Allocated, ArrayValue(Seq()))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
case connects => throw new IllegalArgumentException(s"invalid connections to array $connects")
}
case _ => // non-array, eg Port or Bundle
connectedConstraints.connectionsByLinkPort(portPostfix, false) match {
case PortConnections.ArrayConnect(constrName, constr) => constr.expr match {
case expr.ValueExpr.Expr.ConnectedArray(connected) =>
val expandArrayTask = ElaborateRecord.ExpandArrayConnections(path, constrName)
elaboratePending.addNode(expandArrayTask, Seq(
ElaborateRecord.ParamValue(path.asIndirect + portPostfix.head + IndirectStep.Elements)
))
// Note: actual expansion task set on the link side
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(constrName), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix),
expandArrayTask))
case connects => throw new IllegalArgumentException(s"invalid connections to array $connects")
}
case PortConnections.NotConnected =>
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
case connects => throw new IllegalArgumentException(s"invalid connections to element $connects")
}
}
}
case _ => throw new IllegalArgumentException
}
// Process all the process-able constraints: parameter constraints and non-allocate connected
block.getConstraints.foreach { case (constrName, constr) =>
processParamConstraint(path, constrName, constr, constr)
processConnectedConstraint(path, constrName, constr, false)
}
}
/** Elaborate the unelaborated link at path (but where the parent has been elaborated and is reachable from root),
* and adds it to the parent and replaces the lib_elem proto entry with a placeholder unknown.
* Adds children to the pending queue, and adds constraints to constProp.
* Expands connects in the parent, as needed.
*/
protected def elaborateLink(path: DesignPath): Unit = {
import edg.ExprBuilder.{Ref, ValueExpr}
val link = resolveLink(path).asInstanceOf[wir.Link]
// Queue up sub-trees that need elaboration
link.getLinks.foreach { case (innerLinkName, innerLink) =>
val innerLinkElaborated = expandLink(path + innerLinkName, innerLink.asInstanceOf[wir.LinkLibrary])
link.elaborate(innerLinkName, innerLinkElaborated)
}
def setConnectedLink(portPath: DesignPath, port: PortLike): Unit = (port: @unchecked) match {
case _: wir.Port | _: wir.Bundle =>
elaboratePending.setValue(ElaborateRecord.ConnectedLink(portPath), None)
connectedLink.put(portPath, path)
case port: wir.PortArray =>
port.getPorts.foreach { case (subPortName, subPort) =>
setConnectedLink(portPath + subPortName, subPort)
}
}
for ((portName, port) <- link.getPorts) {
setConnectedLink(path + portName, port)
}
// Aggregate by inner link ports
val connectedConstraints = new ConnectedConstraintManager(link)
link.getLinks.foreach { case (innerLinkName, innerLink) =>
innerLink.asInstanceOf[wir.Link].getPorts.foreach { case (portName, port) =>
val portPostfix = Seq(innerLinkName, portName)
port match {
case _: wir.PortArray => // array case: ignored, handled in lowering
connectedConstraints.connectionsByLinkPort(portPostfix, true) match {
case PortConnections.AllocatedConnect(singleConnects, arrayConnects) =>
val deps = arrayConnects.map { case (allocated, constrName, constr) =>
val extPostfix = constr.getExportedArray.getExteriorPort match {
case ValueExpr.MapExtract(ValueExpr.Ref(extPostfix), Ref(_)) => extPostfix
case extPort => throw new IllegalArgumentException(s"unknown exported exterior $extPort")
}
ElaborateRecord.ParamValue(path.asIndirect ++ extPostfix + IndirectStep.Allocated)
}
arrayConnects.foreach { case (allocated, constrName, constr) =>
val extPostfix = constr.getExportedArray.getExteriorPort match {
case ValueExpr.MapExtract(ValueExpr.Ref(extPostfix), Ref(_)) => extPostfix
case extPort => throw new IllegalArgumentException(s"unknown exported exterior $extPort")
}
val ValueExpr.RefAllocate(intPostfix, _) = constr.getExportedArray.getInternalBlockPort
elaboratePending.addNode(ElaborateRecord.ExpandArrayConnections(path, constrName), Seq(
ElaborateRecord.ParamValue(path.asIndirect ++ extPostfix + IndirectStep.Elements),
// allocated must run first, it depends on constraints not being lowered
ElaborateRecord.ParamValue(path.asIndirect ++ intPostfix + IndirectStep.Allocated)
))
}
val setAllocatedTask = ElaborateRecord.ResolveArrayAllocated(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), true)
elaboratePending.addNode(setAllocatedTask, deps)
val resolveAllocateTask = ElaborateRecord.RewriteConnectAllocate(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), true)
elaboratePending.addNode(resolveAllocateTask, Seq(ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, singleConnects.map(_._2), arrayConnects.map(_._2), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
resolveAllocateTask))
case PortConnections.NotConnected =>
constProp.setValue(path.asIndirect ++ portPostfix + IndirectStep.Allocated, ArrayValue(Seq()))
val resolveConnectedTask = ElaborateRecord.ResolveArrayIsConnected(path, portPostfix, Seq(), Seq(), false)
elaboratePending.addNode(resolveConnectedTask, Seq(
ElaborateRecord.ElaboratePortArray(path ++ portPostfix)))
case connects => throw new IllegalArgumentException(s"invalid connections to element $connects")
}
case _ => // everything else generated
connectedConstraints.connectionsByLinkPort(portPostfix, true) match {
case PortConnections.SingleConnect(constrName, constr) =>
resolvePortConnectivity(path, portPostfix, Some(constrName, constr))
case PortConnections.NotConnected =>
resolvePortConnectivity(path, portPostfix, None)
case connects => throw new IllegalArgumentException(s"invalid connections to element $connects")
}
}
}
}
// Process constraints, as in the block case
link.getConstraints.foreach { case (constrName, constr) =>
processParamConstraint(path, constrName, constr, constr)
processConnectedConstraint(path, constrName, constr, true)
}
}
protected def elaborateLinkArray(path: DesignPath): Unit = {
val link = resolve(path).asInstanceOf[wir.LinkArray]
val linkElements = ArrayValue.ExtractText(
constProp.getValue(path.asIndirect + IndirectStep.Elements).get)
val linkPortArrayElements = link.getModelPorts.collect {
case (portName, port: wir.PortArray) =>
val portElements = ArrayValue.ExtractText(
constProp.getValue(path.asIndirect + portName + IndirectStep.Elements).get)
constProp.setValue(path.asIndirect + portName + IndirectStep.Length, IntValue(portElements.size))
elaboratePending.setValue(ElaborateRecord.ElaboratePortArray(path + portName), None) // resolved in initPortsFromModel
portName -> portElements
}
// Propagate link-wide ELEMENTS to port ELEMENTS and inner-link ALLOCATED
link.getModelPorts.foreach {
case (portName, port: wir.PortArray) =>
linkPortArrayElements(portName).foreach { index =>
constProp.addDirectedEquality(
path.asIndirect + portName + index + IndirectStep.Elements,
path.asIndirect + IndirectStep.Elements,
path, s"$portName.$index (link-array ports-from-elts)")
}
linkElements.foreach { elementIndex =>
constProp.addDirectedEquality(
path.asIndirect + elementIndex + portName + IndirectStep.Allocated,
path.asIndirect + portName + IndirectStep.Elements,