1 /* 2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 16 #define ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 17 18 #include "ecmascript/compiler/pass_manager.h" 19 #include "ecmascript/ecma_vm.h" 20 #include "macros.h" 21 22 namespace panda::ecmascript::kungfu { 23 class OhosPkgArgs; 24 using PGOProfilerDecoder = pgo::PGOProfilerDecoder; 25 26 struct AbcFileInfo { AbcFileInfoAbcFileInfo27 explicit AbcFileInfo(std::string extendedFilePath, std::shared_ptr<JSPandaFile> jsPandaFile) 28 : extendedFilePath_(extendedFilePath), jsPandaFile_(jsPandaFile) {} 29 ~AbcFileInfo() = default; 30 31 std::string extendedFilePath_; 32 std::shared_ptr<JSPandaFile> jsPandaFile_; 33 }; 34 35 struct CompilationOptions { 36 explicit CompilationOptions(EcmaVM *vm, JSRuntimeOptions &runtimeOptions); 37 38 std::string triple_; 39 std::string outputFileName_; 40 size_t optLevel_; 41 size_t relocMode_; 42 std::string logOption_; 43 std::string logMethodsList_; 44 bool compilerLogTime_; 45 size_t maxAotMethodSize_; 46 size_t maxMethodsInModule_; 47 uint32_t hotnessThreshold_; 48 std::string profilerIn_; 49 bool needMerge_ {false}; 50 bool isEnableArrayBoundsCheckElimination_; 51 bool isEnableTypeLowering_; 52 bool isEnableEarlyElimination_; 53 bool isEnableLaterElimination_; 54 bool isEnableValueNumbering_; 55 bool isEnableOptInlining_; 56 bool isEnableOptString_; 57 bool isEnableTypeInfer_; 58 bool isEnableOptPGOType_; 59 bool isEnableOptTrackField_; 60 bool isEnableOptLoopPeeling_; 61 bool isEnableOptLoopInvariantCodeMotion_; 62 bool isEnableCollectLiteralInfo_; 63 bool isEnableOptConstantFolding_; 64 bool isEnableLexenvSpecialization_; 65 bool isEnableNativeInline_; 66 bool isEnablePGOHCRLowering_; 67 bool isEnableLoweringBuiltin_; 68 bool isEnableOptBranchProfiling_; 69 }; 70 71 class AotCompilerPreprocessor { 72 public: AotCompilerPreprocessor(EcmaVM * vm,JSRuntimeOptions & runtimeOptions,std::map<std::string,std::shared_ptr<OhosPkgArgs>> & pkgsArgs,PGOProfilerDecoder & profilerDecoder,arg_list_t & pandaFileNames)73 AotCompilerPreprocessor(EcmaVM *vm, JSRuntimeOptions &runtimeOptions, 74 std::map<std::string, std::shared_ptr<OhosPkgArgs>> &pkgsArgs, 75 PGOProfilerDecoder &profilerDecoder, arg_list_t &pandaFileNames) 76 : vm_(vm), 77 runtimeOptions_(runtimeOptions), 78 pkgsArgs_(pkgsArgs), 79 profilerDecoder_(profilerDecoder), 80 pandaFileNames_(pandaFileNames) {}; 81 82 ~AotCompilerPreprocessor() = default; 83 84 bool HandleTargetCompilerMode(CompilationOptions &cOptions); 85 86 bool HandlePandaFileNames(const int argc, const char **argv); 87 88 void AOTInitialize(); 89 90 void SetShouldCollectLiteralInfo(CompilationOptions &cOptions, const CompilerLog *log); 91 92 bool GenerateAbcFileInfos(); 93 94 void GenerateGlobalTypes(const CompilationOptions &cOptions); 95 96 void GeneratePGOTypes(const CompilationOptions &cOptions); 97 98 void SnapshotInitialize(); 99 100 std::string GetMainPkgArgsAppSignature() const; 101 GetCompilerResult()102 bool GetCompilerResult() 103 { 104 // The size of fileInfos is not equal to pandaFiles size, set compiler result to false 105 return fileInfos_.size() == pandaFileNames_.size(); 106 } 107 GetAbcFileInfo()108 const CVector<AbcFileInfo>& GetAbcFileInfo() const 109 { 110 return fileInfos_; 111 } 112 GetMainPkgArgs()113 std::shared_ptr<OhosPkgArgs> GetMainPkgArgs() const 114 { 115 if (pkgsArgs_.empty()) { 116 return nullptr; 117 } 118 return pkgsArgs_.at(mainPkgName_); 119 } 120 GetPkgsArgs()121 const std::map<std::string, std::shared_ptr<OhosPkgArgs>> &GetPkgsArgs() const 122 { 123 return pkgsArgs_; 124 } 125 GetHelper()126 static std::string GetHelper() 127 { 128 std::string str; 129 str.append(COMPILER_HELP_HEAD_MSG); 130 str.append(HELP_OPTION_MSG); 131 return str; 132 } 133 134 private: 135 NO_COPY_SEMANTIC(AotCompilerPreprocessor); 136 NO_MOVE_SEMANTIC(AotCompilerPreprocessor); 137 void HandleTargetModeInfo(CompilationOptions &cOptions); 138 139 std::shared_ptr<JSPandaFile> CreateAndVerifyJSPandaFile(const std::string &fileName); 140 141 void ResolveModule(const JSPandaFile *jsPandaFile, const std::string &fileName); 142 143 EcmaVM *vm_; 144 JSRuntimeOptions &runtimeOptions_; 145 std::map<std::string, std::shared_ptr<OhosPkgArgs>> &pkgsArgs_; 146 std::string mainPkgName_; 147 PGOProfilerDecoder &profilerDecoder_; 148 arg_list_t &pandaFileNames_; 149 CVector<AbcFileInfo> fileInfos_; 150 friend class OhosPkgArgs; 151 }; 152 } // namespace panda::ecmascript::kungfu 153 #endif // ECMASCRIPT_COMPILER_AOT_COMPILER_PREPROCESSOR_H 154