@mica-js/simulator 2.8.0 → 2.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/README.md +2 -2
- package/dist/device.d.ts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +2303 -1982
- package/dist/index.js.map +1 -1
- package/dist/sandbox.js +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -2132,2087 +2132,2408 @@ function standalone(code, cause, phase) {
|
|
|
2132
2132
|
}));
|
|
2133
2133
|
}
|
|
2134
2134
|
//#endregion
|
|
2135
|
-
//#region src/
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2135
|
+
//#region src/polling.ts
|
|
2136
|
+
function isPollingInstruction(body, pc, pcName) {
|
|
2137
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
2138
|
+
const redirects = [];
|
|
2139
|
+
const visit = (region) => {
|
|
2140
|
+
for (const operation of region.operations) {
|
|
2141
|
+
if ("result" in operation) definitions.set(operation.result, operation);
|
|
2142
|
+
switch (operation.kind) {
|
|
2143
|
+
case "constant":
|
|
2144
|
+
case "unary":
|
|
2145
|
+
case "binary":
|
|
2146
|
+
case "numeric":
|
|
2147
|
+
case "field":
|
|
2148
|
+
case "length":
|
|
2149
|
+
case "index":
|
|
2150
|
+
case "isNone":
|
|
2151
|
+
case "nonNull":
|
|
2152
|
+
case "optional":
|
|
2153
|
+
case "local":
|
|
2154
|
+
case "localRead":
|
|
2155
|
+
case "localWrite":
|
|
2156
|
+
case "memoryLength":
|
|
2157
|
+
case "load8":
|
|
2158
|
+
case "load16le":
|
|
2159
|
+
case "load16be":
|
|
2160
|
+
case "load32le":
|
|
2161
|
+
case "load32be":
|
|
2162
|
+
case "readSample":
|
|
2163
|
+
case "yield": break;
|
|
2164
|
+
case "read":
|
|
2165
|
+
if (operation.resource === "derived" || operation.name === pcName) return false;
|
|
2166
|
+
break;
|
|
2167
|
+
case "if":
|
|
2168
|
+
if (!visit(operation.yes) || operation.no && !visit(operation.no)) return false;
|
|
2169
|
+
break;
|
|
2170
|
+
case "select":
|
|
2171
|
+
if (!visit(operation.yes.body) || !visit(operation.no.body)) return false;
|
|
2172
|
+
break;
|
|
2173
|
+
case "and":
|
|
2174
|
+
case "or":
|
|
2175
|
+
if (!visit(operation.right.body)) return false;
|
|
2176
|
+
break;
|
|
2177
|
+
case "match":
|
|
2178
|
+
if (operation.arms.some((arm) => !visit(arm.body)) || operation.otherwise && !visit(operation.otherwise)) return false;
|
|
2179
|
+
break;
|
|
2180
|
+
case "redirect":
|
|
2181
|
+
redirects.push(operation.target);
|
|
2182
|
+
break;
|
|
2183
|
+
default: return false;
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
return true;
|
|
2187
|
+
};
|
|
2188
|
+
if (!visit(body)) return false;
|
|
2189
|
+
return redirects.some((target) => {
|
|
2190
|
+
const definition = definitions.get(target);
|
|
2191
|
+
return definition?.kind !== "constant" || definition.constant.kind === "number" && definition.constant.value === pc;
|
|
2147
2192
|
});
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2193
|
+
}
|
|
2194
|
+
//#endregion
|
|
2195
|
+
//#region src/runtime/jit.ts
|
|
2196
|
+
/** Large programs dispatch through shards; small ones stay on the faster monolithic form. */
|
|
2197
|
+
var DIRECT_SHARD_SIZE = 32;
|
|
2198
|
+
function syncJitCall(jit, name) {
|
|
2199
|
+
const step = jit.host.call(name, []).next();
|
|
2200
|
+
if (!step.done) throw new TypeError("Unexpected suspension in synchronous function");
|
|
2201
|
+
return step.value;
|
|
2202
|
+
}
|
|
2203
|
+
var checkpointStatsByJit = /* @__PURE__ */ new WeakMap();
|
|
2204
|
+
/** Create the opt-in checkpoint counters once, on the first instrumented Direct frame. */
|
|
2205
|
+
function directCheckpointStats(jit) {
|
|
2206
|
+
let stats = checkpointStatsByJit.get(jit);
|
|
2207
|
+
if (stats === void 0) {
|
|
2208
|
+
stats = {
|
|
2209
|
+
publications: 0,
|
|
2210
|
+
finishes: 0,
|
|
2211
|
+
emptySuffixes: 0,
|
|
2212
|
+
boundaryCalls: 0,
|
|
2213
|
+
continues: 0,
|
|
2214
|
+
exits: 0
|
|
2215
|
+
};
|
|
2216
|
+
checkpointStatsByJit.set(jit, stats);
|
|
2152
2217
|
}
|
|
2153
|
-
|
|
2154
|
-
return model;
|
|
2218
|
+
return stats;
|
|
2155
2219
|
}
|
|
2156
|
-
/**
|
|
2157
|
-
function
|
|
2158
|
-
|
|
2159
|
-
return true;
|
|
2220
|
+
/** Read counters without creating them; undefined means diagnostics stayed off. */
|
|
2221
|
+
function readDirectCheckpointStats(jit) {
|
|
2222
|
+
return checkpointStatsByJit.get(jit);
|
|
2160
2223
|
}
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2224
|
+
var barriers = /* @__PURE__ */ new WeakMap();
|
|
2225
|
+
function directBarriers(jit) {
|
|
2226
|
+
let entry = barriers.get(jit);
|
|
2227
|
+
if (entry === void 0) {
|
|
2228
|
+
entry = {
|
|
2229
|
+
stateMask: /* @__PURE__ */ new Int32Array(0),
|
|
2230
|
+
memoryMask: /* @__PURE__ */ new Int32Array(0)
|
|
2231
|
+
};
|
|
2232
|
+
barriers.set(jit, entry);
|
|
2233
|
+
}
|
|
2234
|
+
return entry;
|
|
2235
|
+
}
|
|
2236
|
+
function directStats(jit) {
|
|
2237
|
+
const counted = jit;
|
|
2238
|
+
return counted.directStats ??= {
|
|
2239
|
+
directCompiles: 0,
|
|
2240
|
+
directShards: 0,
|
|
2241
|
+
directCalls: 0,
|
|
2242
|
+
directFallbacks: 0,
|
|
2243
|
+
directDeadlines: 0,
|
|
2244
|
+
directStopped: 0,
|
|
2245
|
+
directSessions: 0,
|
|
2246
|
+
directShardCompiles: 0,
|
|
2247
|
+
directRouteInstalls: 0,
|
|
2248
|
+
directRouteRemovals: 0,
|
|
2249
|
+
directRoutes: 0,
|
|
2250
|
+
directDispatchMisses: 0,
|
|
2251
|
+
directGenericInstructions: 0,
|
|
2252
|
+
directBodyBytes: 0,
|
|
2253
|
+
directShardBytes: 0,
|
|
2254
|
+
directDispatcherBytes: 0,
|
|
2255
|
+
directHelperBytes: 0,
|
|
2256
|
+
directShareCallbacks: 0,
|
|
2257
|
+
directShareCandidates: 0,
|
|
2258
|
+
directShareGroups: 0,
|
|
2259
|
+
directSharedRoutes: 0,
|
|
2260
|
+
directSharedBodies: 0,
|
|
2261
|
+
directSharedBytes: 0,
|
|
2262
|
+
directShareReusedBodies: 0,
|
|
2263
|
+
directShareReusedRoutes: 0,
|
|
2264
|
+
directShareRejected: {
|
|
2265
|
+
missing: 0,
|
|
2266
|
+
invalid: 0,
|
|
2267
|
+
single: 0,
|
|
2268
|
+
ir: 0,
|
|
2269
|
+
delay: 0,
|
|
2270
|
+
isolated: 0,
|
|
2271
|
+
context: 0
|
|
2174
2272
|
},
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2273
|
+
directShareMarkRejected: {
|
|
2274
|
+
unknownPc: 0,
|
|
2275
|
+
duplicate: 0,
|
|
2276
|
+
invalidPc: 0,
|
|
2277
|
+
invalidLength: 0,
|
|
2278
|
+
outsideRegion: 0,
|
|
2279
|
+
crossView: 0,
|
|
2280
|
+
service: 0,
|
|
2281
|
+
notAdmitted: 0
|
|
2282
|
+
}
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2285
|
+
/** Direct execution counters of one JIT; absent until it first attempted an instruction. */
|
|
2286
|
+
function directJitStats(jit) {
|
|
2287
|
+
return jit.directStats;
|
|
2180
2288
|
}
|
|
2289
|
+
var directSessionSnapshots = /* @__PURE__ */ new WeakMap();
|
|
2181
2290
|
/**
|
|
2182
|
-
*
|
|
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.
|
|
2291
|
+
* Fold monotonic session counters into the per-JIT totals and publish the current route and
|
|
2292
|
+
* shard counts. Called after every commit so the benchmark never inspects session internals.
|
|
2189
2293
|
*/
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2294
|
+
function syncDirectSessionStats(jit, session) {
|
|
2295
|
+
const next = session.stats();
|
|
2296
|
+
const stats = directStats(jit);
|
|
2297
|
+
const previous = directSessionSnapshots.get(jit);
|
|
2298
|
+
const base = previous?.session === session ? previous.stats : void 0;
|
|
2299
|
+
stats.directShardCompiles += next.compiledShards - (base?.compiledShards ?? 0);
|
|
2300
|
+
stats.directRouteInstalls += next.routeInstalls - (base?.routeInstalls ?? 0);
|
|
2301
|
+
stats.directRouteRemovals += next.routeRemovals - (base?.routeRemovals ?? 0);
|
|
2302
|
+
stats.directBodyBytes += next.bodyBytes - (base?.bodyBytes ?? 0);
|
|
2303
|
+
stats.directShardBytes += next.shardBytes - (base?.shardBytes ?? 0);
|
|
2304
|
+
stats.directDispatcherBytes += next.dispatcherBytes - (base?.dispatcherBytes ?? 0);
|
|
2305
|
+
stats.directHelperBytes += next.helperBytes - (base?.helperBytes ?? 0);
|
|
2306
|
+
stats.directShareGroups = next.shareGroups;
|
|
2307
|
+
stats.directSharedRoutes = next.sharedRoutes;
|
|
2308
|
+
stats.directSharedBodies += next.sharedBodies - (base?.sharedBodies ?? 0);
|
|
2309
|
+
stats.directSharedBytes += next.sharedBytes - (base?.sharedBytes ?? 0);
|
|
2310
|
+
stats.directShareReusedBodies += next.shareReusedBodies - (base?.shareReusedBodies ?? 0);
|
|
2311
|
+
stats.directShareReusedRoutes += next.shareReusedRoutes - (base?.shareReusedRoutes ?? 0);
|
|
2312
|
+
stats.directShareRejected.missing += next.shareRejected.missing - (base?.shareRejected.missing ?? 0);
|
|
2313
|
+
stats.directShareRejected.invalid += next.shareRejected.invalid - (base?.shareRejected.invalid ?? 0);
|
|
2314
|
+
stats.directShareRejected.single += next.shareRejected.single - (base?.shareRejected.single ?? 0);
|
|
2315
|
+
stats.directShareRejected.ir += next.shareRejected.ir - (base?.shareRejected.ir ?? 0);
|
|
2316
|
+
stats.directShareRejected.delay += next.shareRejected.delay - (base?.shareRejected.delay ?? 0);
|
|
2317
|
+
stats.directShareRejected.isolated += next.shareRejected.isolated - (base?.shareRejected.isolated ?? 0);
|
|
2318
|
+
stats.directShareRejected.context += next.shareRejected.context - (base?.shareRejected.context ?? 0);
|
|
2319
|
+
stats.directShards = next.shards;
|
|
2320
|
+
stats.directRoutes = next.routes;
|
|
2321
|
+
directSessionSnapshots.set(jit, {
|
|
2322
|
+
session,
|
|
2323
|
+
stats: next
|
|
2324
|
+
});
|
|
2325
|
+
}
|
|
2326
|
+
/** Drop the active session and publish zero current counts; cumulative totals stay. */
|
|
2327
|
+
function releaseDirectSession(jit) {
|
|
2328
|
+
const direct = jit.direct;
|
|
2329
|
+
if (direct !== void 0) {
|
|
2330
|
+
syncDirectSessionStats(jit, direct.session);
|
|
2331
|
+
jit.direct = void 0;
|
|
2332
|
+
direct.session.dispose();
|
|
2333
|
+
directSessionSnapshots.delete(jit);
|
|
2334
|
+
jit.directMemories.clear();
|
|
2227
2335
|
}
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2336
|
+
const stats = directJitStats(jit);
|
|
2337
|
+
if (stats !== void 0) {
|
|
2338
|
+
stats.directShards = 0;
|
|
2339
|
+
stats.directRoutes = 0;
|
|
2340
|
+
stats.directShareGroups = 0;
|
|
2341
|
+
stats.directSharedRoutes = 0;
|
|
2231
2342
|
}
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2343
|
+
}
|
|
2344
|
+
/** Mark the active session stale; an idle session is released immediately. */
|
|
2345
|
+
function staleDirectSession(jit, running) {
|
|
2346
|
+
const direct = jit.direct;
|
|
2347
|
+
if (direct === void 0) {
|
|
2348
|
+
jit.directStale = false;
|
|
2349
|
+
return false;
|
|
2235
2350
|
}
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2351
|
+
jit.directStale = true;
|
|
2352
|
+
if (running || direct.frame.resume !== 0) {
|
|
2353
|
+
direct.frame.stopAfterInstruction = true;
|
|
2354
|
+
return true;
|
|
2239
2355
|
}
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2356
|
+
releaseDirectSession(jit);
|
|
2357
|
+
jit.directStale = false;
|
|
2358
|
+
return true;
|
|
2359
|
+
}
|
|
2360
|
+
/** Invalidate direct execution because its translation inputs changed. */
|
|
2361
|
+
function invalidateDirect(jit, cacheInvalid = false) {
|
|
2362
|
+
if (cacheInvalid) jit.directCacheInvalid = true;
|
|
2363
|
+
jit.directSkip = void 0;
|
|
2364
|
+
staleDirectSession(jit, false);
|
|
2365
|
+
}
|
|
2366
|
+
/** Release the session on explicit registration/board disposal. */
|
|
2367
|
+
function disposeDirect(jit) {
|
|
2368
|
+
releaseDirectSession(jit);
|
|
2369
|
+
jit.directStale = false;
|
|
2370
|
+
jit.directSkip = void 0;
|
|
2371
|
+
jit.directMemories.clear();
|
|
2372
|
+
}
|
|
2373
|
+
/** Region that supplies bytes for a logical PC; undefined means the service-PC fallback. */
|
|
2374
|
+
function codeRegionFor(jit, pc) {
|
|
2375
|
+
for (const region of jit.codeRegions) if (pc >= region.base && pc < region.base + region.size) return region;
|
|
2376
|
+
}
|
|
2377
|
+
/** Additional regions stay on the interpreter; only the primary memory feeds direct programs. */
|
|
2378
|
+
function directCodeInstruction(jit, instruction) {
|
|
2379
|
+
const region = instruction.codeRegion;
|
|
2380
|
+
return region === void 0 || region.memory === jit.options.code;
|
|
2381
|
+
}
|
|
2382
|
+
/** One cache entry may join the direct session: primary code and no effective polling. */
|
|
2383
|
+
function directAdmitted(jit, instruction) {
|
|
2384
|
+
return !instruction.polling && !effectivePolling(jit).has(instruction.pc) && directCodeInstruction(jit, instruction);
|
|
2385
|
+
}
|
|
2386
|
+
function directInstruction(jit, instruction) {
|
|
2387
|
+
return {
|
|
2388
|
+
pc: instruction.pc,
|
|
2389
|
+
nextPc: instruction.nextPc,
|
|
2390
|
+
body: instruction.body,
|
|
2391
|
+
...jit.options.shareDirectBodies === true && instruction.share !== void 0 ? { share: instruction.share } : {}
|
|
2392
|
+
};
|
|
2393
|
+
}
|
|
2394
|
+
/**
|
|
2395
|
+
* Attach validated marks to their translated entries. The block itself is already committed
|
|
2396
|
+
* to the interpreter path, so every rejection only drops sharing for that entry; it never
|
|
2397
|
+
* rejects the translation. Runs once per translated block, never on the `run` hot path.
|
|
2398
|
+
*/
|
|
2399
|
+
function applyDirectShareMarks(jit, startRegion, codeLength, instructions, marks) {
|
|
2400
|
+
if (marks.length === 0) return;
|
|
2401
|
+
const indexByPc = /* @__PURE__ */ new Map();
|
|
2402
|
+
for (let index = 0; index < instructions.length; index++) {
|
|
2403
|
+
const pc = instructions[index].pc;
|
|
2404
|
+
if (!indexByPc.has(pc)) indexByPc.set(pc, index);
|
|
2405
|
+
}
|
|
2406
|
+
const stats = directStats(jit);
|
|
2407
|
+
const rejected = stats.directShareMarkRejected;
|
|
2408
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2409
|
+
for (const { pc, byteLength } of marks) {
|
|
2410
|
+
if (!Number.isSafeInteger(pc) || pc < 0) {
|
|
2411
|
+
rejected.invalidPc++;
|
|
2412
|
+
continue;
|
|
2413
|
+
}
|
|
2414
|
+
if (!Number.isSafeInteger(byteLength) || byteLength <= 0) {
|
|
2415
|
+
rejected.invalidLength++;
|
|
2416
|
+
continue;
|
|
2417
|
+
}
|
|
2418
|
+
if (seen.has(pc)) {
|
|
2419
|
+
rejected.duplicate++;
|
|
2420
|
+
continue;
|
|
2421
|
+
}
|
|
2422
|
+
seen.add(pc);
|
|
2423
|
+
const index = indexByPc.get(pc);
|
|
2424
|
+
if (index === void 0) {
|
|
2425
|
+
rejected.unknownPc++;
|
|
2426
|
+
continue;
|
|
2427
|
+
}
|
|
2428
|
+
const instruction = instructions[index];
|
|
2429
|
+
const region = instruction.codeRegion;
|
|
2430
|
+
if (region === void 0) {
|
|
2431
|
+
rejected.service++;
|
|
2432
|
+
continue;
|
|
2433
|
+
}
|
|
2434
|
+
if (startRegion === void 0 || region !== startRegion) {
|
|
2435
|
+
rejected.crossView++;
|
|
2436
|
+
continue;
|
|
2437
|
+
}
|
|
2438
|
+
const offset = pc - region.base;
|
|
2439
|
+
if (offset < 0 || !Number.isSafeInteger(offset + byteLength) || offset + byteLength > region.size || offset + byteLength > codeLength) {
|
|
2440
|
+
rejected.outsideRegion++;
|
|
2441
|
+
continue;
|
|
2442
|
+
}
|
|
2443
|
+
if (!directAdmitted(jit, instruction)) {
|
|
2444
|
+
rejected.notAdmitted++;
|
|
2445
|
+
continue;
|
|
2446
|
+
}
|
|
2447
|
+
instructions[index] = {
|
|
2448
|
+
...instruction,
|
|
2449
|
+
share: Object.freeze({
|
|
2450
|
+
storage: region.memory,
|
|
2451
|
+
codeVersion: instruction.codeVersion,
|
|
2452
|
+
offset,
|
|
2453
|
+
byteLength
|
|
2454
|
+
})
|
|
2455
|
+
};
|
|
2456
|
+
stats.directShareCandidates++;
|
|
2243
2457
|
}
|
|
2244
|
-
}
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
resistorIntents = /* @__PURE__ */ new Map();
|
|
2282
|
-
sampleEntries = /* @__PURE__ */ new Map();
|
|
2283
|
-
intentEntries = /* @__PURE__ */ new Map();
|
|
2284
|
-
stateSlots = /* @__PURE__ */ new Map();
|
|
2285
|
-
/** Per-instance handles for the fixed resources a resumable process body binds. */
|
|
2286
|
-
resourceHandles = /* @__PURE__ */ new Map();
|
|
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();
|
|
2295
|
-
observationFrames = /* @__PURE__ */ new WeakMap();
|
|
2296
|
-
evaluating = /* @__PURE__ */ new Set();
|
|
2297
|
-
random;
|
|
2298
|
-
host;
|
|
2299
|
-
context;
|
|
2300
|
-
adapter;
|
|
2301
|
-
valid = true;
|
|
2302
|
-
rawProps;
|
|
2303
|
-
constructor(board, entry, id, props, seed, override) {
|
|
2304
|
-
this.board = board;
|
|
2305
|
-
this.entry = entry;
|
|
2306
|
-
this.id = id;
|
|
2307
|
-
for (const declaration of entry.model.declarations) {
|
|
2308
|
-
if ("name" in declaration) this.declarations.set(declaration.name, declaration);
|
|
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, {
|
|
2310
|
-
version: 0,
|
|
2311
|
-
subscribers: /* @__PURE__ */ new Set(),
|
|
2312
|
-
waiters: void 0
|
|
2313
|
-
});
|
|
2314
|
-
}
|
|
2315
|
-
dataObject(props);
|
|
2316
|
-
this.rawProps = props;
|
|
2317
|
-
for (const key of Object.keys(props)) if (this.declarations.get(key)?.kind !== "property") throw new TypeError(`Unknown property ${key}`);
|
|
2318
|
-
this.random = randomState(seed, id, override);
|
|
2319
|
-
this.host = {
|
|
2320
|
-
read: (kind, name) => this.read(kind, name),
|
|
2321
|
-
write: (name, value) => this.writeState(name, value, this.stateSlot(name)),
|
|
2322
|
-
sample: (ref) => {
|
|
2323
|
-
return this.readSample(ref);
|
|
2324
|
-
},
|
|
2325
|
-
intent: (pin, value) => {
|
|
2326
|
-
this.writeIntent(pin, value);
|
|
2327
|
-
},
|
|
2328
|
-
resistor: (pin, resistance) => {
|
|
2329
|
-
const r = electricalNumber(resistance);
|
|
2330
|
-
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
2331
|
-
return this.resistorIntent(pin, r);
|
|
2332
|
-
},
|
|
2333
|
-
memoryLength: (memory) => this.memory(memory).length,
|
|
2334
|
-
load: (kind, memory, offset) => {
|
|
2335
|
-
const plan = this.memoryPlan(memory);
|
|
2336
|
-
const size = kind.startsWith("load8") ? 1 : kind.startsWith("load16") ? 2 : 4;
|
|
2337
|
-
const at = range(plan.bytes, offset, size);
|
|
2338
|
-
const dependencies = this.dependencies();
|
|
2339
|
-
if (dependencies) dependencies.set(plan.slot, plan.slot.version);
|
|
2340
|
-
if (size === 1) return plan.bytes[at];
|
|
2341
|
-
return size === 2 ? plan.view.getUint16(at, kind.endsWith("le")) : plan.view.getUint32(at, kind.endsWith("le"));
|
|
2342
|
-
},
|
|
2343
|
-
store: (kind, memory, offset, value) => {
|
|
2344
|
-
const plan = this.memoryPlan(memory);
|
|
2345
|
-
const size = kind.startsWith("store8") ? 1 : kind.startsWith("store16") ? 2 : 4;
|
|
2346
|
-
const at = range(plan.bytes, offset, size);
|
|
2347
|
-
const n = integer(value, "stored value");
|
|
2348
|
-
if (size === 1) plan.bytes[at] = n;
|
|
2349
|
-
else if (size === 2) plan.view.setUint16(at, n, kind.endsWith("le"));
|
|
2350
|
-
else plan.view.setUint32(at, n, kind.endsWith("le"));
|
|
2351
|
-
this.memoryChangedPlan(plan, at, size);
|
|
2352
|
-
},
|
|
2353
|
-
copyBytes: (memory, offset, length) => {
|
|
2354
|
-
this.track(this.memoryResources, String(memory));
|
|
2355
|
-
const bytes = this.memory(memory), at = range(bytes, offset, length);
|
|
2356
|
-
return bytes.slice(at, at + Number(length));
|
|
2357
|
-
},
|
|
2358
|
-
send: (name, value) => this.send(name, value, false),
|
|
2359
|
-
notify: (name) => {
|
|
2360
|
-
if (!this.notificationOutputs.has(name)) throw new TypeError(`Unknown notification ${name}`);
|
|
2361
|
-
this.notificationOutputs.set(name, board.time);
|
|
2362
|
-
},
|
|
2363
|
-
now: () => board.time,
|
|
2364
|
-
random: () => nextRandom(this.random),
|
|
2365
|
-
call: (name, args) => this.invoke(name, args),
|
|
2366
|
-
assert: (condition, message) => {
|
|
2367
|
-
if (condition !== true) throw new Error(message === void 0 ? "Assertion failed" : String(message));
|
|
2368
|
-
},
|
|
2369
|
-
bind: (specs) => this.bindResources(specs)
|
|
2370
|
-
};
|
|
2371
|
-
this.context = createContext(this, board);
|
|
2372
|
-
}
|
|
2373
|
-
track(resources, name) {
|
|
2374
|
-
this.trackSlot(resources.get(name));
|
|
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
|
-
}
|
|
2381
|
-
trackSlot(slot) {
|
|
2382
|
-
const dependencies = this.dependencies();
|
|
2383
|
-
if (dependencies) dependencies.set(slot, slot.version);
|
|
2384
|
-
}
|
|
2385
|
-
/** Read-only evidence that a process frame bound per-instance resource handles. */
|
|
2386
|
-
bindingReport() {
|
|
2387
|
-
return Object.freeze({
|
|
2388
|
-
instance: this.id,
|
|
2389
|
-
handles: this.resourceHandles.size,
|
|
2390
|
-
bound: Object.freeze([...this.resourceHandles.keys()].sort()),
|
|
2391
|
-
processes: Object.freeze(this.processes.map((process) => Object.freeze({
|
|
2392
|
-
name: process.declaration.kind === "on" ? process.declaration.name : "start",
|
|
2393
|
-
bound: process.frame?.resources?.length ?? 0
|
|
2394
|
-
})))
|
|
2395
|
-
});
|
|
2396
|
-
}
|
|
2397
|
-
dependenciesCurrent(dependencies) {
|
|
2398
|
-
if (!dependencies) return false;
|
|
2399
|
-
for (const [slot, version] of dependencies) if (slot.version !== version) return false;
|
|
2400
|
-
return true;
|
|
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
|
-
}
|
|
2409
|
-
evaluate(compute, target) {
|
|
2410
|
-
const dependencies = target ?? /* @__PURE__ */ new Map();
|
|
2411
|
-
this.dependencyStack.push(dependencies);
|
|
2412
|
-
try {
|
|
2413
|
-
return {
|
|
2414
|
-
value: compute(),
|
|
2415
|
-
dependencies
|
|
2416
|
-
};
|
|
2417
|
-
} finally {
|
|
2418
|
-
this.dependencyStack.pop();
|
|
2419
|
-
const parent = this.dependencies();
|
|
2420
|
-
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
2421
|
-
}
|
|
2422
|
-
}
|
|
2423
|
-
/** Leaf form of :func:`evaluate` for a source walk: no closure and no result wrapper. */
|
|
2424
|
-
evaluateSource(path, dependencies) {
|
|
2425
|
-
this.dependencyStack.push(dependencies);
|
|
2426
|
-
try {
|
|
2427
|
-
return snapshot(this.source(path));
|
|
2428
|
-
} finally {
|
|
2429
|
-
this.dependencyStack.pop();
|
|
2430
|
-
const parent = this.dependencies();
|
|
2431
|
-
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
2432
|
-
}
|
|
2433
|
-
}
|
|
2434
|
-
observe(observation, path) {
|
|
2435
|
-
const read = this.staticObservations.get(observation);
|
|
2436
|
-
if (read) return read();
|
|
2437
|
-
const declaration = this.declarations.get(path[0]);
|
|
2438
|
-
if (declaration?.kind === "pin" || declaration?.kind === "state") {
|
|
2439
|
-
const resources = declaration.kind === "pin" ? this.sampleResources : this.stateResources;
|
|
2440
|
-
const pin = declaration.kind === "pin" ? this.pin(declaration.name) : void 0;
|
|
2441
|
-
if (pin && path[1] !== "sample") throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
2442
|
-
const read = () => {
|
|
2443
|
-
let value = pin ? pin.sample : this.states.get(declaration.name);
|
|
2444
|
-
for (let index = pin ? 2 : 1; index < path.length; index++) value = value[path[index]];
|
|
2445
|
-
return pin ? value : snapshot(value);
|
|
2446
|
-
};
|
|
2447
|
-
const value = read();
|
|
2448
|
-
const slot = resources.get(declaration.name);
|
|
2449
|
-
if (!slot.subscribers.size) this.observerRevision++;
|
|
2450
|
-
slot.subscribers.add(observation);
|
|
2451
|
-
this.staticObservations.set(observation, read);
|
|
2452
|
-
return value;
|
|
2453
|
-
}
|
|
2454
|
-
const frames = this.observationFrames.get(observation);
|
|
2455
|
-
let target;
|
|
2456
|
-
if (frames === void 0) {
|
|
2457
|
-
target = /* @__PURE__ */ new Map();
|
|
2458
|
-
this.observationFrames.set(observation, {
|
|
2459
|
-
first: target,
|
|
2460
|
-
second: /* @__PURE__ */ new Map(),
|
|
2461
|
-
alternate: true
|
|
2462
|
-
});
|
|
2463
|
-
} else {
|
|
2464
|
-
target = frames.alternate ? frames.second : frames.first;
|
|
2465
|
-
frames.alternate = !frames.alternate;
|
|
2466
|
-
target.clear();
|
|
2467
|
-
}
|
|
2468
|
-
const observed = this.evaluateSource(path, target);
|
|
2469
|
-
const previous = this.observations.get(observation);
|
|
2470
|
-
if (previous === void 0 || previous.size !== target.size || !hasEvery(previous, target)) {
|
|
2471
|
-
if (previous) {
|
|
2472
|
-
for (const slot of previous.keys()) if (!target.has(slot) && slot.subscribers.delete(observation) && !slot.subscribers.size) this.observerRevision++;
|
|
2473
|
-
}
|
|
2474
|
-
for (const slot of target.keys()) if (!previous?.has(slot)) {
|
|
2475
|
-
if (!slot.subscribers.size) this.observerRevision++;
|
|
2476
|
-
slot.subscribers.add(observation);
|
|
2477
|
-
}
|
|
2478
|
-
}
|
|
2479
|
-
this.observations.set(observation, target);
|
|
2480
|
-
return observed;
|
|
2481
|
-
}
|
|
2482
|
-
observationReader(observation, path) {
|
|
2483
|
-
return this.staticObservations.get(observation) ?? (() => this.observe(observation, path));
|
|
2484
|
-
}
|
|
2485
|
-
read(kind, name) {
|
|
2486
|
-
if (kind === "state") {
|
|
2487
|
-
this.trackSlot(this.stateSlot(name));
|
|
2488
|
-
return this.states.get(name);
|
|
2489
|
-
}
|
|
2490
|
-
if (kind === "derived") {
|
|
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)) {
|
|
2494
|
-
const declaration = this.declarations.get(name);
|
|
2495
|
-
if (declaration?.kind !== "derived") throw new TypeError(`Unknown derived ${name}`);
|
|
2496
|
-
let buffer = this.derivedBuffers.get(name);
|
|
2497
|
-
if (buffer === void 0) {
|
|
2498
|
-
buffer = /* @__PURE__ */ new Map();
|
|
2499
|
-
this.derivedBuffers.set(name, buffer);
|
|
2500
|
-
} else buffer.clear();
|
|
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();
|
|
2510
|
-
if (parent) for (const [key, version] of cached.dependencies) parent.set(key, version);
|
|
2511
|
-
return cached.value;
|
|
2512
|
-
}
|
|
2513
|
-
const values = kind === "property" ? this.props : this.constants;
|
|
2514
|
-
if (values.has(name)) return values.get(name);
|
|
2515
|
-
if (this.evaluating.has(name)) throw new TypeError(`Circular creation dependency ${name}`);
|
|
2516
|
-
this.evaluating.add(name);
|
|
2517
|
-
try {
|
|
2518
|
-
const declaration = this.declarations.get(name);
|
|
2519
|
-
if (declaration?.kind !== "property" && declaration?.kind !== "const") throw new TypeError(`Invalid creation reference ${name}`);
|
|
2520
|
-
let value;
|
|
2521
|
-
if (declaration.kind === "property") {
|
|
2522
|
-
if (Object.hasOwn(this.rawProps, name)) value = captureValue(declaration.type, this.rawProps[name], this.declarations, true);
|
|
2523
|
-
else if (declaration.default) value = captureValue(declaration.type, computation(declaration.default, this.host), this.declarations, true);
|
|
2524
|
-
else throw new TypeError(`Missing property ${name}`);
|
|
2525
|
-
} else value = computation(declaration.value, this.host);
|
|
2526
|
-
values.set(name, value);
|
|
2527
|
-
return value;
|
|
2528
|
-
} finally {
|
|
2529
|
-
this.evaluating.delete(name);
|
|
2530
|
-
}
|
|
2531
|
-
}
|
|
2532
|
-
invoke(name, args = []) {
|
|
2533
|
-
const fn = this.declarations.get(name);
|
|
2534
|
-
if (fn?.kind !== "function") throw new TypeError(`Unknown function ${name}`);
|
|
2535
|
-
return body(fn.body)(this.host, args);
|
|
2536
|
-
}
|
|
2537
|
-
syncCall(name, args = []) {
|
|
2538
|
-
const step = this.invoke(name, args).next();
|
|
2539
|
-
if (!step.done) throw new TypeError("Unexpected suspension in synchronous function");
|
|
2540
|
-
return step.value;
|
|
2541
|
-
}
|
|
2542
|
-
pin(ref) {
|
|
2543
|
-
const pin = this.pins.get(String(ref));
|
|
2544
|
-
if (!pin) throw new TypeError(`Unknown pin ${String(ref)}`);
|
|
2545
|
-
return pin;
|
|
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
|
-
}
|
|
2565
|
-
/**
|
|
2566
|
-
* One frozen, reusable intent value per (pin, resistance); intents are immutable. The
|
|
2567
|
-
* outer map is keyed by pin name and the inner one by resistance, so the hot path builds
|
|
2568
|
-
* no key string. Bounds: at most 64 names, and at most 8 resistances per name; a full
|
|
2569
|
-
* level is cleared wholesale (no least-recently-used eviction), so identity is only
|
|
2570
|
-
* reused while both levels stay under their limits.
|
|
2571
|
-
*/
|
|
2572
|
-
resistorIntent(pin, resistance) {
|
|
2573
|
-
const name = typeof pin === "string" ? pin : String(pin);
|
|
2574
|
-
let byResistance = this.resistorIntents.get(name);
|
|
2575
|
-
if (byResistance === void 0) {
|
|
2576
|
-
if (this.resistorIntents.size >= 64) this.resistorIntents.clear();
|
|
2577
|
-
byResistance = /* @__PURE__ */ new Map();
|
|
2578
|
-
this.resistorIntents.set(name, byResistance);
|
|
2579
|
-
}
|
|
2580
|
-
let intent = byResistance.get(resistance);
|
|
2581
|
-
if (intent === void 0) {
|
|
2582
|
-
if (byResistance.size >= 8) byResistance.clear();
|
|
2583
|
-
intent = Object.freeze([Object.freeze({
|
|
2584
|
-
to: this.pin(name),
|
|
2585
|
-
resistance
|
|
2586
|
-
})]);
|
|
2587
|
-
byResistance.set(resistance, intent);
|
|
2588
|
-
}
|
|
2589
|
-
return intent;
|
|
2590
|
-
}
|
|
2591
|
-
readSample(ref) {
|
|
2592
|
-
const name = typeof ref === "string" ? ref : String(ref);
|
|
2593
|
-
let entry = this.sampleEntries.get(name);
|
|
2594
|
-
if (entry === void 0) {
|
|
2595
|
-
const pin = this.pin(name);
|
|
2596
|
-
entry = {
|
|
2597
|
-
pin,
|
|
2598
|
-
slot: this.sampleResources.get(pin.name)
|
|
2599
|
-
};
|
|
2600
|
-
if (this.sampleEntries.size >= 512) this.sampleEntries.clear();
|
|
2601
|
-
this.sampleEntries.set(name, entry);
|
|
2602
|
-
}
|
|
2603
|
-
const dependencies = this.dependencies();
|
|
2604
|
-
if (dependencies) dependencies.set(entry.slot, entry.slot.version);
|
|
2605
|
-
return entry.pin.sample;
|
|
2606
|
-
}
|
|
2607
|
-
memory(ref) {
|
|
2608
|
-
const bytes = this.memories.get(String(ref));
|
|
2609
|
-
if (!bytes) throw new TypeError(`Unknown memory ${String(ref)}`);
|
|
2610
|
-
return bytes;
|
|
2611
|
-
}
|
|
2612
|
-
/** Instance-owned numeric bank, mirrored by every non-direct state write. */
|
|
2613
|
-
directStateBank() {
|
|
2614
|
-
if (this.directBank === void 0) {
|
|
2615
|
-
const stateNames = [...this.states.keys()].filter((name) => {
|
|
2616
|
-
const declaration = this.declarations.get(name);
|
|
2617
|
-
return declaration.type.kind === "number" || declaration.type.kind === "bool";
|
|
2618
|
-
});
|
|
2619
|
-
const frozenNames = Object.freeze(stateNames);
|
|
2620
|
-
const stateValues = stateNames.map((name) => this.states.get(name));
|
|
2621
|
-
this.states.bindDirectBank(frozenNames, stateValues);
|
|
2622
|
-
this.directBank = {
|
|
2623
|
-
stateNames: frozenNames,
|
|
2624
|
-
stateSlots: Object.freeze(stateNames.map((name) => this.stateResources.get(name))),
|
|
2625
|
-
states: new Map(stateNames.map((name, index) => [name, index])),
|
|
2626
|
-
stateValues
|
|
2627
|
-
};
|
|
2628
|
-
}
|
|
2629
|
-
return this.directBank;
|
|
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
|
-
}
|
|
2655
|
-
writeState(name, value, slot) {
|
|
2656
|
-
const captured = snapshot(value);
|
|
2657
|
-
this.states.set(name, captured);
|
|
2658
|
-
const index = this.directBank?.states.get(name);
|
|
2659
|
-
if (index !== void 0) this.directBank.stateValues[index] = captured;
|
|
2660
|
-
this.board.invalidate(slot);
|
|
2661
|
-
}
|
|
2662
|
-
stateSlot(name) {
|
|
2663
|
-
let slot = this.stateSlots.get(name);
|
|
2664
|
-
if (slot === void 0) {
|
|
2665
|
-
if (!this.states.has(name)) throw new TypeError(`Unknown state ${name}`);
|
|
2666
|
-
slot = this.stateResources.get(name);
|
|
2667
|
-
this.stateSlots.set(name, slot);
|
|
2668
|
-
}
|
|
2669
|
-
return slot;
|
|
2670
|
-
}
|
|
2671
|
-
memoryPlan(ref) {
|
|
2672
|
-
const name = typeof ref === "string" ? ref : String(ref);
|
|
2673
|
-
let plan = this.memoryPlans.get(name);
|
|
2674
|
-
if (plan === void 0) {
|
|
2675
|
-
const bytes = this.memories.get(name);
|
|
2676
|
-
if (!bytes) throw new TypeError(`Unknown memory ${name}`);
|
|
2677
|
-
plan = {
|
|
2678
|
-
name,
|
|
2679
|
-
bytes,
|
|
2680
|
-
view: this.memoryViews.get(name),
|
|
2681
|
-
slot: this.memoryResources.get(name)
|
|
2682
|
-
};
|
|
2683
|
-
this.memoryPlans.set(name, plan);
|
|
2684
|
-
}
|
|
2685
|
-
return plan;
|
|
2686
|
-
}
|
|
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);
|
|
2740
|
-
}
|
|
2741
|
-
memoryChangedPlan(plan, offset, size) {
|
|
2742
|
-
this.memoryChangedSlot(plan.name, plan.slot, offset, size);
|
|
2743
|
-
}
|
|
2744
|
-
memoryChangedSlot(name, slot, offset, size) {
|
|
2745
|
-
this.board.invalidate(slot);
|
|
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
|
-
}
|
|
2770
|
-
}
|
|
2771
|
-
}
|
|
2772
|
-
/**
|
|
2773
|
-
* Handles for the fixed names a resumable process body uses. They live on the instance,
|
|
2774
|
-
* so a shared program text binds each instance (and each process frame) separately, and
|
|
2775
|
-
* every read still resolves the current sample or state value.
|
|
2776
|
-
*/
|
|
2777
|
-
bindResources(specs) {
|
|
2778
|
-
const handles = new Array(specs.length);
|
|
2779
|
-
for (let index = 0; index < specs.length; index++) {
|
|
2780
|
-
const spec = specs[index];
|
|
2781
|
-
const key = `${spec[0]}\0${spec[1]}`;
|
|
2782
|
-
let handle = this.resourceHandles.get(key);
|
|
2783
|
-
if (handle === void 0) {
|
|
2784
|
-
handle = this.resourceHandle(spec[0], spec[1]);
|
|
2785
|
-
this.resourceHandles.set(key, handle);
|
|
2786
|
-
}
|
|
2787
|
-
handles[index] = handle;
|
|
2788
|
-
}
|
|
2789
|
-
return handles;
|
|
2790
|
-
}
|
|
2791
|
-
/**
|
|
2792
|
-
* Each method repeats the generic host operation exactly: dependency slots are still
|
|
2793
|
-
* recorded, state writes still snapshot, and memory stores still run the code-memory
|
|
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.
|
|
2797
|
-
*/
|
|
2798
|
-
resourceHandle(kind, name) {
|
|
2799
|
-
if (kind === "pin") {
|
|
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;
|
|
2805
|
-
const resolve = () => {
|
|
2806
|
-
if (pin === void 0) {
|
|
2807
|
-
pin = this.pin(name);
|
|
2808
|
-
slot = this.board.electricalSlot(pin);
|
|
2809
|
-
sample = this.sampleResources.get(name);
|
|
2810
|
-
}
|
|
2811
|
-
return pin;
|
|
2812
|
-
};
|
|
2813
|
-
return Object.freeze({
|
|
2814
|
-
sample: () => {
|
|
2815
|
-
const current = resolve();
|
|
2816
|
-
const dependencies = this.dependencies();
|
|
2817
|
-
if (dependencies) dependencies.set(sample, sample.version);
|
|
2818
|
-
return current.sample;
|
|
2819
|
-
},
|
|
2820
|
-
intent: (value) => {
|
|
2821
|
-
const current = resolve();
|
|
2822
|
-
if (current.intent === value) return;
|
|
2823
|
-
current.intent = value;
|
|
2824
|
-
this.board.markElectricalDirty(slot);
|
|
2825
|
-
},
|
|
2826
|
-
resistor: (resistance) => {
|
|
2827
|
-
if (lastIntent !== void 0 && resistance === lastResistance) return lastIntent;
|
|
2828
|
-
const r = electricalNumber(resistance);
|
|
2829
|
-
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
2830
|
-
const intent = this.resistorIntent(name, r);
|
|
2831
|
-
lastResistance = resistance;
|
|
2832
|
-
lastIntent = intent;
|
|
2833
|
-
return intent;
|
|
2834
|
-
}
|
|
2835
|
-
});
|
|
2836
|
-
}
|
|
2837
|
-
if (kind === "state") {
|
|
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;
|
|
2840
|
-
const resolve = () => slot ??= this.stateSlot(name);
|
|
2841
|
-
const states = this.states;
|
|
2842
|
-
return Object.freeze({
|
|
2843
|
-
read: () => {
|
|
2844
|
-
const current = resolve();
|
|
2845
|
-
const dependencies = this.dependencies();
|
|
2846
|
-
if (dependencies) dependencies.set(current, current.version);
|
|
2847
|
-
if (banked === -2) banked = this.directBank?.states.get(name) ?? -1;
|
|
2848
|
-
return banked < 0 ? states.get(name) : this.directBank.stateValues[banked];
|
|
2849
|
-
},
|
|
2850
|
-
write: (value) => {
|
|
2851
|
-
const current = resolve();
|
|
2852
|
-
this.writeState(name, value, current);
|
|
2853
|
-
}
|
|
2854
|
-
});
|
|
2855
|
-
}
|
|
2856
|
-
let plan = this.memories.has(name) ? this.memoryPlan(name) : void 0;
|
|
2857
|
-
const resolve = () => plan ??= this.memoryPlan(name);
|
|
2858
|
-
const read = (size, little, offset) => {
|
|
2859
|
-
const current = resolve();
|
|
2860
|
-
const at = range(current.bytes, offset, size);
|
|
2861
|
-
const dependencies = this.dependencies();
|
|
2862
|
-
if (dependencies) dependencies.set(current.slot, current.slot.version);
|
|
2863
|
-
return size === 1 ? current.bytes[at] : size === 2 ? current.view.getUint16(at, little) : current.view.getUint32(at, little);
|
|
2864
|
-
};
|
|
2865
|
-
const write = (size, little, offset, value) => {
|
|
2866
|
-
const current = resolve();
|
|
2867
|
-
const at = range(current.bytes, offset, size);
|
|
2868
|
-
const n = integer(value, "stored value");
|
|
2869
|
-
if (size === 1) current.bytes[at] = n;
|
|
2870
|
-
else if (size === 2) current.view.setUint16(at, n, little);
|
|
2871
|
-
else current.view.setUint32(at, n, little);
|
|
2872
|
-
this.memoryChangedSlot(name, current.slot, at, size);
|
|
2873
|
-
};
|
|
2874
|
-
return Object.freeze({
|
|
2875
|
-
load8: (offset) => read(1, false, offset),
|
|
2876
|
-
load16le: (offset) => read(2, true, offset),
|
|
2877
|
-
load16be: (offset) => read(2, false, offset),
|
|
2878
|
-
load32le: (offset) => read(4, true, offset),
|
|
2879
|
-
load32be: (offset) => read(4, false, offset),
|
|
2880
|
-
store8: (offset, value) => write(1, false, offset, value),
|
|
2881
|
-
store16le: (offset, value) => write(2, true, offset, value),
|
|
2882
|
-
store16be: (offset, value) => write(2, false, offset, value),
|
|
2883
|
-
store32le: (offset, value) => write(4, true, offset, value),
|
|
2884
|
-
store32be: (offset, value) => write(4, false, offset, value),
|
|
2885
|
-
length: () => this.memory(name).length,
|
|
2886
|
-
copy: (offset, length) => {
|
|
2887
|
-
this.track(this.memoryResources, name);
|
|
2888
|
-
const bytes = this.memory(name), at = range(bytes, offset, length);
|
|
2889
|
-
return bytes.slice(at, at + Number(length));
|
|
2890
|
-
}
|
|
2891
|
-
});
|
|
2892
|
-
}
|
|
2893
|
-
prepare() {
|
|
2894
|
-
for (const declaration of this.entry.model.declarations) if (declaration.kind === "pin") this.pins.set(declaration.name, {
|
|
2895
|
-
id: `${this.id}\0${declaration.name}`,
|
|
2896
|
-
instance: this.id,
|
|
2897
|
-
name: declaration.name,
|
|
2898
|
-
intent: [],
|
|
2899
|
-
sample: {
|
|
2900
|
-
voltage: null,
|
|
2901
|
-
level: "unknown",
|
|
2902
|
-
status: "floating"
|
|
2903
|
-
}
|
|
2904
|
-
});
|
|
2905
|
-
for (const declaration of this.entry.model.declarations) switch (declaration.kind) {
|
|
2906
|
-
case "property":
|
|
2907
|
-
case "const":
|
|
2908
|
-
this.read(declaration.kind, declaration.name);
|
|
2909
|
-
break;
|
|
2910
|
-
case "state":
|
|
2911
|
-
this.states.set(declaration.name, snapshot(computation(declaration.initial, this.host)));
|
|
2912
|
-
break;
|
|
2913
|
-
case "memory": {
|
|
2914
|
-
const length = nonnegative(computation(declaration.length, this.host), "memory length");
|
|
2915
|
-
this.memorySizes.set(declaration.name, length);
|
|
2916
|
-
break;
|
|
2917
|
-
}
|
|
2918
|
-
case "pin": {
|
|
2919
|
-
const pin = this.pins.get(declaration.name);
|
|
2920
|
-
if (declaration.low && declaration.high) {
|
|
2921
|
-
const low = electricalNumber(computation(declaration.low, this.host)), high = electricalNumber(computation(declaration.high, this.host));
|
|
2922
|
-
if (low >= high) throw new ElectricalParameterError("Pin thresholds must satisfy low < high");
|
|
2923
|
-
Object.assign(pin, {
|
|
2924
|
-
low,
|
|
2925
|
-
high
|
|
2926
|
-
});
|
|
2927
|
-
}
|
|
2928
|
-
if (declaration.initial) pin.intent = computation(declaration.initial, this.host);
|
|
2929
|
-
break;
|
|
2930
|
-
}
|
|
2931
|
-
case "channel": {
|
|
2932
|
-
const capacity = nonnegative(computation(declaration.capacity, this.host), "capacity");
|
|
2933
|
-
if (capacity === 0) throw new RangeError("Capacity must be positive");
|
|
2934
|
-
let maxLength;
|
|
2935
|
-
if (declaration.maxLength) {
|
|
2936
|
-
maxLength = nonnegative(computation(declaration.maxLength, this.host), "maxLength");
|
|
2937
|
-
if (maxLength === 0) throw new RangeError("maxLength must be positive");
|
|
2938
|
-
}
|
|
2939
|
-
this.channels.set(declaration.name, {
|
|
2940
|
-
declaration,
|
|
2941
|
-
capacity,
|
|
2942
|
-
...maxLength === void 0 ? {} : { maxLength },
|
|
2943
|
-
overflow: declaration.overflow ? String(computation(declaration.overflow, this.host)) : "drop_new",
|
|
2944
|
-
incoming: new Fifo(),
|
|
2945
|
-
pending: new Fifo(),
|
|
2946
|
-
outgoing: new Fifo()
|
|
2947
|
-
});
|
|
2948
|
-
break;
|
|
2949
|
-
}
|
|
2950
|
-
case "notification":
|
|
2951
|
-
if (declaration.direction === "output") this.notificationOutputs.set(declaration.name, null);
|
|
2952
|
-
break;
|
|
2953
|
-
case "start":
|
|
2954
|
-
case "on":
|
|
2955
|
-
this.processes.push({
|
|
2956
|
-
instance: this,
|
|
2957
|
-
declaration,
|
|
2958
|
-
resumable: resumable(declaration.body),
|
|
2959
|
-
...declaration.kind === "on" ? { source: declaration.sourcePath } : {},
|
|
2960
|
-
state: "idle",
|
|
2961
|
-
pending: false
|
|
2962
|
-
});
|
|
2963
|
-
break;
|
|
2964
|
-
case "reaction": this.reactions.push({
|
|
2965
|
-
instance: this,
|
|
2966
|
-
declaration,
|
|
2967
|
-
previous: void 0,
|
|
2968
|
-
synchronousProgram: synchronousBody(this, declaration.body) ?? null,
|
|
2969
|
-
frame: void 0
|
|
2970
|
-
});
|
|
2971
|
-
}
|
|
2972
|
-
for (const [name, channel] of this.channels) if (channel.declaration.direction === "input") this.batchInputs.push(channel);
|
|
2973
|
-
else {
|
|
2974
|
-
this.batchOutputs.push([name, channel]);
|
|
2975
|
-
this.emptyOutputs[name] = EMPTY_MESSAGES;
|
|
2976
|
-
}
|
|
2977
|
-
for (const name of this.notificationOutputs.keys()) {
|
|
2978
|
-
this.batchNotifications.push(name);
|
|
2979
|
-
this.emptyOutputs[name] = null;
|
|
2980
|
-
}
|
|
2981
|
-
Object.freeze(this.emptyOutputs);
|
|
2982
|
-
}
|
|
2983
|
-
captureBatch(from, to) {
|
|
2984
|
-
for (const channel of this.batchInputs) if (channel.incoming.length) channel.incoming.clear();
|
|
2985
|
-
let outputs = this.emptyOutputs;
|
|
2986
|
-
for (const [name, channel] of this.batchOutputs) {
|
|
2987
|
-
if (!channel.outgoing.length) continue;
|
|
2988
|
-
if (outputs === this.emptyOutputs) outputs = Object.assign(Object.create(null), this.emptyOutputs);
|
|
2989
|
-
outputs[name] = Object.freeze(channel.outgoing.drain().map((message) => Object.freeze({
|
|
2990
|
-
time: message.time,
|
|
2991
|
-
value: externalValue(message.value)
|
|
2992
|
-
})));
|
|
2993
|
-
}
|
|
2994
|
-
for (const name of this.batchNotifications) {
|
|
2995
|
-
const notified = this.notificationOutputs.get(name);
|
|
2996
|
-
if (notified === null) continue;
|
|
2997
|
-
if (outputs === this.emptyOutputs) outputs = Object.assign(Object.create(null), this.emptyOutputs);
|
|
2998
|
-
outputs[name] = Object.freeze({ time: notified });
|
|
2999
|
-
this.notificationOutputs.set(name, null);
|
|
3000
|
-
}
|
|
3001
|
-
return Object.freeze({
|
|
3002
|
-
from,
|
|
3003
|
-
to,
|
|
3004
|
-
outputs: outputs === this.emptyOutputs ? outputs : Object.freeze(outputs)
|
|
3005
|
-
});
|
|
3006
|
-
}
|
|
3007
|
-
assertCreation() {
|
|
3008
|
-
for (const declaration of this.entry.model.declarations) {
|
|
3009
|
-
if (declaration.kind !== "assert") continue;
|
|
3010
|
-
if (!body(declaration.body)(this.host).next().done) throw new TypeError("Creation assertion suspended");
|
|
3011
|
-
}
|
|
3012
|
-
}
|
|
3013
|
-
allocate() {
|
|
3014
|
-
for (const [name, length] of this.memorySizes) {
|
|
3015
|
-
const bytes = new Uint8Array(length);
|
|
3016
|
-
this.memories.set(name, bytes);
|
|
3017
|
-
this.memoryViews.set(name, new DataView(bytes.buffer));
|
|
3018
|
-
}
|
|
3019
|
-
this.memorySizes.clear();
|
|
3020
|
-
}
|
|
3021
|
-
assertCallAllowed() {
|
|
3022
|
-
this.board.assertCallAllowed(this);
|
|
3023
|
-
}
|
|
3024
|
-
inputs() {
|
|
3025
|
-
const result = Object.create(null);
|
|
3026
|
-
for (const declaration of this.entry.model.declarations) {
|
|
3027
|
-
if (declaration.kind === "channel" && declaration.direction === "input") result[declaration.name] = Object.freeze({ send: (value) => {
|
|
3028
|
-
this.assertCallAllowed();
|
|
3029
|
-
this.send(declaration.name, value, true);
|
|
3030
|
-
} });
|
|
3031
|
-
if (declaration.kind === "notification" && declaration.direction === "input") result[declaration.name] = Object.freeze({ notify: () => {
|
|
3032
|
-
this.assertCallAllowed();
|
|
3033
|
-
this.notificationInputs.add(declaration.name);
|
|
3034
|
-
} });
|
|
3035
|
-
}
|
|
3036
|
-
return Object.freeze(result);
|
|
3037
|
-
}
|
|
3038
|
-
publicMemory() {
|
|
3039
|
-
const result = Object.create(null);
|
|
3040
|
-
for (const declaration of this.entry.model.declarations) if (declaration.kind === "memory" && declaration.public) result[declaration.name] = bytesView(this.memory(declaration.name), () => {
|
|
3041
|
-
this.board.assertAlive(this);
|
|
3042
|
-
if (![
|
|
3043
|
-
"idle",
|
|
3044
|
-
"initialize",
|
|
3045
|
-
"sync"
|
|
3046
|
-
].includes(this.board.phase)) throw this.board.error("INVALID_CALL_STATE", "Memory cannot be borrowed in this phase", this);
|
|
3047
|
-
if (this.board.phase === "initialize" && this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "Another factory is initializing", this);
|
|
3048
|
-
});
|
|
3049
|
-
return Object.freeze(result);
|
|
3050
|
-
}
|
|
3051
|
-
send(name, value, input) {
|
|
3052
|
-
const channel = this.channels.get(name);
|
|
3053
|
-
if (!channel || channel.declaration.direction !== (input ? "input" : "output")) throw new TypeError(`Unknown channel ${name}`);
|
|
3054
|
-
const captured = captureValue(channel.declaration.type, value, this.declarations);
|
|
3055
|
-
if (channel.maxLength !== void 0 && captured.length > channel.maxLength) throw new RangeError("Message exceeds maxLength");
|
|
3056
|
-
const queue = input ? channel.pending : channel.outgoing;
|
|
3057
|
-
if (queue.length === channel.capacity) {
|
|
3058
|
-
if (channel.overflow === "drop_new") return;
|
|
3059
|
-
if (channel.overflow === "error") {
|
|
3060
|
-
const error = this.board.error("CHANNEL_OVERFLOW", `Channel ${name} overflow`, this);
|
|
3061
|
-
this.board.fault(error);
|
|
3062
|
-
throw error;
|
|
3063
|
-
}
|
|
3064
|
-
queue.shift();
|
|
3065
|
-
}
|
|
3066
|
-
queue.push({
|
|
3067
|
-
time: this.board.time,
|
|
3068
|
-
value: captured
|
|
2458
|
+
}
|
|
2459
|
+
function effectivePolling(jit) {
|
|
2460
|
+
return jit.metadata?.polling ?? jit.polling;
|
|
2461
|
+
}
|
|
2462
|
+
function effectiveDelays(jit) {
|
|
2463
|
+
return jit.metadata?.delays ?? jit.delays;
|
|
2464
|
+
}
|
|
2465
|
+
/** Aliases of the primary memory must refresh together before a direct program is built. */
|
|
2466
|
+
function primaryCodeRegions(jit) {
|
|
2467
|
+
return jit.codeRegions.filter((region) => region.memory === jit.options.code);
|
|
2468
|
+
}
|
|
2469
|
+
/**
|
|
2470
|
+
* Lazily (re)build code-derived metadata for the requested regions. Provider calls never
|
|
2471
|
+
* happen from attach/store; this runs inside the translate phase immediately before the
|
|
2472
|
+
* polling/direct/translate decision that needs it.
|
|
2473
|
+
*/
|
|
2474
|
+
function ensureMetadata(board, jit, regions) {
|
|
2475
|
+
const metadata = jit.metadata;
|
|
2476
|
+
const provider = jit.options.codeMetadata;
|
|
2477
|
+
if (metadata === void 0 || provider === void 0) return;
|
|
2478
|
+
const needed = [];
|
|
2479
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2480
|
+
for (const region of regions) {
|
|
2481
|
+
const state = metadata.regions.get(region);
|
|
2482
|
+
if (state === void 0 || seen.has(state)) continue;
|
|
2483
|
+
seen.add(state);
|
|
2484
|
+
const version = jit.instance.codeVersion(region.memory);
|
|
2485
|
+
if (state.appliedVersion === version && state.pending.length === 0 && !state.dirty) continue;
|
|
2486
|
+
const changes = state.appliedVersion === -1 ? [Object.freeze({
|
|
2487
|
+
memory: region.memory,
|
|
2488
|
+
offset: 0,
|
|
2489
|
+
length: region.size
|
|
2490
|
+
})] : state.pending.map((change) => Object.freeze({ ...change }));
|
|
2491
|
+
needed.push({
|
|
2492
|
+
state,
|
|
2493
|
+
changes,
|
|
2494
|
+
version
|
|
3069
2495
|
});
|
|
3070
2496
|
}
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
"
|
|
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
|
|
2497
|
+
if (needed.length === 0) return;
|
|
2498
|
+
const prior = board.phase;
|
|
2499
|
+
board.phase = "translate";
|
|
2500
|
+
const prepared = [];
|
|
2501
|
+
try {
|
|
2502
|
+
for (const item of needed) {
|
|
2503
|
+
const { region } = item.state;
|
|
2504
|
+
const memory = jit.instance.memory(region.memory);
|
|
2505
|
+
const bytes = region.size >= memory.length ? memory : memory.subarray(0, region.size);
|
|
2506
|
+
let valid = true;
|
|
2507
|
+
try {
|
|
2508
|
+
const code = bytesView(bytes, () => {
|
|
2509
|
+
if (!valid) throw board.error("OBJECT_DISPOSED", "Metadata view expired", jit.instance);
|
|
3099
2510
|
});
|
|
3100
|
-
|
|
3101
|
-
|
|
2511
|
+
const view = Object.freeze({
|
|
2512
|
+
memory: region.memory,
|
|
2513
|
+
base: region.base,
|
|
2514
|
+
size: region.size,
|
|
2515
|
+
codeVersion: item.version,
|
|
2516
|
+
code
|
|
2517
|
+
});
|
|
2518
|
+
const result = provider(Object.freeze({
|
|
2519
|
+
region: view,
|
|
2520
|
+
changes: Object.freeze(item.changes.slice())
|
|
2521
|
+
}));
|
|
2522
|
+
acceptSync(result);
|
|
2523
|
+
const captured = jit.instance.captureCodeMetadata(region, result);
|
|
2524
|
+
prepared.push({
|
|
2525
|
+
state: item.state,
|
|
2526
|
+
version: item.version,
|
|
2527
|
+
polling: captured.polling,
|
|
2528
|
+
delays: captured.delays
|
|
2529
|
+
});
|
|
2530
|
+
} finally {
|
|
2531
|
+
valid = false;
|
|
3102
2532
|
}
|
|
3103
2533
|
}
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
2534
|
+
let primary = false;
|
|
2535
|
+
for (const item of prepared) {
|
|
2536
|
+
item.state.appliedVersion = item.version;
|
|
2537
|
+
item.state.dirty = false;
|
|
2538
|
+
item.state.pending.length = 0;
|
|
2539
|
+
item.state.polling.clear();
|
|
2540
|
+
for (const [pc, binding] of item.polling) item.state.polling.set(pc, binding);
|
|
2541
|
+
item.state.delays.clear();
|
|
2542
|
+
for (const [pc, delay] of item.delays) item.state.delays.set(pc, delay);
|
|
2543
|
+
if (item.state.region.memory === jit.options.code) primary = true;
|
|
2544
|
+
}
|
|
2545
|
+
metadata.polling.clear();
|
|
2546
|
+
metadata.delays.clear();
|
|
2547
|
+
for (const state of metadata.regions.values()) {
|
|
2548
|
+
for (const [pc, binding] of state.polling) metadata.polling.set(pc, binding);
|
|
2549
|
+
for (const [pc, delay] of state.delays) metadata.delays.set(pc, delay);
|
|
2550
|
+
}
|
|
2551
|
+
if (primary) invalidateDirect(jit, true);
|
|
2552
|
+
else if (jit.direct !== void 0 && !jit.directStale) updateDirectDelays(jit);
|
|
2553
|
+
} catch (cause) {
|
|
2554
|
+
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
2555
|
+
} finally {
|
|
2556
|
+
board.phase = prior;
|
|
3108
2557
|
}
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
2558
|
+
}
|
|
2559
|
+
function ensureMetadataForPc(board, jit, pc) {
|
|
2560
|
+
if (jit.metadata === void 0) return;
|
|
2561
|
+
const region = codeRegionFor(jit, pc) ?? jit.codeRegions[0];
|
|
2562
|
+
const needed = [region];
|
|
2563
|
+
if (board.execution === "throughput" && board.jits.length === 1 && region.memory === jit.options.code) needed.push(...primaryCodeRegions(jit));
|
|
2564
|
+
ensureMetadata(board, jit, needed);
|
|
2565
|
+
}
|
|
2566
|
+
/** Publish the current subscriber sets into the masks the compiled blocks test on writes. */
|
|
2567
|
+
function syncDirectBarriers(jit) {
|
|
2568
|
+
const entry = directBarriers(jit);
|
|
2569
|
+
const { stateIndex, memoryIndex } = entry;
|
|
2570
|
+
if (stateIndex === void 0 || memoryIndex === void 0) return;
|
|
2571
|
+
const fill = (mask, index, guards) => {
|
|
2572
|
+
mask.fill(0);
|
|
2573
|
+
for (const name of guards) {
|
|
2574
|
+
const slot = index.get(name);
|
|
2575
|
+
if (slot === void 0) continue;
|
|
2576
|
+
mask[slot >>> 5] = (mask[slot >>> 5] ?? 0) | 1 << (slot & 31);
|
|
2577
|
+
}
|
|
2578
|
+
};
|
|
2579
|
+
fill(entry.stateMask, stateIndex, jit.directStateGuards);
|
|
2580
|
+
fill(entry.memoryMask, memoryIndex, jit.directMemoryGuards);
|
|
2581
|
+
}
|
|
2582
|
+
/** Settle one checkpoint boundary and attribute it to its source when diagnostics are on. */
|
|
2583
|
+
function checkpointBoundary(board, stats, source) {
|
|
2584
|
+
if (stats !== void 0) stats.boundaryCalls++;
|
|
2585
|
+
board.boundary(source);
|
|
2586
|
+
}
|
|
2587
|
+
/** Reverse-map the hook's resource slots; slot values are not bank indices. */
|
|
2588
|
+
function entryBlockedNames(jit) {
|
|
2589
|
+
const states = /* @__PURE__ */ new Set();
|
|
2590
|
+
const memories = /* @__PURE__ */ new Set([jit.options.code]);
|
|
2591
|
+
const instance = jit.instance;
|
|
2592
|
+
for (const [name, slot] of instance.stateResources) if (jit.directHookReads.has(slot)) states.add(name);
|
|
2593
|
+
for (const [name, slot] of instance.memoryResources) if (jit.directHookReads.has(slot)) memories.add(name);
|
|
2594
|
+
return {
|
|
2595
|
+
states,
|
|
2596
|
+
memories
|
|
2597
|
+
};
|
|
2598
|
+
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Refresh observer masks in place and invalidate the session only when the entry barrier
|
|
2601
|
+
* generation changes (the hook discovered a new state or memory dependency). Returns true
|
|
2602
|
+
* when the running session must stop after the current instruction.
|
|
2603
|
+
*/
|
|
2604
|
+
function refreshDirectGuards(jit, hookReads, running = false) {
|
|
2605
|
+
let observersChanged = false;
|
|
2606
|
+
let entryChanged = false;
|
|
2607
|
+
const instance = jit.instance;
|
|
2608
|
+
const hookCount = jit.directHookReads.size;
|
|
2609
|
+
if (hookReads) for (const slot of hookReads.keys()) {
|
|
2610
|
+
if (jit.directHookReads.has(slot)) continue;
|
|
2611
|
+
jit.directHookReads.add(slot);
|
|
2612
|
+
let resource = false;
|
|
2613
|
+
for (const candidate of instance.stateResources.values()) if (candidate === slot) {
|
|
2614
|
+
resource = true;
|
|
2615
|
+
break;
|
|
2616
|
+
}
|
|
2617
|
+
if (!resource) {
|
|
2618
|
+
for (const candidate of instance.memoryResources.values()) if (candidate === slot) {
|
|
2619
|
+
resource = true;
|
|
2620
|
+
break;
|
|
3142
2621
|
}
|
|
3143
|
-
polling.set(pc, Object.freeze({
|
|
3144
|
-
predicate,
|
|
3145
|
-
...consume === void 0 ? {} : { consume },
|
|
3146
|
-
...wait === void 0 ? {} : { wait }
|
|
3147
|
-
}));
|
|
3148
2622
|
}
|
|
3149
|
-
|
|
2623
|
+
if (resource) entryChanged = true;
|
|
3150
2624
|
}
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3179
|
-
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
2625
|
+
if (jit.directObserverRevision === instance.observerRevision && hookCount === jit.directHookReads.size && !entryChanged) return false;
|
|
2626
|
+
jit.directObserverRevision = instance.observerRevision;
|
|
2627
|
+
for (const [name, slot] of instance.stateResources) {
|
|
2628
|
+
const blocked = slot.subscribers.size > 0;
|
|
2629
|
+
if (jit.directStateGuards.has(name) === blocked) continue;
|
|
2630
|
+
if (blocked) jit.directStateGuards.add(name);
|
|
2631
|
+
else jit.directStateGuards.delete(name);
|
|
2632
|
+
observersChanged = true;
|
|
2633
|
+
}
|
|
2634
|
+
for (const [name, slot] of instance.memoryResources) {
|
|
2635
|
+
const blocked = slot.subscribers.size > 0;
|
|
2636
|
+
if (jit.directMemoryGuards.has(name) === blocked) continue;
|
|
2637
|
+
if (blocked) jit.directMemoryGuards.add(name);
|
|
2638
|
+
else jit.directMemoryGuards.delete(name);
|
|
2639
|
+
observersChanged = true;
|
|
2640
|
+
}
|
|
2641
|
+
let invalidated = false;
|
|
2642
|
+
if (entryChanged && jit.direct !== void 0) invalidated = staleDirectSession(jit, running);
|
|
2643
|
+
if (observersChanged) syncDirectBarriers(jit);
|
|
2644
|
+
if (observersChanged || entryChanged) jit.directSkip = void 0;
|
|
2645
|
+
return invalidated;
|
|
2646
|
+
}
|
|
2647
|
+
/**
|
|
2648
|
+
* Direct programs advance `frame.time` between host calls, while the board only adopts the
|
|
2649
|
+
* final time after `run` returns. Publish the current frame time before every host effect so
|
|
2650
|
+
* channel publications, electrical intents and diagnostics carry the instruction's time.
|
|
2651
|
+
*/
|
|
2652
|
+
function directHost(board, instance, frame) {
|
|
2653
|
+
const publishTime = () => {
|
|
2654
|
+
board.time = frame.time;
|
|
2655
|
+
};
|
|
2656
|
+
return {
|
|
2657
|
+
send: (name, value) => {
|
|
2658
|
+
publishTime();
|
|
2659
|
+
instance.host.send(name, value);
|
|
2660
|
+
},
|
|
2661
|
+
sample: (pin) => {
|
|
2662
|
+
publishTime();
|
|
2663
|
+
return instance.host.sample(pin);
|
|
2664
|
+
},
|
|
2665
|
+
intent: (pin, value) => {
|
|
2666
|
+
publishTime();
|
|
2667
|
+
if (instance.writeIntent(pin, value)) frame.publishPending = true;
|
|
2668
|
+
},
|
|
2669
|
+
resistor: (pin, resistance) => {
|
|
2670
|
+
publishTime();
|
|
2671
|
+
return instance.host.resistor(pin, resistance);
|
|
2672
|
+
},
|
|
2673
|
+
notify: (name) => {
|
|
2674
|
+
publishTime();
|
|
2675
|
+
instance.host.notify(name);
|
|
2676
|
+
},
|
|
2677
|
+
now: () => {
|
|
2678
|
+
publishTime();
|
|
2679
|
+
return instance.host.now();
|
|
2680
|
+
},
|
|
2681
|
+
random: () => {
|
|
2682
|
+
publishTime();
|
|
2683
|
+
return instance.host.random();
|
|
2684
|
+
},
|
|
2685
|
+
assert: (condition, message) => {
|
|
2686
|
+
publishTime();
|
|
2687
|
+
instance.host.assert(condition, message);
|
|
2688
|
+
}
|
|
2689
|
+
};
|
|
2690
|
+
}
|
|
2691
|
+
/** Build the numeric bank layout once; rebuilds then only need the cached instruction bodies. */
|
|
2692
|
+
function directLayout(jit) {
|
|
2693
|
+
if (jit.directLayout !== void 0) return jit.directLayout;
|
|
2694
|
+
const instance = jit.instance;
|
|
2695
|
+
const bank = instance.directStateBank();
|
|
2696
|
+
const memoryNames = [...instance.memories.keys()];
|
|
2697
|
+
const memories = new Map(memoryNames.map((name, index) => [name, index]));
|
|
2698
|
+
const memoryBanks = memoryNames.map((name) => instance.memory(name));
|
|
2699
|
+
const memoryViews = memoryBanks.map((memory) => new DataView(memory.buffer, memory.byteOffset, memory.byteLength));
|
|
2700
|
+
const pinNames = [...instance.pins.keys()];
|
|
2701
|
+
const pins = new Map(pinNames.map((name, index) => [name, index]));
|
|
2702
|
+
const pinObjects = pinNames.map((name) => instance.pin(name));
|
|
2703
|
+
const pinElectricalSlots = Int32Array.from(pinObjects, (pin) => instance.board.electricalSlot(pin));
|
|
2704
|
+
jit.directLayout = {
|
|
2705
|
+
...bank,
|
|
2706
|
+
memoryNames: Object.freeze(memoryNames),
|
|
2707
|
+
memories,
|
|
2708
|
+
memoryBanks: Object.freeze(memoryBanks),
|
|
2709
|
+
memoryViews: Object.freeze(memoryViews),
|
|
2710
|
+
pinNames: Object.freeze(pinNames),
|
|
2711
|
+
pins,
|
|
2712
|
+
pinObjects: Object.freeze(pinObjects),
|
|
2713
|
+
pinElectricalSlots
|
|
2714
|
+
};
|
|
2715
|
+
return jit.directLayout;
|
|
2716
|
+
}
|
|
2717
|
+
function flushDirect(board, instance, layout, frame) {
|
|
2718
|
+
board.time = frame.time;
|
|
2719
|
+
frame.publishPending = false;
|
|
2720
|
+
let wrote = instance.commitDirectStates(frame.stateDirty);
|
|
2721
|
+
for (let word = 0; word < frame.memoryDirty.length; word++) {
|
|
2722
|
+
let bits = frame.memoryDirty[word];
|
|
2723
|
+
if (bits === 0) continue;
|
|
2724
|
+
frame.memoryDirty[word] = 0;
|
|
2725
|
+
while (bits !== 0) {
|
|
2726
|
+
const slot = word * 32 + 31 - Math.clz32(bits & -bits);
|
|
2727
|
+
instance.memoryChanged(layout.memoryNames[slot]);
|
|
2728
|
+
bits &= bits - 1;
|
|
2729
|
+
wrote = true;
|
|
3183
2730
|
}
|
|
3184
|
-
return delays;
|
|
3185
2731
|
}
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3190
|
-
|
|
3191
|
-
|
|
3192
|
-
|
|
2732
|
+
return wrote;
|
|
2733
|
+
}
|
|
2734
|
+
/** Declared busy-wait heads fold their remaining rounds inside the emitted body. */
|
|
2735
|
+
function directDelayLoops(jit, layout) {
|
|
2736
|
+
const delayLoops = /* @__PURE__ */ new Map();
|
|
2737
|
+
for (const [headPc, delay] of effectiveDelays(jit)) {
|
|
2738
|
+
const counter = layout.states.get(delay.counter);
|
|
2739
|
+
if (counter === void 0) continue;
|
|
2740
|
+
const pending = delay.pending === void 0 ? void 0 : {
|
|
2741
|
+
state: layout.states.get(delay.pending.state),
|
|
2742
|
+
period: layout.states.get(delay.pending.period),
|
|
2743
|
+
active: layout.states.get(delay.pending.active)
|
|
3193
2744
|
};
|
|
2745
|
+
delayLoops.set(headPc, pending !== void 0 && pending.state !== void 0 && pending.period !== void 0 && pending.active !== void 0 ? {
|
|
2746
|
+
counter,
|
|
2747
|
+
cycles: delay.cycles,
|
|
2748
|
+
pending
|
|
2749
|
+
} : {
|
|
2750
|
+
counter,
|
|
2751
|
+
cycles: delay.cycles
|
|
2752
|
+
});
|
|
3194
2753
|
}
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
|
|
2754
|
+
return delayLoops;
|
|
2755
|
+
}
|
|
2756
|
+
/** Binding identity of one session; the compiler keys body and shard reuse off it. */
|
|
2757
|
+
function directBinding(jit, layout) {
|
|
2758
|
+
const { states, memories, pins } = layout;
|
|
2759
|
+
const entry = entryBlockedNames(jit);
|
|
2760
|
+
return {
|
|
2761
|
+
states,
|
|
2762
|
+
memories,
|
|
2763
|
+
pins,
|
|
2764
|
+
pc: {
|
|
2765
|
+
name: jit.options.pc,
|
|
2766
|
+
index: states.get(jit.options.pc)
|
|
2767
|
+
},
|
|
2768
|
+
host: true,
|
|
2769
|
+
...jit.functions === void 0 ? {} : { functions: jit.functions },
|
|
2770
|
+
split: true,
|
|
2771
|
+
shardSize: DIRECT_SHARD_SIZE,
|
|
2772
|
+
delayLoops: directDelayLoops(jit, layout),
|
|
2773
|
+
entryBlockedStates: entry.states,
|
|
2774
|
+
entryBlockedMemories: entry.memories,
|
|
2775
|
+
blockedStates: jit.directStateGuards,
|
|
2776
|
+
blockedMemories: jit.directMemoryGuards
|
|
2777
|
+
};
|
|
2778
|
+
}
|
|
2779
|
+
/** Grow the frame's local bank only between runs, never while a suffix is suspended. */
|
|
2780
|
+
function growDirectLocals(direct) {
|
|
2781
|
+
if (direct.frame.resume === 0 && direct.frame.locals.length < direct.session.localCount) direct.frame.locals.length = direct.session.localCount;
|
|
2782
|
+
}
|
|
2783
|
+
/** Reconcile busy-wait descriptors; the session recompiles only affected entries. */
|
|
2784
|
+
function updateDirectDelays(jit) {
|
|
2785
|
+
const direct = jit.direct;
|
|
2786
|
+
if (direct === void 0 || jit.directStale) return;
|
|
2787
|
+
direct.session.updateDelays(directDelayLoops(jit, jit.directLayout));
|
|
2788
|
+
growDirectLocals(direct);
|
|
2789
|
+
syncDirectSessionStats(jit, direct.session);
|
|
2790
|
+
}
|
|
2791
|
+
function addDirectInstructions(jit, instructions) {
|
|
2792
|
+
const direct = jit.direct;
|
|
2793
|
+
if (direct === void 0 || jit.directStale || direct.frame.resume !== 0) return;
|
|
2794
|
+
const admitted = [];
|
|
2795
|
+
for (const instruction of instructions) if (directAdmitted(jit, instruction)) admitted.push(instruction);
|
|
2796
|
+
direct.session.updateDelays(directDelayLoops(jit, jit.directLayout));
|
|
2797
|
+
if (admitted.length) {
|
|
2798
|
+
direct.session.add(admitted.map((instruction) => directInstruction(jit, instruction)));
|
|
2799
|
+
for (const instruction of admitted) jit.directMemories.add(instruction.codeRegion?.memory ?? jit.options.code);
|
|
2800
|
+
growDirectLocals(direct);
|
|
2801
|
+
}
|
|
2802
|
+
syncDirectSessionStats(jit, direct.session);
|
|
2803
|
+
}
|
|
2804
|
+
/**
|
|
2805
|
+
* Commit one translated block to the active session. Overlapping replacements are handled by
|
|
2806
|
+
* the session when idle. While an old suffix is suspended, translation must not retarget it:
|
|
2807
|
+
* the session is marked stale so the suffix finishes once and a fresh session rebuilds.
|
|
2808
|
+
*/
|
|
2809
|
+
function commitDirectTranslation(jit, instructions, replaced) {
|
|
2810
|
+
const direct = jit.direct;
|
|
2811
|
+
if (direct === void 0 || jit.directStale) return;
|
|
2812
|
+
if (direct.frame.resume !== 0) {
|
|
2813
|
+
if (replaced) staleDirectSession(jit, false);
|
|
2814
|
+
else updateDirectDelays(jit);
|
|
2815
|
+
return;
|
|
2816
|
+
}
|
|
2817
|
+
addDirectInstructions(jit, instructions);
|
|
2818
|
+
}
|
|
2819
|
+
/** Install the fixed dispatcher and seed it from every admission-eligible cached entry. */
|
|
2820
|
+
function ensureDirectSession(board, jit) {
|
|
2821
|
+
if (jit.direct !== void 0) return jit.direct;
|
|
2822
|
+
if (jit.directStale) jit.directStale = false;
|
|
2823
|
+
const layout = directLayout(jit);
|
|
2824
|
+
if (jit.directCacheInvalid) {
|
|
2825
|
+
if (jit.directCache !== void 0) compileDirectJit.clearCache(jit.directCache);
|
|
2826
|
+
jit.directCacheInvalid = false;
|
|
2827
|
+
}
|
|
2828
|
+
const cache = jit.directCache ??= compileDirectJit.createCache();
|
|
2829
|
+
let session;
|
|
2830
|
+
const admitted = [];
|
|
2831
|
+
try {
|
|
2832
|
+
session = compileDirectJit.createSession(directBinding(jit, layout), cache);
|
|
2833
|
+
for (const instruction of jit.cache.values()) if (directAdmitted(jit, instruction)) admitted.push(instruction);
|
|
2834
|
+
if (admitted.length) session.add(admitted.map((instruction) => directInstruction(jit, instruction)));
|
|
2835
|
+
session.updateDelays(directDelayLoops(jit, layout));
|
|
2836
|
+
} catch (cause) {
|
|
2837
|
+
session?.dispose();
|
|
2838
|
+
throw cause;
|
|
2839
|
+
}
|
|
2840
|
+
for (const instruction of admitted) jit.directMemories.add(instruction.codeRegion?.memory ?? jit.options.code);
|
|
2841
|
+
const barrier = directBarriers(jit);
|
|
2842
|
+
barrier.stateIndex = layout.states;
|
|
2843
|
+
barrier.memoryIndex = layout.memories;
|
|
2844
|
+
const stateMaskWords = Math.ceil(layout.stateNames.length / 32);
|
|
2845
|
+
const memoryMaskWords = Math.ceil(layout.memoryNames.length / 32);
|
|
2846
|
+
if (barrier.stateMask.length !== stateMaskWords) barrier.stateMask = new Int32Array(stateMaskWords);
|
|
2847
|
+
if (barrier.memoryMask.length !== memoryMaskWords) barrier.memoryMask = new Int32Array(memoryMaskWords);
|
|
2848
|
+
syncDirectBarriers(jit);
|
|
2849
|
+
const frame = createDirectFrame(board, jit, layout, session, barrier);
|
|
2850
|
+
const execution = {
|
|
2851
|
+
session,
|
|
2852
|
+
frame,
|
|
2853
|
+
get pcs() {
|
|
2854
|
+
return new Set(session.pcs);
|
|
3219
2855
|
}
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3232
|
-
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
|
|
3237
|
-
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
const
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
2856
|
+
};
|
|
2857
|
+
jit.direct = execution;
|
|
2858
|
+
jit.directStale = false;
|
|
2859
|
+
const stats = directStats(jit);
|
|
2860
|
+
stats.directCompiles++;
|
|
2861
|
+
stats.directSessions++;
|
|
2862
|
+
syncDirectSessionStats(jit, session);
|
|
2863
|
+
return execution;
|
|
2864
|
+
}
|
|
2865
|
+
function createDirectFrame(board, jit, layout, session, barrier) {
|
|
2866
|
+
const instance = jit.instance;
|
|
2867
|
+
const { pinObjects, pinNames, pinElectricalSlots } = layout;
|
|
2868
|
+
const checkpointStats = board.checkpointDiagnostics ? directCheckpointStats(jit) : void 0;
|
|
2869
|
+
const frame = {
|
|
2870
|
+
states: layout.stateValues,
|
|
2871
|
+
memories: [...layout.memoryBanks],
|
|
2872
|
+
memoryViews: layout.memoryViews,
|
|
2873
|
+
pins: pinObjects,
|
|
2874
|
+
resistors: [],
|
|
2875
|
+
locals: new Array(session.localCount),
|
|
2876
|
+
stateDirty: new Int32Array(Math.ceil(layout.stateNames.length / 32)),
|
|
2877
|
+
memoryDirty: new Int32Array(Math.ceil(layout.memoryNames.length / 32)),
|
|
2878
|
+
time: board.time,
|
|
2879
|
+
due: board.time,
|
|
2880
|
+
resume: 0,
|
|
2881
|
+
stopAfterInstruction: false,
|
|
2882
|
+
boundarySettled: false
|
|
2883
|
+
};
|
|
2884
|
+
frame.writePinIntent = (pinIndex, value) => {
|
|
2885
|
+
if (!Object.hasOwn(instance, "writeIntent")) {
|
|
2886
|
+
const pin = pinObjects[pinIndex];
|
|
2887
|
+
if (pin.intent === value) return;
|
|
2888
|
+
pin.intent = value;
|
|
2889
|
+
instance.board.markElectricalDirty(pinElectricalSlots[pinIndex]);
|
|
2890
|
+
frame.publishPending = true;
|
|
2891
|
+
return;
|
|
2892
|
+
}
|
|
2893
|
+
board.time = frame.time;
|
|
2894
|
+
if (instance.writeIntent(pinNames[pinIndex], value)) frame.publishPending = true;
|
|
2895
|
+
};
|
|
2896
|
+
frame.host = directHost(board, instance, frame);
|
|
2897
|
+
if (jit.metadata === void 0) {
|
|
2898
|
+
frame.publishCheckpoint = () => {
|
|
2899
|
+
if (checkpointStats !== void 0) checkpointStats.publications++;
|
|
2900
|
+
flushDirect(board, instance, layout, frame);
|
|
2901
|
+
checkpointBoundary(board, checkpointStats, "checkpointPublish");
|
|
2902
|
+
if (jit.directStale || jit.direct?.session !== session || !board.canContinueDirect(jit, frame.due)) {
|
|
2903
|
+
frame.boundarySettled = true;
|
|
2904
|
+
if (checkpointStats !== void 0) checkpointStats.exits++;
|
|
2905
|
+
return false;
|
|
2906
|
+
}
|
|
2907
|
+
board.current = instance;
|
|
2908
|
+
if (checkpointStats !== void 0) checkpointStats.continues++;
|
|
2909
|
+
return true;
|
|
2910
|
+
};
|
|
2911
|
+
if (jit.options.beforeInstruction === void 0) {
|
|
2912
|
+
const pcIndex = layout.states.get(jit.options.pc);
|
|
2913
|
+
frame.finishCheckpoint = (deadline, emptySuffix) => {
|
|
2914
|
+
if (checkpointStats !== void 0) {
|
|
2915
|
+
checkpointStats.finishes++;
|
|
2916
|
+
if (emptySuffix) checkpointStats.emptySuffixes++;
|
|
2917
|
+
}
|
|
2918
|
+
if (emptySuffix) board.time = frame.time;
|
|
2919
|
+
else flushDirect(board, instance, layout, frame);
|
|
2920
|
+
checkpointBoundary(board, checkpointStats, "checkpointFinish");
|
|
2921
|
+
const nextPc = frame.states[pcIndex];
|
|
2922
|
+
if (jit.directStale || jit.direct?.session !== session || !session.has(nextPc) || jit.directSkip === nextPc || frame.time >= deadline || board.events.time < deadline || !emptySuffix && !board.canContinueDirect(jit, frame.time)) {
|
|
2923
|
+
frame.boundarySettled = true;
|
|
2924
|
+
if (checkpointStats !== void 0) checkpointStats.exits++;
|
|
2925
|
+
return false;
|
|
2926
|
+
}
|
|
2927
|
+
if (jit.directObserverRevision !== instance.observerRevision) {
|
|
2928
|
+
if (refreshDirectGuards(jit, void 0, true)) {
|
|
2929
|
+
frame.boundarySettled = true;
|
|
2930
|
+
if (checkpointStats !== void 0) checkpointStats.exits++;
|
|
2931
|
+
return false;
|
|
2932
|
+
}
|
|
3258
2933
|
}
|
|
2934
|
+
frame.stopAfterInstruction = false;
|
|
2935
|
+
board.current = instance;
|
|
2936
|
+
if (checkpointStats !== void 0) checkpointStats.continues++;
|
|
2937
|
+
return true;
|
|
3259
2938
|
};
|
|
3260
2939
|
}
|
|
3261
|
-
const jit = {
|
|
3262
|
-
instance: this,
|
|
3263
|
-
model,
|
|
3264
|
-
host,
|
|
3265
|
-
...functions === void 0 ? {} : { functions },
|
|
3266
|
-
options: Object.freeze({
|
|
3267
|
-
...options,
|
|
3268
|
-
...options.codeRegions === void 0 ? {} : { codeRegions: codeRegions.additional },
|
|
3269
|
-
...options.beforeInstruction ? { beforeInstruction: Object.freeze({ ...options.beforeInstruction }) } : {}
|
|
3270
|
-
}),
|
|
3271
|
-
codeRegions: codeRegions.all,
|
|
3272
|
-
cache: /* @__PURE__ */ new Map(),
|
|
3273
|
-
cacheMemories: /* @__PURE__ */ new Set(),
|
|
3274
|
-
metadata,
|
|
3275
|
-
polling,
|
|
3276
|
-
delays: options.delays ?? /* @__PURE__ */ new Map(),
|
|
3277
|
-
revision: 0,
|
|
3278
|
-
directRevision: -1,
|
|
3279
|
-
directObserverRevision: -1,
|
|
3280
|
-
directStateGuards: /* @__PURE__ */ new Set(),
|
|
3281
|
-
directMemoryGuards: /* @__PURE__ */ new Set(),
|
|
3282
|
-
directHookReads: /* @__PURE__ */ new Set(),
|
|
3283
|
-
directPrograms: /* @__PURE__ */ new Map(),
|
|
3284
|
-
directProgramRevision: -1,
|
|
3285
|
-
direct: void 0,
|
|
3286
|
-
directSkip: void 0,
|
|
3287
|
-
directMisses: /* @__PURE__ */ new Map(),
|
|
3288
|
-
pollReads: void 0,
|
|
3289
|
-
currentPc: void 0,
|
|
3290
|
-
automaticPolling: false,
|
|
3291
|
-
pollingWait: false,
|
|
3292
|
-
pollingPeriod: 0,
|
|
3293
|
-
pollingOrigin: 0,
|
|
3294
|
-
pollingToken: 0,
|
|
3295
|
-
dependencyDirty: true,
|
|
3296
|
-
waiterSlots: void 0,
|
|
3297
|
-
active: true,
|
|
3298
|
-
stopped: false,
|
|
3299
|
-
queued: false,
|
|
3300
|
-
execution: void 0
|
|
3301
|
-
};
|
|
3302
|
-
this.jits.push(jit);
|
|
3303
|
-
this.board.jits.push(jit);
|
|
3304
|
-
for (const region of jit.codeRegions) this.codeMemories.add(region.memory);
|
|
3305
|
-
const disposable = { dispose: () => {
|
|
3306
|
-
if (!jit.active) return;
|
|
3307
|
-
jit.active = false;
|
|
3308
|
-
jit.execution = void 0;
|
|
3309
|
-
delete jit.predicate;
|
|
3310
|
-
jit.pollingToken++;
|
|
3311
|
-
this.board.unparkWaiter(jit);
|
|
3312
|
-
jit.cache.clear();
|
|
3313
|
-
jit.cacheMemories.clear();
|
|
3314
|
-
jit.directPrograms.clear();
|
|
3315
|
-
jit.direct = void 0;
|
|
3316
|
-
jit.directMisses.clear();
|
|
3317
|
-
jit.pollReads = void 0;
|
|
3318
|
-
} };
|
|
3319
|
-
this.resources.push(disposable);
|
|
3320
|
-
return disposable;
|
|
3321
2940
|
}
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
|
|
3330
|
-
|
|
2941
|
+
frame.blockedStates = barrier.stateMask;
|
|
2942
|
+
frame.blockedMemories = barrier.memoryMask;
|
|
2943
|
+
return frame;
|
|
2944
|
+
}
|
|
2945
|
+
/**
|
|
2946
|
+
* Run the fixed dispatcher. `untranslated`/`pending` return to `runJit` as an assembly
|
|
2947
|
+
* request; `fallback` means the compiler rejected a PC and the interpreter must take it.
|
|
2948
|
+
*/
|
|
2949
|
+
function runDirect(board, jit, hookReads) {
|
|
2950
|
+
const instance = jit.instance;
|
|
2951
|
+
let pending = Boolean(jit.direct?.frame.resume);
|
|
2952
|
+
if (!pending) {
|
|
2953
|
+
if (jit.directStale) {
|
|
2954
|
+
releaseDirectSession(jit);
|
|
2955
|
+
jit.directStale = false;
|
|
2956
|
+
}
|
|
2957
|
+
const pc = finite(instance.states.get(jit.options.pc), "pc");
|
|
2958
|
+
const region = codeRegionFor(jit, pc);
|
|
2959
|
+
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 "declined";
|
|
2960
|
+
refreshDirectGuards(jit, hookReads);
|
|
2961
|
+
if (jit.directSkip === pc) return "declined";
|
|
2962
|
+
if (jit.direct === void 0 && ensureDirectSession(board, jit) === void 0) return "declined";
|
|
2963
|
+
}
|
|
2964
|
+
const direct = jit.direct;
|
|
2965
|
+
const frame = direct.frame;
|
|
2966
|
+
const layout = jit.directLayout;
|
|
2967
|
+
for (;;) {
|
|
2968
|
+
if (pending) frame.stopAfterInstruction = true;
|
|
2969
|
+
frame.time = board.time;
|
|
2970
|
+
const start = board.time;
|
|
2971
|
+
let reason;
|
|
2972
|
+
let wrote;
|
|
2973
|
+
const stats = directStats(jit);
|
|
2974
|
+
stats.directCalls++;
|
|
2975
|
+
try {
|
|
2976
|
+
reason = direct.session.run(frame, Math.min(board.executionEnd, board.events.time));
|
|
2977
|
+
} finally {
|
|
2978
|
+
wrote = flushDirect(board, instance, layout, frame);
|
|
2979
|
+
}
|
|
2980
|
+
const progressed = wrote || board.time !== start;
|
|
2981
|
+
if (frame.boundarySettled) frame.boundarySettled = false;
|
|
2982
|
+
else if (reason === "fallback" || reason === "untranslated" || reason === "pending" ? progressed : true) board.boundary("runDirect");
|
|
2983
|
+
if (reason === "untranslated" || reason === "pending") {
|
|
2984
|
+
stats.directDispatchMisses++;
|
|
2985
|
+
if (jit.directStale && frame.resume === 0) {
|
|
2986
|
+
releaseDirectSession(jit);
|
|
2987
|
+
jit.directStale = false;
|
|
2988
|
+
}
|
|
2989
|
+
return progressed ? "assembly-progressed" : "assembly";
|
|
2990
|
+
}
|
|
2991
|
+
if (reason === "fallback") {
|
|
2992
|
+
const nextPc = finite(instance.states.get(jit.options.pc), "pc");
|
|
2993
|
+
if (!(effectivePolling(jit).has(nextPc) || jit.cache.get(nextPc)?.polling)) {
|
|
2994
|
+
stats.directFallbacks++;
|
|
2995
|
+
jit.directSkip = nextPc;
|
|
2996
|
+
}
|
|
2997
|
+
if (!progressed && board.time < board.executionEnd) return "fallback";
|
|
2998
|
+
} else if (reason === "stopped") {
|
|
2999
|
+
stats.directStopped++;
|
|
3000
|
+
jit.stopped = true;
|
|
3001
|
+
if (jit.directStale && frame.resume === 0) {
|
|
3002
|
+
releaseDirectSession(jit);
|
|
3003
|
+
jit.directStale = false;
|
|
3004
|
+
}
|
|
3005
|
+
return "ran";
|
|
3006
|
+
} else if (reason === "invalidated") {
|
|
3007
|
+
frame.stopAfterInstruction = false;
|
|
3008
|
+
if (jit.directStale && frame.resume === 0) {
|
|
3009
|
+
releaseDirectSession(jit);
|
|
3010
|
+
jit.directStale = false;
|
|
3011
|
+
}
|
|
3012
|
+
} else stats.directDeadlines++;
|
|
3013
|
+
if (jit.direct === void 0) {
|
|
3014
|
+
board.queueJit(jit);
|
|
3015
|
+
return "ran";
|
|
3016
|
+
}
|
|
3017
|
+
if (frame.resume) {
|
|
3018
|
+
const wake = Math.max(frame.due, board.time);
|
|
3019
|
+
if (jit.metadata === void 0 && !jit.directStale && board.canContinueDirect(jit, wake)) {
|
|
3020
|
+
board.time = wake;
|
|
3021
|
+
board.current = instance;
|
|
3022
|
+
pending = true;
|
|
3023
|
+
continue;
|
|
3024
|
+
}
|
|
3025
|
+
if (wake > board.time) {
|
|
3026
|
+
board.events.push(wake, () => board.queueJit(jit));
|
|
3027
|
+
return "ran";
|
|
3028
|
+
}
|
|
3331
3029
|
}
|
|
3030
|
+
board.queueJit(jit);
|
|
3031
|
+
return "ran";
|
|
3332
3032
|
}
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3033
|
+
}
|
|
3034
|
+
/** A PC is ready for direct execution when its translation is not polling or secondary. */
|
|
3035
|
+
function directPcAdmitted(jit, pc) {
|
|
3036
|
+
const region = codeRegionFor(jit, pc);
|
|
3037
|
+
if (region !== void 0 && region.memory !== jit.options.code) return false;
|
|
3038
|
+
if (effectivePolling(jit).has(pc)) return false;
|
|
3039
|
+
return jit.cache.get(pc)?.polling !== true;
|
|
3040
|
+
}
|
|
3041
|
+
/**
|
|
3042
|
+
* Satisfy one assembly request without replaying the boundary that asked for it. A missing
|
|
3043
|
+
* cache entry uses the same translation path as the interpreter; an existing entry is simply
|
|
3044
|
+
* added because it may have been translated while an old suffix was still suspended.
|
|
3045
|
+
*/
|
|
3046
|
+
function assembleDirect(board, jit, pc) {
|
|
3047
|
+
if (jit.metadata !== void 0) ensureMetadataForPc(board, jit, pc);
|
|
3048
|
+
const direct = jit.direct;
|
|
3049
|
+
if (direct === void 0 || jit.directStale || direct.frame.resume !== 0) return false;
|
|
3050
|
+
if (!directPcAdmitted(jit, pc)) return false;
|
|
3051
|
+
let instruction = jit.cache.get(pc);
|
|
3052
|
+
if (instruction === void 0) {
|
|
3053
|
+
translateBlock(board, jit, pc);
|
|
3054
|
+
instruction = jit.cache.get(pc);
|
|
3055
|
+
return instruction !== void 0;
|
|
3056
|
+
}
|
|
3057
|
+
if (!direct.session.has(pc)) addDirectInstructions(jit, [instruction]);
|
|
3058
|
+
return true;
|
|
3059
|
+
}
|
|
3060
|
+
/**
|
|
3061
|
+
* Handle `untranslated` from the dispatcher. Hooks and polling `consume` already ran for this
|
|
3062
|
+
* boundary, so they are never repeated here. At most one retry keeps a rejected body from
|
|
3063
|
+
* looping; a progressed prefix that still cannot continue direct yields to the scheduler so
|
|
3064
|
+
* the next instruction gets its own boundary.
|
|
3065
|
+
*/
|
|
3066
|
+
function retryDirectAfterAssembly(board, jit, progressed) {
|
|
3067
|
+
if (progressed) {
|
|
3068
|
+
board.queueJit(jit);
|
|
3069
|
+
return true;
|
|
3346
3070
|
}
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3071
|
+
if (jit.direct === void 0 || jit.directStale) return false;
|
|
3072
|
+
if (!assembleDirect(board, jit, finite(jit.instance.states.get(jit.options.pc), "pc"))) return false;
|
|
3073
|
+
const retried = runDirect(board, jit);
|
|
3074
|
+
if (retried === "ran") return true;
|
|
3075
|
+
if (retried === "assembly-progressed") {
|
|
3076
|
+
board.queueJit(jit);
|
|
3077
|
+
return true;
|
|
3078
|
+
}
|
|
3079
|
+
return false;
|
|
3354
3080
|
}
|
|
3355
|
-
function
|
|
3356
|
-
const
|
|
3357
|
-
|
|
3358
|
-
|
|
3081
|
+
function pollingDependencies(jit, dependencies, pc) {
|
|
3082
|
+
const result = new Map(dependencies);
|
|
3083
|
+
const instance = jit.instance;
|
|
3084
|
+
const pcSlot = instance.stateResources.get(jit.options.pc);
|
|
3085
|
+
result.set(pcSlot, pcSlot.version);
|
|
3086
|
+
const memory = (pc === void 0 ? void 0 : codeRegionFor(jit, pc))?.memory ?? jit.options.code;
|
|
3087
|
+
const slot = instance.memoryResources.get(memory);
|
|
3088
|
+
result.set(slot, slot.version);
|
|
3089
|
+
return result;
|
|
3359
3090
|
}
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3363
|
-
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
|
|
3384
|
-
|
|
3385
|
-
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3091
|
+
/** Park a polling JIT and, when requested, schedule the next device time event. */
|
|
3092
|
+
function waitForPolling(board, jit, dependencies, duration, advance) {
|
|
3093
|
+
jit.dependencies = dependencies;
|
|
3094
|
+
jit.pollingWait = true;
|
|
3095
|
+
jit.pollingPeriod = 0;
|
|
3096
|
+
board.parkWaiter(jit);
|
|
3097
|
+
const token = ++jit.pollingToken;
|
|
3098
|
+
board.boundary("polling");
|
|
3099
|
+
if (duration === 0 || advance === void 0) return;
|
|
3100
|
+
const due = time(board.time + duration);
|
|
3101
|
+
board.events.push(due, () => {
|
|
3102
|
+
if (token !== jit.pollingToken || !jit.active || jit.stopped || !board.waitingJits.has(jit)) return;
|
|
3103
|
+
const pending = jit.dependencies;
|
|
3104
|
+
delete jit.dependencies;
|
|
3105
|
+
board.unparkWaiter(jit);
|
|
3106
|
+
jit.pollingToken++;
|
|
3107
|
+
board.current = jit.instance;
|
|
3108
|
+
if (!jit.instance.dependenciesCurrent(pending)) {
|
|
3109
|
+
board.queueJit(jit);
|
|
3110
|
+
return;
|
|
3111
|
+
}
|
|
3112
|
+
jit.instance.syncCall(advance, [duration]);
|
|
3113
|
+
board.boundary("polling");
|
|
3114
|
+
board.queueJit(jit);
|
|
3115
|
+
});
|
|
3116
|
+
}
|
|
3117
|
+
function jitSignal(board, jit, signal) {
|
|
3118
|
+
if (signal.kind === "elapse") {
|
|
3119
|
+
const duration = time(signal.duration);
|
|
3120
|
+
if (duration === 0) return false;
|
|
3121
|
+
const due = time(board.time + duration);
|
|
3122
|
+
board.events.push(due, () => board.queueJit(jit));
|
|
3123
|
+
board.boundary("jitSignal");
|
|
3124
|
+
return true;
|
|
3125
|
+
}
|
|
3126
|
+
if (signal.kind === "waitUntil") {
|
|
3127
|
+
const checked = jit.instance.evaluate(() => syncJitCall(jit, signal.predicate));
|
|
3128
|
+
if (checked.value === true) return false;
|
|
3129
|
+
jit.predicate = signal.predicate;
|
|
3130
|
+
jit.pollingWait = false;
|
|
3131
|
+
jit.dependencies = checked.dependencies;
|
|
3132
|
+
board.parkWaiter(jit);
|
|
3133
|
+
board.boundary("jitSignal");
|
|
3134
|
+
return true;
|
|
3135
|
+
}
|
|
3136
|
+
if (signal.kind === "stop") {
|
|
3137
|
+
jit.stopped = true;
|
|
3138
|
+
jit.execution = void 0;
|
|
3139
|
+
board.boundary("jitSignal");
|
|
3140
|
+
return true;
|
|
3141
|
+
}
|
|
3142
|
+
if (signal.kind === "redirect") {
|
|
3143
|
+
finite(signal.pc, "redirect target");
|
|
3144
|
+
const duration = time(signal.delay);
|
|
3145
|
+
jit.instance.host.write(jit.options.pc, signal.pc);
|
|
3146
|
+
jit.execution = void 0;
|
|
3147
|
+
const polling = jit.automaticPolling && duration > 0 && signal.pc === jit.currentPc && jit.pollReads;
|
|
3148
|
+
const dependencies = polling ? pollingDependencies(jit, polling, jit.currentPc) : void 0;
|
|
3149
|
+
jit.automaticPolling = false;
|
|
3150
|
+
jit.pollReads = void 0;
|
|
3151
|
+
board.boundary("jitSignal");
|
|
3152
|
+
if (duration === 0) board.queueJit(jit);
|
|
3153
|
+
else board.events.push(time(board.time + duration), () => {
|
|
3154
|
+
if (!jit.active || jit.stopped) return;
|
|
3155
|
+
if (dependencies && jit.instance.dependenciesCurrent(dependencies)) {
|
|
3156
|
+
jit.dependencies = dependencies;
|
|
3157
|
+
jit.pollingWait = true;
|
|
3158
|
+
jit.pollingPeriod = duration;
|
|
3159
|
+
jit.pollingOrigin = board.time;
|
|
3160
|
+
board.parkWaiter(jit);
|
|
3161
|
+
} else board.queueJit(jit);
|
|
3162
|
+
});
|
|
3163
|
+
return true;
|
|
3164
|
+
}
|
|
3165
|
+
throw new TypeError("Invalid JIT suspension");
|
|
3166
|
+
}
|
|
3167
|
+
/**
|
|
3168
|
+
* Translate one PC, atomically publish the block's cache entries, and commit its admitted
|
|
3169
|
+
* entries to the active session. Reused by both the interpreter path and assembly retries.
|
|
3170
|
+
*/
|
|
3171
|
+
function translateBlock(board, jit, pc) {
|
|
3172
|
+
const prior = board.phase;
|
|
3173
|
+
board.phase = "translate";
|
|
3174
|
+
let valid = true;
|
|
3175
|
+
let marking = false;
|
|
3176
|
+
try {
|
|
3177
|
+
const codeRegion = codeRegionFor(jit, pc);
|
|
3178
|
+
const codeMemory = codeRegion?.memory ?? jit.options.code;
|
|
3179
|
+
const memory = jit.instance.memory(codeMemory);
|
|
3180
|
+
const bytes = codeRegion === void 0 || codeRegion.size >= memory.length ? memory : memory.subarray(0, codeRegion.size);
|
|
3181
|
+
const code = bytesView(bytes, () => {
|
|
3182
|
+
if (!valid) throw board.error("OBJECT_DISPOSED", "Translation view expired", jit.instance);
|
|
3183
|
+
});
|
|
3184
|
+
const shareMarks = jit.options.shareDirectBodies === true ? [] : void 0;
|
|
3185
|
+
marking = shareMarks !== void 0;
|
|
3186
|
+
const result = jit.options.translate(Object.freeze({
|
|
3187
|
+
pc,
|
|
3188
|
+
code,
|
|
3189
|
+
codeBase: codeRegion?.base ?? 0,
|
|
3190
|
+
codeVersion: jit.instance.codeVersion(codeMemory),
|
|
3191
|
+
...shareMarks === void 0 ? {} : { markDirectBodyShareable: (markPc, byteLength) => {
|
|
3192
|
+
if (!marking) return;
|
|
3193
|
+
directStats(jit).directShareCallbacks++;
|
|
3194
|
+
shareMarks.push({
|
|
3195
|
+
pc: markPc,
|
|
3196
|
+
byteLength
|
|
3197
|
+
});
|
|
3198
|
+
} }
|
|
3199
|
+
}), createJitBlockBuilder({ device: jit.model }));
|
|
3200
|
+
acceptSync(result);
|
|
3201
|
+
marking = false;
|
|
3202
|
+
const block = validateBoundBlockIR(result, jit.model);
|
|
3203
|
+
if (block.instructions[0]?.pc !== pc) throw new TypeError("Translated first pc does not match");
|
|
3204
|
+
const instructions = block.instructions.map((instruction) => {
|
|
3205
|
+
const region = codeRegionFor(jit, instruction.pc);
|
|
3206
|
+
return {
|
|
3207
|
+
pc: instruction.pc,
|
|
3208
|
+
nextPc: instruction.nextPc,
|
|
3209
|
+
body: instruction.body,
|
|
3210
|
+
polling: isPollingInstruction(instruction.body, instruction.pc, jit.options.pc),
|
|
3211
|
+
execute: body(instruction.body),
|
|
3212
|
+
codeRegion: region,
|
|
3213
|
+
codeVersion: jit.instance.codeVersion(region?.memory ?? jit.options.code)
|
|
3214
|
+
};
|
|
3215
|
+
});
|
|
3216
|
+
if (shareMarks !== void 0) applyDirectShareMarks(jit, codeRegion, code.length, instructions, shareMarks);
|
|
3217
|
+
const replaced = instructions.some((entry) => jit.cache.has(entry.pc));
|
|
3218
|
+
for (let index = instructions.length - 1; index >= 0; index--) {
|
|
3219
|
+
const entry = instructions[index];
|
|
3220
|
+
jit.cache.set(entry.pc, entry);
|
|
3221
|
+
jit.cacheMemories.add(entry.codeRegion?.memory ?? jit.options.code);
|
|
3222
|
+
}
|
|
3223
|
+
if (instructions.some((entry) => entry.codeRegion === void 0 || entry.codeRegion.memory === jit.options.code)) jit.revision++;
|
|
3224
|
+
jit.directSkip = void 0;
|
|
3225
|
+
commitDirectTranslation(jit, instructions, replaced);
|
|
3226
|
+
return instructions;
|
|
3227
|
+
} catch (cause) {
|
|
3228
|
+
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
3229
|
+
} finally {
|
|
3230
|
+
valid = false;
|
|
3231
|
+
marking = false;
|
|
3232
|
+
board.phase = prior;
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
function runJit(board, jit) {
|
|
3236
|
+
jit.queued = false;
|
|
3237
|
+
if (!jit.active || jit.stopped) return;
|
|
3238
|
+
board.current = jit.instance;
|
|
3239
|
+
if (jit.metadata !== void 0) ensureMetadataForPc(board, jit, finite(jit.instance.states.get(jit.options.pc), "pc"));
|
|
3240
|
+
if (jit.direct?.frame.resume) {
|
|
3241
|
+
const outcome = runDirect(board, jit);
|
|
3242
|
+
if (outcome === "assembly" || outcome === "assembly-progressed") retryDirectAfterAssembly(board, jit, true);
|
|
3243
|
+
return;
|
|
3244
|
+
}
|
|
3245
|
+
if (!jit.execution) {
|
|
3246
|
+
jit.automaticPolling = false;
|
|
3247
|
+
jit.pollReads = void 0;
|
|
3248
|
+
let hookReads;
|
|
3249
|
+
let hookPending = false;
|
|
3250
|
+
const hook = jit.options.beforeInstruction;
|
|
3251
|
+
if (hook) {
|
|
3252
|
+
const kind = jit.instance.declarations.get(hook.when).kind;
|
|
3253
|
+
const priorPc = jit.instance.states.get(jit.options.pc);
|
|
3254
|
+
const checked = board.execution === "throughput" && (board.jits.length === 1 || effectivePolling(jit).has(priorPc)) ? jit.instance.evaluate(() => jit.instance.read(kind, hook.when)) : void 0;
|
|
3255
|
+
const handled = (checked ? checked.value : jit.instance.read(kind, hook.when)) === true;
|
|
3256
|
+
if (handled) {
|
|
3257
|
+
const execution = jit.instance.invoke(hook.call);
|
|
3258
|
+
const step = execution.next();
|
|
3259
|
+
if (!step.done) {
|
|
3260
|
+
jit.execution = execution;
|
|
3261
|
+
if (jitSignal(board, jit, step.value)) return;
|
|
3262
|
+
throw new TypeError("Invalid hook suspension");
|
|
3263
|
+
}
|
|
3410
3264
|
}
|
|
3265
|
+
hookReads = checked?.dependencies;
|
|
3266
|
+
hookPending = handled;
|
|
3411
3267
|
}
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3268
|
+
const pc = finite(jit.instance.states.get(jit.options.pc), "pc");
|
|
3269
|
+
if (jit.metadata !== void 0 && hook !== void 0) ensureMetadataForPc(board, jit, pc);
|
|
3270
|
+
if (board.execution === "throughput") {
|
|
3271
|
+
const declaration = effectivePolling(jit).get(pc);
|
|
3272
|
+
if (declaration) {
|
|
3273
|
+
const checked = jit.instance.evaluate(() => jit.instance.syncCall(declaration.predicate));
|
|
3274
|
+
if (checked.value !== true) {
|
|
3275
|
+
const dependencies = new Map([...hookReads ?? [], ...checked.dependencies]);
|
|
3276
|
+
let duration = 0;
|
|
3277
|
+
const wait = declaration.wait;
|
|
3278
|
+
if (wait) duration = time(jit.instance.evaluate(() => jit.instance.syncCall(wait.delay), dependencies).value);
|
|
3279
|
+
waitForPolling(board, jit, pollingDependencies(jit, dependencies, pc), duration, wait?.advance);
|
|
3280
|
+
return;
|
|
3281
|
+
}
|
|
3282
|
+
if (declaration.consume) jit.instance.syncCall(declaration.consume);
|
|
3283
|
+
}
|
|
3284
|
+
if (!hookPending && board.jits.length === 1) {
|
|
3285
|
+
const outcome = runDirect(board, jit, hookReads);
|
|
3286
|
+
if (outcome === "ran") return;
|
|
3287
|
+
if (outcome === "assembly" || outcome === "assembly-progressed") {
|
|
3288
|
+
if (retryDirectAfterAssembly(board, jit, outcome === "assembly-progressed")) return;
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
let instruction = jit.cache.get(pc);
|
|
3293
|
+
if (!instruction) instruction = translateBlock(board, jit, pc)[0];
|
|
3294
|
+
jit.currentPc = pc;
|
|
3295
|
+
if (board.execution === "throughput" && board.jits.length === 1) directStats(jit).directGenericInstructions++;
|
|
3296
|
+
jit.automaticPolling = board.execution === "throughput" && board.jits.length === 1 && instruction.polling && !hookPending && (!hook || hookReads !== void 0);
|
|
3297
|
+
jit.pollReads = hookReads;
|
|
3298
|
+
jit.instance.host.write(jit.options.pc, instruction.nextPc);
|
|
3299
|
+
jit.execution = instruction.execute(jit.host);
|
|
3300
|
+
}
|
|
3301
|
+
for (;;) {
|
|
3302
|
+
const step = jit.automaticPolling ? (() => {
|
|
3303
|
+
const checked = jit.instance.evaluate(() => jit.execution.next());
|
|
3304
|
+
jit.pollReads = new Map([...jit.pollReads ?? [], ...checked.dependencies]);
|
|
3305
|
+
return checked.value;
|
|
3306
|
+
})() : jit.execution.next();
|
|
3307
|
+
if (step.done) {
|
|
3308
|
+
jit.execution = void 0;
|
|
3309
|
+
board.boundary("runJit");
|
|
3310
|
+
board.queueJit(jit);
|
|
3311
|
+
return;
|
|
3312
|
+
}
|
|
3313
|
+
if (jitSignal(board, jit, step.value)) return;
|
|
3314
|
+
}
|
|
3419
3315
|
}
|
|
3420
3316
|
//#endregion
|
|
3421
|
-
//#region src/runtime/
|
|
3422
|
-
|
|
3423
|
-
var
|
|
3424
|
-
function
|
|
3425
|
-
|
|
3426
|
-
if (
|
|
3427
|
-
|
|
3317
|
+
//#region src/runtime/instance.ts
|
|
3318
|
+
var EMPTY_MESSAGES = Object.freeze([]);
|
|
3319
|
+
var jitModels = /* @__PURE__ */ new WeakMap();
|
|
3320
|
+
/** Retain the binding identity when adapters reuse the same immutable function table. */
|
|
3321
|
+
function bindJitFunctions(base, functions) {
|
|
3322
|
+
if (functions.length === 0) return base;
|
|
3323
|
+
let models = jitModels.get(base);
|
|
3324
|
+
const cached = models?.get(functions);
|
|
3325
|
+
if (cached !== void 0) return cached;
|
|
3326
|
+
const model = validateDeviceIR({
|
|
3327
|
+
...base,
|
|
3328
|
+
declarations: base.declarations.concat(functions)
|
|
3329
|
+
});
|
|
3330
|
+
for (const declaration of model.declarations.slice(base.declarations.length)) if (declaration.kind !== "function") throw new TypeError("Expected JIT function declaration");
|
|
3331
|
+
if (models === void 0) {
|
|
3332
|
+
models = /* @__PURE__ */ new WeakMap();
|
|
3333
|
+
jitModels.set(base, models);
|
|
3334
|
+
}
|
|
3335
|
+
models.set(functions, model);
|
|
3336
|
+
return model;
|
|
3337
|
+
}
|
|
3338
|
+
/** Membership test without allocating: every key of `next` is already in `previous`. */
|
|
3339
|
+
function hasEvery(previous, next) {
|
|
3340
|
+
for (const slot of next.keys()) if (!previous.has(slot)) return false;
|
|
3341
|
+
return true;
|
|
3342
|
+
}
|
|
3343
|
+
function createContext(owner, board) {
|
|
3344
|
+
return Object.freeze({
|
|
3345
|
+
get props() {
|
|
3346
|
+
board.assertReadAllowed(owner);
|
|
3347
|
+
return Object.freeze(Object.fromEntries([...owner.props].map(([key, value]) => [key, externalValue(value)])));
|
|
3348
|
+
},
|
|
3349
|
+
get inputs() {
|
|
3350
|
+
owner.assertCallAllowed();
|
|
3351
|
+
return owner.inputs();
|
|
3352
|
+
},
|
|
3353
|
+
get memory() {
|
|
3354
|
+
board.assertReadAllowed(owner);
|
|
3355
|
+
return owner.publicMemory();
|
|
3356
|
+
},
|
|
3357
|
+
topics: board.topicBridge(owner),
|
|
3358
|
+
reportError: (error) => board.report(owner, error),
|
|
3359
|
+
attachJit: (options) => owner.attach(options),
|
|
3360
|
+
loadBytes: (name, bytes, ...offset) => owner.loadBytes(name, bytes, offset.length ? offset[0] : 0)
|
|
3361
|
+
});
|
|
3428
3362
|
}
|
|
3429
3363
|
/**
|
|
3430
|
-
*
|
|
3364
|
+
* State map whose scalar direct-bank writes are synced lazily on read.
|
|
3431
3365
|
*
|
|
3432
|
-
*
|
|
3433
|
-
*
|
|
3434
|
-
*
|
|
3435
|
-
*
|
|
3436
|
-
*
|
|
3366
|
+
* Direct JIT writes update `stateValues` and invalidate `stateSlots` at every boundary,
|
|
3367
|
+
* while no observer or host reads `instance.states` until a reaction, observation,
|
|
3368
|
+
* fallback or external query accesses it. Deferring the string-keyed `Map.set` calls
|
|
3369
|
+
* until an actual read removes redundant map updates across resident GPIO edges while
|
|
3370
|
+
* keeping `instance.states` an exact `Map<string, unknown>` at every read site.
|
|
3437
3371
|
*/
|
|
3438
|
-
var
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
3450
|
-
|
|
3451
|
-
|
|
3452
|
-
|
|
3453
|
-
|
|
3454
|
-
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
|
|
3372
|
+
var StateMirrorMap = class extends Map {
|
|
3373
|
+
names = [];
|
|
3374
|
+
bankValues = [];
|
|
3375
|
+
dirty = /* @__PURE__ */ new Int32Array(0);
|
|
3376
|
+
pending = false;
|
|
3377
|
+
bindDirectBank(names, values) {
|
|
3378
|
+
this.names = names;
|
|
3379
|
+
this.bankValues = values;
|
|
3380
|
+
this.dirty = new Int32Array(Math.ceil(names.length / 32));
|
|
3381
|
+
}
|
|
3382
|
+
markDirectDirty(word, bits) {
|
|
3383
|
+
this.dirty[word] |= bits;
|
|
3384
|
+
this.pending = true;
|
|
3385
|
+
}
|
|
3386
|
+
flush() {
|
|
3387
|
+
if (!this.pending) return;
|
|
3388
|
+
this.pending = false;
|
|
3389
|
+
const { names, bankValues, dirty } = this;
|
|
3390
|
+
for (let word = 0; word < dirty.length; word++) {
|
|
3391
|
+
let bits = dirty[word];
|
|
3392
|
+
if (bits === 0) continue;
|
|
3393
|
+
dirty[word] = 0;
|
|
3394
|
+
const base = word << 5;
|
|
3395
|
+
while (bits !== 0) {
|
|
3396
|
+
const index = base + 31 - Math.clz32(bits & -bits);
|
|
3397
|
+
super.set(names[index], bankValues[index]);
|
|
3398
|
+
bits &= bits - 1;
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
}
|
|
3402
|
+
get(key) {
|
|
3403
|
+
if (this.pending) this.flush();
|
|
3404
|
+
return super.get(key);
|
|
3405
|
+
}
|
|
3406
|
+
set(key, value) {
|
|
3407
|
+
if (this.pending) this.flush();
|
|
3408
|
+
return super.set(key, value);
|
|
3409
|
+
}
|
|
3410
|
+
entries() {
|
|
3411
|
+
if (this.pending) this.flush();
|
|
3412
|
+
return super.entries();
|
|
3413
|
+
}
|
|
3414
|
+
values() {
|
|
3415
|
+
if (this.pending) this.flush();
|
|
3416
|
+
return super.values();
|
|
3417
|
+
}
|
|
3418
|
+
forEach(callbackfn, thisArg) {
|
|
3419
|
+
if (this.pending) this.flush();
|
|
3420
|
+
super.forEach(callbackfn, thisArg);
|
|
3421
|
+
}
|
|
3422
|
+
[Symbol.iterator]() {
|
|
3423
|
+
if (this.pending) this.flush();
|
|
3424
|
+
return super[Symbol.iterator]();
|
|
3425
|
+
}
|
|
3426
|
+
};
|
|
3427
|
+
var Instance = class {
|
|
3428
|
+
board;
|
|
3429
|
+
entry;
|
|
3430
|
+
id;
|
|
3431
|
+
declarations = /* @__PURE__ */ new Map();
|
|
3432
|
+
props = /* @__PURE__ */ new Map();
|
|
3433
|
+
constants = /* @__PURE__ */ new Map();
|
|
3434
|
+
states = new StateMirrorMap();
|
|
3435
|
+
directBank;
|
|
3436
|
+
memories = /* @__PURE__ */ new Map();
|
|
3437
|
+
memoryViews = /* @__PURE__ */ new Map();
|
|
3438
|
+
memorySizes = /* @__PURE__ */ new Map();
|
|
3439
|
+
pins = /* @__PURE__ */ new Map();
|
|
3440
|
+
channels = /* @__PURE__ */ new Map();
|
|
3441
|
+
notificationInputs = /* @__PURE__ */ new Set();
|
|
3442
|
+
notificationOutputs = /* @__PURE__ */ new Map();
|
|
3443
|
+
batchInputs = [];
|
|
3444
|
+
batchOutputs = [];
|
|
3445
|
+
batchNotifications = [];
|
|
3446
|
+
emptyOutputs = Object.create(null);
|
|
3447
|
+
resources = [];
|
|
3448
|
+
jits = [];
|
|
3449
|
+
processes = [];
|
|
3450
|
+
reactions = [];
|
|
3451
|
+
derived = /* @__PURE__ */ new Map();
|
|
3452
|
+
/** One-entry memo for repeated `read('derived', name)` calls inside a body. */
|
|
3453
|
+
derivedName;
|
|
3454
|
+
derivedEntry;
|
|
3455
|
+
derivedBuffers = /* @__PURE__ */ new Map();
|
|
3456
|
+
stateResources = /* @__PURE__ */ new Map();
|
|
3457
|
+
memoryResources = /* @__PURE__ */ new Map();
|
|
3458
|
+
sampleResources = /* @__PURE__ */ new Map();
|
|
3459
|
+
observerRevision = 0;
|
|
3460
|
+
dependencyStack = [];
|
|
3461
|
+
observations = /* @__PURE__ */ new WeakMap();
|
|
3462
|
+
staticObservations = /* @__PURE__ */ new WeakMap();
|
|
3463
|
+
resistorIntents = /* @__PURE__ */ new Map();
|
|
3464
|
+
sampleEntries = /* @__PURE__ */ new Map();
|
|
3465
|
+
intentEntries = /* @__PURE__ */ new Map();
|
|
3466
|
+
stateSlots = /* @__PURE__ */ new Map();
|
|
3467
|
+
/** Per-instance handles for the fixed resources a resumable process body binds. */
|
|
3468
|
+
resourceHandles = /* @__PURE__ */ new Map();
|
|
3469
|
+
memoryPlans = /* @__PURE__ */ new Map();
|
|
3470
|
+
/** 按 memory 维护的代码代际;只有 code region 内写入才递增。 */
|
|
3471
|
+
codeVersions = /* @__PURE__ */ new Map();
|
|
3472
|
+
/**
|
|
3473
|
+
* Every memory name any attached JIT binds as a code region. A write outside this set
|
|
3474
|
+
* cannot affect a JIT, so the store path skips the region scans entirely.
|
|
3475
|
+
*/
|
|
3476
|
+
codeMemories = /* @__PURE__ */ new Set();
|
|
3477
|
+
observationFrames = /* @__PURE__ */ new WeakMap();
|
|
3478
|
+
evaluating = /* @__PURE__ */ new Set();
|
|
3479
|
+
random;
|
|
3480
|
+
host;
|
|
3481
|
+
context;
|
|
3482
|
+
adapter;
|
|
3483
|
+
valid = true;
|
|
3484
|
+
rawProps;
|
|
3485
|
+
constructor(board, entry, id, props, seed, override) {
|
|
3486
|
+
this.board = board;
|
|
3487
|
+
this.entry = entry;
|
|
3488
|
+
this.id = id;
|
|
3489
|
+
for (const declaration of entry.model.declarations) {
|
|
3490
|
+
if ("name" in declaration) this.declarations.set(declaration.name, declaration);
|
|
3491
|
+
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, {
|
|
3492
|
+
version: 0,
|
|
3493
|
+
subscribers: /* @__PURE__ */ new Set(),
|
|
3494
|
+
waiters: void 0
|
|
3495
|
+
});
|
|
3496
|
+
}
|
|
3497
|
+
dataObject(props);
|
|
3498
|
+
this.rawProps = props;
|
|
3499
|
+
for (const key of Object.keys(props)) if (this.declarations.get(key)?.kind !== "property") throw new TypeError(`Unknown property ${key}`);
|
|
3500
|
+
this.random = randomState(seed, id, override);
|
|
3501
|
+
this.host = {
|
|
3502
|
+
read: (kind, name) => this.read(kind, name),
|
|
3503
|
+
write: (name, value) => this.writeState(name, value, this.stateSlot(name)),
|
|
3504
|
+
sample: (ref) => {
|
|
3505
|
+
return this.readSample(ref);
|
|
3506
|
+
},
|
|
3507
|
+
intent: (pin, value) => {
|
|
3508
|
+
this.writeIntent(pin, value);
|
|
3509
|
+
},
|
|
3510
|
+
resistor: (pin, resistance) => {
|
|
3511
|
+
const r = electricalNumber(resistance);
|
|
3512
|
+
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
3513
|
+
return this.resistorIntent(pin, r);
|
|
3514
|
+
},
|
|
3515
|
+
memoryLength: (memory) => this.memory(memory).length,
|
|
3516
|
+
load: (kind, memory, offset) => {
|
|
3517
|
+
const plan = this.memoryPlan(memory);
|
|
3518
|
+
const size = kind.startsWith("load8") ? 1 : kind.startsWith("load16") ? 2 : 4;
|
|
3519
|
+
const at = range(plan.bytes, offset, size);
|
|
3520
|
+
const dependencies = this.dependencies();
|
|
3521
|
+
if (dependencies) dependencies.set(plan.slot, plan.slot.version);
|
|
3522
|
+
if (size === 1) return plan.bytes[at];
|
|
3523
|
+
return size === 2 ? plan.view.getUint16(at, kind.endsWith("le")) : plan.view.getUint32(at, kind.endsWith("le"));
|
|
3524
|
+
},
|
|
3525
|
+
store: (kind, memory, offset, value) => {
|
|
3526
|
+
const plan = this.memoryPlan(memory);
|
|
3527
|
+
const size = kind.startsWith("store8") ? 1 : kind.startsWith("store16") ? 2 : 4;
|
|
3528
|
+
const at = range(plan.bytes, offset, size);
|
|
3529
|
+
const n = integer(value, "stored value");
|
|
3530
|
+
if (size === 1) plan.bytes[at] = n;
|
|
3531
|
+
else if (size === 2) plan.view.setUint16(at, n, kind.endsWith("le"));
|
|
3532
|
+
else plan.view.setUint32(at, n, kind.endsWith("le"));
|
|
3533
|
+
this.memoryChangedPlan(plan, at, size);
|
|
3534
|
+
},
|
|
3535
|
+
copyBytes: (memory, offset, length) => {
|
|
3536
|
+
this.track(this.memoryResources, String(memory));
|
|
3537
|
+
const bytes = this.memory(memory), at = range(bytes, offset, length);
|
|
3538
|
+
return bytes.slice(at, at + Number(length));
|
|
3539
|
+
},
|
|
3540
|
+
send: (name, value) => this.send(name, value, false),
|
|
3541
|
+
notify: (name) => {
|
|
3542
|
+
if (!this.notificationOutputs.has(name)) throw new TypeError(`Unknown notification ${name}`);
|
|
3543
|
+
this.notificationOutputs.set(name, board.time);
|
|
3544
|
+
},
|
|
3545
|
+
now: () => board.time,
|
|
3546
|
+
random: () => nextRandom(this.random),
|
|
3547
|
+
call: (name, args) => this.invoke(name, args),
|
|
3548
|
+
assert: (condition, message) => {
|
|
3549
|
+
if (condition !== true) throw new Error(message === void 0 ? "Assertion failed" : String(message));
|
|
3550
|
+
},
|
|
3551
|
+
bind: (specs) => this.bindResources(specs)
|
|
3458
3552
|
};
|
|
3459
|
-
|
|
3553
|
+
this.context = createContext(this, board);
|
|
3460
3554
|
}
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
/** Read counters without creating them; undefined means diagnostics stayed off. */
|
|
3464
|
-
function readDirectCheckpointStats(jit) {
|
|
3465
|
-
return checkpointStatsByJit.get(jit);
|
|
3466
|
-
}
|
|
3467
|
-
var barriers = /* @__PURE__ */ new WeakMap();
|
|
3468
|
-
function directBarriers(jit) {
|
|
3469
|
-
let entry = barriers.get(jit);
|
|
3470
|
-
if (entry === void 0) {
|
|
3471
|
-
entry = {
|
|
3472
|
-
stateMask: /* @__PURE__ */ new Int32Array(0),
|
|
3473
|
-
memoryMask: /* @__PURE__ */ new Int32Array(0)
|
|
3474
|
-
};
|
|
3475
|
-
barriers.set(jit, entry);
|
|
3555
|
+
track(resources, name) {
|
|
3556
|
+
this.trackSlot(resources.get(name));
|
|
3476
3557
|
}
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
|
|
3483
|
-
|
|
3484
|
-
|
|
3485
|
-
|
|
3486
|
-
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
|
|
3494
|
-
|
|
3495
|
-
|
|
3496
|
-
const region = instruction.codeRegion;
|
|
3497
|
-
return region === void 0 || region.memory === jit.options.code;
|
|
3498
|
-
}
|
|
3499
|
-
function effectivePolling(jit) {
|
|
3500
|
-
return jit.metadata?.polling ?? jit.polling;
|
|
3501
|
-
}
|
|
3502
|
-
function effectiveDelays(jit) {
|
|
3503
|
-
return jit.metadata?.delays ?? jit.delays;
|
|
3504
|
-
}
|
|
3505
|
-
/** Aliases of the primary memory must refresh together before a direct program is built. */
|
|
3506
|
-
function primaryCodeRegions(jit) {
|
|
3507
|
-
return jit.codeRegions.filter((region) => region.memory === jit.options.code);
|
|
3508
|
-
}
|
|
3509
|
-
/**
|
|
3510
|
-
* Lazily (re)build code-derived metadata for the requested regions. Provider calls never
|
|
3511
|
-
* happen from attach/store; this runs inside the translate phase immediately before the
|
|
3512
|
-
* polling/direct/translate decision that needs it.
|
|
3513
|
-
*/
|
|
3514
|
-
function ensureMetadata(board, jit, regions) {
|
|
3515
|
-
const metadata = jit.metadata;
|
|
3516
|
-
const provider = jit.options.codeMetadata;
|
|
3517
|
-
if (metadata === void 0 || provider === void 0) return;
|
|
3518
|
-
const needed = [];
|
|
3519
|
-
const seen = /* @__PURE__ */ new Set();
|
|
3520
|
-
for (const region of regions) {
|
|
3521
|
-
const state = metadata.regions.get(region);
|
|
3522
|
-
if (state === void 0 || seen.has(state)) continue;
|
|
3523
|
-
seen.add(state);
|
|
3524
|
-
const version = jit.instance.codeVersion(region.memory);
|
|
3525
|
-
if (state.appliedVersion === version && state.pending.length === 0 && !state.dirty) continue;
|
|
3526
|
-
const changes = state.appliedVersion === -1 ? [Object.freeze({
|
|
3527
|
-
memory: region.memory,
|
|
3528
|
-
offset: 0,
|
|
3529
|
-
length: region.size
|
|
3530
|
-
})] : state.pending.map((change) => Object.freeze({ ...change }));
|
|
3531
|
-
needed.push({
|
|
3532
|
-
state,
|
|
3533
|
-
changes,
|
|
3534
|
-
version
|
|
3558
|
+
/** Innermost dependency sink, or `undefined` outside an evaluation. */
|
|
3559
|
+
dependencies() {
|
|
3560
|
+
const stack = this.dependencyStack;
|
|
3561
|
+
return stack.length === 0 ? void 0 : stack[stack.length - 1];
|
|
3562
|
+
}
|
|
3563
|
+
trackSlot(slot) {
|
|
3564
|
+
const dependencies = this.dependencies();
|
|
3565
|
+
if (dependencies) dependencies.set(slot, slot.version);
|
|
3566
|
+
}
|
|
3567
|
+
/** Read-only evidence that a process frame bound per-instance resource handles. */
|
|
3568
|
+
bindingReport() {
|
|
3569
|
+
return Object.freeze({
|
|
3570
|
+
instance: this.id,
|
|
3571
|
+
handles: this.resourceHandles.size,
|
|
3572
|
+
bound: Object.freeze([...this.resourceHandles.keys()].sort()),
|
|
3573
|
+
processes: Object.freeze(this.processes.map((process) => Object.freeze({
|
|
3574
|
+
name: process.declaration.kind === "on" ? process.declaration.name : "start",
|
|
3575
|
+
bound: process.frame?.resources?.length ?? 0
|
|
3576
|
+
})))
|
|
3535
3577
|
});
|
|
3536
3578
|
}
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3579
|
+
dependenciesCurrent(dependencies) {
|
|
3580
|
+
if (!dependencies) return false;
|
|
3581
|
+
for (const [slot, version] of dependencies) if (slot.version !== version) return false;
|
|
3582
|
+
return true;
|
|
3583
|
+
}
|
|
3584
|
+
/** Publishes a derived cache entry; the memo must follow every table write. */
|
|
3585
|
+
setDerived(name, entry) {
|
|
3586
|
+
if (this.derivedName === name && this.derivedEntry === entry) return;
|
|
3587
|
+
this.derived.set(name, entry);
|
|
3588
|
+
this.derivedName = name;
|
|
3589
|
+
this.derivedEntry = entry;
|
|
3590
|
+
}
|
|
3591
|
+
evaluate(compute, target) {
|
|
3592
|
+
const dependencies = target ?? /* @__PURE__ */ new Map();
|
|
3593
|
+
this.dependencyStack.push(dependencies);
|
|
3594
|
+
try {
|
|
3595
|
+
return {
|
|
3596
|
+
value: compute(),
|
|
3597
|
+
dependencies
|
|
3598
|
+
};
|
|
3599
|
+
} finally {
|
|
3600
|
+
this.dependencyStack.pop();
|
|
3601
|
+
const parent = this.dependencies();
|
|
3602
|
+
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
3603
|
+
}
|
|
3604
|
+
}
|
|
3605
|
+
/** Leaf form of :func:`evaluate` for a source walk: no closure and no result wrapper. */
|
|
3606
|
+
evaluateSource(path, dependencies) {
|
|
3607
|
+
this.dependencyStack.push(dependencies);
|
|
3608
|
+
try {
|
|
3609
|
+
return snapshot(this.source(path));
|
|
3610
|
+
} finally {
|
|
3611
|
+
this.dependencyStack.pop();
|
|
3612
|
+
const parent = this.dependencies();
|
|
3613
|
+
if (parent) for (const [key, version] of dependencies) parent.set(key, version);
|
|
3614
|
+
}
|
|
3615
|
+
}
|
|
3616
|
+
observe(observation, path) {
|
|
3617
|
+
const read = this.staticObservations.get(observation);
|
|
3618
|
+
if (read) return read();
|
|
3619
|
+
const declaration = this.declarations.get(path[0]);
|
|
3620
|
+
if (declaration?.kind === "pin" || declaration?.kind === "state") {
|
|
3621
|
+
const resources = declaration.kind === "pin" ? this.sampleResources : this.stateResources;
|
|
3622
|
+
const pin = declaration.kind === "pin" ? this.pin(declaration.name) : void 0;
|
|
3623
|
+
if (pin && path[1] !== "sample") throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
3624
|
+
const read = () => {
|
|
3625
|
+
let value = pin ? pin.sample : this.states.get(declaration.name);
|
|
3626
|
+
for (let index = pin ? 2 : 1; index < path.length; index++) value = value[path[index]];
|
|
3627
|
+
return pin ? value : snapshot(value);
|
|
3628
|
+
};
|
|
3629
|
+
const value = read();
|
|
3630
|
+
const slot = resources.get(declaration.name);
|
|
3631
|
+
if (!slot.subscribers.size) this.observerRevision++;
|
|
3632
|
+
slot.subscribers.add(observation);
|
|
3633
|
+
this.staticObservations.set(observation, read);
|
|
3634
|
+
return value;
|
|
3635
|
+
}
|
|
3636
|
+
const frames = this.observationFrames.get(observation);
|
|
3637
|
+
let target;
|
|
3638
|
+
if (frames === void 0) {
|
|
3639
|
+
target = /* @__PURE__ */ new Map();
|
|
3640
|
+
this.observationFrames.set(observation, {
|
|
3641
|
+
first: target,
|
|
3642
|
+
second: /* @__PURE__ */ new Map(),
|
|
3643
|
+
alternate: true
|
|
3644
|
+
});
|
|
3645
|
+
} else {
|
|
3646
|
+
target = frames.alternate ? frames.second : frames.first;
|
|
3647
|
+
frames.alternate = !frames.alternate;
|
|
3648
|
+
target.clear();
|
|
3649
|
+
}
|
|
3650
|
+
const observed = this.evaluateSource(path, target);
|
|
3651
|
+
const previous = this.observations.get(observation);
|
|
3652
|
+
if (previous === void 0 || previous.size !== target.size || !hasEvery(previous, target)) {
|
|
3653
|
+
if (previous) {
|
|
3654
|
+
for (const slot of previous.keys()) if (!target.has(slot) && slot.subscribers.delete(observation) && !slot.subscribers.size) this.observerRevision++;
|
|
3655
|
+
}
|
|
3656
|
+
for (const slot of target.keys()) if (!previous?.has(slot)) {
|
|
3657
|
+
if (!slot.subscribers.size) this.observerRevision++;
|
|
3658
|
+
slot.subscribers.add(observation);
|
|
3572
3659
|
}
|
|
3573
3660
|
}
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3661
|
+
this.observations.set(observation, target);
|
|
3662
|
+
return observed;
|
|
3663
|
+
}
|
|
3664
|
+
observationReader(observation, path) {
|
|
3665
|
+
return this.staticObservations.get(observation) ?? (() => this.observe(observation, path));
|
|
3666
|
+
}
|
|
3667
|
+
read(kind, name) {
|
|
3668
|
+
if (kind === "state") {
|
|
3669
|
+
this.trackSlot(this.stateSlot(name));
|
|
3670
|
+
return this.states.get(name);
|
|
3584
3671
|
}
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3672
|
+
if (kind === "derived") {
|
|
3673
|
+
let cached = name === this.derivedName ? this.derivedEntry : this.derived.get(name);
|
|
3674
|
+
const epoch = this.board.resourceEpoch;
|
|
3675
|
+
if (cached === void 0 || cached.epoch !== epoch && !this.dependenciesCurrent(cached.dependencies)) {
|
|
3676
|
+
const declaration = this.declarations.get(name);
|
|
3677
|
+
if (declaration?.kind !== "derived") throw new TypeError(`Unknown derived ${name}`);
|
|
3678
|
+
let buffer = this.derivedBuffers.get(name);
|
|
3679
|
+
if (buffer === void 0) {
|
|
3680
|
+
buffer = /* @__PURE__ */ new Map();
|
|
3681
|
+
this.derivedBuffers.set(name, buffer);
|
|
3682
|
+
} else buffer.clear();
|
|
3683
|
+
const evaluated = this.evaluate(() => computation(declaration.value, this.host), buffer);
|
|
3684
|
+
cached = {
|
|
3685
|
+
value: evaluated.value,
|
|
3686
|
+
dependencies: evaluated.dependencies,
|
|
3687
|
+
epoch: this.board.resourceEpoch
|
|
3688
|
+
};
|
|
3689
|
+
this.setDerived(name, cached);
|
|
3690
|
+
} else if (cached.epoch !== epoch) cached.epoch = epoch;
|
|
3691
|
+
const parent = this.dependencies();
|
|
3692
|
+
if (parent) for (const [key, version] of cached.dependencies) parent.set(key, version);
|
|
3693
|
+
return cached.value;
|
|
3590
3694
|
}
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
3597
|
-
|
|
3598
|
-
|
|
3695
|
+
const values = kind === "property" ? this.props : this.constants;
|
|
3696
|
+
if (values.has(name)) return values.get(name);
|
|
3697
|
+
if (this.evaluating.has(name)) throw new TypeError(`Circular creation dependency ${name}`);
|
|
3698
|
+
this.evaluating.add(name);
|
|
3699
|
+
try {
|
|
3700
|
+
const declaration = this.declarations.get(name);
|
|
3701
|
+
if (declaration?.kind !== "property" && declaration?.kind !== "const") throw new TypeError(`Invalid creation reference ${name}`);
|
|
3702
|
+
let value;
|
|
3703
|
+
if (declaration.kind === "property") {
|
|
3704
|
+
if (Object.hasOwn(this.rawProps, name)) value = captureValue(declaration.type, this.rawProps[name], this.declarations, true);
|
|
3705
|
+
else if (declaration.default) value = captureValue(declaration.type, computation(declaration.default, this.host), this.declarations, true);
|
|
3706
|
+
else throw new TypeError(`Missing property ${name}`);
|
|
3707
|
+
} else value = computation(declaration.value, this.host);
|
|
3708
|
+
values.set(name, value);
|
|
3709
|
+
return value;
|
|
3710
|
+
} finally {
|
|
3711
|
+
this.evaluating.delete(name);
|
|
3599
3712
|
}
|
|
3600
|
-
} catch (cause) {
|
|
3601
|
-
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
3602
|
-
} finally {
|
|
3603
|
-
board.phase = prior;
|
|
3604
3713
|
}
|
|
3605
|
-
|
|
3606
|
-
|
|
3607
|
-
|
|
3608
|
-
|
|
3609
|
-
|
|
3610
|
-
|
|
3611
|
-
|
|
3612
|
-
|
|
3613
|
-
|
|
3614
|
-
|
|
3615
|
-
|
|
3616
|
-
|
|
3617
|
-
|
|
3618
|
-
|
|
3619
|
-
|
|
3620
|
-
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3714
|
+
invoke(name, args = []) {
|
|
3715
|
+
const fn = this.declarations.get(name);
|
|
3716
|
+
if (fn?.kind !== "function") throw new TypeError(`Unknown function ${name}`);
|
|
3717
|
+
return body(fn.body)(this.host, args);
|
|
3718
|
+
}
|
|
3719
|
+
syncCall(name, args = []) {
|
|
3720
|
+
const step = this.invoke(name, args).next();
|
|
3721
|
+
if (!step.done) throw new TypeError("Unexpected suspension in synchronous function");
|
|
3722
|
+
return step.value;
|
|
3723
|
+
}
|
|
3724
|
+
pin(ref) {
|
|
3725
|
+
const pin = this.pins.get(String(ref));
|
|
3726
|
+
if (!pin) throw new TypeError(`Unknown pin ${String(ref)}`);
|
|
3727
|
+
return pin;
|
|
3728
|
+
}
|
|
3729
|
+
/** Shared by the generic host and direct JIT, which needs to know whether to publish. */
|
|
3730
|
+
writeIntent(ref, value) {
|
|
3731
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
3732
|
+
const board = this.board;
|
|
3733
|
+
let entry = this.intentEntries.get(name);
|
|
3734
|
+
if (entry === void 0) {
|
|
3735
|
+
const pin = this.pin(name);
|
|
3736
|
+
entry = {
|
|
3737
|
+
pin,
|
|
3738
|
+
slot: board.electricalSlot(pin)
|
|
3739
|
+
};
|
|
3740
|
+
this.intentEntries.set(name, entry);
|
|
3624
3741
|
}
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
};
|
|
3645
|
-
}
|
|
3646
|
-
function refreshDirectGuards(jit, hookReads) {
|
|
3647
|
-
let observersChanged = false;
|
|
3648
|
-
let entryChanged = false;
|
|
3649
|
-
const instance = jit.instance;
|
|
3650
|
-
const hookCount = jit.directHookReads.size;
|
|
3651
|
-
if (hookReads) for (const slot of hookReads.keys()) {
|
|
3652
|
-
if (jit.directHookReads.has(slot)) continue;
|
|
3653
|
-
jit.directHookReads.add(slot);
|
|
3654
|
-
let resource = false;
|
|
3655
|
-
for (const candidate of instance.stateResources.values()) if (candidate === slot) {
|
|
3656
|
-
resource = true;
|
|
3657
|
-
break;
|
|
3742
|
+
if (entry.pin.intent === value) return false;
|
|
3743
|
+
entry.pin.intent = value;
|
|
3744
|
+
board.markElectricalDirty(entry.slot);
|
|
3745
|
+
return true;
|
|
3746
|
+
}
|
|
3747
|
+
/**
|
|
3748
|
+
* One frozen, reusable intent value per (pin, resistance); intents are immutable. The
|
|
3749
|
+
* outer map is keyed by pin name and the inner one by resistance, so the hot path builds
|
|
3750
|
+
* no key string. Bounds: at most 64 names, and at most 8 resistances per name; a full
|
|
3751
|
+
* level is cleared wholesale (no least-recently-used eviction), so identity is only
|
|
3752
|
+
* reused while both levels stay under their limits.
|
|
3753
|
+
*/
|
|
3754
|
+
resistorIntent(pin, resistance) {
|
|
3755
|
+
const name = typeof pin === "string" ? pin : String(pin);
|
|
3756
|
+
let byResistance = this.resistorIntents.get(name);
|
|
3757
|
+
if (byResistance === void 0) {
|
|
3758
|
+
if (this.resistorIntents.size >= 64) this.resistorIntents.clear();
|
|
3759
|
+
byResistance = /* @__PURE__ */ new Map();
|
|
3760
|
+
this.resistorIntents.set(name, byResistance);
|
|
3658
3761
|
}
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3762
|
+
let intent = byResistance.get(resistance);
|
|
3763
|
+
if (intent === void 0) {
|
|
3764
|
+
if (byResistance.size >= 8) byResistance.clear();
|
|
3765
|
+
intent = Object.freeze([Object.freeze({
|
|
3766
|
+
to: this.pin(name),
|
|
3767
|
+
resistance
|
|
3768
|
+
})]);
|
|
3769
|
+
byResistance.set(resistance, intent);
|
|
3770
|
+
}
|
|
3771
|
+
return intent;
|
|
3772
|
+
}
|
|
3773
|
+
readSample(ref) {
|
|
3774
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
3775
|
+
let entry = this.sampleEntries.get(name);
|
|
3776
|
+
if (entry === void 0) {
|
|
3777
|
+
const pin = this.pin(name);
|
|
3778
|
+
entry = {
|
|
3779
|
+
pin,
|
|
3780
|
+
slot: this.sampleResources.get(pin.name)
|
|
3781
|
+
};
|
|
3782
|
+
if (this.sampleEntries.size >= 512) this.sampleEntries.clear();
|
|
3783
|
+
this.sampleEntries.set(name, entry);
|
|
3784
|
+
}
|
|
3785
|
+
const dependencies = this.dependencies();
|
|
3786
|
+
if (dependencies) dependencies.set(entry.slot, entry.slot.version);
|
|
3787
|
+
return entry.pin.sample;
|
|
3788
|
+
}
|
|
3789
|
+
memory(ref) {
|
|
3790
|
+
const bytes = this.memories.get(String(ref));
|
|
3791
|
+
if (!bytes) throw new TypeError(`Unknown memory ${String(ref)}`);
|
|
3792
|
+
return bytes;
|
|
3793
|
+
}
|
|
3794
|
+
/** Instance-owned numeric bank, mirrored by every non-direct state write. */
|
|
3795
|
+
directStateBank() {
|
|
3796
|
+
if (this.directBank === void 0) {
|
|
3797
|
+
const stateNames = [...this.states.keys()].filter((name) => {
|
|
3798
|
+
const declaration = this.declarations.get(name);
|
|
3799
|
+
return declaration.type.kind === "number" || declaration.type.kind === "bool";
|
|
3800
|
+
});
|
|
3801
|
+
const frozenNames = Object.freeze(stateNames);
|
|
3802
|
+
const stateValues = stateNames.map((name) => this.states.get(name));
|
|
3803
|
+
this.states.bindDirectBank(frozenNames, stateValues);
|
|
3804
|
+
this.directBank = {
|
|
3805
|
+
stateNames: frozenNames,
|
|
3806
|
+
stateSlots: Object.freeze(stateNames.map((name) => this.stateResources.get(name))),
|
|
3807
|
+
states: new Map(stateNames.map((name, index) => [name, index])),
|
|
3808
|
+
stateValues
|
|
3809
|
+
};
|
|
3810
|
+
}
|
|
3811
|
+
return this.directBank;
|
|
3812
|
+
}
|
|
3813
|
+
/** Direct scalar writes are already in the numeric bank. Commit the mirror and version. */
|
|
3814
|
+
commitDirectStates(dirty) {
|
|
3815
|
+
const { stateSlots } = this.directBank;
|
|
3816
|
+
const states = this.states;
|
|
3817
|
+
const board = this.board;
|
|
3818
|
+
let wrote = false;
|
|
3819
|
+
for (let word = 0; word < dirty.length; word++) {
|
|
3820
|
+
let bits = dirty[word];
|
|
3821
|
+
if (bits === 0) continue;
|
|
3822
|
+
dirty[word] = 0;
|
|
3823
|
+
wrote = true;
|
|
3824
|
+
states.markDirectDirty(word, bits);
|
|
3825
|
+
const base = word << 5;
|
|
3826
|
+
while (bits !== 0) {
|
|
3827
|
+
const slot = stateSlots[base + 31 - Math.clz32(bits & -bits)];
|
|
3828
|
+
if (slot.subscribers.size === 0) {
|
|
3829
|
+
slot.version++;
|
|
3830
|
+
board.noteResourceVersion(slot);
|
|
3831
|
+
} else board.invalidate(slot);
|
|
3832
|
+
bits &= bits - 1;
|
|
3663
3833
|
}
|
|
3664
3834
|
}
|
|
3665
|
-
|
|
3835
|
+
return wrote;
|
|
3666
3836
|
}
|
|
3667
|
-
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
const
|
|
3671
|
-
if (
|
|
3672
|
-
|
|
3673
|
-
else jit.directStateGuards.delete(name);
|
|
3674
|
-
observersChanged = true;
|
|
3837
|
+
writeState(name, value, slot) {
|
|
3838
|
+
const captured = snapshot(value);
|
|
3839
|
+
this.states.set(name, captured);
|
|
3840
|
+
const index = this.directBank?.states.get(name);
|
|
3841
|
+
if (index !== void 0) this.directBank.stateValues[index] = captured;
|
|
3842
|
+
this.board.invalidate(slot);
|
|
3675
3843
|
}
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
if (
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3844
|
+
stateSlot(name) {
|
|
3845
|
+
let slot = this.stateSlots.get(name);
|
|
3846
|
+
if (slot === void 0) {
|
|
3847
|
+
if (!this.states.has(name)) throw new TypeError(`Unknown state ${name}`);
|
|
3848
|
+
slot = this.stateResources.get(name);
|
|
3849
|
+
this.stateSlots.set(name, slot);
|
|
3850
|
+
}
|
|
3851
|
+
return slot;
|
|
3682
3852
|
}
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3853
|
+
memoryPlan(ref) {
|
|
3854
|
+
const name = typeof ref === "string" ? ref : String(ref);
|
|
3855
|
+
let plan = this.memoryPlans.get(name);
|
|
3856
|
+
if (plan === void 0) {
|
|
3857
|
+
const bytes = this.memories.get(name);
|
|
3858
|
+
if (!bytes) throw new TypeError(`Unknown memory ${name}`);
|
|
3859
|
+
plan = {
|
|
3860
|
+
name,
|
|
3861
|
+
bytes,
|
|
3862
|
+
view: this.memoryViews.get(name),
|
|
3863
|
+
slot: this.memoryResources.get(name)
|
|
3864
|
+
};
|
|
3865
|
+
this.memoryPlans.set(name, plan);
|
|
3866
|
+
}
|
|
3867
|
+
return plan;
|
|
3687
3868
|
}
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
}
|
|
3691
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
};
|
|
3700
|
-
return {
|
|
3701
|
-
send: (name, value) => {
|
|
3702
|
-
publishTime();
|
|
3703
|
-
instance.host.send(name, value);
|
|
3704
|
-
},
|
|
3705
|
-
sample: (pin) => {
|
|
3706
|
-
publishTime();
|
|
3707
|
-
return instance.host.sample(pin);
|
|
3708
|
-
},
|
|
3709
|
-
intent: (pin, value) => {
|
|
3710
|
-
publishTime();
|
|
3711
|
-
if (instance.writeIntent(pin, value)) frame.publishPending = true;
|
|
3712
|
-
},
|
|
3713
|
-
resistor: (pin, resistance) => {
|
|
3714
|
-
publishTime();
|
|
3715
|
-
return instance.host.resistor(pin, resistance);
|
|
3716
|
-
},
|
|
3717
|
-
notify: (name) => {
|
|
3718
|
-
publishTime();
|
|
3719
|
-
instance.host.notify(name);
|
|
3720
|
-
},
|
|
3721
|
-
now: () => {
|
|
3722
|
-
publishTime();
|
|
3723
|
-
return instance.host.now();
|
|
3724
|
-
},
|
|
3725
|
-
random: () => {
|
|
3726
|
-
publishTime();
|
|
3727
|
-
return instance.host.random();
|
|
3728
|
-
},
|
|
3729
|
-
assert: (condition, message) => {
|
|
3730
|
-
publishTime();
|
|
3731
|
-
instance.host.assert(condition, message);
|
|
3869
|
+
codeVersion(name) {
|
|
3870
|
+
return this.codeVersions.get(name) ?? 0;
|
|
3871
|
+
}
|
|
3872
|
+
intersectsCodeRegion(region, offset, size) {
|
|
3873
|
+
if (offset === void 0 || size === void 0) return true;
|
|
3874
|
+
return offset < region.size && offset + size > 0;
|
|
3875
|
+
}
|
|
3876
|
+
hasCodeRegion(name, offset, size) {
|
|
3877
|
+
for (const jit of this.jits) {
|
|
3878
|
+
if (!jit.active) continue;
|
|
3879
|
+
for (const region of jit.codeRegions) if (region.memory === name && this.intersectsCodeRegion(region, offset, size)) return true;
|
|
3732
3880
|
}
|
|
3733
|
-
|
|
3734
|
-
}
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
const pinElectricalSlots = Int32Array.from(pinObjects, (pin) => instance.board.electricalSlot(pin));
|
|
3748
|
-
jit.directLayout = {
|
|
3749
|
-
...bank,
|
|
3750
|
-
memoryNames: Object.freeze(memoryNames),
|
|
3751
|
-
memories,
|
|
3752
|
-
memoryBanks: Object.freeze(memoryBanks),
|
|
3753
|
-
memoryViews: Object.freeze(memoryViews),
|
|
3754
|
-
pinNames: Object.freeze(pinNames),
|
|
3755
|
-
pins,
|
|
3756
|
-
pinObjects: Object.freeze(pinObjects),
|
|
3757
|
-
pinElectricalSlots
|
|
3758
|
-
};
|
|
3759
|
-
return jit.directLayout;
|
|
3760
|
-
}
|
|
3761
|
-
function flushDirect(board, instance, layout, frame) {
|
|
3762
|
-
board.time = frame.time;
|
|
3763
|
-
frame.publishPending = false;
|
|
3764
|
-
let wrote = instance.commitDirectStates(frame.stateDirty);
|
|
3765
|
-
for (let word = 0; word < frame.memoryDirty.length; word++) {
|
|
3766
|
-
let bits = frame.memoryDirty[word];
|
|
3767
|
-
if (bits === 0) continue;
|
|
3768
|
-
frame.memoryDirty[word] = 0;
|
|
3769
|
-
while (bits !== 0) {
|
|
3770
|
-
const slot = word * 32 + 31 - Math.clz32(bits & -bits);
|
|
3771
|
-
instance.memoryChanged(layout.memoryNames[slot]);
|
|
3772
|
-
bits &= bits - 1;
|
|
3773
|
-
wrote = true;
|
|
3881
|
+
return false;
|
|
3882
|
+
}
|
|
3883
|
+
addMetadataChange(state, change) {
|
|
3884
|
+
const pending = state.pending;
|
|
3885
|
+
let end = change.offset + change.length;
|
|
3886
|
+
let start = change.offset;
|
|
3887
|
+
for (let index = 0; index < pending.length;) {
|
|
3888
|
+
const current = pending[index];
|
|
3889
|
+
const currentEnd = current.offset + current.length;
|
|
3890
|
+
if (change.offset <= currentEnd && current.offset <= end) {
|
|
3891
|
+
start = Math.min(start, current.offset);
|
|
3892
|
+
end = Math.max(end, currentEnd);
|
|
3893
|
+
pending.splice(index, 1);
|
|
3894
|
+
} else index++;
|
|
3774
3895
|
}
|
|
3896
|
+
pending.push({
|
|
3897
|
+
memory: change.memory,
|
|
3898
|
+
offset: start,
|
|
3899
|
+
length: end - start
|
|
3900
|
+
});
|
|
3775
3901
|
}
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3787
|
-
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
due: board.time,
|
|
3793
|
-
resume: 0,
|
|
3794
|
-
stopAfterInstruction: false,
|
|
3795
|
-
boundarySettled: false
|
|
3796
|
-
};
|
|
3797
|
-
frame.writePinIntent = (pinIndex, value) => {
|
|
3798
|
-
if (!Object.hasOwn(instance, "writeIntent")) {
|
|
3799
|
-
const pin = pinObjects[pinIndex];
|
|
3800
|
-
if (pin.intent === value) return;
|
|
3801
|
-
pin.intent = value;
|
|
3802
|
-
instance.board.markElectricalDirty(pinElectricalSlots[pinIndex]);
|
|
3803
|
-
frame.publishPending = true;
|
|
3804
|
-
return;
|
|
3902
|
+
markMetadataChanged(jit, name, offset, size) {
|
|
3903
|
+
if (!jit.metadata) return;
|
|
3904
|
+
for (const state of jit.metadata.regions.values()) {
|
|
3905
|
+
const region = state.region;
|
|
3906
|
+
if (region.memory !== name) continue;
|
|
3907
|
+
if (state.appliedVersion === -1) continue;
|
|
3908
|
+
if (name === jit.options.code) state.dirty = true;
|
|
3909
|
+
if (!this.intersectsCodeRegion(region, offset, size)) continue;
|
|
3910
|
+
state.dirty = true;
|
|
3911
|
+
const start = offset === void 0 ? 0 : Math.max(0, offset);
|
|
3912
|
+
const end = offset === void 0 ? region.size : Math.min(region.size, offset + (size ?? 0));
|
|
3913
|
+
if (end > start) this.addMetadataChange(state, {
|
|
3914
|
+
memory: name,
|
|
3915
|
+
offset: start,
|
|
3916
|
+
length: end - start
|
|
3917
|
+
});
|
|
3805
3918
|
}
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
3809
|
-
|
|
3810
|
-
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3919
|
+
}
|
|
3920
|
+
memoryChanged(name, offset, size) {
|
|
3921
|
+
this.memoryChangedSlot(name, this.memoryPlan(name).slot, offset, size);
|
|
3922
|
+
}
|
|
3923
|
+
memoryChangedPlan(plan, offset, size) {
|
|
3924
|
+
this.memoryChangedSlot(plan.name, plan.slot, offset, size);
|
|
3925
|
+
}
|
|
3926
|
+
memoryChangedSlot(name, slot, offset, size) {
|
|
3927
|
+
this.board.invalidate(slot);
|
|
3928
|
+
if (!this.codeMemories.has(name)) return;
|
|
3929
|
+
if (this.hasCodeRegion(name, offset, size)) this.codeVersions.set(name, this.codeVersion(name) + 1);
|
|
3930
|
+
for (const jit of this.jits) {
|
|
3931
|
+
let affected = false;
|
|
3932
|
+
for (const region of jit.codeRegions) if (region.memory === name && this.intersectsCodeRegion(region, offset, size)) {
|
|
3933
|
+
affected = true;
|
|
3934
|
+
break;
|
|
3935
|
+
}
|
|
3936
|
+
if (!affected) continue;
|
|
3937
|
+
this.markMetadataChanged(jit, name, offset, size);
|
|
3938
|
+
if (jit.options.code === name) {
|
|
3939
|
+
jit.cache.clear();
|
|
3940
|
+
jit.cacheMemories.clear();
|
|
3941
|
+
jit.revision++;
|
|
3942
|
+
invalidateDirect(jit, true);
|
|
3943
|
+
} else if (jit.cacheMemories.has(name)) {
|
|
3944
|
+
for (const [pc, instruction] of jit.cache) if (instruction.codeRegion?.memory === name) jit.cache.delete(pc);
|
|
3945
|
+
jit.cacheMemories.delete(name);
|
|
3946
|
+
if (jit.directMemories.has(name)) invalidateDirect(jit, false);
|
|
3947
|
+
jit.directMemories.delete(name);
|
|
3948
|
+
}
|
|
3949
|
+
}
|
|
3950
|
+
}
|
|
3951
|
+
/**
|
|
3952
|
+
* Handles for the fixed names a resumable process body uses. They live on the instance,
|
|
3953
|
+
* so a shared program text binds each instance (and each process frame) separately, and
|
|
3954
|
+
* every read still resolves the current sample or state value.
|
|
3955
|
+
*/
|
|
3956
|
+
bindResources(specs) {
|
|
3957
|
+
const handles = new Array(specs.length);
|
|
3958
|
+
for (let index = 0; index < specs.length; index++) {
|
|
3959
|
+
const spec = specs[index];
|
|
3960
|
+
const key = `${spec[0]}\0${spec[1]}`;
|
|
3961
|
+
let handle = this.resourceHandles.get(key);
|
|
3962
|
+
if (handle === void 0) {
|
|
3963
|
+
handle = this.resourceHandle(spec[0], spec[1]);
|
|
3964
|
+
this.resourceHandles.set(key, handle);
|
|
3819
3965
|
}
|
|
3820
|
-
|
|
3821
|
-
if (checkpointStats !== void 0) checkpointStats.continues++;
|
|
3822
|
-
return true;
|
|
3823
|
-
};
|
|
3824
|
-
if (jit.options.beforeInstruction === void 0) {
|
|
3825
|
-
const pcIndex = layout.states.get(jit.options.pc);
|
|
3826
|
-
frame.finishCheckpoint = (deadline, emptySuffix) => {
|
|
3827
|
-
if (checkpointStats !== void 0) {
|
|
3828
|
-
checkpointStats.finishes++;
|
|
3829
|
-
if (emptySuffix) checkpointStats.emptySuffixes++;
|
|
3830
|
-
}
|
|
3831
|
-
if (emptySuffix) board.time = frame.time;
|
|
3832
|
-
else flushDirect(board, instance, layout, frame);
|
|
3833
|
-
checkpointBoundary(board, checkpointStats, "checkpointFinish");
|
|
3834
|
-
const direct = jit.direct;
|
|
3835
|
-
const nextPc = frame.states[pcIndex];
|
|
3836
|
-
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)) {
|
|
3837
|
-
frame.boundarySettled = true;
|
|
3838
|
-
if (checkpointStats !== void 0) checkpointStats.exits++;
|
|
3839
|
-
return false;
|
|
3840
|
-
}
|
|
3841
|
-
if (jit.directObserverRevision !== instance.observerRevision) refreshDirectGuards(jit);
|
|
3842
|
-
frame.stopAfterInstruction = false;
|
|
3843
|
-
board.current = instance;
|
|
3844
|
-
if (checkpointStats !== void 0) checkpointStats.continues++;
|
|
3845
|
-
return true;
|
|
3846
|
-
};
|
|
3966
|
+
handles[index] = handle;
|
|
3847
3967
|
}
|
|
3968
|
+
return handles;
|
|
3848
3969
|
}
|
|
3849
|
-
|
|
3850
|
-
|
|
3851
|
-
|
|
3852
|
-
|
|
3853
|
-
|
|
3854
|
-
|
|
3855
|
-
|
|
3856
|
-
|
|
3857
|
-
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
|
|
3862
|
-
|
|
3863
|
-
const
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
|
|
3867
|
-
|
|
3868
|
-
if (!(jit.direct === void 0 || stale >= stride || misses >= DIRECT_REBUILD_PATIENCE)) {
|
|
3869
|
-
if (!covered) {
|
|
3870
|
-
const stats = directStats(jit);
|
|
3871
|
-
stats.directCalls++;
|
|
3872
|
-
stats.directFallbacks++;
|
|
3873
|
-
if (jit.cache.has(pc)) jit.directMisses.set(pc, misses + 1);
|
|
3874
|
-
return false;
|
|
3970
|
+
/**
|
|
3971
|
+
* Each method repeats the generic host operation exactly: dependency slots are still
|
|
3972
|
+
* recorded, state writes still snapshot, and memory stores still run the code-memory
|
|
3973
|
+
* invalidation that clears JIT caches. Every resource a declaration already provides is
|
|
3974
|
+
* resolved here, at bind time; a name that does not exist yet keeps resolving lazily so
|
|
3975
|
+
* it still fails at its first real use, not while binding.
|
|
3976
|
+
*/
|
|
3977
|
+
resourceHandle(kind, name) {
|
|
3978
|
+
if (kind === "pin") {
|
|
3979
|
+
let pin = this.pins.get(name);
|
|
3980
|
+
let slot = pin === void 0 ? -1 : this.board.electricalSlot(pin);
|
|
3981
|
+
let sample = pin === void 0 ? void 0 : this.sampleResources.get(name);
|
|
3982
|
+
let lastResistance;
|
|
3983
|
+
let lastIntent;
|
|
3984
|
+
const resolve = () => {
|
|
3985
|
+
if (pin === void 0) {
|
|
3986
|
+
pin = this.pin(name);
|
|
3987
|
+
slot = this.board.electricalSlot(pin);
|
|
3988
|
+
sample = this.sampleResources.get(name);
|
|
3875
3989
|
}
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3990
|
+
return pin;
|
|
3991
|
+
};
|
|
3992
|
+
return Object.freeze({
|
|
3993
|
+
sample: () => {
|
|
3994
|
+
const current = resolve();
|
|
3995
|
+
const dependencies = this.dependencies();
|
|
3996
|
+
if (dependencies) dependencies.set(sample, sample.version);
|
|
3997
|
+
return current.sample;
|
|
3998
|
+
},
|
|
3999
|
+
intent: (value) => {
|
|
4000
|
+
const current = resolve();
|
|
4001
|
+
if (current.intent === value) return;
|
|
4002
|
+
current.intent = value;
|
|
4003
|
+
this.board.markElectricalDirty(slot);
|
|
4004
|
+
},
|
|
4005
|
+
resistor: (resistance) => {
|
|
4006
|
+
if (lastIntent !== void 0 && resistance === lastResistance) return lastIntent;
|
|
4007
|
+
const r = electricalNumber(resistance);
|
|
4008
|
+
if (r < .001 || r > 1e9) throw new ElectricalParameterError("Resistance outside [0.001, 1e9]");
|
|
4009
|
+
const intent = this.resistorIntent(name, r);
|
|
4010
|
+
lastResistance = resistance;
|
|
4011
|
+
lastIntent = intent;
|
|
4012
|
+
return intent;
|
|
3881
4013
|
}
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
4014
|
+
});
|
|
4015
|
+
}
|
|
4016
|
+
if (kind === "state") {
|
|
4017
|
+
let slot = this.states.has(name) ? this.stateResources.get(name) : void 0;
|
|
4018
|
+
let banked = slot === void 0 ? -1 : this.directBank?.states.get(name) ?? -2;
|
|
4019
|
+
const resolve = () => slot ??= this.stateSlot(name);
|
|
4020
|
+
const states = this.states;
|
|
4021
|
+
return Object.freeze({
|
|
4022
|
+
read: () => {
|
|
4023
|
+
const current = resolve();
|
|
4024
|
+
const dependencies = this.dependencies();
|
|
4025
|
+
if (dependencies) dependencies.set(current, current.version);
|
|
4026
|
+
if (banked === -2) banked = this.directBank?.states.get(name) ?? -1;
|
|
4027
|
+
return banked < 0 ? states.get(name) : this.directBank.stateValues[banked];
|
|
4028
|
+
},
|
|
4029
|
+
write: (value) => {
|
|
4030
|
+
const current = resolve();
|
|
4031
|
+
this.writeState(name, value, current);
|
|
4032
|
+
}
|
|
4033
|
+
});
|
|
4034
|
+
}
|
|
4035
|
+
let plan = this.memories.has(name) ? this.memoryPlan(name) : void 0;
|
|
4036
|
+
const resolve = () => plan ??= this.memoryPlan(name);
|
|
4037
|
+
const read = (size, little, offset) => {
|
|
4038
|
+
const current = resolve();
|
|
4039
|
+
const at = range(current.bytes, offset, size);
|
|
4040
|
+
const dependencies = this.dependencies();
|
|
4041
|
+
if (dependencies) dependencies.set(current.slot, current.slot.version);
|
|
4042
|
+
return size === 1 ? current.bytes[at] : size === 2 ? current.view.getUint16(at, little) : current.view.getUint32(at, little);
|
|
4043
|
+
};
|
|
4044
|
+
const write = (size, little, offset, value) => {
|
|
4045
|
+
const current = resolve();
|
|
4046
|
+
const at = range(current.bytes, offset, size);
|
|
4047
|
+
const n = integer(value, "stored value");
|
|
4048
|
+
if (size === 1) current.bytes[at] = n;
|
|
4049
|
+
else if (size === 2) current.view.setUint16(at, n, little);
|
|
4050
|
+
else current.view.setUint32(at, n, little);
|
|
4051
|
+
this.memoryChangedSlot(name, current.slot, at, size);
|
|
4052
|
+
};
|
|
4053
|
+
return Object.freeze({
|
|
4054
|
+
load8: (offset) => read(1, false, offset),
|
|
4055
|
+
load16le: (offset) => read(2, true, offset),
|
|
4056
|
+
load16be: (offset) => read(2, false, offset),
|
|
4057
|
+
load32le: (offset) => read(4, true, offset),
|
|
4058
|
+
load32be: (offset) => read(4, false, offset),
|
|
4059
|
+
store8: (offset, value) => write(1, false, offset, value),
|
|
4060
|
+
store16le: (offset, value) => write(2, true, offset, value),
|
|
4061
|
+
store16be: (offset, value) => write(2, false, offset, value),
|
|
4062
|
+
store32le: (offset, value) => write(4, true, offset, value),
|
|
4063
|
+
store32be: (offset, value) => write(4, false, offset, value),
|
|
4064
|
+
length: () => this.memory(name).length,
|
|
4065
|
+
copy: (offset, length) => {
|
|
4066
|
+
this.track(this.memoryResources, name);
|
|
4067
|
+
const bytes = this.memory(name), at = range(bytes, offset, length);
|
|
4068
|
+
return bytes.slice(at, at + Number(length));
|
|
4069
|
+
}
|
|
4070
|
+
});
|
|
4071
|
+
}
|
|
4072
|
+
prepare() {
|
|
4073
|
+
for (const declaration of this.entry.model.declarations) if (declaration.kind === "pin") this.pins.set(declaration.name, {
|
|
4074
|
+
id: `${this.id}\0${declaration.name}`,
|
|
4075
|
+
instance: this.id,
|
|
4076
|
+
name: declaration.name,
|
|
4077
|
+
intent: [],
|
|
4078
|
+
sample: {
|
|
4079
|
+
voltage: null,
|
|
4080
|
+
level: "unknown",
|
|
4081
|
+
status: "floating"
|
|
4082
|
+
}
|
|
4083
|
+
});
|
|
4084
|
+
for (const declaration of this.entry.model.declarations) switch (declaration.kind) {
|
|
4085
|
+
case "property":
|
|
4086
|
+
case "const":
|
|
4087
|
+
this.read(declaration.kind, declaration.name);
|
|
4088
|
+
break;
|
|
4089
|
+
case "state":
|
|
4090
|
+
this.states.set(declaration.name, snapshot(computation(declaration.initial, this.host)));
|
|
4091
|
+
break;
|
|
4092
|
+
case "memory": {
|
|
4093
|
+
const length = nonnegative(computation(declaration.length, this.host), "memory length");
|
|
4094
|
+
this.memorySizes.set(declaration.name, length);
|
|
4095
|
+
break;
|
|
4096
|
+
}
|
|
4097
|
+
case "pin": {
|
|
4098
|
+
const pin = this.pins.get(declaration.name);
|
|
4099
|
+
if (declaration.low && declaration.high) {
|
|
4100
|
+
const low = electricalNumber(computation(declaration.low, this.host)), high = electricalNumber(computation(declaration.high, this.host));
|
|
4101
|
+
if (low >= high) throw new ElectricalParameterError("Pin thresholds must satisfy low < high");
|
|
4102
|
+
Object.assign(pin, {
|
|
4103
|
+
low,
|
|
4104
|
+
high
|
|
3901
4105
|
});
|
|
3902
4106
|
}
|
|
3903
|
-
|
|
3904
|
-
|
|
3905
|
-
|
|
3906
|
-
|
|
3907
|
-
|
|
3908
|
-
|
|
3909
|
-
|
|
3910
|
-
|
|
3911
|
-
|
|
3912
|
-
|
|
3913
|
-
states,
|
|
3914
|
-
memories,
|
|
3915
|
-
pins,
|
|
3916
|
-
pc: {
|
|
3917
|
-
name: jit.options.pc,
|
|
3918
|
-
index: states.get(jit.options.pc)
|
|
3919
|
-
},
|
|
3920
|
-
host: true,
|
|
3921
|
-
...jit.functions === void 0 ? {} : { functions: jit.functions },
|
|
3922
|
-
split: true,
|
|
3923
|
-
shardSize: DIRECT_SHARD_SIZE,
|
|
3924
|
-
delayLoops,
|
|
3925
|
-
entryBlockedStates: entry.states,
|
|
3926
|
-
entryBlockedMemories: entry.memories,
|
|
3927
|
-
blockedStates: jit.directStateGuards,
|
|
3928
|
-
blockedMemories: jit.directMemoryGuards
|
|
3929
|
-
};
|
|
3930
|
-
const program = compileDirectJit(instructions, binding, cache);
|
|
3931
|
-
jit.directRevision = jit.revision;
|
|
3932
|
-
jit.directMisses.clear();
|
|
3933
|
-
const stats = directStats(jit);
|
|
3934
|
-
stats.directShards = program?.shards ?? 0;
|
|
3935
|
-
if (program) stats.directCompiles++;
|
|
3936
|
-
const barrier = directBarriers(jit);
|
|
3937
|
-
barrier.stateIndex = states;
|
|
3938
|
-
barrier.memoryIndex = memories;
|
|
3939
|
-
const stateMaskWords = Math.ceil(layout.stateNames.length / 32);
|
|
3940
|
-
const memoryMaskWords = Math.ceil(layout.memoryNames.length / 32);
|
|
3941
|
-
if (barrier.stateMask.length !== stateMaskWords) barrier.stateMask = new Int32Array(stateMaskWords);
|
|
3942
|
-
if (barrier.memoryMask.length !== memoryMaskWords) barrier.memoryMask = new Int32Array(memoryMaskWords);
|
|
3943
|
-
syncDirectBarriers(jit);
|
|
3944
|
-
jit.direct = program ? {
|
|
3945
|
-
program,
|
|
3946
|
-
pcs: new Set(program.pcs),
|
|
3947
|
-
frame: createDirectFrame(board, jit, layout, program, barrier)
|
|
3948
|
-
} : void 0;
|
|
3949
|
-
if (!jit.direct) jit.directPrograms.delete(key);
|
|
3950
|
-
else jit.directPrograms.set(key, jit.direct);
|
|
4107
|
+
if (declaration.initial) pin.intent = computation(declaration.initial, this.host);
|
|
4108
|
+
break;
|
|
4109
|
+
}
|
|
4110
|
+
case "channel": {
|
|
4111
|
+
const capacity = nonnegative(computation(declaration.capacity, this.host), "capacity");
|
|
4112
|
+
if (capacity === 0) throw new RangeError("Capacity must be positive");
|
|
4113
|
+
let maxLength;
|
|
4114
|
+
if (declaration.maxLength) {
|
|
4115
|
+
maxLength = nonnegative(computation(declaration.maxLength, this.host), "maxLength");
|
|
4116
|
+
if (maxLength === 0) throw new RangeError("maxLength must be positive");
|
|
3951
4117
|
}
|
|
4118
|
+
this.channels.set(declaration.name, {
|
|
4119
|
+
declaration,
|
|
4120
|
+
capacity,
|
|
4121
|
+
...maxLength === void 0 ? {} : { maxLength },
|
|
4122
|
+
overflow: declaration.overflow ? String(computation(declaration.overflow, this.host)) : "drop_new",
|
|
4123
|
+
incoming: new Fifo(),
|
|
4124
|
+
pending: new Fifo(),
|
|
4125
|
+
outgoing: new Fifo()
|
|
4126
|
+
});
|
|
4127
|
+
break;
|
|
3952
4128
|
}
|
|
4129
|
+
case "notification":
|
|
4130
|
+
if (declaration.direction === "output") this.notificationOutputs.set(declaration.name, null);
|
|
4131
|
+
break;
|
|
4132
|
+
case "start":
|
|
4133
|
+
case "on":
|
|
4134
|
+
this.processes.push({
|
|
4135
|
+
instance: this,
|
|
4136
|
+
declaration,
|
|
4137
|
+
resumable: resumable(declaration.body),
|
|
4138
|
+
...declaration.kind === "on" ? { source: declaration.sourcePath } : {},
|
|
4139
|
+
state: "idle",
|
|
4140
|
+
pending: false
|
|
4141
|
+
});
|
|
4142
|
+
break;
|
|
4143
|
+
case "reaction": this.reactions.push({
|
|
4144
|
+
instance: this,
|
|
4145
|
+
declaration,
|
|
4146
|
+
previous: void 0,
|
|
4147
|
+
synchronousProgram: synchronousBody(this, declaration.body) ?? null,
|
|
4148
|
+
frame: void 0
|
|
4149
|
+
});
|
|
4150
|
+
}
|
|
4151
|
+
for (const [name, channel] of this.channels) if (channel.declaration.direction === "input") this.batchInputs.push(channel);
|
|
4152
|
+
else {
|
|
4153
|
+
this.batchOutputs.push([name, channel]);
|
|
4154
|
+
this.emptyOutputs[name] = EMPTY_MESSAGES;
|
|
4155
|
+
}
|
|
4156
|
+
for (const name of this.notificationOutputs.keys()) {
|
|
4157
|
+
this.batchNotifications.push(name);
|
|
4158
|
+
this.emptyOutputs[name] = null;
|
|
3953
4159
|
}
|
|
3954
|
-
|
|
4160
|
+
Object.freeze(this.emptyOutputs);
|
|
3955
4161
|
}
|
|
3956
|
-
|
|
3957
|
-
|
|
3958
|
-
|
|
3959
|
-
|
|
3960
|
-
|
|
3961
|
-
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3968
|
-
|
|
3969
|
-
|
|
3970
|
-
|
|
4162
|
+
captureBatch(from, to) {
|
|
4163
|
+
for (const channel of this.batchInputs) if (channel.incoming.length) channel.incoming.clear();
|
|
4164
|
+
let outputs = this.emptyOutputs;
|
|
4165
|
+
for (const [name, channel] of this.batchOutputs) {
|
|
4166
|
+
if (!channel.outgoing.length) continue;
|
|
4167
|
+
if (outputs === this.emptyOutputs) outputs = Object.assign(Object.create(null), this.emptyOutputs);
|
|
4168
|
+
outputs[name] = Object.freeze(channel.outgoing.drain().map((message) => Object.freeze({
|
|
4169
|
+
time: message.time,
|
|
4170
|
+
value: externalValue(message.value)
|
|
4171
|
+
})));
|
|
4172
|
+
}
|
|
4173
|
+
for (const name of this.batchNotifications) {
|
|
4174
|
+
const notified = this.notificationOutputs.get(name);
|
|
4175
|
+
if (notified === null) continue;
|
|
4176
|
+
if (outputs === this.emptyOutputs) outputs = Object.assign(Object.create(null), this.emptyOutputs);
|
|
4177
|
+
outputs[name] = Object.freeze({ time: notified });
|
|
4178
|
+
this.notificationOutputs.set(name, null);
|
|
3971
4179
|
}
|
|
3972
|
-
|
|
3973
|
-
|
|
3974
|
-
|
|
3975
|
-
|
|
3976
|
-
|
|
3977
|
-
|
|
3978
|
-
|
|
3979
|
-
|
|
3980
|
-
if (
|
|
3981
|
-
|
|
3982
|
-
stats.directStopped++;
|
|
3983
|
-
jit.stopped = true;
|
|
3984
|
-
return true;
|
|
3985
|
-
} else if (reason === "invalidated") {
|
|
3986
|
-
frame.stopAfterInstruction = false;
|
|
3987
|
-
if (jit.directProgramRevision === -1 && frame.resume === 0) {
|
|
3988
|
-
jit.direct = void 0;
|
|
3989
|
-
jit.directRevision = -1;
|
|
3990
|
-
}
|
|
3991
|
-
} else stats.directDeadlines++;
|
|
3992
|
-
if (frame.resume) {
|
|
3993
|
-
const wake = Math.max(frame.due, board.time);
|
|
3994
|
-
if (jit.metadata === void 0 && board.canContinueDirect(jit, wake)) {
|
|
3995
|
-
board.time = wake;
|
|
3996
|
-
board.current = instance;
|
|
3997
|
-
pending = true;
|
|
3998
|
-
continue;
|
|
3999
|
-
}
|
|
4000
|
-
if (wake > board.time) {
|
|
4001
|
-
board.events.push(wake, () => board.queueJit(jit));
|
|
4002
|
-
return true;
|
|
4003
|
-
}
|
|
4180
|
+
return Object.freeze({
|
|
4181
|
+
from,
|
|
4182
|
+
to,
|
|
4183
|
+
outputs: outputs === this.emptyOutputs ? outputs : Object.freeze(outputs)
|
|
4184
|
+
});
|
|
4185
|
+
}
|
|
4186
|
+
assertCreation() {
|
|
4187
|
+
for (const declaration of this.entry.model.declarations) {
|
|
4188
|
+
if (declaration.kind !== "assert") continue;
|
|
4189
|
+
if (!body(declaration.body)(this.host).next().done) throw new TypeError("Creation assertion suspended");
|
|
4004
4190
|
}
|
|
4005
|
-
board.queueJit(jit);
|
|
4006
|
-
return true;
|
|
4007
4191
|
}
|
|
4008
|
-
|
|
4009
|
-
|
|
4010
|
-
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
result.set(pcSlot, pcSlot.version);
|
|
4014
|
-
const memory = (pc === void 0 ? void 0 : codeRegionFor(jit, pc))?.memory ?? jit.options.code;
|
|
4015
|
-
const slot = instance.memoryResources.get(memory);
|
|
4016
|
-
result.set(slot, slot.version);
|
|
4017
|
-
return result;
|
|
4018
|
-
}
|
|
4019
|
-
/** Park a polling JIT and, when requested, schedule the next device time event. */
|
|
4020
|
-
function waitForPolling(board, jit, dependencies, duration, advance) {
|
|
4021
|
-
jit.dependencies = dependencies;
|
|
4022
|
-
jit.pollingWait = true;
|
|
4023
|
-
jit.pollingPeriod = 0;
|
|
4024
|
-
board.parkWaiter(jit);
|
|
4025
|
-
const token = ++jit.pollingToken;
|
|
4026
|
-
board.boundary("polling");
|
|
4027
|
-
if (duration === 0 || advance === void 0) return;
|
|
4028
|
-
const due = time(board.time + duration);
|
|
4029
|
-
board.events.push(due, () => {
|
|
4030
|
-
if (token !== jit.pollingToken || !jit.active || jit.stopped || !board.waitingJits.has(jit)) return;
|
|
4031
|
-
const pending = jit.dependencies;
|
|
4032
|
-
delete jit.dependencies;
|
|
4033
|
-
board.unparkWaiter(jit);
|
|
4034
|
-
jit.pollingToken++;
|
|
4035
|
-
board.current = jit.instance;
|
|
4036
|
-
if (!jit.instance.dependenciesCurrent(pending)) {
|
|
4037
|
-
board.queueJit(jit);
|
|
4038
|
-
return;
|
|
4192
|
+
allocate() {
|
|
4193
|
+
for (const [name, length] of this.memorySizes) {
|
|
4194
|
+
const bytes = new Uint8Array(length);
|
|
4195
|
+
this.memories.set(name, bytes);
|
|
4196
|
+
this.memoryViews.set(name, new DataView(bytes.buffer));
|
|
4039
4197
|
}
|
|
4040
|
-
|
|
4041
|
-
board.boundary("polling");
|
|
4042
|
-
board.queueJit(jit);
|
|
4043
|
-
});
|
|
4044
|
-
}
|
|
4045
|
-
function jitSignal(board, jit, signal) {
|
|
4046
|
-
if (signal.kind === "elapse") {
|
|
4047
|
-
const duration = time(signal.duration);
|
|
4048
|
-
if (duration === 0) return false;
|
|
4049
|
-
const due = time(board.time + duration);
|
|
4050
|
-
board.events.push(due, () => board.queueJit(jit));
|
|
4051
|
-
board.boundary("jitSignal");
|
|
4052
|
-
return true;
|
|
4198
|
+
this.memorySizes.clear();
|
|
4053
4199
|
}
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
if (checked.value === true) return false;
|
|
4057
|
-
jit.predicate = signal.predicate;
|
|
4058
|
-
jit.pollingWait = false;
|
|
4059
|
-
jit.dependencies = checked.dependencies;
|
|
4060
|
-
board.parkWaiter(jit);
|
|
4061
|
-
board.boundary("jitSignal");
|
|
4062
|
-
return true;
|
|
4200
|
+
assertCallAllowed() {
|
|
4201
|
+
this.board.assertCallAllowed(this);
|
|
4063
4202
|
}
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4203
|
+
inputs() {
|
|
4204
|
+
const result = Object.create(null);
|
|
4205
|
+
for (const declaration of this.entry.model.declarations) {
|
|
4206
|
+
if (declaration.kind === "channel" && declaration.direction === "input") result[declaration.name] = Object.freeze({ send: (value) => {
|
|
4207
|
+
this.assertCallAllowed();
|
|
4208
|
+
this.send(declaration.name, value, true);
|
|
4209
|
+
} });
|
|
4210
|
+
if (declaration.kind === "notification" && declaration.direction === "input") result[declaration.name] = Object.freeze({ notify: () => {
|
|
4211
|
+
this.assertCallAllowed();
|
|
4212
|
+
this.notificationInputs.add(declaration.name);
|
|
4213
|
+
} });
|
|
4214
|
+
}
|
|
4215
|
+
return Object.freeze(result);
|
|
4069
4216
|
}
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
const
|
|
4073
|
-
|
|
4074
|
-
|
|
4075
|
-
|
|
4076
|
-
|
|
4077
|
-
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
if (duration === 0) board.queueJit(jit);
|
|
4081
|
-
else board.events.push(time(board.time + duration), () => {
|
|
4082
|
-
if (!jit.active || jit.stopped) return;
|
|
4083
|
-
if (dependencies && jit.instance.dependenciesCurrent(dependencies)) {
|
|
4084
|
-
jit.dependencies = dependencies;
|
|
4085
|
-
jit.pollingWait = true;
|
|
4086
|
-
jit.pollingPeriod = duration;
|
|
4087
|
-
jit.pollingOrigin = board.time;
|
|
4088
|
-
board.parkWaiter(jit);
|
|
4089
|
-
} else board.queueJit(jit);
|
|
4217
|
+
publicMemory() {
|
|
4218
|
+
const result = Object.create(null);
|
|
4219
|
+
for (const declaration of this.entry.model.declarations) if (declaration.kind === "memory" && declaration.public) result[declaration.name] = bytesView(this.memory(declaration.name), () => {
|
|
4220
|
+
this.board.assertAlive(this);
|
|
4221
|
+
if (![
|
|
4222
|
+
"idle",
|
|
4223
|
+
"initialize",
|
|
4224
|
+
"sync"
|
|
4225
|
+
].includes(this.board.phase)) throw this.board.error("INVALID_CALL_STATE", "Memory cannot be borrowed in this phase", this);
|
|
4226
|
+
if (this.board.phase === "initialize" && this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "Another factory is initializing", this);
|
|
4090
4227
|
});
|
|
4091
|
-
return
|
|
4228
|
+
return Object.freeze(result);
|
|
4092
4229
|
}
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
|
|
4096
|
-
|
|
4097
|
-
|
|
4098
|
-
|
|
4099
|
-
|
|
4100
|
-
|
|
4101
|
-
|
|
4102
|
-
|
|
4230
|
+
send(name, value, input) {
|
|
4231
|
+
const channel = this.channels.get(name);
|
|
4232
|
+
if (!channel || channel.declaration.direction !== (input ? "input" : "output")) throw new TypeError(`Unknown channel ${name}`);
|
|
4233
|
+
const captured = captureValue(channel.declaration.type, value, this.declarations);
|
|
4234
|
+
if (channel.maxLength !== void 0 && captured.length > channel.maxLength) throw new RangeError("Message exceeds maxLength");
|
|
4235
|
+
const queue = input ? channel.pending : channel.outgoing;
|
|
4236
|
+
if (queue.length === channel.capacity) {
|
|
4237
|
+
if (channel.overflow === "drop_new") return;
|
|
4238
|
+
if (channel.overflow === "error") {
|
|
4239
|
+
const error = this.board.error("CHANNEL_OVERFLOW", `Channel ${name} overflow`, this);
|
|
4240
|
+
this.board.fault(error);
|
|
4241
|
+
throw error;
|
|
4242
|
+
}
|
|
4243
|
+
queue.shift();
|
|
4244
|
+
}
|
|
4245
|
+
queue.push({
|
|
4246
|
+
time: this.board.time,
|
|
4247
|
+
value: captured
|
|
4248
|
+
});
|
|
4103
4249
|
}
|
|
4104
|
-
|
|
4105
|
-
|
|
4106
|
-
|
|
4107
|
-
|
|
4108
|
-
|
|
4109
|
-
|
|
4110
|
-
|
|
4111
|
-
|
|
4112
|
-
|
|
4113
|
-
|
|
4114
|
-
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
|
|
4118
|
-
|
|
4119
|
-
|
|
4120
|
-
|
|
4121
|
-
|
|
4122
|
-
|
|
4250
|
+
captureCodeRegions(options) {
|
|
4251
|
+
const primaryMemory = this.memories.get(options.code);
|
|
4252
|
+
const primary = Object.freeze({
|
|
4253
|
+
memory: options.code,
|
|
4254
|
+
base: 0,
|
|
4255
|
+
size: primaryMemory.length
|
|
4256
|
+
});
|
|
4257
|
+
const additional = [];
|
|
4258
|
+
if (Object.hasOwn(options, "codeRegions")) {
|
|
4259
|
+
if (!Array.isArray(options.codeRegions)) throw new TypeError("codeRegions must be an array");
|
|
4260
|
+
for (const item of options.codeRegions) {
|
|
4261
|
+
dataObject(item, [
|
|
4262
|
+
"memory",
|
|
4263
|
+
"base",
|
|
4264
|
+
"size"
|
|
4265
|
+
]);
|
|
4266
|
+
const memory = identity(item.memory);
|
|
4267
|
+
const base = nonnegative(item.base, "code region base");
|
|
4268
|
+
const size = nonnegative(item.size, "code region size");
|
|
4269
|
+
if (size === 0) throw new RangeError("Code region size must be positive");
|
|
4270
|
+
const length = this.memories.get(memory)?.length;
|
|
4271
|
+
if (length === void 0) throw this.board.error("INVALID_BINDING", "Unknown code region memory", this);
|
|
4272
|
+
if (size > length) throw this.board.error("INVALID_BINDING", "Code region exceeds memory", this);
|
|
4273
|
+
if (base > Number.MAX_SAFE_INTEGER - size) throw this.board.error("INVALID_BINDING", "Code region range is not a safe integer", this);
|
|
4274
|
+
const region = Object.freeze({
|
|
4275
|
+
memory,
|
|
4276
|
+
base,
|
|
4277
|
+
size
|
|
4278
|
+
});
|
|
4279
|
+
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);
|
|
4280
|
+
additional.push(region);
|
|
4123
4281
|
}
|
|
4124
|
-
hookReads = checked?.dependencies;
|
|
4125
|
-
hookPending = handled;
|
|
4126
4282
|
}
|
|
4127
|
-
|
|
4128
|
-
|
|
4129
|
-
|
|
4130
|
-
|
|
4131
|
-
|
|
4132
|
-
|
|
4133
|
-
|
|
4134
|
-
|
|
4135
|
-
|
|
4136
|
-
|
|
4137
|
-
|
|
4138
|
-
|
|
4139
|
-
|
|
4140
|
-
|
|
4283
|
+
return {
|
|
4284
|
+
all: Object.freeze([primary, ...additional]),
|
|
4285
|
+
additional: Object.freeze(additional)
|
|
4286
|
+
};
|
|
4287
|
+
}
|
|
4288
|
+
capturePolling(items, region) {
|
|
4289
|
+
const polling = /* @__PURE__ */ new Map();
|
|
4290
|
+
if (!Array.isArray(items)) throw new TypeError("polling must be an array");
|
|
4291
|
+
for (const item of items) {
|
|
4292
|
+
dataObject(item, [
|
|
4293
|
+
"pc",
|
|
4294
|
+
"predicate",
|
|
4295
|
+
"consume",
|
|
4296
|
+
"wait"
|
|
4297
|
+
]);
|
|
4298
|
+
const pc = finite(item.pc, "polling pc");
|
|
4299
|
+
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);
|
|
4300
|
+
const predicate = identity(item.predicate);
|
|
4301
|
+
const declaration = this.declarations.get(predicate);
|
|
4302
|
+
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);
|
|
4303
|
+
let consume;
|
|
4304
|
+
if (Object.hasOwn(item, "consume")) {
|
|
4305
|
+
consume = identity(item.consume);
|
|
4306
|
+
const fn = this.declarations.get(consume);
|
|
4307
|
+
if (fn?.kind !== "function" || fn.params.length !== 0 || fn.returns !== void 0) throw this.board.error("INVALID_BINDING", "Invalid polling consume function", this);
|
|
4141
4308
|
}
|
|
4142
|
-
|
|
4143
|
-
|
|
4144
|
-
|
|
4145
|
-
|
|
4146
|
-
|
|
4147
|
-
|
|
4148
|
-
|
|
4149
|
-
|
|
4150
|
-
|
|
4151
|
-
|
|
4152
|
-
|
|
4153
|
-
const bytes = codeRegion === void 0 || codeRegion.size >= memory.length ? memory : memory.subarray(0, codeRegion.size);
|
|
4154
|
-
const code = bytesView(bytes, () => {
|
|
4155
|
-
if (!valid) throw board.error("OBJECT_DISPOSED", "Translation view expired", jit.instance);
|
|
4309
|
+
let wait;
|
|
4310
|
+
if (Object.hasOwn(item, "wait")) {
|
|
4311
|
+
dataObject(item.wait, ["delay", "advance"]);
|
|
4312
|
+
const delay = identity(item.wait.delay);
|
|
4313
|
+
const advance = identity(item.wait.advance);
|
|
4314
|
+
const delayFn = this.declarations.get(delay);
|
|
4315
|
+
const advanceFn = this.declarations.get(advance);
|
|
4316
|
+
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);
|
|
4317
|
+
wait = Object.freeze({
|
|
4318
|
+
delay,
|
|
4319
|
+
advance
|
|
4156
4320
|
});
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4160
|
-
|
|
4161
|
-
|
|
4162
|
-
|
|
4163
|
-
|
|
4164
|
-
|
|
4165
|
-
|
|
4166
|
-
|
|
4167
|
-
|
|
4168
|
-
|
|
4169
|
-
|
|
4170
|
-
|
|
4171
|
-
|
|
4172
|
-
|
|
4173
|
-
|
|
4174
|
-
|
|
4175
|
-
|
|
4321
|
+
}
|
|
4322
|
+
polling.set(pc, Object.freeze({
|
|
4323
|
+
predicate,
|
|
4324
|
+
...consume === void 0 ? {} : { consume },
|
|
4325
|
+
...wait === void 0 ? {} : { wait }
|
|
4326
|
+
}));
|
|
4327
|
+
}
|
|
4328
|
+
return polling;
|
|
4329
|
+
}
|
|
4330
|
+
captureMetadataDelays(value, region) {
|
|
4331
|
+
if (!(value instanceof Map)) throw new TypeError("codeMetadata delays must be a Map");
|
|
4332
|
+
const delays = /* @__PURE__ */ new Map();
|
|
4333
|
+
for (const [key, item] of value) {
|
|
4334
|
+
const pc = finite(key, "delay pc");
|
|
4335
|
+
if (pc < region.base || pc >= region.base + region.size) throw this.board.error("INVALID_BINDING", "Delay pc is outside its code region", this);
|
|
4336
|
+
dataObject(item, [
|
|
4337
|
+
"counter",
|
|
4338
|
+
"cycles",
|
|
4339
|
+
"pending"
|
|
4340
|
+
]);
|
|
4341
|
+
const counter = identity(item.counter);
|
|
4342
|
+
const cycles = integer(item.cycles, "delay cycles");
|
|
4343
|
+
if (cycles <= 0) throw new RangeError("Delay cycles must be positive");
|
|
4344
|
+
let pending;
|
|
4345
|
+
if (Object.hasOwn(item, "pending")) {
|
|
4346
|
+
dataObject(item.pending, [
|
|
4347
|
+
"state",
|
|
4348
|
+
"period",
|
|
4349
|
+
"active"
|
|
4350
|
+
]);
|
|
4351
|
+
pending = Object.freeze({
|
|
4352
|
+
state: identity(item.pending.state),
|
|
4353
|
+
period: identity(item.pending.period),
|
|
4354
|
+
active: identity(item.pending.active)
|
|
4176
4355
|
});
|
|
4177
|
-
if (instructions.some((entry) => jit.direct?.pcs.has(entry.pc))) {
|
|
4178
|
-
jit.direct = void 0;
|
|
4179
|
-
jit.directPrograms.clear();
|
|
4180
|
-
}
|
|
4181
|
-
for (let index = instructions.length - 1; index >= 0; index--) {
|
|
4182
|
-
const entry = instructions[index];
|
|
4183
|
-
jit.cache.set(entry.pc, entry);
|
|
4184
|
-
jit.cacheMemories.add(entry.codeRegion?.memory ?? jit.options.code);
|
|
4185
|
-
}
|
|
4186
|
-
if (instructions.some((entry) => entry.codeRegion === void 0 || entry.codeRegion.memory === jit.options.code)) jit.revision++;
|
|
4187
|
-
jit.directSkip = void 0;
|
|
4188
|
-
instruction = instructions[0];
|
|
4189
|
-
} catch (cause) {
|
|
4190
|
-
throw board.wrap(cause, "JIT_FAILED", "translate", jit.instance);
|
|
4191
|
-
} finally {
|
|
4192
|
-
valid = false;
|
|
4193
|
-
board.phase = prior;
|
|
4194
4356
|
}
|
|
4357
|
+
delays.set(pc, Object.freeze({
|
|
4358
|
+
counter,
|
|
4359
|
+
cycles,
|
|
4360
|
+
...pending ? { pending } : {}
|
|
4361
|
+
}));
|
|
4195
4362
|
}
|
|
4196
|
-
|
|
4197
|
-
jit.automaticPolling = board.execution === "throughput" && board.jits.length === 1 && instruction.polling && !hookPending && (!hook || hookReads !== void 0);
|
|
4198
|
-
jit.pollReads = hookReads;
|
|
4199
|
-
jit.instance.host.write(jit.options.pc, instruction.nextPc);
|
|
4200
|
-
jit.execution = instruction.execute(jit.host);
|
|
4363
|
+
return delays;
|
|
4201
4364
|
}
|
|
4202
|
-
|
|
4203
|
-
|
|
4204
|
-
|
|
4205
|
-
|
|
4206
|
-
|
|
4207
|
-
|
|
4208
|
-
|
|
4365
|
+
/** Validate and copy provider output; caller applies it atomically. */
|
|
4366
|
+
captureCodeMetadata(region, value) {
|
|
4367
|
+
dataObject(value, ["polling", "delays"]);
|
|
4368
|
+
if (!Object.hasOwn(value, "polling") || !Object.hasOwn(value, "delays")) throw new TypeError("codeMetadata must contain polling and delays");
|
|
4369
|
+
return {
|
|
4370
|
+
polling: this.capturePolling(value.polling, region),
|
|
4371
|
+
delays: this.captureMetadataDelays(value.delays, region)
|
|
4372
|
+
};
|
|
4373
|
+
}
|
|
4374
|
+
attach(options) {
|
|
4375
|
+
this.assertCallAllowed();
|
|
4376
|
+
if (this.board.phase !== "initialize" || this.board.current !== this) throw this.board.error("INVALID_CALL_STATE", "attachJit is initialization-only", this);
|
|
4377
|
+
dataObject(options, [
|
|
4378
|
+
"entry",
|
|
4379
|
+
"code",
|
|
4380
|
+
"codeRegions",
|
|
4381
|
+
"codeMetadata",
|
|
4382
|
+
"pc",
|
|
4383
|
+
"functions",
|
|
4384
|
+
"beforeInstruction",
|
|
4385
|
+
"polling",
|
|
4386
|
+
"delays",
|
|
4387
|
+
"shareDirectBodies",
|
|
4388
|
+
"translate"
|
|
4389
|
+
]);
|
|
4390
|
+
identity(options.entry);
|
|
4391
|
+
identity(options.code);
|
|
4392
|
+
identity(options.pc);
|
|
4393
|
+
if (this.jits.some((jit) => jit.active && jit.options.entry === options.entry)) throw this.board.error("INVALID_BINDING", "Duplicate JIT entry", this);
|
|
4394
|
+
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);
|
|
4395
|
+
const codeRegions = this.captureCodeRegions(options);
|
|
4396
|
+
if (Object.hasOwn(options, "codeMetadata")) {
|
|
4397
|
+
if (typeof options.codeMetadata !== "function") throw this.board.error("INVALID_BINDING", "Invalid codeMetadata provider", this);
|
|
4398
|
+
if (Object.hasOwn(options, "polling") || Object.hasOwn(options, "delays")) throw this.board.error("INVALID_BINDING", "codeMetadata is mutually exclusive with static polling/delays", this);
|
|
4399
|
+
}
|
|
4400
|
+
if (options.beforeInstruction !== void 0) {
|
|
4401
|
+
dataObject(options.beforeInstruction, ["when", "call"]);
|
|
4402
|
+
const condition = this.declarations.get(options.beforeInstruction.when), fn = this.declarations.get(options.beforeInstruction.call);
|
|
4403
|
+
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);
|
|
4404
|
+
} else if (Object.hasOwn(options, "beforeInstruction")) throw new TypeError("Invalid beforeInstruction");
|
|
4405
|
+
const polling = Object.hasOwn(options, "polling") ? this.capturePolling(options.polling) : /* @__PURE__ */ new Map();
|
|
4406
|
+
if (Object.hasOwn(options, "delays") && !(options.delays instanceof Map)) throw new TypeError("delays must be a Map");
|
|
4407
|
+
if (Object.hasOwn(options, "shareDirectBodies") && typeof options.shareDirectBodies !== "boolean") throw new TypeError("shareDirectBodies must be a boolean");
|
|
4408
|
+
const metadata = Object.hasOwn(options, "codeMetadata") ? {
|
|
4409
|
+
regions: new Map(codeRegions.all.map((region) => [region, {
|
|
4410
|
+
region,
|
|
4411
|
+
appliedVersion: -1,
|
|
4412
|
+
dirty: false,
|
|
4413
|
+
pending: [],
|
|
4414
|
+
polling: /* @__PURE__ */ new Map(),
|
|
4415
|
+
delays: /* @__PURE__ */ new Map()
|
|
4416
|
+
}])),
|
|
4417
|
+
polling: /* @__PURE__ */ new Map(),
|
|
4418
|
+
delays: /* @__PURE__ */ new Map()
|
|
4419
|
+
} : void 0;
|
|
4420
|
+
const base = this.entry.model;
|
|
4421
|
+
const emit = options.functions;
|
|
4422
|
+
if (Object.hasOwn(options, "functions") && typeof emit !== "function") throw new TypeError("functions must be a JIT function builder callback");
|
|
4423
|
+
let model = base;
|
|
4424
|
+
let functions;
|
|
4425
|
+
let host = this.host;
|
|
4426
|
+
if (emit) {
|
|
4427
|
+
const declarations = emit(base);
|
|
4428
|
+
acceptSync(declarations);
|
|
4429
|
+
if (!Array.isArray(declarations)) throw new TypeError("Expected JIT function declarations");
|
|
4430
|
+
model = bindJitFunctions(base, declarations);
|
|
4431
|
+
const local = /* @__PURE__ */ new Map();
|
|
4432
|
+
for (const declaration of model.declarations.slice(base.declarations.length)) if (declaration.kind === "function") local.set(declaration.name, declaration);
|
|
4433
|
+
functions = local;
|
|
4434
|
+
host = {
|
|
4435
|
+
...this.host,
|
|
4436
|
+
call: (name, args) => {
|
|
4437
|
+
const fn = local.get(name);
|
|
4438
|
+
return fn ? body(fn.body)(host, args) : this.invoke(name, args);
|
|
4439
|
+
}
|
|
4440
|
+
};
|
|
4441
|
+
}
|
|
4442
|
+
const jit = {
|
|
4443
|
+
instance: this,
|
|
4444
|
+
model,
|
|
4445
|
+
host,
|
|
4446
|
+
...functions === void 0 ? {} : { functions },
|
|
4447
|
+
options: Object.freeze({
|
|
4448
|
+
...options,
|
|
4449
|
+
...options.codeRegions === void 0 ? {} : { codeRegions: codeRegions.additional },
|
|
4450
|
+
...options.beforeInstruction ? { beforeInstruction: Object.freeze({ ...options.beforeInstruction }) } : {}
|
|
4451
|
+
}),
|
|
4452
|
+
codeRegions: codeRegions.all,
|
|
4453
|
+
cache: /* @__PURE__ */ new Map(),
|
|
4454
|
+
cacheMemories: /* @__PURE__ */ new Set(),
|
|
4455
|
+
metadata,
|
|
4456
|
+
polling,
|
|
4457
|
+
delays: options.delays ?? /* @__PURE__ */ new Map(),
|
|
4458
|
+
revision: 0,
|
|
4459
|
+
directObserverRevision: -1,
|
|
4460
|
+
directStateGuards: /* @__PURE__ */ new Set(),
|
|
4461
|
+
directMemoryGuards: /* @__PURE__ */ new Set(),
|
|
4462
|
+
directHookReads: /* @__PURE__ */ new Set(),
|
|
4463
|
+
directMemories: /* @__PURE__ */ new Set(),
|
|
4464
|
+
direct: void 0,
|
|
4465
|
+
directSkip: void 0,
|
|
4466
|
+
directStale: false,
|
|
4467
|
+
directCacheInvalid: false,
|
|
4468
|
+
pollReads: void 0,
|
|
4469
|
+
currentPc: void 0,
|
|
4470
|
+
automaticPolling: false,
|
|
4471
|
+
pollingWait: false,
|
|
4472
|
+
pollingPeriod: 0,
|
|
4473
|
+
pollingOrigin: 0,
|
|
4474
|
+
pollingToken: 0,
|
|
4475
|
+
dependencyDirty: true,
|
|
4476
|
+
waiterSlots: void 0,
|
|
4477
|
+
active: true,
|
|
4478
|
+
stopped: false,
|
|
4479
|
+
queued: false,
|
|
4480
|
+
execution: void 0
|
|
4481
|
+
};
|
|
4482
|
+
this.jits.push(jit);
|
|
4483
|
+
this.board.jits.push(jit);
|
|
4484
|
+
for (const region of jit.codeRegions) this.codeMemories.add(region.memory);
|
|
4485
|
+
const disposable = { dispose: () => {
|
|
4486
|
+
if (!jit.active) return;
|
|
4487
|
+
jit.active = false;
|
|
4209
4488
|
jit.execution = void 0;
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
|
|
4489
|
+
delete jit.predicate;
|
|
4490
|
+
jit.pollingToken++;
|
|
4491
|
+
this.board.unparkWaiter(jit);
|
|
4492
|
+
jit.cache.clear();
|
|
4493
|
+
jit.cacheMemories.clear();
|
|
4494
|
+
disposeDirect(jit);
|
|
4495
|
+
jit.pollReads = void 0;
|
|
4496
|
+
} };
|
|
4497
|
+
this.resources.push(disposable);
|
|
4498
|
+
return disposable;
|
|
4499
|
+
}
|
|
4500
|
+
loadBytes(name, bytes, offset) {
|
|
4501
|
+
this.assertCallAllowed();
|
|
4502
|
+
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);
|
|
4503
|
+
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);
|
|
4504
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError("Code must be Uint8Array");
|
|
4505
|
+
const memory = this.memory(name), at = range(memory, offset, bytes.length);
|
|
4506
|
+
if (bytes.length) {
|
|
4507
|
+
memory.set(bytes.slice(), at);
|
|
4508
|
+
this.memoryChanged(name, at, bytes.length);
|
|
4213
4509
|
}
|
|
4214
|
-
if (jitSignal(board, jit, step.value)) return;
|
|
4215
4510
|
}
|
|
4511
|
+
source(path) {
|
|
4512
|
+
const declaration = this.declarations.get(path[0]);
|
|
4513
|
+
let value;
|
|
4514
|
+
let firstField = 1;
|
|
4515
|
+
if (declaration?.kind === "pin") {
|
|
4516
|
+
this.track(this.sampleResources, declaration.name);
|
|
4517
|
+
if (path[1] !== "sample") throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
4518
|
+
value = this.pin(declaration.name).sample;
|
|
4519
|
+
firstField = 2;
|
|
4520
|
+
} else if (declaration?.kind === "state" || declaration?.kind === "derived") value = this.read(declaration.kind, declaration.name);
|
|
4521
|
+
else throw new TypeError(`Invalid source ${path.join(".")}`);
|
|
4522
|
+
for (let index = firstField; index < path.length; index++) value = value[path[index]];
|
|
4523
|
+
return value;
|
|
4524
|
+
}
|
|
4525
|
+
};
|
|
4526
|
+
//#endregion
|
|
4527
|
+
//#region src/runtime/publish-gate.ts
|
|
4528
|
+
var gates = /* @__PURE__ */ new WeakMap();
|
|
4529
|
+
/** Attach the gate to the facade the board hands out; never exposed through the package entry. */
|
|
4530
|
+
function registerPublishGate(bridge, gate) {
|
|
4531
|
+
gates.set(bridge, gate);
|
|
4532
|
+
}
|
|
4533
|
+
function publishGate(bridge) {
|
|
4534
|
+
const gate = gates.get(bridge);
|
|
4535
|
+
if (gate === void 0) throw new TypeError("Topic bridge was not created by a board");
|
|
4536
|
+
return gate;
|
|
4216
4537
|
}
|
|
4217
4538
|
//#endregion
|
|
4218
4539
|
//#region src/runtime.ts
|