• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LIBABCKIT_SRC_ADAPTER_DYNAMIC_HELPERS_DYNAMIC_H
17 #define LIBABCKIT_SRC_ADAPTER_DYNAMIC_HELPERS_DYNAMIC_H
18 
19 #include <string>
20 
21 #include "libabckit/include/c/abckit.h"
22 #include "libabckit/src/metadata_inspect_impl.h"
23 #include "assembler/assembly-emitter.h"
24 #include "assembler/assembly-literals.h"
25 
26 #if __has_include(<filesystem>)
27 #include <filesystem>
28 namespace fs = std::filesystem;
29 #elif __has_include(<experimental/filesystem>)
30 #define STD_FILESYSTEM_EXPERIMENTAL
31 #include <experimental/filesystem>
32 namespace fs = std::experimental::filesystem;
33 #endif
34 
35 namespace libabckit {
36 
37 struct ModuleUpdateData {
38     bool isRegularImportsChange = false;
39     bool isLocalExportsChange = false;
40     std::vector<panda::pandasm::LiteralArray::Literal> newLiterals {};
41     std::unordered_map<uint32_t, uint32_t> regularImportsIdxMap {};
42     std::unordered_map<uint32_t, uint32_t> localExportsIdxMap {};
43 };
44 
45 struct ModuleIterateData {
46     AbckitCoreModule *m = nullptr;
47     AbckitModulePayloadDyn *payload = nullptr;
48     panda::pandasm::LiteralArray *moduleLitArr = nullptr;
49     std::pair<bool, bool> isRegularImport {};  // {isImport, isRegular}
50     AbckitDynamicExportKind kind = ABCKIT_DYNAMIC_EXPORT_KIND_LOCAL_EXPORT;
51     libabckit::ModuleUpdateData updateData {};
52 };
53 
54 const panda::panda_file::File *EmitDynamicProgram(AbckitFile *file, panda::pandasm::Program *program,
55                                                   panda::pandasm::AsmEmitter::PandaFileToPandaAsmMaps *mapsp,
56                                                   bool getFile, std::string_view path = {});
57 bool IterateModuleSections(
58     ModuleIterateData &data, const std::function<size_t(ModuleIterateData *)> &requestIdxSectionModifier,
59     const std::function<std::pair<size_t, size_t>(ModuleIterateData *, size_t, size_t)> &sectionModifier,
60     const std::function<bool(ModuleIterateData *, std::pair<size_t, size_t>)> &postAction);
61 void DumpModuleArray(const panda::pandasm::LiteralArray *moduleLitArr, std::string_view name);
62 
63 fs::path Relative(const fs::path &src, const fs::path &base);
64 
65 bool IsServiceRecord(const std::string &name);
66 bool IsModuleDescriptorRecord(const panda::pandasm::Record &rec);
67 bool IsAnnotationInterfaceRecord(const panda::pandasm::Record &rec);
68 bool IsExternalRecord(const panda::pandasm::Record &rec);
69 
70 bool IsMain(const std::string &funcName);
71 bool IsFunction(const std::string &funcName);
72 bool IsCtor(const std::string &funcName);
73 bool IsStatic(const std::string &funcName);
74 bool IsAnonymousName(const std::string &funcName);
75 
76 panda::pandasm::Function *GetDynFunction(AbckitCoreFunction *function);
77 panda::pandasm::Function *GetDynFunction(AbckitCoreClass *klass);
78 AbckitModulePayloadDyn *GetDynModulePayload(AbckitCoreModule *mod);
79 AbckitDynamicImportDescriptorPayload *GetDynImportDescriptorPayload(AbckitCoreImportDescriptor *id);
80 AbckitDynamicExportDescriptorPayload *GetDynExportDescriptorPayload(AbckitCoreExportDescriptor *ed);
81 
82 std::string GetClassNameFromCtor(const std::string &ctorName, const AbckitLiteralArray *scopeNames);
83 
84 AbckitLiteral *FindOrCreateLiteralBoolDynamicImpl(AbckitFile *file, bool value);
85 AbckitLiteral *FindOrCreateLiteralU8DynamicImpl(AbckitFile *file, uint8_t value);
86 AbckitLiteral *FindOrCreateLiteralU16DynamicImpl(AbckitFile *file, uint16_t value);
87 AbckitLiteral *FindOrCreateLiteralMethodAffiliateDynamicImpl(AbckitFile *file, uint16_t value);
88 AbckitLiteral *FindOrCreateLiteralU32DynamicImpl(AbckitFile *file, uint32_t value);
89 AbckitLiteral *FindOrCreateLiteralU64DynamicImpl(AbckitFile *file, uint64_t value);
90 AbckitLiteral *FindOrCreateLiteralFloatDynamicImpl(AbckitFile *file, float value);
91 AbckitLiteral *FindOrCreateLiteralDoubleDynamicImpl(AbckitFile *file, double value);
92 AbckitLiteral *FindOrCreateLiteralLiteralArrayDynamicImpl(AbckitFile *file, const std::string &value);
93 AbckitLiteral *FindOrCreateLiteralStringDynamicImpl(AbckitFile *file, const std::string &value);
94 AbckitLiteral *FindOrCreateLiteralMethodDynamicImpl(AbckitFile *file, const std::string &value);
95 AbckitLiteral *FindOrCreateLiteralDynamic(AbckitFile *file, const panda::pandasm::LiteralArray::Literal &value);
96 
97 AbckitValue *FindOrCreateValueU1DynamicImpl(AbckitFile *file, bool value);
98 AbckitValue *FindOrCreateValueDoubleDynamicImpl(AbckitFile *file, double value);
99 AbckitValue *FindOrCreateValueStringDynamicImpl(AbckitFile *file, const std::string &value);
100 AbckitValue *FindOrCreateLiteralArrayValueDynamicImpl(AbckitFile *file, const std::string &value);
101 AbckitValue *FindOrCreateValueDynamic(AbckitFile *file, const panda::pandasm::Value &value);
102 
103 }  // namespace libabckit
104 
105 #endif
106