1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "abc_literal_array_processor.h"
17
18 namespace panda::abc2program {
19
AbcLiteralArrayProcessor(panda_file::File::EntityId entity_id,Abc2ProgramEntityContainer & entity_container,panda_file::LiteralDataAccessor & literal_data_accessor)20 AbcLiteralArrayProcessor::AbcLiteralArrayProcessor(panda_file::File::EntityId entity_id,
21 Abc2ProgramEntityContainer &entity_container,
22 panda_file::LiteralDataAccessor &literal_data_accessor)
23 : AbcFileEntityProcessor(entity_id, entity_container), literal_data_accessor_(literal_data_accessor) {}
24
FillProgramData()25 void AbcLiteralArrayProcessor::FillProgramData()
26 {
27 GetLiteralArrayById(&literal_array_, entity_id_);
28 program_->literalarray_table.emplace(entity_container_.GetLiteralArrayIdName(entity_id_.GetOffset()),
29 std::move(literal_array_));
30 }
31
FillModuleRequestPhase()32 void AbcLiteralArrayProcessor::FillModuleRequestPhase()
33 {
34 auto sp = file_->GetSpanFromId(entity_id_);
35 auto literal_vals_num = panda_file::helpers::Read<sizeof(uint32_t)>(&sp);
36 for (size_t i = 0; i < literal_vals_num; i++) {
37 pandasm::LiteralArray::Literal lit;
38 lit.tag_ = panda_file::LiteralTag::INTEGER_8;
39 lit.value_ = static_cast<uint8_t>(panda_file::helpers::Read<sizeof(uint8_t)>(&sp));
40 literal_array_.literals_.emplace_back(lit);
41 }
42 program_->literalarray_table.emplace(entity_container_.GetLiteralArrayIdName(entity_id_.GetOffset()),
43 std::move(literal_array_));
44 }
45
GetLiteralArrayById(pandasm::LiteralArray * lit_array,panda_file::File::EntityId lit_array_id) const46 void AbcLiteralArrayProcessor::GetLiteralArrayById(pandasm::LiteralArray *lit_array,
47 panda_file::File::EntityId lit_array_id) const
48 {
49 literal_data_accessor_.EnumerateLiteralVals(
50 lit_array_id, [this, lit_array](
51 const panda_file::LiteralDataAccessor::LiteralValue &value,
52 const panda_file::LiteralTag &tag) {
53 FillLiteralData(lit_array, value, tag);
54 });
55 }
56
FillLiteralData(pandasm::LiteralArray * lit_array,const panda_file::LiteralDataAccessor::LiteralValue & value,const panda_file::LiteralTag & tag) const57 void AbcLiteralArrayProcessor::FillLiteralData(pandasm::LiteralArray *lit_array,
58 const panda_file::LiteralDataAccessor::LiteralValue &value,
59 const panda_file::LiteralTag &tag) const
60 {
61 pandasm::LiteralArray::Literal value_lit;
62 pandasm::LiteralArray::Literal tag_lit;
63 value_lit.tag_ = tag;
64 switch (tag) {
65 case panda_file::LiteralTag::BOOL:
66 value_lit.value_ = std::get<bool>(value);
67 break;
68 case panda_file::LiteralTag::ACCESSOR:
69 case panda_file::LiteralTag::NULLVALUE:
70 case panda_file::LiteralTag::BUILTINTYPEINDEX:
71 value_lit.value_ = std::get<uint8_t>(value);
72 break;
73 case panda_file::LiteralTag::METHODAFFILIATE:
74 value_lit.value_ = std::get<uint16_t>(value);
75 break;
76 case panda_file::LiteralTag::LITERALBUFFERINDEX:
77 case panda_file::LiteralTag::INTEGER:
78 value_lit.value_ = std::get<uint32_t>(value);
79 break;
80 case panda_file::LiteralTag::DOUBLE:
81 value_lit.value_ = std::get<double>(value);
82 break;
83 case panda_file::LiteralTag::STRING:
84 value_lit.value_ = GetStringById(panda_file::File::EntityId{std::get<uint32_t>(value)});
85 break;
86 case panda_file::LiteralTag::METHOD:
87 case panda_file::LiteralTag::GETTER:
88 case panda_file::LiteralTag::SETTER:
89 case panda_file::LiteralTag::GENERATORMETHOD:
90 case panda_file::LiteralTag::ASYNCGENERATORMETHOD: {
91 panda_file::File::EntityId entity_id(std::get<uint32_t>(value));
92 value_lit.value_ = entity_container_.GetFullMethodNameById(entity_id);
93 break;
94 }
95 case panda_file::LiteralTag::LITERALARRAY:
96 value_lit.value_ = entity_container_.GetLiteralArrayIdName(std::get<uint32_t>(value));
97 entity_container_.TryAddUnprocessedNestedLiteralArrayId(std::get<uint32_t>(value));
98 break;
99 case panda_file::LiteralTag::TAGVALUE:
100 value_lit.value_ = std::get<uint8_t>(value);
101 break;
102 default:
103 UNREACHABLE();
104 }
105
106 tag_lit.tag_ = panda_file::LiteralTag::TAGVALUE;
107 tag_lit.value_ = static_cast<uint8_t>(value_lit.tag_);
108
109 lit_array->literals_.emplace_back(tag_lit);
110 lit_array->literals_.emplace_back(value_lit);
111 }
112
113 } // namespace panda::abc2program
114