diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/asm2wasm.h | 12 | ||||
-rw-r--r-- | src/emscripten-optimizer/optimizer-shared.cpp | 21 | ||||
-rw-r--r-- | src/emscripten-optimizer/optimizer.h | 4 | ||||
-rw-r--r-- | src/emscripten-optimizer/parser.h | 18 | ||||
-rw-r--r-- | src/emscripten-optimizer/simple_ast.h | 29 | ||||
-rw-r--r-- | src/s2wasm.h | 86 | ||||
-rw-r--r-- | src/support/safe_integer.cpp | 75 | ||||
-rw-r--r-- | src/support/safe_integer.h | 34 | ||||
-rw-r--r-- | src/wasm-binary.h | 86 | ||||
-rw-r--r-- | src/wasm.h | 5 | ||||
-rw-r--r-- | src/wasm2asm.h | 4 |
11 files changed, 288 insertions, 86 deletions
diff --git a/src/asm2wasm.h b/src/asm2wasm.h index 11cb75b30..280624d9b 100644 --- a/src/asm2wasm.h +++ b/src/asm2wasm.h @@ -390,8 +390,9 @@ private: } if (ast[1] == MINUS && ast[2][0] == NUM) { double num = -ast[2][1]->getNumber(); - assert(isInteger32(num)); - return Literal((int32_t)num); + if (isSInteger32(num)) return Literal((int32_t)num); + if (isUInteger32(num)) return Literal((uint32_t)num); + assert(false && "expected signed or unsigned int32"); } if (ast[1] == PLUS && ast[2][0] == UNARY_PREFIX && ast[2][1] == MINUS && ast[2][2][0] == NUM) { return Literal((double)-ast[2][2][1]->getNumber()); @@ -917,9 +918,12 @@ Function* Asm2WasmBuilder::processFunction(Ref ast) { } else if (what == NUM) { auto ret = allocator.alloc<Const>(); double num = ast[1]->getNumber(); - if (isInteger32(num)) { + if (isSInteger32(num)) { ret->value.type = WasmType::i32; - ret->value.i32 = toInteger32(num); + ret->value.i32 = toSInteger32(num); + } else if (isUInteger32(num)) { + ret->value.type = WasmType::i32; + ret->value.i32 = toUInteger32(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 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) { diff --git a/src/s2wasm.h b/src/s2wasm.h index bd3f12e73..b99a71159 100644 --- a/src/s2wasm.h +++ b/src/s2wasm.h @@ -160,19 +160,36 @@ private: } int32_t getInt() { - int32_t ret = 0; + const char* loc = s; + uint32_t value = 0; bool neg = false; - if (*s == '-') { + if (*loc == '-') { neg = true; - s++; + loc++; } - while (isdigit(*s)) { - ret *= 10; - ret += (*s - '0'); - s++; + while (isdigit(*loc)) { + uint32_t digit = *loc - '0'; + if (value > std::numeric_limits<uint32_t>::max() / 10) { + abort_on("uint32_t overflow"); + } + value *= 10; + if (value > std::numeric_limits<uint32_t>::max() - digit) { + abort_on("uint32_t overflow"); + } + value += digit; + loc++; } - if (neg) ret = -ret; - return ret; + if (neg) { + uint32_t positive_int_min = + (uint32_t) - (1 + std::numeric_limits<int32_t>::min()) + (uint32_t)1; + if (value > positive_int_min) { + abort_on("negative int32_t overflow"); + } + s = loc; + return -value; + } + s = loc; + return value; } // gets a constant, which may be a relocation for later. @@ -183,7 +200,7 @@ private: return false; } else { // a global constant, we need to fix it up later - Name name = getStrToSep(); + Name name = cleanFunction(getStrToSep()); int offset = 0; if (*s == '+') { s++; @@ -198,19 +215,36 @@ private: } int64_t getInt64() { - int64_t ret = 0; + const char* loc = s; + uint64_t value = 0; bool neg = false; - if (*s == '-') { + if (*loc == '-') { neg = true; - s++; + loc++; } - while (isdigit(*s)) { - ret *= 10; - ret += (*s - '0'); - s++; + while (isdigit(*loc)) { + uint64_t digit = *loc - '0'; + if (value > std::numeric_limits<uint64_t>::max() / 10) { + abort_on("uint64_t overflow"); + } + value *= 10; + if (value > std::numeric_limits<uint64_t>::max() - digit) { + abort_on("uint64_t overflow"); + } + value += digit; + loc++; } - if (neg) ret = -ret; - return ret; + if (neg) { + uint64_t positive_int_min = + (uint64_t) - (1 + std::numeric_limits<int64_t>::min()) + (uint64_t)1; + if (value > positive_int_min) { + abort_on("negative int64_t overflow"); + } + s = loc; + return -value; + } + s = loc; + return value; } Name getCommaSeparated() { @@ -282,6 +316,16 @@ private: abort_on("getType"); } + // The LLVM backend emits function names as name@FUNCTION. We can drop the @ and after it. + Name cleanFunction(Name name) { + if (!strchr(name.str, '@')) return name; + char *temp = strdup(name.str); + *strchr(temp, '@') = 0; + Name ret = cashew::IString(temp, false); + free(temp); + return ret; + } + // processors void scan() { @@ -543,7 +587,7 @@ private: curr = specific; } else { assign = getAssign(); - Name target = getCommaSeparated(); + Name target = cleanFunction(getCommaSeparated()); if (implementedFunctions.count(target) > 0) { auto specific = allocator.alloc<Call>(); specific->target = target; @@ -933,7 +977,7 @@ private: } else if (match(".int64")) { size_t size = raw->size(); raw->resize(size + 8); - (*(int64_t*)(&(*raw)[size])) = getInt(); + (*(int64_t*)(&(*raw)[size])) = getInt64(); zero = false; } else { break; diff --git a/src/support/safe_integer.cpp b/src/support/safe_integer.cpp new file mode 100644 index 000000000..dbe62ca52 --- /dev/null +++ b/src/support/safe_integer.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <cassert> +#include <cmath> +#include <limits> + +#include "support/safe_integer.h" + +using namespace wasm; + +bool wasm::isInteger(double x) { return fmod(x, 1) == 0; } + +bool wasm::isUInteger32(double x) { + return isInteger(x) && x >= 0 && x <= std::numeric_limits<uint32_t>::max(); +} + +bool wasm::isSInteger32(double x) { + return isInteger(x) && x >= std::numeric_limits<int32_t>::min() && + x <= std::numeric_limits<int32_t>::max(); +} + +uint32_t wasm::toUInteger32(double x) { + assert(isUInteger32(x)); + return x < std::numeric_limits<uint32_t>::max() + ? x + : std::numeric_limits<uint32_t>::max(); +} + +int32_t wasm::toSInteger32(double x) { + assert(isSInteger32(x)); + return x > std::numeric_limits<int32_t>::min() && + x < std::numeric_limits<int32_t>::max() + ? x + : (x < 0 ? std::numeric_limits<int32_t>::min() + : std::numeric_limits<int32_t>::max()); +} + +bool wasm::isUInteger64(double x) { + return isInteger(x) && x >= 0 && x <= std::numeric_limits<uint64_t>::max(); +} + +bool wasm::isSInteger64(double x) { + return isInteger(x) && x >= std::numeric_limits<int64_t>::min() && + x <= std::numeric_limits<int64_t>::max(); +} + +uint64_t wasm::toUInteger64(double x) { + assert(isUInteger64(x)); + return x < (double)std::numeric_limits<uint64_t>::max() + ? (uint64_t)x + : std::numeric_limits<uint64_t>::max(); +} + +int64_t wasm::toSInteger64(double x) { + assert(isSInteger64(x)); + return x > (double)std::numeric_limits<int64_t>::min() && + x < (double)std::numeric_limits<int64_t>::max() + ? (int64_t)x + : (x < 0 ? std::numeric_limits<int64_t>::min() + : std::numeric_limits<int64_t>::max()); +} diff --git a/src/support/safe_integer.h b/src/support/safe_integer.h new file mode 100644 index 000000000..f240644c8 --- /dev/null +++ b/src/support/safe_integer.h @@ -0,0 +1,34 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef wasm_safe_integer_h +#define wasm_safe_integer_h + +#include <cstdint> + +namespace wasm { +bool isInteger(double x); +bool isUInteger32(double x); +bool isSInteger32(double x); +uint32_t toUInteger32(double x); +int32_t toSInteger32(double x); +bool isUInteger64(double x); +bool isSInteger64(double x); +uint64_t toUInteger64(double x); +int64_t toSInteger64(double x); +} // namespace wasm + +#endif // wasm_safe_integer_h diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 76a9b62be..d923b9ecb 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -30,13 +30,40 @@ namespace wasm { struct LEB128 { - int32_t value; - LEB128(int32_t value) : value(value) {} + uint32_t value; + + LEB128(uint32_t value) : value(value) {} + + void write(std::vector<uint8_t>* out) { + uint32_t temp = value; + do { + uint8_t byte = value & 127; + temp >>= 7; + if (temp) { + byte = byte | 128; + } + out.push_back(byte); + } while (temp); + } + + void read(std::function<uint8_t ()> get) { + value = 0; + uint32_t shift = 0; + while (1) { + uint8_t byte = get(); + value |= ((byte & 127) << shift); + if (byte & 128) break; + shift += 7; + } + } }; +// // We mostly stream into a buffer as we create the binary format, however, -// sometimes we need to backtrack and write to a location behind us. -class BufferWithRandomAccess : public std::vector<unsigned char> { +// sometimes we need to backtrack and write to a location behind us - wasm +// is optimized for reading, not writing. +// +class BufferWithRandomAccess : public std::vector<uint8_t> { public: BufferWithRandomAccess& operator<<(int8_t x) { push_back(x); @@ -66,8 +93,7 @@ public: return *this; } BufferWithRandomAccess& operator<<(LEB128 x) { - // XXX TODO - magic + x.write(this); return *this; } @@ -285,6 +311,7 @@ public: writeDataSegments(); writeFunctionTable(); writeEnd(); + finishUp(); } writeMemory() { @@ -367,10 +394,10 @@ public: } o << getFunctionTypeIndex(type); o << int8_t(FunctionEntry::Named | - (FunctionEntry::Import * !!import) | - (FunctionEntry::Locals * (function && function->locals.size() > 0) | - (FunctionEntry::Export) * (wasm.exportsMap[name].count(name) > 0))); - // TODO: name. how can we do an offset? into what section? and how do we know it now? + (FunctionEntry::Import * !!import) | + (FunctionEntry::Locals * (function && function->locals.size() > 0) | + (FunctionEntry::Export) * (wasm.exportsMap[name].count(name) > 0))); + emitString(Name.str); if (function && function->locals.size() > 0) { mapLocals(function); o << uint16_t(numLocalsByType[i32]) @@ -389,10 +416,10 @@ public: writeDataSegments() { o << Section::DataSegments << LEB128(wasm.memory.segments.size()); for (auto& segment : wasm.memory.segments) { - o << int32_t(segment.offset) - << int32_t(XXX) // TODO: where/when do we emit this? - << int32_t(segment.size) - << int8_t(1); // load at program start + o << int32_t(segment.offset); + emitBuffer(segment.data, segment.size); + o << int32_t(segment.size); + o << int8_t(1); // load at program start } } @@ -418,6 +445,37 @@ public: o << Section::End; } + // helpers + + struct Buffer { + const char* data; + size_t size; + size_t pointerLocation; + Buffer(const char* data, size_t size, size_t pointerLocation) : data(data), size(size), pointerLocation(pointerLocation) {} + }; + + std::vector<Buffer> buffersToWrite; + + void emitBuffer(const char* data, size_t size) { + assert(size > 0); + buffersToWrite.emplace_back(data, size, o.size()); + o << uint32_t(0); // placeholder + } + + void emitString(const char *str) { + emitBuffer(str, strlen(str) + 1); + } + + void finishUp() { + // finish buffers + for (auto& buffer : buffersToWrite) { + o.writeAt(buffer.pointerLocation, (uint32_t)o.size()); + for (size_t i = 0; i < buffer.size; i++) { + o << buffer.data[i]; + } + } + } + // AST writing via visitors std::vector<Name> breakStack; diff --git a/src/wasm.h b/src/wasm.h index b1b8d84d4..c4654580a 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -45,13 +45,14 @@ #define wasm_wasm_h #include <cassert> +#include <cmath> #include <cstddef> #include <cstdint> #include <cstring> #include <fstream> #include <map> -#include <vector> #include <string> +#include <vector> #include "compiler-support.h" #include "emscripten-optimizer/simple_ast.h" @@ -200,7 +201,7 @@ struct Literal { } static void printDouble(std::ostream &o, double d) { - if (d == 0 && 1/d < 0) { + if (d == 0 && std::signbit(d)) { o << "-0"; return; } diff --git a/src/wasm2asm.h b/src/wasm2asm.h index 53cb6df78..1a85bc177 100644 --- a/src/wasm2asm.h +++ b/src/wasm2asm.h @@ -22,6 +22,8 @@ #ifndef wasm_wasm2asm_h #define wasm_wasm2asm_h +#include <cmath> + #include "wasm.h" #include "emscripten-optimizer/optimizer.h" #include "mixed_arena.h" @@ -914,7 +916,7 @@ Ref Wasm2AsmBuilder::processFunctionBody(Expression* curr, IString result) { } case f64: { double d = curr->value.f64; - if (d == 0 && 1/d < 0) { // negative zero + if (d == 0 && std::signbit(d)) { // negative zero return ValueBuilder::makeUnary(PLUS, ValueBuilder::makeUnary(MINUS, ValueBuilder::makeDouble(0))); } return ValueBuilder::makeUnary(PLUS, ValueBuilder::makeDouble(curr->value.f64)); |