porffor 0.2.0-fde989a → 0.14.0-0d97d1e6a

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 (61) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/LICENSE +20 -20
  3. package/README.md +131 -86
  4. package/asur/README.md +2 -0
  5. package/asur/index.js +1262 -0
  6. package/byg/index.js +216 -0
  7. package/compiler/2c.js +2 -53
  8. package/compiler/{sections.js → assemble.js} +95 -21
  9. package/compiler/builtins/annexb_string.js +72 -0
  10. package/compiler/builtins/annexb_string.ts +18 -0
  11. package/compiler/builtins/array.ts +145 -0
  12. package/compiler/builtins/base64.ts +76 -0
  13. package/compiler/builtins/boolean.ts +18 -0
  14. package/compiler/builtins/crypto.ts +120 -0
  15. package/compiler/builtins/date.ts +2067 -0
  16. package/compiler/builtins/escape.ts +141 -0
  17. package/compiler/builtins/function.ts +5 -0
  18. package/compiler/builtins/int.ts +145 -0
  19. package/compiler/builtins/number.ts +529 -0
  20. package/compiler/builtins/object.ts +4 -0
  21. package/compiler/builtins/porffor.d.ts +60 -0
  22. package/compiler/builtins/set.ts +187 -0
  23. package/compiler/builtins/string.ts +1080 -0
  24. package/compiler/builtins/symbol.ts +61 -0
  25. package/compiler/builtins.js +440 -285
  26. package/compiler/{codeGen.js → codegen.js} +1116 -489
  27. package/compiler/decompile.js +3 -4
  28. package/compiler/embedding.js +22 -22
  29. package/compiler/encoding.js +94 -10
  30. package/compiler/expression.js +1 -1
  31. package/compiler/generated_builtins.js +1670 -0
  32. package/compiler/index.js +27 -43
  33. package/compiler/log.js +6 -3
  34. package/compiler/opt.js +55 -41
  35. package/compiler/parse.js +38 -30
  36. package/compiler/precompile.js +120 -0
  37. package/compiler/prefs.js +31 -0
  38. package/compiler/prototype.js +31 -46
  39. package/compiler/types.js +38 -0
  40. package/compiler/wasmSpec.js +33 -8
  41. package/compiler/wrap.js +107 -71
  42. package/package.json +9 -5
  43. package/porf +2 -0
  44. package/rhemyn/compile.js +46 -27
  45. package/rhemyn/parse.js +322 -320
  46. package/rhemyn/test/parse.js +58 -58
  47. package/runner/compare.js +33 -34
  48. package/runner/debug.js +117 -0
  49. package/runner/index.js +78 -11
  50. package/runner/profiler.js +75 -0
  51. package/runner/repl.js +40 -13
  52. package/runner/sizes.js +37 -37
  53. package/runner/version.js +10 -8
  54. package/compiler/builtins/base64.js +0 -92
  55. package/filesize.cmd +0 -2
  56. package/runner/info.js +0 -89
  57. package/runner/profile.js +0 -46
  58. package/runner/results.json +0 -1
  59. package/runner/transform.js +0 -15
  60. package/tmp.c +0 -661
  61. package/util/enum.js +0 -20
@@ -1,5 +1,5 @@
1
- import { Blocktype, Opcodes, Valtype } from "./wasmSpec.js";
2
- import { read_ieee754_binary64, read_signedLEB128, read_unsignedLEB128 } from "./encoding.js";
1
+ import { Blocktype, Opcodes, Valtype } from './wasmSpec.js';
2
+ import { read_ieee754_binary64, read_signedLEB128, read_unsignedLEB128 } from './encoding.js';
3
3
 
4
4
  const inv = (obj, keyMap = x => x) => Object.keys(obj).reduce((acc, x) => { acc[keyMap(obj[x])] = x; return acc; }, {});
5
5
  const invOpcodes = inv(Opcodes);
@@ -110,7 +110,6 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
110
110
 
111
111
  out += '\n';
112
112
  lastInst = inst;
113
- i++;
114
113
  }
115
114
 
116
115
  return highlightAsm(out);
@@ -120,7 +119,7 @@ export const highlightAsm = asm => asm
120
119
  .replace(/(local|global|memory)\.[^\s]*/g, _ => `\x1B[31m${_}\x1B[0m`)
121
120
  .replace(/(i(8|16|32|64)x[0-9]+|v128)(\.[^\s]*)?/g, _ => `\x1B[34m${_}\x1B[0m`)
