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