From 89b8c1de55e2a1600c083d100c4061341f337a23 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Fri, 18 Oct 2024 16:30:36 -0400 Subject: [PATCH 1/8] Split SystemZInstPrinter to two classes based on Asm dialect --- .../SystemZ/AsmParser/SystemZAsmParser.cpp | 10 +- llvm/lib/Target/SystemZ/CMakeLists.txt | 3 +- .../SystemZ/MCTargetDesc/CMakeLists.txt | 4 +- .../MCTargetDesc/SystemZGNUInstPrinter.cpp | 33 +++ .../MCTargetDesc/SystemZGNUInstPrinter.h | 46 +++ .../MCTargetDesc/SystemZHLASMInstPrinter.cpp | 35 +++ .../MCTargetDesc/SystemZHLASMInstPrinter.h | 45 +++ .../MCTargetDesc/SystemZInstPrinter.cpp | 266 ------------------ .../MCTargetDesc/SystemZInstPrinterCommon.cpp | 246 ++++++++++++++++ ...stPrinter.h => SystemZInstPrinterCommon.h} | 29 +- .../MCTargetDesc/SystemZMCTargetDesc.cpp | 8 +- llvm/lib/Target/SystemZ/SystemZ.td | 15 + llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp | 10 +- 13 files changed, 454 insertions(+), 296 deletions(-) create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.cpp create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h delete mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp rename llvm/lib/Target/SystemZ/MCTargetDesc/{SystemZInstPrinter.h => SystemZInstPrinterCommon.h} (78%) diff --git a/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp b/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp index f0a85645b8628..b8469a6ba70ea 100644 --- a/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp +++ b/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "MCTargetDesc/SystemZInstPrinter.h" +#include "MCTargetDesc/SystemZGNUInstPrinter.h" #include "MCTargetDesc/SystemZMCAsmInfo.h" #include "MCTargetDesc/SystemZMCTargetDesc.h" #include "SystemZTargetStreamer.h" @@ -721,7 +721,7 @@ void SystemZOperand::print(raw_ostream &OS) const { OS << "Token:" << getToken(); break; case KindReg: - OS << "Reg:" << SystemZInstPrinter::getRegisterName(getReg()); + OS << "Reg:" << SystemZGNUInstPrinter::getRegisterName(getReg()); break; case KindImm: OS << "Imm:"; @@ -743,10 +743,10 @@ void SystemZOperand::print(raw_ostream &OS) const { if (Op.MemKind == BDLMem) OS << *cast(Op.Length.Imm) << ","; else if (Op.MemKind == BDRMem) - OS << SystemZInstPrinter::getRegisterName(Op.Length.Reg) << ","; + OS << SystemZGNUInstPrinter::getRegisterName(Op.Length.Reg) << ","; if (Op.Index) - OS << SystemZInstPrinter::getRegisterName(Op.Index) << ","; - OS << SystemZInstPrinter::getRegisterName(Op.Base); + OS << SystemZGNUInstPrinter::getRegisterName(Op.Index) << ","; + OS << SystemZGNUInstPrinter::getRegisterName(Op.Base); OS << ")"; } break; diff --git a/llvm/lib/Target/SystemZ/CMakeLists.txt b/llvm/lib/Target/SystemZ/CMakeLists.txt index 063e5bcd44171..0d8f3eac6ee4f 100644 --- a/llvm/lib/Target/SystemZ/CMakeLists.txt +++ b/llvm/lib/Target/SystemZ/CMakeLists.txt @@ -3,7 +3,8 @@ add_llvm_component_group(SystemZ HAS_JIT) set(LLVM_TARGET_DEFINITIONS SystemZ.td) tablegen(LLVM SystemZGenAsmMatcher.inc -gen-asm-matcher) -tablegen(LLVM SystemZGenAsmWriter.inc -gen-asm-writer) +tablegen(LLVM SystemZGenGNUAsmWriter.inc -gen-asm-writer) +tablegen(LLVM SystemZGenHLASMAsmWriter.inc -gen-asm-writer -asmwriternum=1) tablegen(LLVM SystemZGenCallingConv.inc -gen-callingconv) tablegen(LLVM SystemZGenDAGISel.inc -gen-dag-isel) tablegen(LLVM SystemZGenDisassemblerTables.inc -gen-disassembler) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/CMakeLists.txt b/llvm/lib/Target/SystemZ/MCTargetDesc/CMakeLists.txt index 6700d79369708..9c00706531b88 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/CMakeLists.txt +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/CMakeLists.txt @@ -1,7 +1,9 @@ add_llvm_component_library(LLVMSystemZDesc SystemZELFObjectWriter.cpp + SystemZGNUInstPrinter.cpp SystemZGOFFObjectWriter.cpp - SystemZInstPrinter.cpp + SystemZHLASMInstPrinter.cpp + SystemZInstPrinterCommon.cpp SystemZMCAsmBackend.cpp SystemZMCAsmInfo.cpp SystemZMCCodeEmitter.cpp diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.cpp new file mode 100644 index 0000000000000..05113010794e0 --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.cpp @@ -0,0 +1,33 @@ +//===- SystemZGNUInstPrinter.cpp - Convert SystemZ MCInst to GNU assembly -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SystemZGNUInstPrinter.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCRegister.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +#define DEBUG_TYPE "asm-printer" + +#include "SystemZGenGNUAsmWriter.inc" + +void SystemZGNUInstPrinter::printFormattedRegName(const MCAsmInfo *MAI, + MCRegister Reg, + raw_ostream &O) const { + const char *RegName = getRegisterName(Reg); + markup(O, Markup::Register) << '%' << RegName; +} + +void SystemZGNUInstPrinter::printInst(const MCInst *MI, uint64_t Address, + StringRef Annot, + const MCSubtargetInfo &STI, + raw_ostream &O) { + printInstruction(MI, Address, O); + printAnnotation(O, Annot); +} diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h new file mode 100644 index 0000000000000..a1c1a7cad7b72 --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h @@ -0,0 +1,46 @@ +//==- SystemZGNUInstPrinter.h - Convert SystemZ MCInst to assembly --*- C++ -*-==// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===-------------------------------------------------------------------------===// +// +// This class prints a SystemZ MCInst to a .s file in GNU assembly format. +// +//===-------------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H +#define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H + +#include "SystemZInstPrinterCommon.h" +#include "llvm/MC/MCInstPrinter.h" +#include + +namespace llvm { + +class MCOperand; + +class SystemZGNUInstPrinter : public SystemZInstPrinterCommon { +public: + SystemZGNUInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, + const MCRegisterInfo &MRI) + : SystemZInstPrinterCommon(MAI, MII, MRI) {} + + // Automatically generated by tblgen. + std::pair getMnemonic(const MCInst *MI) override; + void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O); + static const char *getRegisterName(MCRegister Reg); + + // Override MCInstPrinter. + void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, + const MCSubtargetInfo &STI, raw_ostream &O) override; + +private: + void printFormattedRegName(const MCAsmInfo *MAI, MCRegister Reg, + raw_ostream &O) const override; +}; + +} // end namespace llvm + +#endif // LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp new file mode 100644 index 0000000000000..644e95ffad97c --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp @@ -0,0 +1,35 @@ +//===- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--------------------------------------------------------------------------===// + +#include "SystemZHLASMInstPrinter.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCRegister.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +#define DEBUG_TYPE "asm-printer" + +#include "SystemZGenHLASMAsmWriter.inc" + +void SystemZHLASMInstPrinter::printFormattedRegName(const MCAsmInfo *MAI, + MCRegister Reg, + raw_ostream &O) const { + const char *RegName = getRegisterName(Reg); + // Skip register prefix so that only register number is left + assert(isalpha(RegName[0]) && isdigit(RegName[1])); + markup(O, Markup::Register) << (RegName + 1); +} + +void SystemZHLASMInstPrinter::printInst(const MCInst *MI, uint64_t Address, + StringRef Annot, + const MCSubtargetInfo &STI, + raw_ostream &O) { + printInstruction(MI, Address, O); + printAnnotation(O, Annot); +} diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h new file mode 100644 index 0000000000000..2d535a52dd583 --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h @@ -0,0 +1,45 @@ +//- SystemZHLASMInstPrinter.h - Convert SystemZ MCInst to assembly --*- C++ -*-// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This class prints a SystemZ MCInst to a .s file in HLASM assembly format. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZHLASMINSTPRINTER_H +#define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZHLASMINSTPRINTER_H + +#include "SystemZInstPrinterCommon.h" +#include "llvm/MC/MCInstPrinter.h" +#include + +namespace llvm { + +class MCOperand; + +class SystemZHLASMInstPrinter : public SystemZInstPrinterCommon { +public: + SystemZHLASMInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, + const MCRegisterInfo &MRI) + : SystemZInstPrinterCommon(MAI, MII, MRI) {} + + // Automatically generated by tblgen. + std::pair getMnemonic(const MCInst *MI) override; + void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O); + static const char *getRegisterName(MCRegister Reg); + + void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, + const MCSubtargetInfo &STI, raw_ostream &O) override; + +private: + void printFormattedRegName(const MCAsmInfo *MAI, MCRegister Reg, + raw_ostream &O) const override; +}; + +} // end namespace llvm + +#endif // LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZHLASMINSTPRINTER_H diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp deleted file mode 100644 index fa534fadc3230..0000000000000 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp +++ /dev/null @@ -1,266 +0,0 @@ -//===- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly syntax -===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "SystemZInstPrinter.h" -#include "llvm/MC/MCExpr.h" -#include "llvm/MC/MCInst.h" -#include "llvm/MC/MCRegister.h" -#include "llvm/MC/MCSymbol.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/MathExtras.h" -#include "llvm/Support/raw_ostream.h" -#include -#include - -using namespace llvm; - -#define DEBUG_TYPE "asm-printer" - -#include "SystemZGenAsmWriter.inc" - -void SystemZInstPrinter::printAddress(const MCAsmInfo *MAI, MCRegister Base, - const MCOperand &DispMO, MCRegister Index, - raw_ostream &O) { - printOperand(DispMO, MAI, O); - if (Base || Index) { - O << '('; - if (Index) { - printFormattedRegName(MAI, Index, O); - O << ','; - } - if (Base) - printFormattedRegName(MAI, Base, O); - else - O << '0'; - O << ')'; - } -} - -void SystemZInstPrinter::printOperand(const MCOperand &MO, const MCAsmInfo *MAI, - raw_ostream &O) { - if (MO.isReg()) { - if (!MO.getReg()) - O << '0'; - else - printFormattedRegName(MAI, MO.getReg(), O); - } - else if (MO.isImm()) - markup(O, Markup::Immediate) << MO.getImm(); - else if (MO.isExpr()) - MO.getExpr()->print(O, MAI); - else - llvm_unreachable("Invalid operand"); -} - -void SystemZInstPrinter::printFormattedRegName(const MCAsmInfo *MAI, - MCRegister Reg, - raw_ostream &O) const { - const char *RegName = getRegisterName(Reg); - if (MAI->getAssemblerDialect() == AD_HLASM) { - // Skip register prefix so that only register number is left - assert(isalpha(RegName[0]) && isdigit(RegName[1])); - markup(O, Markup::Register) << (RegName + 1); - } else - markup(O, Markup::Register) << '%' << RegName; -} - -void SystemZInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const { - printFormattedRegName(&MAI, Reg, O); -} - -void SystemZInstPrinter::printInst(const MCInst *MI, uint64_t Address, - StringRef Annot, const MCSubtargetInfo &STI, - raw_ostream &O) { - printInstruction(MI, Address, O); - printAnnotation(O, Annot); -} - -template -void SystemZInstPrinter::printUImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isExpr()) { - O << *MO.getExpr(); - return; - } - uint64_t Value = static_cast(MO.getImm()); - assert(isUInt(Value) && "Invalid uimm argument"); - markup(O, Markup::Immediate) << Value; -} - -template -void SystemZInstPrinter::printSImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isExpr()) { - O << *MO.getExpr(); - return; - } - int64_t Value = MI->getOperand(OpNum).getImm(); - assert(isInt(Value) && "Invalid simm argument"); - markup(O, Markup::Immediate) << Value; -} - -void SystemZInstPrinter::printU1ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<1>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU2ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<2>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU3ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<3>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<4>(MI, OpNum, O); -} - -void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<8>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<8>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU12ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<12>(MI, OpNum, O); -} - -void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<16>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<16>(MI, OpNum, O); -} - -void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<32>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<32>(MI, OpNum, O); -} - -void SystemZInstPrinter::printU48ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<48>(MI, OpNum, O); -} - -void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isImm()) { - WithMarkup M = markup(O, Markup::Immediate); - O << "0x"; - O.write_hex(MO.getImm()); - } else - MO.getExpr()->print(O, &MAI); -} - -void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI, - uint64_t Address, int OpNum, - raw_ostream &O) { - // Output the PC-relative operand. - printPCRelOperand(MI, OpNum, O); - - // Output the TLS marker if present. - if ((unsigned)OpNum + 1 < MI->getNumOperands()) { - const MCOperand &MO = MI->getOperand(OpNum + 1); - const MCSymbolRefExpr &refExp = cast(*MO.getExpr()); - switch (refExp.getKind()) { - case MCSymbolRefExpr::VK_TLSGD: - O << ":tls_gdcall:"; - break; - case MCSymbolRefExpr::VK_TLSLDM: - O << ":tls_ldcall:"; - break; - default: - llvm_unreachable("Unexpected symbol kind"); - } - O << refExp.getSymbol().getName(); - } -} - -void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printOperand(MI->getOperand(OpNum), &MAI, O); -} - -void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - 0, O); -} - -void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - MI->getOperand(OpNum + 2).getReg(), O); -} - -void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - unsigned Base = MI->getOperand(OpNum).getReg(); - const MCOperand &DispMO = MI->getOperand(OpNum + 1); - uint64_t Length = MI->getOperand(OpNum + 2).getImm(); - printOperand(DispMO, &MAI, O); - O << '(' << Length; - if (Base) { - O << ","; - printRegName(O, Base); - } - O << ')'; -} - -void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - unsigned Base = MI->getOperand(OpNum).getReg(); - const MCOperand &DispMO = MI->getOperand(OpNum + 1); - unsigned Length = MI->getOperand(OpNum + 2).getReg(); - printOperand(DispMO, &MAI, O); - O << "("; - printRegName(O, Length); - if (Base) { - O << ","; - printRegName(O, Base); - } - O << ')'; -} - -void SystemZInstPrinter::printBDVAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - MI->getOperand(OpNum + 2).getReg(), O); -} - -void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum, - raw_ostream &O) { - static const char *const CondNames[] = { - "o", "h", "nle", "l", "nhe", "lh", "ne", - "e", "nlh", "he", "nl", "le", "nh", "no" - }; - uint64_t Imm = MI->getOperand(OpNum).getImm(); - assert(Imm > 0 && Imm < 15 && "Invalid condition"); - O << CondNames[Imm - 1]; -} diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp new file mode 100644 index 0000000000000..ce04877fcbe4b --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp @@ -0,0 +1,246 @@ +//=- SystemZInstPrinterCommon.cpp - Common SystemZ MCInst to assembly funcs -=// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SystemZInstPrinterCommon.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCRegister.h" +#include "llvm/MC/MCSymbol.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; + +#define DEBUG_TYPE "asm-printer" + +void SystemZInstPrinterCommon::printAddress(const MCAsmInfo *MAI, + MCRegister Base, + const MCOperand &DispMO, + MCRegister Index, raw_ostream &O) { + printOperand(DispMO, MAI, O); + if (Base || Index) { + O << '('; + if (Index) { + printRegName(O, Index); + O << ','; + } + if (Base) + printRegName(O, Index); + else + O << '0'; + O << ')'; + } +} + +void SystemZInstPrinterCommon::printOperand(const MCOperand &MO, + const MCAsmInfo *MAI, + raw_ostream &O) { + if (MO.isReg()) { + if (!MO.getReg()) + O << '0'; + else + printRegName(O, MO.getReg()); + } else if (MO.isImm()) + markup(O, Markup::Immediate) << MO.getImm(); + else if (MO.isExpr()) + MO.getExpr()->print(O, MAI); + else + llvm_unreachable("Invalid operand"); +} + +void SystemZInstPrinterCommon::printRegName(raw_ostream &O, + MCRegister Reg) const { + printFormattedRegName(&MAI, Reg, O); +} + +template +void SystemZInstPrinterCommon::printUImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isExpr()) { + O << *MO.getExpr(); + return; + } + uint64_t Value = static_cast(MO.getImm()); + assert(isUInt(Value) && "Invalid uimm argument"); + markup(O, Markup::Immediate) << Value; +} + +template +void SystemZInstPrinterCommon::printSImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isExpr()) { + O << *MO.getExpr(); + return; + } + int64_t Value = MI->getOperand(OpNum).getImm(); + assert(isInt(Value) && "Invalid simm argument"); + markup(O, Markup::Immediate) << Value; +} + +void SystemZInstPrinterCommon::printU1ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<1>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU2ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<2>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU3ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<3>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU4ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<4>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printS8ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<8>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU8ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<8>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU12ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<12>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printS16ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<16>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU16ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<16>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printS32ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<32>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU32ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<32>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printU48ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<48>(MI, OpNum, O); +} + +void SystemZInstPrinterCommon::printPCRelOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isImm()) { + WithMarkup M = markup(O, Markup::Immediate); + O << "0x"; + O.write_hex(MO.getImm()); + } else + MO.getExpr()->print(O, &MAI); +} + +void SystemZInstPrinterCommon::printPCRelTLSOperand(const MCInst *MI, + uint64_t Address, int OpNum, + raw_ostream &O) { + // Output the PC-relative operand. + printPCRelOperand(MI, OpNum, O); + + // Output the TLS marker if present. + if ((unsigned)OpNum + 1 < MI->getNumOperands()) { + const MCOperand &MO = MI->getOperand(OpNum + 1); + const MCSymbolRefExpr &refExp = cast(*MO.getExpr()); + switch (refExp.getKind()) { + case MCSymbolRefExpr::VK_TLSGD: + O << ":tls_gdcall:"; + break; + case MCSymbolRefExpr::VK_TLSLDM: + O << ":tls_ldcall:"; + break; + default: + llvm_unreachable("Unexpected symbol kind"); + } + O << refExp.getSymbol().getName(); + } +} + +void SystemZInstPrinterCommon::printOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printOperand(MI->getOperand(OpNum), &MAI, O); +} + +void SystemZInstPrinterCommon::printBDAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + 0, O); +} + +void SystemZInstPrinterCommon::printBDXAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + MI->getOperand(OpNum + 2).getReg(), O); +} + +void SystemZInstPrinterCommon::printBDLAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + unsigned Base = MI->getOperand(OpNum).getReg(); + const MCOperand &DispMO = MI->getOperand(OpNum + 1); + uint64_t Length = MI->getOperand(OpNum + 2).getImm(); + printOperand(DispMO, &MAI, O); + O << '(' << Length; + if (Base) { + O << ","; + printRegName(O, Base); + } + O << ')'; +} + +void SystemZInstPrinterCommon::printBDRAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + unsigned Base = MI->getOperand(OpNum).getReg(); + const MCOperand &DispMO = MI->getOperand(OpNum + 1); + unsigned Length = MI->getOperand(OpNum + 2).getReg(); + printOperand(DispMO, &MAI, O); + O << "("; + printRegName(O, Length); + if (Base) { + O << ","; + printRegName(O, Base); + } + O << ')'; +} + +void SystemZInstPrinterCommon::printBDVAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + MI->getOperand(OpNum + 2).getReg(), O); +} + +void SystemZInstPrinterCommon::printCond4Operand(const MCInst *MI, int OpNum, + raw_ostream &O) { + static const char *const CondNames[] = {"o", "h", "nle", "l", "nhe", + "lh", "ne", "e", "nlh", "he", + "nl", "le", "nh", "no"}; + uint64_t Imm = MI->getOperand(OpNum).getImm(); + assert(Imm > 0 && Imm < 15 && "Invalid condition"); + O << CondNames[Imm - 1]; +} diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h similarity index 78% rename from llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.h rename to llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h index 4e7490dad2996..0f09aba0121c9 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.h +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h @@ -1,4 +1,4 @@ -//==- SystemZInstPrinter.h - Convert SystemZ MCInst to assembly --*- C++ -*-==// +//== SystemZInstPrinterCommon.h - Common SystemZ InstPrinter funcs *- C++ -*==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -10,28 +10,24 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTER_H -#define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTER_H +#ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTERCOMMON_H +#define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTERCOMMON_H #include "SystemZMCAsmInfo.h" #include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCRegister.h" #include namespace llvm { class MCOperand; -class SystemZInstPrinter : public MCInstPrinter { +class SystemZInstPrinterCommon : public MCInstPrinter { public: - SystemZInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII, - const MCRegisterInfo &MRI) + SystemZInstPrinterCommon(const MCAsmInfo &MAI, const MCInstrInfo &MII, + const MCRegisterInfo &MRI) : MCInstPrinter(MAI, MII, MRI) {} - // Automatically generated by tblgen. - std::pair getMnemonic(const MCInst *MI) override; - void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O); - static const char *getRegisterName(MCRegister Reg); - // Print an address with the given base, displacement and index. void printAddress(const MCAsmInfo *MAI, MCRegister Base, const MCOperand &DispMO, MCRegister Index, raw_ostream &O); @@ -39,16 +35,13 @@ class SystemZInstPrinter : public MCInstPrinter { // Print the given operand. void printOperand(const MCOperand &MO, const MCAsmInfo *MAI, raw_ostream &O); - void printFormattedRegName(const MCAsmInfo *MAI, MCRegister Reg, - raw_ostream &O) const; + virtual void printFormattedRegName(const MCAsmInfo *MAI, MCRegister Reg, + raw_ostream &O) const {} // Override MCInstPrinter. void printRegName(raw_ostream &O, MCRegister Reg) const override; - void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, - const MCSubtargetInfo &STI, raw_ostream &O) override; - -private: +protected: template void printUImmOperand(const MCInst *MI, int OpNum, raw_ostream &O); template @@ -92,4 +85,4 @@ class SystemZInstPrinter : public MCInstPrinter { } // end namespace llvm -#endif // LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTER_H +#endif // LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZINSTPRINTERCOMMON_H diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp index f58674ee118ee..058318ab89c05 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp @@ -6,8 +6,9 @@ // //===----------------------------------------------------------------------===// +#include "SystemZGNUInstPrinter.h" #include "SystemZMCTargetDesc.h" -#include "SystemZInstPrinter.h" +#include "SystemZHLASMInstPrinter.h" #include "SystemZMCAsmInfo.h" #include "SystemZTargetStreamer.h" #include "TargetInfo/SystemZTargetInfo.h" @@ -186,7 +187,10 @@ static MCInstPrinter *createSystemZMCInstPrinter(const Triple &T, const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) { - return new SystemZInstPrinter(MAI, MII, MRI); + if (SyntaxVariant == AD_HLASM) + return new SystemZHLASMInstPrinter(MAI, MII, MRI); + + return new SystemZGNUInstPrinter(MAI, MII, MRI); } void SystemZTargetStreamer::emitConstantPools() { diff --git a/llvm/lib/Target/SystemZ/SystemZ.td b/llvm/lib/Target/SystemZ/SystemZ.td index 9d0c77eafa2e2..ec110645c62dd 100644 --- a/llvm/lib/Target/SystemZ/SystemZ.td +++ b/llvm/lib/Target/SystemZ/SystemZ.td @@ -81,6 +81,20 @@ def HLASMAsmParserVariant : AsmParserVariant { string Name = "hlasm"; } +//===----------------------------------------------------------------------===// +// Assembly writer +//===----------------------------------------------------------------------===// + +// The SystemZ target supports two different syntaxes for emitting machine code. +def GNUAsmWriter : AsmWriter { + string AsmWriterClassName = "GNUInstPrinter"; + int Variant = 0; +} +def HLASMAsmWriter : AsmWriter { + string AsmWriterClassName = "HLASMInstPrinter"; + int Variant = 1; +} + //===----------------------------------------------------------------------===// // Top-level target declaration //===----------------------------------------------------------------------===// @@ -89,5 +103,6 @@ def SystemZ : Target { let InstructionSet = SystemZInstrInfo; let AssemblyParsers = [SystemZAsmParser]; let AssemblyParserVariants = [GNUAsmParserVariant, HLASMAsmParserVariant]; + let AssemblyWriters = [GNUAsmWriter, HLASMAsmWriter]; let AllowRegisterRenaming = 1; } diff --git a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp index ed400e9eceb9c..050a482c69d52 100644 --- a/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp +++ b/llvm/lib/Target/SystemZ/SystemZAsmPrinter.cpp @@ -12,7 +12,8 @@ //===----------------------------------------------------------------------===// #include "SystemZAsmPrinter.h" -#include "MCTargetDesc/SystemZInstPrinter.h" +#include "MCTargetDesc/SystemZGNUInstPrinter.h" +#include "MCTargetDesc/SystemZHLASMInstPrinter.h" #include "MCTargetDesc/SystemZMCExpr.h" #include "SystemZConstantPoolValue.h" #include "SystemZMCInstLower.h" @@ -882,13 +883,16 @@ void SystemZAsmPrinter::emitMachineConstantPoolValue( static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo, raw_ostream &OS) { - const char *RegName = SystemZInstPrinter::getRegisterName(RegNo); + const char *RegName; if (MAI->getAssemblerDialect() == AD_HLASM) { + RegName = SystemZHLASMInstPrinter::getRegisterName(RegNo); // Skip register prefix so that only register number is left assert(isalpha(RegName[0]) && isdigit(RegName[1])); OS << (RegName + 1); - } else + } else { + RegName = SystemZGNUInstPrinter::getRegisterName(RegNo); OS << '%' << RegName; + } } static void printReg(unsigned Reg, const MCAsmInfo *MAI, raw_ostream &OS) { From 59ebdf44fbfd4b87c55fc3b4ee186f0a0c143fbd Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 15:27:16 -0400 Subject: [PATCH 2/8] fix typo and formatting --- .../lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h | 6 +++--- .../Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp | 4 ++-- .../Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h | 2 +- .../SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp | 2 +- .../Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h | 2 +- .../lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h index a1c1a7cad7b72..a7df82f7c16ea 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h @@ -1,14 +1,14 @@ -//==- SystemZGNUInstPrinter.h - Convert SystemZ MCInst to assembly --*- C++ -*-==// +//==- SystemZGNUInstPrinter.h - Convert SystemZ MCInst to assembly -*- C++ -*-==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -//===-------------------------------------------------------------------------===// +//===------------------------------------------------------------------------===// // // This class prints a SystemZ MCInst to a .s file in GNU assembly format. // -//===-------------------------------------------------------------------------===// +//===------------------------------------------------------------------------===// #ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H #define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp index 644e95ffad97c..6d975295c17ff 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp @@ -1,10 +1,10 @@ -//===- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -===// +//===- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -==// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -//===--------------------------------------------------------------------------===// +//===--------------------------------------------------------------------------==// #include "SystemZHLASMInstPrinter.h" #include "llvm/MC/MCInst.h" diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h index 2d535a52dd583..9a69e012c7294 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.h @@ -1,4 +1,4 @@ -//- SystemZHLASMInstPrinter.h - Convert SystemZ MCInst to assembly --*- C++ -*-// +//- SystemZHLASMInstPrinter.h - Convert SystemZ MCInst to assembly -*- C++ -*-// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp index ce04877fcbe4b..00560ab1f4b18 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp @@ -34,7 +34,7 @@ void SystemZInstPrinterCommon::printAddress(const MCAsmInfo *MAI, O << ','; } if (Base) - printRegName(O, Index); + printRegName(O, Base); else O << '0'; O << ')'; diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h index 0f09aba0121c9..9a972824f7ffb 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.h @@ -26,7 +26,7 @@ class SystemZInstPrinterCommon : public MCInstPrinter { public: SystemZInstPrinterCommon(const MCAsmInfo &MAI, const MCInstrInfo &MII, const MCRegisterInfo &MRI) - : MCInstPrinter(MAI, MII, MRI) {} + : MCInstPrinter(MAI, MII, MRI) {} // Print an address with the given base, displacement and index. void printAddress(const MCAsmInfo *MAI, MCRegister Base, diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp index 058318ab89c05..333221c46ebb8 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCTargetDesc.cpp @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "SystemZGNUInstPrinter.h" #include "SystemZMCTargetDesc.h" +#include "SystemZGNUInstPrinter.h" #include "SystemZHLASMInstPrinter.h" #include "SystemZMCAsmInfo.h" #include "SystemZTargetStreamer.h" From 2f4a789225cfb843f185ce65e5e6f7f93809c327 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 15:30:30 -0400 Subject: [PATCH 3/8] more format fixes --- .../lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h | 6 +++--- .../Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h index a7df82f7c16ea..8f62ae0e16c00 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZGNUInstPrinter.h @@ -1,14 +1,14 @@ -//==- SystemZGNUInstPrinter.h - Convert SystemZ MCInst to assembly -*- C++ -*-==// +//=- SystemZGNUInstPrinter.h - Convert SystemZ MCInst to assembly -*- C++ -*-=// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -//===------------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// // // This class prints a SystemZ MCInst to a .s file in GNU assembly format. // -//===------------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// #ifndef LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H #define LLVM_LIB_TARGET_SYSTEMZ_MCTARGETDESC_SYSTEMZGNUINSTPRINTER_H diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp index 6d975295c17ff..ed7ff83a3c6df 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZHLASMInstPrinter.cpp @@ -1,10 +1,10 @@ -//===- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -==// +//=- SystemZHLASMInstPrinter.cpp - Convert SystemZ MCInst to HLASM assembly -=// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // -//===--------------------------------------------------------------------------==// +//===----------------------------------------------------------------------===// #include "SystemZHLASMInstPrinter.h" #include "llvm/MC/MCInst.h" From da7ef0313565b8163a90b6f1ae6e65b0c90cc902 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 15:56:01 -0400 Subject: [PATCH 4/8] attempt to make git recognize renamed file --- .../MCTargetDesc/SystemZInstPrinter.cpp | 266 ++++++++++++++++++ .../MCTargetDesc/SystemZInstPrinterCommon.cpp | 246 ---------------- 2 files changed, 266 insertions(+), 246 deletions(-) create mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp delete mode 100644 llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp new file mode 100644 index 0000000000000..fa534fadc3230 --- /dev/null +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp @@ -0,0 +1,266 @@ +//===- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly syntax -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SystemZInstPrinter.h" +#include "llvm/MC/MCExpr.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCRegister.h" +#include "llvm/MC/MCSymbol.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/MathExtras.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; + +#define DEBUG_TYPE "asm-printer" + +#include "SystemZGenAsmWriter.inc" + +void SystemZInstPrinter::printAddress(const MCAsmInfo *MAI, MCRegister Base, + const MCOperand &DispMO, MCRegister Index, + raw_ostream &O) { + printOperand(DispMO, MAI, O); + if (Base || Index) { + O << '('; + if (Index) { + printFormattedRegName(MAI, Index, O); + O << ','; + } + if (Base) + printFormattedRegName(MAI, Base, O); + else + O << '0'; + O << ')'; + } +} + +void SystemZInstPrinter::printOperand(const MCOperand &MO, const MCAsmInfo *MAI, + raw_ostream &O) { + if (MO.isReg()) { + if (!MO.getReg()) + O << '0'; + else + printFormattedRegName(MAI, MO.getReg(), O); + } + else if (MO.isImm()) + markup(O, Markup::Immediate) << MO.getImm(); + else if (MO.isExpr()) + MO.getExpr()->print(O, MAI); + else + llvm_unreachable("Invalid operand"); +} + +void SystemZInstPrinter::printFormattedRegName(const MCAsmInfo *MAI, + MCRegister Reg, + raw_ostream &O) const { + const char *RegName = getRegisterName(Reg); + if (MAI->getAssemblerDialect() == AD_HLASM) { + // Skip register prefix so that only register number is left + assert(isalpha(RegName[0]) && isdigit(RegName[1])); + markup(O, Markup::Register) << (RegName + 1); + } else + markup(O, Markup::Register) << '%' << RegName; +} + +void SystemZInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const { + printFormattedRegName(&MAI, Reg, O); +} + +void SystemZInstPrinter::printInst(const MCInst *MI, uint64_t Address, + StringRef Annot, const MCSubtargetInfo &STI, + raw_ostream &O) { + printInstruction(MI, Address, O); + printAnnotation(O, Annot); +} + +template +void SystemZInstPrinter::printUImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isExpr()) { + O << *MO.getExpr(); + return; + } + uint64_t Value = static_cast(MO.getImm()); + assert(isUInt(Value) && "Invalid uimm argument"); + markup(O, Markup::Immediate) << Value; +} + +template +void SystemZInstPrinter::printSImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isExpr()) { + O << *MO.getExpr(); + return; + } + int64_t Value = MI->getOperand(OpNum).getImm(); + assert(isInt(Value) && "Invalid simm argument"); + markup(O, Markup::Immediate) << Value; +} + +void SystemZInstPrinter::printU1ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<1>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU2ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<2>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU3ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<3>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<4>(MI, OpNum, O); +} + +void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<8>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<8>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU12ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<12>(MI, OpNum, O); +} + +void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<16>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<16>(MI, OpNum, O); +} + +void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printSImmOperand<32>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<32>(MI, OpNum, O); +} + +void SystemZInstPrinter::printU48ImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printUImmOperand<48>(MI, OpNum, O); +} + +void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + const MCOperand &MO = MI->getOperand(OpNum); + if (MO.isImm()) { + WithMarkup M = markup(O, Markup::Immediate); + O << "0x"; + O.write_hex(MO.getImm()); + } else + MO.getExpr()->print(O, &MAI); +} + +void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI, + uint64_t Address, int OpNum, + raw_ostream &O) { + // Output the PC-relative operand. + printPCRelOperand(MI, OpNum, O); + + // Output the TLS marker if present. + if ((unsigned)OpNum + 1 < MI->getNumOperands()) { + const MCOperand &MO = MI->getOperand(OpNum + 1); + const MCSymbolRefExpr &refExp = cast(*MO.getExpr()); + switch (refExp.getKind()) { + case MCSymbolRefExpr::VK_TLSGD: + O << ":tls_gdcall:"; + break; + case MCSymbolRefExpr::VK_TLSLDM: + O << ":tls_ldcall:"; + break; + default: + llvm_unreachable("Unexpected symbol kind"); + } + O << refExp.getSymbol().getName(); + } +} + +void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printOperand(MI->getOperand(OpNum), &MAI, O); +} + +void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + 0, O); +} + +void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + MI->getOperand(OpNum + 2).getReg(), O); +} + +void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + unsigned Base = MI->getOperand(OpNum).getReg(); + const MCOperand &DispMO = MI->getOperand(OpNum + 1); + uint64_t Length = MI->getOperand(OpNum + 2).getImm(); + printOperand(DispMO, &MAI, O); + O << '(' << Length; + if (Base) { + O << ","; + printRegName(O, Base); + } + O << ')'; +} + +void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + unsigned Base = MI->getOperand(OpNum).getReg(); + const MCOperand &DispMO = MI->getOperand(OpNum + 1); + unsigned Length = MI->getOperand(OpNum + 2).getReg(); + printOperand(DispMO, &MAI, O); + O << "("; + printRegName(O, Length); + if (Base) { + O << ","; + printRegName(O, Base); + } + O << ')'; +} + +void SystemZInstPrinter::printBDVAddrOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { + printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), + MI->getOperand(OpNum + 2).getReg(), O); +} + +void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum, + raw_ostream &O) { + static const char *const CondNames[] = { + "o", "h", "nle", "l", "nhe", "lh", "ne", + "e", "nlh", "he", "nl", "le", "nh", "no" + }; + uint64_t Imm = MI->getOperand(OpNum).getImm(); + assert(Imm > 0 && Imm < 15 && "Invalid condition"); + O << CondNames[Imm - 1]; +} diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp deleted file mode 100644 index 00560ab1f4b18..0000000000000 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp +++ /dev/null @@ -1,246 +0,0 @@ -//=- SystemZInstPrinterCommon.cpp - Common SystemZ MCInst to assembly funcs -=// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#include "SystemZInstPrinterCommon.h" -#include "llvm/MC/MCExpr.h" -#include "llvm/MC/MCInst.h" -#include "llvm/MC/MCRegister.h" -#include "llvm/MC/MCSymbol.h" -#include "llvm/Support/Casting.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/MathExtras.h" -#include "llvm/Support/raw_ostream.h" -#include -#include - -using namespace llvm; - -#define DEBUG_TYPE "asm-printer" - -void SystemZInstPrinterCommon::printAddress(const MCAsmInfo *MAI, - MCRegister Base, - const MCOperand &DispMO, - MCRegister Index, raw_ostream &O) { - printOperand(DispMO, MAI, O); - if (Base || Index) { - O << '('; - if (Index) { - printRegName(O, Index); - O << ','; - } - if (Base) - printRegName(O, Base); - else - O << '0'; - O << ')'; - } -} - -void SystemZInstPrinterCommon::printOperand(const MCOperand &MO, - const MCAsmInfo *MAI, - raw_ostream &O) { - if (MO.isReg()) { - if (!MO.getReg()) - O << '0'; - else - printRegName(O, MO.getReg()); - } else if (MO.isImm()) - markup(O, Markup::Immediate) << MO.getImm(); - else if (MO.isExpr()) - MO.getExpr()->print(O, MAI); - else - llvm_unreachable("Invalid operand"); -} - -void SystemZInstPrinterCommon::printRegName(raw_ostream &O, - MCRegister Reg) const { - printFormattedRegName(&MAI, Reg, O); -} - -template -void SystemZInstPrinterCommon::printUImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isExpr()) { - O << *MO.getExpr(); - return; - } - uint64_t Value = static_cast(MO.getImm()); - assert(isUInt(Value) && "Invalid uimm argument"); - markup(O, Markup::Immediate) << Value; -} - -template -void SystemZInstPrinterCommon::printSImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isExpr()) { - O << *MO.getExpr(); - return; - } - int64_t Value = MI->getOperand(OpNum).getImm(); - assert(isInt(Value) && "Invalid simm argument"); - markup(O, Markup::Immediate) << Value; -} - -void SystemZInstPrinterCommon::printU1ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<1>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU2ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<2>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU3ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<3>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU4ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<4>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printS8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<8>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<8>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU12ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<12>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printS16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<16>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<16>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printS32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printSImmOperand<32>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<32>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printU48ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printUImmOperand<48>(MI, OpNum, O); -} - -void SystemZInstPrinterCommon::printPCRelOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - const MCOperand &MO = MI->getOperand(OpNum); - if (MO.isImm()) { - WithMarkup M = markup(O, Markup::Immediate); - O << "0x"; - O.write_hex(MO.getImm()); - } else - MO.getExpr()->print(O, &MAI); -} - -void SystemZInstPrinterCommon::printPCRelTLSOperand(const MCInst *MI, - uint64_t Address, int OpNum, - raw_ostream &O) { - // Output the PC-relative operand. - printPCRelOperand(MI, OpNum, O); - - // Output the TLS marker if present. - if ((unsigned)OpNum + 1 < MI->getNumOperands()) { - const MCOperand &MO = MI->getOperand(OpNum + 1); - const MCSymbolRefExpr &refExp = cast(*MO.getExpr()); - switch (refExp.getKind()) { - case MCSymbolRefExpr::VK_TLSGD: - O << ":tls_gdcall:"; - break; - case MCSymbolRefExpr::VK_TLSLDM: - O << ":tls_ldcall:"; - break; - default: - llvm_unreachable("Unexpected symbol kind"); - } - O << refExp.getSymbol().getName(); - } -} - -void SystemZInstPrinterCommon::printOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printOperand(MI->getOperand(OpNum), &MAI, O); -} - -void SystemZInstPrinterCommon::printBDAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - 0, O); -} - -void SystemZInstPrinterCommon::printBDXAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - MI->getOperand(OpNum + 2).getReg(), O); -} - -void SystemZInstPrinterCommon::printBDLAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - unsigned Base = MI->getOperand(OpNum).getReg(); - const MCOperand &DispMO = MI->getOperand(OpNum + 1); - uint64_t Length = MI->getOperand(OpNum + 2).getImm(); - printOperand(DispMO, &MAI, O); - O << '(' << Length; - if (Base) { - O << ","; - printRegName(O, Base); - } - O << ')'; -} - -void SystemZInstPrinterCommon::printBDRAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - unsigned Base = MI->getOperand(OpNum).getReg(); - const MCOperand &DispMO = MI->getOperand(OpNum + 1); - unsigned Length = MI->getOperand(OpNum + 2).getReg(); - printOperand(DispMO, &MAI, O); - O << "("; - printRegName(O, Length); - if (Base) { - O << ","; - printRegName(O, Base); - } - O << ')'; -} - -void SystemZInstPrinterCommon::printBDVAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { - printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), - MI->getOperand(OpNum + 2).getReg(), O); -} - -void SystemZInstPrinterCommon::printCond4Operand(const MCInst *MI, int OpNum, - raw_ostream &O) { - static const char *const CondNames[] = {"o", "h", "nle", "l", "nhe", - "lh", "ne", "e", "nlh", "he", - "nl", "le", "nh", "no"}; - uint64_t Imm = MI->getOperand(OpNum).getImm(); - assert(Imm > 0 && Imm < 15 && "Invalid condition"); - O << CondNames[Imm - 1]; -} From 8dcb71aaf9154ad1db4c56d29072b77485c775aa Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 16:00:19 -0400 Subject: [PATCH 5/8] make changed to original file first --- .../MCTargetDesc/SystemZInstPrinter.cpp | 99 ++++++++----------- 1 file changed, 40 insertions(+), 59 deletions(-) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp index fa534fadc3230..293c5b16cb722 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp @@ -1,4 +1,4 @@ -//===- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly syntax -===// +//=- SystemZInstPrinterCommon.cpp - Common SystemZ MCInst to assembly funcs -=// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,7 @@ // //===----------------------------------------------------------------------===// -#include "SystemZInstPrinter.h" +#include "SystemZInstPrinterCommon.h" #include "llvm/MC/MCExpr.h" #include "llvm/MC/MCInst.h" #include "llvm/MC/MCRegister.h" @@ -22,35 +22,34 @@ using namespace llvm; #define DEBUG_TYPE "asm-printer" -#include "SystemZGenAsmWriter.inc" - -void SystemZInstPrinter::printAddress(const MCAsmInfo *MAI, MCRegister Base, - const MCOperand &DispMO, MCRegister Index, - raw_ostream &O) { +void SystemZInstPrinterCommon::printAddress(const MCAsmInfo *MAI, + MCRegister Base, + const MCOperand &DispMO, + MCRegister Index, raw_ostream &O) { printOperand(DispMO, MAI, O); if (Base || Index) { O << '('; if (Index) { - printFormattedRegName(MAI, Index, O); + printRegName(O, Index); O << ','; } if (Base) - printFormattedRegName(MAI, Base, O); + printRegName(O, Base); else O << '0'; O << ')'; } } -void SystemZInstPrinter::printOperand(const MCOperand &MO, const MCAsmInfo *MAI, - raw_ostream &O) { +void SystemZInstPrinterCommon::printOperand(const MCOperand &MO, + const MCAsmInfo *MAI, + raw_ostream &O) { if (MO.isReg()) { if (!MO.getReg()) O << '0'; else - printFormattedRegName(MAI, MO.getReg(), O); - } - else if (MO.isImm()) + printRegName(MAI, MO.getReg(), O); + } else if (MO.isImm()) markup(O, Markup::Immediate) << MO.getImm(); else if (MO.isExpr()) MO.getExpr()->print(O, MAI); @@ -58,32 +57,14 @@ void SystemZInstPrinter::printOperand(const MCOperand &MO, const MCAsmInfo *MAI, llvm_unreachable("Invalid operand"); } -void SystemZInstPrinter::printFormattedRegName(const MCAsmInfo *MAI, - MCRegister Reg, - raw_ostream &O) const { - const char *RegName = getRegisterName(Reg); - if (MAI->getAssemblerDialect() == AD_HLASM) { - // Skip register prefix so that only register number is left - assert(isalpha(RegName[0]) && isdigit(RegName[1])); - markup(O, Markup::Register) << (RegName + 1); - } else - markup(O, Markup::Register) << '%' << RegName; -} - -void SystemZInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) const { +void SystemZInstPrinterCommon::printRegName(raw_ostream &O, + MCRegister Reg) const { printFormattedRegName(&MAI, Reg, O); } -void SystemZInstPrinter::printInst(const MCInst *MI, uint64_t Address, - StringRef Annot, const MCSubtargetInfo &STI, - raw_ostream &O) { - printInstruction(MI, Address, O); - printAnnotation(O, Annot); -} - template -void SystemZInstPrinter::printUImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { +void SystemZInstPrinterCommon::printUImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { const MCOperand &MO = MI->getOperand(OpNum); if (MO.isExpr()) { O << *MO.getExpr(); @@ -95,8 +76,8 @@ void SystemZInstPrinter::printUImmOperand(const MCInst *MI, int OpNum, } template -void SystemZInstPrinter::printSImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { +void SystemZInstPrinterCommon::printSImmOperand(const MCInst *MI, int OpNum, + raw_ostream &O) { const MCOperand &MO = MI->getOperand(OpNum); if (MO.isExpr()) { O << *MO.getExpr(); @@ -107,67 +88,67 @@ void SystemZInstPrinter::printSImmOperand(const MCInst *MI, int OpNum, markup(O, Markup::Immediate) << Value; } -void SystemZInstPrinter::printU1ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU1ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<1>(MI, OpNum, O); } -void SystemZInstPrinter::printU2ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU2ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<2>(MI, OpNum, O); } -void SystemZInstPrinter::printU3ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU3ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<3>(MI, OpNum, O); } -void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU4ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<4>(MI, OpNum, O); } -void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printS8ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printSImmOperand<8>(MI, OpNum, O); } -void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU8ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<8>(MI, OpNum, O); } -void SystemZInstPrinter::printU12ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU12ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<12>(MI, OpNum, O); } -void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printS16ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printSImmOperand<16>(MI, OpNum, O); } -void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU16ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<16>(MI, OpNum, O); } -void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printS32ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printSImmOperand<32>(MI, OpNum, O); } -void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU32ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<32>(MI, OpNum, O); } -void SystemZInstPrinter::printU48ImmOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printU48ImmOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printUImmOperand<48>(MI, OpNum, O); } -void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printPCRelOperand(const MCInst *MI, int OpNum, raw_ostream &O) { const MCOperand &MO = MI->getOperand(OpNum); if (MO.isImm()) { @@ -178,7 +159,7 @@ void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum, MO.getExpr()->print(O, &MAI); } -void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI, +void SystemZInstPrinterCommon::printPCRelTLSOperand(const MCInst *MI, uint64_t Address, int OpNum, raw_ostream &O) { // Output the PC-relative operand. @@ -202,24 +183,24 @@ void SystemZInstPrinter::printPCRelTLSOperand(const MCInst *MI, } } -void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printOperand(MI->getOperand(OpNum), &MAI, O); } -void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printBDAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), 0, O); } -void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printBDXAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), MI->getOperand(OpNum + 2).getReg(), O); } -void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printBDLAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O) { unsigned Base = MI->getOperand(OpNum).getReg(); const MCOperand &DispMO = MI->getOperand(OpNum + 1); @@ -233,7 +214,7 @@ void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum, O << ')'; } -void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printBDRAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O) { unsigned Base = MI->getOperand(OpNum).getReg(); const MCOperand &DispMO = MI->getOperand(OpNum + 1); @@ -248,13 +229,13 @@ void SystemZInstPrinter::printBDRAddrOperand(const MCInst *MI, int OpNum, O << ')'; } -void SystemZInstPrinter::printBDVAddrOperand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printBDVAddrOperand(const MCInst *MI, int OpNum, raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), MI->getOperand(OpNum + 2).getReg(), O); } -void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum, +void SystemZInstPrinterCommon::printCond4Operand(const MCInst *MI, int OpNum, raw_ostream &O) { static const char *const CondNames[] = { "o", "h", "nle", "l", "nhe", "lh", "ne", From ec011f5cb76ef3490bd44a336d76adcf0a1ce332 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 16:00:45 -0400 Subject: [PATCH 6/8] update file name --- .../{SystemZInstPrinter.cpp => SystemZInstPrinterCommon.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename llvm/lib/Target/SystemZ/MCTargetDesc/{SystemZInstPrinter.cpp => SystemZInstPrinterCommon.cpp} (100%) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp similarity index 100% rename from llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinter.cpp rename to llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp From f211b6eb6cf7fe48a75a8bdbd3005166d3e7bca9 Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 16:07:21 -0400 Subject: [PATCH 7/8] minor typo fix --- .../Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp index 293c5b16cb722..23379cc521ecd 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp @@ -48,7 +48,7 @@ void SystemZInstPrinterCommon::printOperand(const MCOperand &MO, if (!MO.getReg()) O << '0'; else - printRegName(MAI, MO.getReg(), O); + printRegName(O, MO.getReg()); } else if (MO.isImm()) markup(O, Markup::Immediate) << MO.getImm(); else if (MO.isExpr()) From be0ac71a8b81ba69741b6b36c8e9416acefb49cc Mon Sep 17 00:00:00 2001 From: Tony Tao Date: Mon, 21 Oct 2024 16:38:25 -0400 Subject: [PATCH 8/8] more formatting --- .../MCTargetDesc/SystemZInstPrinterCommon.cpp | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp index 23379cc521ecd..00560ab1f4b18 100644 --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZInstPrinterCommon.cpp @@ -89,67 +89,67 @@ void SystemZInstPrinterCommon::printSImmOperand(const MCInst *MI, int OpNum, } void SystemZInstPrinterCommon::printU1ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<1>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU2ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<2>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU3ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<3>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU4ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<4>(MI, OpNum, O); } void SystemZInstPrinterCommon::printS8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printSImmOperand<8>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU8ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<8>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU12ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<12>(MI, OpNum, O); } void SystemZInstPrinterCommon::printS16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printSImmOperand<16>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU16ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<16>(MI, OpNum, O); } void SystemZInstPrinterCommon::printS32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printSImmOperand<32>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU32ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<32>(MI, OpNum, O); } void SystemZInstPrinterCommon::printU48ImmOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printUImmOperand<48>(MI, OpNum, O); } void SystemZInstPrinterCommon::printPCRelOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { const MCOperand &MO = MI->getOperand(OpNum); if (MO.isImm()) { WithMarkup M = markup(O, Markup::Immediate); @@ -160,8 +160,8 @@ void SystemZInstPrinterCommon::printPCRelOperand(const MCInst *MI, int OpNum, } void SystemZInstPrinterCommon::printPCRelTLSOperand(const MCInst *MI, - uint64_t Address, int OpNum, - raw_ostream &O) { + uint64_t Address, int OpNum, + raw_ostream &O) { // Output the PC-relative operand. printPCRelOperand(MI, OpNum, O); @@ -170,38 +170,38 @@ void SystemZInstPrinterCommon::printPCRelTLSOperand(const MCInst *MI, const MCOperand &MO = MI->getOperand(OpNum + 1); const MCSymbolRefExpr &refExp = cast(*MO.getExpr()); switch (refExp.getKind()) { - case MCSymbolRefExpr::VK_TLSGD: - O << ":tls_gdcall:"; - break; - case MCSymbolRefExpr::VK_TLSLDM: - O << ":tls_ldcall:"; - break; - default: - llvm_unreachable("Unexpected symbol kind"); + case MCSymbolRefExpr::VK_TLSGD: + O << ":tls_gdcall:"; + break; + case MCSymbolRefExpr::VK_TLSLDM: + O << ":tls_ldcall:"; + break; + default: + llvm_unreachable("Unexpected symbol kind"); } O << refExp.getSymbol().getName(); } } void SystemZInstPrinterCommon::printOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printOperand(MI->getOperand(OpNum), &MAI, O); } void SystemZInstPrinterCommon::printBDAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), 0, O); } void SystemZInstPrinterCommon::printBDXAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), MI->getOperand(OpNum + 2).getReg(), O); } void SystemZInstPrinterCommon::printBDLAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { unsigned Base = MI->getOperand(OpNum).getReg(); const MCOperand &DispMO = MI->getOperand(OpNum + 1); uint64_t Length = MI->getOperand(OpNum + 2).getImm(); @@ -215,7 +215,7 @@ void SystemZInstPrinterCommon::printBDLAddrOperand(const MCInst *MI, int OpNum, } void SystemZInstPrinterCommon::printBDRAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { unsigned Base = MI->getOperand(OpNum).getReg(); const MCOperand &DispMO = MI->getOperand(OpNum + 1); unsigned Length = MI->getOperand(OpNum + 2).getReg(); @@ -230,17 +230,16 @@ void SystemZInstPrinterCommon::printBDRAddrOperand(const MCInst *MI, int OpNum, } void SystemZInstPrinterCommon::printBDVAddrOperand(const MCInst *MI, int OpNum, - raw_ostream &O) { + raw_ostream &O) { printAddress(&MAI, MI->getOperand(OpNum).getReg(), MI->getOperand(OpNum + 1), MI->getOperand(OpNum + 2).getReg(), O); } void SystemZInstPrinterCommon::printCond4Operand(const MCInst *MI, int OpNum, - raw_ostream &O) { - static const char *const CondNames[] = { - "o", "h", "nle", "l", "nhe", "lh", "ne", - "e", "nlh", "he", "nl", "le", "nh", "no" - }; + raw_ostream &O) { + static const char *const CondNames[] = {"o", "h", "nle", "l", "nhe", + "lh", "ne", "e", "nlh", "he", + "nl", "le", "nh", "no"}; uint64_t Imm = MI->getOperand(OpNum).getImm(); assert(Imm > 0 && Imm < 15 && "Invalid condition"); O << CondNames[Imm - 1];