porffor 0.2.0-c7b7423 → 0.2.0-c908b46

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 (55) hide show
  1. package/CONTRIBUTING.md +256 -0
  2. package/LICENSE +20 -20
  3. package/README.md +157 -77
  4. package/asur/README.md +2 -0
  5. package/asur/index.js +1262 -0
  6. package/byg/index.js +237 -0
  7. package/compiler/2c.js +322 -72
  8. package/compiler/{sections.js → assemble.js} +64 -16
  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 +21 -0
  14. package/compiler/builtins/crypto.ts +120 -0
  15. package/compiler/builtins/date.ts +2070 -0
  16. package/compiler/builtins/escape.ts +141 -0
  17. package/compiler/builtins/int.ts +147 -0
  18. package/compiler/builtins/number.ts +534 -0
  19. package/compiler/builtins/porffor.d.ts +59 -0
  20. package/compiler/builtins/string.ts +1070 -0
  21. package/compiler/builtins/tostring.ts +37 -0
  22. package/compiler/builtins.js +580 -272
  23. package/compiler/{codeGen.js → codegen.js} +1390 -553
  24. package/compiler/decompile.js +3 -4
  25. package/compiler/embedding.js +22 -22
  26. package/compiler/encoding.js +98 -114
  27. package/compiler/generated_builtins.js +1517 -0
  28. package/compiler/index.js +36 -34
  29. package/compiler/log.js +6 -3
  30. package/compiler/opt.js +65 -29
  31. package/compiler/parse.js +38 -29
  32. package/compiler/precompile.js +128 -0
  33. package/compiler/prefs.js +27 -0
  34. package/compiler/prototype.js +182 -42
  35. package/compiler/types.js +37 -0
  36. package/compiler/wasmSpec.js +31 -7
  37. package/compiler/wrap.js +141 -43
  38. package/package.json +9 -5
  39. package/porf +4 -0
  40. package/rhemyn/compile.js +46 -27
  41. package/rhemyn/parse.js +322 -320
  42. package/rhemyn/test/parse.js +58 -58
  43. package/runner/compare.js +34 -34
  44. package/runner/debug.js +122 -0
  45. package/runner/index.js +91 -11
  46. package/runner/profiler.js +102 -0
  47. package/runner/repl.js +44 -11
  48. package/runner/sizes.js +37 -37
  49. package/compiler/builtins/base64.js +0 -92
  50. package/runner/info.js +0 -89
  51. package/runner/profile.js +0 -46
  52. package/runner/results.json +0 -1
  53. package/runner/transform.js +0 -15
  54. package/tmp.c +0 -69
  55. package/util/enum.js +0 -20
@@ -15,7 +15,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
15
15
  if (name) out += `${makeSignature(params, returns)} ;; $${name} (${ind})\n`;
16
16
 
17
17
  const justLocals = Object.values(locals).sort((a, b) => a.idx - b.idx).slice(params.length);
18
- if (justLocals.length > 0) out += ` local ${justLocals.map(x => invValtype[x.type]).join(' ')}\n`;
18
+ if (name && justLocals.length > 0) out += ` local ${justLocals.map(x => invValtype[x.type]).join(' ')}\n`;
19
19
 
20
20
  let i = -1, lastInst;
21
21
  let byte = 0;
@@ -32,7 +32,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
32
32
  inst = [ [ inst[0], inst[1] ], ...inst.slice(2) ];
33
33
  }
34
34
 
35
- if (inst[0] === Opcodes.end || inst[0] === Opcodes.else || inst[0] === Opcodes.catch_all) depth--;
35
+ if (depth > 0 && (inst[0] === Opcodes.end || inst[0] === Opcodes.else || inst[0] === Opcodes.catch_all)) depth--;
36
36
 
37
37
  out += ' '.repeat(Math.max(0, depth * 2));
38
38
 
