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