Skip to content

Commit

Permalink
fix: direct call optional parameter function in more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 committed Mar 28, 2023
1 parent 076d8a9 commit 24146f1
Show file tree
Hide file tree
Showing 8 changed files with 20,099 additions and 21,506 deletions.
9 changes: 8 additions & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,14 @@ 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);
if (unaryPrefixExpr.operator == Token.Minus || unaryPrefixExpr.operator == Token.Plus) {
// if expr can compile to const, +expr or -expr should also compile to const
return unaryPrefixExpr.operand.compilesToConst;
}
}
return false;
}
Expand Down
14 changes: 9 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6787,11 +6787,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

0 comments on commit 24146f1

Please sign in to comment.