Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 1 | //===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the InlineAsm class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/InlineAsm.h" |
| 15 | #include "llvm/DerivedTypes.h" |
Jeff Cohen | b24b66f | 2006-02-01 04:37:04 +0000 | [diff] [blame] | 16 | #include <algorithm> |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 17 | #include <cctype> |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 20 | // NOTE: when memoizing the function type, we have to be careful to handle the |
| 21 | // case when the type gets refined. |
| 22 | |
| 23 | InlineAsm *InlineAsm::get(const FunctionType *Ty, const std::string &AsmString, |
| 24 | const std::string &Constraints, bool hasSideEffects) { |
| 25 | // FIXME: memoize! |
| 26 | return new InlineAsm(Ty, AsmString, Constraints, hasSideEffects); |
| 27 | } |
| 28 | |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 29 | InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString, |
Chris Lattner | 8bbcda2 | 2006-01-25 18:57:27 +0000 | [diff] [blame] | 30 | const std::string &constraints, bool hasSideEffects) |
| 31 | : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString), |
| 32 | Constraints(constraints), HasSideEffects(hasSideEffects) { |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 33 | |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 34 | // Do various checks on the constraint string and type. |
| 35 | assert(Verify(Ty, constraints) && "Function type not legal for constraints!"); |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | const FunctionType *InlineAsm::getFunctionType() const { |
| 39 | return cast<FunctionType>(getType()->getElementType()); |
| 40 | } |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 42 | /// Parse - Analyze the specified string (e.g. "==&{eax}") and fill in the |
| 43 | /// fields in this structure. If the constraint string is not understood, |
| 44 | /// return true, otherwise return false. |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame^] | 45 | bool InlineAsm::ConstraintInfo::Parse(const std::string &Str, |
| 46 | std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 47 | std::string::const_iterator I = Str.begin(), E = Str.end(); |
| 48 | |
| 49 | // Initialize |
| 50 | Type = isInput; |
| 51 | isEarlyClobber = false; |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame^] | 52 | isIndirectOutput = false; |
| 53 | hasMatchingInput = false; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 54 | |
| 55 | // Parse the prefix. |
| 56 | if (*I == '~') { |
| 57 | Type = isClobber; |
| 58 | ++I; |
| 59 | } else if (*I == '=') { |
| 60 | ++I; |
| 61 | Type = isOutput; |
| 62 | if (I != E && *I == '=') { |
| 63 | isIndirectOutput = true; |
| 64 | ++I; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (I == E) return true; // Just a prefix, like "==" or "~". |
| 69 | |
| 70 | // Parse the modifiers. |
| 71 | bool DoneWithModifiers = false; |
| 72 | while (!DoneWithModifiers) { |
| 73 | switch (*I) { |
| 74 | default: |
| 75 | DoneWithModifiers = true; |
| 76 | break; |
| 77 | case '&': |
| 78 | if (Type != isOutput || // Cannot early clobber anything but output. |
| 79 | isEarlyClobber) // Reject &&&&&& |
| 80 | return true; |
| 81 | isEarlyClobber = true; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | if (!DoneWithModifiers) { |
| 86 | ++I; |
| 87 | if (I == E) return true; // Just prefixes and modifiers! |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Parse the various constraints. |
| 92 | while (I != E) { |
| 93 | if (*I == '{') { // Physical register reference. |
| 94 | // Find the end of the register name. |
| 95 | std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}'); |
| 96 | if (ConstraintEnd == E) return true; // "{foo" |
| 97 | Codes.push_back(std::string(I, ConstraintEnd+1)); |
| 98 | I = ConstraintEnd+1; |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame^] | 99 | } else if (isdigit(*I)) { // Matching Constraint |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 100 | // Maximal munch numbers. |
| 101 | std::string::const_iterator NumStart = I; |
| 102 | while (I != E && isdigit(*I)) |
| 103 | ++I; |
| 104 | Codes.push_back(std::string(NumStart, I)); |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame^] | 105 | unsigned N = atoi(Codes.back().c_str()); |
| 106 | // Check that this is a valid matching constraint! |
| 107 | if (N >= ConstraintsSoFar.size() || ConstraintsSoFar[N].Type != isOutput|| |
| 108 | Type != isInput) |
| 109 | return true; // Invalid constraint number. |
| 110 | |
| 111 | // Note that operand #n has a matching input. |
| 112 | ConstraintsSoFar[N].hasMatchingInput = true; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 113 | } else { |
| 114 | // Single letter constraint. |
| 115 | Codes.push_back(std::string(I, I+1)); |
| 116 | ++I; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | std::vector<InlineAsm::ConstraintInfo> |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 124 | InlineAsm::ParseConstraints(const std::string &Constraints) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 125 | std::vector<ConstraintInfo> Result; |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 126 | |
| 127 | // Scan the constraints string. |
| 128 | for (std::string::const_iterator I = Constraints.begin(), |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 129 | E = Constraints.end(); I != E; ) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 130 | ConstraintInfo Info; |
| 131 | |
| 132 | // Find the end of this constraint. |
| 133 | std::string::const_iterator ConstraintEnd = std::find(I, E, ','); |
| 134 | |
| 135 | if (ConstraintEnd == I || // Empty constraint like ",," |
Chris Lattner | 2f34a9e | 2006-02-02 00:23:53 +0000 | [diff] [blame^] | 136 | Info.Parse(std::string(I, ConstraintEnd), Result)) { |
| 137 | Result.clear(); // Erroneous constraint? |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 140 | |
| 141 | Result.push_back(Info); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 143 | // ConstraintEnd may be either the next comma or the end of the string. In |
| 144 | // the former case, we skip the comma. |
| 145 | I = ConstraintEnd; |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 146 | if (I != E) { |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 147 | ++I; |
| 148 | if (I == E) { Result.clear(); break; } // don't allow "xyz," |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return Result; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /// Verify - Verify that the specified constraint string is reasonable for the |
| 157 | /// specified function type, and otherwise validate the constraint string. |
| 158 | bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) { |
| 159 | if (Ty->isVarArg()) return false; |
| 160 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 161 | std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 162 | |
| 163 | // Error parsing constraints. |
| 164 | if (Constraints.empty() && !ConstStr.empty()) return false; |
| 165 | |
| 166 | unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; |
| 167 | |
| 168 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 169 | switch (Constraints[i].Type) { |
| 170 | case InlineAsm::isOutput: |
| 171 | if (!Constraints[i].isIndirectOutput) { |
| 172 | if (NumInputs || NumClobbers) return false; // outputs come first. |
| 173 | ++NumOutputs; |
| 174 | break; |
| 175 | } |
| 176 | // FALLTHROUGH for IndirectOutputs. |
| 177 | case InlineAsm::isInput: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 178 | if (NumClobbers) return false; // inputs before clobbers. |
| 179 | ++NumInputs; |
| 180 | break; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 181 | case InlineAsm::isClobber: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 182 | ++NumClobbers; |
| 183 | break; |
| 184 | } |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 185 | } |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 186 | |
| 187 | if (NumOutputs > 1) return false; // Only one result allowed so far. |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 188 | |
| 189 | if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs) |
| 190 | return false; // NumOutputs = 1 iff has a result type. |
| 191 | |
| 192 | if (Ty->getNumParams() != NumInputs) return false; |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 193 | return true; |
| 194 | } |