• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_COMPILER_CORE_COMPILEQUEUE_H
17 #define ES2PANDA_COMPILER_CORE_COMPILEQUEUE_H
18 
19 #include <aot/options.h>
20 #include <macros.h>
21 #include <os/thread.h>
22 #include <util/symbolTable.h>
23 #include <util/workerQueue.h>
24 
25 #include <condition_variable>
26 #include <mutex>
27 
28 #include <abc2program/abc2program_compiler.h>
29 
30 namespace panda::es2panda::binder {
31 class FunctionScope;
32 }  // namespace panda::es2panda::binder
33 
34 namespace panda::es2panda::compiler {
35 
36 class CompilerContext;
37 
38 class CompileFunctionJob : public util::WorkerJob {
39 public:
CompileFunctionJob(CompilerContext * context)40     explicit CompileFunctionJob(CompilerContext *context) : context_(context) {};
41     NO_COPY_SEMANTIC(CompileFunctionJob);
42     NO_MOVE_SEMANTIC(CompileFunctionJob);
43     ~CompileFunctionJob() override = default;
44 
Scope()45     binder::FunctionScope *Scope() const
46     {
47         return scope_;
48     }
49 
SetFunctionScope(binder::FunctionScope * scope)50     void SetFunctionScope(binder::FunctionScope *scope)
51     {
52         scope_ = scope;
53     }
54 
55     void Run() override;
56 
57 private:
58     CompilerContext *context_ {};
59     binder::FunctionScope *scope_ {};
60 };
61 
62 class CompileModuleRecordJob : public util::WorkerJob {
63 public:
CompileModuleRecordJob(CompilerContext * context)64     explicit CompileModuleRecordJob(CompilerContext *context) : context_(context) {};
65     NO_COPY_SEMANTIC(CompileModuleRecordJob);
66     NO_MOVE_SEMANTIC(CompileModuleRecordJob);
67     ~CompileModuleRecordJob() override = default;
68 
69     void Run() override;
70 
71 private:
72     CompilerContext *context_ {};
73 };
74 
75 class CompileFileJob : public util::WorkerJob {
76 public:
CompileFileJob(es2panda::SourceFile * src,es2panda::CompilerOptions * options,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,std::unordered_set<std::string> & optimizationPendingProgs,util::SymbolTable * symbolTable,panda::ArenaAllocator * allocator)77     explicit CompileFileJob(es2panda::SourceFile *src, es2panda::CompilerOptions *options,
78                             std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
79                             std::unordered_set<std::string> &optimizationPendingProgs,
80                             util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator)
81         : src_(src), options_(options), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs),
82           symbolTable_(symbolTable), allocator_(allocator) {};
83     NO_COPY_SEMANTIC(CompileFileJob);
84     NO_MOVE_SEMANTIC(CompileFileJob);
85     ~CompileFileJob() override = default;
86 
87     void Run() override;
88 
89 private:
90     friend class CompileAbcClassJob;
91     bool RetrieveProgramFromCacheFiles(const std::string &buffer);
92 
93     static std::mutex globalMutex_;
94     es2panda::SourceFile *src_;
95     es2panda::CompilerOptions *options_;
96     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
97     std::unordered_set<std::string> &optimizationPendingProgs_;
98     util::SymbolTable *symbolTable_;
99     panda::ArenaAllocator *allocator_;
100 };
101 
102 class CompileAbcClassJob : public util::WorkerJob {
103 public:
104     explicit CompileAbcClassJob(const uint32_t classId,
105                                 const es2panda::CompilerOptions &options,
106                                 abc2program::Abc2ProgramCompiler &compiler,
107                                 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
108                                 panda::ArenaAllocator *allocator,
109                                 std::string abcPkgName,
110                                 bool pkgVersionUpdateRequiredInAbc = true)
classId_(classId)111         : classId_(classId), options_(options), compiler_(compiler), progsInfo_(progsInfo),
112           allocator_(allocator), abcPkgName_(abcPkgName),
113           pkgVersionUpdateRequiredInAbc_(pkgVersionUpdateRequiredInAbc) {};
114 
SetHasUpdatedVersion(bool hasUpdatedVersion)115     void SetHasUpdatedVersion(bool hasUpdatedVersion)
116     {
117         hasUpdatedVersion_ = hasUpdatedVersion;
118     }
119 
120     NO_COPY_SEMANTIC(CompileAbcClassJob);
121     NO_MOVE_SEMANTIC(CompileAbcClassJob);
122     ~CompileAbcClassJob() override = default;
123 
124     void Run() override;
125 
126 private:
127     void UpdatePackageVersion(panda::pandasm::Program *prog, const CompilerOptions &options);
128     void UpdateDynamicImportPackageVersion(panda::pandasm::Program *prog,
129         const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
130     void UpdateStaticImportPackageVersion(panda::pandasm::Program *prog,
131         const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
132 
133     const uint32_t classId_;
134     const es2panda::CompilerOptions &options_;
135     abc2program::Abc2ProgramCompiler &compiler_;
136     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
137     panda::ArenaAllocator *allocator_;
138     std::string abcPkgName_;
139     bool pkgVersionUpdateRequiredInAbc_;
140     bool hasUpdatedVersion_ {false};
141 };
142 
143 class PostAnalysisOptimizeFileJob : public util::WorkerJob {
144 public:
PostAnalysisOptimizeFileJob(const std::string & fileName,pandasm::Program * program)145     explicit PostAnalysisOptimizeFileJob(const std::string &fileName, pandasm::Program *program)
146         : fileName_(std::move(fileName)), program_(program) {}
147     NO_COPY_SEMANTIC(PostAnalysisOptimizeFileJob);
148     NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileJob);
149     ~PostAnalysisOptimizeFileJob() override = default;
150 
151     void Run() override;
152 
153 private:
154     std::string fileName_;
155     pandasm::Program *program_;
156 };
157 
158 class CompileFuncQueue : public util::WorkerQueue {
159 public:
CompileFuncQueue(size_t threadCount,CompilerContext * context)160     explicit CompileFuncQueue(size_t threadCount, CompilerContext *context)
161         : util::WorkerQueue(threadCount), context_(context) {}
162 
163     NO_COPY_SEMANTIC(CompileFuncQueue);
164     NO_MOVE_SEMANTIC(CompileFuncQueue);
165     ~CompileFuncQueue() override = default;
166 
167     void Schedule() override;
168 
169 private:
170     CompilerContext *context_;
171 };
172 
173 class CompileFileQueue : public util::WorkerQueue {
174 public:
CompileFileQueue(size_t threadCount,es2panda::CompilerOptions * options,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,std::unordered_set<std::string> & optimizationPendingProgs,util::SymbolTable * symbolTable,panda::ArenaAllocator * allocator)175     explicit CompileFileQueue(size_t threadCount, es2panda::CompilerOptions *options,
176                               std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
177                               std::unordered_set<std::string> &optimizationPendingProgs,
178                               util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator)
179         : util::WorkerQueue(threadCount), options_(options), progsInfo_(progsInfo),
180         optimizationPendingProgs_(optimizationPendingProgs), symbolTable_(symbolTable),
181         allocator_(allocator) {}
182 
183     NO_COPY_SEMANTIC(CompileFileQueue);
184     NO_MOVE_SEMANTIC(CompileFileQueue);
185     ~CompileFileQueue() override = default;
186 
187     void Schedule() override;
188 
189 private:
190     es2panda::CompilerOptions *options_;
191     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
192     std::unordered_set<std::string> &optimizationPendingProgs_;
193     util::SymbolTable *symbolTable_;
194     panda::ArenaAllocator *allocator_;
195 };
196 
197 class CompileAbcClassQueue : public util::WorkerQueue {
198 public:
CompileAbcClassQueue(size_t threadCount,const es2panda::CompilerOptions & options,abc2program::Abc2ProgramCompiler & compiler,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,panda::ArenaAllocator * allocator,es2panda::SourceFile * src)199     explicit CompileAbcClassQueue(size_t threadCount,
200                                   const es2panda::CompilerOptions &options,
201                                   abc2program::Abc2ProgramCompiler &compiler,
202                                   std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
203                                   panda::ArenaAllocator *allocator,
204                                   es2panda::SourceFile *src)
205         : util::WorkerQueue(threadCount), options_(options), compiler_(compiler), progsInfo_(progsInfo),
206           allocator_(allocator), src_(src) {}
207 
208     NO_COPY_SEMANTIC(CompileAbcClassQueue);
209     NO_MOVE_SEMANTIC(CompileAbcClassQueue);
210     ~CompileAbcClassQueue() override = default;
211 
212     void Schedule() override;
213 
214 private:
215     bool NeedUpdateVersion();
216 
217     static std::mutex globalMutex_;
218     const es2panda::CompilerOptions &options_;
219     abc2program::Abc2ProgramCompiler &compiler_;
220     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
221     panda::ArenaAllocator *allocator_;
222     es2panda::SourceFile *src_;
223 };
224 
225 class PostAnalysisOptimizeFileQueue : public util::WorkerQueue {
226 public:
PostAnalysisOptimizeFileQueue(size_t threadCount,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,std::unordered_set<std::string> & optimizationPendingProgs)227     explicit PostAnalysisOptimizeFileQueue(size_t threadCount,
228                                            std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
229                                            std::unordered_set<std::string> &optimizationPendingProgs)
230         : util::WorkerQueue(threadCount), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs) {}
231 
232     NO_COPY_SEMANTIC(PostAnalysisOptimizeFileQueue);
233     NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileQueue);
234     ~PostAnalysisOptimizeFileQueue() override = default;
235 
236     void Schedule() override;
237 
238 private:
239     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
240     std::unordered_set<std::string> &optimizationPendingProgs_;
241 };
242 
243 }  // namespace panda::es2panda::compiler
244 
245 #endif
246