1 /* 2 * Copyright (c) 2021 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 ECMASCRIPT_COMPILER_PASS_MANAGER_H 17 #define ECMASCRIPT_COMPILER_PASS_MANAGER_H 18 19 #include "ecmascript/compiler/compiler_log.h" 20 #include "ecmascript/compiler/file_generators.h" 21 #include "ecmascript/ecma_vm.h" 22 #include "ecmascript/pgo_profiler/pgo_profiler_loader.h" 23 24 namespace panda::ecmascript::kungfu { 25 class Bytecodes; 26 class LexEnvManager; 27 class CompilationConfig; 28 class BytecodeInfoCollector; 29 30 class PassInfo { 31 public: PassInfo(TSManager * tsManager,Bytecodes * bytecodes,LexEnvManager * lexEnvManager,CompilationConfig * cmpCfg,CompilerLog * log,const JSPandaFile * jsPandaFile,BytecodeInfoCollector * bcInfoCollector,LLVMModule * aotModule)32 explicit PassInfo(TSManager *tsManager, Bytecodes *bytecodes, LexEnvManager *lexEnvManager, 33 CompilationConfig *cmpCfg, CompilerLog *log, const JSPandaFile *jsPandaFile, 34 BytecodeInfoCollector* bcInfoCollector, LLVMModule *aotModule) 35 : tsManager_(tsManager), bytecodes_(bytecodes), lexEnvManager_(lexEnvManager), cmpCfg_(cmpCfg), 36 log_(log), jsPandaFile_(jsPandaFile), bcInfoCollector_(bcInfoCollector), aotModule_(aotModule) 37 { 38 } 39 GetTSManager()40 TSManager* GetTSManager() const 41 { 42 return tsManager_; 43 } 44 GetByteCodes()45 Bytecodes* GetByteCodes() const 46 { 47 return bytecodes_; 48 } 49 GetLexEnvManager()50 LexEnvManager* GetLexEnvManager() const 51 { 52 return lexEnvManager_; 53 } 54 GetCompilerConfig()55 CompilationConfig* GetCompilerConfig() const 56 { 57 return cmpCfg_; 58 } 59 GetCompilerLog()60 CompilerLog* GetCompilerLog() const 61 { 62 return log_; 63 } 64 GetJSPandaFile()65 const JSPandaFile* GetJSPandaFile() const 66 { 67 return jsPandaFile_; 68 } 69 GetBytecodeInfoCollector()70 BytecodeInfoCollector* GetBytecodeInfoCollector() const 71 { 72 return bcInfoCollector_; 73 } 74 GetAOTModule()75 LLVMModule* GetAOTModule() const 76 { 77 return aotModule_; 78 } 79 IsSkippedMethod(uint32_t methodOffset)80 bool IsSkippedMethod(uint32_t methodOffset) const 81 { 82 return bcInfoCollector_->IsSkippedMethod(methodOffset); 83 } 84 GetBytecodeInfo()85 BCInfo& GetBytecodeInfo() 86 { 87 return bcInfoCollector_->GetBytecodeInfo(); 88 } 89 90 private: 91 TSManager *tsManager_ {nullptr}; 92 Bytecodes *bytecodes_ {nullptr}; 93 LexEnvManager *lexEnvManager_ {nullptr}; 94 CompilationConfig *cmpCfg_ {nullptr}; 95 CompilerLog *log_ {nullptr}; 96 const JSPandaFile *jsPandaFile_ {nullptr}; 97 BytecodeInfoCollector *bcInfoCollector_ {nullptr}; 98 LLVMModule *aotModule_ {nullptr}; 99 }; 100 101 class PassManager { 102 public: PassManager(EcmaVM * vm,std::string entry,std::string & triple,size_t optLevel,size_t relocMode,CompilerLog * log,AotMethodLogList * logList,size_t maxAotMethodSize,bool enableTypeLowering,const std::string & profIn,uint32_t hotnessThreshold)103 PassManager(EcmaVM* vm, std::string entry, std::string &triple, size_t optLevel, size_t relocMode, 104 CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, bool enableTypeLowering, 105 const std::string &profIn, uint32_t hotnessThreshold) 106 : vm_(vm), entry_(entry), triple_(triple), optLevel_(optLevel), relocMode_(relocMode), log_(log), 107 logList_(logList), maxAotMethodSize_(maxAotMethodSize), enableTypeLowering_(enableTypeLowering), 108 enableTypeInfer_(enableTypeLowering || vm_->GetTSManager()->AssertTypes()), 109 profilerLoader_(profIn, hotnessThreshold) {}; 110 PassManager() = default; 111 ~PassManager() = default; 112 113 bool Compile(const std::string &fileName, AOTFileGenerator &generator); 114 115 private: 116 JSPandaFile *CreateAndVerifyJSPandaFile(const CString &fileName); 117 bool IsReleasedPandaFile(const JSPandaFile *jsPandaFile) const; 118 void ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName); 119 EnableTypeLowering()120 bool EnableTypeLowering() const 121 { 122 return enableTypeLowering_; 123 } 124 EnableTypeInfer()125 bool EnableTypeInfer() const 126 { 127 return enableTypeInfer_; 128 } 129 130 EcmaVM *vm_ {nullptr}; 131 std::string entry_ {}; 132 std::string triple_ {}; 133 size_t optLevel_ {3}; // 3 : default backend optimization level 134 size_t relocMode_ {2}; // 2 : default relocation mode-- PIC 135 CompilerLog *log_ {nullptr}; 136 AotMethodLogList *logList_ {nullptr}; 137 size_t maxAotMethodSize_ {0}; 138 bool enableTypeLowering_ {true}; 139 bool enableTypeInfer_ {true}; 140 PGOProfilerLoader profilerLoader_; 141 }; 142 } 143 #endif // ECMASCRIPT_COMPILER_PASS_MANAGER_H 144