AsmWriterInst.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===- AsmWriterInst.h - Classes encapsulating a printable inst -----------===//
  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.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "AsmWriterInst.h"
  13. #include "CodeGenTarget.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/TableGen/Error.h"
  16. #include "llvm/TableGen/Record.h"
  17. using namespace llvm;
  18. static bool isIdentChar(char C) { return isAlnum(C) || C == '_'; }
  19. std::string AsmWriterOperand::getCode(bool PassSubtarget) const {
  20. if (OperandType == isLiteralTextOperand) {
  21. if (Str.size() == 1)
  22. return "O << '" + Str + "';";
  23. return "O << \"" + Str + "\";";
  24. }
  25. if (OperandType == isLiteralStatementOperand)
  26. return Str;
  27. std::string Result = Str + "(MI";
  28. if (PCRel)
  29. Result += ", Address";
  30. if (MIOpNo != ~0U)
  31. Result += ", " + utostr(MIOpNo);
  32. if (PassSubtarget)
  33. Result += ", STI";
  34. Result += ", O";
  35. if (!MiModifier.empty())
  36. Result += ", \"" + MiModifier + '"';
  37. return Result + ");";
  38. }
  39. /// ParseAsmString - Parse the specified Instruction's AsmString into this
  40. /// AsmWriterInst.
  41. ///
  42. AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned CGIIndex,
  43. unsigned Variant)
  44. : CGI(&CGI), CGIIndex(CGIIndex) {
  45. // NOTE: Any extensions to this code need to be mirrored in the
  46. // AsmPrinter::printInlineAsm code that executes as compile time (assuming
  47. // that inline asm strings should also get the new feature)!
  48. std::string AsmString = CGI.FlattenAsmStringVariants(CGI.AsmString, Variant);
  49. std::string::size_type LastEmitted = 0;
  50. while (LastEmitted != AsmString.size()) {
  51. std::string::size_type DollarPos =
  52. AsmString.find_first_of("$\\", LastEmitted);
  53. if (DollarPos == std::string::npos) DollarPos = AsmString.size();
  54. // Emit a constant string fragment.
  55. if (DollarPos != LastEmitted) {
  56. for (; LastEmitted != DollarPos; ++LastEmitted)
  57. switch (AsmString[LastEmitted]) {
  58. case '\n':
  59. AddLiteralString("\\n");
  60. break;
  61. case '\t':
  62. AddLiteralString("\\t");
  63. break;
  64. case '"':
  65. AddLiteralString("\\\"");
  66. break;
  67. case '\\':
  68. AddLiteralString("\\\\");
  69. break;
  70. default:
  71. AddLiteralString(std::string(1, AsmString[LastEmitted]));
  72. break;
  73. }
  74. } else if (AsmString[DollarPos] == '\\') {
  75. if (DollarPos+1 != AsmString.size()) {
  76. if (AsmString[DollarPos+1] == 'n') {
  77. AddLiteralString("\\n");
  78. } else if (AsmString[DollarPos+1] == 't') {
  79. AddLiteralString("\\t");
  80. } else if (std::string("${|}\\").find(AsmString[DollarPos+1])
  81. != std::string::npos) {
  82. AddLiteralString(std::string(1, AsmString[DollarPos+1]));
  83. } else {
  84. PrintFatalError(
  85. CGI.TheDef->getLoc(),
  86. "Non-supported escaped character found in instruction '" +
  87. CGI.TheDef->getName() + "'!");
  88. }
  89. LastEmitted = DollarPos+2;
  90. continue;
  91. }
  92. } else if (DollarPos+1 != AsmString.size() &&
  93. AsmString[DollarPos+1] == '$') {
  94. AddLiteralString("$"); // "$$" -> $
  95. LastEmitted = DollarPos+2;
  96. } else {
  97. // Get the name of the variable.
  98. std::string::size_type VarEnd = DollarPos+1;
  99. // handle ${foo}bar as $foo by detecting whether the character following
  100. // the dollar sign is a curly brace. If so, advance VarEnd and DollarPos
  101. // so the variable name does not contain the leading curly brace.
  102. bool hasCurlyBraces = false;
  103. if (VarEnd < AsmString.size() && '{' == AsmString[VarEnd]) {
  104. hasCurlyBraces = true;
  105. ++DollarPos;
  106. ++VarEnd;
  107. }
  108. while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
  109. ++VarEnd;
  110. StringRef VarName(AsmString.data()+DollarPos+1, VarEnd-DollarPos-1);
  111. // Modifier - Support ${foo:modifier} syntax, where "modifier" is passed
  112. // into printOperand. Also support ${:feature}, which is passed into
  113. // PrintSpecial.
  114. std::string Modifier;
  115. // In order to avoid starting the next string at the terminating curly
  116. // brace, advance the end position past it if we found an opening curly
  117. // brace.
  118. if (hasCurlyBraces) {
  119. if (VarEnd >= AsmString.size())
  120. PrintFatalError(
  121. CGI.TheDef->getLoc(),
  122. "Reached end of string before terminating curly brace in '" +
  123. CGI.TheDef->getName() + "'");
  124. // Look for a modifier string.
  125. if (AsmString[VarEnd] == ':') {
  126. ++VarEnd;
  127. if (VarEnd >= AsmString.size())
  128. PrintFatalError(
  129. CGI.TheDef->getLoc(),
  130. "Reached end of string before terminating curly brace in '" +
  131. CGI.TheDef->getName() + "'");
  132. std::string::size_type ModifierStart = VarEnd;
  133. while (VarEnd < AsmString.size() && isIdentChar(AsmString[VarEnd]))
  134. ++VarEnd;
  135. Modifier = AsmString.substr(ModifierStart, VarEnd - ModifierStart);
  136. if (Modifier.empty())
  137. PrintFatalError(CGI.TheDef->getLoc(),
  138. "Bad operand modifier name in '" +
  139. CGI.TheDef->getName() + "'");
  140. }
  141. if (AsmString[VarEnd] != '}')
  142. PrintFatalError(
  143. CGI.TheDef->getLoc(),
  144. "Variable name beginning with '{' did not end with '}' in '" +
  145. CGI.TheDef->getName() + "'");
  146. ++VarEnd;
  147. }
  148. if (VarName.empty() && Modifier.empty())
  149. PrintFatalError(CGI.TheDef->getLoc(),
  150. "Stray '$' in '" + CGI.TheDef->getName() +
  151. "' asm string, maybe you want $$?");
  152. if (VarName.empty()) {
  153. // Just a modifier, pass this into PrintSpecial.
  154. Operands.emplace_back("PrintSpecial", ~0U, Modifier);
  155. } else {
  156. // Otherwise, normal operand.
  157. unsigned OpNo = CGI.Operands.getOperandNamed(VarName);
  158. CGIOperandList::OperandInfo OpInfo = CGI.Operands[OpNo];
  159. unsigned MIOp = OpInfo.MIOperandNo;
  160. Operands.emplace_back(OpInfo.PrinterMethodName, MIOp, Modifier,
  161. AsmWriterOperand::isMachineInstrOperand,
  162. OpInfo.OperandType == "MCOI::OPERAND_PCREL");
  163. }
  164. LastEmitted = VarEnd;
  165. }
  166. }
  167. Operands.emplace_back("return;", AsmWriterOperand::isLiteralStatementOperand);
  168. }
  169. /// MatchesAllButOneOp - If this instruction is exactly identical to the
  170. /// specified instruction except for one differing operand, return the differing
  171. /// operand number. If more than one operand mismatches, return ~1, otherwise
  172. /// if the instructions are identical return ~0.
  173. unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
  174. if (Operands.size() != Other.Operands.size()) return ~1;
  175. unsigned MismatchOperand = ~0U;
  176. for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
  177. if (Operands[i] != Other.Operands[i]) {
  178. if (MismatchOperand != ~0U) // Already have one mismatch?
  179. return ~1U;
  180. MismatchOperand = i;
  181. }
  182. }
  183. return MismatchOperand;
  184. }