porffor 0.16.0-688a50c13 → 0.16.0-79cd8c0c8
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/CONTRIBUTING.md +1 -1
- package/compiler/builtins/date.ts +4 -2
- package/compiler/builtins/set.ts +4 -2
- package/compiler/codegen.js +291 -195
- package/compiler/cyclone.js +11 -11
- package/compiler/generated_builtins.js +18 -18
- package/compiler/opt.js +5 -7
- package/compiler/parse.js +1 -1
- package/compiler/precompile.js +6 -11
- package/compiler/prefs.js +1 -1
- package/compiler/prototype.js +43 -34
- package/compiler/wrap.js +15 -15
- package/package.json +4 -2
- package/runner/index.js +3 -8
- package/a.txt +0 -457
- package/b.txt +0 -457
- package/compiler/allocators/grow.js +0 -26
- package/compiler/allocators/index.js +0 -10
- package/compiler/allocators/static.js +0 -42
package/compiler/prototype.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { Opcodes, Blocktype, Valtype, ValtypeSize } from './wasmSpec.js';
|
2
2
|
import { number } from './embedding.js';
|
3
|
+
import { unsignedLEB128 } from './encoding.js';
|
3
4
|
import { UNDEFINED } from './builtins.js';
|
4
5
|
import { TYPES } from './types.js';
|
5
6
|
import Prefs from './prefs.js';
|
@@ -54,10 +55,10 @@ export const PrototypeFuncs = function() {
|
|
54
55
|
|
55
56
|
// read from memory
|
56
57
|
[ Opcodes.local_get, iTmp ],
|
57
|
-
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
58
|
+
[ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
58
59
|
|
59
60
|
[ Opcodes.local_get, iTmp ],
|
60
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
|
61
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ]
|
61
62
|
],
|
62
63
|
|
63
64
|
// todo: only for 1 argument
|
@@ -73,12 +74,12 @@ export const PrototypeFuncs = function() {
|
|
73
74
|
// store value
|
74
75
|
[ Opcodes.local_get, iTmp ],
|
75
76
|
...wNewMember,
|
76
|
-
[ Opcodes.store, 0, ValtypeSize.i32 ],
|
77
|
+
[ Opcodes.store, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
77
78
|
|
78
79
|
// store type
|
79
80
|
[ Opcodes.local_get, iTmp ],
|
80
81
|
...wType,
|
81
|
-
[ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
82
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
|
82
83
|
|
83
84
|
// bump array length by 1 and return it
|
84
85
|
...length.setI32([
|
@@ -136,10 +137,10 @@ export const PrototypeFuncs = function() {
|
|
136
137
|
[ Opcodes.local_set, iTmp ],
|
137
138
|
|
138
139
|
[ Opcodes.local_get, iTmp ],
|
139
|
-
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
140
|
+
[ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
140
141
|
|
141
142
|
[ Opcodes.local_get, iTmp ],
|
142
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ]
|
143
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ]
|
143
144
|
])
|
144
145
|
],
|
145
146
|
|
@@ -167,10 +168,10 @@ export const PrototypeFuncs = function() {
|
|
167
168
|
// load first element
|
168
169
|
// todo/perf: unusedValue opt
|
169
170
|
...pointer,
|
170
|
-
[ Opcodes.load, 0, ValtypeSize.i32 ],
|
171
|
+
[ Opcodes.load, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
171
172
|
|
172
173
|
...pointer,
|
173
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
174
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
|
174
175
|
|
175
176
|
// offset page by -1 ind
|
176
177
|
// ...number(pointer + ValtypeSize.i32, Valtype.i32), // dst = base array index + length size
|
@@ -249,12 +250,12 @@ export const PrototypeFuncs = function() {
|
|
249
250
|
// store value
|
250
251
|
[ Opcodes.local_get, iTmp2 ],
|
251
252
|
[ Opcodes.local_get, iTmp ],
|
252
|
-
[ Opcodes.store, 0, ValtypeSize.i32 ],
|
253
|
+
[ Opcodes.store, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
253
254
|
|
254
255
|
// store type
|
255
256
|
[ Opcodes.local_get, iTmp2 ],
|
256
257
|
...wType,
|
257
|
-
[ Opcodes.i32_store8, 0, ValtypeSize.i32 + ValtypeSize[valtype] ],
|
258
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize[valtype]) ],
|
258
259
|
|
259
260
|
// pointer - sizeof value
|
260
261
|
...length.getCachedI32(),
|
@@ -291,8 +292,11 @@ export const PrototypeFuncs = function() {
|
|
291
292
|
const [ newOut, newPointer ] = arrayShell(1, 'i16');
|
292
293
|
|
293
294
|
return [
|
294
|
-
// setup new/out array
|
295
|
+
// setup new/out array
|
295
296
|
...newOut,
|
297
|
+
[ Opcodes.drop ],
|
298
|
+
|
299
|
+
...number(0, Valtype.i32), // base 0 for store later
|
296
300
|
|
297
301
|
...wIndex,
|
298
302
|
Opcodes.i32_to_u,
|
@@ -331,14 +335,13 @@ export const PrototypeFuncs = function() {
|
|
331
335
|
[ Opcodes.i32_add ],
|
332
336
|
|
333
337
|
// load current string ind {arg}
|
334
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
338
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
|
335
339
|
|
336
340
|
// store to new string ind 0
|
337
|
-
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
341
|
+
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
338
342
|
|
339
343
|
// return new string (pointer)
|
340
|
-
...newPointer
|
341
|
-
Opcodes.i32_from_u
|
344
|
+
...number(newPointer)
|
342
345
|
];
|
343
346
|
},
|
344
347
|
|
@@ -347,8 +350,11 @@ export const PrototypeFuncs = function() {
|
|
347
350
|
const [ newOut, newPointer ] = arrayShell(1, 'i16');
|
348
351
|
|
349
352
|
return [
|
350
|
-
// setup new/out array
|
353
|
+
// setup new/out array
|
351
354
|
...newOut,
|
355
|
+
[ Opcodes.drop ],
|
356
|
+
|
357
|
+
...number(0, Valtype.i32), // base 0 for store later
|
352
358
|
|
353
359
|
...wIndex,
|
354
360
|
Opcodes.i32_to,
|
@@ -360,14 +366,13 @@ export const PrototypeFuncs = function() {
|
|
360
366
|
[ Opcodes.i32_add ],
|
361
367
|
|
362
368
|
// load current string ind {arg}
|
363
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
369
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
|
364
370
|
|
365
371
|
// store to new string ind 0
|
366
|
-
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
372
|
+
[ Opcodes.i32_store16, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
367
373
|
|
368
374
|
// return new string (page)
|
369
|
-
...newPointer
|
370
|
-
Opcodes.i32_from_u
|
375
|
+
...number(newPointer)
|
371
376
|
];
|
372
377
|
},
|
373
378
|
|
@@ -406,7 +411,7 @@ export const PrototypeFuncs = function() {
|
|
406
411
|
[ Opcodes.i32_add ],
|
407
412
|
|
408
413
|
// load current string ind {arg}
|
409
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
414
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
|
410
415
|
Opcodes.i32_from_u
|
411
416
|
],
|
412
417
|
|
@@ -428,7 +433,7 @@ export const PrototypeFuncs = function() {
|
|
428
433
|
[ Opcodes.block, Blocktype.void ],
|
429
434
|
|
430
435
|
[ Opcodes.local_get, iTmp ],
|
431
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
436
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32) ],
|
432
437
|
[ Opcodes.local_set, iTmp2 ],
|
433
438
|
|
434
439
|
// if not surrogate, continue
|
@@ -450,7 +455,7 @@ export const PrototypeFuncs = function() {
|
|
450
455
|
|
451
456
|
// if not followed by trailing surrogate, return false
|
452
457
|
[ Opcodes.local_get, iTmp ],
|
453
|
-
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 + ValtypeSize.i16 ],
|
458
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ...unsignedLEB128(ValtypeSize.i32 + ValtypeSize.i16) ],
|
454
459
|
...number(0xFC00, Valtype.i32),
|
455
460
|
[ Opcodes.i32_and ],
|
456
461
|
...number(0xDC00, Valtype.i32),
|
@@ -502,8 +507,11 @@ export const PrototypeFuncs = function() {
|
|
502
507
|
const [ newOut, newPointer ] = arrayShell(1, 'i8');
|
503
508
|
|
504
509
|
return [
|
505
|
-
// setup new/out array
|
510
|
+
// setup new/out array
|
506
511
|
...newOut,
|
512
|
+
[ Opcodes.drop ],
|
513
|
+
|
514
|
+
...number(0, Valtype.i32), // base 0 for store later
|
507
515
|
|
508
516
|
...wIndex,
|
509
517
|
Opcodes.i32_to_u,
|
@@ -540,14 +548,13 @@ export const PrototypeFuncs = function() {
|
|
540
548
|
[ Opcodes.i32_add ],
|
541
549
|
|
542
550
|
// load current string ind {arg}
|
543
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
551
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
544
552
|
|
545
553
|
// store to new string ind 0
|
546
|
-
[ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
|
554
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
547
555
|
|
548
556
|
// return new string (pointer)
|
549
|
-
...newPointer
|
550
|
-
Opcodes.i32_from_u
|
557
|
+
...number(newPointer)
|
551
558
|
];
|
552
559
|
},
|
553
560
|
|
@@ -556,8 +563,11 @@ export const PrototypeFuncs = function() {
|
|
556
563
|
const [ newOut, newPointer ] = arrayShell(1, 'i8');
|
557
564
|
|
558
565
|
return [
|
559
|
-
// setup new/out array
|
566
|
+
// setup new/out array
|
560
567
|
...newOut,
|
568
|
+
[ Opcodes.drop ],
|
569
|
+
|
570
|
+
...number(0, Valtype.i32), // base 0 for store later
|
561
571
|
|
562
572
|
...wIndex,
|
563
573
|
Opcodes.i32_to,
|
@@ -566,14 +576,13 @@ export const PrototypeFuncs = function() {
|
|
566
576
|
[ Opcodes.i32_add ],
|
567
577
|
|
568
578
|
// load current string ind {arg}
|
569
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
579
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
570
580
|
|
571
581
|
// store to new string ind 0
|
572
|
-
[ Opcodes.i32_store8, 0, ValtypeSize.i32 ],
|
582
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
573
583
|
|
574
584
|
// return new string (page)
|
575
|
-
...newPointer
|
576
|
-
Opcodes.i32_from_u
|
585
|
+
...number(newPointer)
|
577
586
|
];
|
578
587
|
},
|
579
588
|
|
@@ -609,7 +618,7 @@ export const PrototypeFuncs = function() {
|
|
609
618
|
[ Opcodes.i32_add ],
|
610
619
|
|
611
620
|
// load current string ind {arg}
|
612
|
-
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ],
|
621
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
613
622
|
Opcodes.i32_from_u
|
614
623
|
],
|
615
624
|
|
package/compiler/wrap.js
CHANGED
@@ -11,13 +11,13 @@ const fs = (typeof process?.version !== 'undefined' ? (await import('node:fs'))
|
|
11
11
|
const bold = x => `\u001b[1m${x}\u001b[0m`;
|
12
12
|
|
13
13
|
export const readByteStr = (memory, ptr) => {
|
14
|
-
const length = (new Int32Array(memory.buffer
|
14
|
+
const length = (new Int32Array(memory.buffer, ptr, 1))[0];
|
15
15
|
return Array.from(new Uint8Array(memory.buffer, ptr + 4, length)).map(x => String.fromCharCode(x)).join('');
|
16
16
|
};
|
17
17
|
|
18
18
|
export const writeByteStr = (memory, ptr, str) => {
|
19
19
|
const length = str.length;
|
20
|
-
(new Int32Array(memory.buffer
|
20
|
+
(new Int32Array(memory.buffer, ptr, 1))[0] = length;
|
21
21
|
|
22
22
|
const arr = new Uint8Array(memory.buffer, ptr + 4, length);
|
23
23
|
for (let i = 0; i < length; i++) {
|
@@ -46,17 +46,17 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
|
46
46
|
}
|
47
47
|
|
48
48
|
case TYPES.string: {
|
49
|
-
const length = (new Int32Array(memory.buffer
|
49
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
50
50
|
return Array.from(new Uint16Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
|
51
51
|
}
|
52
52
|
|
53
53
|
case TYPES.bytestring: {
|
54
|
-
const length = (new Int32Array(memory.buffer
|
54
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
55
55
|
return Array.from(new Uint8Array(memory.buffer, value + 4, length)).map(x => String.fromCharCode(x)).join('');
|
56
56
|
}
|
57
57
|
|
58
58
|
case TYPES.array: {
|
59
|
-
const length = (new Int32Array(memory.buffer
|
59
|
+
const length = (new Int32Array(memory.buffer, value, 1))[0];
|
60
60
|
|
61
61
|
const out = [];
|
62
62
|
for (let i = 0; i < length; i++) {
|
@@ -77,12 +77,12 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
|
77
77
|
}
|
78
78
|
|
79
79
|
case TYPES.date: {
|
80
|
-
const t = (new Float64Array(memory.buffer
|
80
|
+
const t = (new Float64Array(memory.buffer, value, 1))[0];
|
81
81
|
return new Date(t);
|
82
82
|
}
|
83
83
|
|
84
84
|
case TYPES.set: {
|
85
|
-
const size = (new Int32Array(memory.buffer
|
85
|
+
const size = (new Int32Array(memory.buffer, value, 1))[0];
|
86
86
|
|
87
87
|
const out = new Set();
|
88
88
|
for (let i = 0; i < size; i++) {
|
@@ -102,17 +102,17 @@ const porfToJSValue = ({ memory, funcs, pages }, value, type) => {
|
|
102
102
|
return out;
|
103
103
|
}
|
104
104
|
|
105
|
-
|
106
|
-
|
107
|
-
|
105
|
+
case TYPES.symbol: {
|
106
|
+
const descStore = pages.get('bytestring: __Porffor_symbol_descStore/ptr').ind * pageSize;
|
107
|
+
const offset = descStore + 4 + ((value - 1) * 9);
|
108
108
|
|
109
|
-
|
110
|
-
|
109
|
+
const v = (new Float64Array(memory.buffer.slice(offset, offset + 8), 0, 1))[0];
|
110
|
+
const t = (new Uint8Array(memory.buffer, offset + 8, 1))[0];
|
111
111
|
|
112
|
-
|
112
|
+
const desc = porfToJSValue({ memory, funcs, pages }, v, t);
|
113
113
|
|
114
|
-
|
115
|
-
|
114
|
+
return Symbol(desc);
|
115
|
+
}
|
116
116
|
|
117
117
|
default: return value;
|
118
118
|
}
|
package/package.json
CHANGED
@@ -1,10 +1,12 @@
|
|
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.16.0-
|
4
|
+
"version": "0.16.0-79cd8c0c8",
|
5
5
|
"author": "CanadaHonk",
|
6
6
|
"license": "MIT",
|
7
|
-
"scripts": {
|
7
|
+
"scripts": {
|
8
|
+
"precompile": "node ./compiler/precompile.js"
|
9
|
+
},
|
8
10
|
"dependencies": {
|
9
11
|
"acorn": "^8.11.3",
|
10
12
|
"node-repl-polyfill": "^0.1.1"
|
package/runner/index.js
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
+
|
3
|
+
import compile from '../compiler/wrap.js';
|
2
4
|
import fs from 'node:fs';
|
3
5
|
|
4
6
|
const start = performance.now();
|
@@ -57,15 +59,10 @@ if (process.argv.includes('--help')) {
|
|
57
59
|
}
|
58
60
|
|
59
61
|
let file = process.argv.slice(2).find(x => x[0] !== '-');
|
60
|
-
if (['
|
62
|
+
if (['run', 'wasm', 'native', 'c', 'profile', 'debug', 'debug-wasm'].includes(file)) {
|
61
63
|
// remove this arg
|
62
64
|
process.argv.splice(process.argv.indexOf(file), 1);
|
63
65
|
|
64
|
-
if (file === 'precompile') {
|
65
|
-
await import('../compiler/precompile.js');
|
66
|
-
await new Promise(() => {}); // do nothing for the rest of this file
|
67
|
-
}
|
68
|
-
|
69
66
|
if (file === 'profile') {
|
70
67
|
await import('./profile.js');
|
71
68
|
await new Promise(() => {}); // do nothing for the rest of this file
|
@@ -108,8 +105,6 @@ if (!file) {
|
|
108
105
|
|
109
106
|
const source = fs.readFileSync(file, 'utf8');
|
110
107
|
|
111
|
-
const compile = (await import('../compiler/wrap.js')).default;
|
112
|
-
|
113
108
|
let cache = '';
|
114
109
|
const print = str => {
|
115
110
|
/* cache += str;
|