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