diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/Asyncify.cpp | 4 | ||||
-rw-r--r-- | src/passes/FuncCastEmulation.cpp | 4 | ||||
-rw-r--r-- | src/passes/GenerateDynCalls.cpp | 6 | ||||
-rw-r--r-- | src/passes/I64ToI32Lowering.cpp | 10 | ||||
-rw-r--r-- | src/passes/LegalizeJSInterface.cpp | 6 | ||||
-rw-r--r-- | src/passes/StackCheck.cpp | 51 | ||||
-rw-r--r-- | src/tools/fuzzing.h | 18 | ||||
-rw-r--r-- | src/wasm-binary.h | 2 | ||||
-rw-r--r-- | src/wasm-builder.h | 70 | ||||
-rw-r--r-- | src/wasm/wasm-binary.cpp | 22 |
10 files changed, 96 insertions, 97 deletions
diff --git a/src/passes/Asyncify.cpp b/src/passes/Asyncify.cpp index a05c9293c..1abfa9628 100644 --- a/src/passes/Asyncify.cpp +++ b/src/passes/Asyncify.cpp @@ -1572,9 +1572,9 @@ private: builder.makeIf(builder.makeBinary(GtUInt32, stackPos, stackEnd), builder.makeUnreachable())); body->finalize(); - auto* func = builder.makeFunction( + auto func = builder.makeFunction( name, Signature(Type(params), Type::none), {}, body); - module->addFunction(func); + module->addFunction(std::move(func)); module->addExport(builder.makeExport(name, name, ExternalKind::Function)); }; diff --git a/src/passes/FuncCastEmulation.cpp b/src/passes/FuncCastEmulation.cpp index a164b4264..ba6d1d583 100644 --- a/src/passes/FuncCastEmulation.cpp +++ b/src/passes/FuncCastEmulation.cpp @@ -209,12 +209,12 @@ private: for (Index i = 0; i < NUM_PARAMS; i++) { thunkParams.push_back(Type::i64); } - auto* thunkFunc = + auto thunkFunc = builder.makeFunction(thunk, Signature(Type(thunkParams), Type::i64), {}, // no vars toABI(call, module)); - module->addFunction(thunkFunc); + module->addFunction(std::move(thunkFunc)); return thunk; } }; diff --git a/src/passes/GenerateDynCalls.cpp b/src/passes/GenerateDynCalls.cpp index 08c8c0ac7..df5145402 100644 --- a/src/passes/GenerateDynCalls.cpp +++ b/src/passes/GenerateDynCalls.cpp @@ -120,7 +120,7 @@ void GenerateDynCalls::generateDynCallThunk(Signature sig) { for (const auto& param : sig.params) { params.emplace_back(std::to_string(p++), param); } - Function* f = builder.makeFunction(name, std::move(params), sig.results, {}); + auto f = builder.makeFunction(name, std::move(params), sig.results, {}); Expression* fptr = builder.makeLocalGet(0, Type::i32); std::vector<Expression*> args; Index i = 0; @@ -130,8 +130,8 @@ void GenerateDynCalls::generateDynCallThunk(Signature sig) { Expression* call = builder.makeCallIndirect(fptr, args, sig); f->body = call; - wasm->addFunction(f); - exportFunction(*wasm, f->name, true); + wasm->addFunction(std::move(f)); + exportFunction(*wasm, name, true); } Pass* createGenerateDynCallsPass() { return new GenerateDynCalls(false); } diff --git a/src/passes/I64ToI32Lowering.cpp b/src/passes/I64ToI32Lowering.cpp index a24d78213..f14f1a7e2 100644 --- a/src/passes/I64ToI32Lowering.cpp +++ b/src/passes/I64ToI32Lowering.cpp @@ -113,11 +113,10 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> { } originallyI64Globals.insert(curr->name); curr->type = Type::i32; - auto* high = builder->makeGlobal(makeHighName(curr->name), - Type::i32, - builder->makeConst(int32_t(0)), - Builder::Mutable); - module->addGlobal(high); + auto high = builder->makeGlobal(makeHighName(curr->name), + Type::i32, + builder->makeConst(int32_t(0)), + Builder::Mutable); if (curr->imported()) { Fatal() << "TODO: imported i64 globals"; } else { @@ -134,6 +133,7 @@ struct I64ToI32Lowering : public WalkerPass<PostWalker<I64ToI32Lowering>> { } curr->init->type = Type::i32; } + module->addGlobal(std::move(high)); } // For functions that return 64-bit values, we use this global variable diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp index b000b0c38..0e810560e 100644 --- a/src/passes/LegalizeJSInterface.cpp +++ b/src/passes/LegalizeJSInterface.cpp @@ -53,7 +53,7 @@ struct LegalizeJSInterface : public Pass { .getArgumentOrDefault("legalize-js-interface-export-originals", "") .empty(); // for each illegal export, we must export a legalized stub instead - std::vector<Export*> newExports; + std::vector<std::unique_ptr<Export>> newExports; for (auto& ex : module->exports) { if (ex->kind == ExternalKind::Function) { // if it's an import, ignore it @@ -81,8 +81,8 @@ struct LegalizeJSInterface : public Pass { } } } - for (auto* ex : newExports) { - module->addExport(ex); + for (auto& ex : newExports) { + module->addExport(std::move(ex)); } // Avoid iterator invalidation later. std::vector<Function*> originalFunctions; diff --git a/src/passes/StackCheck.cpp b/src/passes/StackCheck.cpp index 4388705e6..eb63aacbc 100644 --- a/src/passes/StackCheck.cpp +++ b/src/passes/StackCheck.cpp @@ -52,30 +52,30 @@ static void importStackOverflowHandler(Module& module, Name name) { } } -static void addExportedFunction(Module& module, Function* function) { - module.addFunction(function); - auto export_ = new Export; - export_->name = export_->value = function->name; - export_->kind = ExternalKind::Function; - module.addExport(export_); +static void addExportedFunction(Module& module, + std::unique_ptr<Function> function) { + auto export_ = + Builder::makeExport(function->name, function->name, ExternalKind::Function); + module.addFunction(std::move(function)); + module.addExport(std::move(export_)); } static void generateSetStackLimitFunctions(Module& module) { Builder builder(module); - Function* limitsFunc = builder.makeFunction( + auto limitsFunc = builder.makeFunction( SET_STACK_LIMITS, Signature({Type::i32, Type::i32}, Type::none), {}); LocalGet* getBase = builder.makeLocalGet(0, Type::i32); Expression* storeBase = builder.makeGlobalSet(STACK_BASE, getBase); LocalGet* getLimit = builder.makeLocalGet(1, Type::i32); Expression* storeLimit = builder.makeGlobalSet(STACK_LIMIT, getLimit); limitsFunc->body = builder.makeBlock({storeBase, storeLimit}); - addExportedFunction(module, limitsFunc); + addExportedFunction(module, std::move(limitsFunc)); } struct EnforceStackLimits : public WalkerPass<PostWalker<EnforceStackLimits>> { - EnforceStackLimits(Global* stackPointer, - Global* stackBase, - Global* stackLimit, + EnforceStackLimits(const Global* stackPointer, + const std::unique_ptr<Global>& stackBase, + const std::unique_ptr<Global>& stackLimit, Builder& builder, Name handler) : stackPointer(stackPointer), stackBase(stackBase), stackLimit(stackLimit), @@ -127,9 +127,9 @@ struct EnforceStackLimits : public WalkerPass<PostWalker<EnforceStackLimits>> { } private: - Global* stackPointer; - Global* stackBase; - Global* stackLimit; + const Global* stackPointer; + const std::unique_ptr<Global>& stackBase; + const std::unique_ptr<Global>& stackLimit; Builder& builder; Name handler; }; @@ -151,21 +151,20 @@ struct StackCheck : public Pass { } Builder builder(*module); - Global* stackBase = builder.makeGlobal(STACK_BASE, - stackPointer->type, - builder.makeConst(int32_t(0)), - Builder::Mutable); - module->addGlobal(stackBase); - - Global* stackLimit = builder.makeGlobal(STACK_LIMIT, - stackPointer->type, - builder.makeConst(int32_t(0)), - Builder::Mutable); - module->addGlobal(stackLimit); - + auto stackBase = builder.makeGlobal(STACK_BASE, + stackPointer->type, + builder.makeConst(int32_t(0)), + Builder::Mutable); + + auto stackLimit = builder.makeGlobal(STACK_LIMIT, + stackPointer->type, + builder.makeConst(int32_t(0)), + Builder::Mutable); PassRunner innerRunner(module); EnforceStackLimits(stackPointer, stackBase, stackLimit, builder, handler) .run(&innerRunner, module); + module->addGlobal(std::move(stackBase)); + module->addGlobal(std::move(stackLimit)); generateSetStackLimitFunctions(*module); } }; diff --git a/src/tools/fuzzing.h b/src/tools/fuzzing.h index f24b50d30..df7cf2226 100644 --- a/src/tools/fuzzing.h +++ b/src/tools/fuzzing.h @@ -450,24 +450,24 @@ private: } for (size_t index = upTo(MAX_GLOBALS); index > 0; --index) { auto type = getConcreteType(); - auto* global = + auto global = builder.makeGlobal(Names::getValidGlobalName(wasm, "global$"), type, makeConst(type), Builder::Mutable); - wasm.addGlobal(global); globalsByType[type].push_back(global->name); + wasm.addGlobal(std::move(global)); } } void setupEvents() { Index num = upTo(3); for (size_t i = 0; i < num; i++) { - auto* event = + auto event = builder.makeEvent(Names::getValidEventName(wasm, "event$"), WASM_EVENT_ATTRIBUTE_EXCEPTION, Signature(getControlFlowType(), Type::none)); - wasm.addEvent(event); + wasm.addEvent(std::move(event)); } } @@ -547,11 +547,11 @@ private: } void addHangLimitSupport() { - auto* glob = builder.makeGlobal(HANG_LIMIT_GLOBAL, - Type::i32, - builder.makeConst(int32_t(HANG_LIMIT)), - Builder::Mutable); - wasm.addGlobal(glob); + auto glob = builder.makeGlobal(HANG_LIMIT_GLOBAL, + Type::i32, + builder.makeConst(int32_t(HANG_LIMIT)), + Builder::Mutable); + wasm.addGlobal(std::move(glob)); Name exportName = "hangLimitInitializer"; auto funcName = Names::getValidFunctionName(wasm, exportName); diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 205a9b447..ef3f9c9d1 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -1362,7 +1362,7 @@ public: Index endOfFunction = -1; // we store globals here before wasm.addGlobal after we know their names - std::vector<Global*> globals; + std::vector<std::unique_ptr<Global>> globals; // we store global imports here before wasm.addGlobalImport after we know // their names std::vector<Global*> globalImports; diff --git a/src/wasm-builder.h b/src/wasm-builder.h index d448cbd94..d3af93896 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -41,11 +41,11 @@ public: // make* functions, other globals - Function* makeFunction(Name name, - Signature sig, - std::vector<Type>&& vars, - Expression* body = nullptr) { - auto* func = new Function; + static std::unique_ptr<Function> makeFunction(Name name, + Signature sig, + std::vector<Type>&& vars, + Expression* body = nullptr) { + auto func = std::make_unique<Function>(); func->name = name; func->sig = sig; func->body = body; @@ -53,12 +53,12 @@ public: return func; } - Function* makeFunction(Name name, - std::vector<NameType>&& params, - Type resultType, - std::vector<NameType>&& vars, - Expression* body = nullptr) { - auto* func = new Function; + static std::unique_ptr<Function> makeFunction(Name name, + std::vector<NameType>&& params, + Type resultType, + std::vector<NameType>&& vars, + Expression* body = nullptr) { + auto func = std::make_unique<Function>(); func->name = name; func->body = body; std::vector<Type> paramVec; @@ -78,14 +78,36 @@ public: return func; } - Export* makeExport(Name name, Name value, ExternalKind kind) { - auto* export_ = new Export(); + static std::unique_ptr<Export> + makeExport(Name name, Name value, ExternalKind kind) { + auto export_ = std::make_unique<Export>(); export_->name = name; export_->value = value; export_->kind = kind; return export_; } + enum Mutability { Mutable, Immutable }; + + static std::unique_ptr<Global> + makeGlobal(Name name, Type type, Expression* init, Mutability mutable_) { + auto glob = std::make_unique<Global>(); + glob->name = name; + glob->type = type; + glob->init = init; + glob->mutable_ = mutable_ == Mutable; + return glob; + } + + static std::unique_ptr<Event> + makeEvent(Name name, uint32_t attribute, Signature sig) { + auto event = std::make_unique<Event>(); + event->name = name; + event->attribute = attribute; + event->sig = sig; + return event; + } + // IR nodes Nop* makeNop() { return wasm.allocator.alloc<Nop>(); } @@ -969,28 +991,6 @@ public: } return makeConst(value); } - - // Module-level helpers - - enum Mutability { Mutable, Immutable }; - - static Global* - makeGlobal(Name name, Type type, Expression* init, Mutability mutable_) { - auto* glob = new Global; - glob->name = name; - glob->type = type; - glob->init = init; - glob->mutable_ = mutable_ == Mutable; - return glob; - } - - static Event* makeEvent(Name name, uint32_t attribute, Signature sig) { - auto* event = new Event; - event->name = name; - event->attribute = attribute; - event->sig = sig; - return event; - } }; } // namespace wasm diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 1a5c8a0f4..b343c6caf 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1474,11 +1474,11 @@ void WasmBinaryBuilder::readImports() { throwError("invalid function index " + std::to_string(index) + " / " + std::to_string(signatures.size())); } - auto* curr = builder.makeFunction(name, signatures[index], {}); + auto curr = builder.makeFunction(name, signatures[index], {}); curr->module = module; curr->base = base; - wasm.addFunction(curr); - functionImports.push_back(curr); + functionImports.push_back(curr.get()); + wasm.addFunction(std::move(curr)); break; } case ExternalKind::Table: { @@ -1524,15 +1524,15 @@ void WasmBinaryBuilder::readImports() { Name name(std::string("gimport$") + std::to_string(globalCounter++)); auto type = getConcreteType(); auto mutable_ = getU32LEB(); - auto* curr = + auto curr = builder.makeGlobal(name, type, nullptr, mutable_ ? Builder::Mutable : Builder::Immutable); curr->module = module; curr->base = base; - wasm.addGlobal(curr); - globalImports.push_back(curr); + globalImports.push_back(curr.get()); + wasm.addGlobal(std::move(curr)); break; } case ExternalKind::Event: { @@ -1543,10 +1543,10 @@ void WasmBinaryBuilder::readImports() { throwError("invalid event index " + std::to_string(index) + " / " + std::to_string(signatures.size())); } - auto* curr = builder.makeEvent(name, attribute, signatures[index]); + auto curr = builder.makeEvent(name, attribute, signatures[index]); curr->module = module; curr->base = base; - wasm.addEvent(curr); + wasm.addEvent(std::move(curr)); break; } default: { @@ -2065,8 +2065,8 @@ void WasmBinaryBuilder::processNames() { for (auto* func : functions) { wasm.addFunction(func); } - for (auto* global : globals) { - wasm.addGlobal(global); + for (auto& global : globals) { + wasm.addGlobal(std::move(global)); } // now that we have names, apply things @@ -3123,7 +3123,7 @@ void WasmBinaryBuilder::visitGlobalGet(GlobalGet* curr) { if (adjustedIndex >= globals.size()) { throwError("invalid global index"); } - auto* glob = globals[adjustedIndex]; + auto& glob = globals[adjustedIndex]; curr->name = glob->name; curr->type = glob->type; } |