1 /* 2 * Copyright (c) 2021-2024 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_AOT_BUILDER_LLVM_AOT_BUILDER_H 17 #define COMPILER_AOT_AOT_BUILDER_LLVM_AOT_BUILDER_H 18 19 #include "aot_builder.h" 20 21 namespace ark::compiler { 22 23 class LLVMAotBuilder : public AotBuilder { 24 public: 25 std::unordered_map<std::string, size_t> GetSectionsAddresses(const std::string &cmdline, 26 const std::string &fileName); 27 GetMethods()28 const std::vector<CompiledMethod> &GetMethods() 29 { 30 return methods_; 31 } 32 GetMethodHeaders()33 const std::vector<compiler::MethodHeader> &GetMethodHeaders() 34 { 35 return methodHeaders_; 36 } 37 38 /** 39 * Put method header for a given method. 40 * The method will be patched later (see AdjustMethod below). 41 */ AddMethodHeader(Method * method,size_t methodIndex)42 void AddMethodHeader(Method *method, size_t methodIndex) 43 { 44 auto &methodHeader = methodHeaders_.emplace_back(); 45 methods_.emplace_back(arch_, method, methodIndex); 46 methodHeader.methodId = method->GetFileId().GetOffset(); 47 methodHeader.codeOffset = UNPATCHED_CODE_OFFSET; 48 methodHeader.codeSize = UNPATCHED_CODE_SIZE; 49 currentBitmap_->SetBit(methodIndex); 50 } 51 52 /// Adjust a method's header according to the supplied method AdjustMethodHeader(const CompiledMethod & method,size_t index)53 void AdjustMethodHeader(const CompiledMethod &method, size_t index) 54 { 55 MethodHeader &methodHeader = methodHeaders_[index]; 56 methodHeader.codeOffset = currentCodeSize_; 57 methodHeader.codeSize = method.GetOverallSize(); 58 currentCodeSize_ += methodHeader.codeSize; 59 currentCodeSize_ = RoundUp(currentCodeSize_, GetCodeAlignment(arch_)); 60 } 61 62 /// Adjust a method stored in this aot builder according to the supplied method AdjustMethod(const CompiledMethod & method,size_t index)63 void AdjustMethod(const CompiledMethod &method, size_t index) 64 { 65 ASSERT(methods_.at(index).GetMethod() == method.GetMethod()); 66 ASSERT(methodHeaders_.at(index).codeSize == method.GetOverallSize()); 67 methods_.at(index).SetCode(method.GetCode()); 68 methods_.at(index).SetCodeInfo(method.GetCodeInfo()); 69 } 70 71 private: 72 static constexpr auto UNPATCHED_CODE_OFFSET = std::numeric_limits<decltype(MethodHeader::codeOffset)>::max(); 73 static constexpr auto UNPATCHED_CODE_SIZE = std::numeric_limits<decltype(MethodHeader::codeSize)>::max(); 74 75 template <Arch ARCH> 76 std::unordered_map<std::string, size_t> GetSectionsAddressesImpl(const std::string &cmdline, 77 const std::string &fileName); 78 }; 79 } // namespace ark::compiler 80 #endif // COMPILER_AOT_AOT_BUILDER_LLVM_AOT_BUILDER_H 81