blob: b9345d31c7bf8f85fb889dbc2fdd4f7dc1ccb257 [file] [log] [blame]
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001//===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a target specifier matcher for converting parsed
11// assembly operands in the MCInst structures.
12//
Daniel Dunbar20927f22009-08-07 08:26:05 +000013// The input to the target specific matcher is a list of literal tokens and
14// operands. The target specific parser should generally eliminate any syntax
15// which is not relevant for matching; for example, comma tokens should have
16// already been consumed and eliminated by the parser. Most instructions will
17// end up with a single literal token (the instruction name) and some number of
18// operands.
19//
20// Some example inputs, for X86:
21// 'addl' (immediate ...) (register ...)
22// 'add' (immediate ...) (memory ...)
Jim Grosbacha7c78222010-10-29 22:13:48 +000023// 'call' '*' %epc
Daniel Dunbar20927f22009-08-07 08:26:05 +000024//
25// The assembly matcher is responsible for converting this input into a precise
26// machine instruction (i.e., an instruction with a well defined encoding). This
27// mapping has several properties which complicate matching:
28//
29// - It may be ambiguous; many architectures can legally encode particular
30// variants of an instruction in different ways (for example, using a smaller
31// encoding for small immediates). Such ambiguities should never be
32// arbitrarily resolved by the assembler, the assembler is always responsible
33// for choosing the "best" available instruction.
34//
35// - It may depend on the subtarget or the assembler context. Instructions
36// which are invalid for the current mode, but otherwise unambiguous (e.g.,
37// an SSE instruction in a file being assembled for i486) should be accepted
38// and rejected by the assembler front end. However, if the proper encoding
39// for an instruction is dependent on the assembler context then the matcher
40// is responsible for selecting the correct machine instruction for the
41// current mode.
42//
43// The core matching algorithm attempts to exploit the regularity in most
44// instruction sets to quickly determine the set of possibly matching
45// instructions, and the simplify the generated code. Additionally, this helps
46// to ensure that the ambiguities are intentionally resolved by the user.
47//
48// The matching is divided into two distinct phases:
49//
50// 1. Classification: Each operand is mapped to the unique set which (a)
51// contains it, and (b) is the largest such subset for which a single
52// instruction could match all members.
53//
54// For register classes, we can generate these subgroups automatically. For
55// arbitrary operands, we expect the user to define the classes and their
56// relations to one another (for example, 8-bit signed immediates as a
57// subset of 32-bit immediates).
58//
59// By partitioning the operands in this way, we guarantee that for any
60// tuple of classes, any single instruction must match either all or none
61// of the sets of operands which could classify to that tuple.
62//
63// In addition, the subset relation amongst classes induces a partial order
64// on such tuples, which we use to resolve ambiguities.
65//
66// FIXME: What do we do if a crazy case shows up where this is the wrong
67// resolution?
68//
69// 2. The input can now be treated as a tuple of classes (static tokens are
70// simple singleton sets). Each such tuple should generally map to a single
71// instruction (we currently ignore cases where this isn't true, whee!!!),
72// which we can emit a simple matcher for.
73//
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000074//===----------------------------------------------------------------------===//
75
76#include "AsmMatcherEmitter.h"
77#include "CodeGenTarget.h"
78#include "Record.h"
Chris Lattner5845e5c2010-09-06 02:01:51 +000079#include "StringMatcher.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +000080#include "llvm/ADT/OwningPtr.h"
Chris Lattner1de88232010-11-01 01:47:07 +000081#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000082#include "llvm/ADT/SmallVector.h"
Daniel Dunbar606e8ad2009-08-09 04:00:06 +000083#include "llvm/ADT/STLExtras.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +000084#include "llvm/ADT/StringExtras.h"
85#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000086#include "llvm/Support/Debug.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000087#include <list>
Daniel Dunbarb7479c02009-08-08 05:24:34 +000088#include <map>
89#include <set>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000090using namespace llvm;
91
Daniel Dunbar27249152009-08-07 20:33:39 +000092static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +000093MatchPrefix("match-prefix", cl::init(""),
94 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +000095
Daniel Dunbara027d222009-07-31 02:32:59 +000096/// TokenizeAsmString - Tokenize a simplified assembly string.
Jim Grosbacha7c78222010-10-29 22:13:48 +000097static void TokenizeAsmString(StringRef AsmString,
Daniel Dunbara027d222009-07-31 02:32:59 +000098 SmallVectorImpl<StringRef> &Tokens) {
99 unsigned Prev = 0;
100 bool InTok = true;
101 for (unsigned i = 0, e = AsmString.size(); i != e; ++i) {
102 switch (AsmString[i]) {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000103 case '[':
104 case ']':
Daniel Dunbara027d222009-07-31 02:32:59 +0000105 case '*':
106 case '!':
107 case ' ':
108 case '\t':
109 case ',':
110 if (InTok) {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000111 Tokens.push_back(AsmString.slice(Prev, i));
Daniel Dunbara027d222009-07-31 02:32:59 +0000112 InTok = false;
113 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000114 if (!isspace(AsmString[i]) && AsmString[i] != ',')
115 Tokens.push_back(AsmString.substr(i, 1));
Daniel Dunbara027d222009-07-31 02:32:59 +0000116 Prev = i + 1;
117 break;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000118
Daniel Dunbar20927f22009-08-07 08:26:05 +0000119 case '\\':
120 if (InTok) {
121 Tokens.push_back(AsmString.slice(Prev, i));
122 InTok = false;
123 }
124 ++i;
125 assert(i != AsmString.size() && "Invalid quoted character");
126 Tokens.push_back(AsmString.substr(i, 1));
127 Prev = i + 1;
128 break;
129
130 case '$': {
131 // If this isn't "${", treat like a normal token.
132 if (i + 1 == AsmString.size() || AsmString[i + 1] != '{') {
133 if (InTok) {
134 Tokens.push_back(AsmString.slice(Prev, i));
135 InTok = false;
136 }
137 Prev = i;
138 break;
139 }
140
141 if (InTok) {
142 Tokens.push_back(AsmString.slice(Prev, i));
143 InTok = false;
144 }
145
146 StringRef::iterator End =
147 std::find(AsmString.begin() + i, AsmString.end(), '}');
148 assert(End != AsmString.end() && "Missing brace in operand reference!");
149 size_t EndPos = End - AsmString.begin();
150 Tokens.push_back(AsmString.slice(i, EndPos+1));
151 Prev = EndPos + 1;
152 i = EndPos;
153 break;
154 }
Daniel Dunbara027d222009-07-31 02:32:59 +0000155
Daniel Dunbar4d39b672010-08-11 06:36:59 +0000156 case '.':
157 if (InTok) {
158 Tokens.push_back(AsmString.slice(Prev, i));
159 }
160 Prev = i;
161 InTok = true;
162 break;
163
Daniel Dunbara027d222009-07-31 02:32:59 +0000164 default:
165 InTok = true;
166 }
167 }
168 if (InTok && Prev != AsmString.size())
Daniel Dunbar20927f22009-08-07 08:26:05 +0000169 Tokens.push_back(AsmString.substr(Prev));
170}
171
Daniel Dunbar20927f22009-08-07 08:26:05 +0000172
173namespace {
Chris Lattner02bcbc92010-11-01 01:37:30 +0000174 class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000175struct SubtargetFeatureInfo;
176
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000177/// ClassInfo - Helper class for storing the information about a particular
178/// class of operands which can be matched.
179struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000180 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000181 /// Invalid kind, for use as a sentinel value.
182 Invalid = 0,
183
184 /// The class for a particular token.
185 Token,
186
187 /// The (first) register class, subsequent register classes are
188 /// RegisterClass0+1, and so on.
189 RegisterClass0,
190
191 /// The (first) user defined class, subsequent user defined classes are
192 /// UserClass0+1, and so on.
193 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000194 };
195
196 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
197 /// N) for the Nth user defined class.
198 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000199
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000200 /// SuperClasses - The super classes of this class. Note that for simplicities
201 /// sake user operands only record their immediate super class, while register
202 /// operands include all superclasses.
203 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000204
Daniel Dunbar6745d422009-08-09 05:18:30 +0000205 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000206 std::string Name;
207
Daniel Dunbar6745d422009-08-09 05:18:30 +0000208 /// ClassName - The unadorned generic name for this class (e.g., Token).
209 std::string ClassName;
210
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000211 /// ValueName - The name of the value this class represents; for a token this
212 /// is the literal token string, for an operand it is the TableGen class (or
213 /// empty if this is a derived class).
214 std::string ValueName;
215
216 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000217 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000218 std::string PredicateMethod;
219
220 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000221 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000222 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000223
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000224 /// For register classes, the records for all the registers in this class.
225 std::set<Record*> Registers;
226
227public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000228 /// isRegisterClass() - Check if this is a register class.
229 bool isRegisterClass() const {
230 return Kind >= RegisterClass0 && Kind < UserClass0;
231 }
232
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000233 /// isUserClass() - Check if this is a user defined class.
234 bool isUserClass() const {
235 return Kind >= UserClass0;
236 }
237
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000238 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
239 /// are related if they are in the same class hierarchy.
240 bool isRelatedTo(const ClassInfo &RHS) const {
241 // Tokens are only related to tokens.
242 if (Kind == Token || RHS.Kind == Token)
243 return Kind == Token && RHS.Kind == Token;
244
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000245 // Registers classes are only related to registers classes, and only if
246 // their intersection is non-empty.
247 if (isRegisterClass() || RHS.isRegisterClass()) {
248 if (!isRegisterClass() || !RHS.isRegisterClass())
249 return false;
250
251 std::set<Record*> Tmp;
252 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000253 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000254 RHS.Registers.begin(), RHS.Registers.end(),
255 II);
256
257 return !Tmp.empty();
258 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000259
260 // Otherwise we have two users operands; they are related if they are in the
261 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000262 //
263 // FIXME: This is an oversimplification, they should only be related if they
264 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000265 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
266 const ClassInfo *Root = this;
267 while (!Root->SuperClasses.empty())
268 Root = Root->SuperClasses.front();
269
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000270 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000271 while (!RHSRoot->SuperClasses.empty())
272 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000273
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000274 return Root == RHSRoot;
275 }
276
Jim Grosbacha7c78222010-10-29 22:13:48 +0000277 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000278 bool isSubsetOf(const ClassInfo &RHS) const {
279 // This is a subset of RHS if it is the same class...
280 if (this == &RHS)
281 return true;
282
283 // ... or if any of its super classes are a subset of RHS.
284 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
285 ie = SuperClasses.end(); it != ie; ++it)
286 if ((*it)->isSubsetOf(RHS))
287 return true;
288
289 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000290 }
291
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000292 /// operator< - Compare two classes.
293 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000294 if (this == &RHS)
295 return false;
296
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000297 // Unrelated classes can be ordered by kind.
298 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000299 return Kind < RHS.Kind;
300
301 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000302 case Invalid:
303 assert(0 && "Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000304 case Token:
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000305 // Tokens are comparable by value.
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000306 //
307 // FIXME: Compare by enum value.
308 return ValueName < RHS.ValueName;
309
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000310 default:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000311 // This class preceeds the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000312 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000313 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000314 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000315 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000316
317 // Otherwise, order by name to ensure we have a total ordering.
318 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000319 }
320 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000321};
322
Chris Lattner22bc5c42010-11-01 05:06:45 +0000323/// MatchableInfo - Helper class for storing the necessary information for an
324/// instruction or alias which is capable of being matched.
325struct MatchableInfo {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000326 struct Operand {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000327 /// The unique class instance this operand should match.
328 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000329
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000330 /// The original operand this corresponds to, if any.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000331 const CGIOperandList::OperandInfo *OperandInfo;
Chris Lattner4c9f4e42010-11-01 23:08:02 +0000332
333 Operand(ClassInfo *C, const CGIOperandList::OperandInfo *OpInfo)
334 : Class(C), OperandInfo(OpInfo) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000335 };
336
337 /// InstrName - The target name for this instruction.
338 std::string InstrName;
339
Chris Lattner5bc93872010-11-01 04:34:44 +0000340 Record *const TheDef;
341 const CGIOperandList &OperandList;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000342
343 /// AsmString - The assembly string for this instruction (with variants
344 /// removed).
345 std::string AsmString;
346
347 /// Tokens - The tokenized assembly pattern that this instruction matches.
348 SmallVector<StringRef, 4> Tokens;
349
Chris Lattner3116fef2010-11-02 01:03:43 +0000350 /// AsmOperands - The textual operands that this instruction matches,
351 /// including literal tokens for the mnemonic, etc.
352 SmallVector<Operand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000353
Daniel Dunbar54074b52010-07-19 05:44:09 +0000354 /// Predicates - The required subtarget features to match this instruction.
355 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
356
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000357 /// ConversionFnKind - The enum value which is passed to the generated
358 /// ConvertToMCInst to convert parsed operands into an MCInst for this
359 /// function.
360 std::string ConversionFnKind;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000361
Chris Lattner22bc5c42010-11-01 05:06:45 +0000362 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000363 : TheDef(CGI.TheDef), OperandList(CGI.Operands), AsmString(CGI.AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000364 InstrName = TheDef->getName();
Chris Lattner5bc93872010-11-01 04:34:44 +0000365 }
366
Chris Lattner22bc5c42010-11-01 05:06:45 +0000367 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000368 : TheDef(Alias->TheDef), OperandList(Alias->Operands),
369 AsmString(Alias->AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000370
371 // FIXME: Huge hack.
372 DefInit *DI = dynamic_cast<DefInit*>(Alias->Result->getOperator());
373 assert(DI);
374
375 InstrName = DI->getDef()->getName();
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000376 }
377
378 void Initialize(const AsmMatcherInfo &Info,
379 SmallPtrSet<Record*, 16> &SingletonRegisters);
380
Chris Lattner22bc5c42010-11-01 05:06:45 +0000381 /// Validate - Return true if this matchable is a valid thing to match against
382 /// and perform a bunch of validity checking.
383 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Chris Lattner5bc93872010-11-01 04:34:44 +0000384
Chris Lattner02bcbc92010-11-01 01:37:30 +0000385 /// getSingletonRegisterForToken - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000386 /// register, return the Record for it, otherwise return null.
387 Record *getSingletonRegisterForToken(unsigned i,
388 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000389
Chris Lattner22bc5c42010-11-01 05:06:45 +0000390 /// operator< - Compare two matchables.
391 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000392 // The primary comparator is the instruction mnemonic.
393 if (Tokens[0] != RHS.Tokens[0])
394 return Tokens[0] < RHS.Tokens[0];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000395
Chris Lattner3116fef2010-11-02 01:03:43 +0000396 if (AsmOperands.size() != RHS.AsmOperands.size())
397 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000398
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000399 // Compare lexicographically by operand. The matcher validates that other
400 // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000401 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
402 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000403 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000404 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000405 return false;
406 }
407
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000408 return false;
409 }
410
Chris Lattner22bc5c42010-11-01 05:06:45 +0000411 /// CouldMatchAmiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000412 /// ambiguously match the same set of operands as \arg RHS (without being a
413 /// strictly superior match).
Chris Lattner22bc5c42010-11-01 05:06:45 +0000414 bool CouldMatchAmiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000415 // The primary comparator is the instruction mnemonic.
416 if (Tokens[0] != RHS.Tokens[0])
417 return false;
418
Daniel Dunbar2b544812009-08-09 06:05:33 +0000419 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000420 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000421 return false;
422
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000423 // Otherwise, make sure the ordering of the two instructions is unambiguous
424 // by checking that either (a) a token or operand kind discriminates them,
425 // or (b) the ordering among equivalent kinds is consistent.
426
Daniel Dunbar2b544812009-08-09 06:05:33 +0000427 // Tokens and operand kinds are unambiguous (assuming a correct target
428 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000429 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
430 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
431 AsmOperands[i].Class->Kind == ClassInfo::Token)
432 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
433 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000434 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000435
Daniel Dunbar2b544812009-08-09 06:05:33 +0000436 // Otherwise, this operand could commute if all operands are equivalent, or
437 // there is a pair of operands that compare less than and a pair that
438 // compare greater than.
439 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000440 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
441 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000442 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000443 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000444 HasGT = true;
445 }
446
447 return !(HasLT ^ HasGT);
448 }
449
Daniel Dunbar20927f22009-08-07 08:26:05 +0000450 void dump();
451};
452
Daniel Dunbar54074b52010-07-19 05:44:09 +0000453/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
454/// feature which participates in instruction matching.
455struct SubtargetFeatureInfo {
456 /// \brief The predicate record for this feature.
457 Record *TheDef;
458
459 /// \brief An unique index assigned to represent this feature.
460 unsigned Index;
461
Chris Lattner0aed1e72010-10-30 20:07:57 +0000462 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
463
Daniel Dunbar54074b52010-07-19 05:44:09 +0000464 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000465 std::string getEnumName() const {
466 return "Feature_" + TheDef->getName();
467 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000468};
469
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000470class AsmMatcherInfo {
471public:
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000472 /// The tablegen AsmParser record.
473 Record *AsmParser;
474
Chris Lattner02bcbc92010-11-01 01:37:30 +0000475 /// Target - The target information.
476 CodeGenTarget &Target;
477
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000478 /// The AsmParser "RegisterPrefix" value.
479 std::string RegisterPrefix;
480
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000481 /// The classes which are needed for matching.
482 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000483
Chris Lattner22bc5c42010-11-01 05:06:45 +0000484 /// The information on the matchables to match.
485 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000486
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000487 /// Map of Register records to their class information.
488 std::map<Record*, ClassInfo*> RegisterClasses;
489
Daniel Dunbar54074b52010-07-19 05:44:09 +0000490 /// Map of Predicate records to their subtarget information.
491 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Chris Lattner6fa152c2010-10-30 20:15:02 +0000492
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000493private:
494 /// Map of token to class information which has already been constructed.
495 std::map<std::string, ClassInfo*> TokenClasses;
496
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000497 /// Map of RegisterClass records to their class information.
498 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000499
Daniel Dunbar338825c2009-08-10 18:41:10 +0000500 /// Map of AsmOperandClass records to their class information.
501 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000502
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000503private:
504 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000505 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000506
507 /// getOperandClass - Lookup or create the class for the given operand.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000508 ClassInfo *getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000509 const CGIOperandList::OperandInfo &OI);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000510
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000511 /// BuildRegisterClasses - Build the ClassInfo* instances for register
512 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000513 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000514
515 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
516 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000517 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000518
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000519public:
Chris Lattner02bcbc92010-11-01 01:37:30 +0000520 AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000521
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000522 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000523 void BuildInfo();
Chris Lattner6fa152c2010-10-30 20:15:02 +0000524
525 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
526 /// given operand.
527 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
528 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
529 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
530 SubtargetFeatures.find(Def);
531 return I == SubtargetFeatures.end() ? 0 : I->second;
532 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000533};
534
Daniel Dunbar20927f22009-08-07 08:26:05 +0000535}
536
Chris Lattner22bc5c42010-11-01 05:06:45 +0000537void MatchableInfo::dump() {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000538 errs() << InstrName << " -- " << "flattened:\"" << AsmString << '\"'
539 << ", tokens:[";
540 for (unsigned i = 0, e = Tokens.size(); i != e; ++i) {
541 errs() << Tokens[i];
542 if (i + 1 != e)
543 errs() << ", ";
544 }
545 errs() << "]\n";
546
Chris Lattner3116fef2010-11-02 01:03:43 +0000547 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
548 Operand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000549 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000550 if (Op.Class->Kind == ClassInfo::Token) {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000551 errs() << '\"' << Tokens[i] << "\"\n";
552 continue;
553 }
554
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000555 if (!Op.OperandInfo) {
556 errs() << "(singleton register)\n";
557 continue;
558 }
559
Chris Lattnerc240bb02010-11-01 04:03:32 +0000560 const CGIOperandList::OperandInfo &OI = *Op.OperandInfo;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000561 errs() << OI.Name << " " << OI.Rec->getName()
562 << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n";
563 }
564}
565
Chris Lattner22bc5c42010-11-01 05:06:45 +0000566void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
567 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000568 // TODO: Eventually support asmparser for Variant != 0.
569 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
570
571 TokenizeAsmString(AsmString, Tokens);
572
573 // Compute the require features.
574 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
575 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
576 if (SubtargetFeatureInfo *Feature =
577 Info.getSubtargetFeature(Predicates[i]))
578 RequiredFeatures.push_back(Feature);
579
580 // Collect singleton registers, if used.
581 for (unsigned i = 0, e = Tokens.size(); i != e; ++i) {
582 if (Record *Reg = getSingletonRegisterForToken(i, Info))
583 SingletonRegisters.insert(Reg);
584 }
585}
586
587
Chris Lattner02bcbc92010-11-01 01:37:30 +0000588/// getRegisterRecord - Get the register record for \arg name, or 0.
589static Record *getRegisterRecord(CodeGenTarget &Target, StringRef Name) {
590 for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) {
591 const CodeGenRegister &Reg = Target.getRegisters()[i];
592 if (Name == Reg.TheDef->getValueAsString("AsmName"))
593 return Reg.TheDef;
594 }
595
596 return 0;
597}
598
Chris Lattner22bc5c42010-11-01 05:06:45 +0000599bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
600 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000601 if (AsmString.empty())
602 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
603
Chris Lattner22bc5c42010-11-01 05:06:45 +0000604 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000605 // isCodeGenOnly if they are pseudo instructions.
606 if (AsmString.find('\n') != std::string::npos)
607 throw TGError(TheDef->getLoc(),
608 "multiline instruction is not valid for the asmparser, "
609 "mark it isCodeGenOnly");
610
Chris Lattner4164f6b2010-11-01 04:44:29 +0000611 // Remove comments from the asm string. We know that the asmstring only
612 // has one line.
613 if (!CommentDelimiter.empty() &&
614 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
615 throw TGError(TheDef->getLoc(),
616 "asmstring for instruction has comment character in it, "
617 "mark it isCodeGenOnly");
618
Chris Lattner22bc5c42010-11-01 05:06:45 +0000619 // Reject matchables with operand modifiers, these aren't something we can
620 /// handle, the target should be refactored to use operands instead of
621 /// modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000622 //
623 // Also, check for instructions which reference the operand multiple times;
624 // this implies a constraint we would not honor.
625 std::set<std::string> OperandNames;
626 for (unsigned i = 1, e = Tokens.size(); i < e; ++i) {
627 if (Tokens[i][0] == '$' && Tokens[i].find(':') != StringRef::npos)
628 throw TGError(TheDef->getLoc(),
Chris Lattner22bc5c42010-11-01 05:06:45 +0000629 "matchable with operand modifier '" + Tokens[i].str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000630 "' not supported by asm matcher. Mark isCodeGenOnly!");
631
Chris Lattner22bc5c42010-11-01 05:06:45 +0000632 // Verify that any operand is only mentioned once.
Chris Lattner5bc93872010-11-01 04:34:44 +0000633 if (Tokens[i][0] == '$' && !OperandNames.insert(Tokens[i]).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000634 if (!Hack)
635 throw TGError(TheDef->getLoc(),
636 "ERROR: matchable with tied operand '" + Tokens[i].str() +
637 "' can never be matched!");
638 // FIXME: Should reject these. The ARM backend hits this with $lane in a
639 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000640 DEBUG({
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000641 errs() << "warning: '" << InstrName << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000642 << "ignoring instruction with tied operand '"
643 << Tokens[i].str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000644 });
645 return false;
646 }
647 }
648
649 return true;
650}
651
652
Chris Lattner02bcbc92010-11-01 01:37:30 +0000653/// getSingletonRegisterForToken - If the specified token is a singleton
654/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000655Record *MatchableInfo::
Chris Lattner02bcbc92010-11-01 01:37:30 +0000656getSingletonRegisterForToken(unsigned i, const AsmMatcherInfo &Info) const {
657 StringRef Tok = Tokens[i];
658 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000659 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000660
661 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattner1de88232010-11-01 01:47:07 +0000662 if (Record *Rec = getRegisterRecord(Info.Target, RegName))
663 return Rec;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000664
Chris Lattner1de88232010-11-01 01:47:07 +0000665 // If there is no register prefix (i.e. "%" in "%eax"), then this may
666 // be some random non-register token, just ignore it.
667 if (Info.RegisterPrefix.empty())
668 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000669
Chris Lattner1de88232010-11-01 01:47:07 +0000670 std::string Err = "unable to find register for '" + RegName.str() +
671 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000672 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000673}
674
675
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000676static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000677 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000678
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000679 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
680 switch (*it) {
681 case '*': Res += "_STAR_"; break;
682 case '%': Res += "_PCT_"; break;
683 case ':': Res += "_COLON_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000684 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000685 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000686 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000687 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000688 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000689 }
690 }
691
692 return Res;
693}
694
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000695ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000696 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000697
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000698 if (!Entry) {
699 Entry = new ClassInfo();
700 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000701 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000702 Entry->Name = "MCK_" + getEnumNameForToken(Token);
703 Entry->ValueName = Token;
704 Entry->PredicateMethod = "<invalid>";
705 Entry->RenderMethod = "<invalid>";
706 Classes.push_back(Entry);
707 }
708
709 return Entry;
710}
711
712ClassInfo *
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000713AsmMatcherInfo::getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000714 const CGIOperandList::OperandInfo &OI) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000715 if (OI.Rec->isSubClassOf("RegisterClass")) {
716 ClassInfo *CI = RegisterClassClasses[OI.Rec];
717
Chris Lattner4164f6b2010-11-01 04:44:29 +0000718 if (!CI)
719 throw TGError(OI.Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000720
721 return CI;
722 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000723
Daniel Dunbar338825c2009-08-10 18:41:10 +0000724 assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!");
725 Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass");
726 ClassInfo *CI = AsmOperandClasses[MatchClass];
727
Chris Lattner4164f6b2010-11-01 04:44:29 +0000728 if (!CI)
729 throw TGError(OI.Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000730
Daniel Dunbar338825c2009-08-10 18:41:10 +0000731 return CI;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000732}
733
Chris Lattner1de88232010-11-01 01:47:07 +0000734void AsmMatcherInfo::
735BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000736 std::vector<CodeGenRegisterClass> RegisterClasses;
737 std::vector<CodeGenRegister> Registers;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000738
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000739 RegisterClasses = Target.getRegisterClasses();
740 Registers = Target.getRegisters();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000741
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000742 // The register sets used for matching.
743 std::set< std::set<Record*> > RegisterSets;
744
Jim Grosbacha7c78222010-10-29 22:13:48 +0000745 // Gather the defined sets.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000746 for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(),
747 ie = RegisterClasses.end(); it != ie; ++it)
748 RegisterSets.insert(std::set<Record*>(it->Elements.begin(),
749 it->Elements.end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000750
751 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000752 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
753 ie = SingletonRegisters.end(); it != ie; ++it) {
754 Record *Rec = *it;
755 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
756 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000757
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000758 // Introduce derived sets where necessary (when a register does not determine
759 // a unique register set class), and build the mapping of registers to the set
760 // they should classify to.
761 std::map<Record*, std::set<Record*> > RegisterMap;
762 for (std::vector<CodeGenRegister>::iterator it = Registers.begin(),
763 ie = Registers.end(); it != ie; ++it) {
764 CodeGenRegister &CGR = *it;
765 // Compute the intersection of all sets containing this register.
766 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000767
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000768 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
769 ie = RegisterSets.end(); it != ie; ++it) {
770 if (!it->count(CGR.TheDef))
771 continue;
772
773 if (ContainingSet.empty()) {
774 ContainingSet = *it;
775 } else {
776 std::set<Record*> Tmp;
777 std::swap(Tmp, ContainingSet);
778 std::insert_iterator< std::set<Record*> > II(ContainingSet,
779 ContainingSet.begin());
780 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(),
781 II);
782 }
783 }
784
785 if (!ContainingSet.empty()) {
786 RegisterSets.insert(ContainingSet);
787 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
788 }
789 }
790
791 // Construct the register classes.
792 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
793 unsigned Index = 0;
794 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
795 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
796 ClassInfo *CI = new ClassInfo();
797 CI->Kind = ClassInfo::RegisterClass0 + Index;
798 CI->ClassName = "Reg" + utostr(Index);
799 CI->Name = "MCK_Reg" + utostr(Index);
800 CI->ValueName = "";
801 CI->PredicateMethod = ""; // unused
802 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000803 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000804 Classes.push_back(CI);
805 RegisterSetClasses.insert(std::make_pair(*it, CI));
806 }
807
808 // Find the superclasses; we could compute only the subgroup lattice edges,
809 // but there isn't really a point.
810 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
811 ie = RegisterSets.end(); it != ie; ++it) {
812 ClassInfo *CI = RegisterSetClasses[*it];
813 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
814 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000815 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000816 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
817 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
818 }
819
820 // Name the register classes which correspond to a user defined RegisterClass.
821 for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(),
822 ie = RegisterClasses.end(); it != ie; ++it) {
823 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(),
824 it->Elements.end())];
825 if (CI->ValueName.empty()) {
826 CI->ClassName = it->getName();
827 CI->Name = "MCK_" + it->getName();
828 CI->ValueName = it->getName();
829 } else
830 CI->ValueName = CI->ValueName + "," + it->getName();
831
832 RegisterClassClasses.insert(std::make_pair(it->TheDef, CI));
833 }
834
835 // Populate the map for individual registers.
836 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
837 ie = RegisterMap.end(); it != ie; ++it)
838 this->RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000839
840 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +0000841 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
842 ie = SingletonRegisters.end(); it != ie; ++it) {
843 Record *Rec = *it;
844 ClassInfo *CI = this->RegisterClasses[Rec];
845 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000846
Chris Lattner1de88232010-11-01 01:47:07 +0000847 if (CI->ValueName.empty()) {
848 CI->ClassName = Rec->getName();
849 CI->Name = "MCK_" + Rec->getName();
850 CI->ValueName = Rec->getName();
851 } else
852 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000853 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000854}
855
Chris Lattner02bcbc92010-11-01 01:37:30 +0000856void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000857 std::vector<Record*> AsmOperands =
858 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000859
860 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +0000861 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000862 ie = AsmOperands.end(); it != ie; ++it)
863 AsmOperandClasses[*it] = new ClassInfo();
864
Daniel Dunbar338825c2009-08-10 18:41:10 +0000865 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000866 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +0000867 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000868 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +0000869 CI->Kind = ClassInfo::UserClass0 + Index;
870
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +0000871 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
872 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
873 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
874 if (!DI) {
875 PrintError((*it)->getLoc(), "Invalid super class reference!");
876 continue;
877 }
878
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000879 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
880 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +0000881 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000882 else
883 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +0000884 }
885 CI->ClassName = (*it)->getValueAsString("Name");
886 CI->Name = "MCK_" + CI->ClassName;
887 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000888
889 // Get or construct the predicate method name.
890 Init *PMName = (*it)->getValueInit("PredicateMethod");
891 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
892 CI->PredicateMethod = SI->getValue();
893 } else {
Jim Grosbacha7c78222010-10-29 22:13:48 +0000894 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000895 "Unexpected PredicateMethod field!");
896 CI->PredicateMethod = "is" + CI->ClassName;
897 }
898
899 // Get or construct the render method name.
900 Init *RMName = (*it)->getValueInit("RenderMethod");
901 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
902 CI->RenderMethod = SI->getValue();
903 } else {
904 assert(dynamic_cast<UnsetInit*>(RMName) &&
905 "Unexpected RenderMethod field!");
906 CI->RenderMethod = "add" + CI->ClassName + "Operands";
907 }
908
Daniel Dunbar338825c2009-08-10 18:41:10 +0000909 AsmOperandClasses[*it] = CI;
910 Classes.push_back(CI);
911 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000912}
913
Chris Lattner02bcbc92010-11-01 01:37:30 +0000914AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target)
915 : AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000916 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000917}
918
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000919
Chris Lattner02bcbc92010-11-01 01:37:30 +0000920void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +0000921 // Build information about all of the AssemblerPredicates.
922 std::vector<Record*> AllPredicates =
923 Records.getAllDerivedDefinitions("Predicate");
924 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
925 Record *Pred = AllPredicates[i];
926 // Ignore predicates that are not intended for the assembler.
927 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
928 continue;
929
Chris Lattner4164f6b2010-11-01 04:44:29 +0000930 if (Pred->getName().empty())
931 throw TGError(Pred->getLoc(), "Predicate has no name!");
Chris Lattner0aed1e72010-10-30 20:07:57 +0000932
933 unsigned FeatureNo = SubtargetFeatures.size();
934 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
935 assert(FeatureNo < 32 && "Too many subtarget features!");
936 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000937
Chris Lattner4164f6b2010-11-01 04:44:29 +0000938 StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
939
Chris Lattner39ee0362010-10-31 19:10:56 +0000940 // Parse the instructions; we need to do this first so that we can gather the
941 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000942 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000943 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
944 E = Target.inst_end(); I != E; ++I) {
945 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000946
Chris Lattner39ee0362010-10-31 19:10:56 +0000947 // If the tblgen -match-prefix option is specified (for tblgen hackers),
948 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000949 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000950 continue;
951
Chris Lattner5bc93872010-11-01 04:34:44 +0000952 // Ignore "codegen only" instructions.
953 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
954 continue;
955
Chris Lattner22bc5c42010-11-01 05:06:45 +0000956 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000957
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000958 II->Initialize(*this, SingletonRegisters);
959
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000960 // Ignore instructions which shouldn't be matched and diagnose invalid
961 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000962 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +0000963 continue;
964
965 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
966 //
967 // FIXME: This is a total hack.
968 if (StringRef(II->InstrName).startswith("Int_") ||
969 StringRef(II->InstrName).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000970 continue;
Chris Lattner39ee0362010-10-31 19:10:56 +0000971
Chris Lattner22bc5c42010-11-01 05:06:45 +0000972 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000973 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000974
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000975 // Parse all of the InstAlias definitions and stick them in the list of
976 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000977 std::vector<Record*> AllInstAliases =
978 Records.getAllDerivedDefinitions("InstAlias");
979 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
980 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i]);
981
Chris Lattner22bc5c42010-11-01 05:06:45 +0000982 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000983
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000984 II->Initialize(*this, SingletonRegisters);
985
Chris Lattner22bc5c42010-11-01 05:06:45 +0000986 // Validate the alias definitions.
987 II->Validate(CommentDelimiter, false);
988
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000989 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000990 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000991
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000992 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000993 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000994
995 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000996 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000997
Chris Lattner22bc5c42010-11-01 05:06:45 +0000998 // Build the information about matchables.
999 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1000 ie = Matchables.end(); it != ie; ++it) {
1001 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001002
Chris Lattnere206fcf2010-09-06 21:01:37 +00001003 // The first token of the instruction is the mnemonic, which must be a
Chris Lattner02bcbc92010-11-01 01:37:30 +00001004 // simple string, not a $foo variable or a singleton register.
Chris Lattnere206fcf2010-09-06 21:01:37 +00001005 assert(!II->Tokens.empty() && "Instruction has no tokens?");
1006 StringRef Mnemonic = II->Tokens[0];
Chris Lattner1de88232010-11-01 01:47:07 +00001007 if (Mnemonic[0] == '$' || II->getSingletonRegisterForToken(0, *this))
Chris Lattner5bc93872010-11-01 04:34:44 +00001008 throw TGError(II->TheDef->getLoc(),
Chris Lattner02bcbc92010-11-01 01:37:30 +00001009 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Jim Grosbacha7c78222010-10-29 22:13:48 +00001010
Chris Lattnere206fcf2010-09-06 21:01:37 +00001011 // Parse the tokens after the mnemonic.
1012 for (unsigned i = 1, e = II->Tokens.size(); i != e; ++i) {
Daniel Dunbar20927f22009-08-07 08:26:05 +00001013 StringRef Token = II->Tokens[i];
1014
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001015 // Check for singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001016 if (Record *RegRecord = II->getSingletonRegisterForToken(i, *this)) {
Chris Lattner4c9f4e42010-11-01 23:08:02 +00001017 MatchableInfo::Operand Op(RegisterClasses[RegRecord], 0);
Chris Lattner02bcbc92010-11-01 01:37:30 +00001018 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1019 "Unexpected class for singleton register");
Chris Lattner3116fef2010-11-02 01:03:43 +00001020 II->AsmOperands.push_back(Op);
Chris Lattner02bcbc92010-11-01 01:37:30 +00001021 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001022 }
1023
Daniel Dunbar20927f22009-08-07 08:26:05 +00001024 // Check for simple tokens.
1025 if (Token[0] != '$') {
Chris Lattner3116fef2010-11-02 01:03:43 +00001026 II->AsmOperands.push_back(MatchableInfo::Operand(getTokenClass(Token),
1027 0));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001028 continue;
1029 }
1030
1031 // Otherwise this is an operand reference.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001032 StringRef OperandName;
1033 if (Token[1] == '{')
1034 OperandName = Token.substr(2, Token.size() - 3);
1035 else
1036 OperandName = Token.substr(1);
1037
1038 // Map this token to an operand. FIXME: Move elsewhere.
1039 unsigned Idx;
Chris Lattner5bc93872010-11-01 04:34:44 +00001040 if (!II->OperandList.hasOperandNamed(OperandName, Idx))
Chris Lattner4164f6b2010-11-01 04:44:29 +00001041 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1042 OperandName.str() + "'");
Daniel Dunbar20927f22009-08-07 08:26:05 +00001043
Daniel Dunbaraf616812010-02-10 08:15:48 +00001044 // FIXME: This is annoying, the named operand may be tied (e.g.,
1045 // XCHG8rm). What we want is the untied operand, which we now have to
1046 // grovel for. Only worry about this for single entry operands, we have to
1047 // clean this up anyway.
Chris Lattner5bc93872010-11-01 04:34:44 +00001048 const CGIOperandList::OperandInfo *OI = &II->OperandList[Idx];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001049 if (OI->Constraints[0].isTied()) {
1050 unsigned TiedOp = OI->Constraints[0].getTiedOperand();
1051
1052 // The tied operand index is an MIOperand index, find the operand that
1053 // contains it.
Chris Lattner5bc93872010-11-01 04:34:44 +00001054 for (unsigned i = 0, e = II->OperandList.size(); i != e; ++i) {
1055 if (II->OperandList[i].MIOperandNo == TiedOp) {
1056 OI = &II->OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001057 break;
1058 }
1059 }
1060
1061 assert(OI && "Unable to find tied operand target!");
1062 }
1063
Chris Lattner3116fef2010-11-02 01:03:43 +00001064 II->AsmOperands.push_back(MatchableInfo::Operand(getOperandClass(Token,
Chris Lattner4c9f4e42010-11-01 23:08:02 +00001065 *OI), OI));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001066 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001067 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001068
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001069 // Reorder classes so that classes preceed super classes.
1070 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001071}
1072
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001073static std::pair<unsigned, unsigned> *
1074GetTiedOperandAtIndex(SmallVectorImpl<std::pair<unsigned, unsigned> > &List,
1075 unsigned Index) {
1076 for (unsigned i = 0, e = List.size(); i != e; ++i)
1077 if (Index == List[i].first)
1078 return &List[i];
1079
1080 return 0;
1081}
1082
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001083static void EmitConvertToMCInst(CodeGenTarget &Target,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001084 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001085 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001086 // Write the convert function to a separate stream, so we can drop it after
1087 // the enum.
1088 std::string ConvertFnBody;
1089 raw_string_ostream CvtOS(ConvertFnBody);
1090
Daniel Dunbar20927f22009-08-07 08:26:05 +00001091 // Function we have already generated.
1092 std::set<std::string> GeneratedFns;
1093
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001094 // Start the unified conversion function.
1095
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001096 CvtOS << "static void ConvertToMCInst(ConversionKind Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001097 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001098 << " const SmallVectorImpl<MCParsedAsmOperand*"
1099 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001100 CvtOS << " Inst.setOpcode(Opcode);\n";
1101 CvtOS << " switch (Kind) {\n";
1102 CvtOS << " default:\n";
1103
1104 // Start the enum, which we will generate inline.
1105
1106 OS << "// Unified function for converting operants to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001107 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001108
Chris Lattner98986712010-01-14 22:21:20 +00001109 // TargetOperandClass - This is the target's operand class, like X86Operand.
1110 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001111
Chris Lattner22bc5c42010-11-01 05:06:45 +00001112 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001113 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001114 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001115
1116 // Order the (class) operands by the order to convert them into an MCInst.
1117 SmallVector<std::pair<unsigned, unsigned>, 4> MIOperandList;
Chris Lattner3116fef2010-11-02 01:03:43 +00001118 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1119 MatchableInfo::Operand &Op = II.AsmOperands[i];
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001120 if (Op.OperandInfo)
1121 MIOperandList.push_back(std::make_pair(Op.OperandInfo->MIOperandNo, i));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001122 }
Daniel Dunbaraf616812010-02-10 08:15:48 +00001123
1124 // Find any tied operands.
1125 SmallVector<std::pair<unsigned, unsigned>, 4> TiedOperands;
Chris Lattner5bc93872010-11-01 04:34:44 +00001126 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1127 const CGIOperandList::OperandInfo &OpInfo = II.OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001128 for (unsigned j = 0, e = OpInfo.Constraints.size(); j != e; ++j) {
Chris Lattnerc240bb02010-11-01 04:03:32 +00001129 const CGIOperandList::ConstraintInfo &CI = OpInfo.Constraints[j];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001130 if (CI.isTied())
1131 TiedOperands.push_back(std::make_pair(OpInfo.MIOperandNo + j,
1132 CI.getTiedOperand()));
1133 }
1134 }
1135
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001136 array_pod_sort(MIOperandList.begin(), MIOperandList.end());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001137
1138 // Compute the total number of operands.
1139 unsigned NumMIOperands = 0;
Chris Lattner5bc93872010-11-01 04:34:44 +00001140 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1141 const CGIOperandList::OperandInfo &OI = II.OperandList[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001142 NumMIOperands = std::max(NumMIOperands,
Daniel Dunbar20927f22009-08-07 08:26:05 +00001143 OI.MIOperandNo + OI.MINumOperands);
1144 }
1145
1146 // Build the conversion function signature.
1147 std::string Signature = "Convert";
1148 unsigned CurIndex = 0;
1149 for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) {
Chris Lattner3116fef2010-11-02 01:03:43 +00001150 MatchableInfo::Operand &Op = II.AsmOperands[MIOperandList[i].second];
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001151 assert(CurIndex <= Op.OperandInfo->MIOperandNo &&
Daniel Dunbar20927f22009-08-07 08:26:05 +00001152 "Duplicate match for instruction operand!");
Jim Grosbacha7c78222010-10-29 22:13:48 +00001153
Daniel Dunbar20927f22009-08-07 08:26:05 +00001154 // Skip operands which weren't matched by anything, this occurs when the
1155 // .td file encodes "implicit" operands as explicit ones.
1156 //
1157 // FIXME: This should be removed from the MCInst structure.
Daniel Dunbaraf616812010-02-10 08:15:48 +00001158 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) {
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001159 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1160 CurIndex);
1161 if (!Tie)
Daniel Dunbaraf616812010-02-10 08:15:48 +00001162 Signature += "__Imp";
1163 else
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001164 Signature += "__Tie" + utostr(Tie->second);
Daniel Dunbaraf616812010-02-10 08:15:48 +00001165 }
1166
1167 Signature += "__";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001168
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001169 // Registers are always converted the same, don't duplicate the conversion
1170 // function based on them.
1171 //
1172 // FIXME: We could generalize this based on the render method, if it
1173 // mattered.
1174 if (Op.Class->isRegisterClass())
1175 Signature += "Reg";
1176 else
1177 Signature += Op.Class->ClassName;
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001178 Signature += utostr(Op.OperandInfo->MINumOperands);
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001179 Signature += "_" + utostr(MIOperandList[i].second);
1180
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001181 CurIndex += Op.OperandInfo->MINumOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001182 }
1183
1184 // Add any trailing implicit operands.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001185 for (; CurIndex != NumMIOperands; ++CurIndex) {
1186 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1187 CurIndex);
1188 if (!Tie)
1189 Signature += "__Imp";
1190 else
1191 Signature += "__Tie" + utostr(Tie->second);
1192 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001193
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001194 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001195
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001196 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001197 if (!GeneratedFns.insert(Signature).second)
1198 continue;
1199
1200 // If not, emit it now.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001201
1202 // Add to the enum list.
1203 OS << " " << Signature << ",\n";
1204
1205 // And to the convert function.
1206 CvtOS << " case " << Signature << ":\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001207 CurIndex = 0;
1208 for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) {
Chris Lattner3116fef2010-11-02 01:03:43 +00001209 MatchableInfo::Operand &Op = II.AsmOperands[MIOperandList[i].second];
Daniel Dunbar20927f22009-08-07 08:26:05 +00001210
1211 // Add the implicit operands.
Daniel Dunbaraf616812010-02-10 08:15:48 +00001212 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) {
1213 // See if this is a tied operand.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001214 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1215 CurIndex);
Daniel Dunbaraf616812010-02-10 08:15:48 +00001216
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001217 if (!Tie) {
Daniel Dunbaraf616812010-02-10 08:15:48 +00001218 // If not, this is some implicit operand. Just assume it is a register
1219 // for now.
1220 CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1221 } else {
1222 // Copy the tied operand.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001223 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
Daniel Dunbaraf616812010-02-10 08:15:48 +00001224 CvtOS << " Inst.addOperand(Inst.getOperand("
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001225 << Tie->second << "));\n";
Daniel Dunbaraf616812010-02-10 08:15:48 +00001226 }
1227 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001228
Chris Lattner98986712010-01-14 22:21:20 +00001229 CvtOS << " ((" << TargetOperandClass << "*)Operands["
Jim Grosbacha7c78222010-10-29 22:13:48 +00001230 << MIOperandList[i].second
1231 << "+1])->" << Op.Class->RenderMethod
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001232 << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n";
1233 CurIndex += Op.OperandInfo->MINumOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001234 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001235
Daniel Dunbar20927f22009-08-07 08:26:05 +00001236 // And add trailing implicit operands.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001237 for (; CurIndex != NumMIOperands; ++CurIndex) {
1238 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1239 CurIndex);
1240
1241 if (!Tie) {
1242 // If not, this is some implicit operand. Just assume it is a register
1243 // for now.
1244 CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1245 } else {
1246 // Copy the tied operand.
1247 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
1248 CvtOS << " Inst.addOperand(Inst.getOperand("
1249 << Tie->second << "));\n";
1250 }
1251 }
1252
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001253 CvtOS << " return;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001254 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001255
1256 // Finish the convert function.
1257
1258 CvtOS << " }\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001259 CvtOS << "}\n\n";
1260
1261 // Finish the enum, and drop the convert function after it.
1262
1263 OS << " NumConversionVariants\n";
1264 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001265
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001266 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001267}
1268
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001269/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1270static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1271 std::vector<ClassInfo*> &Infos,
1272 raw_ostream &OS) {
1273 OS << "namespace {\n\n";
1274
1275 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1276 << "/// instruction matching.\n";
1277 OS << "enum MatchClassKind {\n";
1278 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001279 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001280 ie = Infos.end(); it != ie; ++it) {
1281 ClassInfo &CI = **it;
1282 OS << " " << CI.Name << ", // ";
1283 if (CI.Kind == ClassInfo::Token) {
1284 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001285 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001286 if (!CI.ValueName.empty())
1287 OS << "register class '" << CI.ValueName << "'\n";
1288 else
1289 OS << "derived register class\n";
1290 } else {
1291 OS << "user defined class '" << CI.ValueName << "'\n";
1292 }
1293 }
1294 OS << " NumMatchClassKinds\n";
1295 OS << "};\n\n";
1296
1297 OS << "}\n\n";
1298}
1299
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001300/// EmitClassifyOperand - Emit the function to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001301static void EmitClassifyOperand(AsmMatcherInfo &Info,
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001302 raw_ostream &OS) {
Chris Lattner98986712010-01-14 22:21:20 +00001303 OS << "static MatchClassKind ClassifyOperand(MCParsedAsmOperand *GOp) {\n"
Chris Lattner02bcbc92010-11-01 01:37:30 +00001304 << " " << Info.Target.getName() << "Operand &Operand = *("
1305 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001306
1307 // Classify tokens.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001308 OS << " if (Operand.isToken())\n";
1309 OS << " return MatchTokenString(Operand.getToken());\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001310
1311 // Classify registers.
1312 //
1313 // FIXME: Don't hardcode isReg, getReg.
1314 OS << " if (Operand.isReg()) {\n";
1315 OS << " switch (Operand.getReg()) {\n";
1316 OS << " default: return InvalidMatchClass;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001317 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001318 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1319 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001320 OS << " case " << Info.Target.getName() << "::"
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001321 << it->first->getName() << ": return " << it->second->Name << ";\n";
1322 OS << " }\n";
1323 OS << " }\n\n";
1324
1325 // Classify user defined operands.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001326 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001327 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001328 ClassInfo &CI = **it;
1329
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001330 if (!CI.isUserClass())
1331 continue;
1332
1333 OS << " // '" << CI.ClassName << "' class";
1334 if (!CI.SuperClasses.empty()) {
1335 OS << ", subclass of ";
1336 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) {
1337 if (i) OS << ", ";
1338 OS << "'" << CI.SuperClasses[i]->ClassName << "'";
1339 assert(CI < *CI.SuperClasses[i] && "Invalid class relation!");
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001340 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001341 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001342 OS << "\n";
1343
1344 OS << " if (Operand." << CI.PredicateMethod << "()) {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001345
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001346 // Validate subclass relationships.
1347 if (!CI.SuperClasses.empty()) {
1348 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i)
1349 OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod
1350 << "() && \"Invalid class relationship!\");\n";
1351 }
1352
1353 OS << " return " << CI.Name << ";\n";
1354 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001355 }
1356 OS << " return InvalidMatchClass;\n";
1357 OS << "}\n\n";
1358}
1359
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001360/// EmitIsSubclass - Emit the subclass predicate function.
1361static void EmitIsSubclass(CodeGenTarget &Target,
1362 std::vector<ClassInfo*> &Infos,
1363 raw_ostream &OS) {
1364 OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1365 OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n";
1366 OS << " if (A == B)\n";
1367 OS << " return true;\n\n";
1368
1369 OS << " switch (A) {\n";
1370 OS << " default:\n";
1371 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001372 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001373 ie = Infos.end(); it != ie; ++it) {
1374 ClassInfo &A = **it;
1375
1376 if (A.Kind != ClassInfo::Token) {
1377 std::vector<StringRef> SuperClasses;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001378 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001379 ie = Infos.end(); it != ie; ++it) {
1380 ClassInfo &B = **it;
1381
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001382 if (&A != &B && A.isSubsetOf(B))
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001383 SuperClasses.push_back(B.Name);
1384 }
1385
1386 if (SuperClasses.empty())
1387 continue;
1388
1389 OS << "\n case " << A.Name << ":\n";
1390
1391 if (SuperClasses.size() == 1) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001392 OS << " return B == " << SuperClasses.back() << ";\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001393 continue;
1394 }
1395
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001396 OS << " switch (B) {\n";
1397 OS << " default: return false;\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001398 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001399 OS << " case " << SuperClasses[i] << ": return true;\n";
1400 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001401 }
1402 }
1403 OS << " }\n";
1404 OS << "}\n\n";
1405}
1406
Chris Lattner70add882009-08-08 20:02:57 +00001407
1408
Daniel Dunbar245f0582009-08-08 21:22:41 +00001409/// EmitMatchTokenString - Emit the function to match a token string to the
1410/// appropriate match class value.
1411static void EmitMatchTokenString(CodeGenTarget &Target,
1412 std::vector<ClassInfo*> &Infos,
1413 raw_ostream &OS) {
1414 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001415 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001416 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001417 ie = Infos.end(); it != ie; ++it) {
1418 ClassInfo &CI = **it;
1419
1420 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001421 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1422 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001423 }
1424
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001425 OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001426
Chris Lattner5845e5c2010-09-06 02:01:51 +00001427 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001428
1429 OS << " return InvalidMatchClass;\n";
1430 OS << "}\n\n";
1431}
Chris Lattner70add882009-08-08 20:02:57 +00001432
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001433/// EmitMatchRegisterName - Emit the function to match a string to the target
1434/// specific register enum.
1435static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1436 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001437 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001438 std::vector<StringMatcher::StringPair> Matches;
Daniel Dunbar245f0582009-08-08 21:22:41 +00001439 for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) {
1440 const CodeGenRegister &Reg = Target.getRegisters()[i];
Daniel Dunbar22be5222009-07-17 18:51:11 +00001441 if (Reg.TheDef->getValueAsString("AsmName").empty())
1442 continue;
1443
Chris Lattner5845e5c2010-09-06 02:01:51 +00001444 Matches.push_back(StringMatcher::StringPair(
1445 Reg.TheDef->getValueAsString("AsmName"),
1446 "return " + utostr(i + 1) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001447 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001448
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001449 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001450
Chris Lattner5845e5c2010-09-06 02:01:51 +00001451 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001452
Daniel Dunbar245f0582009-08-08 21:22:41 +00001453 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001454 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001455}
Daniel Dunbara027d222009-07-31 02:32:59 +00001456
Daniel Dunbar54074b52010-07-19 05:44:09 +00001457/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1458/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001459static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001460 raw_ostream &OS) {
1461 OS << "// Flags for subtarget features that participate in "
1462 << "instruction matching.\n";
1463 OS << "enum SubtargetFeatureFlag {\n";
1464 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1465 it = Info.SubtargetFeatures.begin(),
1466 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1467 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001468 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001469 }
1470 OS << " Feature_None = 0\n";
1471 OS << "};\n\n";
1472}
1473
1474/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1475/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001476static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001477 raw_ostream &OS) {
1478 std::string ClassName =
1479 Info.AsmParser->getValueAsString("AsmParserClassName");
1480
Chris Lattner02bcbc92010-11-01 01:37:30 +00001481 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
1482 << "ComputeAvailableFeatures(const " << Info.Target.getName()
Daniel Dunbar54074b52010-07-19 05:44:09 +00001483 << "Subtarget *Subtarget) const {\n";
1484 OS << " unsigned Features = 0;\n";
1485 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1486 it = Info.SubtargetFeatures.begin(),
1487 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1488 SubtargetFeatureInfo &SFI = *it->second;
1489 OS << " if (" << SFI.TheDef->getValueAsString("CondString")
1490 << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001491 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001492 }
1493 OS << " return Features;\n";
1494 OS << "}\n\n";
1495}
1496
Chris Lattner6fa152c2010-10-30 20:15:02 +00001497static std::string GetAliasRequiredFeatures(Record *R,
1498 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001499 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001500 std::string Result;
1501 unsigned NumFeatures = 0;
1502 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001503 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Chris Lattner693173f2010-10-30 19:23:13 +00001504
Chris Lattner4a74ee72010-11-01 02:09:21 +00001505 if (F == 0)
1506 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1507 "' is not marked as an AssemblerPredicate!");
1508
1509 if (NumFeatures)
1510 Result += '|';
1511
1512 Result += F->getEnumName();
1513 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001514 }
1515
1516 if (NumFeatures > 1)
1517 Result = '(' + Result + ')';
1518 return Result;
1519}
1520
Chris Lattner674c1dc2010-10-30 17:36:36 +00001521/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001522/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001523static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Chris Lattner674c1dc2010-10-30 17:36:36 +00001524 std::vector<Record*> Aliases =
1525 Records.getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001526 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001527
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001528 OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, "
1529 "unsigned Features) {\n";
1530
Chris Lattner4fd32c62010-10-30 18:56:12 +00001531 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1532 // iteration order of the map is stable.
1533 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
1534
Chris Lattner674c1dc2010-10-30 17:36:36 +00001535 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1536 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001537 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001538 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001539
1540 // Process each alias a "from" mnemonic at a time, building the code executed
1541 // by the string remapper.
1542 std::vector<StringMatcher::StringPair> Cases;
1543 for (std::map<std::string, std::vector<Record*> >::iterator
1544 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1545 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001546 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001547
1548 // Loop through each alias and emit code that handles each case. If there
1549 // are two instructions without predicates, emit an error. If there is one,
1550 // emit it last.
1551 std::string MatchCode;
1552 int AliasWithNoPredicate = -1;
Chris Lattner4fd32c62010-10-30 18:56:12 +00001553
Chris Lattner693173f2010-10-30 19:23:13 +00001554 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1555 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001556 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Chris Lattner693173f2010-10-30 19:23:13 +00001557
1558 // If this unconditionally matches, remember it for later and diagnose
1559 // duplicates.
1560 if (FeatureMask.empty()) {
1561 if (AliasWithNoPredicate != -1) {
1562 // We can't have two aliases from the same mnemonic with no predicate.
1563 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1564 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001565 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001566 }
1567
1568 AliasWithNoPredicate = i;
1569 continue;
1570 }
1571
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001572 if (!MatchCode.empty())
1573 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001574 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1575 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001576 }
1577
Chris Lattner693173f2010-10-30 19:23:13 +00001578 if (AliasWithNoPredicate != -1) {
1579 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001580 if (!MatchCode.empty())
1581 MatchCode += "else\n ";
1582 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001583 }
1584
1585 MatchCode += "return;";
1586
1587 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001588 }
1589
Chris Lattner674c1dc2010-10-30 17:36:36 +00001590
1591 StringMatcher("Mnemonic", Cases, OS).Emit();
Chris Lattner7fd44892010-10-30 18:48:18 +00001592 OS << "}\n";
1593
1594 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001595}
1596
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001597void AsmMatcherEmitter::run(raw_ostream &OS) {
1598 CodeGenTarget Target;
1599 Record *AsmParser = Target.getAsmParser();
1600 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
1601
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001602 // Compute the information on the instructions to match.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001603 AsmMatcherInfo Info(AsmParser, Target);
1604 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00001605
Daniel Dunbare1f6de32010-02-02 23:46:36 +00001606 // Sort the instruction table using the partial order on classes. We use
1607 // stable_sort to ensure that ambiguous instructions are still
1608 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001609 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
1610 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001611
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001612 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001613 for (std::vector<MatchableInfo*>::iterator
1614 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001615 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00001616 (*it)->dump();
1617 });
Daniel Dunbara027d222009-07-31 02:32:59 +00001618
Chris Lattner22bc5c42010-11-01 05:06:45 +00001619 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001620 DEBUG_WITH_TYPE("ambiguous_instrs", {
1621 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001622 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00001623 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001624 MatchableInfo &A = *Info.Matchables[i];
1625 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001626
Chris Lattner87410362010-09-06 20:21:47 +00001627 if (A.CouldMatchAmiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001628 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001629 A.dump();
1630 errs() << "\nis incomparable with:\n";
1631 B.dump();
1632 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00001633 ++NumAmbiguous;
1634 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00001635 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001636 }
Chris Lattner87410362010-09-06 20:21:47 +00001637 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001638 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00001639 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001640 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001641
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001642 // Write the output.
1643
1644 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
1645
Chris Lattner0692ee62010-09-06 19:11:01 +00001646 // Information for the class declaration.
1647 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
1648 OS << "#undef GET_ASSEMBLER_HEADER\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001649 OS << " // This should be included into the middle of the declaration of \n";
1650 OS << " // your subclasses implementation of TargetAsmParser.\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001651 OS << " unsigned ComputeAvailableFeatures(const " <<
1652 Target.getName() << "Subtarget *Subtarget) const;\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001653 OS << " enum MatchResultTy {\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001654 OS << " Match_Success, Match_MnemonicFail, Match_InvalidOperand,\n";
1655 OS << " Match_MissingFeature\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001656 OS << " };\n";
Jim Grosbachbb168242010-10-08 18:13:57 +00001657 OS << " MatchResultTy MatchInstructionImpl(const "
1658 << "SmallVectorImpl<MCParsedAsmOperand*>"
Chris Lattnerce4a3352010-09-06 22:11:18 +00001659 << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001660 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
1661
Jim Grosbacha7c78222010-10-29 22:13:48 +00001662
1663
1664
Chris Lattner0692ee62010-09-06 19:11:01 +00001665 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
1666 OS << "#undef GET_REGISTER_MATCHER\n\n";
1667
Daniel Dunbar54074b52010-07-19 05:44:09 +00001668 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001669 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001670
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001671 // Emit the function to match a register name to number.
1672 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00001673
1674 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001675
Chris Lattner0692ee62010-09-06 19:11:01 +00001676
1677 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
1678 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001679
Chris Lattner7fd44892010-10-30 18:48:18 +00001680 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001681 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Chris Lattner7fd44892010-10-30 18:48:18 +00001682
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001683 // Generate the unified function to convert operands into an MCInst.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001684 EmitConvertToMCInst(Target, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001685
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001686 // Emit the enumeration for classes which participate in matching.
1687 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001688
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001689 // Emit the routine to match token strings to their match class.
1690 EmitMatchTokenString(Target, Info.Classes, OS);
1691
1692 // Emit the routine to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001693 EmitClassifyOperand(Info, OS);
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001694
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001695 // Emit the subclass predicate routine.
1696 EmitIsSubclass(Target, Info.Classes, OS);
1697
Daniel Dunbar54074b52010-07-19 05:44:09 +00001698 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001699 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001700
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001701
1702 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001703 for (std::vector<MatchableInfo*>::const_iterator it =
1704 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001705 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00001706 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001707
1708
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001709 // Emit the static match table; unused classes get initalized to 0 which is
1710 // guaranteed to be InvalidMatchClass.
1711 //
1712 // FIXME: We can reduce the size of this table very easily. First, we change
1713 // it so that store the kinds in separate bit-fields for each index, which
1714 // only needs to be the max width used for classes at that index (we also need
1715 // to reject based on this during classification). If we then make sure to
1716 // order the match kinds appropriately (putting mnemonics last), then we
1717 // should only end up using a few bits for each class, especially the ones
1718 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00001719 OS << "namespace {\n";
1720 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001721 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001722 OS << " const char *Mnemonic;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001723 OS << " ConversionKind ConvertFn;\n";
1724 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001725 OS << " unsigned RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001726 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001727
Chris Lattner2b1f9432010-09-06 21:22:45 +00001728 OS << "// Predicate for searching for an opcode.\n";
1729 OS << " struct LessOpcode {\n";
1730 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
1731 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
1732 OS << " }\n";
1733 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
1734 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
1735 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00001736 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
1737 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
1738 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00001739 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001740
Chris Lattner96352e52010-09-06 21:08:38 +00001741 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001742
Chris Lattner96352e52010-09-06 21:08:38 +00001743 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00001744 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001745
Chris Lattner22bc5c42010-11-01 05:06:45 +00001746 for (std::vector<MatchableInfo*>::const_iterator it =
1747 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001748 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001749 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001750
Chris Lattner96352e52010-09-06 21:08:38 +00001751 OS << " { " << Target.getName() << "::" << II.InstrName
1752 << ", \"" << II.Tokens[0] << "\""
1753 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00001754 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1755 MatchableInfo::Operand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001756
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001757 if (i) OS << ", ";
1758 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001759 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00001760 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001761
Daniel Dunbar54074b52010-07-19 05:44:09 +00001762 // Write the required features mask.
1763 if (!II.RequiredFeatures.empty()) {
1764 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
1765 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001766 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00001767 }
1768 } else
1769 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001770
Daniel Dunbar54074b52010-07-19 05:44:09 +00001771 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001772 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001773
Chris Lattner96352e52010-09-06 21:08:38 +00001774 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001775
Chris Lattner96352e52010-09-06 21:08:38 +00001776 // Finally, build the match function.
1777 OS << Target.getName() << ClassName << "::MatchResultTy "
1778 << Target.getName() << ClassName << "::\n"
1779 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
1780 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001781 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001782
1783 // Emit code to get the available features.
1784 OS << " // Get the current feature set.\n";
1785 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
1786
Chris Lattner674c1dc2010-10-30 17:36:36 +00001787 OS << " // Get the instruction mnemonic, which is the first token.\n";
1788 OS << " StringRef Mnemonic = ((" << Target.getName()
1789 << "Operand*)Operands[0])->getToken();\n\n";
1790
Chris Lattner7fd44892010-10-30 18:48:18 +00001791 if (HasMnemonicAliases) {
1792 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
1793 OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
1794 }
Chris Lattner674c1dc2010-10-30 17:36:36 +00001795
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001796 // Emit code to compute the class list for this operand vector.
1797 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001798 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
1799 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
1800 OS << " return Match_InvalidOperand;\n";
1801 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001802
1803 OS << " // Compute the class list for this operand vector.\n";
1804 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001805 OS << " for (unsigned i = 1, e = Operands.size(); i != e; ++i) {\n";
1806 OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001807
1808 OS << " // Check for invalid operands before matching.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001809 OS << " if (Classes[i-1] == InvalidMatchClass) {\n";
1810 OS << " ErrorInfo = i;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001811 OS << " return Match_InvalidOperand;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001812 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001813 OS << " }\n\n";
1814
1815 OS << " // Mark unused classes.\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001816 OS << " for (unsigned i = Operands.size()-1, e = " << MaxNumOperands << "; "
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001817 << "i != e; ++i)\n";
1818 OS << " Classes[i] = InvalidMatchClass;\n\n";
1819
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001820 OS << " // Some state to try to produce better error messages.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001821 OS << " bool HadMatchOtherThanFeatures = false;\n\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001822 OS << " // Set ErrorInfo to the operand that mismatches if it is \n";
1823 OS << " // wrong for all instances of the instruction.\n";
1824 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001825
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001826 // Emit code to search the table.
1827 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001828 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
1829 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00001830 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001831
Chris Lattnera008e8a2010-09-06 21:54:15 +00001832 OS << " // Return a more specific error code if no mnemonics match.\n";
1833 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
1834 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001835
Chris Lattner2b1f9432010-09-06 21:22:45 +00001836 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00001837 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001838 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001839
Gabor Greife53ee3b2010-09-07 06:06:06 +00001840 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00001841 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001842
Daniel Dunbar54074b52010-07-19 05:44:09 +00001843 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001844 OS << " bool OperandsValid = true;\n";
1845 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
1846 OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n";
1847 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001848 OS << " // If this operand is broken for all of the instances of this\n";
1849 OS << " // mnemonic, keep track of it so we can report loc info.\n";
1850 OS << " if (it == MnemonicRange.first || ErrorInfo == i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001851 OS << " ErrorInfo = i+1;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001852 OS << " else\n";
1853 OS << " ErrorInfo = ~0U;";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001854 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
1855 OS << " OperandsValid = false;\n";
1856 OS << " break;\n";
1857 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001858
Chris Lattnerce4a3352010-09-06 22:11:18 +00001859 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00001860
1861 // Emit check that the required features are available.
1862 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
1863 << "!= it->RequiredFeatures) {\n";
1864 OS << " HadMatchOtherThanFeatures = true;\n";
1865 OS << " continue;\n";
1866 OS << " }\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001867
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001868 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001869 OS << " ConvertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
1870
1871 // Call the post-processing function, if used.
1872 std::string InsnCleanupFn =
1873 AsmParser->getValueAsString("AsmParserInstCleanup");
1874 if (!InsnCleanupFn.empty())
1875 OS << " " << InsnCleanupFn << "(Inst);\n";
1876
Chris Lattner79ed3f72010-09-06 19:22:17 +00001877 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001878 OS << " }\n\n";
1879
Chris Lattnerec6789f2010-09-06 20:08:02 +00001880 OS << " // Okay, we had no match. Try to return a useful error code.\n";
1881 OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001882 OS << " return Match_InvalidOperand;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001883 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001884
Chris Lattner0692ee62010-09-06 19:11:01 +00001885 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001886}