[PATCH] Add method to MCInstPrinter for printing with disassembly context

Colin LeMahieu colinl at codeaurora.org
Wed May 27 09:49:40 PDT 2015


I've been working more on this and have reconsidered the design.  The issue I ran in to is there are other users of InstPrinter that need to render the instruction in different ways.  The other major example besides objdump is lldb which prints out additional information like the function name and offset.

If things were done as in the last patch, each user of InstPrinter could require a new virtual method containing the information it wants to display, effectively InstPrinter would need to know about all its users.

This patch moves the rendering to the user which leaves InstPrinter unchanged.  LLDB has existing infrastructure to do similar target-specific rendering.


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8427

Files:
  tools/llvm-objdump/llvm-objdump.cpp

Index: tools/llvm-objdump/llvm-objdump.cpp
===================================================================
--- tools/llvm-objdump/llvm-objdump.cpp
+++ tools/llvm-objdump/llvm-objdump.cpp
@@ -214,6 +214,43 @@
   return a_addr < b_addr;
 }
 
+namespace {
+class PrettyPrinter {
+public:
+  virtual void printInst(MCInstPrinter &IP, const MCInst *MI, bool ShowRawInsn,
+                         ArrayRef<uint8_t> Bytes, uint64_t Address,
+                         raw_ostream &OS, StringRef Annot,
+                         MCSubtargetInfo const &STI) {
+    OS << format("%8" PRIx64 ":", Address);
+    if (ShowRawInsn) {
+      OS << "\t";
+      DumpBytes(Bytes);
+    }
+    IP.printInst(MI, OS, Annot, STI);
+    OS << "\n";
+  }
+};
+PrettyPrinter PrettyPrinterInst;
+class HexagonPrettyPrinter : public PrettyPrinter {
+public:
+  void printInst(MCInstPrinter &IP, const MCInst *MI, bool ShowRawInsn,
+                 ArrayRef<uint8_t> Bytes, uint64_t Address,
+                 raw_ostream &OS, StringRef Annot,
+                 MCSubtargetInfo const &STI) override {
+    // Special printing
+  }
+};
+HexagonPrettyPrinter HexagonPrettyPrinterInst;
+PrettyPrinter &selectPrettyPrinter(Triple const &Triple, MCInstPrinter &IP) {
+  switch(Triple.getArch()) {
+  default:
+    return PrettyPrinterInst;
+  case Triple::hexagon:
+    return HexagonPrettyPrinterInst;
+  }
+}
+}
+
 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
   const Target *TheTarget = getTarget(Obj);
   // getTarget() will have already issued a diagnostic if necessary, so
@@ -280,6 +317,7 @@
       << '\n';
     return;
   }
+  PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName), *IP);
 
   StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ":  " :
                                                  "\t\t\t%08" PRIx64 ":  ";
@@ -396,12 +434,9 @@
         if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
                                    SectionAddr + Index, DebugOut,
                                    CommentStream)) {
-          outs() << format("%8" PRIx64 ":", SectionAddr + Index);
-          if (!NoShowRawInsn) {
-            outs() << "\t";
-            DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
-          }
-          IP->printInst(&Inst, outs(), "", *STI);
+          PIP.printInst(*IP, &Inst, !NoShowRawInsn,
+                        Bytes.slice(Index, Size),
+                        SectionAddr + Index, outs(), "", *STI);
           outs() << CommentStream.str();
           Comments.clear();
           outs() << "\n";

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8427.26609.patch
Type: text/x-patch
Size: 2601 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150527/226b599d/attachment.bin>


More information about the llvm-commits mailing list