porffor 0.2.0-dfa0583 → 0.2.0-e0256ae
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 +183 -0
- package/LICENSE +20 -20
- package/README.md +154 -89
- package/asur/README.md +2 -0
- package/asur/index.js +1262 -0
- package/byg/index.js +237 -0
- package/compiler/2c.js +317 -72
- package/compiler/{sections.js → assemble.js} +63 -15
- package/compiler/builtins/annexb_string.js +72 -0
- package/compiler/builtins/annexb_string.ts +19 -0
- package/compiler/builtins/array.ts +145 -0
- package/compiler/builtins/base64.ts +151 -0
- package/compiler/builtins/crypto.ts +120 -0
- package/compiler/builtins/date.ts +1856 -0
- package/compiler/builtins/escape.ts +141 -0
- package/compiler/builtins/int.ts +147 -0
- package/compiler/builtins/number.ts +527 -0
- package/compiler/builtins/porffor.d.ts +42 -0
- package/compiler/builtins/string.ts +1055 -0
- package/compiler/builtins/tostring.ts +45 -0
- package/compiler/builtins.js +470 -269
- package/compiler/{codeGen.js → codegen.js} +1005 -380
- package/compiler/decompile.js +0 -1
- package/compiler/embedding.js +22 -22
- package/compiler/encoding.js +108 -10
- package/compiler/generated_builtins.js +1445 -0
- package/compiler/index.js +36 -34
- package/compiler/log.js +6 -3
- package/compiler/opt.js +50 -36
- package/compiler/parse.js +33 -23
- package/compiler/precompile.js +128 -0
- package/compiler/prefs.js +27 -0
- package/compiler/prototype.js +13 -28
- package/compiler/types.js +37 -0
- package/compiler/wasmSpec.js +28 -8
- package/compiler/wrap.js +51 -46
- package/package.json +9 -5
- package/porf +4 -0
- package/rhemyn/compile.js +46 -27
- package/rhemyn/parse.js +322 -320
- package/rhemyn/test/parse.js +58 -58
- package/runner/compare.js +34 -34
- package/runner/debug.js +122 -0
- package/runner/index.js +91 -11
- package/runner/profiler.js +102 -0
- package/runner/repl.js +42 -9
- package/runner/sizes.js +37 -37
- package/compiler/builtins/base64.js +0 -92
- package/runner/info.js +0 -89
- package/runner/profile.js +0 -46
- package/runner/results.json +0 -1
- package/runner/transform.js +0 -15
- package/util/enum.js +0 -20
@@ -7,6 +7,8 @@ import { number, i32x4, enforceOneByte, enforceTwoBytes, enforceFourBytes, enfor
|
|
7
7
|
import { log } from "./log.js";
|
8
8
|
import parse from "./parse.js";
|
9
9
|
import * as Rhemyn from "../rhemyn/compile.js";
|
10
|
+
import Prefs from './prefs.js';
|
11
|
+
import { TYPES, TYPE_NAMES } from './types.js';
|
10
12
|
|
11
13
|
let globals = {};
|
12
14
|
let globalInd = 0;
|
@@ -23,35 +25,37 @@ const debug = str => {
|
|
23
25
|
const logChar = n => {
|
24
26
|
code.push(...number(n));
|
25
27
|
|
26
|
-
code.push(Opcodes.call);
|
27
|
-
code.push(...unsignedLEB128(0));
|
28
|
+
code.push([ Opcodes.call, 0 ]);
|
28
29
|
};
|
29
30
|
|
30
31
|
for (let i = 0; i < str.length; i++) {
|
31
32
|
logChar(str.charCodeAt(i));
|
32
33
|
}
|
33
34
|
|
34
|
-
logChar(
|
35
|
+
logChar(10); // new line
|
35
36
|
|
36
37
|
return code;
|
37
38
|
};
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
this.name = 'TodoError';
|
44
|
-
}
|
40
|
+
class TodoError extends Error {
|
41
|
+
constructor(message) {
|
42
|
+
super(message);
|
43
|
+
this.name = 'TodoError';
|
45
44
|
}
|
45
|
+
}
|
46
|
+
const todo = (scope, msg, expectsValue = undefined) => {
|
47
|
+
switch (Prefs.todoTime ?? 'runtime') {
|
48
|
+
case 'compile':
|
49
|
+
throw new TodoError(msg);
|
46
50
|
|
47
|
-
|
48
|
-
|
49
|
-
const code = [];
|
50
|
-
|
51
|
-
code.push(...debug(`todo! ` + msg));
|
52
|
-
code.push(Opcodes.unreachable);
|
51
|
+
case 'runtime':
|
52
|
+
return internalThrow(scope, 'TodoError', msg, expectsValue);
|
53
53
|
|
54
|
-
|
54
|
+
// return [
|
55
|
+
// ...debug(`todo! ${msg}`),
|
56
|
+
// [ Opcodes.unreachable ]
|
57
|
+
// ];
|
58
|
+
}
|
55
59
|
};
|
56
60
|
|
57
61
|
const isFuncType = type => type === 'FunctionDeclaration' || type === 'FunctionExpression' || type === 'ArrowFunctionExpression';
|
@@ -104,7 +108,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
104
108
|
return generateUnary(scope, decl);
|
105
109
|
|
106
110
|
case 'UpdateExpression':
|
107
|
-
return generateUpdate(scope, decl);
|
111
|
+
return generateUpdate(scope, decl, global, name, valueUnused);
|
108
112
|
|
109
113
|
case 'IfStatement':
|
110
114
|
return generateIf(scope, decl);
|
@@ -115,6 +119,9 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
115
119
|
case 'WhileStatement':
|
116
120
|
return generateWhile(scope, decl);
|
117
121
|
|
122
|
+
case 'DoWhileStatement':
|
123
|
+
return generateDoWhile(scope, decl);
|
124
|
+
|
118
125
|
case 'ForOfStatement':
|
119
126
|
return generateForOf(scope, decl);
|
120
127
|
|
@@ -124,6 +131,9 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
124
131
|
case 'ContinueStatement':
|
125
132
|
return generateContinue(scope, decl);
|
126
133
|
|
134
|
+
case 'LabeledStatement':
|
135
|
+
return generateLabel(scope, decl);
|
136
|
+
|
127
137
|
case 'EmptyStatement':
|
128
138
|
return generateEmpty(scope, decl);
|
129
139
|
|
@@ -137,7 +147,7 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
137
147
|
return generateTry(scope, decl);
|
138
148
|
|
139
149
|
case 'DebuggerStatement':
|
140
|
-
// todo:
|
150
|
+
// todo: hook into terminal debugger
|
141
151
|
return [];
|
142
152
|
|
143
153
|
case 'ArrayExpression':
|
@@ -151,16 +161,22 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
151
161
|
const funcsBefore = funcs.length;
|
152
162
|
generate(scope, decl.declaration);
|
153
163
|
|
154
|
-
if (funcsBefore
|
164
|
+
if (funcsBefore !== funcs.length) {
|
165
|
+
// new func added
|
166
|
+
const newFunc = funcs[funcs.length - 1];
|
167
|
+
newFunc.export = true;
|
168
|
+
}
|
169
|
+
|
170
|
+
// if (funcsBefore === funcs.length) throw new Error('no new func added in export');
|
155
171
|
|
156
|
-
const newFunc = funcs[funcs.length - 1];
|
157
|
-
newFunc.export = true;
|
172
|
+
// const newFunc = funcs[funcs.length - 1];
|
173
|
+
// newFunc.export = true;
|
158
174
|
|
159
175
|
return [];
|
160
176
|
|
161
177
|
case 'TaggedTemplateExpression': {
|
162
178
|
const funcs = {
|
163
|
-
|
179
|
+
__Porffor_wasm: str => {
|
164
180
|
let out = [];
|
165
181
|
|
166
182
|
for (const line of str.split('\n')) {
|
@@ -168,8 +184,8 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
168
184
|
if (asm[0] === '') continue; // blank
|
169
185
|
|
170
186
|
if (asm[0] === 'local') {
|
171
|
-
const [ name,
|
172
|
-
scope.locals[name] = { idx:
|
187
|
+
const [ name, type ] = asm.slice(1);
|
188
|
+
scope.locals[name] = { idx: scope.localInd++, type: Valtype[type] };
|
173
189
|
continue;
|
174
190
|
}
|
175
191
|
|
@@ -179,52 +195,74 @@ const generate = (scope, decl, global = false, name = undefined, valueUnused = f
|
|
179
195
|
}
|
180
196
|
|
181
197
|
if (asm[0] === 'memory') {
|
182
|
-
allocPage('asm instrinsic');
|
198
|
+
allocPage(scope, 'asm instrinsic');
|
183
199
|
// todo: add to store/load offset insts
|
184
200
|
continue;
|
185
201
|
}
|
186
202
|
|
187
203
|
let inst = Opcodes[asm[0].replace('.', '_')];
|
188
|
-
if (
|
204
|
+
if (inst == null) throw new Error(`inline asm: inst ${asm[0]} not found`);
|
189
205
|
|
190
206
|
if (!Array.isArray(inst)) inst = [ inst ];
|
191
|
-
const immediates = asm.slice(1).map(x =>
|
207
|
+
const immediates = asm.slice(1).map(x => {
|
208
|
+
const int = parseInt(x);
|
209
|
+
if (Number.isNaN(int)) return scope.locals[x]?.idx;
|
210
|
+
return int;
|
211
|
+
});
|
192
212
|
|
193
|
-
out.push([ ...inst, ...immediates ]);
|
213
|
+
out.push([ ...inst, ...immediates.flatMap(x => signedLEB128(x)) ]);
|
194
214
|
}
|
195
215
|
|
196
216
|
return out;
|
197
217
|
},
|
198
218
|
|
199
|
-
|
200
|
-
|
219
|
+
__Porffor_bs: str => [
|
220
|
+
...makeString(scope, str, global, name, true),
|
201
221
|
|
202
|
-
|
203
|
-
...number(
|
204
|
-
|
222
|
+
...(name ? setType(scope, name, TYPES._bytestring) : [
|
223
|
+
...number(TYPES._bytestring, Valtype.i32),
|
224
|
+
...setLastType(scope)
|
225
|
+
])
|
226
|
+
],
|
227
|
+
__Porffor_s: str => [
|
228
|
+
...makeString(scope, str, global, name, false),
|
205
229
|
|
206
|
-
|
207
|
-
...number(
|
208
|
-
|
209
|
-
]
|
210
|
-
|
211
|
-
}
|
230
|
+
...(name ? setType(scope, name, TYPES.string) : [
|
231
|
+
...number(TYPES.string, Valtype.i32),
|
232
|
+
...setLastType(scope)
|
233
|
+
])
|
234
|
+
],
|
235
|
+
};
|
212
236
|
|
213
|
-
const
|
237
|
+
const func = decl.tag.name;
|
214
238
|
// hack for inline asm
|
215
|
-
if (!funcs[
|
239
|
+
if (!funcs[func]) return todo(scope, 'tagged template expressions not implemented', true);
|
240
|
+
|
241
|
+
const { quasis, expressions } = decl.quasi;
|
242
|
+
let str = quasis[0].value.raw;
|
243
|
+
|
244
|
+
for (let i = 0; i < expressions.length; i++) {
|
245
|
+
const e = expressions[i];
|
246
|
+
if (!e.name) {
|
247
|
+
if (e.type === 'BinaryExpression' && e.operator === '+' && e.left.type === 'Identifier' && e.right.type === 'Literal') {
|
248
|
+
str += lookupName(scope, e.left.name)[0].idx + e.right.value;
|
249
|
+
} else todo(scope, 'unsupported expression in intrinsic');
|
250
|
+
} else str += lookupName(scope, e.name)[0].idx;
|
216
251
|
|
217
|
-
|
218
|
-
|
252
|
+
str += quasis[i + 1].value.raw;
|
253
|
+
}
|
254
|
+
|
255
|
+
return funcs[func](str);
|
219
256
|
}
|
220
257
|
|
221
258
|
default:
|
222
|
-
|
223
|
-
|
259
|
+
// ignore typescript nodes
|
260
|
+
if (decl.type.startsWith('TS') ||
|
261
|
+
decl.type === 'ImportDeclaration' && decl.importKind === 'type') {
|
224
262
|
return [];
|
225
263
|
}
|
226
264
|
|
227
|
-
return todo(`no generation for ${decl.type}!`);
|
265
|
+
return todo(scope, `no generation for ${decl.type}!`);
|
228
266
|
}
|
229
267
|
};
|
230
268
|
|
@@ -252,7 +290,7 @@ const lookupName = (scope, _name) => {
|
|
252
290
|
return [ undefined, undefined ];
|
253
291
|
};
|
254
292
|
|
255
|
-
const internalThrow = (scope, constructor, message, expectsValue =
|
293
|
+
const internalThrow = (scope, constructor, message, expectsValue = Prefs.alwaysValueInternalThrows) => [
|
256
294
|
...generateThrow(scope, {
|
257
295
|
argument: {
|
258
296
|
type: 'NewExpression',
|
@@ -274,25 +312,33 @@ const generateIdent = (scope, decl) => {
|
|
274
312
|
const name = mapName(rawName);
|
275
313
|
let local = scope.locals[rawName];
|
276
314
|
|
277
|
-
if (builtinVars
|
315
|
+
if (Object.hasOwn(builtinVars, name)) {
|
278
316
|
if (builtinVars[name].floatOnly && valtype[0] === 'i') throw new Error(`Cannot use ${unhackName(name)} with integer valtype`);
|
279
|
-
|
317
|
+
|
318
|
+
let wasm = builtinVars[name];
|
319
|
+
if (typeof wasm === 'function') wasm = asmFuncToAsm(wasm, { name });
|
320
|
+
return wasm.slice();
|
321
|
+
}
|
322
|
+
|
323
|
+
if (Object.hasOwn(builtinFuncs, name) || Object.hasOwn(internalConstrs, name)) {
|
324
|
+
// todo: return an actual something
|
325
|
+
return number(1);
|
280
326
|
}
|
281
327
|
|
282
|
-
if (
|
328
|
+
if (isExistingProtoFunc(name)) {
|
283
329
|
// todo: return an actual something
|
284
330
|
return number(1);
|
285
331
|
}
|
286
332
|
|
287
|
-
if (local === undefined) {
|
333
|
+
if (local?.idx === undefined) {
|
288
334
|
// no local var with name
|
289
|
-
if (
|
290
|
-
if (funcIndex
|
335
|
+
if (Object.hasOwn(importedFuncs, name)) return number(importedFuncs[name]);
|
336
|
+
if (Object.hasOwn(funcIndex, name)) return number(funcIndex[name]);
|
291
337
|
|
292
|
-
if (globals
|
338
|
+
if (Object.hasOwn(globals, name)) return [ [ Opcodes.global_get, globals[name].idx ] ];
|
293
339
|
}
|
294
340
|
|
295
|
-
if (local === undefined && rawName.startsWith('__')) {
|
341
|
+
if (local?.idx === undefined && rawName.startsWith('__')) {
|
296
342
|
// return undefined if unknown key in already known var
|
297
343
|
let parent = rawName.slice(2).split('_').slice(0, -1).join('_');
|
298
344
|
if (parent.includes('_')) parent = '__' + parent;
|
@@ -301,7 +347,7 @@ const generateIdent = (scope, decl) => {
|
|
301
347
|
if (!parentLookup[1]) return number(UNDEFINED);
|
302
348
|
}
|
303
349
|
|
304
|
-
if (local === undefined) return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
|
350
|
+
if (local?.idx === undefined) return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
|
305
351
|
|
306
352
|
return [ [ Opcodes.local_get, local.idx ] ];
|
307
353
|
};
|
@@ -314,14 +360,18 @@ const generateReturn = (scope, decl) => {
|
|
314
360
|
// just bare "return"
|
315
361
|
return [
|
316
362
|
...number(UNDEFINED), // "undefined" if func returns
|
317
|
-
...
|
363
|
+
...(scope.returnType != null ? [] : [
|
364
|
+
...number(TYPES.undefined, Valtype.i32) // type undefined
|
365
|
+
]),
|
318
366
|
[ Opcodes.return ]
|
319
367
|
];
|
320
368
|
}
|
321
369
|
|
322
370
|
return [
|
323
371
|
...generate(scope, decl.argument),
|
324
|
-
...
|
372
|
+
...(scope.returnType != null ? [] : [
|
373
|
+
...getNodeType(scope, decl.argument)
|
374
|
+
]),
|
325
375
|
[ Opcodes.return ]
|
326
376
|
];
|
327
377
|
};
|
@@ -335,7 +385,8 @@ const localTmp = (scope, name, type = valtypeBinary) => {
|
|
335
385
|
return idx;
|
336
386
|
};
|
337
387
|
|
338
|
-
const isIntOp = op => op && (op[0] >=
|
388
|
+
const isIntOp = op => op && ((op[0] >= 0x45 && op[0] <= 0x4f) || (op[0] >= 0x67 && op[0] <= 0x78) || op[0] === 0x41);
|
389
|
+
const isFloatToIntOp = op => op && (op[0] >= 0xb7 && op[0] <= 0xba);
|
339
390
|
|
340
391
|
const performLogicOp = (scope, op, left, right, leftType, rightType) => {
|
341
392
|
const checks = {
|
@@ -344,7 +395,7 @@ const performLogicOp = (scope, op, left, right, leftType, rightType) => {
|
|
344
395
|
'??': nullish
|
345
396
|
};
|
346
397
|
|
347
|
-
if (!checks[op]) return todo(`logic operator ${op} not implemented yet
|
398
|
+
if (!checks[op]) return todo(scope, `logic operator ${op} not implemented yet`, true);
|
348
399
|
|
349
400
|
// generic structure for {a} OP {b}
|
350
401
|
// -->
|
@@ -352,8 +403,8 @@ const performLogicOp = (scope, op, left, right, leftType, rightType) => {
|
|
352
403
|
|
353
404
|
// if we can, use int tmp and convert at the end to help prevent unneeded conversions
|
354
405
|
// (like if we are in an if condition - very common)
|
355
|
-
const leftIsInt =
|
356
|
-
const rightIsInt =
|
406
|
+
const leftIsInt = isFloatToIntOp(left[left.length - 1]);
|
407
|
+
const rightIsInt = isFloatToIntOp(right[right.length - 1]);
|
357
408
|
|
358
409
|
const canInt = leftIsInt && rightIsInt;
|
359
410
|
|
@@ -370,12 +421,12 @@ const performLogicOp = (scope, op, left, right, leftType, rightType) => {
|
|
370
421
|
...right,
|
371
422
|
// note type
|
372
423
|
...rightType,
|
373
|
-
setLastType(scope),
|
424
|
+
...setLastType(scope),
|
374
425
|
[ Opcodes.else ],
|
375
426
|
[ Opcodes.local_get, localTmp(scope, 'logictmpi', Valtype.i32) ],
|
376
427
|
// note type
|
377
428
|
...leftType,
|
378
|
-
setLastType(scope),
|
429
|
+
...setLastType(scope),
|
379
430
|
[ Opcodes.end ],
|
380
431
|
Opcodes.i32_from
|
381
432
|
];
|
@@ -389,17 +440,17 @@ const performLogicOp = (scope, op, left, right, leftType, rightType) => {
|
|
389
440
|
...right,
|
390
441
|
// note type
|
391
442
|
...rightType,
|
392
|
-
setLastType(scope),
|
443
|
+
...setLastType(scope),
|
393
444
|
[ Opcodes.else ],
|
394
445
|
[ Opcodes.local_get, localTmp(scope, 'logictmp') ],
|
395
446
|
// note type
|
396
447
|
...leftType,
|
397
|
-
setLastType(scope),
|
448
|
+
...setLastType(scope),
|
398
449
|
[ Opcodes.end ]
|
399
450
|
];
|
400
451
|
};
|
401
452
|
|
402
|
-
const concatStrings = (scope, left, right, global, name, assign) => {
|
453
|
+
const concatStrings = (scope, left, right, global, name, assign = false, bytestrings = false) => {
|
403
454
|
// todo: this should be rewritten into a built-in/func: String.prototype.concat
|
404
455
|
// todo: convert left and right to strings if not
|
405
456
|
// todo: optimize by looking up names in arrays and using that if exists?
|
@@ -409,11 +460,8 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
409
460
|
const rightLength = localTmp(scope, 'concat_right_length', Valtype.i32);
|
410
461
|
const leftLength = localTmp(scope, 'concat_left_length', Valtype.i32);
|
411
462
|
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
if (assign) {
|
416
|
-
const pointer = arrays.get(name ?? '$undeclared');
|
463
|
+
if (assign && Prefs.aotPointerOpt) {
|
464
|
+
const pointer = scope.arrays?.get(name ?? '$undeclared');
|
417
465
|
|
418
466
|
return [
|
419
467
|
// setup right
|
@@ -438,11 +486,11 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
438
486
|
[ Opcodes.i32_store, Math.log2(ValtypeSize.i32) - 1, ...unsignedLEB128(pointer) ],
|
439
487
|
|
440
488
|
// copy right
|
441
|
-
// dst = out pointer + length size + current length *
|
489
|
+
// dst = out pointer + length size + current length * sizeof valtype
|
442
490
|
...number(pointer + ValtypeSize.i32, Valtype.i32),
|
443
491
|
|
444
492
|
[ Opcodes.local_get, leftLength ],
|
445
|
-
...number(ValtypeSize.i16, Valtype.i32),
|
493
|
+
...number(bytestrings ? ValtypeSize.i8 : ValtypeSize.i16, Valtype.i32),
|
446
494
|
[ Opcodes.i32_mul ],
|
447
495
|
[ Opcodes.i32_add ],
|
448
496
|
|
@@ -451,9 +499,9 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
451
499
|
...number(ValtypeSize.i32, Valtype.i32),
|
452
500
|
[ Opcodes.i32_add ],
|
453
501
|
|
454
|
-
// size = right length *
|
502
|
+
// size = right length * sizeof valtype
|
455
503
|
[ Opcodes.local_get, rightLength ],
|
456
|
-
...number(ValtypeSize.i16, Valtype.i32),
|
504
|
+
...number(bytestrings ? ValtypeSize.i8 : ValtypeSize.i16, Valtype.i32),
|
457
505
|
[ Opcodes.i32_mul ],
|
458
506
|
|
459
507
|
[ ...Opcodes.memory_copy, 0x00, 0x00 ],
|
@@ -511,11 +559,11 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
511
559
|
[ ...Opcodes.memory_copy, 0x00, 0x00 ],
|
512
560
|
|
513
561
|
// copy right
|
514
|
-
// dst = out pointer + length size + left length *
|
562
|
+
// dst = out pointer + length size + left length * sizeof valtype
|
515
563
|
...number(pointer + ValtypeSize.i32, Valtype.i32),
|
516
564
|
|
517
565
|
[ Opcodes.local_get, leftLength ],
|
518
|
-
...number(ValtypeSize.i16, Valtype.i32),
|
566
|
+
...number(bytestrings ? ValtypeSize.i8 : ValtypeSize.i16, Valtype.i32),
|
519
567
|
[ Opcodes.i32_mul ],
|
520
568
|
[ Opcodes.i32_add ],
|
521
569
|
|
@@ -524,9 +572,9 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
524
572
|
...number(ValtypeSize.i32, Valtype.i32),
|
525
573
|
[ Opcodes.i32_add ],
|
526
574
|
|
527
|
-
// size = right length *
|
575
|
+
// size = right length * sizeof valtype
|
528
576
|
[ Opcodes.local_get, rightLength ],
|
529
|
-
...number(ValtypeSize.i16, Valtype.i32),
|
577
|
+
...number(bytestrings ? ValtypeSize.i8 : ValtypeSize.i16, Valtype.i32),
|
530
578
|
[ Opcodes.i32_mul ],
|
531
579
|
|
532
580
|
[ ...Opcodes.memory_copy, 0x00, 0x00 ],
|
@@ -536,7 +584,7 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
536
584
|
];
|
537
585
|
};
|
538
586
|
|
539
|
-
const compareStrings = (scope, left, right) => {
|
587
|
+
const compareStrings = (scope, left, right, bytestrings = false) => {
|
540
588
|
// todo: this should be rewritten into a func
|
541
589
|
// todo: convert left and right to strings if not
|
542
590
|
// todo: optimize by looking up names in arrays and using that if exists?
|
@@ -545,7 +593,6 @@ const compareStrings = (scope, left, right) => {
|
|
545
593
|
const leftPointer = localTmp(scope, 'compare_left_pointer', Valtype.i32);
|
546
594
|
const leftLength = localTmp(scope, 'compare_left_length', Valtype.i32);
|
547
595
|
const rightPointer = localTmp(scope, 'compare_right_pointer', Valtype.i32);
|
548
|
-
const rightLength = localTmp(scope, 'compare_right_length', Valtype.i32);
|
549
596
|
|
550
597
|
const index = localTmp(scope, 'compare_index', Valtype.i32);
|
551
598
|
const indexEnd = localTmp(scope, 'compare_index_end', Valtype.i32);
|
@@ -573,7 +620,6 @@ const compareStrings = (scope, left, right) => {
|
|
573
620
|
|
574
621
|
[ Opcodes.local_get, rightPointer ],
|
575
622
|
[ Opcodes.i32_load, Math.log2(ValtypeSize[valtype]) - 1, ...unsignedLEB128(0) ],
|
576
|
-
[ Opcodes.local_tee, rightLength ],
|
577
623
|
|
578
624
|
// fast path: check leftLength != rightLength
|
579
625
|
[ Opcodes.i32_ne ],
|
@@ -588,11 +634,13 @@ const compareStrings = (scope, left, right) => {
|
|
588
634
|
...number(0, Valtype.i32),
|
589
635
|
[ Opcodes.local_set, index ],
|
590
636
|
|
591
|
-
// setup index end as length * sizeof
|
637
|
+
// setup index end as length * sizeof valtype (1 for bytestring, 2 for string)
|
592
638
|
// we do this instead of having to do mul/div each iter for perf™
|
593
639
|
[ Opcodes.local_get, leftLength ],
|
594
|
-
...
|
595
|
-
|
640
|
+
...(bytestrings ? [] : [
|
641
|
+
...number(ValtypeSize.i16, Valtype.i32),
|
642
|
+
[ Opcodes.i32_mul ],
|
643
|
+
]),
|
596
644
|
[ Opcodes.local_set, indexEnd ],
|
597
645
|
|
598
646
|
// iterate over each char and check if eq
|
@@ -602,13 +650,17 @@ const compareStrings = (scope, left, right) => {
|
|
602
650
|
[ Opcodes.local_get, index ],
|
603
651
|
[ Opcodes.local_get, leftPointer ],
|
604
652
|
[ Opcodes.i32_add ],
|
605
|
-
|
653
|
+
bytestrings ?
|
654
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ] :
|
655
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
606
656
|
|
607
657
|
// fetch right
|
608
658
|
[ Opcodes.local_get, index ],
|
609
659
|
[ Opcodes.local_get, rightPointer ],
|
610
660
|
[ Opcodes.i32_add ],
|
611
|
-
|
661
|
+
bytestrings ?
|
662
|
+
[ Opcodes.i32_load8_u, 0, ValtypeSize.i32 ] :
|
663
|
+
[ Opcodes.i32_load16_u, Math.log2(ValtypeSize.i16) - 1, ValtypeSize.i32 ],
|
612
664
|
|
613
665
|
// not equal, "return" false
|
614
666
|
[ Opcodes.i32_ne ],
|
@@ -617,13 +669,13 @@ const compareStrings = (scope, left, right) => {
|
|
617
669
|
[ Opcodes.br, 2 ],
|
618
670
|
[ Opcodes.end ],
|
619
671
|
|
620
|
-
// index += sizeof
|
672
|
+
// index += sizeof valtype (1 for bytestring, 2 for string)
|
621
673
|
[ Opcodes.local_get, index ],
|
622
|
-
...number(ValtypeSize.i16, Valtype.i32),
|
674
|
+
...number(bytestrings ? ValtypeSize.i8 : ValtypeSize.i16, Valtype.i32),
|
623
675
|
[ Opcodes.i32_add ],
|
624
676
|
[ Opcodes.local_tee, index ],
|
625
677
|
|
626
|
-
// if index != index end (length * sizeof
|
678
|
+
// if index != index end (length * sizeof valtype), loop
|
627
679
|
[ Opcodes.local_get, indexEnd ],
|
628
680
|
[ Opcodes.i32_ne ],
|
629
681
|
[ Opcodes.br_if, 0 ],
|
@@ -644,16 +696,18 @@ const compareStrings = (scope, left, right) => {
|
|
644
696
|
};
|
645
697
|
|
646
698
|
const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
|
647
|
-
if (
|
699
|
+
if (isFloatToIntOp(wasm[wasm.length - 1])) return [
|
648
700
|
...wasm,
|
649
701
|
...(!intIn && intOut ? [ Opcodes.i32_to_u ] : [])
|
650
702
|
];
|
703
|
+
// if (isIntOp(wasm[wasm.length - 1])) return [ ...wasm ];
|
651
704
|
|
652
|
-
const
|
705
|
+
const useTmp = knownType(scope, type) == null;
|
706
|
+
const tmp = useTmp && localTmp(scope, `#logicinner_tmp${intIn ? '_int' : ''}`, intIn ? Valtype.i32 : valtypeBinary);
|
653
707
|
|
654
708
|
const def = [
|
655
709
|
// if value != 0
|
656
|
-
[ Opcodes.local_get, tmp ],
|
710
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
657
711
|
|
658
712
|
// ...(intIn ? [ [ Opcodes.i32_eqz ] ] : [ ...Opcodes.eqz ]),
|
659
713
|
...(!intOut || (intIn && intOut) ? [] : [ Opcodes.i32_to_u ]),
|
@@ -665,7 +719,7 @@ const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
665
719
|
|
666
720
|
return [
|
667
721
|
...wasm,
|
668
|
-
[ Opcodes.local_set, tmp ],
|
722
|
+
...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
|
669
723
|
|
670
724
|
...typeSwitch(scope, type, {
|
671
725
|
// [TYPES.number]: def,
|
@@ -674,7 +728,7 @@ const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
674
728
|
...number(1, intOut ? Valtype.i32 : valtypeBinary)
|
675
729
|
],
|
676
730
|
[TYPES.string]: [
|
677
|
-
[ Opcodes.local_get, tmp ],
|
731
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
678
732
|
...(intIn ? [] : [ Opcodes.i32_to_u ]),
|
679
733
|
|
680
734
|
// get length
|
@@ -686,7 +740,7 @@ const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
686
740
|
...(intOut ? [] : [ Opcodes.i32_from_u ])
|
687
741
|
],
|
688
742
|
[TYPES._bytestring]: [ // duplicate of string
|
689
|
-
|
743
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
690
744
|
...(intIn ? [] : [ Opcodes.i32_to_u ]),
|
691
745
|
|
692
746
|
// get length
|
@@ -700,10 +754,12 @@ const truthy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
700
754
|
};
|
701
755
|
|
702
756
|
const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
|
703
|
-
const
|
757
|
+
const useTmp = knownType(scope, type) == null;
|
758
|
+
const tmp = useTmp && localTmp(scope, `#logicinner_tmp${intIn ? '_int' : ''}`, intIn ? Valtype.i32 : valtypeBinary);
|
759
|
+
|
704
760
|
return [
|
705
761
|
...wasm,
|
706
|
-
[ Opcodes.local_set, tmp ],
|
762
|
+
...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
|
707
763
|
|
708
764
|
...typeSwitch(scope, type, {
|
709
765
|
[TYPES._array]: [
|
@@ -711,7 +767,7 @@ const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
711
767
|
...number(0, intOut ? Valtype.i32 : valtypeBinary)
|
712
768
|
],
|
713
769
|
[TYPES.string]: [
|
714
|
-
[ Opcodes.local_get, tmp ],
|
770
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
715
771
|
...(intIn ? [] : [ Opcodes.i32_to_u ]),
|
716
772
|
|
717
773
|
// get length
|
@@ -722,7 +778,7 @@ const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
722
778
|
...(intOut ? [] : [ Opcodes.i32_from_u ])
|
723
779
|
],
|
724
780
|
[TYPES._bytestring]: [ // duplicate of string
|
725
|
-
[ Opcodes.local_get, tmp ],
|
781
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
726
782
|
...(intIn ? [] : [ Opcodes.i32_to_u ]),
|
727
783
|
|
728
784
|
// get length
|
@@ -734,7 +790,7 @@ const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
734
790
|
],
|
735
791
|
default: [
|
736
792
|
// if value == 0
|
737
|
-
[ Opcodes.local_get, tmp ],
|
793
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
738
794
|
|
739
795
|
...(intIn ? [ [ Opcodes.i32_eqz ] ] : [ ...Opcodes.eqz ]),
|
740
796
|
...(intOut ? [] : [ Opcodes.i32_from_u ])
|
@@ -744,10 +800,12 @@ const falsy = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
744
800
|
};
|
745
801
|
|
746
802
|
const nullish = (scope, wasm, type, intIn = false, intOut = false) => {
|
747
|
-
const
|
803
|
+
const useTmp = knownType(scope, type) == null;
|
804
|
+
const tmp = useTmp && localTmp(scope, `#logicinner_tmp${intIn ? '_int' : ''}`, intIn ? Valtype.i32 : valtypeBinary);
|
805
|
+
|
748
806
|
return [
|
749
807
|
...wasm,
|
750
|
-
[ Opcodes.local_set, tmp ],
|
808
|
+
...(!useTmp ? [] : [ [ Opcodes.local_set, tmp ] ]),
|
751
809
|
|
752
810
|
...typeSwitch(scope, type, {
|
753
811
|
[TYPES.undefined]: [
|
@@ -756,7 +814,7 @@ const nullish = (scope, wasm, type, intIn = false, intOut = false) => {
|
|
756
814
|
],
|
757
815
|
[TYPES.object]: [
|
758
816
|
// object, null if == 0
|
759
|
-
[ Opcodes.local_get, tmp ],
|
817
|
+
...(!useTmp ? [] : [ [ Opcodes.local_get, tmp ] ]),
|
760
818
|
|
761
819
|
...(intIn ? [ [ Opcodes.i32_eqz ] ] : [ ...Opcodes.eqz ]),
|
762
820
|
...(intOut ? [] : [ Opcodes.i32_from_u ])
|
@@ -785,11 +843,14 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
785
843
|
return performLogicOp(scope, op, left, right, leftType, rightType);
|
786
844
|
}
|
787
845
|
|
846
|
+
const knownLeft = knownType(scope, leftType);
|
847
|
+
const knownRight = knownType(scope, rightType);
|
848
|
+
|
788
849
|
const eqOp = ['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(op);
|
789
850
|
const strictOp = op === '===' || op === '!==';
|
790
851
|
|
791
852
|
const startOut = [], endOut = [];
|
792
|
-
const
|
853
|
+
const finalize = out => startOut.concat(out, endOut);
|
793
854
|
|
794
855
|
// if strict (in)equal check types match
|
795
856
|
if (strictOp) {
|
@@ -834,31 +895,59 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
834
895
|
// todo: if equality op and an operand is undefined, return false
|
835
896
|
// todo: niche null hell with 0
|
836
897
|
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
898
|
+
if (knownLeft === TYPES.string || knownRight === TYPES.string) {
|
899
|
+
if (op === '+') {
|
900
|
+
// todo: this should be dynamic too but for now only static
|
901
|
+
// string concat (a + b)
|
902
|
+
return concatStrings(scope, left, right, _global, _name, assign, false);
|
903
|
+
}
|
904
|
+
|
905
|
+
// not an equality op, NaN
|
906
|
+
if (!eqOp) return number(NaN);
|
907
|
+
|
908
|
+
// else leave bool ops
|
909
|
+
// todo: convert string to number if string and number/bool
|
910
|
+
// todo: string (>|>=|<|<=) string
|
911
|
+
|
912
|
+
// string comparison
|
913
|
+
if (op === '===' || op === '==') {
|
914
|
+
return compareStrings(scope, left, right);
|
915
|
+
}
|
916
|
+
|
917
|
+
if (op === '!==' || op === '!=') {
|
918
|
+
return [
|
919
|
+
...compareStrings(scope, left, right),
|
920
|
+
[ Opcodes.i32_eqz ]
|
921
|
+
];
|
922
|
+
}
|
923
|
+
}
|
924
|
+
|
925
|
+
if (knownLeft === TYPES._bytestring || knownRight === TYPES._bytestring) {
|
926
|
+
if (op === '+') {
|
927
|
+
// todo: this should be dynamic too but for now only static
|
928
|
+
// string concat (a + b)
|
929
|
+
return concatStrings(scope, left, right, _global, _name, assign, true);
|
930
|
+
}
|
931
|
+
|
932
|
+
// not an equality op, NaN
|
933
|
+
if (!eqOp) return number(NaN);
|
934
|
+
|
935
|
+
// else leave bool ops
|
936
|
+
// todo: convert string to number if string and number/bool
|
937
|
+
// todo: string (>|>=|<|<=) string
|
938
|
+
|
939
|
+
// string comparison
|
940
|
+
if (op === '===' || op === '==') {
|
941
|
+
return compareStrings(scope, left, right, true);
|
942
|
+
}
|
943
|
+
|
944
|
+
if (op === '!==' || op === '!=') {
|
945
|
+
return [
|
946
|
+
...compareStrings(scope, left, right, true),
|
947
|
+
[ Opcodes.i32_eqz ]
|
948
|
+
];
|
949
|
+
}
|
950
|
+
}
|
862
951
|
|
863
952
|
let ops = operatorOpcode[valtype][op];
|
864
953
|
|
@@ -868,33 +957,69 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
868
957
|
includeBuiltin(scope, builtinName);
|
869
958
|
const idx = funcIndex[builtinName];
|
870
959
|
|
871
|
-
return
|
960
|
+
return finalize([
|
872
961
|
...left,
|
873
962
|
...right,
|
874
963
|
[ Opcodes.call, idx ]
|
875
964
|
]);
|
876
965
|
}
|
877
966
|
|
878
|
-
if (!ops) return todo(`operator ${op} not implemented yet
|
967
|
+
if (!ops) return todo(scope, `operator ${op} not implemented yet`, true);
|
879
968
|
|
880
969
|
if (!Array.isArray(ops)) ops = [ ops ];
|
881
970
|
ops = [ ops ];
|
882
971
|
|
883
972
|
let tmpLeft, tmpRight;
|
884
973
|
// if equal op, check if strings for compareStrings
|
885
|
-
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
// todo: intelligent partial skip later
|
890
|
-
// if neither known are string, stop this madness
|
891
|
-
if ((knownLeft != null && knownLeft !== TYPES.string) && (knownRight != null && knownRight !== TYPES.string)) {
|
892
|
-
return;
|
893
|
-
}
|
974
|
+
// todo: intelligent partial skip later
|
975
|
+
// if neither known are string, stop this madness
|
976
|
+
// we already do known checks earlier, so don't need to recheck
|
894
977
|
|
978
|
+
if ((op === '===' || op === '==' || op === '!==' || op === '!=') && (knownLeft == null && knownRight == null)) {
|
895
979
|
tmpLeft = localTmp(scope, '__tmpop_left');
|
896
980
|
tmpRight = localTmp(scope, '__tmpop_right');
|
897
981
|
|
982
|
+
// returns false for one string, one not - but more ops/slower
|
983
|
+
// ops.unshift(...stringOnly([
|
984
|
+
// // if left is string
|
985
|
+
// ...leftType,
|
986
|
+
// ...number(TYPES.string, Valtype.i32),
|
987
|
+
// [ Opcodes.i32_eq ],
|
988
|
+
|
989
|
+
// // if right is string
|
990
|
+
// ...rightType,
|
991
|
+
// ...number(TYPES.string, Valtype.i32),
|
992
|
+
// [ Opcodes.i32_eq ],
|
993
|
+
|
994
|
+
// // if either are true
|
995
|
+
// [ Opcodes.i32_or ],
|
996
|
+
// [ Opcodes.if, Blocktype.void ],
|
997
|
+
|
998
|
+
// // todo: convert non-strings to strings, for now fail immediately if one is not
|
999
|
+
// // if left is not string
|
1000
|
+
// ...leftType,
|
1001
|
+
// ...number(TYPES.string, Valtype.i32),
|
1002
|
+
// [ Opcodes.i32_ne ],
|
1003
|
+
|
1004
|
+
// // if right is not string
|
1005
|
+
// ...rightType,
|
1006
|
+
// ...number(TYPES.string, Valtype.i32),
|
1007
|
+
// [ Opcodes.i32_ne ],
|
1008
|
+
|
1009
|
+
// // if either are true
|
1010
|
+
// [ Opcodes.i32_or ],
|
1011
|
+
// [ Opcodes.if, Blocktype.void ],
|
1012
|
+
// ...number(0, Valtype.i32),
|
1013
|
+
// [ Opcodes.br, 2 ],
|
1014
|
+
// [ Opcodes.end ],
|
1015
|
+
|
1016
|
+
// ...compareStrings(scope, [ [ Opcodes.local_get, tmpLeft ] ], [ [ Opcodes.local_get, tmpRight ] ]),
|
1017
|
+
// ...(op === '!==' || op === '!=' ? [ [ Opcodes.i32_eqz ] ] : []),
|
1018
|
+
// [ Opcodes.br, 1 ],
|
1019
|
+
// [ Opcodes.end ],
|
1020
|
+
// ]));
|
1021
|
+
|
1022
|
+
// does not handle one string, one not (such cases go past)
|
898
1023
|
ops.unshift(...stringOnly([
|
899
1024
|
// if left is string
|
900
1025
|
...leftType,
|
@@ -906,30 +1031,28 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
906
1031
|
...number(TYPES.string, Valtype.i32),
|
907
1032
|
[ Opcodes.i32_eq ],
|
908
1033
|
|
909
|
-
// if
|
910
|
-
[ Opcodes.
|
1034
|
+
// if both are true
|
1035
|
+
[ Opcodes.i32_and ],
|
911
1036
|
[ Opcodes.if, Blocktype.void ],
|
1037
|
+
...compareStrings(scope, [ [ Opcodes.local_get, tmpLeft ] ], [ [ Opcodes.local_get, tmpRight ] ]),
|
1038
|
+
...(op === '!==' || op === '!=' ? [ [ Opcodes.i32_eqz ] ] : []),
|
1039
|
+
[ Opcodes.br, 1 ],
|
1040
|
+
[ Opcodes.end ],
|
912
1041
|
|
913
|
-
//
|
914
|
-
// if left is not string
|
1042
|
+
// if left is bytestring
|
915
1043
|
...leftType,
|
916
|
-
...number(TYPES.
|
917
|
-
[ Opcodes.
|
1044
|
+
...number(TYPES._bytestring, Valtype.i32),
|
1045
|
+
[ Opcodes.i32_eq ],
|
918
1046
|
|
919
|
-
// if right is
|
1047
|
+
// if right is bytestring
|
920
1048
|
...rightType,
|
921
|
-
...number(TYPES.
|
922
|
-
[ Opcodes.
|
1049
|
+
...number(TYPES._bytestring, Valtype.i32),
|
1050
|
+
[ Opcodes.i32_eq ],
|
923
1051
|
|
924
|
-
// if
|
925
|
-
[ Opcodes.
|
1052
|
+
// if both are true
|
1053
|
+
[ Opcodes.i32_and ],
|
926
1054
|
[ Opcodes.if, Blocktype.void ],
|
927
|
-
...
|
928
|
-
[ Opcodes.br, 1 ],
|
929
|
-
[ Opcodes.end ],
|
930
|
-
|
931
|
-
...compareStrings(scope, [ [ Opcodes.local_get, tmpLeft ] ], [ [ Opcodes.local_get, tmpRight ] ]),
|
932
|
-
// ...compareStrings(scope, [ [ Opcodes.local_get, tmpLeft ] ], [ [ Opcodes.local_get, tmpRight ] ]),
|
1055
|
+
...compareStrings(scope, [ [ Opcodes.local_get, tmpLeft ] ], [ [ Opcodes.local_get, tmpRight ] ], true),
|
933
1056
|
...(op === '!==' || op === '!=' ? [ [ Opcodes.i32_eqz ] ] : []),
|
934
1057
|
[ Opcodes.br, 1 ],
|
935
1058
|
[ Opcodes.end ],
|
@@ -941,9 +1064,9 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
941
1064
|
// endOut.push(stringOnly([ Opcodes.end ]));
|
942
1065
|
endOut.unshift(stringOnly([ Opcodes.end ]));
|
943
1066
|
// }
|
944
|
-
}
|
1067
|
+
}
|
945
1068
|
|
946
|
-
return
|
1069
|
+
return finalize([
|
947
1070
|
...left,
|
948
1071
|
...(tmpLeft != null ? stringOnly([ [ Opcodes.local_tee, tmpLeft ] ]) : []),
|
949
1072
|
...right,
|
@@ -960,7 +1083,22 @@ const generateBinaryExp = (scope, decl, _global, _name) => {
|
|
960
1083
|
return out;
|
961
1084
|
};
|
962
1085
|
|
963
|
-
const
|
1086
|
+
const asmFuncToAsm = (func, { name = '#unknown_asm_func', params = [], locals = [], returns = [], localInd = 0 }) => {
|
1087
|
+
return func({ name, params, locals, returns, localInd }, {
|
1088
|
+
TYPES, TYPE_NAMES, typeSwitch, makeArray, makeString, allocPage,
|
1089
|
+
builtin: name => {
|
1090
|
+
let idx = funcIndex[name] ?? importedFuncs[name];
|
1091
|
+
if (idx === undefined && builtinFuncs[name]) {
|
1092
|
+
includeBuiltin(null, name);
|
1093
|
+
idx = funcIndex[name];
|
1094
|
+
}
|
1095
|
+
|
1096
|
+
return idx;
|
1097
|
+
}
|
1098
|
+
});
|
1099
|
+
};
|
1100
|
+
|
1101
|
+
const asmFunc = (name, { wasm, params, locals: localTypes, globals: globalTypes = [], globalInits, returns, returnType, localNames = [], globalNames = [], data: _data = [] }) => {
|
964
1102
|
const existing = funcs.find(x => x.name === name);
|
965
1103
|
if (existing) return existing;
|
966
1104
|
|
@@ -972,18 +1110,14 @@ const asmFunc = (name, { wasm, params, locals: localTypes, globals: globalTypes
|
|
972
1110
|
locals[nameParam(i)] = { idx: i, type: allLocals[i] };
|
973
1111
|
}
|
974
1112
|
|
975
|
-
|
976
|
-
const
|
977
|
-
|
978
|
-
|
979
|
-
locals,
|
980
|
-
returns,
|
981
|
-
localInd: allLocals.length,
|
982
|
-
};
|
983
|
-
|
984
|
-
wasm = wasm(scope, { TYPES, typeSwitch, makeArray });
|
1113
|
+
for (const x of _data) {
|
1114
|
+
const copy = { ...x };
|
1115
|
+
copy.offset += pages.size * pageSize;
|
1116
|
+
data.push(copy);
|
985
1117
|
}
|
986
1118
|
|
1119
|
+
if (typeof wasm === 'function') wasm = asmFuncToAsm(wasm, { name, params, locals, returns, localInd: allLocals.length });
|
1120
|
+
|
987
1121
|
let baseGlobalIdx, i = 0;
|
988
1122
|
for (const type of globalTypes) {
|
989
1123
|
if (baseGlobalIdx === undefined) baseGlobalIdx = globalInd;
|
@@ -1006,7 +1140,7 @@ const asmFunc = (name, { wasm, params, locals: localTypes, globals: globalTypes
|
|
1006
1140
|
params,
|
1007
1141
|
locals,
|
1008
1142
|
returns,
|
1009
|
-
returnType:
|
1143
|
+
returnType: returnType ?? TYPES.number,
|
1010
1144
|
wasm,
|
1011
1145
|
internal: true,
|
1012
1146
|
index: currentFuncIndex++
|
@@ -1029,6 +1163,7 @@ const generateLogicExp = (scope, decl) => {
|
|
1029
1163
|
return performLogicOp(scope, decl.operator, generate(scope, decl.left), generate(scope, decl.right), getNodeType(scope, decl.left), getNodeType(scope, decl.right));
|
1030
1164
|
};
|
1031
1165
|
|
1166
|
+
// potential future ideas for nan boxing (unused):
|
1032
1167
|
// T = JS type, V = value/pointer
|
1033
1168
|
// 0bTTT
|
1034
1169
|
// qNAN: 0 11111111111 1000000000000000000000000000000000000000000000000001
|
@@ -1052,49 +1187,29 @@ const generateLogicExp = (scope, decl) => {
|
|
1052
1187
|
// 4: internal type
|
1053
1188
|
// 5: pointer
|
1054
1189
|
|
1055
|
-
const
|
1056
|
-
|
1057
|
-
|
1058
|
-
|
1059
|
-
|
1060
|
-
object: 0x04,
|
1061
|
-
function: 0x05,
|
1062
|
-
symbol: 0x06,
|
1063
|
-
bigint: 0x07,
|
1064
|
-
|
1065
|
-
// these are not "typeof" types but tracked internally
|
1066
|
-
_array: 0x10,
|
1067
|
-
_regexp: 0x11,
|
1068
|
-
_bytestring: 0x12
|
1069
|
-
};
|
1070
|
-
|
1071
|
-
const TYPE_NAMES = {
|
1072
|
-
[TYPES.number]: 'Number',
|
1073
|
-
[TYPES.boolean]: 'Boolean',
|
1074
|
-
[TYPES.string]: 'String',
|
1075
|
-
[TYPES.undefined]: 'undefined',
|
1076
|
-
[TYPES.object]: 'Object',
|
1077
|
-
[TYPES.function]: 'Function',
|
1078
|
-
[TYPES.symbol]: 'Symbol',
|
1079
|
-
[TYPES.bigint]: 'BigInt',
|
1080
|
-
|
1081
|
-
[TYPES._array]: 'Array',
|
1082
|
-
[TYPES._regexp]: 'RegExp',
|
1083
|
-
[TYPES._bytestring]: 'ByteString'
|
1190
|
+
const isExistingProtoFunc = name => {
|
1191
|
+
if (name.startsWith('__Array_prototype')) return !!prototypeFuncs[TYPES._array][name.slice(18)];
|
1192
|
+
if (name.startsWith('__String_prototype_')) return !!prototypeFuncs[TYPES.string][name.slice(19)];
|
1193
|
+
|
1194
|
+
return false;
|
1084
1195
|
};
|
1085
1196
|
|
1086
1197
|
const getType = (scope, _name) => {
|
1087
1198
|
const name = mapName(_name);
|
1088
1199
|
|
1200
|
+
// if (scope.locals[name] && !scope.locals[name + '#type']) console.log(name);
|
1201
|
+
|
1202
|
+
if (typedInput && scope.locals[name]?.metadata?.type != null) return number(scope.locals[name].metadata.type, Valtype.i32);
|
1089
1203
|
if (scope.locals[name]) return [ [ Opcodes.local_get, scope.locals[name + '#type'].idx ] ];
|
1204
|
+
|
1205
|
+
if (typedInput && globals[name]?.metadata?.type != null) return number(globals[name].metadata.type, Valtype.i32);
|
1090
1206
|
if (globals[name]) return [ [ Opcodes.global_get, globals[name + '#type'].idx ] ];
|
1091
1207
|
|
1092
1208
|
let type = TYPES.undefined;
|
1093
|
-
if (builtinVars[name]) type =
|
1209
|
+
if (builtinVars[name]) type = builtinVars[name].type ?? TYPES.number;
|
1094
1210
|
if (builtinFuncs[name] !== undefined || importedFuncs[name] !== undefined || funcIndex[name] !== undefined || internalConstrs[name] !== undefined) type = TYPES.function;
|
1095
1211
|
|
1096
|
-
if (name
|
1097
|
-
name.startsWith('__String_prototype_') && prototypeFuncs[TYPES.string][name.slice(19)]) type = TYPES.function;
|
1212
|
+
if (isExistingProtoFunc(name)) type = TYPES.function;
|
1098
1213
|
|
1099
1214
|
return number(type, Valtype.i32);
|
1100
1215
|
};
|
@@ -1117,15 +1232,16 @@ const setType = (scope, _name, type) => {
|
|
1117
1232
|
];
|
1118
1233
|
|
1119
1234
|
// throw new Error('could not find var');
|
1235
|
+
return [];
|
1120
1236
|
};
|
1121
1237
|
|
1122
1238
|
const getLastType = scope => {
|
1123
1239
|
scope.gotLastType = true;
|
1124
|
-
return [ Opcodes.local_get, localTmp(scope, '#last_type', Valtype.i32) ];
|
1240
|
+
return [ [ Opcodes.local_get, localTmp(scope, '#last_type', Valtype.i32) ] ];
|
1125
1241
|
};
|
1126
1242
|
|
1127
1243
|
const setLastType = scope => {
|
1128
|
-
return [ Opcodes.local_set, localTmp(scope, '#last_type', Valtype.i32) ];
|
1244
|
+
return [ [ Opcodes.local_set, localTmp(scope, '#last_type', Valtype.i32) ] ];
|
1129
1245
|
};
|
1130
1246
|
|
1131
1247
|
const getNodeType = (scope, node) => {
|
@@ -1150,13 +1266,25 @@ const getNodeType = (scope, node) => {
|
|
1150
1266
|
const name = node.callee.name;
|
1151
1267
|
if (!name) {
|
1152
1268
|
// iife
|
1153
|
-
if (scope.locals['#last_type']) return
|
1269
|
+
if (scope.locals['#last_type']) return getLastType(scope);
|
1154
1270
|
|
1155
1271
|
// presume
|
1156
1272
|
// todo: warn here?
|
1157
1273
|
return TYPES.number;
|
1158
1274
|
}
|
1159
1275
|
|
1276
|
+
if (node.type === 'NewExpression' && builtinFuncs[name + '$constructor']) {
|
1277
|
+
if (builtinFuncs[name + '$constructor'].typedReturns) {
|
1278
|
+
if (scope.locals['#last_type']) return getLastType(scope);
|
1279
|
+
|
1280
|
+
// presume
|
1281
|
+
// todo: warn here?
|
1282
|
+
return TYPES.number;
|
1283
|
+
}
|
1284
|
+
|
1285
|
+
return builtinFuncs[name + '$constructor'].returnType ?? TYPES.number;
|
1286
|
+
}
|
1287
|
+
|
1160
1288
|
const func = funcs.find(x => x.name === name);
|
1161
1289
|
|
1162
1290
|
if (func) {
|
@@ -1164,7 +1292,7 @@ const getNodeType = (scope, node) => {
|
|
1164
1292
|
if (func.returnType) return func.returnType;
|
1165
1293
|
}
|
1166
1294
|
|
1167
|
-
if (builtinFuncs[name]) return
|
1295
|
+
if (builtinFuncs[name] && !builtinFuncs[name].typedReturns) return builtinFuncs[name].returnType ?? TYPES.number;
|
1168
1296
|
if (internalConstrs[name]) return internalConstrs[name].type;
|
1169
1297
|
|
1170
1298
|
// check if this is a prototype function
|
@@ -1179,7 +1307,12 @@ const getNodeType = (scope, node) => {
|
|
1179
1307
|
if (protoFuncs.length === 1) return protoFuncs[0].returnType ?? TYPES.number;
|
1180
1308
|
}
|
1181
1309
|
|
1182
|
-
if (
|
1310
|
+
if (name.startsWith('__Porffor_wasm_')) {
|
1311
|
+
// todo: return undefined for non-returning ops
|
1312
|
+
return TYPES.number;
|
1313
|
+
}
|
1314
|
+
|
1315
|
+
if (scope.locals['#last_type']) return getLastType(scope);
|
1183
1316
|
|
1184
1317
|
// presume
|
1185
1318
|
// todo: warn here?
|
@@ -1227,6 +1360,15 @@ const getNodeType = (scope, node) => {
|
|
1227
1360
|
|
1228
1361
|
if (node.type === 'BinaryExpression') {
|
1229
1362
|
if (['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(node.operator)) return TYPES.boolean;
|
1363
|
+
if (node.operator !== '+') return TYPES.number;
|
1364
|
+
|
1365
|
+
const knownLeft = knownType(scope, getNodeType(scope, node.left));
|
1366
|
+
const knownRight = knownType(scope, getNodeType(scope, node.right));
|
1367
|
+
|
1368
|
+
// todo: this should be dynamic but for now only static
|
1369
|
+
if (knownLeft === TYPES.string || knownRight === TYPES.string) return TYPES.string;
|
1370
|
+
if (knownLeft === TYPES._bytestring || knownRight === TYPES._bytestring) return TYPES._bytestring;
|
1371
|
+
|
1230
1372
|
return TYPES.number;
|
1231
1373
|
|
1232
1374
|
// todo: string concat types
|
@@ -1251,7 +1393,7 @@ const getNodeType = (scope, node) => {
|
|
1251
1393
|
if (node.operator === '!') return TYPES.boolean;
|
1252
1394
|
if (node.operator === 'void') return TYPES.undefined;
|
1253
1395
|
if (node.operator === 'delete') return TYPES.boolean;
|
1254
|
-
if (node.operator === 'typeof') return
|
1396
|
+
if (node.operator === 'typeof') return Prefs.bytestring ? TYPES._bytestring : TYPES.string;
|
1255
1397
|
|
1256
1398
|
return TYPES.number;
|
1257
1399
|
}
|
@@ -1262,15 +1404,21 @@ const getNodeType = (scope, node) => {
|
|
1262
1404
|
|
1263
1405
|
// ts hack
|
1264
1406
|
if (scope.locals[node.object.name]?.metadata?.type === TYPES.string) return TYPES.string;
|
1407
|
+
if (scope.locals[node.object.name]?.metadata?.type === TYPES._bytestring) return TYPES._bytestring;
|
1265
1408
|
if (scope.locals[node.object.name]?.metadata?.type === TYPES._array) return TYPES.number;
|
1266
1409
|
|
1267
|
-
if (scope.locals['#last_type']) return
|
1410
|
+
if (scope.locals['#last_type']) return getLastType(scope);
|
1268
1411
|
|
1269
1412
|
// presume
|
1270
1413
|
return TYPES.number;
|
1271
1414
|
}
|
1272
1415
|
|
1273
|
-
if (
|
1416
|
+
if (node.type === 'TaggedTemplateExpression') {
|
1417
|
+
// hack
|
1418
|
+
if (node.tag.name.startsWith('__Porffor_')) return TYPES.number;
|
1419
|
+
}
|
1420
|
+
|
1421
|
+
if (scope.locals['#last_type']) return getLastType(scope);
|
1274
1422
|
|
1275
1423
|
// presume
|
1276
1424
|
// todo: warn here?
|
@@ -1303,7 +1451,7 @@ const generateLiteral = (scope, decl, global, name) => {
|
|
1303
1451
|
return makeString(scope, decl.value, global, name);
|
1304
1452
|
|
1305
1453
|
default:
|
1306
|
-
return todo(`cannot generate literal of type ${typeof decl.value}
|
1454
|
+
return todo(scope, `cannot generate literal of type ${typeof decl.value}`, true);
|
1307
1455
|
}
|
1308
1456
|
};
|
1309
1457
|
|
@@ -1312,6 +1460,8 @@ const countLeftover = wasm => {
|
|
1312
1460
|
|
1313
1461
|
for (let i = 0; i < wasm.length; i++) {
|
1314
1462
|
const inst = wasm[i];
|
1463
|
+
if (inst[0] == null) continue;
|
1464
|
+
|
1315
1465
|
if (depth === 0 && (inst[0] === Opcodes.if || inst[0] === Opcodes.block || inst[0] === Opcodes.loop)) {
|
1316
1466
|
if (inst[0] === Opcodes.if) count--;
|
1317
1467
|
if (inst[1] !== Blocktype.void) count++;
|
@@ -1320,18 +1470,25 @@ const countLeftover = wasm => {
|
|
1320
1470
|
if (inst[0] === Opcodes.end) depth--;
|
1321
1471
|
|
1322
1472
|
if (depth === 0)
|
1323
|
-
if ([Opcodes.throw,Opcodes.drop, Opcodes.local_set, Opcodes.global_set].includes(inst[0])) count--;
|
1473
|
+
if ([Opcodes.throw, Opcodes.drop, Opcodes.local_set, Opcodes.global_set].includes(inst[0])) count--;
|
1324
1474
|
else if ([null, Opcodes.i32_eqz, Opcodes.i64_eqz, Opcodes.f64_ceil, Opcodes.f64_floor, Opcodes.f64_trunc, Opcodes.f64_nearest, Opcodes.f64_sqrt, Opcodes.local_tee, Opcodes.i32_wrap_i64, Opcodes.i64_extend_i32_s, Opcodes.i64_extend_i32_u, Opcodes.f32_demote_f64, Opcodes.f64_promote_f32, Opcodes.f64_convert_i32_s, Opcodes.f64_convert_i32_u, Opcodes.i32_clz, Opcodes.i32_ctz, Opcodes.i32_popcnt, Opcodes.f64_neg, Opcodes.end, Opcodes.i32_trunc_sat_f64_s[0], Opcodes.i32x4_extract_lane, Opcodes.i16x8_extract_lane, Opcodes.i32_load, Opcodes.i64_load, Opcodes.f64_load, Opcodes.v128_load, Opcodes.i32_load16_u, Opcodes.i32_load16_s, Opcodes.i32_load8_u, Opcodes.i32_load8_s, Opcodes.memory_grow].includes(inst[0]) && (inst[0] !== 0xfc || inst[1] < 0x0a)) {}
|
1325
|
-
else if ([Opcodes.local_get, Opcodes.global_get, Opcodes.f64_const, Opcodes.i32_const, Opcodes.i64_const, Opcodes.v128_const].includes(inst[0])) count++;
|
1475
|
+
else if ([Opcodes.local_get, Opcodes.global_get, Opcodes.f64_const, Opcodes.i32_const, Opcodes.i64_const, Opcodes.v128_const, Opcodes.memory_size].includes(inst[0])) count++;
|
1326
1476
|
else if ([Opcodes.i32_store, Opcodes.i64_store, Opcodes.f64_store, Opcodes.i32_store16, Opcodes.i32_store8].includes(inst[0])) count -= 2;
|
1327
1477
|
else if (Opcodes.memory_copy[0] === inst[0] && Opcodes.memory_copy[1] === inst[1]) count -= 3;
|
1328
1478
|
else if (inst[0] === Opcodes.return) count = 0;
|
1329
1479
|
else if (inst[0] === Opcodes.call) {
|
1330
1480
|
let func = funcs.find(x => x.index === inst[1]);
|
1331
|
-
if (
|
1332
|
-
count
|
1333
|
-
} else
|
1334
|
-
|
1481
|
+
if (inst[1] === -1) {
|
1482
|
+
// todo: count for calling self
|
1483
|
+
} else if (!func && inst[1] < importedFuncs.length) {
|
1484
|
+
count -= importedFuncs[inst[1]].params;
|
1485
|
+
count += importedFuncs[inst[1]].returns;
|
1486
|
+
} else {
|
1487
|
+
if (func) {
|
1488
|
+
count -= func.params.length;
|
1489
|
+
} else count--;
|
1490
|
+
if (func) count += func.returns.length;
|
1491
|
+
}
|
1335
1492
|
} else count--;
|
1336
1493
|
|
1337
1494
|
// console.log(count, decompile([ inst ]).slice(0, -1));
|
@@ -1423,10 +1580,21 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1423
1580
|
name = func.name;
|
1424
1581
|
}
|
1425
1582
|
|
1426
|
-
if (name === 'eval' && decl.arguments[0]
|
1583
|
+
if (name === 'eval' && decl.arguments[0]?.type === 'Literal') {
|
1427
1584
|
// literal eval hack
|
1428
|
-
const code = decl.arguments[0]
|
1429
|
-
|
1585
|
+
const code = decl.arguments[0]?.value ?? '';
|
1586
|
+
|
1587
|
+
let parsed;
|
1588
|
+
try {
|
1589
|
+
parsed = parse(code, []);
|
1590
|
+
} catch (e) {
|
1591
|
+
if (e.name === 'SyntaxError') {
|
1592
|
+
// throw syntax errors of evals at runtime instead
|
1593
|
+
return internalThrow(scope, 'SyntaxError', e.message, true);
|
1594
|
+
}
|
1595
|
+
|
1596
|
+
throw e;
|
1597
|
+
}
|
1430
1598
|
|
1431
1599
|
const out = generate(scope, {
|
1432
1600
|
type: 'BlockStatement',
|
@@ -1440,13 +1608,13 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1440
1608
|
const finalStatement = parsed.body[parsed.body.length - 1];
|
1441
1609
|
out.push(
|
1442
1610
|
...getNodeType(scope, finalStatement),
|
1443
|
-
setLastType(scope)
|
1611
|
+
...setLastType(scope)
|
1444
1612
|
);
|
1445
1613
|
} else if (countLeftover(out) === 0) {
|
1446
1614
|
out.push(...number(UNDEFINED));
|
1447
1615
|
out.push(
|
1448
1616
|
...number(TYPES.undefined, Valtype.i32),
|
1449
|
-
setLastType(scope)
|
1617
|
+
...setLastType(scope)
|
1450
1618
|
);
|
1451
1619
|
}
|
1452
1620
|
|
@@ -1468,6 +1636,9 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1468
1636
|
|
1469
1637
|
target = { ...decl.callee };
|
1470
1638
|
target.name = spl.slice(0, -1).join('_');
|
1639
|
+
|
1640
|
+
// failed to lookup name, abort
|
1641
|
+
if (!lookupName(scope, target.name)[0]) protoName = null;
|
1471
1642
|
}
|
1472
1643
|
|
1473
1644
|
// literal.func()
|
@@ -1475,22 +1646,29 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1475
1646
|
// megahack for /regex/.func()
|
1476
1647
|
const funcName = decl.callee.property.name;
|
1477
1648
|
if (decl.callee.object.regex && Object.hasOwn(Rhemyn, funcName)) {
|
1478
|
-
const
|
1649
|
+
const regex = decl.callee.object.regex.pattern;
|
1650
|
+
const rhemynName = `regex_${funcName}_${regex}`;
|
1479
1651
|
|
1480
|
-
funcIndex[
|
1481
|
-
|
1652
|
+
if (!funcIndex[rhemynName]) {
|
1653
|
+
const func = Rhemyn[funcName](regex, currentFuncIndex++, rhemynName);
|
1482
1654
|
|
1655
|
+
funcIndex[func.name] = func.index;
|
1656
|
+
funcs.push(func);
|
1657
|
+
}
|
1658
|
+
|
1659
|
+
const idx = funcIndex[rhemynName];
|
1483
1660
|
return [
|
1484
1661
|
// make string arg
|
1485
1662
|
...generate(scope, decl.arguments[0]),
|
1663
|
+
Opcodes.i32_to_u,
|
1664
|
+
...getNodeType(scope, decl.arguments[0]),
|
1486
1665
|
|
1487
1666
|
// call regex func
|
1488
|
-
Opcodes.
|
1489
|
-
[ Opcodes.call, func.index ],
|
1667
|
+
[ Opcodes.call, idx ],
|
1490
1668
|
Opcodes.i32_from_u,
|
1491
1669
|
|
1492
1670
|
...number(TYPES.boolean, Valtype.i32),
|
1493
|
-
setLastType(scope)
|
1671
|
+
...setLastType(scope)
|
1494
1672
|
];
|
1495
1673
|
}
|
1496
1674
|
|
@@ -1515,12 +1693,31 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1515
1693
|
// }
|
1516
1694
|
|
1517
1695
|
if (protoName) {
|
1696
|
+
const protoBC = {};
|
1697
|
+
|
1698
|
+
const builtinProtoCands = Object.keys(builtinFuncs).filter(x => x.startsWith('__') && x.endsWith('_prototype_' + protoName));
|
1699
|
+
|
1700
|
+
if (!decl._protoInternalCall && builtinProtoCands.length > 0) {
|
1701
|
+
for (const x of builtinProtoCands) {
|
1702
|
+
const type = TYPES[x.split('_prototype_')[0].slice(2).toLowerCase()];
|
1703
|
+
if (type == null) continue;
|
1704
|
+
|
1705
|
+
protoBC[type] = generateCall(scope, {
|
1706
|
+
callee: {
|
1707
|
+
type: 'Identifier',
|
1708
|
+
name: x
|
1709
|
+
},
|
1710
|
+
arguments: [ target, ...decl.arguments ],
|
1711
|
+
_protoInternalCall: true
|
1712
|
+
});
|
1713
|
+
}
|
1714
|
+
}
|
1715
|
+
|
1518
1716
|
const protoCands = Object.keys(prototypeFuncs).reduce((acc, x) => {
|
1519
1717
|
if (Object.hasOwn(prototypeFuncs[x], protoName)) acc[x] = prototypeFuncs[x][protoName];
|
1520
1718
|
return acc;
|
1521
1719
|
}, {});
|
1522
1720
|
|
1523
|
-
// no prototype function candidates, ignore
|
1524
1721
|
if (Object.keys(protoCands).length > 0) {
|
1525
1722
|
// use local for cached i32 length as commonly used
|
1526
1723
|
const lengthLocal = localTmp(scope, '__proto_length_cache', Valtype.i32);
|
@@ -1538,7 +1735,6 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1538
1735
|
|
1539
1736
|
let allOptUnused = true;
|
1540
1737
|
let lengthI32CacheUsed = false;
|
1541
|
-
const protoBC = {};
|
1542
1738
|
for (const x in protoCands) {
|
1543
1739
|
const protoFunc = protoCands[x];
|
1544
1740
|
if (protoFunc.noArgRetLength && decl.arguments.length === 0) {
|
@@ -1546,7 +1742,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1546
1742
|
...RTArrayUtil.getLength(getPointer),
|
1547
1743
|
|
1548
1744
|
...number(TYPES.number, Valtype.i32),
|
1549
|
-
setLastType(scope)
|
1745
|
+
...setLastType(scope)
|
1550
1746
|
];
|
1551
1747
|
continue;
|
1552
1748
|
}
|
@@ -1583,7 +1779,7 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1583
1779
|
...protoOut,
|
1584
1780
|
|
1585
1781
|
...number(protoFunc.returnType ?? TYPES.number, Valtype.i32),
|
1586
|
-
setLastType(scope),
|
1782
|
+
...setLastType(scope),
|
1587
1783
|
[ Opcodes.end ]
|
1588
1784
|
];
|
1589
1785
|
}
|
@@ -1609,10 +1805,19 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1609
1805
|
}, allOptUnused && unusedValue ? Blocktype.void : valtypeBinary),
|
1610
1806
|
];
|
1611
1807
|
}
|
1808
|
+
|
1809
|
+
if (Object.keys(protoBC).length > 0) {
|
1810
|
+
return typeSwitch(scope, getNodeType(scope, target), {
|
1811
|
+
...protoBC,
|
1812
|
+
|
1813
|
+
// TODO: error better
|
1814
|
+
default: internalThrow(scope, 'TypeError', `'${protoName}' proto func tried to be called on a type without an impl`)
|
1815
|
+
}, valtypeBinary);
|
1816
|
+
}
|
1612
1817
|
}
|
1613
1818
|
|
1614
1819
|
// TODO: only allows callee as literal
|
1615
|
-
if (!name) return todo(`only literal callees (got ${decl.callee.type})`);
|
1820
|
+
if (!name) return todo(scope, `only literal callees (got ${decl.callee.type})`);
|
1616
1821
|
|
1617
1822
|
let idx = funcIndex[name] ?? importedFuncs[name];
|
1618
1823
|
if (idx === undefined && builtinFuncs[name]) {
|
@@ -1622,20 +1827,20 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1622
1827
|
idx = funcIndex[name];
|
1623
1828
|
|
1624
1829
|
// infer arguments types from builtins params
|
1625
|
-
const func = funcs.find(x => x.name === name);
|
1626
|
-
for (let i = 0; i < decl.arguments.length; i++) {
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
}
|
1830
|
+
// const func = funcs.find(x => x.name === name);
|
1831
|
+
// for (let i = 0; i < decl.arguments.length; i++) {
|
1832
|
+
// const arg = decl.arguments[i];
|
1833
|
+
// if (!arg.name) continue;
|
1834
|
+
|
1835
|
+
// const local = scope.locals[arg.name];
|
1836
|
+
// if (!local) continue;
|
1837
|
+
|
1838
|
+
// local.type = func.params[i];
|
1839
|
+
// if (local.type === Valtype.v128) {
|
1840
|
+
// // specify vec subtype inferred from last vec type in function name
|
1841
|
+
// local.vecType = name.split('_').reverse().find(x => x.includes('x'));
|
1842
|
+
// }
|
1843
|
+
// }
|
1639
1844
|
}
|
1640
1845
|
|
1641
1846
|
if (idx === undefined && internalConstrs[name]) return internalConstrs[name].generate(scope, decl, _global, _name);
|
@@ -1645,16 +1850,65 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1645
1850
|
idx = -1;
|
1646
1851
|
}
|
1647
1852
|
|
1853
|
+
if (idx === undefined && name.startsWith('__Porffor_wasm_')) {
|
1854
|
+
const wasmOps = {
|
1855
|
+
// pointer, align, offset
|
1856
|
+
i32_load: { imms: 2, args: [ true ], returns: 1 },
|
1857
|
+
// pointer, value, align, offset
|
1858
|
+
i32_store: { imms: 2, args: [ true, true ], returns: 0 },
|
1859
|
+
// pointer, align, offset
|
1860
|
+
i32_load8_u: { imms: 2, args: [ true ], returns: 1 },
|
1861
|
+
// pointer, value, align, offset
|
1862
|
+
i32_store8: { imms: 2, args: [ true, true ], returns: 0 },
|
1863
|
+
// pointer, align, offset
|
1864
|
+
i32_load16_u: { imms: 2, args: [ true ], returns: 1 },
|
1865
|
+
// pointer, value, align, offset
|
1866
|
+
i32_store16: { imms: 2, args: [ true, true ], returns: 0 },
|
1867
|
+
|
1868
|
+
// pointer, align, offset
|
1869
|
+
f64_load: { imms: 2, args: [ true ], returns: 0 }, // 0 due to not i32
|
1870
|
+
// pointer, value, align, offset
|
1871
|
+
f64_store: { imms: 2, args: [ true, false ], returns: 0 },
|
1872
|
+
|
1873
|
+
// value
|
1874
|
+
i32_const: { imms: 1, args: [], returns: 1 },
|
1875
|
+
|
1876
|
+
// a, b
|
1877
|
+
i32_or: { imms: 0, args: [ true, true ], returns: 1 },
|
1878
|
+
};
|
1879
|
+
|
1880
|
+
const opName = name.slice('__Porffor_wasm_'.length);
|
1881
|
+
|
1882
|
+
if (wasmOps[opName]) {
|
1883
|
+
const op = wasmOps[opName];
|
1884
|
+
|
1885
|
+
const argOut = [];
|
1886
|
+
for (let i = 0; i < op.args.length; i++) argOut.push(
|
1887
|
+
...generate(scope, decl.arguments[i]),
|
1888
|
+
...(op.args[i] ? [ Opcodes.i32_to ] : [])
|
1889
|
+
);
|
1890
|
+
|
1891
|
+
// literals only
|
1892
|
+
const imms = decl.arguments.slice(op.args.length).map(x => x.value);
|
1893
|
+
|
1894
|
+
return [
|
1895
|
+
...argOut,
|
1896
|
+
[ Opcodes[opName], ...imms ],
|
1897
|
+
...(new Array(op.returns).fill(Opcodes.i32_from))
|
1898
|
+
];
|
1899
|
+
}
|
1900
|
+
}
|
1901
|
+
|
1648
1902
|
if (idx === undefined) {
|
1649
|
-
if (scope.locals[name] !== undefined || globals[name] !== undefined || builtinVars[name] !== undefined) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function
|
1650
|
-
return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined
|
1903
|
+
if (scope.locals[name] !== undefined || globals[name] !== undefined || builtinVars[name] !== undefined) return internalThrow(scope, 'TypeError', `${unhackName(name)} is not a function`, true);
|
1904
|
+
return internalThrow(scope, 'ReferenceError', `${unhackName(name)} is not defined`, true);
|
1651
1905
|
}
|
1652
1906
|
|
1653
1907
|
const func = funcs.find(x => x.index === idx);
|
1654
1908
|
|
1655
1909
|
const userFunc = (funcIndex[name] && !importedFuncs[name] && !builtinFuncs[name] && !internalConstrs[name]) || idx === -1;
|
1656
1910
|
const typedParams = userFunc || builtinFuncs[name]?.typedParams;
|
1657
|
-
const
|
1911
|
+
const typedReturns = (func ? func.returnType == null : userFunc) || builtinFuncs[name]?.typedReturns;
|
1658
1912
|
const paramCount = func && (typedParams ? func.params.length / 2 : func.params.length);
|
1659
1913
|
|
1660
1914
|
let args = decl.arguments;
|
@@ -1671,14 +1925,24 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1671
1925
|
if (func && func.throws) scope.throws = true;
|
1672
1926
|
|
1673
1927
|
let out = [];
|
1674
|
-
for (
|
1928
|
+
for (let i = 0; i < args.length; i++) {
|
1929
|
+
const arg = args[i];
|
1675
1930
|
out = out.concat(generate(scope, arg));
|
1931
|
+
|
1932
|
+
if (builtinFuncs[name] && builtinFuncs[name].params[i * (typedParams ? 2 : 1)] === Valtype.i32 && valtypeBinary !== Valtype.i32) {
|
1933
|
+
out.push(Opcodes.i32_to);
|
1934
|
+
}
|
1935
|
+
|
1936
|
+
if (importedFuncs[name] && name.startsWith('profile')) {
|
1937
|
+
out.push(Opcodes.i32_to);
|
1938
|
+
}
|
1939
|
+
|
1676
1940
|
if (typedParams) out = out.concat(getNodeType(scope, arg));
|
1677
1941
|
}
|
1678
1942
|
|
1679
1943
|
out.push([ Opcodes.call, idx ]);
|
1680
1944
|
|
1681
|
-
if (!
|
1945
|
+
if (!typedReturns) {
|
1682
1946
|
// let type;
|
1683
1947
|
// if (builtinFuncs[name]) type = TYPES[builtinFuncs[name].returnType ?? 'number'];
|
1684
1948
|
// if (internalConstrs[name]) type = internalConstrs[name].type;
|
@@ -1688,7 +1952,11 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1688
1952
|
// ...number(type, Valtype.i32),
|
1689
1953
|
// [ Opcodes.local_set, localTmp(scope, '#last_type', Valtype.i32) ]
|
1690
1954
|
// );
|
1691
|
-
} else out.push(setLastType(scope));
|
1955
|
+
} else out.push(...setLastType(scope));
|
1956
|
+
|
1957
|
+
if (builtinFuncs[name] && builtinFuncs[name].returns?.[0] === Valtype.i32 && valtypeBinary !== Valtype.i32) {
|
1958
|
+
out.push(Opcodes.i32_from);
|
1959
|
+
}
|
1692
1960
|
|
1693
1961
|
return out;
|
1694
1962
|
};
|
@@ -1696,8 +1964,21 @@ const generateCall = (scope, decl, _global, _name, unusedValue = false) => {
|
|
1696
1964
|
const generateNew = (scope, decl, _global, _name) => {
|
1697
1965
|
// hack: basically treat this as a normal call for builtins for now
|
1698
1966
|
const name = mapName(decl.callee.name);
|
1967
|
+
|
1699
1968
|
if (internalConstrs[name] && !internalConstrs[name].notConstr) return internalConstrs[name].generate(scope, decl, _global, _name);
|
1700
|
-
|
1969
|
+
|
1970
|
+
if (builtinFuncs[name + '$constructor']) {
|
1971
|
+
// custom ...$constructor override builtin func
|
1972
|
+
return generateCall(scope, {
|
1973
|
+
...decl,
|
1974
|
+
callee: {
|
1975
|
+
type: 'Identifier',
|
1976
|
+
name: name + '$constructor'
|
1977
|
+
}
|
1978
|
+
}, _global, _name);
|
1979
|
+
}
|
1980
|
+
|
1981
|
+
if (!builtinFuncs[name]) return todo(scope, `new statement is not supported yet`); // return todo(scope, `new statement is not supported yet (new ${unhackName(name)})`);
|
1701
1982
|
|
1702
1983
|
return generateCall(scope, decl, _global, _name);
|
1703
1984
|
};
|
@@ -1814,14 +2095,14 @@ const brTable = (input, bc, returns) => {
|
|
1814
2095
|
};
|
1815
2096
|
|
1816
2097
|
const typeSwitch = (scope, type, bc, returns = valtypeBinary) => {
|
1817
|
-
if (!
|
2098
|
+
if (!Prefs.bytestring) delete bc[TYPES._bytestring];
|
1818
2099
|
|
1819
2100
|
const known = knownType(scope, type);
|
1820
2101
|
if (known != null) {
|
1821
2102
|
return bc[known] ?? bc.default;
|
1822
2103
|
}
|
1823
2104
|
|
1824
|
-
if (
|
2105
|
+
if (Prefs.typeswitchUseBrtable)
|
1825
2106
|
return brTable(type, bc, returns);
|
1826
2107
|
|
1827
2108
|
const tmp = localTmp(scope, '#typeswitch_tmp', Valtype.i32);
|
@@ -1831,8 +2112,6 @@ const typeSwitch = (scope, type, bc, returns = valtypeBinary) => {
|
|
1831
2112
|
[ Opcodes.block, returns ]
|
1832
2113
|
];
|
1833
2114
|
|
1834
|
-
// todo: use br_table?
|
1835
|
-
|
1836
2115
|
for (const x in bc) {
|
1837
2116
|
if (x === 'default') continue;
|
1838
2117
|
|
@@ -1856,7 +2135,7 @@ const typeSwitch = (scope, type, bc, returns = valtypeBinary) => {
|
|
1856
2135
|
return out;
|
1857
2136
|
};
|
1858
2137
|
|
1859
|
-
const allocVar = (scope, name, global = false) => {
|
2138
|
+
const allocVar = (scope, name, global = false, type = true) => {
|
1860
2139
|
const target = global ? globals : scope.locals;
|
1861
2140
|
|
1862
2141
|
// already declared
|
@@ -1870,8 +2149,10 @@ const allocVar = (scope, name, global = false) => {
|
|
1870
2149
|
let idx = global ? globalInd++ : scope.localInd++;
|
1871
2150
|
target[name] = { idx, type: valtypeBinary };
|
1872
2151
|
|
1873
|
-
|
1874
|
-
|
2152
|
+
if (type) {
|
2153
|
+
let typeIdx = global ? globalInd++ : scope.localInd++;
|
2154
|
+
target[name + '#type'] = { idx: typeIdx, type: Valtype.i32 };
|
2155
|
+
}
|
1875
2156
|
|
1876
2157
|
return idx;
|
1877
2158
|
};
|
@@ -1886,11 +2167,14 @@ const addVarMetadata = (scope, name, global = false, metadata = {}) => {
|
|
1886
2167
|
};
|
1887
2168
|
|
1888
2169
|
const typeAnnoToPorfType = x => {
|
1889
|
-
if (
|
1890
|
-
if (TYPES[
|
2170
|
+
if (!x) return null;
|
2171
|
+
if (TYPES[x.toLowerCase()] != null) return TYPES[x.toLowerCase()];
|
2172
|
+
if (TYPES['_' + x.toLowerCase()] != null) return TYPES['_' + x.toLowerCase()];
|
1891
2173
|
|
1892
2174
|
switch (x) {
|
1893
2175
|
case 'i32':
|
2176
|
+
case 'i64':
|
2177
|
+
case 'f64':
|
1894
2178
|
return TYPES.number;
|
1895
2179
|
}
|
1896
2180
|
|
@@ -1901,7 +2185,7 @@ const extractTypeAnnotation = decl => {
|
|
1901
2185
|
let a = decl;
|
1902
2186
|
while (a.typeAnnotation) a = a.typeAnnotation;
|
1903
2187
|
|
1904
|
-
let type, elementType;
|
2188
|
+
let type = null, elementType = null;
|
1905
2189
|
if (a.typeName) {
|
1906
2190
|
type = a.typeName.name;
|
1907
2191
|
} else if (a.type.endsWith('Keyword')) {
|
@@ -1914,7 +2198,7 @@ const extractTypeAnnotation = decl => {
|
|
1914
2198
|
const typeName = type;
|
1915
2199
|
type = typeAnnoToPorfType(type);
|
1916
2200
|
|
1917
|
-
if (type === TYPES._bytestring && !
|
2201
|
+
if (type === TYPES._bytestring && !Prefs.bytestring) type = TYPES.string;
|
1918
2202
|
|
1919
2203
|
// if (decl.name) console.log(decl.name, { type, elementType });
|
1920
2204
|
|
@@ -1928,11 +2212,12 @@ const generateVar = (scope, decl) => {
|
|
1928
2212
|
|
1929
2213
|
// global variable if in top scope (main) and var ..., or if wanted
|
1930
2214
|
const global = topLevel || decl._bare; // decl.kind === 'var';
|
2215
|
+
const target = global ? globals : scope.locals;
|
1931
2216
|
|
1932
2217
|
for (const x of decl.declarations) {
|
1933
2218
|
const name = mapName(x.id.name);
|
1934
2219
|
|
1935
|
-
if (!name) return todo('destructuring is not supported yet');
|
2220
|
+
if (!name) return todo(scope, 'destructuring is not supported yet');
|
1936
2221
|
|
1937
2222
|
if (x.init && isFuncType(x.init.type)) {
|
1938
2223
|
// hack for let a = function () { ... }
|
@@ -1949,16 +2234,29 @@ const generateVar = (scope, decl) => {
|
|
1949
2234
|
continue; // always ignore
|
1950
2235
|
}
|
1951
2236
|
|
1952
|
-
|
2237
|
+
// // generate init before allocating var
|
2238
|
+
// let generated;
|
2239
|
+
// if (x.init) generated = generate(scope, x.init, global, name);
|
2240
|
+
|
2241
|
+
const typed = typedInput && x.id.typeAnnotation;
|
2242
|
+
let idx = allocVar(scope, name, global, !(typed && extractTypeAnnotation(x.id).type != null));
|
1953
2243
|
|
1954
|
-
if (
|
2244
|
+
if (typed) {
|
1955
2245
|
addVarMetadata(scope, name, global, extractTypeAnnotation(x.id));
|
1956
2246
|
}
|
1957
2247
|
|
1958
2248
|
if (x.init) {
|
1959
|
-
|
1960
|
-
|
1961
|
-
|
2249
|
+
const generated = generate(scope, x.init, global, name);
|
2250
|
+
if (scope.arrays?.get(name) != null) {
|
2251
|
+
// hack to set local as pointer before
|
2252
|
+
out.push(...number(scope.arrays.get(name)), [ global ? Opcodes.global_set : Opcodes.local_set, idx ]);
|
2253
|
+
if (generated.at(-1) == Opcodes.i32_from_u) generated.pop();
|
2254
|
+
generated.pop();
|
2255
|
+
out = out.concat(generated);
|
2256
|
+
} else {
|
2257
|
+
out = out.concat(generated);
|
2258
|
+
out.push([ global ? Opcodes.global_set : Opcodes.local_set, idx ]);
|
2259
|
+
}
|
1962
2260
|
out.push(...setType(scope, name, getNodeType(scope, x.init)));
|
1963
2261
|
}
|
1964
2262
|
|
@@ -1969,7 +2267,8 @@ const generateVar = (scope, decl) => {
|
|
1969
2267
|
return out;
|
1970
2268
|
};
|
1971
2269
|
|
1972
|
-
|
2270
|
+
// todo: optimize this func for valueUnused
|
2271
|
+
const generateAssign = (scope, decl, _global, _name, valueUnused = false) => {
|
1973
2272
|
const { type, name } = decl.left;
|
1974
2273
|
|
1975
2274
|
if (type === 'ObjectPattern') {
|
@@ -1984,22 +2283,30 @@ const generateAssign = (scope, decl) => {
|
|
1984
2283
|
return [];
|
1985
2284
|
}
|
1986
2285
|
|
2286
|
+
const op = decl.operator.slice(0, -1) || '=';
|
2287
|
+
|
1987
2288
|
// hack: .length setter
|
1988
2289
|
if (decl.left.type === 'MemberExpression' && decl.left.property.name === 'length') {
|
1989
2290
|
const name = decl.left.object.name;
|
1990
|
-
const pointer = arrays
|
2291
|
+
const pointer = scope.arrays?.get(name);
|
1991
2292
|
|
1992
|
-
const aotPointer = pointer != null;
|
2293
|
+
const aotPointer = Prefs.aotPointerOpt && pointer != null;
|
1993
2294
|
|
1994
2295
|
const newValueTmp = localTmp(scope, '__length_setter_tmp');
|
2296
|
+
const pointerTmp = op === '=' ? null : localTmp(scope, '__member_setter_ptr_tmp', Valtype.i32);
|
1995
2297
|
|
1996
2298
|
return [
|
1997
2299
|
...(aotPointer ? number(0, Valtype.i32) : [
|
1998
2300
|
...generate(scope, decl.left.object),
|
1999
2301
|
Opcodes.i32_to_u
|
2000
2302
|
]),
|
2303
|
+
...(!pointerTmp ? [] : [ [ Opcodes.local_tee, pointerTmp ] ]),
|
2001
2304
|
|
2002
|
-
...generate(scope, decl.right),
|
2305
|
+
...(op === '=' ? generate(scope, decl.right) : performOp(scope, op, [
|
2306
|
+
[ Opcodes.local_get, pointerTmp ],
|
2307
|
+
[ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
|
2308
|
+
Opcodes.i32_from_u
|
2309
|
+
], generate(scope, decl.right), number(TYPES.number, Valtype.i32), getNodeType(scope, decl.right))),
|
2003
2310
|
[ Opcodes.local_tee, newValueTmp ],
|
2004
2311
|
|
2005
2312
|
Opcodes.i32_to_u,
|
@@ -2009,14 +2316,12 @@ const generateAssign = (scope, decl) => {
|
|
2009
2316
|
];
|
2010
2317
|
}
|
2011
2318
|
|
2012
|
-
const op = decl.operator.slice(0, -1) || '=';
|
2013
|
-
|
2014
2319
|
// arr[i]
|
2015
2320
|
if (decl.left.type === 'MemberExpression' && decl.left.computed) {
|
2016
2321
|
const name = decl.left.object.name;
|
2017
|
-
const pointer = arrays
|
2322
|
+
const pointer = scope.arrays?.get(name);
|
2018
2323
|
|
2019
|
-
const aotPointer = pointer != null;
|
2324
|
+
const aotPointer = Prefs.aotPointerOpt && pointer != null;
|
2020
2325
|
|
2021
2326
|
const newValueTmp = localTmp(scope, '__member_setter_val_tmp');
|
2022
2327
|
const pointerTmp = op === '=' ? -1 : localTmp(scope, '__member_setter_ptr_tmp', Valtype.i32);
|
@@ -2072,7 +2377,7 @@ const generateAssign = (scope, decl) => {
|
|
2072
2377
|
];
|
2073
2378
|
}
|
2074
2379
|
|
2075
|
-
if (!name) return todo('destructuring is not supported yet');
|
2380
|
+
if (!name) return todo(scope, 'destructuring is not supported yet', true);
|
2076
2381
|
|
2077
2382
|
const [ local, isGlobal ] = lookupName(scope, name);
|
2078
2383
|
|
@@ -2120,9 +2425,7 @@ const generateAssign = (scope, decl) => {
|
|
2120
2425
|
], getType(scope, name), getNodeType(scope, decl.right), isGlobal, name, true),
|
2121
2426
|
[ isGlobal ? Opcodes.global_get : Opcodes.local_get, local.idx ],
|
2122
2427
|
|
2123
|
-
getLastType(scope)
|
2124
|
-
// hack: type is idx+1
|
2125
|
-
[ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx + 1 ],
|
2428
|
+
...setType(scope, name, getLastType(scope))
|
2126
2429
|
];
|
2127
2430
|
}
|
2128
2431
|
|
@@ -2133,9 +2436,7 @@ const generateAssign = (scope, decl) => {
|
|
2133
2436
|
|
2134
2437
|
// todo: string concat types
|
2135
2438
|
|
2136
|
-
|
2137
|
-
...number(TYPES.number, Valtype.i32),
|
2138
|
-
[ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx + 1 ],
|
2439
|
+
...setType(scope, name, TYPES.number)
|
2139
2440
|
];
|
2140
2441
|
};
|
2141
2442
|
|
@@ -2181,7 +2482,7 @@ const generateUnary = (scope, decl) => {
|
|
2181
2482
|
return out;
|
2182
2483
|
}
|
2183
2484
|
|
2184
|
-
case 'delete':
|
2485
|
+
case 'delete': {
|
2185
2486
|
let toReturn = true, toGenerate = true;
|
2186
2487
|
|
2187
2488
|
if (decl.argument.type === 'Identifier') {
|
@@ -2203,9 +2504,26 @@ const generateUnary = (scope, decl) => {
|
|
2203
2504
|
|
2204
2505
|
out.push(...number(toReturn ? 1 : 0));
|
2205
2506
|
return out;
|
2507
|
+
}
|
2206
2508
|
|
2207
|
-
case 'typeof':
|
2208
|
-
|
2509
|
+
case 'typeof': {
|
2510
|
+
let overrideType, toGenerate = true;
|
2511
|
+
|
2512
|
+
if (decl.argument.type === 'Identifier') {
|
2513
|
+
const out = generateIdent(scope, decl.argument);
|
2514
|
+
|
2515
|
+
// if ReferenceError (undeclared var), ignore and return undefined
|
2516
|
+
if (out[1]) {
|
2517
|
+
// does not exist (2 ops from throw)
|
2518
|
+
overrideType = number(TYPES.undefined, Valtype.i32);
|
2519
|
+
toGenerate = false;
|
2520
|
+
}
|
2521
|
+
}
|
2522
|
+
|
2523
|
+
const out = toGenerate ? generate(scope, decl.argument) : [];
|
2524
|
+
disposeLeftover(out);
|
2525
|
+
|
2526
|
+
out.push(...typeSwitch(scope, overrideType ?? getNodeType(scope, decl.argument), {
|
2209
2527
|
[TYPES.number]: makeString(scope, 'number', false, '#typeof_result'),
|
2210
2528
|
[TYPES.boolean]: makeString(scope, 'boolean', false, '#typeof_result'),
|
2211
2529
|
[TYPES.string]: makeString(scope, 'string', false, '#typeof_result'),
|
@@ -2216,27 +2534,30 @@ const generateUnary = (scope, decl) => {
|
|
2216
2534
|
|
2217
2535
|
// object and internal types
|
2218
2536
|
default: makeString(scope, 'object', false, '#typeof_result'),
|
2219
|
-
});
|
2537
|
+
}));
|
2538
|
+
|
2539
|
+
return out;
|
2540
|
+
}
|
2220
2541
|
|
2221
2542
|
default:
|
2222
|
-
return todo(`unary operator ${decl.operator} not implemented yet
|
2543
|
+
return todo(scope, `unary operator ${decl.operator} not implemented yet`, true);
|
2223
2544
|
}
|
2224
2545
|
};
|
2225
2546
|
|
2226
|
-
const generateUpdate = (scope, decl) => {
|
2547
|
+
const generateUpdate = (scope, decl, _global, _name, valueUnused = false) => {
|
2227
2548
|
const { name } = decl.argument;
|
2228
2549
|
|
2229
2550
|
const [ local, isGlobal ] = lookupName(scope, name);
|
2230
2551
|
|
2231
2552
|
if (local === undefined) {
|
2232
|
-
return todo(`update expression with undefined variable
|
2553
|
+
return todo(scope, `update expression with undefined variable`, true);
|
2233
2554
|
}
|
2234
2555
|
|
2235
2556
|
const idx = local.idx;
|
2236
2557
|
const out = [];
|
2237
2558
|
|
2238
2559
|
out.push([ isGlobal ? Opcodes.global_get : Opcodes.local_get, idx ]);
|
2239
|
-
if (!decl.prefix) out.push([ isGlobal ? Opcodes.global_get : Opcodes.local_get, idx ]);
|
2560
|
+
if (!decl.prefix && !valueUnused) out.push([ isGlobal ? Opcodes.global_get : Opcodes.local_get, idx ]);
|
2240
2561
|
|
2241
2562
|
switch (decl.operator) {
|
2242
2563
|
case '++':
|
@@ -2249,7 +2570,7 @@ const generateUpdate = (scope, decl) => {
|
|
2249
2570
|
}
|
2250
2571
|
|
2251
2572
|
out.push([ isGlobal ? Opcodes.global_set : Opcodes.local_set, idx ]);
|
2252
|
-
if (decl.prefix) out.push([ isGlobal ? Opcodes.global_get : Opcodes.local_get, idx ]);
|
2573
|
+
if (decl.prefix && !valueUnused) out.push([ isGlobal ? Opcodes.global_get : Opcodes.local_get, idx ]);
|
2253
2574
|
|
2254
2575
|
return out;
|
2255
2576
|
};
|
@@ -2289,7 +2610,7 @@ const generateConditional = (scope, decl) => {
|
|
2289
2610
|
// note type
|
2290
2611
|
out.push(
|
2291
2612
|
...getNodeType(scope, decl.consequent),
|
2292
|
-
setLastType(scope)
|
2613
|
+
...setLastType(scope)
|
2293
2614
|
);
|
2294
2615
|
|
2295
2616
|
out.push([ Opcodes.else ]);
|
@@ -2298,7 +2619,7 @@ const generateConditional = (scope, decl) => {
|
|
2298
2619
|
// note type
|
2299
2620
|
out.push(
|
2300
2621
|
...getNodeType(scope, decl.alternate),
|
2301
|
-
setLastType(scope)
|
2622
|
+
...setLastType(scope)
|
2302
2623
|
);
|
2303
2624
|
|
2304
2625
|
out.push([ Opcodes.end ]);
|
@@ -2312,7 +2633,7 @@ const generateFor = (scope, decl) => {
|
|
2312
2633
|
const out = [];
|
2313
2634
|
|
2314
2635
|
if (decl.init) {
|
2315
|
-
out.push(...generate(scope, decl.init));
|
2636
|
+
out.push(...generate(scope, decl.init, false, undefined, true));
|
2316
2637
|
disposeLeftover(out);
|
2317
2638
|
}
|
2318
2639
|
|
@@ -2330,7 +2651,7 @@ const generateFor = (scope, decl) => {
|
|
2330
2651
|
out.push(...generate(scope, decl.body));
|
2331
2652
|
out.push([ Opcodes.end ]);
|
2332
2653
|
|
2333
|
-
if (decl.update) out.push(...generate(scope, decl.update));
|
2654
|
+
if (decl.update) out.push(...generate(scope, decl.update, false, undefined, true));
|
2334
2655
|
|
2335
2656
|
out.push([ Opcodes.br, 1 ]);
|
2336
2657
|
out.push([ Opcodes.end ], [ Opcodes.end ]);
|
@@ -2358,6 +2679,36 @@ const generateWhile = (scope, decl) => {
|
|
2358
2679
|
return out;
|
2359
2680
|
};
|
2360
2681
|
|
2682
|
+
const generateDoWhile = (scope, decl) => {
|
2683
|
+
const out = [];
|
2684
|
+
|
2685
|
+
out.push([ Opcodes.loop, Blocktype.void ]);
|
2686
|
+
depth.push('dowhile');
|
2687
|
+
|
2688
|
+
// block for break (includes all)
|
2689
|
+
out.push([ Opcodes.block, Blocktype.void ]);
|
2690
|
+
depth.push('block');
|
2691
|
+
|
2692
|
+
// block for continue
|
2693
|
+
// includes body but not test+loop so we can exit body at anytime
|
2694
|
+
// and still test+loop after
|
2695
|
+
out.push([ Opcodes.block, Blocktype.void ]);
|
2696
|
+
depth.push('block');
|
2697
|
+
|
2698
|
+
out.push(...generate(scope, decl.body));
|
2699
|
+
|
2700
|
+
out.push([ Opcodes.end ]);
|
2701
|
+
depth.pop();
|
2702
|
+
|
2703
|
+
out.push(...generate(scope, decl.test), Opcodes.i32_to);
|
2704
|
+
out.push([ Opcodes.br_if, 1 ]);
|
2705
|
+
|
2706
|
+
out.push([ Opcodes.end ], [ Opcodes.end ]);
|
2707
|
+
depth.pop(); depth.pop();
|
2708
|
+
|
2709
|
+
return out;
|
2710
|
+
};
|
2711
|
+
|
2361
2712
|
const generateForOf = (scope, decl) => {
|
2362
2713
|
const out = [];
|
2363
2714
|
|
@@ -2394,7 +2745,10 @@ const generateForOf = (scope, decl) => {
|
|
2394
2745
|
generateVar(scope, { kind: 'var', _bare: true, declarations: [ { id: { name: leftName } } ] })
|
2395
2746
|
}
|
2396
2747
|
|
2748
|
+
// if (!leftName) console.log(decl.left?.declarations?.[0]?.id ?? decl.left);
|
2749
|
+
|
2397
2750
|
const [ local, isGlobal ] = lookupName(scope, leftName);
|
2751
|
+
if (!local) return todo(scope, 'for of failed to get left local (probably destructure)');
|
2398
2752
|
|
2399
2753
|
depth.push('block');
|
2400
2754
|
depth.push('block');
|
@@ -2403,6 +2757,7 @@ const generateForOf = (scope, decl) => {
|
|
2403
2757
|
// hack: this is naughty and will break things!
|
2404
2758
|
let newOut = number(0, Valtype.f64), newPointer = -1;
|
2405
2759
|
if (pages.hasAnyString) {
|
2760
|
+
// todo: we use i16 even for bytestrings which should not make a bad thing happen, just be confusing for debugging?
|
2406
2761
|
0, [ newOut, newPointer ] = makeArray(scope, {
|
2407
2762
|
rawElements: new Array(1)
|
2408
2763
|
}, isGlobal, leftName, true, 'i16');
|
@@ -2494,6 +2849,56 @@ const generateForOf = (scope, decl) => {
|
|
2494
2849
|
[ Opcodes.end ],
|
2495
2850
|
[ Opcodes.end ]
|
2496
2851
|
],
|
2852
|
+
[TYPES._bytestring]: [
|
2853
|
+
...setType(scope, leftName, TYPES._bytestring),
|
2854
|
+
|
2855
|
+
[ Opcodes.loop, Blocktype.void ],
|
2856
|
+
|
2857
|
+
// setup new/out array
|
2858
|
+
...newOut,
|
2859
|
+
[ Opcodes.drop ],
|
2860
|
+
|
2861
|
+
...number(0, Valtype.i32), // base 0 for store after
|
2862
|
+
|
2863
|
+
// load current string ind {arg}
|
2864
|
+
[ Opcodes.local_get, pointer ],
|
2865
|
+
[ Opcodes.local_get, counter ],
|
2866
|
+
[ Opcodes.i32_add ],
|
2867
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128(ValtypeSize.i32) ],
|
2868
|
+
|
2869
|
+
// store to new string ind 0
|
2870
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
2871
|
+
|
2872
|
+
// return new string (page)
|
2873
|
+
...number(newPointer),
|
2874
|
+
|
2875
|
+
[ isGlobal ? Opcodes.global_set : Opcodes.local_set, local.idx ],
|
2876
|
+
|
2877
|
+
[ Opcodes.block, Blocktype.void ],
|
2878
|
+
[ Opcodes.block, Blocktype.void ],
|
2879
|
+
...generate(scope, decl.body),
|
2880
|
+
[ Opcodes.end ],
|
2881
|
+
|
2882
|
+
// increment iter pointer
|
2883
|
+
// [ Opcodes.local_get, pointer ],
|
2884
|
+
// ...number(1, Valtype.i32),
|
2885
|
+
// [ Opcodes.i32_add ],
|
2886
|
+
// [ Opcodes.local_set, pointer ],
|
2887
|
+
|
2888
|
+
// increment counter by 1
|
2889
|
+
[ Opcodes.local_get, counter ],
|
2890
|
+
...number(1, Valtype.i32),
|
2891
|
+
[ Opcodes.i32_add ],
|
2892
|
+
[ Opcodes.local_tee, counter ],
|
2893
|
+
|
2894
|
+
// loop if counter != length
|
2895
|
+
[ Opcodes.local_get, length ],
|
2896
|
+
[ Opcodes.i32_ne ],
|
2897
|
+
[ Opcodes.br_if, 1 ],
|
2898
|
+
|
2899
|
+
[ Opcodes.end ],
|
2900
|
+
[ Opcodes.end ]
|
2901
|
+
],
|
2497
2902
|
default: internalThrow(scope, 'TypeError', `Tried for..of on non-iterable type`)
|
2498
2903
|
}, Blocktype.void));
|
2499
2904
|
|
@@ -2504,28 +2909,65 @@ const generateForOf = (scope, decl) => {
|
|
2504
2909
|
return out;
|
2505
2910
|
};
|
2506
2911
|
|
2912
|
+
// find the nearest loop in depth map by type
|
2507
2913
|
const getNearestLoop = () => {
|
2508
2914
|
for (let i = depth.length - 1; i >= 0; i--) {
|
2509
|
-
if (
|
2915
|
+
if (['while', 'dowhile', 'for', 'forof'].includes(depth[i])) return i;
|
2510
2916
|
}
|
2511
2917
|
|
2512
2918
|
return -1;
|
2513
2919
|
};
|
2514
2920
|
|
2515
2921
|
const generateBreak = (scope, decl) => {
|
2516
|
-
const
|
2922
|
+
const target = decl.label ? scope.labels.get(decl.label.name) : getNearestLoop();
|
2923
|
+
const type = depth[target];
|
2924
|
+
|
2925
|
+
// different loop types have different branch offsets
|
2926
|
+
// as they have different wasm block/loop/if structures
|
2927
|
+
// we need to use the right offset by type to branch to the one we want
|
2928
|
+
// for a break: exit the loop without executing anything else inside it
|
2929
|
+
const offset = ({
|
2930
|
+
for: 2, // loop > if (wanted branch) > block (we are here)
|
2931
|
+
while: 2, // loop > if (wanted branch) (we are here)
|
2932
|
+
dowhile: 2, // loop > block (wanted branch) > block (we are here)
|
2933
|
+
forof: 2, // loop > block (wanted branch) > block (we are here)
|
2934
|
+
if: 1 // break inside if, branch 0 to skip the rest of the if
|
2935
|
+
})[type];
|
2936
|
+
|
2517
2937
|
return [
|
2518
|
-
[ Opcodes.br, ...signedLEB128(
|
2938
|
+
[ Opcodes.br, ...signedLEB128(depth.length - target - offset) ]
|
2519
2939
|
];
|
2520
2940
|
};
|
2521
2941
|
|
2522
2942
|
const generateContinue = (scope, decl) => {
|
2523
|
-
const
|
2943
|
+
const target = decl.label ? scope.labels.get(decl.label.name) : getNearestLoop();
|
2944
|
+
const type = depth[target];
|
2945
|
+
|
2946
|
+
// different loop types have different branch offsets
|
2947
|
+
// as they have different wasm block/loop/if structures
|
2948
|
+
// we need to use the right offset by type to branch to the one we want
|
2949
|
+
// for a continue: do test for the loop, and then loop depending on that success
|
2950
|
+
const offset = ({
|
2951
|
+
for: 3, // loop (wanted branch) > if > block (we are here)
|
2952
|
+
while: 1, // loop (wanted branch) > if (we are here)
|
2953
|
+
dowhile: 3, // loop > block > block (wanted branch) (we are here)
|
2954
|
+
forof: 3 // loop > block > block (wanted branch) (we are here)
|
2955
|
+
})[type];
|
2956
|
+
|
2524
2957
|
return [
|
2525
|
-
[ Opcodes.br, ...signedLEB128(
|
2958
|
+
[ Opcodes.br, ...signedLEB128(depth.length - target - offset) ]
|
2526
2959
|
];
|
2527
2960
|
};
|
2528
2961
|
|
2962
|
+
const generateLabel = (scope, decl) => {
|
2963
|
+
scope.labels ??= new Map();
|
2964
|
+
|
2965
|
+
const name = decl.label.name;
|
2966
|
+
scope.labels.set(name, depth.length);
|
2967
|
+
|
2968
|
+
return generate(scope, decl.body);
|
2969
|
+
};
|
2970
|
+
|
2529
2971
|
const generateThrow = (scope, decl) => {
|
2530
2972
|
scope.throws = true;
|
2531
2973
|
|
@@ -2546,6 +2988,9 @@ const generateThrow = (scope, decl) => {
|
|
2546
2988
|
let exceptId = exceptions.push({ constructor, message }) - 1;
|
2547
2989
|
let tagIdx = tags[0].idx;
|
2548
2990
|
|
2991
|
+
scope.exceptions ??= [];
|
2992
|
+
scope.exceptions.push(exceptId);
|
2993
|
+
|
2549
2994
|
// todo: write a description of how this works lol
|
2550
2995
|
|
2551
2996
|
return [
|
@@ -2555,7 +3000,7 @@ const generateThrow = (scope, decl) => {
|
|
2555
3000
|
};
|
2556
3001
|
|
2557
3002
|
const generateTry = (scope, decl) => {
|
2558
|
-
if (decl.finalizer) return todo('try finally not implemented yet');
|
3003
|
+
if (decl.finalizer) return todo(scope, 'try finally not implemented yet');
|
2559
3004
|
|
2560
3005
|
const out = [];
|
2561
3006
|
|
@@ -2586,11 +3031,11 @@ const generateAssignPat = (scope, decl) => {
|
|
2586
3031
|
// TODO
|
2587
3032
|
// if identifier declared, use that
|
2588
3033
|
// else, use default (right)
|
2589
|
-
return todo('assignment pattern (optional arg)');
|
3034
|
+
return todo(scope, 'assignment pattern (optional arg)');
|
2590
3035
|
};
|
2591
3036
|
|
2592
3037
|
let pages = new Map();
|
2593
|
-
const allocPage = (reason, type) => {
|
3038
|
+
const allocPage = (scope, reason, type) => {
|
2594
3039
|
if (pages.has(reason)) return pages.get(reason).ind;
|
2595
3040
|
|
2596
3041
|
if (reason.startsWith('array:')) pages.hasArray = true;
|
@@ -2601,16 +3046,20 @@ const allocPage = (reason, type) => {
|
|
2601
3046
|
const ind = pages.size;
|
2602
3047
|
pages.set(reason, { ind, type });
|
2603
3048
|
|
2604
|
-
|
3049
|
+
scope.pages ??= new Map();
|
3050
|
+
scope.pages.set(reason, { ind, type });
|
3051
|
+
|
3052
|
+
if (Prefs.allocLog) log('alloc', `allocated new page of memory (${ind}) | ${reason} (type: ${type})`);
|
2605
3053
|
|
2606
3054
|
return ind;
|
2607
3055
|
};
|
2608
3056
|
|
3057
|
+
// todo: add scope.pages
|
2609
3058
|
const freePage = reason => {
|
2610
3059
|
const { ind } = pages.get(reason);
|
2611
3060
|
pages.delete(reason);
|
2612
3061
|
|
2613
|
-
if (allocLog) log('alloc', `freed page of memory (${ind}) | ${reason}`);
|
3062
|
+
if (Prefs.allocLog) log('alloc', `freed page of memory (${ind}) | ${reason}`);
|
2614
3063
|
|
2615
3064
|
return ind;
|
2616
3065
|
};
|
@@ -2636,15 +3085,14 @@ const StoreOps = {
|
|
2636
3085
|
|
2637
3086
|
let data = [];
|
2638
3087
|
|
2639
|
-
const compileBytes = (val, itemType
|
3088
|
+
const compileBytes = (val, itemType) => {
|
2640
3089
|
// todo: this is a mess and needs confirming / ????
|
2641
3090
|
switch (itemType) {
|
2642
3091
|
case 'i8': return [ val % 256 ];
|
2643
|
-
case 'i16': return [ val % 256,
|
2644
|
-
|
2645
|
-
case 'i32':
|
2646
|
-
|
2647
|
-
return enforceFourBytes(signedLEB128(val));
|
3092
|
+
case 'i16': return [ val % 256, (val / 256 | 0) % 256 ];
|
3093
|
+
case 'i16': return [ val % 256, (val / 256 | 0) % 256 ];
|
3094
|
+
case 'i32': return [...new Uint8Array(new Int32Array([ val ]).buffer)];
|
3095
|
+
// todo: i64
|
2648
3096
|
|
2649
3097
|
case 'f64': return ieee754_binary64(val);
|
2650
3098
|
}
|
@@ -2662,16 +3110,22 @@ const getAllocType = itemType => {
|
|
2662
3110
|
const makeArray = (scope, decl, global = false, name = '$undeclared', initEmpty = false, itemType = valtype) => {
|
2663
3111
|
const out = [];
|
2664
3112
|
|
3113
|
+
scope.arrays ??= new Map();
|
3114
|
+
|
2665
3115
|
let firstAssign = false;
|
2666
|
-
if (!arrays.has(name) || name === '$undeclared') {
|
3116
|
+
if (!scope.arrays.has(name) || name === '$undeclared') {
|
2667
3117
|
firstAssign = true;
|
2668
3118
|
|
2669
3119
|
// todo: can we just have 1 undeclared array? probably not? but this is not really memory efficient
|
2670
3120
|
const uniqueName = name === '$undeclared' ? name + Math.random().toString().slice(2) : name;
|
2671
|
-
|
3121
|
+
|
3122
|
+
if (Prefs.scopedPageNames) scope.arrays.set(name, allocPage(scope, `${getAllocType(itemType)}: ${scope.name}/${uniqueName}`, itemType) * pageSize);
|
3123
|
+
else scope.arrays.set(name, allocPage(scope, `${getAllocType(itemType)}: ${uniqueName}`, itemType) * pageSize);
|
2672
3124
|
}
|
2673
3125
|
|
2674
|
-
const pointer = arrays.get(name);
|
3126
|
+
const pointer = scope.arrays.get(name);
|
3127
|
+
|
3128
|
+
const local = global ? globals[name] : scope.locals[name];
|
2675
3129
|
|
2676
3130
|
const useRawElements = !!decl.rawElements;
|
2677
3131
|
const elements = useRawElements ? decl.rawElements : decl.elements;
|
@@ -2679,19 +3133,25 @@ const makeArray = (scope, decl, global = false, name = '$undeclared', initEmpty
|
|
2679
3133
|
const valtype = itemTypeToValtype[itemType];
|
2680
3134
|
const length = elements.length;
|
2681
3135
|
|
2682
|
-
if (firstAssign && useRawElements) {
|
2683
|
-
|
3136
|
+
if (firstAssign && useRawElements && !Prefs.noData) {
|
3137
|
+
// if length is 0 memory/data will just be 0000... anyway
|
3138
|
+
if (length !== 0) {
|
3139
|
+
let bytes = compileBytes(length, 'i32');
|
2684
3140
|
|
2685
|
-
|
2686
|
-
|
3141
|
+
if (!initEmpty) for (let i = 0; i < length; i++) {
|
3142
|
+
if (elements[i] == null) continue;
|
2687
3143
|
|
2688
|
-
|
2689
|
-
|
3144
|
+
bytes.push(...compileBytes(elements[i], itemType));
|
3145
|
+
}
|
2690
3146
|
|
2691
|
-
|
2692
|
-
|
2693
|
-
|
2694
|
-
|
3147
|
+
const ind = data.push({
|
3148
|
+
offset: pointer,
|
3149
|
+
bytes
|
3150
|
+
}) - 1;
|
3151
|
+
|
3152
|
+
scope.data ??= [];
|
3153
|
+
scope.data.push(ind);
|
3154
|
+
}
|
2695
3155
|
|
2696
3156
|
// local value as pointer
|
2697
3157
|
out.push(...number(pointer));
|
@@ -2699,11 +3159,22 @@ const makeArray = (scope, decl, global = false, name = '$undeclared', initEmpty
|
|
2699
3159
|
return [ out, pointer ];
|
2700
3160
|
}
|
2701
3161
|
|
3162
|
+
const pointerTmp = local != null ? localTmp(scope, '#makearray_pointer_tmp', Valtype.i32) : null;
|
3163
|
+
if (pointerTmp != null) {
|
3164
|
+
out.push(
|
3165
|
+
[ global ? Opcodes.global_get : Opcodes.local_get, local.idx ],
|
3166
|
+
Opcodes.i32_to_u,
|
3167
|
+
[ Opcodes.local_set, pointerTmp ]
|
3168
|
+
);
|
3169
|
+
}
|
3170
|
+
|
3171
|
+
const pointerWasm = pointerTmp != null ? [ [ Opcodes.local_get, pointerTmp ] ] : number(pointer, Valtype.i32);
|
3172
|
+
|
2702
3173
|
// store length as 0th array
|
2703
3174
|
out.push(
|
2704
|
-
...
|
3175
|
+
...pointerWasm,
|
2705
3176
|
...number(length, Valtype.i32),
|
2706
|
-
[ Opcodes.i32_store, Math.log2(ValtypeSize.i32) - 1,
|
3177
|
+
[ Opcodes.i32_store, Math.log2(ValtypeSize.i32) - 1, 0 ]
|
2707
3178
|
);
|
2708
3179
|
|
2709
3180
|
const storeOp = StoreOps[itemType];
|
@@ -2712,20 +3183,20 @@ const makeArray = (scope, decl, global = false, name = '$undeclared', initEmpty
|
|
2712
3183
|
if (elements[i] == null) continue;
|
2713
3184
|
|
2714
3185
|
out.push(
|
2715
|
-
...
|
3186
|
+
...pointerWasm,
|
2716
3187
|
...(useRawElements ? number(elements[i], Valtype[valtype]) : generate(scope, elements[i])),
|
2717
|
-
[ storeOp, (Math.log2(ValtypeSize[itemType]) || 1) - 1, ...unsignedLEB128(
|
3188
|
+
[ storeOp, (Math.log2(ValtypeSize[itemType]) || 1) - 1, ...unsignedLEB128(ValtypeSize.i32 + i * ValtypeSize[itemType]) ]
|
2718
3189
|
);
|
2719
3190
|
}
|
2720
3191
|
|
2721
3192
|
// local value as pointer
|
2722
|
-
out.push(...
|
3193
|
+
out.push(...pointerWasm, Opcodes.i32_from_u);
|
2723
3194
|
|
2724
3195
|
return [ out, pointer ];
|
2725
3196
|
};
|
2726
3197
|
|
2727
3198
|
const byteStringable = str => {
|
2728
|
-
if (!
|
3199
|
+
if (!Prefs.bytestring) return false;
|
2729
3200
|
|
2730
3201
|
for (let i = 0; i < str.length; i++) {
|
2731
3202
|
if (str.charCodeAt(i) > 0xFF) return false;
|
@@ -2734,9 +3205,9 @@ const byteStringable = str => {
|
|
2734
3205
|
return true;
|
2735
3206
|
};
|
2736
3207
|
|
2737
|
-
const makeString = (scope, str, global = false, name = '$undeclared') => {
|
3208
|
+
const makeString = (scope, str, global = false, name = '$undeclared', forceBytestring = undefined) => {
|
2738
3209
|
const rawElements = new Array(str.length);
|
2739
|
-
let byteStringable =
|
3210
|
+
let byteStringable = Prefs.bytestring;
|
2740
3211
|
for (let i = 0; i < str.length; i++) {
|
2741
3212
|
const c = str.charCodeAt(i);
|
2742
3213
|
rawElements[i] = c;
|
@@ -2744,25 +3215,36 @@ const makeString = (scope, str, global = false, name = '$undeclared') => {
|
|
2744
3215
|
if (byteStringable && c > 0xFF) byteStringable = false;
|
2745
3216
|
}
|
2746
3217
|
|
3218
|
+
if (byteStringable && forceBytestring === false) byteStringable = false;
|
3219
|
+
|
2747
3220
|
return makeArray(scope, {
|
2748
3221
|
rawElements
|
2749
3222
|
}, global, name, false, byteStringable ? 'i8' : 'i16')[0];
|
2750
3223
|
};
|
2751
3224
|
|
2752
|
-
let arrays = new Map();
|
2753
3225
|
const generateArray = (scope, decl, global = false, name = '$undeclared', initEmpty = false) => {
|
2754
3226
|
return makeArray(scope, decl, global, name, initEmpty, valtype)[0];
|
2755
3227
|
};
|
2756
3228
|
|
2757
3229
|
export const generateMember = (scope, decl, _global, _name) => {
|
2758
3230
|
const name = decl.object.name;
|
2759
|
-
const pointer = arrays
|
3231
|
+
const pointer = scope.arrays?.get(name);
|
2760
3232
|
|
2761
|
-
const aotPointer = pointer != null;
|
3233
|
+
const aotPointer = Prefs.aotPointerOpt && pointer != null;
|
2762
3234
|
|
2763
3235
|
// hack: .length
|
2764
3236
|
if (decl.property.name === 'length') {
|
2765
|
-
|
3237
|
+
const func = funcs.find(x => x.name === name);
|
3238
|
+
if (func) {
|
3239
|
+
const userFunc = funcIndex[name] && !importedFuncs[name] && !builtinFuncs[name] && !internalConstrs[name];
|
3240
|
+
const typedParams = userFunc || builtinFuncs[name]?.typedParams;
|
3241
|
+
return number(typedParams ? func.params.length / 2 : func.params.length);
|
3242
|
+
}
|
3243
|
+
|
3244
|
+
if (builtinFuncs[name]) return number(builtinFuncs[name].typedParams ? (builtinFuncs[name].params.length / 2) : builtinFuncs[name].params.length);
|
3245
|
+
if (importedFuncs[name]) return number(importedFuncs[name].params);
|
3246
|
+
if (internalConstrs[name]) return number(internalConstrs[name].length ?? 0);
|
3247
|
+
|
2766
3248
|
return [
|
2767
3249
|
...(aotPointer ? number(0, Valtype.i32) : [
|
2768
3250
|
...generate(scope, decl.object),
|
@@ -2806,7 +3288,7 @@ export const generateMember = (scope, decl, _global, _name) => {
|
|
2806
3288
|
[ Opcodes.load, Math.log2(ValtypeSize[valtype]) - 1, ...unsignedLEB128((aotPointer ? pointer : 0) + ValtypeSize.i32) ],
|
2807
3289
|
|
2808
3290
|
...number(TYPES.number, Valtype.i32),
|
2809
|
-
setLastType(scope)
|
3291
|
+
...setLastType(scope)
|
2810
3292
|
],
|
2811
3293
|
|
2812
3294
|
[TYPES.string]: [
|
@@ -2838,7 +3320,7 @@ export const generateMember = (scope, decl, _global, _name) => {
|
|
2838
3320
|
...number(newPointer),
|
2839
3321
|
|
2840
3322
|
...number(TYPES.string, Valtype.i32),
|
2841
|
-
setLastType(scope)
|
3323
|
+
...setLastType(scope)
|
2842
3324
|
],
|
2843
3325
|
[TYPES._bytestring]: [
|
2844
3326
|
// setup new/out array
|
@@ -2857,19 +3339,19 @@ export const generateMember = (scope, decl, _global, _name) => {
|
|
2857
3339
|
]),
|
2858
3340
|
|
2859
3341
|
// load current string ind {arg}
|
2860
|
-
[ Opcodes.i32_load8_u,
|
3342
|
+
[ Opcodes.i32_load8_u, 0, ...unsignedLEB128((aotPointer ? pointer : 0) + ValtypeSize.i32) ],
|
2861
3343
|
|
2862
3344
|
// store to new string ind 0
|
2863
|
-
[ Opcodes.i32_store8,
|
3345
|
+
[ Opcodes.i32_store8, 0, ...unsignedLEB128(newPointer + ValtypeSize.i32) ],
|
2864
3346
|
|
2865
3347
|
// return new string (page)
|
2866
3348
|
...number(newPointer),
|
2867
3349
|
|
2868
3350
|
...number(TYPES._bytestring, Valtype.i32),
|
2869
|
-
setLastType(scope)
|
3351
|
+
...setLastType(scope)
|
2870
3352
|
],
|
2871
3353
|
|
2872
|
-
default:
|
3354
|
+
default: internalThrow(scope, 'TypeError', 'Member expression is not supported for non-string non-array yet', true)
|
2873
3355
|
});
|
2874
3356
|
};
|
2875
3357
|
|
@@ -2879,28 +3361,36 @@ const objectHack = node => {
|
|
2879
3361
|
if (!node) return node;
|
2880
3362
|
|
2881
3363
|
if (node.type === 'MemberExpression') {
|
2882
|
-
|
3364
|
+
const out = (() => {
|
3365
|
+
if (node.computed || node.optional) return;
|
2883
3366
|
|
2884
|
-
|
3367
|
+
let objectName = node.object.name;
|
2885
3368
|
|
2886
|
-
|
2887
|
-
|
3369
|
+
// if object is not identifier or another member exp, give up
|
3370
|
+
if (node.object.type !== 'Identifier' && node.object.type !== 'MemberExpression') return;
|
3371
|
+
if (objectName && ['undefined', 'null', 'NaN', 'Infinity'].includes(objectName)) return;
|
2888
3372
|
|
2889
|
-
|
3373
|
+
if (!objectName) objectName = objectHack(node.object)?.name?.slice?.(2);
|
2890
3374
|
|
2891
|
-
|
2892
|
-
|
3375
|
+
// if .length, give up (hack within a hack!)
|
3376
|
+
if (node.property.name === 'length') {
|
3377
|
+
node.object = objectHack(node.object);
|
3378
|
+
return;
|
3379
|
+
}
|
2893
3380
|
|
2894
|
-
|
2895
|
-
|
3381
|
+
// no object name, give up
|
3382
|
+
if (!objectName) return;
|
2896
3383
|
|
2897
|
-
|
2898
|
-
|
3384
|
+
const name = '__' + objectName + '_' + node.property.name;
|
3385
|
+
if (Prefs.codeLog) log('codegen', `object hack! ${node.object.name}.${node.property.name} -> ${name}`);
|
2899
3386
|
|
2900
|
-
|
2901
|
-
|
2902
|
-
|
2903
|
-
|
3387
|
+
return {
|
3388
|
+
type: 'Identifier',
|
3389
|
+
name
|
3390
|
+
};
|
3391
|
+
})();
|
3392
|
+
|
3393
|
+
if (out) return out;
|
2904
3394
|
}
|
2905
3395
|
|
2906
3396
|
for (const x in node) {
|
@@ -2914,8 +3404,8 @@ const objectHack = node => {
|
|
2914
3404
|
};
|
2915
3405
|
|
2916
3406
|
const generateFunc = (scope, decl) => {
|
2917
|
-
if (decl.async) return todo('async functions are not supported');
|
2918
|
-
if (decl.generator) return todo('generator functions are not supported');
|
3407
|
+
if (decl.async) return todo(scope, 'async functions are not supported');
|
3408
|
+
if (decl.generator) return todo(scope, 'generator functions are not supported');
|
2919
3409
|
|
2920
3410
|
const name = decl.id ? decl.id.name : `anonymous_${randId()}`;
|
2921
3411
|
const params = decl.params ?? [];
|
@@ -2931,6 +3421,11 @@ const generateFunc = (scope, decl) => {
|
|
2931
3421
|
name
|
2932
3422
|
};
|
2933
3423
|
|
3424
|
+
if (typedInput && decl.returnType) {
|
3425
|
+
innerScope.returnType = extractTypeAnnotation(decl.returnType).type;
|
3426
|
+
innerScope.returns = [ valtypeBinary ];
|
3427
|
+
}
|
3428
|
+
|
2934
3429
|
for (let i = 0; i < params.length; i++) {
|
2935
3430
|
allocVar(innerScope, params[i].name, false);
|
2936
3431
|
|
@@ -2957,6 +3452,8 @@ const generateFunc = (scope, decl) => {
|
|
2957
3452
|
};
|
2958
3453
|
funcIndex[name] = func.index;
|
2959
3454
|
|
3455
|
+
if (name === 'main') func.gotLastType = true;
|
3456
|
+
|
2960
3457
|
// quick hack fixes
|
2961
3458
|
for (const inst of wasm) {
|
2962
3459
|
if (inst[0] === Opcodes.call && inst[1] === -1) {
|
@@ -3008,7 +3505,7 @@ const internalConstrs = {
|
|
3008
3505
|
|
3009
3506
|
// todo: check in wasm instead of here
|
3010
3507
|
const literalValue = arg.value ?? 0;
|
3011
|
-
if (literalValue < 0 || !Number.isFinite(literalValue) || literalValue > 4294967295) return internalThrow(scope, 'RangeThrow', 'Invalid array length');
|
3508
|
+
if (literalValue < 0 || !Number.isFinite(literalValue) || literalValue > 4294967295) return internalThrow(scope, 'RangeThrow', 'Invalid array length', true);
|
3012
3509
|
|
3013
3510
|
return [
|
3014
3511
|
...number(0, Valtype.i32),
|
@@ -3019,7 +3516,8 @@ const internalConstrs = {
|
|
3019
3516
|
...number(pointer)
|
3020
3517
|
];
|
3021
3518
|
},
|
3022
|
-
type: TYPES._array
|
3519
|
+
type: TYPES._array,
|
3520
|
+
length: 1
|
3023
3521
|
},
|
3024
3522
|
|
3025
3523
|
__Array_of: {
|
@@ -3031,7 +3529,131 @@ const internalConstrs = {
|
|
3031
3529
|
}, global, name);
|
3032
3530
|
},
|
3033
3531
|
type: TYPES._array,
|
3532
|
+
notConstr: true,
|
3533
|
+
length: 0
|
3534
|
+
},
|
3535
|
+
|
3536
|
+
__Porffor_fastOr: {
|
3537
|
+
generate: (scope, decl) => {
|
3538
|
+
const out = [];
|
3539
|
+
|
3540
|
+
for (let i = 0; i < decl.arguments.length; i++) {
|
3541
|
+
out.push(
|
3542
|
+
...generate(scope, decl.arguments[i]),
|
3543
|
+
Opcodes.i32_to_u,
|
3544
|
+
...(i > 0 ? [ [ Opcodes.i32_or ] ] : [])
|
3545
|
+
);
|
3546
|
+
}
|
3547
|
+
|
3548
|
+
out.push(Opcodes.i32_from_u);
|
3549
|
+
|
3550
|
+
return out;
|
3551
|
+
},
|
3552
|
+
type: TYPES.boolean,
|
3553
|
+
notConstr: true
|
3554
|
+
},
|
3555
|
+
|
3556
|
+
__Porffor_fastAnd: {
|
3557
|
+
generate: (scope, decl) => {
|
3558
|
+
const out = [];
|
3559
|
+
|
3560
|
+
for (let i = 0; i < decl.arguments.length; i++) {
|
3561
|
+
out.push(
|
3562
|
+
...generate(scope, decl.arguments[i]),
|
3563
|
+
Opcodes.i32_to_u,
|
3564
|
+
...(i > 0 ? [ [ Opcodes.i32_and ] ] : [])
|
3565
|
+
);
|
3566
|
+
}
|
3567
|
+
|
3568
|
+
out.push(Opcodes.i32_from_u);
|
3569
|
+
|
3570
|
+
return out;
|
3571
|
+
},
|
3572
|
+
type: TYPES.boolean,
|
3034
3573
|
notConstr: true
|
3574
|
+
},
|
3575
|
+
|
3576
|
+
Boolean: {
|
3577
|
+
generate: (scope, decl) => {
|
3578
|
+
// todo: boolean object when used as constructor
|
3579
|
+
const arg = decl.arguments[0] ?? DEFAULT_VALUE;
|
3580
|
+
return truthy(scope, generate(scope, arg), getNodeType(scope, arg));
|
3581
|
+
},
|
3582
|
+
type: TYPES.boolean,
|
3583
|
+
length: 1
|
3584
|
+
},
|
3585
|
+
|
3586
|
+
__Math_max: {
|
3587
|
+
generate: (scope, decl) => {
|
3588
|
+
const out = [
|
3589
|
+
...number(-Infinity)
|
3590
|
+
];
|
3591
|
+
|
3592
|
+
for (let i = 0; i < decl.arguments.length; i++) {
|
3593
|
+
out.push(
|
3594
|
+
...generate(scope, decl.arguments[i]),
|
3595
|
+
[ Opcodes.f64_max ]
|
3596
|
+
);
|
3597
|
+
}
|
3598
|
+
|
3599
|
+
return out;
|
3600
|
+
},
|
3601
|
+
type: TYPES.number,
|
3602
|
+
notConstr: true,
|
3603
|
+
length: 2
|
3604
|
+
},
|
3605
|
+
|
3606
|
+
__Math_min: {
|
3607
|
+
generate: (scope, decl) => {
|
3608
|
+
const out = [
|
3609
|
+
...number(Infinity)
|
3610
|
+
];
|
3611
|
+
|
3612
|
+
for (let i = 0; i < decl.arguments.length; i++) {
|
3613
|
+
out.push(
|
3614
|
+
...generate(scope, decl.arguments[i]),
|
3615
|
+
[ Opcodes.f64_min ]
|
3616
|
+
);
|
3617
|
+
}
|
3618
|
+
|
3619
|
+
return out;
|
3620
|
+
},
|
3621
|
+
type: TYPES.number,
|
3622
|
+
notConstr: true,
|
3623
|
+
length: 2
|
3624
|
+
},
|
3625
|
+
|
3626
|
+
__console_log: {
|
3627
|
+
generate: (scope, decl) => {
|
3628
|
+
const out = [];
|
3629
|
+
|
3630
|
+
for (let i = 0; i < decl.arguments.length; i++) {
|
3631
|
+
out.push(
|
3632
|
+
...generateCall(scope, {
|
3633
|
+
callee: {
|
3634
|
+
type: 'Identifier',
|
3635
|
+
name: '__Porffor_print'
|
3636
|
+
},
|
3637
|
+
arguments: [ decl.arguments[i] ]
|
3638
|
+
}),
|
3639
|
+
|
3640
|
+
// print space
|
3641
|
+
...number(32),
|
3642
|
+
[ Opcodes.call, importedFuncs.printChar ]
|
3643
|
+
);
|
3644
|
+
}
|
3645
|
+
|
3646
|
+
// print newline
|
3647
|
+
out.push(
|
3648
|
+
...number(10),
|
3649
|
+
[ Opcodes.call, importedFuncs.printChar ]
|
3650
|
+
);
|
3651
|
+
|
3652
|
+
return out;
|
3653
|
+
},
|
3654
|
+
type: TYPES.undefined,
|
3655
|
+
notConstr: true,
|
3656
|
+
length: 0
|
3035
3657
|
}
|
3036
3658
|
};
|
3037
3659
|
|
@@ -3060,20 +3682,23 @@ export default program => {
|
|
3060
3682
|
funcs = [];
|
3061
3683
|
funcIndex = {};
|
3062
3684
|
depth = [];
|
3063
|
-
arrays = new Map();
|
3064
3685
|
pages = new Map();
|
3065
3686
|
data = [];
|
3066
3687
|
currentFuncIndex = importedFuncs.length;
|
3067
3688
|
|
3068
3689
|
globalThis.valtype = 'f64';
|
3069
3690
|
|
3070
|
-
const valtypeOpt = process.argv.find(x => x.startsWith('
|
3691
|
+
const valtypeOpt = process.argv.find(x => x.startsWith('--valtype='));
|
3071
3692
|
if (valtypeOpt) valtype = valtypeOpt.split('=')[1];
|
3072
3693
|
|
3073
3694
|
globalThis.valtypeBinary = Valtype[valtype];
|
3074
3695
|
|
3075
3696
|
const valtypeInd = ['i32', 'i64', 'f64'].indexOf(valtype);
|
3076
3697
|
|
3698
|
+
globalThis.pageSize = PageSize;
|
3699
|
+
const pageSizeOpt = process.argv.find(x => x.startsWith('--page-size='));
|
3700
|
+
if (pageSizeOpt) pageSize = parseInt(pageSizeOpt.split('=')[1]) * 1024;
|
3701
|
+
|
3077
3702
|
// set generic opcodes for current valtype
|
3078
3703
|
Opcodes.const = [ Opcodes.i32_const, Opcodes.i64_const, Opcodes.f64_const ][valtypeInd];
|
3079
3704
|
Opcodes.eq = [ Opcodes.i32_eq, Opcodes.i64_eq, Opcodes.f64_eq ][valtypeInd];
|
@@ -3082,10 +3707,10 @@ export default program => {
|
|
3082
3707
|
Opcodes.add = [ Opcodes.i32_add, Opcodes.i64_add, Opcodes.f64_add ][valtypeInd];
|
3083
3708
|
Opcodes.sub = [ Opcodes.i32_sub, Opcodes.i64_sub, Opcodes.f64_sub ][valtypeInd];
|
3084
3709
|
|
3085
|
-
Opcodes.i32_to = [ [
|
3086
|
-
Opcodes.i32_to_u = [ [
|
3087
|
-
Opcodes.i32_from = [ [
|
3088
|
-
Opcodes.i32_from_u = [ [
|
3710
|
+
Opcodes.i32_to = [ [], [ Opcodes.i32_wrap_i64 ], Opcodes.i32_trunc_sat_f64_s ][valtypeInd];
|
3711
|
+
Opcodes.i32_to_u = [ [], [ Opcodes.i32_wrap_i64 ], Opcodes.i32_trunc_sat_f64_u ][valtypeInd];
|
3712
|
+
Opcodes.i32_from = [ [], [ Opcodes.i64_extend_i32_s ], [ Opcodes.f64_convert_i32_s ] ][valtypeInd];
|
3713
|
+
Opcodes.i32_from_u = [ [], [ Opcodes.i64_extend_i32_u ], [ Opcodes.f64_convert_i32_u ] ][valtypeInd];
|
3089
3714
|
|
3090
3715
|
Opcodes.load = [ Opcodes.i32_load, Opcodes.i64_load, Opcodes.f64_load ][valtypeInd];
|
3091
3716
|
Opcodes.store = [ Opcodes.i32_store, Opcodes.i64_store, Opcodes.f64_store ][valtypeInd];
|
@@ -3098,10 +3723,6 @@ export default program => {
|
|
3098
3723
|
|
3099
3724
|
program.id = { name: 'main' };
|
3100
3725
|
|
3101
|
-
globalThis.pageSize = PageSize;
|
3102
|
-
const pageSizeOpt = process.argv.find(x => x.startsWith('-page-size='));
|
3103
|
-
if (pageSizeOpt) pageSize = parseInt(pageSizeOpt.split('=')[1]) * 1024;
|
3104
|
-
|
3105
3726
|
const scope = {
|
3106
3727
|
locals: {},
|
3107
3728
|
localInd: 0
|
@@ -3112,7 +3733,7 @@ export default program => {
|
|
3112
3733
|
body: program.body
|
3113
3734
|
};
|
3114
3735
|
|
3115
|
-
if (
|
3736
|
+
if (Prefs.astLog) console.log(JSON.stringify(program.body.body, null, 2));
|
3116
3737
|
|
3117
3738
|
generateFunc(scope, program);
|
3118
3739
|
|
@@ -3129,7 +3750,11 @@ export default program => {
|
|
3129
3750
|
}
|
3130
3751
|
|
3131
3752
|
if (lastInst[0] === Opcodes.end || lastInst[0] === Opcodes.local_set || lastInst[0] === Opcodes.global_set) {
|
3132
|
-
|
3753
|
+
if (lastInst[0] === Opcodes.local_set && lastInst[1] === main.locals['#last_type'].idx) {
|
3754
|
+
main.wasm.splice(main.wasm.length - 1, 1);
|
3755
|
+
} else {
|
3756
|
+
main.returns = [];
|
3757
|
+
}
|
3133
3758
|
}
|
3134
3759
|
|
3135
3760
|
if (lastInst[0] === Opcodes.call) {
|