porffor 0.2.0-f2bbe1f → 0.2.0-f7ea197

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.
Files changed (52) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +154 -89
  3. package/asur/README.md +2 -0
  4. package/asur/index.js +1262 -0
  5. package/byg/index.js +237 -0
  6. package/compiler/2c.js +317 -72
  7. package/compiler/{sections.js → assemble.js} +63 -15
  8. package/compiler/builtins/annexb_string.js +72 -0
  9. package/compiler/builtins/annexb_string.ts +19 -0
  10. package/compiler/builtins/array.ts +145 -0
  11. package/compiler/builtins/base64.ts +151 -0
  12. package/compiler/builtins/crypto.ts +120 -0
  13. package/compiler/builtins/date.ts +1370 -0
  14. package/compiler/builtins/escape.ts +141 -0
  15. package/compiler/builtins/int.ts +147 -0
  16. package/compiler/builtins/number.ts +527 -0
  17. package/compiler/builtins/porffor.d.ts +42 -0
  18. package/compiler/builtins/string.ts +1055 -0
  19. package/compiler/builtins/tostring.ts +45 -0
  20. package/compiler/builtins.js +470 -269
  21. package/compiler/{codeGen.js → codegen.js} +1098 -392
  22. package/compiler/embedding.js +22 -22
  23. package/compiler/encoding.js +108 -10
  24. package/compiler/generated_builtins.js +1262 -0
  25. package/compiler/index.js +36 -34
  26. package/compiler/log.js +6 -3
  27. package/compiler/opt.js +51 -36
  28. package/compiler/parse.js +36 -28
  29. package/compiler/precompile.js +123 -0
  30. package/compiler/prefs.js +27 -0
  31. package/compiler/prototype.js +177 -37
  32. package/compiler/types.js +37 -0
  33. package/compiler/wasmSpec.js +30 -7
  34. package/compiler/wrap.js +56 -40
  35. package/fib.js +10 -0
  36. package/package.json +9 -5
  37. package/porf +4 -0
  38. package/rhemyn/compile.js +46 -27
  39. package/rhemyn/parse.js +322 -320
  40. package/rhemyn/test/parse.js +58 -58
  41. package/runner/compare.js +34 -34
  42. package/runner/debug.js +122 -0
  43. package/runner/index.js +91 -11
  44. package/runner/profiler.js +102 -0
  45. package/runner/repl.js +42 -9
  46. package/runner/sizes.js +37 -37
  47. package/compiler/builtins/base64.js +0 -92
  48. package/runner/info.js +0 -89
  49. package/runner/profile.js +0 -46
  50. package/runner/results.json +0 -1
  51. package/runner/transform.js +0 -15
  52. package/util/enum.js +0 -20
package/runner/sizes.js CHANGED
@@ -1,38 +1,38 @@
1
- import fs from 'node:fs';
2
- import compile from '../compiler/index.js';
3
-
4
- // deno compat
5
- const textEncoder = new TextEncoder();
6
- if (typeof process === 'undefined') globalThis.process = { argv: ['', '', ...Deno.args], stdout: { write: str => Deno.writeAllSync(Deno.stdout, textEncoder.encode(str)) } };
7
-
8
- let csv = `file,porffor i32 -O0,porffor i32 -O1,porffor i32 -O2,porffor i32 -O3,porffor i64 -O0,porffor i64 -O1,porffor i64 -O2,porffor i64 -O3,porffor f64 -O0, porffor f64 -O1, porffor f64 -O2, porffor f64 -O3\n`;
9
- const perform = async (file, args) => {
10
- process.argv = process.argv.slice(0, 2).concat(args);
11
- const source = fs.readFileSync(file, 'utf8');
12
-
13
- const { wasm } = compile(source, []);
14
- const size = wasm.byteLength;
15
-
16
- const label = `${file} ${args.join(' ')}`;
17
- csv += `${size},`;
18
- console.log(label, ' '.repeat(40 - label.length), `${size}b`);
19
- };
20
-
21
- const argsValtypes = [ '-valtype=i32', '-valtype=i64', '-valtype=f64' ];
22
- const argsOptlevels = [ '-O0', '-O1', '-O2', '-O3' ];
23
-
24
- for (const file of [ 'bench/prime_basic.js', 'bench/fib_iter.js', 'test/math_1.js', 'test/math_3.js', 'test/while_1.js', 'test/for_2.js', 'test/unary_3.js', 'test/updateexp_1.js', 'test/eq_3.js', 'test/empty.js' ]) {
25
- const niceFile = file.split('/')[1].slice(0, -3);
26
- csv += `${niceFile},`;
27
-
28
- for (const x of argsValtypes) {
29
- for (const y of argsOptlevels) {
30
- await perform(file, [ x, y ]);
31
- }
32
- }
33
-
34
- csv = csv.slice(0, -1) + '\n';
35
- }
36
-
37
- fs.writeFileSync('sizes.csv', csv);
1
+ import fs from 'node:fs';
2
+ import compile from '../compiler/index.js';
3
+
4
+ // deno compat
5
+ const textEncoder = new TextEncoder();
6
+ if (typeof process === 'undefined') globalThis.process = { argv: ['', '', ...Deno.args], stdout: { write: str => Deno.writeAllSync(Deno.stdout, textEncoder.encode(str)) } };
7
+
8
+ let csv = `file,porffor i32 -O0,porffor i32 -O1,porffor i32 -O2,porffor i32 -O3,porffor i64 -O0,porffor i64 -O1,porffor i64 -O2,porffor i64 -O3,porffor f64 -O0, porffor f64 -O1, porffor f64 -O2, porffor f64 -O3\n`;
9
+ const perform = async (file, args) => {
10
+ process.argv = process.argv.slice(0, 2).concat(args);
11
+ const source = fs.readFileSync(file, 'utf8');
12
+
13
+ const { wasm } = compile(source, []);
14
+ const size = wasm.byteLength;
15
+
16
+ const label = `${file} ${args.join(' ')}`;
17
+ csv += `${size},`;
18
+ console.log(label, ' '.repeat(40 - label.length), `${size}b`);
19
+ };
20
+
21
+ const argsValtypes = [ '--valtype=i32', '--valtype=i64', '--valtype=f64' ];
22
+ const argsOptlevels = [ '-O0', '-O1', '-O2', '-O3' ];
23
+
24
+ for (const file of [ 'bench/prime_basic.js', 'bench/fib_iter.js', 'test/math_1.js', 'test/math_3.js', 'test/while_1.js', 'test/for_2.js', 'test/unary_3.js', 'test/updateexp_1.js', 'test/eq_3.js', 'test/empty.js' ]) {
25
+ const niceFile = file.split('/')[1].slice(0, -3);
26
+ csv += `${niceFile},`;
27
+
28
+ for (const x of argsValtypes) {
29
+ for (const y of argsOptlevels) {
30
+ await perform(file, [ x, y ]);
31
+ }
32
+ }
33
+
34
+ csv = csv.slice(0, -1) + '\n';
35
+ }
36
+
37
+ fs.writeFileSync('sizes.csv', csv);
38
38
  console.log(csv);
