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 ASSEMBLER_ASSEMBLY_EMITTER_H 17 #define ASSEMBLER_ASSEMBLY_EMITTER_H 18 19 #include <memory> 20 #include <type_traits> 21 #include <vector> 22 #include <unordered_map> 23 24 #include "assembly-ins.h" 25 #include "assembly-literals.h" 26 #include "assembly-program.h" 27 #include "assembly-type.h" 28 #include "assembly-function.h" 29 #include "bytecode_emitter.h" 30 #include "file_item_container.h" 31 #include "pgo.h" 32 33 namespace panda::pandasm { 34 class AsmEmitter { 35 public: 36 struct PandaFileToPandaAsmMaps { 37 std::unordered_map<uint32_t, std::string> methods; 38 std::unordered_map<uint32_t, std::string> fields; 39 std::unordered_map<uint32_t, std::string> classes; 40 std::unordered_map<uint32_t, std::string> strings; 41 std::unordered_map<uint32_t, std::string> literalarrays; 42 }; 43 44 struct AsmEntityCollections { 45 std::unordered_map<std::string, panda_file::BaseMethodItem *> method_items; 46 std::unordered_map<std::string, panda_file::BaseFieldItem *> field_items; 47 std::unordered_map<std::string, panda_file::BaseClassItem *> class_items; 48 std::unordered_map<std::string, panda_file::StringItem *> string_items; 49 std::unordered_map<std::string, panda_file::LiteralArrayItem *> literalarray_items; 50 }; 51 52 static bool Emit(panda_file::ItemContainer *items, const Program &program, PandaFileToPandaAsmMaps *maps = nullptr, 53 bool emit_debug_info = true, panda::panda_file::pgo::ProfileOptimizer *profile_opt = nullptr); 54 55 static bool Emit(panda_file::Writer *writer, const Program &program, std::map<std::string, size_t> *stat = nullptr, 56 PandaFileToPandaAsmMaps *maps = nullptr, bool debug_info = true, 57 panda::panda_file::pgo::ProfileOptimizer *profile_opt = nullptr); 58 59 static bool Emit(const std::string &filename, const Program &program, std::map<std::string, size_t> *stat = nullptr, 60 PandaFileToPandaAsmMaps *maps = nullptr, bool debug_info = true, 61 panda::panda_file::pgo::ProfileOptimizer *profile_opt = nullptr); 62 63 static bool EmitPrograms(const std::string &filename, const std::vector<Program *> &progs, bool emit_debug_info); 64 65 static std::unique_ptr<const panda_file::File> Emit(const Program &program, 66 PandaFileToPandaAsmMaps *maps = nullptr); 67 GetLastError()68 static std::string GetLastError() 69 { 70 return last_error; 71 } 72 73 private: 74 static void MakeStringItems(panda_file::ItemContainer *items, const Program &program, 75 AsmEntityCollections &entities); 76 static void MakeLiteralItems(panda_file::ItemContainer *items, const Program &program, 77 AsmEmitter::AsmEntityCollections &entities); 78 static void MakeArrayTypeItems(panda_file::ItemContainer *items, const Program &program, 79 AsmEntityCollections &entities); 80 static bool HandleRecordAsForeign( 81 panda_file::ItemContainer *items, const Program &program, AsmEntityCollections &entities, 82 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 83 const std::string &name, const Record &rec); 84 static bool HandleBaseRecord(panda_file::ItemContainer *items, const Program &program, const std::string &name, 85 const Record &base_rec, panda_file::ClassItem *record); 86 static bool HandleInterfaces(panda_file::ItemContainer *items, const Program &program, const std::string &name, 87 const Record &rec, panda_file::ClassItem *record); 88 static bool HandleFields( 89 panda_file::ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities, 90 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 91 const std::string &name, const Record &rec, panda_file::ClassItem *record); 92 static bool HandleRecord( 93 panda_file::ItemContainer *items, const Program &program, AsmEntityCollections &entities, 94 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 95 const std::string &name, const Record &rec); 96 static bool MakeRecordItems( 97 panda_file::ItemContainer *items, const Program &program, AsmEntityCollections &entities, 98 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types); 99 static panda_file::StringItem *GetMethodName(panda_file::ItemContainer *items, const Function &func, 100 const std::string &name); 101 static bool HandleAreaForInner(panda_file::ItemContainer *items, const Program &program, 102 panda_file::ClassItem **area, panda_file::ForeignClassItem **foreign_area, 103 const std::string &name, const std::string &record_owner_name); 104 static bool HandleRecordOnwer(panda_file::ItemContainer *items, const Program &program, 105 panda_file::ClassItem **area, panda_file::ForeignClassItem **foreign_area, 106 const std::string &name, const std::string &record_owner_name); 107 static bool HandleFunctionParams( 108 panda_file::ItemContainer *items, const Program &program, size_t idx, const std::string &name, 109 const Function &func, 110 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 111 std::vector<panda_file::MethodParamItem> ¶ms); 112 static bool HandleFunctionLocalVariables(panda_file::ItemContainer *items, const Function &func, 113 const std::string &name); 114 static bool CreateMethodItem(panda_file::ItemContainer *items, AsmEmitter::AsmEntityCollections &entities, 115 const Function &func, panda_file::TypeItem *type_item, panda_file::ClassItem *area, 116 panda_file::ForeignClassItem *foreign_area, uint32_t access_flags, 117 panda_file::StringItem *method_name, const std::string &mangled_name, 118 const std::string &name, std::vector<panda_file::MethodParamItem> ¶ms); 119 static bool MakeFunctionItems( 120 panda_file::ItemContainer *items, const Program &program, AsmEntityCollections &entities, 121 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 122 bool emit_debug_info); 123 static bool MakeRecordAnnotations(panda_file::ItemContainer *items, const Program &program, 124 const AsmEntityCollections &entities); 125 static void SetCodeAndDebugInfo(panda_file::ItemContainer *items, panda_file::MethodItem *method, 126 const Function &func, bool emit_debug_info); 127 static bool AddMethodAndParamsAnnotations(panda_file::ItemContainer *items, const Program &program, 128 const AsmEmitter::AsmEntityCollections &entities, 129 panda_file::MethodItem *method, const Function &func); 130 static bool MakeFunctionDebugInfoAndAnnotations(panda_file::ItemContainer *items, const Program &program, 131 const AsmEntityCollections &entities, bool emit_debug_info); 132 static void FillMap(PandaFileToPandaAsmMaps *maps, AsmEntityCollections &entities); 133 static void EmitDebugInfo(panda_file::ItemContainer *items, const Program &program, 134 const std::vector<uint8_t> *bytes, const panda_file::MethodItem *method, 135 const Function &func, const std::string &name, bool emit_debug_info); 136 static bool EmitFunctions(panda_file::ItemContainer *items, const Program &program, 137 const AsmEntityCollections &entities, bool emit_debug_info); 138 139 static panda_file::TypeItem *GetTypeItem( 140 panda_file::ItemContainer *items, 141 const std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> &primitive_types, 142 const Type &type, const Program &program); 143 SetLastError(const std::string & message)144 static void SetLastError(const std::string &message) 145 { 146 last_error = message; 147 } 148 149 static bool MakeItemsForSingleProgram(panda_file::ItemContainer *items, const Program &program, 150 bool emit_debug_info, AsmEntityCollections &entities, 151 std::unordered_map<panda_file::Type::TypeId, panda_file::PrimitiveTypeItem *> primitive_types); 152 153 static std::string GetMethodSignatureFromProgram(const std::string &name, const Program &program); 154 155 static panda_file::LiteralItem *CreateLiteralItem( 156 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::LiteralItem> *out, 157 const AsmEmitter::AsmEntityCollections &entities); 158 159 template <class PrimType> CreateScalarPrimValueItem(panda_file::ItemContainer * container,const Value * value,std::vector<panda_file::ScalarValueItem> * out)160 static panda_file::ScalarValueItem *CreateScalarPrimValueItem(panda_file::ItemContainer *container, 161 const Value *value, 162 std::vector<panda_file::ScalarValueItem> *out) 163 { 164 static_assert(std::is_arithmetic<PrimType>::value); 165 auto v = value->GetAsScalar()->GetValue<PrimType>(); 166 if (out != nullptr) { 167 out->emplace_back(v, container); 168 return &out->back(); 169 } 170 171 if constexpr (std::is_same<PrimType, uint32_t>::value) { 172 return container->GetOrCreateIntegerValueItem(v); 173 } else if constexpr (std::is_same<PrimType, uint64_t>::value) { 174 return container->GetOrCreateLongValueItem(v); 175 } else if constexpr (std::is_same<PrimType, float>::value) { 176 return container->GetOrCreateFloatValueItem(v); 177 } else if constexpr (std::is_same<PrimType, double>::value) { 178 return container->GetOrCreateDoubleValueItem(v); 179 } else { 180 UNREACHABLE(); 181 return nullptr; 182 } 183 } 184 185 static panda_file::ScalarValueItem *CreateScalarStringValueItem(panda_file::ItemContainer *container, 186 const Value *value, 187 std::vector<panda_file::ScalarValueItem> *out); 188 static panda_file::ScalarValueItem *CreateScalarRecordValueItem( 189 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 190 const std::unordered_map<std::string, panda_file::BaseClassItem *> &classes); 191 static panda_file::ScalarValueItem *CreateScalarMethodValueItem( 192 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 193 const Program &program, const std::unordered_map<std::string, panda_file::BaseMethodItem *> &methods); 194 static panda_file::ScalarValueItem *CreateScalarLiteralArrayItem( 195 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 196 const Program &program, const std::unordered_map<std::string, panda_file::LiteralArrayItem *> &literalarrays); 197 static panda_file::ScalarValueItem *CreateScalarEnumValueItem( 198 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 199 const std::unordered_map<std::string, panda_file::BaseFieldItem *> &fields); 200 static panda_file::ScalarValueItem *CreateScalarAnnotationValueItem( 201 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 202 const Program &program, const AsmEmitter::AsmEntityCollections &entities); 203 204 static panda_file::ScalarValueItem *CreateScalarValueItem( 205 panda_file::ItemContainer *container, const Value *value, std::vector<panda_file::ScalarValueItem> *out, 206 const Program &program, const AsmEmitter::AsmEntityCollections &entities); 207 208 static panda_file::ValueItem *CreateValueItem( 209 panda_file::ItemContainer *container, const Value *value, const Program &program, 210 const AsmEmitter::AsmEntityCollections &entities); 211 212 static panda_file::AnnotationItem *CreateAnnotationItem( 213 panda_file::ItemContainer *container, const AnnotationData &annotation, const Program &program, 214 const AsmEmitter::AsmEntityCollections &entities); 215 216 static panda_file::MethodHandleItem *CreateMethodHandleItem( 217 panda_file::ItemContainer *container, const MethodHandle &mh, 218 const std::unordered_map<std::string, panda_file::BaseFieldItem *> &fields, 219 const std::unordered_map<std::string, panda_file::BaseMethodItem *> &methods); 220 221 template <class T> 222 static bool AddAnnotations(T *item, panda_file::ItemContainer *container, const AnnotationMetadata &metadata, 223 const Program &program, const AsmEmitter::AsmEntityCollections &entities); 224 225 // TODO(mgonopolsky): Refactor to introduce a single error-processing mechanism for parser and emitter 226 static std::string last_error; 227 }; 228 229 std::string GetOwnerName(std::string name); 230 std::string GetItemName(std::string name); 231 232 } // namespace panda::pandasm 233 234 #endif // ASSEMBLER_ASSEMBLY_EMITTER_H 235