Skip to content

Commit 4d46a7e

Browse files
authored
fix import type detection of calls in comma operators; when the parent is a comma, it can't be a coersion (or that would have been the parent), so there is no coercion, so the result type is none (#1115)
1 parent 203b7f7 commit 4d46a7e

11 files changed

Lines changed: 156 additions & 67 deletions

src/asm2wasm.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,20 @@ class Asm2WasmBuilder {
476476
}
477477
}
478478

479-
FunctionType* getFunctionType(Ref parent, ExpressionList& operands) {
480-
WasmType result = none;
479+
WasmType getResultTypeOfCallUsingParent(Ref parent, AsmData* data) {
480+
auto result = none;
481481
if (!!parent) {
482482
// if the parent is a seq, we cannot be the last element in it (we would have a coercion, which would be
483483
// the parent), so we must be (us, somethingElse), and so our return is void
484484
if (parent[0] != SEQ) {
485-
result = detectWasmType(parent, nullptr);
485+
result = detectWasmType(parent, data);
486486
}
487487
}
488+
return result;
489+
}
490+
491+
FunctionType* getFunctionType(Ref parent, ExpressionList& operands, AsmData* data) {
492+
WasmType result = getResultTypeOfCallUsingParent(parent, data);
488493
return ensureFunctionType(getSig(result, operands), &wasm);
489494
}
490495

@@ -2189,7 +2194,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
21892194
if (tableCall) {
21902195
auto specific = ret->dynCast<CallIndirect>();
21912196
// note that we could also get the type from the suffix of the name, e.g., mftCall_vi
2192-
auto* fullType = getFunctionType(astStackHelper.getParent(), specific->operands);
2197+
auto* fullType = getFunctionType(astStackHelper.getParent(), specific->operands, &asmData);
21932198
specific->fullType = fullType->name;
21942199
specific->type = fullType->result;
21952200
}
@@ -2202,8 +2207,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
22022207
// this is important as we run the optimizer on functions before we get
22032208
// to finalizeCalls (which we can only do once we've read all the functions,
22042209
// and we optimize in parallel starting earlier).
2205-
Ref parent = astStackHelper.getParent();
2206-
callImport->type = !!parent ? detectWasmType(parent, &asmData) : none;
2210+
callImport->type = getResultTypeOfCallUsingParent(astStackHelper.getParent(), &asmData);
22072211
noteImportedFunctionCall(ast, callImport->type, callImport);
22082212
}
22092213
return ret;
@@ -2217,7 +2221,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
22172221
for (unsigned i = 0; i < args->size(); i++) {
22182222
ret->operands.push_back(process(args[i]));
22192223
}
2220-
auto* fullType = getFunctionType(astStackHelper.getParent(), ret->operands);
2224+
auto* fullType = getFunctionType(astStackHelper.getParent(), ret->operands, &asmData);
22212225
ret->fullType = fullType->name;
22222226
ret->type = fullType->result;
22232227
// we don't know the table offset yet. emit target = target + callImport(tableName), which we fix up later when we know how asm function tables are layed out inside the wasm table.