122
121
  .replace(/(i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `\x1B[36m${_}\x1B[0m`)
123
- .replace(/(return_call|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
122
+ .replace(/(return_call|call_indirect|call|br_if|br|return|rethrow|throw)/g, _ => `\x1B[35m${_}\x1B[0m`)
124
123
  .replace(/(block|loop|if|end|else|try|catch_all|catch|delegate)/g, _ => `\x1B[95m${_}\x1B[0m`)
125
124
  .replace(/unreachable/g, _ => `\x1B[91m${_}\x1B[0m`)
126
125
  .replace(/ \-?[0-9\.]+/g, _ => ` \x1B[33m${_.slice(1)}\x1B[0m`)
@@ -1,23 +1,23 @@
1
- import { Opcodes, Valtype } from "./wasmSpec.js";
2
- import { signedLEB128, ieee754_binary64 } from "./encoding.js";
3
-
4
- export const number = (n, valtype = valtypeBinary) => {
5
- switch (valtype) {
6
- case Valtype.i32: return [ [ Opcodes.i32_const, ...signedLEB128(n) ] ];
7
- case Valtype.i64: return [ [ Opcodes.i64_const, ...signedLEB128(n) ] ];
8
- case Valtype.f64: return [ [ Opcodes.f64_const, ...ieee754_binary64(n) ] ];
9
- }
10
- };
11
-
12
- export const enforceOneByte = arr => [ arr[0] ?? 0 ];
13
- export const enforceTwoBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0 ];
14
- export const enforceFourBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0, arr[2] ?? 0, arr[3] ?? 0 ];
15
- export const enforceEightBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0, arr[2] ?? 0, arr[3] ?? 0, arr[4] ?? 0, arr[5] ?? 0, arr[6] ?? 0, arr[7] ?? 0 ];
16
-
17
- export const i32x4 = (a, b, c, d) => [ [
18
- ...Opcodes.v128_const,
19
- ...enforceFourBytes(signedLEB128(a)),
20
- ...enforceFourBytes(signedLEB128(b)),
21
- ...enforceFourBytes(signedLEB128(c)),
22
- ...enforceFourBytes(signedLEB128(d))
1
+ import { Opcodes, Valtype } from './wasmSpec.js';
2
+ import { signedLEB128, ieee754_binary64 } from './encoding.js';
3
+
4
+ export const number = (n, valtype = valtypeBinary) => {
5
+ switch (valtype) {
6
+ case Valtype.i32: return [ [ Opcodes.i32_const, ...signedLEB128(n) ] ];
7
+ case Valtype.i64: return [ [ Opcodes.i64_const, ...signedLEB128(n) ] ];
8
+ case Valtype.f64: return [ [ Opcodes.f64_const, ...ieee754_binary64(n) ] ];
9
+ }
10
+ };
11
+
12
+ export const enforceOneByte = arr => [ arr[0] ?? 0 ];
13
+ export const enforceTwoBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0 ];
14
+ export const enforceFourBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0, arr[2] ?? 0, arr[3] ?? 0 ];
15
+ export const enforceEightBytes = arr => [ arr[0] ?? 0, arr[1] ?? 0, arr[2] ?? 0, arr[3] ?? 0, arr[4] ?? 0, arr[5] ?? 0, arr[6] ?? 0, arr[7] ?? 0 ];
16
+
17
+ export const i32x4 = (a, b, c, d) => [ [
18
+ ...Opcodes.v128_const,
19
+ ...enforceFourBytes(signedLEB128(a)),
20
+ ...enforceFourBytes(signedLEB128(b)),
21
+ ...enforceFourBytes(signedLEB128(c)),
22
+ ...enforceFourBytes(signedLEB128(d))
23
23
  ] ];
@@ -7,15 +7,8 @@ export const codifyString = str => {
7
7
  return out;
8
8
  };
9
9
 
10
- export const encodeString = str => [
11
- str.length,
12
- ...codifyString(str)
13
- ];
14
-
15
- export const encodeVector = data => [
16
- ...unsignedLEB128(data.length),
17
- ...data.flat()
18
- ];
10
+ export const encodeString = str => unsignedLEB128(str.length).concat(codifyString(str));
11
+ export const encodeVector = data => unsignedLEB128(data.length).concat(data.flat());
19
12
 
