Skip to content

Commit 343a18b

Browse files
committed
precompile: rewrite comptime reset prefs code
~3% smaller output file and more readable diffs test262: 61.72% | πŸ§ͺ 50718 | 🀠 31304 | ❌ 7603 | πŸ’€ 11552 | πŸ—οΈ 0 | πŸ’₯ 239 | ⏰ 20
1 parent 0c4be88 commit 343a18b

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

β€Žcompiler/builtins_precompiled.jsβ€Ž

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žcompiler/precompile.jsβ€Ž

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Opcodes, Valtype } from './wasmSpec.js';
2-
import { read_signedLEB128 } from './encoding.js';
32
import { TYPES, TYPE_NAMES } from './types.js';
43
import { createImport, importedFuncs } from './builtins.js';
54
import { log } from './log.js';
@@ -48,6 +47,7 @@ globalThis.valtypeOverrides = {
4847
const argv = process.argv.slice();
4948

5049
const timing = {};
50+
let defaultPrefs = null;
5151
const compile = async (file, _funcs) => {
5252
let source = fs.readFileSync(file, 'utf8');
5353
let first = source.slice(0, source.indexOf('\n'));
@@ -57,7 +57,13 @@ const compile = async (file, _funcs) => {
5757
first = source.slice(0, source.indexOf('\n'));
5858
}
5959

60-
let args = ['--module', '--truthy=no_nan_negative', '--no-rm-unused-types', '--fast-length', '--parse-types', '--opt-types', '--no-passive-data', '--active-data', '--no-treeshake-wasm-imports', '--no-coctc', '--no-closures'];
60+
let args = ['--module', '--truthy=no_nan_negative', '--no-rm-unused-types', '--fast-length', '--parse-types', '--opt-types', '--no-passive-data', '--active-data', '--no-treeshake-wasm-imports', '--no-coctc', '--no-closures', '--never-fallback-builtin-proto'];
61+
if (!defaultPrefs) {
62+
process.argv = argv.concat(args);
63+
globalThis.argvChanged?.();
64+
defaultPrefs = globalThis.Prefs;
65+
}
66+
6167
if (first.startsWith('// @porf')) {
6268
args = first.slice('// @porf '.length).split(' ').concat(args);
6369
}
@@ -238,6 +244,9 @@ const precompile = async () => {
238244
return `// autogenerated by compiler/precompile.js
239245
import { number } from './encoding.js';
240246
247+
const defaultPrefs = ${JSON.stringify(defaultPrefs)};
248+
const resetGlobals = (Valtype,Opcodes)=>{valtype=Prefs.valtype??'f64';valtypeBinary=Valtype[valtype];Opcodes.const=valtypeBinary===Valtype.i32?Opcodes.i32_const:Opcodes.f64_const;Opcodes.eq=valtypeBinary===Valtype.i32?Opcodes.i32_eq:Opcodes.f64_eq;Opcodes.eqz=valtypeBinary===Valtype.i32?[[Opcodes.i32_eqz]]:[number(0),[Opcodes.f64_eq]];Opcodes.mul=valtypeBinary===Valtype.i32?Opcodes.i32_mul:Opcodes.f64_mul;Opcodes.add=valtypeBinary===Valtype.i32?Opcodes.i32_add:Opcodes.f64_add;Opcodes.sub=valtypeBinary===Valtype.i32?Opcodes.i32_sub:Opcodes.f64_sub;Opcodes.i32_to=valtypeBinary===Valtype.i32?[]:Opcodes.i32_trunc_sat_f64_s;Opcodes.i32_to_u=valtypeBinary===Valtype.i32?[]:Opcodes.i32_trunc_sat_f64_u;Opcodes.i32_from=valtypeBinary===Valtype.i32?[]:[Opcodes.f64_convert_i32_s];Opcodes.i32_from_u=valtypeBinary===Valtype.i32?[]:[Opcodes.f64_convert_i32_u];Opcodes.load=valtypeBinary===Valtype.i32?Opcodes.i32_load:Opcodes.f64_load;Opcodes.store=valtypeBinary===Valtype.i32?Opcodes.i32_store:Opcodes.f64_store;};
249+
241250
export const BuiltinFuncs = x => {
242251
${funcs.map(x => {
243252
const rewriteWasm = wasm => {
@@ -266,11 +275,15 @@ ${funcs.map(x => {
266275
passAst = processAst(passAst);
267276
failAst = processAst(failAst);
268277
278+
// ignore default prefs in prefs for better diff and size
279+
prefs = JSON.parse(prefs);
280+
const diffPrefs = Object.keys(prefs).reduce((acc, x) => { if (prefs[x] !== defaultPrefs[x]) acc[x] = prefs[x]; return acc; }, {});
281+
269282
const [ id, extra ] = flag.split('.');
270-
return `[null,()=>{const r=()=>{valtype=Prefs.valtype??'f64';valtypeBinary=Valtype[valtype];Opcodes.const=valtypeBinary===Valtype.i32?Opcodes.i32_const:Opcodes.f64_const;Opcodes.eq=valtypeBinary===Valtype.i32?Opcodes.i32_eq:Opcodes.f64_eq;Opcodes.eqz=valtypeBinary===Valtype.i32?[[Opcodes.i32_eqz]]:[number(0),[Opcodes.f64_eq]];Opcodes.mul=valtypeBinary===Valtype.i32?Opcodes.i32_mul:Opcodes.f64_mul;Opcodes.add=valtypeBinary===Valtype.i32?Opcodes.i32_add:Opcodes.f64_add;Opcodes.sub=valtypeBinary===Valtype.i32?Opcodes.i32_sub:Opcodes.f64_sub;Opcodes.i32_to=valtypeBinary===Valtype.i32?[]:Opcodes.i32_trunc_sat_f64_s;Opcodes.i32_to_u=valtypeBinary===Valtype.i32?[]:Opcodes.i32_trunc_sat_f64_u;Opcodes.i32_from=valtypeBinary===Valtype.i32?[]:[Opcodes.f64_convert_i32_s];Opcodes.i32_from_u=valtypeBinary===Valtype.i32?[]:[Opcodes.f64_convert_i32_u];Opcodes.load=valtypeBinary===Valtype.i32?Opcodes.i32_load:Opcodes.f64_load;Opcodes.store=valtypeBinary===Valtype.i32?Opcodes.i32_store:Opcodes.f64_store};const a=Prefs;Prefs=${prefs};r();const b=generate(_,${comptimeFlagChecks[id](extra)}?${passAst}:${failAst});if(b.at(-1)[0]>=0x41&&b.at(-1)[0]<=0x44)b.pop();Prefs=a;r();return b;}]`;
283+
return `[null,()=>{const a=Prefs;Prefs={...defaultPrefs,${JSON.stringify(diffPrefs).slice(1, -1)}};resetGlobals(Valtype,Opcodes);const b=generate(_,${comptimeFlagChecks[id](extra)}?${passAst}:${failAst});if(b.at(-1)[0]>=0x41&&b.at(-1)[0]<=0x44)b.pop();Prefs=a;resetGlobals(Valtype,Opcodes);return b;}]`;
271284
});
272285
273-
return `(_,{${str.includes('usedTypes.') ? 'usedTypes,' : ''}${str.includes('hasFunc(') ? 'hasFunc,' : ''}${str.includes('Valtype[') ? 'Valtype,' : ''}${str.includes('i32ify') ? 'i32ify,' : ''}${str.includes('Opcodes.') ? 'Opcodes,' : ''}${str.includes('...t(') ? 't,' : ''}${`${str.includes('allocPage(') ? 'allocPage,' : ''}${str.includes('makeString(') ? 'makeString,' : ''}${str.includes('glbl(') ? 'glbl,' : ''}${str.includes('loc(') ? 'loc,' : ''}${str.includes('builtin(') ? 'builtin,' : ''}${str.includes('funcRef(') ? 'funcRef,' : ''}${str.includes('internalThrow(') ? 'internalThrow,' : ''}${str.includes('generateIdent(') ? 'generateIdent,' : ''}${str.includes('generate(') ? 'generate,' : ''}`.slice(0, -1)}})=>`.replace('_,{}', '') + `eval(${JSON.stringify(str)})`;
286+
return `(_,{${str.includes('usedTypes.') ? 'usedTypes,' : ''}${str.includes('hasFunc(') ? 'hasFunc,' : ''}${str.includes('Valtype') ? 'Valtype,' : ''}${str.includes('i32ify') ? 'i32ify,' : ''}${str.includes('Opcodes') ? 'Opcodes,' : ''}${str.includes('...t(') ? 't,' : ''}${`${str.includes('allocPage(') ? 'allocPage,' : ''}${str.includes('makeString(') ? 'makeString,' : ''}${str.includes('glbl(') ? 'glbl,' : ''}${str.includes('loc(') ? 'loc,' : ''}${str.includes('builtin(') ? 'builtin,' : ''}${str.includes('funcRef(') ? 'funcRef,' : ''}${str.includes('internalThrow(') ? 'internalThrow,' : ''}${str.includes('generateIdent(') ? 'generateIdent,' : ''}${str.includes('generate(') ? 'generate,' : ''}`.slice(0, -1)}})=>`.replace('_,{}', '') + `eval(${JSON.stringify(str)})`;
274287
};
275288
276289
const locals = Object.entries(x.locals).sort((a,b) => a[1].idx - b[1].idx)

β€Žjsr.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@honk/porffor",
3-
"version": "0.60.26",
3+
"version": "0.60.27",
44
"exports": "./compiler/wrap.js",
55
"publish": {
66
"exclude": [

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "porffor",
33
"description": "An ahead-of-time JavaScript compiler",
4-
"version": "0.60.26",
4+
"version": "0.60.27",
55
"author": "Oliver Medhurst <[email protected]>",
66
"license": "MIT",
77
"scripts": {},

β€Žruntime/index.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
import fs from 'node:fs';
3-
globalThis.version = '0.60.26';
3+
globalThis.version = '0.60.27';
44

55
// deno compat
66
if (typeof process === 'undefined' && typeof Deno !== 'undefined') {

β€Žtest262/history.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
Β (0)