• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 MAPLE_IR_INCLUDE_BIN_MPL_IMPORT_H
17 #define MAPLE_IR_INCLUDE_BIN_MPL_IMPORT_H
18 #include "mir_module.h"
19 #include "mir_nodes.h"
20 #include "mir_preg.h"
21 #include "parser_opt.h"
22 #include "mir_builder.h"
23 #include "ea_connection_graph.h"
24 namespace maple {
25 class BinaryMplImport {
26 public:
BinaryMplImport(MIRModule & md)27     explicit BinaryMplImport(MIRModule &md) : mod(md), mirBuilder(&md) {}
28     BinaryMplImport &operator=(const BinaryMplImport &) = delete;
29     BinaryMplImport(const BinaryMplImport &) = delete;
30 
~BinaryMplImport()31     virtual ~BinaryMplImport()
32     {
33         for (MIRStructType *structPtr : tmpStruct) {
34             delete structPtr;
35         }
36         for (MIRClassType *classPtr : tmpClass) {
37             delete classPtr;
38         }
39         for (MIRInterfaceType *interfacePtr : tmpInterface) {
40             delete interfacePtr;
41         }
42     }
43 
GetBufI()44     uint64 GetBufI() const
45     {
46         return bufI;
47     }
SetBufI(uint64 bufIVal)48     void SetBufI(uint64 bufIVal)
49     {
50         bufI = bufIVal;
51     }
52 
IsBufEmpty()53     bool IsBufEmpty() const
54     {
55         return buf.empty();
56     }
GetBufSize()57     size_t GetBufSize() const
58     {
59         return buf.size();
60     }
61 
GetContent(int64 key)62     int32 GetContent(int64 key) const
63     {
64         return content.at(key);
65     }
66 
SetImported(bool importedVal)67     void SetImported(bool importedVal)
68     {
69         imported = importedVal;
70     }
71 
72     bool Import(const std::string &modid, bool readSymbols = false, bool readSe = false);
73     bool ImportForSrcLang(const std::string &modid, MIRSrcLang &srcLang);
74     MIRSymbol *GetOrCreateSymbol(TyIdx tyIdx, GStrIdx strIdx, MIRSymKind mclass, MIRStorageClass sclass,
75                                  MIRFunction *func, uint8 scpID);
76     int32 ReadInt();
77     int64 ReadNum();
78 
79 private:
80     void ReadContentField();
81     void ReadStrField();
82     void ReadHeaderField();
83     void ReadTypeField();
84     void ReadSymField();
85     void ReadSymTabField();
86     void ReadCgField();
87     EAConnectionGraph *ReadEaCgField();
88     void ReadEaField();
89     EACGBaseNode &InEaCgNode(EAConnectionGraph &newEaCg);
90     void InEaCgBaseNode(EACGBaseNode &base, EAConnectionGraph &newEaCg, bool firstPart);
91     void InEaCgActNode(EACGActualNode &actual);
92     void InEaCgFieldNode(EACGFieldNode &field, EAConnectionGraph &newEaCg);
93     void InEaCgObjNode(EACGObjectNode &obj, EAConnectionGraph &newEaCg);
94     void InEaCgRefNode(EACGRefNode &ref);
95     CallInfo *ImportCallInfo();
96     void MergeDuplicated(PUIdx methodPuidx, std::vector<CallInfo *> &targetSet, std::vector<CallInfo *> &newSet);
97     void ReadSeField();
98     void Jump2NextField();
99     void Reset();
100     void SkipTotalSize();
101     void ImportFieldsOfStructType(FieldVector &fields, uint32 methodSize);
102     MIRType &InsertInTypeTables(MIRType &ptype);
103     void InsertInHashTable(MIRType &ptype);
104     void SetupEHRootType();
105     void UpdateMethodSymbols();
106     void ImportConstBase(MIRConstKind &kind, MIRTypePtr &type);
107     MIRConst *ImportConst(MIRFunction *func);
108     GStrIdx ImportStr();
109     UStrIdx ImportUsrStr();
110     MIRType *CreateMirType(MIRTypeKind kind, GStrIdx strIdx, int64 tag) const;
111     MIRGenericInstantType *CreateMirGenericInstantType(GStrIdx strIdx) const;
112     MIRBitFieldType *CreateBitFieldType(uint8 fieldsize, PrimType pt, GStrIdx strIdx) const;
113     void CompleteAggInfo(TyIdx tyIdx);
114     TyIdx ImportType(bool forPointedType = false);
115     TyIdx ImportTypeNonJava();
116     void ImportTypeBase(PrimType &primType, GStrIdx &strIdx, bool &nameIsLocal);
117     void InSymTypeTable();
118     void ImportTypePairs(std::vector<TypePair> &insVecType);
119     TypeAttrs ImportTypeAttrs();
120     MIRPragmaElement *ImportPragmaElement();
121     MIRPragma *ImportPragma();
122     void ImportFieldPair(FieldPair &fp);
123     void ImportMethodPair(MethodPair &memPool);
124     void ImportMethodsOfStructType(MethodVector &methods);
125     void ImportStructTypeData(MIRStructType &type);
126     void ImportInterfacesOfClassType(std::vector<TyIdx> &interfaces);
127     void ImportInfoIsStringOfStructType(MIRStructType &type);
128     void ImportInfoOfStructType(MIRStructType &type);
129     void ImportPragmaOfStructType(MIRStructType &type);
130     void SetClassTyidxOfMethods(MIRStructType &type);
131     void ImportClassTypeData(MIRClassType &type);
132     void ImportInterfaceTypeData(MIRInterfaceType &type);
133     PUIdx ImportFunction();
134     MIRSymbol *InSymbol(MIRFunction *func);
135     void ImportInfoVector(MIRInfoVector &infoVector, MapleVector<bool> &infoVectorIsString);
136     void ImportLocalTypeNameTable(MIRTypeNameTable *typeNameTab);
137     void ImportFuncIdInfo(MIRFunction *func);
138     MIRSymbol *ImportLocalSymbol(MIRFunction *func);
139     PregIdx ImportPreg(MIRFunction *func);
140     LabelIdx ImportLabel(MIRFunction *func);
141     void ImportFormalsStIdx(MIRFunction *func);
142     void ImportAliasMap(MIRFunction *func);
143     void ImportSrcPos(SrcPosition &pos);
144     void ImportBaseNode(Opcode &o, PrimType &typ);
145     PUIdx ImportFuncViaSym(MIRFunction *func);
146     BaseNode *ImportExpression(MIRFunction *func);
147     void ImportReturnValues(MIRFunction *func, CallReturnVector *retv);
148     BlockNode *ImportBlockNode(MIRFunction *fn);
149     void ReadFunctionBodyField();
150     void ReadFileAt(const std::string &modid, int32 offset);
151     uint8 Read();
152     int64 ReadInt64();
153     void ReadAsciiStr(std::string &str);
154     int32 GetIPAFileIndex(std::string &name);
155 
156     bool inCG = false;
157     bool inIPA = false;
158     bool imported = true;            // used only by irbuild to convert to ascii
159     bool importingFromMplt = false;  // decided based on magic number
160     uint64 bufI = 0;
161     std::vector<uint8> buf;
162     std::map<int64, int32> content;
163     MIRModule &mod;
164     MIRBuilder mirBuilder;
165     std::vector<GStrIdx> gStrTab;
166     std::vector<UStrIdx> uStrTab;
167     std::vector<MIRStructType *> tmpStruct;
168     std::vector<MIRClassType *> tmpClass;
169     std::vector<MIRInterfaceType *> tmpInterface;
170     std::vector<TyIdx> typTab;
171     std::vector<MIRFunction *> funcTab;
172     std::vector<MIRSymbol *> symTab;
173     std::vector<MIRSymbol *> localSymTab;
174     std::vector<PregIdx> localPregTab;
175     std::vector<LabelIdx> localLabelTab;
176     std::vector<CallInfo *> callInfoTab;
177     std::vector<EACGBaseNode *> eaCgTab;
178     std::vector<MIRSymbol *> methodSymbols;
179     std::vector<bool> definedLabels;
180     std::string importFileName;
181 };
182 }  // namespace maple
183 #endif  // MAPLE_IR_INCLUDE_BIN_MPL_IMPORT_H
184