@mica-js/simulator 2.5.2 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -4
- package/dist/device.d.ts +56 -1
- package/dist/index.d.ts +55 -1
- package/dist/index.js +1376 -456
- package/dist/index.js.map +1 -1
- package/dist/sandbox.js +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -125,26 +125,13 @@ function prepareTopology(nodes, pinEdges) {
|
|
|
125
125
|
let floatingComponents = 0;
|
|
126
126
|
const blocks = [];
|
|
127
127
|
const edgeBlocks = [];
|
|
128
|
-
const nodeComponents = new Array(nodes.length);
|
|
129
|
-
const nodeUniform = new Array(nodes.length);
|
|
130
|
-
let componentIndex = 0;
|
|
131
128
|
for (const component of groups.values()) {
|
|
132
|
-
let reference;
|
|
133
129
|
let floating = true;
|
|
134
|
-
|
|
135
|
-
for (const node of component.nodes) {
|
|
136
|
-
const potential = nodes[node].reference;
|
|
137
|
-
if (potential === void 0) continue;
|
|
130
|
+
for (const node of component.nodes) if (nodes[node].reference !== void 0) {
|
|
138
131
|
floating = false;
|
|
139
|
-
|
|
140
|
-
else if (potential !== reference) uniform = 0;
|
|
132
|
+
break;
|
|
141
133
|
}
|
|
142
134
|
if (floating) floatingComponents++;
|
|
143
|
-
for (const node of component.nodes) {
|
|
144
|
-
nodeComponents[node] = componentIndex;
|
|
145
|
-
nodeUniform[node] = uniform;
|
|
146
|
-
}
|
|
147
|
-
componentIndex++;
|
|
148
135
|
for (const block of solveBlocks(component, nodes)) {
|
|
149
136
|
const index = blocks.length;
|
|
150
137
|
blocks.push(prepareBlock(block));
|
|
@@ -155,9 +142,7 @@ function prepareTopology(nodes, pinEdges) {
|
|
|
155
142
|
blocks,
|
|
156
143
|
edgeBlocks,
|
|
157
144
|
components: groups.size,
|
|
158
|
-
floatingComponents
|
|
159
|
-
nodeComponents,
|
|
160
|
-
nodeUniform
|
|
145
|
+
floatingComponents
|
|
161
146
|
};
|
|
162
147
|
}
|
|
163
148
|
var cleanZero = (value) => value === 0 ? 0 : value;
|
|
@@ -365,10 +350,6 @@ function substitute(factorization, right, fail) {
|
|
|
365
350
|
}
|
|
366
351
|
return solution;
|
|
367
352
|
}
|
|
368
|
-
/** Changed pins publish in ascending board slot order; slots are already that order. */
|
|
369
|
-
function ascendingSlot(first, second) {
|
|
370
|
-
return first - second;
|
|
371
|
-
}
|
|
372
353
|
function sumPair(first, second) {
|
|
373
354
|
first = 0 + first;
|
|
374
355
|
const total = first + second;
|
|
@@ -539,15 +520,10 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
539
520
|
const updatedEdgeLists = new Array(pins.length);
|
|
540
521
|
const updatedEdgeSlots = [];
|
|
541
522
|
const replacedEdges = [];
|
|
542
|
-
const pendingPins = [];
|
|
543
|
-
const pendingSamples = [];
|
|
544
523
|
const seenSlots = new Int32Array(pins.length);
|
|
545
524
|
let seenStamp = 0;
|
|
546
525
|
/** Only the matrix partition is out of date; component arrays and buckets stay valid. */
|
|
547
526
|
let blocksStale = false;
|
|
548
|
-
/** Component arrays are copied before a local update mutates them, because admitted
|
|
549
|
-
* topology snapshots keep referencing the arrays they were created with. */
|
|
550
|
-
let componentArraysOwned = false;
|
|
551
527
|
const syncDegrees = (previous, next) => {
|
|
552
528
|
for (const edge of previous) if (next.indexOf(edge) === -1) {
|
|
553
529
|
nodeDegree[edge.from] = nodeDegree[edge.from] - 1;
|
|
@@ -572,10 +548,19 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
572
548
|
const emptySlots = Object.freeze([]);
|
|
573
549
|
/** Dirty slots supplied by the caller, deduplicated in place without an iterator per pin. */
|
|
574
550
|
const inputSlots = [];
|
|
575
|
-
/**
|
|
551
|
+
/**
|
|
552
|
+
* Changed pin slots of the last solve, valid in `[0, changedCount)`. The leaf path
|
|
553
|
+
* collects them in visit order and orders them only for the entries that return an
|
|
554
|
+
* array; the board publication sink is a slot bitmap. The array keeps its high-water
|
|
555
|
+
* length so a new batch never pays for clearing it.
|
|
556
|
+
*/
|
|
576
557
|
const changedSlots = [];
|
|
558
|
+
let changedCount = 0;
|
|
577
559
|
/** Admitted leaf shapes per pin slot; a pin normally alternates between two references. */
|
|
578
560
|
const leafPlans = new Array(pins.length);
|
|
561
|
+
/** Last topology check for the single-slot entry, independent of intent identity. */
|
|
562
|
+
const leafEpochs = new Int32Array(pins.length);
|
|
563
|
+
let leafEpoch = 1;
|
|
579
564
|
const leafSlots = [];
|
|
580
565
|
const leafIntents = [];
|
|
581
566
|
const leafEdges = [];
|
|
@@ -624,7 +609,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
624
609
|
* A replay therefore commits precomputed samples instead of re-deriving voltages, levels
|
|
625
610
|
* and canonical samples on every publication.
|
|
626
611
|
*/
|
|
627
|
-
const leafPlan = (slot, branch, target, edges) => {
|
|
612
|
+
const leafPlan = (slot, intent, branch, target, edges) => {
|
|
628
613
|
const node = pinNodes[slot];
|
|
629
614
|
const nodePins = nodeSlots[node];
|
|
630
615
|
const voltage = nodes[target].reference;
|
|
@@ -638,27 +623,32 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
638
623
|
planPins.push(pinned);
|
|
639
624
|
samples.push(sampleFor(voltage, pinLevel(pinned, voltage)));
|
|
640
625
|
}
|
|
641
|
-
return
|
|
626
|
+
return {
|
|
627
|
+
intent,
|
|
642
628
|
targetPin: branch.to,
|
|
643
629
|
resistance: branch.resistance,
|
|
644
630
|
target,
|
|
645
631
|
edges,
|
|
632
|
+
edgeHash: hashEdges(slot, edges),
|
|
646
633
|
node,
|
|
647
634
|
voltage,
|
|
648
635
|
slots: Object.freeze(slots),
|
|
649
636
|
pins: Object.freeze(planPins),
|
|
650
637
|
samples: Object.freeze(samples)
|
|
651
|
-
}
|
|
638
|
+
};
|
|
652
639
|
};
|
|
653
|
-
const admitLeaf = (slot, branch, target, edges) => {
|
|
654
|
-
const plan = leafPlan(slot, branch, target, edges);
|
|
640
|
+
const admitLeaf = (slot, intent, branch, target, edges) => {
|
|
641
|
+
const plan = leafPlan(slot, intent, branch, target, edges);
|
|
655
642
|
const plans = leafPlans[slot];
|
|
656
643
|
if (plans === void 0) {
|
|
657
644
|
leafPlans[slot] = [plan];
|
|
658
645
|
statistics.leafPlans++;
|
|
659
646
|
return;
|
|
660
647
|
}
|
|
661
|
-
for (const existing of plans) if (existing.targetPin === branch.to && existing.resistance === branch.resistance)
|
|
648
|
+
for (const existing of plans) if (existing.targetPin === branch.to && existing.resistance === branch.resistance) {
|
|
649
|
+
existing.intent = intent;
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
662
652
|
if (plans.length < 2) plans.push(plan);
|
|
663
653
|
else plans[plans.length - 1] = plan;
|
|
664
654
|
statistics.leafPlans++;
|
|
@@ -666,76 +656,137 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
666
656
|
const flushAdmissions = () => {
|
|
667
657
|
for (let index = 0; index < pendingAdmissions.length; index++) {
|
|
668
658
|
const admission = pendingAdmissions[index];
|
|
669
|
-
admitLeaf(admission.slot, admission.branch, admission.target, admission.edges);
|
|
659
|
+
admitLeaf(admission.slot, admission.intent, admission.branch, admission.target, admission.edges);
|
|
670
660
|
}
|
|
671
661
|
pendingAdmissions.length = 0;
|
|
672
662
|
};
|
|
673
663
|
const matchLeaf = (slot, intent) => {
|
|
674
|
-
if (prepared === void 0
|
|
664
|
+
if (prepared === void 0) return void 0;
|
|
675
665
|
const plans = leafPlans[slot];
|
|
676
666
|
if (plans === void 0) return void 0;
|
|
677
667
|
const previous = pinEdges[slot];
|
|
678
668
|
if (previous.length !== 1 || nodes[previous[0].to].reference === void 0) return void 0;
|
|
679
669
|
const from = pinNodes[slot];
|
|
680
670
|
if (nodeDegree[from] !== 1 || nodes[from].reference !== void 0) return void 0;
|
|
671
|
+
const p0 = plans[0];
|
|
672
|
+
if (p0.intent === intent) return nodes[p0.target].reference === void 0 ? void 0 : p0;
|
|
673
|
+
if (plans.length > 1) {
|
|
674
|
+
const p1 = plans[1];
|
|
675
|
+
if (p1.intent === intent) return nodes[p1.target].reference === void 0 ? void 0 : p1;
|
|
676
|
+
}
|
|
677
|
+
if (intent.length !== 1) return void 0;
|
|
681
678
|
const branch = intent[0];
|
|
682
679
|
for (let index = 0; index < plans.length; index++) {
|
|
683
680
|
const plan = plans[index];
|
|
684
681
|
if (plan.targetPin !== branch.to || plan.resistance !== branch.resistance) continue;
|
|
685
|
-
|
|
682
|
+
if (nodes[plan.target].reference === void 0) return void 0;
|
|
683
|
+
plan.intent = intent;
|
|
684
|
+
return plan;
|
|
686
685
|
}
|
|
687
686
|
};
|
|
688
687
|
/**
|
|
689
|
-
*
|
|
690
|
-
*
|
|
688
|
+
* Appends one changed slot. A whole corrected batch is ordered once, after collecting,
|
|
689
|
+
* so each leaf contributes its pins without paying for an insertion pass per pin.
|
|
691
690
|
*/
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
691
|
+
const pushChanged = (slot) => {
|
|
692
|
+
changedSlots[changedCount++] = slot;
|
|
693
|
+
};
|
|
694
|
+
/** Restores the ascending board order the public entries promise; callers opt in. */
|
|
695
|
+
const sortChanged = () => {
|
|
696
|
+
for (let index = 1; index < changedCount; index++) {
|
|
697
|
+
const slot = changedSlots[index];
|
|
698
|
+
let position = index;
|
|
699
|
+
while (position > 0 && changedSlots[position - 1] > slot) {
|
|
700
|
+
changedSlots[position] = changedSlots[position - 1];
|
|
701
|
+
position--;
|
|
702
|
+
}
|
|
703
|
+
changedSlots[position] = slot;
|
|
697
704
|
}
|
|
698
|
-
|
|
705
|
+
};
|
|
706
|
+
/** Hands every published slot to the board. The consumer is a slot bitmap. */
|
|
707
|
+
const emitChanged = (publish) => {
|
|
708
|
+
for (let index = 0; index < changedCount; index++) publish(changedSlots[index]);
|
|
709
|
+
};
|
|
710
|
+
/** A fresh ascending copy for the public solve entries; the buffer is reused. */
|
|
711
|
+
const copyChanged = () => {
|
|
712
|
+
if (changedCount === 0) return emptySlots;
|
|
713
|
+
sortChanged();
|
|
714
|
+
const result = new Array(changedCount);
|
|
715
|
+
for (let index = 0; index < changedCount; index++) result[index] = changedSlots[index];
|
|
716
|
+
return result;
|
|
717
|
+
};
|
|
718
|
+
const commitLeafEdges = (slot, target, voltage, intent, edges, precomputedHash) => {
|
|
719
|
+
const previousTarget = pinEdges[slot][0].to;
|
|
720
|
+
if (previousTarget !== target) {
|
|
721
|
+
nodeDegree[previousTarget] = nodeDegree[previousTarget] - 1;
|
|
722
|
+
nodeDegree[target] = nodeDegree[target] + 1;
|
|
723
|
+
}
|
|
724
|
+
publishedVoltages[pinNodes[slot]] = voltage;
|
|
725
|
+
intents[slot] = intent;
|
|
726
|
+
pinEdges[slot] = edges;
|
|
727
|
+
if (precomputedHash !== void 0) {
|
|
728
|
+
const previous = pinHashes[slot];
|
|
729
|
+
if (precomputedHash !== previous) {
|
|
730
|
+
pinHashes[slot] = precomputedHash;
|
|
731
|
+
topologyHash += precomputedHash - previous;
|
|
732
|
+
topologySpread ^= previous ^ precomputedHash;
|
|
733
|
+
}
|
|
734
|
+
} else setPinHash(slot, edges);
|
|
735
|
+
};
|
|
736
|
+
const finishLeaf = () => {
|
|
737
|
+
blocksStale = true;
|
|
738
|
+
statistics.solves++;
|
|
739
|
+
statistics.leafPropagations++;
|
|
699
740
|
};
|
|
700
741
|
/**
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
* No pin, sample, intent or degree changes before the whole batch is accepted.
|
|
742
|
+
* Once the single dirty slot is admitted, the whole batch is known to be valid.
|
|
743
|
+
* The plan visits one node's pins in board order, so no staging or merging is needed.
|
|
704
744
|
*/
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
745
|
+
const applySingleLeaf = (slot, intent, plan) => {
|
|
746
|
+
const { node, target, voltage } = plan;
|
|
747
|
+
changedCount = 0;
|
|
748
|
+
if (publishedVoltages[node] !== voltage) {
|
|
749
|
+
const planPins = plan.pins;
|
|
750
|
+
const planSamples = plan.samples;
|
|
751
|
+
const planSlots = plan.slots;
|
|
752
|
+
for (let index = 0; index < planPins.length; index++) {
|
|
753
|
+
const pin = planPins[index];
|
|
754
|
+
const sample = planSamples[index];
|
|
755
|
+
if (pin.sample === sample) continue;
|
|
756
|
+
pin.sample = sample;
|
|
757
|
+
changedSlots[changedCount++] = planSlots[index];
|
|
758
|
+
}
|
|
713
759
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
760
|
+
commitLeafEdges(slot, target, voltage, intent, plan.edges, plan.edgeHash);
|
|
761
|
+
finishLeaf();
|
|
762
|
+
};
|
|
763
|
+
/**
|
|
764
|
+
* Publishes the accepted leaf batch. The caller has already matched every dirty slot to
|
|
765
|
+
* an admitted shape (or proven the shapes below through the general solver), so each
|
|
766
|
+
* pin's sample is an independent function of its node's fixed reference: committing them
|
|
767
|
+
* in place is atomic. Slots are collected in visit order and ordered only when a public
|
|
768
|
+
* entry asks for the ascending array.
|
|
769
|
+
*/
|
|
770
|
+
const applyLeaf = (count) => {
|
|
771
|
+
changedCount = 0;
|
|
719
772
|
for (let position = 0; position < count; position++) {
|
|
720
773
|
const node = pinNodes[leafSlots[position]];
|
|
721
|
-
const target = leafTargets[position];
|
|
722
|
-
nodeComponents[node] = nodeComponents[target];
|
|
723
|
-
nodeUniform[node] = nodeUniform[target];
|
|
724
774
|
const plan = leafPayloads[position];
|
|
725
775
|
if (plan !== void 0) {
|
|
726
776
|
if (publishedVoltages[node] === plan.voltage) continue;
|
|
727
777
|
const planSlots = plan.slots;
|
|
778
|
+
const planPins = plan.pins;
|
|
779
|
+
const planSamples = plan.samples;
|
|
728
780
|
for (let at = 0; at < planSlots.length; at++) {
|
|
729
|
-
const pinned =
|
|
730
|
-
const sample =
|
|
781
|
+
const pinned = planPins[at];
|
|
782
|
+
const sample = planSamples[at];
|
|
731
783
|
if (pinned.sample === sample) continue;
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
pendingSamples.push(sample);
|
|
784
|
+
pinned.sample = sample;
|
|
785
|
+
pushChanged(planSlots[at]);
|
|
735
786
|
}
|
|
736
787
|
continue;
|
|
737
788
|
}
|
|
738
|
-
const voltage = nodes[
|
|
789
|
+
const voltage = nodes[leafTargets[position]].reference;
|
|
739
790
|
if (publishedVoltages[node] === voltage) continue;
|
|
740
791
|
const slots = nodeSlots[node];
|
|
741
792
|
for (let at = 0; at < slots.length; at++) {
|
|
@@ -743,80 +794,140 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
743
794
|
const pin = pins[slot];
|
|
744
795
|
const level = pin.low === void 0 ? "unknown" : voltage <= pin.low ? "low" : voltage >= pin.high ? "high" : "unknown";
|
|
745
796
|
const previous = pin.sample;
|
|
746
|
-
if (previous.voltage
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
pendingSamples.push(sampleFor(voltage, level));
|
|
750
|
-
}
|
|
797
|
+
if (previous.voltage === voltage && previous.level === level && previous.status === "valid") continue;
|
|
798
|
+
pin.sample = sampleFor(voltage, level);
|
|
799
|
+
pushChanged(slot);
|
|
751
800
|
}
|
|
752
801
|
}
|
|
753
|
-
for (let position = 0; position < pendingPins.length; position++) pendingPins[position].sample = pendingSamples[position];
|
|
754
802
|
for (let position = 0; position < count; position++) {
|
|
755
803
|
const slot = leafSlots[position];
|
|
756
804
|
const target = leafTargets[position];
|
|
757
|
-
|
|
758
|
-
publishedVoltages[pinNodes[slot]] = nodes[target].reference;
|
|
759
|
-
if (previousTarget !== target) {
|
|
760
|
-
nodeDegree[previousTarget] = nodeDegree[previousTarget] - 1;
|
|
761
|
-
nodeDegree[target] = nodeDegree[target] + 1;
|
|
762
|
-
}
|
|
763
|
-
intents[slot] = leafIntents[position];
|
|
764
|
-
const edges = leafEdges[position];
|
|
765
|
-
pinEdges[slot] = edges;
|
|
766
|
-
setPinHash(slot, edges);
|
|
805
|
+
commitLeafEdges(slot, target, nodes[target].reference, leafIntents[position], leafEdges[position], leafPayloads[position]?.edgeHash);
|
|
767
806
|
}
|
|
768
|
-
|
|
769
|
-
statistics.solves++;
|
|
770
|
-
statistics.leafPropagations++;
|
|
807
|
+
finishLeaf();
|
|
771
808
|
};
|
|
809
|
+
/** One complete single-slot dirty batch; leaves the publication buffer filled. */
|
|
810
|
+
function solveSingleSlot(slot) {
|
|
811
|
+
const plans = leafPlans[slot];
|
|
812
|
+
if (plans !== void 0) {
|
|
813
|
+
const intent = pins[slot].intent;
|
|
814
|
+
if (intent !== intents[slot]) {
|
|
815
|
+
if (leafEpochs[slot] === leafEpoch) {
|
|
816
|
+
const first = plans[0];
|
|
817
|
+
const plan = first.intent === intent ? first : plans[1];
|
|
818
|
+
if (plan?.intent === intent) {
|
|
819
|
+
applySingleLeaf(slot, intent, plan);
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const plan = matchLeaf(slot, intent);
|
|
824
|
+
if (plan !== void 0) {
|
|
825
|
+
leafEpochs[slot] = leafEpoch;
|
|
826
|
+
applySingleLeaf(slot, intent, plan);
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
inputSlots[0] = slot;
|
|
832
|
+
inputSlots.length = 1;
|
|
833
|
+
runSolve(inputSlots);
|
|
834
|
+
}
|
|
835
|
+
/**
|
|
836
|
+
* One admitted-shape probe over `slots[0, length)`. A slot whose shape this epoch already
|
|
837
|
+
* proved resolves through the same two-plan pointer comparison as `solveSingleSlot`; only
|
|
838
|
+
* the first switch of a shape pays for `matchLeaf`. Any slot without an admitted shape
|
|
839
|
+
* hands the whole batch to the general solver, which repeats the match.
|
|
840
|
+
*/
|
|
841
|
+
function solveBatch(slots, length) {
|
|
842
|
+
if (prepared !== void 0) {
|
|
843
|
+
const stamp = nextStamp();
|
|
844
|
+
let count = 0;
|
|
845
|
+
let complete = true;
|
|
846
|
+
for (let position = 0; position < length; position++) {
|
|
847
|
+
const slot = slots[position];
|
|
848
|
+
if (seenSlots[slot] === stamp) continue;
|
|
849
|
+
seenSlots[slot] = stamp;
|
|
850
|
+
const intent = pins[slot].intent;
|
|
851
|
+
if (intent === intents[slot]) continue;
|
|
852
|
+
let plan;
|
|
853
|
+
if (leafEpochs[slot] === leafEpoch) {
|
|
854
|
+
const plans = leafPlans[slot];
|
|
855
|
+
const first = plans[0];
|
|
856
|
+
const cached = first.intent === intent ? first : plans[1];
|
|
857
|
+
if (cached?.intent === intent) plan = cached;
|
|
858
|
+
}
|
|
859
|
+
if (plan === void 0) {
|
|
860
|
+
plan = matchLeaf(slot, intent);
|
|
861
|
+
if (plan === void 0) {
|
|
862
|
+
complete = false;
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
leafEpochs[slot] = leafEpoch;
|
|
866
|
+
}
|
|
867
|
+
leafSlots[count] = slot;
|
|
868
|
+
leafIntents[count] = intent;
|
|
869
|
+
leafEdges[count] = plan.edges;
|
|
870
|
+
leafTargets[count] = plan.target;
|
|
871
|
+
leafPayloads[count] = plan;
|
|
872
|
+
count++;
|
|
873
|
+
}
|
|
874
|
+
if (complete) {
|
|
875
|
+
if (count === 0) {
|
|
876
|
+
fillInput(slots, length);
|
|
877
|
+
runSolve(inputSlots);
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
applyLeaf(count);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
fillInput(slots, length);
|
|
885
|
+
runSolve(inputSlots);
|
|
886
|
+
}
|
|
887
|
+
/** Board-facing single-slot entry: solves and publishes without an intermediate array. */
|
|
888
|
+
function solveSlotInto(slot, publish) {
|
|
889
|
+
solveSingleSlot(slot);
|
|
890
|
+
if (publish !== void 0 && changedCount !== 0) emitChanged(publish);
|
|
891
|
+
}
|
|
892
|
+
/** Board-facing batch entry: solves and publishes without an intermediate array. */
|
|
893
|
+
function solveSlotBatchInto(slots, count, publish) {
|
|
894
|
+
solveBatch(slots, count);
|
|
895
|
+
if (publish !== void 0 && changedCount !== 0) emitChanged(publish);
|
|
896
|
+
}
|
|
772
897
|
return {
|
|
773
898
|
get stats() {
|
|
774
899
|
return Object.freeze({ ...statistics });
|
|
775
900
|
},
|
|
776
901
|
solve(changedPins) {
|
|
777
|
-
|
|
778
|
-
if (
|
|
902
|
+
runSolve(gatherPins(changedPins));
|
|
903
|
+
if (changedCount === 0) return unchangedPins;
|
|
904
|
+
sortChanged();
|
|
779
905
|
const published = [];
|
|
780
|
-
for (let position = 0; position <
|
|
906
|
+
for (let position = 0; position < changedCount; position++) published.push(pins[changedSlots[position]]);
|
|
781
907
|
return Object.freeze(published);
|
|
782
908
|
},
|
|
909
|
+
solveSlot(slot) {
|
|
910
|
+
solveSingleSlot(slot);
|
|
911
|
+
return copyChanged();
|
|
912
|
+
},
|
|
913
|
+
solveSlotInto,
|
|
914
|
+
solveSlotBatchInto,
|
|
783
915
|
solveSlots(changedSlotNumbers) {
|
|
784
|
-
if (changedSlotNumbers ===
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
inputSlots.length = 0;
|
|
788
|
-
for (const slot of changedSlotNumbers) inputSlots.push(slot);
|
|
916
|
+
if (typeof changedSlotNumbers === "number") {
|
|
917
|
+
solveSingleSlot(changedSlotNumbers);
|
|
918
|
+
return copyChanged();
|
|
789
919
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
const stamp = nextStamp();
|
|
794
|
-
let count = 0;
|
|
795
|
-
let complete = true;
|
|
796
|
-
for (const slot of probe) {
|
|
797
|
-
if (seenSlots[slot] === stamp) continue;
|
|
798
|
-
seenSlots[slot] = stamp;
|
|
799
|
-
const intent = pins[slot].intent;
|
|
800
|
-
if (intent === intents[slot]) continue;
|
|
801
|
-
const plan = matchLeaf(slot, intent);
|
|
802
|
-
if (plan === void 0) {
|
|
803
|
-
complete = false;
|
|
804
|
-
break;
|
|
805
|
-
}
|
|
806
|
-
leafSlots[count] = slot;
|
|
807
|
-
leafIntents[count] = intent;
|
|
808
|
-
leafEdges[count] = plan.edges;
|
|
809
|
-
leafTargets[count] = plan.target;
|
|
810
|
-
leafPayloads[count] = plan;
|
|
811
|
-
count++;
|
|
812
|
-
}
|
|
813
|
-
if (complete) {
|
|
814
|
-
if (count === 0) return runSolve(gathered());
|
|
815
|
-
applyLeaf(count);
|
|
816
|
-
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
817
|
-
}
|
|
920
|
+
if (changedSlotNumbers === void 0) {
|
|
921
|
+
runSolve(void 0);
|
|
922
|
+
return copyChanged();
|
|
818
923
|
}
|
|
819
|
-
|
|
924
|
+
inputSlots.length = 0;
|
|
925
|
+
if (Array.isArray(changedSlotNumbers) || ArrayBuffer.isView(changedSlotNumbers) && Symbol.iterator in changedSlotNumbers) {
|
|
926
|
+
const indexed = changedSlotNumbers;
|
|
927
|
+
for (let position = 0; position < indexed.length; position++) inputSlots.push(indexed[position]);
|
|
928
|
+
} else for (const slot of changedSlotNumbers) inputSlots.push(slot);
|
|
929
|
+
solveBatch(inputSlots, inputSlots.length);
|
|
930
|
+
return copyChanged();
|
|
820
931
|
}
|
|
821
932
|
};
|
|
822
933
|
function gatherPins(changedPins) {
|
|
@@ -825,23 +936,27 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
825
936
|
for (const pin of changedPins) inputSlots.push(requirePin(pin));
|
|
826
937
|
return inputSlots;
|
|
827
938
|
}
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
inputSlots.length =
|
|
831
|
-
for (
|
|
832
|
-
return inputSlots;
|
|
939
|
+
/** Places a caller-supplied batch into the solver input buffer without allocating. */
|
|
940
|
+
function fillInput(slots, length) {
|
|
941
|
+
inputSlots.length = length;
|
|
942
|
+
for (let position = 0; position < length; position++) inputSlots[position] = slots[position];
|
|
833
943
|
}
|
|
834
944
|
/**
|
|
835
945
|
* One atomically published solve over `input` (dirty slots) or, when it is omitted, over
|
|
836
|
-
* every pin.
|
|
946
|
+
* every pin. The published slots end up in the ascending buffer.
|
|
837
947
|
*/
|
|
838
948
|
function runSolve(input) {
|
|
949
|
+
if (++leafEpoch === 2147483647) {
|
|
950
|
+
leafEpochs.fill(0);
|
|
951
|
+
leafEpoch = 1;
|
|
952
|
+
}
|
|
839
953
|
const initial = prepared === void 0;
|
|
840
954
|
pendingAdmissions.length = 0;
|
|
955
|
+
changedCount = 0;
|
|
841
956
|
if (prepared !== void 0 && !blocksStale && input !== void 0 && input.length === 0) {
|
|
842
957
|
statistics.solves++;
|
|
843
958
|
statistics.cacheHits += prepared.blocks.length;
|
|
844
|
-
return
|
|
959
|
+
return;
|
|
845
960
|
}
|
|
846
961
|
updatedSlots.length = 0;
|
|
847
962
|
updatedIntents.length = 0;
|
|
@@ -872,7 +987,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
872
987
|
}
|
|
873
988
|
if (count > 0) {
|
|
874
989
|
applyLeaf(count);
|
|
875
|
-
return
|
|
990
|
+
return;
|
|
876
991
|
}
|
|
877
992
|
stamp = nextStamp();
|
|
878
993
|
}
|
|
@@ -914,6 +1029,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
914
1029
|
if (leafSwitch && !leafShape) leafSwitch = false;
|
|
915
1030
|
if (leafShape && intent.length === 1 && edges[0] !== previous[0]) pendingAdmissions.push({
|
|
916
1031
|
slot: index,
|
|
1032
|
+
intent,
|
|
917
1033
|
branch: intent[0],
|
|
918
1034
|
target: edges[0].to,
|
|
919
1035
|
edges
|
|
@@ -932,7 +1048,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
932
1048
|
}
|
|
933
1049
|
flushAdmissions();
|
|
934
1050
|
applyLeaf(updatedSlots.length);
|
|
935
|
-
return
|
|
1051
|
+
return;
|
|
936
1052
|
}
|
|
937
1053
|
if (blocksStale) topologyChanged = true;
|
|
938
1054
|
const updated = updatedSlots.length;
|
|
@@ -945,7 +1061,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
945
1061
|
statistics.solves++;
|
|
946
1062
|
statistics.cacheHits += prepared.blocks.length;
|
|
947
1063
|
flushAdmissions();
|
|
948
|
-
return
|
|
1064
|
+
return;
|
|
949
1065
|
}
|
|
950
1066
|
let topology = prepared;
|
|
951
1067
|
let bucket;
|
|
@@ -1096,7 +1212,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1096
1212
|
for (const [node, voltage] of voltages) if (publishedVoltages[node] !== voltage) pendingVoltages.set(node, voltage);
|
|
1097
1213
|
}
|
|
1098
1214
|
const pending = /* @__PURE__ */ new Map();
|
|
1099
|
-
|
|
1215
|
+
changedCount = 0;
|
|
1100
1216
|
for (const [node, voltage] of pendingVoltages) {
|
|
1101
1217
|
const slots = nodeSlots[node];
|
|
1102
1218
|
for (let at = 0; at < slots.length; at++) {
|
|
@@ -1106,7 +1222,7 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1106
1222
|
const status = voltage === null ? "floating" : "valid";
|
|
1107
1223
|
const previous = pin.sample;
|
|
1108
1224
|
const dirty = previous.voltage !== voltage || previous.level !== level || previous.status !== status;
|
|
1109
|
-
if (dirty) changedSlots
|
|
1225
|
+
if (dirty) changedSlots[changedCount++] = slot;
|
|
1110
1226
|
if (dirty || statistics.solves === 0) pending.set(pin, Object.freeze({
|
|
1111
1227
|
voltage: voltage === null ? null : cleanZero(voltage),
|
|
1112
1228
|
level,
|
|
@@ -1114,7 +1230,15 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1114
1230
|
}));
|
|
1115
1231
|
}
|
|
1116
1232
|
}
|
|
1117
|
-
|
|
1233
|
+
for (let index = 1; index < changedCount; index++) {
|
|
1234
|
+
const slot = changedSlots[index];
|
|
1235
|
+
let position = index;
|
|
1236
|
+
while (position > 0 && changedSlots[position - 1] > slot) {
|
|
1237
|
+
changedSlots[position] = changedSlots[position - 1];
|
|
1238
|
+
position--;
|
|
1239
|
+
}
|
|
1240
|
+
changedSlots[position] = slot;
|
|
1241
|
+
}
|
|
1118
1242
|
for (const [pin, sample] of pending) pin.sample = sample;
|
|
1119
1243
|
for (const [node, voltage] of pendingVoltages) publishedVoltages[node] = voltage;
|
|
1120
1244
|
for (let position = 0; position < updated; position++) {
|
|
@@ -1131,7 +1255,6 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1131
1255
|
for (const [index, block] of dirtyBlocks) topology.blocks[index] = block;
|
|
1132
1256
|
}
|
|
1133
1257
|
prepared = topology;
|
|
1134
|
-
componentArraysOwned = false;
|
|
1135
1258
|
blocksStale = false;
|
|
1136
1259
|
if (topologyChanged) {
|
|
1137
1260
|
const admitted = restored !== void 0 && topology === restored.topology ? restored : {
|
|
@@ -1150,63 +1273,101 @@ function createElectricalNetwork(inputPins, inputNets) {
|
|
|
1150
1273
|
statistics.cacheHits += cacheHits;
|
|
1151
1274
|
statistics.topologyPrepares += topologyPrepares;
|
|
1152
1275
|
flushAdmissions();
|
|
1153
|
-
return changedSlots.length === 0 ? emptySlots : changedSlots;
|
|
1154
1276
|
}
|
|
1155
1277
|
}
|
|
1156
1278
|
//#endregion
|
|
1157
1279
|
//#region src/queue.ts
|
|
1280
|
+
/**
|
|
1281
|
+
* Binary min-heap over parallel arrays. Every push/pop used to allocate a `{ time, order,
|
|
1282
|
+
* value }` record; a timed board pushes one continuation per suspension, so the flat form
|
|
1283
|
+
* keeps the scheduler loop allocation-free.
|
|
1284
|
+
*/
|
|
1158
1285
|
var EventQueue = class {
|
|
1159
|
-
|
|
1286
|
+
times = [];
|
|
1287
|
+
orders = [];
|
|
1288
|
+
values = [];
|
|
1289
|
+
size = 0;
|
|
1160
1290
|
sequence = 0;
|
|
1161
1291
|
get time() {
|
|
1162
|
-
return this.
|
|
1292
|
+
return this.size === 0 ? Infinity : this.times[0];
|
|
1163
1293
|
}
|
|
1164
1294
|
push(time, value) {
|
|
1165
|
-
const
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
let at = this.heap.length;
|
|
1171
|
-
this.heap.push(task);
|
|
1172
|
-
while (at > 0) {
|
|
1173
|
-
const parent = at - 1 >>> 1;
|
|
1174
|
-
if (!this.before(task, this.heap[parent])) break;
|
|
1175
|
-
this.heap[at] = this.heap[parent];
|
|
1176
|
-
at = parent;
|
|
1177
|
-
}
|
|
1178
|
-
this.heap[at] = task;
|
|
1295
|
+
const at = this.size++;
|
|
1296
|
+
this.times[at] = time;
|
|
1297
|
+
this.orders[at] = this.sequence++;
|
|
1298
|
+
this.values[at] = value;
|
|
1299
|
+
this.siftUp(at);
|
|
1179
1300
|
}
|
|
1180
1301
|
pop() {
|
|
1181
|
-
const
|
|
1182
|
-
const last = this.
|
|
1183
|
-
if (
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
}
|
|
1192
|
-
this.heap[at] = last;
|
|
1302
|
+
const value = this.values[0];
|
|
1303
|
+
const last = --this.size;
|
|
1304
|
+
if (last >= 0) {
|
|
1305
|
+
this.times[0] = this.times[last];
|
|
1306
|
+
this.orders[0] = this.orders[last];
|
|
1307
|
+
this.values[0] = this.values[last];
|
|
1308
|
+
this.times[last] = 0;
|
|
1309
|
+
this.orders[last] = 0;
|
|
1310
|
+
this.values[last] = void 0;
|
|
1311
|
+
this.siftDown();
|
|
1193
1312
|
}
|
|
1194
|
-
return
|
|
1313
|
+
return value;
|
|
1195
1314
|
}
|
|
1196
1315
|
clear() {
|
|
1197
|
-
this.
|
|
1316
|
+
this.size = 0;
|
|
1317
|
+
}
|
|
1318
|
+
before(first, second) {
|
|
1319
|
+
const times = this.times;
|
|
1320
|
+
const a = times[first];
|
|
1321
|
+
const b = times[second];
|
|
1322
|
+
return a < b || a === b && this.orders[first] < this.orders[second];
|
|
1323
|
+
}
|
|
1324
|
+
swap(first, second) {
|
|
1325
|
+
const times = this.times;
|
|
1326
|
+
const orders = this.orders;
|
|
1327
|
+
const values = this.values;
|
|
1328
|
+
const time = times[first];
|
|
1329
|
+
const order = orders[first];
|
|
1330
|
+
const value = values[first];
|
|
1331
|
+
times[first] = times[second];
|
|
1332
|
+
orders[first] = orders[second];
|
|
1333
|
+
values[first] = values[second];
|
|
1334
|
+
times[second] = time;
|
|
1335
|
+
orders[second] = order;
|
|
1336
|
+
values[second] = value;
|
|
1337
|
+
}
|
|
1338
|
+
siftUp(at) {
|
|
1339
|
+
while (at > 0) {
|
|
1340
|
+
const parent = at - 1 >>> 1;
|
|
1341
|
+
if (!this.before(at, parent)) break;
|
|
1342
|
+
this.swap(at, parent);
|
|
1343
|
+
at = parent;
|
|
1344
|
+
}
|
|
1198
1345
|
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1346
|
+
siftDown() {
|
|
1347
|
+
const size = this.size;
|
|
1348
|
+
let at = 0;
|
|
1349
|
+
for (;;) {
|
|
1350
|
+
const left = at * 2 + 1;
|
|
1351
|
+
if (left >= size) break;
|
|
1352
|
+
const right = left + 1;
|
|
1353
|
+
const child = right < size && this.before(right, left) ? right : left;
|
|
1354
|
+
if (!this.before(child, at)) break;
|
|
1355
|
+
this.swap(at, child);
|
|
1356
|
+
at = child;
|
|
1357
|
+
}
|
|
1201
1358
|
}
|
|
1202
1359
|
};
|
|
1203
1360
|
var ActivationQueue = class {
|
|
1204
1361
|
entries = [];
|
|
1205
1362
|
words = [];
|
|
1363
|
+
/**
|
|
1364
|
+
* Active word indices in `[0, activeCount)`. The array keeps its high-water length:
|
|
1365
|
+
* truncating it would right-trim the backing store on every boundary.
|
|
1366
|
+
*/
|
|
1206
1367
|
activeWords = [];
|
|
1207
|
-
|
|
1368
|
+
activeCount = 0;
|
|
1208
1369
|
get size() {
|
|
1209
|
-
return this.
|
|
1370
|
+
return this.activeCount;
|
|
1210
1371
|
}
|
|
1211
1372
|
register(entry) {
|
|
1212
1373
|
return this.entries.push(entry) - 1;
|
|
@@ -1214,53 +1375,75 @@ var ActivationQueue = class {
|
|
|
1214
1375
|
add(slot) {
|
|
1215
1376
|
const word = slot >>> 5;
|
|
1216
1377
|
const previous = this.words[word] ?? 0;
|
|
1217
|
-
if (previous === 0) this.activeWords.
|
|
1378
|
+
if (previous === 0) this.activeWords[this.activeCount++] = word;
|
|
1218
1379
|
this.words[word] = previous | 1 << (slot & 31);
|
|
1219
1380
|
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1381
|
+
/**
|
|
1382
|
+
* Visits every active entry in board order without materialising a batch. Entries added
|
|
1383
|
+
* by the visitor land past the visited range and survive.
|
|
1384
|
+
*/
|
|
1385
|
+
drain(visit) {
|
|
1386
|
+
const active = this.activeWords;
|
|
1387
|
+
const visited = this.activeCount;
|
|
1388
|
+
if (visited === 0) return;
|
|
1389
|
+
if (visited > 1) for (let index = 1; index < visited; index++) {
|
|
1390
|
+
const word = active[index];
|
|
1391
|
+
let position = index;
|
|
1392
|
+
while (position > 0 && active[position - 1] > word) {
|
|
1393
|
+
active[position] = active[position - 1];
|
|
1394
|
+
position--;
|
|
1395
|
+
}
|
|
1396
|
+
active[position] = word;
|
|
1397
|
+
}
|
|
1398
|
+
for (let index = 0; index < visited; index++) {
|
|
1399
|
+
const word = active[index];
|
|
1225
1400
|
let bits = this.words[word];
|
|
1226
1401
|
this.words[word] = 0;
|
|
1402
|
+
const base = word * 32;
|
|
1227
1403
|
while (bits !== 0) {
|
|
1228
|
-
|
|
1229
|
-
batch.push(this.entries[word * 32 + bit]);
|
|
1404
|
+
visit(this.entries[base + 31 - Math.clz32(bits & -bits)]);
|
|
1230
1405
|
bits &= bits - 1;
|
|
1231
1406
|
}
|
|
1232
1407
|
}
|
|
1233
|
-
this.
|
|
1234
|
-
|
|
1408
|
+
if (this.activeCount === visited) this.activeCount = 0;
|
|
1409
|
+
else {
|
|
1410
|
+
for (let index = visited; index < this.activeCount; index++) active[index - visited] = active[index];
|
|
1411
|
+
this.activeCount -= visited;
|
|
1412
|
+
}
|
|
1235
1413
|
}
|
|
1236
1414
|
clear() {
|
|
1237
1415
|
this.entries.length = 0;
|
|
1238
1416
|
this.words.length = 0;
|
|
1239
1417
|
this.activeWords.length = 0;
|
|
1240
|
-
this.
|
|
1418
|
+
this.activeCount = 0;
|
|
1241
1419
|
}
|
|
1242
1420
|
};
|
|
1243
1421
|
var Fifo = class {
|
|
1244
1422
|
values = [];
|
|
1245
1423
|
head = 0;
|
|
1424
|
+
tail = 0;
|
|
1246
1425
|
get length() {
|
|
1247
|
-
return this.
|
|
1426
|
+
return this.tail - this.head;
|
|
1248
1427
|
}
|
|
1249
1428
|
push(value) {
|
|
1250
|
-
this.values.
|
|
1429
|
+
this.values[this.tail++] = value;
|
|
1251
1430
|
}
|
|
1252
1431
|
shift() {
|
|
1253
1432
|
const value = this.values[this.head];
|
|
1254
1433
|
this.values[this.head++] = void 0;
|
|
1255
|
-
if (this.head === this.
|
|
1434
|
+
if (this.head === this.tail) {
|
|
1435
|
+
this.head = 0;
|
|
1436
|
+
this.tail = 0;
|
|
1437
|
+
}
|
|
1256
1438
|
return value;
|
|
1257
1439
|
}
|
|
1258
1440
|
clear() {
|
|
1259
1441
|
this.values.length = 0;
|
|
1260
1442
|
this.head = 0;
|
|
1443
|
+
this.tail = 0;
|
|
1261
1444
|
}
|
|
1262
1445
|
drain() {
|
|
1263
|
-
const result = this.values.slice(this.head);
|
|
1446
|
+
const result = this.values.slice(this.head, this.tail);
|
|
1264
1447
|
this.clear();
|
|
1265
1448
|
return result;
|
|
1266
1449
|
}
|
|
@@ -1951,6 +2134,25 @@ function standalone(code, cause, phase) {
|
|
|
1951
2134
|
//#endregion
|
|
1952
2135
|
//#region src/runtime/instance.ts
|
|
1953
2136
|
var EMPTY_MESSAGES = Object.freeze([]);
|
|
2137
|
+
var jitModels = /* @__PURE__ */ new WeakMap();
|
|
2138
|
+
/** Retain the binding identity when adapters reuse the same immutable function table. */
|
|
2139
|
+
function bindJitFunctions(base, functions) {
|
|
2140
|
+
if (functions.length === 0) return base;
|
|
2141
|
+
let models = jitModels.get(base);
|
|
2142
|
+
const cached = models?.get(functions);
|
|
2143
|
+
if (cached !== void 0) return cached;
|
|
2144
|
+
const model = validateDeviceIR({
|
|
2145
|
+
...base,
|
|
2146
|
+
declarations: base.declarations.concat(functions)
|
|
2147
|
+
});
|
|
2148
|
+
for (const declaration of model.declarations.slice(base.declarations.length)) if (declaration.kind !== "function") throw new TypeError("Expected JIT function declaration");
|
|
2149
|
+
if (models === void 0) {
|
|
2150
|
+
models = /* @__PURE__ */ new WeakMap();
|
|
2151
|
+
jitModels.set(base, models);
|
|
2152
|
+
}
|
|
2153
|
+
models.set(functions, model);
|
|
2154
|
+
return model;
|
|
2155
|
+
}
|
|
1954
2156
|
/** Membership test without allocating: every key of `next` is already in `previous`. */
|
|
1955
2157
|
function hasEvery(previous, next) {
|
|
1956
2158
|
for (const slot of next.keys()) if (!previous.has(slot)) return false;
|
|
@@ -1976,6 +2178,70 @@ function createContext(owner, board) {
|
|
|
1976
2178
|
loadBytes: (name, bytes, ...offset) => owner.loadBytes(name, bytes, offset.length ? offset[0] : 0)
|
|
1977
2179
|
});
|
|
1978
2180
|
}
|
|
2181
|
+
/**
|
|
2182
|
+
* State map whose scalar direct-bank writes are synced lazily on read.
|
|
2183
|
+
*
|
|
2184
|
+
* Direct JIT writes update `stateValues` and invalidate `stateSlots` at every boundary,
|
|
2185
|
+
* while no observer or host reads `instance.states` until a reaction, observation,
|
|
2186
|
+
* fallback or external query accesses it. Deferring the string-keyed `Map.set` calls
|
|
2187
|
+
* until an actual read removes redundant map updates across resident GPIO edges while
|
|
2188
|
+
* keeping `instance.states` an exact `Map<string, unknown>` at every read site.
|
|
2189
|
+
*/
|
|
2190
|
+
var StateMirrorMap = class extends Map {
|
|
2191
|
+
names = [];
|
|
2192
|
+
bankValues = [];
|
|
2193
|
+
dirty = /* @__PURE__ */ new Int32Array(0);
|
|
2194
|
+
pending = false;
|
|
2195
|
+
bindDirectBank(names, values) {
|
|
2196
|
+
this.names = names;
|
|
2197
|
+
this.bankValues = values;
|
|
2198
|
+
this.dirty = new Int32Array(Math.ceil(names.length / 32));
|
|
2199
|
+
}
|
|
2200
|
+
markDirectDirty(word, bits) {
|
|
2201
|
+
this.dirty[word] |= bits;
|
|
2202
|
+
this.pending = true;
|
|
2203
|
+
}
|
|
2204
|
+
flush() {
|
|
2205
|
+
if (!this.pending) return;
|
|
2206
|
+
this.pending = false;
|
|
2207
|
+
const { names, bankValues, dirty } = this;
|
|
2208
|
+
for (let word = 0; word < dirty.length; word++) {
|
|
2209
|
+
let bits = dirty[word];
|
|
2210
|
+
if (bits === 0) continue;
|
|
2211
|
+
dirty[word] = 0;
|
|
2212
|
+
const base = word << 5;
|
|
2213
|
+
while (bits !== 0) {
|
|
2214
|
+
const index = base + 31 - Math.clz32(bits & -bits);
|
|
2215
|
+
super.set(names[index], bankValues[index]);
|
|
2216
|
+
bits &= bits - 1;
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
get(key) {
|
|
2221
|
+
if (this.pending) this.flush();
|
|
2222
|
+
return super.get(key);
|
|
2223
|
+
}
|
|
2224
|
+
set(key, value) {
|
|
2225
|
+
if (this.pending) this.flush();
|
|
2226
|
+
return super.set(key, value);
|
|
2227
|
+
}
|
|
2228
|
+
entries() {
|
|
2229
|
+
if (this.pending) this.flush();
|
|
2230
|
+
return super.entries();
|
|
2231
|
+
}
|
|
2232
|
+
values() {
|
|
2233
|
+
if (this.pending) this.flush();
|
|
2234
|
+
return super.values();
|
|
2235
|
+
}
|
|
2236
|
+
forEach(callbackfn, thisArg) {
|
|
2237
|
+
if (this.pending) this.flush();
|
|
2238
|
+
super.forEach(callbackfn, thisArg);
|
|
2239
|
+
}
|
|
2240
|
+
[Symbol.iterator]() {
|
|
2241
|
+
if (this.pending) this.flush();
|
|
2242
|
+
return super[Symbol.iterator]();
|
|
2243
|
+
}
|
|
2244
|
+
};
|
|
1979
2245
|
var Instance = class {
|
|
1980
2246
|
board;
|
|
1981
2247
|
entry;
|
|
@@ -1983,7 +2249,7 @@ var Instance = class {
|
|
|
1983
2249
|
declarations = /* @__PURE__ */ new Map();
|
|
1984
2250
|
props = /* @__PURE__ */ new Map();
|
|
1985
2251
|
constants = /* @__PURE__ */ new Map();
|
|
1986
|
-
states =
|
|
2252
|
+
states = new StateMirrorMap();
|
|
1987
2253
|
directBank;
|
|
1988
2254
|
memories = /* @__PURE__ */ new Map();
|
|
1989
2255
|
memoryViews = /* @__PURE__ */ new Map();
|
|
@@ -2001,6 +2267,9 @@ var Instance = class {
|
|
|
2001
2267
|
processes = [];
|
|
2002
2268
|
reactions = [];
|
|
2003
2269
|
derived = /* @__PURE__ */ new Map();
|
|
2270
|
+
/** One-entry memo for repeated `read('derived', name)` calls inside a body. */
|
|
2271
|
+
derivedName;
|
|
2272
|
+
derivedEntry;
|
|
2004
2273
|
derivedBuffers = /* @__PURE__ */ new Map();
|
|
2005
2274
|
stateResources = /* @__PURE__ */ new Map();
|
|
2006
2275
|
memoryResources = /* @__PURE__ */ new Map();
|
|
@@ -2011,10 +2280,18 @@ var Instance = class {
|
|
|
2011
2280
|
staticObservations = /* @__PURE__ */ new WeakMap();
|
|
2012
2281
|
resistorIntents = /* @__PURE__ */ new Map();
|
|
2013
2282
|
sampleEntries = /* @__PURE__ */ new Map();
|
|
2283
|
+
intentEntries = /* @__PURE__ */ new Map();
|
|
2014
2284
|
stateSlots = /* @__PURE__ */ new Map();
|
|
2015
2285
|
/** Per-instance handles for the fixed resources a resumable process body binds. */
|
|
2016
2286
|
resourceHandles = /* @__PURE__ */ new Map();
|
|
2017
2287
|
memoryPlans = /* @__PURE__ */ new Map();
|
|
2288
|
+
/** 按 memory 维护的代码代际;只有 code region 内写入才递增。 */
|
|
2289
|
+
codeVersions = /* @__PURE__ */ new Map();
|
|
2290
|
+
/**
|
|
2291
|
+
* Every memory name any attached JIT binds as a code region. A write outside this set
|
|
2292
|
+
* cannot affect a JIT, so the store path skips the region scans entirely.
|
|
2293
|
+
*/
|
|
2294
|
+
codeMemories = /* @__PURE__ */ new Set();
|
|
2018
2295
|
observationFrames = /* @__PURE__ */ new WeakMap();
|
|
2019
2296
|
evaluating = /* @__PURE__ */ new Set();
|
|
2020
2297
|
random;
|
|
@@ -2031,7 +2308,8 @@ var Instance = class {
|
|
|
2031
2308
|
if ("name" in declaration) this.declarations.set(declaration.name, declaration);
|
|
2032
2309
|
if (declaration.kind === "state" || declaration.kind === "memory" || declaration.kind === "pin") (declaration.kind === "state" ? this.stateResources : declaration.kind === "memory" ? this.memoryResources : this.sampleResources).set(declaration.name, {
|
|
2033
2310
|
version: 0,
|
|
2034
|
-
subscribers: /* @__PURE__ */ new Set()
|
|
2311
|
+
subscribers: /* @__PURE__ */ new Set(),
|
|
2312
|
+
waiters: void 0
|
|
2035
2313
|
});
|
|
2036
2314
|
}
|
|
2037
2315
|
dataObject(props);
|
|
@@ -2045,11 +2323,7 @@ var Instance = class {
|
|
|
2045
2323
|
return this.readSample(ref);
|
|
2046
2324
|
},
|
|
2047
2325
|
intent: (pin, value) => {
|
|
2048
|
-
|
|
2049
|
-
if (target.intent === value) return;
|
|
2050
|
-
target.intent = value;
|
|
2051
|
-
board.dirtyElectricalSlots.add(board.electricalSlot(target));
|
|
2052
|
-
board.electricalDirty = true;
|
|
2326
|
+
this.writeIntent(pin, value);
|
|
2053
2327
|
},
|
|
2054
2328
|
resistor: (pin, resistance) => {
|
|
2055
2329
|
const r = electricalNumber(resistance);
|
|
@@ -2061,7 +2335,7 @@ var Instance = class {
|
|
|
2061
2335
|
const plan = this.memoryPlan(memory);
|
|
2062
2336
|
const size = kind.startsWith("load8") ? 1 : kind.startsWith("load16") ? 2 : 4;
|
|
2063
2337
|
const at = range(plan.bytes, offset, size);
|
|
2064
|
-
const dependencies = this.
|
|
2338
|
+
const dependencies = this.dependencies();
|
|
2065
2339
|
if (dependencies) dependencies.set(plan.slot, plan.slot.version);
|
|
2066
2340
|
if (size === 1) return plan.bytes[at];
|
|
2067
2341
|
return size === 2 ? plan.view.getUint16(at, kind.endsWith("le")) : plan.view.getUint32(at, kind.endsWith("le"));
|
|
@@ -2074,7 +2348,7 @@ var Instance = class {
|
|
|
2074
2348
|
if (size === 1) plan.bytes[at] = n;
|
|
2075
2349
|
else if (size === 2) plan.view.setUint16(at, n, kind.endsWith("le"));
|
|
2076
2350
|
else plan.view.setUint32(at, n, kind.endsWith("le"));
|
|
2077
|
-
this.memoryChangedPlan(plan);
|
|
2351
|
+
this.memoryChangedPlan(plan, at, size);
|
|
2078
2352
|
},
|
|
2079
2353
|
copyBytes: (memory, offset, length) => {
|
|
2080
2354
|
this.track(this.memoryResources, String(memory));
|
|
@@ -2099,8 +2373,13 @@ var Instance = class {
|
|
|
2099
2373
|
track(resources, name) {
|
|
2100
2374
|
this.trackSlot(resources.get(name));
|
|
2101
2375
|
}
|
|
2376
|
+
/** Innermost dependency sink, or `undefined` outside an evaluation. */
|
|
2377
|
+
dependencies() {
|
|
2378
|
+
const stack = this.dependencyStack;
|
|
2379
|
+
return stack.length === 0 ? void 0 : stack[stack.length - 1];
|
|
2380
|
+
}
|
|
2102
2381
|
trackSlot(slot) {
|
|
2103
|
-
const dependencies = this.
|
|
2382
|
+
const dependencies = this.dependencies();
|
|
2104
2383
|
if (dependencies) dependencies.set(slot, slot.version);
|
|
2105
2384
|
}
|
|
2106
2385
|
/** Read-only evidence that a process frame bound per-instance resource handles. */
|
|
@@ -2120,6 +2399,13 @@ var Instance = class {
|
|
|
2120
2399
|
for (const [slot, version] of dependencies) if (slot.version !== version) return false;
|
|
2121
2400
|
return true;
|
|
2122
2401
|
}
|
|
2402
|
+
/** Publishes a derived cache entry; the memo must follow every table write. */
|
|
2403
|
+
setDerived(name, entry) {
|
|
2404
|
+
if (this.derivedName === name && this.derivedEntry === entry) return;
|
|
2405
|
+
this.derived.set(name, entry);
|
|
2406
|
+
this.derivedName = name;
|
|
2407
|
+
this.derivedEntry = entry;
|
|
2408
|
+
}
|
|
2123
2409
|
evaluate(compute, target) {
|
|
2124
2410
|
const dependencies = target ?? /* @__PURE__ */ new Map();
|
|
2125
2411
|
this.dependencyStack.push(dependencies);
|
|
@@ -2130,7 +2416,7 @@ var Instance = class {
|
|
|
2130
2416
|
};
|
|
2131
2417
|
} finally {
|
|
2132
2418
|
this.dependencyStack.pop();
|
|
2133
|
-
const parent = this.
|
|
2419
|
+
const parent = this.dependencies();
|
|
2134
2420
|
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
2135
2421
|
}
|
|
2136
2422
|
}
|
|
@@ -2141,7 +2427,7 @@ var Instance = class {
|
|
|
2141
2427
|
return snapshot(this.source(path));
|
|
2142
2428
|
} finally {
|
|
2143
2429
|
this.dependencyStack.pop();
|
|
2144
|
-
const parent = this.
|
|
2430
|
+
const parent = this.dependencies();
|
|
2145
2431
|
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
2146
2432
|
}
|
|
2147
2433
|
}
|
|
@@ -2202,8 +2488,9 @@ var Instance = class {
|
|
|
2202
2488
|
return this.states.get(name);
|
|
2203
2489
|
}
|
|
2204
2490
|
if (kind === "derived") {
|
|
2205
|
-
let cached = this.derived.get(name);
|
|
2206
|
-
|
|
2491
|
+
let cached = name === this.derivedName ? this.derivedEntry : this.derived.get(name);
|
|
2492
|
+
const epoch = this.board.resourceEpoch;
|
|
2493
|
+
if (cached === void 0 || cached.epoch !== epoch && !this.dependenciesCurrent(cached.dependencies)) {
|
|
2207
2494
|
const declaration = this.declarations.get(name);
|
|
2208
2495
|
if (declaration?.kind !== "derived") throw new TypeError(`Unknown derived ${name}`);
|
|
2209
2496
|
let buffer = this.derivedBuffers.get(name);
|
|
@@ -2211,10 +2498,15 @@ var Instance = class {
|
|
|
2211
2498
|
buffer = /* @__PURE__ */ new Map();
|
|
2212
2499
|
this.derivedBuffers.set(name, buffer);
|
|
2213
2500
|
} else buffer.clear();
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2501
|
+
const evaluated = this.evaluate(() => computation(declaration.value, this.host), buffer);
|
|
2502
|
+
cached = {
|
|
2503
|
+
value: evaluated.value,
|
|
2504
|
+
dependencies: evaluated.dependencies,
|
|
2505
|
+
epoch: this.board.resourceEpoch
|
|
2506
|
+
};
|
|
2507
|
+
this.setDerived(name, cached);
|
|
2508
|
+
} else if (cached.epoch !== epoch) cached.epoch = epoch;
|
|
2509
|
+
const parent = this.dependencies();
|
|
2218
2510
|
if (parent) for (const [key, version] of cached.dependencies) parent.set(key, version);
|
|
2219
2511
|
return cached.value;
|
|
2220
2512
|
}
|
|
@@ -2252,6 +2544,24 @@ var Instance = class {
|
|
|
2252
2544
|
if (!pin) throw new TypeError(`Unknown pin ${String(ref)}`);
|
|
2253
2545
|
return pin;
|
|
2254
2546
|
}
|
|
2547
|
+
/** Shared by the generic host and direct JIT, which needs to know whether to publish. */
|
|
2548
|
+
writeIntent(ref, value) {
|
|
2549
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
2550
|
+
const board = this.board;
|
|
2551
|
+
let entry = this.intentEntries.get(name);
|
|
2552
|
+
if (entry === void 0) {
|
|
2553
|
+
const pin = this.pin(name);
|
|
2554
|
+
entry = {
|
|
2555
|
+
pin,
|
|
2556
|
+
slot: board.electricalSlot(pin)
|
|
2557
|
+
};
|
|
2558
|
+
this.intentEntries.set(name, entry);
|
|
2559
|
+
}
|
|
2560
|
+
if (entry.pin.intent === value) return false;
|
|
2561
|
+
entry.pin.intent = value;
|
|
2562
|
+
board.markElectricalDirty(entry.slot);
|
|
2563
|
+
return true;
|
|
2564
|
+
}
|
|
2255
2565
|
/**
|
|
2256
2566
|
* One frozen, reusable intent value per (pin, resistance); intents are immutable. The
|
|
2257
2567
|
* outer map is keyed by pin name and the inner one by resistance, so the hot path builds
|
|
@@ -2290,7 +2600,7 @@ var Instance = class {
|
|
|
2290
2600
|
if (this.sampleEntries.size >= 512) this.sampleEntries.clear();
|
|
2291
2601
|
this.sampleEntries.set(name, entry);
|
|
2292
2602
|
}
|
|
2293
|
-
const dependencies = this.
|
|
2603
|
+
const dependencies = this.dependencies();
|
|
2294
2604
|
if (dependencies) dependencies.set(entry.slot, entry.slot.version);
|
|
2295
2605
|
return entry.pin.sample;
|
|
2296
2606
|
}
|
|
@@ -2306,14 +2616,42 @@ var Instance = class {
|
|
|
2306
2616
|
const declaration = this.declarations.get(name);
|
|
2307
2617
|
return declaration.type.kind === "number" || declaration.type.kind === "bool";
|
|
2308
2618
|
});
|
|
2619
|
+
const frozenNames = Object.freeze(stateNames);
|
|
2620
|
+
const stateValues = stateNames.map((name) => this.states.get(name));
|
|
2621
|
+
this.states.bindDirectBank(frozenNames, stateValues);
|
|
2309
2622
|
this.directBank = {
|
|
2310
|
-
stateNames:
|
|
2623
|
+
stateNames: frozenNames,
|
|
2624
|
+
stateSlots: Object.freeze(stateNames.map((name) => this.stateResources.get(name))),
|
|
2311
2625
|
states: new Map(stateNames.map((name, index) => [name, index])),
|
|
2312
|
-
stateValues
|
|
2626
|
+
stateValues
|
|
2313
2627
|
};
|
|
2314
2628
|
}
|
|
2315
2629
|
return this.directBank;
|
|
2316
2630
|
}
|
|
2631
|
+
/** Direct scalar writes are already in the numeric bank. Commit the mirror and version. */
|
|
2632
|
+
commitDirectStates(dirty) {
|
|
2633
|
+
const { stateSlots } = this.directBank;
|
|
2634
|
+
const states = this.states;
|
|
2635
|
+
const board = this.board;
|
|
2636
|
+
let wrote = false;
|
|
2637
|
+
for (let word = 0; word < dirty.length; word++) {
|
|
2638
|
+
let bits = dirty[word];
|
|
2639
|
+
if (bits === 0) continue;
|
|
2640
|
+
dirty[word] = 0;
|
|
2641
|
+
wrote = true;
|
|
2642
|
+
states.markDirectDirty(word, bits);
|
|
2643
|
+
const base = word << 5;
|
|
2644
|
+
while (bits !== 0) {
|
|
2645
|
+
const slot = stateSlots[base + 31 - Math.clz32(bits & -bits)];
|
|
2646
|
+
if (slot.subscribers.size === 0) {
|
|
2647
|
+
slot.version++;
|
|
2648
|
+
board.noteResourceVersion(slot);
|
|
2649
|
+
} else board.invalidate(slot);
|
|
2650
|
+
bits &= bits - 1;
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
return wrote;
|
|
2654
|
+
}
|
|
2317
2655
|
writeState(name, value, slot) {
|
|
2318
2656
|
const captured = snapshot(value);
|
|
2319
2657
|
this.states.set(name, captured);
|
|
@@ -2346,23 +2684,89 @@ var Instance = class {
|
|
|
2346
2684
|
}
|
|
2347
2685
|
return plan;
|
|
2348
2686
|
}
|
|
2349
|
-
|
|
2350
|
-
|
|
2687
|
+
codeVersion(name) {
|
|
2688
|
+
return this.codeVersions.get(name) ?? 0;
|
|
2689
|
+
}
|
|
2690
|
+
intersectsCodeRegion(region, offset, size) {
|
|
2691
|
+
if (offset === void 0 || size === void 0) return true;
|
|
2692
|
+
return offset < region.size && offset + size > 0;
|
|
2693
|
+
}
|
|
2694
|
+
hasCodeRegion(name, offset, size) {
|
|
2695
|
+
for (const jit of this.jits) {
|
|
2696
|
+
if (!jit.active) continue;
|
|
2697
|
+
for (const region of jit.codeRegions) if (region.memory === name && this.intersectsCodeRegion(region, offset, size)) return true;
|
|
2698
|
+
}
|
|
2699
|
+
return false;
|
|
2700
|
+
}
|
|
2701
|
+
addMetadataChange(state, change) {
|
|
2702
|
+
const pending = state.pending;
|
|
2703
|
+
let end = change.offset + change.length;
|
|
2704
|
+
let start = change.offset;
|
|
2705
|
+
for (let index = 0; index < pending.length;) {
|
|
2706
|
+
const current = pending[index];
|
|
2707
|
+
const currentEnd = current.offset + current.length;
|
|
2708
|
+
if (change.offset <= currentEnd && current.offset <= end) {
|
|
2709
|
+
start = Math.min(start, current.offset);
|
|
2710
|
+
end = Math.max(end, currentEnd);
|
|
2711
|
+
pending.splice(index, 1);
|
|
2712
|
+
} else index++;
|
|
2713
|
+
}
|
|
2714
|
+
pending.push({
|
|
2715
|
+
memory: change.memory,
|
|
2716
|
+
offset: start,
|
|
2717
|
+
length: end - start
|
|
2718
|
+
});
|
|
2719
|
+
}
|
|
2720
|
+
markMetadataChanged(jit, name, offset, size) {
|
|
2721
|
+
if (!jit.metadata) return;
|
|
2722
|
+
for (const state of jit.metadata.regions.values()) {
|
|
2723
|
+
const region = state.region;
|
|
2724
|
+
if (region.memory !== name) continue;
|
|
2725
|
+
if (state.appliedVersion === -1) continue;
|
|
2726
|
+
if (name === jit.options.code) state.dirty = true;
|
|
2727
|
+
if (!this.intersectsCodeRegion(region, offset, size)) continue;
|
|
2728
|
+
state.dirty = true;
|
|
2729
|
+
const start = offset === void 0 ? 0 : Math.max(0, offset);
|
|
2730
|
+
const end = offset === void 0 ? region.size : Math.min(region.size, offset + (size ?? 0));
|
|
2731
|
+
if (end > start) this.addMetadataChange(state, {
|
|
2732
|
+
memory: name,
|
|
2733
|
+
offset: start,
|
|
2734
|
+
length: end - start
|
|
2735
|
+
});
|
|
2736
|
+
}
|
|
2737
|
+
}
|
|
2738
|
+
memoryChanged(name, offset, size) {
|
|
2739
|
+
this.memoryChangedSlot(name, this.memoryPlan(name).slot, offset, size);
|
|
2351
2740
|
}
|
|
2352
|
-
memoryChangedPlan(plan) {
|
|
2353
|
-
this.memoryChangedSlot(plan.name, plan.slot);
|
|
2741
|
+
memoryChangedPlan(plan, offset, size) {
|
|
2742
|
+
this.memoryChangedSlot(plan.name, plan.slot, offset, size);
|
|
2354
2743
|
}
|
|
2355
|
-
memoryChangedSlot(name, slot) {
|
|
2744
|
+
memoryChangedSlot(name, slot, offset, size) {
|
|
2356
2745
|
this.board.invalidate(slot);
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
jit.
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2746
|
+
if (!this.codeMemories.has(name)) return;
|
|
2747
|
+
if (this.hasCodeRegion(name, offset, size)) this.codeVersions.set(name, this.codeVersion(name) + 1);
|
|
2748
|
+
for (const jit of this.jits) {
|
|
2749
|
+
let affected = false;
|
|
2750
|
+
for (const region of jit.codeRegions) if (region.memory === name && this.intersectsCodeRegion(region, offset, size)) {
|
|
2751
|
+
affected = true;
|
|
2752
|
+
break;
|
|
2753
|
+
}
|
|
2754
|
+
if (!affected) continue;
|
|
2755
|
+
this.markMetadataChanged(jit, name, offset, size);
|
|
2756
|
+
if (jit.options.code === name) {
|
|
2757
|
+
jit.cache.clear();
|
|
2758
|
+
jit.cacheMemories.clear();
|
|
2759
|
+
jit.revision++;
|
|
2760
|
+
jit.directPrograms.clear();
|
|
2761
|
+
jit.directProgramRevision = -1;
|
|
2762
|
+
jit.directSkip = void 0;
|
|
2763
|
+
jit.directMisses.clear();
|
|
2764
|
+
if (jit.direct?.frame.resume) jit.direct.frame.stopAfterInstruction = true;
|
|
2765
|
+
else jit.direct = void 0;
|
|
2766
|
+
} else if (jit.cacheMemories.has(name)) {
|
|
2767
|
+
for (const [pc, instruction] of jit.cache) if (instruction.codeRegion?.memory === name) jit.cache.delete(pc);
|
|
2768
|
+
jit.cacheMemories.delete(name);
|
|
2769
|
+
}
|
|
2366
2770
|
}
|
|
2367
2771
|
}
|
|
2368
2772
|
/**
|
|
@@ -2385,16 +2789,19 @@ var Instance = class {
|
|
|
2385
2789
|
return handles;
|
|
2386
2790
|
}
|
|
2387
2791
|
/**
|
|
2388
|
-
* Each method repeats the generic host operation exactly:
|
|
2389
|
-
* an invalid name still fails at the same operation, dependency slots are still
|
|
2792
|
+
* Each method repeats the generic host operation exactly: dependency slots are still
|
|
2390
2793
|
* recorded, state writes still snapshot, and memory stores still run the code-memory
|
|
2391
|
-
* invalidation that clears JIT caches.
|
|
2794
|
+
* invalidation that clears JIT caches. Every resource a declaration already provides is
|
|
2795
|
+
* resolved here, at bind time; a name that does not exist yet keeps resolving lazily so
|
|
2796
|
+
* it still fails at its first real use, not while binding.
|
|
2392
2797
|
*/
|
|
2393
2798
|
resourceHandle(kind, name) {
|
|
2394
2799
|
if (kind === "pin") {
|
|
2395
|
-
let pin;
|
|
2396
|
-
let slot = -1;
|
|
2397
|
-
let sample;
|
|
2800
|
+
let pin = this.pins.get(name);
|
|
2801
|
+
let slot = pin === void 0 ? -1 : this.board.electricalSlot(pin);
|
|
2802
|
+
let sample = pin === void 0 ? void 0 : this.sampleResources.get(name);
|
|
2803
|
+
let lastResistance;
|
|
2804
|
+
let lastIntent;
|
|
2398
2805
|
const resolve = () => {
|
|
2399
2806
|
if (pin === void 0) {
|
|
2400
2807
|
pin = this.pin(name);
|
|
@@ -2406,7 +2813,7 @@ var Instance = class {
|
|
|
2406
2813
|
return Object.freeze({
|
|
2407
2814
|
sample: () => {
|
|
2408
2815
|
const current = resolve();
|
|
2409
|
-
const dependencies = this.
|
|
2816
|
+
const dependencies = this.dependencies();
|
|
2410
2817
|
if (dependencies) dependencies.set(sample, sample.version);
|
|
2411
2818
|
return current.sample;
|
|
2412
2819
|
},
|
|
@@ -2414,26 +2821,31 @@ var Instance = class {
|
|
|
2414
2821
|
const current = resolve();
|
|
2415
2822
|
if (current.intent === value) return;
|
|
2416
2823
|
current.intent = value;
|
|
2417
|
-
this.board.
|
|
2418
|
-
this.board.electricalDirty = true;
|
|
2824
|
+
this.board.markElectricalDirty(slot);
|
|
2419
2825
|
},
|
|
2420
2826
|
resistor: (resistance) => {
|
|
2827
|
+
if (lastIntent !== void 0 && resistance === lastResistance) return lastIntent;
|
|
2421
2828
|
const r = electricalNumber(resistance);
|
|
2422
2829
|
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
2423
|
-
|
|
2830
|
+
const intent = this.resistorIntent(name, r);
|
|
2831
|
+
lastResistance = resistance;
|
|
2832
|
+
lastIntent = intent;
|
|
2833
|
+
return intent;
|
|
2424
2834
|
}
|
|
2425
2835
|
});
|
|
2426
2836
|
}
|
|
2427
2837
|
if (kind === "state") {
|
|
2428
|
-
let slot;
|
|
2838
|
+
let slot = this.states.has(name) ? this.stateResources.get(name) : void 0;
|
|
2839
|
+
let banked = slot === void 0 ? -1 : this.directBank?.states.get(name) ?? -2;
|
|
2429
2840
|
const resolve = () => slot ??= this.stateSlot(name);
|
|
2430
2841
|
const states = this.states;
|
|
2431
2842
|
return Object.freeze({
|
|
2432
2843
|
read: () => {
|
|
2433
2844
|
const current = resolve();
|
|
2434
|
-
const dependencies = this.
|
|
2845
|
+
const dependencies = this.dependencies();
|
|
2435
2846
|
if (dependencies) dependencies.set(current, current.version);
|
|
2436
|
-
|
|
2847
|
+
if (banked === -2) banked = this.directBank?.states.get(name) ?? -1;
|
|
2848
|
+
return banked < 0 ? states.get(name) : this.directBank.stateValues[banked];
|
|
2437
2849
|
},
|
|
2438
2850
|
write: (value) => {
|
|
2439
2851
|
const current = resolve();
|
|
@@ -2441,12 +2853,12 @@ var Instance = class {
|
|
|
2441
2853
|
}
|
|
2442
2854
|
});
|
|
2443
2855
|
}
|
|
2444
|
-
let plan;
|
|
2856
|
+
let plan = this.memories.has(name) ? this.memoryPlan(name) : void 0;
|
|
2445
2857
|
const resolve = () => plan ??= this.memoryPlan(name);
|
|
2446
2858
|
const read = (size, little, offset) => {
|
|
2447
2859
|
const current = resolve();
|
|
2448
2860
|
const at = range(current.bytes, offset, size);
|
|
2449
|
-
const dependencies = this.
|
|
2861
|
+
const dependencies = this.dependencies();
|
|
2450
2862
|
if (dependencies) dependencies.set(current.slot, current.slot.version);
|
|
2451
2863
|
return size === 1 ? current.bytes[at] : size === 2 ? current.view.getUint16(at, little) : current.view.getUint32(at, little);
|
|
2452
2864
|
};
|
|
@@ -2457,7 +2869,7 @@ var Instance = class {
|
|
|
2457
2869
|
if (size === 1) current.bytes[at] = n;
|
|
2458
2870
|
else if (size === 2) current.view.setUint16(at, n, little);
|
|
2459
2871
|
else current.view.setUint32(at, n, little);
|
|
2460
|
-
this.memoryChangedSlot(name, current.slot);
|
|
2872
|
+
this.memoryChangedSlot(name, current.slot, at, size);
|
|
2461
2873
|
};
|
|
2462
2874
|
return Object.freeze({
|
|
2463
2875
|
load8: (offset) => read(1, false, offset),
|
|
@@ -2656,13 +3068,140 @@ var Instance = class {
|
|
|
2656
3068
|
value: captured
|
|
2657
3069
|
});
|
|
2658
3070
|
}
|
|
3071
|
+
captureCodeRegions(options) {
|
|
3072
|
+
const primaryMemory = this.memories.get(options.code);
|
|
3073
|
+
const primary = Object.freeze({
|
|
3074
|
+
memory: options.code,
|
|
3075
|
+
base: 0,
|
|
3076
|
+
size: primaryMemory.length
|
|
3077
|
+
});
|
|
3078
|
+
const additional = [];
|
|
3079
|
+
if (Object.hasOwn(options, "codeRegions")) {
|
|
3080
|
+
if (!Array.isArray(options.codeRegions)) throw new TypeError("codeRegions must be an array");
|
|
3081
|
+
for (const item of options.codeRegions) {
|
|
3082
|
+
dataObject(item, [
|
|
3083
|
+
"memory",
|
|
3084
|
+
"base",
|
|
3085
|
+
"size"
|
|
3086
|
+
]);
|
|
3087
|
+
const memory = identity(item.memory);
|
|
3088
|
+
const base = nonnegative(item.base, "code region base");
|
|
3089
|
+
const size = nonnegative(item.size, "code region size");
|
|
3090
|
+
if (size === 0) throw new RangeError("Code region size must be positive");
|
|
3091
|
+
const length = this.memories.get(memory)?.length;
|
|
3092
|
+
if (length === void 0) throw this.board.error("INVALID_BINDING", "Unknown code region memory", this);
|
|
3093
|
+
if (size > length) throw this.board.error("INVALID_BINDING", "Code region exceeds memory", this);
|
|
3094
|
+
if (base > Number.MAX_SAFE_INTEGER - size) throw this.board.error("INVALID_BINDING", "Code region range is not a safe integer", this);
|
|
3095
|
+
const region = Object.freeze({
|
|
3096
|
+
memory,
|
|
3097
|
+
base,
|
|
3098
|
+
size
|
|
3099
|
+
});
|
|
3100
|
+
for (const existing of [primary, ...additional]) if (region.base < existing.base + existing.size && existing.base < region.base + region.size) throw this.board.error("INVALID_BINDING", "Code regions overlap", this);
|
|
3101
|
+
additional.push(region);
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
return {
|
|
3105
|
+
all: Object.freeze([primary, ...additional]),
|
|
3106
|
+
additional: Object.freeze(additional)
|
|
3107
|
+
};
|
|
3108
|
+
}
|
|
3109
|
+
capturePolling(items, region) {
|
|
3110
|
+
const polling = /* @__PURE__ */ new Map();
|
|
3111
|
+
if (!Array.isArray(items)) throw new TypeError("polling must be an array");
|
|
3112
|
+
for (const item of items) {
|
|
3113
|
+
dataObject(item, [
|
|
3114
|
+
"pc",
|
|
3115
|
+
"predicate",
|
|
3116
|
+
"consume",
|
|
3117
|
+
"wait"
|
|
3118
|
+
]);
|
|
3119
|
+
const pc = finite(item.pc, "polling pc");
|
|
3120
|
+
if (region !== void 0 && (pc < region.base || pc >= region.base + region.size)) throw this.board.error("INVALID_BINDING", "Polling pc is outside its code region", this);
|
|
3121
|
+
const predicate = identity(item.predicate);
|
|
3122
|
+
const declaration = this.declarations.get(predicate);
|
|
3123
|
+
if (polling.has(pc) || declaration?.kind !== "function" || !declaration.pure || declaration.params.length || declaration.returns?.kind !== "bool") throw this.board.error("INVALID_BINDING", "Invalid polling declaration", this);
|
|
3124
|
+
let consume;
|
|
3125
|
+
if (Object.hasOwn(item, "consume")) {
|
|
3126
|
+
consume = identity(item.consume);
|
|
3127
|
+
const fn = this.declarations.get(consume);
|
|
3128
|
+
if (fn?.kind !== "function" || fn.params.length !== 0 || fn.returns !== void 0) throw this.board.error("INVALID_BINDING", "Invalid polling consume function", this);
|
|
3129
|
+
}
|
|
3130
|
+
let wait;
|
|
3131
|
+
if (Object.hasOwn(item, "wait")) {
|
|
3132
|
+
dataObject(item.wait, ["delay", "advance"]);
|
|
3133
|
+
const delay = identity(item.wait.delay);
|
|
3134
|
+
const advance = identity(item.wait.advance);
|
|
3135
|
+
const delayFn = this.declarations.get(delay);
|
|
3136
|
+
const advanceFn = this.declarations.get(advance);
|
|
3137
|
+
if (delayFn?.kind !== "function" || !delayFn.pure || delayFn.params.length !== 0 || delayFn.returns?.kind !== "number" || advanceFn?.kind !== "function" || advanceFn.params.length !== 1 || advanceFn.params[0].type.kind !== "number" || advanceFn.returns !== void 0) throw this.board.error("INVALID_BINDING", "Invalid polling wait functions", this);
|
|
3138
|
+
wait = Object.freeze({
|
|
3139
|
+
delay,
|
|
3140
|
+
advance
|
|
3141
|
+
});
|
|
3142
|
+
}
|
|
3143
|
+
polling.set(pc, Object.freeze({
|
|
3144
|
+
predicate,
|
|
3145
|
+
...consume === void 0 ? {} : { consume },
|
|
3146
|
+
...wait === void 0 ? {} : { wait }
|
|
3147
|
+
}));
|
|
3148
|
+
}
|
|
3149
|
+
return polling;
|
|
3150
|
+
}
|
|
3151
|
+
captureMetadataDelays(value, region) {
|
|
3152
|
+
if (!(value instanceof Map)) throw new TypeError("codeMetadata delays must be a Map");
|
|
3153
|
+
const delays = /* @__PURE__ */ new Map();
|
|
3154
|
+
for (const [key, item] of value) {
|
|
3155
|
+
const pc = finite(key, "delay pc");
|
|
3156
|
+
if (pc < region.base || pc >= region.base + region.size) throw this.board.error("INVALID_BINDING", "Delay pc is outside its code region", this);
|
|
3157
|
+
dataObject(item, [
|
|
3158
|
+
"counter",
|
|
3159
|
+
"cycles",
|
|
3160
|
+
"pending"
|
|
3161
|
+
]);
|
|
3162
|
+
const counter = identity(item.counter);
|
|
3163
|
+
const cycles = integer(item.cycles, "delay cycles");
|
|
3164
|
+
if (cycles <= 0) throw new RangeError("Delay cycles must be positive");
|
|
3165
|
+
let pending;
|
|
3166
|
+
if (Object.hasOwn(item, "pending")) {
|
|
3167
|
+
dataObject(item.pending, [
|
|
3168
|
+
"state",
|
|
3169
|
+
"period",
|
|
3170
|
+
"active"
|
|
3171
|
+
]);
|
|
3172
|
+
pending = Object.freeze({
|
|
3173
|
+
state: identity(item.pending.state),
|
|
3174
|
+
period: identity(item.pending.period),
|
|
3175
|
+
active: identity(item.pending.active)
|
|
3176
|
+
});
|
|
3177
|
+
}
|
|
3178
|
+
delays.set(pc, Object.freeze({
|
|
3179
|
+
counter,
|
|
3180
|
+
cycles,
|
|
3181
|
+
...pending ? { pending } : {}
|
|
3182
|
+
}));
|
|
3183
|
+
}
|
|
3184
|
+
return delays;
|
|
3185
|
+
}
|
|
3186
|
+
/** Validate and copy provider output; caller applies it atomically. */
|
|
3187
|
+
captureCodeMetadata(region, value) {
|
|
3188
|
+
dataObject(value, ["polling", "delays"]);
|
|
3189
|
+
if (!Object.hasOwn(value, "polling") || !Object.hasOwn(value, "delays")) throw new TypeError("codeMetadata must contain polling and delays");
|
|
3190
|
+
return {
|
|
3191
|
+
polling: this.capturePolling(value.polling, region),
|
|
3192
|
+
delays: this.captureMetadataDelays(value.delays, region)
|
|
3193
|
+
};
|
|
3194
|
+
}
|
|
2659
3195
|
attach(options) {
|
|
2660
3196
|
this.assertCallAllowed();
|
|
2661
3197
|
if (this.board.phase !== "initialize" || this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "attachJit is initialization-only", this);
|
|
2662
3198
|
dataObject(options, [
|
|
2663
3199
|
"entry",
|
|
2664
3200
|
"code",
|
|
3201
|
+
"codeRegions",
|
|
3202
|
+
"codeMetadata",
|
|
2665
3203
|
"pc",
|
|
3204
|
+
"functions",
|
|
2666
3205
|
"beforeInstruction",
|
|
2667
3206
|
"polling",
|
|
2668
3207
|
"delays",
|
|
@@ -2673,59 +3212,66 @@ var Instance = class {
|
|
|
2673
3212
|
identity(options.pc);
|
|
2674
3213
|
if (this.jits.some((jit) => jit.active && jit.options.entry === options.entry)) throw this.board.error("INVALID_BINDING", "Duplicate JIT entry", this);
|
|
2675
3214
|
if (!this.memories.has(options.code) || this.declarations.get(options.pc)?.kind !== "state" || this.declarations.get(options.pc).type.kind !== "number" || typeof options.translate !== "function") throw this.board.error("INVALID_BINDING", "Invalid JIT binding", this);
|
|
3215
|
+
const codeRegions = this.captureCodeRegions(options);
|
|
3216
|
+
if (Object.hasOwn(options, "codeMetadata")) {
|
|
3217
|
+
if (typeof options.codeMetadata !== "function") throw this.board.error("INVALID_BINDING", "Invalid codeMetadata provider", this);
|
|
3218
|
+
if (Object.hasOwn(options, "polling") || Object.hasOwn(options, "delays")) throw this.board.error("INVALID_BINDING", "codeMetadata is mutually exclusive with static polling/delays", this);
|
|
3219
|
+
}
|
|
2676
3220
|
if (options.beforeInstruction !== void 0) {
|
|
2677
3221
|
dataObject(options.beforeInstruction, ["when", "call"]);
|
|
2678
3222
|
const condition = this.declarations.get(options.beforeInstruction.when), fn = this.declarations.get(options.beforeInstruction.call);
|
|
2679
3223
|
if (!condition || !["state", "derived"].includes(condition.kind) || !("type" in condition) || condition.type.kind !== "bool" || fn?.kind !== "function" || fn.params.length || fn.returns) throw this.board.error("INVALID_BINDING", "Invalid instruction hook", this);
|
|
2680
3224
|
} else if (Object.hasOwn(options, "beforeInstruction")) throw new TypeError("Invalid beforeInstruction");
|
|
2681
|
-
const polling = /* @__PURE__ */ new Map();
|
|
2682
|
-
if (Object.hasOwn(options, "
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
|
|
2697
|
-
|
|
2698
|
-
|
|
2699
|
-
|
|
2700
|
-
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2706
|
-
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
3225
|
+
const polling = Object.hasOwn(options, "polling") ? this.capturePolling(options.polling) : /* @__PURE__ */ new Map();
|
|
3226
|
+
if (Object.hasOwn(options, "delays") && !(options.delays instanceof Map)) throw new TypeError("delays must be a Map");
|
|
3227
|
+
const metadata = Object.hasOwn(options, "codeMetadata") ? {
|
|
3228
|
+
regions: new Map(codeRegions.all.map((region) => [region, {
|
|
3229
|
+
region,
|
|
3230
|
+
appliedVersion: -1,
|
|
3231
|
+
dirty: false,
|
|
3232
|
+
pending: [],
|
|
3233
|
+
polling: /* @__PURE__ */ new Map(),
|
|
3234
|
+
delays: /* @__PURE__ */ new Map()
|
|
3235
|
+
}])),
|
|
3236
|
+
polling: /* @__PURE__ */ new Map(),
|
|
3237
|
+
delays: /* @__PURE__ */ new Map()
|
|
3238
|
+
} : void 0;
|
|
3239
|
+
const base = this.entry.model;
|
|
3240
|
+
const emit = options.functions;
|
|
3241
|
+
if (Object.hasOwn(options, "functions") && typeof emit !== "function") throw new TypeError("functions must be a JIT function builder callback");
|
|
3242
|
+
let model = base;
|
|
3243
|
+
let functions;
|
|
3244
|
+
let host = this.host;
|
|
3245
|
+
if (emit) {
|
|
3246
|
+
const declarations = emit(base);
|
|
3247
|
+
acceptSync(declarations);
|
|
3248
|
+
if (!Array.isArray(declarations)) throw new TypeError("Expected JIT function declarations");
|
|
3249
|
+
model = bindJitFunctions(base, declarations);
|
|
3250
|
+
const local = /* @__PURE__ */ new Map();
|
|
3251
|
+
for (const declaration of model.declarations.slice(base.declarations.length)) if (declaration.kind === "function") local.set(declaration.name, declaration);
|
|
3252
|
+
functions = local;
|
|
3253
|
+
host = {
|
|
3254
|
+
...this.host,
|
|
3255
|
+
call: (name, args) => {
|
|
3256
|
+
const fn = local.get(name);
|
|
3257
|
+
return fn ? body(fn.body)(host, args) : this.invoke(name, args);
|
|
2713
3258
|
}
|
|
2714
|
-
|
|
2715
|
-
predicate,
|
|
2716
|
-
...consume === void 0 ? {} : { consume },
|
|
2717
|
-
...wait === void 0 ? {} : { wait }
|
|
2718
|
-
}));
|
|
2719
|
-
}
|
|
3259
|
+
};
|
|
2720
3260
|
}
|
|
2721
|
-
if (Object.hasOwn(options, "delays") && !(options.delays instanceof Map)) throw new TypeError("delays must be a Map");
|
|
2722
3261
|
const jit = {
|
|
2723
3262
|
instance: this,
|
|
3263
|
+
model,
|
|
3264
|
+
host,
|
|
3265
|
+
...functions === void 0 ? {} : { functions },
|
|
2724
3266
|
options: Object.freeze({
|
|
2725
3267
|
...options,
|
|
3268
|
+
...options.codeRegions === void 0 ? {} : { codeRegions: codeRegions.additional },
|
|
2726
3269
|
...options.beforeInstruction ? { beforeInstruction: Object.freeze({ ...options.beforeInstruction }) } : {}
|
|
2727
3270
|
}),
|
|
3271
|
+
codeRegions: codeRegions.all,
|
|
2728
3272
|
cache: /* @__PURE__ */ new Map(),
|
|
3273
|
+
cacheMemories: /* @__PURE__ */ new Set(),
|
|
3274
|
+
metadata,
|
|
2729
3275
|
polling,
|
|
2730
3276
|
delays: options.delays ?? /* @__PURE__ */ new Map(),
|
|
2731
3277
|
revision: 0,
|
|
@@ -2738,7 +3284,7 @@ var Instance = class {
|
|
|
2738
3284
|
directProgramRevision: -1,
|
|
2739
3285
|
direct: void 0,
|
|
2740
3286
|
directSkip: void 0,
|
|
2741
|
-
directMisses:
|
|
3287
|
+
directMisses: /* @__PURE__ */ new Map(),
|
|
2742
3288
|
pollReads: void 0,
|
|
2743
3289
|
currentPc: void 0,
|
|
2744
3290
|
automaticPolling: false,
|
|
@@ -2746,6 +3292,8 @@ var Instance = class {
|
|
|
2746
3292
|
pollingPeriod: 0,
|
|
2747
3293
|
pollingOrigin: 0,
|
|
2748
3294
|
pollingToken: 0,
|
|
3295
|
+
dependencyDirty: true,
|
|
3296
|
+
waiterSlots: void 0,
|
|
2749
3297
|
active: true,
|
|
2750
3298
|
stopped: false,
|
|
2751
3299
|
queued: false,
|
|
@@ -2753,16 +3301,19 @@ var Instance = class {
|
|
|
2753
3301
|
};
|
|
2754
3302
|
this.jits.push(jit);
|
|
2755
3303
|
this.board.jits.push(jit);
|
|
3304
|
+
for (const region of jit.codeRegions) this.codeMemories.add(region.memory);
|
|
2756
3305
|
const disposable = { dispose: () => {
|
|
2757
3306
|
if (!jit.active) return;
|
|
2758
3307
|
jit.active = false;
|
|
2759
3308
|
jit.execution = void 0;
|
|
2760
3309
|
delete jit.predicate;
|
|
2761
3310
|
jit.pollingToken++;
|
|
2762
|
-
this.board.
|
|
3311
|
+
this.board.unparkWaiter(jit);
|
|
2763
3312
|
jit.cache.clear();
|
|
3313
|
+
jit.cacheMemories.clear();
|
|
2764
3314
|
jit.directPrograms.clear();
|
|
2765
3315
|
jit.direct = void 0;
|
|
3316
|
+
jit.directMisses.clear();
|
|
2766
3317
|
jit.pollReads = void 0;
|
|
2767
3318
|
} };
|
|
2768
3319
|
this.resources.push(disposable);
|
|
@@ -2771,12 +3322,12 @@ var Instance = class {
|
|
|
2771
3322
|
loadBytes(name, bytes, offset) {
|
|
2772
3323
|
this.assertCallAllowed();
|
|
2773
3324
|
if (this.board.phase !== "idle" && !(this.board.phase === "initialize" && this.board.current === this)) throw this.board.error("INVALID_CALL_STATE", "Code loading is not allowed in this phase", this);
|
|
2774
|
-
if (typeof name !== "string" || !this.jits.some((jit) => jit.active && jit.
|
|
3325
|
+
if (typeof name !== "string" || !this.jits.some((jit) => jit.active && jit.codeRegions.some((region) => region.memory === name))) throw this.board.error("INVALID_BINDING", "Memory is not bound as code", this);
|
|
2775
3326
|
if (!(bytes instanceof Uint8Array)) throw new TypeError("Code must be Uint8Array");
|
|
2776
3327
|
const memory = this.memory(name), at = range(memory, offset, bytes.length);
|
|
2777
3328
|
if (bytes.length) {
|
|
2778
3329
|
memory.set(bytes.slice(), at);
|
|
2779
|
-
this.memoryChanged(name);
|
|
3330
|
+
this.memoryChanged(name, at, bytes.length);
|
|
2780
3331
|
}
|
|
2781
3332
|
}
|
|
2782
3333
|
source(path) {
|
|
@@ -2869,7 +3420,12 @@ function isPollingInstruction(body, pc, pcName) {
|
|
|
2869
3420
|
//#endregion
|
|
2870
3421
|
//#region src/runtime/jit.ts
|
|
2871
3422
|
/** Large programs dispatch through shards; small ones stay on the faster monolithic form. */
|
|
2872
|
-
var DIRECT_SHARD_SIZE =
|
|
3423
|
+
var DIRECT_SHARD_SIZE = 32;
|
|
3424
|
+
function syncJitCall(jit, name) {
|
|
3425
|
+
const step = jit.host.call(name, []).next();
|
|
3426
|
+
if (!step.done) throw new TypeError("Unexpected suspension in synchronous function");
|
|
3427
|
+
return step.value;
|
|
3428
|
+
}
|
|
2873
3429
|
/**
|
|
2874
3430
|
* Program size above which whole-program rebuilds are amortized instead of immediate.
|
|
2875
3431
|
*
|
|
@@ -2882,15 +3438,18 @@ var DIRECT_SHARD_SIZE = 8;
|
|
|
2882
3438
|
var DIRECT_REBUILD_LINEAR_LIMIT = 16;
|
|
2883
3439
|
/** Stride floor once a program exceeds `DIRECT_REBUILD_LINEAR_LIMIT`. */
|
|
2884
3440
|
var DIRECT_REBUILD_MIN_BLOCKS = 64;
|
|
2885
|
-
/** A
|
|
2886
|
-
var DIRECT_REBUILD_PATIENCE =
|
|
3441
|
+
/** A repeatedly visited translated PC can force a rebuild before the append-only stride. */
|
|
3442
|
+
var DIRECT_REBUILD_PATIENCE = 4;
|
|
3443
|
+
function directRebuildStride(compiled) {
|
|
3444
|
+
return compiled < DIRECT_REBUILD_LINEAR_LIMIT ? 1 : Math.max(DIRECT_REBUILD_MIN_BLOCKS, compiled);
|
|
3445
|
+
}
|
|
2887
3446
|
var barriers = /* @__PURE__ */ new WeakMap();
|
|
2888
3447
|
function directBarriers(jit) {
|
|
2889
3448
|
let entry = barriers.get(jit);
|
|
2890
3449
|
if (entry === void 0) {
|
|
2891
3450
|
entry = {
|
|
2892
|
-
stateMask:
|
|
2893
|
-
memoryMask:
|
|
3451
|
+
stateMask: /* @__PURE__ */ new Int32Array(0),
|
|
3452
|
+
memoryMask: /* @__PURE__ */ new Int32Array(0)
|
|
2894
3453
|
};
|
|
2895
3454
|
barriers.set(jit, entry);
|
|
2896
3455
|
}
|
|
@@ -2907,6 +3466,129 @@ function directStats(jit) {
|
|
|
2907
3466
|
directStopped: 0
|
|
2908
3467
|
};
|
|
2909
3468
|
}
|
|
3469
|
+
/** Region that supplies bytes for a logical PC; undefined means the service-PC fallback. */
|
|
3470
|
+
function codeRegionFor(jit, pc) {
|
|
3471
|
+
for (const region of jit.codeRegions) if (pc >= region.base && pc < region.base + region.size) return region;
|
|
3472
|
+
}
|
|
3473
|
+
/** Additional regions stay on the interpreter; only the primary memory feeds direct programs. */
|
|
3474
|
+
function directCodeInstruction(jit, instruction) {
|
|
3475
|
+
const region = instruction.codeRegion;
|
|
3476
|
+
return region === void 0 || region.memory === jit.options.code;
|
|
3477
|
+
}
|
|
3478
|
+
function effectivePolling(jit) {
|
|
3479
|
+
return jit.metadata?.polling ?? jit.polling;
|
|
3480
|
+
}
|
|
3481
|
+
function effectiveDelays(jit) {
|
|
3482
|
+
return jit.metadata?.delays ?? jit.delays;
|
|
3483
|
+
}
|
|
3484
|
+
/** Aliases of the primary memory must refresh together before a direct program is built. */
|
|
3485
|
+
function primaryCodeRegions(jit) {
|
|
3486
|
+
return jit.codeRegions.filter((region) => region.memory === jit.options.code);
|
|
3487
|
+
}
|
|
3488
|
+
/**
|
|
3489
|
+
* Lazily (re)build code-derived metadata for the requested regions. Provider calls never
|
|
3490
|
+
* happen from attach/store; this runs inside the translate phase immediately before the
|
|
3491
|
+
* polling/direct/translate decision that needs it.
|
|
3492
|
+
*/
|
|
3493
|
+
function ensureMetadata(board, jit, regions) {
|
|
3494
|
+
const metadata = jit.metadata;
|
|
3495
|
+
const provider = jit.options.codeMetadata;
|
|
3496
|
+
if (metadata === void 0 || provider === void 0) return;
|
|
3497
|
+
const needed = [];
|
|
3498
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3499
|
+
for (const region of regions) {
|
|
3500
|
+
const state = metadata.regions.get(region);
|
|
3501
|
+
if (state === void 0 || seen.has(state)) continue;
|
|
3502
|
+
seen.add(state);
|
|
3503
|
+
const version = jit.instance.codeVersion(region.memory);
|
|
3504
|
+
if (state.appliedVersion === version && state.pending.length === 0 && !state.dirty) continue;
|
|
3505
|
+
const changes = state.appliedVersion === -1 ? [Object.freeze({
|
|
3506
|
+
memory: region.memory,
|
|
3507
|
+
offset: 0,
|
|
3508
|
+
length: region.size
|
|
3509
|
+
})] : state.pending.map((change) => Object.freeze({ ...change }));
|
|
3510
|
+
needed.push({
|
|
3511
|
+
state,
|
|
3512
|
+
changes,
|
|
3513
|
+
version
|
|
3514
|
+
});
|
|
3515
|
+
}
|
|
3516
|
+
if (needed.length === 0) return;
|
|
3517
|
+
const prior = board.phase;
|
|
3518
|
+
board.phase = "translate";
|
|
3519
|
+
const prepared = [];
|
|
3520
|
+
try {
|
|
3521
|
+
for (const item of needed) {
|
|
3522
|
+
const { region } = item.state;
|
|
3523
|
+
const memory = jit.instance.memory(region.memory);
|
|
3524
|
+
const bytes = region.size >= memory.length ? memory : memory.subarray(0, region.size);
|
|
3525
|
+
let valid = true;
|
|
3526
|
+
try {
|
|
3527
|
+
const code = bytesView(bytes, () => {
|
|
3528
|
+
if (!valid) throw board.error("OBJECT_DISPOSED", "Metadata view expired", jit.instance);
|
|
3529
|
+
});
|
|
3530
|
+
const view = Object.freeze({
|
|
3531
|
+
memory: region.memory,
|
|
3532
|
+
base: region.base,
|
|
3533
|
+
size: region.size,
|
|
3534
|
+
codeVersion: item.version,
|
|
3535
|
+
code
|
|
3536
|
+
});
|
|
3537
|
+
const result = provider(Object.freeze({
|
|
3538
|
+
region: view,
|
|
3539
|
+
changes: Object.freeze(item.changes.slice())
|
|
3540
|
+
}));
|
|
3541
|
+
acceptSync(result);
|
|
3542
|
+
const captured = jit.instance.captureCodeMetadata(region, result);
|
|
3543
|
+
prepared.push({
|
|
3544
|
+
state: item.state,
|
|
3545
|
+
version: item.version,
|
|
3546
|
+
polling: captured.polling,
|
|
3547
|
+
delays: captured.delays
|
|
3548
|
+
});
|
|
3549
|
+
} finally {
|
|
3550
|
+
valid = false;
|
|
3551
|
+
}
|
|
3552
|
+
}
|
|
3553
|
+
let primary = false;
|
|
3554
|
+
for (const item of prepared) {
|
|
3555
|
+
item.state.appliedVersion = item.version;
|
|
3556
|
+
item.state.dirty = false;
|
|
3557
|
+
item.state.pending.length = 0;
|
|
3558
|
+
item.state.polling.clear();
|
|
3559
|
+
for (const [pc, binding] of item.polling) item.state.polling.set(pc, binding);
|
|
3560
|
+
item.state.delays.clear();
|
|
3561
|
+
for (const [pc, delay] of item.delays) item.state.delays.set(pc, delay);
|
|
3562
|
+
if (item.state.region.memory === jit.options.code) primary = true;
|
|
3563
|
+
}
|
|
3564
|
+
metadata.polling.clear();
|
|
3565
|
+
metadata.delays.clear();
|
|
3566
|
+
for (const state of metadata.regions.values()) {
|
|
3567
|
+
for (const [pc, binding] of state.polling) metadata.polling.set(pc, binding);
|
|
3568
|
+
for (const [pc, delay] of state.delays) metadata.delays.set(pc, delay);
|
|
3569
|
+
}
|
|
3570
|
+
if (primary) {
|
|
3571
|
+
jit.directPrograms.clear();
|
|
3572
|
+
jit.directProgramRevision = -1;
|
|
3573
|
+
jit.directSkip = void 0;
|
|
3574
|
+
if (!jit.direct?.frame.resume) {
|
|
3575
|
+
jit.direct = void 0;
|
|
3576
|
+
jit.directRevision = -1;
|
|
3577
|
+
}
|
|
3578
|
+
}
|
|
3579
|
+
} catch (cause) {
|
|
3580
|
+
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
3581
|
+
} finally {
|
|
3582
|
+
board.phase = prior;
|
|
3583
|
+
}
|
|
3584
|
+
}
|
|
3585
|
+
function ensureMetadataForPc(board, jit, pc) {
|
|
3586
|
+
if (jit.metadata === void 0) return;
|
|
3587
|
+
const region = codeRegionFor(jit, pc) ?? jit.codeRegions[0];
|
|
3588
|
+
const needed = [region];
|
|
3589
|
+
if (board.execution === "throughput" && board.jits.length === 1 && region.memory === jit.options.code) needed.push(...primaryCodeRegions(jit));
|
|
3590
|
+
ensureMetadata(board, jit, needed);
|
|
3591
|
+
}
|
|
2910
3592
|
/** Publish the current guard sets into the masks the compiled blocks test at entry. */
|
|
2911
3593
|
function syncDirectBarriers(jit) {
|
|
2912
3594
|
const entry = directBarriers(jit);
|
|
@@ -2969,9 +3651,7 @@ function directHost(board, instance, frame) {
|
|
|
2969
3651
|
},
|
|
2970
3652
|
intent: (pin, value) => {
|
|
2971
3653
|
publishTime();
|
|
2972
|
-
|
|
2973
|
-
instance.host.intent(pin, value);
|
|
2974
|
-
if (changed) frame.publishPending = true;
|
|
3654
|
+
if (instance.writeIntent(pin, value)) frame.publishPending = true;
|
|
2975
3655
|
},
|
|
2976
3656
|
resistor: (pin, resistance) => {
|
|
2977
3657
|
publishTime();
|
|
@@ -3004,51 +3684,126 @@ function directLayout(jit) {
|
|
|
3004
3684
|
const memories = new Map(memoryNames.map((name, index) => [name, index]));
|
|
3005
3685
|
const memoryBanks = memoryNames.map((name) => instance.memory(name));
|
|
3006
3686
|
const memoryViews = memoryBanks.map((memory) => new DataView(memory.buffer, memory.byteOffset, memory.byteLength));
|
|
3687
|
+
const pinNames = [...instance.pins.keys()];
|
|
3688
|
+
const pins = new Map(pinNames.map((name, index) => [name, index]));
|
|
3689
|
+
const pinObjects = pinNames.map((name) => instance.pin(name));
|
|
3690
|
+
const pinElectricalSlots = Int32Array.from(pinObjects, (pin) => instance.board.electricalSlot(pin));
|
|
3007
3691
|
jit.directLayout = {
|
|
3008
3692
|
...bank,
|
|
3009
3693
|
memoryNames: Object.freeze(memoryNames),
|
|
3010
3694
|
memories,
|
|
3011
3695
|
memoryBanks: Object.freeze(memoryBanks),
|
|
3012
|
-
memoryViews: Object.freeze(memoryViews)
|
|
3696
|
+
memoryViews: Object.freeze(memoryViews),
|
|
3697
|
+
pinNames: Object.freeze(pinNames),
|
|
3698
|
+
pins,
|
|
3699
|
+
pinObjects: Object.freeze(pinObjects),
|
|
3700
|
+
pinElectricalSlots
|
|
3013
3701
|
};
|
|
3014
3702
|
return jit.directLayout;
|
|
3015
3703
|
}
|
|
3016
|
-
function
|
|
3704
|
+
function flushDirect(board, instance, layout, frame) {
|
|
3705
|
+
board.time = frame.time;
|
|
3706
|
+
frame.publishPending = false;
|
|
3707
|
+
let wrote = instance.commitDirectStates(frame.stateDirty);
|
|
3708
|
+
for (let word = 0; word < frame.memoryDirty.length; word++) {
|
|
3709
|
+
let bits = frame.memoryDirty[word];
|
|
3710
|
+
if (bits === 0) continue;
|
|
3711
|
+
frame.memoryDirty[word] = 0;
|
|
3712
|
+
while (bits !== 0) {
|
|
3713
|
+
const slot = word * 32 + 31 - Math.clz32(bits & -bits);
|
|
3714
|
+
instance.memoryChanged(layout.memoryNames[slot]);
|
|
3715
|
+
bits &= bits - 1;
|
|
3716
|
+
wrote = true;
|
|
3717
|
+
}
|
|
3718
|
+
}
|
|
3719
|
+
return wrote;
|
|
3720
|
+
}
|
|
3721
|
+
function createDirectFrame(board, jit, layout, program, barrier) {
|
|
3722
|
+
const instance = jit.instance;
|
|
3723
|
+
const { pinObjects, pinNames, pinElectricalSlots } = layout;
|
|
3017
3724
|
const frame = {
|
|
3018
3725
|
states: layout.stateValues,
|
|
3019
3726
|
memories: [...layout.memoryBanks],
|
|
3020
3727
|
memoryViews: layout.memoryViews,
|
|
3728
|
+
pins: pinObjects,
|
|
3729
|
+
resistors: [],
|
|
3021
3730
|
locals: new Array(program.localCount),
|
|
3022
|
-
stateDirty: new
|
|
3023
|
-
memoryDirty: new
|
|
3731
|
+
stateDirty: new Int32Array(Math.ceil(layout.stateNames.length / 32)),
|
|
3732
|
+
memoryDirty: new Int32Array(Math.ceil(layout.memoryNames.length / 32)),
|
|
3024
3733
|
time: board.time,
|
|
3025
3734
|
due: board.time,
|
|
3026
|
-
resume: 0
|
|
3735
|
+
resume: 0,
|
|
3736
|
+
stopAfterInstruction: false,
|
|
3737
|
+
boundarySettled: false
|
|
3738
|
+
};
|
|
3739
|
+
frame.writePinIntent = (pinIndex, value) => {
|
|
3740
|
+
if (!Object.hasOwn(instance, "writeIntent")) {
|
|
3741
|
+
const pin = pinObjects[pinIndex];
|
|
3742
|
+
if (pin.intent === value) return;
|
|
3743
|
+
pin.intent = value;
|
|
3744
|
+
instance.board.markElectricalDirty(pinElectricalSlots[pinIndex]);
|
|
3745
|
+
frame.publishPending = true;
|
|
3746
|
+
return;
|
|
3747
|
+
}
|
|
3748
|
+
board.time = frame.time;
|
|
3749
|
+
if (instance.writeIntent(pinNames[pinIndex], value)) frame.publishPending = true;
|
|
3027
3750
|
};
|
|
3028
3751
|
frame.host = directHost(board, instance, frame);
|
|
3752
|
+
if (jit.metadata === void 0) {
|
|
3753
|
+
frame.publishCheckpoint = () => {
|
|
3754
|
+
flushDirect(board, instance, layout, frame);
|
|
3755
|
+
board.boundary();
|
|
3756
|
+
if (jit.directProgramRevision === -1 || !board.canContinueDirect(jit, frame.due)) {
|
|
3757
|
+
frame.boundarySettled = true;
|
|
3758
|
+
return false;
|
|
3759
|
+
}
|
|
3760
|
+
board.current = instance;
|
|
3761
|
+
return true;
|
|
3762
|
+
};
|
|
3763
|
+
if (jit.options.beforeInstruction === void 0) {
|
|
3764
|
+
const pcIndex = layout.states.get(jit.options.pc);
|
|
3765
|
+
frame.finishCheckpoint = (deadline, emptySuffix) => {
|
|
3766
|
+
if (emptySuffix) board.time = frame.time;
|
|
3767
|
+
else flushDirect(board, instance, layout, frame);
|
|
3768
|
+
board.boundary();
|
|
3769
|
+
const direct = jit.direct;
|
|
3770
|
+
const nextPc = frame.states[pcIndex];
|
|
3771
|
+
if (jit.directProgramRevision === -1 || direct?.frame !== frame || !direct.pcs.has(nextPc) || jit.directSkip === nextPc || jit.directRevision !== jit.revision && jit.revision - jit.directRevision >= directRebuildStride(direct.pcs.size) || frame.time >= deadline || board.events.time < deadline || !emptySuffix && !board.canContinueDirect(jit, frame.time)) {
|
|
3772
|
+
frame.boundarySettled = true;
|
|
3773
|
+
return false;
|
|
3774
|
+
}
|
|
3775
|
+
if (jit.directObserverRevision !== instance.observerRevision) refreshDirectGuards(jit);
|
|
3776
|
+
frame.stopAfterInstruction = false;
|
|
3777
|
+
board.current = instance;
|
|
3778
|
+
return true;
|
|
3779
|
+
};
|
|
3780
|
+
}
|
|
3781
|
+
}
|
|
3029
3782
|
frame.blockedStates = barrier.stateMask;
|
|
3030
3783
|
frame.blockedMemories = barrier.memoryMask;
|
|
3031
3784
|
return frame;
|
|
3032
3785
|
}
|
|
3033
3786
|
function runDirect(board, jit, hookReads) {
|
|
3034
3787
|
const instance = jit.instance;
|
|
3035
|
-
|
|
3788
|
+
let pending = Boolean(jit.direct?.frame.resume);
|
|
3036
3789
|
if (!pending) {
|
|
3037
3790
|
const pc = finite(instance.states.get(jit.options.pc), "pc");
|
|
3038
|
-
|
|
3791
|
+
const region = codeRegionFor(jit, pc);
|
|
3792
|
+
if (board.execution !== "throughput" || board.jits.length !== 1 || region !== void 0 && region.memory !== jit.options.code || effectivePolling(jit).has(pc) || jit.cache.get(pc)?.polling) return false;
|
|
3039
3793
|
refreshDirectGuards(jit, hookReads);
|
|
3040
3794
|
if (jit.directSkip === pc) return false;
|
|
3041
3795
|
if (jit.directRevision !== jit.revision) {
|
|
3042
3796
|
const compiled = jit.direct?.pcs.size ?? 0;
|
|
3043
3797
|
const stale = jit.revision - jit.directRevision;
|
|
3044
|
-
const stride =
|
|
3798
|
+
const stride = directRebuildStride(compiled);
|
|
3045
3799
|
const covered = jit.direct?.pcs.has(pc) === true;
|
|
3046
|
-
|
|
3800
|
+
const misses = covered ? 0 : jit.directMisses.get(pc) ?? 0;
|
|
3801
|
+
if (!(jit.direct === void 0 || stale >= stride || misses >= DIRECT_REBUILD_PATIENCE)) {
|
|
3047
3802
|
if (!covered) {
|
|
3048
3803
|
const stats = directStats(jit);
|
|
3049
3804
|
stats.directCalls++;
|
|
3050
3805
|
stats.directFallbacks++;
|
|
3051
|
-
jit.directMisses
|
|
3806
|
+
if (jit.cache.has(pc)) jit.directMisses.set(pc, misses + 1);
|
|
3052
3807
|
return false;
|
|
3053
3808
|
}
|
|
3054
3809
|
} else {
|
|
@@ -3058,10 +3813,10 @@ function runDirect(board, jit, hookReads) {
|
|
|
3058
3813
|
jit.directProgramRevision = jit.revision;
|
|
3059
3814
|
}
|
|
3060
3815
|
const layout = directLayout(jit);
|
|
3061
|
-
const instructions = [...jit.cache.values()].filter((instruction) => !instruction.polling && !jit.
|
|
3816
|
+
const instructions = [...jit.cache.values()].filter((instruction) => !instruction.polling && !effectivePolling(jit).has(instruction.pc) && directCodeInstruction(jit, instruction));
|
|
3062
3817
|
/** Declared busy-wait heads fold their remaining rounds inside the emitted body. */
|
|
3063
3818
|
const delayLoops = /* @__PURE__ */ new Map();
|
|
3064
|
-
for (const [headPc, delay] of jit
|
|
3819
|
+
for (const [headPc, delay] of effectiveDelays(jit)) {
|
|
3065
3820
|
const counter = layout.states.get(delay.counter);
|
|
3066
3821
|
if (counter === void 0) continue;
|
|
3067
3822
|
const pending = delay.pending === void 0 ? void 0 : {
|
|
@@ -3084,36 +3839,39 @@ function runDirect(board, jit, hookReads) {
|
|
|
3084
3839
|
jit.direct = cached;
|
|
3085
3840
|
jit.directRevision = jit.revision;
|
|
3086
3841
|
} else {
|
|
3087
|
-
const { states, memories } = layout;
|
|
3842
|
+
const { states, memories, pins } = layout;
|
|
3088
3843
|
const cache = jit.directCache ??= compileDirectJit.createCache();
|
|
3089
3844
|
const program = compileDirectJit(instructions, {
|
|
3090
3845
|
states,
|
|
3091
3846
|
memories,
|
|
3847
|
+
pins,
|
|
3092
3848
|
pc: {
|
|
3093
3849
|
name: jit.options.pc,
|
|
3094
3850
|
index: states.get(jit.options.pc)
|
|
3095
3851
|
},
|
|
3096
3852
|
host: true,
|
|
3853
|
+
...jit.functions === void 0 ? {} : { functions: jit.functions },
|
|
3097
3854
|
split: true,
|
|
3098
3855
|
shardSize: DIRECT_SHARD_SIZE,
|
|
3099
3856
|
delayLoops
|
|
3100
3857
|
}, cache);
|
|
3101
3858
|
jit.directRevision = jit.revision;
|
|
3102
|
-
jit.directMisses
|
|
3859
|
+
jit.directMisses.clear();
|
|
3103
3860
|
const stats = directStats(jit);
|
|
3104
3861
|
stats.directShards = program?.shards ?? 0;
|
|
3105
3862
|
if (program) stats.directCompiles++;
|
|
3106
3863
|
const barrier = directBarriers(jit);
|
|
3107
3864
|
barrier.stateIndex = states;
|
|
3108
3865
|
barrier.memoryIndex = memories;
|
|
3109
|
-
|
|
3110
|
-
|
|
3866
|
+
const stateMaskWords = Math.ceil(layout.stateNames.length / 32);
|
|
3867
|
+
const memoryMaskWords = Math.ceil(layout.memoryNames.length / 32);
|
|
3868
|
+
if (barrier.stateMask.length !== stateMaskWords) barrier.stateMask = new Int32Array(stateMaskWords);
|
|
3869
|
+
if (barrier.memoryMask.length !== memoryMaskWords) barrier.memoryMask = new Int32Array(memoryMaskWords);
|
|
3111
3870
|
syncDirectBarriers(jit);
|
|
3112
3871
|
jit.direct = program ? {
|
|
3113
3872
|
program,
|
|
3114
3873
|
pcs: new Set(program.pcs),
|
|
3115
|
-
|
|
3116
|
-
frame: createDirectFrame(board, instance, layout, program, barrier)
|
|
3874
|
+
frame: createDirectFrame(board, jit, layout, program, barrier)
|
|
3117
3875
|
} : void 0;
|
|
3118
3876
|
if (!jit.direct) jit.directPrograms.delete(key);
|
|
3119
3877
|
else jit.directPrograms.set(key, jit.direct);
|
|
@@ -3124,67 +3882,65 @@ function runDirect(board, jit, hookReads) {
|
|
|
3124
3882
|
}
|
|
3125
3883
|
const direct = jit.direct;
|
|
3126
3884
|
const frame = direct.frame;
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
|
|
3148
|
-
wrote = true;
|
|
3885
|
+
const layout = jit.directLayout;
|
|
3886
|
+
for (;;) {
|
|
3887
|
+
if (pending) frame.stopAfterInstruction = true;
|
|
3888
|
+
frame.time = board.time;
|
|
3889
|
+
const start = board.time;
|
|
3890
|
+
let reason;
|
|
3891
|
+
let wrote;
|
|
3892
|
+
const stats = directStats(jit);
|
|
3893
|
+
stats.directCalls++;
|
|
3894
|
+
try {
|
|
3895
|
+
reason = direct.program.run(frame, Math.min(board.executionEnd, board.events.time));
|
|
3896
|
+
} finally {
|
|
3897
|
+
wrote = flushDirect(board, instance, layout, frame);
|
|
3898
|
+
}
|
|
3899
|
+
if (frame.boundarySettled) frame.boundarySettled = false;
|
|
3900
|
+
else if (reason !== "fallback" || wrote || board.time !== start) board.boundary();
|
|
3901
|
+
if (reason === "fallback") {
|
|
3902
|
+
const nextPc = finite(instance.states.get(jit.options.pc), "pc");
|
|
3903
|
+
if (!(effectivePolling(jit).has(nextPc) || jit.cache.get(nextPc)?.polling)) {
|
|
3904
|
+
stats.directFallbacks++;
|
|
3905
|
+
jit.directSkip = nextPc;
|
|
3149
3906
|
}
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3907
|
+
if (!wrote && board.time === start && board.time < board.executionEnd) return false;
|
|
3908
|
+
} else if (reason === "stopped") {
|
|
3909
|
+
stats.directStopped++;
|
|
3910
|
+
jit.stopped = true;
|
|
3911
|
+
return true;
|
|
3912
|
+
} else if (reason === "invalidated") {
|
|
3913
|
+
frame.stopAfterInstruction = false;
|
|
3914
|
+
if (jit.directProgramRevision === -1 && frame.resume === 0) {
|
|
3915
|
+
jit.direct = void 0;
|
|
3916
|
+
jit.directRevision = -1;
|
|
3917
|
+
}
|
|
3918
|
+
} else stats.directDeadlines++;
|
|
3919
|
+
if (frame.resume) {
|
|
3920
|
+
const wake = Math.max(frame.due, board.time);
|
|
3921
|
+
if (jit.metadata === void 0 && board.canContinueDirect(jit, wake)) {
|
|
3922
|
+
board.time = wake;
|
|
3923
|
+
board.current = instance;
|
|
3924
|
+
pending = true;
|
|
3925
|
+
continue;
|
|
3926
|
+
}
|
|
3927
|
+
if (wake > board.time) {
|
|
3928
|
+
board.events.push(wake, () => board.queueJit(jit));
|
|
3929
|
+
return true;
|
|
3158
3930
|
}
|
|
3159
3931
|
}
|
|
3160
|
-
|
|
3161
|
-
if (reason !== "fallback" || wrote || board.time !== start) board.boundary();
|
|
3162
|
-
if (reason === "fallback") {
|
|
3163
|
-
const nextPc = finite(instance.states.get(jit.options.pc), "pc");
|
|
3164
|
-
if (!(jit.polling.has(nextPc) || jit.cache.get(nextPc)?.polling)) {
|
|
3165
|
-
stats.directFallbacks++;
|
|
3166
|
-
jit.directSkip = nextPc;
|
|
3167
|
-
}
|
|
3168
|
-
if (board.time === start && board.time < board.executionEnd) return false;
|
|
3169
|
-
} else if (reason === "stopped") {
|
|
3170
|
-
stats.directStopped++;
|
|
3171
|
-
jit.stopped = true;
|
|
3932
|
+
board.queueJit(jit);
|
|
3172
3933
|
return true;
|
|
3173
|
-
}
|
|
3174
|
-
frame.stopAfterInstruction = false;
|
|
3175
|
-
if (direct.revision !== jit.revision) {
|
|
3176
|
-
jit.direct = void 0;
|
|
3177
|
-
jit.directRevision = -1;
|
|
3178
|
-
}
|
|
3179
|
-
} else stats.directDeadlines++;
|
|
3180
|
-
if (frame.resume && frame.due > board.time) board.events.push(frame.due, () => board.queueJit(jit));
|
|
3181
|
-
else board.queueJit(jit);
|
|
3182
|
-
return true;
|
|
3934
|
+
}
|
|
3183
3935
|
}
|
|
3184
|
-
function pollingDependencies(jit, dependencies) {
|
|
3936
|
+
function pollingDependencies(jit, dependencies, pc) {
|
|
3185
3937
|
const result = new Map(dependencies);
|
|
3186
3938
|
const instance = jit.instance;
|
|
3187
|
-
|
|
3939
|
+
const pcSlot = instance.stateResources.get(jit.options.pc);
|
|
3940
|
+
result.set(pcSlot, pcSlot.version);
|
|
3941
|
+
const memory = (pc === void 0 ? void 0 : codeRegionFor(jit, pc))?.memory ?? jit.options.code;
|
|
3942
|
+
const slot = instance.memoryResources.get(memory);
|
|
3943
|
+
result.set(slot, slot.version);
|
|
3188
3944
|
return result;
|
|
3189
3945
|
}
|
|
3190
3946
|
/** Park a polling JIT and, when requested, schedule the next device time event. */
|
|
@@ -3192,7 +3948,7 @@ function waitForPolling(board, jit, dependencies, duration, advance) {
|
|
|
3192
3948
|
jit.dependencies = dependencies;
|
|
3193
3949
|
jit.pollingWait = true;
|
|
3194
3950
|
jit.pollingPeriod = 0;
|
|
3195
|
-
board.
|
|
3951
|
+
board.parkWaiter(jit);
|
|
3196
3952
|
const token = ++jit.pollingToken;
|
|
3197
3953
|
board.boundary();
|
|
3198
3954
|
if (duration === 0 || advance === void 0) return;
|
|
@@ -3200,9 +3956,8 @@ function waitForPolling(board, jit, dependencies, duration, advance) {
|
|
|
3200
3956
|
board.events.push(due, () => {
|
|
3201
3957
|
if (token !== jit.pollingToken || !jit.active || jit.stopped || !board.waitingJits.has(jit)) return;
|
|
3202
3958
|
const pending = jit.dependencies;
|
|
3203
|
-
jit.pollingWait = false;
|
|
3204
3959
|
delete jit.dependencies;
|
|
3205
|
-
board.
|
|
3960
|
+
board.unparkWaiter(jit);
|
|
3206
3961
|
jit.pollingToken++;
|
|
3207
3962
|
board.current = jit.instance;
|
|
3208
3963
|
if (!jit.instance.dependenciesCurrent(pending)) {
|
|
@@ -3224,12 +3979,12 @@ function jitSignal(board, jit, signal) {
|
|
|
3224
3979
|
return true;
|
|
3225
3980
|
}
|
|
3226
3981
|
if (signal.kind === "waitUntil") {
|
|
3227
|
-
const checked = jit.instance.evaluate(() => jit
|
|
3982
|
+
const checked = jit.instance.evaluate(() => syncJitCall(jit, signal.predicate));
|
|
3228
3983
|
if (checked.value === true) return false;
|
|
3229
3984
|
jit.predicate = signal.predicate;
|
|
3230
3985
|
jit.pollingWait = false;
|
|
3231
3986
|
jit.dependencies = checked.dependencies;
|
|
3232
|
-
board.
|
|
3987
|
+
board.parkWaiter(jit);
|
|
3233
3988
|
board.boundary();
|
|
3234
3989
|
return true;
|
|
3235
3990
|
}
|
|
@@ -3245,7 +4000,7 @@ function jitSignal(board, jit, signal) {
|
|
|
3245
4000
|
jit.instance.host.write(jit.options.pc, signal.pc);
|
|
3246
4001
|
jit.execution = void 0;
|
|
3247
4002
|
const polling = jit.automaticPolling && duration > 0 && signal.pc === jit.currentPc && jit.pollReads;
|
|
3248
|
-
const dependencies = polling ? pollingDependencies(jit, polling) : void 0;
|
|
4003
|
+
const dependencies = polling ? pollingDependencies(jit, polling, jit.currentPc) : void 0;
|
|
3249
4004
|
jit.automaticPolling = false;
|
|
3250
4005
|
jit.pollReads = void 0;
|
|
3251
4006
|
board.boundary();
|
|
@@ -3257,7 +4012,7 @@ function jitSignal(board, jit, signal) {
|
|
|
3257
4012
|
jit.pollingWait = true;
|
|
3258
4013
|
jit.pollingPeriod = duration;
|
|
3259
4014
|
jit.pollingOrigin = board.time;
|
|
3260
|
-
board.
|
|
4015
|
+
board.parkWaiter(jit);
|
|
3261
4016
|
} else board.queueJit(jit);
|
|
3262
4017
|
});
|
|
3263
4018
|
return true;
|
|
@@ -3268,6 +4023,7 @@ function runJit(board, jit) {
|
|
|
3268
4023
|
jit.queued = false;
|
|
3269
4024
|
if (!jit.active || jit.stopped) return;
|
|
3270
4025
|
board.current = jit.instance;
|
|
4026
|
+
if (jit.metadata !== void 0) ensureMetadataForPc(board, jit, finite(jit.instance.states.get(jit.options.pc), "pc"));
|
|
3271
4027
|
if (jit.direct?.frame.resume) {
|
|
3272
4028
|
runDirect(board, jit);
|
|
3273
4029
|
return;
|
|
@@ -3281,7 +4037,7 @@ function runJit(board, jit) {
|
|
|
3281
4037
|
if (hook) {
|
|
3282
4038
|
const kind = jit.instance.declarations.get(hook.when).kind;
|
|
3283
4039
|
const priorPc = jit.instance.states.get(jit.options.pc);
|
|
3284
|
-
const checked = board.execution === "throughput" && (board.jits.length === 1 || jit.
|
|
4040
|
+
const checked = board.execution === "throughput" && (board.jits.length === 1 || effectivePolling(jit).has(priorPc)) ? jit.instance.evaluate(() => jit.instance.read(kind, hook.when)) : void 0;
|
|
3285
4041
|
const handled = (checked ? checked.value : jit.instance.read(kind, hook.when)) === true;
|
|
3286
4042
|
if (handled) {
|
|
3287
4043
|
const execution = jit.instance.invoke(hook.call);
|
|
@@ -3297,7 +4053,7 @@ function runJit(board, jit) {
|
|
|
3297
4053
|
}
|
|
3298
4054
|
const pc = finite(jit.instance.states.get(jit.options.pc), "pc");
|
|
3299
4055
|
if (board.execution === "throughput") {
|
|
3300
|
-
const declaration = jit.
|
|
4056
|
+
const declaration = effectivePolling(jit).get(pc);
|
|
3301
4057
|
if (declaration) {
|
|
3302
4058
|
const checked = jit.instance.evaluate(() => jit.instance.syncCall(declaration.predicate));
|
|
3303
4059
|
if (checked.value !== true) {
|
|
@@ -3305,7 +4061,7 @@ function runJit(board, jit) {
|
|
|
3305
4061
|
let duration = 0;
|
|
3306
4062
|
const wait = declaration.wait;
|
|
3307
4063
|
if (wait) duration = time(jit.instance.evaluate(() => jit.instance.syncCall(wait.delay), dependencies).value);
|
|
3308
|
-
waitForPolling(board, jit, pollingDependencies(jit, dependencies), duration, wait?.advance);
|
|
4064
|
+
waitForPolling(board, jit, pollingDependencies(jit, dependencies, pc), duration, wait?.advance);
|
|
3309
4065
|
return;
|
|
3310
4066
|
}
|
|
3311
4067
|
if (declaration.consume) jit.instance.syncCall(declaration.consume);
|
|
@@ -3318,28 +4074,43 @@ function runJit(board, jit) {
|
|
|
3318
4074
|
board.phase = "translate";
|
|
3319
4075
|
let valid = true;
|
|
3320
4076
|
try {
|
|
3321
|
-
const
|
|
4077
|
+
const codeRegion = codeRegionFor(jit, pc);
|
|
4078
|
+
const codeMemory = codeRegion?.memory ?? jit.options.code;
|
|
4079
|
+
const memory = jit.instance.memory(codeMemory);
|
|
4080
|
+
const bytes = codeRegion === void 0 || codeRegion.size >= memory.length ? memory : memory.subarray(0, codeRegion.size);
|
|
4081
|
+
const code = bytesView(bytes, () => {
|
|
3322
4082
|
if (!valid) throw board.error("OBJECT_DISPOSED", "Translation view expired", jit.instance);
|
|
3323
4083
|
});
|
|
3324
4084
|
const result = jit.options.translate(Object.freeze({
|
|
3325
4085
|
pc,
|
|
3326
|
-
code
|
|
3327
|
-
|
|
4086
|
+
code,
|
|
4087
|
+
codeBase: codeRegion?.base ?? 0,
|
|
4088
|
+
codeVersion: jit.instance.codeVersion(codeMemory)
|
|
4089
|
+
}), createJitBlockBuilder({ device: jit.model }));
|
|
3328
4090
|
acceptSync(result);
|
|
3329
|
-
const block = validateBoundBlockIR(result, jit.
|
|
4091
|
+
const block = validateBoundBlockIR(result, jit.model);
|
|
3330
4092
|
if (block.instructions[0]?.pc !== pc) throw new TypeError("Translated first pc does not match");
|
|
3331
|
-
const instructions = block.instructions.map((instruction) =>
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
4093
|
+
const instructions = block.instructions.map((instruction) => {
|
|
4094
|
+
const region = codeRegionFor(jit, instruction.pc);
|
|
4095
|
+
return {
|
|
4096
|
+
pc: instruction.pc,
|
|
4097
|
+
nextPc: instruction.nextPc,
|
|
4098
|
+
body: instruction.body,
|
|
4099
|
+
polling: isPollingInstruction(instruction.body, instruction.pc, jit.options.pc),
|
|
4100
|
+
execute: body(instruction.body),
|
|
4101
|
+
codeRegion: region
|
|
4102
|
+
};
|
|
4103
|
+
});
|
|
4104
|
+
if (instructions.some((entry) => jit.direct?.pcs.has(entry.pc))) {
|
|
4105
|
+
jit.direct = void 0;
|
|
4106
|
+
jit.directPrograms.clear();
|
|
4107
|
+
}
|
|
3338
4108
|
for (let index = instructions.length - 1; index >= 0; index--) {
|
|
3339
4109
|
const entry = instructions[index];
|
|
3340
4110
|
jit.cache.set(entry.pc, entry);
|
|
4111
|
+
jit.cacheMemories.add(entry.codeRegion?.memory ?? jit.options.code);
|
|
3341
4112
|
}
|
|
3342
|
-
jit.revision++;
|
|
4113
|
+
if (instructions.some((entry) => entry.codeRegion === void 0 || entry.codeRegion.memory === jit.options.code)) jit.revision++;
|
|
3343
4114
|
jit.directSkip = void 0;
|
|
3344
4115
|
instruction = instructions[0];
|
|
3345
4116
|
} catch (cause) {
|
|
@@ -3353,7 +4124,7 @@ function runJit(board, jit) {
|
|
|
3353
4124
|
jit.automaticPolling = board.execution === "throughput" && board.jits.length === 1 && instruction.polling && !hookPending && (!hook || hookReads !== void 0);
|
|
3354
4125
|
jit.pollReads = hookReads;
|
|
3355
4126
|
jit.instance.host.write(jit.options.pc, instruction.nextPc);
|
|
3356
|
-
jit.execution = instruction.execute(jit.
|
|
4127
|
+
jit.execution = instruction.execute(jit.host);
|
|
3357
4128
|
}
|
|
3358
4129
|
for (;;) {
|
|
3359
4130
|
const step = jit.automaticPolling ? (() => {
|
|
@@ -3415,9 +4186,13 @@ var BoardRuntime = class {
|
|
|
3415
4186
|
status = "ready";
|
|
3416
4187
|
phase = "assembly";
|
|
3417
4188
|
current;
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
4189
|
+
/** -1: clean; -2: initial solve or multiple dirty slots; otherwise the only dirty slot. */
|
|
4190
|
+
pendingElectricalSlot = -2;
|
|
4191
|
+
/** Flat dirty batch for the multi-slot publication path, stamped to drop repetitions. */
|
|
4192
|
+
dirtyElectricalSlots = /* @__PURE__ */ new Int32Array(0);
|
|
4193
|
+
dirtyElectricalStamps = /* @__PURE__ */ new Int32Array(0);
|
|
4194
|
+
dirtyElectricalLength = 0;
|
|
4195
|
+
dirtyElectricalEpoch = 1;
|
|
3421
4196
|
observations = new ActivationQueue();
|
|
3422
4197
|
reactionBatch = [];
|
|
3423
4198
|
changeObservations = [];
|
|
@@ -3441,7 +4216,27 @@ var BoardRuntime = class {
|
|
|
3441
4216
|
models = new Fifo();
|
|
3442
4217
|
events = new EventQueue();
|
|
3443
4218
|
readyJits = new Fifo();
|
|
4219
|
+
/**
|
|
4220
|
+
* Bumped on every resource-version mutation board wide. The value is bare arithmetic: a
|
|
4221
|
+
* difference proves *some* resource moved, never which one, so it only ever guards
|
|
4222
|
+
* "nothing changed at all" conclusions.
|
|
4223
|
+
*
|
|
4224
|
+
* Mutations cluster inside `publish()` while derived reads spread across the gaps
|
|
4225
|
+
* between publications, so a checkpoint compared against this counter hits far more
|
|
4226
|
+
* often than the raw mutation-to-read ratio suggests: `camera-mcu` sees 8514 bumps and
|
|
4227
|
+
* 5638 derived reads per frame, yet 99.93% of those reads find the epoch unchanged.
|
|
4228
|
+
*/
|
|
4229
|
+
versionEpoch = 0;
|
|
3444
4230
|
waitingJits = /* @__PURE__ */ new Set();
|
|
4231
|
+
/**
|
|
4232
|
+
* Advances whenever a parked JIT is flagged or parks. The global `versionEpoch` cannot
|
|
4233
|
+
* gate the scan: every electrical publish bumps it even when no JIT waits on the slot.
|
|
4234
|
+
*/
|
|
4235
|
+
waiterEpoch = 0;
|
|
4236
|
+
/** `waiterEpoch` at the last parked-JIT scan; equal means no parked JIT can have woken. */
|
|
4237
|
+
waitingScanEpoch = 0;
|
|
4238
|
+
/** Parked JITs whose `pollingWait` is set: their polling boundary needs every round. */
|
|
4239
|
+
pollingWaiterCount = 0;
|
|
3445
4240
|
jits = [];
|
|
3446
4241
|
instances = [];
|
|
3447
4242
|
network;
|
|
@@ -3550,6 +4345,8 @@ var BoardRuntime = class {
|
|
|
3550
4345
|
this.sampleSlotsBySlot.push(slot);
|
|
3551
4346
|
}
|
|
3552
4347
|
this.network = createElectricalNetwork(pins, nets);
|
|
4348
|
+
this.dirtyElectricalSlots = new Int32Array(pins.length);
|
|
4349
|
+
this.dirtyElectricalStamps = new Int32Array(pins.length);
|
|
3553
4350
|
this.phase = "initialize";
|
|
3554
4351
|
for (const instance of this.instances) {
|
|
3555
4352
|
this.current = instance;
|
|
@@ -3615,13 +4412,19 @@ var BoardRuntime = class {
|
|
|
3615
4412
|
processReport() {
|
|
3616
4413
|
const resumable = [];
|
|
3617
4414
|
const generic = [];
|
|
4415
|
+
const programs = [];
|
|
3618
4416
|
for (const instance of this.instances) for (const process of instance.processes) {
|
|
3619
4417
|
const key = `${instance.id}/${process.declaration.kind === "on" ? process.declaration.name : "start"}`;
|
|
3620
4418
|
(process.resumable ? resumable : generic).push(key);
|
|
4419
|
+
programs.push(Object.freeze({
|
|
4420
|
+
key,
|
|
4421
|
+
blocks: process.resumable?.blocks ?? 0
|
|
4422
|
+
}));
|
|
3621
4423
|
}
|
|
3622
4424
|
return Object.freeze({
|
|
3623
4425
|
resumable: Object.freeze(resumable),
|
|
3624
4426
|
generic: Object.freeze(generic),
|
|
4427
|
+
programs: Object.freeze(programs),
|
|
3625
4428
|
bindings: Object.freeze(this.instances.map((instance) => instance.bindingReport()))
|
|
3626
4429
|
});
|
|
3627
4430
|
}
|
|
@@ -3773,7 +4576,7 @@ var BoardRuntime = class {
|
|
|
3773
4576
|
}
|
|
3774
4577
|
}
|
|
3775
4578
|
/**
|
|
3776
|
-
* Fail-closed admission for
|
|
4579
|
+
* Fail-closed admission for resuming generated code without a scheduler round trip.
|
|
3777
4580
|
* The JIT must be the only direct owner, all same-time scheduler stages must be empty,
|
|
3778
4581
|
* and no event may be due through the requested horizon. The target time is exclusive:
|
|
3779
4582
|
* code must not resume at `executionEnd`.
|
|
@@ -3783,20 +4586,118 @@ var BoardRuntime = class {
|
|
|
3783
4586
|
}
|
|
3784
4587
|
invalidate(slot) {
|
|
3785
4588
|
slot.version++;
|
|
4589
|
+
this.versionEpoch++;
|
|
4590
|
+
this.flagWaiters(slot);
|
|
3786
4591
|
if (slot.subscribers.size === 0) return;
|
|
3787
4592
|
for (const observation of slot.subscribers) this.observations.add(observation.order);
|
|
3788
4593
|
}
|
|
4594
|
+
/** Records a resource version bumped outside :func:`invalidate` (direct JIT writes). */
|
|
4595
|
+
noteResourceVersion(slot) {
|
|
4596
|
+
this.versionEpoch++;
|
|
4597
|
+
this.flagWaiters(slot);
|
|
4598
|
+
}
|
|
4599
|
+
/** Flags every parked JIT that recorded `slot`, so the next round rescans it exactly once. */
|
|
4600
|
+
flagWaiters(slot) {
|
|
4601
|
+
const waiters = slot.waiters;
|
|
4602
|
+
if (waiters === void 0 || waiters.size === 0) return;
|
|
4603
|
+
for (const jit of waiters) jit.dependencyDirty = true;
|
|
4604
|
+
this.waiterEpoch++;
|
|
4605
|
+
}
|
|
4606
|
+
/**
|
|
4607
|
+
* Registers a parked JIT on the slots its predicate recorded. A later version bump then
|
|
4608
|
+
* flags that JIT directly, so a scheduler round rescans dependencies only when the
|
|
4609
|
+
* evidence it would read has actually moved.
|
|
4610
|
+
*/
|
|
4611
|
+
watchWaiter(jit) {
|
|
4612
|
+
const dependencies = jit.dependencies;
|
|
4613
|
+
if (jit.waiterSlots === dependencies) return;
|
|
4614
|
+
const previous = jit.waiterSlots;
|
|
4615
|
+
if (previous !== void 0) for (const slot of previous.keys()) slot.waiters?.delete(jit);
|
|
4616
|
+
if (dependencies !== void 0) for (const slot of dependencies.keys()) (slot.waiters ??= /* @__PURE__ */ new Set()).add(jit);
|
|
4617
|
+
jit.waiterSlots = dependencies;
|
|
4618
|
+
jit.dependencyDirty = true;
|
|
4619
|
+
}
|
|
4620
|
+
/**
|
|
4621
|
+
* Parks a JIT on the versions it just recorded. Registering here instead of in the scan
|
|
4622
|
+
* makes `waiterEpoch` a complete wake source: the scan can then be skipped whenever no
|
|
4623
|
+
* parked JIT was flagged or registered since the last scan, and no poll is due.
|
|
4624
|
+
*/
|
|
4625
|
+
parkWaiter(jit) {
|
|
4626
|
+
if (!this.waitingJits.has(jit)) {
|
|
4627
|
+
this.waitingJits.add(jit);
|
|
4628
|
+
if (jit.pollingWait) this.pollingWaiterCount++;
|
|
4629
|
+
}
|
|
4630
|
+
this.watchWaiter(jit);
|
|
4631
|
+
jit.dependencyDirty = true;
|
|
4632
|
+
this.waiterEpoch++;
|
|
4633
|
+
}
|
|
4634
|
+
/** Wakes or drops a parked JIT and removes the registrations that would flag it later. */
|
|
4635
|
+
unparkWaiter(jit) {
|
|
4636
|
+
if (this.waitingJits.delete(jit) && jit.pollingWait) {
|
|
4637
|
+
jit.pollingWait = false;
|
|
4638
|
+
this.pollingWaiterCount--;
|
|
4639
|
+
}
|
|
4640
|
+
const slots = jit.waiterSlots;
|
|
4641
|
+
if (slots !== void 0) {
|
|
4642
|
+
for (const slot of slots.keys()) slot.waiters?.delete(jit);
|
|
4643
|
+
jit.waiterSlots = void 0;
|
|
4644
|
+
}
|
|
4645
|
+
}
|
|
4646
|
+
/**
|
|
4647
|
+
* Checkpoint source for cached dependency evidence. An unchanged value since a previous
|
|
4648
|
+
* checkpoint proves no resource version moved in between, which is enough to reuse that
|
|
4649
|
+
* evidence without scanning it.
|
|
4650
|
+
*/
|
|
4651
|
+
get resourceEpoch() {
|
|
4652
|
+
return this.versionEpoch;
|
|
4653
|
+
}
|
|
3789
4654
|
/** Pin slot of an electrical endpoint; bound once because the board order is fixed. */
|
|
3790
4655
|
electricalSlot(pin) {
|
|
3791
4656
|
return this.electricalSlots.get(pin);
|
|
3792
4657
|
}
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
this.
|
|
3797
|
-
|
|
3798
|
-
|
|
4658
|
+
markElectricalDirty(slot) {
|
|
4659
|
+
const pending = this.pendingElectricalSlot;
|
|
4660
|
+
if (pending === slot) return;
|
|
4661
|
+
const stamps = this.dirtyElectricalStamps;
|
|
4662
|
+
if (slot >= stamps.length) throw new RangeError("Electrical slot is outside the board");
|
|
4663
|
+
const epoch = this.dirtyElectricalEpoch;
|
|
4664
|
+
if (stamps[slot] === epoch) return;
|
|
4665
|
+
stamps[slot] = epoch;
|
|
4666
|
+
if (pending >= 0) {
|
|
4667
|
+
const list = this.dirtyElectricalSlots;
|
|
4668
|
+
list[0] = pending;
|
|
4669
|
+
list[1] = slot;
|
|
4670
|
+
stamps[pending] = epoch;
|
|
4671
|
+
this.dirtyElectricalLength = 2;
|
|
4672
|
+
this.pendingElectricalSlot = -2;
|
|
4673
|
+
return;
|
|
4674
|
+
}
|
|
4675
|
+
if (pending === -1) {
|
|
4676
|
+
this.pendingElectricalSlot = slot;
|
|
4677
|
+
return;
|
|
4678
|
+
}
|
|
4679
|
+
this.dirtyElectricalSlots[this.dirtyElectricalLength++] = slot;
|
|
4680
|
+
}
|
|
4681
|
+
/** Dirty slot count for diagnostics, not used by the publication path. */
|
|
4682
|
+
get dirtyElectricalCount() {
|
|
4683
|
+
return this.pendingElectricalSlot >= 0 ? 1 : this.dirtyElectricalLength;
|
|
3799
4684
|
}
|
|
4685
|
+
publish() {
|
|
4686
|
+
const pending = this.pendingElectricalSlot;
|
|
4687
|
+
if (pending === -1) return;
|
|
4688
|
+
if (pending >= 0) this.network.solveSlotInto(pending, this.publishSlot);
|
|
4689
|
+
else this.network.solveSlotBatchInto(this.dirtyElectricalSlots, this.dirtyElectricalLength, this.publishSlot);
|
|
4690
|
+
if (pending === -2) this.dirtyElectricalLength = 0;
|
|
4691
|
+
if (++this.dirtyElectricalEpoch === 2147483647) {
|
|
4692
|
+
this.dirtyElectricalStamps.fill(0);
|
|
4693
|
+
this.dirtyElectricalEpoch = 1;
|
|
4694
|
+
}
|
|
4695
|
+
this.pendingElectricalSlot = -1;
|
|
4696
|
+
}
|
|
4697
|
+
/** Publication sink: enter the observation queue for one published board slot. */
|
|
4698
|
+
publishSlot = (slot) => {
|
|
4699
|
+
this.invalidate(this.sampleSlotsBySlot[slot]);
|
|
4700
|
+
};
|
|
3800
4701
|
bindObservation(observation, path) {
|
|
3801
4702
|
const instance = observation.instance;
|
|
3802
4703
|
observation.previous = instance.observe(observation, path);
|
|
@@ -3849,7 +4750,8 @@ var BoardRuntime = class {
|
|
|
3849
4750
|
dependencies: /* @__PURE__ */ new Map(),
|
|
3850
4751
|
entry: {
|
|
3851
4752
|
dependencies: /* @__PURE__ */ new Map(),
|
|
3852
|
-
value: void 0
|
|
4753
|
+
value: void 0,
|
|
4754
|
+
epoch: -1
|
|
3853
4755
|
},
|
|
3854
4756
|
order: 0,
|
|
3855
4757
|
previous: void 0,
|
|
@@ -3889,11 +4791,17 @@ var BoardRuntime = class {
|
|
|
3889
4791
|
commitChain(chain, value) {
|
|
3890
4792
|
const path = chain.plan.path;
|
|
3891
4793
|
if (path.length !== 1) return;
|
|
3892
|
-
|
|
4794
|
+
const slots = chain.plan.slots;
|
|
4795
|
+
const dependencies = chain.dependencies;
|
|
4796
|
+
for (let index = 0; index < slots.length; index++) {
|
|
4797
|
+
const slot = slots[index];
|
|
4798
|
+
dependencies.set(slot, slot.version);
|
|
4799
|
+
}
|
|
3893
4800
|
const entry = chain.entry;
|
|
3894
4801
|
if (entry.value !== value) entry.value = value;
|
|
3895
|
-
entry.dependencies =
|
|
3896
|
-
|
|
4802
|
+
entry.dependencies = dependencies;
|
|
4803
|
+
entry.epoch = this.versionEpoch;
|
|
4804
|
+
chain.instance.setDerived(path[0], entry);
|
|
3897
4805
|
}
|
|
3898
4806
|
collectChain(chain) {
|
|
3899
4807
|
const value = chain.plan.evaluate();
|
|
@@ -3951,28 +4859,15 @@ var BoardRuntime = class {
|
|
|
3951
4859
|
}
|
|
3952
4860
|
boundary() {
|
|
3953
4861
|
for (;;) {
|
|
3954
|
-
this.publish();
|
|
4862
|
+
if (this.pendingElectricalSlot !== -1) this.publish();
|
|
3955
4863
|
if (!this.observations.size) return;
|
|
3956
4864
|
const reactions = this.reactionBatch;
|
|
3957
|
-
reactions.length = 0;
|
|
4865
|
+
if (reactions.length) reactions.length = 0;
|
|
3958
4866
|
const changes = this.changeObservations;
|
|
3959
|
-
changes.length = 0;
|
|
3960
|
-
this.changePrevious.length = 0;
|
|
3961
|
-
this.changeCurrent.length = 0;
|
|
3962
|
-
|
|
3963
|
-
for (let index = 0; index < batch.length; index++) {
|
|
3964
|
-
const observation = batch[index];
|
|
3965
|
-
if ("plan" in observation) {
|
|
3966
|
-
this.collectChain(observation);
|
|
3967
|
-
continue;
|
|
3968
|
-
}
|
|
3969
|
-
const current = observation.readSource();
|
|
3970
|
-
if (same(observation.previous, current)) continue;
|
|
3971
|
-
const previous = observation.previous;
|
|
3972
|
-
observation.previous = current;
|
|
3973
|
-
if (changes.length) this.recordChange(observation, previous, current);
|
|
3974
|
-
else this.applyChange(observation, previous, current);
|
|
3975
|
-
}
|
|
4867
|
+
if (changes.length) changes.length = 0;
|
|
4868
|
+
if (this.changePrevious.length) this.changePrevious.length = 0;
|
|
4869
|
+
if (this.changeCurrent.length) this.changeCurrent.length = 0;
|
|
4870
|
+
this.observations.drain(this.consumeObservation);
|
|
3976
4871
|
const count = changes.length;
|
|
3977
4872
|
if (count) {
|
|
3978
4873
|
for (let index = 0; index < count; index++) this.applyChange(changes[index], this.changePrevious[index], this.changeCurrent[index]);
|
|
@@ -3984,6 +4879,19 @@ var BoardRuntime = class {
|
|
|
3984
4879
|
else return;
|
|
3985
4880
|
}
|
|
3986
4881
|
}
|
|
4882
|
+
/** One due receiver: chains collapse their members, plain sources apply inline. */
|
|
4883
|
+
consumeObservation = (observation) => {
|
|
4884
|
+
if ("plan" in observation) {
|
|
4885
|
+
this.collectChain(observation);
|
|
4886
|
+
return;
|
|
4887
|
+
}
|
|
4888
|
+
const current = observation.readSource();
|
|
4889
|
+
if (same(observation.previous, current)) return;
|
|
4890
|
+
const previous = observation.previous;
|
|
4891
|
+
observation.previous = current;
|
|
4892
|
+
if (this.changeObservations.length) this.recordChange(observation, previous, current);
|
|
4893
|
+
else this.applyChange(observation, previous, current);
|
|
4894
|
+
};
|
|
3987
4895
|
/** Shared capture barrier and ordered body runner for generic and static boundaries. */
|
|
3988
4896
|
runBoundaryReactions(reactions) {
|
|
3989
4897
|
try {
|
|
@@ -4088,13 +4996,15 @@ var BoardRuntime = class {
|
|
|
4088
4996
|
* the body reports ticks/receive through the frame and returns whether it suspended.
|
|
4089
4997
|
*/
|
|
4090
4998
|
driveResumable(process, program, value) {
|
|
4091
|
-
|
|
4999
|
+
const instance = process.instance;
|
|
5000
|
+
this.current = instance;
|
|
4092
5001
|
process.state = "running";
|
|
4093
5002
|
const state = process.frame ??= frame(program);
|
|
5003
|
+
const host = instance.host;
|
|
4094
5004
|
let input = value;
|
|
4095
5005
|
for (;;) {
|
|
4096
5006
|
state.value = input;
|
|
4097
|
-
if (!program.run(
|
|
5007
|
+
if (!program.run(host, void 0, void 0, state)) {
|
|
4098
5008
|
this.boundary();
|
|
4099
5009
|
process.state = "idle";
|
|
4100
5010
|
if (process.pending) {
|
|
@@ -4104,7 +5014,7 @@ var BoardRuntime = class {
|
|
|
4104
5014
|
return;
|
|
4105
5015
|
}
|
|
4106
5016
|
if (state.kind === RESUMABLE_RECEIVE) {
|
|
4107
|
-
const channel =
|
|
5017
|
+
const channel = instance.channels.get(state.channel);
|
|
4108
5018
|
if (channel.incoming.length) {
|
|
4109
5019
|
input = channel.incoming.shift().value;
|
|
4110
5020
|
continue;
|
|
@@ -4174,40 +5084,48 @@ var BoardRuntime = class {
|
|
|
4174
5084
|
continue;
|
|
4175
5085
|
}
|
|
4176
5086
|
let pollingBoundary = Infinity;
|
|
4177
|
-
if (this.waitingJits.size
|
|
4178
|
-
|
|
4179
|
-
|
|
4180
|
-
|
|
4181
|
-
if (jit.
|
|
4182
|
-
|
|
4183
|
-
|
|
4184
|
-
|
|
4185
|
-
|
|
4186
|
-
|
|
4187
|
-
|
|
4188
|
-
|
|
4189
|
-
this.
|
|
4190
|
-
|
|
5087
|
+
if (this.waitingJits.size && (this.waiterEpoch !== this.waitingScanEpoch || this.pollingWaiterCount !== 0)) {
|
|
5088
|
+
this.waitingScanEpoch = this.waiterEpoch;
|
|
5089
|
+
for (let index = 0; index < this.jits.length; index++) {
|
|
5090
|
+
const jit = this.jits[index];
|
|
5091
|
+
if (!jit.active || jit.stopped || !this.waitingJits.has(jit)) continue;
|
|
5092
|
+
this.watchWaiter(jit);
|
|
5093
|
+
if (!jit.dependencyDirty && !jit.pollingWait) continue;
|
|
5094
|
+
if (jit.instance.dependenciesCurrent(jit.dependencies)) {
|
|
5095
|
+
jit.dependencyDirty = false;
|
|
5096
|
+
if (jit.pollingWait && jit.pollingPeriod > 0) {
|
|
5097
|
+
const lastStart = MAX_TIME - (MAX_TIME - jit.pollingOrigin) % jit.pollingPeriod;
|
|
5098
|
+
if (lastStart < 72e14 && lastStart < end) {
|
|
5099
|
+
if (lastStart === this.time) {
|
|
5100
|
+
jit.pollingToken++;
|
|
5101
|
+
delete jit.dependencies;
|
|
5102
|
+
this.unparkWaiter(jit);
|
|
5103
|
+
this.queueJit(jit);
|
|
5104
|
+
} else pollingBoundary = Math.min(pollingBoundary, lastStart);
|
|
5105
|
+
}
|
|
4191
5106
|
}
|
|
5107
|
+
continue;
|
|
4192
5108
|
}
|
|
4193
|
-
|
|
4194
|
-
|
|
4195
|
-
|
|
4196
|
-
jit.pollingWait = false;
|
|
4197
|
-
jit.pollingToken++;
|
|
4198
|
-
delete jit.dependencies;
|
|
4199
|
-
this.waitingJits.delete(jit);
|
|
4200
|
-
const remainder = jit.pollingPeriod > 0 ? (this.time - jit.pollingOrigin) % jit.pollingPeriod : 0;
|
|
4201
|
-
if (remainder) this.events.push(time(this.time + (jit.pollingPeriod - remainder)), () => this.queueJit(jit));
|
|
4202
|
-
else this.queueJit(jit);
|
|
4203
|
-
} else if (jit.predicate) {
|
|
4204
|
-
const checked = jit.instance.evaluate(() => jit.instance.syncCall(jit.predicate));
|
|
4205
|
-
jit.dependencies = checked.dependencies;
|
|
4206
|
-
if (checked.value === true) {
|
|
4207
|
-
delete jit.predicate;
|
|
5109
|
+
jit.dependencyDirty = false;
|
|
5110
|
+
if (jit.pollingWait) {
|
|
5111
|
+
jit.pollingToken++;
|
|
4208
5112
|
delete jit.dependencies;
|
|
4209
|
-
this.
|
|
4210
|
-
this.
|
|
5113
|
+
this.unparkWaiter(jit);
|
|
5114
|
+
const remainder = jit.pollingPeriod > 0 ? (this.time - jit.pollingOrigin) % jit.pollingPeriod : 0;
|
|
5115
|
+
if (remainder) this.events.push(time(this.time + (jit.pollingPeriod - remainder)), () => this.queueJit(jit));
|
|
5116
|
+
else this.queueJit(jit);
|
|
5117
|
+
} else if (jit.predicate) {
|
|
5118
|
+
const checked = jit.instance.evaluate(() => syncJitCall(jit, jit.predicate));
|
|
5119
|
+
jit.dependencies = checked.dependencies;
|
|
5120
|
+
if (checked.value === true) {
|
|
5121
|
+
delete jit.predicate;
|
|
5122
|
+
delete jit.dependencies;
|
|
5123
|
+
this.unparkWaiter(jit);
|
|
5124
|
+
this.queueJit(jit);
|
|
5125
|
+
} else {
|
|
5126
|
+
this.watchWaiter(jit);
|
|
5127
|
+
this.waiterEpoch++;
|
|
5128
|
+
}
|
|
4211
5129
|
}
|
|
4212
5130
|
}
|
|
4213
5131
|
}
|
|
@@ -4220,8 +5138,10 @@ var BoardRuntime = class {
|
|
|
4220
5138
|
this.time = pollingBoundary;
|
|
4221
5139
|
continue;
|
|
4222
5140
|
}
|
|
4223
|
-
|
|
4224
|
-
|
|
5141
|
+
const next = this.events.time;
|
|
5142
|
+
if (next <= end) {
|
|
5143
|
+
this.time = next;
|
|
5144
|
+
while (this.events.time === this.time) this.events.pop()();
|
|
4225
5145
|
continue;
|
|
4226
5146
|
}
|
|
4227
5147
|
this.time = end;
|