blob: 430fb07008e383caa79516206596dfc1bad16cf1 [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"
Chris Lattner8547e3a2006-01-26 00:48:33 +000016#include <cctype>
Chris Lattnereef2fe72006-01-24 04:13:11 +000017using namespace llvm;
18
Chris Lattnera2d810d2006-01-25 22:26:05 +000019// NOTE: when memoizing the function type, we have to be careful to handle the
20// case when the type gets refined.
21
22InlineAsm *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 Lattnereef2fe72006-01-24 04:13:11 +000028InlineAsm::InlineAsm(const FunctionType *Ty, const std::string &asmString,
Chris Lattner8bbcda22006-01-25 18:57:27 +000029 const std::string &constraints, bool hasSideEffects)
30 : Value(PointerType::get(Ty), Value::InlineAsmVal), AsmString(asmString),
31 Constraints(constraints), HasSideEffects(hasSideEffects) {
Chris Lattnereef2fe72006-01-24 04:13:11 +000032
Chris Lattnera2d810d2006-01-25 22:26:05 +000033 // Do various checks on the constraint string and type.
34 assert(Verify(Ty, constraints) && "Function type not legal for constraints!");
Chris Lattnereef2fe72006-01-24 04:13:11 +000035}
36
37const FunctionType *InlineAsm::getFunctionType() const {
38 return cast<FunctionType>(getType()->getElementType());
39}
Chris Lattnera2d810d2006-01-25 22:26:05 +000040
Chris Lattner7ed31012006-02-01 01:29:47 +000041/// 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.
44bool 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
112std::vector<InlineAsm::ConstraintInfo>
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000113InlineAsm::ParseConstraints(const std::string &Constraints) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000114 std::vector<ConstraintInfo> Result;
Chris Lattner8547e3a2006-01-26 00:48:33 +0000115
116 // Scan the constraints string.
117 for (std::string::const_iterator I = Constraints.begin(),
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000118 E = Constraints.end(); I != E; ) {
Chris Lattner7ed31012006-02-01 01:29:47 +0000119 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 Lattnerc981b8e2006-01-26 02:21:59 +0000126 Result.clear();
127 break;
128 }
Chris Lattner7ed31012006-02-01 01:29:47 +0000129
130 Result.push_back(Info);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000131
Chris Lattner7ed31012006-02-01 01:29:47 +0000132 // 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 Lattnerc981b8e2006-01-26 02:21:59 +0000135 if (I != E) {
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000136 ++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.
147bool InlineAsm::Verify(const FunctionType *Ty, const std::string &ConstStr) {
148 if (Ty->isVarArg()) return false;
149
Chris Lattner7ed31012006-02-01 01:29:47 +0000150 std::vector<ConstraintInfo> Constraints = ParseConstraints(ConstStr);
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000151
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 Lattner7ed31012006-02-01 01:29:47 +0000158 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 Lattner8547e3a2006-01-26 00:48:33 +0000167 if (NumClobbers) return false; // inputs before clobbers.
168 ++NumInputs;
169 break;
Chris Lattner7ed31012006-02-01 01:29:47 +0000170 case InlineAsm::isClobber:
Chris Lattner8547e3a2006-01-26 00:48:33 +0000171 ++NumClobbers;
172 break;
173 }
Chris Lattner8547e3a2006-01-26 00:48:33 +0000174 }
Chris Lattnerc981b8e2006-01-26 02:21:59 +0000175
176 if (NumOutputs > 1) return false; // Only one result allowed so far.
Chris Lattner8547e3a2006-01-26 00:48:33 +0000177
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 Lattnera2d810d2006-01-25 22:26:05 +0000182 return true;
183}