From 0e3aea0c60d66e424c577272cdfb03047e77ec1a Mon Sep 17 00:00:00 2001 From: billow Date: Tue, 4 Apr 2023 02:52:54 +0800 Subject: [PATCH] Fix issues with Mnemonic translation and C++ syntax. * refactor: Refactor `setPrinterParameters` function for broader coverage - Improve handling of `GPostfix` in `setPrinterParameters` function in `DecoderEmitter.cpp` - Update `setPrinterParameters` to handle more cases when setting `GPostfix` - Add `switch` statement to handle different cases when setting `GPostfix` based on output language - Remove unnecessary line setting `GPostfix` in `DecoderEmitter.cpp` * refactor: Refactor instruction printing in TableGen. - Refactor PrinterCapstone.cpp to utilize a new normal mnemonic function for MatchableInfo objects - Replace hardcoded Mnemonic.upper() with new function call for readability * Fix: Capstone printer code - Change type of `Mn` to `std::string` in [llvm/utils/TableGen/PrinterCapstone.cpp] - Use `mapped_iterator` to uppercase `Mnemonic` before assigning to `Mn` * Fix: capstone register enum code in LLVM TableGen printer - Replace ARM_REG_ENDING with TargetName_REG_ENDING in PrinterCapstone.cpp --- llvm/utils/TableGen/DecoderEmitter.cpp | 12 ++++++++- llvm/utils/TableGen/PrinterCapstone.cpp | 33 +++++++++++++++++++------ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp index d68258c98768..bc48369d21c2 100644 --- a/llvm/utils/TableGen/DecoderEmitter.cpp +++ b/llvm/utils/TableGen/DecoderEmitter.cpp @@ -2064,8 +2064,18 @@ static void setPrinterParameters(CodeGenTarget &Target, PrinterLanguage PL, } else { PredicateNamespace = Target.getName().str(); GPrefix = "if ("; - GPostfix = " == MCDisassembler::Fail)"; L = ""; + + switch (PL) { + default: + PrintFatalNote("DecoderEmitter does not support the given output language."); + case llvm::PRINTER_LANG_CPP: + GPostfix = " == MCDisassembler::Fail)"; + break; + case llvm::PRINTER_LANG_CAPSTONE_C: + GPostfix = " == MCDisassembler_Fail)"; + break; + } } ROK = "S"; diff --git a/llvm/utils/TableGen/PrinterCapstone.cpp b/llvm/utils/TableGen/PrinterCapstone.cpp index 8b6cc0839841..aaef756095fd 100644 --- a/llvm/utils/TableGen/PrinterCapstone.cpp +++ b/llvm/utils/TableGen/PrinterCapstone.cpp @@ -11,6 +11,8 @@ //===----------------------------------------------------------------------===// #include "Printer.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/CodeGen/ValueTypes.h" #include "llvm/MC/MCInst.h" #include "llvm/Support/FileSystem.h" @@ -165,7 +167,8 @@ void PrinterCapstone::regInfoEmitEnums(CodeGenTarget const &Target, "Register enum value mismatch!"); OS << " NUM_TARGET_REGS // " << Registers.size() + 1 << "\n"; OS << "};\n"; - CSRegEnum << "\tARM_REG_ENDING, // " << Registers.size() + 1 << "\n"; + CSRegEnum << "\t" << TargetName << "_REG_ENDING, // " << Registers.size() + 1 << "\n"; + writeFile(TargetName + "GenCSRegEnum.inc", CSRegEnumStr); const auto &RegisterClasses = Bank.getRegClasses(); @@ -2313,11 +2316,22 @@ std::string getImplicitDefs(StringRef const &TargetName, return Flags; } +static inline std::string +getNormalMnemonic(std::unique_ptr const &MI, const bool upper = true) { + auto Mn = MI->Mnemonic.str(); + std::replace(Mn.begin(), Mn.end(), '.', '_'); + if (upper) { + std::transform(Mn.begin(), Mn.end(), Mn.begin(), + ::toupper); + } + return Mn; +} + std::string getReqFeatures(StringRef const &TargetName, std::unique_ptr const &MI, bool UseMI, CodeGenInstruction const *CGI) { std::string Flags = "{ "; - std::string Mn = MI->Mnemonic.upper(); + std::string Mn = getNormalMnemonic(MI); // The debug if if (CGI->isBranch && !CGI->isCall) { Flags += TargetName.str() + "_GRP_JUMP, "; @@ -2358,7 +2372,7 @@ void printInsnMapEntry(StringRef const &TargetName, InsnMap.indent(2) << getLLVMInstEnumName(TargetName, CGI) << " /* " << InsnNum << " */"; InsnMap << ", " << TargetName << "_INS_" - << (UseMI ? MI->Mnemonic.upper() : "INVALID") << ",\n"; + << (UseMI ? getNormalMnemonic(MI) : "INVALID") << ",\n"; InsnMap.indent(2) << "#ifndef CAPSTONE_DIET\n"; if (UseMI) { InsnMap.indent(4) << getImplicitUses(TargetName, CGI) << ", "; @@ -2540,7 +2554,7 @@ void printInsnOpMapEntry(CodeGenTarget const &Target, // Write the C struct of the Instruction operands. InsnOpMap << "{ /* " + LLVMEnum + " (" << InsnNum << ") - " + TargetName + "_INS_" + - (UseMI ? MI->Mnemonic.upper() : "INVALID") + " - " + + (UseMI ? getNormalMnemonic(MI) : "INVALID") + " - " + CGI->AsmString + " */\n"; InsnOpMap << " 0 \n"; InsnOpMap << "},\n"; @@ -2605,7 +2619,7 @@ void printInsnOpMapEntry(CodeGenTarget const &Target, // Write the C struct of the Instruction operands. InsnOpMap << "{ /* " + LLVMEnum + " (" << InsnNum << ") - " + TargetName + "_INS_" + - (UseMI ? MI->Mnemonic.upper() : "INVALID") + " - " + + (UseMI ? getNormalMnemonic(MI) : "INVALID") + " - " + CGI->AsmString + " */\n"; InsnOpMap << "{\n"; for (OpData const &OD : InsOps) { @@ -2621,13 +2635,16 @@ void printInsnNameMapEnumEntry(StringRef const &TargetName, std::unique_ptr const &MI, raw_string_ostream &InsnNameMap, raw_string_ostream &InsnEnum) { - static std::set MnemonicsSeen; - StringRef Mnemonic = MI->Mnemonic; + static std::set MnemonicsSeen; + auto Mnemonic = getNormalMnemonic(MI, false); if (MnemonicsSeen.find(Mnemonic) != MnemonicsSeen.end()) return; MnemonicsSeen.emplace(Mnemonic); - std::string EnumName = TargetName.str() + "_INS_" + Mnemonic.upper(); + std::string Mn{mapped_iterator(Mnemonic.begin(), toUpper), + mapped_iterator(Mnemonic.end(), toUpper)}; + + std::string EnumName = TargetName.str() + "_INS_" + Mn; InsnNameMap.indent(2) << "\"" + Mnemonic + "\", // " + EnumName + "\n"; InsnEnum.indent(2) << EnumName + ",\n"; }