test/unit.asm.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function asm(global, env, buffer) {
1919
var print = env.print;
2020
var h = env.h;
2121
var return_int = env.return_int;
22+
var emscripten_log = env.emscripten_log;
2223

2324
var HEAP8 = new global.Int8Array(buffer);
2425
var HEAP16 = new global.Int16Array(buffer);
@@ -718,13 +719,19 @@ function asm(global, env, buffer) {
718719
FUNCTION_TABLE_vi[(Int = 1) & 7](i1 | 0);
719720
}
720721

722+
function call_emscripten_log() {
723+
// emscripten_log has no return value, don't let the conditional after the comma confuse you
724+
emscripten_log(), 2 ? abort() | 0 : 3;
725+
}
726+
721727
function keepAlive() {
722728
sqrts(3.14159);
723729
f2u(100.0);
724730
f2s(100.0);
725731
autoDrop(52) | 0;
726732
indirectInSequence();
727733
emterpretify_assertions_safeHeap();
734+
call_emscripten_log();
728735
}
729736

730737
function v() {

test/unit.fromasm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
(import "env" "print" (func $print (param i32)))
1515
(import "env" "h" (func $h (param i32)))
1616
(import "env" "return_int" (func $return_int (result i32)))
17+
(import "env" "emscripten_log" (func $emscripten_log))
1718
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
1819
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1920
(import "env" "memory" (memory $0 256 256))
@@ -1196,6 +1197,16 @@
11961197
)
11971198
)
11981199
)
1200+
(func $call_emscripten_log
1201+
(call $emscripten_log)
1202+
(drop
1203+
(call $f64-to-int
1204+
(call $abort
1205+
(f64.const 0)
1206+
)
1207+
)
1208+
)
1209+
)
11991210
(func $keepAlive
12001211
(drop
12011212
(call $sqrts
@@ -1219,6 +1230,7 @@
12191230
)
12201231
(call $indirectInSequence)
12211232
(call $emterpretify_assertions_safeHeap)
1233+
(call $call_emscripten_log)
12221234
)
12231235
(func $vi (param $0 i32)
12241236
(nop)

test/unit.fromasm.clamp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(import "env" "print" (func $print (param i32)))
1414
(import "env" "h" (func $h (param i32)))
1515
(import "env" "return_int" (func $return_int (result i32)))
16+
(import "env" "emscripten_log" (func $emscripten_log))
1617
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1718
(import "env" "memory" (memory $0 256 256))
1819
(import "env" "table" (table 25 25 anyfunc))
@@ -1220,6 +1221,16 @@
12201221
)
12211222
)
12221223
)
1224+
(func $call_emscripten_log
1225+
(call $emscripten_log)
1226+
(drop
1227+
(call $f64-to-int
1228+
(call $abort
1229+
(f64.const 0)
1230+
)
1231+
)
1232+
)
1233+
)
12231234
(func $keepAlive
12241235
(drop
12251236
(call $sqrts
@@ -1243,6 +1254,7 @@
12431254
)
12441255
(call $indirectInSequence)
12451256
(call $emterpretify_assertions_safeHeap)
1257+
(call $call_emscripten_log)
12461258
)
12471259
(func $vi (param $0 i32)
12481260
(nop)

test/unit.fromasm.clamp.no-opts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
(import "env" "print" (func $print (param i32)))
1818
(import "env" "h" (func $h (param i32)))
1919
(import "env" "return_int" (func $return_int (result i32)))
20+
(import "env" "emscripten_log" (func $emscripten_log))
2021
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2122
(import "env" "memory" (memory $0 256 256))
2223
(import "env" "table" (table 25 25 anyfunc))
@@ -2010,6 +2011,22 @@
20102011
)
20112012
)
20122013
)
2014+
(func $call_emscripten_log
2015+
(call $emscripten_log)
2016+
(if
2017+
(i32.const 2)
2018+
(drop
2019+
(call $f64-to-int
2020+
(call $abort
2021+
(f64.const 0)
2022+
)
2023+
)
2024+
)
2025+
(drop
2026+
(i32.const 3)
2027+
)
2028+
)
2029+
)
20132030
(func $keepAlive
20142031
(drop
20152032
(call $sqrts
@@ -2033,6 +2050,7 @@
20332050
)
20342051
(call $indirectInSequence)
20352052
(call $emterpretify_assertions_safeHeap)
2053+
(call $call_emscripten_log)
20362054
)
20372055
(func $v
20382056
(nop)

test/unit.fromasm.imprecise

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(import "env" "print" (func $print (param i32)))
1414
(import "env" "h" (func $h (param i32)))
1515
(import "env" "return_int" (func $return_int (result i32)))
16+
(import "env" "emscripten_log" (func $emscripten_log))
1617
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
1718
(import "env" "memory" (memory $0 256 256))
1819
(import "env" "table" (table 25 25 anyfunc))
@@ -1169,6 +1170,16 @@
11691170
)
11701171
)
11711172
)
1173+
(func $call_emscripten_log
1174+
(call $emscripten_log)
1175+
(drop
1176+
(i32.trunc_s/f64
1177+
(call $abort
1178+
(f64.const 0)
1179+
)
1180+
)
1181+
)
1182+
)
11721183
(func $keepAlive
11731184
(drop
11741185
(call $sqrts
@@ -1192,6 +1203,7 @@
11921203
)
11931204
(call $indirectInSequence)
11941205
(call $emterpretify_assertions_safeHeap)
1206+
(call $call_emscripten_log)
11951207
)
11961208
(func $vi (param $0 i32)
11971209
(nop)

test/unit.fromasm.imprecise.no-opts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
(import "env" "print" (func $print (param i32)))
1818
(import "env" "h" (func $h (param i32)))
1919
(import "env" "return_int" (func $return_int (result i32)))
20+
(import "env" "emscripten_log" (func $emscripten_log))
2021
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2122
(import "env" "memory" (memory $0 256 256))
2223
(import "env" "table" (table 25 25 anyfunc))
@@ -1970,6 +1971,22 @@
19701971
)
19711972
)
19721973
)
1974+
(func $call_emscripten_log
1975+
(call $emscripten_log)
1976+
(if
1977+
(i32.const 2)
1978+
(drop
1979+
(i32.trunc_s/f64
1980+
(call $abort
1981+
(f64.const 0)
1982+
)
1983+
)
1984+
)
1985+
(drop
1986+
(i32.const 3)
1987+
)
1988+
)
1989+
)
19731990
(func $keepAlive
19741991
(drop
19751992
(call $sqrts
@@ -1993,6 +2010,7 @@
19932010
)
19942011
(call $indirectInSequence)
19952012
(call $emterpretify_assertions_safeHeap)
2013+
(call $call_emscripten_log)
19962014
)
19972015
(func $v
19982016
(nop)

test/unit.fromasm.no-opts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
(import "env" "print" (func $print (param i32)))
1919
(import "env" "h" (func $h (param i32)))
2020
(import "env" "return_int" (func $return_int (result i32)))
21+
(import "env" "emscripten_log" (func $emscripten_log))
2122
(import "asm2wasm" "f64-to-int" (func $f64-to-int (param f64) (result i32)))
2223
(import "asm2wasm" "f64-rem" (func $f64-rem (param f64 f64) (result f64)))
2324
(import "env" "memory" (memory $0 256 256))
@@ -1986,6 +1987,22 @@
19861987
)
19871988
)
19881989
)
1990+
(func $call_emscripten_log
1991+
(call $emscripten_log)
1992+
(if
1993+
(i32.const 2)
1994+
(drop
1995+
(call $f64-to-int
1996+
(call $abort
1997+
(f64.const 0)
1998+
)
1999+
)
2000+
)
2001+
(drop
2002+
(i32.const 3)
2003+
)
2004+
)
2005+
)
19892006
(func $keepAlive
19902007
(drop
19912008
(call $sqrts
@@ -2009,6 +2026,7 @@
20092026
)
20102027
(call $indirectInSequence)
20112028
(call $emterpretify_assertions_safeHeap)
2029+
(call $call_emscripten_log)
20122030
)
20132031
(func $v
20142032
(nop)
Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(module
2-
(type $FUNCSIG$ii (func (param i32) (result i32)))
3-
(import "env" "setTempRet0" (func $setTempRet0 (param i32) (result i32)))
2+
(type $FUNCSIG$vi (func (param i32)))
3+
(import "env" "setTempRet0" (func $setTempRet0 (param i32)))
44
(import "env" "memory" (memory $0 256 256))
55
(import "env" "table" (table 0 0 anyfunc))
66
(import "env" "memoryBase" (global $memoryBase i32))
@@ -13,23 +13,21 @@
1313
(local $$1$0 i32)
1414
(return
1515
(block (result i32)
16-
(drop
17-
(call $setTempRet0
18-
(i32.or
16+
(call $setTempRet0
17+
(i32.or
18+
(i32.add
1919
(i32.add
20-
(i32.add
21-
(i32.mul
22-
(get_local $$b$1)
23-
(get_local $$x_sroa_0_0_extract_trunc)
24-
)
25-
(get_local $$2)
20+
(i32.mul
21+
(get_local $$b$1)
22+
(get_local $$x_sroa_0_0_extract_trunc)
2623
)
27-
(get_local $$1$1)
28-
)
29-
(i32.and
30-
(get_local $$1$1)
31-
(i32.const 0)
24+
(get_local $$2)
3225
)
26+
(get_local $$1$1)
27+
)
28+
(i32.and
29+
(get_local $$1$1)
30+
(i32.const 0)
3331
)
3432
)
3533
)
@@ -44,10 +42,8 @@
4442
)
4543
)
4644
(func $test2
47-
(drop
48-
(call $setTempRet0
49-
(i32.const 10)
50-
)
45+
(call $setTempRet0
46+
(i32.const 10)
5147
)
5248
)
5349
)

0 commit comments

Comments
 (0)