porffor 0.2.0-47b4f2e → 0.2.0-4b72c49
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.
- package/README.md +89 -47
- package/compiler/2c.js +321 -71
- package/compiler/builtins/base64.ts +153 -0
- package/compiler/builtins/porffor.d.ts +23 -0
- package/compiler/builtins.js +219 -204
- package/compiler/codeGen.js +635 -301
- package/compiler/decompile.js +11 -12
- package/compiler/encoding.js +2 -116
- package/compiler/generated_builtins.js +15 -0
- package/compiler/index.js +22 -22
- package/compiler/opt.js +61 -26
- package/compiler/parse.js +13 -12
- package/compiler/precompile.js +129 -0
- package/compiler/prefs.js +26 -0
- package/compiler/prototype.js +177 -21
- package/compiler/sections.js +8 -7
- package/compiler/wasmSpec.js +15 -3
- package/compiler/wrap.js +103 -9
- package/demo.js +15 -0
- package/package.json +1 -1
- package/porf +2 -0
- package/rhemyn/compile.js +2 -1
- package/runner/index.js +20 -3
- package/runner/repl.js +2 -2
- package/compiler/builtins/base64.js +0 -92
- package/tmp.c +0 -69
@@ -0,0 +1,153 @@
|
|
1
|
+
// @porf -funsafe-no-unlikely-proto-checks -valtype=i32
|
2
|
+
|
3
|
+
import type { i32, bytestring } from './porffor.d.ts';
|
4
|
+
|
5
|
+
// while (len >= 8) {
|
6
|
+
// Porffor.wasm`
|
7
|
+
// local tmp i64
|
8
|
+
// local.get ${i}
|
9
|
+
// i64.load 0 4
|
10
|
+
// local.set tmp
|
11
|
+
|
12
|
+
// local k i64
|
13
|
+
// i64.const 0
|
14
|
+
// local.set k
|
15
|
+
|
16
|
+
// loop 64
|
17
|
+
// local.get ${j}
|
18
|
+
|
19
|
+
// local.get ${keyStrPtr}
|
20
|
+
|
21
|
+
// local.get tmp
|
22
|
+
|
23
|
+
// ;; k * 6
|
24
|
+
// i64.const 58
|
25
|
+
|
26
|
+
// local.get k
|
27
|
+
// i64.const 6
|
28
|
+
// i64.mul
|
29
|
+
|
30
|
+
// i64.sub
|
31
|
+
|
32
|
+
// ;; tmp >> (58 - (k * 6))
|
33
|
+
// i64.shr_u
|
34
|
+
|
35
|
+
// ;; (tmp >> (58 - (k * 6))) & 0x3f
|
36
|
+
// i64.const 63
|
37
|
+
// i64.and
|
38
|
+
|
39
|
+
// i32.wrap_i64
|
40
|
+
|
41
|
+
// ;; keyStrPtr + ...
|
42
|
+
// i32.add
|
43
|
+
|
44
|
+
// ;; load character from keyStr
|
45
|
+
// i32.load8_u 0 4
|
46
|
+
|
47
|
+
// ;; store in output at j
|
48
|
+
// i32.store8 0 4
|
49
|
+
|
50
|
+
// local.get ${j}
|
51
|
+
// i32.const 1
|
52
|
+
// i32.add
|
53
|
+
// local.set ${j}
|
54
|
+
|
55
|
+
// local.get k
|
56
|
+
// i64.const 1
|
57
|
+
// i64.add
|
58
|
+
// local.tee k
|
59
|
+
|
60
|
+
// i64.const 8
|
61
|
+
// i64.lt_s
|
62
|
+
// br_if 0
|
63
|
+
// end
|
64
|
+
|
65
|
+
// `;
|
66
|
+
|
67
|
+
// // len -= 6;
|
68
|
+
// i += 6;
|
69
|
+
// }
|
70
|
+
|
71
|
+
// // while (k < 8) {
|
72
|
+
// // Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + Porffor.wasm.i32.wrap_i64(Porffor.wasm.i64.and(
|
73
|
+
// // Porffor.wasm.i64.shr_u(tmp, Porffor.wasm.i64.extend_i32_u(58 - k * 6)),
|
74
|
+
// // Porffor.wasm.i64.const(0x3f)
|
75
|
+
// // )), 0, 4), 0, 4);
|
76
|
+
// // k += 1;
|
77
|
+
// // }
|
78
|
+
|
79
|
+
// i += 6;
|
80
|
+
// len -= 6;
|
81
|
+
// }
|
82
|
+
|
83
|
+
export const btoa = (input: bytestring): bytestring => {
|
84
|
+
const keyStr: bytestring = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
85
|
+
const keyStrPtr: i32 = Porffor.i32.ptrUnsafe(keyStr);
|
86
|
+
|
87
|
+
let len: i32 = input.length;
|
88
|
+
let output: bytestring = '';
|
89
|
+
output.length = 4 * (len / 3 + !!(len % 3));
|
90
|
+
|
91
|
+
let i: i32 = Porffor.i32.ptrUnsafe(input),
|
92
|
+
j: i32 = Porffor.i32.ptrUnsafe(output);
|
93
|
+
|
94
|
+
const endPtr = i + len;
|
95
|
+
while (i < endPtr) {
|
96
|
+
const chr1: i32 = Porffor.wasm.i32.load8_u(i++, 0, 4);
|
97
|
+
const chr2: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
|
98
|
+
const chr3: i32 = i < endPtr ? Porffor.wasm.i32.load8_u(i++, 0, 4) : -1;
|
99
|
+
|
100
|
+
const enc1: i32 = chr1 >> 2;
|
101
|
+
const enc2: i32 = ((chr1 & 3) << 4) | (chr2 == -1 ? 0 : (chr2 >> 4));
|
102
|
+
let enc3: i32 = ((chr2 & 15) << 2) | (chr3 == -1 ? 0 : (chr3 >> 6));
|
103
|
+
let enc4: i32 = chr3 & 63;
|
104
|
+
|
105
|
+
if (chr2 == -1) {
|
106
|
+
enc3 = 64;
|
107
|
+
enc4 = 64;
|
108
|
+
} else if (chr3 == -1) {
|
109
|
+
enc4 = 64;
|
110
|
+
}
|
111
|
+
|
112
|
+
Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc1, 0, 4), 0, 4);
|
113
|
+
Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc2, 0, 4), 0, 4);
|
114
|
+
Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc3, 0, 4), 0, 4);
|
115
|
+
Porffor.wasm.i32.store8(j++, Porffor.wasm.i32.load8_u(keyStrPtr + enc4, 0, 4), 0, 4);
|
116
|
+
}
|
117
|
+
|
118
|
+
return output;
|
119
|
+
};
|
120
|
+
|
121
|
+
/* var atob = function (input) {
|
122
|
+
const keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
123
|
+
|
124
|
+
let output = "";
|
125
|
+
let chr1, chr2, chr3;
|
126
|
+
let enc1, enc2, enc3, enc4;
|
127
|
+
let i = 0;
|
128
|
+
|
129
|
+
while (i < input.length) {
|
130
|
+
enc1 = keyStr.indexOf(input.charAt(i++));
|
131
|
+
enc2 = keyStr.indexOf(input.charAt(i++));
|
132
|
+
enc3 = keyStr.indexOf(input.charAt(i++));
|
133
|
+
enc4 = keyStr.indexOf(input.charAt(i++));
|
134
|
+
|
135
|
+
chr1 = (enc1 << 2) | (enc2 >> 4);
|
136
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
137
|
+
chr3 = ((enc3 & 3) << 6) | enc4;
|
138
|
+
|
139
|
+
// output += String.fromCharCode(chr1);
|
140
|
+
Porffor.bytestring.appendCharCode(output, chr1);
|
141
|
+
|
142
|
+
if (enc3 != 64) {
|
143
|
+
// output += String.fromCharCode(chr2);
|
144
|
+
Porffor.bytestring.appendCharCode(output, chr2);
|
145
|
+
}
|
146
|
+
if (enc4 != 64) {
|
147
|
+
// output += String.fromCharCode(chr3);
|
148
|
+
Porffor.bytestring.appendCharCode(output, chr3);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
return output;
|
153
|
+
}; */
|
@@ -0,0 +1,23 @@
|
|
1
|
+
export type i32 = number;
|
2
|
+
export type i64 = number;
|
3
|
+
export type bytestring = string;
|
4
|
+
|
5
|
+
type PorfforGlobal = {
|
6
|
+
wasm: {
|
7
|
+
(...args: any[]): void;
|
8
|
+
i32: {
|
9
|
+
load8_u: (pointer: i32, align: i32, offset: i32) => i32;
|
10
|
+
store8: (pointer: i32, value: i32, align: i32, offset: i32) => i32;
|
11
|
+
}
|
12
|
+
}
|
13
|
+
ptr: (obj: any) => i32;
|
14
|
+
|
15
|
+
i32: {
|
16
|
+
ptr: (obj: any) => i32;
|
17
|
+
ptrUnsafe: (obj: any) => i32;
|
18
|
+
}
|
19
|
+
};
|
20
|
+
|
21
|
+
declare global {
|
22
|
+
const Porffor: PorfforGlobal;
|
23
|
+
}
|
package/compiler/builtins.js
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
import { Blocktype, Opcodes, Valtype } from "./wasmSpec.js";
|
1
|
+
import { Blocktype, Opcodes, Valtype, ValtypeSize } from "./wasmSpec.js";
|
2
2
|
import { number, i32x4 } from "./embedding.js";
|
3
|
+
import Prefs from './prefs.js';
|
4
|
+
import * as GeneratedBuiltins from './generated_builtins.js';
|
3
5
|
|
4
6
|
export const importedFuncs = [
|
5
7
|
{
|
@@ -29,6 +31,21 @@ for (let i = 0; i < importedFuncs.length; i++) {
|
|
29
31
|
|
30
32
|
const char = c => number(c.charCodeAt(0));
|
31
33
|
|
34
|
+
const printStaticStr = str => {
|
35
|
+
const out = [];
|
36
|
+
|
37
|
+
for (let i = 0; i < str.length; i++) {
|
38
|
+
out.push(
|
39
|
+
// ...number(str.charCodeAt(i)),
|
40
|
+
...number(str.charCodeAt(i), Valtype.i32),
|
41
|
+
Opcodes.i32_from_u,
|
42
|
+
[ Opcodes.call, importedFuncs.printChar ]
|
43
|
+
);
|
44
|
+
}
|
45
|
+
|
46
|
+
return out;
|
47
|
+
};
|
48
|
+
|
32
49
|
// todo: somehow diff between these (undefined != null) while remaining falsey in wasm as a number value
|
33
50
|
export const UNDEFINED = 0;
|
34
51
|
export const NULL = 0;
|
@@ -155,25 +172,6 @@ export const BuiltinFuncs = function() {
|
|
155
172
|
]
|
156
173
|
};
|
157
174
|
|
158
|
-
// todo: return false for NaN
|
159
|
-
this.Boolean = {
|
160
|
-
params: [ valtypeBinary ],
|
161
|
-
locals: [],
|
162
|
-
returns: [ valtypeBinary ],
|
163
|
-
returnType: 'boolean',
|
164
|
-
wasm: [
|
165
|
-
[ Opcodes.local_get, 0 ],
|
166
|
-
...(valtype === 'f64' ? [
|
167
|
-
...number(0),
|
168
|
-
[ Opcodes.f64_ne ]
|
169
|
-
] : [
|
170
|
-
...Opcodes.eqz,
|
171
|
-
[ Opcodes.i32_eqz ]
|
172
|
-
]),
|
173
|
-
Opcodes.i32_from
|
174
|
-
]
|
175
|
-
};
|
176
|
-
|
177
175
|
// just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
|
178
176
|
this.Object = {
|
179
177
|
params: [ valtypeBinary ],
|
@@ -187,12 +185,159 @@ export const BuiltinFuncs = function() {
|
|
187
185
|
|
188
186
|
|
189
187
|
this.__console_log = {
|
190
|
-
params: [ valtypeBinary ],
|
191
|
-
|
188
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
189
|
+
typedParams: true,
|
190
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
192
191
|
returns: [],
|
193
|
-
wasm: [
|
194
|
-
[ Opcodes.local_get,
|
195
|
-
|
192
|
+
wasm: (scope, { TYPES, typeSwitch }) => [
|
193
|
+
...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
|
194
|
+
[TYPES.number]: [
|
195
|
+
[ Opcodes.local_get, 0 ],
|
196
|
+
[ Opcodes.call, importedFuncs.print ],
|
197
|
+
],
|
198
|
+
[TYPES.boolean]: [
|
199
|
+
[ Opcodes.local_get, 0 ],
|
200
|
+
Opcodes.i32_to_u,
|
201
|
+
[ Opcodes.if, Blocktype.void ],
|
202
|
+
...printStaticStr('true'),
|
203
|
+
[ Opcodes.else ],
|
204
|
+
...printStaticStr('false'),
|
205
|
+
[ Opcodes.end ]
|
206
|
+
],
|
207
|
+
[TYPES.string]: [
|
208
|
+
// simply print a string :))
|
209
|
+
// cache input pointer as i32
|
210
|
+
[ Opcodes.local_get, 0 ],
|
211
|
+
Opcodes.i32_to_u,
|
212
|
+
[ Opcodes.local_tee, 2 ],
|
213
|
+
|
214
|
+
// make end pointer
|
215
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
216
|
+
...number(ValtypeSize.i16, Valtype.i32),
|
217
|
+
[ Opcodes.i32_mul ],
|
218
|
+
|
219
|
+
[ Opcodes.local_get, 2 ],
|
220
|
+
[ Opcodes.i32_add ],
|
221
|
+
[ Opcodes.local_set, 3 ],
|
222
|
+
|
223
|
+
[ Opcodes.loop, Blocktype.void ],
|
224
|
+
|
225
|
+
// print current char
|
226
|
+
[ Opcodes.local_get, 2 ],
|
227
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
228
|
+
Opcodes.i32_from_u,
|
229
|
+
[ Opcodes.call, importedFuncs.printChar ],
|
230
|
+
|
231
|
+
// increment pointer by sizeof i16
|
232
|
+
[ Opcodes.local_get, 2 ],
|
233
|
+
...number(ValtypeSize.i16, Valtype.i32),
|
234
|
+
[ Opcodes.i32_add ],
|
235
|
+
[ Opcodes.local_tee, 2 ],
|
236
|
+
|
237
|
+
// if pointer != end pointer, loop
|
238
|
+
[ Opcodes.local_get, 3 ],
|
239
|
+
[ Opcodes.i32_ne ],
|
240
|
+
[ Opcodes.br_if, 0 ],
|
241
|
+
|
242
|
+
[ Opcodes.end ]
|
243
|
+
],
|
244
|
+
[TYPES._bytestring]: [
|
245
|
+
// simply print a (byte)string :))
|
246
|
+
// cache input pointer as i32
|
247
|
+
[ Opcodes.local_get, 0 ],
|
248
|
+
Opcodes.i32_to_u,
|
249
|
+
[ Opcodes.local_tee, 2 ],
|
250
|
+
|
251
|
+
// make end pointer
|
252
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
253
|
+
[ Opcodes.local_get, 2 ],
|
254
|
+
[ Opcodes.i32_add ],
|
255
|
+
[ Opcodes.local_set, 3 ],
|
256
|
+
|
257
|
+
[ Opcodes.loop, Blocktype.void ],
|
258
|
+
|
259
|
+
// print current char
|
260
|
+
[ Opcodes.local_get, 2 ],
|
261
|
+
[ Opcodes.i32_load8_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
262
|
+
Opcodes.i32_from_u,
|
263
|
+
[ Opcodes.call, importedFuncs.printChar ],
|
264
|
+
|
265
|
+
// increment pointer
|
266
|
+
[ Opcodes.local_get, 2 ],
|
267
|
+
[ Opcodes.i32_const, 1 ],
|
268
|
+
[ Opcodes.i32_add ],
|
269
|
+
[ Opcodes.local_tee, 2 ],
|
270
|
+
|
271
|
+
// if pointer != end pointer, loop
|
272
|
+
[ Opcodes.local_get, 3 ],
|
273
|
+
[ Opcodes.i32_ne ],
|
274
|
+
[ Opcodes.br_if, 0 ],
|
275
|
+
|
276
|
+
[ Opcodes.end ]
|
277
|
+
],
|
278
|
+
[TYPES._array]: [
|
279
|
+
...printStaticStr('[ '),
|
280
|
+
|
281
|
+
// cache input pointer as i32
|
282
|
+
[ Opcodes.local_get, 0 ],
|
283
|
+
Opcodes.i32_to_u,
|
284
|
+
[ Opcodes.local_tee, 2 ],
|
285
|
+
|
286
|
+
// make end pointer
|
287
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
288
|
+
...number(ValtypeSize[valtype], Valtype.i32),
|
289
|
+
[ Opcodes.i32_mul ],
|
290
|
+
|
291
|
+
[ Opcodes.local_get, 2 ],
|
292
|
+
[ Opcodes.i32_add ],
|
293
|
+
[ Opcodes.local_set, 3 ],
|
294
|
+
|
295
|
+
[ Opcodes.loop, Blocktype.void ],
|
296
|
+
|
297
|
+
// print current char
|
298
|
+
[ Opcodes.local_get, 2 ],
|
299
|
+
[ Opcodes.load, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
300
|
+
[ Opcodes.call, importedFuncs.print ],
|
301
|
+
|
302
|
+
// increment pointer by sizeof valtype
|
303
|
+
[ Opcodes.local_get, 2 ],
|
304
|
+
...number(ValtypeSize[valtype], Valtype.i32),
|
305
|
+
[ Opcodes.i32_add ],
|
306
|
+
[ Opcodes.local_tee, 2 ],
|
307
|
+
|
308
|
+
// if pointer != end pointer, print separator and loop
|
309
|
+
[ Opcodes.local_get, 3 ],
|
310
|
+
[ Opcodes.i32_ne ],
|
311
|
+
[ Opcodes.if, Blocktype.void ],
|
312
|
+
...printStaticStr(', '),
|
313
|
+
[ Opcodes.br, 1 ],
|
314
|
+
[ Opcodes.end ],
|
315
|
+
|
316
|
+
[ Opcodes.end ],
|
317
|
+
|
318
|
+
...printStaticStr(' ]'),
|
319
|
+
],
|
320
|
+
[TYPES.undefined]: [
|
321
|
+
...printStaticStr('undefined')
|
322
|
+
],
|
323
|
+
[TYPES.function]: [
|
324
|
+
...printStaticStr('function () {}')
|
325
|
+
],
|
326
|
+
[TYPES.object]: [
|
327
|
+
[ Opcodes.local_get, 0 ],
|
328
|
+
Opcodes.i32_to_u,
|
329
|
+
[ Opcodes.if, Blocktype.void ],
|
330
|
+
...printStaticStr('{}'),
|
331
|
+
[ Opcodes.else ],
|
332
|
+
...printStaticStr('null'),
|
333
|
+
[ Opcodes.end ]
|
334
|
+
],
|
335
|
+
default: [
|
336
|
+
[ Opcodes.local_get, 0 ],
|
337
|
+
[ Opcodes.call, importedFuncs.print ],
|
338
|
+
]
|
339
|
+
}, Blocktype.void),
|
340
|
+
|
196
341
|
...char('\n'),
|
197
342
|
[ Opcodes.call, importedFuncs.printChar ]
|
198
343
|
]
|
@@ -571,206 +716,76 @@ export const BuiltinFuncs = function() {
|
|
571
716
|
};
|
572
717
|
|
573
718
|
|
574
|
-
this.
|
575
|
-
params: [ Valtype.i32 ],
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
wasm: [
|
589
|
-
[ Opcodes.local_get, 0 ],
|
590
|
-
[ ...Opcodes.i32x4_splat ],
|
591
|
-
]
|
592
|
-
};
|
593
|
-
|
594
|
-
this.__SIMD_i16x8_create = {
|
595
|
-
params: [ Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32, Valtype.i32 ],
|
596
|
-
locals: [],
|
597
|
-
returns: [ Valtype.v128 ],
|
598
|
-
wasm: [
|
599
|
-
...i32x4(0, 0, 0, 0),
|
600
|
-
[ Opcodes.local_get, 0 ],
|
601
|
-
[ ...Opcodes.i16x8_replace_lane, 0 ],
|
602
|
-
[ Opcodes.local_get, 1 ],
|
603
|
-
[ ...Opcodes.i16x8_replace_lane, 1 ],
|
604
|
-
[ Opcodes.local_get, 2 ],
|
605
|
-
[ ...Opcodes.i16x8_replace_lane, 2 ],
|
606
|
-
[ Opcodes.local_get, 3 ],
|
607
|
-
[ ...Opcodes.i16x8_replace_lane, 3 ],
|
608
|
-
[ Opcodes.local_get, 4 ],
|
609
|
-
[ ...Opcodes.i16x8_replace_lane, 4 ],
|
610
|
-
[ Opcodes.local_get, 5 ],
|
611
|
-
[ ...Opcodes.i16x8_replace_lane, 5 ],
|
612
|
-
[ Opcodes.local_get, 6 ],
|
613
|
-
[ ...Opcodes.i16x8_replace_lane, 6 ],
|
614
|
-
[ Opcodes.local_get, 7 ],
|
615
|
-
[ ...Opcodes.i16x8_replace_lane, 7 ],
|
616
|
-
]
|
617
|
-
};
|
618
|
-
|
619
|
-
this.__SIMD_i32x4_dot_i16x8 = {
|
620
|
-
params: [ Valtype.v128, Valtype.v128 ],
|
621
|
-
locals: [],
|
622
|
-
returns: [ Valtype.v128 ],
|
623
|
-
wasm: [
|
624
|
-
[ Opcodes.local_get, 0 ],
|
625
|
-
[ Opcodes.local_get, 1 ],
|
626
|
-
[ ...Opcodes.i32x4_dot_i16x8_s ]
|
627
|
-
]
|
719
|
+
this.__Porffor_type = {
|
720
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
721
|
+
typedParams: true,
|
722
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
723
|
+
returns: [ valtypeBinary ],
|
724
|
+
returnType: Prefs.bytestring ? '_bytestring' : 'string',
|
725
|
+
wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
|
726
|
+
const bc = {};
|
727
|
+
for (const x in TYPE_NAMES) {
|
728
|
+
bc[x] = makeString(scope, TYPE_NAMES[x], false, '#Porffor_type_result');
|
729
|
+
}
|
730
|
+
|
731
|
+
return typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], bc);
|
732
|
+
}
|
628
733
|
};
|
629
734
|
|
630
|
-
|
631
|
-
|
632
|
-
locals: [],
|
633
|
-
returns: [ Valtype.v128 ],
|
634
|
-
wasm: [
|
635
|
-
...i32x4(0, 0, 0, 0),
|
636
|
-
[ Opcodes.local_get, 0 ],
|
637
|
-
[ ...Opcodes.i32x4_replace_lane, 0 ],
|
638
|
-
[ Opcodes.local_get, 1 ],
|
639
|
-
[ ...Opcodes.i32x4_replace_lane, 1 ],
|
640
|
-
[ Opcodes.local_get, 2 ],
|
641
|
-
[ ...Opcodes.i32x4_replace_lane, 2 ],
|
642
|
-
[ Opcodes.local_get, 3 ],
|
643
|
-
[ ...Opcodes.i32x4_replace_lane, 3 ],
|
644
|
-
]
|
645
|
-
};
|
735
|
+
const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
|
736
|
+
const out = [];
|
646
737
|
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
wasm: [
|
652
|
-
[ Opcodes.local_get, 0 ],
|
653
|
-
[ Opcodes.local_get, 1 ],
|
654
|
-
[ ...Opcodes.i32x4_add ]
|
655
|
-
]
|
656
|
-
};
|
738
|
+
for (let i = 0; i < arr.length; i++) {
|
739
|
+
out.push(...getter, ...number(arr[i], valtype), valtype === Valtype.f64 ? [ Opcodes.f64_eq ] : [ Opcodes.i32_eq ]);
|
740
|
+
if (i !== 0) out.push([ Opcodes.i32_or ]);
|
741
|
+
}
|
657
742
|
|
658
|
-
|
659
|
-
params: [ Valtype.v128, Valtype.v128 ],
|
660
|
-
locals: [],
|
661
|
-
returns: [ Valtype.v128 ],
|
662
|
-
wasm: [
|
663
|
-
[ Opcodes.local_get, 0 ],
|
664
|
-
[ Opcodes.local_get, 1 ],
|
665
|
-
[ ...Opcodes.i32x4_sub ]
|
666
|
-
]
|
743
|
+
return out;
|
667
744
|
};
|
668
745
|
|
669
|
-
this.
|
670
|
-
params: [
|
671
|
-
|
672
|
-
|
673
|
-
|
746
|
+
this.__Porffor_ptr = {
|
747
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
748
|
+
typedParams: true,
|
749
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
750
|
+
returns: [ valtypeBinary ],
|
751
|
+
wasm: (scope, { TYPES }) => [
|
752
|
+
...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
|
753
|
+
[ Opcodes.if, valtypeBinary ],
|
674
754
|
[ Opcodes.local_get, 0 ],
|
675
|
-
[ Opcodes.
|
676
|
-
|
755
|
+
[ Opcodes.else ],
|
756
|
+
...number(NaN),
|
757
|
+
[ Opcodes.end ]
|
677
758
|
]
|
678
759
|
};
|
679
760
|
|
680
|
-
this.
|
681
|
-
params: [ Valtype.
|
682
|
-
|
761
|
+
this.__Porffor_i32_ptr = {
|
762
|
+
params: [ Valtype.i32, Valtype.i32 ],
|
763
|
+
typedParams: true,
|
764
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
683
765
|
returns: [ Valtype.i32 ],
|
684
|
-
wasm: [
|
766
|
+
wasm: (scope, { TYPES }) => [
|
767
|
+
...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
|
768
|
+
[ Opcodes.if, Valtype.i32 ],
|
685
769
|
[ Opcodes.local_get, 0 ],
|
686
|
-
[
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
this.__SIMD_i32x4_get1 = {
|
691
|
-
params: [ Valtype.v128 ],
|
692
|
-
locals: [],
|
693
|
-
returns: [ Valtype.i32 ],
|
694
|
-
wasm: [
|
695
|
-
[ Opcodes.local_get, 0 ],
|
696
|
-
[ ...Opcodes.i32x4_extract_lane, 1 ],
|
697
|
-
],
|
698
|
-
};
|
699
|
-
|
700
|
-
this.__SIMD_i32x4_get2 = {
|
701
|
-
params: [ Valtype.v128 ],
|
702
|
-
locals: [],
|
703
|
-
returns: [ Valtype.i32 ],
|
704
|
-
wasm: [
|
705
|
-
[ Opcodes.local_get, 0 ],
|
706
|
-
[ ...Opcodes.i32x4_extract_lane, 2 ],
|
707
|
-
],
|
708
|
-
};
|
709
|
-
|
710
|
-
this.__SIMD_i32x4_get3 = {
|
711
|
-
params: [ Valtype.v128 ],
|
712
|
-
locals: [],
|
713
|
-
returns: [ Valtype.i32 ],
|
714
|
-
wasm: [
|
715
|
-
[ Opcodes.local_get, 0 ],
|
716
|
-
[ ...Opcodes.i32x4_extract_lane, 3 ],
|
717
|
-
],
|
718
|
-
};
|
719
|
-
|
720
|
-
this.__SIMD_i32x4_shuffle_000c = {
|
721
|
-
params: [ Valtype.v128 ],
|
722
|
-
locals: [],
|
723
|
-
returns: [ Valtype.v128 ],
|
724
|
-
wasm: [
|
725
|
-
[ Opcodes.local_get, 0 ],
|
726
|
-
...i32x4(0, 0, 0, 0),
|
727
|
-
[ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 9, 10, 11 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, 0, c)
|
770
|
+
[ Opcodes.else ],
|
771
|
+
...number(-1, Valtype.i32),
|
772
|
+
[ Opcodes.end ]
|
728
773
|
]
|
729
774
|
};
|
730
775
|
|
731
|
-
|
732
|
-
|
776
|
+
// unsafe: does not check type just ~casts to number
|
777
|
+
this.__Porffor_i32_ptrUnsafe = {
|
778
|
+
params: [ Valtype.i32 ],
|
733
779
|
locals: [],
|
734
|
-
returns: [ Valtype.
|
780
|
+
returns: [ Valtype.i32 ],
|
735
781
|
wasm: [
|
736
782
|
[ Opcodes.local_get, 0 ],
|
737
|
-
...i32x4(0, 0, 0, 0),
|
738
|
-
[ ...Opcodes.i8x16_shuffle, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7 ], // i32x4 (a, b, c, d) -> i32x4 (0, 0, a, b)
|
739
783
|
]
|
740
784
|
};
|
741
|
-
};
|
742
785
|
|
743
|
-
export const BuiltinPreludes = {
|
744
|
-
btoa: `var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
745
|
-
var btoa = function (input) {
|
746
|
-
// todo: throw invalid character for unicode
|
747
|
-
|
748
|
-
let output = "";
|
749
|
-
let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
750
|
-
let i = 0;
|
751
|
-
|
752
|
-
while (i < input.length) {
|
753
|
-
chr1 = input.charCodeAt(i++);
|
754
|
-
chr2 = input.charCodeAt(i++);
|
755
|
-
chr3 = input.charCodeAt(i++);
|
756
|
-
|
757
|
-
enc1 = chr1 >> 2;
|
758
|
-
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
759
|
-
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
760
|
-
enc4 = chr3 & 63;
|
761
|
-
|
762
|
-
if (isNaN(chr2)) {
|
763
|
-
enc3 = enc4 = 64;
|
764
|
-
} else if (isNaN(chr3)) {
|
765
|
-
enc4 = 64;
|
766
|
-
}
|
767
786
|
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
output += keyStr.charAt(enc4);
|
787
|
+
const generated = new GeneratedBuiltins.BuiltinFuncs();
|
788
|
+
for (const x in generated) {
|
789
|
+
this[x] = generated[x];
|
772
790
|
}
|
773
|
-
|
774
|
-
return output;
|
775
|
-
};`
|
776
791
|
};
|