Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 17 | #ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H |
| 18 | #define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace llvm { |
| 24 | class CodeGenInstruction; |
| 25 | class Record; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 26 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 27 | struct AsmWriterOperand { |
| 28 | enum OpType { |
| 29 | // Output this text surrounded by quotes to the asm. |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 30 | isLiteralTextOperand, |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 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; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 37 | |
Craig Topper | 43b7990 | 2016-01-22 05:59:40 +0000 | [diff] [blame] | 38 | /// MiOpNo - For isMachineInstrOperand, this is the operand number of the |
| 39 | /// machine instruction. |
| 40 | unsigned MIOpNo; |
| 41 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 42 | /// Str - For isLiteralTextOperand, this IS the literal text. For |
| 43 | /// isMachineInstrOperand, this is the PrinterMethodName for the operand.. |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 44 | /// For isLiteralStatementOperand, this is the code to insert verbatim |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 45 | /// into the asm writer. |
| 46 | std::string Str; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 47 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 48 | /// MiModifier - For isMachineInstrOperand, this is the modifier string for |
| 49 | /// an operand, specified with syntax like ${opname:modifier}. |
| 50 | std::string MiModifier; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 51 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 52 | // To make VS STL happy |
| 53 | AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {} |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 54 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 55 | AsmWriterOperand(const std::string &LitStr, |
| 56 | OpType op = isLiteralTextOperand) |
| 57 | : OperandType(op), Str(LitStr) {} |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 58 | |
Sean Callanan | 397170b | 2010-02-10 02:27:43 +0000 | [diff] [blame] | 59 | AsmWriterOperand(const std::string &Printer, |
Sean Callanan | 397170b | 2010-02-10 02:27:43 +0000 | [diff] [blame] | 60 | unsigned _MIOpNo, |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 61 | const std::string &Modifier, |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 62 | OpType op = isMachineInstrOperand) |
Craig Topper | 43b7990 | 2016-01-22 05:59:40 +0000 | [diff] [blame] | 63 | : OperandType(op), MIOpNo(_MIOpNo), Str(Printer), MiModifier(Modifier) {} |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 64 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 65 | bool operator!=(const AsmWriterOperand &Other) const { |
| 66 | if (OperandType != Other.OperandType || Str != Other.Str) return true; |
| 67 | if (OperandType == isMachineInstrOperand) |
| 68 | return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier; |
| 69 | return false; |
| 70 | } |
| 71 | bool operator==(const AsmWriterOperand &Other) const { |
| 72 | return !operator!=(Other); |
| 73 | } |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 74 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 75 | /// getCode - Return the code that prints this operand. |
Craig Topper | efefcdd | 2016-01-14 06:15:07 +0000 | [diff] [blame] | 76 | std::string getCode(bool PassSubtarget) const; |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 77 | }; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 78 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 79 | class AsmWriterInst { |
| 80 | public: |
| 81 | std::vector<AsmWriterOperand> Operands; |
| 82 | const CodeGenInstruction *CGI; |
Craig Topper | 73251b7 | 2016-01-17 08:05:33 +0000 | [diff] [blame] | 83 | unsigned CGIIndex; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 84 | |
Craig Topper | 73251b7 | 2016-01-17 08:05:33 +0000 | [diff] [blame] | 85 | AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex, |
| 86 | unsigned Variant); |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 87 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 88 | /// MatchesAllButOneOp - If this instruction is exactly identical to the |
| 89 | /// specified instruction except for one differing operand, return the |
| 90 | /// differing operand number. Otherwise return ~0. |
| 91 | unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const; |
Jim Grosbach | c4bd6fb | 2010-10-11 19:38:01 +0000 | [diff] [blame] | 92 | |
Sean Callanan | d32c02f | 2010-02-09 21:50:41 +0000 | [diff] [blame] | 93 | private: |
| 94 | void AddLiteralString(const std::string &Str) { |
| 95 | // If the last operand was already a literal text string, append this to |
| 96 | // it, otherwise add a new operand. |
| 97 | if (!Operands.empty() && |
| 98 | Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand) |
| 99 | Operands.back().Str.append(Str); |
| 100 | else |
| 101 | Operands.push_back(AsmWriterOperand(Str)); |
| 102 | } |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | #endif |