Add support for stored annotations to MCInst, and provide facilities for MC-based...
[oota-llvm.git] / lib / MC / MCInst.cpp
1 //===- lib/MC/MCInst.cpp - MCInst implementation --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCInst.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/MC/MCInstPrinter.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/raw_ostream.h"
15
16 using namespace llvm;
17
18 void MCOperand::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
19   OS << "<MCOperand ";
20   if (!isValid())
21     OS << "INVALID";
22   else if (isReg())
23     OS << "Reg:" << getReg();
24   else if (isImm())
25     OS << "Imm:" << getImm();
26   else if (isExpr()) {
27     OS << "Expr:(" << *getExpr() << ")";
28   } else
29     OS << "UNDEFINED";
30   OS << ">";
31 }
32
33 void MCOperand::dump() const {
34   print(dbgs(), 0);
35   dbgs() << "\n";
36 }
37
38 void MCInst::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
39   OS << "<MCInst " << getOpcode();
40   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
41     OS << " ";
42     getOperand(i).print(OS, MAI);
43   }
44
45   if (getNumAnnotations()) {
46     OS << " # Annots: ";
47     for (unsigned i = 0, e = getNumAnnotations(); i != e; ++i) {
48       OS << " \"";
49       OS << getAnnotation(i);
50       OS << '"';
51     }
52   }
53
54   OS << ">";
55 }
56
57 void MCInst::dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI,
58                          const MCInstPrinter *Printer,
59                          StringRef Separator) const {
60   OS << "<MCInst #" << getOpcode();
61
62   // Show the instruction opcode name if we have access to a printer.
63   if (Printer)
64     OS << ' ' << Printer->getOpcodeName(getOpcode());
65
66   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
67     OS << Separator;
68     getOperand(i).print(OS, MAI);
69   }
70
71   if (getNumAnnotations()) {
72     OS << " # Annots: ";
73     for (unsigned i = 0, e = getNumAnnotations(); i != e; ++i) {
74       OS << Separator;
75       OS << '"';
76       OS << getAnnotation(i);
77       OS << '"';
78     }
79   }
80
81   OS << ">";
82 }
83
84 void MCInst::dump() const {
85   print(dbgs(), 0);
86   dbgs() << "\n";
87 }