watr 4.6.5 → 4.6.7
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/dist/watr.js +354 -401
- package/dist/watr.min.js +6 -6
- package/dist/watr.wasm +0 -0
- package/package.json +1 -1
- package/src/const.js +23 -0
- package/src/optimize.js +191 -230
- package/src/polyfill.js +91 -135
- package/src/template.js +245 -0
- package/src/util.js +43 -7
- package/types/src/const.d.ts +1 -0
- package/types/src/const.d.ts.map +1 -1
- package/types/src/optimize.d.ts +7 -29
- package/types/src/optimize.d.ts.map +1 -1
- package/types/src/polyfill.d.ts +8 -14
- package/types/src/polyfill.d.ts.map +1 -1
- package/types/src/template.d.ts +22 -0
- package/types/src/template.d.ts.map +1 -0
- package/types/src/util.d.ts +3 -1
- package/types/src/util.d.ts.map +1 -1
- package/types/watr.d.ts +4 -10
- package/types/watr.d.ts.map +1 -1
- package/watr.js +9 -224
package/dist/watr.js
CHANGED
|
@@ -56,6 +56,17 @@ var str = (s) => {
|
|
|
56
56
|
return bytes;
|
|
57
57
|
};
|
|
58
58
|
var unescape = (s) => tdec.decode(new Uint8Array(str(s)));
|
|
59
|
+
var clone = (node) => Array.isArray(node) ? node.map(clone) : node;
|
|
60
|
+
var walk = (node, fn, parent, idx) => {
|
|
61
|
+
fn(node, parent, idx);
|
|
62
|
+
if (Array.isArray(node)) for (let i = 0; i < node.length; i++) walk(node[i], fn, node, i);
|
|
63
|
+
};
|
|
64
|
+
var walkPost = (node, fn, parent, idx) => {
|
|
65
|
+
if (Array.isArray(node)) for (let i = 0; i < node.length; i++) walkPost(node[i], fn, node, i);
|
|
66
|
+
const result = fn(node, parent, idx);
|
|
67
|
+
if (result !== void 0 && parent) parent[idx] = result;
|
|
68
|
+
return result !== void 0 ? result : node;
|
|
69
|
+
};
|
|
59
70
|
|
|
60
71
|
// src/encode.js
|
|
61
72
|
var uleb = (n, buffer = []) => {
|
|
@@ -1243,6 +1254,17 @@ var INSTR = [
|
|
|
1243
1254
|
"i64.atomic.rmw32.cmpxchg_u memarg"
|
|
1244
1255
|
]
|
|
1245
1256
|
];
|
|
1257
|
+
var resultType = (op) => {
|
|
1258
|
+
if (typeof op !== "string") return null;
|
|
1259
|
+
const dot = op.indexOf(".");
|
|
1260
|
+
if (dot < 0) return null;
|
|
1261
|
+
const prefix = op.slice(0, dot);
|
|
1262
|
+
const scalar = prefix === "i32" || prefix === "i64" || prefix === "f32" || prefix === "f64";
|
|
1263
|
+
if (scalar && /^(eqz?|ne|[lg][te])(_[su])?$/.test(op.slice(dot + 1))) return "i32";
|
|
1264
|
+
if (scalar || prefix === "v128") return prefix;
|
|
1265
|
+
if (op === "memory.size" || op === "memory.grow") return "i32";
|
|
1266
|
+
return null;
|
|
1267
|
+
};
|
|
1246
1268
|
var SECTION = { custom: 0, type: 1, import: 2, func: 3, table: 4, memory: 5, tag: 13, strings: 14, global: 6, export: 7, start: 8, elem: 9, datacount: 12, code: 10, data: 11 };
|
|
1247
1269
|
var TYPE = {
|
|
1248
1270
|
// Value types
|
|
@@ -2177,91 +2199,6 @@ function print(tree, options = {}) {
|
|
|
2177
2199
|
}
|
|
2178
2200
|
|
|
2179
2201
|
// src/polyfill.js
|
|
2180
|
-
var FEATURES = {
|
|
2181
|
-
funcref: ["ref.func", "call_ref", "return_call_ref"],
|
|
2182
|
-
sign_ext: ["i32.extend8_s", "i32.extend16_s", "i64.extend8_s", "i64.extend16_s", "i64.extend32_s"],
|
|
2183
|
-
nontrapping: [
|
|
2184
|
-
"i32.trunc_sat_f32_s",
|
|
2185
|
-
"i32.trunc_sat_f32_u",
|
|
2186
|
-
"i32.trunc_sat_f64_s",
|
|
2187
|
-
"i32.trunc_sat_f64_u",
|
|
2188
|
-
"i64.trunc_sat_f32_s",
|
|
2189
|
-
"i64.trunc_sat_f32_u",
|
|
2190
|
-
"i64.trunc_sat_f64_s",
|
|
2191
|
-
"i64.trunc_sat_f64_u"
|
|
2192
|
-
],
|
|
2193
|
-
bulk_memory: ["memory.copy", "memory.fill"],
|
|
2194
|
-
return_call: ["return_call", "return_call_indirect"],
|
|
2195
|
-
i31ref: ["ref.i31", "i31.get_s", "i31.get_u"],
|
|
2196
|
-
extended_const: ["global.get"],
|
|
2197
|
-
// in const context - detected specially
|
|
2198
|
-
multi_value: [],
|
|
2199
|
-
// detected by result count
|
|
2200
|
-
gc: [
|
|
2201
|
-
"struct.new",
|
|
2202
|
-
"struct.get",
|
|
2203
|
-
"struct.set",
|
|
2204
|
-
"array.new",
|
|
2205
|
-
"array.get",
|
|
2206
|
-
"array.set",
|
|
2207
|
-
"array.len",
|
|
2208
|
-
"struct.new_default",
|
|
2209
|
-
"array.new_default",
|
|
2210
|
-
"array.new_fixed",
|
|
2211
|
-
"array.copy"
|
|
2212
|
-
],
|
|
2213
|
-
ref_cast: ["ref.test", "ref.cast", "br_on_cast", "br_on_cast_fail"]
|
|
2214
|
-
};
|
|
2215
|
-
var ALL = Object.keys(FEATURES);
|
|
2216
|
-
var normalize2 = (opts) => {
|
|
2217
|
-
if (opts === true) return Object.fromEntries(ALL.map((f) => [f, true]));
|
|
2218
|
-
if (opts === false) return {};
|
|
2219
|
-
if (typeof opts === "string") {
|
|
2220
|
-
const set = new Set(opts.split(/\s+/).filter(Boolean));
|
|
2221
|
-
return Object.fromEntries(ALL.map((f) => [f, set.has(f) || set.has("all")]));
|
|
2222
|
-
}
|
|
2223
|
-
return { ...opts };
|
|
2224
|
-
};
|
|
2225
|
-
var walk = (node, fn, parent, idx) => {
|
|
2226
|
-
fn(node, parent, idx);
|
|
2227
|
-
if (Array.isArray(node)) for (let i = 0; i < node.length; i++) walk(node[i], fn, node, i);
|
|
2228
|
-
};
|
|
2229
|
-
var walkPost = (node, fn, parent, idx) => {
|
|
2230
|
-
if (Array.isArray(node)) for (let i = 0; i < node.length; i++) walkPost(node[i], fn, node, i);
|
|
2231
|
-
fn(node, parent, idx);
|
|
2232
|
-
};
|
|
2233
|
-
var detect = (ast) => {
|
|
2234
|
-
const used = /* @__PURE__ */ new Set();
|
|
2235
|
-
walk(ast, (node) => {
|
|
2236
|
-
if (typeof node !== "string") return;
|
|
2237
|
-
for (const [feat, ops] of Object.entries(FEATURES)) {
|
|
2238
|
-
if (ops.some((op) => node === op || node.startsWith(op + " "))) used.add(feat);
|
|
2239
|
-
}
|
|
2240
|
-
});
|
|
2241
|
-
walk(ast, (node) => {
|
|
2242
|
-
if (!Array.isArray(node) || node[0] !== "global") return;
|
|
2243
|
-
for (const init of node) {
|
|
2244
|
-
if (!Array.isArray(init)) continue;
|
|
2245
|
-
if (init[0] === "i32.add" || init[0] === "i32.sub" || init[0] === "i32.mul" || init[0] === "i64.add" || init[0] === "i64.sub" || init[0] === "i64.mul") {
|
|
2246
|
-
walk(init, (inner) => {
|
|
2247
|
-
if (Array.isArray(inner) && inner[0] === "global.get") used.add("extended_const");
|
|
2248
|
-
});
|
|
2249
|
-
}
|
|
2250
|
-
}
|
|
2251
|
-
});
|
|
2252
|
-
walk(ast, (node) => {
|
|
2253
|
-
if (!Array.isArray(node) || node[0] !== "func") return;
|
|
2254
|
-
let resultCount = 0;
|
|
2255
|
-
for (const part of node) {
|
|
2256
|
-
if (Array.isArray(part) && part[0] === "result") {
|
|
2257
|
-
resultCount += part.length - 1;
|
|
2258
|
-
}
|
|
2259
|
-
}
|
|
2260
|
-
if (resultCount > 1) used.add("multi_value");
|
|
2261
|
-
});
|
|
2262
|
-
return used;
|
|
2263
|
-
};
|
|
2264
|
-
var clone = (node) => Array.isArray(node) ? node.map(clone) : node;
|
|
2265
2202
|
var findNodes = (ast, kind) => {
|
|
2266
2203
|
const nodes = [];
|
|
2267
2204
|
const start = ast[0] === "module" ? 1 : 0;
|
|
@@ -2319,7 +2256,6 @@ var funcref = (ast, ctx) => {
|
|
|
2319
2256
|
});
|
|
2320
2257
|
return ast;
|
|
2321
2258
|
};
|
|
2322
|
-
var transforms = { funcref };
|
|
2323
2259
|
var SIGN_EXT_SHIFTS = {
|
|
2324
2260
|
"i32.extend8_s": ["i32", 24],
|
|
2325
2261
|
"i32.extend16_s": ["i32", 16],
|
|
@@ -2342,7 +2278,6 @@ var sign_ext = (ast, ctx) => {
|
|
|
2342
2278
|
});
|
|
2343
2279
|
return ast;
|
|
2344
2280
|
};
|
|
2345
|
-
transforms.sign_ext = sign_ext;
|
|
2346
2281
|
var TRUNC_SAT_INFO = {
|
|
2347
2282
|
"i32.trunc_sat_f32_s": { itype: "i32", ftype: "f32", signed: true, min: -2147483648, max: 2147483647 },
|
|
2348
2283
|
"i32.trunc_sat_f32_u": { itype: "i32", ftype: "f32", signed: false, min: 0, max: 4294967295 },
|
|
@@ -2410,7 +2345,6 @@ var nontrapping = (ast, ctx) => {
|
|
|
2410
2345
|
});
|
|
2411
2346
|
return ast;
|
|
2412
2347
|
};
|
|
2413
|
-
transforms.nontrapping = nontrapping;
|
|
2414
2348
|
var bulk_memory = (ast, ctx) => {
|
|
2415
2349
|
const needsCopy = /* @__PURE__ */ new Set(), needsFill = /* @__PURE__ */ new Set();
|
|
2416
2350
|
walk(ast, (node) => {
|
|
@@ -2502,7 +2436,6 @@ var bulk_memory = (ast, ctx) => {
|
|
|
2502
2436
|
});
|
|
2503
2437
|
return ast;
|
|
2504
2438
|
};
|
|
2505
|
-
transforms.bulk_memory = bulk_memory;
|
|
2506
2439
|
var return_call_transform = (ast, ctx) => {
|
|
2507
2440
|
let hasAnyTailCall = false;
|
|
2508
2441
|
walk(ast, (node) => {
|
|
@@ -2522,7 +2455,6 @@ var return_call_transform = (ast, ctx) => {
|
|
|
2522
2455
|
});
|
|
2523
2456
|
return ast;
|
|
2524
2457
|
};
|
|
2525
|
-
transforms.return_call = return_call_transform;
|
|
2526
2458
|
var i31ref = (ast, ctx) => {
|
|
2527
2459
|
walkPost(ast, (node, parent, idx) => {
|
|
2528
2460
|
if (!Array.isArray(node) || !parent) return;
|
|
@@ -2539,7 +2471,6 @@ var i31ref = (ast, ctx) => {
|
|
|
2539
2471
|
});
|
|
2540
2472
|
return ast;
|
|
2541
2473
|
};
|
|
2542
|
-
transforms.i31ref = i31ref;
|
|
2543
2474
|
var extended_const = (ast, ctx) => {
|
|
2544
2475
|
const globals2 = {};
|
|
2545
2476
|
walk(ast, (node) => {
|
|
@@ -2602,7 +2533,6 @@ var extended_const = (ast, ctx) => {
|
|
|
2602
2533
|
});
|
|
2603
2534
|
return ast;
|
|
2604
2535
|
};
|
|
2605
|
-
transforms.extended_const = extended_const;
|
|
2606
2536
|
var multi_value = (ast, ctx) => {
|
|
2607
2537
|
const multiResultFuncs = /* @__PURE__ */ new Map();
|
|
2608
2538
|
const returnGlobals = [];
|
|
@@ -2651,7 +2581,6 @@ var multi_value = (ast, ctx) => {
|
|
|
2651
2581
|
});
|
|
2652
2582
|
return ast;
|
|
2653
2583
|
};
|
|
2654
|
-
transforms.multi_value = multi_value;
|
|
2655
2584
|
var TYPE_SIZES = { i32: 4, i64: 8, f32: 4, f64: 8 };
|
|
2656
2585
|
var gc = (ast, ctx) => {
|
|
2657
2586
|
const types = /* @__PURE__ */ new Map();
|
|
@@ -2895,7 +2824,6 @@ var gc = (ast, ctx) => {
|
|
|
2895
2824
|
});
|
|
2896
2825
|
return ast;
|
|
2897
2826
|
};
|
|
2898
|
-
transforms.gc = gc;
|
|
2899
2827
|
var ref_cast = (ast, ctx) => {
|
|
2900
2828
|
const typeIndices = /* @__PURE__ */ new Map();
|
|
2901
2829
|
let typeIdx = 1;
|
|
@@ -3039,77 +2967,94 @@ var ref_cast = (ast, ctx) => {
|
|
|
3039
2967
|
});
|
|
3040
2968
|
return ast;
|
|
3041
2969
|
};
|
|
3042
|
-
|
|
2970
|
+
var POLYFILLS = [
|
|
2971
|
+
["funcref", ["ref.func", "call_ref", "return_call_ref"], funcref],
|
|
2972
|
+
["sign_ext", ["i32.extend8_s", "i32.extend16_s", "i64.extend8_s", "i64.extend16_s", "i64.extend32_s"], sign_ext],
|
|
2973
|
+
["nontrapping", [
|
|
2974
|
+
"i32.trunc_sat_f32_s",
|
|
2975
|
+
"i32.trunc_sat_f32_u",
|
|
2976
|
+
"i32.trunc_sat_f64_s",
|
|
2977
|
+
"i32.trunc_sat_f64_u",
|
|
2978
|
+
"i64.trunc_sat_f32_s",
|
|
2979
|
+
"i64.trunc_sat_f32_u",
|
|
2980
|
+
"i64.trunc_sat_f64_s",
|
|
2981
|
+
"i64.trunc_sat_f64_u"
|
|
2982
|
+
], nontrapping],
|
|
2983
|
+
["bulk_memory", ["memory.copy", "memory.fill"], bulk_memory],
|
|
2984
|
+
["return_call", ["return_call", "return_call_indirect"], return_call_transform],
|
|
2985
|
+
["i31ref", ["ref.i31", "i31.get_s", "i31.get_u"], i31ref],
|
|
2986
|
+
["extended_const", ["global.get"], extended_const],
|
|
2987
|
+
// global.get in a const initializer — also detected specially
|
|
2988
|
+
["multi_value", [], multi_value],
|
|
2989
|
+
// functions with >1 result — detected by result count
|
|
2990
|
+
["gc", [
|
|
2991
|
+
"struct.new",
|
|
2992
|
+
"struct.get",
|
|
2993
|
+
"struct.set",
|
|
2994
|
+
"array.new",
|
|
2995
|
+
"array.get",
|
|
2996
|
+
"array.set",
|
|
2997
|
+
"array.len",
|
|
2998
|
+
"struct.new_default",
|
|
2999
|
+
"array.new_default",
|
|
3000
|
+
"array.new_fixed",
|
|
3001
|
+
"array.copy"
|
|
3002
|
+
], gc],
|
|
3003
|
+
["ref_cast", ["ref.test", "ref.cast", "br_on_cast", "br_on_cast_fail"], ref_cast]
|
|
3004
|
+
];
|
|
3005
|
+
var FEATURES = Object.fromEntries(POLYFILLS.map((p) => [p[0], p[1]]));
|
|
3006
|
+
var normalize2 = (opts) => {
|
|
3007
|
+
if (opts === false) return {};
|
|
3008
|
+
if (opts !== true && typeof opts !== "string") return { ...opts };
|
|
3009
|
+
const set = typeof opts === "string" ? new Set(opts.split(/\s+/).filter(Boolean)) : null;
|
|
3010
|
+
const m = {};
|
|
3011
|
+
for (const p of POLYFILLS) m[p[0]] = set ? set.has("all") || set.has(p[0]) : true;
|
|
3012
|
+
return m;
|
|
3013
|
+
};
|
|
3014
|
+
var detect = (ast) => {
|
|
3015
|
+
const used = /* @__PURE__ */ new Set();
|
|
3016
|
+
walk(ast, (node) => {
|
|
3017
|
+
if (typeof node !== "string") return;
|
|
3018
|
+
for (const p of POLYFILLS) {
|
|
3019
|
+
const ops = p[1];
|
|
3020
|
+
if (ops.some((op) => node === op || node.startsWith(op + " "))) used.add(p[0]);
|
|
3021
|
+
}
|
|
3022
|
+
});
|
|
3023
|
+
walk(ast, (node) => {
|
|
3024
|
+
if (!Array.isArray(node) || node[0] !== "global") return;
|
|
3025
|
+
for (const init of node) {
|
|
3026
|
+
if (!Array.isArray(init)) continue;
|
|
3027
|
+
if (init[0] === "i32.add" || init[0] === "i32.sub" || init[0] === "i32.mul" || init[0] === "i64.add" || init[0] === "i64.sub" || init[0] === "i64.mul") {
|
|
3028
|
+
walk(init, (inner) => {
|
|
3029
|
+
if (Array.isArray(inner) && inner[0] === "global.get") used.add("extended_const");
|
|
3030
|
+
});
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
});
|
|
3034
|
+
walk(ast, (node) => {
|
|
3035
|
+
if (!Array.isArray(node) || node[0] !== "func") return;
|
|
3036
|
+
let resultCount = 0;
|
|
3037
|
+
for (const part of node) {
|
|
3038
|
+
if (Array.isArray(part) && part[0] === "result") resultCount += part.length - 1;
|
|
3039
|
+
}
|
|
3040
|
+
if (resultCount > 1) used.add("multi_value");
|
|
3041
|
+
});
|
|
3042
|
+
return used;
|
|
3043
|
+
};
|
|
3043
3044
|
function polyfill(ast, opts = true) {
|
|
3044
3045
|
if (typeof ast === "string") ast = parse_default(ast);
|
|
3045
3046
|
ast = clone(ast);
|
|
3046
3047
|
opts = normalize2(opts);
|
|
3047
3048
|
const used = detect(ast);
|
|
3048
3049
|
const ctx = { uid: 0 };
|
|
3049
|
-
for (const
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
}
|
|
3050
|
+
for (const p of POLYFILLS) {
|
|
3051
|
+
const fn = p[2];
|
|
3052
|
+
if (used.has(p[0]) && opts[p[0]] !== false) ast = fn(ast, ctx);
|
|
3053
3053
|
}
|
|
3054
3054
|
return ast;
|
|
3055
3055
|
}
|
|
3056
3056
|
|
|
3057
3057
|
// src/optimize.js
|
|
3058
|
-
var OPTS = {
|
|
3059
|
-
treeshake: true,
|
|
3060
|
-
// remove unused funcs/globals/types/tables
|
|
3061
|
-
fold: true,
|
|
3062
|
-
// constant folding
|
|
3063
|
-
deadcode: true,
|
|
3064
|
-
// eliminate dead code after unreachable/br/return
|
|
3065
|
-
locals: true,
|
|
3066
|
-
// remove unused locals
|
|
3067
|
-
identity: true,
|
|
3068
|
-
// remove identity ops (x + 0 → x)
|
|
3069
|
-
strength: true,
|
|
3070
|
-
// strength reduction (x * 2 → x << 1)
|
|
3071
|
-
branch: true,
|
|
3072
|
-
// simplify constant branches
|
|
3073
|
-
propagate: true,
|
|
3074
|
-
// forward-propagate single-use locals & tiny consts (never inflates)
|
|
3075
|
-
inline: false,
|
|
3076
|
-
// inline tiny functions — can duplicate bodies
|
|
3077
|
-
inlineOnce: true,
|
|
3078
|
-
// inline single-call functions into their lone caller (never duplicates)
|
|
3079
|
-
vacuum: true,
|
|
3080
|
-
// remove nops, drop-of-pure, empty branches
|
|
3081
|
-
mergeBlocks: true,
|
|
3082
|
-
// unwrap `(block $L …)` whose label is never targeted
|
|
3083
|
-
coalesce: true,
|
|
3084
|
-
// share local slots between same-type non-overlapping locals
|
|
3085
|
-
peephole: true,
|
|
3086
|
-
// x-x→0, x&0→0, etc.
|
|
3087
|
-
globals: true,
|
|
3088
|
-
// propagate immutable global constants
|
|
3089
|
-
offset: true,
|
|
3090
|
-
// fold add+const into load/store offset
|
|
3091
|
-
unbranch: true,
|
|
3092
|
-
// remove redundant br at end of own block
|
|
3093
|
-
loopify: true,
|
|
3094
|
-
// collapse block+loop+brif while-idiom into loop+if
|
|
3095
|
-
stripmut: true,
|
|
3096
|
-
// strip mut from never-written globals
|
|
3097
|
-
brif: true,
|
|
3098
|
-
// if-then-br → br_if
|
|
3099
|
-
foldarms: false,
|
|
3100
|
-
// merge identical trailing if arms — can add block wrapper
|
|
3101
|
-
dedupe: true,
|
|
3102
|
-
// eliminate duplicate functions
|
|
3103
|
-
reorder: false,
|
|
3104
|
-
// put hot functions first — no AST reduction
|
|
3105
|
-
dedupTypes: true,
|
|
3106
|
-
// merge identical type definitions
|
|
3107
|
-
packData: true,
|
|
3108
|
-
// trim trailing zeros, merge adjacent data segments
|
|
3109
|
-
minifyImports: false
|
|
3110
|
-
// shorten import names — enable only when you control the host
|
|
3111
|
-
};
|
|
3112
|
-
var ALL2 = Object.keys(OPTS);
|
|
3113
3058
|
var binarySize = (ast) => {
|
|
3114
3059
|
try {
|
|
3115
3060
|
return compile(ast).length;
|
|
@@ -3126,34 +3071,6 @@ var equal = (a, b) => {
|
|
|
3126
3071
|
for (let i = 0; i < a.length; i++) if (!equal(a[i], b[i])) return false;
|
|
3127
3072
|
return true;
|
|
3128
3073
|
};
|
|
3129
|
-
var normalize3 = (opts) => {
|
|
3130
|
-
if (opts === true) return { ...OPTS };
|
|
3131
|
-
if (opts === false) return {};
|
|
3132
|
-
if (typeof opts === "string") {
|
|
3133
|
-
const set = new Set(opts.split(/\s+/).filter(Boolean));
|
|
3134
|
-
if (set.has("all")) return Object.fromEntries(ALL2.map((f) => [f, true]));
|
|
3135
|
-
return Object.fromEntries(ALL2.map((f) => [f, set.has(f)]));
|
|
3136
|
-
}
|
|
3137
|
-
return { ...OPTS, ...opts };
|
|
3138
|
-
};
|
|
3139
|
-
var clone2 = (node) => {
|
|
3140
|
-
if (!Array.isArray(node)) return node;
|
|
3141
|
-
return node.map(clone2);
|
|
3142
|
-
};
|
|
3143
|
-
var walk2 = (node, fn, parent, idx) => {
|
|
3144
|
-
fn(node, parent, idx);
|
|
3145
|
-
if (Array.isArray(node)) for (let i = 0; i < node.length; i++) walk2(node[i], fn, node, i);
|
|
3146
|
-
};
|
|
3147
|
-
var walkPost2 = (node, fn, parent, idx) => {
|
|
3148
|
-
if (Array.isArray(node)) {
|
|
3149
|
-
for (let i = 0; i < node.length; i++) {
|
|
3150
|
-
const result2 = walkPost2(node[i], fn, node, i);
|
|
3151
|
-
if (result2 !== void 0) node[i] = result2;
|
|
3152
|
-
}
|
|
3153
|
-
}
|
|
3154
|
-
const result = fn(node, parent, idx);
|
|
3155
|
-
return result !== void 0 ? result : node;
|
|
3156
|
-
};
|
|
3157
3074
|
var parseIf = (node) => {
|
|
3158
3075
|
let condIdx = 1;
|
|
3159
3076
|
while (condIdx < node.length) {
|
|
@@ -3254,7 +3171,7 @@ var treeshake = (ast) => {
|
|
|
3254
3171
|
markFunc(ref);
|
|
3255
3172
|
}
|
|
3256
3173
|
for (const elem of elems) {
|
|
3257
|
-
|
|
3174
|
+
walk(elem, (n) => {
|
|
3258
3175
|
if (Array.isArray(n) && n[0] === "ref.func") markFunc(n[1]);
|
|
3259
3176
|
else if (typeof n === "string" && n[0] === "$") markFunc(n);
|
|
3260
3177
|
});
|
|
@@ -3276,7 +3193,7 @@ var treeshake = (ast) => {
|
|
|
3276
3193
|
if (entry.scanned) continue;
|
|
3277
3194
|
entry.scanned = true;
|
|
3278
3195
|
if (entry.isImport) continue;
|
|
3279
|
-
|
|
3196
|
+
walk(entry.node, (n) => {
|
|
3280
3197
|
if (!Array.isArray(n)) {
|
|
3281
3198
|
if (typeof n === "string" && n[0] === "$") markFunc(n);
|
|
3282
3199
|
return;
|
|
@@ -3348,120 +3265,120 @@ var i64c = (fn) => (a, b) => fn(a, b) ? 1 : 0;
|
|
|
3348
3265
|
var u64c = (fn) => (a, b) => fn(BigInt.asUintN(64, a), BigInt.asUintN(64, b)) ? 1 : 0;
|
|
3349
3266
|
var FOLDABLE = {
|
|
3350
3267
|
// i32 arithmetic
|
|
3351
|
-
"i32.add":
|
|
3352
|
-
"i32.sub":
|
|
3353
|
-
"i32.mul":
|
|
3354
|
-
"i32.div_s":
|
|
3355
|
-
"i32.div_u":
|
|
3356
|
-
"i32.rem_s":
|
|
3357
|
-
"i32.rem_u":
|
|
3358
|
-
"i32.and":
|
|
3359
|
-
"i32.or":
|
|
3360
|
-
"i32.xor":
|
|
3361
|
-
"i32.shl":
|
|
3362
|
-
"i32.shr_s":
|
|
3363
|
-
"i32.shr_u":
|
|
3364
|
-
"i32.rotl":
|
|
3268
|
+
"i32.add": (a, b) => a + b | 0,
|
|
3269
|
+
"i32.sub": (a, b) => a - b | 0,
|
|
3270
|
+
"i32.mul": (a, b) => Math.imul(a, b),
|
|
3271
|
+
"i32.div_s": (a, b) => b !== 0 ? a / b | 0 : null,
|
|
3272
|
+
"i32.div_u": (a, b) => b !== 0 ? (a >>> 0) / (b >>> 0) | 0 : null,
|
|
3273
|
+
"i32.rem_s": (a, b) => b !== 0 ? a % b | 0 : null,
|
|
3274
|
+
"i32.rem_u": (a, b) => b !== 0 ? (a >>> 0) % (b >>> 0) | 0 : null,
|
|
3275
|
+
"i32.and": (a, b) => a & b,
|
|
3276
|
+
"i32.or": (a, b) => a | b,
|
|
3277
|
+
"i32.xor": (a, b) => a ^ b,
|
|
3278
|
+
"i32.shl": (a, b) => a << (b & 31),
|
|
3279
|
+
"i32.shr_s": (a, b) => a >> (b & 31),
|
|
3280
|
+
"i32.shr_u": (a, b) => a >>> (b & 31),
|
|
3281
|
+
"i32.rotl": (a, b) => {
|
|
3365
3282
|
b &= 31;
|
|
3366
3283
|
return a << b | a >>> 32 - b | 0;
|
|
3367
|
-
},
|
|
3368
|
-
"i32.rotr":
|
|
3284
|
+
},
|
|
3285
|
+
"i32.rotr": (a, b) => {
|
|
3369
3286
|
b &= 31;
|
|
3370
3287
|
return a >>> b | a << 32 - b | 0;
|
|
3371
|
-
},
|
|
3372
|
-
"i32.eq":
|
|
3373
|
-
"i32.ne":
|
|
3374
|
-
"i32.lt_s":
|
|
3375
|
-
"i32.lt_u":
|
|
3376
|
-
"i32.gt_s":
|
|
3377
|
-
"i32.gt_u":
|
|
3378
|
-
"i32.le_s":
|
|
3379
|
-
"i32.le_u":
|
|
3380
|
-
"i32.ge_s":
|
|
3381
|
-
"i32.ge_u":
|
|
3382
|
-
"i32.eqz":
|
|
3383
|
-
"i32.clz":
|
|
3384
|
-
"i32.ctz":
|
|
3385
|
-
"i32.popcnt":
|
|
3288
|
+
},
|
|
3289
|
+
"i32.eq": i32c((a, b) => a === b),
|
|
3290
|
+
"i32.ne": i32c((a, b) => a !== b),
|
|
3291
|
+
"i32.lt_s": i32c((a, b) => a < b),
|
|
3292
|
+
"i32.lt_u": u32c((a, b) => a < b),
|
|
3293
|
+
"i32.gt_s": i32c((a, b) => a > b),
|
|
3294
|
+
"i32.gt_u": u32c((a, b) => a > b),
|
|
3295
|
+
"i32.le_s": i32c((a, b) => a <= b),
|
|
3296
|
+
"i32.le_u": u32c((a, b) => a <= b),
|
|
3297
|
+
"i32.ge_s": i32c((a, b) => a >= b),
|
|
3298
|
+
"i32.ge_u": u32c((a, b) => a >= b),
|
|
3299
|
+
"i32.eqz": (a) => a === 0 ? 1 : 0,
|
|
3300
|
+
"i32.clz": (a) => Math.clz32(a),
|
|
3301
|
+
"i32.ctz": (a) => a === 0 ? 32 : 31 - Math.clz32(a & -a),
|
|
3302
|
+
"i32.popcnt": (a) => {
|
|
3386
3303
|
let c = 0;
|
|
3387
3304
|
while (a) {
|
|
3388
3305
|
c += a & 1;
|
|
3389
3306
|
a >>>= 1;
|
|
3390
3307
|
}
|
|
3391
3308
|
return c;
|
|
3392
|
-
},
|
|
3393
|
-
"i32.wrap_i64":
|
|
3394
|
-
"i32.extend8_s":
|
|
3395
|
-
"i32.extend16_s":
|
|
3309
|
+
},
|
|
3310
|
+
"i32.wrap_i64": (a) => Number(BigInt.asIntN(32, a)),
|
|
3311
|
+
"i32.extend8_s": (a) => a << 24 >> 24,
|
|
3312
|
+
"i32.extend16_s": (a) => a << 16 >> 16,
|
|
3396
3313
|
// i64 (using BigInt)
|
|
3397
|
-
"i64.add":
|
|
3398
|
-
"i64.sub":
|
|
3399
|
-
"i64.mul":
|
|
3400
|
-
"i64.div_s":
|
|
3401
|
-
"i64.div_u":
|
|
3402
|
-
"i64.rem_s":
|
|
3403
|
-
"i64.rem_u":
|
|
3404
|
-
"i64.and":
|
|
3405
|
-
"i64.or":
|
|
3406
|
-
"i64.xor":
|
|
3407
|
-
"i64.shl":
|
|
3408
|
-
"i64.shr_s":
|
|
3409
|
-
"i64.shr_u":
|
|
3410
|
-
"i64.eq":
|
|
3411
|
-
"i64.ne":
|
|
3412
|
-
"i64.lt_s":
|
|
3413
|
-
"i64.lt_u":
|
|
3414
|
-
"i64.gt_s":
|
|
3415
|
-
"i64.gt_u":
|
|
3416
|
-
"i64.le_s":
|
|
3417
|
-
"i64.le_u":
|
|
3418
|
-
"i64.ge_s":
|
|
3419
|
-
"i64.ge_u":
|
|
3420
|
-
"i64.eqz":
|
|
3421
|
-
"i64.extend_i32_s":
|
|
3422
|
-
"i64.extend_i32_u":
|
|
3423
|
-
"i64.extend8_s":
|
|
3424
|
-
"i64.extend16_s":
|
|
3425
|
-
"i64.extend32_s":
|
|
3314
|
+
"i64.add": (a, b) => BigInt.asIntN(64, a + b),
|
|
3315
|
+
"i64.sub": (a, b) => BigInt.asIntN(64, a - b),
|
|
3316
|
+
"i64.mul": (a, b) => BigInt.asIntN(64, a * b),
|
|
3317
|
+
"i64.div_s": (a, b) => b !== 0n ? BigInt.asIntN(64, a / b) : null,
|
|
3318
|
+
"i64.div_u": (a, b) => b !== 0n ? BigInt.asUintN(64, BigInt.asUintN(64, a) / BigInt.asUintN(64, b)) : null,
|
|
3319
|
+
"i64.rem_s": (a, b) => b !== 0n ? BigInt.asIntN(64, a % b) : null,
|
|
3320
|
+
"i64.rem_u": (a, b) => b !== 0n ? BigInt.asUintN(64, BigInt.asUintN(64, a) % BigInt.asUintN(64, b)) : null,
|
|
3321
|
+
"i64.and": (a, b) => BigInt.asIntN(64, a & b),
|
|
3322
|
+
"i64.or": (a, b) => BigInt.asIntN(64, a | b),
|
|
3323
|
+
"i64.xor": (a, b) => BigInt.asIntN(64, a ^ b),
|
|
3324
|
+
"i64.shl": (a, b) => BigInt.asIntN(64, a << (b & 63n)),
|
|
3325
|
+
"i64.shr_s": (a, b) => BigInt.asIntN(64, a >> (b & 63n)),
|
|
3326
|
+
"i64.shr_u": (a, b) => BigInt.asUintN(64, BigInt.asUintN(64, a) >> (b & 63n)),
|
|
3327
|
+
"i64.eq": i64c((a, b) => a === b),
|
|
3328
|
+
"i64.ne": i64c((a, b) => a !== b),
|
|
3329
|
+
"i64.lt_s": i64c((a, b) => a < b),
|
|
3330
|
+
"i64.lt_u": u64c((a, b) => a < b),
|
|
3331
|
+
"i64.gt_s": i64c((a, b) => a > b),
|
|
3332
|
+
"i64.gt_u": u64c((a, b) => a > b),
|
|
3333
|
+
"i64.le_s": i64c((a, b) => a <= b),
|
|
3334
|
+
"i64.le_u": u64c((a, b) => a <= b),
|
|
3335
|
+
"i64.ge_s": i64c((a, b) => a >= b),
|
|
3336
|
+
"i64.ge_u": u64c((a, b) => a >= b),
|
|
3337
|
+
"i64.eqz": (a) => a === 0n ? 1 : 0,
|
|
3338
|
+
"i64.extend_i32_s": (a) => BigInt(a),
|
|
3339
|
+
"i64.extend_i32_u": (a) => BigInt(a >>> 0),
|
|
3340
|
+
"i64.extend8_s": (a) => BigInt.asIntN(64, BigInt.asIntN(8, a)),
|
|
3341
|
+
"i64.extend16_s": (a) => BigInt.asIntN(64, BigInt.asIntN(16, a)),
|
|
3342
|
+
"i64.extend32_s": (a) => BigInt.asIntN(64, BigInt.asIntN(32, a)),
|
|
3426
3343
|
// f32/f64 (NaN/precision-aware via Math.fround)
|
|
3427
|
-
"f32.add":
|
|
3428
|
-
"f32.sub":
|
|
3429
|
-
"f32.mul":
|
|
3430
|
-
"f32.div":
|
|
3431
|
-
"f32.neg":
|
|
3432
|
-
"f32.abs":
|
|
3433
|
-
"f32.sqrt":
|
|
3434
|
-
"f32.ceil":
|
|
3435
|
-
"f32.floor":
|
|
3436
|
-
"f32.trunc":
|
|
3437
|
-
"f32.nearest":
|
|
3438
|
-
"f64.add":
|
|
3439
|
-
"f64.sub":
|
|
3440
|
-
"f64.mul":
|
|
3441
|
-
"f64.div":
|
|
3442
|
-
"f64.neg":
|
|
3443
|
-
"f64.abs":
|
|
3444
|
-
"f64.sqrt":
|
|
3445
|
-
"f64.ceil":
|
|
3446
|
-
"f64.floor":
|
|
3447
|
-
"f64.trunc":
|
|
3448
|
-
"f64.nearest":
|
|
3344
|
+
"f32.add": (a, b) => Math.fround(a + b),
|
|
3345
|
+
"f32.sub": (a, b) => Math.fround(a - b),
|
|
3346
|
+
"f32.mul": (a, b) => Math.fround(a * b),
|
|
3347
|
+
"f32.div": (a, b) => Math.fround(a / b),
|
|
3348
|
+
"f32.neg": (a) => Math.fround(-a),
|
|
3349
|
+
"f32.abs": (a) => Math.fround(Math.abs(a)),
|
|
3350
|
+
"f32.sqrt": (a) => Math.fround(Math.sqrt(a)),
|
|
3351
|
+
"f32.ceil": (a) => Math.fround(Math.ceil(a)),
|
|
3352
|
+
"f32.floor": (a) => Math.fround(Math.floor(a)),
|
|
3353
|
+
"f32.trunc": (a) => Math.fround(Math.trunc(a)),
|
|
3354
|
+
"f32.nearest": (a) => Math.fround(roundEven(a)),
|
|
3355
|
+
"f64.add": (a, b) => a + b,
|
|
3356
|
+
"f64.sub": (a, b) => a - b,
|
|
3357
|
+
"f64.mul": (a, b) => a * b,
|
|
3358
|
+
"f64.div": (a, b) => a / b,
|
|
3359
|
+
"f64.neg": (a) => -a,
|
|
3360
|
+
"f64.abs": Math.abs,
|
|
3361
|
+
"f64.sqrt": Math.sqrt,
|
|
3362
|
+
"f64.ceil": Math.ceil,
|
|
3363
|
+
"f64.floor": Math.floor,
|
|
3364
|
+
"f64.trunc": Math.trunc,
|
|
3365
|
+
"f64.nearest": roundEven,
|
|
3449
3366
|
// Bit-exact reinterprets (preserve NaN payloads)
|
|
3450
|
-
"i32.reinterpret_f32":
|
|
3451
|
-
"f32.reinterpret_i32":
|
|
3452
|
-
"i64.reinterpret_f64":
|
|
3453
|
-
"f64.reinterpret_i64":
|
|
3367
|
+
"i32.reinterpret_f32": i32FromF32,
|
|
3368
|
+
"f32.reinterpret_i32": f32FromI32,
|
|
3369
|
+
"i64.reinterpret_f64": i64FromF64,
|
|
3370
|
+
"f64.reinterpret_i64": f64FromI64,
|
|
3454
3371
|
// Numeric conversions (value-preserving where representable)
|
|
3455
|
-
"f32.convert_i32_s":
|
|
3456
|
-
"f32.convert_i32_u":
|
|
3457
|
-
"f32.convert_i64_s":
|
|
3458
|
-
"f32.convert_i64_u":
|
|
3459
|
-
"f64.convert_i32_s":
|
|
3460
|
-
"f64.convert_i32_u":
|
|
3461
|
-
"f64.convert_i64_s":
|
|
3462
|
-
"f64.convert_i64_u":
|
|
3463
|
-
"f32.demote_f64":
|
|
3464
|
-
"f64.promote_f32":
|
|
3372
|
+
"f32.convert_i32_s": (a) => Math.fround(a | 0),
|
|
3373
|
+
"f32.convert_i32_u": (a) => Math.fround(a >>> 0),
|
|
3374
|
+
"f32.convert_i64_s": (a) => Math.fround(Number(BigInt.asIntN(64, a))),
|
|
3375
|
+
"f32.convert_i64_u": (a) => Math.fround(Number(BigInt.asUintN(64, a))),
|
|
3376
|
+
"f64.convert_i32_s": (a) => a | 0,
|
|
3377
|
+
"f64.convert_i32_u": (a) => a >>> 0,
|
|
3378
|
+
"f64.convert_i64_s": (a) => Number(BigInt.asIntN(64, a)),
|
|
3379
|
+
"f64.convert_i64_u": (a) => Number(BigInt.asUintN(64, a)),
|
|
3380
|
+
"f32.demote_f64": (a) => Math.fround(a),
|
|
3381
|
+
"f64.promote_f32": (a) => Math.fround(a)
|
|
3465
3382
|
};
|
|
3466
3383
|
var _parseNanF64 = (s, i = s?.indexOf?.("nan")) => {
|
|
3467
3384
|
if (i < 0 || i == null) return null;
|
|
@@ -3498,24 +3415,23 @@ var makeConst = (type, value) => {
|
|
|
3498
3415
|
return null;
|
|
3499
3416
|
};
|
|
3500
3417
|
var fold = (ast) => {
|
|
3501
|
-
return
|
|
3418
|
+
return walkPost(ast, (node) => {
|
|
3502
3419
|
if (!Array.isArray(node)) return;
|
|
3503
|
-
const
|
|
3504
|
-
if (!
|
|
3505
|
-
const [fn, t] = entry;
|
|
3420
|
+
const fn = FOLDABLE[node[0]];
|
|
3421
|
+
if (!fn) return;
|
|
3506
3422
|
if (fn.length === 1 && node.length === 2) {
|
|
3507
3423
|
const a = getConst(node[1]);
|
|
3508
3424
|
if (!a) return;
|
|
3509
3425
|
const r = fn(a.value);
|
|
3510
3426
|
if (r === null) return;
|
|
3511
|
-
return makeConst(
|
|
3427
|
+
return makeConst(resultType(node[0]), r);
|
|
3512
3428
|
}
|
|
3513
3429
|
if (fn.length === 2 && node.length === 3) {
|
|
3514
3430
|
const a = getConst(node[1]), b = getConst(node[2]);
|
|
3515
3431
|
if (!a || !b) return;
|
|
3516
3432
|
const r = fn(a.value, b.value);
|
|
3517
3433
|
if (r === null) return;
|
|
3518
|
-
return makeConst(
|
|
3434
|
+
return makeConst(resultType(node[0]), r);
|
|
3519
3435
|
}
|
|
3520
3436
|
});
|
|
3521
3437
|
};
|
|
@@ -3561,7 +3477,7 @@ var IDENTITIES = {
|
|
|
3561
3477
|
// f * 1 → x (careful with NaN, skip for floats)
|
|
3562
3478
|
};
|
|
3563
3479
|
var identity = (ast) => {
|
|
3564
|
-
return
|
|
3480
|
+
return walkPost(ast, (node) => {
|
|
3565
3481
|
if (!Array.isArray(node) || node.length !== 3) return;
|
|
3566
3482
|
const fn = IDENTITIES[node[0]];
|
|
3567
3483
|
if (!fn) return;
|
|
@@ -3571,7 +3487,7 @@ var identity = (ast) => {
|
|
|
3571
3487
|
});
|
|
3572
3488
|
};
|
|
3573
3489
|
var strength = (ast) => {
|
|
3574
|
-
return
|
|
3490
|
+
return walkPost(ast, (node) => {
|
|
3575
3491
|
if (!Array.isArray(node) || node.length !== 3) return;
|
|
3576
3492
|
const [op, a, b] = node;
|
|
3577
3493
|
if (op === "i32.mul") {
|
|
@@ -3627,7 +3543,7 @@ var strength = (ast) => {
|
|
|
3627
3543
|
});
|
|
3628
3544
|
};
|
|
3629
3545
|
var branch = (ast) => {
|
|
3630
|
-
return
|
|
3546
|
+
return walkPost(ast, (node) => {
|
|
3631
3547
|
if (!Array.isArray(node)) return;
|
|
3632
3548
|
const op = node[0];
|
|
3633
3549
|
if (op === "if") {
|
|
@@ -3659,7 +3575,7 @@ var branch = (ast) => {
|
|
|
3659
3575
|
};
|
|
3660
3576
|
var TERMINATORS = /* @__PURE__ */ new Set(["unreachable", "return", "br", "br_table"]);
|
|
3661
3577
|
var deadcode = (ast) => {
|
|
3662
|
-
|
|
3578
|
+
walk(ast, (node) => {
|
|
3663
3579
|
if (!Array.isArray(node)) return;
|
|
3664
3580
|
const kind = node[0];
|
|
3665
3581
|
if (kind === "func" || kind === "block" || kind === "loop") {
|
|
@@ -3705,7 +3621,7 @@ var eliminateDeadInBlock = (block) => {
|
|
|
3705
3621
|
}
|
|
3706
3622
|
};
|
|
3707
3623
|
var localReuse = (ast) => {
|
|
3708
|
-
|
|
3624
|
+
walk(ast, (node) => {
|
|
3709
3625
|
if (!Array.isArray(node) || node[0] !== "func") return;
|
|
3710
3626
|
const localDecls = [];
|
|
3711
3627
|
const localTypes = /* @__PURE__ */ new Map();
|
|
@@ -3726,7 +3642,7 @@ var localReuse = (ast) => {
|
|
|
3726
3642
|
}
|
|
3727
3643
|
}
|
|
3728
3644
|
}
|
|
3729
|
-
|
|
3645
|
+
walk(node, (n) => {
|
|
3730
3646
|
if (!Array.isArray(n)) return;
|
|
3731
3647
|
const op = n[0];
|
|
3732
3648
|
if (op === "local.get" || op === "local.set" || op === "local.tee") {
|
|
@@ -3799,7 +3715,7 @@ var countLocalUses = (node) => {
|
|
|
3799
3715
|
if (!counts.has(name2)) counts.set(name2, { gets: 0, sets: 0, tees: 0 });
|
|
3800
3716
|
return counts.get(name2);
|
|
3801
3717
|
};
|
|
3802
|
-
|
|
3718
|
+
walk(node, (n) => {
|
|
3803
3719
|
if (!Array.isArray(n) || n.length < 2 || typeof n[1] !== "string") return;
|
|
3804
3720
|
if (n[0] === "local.get") ensure(n[1]).gets++;
|
|
3805
3721
|
else if (n[0] === "local.set") ensure(n[1]).sets++;
|
|
@@ -3824,7 +3740,7 @@ var canSubst = (k) => k.pure && k.singleUse || isTinyConst(k.val);
|
|
|
3824
3740
|
var purgeRefs = (known, name2) => {
|
|
3825
3741
|
for (const [key, tracked] of known) {
|
|
3826
3742
|
let refs = false;
|
|
3827
|
-
|
|
3743
|
+
walk(tracked.val, (n) => {
|
|
3828
3744
|
if (Array.isArray(n) && (n[0] === "local.get" || n[0] === "local.tee") && n[1] === name2) refs = true;
|
|
3829
3745
|
});
|
|
3830
3746
|
if (refs) known.delete(key);
|
|
@@ -3854,13 +3770,13 @@ var substGets = (node, known) => {
|
|
|
3854
3770
|
const op = node[0];
|
|
3855
3771
|
if (op === "local.get" && node.length === 2) {
|
|
3856
3772
|
const k = typeof node[1] === "string" && known.get(node[1]);
|
|
3857
|
-
if (k && canSubst(k)) return
|
|
3773
|
+
if (k && canSubst(k)) return clone(k.val);
|
|
3858
3774
|
return node;
|
|
3859
3775
|
}
|
|
3860
3776
|
let inner = known;
|
|
3861
3777
|
if (op === "block" || op === "loop" || op === "if") {
|
|
3862
3778
|
let cloned = null;
|
|
3863
|
-
|
|
3779
|
+
walk(node, (n) => {
|
|
3864
3780
|
if (!Array.isArray(n)) return;
|
|
3865
3781
|
if ((n[0] === "local.set" || n[0] === "local.tee") && typeof n[1] === "string" && known.has(n[1])) {
|
|
3866
3782
|
if (!cloned) cloned = new Map(known);
|
|
@@ -3873,7 +3789,7 @@ var substGets = (node, known) => {
|
|
|
3873
3789
|
const r = substGets(node[i], inner);
|
|
3874
3790
|
if (r !== node[i]) node[i] = r;
|
|
3875
3791
|
if (i + 1 < node.length && Array.isArray(node[i])) {
|
|
3876
|
-
|
|
3792
|
+
walk(node[i], (n) => {
|
|
3877
3793
|
if (!Array.isArray(n)) return;
|
|
3878
3794
|
if ((n[0] === "local.set" || n[0] === "local.tee") && typeof n[1] === "string") {
|
|
3879
3795
|
if (inner === known) inner = new Map(known);
|
|
@@ -3920,7 +3836,7 @@ var forwardPropagate = (funcNode, params, useCounts) => {
|
|
|
3920
3836
|
if (op === "local.get" && instr2.length === 2 && typeof instr2[1] === "string") {
|
|
3921
3837
|
const tracked = known.get(instr2[1]);
|
|
3922
3838
|
if (tracked && canSubst(tracked)) {
|
|
3923
|
-
const replacement =
|
|
3839
|
+
const replacement = clone(tracked.val);
|
|
3924
3840
|
instr2.length = 0;
|
|
3925
3841
|
instr2.push(...Array.isArray(replacement) ? replacement : [replacement]);
|
|
3926
3842
|
changed = true;
|
|
@@ -3928,10 +3844,10 @@ var forwardPropagate = (funcNode, params, useCounts) => {
|
|
|
3928
3844
|
}
|
|
3929
3845
|
}
|
|
3930
3846
|
if (op !== "block" && op !== "loop" && op !== "if") {
|
|
3931
|
-
const prev =
|
|
3847
|
+
const prev = clone(instr2);
|
|
3932
3848
|
substGets(instr2, known);
|
|
3933
3849
|
if (!equal(prev, instr2)) changed = true;
|
|
3934
|
-
|
|
3850
|
+
walk(instr2, (n) => {
|
|
3935
3851
|
if (Array.isArray(n) && (n[0] === "local.set" || n[0] === "local.tee") && typeof n[1] === "string") {
|
|
3936
3852
|
known.delete(n[1]);
|
|
3937
3853
|
purgeRefs(known, n[1]);
|
|
@@ -3955,7 +3871,7 @@ var eliminateSetGetPairs = (funcNode, params, useCounts) => {
|
|
|
3955
3871
|
if (getNode[1] !== name2 || params.has(name2)) continue;
|
|
3956
3872
|
const uses = useCounts.get(name2) || { gets: 0, sets: 0, tees: 0 };
|
|
3957
3873
|
if (uses.sets !== 1 || uses.gets !== 1 || uses.tees !== 0) continue;
|
|
3958
|
-
const expr2 =
|
|
3874
|
+
const expr2 = clone(setNode[2]);
|
|
3959
3875
|
funcNode.splice(i, 2, ...Array.isArray(expr2) ? [expr2] : [expr2]);
|
|
3960
3876
|
changed = true;
|
|
3961
3877
|
i--;
|
|
@@ -3973,7 +3889,7 @@ var createLocalTees = (funcNode, params, useCounts) => {
|
|
|
3973
3889
|
if (getNode[1] !== name2 || params.has(name2)) continue;
|
|
3974
3890
|
const uses = useCounts.get(name2) || { gets: 0, sets: 0, tees: 0 };
|
|
3975
3891
|
if (uses.sets + uses.gets + uses.tees <= 2) continue;
|
|
3976
|
-
funcNode.splice(i, 2, ["local.tee", name2,
|
|
3892
|
+
funcNode.splice(i, 2, ["local.tee", name2, clone(setNode[2])]);
|
|
3977
3893
|
changed = true;
|
|
3978
3894
|
}
|
|
3979
3895
|
return changed;
|
|
@@ -3987,9 +3903,16 @@ var eliminateDeadStores = (funcNode, params, useCounts) => {
|
|
|
3987
3903
|
const name2 = typeof sub[1] === "string" ? sub[1] : null;
|
|
3988
3904
|
if (!name2 || params.has(name2)) continue;
|
|
3989
3905
|
const uses = getPostUseCount(name2);
|
|
3990
|
-
if (sub[0] === "local.set" && uses.gets === 0 && uses.tees === 0
|
|
3991
|
-
|
|
3992
|
-
|
|
3906
|
+
if (sub[0] === "local.set" && uses.gets === 0 && uses.tees === 0) {
|
|
3907
|
+
if (sub.length === 3) {
|
|
3908
|
+
if (isPure(sub[2])) {
|
|
3909
|
+
funcNode.splice(i, 1);
|
|
3910
|
+
changed = true;
|
|
3911
|
+
}
|
|
3912
|
+
} else if (sub.length === 2) {
|
|
3913
|
+
funcNode[i] = ["drop"];
|
|
3914
|
+
changed = true;
|
|
3915
|
+
}
|
|
3993
3916
|
} else if (sub[0] === "local" && name2[0] === "$" && uses.gets === 0 && uses.sets === 0 && uses.tees === 0) {
|
|
3994
3917
|
funcNode.splice(i, 1);
|
|
3995
3918
|
changed = true;
|
|
@@ -3999,13 +3922,13 @@ var eliminateDeadStores = (funcNode, params, useCounts) => {
|
|
|
3999
3922
|
};
|
|
4000
3923
|
var isScopeNode = (n) => Array.isArray(n) && (n[0] === "func" || n[0] === "block" || n[0] === "loop" || n[0] === "then" || n[0] === "else");
|
|
4001
3924
|
var propagate = (ast) => {
|
|
4002
|
-
|
|
3925
|
+
walk(ast, (funcNode) => {
|
|
4003
3926
|
if (!Array.isArray(funcNode) || funcNode[0] !== "func") return;
|
|
4004
3927
|
const params = /* @__PURE__ */ new Set();
|
|
4005
3928
|
for (const sub of funcNode)
|
|
4006
3929
|
if (Array.isArray(sub) && sub[0] === "param" && typeof sub[1] === "string") params.add(sub[1]);
|
|
4007
3930
|
const scopes = [];
|
|
4008
|
-
|
|
3931
|
+
walkPost(funcNode, (n) => {
|
|
4009
3932
|
if (isScopeNode(n)) scopes.push(n);
|
|
4010
3933
|
});
|
|
4011
3934
|
for (let round = 0; round < 6; round++) {
|
|
@@ -4055,7 +3978,7 @@ var inline = (ast) => {
|
|
|
4055
3978
|
const paramNames = new Set(params.map((p) => p.name));
|
|
4056
3979
|
let mutatesParam = false;
|
|
4057
3980
|
let hasReturn = false;
|
|
4058
|
-
|
|
3981
|
+
walk(body[0], (n) => {
|
|
4059
3982
|
if (!Array.isArray(n)) return;
|
|
4060
3983
|
if ((n[0] === "local.set" || n[0] === "local.tee") && paramNames.has(n[1])) {
|
|
4061
3984
|
mutatesParam = true;
|
|
@@ -4070,22 +3993,22 @@ var inline = (ast) => {
|
|
|
4070
3993
|
}
|
|
4071
3994
|
}
|
|
4072
3995
|
if (inlinable.size === 0) return ast;
|
|
4073
|
-
|
|
3996
|
+
walkPost(ast, (node) => {
|
|
4074
3997
|
if (!Array.isArray(node) || node[0] !== "call") return;
|
|
4075
3998
|
const fname = node[1];
|
|
4076
3999
|
if (!inlinable.has(fname)) return;
|
|
4077
4000
|
const { body, params } = inlinable.get(fname);
|
|
4078
4001
|
const args = node.slice(2);
|
|
4079
4002
|
if (params.length === 0) {
|
|
4080
|
-
return
|
|
4003
|
+
return clone(body);
|
|
4081
4004
|
}
|
|
4082
|
-
const substituted =
|
|
4083
|
-
|
|
4005
|
+
const substituted = clone(body);
|
|
4006
|
+
walkPost(substituted, (n) => {
|
|
4084
4007
|
if (!Array.isArray(n) || n[0] !== "local.get") return;
|
|
4085
4008
|
const local = n[1];
|
|
4086
4009
|
const paramIdx = params.findIndex((p) => p.name === local);
|
|
4087
4010
|
if (paramIdx !== -1 && args[paramIdx]) {
|
|
4088
|
-
return
|
|
4011
|
+
return clone(args[paramIdx]);
|
|
4089
4012
|
}
|
|
4090
4013
|
});
|
|
4091
4014
|
return substituted;
|
|
@@ -4224,13 +4147,13 @@ var inlineOnce = (ast) => {
|
|
|
4224
4147
|
if (!calleeName) break;
|
|
4225
4148
|
const callee = funcByName.get(calleeName);
|
|
4226
4149
|
const params = [], locals = [];
|
|
4227
|
-
let
|
|
4150
|
+
let inlResult = null;
|
|
4228
4151
|
for (let i = 2; i < callee.length; i++) {
|
|
4229
4152
|
const c = callee[i];
|
|
4230
4153
|
if (typeof c === "string" || !Array.isArray(c)) continue;
|
|
4231
4154
|
if (c[0] === "param") params.push({ name: c[1], type: c[2] });
|
|
4232
4155
|
else if (c[0] === "result") {
|
|
4233
|
-
if (c.length > 1)
|
|
4156
|
+
if (c.length > 1) inlResult = c[1];
|
|
4234
4157
|
} else if (c[0] === "local") locals.push({ name: c[1], type: c[2] });
|
|
4235
4158
|
else if (c[0] === "export" || c[0] === "type") continue;
|
|
4236
4159
|
else break;
|
|
@@ -4266,7 +4189,7 @@ var inlineOnce = (ast) => {
|
|
|
4266
4189
|
if (fn === callee || done) continue;
|
|
4267
4190
|
const start = bodyStart(fn);
|
|
4268
4191
|
for (let i = start; i < fn.length; i++) {
|
|
4269
|
-
const replaced =
|
|
4192
|
+
const replaced = walkPost(fn[i], (n) => {
|
|
4270
4193
|
if (done || !Array.isArray(n) || n[0] !== "call" || n[1] !== calleeName) return;
|
|
4271
4194
|
const args = n.slice(2);
|
|
4272
4195
|
if (args.length !== params.length) return;
|
|
@@ -4274,7 +4197,7 @@ var inlineOnce = (ast) => {
|
|
|
4274
4197
|
const resets = locals.filter((l) => needsReset(cBody, l.name)).map((l) => ["local.set", rename.get(l.name), zeroFor(l.type)]);
|
|
4275
4198
|
const inner = cBody.map(sub);
|
|
4276
4199
|
done = true;
|
|
4277
|
-
return
|
|
4200
|
+
return inlResult ? ["block", exit, ["result", inlResult], ...setup, ...resets, ...inner] : ["block", exit, ...setup, ...resets, ...inner];
|
|
4278
4201
|
});
|
|
4279
4202
|
if (replaced !== fn[i]) fn[i] = replaced;
|
|
4280
4203
|
if (done) {
|
|
@@ -4313,6 +4236,16 @@ var targetsLabel = (body, label) => {
|
|
|
4313
4236
|
}
|
|
4314
4237
|
} else break;
|
|
4315
4238
|
}
|
|
4239
|
+
} else if (op === "catch" || op === "catch_ref") {
|
|
4240
|
+
if (n[2] === label) {
|
|
4241
|
+
found = true;
|
|
4242
|
+
return;
|
|
4243
|
+
}
|
|
4244
|
+
} else if (op === "catch_all" || op === "catch_all_ref") {
|
|
4245
|
+
if (n[1] === label) {
|
|
4246
|
+
found = true;
|
|
4247
|
+
return;
|
|
4248
|
+
}
|
|
4316
4249
|
}
|
|
4317
4250
|
}
|
|
4318
4251
|
for (let i = 1; i < n.length; i++) search(n[i], inner);
|
|
@@ -4321,7 +4254,7 @@ var targetsLabel = (body, label) => {
|
|
|
4321
4254
|
return found;
|
|
4322
4255
|
};
|
|
4323
4256
|
var mergeBlocks = (ast) => {
|
|
4324
|
-
|
|
4257
|
+
walkPost(ast, (node) => {
|
|
4325
4258
|
if (!Array.isArray(node) || node[0] !== "block") return;
|
|
4326
4259
|
let bi = 1, label = null;
|
|
4327
4260
|
if (typeof node[1] === "string" && node[1][0] === "$") {
|
|
@@ -4350,7 +4283,7 @@ var mergeBlocks = (ast) => {
|
|
|
4350
4283
|
node.length = 0;
|
|
4351
4284
|
for (const tok of only) node.push(tok);
|
|
4352
4285
|
});
|
|
4353
|
-
|
|
4286
|
+
walk(ast, (node) => {
|
|
4354
4287
|
if (!isScopeNode(node)) return;
|
|
4355
4288
|
let i = 1;
|
|
4356
4289
|
while (i < node.length) {
|
|
@@ -4424,7 +4357,7 @@ var mergeBlocks = (ast) => {
|
|
|
4424
4357
|
return ast;
|
|
4425
4358
|
};
|
|
4426
4359
|
var coalesceLocals = (ast) => {
|
|
4427
|
-
|
|
4360
|
+
walk(ast, (funcNode) => {
|
|
4428
4361
|
if (!Array.isArray(funcNode) || funcNode[0] !== "func") return;
|
|
4429
4362
|
const decls = /* @__PURE__ */ new Map();
|
|
4430
4363
|
for (const sub of funcNode) {
|
|
@@ -4496,7 +4429,7 @@ var coalesceLocals = (ast) => {
|
|
|
4496
4429
|
} else slots.push({ primary: name2, type, end: range.end });
|
|
4497
4430
|
}
|
|
4498
4431
|
if (rename.size === 0) return;
|
|
4499
|
-
|
|
4432
|
+
walk(funcNode, (n) => {
|
|
4500
4433
|
if (Array.isArray(n) && (n[0] === "local.get" || n[0] === "local.set" || n[0] === "local.tee") && rename.has(n[1])) {
|
|
4501
4434
|
n[1] = rename.get(n[1]);
|
|
4502
4435
|
}
|
|
@@ -4505,7 +4438,7 @@ var coalesceLocals = (ast) => {
|
|
|
4505
4438
|
return ast;
|
|
4506
4439
|
};
|
|
4507
4440
|
var vacuum = (ast) => {
|
|
4508
|
-
return
|
|
4441
|
+
return walkPost(ast, (node) => {
|
|
4509
4442
|
if (!Array.isArray(node)) return;
|
|
4510
4443
|
const op = node[0];
|
|
4511
4444
|
if (op === "nop") return ["nop"];
|
|
@@ -4604,7 +4537,7 @@ var PEEPHOLE = {
|
|
|
4604
4537
|
"local.set": (a, b) => Array.isArray(b) && b[0] === "local.get" && b[1] === a ? ["nop"] : null
|
|
4605
4538
|
};
|
|
4606
4539
|
var peephole = (ast) => {
|
|
4607
|
-
return
|
|
4540
|
+
return walkPost(ast, (node) => {
|
|
4608
4541
|
if (!Array.isArray(node) || node.length !== 3) return;
|
|
4609
4542
|
const fn = PEEPHOLE[node[0]];
|
|
4610
4543
|
if (!fn) return;
|
|
@@ -4661,7 +4594,7 @@ var globals = (ast) => {
|
|
|
4661
4594
|
}
|
|
4662
4595
|
if (constGlobals.size === 0) return ast;
|
|
4663
4596
|
const reads = /* @__PURE__ */ new Map();
|
|
4664
|
-
|
|
4597
|
+
walk(ast, (n) => {
|
|
4665
4598
|
if (!Array.isArray(n)) return;
|
|
4666
4599
|
const ref = n[1];
|
|
4667
4600
|
if (typeof ref !== "string" || ref[0] !== "$") return;
|
|
@@ -4679,9 +4612,9 @@ var globals = (ast) => {
|
|
|
4679
4612
|
if (after <= before) propagate2.add(name2);
|
|
4680
4613
|
}
|
|
4681
4614
|
if (propagate2.size === 0) return ast;
|
|
4682
|
-
|
|
4615
|
+
walkPost(ast, (node) => {
|
|
4683
4616
|
if (!Array.isArray(node) || node[0] !== "global.get" || node.length !== 2) return;
|
|
4684
|
-
if (propagate2.has(node[1])) return
|
|
4617
|
+
if (propagate2.has(node[1])) return clone(constGlobals.get(node[1]));
|
|
4685
4618
|
});
|
|
4686
4619
|
for (let i = ast.length - 1; i >= 1; i--) {
|
|
4687
4620
|
const n = ast[i];
|
|
@@ -4690,7 +4623,7 @@ var globals = (ast) => {
|
|
|
4690
4623
|
return ast;
|
|
4691
4624
|
};
|
|
4692
4625
|
var offset = (ast) => {
|
|
4693
|
-
return
|
|
4626
|
+
return walkPost(ast, (node) => {
|
|
4694
4627
|
if (!Array.isArray(node)) return;
|
|
4695
4628
|
const op = node[0];
|
|
4696
4629
|
if (typeof op !== "string" || !op.endsWith("load") && !op.endsWith("store")) return;
|
|
@@ -4741,7 +4674,7 @@ var offset = (ast) => {
|
|
|
4741
4674
|
});
|
|
4742
4675
|
};
|
|
4743
4676
|
var unbranch = (ast) => {
|
|
4744
|
-
|
|
4677
|
+
walk(ast, (node) => {
|
|
4745
4678
|
if (!Array.isArray(node)) return;
|
|
4746
4679
|
const op = node[0];
|
|
4747
4680
|
if (op !== "block") return;
|
|
@@ -4773,7 +4706,7 @@ var unbranch = (ast) => {
|
|
|
4773
4706
|
return ast;
|
|
4774
4707
|
};
|
|
4775
4708
|
var loopify = (ast) => {
|
|
4776
|
-
|
|
4709
|
+
walk(ast, (node) => {
|
|
4777
4710
|
if (!Array.isArray(node) || node[0] !== "block") return;
|
|
4778
4711
|
let bi = 1, label = null;
|
|
4779
4712
|
if (typeof node[1] === "string" && node[1][0] === "$") {
|
|
@@ -4832,10 +4765,10 @@ var loopify = (ast) => {
|
|
|
4832
4765
|
var stripmut = (ast) => {
|
|
4833
4766
|
if (!Array.isArray(ast) || ast[0] !== "module") return ast;
|
|
4834
4767
|
const written = /* @__PURE__ */ new Set();
|
|
4835
|
-
|
|
4768
|
+
walk(ast, (n) => {
|
|
4836
4769
|
if (Array.isArray(n) && n[0] === "global.set" && typeof n[1] === "string") written.add(n[1]);
|
|
4837
4770
|
});
|
|
4838
|
-
return
|
|
4771
|
+
return walkPost(ast, (node) => {
|
|
4839
4772
|
if (!Array.isArray(node) || node[0] !== "global") return;
|
|
4840
4773
|
const name2 = typeof node[1] === "string" && node[1][0] === "$" ? node[1] : null;
|
|
4841
4774
|
if (!name2 || written.has(name2)) return;
|
|
@@ -4849,7 +4782,7 @@ var stripmut = (ast) => {
|
|
|
4849
4782
|
});
|
|
4850
4783
|
};
|
|
4851
4784
|
var brif = (ast) => {
|
|
4852
|
-
return
|
|
4785
|
+
return walkPost(ast, (node) => {
|
|
4853
4786
|
if (!Array.isArray(node) || node[0] !== "if") return;
|
|
4854
4787
|
const { cond, thenBranch, elseBranch } = parseIf(node);
|
|
4855
4788
|
const thenEmpty = !thenBranch || thenBranch.length <= 1;
|
|
@@ -4865,7 +4798,7 @@ var brif = (ast) => {
|
|
|
4865
4798
|
});
|
|
4866
4799
|
};
|
|
4867
4800
|
var foldarms = (ast) => {
|
|
4868
|
-
return
|
|
4801
|
+
return walkPost(ast, (node) => {
|
|
4869
4802
|
if (!Array.isArray(node) || node[0] !== "if") return;
|
|
4870
4803
|
const { thenBranch, elseBranch } = parseIf(node);
|
|
4871
4804
|
if (!thenBranch || !elseBranch) return;
|
|
@@ -4938,7 +4871,7 @@ var dedupe = (ast) => {
|
|
|
4938
4871
|
if (!name2) continue;
|
|
4939
4872
|
const localNames = /* @__PURE__ */ new Set();
|
|
4940
4873
|
if (typeof node[1] === "string" && node[1][0] === "$") localNames.add(node[1]);
|
|
4941
|
-
|
|
4874
|
+
walk(node, (n) => {
|
|
4942
4875
|
if (!Array.isArray(n) || typeof n[1] !== "string" || n[1][0] !== "$") return;
|
|
4943
4876
|
const op = n[0];
|
|
4944
4877
|
if (op === "param" || op === "local" || op === "block" || op === "loop" || op === "if") {
|
|
@@ -4953,7 +4886,7 @@ var dedupe = (ast) => {
|
|
|
4953
4886
|
}
|
|
4954
4887
|
}
|
|
4955
4888
|
if (redirects.size === 0) return ast;
|
|
4956
|
-
|
|
4889
|
+
walkPost(ast, (node) => {
|
|
4957
4890
|
if (!Array.isArray(node)) return;
|
|
4958
4891
|
const op = node[0];
|
|
4959
4892
|
if ((op === "call" || op === "return_call") && redirects.has(node[1])) {
|
|
@@ -5000,7 +4933,7 @@ var dedupTypes = (ast) => {
|
|
|
5000
4933
|
if (name2 && redirects.has(name2)) ast.splice(i, 1);
|
|
5001
4934
|
}
|
|
5002
4935
|
}
|
|
5003
|
-
|
|
4936
|
+
walkPost(ast, (node) => {
|
|
5004
4937
|
if (!Array.isArray(node)) return;
|
|
5005
4938
|
const op = node[0];
|
|
5006
4939
|
if (op === "func") {
|
|
@@ -5227,7 +5160,7 @@ var minifyImports = (ast) => {
|
|
|
5227
5160
|
};
|
|
5228
5161
|
var reorderSafe = (ast) => {
|
|
5229
5162
|
let safe = true;
|
|
5230
|
-
|
|
5163
|
+
walk(ast, (n) => {
|
|
5231
5164
|
if (!safe || !Array.isArray(n)) return;
|
|
5232
5165
|
const op = n[0];
|
|
5233
5166
|
if (op === "func" && (typeof n[1] !== "string" || n[1][0] !== "$")) safe = false;
|
|
@@ -5252,7 +5185,7 @@ var reorder = (ast) => {
|
|
|
5252
5185
|
if (!Array.isArray(ast) || ast[0] !== "module") return ast;
|
|
5253
5186
|
if (!reorderSafe(ast)) return ast;
|
|
5254
5187
|
const callCounts = /* @__PURE__ */ new Map();
|
|
5255
|
-
|
|
5188
|
+
walk(ast, (n) => {
|
|
5256
5189
|
if (!Array.isArray(n)) return;
|
|
5257
5190
|
if (n[0] === "call" || n[0] === "return_call") {
|
|
5258
5191
|
callCounts.set(n[1], (callCounts.get(n[1]) || 0) + 1);
|
|
@@ -5271,6 +5204,47 @@ var reorder = (ast) => {
|
|
|
5271
5204
|
funcs.sort((a, b) => (callCounts.get(b[1]) || 0) - (callCounts.get(a[1]) || 0));
|
|
5272
5205
|
return ["module", ...imports, ...funcs, ...others];
|
|
5273
5206
|
};
|
|
5207
|
+
var PASSES = [
|
|
5208
|
+
["stripmut", stripmut, true, "strip mut from never-written globals"],
|
|
5209
|
+
["globals", globals, true, "propagate immutable global constants"],
|
|
5210
|
+
["fold", fold, true, "constant folding"],
|
|
5211
|
+
["identity", identity, true, "remove identity ops (x + 0 \u2192 x)"],
|
|
5212
|
+
["peephole", peephole, true, "x-x\u21920, x&0\u21920, etc."],
|
|
5213
|
+
["strength", strength, true, "strength reduction (x * 2 \u2192 x << 1)"],
|
|
5214
|
+
["branch", branch, true, "simplify constant branches"],
|
|
5215
|
+
["propagate", propagate, true, "forward-propagate single-use locals & tiny consts (never inflates)"],
|
|
5216
|
+
["inlineOnce", inlineOnce, true, "inline single-call functions into their lone caller (never duplicates)"],
|
|
5217
|
+
["inline", inline, false, "inline tiny functions \u2014 can duplicate bodies"],
|
|
5218
|
+
["offset", offset, true, "fold add+const into load/store offset"],
|
|
5219
|
+
["unbranch", unbranch, true, "remove redundant br at end of own block"],
|
|
5220
|
+
["loopify", loopify, true, "collapse block+loop+brif while-idiom into loop+if"],
|
|
5221
|
+
["brif", brif, true, "if-then-br \u2192 br_if"],
|
|
5222
|
+
["foldarms", foldarms, false, "merge identical trailing if arms \u2014 can add block wrapper"],
|
|
5223
|
+
["deadcode", deadcode, true, "eliminate dead code after unreachable/br/return"],
|
|
5224
|
+
["vacuum", vacuum, true, "remove nops, drop-of-pure, empty branches"],
|
|
5225
|
+
["mergeBlocks", mergeBlocks, true, "unwrap `(block $L \u2026)` whose label is never targeted"],
|
|
5226
|
+
["coalesce", coalesceLocals, true, "share local slots between same-type non-overlapping locals"],
|
|
5227
|
+
["locals", localReuse, true, "remove unused locals"],
|
|
5228
|
+
["dedupe", dedupe, true, "eliminate duplicate functions"],
|
|
5229
|
+
["dedupTypes", dedupTypes, true, "merge identical type definitions"],
|
|
5230
|
+
["packData", packData, true, "trim trailing zeros, merge adjacent data segments"],
|
|
5231
|
+
["reorder", reorder, false, "put hot functions first \u2014 no AST reduction"],
|
|
5232
|
+
["treeshake", treeshake, true, "remove unused funcs/globals/types/tables"],
|
|
5233
|
+
["minifyImports", minifyImports, false, "shorten import names \u2014 enable only when you control the host"]
|
|
5234
|
+
];
|
|
5235
|
+
var OPTS = Object.fromEntries(PASSES.map((p) => [p[0], p[2]]));
|
|
5236
|
+
var normalize3 = (opts) => {
|
|
5237
|
+
if (opts === false) return {};
|
|
5238
|
+
if (opts !== true && typeof opts !== "string") {
|
|
5239
|
+
const m2 = { ...opts };
|
|
5240
|
+
for (const p of PASSES) if (m2[p[0]] === void 0) m2[p[0]] = p[2];
|
|
5241
|
+
return m2;
|
|
5242
|
+
}
|
|
5243
|
+
const set = typeof opts === "string" ? new Set(opts.split(/\s+/).filter(Boolean)) : null;
|
|
5244
|
+
const m = {};
|
|
5245
|
+
for (const p of PASSES) m[p[0]] = set ? set.has("all") || set.has(p[0]) : p[2];
|
|
5246
|
+
return m;
|
|
5247
|
+
};
|
|
5274
5248
|
function optimize(ast, opts = true) {
|
|
5275
5249
|
if (typeof ast === "string") ast = parse_default(ast);
|
|
5276
5250
|
const strictGuard = opts === true;
|
|
@@ -5278,37 +5252,12 @@ function optimize(ast, opts = true) {
|
|
|
5278
5252
|
const log = opts.log ? (msg, delta) => opts.log(msg, delta) : () => {
|
|
5279
5253
|
};
|
|
5280
5254
|
const verbose = opts.verbose || opts.log;
|
|
5281
|
-
ast =
|
|
5255
|
+
ast = clone(ast);
|
|
5282
5256
|
let beforeRound = null;
|
|
5283
5257
|
for (let round = 0; round < 3; round++) {
|
|
5284
|
-
beforeRound =
|
|
5258
|
+
beforeRound = clone(ast);
|
|
5285
5259
|
const sizeBefore = binarySize(ast);
|
|
5286
|
-
if (opts
|
|
5287
|
-
if (opts.globals) ast = globals(ast);
|
|
5288
|
-
if (opts.fold) ast = fold(ast);
|
|
5289
|
-
if (opts.identity) ast = identity(ast);
|
|
5290
|
-
if (opts.peephole) ast = peephole(ast);
|
|
5291
|
-
if (opts.strength) ast = strength(ast);
|
|
5292
|
-
if (opts.branch) ast = branch(ast);
|
|
5293
|
-
if (opts.propagate) ast = propagate(ast);
|
|
5294
|
-
if (opts.inlineOnce) ast = inlineOnce(ast);
|
|
5295
|
-
if (opts.inline) ast = inline(ast);
|
|
5296
|
-
if (opts.offset) ast = offset(ast);
|
|
5297
|
-
if (opts.unbranch) ast = unbranch(ast);
|
|
5298
|
-
if (opts.loopify) ast = loopify(ast);
|
|
5299
|
-
if (opts.brif) ast = brif(ast);
|
|
5300
|
-
if (opts.foldarms) ast = foldarms(ast);
|
|
5301
|
-
if (opts.deadcode) ast = deadcode(ast);
|
|
5302
|
-
if (opts.vacuum) ast = vacuum(ast);
|
|
5303
|
-
if (opts.mergeBlocks) ast = mergeBlocks(ast);
|
|
5304
|
-
if (opts.coalesce) ast = coalesceLocals(ast);
|
|
5305
|
-
if (opts.locals) ast = localReuse(ast);
|
|
5306
|
-
if (opts.dedupe) ast = dedupe(ast);
|
|
5307
|
-
if (opts.dedupTypes) ast = dedupTypes(ast);
|
|
5308
|
-
if (opts.packData) ast = packData(ast);
|
|
5309
|
-
if (opts.reorder) ast = reorder(ast);
|
|
5310
|
-
if (opts.treeshake) ast = treeshake(ast);
|
|
5311
|
-
if (opts.minifyImports) ast = minifyImports(ast);
|
|
5260
|
+
for (const [key, fn] of PASSES) if (opts[key]) ast = fn(ast);
|
|
5312
5261
|
if (opts.propagate && (opts.inlineOnce || opts.inline)) ast = propagate(ast);
|
|
5313
5262
|
const sizeAfter = binarySize(ast);
|
|
5314
5263
|
const delta = sizeAfter - sizeBefore;
|
|
@@ -5326,32 +5275,25 @@ function optimize(ast, opts = true) {
|
|
|
5326
5275
|
return ast;
|
|
5327
5276
|
}
|
|
5328
5277
|
|
|
5329
|
-
//
|
|
5278
|
+
// src/template.js
|
|
5330
5279
|
var PUA = "\uE000";
|
|
5331
|
-
var instrType = (op) => {
|
|
5332
|
-
if (!op || typeof op !== "string") return null;
|
|
5333
|
-
const prefix = op.split(".")[0];
|
|
5334
|
-
if (/^[if](32|64)|v128/.test(prefix)) return prefix;
|
|
5335
|
-
if (/\.(eq|ne|[lg][te]|eqz)/.test(op)) return "i32";
|
|
5336
|
-
if (op === "memory.size" || op === "memory.grow") return "i32";
|
|
5337
|
-
return null;
|
|
5338
|
-
};
|
|
5339
5280
|
var exprType = (node, ctx = {}) => {
|
|
5340
5281
|
if (!Array.isArray(node)) {
|
|
5341
5282
|
if (typeof node === "string" && node[0] === "$" && ctx.locals?.[node]) return ctx.locals[node];
|
|
5342
5283
|
return null;
|
|
5343
5284
|
}
|
|
5344
5285
|
const [op, ...args] = node;
|
|
5345
|
-
|
|
5286
|
+
const rt = resultType(op);
|
|
5287
|
+
if (rt) return rt;
|
|
5346
5288
|
if (op === "local.get" && ctx.locals?.[args[0]]) return ctx.locals[args[0]];
|
|
5347
5289
|
if (op === "call" && ctx.funcs?.[args[0]]) return ctx.funcs[args[0]].result?.[0];
|
|
5348
5290
|
return null;
|
|
5349
5291
|
};
|
|
5350
|
-
function
|
|
5292
|
+
function walk2(node, fn) {
|
|
5351
5293
|
node = fn(node);
|
|
5352
5294
|
if (Array.isArray(node)) {
|
|
5353
5295
|
for (let i = 0; i < node.length; i++) {
|
|
5354
|
-
let child =
|
|
5296
|
+
let child = walk2(node[i], fn);
|
|
5355
5297
|
if (child?._splice) node.splice(i, 1, ...child), i += child.length - 1;
|
|
5356
5298
|
else node[i] = child;
|
|
5357
5299
|
}
|
|
@@ -5361,7 +5303,7 @@ function walk3(node, fn) {
|
|
|
5361
5303
|
function inferImports(ast, funcs) {
|
|
5362
5304
|
const imports = [];
|
|
5363
5305
|
const importMap = /* @__PURE__ */ new Map();
|
|
5364
|
-
|
|
5306
|
+
walk2(ast, (node) => {
|
|
5365
5307
|
if (!Array.isArray(node)) return node;
|
|
5366
5308
|
if (node[0] === "call" && typeof node[1] === "function") {
|
|
5367
5309
|
const fn = node[1];
|
|
@@ -5388,7 +5330,8 @@ function genImports(imports) {
|
|
|
5388
5330
|
({ name: name2, params }) => ["import", '"env"', `"${name2.slice(1)}"`, ["func", name2, ...params.map((t) => ["param", t])]]
|
|
5389
5331
|
);
|
|
5390
5332
|
}
|
|
5391
|
-
function compile2(source,
|
|
5333
|
+
function compile2(backend2, source, values) {
|
|
5334
|
+
const { parse, compile: emit, optimize: optimize2, polyfill: polyfill2 } = backend2;
|
|
5392
5335
|
let opts = {};
|
|
5393
5336
|
if (!Array.isArray(source) && values.length && typeof values[values.length - 1] === "object" && values[values.length - 1] !== null && !values[values.length - 1].byteLength) {
|
|
5394
5337
|
opts = values.pop();
|
|
@@ -5398,10 +5341,10 @@ function compile2(source, ...values) {
|
|
|
5398
5341
|
for (let i = 0; i < values.length; i++) {
|
|
5399
5342
|
src += PUA + source[i + 1];
|
|
5400
5343
|
}
|
|
5401
|
-
let ast =
|
|
5344
|
+
let ast = parse(src);
|
|
5402
5345
|
const funcsToImport = [];
|
|
5403
5346
|
let idx = 0;
|
|
5404
|
-
ast =
|
|
5347
|
+
ast = walk2(ast, (node) => {
|
|
5405
5348
|
if (node === PUA) {
|
|
5406
5349
|
const value = values[idx++];
|
|
5407
5350
|
if (typeof value === "function") {
|
|
@@ -5409,13 +5352,14 @@ function compile2(source, ...values) {
|
|
|
5409
5352
|
return value;
|
|
5410
5353
|
}
|
|
5411
5354
|
if (typeof value === "string" && (value[0] === "(" || /^\s*\(/.test(value))) {
|
|
5412
|
-
const parsed =
|
|
5355
|
+
const parsed = parse(value);
|
|
5413
5356
|
if (Array.isArray(parsed) && Array.isArray(parsed[0])) {
|
|
5414
5357
|
parsed._splice = true;
|
|
5415
5358
|
}
|
|
5416
5359
|
return parsed;
|
|
5417
5360
|
}
|
|
5418
|
-
if (value
|
|
5361
|
+
if (value?.byteLength !== void 0) return [...value];
|
|
5362
|
+
if (typeof value === "bigint") return value.toString();
|
|
5419
5363
|
return value;
|
|
5420
5364
|
}
|
|
5421
5365
|
return node;
|
|
@@ -5438,33 +5382,42 @@ function compile2(source, ...values) {
|
|
|
5438
5382
|
}
|
|
5439
5383
|
}
|
|
5440
5384
|
}
|
|
5441
|
-
if (opts.polyfill) ast =
|
|
5442
|
-
if (opts.optimize) ast =
|
|
5443
|
-
const binary =
|
|
5385
|
+
if (opts.polyfill) ast = polyfill2(ast, opts.polyfill);
|
|
5386
|
+
if (opts.optimize) ast = optimize2(ast, opts.optimize);
|
|
5387
|
+
const binary = emit(ast);
|
|
5444
5388
|
if (importObjs) binary._imports = importObjs;
|
|
5445
5389
|
return binary;
|
|
5446
5390
|
}
|
|
5447
5391
|
if (opts.polyfill || opts.optimize) {
|
|
5448
|
-
let ast = typeof source === "string" ?
|
|
5449
|
-
if (opts.polyfill) ast =
|
|
5450
|
-
if (opts.optimize) ast =
|
|
5451
|
-
return
|
|
5392
|
+
let ast = typeof source === "string" ? parse(source) : source;
|
|
5393
|
+
if (opts.polyfill) ast = polyfill2(ast, opts.polyfill);
|
|
5394
|
+
if (opts.optimize) ast = optimize2(ast, opts.optimize);
|
|
5395
|
+
return emit(ast);
|
|
5452
5396
|
}
|
|
5453
|
-
return
|
|
5397
|
+
return emit(source);
|
|
5454
5398
|
}
|
|
5455
|
-
function watr(
|
|
5456
|
-
const binary = compile2(
|
|
5399
|
+
function watr(backend2, source, values) {
|
|
5400
|
+
const binary = compile2(backend2, source, values);
|
|
5457
5401
|
const module = new WebAssembly.Module(binary);
|
|
5458
5402
|
const instance = new WebAssembly.Instance(module, binary._imports);
|
|
5459
5403
|
return instance.exports;
|
|
5460
5404
|
}
|
|
5461
|
-
|
|
5405
|
+
|
|
5406
|
+
// watr.js
|
|
5407
|
+
var backend = { parse: parse_default, compile, optimize, polyfill };
|
|
5408
|
+
function compile3(source, ...values) {
|
|
5409
|
+
return compile2(backend, source, values);
|
|
5410
|
+
}
|
|
5411
|
+
function watr2(source, ...values) {
|
|
5412
|
+
return watr(backend, source, values);
|
|
5413
|
+
}
|
|
5414
|
+
var watr_default = watr2;
|
|
5462
5415
|
export {
|
|
5463
|
-
|
|
5416
|
+
compile3 as compile,
|
|
5464
5417
|
watr_default as default,
|
|
5465
5418
|
optimize,
|
|
5466
5419
|
parse_default as parse,
|
|
5467
5420
|
polyfill,
|
|
5468
5421
|
print,
|
|
5469
|
-
watr
|
|
5422
|
+
watr2 as watr
|
|
5470
5423
|
};
|