1 /* 2 * Copyright (c) 2021-2025 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 PANDA_AOT_DATA_H 17 #define PANDA_AOT_DATA_H 18 19 #include <map> 20 #include "runtime_interface.h" 21 22 namespace ark::panda_file { 23 class File; 24 } // namespace ark::panda_file 25 26 namespace ark::compiler { 27 class Graph; 28 29 class SharedSlowPathData { 30 public: SharedSlowPathData()31 SharedSlowPathData() 32 { 33 ASSERT(std::all_of(entrypointsOffsets_.begin(), entrypointsOffsets_.end(), [](auto v) { return v == 0; })); 34 } SetSharedSlowPathOffset(RuntimeInterface::EntrypointId id,uintptr_t offset)35 void SetSharedSlowPathOffset(RuntimeInterface::EntrypointId id, uintptr_t offset) 36 { 37 entrypointsOffsets_[static_cast<size_t>(id)] = offset; 38 } GetSharedSlowPathOffset(RuntimeInterface::EntrypointId id)39 uintptr_t GetSharedSlowPathOffset(RuntimeInterface::EntrypointId id) 40 { 41 return entrypointsOffsets_[static_cast<size_t>(id)]; 42 } 43 44 private: 45 static constexpr size_t SIZE = static_cast<size_t>(RuntimeInterface::EntrypointId::COUNT); 46 std::array<uintptr_t, SIZE> entrypointsOffsets_ {}; 47 }; 48 49 class AotData { 50 static constexpr uintptr_t INVALID_ADDRESS = std::numeric_limits<uintptr_t>::max(); 51 using AddressType = uintptr_t; 52 using File = const panda_file::File; 53 using MethodPtr = RuntimeInterface::MethodPtr; 54 using ClassPtr = RuntimeInterface::ClassPtr; 55 56 public: 57 struct AotDataArgs { 58 File *pfile; 59 Graph *graph; 60 SharedSlowPathData *slowPathData; 61 AddressType codeAddr; 62 uint64_t *intfInlineCacheIndex; 63 std::array<std::map<std::pair<const File *, uint32_t>, int32_t> *, 4U> mapArgs32; 64 std::array<std::map<std::pair<const File *, uint64_t>, int32_t> *, 2U> mapArgs64; 65 }; 66 AotData(const AotDataArgs & args)67 explicit AotData(const AotDataArgs &args) 68 : pfile_(args.pfile), 69 graph_(args.graph), 70 slowPathData_(args.slowPathData), 71 codeAddress_(args.codeAddr), 72 intfInlineCacheIndex_(args.intfInlineCacheIndex), 73 gotPlt_(args.mapArgs32[0U]), 74 gotVirtIndexes_(args.mapArgs32[1U]), 75 gotClass_(args.mapArgs32[2U]), 76 gotString_(args.mapArgs32[3U]), 77 gotIntfInlineCache_(args.mapArgs64[0U]), 78 gotCommon_(args.mapArgs64[1U]) 79 { 80 } 81 82 intptr_t GetEpTableOffset() const; 83 intptr_t GetEntrypointOffset(uint64_t pc, int32_t slotId) const; 84 intptr_t GetSharedSlowPathOffset(RuntimeInterface::EntrypointId id, uintptr_t pc) const; 85 void SetSharedSlowPathOffset(RuntimeInterface::EntrypointId id, uintptr_t pc); 86 intptr_t GetPltSlotOffset(uint64_t pc, uint32_t methodId); 87 intptr_t GetVirtIndexSlotOffset(uint64_t pc, uint32_t methodId); 88 intptr_t GetClassSlotOffset(uint64_t pc, uint32_t klassId, bool init); 89 intptr_t GetCommonSlotOffset(uint64_t pc, uint32_t id); 90 intptr_t GetStringSlotOffset(uint64_t pc, uint32_t stringId); 91 uint64_t GetInfInlineCacheSlotOffset(uint64_t pc, uint64_t cacheIdx); 92 93 int32_t GetClassSlotId(uint32_t klassId); 94 int32_t GetStringSlotId(uint32_t stringId); 95 int32_t GetPltSlotId(uint32_t methodId); 96 int32_t GetIntfInlineCacheSlotId(uint64_t cacheIdx); 97 GetCodeOffset()98 AddressType GetCodeOffset() const 99 { 100 return codeAddress_; 101 } GetUseCha()102 bool GetUseCha() const 103 { 104 return useCha_; 105 } SetUseCha(bool useCha)106 void SetUseCha(bool useCha) 107 { 108 useCha_ = useCha; 109 } 110 SetIntfInlineCacheIndex(uint64_t intfInlineCacheIndex)111 void SetIntfInlineCacheIndex(uint64_t intfInlineCacheIndex) 112 { 113 *intfInlineCacheIndex_ = intfInlineCacheIndex; 114 } 115 GetIntfInlineCacheIndex()116 uint64_t GetIntfInlineCacheIndex() const 117 { 118 return *intfInlineCacheIndex_; 119 } 120 HasProfileData()121 bool HasProfileData() const 122 { 123 return hasProfileData_; 124 } 125 SetHasProfileData(bool hasProfileData)126 void SetHasProfileData(bool hasProfileData) 127 { 128 hasProfileData_ = hasProfileData; 129 } 130 131 private: 132 inline int32_t GetSlotId() const; 133 134 const File *pfile_; 135 Graph *graph_ {nullptr}; 136 SharedSlowPathData *slowPathData_; 137 AddressType codeAddress_ {INVALID_ADDRESS}; 138 uint64_t *intfInlineCacheIndex_; 139 std::map<std::pair<const File *, uint32_t>, int32_t> *gotPlt_; 140 std::map<std::pair<const File *, uint32_t>, int32_t> *gotVirtIndexes_; 141 std::map<std::pair<const File *, uint32_t>, int32_t> *gotClass_; 142 std::map<std::pair<const File *, uint32_t>, int32_t> *gotString_; 143 std::map<std::pair<const File *, uint64_t>, int32_t> *gotIntfInlineCache_; 144 std::map<std::pair<const File *, uint64_t>, int32_t> *gotCommon_; 145 bool useCha_ {false}; 146 bool hasProfileData_ {false}; 147 }; 148 } // namespace ark::compiler 149 150 #endif // PANDA_AOT_DATA_H 151