diff --git a/src/bin/jbc2mpl b/src/bin/jbc2mpl index 511c5e1f1085e930806672b0ec9b0a9b62b90d89..66459618f1ed223d8460ef281153e95b26dbed92 100755 Binary files a/src/bin/jbc2mpl and b/src/bin/jbc2mpl differ diff --git a/src/bin/maple b/src/bin/maple index a321de780c0de81e94ed79e6d4371890bafbf2f0..f319477cef40ee3e4a297c6aebd75b9bdf0e5a03 100755 Binary files a/src/bin/maple and b/src/bin/maple differ diff --git a/src/bin/mplcg b/src/bin/mplcg index b884c84c588612a925649a40df39c2264f6158b1..ec743933d800d5046eb4d33e72d8873d3adba98e 100755 Binary files a/src/bin/mplcg and b/src/bin/mplcg differ diff --git a/src/maple_ir/include/mir_builder.h b/src/maple_ir/include/mir_builder.h index 4db4ac89450a3c972a89b32190aba4062c314774..f58b657662e5a1dce98aad99be62c299fb34a322 100644 --- a/src/maple_ir/include/mir_builder.h +++ b/src/maple_ir/include/mir_builder.h @@ -95,7 +95,7 @@ class MIRBuilder { } MIRFunction *GetOrCreateFunction(const std::string&, TyIdx); - MIRFunction *GetFunctionFromSymbol(MIRSymbol *funcst) const; + MIRFunction *GetFunctionFromSymbol(const MIRSymbol &funcst) const; MIRFunction *GetFunctionFromStidx(StIdx stIdx); MIRFunction *GetFunctionFromName(const std::string&); // For compiler-generated metadata struct @@ -132,14 +132,14 @@ class MIRBuilder { } MIRSymbol *GetSymbolFromEnclosingScope(StIdx stIdx); - virtual MIRSymbol *GetOrCreateLocalDecl(const std::string &str, MIRType *type); + virtual MIRSymbol *GetOrCreateLocalDecl(const std::string &str, MIRType &type); MIRSymbol *GetLocalDecl(const std::string &str); - MIRSymbol *CreateLocalDecl(const std::string &str, const MIRType *type); + MIRSymbol *CreateLocalDecl(const std::string &str, const MIRType &type); MIRSymbol *GetOrCreateGlobalDecl(const std::string &str, const MIRType *type); MIRSymbol *GetGlobalDecl(const std::string &str); MIRSymbol *GetDecl(const std::string &str); MIRSymbol *CreateGlobalDecl(const std::string &str, const MIRType *type, MIRStorageClass sc = kScGlobal); - MIRSymbol *GetOrCreateDeclInFunc(const std::string &str, const MIRType *type, MIRFunction *func); + MIRSymbol *GetOrCreateDeclInFunc(const std::string &str, const MIRType &type, MIRFunction &func); // for creating Expression ConstvalNode *CreateIntConst(int64, PrimType); ConstvalNode *CreateFloatConst(float val); @@ -279,8 +279,9 @@ class MIRBuilder { virtual MapleAllocator *GetCurrentFuncCodeMpAllocator(); private: - MIRSymbol *GetOrCreateDecl(const std::string &str, const MIRType *type, MIRFunction *func, bool isLocal, - bool &created); + MIRSymbol *GetOrCreateGlobalDecl(const std::string &str, const TyIdx &tyIdx, bool &created) const; + MIRSymbol *GetOrCreateLocalDecl(const std::string &str, const TyIdx &tyIdx, MIRSymbolTable &symbolTable, + bool &created) const; bool IsValidCallReturn(const MIRSymbol &ret) const { return ret.GetStorageClass() == kScAuto || ret.GetStorageClass() == kScFormal || ret.GetStorageClass() == kScExtern || ret.GetStorageClass() == kScGlobal; diff --git a/src/maple_ir/src/mir_builder.cpp b/src/maple_ir/src/mir_builder.cpp index 65f9a2fe93c829b4077d269e1594bfffcdbb5f24..f558920d731dda151f9564c1c62ab9754925dff8 100644 --- a/src/maple_ir/src/mir_builder.cpp +++ b/src/maple_ir/src/mir_builder.cpp @@ -274,74 +274,64 @@ MIRFunction *MIRBuilder::GetOrCreateFunction(const std::string &str, TyIdx retTy return fn; } -MIRFunction *MIRBuilder::GetFunctionFromSymbol(MIRSymbol *funcSymbol) const { - if (!funcSymbol) { - return nullptr; - } - ASSERT(funcSymbol->GetSKind() == kStFunc, "Symbol %s is not a function symbol", funcSymbol->GetName().c_str()); - return funcSymbol->GetFunction(); +MIRFunction *MIRBuilder::GetFunctionFromSymbol(const MIRSymbol &funcSymbol) const { + ASSERT(funcSymbol.GetSKind() == kStFunc, "Symbol %s is not a function symbol", funcSymbol.GetName().c_str()); + return funcSymbol.GetFunction(); } MIRFunction *MIRBuilder::GetFunctionFromName(const std::string &str) { - return GetFunctionFromSymbol( - GlobalTables::GetGsymTable().GetSymbolFromStrIdx(GlobalTables::GetStrTable().GetStrIdxFromName(str))); + auto *funcSymbol = + GlobalTables::GetGsymTable().GetSymbolFromStrIdx(GlobalTables::GetStrTable().GetStrIdxFromName(str)); + return funcSymbol != nullptr ? GetFunctionFromSymbol(*funcSymbol) : nullptr; } MIRFunction *MIRBuilder::GetFunctionFromStidx(StIdx stIdx) { - return GetFunctionFromSymbol(GlobalTables::GetGsymTable().GetSymbolFromStidx(stIdx.Idx())); + auto *funcSymbol = GlobalTables::GetGsymTable().GetSymbolFromStidx(stIdx.Idx()); + return funcSymbol != nullptr ? GetFunctionFromSymbol(*funcSymbol) : nullptr; } -MIRSymbol *MIRBuilder::GetOrCreateDecl(const std::string &str, const MIRType *type, MIRFunction *func, bool isLocal, - bool &created) { - MIRSymbolTable *symbolTable = nullptr; - if (isLocal) { - if (func) { - symbolTable = func->GetSymTab(); - } else { - MIRFunction *currentFunctionInner = GetCurrentFunction(); - CHECK_FATAL(currentFunctionInner != nullptr, "null ptr check"); - symbolTable = currentFunctionInner->GetSymTab(); - } - ASSERT(symbolTable != nullptr, "symbol_table is null in MIRBuilder::GetOrCreateDecl"); - } +MIRSymbol *MIRBuilder::GetOrCreateGlobalDecl(const std::string &str, const TyIdx &tyIdx, bool &created) const { GStrIdx strIdx = GetStringIndex(str); if (strIdx != 0) { - StIdx stidx = - isLocal ? symbolTable->GetStIdxFromStrIdx(strIdx) : GlobalTables::GetGsymTable().GetStIdxFromStrIdx(strIdx); + StIdx stidx = GlobalTables::GetGsymTable().GetStIdxFromStrIdx(strIdx); if (stidx.Idx() != 0) { created = false; - return isLocal ? symbolTable->GetSymbolFromStIdx(stidx.Idx()) - : GlobalTables::GetGsymTable().GetSymbolFromStidx(stidx.Idx()); + return GlobalTables::GetGsymTable().GetSymbolFromStidx(stidx.Idx()); } } created = true; strIdx = GetOrCreateStringIndex(str); - MIRSymbol *st = - isLocal ? symbolTable->CreateSymbol(kScopeLocal) : GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal); + MIRSymbol *st = GlobalTables::GetGsymTable().CreateSymbol(kScopeGlobal); st->SetNameStrIdx(strIdx); - st->SetTyIdx(type->GetTypeIndex()); - if (isLocal) { - (void)symbolTable->AddToStringSymbolMap(st); - } else { - (void)GlobalTables::GetGsymTable().AddToStringSymbolMap(st); - } + st->SetTyIdx(tyIdx); + (void)GlobalTables::GetGsymTable().AddToStringSymbolMap(st); return st; } -MIRSymbol *MIRBuilder::GetOrCreateDeclInFunc(const std::string &str, const MIRType *type, MIRFunction *func) { - bool isCreated = false; - MIRSymbol *st = GetOrCreateDecl(str, type, func, true, isCreated); - if (isCreated) { - st->SetStorageClass(kScAuto); - st->SetSKind(kStVar); +MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, const TyIdx &tyIdx, MIRSymbolTable &symbolTable, + bool &created) const { + GStrIdx strIdx = GetStringIndex(str); + if (strIdx != 0) { + StIdx stidx = symbolTable.GetStIdxFromStrIdx(strIdx); + if (stidx.Idx() != 0) { + created = false; + return symbolTable.GetSymbolFromStIdx(stidx.Idx()); + } } + created = true; + strIdx = GetOrCreateStringIndex(str); + MIRSymbol *st = symbolTable.CreateSymbol(kScopeLocal); + st->SetNameStrIdx(strIdx); + st->SetTyIdx(tyIdx); + (void)symbolTable.AddToStringSymbolMap(st); return st; } -MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, MIRType *type) { +MIRSymbol *MIRBuilder::GetOrCreateDeclInFunc(const std::string &str, const MIRType &type, MIRFunction &func) { + MIRSymbolTable *symbolTable = func.GetSymTab(); + ASSERT(symbolTable != nullptr, "symbol_table is null"); bool isCreated = false; - MIRSymbol *st = GetOrCreateDecl(str, type, nullptr, true, isCreated); - ASSERT(st != nullptr, "st is null"); + MIRSymbol *st = GetOrCreateLocalDecl(str, type.GetTypeIndex(), *symbolTable, isCreated); if (isCreated) { st->SetStorageClass(kScAuto); st->SetSKind(kStVar); @@ -349,14 +339,20 @@ MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, MIRType *typ return st; } -MIRSymbol *MIRBuilder::CreateLocalDecl(const std::string &str, const MIRType *type) { +MIRSymbol *MIRBuilder::GetOrCreateLocalDecl(const std::string &str, MIRType &type) { + MIRFunction *currentFunc = GetCurrentFunction(); + CHECK_FATAL(currentFunc != nullptr, "null ptr check"); + return GetOrCreateDeclInFunc(str, type, *currentFunc); +} + +MIRSymbol *MIRBuilder::CreateLocalDecl(const std::string &str, const MIRType &type) { GStrIdx stridx = GetOrCreateStringIndex(str); MIRFunction *currentFunctionInner = GetCurrentFunction(); CHECK_FATAL(currentFunctionInner != nullptr, "null ptr check"); MIRSymbolTable *symbolTable = currentFunctionInner->GetSymTab(); MIRSymbol *st = symbolTable->CreateSymbol(kScopeLocal); st->SetNameStrIdx(stridx); - st->SetTyIdx(type->GetTypeIndex()); + st->SetTyIdx(type.GetTypeIndex()); (void)symbolTable->AddToStringSymbolMap(st); st->SetStorageClass(kScAuto); st->SetSKind(kStVar); @@ -418,7 +414,7 @@ MIRSymbol *MIRBuilder::CreateGlobalDecl(const std::string &str, const MIRType *t MIRSymbol *MIRBuilder::GetOrCreateGlobalDecl(const std::string &str, const MIRType *type) { bool isCreated = false; - MIRSymbol *st = GetOrCreateDecl(str, type, nullptr, false, isCreated); + MIRSymbol *st = GetOrCreateGlobalDecl(str, type->GetTypeIndex(), isCreated); if (isCreated) { st->SetStorageClass(kScGlobal); st->SetSKind(kStVar); diff --git a/src/maple_me/src/me_cfg.cpp b/src/maple_me/src/me_cfg.cpp index 8adfeeb837e59d3ccddc5727c174c7658c36736b..eb6c654bc2c9911d6131d86250c117442b5cbf5a 100644 --- a/src/maple_me/src/me_cfg.cpp +++ b/src/maple_me/src/me_cfg.cpp @@ -320,7 +320,7 @@ void MeCFG::FixMirCFG() { func.GetMirFunc()->IncTempCount(); std::string tempStr = std::string("tempRet").append(std::to_string(func.GetMirFunc()->GetTempCount())); MIRBuilder *builder = func.GetMirFunc()->GetModule()->GetMIRBuilder(); - MIRSymbol *targetSt = builder->GetOrCreateLocalDecl(tempStr.c_str(), sym->GetType()); + MIRSymbol *targetSt = builder->GetOrCreateLocalDecl(tempStr.c_str(), *sym->GetType()); targetSt->ResetIsDeleted(); if (stmt->GetOpCode() == OP_dassign) { BaseNode *rhs = static_cast(stmt)->GetRHS(); diff --git a/src/maple_me/src/me_rc_lowering.cpp b/src/maple_me/src/me_rc_lowering.cpp index 5cc06f9b975d89a9f2fb5c36510cf7fd95c92ba7..70deadecc6196f6a5f7f4a1583ca041e15c2c0cc 100644 --- a/src/maple_me/src/me_rc_lowering.cpp +++ b/src/maple_me/src/me_rc_lowering.cpp @@ -820,7 +820,7 @@ void RCLowering::Finish() { OriginalSt *RCLowering::RetrieveOSt(const std::string &name, bool isLocalrefvar) const { MIRSymbol *backupSym = mirModule.GetMIRBuilder()->GetOrCreateLocalDecl( - name, GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(PTY_ptr))); // use PTY_ptr for temp + name, *GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(PTY_ptr))); // use PTY_ptr for temp backupSym->SetIsTmp(true); if (isLocalrefvar) { backupSym->SetLocalRefVar(); diff --git a/src/mpl2mpl/src/muid_replacement.cpp b/src/mpl2mpl/src/muid_replacement.cpp index 5a3b391ffdfb5ff544cd9d149b7dd0b587be9e81..57fa3cf87ff3f437ae64256f543f7107c8aa3481 100644 --- a/src/mpl2mpl/src/muid_replacement.cpp +++ b/src/mpl2mpl/src/muid_replacement.cpp @@ -852,7 +852,7 @@ void MUIDReplacement::ReplaceDirectInvokeOrAddroffunc(MIRFunction *currentFunc, currentFunc->GetBody()->InsertBefore(stmt, funcPtrPregAssign); readFuncPtr = builder->CreateExprRegread(PTY_ptr, funcPtrPreg); } else { - funcPtrSym = builder->GetOrCreateLocalDecl(kMuidSymPtrStr, GlobalTables::GetTypeTable().GetVoidPtr()); + funcPtrSym = builder->GetOrCreateLocalDecl(kMuidSymPtrStr, *GlobalTables::GetTypeTable().GetVoidPtr()); DassignNode *addrNode = builder->CreateStmtDassign(funcPtrSym, 0, ireadPtrExpr); currentFunc->GetBody()->InsertBefore(stmt, addrNode); readFuncPtr = builder->CreateExprDread(funcPtrSym); @@ -919,7 +919,7 @@ void MUIDReplacement::ReplaceDassign(MIRFunction *currentFunc, DassignNode *dass currentFunc->GetBody()->InsertBefore(dassignNode, symPtrPregAssign); destExpr = builder->CreateExprRegread(PTY_ptr, symPtrPreg); } else { - symPtrSym = builder->GetOrCreateLocalDecl(kMuidFuncPtrStr, mVoidPtr); + symPtrSym = builder->GetOrCreateLocalDecl(kMuidFuncPtrStr, *mVoidPtr); DassignNode *addrNode = builder->CreateStmtDassign(symPtrSym, 0, ireadPtrExpr); currentFunc->GetBody()->InsertBefore(dassignNode, addrNode); destExpr = builder->CreateExprDread(symPtrSym);