diff options
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/literal.cpp | 90 | ||||
-rw-r--r-- | src/wasm/wasm-stack.cpp | 16 | ||||
-rw-r--r-- | src/wasm/wasm-type.cpp | 103 | ||||
-rw-r--r-- | src/wasm/wasm-validator.cpp | 6 |
4 files changed, 108 insertions, 107 deletions
diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp index 4f66b36e3..d5333847d 100644 --- a/src/wasm/literal.cpp +++ b/src/wasm/literal.cpp @@ -102,7 +102,7 @@ Literal Literal::castToI64() { } int64_t Literal::getInteger() const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return i32; case Type::i64: @@ -113,7 +113,7 @@ int64_t Literal::getInteger() const { } double Literal::getFloat() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return getf32(); case Type::f64: @@ -125,7 +125,7 @@ double Literal::getFloat() const { void Literal::getBits(uint8_t (&buf)[16]) const { memset(buf, 0, 16); - switch (type) { + switch (type.getSingle()) { case Type::i32: case Type::f32: memcpy(buf, &i32, sizeof(i32)); @@ -266,7 +266,7 @@ void Literal::printVec128(std::ostream& o, const std::array<uint8_t, 16>& v) { std::ostream& operator<<(std::ostream& o, Literal literal) { prepareMinorColor(o); - switch (literal.type) { + switch (literal.type.getSingle()) { case Type::none: o << "?"; break; @@ -486,7 +486,7 @@ Literal Literal::truncSatToUI64() const { } Literal Literal::eqz() const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return eq(Literal(int32_t(0))); case Type::i64: @@ -508,7 +508,7 @@ Literal Literal::eqz() const { } Literal Literal::neg() const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(-uint32_t(i32)); case Type::i64: @@ -530,7 +530,7 @@ Literal Literal::neg() const { } Literal Literal::abs() const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 & 0x7fffffff); case Type::i64: @@ -552,7 +552,7 @@ Literal Literal::abs() const { } Literal Literal::ceil() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(std::ceil(getf32())); case Type::f64: @@ -563,7 +563,7 @@ Literal Literal::ceil() const { } Literal Literal::floor() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(std::floor(getf32())); case Type::f64: @@ -574,7 +574,7 @@ Literal Literal::floor() const { } Literal Literal::trunc() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(std::trunc(getf32())); case Type::f64: @@ -585,7 +585,7 @@ Literal Literal::trunc() const { } Literal Literal::nearbyint() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(std::nearbyint(getf32())); case Type::f64: @@ -596,7 +596,7 @@ Literal Literal::nearbyint() const { } Literal Literal::sqrt() const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(std::sqrt(getf32())); case Type::f64: @@ -635,7 +635,7 @@ Literal Literal::demote() const { } Literal Literal::add(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) + uint32_t(other.i32)); case Type::i64: @@ -657,7 +657,7 @@ Literal Literal::add(const Literal& other) const { } Literal Literal::sub(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) - uint32_t(other.i32)); case Type::i64: @@ -750,7 +750,7 @@ Literal Literal::subSatUI16(const Literal& other) const { } Literal Literal::mul(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) * uint32_t(other.i32)); case Type::i64: @@ -772,7 +772,7 @@ Literal Literal::mul(const Literal& other) const { } Literal Literal::div(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: { float lhs = getf32(), rhs = other.getf32(); float sign = std::signbit(lhs) == std::signbit(rhs) ? 0.f : -0.f; @@ -835,7 +835,7 @@ Literal Literal::div(const Literal& other) const { } Literal Literal::divS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 / other.i32); case Type::i64: @@ -846,7 +846,7 @@ Literal Literal::divS(const Literal& other) const { } Literal Literal::divU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) / uint32_t(other.i32)); case Type::i64: @@ -857,7 +857,7 @@ Literal Literal::divU(const Literal& other) const { } Literal Literal::remS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 % other.i32); case Type::i64: @@ -868,7 +868,7 @@ Literal Literal::remS(const Literal& other) const { } Literal Literal::remU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) % uint32_t(other.i32)); case Type::i64: @@ -896,7 +896,7 @@ Literal Literal::avgrUInt(const Literal& other) const { } Literal Literal::and_(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 & other.i32); case Type::i64: @@ -907,7 +907,7 @@ Literal Literal::and_(const Literal& other) const { } Literal Literal::or_(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 | other.i32); case Type::i64: @@ -918,7 +918,7 @@ Literal Literal::or_(const Literal& other) const { } Literal Literal::xor_(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 ^ other.i32); case Type::i64: @@ -929,7 +929,7 @@ Literal Literal::xor_(const Literal& other) const { } Literal Literal::shl(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) << Bits::getEffectiveShifts(other.i32, Type::i32)); @@ -942,7 +942,7 @@ Literal Literal::shl(const Literal& other) const { } Literal Literal::shrS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 >> Bits::getEffectiveShifts(other.i32, Type::i32)); case Type::i64: @@ -953,7 +953,7 @@ Literal Literal::shrS(const Literal& other) const { } Literal Literal::shrU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) >> Bits::getEffectiveShifts(other.i32, Type::i32)); @@ -966,7 +966,7 @@ Literal Literal::shrU(const Literal& other) const { } Literal Literal::rotL(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(RotateLeft(uint32_t(i32), uint32_t(other.i32))); case Type::i64: @@ -977,7 +977,7 @@ Literal Literal::rotL(const Literal& other) const { } Literal Literal::rotR(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(RotateRight(uint32_t(i32), uint32_t(other.i32))); case Type::i64: @@ -988,7 +988,7 @@ Literal Literal::rotR(const Literal& other) const { } Literal Literal::eq(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 == other.i32); case Type::i64: @@ -1010,7 +1010,7 @@ Literal Literal::eq(const Literal& other) const { } Literal Literal::ne(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 != other.i32); case Type::i64: @@ -1032,7 +1032,7 @@ Literal Literal::ne(const Literal& other) const { } Literal Literal::ltS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 < other.i32); case Type::i64: @@ -1043,7 +1043,7 @@ Literal Literal::ltS(const Literal& other) const { } Literal Literal::ltU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) < uint32_t(other.i32)); case Type::i64: @@ -1054,7 +1054,7 @@ Literal Literal::ltU(const Literal& other) const { } Literal Literal::lt(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(getf32() < other.getf32()); case Type::f64: @@ -1065,7 +1065,7 @@ Literal Literal::lt(const Literal& other) const { } Literal Literal::leS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 <= other.i32); case Type::i64: @@ -1076,7 +1076,7 @@ Literal Literal::leS(const Literal& other) const { } Literal Literal::leU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) <= uint32_t(other.i32)); case Type::i64: @@ -1087,7 +1087,7 @@ Literal Literal::leU(const Literal& other) const { } Literal Literal::le(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(getf32() <= other.getf32()); case Type::f64: @@ -1098,7 +1098,7 @@ Literal Literal::le(const Literal& other) const { } Literal Literal::gtS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 > other.i32); case Type::i64: @@ -1109,7 +1109,7 @@ Literal Literal::gtS(const Literal& other) const { } Literal Literal::gtU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) > uint32_t(other.i32)); case Type::i64: @@ -1120,7 +1120,7 @@ Literal Literal::gtU(const Literal& other) const { } Literal Literal::gt(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(getf32() > other.getf32()); case Type::f64: @@ -1131,7 +1131,7 @@ Literal Literal::gt(const Literal& other) const { } Literal Literal::geS(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(i32 >= other.i32); case Type::i64: @@ -1142,7 +1142,7 @@ Literal Literal::geS(const Literal& other) const { } Literal Literal::geU(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::i32: return Literal(uint32_t(i32) >= uint32_t(other.i32)); case Type::i64: @@ -1153,7 +1153,7 @@ Literal Literal::geU(const Literal& other) const { } Literal Literal::ge(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal(getf32() >= other.getf32()); case Type::f64: @@ -1164,7 +1164,7 @@ Literal Literal::ge(const Literal& other) const { } Literal Literal::min(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: { auto l = getf32(), r = other.getf32(); if (l == r && l == 0) { @@ -1207,7 +1207,7 @@ Literal Literal::min(const Literal& other) const { } Literal Literal::max(const Literal& other) const { - switch (type) { + switch (type.getSingle()) { case Type::f32: { auto l = getf32(), r = other.getf32(); if (l == r && l == 0) { @@ -1251,7 +1251,7 @@ Literal Literal::max(const Literal& other) const { Literal Literal::copysign(const Literal& other) const { // operate on bits directly, to avoid signalling bit being set on a float - switch (type) { + switch (type.getSingle()) { case Type::f32: return Literal((i32 & 0x7fffffff) | (other.i32 & 0x80000000)).castToF32(); break; diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 551b1f132..a96dbb153 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -93,7 +93,7 @@ void BinaryInstWriter::visitGlobalSet(GlobalSet* curr) { void BinaryInstWriter::visitLoad(Load* curr) { if (!curr->isAtomic) { - switch (curr->type) { + switch (curr->type.getSingle()) { case Type::i32: { switch (curr->bytes) { case 1: @@ -156,7 +156,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { } } else { o << int8_t(BinaryConsts::AtomicPrefix); - switch (curr->type) { + switch (curr->type.getSingle()) { case Type::i32: { switch (curr->bytes) { case 1: @@ -203,7 +203,7 @@ void BinaryInstWriter::visitLoad(Load* curr) { void BinaryInstWriter::visitStore(Store* curr) { if (!curr->isAtomic) { - switch (curr->valueType) { + switch (curr->valueType.getSingle()) { case Type::i32: { switch (curr->bytes) { case 1: @@ -259,7 +259,7 @@ void BinaryInstWriter::visitStore(Store* curr) { } } else { o << int8_t(BinaryConsts::AtomicPrefix); - switch (curr->valueType) { + switch (curr->valueType.getSingle()) { case Type::i32: { switch (curr->bytes) { case 1: @@ -307,7 +307,7 @@ void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) { #define CASE_FOR_OP(Op) \ case Op: \ - switch (curr->type) { \ + switch (curr->type.getSingle()) { \ case Type::i32: \ switch (curr->bytes) { \ case 1: \ @@ -363,7 +363,7 @@ void BinaryInstWriter::visitAtomicRMW(AtomicRMW* curr) { void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) { o << int8_t(BinaryConsts::AtomicPrefix); - switch (curr->type) { + switch (curr->type.getSingle()) { case Type::i32: switch (curr->bytes) { case 1: @@ -405,7 +405,7 @@ void BinaryInstWriter::visitAtomicCmpxchg(AtomicCmpxchg* curr) { void BinaryInstWriter::visitAtomicWait(AtomicWait* curr) { o << int8_t(BinaryConsts::AtomicPrefix); - switch (curr->expectedType) { + switch (curr->expectedType.getSingle()) { case Type::i32: { o << int8_t(BinaryConsts::I32AtomicWait); emitMemoryAccess(4, 4, curr->offset); @@ -621,7 +621,7 @@ void BinaryInstWriter::visitMemoryFill(MemoryFill* curr) { } void BinaryInstWriter::visitConst(Const* curr) { - switch (curr->type) { + switch (curr->type.getSingle()) { case Type::i32: { o << int8_t(BinaryConsts::I32Const) << S32LEB(curr->value.geti32()); break; diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 62c30d1e0..3542f5d69 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -28,8 +28,8 @@ template<> class std::hash<std::vector<wasm::Type>> { public: size_t operator()(const std::vector<wasm::Type>& types) const { uint32_t res = wasm::rehash(0, uint32_t(types.size())); - for (auto vt : types) { - res = wasm::rehash(res, uint32_t(vt)); + for (auto t : types) { + res = wasm::rehash(res, t.getID()); } return res; } @@ -37,8 +37,8 @@ public: size_t std::hash<wasm::Signature>:: operator()(const wasm::Signature& sig) const { - return std::hash<uint64_t>{}(uint64_t(sig.params) << 32 | - uint64_t(sig.results)); + return std::hash<uint64_t>{}(uint64_t(sig.params.getID()) << 32 | + uint64_t(sig.results.getID())); } namespace wasm { @@ -141,13 +141,13 @@ bool Type::operator<(const Type& other) const { these.end(), others.begin(), others.end(), - [](const Type& a, const Type& b) { return uint32_t(a) < uint32_t(b); }); + [](const Type& a, const Type& b) { return a.getSingle() < b.getSingle(); }); } unsigned Type::getByteSize() const { assert(isSingle() && "getByteSize does not works with single types"); Type singleType = *expand().begin(); - switch (singleType) { + switch (singleType.getSingle()) { case Type::i32: return 4; case Type::i64: @@ -172,7 +172,7 @@ unsigned Type::getByteSize() const { Type Type::reinterpret() const { assert(isSingle() && "reinterpretType only works with single types"); Type singleType = *expand().begin(); - switch (singleType) { + switch (singleType.getSingle()) { case Type::i32: return f32; case Type::i64: @@ -196,7 +196,7 @@ Type Type::reinterpret() const { FeatureSet Type::getFeatures() const { FeatureSet feats = FeatureSet::MVP; for (Type t : expand()) { - switch (t) { + switch (t.getSingle()) { case Type::v128: feats |= FeatureSet::SIMD; break; @@ -299,50 +299,51 @@ bool Signature::operator<(const Signature& other) const { } std::ostream& operator<<(std::ostream& os, Type type) { - switch (type) { - case Type::none: - os << "none"; - break; - case Type::unreachable: - os << "unreachable"; - break; - case Type::i32: - os << "i32"; - break; - case Type::i64: - os << "i64"; - break; - case Type::f32: - os << "f32"; - break; - case Type::f64: - os << "f64"; - break; - case Type::v128: - os << "v128"; - break; - case Type::funcref: - os << "funcref"; - break; - case Type::anyref: - os << "anyref"; - break; - case Type::nullref: - os << "nullref"; - break; - case Type::exnref: - os << "exnref"; - break; - default: { - os << '('; - const std::vector<Type>& types = type.expand(); - for (size_t i = 0; i < types.size(); ++i) { - os << types[i]; - if (i < types.size() - 1) { - os << ", "; - } + if (type.isMulti()) { + os << '('; + const std::vector<Type>& types = type.expand(); + for (size_t i = 0; i < types.size(); ++i) { + os << types[i]; + if (i < types.size() - 1) { + os << ", "; } - os << ')'; + } + os << ')'; + } else { + switch (type.getSingle()) { + case Type::none: + os << "none"; + break; + case Type::unreachable: + os << "unreachable"; + break; + case Type::i32: + os << "i32"; + break; + case Type::i64: + os << "i64"; + break; + case Type::f32: + os << "f32"; + break; + case Type::f64: + os << "f64"; + break; + case Type::v128: + os << "v128"; + break; + case Type::funcref: + os << "funcref"; + break; + case Type::anyref: + os << "anyref"; + break; + case Type::nullref: + os << "nullref"; + break; + case Type::exnref: + os << "exnref"; + break; } } return os; diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 30e182f0b..484376358 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -172,7 +172,7 @@ struct ValidationInfo { Expression* curr, const char* text, Function* func = nullptr) { - switch (ty) { + switch (ty.getSingle()) { case Type::i32: case Type::i64: case Type::unreachable: { @@ -1227,7 +1227,7 @@ void FunctionValidator::visitMemoryFill(MemoryFill* curr) { void FunctionValidator::validateMemBytes(uint8_t bytes, Type type, Expression* curr) { - switch (type) { + switch (type.getSingle()) { case Type::i32: shouldBeTrue(bytes == 1 || bytes == 2 || bytes == 4, curr, @@ -1975,7 +1975,7 @@ void FunctionValidator::validateAlignment( } } shouldBeTrue(align <= bytes, curr, "alignment must not exceed natural"); - switch (type) { + switch (type.getSingle()) { case Type::i32: case Type::f32: { shouldBeTrue(align <= 4, curr, "alignment must not exceed natural"); |