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_AOT_BULDER_AOT_FILE_BUILDER_H 17 #define COMPILER_AOT_AOT_BULDER_AOT_FILE_BUILDER_H 18 19 #include <string> 20 #include <vector> 21 #include "aot/compiled_method.h" 22 #include "aot/aot_file.h" 23 #include "elf_builder.h" 24 #include "utils/arch.h" 25 #include "utils/arena_containers.h" 26 #include "utils/bit_vector.h" 27 #include "optimizer/ir/runtime_interface.h" 28 #include "mem/gc/gc_types.h" 29 30 namespace panda { 31 class Class; 32 } // namespace panda 33 34 namespace panda::compiler { 35 36 template <Arch arch, bool is_jit_mode> 37 class ElfBuilder; 38 39 class AotBuilder : public ElfWriter { 40 public: SetGcType(uint32_t gc_type)41 void SetGcType(uint32_t gc_type) 42 { 43 gc_type_ = gc_type; 44 } GetGcType()45 uint32_t GetGcType() const 46 { 47 return gc_type_; 48 } 49 GetIntfInlineCacheIndex()50 uint64_t *GetIntfInlineCacheIndex() 51 { 52 return &intf_inline_cache_index_; 53 } 54 55 int Write(const std::string &cmdline, const std::string &file_name); 56 57 void StartFile(const std::string &name, uint32_t checksum); 58 void EndFile(); 59 GetGotPlt()60 auto *GetGotPlt() 61 { 62 return &got_plt_; 63 } 64 GetGotVirtIndexes()65 auto *GetGotVirtIndexes() 66 { 67 return &got_virt_indexes_; 68 } 69 GetGotClass()70 auto *GetGotClass() 71 { 72 return &got_class_; 73 } 74 GetGotString()75 auto *GetGotString() 76 { 77 return &got_string_; 78 } 79 GetGotIntfInlineCache()80 auto *GetGotIntfInlineCache() 81 { 82 return &got_intf_inline_cache_; 83 } 84 SetBootAot(bool boot_aot)85 void SetBootAot(bool boot_aot) 86 { 87 boot_aot_ = boot_aot; 88 } 89 SetWithCha(bool with_cha)90 void SetWithCha(bool with_cha) 91 { 92 with_cha_ = with_cha; 93 } 94 SetGenerateSymbols(bool generate_symbols)95 void SetGenerateSymbols(bool generate_symbols) 96 { 97 generate_symbols_ = generate_symbols; 98 } 99 100 void AddClassHashTable(const panda_file::File &panda_file); 101 InsertEntityPairHeader(uint32_t class_hash,uint32_t class_id)102 void InsertEntityPairHeader(uint32_t class_hash, uint32_t class_id) 103 { 104 entity_pair_headers_.emplace_back(); 105 auto &entity_pair = entity_pair_headers_.back(); 106 entity_pair.descriptor_hash = class_hash; 107 entity_pair.entity_id_offset = class_id; 108 } 109 GetEntityPairHeaders()110 auto *GetEntityPairHeaders() const 111 { 112 return &entity_pair_headers_; 113 } 114 InsertClassHashTableSize(uint32_t size)115 void InsertClassHashTableSize(uint32_t size) 116 { 117 class_hash_tables_size_.emplace_back(size); 118 } 119 GetClassHashTableSize()120 auto *GetClassHashTableSize() const 121 { 122 return &class_hash_tables_size_; 123 } 124 125 private: 126 template <Arch arch> 127 int WriteImpl(const std::string &cmdline, const std::string &file_name); 128 129 template <Arch arch> 130 void GenerateSymbols(ElfBuilder<arch> &builder); 131 132 template <Arch arch> 133 void EmitPlt(Span<typename ArchTraits<arch>::WordType> ptr_view, size_t got_data_size); 134 135 void FillHeader(const std::string &cmdline, const std::string &file_name); 136 137 void ResolveConflictClassHashTable(const panda_file::File &panda_file, 138 std::vector<unsigned int> conflict_entity_table, size_t conflict_num, 139 std::vector<panda_file::EntityPairHeader> &entity_pairs); 140 141 private: 142 std::string file_name_; 143 compiler::AotHeader aot_header_ {}; 144 uint32_t gc_type_ {static_cast<uint32_t>(mem::GCType::INVALID_GC)}; 145 uint64_t intf_inline_cache_index_ {0}; 146 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> got_plt_; 147 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> got_virt_indexes_; 148 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> got_class_; 149 std::map<std::pair<const panda_file::File *, uint32_t>, int32_t> got_string_; 150 std::map<std::pair<const panda_file::File *, uint64_t>, int32_t> got_intf_inline_cache_; 151 bool boot_aot_ {false}; 152 bool with_cha_ {true}; 153 bool generate_symbols_ {false}; 154 155 std::vector<panda_file::EntityPairHeader> entity_pair_headers_; 156 std::vector<uint32_t> class_hash_tables_size_; 157 friend class CodeDataProvider; 158 friend class JitCodeDataProvider; 159 }; 160 161 } // namespace panda::compiler 162 163 #endif // COMPILER_AOT_AOT_BULDER_AOT_FILE_BUILDER_H 164