xref: /aosp_15_r20/external/llvm/utils/TableGen/AsmWriterInst.h (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- AsmWriterInst.h - Classes encapsulating a printable inst -*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // These classes implement a parser for assembly strings.  The parser splits
11*9880d681SAndroid Build Coastguard Worker // the string into operands, which can be literal strings (the constant bits of
12*9880d681SAndroid Build Coastguard Worker // the string), actual operands (i.e., operands from the MachineInstr), and
13*9880d681SAndroid Build Coastguard Worker // dynamically-generated text, specified by raw C++ code.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
18*9880d681SAndroid Build Coastguard Worker #define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
19*9880d681SAndroid Build Coastguard Worker 
20*9880d681SAndroid Build Coastguard Worker #include <string>
21*9880d681SAndroid Build Coastguard Worker #include <vector>
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker namespace llvm {
24*9880d681SAndroid Build Coastguard Worker   class CodeGenInstruction;
25*9880d681SAndroid Build Coastguard Worker   class Record;
26*9880d681SAndroid Build Coastguard Worker 
27*9880d681SAndroid Build Coastguard Worker   struct AsmWriterOperand {
28*9880d681SAndroid Build Coastguard Worker     enum OpType {
29*9880d681SAndroid Build Coastguard Worker       // Output this text surrounded by quotes to the asm.
30*9880d681SAndroid Build Coastguard Worker       isLiteralTextOperand,
31*9880d681SAndroid Build Coastguard Worker       // This is the name of a routine to call to print the operand.
32*9880d681SAndroid Build Coastguard Worker       isMachineInstrOperand,
33*9880d681SAndroid Build Coastguard Worker       // Output this text verbatim to the asm writer.  It is code that
34*9880d681SAndroid Build Coastguard Worker       // will output some text to the asm.
35*9880d681SAndroid Build Coastguard Worker       isLiteralStatementOperand
36*9880d681SAndroid Build Coastguard Worker     } OperandType;
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker     /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
39*9880d681SAndroid Build Coastguard Worker     /// machine instruction.
40*9880d681SAndroid Build Coastguard Worker     unsigned MIOpNo;
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker     /// Str - For isLiteralTextOperand, this IS the literal text.  For
43*9880d681SAndroid Build Coastguard Worker     /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
44*9880d681SAndroid Build Coastguard Worker     /// For isLiteralStatementOperand, this is the code to insert verbatim
45*9880d681SAndroid Build Coastguard Worker     /// into the asm writer.
46*9880d681SAndroid Build Coastguard Worker     std::string Str;
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker     /// MiModifier - For isMachineInstrOperand, this is the modifier string for
49*9880d681SAndroid Build Coastguard Worker     /// an operand, specified with syntax like ${opname:modifier}.
50*9880d681SAndroid Build Coastguard Worker     std::string MiModifier;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker     // To make VS STL happy
OperandTypeAsmWriterOperand53*9880d681SAndroid Build Coastguard Worker     AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker     AsmWriterOperand(const std::string &LitStr,
56*9880d681SAndroid Build Coastguard Worker                      OpType op = isLiteralTextOperand)
OperandTypeAsmWriterOperand57*9880d681SAndroid Build Coastguard Worker     : OperandType(op), Str(LitStr) {}
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker     AsmWriterOperand(const std::string &Printer,
60*9880d681SAndroid Build Coastguard Worker                      unsigned _MIOpNo,
61*9880d681SAndroid Build Coastguard Worker                      const std::string &Modifier,
62*9880d681SAndroid Build Coastguard Worker                      OpType op = isMachineInstrOperand)
OperandTypeAsmWriterOperand63*9880d681SAndroid Build Coastguard Worker     : OperandType(op), MIOpNo(_MIOpNo), Str(Printer), MiModifier(Modifier) {}
64*9880d681SAndroid Build Coastguard Worker 
65*9880d681SAndroid Build Coastguard Worker     bool operator!=(const AsmWriterOperand &Other) const {
66*9880d681SAndroid Build Coastguard Worker       if (OperandType != Other.OperandType || Str != Other.Str) return true;
67*9880d681SAndroid Build Coastguard Worker       if (OperandType == isMachineInstrOperand)
68*9880d681SAndroid Build Coastguard Worker         return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
69*9880d681SAndroid Build Coastguard Worker       return false;
70*9880d681SAndroid Build Coastguard Worker     }
71*9880d681SAndroid Build Coastguard Worker     bool operator==(const AsmWriterOperand &Other) const {
72*9880d681SAndroid Build Coastguard Worker       return !operator!=(Other);
73*9880d681SAndroid Build Coastguard Worker     }
74*9880d681SAndroid Build Coastguard Worker 
75*9880d681SAndroid Build Coastguard Worker     /// getCode - Return the code that prints this operand.
76*9880d681SAndroid Build Coastguard Worker     std::string getCode(bool PassSubtarget) const;
77*9880d681SAndroid Build Coastguard Worker   };
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker   class AsmWriterInst {
80*9880d681SAndroid Build Coastguard Worker   public:
81*9880d681SAndroid Build Coastguard Worker     std::vector<AsmWriterOperand> Operands;
82*9880d681SAndroid Build Coastguard Worker     const CodeGenInstruction *CGI;
83*9880d681SAndroid Build Coastguard Worker     unsigned CGIIndex;
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker     AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
86*9880d681SAndroid Build Coastguard Worker                   unsigned Variant);
87*9880d681SAndroid Build Coastguard Worker 
88*9880d681SAndroid Build Coastguard Worker     /// MatchesAllButOneOp - If this instruction is exactly identical to the
89*9880d681SAndroid Build Coastguard Worker     /// specified instruction except for one differing operand, return the
90*9880d681SAndroid Build Coastguard Worker     /// differing operand number.  Otherwise return ~0.
91*9880d681SAndroid Build Coastguard Worker     unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker   private:
AddLiteralString(const std::string & Str)94*9880d681SAndroid Build Coastguard Worker     void AddLiteralString(const std::string &Str) {
95*9880d681SAndroid Build Coastguard Worker       // If the last operand was already a literal text string, append this to
96*9880d681SAndroid Build Coastguard Worker       // it, otherwise add a new operand.
97*9880d681SAndroid Build Coastguard Worker       if (!Operands.empty() &&
98*9880d681SAndroid Build Coastguard Worker           Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
99*9880d681SAndroid Build Coastguard Worker         Operands.back().Str.append(Str);
100*9880d681SAndroid Build Coastguard Worker       else
101*9880d681SAndroid Build Coastguard Worker         Operands.push_back(AsmWriterOperand(Str));
102*9880d681SAndroid Build Coastguard Worker     }
103*9880d681SAndroid Build Coastguard Worker   };
104*9880d681SAndroid Build Coastguard Worker }
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker #endif
107