porffor 0.2.0-c7b7423 → 0.2.0-cb647c8
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 +68 -0
- package/compiler/builtins/porffor.d.ts +2 -0
- package/compiler/builtins.js +177 -60
- package/compiler/codeGen.js +461 -227
- package/compiler/decompile.js +3 -3
- package/compiler/encoding.js +2 -116
- package/compiler/index.js +15 -9
- package/compiler/opt.js +24 -2
- package/compiler/parse.js +11 -12
- package/compiler/prototype.js +171 -16
- package/compiler/sections.js +1 -1
- package/compiler/wasmSpec.js +6 -2
- package/compiler/wrap.js +103 -9
- package/demo.js +3 -0
- package/demo.ts +1 -0
- package/filesize.cmd +2 -0
- package/package.json +1 -1
- package/porf +2 -0
- package/runner/index.js +20 -3
- package/runner/repl.js +2 -2
- package/tmp.c +1231 -52
- package/compiler/builtins/base64.js +0 -92
package/compiler/builtins.js
CHANGED
@@ -1,4 +1,4 @@
|
|
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
3
|
|
4
4
|
export const importedFuncs = [
|
@@ -29,6 +29,21 @@ for (let i = 0; i < importedFuncs.length; i++) {
|
|
29
29
|
|
30
30
|
const char = c => number(c.charCodeAt(0));
|
31
31
|
|
32
|
+
const printStaticStr = str => {
|
33
|
+
const out = [];
|
34
|
+
|
35
|
+
for (let i = 0; i < str.length; i++) {
|
36
|
+
out.push(
|
37
|
+
// ...number(str.charCodeAt(i)),
|
38
|
+
...number(str.charCodeAt(i), Valtype.i32),
|
39
|
+
Opcodes.i32_from_u,
|
40
|
+
[ Opcodes.call, importedFuncs.printChar ]
|
41
|
+
);
|
42
|
+
}
|
43
|
+
|
44
|
+
return out;
|
45
|
+
};
|
46
|
+
|
32
47
|
// todo: somehow diff between these (undefined != null) while remaining falsey in wasm as a number value
|
33
48
|
export const UNDEFINED = 0;
|
34
49
|
export const NULL = 0;
|
@@ -155,25 +170,6 @@ export const BuiltinFuncs = function() {
|
|
155
170
|
]
|
156
171
|
};
|
157
172
|
|
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
173
|
// just return given (default 0) for (new) Object() as we somewhat supports object just not constructor
|
178
174
|
this.Object = {
|
179
175
|
params: [ valtypeBinary ],
|
@@ -187,12 +183,125 @@ export const BuiltinFuncs = function() {
|
|
187
183
|
|
188
184
|
|
189
185
|
this.__console_log = {
|
190
|
-
params: [ valtypeBinary ],
|
191
|
-
|
186
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
187
|
+
typedParams: true,
|
188
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
192
189
|
returns: [],
|
193
|
-
wasm: [
|
194
|
-
[ Opcodes.local_get,
|
195
|
-
|
190
|
+
wasm: (scope, { TYPES, typeSwitch }) => [
|
191
|
+
...typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], {
|
192
|
+
[TYPES.number]: [
|
193
|
+
[ Opcodes.local_get, 0 ],
|
194
|
+
[ Opcodes.call, importedFuncs.print ],
|
195
|
+
],
|
196
|
+
[TYPES.boolean]: [
|
197
|
+
[ Opcodes.local_get, 0 ],
|
198
|
+
Opcodes.i32_to_u,
|
199
|
+
[ Opcodes.if, Blocktype.void ],
|
200
|
+
...printStaticStr('true'),
|
201
|
+
[ Opcodes.else ],
|
202
|
+
...printStaticStr('false'),
|
203
|
+
[ Opcodes.end ]
|
204
|
+
],
|
205
|
+
[TYPES.string]: [
|
206
|
+
// simply print a string :))
|
207
|
+
// cache input pointer as i32
|
208
|
+
[ Opcodes.local_get, 0 ],
|
209
|
+
Opcodes.i32_to_u,
|
210
|
+
[ Opcodes.local_tee, 2 ],
|
211
|
+
|
212
|
+
// make end pointer
|
213
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
214
|
+
...number(ValtypeSize.i16, Valtype.i32),
|
215
|
+
[ Opcodes.i32_mul ],
|
216
|
+
|
217
|
+
[ Opcodes.local_get, 2 ],
|
218
|
+
[ Opcodes.i32_add ],
|
219
|
+
[ Opcodes.local_set, 3 ],
|
220
|
+
|
221
|
+
[ Opcodes.loop, Blocktype.void ],
|
222
|
+
|
223
|
+
// print current char
|
224
|
+
[ Opcodes.local_get, 2 ],
|
225
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
226
|
+
Opcodes.i32_from_u,
|
227
|
+
[ Opcodes.call, importedFuncs.printChar ],
|
228
|
+
|
229
|
+
// increment pointer by sizeof i16
|
230
|
+
[ Opcodes.local_get, 2 ],
|
231
|
+
...number(ValtypeSize.i16, Valtype.i32),
|
232
|
+
[ Opcodes.i32_add ],
|
233
|
+
[ Opcodes.local_tee, 2 ],
|
234
|
+
|
235
|
+
// if pointer != end pointer, loop
|
236
|
+
[ Opcodes.local_get, 3 ],
|
237
|
+
[ Opcodes.i32_ne ],
|
238
|
+
[ Opcodes.br_if, 0 ],
|
239
|
+
|
240
|
+
[ Opcodes.end ]
|
241
|
+
],
|
242
|
+
[TYPES._array]: [
|
243
|
+
...printStaticStr('[ '),
|
244
|
+
|
245
|
+
// cache input pointer as i32
|
246
|
+
[ Opcodes.local_get, 0 ],
|
247
|
+
Opcodes.i32_to_u,
|
248
|
+
[ Opcodes.local_tee, 2 ],
|
249
|
+
|
250
|
+
// make end pointer
|
251
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
252
|
+
...number(ValtypeSize[valtype], Valtype.i32),
|
253
|
+
[ Opcodes.i32_mul ],
|
254
|
+
|
255
|
+
[ Opcodes.local_get, 2 ],
|
256
|
+
[ Opcodes.i32_add ],
|
257
|
+
[ Opcodes.local_set, 3 ],
|
258
|
+
|
259
|
+
[ Opcodes.loop, Blocktype.void ],
|
260
|
+
|
261
|
+
// print current char
|
262
|
+
[ Opcodes.local_get, 2 ],
|
263
|
+
[ Opcodes.load, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
264
|
+
[ Opcodes.call, importedFuncs.print ],
|
265
|
+
|
266
|
+
// increment pointer by sizeof valtype
|
267
|
+
[ Opcodes.local_get, 2 ],
|
268
|
+
...number(ValtypeSize[valtype], Valtype.i32),
|
269
|
+
[ Opcodes.i32_add ],
|
270
|
+
[ Opcodes.local_tee, 2 ],
|
271
|
+
|
272
|
+
// if pointer != end pointer, print separator and loop
|
273
|
+
[ Opcodes.local_get, 3 ],
|
274
|
+
[ Opcodes.i32_ne ],
|
275
|
+
[ Opcodes.if, Blocktype.void ],
|
276
|
+
...printStaticStr(', '),
|
277
|
+
[ Opcodes.br, 1 ],
|
278
|
+
[ Opcodes.end ],
|
279
|
+
|
280
|
+
[ Opcodes.end ],
|
281
|
+
|
282
|
+
...printStaticStr(' ]'),
|
283
|
+
],
|
284
|
+
[TYPES.undefined]: [
|
285
|
+
...printStaticStr('undefined')
|
286
|
+
],
|
287
|
+
[TYPES.function]: [
|
288
|
+
...printStaticStr('function () {}')
|
289
|
+
],
|
290
|
+
[TYPES.object]: [
|
291
|
+
[ Opcodes.local_get, 0 ],
|
292
|
+
Opcodes.i32_to_u,
|
293
|
+
[ Opcodes.if, Blocktype.void ],
|
294
|
+
...printStaticStr('{}'),
|
295
|
+
[ Opcodes.else ],
|
296
|
+
...printStaticStr('null'),
|
297
|
+
[ Opcodes.end ]
|
298
|
+
],
|
299
|
+
default: [
|
300
|
+
[ Opcodes.local_get, 0 ],
|
301
|
+
[ Opcodes.call, importedFuncs.print ],
|
302
|
+
]
|
303
|
+
}, Blocktype.void),
|
304
|
+
|
196
305
|
...char('\n'),
|
197
306
|
[ Opcodes.call, importedFuncs.printChar ]
|
198
307
|
]
|
@@ -571,6 +680,49 @@ export const BuiltinFuncs = function() {
|
|
571
680
|
};
|
572
681
|
|
573
682
|
|
683
|
+
this.__Porffor_type = {
|
684
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
685
|
+
typedParams: true,
|
686
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
687
|
+
returns: [ valtypeBinary ],
|
688
|
+
returnType: process.argv.includes('-bytestring') ? '_bytestring' : 'string',
|
689
|
+
wasm: (scope, { TYPE_NAMES, typeSwitch, makeString }) => {
|
690
|
+
const bc = {};
|
691
|
+
for (const x in TYPE_NAMES) {
|
692
|
+
bc[x] = makeString(scope, TYPE_NAMES[x], false, '#Porffor_type_result');
|
693
|
+
}
|
694
|
+
|
695
|
+
return typeSwitch(scope, [ [ Opcodes.local_get, 1 ] ], bc);
|
696
|
+
}
|
697
|
+
};
|
698
|
+
|
699
|
+
const localIsOneOf = (getter, arr, valtype = valtypeBinary) => {
|
700
|
+
const out = [];
|
701
|
+
|
702
|
+
for (let i = 0; i < arr.length; i++) {
|
703
|
+
out.push(...getter, ...number(arr[i], valtype), valtype === Valtype.f64 ? [ Opcodes.f64_eq ] : [ Opcodes.i32_eq ]);
|
704
|
+
if (i !== 0) out.push([ Opcodes.i32_or ]);
|
705
|
+
}
|
706
|
+
|
707
|
+
return out;
|
708
|
+
};
|
709
|
+
|
710
|
+
this.__Porffor_pointer = {
|
711
|
+
params: [ valtypeBinary, Valtype.i32 ],
|
712
|
+
typedParams: true,
|
713
|
+
locals: [ Valtype.i32, Valtype.i32 ],
|
714
|
+
returns: [ valtypeBinary ],
|
715
|
+
wasm: (scope, { TYPES }) => [
|
716
|
+
...localIsOneOf([ [ Opcodes.local_get, 1 ] ], [ TYPES.string, TYPES._array, TYPES._bytestring ], Valtype.i32),
|
717
|
+
[ Opcodes.if, valtypeBinary ],
|
718
|
+
[ Opcodes.local_get, 0 ],
|
719
|
+
[ Opcodes.else ],
|
720
|
+
...number(NaN),
|
721
|
+
[ Opcodes.end ]
|
722
|
+
]
|
723
|
+
};
|
724
|
+
|
725
|
+
|
574
726
|
this.__SIMD_i32x4_load = {
|
575
727
|
params: [ Valtype.i32 ],
|
576
728
|
locals: [],
|
@@ -738,39 +890,4 @@ export const BuiltinFuncs = function() {
|
|
738
890
|
[ ...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
891
|
]
|
740
892
|
};
|
741
|
-
};
|
742
|
-
|
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
|
-
|
768
|
-
output += keyStr.charAt(enc1);
|
769
|
-
output += keyStr.charAt(enc2);
|
770
|
-
output += keyStr.charAt(enc3);
|
771
|
-
output += keyStr.charAt(enc4);
|
772
|
-
}
|
773
|
-
|
774
|
-
return output;
|
775
|
-
};`
|
776
893
|
};
|