-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmodulevar.cpp
More file actions
251 lines (194 loc) · 12.6 KB
/
modulevar.cpp
File metadata and controls
251 lines (194 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "modulevar.h"
std::string GetStringByOffset(std::unique_ptr<const panda_file::File>& file_, uint32_t offset)
{
const auto sd = file_->GetStringData(panda_file::File::EntityId(offset));
return std::string(utf::Mutf8AsCString(sd.data));
}
bool IsValidOffset(std::unique_ptr<const panda_file::File>& file_, uint32_t offset)
{
return panda_file::File::EntityId(offset).IsValid() && offset < file_->GetHeader()->file_size;
}
void AddImportAst(panda::es2panda::parser::Program *parser_program, std::string imported_name, std::string local_name, std::string module_name){
auto program_ast = parser_program->Ast();
auto program_statements = program_ast->Statements();
panda::es2panda::util::StringView imported_name_view = panda::es2panda::util::StringView(*new std::string(imported_name));
auto imported_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, imported_name_view);
panda::es2panda::util::StringView local_name_strview = panda::es2panda::util::StringView(*new std::string(local_name));
auto local_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, local_name_strview);
auto importspefic = AllocNode<panda::es2panda::ir::ImportSpecifier>(parser_program, imported_nameid, local_nameid, false, false);
ArenaVector<panda::es2panda::ir::AstNode *> specifiers(parser_program->Allocator()->Adapter());
specifiers.push_back(importspefic);
std::string* source_str_ptr = new std::string(module_name);
es2panda::util::StringView literal_strview(*source_str_ptr);
auto source = AllocNode<panda::es2panda::ir::StringLiteral>(parser_program, literal_strview);
auto *importDeclaration = AllocNode<panda::es2panda::ir::ImportDeclaration>(parser_program, source, std::move(specifiers), nullptr, false, false);
program_ast->AddStatementAtPos(program_statements.size(), importDeclaration);
}
void AddExportAstAll(panda::es2panda::parser::Program *parser_program, std::string module_name){
auto program_ast = parser_program->Ast();
auto program_statements = program_ast->Statements();
panda::es2panda::util::StringView local_name_strview = panda::es2panda::util::StringView(*new std::string("*"));
auto local_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, local_name_strview);
std::string* source_str_ptr = new std::string(module_name);
es2panda::util::StringView literal_strview(*source_str_ptr);
auto source = AllocNode<panda::es2panda::ir::StringLiteral>(parser_program, literal_strview);
auto *export_all = AllocNode<panda::es2panda::ir::ExportAllDeclaration>(parser_program, source, local_nameid, nullptr);
program_ast->AddStatementAtPos(program_statements.size(), export_all);
}
void AddExportAstNamed(panda::es2panda::parser::Program *parser_program, std::string import_name, std::string export_name, std::string module_name){
auto program_ast = parser_program->Ast();
auto program_statements = program_ast->Statements();
panda::es2panda::util::StringView import_name_view = panda::es2panda::util::StringView(*new std::string(import_name));
auto import_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, import_name_view);
panda::es2panda::util::StringView export_name_strview = panda::es2panda::util::StringView(*new std::string(export_name));
auto export_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, export_name_strview);
auto exportspefic = AllocNode<panda::es2panda::ir::ExportSpecifier>(parser_program, import_nameid, export_nameid, false);
ArenaVector<panda::es2panda::ir::ExportSpecifier *> specifiers(parser_program->Allocator()->Adapter());
specifiers.push_back(exportspefic);
std::string* source_str_ptr = new std::string(module_name);
es2panda::util::StringView literal_strview(*source_str_ptr);
auto source = AllocNode<panda::es2panda::ir::StringLiteral>(parser_program, literal_strview);
auto *exportDeclaration = AllocNode<panda::es2panda::ir::ExportNamedDeclaration>(parser_program, source, std::move(specifiers), nullptr, false);
program_ast->AddStatementAtPos(program_statements.size(), exportDeclaration);
}
void AddExportAst(panda::es2panda::parser::Program *parser_program, std::string local_name, std::string export_name){
auto program_ast = parser_program->Ast();
auto program_statements = program_ast->Statements();
panda::es2panda::util::StringView local_name_strview = panda::es2panda::util::StringView(*new std::string(local_name));
auto local_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, local_name_strview);
panda::es2panda::util::StringView export_name_strview = panda::es2panda::util::StringView(*new std::string(export_name));
auto export_nameid = AllocNode<panda::es2panda::ir::Identifier>(parser_program, export_name_strview);
auto *exportast = AllocNode<panda::es2panda::ir::ExportSpecifier >(parser_program, local_nameid, export_nameid, false);
program_ast->AddStatementAtPos(program_statements.size(), exportast);
}
void GetModuleLiteralArray(std::unique_ptr<const panda_file::File>& file_, panda_file::File::EntityId &module_id, panda::disasm::Disassembler& disasm,
panda::es2panda::parser::Program *parser_program, std::map<size_t, std::vector<std::string>> &index2namespaces,
std::vector<std::string>& localnamespaces)
{
// AddImportAst(parser_program, "a", "b", "./module_foo1"); // mock refs
std::map<size_t, std::string> index2importmodule;
std::map<std::string, size_t> importmodule2index;
std::map<size_t, std::map<std::string, std::string>> index2importmaps;
std::vector<std::map<std::string, std::string>> exportmaps_arrays;
panda_file::ModuleDataAccessor mda(*file_, module_id);
const std::vector<uint32_t> &request_modules_offset = mda.getRequestModules();
std::vector<std::string> module_literal_array;
for (size_t index = 0; index < request_modules_offset.size(); ++index) {
index2importmodule[index] = GetStringByOffset(file_, request_modules_offset[index]);
importmodule2index[GetStringByOffset(file_, request_modules_offset[index])] = index;
index2namespaces[index] = std::vector<std::string>();
}
mda.EnumerateModuleRecord([&](panda_file::ModuleTag tag, uint32_t export_name_offset, uint32_t request_module_idx,
uint32_t import_name_offset, uint32_t local_name_offset) {
std::map<std::string, std::string> curmaps;
std::stringstream ss;
if (tag == panda_file::ModuleTag::REGULAR_IMPORT ||
tag == panda_file::ModuleTag::NAMESPACE_IMPORT || tag == panda_file::ModuleTag::LOCAL_EXPORT) {
if (!disasm.IsValidOffset(local_name_offset)) {
LOG(ERROR, DISASSEMBLER) << "Get invalid local name offset!" << std::endl;
return;
}
curmaps["local_name"] = GetStringByOffset(file_, local_name_offset);
ss << ", local_name: " << GetStringByOffset(file_, local_name_offset);
}
if (tag == panda_file::ModuleTag::LOCAL_EXPORT || tag == panda_file::ModuleTag::INDIRECT_EXPORT) {
if (!IsValidOffset(file_, export_name_offset)) {
LOG(ERROR, DISASSEMBLER) << "Get invalid export name offset!" << std::endl;
return;
}
curmaps["export_name"] = GetStringByOffset(file_, export_name_offset);
ss << ", export_name: " << GetStringByOffset(file_, export_name_offset);
}
if (tag == panda_file::ModuleTag::REGULAR_IMPORT || tag == panda_file::ModuleTag::INDIRECT_EXPORT) {
if (!IsValidOffset(file_, import_name_offset)) {
LOG(ERROR, DISASSEMBLER) << "Get invalid import name offset!" << std::endl;
return;
}
curmaps["import_name"] = GetStringByOffset(file_, import_name_offset);
ss << ", import_name: " << GetStringByOffset(file_, import_name_offset);
}
if (tag != panda_file::ModuleTag::LOCAL_EXPORT) {
if (request_module_idx >= request_modules_offset.size() ||
!IsValidOffset(file_, request_modules_offset[request_module_idx])) {
LOG(ERROR, DISASSEMBLER) << "Get invalid request module offset!" << std::endl;
return;
}
curmaps["module_request"] = GetStringByOffset(file_, request_modules_offset[request_module_idx]);
ss << ", module_request: " << GetStringByOffset(file_, request_modules_offset[request_module_idx]);
}
if(tag == panda_file::ModuleTag::REGULAR_IMPORT || tag == panda_file::ModuleTag::NAMESPACE_IMPORT){
ss << ", IMPORT ";
auto module_request_name = curmaps["module_request"];
auto index = importmodule2index[module_request_name];
index2importmaps[index] = curmaps;
if(curmaps.count("import_name") > 0){
AddImportAst(parser_program, curmaps["import_name"], curmaps["local_name"], curmaps["module_request"]);
}else{
AddImportAst(parser_program, "*", curmaps["local_name"], curmaps["module_request"]);
}
index2namespaces[importmodule2index[curmaps["module_request"]]].push_back(curmaps["local_name"]);
localnamespaces.push_back(curmaps["local_name"]);
}else{
ss << ", EXPORT ";
exportmaps_arrays.push_back(curmaps);
// std::cout << curmaps.count("export_name") << " , " << curmaps.count("import_name") << " , " << curmaps.count("local_name") << " , " << curmaps.count("module_request") << std::endl;
if(curmaps.count("export_name") == 0 && curmaps.count("local_name") == 0){
// ExportAllDeclaration
AddExportAstAll(parser_program, curmaps["module_request"]);
}else if(curmaps.count("module_request") == 0){
// ExportSpecifier
AddExportAst(parser_program, curmaps["local_name"], curmaps["export_name"]);
localnamespaces.push_back(curmaps["local_name"]);
}else{
// ExportNamedDeclaration
AddExportAstNamed(parser_program, curmaps["import_name"], curmaps["export_name"], curmaps["module_request"]);
localnamespaces.push_back(curmaps["import_name"]);
}
}
std::cout << ss.str() << std::endl;;
});
}
void ParseModuleVars(std::unique_ptr<const panda_file::File>& file_, pandasm::Program *prog, panda::disasm::Disassembler& disasm,
panda::es2panda::parser::Program *parser_program, std::map<size_t, std::vector<std::string>>& index2namespaces,
std::vector<std::string>& localnamespaces){
if (panda_file::ContainsLiteralArrayInHeader(file_->GetHeader()->version)) {
const auto lit_arrays_id = file_->GetLiteralArraysId();
panda_file::LiteralDataAccessor lda(*file_, lit_arrays_id);
size_t num_litarrays = lda.GetLiteralNum();
for (size_t index = 0; index < num_litarrays; index++) {
auto id = lda.GetLiteralArrayId(index);
if (disasm.module_request_phase_literals_.count(id.GetOffset())) {
continue;
}
if (disasm.IsModuleLiteralOffset(id)) {
GetModuleLiteralArray(file_, id, disasm, parser_program, index2namespaces, localnamespaces);
}
}
}else{
// release mode
for (const auto &r : prog->record_table) {
auto& record = r.second;
for (const auto &f : record.field_list) {
if (!f.metadata->GetValue().has_value()) {
continue;
}
if (f.type.GetId() == panda_file::Type::TypeId::U32) {
panda_file::File::EntityId module_entity_id(f.metadata->GetValue().value().GetValue<uint32_t>());
if (disasm.IsModuleLiteralOffset(module_entity_id)) {
GetModuleLiteralArray(file_, module_entity_id, disasm, parser_program, index2namespaces, localnamespaces);
}
}
}
}
// debug mode
// panda::libpandafile::CollectUtil collect_util;
// std::unordered_set<uint32_t> literal_array_ids;
// collect_util.CollectLiteralArray(*file_, literal_array_ids);
// for (uint32_t literal_array_id : literal_array_ids) {
// panda_file::File::EntityId id {literal_array_id};
// if (disasm.IsModuleLiteralOffset(id)) {
// GetModuleLiteralArray(file_, id, disasm, parser_program, index2namespaces, localnamespaces);
// }
// }
}
}