porffor 0.16.0-ec15a329e → 0.16.0-f9e625159
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/README.md +5 -17
- package/compiler/2c.js +75 -15
- package/compiler/allocators.js +128 -0
- package/compiler/assemble.js +12 -5
- package/compiler/builtins/array.ts +72 -5
- package/compiler/builtins/date.ts +3 -30
- package/compiler/builtins/number.ts +10 -21
- package/compiler/builtins/porffor.d.ts +7 -0
- package/compiler/builtins/set.ts +2 -5
- package/compiler/builtins/z_ecma262.ts +62 -0
- package/compiler/builtins.js +23 -3
- package/compiler/codegen.js +293 -366
- package/compiler/cyclone.js +535 -0
- package/compiler/decompile.js +3 -1
- package/compiler/generated_builtins.js +113 -75
- package/compiler/havoc.js +93 -0
- package/compiler/index.js +104 -7
- package/compiler/opt.js +10 -44
- package/compiler/parse.js +1 -7
- package/compiler/pgo.js +212 -0
- package/compiler/precompile.js +12 -7
- package/compiler/prefs.js +8 -4
- package/compiler/prototype.js +34 -43
- package/compiler/wasmSpec.js +2 -2
- package/compiler/wrap.js +55 -13
- package/package.json +3 -5
- package/runner/index.js +26 -11
- /package/runner/{profiler.js → profile.js} +0 -0
@@ -28,6 +28,13 @@ type PorfforGlobal = {
|
|
28
28
|
write(_this: any, index: number, value: any): boolean;
|
29
29
|
}
|
30
30
|
|
31
|
+
bytestring: {
|
32
|
+
// defined in date.ts
|
33
|
+
appendStr(str: bytestring, appendage: bytestring): i32;
|
34
|
+
appendChar(str: bytestring, char: i32): i32;
|
35
|
+
appendPadNum(str: bytestring, num: number, len: number): i32;
|
36
|
+
}
|
37
|
+
|
31
38
|
print(x: any): i32;
|
32
39
|
|
33
40
|
randomByte(): i32;
|
package/compiler/builtins/set.ts
CHANGED
@@ -2,12 +2,9 @@ import type {} from './porffor.d.ts';
|
|
2
2
|
|
3
3
|
// dark wasm magic for dealing with memory, sorry.
|
4
4
|
export const __Porffor_allocate = (): number => {
|
5
|
-
Porffor.wasm`
|
6
|
-
memory.grow 0
|
7
|
-
drop
|
8
|
-
memory.size 0
|
5
|
+
Porffor.wasm`
|
9
6
|
i32.const 1
|
10
|
-
|
7
|
+
memory.grow 0
|
11
8
|
i32.const 65536
|
12
9
|
i32.mul
|
13
10
|
i32.from_u
|
@@ -0,0 +1,62 @@
|
|
1
|
+
// general widely used ecma262/spec functions
|
2
|
+
|
3
|
+
// 7.1.5 ToIntegerOrInfinity (argument)
|
4
|
+
// https://tc39.es/ecma262/#sec-tointegerorinfinity
|
5
|
+
export const __ecma262_ToIntegerOrInfinity = (argument: unknown): number => {
|
6
|
+
// 1. Let number be ? ToNumber(argument).
|
7
|
+
let number: number = Number(argument);
|
8
|
+
|
9
|
+
// 2. If number is one of NaN, +0𝔽, or -0𝔽, return 0.
|
10
|
+
if (Number.isNaN(number)) return 0;
|
11
|
+
|
12
|
+
// 3. If number is +∞𝔽, return +∞.
|
13
|
+
// 4. If number is -∞𝔽, return -∞.
|
14
|
+
if (!Number.isFinite(number)) return number;
|
15
|
+
|
16
|
+
// 5. Return truncate(ℝ(number)).
|
17
|
+
number = Math.trunc(number);
|
18
|
+
|
19
|
+
// return 0 for -0
|
20
|
+
if (number == 0) return 0;
|
21
|
+
return number;
|
22
|
+
};
|
23
|
+
|
24
|
+
// todo: support non-bytestring properly
|
25
|
+
// 7.1.17 ToString (argument)
|
26
|
+
// https://tc39.es/ecma262/#sec-tostring
|
27
|
+
export const __ecma262_ToString = (argument: unknown): bytestring => {
|
28
|
+
let out: bytestring = '';
|
29
|
+
const type: i32 = Porffor.rawType(argument);
|
30
|
+
|
31
|
+
// 1. If argument is a String, return argument.
|
32
|
+
if (Porffor.fastOr(
|
33
|
+
type == Porffor.TYPES.string,
|
34
|
+
type == Porffor.TYPES.bytestring)) return argument;
|
35
|
+
|
36
|
+
// 2. If argument is a Symbol, throw a TypeError exception.
|
37
|
+
if (type == Porffor.TYPES.symbol) throw new TypeError('Cannot convert a Symbol value to a string');
|
38
|
+
|
39
|
+
// 3. If argument is undefined, return "undefined".
|
40
|
+
if (type == Porffor.TYPES.undefined) return out = 'undefined';
|
41
|
+
|
42
|
+
// 4. If argument is null, return "null".
|
43
|
+
if (Porffor.fastAnd(
|
44
|
+
type == Porffor.TYPES.object,
|
45
|
+
argument == 0)) return out = 'null';
|
46
|
+
|
47
|
+
if (type == Porffor.TYPES.boolean) {
|
48
|
+
// 5. If argument is true, return "true".
|
49
|
+
if (argument == true) return out = 'true';
|
50
|
+
|
51
|
+
// 6. If argument is false, return "false".
|
52
|
+
return out = 'false';
|
53
|
+
}
|
54
|
+
|
55
|
+
// 7. If argument is a Number, return Number::toString(argument, 10).
|
56
|
+
// 8. If argument is a BigInt, return BigInt::toString(argument, 10).
|
57
|
+
// 9. Assert: argument is an Object.
|
58
|
+
// 10. Let primValue be ? ToPrimitive(argument, string).
|
59
|
+
// 11. Assert: primValue is not an Object.
|
60
|
+
// 12. Return ? ToString(primValue).
|
61
|
+
return argument.toString();
|
62
|
+
};
|
package/compiler/builtins.js
CHANGED
@@ -32,13 +32,13 @@ export const importedFuncs = [
|
|
32
32
|
{
|
33
33
|
name: 'profile1',
|
34
34
|
import: 'y',
|
35
|
-
params:
|
35
|
+
params: [ Valtype.i32 ],
|
36
36
|
returns: 0
|
37
37
|
},
|
38
38
|
{
|
39
39
|
name: 'profile2',
|
40
40
|
import: 'z',
|
41
|
-
params:
|
41
|
+
params: [ Valtype.i32 ],
|
42
42
|
returns: 0
|
43
43
|
},
|
44
44
|
{
|
@@ -167,6 +167,7 @@ export const BuiltinFuncs = function() {
|
|
167
167
|
params: [ valtypeBinary, valtypeBinary ],
|
168
168
|
locals: [],
|
169
169
|
returns: [ valtypeBinary ],
|
170
|
+
returnType: TYPES.number,
|
170
171
|
wasm: [ // x - truncf(x / y) * y
|
171
172
|
[ Opcodes.local_get, 0 ], // x
|
172
173
|
|
@@ -189,6 +190,7 @@ export const BuiltinFuncs = function() {
|
|
189
190
|
params: [ valtypeBinary, valtypeBinary ],
|
190
191
|
locals: [],
|
191
192
|
returns: [ valtypeBinary ],
|
193
|
+
returnType: TYPES.number,
|
192
194
|
wasm: [
|
193
195
|
[ Opcodes.local_get, 0 ],
|
194
196
|
Opcodes.i32_to,
|
@@ -208,6 +210,7 @@ export const BuiltinFuncs = function() {
|
|
208
210
|
params: [ valtypeBinary ],
|
209
211
|
locals: [],
|
210
212
|
returns: [ valtypeBinary ],
|
213
|
+
returnType: TYPES.number,
|
211
214
|
wasm: [
|
212
215
|
[ Opcodes.local_get, 0 ]
|
213
216
|
],
|
@@ -469,6 +472,7 @@ export const BuiltinFuncs = function() {
|
|
469
472
|
params: [ valtypeBinary ],
|
470
473
|
locals: [],
|
471
474
|
returns: [ valtypeBinary ],
|
475
|
+
returnType: TYPES.number,
|
472
476
|
wasm: [
|
473
477
|
[ Opcodes.local_get, 0 ],
|
474
478
|
[ Opcodes.f64_sqrt ]
|
@@ -480,6 +484,7 @@ export const BuiltinFuncs = function() {
|
|
480
484
|
params: [ valtypeBinary ],
|
481
485
|
locals: [],
|
482
486
|
returns: [ valtypeBinary ],
|
487
|
+
returnType: TYPES.number,
|
483
488
|
wasm: [
|
484
489
|
[ Opcodes.local_get, 0 ],
|
485
490
|
[ Opcodes.f64_abs ]
|
@@ -491,6 +496,7 @@ export const BuiltinFuncs = function() {
|
|
491
496
|
params: [ valtypeBinary ],
|
492
497
|
locals: [],
|
493
498
|
returns: [ valtypeBinary ],
|
499
|
+
returnType: TYPES.number,
|
494
500
|
wasm: [
|
495
501
|
...number(1),
|
496
502
|
[ Opcodes.local_get, 0 ],
|
@@ -503,6 +509,7 @@ export const BuiltinFuncs = function() {
|
|
503
509
|
params: [ valtypeBinary ],
|
504
510
|
locals: [],
|
505
511
|
returns: [ valtypeBinary ],
|
512
|
+
returnType: TYPES.number,
|
506
513
|
wasm: [
|
507
514
|
[ Opcodes.local_get, 0 ],
|
508
515
|
[ Opcodes.f64_floor ]
|
@@ -514,6 +521,7 @@ export const BuiltinFuncs = function() {
|
|
514
521
|
params: [ valtypeBinary ],
|
515
522
|
locals: [],
|
516
523
|
returns: [ valtypeBinary ],
|
524
|
+
returnType: TYPES.number,
|
517
525
|
wasm: [
|
518
526
|
[ Opcodes.local_get, 0 ],
|
519
527
|
[ Opcodes.f64_ceil ]
|
@@ -525,6 +533,7 @@ export const BuiltinFuncs = function() {
|
|
525
533
|
params: [ valtypeBinary ],
|
526
534
|
locals: [],
|
527
535
|
returns: [ valtypeBinary ],
|
536
|
+
returnType: TYPES.number,
|
528
537
|
wasm: [
|
529
538
|
[ Opcodes.local_get, 0 ],
|
530
539
|
[ Opcodes.f64_nearest ]
|
@@ -536,6 +545,7 @@ export const BuiltinFuncs = function() {
|
|
536
545
|
params: [ valtypeBinary ],
|
537
546
|
locals: [],
|
538
547
|
returns: [ valtypeBinary ],
|
548
|
+
returnType: TYPES.number,
|
539
549
|
wasm: [
|
540
550
|
[ Opcodes.local_get, 0 ],
|
541
551
|
[ Opcodes.f64_trunc ]
|
@@ -547,6 +557,7 @@ export const BuiltinFuncs = function() {
|
|
547
557
|
params: [ valtypeBinary ],
|
548
558
|
locals: [],
|
549
559
|
returns: [ valtypeBinary ],
|
560
|
+
returnType: TYPES.number,
|
550
561
|
wasm: [
|
551
562
|
[ Opcodes.local_get, 0 ],
|
552
563
|
Opcodes.i32_trunc_sat_f64_u,
|
@@ -560,6 +571,7 @@ export const BuiltinFuncs = function() {
|
|
560
571
|
params: [ valtypeBinary ],
|
561
572
|
locals: [],
|
562
573
|
returns: [ valtypeBinary ],
|
574
|
+
returnType: TYPES.number,
|
563
575
|
wasm: [
|
564
576
|
[ Opcodes.local_get, 0 ],
|
565
577
|
[ Opcodes.f32_demote_f64 ],
|
@@ -573,6 +585,7 @@ export const BuiltinFuncs = function() {
|
|
573
585
|
params: [ valtypeBinary, valtypeBinary ],
|
574
586
|
locals: [],
|
575
587
|
returns: [ valtypeBinary ],
|
588
|
+
returnType: TYPES.number,
|
576
589
|
wasm: [
|
577
590
|
[ Opcodes.local_get, 0 ],
|
578
591
|
Opcodes.i32_trunc_sat_f64_s,
|
@@ -887,6 +900,7 @@ export const BuiltinFuncs = function() {
|
|
887
900
|
globalNames: [ 'state0', 'state1' ],
|
888
901
|
globalInits: [ prngSeed0, prngSeed1 ],
|
889
902
|
returns: [ Valtype.f64 ],
|
903
|
+
returnType: TYPES.number,
|
890
904
|
wasm: [
|
891
905
|
...prng.wasm,
|
892
906
|
|
@@ -938,6 +952,7 @@ export const BuiltinFuncs = function() {
|
|
938
952
|
globalNames: [ 'state0', 'state1' ],
|
939
953
|
globalInits: [ prngSeed0, prngSeed1 ],
|
940
954
|
returns: [ Valtype.i32 ],
|
955
|
+
returnType: TYPES.number,
|
941
956
|
wasm: [
|
942
957
|
...prng.wasm,
|
943
958
|
|
@@ -959,6 +974,7 @@ export const BuiltinFuncs = function() {
|
|
959
974
|
params: [ valtypeBinary ],
|
960
975
|
locals: [],
|
961
976
|
returns: [ valtypeBinary ],
|
977
|
+
returnType: TYPES.number,
|
962
978
|
wasm: [
|
963
979
|
[ Opcodes.local_get, 0 ],
|
964
980
|
...number(Math.PI / 180),
|
@@ -971,6 +987,7 @@ export const BuiltinFuncs = function() {
|
|
971
987
|
params: [ valtypeBinary ],
|
972
988
|
locals: [],
|
973
989
|
returns: [ valtypeBinary ],
|
990
|
+
returnType: TYPES.number,
|
974
991
|
wasm: [
|
975
992
|
[ Opcodes.local_get, 0 ],
|
976
993
|
...number(180 / Math.PI),
|
@@ -984,6 +1001,7 @@ export const BuiltinFuncs = function() {
|
|
984
1001
|
locals: [],
|
985
1002
|
localNames: [ 'x', 'lower', 'upper' ],
|
986
1003
|
returns: [ valtypeBinary ],
|
1004
|
+
returnType: TYPES.number,
|
987
1005
|
wasm: [
|
988
1006
|
[ Opcodes.local_get, 0 ],
|
989
1007
|
[ Opcodes.local_get, 1 ],
|
@@ -999,9 +1017,9 @@ export const BuiltinFuncs = function() {
|
|
999
1017
|
locals: [],
|
1000
1018
|
localNames: [ 'x', 'inLow', 'inHigh', 'outLow', 'outHigh' ],
|
1001
1019
|
returns: [ valtypeBinary ],
|
1020
|
+
returnType: TYPES.number,
|
1002
1021
|
wasm: [
|
1003
1022
|
// (x − inLow) * (outHigh − outLow) / (inHigh - inLow) + outLow
|
1004
|
-
|
1005
1023
|
[ Opcodes.local_get, 0 ],
|
1006
1024
|
[ Opcodes.local_get, 1 ],
|
1007
1025
|
[ Opcodes.f64_sub ],
|
@@ -1043,6 +1061,7 @@ export const BuiltinFuncs = function() {
|
|
1043
1061
|
params: [],
|
1044
1062
|
locals: [],
|
1045
1063
|
returns: [ valtypeBinary ],
|
1064
|
+
returnType: TYPES.number,
|
1046
1065
|
wasm: [
|
1047
1066
|
[ Opcodes.call, importedFuncs.time ]
|
1048
1067
|
]
|
@@ -1070,6 +1089,7 @@ export const BuiltinFuncs = function() {
|
|
1070
1089
|
typedParams: true,
|
1071
1090
|
locals: [],
|
1072
1091
|
returns: [ valtypeBinary ],
|
1092
|
+
returnType: TYPES.number,
|
1073
1093
|
wasm: [
|
1074
1094
|
[ Opcodes.local_get, 1 ],
|
1075
1095
|
Opcodes.i32_from_u
|