[MCInstPrinter] Enable MCInstPrinter to change its behavior based on the
[oota-llvm.git] / utils / TableGen / AsmWriterInst.h
1 //===- AsmWriterInst.h - Classes encapsulating a printable inst -*- C++ -*-===//
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 // These classes implement a parser for assembly strings.  The parser splits
11 // the string into operands, which can be literal strings (the constant bits of
12 // the string), actual operands (i.e., operands from the MachineInstr), and
13 // dynamically-generated text, specified by raw C++ code.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
18 #define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
19
20 #include <string>
21 #include <vector>
22
23 namespace llvm {
24   class CodeGenInstruction;
25   class Record;
26
27   struct AsmWriterOperand {
28     enum OpType {
29       // Output this text surrounded by quotes to the asm.
30       isLiteralTextOperand,
31       // This is the name of a routine to call to print the operand.
32       isMachineInstrOperand,
33       // Output this text verbatim to the asm writer.  It is code that
34       // will output some text to the asm.
35       isLiteralStatementOperand
36     } OperandType;
37
38     /// Str - For isLiteralTextOperand, this IS the literal text.  For
39     /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
40     /// For isLiteralStatementOperand, this is the code to insert verbatim
41     /// into the asm writer.
42     std::string Str;
43
44     /// CGIOpNo - For isMachineInstrOperand, this is the index of the operand in
45     /// the CodeGenInstruction.
46     unsigned CGIOpNo;
47
48     /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
49     /// machine instruction.
50     unsigned MIOpNo;
51
52     /// MiModifier - For isMachineInstrOperand, this is the modifier string for
53     /// an operand, specified with syntax like ${opname:modifier}.
54     std::string MiModifier;
55
56     // PassSubtarget - Pass MCSubtargetInfo to the print method if this is
57     // equal to 1.
58     // FIXME: Remove after all ports are updated.
59     unsigned PassSubtarget;
60
61     // To make VS STL happy
62     AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
63
64     AsmWriterOperand(const std::string &LitStr,
65                      OpType op = isLiteralTextOperand)
66     : OperandType(op), Str(LitStr) {}
67
68     AsmWriterOperand(const std::string &Printer,
69                      unsigned _CGIOpNo,
70                      unsigned _MIOpNo,
71                      const std::string &Modifier,
72                      unsigned PassSubtarget,
73                      OpType op = isMachineInstrOperand)
74     : OperandType(op), Str(Printer), CGIOpNo(_CGIOpNo), MIOpNo(_MIOpNo),
75     MiModifier(Modifier), PassSubtarget(PassSubtarget) {}
76
77     bool operator!=(const AsmWriterOperand &Other) const {
78       if (OperandType != Other.OperandType || Str != Other.Str) return true;
79       if (OperandType == isMachineInstrOperand)
80         return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
81       return false;
82     }
83     bool operator==(const AsmWriterOperand &Other) const {
84       return !operator!=(Other);
85     }
86
87     /// getCode - Return the code that prints this operand.
88     std::string getCode() const;
89   };
90
91   class AsmWriterInst {
92   public:
93     std::vector<AsmWriterOperand> Operands;
94     const CodeGenInstruction *CGI;
95
96     AsmWriterInst(const CodeGenInstruction &CGI,
97                   unsigned Variant, unsigned PassSubtarget);
98
99     /// MatchesAllButOneOp - If this instruction is exactly identical to the
100     /// specified instruction except for one differing operand, return the
101     /// differing operand number.  Otherwise return ~0.
102     unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
103
104   private:
105     void AddLiteralString(const std::string &Str) {
106       // If the last operand was already a literal text string, append this to
107       // it, otherwise add a new operand.
108       if (!Operands.empty() &&
109           Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
110         Operands.back().Str.append(Str);
111       else
112         Operands.push_back(AsmWriterOperand(Str));
113     }
114   };
115 }
116
117 #endif