[llvm] aaa2503 - [M68k] Factoring out memory operand printer into a separate file

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 8 13:53:51 PST 2023


Author: Min-Yih Hsu
Date: 2023-03-08T13:51:53-08:00
New Revision: aaa2503ad9629ffe5eb9b325bf867a89bd19a31e

URL: https://github.com/llvm/llvm-project/commit/aaa2503ad9629ffe5eb9b325bf867a89bd19a31e
DIFF: https://github.com/llvm/llvm-project/commit/aaa2503ad9629ffe5eb9b325bf867a89bd19a31e.diff

LOG: [M68k] Factoring out memory operand printer into a separate file

In order to support inline asm with memory constraints,
AsmPrinter::PrintAsmMemOperand needs to be implemented, which has lots
of overlaps with MCInstPrinter especially on the format of complex
addressing modes. This patch factors out the common printing logics from
MCInstPrinter into a separate class inherited by both AsmPrinter and
MCInstPrinter, in which the derived classes only need to provide
primitives like printOperand and printDisp.

This change is basically NFC. See D143529 for changes on AsmPrinter.

Differential Revision: https://reviews.llvm.org/D143528

Added: 
    llvm/lib/Target/M68k/MCTargetDesc/M68kMemOperandPrinter.h

Modified: 
    llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.cpp
    llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.cpp b/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.cpp
index 97a5af45de022..84800fc762cbb 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.cpp
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.cpp
@@ -147,47 +147,6 @@ void M68kInstPrinter::printDisp(const MCInst *MI, unsigned opNum,
   Op.getExpr()->print(O, &MAI);
 }
 
