Skip to content

Commit 3dd6930

Browse files
authored
Properly propagate existing conditionals in flows (AssemblyScript#844)
1 parent c165ee5 commit 3dd6930

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

src/flow.ts

+2
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ export class Flow {
521521

522522
/** Inherits categorical flags as conditional flags from the specified flow (e.g. then without else). */
523523
inheritConditional(other: Flow): void {
524+
this.set(other.flags & FlowFlags.ANY_CONDITIONAL);
524525
if (other.is(FlowFlags.RETURNS)) {
525526
this.set(FlowFlags.CONDITIONALLY_RETURNS);
526527
}
@@ -539,6 +540,7 @@ export class Flow {
539540
var localFlags = other.localFlags;
540541
for (let i = 0, k = localFlags.length; i < k; ++i) {
541542
let flags = localFlags[i];
543+
this.setLocalFlag(i, flags & LocalFlags.ANY_CONDITIONAL);
542544
if (flags & LocalFlags.RETAINED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETAINED);
543545
if (flags & LocalFlags.READFROM) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_READFROM);
544546
if (flags & LocalFlags.WRITTENTO) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_WRITTENTO);

tests/compiler/continue.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"asc_flags": [
3+
"--runtime none"
4+
]
5+
}

tests/compiler/continue.optimized.wat

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(module
2+
(type $FUNCSIG$vi (func (param i32)))
3+
(type $FUNCSIG$v (func))
4+
(memory $0 0)
5+
(export "memory" (memory $0))
6+
(export "testInherit" (func $continue/testInherit))
7+
(func $continue/testInherit (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
8+
(local $1 i32)
9+
block $break|0
10+
loop $loop|0
11+
local.get $1
12+
i32.const 10
13+
i32.ge_s
14+
br_if $break|0
15+
block $continue|0
16+
local.get $0
17+
i32.const 0
18+
local.get $1
19+
i32.const 5
20+
i32.eq
21+
select
22+
br_if $continue|0
23+
end
24+
local.get $1
25+
i32.const 1
26+
i32.add
27+
local.set $1
28+
br $loop|0
29+
end
30+
unreachable
31+
end
32+
)
33+
(func $null (; 1 ;) (type $FUNCSIG$v)
34+
nop
35+
)
36+
)

tests/compiler/continue.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// see https://github.com/AssemblyScript/assemblyscript/issues/813
2+
export function testInherit(b: bool): void {
3+
for (let i = 0; i < 10; i++) {
4+
if (b) {
5+
if (i == 5) {
6+
continue;
7+
}
8+
// inheritConditional
9+
}
10+
// yielding a continue block
11+
}
12+
}

tests/compiler/continue.untouched.wat

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(module
2+
(type $FUNCSIG$vi (func (param i32)))
3+
(type $FUNCSIG$v (func))
4+
(memory $0 0)
5+
(table $0 1 funcref)
6+
(elem (i32.const 0) $null)
7+
(export "memory" (memory $0))
8+
(export "testInherit" (func $continue/testInherit))
9+
(func $continue/testInherit (; 0 ;) (type $FUNCSIG$vi) (param $0 i32)
10+
(local $1 i32)
11+
block $break|0
12+
i32.const 0
13+
local.set $1
14+
loop $loop|0
15+
local.get $1
16+
i32.const 10
17+
i32.lt_s
18+
i32.eqz
19+
br_if $break|0
20+
block $continue|0
21+
local.get $0
22+
if
23+
local.get $1
24+
i32.const 5
25+
i32.eq
26+
if
27+
br $continue|0
28+
end
29+
end
30+
end
31+
local.get $1
32+
i32.const 1
33+
i32.add
34+
local.set $1
35+
br $loop|0
36+
end
37+
unreachable
38+
end
39+
)
40+
(func $null (; 1 ;) (type $FUNCSIG$v)
41+
)
42+
)

0 commit comments

Comments
 (0)