diff --git a/src/mapleall/maple_driver/src/driver_runner.cpp b/src/mapleall/maple_driver/src/driver_runner.cpp index 6cf865b07e479de6360a828a2a9babc1df3d6465..31993707e9a634ac95816ad9c422d92fc63ebd6b 100644 --- a/src/mapleall/maple_driver/src/driver_runner.cpp +++ b/src/mapleall/maple_driver/src/driver_runner.cpp @@ -203,13 +203,14 @@ ErrorCode DriverRunner::ParseInput() const { void DriverRunner::ProcessMpl2mplAndMePhases(const std::string &outputFile, const std::string &vtableImplFile) const { CHECK_MODULE(); theMIRModule = theModule; + if (hasDebugFlag) { std::cout << "set up debug info " << std::endl; theMIRModule->GetDbgInfo()->BuildDebugInfo(); } + if (mpl2mplOptions != nullptr || meOptions != nullptr) { LogInfo::MapleLogger() << "Processing maplecomb" << '\n'; - InterleavedManager mgr(optMp, theModule, meInput, timePhases); std::vector phases; #include "phases.def" diff --git a/src/mapleall/maple_ir/src/lexer.cpp b/src/mapleall/maple_ir/src/lexer.cpp index e21cc8c01ae5f9cbb5d7a80e13fce30fda69a3a8..7c20a08803743e399d4f30346f6d32286e645470 100644 --- a/src/mapleall/maple_ir/src/lexer.cpp +++ b/src/mapleall/maple_ir/src/lexer.cpp @@ -86,6 +86,7 @@ void MIRLexer::PrepareForFile(const std::string &filename) { } else { lineNum = 1; } + module.GetDbgInfo()->UpdateMsg(lineNum, line.c_str()); kind = TK_invalid; } @@ -542,6 +543,7 @@ TokenKind MIRLexer::LexToken() { return TK_eof; } ++lineNum; // a new line read. + module.GetDbgInfo()->UpdateMsg(lineNum, line.c_str()); // skip spaces c = GetCurrentCharWithUpperCheck(); while (c == ' ' || c == '\t') { diff --git a/src/mapleall/maple_ir/src/mir_module.cpp b/src/mapleall/maple_ir/src/mir_module.cpp index 7a0a2b611ef3a32ad3d2d4198ccc6c427118bb1b..4fb2dc7270e213b07a1213bc3892ccd310e6dae4 100644 --- a/src/mapleall/maple_ir/src/mir_module.cpp +++ b/src/mapleall/maple_ir/src/mir_module.cpp @@ -639,6 +639,9 @@ void MIRModule::OutputAsciiMpl(const char *phaseName, const char *suffix, std::streambuf *backup = LogInfo::MapleLogger().rdbuf(); LogInfo::MapleLogger().rdbuf(mplFile.rdbuf()); // change LogInfo::MapleLogger()'s buffer to that of file Dump(emitStructureType, dumpFuncSet); + if (withDbgInfo) { + dbgInfo->Dump(0); + } LogInfo::MapleLogger().rdbuf(backup); // restore LogInfo::MapleLogger()'s buffer mplFile.close(); } else { diff --git a/src/mapleall/maple_ir/src/mpl_dbg.cpp b/src/mapleall/maple_ir/src/mpl_dbg.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10b5a4435a45a2f8633dcfd261ff872cdbaf3d8c --- /dev/null +++ b/src/mapleall/maple_ir/src/mpl_dbg.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (C) [2021] Futurewei Technologies, Inc. All rights reverved. + * + * OpenArkCompiler is licensed under the Mulan Permissive Software License v2. + * You can use this software according to the terms and conditions of the MulanPSL - 2.0. + * You may obtain a copy of MulanPSL - 2.0 at: + * + * https://opensource.org/licenses/MulanPSL-2.0 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR + * FIT FOR A PARTICULAR PURPOSE. + * See the MulanPSL - 2.0 for more details. + */ + +#include "mir_parser.h" +#include "debug_info.h" +#include "bin_mplt.h" +#include "opcode_info.h" +#include +#include "mir_function.h" +#include "mir_type.h" +#include +#include + +using namespace maple; + +std::unordered_set dumpFuncSet = {}; + +int main(int argc, char **argv) { + if (argc < 2) { + MIR_PRINTF("usage: mpldbg foo.mpl\n"); + exit(1); + } + std::vector themodule(argc, nullptr); + bool useBinary = false; + MIRSrcLang srcLang = kSrcLangUnknown; + // process the options which must come first + maple::int32 i = 1; + while (argv[i][0] == '-' ) { + if (argv[i][1] == 'b' && argv[i][2] == '\0') { + useBinary = true; + } else if (strncmp(argv[i], "-dumpfunc=", 10) == 0 && strlen(argv[i]) > 10) { + std::string funcName(&argv[i][10]); + dumpFuncSet.insert(funcName); + } else if (strcmp(argv[i], "-srclang=java") == 0 ) { + srcLang = kSrcLangJava; + } else if (strcmp(argv[i], "-srclang=c") == 0 ) { + srcLang = kSrcLangC; + } else if (strcmp(argv[i], "-srclang=c++") == 0 ) { + srcLang = kSrcLangCPlusPlus; + } else { + ERR(kLncErr, "mpldbg: unrecognized command line option"); + return 1; + } + i++; + } + // process the input files + while (i < argc) { + themodule[i] = new maple::MIRModule(argv[i]); + themodule[i]->SetSrcLang(srcLang); + std::string::size_type lastdot = themodule[i]->GetFileName().find_last_of("."); + bool ismplt = themodule[i]->GetFileName().compare(lastdot, 5, ".mplt") == 0; + 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, "mpldbg: input must be .mplt or .mpl or .bpl or .tmpl file"); + return 1; + } + // input the file + if (ismpl || istmpl) { + maple::MIRParser theparser(*themodule[i]); + if (!theparser.ParseMIR()) { + theparser.EmitError(themodule[i]->GetFileName().c_str()); + return 1; + } + } else { + BinaryMplImport binMplt(*themodule[i]); + binMplt.SetImported(false); + std::string modid = themodule[i]->GetFileName(); + if (!binMplt.Import(modid, true)) { + ERR(kLncErr, "mpldbg: cannot open .mplt or .bpl file: %s", modid.c_str()); + return 1; + } + } + + themodule[i]->GetDbgInfo()->BuildDebugInfo(); + themodule[i]->SetWithDbgInfo(true); + + // output the file + themodule[i]->OutputAsciiMpl(".dbg", (ismpl || isbpl) ? ".mpl" : ".tmpl"); + i++; + } + return 0; +} diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index 1090783ba5196c14817d6a45b34c9508f4d7bb24..7e4044fc82a4a79a7552c73cf50c737e55c5240f 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -162,6 +162,8 @@ void MIRParser::Error(const std::string &str) { message += ": "; message += lexer.GetTokenString(); message += "\n"; + + mod.GetDbgInfo()->SetErrPos(lexer.GetLineNum(), lexer.GetCurIdx()); } const std::string &MIRParser::GetError() { @@ -2880,6 +2882,7 @@ void MIRParser::EmitError(const std::string &fileName) { if (!strlen(GetError().c_str())) { return; } + mod.GetDbgInfo()->EmitMsg(); ERR(kLncErr, "%s \n%s", fileName.c_str(), GetError().c_str()); }