Skip to content

Commit

Permalink
Fix issues with Mnemonic translation and C++ syntax.
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
imbillow authored Apr 3, 2023
1 parent 20eac72 commit 0e3aea0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
12 changes: 11 additions & 1 deletion llvm/utils/TableGen/DecoderEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
33 changes: 25 additions & 8 deletions llvm/utils/TableGen/PrinterCapstone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -2313,11 +2316,22 @@ std::string getImplicitDefs(StringRef const &TargetName,
return Flags;
}

static inline std::string
getNormalMnemonic(std::unique_ptr<MatchableInfo> 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<MatchableInfo> 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, ";
Expand Down Expand Up @@ -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) << ", ";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand All @@ -2621,13 +2635,16 @@ void printInsnNameMapEnumEntry(StringRef const &TargetName,
std::unique_ptr<MatchableInfo> const &MI,
raw_string_ostream &InsnNameMap,
raw_string_ostream &InsnEnum) {
static std::set<StringRef> MnemonicsSeen;
StringRef Mnemonic = MI->Mnemonic;
static std::set<std::string> 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";
}
Expand Down

0 comments on commit 0e3aea0

Please sign in to comment.