diff options
Diffstat (limited to 'src/wasm')
-rw-r--r-- | src/wasm/literal.cpp | 2 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 18 | ||||
-rw-r--r-- | src/wasm/wasm-emscripten.cpp | 21 | ||||
-rw-r--r-- | src/wasm/wasm-s-parser.cpp | 6 | ||||
-rw-r--r-- | src/wasm/wasm-stack.cpp | 9 | ||||
-rw-r--r-- | src/wasm/wasm-type.cpp | 63 | ||||
-rw-r--r-- | src/wasm/wasm-validator.cpp | 57 | ||||
-rw-r--r-- | src/wasm/wasm.cpp | 4 |
8 files changed, 76 insertions, 104 deletions
diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp index 699d3ea63..11130bc18 100644 --- a/src/wasm/literal.cpp +++ b/src/wasm/literal.cpp @@ -102,7 +102,7 @@ Literal::Literal(const LaneArray<2>& lanes) : type(Type::v128) { Literals Literal::makeZero(Type type) { assert(type.isConcrete()); Literals zeroes; - for (auto t : type.expand()) { + for (auto& t : type) { zeroes.push_back(makeSingleZero(t)); } return zeroes; diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index ddc4de36c..285fd578f 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -220,7 +220,7 @@ void WasmBinaryWriter::writeTypes() { o << S32LEB(BinaryConsts::EncodedType::Func); for (auto& sigType : {sig.params, sig.results}) { o << U32LEB(sigType.size()); - for (auto type : sigType.expand()) { + for (auto& type : sigType) { o << binaryType(type); } } @@ -385,16 +385,17 @@ void WasmBinaryWriter::writeGlobals() { o << U32LEB(num); ModuleUtils::iterDefinedGlobals(*wasm, [&](Global* global) { BYN_TRACE("write one\n"); - const auto& types = global->type.expand(); - for (size_t i = 0; i < types.size(); ++i) { - o << binaryType(types[i]); + size_t i = 0; + for (auto& t : global->type) { + o << binaryType(t); o << U32LEB(global->mutable_); - if (types.size() == 1) { + if (global->type.size() == 1) { writeExpression(global->init); } else { writeExpression(global->init->cast<TupleMake>()->operands[i]); } o << int8_t(BinaryConsts::End); + ++i; } }); finishSection(start); @@ -1385,7 +1386,9 @@ void WasmBinaryBuilder::readImports() { wasm.addEvent(curr); break; } - default: { throwError("bad import kind"); } + default: { + throwError("bad import kind"); + } } } } @@ -1795,8 +1798,7 @@ void WasmBinaryBuilder::pushExpression(Expression* curr) { Builder builder(wasm); Index tuple = builder.addVar(currFunction, curr->type); expressionStack.push_back(builder.makeLocalSet(tuple, curr)); - const std::vector<Type> types = curr->type.expand(); - for (Index i = 0; i < types.size(); ++i) { + for (Index i = 0; i < curr->type.size(); ++i) { expressionStack.push_back( builder.makeTupleExtract(builder.makeLocalGet(tuple, curr->type), i)); } diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index f4da0e6e7..48cf4b6f1 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -154,15 +154,15 @@ void EmscriptenGlueGenerator::generateDynCallThunk(Signature sig) { std::vector<NameType> params; params.emplace_back("fptr", Type::i32); // function pointer param int p = 0; - const std::vector<Type>& paramTypes = sig.params.expand(); - for (const auto& ty : paramTypes) { - params.emplace_back(std::to_string(p++), ty); + for (auto& param : sig.params) { + params.emplace_back(std::to_string(p++), param); } Function* f = builder.makeFunction(name, std::move(params), sig.results, {}); Expression* fptr = builder.makeLocalGet(0, Type::i32); std::vector<Expression*> args; - for (unsigned i = 0; i < paramTypes.size(); ++i) { - args.push_back(builder.makeLocalGet(i + 1, paramTypes[i])); + Index i = 0; + for (auto& param : sig.params) { + args.push_back(builder.makeLocalGet(++i, param)); } Expression* call = builder.makeCallIndirect(fptr, args, sig); f->body = call; @@ -493,12 +493,12 @@ AsmConstWalker::AsmConst& AsmConstWalker::createAsmConst(uint32_t id, } Signature AsmConstWalker::asmConstSig(Signature baseSig) { - std::vector<Type> params = baseSig.params.expand(); - assert(params.size() >= 1); + assert(baseSig.params.size() >= 1); // Omit the signature of the "code" parameter, taken as a string, as the // first argument - params.erase(params.begin()); - return Signature(Type(params), baseSig.results); + return Signature( + Type(std::vector<Type>(baseSig.params.begin() + 1, baseSig.params.end())), + baseSig.results); } Name AsmConstWalker::nameForImportWithSig(Signature sig, Proxying proxy) { @@ -662,8 +662,7 @@ struct FixInvokeFunctionNamesWalker return name; } - const std::vector<Type>& params = sig.params.expand(); - std::vector<Type> newParams(params.begin() + 1, params.end()); + std::vector<Type> newParams(sig.params.begin() + 1, sig.params.end()); Signature sigWoOrigFunc = Signature(Type(newParams), sig.results); invokeSigs.insert(sigWoOrigFunc); return Name("invoke_" + diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp index 5e6008bca..15ec266b3 100644 --- a/src/wasm/wasm-s-parser.cpp +++ b/src/wasm/wasm-s-parser.cpp @@ -640,9 +640,9 @@ SExpressionWasmBuilder::parseTypeUse(Element& s, // If only (type) is specified, populate `namedParams` if (!paramsOrResultsExist) { - const std::vector<Type>& funcParams = functionSignature.params.expand(); - for (size_t index = 0, e = funcParams.size(); index < e; index++) { - namedParams.emplace_back(Name::fromInt(index), funcParams[index]); + size_t index = 0; + for (auto& param : functionSignature.params) { + namedParams.emplace_back(Name::fromInt(index++), param); } } diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 19a4590e3..e64e330e8 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -1808,17 +1808,16 @@ void BinaryInstWriter::mapLocalsAndEmitHeader() { return; } for (auto type : func->vars) { - for (auto t : type.expand()) { + for (auto& t : type) { numLocalsByType[t]++; } } countScratchLocals(); std::map<Type, size_t> currLocalsByType; for (Index i = func->getVarIndexBase(); i < func->getNumLocals(); i++) { - const std::vector<Type> types = func->getLocalType(i).expand(); - for (Index j = 0; j < types.size(); j++) { - Type type = types[j]; - auto fullIndex = std::make_pair(i, j); + Index j = 0; + for (auto& type : func->getLocalType(i)) { + auto fullIndex = std::make_pair(i, j++); Index index = func->getVarIndexBase(); for (auto& typeCount : numLocalsByType) { if (type == typeCount.first) { diff --git a/src/wasm/wasm-type.cpp b/src/wasm/wasm-type.cpp index 5db04b40d..6e887d742 100644 --- a/src/wasm/wasm-type.cpp +++ b/src/wasm/wasm-type.cpp @@ -56,19 +56,6 @@ namespace { std::mutex mutex; -std::array<std::vector<Type>, Type::_last_value_type + 1> basicTypes = { - {{}, - {Type::unreachable}, - {Type::i32}, - {Type::i64}, - {Type::f32}, - {Type::f64}, - {Type::v128}, - {Type::funcref}, - {Type::externref}, - {Type::nullref}, - {Type::exnref}}}; - // Track unique_ptrs for constructed types to avoid leaks std::vector<std::unique_ptr<std::vector<Type>>> constructedTypes; @@ -123,23 +110,11 @@ Type::Type(std::initializer_list<Type> types) { init(types); } Type::Type(const std::vector<Type>& types) { init(types); } -size_t Type::size() const { return expand().size(); } - -const std::vector<Type>& Type::expand() const { - if (id <= _last_value_type) { - return basicTypes[id]; - } else { - return *(std::vector<Type>*)id; - } -} - bool Type::operator<(const Type& other) const { - const std::vector<Type>& these = expand(); - const std::vector<Type>& others = other.expand(); - return std::lexicographical_compare(these.begin(), - these.end(), - others.begin(), - others.end(), + return std::lexicographical_compare((*this).begin(), + (*this).end(), + other.begin(), + other.end(), [](const Type& a, const Type& b) { TODO_SINGLE_COMPOUND(a); TODO_SINGLE_COMPOUND(b); @@ -172,7 +147,7 @@ unsigned Type::getByteSize() const { if (isMulti()) { unsigned size = 0; - for (auto t : expand()) { + for (auto& t : *this) { size += getSingleByteSize(t); } return size; @@ -181,7 +156,7 @@ unsigned Type::getByteSize() const { } Type Type::reinterpret() const { - Type singleType = *expand().begin(); + auto singleType = *(*this).begin(); switch (singleType.getBasic()) { case Type::i32: return f32; @@ -222,7 +197,7 @@ FeatureSet Type::getFeatures() const { if (isMulti()) { FeatureSet feats = FeatureSet::Multivalue; - for (Type t : expand()) { + for (auto& t : *this) { feats |= getSingleFeatures(t); } return feats; @@ -255,13 +230,11 @@ bool Type::isSubType(Type left, Type right) { return true; } if (left.isMulti() && right.isMulti()) { - const auto& leftElems = left.expand(); - const auto& rightElems = right.expand(); - if (leftElems.size() != rightElems.size()) { + if (left.size() != right.size()) { return false; } - for (size_t i = 0; i < leftElems.size(); ++i) { - if (!isSubType(leftElems[i], rightElems[i])) { + for (size_t i = 0; i < left.size(); ++i) { + if (!isSubType(left[i], right[i])) { return false; } } @@ -286,10 +259,8 @@ Type Type::getLeastUpperBound(Type a, Type b) { if (a.isMulti()) { std::vector<Type> types; types.resize(a.size()); - const auto& as = a.expand(); - const auto& bs = b.expand(); for (size_t i = 0; i < types.size(); ++i) { - types[i] = getLeastUpperBound(as[i], bs[i]); + types[i] = getLeastUpperBound(a[i], b[i]); if (types[i] == Type::none) { return Type::none; } @@ -313,7 +284,7 @@ namespace { std::ostream& printPrefixedTypes(std::ostream& os, const char* prefix, Type type) { os << '(' << prefix; - for (auto t : type.expand()) { + for (auto& t : type) { os << " " << t; } os << ')'; @@ -347,12 +318,10 @@ bool Signature::operator<(const Signature& other) const { std::ostream& operator<<(std::ostream& os, Type type) { 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 << ", "; - } + auto sep = ""; + for (auto& t : type) { + os << sep << t; + sep = ", "; } os << ')'; } else { diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp index 4921dafdd..1ff04906e 100644 --- a/src/wasm/wasm-validator.cpp +++ b/src/wasm/wasm-validator.cpp @@ -641,20 +641,21 @@ void FunctionValidator::visitCall(Call* curr) { if (!shouldBeTrue(!!target, curr, "call target must exist")) { return; } - const std::vector<Type> params = target->sig.params.expand(); - if (!shouldBeTrue(curr->operands.size() == params.size(), + if (!shouldBeTrue(curr->operands.size() == target->sig.params.size(), curr, "call param number must match")) { return; } - for (size_t i = 0; i < curr->operands.size(); i++) { + size_t i = 0; + for (auto& param : target->sig.params) { if (!shouldBeSubTypeOrFirstIsUnreachable(curr->operands[i]->type, - params[i], + param, curr, "call param types must match") && !info.quiet) { getStream() << "(on argument " << i << ")\n"; } + ++i; } if (curr->isReturn) { shouldBeEqual(curr->type, @@ -692,24 +693,25 @@ void FunctionValidator::visitCallIndirect(CallIndirect* curr) { if (!info.validateGlobally) { return; } - const std::vector<Type>& params = curr->sig.params.expand(); shouldBeEqualOrFirstIsUnreachable(curr->target->type, Type(Type::i32), curr, "indirect call target must be an i32"); - if (!shouldBeTrue(curr->operands.size() == params.size(), + if (!shouldBeTrue(curr->operands.size() == curr->sig.params.size(), curr, "call param number must match")) { return; } - for (size_t i = 0; i < curr->operands.size(); i++) { + size_t i = 0; + for (auto& param : curr->sig.params) { if (!shouldBeSubTypeOrFirstIsUnreachable(curr->operands[i]->type, - params[i], + param, curr, "call param types must match") && !info.quiet) { getStream() << "(on argument " << i << ")\n"; } + ++i; } if (curr->isReturn) { shouldBeEqual(curr->type, @@ -1871,15 +1873,16 @@ void FunctionValidator::visitThrow(Throw* curr) { "event's param numbers must match")) { return; } - const std::vector<Type>& paramTypes = event->sig.params.expand(); - for (size_t i = 0; i < curr->operands.size(); i++) { + size_t i = 0; + for (auto& param : event->sig.params) { if (!shouldBeSubTypeOrFirstIsUnreachable(curr->operands[i]->type, - paramTypes[i], + param, curr->operands[i], "event param types must match") && !info.quiet) { getStream() << "(on argument " << i << ")\n"; } + ++i; } } @@ -1957,7 +1960,7 @@ void FunctionValidator::visitTupleExtract(TupleExtract* curr) { shouldBeTrue(inBounds, curr, "tuple.extract index out of bounds"); if (inBounds) { shouldBeSubType( - curr->tuple->type.expand()[curr->index], + curr->tuple->type[curr->index], curr->type, curr, "tuple.extract type does not match the type of the extracted element"); @@ -1972,17 +1975,17 @@ void FunctionValidator::visitFunction(Function* curr) { "Multivalue function results (multivalue is not enabled)"); } FeatureSet features; - for (auto type : curr->sig.params.expand()) { - features |= type.getFeatures(); - shouldBeTrue(type.isConcrete(), curr, "params must be concretely typed"); + for (auto& param : curr->sig.params) { + features |= param.getFeatures(); + shouldBeTrue(param.isConcrete(), curr, "params must be concretely typed"); } - for (auto type : curr->sig.results.expand()) { - features |= type.getFeatures(); - shouldBeTrue(type.isConcrete(), curr, "results must be concretely typed"); + for (auto& result : curr->sig.results) { + features |= result.getFeatures(); + shouldBeTrue(result.isConcrete(), curr, "results must be concretely typed"); } - for (auto type : curr->vars) { - features |= type.getFeatures(); - shouldBeTrue(type.isConcrete(), curr, "vars must be concretely typed"); + for (auto& var : curr->vars) { + features |= var.getFeatures(); + shouldBeTrue(var.isConcrete(), curr, "vars must be concretely typed"); } shouldBeTrue(features <= getModule()->features, curr, @@ -2141,13 +2144,13 @@ static void validateImports(Module& module, ValidationInfo& info) { "(multivalue is not enabled)"); } if (info.validateWeb) { - for (Type param : curr->sig.params.expand()) { + for (auto& param : curr->sig.params) { info.shouldBeUnequal(param, Type(Type::i64), curr->name, "Imported function must not have i64 parameters"); } - for (Type result : curr->sig.results.expand()) { + for (auto& result : curr->sig.results) { info.shouldBeUnequal(result, Type(Type::i64), curr->name, @@ -2170,14 +2173,14 @@ static void validateExports(Module& module, ValidationInfo& info) { if (curr->kind == ExternalKind::Function) { if (info.validateWeb) { Function* f = module.getFunction(curr->value); - for (auto param : f->sig.params.expand()) { + for (auto& param : f->sig.params) { info.shouldBeUnequal( param, Type(Type::i64), f->name, "Exported function must not have i64 parameters"); } - for (auto result : f->sig.results.expand()) { + for (auto& result : f->sig.results) { info.shouldBeUnequal(result, Type(Type::i64), f->name, @@ -2349,8 +2352,8 @@ static void validateEvents(Module& module, ValidationInfo& info) { curr->name, "Multivalue event type (multivalue is not enabled)"); } - for (auto type : curr->sig.params.expand()) { - info.shouldBeTrue(type.isConcrete(), + for (auto& param : curr->sig.params) { + info.shouldBeTrue(param.isConcrete(), curr->name, "Values in an event should have concrete types"); } diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index be3ab6ccc..387ea577f 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -948,7 +948,7 @@ void TupleExtract::finalize() { if (tuple->type == Type::unreachable) { type = Type::unreachable; } else { - type = tuple->type.expand()[index]; + type = tuple->type[index]; } } @@ -1006,7 +1006,7 @@ Index Function::getVarIndexBase() { return sig.params.size(); } Type Function::getLocalType(Index index) { auto numParams = sig.params.size(); if (index < numParams) { - return sig.params.expand()[index]; + return sig.params[index]; } else if (isVar(index)) { return vars[index - numParams]; } else { |