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_EXPORT_H 17 #define MAPLE_IR_INCLUDE_BIN_MPL_EXPORT_H 18 #include "mir_module.h" 19 #include "mir_nodes.h" 20 #include "mir_function.h" 21 #include "mir_preg.h" 22 #include "parser_opt.h" 23 #include "ea_connection_graph.h" 24 25 namespace maple { 26 enum : uint8 { 27 kBinString = 1, 28 kBinUsrString = kBinString, 29 kBinInitConst = 2, 30 kBinSymbol = 3, 31 kBinFunction = 4, 32 kBinCallinfo = 5, 33 kBinKindTypeScalar = 6, 34 kBinKindTypeByName = 7, 35 kBinKindTypePointer = 8, 36 kBinKindTypeFArray = 9, 37 kBinKindTypeJarray = 10, 38 kBinKindTypeArray = 11, 39 kBinKindTypeFunction = 12, 40 kBinKindTypeParam = 13, 41 kBinKindTypeInstantVector = 14, 42 kBinKindTypeGenericInstant = 15, 43 kBinKindTypeBitField = 16, 44 kBinKindTypeStruct = 17, // for kTypeStruct, kTypeStructIncomplete and kTypeUnion 45 kBinKindTypeClass = 18, // for kTypeClass, and kTypeClassIncomplete 46 kBinKindTypeInterface = 19, // for kTypeInterface, and kTypeInterfaceIncomplete 47 kBinKindConstInt = 20, 48 kBinKindConstAddrof = 21, 49 kBinKindConstAddrofFunc = 22, 50 kBinKindConstStr = 23, 51 kBinKindConstStr16 = 24, 52 kBinKindConstFloat = 25, 53 kBinKindConstDouble = 26, 54 kBinKindConstAgg = 27, 55 kBinKindConstSt = 28, 56 kBinContentStart = 29, 57 kBinStrStart = 30, 58 kBinTypeStart = 31, 59 kBinCgStart = 32, 60 kBinSeStart = 33, 61 kBinFinish = 34, 62 kStartMethod = 35, 63 kBinEaCgNode = 36, 64 kBinEaCgActNode = 37, 65 kBinEaCgFieldNode = 38, 66 kBinEaCgRefNode = 39, 67 kBinEaCgObjNode = 40, 68 kBinEaCgStart = 41, 69 kBinEaStart = 42, 70 kBinNodeBlock = 43, 71 // kBinOpStatement : 44, 72 // kBinOpExpression : 45, 73 kBinReturnvals = 46, 74 kBinTypeTabStart = 47, 75 kBinSymStart = 48, 76 kBinSymTabStart = 49, 77 kBinFuncIdInfoStart = 50, 78 kBinFormalStart = 51, 79 kBinPreg = 52, 80 kBinSpecialReg = 53, 81 kBinLabel = 54, 82 kBinTypenameStart = 55, 83 kBinHeaderStart = 56, 84 kBinAliasMapStart = 57, 85 // kBinKindTypeViaTypename : 58, 86 // kBinKindSymViaSymname : 59, 87 // kBinKindFuncViaSymname : 60, 88 kBinFunctionBodyStart = 61, 89 kBinFormalWordsTypeTagged = 62, 90 kBinFormalWordsRefCounted = 63, 91 kBinLocalWordsTypeTagged = 64, 92 kBinLocalWordsRefCounter = 65, 93 kBinKindConstAddrofLabel = 66, 94 kBinKindConstAddrofLocal = 67, 95 }; 96 97 // this value is used to check wether a file is a binary mplt file 98 constexpr int32 kMpltMagicNumber = 0xC0FFEE; 99 class BinaryMplExport { 100 public: 101 explicit BinaryMplExport(MIRModule &md); 102 virtual ~BinaryMplExport() = default; 103 104 void Export(const std::string &fname, std::unordered_set<std::string> *dumpFuncSet); 105 void WriteNum(int64 x); 106 void Write(uint8 b); 107 void OutputType(TyIdx tyIdx); 108 void WriteFunctionBodyField(uint64 contentIdx, std::unordered_set<std::string> *dumpFuncSet); 109 void OutputConst(MIRConst *constVal); 110 void OutputConstBase(const MIRConst &constVal); 111 void OutputTypeBase(const MIRType &type); 112 void OutputTypePairs(const MIRInstantVectorType &type); 113 void OutputStr(const GStrIdx &gstr); 114 void OutputUsrStr(UStrIdx ustr); 115 void OutputTypeAttrs(const TypeAttrs &ta); 116 void OutputPragmaElement(const MIRPragmaElement &e); 117 void OutputPragma(const MIRPragma &p); 118 void OutputFieldPair(const FieldPair &fp); 119 void OutputMethodPair(const MethodPair &memPool); 120 void OutputFieldsOfStruct(const FieldVector &fields); 121 void OutputMethodsOfStruct(const MethodVector &methods); 122 void OutputStructTypeData(const MIRStructType &type); 123 void OutputImplementedInterfaces(const std::vector<TyIdx> &interfaces); 124 void OutputInfoIsString(const std::vector<bool> &infoIsString); 125 void OutputInfo(const std::vector<MIRInfoPair> &info, const std::vector<bool> &infoIsString); 126 void OutputPragmaVec(const std::vector<MIRPragma *> &pragmaVec); 127 void OutputClassTypeData(const MIRClassType &type); 128 void OutputSymbol(MIRSymbol *sym); 129 void OutputFunction(PUIdx puIdx); 130 void OutputInterfaceTypeData(const MIRInterfaceType &type); 131 void OutputSrcPos(const SrcPosition &pos); 132 void OutputAliasMap(MapleMap<GStrIdx, MIRAliasVars> &aliasVarMap); 133 void OutputInfoVector(const MIRInfoVector &infoVector, const MapleVector<bool> &infoVectorIsString); 134 void OutputFuncIdInfo(MIRFunction *func); 135 void OutputLocalSymbol(MIRSymbol *sym); 136 void OutputPreg(MIRPreg *preg); 137 void OutputLabel(LabelIdx lidx); 138 void OutputLocalTypeNameTab(const MIRTypeNameTable *typeNameTab); 139 void OutputFormalsStIdx(MIRFunction *func); 140 void OutputFuncViaSym(PUIdx puIdx); 141 void OutputExpression(BaseNode *e); 142 void OutputBaseNode(const BaseNode *b); 143 void OutputReturnValues(const CallReturnVector *retv); 144 void OutputBlockNode(BlockNode *block); 145 GetMIRModule()146 const MIRModule &GetMIRModule() const 147 { 148 return mod; 149 } 150 151 bool not2mplt; // this export is not to an mplt file 152 MIRFunction *curFunc = nullptr; 153 154 private: 155 using CallSite = std::pair<CallInfo *, PUIdx>; 156 void WriteEaField(const CallGraph &cg); 157 void WriteEaCgField(EAConnectionGraph *eaCg); 158 void OutEaCgNode(EACGBaseNode &node); 159 void OutEaCgBaseNode(const EACGBaseNode &node, bool firstPart); 160 void OutEaCgFieldNode(EACGFieldNode &field); 161 void OutEaCgRefNode(const EACGRefNode &ref); 162 void OutEaCgActNode(const EACGActualNode &act); 163 void OutEaCgObjNode(EACGObjectNode &obj); 164 void WriteCgField(uint64 contentIdx, const CallGraph *cg); 165 void WriteSeField(); 166 void OutputCallInfo(CallInfo &callInfo); 167 void WriteContentField4mplt(int fieldNum, uint64 *fieldStartP); 168 void WriteContentField4nonmplt(int fieldNum, uint64 *fieldStartP); 169 void WriteContentField4nonJava(int fieldNum, uint64 *fieldStartP); 170 void WriteStrField(uint64 contentIdx); 171 void WriteHeaderField(uint64 contentIdx); 172 void WriteTypeField(uint64 contentIdx, bool useClassList = true); 173 void Init(); 174 void WriteSymField(uint64 contentIdx); 175 void WriteInt(int32 x); 176 uint8 Read(); 177 int32 ReadInt(); 178 void WriteInt64(int64 x); 179 void WriteAsciiStr(const std::string &str); 180 void Fixup(size_t i, int32 x); 181 void DumpBuf(const std::string &name); 182 void AppendAt(const std::string &name, int32 offset); 183 void ExpandFourBuffSize(); 184 185 MIRModule &mod; 186 size_t bufI = 0; 187 std::vector<uint8> buf; 188 std::unordered_map<GStrIdx, int64, GStrIdxHash> gStrMark; 189 std::unordered_map<MIRFunction *, int64> funcMark; 190 std::string importFileName; 191 std::unordered_map<UStrIdx, int64, UStrIdxHash> uStrMark; 192 std::unordered_map<const MIRSymbol *, int64> symMark; 193 std::unordered_map<MIRType *, int64> typMark; 194 std::unordered_map<const MIRSymbol *, int64> localSymMark; 195 std::unordered_map<const MIRPreg *, int64> localPregMark; 196 std::unordered_map<LabelIdx, int64> labelMark; 197 friend class UpdateMplt; 198 std::unordered_map<uint32, int64> callInfoMark; 199 std::map<GStrIdx, uint8> *func2SEMap = nullptr; 200 std::unordered_map<EACGBaseNode *, int64> eaNodeMark; 201 bool inIPA = false; 202 static int typeMarkOffset; // offset of mark (tag in binmplimport) resulting from duplicated function 203 }; 204 205 class UpdateMplt { 206 public: 207 UpdateMplt() = default; 208 ~UpdateMplt() = default; 209 class ManualSideEffect { 210 public: ManualSideEffect(std::string name,bool p,bool u,bool d,bool o,bool e)211 ManualSideEffect(std::string name, bool p, bool u, bool d, bool o, bool e) 212 : funcName(name), pure(p), defArg(u), def(d), object(o), exception(e) {}; 213 virtual ~ManualSideEffect() = default; 214 GetFuncName()215 const std::string &GetFuncName() const 216 { 217 return funcName; 218 } 219 GetPure()220 bool GetPure() const 221 { 222 return pure; 223 } 224 GetDefArg()225 bool GetDefArg() const 226 { 227 return defArg; 228 } 229 GetDef()230 bool GetDef() const 231 { 232 return def; 233 } 234 GetObject()235 bool GetObject() const 236 { 237 return object; 238 } 239 GetException()240 bool GetException() const 241 { 242 return exception; 243 } 244 GetPrivateUse()245 bool GetPrivateUse() const 246 { 247 return privateUse; 248 } 249 GetPrivateDef()250 bool GetPrivateDef() const 251 { 252 return privateDef; 253 } 254 255 private: 256 std::string funcName; 257 bool pure; 258 bool defArg; 259 bool def; 260 bool object; 261 bool exception; 262 bool privateUse = false; 263 bool privateDef = false; 264 }; 265 void UpdateCgField(BinaryMplt &binMplt, const CallGraph &cg); 266 }; 267 } // namespace maple 268 #endif // MAPLE_IR_INCLUDE_BIN_MPL_EXPORT_H 269