blob: e49bb6dc8fba2fca31ec2c84aa91be7a69388bd3 [file] [log] [blame]
Chris Lattnercc041ba2006-01-24 04:13:11 +00001//===-- InlineAsm.cpp - Implement the InlineAsm class ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercc041ba2006-01-24 04:13:11 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the InlineAsm class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/InlineAsm.h"
15#include "llvm/DerivedTypes.h"
Jeff Cohen09f0bd32006-02-01 04:37:04 +000016#include <algorithm>
Chris Lattner3b917782006-01-26 00:48:33 +000017#include <cctype>
Chris Lattnercc041ba2006-01-24 04:13:11 +000018using namespace llvm;
19
Gordon Henriksenafba8fe2007-12-10 02:14:30 +000020// Implement the first virtual method in this class in this file so the
21// InlineAsm vtable is emitted here.
22InlineAsm::~InlineAsm() {
23}
24
25
Chris Lattner80cd1152006-01-25 22:26:05 +000026// NOTE: when memoizing the function type, we have to be careful to handle the
27// case when the type gets refined.
28
29InlineAsm *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 Lattnercc041ba2006-01-24 04:13:11 +000035InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
Chris Lattner863517a2006-01-25 18:57:27 +000036 const std::string &constraints, bool hasSideEffects)
Christopher Lamb43ad6b32007-12-17 01:12:55 +000037 : Value(PointerType::getUnqual(Ty),
38 Value::InlineAsmVal),
39 AsmString(asmString),
Chris Lattner863517a2006-01-25 18:57:27 +000040 Constraints(constraints), HasSideEffects(hasSideEffects) {
Chris Lattnercc041ba2006-01-24 04:13:11 +000041
Chris Lattner80cd1152006-01-25 22:26:05 +000042 // Do various checks on the constraint string and type.
43 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnercc041ba2006-01-24 04:13:11 +000044}
45
46const FunctionType *InlineAsm::getFunctionType() const {
47 return cast<FunctionType>(getType()->getElementType());
48}
Chris Lattner80cd1152006-01-25 22:26:05 +000049
Chris Lattnera55079a2006-02-01 01:29:47 +000050/// 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 Lattner2f0eec62006-02-02 00:23:53 +000053bool InlineAsm::ConstraintInfo::Parse(const std::string &Str,
54 std::vector<InlineAsm::ConstraintInfo> &ConstraintsSoFar) {
Chris Lattnera55079a2006-02-01 01:29:47 +000055 std::string::const_iterator I = Str.begin(), E = Str.end();
56
57 // Initialize
58 Type = isInput;
59 isEarlyClobber = false;
Chris Lattner2f0eec62006-02-02 00:23:53 +000060 hasMatchingInput = false;
Chris Lattnerfe3db462006-02-23 23:36:53 +000061 isCommutative = false;
Chris Lattner73d0d0d2007-04-28 01:02:58 +000062 isIndirect = false;
Chris Lattnera55079a2006-02-01 01:29:47 +000063
Chris Lattner73d0d0d2007-04-28 01:02:58 +000064 // Parse prefixes.
Chris Lattnera55079a2006-02-01 01:29:47 +000065 if (*I == '~') {
66 Type = isClobber;
67 ++I;
68 } else if (*I == '=') {
69 ++I;
70 Type = isOutput;
Chris Lattner73d0d0d2007-04-28 01:02:58 +000071 }
72
73 if (*I == '*') {
74 isIndirect = true;
75 ++I;
Chris Lattnera55079a2006-02-01 01:29:47 +000076 }
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 Lattnerfe3db462006-02-23 23:36:53 +000087 case '&': // Early clobber.
Chris Lattnera55079a2006-02-01 01:29:47 +000088 if (Type != isOutput || // Cannot early clobber anything but output.
89 isEarlyClobber) // Reject &&&&&&
90 return true;
91 isEarlyClobber = true;
92 break;
Chris Lattnerfe3db462006-02-23 23:36:53 +000093 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 Lattnera55079a2006-02-01 01:29:47 +0000102 }
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 Lattner2f0eec62006-02-02 00:23:53 +0000118 } else if (isdigit(*I)) { // Matching Constraint
Chris Lattnera55079a2006-02-01 01:29:47 +0000119 // 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 Lattner2f0eec62006-02-02 00:23:53 +0000124 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 Lattnera55079a2006-02-01 01:29:47 +0000132 } else {
133 // Single letter constraint.
134 Codes.push_back(std::string(I, I+1));
135 ++I;
136 }
137 }
138
139 return false;
140}
141
142std::vector<InlineAsm::ConstraintInfo>
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000143InlineAsm::ParseConstraints(const std::string &Constraints) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000144 std::vector<ConstraintInfo> Result;
Chris Lattner3b917782006-01-26 00:48:33 +0000145
146 // Scan the constraints string.
147 for (std::string::const_iterator I = Constraints.begin(),
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000148 E = Constraints.end(); I != E; ) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000149 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 Lattner2f0eec62006-02-02 00:23:53 +0000155 Info.Parse(std::string(I, ConstraintEnd), Result)) {
156 Result.clear(); // Erroneous constraint?
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000157 break;
158 }
Chris Lattnera55079a2006-02-01 01:29:47 +0000159
160 Result.push_back(Info);
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000161
Chris Lattnera55079a2006-02-01 01:29:47 +0000162 // 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 Lattnerf0b415f2006-01-26 02:21:59 +0000165 if (I != E) {
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000166 ++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.
177bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
178 if (Ty->isVarArg()) return false;
179
Chris Lattnera55079a2006-02-01 01:29:47 +0000180 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000181
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 Lattnera55079a2006-02-01 01:29:47 +0000188 switch (Constraints[i].Type) {
189 case InlineAsm::isOutput:
Chris Lattner73d0d0d2007-04-28 01:02:58 +0000190 if (!Constraints[i].isIndirect) {
Chris Lattnera55079a2006-02-01 01:29:47 +0000191 if (NumInputs || NumClobbers) return false; // outputs come first.
192 ++NumOutputs;
193 break;
194 }
Chris Lattner73d0d0d2007-04-28 01:02:58 +0000195 // FALLTHROUGH for Indirect Outputs.
Chris Lattnera55079a2006-02-01 01:29:47 +0000196 case InlineAsm::isInput:
Chris Lattner3b917782006-01-26 00:48:33 +0000197 if (NumClobbers) return false; // inputs before clobbers.
198 ++NumInputs;
199 break;
Chris Lattnera55079a2006-02-01 01:29:47 +0000200 case InlineAsm::isClobber:
Chris Lattner3b917782006-01-26 00:48:33 +0000201 ++NumClobbers;
202 break;
203 }
Chris Lattner3b917782006-01-26 00:48:33 +0000204 }
Chris Lattnerf0b415f2006-01-26 02:21:59 +0000205
206 if (NumOutputs > 1) return false; // Only one result allowed so far.
Chris Lattner3b917782006-01-26 00:48:33 +0000207
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 Lattner80cd1152006-01-25 22:26:05 +0000212 return true;
213}
Reid Spenceraf303d52006-06-07 23:03:13 +0000214