• 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     void CompileProgram();
92     void OptimizeAndCacheProgram(panda::pandasm::Program *prog);
93     bool RetrieveProgramFromCacheFiles(const std::string &buffer, bool isAbcFile = false);
94     void CompileAbcFileJobInParallel(es2panda::Compiler &compiler);
95     void InsertAbcCachePrograms(uint32_t hashCode,
96                                 std::map<std::string, panda::es2panda::util::ProgramCache *> &abcProgramsInfo);
97 
98     static std::mutex globalMutex_;
99     es2panda::SourceFile *src_;
100     es2panda::CompilerOptions *options_;
101     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
102     std::unordered_set<std::string> &optimizationPendingProgs_;
103     util::SymbolTable *symbolTable_;
104     panda::ArenaAllocator *allocator_;
105 };
106 
107 class CompileAbcClassJob : public util::WorkerJob {
108 public:
109     explicit CompileAbcClassJob(es2panda::SourceFile *src, const uint32_t classId,
110                                 const es2panda::CompilerOptions &options,
111                                 abc2program::Abc2ProgramCompiler &compiler,
112                                 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
113                                 panda::ArenaAllocator *allocator,
114                                 std::string abcPkgName,
115                                 bool pkgVersionUpdateRequiredInAbc = true)
src_(src)116         : src_(src), classId_(classId), options_(options), compiler_(compiler), progsInfo_(progsInfo),
117           allocator_(allocator), abcPkgName_(abcPkgName),
118           pkgVersionUpdateRequiredInAbc_(pkgVersionUpdateRequiredInAbc) {};
119 
SetOhmurlBeenChanged(bool hasOhmurlBeenChanged)120     void SetOhmurlBeenChanged(bool hasOhmurlBeenChanged)
121     {
122         hasOhmurlBeenChanged_ = hasOhmurlBeenChanged;
123     }
124     void UpdatePkgNameOfImportOhmurl(panda::pandasm::Program *prog, const panda::es2panda::CompilerOptions &options);
125 
126     NO_COPY_SEMANTIC(CompileAbcClassJob);
127     NO_MOVE_SEMANTIC(CompileAbcClassJob);
128     ~CompileAbcClassJob() override = default;
129 
130     void Run() override;
131 
132 private:
133     void UpdateImportOhmurl(panda::pandasm::Program *prog, const CompilerOptions &options);
134     void UpdateDynamicImport(panda::pandasm::Program *prog,
135         const std::map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
136     void UpdateStaticImport(panda::pandasm::Program *prog,
137         const std::map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo);
138     void UpdateBundleNameOfOhmurl(std::string &ohmurl);
139 
140     es2panda::SourceFile *src_;
141     const uint32_t classId_;
142     const es2panda::CompilerOptions &options_;
143     abc2program::Abc2ProgramCompiler &compiler_;
144     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
145     panda::ArenaAllocator *allocator_;
146     std::string abcPkgName_;
147     bool pkgVersionUpdateRequiredInAbc_;
148     bool hasOhmurlBeenChanged_ {false};
149 };
150 
151 class PostAnalysisOptimizeFileJob : public util::WorkerJob {
152 public:
PostAnalysisOptimizeFileJob(const std::string & fileName,pandasm::Program * program)153     explicit PostAnalysisOptimizeFileJob(const std::string &fileName, pandasm::Program *program)
154         : fileName_(std::move(fileName)), program_(program) {}
155     NO_COPY_SEMANTIC(PostAnalysisOptimizeFileJob);
156     NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileJob);
157     ~PostAnalysisOptimizeFileJob() override = default;
158 
159     void Run() override;
160 
161 private:
162     std::string fileName_;
163     pandasm::Program *program_;
164 };
165 
166 class CompileFuncQueue : public util::WorkerQueue {
167 public:
CompileFuncQueue(size_t threadCount,CompilerContext * context)168     explicit CompileFuncQueue(size_t threadCount, CompilerContext *context)
169         : util::WorkerQueue(threadCount), context_(context) {}
170 
171     NO_COPY_SEMANTIC(CompileFuncQueue);
172     NO_MOVE_SEMANTIC(CompileFuncQueue);
173     ~CompileFuncQueue() override = default;
174 
175     void Schedule() override;
176 
177 private:
178     CompilerContext *context_;
179 };
180 
181 class CompileFileQueue : public util::WorkerQueue {
182 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)183     explicit CompileFileQueue(size_t threadCount, es2panda::CompilerOptions *options,
184                               std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
185                               std::unordered_set<std::string> &optimizationPendingProgs,
186                               util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator)
187         : util::WorkerQueue(threadCount), options_(options), progsInfo_(progsInfo),
188         optimizationPendingProgs_(optimizationPendingProgs), symbolTable_(symbolTable),
189         allocator_(allocator) {}
190 
191     NO_COPY_SEMANTIC(CompileFileQueue);
192     NO_MOVE_SEMANTIC(CompileFileQueue);
193     ~CompileFileQueue() override = default;
194 
195     void Schedule() override;
196 
197 private:
198     es2panda::CompilerOptions *options_;
199     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
200     std::unordered_set<std::string> &optimizationPendingProgs_;
201     util::SymbolTable *symbolTable_;
202     panda::ArenaAllocator *allocator_;
203 };
204 
205 class CompileAbcClassQueue : public util::WorkerQueue {
206 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)207     explicit CompileAbcClassQueue(size_t threadCount,
208                                   const es2panda::CompilerOptions &options,
209                                   abc2program::Abc2ProgramCompiler &compiler,
210                                   std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
211                                   panda::ArenaAllocator *allocator,
212                                   es2panda::SourceFile *src)
213         : util::WorkerQueue(threadCount), options_(options), compiler_(compiler), progsInfo_(progsInfo),
214           allocator_(allocator), src_(src) {}
215 
216     NO_COPY_SEMANTIC(CompileAbcClassQueue);
217     NO_MOVE_SEMANTIC(CompileAbcClassQueue);
218     ~CompileAbcClassQueue() override = default;
219 
220     void Schedule() override;
221 
222 private:
223     bool NeedUpdateVersion();
224 
225     static std::mutex globalMutex_;
226     const es2panda::CompilerOptions &options_;
227     abc2program::Abc2ProgramCompiler &compiler_;
228     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
229     panda::ArenaAllocator *allocator_;
230     es2panda::SourceFile *src_;
231 };
232 
233 class PostAnalysisOptimizeFileQueue : public util::WorkerQueue {
234 public:
PostAnalysisOptimizeFileQueue(size_t threadCount,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,std::unordered_set<std::string> & optimizationPendingProgs)235     explicit PostAnalysisOptimizeFileQueue(size_t threadCount,
236                                            std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
237                                            std::unordered_set<std::string> &optimizationPendingProgs)
238         : util::WorkerQueue(threadCount), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs) {}
239 
240     NO_COPY_SEMANTIC(PostAnalysisOptimizeFileQueue);
241     NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileQueue);
242     ~PostAnalysisOptimizeFileQueue() override = default;
243 
244     void Schedule() override;
245 
246 private:
247     std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
248     std::unordered_set<std::string> &optimizationPendingProgs_;
249 };
250 
251 }  // namespace panda::es2panda::compiler
252 
253 #endif
254