Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: direct call optional parameter function in more cases #2676

Closed
Closed
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
13 changes: 11 additions & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ export abstract class Node {
}

/** Tests whether this node is guaranteed to compile to a constant value. */
get compilesToConst(): bool {
isLiteral(): bool {
switch (this.kind) {
case NodeKind.Literal: {
switch ((<LiteralExpression>changetype<Node>(this)).literalKind) { // TS
Expand All @@ -788,7 +788,16 @@ export abstract class Node {
}
case NodeKind.Null:
case NodeKind.True:
case NodeKind.False: return true;
case NodeKind.False:
return true;
case NodeKind.UnaryPrefix: {
const unaryPrefixExpr = <UnaryPrefixExpression>changetype<Node>(this);
const op = unaryPrefixExpr.operator;
if (op == Token.Minus || op == Token.Plus || op == Token.Exclamation || op == Token.Tilde) {
// if expr can compile to const, +expr or -expr should also compile to const
return unaryPrefixExpr.operand.isLiteral();
}
}
}
return false;
}
Expand Down
16 changes: 10 additions & 6 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6807,7 +6807,7 @@ export class Compiler extends DiagnosticEmitter {
for (let i = numArguments; i < maxArguments; ++i) {
let initializer = parameterNodes[i].initializer;
if (initializer) {
if (initializer.compilesToConst) {
if (initializer.isLiteral()) {
operands.push(this.compileExpression(
initializer,
parameterTypes[i],
Expand All @@ -6818,11 +6818,15 @@ export class Compiler extends DiagnosticEmitter {
let resolved = this.resolver.lookupExpression(initializer, instance.flow, parameterTypes[i], ReportMode.Swallow);
if (resolved && resolved.kind == ElementKind.Global) {
let global = <Global>resolved;
if (this.compileGlobalLazy(global, initializer) && global.is(CommonFlags.Inlined)) {
operands.push(
this.compileInlineConstant(global, parameterTypes[i], Constraints.ConvImplicit)
);
continue;
if (this.compileGlobalLazy(global, initializer)) {
if (global.is(CommonFlags.Inlined)) {
operands.push(this.compileInlineConstant(global, parameterTypes[i], Constraints.ConvImplicit));
continue;
}
if (!global.hasDecorator(DecoratorFlags.Lazy)) {
operands.push(module.global_get(global.internalName, global.type.toRef()));
continue;
}
}
}
}
Expand Down
62 changes: 29 additions & 33 deletions tests/compiler/call-optional.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
(type $none_=>_none (func))
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $call-optional/optIndirect (mut i32) (i32.const 96))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $~lib/memory/__data_end i32 (i32.const 108))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 32876))
(global $~lib/memory/__heap_base i32 (i32.const 32876))
Expand All @@ -23,36 +23,11 @@
i32.add
return
)
(func $call-optional/opt@varargs (param $a i32) (param $b i32) (param $c i32) (result i32)
block $2of2
block $1of2
block $0of2
block $outOfRange
global.get $~argumentsLength
i32.const 1
i32.sub
br_table $0of2 $1of2 $2of2 $outOfRange
end
unreachable
end
i32.const -1
local.set $b
end
i32.const -2
local.set $c
end
local.get $a
local.get $b
local.get $c
call $call-optional/opt
)
(func $start:call-optional
i32.const 3
i32.const 0
i32.const 1
global.set $~argumentsLength
i32.const 0
call $call-optional/opt@varargs
i32.const -1
i32.const -2
call $call-optional/opt
i32.const 0
i32.eq
i32.eqz
Expand All @@ -66,10 +41,8 @@
end
i32.const 3
i32.const 4
i32.const 2
global.set $~argumentsLength
i32.const 0
call $call-optional/opt@varargs
i32.const -2
call $call-optional/opt
i32.const 5
i32.eq
i32.eqz
Expand Down Expand Up @@ -154,6 +127,29 @@
unreachable
end
)
(func $call-optional/opt@varargs (param $a i32) (param $b i32) (param $c i32) (result i32)
block $2of2
block $1of2
block $0of2
block $outOfRange
global.get $~argumentsLength
i32.const 1
i32.sub
br_table $0of2 $1of2 $2of2 $outOfRange
end
unreachable
end
i32.const -1
local.set $b
end
i32.const -2
local.set $c
end
local.get $a
local.get $b
local.get $c
call $call-optional/opt
)
(func $~start
call $start:call-optional
)
Expand Down
4 changes: 0 additions & 4 deletions tests/compiler/call-optional.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
i32.add
)
(func $~start
i32.const 1
global.set $~argumentsLength
i32.const 2
global.set $~argumentsLength
i32.const 1
global.set $~argumentsLength
i32.const 3
Expand Down
44 changes: 9 additions & 35 deletions tests/compiler/std/string.debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/native/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $~lib/string/String.MAX_LENGTH i32 (i32.const 536870910))
(global $~argumentsLength (mut i32) (i32.const 0))
(global $~lib/builtins/i32.MAX_VALUE i32 (i32.const 2147483647))
(global $~lib/util/string/__fixmulShift (mut i64) (i64.const 0))
(global $~lib/number/I32.MAX_VALUE i32 (i32.const 2147483647))
Expand Down Expand Up @@ -2858,24 +2858,6 @@
local.get $ptr
return
)
(func $~lib/string/String.fromCharCode@varargs (param $unit i32) (param $surr i32) (result i32)
block $1of1
block $0of1
block $outOfRange
global.get $~argumentsLength
i32.const 1
i32.sub
br_table $0of1 $1of1 $outOfRange
end
unreachable
end
i32.const -1
local.set $surr
end
local.get $unit
local.get $surr
call $~lib/string/String.fromCharCode
)
(func $~lib/rt/__newBuffer (param $size i32) (param $id i32) (param $data i32) (result i32)
(local $buffer i32)
local.get $size
Expand Down Expand Up @@ -12470,10 +12452,8 @@
unreachable
end
i32.const 0
i32.const 1
global.set $~argumentsLength
i32.const 0
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $55
global.get $~lib/memory/__stack_pointer
local.get $55
Expand All @@ -12496,10 +12476,8 @@
unreachable
end
i32.const 65600
i32.const 1
global.set $~argumentsLength
i32.const 0
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $55
global.get $~lib/memory/__stack_pointer
local.get $55
Expand All @@ -12522,10 +12500,8 @@
unreachable
end
i32.const 54
i32.const 1
global.set $~argumentsLength
i32.const 0
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $55
global.get $~lib/memory/__stack_pointer
local.get $55
Expand All @@ -12550,10 +12526,8 @@
i32.const 65536
i32.const 54
i32.add
i32.const 1
global.set $~argumentsLength
i32.const 0
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $55
global.get $~lib/memory/__stack_pointer
local.get $55
Expand Down
39 changes: 8 additions & 31 deletions tests/compiler/std/string.release.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2532,25 +2532,6 @@
memory.fill $0
local.get $1
)
(func $~lib/string/String.fromCharCode@varargs (param $0 i32) (result i32)
(local $1 i32)
block $1of1
block $0of1
block $outOfRange
global.get $~argumentsLength
i32.const 1
i32.sub
br_table $0of1 $1of1 $outOfRange
end
unreachable
end
i32.const -1
local.set $1
end
local.get $0
local.get $1
call $~lib/string/String.fromCharCode
)
(func $~lib/math/ipow32 (param $0 i32) (result i32)
(local $1 i32)
(local $2 i32)
Expand Down Expand Up @@ -10585,10 +10566,9 @@
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~argumentsLength
i32.const 0
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $5
Expand All @@ -10609,10 +10589,9 @@
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~argumentsLength
i32.const 65600
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $5
Expand All @@ -10633,10 +10612,9 @@
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~argumentsLength
i32.const 54
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $5
Expand All @@ -10657,10 +10635,9 @@
call $~lib/builtins/abort
unreachable
end
i32.const 1
global.set $~argumentsLength
i32.const 65590
call $~lib/string/String.fromCharCode@varargs
i32.const -1
call $~lib/string/String.fromCharCode
local.set $0
global.get $~lib/memory/__stack_pointer
local.tee $5
Expand Down
Loading