@@ -1,92 +0,0 @@
1
- var btoa_a = str => {
2
- // todo: throw invalid character for unicode
3
-
4
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
5
- const mask = (1 << 6) - 1;
6
-
7
- let out = '';
8
- let bits = 0, buffer = 0;
9
- for (let i = 0; i < str.length; i++) {
10
- buffer = (buffer << 8) | (0xff & str.charCodeAt(i));
11
- bits += 8;
12
-
13
- while (bits > 6) {
14
- bits -= 6;
15
- out += chars[mask & (buffer >> bits)];
16
- }
17
- }
18
-
19
- if (bits) {
20
- out += chars[mask & (buffer << (6 - bits))]
21
- }
22
-
23
- while ((out.length * 6) & 7) {
24
- out += '=';
25
- }
26
-
27
- return out;
28
- };
29
-
30
- var btoa = function (input) {
31
- // todo: throw invalid character for unicode
32
- const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
33
-
34
- let output = "";
35
- let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
36
- let i = 0;
37
-
38
- while (i < input.length) {
39
- chr1 = input.charCodeAt(i++);
40
- chr2 = input.charCodeAt(i++);
41
- chr3 = input.charCodeAt(i++);
42
-
43
- enc1 = chr1 >> 2;
44
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
45
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
46
- enc4 = chr3 & 63;
47
-
48
- if (isNaN(chr2)) {
49
- enc3 = enc4 = 64;
50
- } else if (isNaN(chr3)) {
51
- enc4 = 64;
52
- }
53
-
54
- output += keyStr.charAt(enc1);
55
- output += keyStr.charAt(enc2);
56
- output += keyStr.charAt(enc3);
57
- output += keyStr.charAt(enc4);
58
- }
59
-
60
- return output;
61
- };
62
-
63
- var atob_b = function (input) {
64
- const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
65
-
66
- let output = "";
67
- let chr1, chr2, chr3;
68
- let enc1, enc2, enc3, enc4;
69
- let i = 0;
70
-
71
- while (i < input.length) {
72
- enc1 = keyStr.indexOf(input.charAt(i++));
73
- enc2 = keyStr.indexOf(input.charAt(i++));
74
- enc3 = keyStr.indexOf(input.charAt(i++));
75
- enc4 = keyStr.indexOf(input.charAt(i++));
76
-
77
- chr1 = (enc1 << 2) | (enc2 >> 4);
78
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
79
- chr3 = ((enc3 & 3) << 6) | enc4;
80
-
81
- output += String.fromCharCode(chr1);
82
-
83
- if (enc3 != 64) {
84
- output += String.fromCharCode(chr2);
85
- }
86
- if (enc4 != 64) {
87
- output += String.fromCharCode(chr3);
88
- }
89
- }
90
-
91
- return output;
92
- };
package/runner/info.js DELETED
@@ -1,89 +0,0 @@
1
- import compile from '../compiler/wrap.js';
2
- import fs from 'node:fs';
3
-
4
- /* if (globalThis.process) {
5
- const v8 = await import('node:v8');
6
- v8.setFlagsFromString('--experimental-wasm-gc');
7
- } */
8
-
9
- const raw = process.argv.includes('-raw');
10
-
11
- const file = process.argv.slice(2).find(x => x[0] !== '-');
12
- if (!file) {
13
- // run repl if no file given
14
- await import('./repl.js');
15
-
16
- // do nothing for the rest of this file
17
- await new Promise(() => {});
18
- }
19
-
20
- const source = fs.readFileSync(file, 'utf8');
21
-
22
- const underline = x => `\u001b[4m\u001b[1m${x}\u001b[0m`;
23
- const bold = x => `\u001b[1m${x}\u001b[0m`;
24
-
25
- if (!raw) console.log(`\n${underline('source')}\n` + source);
26
- if (!raw) console.log(`\n\n${underline('processing')}`);
27
-
28
- let cache = '';
29
- const print = str => {
30
- cache += str;
31
-
32
- if (str === '\n') {
33
- process.stdout.write(cache);
34
- cache = '';
35
- }
36
- };
37
-
38
- const t0 = performance.now();
39
- const { wasm, exports, pages } = await compile(source, raw ? [ 'module' ] : [ 'module', 'info' ], {}, print);
40
-
41
- if (!raw && typeof Deno === 'undefined') fs.writeFileSync('out.wasm', Buffer.from(wasm));
42
-
43
- if (!process.argv.includes('-no-run')) {
44
- console.log(`\n\n${underline('output')}`);
45
- const t2 = performance.now();
46
-
47
- exports.main();
48
- print('\n');
49
-
50
- if (!raw) console.log(bold(`\n\nexecuted in ${(performance.now() - t2).toFixed(2)}ms`));
51
- }
52
-
53
- if (!raw) console.log(bold(`wasm binary is ${wasm.byteLength} bytes`));
54
- if (!raw) console.log(`total: ${(performance.now() - t0).toFixed(2)}ms`);
55
-
56
- if (!raw && process.argv.includes('-mem') && exports.$) {
57
- console.log();
58
-
59
- let lastMemory, lastPages;
60
- const PageSize = 65536;
61
- const memoryToString = mem => {
62
- let out = '';
63
- const pages = lastPages.length;
64
- const wasmPages = mem.buffer.byteLength / PageSize;
65
-
66
- out += `\x1B[1mallocated ${mem.buffer.byteLength / 1024}KiB\x1B[0m for ${pages} things using ${wasmPages} Wasm page${wasmPages === 1 ? '' : 's'}\n`;
67
-
68
- const buf = new Uint8Array(mem.buffer);
69
-
70
- for (let i = 0; i < pages; i++) {
71
- out += `\x1B[36m${lastPages[i]}\x1B[2m | \x1B[0m`;
72
-
73
- for (let j = 0; j < 50; j++) {
74
- const val = buf[i * pageSize + j];
75
- if (val === 0) out += '\x1B[2m';
76
- out += val.toString(16).padStart(2, '0');
77
- if (val === 0) out += '\x1B[0m';
78
- out += ' ';
79
- }
80
- out += '\n';
81
- }
82
-
83
- return out;
84
- };
85
-
86
- lastPages = [...pages.keys()];
87
- lastMemory = exports.$;
88
- console.log(memoryToString(lastMemory));
89
- }
package/runner/profile.js DELETED
@@ -1,46 +0,0 @@
1
- import compile from '../compiler/index.js';
2
- import fs from 'node:fs';
3
-
4
- let csv = `phase,time\n`;
5
-
6
- csv += `node,${performance.now()}\n`;
7
-
8
- const t0 = performance.now();
9
- const file = process.argv.slice(2).find(x => x[0] !== '-');
10
- const source = fs.readFileSync(file, 'utf8');
11
- csv += `read,${performance.now() - t0}\n`;
12
-
13
- console.log = x => {
14
- if (x.includes(' in ')) {
15
- csv += [ 'parse', 'codegen', 'opt', 'sections' ][parseInt(x[0]) - 1] + ',' + x.split(' in ')[1].slice(0, -2) + '\n';
16
- }
17
- };
18
-
19
- const wasm = compile(source, [ 'info' ]);
20
-
21
- let cache = '';
22
- const print = str => {
23
- cache += str;
24
-
25
- if (str === '\n') {
26
- process.stdout.write(cache);
27
- cache = '';
28
- }
29
- };
30
-
31
- const t1 = performance.now();
32
- const { instance } = await WebAssembly.instantiate(wasm, {
33
- '': {
34
- p: i => print(Number(i).toString()),
35
- c: i => print(String.fromCharCode(Number(i)))
36
- }
37
- });
38
- csv += `inst,${performance.now() - t1}\n`;
39
-
40
- const t2 = performance.now();
41
- instance.exports.m();
42
- print('\n');
43
-
44
- csv += `exec,${performance.now() - t2}`;
45
-
46
- fs.writeFileSync(`profile.csv`, csv);