porffor 0.14.0-a24ac8cf2 → 0.14.0-f67c123a1

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.
@@ -65,7 +65,6 @@ export default (funcs, globals, tags, pages, data, flags) => {
65
65
  importFuncs = [...imports.values()];
66
66
 
67
67
  // fix call indexes for non-imports
68
- // also fix call_indirect types
69
68
  const delta = importedFuncs.length - importFuncs.length;
70
69
  for (const f of funcs) {
71
70
  f.originalIndex = f.index;
@@ -75,16 +74,6 @@ export default (funcs, globals, tags, pages, data, flags) => {
75
74
  if ((inst[0] === Opcodes.call || inst[0] === Opcodes.return_call) && inst[1] >= importedFuncs.length) {
76
75
  inst[1] -= delta;
77
76
  }
78
-
79
- if (inst[0] === Opcodes.call_indirect) {
80
- const params = [];
81
- for (let i = 0; i < inst[1]; i++) {
82
- params.push(valtypeBinary, Valtype.i32);
83
- }
84
-
85
- const returns = [ valtypeBinary, Valtype.i32 ];
86
- inst[1] = getType(params, returns);
87
- }
88
77
  }
89
78
  }
90
79
  }
@@ -112,7 +101,7 @@ export default (funcs, globals, tags, pages, data, flags) => {
112
101
  encodeVector([ [
113
102
  0x00,
114
103
  Opcodes.i32_const, 0, Opcodes.end,
115
- ...encodeVector(funcs.map(x => x.index))
104
+ encodeVector(funcs.map(x => x.index))
116
105
  ] ])
117
106
  );
118
107
 
@@ -351,7 +351,9 @@ const generateReturn = (scope, decl) => {
351
351
 
352
352
  return [
353
353
  ...generate(scope, decl.argument),
354
- ...(scope.returnType != null ? [] : getNodeType(scope, decl.argument)),
354
+ ...(scope.returnType != null ? [] : [
355
+ ...getNodeType(scope, decl.argument)
356
+ ]),
355
357
  [ Opcodes.return ]
356
358
  ];
357
359
  };
@@ -1449,10 +1451,6 @@ const countLeftover = wasm => {
1449
1451
  } else count--;
1450
1452
  if (func) count += func.returns.length;
1451
1453
  }
1452
- } else if (inst[0] === Opcodes.call_indirect) {
1453
- count--; // funcidx
1454
- count -= inst[1] * 2; // params * 2 (typed)
1455
- count += 2; // fixed return (value, type)
1456
1454
  } else count--;
1457
1455
 
1458
1456
  // console.log(count, decompile([ inst ]).slice(0, -1));
@@ -1835,44 +1833,22 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
1835
1833
 
1836
1834
  if (idx === undefined) {
1837
1835
  if (scope.locals[name] !== undefined || globals[name] !== undefined || builtinVars[name] !== undefined) {
1836
+ if (!Prefs.indirectCalls) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true);
1837
+
1838
1838
  const [ local, global ] = lookupName(scope, name);
1839
- if (!Prefs.indirectCalls || local == null) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true);
1839
+ funcs.table = true;
1840
1840
 
1841
1841
  // todo: only works when:
1842
1842
  // 1. arg count matches arg count of function
1843
1843
  // 2. function uses typedParams and typedReturns
1844
-
1845
- funcs.table = true;
1846
-
1847
- let args = decl.arguments;
1848
- let argWasm = [];
1849
-
1850
- for (let i = 0; i < args.length; i++) {
1851
- const arg = args[i];
1852
- argWasm = argWasm.concat(generate(scope, arg));
1853
-
1854
- if (valtypeBinary !== Valtype.i32 && (
1855
- (builtinFuncs[name] && builtinFuncs[name].params[i * (typedParams ? 2 : 1)] === Valtype.i32) ||
1856
- (importedFuncs[name] && name.startsWith('profile'))
1857
- )) {
1858
- argWasm.push(Opcodes.i32_to);
1859
- }
1860
-
1861
- argWasm = argWasm.concat(getNodeType(scope, arg));
1862
- }
1863
-
1864
- return typeSwitch(scope, getNodeType(scope, decl.callee), {
1844
+ return typeSwitch(scope, getNodeType(decl.callee), {
1865
1845
  [TYPES.function]: [
1866
- ...argWasm,
1867
1846
  [ global ? Opcodes.global_get : Opcodes.local_get, local.idx ],
1868
- Opcodes.i32_to_u,
1869
- [ Opcodes.call_indirect, args.length, 0 ],
1870
- ...setLastType(scope)
1847
+ [ Opcodes.call_indirect ]
1871
1848
  ],
1872
1849
  default: internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true)
1873
1850
  });
1874
1851
  }
