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