1 /** 2 * Copyright (c) 2021-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 16 #ifndef COMPILER_AOT_COMPILED_METHOD_H 17 #define COMPILER_AOT_COMPILED_METHOD_H 18 19 #include "utils/arch.h" 20 #include "utils/span.h" 21 #include "compiler/code_info/code_info.h" 22 #include "compiler/optimizer/code_generator/callconv.h" 23 24 #include <cstdint> 25 #include <vector> 26 27 namespace panda { 28 class Method; 29 } // namespace panda 30 31 namespace panda::compiler { 32 class CompiledMethod { 33 public: CompiledMethod(Arch arch,Method * method)34 CompiledMethod(Arch arch, Method *method) : arch_(arch), method_(method) {} 35 NO_COPY_OPERATOR(CompiledMethod); 36 DEFAULT_COPY_CTOR(CompiledMethod) 37 DEFAULT_MOVE_SEMANTIC(CompiledMethod); 38 ~CompiledMethod() = default; 39 SetCode(Span<const uint8_t> data)40 void SetCode(Span<const uint8_t> data) 41 { 42 code_.reserve(data.size()); 43 std::copy(data.begin(), data.end(), std::back_inserter(code_)); 44 } 45 SetCodeInfo(Span<const uint8_t> data)46 void SetCodeInfo(Span<const uint8_t> data) 47 { 48 code_info_.reserve(data.size()); 49 std::copy(data.begin(), data.end(), std::back_inserter(code_info_)); 50 } 51 GetMethod()52 Method *GetMethod() 53 { 54 return method_; 55 } 56 GetMethod()57 const Method *GetMethod() const 58 { 59 return method_; 60 } 61 GetCode()62 Span<const uint8_t> GetCode() const 63 { 64 return Span(code_); 65 } 66 GetCodeInfo()67 Span<const uint8_t> GetCodeInfo() const 68 { 69 return Span(code_info_); 70 } 71 GetOverallSize()72 size_t GetOverallSize() const 73 { 74 return RoundUp(CodePrefix::STRUCT_SIZE, GetCodeAlignment(arch_)) + RoundUp(code_.size(), CodeInfo::ALIGNMENT) + 75 RoundUp(code_info_.size(), CodeInfo::SIZE_ALIGNMENT); 76 } 77 78 #ifdef PANDA_COMPILER_CFI GetCfiInfo()79 CfiInfo &GetCfiInfo() 80 { 81 return cfi_info_; 82 } 83 GetCfiInfo()84 const CfiInfo &GetCfiInfo() const 85 { 86 return cfi_info_; 87 } 88 SetCfiInfo(const CfiInfo & cfi_info)89 void SetCfiInfo(const CfiInfo &cfi_info) 90 { 91 cfi_info_ = cfi_info; 92 } 93 #endif 94 95 private: 96 Arch arch_ {RUNTIME_ARCH}; 97 Method *method_ {nullptr}; 98 std::vector<uint8_t> code_; 99 std::vector<uint8_t> code_info_; 100 #ifdef PANDA_COMPILER_CFI 101 CfiInfo cfi_info_; 102 #endif 103 }; 104 } // namespace panda::compiler 105 106 #endif // COMPILER_AOT_COMPILED_METHOD_H 107