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 16 #ifndef ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_PARSER_H 17 #define ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_PARSER_H 18 19 #include "ecmascript/compiler/bytecode_info_collector.h" 20 #include "ecmascript/compiler/pgo_bc_info.h" 21 #include "ecmascript/compiler/pgo_type/pgo_hclass_generator.h" 22 #include "ecmascript/compiler/pgo_type/pgo_type_manager.h" 23 #include "ecmascript/compiler/pgo_type/pgo_type_recorder.h" 24 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h" 25 26 namespace panda::ecmascript::kungfu { 27 class BaseParser { 28 public: BaseParser(PGOTypeManager * ptManager,PGOBCInfo::Type type)29 BaseParser(PGOTypeManager *ptManager, PGOBCInfo::Type type) : ptManager_(ptManager), type_(type) {} 30 virtual ~BaseParser() = default; 31 32 void Parse(const BytecodeInfoCollector &collector, const PGOTypeRecorder &typeRecorder, 33 const PGOHClassGenerator &generator, uint32_t methodOffset); 34 35 protected: 36 virtual bool RecordTypeInfo(const PGODefineOpType &defType, const PGOTypeLocation &loc) = 0; GenerateHClass(const PGOHClassGenerator & generator,const PGOTypeLocation & loc)37 virtual void GenerateHClass( 38 [[maybe_unused]] const PGOHClassGenerator &generator, [[maybe_unused]] const PGOTypeLocation &loc) {}; 39 40 PGOTypeManager *ptManager_ { nullptr }; 41 PGOBCInfo::Type type_; 42 }; 43 44 class ClassParser final : public BaseParser { 45 public: ClassParser(PGOTypeManager * ptManager)46 ClassParser(PGOTypeManager *ptManager) : BaseParser(ptManager, PGOBCInfo::Type::CLASS) {} 47 48 private: 49 virtual bool RecordTypeInfo(const PGODefineOpType &defType, const PGOTypeLocation &loc) override; 50 virtual void GenerateHClass(const PGOHClassGenerator &generator, const PGOTypeLocation &loc) override; 51 }; 52 53 class ArrayParser : public BaseParser { 54 public: ArrayParser(PGOTypeManager * ptManager,PGOBCInfo::Type type)55 ArrayParser(PGOTypeManager *ptManager, PGOBCInfo::Type type) : BaseParser(ptManager, type) {} 56 57 private: 58 virtual bool RecordTypeInfo(const PGODefineOpType &defType, const PGOTypeLocation &loc) override; 59 }; 60 61 class EmptyArrayParser final : public ArrayParser { 62 public: EmptyArrayParser(PGOTypeManager * ptManager)63 EmptyArrayParser(PGOTypeManager *ptManager) : ArrayParser(ptManager, PGOBCInfo::Type::EMPTY_ARRAY) {} 64 }; 65 66 class ArrayLiteralParser final : public ArrayParser { 67 public: ArrayLiteralParser(PGOTypeManager * ptManager)68 ArrayLiteralParser(PGOTypeManager *ptManager) : ArrayParser(ptManager, PGOBCInfo::Type::ARRAY_LITERAL) {} 69 }; 70 71 class ObjectLiteralParser final : public BaseParser { 72 public: ObjectLiteralParser(PGOTypeManager * ptManager)73 ObjectLiteralParser(PGOTypeManager *ptManager) : BaseParser(ptManager, PGOBCInfo::Type::OBJ_LITERAL) {} 74 75 private: 76 virtual bool RecordTypeInfo(const PGODefineOpType &defType, const PGOTypeLocation &loc) override; 77 virtual void GenerateHClass(const PGOHClassGenerator &generator, const PGOTypeLocation &loc) override; 78 }; 79 80 class FunctionParser final : public BaseParser { 81 public: FunctionParser(PGOTypeManager * ptManager)82 FunctionParser(PGOTypeManager *ptManager) : BaseParser(ptManager, PGOBCInfo::Type::FUNCTION) {} 83 84 private: 85 virtual bool RecordTypeInfo(const PGODefineOpType &defType, const PGOTypeLocation &loc) override; 86 virtual void GenerateHClass(const PGOHClassGenerator &generator, const PGOTypeLocation &loc) override; 87 }; 88 89 class PGOTypeParser { 90 public: 91 explicit PUBLIC_API PGOTypeParser(const PGOProfilerDecoder &decoder, PGOTypeManager *ptManager); 92 ~PGOTypeParser() = default; 93 94 void PUBLIC_API CreatePGOType(BytecodeInfoCollector &collector); 95 96 private: 97 void GenerateHClassForNapiType(ProfileType rootType, const PGOHClassGenerator &generator); 98 void GenerateHClassForClassType(ProfileType rootType, ProfileType protoPt, const PGOHClassGenerator &generator); 99 void GenerateHClassForPrototype(ProfileType rootType, const PGOHClassGenerator &generator); 100 bool SkipGenerateHClass(PGOTypeRecorder typeRecorder, ProfileType rootType, 101 bool isCache, PGOHClassTreeDesc *desc); 102 103 const PGOProfilerDecoder &decoder_; 104 PGOTypeManager *ptManager_; 105 CVector<std::unique_ptr<BaseParser>> parsers_; 106 }; 107 } // namespace panda::ecmascript::kungfu 108 #endif // ECMASCRIPT_COMPILER_PGO_TYPE_PGO_TYPE_PARSER_H 109