13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#ifndef ES2PANDA_COMPILER_CORE_COMPILEQUEUE_H
173af6ab5fSopenharmony_ci#define ES2PANDA_COMPILER_CORE_COMPILEQUEUE_H
183af6ab5fSopenharmony_ci
193af6ab5fSopenharmony_ci#include <aot/options.h>
203af6ab5fSopenharmony_ci#include <macros.h>
213af6ab5fSopenharmony_ci#include <os/thread.h>
223af6ab5fSopenharmony_ci#include <util/symbolTable.h>
233af6ab5fSopenharmony_ci#include <util/workerQueue.h>
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ci#include <condition_variable>
263af6ab5fSopenharmony_ci#include <mutex>
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_ci#include <abc2program/abc2program_compiler.h>
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_cinamespace panda::es2panda::binder {
313af6ab5fSopenharmony_ciclass FunctionScope;
323af6ab5fSopenharmony_ci}  // namespace panda::es2panda::binder
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_cinamespace panda::es2panda::compiler {
353af6ab5fSopenharmony_ci
363af6ab5fSopenharmony_ciclass CompilerContext;
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ciclass CompileFunctionJob : public util::WorkerJob {
393af6ab5fSopenharmony_cipublic:
403af6ab5fSopenharmony_ci    explicit CompileFunctionJob(CompilerContext *context) : context_(context) {};
413af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileFunctionJob);
423af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileFunctionJob);
433af6ab5fSopenharmony_ci    ~CompileFunctionJob() override = default;
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ci    binder::FunctionScope *Scope() const
463af6ab5fSopenharmony_ci    {
473af6ab5fSopenharmony_ci        return scope_;
483af6ab5fSopenharmony_ci    }
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_ci    void SetFunctionScope(binder::FunctionScope *scope)
513af6ab5fSopenharmony_ci    {
523af6ab5fSopenharmony_ci        scope_ = scope;
533af6ab5fSopenharmony_ci    }
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    void Run() override;
563af6ab5fSopenharmony_ci
573af6ab5fSopenharmony_ciprivate:
583af6ab5fSopenharmony_ci    CompilerContext *context_ {};
593af6ab5fSopenharmony_ci    binder::FunctionScope *scope_ {};
603af6ab5fSopenharmony_ci};
613af6ab5fSopenharmony_ci
623af6ab5fSopenharmony_ciclass CompileModuleRecordJob : public util::WorkerJob {
633af6ab5fSopenharmony_cipublic:
643af6ab5fSopenharmony_ci    explicit CompileModuleRecordJob(CompilerContext *context) : context_(context) {};
653af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileModuleRecordJob);
663af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileModuleRecordJob);
673af6ab5fSopenharmony_ci    ~CompileModuleRecordJob() override = default;
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci    void Run() override;
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ciprivate:
723af6ab5fSopenharmony_ci    CompilerContext *context_ {};
733af6ab5fSopenharmony_ci};
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_ciclass CompileFileJob : public util::WorkerJob {
763af6ab5fSopenharmony_cipublic:
773af6ab5fSopenharmony_ci    explicit CompileFileJob(es2panda::SourceFile *src, es2panda::CompilerOptions *options,
783af6ab5fSopenharmony_ci                            std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
793af6ab5fSopenharmony_ci                            std::unordered_set<std::string> &optimizationPendingProgs,
803af6ab5fSopenharmony_ci                            util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator)
813af6ab5fSopenharmony_ci        : src_(src), options_(options), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs),
823af6ab5fSopenharmony_ci          symbolTable_(symbolTable), allocator_(allocator) {};
833af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileFileJob);
843af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileFileJob);
853af6ab5fSopenharmony_ci    ~CompileFileJob() override = default;
863af6ab5fSopenharmony_ci
873af6ab5fSopenharmony_ci    void Run() override;
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ciprivate:
903af6ab5fSopenharmony_ci    friend class CompileAbcClassJob;
913af6ab5fSopenharmony_ci    bool RetrieveProgramFromCacheFiles(const std::string &buffer);
923af6ab5fSopenharmony_ci    void CompileProgram();
933af6ab5fSopenharmony_ci    void OptimizeAndCacheProgram(panda::pandasm::Program *prog);
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    static std::mutex globalMutex_;
963af6ab5fSopenharmony_ci    es2panda::SourceFile *src_;
973af6ab5fSopenharmony_ci    es2panda::CompilerOptions *options_;
983af6ab5fSopenharmony_ci    std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
993af6ab5fSopenharmony_ci    std::unordered_set<std::string> &optimizationPendingProgs_;
1003af6ab5fSopenharmony_ci    util::SymbolTable *symbolTable_;
1013af6ab5fSopenharmony_ci    panda::ArenaAllocator *allocator_;
1023af6ab5fSopenharmony_ci};
1033af6ab5fSopenharmony_ci
1043af6ab5fSopenharmony_ciclass CompileAbcClassJob : public util::WorkerJob {
1053af6ab5fSopenharmony_cipublic:
1063af6ab5fSopenharmony_ci    explicit CompileAbcClassJob(const uint32_t classId,
1073af6ab5fSopenharmony_ci                                const es2panda::CompilerOptions &options,
1083af6ab5fSopenharmony_ci                                abc2program::Abc2ProgramCompiler &compiler,
1093af6ab5fSopenharmony_ci                                std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
1103af6ab5fSopenharmony_ci                                panda::ArenaAllocator *allocator,
1113af6ab5fSopenharmony_ci                                std::string abcPkgName,
1123af6ab5fSopenharmony_ci                                bool pkgVersionUpdateRequiredInAbc = true)
1133af6ab5fSopenharmony_ci        : classId_(classId), options_(options), compiler_(compiler), progsInfo_(progsInfo),
1143af6ab5fSopenharmony_ci          allocator_(allocator), abcPkgName_(abcPkgName),
1153af6ab5fSopenharmony_ci          pkgVersionUpdateRequiredInAbc_(pkgVersionUpdateRequiredInAbc) {};
1163af6ab5fSopenharmony_ci
1173af6ab5fSopenharmony_ci    void SetHasUpdatedVersion(bool hasUpdatedVersion)
1183af6ab5fSopenharmony_ci    {
1193af6ab5fSopenharmony_ci        hasUpdatedVersion_ = hasUpdatedVersion;
1203af6ab5fSopenharmony_ci    }
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileAbcClassJob);
1233af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileAbcClassJob);
1243af6ab5fSopenharmony_ci    ~CompileAbcClassJob() override = default;
1253af6ab5fSopenharmony_ci
1263af6ab5fSopenharmony_ci    void Run() override;
1273af6ab5fSopenharmony_ci
1283af6ab5fSopenharmony_ciprivate:
1293af6ab5fSopenharmony_ci    void UpdatePackageVersion(panda::pandasm::Program *prog, const CompilerOptions &options);
1303af6ab5fSopenharmony_ci    void UpdateDynamicImportPackageVersion(panda::pandasm::Program *prog,
1313af6ab5fSopenharmony_ci        const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
1323af6ab5fSopenharmony_ci    void UpdateStaticImportPackageVersion(panda::pandasm::Program *prog,
1333af6ab5fSopenharmony_ci        const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
1343af6ab5fSopenharmony_ci
1353af6ab5fSopenharmony_ci    const uint32_t classId_;
1363af6ab5fSopenharmony_ci    const es2panda::CompilerOptions &options_;
1373af6ab5fSopenharmony_ci    abc2program::Abc2ProgramCompiler &compiler_;
1383af6ab5fSopenharmony_ci    std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
1393af6ab5fSopenharmony_ci    panda::ArenaAllocator *allocator_;
1403af6ab5fSopenharmony_ci    std::string abcPkgName_;
1413af6ab5fSopenharmony_ci    bool pkgVersionUpdateRequiredInAbc_;
1423af6ab5fSopenharmony_ci    bool hasUpdatedVersion_ {false};
1433af6ab5fSopenharmony_ci};
1443af6ab5fSopenharmony_ci
1453af6ab5fSopenharmony_ciclass PostAnalysisOptimizeFileJob : public util::WorkerJob {
1463af6ab5fSopenharmony_cipublic:
1473af6ab5fSopenharmony_ci    explicit PostAnalysisOptimizeFileJob(const std::string &fileName, pandasm::Program *program)
1483af6ab5fSopenharmony_ci        : fileName_(std::move(fileName)), program_(program) {}
1493af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(PostAnalysisOptimizeFileJob);
1503af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileJob);
1513af6ab5fSopenharmony_ci    ~PostAnalysisOptimizeFileJob() override = default;
1523af6ab5fSopenharmony_ci
1533af6ab5fSopenharmony_ci    void Run() override;
1543af6ab5fSopenharmony_ci
1553af6ab5fSopenharmony_ciprivate:
1563af6ab5fSopenharmony_ci    std::string fileName_;
1573af6ab5fSopenharmony_ci    pandasm::Program *program_;
1583af6ab5fSopenharmony_ci};
1593af6ab5fSopenharmony_ci
1603af6ab5fSopenharmony_ciclass CompileFuncQueue : public util::WorkerQueue {
1613af6ab5fSopenharmony_cipublic:
1623af6ab5fSopenharmony_ci    explicit CompileFuncQueue(size_t threadCount, CompilerContext *context)
1633af6ab5fSopenharmony_ci        : util::WorkerQueue(threadCount), context_(context) {}
1643af6ab5fSopenharmony_ci
1653af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileFuncQueue);
1663af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileFuncQueue);
1673af6ab5fSopenharmony_ci    ~CompileFuncQueue() override = default;
1683af6ab5fSopenharmony_ci
1693af6ab5fSopenharmony_ci    void Schedule() override;
1703af6ab5fSopenharmony_ci
1713af6ab5fSopenharmony_ciprivate:
1723af6ab5fSopenharmony_ci    CompilerContext *context_;
1733af6ab5fSopenharmony_ci};
1743af6ab5fSopenharmony_ci
1753af6ab5fSopenharmony_ciclass CompileFileQueue : public util::WorkerQueue {
1763af6ab5fSopenharmony_cipublic:
1773af6ab5fSopenharmony_ci    explicit CompileFileQueue(size_t threadCount, es2panda::CompilerOptions *options,
1783af6ab5fSopenharmony_ci                              std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
1793af6ab5fSopenharmony_ci                              std::unordered_set<std::string> &optimizationPendingProgs,
1803af6ab5fSopenharmony_ci                              util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator)
1813af6ab5fSopenharmony_ci        : util::WorkerQueue(threadCount), options_(options), progsInfo_(progsInfo),
1823af6ab5fSopenharmony_ci        optimizationPendingProgs_(optimizationPendingProgs), symbolTable_(symbolTable),
1833af6ab5fSopenharmony_ci        allocator_(allocator) {}
1843af6ab5fSopenharmony_ci
1853af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileFileQueue);
1863af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileFileQueue);
1873af6ab5fSopenharmony_ci    ~CompileFileQueue() override = default;
1883af6ab5fSopenharmony_ci
1893af6ab5fSopenharmony_ci    void Schedule() override;
1903af6ab5fSopenharmony_ci
1913af6ab5fSopenharmony_ciprivate:
1923af6ab5fSopenharmony_ci    es2panda::CompilerOptions *options_;
1933af6ab5fSopenharmony_ci    std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
1943af6ab5fSopenharmony_ci    std::unordered_set<std::string> &optimizationPendingProgs_;
1953af6ab5fSopenharmony_ci    util::SymbolTable *symbolTable_;
1963af6ab5fSopenharmony_ci    panda::ArenaAllocator *allocator_;
1973af6ab5fSopenharmony_ci};
1983af6ab5fSopenharmony_ci
1993af6ab5fSopenharmony_ciclass CompileAbcClassQueue : public util::WorkerQueue {
2003af6ab5fSopenharmony_cipublic:
2013af6ab5fSopenharmony_ci    explicit CompileAbcClassQueue(size_t threadCount,
2023af6ab5fSopenharmony_ci                                  const es2panda::CompilerOptions &options,
2033af6ab5fSopenharmony_ci                                  abc2program::Abc2ProgramCompiler &compiler,
2043af6ab5fSopenharmony_ci                                  std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
2053af6ab5fSopenharmony_ci                                  panda::ArenaAllocator *allocator,
2063af6ab5fSopenharmony_ci                                  es2panda::SourceFile *src)
2073af6ab5fSopenharmony_ci        : util::WorkerQueue(threadCount), options_(options), compiler_(compiler), progsInfo_(progsInfo),
2083af6ab5fSopenharmony_ci          allocator_(allocator), src_(src) {}
2093af6ab5fSopenharmony_ci
2103af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(CompileAbcClassQueue);
2113af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(CompileAbcClassQueue);
2123af6ab5fSopenharmony_ci    ~CompileAbcClassQueue() override = default;
2133af6ab5fSopenharmony_ci
2143af6ab5fSopenharmony_ci    void Schedule() override;
2153af6ab5fSopenharmony_ci
2163af6ab5fSopenharmony_ciprivate:
2173af6ab5fSopenharmony_ci    bool NeedUpdateVersion();
2183af6ab5fSopenharmony_ci
2193af6ab5fSopenharmony_ci    static std::mutex globalMutex_;
2203af6ab5fSopenharmony_ci    const es2panda::CompilerOptions &options_;
2213af6ab5fSopenharmony_ci    abc2program::Abc2ProgramCompiler &compiler_;
2223af6ab5fSopenharmony_ci    std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
2233af6ab5fSopenharmony_ci    panda::ArenaAllocator *allocator_;
2243af6ab5fSopenharmony_ci    es2panda::SourceFile *src_;
2253af6ab5fSopenharmony_ci};
2263af6ab5fSopenharmony_ci
2273af6ab5fSopenharmony_ciclass PostAnalysisOptimizeFileQueue : public util::WorkerQueue {
2283af6ab5fSopenharmony_cipublic:
2293af6ab5fSopenharmony_ci    explicit PostAnalysisOptimizeFileQueue(size_t threadCount,
2303af6ab5fSopenharmony_ci                                           std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
2313af6ab5fSopenharmony_ci                                           std::unordered_set<std::string> &optimizationPendingProgs)
2323af6ab5fSopenharmony_ci        : util::WorkerQueue(threadCount), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs) {}
2333af6ab5fSopenharmony_ci
2343af6ab5fSopenharmony_ci    NO_COPY_SEMANTIC(PostAnalysisOptimizeFileQueue);
2353af6ab5fSopenharmony_ci    NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileQueue);
2363af6ab5fSopenharmony_ci    ~PostAnalysisOptimizeFileQueue() override = default;
2373af6ab5fSopenharmony_ci
2383af6ab5fSopenharmony_ci    void Schedule() override;
2393af6ab5fSopenharmony_ci
2403af6ab5fSopenharmony_ciprivate:
2413af6ab5fSopenharmony_ci    std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
2423af6ab5fSopenharmony_ci    std::unordered_set<std::string> &optimizationPendingProgs_;
2433af6ab5fSopenharmony_ci};
2443af6ab5fSopenharmony_ci
2453af6ab5fSopenharmony_ci}  // namespace panda::es2panda::compiler
2463af6ab5fSopenharmony_ci
2473af6ab5fSopenharmony_ci#endif
248