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 SetOhmurlBeenChanged(bool hasOhmurlBeenChanged)115 void SetOhmurlBeenChanged(bool hasOhmurlBeenChanged) 116 { 117 hasOhmurlBeenChanged_ = hasOhmurlBeenChanged; 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 UpdateImportOhmurl(panda::pandasm::Program *prog, const CompilerOptions &options); 128 void UpdateDynamicImport(panda::pandasm::Program *prog, 129 const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo); 130 void UpdateStaticImport(panda::pandasm::Program *prog, 131 const std::unordered_map<std::string, panda::es2panda::PkgInfo> &pkgContextInfo); 132 void UpdateBundleNameOfOhmurl(std::string &ohmurl); 133 134 const uint32_t classId_; 135 const es2panda::CompilerOptions &options_; 136 abc2program::Abc2ProgramCompiler &compiler_; 137 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_; 138 panda::ArenaAllocator *allocator_; 139 std::string abcPkgName_; 140 bool pkgVersionUpdateRequiredInAbc_; 141 bool hasOhmurlBeenChanged_ {false}; 142 }; 143 144 class PostAnalysisOptimizeFileJob : public util::WorkerJob { 145 public: PostAnalysisOptimizeFileJob(const std::string & fileName,pandasm::Program * program)146 explicit PostAnalysisOptimizeFileJob(const std::string &fileName, pandasm::Program *program) 147 : fileName_(std::move(fileName)), program_(program) {} 148 NO_COPY_SEMANTIC(PostAnalysisOptimizeFileJob); 149 NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileJob); 150 ~PostAnalysisOptimizeFileJob() override = default; 151 152 void Run() override; 153 154 private: 155 std::string fileName_; 156 pandasm::Program *program_; 157 }; 158 159 class CompileFuncQueue : public util::WorkerQueue { 160 public: CompileFuncQueue(size_t threadCount,CompilerContext * context)161 explicit CompileFuncQueue(size_t threadCount, CompilerContext *context) 162 : util::WorkerQueue(threadCount), context_(context) {} 163 164 NO_COPY_SEMANTIC(CompileFuncQueue); 165 NO_MOVE_SEMANTIC(CompileFuncQueue); 166 ~CompileFuncQueue() override = default; 167 168 void Schedule() override; 169 170 private: 171 CompilerContext *context_; 172 }; 173 174 class CompileFileQueue : public util::WorkerQueue { 175 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)176 explicit CompileFileQueue(size_t threadCount, es2panda::CompilerOptions *options, 177 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, 178 std::unordered_set<std::string> &optimizationPendingProgs, 179 util::SymbolTable *symbolTable, panda::ArenaAllocator *allocator) 180 : util::WorkerQueue(threadCount), options_(options), progsInfo_(progsInfo), 181 optimizationPendingProgs_(optimizationPendingProgs), symbolTable_(symbolTable), 182 allocator_(allocator) {} 183 184 NO_COPY_SEMANTIC(CompileFileQueue); 185 NO_MOVE_SEMANTIC(CompileFileQueue); 186 ~CompileFileQueue() override = default; 187 188 void Schedule() override; 189 190 private: 191 es2panda::CompilerOptions *options_; 192 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_; 193 std::unordered_set<std::string> &optimizationPendingProgs_; 194 util::SymbolTable *symbolTable_; 195 panda::ArenaAllocator *allocator_; 196 }; 197 198 class CompileAbcClassQueue : public util::WorkerQueue { 199 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)200 explicit CompileAbcClassQueue(size_t threadCount, 201 const es2panda::CompilerOptions &options, 202 abc2program::Abc2ProgramCompiler &compiler, 203 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, 204 panda::ArenaAllocator *allocator, 205 es2panda::SourceFile *src) 206 : util::WorkerQueue(threadCount), options_(options), compiler_(compiler), progsInfo_(progsInfo), 207 allocator_(allocator), src_(src) {} 208 209 NO_COPY_SEMANTIC(CompileAbcClassQueue); 210 NO_MOVE_SEMANTIC(CompileAbcClassQueue); 211 ~CompileAbcClassQueue() override = default; 212 213 void Schedule() override; 214 215 private: 216 bool NeedUpdateVersion(); 217 218 static std::mutex globalMutex_; 219 const es2panda::CompilerOptions &options_; 220 abc2program::Abc2ProgramCompiler &compiler_; 221 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_; 222 panda::ArenaAllocator *allocator_; 223 es2panda::SourceFile *src_; 224 }; 225 226 class PostAnalysisOptimizeFileQueue : public util::WorkerQueue { 227 public: PostAnalysisOptimizeFileQueue(size_t threadCount,std::map<std::string,panda::es2panda::util::ProgramCache * > & progsInfo,std::unordered_set<std::string> & optimizationPendingProgs)228 explicit PostAnalysisOptimizeFileQueue(size_t threadCount, 229 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, 230 std::unordered_set<std::string> &optimizationPendingProgs) 231 : util::WorkerQueue(threadCount), progsInfo_(progsInfo), optimizationPendingProgs_(optimizationPendingProgs) {} 232 233 NO_COPY_SEMANTIC(PostAnalysisOptimizeFileQueue); 234 NO_MOVE_SEMANTIC(PostAnalysisOptimizeFileQueue); 235 ~PostAnalysisOptimizeFileQueue() override = default; 236 237 void Schedule() override; 238 239 private: 240 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_; 241 std::unordered_set<std::string> &optimizationPendingProgs_; 242 }; 243 244 } // namespace panda::es2panda::compiler 245 246 #endif 247