AsmWriterInst.cpp 7.5 KB

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