• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "assembly-emitter.h"
17 
18 #include <algorithm>
19 #include <cctype>
20 #include <iostream>
21 #include <sstream>
22 
23 #include "bytecode_instruction-inl.h"
24 #include "file_items.h"
25 #include "file_writer.h"
26 #include "mangling.h"
27 #include "os/file.h"
28 
29 namespace {
30 
31 using panda::os::file::Mode;
32 using panda::os::file::Open;
33 using panda::panda_file::AnnotationItem;
34 using panda::panda_file::ArrayValueItem;
35 using panda::panda_file::BaseClassItem;
36 using panda::panda_file::BaseFieldItem;
37 using panda::panda_file::BaseMethodItem;
38 using panda::panda_file::ClassItem;
39 using panda::panda_file::CodeItem;
40 using panda::panda_file::DebugInfoItem;
41 using panda::panda_file::FieldItem;
42 using panda::panda_file::FileWriter;
43 using panda::panda_file::ForeignClassItem;
44 using panda::panda_file::ForeignFieldItem;
45 using panda::panda_file::ForeignMethodItem;
46 using panda::panda_file::ItemContainer;
47 using panda::panda_file::LineNumberProgramItem;
48 using panda::panda_file::MemoryBufferWriter;
49 using panda::panda_file::MethodHandleItem;
50 using panda::panda_file::MethodItem;
51 using panda::panda_file::MethodParamItem;
52 using panda::panda_file::ParamAnnotationsItem;
53 using panda::panda_file::PrimitiveTypeItem;
54 using panda::panda_file::ProtoItem;
55 using panda::panda_file::ScalarValueItem;
56 using panda::panda_file::StringItem;
57 using panda::panda_file::Type;
58 using panda::panda_file::TypeItem;
59 using panda::panda_file::ValueItem;
60 using panda::panda_file::Writer;
61 using panda::panda_file::LiteralArrayItem;
62 
CreatePrimitiveTypes(ItemContainer * container)63 std::unordered_map<Type::TypeId, PrimitiveTypeItem *> CreatePrimitiveTypes(ItemContainer *container)
64 {
65     auto res = std::unordered_map<Type::TypeId, PrimitiveTypeItem *> {};
66     res.insert({Type::TypeId::VOID, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::VOID)});
67     res.insert({Type::TypeId::U1, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::U1)});
68     res.insert({Type::TypeId::I8, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::I8)});
69     res.insert({Type::TypeId::U8, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::U8)});
70     res.insert({Type::TypeId::I16, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::I16)});
71     res.insert({Type::TypeId::U16, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::U16)});
72     res.insert({Type::TypeId::I32, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::I32)});
73     res.insert({Type::TypeId::U32, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::U32)});
74     res.insert({Type::TypeId::I64, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::I64)});
75     res.insert({Type::TypeId::U64, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::U64)});
76     res.insert({Type::TypeId::F32, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::F32)});
77     res.insert({Type::TypeId::F64, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::F64)});
78     res.insert({Type::TypeId::TAGGED, container->GetOrCreatePrimitiveTypeItem(Type::TypeId::TAGGED)});
79     return res;
80 }
81 
82 template <class T>
Find(const T & map,typename T::key_type key)83 typename T::mapped_type Find(const T &map, typename T::key_type key)
84 {
85     auto res = map.find(key);
86     ASSERT(res != map.end());
87     return res->second;
88 }
89 
90 }  // anonymous namespace
91 
92 namespace panda::pandasm {
93 
94 /* static */
95 std::string AsmEmitter::last_error("");
96 
GetTypeId(Value::Type type)97 static panda_file::Type::TypeId GetTypeId(Value::Type type)
98 {
99     switch (type) {
100         case Value::Type::U1:
101             return panda_file::Type::TypeId::U1;
102         case Value::Type::I8:
103             return panda_file::Type::TypeId::I8;
104         case Value::Type::U8:
105             return panda_file::Type::TypeId::U8;
106         case Value::Type::I16:
107             return panda_file::Type::TypeId::I16;
108         case Value::Type::U16:
109             return panda_file::Type::TypeId::U16;
110         case Value::Type::I32:
111             return panda_file::Type::TypeId::I32;
112         case Value::Type::U32:
113             return panda_file::Type::TypeId::U32;
114         case Value::Type::I64:
115             return panda_file::Type::TypeId::I64;
116         case Value::Type::U64:
117             return panda_file::Type::TypeId::U64;
118         case Value::Type::F32:
119             return panda_file::Type::TypeId::F32;
120         case Value::Type::F64:
121             return panda_file::Type::TypeId::F64;
122         case Value::Type::VOID:
123             return panda_file::Type::TypeId::VOID;
124         default:
125             return panda_file::Type::TypeId::REFERENCE;
126     }
127 }
128 
129 /* static */
GetMethodSignatureFromProgram(const std::string & name,const Program & program)130 std::string AsmEmitter::GetMethodSignatureFromProgram(const std::string &name, const Program &program)
131 {
132     if (IsSignatureOrMangled(name)) {
133         return name;
134     }
135 
136     const auto it_synonym = program.function_synonyms.find(name);
137     const bool is_method_known = (it_synonym != program.function_synonyms.end());
138     const bool is_single_synonym = (is_method_known && (it_synonym->second.size() == 1));
139     if (is_single_synonym) {
140         return it_synonym->second[0];
141     } else {
142         SetLastError("More than one alternative for method " + name);
143         return std::string("");
144     }
145 }
146 
147 /* static */
CreateLiteralItem(ItemContainer * container,const Value * value,std::vector<panda_file::LiteralItem> * out,const AsmEmitter::AsmEntityCollections & entities)148 panda_file::LiteralItem *AsmEmitter::CreateLiteralItem(
149     ItemContainer *container, const Value *value, std::vector<panda_file::LiteralItem> *out,
150     const AsmEmitter::AsmEntityCollections &entities)
151 {
152     ASSERT(out != nullptr);
153 
154     auto value_type = value->GetType();
155 
156     switch (value_type) {
157         case Value::Type::U1:
158         case Value::Type::I8:
159         case Value::Type::U8: {
160             auto v = value->GetAsScalar()->GetValue<uint8_t>();
161             out->emplace_back(v);
162             return &out->back();
163         }
164         case Value::Type::I16:
165         case Value::Type::U16: {
166             auto v = value->GetAsScalar()->GetValue<uint16_t>();
167             out->emplace_back(v);
168             return &out->back();
169         }
170         case Value::Type::I32:
171         case Value::Type::U32:
172         case Value::Type::STRING_NULLPTR: {
173             auto v = value->GetAsScalar()->GetValue<uint32_t>();
174             out->emplace_back(v);
175             return &out->back();
176         }
177         case Value::Type::I64:
178         case Value::Type::U64: {
179             auto v = value->GetAsScalar()->GetValue<uint64_t>();
180             out->emplace_back(v);
181             return &out->back();
182         }
183         case Value::Type::F32: {
184             auto v = bit_cast<uint32_t>(value->GetAsScalar()->GetValue<float>());
185             out->emplace_back(v);
186             return &out->back();
187         }
188         case Value::Type::F64: {
189             auto v = bit_cast<uint64_t>(value->GetAsScalar()->GetValue<double>());
190             out->emplace_back(v);
191             return &out->back();
192         }
193         case Value::Type::STRING: {
194             auto *string_item = container->GetOrCreateStringItem(value->GetAsScalar()->GetValue<std::string>());
195             out->emplace_back(string_item);
196             return &out->back();
197         }
198         case Value::Type::METHOD: {
199             auto name = value->GetAsScalar()->GetValue<std::string>();
200             auto method_item = static_cast<panda::panda_file::MethodItem *>(Find(entities.method_items, name));
201             out->emplace_back(method_item);
202             return &out->back();
203         }
204         case Value::Type::LITERALARRAY: {
205             auto key = value->GetAsScalar()->GetValue<std::string>();
206             auto lit_item = Find(entities.literalarray_items, key);
207             out->emplace_back(lit_item);
208             return &out->back();
209         }
210         default:
211             return nullptr;
212     }
213 }
214 
215 /* static */
CreateScalarStringValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out)216 ScalarValueItem *AsmEmitter::CreateScalarStringValueItem(ItemContainer *container, const Value *value,
217                                                          std::vector<ScalarValueItem> *out)
218 {
219     auto *string_item = container->GetOrCreateStringItem(value->GetAsScalar()->GetValue<std::string>());
220     if (out != nullptr) {
221         out->emplace_back(string_item, container);
222         return &out->back();
223     }
224 
225     return container->CreateItem<ScalarValueItem>(string_item);
226 }
227 
228 /* static */
CreateScalarRecordValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const std::unordered_map<std::string,BaseClassItem * > & classes)229 ScalarValueItem *AsmEmitter::CreateScalarRecordValueItem(
230     ItemContainer *container, const Value *value, std::vector<ScalarValueItem> *out,
231     const std::unordered_map<std::string, BaseClassItem *> &classes)
232 {
233     auto type = value->GetAsScalar()->GetValue<Type>();
234     BaseClassItem *class_item;
235     if (type.IsObject()) {
236         auto name = type.GetName();
237         auto it = classes.find(name);
238         if (it == classes.cend()) {
239             return nullptr;
240         }
241 
242         class_item = it->second;
243     } else {
244         class_item = container->GetOrCreateForeignClassItem(type.GetDescriptor());
245     }
246 
247     if (out != nullptr) {
248         out->emplace_back(class_item, container);
249         return &out->back();
250     }
251 
252     return container->CreateItem<ScalarValueItem>(class_item);
253 }
254 
255 /* static */
CreateScalarMethodValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const Program & program,const std::unordered_map<std::string,BaseMethodItem * > & methods)256 ScalarValueItem *AsmEmitter::CreateScalarMethodValueItem(
257     ItemContainer *container, const Value *value, std::vector<ScalarValueItem> *out, const Program &program,
258     const std::unordered_map<std::string, BaseMethodItem *> &methods)
259 {
260     auto name = value->GetAsScalar()->GetValue<std::string>();
261 
262     name = GetMethodSignatureFromProgram(name, program);
263 
264     auto it = methods.find(name);
265     if (it == methods.cend()) {
266         return nullptr;
267     }
268 
269     auto *method_item = it->second;
270     if (out != nullptr) {
271         out->emplace_back(method_item, container);
272         return &out->back();
273     }
274 
275     return container->CreateItem<ScalarValueItem>(method_item);
276 }
277 
278 /* static */
CreateScalarLiteralArrayItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const Program & program,const std::unordered_map<std::string,LiteralArrayItem * > & literalarrays)279 ScalarValueItem *AsmEmitter::CreateScalarLiteralArrayItem(
280     ItemContainer *container, const Value *value, std::vector<ScalarValueItem> *out, const Program &program,
281     const std::unordered_map<std::string, LiteralArrayItem *> &literalarrays)
282 {
283     auto name = value->GetAsScalar()->GetValue<std::string>();
284     auto it = literalarrays.find(name);
285     ASSERT(it != literalarrays.end());
286     auto *literalarray_item = it->second;
287     if (out != nullptr) {
288         out->emplace_back(literalarray_item, container);
289         return &out->back();
290     }
291 
292     return container->CreateItem<ScalarValueItem>(literalarray_item);
293 }
294 
295 /* static */
CreateScalarEnumValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const std::unordered_map<std::string,BaseFieldItem * > & fields)296 ScalarValueItem *AsmEmitter::CreateScalarEnumValueItem(ItemContainer *container, const Value *value,
297                                                        std::vector<ScalarValueItem> *out,
298                                                        const std::unordered_map<std::string, BaseFieldItem *> &fields)
299 {
300     auto name = value->GetAsScalar()->GetValue<std::string>();
301     auto it = fields.find(name);
302     if (it == fields.cend()) {
303         return nullptr;
304     }
305 
306     auto *field_item = it->second;
307     if (out != nullptr) {
308         out->emplace_back(field_item, container);
309         return &out->back();
310     }
311 
312     return container->CreateItem<ScalarValueItem>(field_item);
313 }
314 
315 /* static */
CreateScalarAnnotationValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const Program & program,const AsmEmitter::AsmEntityCollections & entities)316 ScalarValueItem *AsmEmitter::CreateScalarAnnotationValueItem(
317     ItemContainer *container, const Value *value, std::vector<ScalarValueItem> *out, const Program &program,
318     const AsmEmitter::AsmEntityCollections &entities)
319 {
320     auto annotation = value->GetAsScalar()->GetValue<AnnotationData>();
321     auto *annotation_item = CreateAnnotationItem(container, annotation, program, entities);
322     if (annotation_item == nullptr) {
323         return nullptr;
324     }
325 
326     if (out != nullptr) {
327         out->emplace_back(annotation_item, container);
328         return &out->back();
329     }
330 
331     return container->CreateItem<ScalarValueItem>(annotation_item);
332 }
333 
334 /* static */
CreateScalarValueItem(ItemContainer * container,const Value * value,std::vector<ScalarValueItem> * out,const Program & program,const AsmEmitter::AsmEntityCollections & entities)335 ScalarValueItem *AsmEmitter::CreateScalarValueItem(ItemContainer *container, const Value *value,
336                                                    std::vector<ScalarValueItem> *out, const Program &program,
337                                                    const AsmEmitter::AsmEntityCollections &entities)
338 {
339     auto value_type = value->GetType();
340 
341     switch (value_type) {
342         case Value::Type::U1:
343         case Value::Type::I8:
344         case Value::Type::U8:
345         case Value::Type::I16:
346         case Value::Type::U16:
347         case Value::Type::I32:
348         case Value::Type::U32:
349         case Value::Type::STRING_NULLPTR: {
350             return CreateScalarPrimValueItem<uint32_t>(container, value, out);
351         }
352         case Value::Type::I64:
353         case Value::Type::U64: {
354             return CreateScalarPrimValueItem<uint64_t>(container, value, out);
355         }
356         case Value::Type::F32: {
357             return CreateScalarPrimValueItem<float>(container, value, out);
358         }
359         case Value::Type::F64: {
360             return CreateScalarPrimValueItem<double>(container, value, out);
361         }
362         case Value::Type::STRING: {
363             return CreateScalarStringValueItem(container, value, out);
364         }
365         case Value::Type::RECORD: {
366             return CreateScalarRecordValueItem(container, value, out, entities.class_items);
367         }
368         case Value::Type::METHOD: {
369             return CreateScalarMethodValueItem(container, value, out, program, entities.method_items);
370         }
371         case Value::Type::ENUM: {
372             return CreateScalarEnumValueItem(container, value, out, entities.field_items);
373         }
374         case Value::Type::ANNOTATION: {
375             return CreateScalarAnnotationValueItem(container, value, out, program, entities);
376         }
377         case Value::Type::LITERALARRAY: {
378             return CreateScalarLiteralArrayItem(container, value, out, program, entities.literalarray_items);
379         }
380         default: {
381             UNREACHABLE();
382             return nullptr;
383         }
384     }
385 }
386 
387 /* static */
CreateValueItem(ItemContainer * container,const Value * value,const Program & program,const AsmEmitter::AsmEntityCollections & entities)388 ValueItem *AsmEmitter::CreateValueItem(ItemContainer *container, const Value *value, const Program &program,
389                                        const AsmEmitter::AsmEntityCollections &entities)
390 {
391     switch (value->GetType()) {
392         case Value::Type::ARRAY: {
393             std::vector<ScalarValueItem> elements;
394             for (const auto &elem_value : value->GetAsArray()->GetValues()) {
395                 auto *item =
396                     CreateScalarValueItem(container, &elem_value, &elements, program, entities);
397                 if (item == nullptr) {
398                     return nullptr;
399                 }
400             }
401 
402             auto component_type = value->GetAsArray()->GetComponentType();
403             return container->CreateItem<ArrayValueItem>(panda_file::Type(GetTypeId(component_type)),
404                                                          std::move(elements));
405         }
406         default: {
407             return CreateScalarValueItem(container, value, nullptr, program, entities);
408         }
409     }
410 }
411 
412 /* static */
CreateAnnotationItem(ItemContainer * container,const AnnotationData & annotation,const Program & program,const AsmEmitter::AsmEntityCollections & entities)413 AnnotationItem *AsmEmitter::CreateAnnotationItem(ItemContainer *container, const AnnotationData &annotation,
414     const Program &program, const AsmEmitter::AsmEntityCollections &entities)
415 {
416     auto record_name = annotation.GetName();
417     auto it = program.record_table.find(record_name);
418     if (it == program.record_table.cend()) {
419         SetLastError("Record " + record_name + " not found");
420         return nullptr;
421     }
422 
423     auto &record = it->second;
424     if (!record.metadata->IsAnnotation()) {
425         SetLastError("Record " + record_name + " isn't annotation");
426         return nullptr;
427     }
428 
429     std::vector<AnnotationItem::Elem> item_elements;
430     std::vector<AnnotationItem::Tag> tag_elements;
431 
432     for (const auto &element : annotation.GetElements()) {
433         auto name = element.GetName();
434         auto *value = element.GetValue();
435 
436         auto value_type = value->GetType();
437 
438         uint8_t tag_type;
439 
440         if (value_type == Value::Type::ARRAY && !value->GetAsArray()->GetValues().empty()) {
441             auto array_element_type = value->GetAsArray()->GetComponentType();
442             tag_type = static_cast<uint8_t>(Value::GetArrayTypeAsChar(array_element_type));
443         } else {
444             tag_type = static_cast<uint8_t>(Value::GetTypeAsChar(value_type));
445         }
446 
447         ASSERT(tag_type != '0');
448 
449         auto *item = CreateValueItem(container, value, program, entities);
450         if (item == nullptr) {
451             SetLastError("Cannot create value item for annotation element " + name + ": " + GetLastError());
452             return nullptr;
453         }
454 
455         item_elements.emplace_back(container->GetOrCreateStringItem(name), item);
456         tag_elements.emplace_back(tag_type);
457     }
458 
459     auto *cls = entities.class_items.find(record_name)->second;
460     return container->CreateItem<AnnotationItem>(cls, std::move(item_elements), std::move(tag_elements));
461 }
462 
CreateMethodHandleItem(ItemContainer * container,const MethodHandle & mh,const std::unordered_map<std::string,BaseFieldItem * > & fields,const std::unordered_map<std::string,BaseMethodItem * > & methods)463 MethodHandleItem *AsmEmitter::CreateMethodHandleItem(ItemContainer *container, const MethodHandle &mh,
464                                                      const std::unordered_map<std::string, BaseFieldItem *> &fields,
465                                                      const std::unordered_map<std::string, BaseMethodItem *> &methods)
466 {
467     MethodHandleItem *item = nullptr;
468     switch (mh.type) {
469         case panda_file::MethodHandleType::PUT_STATIC:
470         case panda_file::MethodHandleType::GET_STATIC:
471         case panda_file::MethodHandleType::PUT_INSTANCE:
472         case panda_file::MethodHandleType::GET_INSTANCE: {
473             item = container->CreateItem<MethodHandleItem>(mh.type, fields.at(mh.item_name));
474             break;
475         }
476         case panda_file::MethodHandleType::INVOKE_STATIC:
477         case panda_file::MethodHandleType::INVOKE_INSTANCE:
478         case panda_file::MethodHandleType::INVOKE_CONSTRUCTOR:
479         case panda_file::MethodHandleType::INVOKE_DIRECT:
480         case panda_file::MethodHandleType::INVOKE_INTERFACE: {
481             item = container->CreateItem<MethodHandleItem>(mh.type, methods.at(mh.item_name));
482             break;
483         }
484         default:
485             UNREACHABLE();
486             break;
487     }
488     return item;
489 }
490 
491 /* static */
492 template <class T>
AddAnnotations(T * item,ItemContainer * container,const AnnotationMetadata & metadata,const Program & program,const AsmEmitter::AsmEntityCollections & entities)493 bool AsmEmitter::AddAnnotations(T *item, ItemContainer *container, const AnnotationMetadata &metadata,
494                                 const Program &program, const AsmEmitter::AsmEntityCollections &entities)
495 {
496     for (const auto &annotation : metadata.GetAnnotations()) {
497         auto *annotation_item = CreateAnnotationItem(container, annotation, program, entities);
498         if (annotation_item == nullptr) {
499             return false;
500         }
501 
502         auto &record = program.record_table.find(annotation.GetName())->second;
503         if (record.metadata->IsRuntimeAnnotation()) {
504             item->AddRuntimeAnnotation(annotation_item);
505         } else if (record.metadata->IsAnnotation()) {
506             item->AddAnnotation(annotation_item);
507         } else if (record.metadata->IsRuntimeTypeAnnotation()) {
508             item->AddRuntimeTypeAnnotation(annotation_item);
509         } else if (record.metadata->IsTypeAnnotation()) {
510             item->AddTypeAnnotation(annotation_item);
511         }
512     }
513 
514     return true;
515 }
516 
517 template <class T>
AddDependencyByIndex(MethodItem * method,const Ins & insn,const std::unordered_map<std::string,T * > & items,size_t idx=0)518 static bool AddDependencyByIndex(MethodItem *method, const Ins &insn,
519                                  const std::unordered_map<std::string, T *> &items, size_t idx = 0)
520 {
521     if (idx >= insn.ids.size()) {
522         return false;
523     };
524     const auto &id = insn.ids[idx];
525     auto it = items.find(id);
526     ASSERT(it != items.cend());
527     auto *item = it->second;
528     ASSERT(item->GetIndexType() != panda_file::IndexType::NONE);
529     method->AddIndexDependency(item);
530     return true;
531 }
532 
AddBytecodeIndexDependencies(MethodItem * method,const Function & func,const AsmEmitter::AsmEntityCollections & entities)533 static bool AddBytecodeIndexDependencies(MethodItem *method, const Function &func,
534                                          const AsmEmitter::AsmEntityCollections &entities)
535 {
536     for (const auto &insn : func.ins) {
537         if (insn.opcode == Opcode::INVALID) {
538             continue;
539         }
540 
541         if (insn.opcode == Opcode::DEFINECLASSWITHBUFFER) {
542             if (!AddDependencyByIndex(method, insn, entities.method_items)) {
543                 return false;
544             }
545             if (!AddDependencyByIndex(method, insn, entities.literalarray_items, 1)) {
546                 return false;
547             }
548             continue;
549         }
550 
551         if (insn.opcode == Opcode::CALLRUNTIME_DEFINESENDABLECLASS) {
552             if (!AddDependencyByIndex(method, insn, entities.method_items)) {
553                 return false;
554             }
555             if (!AddDependencyByIndex(method, insn, entities.literalarray_items, 1)) {
556                 return false;
557             }
558             continue;
559         }
560 
561         if (insn.HasFlag(InstFlags::METHOD_ID)) {
562             if (!AddDependencyByIndex(method, insn, entities.method_items)) {
563                 return false;
564             }
565             continue;
566         }
567 
568         if (insn.HasFlag(InstFlags::STRING_ID)) {
569             if (!AddDependencyByIndex(method, insn, entities.string_items)) {
570                 return false;
571             }
572             continue;
573         }
574 
575         if (insn.HasFlag(InstFlags::LITERALARRAY_ID)) {
576             if (!AddDependencyByIndex(method, insn, entities.literalarray_items)) {
577                 return false;
578             }
579         }
580     }
581     return true;
582 }
583 
584 /* static */
MakeStringItems(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities)585 void AsmEmitter::MakeStringItems(ItemContainer *items, const Program &program,
586                                  AsmEmitter::AsmEntityCollections &entities)
587 {
588     for (const auto &s : program.strings) {
589         auto *item = items->GetOrCreateStringItem(s);
590         entities.string_items.insert({s, item});
591     }
592 }
593 
594 /* static */
MakeLiteralItems(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities)595 void AsmEmitter::MakeLiteralItems(ItemContainer *items, const Program &program,
596                                   AsmEmitter::AsmEntityCollections &entities)
597 {
598     for (const auto &[id, l] : program.literalarray_table) {
599         auto *literal_array_item = items->GetOrCreateLiteralArrayItem(id);
600         entities.literalarray_items.insert({id, literal_array_item});
601     }
602 
603     for (const auto &[id, l] : program.literalarray_table) {
604         auto *literal_array_item = entities.literalarray_items.find(id)->second;
605         std::vector<panda_file::LiteralItem> literal_array;
606         for (auto &literal : l.literals_) {
607             std::unique_ptr<ScalarValue> value;
608             switch (literal.tag_) {
609                 case panda_file::LiteralTag::ARRAY_U1: {
610                     ASSERT(program.array_types.find(Type("u1", 1)) != program.array_types.end());
611                     value = std::make_unique<ScalarValue>(
612                         ScalarValue::Create<Value::Type::U1>(static_cast<bool>(std::get<bool>(literal.value_))));
613                     break;
614                 }
615                 case panda_file::LiteralTag::ARRAY_U8: {
616                     ASSERT(program.array_types.find(Type("u8", 1)) != program.array_types.end());
617                     value = std::make_unique<ScalarValue>(
618                         ScalarValue::Create<Value::Type::U8>(std::get<uint8_t>(literal.value_)));
619                     break;
620                 }
621                 case panda_file::LiteralTag::ARRAY_I8: {
622                     ASSERT(program.array_types.find(Type("i8", 1)) != program.array_types.end());
623                     value = std::make_unique<ScalarValue>(
624                         ScalarValue::Create<Value::Type::I8>(std::get<uint8_t>(literal.value_)));
625                     break;
626                 }
627                 case panda_file::LiteralTag::ARRAY_U16: {
628                     ASSERT(program.array_types.find(Type("u16", 1)) != program.array_types.end());
629                     value = std::make_unique<ScalarValue>(
630                         ScalarValue::Create<Value::Type::U16>(std::get<uint16_t>(literal.value_)));
631                     break;
632                 }
633                 case panda_file::LiteralTag::ARRAY_I16: {
634                     ASSERT(program.array_types.find(Type("i16", 1)) != program.array_types.end());
635                     value = std::make_unique<ScalarValue>(
636                         ScalarValue::Create<Value::Type::I16>(std::get<uint16_t>(literal.value_)));
637                     break;
638                 }
639                 case panda_file::LiteralTag::ARRAY_U32: {
640                     ASSERT(program.array_types.find(Type("u32", 1)) != program.array_types.end());
641                     value = std::make_unique<ScalarValue>(
642                         ScalarValue::Create<Value::Type::U32>(std::get<uint32_t>(literal.value_)));
643                     break;
644                 }
645                 case panda_file::LiteralTag::ARRAY_I32: {
646                     ASSERT(program.array_types.find(Type("i32", 1)) != program.array_types.end());
647                     value = std::make_unique<ScalarValue>(
648                         ScalarValue::Create<Value::Type::I32>(std::get<uint32_t>(literal.value_)));
649                     break;
650                 }
651                 case panda_file::LiteralTag::ARRAY_U64: {
652                     ASSERT(program.array_types.find(Type("u64", 1)) != program.array_types.end());
653                     value = std::make_unique<ScalarValue>(
654                         ScalarValue::Create<Value::Type::U64>(std::get<uint64_t>(literal.value_)));
655                     break;
656                 }
657                 case panda_file::LiteralTag::ARRAY_I64: {
658                     ASSERT(program.array_types.find(Type("i64", 1)) != program.array_types.end());
659                     value = std::make_unique<ScalarValue>(
660                         ScalarValue::Create<Value::Type::I64>(std::get<uint64_t>(literal.value_)));
661                     break;
662                 }
663                 case panda_file::LiteralTag::ARRAY_F32: {
664                     ASSERT(program.array_types.find(Type("f32", 1)) != program.array_types.end());
665                     value = std::make_unique<ScalarValue>(
666                         ScalarValue::Create<Value::Type::F32>(std::get<float>(literal.value_)));
667                     break;
668                 }
669                 case panda_file::LiteralTag::ARRAY_F64: {
670                     ASSERT(program.array_types.find(Type("f64", 1)) != program.array_types.end());
671                     value = std::make_unique<ScalarValue>(
672                         ScalarValue::Create<Value::Type::F64>(std::get<double>(literal.value_)));
673                     break;
674                 }
675                 case panda_file::LiteralTag::ARRAY_STRING:
676                     ASSERT(program.array_types.find(Type(
677                         Type::FromDescriptor(panda::panda_file::GetStringClassDescriptor(program.lang)), 1)) !=
678                            program.array_types.end());
679                     value = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::STRING>(
680                         std::string_view(std::get<std::string>(literal.value_))));
681                     break;
682                 case panda_file::LiteralTag::TAGVALUE:
683                 case panda_file::LiteralTag::ACCESSOR:
684                 case panda_file::LiteralTag::NULLVALUE:
685                 case panda_file::LiteralTag::BUILTINTYPEINDEX:
686                     value = std::make_unique<ScalarValue>(
687                         ScalarValue::Create<Value::Type::U8>(static_cast<uint8_t>(std::get<uint8_t>(literal.value_))));
688                     break;
689                 case panda_file::LiteralTag::BOOL:
690                     value = std::make_unique<ScalarValue>(
691                         ScalarValue::Create<Value::Type::U8>(static_cast<uint8_t>(std::get<bool>(literal.value_))));
692                     break;
693                 case panda_file::LiteralTag::METHODAFFILIATE:
694                     value = std::make_unique<ScalarValue>(
695                         ScalarValue::Create<Value::Type::U16>(std::get<uint16_t>(literal.value_)));
696                     break;
697                 case panda_file::LiteralTag::INTEGER:
698                 case panda_file::LiteralTag::LITERALBUFFERINDEX:
699                     value = std::make_unique<ScalarValue>(
700                         ScalarValue::Create<Value::Type::I32>(std::get<uint32_t>(literal.value_)));
701                     break;
702                 case panda_file::LiteralTag::FLOAT:
703                     value = std::make_unique<ScalarValue>(
704                         ScalarValue::Create<Value::Type::F32>(std::get<float>(literal.value_)));
705                     break;
706                 case panda_file::LiteralTag::DOUBLE:
707                     value = std::make_unique<ScalarValue>(
708                         ScalarValue::Create<Value::Type::F64>(std::get<double>(literal.value_)));
709                     break;
710                 case panda_file::LiteralTag::STRING:
711                     value = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::STRING>(
712                         std::string_view(std::get<std::string>(literal.value_))));
713                     break;
714                 case panda_file::LiteralTag::METHOD:
715                 case panda_file::LiteralTag::GETTER:
716                 case panda_file::LiteralTag::SETTER:
717                 case panda_file::LiteralTag::GENERATORMETHOD:
718                 case panda_file::LiteralTag::ASYNCGENERATORMETHOD:
719                     value = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::METHOD>(
720                         std::string_view(std::get<std::string>(literal.value_))));
721                     break;
722                 case panda_file::LiteralTag::LITERALARRAY:
723                     value = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::LITERALARRAY>(
724                         std::string_view(std::get<std::string>(literal.value_))));
725                     break;
726                 default:
727                     UNREACHABLE();
728             }
729 
730             // the return pointer of vector element should not be rewrited
731             CreateLiteralItem(items, value.get(), &literal_array, entities);
732         }
733         literal_array_item->AddItems(literal_array);
734     }
735 }
736 
737 /* static */
MakeArrayTypeItems(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities)738 void AsmEmitter::MakeArrayTypeItems(ItemContainer *items, const Program &program,
739                                     AsmEmitter::AsmEntityCollections &entities)
740 {
741     for (const auto &t : program.array_types) {
742         auto *foreign_record = items->GetOrCreateForeignClassItem(t.GetDescriptor());
743         entities.class_items.insert({t.GetName(), foreign_record});
744     }
745 }
746 
747 /* static */
HandleRecordAsForeign(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,const std::string & name,const Record & rec)748 bool AsmEmitter::HandleRecordAsForeign(
749     ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities,
750     const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types, const std::string &name,
751     const Record &rec)
752 {
753     Type record_type = Type::FromName(name);
754     auto *foreign_record = items->GetOrCreateForeignClassItem(record_type.GetDescriptor(rec.conflict));
755     entities.class_items.insert({name, foreign_record});
756     for (const auto &f : rec.field_list) {
757         ASSERT(f.metadata->IsForeign());
758         auto *field_name = items->GetOrCreateStringItem(pandasm::DeMangleName(f.name));
759         std::string full_field_name = name + "." + f.name;
760         if (!f.metadata->IsForeign()) {
761             SetLastError("External record " + name + " has a non-external field " + f.name);
762             return false;
763         }
764         auto *type_item = GetTypeItem(items, primitive_types, f.type, program);
765         if (type_item == nullptr) {
766             SetLastError("Field " + full_field_name + " has undefined type");
767             return false;
768         }
769         auto *field = items->CreateItem<ForeignFieldItem>(foreign_record, field_name, type_item);
770         entities.field_items.insert({full_field_name, field});
771     }
772     return true;
773 }
774 
775 /* static */
HandleBaseRecord(ItemContainer * items,const Program & program,const std::string & name,const Record & base_rec,ClassItem * record)776 bool AsmEmitter::HandleBaseRecord(ItemContainer *items, const Program &program, const std::string &name,
777                                   const Record &base_rec, ClassItem *record)
778 {
779     auto base_name = base_rec.metadata->GetBase();
780     if (!base_name.empty()) {
781         auto it = program.record_table.find(base_name);
782         if (it == program.record_table.cend()) {
783             SetLastError("Base record " + base_name + " is not defined for record " + name);
784             return false;
785         }
786         auto &rec = it->second;
787         Type base_type(base_name, 0);
788         if (rec.metadata->IsForeign()) {
789             record->SetSuperClass(items->GetOrCreateForeignClassItem(base_type.GetDescriptor(rec.conflict)));
790         } else {
791             record->SetSuperClass(items->GetOrCreateClassItem(base_type.GetDescriptor(rec.conflict)));
792         }
793     }
794     return true;
795 }
796 
797 /* static */
HandleInterfaces(ItemContainer * items,const Program & program,const std::string & name,const Record & rec,ClassItem * record)798 bool AsmEmitter::HandleInterfaces(ItemContainer *items, const Program &program, const std::string &name,
799                                   const Record &rec, ClassItem *record)
800 {
801     auto ifaces = rec.metadata->GetInterfaces();
802     for (const auto &item : ifaces) {
803         auto it = program.record_table.find(item);
804         if (it == program.record_table.cend()) {
805             SetLastError("Interface record " + item + " is not defined for record " + name);
806             return false;
807         }
808         auto &iface = it->second;
809         Type iface_type(item, 0);
810         if (iface.metadata->IsForeign()) {
811             record->AddInterface(items->GetOrCreateForeignClassItem(iface_type.GetDescriptor(iface.conflict)));
812         } else {
813             record->AddInterface(items->GetOrCreateClassItem(iface_type.GetDescriptor(iface.conflict)));
814         }
815     }
816     return true;
817 }
818 
819 /* static */
HandleFields(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,const std::string & name,const Record & rec,ClassItem * record)820 bool AsmEmitter::HandleFields(ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities,
821                               const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types,
822                               const std::string &name, const Record &rec, ClassItem *record)
823 {
824     for (const auto &f : rec.field_list) {
825         auto *field_name = items->GetOrCreateStringItem(pandasm::DeMangleName(f.name));
826         std::string full_field_name = name + "." + f.name;
827         auto *type_item = GetTypeItem(items, primitive_types, f.type, program);
828         if (type_item == nullptr) {
829             SetLastError("Field " + full_field_name + " has undefined type");
830             return false;
831         }
832         BaseFieldItem *field;
833         if (f.metadata->IsForeign()) {
834             field = items->CreateItem<ForeignFieldItem>(record, field_name, type_item);
835         } else {
836             field = record->AddField(field_name, type_item, f.metadata->GetAccessFlags());
837         }
838         entities.field_items.insert({full_field_name, field});
839     }
840     return true;
841 }
842 
843 /* static */
HandleRecord(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,const std::string & name,const Record & rec)844 bool AsmEmitter::HandleRecord(ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities,
845                               const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types,
846                               const std::string &name, const Record &rec)
847 {
848     Type record_type = Type::FromName(name);
849     auto *record = items->GetOrCreateClassItem(record_type.GetDescriptor(rec.conflict));
850     entities.class_items.insert({name, record});
851 
852     record->SetAccessFlags(rec.metadata->GetAccessFlags());
853     record->SetSourceLang(rec.language);
854 
855     if (!rec.source_file.empty()) {
856         auto *source_file_item = items->GetOrCreateStringItem(rec.source_file);
857         record->SetSourceFile(source_file_item);
858     }
859 
860     if (!HandleBaseRecord(items, program, name, rec, record)) {
861         return false;
862     }
863 
864     if (!HandleInterfaces(items, program, name, rec, record)) {
865         return false;
866     }
867 
868     if (!HandleFields(items, program, entities, primitive_types, name, rec, record)) {
869         return false;
870     }
871 
872     return true;
873 }
874 
875 /* static */
MakeRecordItems(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types)876 bool AsmEmitter::MakeRecordItems(
877     ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities,
878     const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types)
879 {
880     for (const auto &[name, rec] : program.record_table) {
881         if (rec.metadata->IsForeign()) {
882             if (!HandleRecordAsForeign(items, program, entities, primitive_types, name, rec)) {
883                 return false;
884             }
885         } else {
886             if (!HandleRecord(items, program, entities, primitive_types, name, rec)) {
887                 return false;
888             }
889         }
890     }
891     return true;
892 }
893 
894 /* static */
GetMethodName(ItemContainer * items,const Function & func,const std::string & name)895 StringItem *AsmEmitter::GetMethodName(ItemContainer *items, const Function &func, const std::string &name)
896 {
897     if (func.metadata->IsCtor()) {
898         return items->GetOrCreateStringItem(panda::panda_file::GetCtorName(func.language));
899     } else if (func.metadata->IsCctor()) {
900         return items->GetOrCreateStringItem(panda::panda_file::GetCctorName(func.language));
901     } else {
902         return items->GetOrCreateStringItem(GetItemName(name));
903     }
904 }
905 
906 /* static */
HandleAreaForInner(ItemContainer * items,const Program & program,ClassItem ** area,ForeignClassItem ** foreign_area,const std::string & name,const std::string & record_owner_name)907 bool AsmEmitter::HandleAreaForInner(ItemContainer *items, const Program &program, ClassItem **area,
908                                     ForeignClassItem **foreign_area, const std::string &name,
909                                     const std::string &record_owner_name)
910 {
911     auto iter = program.record_table.find(record_owner_name);
912     if (iter != program.record_table.end()) {
913         auto &rec = iter->second;
914         Type record_owner_type = Type::FromName(record_owner_name);
915         auto descriptor = record_owner_type.GetDescriptor(rec.conflict);
916         if (rec.metadata->IsForeign()) {
917             *foreign_area = items->GetOrCreateForeignClassItem(descriptor);
918             if (*foreign_area == nullptr) {
919                 SetLastError("Unable to create external record " + iter->first);
920                 return false;
921             }
922         } else {
923             *area = items->GetOrCreateClassItem(descriptor);
924             (*area)->SetAccessFlags(rec.metadata->GetAccessFlags());
925         }
926     } else {
927         SetLastError("Function " + name + " is bound to undefined record " + record_owner_name);
928         return false;
929     }
930     return true;
931 }
932 
933 /* static */
HandleRecordOnwer(ItemContainer * items,const Program & program,ClassItem ** area,ForeignClassItem ** foreign_area,const std::string & name,const std::string & record_owner_name)934 bool AsmEmitter::HandleRecordOnwer(ItemContainer *items, const Program &program, ClassItem **area,
935                                    ForeignClassItem **foreign_area, const std::string &name,
936                                    const std::string &record_owner_name)
937 {
938     if (record_owner_name.empty()) {
939         *area = items->GetOrCreateGlobalClassItem();
940         (*area)->SetAccessFlags(ACC_PUBLIC);
941         (*area)->SetSourceLang(program.lang);
942     } else {
943         if (!HandleAreaForInner(items, program, area, foreign_area, name, record_owner_name)) {
944             return false;
945         }
946     }
947     return true;
948 }
949 
950 /* static */
HandleFunctionParams(ItemContainer * items,const Program & program,size_t idx,const std::string & name,const Function & func,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,std::vector<MethodParamItem> & params)951 bool AsmEmitter::HandleFunctionParams(
952     ItemContainer *items, const Program &program, size_t idx, const std::string &name, const Function &func,
953     const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types,
954     std::vector<MethodParamItem> &params)
955 {
956     for (size_t i = idx; i < func.params.size(); i++) {
957         const auto &p = func.params[i].type;
958         auto *type_item = GetTypeItem(items, primitive_types, p, program);
959         if (type_item == nullptr) {
960             SetLastError("Argument " + std::to_string(i) + " of function " + name + " has undefined type");
961             return false;
962         }
963         params.emplace_back(type_item);
964     }
965     return true;
966 }
967 
968 /* static */
HandleFunctionLocalVariables(ItemContainer * items,const Function & func,const std::string & name)969 bool AsmEmitter::HandleFunctionLocalVariables(ItemContainer *items, const Function &func, const std::string &name)
970 {
971     for (const auto &v : func.local_variable_debug) {
972         if (v.name.empty()) {
973             SetLastError("Function '" + name + "' has an empty local variable name");
974             return false;
975         }
976         if (v.signature.empty()) {
977             SetLastError("Function '" + name + "' has an empty local variable signature");
978             return false;
979         }
980         items->GetOrCreateStringItem(v.name);
981         // Skip signature and signature type for parameters
982         ASSERT(v.reg >= 0);
983         if (func.IsParameter(v.reg)) {
984             continue;
985         }
986         items->GetOrCreateStringItem(v.signature);
987         if (!v.signature_type.empty()) {
988             items->GetOrCreateStringItem(v.signature_type);
989         }
990     }
991     return true;
992 }
993 
994 /* static */
CreateMethodItem(ItemContainer * items,AsmEmitter::AsmEntityCollections & entities,const Function & func,TypeItem * type_item,ClassItem * area,ForeignClassItem * foreign_area,uint32_t access_flags,StringItem * method_name,const std::string & mangled_name,const std::string & name,std::vector<MethodParamItem> & params)995 bool AsmEmitter::CreateMethodItem(ItemContainer *items, AsmEmitter::AsmEntityCollections &entities,
996                                   const Function &func, TypeItem *type_item, ClassItem *area,
997                                   ForeignClassItem *foreign_area, uint32_t access_flags, StringItem *method_name,
998                                   const std::string &mangled_name, const std::string &name,
999                                   std::vector<MethodParamItem> &params)
1000 {
1001     auto *proto = items->GetOrCreateProtoItem(type_item, params);
1002     BaseMethodItem *method;
1003     if (foreign_area == nullptr) {
1004         if (func.metadata->IsForeign()) {
1005             method = items->CreateItem<ForeignMethodItem>(area, method_name, proto, access_flags);
1006         } else {
1007             method = area->AddMethod(method_name, proto, access_flags, params);
1008             method->SetFunctionKind(func.GetFunctionKind());
1009         }
1010     } else {
1011         if (!func.metadata->IsForeign()) {
1012             SetLastError("Non-external function " + name + " is bound to external record");
1013             return false;
1014         }
1015         method = items->CreateItem<ForeignMethodItem>(foreign_area, method_name, proto, access_flags);
1016     }
1017     entities.method_items.insert({mangled_name, method});
1018     if (!func.metadata->IsForeign()) {
1019         if (!func.source_file.empty()) {
1020             items->GetOrCreateStringItem(func.source_file);
1021         }
1022         if (!func.source_code.empty()) {
1023             items->GetOrCreateStringItem(func.source_code);
1024         }
1025     }
1026     return true;
1027 }
1028 
1029 /* static */
MakeFunctionItems(ItemContainer * items,const Program & program,AsmEmitter::AsmEntityCollections & entities,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,bool emit_debug_info)1030 bool AsmEmitter::MakeFunctionItems(
1031     ItemContainer *items, const Program &program, AsmEmitter::AsmEntityCollections &entities,
1032     const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types, bool emit_debug_info)
1033 {
1034     for (const auto &f : program.function_table) {
1035         const auto &[mangled_name, func] = f;
1036 
1037         auto name = pandasm::DeMangleName(mangled_name);
1038 
1039         StringItem *method_name = GetMethodName(items, func, name);
1040 
1041         ClassItem *area = nullptr;
1042         ForeignClassItem *foreign_area = nullptr;
1043 
1044         std::string record_owner_name = GetOwnerName(name);
1045         if (!HandleRecordOnwer(items, program, &area, &foreign_area, name, record_owner_name)) {
1046             return false;
1047         }
1048 
1049         auto params = std::vector<MethodParamItem> {};
1050 
1051         uint32_t access_flags = func.metadata->GetAccessFlags();
1052 
1053         if (func.params.empty() || func.params[0].type.GetName() != record_owner_name) {
1054             access_flags |= ACC_STATIC;
1055         }
1056 
1057         bool is_static = (access_flags & ACC_STATIC) != 0;
1058         size_t idx = is_static ? 0 : 1;
1059 
1060         if (!HandleFunctionParams(items, program, idx, name, func, primitive_types, params)) {
1061             return false;
1062         }
1063 
1064         if (emit_debug_info && !HandleFunctionLocalVariables(items, func, name)) {
1065             return false;
1066         }
1067 
1068         auto *type_item = GetTypeItem(items, primitive_types, func.return_type, program);
1069         if (type_item == nullptr) {
1070             SetLastError("Function " + name + " has undefined return type");
1071             return false;
1072         }
1073 
1074         if (!CreateMethodItem(items, entities, func, type_item, area, foreign_area, access_flags, method_name,
1075                               mangled_name, name, params)) {
1076             return false;
1077         }
1078     }
1079     return true;
1080 }
1081 
1082 /* static */
MakeRecordAnnotations(ItemContainer * items,const Program & program,const AsmEmitter::AsmEntityCollections & entities)1083 bool AsmEmitter::MakeRecordAnnotations(ItemContainer *items, const Program &program,
1084                                        const AsmEmitter::AsmEntityCollections &entities)
1085 {
1086     for (const auto &[name, record] : program.record_table) {
1087         if (record.metadata->IsForeign()) {
1088             continue;
1089         }
1090 
1091         auto *class_item = static_cast<ClassItem *>(Find(entities.class_items, name));
1092         if (!AddAnnotations(class_item, items, *record.metadata, program, entities)) {
1093             SetLastError("Cannot emit annotations for record " + record.name + ": " + GetLastError());
1094             return false;
1095         }
1096 
1097         for (const auto &field : record.field_list) {
1098             auto field_name = record.name + "." + field.name;
1099             auto *field_item = static_cast<FieldItem *>(Find(entities.field_items, field_name));
1100             if (!AddAnnotations(field_item, items, *field.metadata, program, entities)) {
1101                 SetLastError("Cannot emit annotations for field " + field_name + ": " + GetLastError());
1102                 return false;
1103             }
1104 
1105             auto res = field.metadata->GetValue();
1106             if (res) {
1107                 auto value = res.value();
1108                 auto *item = CreateValueItem(items, &value, program, entities);
1109                 field_item->SetValue(item);
1110             }
1111         }
1112     }
1113     return true;
1114 }
1115 
1116 /* static */
SetCodeAndDebugInfo(ItemContainer * items,MethodItem * method,const Function & func,bool emit_debug_info)1117 void AsmEmitter::SetCodeAndDebugInfo(ItemContainer *items, MethodItem *method, const Function &func,
1118                                      bool emit_debug_info)
1119 {
1120     auto *code = items->CreateItem<CodeItem>();
1121     method->SetCode(code);
1122     code->AddMethod(method);  // we need it for Profile-Guided optimization
1123 
1124     if (!emit_debug_info && !func.CanThrow()) {
1125         return;
1126     }
1127 
1128     auto *line_number_program = items->CreateLineNumberProgramItem();
1129     auto *debug_info = items->CreateItem<DebugInfoItem>(line_number_program);
1130     if (emit_debug_info) {
1131         for (const auto &v : func.local_variable_debug) {
1132             ASSERT(v.reg >= 0);
1133             if (func.IsParameter(v.reg)) {
1134                 debug_info->AddParameter(items->GetOrCreateStringItem(v.name));
1135             }
1136         }
1137     } else {
1138         auto nparams = method->GetParams().size();
1139         for (size_t i = 0; i < nparams; i++) {
1140             debug_info->AddParameter(nullptr);
1141         }
1142     }
1143     method->SetDebugInfo(debug_info);
1144 }
1145 
1146 /* static */
AddMethodAndParamsAnnotations(ItemContainer * items,const Program & program,const AsmEmitter::AsmEntityCollections & entities,MethodItem * method,const Function & func)1147 bool AsmEmitter::AddMethodAndParamsAnnotations(ItemContainer *items, const Program &program,
1148                                                const AsmEmitter::AsmEntityCollections &entities, MethodItem *method,
1149                                                const Function &func)
1150 {
1151     if (!AddAnnotations(method, items, *func.metadata, program, entities)) {
1152         SetLastError("Cannot emit annotations for function " + func.name + ": " + GetLastError());
1153         return false;
1154     }
1155 
1156     auto &param_items = method->GetParams();
1157     for (size_t proto_idx = 0; proto_idx < param_items.size(); proto_idx++) {
1158         size_t param_idx = method->IsStatic() ? proto_idx : proto_idx + 1;
1159         auto &param = func.params[param_idx];
1160         auto &param_item = param_items[proto_idx];
1161         if (!AddAnnotations(&param_item, items, *param.metadata, program, entities)) {
1162             SetLastError("Cannot emit annotations for parameter a" + std::to_string(param_idx) + "of function " +
1163                          func.name + ": " + GetLastError());
1164             return false;
1165         }
1166     }
1167 
1168     if (method->HasRuntimeParamAnnotations()) {
1169         items->CreateItem<ParamAnnotationsItem>(method, true);
1170     }
1171 
1172     if (method->HasParamAnnotations()) {
1173         items->CreateItem<ParamAnnotationsItem>(method, false);
1174     }
1175 
1176     return true;
1177 }
1178 
1179 /* static */
MakeFunctionDebugInfoAndAnnotations(ItemContainer * items,const Program & program,const AsmEmitter::AsmEntityCollections & entities,bool emit_debug_info)1180 bool AsmEmitter::MakeFunctionDebugInfoAndAnnotations(ItemContainer *items, const Program &program,
1181                                                      const AsmEmitter::AsmEntityCollections &entities,
1182                                                      bool emit_debug_info)
1183 {
1184     for (const auto &[name, func] : program.function_table) {
1185         if (func.metadata->IsForeign()) {
1186             continue;
1187         }
1188 
1189         auto *method = static_cast<MethodItem *>(Find(entities.method_items, name));
1190 
1191         SetCodeAndDebugInfo(items, method, func, emit_debug_info);
1192         if (!AddBytecodeIndexDependencies(method, func, entities)) {
1193             return false;
1194         }
1195 
1196         method->SetSourceLang(func.language);
1197 
1198         if (!AddMethodAndParamsAnnotations(items, program, entities, method, func)) {
1199             return false;
1200         }
1201     }
1202     return true;
1203 }
1204 
1205 /* static */
FillMap(PandaFileToPandaAsmMaps * maps,AsmEmitter::AsmEntityCollections & entities)1206 void AsmEmitter::FillMap(PandaFileToPandaAsmMaps *maps, AsmEmitter::AsmEntityCollections &entities)
1207 {
1208     for (const auto &[name, method] : entities.method_items) {
1209         maps->methods.insert({method->GetFileId().GetOffset(), std::string(name)});
1210     }
1211 
1212     for (const auto &[name, field] : entities.field_items) {
1213         maps->fields.insert({field->GetFileId().GetOffset(), std::string(name)});
1214     }
1215 
1216     for (const auto &[name, cls] : entities.class_items) {
1217         maps->classes.insert({cls->GetFileId().GetOffset(), std::string(name)});
1218     }
1219 
1220     for (const auto &[name, str] : entities.string_items) {
1221         maps->strings.insert({str->GetFileId().GetOffset(), std::string(name)});
1222     }
1223 
1224     for (const auto &[name, arr] : entities.literalarray_items) {
1225         maps->literalarrays.emplace(arr->GetFileId().GetOffset(), name);
1226     }
1227 }
1228 
1229 /* static */
EmitDebugInfo(ItemContainer * items,const Program & program,const std::vector<uint8_t> * bytes,const MethodItem * method,const Function & func,const std::string & name,bool emit_debug_info)1230 void AsmEmitter::EmitDebugInfo(ItemContainer *items, const Program &program, const std::vector<uint8_t> *bytes,
1231                                const MethodItem *method, const Function &func, const std::string &name,
1232                                bool emit_debug_info)
1233 {
1234     auto *debug_info = method->GetDebugInfo();
1235     if (debug_info == nullptr) {
1236         return;
1237     }
1238 
1239     auto *line_number_program = debug_info->GetLineNumberProgram();
1240     auto *constant_pool = debug_info->GetConstantPool();
1241 
1242     std::string record_name = GetOwnerName(name);
1243     std::string record_source_file;
1244     if (!record_name.empty()) {
1245         auto &rec = program.record_table.find(record_name)->second;
1246         record_source_file = rec.source_file;
1247     }
1248 
1249     if (!func.source_file.empty() && func.source_file != record_source_file) {
1250         if (!func.source_code.empty()) {
1251             auto *source_code_item = items->GetOrCreateStringItem(func.source_code);
1252             ASSERT(source_code_item->GetOffset() != 0);
1253             line_number_program->EmitSetSourceCode(constant_pool, source_code_item);
1254         }
1255         auto *source_file_item = items->GetOrCreateStringItem(func.source_file);
1256         ASSERT(source_file_item->GetOffset() != 0);
1257         line_number_program->EmitSetFile(constant_pool, source_file_item);
1258     }
1259     func.BuildLineNumberProgram(debug_info, *bytes, items, constant_pool, emit_debug_info);
1260 }
1261 
1262 /* static */
EmitFunctions(ItemContainer * items,const Program & program,const AsmEmitter::AsmEntityCollections & entities,bool emit_debug_info)1263 bool AsmEmitter::EmitFunctions(ItemContainer *items, const Program &program,
1264                                const AsmEmitter::AsmEntityCollections &entities, bool emit_debug_info)
1265 {
1266     for (const auto &f : program.function_table) {
1267         const auto &[name, func] = f;
1268 
1269         if (func.metadata->IsForeign()) {
1270             continue;
1271         }
1272 
1273         auto emitter = BytecodeEmitter {};
1274         auto *method = static_cast<MethodItem *>(Find(entities.method_items, name));
1275         if (!func.Emit(emitter, method, entities.method_items, entities.field_items, entities.class_items,
1276                        entities.string_items, entities.literalarray_items)) {
1277             SetLastError("Internal error during emitting function: " + func.name);
1278             return false;
1279         }
1280 
1281         auto *code = method->GetCode();
1282         code->SetNumVregs(func.regs_num);
1283         code->SetNumArgs(func.GetParamsNum());
1284 
1285         auto num_ins = static_cast<size_t>(
1286             std::count_if(func.ins.begin(), func.ins.end(), [](auto it) { return it.opcode != Opcode::INVALID; }));
1287         code->SetNumInstructions(num_ins);
1288 
1289         auto *bytes = code->GetInstructions();
1290         auto status = emitter.Build(static_cast<std::vector<unsigned char> *>(bytes));
1291         if (status != BytecodeEmitter::ErrorCode::SUCCESS) {
1292             SetLastError("Internal error during emitting binary code, status=" +
1293                          std::to_string(static_cast<int>(status)));
1294             return false;
1295         }
1296         auto try_blocks = func.BuildTryBlocks(method, entities.class_items, *bytes);
1297         for (auto &try_block : try_blocks) {
1298             code->AddTryBlock(try_block);
1299         }
1300 
1301         EmitDebugInfo(items, program, bytes, method, func, name, emit_debug_info);
1302     }
1303     return true;
1304 }
1305 
1306 /* static */
MakeItemsForSingleProgram(ItemContainer * items,const Program & program,bool emit_debug_info,AsmEmitter::AsmEntityCollections & entities,std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > primitive_types)1307 bool AsmEmitter::MakeItemsForSingleProgram(ItemContainer *items, const Program &program, bool emit_debug_info,
1308     AsmEmitter::AsmEntityCollections &entities,
1309     std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> primitive_types)
1310 {
1311     MakeStringItems(items, program, entities);
1312     MakeArrayTypeItems(items, program, entities);
1313     if (!MakeRecordItems(items, program, entities, primitive_types)) {
1314         return false;
1315     }
1316     if (!MakeFunctionItems(items, program, entities, primitive_types, emit_debug_info)) {
1317         return false;
1318     }
1319     MakeLiteralItems(items, program, entities);
1320     // Add annotations for records and fields
1321     if (!MakeRecordAnnotations(items, program, entities)) {
1322         return false;
1323     }
1324     return true;
1325 }
1326 
1327 //  temp plan for ic slot number, should be deleted after method refactoring
MakeSlotNumberRecord(Program * prog)1328 static void MakeSlotNumberRecord(Program *prog)
1329 {
1330     static const std::string SLOT_NUMBER = "_ESSlotNumberAnnotation";
1331     pandasm::Record record(SLOT_NUMBER, pandasm::extensions::Language::ECMASCRIPT);
1332     record.metadata->SetAccessFlags(panda::ACC_ANNOTATION);
1333     prog->record_table.emplace(SLOT_NUMBER, std::move(record));
1334 }
1335 
1336 //  temp plan for ic slot number, should be deleted after method refactoring
MakeSlotNumberAnnotation(Program * prog)1337 static void MakeSlotNumberAnnotation(Program *prog)
1338 {
1339     static const std::string SLOT_NUMBER = "_ESSlotNumberAnnotation";
1340     static const std::string ELEMENT_NAME = "SlotNumber";
1341     for (auto &[name, func] : prog->function_table) {
1342         for (const auto &an : func.metadata->GetAnnotations()) {
1343             if (an.GetName() == SLOT_NUMBER) {
1344                 return;
1345             }
1346         }
1347         pandasm::AnnotationData anno(SLOT_NUMBER);
1348         pandasm::AnnotationElement ele(ELEMENT_NAME, std::make_unique<pandasm::ScalarValue>(
1349             pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(static_cast<uint32_t>(func.GetSlotsNum()))));
1350         anno.AddElement(std::move(ele));
1351         std::vector<pandasm::AnnotationData> annos;
1352         annos.emplace_back(anno);
1353         func.metadata->AddAnnotations(annos);
1354     }
1355 }
1356 
MakeConcurrentModuleRequestsRecord(Program * prog)1357 static void MakeConcurrentModuleRequestsRecord(Program *prog)
1358 {
1359     static const std::string CONCURRENT_MODULE_REQUESTS = "_ESConcurrentModuleRequestsAnnotation";
1360     pandasm::Record record(CONCURRENT_MODULE_REQUESTS, pandasm::extensions::Language::ECMASCRIPT);
1361     record.metadata->SetAccessFlags(panda::ACC_ANNOTATION);
1362     prog->record_table.emplace(CONCURRENT_MODULE_REQUESTS, std::move(record));
1363 }
1364 
MakeConcurrentModuleRequestsAnnotation(Program * prog)1365 static void MakeConcurrentModuleRequestsAnnotation(Program *prog)
1366 {
1367     static const std::string CONCURRENT_MODULE_REQUESTS = "_ESConcurrentModuleRequestsAnnotation";
1368     static const std::string ELEMENT_NAME = "ConcurrentModuleRequest";
1369     for (auto &[name, func] : prog->function_table) {
1370         if (func.GetFunctionKind() != panda::panda_file::FunctionKind::CONCURRENT_FUNCTION) {
1371             continue;
1372         }
1373 
1374         for (const auto &an : func.metadata->GetAnnotations()) {
1375             if (an.GetName() == CONCURRENT_MODULE_REQUESTS) {
1376                 return;
1377             }
1378         }
1379 
1380         pandasm::AnnotationData anno(CONCURRENT_MODULE_REQUESTS);
1381         for (auto &it : func.concurrent_module_requests) {
1382             panda::pandasm::AnnotationElement module_request(ELEMENT_NAME, std::make_unique<pandasm::ScalarValue>(
1383                 pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(static_cast<uint32_t>(it))));
1384             anno.AddElement(std::move(module_request));
1385         }
1386 
1387         std::vector<pandasm::AnnotationData> annos;
1388         annos.emplace_back(anno);
1389         func.metadata->AddAnnotations(annos);
1390     }
1391 }
1392 
EmitPrograms(const std::string & filename,const std::vector<Program * > & progs,bool emit_debug_info,uint8_t api)1393 bool AsmEmitter::EmitPrograms(const std::string &filename, const std::vector<Program *> &progs, bool emit_debug_info,
1394                               uint8_t api)
1395 {
1396     ASSERT(!progs.empty());
1397     for (auto *prog : progs) {
1398         MakeSlotNumberRecord(prog);
1399         MakeSlotNumberAnnotation(prog);
1400         MakeConcurrentModuleRequestsRecord(prog);
1401         MakeConcurrentModuleRequestsAnnotation(prog);
1402     }
1403 
1404     auto items = ItemContainer {api};
1405     auto primitive_types = CreatePrimitiveTypes(&items);
1406     auto entities = AsmEmitter::AsmEntityCollections {};
1407     SetLastError("");
1408 
1409     for (const auto *prog : progs) {
1410         if (!MakeItemsForSingleProgram(&items, *prog, emit_debug_info, entities, primitive_types)) {
1411             return false;
1412         }
1413     }
1414 
1415     for (const auto *prog : progs) {
1416         if (!MakeFunctionDebugInfoAndAnnotations(&items, *prog, entities, emit_debug_info)) {
1417             return false;
1418         }
1419     }
1420 
1421     items.ComputeLayout();
1422 
1423     for (const auto *prog : progs) {
1424         if (!EmitFunctions(&items, *prog, entities, emit_debug_info)) {
1425             return false;
1426         }
1427     }
1428 
1429     auto writer = FileWriter(filename);
1430     if (!writer) {
1431         SetLastError("Unable to open" + filename + " for writing");
1432         return false;
1433     }
1434 
1435     return items.Write(&writer);
1436 }
1437 
1438 /* static */
Emit(ItemContainer * items,const Program & program,PandaFileToPandaAsmMaps * maps,bool emit_debug_info,panda::panda_file::pgo::ProfileOptimizer * profile_opt)1439 bool AsmEmitter::Emit(ItemContainer *items, const Program &program, PandaFileToPandaAsmMaps *maps, bool emit_debug_info,
1440                       panda::panda_file::pgo::ProfileOptimizer *profile_opt)
1441 {
1442     MakeSlotNumberRecord(const_cast<Program *>(&program));
1443     MakeSlotNumberAnnotation(const_cast<Program *>(&program));
1444     auto primitive_types = CreatePrimitiveTypes(items);
1445 
1446     auto entities = AsmEmitter::AsmEntityCollections {};
1447 
1448     SetLastError("");
1449 
1450     if (!MakeItemsForSingleProgram(items, program, emit_debug_info, entities, primitive_types)) {
1451         return false;
1452     }
1453 
1454     // Add Code and DebugInfo items last due to they have variable size that depends on bytecode
1455     if (!MakeFunctionDebugInfoAndAnnotations(items, program, entities, emit_debug_info)) {
1456         return false;
1457     }
1458 
1459     if (profile_opt != nullptr) {
1460         items->ReorderItems(profile_opt);
1461     }
1462 
1463     items->ComputeLayout();
1464 
1465     if (maps != nullptr) {
1466         FillMap(maps, entities);
1467     }
1468 
1469     if (!EmitFunctions(items, program, entities, emit_debug_info)) {
1470         return false;
1471     }
1472 
1473     return true;
1474 }
1475 
Emit(Writer * writer,const Program & program,std::map<std::string,size_t> * stat,PandaFileToPandaAsmMaps * maps,bool debug_info,panda::panda_file::pgo::ProfileOptimizer * profile_opt,uint8_t api)1476 bool AsmEmitter::Emit(Writer *writer, const Program &program, std::map<std::string, size_t> *stat,
1477                       PandaFileToPandaAsmMaps *maps, bool debug_info,
1478                       panda::panda_file::pgo::ProfileOptimizer *profile_opt, uint8_t api)
1479 {
1480     auto items = ItemContainer {api};
1481     if (!Emit(&items, program, maps, debug_info, profile_opt)) {
1482         return false;
1483     }
1484 
1485     if (stat != nullptr) {
1486         *stat = items.GetStat();
1487     }
1488 
1489     return items.Write(writer);
1490 }
1491 
Emit(const std::string & filename,const Program & program,std::map<std::string,size_t> * stat,PandaFileToPandaAsmMaps * maps,bool debug_info,panda::panda_file::pgo::ProfileOptimizer * profile_opt,uint8_t api)1492 bool AsmEmitter::Emit(const std::string &filename, const Program &program, std::map<std::string, size_t> *stat,
1493                       PandaFileToPandaAsmMaps *maps, bool debug_info,
1494                       panda::panda_file::pgo::ProfileOptimizer *profile_opt, uint8_t api)
1495 {
1496     auto writer = FileWriter(filename);
1497     if (!writer) {
1498         SetLastError("Unable to open" + filename + " for writing");
1499         return false;
1500     }
1501     return Emit(&writer, program, stat, maps, debug_info, profile_opt, api);
1502 }
1503 
Emit(const Program & program,PandaFileToPandaAsmMaps * maps,uint8_t api)1504 std::unique_ptr<const panda_file::File> AsmEmitter::Emit(const Program &program, PandaFileToPandaAsmMaps *maps,
1505                                                          uint8_t api)
1506 {
1507     auto items = ItemContainer {api};
1508     if (!Emit(&items, program, maps)) {
1509         return nullptr;
1510     }
1511 
1512     size_t size = items.ComputeLayout();
1513     auto *buffer = new std::byte[size];
1514 
1515     auto writer = MemoryBufferWriter(reinterpret_cast<uint8_t *>(buffer), size);
1516     if (!items.Write(&writer)) {
1517         return nullptr;
1518     }
1519 
1520     os::mem::ConstBytePtr ptr(
1521         buffer, size, [](std::byte *buffer_ptr, [[maybe_unused]] size_t param_size) noexcept { delete[] buffer_ptr; });
1522     return panda_file::File::OpenFromMemory(std::move(ptr));
1523 }
1524 
GetTypeItem(ItemContainer * items,const std::unordered_map<panda_file::Type::TypeId,PrimitiveTypeItem * > & primitive_types,const Type & type,const Program & program)1525 TypeItem *AsmEmitter::GetTypeItem(
1526     ItemContainer *items, const std::unordered_map<panda_file::Type::TypeId, PrimitiveTypeItem *> &primitive_types,
1527     const Type &type, const Program &program)
1528 {
1529     if (!type.IsObject()) {
1530         return Find(primitive_types, type.GetId());
1531     }
1532 
1533     if (type.IsArray()) {
1534         return items->GetOrCreateForeignClassItem(type.GetDescriptor());
1535     }
1536 
1537     const auto &name = type.GetName();
1538     auto iter = program.record_table.find(name);
1539     if (iter == program.record_table.end()) {
1540         return nullptr;
1541     }
1542 
1543     auto &rec = iter->second;
1544 
1545     if (rec.metadata->IsForeign()) {
1546         return items->GetOrCreateForeignClassItem(type.GetDescriptor());
1547     }
1548 
1549     return items->GetOrCreateClassItem(type.GetDescriptor());
1550 }
1551 
Emit(BytecodeEmitter & emitter,panda_file::MethodItem * method,const std::unordered_map<std::string,panda_file::BaseMethodItem * > & methods,const std::unordered_map<std::string,panda_file::BaseFieldItem * > & fields,const std::unordered_map<std::string,panda_file::BaseClassItem * > & classes,const std::unordered_map<std::string,panda_file::StringItem * > & strings,const std::unordered_map<std::string,panda_file::LiteralArrayItem * > & literalarrays) const1552 bool Function::Emit(BytecodeEmitter &emitter, panda_file::MethodItem *method,
1553                     const std::unordered_map<std::string, panda_file::BaseMethodItem *> &methods,
1554                     const std::unordered_map<std::string, panda_file::BaseFieldItem *> &fields,
1555                     const std::unordered_map<std::string, panda_file::BaseClassItem *> &classes,
1556                     const std::unordered_map<std::string, panda_file::StringItem *> &strings,
1557                     const std::unordered_map<std::string, panda_file::LiteralArrayItem *> &literalarrays) const
1558 {
1559     auto labels = std::unordered_map<std::string_view, panda::Label> {};
1560 
1561     for (const auto &insn : ins) {
1562         if (insn.set_label) {
1563             labels.insert_or_assign(insn.label, emitter.CreateLabel());
1564         }
1565     }
1566 
1567     for (const auto &insn : ins) {
1568         if (insn.set_label) {
1569             auto search = labels.find(insn.label);
1570             ASSERT(search != labels.end());
1571             emitter.Bind(search->second);
1572         }
1573 
1574         if (insn.opcode != Opcode::INVALID) {
1575             if (!insn.Emit(emitter, method, methods, fields, classes, strings, literalarrays, labels)) {
1576                 return false;
1577             }
1578         }
1579     }
1580 
1581     return true;
1582 }
1583 
TryEmitPc(panda_file::LineNumberProgramItem * program,std::vector<uint8_t> * constant_pool,uint32_t & pc_inc)1584 static void TryEmitPc(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
1585                       uint32_t &pc_inc)
1586 {
1587     if (pc_inc) {
1588         program->EmitAdvancePc(constant_pool, pc_inc);
1589         pc_inc = 0;
1590     }
1591 }
1592 
EmitLocalVariable(panda_file::LineNumberProgramItem * program,ItemContainer * container,std::vector<uint8_t> * constant_pool,uint32_t & pc_inc,size_t instruction_number,size_t variable_index) const1593 void Function::EmitLocalVariable(panda_file::LineNumberProgramItem *program, ItemContainer *container,
1594                                  std::vector<uint8_t> *constant_pool, uint32_t &pc_inc, size_t instruction_number,
1595                                  size_t variable_index) const
1596 {
1597     ASSERT(variable_index < local_variable_debug.size());
1598     const auto &v = local_variable_debug[variable_index];
1599     ASSERT(!IsParameter(v.reg));
1600     if (instruction_number == v.start) {
1601         TryEmitPc(program, constant_pool, pc_inc);
1602         StringItem *variable_name = container->GetOrCreateStringItem(v.name);
1603         StringItem *variable_type = container->GetOrCreateStringItem(v.signature);
1604         if (v.signature_type.empty()) {
1605             program->EmitStartLocal(constant_pool, v.reg, variable_name, variable_type);
1606         } else {
1607             StringItem *type_signature = container->GetOrCreateStringItem(v.signature_type);
1608             program->EmitStartLocalExtended(constant_pool, v.reg, variable_name, variable_type, type_signature);
1609         }
1610     }
1611 
1612     if (instruction_number == (v.start + v.length)) {
1613         TryEmitPc(program, constant_pool, pc_inc);
1614         program->EmitEndLocal(v.reg);
1615     }
1616 }
1617 
GetLineNumber(size_t i) const1618 size_t Function::GetLineNumber(size_t i) const
1619 {
1620     return ins[i].ins_debug.line_number;
1621 }
1622 
GetColumnNumber(size_t i) const1623 uint32_t Function::GetColumnNumber(size_t i) const
1624 {
1625     return ins[i].ins_debug.column_number;
1626 }
1627 
EmitNumber(panda_file::LineNumberProgramItem * program,std::vector<uint8_t> * constant_pool,uint32_t pc_inc,int32_t line_inc) const1628 void Function::EmitNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
1629                           uint32_t pc_inc, int32_t line_inc) const
1630 {
1631     if (!program->EmitSpecialOpcode(pc_inc, line_inc)) {
1632         if (pc_inc) {
1633             program->EmitAdvancePc(constant_pool, pc_inc);
1634             if (!program->EmitSpecialOpcode(0, line_inc)) {
1635                 program->EmitAdvanceLine(constant_pool, line_inc);
1636                 program->EmitSpecialOpcode(0, 0);
1637             }
1638         } else {
1639             program->EmitAdvanceLine(constant_pool, line_inc);
1640             program->EmitSpecialOpcode(0, 0);
1641         }
1642     }
1643 }
1644 
EmitLineNumber(panda_file::LineNumberProgramItem * program,std::vector<uint8_t> * constant_pool,int32_t & prev_line_number,uint32_t & pc_inc,size_t instruction_number) const1645 void Function::EmitLineNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
1646                               int32_t &prev_line_number, uint32_t &pc_inc, size_t instruction_number) const
1647 {
1648     int32_t line_inc = static_cast<int32_t>(GetLineNumber(instruction_number)) - prev_line_number;
1649     if (line_inc) {
1650         prev_line_number = static_cast<int32_t>(GetLineNumber(instruction_number));
1651         EmitNumber(program, constant_pool, pc_inc, line_inc);
1652         pc_inc = 0;
1653     }
1654 }
1655 
EmitColumnNumber(panda_file::LineNumberProgramItem * program,std::vector<uint8_t> * constant_pool,uint32_t & prev_column_number,uint32_t & pc_inc,size_t instruction_number) const1656 void Function::EmitColumnNumber(panda_file::LineNumberProgramItem *program, std::vector<uint8_t> *constant_pool,
1657                                 uint32_t &prev_column_number, uint32_t &pc_inc, size_t instruction_number) const
1658 {
1659     auto cn = GetColumnNumber(instruction_number);
1660     if (cn != prev_column_number) {
1661         program->EmitColumn(constant_pool, pc_inc, cn);
1662         pc_inc = 0;
1663         prev_column_number = cn;
1664     }
1665 }
1666 
CollectLocalVariable(std::vector<Function::LocalVariablePair> & local_variable_info) const1667 void Function::CollectLocalVariable(std::vector<Function::LocalVariablePair> &local_variable_info) const
1668 {
1669     for (size_t i = 0; i < local_variable_debug.size(); i++) {
1670         const auto &v = local_variable_debug[i];
1671         if (IsParameter(v.reg)) {
1672             continue;
1673         }
1674         local_variable_info.emplace_back(v.start, i);
1675         local_variable_info.emplace_back(v.start + v.length, i);
1676     }
1677 
1678     std::sort(local_variable_info.begin(), local_variable_info.end(),
1679         [&](const Function::LocalVariablePair &a, const Function::LocalVariablePair &b) {
1680         return a.insn_order < b.insn_order;
1681     });
1682 }
1683 
BuildLineNumberProgram(panda_file::DebugInfoItem * debug_item,const std::vector<uint8_t> & bytecode,ItemContainer * container,std::vector<uint8_t> * constant_pool,bool emit_debug_info) const1684 void Function::BuildLineNumberProgram(panda_file::DebugInfoItem *debug_item, const std::vector<uint8_t> &bytecode,
1685                                       ItemContainer *container, std::vector<uint8_t> *constant_pool,
1686                                       bool emit_debug_info) const
1687 {
1688     auto *program = debug_item->GetLineNumberProgram();
1689 
1690     if (ins.empty()) {
1691         program->EmitEnd();
1692         return;
1693     }
1694 
1695     uint32_t pc_inc = 0;
1696     auto prev_line_number = static_cast<int32_t>(GetLineNumber(0));
1697     uint32_t prev_column_number = std::numeric_limits<uint32_t>::max();
1698     BytecodeInstruction bi(bytecode.data());
1699     debug_item->SetLineNumber(static_cast<uint32_t>(prev_line_number));
1700 
1701     std::vector<Function::LocalVariablePair> local_variable_info;
1702     if (emit_debug_info) {
1703         CollectLocalVariable(local_variable_info);
1704     }
1705     const size_t num_ins = ins.size();
1706     size_t start = 0;
1707     auto iter = local_variable_info.begin();
1708     do {
1709         size_t end = emit_debug_info && iter != local_variable_info.end() ? iter->insn_order : num_ins;
1710         for (size_t i = start; i < end; i++) {
1711             if (ins[i].opcode == Opcode::INVALID) {
1712                 continue;
1713             }
1714             if (emit_debug_info || ins[i].CanThrow()) {
1715                 EmitLineNumber(program, constant_pool, prev_line_number, pc_inc, i);
1716             }
1717             if (emit_debug_info) {
1718                 EmitColumnNumber(program, constant_pool, prev_column_number, pc_inc, i);
1719             }
1720             pc_inc += bi.GetSize();
1721             bi = bi.GetNext();
1722         }
1723         if (iter == local_variable_info.end()) {
1724             break;
1725         }
1726         if (emit_debug_info) {
1727             EmitLocalVariable(program, container, constant_pool, pc_inc, end, iter->variable_index);
1728         }
1729         start = end;
1730         iter++;
1731     } while (true);
1732 
1733     program->EmitEnd();
1734 }
1735 
MakeOrderAndOffsets(const std::vector<uint8_t> & bytecode) const1736 Function::TryCatchInfo Function::MakeOrderAndOffsets(const std::vector<uint8_t> &bytecode) const
1737 {
1738     std::unordered_map<std::string_view, size_t> try_catch_labels;
1739     std::unordered_map<std::string, std::vector<const CatchBlock *>> try_catch_map;
1740     std::vector<std::string> try_catch_order;
1741 
1742     for (auto &catch_block : catch_blocks) {
1743         try_catch_labels.insert_or_assign(catch_block.try_begin_label, 0);
1744         try_catch_labels.insert_or_assign(catch_block.try_end_label, 0);
1745         try_catch_labels.insert_or_assign(catch_block.catch_begin_label, 0);
1746         try_catch_labels.insert_or_assign(catch_block.catch_end_label, 0);
1747 
1748         std::string try_key = catch_block.try_begin_label + ":" + catch_block.try_end_label;
1749         auto it = try_catch_map.find(try_key);
1750         if (it == try_catch_map.cend()) {
1751             std::tie(it, std::ignore) = try_catch_map.try_emplace(try_key);
1752             try_catch_order.push_back(try_key);
1753         }
1754         it->second.push_back(&catch_block);
1755     }
1756 
1757     BytecodeInstruction bi(bytecode.data());
1758     size_t pc_offset = 0;
1759 
1760     for (size_t i = 0; i < ins.size(); i++) {
1761         if (ins[i].set_label) {
1762             auto it = try_catch_labels.find(ins[i].label);
1763             if (it != try_catch_labels.cend()) {
1764                 try_catch_labels[ins[i].label] = pc_offset;
1765             }
1766         }
1767         if (ins[i].opcode == Opcode::INVALID) {
1768             continue;
1769         }
1770 
1771         pc_offset += bi.GetSize();
1772         bi = bi.GetNext();
1773     }
1774 
1775     return Function::TryCatchInfo {try_catch_labels, try_catch_map, try_catch_order};
1776 }
1777 
BuildTryBlocks(MethodItem * method,const std::unordered_map<std::string,BaseClassItem * > & class_items,const std::vector<uint8_t> & bytecode) const1778 std::vector<CodeItem::TryBlock> Function::BuildTryBlocks(
1779     MethodItem *method, const std::unordered_map<std::string, BaseClassItem *> &class_items,
1780     const std::vector<uint8_t> &bytecode) const
1781 {
1782     std::vector<CodeItem::TryBlock> try_blocks;
1783 
1784     if (ins.empty()) {
1785         return try_blocks;
1786     }
1787 
1788     Function::TryCatchInfo tcs = MakeOrderAndOffsets(bytecode);
1789 
1790     for (const auto &t_key : tcs.try_catch_order) {
1791         auto kv = tcs.try_catch_map.find(t_key);
1792         ASSERT(kv != tcs.try_catch_map.cend());
1793         auto &try_catch_blocks = kv->second;
1794 
1795         ASSERT(!try_catch_blocks.empty());
1796 
1797         std::vector<CodeItem::CatchBlock> catch_block_items;
1798 
1799         for (auto *catch_block : try_catch_blocks) {
1800             auto class_name = catch_block->exception_record;
1801 
1802             BaseClassItem *class_item = nullptr;
1803             if (!class_name.empty()) {
1804                 auto it = class_items.find(class_name);
1805                 ASSERT(it != class_items.cend());
1806                 class_item = it->second;
1807             }
1808 
1809             auto handler_pc_offset = tcs.try_catch_labels[catch_block->catch_begin_label];
1810             auto handler_code_size = tcs.try_catch_labels[catch_block->catch_end_label] - handler_pc_offset;
1811             catch_block_items.emplace_back(method, class_item, handler_pc_offset, handler_code_size);
1812         }
1813 
1814         auto try_start_pc_offset = tcs.try_catch_labels[try_catch_blocks[0]->try_begin_label];
1815         auto try_end_pc_offset = tcs.try_catch_labels[try_catch_blocks[0]->try_end_label];
1816         ASSERT(try_end_pc_offset >= try_start_pc_offset);
1817         try_blocks.emplace_back(try_start_pc_offset, try_end_pc_offset - try_start_pc_offset, catch_block_items);
1818     }
1819 
1820     return try_blocks;
1821 }
1822 
DebugDump() const1823 void Function::DebugDump() const
1824 {
1825     std::cerr << "name: " << name << std::endl;
1826     for (const auto &i : ins) {
1827         std::cerr << i.ToString("\n", true, regs_num);
1828     }
1829 }
1830 
GetOwnerName(std::string name)1831 std::string GetOwnerName(std::string name)
1832 {
1833     name = DeMangleName(name);
1834     auto super_pos = name.find_last_of(PARSE_AREA_MARKER);
1835     if (super_pos == std::string::npos) {
1836         return "";
1837     }
1838 
1839     return name.substr(0, super_pos);
1840 }
1841 
GetItemName(std::string name)1842 std::string GetItemName(std::string name)
1843 {
1844     name = DeMangleName(name);
1845     auto super_pos = name.find_last_of(PARSE_AREA_MARKER);
1846     if (super_pos == std::string::npos) {
1847         return name;
1848     }
1849 
1850     return name.substr(super_pos + 1);
1851 }
1852 
1853 }  // namespace panda::pandasm
1854