Skip to content

Commit 1928f46

Browse files
committed
Also handle indirect recursive inlining
1 parent 4b85003 commit 1928f46

6 files changed

+47
-17
lines changed

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ export class Compiler extends DiagnosticEmitter {
265265
currentFunction: Function;
266266
/** Current outer function in compilation, if compiling a function expression. */
267267
currentOuterFunction: Function | null = null;
268-
/** Current inline function in compilation. */
269-
currentInlineFunction: Function | null = null;
268+
/** Current inline functions stack. */
269+
currentInlineFunctions: Function[] = [];
270270
/** Current enum in compilation. */
271271
currentEnum: Enum | null = null;
272272
/** Current type in compilation. */
@@ -5259,20 +5259,16 @@ export class Compiler extends DiagnosticEmitter {
52595259
// Inline if explicitly requested
52605260
if (inline) {
52615261
assert(!instance.is(CommonFlags.TRAMPOLINE)); // doesn't make sense
5262-
if (instance === this.currentInlineFunction) {
5263-
// skip inlining when trying to inline a function into itself and print a warning when
5264-
// instead compiling the function the normal way.
5265-
if (instance === this.currentFunction) {
5266-
this.warning(
5267-
DiagnosticCode.Function_0_cannot_be_inlined_into_itself,
5268-
reportNode.range, instance.internalName
5269-
);
5270-
}
5262+
if (this.currentInlineFunctions.includes(instance)) {
5263+
this.warning(
5264+
DiagnosticCode.Function_0_cannot_be_inlined_into_itself,
5265+
reportNode.range, instance.internalName
5266+
);
52715267
} else {
5272-
this.currentInlineFunction = instance;
5273-
let ret = this.compileCallInlineUnchecked(instance, argumentExpressions, reportNode, thisArg);
5274-
this.currentInlineFunction = null;
5275-
return ret;
5268+
this.currentInlineFunctions.push(instance);
5269+
let expr = this.compileCallInlineUnchecked(instance, argumentExpressions, reportNode, thisArg);
5270+
this.currentInlineFunctions.pop();
5271+
return expr;
52765272
}
52775273
}
52785274

tests/compiler/inlining-recursive.optimized.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22
(type $v (func))
33
(memory $0 0)
44
(export "foo" (func $inlining-recursive/foo))
5+
(export "baz" (func $inlining-recursive/baz))
6+
(export "bar" (func $inlining-recursive/bar))
57
(export "memory" (memory $0))
68
(func $inlining-recursive/foo (; 0 ;) (type $v)
79
(call $inlining-recursive/foo)
810
)
11+
(func $inlining-recursive/baz (; 1 ;) (type $v)
12+
(call $inlining-recursive/bar)
13+
)
14+
(func $inlining-recursive/bar (; 2 ;) (type $v)
15+
(call $inlining-recursive/baz)
16+
)
917
)

tests/compiler/inlining-recursive.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1+
// direct
2+
13
@inline
24
export function foo(): void {
35
foo();
46
}
7+
8+
// indirect
9+
10+
@inline
11+
export function bar(): void {
12+
baz();
13+
}
14+
15+
@inline
16+
export function baz(): void {
17+
bar();
18+
}

tests/compiler/inlining-recursive.untouched.wat

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
(global $HEAP_BASE i32 (i32.const 8))
44
(memory $0 0)
55
(export "foo" (func $inlining-recursive/foo))
6+
(export "baz" (func $inlining-recursive/baz))
7+
(export "bar" (func $inlining-recursive/bar))
68
(export "memory" (memory $0))
79
(func $inlining-recursive/foo (; 0 ;) (type $v)
810
(block $inlining-recursive/foo|inlined.0
911
(call $inlining-recursive/foo)
1012
)
1113
)
14+
(func $inlining-recursive/baz (; 1 ;) (type $v)
15+
(call $inlining-recursive/bar)
16+
)
17+
(func $inlining-recursive/bar (; 2 ;) (type $v)
18+
(block $inlining-recursive/baz|inlined.0
19+
(block $inlining-recursive/bar|inlined.0
20+
(call $inlining-recursive/baz)
21+
)
22+
)
23+
)
1224
)

0 commit comments

Comments
 (0)