1875
-
1876
1852
  return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
1877
1853
  }
1878
1854
 
@@ -3652,10 +3628,8 @@ const internalConstrs = {
3652
3628
  }),
3653
3629
 
3654
3630
  // print space
3655
- ...(i !== decl.arguments.length - 1 ? [
3656
- ...number(32),
3657
- [ Opcodes.call, importedFuncs.printChar ]
3658
- ] : [])
3631
+ ...number(32),
3632
+ [ Opcodes.call, importedFuncs.printChar ]
3659
3633
  );
3660
3634
  }
3661
3635
 
@@ -3665,8 +3639,6 @@ const internalConstrs = {
3665
3639
  [ Opcodes.call, importedFuncs.printChar ]
3666
3640
  );
3667
3641
 
3668
- out.push(...number(UNDEFINED));
3669
-
3670
3642
  return out;
3671
3643
  },
3672
3644
  type: TYPES.undefined,
package/compiler/index.js CHANGED
@@ -12,7 +12,14 @@ globalThis.decompile = decompile;
12
12
  const logFuncs = (funcs, globals, exceptions) => {
13
13
  console.log('\n' + underline(bold('funcs')));
14
14
 
15
+ const startIndex = funcs.sort((a, b) => a.index - b.index)[0].index;
15
16
  for (const f of funcs) {
17
+ console.log(`${underline(f.name)} (${f.index - startIndex})`);
18
+
19
+ console.log(`params: ${f.params.map((_, i) => Object.keys(f.locals)[Object.values(f.locals).indexOf(Object.values(f.locals).find(x => x.idx === i))]).join(', ')}`);
20
+ console.log(`returns: ${f.returns.length > 0 ? true : false}`);
21
+ console.log(`locals: ${Object.keys(f.locals).sort((a, b) => f.locals[a].idx - f.locals[b].idx).map(x => `${x} (${f.locals[x].idx})`).join(', ')}`);
22
+ console.log();
16
23
  console.log(decompile(f.wasm, f.name, f.index, f.locals, f.params, f.returns, funcs, globals, exceptions));
17
24
  }
18
25
 
@@ -37,14 +44,12 @@ export default (code, flags) => {
37
44
  opt(funcs, globals, pages, tags, exceptions);
38
45
  if (Prefs.profileCompiler) console.log(`3. optimized in ${(performance.now() - t2).toFixed(2)}ms`);
39
46
 
40
- // if (Prefs.optFuncs) logFuncs(funcs, globals, exceptions);
47
+ if (Prefs.optFuncs) logFuncs(funcs, globals, exceptions);
41
48
 
42
49
  const t3 = performance.now();
43
50
  const wasm = assemble(funcs, globals, tags, pages, data, flags);
44
51
  if (Prefs.profileCompiler) console.log(`4. assembled in ${(performance.now() - t3).toFixed(2)}ms`);
45
52
 
46
- if (Prefs.optFuncs) logFuncs(funcs, globals, exceptions);
47
-
48
53
  if (Prefs.allocLog) {
49
54
  const wasmPages = Math.ceil((pages.size * pageSize) / 65536);
50
55
  const bytes = wasmPages * 65536;
@@ -18,7 +18,7 @@ const compile = async (file, [ _funcs, _globals ]) => {
18
18
  first = source.slice(0, source.indexOf('\n'));
19
19
  }
20
20
 
21
- let args = ['--bytestring', '--todo-time=compile', '--no-aot-pointer-opt', '--no-indirect-calls', '--no-treeshake-wasm-imports', '--no-rm-unused-types', '--scoped-page-names', '--funsafe-no-unlikely-proto-checks', '--parse-types', '--opt-types'];
21
+ let args = ['--bytestring', '--todo-time=compile', '--no-aot-pointer-opt', '--no-treeshake-wasm-imports', '--no-rm-unused-types', '--scoped-page-names', '--funsafe-no-unlikely-proto-checks', '--parse-types', '--opt-types'];
22
22
  if (first.startsWith('// @porf')) {
23
23
  args = args.concat(first.slice('// @porf '.length).split(' '));
24
24
  }
package/compiler/prefs.js CHANGED
@@ -1,4 +1,4 @@
1
- const onByDefault = [ 'bytestring', 'aotPointerOpt', 'treeshakeWasmImports', 'alwaysMemory', 'indirectCalls' ];
1
+ const onByDefault = [ 'bytestring', 'aotPointerOpt', 'treeshakeWasmImports', 'alwaysMemory' ];
2
2
 
3
3
  let cache = {};
4
4
  const obj = new Proxy({}, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.14.0-a24ac8cf2",
4
+ "version": "0.14.0-f67c123a1",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {