Skip to content

Commit dbe3384

Browse files
authored
asm2wasm: properly infer return type of a call inside a sequence (#1006)
1 parent 647271a commit dbe3384

8 files changed

+134
-14
lines changed

src/asm2wasm.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,14 @@ class Asm2WasmBuilder {
478478
}
479479

480480
FunctionType* getFunctionType(Ref parent, ExpressionList& operands) {
481-
// generate signature
482-
WasmType result = !!parent ? detectWasmType(parent, nullptr) : none;
481+
WasmType result = none;
482+
if (!!parent) {
483+
// if the parent is a seq, we cannot be the last element in it (we would have a coercion, which would be
484+
// the parent), so we must be (us, somethingElse), and so our return is void
485+
if (parent[0] != SEQ) {
486+
result = detectWasmType(parent, nullptr);
487+
}
488+
}
483489
return ensureFunctionType(getSig(result, operands), &wasm);
484490
}
485491

test/unit.asm.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,23 +703,35 @@ function asm(global, env, buffer) {
703703
return x | 0;
704704
}
705705

706+
function indirectInSequence() {
707+
var i1 = 0;
708+
// this indirect call should have the right type, vi
709+
i1 = (FUNCTION_TABLE_vi[1 & 7](0), 1);
710+
}
711+
706712
function keepAlive() {
707713
sqrts(3.14159);
708714
f2u(100.0);
709715
f2s(100.0);
710716
autoDrop(52) | 0;
717+
indirectInSequence();
711718
}
712719

713720
function v() {
714721
}
715722
function vi(x) {
716723
x = x | 0;
717724
}
725+
function ii(x) {
726+
x = x | 0;
727+
return x | 0;
728+
}
718729

719730
var FUNCTION_TABLE_a = [ v, big_negative, v, v ];
720731
var FUNCTION_TABLE_b = [ w, w, importedDoubles, w ];
721732
var FUNCTION_TABLE_c = [ z, cneg, z, z, z, z, z, z ];
722733
var FUNCTION_TABLE_vi = [ vi, vi, vi, vi, vi, vi, vi, vi ];
734+
var FUNCTION_TABLE_ii = [ ii ];
723735

724736
return { big_negative: big_negative, pick: forgetMe, pick: exportMe, doubleCompares: doubleCompares, intOps: intOps, conversions: conversions, switcher: switcher, frem: frem, big_uint_div_u: big_uint_div_u, fr: fr, negZero: negZero, neg: neg, smallCompare: smallCompare, cneg_nosemicolon: cneg_nosemicolon, forLoop: forLoop, ceiling_32_64: ceiling_32_64, aborts: aborts, continues: continues, bitcasts: bitcasts, recursiveBlockMerging: recursiveBlockMerging, lb: lb, zeroInit: zeroInit, phi: phi, smallIf: smallIf, dropCall: dropCall, useSetGlobal: useSetGlobal, usesSetGlobal2: usesSetGlobal2, breakThroughMany: breakThroughMany, ifChainEmpty: ifChainEmpty, heap8NoShift: heap8NoShift, conditionalTypeFun: conditionalTypeFun, loadSigned: loadSigned, globalOpts: globalOpts, dropCallImport: dropCallImport, loophi: loophi, loophi2: loophi2, relooperJumpThreading: relooperJumpThreading, relooperJumpThreading__ZN4game14preloadweaponsEv: relooperJumpThreading__ZN4game14preloadweaponsEv, __Z12multi_varargiz: __Z12multi_varargiz, jumpThreadDrop: jumpThreadDrop, dropIgnoredImportInIf: dropIgnoredImportInIf, dropIgnoredImportsInIf: dropIgnoredImportsInIf, relooperJumpThreading_irreducible: relooperJumpThreading_irreducible, store_fround: store_fround, exportedNumber: 42, relocatableAndModules: relocatableAndModules, exported_f32_user: exported_f32_user, keepAlive: keepAlive };
725737
}

