AsmWriterInst.cpp 7.1 KB

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