Add support for stored annotations to MCInst, and provide facilities for MC-based...
authorOwen Anderson <resistor@mac.com>
Thu, 15 Sep 2011 18:36:29 +0000 (18:36 +0000)
committerOwen Anderson <resistor@mac.com>
Thu, 15 Sep 2011 18:36:29 +0000 (18:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139820 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCInst.h
include/llvm/MC/MCInstPrinter.h
lib/MC/MCInst.cpp
lib/MC/MCInstPrinter.cpp
lib/Target/ARM/InstPrinter/ARMInstPrinter.cpp
lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp
lib/Target/X86/InstPrinter/X86IntelInstPrinter.cpp

index d384764774958e01d363873c410f626e55a7a5a6..d443536d40f979bfab2fdacd1bc2738c47d5e2b8 100644 (file)
@@ -129,6 +129,7 @@ public:
 class MCInst {
   unsigned Opcode;
   SmallVector<MCOperand, 8> Operands;
+  SmallVector<std::string, 1> Annotations;
 public:
   MCInst() : Opcode(0) {}
 
@@ -144,7 +145,15 @@ public:
     Operands.push_back(Op);
   }
 
-  void clear() { Operands.clear(); }
+  void addAnnotation(const std::string &Annot) {
+    Annotations.push_back(Annot);
+  }
+
+  void clear() {
+    Operands.clear();
+    Annotations.clear();
+  }
+
   size_t size() { return Operands.size(); }
 
   typedef SmallVector<MCOperand, 8>::iterator iterator;
@@ -154,6 +163,9 @@ public:
     return Operands.insert(I, Op);
   }
 
+  size_t getNumAnnotations() const { return Annotations.size(); }
+  std::string getAnnotation(size_t i) const { return Annotations[i]; }
+
   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
   void dump() const;
 
index 39002dabca14cf43d589452c461b3478f2d1ccb0..4c12d10d5be02448e5b97da1442745b81b5185c4 100644 (file)
@@ -41,6 +41,10 @@ public:
   ///
   virtual void printInst(const MCInst *MI, raw_ostream &OS) = 0;
 
+  /// printAnnotations - Print the annotation comments attached to specified
+  /// MCInst to the specified raw_ostream.
+  void printAnnotations(const MCInst *MI, raw_ostream &OS);
+
   /// getOpcodeName - Return the name of the specified opcode enum (e.g.
   /// "MOV32ri") or empty if we can't resolve it.
   virtual StringRef getOpcodeName(unsigned Opcode) const;
index 4cb628b395c3fba2ca0a61d26fa32499f235d217..ec97acc554f5786c85263523a67f4ecd58a02bf8 100644 (file)
@@ -41,6 +41,16 @@ void MCInst::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
     OS << " ";
     getOperand(i).print(OS, MAI);
   }
+
+  if (getNumAnnotations()) {
+    OS << " # Annots: ";
+    for (unsigned i = 0, e = getNumAnnotations(); i != e; ++i) {
+      OS << " \"";
+      OS << getAnnotation(i);
+      OS << '"';
+    }
+  }
+
   OS << ">";
 }
 
@@ -57,6 +67,17 @@ void MCInst::dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI,
     OS << Separator;
     getOperand(i).print(OS, MAI);
   }
+
+  if (getNumAnnotations()) {
+    OS << " # Annots: ";
+    for (unsigned i = 0, e = getNumAnnotations(); i != e; ++i) {
+      OS << Separator;
+      OS << '"';
+      OS << getAnnotation(i);
+      OS << '"';
+    }
+  }
+
   OS << ">";
 }
 
