@onda-lang/binaryen-web 0.5.3 → 0.6.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 +2 -1
- package/package.json +2 -2
- package/src/artifact.d.ts +2 -0
- package/src/constants.js +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +265 -28
package/README.md
CHANGED
|
@@ -31,7 +31,8 @@ a JSON string, a MessagePack `ArrayBuffer`/typed-array view, or a decoded object
|
|
|
31
31
|
for inspection; the browser compiler and test corpus use MessagePack as the production transport.
|
|
32
32
|
|
|
33
33
|
The generated module exports `memory`, `__heap_base`, `onda_init(params_ptr, state_ptr)`, the
|
|
34
|
-
11-argument processor `onda_process`, and one `onda_event_N` function per declared event.
|
|
34
|
+
11-argument processor `onda_process`, and one `onda_event_N` function per declared event. Each
|
|
35
|
+
function returns zero on success or a positive generated execution-failure code. These are
|
|
35
36
|
the complete wasm32-module profile of the generic
|
|
36
37
|
[`Onda processor ABI`](../../docs/processor-abi.md), not a Web Audio-specific interface. The host
|
|
37
38
|
owns allocation in linear memory. Metadata contains resolved target/integration facts,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onda-lang/binaryen-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Binaryen backend for versioned Onda MIR in browsers and Node.js",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"verify-mir": "node ./scripts/verify-mir.mjs"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@onda-lang/processor-abi": "0.
|
|
47
|
+
"@onda-lang/processor-abi": "0.6.0",
|
|
48
48
|
"binaryen": "130.0.0"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
package/src/artifact.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ export {
|
|
|
3
3
|
PROCESSOR_ABI_VERSION,
|
|
4
4
|
PROCESSOR_ARTIFACT_FORMAT,
|
|
5
5
|
PROCESSOR_ARTIFACT_FORMAT_VERSION,
|
|
6
|
+
PROCESSOR_EXECUTION_OK,
|
|
7
|
+
PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE,
|
|
6
8
|
PROCESSOR_SNAPSHOT_FORMAT_VERSION,
|
|
7
9
|
createProcessorArtifactFiles,
|
|
8
10
|
loadProcessorArtifactFiles,
|
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 = 3;
|
package/src/index.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ export {
|
|
|
4
4
|
PROCESSOR_ABI_VERSION,
|
|
5
5
|
PROCESSOR_ARTIFACT_FORMAT,
|
|
6
6
|
PROCESSOR_ARTIFACT_FORMAT_VERSION,
|
|
7
|
+
PROCESSOR_EXECUTION_OK,
|
|
8
|
+
PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE,
|
|
7
9
|
PROCESSOR_SNAPSHOT_FORMAT_VERSION,
|
|
8
10
|
createProcessorArtifactFiles,
|
|
9
11
|
loadProcessorArtifactFiles,
|
package/src/index.js
CHANGED
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
PROCESSOR_ABI_VERSION,
|
|
9
9
|
PROCESSOR_ARTIFACT_FORMAT,
|
|
10
10
|
PROCESSOR_ARTIFACT_FORMAT_VERSION,
|
|
11
|
+
PROCESSOR_EXECUTION_OK,
|
|
12
|
+
PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE,
|
|
11
13
|
PROCESSOR_SNAPSHOT_FORMAT_VERSION,
|
|
12
14
|
validateProcessorMetadata,
|
|
13
15
|
} from "./artifact.js";
|
|
@@ -17,6 +19,8 @@ export {
|
|
|
17
19
|
PROCESSOR_ABI_VERSION,
|
|
18
20
|
PROCESSOR_ARTIFACT_FORMAT,
|
|
19
21
|
PROCESSOR_ARTIFACT_FORMAT_VERSION,
|
|
22
|
+
PROCESSOR_EXECUTION_OK,
|
|
23
|
+
PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE,
|
|
20
24
|
PROCESSOR_SNAPSHOT_FORMAT_VERSION,
|
|
21
25
|
createProcessorArtifactFiles,
|
|
22
26
|
loadProcessorArtifactFiles,
|
|
@@ -38,6 +42,7 @@ const MAX_MEMORY_PAGES = 65_536;
|
|
|
38
42
|
const WASM32_ADDRESS_SPACE_BYTES = MAX_MEMORY_PAGES * PAGE_BYTES;
|
|
39
43
|
const DEFAULT_OPTIMIZE_LEVEL = 4;
|
|
40
44
|
const ONDA_PROCESS_FULL_BLOCK = (1 << 0) | (1 << 1);
|
|
45
|
+
const RUNTIME_FAILURE_GLOBAL = "$onda.runtime_failure";
|
|
41
46
|
const MATH_KERNEL_INTRINSICS = new Set([
|
|
42
47
|
"sin",
|
|
43
48
|
"cos",
|
|
@@ -179,6 +184,7 @@ class MirCompiler {
|
|
|
179
184
|
? MATH_KERNEL_RESERVED_END
|
|
180
185
|
: STATIC_BASE;
|
|
181
186
|
this.internalHelpers = new Set();
|
|
187
|
+
this.functionMayFail = [];
|
|
182
188
|
this.nextLabel = 0;
|
|
183
189
|
}
|
|
184
190
|
|
|
@@ -297,6 +303,7 @@ class MirCompiler {
|
|
|
297
303
|
this.validateCurrentSchemaEnvelope();
|
|
298
304
|
this.validateProcessEntrySignature();
|
|
299
305
|
this.validateAcyclicCallGraph();
|
|
306
|
+
this.analyzeRecoverableFailures();
|
|
300
307
|
}
|
|
301
308
|
|
|
302
309
|
validateCurrentSchemaEnvelope() {
|
|
@@ -444,6 +451,86 @@ class MirCompiler {
|
|
|
444
451
|
}
|
|
445
452
|
}
|
|
446
453
|
|
|
454
|
+
analyzeRecoverableFailures() {
|
|
455
|
+
const callees = this.mir.functions.map(() => new Set());
|
|
456
|
+
const direct = this.mir.functions.map(() => false);
|
|
457
|
+
const binaryMayFail = (functionId, value) => {
|
|
458
|
+
if (
|
|
459
|
+
value.kind !== "binary"
|
|
460
|
+
|| !["divide", "remainder"].includes(value.data?.op)
|
|
461
|
+
) {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
const lhs = value.data.lhs;
|
|
465
|
+
const scalar = lhs.kind === "constant"
|
|
466
|
+
? lhs.data.type
|
|
467
|
+
: this.requireScalarType(
|
|
468
|
+
this.mir.functions[functionId].locals[lhs.data].ty,
|
|
469
|
+
`binary operand in '${this.mir.functions[functionId].name}'`,
|
|
470
|
+
);
|
|
471
|
+
return scalar === "i32" || scalar === "i64";
|
|
472
|
+
};
|
|
473
|
+
const checkedBoundsKinds = new Set([
|
|
474
|
+
"array_window",
|
|
475
|
+
"buffer_load",
|
|
476
|
+
"buffer_param_load",
|
|
477
|
+
"buffer_param_store",
|
|
478
|
+
"buffer_store",
|
|
479
|
+
"const_data_load",
|
|
480
|
+
"index",
|
|
481
|
+
"input_load",
|
|
482
|
+
"make_slice",
|
|
483
|
+
"output_load",
|
|
484
|
+
"output_store",
|
|
485
|
+
"control_output_store",
|
|
486
|
+
]);
|
|
487
|
+
const dynamicBoundsKinds = new Set([
|
|
488
|
+
"slice_element",
|
|
489
|
+
"slice_load",
|
|
490
|
+
"slice_store",
|
|
491
|
+
"slice_window",
|
|
492
|
+
]);
|
|
493
|
+
const scan = (functionId, value) => {
|
|
494
|
+
if (value === null || value === undefined) return;
|
|
495
|
+
if (Array.isArray(value)) {
|
|
496
|
+
for (const entry of value) scan(functionId, entry);
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
if (typeof value !== "object") return;
|
|
500
|
+
if (value.kind === "call" && Number.isInteger(value.data?.function)) {
|
|
501
|
+
callees[functionId].add(value.data.function);
|
|
502
|
+
}
|
|
503
|
+
const bounds = value.data?.bounds;
|
|
504
|
+
if (
|
|
505
|
+
value.kind === "process_frame"
|
|
506
|
+
|| value.kind === "slice_copy"
|
|
507
|
+
|| binaryMayFail(functionId, value)
|
|
508
|
+
|| (checkedBoundsKinds.has(value.kind) && bounds === "checked")
|
|
509
|
+
|| (dynamicBoundsKinds.has(value.kind) && bounds !== "unchecked")
|
|
510
|
+
) {
|
|
511
|
+
direct[functionId] = true;
|
|
512
|
+
}
|
|
513
|
+
for (const child of Object.values(value)) scan(functionId, child);
|
|
514
|
+
};
|
|
515
|
+
for (const [functionId, func] of this.mir.functions.entries()) {
|
|
516
|
+
scan(functionId, func.body);
|
|
517
|
+
}
|
|
518
|
+
this.functionMayFail = [...direct];
|
|
519
|
+
let changed = true;
|
|
520
|
+
while (changed) {
|
|
521
|
+
changed = false;
|
|
522
|
+
for (const [functionId, targets] of callees.entries()) {
|
|
523
|
+
if (
|
|
524
|
+
!this.functionMayFail[functionId]
|
|
525
|
+
&& [...targets].some((target) => this.functionMayFail[target])
|
|
526
|
+
) {
|
|
527
|
+
this.functionMayFail[functionId] = true;
|
|
528
|
+
changed = true;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
447
534
|
buildLayouts() {
|
|
448
535
|
this.stateLayout = this.layoutNamedValues(this.mir.state);
|
|
449
536
|
this.paramLayout = this.layoutNamedValues(this.mir.interface.params);
|
|
@@ -663,6 +750,12 @@ class MirCompiler {
|
|
|
663
750
|
for (const name of Object.values(POINTER_GLOBALS)) {
|
|
664
751
|
this.module.addGlobal(name, binaryen.i32, true, this.module.i32.const(0));
|
|
665
752
|
}
|
|
753
|
+
this.module.addGlobal(
|
|
754
|
+
RUNTIME_FAILURE_GLOBAL,
|
|
755
|
+
binaryen.i32,
|
|
756
|
+
true,
|
|
757
|
+
this.module.i32.const(0),
|
|
758
|
+
);
|
|
666
759
|
}
|
|
667
760
|
|
|
668
761
|
addMathKernel() {
|
|
@@ -1049,6 +1142,62 @@ class MirCompiler {
|
|
|
1049
1142
|
);
|
|
1050
1143
|
}
|
|
1051
1144
|
|
|
1145
|
+
defaultFunctionResult(context) {
|
|
1146
|
+
const scalars = context.function.results.map((typeId, resultId) =>
|
|
1147
|
+
this.requireScalarType(
|
|
1148
|
+
typeId,
|
|
1149
|
+
`result ${resultId} of '${context.function.name}'`,
|
|
1150
|
+
),
|
|
1151
|
+
);
|
|
1152
|
+
const values = scalars.map((scalar) => this.zero(scalar));
|
|
1153
|
+
if (values.length === 0) return undefined;
|
|
1154
|
+
if (values.length === 1) return values[0];
|
|
1155
|
+
return this.module.tuple.make(values);
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
returnFromCurrentFunction(context) {
|
|
1159
|
+
return this.module.return(this.defaultFunctionResult(context));
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
raiseRuntimeFailure(context) {
|
|
1163
|
+
return this.module.block(null, [
|
|
1164
|
+
this.module.global.set(
|
|
1165
|
+
RUNTIME_FAILURE_GLOBAL,
|
|
1166
|
+
this.module.i32.const(PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE),
|
|
1167
|
+
),
|
|
1168
|
+
this.returnFromCurrentFunction(context),
|
|
1169
|
+
]);
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
propagateRuntimeFailure(calleeId, context) {
|
|
1173
|
+
if (!this.functionMayFail[calleeId]) return [];
|
|
1174
|
+
return [
|
|
1175
|
+
this.module.if(
|
|
1176
|
+
this.module.i32.ne(
|
|
1177
|
+
this.module.global.get(RUNTIME_FAILURE_GLOBAL, binaryen.i32),
|
|
1178
|
+
this.module.i32.const(PROCESSOR_EXECUTION_OK),
|
|
1179
|
+
),
|
|
1180
|
+
this.returnFromCurrentFunction(context),
|
|
1181
|
+
),
|
|
1182
|
+
];
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
resetRuntimeFailure(functionId) {
|
|
1186
|
+
if (!this.functionMayFail[functionId]) return [];
|
|
1187
|
+
return [
|
|
1188
|
+
this.module.global.set(
|
|
1189
|
+
RUNTIME_FAILURE_GLOBAL,
|
|
1190
|
+
this.module.i32.const(PROCESSOR_EXECUTION_OK),
|
|
1191
|
+
),
|
|
1192
|
+
];
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
executionStatus(functionId) {
|
|
1196
|
+
return this.functionMayFail[functionId]
|
|
1197
|
+
? this.module.global.get(RUNTIME_FAILURE_GLOBAL, binaryen.i32)
|
|
1198
|
+
: this.module.i32.const(PROCESSOR_EXECUTION_OK);
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1052
1201
|
addAbiWrappers() {
|
|
1053
1202
|
const initId = this.mir.entry_points.init;
|
|
1054
1203
|
const processId = this.mir.entry_points.process;
|
|
@@ -1062,6 +1211,7 @@ class MirCompiler {
|
|
|
1062
1211
|
(this.options.simd ? binaryen.Features.SIMD128 : 0),
|
|
1063
1212
|
);
|
|
1064
1213
|
const initBody = this.module.block(null, [
|
|
1214
|
+
...this.resetRuntimeFailure(initId),
|
|
1065
1215
|
this.module.global.set(
|
|
1066
1216
|
POINTER_GLOBALS.params,
|
|
1067
1217
|
this.module.local.get(0, binaryen.i32),
|
|
@@ -1076,11 +1226,12 @@ class MirCompiler {
|
|
|
1076
1226
|
this.module.i32.const(this.stateLayout.byteLength ?? 0),
|
|
1077
1227
|
),
|
|
1078
1228
|
this.module.call(this.functionNames[initId], [], binaryen.none),
|
|
1079
|
-
|
|
1229
|
+
this.executionStatus(initId),
|
|
1230
|
+
], binaryen.i32);
|
|
1080
1231
|
this.module.addFunction(
|
|
1081
1232
|
"$onda.abi.init",
|
|
1082
1233
|
binaryen.createType([binaryen.i32, binaryen.i32]),
|
|
1083
|
-
binaryen.
|
|
1234
|
+
binaryen.i32,
|
|
1084
1235
|
[],
|
|
1085
1236
|
initBody,
|
|
1086
1237
|
);
|
|
@@ -1121,7 +1272,13 @@ class MirCompiler {
|
|
|
1121
1272
|
),
|
|
1122
1273
|
);
|
|
1123
1274
|
const processBody = this.module.block(null, [
|
|
1124
|
-
this.module.if(
|
|
1275
|
+
this.module.if(
|
|
1276
|
+
invalidRange,
|
|
1277
|
+
this.module.return(
|
|
1278
|
+
this.module.i32.const(PROCESSOR_EXECUTION_RUNTIME_SAFETY_FAILURE),
|
|
1279
|
+
),
|
|
1280
|
+
),
|
|
1281
|
+
...this.resetRuntimeFailure(processId),
|
|
1125
1282
|
this.module.global.set(
|
|
1126
1283
|
POINTER_GLOBALS.state,
|
|
1127
1284
|
this.module.local.get(0, binaryen.i32),
|
|
@@ -1159,11 +1316,12 @@ class MirCompiler {
|
|
|
1159
1316
|
[startFrame(), frames(), flags()],
|
|
1160
1317
|
binaryen.none,
|
|
1161
1318
|
),
|
|
1162
|
-
|
|
1319
|
+
this.executionStatus(processId),
|
|
1320
|
+
], binaryen.i32);
|
|
1163
1321
|
this.module.addFunction(
|
|
1164
1322
|
"$onda.abi.process",
|
|
1165
1323
|
processParams,
|
|
1166
|
-
binaryen.
|
|
1324
|
+
binaryen.i32,
|
|
1167
1325
|
[],
|
|
1168
1326
|
processBody,
|
|
1169
1327
|
);
|
|
@@ -1182,6 +1340,7 @@ class MirCompiler {
|
|
|
1182
1340
|
}
|
|
1183
1341
|
const wrapperName = `$onda.abi.event.${eventId}`;
|
|
1184
1342
|
const body = this.module.block(null, [
|
|
1343
|
+
...this.resetRuntimeFailure(event.handler),
|
|
1185
1344
|
this.module.global.set(
|
|
1186
1345
|
POINTER_GLOBALS.eventPayload,
|
|
1187
1346
|
this.module.local.get(0, binaryen.i32),
|
|
@@ -1211,11 +1370,12 @@ class MirCompiler {
|
|
|
1211
1370
|
this.module.local.get(6, binaryen.i32),
|
|
1212
1371
|
),
|
|
1213
1372
|
this.module.call(this.functionNames[event.handler], [], binaryen.none),
|
|
1214
|
-
|
|
1373
|
+
this.executionStatus(event.handler),
|
|
1374
|
+
], binaryen.i32);
|
|
1215
1375
|
this.module.addFunction(
|
|
1216
1376
|
wrapperName,
|
|
1217
1377
|
binaryen.createType(Array.from({ length: 7 }, () => binaryen.i32)),
|
|
1218
|
-
binaryen.
|
|
1378
|
+
binaryen.i32,
|
|
1219
1379
|
[],
|
|
1220
1380
|
body,
|
|
1221
1381
|
);
|
|
@@ -1465,6 +1625,7 @@ class MirCompiler {
|
|
|
1465
1625
|
this.module.local.set(resultSpill.index, call),
|
|
1466
1626
|
...afterCall,
|
|
1467
1627
|
...assignResults,
|
|
1628
|
+
...this.propagateRuntimeFailure(data.function, context),
|
|
1468
1629
|
]);
|
|
1469
1630
|
}
|
|
1470
1631
|
let compiledCall;
|
|
@@ -1492,8 +1653,18 @@ class MirCompiler {
|
|
|
1492
1653
|
),
|
|
1493
1654
|
]);
|
|
1494
1655
|
}
|
|
1495
|
-
if (localReferenceSync.length === 0)
|
|
1496
|
-
|
|
1656
|
+
if (localReferenceSync.length === 0) {
|
|
1657
|
+
const propagation = this.propagateRuntimeFailure(data.function, context);
|
|
1658
|
+
return propagation.length === 0
|
|
1659
|
+
? compiledCall
|
|
1660
|
+
: this.module.block(null, [compiledCall, ...propagation]);
|
|
1661
|
+
}
|
|
1662
|
+
return this.module.block(null, [
|
|
1663
|
+
...beforeCall,
|
|
1664
|
+
compiledCall,
|
|
1665
|
+
...afterCall,
|
|
1666
|
+
...this.propagateRuntimeFailure(data.function, context),
|
|
1667
|
+
]);
|
|
1497
1668
|
}
|
|
1498
1669
|
|
|
1499
1670
|
compileOutputStore(data, context) {
|
|
@@ -1608,6 +1779,7 @@ class MirCompiler {
|
|
|
1608
1779
|
scalar,
|
|
1609
1780
|
() => this.compileValue(data.lhs, context),
|
|
1610
1781
|
() => this.compileValue(data.rhs, context),
|
|
1782
|
+
context,
|
|
1611
1783
|
);
|
|
1612
1784
|
}
|
|
1613
1785
|
case "compare": {
|
|
@@ -1718,7 +1890,7 @@ class MirCompiler {
|
|
|
1718
1890
|
const invalid = this.module.i32.ge_u(offset(), frames());
|
|
1719
1891
|
return this.module.if(
|
|
1720
1892
|
invalid,
|
|
1721
|
-
this.
|
|
1893
|
+
this.raiseRuntimeFailure(context),
|
|
1722
1894
|
this.module.i32.add(startFrame(), offset()),
|
|
1723
1895
|
);
|
|
1724
1896
|
}
|
|
@@ -1816,6 +1988,7 @@ class MirCompiler {
|
|
|
1816
1988
|
rawIndex,
|
|
1817
1989
|
() => this.compileBufferParamTotalScalarLen(data.parameter, context),
|
|
1818
1990
|
data.bounds,
|
|
1991
|
+
context,
|
|
1819
1992
|
true,
|
|
1820
1993
|
);
|
|
1821
1994
|
return this.module.i32.add(
|
|
@@ -1849,6 +2022,7 @@ class MirCompiler {
|
|
|
1849
2022
|
rawIndex,
|
|
1850
2023
|
() => this.compileBufferTotalScalarLen(data.buffer),
|
|
1851
2024
|
data.bounds,
|
|
2025
|
+
context,
|
|
1852
2026
|
true,
|
|
1853
2027
|
);
|
|
1854
2028
|
const pointer = this.loadBufferTableValue(
|
|
@@ -2061,6 +2235,7 @@ class MirCompiler {
|
|
|
2061
2235
|
() => this.compileValue(data.len, context),
|
|
2062
2236
|
() => source()[1],
|
|
2063
2237
|
data.bounds,
|
|
2238
|
+
context,
|
|
2064
2239
|
);
|
|
2065
2240
|
return [
|
|
2066
2241
|
this.module.i32.add(
|
|
@@ -2075,7 +2250,7 @@ class MirCompiler {
|
|
|
2075
2250
|
];
|
|
2076
2251
|
}
|
|
2077
2252
|
|
|
2078
|
-
compileSliceRange(start, len, sourceLen, bounds) {
|
|
2253
|
+
compileSliceRange(start, len, sourceLen, bounds, context) {
|
|
2079
2254
|
const zero = () => this.module.i32.const(0);
|
|
2080
2255
|
if (bounds === "unchecked") {
|
|
2081
2256
|
return { start, len };
|
|
@@ -2111,7 +2286,7 @@ class MirCompiler {
|
|
|
2111
2286
|
};
|
|
2112
2287
|
return { start: normalizedStart, len: normalizedLen };
|
|
2113
2288
|
}
|
|
2114
|
-
if (bounds === "
|
|
2289
|
+
if (bounds === "checked") {
|
|
2115
2290
|
const invalid = () => {
|
|
2116
2291
|
const remaining = () => this.module.i32.sub(sourceLen(), start());
|
|
2117
2292
|
return this.module.i32.or(
|
|
@@ -2127,7 +2302,7 @@ class MirCompiler {
|
|
|
2127
2302
|
};
|
|
2128
2303
|
return {
|
|
2129
2304
|
start: () =>
|
|
2130
|
-
this.module.if(invalid(), this.
|
|
2305
|
+
this.module.if(invalid(), this.raiseRuntimeFailure(context), start()),
|
|
2131
2306
|
len,
|
|
2132
2307
|
};
|
|
2133
2308
|
}
|
|
@@ -2188,6 +2363,7 @@ class MirCompiler {
|
|
|
2188
2363
|
() => this.compileValue(source.data.channel, context),
|
|
2189
2364
|
channels,
|
|
2190
2365
|
"clamp",
|
|
2366
|
+
context,
|
|
2191
2367
|
true,
|
|
2192
2368
|
);
|
|
2193
2369
|
return [
|
|
@@ -2221,6 +2397,7 @@ class MirCompiler {
|
|
|
2221
2397
|
() => this.compileValue(source.data.channel, context),
|
|
2222
2398
|
channels,
|
|
2223
2399
|
"clamp",
|
|
2400
|
+
context,
|
|
2224
2401
|
true,
|
|
2225
2402
|
);
|
|
2226
2403
|
return [
|
|
@@ -2260,14 +2437,16 @@ class MirCompiler {
|
|
|
2260
2437
|
() => this.compileSliceValue(slice, context),
|
|
2261
2438
|
() => this.compileValue(index, context),
|
|
2262
2439
|
bounds,
|
|
2440
|
+
context,
|
|
2263
2441
|
);
|
|
2264
2442
|
}
|
|
2265
2443
|
|
|
2266
|
-
compileSliceAddressWithFactories(slice, index, bounds) {
|
|
2444
|
+
compileSliceAddressWithFactories(slice, index, bounds, context) {
|
|
2267
2445
|
const bounded = this.compileDynamicBoundedIndex(
|
|
2268
2446
|
index,
|
|
2269
2447
|
() => slice()[1],
|
|
2270
2448
|
bounds,
|
|
2449
|
+
context,
|
|
2271
2450
|
);
|
|
2272
2451
|
return this.module.i32.add(
|
|
2273
2452
|
slice()[0],
|
|
@@ -2295,6 +2474,7 @@ class MirCompiler {
|
|
|
2295
2474
|
sourceType.data.len - parameterType.data.len,
|
|
2296
2475
|
),
|
|
2297
2476
|
data.bounds,
|
|
2477
|
+
context,
|
|
2298
2478
|
);
|
|
2299
2479
|
return this.module.i32.add(
|
|
2300
2480
|
this.placeAddress(data.array, context),
|
|
@@ -2318,6 +2498,7 @@ class MirCompiler {
|
|
|
2318
2498
|
this.module.i32.const(requiredLen),
|
|
2319
2499
|
),
|
|
2320
2500
|
data.bounds,
|
|
2501
|
+
context,
|
|
2321
2502
|
);
|
|
2322
2503
|
const address = () =>
|
|
2323
2504
|
this.module.i32.add(
|
|
@@ -2340,12 +2521,12 @@ class MirCompiler {
|
|
|
2340
2521
|
);
|
|
2341
2522
|
return this.module.if(
|
|
2342
2523
|
invalidShape(),
|
|
2343
|
-
this.
|
|
2524
|
+
this.raiseRuntimeFailure(context),
|
|
2344
2525
|
address(),
|
|
2345
2526
|
);
|
|
2346
2527
|
}
|
|
2347
2528
|
|
|
2348
|
-
compileWindowStart(start, maximum, bounds) {
|
|
2529
|
+
compileWindowStart(start, maximum, bounds, context) {
|
|
2349
2530
|
const zero = () => this.module.i32.const(0);
|
|
2350
2531
|
if (bounds === "unchecked") {
|
|
2351
2532
|
return start;
|
|
@@ -2365,14 +2546,14 @@ class MirCompiler {
|
|
|
2365
2546
|
);
|
|
2366
2547
|
};
|
|
2367
2548
|
}
|
|
2368
|
-
if (bounds === "
|
|
2549
|
+
if (bounds === "checked") {
|
|
2369
2550
|
return () =>
|
|
2370
2551
|
this.module.if(
|
|
2371
2552
|
this.module.i32.or(
|
|
2372
2553
|
this.module.i32.lt_s(start(), zero()),
|
|
2373
2554
|
this.module.i32.gt_s(start(), maximum()),
|
|
2374
2555
|
),
|
|
2375
|
-
this.
|
|
2556
|
+
this.raiseRuntimeFailure(context),
|
|
2376
2557
|
start(),
|
|
2377
2558
|
);
|
|
2378
2559
|
}
|
|
@@ -2629,7 +2810,7 @@ class MirCompiler {
|
|
|
2629
2810
|
source()[1],
|
|
2630
2811
|
),
|
|
2631
2812
|
),
|
|
2632
|
-
this.module.if(invalidOverlap(), this.
|
|
2813
|
+
this.module.if(invalidOverlap(), this.raiseRuntimeFailure(context)),
|
|
2633
2814
|
this.module.local.set(counter, this.module.i32.const(0)),
|
|
2634
2815
|
copy,
|
|
2635
2816
|
]);
|
|
@@ -2892,7 +3073,7 @@ class MirCompiler {
|
|
|
2892
3073
|
),
|
|
2893
3074
|
);
|
|
2894
3075
|
}
|
|
2895
|
-
if (bounds === "
|
|
3076
|
+
if (bounds === "checked") {
|
|
2896
3077
|
const outOfBounds = this.module.i32.or(
|
|
2897
3078
|
this.module.i32.lt_s(
|
|
2898
3079
|
this.compileValue(value, context),
|
|
@@ -2905,14 +3086,20 @@ class MirCompiler {
|
|
|
2905
3086
|
);
|
|
2906
3087
|
return this.module.if(
|
|
2907
3088
|
outOfBounds,
|
|
2908
|
-
this.
|
|
3089
|
+
this.raiseRuntimeFailure(context),
|
|
2909
3090
|
this.compileValue(value, context),
|
|
2910
3091
|
);
|
|
2911
3092
|
}
|
|
2912
3093
|
this.fail(`unknown bounds mode '${String(bounds)}'`);
|
|
2913
3094
|
}
|
|
2914
3095
|
|
|
2915
|
-
compileDynamicBoundedIndex(
|
|
3096
|
+
compileDynamicBoundedIndex(
|
|
3097
|
+
index,
|
|
3098
|
+
length,
|
|
3099
|
+
bounds,
|
|
3100
|
+
context,
|
|
3101
|
+
clampLengthKnownPositive = false,
|
|
3102
|
+
) {
|
|
2916
3103
|
if (bounds === "unchecked") {
|
|
2917
3104
|
return index();
|
|
2918
3105
|
}
|
|
@@ -2932,17 +3119,17 @@ class MirCompiler {
|
|
|
2932
3119
|
if (clampLengthKnownPositive) return clamped();
|
|
2933
3120
|
return this.module.if(
|
|
2934
3121
|
this.module.i32.le_s(length(), this.module.i32.const(0)),
|
|
2935
|
-
this.
|
|
3122
|
+
this.raiseRuntimeFailure(context),
|
|
2936
3123
|
clamped(),
|
|
2937
3124
|
);
|
|
2938
3125
|
}
|
|
2939
|
-
if (bounds === "
|
|
3126
|
+
if (bounds === "checked") {
|
|
2940
3127
|
return this.module.if(
|
|
2941
3128
|
this.module.i32.or(
|
|
2942
3129
|
this.module.i32.lt_s(index(), this.module.i32.const(0)),
|
|
2943
3130
|
this.module.i32.ge_s(index(), length()),
|
|
2944
3131
|
),
|
|
2945
|
-
this.
|
|
3132
|
+
this.raiseRuntimeFailure(context),
|
|
2946
3133
|
index(),
|
|
2947
3134
|
);
|
|
2948
3135
|
}
|
|
@@ -2966,7 +3153,7 @@ class MirCompiler {
|
|
|
2966
3153
|
}
|
|
2967
3154
|
}
|
|
2968
3155
|
|
|
2969
|
-
compileBinary(op, scalar, lhs, rhs) {
|
|
3156
|
+
compileBinary(op, scalar, lhs, rhs, context) {
|
|
2970
3157
|
if (!supportsMirOperation("binary", op, scalar)) {
|
|
2971
3158
|
this.fail(`binary operation '${String(op)}' does not support scalar '${scalar}'`);
|
|
2972
3159
|
}
|
|
@@ -2990,13 +3177,26 @@ class MirCompiler {
|
|
|
2990
3177
|
wasm.eq(lhs(), minimum()),
|
|
2991
3178
|
wasm.eq(rhs(), negativeOne()),
|
|
2992
3179
|
);
|
|
2993
|
-
|
|
3180
|
+
const division = this.module.if(
|
|
3181
|
+
overflow,
|
|
3182
|
+
minimum(),
|
|
3183
|
+
wasm.div_s(lhs(), rhs()),
|
|
3184
|
+
);
|
|
3185
|
+
return this.module.if(
|
|
3186
|
+
wasm.eq(rhs(), this.zero(scalar)),
|
|
3187
|
+
this.raiseRuntimeFailure(context),
|
|
3188
|
+
division,
|
|
3189
|
+
);
|
|
2994
3190
|
}
|
|
2995
3191
|
case "remainder":
|
|
2996
3192
|
if (!integer) {
|
|
2997
3193
|
return this.compileMathKernelCall("remainder", scalar, [lhs(), rhs()]);
|
|
2998
3194
|
}
|
|
2999
|
-
return
|
|
3195
|
+
return this.module.if(
|
|
3196
|
+
wasm.eq(rhs(), this.zero(scalar)),
|
|
3197
|
+
this.raiseRuntimeFailure(context),
|
|
3198
|
+
wasm.rem_s(lhs(), rhs()),
|
|
3199
|
+
);
|
|
3000
3200
|
case "bit_and": return wasm.and(lhs(), rhs());
|
|
3001
3201
|
case "bit_or": return wasm.or(lhs(), rhs());
|
|
3002
3202
|
case "bit_xor": return wasm.xor(lhs(), rhs());
|
|
@@ -3083,6 +3283,16 @@ class MirCompiler {
|
|
|
3083
3283
|
return this.module.select(wasm.lt_s(arg(0), arg(1)), arg(0), arg(1));
|
|
3084
3284
|
case "max":
|
|
3085
3285
|
return this.module.select(wasm.gt_s(arg(0), arg(1)), arg(0), arg(1));
|
|
3286
|
+
case "range_clamp":
|
|
3287
|
+
return this.module.select(
|
|
3288
|
+
wasm.lt_s(arg(0), arg(1)),
|
|
3289
|
+
arg(1),
|
|
3290
|
+
this.module.select(
|
|
3291
|
+
wasm.gt_s(arg(0), arg(2)),
|
|
3292
|
+
arg(2),
|
|
3293
|
+
arg(0),
|
|
3294
|
+
),
|
|
3295
|
+
);
|
|
3086
3296
|
default:
|
|
3087
3297
|
this.fail(`intrinsic '${data.intrinsic}' requires f32 or f64 operands`);
|
|
3088
3298
|
}
|
|
@@ -3096,6 +3306,22 @@ class MirCompiler {
|
|
|
3096
3306
|
case "trunc": return wasm.trunc(args[0]);
|
|
3097
3307
|
case "min": return wasm.min(args[0], args[1]);
|
|
3098
3308
|
case "max": return wasm.max(args[0], args[1]);
|
|
3309
|
+
case "range_clamp": {
|
|
3310
|
+
const arg = (index) => this.compileValue(data.args[index], context);
|
|
3311
|
+
return this.module.select(
|
|
3312
|
+
wasm.ne(arg(0), arg(0)),
|
|
3313
|
+
arg(1),
|
|
3314
|
+
this.module.select(
|
|
3315
|
+
wasm.lt(arg(0), arg(1)),
|
|
3316
|
+
arg(1),
|
|
3317
|
+
this.module.select(
|
|
3318
|
+
wasm.gt(arg(0), arg(2)),
|
|
3319
|
+
arg(2),
|
|
3320
|
+
arg(0),
|
|
3321
|
+
),
|
|
3322
|
+
),
|
|
3323
|
+
);
|
|
3324
|
+
}
|
|
3099
3325
|
case "fma": return this.compileMathKernelCall(data.intrinsic, scalar, args);
|
|
3100
3326
|
case "sin":
|
|
3101
3327
|
case "cos":
|
|
@@ -3418,6 +3644,7 @@ class MirCompiler {
|
|
|
3418
3644
|
default_reprs: null,
|
|
3419
3645
|
range_min_repr: null,
|
|
3420
3646
|
range_max_repr: null,
|
|
3647
|
+
param_control: null,
|
|
3421
3648
|
})),
|
|
3422
3649
|
params: this.mir.interface.params.map((param, id) => ({
|
|
3423
3650
|
name: param.name,
|
|
@@ -3432,6 +3659,15 @@ class MirCompiler {
|
|
|
3432
3659
|
default_reprs: this.constantReprs(param.default),
|
|
3433
3660
|
range_min_repr: this.scalarRepr(param.range?.min),
|
|
3434
3661
|
range_max_repr: this.scalarRepr(param.range?.max),
|
|
3662
|
+
param_control: this.storageShape(param.ty).length === 1 && param.range
|
|
3663
|
+
? {
|
|
3664
|
+
scale: param.control.scale,
|
|
3665
|
+
curve: param.control.curve,
|
|
3666
|
+
unit: param.control.unit,
|
|
3667
|
+
step_repr: this.scalarRepr(param.control.step),
|
|
3668
|
+
step_count: param.control.step_count,
|
|
3669
|
+
}
|
|
3670
|
+
: null,
|
|
3435
3671
|
})),
|
|
3436
3672
|
buffers: this.mir.interface.buffers.map((buffer) => {
|
|
3437
3673
|
const channels = this.bufferChannelMetadata(buffer.channels);
|
|
@@ -3509,6 +3745,7 @@ class MirCompiler {
|
|
|
3509
3745
|
default_reprs: null,
|
|
3510
3746
|
range_min_repr: null,
|
|
3511
3747
|
range_max_repr: null,
|
|
3748
|
+
param_control: null,
|
|
3512
3749
|
}));
|
|
3513
3750
|
}
|
|
3514
3751
|
|