From 4c3707e40f946a4dbf57467973e95e21c2450632 Mon Sep 17 00:00:00 2001 From: Brice Dobry Date: Thu, 11 Mar 2021 22:52:29 -0500 Subject: [PATCH] Get mempool from module when not in a function This allows the same builder methods to be used whether currently in a function or not (global initialization). --- src/mapleall/maple_ir/include/mir_builder.h | 2 ++ src/mapleall/maple_ir/src/mir_builder.cpp | 30 +++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/mapleall/maple_ir/include/mir_builder.h b/src/mapleall/maple_ir/include/mir_builder.h index be6e53c06e..1cc176d672 100644 --- a/src/mapleall/maple_ir/include/mir_builder.h +++ b/src/mapleall/maple_ir/include/mir_builder.h @@ -149,6 +149,7 @@ class MIRBuilder { MIRStorageClass sc = kScGlobal); MIRSymbol *GetOrCreateDeclInFunc(const std::string &str, const MIRType &type, MIRFunction &func); // for creating Expression + ConstvalNode *CreateConstval(MIRConst *constVal); ConstvalNode *CreateIntConst(int64, PrimType); ConstvalNode *CreateFloatConst(float val); ConstvalNode *CreateDoubleConst(double val); @@ -285,6 +286,7 @@ class MIRBuilder { AddrofNode *CreateDread(const MIRSymbol &st, PrimType pty); virtual MemPool *GetCurrentFuncCodeMp(); virtual MapleAllocator *GetCurrentFuncCodeMpAllocator(); + virtual MemPool *GetCurrentFuncDataMp(); virtual void GlobalLock() {} virtual void GlobalUnlock() {} diff --git a/src/mapleall/maple_ir/src/mir_builder.cpp b/src/mapleall/maple_ir/src/mir_builder.cpp index b6f232a3a4..67da936926 100644 --- a/src/mapleall/maple_ir/src/mir_builder.cpp +++ b/src/mapleall/maple_ir/src/mir_builder.cpp @@ -468,6 +468,10 @@ MIRSymbol *MIRBuilder::CreatePregFormalSymbol(TyIdx tyIdx, PregIdx pRegIdx, MIRF return MIRSymbolBuilder::Instance().CreatePregFormalSymbol(tyIdx, pRegIdx, func); } +ConstvalNode *MIRBuilder::CreateConstval(MIRConst *mirConst) { + return GetCurrentFuncCodeMp()->New(mirConst->GetType().GetPrimType(), mirConst); +} + ConstvalNode *MIRBuilder::CreateIntConst(int64 val, PrimType pty) { auto *mirConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(val, *GlobalTables::GetTypeTable().GetPrimType(pty)); @@ -475,22 +479,19 @@ ConstvalNode *MIRBuilder::CreateIntConst(int64 val, PrimType pty) { } ConstvalNode *MIRBuilder::CreateFloatConst(float val) { - MIRFunction *currentFunctionInner = GetCurrentFunctionNotNull(); - auto *mirConst = currentFunctionInner->GetDataMemPool()->New( + auto *mirConst = GetCurrentFuncDataMp()->New( val, *GlobalTables::GetTypeTable().GetPrimType(PTY_f32)); return GetCurrentFuncCodeMp()->New(PTY_f32, mirConst); } ConstvalNode *MIRBuilder::CreateDoubleConst(double val) { - MIRFunction *currentFunctionInner = GetCurrentFunctionNotNull(); - auto *mirConst = currentFunctionInner->GetDataMemPool()->New( + auto *mirConst = GetCurrentFuncDataMp()->New( val, *GlobalTables::GetTypeTable().GetPrimType(PTY_f64)); return GetCurrentFuncCodeMp()->New(PTY_f64, mirConst); } ConstvalNode *MIRBuilder::CreateFloat128Const(const uint64 *val) { - MIRFunction *currentFunctionInner = GetCurrentFunctionNotNull(); - auto *mirConst = currentFunctionInner->GetDataMemPool()->New( + auto *mirConst = GetCurrentFuncDataMp()->New( *val, *GlobalTables::GetTypeTable().GetPrimType(PTY_f128)); return GetCurrentFuncCodeMp()->New(PTY_f128, mirConst); } @@ -1011,11 +1012,24 @@ void MIRBuilder::AddStmtInCurrentFunctionBody(StmtNode &stmt) { } MemPool *MIRBuilder::GetCurrentFuncCodeMp() { - return mirModule->CurFuncCodeMemPool(); + if (MIRFunction *curFunction = GetCurrentFunction()) { + return curFunction->GetCodeMemPool(); + } + return mirModule->GetMemPool(); } MapleAllocator *MIRBuilder::GetCurrentFuncCodeMpAllocator() { - return mirModule->CurFuncCodeMemPoolAllocator(); + if (MIRFunction *curFunction = GetCurrentFunction()) { + return &curFunction->GetCodeMPAllocator(); + } + return &mirModule->GetMPAllocator(); +} + +MemPool *MIRBuilder::GetCurrentFuncDataMp() { + if (MIRFunction *curFunction = GetCurrentFunction()) { + return curFunction->GetDataMemPool(); + } + return mirModule->GetMemPool(); } MIRBuilderExt::MIRBuilderExt(MIRModule *module, pthread_mutex_t *mutex) : MIRBuilder(module), mutex(mutex) {} -- Gitee