diff options
author | Thomas Lively <7121787+tlively@users.noreply.github.com> | 2019-03-19 09:18:53 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-19 09:18:53 -0700 |
commit | 9e2559b789424ea1920c5e9c73b63a976390226c (patch) | |
tree | 67acda9f9737596c53200661901994adc24095f9 /src/wasm | |
parent | dd3873375ca7375b79105742b30fa03f9f8a0c24 (diff) | |
download | binaryen-9e2559b789424ea1920c5e9c73b63a976390226c.tar.gz binaryen-9e2559b789424ea1920c5e9c73b63a976390226c.tar.bz2 binaryen-9e2559b789424ea1920c5e9c73b63a976390226c.zip |
Update v128.const text formats (#1934)
Parse the formats allowed by the spec proposal and emit the i32x4
canonical format.
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/literal.cpp | 2 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 56 |
2 files changed, 30 insertions, 28 deletions
diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp index bc45f5834..b374566d1 100644 --- a/src/wasm/literal.cpp +++ b/src/wasm/literal.cpp @@ -243,7 +243,7 @@ std::ostream& operator<<(std::ostream& o, Literal literal) { case Type::i64: o << literal.i64; break; case Type::f32: literal.printFloat(o, literal.getf32()); break; case Type::f64: literal.printDouble(o, literal.getf64()); break; - case Type::v128: o << "i32 "; literal.printVec128(o, literal.getv128()); break; + case Type::v128: o << "i32x4 "; literal.printVec128(o, literal.getv128()); break; case Type::unreachable: WASM_UNREACHABLE(); } restoreNormalColor(o); diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 7fd4679b1..1067264f7 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -641,6 +641,16 @@ Type SExpressionWasmBuilder::stringToType(const char* str, bool allowError, bool throw ParseException("invalid wasm type"); } +Type SExpressionWasmBuilder::stringToLaneType(const char* str) { + if (strcmp(str, "i8x16") == 0) return i32; + if (strcmp(str, "i16x8") == 0) return i32; + if (strcmp(str, "i32x4") == 0) return i32; + if (strcmp(str, "i64x2") == 0) return i64; + if (strcmp(str, "f32x4") == 0) return f32; + if (strcmp(str, "f64x2") == 0) return f64; + return none; +} + Function::DebugLocation SExpressionWasmBuilder::getDebugLocation(const SourceLocation& loc) { IString file = loc.filename; auto& debugInfoFileNames = wasm.debugInfoFileNames; @@ -864,6 +874,20 @@ Expression* SExpressionWasmBuilder::makeThenOrElse(Element& s) { return ret; } +template<int Lanes> +static Literal makeLanes(Element& s, MixedArena& allocator, Type lane_t) { + std::array<Literal, Lanes> lanes; + for (size_t i = 0; i < Lanes; ++i) { + Expression* lane = parseConst(s[i+2]->str(), lane_t, allocator); + if (lane) { + lanes[i] = lane->cast<Const>()->value; + } else { + throw ParseException("Could not parse v128 lane"); + } + } + return Literal(lanes); +} + Expression* SExpressionWasmBuilder::makeConst(Element& s, Type type) { if (type != v128) { auto ret = parseConst(s[1]->str(), type, allocator); @@ -872,57 +896,35 @@ Expression* SExpressionWasmBuilder::makeConst(Element& s, Type type) { } auto ret = allocator.alloc<Const>(); - auto getLiteral = [](Expression* expr) { - if (expr == nullptr) { - throw ParseException("Could not parse v128 lane"); - } - return expr->cast<Const>()->value; - }; - Type lane_t = stringToType(s[1]->str()); + Type lane_t = stringToLaneType(s[1]->str().str); size_t lanes = s.size() - 2; switch (lanes) { case 2: { if (lane_t != i64 && lane_t != f64) { throw ParseException("Unexpected v128 literal lane type"); } - std::array<Literal, 2> lanes; - for (size_t i = 0; i < 2; ++i) { - lanes[i] = getLiteral(parseConst(s[i+2]->str(), lane_t, allocator)); - } - ret->value = Literal(lanes); + ret->value = makeLanes<2>(s, allocator, lane_t); break; } case 4: { if (lane_t != i32 && lane_t != f32) { throw ParseException("Unexpected v128 literal lane type"); } - std::array<Literal, 4> lanes; - for (size_t i = 0; i < 4; ++i) { - lanes[i] = getLiteral(parseConst(s[i+2]->str(), lane_t, allocator)); - } - ret->value = Literal(lanes); + ret->value = makeLanes<4>(s, allocator, lane_t); break; } case 8: { if (lane_t != i32) { throw ParseException("Unexpected v128 literal lane type"); } - std::array<Literal, 8> lanes; - for (size_t i = 0; i < 8; ++i) { - lanes[i] = getLiteral(parseConst(s[i+2]->str(), lane_t, allocator)); - } - ret->value = Literal(lanes); + ret->value = makeLanes<8>(s, allocator, lane_t); break; } case 16: { if (lane_t != i32) { throw ParseException("Unexpected v128 literal lane type"); } - std::array<Literal, 16> lanes; - for (size_t i = 0; i < 16; ++i) { - lanes[i] = getLiteral(parseConst(s[i+2]->str(), lane_t, allocator)); - } - ret->value = Literal(lanes); + ret->value = makeLanes<16>(s, allocator, lane_t); break; } default: throw ParseException("Unexpected number of lanes in v128 literal"); |