diff --git a/include/runtime/instance/type.h b/include/runtime/instance/type.h index a20a3b238c7339a349020349ecae298f5c9bd17f..ed39eb230126341275511358b09565afce00871d 100644 --- a/include/runtime/instance/type.h +++ b/include/runtime/instance/type.h @@ -41,8 +41,6 @@ struct FType { /// Getter of symbol const auto &getSymbol() const noexcept { return Symbol; } - /// Setter of symbol - void setSymbol(Loader::Symbol S) { Symbol = std::move(S); } std::vector Params; std::vector Returns; diff --git a/include/runtime/storemgr.h b/include/runtime/storemgr.h index 236c955acd55f575f94a74a2a31e489e6d28ae02..88e1f92eab17a14cf0f5e67470600d426fde5115 100644 --- a/include/runtime/storemgr.h +++ b/include/runtime/storemgr.h @@ -55,27 +55,33 @@ public: ~StoreManager() = default; /// Import instances and move owner to store manager. - uint32_t importModule(std::unique_ptr Mod) { - Mod->Addr = ModInsts.size(); - return importInstance(std::move(Mod), ImpModInsts, ModInsts); + template uint32_t importModule(Args &&... Values) { + uint32_t ModAddr = + importInstance(ImpModInsts, ModInsts, std::forward(Values)...); + ModInsts.back()->Addr = ModAddr; + return ModAddr; } - uint32_t importFunction(std::unique_ptr Func) { - return importInstance(std::move(Func), ImpFuncInsts, FuncInsts); + template uint32_t importFunction(Args &&... Values) { + return importInstance(ImpFuncInsts, FuncInsts, + std::forward(Values)...); } - uint32_t importTable(std::unique_ptr Tab) { - return importInstance(std::move(Tab), ImpTabInsts, TabInsts); + template uint32_t importTable(Args &&... Values) { + return importInstance(ImpTabInsts, TabInsts, std::forward(Values)...); } - uint32_t importMemory(std::unique_ptr Mem) { - return importInstance(std::move(Mem), ImpMemInsts, MemInsts); + template uint32_t importMemory(Args &&... Values) { + return importInstance(ImpMemInsts, MemInsts, std::forward(Values)...); } - uint32_t importGlobal(std::unique_ptr Glob) { - return importInstance(std::move(Glob), ImpGlobInsts, GlobInsts); + template uint32_t importGlobal(Args &&... Values) { + return importInstance(ImpGlobInsts, GlobInsts, + std::forward(Values)...); } - uint32_t importElement(std::unique_ptr Elem) { - return importInstance(std::move(Elem), ImpElemInsts, ElemInsts); + template uint32_t importElement(Args &&... Values) { + return importInstance(ImpElemInsts, ElemInsts, + std::forward(Values)...); } - uint32_t importData(std::unique_ptr Data) { - return importInstance(std::move(Data), ImpDataInsts, DataInsts); + template uint32_t importData(Args &&... Values) { + return importInstance(ImpDataInsts, DataInsts, + std::forward(Values)...); } /// Import host instances but not move ownership. @@ -93,34 +99,40 @@ public: } /// Insert instances for instantiation and move ownership to store manager. - uint32_t pushModule(std::unique_ptr Mod) { + template uint32_t pushModule(Args &&... Values) { ++NumMod; - Mod->Addr = ModInsts.size(); - return importInstance(std::move(Mod), ImpModInsts, ModInsts); + uint32_t ModAddr = + importInstance(ImpModInsts, ModInsts, std::forward(Values)...); + ModInsts.back()->Addr = ModAddr; + return ModAddr; } - uint32_t pushFunction(std::unique_ptr Func) { + template uint32_t pushFunction(Args &&... Values) { ++NumFunc; - return importInstance(std::move(Func), ImpFuncInsts, FuncInsts); + return importInstance(ImpFuncInsts, FuncInsts, + std::forward(Values)...); } - uint32_t pushTable(std::unique_ptr Tab) { + template uint32_t pushTable(Args &&... Values) { ++NumTab; - return importInstance(std::move(Tab), ImpTabInsts, TabInsts); + return importInstance(ImpTabInsts, TabInsts, std::forward(Values)...); } - uint32_t pushMemory(std::unique_ptr Mem) { + template uint32_t pushMemory(Args &&... Values) { ++NumMem; - return importInstance(std::move(Mem), ImpMemInsts, MemInsts); + return importInstance(ImpMemInsts, MemInsts, std::forward(Values)...); } - uint32_t pushGlobal(std::unique_ptr Glob) { + template uint32_t pushGlobal(Args &&... Values) { ++NumGlob; - return importInstance(std::move(Glob), ImpGlobInsts, GlobInsts); + return importInstance(ImpGlobInsts, GlobInsts, + std::forward(Values)...); } - uint32_t pushElement(std::unique_ptr Elem) { + template uint32_t pushElement(Args &&... Values) { ++NumElem; - return importInstance(std::move(Elem), ImpElemInsts, ElemInsts); + return importInstance(ImpElemInsts, ElemInsts, + std::forward(Values)...); } - uint32_t pushData(std::unique_ptr Data) { + template uint32_t pushData(Args &&... Values) { ++NumData; - return importInstance(std::move(Data), ImpDataInsts, DataInsts); + return importInstance(ImpDataInsts, DataInsts, + std::forward(Values)...); } /// Pop temp. module. Dangerous function for used when instantiating only. @@ -266,14 +278,13 @@ public: private: /// Helper function for importing instances and move ownership. - template + template std::enable_if_t, uint32_t> - importInstance(std::unique_ptr Inst, - std::vector> &ImpInstsVec, - std::vector &InstsVec) { + importInstance(std::vector> &ImpInstsVec, + std::vector &InstsVec, Args &&... Values) { uint32_t Addr = InstsVec.size(); - InstsVec.push_back(Inst.get()); - ImpInstsVec.push_back(std::move(Inst)); + ImpInstsVec.push_back(std::make_unique(std::forward(Values)...)); + InstsVec.push_back(ImpInstsVec.back().get()); return Addr; } diff --git a/lib/interpreter/instantiate/data.cpp b/lib/interpreter/instantiate/data.cpp index 0dcd694ea62c449cdaf9cdb6ca9cf8516194fc9c..d71adb062bd7854ee6c825cd8f23c329a1c176a8 100644 --- a/lib/interpreter/instantiate/data.cpp +++ b/lib/interpreter/instantiate/data.cpp @@ -42,16 +42,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, } } - /// Make a new data instance. - auto NewDataInst = std::make_unique( - Offset, DataSeg.getData()); - /// Insert data instance to store manager. uint32_t NewDataInstAddr; if (InsMode == InstantiateMode::Instantiate) { - NewDataInstAddr = StoreMgr.pushData(std::move(NewDataInst)); + NewDataInstAddr = StoreMgr.pushData(Offset, DataSeg.getData()); } else { - NewDataInstAddr = StoreMgr.importData(std::move(NewDataInst)); + NewDataInstAddr = StoreMgr.importData(Offset, DataSeg.getData()); } ModInst.addDataAddr(NewDataInstAddr); } diff --git a/lib/interpreter/instantiate/elem.cpp b/lib/interpreter/instantiate/elem.cpp index 8bc10ce09e98406ad8a4dac8d0379b54d474641f..9a5239699c54c8b26eb08c5a934ad8eae67d33f4 100644 --- a/lib/interpreter/instantiate/elem.cpp +++ b/lib/interpreter/instantiate/elem.cpp @@ -53,16 +53,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, } } - /// Make a new element instance. - auto NewElemInst = std::make_unique( - Offset, ElemSeg.getRefType(), InitVals); - /// Insert element instance to store manager. uint32_t NewElemInstAddr; if (InsMode == InstantiateMode::Instantiate) { - NewElemInstAddr = StoreMgr.pushElement(std::move(NewElemInst)); + NewElemInstAddr = + StoreMgr.pushElement(Offset, ElemSeg.getRefType(), InitVals); } else { - NewElemInstAddr = StoreMgr.importElement(std::move(NewElemInst)); + NewElemInstAddr = + StoreMgr.importElement(Offset, ElemSeg.getRefType(), InitVals); } ModInst.addElemAddr(NewElemInstAddr); } diff --git a/lib/interpreter/instantiate/function.cpp b/lib/interpreter/instantiate/function.cpp index 32906e5fee5814891829956a842878a5ecbb7b35..ae06d991b84102fd88eddaa987eee813b5b01258 100644 --- a/lib/interpreter/instantiate/function.cpp +++ b/lib/interpreter/instantiate/function.cpp @@ -18,22 +18,27 @@ Expect Interpreter::instantiate( /// Iterate through code segments to make function instances. for (uint32_t I = 0; I < CodeSegs.size(); ++I) { - /// Make a new function instance. - auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]); - auto NewFuncInst = std::make_unique( - ModInst.Addr, *FuncType, CodeSegs[I].getLocals(), - CodeSegs[I].getInstrs()); - - if (auto Symbol = CodeSegs[I].getSymbol()) { - NewFuncInst->setSymbol(std::move(Symbol)); - } - /// Insert function instance to store manager. uint32_t NewFuncInstAddr; + auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]); if (InsMode == InstantiateMode::Instantiate) { - NewFuncInstAddr = StoreMgr.pushFunction(std::move(NewFuncInst)); + if (auto Symbol = CodeSegs[I].getSymbol()) { + NewFuncInstAddr = + StoreMgr.pushFunction(ModInst.Addr, *FuncType, std::move(Symbol)); + } else { + NewFuncInstAddr = StoreMgr.pushFunction(ModInst.Addr, *FuncType, + CodeSegs[I].getLocals(), + CodeSegs[I].getInstrs()); + } } else { - NewFuncInstAddr = StoreMgr.importFunction(std::move(NewFuncInst)); + if (auto Symbol = CodeSegs[I].getSymbol()) { + NewFuncInstAddr = + StoreMgr.importFunction(ModInst.Addr, *FuncType, std::move(Symbol)); + } else { + NewFuncInstAddr = StoreMgr.importFunction(ModInst.Addr, *FuncType, + CodeSegs[I].getLocals(), + CodeSegs[I].getInstrs()); + } } ModInst.addFuncAddr(NewFuncInstAddr); } diff --git a/lib/interpreter/instantiate/global.cpp b/lib/interpreter/instantiate/global.cpp index cbd1e4944690f77fee5dad750130073396172381..6ec0b88a3e338c49fe2b3db6f27ffe3e7d47b894 100644 --- a/lib/interpreter/instantiate/global.cpp +++ b/lib/interpreter/instantiate/global.cpp @@ -15,10 +15,17 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, /// A frame with temp. module is pushed into stack outside. /// Instantiate and initialize globals. for (const auto &GlobSeg : GlobSec.getContent()) { - /// Make a new global instance. + /// Insert global instance to store manager. const auto &GlobType = GlobSeg.getGlobalType(); - auto NewGlobInst = std::make_unique( - GlobType.getValueType(), GlobType.getValueMutation()); + uint32_t NewGlobInstAddr; + if (InsMode == InstantiateMode::Instantiate) { + NewGlobInstAddr = StoreMgr.pushGlobal(GlobType.getValueType(), + GlobType.getValueMutation()); + } else { + NewGlobInstAddr = StoreMgr.importGlobal(GlobType.getValueType(), + GlobType.getValueMutation()); + } + ModInst.addGlobalAddr(NewGlobInstAddr); /// Run initialize expression. if (auto Res = runExpression(StoreMgr, GlobSeg.getInstrs()); !Res) { @@ -27,16 +34,8 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, } /// Pop result from stack. + auto *NewGlobInst = *StoreMgr.getGlobal(NewGlobInstAddr); NewGlobInst->getValue() = StackMgr.pop(); - - /// Insert global instance to store manager. - uint32_t NewGlobInstAddr; - if (InsMode == InstantiateMode::Instantiate) { - NewGlobInstAddr = StoreMgr.pushGlobal(std::move(NewGlobInst)); - } else { - NewGlobInstAddr = StoreMgr.importGlobal(std::move(NewGlobInst)); - } - ModInst.addGlobalAddr(NewGlobInstAddr); } return {}; } diff --git a/lib/interpreter/instantiate/memory.cpp b/lib/interpreter/instantiate/memory.cpp index f1d7d2a141940ee19350f42987bfa226ffe215c6..2b8a4f94e4810d9930d249e316c430eb909b35a1 100644 --- a/lib/interpreter/instantiate/memory.cpp +++ b/lib/interpreter/instantiate/memory.cpp @@ -14,16 +14,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, const AST::MemorySection &MemSec) { /// Iterate and istantiate memory types. for (const auto &MemType : MemSec.getContent()) { - /// Make a new memory instance. - auto NewMemInst = - std::make_unique(MemType.getLimit()); - /// Insert memory instance to store manager. uint32_t NewMemInstAddr; if (InsMode == InstantiateMode::Instantiate) { - NewMemInstAddr = StoreMgr.pushMemory(std::move(NewMemInst)); + NewMemInstAddr = StoreMgr.pushMemory(MemType.getLimit()); } else { - NewMemInstAddr = StoreMgr.importMemory(std::move(NewMemInst)); + NewMemInstAddr = StoreMgr.importMemory(MemType.getLimit()); } ModInst.addMemAddr(NewMemInstAddr); } diff --git a/lib/interpreter/instantiate/module.cpp b/lib/interpreter/instantiate/module.cpp index 92d284c78917993ab2def1e46df6679345f234b0..ac9e20487d9b1844d852dab9bca8cf06350f69fb 100644 --- a/lib/interpreter/instantiate/module.cpp +++ b/lib/interpreter/instantiate/module.cpp @@ -22,14 +22,13 @@ Expect Interpreter::instantiate(Runtime::StoreManager &StoreMgr, LOG(ERROR) << ErrInfo::InfoAST(Mod.NodeAttr); return Unexpect(ErrCode::ModuleNameConflict); } - auto NewModInst = std::make_unique(Name); /// Insert the module instance to store manager and retrieve instance. uint32_t ModInstAddr; if (InsMode == InstantiateMode::Instantiate) { - ModInstAddr = StoreMgr.pushModule(std::move(NewModInst)); + ModInstAddr = StoreMgr.pushModule(Name); } else { - ModInstAddr = StoreMgr.importModule(std::move(NewModInst)); + ModInstAddr = StoreMgr.importModule(Name); } auto *ModInst = *StoreMgr.getModule(ModInstAddr); @@ -74,14 +73,14 @@ Expect Interpreter::instantiate(Runtime::StoreManager &StoreMgr, } /// Add a temp module to Store with only imported globals for initialization. - auto TmpMod = std::make_unique(""); + uint32_t TmpModInstAddr = StoreMgr.pushModule(""); + auto *TmpModInst = *StoreMgr.getModule(TmpModInstAddr); for (uint32_t I = 0; I < ModInst->getGlobalImportNum(); ++I) { - TmpMod->importGlobal(*(ModInst->getGlobalAddr(I))); + TmpModInst->importGlobal(*(ModInst->getGlobalAddr(I))); } for (uint32_t I = 0; I < ModInst->getFuncNum(); ++I) { - TmpMod->importFunction(*(ModInst->getFuncAddr(I))); + TmpModInst->importFunction(*(ModInst->getFuncAddr(I))); } - uint32_t TmpModInstAddr = StoreMgr.pushModule(std::move(TmpMod)); /// Push a new frame {TmpModInst:{globaddrs}, locals:none} StackMgr.pushFrame(TmpModInstAddr, 0, 0); diff --git a/lib/interpreter/instantiate/table.cpp b/lib/interpreter/instantiate/table.cpp index 0905e61a2f6eadc576719795627f5861ba0d76b0..2f7d689f468d99a952a6257858c1a08dd936775b 100644 --- a/lib/interpreter/instantiate/table.cpp +++ b/lib/interpreter/instantiate/table.cpp @@ -14,16 +14,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, const AST::TableSection &TabSec) { /// Iterate and instantiate table types. for (const auto &TabType : TabSec.getContent()) { - /// Make a new table instance. - auto NewTabInst = std::make_unique( - TabType.getReferenceType(), TabType.getLimit()); - /// Insert table instance to store manager. uint32_t NewTabInstAddr; if (InsMode == InstantiateMode::Instantiate) { - NewTabInstAddr = StoreMgr.pushTable(std::move(NewTabInst)); + NewTabInstAddr = + StoreMgr.pushTable(TabType.getReferenceType(), TabType.getLimit()); } else { - NewTabInstAddr = StoreMgr.importTable(std::move(NewTabInst)); + NewTabInstAddr = + StoreMgr.importTable(TabType.getReferenceType(), TabType.getLimit()); } ModInst.addTableAddr(NewTabInstAddr); } diff --git a/lib/interpreter/interpreter.cpp b/lib/interpreter/interpreter.cpp index 68229902a275c0e8b7db5ba0e9d6338e3ae159f8..185a2f002ac9736e47ac7ebb55394937ec9302f1 100644 --- a/lib/interpreter/interpreter.cpp +++ b/lib/interpreter/interpreter.cpp @@ -32,13 +32,10 @@ Expect Interpreter::registerModule(Runtime::StoreManager &StoreMgr, LOG(ERROR) << ErrInfo::InfoRegistering(Obj.getModuleName()); return Unexpect(ErrCode::ModuleNameConflict); } - auto NewModInst = - std::make_unique(Obj.getModuleName()); - auto ModInstAddr = StoreMgr.importModule(std::move(NewModInst)); + auto ModInstAddr = StoreMgr.importModule(Obj.getModuleName()); auto *ModInst = *StoreMgr.getModule(ModInstAddr); for (auto &Func : Obj.getFuncs()) { - Func.second->setModuleAddr(ModInstAddr); uint32_t Addr = StoreMgr.importHostFunction(*Func.second.get()); ModInst->addFuncAddr(Addr); ModInst->exportFunction(Func.first, ModInst->getFuncNum() - 1);