From 6c3e2abc76ec096468f18205dae81a345efbc8de Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Tue, 10 Jan 2023 20:12:00 +0800 Subject: [PATCH] Implement stage-2 of taskpool Issue:I69V3K Signed-off-by: ctw-ian Change-Id: I1819a09edb7b5017a25d7f6c1179f206eb872401 --- es2panda/binder/binder.cpp | 2 +- es2panda/binder/declaration.h | 5 +++ es2panda/binder/scope.h | 11 ----- es2panda/compiler/core/function.cpp | 3 -- es2panda/compiler/core/pandagen.cpp | 5 --- es2panda/compiler/core/pandagen.h | 1 - ...oncurrent-only-in-top-scope-2-expected.txt | 2 - ...ing-const-lexical-variable-4-expected.txt} | 2 +- ...1.js => using-const-lexical-variable-4.js} | 9 ++-- ...ng-mutable-lexical-variable-5-expected.txt | 2 + ...js => using-mutable-lexical-variable-5.js} | 10 ++--- es2panda/util/concurrent.cpp | 41 +++---------------- es2panda/util/concurrent.h | 8 ++-- 13 files changed, 28 insertions(+), 73 deletions(-) delete mode 100644 es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2-expected.txt rename es2panda/test/parser/concurrent/{use-concurrent-only-in-top-scope-1-expected.txt => using-const-lexical-variable-4-expected.txt} (30%) rename es2panda/test/parser/concurrent/{use-concurrent-only-in-top-scope-1.js => using-const-lexical-variable-4.js} (90%) create mode 100644 es2panda/test/parser/concurrent/using-mutable-lexical-variable-5-expected.txt rename es2panda/test/parser/concurrent/{use-concurrent-only-in-top-scope-2.js => using-mutable-lexical-variable-5.js} (89%) diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index a8fe039c80c..f7dd8b5d568 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -227,7 +227,7 @@ void Binder::LookupIdentReference(ir::Identifier *ident) if (res.level != 0) { ASSERT(res.variable); - util::Concurrent::VerifyConstLexicalVarForConcurrentFunction(Program()->GetLineIndex(), ident, res); + util::Concurrent::VerifyImportVarForConcurrentFunction(Program()->GetLineIndex(), ident, res); res.variable->SetLexical(res.scope, program_->HotfixHelper()); } diff --git a/es2panda/binder/declaration.h b/es2panda/binder/declaration.h index 8e20c759a21..e1b1ad05b7f 100644 --- a/es2panda/binder/declaration.h +++ b/es2panda/binder/declaration.h @@ -100,6 +100,11 @@ public: return (flags_ & flag) != 0; } + bool IsImportDecl() const + { + return HasFlag(DeclarationFlags::IMPORT); + } + bool IsImportOrExportDecl() const { return HasFlag(DeclarationFlags::IMPORT | DeclarationFlags::EXPORT); diff --git a/es2panda/binder/scope.h b/es2panda/binder/scope.h index 19e31ec9a41..d2bc3419d42 100644 --- a/es2panda/binder/scope.h +++ b/es2panda/binder/scope.h @@ -592,23 +592,12 @@ public: return internalName_; } - void UseConcurrent() - { - hasConcurrent_ = true; - } - - bool HasConcurrent() const - { - return hasConcurrent_; - } - bool AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension) override; private: util::StringView name_ {}; util::StringView internalName_ {}; - bool hasConcurrent_ {false}; }; class LocalScope : public Scope { diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index a149872d70b..89c321bb548 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include namespace panda::es2panda::compiler { @@ -187,8 +186,6 @@ static void CompileFunction(PandaGen *pg) static void CompileFunctionOrProgram(PandaGen *pg) { - util::Concurrent::StoreEnvForConcurrent(pg, pg->RootNode()); - FunctionRegScope lrs(pg); const auto *topScope = pg->TopScope(); diff --git a/es2panda/compiler/core/pandagen.cpp b/es2panda/compiler/core/pandagen.cpp index ae5017dd6f5..605cd7defa8 100644 --- a/es2panda/compiler/core/pandagen.cpp +++ b/es2panda/compiler/core/pandagen.cpp @@ -1841,11 +1841,6 @@ void PandaGen::StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t StoreLexicalVar(node, level, slot, value); } -void PandaGen::StoreLexicalEnv(const ir::AstNode *node) -{ - ra_.Emit(node); -} - void PandaGen::ThrowIfSuperNotCorrectCall(const ir::AstNode *node, int64_t num) { ra_.Emit(node, num); diff --git a/es2panda/compiler/core/pandagen.h b/es2panda/compiler/core/pandagen.h index b947192568f..dd707b12a57 100644 --- a/es2panda/compiler/core/pandagen.h +++ b/es2panda/compiler/core/pandagen.h @@ -428,7 +428,6 @@ public: void StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot); void StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot, const binder::LocalVariable *local); void StoreLexicalVar(const ir::AstNode *node, uint32_t level, uint32_t slot, VReg value); - void StoreLexicalEnv(const ir::AstNode *node); void ThrowIfSuperNotCorrectCall(const ir::AstNode *node, int64_t num); void ThrowUndefinedIfHole(const ir::AstNode *node, const util::StringView &name); diff --git a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2-expected.txt b/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2-expected.txt deleted file mode 100644 index 3114f7b8cf3..00000000000 --- a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2-expected.txt +++ /dev/null @@ -1,2 +0,0 @@ -Error: Concurrent function should only be defined in top-level scope [use-concurrent-only-in-top-scope-2.js:19:8] -the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1-expected.txt b/es2panda/test/parser/concurrent/using-const-lexical-variable-4-expected.txt similarity index 30% rename from es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1-expected.txt rename to es2panda/test/parser/concurrent/using-const-lexical-variable-4-expected.txt index 1add0331da1..6565cc7a652 100644 --- a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1-expected.txt +++ b/es2panda/test/parser/concurrent/using-const-lexical-variable-4-expected.txt @@ -1,2 +1,2 @@ -Error: Concurrent function should only be defined in top-level scope [use-concurrent-only-in-top-scope-1.js:18:8] +Error: Concurrent function should only use import variable or local variable [using-const-lexical-variable-4.js:20:11] the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1.js b/es2panda/test/parser/concurrent/using-const-lexical-variable-4.js similarity index 90% rename from es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1.js rename to es2panda/test/parser/concurrent/using-const-lexical-variable-4.js index 793cb030eb0..442fad97134 100644 --- a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-1.js +++ b/es2panda/test/parser/concurrent/using-const-lexical-variable-4.js @@ -14,8 +14,9 @@ */ "use strict"; -{ - function a() { - "use concurrent"; - } +const a = 1; + +function b() { + "use concurrent"; + return a; } diff --git a/es2panda/test/parser/concurrent/using-mutable-lexical-variable-5-expected.txt b/es2panda/test/parser/concurrent/using-mutable-lexical-variable-5-expected.txt new file mode 100644 index 00000000000..c07b60b0478 --- /dev/null +++ b/es2panda/test/parser/concurrent/using-mutable-lexical-variable-5-expected.txt @@ -0,0 +1,2 @@ +Error: Concurrent function should only use import variable or local variable [using-mutable-lexical-variable-5.js:20:11] +the size of programs is expected to be 1, but is 0 diff --git a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2.js b/es2panda/test/parser/concurrent/using-mutable-lexical-variable-5.js similarity index 89% rename from es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2.js rename to es2panda/test/parser/concurrent/using-mutable-lexical-variable-5.js index 1543a1e2cc5..8f33d4799b3 100644 --- a/es2panda/test/parser/concurrent/use-concurrent-only-in-top-scope-2.js +++ b/es2panda/test/parser/concurrent/using-mutable-lexical-variable-5.js @@ -14,9 +14,9 @@ */ "use strict"; +export let a = 1; -function a() { - function b() { - "use concurrent"; - } -} \ No newline at end of file +function b() { + "use concurrent"; + return a; +} diff --git a/es2panda/util/concurrent.cpp b/es2panda/util/concurrent.cpp index 2eb77e9f3c4..d6959c30ed3 100644 --- a/es2panda/util/concurrent.cpp +++ b/es2panda/util/concurrent.cpp @@ -72,21 +72,7 @@ void Concurrent::SetConcurrent(ir::ScriptFunction *func, const lexer::LineIndex ThrowInvalidConcurrentFunction(lineIndex, stmt, ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION); } - // concurrent function should be defined in top-level scope - if (!func->Parent()->Parent()->IsProgram()) { - ThrowInvalidConcurrentFunction(lineIndex, stmt, ConcurrentInvalidFlag::NOT_TOP_LEVEL); - } - func->AddFlag(ir::ScriptFunctionFlags::CONCURRENT); - auto *funcScope = func->Scope()->Parent(); - while (funcScope) { - if (funcScope->IsGlobalScope() || funcScope->IsModuleScope()) { - (static_cast(funcScope))->UseConcurrent(); - break; - } - - funcScope = funcScope->Parent(); - } } void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *expr, @@ -95,17 +81,12 @@ void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineInde auto line = expr->Range().start.line; auto column = (const_cast(lineIndex)).GetLocation(expr->Range().start).col - 1; switch (errFlag) { - case ConcurrentInvalidFlag::NOT_TOP_LEVEL: { - throw Error {ErrorType::GENERIC, "Concurrent function should only be defined in top-level scope", line, - column}; - break; - } case ConcurrentInvalidFlag::NOT_ORDINARY_FUNCTION: { throw Error {ErrorType::GENERIC, "Concurrent function should only be function declaration", line, column}; break; } - case ConcurrentInvalidFlag::USING_MUTABLE_VARIABLE: { + case ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE: { throw Error {ErrorType::GENERIC, "Concurrent function should only use import variable or local variable", line, column}; break; @@ -115,28 +96,18 @@ void Concurrent::ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineInde } } -void Concurrent::StoreEnvForConcurrent(compiler::PandaGen *pg, const ir::AstNode *node) -{ - // Store env for concurrent should only be implemented in Top-Level scope - if (!pg->TopScope()->IsGlobalScope() && !pg->TopScope()->IsModuleScope()) { - return; - } - - if (pg->TopScope()->HasConcurrent()) { - pg->StoreLexicalEnv(node); - } -} - -void Concurrent::VerifyConstLexicalVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node, +void Concurrent::VerifyImportVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node, const binder::ScopeFindResult &result) { if (!result.crossConcurrent) { return; } - if (!result.variable->Declaration()->IsConstDecl()) { - ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::USING_MUTABLE_VARIABLE); + if (result.variable->IsModuleVariable() && result.variable->Declaration()->IsImportDecl()) { + return; } + + ThrowInvalidConcurrentFunction(lineIndex, node, ConcurrentInvalidFlag::NOT_IMPORT_VARIABLE); } } // namespace panda::es2panda::util \ No newline at end of file diff --git a/es2panda/util/concurrent.h b/es2panda/util/concurrent.h index d7f4c701477..52fa2eda7fa 100644 --- a/es2panda/util/concurrent.h +++ b/es2panda/util/concurrent.h @@ -39,9 +39,8 @@ class LineIndex; namespace panda::es2panda::util { enum class ConcurrentInvalidFlag { - NOT_TOP_LEVEL = 1, - NOT_ORDINARY_FUNCTION = 2, - USING_MUTABLE_VARIABLE = 3 + NOT_ORDINARY_FUNCTION = 1, + NOT_IMPORT_VARIABLE = 2 }; class Concurrent { @@ -51,8 +50,7 @@ public: static void SetConcurrent(ir::ScriptFunction *func, const lexer::LineIndex &lineIndex); static void ThrowInvalidConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *expr, ConcurrentInvalidFlag errFlag); - static void StoreEnvForConcurrent(compiler::PandaGen *pg, const ir::AstNode *node); - static void VerifyConstLexicalVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node, + static void VerifyImportVarForConcurrentFunction(const lexer::LineIndex &lineIndex, const ir::AstNode *node, const binder::ScopeFindResult &result); static constexpr std::string_view USE_CONCURRENT = "use concurrent"; -- Gitee