-void M68kInstPrinter::printARIMem(const MCInst *MI, unsigned opNum,
-                                  raw_ostream &O) {
-  O << '(';
-  printOperand(MI, opNum, O);
-  O << ')';
-}
-
-void M68kInstPrinter::printARIPIMem(const MCInst *MI, unsigned opNum,
-                                    raw_ostream &O) {
-  O << "(";
-  printOperand(MI, opNum, O);
-  O << ")+";
-}
-
-void M68kInstPrinter::printARIPDMem(const MCInst *MI, unsigned opNum,
-                                    raw_ostream &O) {
-  O << "-(";
-  printOperand(MI, opNum, O);
-  O << ")";
-}
-
-void M68kInstPrinter::printARIDMem(const MCInst *MI, unsigned opNum,
-                                   raw_ostream &O) {
-  O << '(';
-  printDisp(MI, opNum + M68k::MemDisp, O);
-  O << ',';
-  printOperand(MI, opNum + M68k::MemBase, O);
-  O << ')';
-}
-
-void M68kInstPrinter::printARIIMem(const MCInst *MI, unsigned opNum,
-                                   raw_ostream &O) {
-  O << '(';
-  printDisp(MI, opNum + M68k::MemDisp, O);
-  O << ',';
-  printOperand(MI, opNum + M68k::MemBase, O);
-  O << ',';
-  printOperand(MI, opNum + M68k::MemIndex, O);
-  O << ')';
-}
-
 // NOTE forcing (W,L) size available since M68020 only
 void M68kInstPrinter::printAbsMem(const MCInst *MI, unsigned opNum,
                                   raw_ostream &O) {
@@ -201,19 +160,3 @@ void M68kInstPrinter::printAbsMem(const MCInst *MI, unsigned opNum,
   assert(MO.isImm() && "absolute memory addressing needs an immediate");
   O << format("$%0" PRIx64, (uint64_t)MO.getImm());
 }
-
-void M68kInstPrinter::printPCDMem(const MCInst *MI, uint64_t Address,
-                                  unsigned opNum, raw_ostream &O) {
-  O << '(';
-  printDisp(MI, opNum + M68k::PCRelDisp, O);
-  O << ",%pc)";
-}
-
-void M68kInstPrinter::printPCIMem(const MCInst *MI, uint64_t Address,
-                                  unsigned opNum, raw_ostream &O) {
-  O << '(';
-  printDisp(MI, opNum + M68k::PCRelDisp, O);
-  O << ",%pc,";
-  printOperand(MI, opNum + M68k::PCRelIndex, O);
-  O << ')';
-}

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h b/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h
index 5e104856adb16..0963176304587 100644
--- a/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kInstPrinter.h
@@ -14,13 +14,17 @@
 #ifndef LLVM_LIB_TARGET_M68K_INSTPRINTER_M68KINSTPRINTER_H
 #define LLVM_LIB_TARGET_M68K_INSTPRINTER_M68KINSTPRINTER_H
 
+#include "M68kMemOperandPrinter.h"
 #include "llvm/MC/MCInstPrinter.h"
 
 namespace llvm {
 
 class TargetMachine;
 
-class M68kInstPrinter : public MCInstPrinter {
+class M68kInstPrinter : public MCInstPrinter,
+                        public M68kMemOperandPrinter<M68kInstPrinter, MCInst> {
+  friend class M68kMemOperandPrinter;
+
 public:
   M68kInstPrinter(const MCAsmInfo &MAI, const MCInstrInfo &MII,
                   const MCRegisterInfo &MRI)
@@ -48,16 +52,7 @@ class M68kInstPrinter : public MCInstPrinter {
   /// Print register mask for MOVEM instruction in order A7-A0,D7-D0
   void printMoveMaskR(const MCInst *MI, unsigned opNum, raw_ostream &O);
   void printDisp(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printARIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printARIPIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printARIPDMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printARIDMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printARIIMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
   void printAbsMem(const MCInst *MI, unsigned opNum, raw_ostream &O);
-  void printPCDMem(const MCInst *MI, uint64_t Address, unsigned opNum,
-                   raw_ostream &O);
-  void printPCIMem(const MCInst *MI, uint64_t Address, unsigned opNum,
-                   raw_ostream &O);
 
   //===----------------------------------------------------------------------===//
   // Specializations

diff  --git a/llvm/lib/Target/M68k/MCTargetDesc/M68kMemOperandPrinter.h b/llvm/lib/Target/M68k/MCTargetDesc/M68kMemOperandPrinter.h
new file mode 100644
index 0000000000000..cc5cc7a37e857
--- /dev/null
+++ b/llvm/lib/Target/M68k/MCTargetDesc/M68kMemOperandPrinter.h
@@ -0,0 +1,80 @@
+//===-- M68kMemOperandPrinter.h - Memory operands printing ------*- 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains memory operand printing logics shared between AsmPrinter
+//  and MCInstPrinter.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_M68K_MEMOPERANDPRINTER_M68KINSTPRINTER_H
+#define LLVM_LIB_TARGET_M68K_MEMOPERANDPRINTER_M68KINSTPRINTER_H
+
+#include "M68kBaseInfo.h"
+
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+template <class Derived, typename InstTy> class M68kMemOperandPrinter {
+  Derived &impl() { return *static_cast<Derived *>(this); }
+
+protected:
+  void printARIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
+    O << '(';
+    impl().printOperand(MI, OpNum, O);
+    O << ')';
+  }
+
+  void printARIPIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
+    O << "(";
+    impl().printOperand(MI, OpNum, O);
+    O << ")+";
+  }
+
+  void printARIPDMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
+    O << "-(";
+    impl().printOperand(MI, OpNum, O);
+    O << ")";
+  }
+
+  void printARIDMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
+    O << '(';
+    impl().printDisp(MI, OpNum + M68k::MemDisp, O);
+    O << ',';
+    impl().printOperand(MI, OpNum + M68k::MemBase, O);
+    O << ')';
+  }
+
+  void printARIIMem(const InstTy *MI, unsigned OpNum, raw_ostream &O) {
+    O << '(';
+    impl().printDisp(MI, OpNum + M68k::MemDisp, O);
+    O << ',';
+    impl().printOperand(MI, OpNum + M68k::MemBase, O);
+    O << ',';
+    impl().printOperand(MI, OpNum + M68k::MemIndex, O);
+    O << ')';
+  }
+
+  void printPCDMem(const InstTy *MI, uint64_t Address, unsigned OpNum,
+                   raw_ostream &O) {
+    O << '(';
+    impl().printDisp(MI, OpNum + M68k::PCRelDisp, O);
+    O << ",%pc)";
+  }
+
+  void printPCIMem(const InstTy *MI, uint64_t Address, unsigned OpNum,
+                   raw_ostream &O) {
+    O << '(';
+    impl().printDisp(MI, OpNum + M68k::PCRelDisp, O);
+    O << ",%pc,";
+    impl().printOperand(MI, OpNum + M68k::PCRelIndex, O);
+    O << ')';
+  }
+};
+} // end namespace llvm
+#endif


        


More information about the llvm-commits mailing list