Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions assemblyscript/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7650,15 +7650,25 @@ export class Compiler extends DiagnosticEmitter {
}

let callPrepareStmts: ExpressionRef[] = new Array();
let stmts: ExpressionRef[] = new Array();

let functionAddrExprForEnv = module.tryCopyTrivialExpression(functionArg);
let allTrivial = functionAddrExprForEnv != 0;
if (operands && allTrivial) {
for (let i = 0; i < numOperands; ++i) {
if (!module.isTrivialExpression(operands[i])) {
allTrivial = false;
break;
}
}
}
let functionAddrExprForIndex: ExpressionRef;
if (functionAddrExprForEnv) {
if (allTrivial) {
functionAddrExprForIndex = functionArg;
} else {
const tempFunctionAddrLocal = this.currentFlow.getTempLocal(Type.i32);
const setExpr = module.local_set(tempFunctionAddrLocal.index, functionArg, false);
callPrepareStmts.push(setExpr);
stmts.push(setExpr);
functionAddrExprForEnv = module.local_get(tempFunctionAddrLocal.index, TypeRef.I32);
functionAddrExprForIndex = module.local_get(tempFunctionAddrLocal.index, TypeRef.I32);
}
Expand Down Expand Up @@ -7690,8 +7700,8 @@ export class Compiler extends DiagnosticEmitter {
signature.resultRefs
);
this.currentType = returnType;

return expr;
stmts.push(expr);
return module.flatten(stmts, returnType.toRef());
}

private compileCommaExpression(
Expand Down
11 changes: 9 additions & 2 deletions assemblyscript/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,13 @@ export class Module {

/** Makes a copy of a trivial expression (doesn't contain subexpressions). Returns `0` if non-trivial. */
tryCopyTrivialExpression(expr: ExpressionRef): ExpressionRef {
if (this.isTrivialExpression(expr)) {
return this.copyExpression(expr);
}
return 0;
}

isTrivialExpression(expr: ExpressionRef): bool {
switch (binaryen._BinaryenExpressionGetId(expr)) {
case ExpressionId.LocalGet:
case ExpressionId.GlobalGet:
Expand All @@ -1276,9 +1283,9 @@ export class Module {
case ExpressionId.Unreachable:
case ExpressionId.DataDrop:
case ExpressionId.RefNull:
return this.copyExpression(expr);
return true;
}
return 0;
return false;
}

/** Makes a copy of any expression including all subexpressions. */
Expand Down
232 changes: 120 additions & 112 deletions tests/frontend/compiler/assert-nonnull.wat
Original file line number Diff line number Diff line change
Expand Up @@ -460,40 +460,42 @@
(local $1 i32)
(local $2 i32)
(return
(call_indirect (type $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(local.get $fn)
)
)
(then
(local.get $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(local.get $fn)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 35)
(i32.const 10)
)
(unreachable)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 35)
(i32.const 10)
)
(unreachable)
)
)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
)
(call_indirect (type $1)
(block (result i32)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
)
)
Expand Down Expand Up @@ -550,40 +552,42 @@
(if (result i32)
(local.tee $3
(call $~lib/rt/__localtostack
(call_indirect (type $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(local.get $fn)
)
)
(then
(local.get $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(local.get $fn)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 44)
(i32.const 10)
)
(unreachable)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 44)
(i32.const 10)
)
(unreachable)
)
)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
)
(call_indirect (type $1)
(block (result i32)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
)
)
Expand Down Expand Up @@ -612,44 +616,46 @@
(local $1 i32)
(local $2 i32)
(return
(call_indirect (type $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(call $assert-nonnull/Foo#get:baz
(call $~lib/rt/__tmptostack
(local.get $foo)
)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(call $assert-nonnull/Foo#get:baz
(call $~lib/rt/__tmptostack
(local.get $foo)
)
)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 48)
(i32.const 10)
)
(unreachable)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 48)
(i32.const 10)
)
(unreachable)
)
)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
)
(call_indirect (type $1)
(block (result i32)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
)
)
Expand All @@ -662,44 +668,46 @@
(if (result i32)
(local.tee $3
(call $~lib/rt/__localtostack
(call_indirect (type $1)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(call $assert-nonnull/Foo#get:baz
(call $~lib/rt/__tmptostack
(local.get $foo)
)
(block (result i32)
(local.set $2
(if (result i32)
(local.tee $1
(call $~lib/rt/__localtostack
(call $assert-nonnull/Foo#get:baz
(call $~lib/rt/__tmptostack
(local.get $foo)
)
)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 52)
(i32.const 10)
)
(unreachable)
)
(then
(local.get $1)
)
(else
(call $~lib/builtins/abort
(i32.const 32)
(i32.const 160)
(i32.const 52)
(i32.const 10)
)
(unreachable)
)
)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
)
(call_indirect (type $1)
(block (result i32)
(call $~lib/rt/closure/setClosureEnv
(i32.load offset=4
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
(global.set $~argumentsLength
(i32.const 0)
)
(i32.load
(local.get $2)
)
)
)
)
Expand Down
Loading
Loading