1 /* 2 * Copyright (c) 2022 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_ARK_STACKMAP_BUILD_H 16 #define ECMASCRIPT_ARK_STACKMAP_BUILD_H 17 18 #include <iostream> 19 #include <memory> 20 #include <set> 21 #include <tuple> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "ecmascript/frames.h" 26 #include "ecmascript/stackmap/ark_stackmap.h" 27 28 namespace panda::ecmascript { 29 class BinaryBufferParser; 30 } 31 32 namespace panda::ecmascript::kungfu { 33 class BinaryBufferWriter { 34 public: BinaryBufferWriter(uint8_t * buffer,uint32_t length)35 BinaryBufferWriter(uint8_t *buffer, uint32_t length) : buffer_(buffer), length_(length) {} 36 ~BinaryBufferWriter() = default; 37 void WriteBuffer(const uint8_t *src, uint32_t count, bool flag = false); GetOffset()38 uint32_t GetOffset() const 39 { 40 return offset_; 41 } 42 private: 43 uint8_t *buffer_ {nullptr}; 44 uint32_t length_ {0}; 45 uint32_t offset_ {0}; 46 }; 47 48 class ArkStackMapBuilder { 49 public: 50 ArkStackMapBuilder() = default; 51 ~ArkStackMapBuilder() = default; 52 std::pair<std::shared_ptr<uint8_t>, uint32_t> PUBLIC_API Run(std::unique_ptr<uint8_t []> stackMapAddr, 53 uintptr_t hostCodeSectionAddr); 54 private: 55 template <class Vec> 56 void SortCallSite(std::vector<std::unordered_map<uintptr_t, Vec>> &infos, 57 std::vector<std::pair<uintptr_t, Vec>>& result); 58 59 void CalcCallsitePc(std::vector<std::pair<uintptr_t, DeoptInfoType>> &pc2Deopt, 60 std::vector<std::pair<uintptr_t, CallSiteInfo>> &pc2StackMap, std::vector<intptr_t> &callsitePcs); 61 62 void GenArkCallsiteAOTFileInfo(std::vector<Pc2CallSiteInfo> &pc2stackMaps, 63 std::vector<Pc2Deopt>& pc2DeoptVec, ARKCallsiteAOTFileInfo &result); 64 void SaveArkDeopt(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer); 65 void SaveArkStackMap(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer); 66 void SaveArkCallsiteAOTFileInfo(uint8_t *ptr, uint32_t length, const ARKCallsiteAOTFileInfo& info); 67 int FindLoc(std::vector<intptr_t> &CallsitePcs, intptr_t pc); 68 void GenARKDeopt(const DeoptInfoType& deopt, std::pair<uint32_t, std::vector<kungfu::ARKDeopt>> &sizeAndArkDeopt); 69 }; 70 } // namespace panda::ecmascript::kungfu 71 #endif // ECMASCRIPT_ARK_STACKMAP_BUILD_H 72