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