1 /* 2 * Copyright (c) 2021 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_LLVM_STACKMAP_PARSER_H 17 #define ECMASCRIPT_LLVM_STACKMAP_PARSER_H 18 19 #include <iostream> 20 #include <memory> 21 #include <tuple> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "ecmascript/common.h" 26 #include "ecmascript/ecma_macros.h" 27 #include "ecmascript/interpreter/interpreter-inl.h" 28 #include "ecmascript/stackmap/ark_stackmap.h" 29 #include "ecmascript/stackmap/ark_stackmap_builder.h" 30 #include "ecmascript/stackmap/llvm_stackmap_type.h" 31 32 namespace panda::ecmascript::kungfu { 33 class LLVMStackMapParser { 34 public: 35 bool PUBLIC_API CalculateStackMap(std::unique_ptr<uint8_t []> stackMapAddr); 36 bool PUBLIC_API CalculateStackMap(std::unique_ptr<uint8_t []> stackMapAddr, 37 uintptr_t hostCodeSectionAddr, 38 uintptr_t hostCodeSectionOffset); Print()39 void PUBLIC_API Print() const 40 { 41 if (IsLogEnabled()) { 42 llvmStackMap_.Print(); 43 } 44 } 45 IsLogEnabled()46 bool IsLogEnabled() const 47 { 48 return enableLog_; 49 } 50 51 void PUBLIC_API CalculateFuncFpDelta(LLVMStackMapType::Func2FpDelta info, uint32_t moduleIndex); 52 53 LLVMStackMapParser(std::vector<LLVMStackMapType::Pc2CallSiteInfo> &pc2CallSiteInfoVec, 54 std::vector<LLVMStackMapType::Pc2Deopt> &pc2DeoptVec, 55 bool enableLog = false) pc2CallSiteInfoVec_(pc2CallSiteInfoVec)56 : pc2CallSiteInfoVec_(pc2CallSiteInfoVec), 57 pc2DeoptVec_(pc2DeoptVec), 58 enableLog_(enableLog) 59 { 60 fun2RecordNum_.clear(); 61 dataInfo_ = nullptr; 62 funAddr_.clear(); 63 fun2FpDelta_.clear(); 64 } ~LLVMStackMapParser()65 ~LLVMStackMapParser() 66 { 67 fun2RecordNum_.clear(); 68 dataInfo_ = nullptr; 69 funAddr_.clear(); 70 fun2FpDelta_.clear(); 71 } 72 73 private: 74 void CalcCallSite(); 75 struct LLVMStackMap llvmStackMap_; 76 // use reference here to avoid extra copy 77 std::vector<LLVMStackMapType::Pc2CallSiteInfo> &pc2CallSiteInfoVec_; 78 std::vector<LLVMStackMapType::Pc2Deopt> &pc2DeoptVec_; 79 std::vector<std::pair<uintptr_t, uint64_t>> fun2RecordNum_; 80 std::unique_ptr<DataInfo> dataInfo_; 81 bool enableLog_ {false}; 82 std::set<uintptr_t> funAddr_; 83 std::vector<LLVMStackMapType::Func2FpDelta> fun2FpDelta_; 84 std::map<uint32_t, std::vector<LLVMStackMapType::Func2FpDelta>> module2fun2FpDelta_; 85 std::map<uint32_t, std::set<uintptr_t>> module2funAddr_; 86 }; 87 } // namespace panda::ecmascript::kungfu 88 #endif // ECMASCRIPT_LLVM_STACKMAP_PARSER_H 89