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. |
| 45 | bool InlineAsm::ConstraintInfo::Parse(const std::string &Str) { |
| 46 | std::string::const_iterator I = Str.begin(), E = Str.end(); |
| 47 | |
| 48 | // Initialize |
| 49 | Type = isInput; |
| 50 | isEarlyClobber = false; |
| 51 | isIndirectOutput =false; |
| 52 | |
| 53 | // Parse the prefix. |
| 54 | if (*I == '~') { |
| 55 | Type = isClobber; |
| 56 | ++I; |
| 57 | } else if (*I == '=') { |
| 58 | ++I; |
| 59 | Type = isOutput; |
| 60 | if (I != E && *I == '=') { |
| 61 | isIndirectOutput = true; |
| 62 | ++I; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (I == E) return true; // Just a prefix, like "==" or "~". |
| 67 | |
| 68 | // Parse the modifiers. |
| 69 | bool DoneWithModifiers = false; |
| 70 | while (!DoneWithModifiers) { |
| 71 | switch (*I) { |
| 72 | default: |
| 73 | DoneWithModifiers = true; |
| 74 | break; |
| 75 | case '&': |
| 76 | if (Type != isOutput || // Cannot early clobber anything but output. |
| 77 | isEarlyClobber) // Reject &&&&&& |
| 78 | return true; |
| 79 | isEarlyClobber = true; |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (!DoneWithModifiers) { |
| 84 | ++I; |
| 85 | if (I == E) return true; // Just prefixes and modifiers! |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Parse the various constraints. |
| 90 | while (I != E) { |
| 91 | if (*I == '{') { // Physical register reference. |
| 92 | // Find the end of the register name. |
| 93 | std::string::const_iterator ConstraintEnd = std::find(I+1, E, '}'); |
| 94 | if (ConstraintEnd == E) return true; // "{foo" |
| 95 | Codes.push_back(std::string(I, ConstraintEnd+1)); |
| 96 | I = ConstraintEnd+1; |
| 97 | } else if (isdigit(*I)) { |
| 98 | // Maximal munch numbers. |
| 99 | std::string::const_iterator NumStart = I; |
| 100 | while (I != E && isdigit(*I)) |
| 101 | ++I; |
| 102 | Codes.push_back(std::string(NumStart, I)); |
| 103 | } else { |
| 104 | // Single letter constraint. |
| 105 | Codes.push_back(std::string(I, I+1)); |
| 106 | ++I; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | std::vector<InlineAsm::ConstraintInfo> |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 114 | InlineAsm::ParseConstraints(const std::string &Constraints) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 115 | std::vector<ConstraintInfo> Result; |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 116 | |
| 117 | // Scan the constraints string. |
| 118 | for (std::string::const_iterator I = Constraints.begin(), |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 119 | E = Constraints.end(); I != E; ) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 120 | ConstraintInfo Info; |
| 121 | |
| 122 | // Find the end of this constraint. |
| 123 | std::string::const_iterator ConstraintEnd = std::find(I, E, ','); |
| 124 | |
| 125 | if (ConstraintEnd == I || // Empty constraint like ",," |
| 126 | Info.Parse(std::string(I, ConstraintEnd))) { // Erroneous constraint? |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 127 | Result.clear(); |
| 128 | break; |
| 129 | } |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 130 | |
| 131 | Result.push_back(Info); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 133 | // ConstraintEnd may be either the next comma or the end of the string. In |
| 134 | // the former case, we skip the comma. |
| 135 | I = ConstraintEnd; |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 136 | if (I != E) { |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 137 | ++I; |
| 138 | if (I == E) { Result.clear(); break; } // don't allow "xyz," |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return Result; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /// Verify - Verify that the specified constraint string is reasonable for the |
| 147 | /// specified function type, and otherwise validate the constraint string. |
| 148 | bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) { |
| 149 | if (Ty->isVarArg()) return false; |
| 150 | |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 151 | std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr); |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 152 | |
| 153 | // Error parsing constraints. |
| 154 | if (Constraints.empty() && !ConstStr.empty()) return false; |
| 155 | |
| 156 | unsigned NumOutputs = 0, NumInputs = 0, NumClobbers = 0; |
| 157 | |
| 158 | for (unsigned i = 0, e = Constraints.size(); i != e; ++i) { |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 159 | switch (Constraints[i].Type) { |
| 160 | case InlineAsm::isOutput: |
| 161 | if (!Constraints[i].isIndirectOutput) { |
| 162 | if (NumInputs || NumClobbers) return false; // outputs come first. |
| 163 | ++NumOutputs; |
| 164 | break; |
| 165 | } |
| 166 | // FALLTHROUGH for IndirectOutputs. |
| 167 | case InlineAsm::isInput: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 168 | if (NumClobbers) return false; // inputs before clobbers. |
| 169 | ++NumInputs; |
| 170 | break; |
Chris Lattner | 7ed3101 | 2006-02-01 01:29:47 +0000 | [diff] [blame] | 171 | case InlineAsm::isClobber: |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 172 | ++NumClobbers; |
| 173 | break; |
| 174 | } |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | c981b8e | 2006-01-26 02:21:59 +0000 | [diff] [blame] | 176 | |
| 177 | if (NumOutputs > 1) return false; // Only one result allowed so far. |
Chris Lattner | 8547e3a | 2006-01-26 00:48:33 +0000 | [diff] [blame] | 178 | |
| 179 | if ((Ty->getReturnType() != Type::VoidTy) != NumOutputs) |
| 180 | return false; // NumOutputs = 1 iff has a result type. |
| 181 | |
| 182 | if (Ty->getNumParams() != NumInputs) return false; |
Chris Lattner | a2d810d | 2006-01-25 22:26:05 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |