Skip to content

Commit 99681ba

Browse files
committed
use proper stubs
1 parent 4972859 commit 99681ba

19 files changed

+518
-466
lines changed

src/common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ export enum CommonFlags {
6666
INLINED = 1 << 23,
6767
/** Is scoped. */
6868
SCOPED = 1 << 24,
69-
/** Is a trampoline. */
70-
TRAMPOLINE = 1 << 25,
69+
/** Is a stub. */
70+
STUB = 1 << 25,
7171
/** Is a virtual method. */
7272
VIRTUAL = 1 << 26,
7373
/** Is (part of) a closure. */
@@ -99,6 +99,8 @@ export const LIBRARY_SUBST = "~lib";
9999
export const LIBRARY_PREFIX = LIBRARY_SUBST + PATH_DELIMITER;
100100
/** Path index suffix. */
101101
export const INDEX_SUFFIX = PATH_DELIMITER + "index";
102+
/** Stub function delimiter. */
103+
export const STUB_DELIMITER = "@";
102104

103105
/** Common names. */
104106
export namespace CommonNames {

src/compiler.ts

Lines changed: 246 additions & 231 deletions
Large diffs are not rendered by default.

src/program.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
INNER_DELIMITER,
4848
LIBRARY_SUBST,
4949
INDEX_SUFFIX,
50+
STUB_DELIMITER,
5051
CommonNames,
5152
Feature,
5253
Target
@@ -3276,12 +3277,12 @@ export class Function extends TypedElement {
32763277
debugLocations: Range[] = [];
32773278
/** Function reference, if compiled. */
32783279
ref: FunctionRef = 0;
3279-
/** Function reference of the virtual stub, if compiled. */
3280-
virtualRef: FunctionRef = 0;
32813280
/** Function table index, if any. */
32823281
functionTableIndex: i32 = -1;
3283-
/** Trampoline function for calling with omitted arguments. */
3284-
trampoline: Function | null = null;
3282+
/** Varargs stub for calling with omitted arguments. */
3283+
varargsStub: Function | null = null;
3284+
/** Virtual stub for calling overloads. */
3285+
virtualStub: Function | null = null;
32853286

32863287
/** Counting id of inline operations involving this function. */
32873288
nextInlineId: i32 = 0;
@@ -3349,6 +3350,19 @@ export class Function extends TypedElement {
33493350
registerConcreteElement(program, this);
33503351
}
33513352

3353+
/** Creates a stub for use with this function, i.e. for varargs or virtual calls. */
3354+
newStub(postfix: string): Function {
3355+
var stub = new Function(
3356+
this.name + STUB_DELIMITER + postfix,
3357+
this.prototype,
3358+
this.typeArguments,
3359+
this.signature.clone(),
3360+
this.contextualTypeArguments
3361+
);
3362+
stub.set(this.flags & ~CommonFlags.COMPILED | CommonFlags.STUB);
3363+
return stub;
3364+
}
3365+
33523366
/** Adds a local of the specified type, with an optional name. */
33533367
addLocal(type: Type, name: string | null = null, declaration: VariableDeclaration | null = null): Local {
33543368
// if it has a name, check previously as this method will throw otherwise

src/types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,27 @@ export class Signature {
719719
sb.push(this.returnType.toString());
720720
return sb.join("");
721721
}
722+
723+
/** Creates a clone of this signature that is safe to modify. */
724+
clone(): Signature {
725+
var parameterTypes = this.parameterTypes;
726+
var numParameterTypes = parameterTypes.length;
727+
var cloneParameterTypes = new Array<Type>(numParameterTypes);
728+
for (let i = 0; i < numParameterTypes; ++i) {
729+
cloneParameterTypes[i] = parameterTypes[i];
730+
}
731+
var clone = new Signature(this.program, cloneParameterTypes, this.returnType, this.thisType);
732+
var parameterNames = this.parameterNames;
733+
if (parameterNames) {
734+
let numParameterNames = parameterNames.length;
735+
let cloneParameterNames = new Array<string>(numParameterNames);
736+
for (let i = 0; i < numParameterNames; ++i) {
737+
cloneParameterNames[i] = parameterNames[i];
738+
}
739+
clone.parameterNames = cloneParameterNames;
740+
}
741+
return clone;
742+
}
722743
}
723744

724745
// helpers

tests/compiler/call-inferred.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
(func $call-inferred/bar<f32> (param $0 f32) (result f32)
2626
local.get $0
2727
)
28-
(func $call-inferred/bar<f32>|trampoline (param $0 f32) (result f32)
28+
(func $call-inferred/bar<f32>@varargs (param $0 f32) (result f32)
2929
block $1of1
3030
block $0of1
3131
block $outOfRange
@@ -87,7 +87,7 @@
8787
i32.const 0
8888
global.set $~argumentsLength
8989
f32.const 0
90-
call $call-inferred/bar<f32>|trampoline
90+
call $call-inferred/bar<f32>@varargs
9191
f32.const 42
9292
f32.eq
9393
i32.eqz

tests/compiler/call-optional.optimized.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
i32.add
1818
i32.add
1919
)
20-
(func $call-optional/opt|trampoline (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
20+
(func $call-optional/opt@varargs (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
2121
block $2of2
2222
block $1of2
2323
block $0of2
@@ -50,7 +50,7 @@
5050
i32.const 3
5151
i32.const 0
5252
i32.const 0
53-
call $call-optional/opt|trampoline
53+
call $call-optional/opt@varargs
5454
if
5555
i32.const 0
5656
i32.const 1040
@@ -64,7 +64,7 @@
6464
i32.const 3
6565
i32.const 4
6666
i32.const 0
67-
call $call-optional/opt|trampoline
67+
call $call-optional/opt@varargs
6868
i32.const 5
6969
i32.ne
7070
if
@@ -94,7 +94,7 @@
9494
i32.const 3
9595
i32.const 0
9696
i32.const 0
97-
call $call-optional/opt|trampoline
97+
call $call-optional/opt@varargs
9898
if
9999
i32.const 0
100100
i32.const 1040
@@ -108,7 +108,7 @@
108108
i32.const 3
109109
i32.const 4
110110
i32.const 0
111-
call $call-optional/opt|trampoline
111+
call $call-optional/opt@varargs
112112
i32.const 5
113113
i32.ne
114114
if
@@ -124,7 +124,7 @@
124124
i32.const 3
125125
i32.const 4
126126
i32.const 5
127-
call $call-optional/opt|trampoline
127+
call $call-optional/opt@varargs
128128
i32.const 12
129129
i32.ne
130130
if

tests/compiler/call-optional.untouched.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
(memory $0 1)
88
(data (i32.const 16) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00c\00a\00l\00l\00-\00o\00p\00t\00i\00o\00n\00a\00l\00.\00t\00s\00")
99
(table $0 2 funcref)
10-
(elem (i32.const 1) $call-optional/opt|trampoline)
10+
(elem (i32.const 1) $call-optional/opt@varargs)
1111
(global $~argumentsLength (mut i32) (i32.const 0))
1212
(global $call-optional/optIndirect (mut i32) (i32.const 1))
1313
(export "__setArgumentsLength" (func $~setArgumentsLength))
@@ -20,7 +20,7 @@
2020
local.get $2
2121
i32.add
2222
)
23-
(func $call-optional/opt|trampoline (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
23+
(func $call-optional/opt@varargs (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
2424
block $2of2
2525
block $1of2
2626
block $0of2
@@ -53,7 +53,7 @@
5353
i32.const 1
5454
global.set $~argumentsLength
5555
i32.const 0
56-
call $call-optional/opt|trampoline
56+
call $call-optional/opt@varargs
5757
i32.const 0
5858
i32.eq
5959
i32.eqz
@@ -70,7 +70,7 @@
7070
i32.const 2
7171
global.set $~argumentsLength
7272
i32.const 0
73-
call $call-optional/opt|trampoline
73+
call $call-optional/opt@varargs
7474
i32.const 5
7575
i32.eq
7676
i32.eqz

tests/compiler/class-overloading.optimized.wat

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
call $class-overloading/B#constructor
268268
global.set $class-overloading/a
269269
global.get $class-overloading/a
270-
call $class-overloading/A#a<i32>|virtual
270+
call $class-overloading/A#a<i32>@virtual
271271
global.get $class-overloading/which
272272
i32.const 1088
273273
call $~lib/string/String.__eq
@@ -283,7 +283,7 @@
283283
i32.const 1040
284284
global.set $class-overloading/which
285285
global.get $class-overloading/a
286-
call $class-overloading/A#b|virtual
286+
call $class-overloading/A#b@virtual
287287
global.get $class-overloading/which
288288
i32.const 1088
289289
call $~lib/string/String.__eq
@@ -299,7 +299,7 @@
299299
i32.const 1040
300300
global.set $class-overloading/which
301301
global.get $class-overloading/a
302-
call $class-overloading/A#b|virtual
302+
call $class-overloading/A#b@virtual
303303
global.get $class-overloading/which
304304
i32.const 1088
305305
call $~lib/string/String.__eq
@@ -315,7 +315,7 @@
315315
i32.const 1040
316316
global.set $class-overloading/which
317317
global.get $class-overloading/a
318-
call $class-overloading/A#b|virtual
318+
call $class-overloading/A#b@virtual
319319
global.get $class-overloading/which
320320
i32.const 1088
321321
call $~lib/string/String.__eq
@@ -399,7 +399,7 @@
399399
i32.const 1040
400400
global.set $class-overloading/which
401401
global.get $class-overloading/a
402-
call $class-overloading/A#a<i32>|virtual
402+
call $class-overloading/A#a<i32>@virtual
403403
global.get $class-overloading/which
404404
i32.const 1088
405405
call $~lib/string/String.__eq
@@ -415,7 +415,7 @@
415415
i32.const 1040
416416
global.set $class-overloading/which
417417
global.get $class-overloading/a
418-
call $class-overloading/A#b|virtual
418+
call $class-overloading/A#b@virtual
419419
global.get $class-overloading/which
420420
i32.const 1088
421421
call $~lib/string/String.__eq
@@ -431,7 +431,7 @@
431431
i32.const 1040
432432
global.set $class-overloading/which
433433
global.get $class-overloading/a
434-
call $class-overloading/A#b|virtual
434+
call $class-overloading/A#b@virtual
435435
global.get $class-overloading/which
436436
i32.const 1088
437437
call $~lib/string/String.__eq
@@ -445,7 +445,7 @@
445445
unreachable
446446
end
447447
global.get $class-overloading/a
448-
call $class-overloading/A#b|virtual
448+
call $class-overloading/A#b@virtual
449449
global.get $class-overloading/which
450450
i32.const 1088
451451
call $~lib/string/String.__eq
@@ -464,7 +464,7 @@
464464
i32.const 1040
465465
global.set $class-overloading/which
466466
global.get $class-overloading/a
467-
call $class-overloading/A#a<i32>|virtual
467+
call $class-overloading/A#a<i32>@virtual
468468
global.get $class-overloading/which
469469
i32.const 1088
470470
call $~lib/string/String.__eq
@@ -480,7 +480,7 @@
480480
i32.const 1040
481481
global.set $class-overloading/which
482482
global.get $class-overloading/a
483-
call $class-overloading/A#b|virtual
483+
call $class-overloading/A#b@virtual
484484
global.get $class-overloading/which
485485
i32.const 1088
486486
call $~lib/string/String.__eq
@@ -496,7 +496,7 @@
496496
i32.const 1040
497497
global.set $class-overloading/which
498498
global.get $class-overloading/a
499-
call $class-overloading/A#b|virtual
499+
call $class-overloading/A#b@virtual
500500
global.get $class-overloading/which
501501
i32.const 1088
502502
call $~lib/string/String.__eq
@@ -510,7 +510,7 @@
510510
unreachable
511511
end
512512
global.get $class-overloading/a
513-
call $class-overloading/A#b|virtual
513+
call $class-overloading/A#b@virtual
514514
global.get $class-overloading/which
515515
i32.const 1088
516516
call $~lib/string/String.__eq
@@ -530,7 +530,7 @@
530530
i32.const 1040
531531
global.set $class-overloading/which
532532
global.get $class-overloading/a
533-
call $class-overloading/A#a<i32>|virtual
533+
call $class-overloading/A#a<i32>@virtual
534534
global.get $class-overloading/which
535535
i32.const 1216
536536
call $~lib/string/String.__eq
@@ -546,7 +546,7 @@
546546
i32.const 1040
547547
global.set $class-overloading/which
548548
global.get $class-overloading/a
549-
call $class-overloading/A#b|virtual
549+
call $class-overloading/A#b@virtual
550550
global.get $class-overloading/which
551551
i32.const 1216
552552
call $~lib/string/String.__eq
@@ -562,7 +562,7 @@
562562
i32.const 1040
563563
global.set $class-overloading/which
564564
global.get $class-overloading/a
565-
call $class-overloading/A#b|virtual
565+
call $class-overloading/A#b@virtual
566566
global.get $class-overloading/which
567567
i32.const 1216
568568
call $~lib/string/String.__eq
@@ -578,7 +578,7 @@
578578
i32.const 1040
579579
global.set $class-overloading/which
580580
global.get $class-overloading/a
581-
call $class-overloading/A#b|virtual
581+
call $class-overloading/A#b@virtual
582582
global.get $class-overloading/which
583583
i32.const 1216
584584
call $~lib/string/String.__eq
@@ -597,7 +597,7 @@
597597
i32.const 1040
598598
global.set $class-overloading/which
599599
global.get $class-overloading/ia
600-
call $class-overloading/IA#foo|virtual
600+
call $class-overloading/IA#foo@virtual
601601
global.get $class-overloading/which
602602
i32.const 1248
603603
call $~lib/string/String.__eq
@@ -616,7 +616,7 @@
616616
i32.const 1040
617617
global.set $class-overloading/which
618618
global.get $class-overloading/ic
619-
call $class-overloading/IA#foo|virtual
619+
call $class-overloading/IA#foo@virtual
620620
global.get $class-overloading/which
621621
i32.const 1280
622622
call $~lib/string/String.__eq
@@ -640,7 +640,7 @@
640640
end
641641
call $start:class-overloading
642642
)
643-
(func $class-overloading/A#a<i32>|virtual (param $0 i32)
643+
(func $class-overloading/A#a<i32>@virtual (param $0 i32)
644644
block $default
645645
block $case2
646646
block $case1
@@ -679,7 +679,7 @@
679679
i32.const 1056
680680
global.set $class-overloading/which
681681
)
682-
(func $class-overloading/A#b|virtual (param $0 i32)
682+
(func $class-overloading/A#b@virtual (param $0 i32)
683683
block $default
684684
block $case2
685685
block $case1
@@ -719,7 +719,7 @@
719719
i32.const 1056
720720
global.set $class-overloading/which
721721
)
722-
(func $class-overloading/IA#foo|virtual (param $0 i32)
722+
(func $class-overloading/IA#foo@virtual (param $0 i32)
723723
block $default
724724
block $case1
725725
local.get $0

0 commit comments

Comments
 (0)