106f32e7eSjoerg //===- InlineAsm.cpp - Implement the InlineAsm class ----------------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file implements the InlineAsm class.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "llvm/IR/InlineAsm.h"
1406f32e7eSjoerg #include "ConstantsContext.h"
1506f32e7eSjoerg #include "LLVMContextImpl.h"
1606f32e7eSjoerg #include "llvm/ADT/StringRef.h"
1706f32e7eSjoerg #include "llvm/IR/DerivedTypes.h"
1806f32e7eSjoerg #include "llvm/IR/LLVMContext.h"
1906f32e7eSjoerg #include "llvm/IR/Value.h"
2006f32e7eSjoerg #include "llvm/Support/Casting.h"
2106f32e7eSjoerg #include "llvm/Support/Compiler.h"
2206f32e7eSjoerg #include <algorithm>
2306f32e7eSjoerg #include <cassert>
2406f32e7eSjoerg #include <cctype>
2506f32e7eSjoerg #include <cstddef>
2606f32e7eSjoerg #include <cstdlib>
2706f32e7eSjoerg 
2806f32e7eSjoerg using namespace llvm;
2906f32e7eSjoerg 
InlineAsm(FunctionType * FTy,const std::string & asmString,const std::string & constraints,bool hasSideEffects,bool isAlignStack,AsmDialect asmDialect,bool canThrow)3006f32e7eSjoerg InlineAsm::InlineAsm(FunctionType *FTy, const std::string &asmString,
3106f32e7eSjoerg                      const std::string &constraints, bool hasSideEffects,
32*da58b97aSjoerg                      bool isAlignStack, AsmDialect asmDialect, bool canThrow)
3306f32e7eSjoerg     : Value(PointerType::getUnqual(FTy), Value::InlineAsmVal),
3406f32e7eSjoerg       AsmString(asmString), Constraints(constraints), FTy(FTy),
3506f32e7eSjoerg       HasSideEffects(hasSideEffects), IsAlignStack(isAlignStack),
36*da58b97aSjoerg       Dialect(asmDialect), CanThrow(canThrow) {
3706f32e7eSjoerg   // Do various checks on the constraint string and type.
3806f32e7eSjoerg   assert(Verify(getFunctionType(), constraints) &&
3906f32e7eSjoerg          "Function type not legal for constraints!");
4006f32e7eSjoerg }
4106f32e7eSjoerg 
get(FunctionType * FTy,StringRef AsmString,StringRef Constraints,bool hasSideEffects,bool isAlignStack,AsmDialect asmDialect,bool canThrow)4206f32e7eSjoerg InlineAsm *InlineAsm::get(FunctionType *FTy, StringRef AsmString,
4306f32e7eSjoerg                           StringRef Constraints, bool hasSideEffects,
44*da58b97aSjoerg                           bool isAlignStack, AsmDialect asmDialect,
45*da58b97aSjoerg                           bool canThrow) {
4606f32e7eSjoerg   InlineAsmKeyType Key(AsmString, Constraints, FTy, hasSideEffects,
47*da58b97aSjoerg                        isAlignStack, asmDialect, canThrow);
4806f32e7eSjoerg   LLVMContextImpl *pImpl = FTy->getContext().pImpl;
4906f32e7eSjoerg   return pImpl->InlineAsms.getOrCreate(PointerType::getUnqual(FTy), Key);
5006f32e7eSjoerg }
5106f32e7eSjoerg 
destroyConstant()5206f32e7eSjoerg void InlineAsm::destroyConstant() {
5306f32e7eSjoerg   getType()->getContext().pImpl->InlineAsms.remove(this);
5406f32e7eSjoerg   delete this;
5506f32e7eSjoerg }
5606f32e7eSjoerg 
getFunctionType() const5706f32e7eSjoerg FunctionType *InlineAsm::getFunctionType() const {
5806f32e7eSjoerg   return FTy;
5906f32e7eSjoerg }
6006f32e7eSjoerg 
6106f32e7eSjoerg /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the
6206f32e7eSjoerg /// fields in this structure.  If the constraint string is not understood,
6306f32e7eSjoerg /// return true, otherwise return false.
Parse(StringRef Str,InlineAsm::ConstraintInfoVector & ConstraintsSoFar)6406f32e7eSjoerg bool InlineAsm::ConstraintInfo::Parse(StringRef Str,
6506f32e7eSjoerg                      InlineAsm::ConstraintInfoVector &ConstraintsSoFar) {
6606f32e7eSjoerg   StringRef::iterator I = Str.begin(), E = Str.end();
6706f32e7eSjoerg   unsigned multipleAlternativeCount = Str.count('|') + 1;
6806f32e7eSjoerg   unsigned multipleAlternativeIndex = 0;
6906f32e7eSjoerg   ConstraintCodeVector *pCodes = &Codes;
7006f32e7eSjoerg 
7106f32e7eSjoerg   // Initialize
7206f32e7eSjoerg   isMultipleAlternative = multipleAlternativeCount > 1;
7306f32e7eSjoerg   if (isMultipleAlternative) {
7406f32e7eSjoerg     multipleAlternatives.resize(multipleAlternativeCount);
7506f32e7eSjoerg     pCodes = &multipleAlternatives[0].Codes;
7606f32e7eSjoerg   }
7706f32e7eSjoerg   Type = isInput;
7806f32e7eSjoerg   isEarlyClobber = false;
7906f32e7eSjoerg   MatchingInput = -1;
8006f32e7eSjoerg   isCommutative = false;
8106f32e7eSjoerg   isIndirect = false;
8206f32e7eSjoerg   currentAlternativeIndex = 0;
8306f32e7eSjoerg 
8406f32e7eSjoerg   // Parse prefixes.
8506f32e7eSjoerg   if (*I == '~') {
8606f32e7eSjoerg     Type = isClobber;
8706f32e7eSjoerg     ++I;
8806f32e7eSjoerg 
8906f32e7eSjoerg     // '{' must immediately follow '~'.
9006f32e7eSjoerg     if (I != E && *I != '{')
9106f32e7eSjoerg       return true;
9206f32e7eSjoerg   } else if (*I == '=') {
9306f32e7eSjoerg     ++I;
9406f32e7eSjoerg     Type = isOutput;
9506f32e7eSjoerg   }
9606f32e7eSjoerg 
9706f32e7eSjoerg   if (*I == '*') {
9806f32e7eSjoerg     isIndirect = true;
9906f32e7eSjoerg     ++I;
10006f32e7eSjoerg   }
10106f32e7eSjoerg 
10206f32e7eSjoerg   if (I == E) return true;  // Just a prefix, like "==" or "~".
10306f32e7eSjoerg 
10406f32e7eSjoerg   // Parse the modifiers.
10506f32e7eSjoerg   bool DoneWithModifiers = false;
10606f32e7eSjoerg   while (!DoneWithModifiers) {
10706f32e7eSjoerg     switch (*I) {
10806f32e7eSjoerg     default:
10906f32e7eSjoerg       DoneWithModifiers = true;
11006f32e7eSjoerg       break;
11106f32e7eSjoerg     case '&':     // Early clobber.
11206f32e7eSjoerg       if (Type != isOutput ||      // Cannot early clobber anything but output.
11306f32e7eSjoerg           isEarlyClobber)          // Reject &&&&&&
11406f32e7eSjoerg         return true;
11506f32e7eSjoerg       isEarlyClobber = true;
11606f32e7eSjoerg       break;
11706f32e7eSjoerg     case '%':     // Commutative.
11806f32e7eSjoerg       if (Type == isClobber ||     // Cannot commute clobbers.
11906f32e7eSjoerg           isCommutative)           // Reject %%%%%
12006f32e7eSjoerg         return true;
12106f32e7eSjoerg       isCommutative = true;
12206f32e7eSjoerg       break;
12306f32e7eSjoerg     case '#':     // Comment.
12406f32e7eSjoerg     case '*':     // Register preferencing.
12506f32e7eSjoerg       return true;     // Not supported.
12606f32e7eSjoerg     }
12706f32e7eSjoerg 
12806f32e7eSjoerg     if (!DoneWithModifiers) {
12906f32e7eSjoerg       ++I;
13006f32e7eSjoerg       if (I == E) return true;   // Just prefixes and modifiers!
13106f32e7eSjoerg     }
13206f32e7eSjoerg   }
13306f32e7eSjoerg 
13406f32e7eSjoerg   // Parse the various constraints.
13506f32e7eSjoerg   while (I != E) {
13606f32e7eSjoerg     if (*I == '{') {   // Physical register reference.
13706f32e7eSjoerg       // Find the end of the register name.
13806f32e7eSjoerg       StringRef::iterator ConstraintEnd = std::find(I+1, E, '}');
13906f32e7eSjoerg       if (ConstraintEnd == E) return true;  // "{foo"
140*da58b97aSjoerg       pCodes->push_back(std::string(StringRef(I, ConstraintEnd + 1 - I)));
14106f32e7eSjoerg       I = ConstraintEnd+1;
14206f32e7eSjoerg     } else if (isdigit(static_cast<unsigned char>(*I))) { // Matching Constraint
14306f32e7eSjoerg       // Maximal munch numbers.
14406f32e7eSjoerg       StringRef::iterator NumStart = I;
14506f32e7eSjoerg       while (I != E && isdigit(static_cast<unsigned char>(*I)))
14606f32e7eSjoerg         ++I;
147*da58b97aSjoerg       pCodes->push_back(std::string(StringRef(NumStart, I - NumStart)));
14806f32e7eSjoerg       unsigned N = atoi(pCodes->back().c_str());
14906f32e7eSjoerg       // Check that this is a valid matching constraint!
15006f32e7eSjoerg       if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput||
15106f32e7eSjoerg           Type != isInput)
15206f32e7eSjoerg         return true;  // Invalid constraint number.
15306f32e7eSjoerg 
15406f32e7eSjoerg       // If Operand N already has a matching input, reject this.  An output
15506f32e7eSjoerg       // can't be constrained to the same value as multiple inputs.
15606f32e7eSjoerg       if (isMultipleAlternative) {
15706f32e7eSjoerg         if (multipleAlternativeIndex >=
15806f32e7eSjoerg             ConstraintsSoFar[N].multipleAlternatives.size())
15906f32e7eSjoerg           return true;
16006f32e7eSjoerg         InlineAsm::SubConstraintInfo &scInfo =
16106f32e7eSjoerg           ConstraintsSoFar[N].multipleAlternatives[multipleAlternativeIndex];
16206f32e7eSjoerg         if (scInfo.MatchingInput != -1)
16306f32e7eSjoerg           return true;
16406f32e7eSjoerg         // Note that operand #n has a matching input.
16506f32e7eSjoerg         scInfo.MatchingInput = ConstraintsSoFar.size();
16606f32e7eSjoerg         assert(scInfo.MatchingInput >= 0);
16706f32e7eSjoerg       } else {
16806f32e7eSjoerg         if (ConstraintsSoFar[N].hasMatchingInput() &&
16906f32e7eSjoerg             (size_t)ConstraintsSoFar[N].MatchingInput !=
17006f32e7eSjoerg                 ConstraintsSoFar.size())
17106f32e7eSjoerg           return true;
17206f32e7eSjoerg         // Note that operand #n has a matching input.
17306f32e7eSjoerg         ConstraintsSoFar[N].MatchingInput = ConstraintsSoFar.size();
17406f32e7eSjoerg         assert(ConstraintsSoFar[N].MatchingInput >= 0);
17506f32e7eSjoerg         }
17606f32e7eSjoerg     } else if (*I == '|') {
17706f32e7eSjoerg       multipleAlternativeIndex++;
17806f32e7eSjoerg       pCodes = &multipleAlternatives[multipleAlternativeIndex].Codes;
17906f32e7eSjoerg       ++I;
18006f32e7eSjoerg     } else if (*I == '^') {
18106f32e7eSjoerg       // Multi-letter constraint
18206f32e7eSjoerg       // FIXME: For now assuming these are 2-character constraints.
183*da58b97aSjoerg       pCodes->push_back(std::string(StringRef(I + 1, 2)));
18406f32e7eSjoerg       I += 3;
18506f32e7eSjoerg     } else if (*I == '@') {
18606f32e7eSjoerg       // Multi-letter constraint
18706f32e7eSjoerg       ++I;
18806f32e7eSjoerg       unsigned char C = static_cast<unsigned char>(*I);
18906f32e7eSjoerg       assert(isdigit(C) && "Expected a digit!");
19006f32e7eSjoerg       int N = C - '0';
19106f32e7eSjoerg       assert(N > 0 && "Found a zero letter constraint!");
19206f32e7eSjoerg       ++I;
193*da58b97aSjoerg       pCodes->push_back(std::string(StringRef(I, N)));
19406f32e7eSjoerg       I += N;
19506f32e7eSjoerg     } else {
19606f32e7eSjoerg       // Single letter constraint.
197*da58b97aSjoerg       pCodes->push_back(std::string(StringRef(I, 1)));
19806f32e7eSjoerg       ++I;
19906f32e7eSjoerg     }
20006f32e7eSjoerg   }
20106f32e7eSjoerg 
20206f32e7eSjoerg   return false;
20306f32e7eSjoerg }
20406f32e7eSjoerg 
20506f32e7eSjoerg /// selectAlternative - Point this constraint to the alternative constraint
20606f32e7eSjoerg /// indicated by the index.
selectAlternative(unsigned index)20706f32e7eSjoerg void InlineAsm::ConstraintInfo::selectAlternative(unsigned index) {
20806f32e7eSjoerg   if (index < multipleAlternatives.size()) {
20906f32e7eSjoerg     currentAlternativeIndex = index;
21006f32e7eSjoerg     InlineAsm::SubConstraintInfo &scInfo =
21106f32e7eSjoerg       multipleAlternatives[currentAlternativeIndex];
21206f32e7eSjoerg     MatchingInput = scInfo.MatchingInput;
21306f32e7eSjoerg     Codes = scInfo.Codes;
21406f32e7eSjoerg   }
21506f32e7eSjoerg }
21606f32e7eSjoerg 
21706f32e7eSjoerg InlineAsm::ConstraintInfoVector
ParseConstraints(StringRef Constraints)21806f32e7eSjoerg InlineAsm::ParseConstraints(StringRef Constraints) {
21906f32e7eSjoerg   ConstraintInfoVector Result;
22006f32e7eSjoerg 
22106f32e7eSjoerg   // Scan the constraints string.
22206f32e7eSjoerg   for (StringRef::iterator I = Constraints.begin(),
22306f32e7eSjoerg          E = Constraints.end(); I != E; ) {
22406f32e7eSjoerg     ConstraintInfo Info;
22506f32e7eSjoerg 
22606f32e7eSjoerg     // Find the end of this constraint.
22706f32e7eSjoerg     StringRef::iterator ConstraintEnd = std::find(I, E, ',');
22806f32e7eSjoerg 
22906f32e7eSjoerg     if (ConstraintEnd == I ||  // Empty constraint like ",,"
23006f32e7eSjoerg         Info.Parse(StringRef(I, ConstraintEnd-I), Result)) {
23106f32e7eSjoerg       Result.clear();          // Erroneous constraint?
23206f32e7eSjoerg       break;
23306f32e7eSjoerg     }
23406f32e7eSjoerg 
23506f32e7eSjoerg     Result.push_back(Info);
23606f32e7eSjoerg 
23706f32e7eSjoerg     // ConstraintEnd may be either the next comma or the end of the string.  In
23806f32e7eSjoerg     // the former case, we skip the comma.
23906f32e7eSjoerg     I = ConstraintEnd;
24006f32e7eSjoerg     if (I != E) {
24106f32e7eSjoerg       ++I;
24206f32e7eSjoerg       if (I == E) {
24306f32e7eSjoerg         Result.clear();
24406f32e7eSjoerg         break;
24506f32e7eSjoerg       } // don't allow "xyz,"
24606f32e7eSjoerg     }
24706f32e7eSjoerg   }
24806f32e7eSjoerg 
24906f32e7eSjoerg   return Result;
25006f32e7eSjoerg }
25106f32e7eSjoerg 
25206f32e7eSjoerg /// Verify - Verify that the specified constraint string is reasonable for the
25306f32e7eSjoerg /// specified function type, and otherwise validate the constraint string.
Verify(FunctionType * Ty,StringRef ConstStr)25406f32e7eSjoerg bool InlineAsm::Verify(FunctionType *Ty, StringRef ConstStr) {
25506f32e7eSjoerg   if (Ty->isVarArg()) return false;
25606f32e7eSjoerg 
25706f32e7eSjoerg   ConstraintInfoVector Constraints = ParseConstraints(ConstStr);
25806f32e7eSjoerg 
25906f32e7eSjoerg   // Error parsing constraints.
26006f32e7eSjoerg   if (Constraints.empty() && !ConstStr.empty()) return false;
26106f32e7eSjoerg 
26206f32e7eSjoerg   unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0;
26306f32e7eSjoerg   unsigned NumIndirect = 0;
26406f32e7eSjoerg 
26506f32e7eSjoerg   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
26606f32e7eSjoerg     switch (Constraints[i].Type) {
26706f32e7eSjoerg     case InlineAsm::isOutput:
26806f32e7eSjoerg       if ((NumInputs-NumIndirect) != 0 || NumClobbers != 0)
26906f32e7eSjoerg         return false;  // outputs before inputs and clobbers.
27006f32e7eSjoerg       if (!Constraints[i].isIndirect) {
27106f32e7eSjoerg         ++NumOutputs;
27206f32e7eSjoerg         break;
27306f32e7eSjoerg       }
27406f32e7eSjoerg       ++NumIndirect;
27506f32e7eSjoerg       LLVM_FALLTHROUGH; // We fall through for Indirect Outputs.
27606f32e7eSjoerg     case InlineAsm::isInput:
27706f32e7eSjoerg       if (NumClobbers) return false;               // inputs before clobbers.
27806f32e7eSjoerg       ++NumInputs;
27906f32e7eSjoerg       break;
28006f32e7eSjoerg     case InlineAsm::isClobber:
28106f32e7eSjoerg       ++NumClobbers;
28206f32e7eSjoerg       break;
28306f32e7eSjoerg     }
28406f32e7eSjoerg   }
28506f32e7eSjoerg 
28606f32e7eSjoerg   switch (NumOutputs) {
28706f32e7eSjoerg   case 0:
28806f32e7eSjoerg     if (!Ty->getReturnType()->isVoidTy()) return false;
28906f32e7eSjoerg     break;
29006f32e7eSjoerg   case 1:
29106f32e7eSjoerg     if (Ty->getReturnType()->isStructTy()) return false;
29206f32e7eSjoerg     break;
29306f32e7eSjoerg   default:
29406f32e7eSjoerg     StructType *STy = dyn_cast<StructType>(Ty->getReturnType());
29506f32e7eSjoerg     if (!STy || STy->getNumElements() != NumOutputs)
29606f32e7eSjoerg       return false;
29706f32e7eSjoerg     break;
29806f32e7eSjoerg   }
29906f32e7eSjoerg 
30006f32e7eSjoerg   if (Ty->getNumParams() != NumInputs) return false;
30106f32e7eSjoerg   return true;
30206f32e7eSjoerg }
303