20
13
  export const encodeLocal = (count, type) => [
21
14
  ...unsignedLEB128(count),
@@ -24,6 +17,8 @@ export const encodeLocal = (count, type) => [
24
17
 
25
18
  // todo: this only works with integers within 32 bit range
26
19
  export const signedLEB128 = n => {
20
+ if (typeof n === 'bigint') return big_signedLEB128(n);
21
+
27
22
  n |= 0;
28
23
 
29
24
  // just input for small numbers (for perf as common)
@@ -50,6 +45,8 @@ export const signedLEB128 = n => {
50
45
  };
51
46
 
52
47
  export const unsignedLEB128 = n => {
48
+ if (typeof n === 'bigint') return big_unsignedLEB128(n);
49
+
53
50
  n |= 0;
54
51
 
55
52
  // just input for small numbers (for perf as common)
@@ -67,6 +64,46 @@ export const unsignedLEB128 = n => {
67
64
  return buffer;
68
65
  };
69
66
 
67
+ export const big_signedLEB128 = n => {
68
+ // just input for small numbers (for perf as common)
69
+ if (n >= 0n && n <= 63n) return [ Number(n) ];
70
+ if (n >= -64n && n <= 0n) return [ 128 + Number(n) ];
71
+
72
+ const buffer = [];
73
+
74
+ while (true) {
75
+ let byte = Number(n & 0x7fn);
76
+ n >>= 7n;
77
+
78
+ if ((n === 0n && (byte & 0x40) === 0) || (n === -1n && (byte & 0x40) !== 0)) {
79
+ buffer.push(byte);
80
+ break;
81
+ } else {
82
+ byte |= 0x80n;
83
+ }
84
+
85
+ buffer.push(byte);
86
+ }
87
+
88
+ return buffer;
89
+ };
90
+
91
+ export const big_unsignedLEB128 = n => {
92
+ // just input for small numbers (for perf as common)
93
+ if (n >= 0n && n <= 127n) return [ n ];
94
+
95
+ const buffer = [];
96
+ do {
97
+ let byte = Number(n & 0x7fn);
98
+ n >>>= 7n;
99
+ if (n !== 0n) {
100
+ byte |= 0x80;
101
+ }
102
+ buffer.push(byte);
103
+ } while (n !== 0n);
104
+ return buffer;
105
+ };
106
+
70
107
  export const read_signedLEB128 = _input => {
71
108
  const input = [..._input];
72
109
  let result = 0, shift = 0;
@@ -105,5 +142,52 @@ export const read_unsignedLEB128 = _input => {
105
142
  };
106
143
 
107
144
  // ieee 754 binary64
145
+ // const ieee754Cache = {};
146
+ // export const ieee754_binary64 = value => {
147
+ // if (ieee754Cache[value]) return ieee754Cache[value];
148
+ // return ieee754Cache[value] = [...new Uint8Array(new Float64Array([ value ]).buffer)];
149
+ // };
108
150
  export const ieee754_binary64 = value => [...new Uint8Array(new Float64Array([ value ]).buffer)];
109
- export const read_ieee754_binary64 = buffer => new Float64Array(new Uint8Array(buffer).buffer)[0];
151
+ export const read_ieee754_binary64 = buffer => new Float64Array(new Uint8Array(buffer).buffer)[0];
152
+
153
+
154
+ // into funcs append to a given existing buffer instead of creating our own for perf
155
+ export const signedLEB128_into = (n, buffer) => {
156
+ n |= 0;
157
+
158
+ // just input for small numbers (for perf as common)
159
+ if (n >= 0 && n <= 63) return buffer.push(n);
160
+ if (n >= -64 && n <= 0) return buffer.push(128 + n);
161
+
162
+ while (true) {
163
+ let byte = n & 0x7f;
164
+ n >>= 7;
165
+
166
+ if ((n === 0 && (byte & 0x40) === 0) || (n === -1 && (byte & 0x40) !== 0)) {
167
+ buffer.push(byte);
168
+ break;
169
+ } else {
170
+ byte |= 0x80;
171
+ }
172
+
173
+ buffer.push(byte);
174
+ }
175
+ };
176
+
177
+ export const unsignedLEB128_into = (n, buffer) => {
178
+ n |= 0;
179
+
180
+ // just input for small numbers (for perf as common)
181
+ if (n >= 0 && n <= 127) return buffer.push(n);
182
+
183
+ do {
184
+ let byte = n & 0x7f;
185
+ n >>>= 7;
186
+ if (n !== 0) {
187
+ byte |= 0x80;
188
+ }
189
+ buffer.push(byte);
190
+ } while (n !== 0);
191
+ };
192
+
193
+ export const ieee754_binary64_into = (value, buffer) => buffer.push(...new Uint8Array(new Float64Array([ value ]).buffer));
@@ -1,4 +1,4 @@
1
- import { Opcodes } from "./wasmSpec.js";
1
+ import { Opcodes } from './wasmSpec.js';
2
2
 
3
3
  export const operatorOpcode = {
4
4
  i32: {