Skip to content

Commit

Permalink
[AsmWriter] Factor out mnemonic generation to accessible getMnemonic.
Browse files Browse the repository at this point in the history
This patch factors out the part of printInstruction that gets the
mnemonic string for a given MCInst. This is intended to be used
subsequently for the instruction-mix remarks to display the final
mnemonic (D90040).

Unfortunately making `getMnemonic` available to the AsmPrinter
seems to require making it virtual. Not sure if there's a way around
that with the current layering of the AsmPrinters.

Reviewed By: Paul-C-Anagnostopoulos

Differential Revision: https://reviews.llvm.org/D90039
  • Loading branch information
fhahn committed Nov 17, 2020
1 parent c6ef6e1 commit b2f4c5f
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 22 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/MC/MCInstPrinter.h
Expand Up @@ -88,6 +88,10 @@ class MCInstPrinter {
/// Specify a stream to emit comments to.
void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }

/// Returns a pair containing the mnemonic for \p MI and the number of bits
/// left for further processing by printInstruction (generated by tablegen).
virtual std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) = 0;

/// Print the specified MCInst to the specified raw_ostream.
///
/// \p Address the address of current instruction on most targets, used to
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64InstPrinter.h
Expand Up @@ -30,6 +30,7 @@ class AArch64InstPrinter : public MCInstPrinter {
void printRegName(raw_ostream &OS, unsigned RegNo) const override;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
virtual void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O);
virtual bool printAliasInstr(const MCInst *MI, uint64_t Address,
Expand Down Expand Up @@ -203,6 +204,7 @@ class AArch64AppleInstPrinter : public AArch64InstPrinter {
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
const MCSubtargetInfo &STI, raw_ostream &O) override;

std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O) override;
bool printAliasInstr(const MCInst *MI, uint64_t Address,
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUInstPrinter.h
Expand Up @@ -24,6 +24,7 @@ class AMDGPUInstPrinter : public MCInstPrinter {

//Autogenerated by tblgen
void printRegName(raw_ostream &OS, unsigned RegNo) const override;
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);
Expand Down Expand Up @@ -253,6 +254,7 @@ class R600InstPrinter : public MCInstPrinter {

void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
const MCSubtargetInfo &STI, raw_ostream &O) override;
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/ARC/MCTargetDesc/ARCInstPrinter.h
Expand Up @@ -26,6 +26,7 @@ class ARCInstPrinter : public MCInstPrinter {
: MCInstPrinter(MAI, MII, MRI) {}

// Autogenerated by tblgen.
std::pair<std::string, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMInstPrinter.h
Expand Up @@ -30,6 +30,7 @@ class ARMInstPrinter : public MCInstPrinter {
void printRegName(raw_ostream &OS, unsigned RegNo) const override;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O);
virtual bool printAliasInstr(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/AVR/MCTargetDesc/AVRInstPrinter.h
Expand Up @@ -45,6 +45,7 @@ class AVRInstPrinter : public MCInstPrinter {
void printMemri(const MCInst *MI, unsigned OpNo, raw_ostream &O);

// Autogenerated by TableGen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &O);
void printCustomAliasOperand(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
Expand Up @@ -32,6 +32,7 @@ class BPFInstPrinter : public MCInstPrinter {
void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);
};
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Hexagon/MCTargetDesc/HexagonInstPrinter.h
Expand Up @@ -34,6 +34,7 @@ class HexagonInstPrinter : public MCInstPrinter {

static char const *getRegisterName(unsigned RegNo);

std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
void printOperand(MCInst const *MI, unsigned OpNo, raw_ostream &O) const;
void printBrtarget(MCInst const *MI, unsigned OpNo, raw_ostream &O) const;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
Expand Up @@ -43,6 +43,7 @@ class LanaiInstPrinter : public MCInstPrinter {
void printMemImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &OS);
void printCustomAliasOperand(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
Expand Up @@ -26,6 +26,7 @@ namespace llvm {
const MCSubtargetInfo &STI, raw_ostream &O) override;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &O);
void printCustomAliasOperand(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsInstPrinter.h
Expand Up @@ -79,6 +79,7 @@ class MipsInstPrinter : public MCInstPrinter {
: MCInstPrinter(MAI, MII, MRI) {}

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/NVPTX/MCTargetDesc/NVPTXInstPrinter.h
Expand Up @@ -29,6 +29,7 @@ class NVPTXInstPrinter : public MCInstPrinter {
const MCSubtargetInfo &STI, raw_ostream &OS) override;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);
// End
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
Expand Up @@ -36,6 +36,7 @@ class PPCInstPrinter : public MCInstPrinter {
const MCSubtargetInfo &STI, raw_ostream &O) override;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
Expand Up @@ -48,6 +48,7 @@ class RISCVInstPrinter : public MCInstPrinter {
const MCSubtargetInfo &STI, raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O);
bool printAliasInstr(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/Sparc/MCTargetDesc/SparcInstPrinter.h
Expand Up @@ -31,6 +31,7 @@ class SparcInstPrinter : public MCInstPrinter {
bool isV9(const MCSubtargetInfo &STI) const;

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address,
const MCSubtargetInfo &STI, raw_ostream &O);
bool printAliasInstr(const MCInst *MI, uint64_t Address,
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.h
Expand Up @@ -27,6 +27,7 @@ class SystemZInstPrinter : public MCInstPrinter {
: MCInstPrinter(MAI, MII, MRI) {}

// Automatically generated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
Expand Up @@ -52,6 +52,7 @@ class WebAssemblyInstPrinter final : public MCInstPrinter {
raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);
};
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/MCTargetDesc/X86ATTInstPrinter.h
Expand Up @@ -36,6 +36,7 @@ class X86ATTInstPrinter final : public X86InstPrinterCommon {
raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &OS);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/X86/MCTargetDesc/X86IntelInstPrinter.h
Expand Up @@ -37,6 +37,7 @@ class X86IntelInstPrinter final : public X86InstPrinterCommon {
raw_ostream &O);

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/XCore/MCTargetDesc/XCoreInstPrinter.h
Expand Up @@ -27,6 +27,7 @@ class XCoreInstPrinter : public MCInstPrinter {
: MCInstPrinter(MAI, MII, MRI) {}

// Autogenerated by tblgen.
std::pair<const char *, uint64_t> getMnemonic(const MCInst *MI) override;
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O);
static const char *getRegisterName(unsigned RegNo);

Expand Down
81 changes: 59 additions & 22 deletions llvm/utils/TableGen/AsmWriterEmitter.cpp
Expand Up @@ -64,9 +64,15 @@ class AsmWriterEmitter {
AsmWriterEmitter(RecordKeeper &R);

void run(raw_ostream &o);

private:
void EmitPrintInstruction(raw_ostream &o);
void EmitGetMnemonic(
raw_ostream &o,
std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
unsigned &BitsLeft, unsigned &AsmStrBits);
void EmitPrintInstruction(
raw_ostream &o,
std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
unsigned &BitsLeft, unsigned &AsmStrBits);
void EmitGetRegisterName(raw_ostream &o);
void EmitPrintAliasInstruction(raw_ostream &O);

Expand Down Expand Up @@ -288,22 +294,19 @@ static void UnescapeAliasString(std::string &Str) {
}
}

/// EmitPrintInstruction - Generate the code for the "printInstruction" method
/// implementation. Destroys all instances of AsmWriterInst information, by
/// clearing the Instructions vector.
void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
void AsmWriterEmitter::EmitGetMnemonic(
raw_ostream &O,
std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
unsigned &BitsLeft, unsigned &AsmStrBits) {
Record *AsmWriter = Target.getAsmWriter();
StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");

O << "/// printInstruction - This method is automatically generated by "
O << "/// getMnemonic - This method is automatically generated by "
"tablegen\n"
"/// from the instruction set description.\n"
"void "
<< Target.getName() << ClassName
<< "::printInstruction(const MCInst *MI, uint64_t Address, "
<< (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
<< "raw_ostream &O) {\n";
"std::pair<const char *, uint64_t> "
<< Target.getName() << ClassName << "::getMnemonic(const MCInst *MI) {\n";

// Build an aggregate string, and build a table of offsets into it.
SequenceToOffsetTable<std::string> StringTable;
Expand Down Expand Up @@ -349,13 +352,11 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
}

// Figure out how many bits we used for the string index.
unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+2);
AsmStrBits = Log2_32_Ceil(MaxStringIdx + 2);

// To reduce code size, we compactify common instructions into a few bits
// in the opcode-indexed table.
unsigned BitsLeft = OpcodeInfoBits-AsmStrBits;

std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
BitsLeft = OpcodeInfoBits - AsmStrBits;

while (true) {
std::vector<std::string> UniqueOperandCommands;
Expand Down Expand Up @@ -435,15 +436,47 @@ void AsmWriterEmitter::EmitPrintInstruction(raw_ostream &O) {
++Table;
}

// Emit the initial tab character.
O << " O << \"\\t\";\n\n";

O << " // Emit the opcode for the instruction.\n";
O << BitsString;

// Return mnemonic string and bits.
O << " return {AsmStrs+(Bits & " << (1 << AsmStrBits) - 1
<< ")-1, Bits};\n\n";

O << "}\n";
}

/// EmitPrintInstruction - Generate the code for the "printInstruction" method
/// implementation. Destroys all instances of AsmWriterInst information, by
/// clearing the Instructions vector.
void AsmWriterEmitter::EmitPrintInstruction(
raw_ostream &O,
std::vector<std::vector<std::string>> &TableDrivenOperandPrinters,
unsigned &BitsLeft, unsigned &AsmStrBits) {
const unsigned OpcodeInfoBits = 64;
Record *AsmWriter = Target.getAsmWriter();
StringRef ClassName = AsmWriter->getValueAsString("AsmWriterClassName");
bool PassSubtarget = AsmWriter->getValueAsInt("PassSubtarget");

O << "/// printInstruction - This method is automatically generated by "
"tablegen\n"
"/// from the instruction set description.\n"
"void "
<< Target.getName() << ClassName
<< "::printInstruction(const MCInst *MI, uint64_t Address, "
<< (PassSubtarget ? "const MCSubtargetInfo &STI, " : "")
<< "raw_ostream &O) {\n";

// Emit the initial tab character.
O << " O << \"\\t\";\n\n";

// Emit the starting string.
O << " assert(Bits != 0 && \"Cannot print this instruction.\");\n"
<< " O << AsmStrs + (Bits & " << (1 << AsmStrBits)-1 << ") - 1;\n\n";
O << " auto MnemonicInfo = getMnemonic(MI);\n\n";
O << " O << MnemonicInfo.first;\n\n";

O << " uint" << ((BitsLeft < (OpcodeInfoBits - 32)) ? 64 : 32)
<< "_t Bits = MnemonicInfo.second;\n"
<< " assert(Bits != 0 && \"Cannot print this instruction.\");\n";

// Output the table driven operand information.
BitsLeft = OpcodeInfoBits-AsmStrBits;
Expand Down Expand Up @@ -1262,7 +1295,11 @@ AsmWriterEmitter::AsmWriterEmitter(RecordKeeper &R) : Records(R), Target(R) {
}

void AsmWriterEmitter::run(raw_ostream &O) {
EmitPrintInstruction(O);
std::vector<std::vector<std::string>> TableDrivenOperandPrinters;
unsigned BitsLeft = 0;
unsigned AsmStrBits = 0;
EmitGetMnemonic(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
EmitPrintInstruction(O, TableDrivenOperandPrinters, BitsLeft, AsmStrBits);
EmitGetRegisterName(O);
EmitPrintAliasInstruction(O);
}
Expand Down

0 comments on commit b2f4c5f

Please sign in to comment.