@onda-lang/binaryen-web 0.8.40 → 0.9.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/LICENSE +202 -19
- package/README.md +1 -2
- package/api.md +46 -7
- package/package.json +4 -4
- package/src/compiler/core.js +173 -66
- package/src/compiler/index.js +5 -3
- package/src/compiler/lowering.js +129 -51
- package/src/compiler/resource-origins.js +82 -0
- package/src/constants.js +1 -1
- package/src/index.d.ts +0 -1
- package/src/index.js +0 -3
package/src/compiler/lowering.js
CHANGED
|
@@ -66,6 +66,13 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
66
66
|
context,
|
|
67
67
|
);
|
|
68
68
|
}
|
|
69
|
+
if (["buffer", "buffer_span", "buffer_map"].includes(this.type(destinationType).kind)) {
|
|
70
|
+
const components = this.compileResourceRvalue(data.value, destinationType, context);
|
|
71
|
+
if (data.destination.base.kind !== "local" || data.destination.projections.length !== 0) this.fail("buffer descriptor destination must be an unprojected local");
|
|
72
|
+
const layout = context.localLayouts[data.destination.base.data];
|
|
73
|
+
if (layout.components.length !== components.length) this.fail("buffer descriptor component count differs");
|
|
74
|
+
return this.module.block(null, components.map((component, offset) => this.module.local.set(layout.index + offset, component)));
|
|
75
|
+
}
|
|
69
76
|
const scalar = this.placeScalarType(data.destination, context);
|
|
70
77
|
const value = this.compileRvalue(data.value, scalar, context);
|
|
71
78
|
return this.storePlace(data.destination, value, scalar, context);
|
|
@@ -219,7 +226,8 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
219
226
|
}
|
|
220
227
|
this.fail(`buffer call argument ${index} of '${target.name}' is invalid`);
|
|
221
228
|
}
|
|
222
|
-
if (parameterType.kind
|
|
229
|
+
if (["buffer_span", "buffer_map"].includes(parameterType.kind)) {
|
|
230
|
+
if (argument.kind === "value" && argument.data.kind === "local") return this.resourcePlaceComponents({ base: argument.data, projections: [] }, context);
|
|
223
231
|
if (argument.kind !== "buffer_span") {
|
|
224
232
|
this.fail(`buffer span call argument ${index} of '${target.name}' is invalid`);
|
|
225
233
|
}
|
|
@@ -236,7 +244,14 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
236
244
|
this.requireScalarType(result, `result ${resultId} of '${target.name}'`),
|
|
237
245
|
);
|
|
238
246
|
const resultType = this.wasmResultType(resultScalars);
|
|
239
|
-
const
|
|
247
|
+
const packed = this.functionAbiLayout[data.function];
|
|
248
|
+
const callArgs = packed ? [this.module.i32.const(packed.address)] : args;
|
|
249
|
+
const directCall = this.module.call(this.functionNames[data.function], callArgs, resultType);
|
|
250
|
+
const call = packed ? this.module.block(null, [
|
|
251
|
+
...packed.entries.map(({ scalar, address }, index) =>
|
|
252
|
+
this.storeScalar(scalar, this.module.i32.const(address), args[index])),
|
|
253
|
+
directCall,
|
|
254
|
+
], resultType) : directCall;
|
|
240
255
|
let compiledCall;
|
|
241
256
|
if (data.results.length === 0) {
|
|
242
257
|
compiledCall = call;
|
|
@@ -1003,10 +1018,10 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1003
1018
|
return this.compileBufferLoad(data, context);
|
|
1004
1019
|
case "buffer_param_load":
|
|
1005
1020
|
return this.compileBufferParamLoad(data, context);
|
|
1006
|
-
case "
|
|
1007
|
-
return this.
|
|
1008
|
-
case "
|
|
1009
|
-
return this.
|
|
1021
|
+
case "buffer_samples":
|
|
1022
|
+
return this.compileBufferSamples(data, context);
|
|
1023
|
+
case "buffer_param_samples":
|
|
1024
|
+
return this.compileBufferParamSamples(data, context);
|
|
1010
1025
|
case "buffer_channels":
|
|
1011
1026
|
return this.compileBufferChannels(data, context);
|
|
1012
1027
|
case "buffer_param_channels":
|
|
@@ -1036,6 +1051,27 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1036
1051
|
}
|
|
1037
1052
|
}
|
|
1038
1053
|
|
|
1054
|
+
resourcePlaceComponents(place, context) {
|
|
1055
|
+
if (place.projections.length !== 0) this.fail("buffer descriptor places cannot have projections");
|
|
1056
|
+
const layout = place.base.kind === "local" ? context.localLayouts[place.base.data]
|
|
1057
|
+
: place.base.kind === "parameter" ? context.paramLayouts[place.base.data] : null;
|
|
1058
|
+
if (!layout || !["buffer", "buffer_span", "buffer_map"].includes(layout.kind)) this.fail("resource place has no buffer descriptor");
|
|
1059
|
+
return layout.components.map((scalar, offset) => this.module.local.get(layout.index + offset, this.wasmType(scalar)));
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
compileResourceRvalue(rvalue, typeId, context) {
|
|
1063
|
+
if (rvalue.kind === "use" && rvalue.data.kind === "local") return this.resourcePlaceComponents({ base: rvalue.data, projections: [] }, context);
|
|
1064
|
+
if (rvalue.kind === "load") return this.resourcePlaceComponents(rvalue.data, context);
|
|
1065
|
+
if (rvalue.kind !== "make_buffer" || !this.typesEquivalent(rvalue.data.ty, typeId)) this.fail("invalid buffer descriptor rvalue");
|
|
1066
|
+
const source = rvalue.data.source;
|
|
1067
|
+
if (source.kind === "buffer") return this.compileInterfaceBufferValue(source.data, context);
|
|
1068
|
+
if (source.kind === "buffer_param") return this.loadBufferParamValue(source.data, context);
|
|
1069
|
+
if (source.kind === "buffer_span") return this.compileBufferSpanValue(source.data, this.type(typeId), context);
|
|
1070
|
+
if (source.kind === "place") return this.resourcePlaceComponents(source.data, context);
|
|
1071
|
+
if (source.kind === "value" && source.data.kind === "local") return this.resourcePlaceComponents({ base: source.data, projections: [] }, context);
|
|
1072
|
+
this.fail("make_buffer source is not a buffer descriptor");
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1039
1075
|
compileSliceRvalue(rvalue, context) {
|
|
1040
1076
|
switch (rvalue.kind) {
|
|
1041
1077
|
case "use":
|
|
@@ -1410,7 +1446,7 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1410
1446
|
);
|
|
1411
1447
|
}
|
|
1412
1448
|
|
|
1413
|
-
|
|
1449
|
+
compileBufferSamples(bufferRef, context) {
|
|
1414
1450
|
this.requireBufferRef(bufferRef);
|
|
1415
1451
|
return this.withBufferRefIndex(
|
|
1416
1452
|
bufferRef,
|
|
@@ -1442,13 +1478,13 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1442
1478
|
|
|
1443
1479
|
compileBufferChannels(bufferRef, context) {
|
|
1444
1480
|
const channels = this.bufferRefChannelMetadata(bufferRef);
|
|
1445
|
-
if (channels.kind === "mono") return this.module.i32.const(1);
|
|
1446
|
-
if (channels.kind === "static") return this.module.i32.const(channels.count);
|
|
1447
1481
|
return this.withBufferRefIndex(
|
|
1448
1482
|
bufferRef,
|
|
1449
1483
|
context,
|
|
1450
1484
|
binaryen.i32,
|
|
1451
|
-
(index, staticIndex) =>
|
|
1485
|
+
(index, staticIndex) => channels.kind !== "dynamic"
|
|
1486
|
+
? this.module.i32.const(channels.count ?? 1)
|
|
1487
|
+
: this.bufferTableValueFactory(
|
|
1452
1488
|
POINTER_GLOBALS.bufferChannels,
|
|
1453
1489
|
index,
|
|
1454
1490
|
staticIndex,
|
|
@@ -1458,7 +1494,7 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1458
1494
|
);
|
|
1459
1495
|
}
|
|
1460
1496
|
|
|
1461
|
-
|
|
1497
|
+
compileBufferParamSamples(parameterId, context) {
|
|
1462
1498
|
this.bufferParamType(parameterId, context);
|
|
1463
1499
|
return this.withBufferParamSelector(
|
|
1464
1500
|
parameterId,
|
|
@@ -1481,13 +1517,13 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1481
1517
|
type.data.channels,
|
|
1482
1518
|
type.data.element,
|
|
1483
1519
|
);
|
|
1484
|
-
if (channels.kind === "mono") return this.module.i32.const(1);
|
|
1485
|
-
if (channels.kind === "static") return this.module.i32.const(channels.count);
|
|
1486
1520
|
return this.withBufferParamSelector(
|
|
1487
1521
|
parameterId,
|
|
1488
1522
|
context,
|
|
1489
1523
|
binaryen.i32,
|
|
1490
|
-
(selector, staticSelector) =>
|
|
1524
|
+
(selector, staticSelector) => channels.kind !== "dynamic"
|
|
1525
|
+
? this.module.i32.const(channels.count ?? 1)
|
|
1526
|
+
: this.bufferParamComponentFactory(
|
|
1491
1527
|
parameterId,
|
|
1492
1528
|
3,
|
|
1493
1529
|
"i32",
|
|
@@ -1499,18 +1535,21 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1499
1535
|
}
|
|
1500
1536
|
|
|
1501
1537
|
bufferParamType(parameterId, context) {
|
|
1502
|
-
const
|
|
1538
|
+
const id = this.bufferParamIds(parameterId, context)[0];
|
|
1539
|
+
const parameter = ["local", "local_element"].includes(parameterId?.kind) ? context.function.locals[id] : context.function.params[id];
|
|
1503
1540
|
const type = parameter && this.type(parameter.ty);
|
|
1504
|
-
const
|
|
1505
|
-
? "buffer_span"
|
|
1506
|
-
: "buffer";
|
|
1507
|
-
if (!type || type.kind
|
|
1541
|
+
const expectedKinds = ["array_element", "local_element"].includes(parameterId?.kind)
|
|
1542
|
+
? ["buffer_span", "buffer_map"]
|
|
1543
|
+
: ["buffer"];
|
|
1544
|
+
if (!type || !expectedKinds.includes(type.kind)) {
|
|
1508
1545
|
this.fail(`parameter id ${parameterId} is not a buffer`);
|
|
1509
1546
|
}
|
|
1510
1547
|
return type;
|
|
1511
1548
|
}
|
|
1512
1549
|
|
|
1513
1550
|
bufferParamIds(parameterRef, context) {
|
|
1551
|
+
if (parameterRef?.kind === "local" && Number.isInteger(parameterRef.data) && context.function.locals[parameterRef.data]) return [parameterRef.data];
|
|
1552
|
+
if (parameterRef?.kind === "local_element" && Number.isInteger(parameterRef.data?.collection) && context.function.locals[parameterRef.data.collection]) return [parameterRef.data.collection];
|
|
1514
1553
|
if (Number.isInteger(parameterRef)) return [parameterRef];
|
|
1515
1554
|
if (
|
|
1516
1555
|
parameterRef?.kind === "direct"
|
|
@@ -1529,16 +1568,17 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1529
1568
|
this.fail("invalid buffer parameter reference");
|
|
1530
1569
|
}
|
|
1531
1570
|
|
|
1532
|
-
bufferParamLayout(
|
|
1533
|
-
const
|
|
1534
|
-
|
|
1571
|
+
bufferParamLayout(parameterRef, context) {
|
|
1572
|
+
const parameterId = this.bufferParamIds(parameterRef, context)[0];
|
|
1573
|
+
const layout = ["local", "local_element"].includes(parameterRef?.kind) ? context.localLayouts[parameterId] : context.paramLayouts[parameterId];
|
|
1574
|
+
if (!layout || !["buffer", "buffer_span", "buffer_map"].includes(layout.kind)) {
|
|
1535
1575
|
this.fail(`parameter id ${parameterId} has no buffer descriptor`);
|
|
1536
1576
|
}
|
|
1537
1577
|
return layout;
|
|
1538
1578
|
}
|
|
1539
1579
|
|
|
1540
1580
|
staticBufferParamSelector(parameterRef, context) {
|
|
1541
|
-
if (parameterRef?.kind
|
|
1581
|
+
if (!["array_element", "local_element"].includes(parameterRef?.kind)) return null;
|
|
1542
1582
|
const reference = parameterRef.data;
|
|
1543
1583
|
const type = this.bufferParamType(parameterRef, context);
|
|
1544
1584
|
const selector = reference.selector;
|
|
@@ -1571,7 +1611,7 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1571
1611
|
}
|
|
1572
1612
|
|
|
1573
1613
|
withBufferParamSelector(parameterRef, context, resultType, build) {
|
|
1574
|
-
if (parameterRef?.kind
|
|
1614
|
+
if (!["array_element", "local_element"].includes(parameterRef?.kind)) {
|
|
1575
1615
|
return build(null, null, []);
|
|
1576
1616
|
}
|
|
1577
1617
|
const staticSelector = this.staticBufferParamSelector(parameterRef, context);
|
|
@@ -1610,12 +1650,17 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1610
1650
|
context,
|
|
1611
1651
|
) {
|
|
1612
1652
|
const parameterId = this.bufferParamIds(parameterRef, context)[0];
|
|
1613
|
-
const layout = this.bufferParamLayout(
|
|
1614
|
-
if (parameterRef?.kind
|
|
1653
|
+
const layout = this.bufferParamLayout(parameterRef, context);
|
|
1654
|
+
if (!["array_element", "local_element"].includes(parameterRef?.kind)) {
|
|
1615
1655
|
return () =>
|
|
1616
1656
|
this.module.local.get(layout.index + offset, this.wasmType(scalar));
|
|
1617
1657
|
}
|
|
1618
1658
|
const rawLoad = (index) => {
|
|
1659
|
+
if (layout.kind === "buffer_map") {
|
|
1660
|
+
const map = this.module.local.get(layout.index + 6, binaryen.i32);
|
|
1661
|
+
index = this.loadScalar("i32", this.module.i32.add(map,
|
|
1662
|
+
this.module.i32.mul(index, this.module.i32.const(4))));
|
|
1663
|
+
}
|
|
1619
1664
|
const table = this.module.local.get(layout.index + offset, binaryen.i32);
|
|
1620
1665
|
const address = this.module.i32.add(
|
|
1621
1666
|
table,
|
|
@@ -1639,9 +1684,9 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1639
1684
|
context,
|
|
1640
1685
|
);
|
|
1641
1686
|
};
|
|
1642
|
-
if (staticSelector === null) return () => load(selector());
|
|
1687
|
+
if (staticSelector === null || ["local", "local_element"].includes(parameterRef?.kind)) return () => load(selector());
|
|
1643
1688
|
|
|
1644
|
-
const key = `buffer_param:${parameterId}:${staticSelector}:${offset}:${scalar}`;
|
|
1689
|
+
const key = `buffer_param:${parameterRef?.kind ?? "parameter"}:${parameterId}:${staticSelector}:${offset}:${scalar}`;
|
|
1645
1690
|
let local = context.bufferDescriptorCache.get(key);
|
|
1646
1691
|
if (local === undefined) {
|
|
1647
1692
|
local = this.allocateGeneratedLocal(
|
|
@@ -1703,7 +1748,7 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1703
1748
|
|
|
1704
1749
|
loadBufferParamValue(parameterId, context) {
|
|
1705
1750
|
const components = ["i32", "i32", "i32", "i32", "f32", "i32"];
|
|
1706
|
-
if (parameterId?.kind
|
|
1751
|
+
if (!["array_element", "local_element"].includes(parameterId?.kind)) {
|
|
1707
1752
|
return components.map((scalar, offset) =>
|
|
1708
1753
|
this.loadBufferParamComponent(parameterId, offset, scalar, context),
|
|
1709
1754
|
);
|
|
@@ -1746,13 +1791,7 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1746
1791
|
}
|
|
1747
1792
|
|
|
1748
1793
|
loadBufferPlace(place, context) {
|
|
1749
|
-
|
|
1750
|
-
this.fail("buffer call arguments must be unprojected buffer parameters");
|
|
1751
|
-
}
|
|
1752
|
-
const layout = this.bufferParamLayout(place.base.data, context);
|
|
1753
|
-
return layout.components.map((scalar, offset) =>
|
|
1754
|
-
this.module.local.get(layout.index + offset, this.wasmType(scalar)),
|
|
1755
|
-
);
|
|
1794
|
+
return this.resourcePlaceComponents(place, context);
|
|
1756
1795
|
}
|
|
1757
1796
|
|
|
1758
1797
|
compileInterfaceBufferValue(bufferRef, context) {
|
|
@@ -1821,37 +1860,76 @@ export class MirCompilerLowering extends MirCompilerCore {
|
|
|
1821
1860
|
const tableScalars = ["i32", "i32", "i32", "i32", "f32", "i32"];
|
|
1822
1861
|
let tables;
|
|
1823
1862
|
let start;
|
|
1863
|
+
let resourceMap = null;
|
|
1864
|
+
const mapped = expectedType.kind === "buffer_map";
|
|
1865
|
+
const expectedChannels = this.bufferChannelMetadata(expectedType.data.channels, expectedType.data.element);
|
|
1866
|
+
const accepts = (source) => {
|
|
1867
|
+
if (!source || source.element !== expectedType.data.element
|
|
1868
|
+
|| !["read_only", "read_write"].includes(source.access)
|
|
1869
|
+
|| (expectedType.data.access === "read_write" && source.access !== "read_write")) return false;
|
|
1870
|
+
const channels = this.bufferChannelMetadata(source.channels, source.element);
|
|
1871
|
+
return expectedChannels.kind === "dynamic"
|
|
1872
|
+
|| (channels.kind !== "dynamic"
|
|
1873
|
+
&& (channels.count ?? 1) === (expectedChannels.count ?? 1));
|
|
1874
|
+
};
|
|
1824
1875
|
if (spanRef.kind === "interface") {
|
|
1825
|
-
if (
|
|
1826
|
-
!Number.isInteger(data.first)
|
|
1827
|
-
|| data.first < 0
|
|
1876
|
+
if (mapped || !Number.isInteger(data.first) || data.first < 0
|
|
1828
1877
|
|| data.first + data.len > this.mir.interface.buffers.length
|
|
1829
|
-
|
|
1830
|
-
this.fail("interface buffer span is out of range");
|
|
1878
|
+
|| !this.mir.interface.buffers.slice(data.first, data.first + data.len).every(accepts)) {
|
|
1879
|
+
this.fail("interface buffer span is incompatible or out of range");
|
|
1831
1880
|
}
|
|
1832
1881
|
tables = tableGlobals.map((name) => this.module.global.get(name, binaryen.i32));
|
|
1833
1882
|
start = data.first;
|
|
1883
|
+
} else if (spanRef.kind === "mapped_interface") {
|
|
1884
|
+
const map = this.mir.buffer_maps?.[data.map];
|
|
1885
|
+
if (!mapped || !Number.isInteger(data.map) || !map
|
|
1886
|
+
|| !Number.isInteger(data.start) || data.start < 0
|
|
1887
|
+
|| data.start + data.len > map.buffers.length
|
|
1888
|
+
|| !map.buffers.slice(data.start, data.start + data.len)
|
|
1889
|
+
.every((id) => accepts(this.mir.interface.buffers[id]))) {
|
|
1890
|
+
this.fail("mapped buffer collection is incompatible or out of range");
|
|
1891
|
+
}
|
|
1892
|
+
tables = tableGlobals.map((name) => this.module.global.get(name, binaryen.i32));
|
|
1893
|
+
start = data.start;
|
|
1894
|
+
resourceMap = this.module.i32.const(this.bufferMapLayout[data.map].address);
|
|
1895
|
+
} else if (spanRef.kind === "local") {
|
|
1896
|
+
const source = context.function.locals[data.collection];
|
|
1897
|
+
const sourceType = source && this.type(source.ty);
|
|
1898
|
+
const layout = context.localLayouts[data.collection];
|
|
1899
|
+
if (!sourceType || sourceType.kind !== expectedType.kind || !layout || layout.kind !== expectedType.kind || !accepts(sourceType.data) || data.len > sourceType.data.len) this.fail("local buffer collection window is incompatible");
|
|
1900
|
+
const indexLocal = this.allocateGeneratedLocal(context, "i32", "buffer_collection.window");
|
|
1901
|
+
const initialize = this.module.local.set(indexLocal, this.compileDynamicBoundedIndex(
|
|
1902
|
+
() => this.compileValue(data.start, context), () => this.module.i32.const(sourceType.data.len - data.len + 1), data.bounds, context, true));
|
|
1903
|
+
const index = () => this.module.local.get(indexLocal, binaryen.i32);
|
|
1904
|
+
const values = layout.components.map((_, offset) => {
|
|
1905
|
+
const base = this.module.local.get(layout.index + offset, binaryen.i32);
|
|
1906
|
+
if (mapped && offset < 6) return base;
|
|
1907
|
+
const size = mapped ? 4 : this.scalarSize(tableScalars[offset]);
|
|
1908
|
+
return this.module.i32.add(base, this.module.i32.mul(index(), this.module.i32.const(size)));
|
|
1909
|
+
});
|
|
1910
|
+
values[0] = this.module.block(null, [initialize, values[0]], binaryen.i32);
|
|
1911
|
+
return values;
|
|
1834
1912
|
} else if (spanRef.kind === "parameter") {
|
|
1835
1913
|
const parameter = context.function.params[data.span];
|
|
1836
1914
|
const sourceType = parameter && this.type(parameter.ty);
|
|
1837
1915
|
const layout = context.paramLayouts[data.span];
|
|
1838
|
-
if (
|
|
1839
|
-
!
|
|
1840
|
-
||
|
|
1841
|
-
|| !layout
|
|
1842
|
-
|| layout.kind !== "buffer_span"
|
|
1843
|
-
|| !Number.isInteger(data.start)
|
|
1844
|
-
|| data.start < 0
|
|
1916
|
+
if (!sourceType || sourceType.kind !== expectedType.kind
|
|
1917
|
+
|| !layout || layout.kind !== expectedType.kind
|
|
1918
|
+
|| !Number.isInteger(data.start) || data.start < 0
|
|
1845
1919
|
|| data.start + data.len > sourceType.data.len
|
|
1846
|
-
|
|
1847
|
-
this.fail("buffer
|
|
1920
|
+
|| !accepts(sourceType.data)) {
|
|
1921
|
+
this.fail("buffer collection parameter window is incompatible or out of range");
|
|
1848
1922
|
}
|
|
1849
|
-
tables = layout.components.map((_, offset) =>
|
|
1923
|
+
tables = layout.components.slice(0, 6).map((_, offset) =>
|
|
1850
1924
|
this.module.local.get(layout.index + offset, binaryen.i32));
|
|
1851
1925
|
start = data.start;
|
|
1926
|
+
if (mapped) resourceMap = this.module.local.get(layout.index + 6, binaryen.i32);
|
|
1852
1927
|
} else {
|
|
1853
1928
|
this.fail("invalid buffer span reference");
|
|
1854
1929
|
}
|
|
1930
|
+
if (mapped) {
|
|
1931
|
+
return [...tables, this.module.i32.add(resourceMap, this.module.i32.const(start * 4))];
|
|
1932
|
+
}
|
|
1855
1933
|
return tables.map((table, offset) => this.module.i32.add(
|
|
1856
1934
|
table,
|
|
1857
1935
|
this.module.i32.const(start * this.scalarSize(tableScalars[offset])),
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// Collection provenance stays symbolic until an operation demands a slot range.
|
|
2
|
+
export function selectedResourceRange(selector, len, bounds) {
|
|
3
|
+
if (!Number.isInteger(len) || len <= 0) return [0, 0];
|
|
4
|
+
if (selector?.kind === "constant" && selector.data?.type === "i32"
|
|
5
|
+
&& Number.isInteger(selector.data.value)) {
|
|
6
|
+
let slot = selector.data.value;
|
|
7
|
+
if (bounds === "clamp") slot = Math.min(len - 1, Math.max(0, slot));
|
|
8
|
+
if (slot >= 0 && slot < len) return [slot, slot + 1];
|
|
9
|
+
}
|
|
10
|
+
return [0, len];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class CollectionOrigins {
|
|
14
|
+
constructor(maps, origins) {
|
|
15
|
+
this.maps = maps ?? [];
|
|
16
|
+
this.origins = origins;
|
|
17
|
+
this.nodes = new Map();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
add(id, len, source) {
|
|
21
|
+
if (!this.nodes.has(id)) this.nodes.set(id, { len, sources: [] });
|
|
22
|
+
if (source) this.nodes.get(id).sources.push(source);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
has(id) { return this.nodes.has(id); }
|
|
26
|
+
|
|
27
|
+
slot(id, slot, scalarOrigins) {
|
|
28
|
+
const len = this.nodes.get(id)?.len ?? 0;
|
|
29
|
+
return Number.isInteger(slot) && slot >= 0 && slot < len
|
|
30
|
+
? this.range(id, slot, slot + 1, scalarOrigins)
|
|
31
|
+
: this.range(id, 0, len, scalarOrigins);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
range(id, start, end, scalarOrigins) {
|
|
35
|
+
const result = new Set();
|
|
36
|
+
const pending = [[id, start, end]];
|
|
37
|
+
const visited = new Set();
|
|
38
|
+
const emit = (origin, offset, start, end) => {
|
|
39
|
+
for (let index = start; index < end; index += 1) result.add(origin(offset + index));
|
|
40
|
+
};
|
|
41
|
+
while (pending.length) {
|
|
42
|
+
let [id, start, end] = pending.pop();
|
|
43
|
+
const key = `${id}:${start}:${end}`;
|
|
44
|
+
if (start >= end || visited.has(key)) continue;
|
|
45
|
+
visited.add(key);
|
|
46
|
+
const node = this.nodes.get(id);
|
|
47
|
+
if (!node) {
|
|
48
|
+
for (const origin of scalarOrigins(id)) result.add(origin);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
end = Math.min(end, node.len);
|
|
52
|
+
for (const source of node.sources) {
|
|
53
|
+
if (source.kind === "value" && source.data.kind === "local") {
|
|
54
|
+
pending.push([source.data.data, start, end]);
|
|
55
|
+
} else if (source.kind === "place") {
|
|
56
|
+
const base = source.data.base;
|
|
57
|
+
if (base.kind === "local") pending.push([base.data, start, end]);
|
|
58
|
+
else if (base.kind === "parameter") {
|
|
59
|
+
emit((slot) => this.origins.parameter(base.data, slot), 0, start, end);
|
|
60
|
+
}
|
|
61
|
+
} else if (source.kind === "buffer_span") {
|
|
62
|
+
const { kind, data } = source.data;
|
|
63
|
+
if (kind === "interface") emit(this.origins.buffer, data.first, start, end);
|
|
64
|
+
else if (kind === "parameter") {
|
|
65
|
+
emit((slot) => this.origins.parameter(data.span, slot), data.start, start, end);
|
|
66
|
+
} else if (kind === "mapped_interface") {
|
|
67
|
+
const entries = this.maps[data.map]?.buffers ?? [];
|
|
68
|
+
for (let index = start; index < end; index += 1) {
|
|
69
|
+
const buffer = entries[data.start + index];
|
|
70
|
+
if (buffer !== undefined) result.add(this.origins.buffer(buffer));
|
|
71
|
+
}
|
|
72
|
+
} else if (kind === "local") {
|
|
73
|
+
const sourceLen = this.nodes.get(data.collection)?.len ?? 0;
|
|
74
|
+
const [first, last] = selectedResourceRange(data.start, sourceLen - data.len + 1, data.bounds);
|
|
75
|
+
if (first < last) pending.push([data.collection, first + start, last - 1 + end]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
}
|
package/src/constants.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Synchronized from format-versions.json; do not edit this copy directly.
|
|
2
|
-
export const SUPPORTED_MIR_SCHEMA_VERSION =
|
|
2
|
+
export const SUPPORTED_MIR_SCHEMA_VERSION = 9;
|
package/src/index.d.ts
CHANGED