AsmWriterInst.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
  17. #define LLVM_UTILS_TABLEGEN_ASMWRITERINST_H
  18. #include <string>
  19. #include <vector>
  20. namespace llvm {
  21. class CodeGenInstruction;
  22. class Record;
  23. struct AsmWriterOperand {
  24. enum OpType {
  25. // Output this text surrounded by quotes to the asm.
  26. isLiteralTextOperand,
  27. // This is the name of a routine to call to print the operand.
  28. isMachineInstrOperand,
  29. // Output this text verbatim to the asm writer. It is code that
  30. // will output some text to the asm.
  31. isLiteralStatementOperand
  32. } OperandType;
  33. /// Str - For isLiteralTextOperand, this IS the literal text. For
  34. /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
  35. /// For isLiteralStatementOperand, this is the code to insert verbatim
  36. /// into the asm writer.
  37. std::string Str;
  38. /// CGIOpNo - For isMachineInstrOperand, this is the index of the operand in
  39. /// the CodeGenInstruction.
  40. unsigned CGIOpNo;
  41. /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
  42. /// machine instruction.
  43. unsigned MIOpNo;
  44. /// MiModifier - For isMachineInstrOperand, this is the modifier string for
  45. /// an operand, specified with syntax like ${opname:modifier}.
  46. std::string MiModifier;
  47. // PassSubtarget - Pass MCSubtargetInfo to the print method if this is
  48. // equal to 1.
  49. // FIXME: Remove after all ports are updated.
  50. unsigned PassSubtarget;
  51. // To make VS STL happy
  52. AsmWriterOperand(OpType op = isLiteralTextOperand):OperandType(op) {}
  53. AsmWriterOperand(const std::string &LitStr,
  54. OpType op = isLiteralTextOperand)
  55. : OperandType(op), Str(LitStr) {}
  56. AsmWriterOperand(const std::string &Printer,
  57. unsigned _CGIOpNo,
  58. unsigned _MIOpNo,
  59. const std::string &Modifier,
  60. unsigned PassSubtarget,
  61. OpType op = isMachineInstrOperand)
  62. : OperandType(op), Str(Printer), CGIOpNo(_CGIOpNo), MIOpNo(_MIOpNo),
  63. MiModifier(Modifier), PassSubtarget(PassSubtarget) {}
  64. bool operator!=(const AsmWriterOperand &Other) const {
  65. if (OperandType != Other.OperandType || Str != Other.Str) return true;
  66. if (OperandType == isMachineInstrOperand)
  67. return MIOpNo != Other.MIOpNo || MiModifier != Other.MiModifier;
  68. return false;
  69. }
  70. bool operator==(const AsmWriterOperand &Other) const {
  71. return !operator!=(Other);
  72. }
  73. /// getCode - Return the code that prints this operand.
  74. std::string getCode() const;
  75. };
  76. class AsmWriterInst {
  77. public:
  78. std::vector<AsmWriterOperand> Operands;
  79. const CodeGenInstruction *CGI;
  80. AsmWriterInst(const CodeGenInstruction &CGI,
  81. unsigned Variant, unsigned PassSubtarget);
  82. /// MatchesAllButOneOp - If this instruction is exactly identical to the
  83. /// specified instruction except for one differing operand, return the
  84. /// differing operand number. Otherwise return ~0.
  85. unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
  86. private:
  87. void AddLiteralString(const std::string &Str) {
  88. // If the last operand was already a literal text string, append this to
  89. // it, otherwise add a new operand.
  90. if (!Operands.empty() &&
  91. Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
  92. Operands.back().Str.append(Str);
  93. else
  94. Operands.push_back(AsmWriterOperand(Str));
  95. }
  96. };
  97. }
  98. #endif