blob: cdb8c46e95fa53697197af6a5cbd84355b961833 [file] [log] [blame]
Chris Lattnereef2fe72006-01-24 04:13:11 +00001//===-- 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 Cohenb24b66f2006-02-01 04:37:04 +000016#include <algorithm>
Chris Lattner8547e3a2006-01-26 00:48:33 +000017#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000018using namespace llvm;
19
Chris Lattnera2d810d2006-01-25 22:26:05 +000020// NOTE: when memoizing the function type, we have to be careful to handle the
21// case when the type gets refined.
22
23InlineAsm *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 Lattnereef2fe72006-01-24 04:13:11 +000029InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
Chris Lattner8bbcda22006-01-25 18:57:27 +000030 const std::string &constraints, bool hasSideEffects)
31 : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString),
32 Constraints(constraints), HasSideEffects(hasSideEffects) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000033
Chris Lattnera2d810d2006-01-25 22:26:05 +000034 // Do various checks on the constraint string and type.
35 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000036}
37
38const FunctionType *InlineAsm::getFunctionType() const {
39 return cast<FunctionType>(getType()->getElementType());
40}
Chris Lattnera2d810d2006-01-25 22:26:05 +000041
Chris Lattner7ed31012006-02-01 01:29:47 +000042/// 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 Lattner2f34a9e2006-02-02 00:23:53 +000045bool InlineAsm::ConstraintInfo::Parse(const std::string &Str,
46 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Chris Lattner7ed31012006-02-01 01:29:47 +000047 std::string::const_iterator I = Str.begin(), E = Str.end();
48
49 // Initialize
50 Type = isInput;
51 isEarlyClobber = false;
Chris Lattner2f34a9e2006-02-02 00:23:53 +000052 isIndirectOutput = false;
53 hasMatchingInput = false;
Chris Lattner7ed31012006-02-01 01:29:47 +000054
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 Lattner2f34a9e2006-02-02 00:23:53 +000099 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattner7ed31012006-02-01 01:29:47 +0000100 // 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 Lattner2f34a9e2006-02-02 00:23:53 +0000105 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 Lattner7ed31012006-02-01 01:29:47 +0000113 } else {
114 // Single letter constraint.
115 Codes.push_back(std::string(I, I+1));
116 ++I;
117 }
118 }
119
120 return false;
121}
122
123std::vector<InlineAsm::ConstraintInfo>
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000124InlineAsm::ParseConstraints(const std::string &Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000125 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000126
127 // Scan the constraints string.
128 for (std::string::const_iterator I = Constraints.begin(),
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000129 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000130 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 Lattner2f34a9e2006-02-02 00:23:53 +0000136 Info.Parse(std::string(I, ConstraintEnd), Result)) {
137 Result.clear(); // Erroneous constraint?
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000138 break;
139 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000140
141 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000142
Chris Lattner7ed31012006-02-01 01:29:47 +0000143 // 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 Lattnerc981b8e2006-01-26 02:21:59 +0000146 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000147 ++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.
158bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
159 if (Ty->isVarArg()) return false;
160
Chris Lattner7ed31012006-02-01 01:29:47 +0000161 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000162
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 Lattner7ed31012006-02-01 01:29:47 +0000169 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 Lattner8547e3a2006-01-26 00:48:33 +0000178 if (NumClobbers) return false; // inputs before clobbers.
179 ++NumInputs;
180 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000181 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000182 ++NumClobbers;
183 break;
184 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000185 }
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000186
187 if (NumOutputs > 1) return false; // Only one result allowed so far.
Chris Lattner8547e3a2006-01-26 00:48:33 +0000188
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 Lattnera2d810d2006-01-25 22:26:05 +0000193 return true;
194}