summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-11-28 20:07:28 -0800
committerAlon Zakai <alonzakai@gmail.com>2015-11-28 20:16:52 -0800
commitd8f258e8e74b17d06ee7d94fc7c4dd3165819f0d (patch)
tree16c6706992f27a2e86fc9c112838aa168d2b637f
parent37b7e93a71e1a51fcb826cb2bce88f47bd396674 (diff)
downloadbinaryen-d8f258e8e74b17d06ee7d94fc7c4dd3165819f0d.tar.gz
binaryen-d8f258e8e74b17d06ee7d94fc7c4dd3165819f0d.tar.bz2
binaryen-d8f258e8e74b17d06ee7d94fc7c4dd3165819f0d.zip
be very careful when converting doubles to ints in asm2wasm
-rw-r--r--src/asm2wasm.h2
-rw-r--r--src/emscripten-optimizer/optimizer-shared.cpp6
-rw-r--r--src/emscripten-optimizer/optimizer.h2
-rw-r--r--test/emcc_O2_hello_world.wast4
-rw-r--r--test/emcc_hello_world.wast8
-rw-r--r--test/unit.asm.js4
-rw-r--r--test/unit.wast12
7 files changed, 29 insertions, 9 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h
index 8dcefec0e..51536663d 100644
--- a/src/asm2wasm.h
+++ b/src/asm2wasm.h
@@ -935,7 +935,7 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) {
double num = ast[1]->getNumber();
if (isInteger32(num)) {
ret->value.type = WasmType::i32;
- ret->value.i32 = num;
+ ret->value.i32 = toInteger32(num);
} else {
ret->value.type = WasmType::f64;
ret->value.f64 = num;
diff --git a/src/emscripten-optimizer/optimizer-shared.cpp b/src/emscripten-optimizer/optimizer-shared.cpp
index 24c19cd79..6a8bbf9b5 100644
--- a/src/emscripten-optimizer/optimizer-shared.cpp
+++ b/src/emscripten-optimizer/optimizer-shared.cpp
@@ -19,6 +19,12 @@ bool isInteger32(double x) {
return isInteger(x) && (x == (int32_t)x || x == (uint32_t)x);
}
+int32_t toInteger32(double x) {
+ if (x == (int32_t)x) return (int32_t)x;
+ assert(x == (uint32_t)x);
+ return (uint32_t)x;
+}
+
int parseInt(const char *str) {
int ret = *str - '0';
while (*(++str)) {
diff --git a/src/emscripten-optimizer/optimizer.h b/src/emscripten-optimizer/optimizer.h
index faef2cccb..12d0671eb 100644
--- a/src/emscripten-optimizer/optimizer.h
+++ b/src/emscripten-optimizer/optimizer.h
@@ -102,8 +102,8 @@ struct AsmData {
};
bool isInteger(double x);
-
bool isInteger32(double x);
+int32_t toInteger32(double x);
extern cashew::IString ASM_FLOAT_ZERO;
diff --git a/test/emcc_O2_hello_world.wast b/test/emcc_O2_hello_world.wast
index 3e580d2d4..66d772963 100644
--- a/test/emcc_O2_hello_world.wast
+++ b/test/emcc_O2_hello_world.wast
@@ -1730,7 +1730,7 @@
(get_local $i1)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -65)
)
(block
(set_local $i7
@@ -12453,7 +12453,7 @@
(get_local $i1)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -4096)
)
(block
(i32.store align=4
diff --git a/test/emcc_hello_world.wast b/test/emcc_hello_world.wast
index ae799429b..d33811b80 100644
--- a/test/emcc_hello_world.wast
+++ b/test/emcc_hello_world.wast
@@ -4510,7 +4510,7 @@
(get_local $$r)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -4096)
)
)
(if_else
@@ -16251,7 +16251,7 @@
(get_local $$0)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -1)
)
)
(set_local $$4
@@ -16349,7 +16349,7 @@
(get_local $$7)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -1)
)
)
(set_local $$17
@@ -20126,7 +20126,7 @@
(get_local $$bytes)
(i32.const 0)
)
- (i32.const -2147483648)
+ (i32.const -65)
)
)
(if_else
diff --git a/test/unit.asm.js b/test/unit.asm.js
index 1540beb72..cef639837 100644
--- a/test/unit.asm.js
+++ b/test/unit.asm.js
@@ -112,6 +112,10 @@ function () {
x = -x;
FUNCTION_TABLE_c[1 & 7](x);
}
+ function ___syscall_ret() {
+ var $0 = 0;
+ ($0>>>0) > 4294963200; // -4096
+ }
function z() {
}
diff --git a/test/unit.wast b/test/unit.wast
index c965d30b3..0c08b39c8 100644
--- a/test/unit.wast
+++ b/test/unit.wast
@@ -267,7 +267,7 @@
(set_local $x
(i32.and
(i32.div_u
- (i32.const -2147483648)
+ (i32.const -1)
(i32.const 2)
)
(i32.const -1)
@@ -349,6 +349,16 @@
)
)
)
+ (func $___syscall_ret
+ (local $$0 i32)
+ (i32.gt_u
+ (i32.shr_u
+ (get_local $$0)
+ (i32.const 0)
+ )
+ (i32.const -4096)
+ )
+ )
(func $z
(nop)
)