test/unit.fromasm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
1818
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1919
(import "env" "memory" (memory $0 256 256))
20-
(import "env" "table" (table 24 24 anyfunc))
20+
(import "env" "table" (table 25 25 anyfunc))
2121
(import "env" "memoryBase" (global $memoryBase i32))
2222
(import "env" "tableBase" (global $tableBase i32))
2323
(global $Int (mut i32) (i32.const 0))
2424
(global $Double (mut f64) (f64.const 0))
2525
(global $n (mut i32) (get_global $n$asm2wasm$import))
2626
(global $exportedNumber i32 (i32.const 42))
27-
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi)
27+
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi $ii)
2828
(data (get_global $memoryBase) "unit.asm.js")
2929
(export "big_negative" (func $big_negative))
3030
(export "pick" (func $big_negative))
@@ -1170,6 +1170,12 @@
11701170
)
11711171
(get_local $0)
11721172
)
1173+
(func $indirectInSequence
1174+
(call_indirect $FUNCSIG$vi
1175+
(i32.const 0)
1176+
(i32.const 17)
1177+
)
1178+
)
11731179
(func $keepAlive
11741180
(drop
11751181
(call $sqrts
@@ -1191,10 +1197,14 @@
11911197
(i32.const 52)
11921198
)
11931199
)
1200+
(call $indirectInSequence)
11941201
)
11951202
(func $vi (param $0 i32)
11961203
(nop)
11971204
)
1205+
(func $ii (param $0 i32) (result i32)
1206+
(get_local $0)
1207+
)
11981208
(func $legalstub$fr (param $0 f64)
11991209
(call $fr
12001210
(f32.demote/f64

test/unit.fromasm.clamp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
(import "env" "return_int" (func $return_int (result i32)))
1616
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1717
(import "env" "memory" (memory $0 256 256))
18-
(import "env" "table" (table 24 24 anyfunc))
18+
(import "env" "table" (table 25 25 anyfunc))
1919
(import "env" "memoryBase" (global $memoryBase i32))
2020
(import "env" "tableBase" (global $tableBase i32))
2121
(global $Int (mut i32) (i32.const 0))
2222
(global $Double (mut f64) (f64.const 0))
2323
(global $n (mut i32) (get_global $n$asm2wasm$import))
2424
(global $exportedNumber i32 (i32.const 42))
25-
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi)
25+
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi $ii)
2626
(data (get_global $memoryBase) "unit.asm.js")
2727
(export "big_negative" (func $big_negative))
2828
(export "pick" (func $big_negative))
@@ -1194,6 +1194,12 @@
11941194
)
11951195
(get_local $0)
11961196
)
1197+
(func $indirectInSequence
1198+
(call_indirect $FUNCSIG$vi
1199+
(i32.const 0)
1200+
(i32.const 17)
1201+
)
1202+
)
11971203
(func $keepAlive
11981204
(drop
11991205
(call $sqrts
@@ -1215,10 +1221,14 @@
12151221
(i32.const 52)
12161222
)
12171223
)
1224+
(call $indirectInSequence)
12181225
)
12191226
(func $vi (param $0 i32)
12201227
(nop)
12211228
)
1229+
(func $ii (param $0 i32) (result i32)
1230+
(get_local $0)
1231+
)
12221232
(func $legalstub$fr (param $0 f64)
12231233
(call $fr
12241234
(f32.demote/f64

test/unit.fromasm.clamp.no-opts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
(import "env" "return_int" (func $return_int (result i32)))
2020
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2121
(import "env" "memory" (memory $0 256 256))
22-
(import "env" "table" (table 24 24 anyfunc))
22+
(import "env" "table" (table 25 25 anyfunc))
2323
(import "env" "memoryBase" (global $memoryBase i32))
2424
(import "env" "tableBase" (global $tableBase i32))
2525
(global $t (mut f64) (get_global $t$asm2wasm$import))
@@ -30,7 +30,7 @@
3030
(global $n (mut i32) (get_global $n$asm2wasm$import))
3131
(global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import))
3232
(global $exportedNumber i32 (i32.const 42))
33-
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi)
33+
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi $ii)
3434
(export "big_negative" (func $big_negative))
3535
(export "pick" (func $exportMe))
3636
(export "doubleCompares" (func $doubleCompares))
@@ -1959,6 +1959,24 @@
19591959
(get_local $x)
19601960
)
19611961
)
1962+
(func $indirectInSequence
1963+
(local $i1 i32)
1964+
(set_local $i1
1965+
(block i32
1966+
(call_indirect $FUNCSIG$vi
1967+
(i32.const 0)
1968+
(i32.add
1969+
(i32.and
1970+
(i32.const 1)
1971+
(i32.const 7)
1972+
)
1973+
(i32.const 16)
1974+
)
1975+
)
1976+
(i32.const 1)
1977+
)
1978+
)
1979+
)
19621980
(func $keepAlive
19631981
(drop
19641982
(call $sqrts
@@ -1980,13 +1998,19 @@
19801998
(i32.const 52)
19811999
)
19822000
)
2001+
(call $indirectInSequence)
19832002
)
19842003
(func $v
19852004
(nop)
19862005
)
19872006
(func $vi (param $x i32)
19882007
(nop)
19892008
)
2009+
(func $ii (param $x i32) (result i32)
2010+
(return
2011+
(get_local $x)
2012+
)
2013+
)
19902014
(func $legalstub$fr (param $0 f64)
19912015
(call $fr
19922016
(f32.demote/f64

test/unit.fromasm.imprecise

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
(import "env" "return_int" (func $return_int (result i32)))
1616
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1717
(import "env" "memory" (memory $0 256 256))
18-
(import "env" "table" (table 24 24 anyfunc))
18+
(import "env" "table" (table 25 25 anyfunc))
1919
(import "env" "memoryBase" (global $memoryBase i32))
2020
(import "env" "tableBase" (global $tableBase i32))
2121
(global $Int (mut i32) (i32.const 0))
2222
(global $Double (mut f64) (f64.const 0))
2323
(global $n (mut i32) (get_global $n$asm2wasm$import))
2424
(global $exportedNumber i32 (i32.const 42))
25-
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi)
25+
(elem (get_global $tableBase) $big_negative $big_negative $big_negative $big_negative $w $w $importedDoubles $w $fr $cneg $fr $fr $fr $fr $fr $fr $vi $vi $vi $vi $vi $vi $vi $vi $ii)
2626
(export "big_negative" (func $big_negative))
2727
(export "pick" (func $big_negative))
2828
(export "doubleCompares" (func $doubleCompares))
@@ -1143,6 +1143,12 @@
11431143
)
11441144
(get_local $0)
11451145
)
1146+
(func $indirectInSequence
1147+
(call_indirect $FUNCSIG$vi
1148+
(i32.const 0)
1149+
(i32.const 17)
1150+
)
1151+
)
11461152
(func $keepAlive
11471153
(drop
11481154
(call $sqrts
@@ -1164,10 +1170,14 @@
11641170
(i32.const 52)
11651171
)
11661172
)
1173+
(call $indirectInSequence)
11671174
)
11681175
(func $vi (param $0 i32)
11691176
(nop)
11701177
)
1178+
(func $ii (param $0 i32) (result i32)
1179+
(get_local $0)
1180+
)
11711181
(func $legalstub$fr (param $0 f64)
11721182
(call $fr
11731183
(f32.demote/f64

test/unit.fromasm.imprecise.no-opts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
(import "env" "return_int" (func $return_int (result i32)))
2020
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2121
(import "env" "memory" (memory $0 256 256))
22-
(import "env" "table" (table 24 24 anyfunc))
22+
(import "env" "table" (table 25 25 anyfunc))
2323
(import "env" "memoryBase" (global $memoryBase i32))
2424
(import "env" "tableBase" (global $tableBase i32))
2525
(global $t (mut f64) (get_global $t$asm2wasm$import))
@@ -30,7 +30,7 @@
3030
(global $n (mut i32) (get_global $n$asm2wasm$import))
3131
(global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import))
3232
(global $exportedNumber i32 (i32.const 42))
33-
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi)
33+
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi $ii)
3434
(export "big_negative" (func $big_negative))
3535
(export "pick" (func $exportMe))
3636
(export "doubleCompares" (func $doubleCompares))
@@ -1919,6 +1919,24 @@
19191919
(get_local $x)
19201920
)
19211921
)
1922+
(func $indirectInSequence
1923+
(local $i1 i32)
1924+
(set_local $i1
1925+
(block i32
1926+
(call_indirect $FUNCSIG$vi
1927+
(i32.const 0)
1928+
(i32.add
1929+
(i32.and
1930+
(i32.const 1)
1931+
(i32.const 7)
1932+
)
1933+
(i32.const 16)
1934+
)
1935+
)
1936+
(i32.const 1)
1937+
)
1938+
)
1939+
)
19221940
(func $keepAlive
19231941
(drop
19241942
(call $sqrts
@@ -1940,13 +1958,19 @@
19401958
(i32.const 52)
19411959
)
19421960
)
1961+
(call $indirectInSequence)
19431962
)
19441963
(func $v
19451964
(nop)
19461965
)
19471966
(func $vi (param $x i32)
19481967
(nop)
19491968
)
1969+
(func $ii (param $x i32) (result i32)
1970+
(return
1971+
(get_local $x)
1972+
)
1973+
)
19501974
(func $legalstub$fr (param $0 f64)
19511975
(call $fr
19521976
(f32.demote/f64

test/unit.fromasm.no-opts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
2222
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2323
(import "env" "memory" (memory $0 256 256))
24-
(import "env" "table" (table 24 24 anyfunc))
24+
(import "env" "table" (table 25 25 anyfunc))
2525
(import "env" "memoryBase" (global $memoryBase i32))
2626
(import "env" "tableBase" (global $tableBase i32))
2727
(global $t (mut f64) (get_global $t$asm2wasm$import))
@@ -32,7 +32,7 @@
3232
(global $n (mut i32) (get_global $n$asm2wasm$import))
3333
(global $STACKTOP (mut i32) (get_global $STACKTOP$asm2wasm$import))
3434
(global $exportedNumber i32 (i32.const 42))
35-
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi)
35+
(elem (get_global $tableBase) $v $big_negative $v $v $w $w $importedDoubles $w $z $cneg $z $z $z $z $z $z $vi $vi $vi $vi $vi $vi $vi $vi $ii)
3636
(export "big_negative" (func $big_negative))
3737
(export "pick" (func $exportMe))
3838
(export "doubleCompares" (func $doubleCompares))
@@ -1935,6 +1935,24 @@
19351935
(get_local $x)
19361936
)
19371937
)
1938+
(func $indirectInSequence
1939+
(local $i1 i32)
1940+
(set_local $i1
1941+
(block i32
1942+
(call_indirect $FUNCSIG$vi
1943+
(i32.const 0)
1944+
(i32.add
1945+
(i32.and
1946+
(i32.const 1)
1947+
(i32.const 7)
1948+
)
1949+
(i32.const 16)
1950+
)
1951+
)
1952+
(i32.const 1)
1953+
)
1954+
)
1955+
)
19381956
(func $keepAlive
19391957
(drop
19401958
(call $sqrts
@@ -1956,13 +1974,19 @@
19561974
(i32.const 52)
19571975
)
19581976
)
1977+
(call $indirectInSequence)
19591978
)
19601979
(func $v
19611980
(nop)
19621981
)
19631982
(func $vi (param $x i32)
19641983
(nop)
19651984
)
1985+
(func $ii (param $x i32) (result i32)
1986+
(return
1987+
(get_local $x)
1988+
)
1989+
)
19661990
(func $legalstub$fr (param $0 f64)
19671991
(call $fr
19681992
(f32.demote/f64

0 commit comments

Comments
 (0)