From d792c47e7a1d460df535fb7d48c656bb27b90d84 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Sat, 26 Mar 2022 00:13:24 -0700 Subject: [PATCH] put mplme's output maple IR in binary form in .mbc file when -genmaplebc is specified Some bug fixes in binary export/import related to preg table. This change breaks compatibility of .bpl files. Make irbuild and maple driver accepts .mbc files as input. mplcg can take .mbc as input. --- .../include/driver_option_common.h | 1 + .../maple_driver/include/driver_runner.h | 8 +++++--- .../maple_driver/include/mpl_options.h | 11 +++++++++++ .../maple_driver/src/driver_option_common.cpp | 9 +++++++++ .../maple_driver/src/driver_runner.cpp | 1 + .../maple_driver/src/maple_comb_compiler.cpp | 2 +- src/mapleall/maple_driver/src/mpl_options.cpp | 4 ++++ .../maple_driver/src/mplcg_compiler.cpp | 4 +++- src/mapleall/maple_ir/include/bin_mpl_import.h | 2 +- src/mapleall/maple_ir/src/bin_func_export.cpp | 4 ++-- src/mapleall/maple_ir/src/bin_func_import.cpp | 18 +++--------------- src/mapleall/maple_ir/src/driver.cpp | 16 +++++++++------- .../maple_me/include/me_phase_manager.h | 1 + src/mapleall/maple_me/src/me_phase_manager.cpp | 11 +++++++++++ 14 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/mapleall/maple_driver/include/driver_option_common.h b/src/mapleall/maple_driver/include/driver_option_common.h index 6eb720443f..70d57425cc 100644 --- a/src/mapleall/maple_driver/include/driver_option_common.h +++ b/src/mapleall/maple_driver/include/driver_option_common.h @@ -50,6 +50,7 @@ enum DriverOptionIndex { kOption, kTimePhases, kGenMeMpl, + kGenMapleBC, kGenVtableImpl, kVerbose, kAllDebug, diff --git a/src/mapleall/maple_driver/include/driver_runner.h b/src/mapleall/maple_driver/include/driver_runner.h index c17d98fafd..d765a88484 100644 --- a/src/mapleall/maple_driver/include/driver_runner.h +++ b/src/mapleall/maple_driver/include/driver_runner.h @@ -37,7 +37,7 @@ class DriverRunner final { DriverRunner(MIRModule *theModule, const std::vector &exeNames, InputFileType inpFileType, const std::string &mpl2mplInput, const std::string &meInput, const std::string &actualInput, bool dwarf, bool fileParsed = false, bool timePhases = false, - bool genVtableImpl = false, bool genMeMpl = false) + bool genVtableImpl = false, bool genMeMpl = false, bool genMapleBC = false) : theModule(theModule), exeNames(exeNames), mpl2mplInput(mpl2mplInput), @@ -48,6 +48,7 @@ class DriverRunner final { timePhases(timePhases), genVtableImpl(genVtableImpl), genMeMpl(genMeMpl), + genMapleBC(genMapleBC), inputFileType(inpFileType) { auto lastDot = actualInput.find_last_of("."); baseName = (lastDot == std::string::npos) ? actualInput : actualInput.substr(0, lastDot); @@ -55,9 +56,9 @@ class DriverRunner final { DriverRunner(MIRModule *theModule, const std::vector &exeNames, InputFileType inpFileType, const std::string &actualInput, bool dwarf, bool fileParsed = false, bool timePhases = false, - bool genVtableImpl = false, bool genMeMpl = false) + bool genVtableImpl = false, bool genMeMpl = false, bool genMapleBC = false) : DriverRunner(theModule, exeNames, inpFileType, "", "", actualInput, dwarf, - fileParsed, timePhases, genVtableImpl, genMeMpl) { + fileParsed, timePhases, genVtableImpl, genMeMpl, genMapleBC) { auto lastDot = actualInput.find_last_of("."); baseName = (lastDot == std::string::npos) ? actualInput : actualInput.substr(0, lastDot); } @@ -105,6 +106,7 @@ class DriverRunner final { bool timePhases = false; bool genVtableImpl = false; bool genMeMpl = false; + bool genMapleBC = false; std::string printOutExe = ""; std::string baseName; std::string outputFile; diff --git a/src/mapleall/maple_driver/include/mpl_options.h b/src/mapleall/maple_driver/include/mpl_options.h index fdb48cff85..29b8fd6113 100644 --- a/src/mapleall/maple_driver/include/mpl_options.h +++ b/src/mapleall/maple_driver/include/mpl_options.h @@ -44,6 +44,8 @@ enum InputFileType { kFileTypeObj, kFileTypeBpl, kFileTypeMeMpl, + kFileTypeMbc, + kFileTypeLmbc, }; enum OptimizationLevel { @@ -118,6 +120,10 @@ public: fileType = InputFileType::kFileTypeS; } else if (extensionName == "o") { fileType = InputFileType::kFileTypeObj; + } else if (extensionName == "mbc") { + fileType = InputFileType::kFileTypeMbc; + } else if (extensionName == "lmbc") { + fileType = InputFileType::kFileTypeLmbc; } return fileType; @@ -362,6 +368,10 @@ class MplOptions { return genMeMpl; } + bool HasSetGenMapleBC() const { + return genMapleBC; + } + bool HasSetGenOnlyObj() const { return genObj; } @@ -459,6 +469,7 @@ class MplOptions { bool timePhases = false; bool genObj = false; bool genMeMpl = false; + bool genMapleBC = false; bool runMaplePhaseOnly = true; bool genVtableImpl = false; bool hasPrinted = false; diff --git a/src/mapleall/maple_driver/src/driver_option_common.cpp b/src/mapleall/maple_driver/src/driver_option_common.cpp index 7652807453..f0477dc47e 100644 --- a/src/mapleall/maple_driver/src/driver_option_common.cpp +++ b/src/mapleall/maple_driver/src/driver_option_common.cpp @@ -312,6 +312,15 @@ const mapleOption::Descriptor kUsages[] = { " --genmempl \tGenerate me.mpl file\n", "driver", {} }, + { kGenMapleBC, + 0, + "", + "genmaplebc", + kBuildTypeProduct, + kArgCheckPolicyNone, + " --genmaplebc \tGenerate .mbc file\n", + "driver", + {} }, { kGenObj, kEnable, "c", diff --git a/src/mapleall/maple_driver/src/driver_runner.cpp b/src/mapleall/maple_driver/src/driver_runner.cpp index 58dfd86179..741f8ba2c5 100644 --- a/src/mapleall/maple_driver/src/driver_runner.cpp +++ b/src/mapleall/maple_driver/src/driver_runner.cpp @@ -266,6 +266,7 @@ void DriverRunner::RunNewPM(const std::string &output, const std::string &vtable topLevelPhaseManager->InitTimeHandler(); } MeFuncPM::genMeMpl = genMeMpl; + MeFuncPM::genMapleBC = genMapleBC; MeFuncPM::timePhases = timePhases; MPLTimer timer; timer.Start(); diff --git a/src/mapleall/maple_driver/src/maple_comb_compiler.cpp b/src/mapleall/maple_driver/src/maple_comb_compiler.cpp index 164a306eee..faf7320952 100644 --- a/src/mapleall/maple_driver/src/maple_comb_compiler.cpp +++ b/src/mapleall/maple_driver/src/maple_comb_compiler.cpp @@ -180,7 +180,7 @@ ErrorCode MapleCombCompiler::Compile(MplOptions &options, const Action &action, theModule->InitPartO2List(options.GetPartO2List()); DriverRunner runner(theModule.get(), options.GetSelectedExes(), action.GetInputFileType(), fileName, fileName, fileName, options.WithDwarf(), fileParsed, - options.HasSetTimePhases(), options.HasSetGenVtableImpl(), options.HasSetGenMeMpl()); + options.HasSetTimePhases(), options.HasSetGenVtableImpl(), options.HasSetGenMeMpl(), options.HasSetGenMapleBC()); ErrorCode ret = kErrorNoError; MIRParser parser(*theModule); diff --git a/src/mapleall/maple_driver/src/mpl_options.cpp b/src/mapleall/maple_driver/src/mpl_options.cpp index b091b628d1..971903ee39 100644 --- a/src/mapleall/maple_driver/src/mpl_options.cpp +++ b/src/mapleall/maple_driver/src/mpl_options.cpp @@ -189,12 +189,16 @@ ErrorCode MplOptions::HandleGeneralOptions() { case kGenMeMpl: genMeMpl = true; break; + case kGenMapleBC: + genMapleBC = true; + break; case kGenVtableImpl: genVtableImpl = true; break; case kSaveTemps: isSaveTmps = true; genMeMpl = true; + genMapleBC = true; genVtableImpl = true; StringUtils::Split(opt.Args(), saveFiles, ','); break; diff --git a/src/mapleall/maple_driver/src/mplcg_compiler.cpp b/src/mapleall/maple_driver/src/mplcg_compiler.cpp index 6384fe3d0e..bbbb200323 100644 --- a/src/mapleall/maple_driver/src/mplcg_compiler.cpp +++ b/src/mapleall/maple_driver/src/mplcg_compiler.cpp @@ -184,7 +184,9 @@ ErrorCode MplcgCompiler::Compile(MplOptions &options, const Action &action, theModule->SetWithMe( std::find(options.GetRunningExes().begin(), options.GetRunningExes().end(), kBinNameMe) != options.GetRunningExes().end()); - if (action.GetInputFileType() != kFileTypeBpl) { + if (action.GetInputFileType() != kFileTypeBpl && + action.GetInputFileType() != kFileTypeMbc && + action.GetInputFileType() != kFileTypeLmbc) { std::unique_ptr theParser; theParser.reset(new MIRParser(*theModule)); bool parsed = theParser->ParseMIR(0, cgOption.GetParserOption()); diff --git a/src/mapleall/maple_ir/include/bin_mpl_import.h b/src/mapleall/maple_ir/include/bin_mpl_import.h index 67c4a24c72..3697654872 100644 --- a/src/mapleall/maple_ir/include/bin_mpl_import.h +++ b/src/mapleall/maple_ir/include/bin_mpl_import.h @@ -133,7 +133,7 @@ class BinaryMplImport { void ImportFormalsStIdx(MIRFunction *func); void ImportAliasMap(MIRFunction *func); void ImportSrcPos(SrcPosition &pos); - void ImportBaseNode(Opcode &o, PrimType &typ, uint8 &numOpr); + void ImportBaseNode(Opcode &o, PrimType &typ); PUIdx ImportFuncViaSymName(); BaseNode *ImportExpression(MIRFunction *func); void ImportReturnValues(MIRFunction *func, CallReturnVector *retv); diff --git a/src/mapleall/maple_ir/src/bin_func_export.cpp b/src/mapleall/maple_ir/src/bin_func_export.cpp index 0463df12b7..ce263debcf 100644 --- a/src/mapleall/maple_ir/src/bin_func_export.cpp +++ b/src/mapleall/maple_ir/src/bin_func_export.cpp @@ -46,7 +46,6 @@ void BinaryMplExport::OutputFuncIdInfo(MIRFunction *func) { void BinaryMplExport::OutputBaseNode(const BaseNode *b) { WriteNum(b->GetOpCode()); WriteNum(b->GetPrimType()); - WriteNum(b->GetNumOpnds()); } void BinaryMplExport::OutputLocalSymbol(MIRSymbol *sym) { @@ -107,6 +106,7 @@ void BinaryMplExport::OutputPregTab(const MIRFunction *func) { MIRPreg *mirpreg = func->GetPregTab()->PregFromPregIdx(static_cast(i)); if (mirpreg == nullptr) { WriteNum(0); + size++; continue; } WriteNum(kBinPreg); @@ -690,8 +690,8 @@ void BinaryMplExport::WriteFunctionBodyField(uint64 contentIdx, std::unordered_s OutputFunction(func->GetPuidx()); CHECK_FATAL(func->GetBody() != nullptr, "WriteFunctionBodyField: no function body"); OutputFuncIdInfo(func); - OutputLocalSymTab(func); OutputPregTab(func); + OutputLocalSymTab(func); OutputLabelTab(func); OutputLocalTypeNameTab(func->GetTypeNameTab()); OutputFormalsStIdx(func); diff --git a/src/mapleall/maple_ir/src/bin_func_import.cpp b/src/mapleall/maple_ir/src/bin_func_import.cpp index 3162c59b38..0802da3407 100644 --- a/src/mapleall/maple_ir/src/bin_func_import.cpp +++ b/src/mapleall/maple_ir/src/bin_func_import.cpp @@ -50,10 +50,9 @@ void BinaryMplImport::ImportFuncIdInfo(MIRFunction *func) { CHECK_FATAL(tag == ~kBinFuncIdInfoStart, "pattern mismatch in ImportFuncIdInfo()"); } -void BinaryMplImport::ImportBaseNode(Opcode &o, PrimType &typ, uint8 &numopr) { +void BinaryMplImport::ImportBaseNode(Opcode &o, PrimType &typ) { o = (Opcode)ReadNum(); typ = (PrimType)ReadNum(); - numopr = static_cast(ReadNum()); } void BinaryMplImport::ImportLocalSymbol(MIRFunction *func) { @@ -194,8 +193,7 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { CHECK_FATAL(tag == kBinOpExpression, "kBinOpExpression expected"); Opcode op; PrimType typ; - uint8 numOpr; - ImportBaseNode(op, typ, numOpr); + ImportBaseNode(op, typ); switch (op) { // leaf case OP_constval: { @@ -282,7 +280,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { case OP_sqrt: case OP_alloca: case OP_malloc: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); UnaryNode *unNode = mod.CurFuncCodeMemPool()->New(op, typ); unNode->SetOpnd(ImportExpression(func), 0); return unNode; @@ -291,14 +288,12 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { case OP_cvt: case OP_floor: case OP_trunc: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); TypeCvtNode *typecvtNode = mod.CurFuncCodeMemPool()->New(op, typ); typecvtNode->SetFromType((PrimType)ReadNum()); typecvtNode->SetOpnd(ImportExpression(func), 0); return typecvtNode; } case OP_retype: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); RetypeNode *retypeNode = mod.CurFuncCodeMemPool()->New(typ); retypeNode->SetTyIdx(ImportType()); retypeNode->SetOpnd(ImportExpression(func), 0); @@ -306,7 +301,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { } case OP_iread: case OP_iaddrof: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); IreadNode *irNode = mod.CurFuncCodeMemPool()->New(op, typ); irNode->SetTyIdx(ImportType()); irNode->SetFieldID(static_cast(ReadNum())); @@ -316,7 +310,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { case OP_sext: case OP_zext: case OP_extractbits: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); ExtractbitsNode *extNode = mod.CurFuncCodeMemPool()->New(op, typ); extNode->SetBitsOffset(static_cast(ReadNum())); extNode->SetBitsSize(static_cast(ReadNum())); @@ -325,7 +318,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { } case OP_gcmallocjarray: case OP_gcpermallocjarray: { - CHECK_FATAL(numOpr == 1, "expected numOpnds to be 1"); JarrayMallocNode *gcNode = mod.CurFuncCodeMemPool()->New(op, typ); gcNode->SetTyIdx(ImportType()); gcNode->SetOpnd(ImportExpression(func), 0); @@ -349,7 +341,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { case OP_land: case OP_lior: case OP_add: { - CHECK_FATAL(numOpr == 2, "expected numOpnds to be 2"); BinaryNode *binNode = mod.CurFuncCodeMemPool()->New(op, typ); binNode->SetOpnd(ImportExpression(func), 0); binNode->SetOpnd(ImportExpression(func), 1); @@ -364,7 +355,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { case OP_cmpg: case OP_cmpl: case OP_cmp: { - CHECK_FATAL(numOpr == 2, "expected numOpnds to be 2"); CompareNode *cmpNode = mod.CurFuncCodeMemPool()->New(op, typ); cmpNode->SetOpndType((PrimType)ReadNum()); cmpNode->SetOpnd(ImportExpression(func), 0); @@ -373,7 +363,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { } case OP_resolveinterfacefunc: case OP_resolvevirtualfunc: { - CHECK_FATAL(numOpr == 2, "expected numOpnds to be 2"); ResolveFuncNode *rsNode = mod.CurFuncCodeMemPool()->New(op, typ); rsNode->SetPUIdx(ImportFuncViaSymName()); rsNode->SetOpnd(ImportExpression(func), 0); @@ -382,7 +371,6 @@ BaseNode *BinaryMplImport::ImportExpression(MIRFunction *func) { } // ternary case OP_select: { - CHECK_FATAL(numOpr == 3, "expected numOpnds to be 3"); TernaryNode *tNode = mod.CurFuncCodeMemPool()->New(op, typ); tNode->SetOpnd(ImportExpression(func), 0); tNode->SetOpnd(ImportExpression(func), 1); @@ -887,8 +875,8 @@ void BinaryMplImport::ReadFunctionBodyField() { fn->AllocLabelTab(); ImportFuncIdInfo(fn); - ImportLocalSymTab(fn); ImportPregTab(fn); + ImportLocalSymTab(fn); ImportLabelTab(fn); ImportLocalTypeNameTable(fn->GetTypeNameTab()); ImportFormalsStIdx(fn); diff --git a/src/mapleall/maple_ir/src/driver.cpp b/src/mapleall/maple_ir/src/driver.cpp index 536ff56de4..468ad9cbca 100644 --- a/src/mapleall/maple_ir/src/driver.cpp +++ b/src/mapleall/maple_ir/src/driver.cpp @@ -32,7 +32,7 @@ int main(int argc, char **argv) { constexpr int judgeNumber = 2; if (argc < judgeNumber) { (void)MIR_PRINTF( - "usage: ./irbuild [-b] [-dumpfunc=] [-srclang=] ] [-srclang=] \n" " By default, the files are converted to corresponding ascii format.\n" " If -b is specified, output is binary format instead.\n" @@ -78,8 +78,10 @@ int main(int argc, char **argv) { bool istmpl = themodule[i]->GetFileName().compare(lastdot, 5, ".tmpl") == 0; bool ismpl = themodule[i]->GetFileName().compare(lastdot, 5, ".mpl\0") == 0; bool isbpl = themodule[i]->GetFileName().compare(lastdot, 5, ".bpl\0") == 0; - if (!ismplt && !istmpl && !ismpl && !isbpl) { - ERR(kLncErr, "irbuild: input must be .mplt or .mpl or .bpl or .tmpl file"); + bool ismbc = themodule[i]->GetFileName().compare(lastdot, 5, ".mbc\0") == 0; + bool islmbc = themodule[i]->GetFileName().compare(lastdot, 5, ".lmbc\0") == 0; + if (!ismplt && !istmpl && !ismpl && !isbpl && !ismbc && !islmbc) { + ERR(kLncErr, "irbuild: input must be .mplt or .mpl or .bpl or .mbc or .lmbc or .tmpl file"); return 1; } // input the file @@ -94,20 +96,20 @@ int main(int argc, char **argv) { binMplt.SetImported(false); std::string modid = themodule[i]->GetFileName(); if (!binMplt.Import(modid, true)) { - ERR(kLncErr, "irbuild: cannot open .mplt or .bpl file: %s", modid.c_str()); + ERR(kLncErr, "irbuild: cannot open .mplt or .bpl or .mbc or .lmbc file: %s", modid.c_str()); return 1; } } // output the file if (!useBinary) { - themodule[i]->OutputAsciiMpl(".irb", (ismpl || isbpl) ? ".mpl" : ".tmpl", &dumpFuncSet, true, false); + themodule[i]->OutputAsciiMpl(".irb", (ismpl || isbpl || ismbc || islmbc) ? ".mpl" : ".tmpl", &dumpFuncSet, true, false); } else { BinaryMplt binMplt(*themodule[i]); std::string modid = themodule[i]->GetFileName(); - binMplt.GetBinExport().not2mplt = ismpl || isbpl; + binMplt.GetBinExport().not2mplt = ismpl || isbpl || ismbc || islmbc; std::string filestem = modid.substr(0, lastdot); - binMplt.Export(filestem + ((ismpl || isbpl) ? ".irb.bpl" : ".irb.mplt"), &dumpFuncSet); + binMplt.Export(filestem + ((ismpl || isbpl || ismbc || islmbc) ? ".irb.bpl" : ".irb.mplt"), &dumpFuncSet); } ++i; } diff --git a/src/mapleall/maple_me/include/me_phase_manager.h b/src/mapleall/maple_me/include/me_phase_manager.h index 25189fbd01..eb29b79517 100644 --- a/src/mapleall/maple_me/include/me_phase_manager.h +++ b/src/mapleall/maple_me/include/me_phase_manager.h @@ -101,6 +101,7 @@ class MeFuncPM : public FunctionPM { ~MeFuncPM() override {} static bool genMeMpl; static bool timePhases; + static bool genMapleBC; void SetMeInput(const std::string &str) { meInput = str; diff --git a/src/mapleall/maple_me/src/me_phase_manager.cpp b/src/mapleall/maple_me/src/me_phase_manager.cpp index 0025bd4180..4cce3a004e 100644 --- a/src/mapleall/maple_me/src/me_phase_manager.cpp +++ b/src/mapleall/maple_me/src/me_phase_manager.cpp @@ -13,12 +13,14 @@ * See the Mulan PSL v2 for more details. */ #include "me_phase_manager.h" +#include "bin_mplt.h" #define JAVALANG (mirModule.IsJavaModule()) #define CLANG (mirModule.IsCModule()) namespace maple { bool MeFuncPM::genMeMpl = false; +bool MeFuncPM::genMapleBC = false; bool MeFuncPM::timePhases = false; void MeFuncPM::DumpMEIR(const MeFunction &f, const std::string phaseName, bool isBefore) { @@ -111,6 +113,15 @@ bool MeFuncPM::PhaseRun(maple::MIRModule &m) { if (genMeMpl) { m.Emit("comb.me.mpl"); } + if (genMapleBC) { + BinaryMplt binMplt(m); + std::string modFileName = m.GetFileName(); + std::string::size_type lastdot = modFileName.find_last_of("."); + + binMplt.GetBinExport().not2mplt = true; + std::string filestem = modFileName.substr(0, lastdot); + binMplt.Export(filestem + ".mbc", nullptr); + } if (MeFuncPM::timePhases) { DumpPhaseTime(); } -- Gitee