summaryrefslogtreecommitdiff
path: root/src/emscripten-optimizer
diff options
context:
space:
mode:
Diffstat (limited to 'src/emscripten-optimizer')
-rw-r--r--src/emscripten-optimizer/optimizer-shared.cpp21
-rw-r--r--src/emscripten-optimizer/optimizer.h4
-rw-r--r--src/emscripten-optimizer/parser.h18
-rw-r--r--src/emscripten-optimizer/simple_ast.h29
4 files changed, 28 insertions, 44 deletions
diff --git a/src/emscripten-optimizer/optimizer-shared.cpp b/src/emscripten-optimizer/optimizer-shared.cpp
index 4466fb0e9..6831d81b0 100644
--- a/src/emscripten-optimizer/optimizer-shared.cpp
+++ b/src/emscripten-optimizer/optimizer-shared.cpp
@@ -14,7 +14,10 @@
* limitations under the License.
*/
+#include <limits>
+
#include "optimizer.h"
+#include "support/safe_integer.h"
using namespace cashew;
@@ -26,20 +29,6 @@ IString SIMD_INT8X16_CHECK("SIMD_Int8x16_check"),
SIMD_FLOAT32X4_CHECK("SIMD_Float32x4_check"),
SIMD_FLOAT64X2_CHECK("SIMD_Float64x2_check");
-bool isInteger(double x) {
- return fmod(x, 1) == 0;
-}
-
-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)) {
@@ -67,7 +56,7 @@ AsmType detectType(Ref node, AsmData *asmData, bool inVarDef, IString minifiedFr
switch (node[0]->getCString()[0]) {
case 'n': {
if (node[0] == NUM) {
- if (!isInteger(node[1]->getNumber())) return ASM_DOUBLE;
+ if (!wasm::isInteger(node[1]->getNumber())) return ASM_DOUBLE;
return ASM_INT;
} else if (node[0] == NAME) {
if (asmData) {
@@ -176,7 +165,7 @@ AsmSign detectSign(Ref node, IString minifiedFround) {
double value = node[1]->getNumber();
if (value < 0) return ASM_SIGNED;
if (value > uint32_t(-1) || fmod(value, 1) != 0) return ASM_NONSIGNED;
- if (value == int32_t(value)) return ASM_FLEXIBLE;
+ if (wasm::isSInteger32(value)) return ASM_FLEXIBLE;
return ASM_UNSIGNED;
} else if (type == NAME) {
return ASM_FLEXIBLE;
diff --git a/src/emscripten-optimizer/optimizer.h b/src/emscripten-optimizer/optimizer.h
index 451a9e286..684fc0164 100644
--- a/src/emscripten-optimizer/optimizer.h
+++ b/src/emscripten-optimizer/optimizer.h
@@ -116,10 +116,6 @@ struct AsmData {
}
};
-bool isInteger(double x);
-bool isInteger32(double x);
-int32_t toInteger32(double x);
-
extern cashew::IString ASM_FLOAT_ZERO;
extern cashew::IString SIMD_INT8X16_CHECK,
diff --git a/src/emscripten-optimizer/parser.h b/src/emscripten-optimizer/parser.h
index c805ca9f6..da3e6f5c7 100644
--- a/src/emscripten-optimizer/parser.h
+++ b/src/emscripten-optimizer/parser.h
@@ -22,13 +22,14 @@
#ifndef wasm_parser_h
#define wasm_parser_h
-#include <vector>
-#include <iostream>
#include <algorithm>
-
-#include <stdio.h>
+#include <cstdio>
+#include <iostream>
+#include <limits>
+#include <vector>
#include "istring.h"
+#include "support/safe_integer.h"
namespace cashew {
@@ -179,10 +180,6 @@ class Parser {
static bool hasChar(const char* list, char x) { while (*list) if (*list++ == x) return true; return false; }
- static bool is32Bit(double x) {
- return x == (int)x || x == (unsigned int)x;
- }
-
// An atomic fragment of something. Stops at a natural boundary.
enum FragType {
KEYWORD = 0,
@@ -249,7 +246,10 @@ class Parser {
// for valid asm.js input, the '.' should be enough, and for uglify
// in the emscripten optimizer pipeline, we use simple_ast where INT/DOUBLE
// is quite the same at this point anyhow
- type = (std::find(start, src, '.') == src && is32Bit(num)) ? INT : DOUBLE;
+ type = (std::find(start, src, '.') == src &&
+ (wasm::isSInteger32(num) || wasm::isUInteger32(num)))
+ ? INT
+ : DOUBLE;
assert(src > start);
} else if (hasChar(OPERATOR_INITS, *src)) {
switch (*src) {
diff --git a/src/emscripten-optimizer/simple_ast.h b/src/emscripten-optimizer/simple_ast.h
index 712845dea..73037815f 100644
--- a/src/emscripten-optimizer/simple_ast.h
+++ b/src/emscripten-optimizer/simple_ast.h
@@ -17,26 +17,25 @@
#ifndef wasm_simple_ast_h
#define wasm_simple_ast_h
-#include <assert.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <math.h>
-
-#include <vector>
-#include <ostream>
-#include <iostream>
+#include <algorithm>
+#include <cassert>
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <functional>
#include <iomanip>
+#include <iostream>
#include <limits>
-#include <functional>
-#include <algorithm>
+#include <ostream>
#include <set>
-#include <unordered_set>
#include <unordered_map>
+#include <unordered_set>
+#include <vector>
#include "parser.h"
-
#include "snprintf.h"
+#include "support/safe_integer.h"
#define err(str) fprintf(stderr, str "\n");
#define errv(str, ...) fprintf(stderr, str "\n", __VA_ARGS__);
@@ -870,8 +869,8 @@ struct JSPrinter {
} else {
// integer
assert(d >= 0);
- unsigned long long uu = (unsigned long long)d;
- if (uu == d) {
+ if (wasm::isUInteger64(d)) {
+ unsigned long long uu = wasm::toUInteger64(d);
bool asHex = e && !finalize;
snprintf(buffer, BUFFERSIZE-1, asHex ? "0x%llx" : "%llu", uu);
if (asHex) {