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 LLVMStackMapParser(LLVMStackMapInfo &stackMapInfo, bool enableLog = false) stackMapInfo(stackMapInfo)52 : stackMapInfo(stackMapInfo), 53 enableLog_(enableLog) 54 { 55 fun2RecordNum_.clear(); 56 dataInfo_ = nullptr; 57 funAddr_.clear(); 58 fun2FpDelta_.clear(); 59 } ~LLVMStackMapParser()60 ~LLVMStackMapParser() 61 { 62 fun2RecordNum_.clear(); 63 dataInfo_ = nullptr; 64 funAddr_.clear(); 65 fun2FpDelta_.clear(); 66 } 67 68 private: 69 static const size_t GC_PAIR_SIZE = 2; 70 void FilterCallSiteInfo(LLVMStackMapType::CallSiteInfo &info); 71 void CalcCallSite(); 72 struct LLVMStackMap llvmStackMap_; 73 // use reference here to avoid extra copy 74 LLVMStackMapInfo &stackMapInfo; 75 std::vector<std::pair<uintptr_t, uint64_t>> fun2RecordNum_; 76 std::unique_ptr<DataInfo> dataInfo_; 77 bool enableLog_ {false}; 78 std::set<uintptr_t> funAddr_; 79 std::vector<LLVMStackMapType::Func2FpDelta> fun2FpDelta_; 80 std::map<uint32_t, std::vector<LLVMStackMapType::Func2FpDelta>> module2fun2FpDelta_; 81 std::map<uint32_t, std::set<uintptr_t>> module2funAddr_; 82 }; 83 } // namespace panda::ecmascript::kungfu 84 #endif // ECMASCRIPT_LLVM_STACKMAP_PARSER_H 85