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/bytecode_info_collector.h" 20 #include "ecmascript/compiler/compiler_log.h" 21 #include "ecmascript/compiler/file_generators.h" 22 #include "ecmascript/compiler/ir_module.h" 23 #include "ecmascript/compiler/pass_options.h" 24 #include "ecmascript/compiler/ir_module.h" 25 #include "ecmascript/ecma_vm.h" 26 #include "ecmascript/jspandafile/method_literal.h" 27 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 28 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h" 29 #include "ecmascript/ts_types/ts_manager.h" 30 31 namespace panda::ecmascript::kungfu { 32 class Bytecodes; 33 class LexEnvManager; 34 class CompilationConfig; 35 class PassData; 36 class PassContext { 37 public: PassContext(const std::string & triple,CompilerLog * log,BytecodeInfoCollector * collector,IRModule * aotModule,PGOProfilerDecoder * decoder)38 PassContext(const std::string &triple, CompilerLog *log, BytecodeInfoCollector* collector, IRModule *aotModule, 39 PGOProfilerDecoder *decoder) 40 : vm_(collector->GetVM()), 41 bcInfoCollector_(collector), 42 tsManager_(vm_->GetJSThread()->GetCurrentEcmaContext()->GetTSManager()), 43 bytecodes_(collector->GetByteCodes()), 44 lexEnvManager_(bcInfoCollector_->GetEnvManager()), 45 cmpCfg_(triple, &vm_->GetJSOptions()), 46 log_(log), 47 jsPandaFile_(collector->GetJSPandaFile()), 48 aotModule_(aotModule), 49 decoder_(decoder) 50 { 51 } 52 GetTSManager()53 TSManager* GetTSManager() const 54 { 55 return tsManager_; 56 } 57 GetPTManager()58 PGOTypeManager* GetPTManager() const 59 { 60 return vm_->GetJSThread()->GetCurrentEcmaContext()->GetPTManager(); 61 } 62 GetByteCodes()63 Bytecodes* GetByteCodes() 64 { 65 return bytecodes_; 66 } 67 GetLexEnvManager()68 LexEnvManager* GetLexEnvManager() const 69 { 70 return lexEnvManager_; 71 } 72 GetCompilerConfig()73 CompilationConfig* GetCompilerConfig() 74 { 75 return &cmpCfg_; 76 } 77 GetCompilerLog()78 CompilerLog* GetCompilerLog() const 79 { 80 return log_; 81 } 82 GetJSPandaFile()83 const JSPandaFile *GetJSPandaFile() const 84 { 85 return jsPandaFile_; 86 } 87 GetBytecodeInfoCollector()88 BytecodeInfoCollector* GetBytecodeInfoCollector() const 89 { 90 return bcInfoCollector_; 91 } 92 GetAOTModule()93 IRModule* GetAOTModule() const 94 { 95 return aotModule_; 96 } 97 FilterMethod(const MethodLiteral * methodLiteral,const MethodPcInfo & methodPCInfo)98 bool FilterMethod(const MethodLiteral *methodLiteral, const MethodPcInfo &methodPCInfo) const 99 { 100 return bcInfoCollector_->FilterMethod(methodLiteral, methodPCInfo); 101 } 102 IsSkippedMethod(uint32_t methodOffset)103 bool IsSkippedMethod(uint32_t methodOffset) const 104 { 105 return bcInfoCollector_->IsSkippedMethod(methodOffset); 106 } 107 GetBytecodeInfo()108 BCInfo& GetBytecodeInfo() 109 { 110 return bcInfoCollector_->GetBytecodeInfo(); 111 } 112 GetNativeAreaAllocator()113 NativeAreaAllocator *GetNativeAreaAllocator() const 114 { 115 return vm_->GetNativeAreaAllocator(); 116 } 117 GetEcmaVM()118 EcmaVM *GetEcmaVM() const 119 { 120 return vm_; 121 } 122 GetPfDecoder()123 PGOProfilerDecoder *GetPfDecoder() const 124 { 125 return decoder_; 126 } 127 128 private: 129 EcmaVM *vm_ {nullptr}; 130 BytecodeInfoCollector *bcInfoCollector_ {nullptr}; 131 TSManager *tsManager_ {nullptr}; 132 Bytecodes *bytecodes_ {nullptr}; 133 LexEnvManager *lexEnvManager_ {nullptr}; 134 CompilationConfig cmpCfg_; 135 CompilerLog *log_ {nullptr}; 136 const JSPandaFile *jsPandaFile_ {nullptr}; 137 IRModule *aotModule_ {nullptr}; 138 PGOProfilerDecoder *decoder_ {nullptr}; 139 }; 140 141 class PassManager { 142 public: PassManager(EcmaVM * vm,std::string & triple,size_t optLevel,size_t relocMode,CompilerLog * log,AotMethodLogList * logList,size_t maxAotMethodSize,size_t maxMethodsInModule,PGOProfilerDecoder & profilerDecoder,PassOptions * passOptions)143 explicit PassManager(EcmaVM* vm, std::string &triple, size_t optLevel, size_t relocMode, 144 CompilerLog *log, AotMethodLogList *logList, size_t maxAotMethodSize, size_t maxMethodsInModule, 145 PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions) 146 : vm_(vm), triple_(triple), optLevel_(optLevel), relocMode_(relocMode), log_(log), 147 logList_(logList), maxAotMethodSize_(maxAotMethodSize), maxMethodsInModule_(maxMethodsInModule), 148 profilerDecoder_(profilerDecoder), passOptions_(passOptions) { 149 enableJITLog_ = vm_->GetJSOptions().GetTraceJIT(); 150 }; 151 152 virtual ~PassManager() = default; 153 bool Compile(JSPandaFile *jsPandaFile, const std::string &fileName, AOTFileGenerator &generator); 154 155 protected: 156 bool IsReleasedPandaFile(const JSPandaFile *jsPandaFile) const; 157 158 EcmaVM *vm_ {nullptr}; 159 std::string triple_ {}; 160 size_t optLevel_ {3}; // 3 : default backend optimization level 161 size_t relocMode_ {2}; // 2 : default relocation mode-- PIC 162 CompilerLog *log_ {nullptr}; 163 AotMethodLogList *logList_ {nullptr}; 164 size_t maxAotMethodSize_ {0}; 165 size_t maxMethodsInModule_ {0}; 166 PGOProfilerDecoder &profilerDecoder_; 167 PassOptions *passOptions_ {nullptr}; 168 bool enableJITLog_ {false}; 169 }; 170 171 class JitPassManager : public PassManager { 172 public: JitPassManager(EcmaVM * vm,std::string & triple,size_t optLevel,size_t relocMode,CompilerLog * log,AotMethodLogList * logList,PGOProfilerDecoder & profilerDecoder,PassOptions * passOptions)173 JitPassManager(EcmaVM* vm, std::string &triple, size_t optLevel, size_t relocMode, 174 CompilerLog *log, AotMethodLogList *logList, 175 PGOProfilerDecoder &profilerDecoder, PassOptions *passOptions) 176 : PassManager(vm, triple, optLevel, relocMode, log, logList, 1, 1, profilerDecoder, passOptions) { }; 177 178 bool Compile(JSHandle<JSFunction> &jsFunction, AOTFileGenerator &gen); 179 bool RunCg(); 180 virtual ~JitPassManager(); 181 182 private: 183 BytecodeInfoCollector *collector_ {nullptr}; 184 LOptions *lOptions_ {nullptr}; 185 JitCompilationDriver *cmpDriver_ {nullptr}; 186 187 PassContext *ctx_ {nullptr}; 188 Circuit *circuit_ {nullptr}; 189 BytecodeCircuitBuilder *builder_ {nullptr}; 190 PassData *data_ {nullptr}; 191 }; 192 } 193 #endif // ECMASCRIPT_COMPILER_PASS_MANAGER_H 194