diff --git a/src/bin/jbc2mpl b/src/bin/jbc2mpl index 7451ea62006d781e944ed91202d975f8c463c0e5..50a00fb9b0abb546644b318a510e1ec5c56f79a4 100755 Binary files a/src/bin/jbc2mpl and b/src/bin/jbc2mpl differ diff --git a/src/bin/maple b/src/bin/maple index 76b51d98cabe92190c9f7a1e12bda186874d9042..4cb8c472a4f0cd07e05d33f42805871d914a30e6 100755 Binary files a/src/bin/maple and b/src/bin/maple differ diff --git a/src/deplibs/libmplphase.a b/src/deplibs/libmplphase.a index ca1ed9dfbe87951d7a3d5ec221e0c1eefaf59d0e..155adbb875fca2dc4a8b608c315285aa28f8875e 100644 Binary files a/src/deplibs/libmplphase.a and b/src/deplibs/libmplphase.a differ diff --git a/src/deplibs/libmplutil.a b/src/deplibs/libmplutil.a index 95112b0c8f5638ef23aef4cd513e4fc14a956871..036cac2e4ce9991d3dac424594267d650ecf8248 100644 Binary files a/src/deplibs/libmplutil.a and b/src/deplibs/libmplutil.a differ diff --git a/src/maple_be/src/be/becommon.cpp b/src/maple_be/src/be/becommon.cpp index b7f1973f6c2a35c8b6f442a1d98a64395d544198..e803b78d9a285d07238fc43f8e1cca5d867712ac 100644 --- a/src/maple_be/src/be/becommon.cpp +++ b/src/maple_be/src/be/becommon.cpp @@ -288,6 +288,12 @@ void BECommon::ComputeTypeSizesAligns(MIRType &ty, uint8 align) { switch (ty.GetKind()) { case kTypeScalar: +#ifdef TARGARM32 + SetTypeSize(tyIdx, GetPrimTypeSize(ty.GetPrimType())); + SetTypeAlign(tyIdx, GetPrimTypeSize(ty.GetPrimType()) > k4ByteSize ? k4ByteSize + : GetPrimTypeSize(ty.GetPrimType())); + break; +#endif case kTypePointer: case kTypeBitField: case kTypeFunction: diff --git a/src/maple_me/include/me_pgo_instrument.h b/src/maple_me/include/me_pgo_instrument.h index 3416d4a12cba2eba359892eba80fdb31eb4b0801..c69986656dc951709e74e3ff591d5ca017a77ec1 100644 --- a/src/maple_me/include/me_pgo_instrument.h +++ b/src/maple_me/include/me_pgo_instrument.h @@ -17,6 +17,7 @@ #include "bb.h" #include "me_cfg_mst.h" +#include "me_cfg.h" #include "itab_util.h" namespace maple { @@ -105,6 +106,7 @@ class PGOInstrument { } else if (!edge->IsCritical()) { bbs.push_back(dest); } else { + func->GetTheCfg()->DumpToFile("profGenError", false); CHECK_FATAL(false, "impossible critial edge %d -> %d", src->UintID(), dest->UintID()); } } diff --git a/src/maple_me/include/me_profile_gen.h b/src/maple_me/include/me_profile_gen.h index 615e736dbc02d800c945b4a46e343377723429d4..5acf00b3dda6fb2b2659e290a4c68ab303948726 100644 --- a/src/maple_me/include/me_profile_gen.h +++ b/src/maple_me/include/me_profile_gen.h @@ -28,7 +28,10 @@ class MeProfGen : public PGOInstrument { : PGOInstrument(func, mp, dump), func(&func), hMap(&hMap) { Init(); } + bool CanInstrument() const; void InstrumentFunc(); + static void DumpSummary(); + static void IncTotalFunc(); private: void Init(); void InstrumentBB(BB &bb); @@ -38,6 +41,8 @@ class MeProfGen : public PGOInstrument { static uint64 counterIdx; static uint64 totalBB; static uint64 instrumentBB; + static uint64 totalFunc; + static uint64 instrumentFunc; static MIRSymbol *bbCounterTabSym; static bool firstRun; }; diff --git a/src/maple_me/src/bb.cpp b/src/maple_me/src/bb.cpp index eb4fc20f857ec5c91e9b50412dd99ebce8da98f6..40866dcd7733766eeced8a3dd1d5622493781aa5 100644 --- a/src/maple_me/src/bb.cpp +++ b/src/maple_me/src/bb.cpp @@ -50,6 +50,9 @@ void BB::DumpBBAttribute(const MIRModule *mod) const { if (GetAttributes(kBBAttrIsExit)) { mod->GetOut() << " Exit "; } + if (GetAttributes(kBBAttrWontExit)) { + mod->GetOut() << " WontExit "; + } if (GetAttributes(kBBAttrIsTry)) { mod->GetOut() << " Try "; } diff --git a/src/maple_me/src/me_profile_gen.cpp b/src/maple_me/src/me_profile_gen.cpp index 000703f7450093afce3b2d0f6d587d531a1e453e..bf679521b7c27bb6c5c38758fa565a3f42288027 100644 --- a/src/maple_me/src/me_profile_gen.cpp +++ b/src/maple_me/src/me_profile_gen.cpp @@ -35,9 +35,22 @@ namespace maple { uint64 MeProfGen::counterIdx = 0; uint64 MeProfGen::totalBB = 0; uint64 MeProfGen::instrumentBB = 0; +uint64 MeProfGen::totalFunc = 0; +uint64 MeProfGen::instrumentFunc = 0; bool MeProfGen::firstRun = true; MIRSymbol *MeProfGen::bbCounterTabSym = nullptr; +void MeProfGen::DumpSummary() { + LogInfo::MapleLogger() << "instrument BB " << instrumentBB << " total BB " << totalBB << std::setprecision(2) + << " ratio " << (static_cast(instrumentBB) / totalBB) << "\n"; + LogInfo::MapleLogger() << "instrument func " << instrumentFunc << " total BB " << totalFunc << std::setprecision(2) + << " ratio " << (static_cast(instrumentFunc) / totalFunc) << "\n"; +} + +void MeProfGen::IncTotalFunc() { + totalFunc++; +} + void MeProfGen::Init() { if (!firstRun) { return; @@ -99,6 +112,7 @@ void MeProfGen::InstrumentFunc() { func->AddProfileDesc(hash, counterStart, counterEnd); instrumentBB += instrumentBBs.size(); totalBB += GetAllBBs(); + instrumentFunc++; SaveProfile(); if (dump) { LogInfo::MapleLogger() << "******************after profile gen dump function******************\n"; @@ -111,8 +125,28 @@ void MeProfGen::InstrumentFunc() { } } +// if function have try can't instrument +// if function have infinite loop, can't instrument,because it may cause counter +// overflow +bool MeProfGen::CanInstrument() const { + auto eIt = func->valid_end(); + for (auto bIt = func->valid_begin(); bIt != eIt; ++bIt) { + if (bIt == func->common_entry() || bIt == func->common_exit()) { + continue; + } + auto *bb = *bIt; + if (bb->GetAttributes(kBBAttrIsTry) || bb->GetAttributes(kBBAttrWontExit)) { + return false; + } + } + return true; +} + AnalysisResult *MeDoProfGen::Run(MeFunction *func, MeFuncResultMgr *m, ModuleResultMgr*) { MemPool *tempMp = NewMemPool(); + if (!func->empty()) { + MeProfGen::IncTotalFunc(); + } // function with try can't determine the instrument BB,because // there have critial-edge which can't be split if (func->HasException()) { @@ -121,6 +155,9 @@ AnalysisResult *MeDoProfGen::Run(MeFunction *func, MeFuncResultMgr *m, ModuleRes auto *hMap = static_cast(m->GetAnalysisResult(MeFuncPhase_IRMAP, func)); CHECK_FATAL(hMap != nullptr, "hssamap is nullptr"); MeProfGen profGen(*func, *tempMp, *hMap, DEBUGFUNC(func)); + if (!profGen.CanInstrument()) { + return nullptr; + } profGen.InstrumentFunc(); if (DEBUGFUNC(func)) { diff --git a/src/maple_util/include/profile.h b/src/maple_util/include/profile.h index 24662ed45174d4bbfb06b40c967804e1aafffc36..046a5738d6abc74dfbf7dbc6c29dfcd9150f4539 100644 --- a/src/maple_util/include/profile.h +++ b/src/maple_util/include/profile.h @@ -70,6 +70,8 @@ class Profile { bool CheckDexValid(uint32 idx); void SetProfileMode(); void Dump() const; + void DumpFuncIRProfUseInfo() const; + void SetFuncStatus(const std::string &funcName, bool succ); Profile(); ~Profile() = default; @@ -91,6 +93,7 @@ class Profile { std::unordered_map funcProfData; std::unordered_set &GetMeta(uint8 type); std::unordered_map funcBBProfData; + std::unordered_map funcBBProfUseInfo; std::unordered_map funcDesc; std::vector counterTab; bool CheckProfileHeader(const Header *header) const; diff --git a/src/maple_util/src/profile.cpp b/src/maple_util/src/profile.cpp index eea403bf6c93ee5da56dfabd13593cf7242edef5..a756face677079363168bd229b6b700f315294c5 100644 --- a/src/maple_util/src/profile.cpp +++ b/src/maple_util/src/profile.cpp @@ -479,6 +479,20 @@ std::unordered_set &Profile::GetMeta(uint8 type) { } } +void Profile::SetFuncStatus(const std::string &funcName, bool succ) { + if (succ) { + funcBBProfUseInfo[funcName] = true; + } +} + +void Profile::DumpFuncIRProfUseInfo() const { + if (funcBBProfData.empty()) { + return; + } + LogInfo::MapleLogger() << "ir profile succ " << funcBBProfUseInfo.size() << " total func " + << funcBBProfData.size() << '\n'; +} + void Profile::Dump() const { std::ofstream outfile; outfile.open("prof.dump"); diff --git a/src/mpl2mpl/include/coderelayout.h b/src/mpl2mpl/include/coderelayout.h index 8be4c99076480793b0bddfaa6f4bd2b87eb0c33c..8ab0ecd0fb0d6c9ef45e34ffb12aa6118a56be8f 100644 --- a/src/mpl2mpl/include/coderelayout.h +++ b/src/mpl2mpl/include/coderelayout.h @@ -41,9 +41,6 @@ class CodeReLayout : public FuncOptimizeImpl { void InsertProfileBeforeDread(const StmtNode *stmt, BaseNode *opnd); MIRSymbol *GetorCreateStaticFieldSym(const std::string &fieldName); MIRSymbol *GenStrSym(const std::string &str); - const std::string exeFuncTag = "executedFuncStart"; - const std::string profileStartTag = "#profile_start"; - const std::string profileSummaryTag = "#profile_summary"; std::unordered_map str2SymMap; uint32 layoutCount[static_cast(LayoutType::kLayoutTypeCount)] = {}; }; diff --git a/src/mpl2mpl/src/coderelayout.cpp b/src/mpl2mpl/src/coderelayout.cpp index 914afa99c72df0e2de06966ae75dc6167f244bf5..17f27c17573df029a6eb198c83894f56605b76e6 100644 --- a/src/mpl2mpl/src/coderelayout.cpp +++ b/src/mpl2mpl/src/coderelayout.cpp @@ -181,7 +181,8 @@ void CodeReLayout::Finish() { } if (trace) { for (uint32 i = 0; i < static_cast(LayoutType::kLayoutTypeCount); ++i) { - LogInfo::MapleLogger() << "function in category\t" << i << "\tcount=" << layoutCount[i] << "\n"; + LogInfo::MapleLogger() << "function in category\t" << GetLayoutTypeString(i) + << "\tcount=" << layoutCount[i] << "\n"; } } std::stable_sort(GetMIRModule().GetFunctionList().begin(), GetMIRModule().GetFunctionList().end(), diff --git a/src/mpl2mpl/src/muid_replacement.cpp b/src/mpl2mpl/src/muid_replacement.cpp index fb2f5cf07675f7e043eab946a4fae7c58c3a2251..89b0019fa23350e16a68aa05153e48699dd74223 100644 --- a/src/mpl2mpl/src/muid_replacement.cpp +++ b/src/mpl2mpl/src/muid_replacement.cpp @@ -16,6 +16,7 @@ #include #include "vtable_analysis.h" #include "reflection_analysis.h" +#include "me_profile_gen.h" namespace { // Version for the mpl linker @@ -375,6 +376,9 @@ void MUIDReplacement::GenerateFuncDefTable() { << ", Offset in muid order: " << iter->second.second << "\n"; } } + if (Options::genIRProfile) { + MeProfGen::DumpSummary(); + } // Create funcDefTab, funcInfoTab and funcMuidTab sorted by address, funcMuidIdxTab sorted by muid for (auto keyVal : funcDefArray) { MIRSymbol *funcSymbol = keyVal.first; diff --git a/src/mplfe/common/include/fe_type_manager.h b/src/mplfe/common/include/fe_type_manager.h index 393f6ee7b43990e68e6221d25295401383b9d460..2af592e885bcbfcf9c5c76e028125159f8556766 100644 --- a/src/mplfe/common/include/fe_type_manager.h +++ b/src/mplfe/common/include/fe_type_manager.h @@ -162,6 +162,13 @@ class FETypeManager { MIRFunction *CreateFunction(const std::string &methodName, const std::string &returnTypeName, const std::vector &argTypeNames, bool isVarg, bool isStatic); + // FEIRType GetOrCreate + const FEIRType *GetOrCreateFEIRTypeByName(const std::string &typeName, const GStrIdx &typeNameIdx, + MIRSrcLang argSrcLang = kSrcLangJava); + const FEIRType *GetOrCreateFEIRTypeByName(const GStrIdx &typeNameIdx, MIRSrcLang argSrcLang = kSrcLangJava); + const FEIRType *GetFEIRTypeByName(const std::string &typeName) const; + const FEIRType *GetFEIRTypeByName(const GStrIdx &typeNameIdx) const; + // MCC function void InitMCCFunctions(); MIRFunction *GetMCCFunction(const std::string &funcName) const; @@ -222,6 +229,10 @@ class FETypeManager { std::unordered_map mpltNameFuncMap; std::unordered_map mpltNameStaticFuncMap; + // ---------- FEIRType list ---------- + std::unordered_map nameFEIRTypeMap; + std::list nameFEIRTypeList; + // ---------- MCC function list ---------- std::unordered_map nameMCCFuncMap; MIRFunction *funcMCCGetOrInsertLiteral; diff --git a/src/mplfe/common/include/feir_type.h b/src/mplfe/common/include/feir_type.h index ec6235304e817d0d2ae857061ba141fde5398b83..7543ed6fe0706126e4369a742b0643acc0d051da 100644 --- a/src/mplfe/common/include/feir_type.h +++ b/src/mplfe/common/include/feir_type.h @@ -129,6 +129,10 @@ class FEIRType { return HashImpl(); } + std::string GetTypeName() const { + return GetTypeNameImpl(); + } + static std::map> langConfig; protected: @@ -147,6 +151,9 @@ class FEIRType { virtual bool IsArrayImpl() const = 0; virtual bool IsPreciseImpl() const = 0; virtual bool IsValidImpl() const = 0; + virtual std::string GetTypeNameImpl() const { + return ""; + } FEIRTypeKind kind : 7; bool isZero : 1; @@ -209,6 +216,7 @@ class FEIRTypeDefault : public FEIRType { void SetPrimTypeImpl(PrimType pt) override; MIRType *GenerateMIRTypeInternal(const GStrIdx &argTypeNameIdx, bool usePtr) const; MIRType *GenerateMIRTypeInternal(const GStrIdx &argTypeNameIdx, bool usePtr, PrimType ptyPtr) const; + std::string GetTypeNameImpl() const override; bool IsRefImpl() const override { return dim > 0 || !IsScalarPrimType(primType); diff --git a/src/mplfe/common/src/fe_type_manager.cpp b/src/mplfe/common/src/fe_type_manager.cpp index c92489daefe2edca4f50f8908347a3e63e8689c1..2f1e447d0d7417438b2914c4e93ac8c449ec854d 100644 --- a/src/mplfe/common/src/fe_type_manager.cpp +++ b/src/mplfe/common/src/fe_type_manager.cpp @@ -20,6 +20,7 @@ #include "global_tables.h" #include "fe_timer.h" #include "fe_config_parallel.h" +#include "feir_type_helper.h" namespace maple { const UniqueFEIRType FETypeManager::kPrimFEIRTypeUnknown = std::make_unique(PTY_unknown); @@ -424,6 +425,51 @@ MIRFunction *FETypeManager::CreateFunction(const std::string &methodName, const return CreateFunction(nameIdx, returnType->GetTypeIndex(), argsTypeIdx, isVarg, isStatic); } +const FEIRType *FETypeManager::GetOrCreateFEIRTypeByName(const std::string &typeName, const GStrIdx &typeNameIdx, + MIRSrcLang argSrcLang) { + const FEIRType *feirType = GetFEIRTypeByName(typeName); + if (feirType != nullptr) { + return feirType; + } + MPLFE_PARALLEL_FORBIDDEN(); + UniqueFEIRType uniType; + switch (argSrcLang) { + case kSrcLangJava: + uniType = FEIRTypeHelper::CreateTypeByJavaName(typeName, true, false); + break; + default: + CHECK_FATAL(false, "unsupported language"); + return nullptr; + } + feirType = uniType.get(); + nameFEIRTypeList.push_back(std::move(uniType)); + if (typeNameIdx == 0) { + nameFEIRTypeMap[GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(typeName)] = feirType; + } else { + nameFEIRTypeMap[typeNameIdx] = feirType; + } + return feirType; +} + +const FEIRType *FETypeManager::GetOrCreateFEIRTypeByName(const GStrIdx &typeNameIdx, MIRSrcLang argSrcLang) { + const std::string &typeName = GlobalTables::GetStrTable().GetStringFromStrIdx(typeNameIdx); + return GetOrCreateFEIRTypeByName(typeName, typeNameIdx, argSrcLang); +} + +const FEIRType *FETypeManager::GetFEIRTypeByName(const std::string &typeName) const { + GStrIdx typeNameIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(typeName); + return GetFEIRTypeByName(typeNameIdx); +} + +const FEIRType *FETypeManager::GetFEIRTypeByName(const GStrIdx &typeNameIdx) const { + auto it = nameFEIRTypeMap.find(typeNameIdx); + if (it == nameFEIRTypeMap.end()) { + return nullptr; + } else { + return it->second; + } +} + bool FETypeManager::IsAntiProguardFieldStruct(const GStrIdx &structNameIdx) { return setAntiProguardFieldStructIdx.find(structNameIdx) != setAntiProguardFieldStructIdx.end(); } diff --git a/src/mplfe/common/src/feir_stmt.cpp b/src/mplfe/common/src/feir_stmt.cpp index cf87a4143eb988011aa7bf3b0926ea3e111fc7d7..419c955a54b035b8b78cb9e572ce9446aea47f8f 100644 --- a/src/mplfe/common/src/feir_stmt.cpp +++ b/src/mplfe/common/src/feir_stmt.cpp @@ -764,7 +764,7 @@ std::unique_ptr FEIRExprDRead::CloneImpl() const { } BaseNode *FEIRExprDRead::GenMIRNodeImpl(MIRBuilder &mirBuilder) const { - MIRType *type = varSrc->GetType()->GenerateMIRType(); + MIRType *type = varSrc->GetType()->GenerateMIRTypeAuto(); MIRSymbol *symbol = varSrc->GenerateMIRSymbol(mirBuilder); ASSERT(type != nullptr, "type is nullptr"); AddrofNode *node = mirBuilder.CreateExprDread(*type, *symbol); diff --git a/src/mplfe/common/src/feir_type.cpp b/src/mplfe/common/src/feir_type.cpp index b36e6a382213d53c01c17d71869a41db1f9dcd8b..d8ace12ecafad72a54dd61191e993c5d2f77a714 100644 --- a/src/mplfe/common/src/feir_type.cpp +++ b/src/mplfe/common/src/feir_type.cpp @@ -288,6 +288,25 @@ MIRType *FEIRTypeDefault::GenerateMIRTypeInternal(const GStrIdx &argTypeNameIdx, return usePtr ? FEManager::GetTypeManager().GetOrCreatePointerType(*type, ptyPtr) : type; } +std::string FEIRTypeDefault::GetTypeNameImpl() const { + if (dim == 0) { + return GlobalTables::GetStrTable().GetStringFromStrIdx(typeNameIdx); + } + std::string name; + switch (srcLang) { + case kSrcLangJava: { + for (TypeDim i = 0; i < dim; i++) { + (void)name.append("A"); + } + (void)name.append(GlobalTables::GetStrTable().GetStringFromStrIdx(typeNameIdx)); + return name; + } + default: + CHECK_FATAL(false, "undefined language"); + return ""; + } +} + // ---------- FEIRTypeByName ---------- FEIRTypeByName::FEIRTypeByName(PrimType argPrimType, const std::string &argTypeName, TypeDim argDim) : FEIRTypeDefault(argPrimType, GStrIdx(0), argDim), diff --git a/src/mplfe/jbc_input/include/jbc_attr.h b/src/mplfe/jbc_input/include/jbc_attr.h index 1cc6bc20a57c8dc319da4fe30087c4d2acc5926b..66a735500eb2fd726d4a69aa6ca7c73c8b1a2822 100644 --- a/src/mplfe/jbc_input/include/jbc_attr.h +++ b/src/mplfe/jbc_input/include/jbc_attr.h @@ -25,6 +25,7 @@ namespace maple { namespace jbc { const static uint32 kInvalidPC = UINT32_MAX; +const static uint16 kInvalidPC16 = UINT16_MAX; const static uint32 kMaxPC32 = 0x0000FFFF; enum JBCAttrKind : uint8 { @@ -97,14 +98,20 @@ class JBCAttrRaw : public JBCAttr { }; struct JavaAttrLocalVariableInfoItem { - JavaAttrLocalVariableInfoItem() : slotIdx(0), start(0), length(0), nameIdx(0), typeNameIdx(0), signatureNameIdx(0) {} + JavaAttrLocalVariableInfoItem() + : slotIdx(0), + start(0), + length(0), + nameIdx(0), + feirType(nullptr), + signatureNameIdx(0) {} uint16 slotIdx; uint16 start; uint16 length; - GStrIdx nameIdx; // in java format - GStrIdx typeNameIdx; // in java format - GStrIdx signatureNameIdx; // in java format + GStrIdx nameIdx; // in java format + const FEIRType *feirType; + GStrIdx signatureNameIdx; // in java format }; class JBCAttrLocalVariableInfo { @@ -114,7 +121,7 @@ class JBCAttrLocalVariableInfo { void RegisterItem(const attr::LocalVariableTableItem &itemAttr); void RegisterTypeItem(const attr::LocalVariableTypeTableItem &itemAttr); const JavaAttrLocalVariableInfoItem &GetItemByStart(uint16 slotIdx, uint16 start) const; - uint32 GetStart(uint16 slotIdx, uint16 pc) const; + uint16 GetStart(uint16 slotIdx, uint16 pc) const; std::list EmitToStrings() const; static bool IsInvalidLocalVariableInfoItem(const JavaAttrLocalVariableInfoItem &item); diff --git a/src/mplfe/jbc_input/include/jbc_attr_item.h b/src/mplfe/jbc_input/include/jbc_attr_item.h index 179b774601dbe307691021e7c2e071128ba118ef..53c6a677f875c782805320a1122f003deb98384f 100644 --- a/src/mplfe/jbc_input/include/jbc_attr_item.h +++ b/src/mplfe/jbc_input/include/jbc_attr_item.h @@ -347,9 +347,8 @@ class LocalVariableTableItem : public JBCAttrItem { return constName->GetStrIdx(); } - GStrIdx GetDescStrIdx() const { - CHECK_NULL_FATAL(constDesc); - return constDesc->GetStrIdx(); + const FEIRType *GetFEIRType() const { + return feirType; } protected: @@ -366,7 +365,7 @@ class LocalVariableTableItem : public JBCAttrItem { const JBCConstUTF8 *constName; const JBCConstUTF8 *constDesc; GStrIdx nameIdxMpl; - GStrIdx descNameIdxMpl; + const FEIRType *feirType; }; // LocalVariableTypeTableItem in Attr LocalVariableTypeTable @@ -419,7 +418,6 @@ class LocalVariableTypeTableItem : public JBCAttrItem { const JBCConstUTF8 *constName; const JBCConstUTF8 *constSignature; GStrIdx nameIdxMpl; - GStrIdx signatureNameIdxMpl; }; class ElementValueItem : public JBCAttrItem { diff --git a/src/mplfe/jbc_input/include/jbc_function_context.h b/src/mplfe/jbc_input/include/jbc_function_context.h index 5b5df135881ca7ecde4fa9b03e2f67d7d3fb81ca..94354a919482ace9545bd5e1ac5d3604f9150592 100644 --- a/src/mplfe/jbc_input/include/jbc_function_context.h +++ b/src/mplfe/jbc_input/include/jbc_function_context.h @@ -36,6 +36,8 @@ class JBCFunctionContext { int32 RegisterJsrSlotRetAddr(uint16 slotIdx, uint32 nextPC); void ArrangeStmts(); + const FEIRType *GetSlotType(uint16 slotIdx, uint32 pc) const; + const FEIRType *GetSlotType(uint16 slotIdx) const; JBCStack2FEHelper &GetStack2FEHelper() { return stack2feHelper; @@ -114,6 +116,14 @@ class JBCFunctionContext { return code->GetLocalVarInfo(); } + void SetCurrPC(uint32 pc) { + currPC = pc; + } + + uint32 GetCurrPC() const { + return currPC; + } + private: const jbc::JBCConstPool &constPool; JBCStack2FEHelper &stack2feHelper; @@ -126,6 +136,7 @@ class JBCFunctionContext { std::map mapPCStmtLOC; // key: locPC, value: stmt std::map mapPCCommentStmt; // key: commentPC, value: stmt std::map> mapJsrSlotRetAddr; // key: slotIdx, value: map + uint32 currPC = 0; }; // class JBCFunctionContext } // namespace maple #endif // MPLFE_INCLUDE_JBC_INPUT_JBC_FUNCTION_CONTEXT_H \ No newline at end of file diff --git a/src/mplfe/jbc_input/include/jbc_stack2fe_helper.h b/src/mplfe/jbc_input/include/jbc_stack2fe_helper.h index b69644be22911f9b56f83749028847dc34ff674b..df9de3aacbfb288a35233f4480f9535ab3891dd8 100644 --- a/src/mplfe/jbc_input/include/jbc_stack2fe_helper.h +++ b/src/mplfe/jbc_input/include/jbc_stack2fe_helper.h @@ -37,6 +37,7 @@ class JBCStack2FEHelper { UniqueFEIRStmt PushItem(UniqueFEIRExpr expr, PrimType pty, bool hasException = false); UniqueFEIRStmt PushItem(UniqueFEIRExpr expr, UniqueFEIRType type, bool hasException = false); UniqueFEIRVar PopItem(PrimType pty); + UniqueFEIRVar PopItem(UniqueFEIRType type); UniqueFEIRVar PopItem(bool isWide, PrimType &pty); UniqueFEIRVar PopItemAuto(PrimType &pty); bool Swap(); diff --git a/src/mplfe/jbc_input/include/jbc_stmt.h b/src/mplfe/jbc_input/include/jbc_stmt.h index 4ce57018d332e9d341c2ff78102583ccf6e8a1b4..b6821794d78ed6517e631dd235c9669267c42b59 100644 --- a/src/mplfe/jbc_input/include/jbc_stmt.h +++ b/src/mplfe/jbc_input/include/jbc_stmt.h @@ -49,11 +49,13 @@ class JBCStmtKindHelper { class JBCStmt : public GeneralStmt { public: explicit JBCStmt(JBCStmtKind argKind) - : kind(argKind) {} + : kind(argKind), + pc(UINT32_MAX) {} JBCStmt(GeneralStmtKind argGenKind, JBCStmtKind argKind) : GeneralStmt(argGenKind), - kind(argKind) {} + kind(argKind), + pc(UINT32_MAX) {} virtual ~JBCStmt() = default; std::list EmitToFEIR(JBCFunctionContext &context, bool &success) const { @@ -72,10 +74,19 @@ class JBCStmt : public GeneralStmt { return kind == JBCStmtKind::kJBCStmtInstBranch || kind == JBCStmtKind::kJBCStmtInstBranchRet; } + void SetPC(uint32 argPC) { + pc = argPC; + } + + uint32 GetPC() const { + return pc; + } + protected: virtual std::list EmitToFEIRImpl(JBCFunctionContext &context, bool &success) const = 0; JBCStmtKind kind; + uint32 pc; }; class JBCStmtInst : public JBCStmt { diff --git a/src/mplfe/jbc_input/src/jbc_attr.cpp b/src/mplfe/jbc_input/src/jbc_attr.cpp index 2c295ed3e962eb8940fb961ea0b79e2bff840543..cd0a77140c86661650d32de97c437a119f709829 100644 --- a/src/mplfe/jbc_input/src/jbc_attr.cpp +++ b/src/mplfe/jbc_input/src/jbc_attr.cpp @@ -172,11 +172,11 @@ void JBCAttrLocalVariableInfo::RegisterItem(const attr::LocalVariableTableItem & itemRef.start = startPC; itemRef.length = length; itemRef.nameIdx = itemAttr.GetNameStrIdx(); - itemRef.typeNameIdx = itemAttr.GetDescStrIdx(); + itemRef.feirType = itemAttr.GetFEIRType(); } else { - if (item->start == startPC && item->length == length && item->nameIdx == itemAttr.GetDescStrIdx()) { - CHECK_FATAL(item->typeNameIdx == 0, "Item already defined"); - item->typeNameIdx = itemAttr.GetDescStrIdx(); + if (item->start == startPC && item->length == length && item->nameIdx == itemAttr.GetNameStrIdx()) { + CHECK_FATAL(item->feirType == nullptr, "Item already defined"); + item->feirType = itemAttr.GetFEIRType(); } else { CHECK_FATAL(false, "Item mismatch in RegisterItem()"); } @@ -214,8 +214,11 @@ const JavaAttrLocalVariableInfoItem &JBCAttrLocalVariableInfo::GetItemByStart(ui } std::map, JavaAttrLocalVariableInfoItem>::const_iterator it = itemMap.find(std::make_pair(slotIdx, itemPCStart)); - CHECK_FATAL(it != itemMap.end(), "Item@%d not found", start); - return it->second; + if (it != itemMap.end()) { + return it->second; + } else { + return kInvalidInfoItem; + } } JavaAttrLocalVariableInfoItem *JBCAttrLocalVariableInfo::GetItemByStartInternal(uint16 slotIdx, uint16 start) { @@ -229,12 +232,12 @@ JavaAttrLocalVariableInfoItem *JBCAttrLocalVariableInfo::GetItemByStartInternal( return &(it->second); } -uint32 JBCAttrLocalVariableInfo::GetStart(uint16 slotIdx, uint16 pc) const { +uint16 JBCAttrLocalVariableInfo::GetStart(uint16 slotIdx, uint16 pc) const { MapleMap>::const_iterator it = slotStartMap.find(slotIdx); if (it == slotStartMap.end()) { - return jbc::kInvalidPC; + return jbc::kInvalidPC16; } - uint32 startLast = jbc::kInvalidPC; + uint16 startLast = jbc::kInvalidPC16; for (uint16 start : it->second) { if (pc == start) { return start; @@ -257,7 +260,7 @@ std::list JBCAttrLocalVariableInfo::EmitToStrings() const { ss << "start=" << item.start << ", "; ss << "lenght=" << item.length << ", "; ss << "name=\'" << GlobalTables::GetStrTable().GetStringFromStrIdx(item.nameIdx) << "\', "; - ss << "type=\'" << GlobalTables::GetStrTable().GetStringFromStrIdx(item.typeNameIdx) << "\', "; + ss << "type=\'" << item.feirType->GetTypeName() << "\', "; ss << "signature=\'" << GlobalTables::GetStrTable().GetStringFromStrIdx(item.signatureNameIdx) << "\'"; ans.push_back(ss.str()); } @@ -266,7 +269,7 @@ std::list JBCAttrLocalVariableInfo::EmitToStrings() const { } bool JBCAttrLocalVariableInfo::IsInvalidLocalVariableInfoItem(const JavaAttrLocalVariableInfoItem &item) { - return (item.nameIdx == 0 && item.typeNameIdx == 0 && item.signatureNameIdx == 0); + return (item.nameIdx == 0 && item.feirType == nullptr && item.signatureNameIdx == 0); } void JBCAttrLocalVariableInfo::AddSlotStartMap(uint16 slotIdx, uint16 startPC) { @@ -282,7 +285,7 @@ void JBCAttrLocalVariableInfo::AddSlotStartMap(uint16 slotIdx, uint16 startPC) { void JBCAttrLocalVariableInfo::CheckItemAvaiable(uint16 slotIdx, uint16 start) const { uint32 itemPCStart = GetStart(slotIdx, start); - if (itemPCStart == jbc::kInvalidPC) { + if (itemPCStart == jbc::kInvalidPC16) { return; } CHECK_FATAL(itemPCStart <= jbc::kMaxPC32, "Invalid PC"); diff --git a/src/mplfe/jbc_input/src/jbc_attr_item.cpp b/src/mplfe/jbc_input/src/jbc_attr_item.cpp index 6f03388a30a9827cb9dea09339fd507df6cdcbb1..6dc7f63485bc36771a9bf53678887a7247d296ae 100644 --- a/src/mplfe/jbc_input/src/jbc_attr_item.cpp +++ b/src/mplfe/jbc_input/src/jbc_attr_item.cpp @@ -13,6 +13,7 @@ * See the Mulan PSL v1 for more details. */ #include "jbc_attr_item.h" +#include "fe_manager.h" namespace maple { namespace jbc { @@ -455,11 +456,12 @@ LocalVariableTableItem::LocalVariableTableItem() constName(nullptr), constDesc(nullptr), nameIdxMpl(0), - descNameIdxMpl(0) {} + feirType(nullptr) {} LocalVariableTableItem::~LocalVariableTableItem() { constName = nullptr; constDesc = nullptr; + feirType = nullptr; } bool LocalVariableTableItem::ParseFileImpl(MapleAllocator &allocator, BasicIORead &io) { @@ -479,7 +481,8 @@ bool LocalVariableTableItem::PreProcessImpl(const JBCConstPool &constPool) { return false; } nameIdxMpl = GetOrCreateGStrIdx(constName->GetString()); - descNameIdxMpl = GetOrCreateGStrIdxWithMangler(constDesc->GetString()); + feirType = FEManager::GetTypeManager().GetOrCreateFEIRTypeByName(NameMangler::EncodeName(constDesc->GetString()), + GStrIdx(0), kSrcLangJava); return true; } @@ -490,7 +493,14 @@ SimpleXMLElem *LocalVariableTableItem::GenXmlElemImpl(MapleAllocator &allocator, // ---------- LocalVariableTypeTableItem ---------- LocalVariableTypeTableItem::LocalVariableTypeTableItem() - : startPC(0), length(0), nameIdx(0), signatureIdx(0), index(0), constName(nullptr), constSignature(nullptr) {} + : startPC(0), + length(0), + nameIdx(0), + signatureIdx(0), + index(0), + constName(nullptr), + constSignature(nullptr), + nameIdxMpl(0) {} LocalVariableTypeTableItem::~LocalVariableTypeTableItem() { constName = nullptr; @@ -515,7 +525,6 @@ bool LocalVariableTypeTableItem::PreProcessImpl(const JBCConstPool &constPool) { return false; } nameIdxMpl = GetOrCreateGStrIdx(constName->GetString()); - signatureNameIdxMpl = GetOrCreateGStrIdxWithMangler(constSignature->GetString()); return true; } diff --git a/src/mplfe/jbc_input/src/jbc_function.cpp b/src/mplfe/jbc_input/src/jbc_function.cpp index a6481c0ad4fdb6b024c96ee18ed8d486e7824a0c..c45288a171c4f3d2e157dd0befd68815e453771a 100644 --- a/src/mplfe/jbc_input/src/jbc_function.cpp +++ b/src/mplfe/jbc_input/src/jbc_function.cpp @@ -381,6 +381,7 @@ bool JBCFunction::BuildStmtFromInstruction(const jbc::JBCAttrCode &code) { stmt = RegisterGeneralStmt(std::make_unique(*op)); break; } + static_cast(stmt)->SetPC(pc); genStmtTail->InsertBefore(stmt); context.UpdateMapPCStmtInst(pc, stmt); } diff --git a/src/mplfe/jbc_input/src/jbc_function_context.cpp b/src/mplfe/jbc_input/src/jbc_function_context.cpp index 9eba3336506425f3250330417ee2dd24b521816d..cf0e3dcea6a80930727d68b36db4b5ec0d43a5de 100644 --- a/src/mplfe/jbc_input/src/jbc_function_context.cpp +++ b/src/mplfe/jbc_input/src/jbc_function_context.cpp @@ -62,4 +62,22 @@ void JBCFunctionContext::ArrangeStmts() { } } } + +const FEIRType *JBCFunctionContext::GetSlotType(uint16 slotIdx, uint32 pc) const { + CHECK_NULL_FATAL(code); + const jbc::JBCAttrLocalVariableInfo &localVarInfo = code->GetLocalVarInfo(); + CHECK_FATAL(pc < UINT16_MAX, "pc out of range"); + uint16 startPC = localVarInfo.GetStart(slotIdx, static_cast(pc)); + const jbc::JavaAttrLocalVariableInfoItem &info = localVarInfo.GetItemByStart(slotIdx, startPC); + return info.feirType; +} + +const FEIRType *JBCFunctionContext::GetSlotType(uint16 slotIdx) const { + CHECK_NULL_FATAL(code); + const jbc::JBCAttrLocalVariableInfo &localVarInfo = code->GetLocalVarInfo(); + CHECK_FATAL(currPC < UINT16_MAX, "pc out of range"); + uint16 startPC = localVarInfo.GetStart(slotIdx, static_cast(currPC)); + const jbc::JavaAttrLocalVariableInfoItem &info = localVarInfo.GetItemByStart(slotIdx, startPC); + return info.feirType; +} } // namespace maple \ No newline at end of file diff --git a/src/mplfe/jbc_input/src/jbc_stack2fe_helper.cpp b/src/mplfe/jbc_input/src/jbc_stack2fe_helper.cpp index cb2c670793f094c0c2c36ac8cc9f372d7d51ccda..e66398f892fc49d457d9e37ec74ada9fe2531554 100644 --- a/src/mplfe/jbc_input/src/jbc_stack2fe_helper.cpp +++ b/src/mplfe/jbc_input/src/jbc_stack2fe_helper.cpp @@ -148,6 +148,12 @@ UniqueFEIRVar JBCStack2FEHelper::PopItem(PrimType pty) { } } +UniqueFEIRVar JBCStack2FEHelper::PopItem(UniqueFEIRType type) { + UniqueFEIRVar var = PopItem(type->GetPrimType()); + var->SetType(std::move(type)); + return var; +} + UniqueFEIRVar JBCStack2FEHelper::PopItem(bool isWide, PrimType &pty) { size_t size = stack.size(); pty = PTY_unknown; diff --git a/src/mplfe/jbc_input/src/jbc_stmt.cpp b/src/mplfe/jbc_input/src/jbc_stmt.cpp index ed8f845a3d15e619db639902db0faee107ffd96e..3d847f02a5cf38502a13fefad3ccdf5df7da044c 100644 --- a/src/mplfe/jbc_input/src/jbc_stmt.cpp +++ b/src/mplfe/jbc_input/src/jbc_stmt.cpp @@ -385,8 +385,15 @@ std::list JBCStmtInst::EmitToFEIRForOpLoad(JBCFunctionContext &c jbc::JBCPrimType stackOutType = op.GetOutputTypesToStack(); PrimType pty = JBCStack2FEHelper::JBCStackItemTypeToPrimType(stackOutType); uint32 regNum = stack2feHelper.GetRegNumForSlot(opLoad.GetSlotIdx()); - UniqueFEIRVar var = FEIRBuilder::CreateVarReg(regNum, pty); + const FEIRType *slotType = context.GetSlotType(opLoad.GetSlotIdx(), pc); + UniqueFEIRVar var; UniqueFEIRVar varStack = stack2feHelper.PushItem(pty); + if (slotType != nullptr) { + var = FEIRBuilder::CreateVarReg(regNum, slotType->Clone()); + varStack->SetType(slotType->Clone()); + } else { + var = FEIRBuilder::CreateVarReg(regNum, pty); + } success = success && (varStack != nullptr); UniqueFEIRExpr expr = FEIRBuilder::CreateExprDRead(std::move(var)); UniqueFEIRStmt stmt = FEIRBuilder::CreateStmtDAssign(std::move(varStack), std::move(expr)); @@ -409,8 +416,17 @@ std::list JBCStmtInst::EmitToFEIRForOpStore(JBCFunctionContext & CHECK_FATAL(stackInTypes.size() == 1, "store op need one stack opnd"); PrimType pty = JBCStack2FEHelper::JBCStackItemTypeToPrimType(stackInTypes[0]); uint32 regSlot = stack2feHelper.GetRegNumForSlot(opStore.GetSlotIdx()); - UniqueFEIRVar varDst = FEIRBuilder::CreateVarReg(regSlot, pty); - UniqueFEIRVar varSrc = stack2feHelper.PopItem(pty); + const FEIRType *slotType = context.GetSlotType(opStore.GetSlotIdx(), pc); + UniqueFEIRVar varDst; + UniqueFEIRVar varSrc; + if (slotType != nullptr) { + varDst = FEIRBuilder::CreateVarReg(regSlot, slotType->Clone()); + varSrc = stack2feHelper.PopItem(pty); + varSrc->SetType(slotType->Clone()); + } else { + varDst = FEIRBuilder::CreateVarReg(regSlot, pty); + varSrc = stack2feHelper.PopItem(pty); + } if (varSrc == nullptr) { success = false; return ans;