AsmWriterInst.h 3.9 KB

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