InlineAsm.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
  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. // This file implements the InlineAsm class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/IR/InlineAsm.h"
  13. #include "ConstantsContext.h"
  14. #include "LLVMContextImpl.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/IR/Value.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include <algorithm>
  22. #include <cassert>
  23. #include <cctype>
  24. #include <cstdlib>
  25. using namespace llvm;
  26. InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
  27. const std::string &constraints, bool hasSideEffects,
  28. bool isAlignStack, AsmDialect asmDialect, bool canThrow)
  29. : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
  30. AsmString(asmString), Constraints(constraints), FTy(FTy),
  31. HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
  32. Dialect(asmDialect), CanThrow(canThrow) {
  33. // Do various checks on the constraint string and type.
  34. assert(Verify(getFunctionType(), constraints) &&
  35. "Function type not legal for constraints!");
  36. }
  37. InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
  38. StringRef Constraints, bool hasSideEffects,
  39. bool isAlignStack, AsmDialect asmDialect,
  40. bool canThrow) {
  41. InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
  42. isAlignStack, asmDialect, canThrow);
  43. LLVMContextImpl *pImpl = FTy->getContext().pImpl;
  44. return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
  45. }
  46. void InlineAsm::destroyConstant() {
  47. getType()->getContext().pImpl->InlineAsms.remove(this);
  48. delete this;
  49. }
  50. FunctionType *InlineAsm::getFunctionType() const {
  51. return FTy;
  52. }
  53. /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
  54. /// fields in this structure. If the constraint string is not understood,
  55. /// return true, otherwise return false.
  56. bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
  57. InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
  58. StringRef::iterator I = Str.begin(), E = Str.end();
  59. unsigned multipleAlternativeCount = Str.count('|') + 1;
  60. unsigned multipleAlternativeIndex = 0;
  61. ConstraintCodeVector *pCodes = &Codes;
  62. // Initialize
  63. isMultipleAlternative = multipleAlternativeCount > 1;
  64. if (isMultipleAlternative) {
  65. multipleAlternatives.resize(multipleAlternativeCount);
  66. pCodes = &multipleAlternatives[0].Codes;
  67. }
  68. Type = isInput;
  69. isEarlyClobber = false;
  70. MatchingInput = -1;
  71. isCommutative = false;
  72. isIndirect = false;
  73. currentAlternativeIndex = 0;
  74. // Parse prefixes.
  75. if (*I == '~') {
  76. Type = isClobber;
  77. ++I;
  78. // '{' must immediately follow '~'.
  79. if (I != E && *I != '{')
  80. return true;
  81. } else if (*I == '=') {
  82. ++I;
  83. Type = isOutput;
  84. }
  85. if (*I == '*') {
  86. isIndirect = true;
  87. ++I;
  88. }
  89. if (I == E) return true; // Just a prefix, like "==" or "~".
  90. // Parse the modifiers.
  91. bool DoneWithModifiers = false;
  92. while (!DoneWithModifiers) {
  93. switch (*I) {
  94. default:
  95. DoneWithModifiers = true;
  96. break;
  97. case '&': // Early clobber.
  98. if (Type != isOutput || // Cannot early clobber anything but output.
  99. isEarlyClobber) // Reject &&&&&&
  100. return true;
  101. isEarlyClobber = true;
  102. break;
  103. case '%': // Commutative.
  104. if (Type == isClobber || // Cannot commute clobbers.
  105. isCommutative) // Reject %%%%%
  106. return true;
  107. isCommutative = true;
  108. break;
  109. case '#': // Comment.
  110. case '*': // Register preferencing.
  111. return true; // Not supported.
  112. }
  113. if (!DoneWithModifiers) {
  114. ++I;
  115. if (I == E) return true; // Just prefixes and modifiers!
  116. }
  117. }
  118. // Parse the various constraints.
  119. while (I != E) {
  120. if (*I == '{') { // Physical register reference.
  121. // Find the end of the register name.
  122. StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
  123. if (ConstraintEnd == E) return true; // "{foo"
  124. pCodes->push_back(std::string(StringRef(I, ConstraintEnd + 1 - I)));
  125. I = ConstraintEnd+1;
  126. } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
  127. // Maximal munch numbers.
  128. StringRef::iterator NumStart = I;
  129. while (I != E && isdigit(static_cast<unsigned char>(*I)))
  130. ++I;
  131. pCodes->push_back(std::string(StringRef(NumStart, I - NumStart)));
  132. unsigned N = atoi(pCodes->back().c_str());
  133. // Check that this is a valid matching constraint!
  134. if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
  135. Type != isInput)
  136. return true; // Invalid constraint number.
  137. // If Operand N already has a matching input, reject this. An output
  138. // can't be constrained to the same value as multiple inputs.
  139. if (isMultipleAlternative) {
  140. if (multipleAlternativeIndex >=
  141. ConstraintsSoFar[N].multipleAlternatives.size())
  142. return true;
  143. InlineAsm::SubConstraintInfo &scInfo =
  144. ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
  145. if (scInfo.MatchingInput != -1)
  146. return true;
  147. // Note that operand #n has a matching input.
  148. scInfo.MatchingInput = ConstraintsSoFar.size();
  149. assert(scInfo.MatchingInput >= 0);
  150. } else {
  151. if (ConstraintsSoFar[N].hasMatchingInput() &&
  152. (size_t)ConstraintsSoFar[N].MatchingInput !=
  153. ConstraintsSoFar.size())
  154. return true;
  155. // Note that operand #n has a matching input.
  156. ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
  157. assert(ConstraintsSoFar[N].MatchingInput >= 0);
  158. }
  159. } else if (*I == '|') {
  160. multipleAlternativeIndex++;
  161. pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
  162. ++I;
  163. } else if (*I == '^') {
  164. // Multi-letter constraint
  165. // FIXME: For now assuming these are 2-character constraints.
  166. pCodes->push_back(std::string(StringRef(I + 1, 2)));
  167. I += 3;
  168. } else if (*I == '@') {
  169. // Multi-letter constraint
  170. ++I;
  171. unsigned char C = static_cast<unsigned char>(*I);
  172. assert(isdigit(C) && "Expected a digit!");
  173. int N = C - '0';
  174. assert(N > 0 && "Found a zero letter constraint!");
  175. ++I;
  176. pCodes->push_back(std::string(StringRef(I, N)));
  177. I += N;
  178. } else {
  179. // Single letter constraint.
  180. pCodes->push_back(std::string(StringRef(I, 1)));
  181. ++I;
  182. }
  183. }
  184. return false;
  185. }
  186. /// selectAlternative - Point this constraint to the alternative constraint
  187. /// indicated by the index.
  188. void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
  189. if (index < multipleAlternatives.size()) {
  190. currentAlternativeIndex = index;
  191. InlineAsm::SubConstraintInfo &scInfo =
  192. multipleAlternatives[currentAlternativeIndex];
  193. MatchingInput = scInfo.MatchingInput;
  194. Codes = scInfo.Codes;
  195. }
  196. }
  197. InlineAsm::ConstraintInfoVector
  198. InlineAsm::ParseConstraints(StringRef Constraints) {
  199. ConstraintInfoVector Result;
  200. // Scan the constraints string.
  201. for (StringRef::iterator I = Constraints.begin(),
  202. E = Constraints.end(); I != E; ) {
  203. ConstraintInfo Info;
  204. // Find the end of this constraint.
  205. StringRef::iterator ConstraintEnd = std::find(I, E, ',');
  206. if (ConstraintEnd == I || // Empty constraint like ",,"
  207. Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
  208. Result.clear(); // Erroneous constraint?
  209. break;
  210. }
  211. Result.push_back(Info);
  212. // ConstraintEnd may be either the next comma or the end of the string. In
  213. // the former case, we skip the comma.
  214. I = ConstraintEnd;
  215. if (I != E) {
  216. ++I;
  217. if (I == E) {
  218. Result.clear();
  219. break;
  220. } // don't allow "xyz,"
  221. }
  222. }
  223. return Result;
  224. }
  225. /// Verify - Verify that the specified constraint string is reasonable for the
  226. /// specified function type, and otherwise validate the constraint string.
  227. bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
  228. if (Ty->isVarArg()) return false;
  229. ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
  230. // Error parsing constraints.
  231. if (Constraints.empty() && !ConstStr.empty()) return false;
  232. unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
  233. unsigned NumIndirect = 0;
  234. for (const ConstraintInfo &Constraint : Constraints) {
  235. switch (Constraint.Type) {
  236. case InlineAsm::isOutput:
  237. if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
  238. return false; // outputs before inputs and clobbers.
  239. if (!Constraint.isIndirect) {
  240. ++NumOutputs;
  241. break;
  242. }
  243. ++NumIndirect;
  244. LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
  245. case InlineAsm::isInput:
  246. if (NumClobbers) return false; // inputs before clobbers.
  247. ++NumInputs;
  248. break;
  249. case InlineAsm::isClobber:
  250. ++NumClobbers;
  251. break;
  252. }
  253. }
  254. switch (NumOutputs) {
  255. case 0:
  256. if (!Ty->getReturnType()->isVoidTy()) return false;
  257. break;
  258. case 1:
  259. if (Ty->getReturnType()->isStructTy()) return false;
  260. break;
  261. default:
  262. StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
  263. if (!STy || STy->getNumElements() != NumOutputs)
  264. return false;
  265. break;
  266. }
  267. if (Ty->getNumParams() != NumInputs) return false;
  268. return true;
  269. }