blob: 54367204175cdedea9bbd031ae311a516948d01c [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.
45bool 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
113std::vector<InlineAsm::ConstraintInfo>
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000114InlineAsm::ParseConstraints(const std::string &Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000115 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000116
117 // Scan the constraints string.
118 for (std::string::const_iterator I = Constraints.begin(),
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000119 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000120 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 Lattnerc981b8e2006-01-26 02:21:59 +0000127 Result.clear();
128 break;
129 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000130
131 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000132
Chris Lattner7ed31012006-02-01 01:29:47 +0000133 // 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 Lattnerc981b8e2006-01-26 02:21:59 +0000136 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000137 ++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.
148bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
149 if (Ty->isVarArg()) return false;
150
Chris Lattner7ed31012006-02-01 01:29:47 +0000151 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000152
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 Lattner7ed31012006-02-01 01:29:47 +0000159 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 Lattner8547e3a2006-01-26 00:48:33 +0000168 if (NumClobbers) return false; // inputs before clobbers.
169 ++NumInputs;
170 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000171 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000172 ++NumClobbers;
173 break;
174 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000175 }
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000176
177 if (NumOutputs > 1) return false; // Only one result allowed so far.
Chris Lattner8547e3a2006-01-26 00:48:33 +0000178
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 Lattnera2d810d2006-01-25 22:26:05 +0000183 return true;
184}