提交 ccd18560 编写于 作者: Y YiYing He 提交者: hydai

[Runtime] Refactor push functions in store manager.

上级 31d2be18
...@@ -41,8 +41,6 @@ struct FType { ...@@ -41,8 +41,6 @@ struct FType {
/// Getter of symbol /// Getter of symbol
const auto &getSymbol() const noexcept { return Symbol; } const auto &getSymbol() const noexcept { return Symbol; }
/// Setter of symbol
void setSymbol(Loader::Symbol<Wrapper> S) { Symbol = std::move(S); }
std::vector<ValType> Params; std::vector<ValType> Params;
std::vector<ValType> Returns; std::vector<ValType> Returns;
......
...@@ -55,27 +55,33 @@ public: ...@@ -55,27 +55,33 @@ public:
~StoreManager() = default; ~StoreManager() = default;
/// Import instances and move owner to store manager. /// Import instances and move owner to store manager.
uint32_t importModule(std::unique_ptr<Instance::ModuleInstance> Mod) { template <typename... Args> uint32_t importModule(Args &&... Values) {
Mod->Addr = ModInsts.size(); uint32_t ModAddr =
return importInstance(std::move(Mod), ImpModInsts, ModInsts); importInstance(ImpModInsts, ModInsts, std::forward<Args>(Values)...);
ModInsts.back()->Addr = ModAddr;
return ModAddr;
} }
uint32_t importFunction(std::unique_ptr<Instance::FunctionInstance> Func) { template <typename... Args> uint32_t importFunction(Args &&... Values) {
return importInstance(std::move(Func), ImpFuncInsts, FuncInsts); return importInstance(ImpFuncInsts, FuncInsts,
std::forward<Args>(Values)...);
} }
uint32_t importTable(std::unique_ptr<Instance::TableInstance> Tab) { template <typename... Args> uint32_t importTable(Args &&... Values) {
return importInstance(std::move(Tab), ImpTabInsts, TabInsts); return importInstance(ImpTabInsts, TabInsts, std::forward<Args>(Values)...);
} }
uint32_t importMemory(std::unique_ptr<Instance::MemoryInstance> Mem) { template <typename... Args> uint32_t importMemory(Args &&... Values) {
return importInstance(std::move(Mem), ImpMemInsts, MemInsts); return importInstance(ImpMemInsts, MemInsts, std::forward<Args>(Values)...);
} }
uint32_t importGlobal(std::unique_ptr<Instance::GlobalInstance> Glob) { template <typename... Args> uint32_t importGlobal(Args &&... Values) {
return importInstance(std::move(Glob), ImpGlobInsts, GlobInsts); return importInstance(ImpGlobInsts, GlobInsts,
std::forward<Args>(Values)...);
} }
uint32_t importElement(std::unique_ptr<Instance::ElementInstance> Elem) { template <typename... Args> uint32_t importElement(Args &&... Values) {
return importInstance(std::move(Elem), ImpElemInsts, ElemInsts); return importInstance(ImpElemInsts, ElemInsts,
std::forward<Args>(Values)...);
} }
uint32_t importData(std::unique_ptr<Instance::DataInstance> Data) { template <typename... Args> uint32_t importData(Args &&... Values) {
return importInstance(std::move(Data), ImpDataInsts, DataInsts); return importInstance(ImpDataInsts, DataInsts,
std::forward<Args>(Values)...);
} }
/// Import host instances but not move ownership. /// Import host instances but not move ownership.
...@@ -93,34 +99,40 @@ public: ...@@ -93,34 +99,40 @@ public:
} }
/// Insert instances for instantiation and move ownership to store manager. /// Insert instances for instantiation and move ownership to store manager.
uint32_t pushModule(std::unique_ptr<Instance::ModuleInstance> Mod) { template <typename... Args> uint32_t pushModule(Args &&... Values) {
++NumMod; ++NumMod;
Mod->Addr = ModInsts.size(); uint32_t ModAddr =
return importInstance(std::move(Mod), ImpModInsts, ModInsts); importInstance(ImpModInsts, ModInsts, std::forward<Args>(Values)...);
ModInsts.back()->Addr = ModAddr;
return ModAddr;
} }
uint32_t pushFunction(std::unique_ptr<Instance::FunctionInstance> Func) { template <typename... Args> uint32_t pushFunction(Args &&... Values) {
++NumFunc; ++NumFunc;
return importInstance(std::move(Func), ImpFuncInsts, FuncInsts); return importInstance(ImpFuncInsts, FuncInsts,
std::forward<Args>(Values)...);
} }
uint32_t pushTable(std::unique_ptr<Instance::TableInstance> Tab) { template <typename... Args> uint32_t pushTable(Args &&... Values) {
++NumTab; ++NumTab;
return importInstance(std::move(Tab), ImpTabInsts, TabInsts); return importInstance(ImpTabInsts, TabInsts, std::forward<Args>(Values)...);
} }
uint32_t pushMemory(std::unique_ptr<Instance::MemoryInstance> Mem) { template <typename... Args> uint32_t pushMemory(Args &&... Values) {
++NumMem; ++NumMem;
return importInstance(std::move(Mem), ImpMemInsts, MemInsts); return importInstance(ImpMemInsts, MemInsts, std::forward<Args>(Values)...);
} }
uint32_t pushGlobal(std::unique_ptr<Instance::GlobalInstance> Glob) { template <typename... Args> uint32_t pushGlobal(Args &&... Values) {
++NumGlob; ++NumGlob;
return importInstance(std::move(Glob), ImpGlobInsts, GlobInsts); return importInstance(ImpGlobInsts, GlobInsts,
std::forward<Args>(Values)...);
} }
uint32_t pushElement(std::unique_ptr<Instance::ElementInstance> Elem) { template <typename... Args> uint32_t pushElement(Args &&... Values) {
++NumElem; ++NumElem;
return importInstance(std::move(Elem), ImpElemInsts, ElemInsts); return importInstance(ImpElemInsts, ElemInsts,
std::forward<Args>(Values)...);
} }
uint32_t pushData(std::unique_ptr<Instance::DataInstance> Data) { template <typename... Args> uint32_t pushData(Args &&... Values) {
++NumData; ++NumData;
return importInstance(std::move(Data), ImpDataInsts, DataInsts); return importInstance(ImpDataInsts, DataInsts,
std::forward<Args>(Values)...);
} }
/// Pop temp. module. Dangerous function for used when instantiating only. /// Pop temp. module. Dangerous function for used when instantiating only.
...@@ -266,14 +278,13 @@ public: ...@@ -266,14 +278,13 @@ public:
private: private:
/// Helper function for importing instances and move ownership. /// Helper function for importing instances and move ownership.
template <typename T> template <typename T, typename... Args>
std::enable_if_t<IsInstanceV<T>, uint32_t> std::enable_if_t<IsInstanceV<T>, uint32_t>
importInstance(std::unique_ptr<T> Inst, importInstance(std::vector<std::unique_ptr<T>> &ImpInstsVec,
std::vector<std::unique_ptr<T>> &ImpInstsVec, std::vector<T *> &InstsVec, Args &&... Values) {
std::vector<T *> &InstsVec) {
uint32_t Addr = InstsVec.size(); uint32_t Addr = InstsVec.size();
InstsVec.push_back(Inst.get()); ImpInstsVec.push_back(std::make_unique<T>(std::forward<Args>(Values)...));
ImpInstsVec.push_back(std::move(Inst)); InstsVec.push_back(ImpInstsVec.back().get());
return Addr; return Addr;
} }
......
...@@ -42,16 +42,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -42,16 +42,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
} }
} }
/// Make a new data instance.
auto NewDataInst = std::make_unique<Runtime::Instance::DataInstance>(
Offset, DataSeg.getData());
/// Insert data instance to store manager. /// Insert data instance to store manager.
uint32_t NewDataInstAddr; uint32_t NewDataInstAddr;
if (InsMode == InstantiateMode::Instantiate) { if (InsMode == InstantiateMode::Instantiate) {
NewDataInstAddr = StoreMgr.pushData(std::move(NewDataInst)); NewDataInstAddr = StoreMgr.pushData(Offset, DataSeg.getData());
} else { } else {
NewDataInstAddr = StoreMgr.importData(std::move(NewDataInst)); NewDataInstAddr = StoreMgr.importData(Offset, DataSeg.getData());
} }
ModInst.addDataAddr(NewDataInstAddr); ModInst.addDataAddr(NewDataInstAddr);
} }
......
...@@ -53,16 +53,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -53,16 +53,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
} }
} }
/// Make a new element instance.
auto NewElemInst = std::make_unique<Runtime::Instance::ElementInstance>(
Offset, ElemSeg.getRefType(), InitVals);
/// Insert element instance to store manager. /// Insert element instance to store manager.
uint32_t NewElemInstAddr; uint32_t NewElemInstAddr;
if (InsMode == InstantiateMode::Instantiate) { if (InsMode == InstantiateMode::Instantiate) {
NewElemInstAddr = StoreMgr.pushElement(std::move(NewElemInst)); NewElemInstAddr =
StoreMgr.pushElement(Offset, ElemSeg.getRefType(), InitVals);
} else { } else {
NewElemInstAddr = StoreMgr.importElement(std::move(NewElemInst)); NewElemInstAddr =
StoreMgr.importElement(Offset, ElemSeg.getRefType(), InitVals);
} }
ModInst.addElemAddr(NewElemInstAddr); ModInst.addElemAddr(NewElemInstAddr);
} }
......
...@@ -18,22 +18,27 @@ Expect<void> Interpreter::instantiate( ...@@ -18,22 +18,27 @@ Expect<void> Interpreter::instantiate(
/// Iterate through code segments to make function instances. /// Iterate through code segments to make function instances.
for (uint32_t I = 0; I < CodeSegs.size(); ++I) { 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<Runtime::Instance::FunctionInstance>(
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. /// Insert function instance to store manager.
uint32_t NewFuncInstAddr; uint32_t NewFuncInstAddr;
auto *FuncType = *ModInst.getFuncType(TypeIdxs[I]);
if (InsMode == InstantiateMode::Instantiate) { 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 { } 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); ModInst.addFuncAddr(NewFuncInstAddr);
} }
......
...@@ -15,10 +15,17 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -15,10 +15,17 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
/// A frame with temp. module is pushed into stack outside. /// A frame with temp. module is pushed into stack outside.
/// Instantiate and initialize globals. /// Instantiate and initialize globals.
for (const auto &GlobSeg : GlobSec.getContent()) { for (const auto &GlobSeg : GlobSec.getContent()) {
/// Make a new global instance. /// Insert global instance to store manager.
const auto &GlobType = GlobSeg.getGlobalType(); const auto &GlobType = GlobSeg.getGlobalType();
auto NewGlobInst = std::make_unique<Runtime::Instance::GlobalInstance>( uint32_t NewGlobInstAddr;
GlobType.getValueType(), GlobType.getValueMutation()); 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. /// Run initialize expression.
if (auto Res = runExpression(StoreMgr, GlobSeg.getInstrs()); !Res) { if (auto Res = runExpression(StoreMgr, GlobSeg.getInstrs()); !Res) {
...@@ -27,16 +34,8 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -27,16 +34,8 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
} }
/// Pop result from stack. /// Pop result from stack.
auto *NewGlobInst = *StoreMgr.getGlobal(NewGlobInstAddr);
NewGlobInst->getValue() = StackMgr.pop(); 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 {}; return {};
} }
......
...@@ -14,16 +14,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -14,16 +14,12 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
const AST::MemorySection &MemSec) { const AST::MemorySection &MemSec) {
/// Iterate and istantiate memory types. /// Iterate and istantiate memory types.
for (const auto &MemType : MemSec.getContent()) { for (const auto &MemType : MemSec.getContent()) {
/// Make a new memory instance.
auto NewMemInst =
std::make_unique<Runtime::Instance::MemoryInstance>(MemType.getLimit());
/// Insert memory instance to store manager. /// Insert memory instance to store manager.
uint32_t NewMemInstAddr; uint32_t NewMemInstAddr;
if (InsMode == InstantiateMode::Instantiate) { if (InsMode == InstantiateMode::Instantiate) {
NewMemInstAddr = StoreMgr.pushMemory(std::move(NewMemInst)); NewMemInstAddr = StoreMgr.pushMemory(MemType.getLimit());
} else { } else {
NewMemInstAddr = StoreMgr.importMemory(std::move(NewMemInst)); NewMemInstAddr = StoreMgr.importMemory(MemType.getLimit());
} }
ModInst.addMemAddr(NewMemInstAddr); ModInst.addMemAddr(NewMemInstAddr);
} }
......
...@@ -22,14 +22,13 @@ Expect<void> Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -22,14 +22,13 @@ Expect<void> Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
LOG(ERROR) << ErrInfo::InfoAST(Mod.NodeAttr); LOG(ERROR) << ErrInfo::InfoAST(Mod.NodeAttr);
return Unexpect(ErrCode::ModuleNameConflict); return Unexpect(ErrCode::ModuleNameConflict);
} }
auto NewModInst = std::make_unique<Runtime::Instance::ModuleInstance>(Name);
/// Insert the module instance to store manager and retrieve instance. /// Insert the module instance to store manager and retrieve instance.
uint32_t ModInstAddr; uint32_t ModInstAddr;
if (InsMode == InstantiateMode::Instantiate) { if (InsMode == InstantiateMode::Instantiate) {
ModInstAddr = StoreMgr.pushModule(std::move(NewModInst)); ModInstAddr = StoreMgr.pushModule(Name);
} else { } else {
ModInstAddr = StoreMgr.importModule(std::move(NewModInst)); ModInstAddr = StoreMgr.importModule(Name);
} }
auto *ModInst = *StoreMgr.getModule(ModInstAddr); auto *ModInst = *StoreMgr.getModule(ModInstAddr);
...@@ -74,14 +73,14 @@ Expect<void> Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -74,14 +73,14 @@ Expect<void> Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
} }
/// Add a temp module to Store with only imported globals for initialization. /// Add a temp module to Store with only imported globals for initialization.
auto TmpMod = std::make_unique<Runtime::Instance::ModuleInstance>(""); uint32_t TmpModInstAddr = StoreMgr.pushModule("");
auto *TmpModInst = *StoreMgr.getModule(TmpModInstAddr);
for (uint32_t I = 0; I < ModInst->getGlobalImportNum(); ++I) { 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) { 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} /// Push a new frame {TmpModInst:{globaddrs}, locals:none}
StackMgr.pushFrame(TmpModInstAddr, 0, 0); StackMgr.pushFrame(TmpModInstAddr, 0, 0);
......
...@@ -14,16 +14,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr, ...@@ -14,16 +14,14 @@ Interpreter::instantiate(Runtime::StoreManager &StoreMgr,
const AST::TableSection &TabSec) { const AST::TableSection &TabSec) {
/// Iterate and instantiate table types. /// Iterate and instantiate table types.
for (const auto &TabType : TabSec.getContent()) { for (const auto &TabType : TabSec.getContent()) {
/// Make a new table instance.
auto NewTabInst = std::make_unique<Runtime::Instance::TableInstance>(
TabType.getReferenceType(), TabType.getLimit());
/// Insert table instance to store manager. /// Insert table instance to store manager.
uint32_t NewTabInstAddr; uint32_t NewTabInstAddr;
if (InsMode == InstantiateMode::Instantiate) { if (InsMode == InstantiateMode::Instantiate) {
NewTabInstAddr = StoreMgr.pushTable(std::move(NewTabInst)); NewTabInstAddr =
StoreMgr.pushTable(TabType.getReferenceType(), TabType.getLimit());
} else { } else {
NewTabInstAddr = StoreMgr.importTable(std::move(NewTabInst)); NewTabInstAddr =
StoreMgr.importTable(TabType.getReferenceType(), TabType.getLimit());
} }
ModInst.addTableAddr(NewTabInstAddr); ModInst.addTableAddr(NewTabInstAddr);
} }
......
...@@ -32,13 +32,10 @@ Expect<void> Interpreter::registerModule(Runtime::StoreManager &StoreMgr, ...@@ -32,13 +32,10 @@ Expect<void> Interpreter::registerModule(Runtime::StoreManager &StoreMgr,
LOG(ERROR) << ErrInfo::InfoRegistering(Obj.getModuleName()); LOG(ERROR) << ErrInfo::InfoRegistering(Obj.getModuleName());
return Unexpect(ErrCode::ModuleNameConflict); return Unexpect(ErrCode::ModuleNameConflict);
} }
auto NewModInst = auto ModInstAddr = StoreMgr.importModule(Obj.getModuleName());
std::make_unique<Runtime::Instance::ModuleInstance>(Obj.getModuleName());
auto ModInstAddr = StoreMgr.importModule(std::move(NewModInst));
auto *ModInst = *StoreMgr.getModule(ModInstAddr); auto *ModInst = *StoreMgr.getModule(ModInstAddr);
for (auto &Func : Obj.getFuncs()) { for (auto &Func : Obj.getFuncs()) {
Func.second->setModuleAddr(ModInstAddr);
uint32_t Addr = StoreMgr.importHostFunction(*Func.second.get()); uint32_t Addr = StoreMgr.importHostFunction(*Func.second.get());
ModInst->addFuncAddr(Addr); ModInst->addFuncAddr(Addr);
ModInst->exportFunction(Func.first, ModInst->getFuncNum() - 1); ModInst->exportFunction(Func.first, ModInst->getFuncNum() - 1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册