AsmWriterInst.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. return false;
  59. }
  60. bool operator==(const AsmWriterOperand &Other) const {
  61. return !operator!=(Other);
  62. }
  63. /// getCode - Return the code that prints this operand.
  64. std::string getCode(bool PassSubtarget) const;
  65. };
  66. class AsmWriterInst {
  67. public:
  68. std::vector<AsmWriterOperand> Operands;
  69. const CodeGenInstruction *CGI;
  70. unsigned CGIIndex;
  71. AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
  72. unsigned Variant);
  73. /// MatchesAllButOneOp - If this instruction is exactly identical to the
  74. /// specified instruction except for one differing operand, return the
  75. /// differing operand number. Otherwise return ~0.
  76. unsigned MatchesAllButOneOp(const AsmWriterInst &Other) const;
  77. private:
  78. void AddLiteralString(const std::string &Str) {
  79. // If the last operand was already a literal text string, append this to
  80. // it, otherwise add a new operand.
  81. if (!Operands.empty() &&
  82. Operands.back().OperandType == AsmWriterOperand::isLiteralTextOperand)
  83. Operands.back().Str.append(Str);
  84. else
  85. Operands.push_back(AsmWriterOperand(Str));
  86. }
  87. };
  88. }
  89. #endif