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 OutputConst(MIRConst *constVal); 109 void OutputConstBase(const MIRConst &constVal); 110 void OutputTypeBase(const MIRType &type); 111 void OutputTypePairs(const MIRInstantVectorType &type); 112 void OutputStr(const GStrIdx &gstr); 113 void OutputUsrStr(UStrIdx ustr); 114 void OutputTypeAttrs(const TypeAttrs &ta); 115 void OutputPragmaElement(const MIRPragmaElement &e); 116 void OutputPragma(const MIRPragma &p); 117 void OutputFieldPair(const FieldPair &fp); 118 void OutputMethodPair(const MethodPair &memPool); 119 void OutputFieldsOfStruct(const FieldVector &fields); 120 void OutputMethodsOfStruct(const MethodVector &methods); 121 void OutputStructTypeData(const MIRStructType &type); 122 void OutputImplementedInterfaces(const std::vector<TyIdx> &interfaces); 123 void OutputInfoIsString(const std::vector<bool> &infoIsString); 124 void OutputInfo(const std::vector<MIRInfoPair> &info, const std::vector<bool> &infoIsString); 125 void OutputPragmaVec(const std::vector<MIRPragma *> &pragmaVec); 126 void OutputClassTypeData(const MIRClassType &type); 127 void OutputSymbol(MIRSymbol *sym); 128 void OutputInterfaceTypeData(const MIRInterfaceType &type); 129 void OutputSrcPos(const SrcPosition &pos); 130 void OutputAliasMap(MapleMap<GStrIdx, MIRAliasVars> &aliasVarMap); 131 void OutputInfoVector(const MIRInfoVector &infoVector, const MapleVector<bool> &infoVectorIsString); 132 void OutputFuncIdInfo(MIRFunction *func); 133 void OutputLocalSymbol(MIRSymbol *sym); 134 void OutputPreg(MIRPreg *preg); 135 void OutputLabel(LabelIdx lidx); 136 void OutputLocalTypeNameTab(const MIRTypeNameTable *typeNameTab); 137 void OutputFormalsStIdx(MIRFunction *func); 138 void OutputFuncViaSym(PUIdx puIdx); 139 void OutputBaseNode(const BaseNode *b); 140 void OutputReturnValues(const CallReturnVector *retv); 141 GetMIRModule()142 const MIRModule &GetMIRModule() const 143 { 144 return mod; 145 } 146 147 bool not2mplt; // this export is not to an mplt file 148 MIRFunction *curFunc = nullptr; 149 150 private: 151 using CallSite = std::pair<CallInfo *, PUIdx>; 152 void WriteEaField(const CallGraph &cg); 153 void WriteEaCgField(EAConnectionGraph *eaCg); 154 void OutEaCgNode(EACGBaseNode &node); 155 void OutEaCgBaseNode(const EACGBaseNode &node, bool firstPart); 156 void OutEaCgFieldNode(EACGFieldNode &field); 157 void OutEaCgRefNode(const EACGRefNode &ref); 158 void OutEaCgActNode(const EACGActualNode &act); 159 void OutEaCgObjNode(EACGObjectNode &obj); 160 void WriteCgField(uint64 contentIdx, const CallGraph *cg); 161 void WriteSeField(); 162 void OutputCallInfo(CallInfo &callInfo); 163 void WriteContentField4mplt(int fieldNum, uint64 *fieldStartP); 164 void WriteContentField4nonmplt(int fieldNum, uint64 *fieldStartP); 165 void WriteStrField(uint64 contentIdx); 166 void WriteHeaderField(uint64 contentIdx); 167 void WriteTypeField(uint64 contentIdx, bool useClassList = true); 168 void Init(); 169 void WriteSymField(uint64 contentIdx); 170 void WriteInt(int32 x); 171 uint8 Read(); 172 int32 ReadInt(); 173 void WriteInt64(int64 x); 174 void WriteAsciiStr(const std::string &str); 175 void Fixup(size_t i, int32 x); 176 void DumpBuf(const std::string &name); 177 void AppendAt(const std::string &name, int32 offset); 178 void ExpandFourBuffSize(); 179 180 MIRModule &mod; 181 size_t bufI = 0; 182 std::vector<uint8> buf; 183 std::unordered_map<GStrIdx, int64, GStrIdxHash> gStrMark; 184 std::unordered_map<MIRFunction *, int64> funcMark; 185 std::string importFileName; 186 std::unordered_map<UStrIdx, int64, UStrIdxHash> uStrMark; 187 std::unordered_map<const MIRSymbol *, int64> symMark; 188 std::unordered_map<MIRType *, int64> typMark; 189 std::unordered_map<const MIRSymbol *, int64> localSymMark; 190 std::unordered_map<const MIRPreg *, int64> localPregMark; 191 std::unordered_map<LabelIdx, int64> labelMark; 192 friend class UpdateMplt; 193 std::unordered_map<uint32, int64> callInfoMark; 194 std::map<GStrIdx, uint8> *func2SEMap = nullptr; 195 std::unordered_map<EACGBaseNode *, int64> eaNodeMark; 196 bool inIPA = false; 197 static int typeMarkOffset; // offset of mark (tag in binmplimport) resulting from duplicated function 198 }; 199 200 class UpdateMplt { 201 public: 202 UpdateMplt() = default; 203 ~UpdateMplt() = default; 204 class ManualSideEffect { 205 public: ManualSideEffect(std::string name,bool p,bool u,bool d,bool o,bool e)206 ManualSideEffect(std::string name, bool p, bool u, bool d, bool o, bool e) 207 : funcName(name), pure(p), defArg(u), def(d), object(o), exception(e) {}; 208 virtual ~ManualSideEffect() = default; 209 GetFuncName()210 const std::string &GetFuncName() const 211 { 212 return funcName; 213 } 214 GetPure()215 bool GetPure() const 216 { 217 return pure; 218 } 219 GetDefArg()220 bool GetDefArg() const 221 { 222 return defArg; 223 } 224 GetDef()225 bool GetDef() const 226 { 227 return def; 228 } 229 GetObject()230 bool GetObject() const 231 { 232 return object; 233 } 234 GetException()235 bool GetException() const 236 { 237 return exception; 238 } 239 GetPrivateUse()240 bool GetPrivateUse() const 241 { 242 return privateUse; 243 } 244 GetPrivateDef()245 bool GetPrivateDef() const 246 { 247 return privateDef; 248 } 249 250 private: 251 std::string funcName; 252 bool pure; 253 bool defArg; 254 bool def; 255 bool object; 256 bool exception; 257 bool privateUse = false; 258 bool privateDef = false; 259 }; 260 void UpdateCgField(BinaryMplt &binMplt, const CallGraph &cg); 261 }; 262 } // namespace maple 263 #endif // MAPLE_IR_INCLUDE_BIN_MPL_EXPORT_H 264