index 81a939ffaaef3156fc30132b1b241b6d19bc3163..f0fa2cda4a8319ac0a2fb851a3ba0c8b58c34062 100644 (file)
@@ -8,7 +8,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/MCInstPrinter.h"
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCInst.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 MCInstPrinter::~MCInstPrinter() {
@@ -23,3 +26,9 @@ StringRef MCInstPrinter::getOpcodeName(unsigned Opcode) const {
 void MCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
   assert(0 && "Target should implement this");
 }
+
+void MCInstPrinter::printAnnotations(const MCInst *MI, raw_ostream &OS) {
+  for (unsigned i = 0, e = MI->getNumAnnotations(); i != e; ++i) {
+    OS << MI->getAnnotation(i) << "\n";
+  }
+}
index 7eec0dddf8a2112ad2477e540807b2d38b9d9919..289d1921d1c3e6b1d1d5788708d37d5d609aa645 100644 (file)
@@ -71,6 +71,9 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
 
     O << ", " << getRegisterName(MO2.getReg());
     assert(ARM_AM::getSORegOffset(MO3.getImm()) == 0);
+
+    if (CommentStream) printAnnotations(MI, *CommentStream);
+
     return;
   }
 
@@ -87,10 +90,14 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     O << '\t' << getRegisterName(Dst.getReg())
       << ", " << getRegisterName(MO1.getReg());
 
-    if (ARM_AM::getSORegShOp(MO2.getImm()) == ARM_AM::rrx)
+    if (ARM_AM::getSORegShOp(MO2.getImm()) == ARM_AM::rrx) {
+      if (CommentStream) printAnnotations(MI, *CommentStream);
       return;
+    }
 
     O << ", #" << translateShiftImm(ARM_AM::getSORegOffset(MO2.getImm()));
+
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -104,6 +111,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
       O << ".w";
     O << '\t';
     printRegisterList(MI, 4, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
   if (Opcode == ARM::STR_PRE_IMM && MI->getOperand(2).getReg() == ARM::SP &&
@@ -111,6 +119,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     O << '\t' << "push";
     printPredicateOperand(MI, 4, O);
     O << "\t{" << getRegisterName(MI->getOperand(1).getReg()) << "}";
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -123,6 +132,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
       O << ".w";
     O << '\t';
     printRegisterList(MI, 4, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
   if (Opcode == ARM::LDR_POST_IMM && MI->getOperand(2).getReg() == ARM::SP &&
@@ -130,6 +140,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     O << '\t' << "pop";
     printPredicateOperand(MI, 5, O);
     O << "\t{" << getRegisterName(MI->getOperand(0).getReg()) << "}";
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -141,6 +152,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     printPredicateOperand(MI, 2, O);
     O << '\t';
     printRegisterList(MI, 4, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -151,6 +163,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     printPredicateOperand(MI, 2, O);
     O << '\t';
     printRegisterList(MI, 4, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -169,6 +182,7 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     if (Writeback) O << "!";
     O << ", ";
     printRegisterList(MI, 3, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
@@ -177,10 +191,12 @@ void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
       MI->getOperand(1).getReg() == ARM::R8) {
     O << "\tnop";
     printPredicateOperand(MI, 2, O);
+    if (CommentStream) printAnnotations(MI, *CommentStream);
     return;
   }
 
   printInstruction(MI, O);
+  if (CommentStream) printAnnotations(MI, *CommentStream);
 }
 
 void ARMInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
index 591b5838310d193c090133b91c9a27b57cd2699a..76a1da49595f6638fbc8f7d8f3fb5067b3ee3fad 100644 (file)
@@ -45,8 +45,10 @@ void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
     printInstruction(MI, OS);
   
   // If verbose assembly is enabled, we can print some informative comments.
-  if (CommentStream)
+  if (CommentStream) {
+    printAnnotations(MI, *CommentStream);
     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
+  }
 }
 
 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
index 506e26cbf7cdf6bb49a936b1c5e21f2334715b25..6cca1d19b3878be727b5df30a778456a0ec79066 100644 (file)
@@ -36,8 +36,10 @@ void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
   printInstruction(MI, OS);
   
   // If verbose assembly is enabled, we can print some informative comments.
-  if (CommentStream)
+  if (CommentStream) {
+    printAnnotations(MI, *CommentStream);
     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
+  }
 }
 StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
   return getInstructionName(Opcode);