@@ -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);
@@ -119,7 +118,7 @@ export default (wasm, name = '', ind = 0, locals = {}, params = [], returns = []
119
118
  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
- .replace(/[^m](i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `${_[0]}\x1B[36m${_.slice(1)}\x1B[0m`)
121
+ .replace(/(i32|i64|f32|f64|drop)(\.[^\s]*)?/g, _ => `\x1B[36m${_}\x1B[0m`)
123
122
  .replace(/(return_call|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`)
@@ -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,22 @@ 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 => [
11
+ // str.length,
12
+ // ...codifyString(str)
13
+ // ];
14
+ export const encodeString = str => unsignedLEB128(str.length).concat(codifyString(str));
15
+
16
+ // export const encodeVector = data => [
17
+ // ...unsignedLEB128(data.length),
18
+ // ...data.flat()
19
+ // ];
20
+ // export const encodeVector = data => {
21
+ // const out = data.flat();
22
+ // out.unshift.apply(out, unsignedLEB128(data.length));
23
+ // return out;
24
+ // };
25
+ export const encodeVector = data => unsignedLEB128(data.length).concat(data.flat());
19
26
 
20
27
  export const encodeLocal = (count, type) => [
21
28
  ...unsignedLEB128(count),
@@ -24,6 +31,8 @@ export const encodeLocal = (count, type) => [
24
31
 
25
32
  // todo: this only works with integers within 32 bit range
26
33
  export const signedLEB128 = n => {
34
+ if (typeof n === 'bigint') return big_signedLEB128(n);
35
+
27
36
  n |= 0;
28
37
 
29
38
  // just input for small numbers (for perf as common)
@@ -50,6 +59,8 @@ export const signedLEB128 = n => {
50
59
  };
51
60
 
52
61
  export const unsignedLEB128 = n => {
62
+ if (typeof n === 'bigint') return big_unsignedLEB128(n);
63
+
53
64
  n |= 0;
54
65
 
55
66
  // just input for small numbers (for perf as common)
@@ -67,6 +78,46 @@ export const unsignedLEB128 = n => {
67
78
  return buffer;
68
79
  };
69
80
 
81
+ export const big_signedLEB128 = n => {
82
+ // just input for small numbers (for perf as common)
83
+ if (n >= 0n && n <= 63n) return [ Number(n) ];
84
+ if (n >= -64n && n <= 0n) return [ 128 + Number(n) ];
85
+
86
+ const buffer = [];
87
+
88
+ while (true) {
89
+ let byte = Number(n & 0x7fn);
90
+ n >>= 7n;
91
+
92
+ if ((n === 0n && (byte & 0x40) === 0) || (n === -1n && (byte & 0x40) !== 0)) {
93
+ buffer.push(byte);
94
+ break;
95
+ } else {
96
+ byte |= 0x80n;
97
+ }
98
+
99
+ buffer.push(byte);
100
+ }
101
+
102
+ return buffer;
103
+ };
104
+
105
+ export const big_unsignedLEB128 = n => {
106
+ // just input for small numbers (for perf as common)
107
+ if (n >= 0n && n <= 127n) return [ n ];
108
+
109
+ const buffer = [];
110
+ do {
111
+ let byte = Number(n & 0x7fn);
112
+ n >>>= 7n;
113
+ if (n !== 0n) {
114
+ byte |= 0x80;
115
+ }
116
+ buffer.push(byte);
117
+ } while (n !== 0n);
118
+ return buffer;
119
+ };
120
+
70
121
  export const read_signedLEB128 = _input => {
71
122
  const input = [..._input];
72
123
  let result = 0, shift = 0;
@@ -105,119 +156,52 @@ export const read_unsignedLEB128 = _input => {
105
156
  };
106
157
 
107
158
  // ieee 754 binary64
159
+ // const ieee754Cache = {};
160
+ // export const ieee754_binary64 = value => {
161
+ // if (ieee754Cache[value]) return ieee754Cache[value];
162
+ // return ieee754Cache[value] = [...new Uint8Array(new Float64Array([ value ]).buffer)];
163
+ // };
164
+ export const ieee754_binary64 = value => [...new Uint8Array(new Float64Array([ value ]).buffer)];
165
+ export const read_ieee754_binary64 = buffer => new Float64Array(new Uint8Array(buffer).buffer)[0];
108
166
 
109
- // from https://github.com/feross/ieee754
110
- // BSD 3-Clause. Copyright 2008 Fair Oaks Labs, Inc. (https://github.com/feross/ieee754/blob/master/LICENSE)
111
- export const ieee754_binary64 = value => {
112
- return [...new Uint8Array(new Float64Array([ value ]).buffer)];
113
-
114
- let isLE = true, mLen = 52, nBytes = 8, offset = 0;
115
- let buffer = new Array(nBytes).fill(0);
116
-
117
- let e, m, c
118
- let eLen = (nBytes * 8) - mLen - 1
119
- const eMax = (1 << eLen) - 1
120
- const eBias = eMax >> 1
121
- const rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
122
- let i = isLE ? 0 : (nBytes - 1)
123
- const d = isLE ? 1 : -1
124
- const s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
125
-
126
- value = Math.abs(value)
127
-
128
- if (isNaN(value) || value === Infinity) {
129
- m = isNaN(value) ? 1 : 0
130
- e = eMax
131
- } else {
132
- e = Math.floor(Math.log(value) / Math.LN2)
133
- if (value * (c = Math.pow(2, -e)) < 1) {
134
- e--
135
- c *= 2
136
- }
137
- if (e + eBias >= 1) {
138
- value += rt / c
139
- } else {
140
- value += rt * Math.pow(2, 1 - eBias)
141
- }
142
- if (value * c >= 2) {
143
- e++
144
- c /= 2
145
- }
146
167
 
147
- if (e + eBias >= eMax) {
148
- m = 0
149
- e = eMax
150
- } else if (e + eBias >= 1) {
151
- m = ((value * c) - 1) * Math.pow(2, mLen)
152
- e = e + eBias
168
+ // into funcs append to a given existing buffer instead of creating our own for perf
169
+ export const signedLEB128_into = (n, buffer) => {
170
+ n |= 0;
171
+
172
+ // just input for small numbers (for perf as common)
173
+ if (n >= 0 && n <= 63) return buffer.push(n);
174
+ if (n >= -64 && n <= 0) return buffer.push(128 + n);
175
+
176
+ while (true) {
177
+ let byte = n & 0x7f;
178
+ n >>= 7;
179
+
180
+ if ((n === 0 && (byte & 0x40) === 0) || (n === -1 && (byte & 0x40) !== 0)) {
181
+ buffer.push(byte);
182
+ break;
153
183
  } else {
154
- m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
155
- e = 0
184
+ byte |= 0x80;
156
185
  }
157
- }
158
186
 
159
- while (mLen >= 8) {
160
- buffer[offset + i] = m & 0xff
161
- i += d
162
- m /= 256
163
- mLen -= 8
187
+ buffer.push(byte);
164
188
  }
189
+ };
165
190
 
166
- e = (e << mLen) | m
167
- eLen += mLen
168
- while (eLen > 0) {
169
- buffer[offset + i] = e & 0xff
170
- i += d
171
- e /= 256
172
- eLen -= 8
173
- }
191
+ export const unsignedLEB128_into = (n, buffer) => {
192
+ n |= 0;
174
193
 
175
- buffer[offset + i - d] |= s * 128
194
+ // just input for small numbers (for perf as common)
195
+ if (n >= 0 && n <= 127) return buffer.push(n);
176
196
 
177
- return buffer;
197
+ do {
198
+ let byte = n & 0x7f;
199
+ n >>>= 7;
200
+ if (n !== 0) {
201
+ byte |= 0x80;
202
+ }
203
+ buffer.push(byte);
204
+ } while (n !== 0);
178
205
  };
179
206
 
180
- export const read_ieee754_binary64 = buffer => {
181
- return new Float64Array(new Uint8Array(buffer).buffer)[0];
182
-
183
- let isLE = true, mLen = 52, nBytes = 8, offset = 0;
184
-
185
- let e, m
186
- const eLen = (nBytes * 8) - mLen - 1
187
- const eMax = (1 << eLen) - 1
188
- const eBias = eMax >> 1
189
- let nBits = -7
190
- let i = isLE ? (nBytes - 1) : 0
191
- const d = isLE ? -1 : 1
192
- let s = buffer[offset + i]
193
-
194
- i += d
195
-
196
- e = s & ((1 << (-nBits)) - 1)
197
- s >>= (-nBits)
198
- nBits += eLen
199
- while (nBits > 0) {
200
- e = (e * 256) + buffer[offset + i]
201
- i += d
202
- nBits -= 8
203
- }
204
-
205
- m = e & ((1 << (-nBits)) - 1)
206
- e >>= (-nBits)
207
- nBits += mLen
208
- while (nBits > 0) {
209
- m = (m * 256) + buffer[offset + i]
210
- i += d
211
- nBits -= 8
212
- }
213
-
214
- if (e === 0) {
215
- e = 1 - eBias
216
- } else if (e === eMax) {
217
- return m ? NaN : ((s ? -1 : 1) * Infinity)
218
- } else {
219
- m = m + Math.pow(2, mLen)
220
- e = e - eBias
221
- }
222
- return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
223
- };
207
+ export const ieee754_binary64_into = (value, buffer) => buffer.push(...new Uint8Array(new Float64Array([ value ]).buffer));