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 #include "bin_mpl_export.h"
17 #include "bin_mpl_import.h"
18 #include "mir_function.h"
19 #include "opcode_info.h"
20 #include "mir_pragma.h"
21 #include "mir_builder.h"
22 using namespace std;
23
24 namespace maple {
ImportInfoVector(MIRInfoVector & infoVector,MapleVector<bool> & infoVectorIsString)25 void BinaryMplImport::ImportInfoVector(MIRInfoVector &infoVector, MapleVector<bool> &infoVectorIsString)
26 {
27 int64 size = ReadNum();
28 for (int64 i = 0; i < size; ++i) {
29 GStrIdx gStrIdx = ImportStr();
30 bool isstring = (ReadNum() != 0);
31 infoVectorIsString.push_back(isstring);
32 if (isstring) {
33 GStrIdx fieldval = ImportStr();
34 infoVector.emplace_back(MIRInfoPair(gStrIdx, fieldval.GetIdx()));
35 } else {
36 auto fieldval = static_cast<uint32>(ReadNum());
37 infoVector.emplace_back(MIRInfoPair(gStrIdx, fieldval));
38 }
39 }
40 }
41
ImportFuncIdInfo(MIRFunction * func)42 void BinaryMplImport::ImportFuncIdInfo(MIRFunction *func)
43 {
44 int64 tag = ReadNum();
45 CHECK_FATAL(tag == kBinFuncIdInfoStart, "kBinFuncIdInfoStart expected");
46 func->SetPuidxOrigin(static_cast<PUIdx>(ReadNum()));
47 ImportInfoVector(func->GetInfoVector(), func->InfoIsString());
48 if (mod.GetFlavor() == kFlavorLmbc) {
49 func->SetFrameSize(static_cast<uint32>(ReadNum()));
50 }
51 }
52
ImportBaseNode(Opcode & o,PrimType & typ)53 void BinaryMplImport::ImportBaseNode(Opcode &o, PrimType &typ)
54 {
55 o = static_cast<Opcode>(Read());
56 typ = static_cast<PrimType>(Read());
57 }
58
ImportLocalSymbol(MIRFunction * func)59 MIRSymbol *BinaryMplImport::ImportLocalSymbol(MIRFunction *func)
60 {
61 int64 tag = ReadNum();
62 if (tag == 0) {
63 return nullptr;
64 }
65 if (tag < 0) {
66 CHECK_FATAL(static_cast<size_t>(-tag) < localSymTab.size(), "index out of bounds");
67 return localSymTab.at(static_cast<size_t>(-tag));
68 }
69 CHECK_FATAL(tag == kBinSymbol, "expecting kBinSymbol in ImportLocalSymbol()");
70 MIRSymbol *sym = func->GetSymTab()->CreateSymbol(kScopeLocal);
71 localSymTab.push_back(sym);
72 sym->SetNameStrIdx(ImportStr());
73 (void)func->GetSymTab()->AddToStringSymbolMap(*sym);
74 sym->SetSKind(static_cast<MIRSymKind>(ReadNum()));
75 sym->SetStorageClass(static_cast<MIRStorageClass>(ReadNum()));
76 sym->SetAttrs(ImportTypeAttrs());
77 sym->SetIsTmp(ReadNum() != 0);
78 if (sym->GetSKind() == kStVar || sym->GetSKind() == kStFunc) {
79 ImportSrcPos(sym->GetSrcPosition());
80 }
81 sym->SetTyIdx(ImportJType());
82 if (sym->GetSKind() == kStPreg) {
83 PregIdx pregidx = ImportPreg(func);
84 MIRPreg *preg = func->GetPregTab()->PregFromPregIdx(pregidx);
85 sym->SetPreg(preg);
86 }
87 return sym;
88 }
89
ImportPreg(MIRFunction * func)90 PregIdx BinaryMplImport::ImportPreg(MIRFunction *func)
91 {
92 int64 tag = ReadNum();
93 if (tag == 0) {
94 return 0;
95 }
96 if (tag == kBinSpecialReg) {
97 return -Read();
98 }
99 if (tag < 0) {
100 CHECK_FATAL(static_cast<size_t>(-tag) < localPregTab.size(), "index out of bounds");
101 return localPregTab.at(static_cast<size_t>(-tag));
102 }
103 CHECK_FATAL(tag == kBinPreg, "expecting kBinPreg in ImportPreg()");
104
105 PrimType primType = static_cast<PrimType>(Read());
106 PregIdx pidx = func->GetPregTab()->CreatePreg(primType);
107 localPregTab.push_back(pidx);
108 return pidx;
109 }
110
ImportLabel(MIRFunction * func)111 LabelIdx BinaryMplImport::ImportLabel(MIRFunction *func)
112 {
113 int64 tag = ReadNum();
114 if (tag == 0) {
115 return 0;
116 }
117 if (tag < 0) {
118 CHECK_FATAL(static_cast<size_t>(-tag) < localLabelTab.size(), "index out of bounds");
119 return localLabelTab.at(static_cast<size_t>(-tag));
120 }
121 CHECK_FATAL(tag == kBinLabel, "kBinLabel expected in ImportLabel()");
122
123 LabelIdx lidx = func->GetLabelTab()->CreateLabel();
124 localLabelTab.push_back(lidx);
125 return lidx;
126 }
127
ImportLocalTypeNameTable(MIRTypeNameTable * typeNameTab)128 void BinaryMplImport::ImportLocalTypeNameTable(MIRTypeNameTable *typeNameTab)
129 {
130 int64 tag = ReadNum();
131 CHECK_FATAL(tag == kBinTypenameStart, "kBinTypenameStart expected in ImportLocalTypeNameTable()");
132 int64 size = ReadNum();
133 for (int64 i = 0; i < size; ++i) {
134 GStrIdx strIdx = ImportStr();
135 TyIdx tyIdx = ImportJType();
136 typeNameTab->SetGStrIdxToTyIdx(strIdx, tyIdx);
137 }
138 }
139
ImportFormalsStIdx(MIRFunction * func)140 void BinaryMplImport::ImportFormalsStIdx(MIRFunction *func)
141 {
142 auto tag = ReadNum();
143 CHECK_FATAL(tag == kBinFormalStart, "kBinFormalStart expected in ImportFormalsStIdx()");
144 auto size = ReadNum();
145 for (int64 i = 0; i < size; ++i) {
146 func->GetFormalDefVec()[static_cast<uint64>(i)].formalSym = ImportLocalSymbol(func);
147 }
148 }
149
ImportAliasMap(MIRFunction * func)150 void BinaryMplImport::ImportAliasMap(MIRFunction *func)
151 {
152 int64 tag = ReadNum();
153 CHECK_FATAL(tag == kBinAliasMapStart, "kBinAliasMapStart expected in ImportAliasMap()");
154 int32 size = ReadInt();
155 for (int32 i = 0; i < size; ++i) {
156 MIRAliasVars aliasvars;
157 GStrIdx strIdx = ImportStr();
158 aliasvars.mplStrIdx = ImportStr();
159 aliasvars.tyIdx = ImportJType();
160 (void)ImportStr(); // not assigning to mimic parser
161 func->GetAliasVarMap()[strIdx] = aliasvars;
162 }
163 }
164
ImportSrcPos(SrcPosition & pos)165 void BinaryMplImport::ImportSrcPos(SrcPosition &pos)
166 {
167 if (!mod.IsWithDbgInfo()) {
168 return;
169 }
170 pos.SetRawData(static_cast<uint32>(ReadNum()));
171 pos.SetLineNum(static_cast<uint32>(ReadNum()));
172 }
173
ImportReturnValues(MIRFunction * func,CallReturnVector * retv)174 void BinaryMplImport::ImportReturnValues(MIRFunction *func, CallReturnVector *retv)
175 {
176 int64 tag = ReadNum();
177 CHECK_FATAL(tag == kBinReturnvals, "expecting return values");
178 auto size = static_cast<uint32>(ReadNum());
179 for (uint32 i = 0; i < size; ++i) {
180 RegFieldPair rfp;
181 rfp.SetPregIdx(ImportPreg(func));
182 if (rfp.IsReg()) {
183 retv->push_back(std::make_pair(StIdx(), rfp));
184 continue;
185 }
186 rfp.SetFieldID(static_cast<FieldID>(ReadNum()));
187 MIRSymbol *lsym = ImportLocalSymbol(func);
188 CHECK_FATAL(lsym != nullptr, "null ptr check");
189 retv->push_back(std::make_pair(lsym->GetStIdx(), rfp));
190 if (lsym->GetName().find("L_STR") == 0) {
191 MIRType *ty = GlobalTables::GetTypeTable().GetTypeFromTyIdx(lsym->GetTyIdx());
192 CHECK_FATAL(ty->GetKind() == kTypePointer, "Pointer type expected for L_STR prefix");
193 MIRPtrType tempType(static_cast<MIRPtrType *>(ty)->GetPointedTyIdx(), PTY_ptr);
194 TyIdx newTyidx = GlobalTables::GetTypeTable().GetOrCreateMIRType(&tempType);
195 lsym->SetTyIdx(newTyidx);
196 }
197 }
198 }
199 } // namespace maple
200