From 736523850150b36c982380aad568c9975d3616ff Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 17 Aug 2022 11:31:14 +0800 Subject: [PATCH] Adjust module insn Signed-off-by: hufeng Change-Id: I99a6a1b84b2e485ad1f7c2c4323124a1d7fdea1f --- es2panda/binder/binder.cpp | 9 +++++++ es2panda/binder/binder.h | 1 + es2panda/binder/scope.cpp | 8 ++++++ es2panda/binder/scope.h | 2 ++ es2panda/binder/variable.h | 13 ++++++++++ es2panda/compiler/base/hoisting.cpp | 6 +++-- es2panda/compiler/core/pandagen.cpp | 25 ++++++++++--------- es2panda/compiler/core/pandagen.h | 6 ++--- .../ir/module/exportDefaultDeclaration.cpp | 5 +++- .../parser/module/sourceTextModuleRecord.cpp | 19 ++++++++++++++ .../parser/module/sourceTextModuleRecord.h | 6 +++++ 11 files changed, 82 insertions(+), 18 deletions(-) diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 3398132ee70..bf24a127466 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -87,6 +87,12 @@ void Binder::ThrowUndeclaredExport(const lexer::SourcePosition &pos, const util: throw Error(ErrorType::SYNTAX, ss.str(), loc.line, loc.col); } +void Binder::AssignIndexToModuleVariable() +{ + ASSERT(program_->ModuleRecord()); + program_->ModuleRecord()->AssignIndexToModuleVariable(topScope_->AsModuleScope()); +} + void Binder::IdentifierAnalysis() { ASSERT(program_->Ast()); @@ -95,6 +101,9 @@ void Binder::IdentifierAnalysis() BuildFunction(topScope_, MAIN_FUNC_NAME); ResolveReferences(program_->Ast()); AddMandatoryParams(); + if (topScope_->IsModuleScope()) { + AssignIndexToModuleVariable(); + } } void Binder::ValidateExportDecl(const ir::ExportNamedDeclaration *exportDecl) diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 97a7a0c735e..1418f39fbfd 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -153,6 +153,7 @@ private: } void AddMandatoryParams(); + void AssignIndexToModuleVariable(); void BuildFunction(FunctionScope *funcScope, util::StringView name); void BuildScriptFunction(Scope *outerScope, const ir::ScriptFunction *scriptFunc); void BuildClassDefinition(ir::ClassDefinition *classDef); diff --git a/es2panda/binder/scope.cpp b/es2panda/binder/scope.cpp index 9d222e2cd9a..4edb2d263c2 100644 --- a/es2panda/binder/scope.cpp +++ b/es2panda/binder/scope.cpp @@ -356,6 +356,14 @@ void ModuleScope::ConvertLocalVariableToModuleVariable(ArenaAllocator *allocator } } +void ModuleScope::AssignIndexToModuleVariable(util::StringView name, uint32_t index) +{ + auto *moduleVar = FindLocal(name); + ASSERT(moduleVar != nullptr); + ASSERT(moduleVar->IsModuleVariable()); + moduleVar->AsModuleVariable()->AssignIndex(index); +} + bool ModuleScope::AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension) { diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index a6046c098a5..ffa0df65000 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -599,6 +599,8 @@ public: return ScopeType::MODULE; } + void AssignIndexToModuleVariable(util::StringView name, uint32_t index); + void ConvertLocalVariableToModuleVariable(ArenaAllocator *allocator, util::StringView localName); bool AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, diff --git a/es2panda/binder/variable.h b/es2panda/binder/variable.h index 36190236d6e..00a6bbe2d1d 100644 --- a/es2panda/binder/variable.h +++ b/es2panda/binder/variable.h @@ -183,6 +183,19 @@ public: } void SetLexical([[maybe_unused]] Scope *scope) override; + + void AssignIndex(uint32_t index) + { + index_ = index; + } + + uint32_t Index() const + { + return index_; + } + +private: + uint32_t index_; }; class EnumVariable : public Variable { diff --git a/es2panda/compiler/base/hoisting.cpp b/es2panda/compiler/base/hoisting.cpp index e8b61fc97d3..02c8b078136 100644 --- a/es2panda/compiler/base/hoisting.cpp +++ b/es2panda/compiler/base/hoisting.cpp @@ -27,7 +27,9 @@ static void StoreModuleVarOrLocalVar(PandaGen *pg, binder::ScopeFindResult &resu { if (decl->IsImportOrExportDecl()) { ASSERT(pg->Scope()->IsModuleScope()); - pg->StoreModuleVariable(decl->Node(), decl->Name()); + auto *var = pg->Scope()->FindLocal(decl->Name()); + ASSERT(var->IsModuleVariable()); + pg->StoreModuleVariable(decl->Node(), var->AsModuleVariable()->Index()); } else { pg->StoreAccToLexEnv(decl->Node(), result, true); } @@ -84,7 +86,7 @@ static void HoistNameSpaceImports(PandaGen *pg) ASSERT(var != nullptr); auto *node = var->Declaration()->Node(); ASSERT(node != nullptr); - pg->GetModuleNamespace(node, nameSpaceEntry->localName_); + pg->GetModuleNamespace(node, nameSpaceEntry->moduleRequestIdx_); pg->StoreVar(node, {nameSpaceEntry->localName_, pg->TopScope(), 0, var}, true); } } diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index 78a49b5f538..18314fce6de 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -236,7 +236,7 @@ void PandaGen::LoadVar(const ir::Identifier *node, const binder::ScopeFindResult } if (var->IsModuleVariable()) { - LoadModuleVariable(node, var->Name(), var->HasFlag(binder::VariableFlags::LOCAL_EXPORT)); + LoadModuleVariable(node, var->AsModuleVariable()->Index(), var->HasFlag(binder::VariableFlags::LOCAL_EXPORT)); if (var->Declaration()->IsLetOrConstOrClassDecl()) { ThrowUndefinedIfHole(node, var->Name()); } @@ -278,12 +278,12 @@ void PandaGen::StoreVar(const ir::AstNode *node, const binder::ScopeFindResult & RegScope rs(this); VReg valueReg = AllocReg(); StoreAccumulator(node, valueReg); - LoadModuleVariable(node, var->Name(), true); + LoadModuleVariable(node, var->AsModuleVariable()->Index(), true); ThrowUndefinedIfHole(node, var->Name()); LoadAccumulator(node, valueReg); } - StoreModuleVariable(node, var->Name()); + StoreModuleVariable(node, var->AsModuleVariable()->Index()); return; } @@ -1463,22 +1463,23 @@ void PandaGen::DefineClassWithBuffer(const ir::AstNode *node, const util::String strings_.insert(ctorId); } -void PandaGen::LoadModuleVariable(const ir::AstNode *node, const util::StringView &name, bool isLocalExport) +void PandaGen::LoadModuleVariable(const ir::AstNode *node, uint32_t index, bool isLocalExport) { - ra_.Emit(node, name, isLocalExport ? static_cast(1) : static_cast(0)); - strings_.insert(name); + if (isLocalExport) { + sa_.Emit(node, index); + } else { + sa_.Emit(node, index); + } } -void PandaGen::StoreModuleVariable(const ir::AstNode *node, const util::StringView &name) +void PandaGen::StoreModuleVariable(const ir::AstNode *node, uint32_t index) { - sa_.Emit(node, name); - strings_.insert(name); + sa_.Emit(node, index); } -void PandaGen::GetModuleNamespace(const ir::AstNode *node, const util::StringView &name) +void PandaGen::GetModuleNamespace(const ir::AstNode *node, uint32_t index) { - sa_.Emit(node, name); - strings_.insert(name); + sa_.Emit(node, index); } void PandaGen::StSuperByName(const ir::AstNode *node, VReg obj, const util::StringView &key) diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index fc421d63f6f..d9055b2e798 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -348,9 +348,9 @@ public: void DefineClassWithBuffer(const ir::AstNode *node, const util::StringView &ctorId, int32_t litIdx, VReg lexenv, VReg base); - void LoadModuleVariable(const ir::AstNode *node, const util::StringView &name, bool isLocalExport); - void StoreModuleVariable(const ir::AstNode *node, const util::StringView &name); - void GetModuleNamespace(const ir::AstNode *node, const util::StringView &name); + void LoadModuleVariable(const ir::AstNode *node, uint32_t index, bool isLocalExport); + void StoreModuleVariable(const ir::AstNode *node, uint32_t index); + void GetModuleNamespace(const ir::AstNode *node, uint32_t index); void StSuperByName(const ir::AstNode *node, VReg obj, const util::StringView &key); void LdSuperByName(const ir::AstNode *node, VReg obj, const util::StringView &key); diff --git a/es2panda/ir/module/exportDefaultDeclaration.cpp b/es2panda/ir/module/exportDefaultDeclaration.cpp index 948b0d54e16..745e48dda40 100644 --- a/es2panda/ir/module/exportDefaultDeclaration.cpp +++ b/es2panda/ir/module/exportDefaultDeclaration.cpp @@ -37,7 +37,10 @@ void ExportDefaultDeclaration::Compile([[maybe_unused]] compiler::PandaGen *pg) if (decl_->IsExpression()) { // export default [AssignmentExpression] // e.g. export default 42 (42 be exported as [default]) - pg->StoreModuleVariable(this, parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME); + ASSERT(pg->Scope()->IsModuleScope()); + auto *var = pg->Scope()->FindLocal(parser::SourceTextModuleRecord::DEFAULT_LOCAL_NAME); + ASSERT(var->IsModuleVariable()); + pg->StoreModuleVariable(this, var->AsModuleVariable()->Index()); } } diff --git a/es2panda/parser/module/sourceTextModuleRecord.cpp b/es2panda/parser/module/sourceTextModuleRecord.cpp index 6a25328bcc4..e821f653148 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.cpp +++ b/es2panda/parser/module/sourceTextModuleRecord.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "sourceTextModuleRecord.h" namespace panda::es2panda::parser { @@ -160,4 +162,21 @@ namespace panda::es2panda::parser { exportEntry->moduleRequestIdx_ = importEntry->moduleRequestIdx_; exportEntry->localName_ = util::StringView(""); } + + void SourceTextModuleRecord::AssignIndexToModuleVariable(binder::ModuleScope *moduleScope) + { + uint32_t index = 0; + for (auto it = localExportEntries_.begin(); it != localExportEntries_.end(); + it = localExportEntries_.upper_bound(it->first)) + { + moduleScope->AssignIndexToModuleVariable(it->first, index); + index++; + } + + index = 0; + for (const auto &elem : regularImportEntries_) { + moduleScope->AssignIndexToModuleVariable(elem.first, index); + index++; + } + } } // namespace panda::es2panda::parser diff --git a/es2panda/parser/module/sourceTextModuleRecord.h b/es2panda/parser/module/sourceTextModuleRecord.h index f4a52bf09d8..50004bda17f 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.h +++ b/es2panda/parser/module/sourceTextModuleRecord.h @@ -18,6 +18,10 @@ #include +namespace panda::es2panda::binder { +class ModuleScope; +} + namespace panda::es2panda::parser { class SourceTextModuleRecord { public: @@ -80,6 +84,8 @@ public: bool CheckImplicitIndirectExport(ExportEntry *exportEntry); void CheckImplicitIndirectExport(ImportEntry *importEntry); + void AssignIndexToModuleVariable(binder::ModuleScope *moduleScope); + using ModuleRequestList = ArenaVector; using ModuleRequestMap = ArenaMap; using LocalExportEntryMap = ArenaMultiMap; -- Gitee