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 #include <iomanip> 25 26 #include "ecmascript/frames.h" 27 #include "ecmascript/stackmap/ark_stackmap.h" 28 #include "ecmascript/stackmap/llvm_stackmap_type.h" 29 30 #include "libpandabase/utils/leb128.h" 31 namespace panda::ecmascript { 32 class BinaryBufferParser; 33 } 34 35 namespace panda::ecmascript::kungfu { 36 class BinaryBufferWriter { 37 public: BinaryBufferWriter(uint8_t * buffer,uint32_t length)38 BinaryBufferWriter(uint8_t *buffer, uint32_t length) : buffer_(buffer), length_(length) {} 39 ~BinaryBufferWriter() = default; 40 void WriteBuffer(const uint8_t *src, uint32_t count, bool flag = false); GetOffset()41 uint32_t GetOffset() const 42 { 43 return offset_; 44 } AlignOffset()45 void AlignOffset() 46 { 47 offset_ = AlignUp(offset_, LLVMStackMapType::STACKMAP_ALIGN_BYTES); 48 } 49 private: 50 uint8_t *buffer_ {nullptr}; 51 uint32_t length_ {0}; 52 uint32_t offset_ {0}; 53 }; 54 55 struct StackMapDumper { 56 uint32_t callsiteHeadSize {0}; 57 uint32_t arkStackMapSize {0}; 58 uint32_t deoptSize {0}; 59 uint32_t callsiteNum {0}; 60 uint32_t stackmapNum {0}; 61 uint32_t deoptNum {0}; 62 }; 63 64 class ArkStackMapBuilder { 65 public: 66 ArkStackMapBuilder() = default; 67 ~ArkStackMapBuilder() = default; 68 std::pair<std::shared_ptr<uint8_t>, uint32_t> PUBLIC_API Run(std::unique_ptr<uint8_t []> stackMapAddr, 69 uintptr_t hostCodeSectionAddr, Triple triple); 70 std::pair<std::shared_ptr<uint8_t>, uint32_t> GenerateArkStackMap( 71 std::vector<LLVMStackMapType::Pc2CallSiteInfo> &pc2CallsiteInfoVec, 72 std::vector<LLVMStackMapType::Pc2Deopt> &pc2DeoptVec, 73 Triple triple); 74 void Collect(std::unique_ptr<uint8_t []> stackMapAddr, 75 uintptr_t hostCodeSectionAddr, 76 uintptr_t hostCodeSectionOffset, 77 std::vector<LLVMStackMapType::Pc2CallSiteInfo> &pc2CallsiteInfoVec, 78 std::vector<LLVMStackMapType::Pc2Deopt> &pc2DeoptVec); SetTraceStackMap(bool flag)79 void SetTraceStackMap(bool flag) 80 { 81 traceStackMap_ = flag; 82 } 83 84 private: 85 static constexpr int DECIMAL_LENS = 2; 86 StackMapDumper dumper_; 87 88 template <class Vec> 89 void SortCallSite(std::vector<std::unordered_map<uintptr_t, Vec>> &infos, 90 std::vector<std::pair<uintptr_t, Vec>>& result); 91 void CalcCallsitePc(std::vector<std::pair<uintptr_t, LLVMStackMapType::DeoptInfoType>> &pc2Deopt, 92 std::vector<std::pair<uintptr_t, LLVMStackMapType::CallSiteInfo>> &pc2StackMap, 93 std::vector<intptr_t> &callsitePcs); 94 void GenArkCallsiteAOTFileInfo(std::vector<LLVMStackMapType::Pc2CallSiteInfo> &pc2StackMaps, 95 std::vector<LLVMStackMapType::Pc2Deopt>& pc2DeoptVec, ARKCallsiteAOTFileInfo &result, Triple triple); 96 void SaveArkDeopt(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer, Triple triple); 97 void SaveArkStackMap(const ARKCallsiteAOTFileInfo& info, BinaryBufferWriter& writer, Triple triple); 98 void SaveArkCallsiteAOTFileInfo(uint8_t *ptr, uint32_t length, const ARKCallsiteAOTFileInfo& info, Triple triple); 99 int FindLoc(std::vector<intptr_t> &CallsitePcs, intptr_t pc); 100 void GenARKDeopt(const LLVMStackMapType::DeoptInfoType& deopt, 101 std::pair<uint32_t, std::vector<ARKDeopt>> &sizeAndArkDeopt, Triple triple); 102 void Dump(const StackMapDumper& dumpInfo) const; 103 bool traceStackMap_{false}; 104 }; 105 } // namespace panda::ecmascript::kungfu 106 #endif // ECMASCRIPT_ARK_STACKMAP_BUILD_H 107