blob: f422106ef5642189486908d33288bd4c4c10469c [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;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000332 };
333
334 /// InstrName - The target name for this instruction.
335 std::string InstrName;
336
Chris Lattner5bc93872010-11-01 04:34:44 +0000337 Record *const TheDef;
338 const CGIOperandList &OperandList;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000339
340 /// AsmString - The assembly string for this instruction (with variants
341 /// removed).
342 std::string AsmString;
343
344 /// Tokens - The tokenized assembly pattern that this instruction matches.
345 SmallVector<StringRef, 4> Tokens;
346
347 /// Operands - The operands that this instruction matches.
348 SmallVector<Operand, 4> Operands;
349
Daniel Dunbar54074b52010-07-19 05:44:09 +0000350 /// Predicates - The required subtarget features to match this instruction.
351 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
352
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000353 /// ConversionFnKind - The enum value which is passed to the generated
354 /// ConvertToMCInst to convert parsed operands into an MCInst for this
355 /// function.
356 std::string ConversionFnKind;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000357
Chris Lattner22bc5c42010-11-01 05:06:45 +0000358 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000359 : TheDef(CGI.TheDef), OperandList(CGI.Operands), AsmString(CGI.AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000360 InstrName = TheDef->getName();
Chris Lattner5bc93872010-11-01 04:34:44 +0000361 }
362
Chris Lattner22bc5c42010-11-01 05:06:45 +0000363 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000364 : TheDef(Alias->TheDef), OperandList(Alias->Operands),
365 AsmString(Alias->AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000366
367 // FIXME: Huge hack.
368 DefInit *DI = dynamic_cast<DefInit*>(Alias->Result->getOperator());
369 assert(DI);
370
371 InstrName = DI->getDef()->getName();
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000372 }
373
374 void Initialize(const AsmMatcherInfo &Info,
375 SmallPtrSet<Record*, 16> &SingletonRegisters);
376
Chris Lattner22bc5c42010-11-01 05:06:45 +0000377 /// Validate - Return true if this matchable is a valid thing to match against
378 /// and perform a bunch of validity checking.
379 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Chris Lattner5bc93872010-11-01 04:34:44 +0000380
Chris Lattner02bcbc92010-11-01 01:37:30 +0000381 /// getSingletonRegisterForToken - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000382 /// register, return the Record for it, otherwise return null.
383 Record *getSingletonRegisterForToken(unsigned i,
384 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000385
Chris Lattner22bc5c42010-11-01 05:06:45 +0000386 /// operator< - Compare two matchables.
387 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000388 // The primary comparator is the instruction mnemonic.
389 if (Tokens[0] != RHS.Tokens[0])
390 return Tokens[0] < RHS.Tokens[0];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000391
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000392 if (Operands.size() != RHS.Operands.size())
393 return Operands.size() < RHS.Operands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000394
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000395 // Compare lexicographically by operand. The matcher validates that other
396 // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith().
397 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000398 if (*Operands[i].Class < *RHS.Operands[i].Class)
399 return true;
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000400 if (*RHS.Operands[i].Class < *Operands[i].Class)
401 return false;
402 }
403
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000404 return false;
405 }
406
Chris Lattner22bc5c42010-11-01 05:06:45 +0000407 /// CouldMatchAmiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000408 /// ambiguously match the same set of operands as \arg RHS (without being a
409 /// strictly superior match).
Chris Lattner22bc5c42010-11-01 05:06:45 +0000410 bool CouldMatchAmiguouslyWith(const MatchableInfo &RHS) {
Daniel Dunbar2b544812009-08-09 06:05:33 +0000411 // The number of operands is unambiguous.
412 if (Operands.size() != RHS.Operands.size())
413 return false;
414
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000415 // Otherwise, make sure the ordering of the two instructions is unambiguous
416 // by checking that either (a) a token or operand kind discriminates them,
417 // or (b) the ordering among equivalent kinds is consistent.
418
Daniel Dunbar2b544812009-08-09 06:05:33 +0000419 // Tokens and operand kinds are unambiguous (assuming a correct target
420 // specific parser).
421 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
422 if (Operands[i].Class->Kind != RHS.Operands[i].Class->Kind ||
423 Operands[i].Class->Kind == ClassInfo::Token)
424 if (*Operands[i].Class < *RHS.Operands[i].Class ||
425 *RHS.Operands[i].Class < *Operands[i].Class)
426 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000427
Daniel Dunbar2b544812009-08-09 06:05:33 +0000428 // Otherwise, this operand could commute if all operands are equivalent, or
429 // there is a pair of operands that compare less than and a pair that
430 // compare greater than.
431 bool HasLT = false, HasGT = false;
432 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
433 if (*Operands[i].Class < *RHS.Operands[i].Class)
434 HasLT = true;
435 if (*RHS.Operands[i].Class < *Operands[i].Class)
436 HasGT = true;
437 }
438
439 return !(HasLT ^ HasGT);
440 }
441
Daniel Dunbar20927f22009-08-07 08:26:05 +0000442 void dump();
443};
444
Daniel Dunbar54074b52010-07-19 05:44:09 +0000445/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
446/// feature which participates in instruction matching.
447struct SubtargetFeatureInfo {
448 /// \brief The predicate record for this feature.
449 Record *TheDef;
450
451 /// \brief An unique index assigned to represent this feature.
452 unsigned Index;
453
Chris Lattner0aed1e72010-10-30 20:07:57 +0000454 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
455
Daniel Dunbar54074b52010-07-19 05:44:09 +0000456 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000457 std::string getEnumName() const {
458 return "Feature_" + TheDef->getName();
459 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000460};
461
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000462class AsmMatcherInfo {
463public:
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000464 /// The tablegen AsmParser record.
465 Record *AsmParser;
466
Chris Lattner02bcbc92010-11-01 01:37:30 +0000467 /// Target - The target information.
468 CodeGenTarget &Target;
469
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000470 /// The AsmParser "RegisterPrefix" value.
471 std::string RegisterPrefix;
472
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000473 /// The classes which are needed for matching.
474 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000475
Chris Lattner22bc5c42010-11-01 05:06:45 +0000476 /// The information on the matchables to match.
477 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000478
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000479 /// Map of Register records to their class information.
480 std::map<Record*, ClassInfo*> RegisterClasses;
481
Daniel Dunbar54074b52010-07-19 05:44:09 +0000482 /// Map of Predicate records to their subtarget information.
483 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Chris Lattner6fa152c2010-10-30 20:15:02 +0000484
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000485private:
486 /// Map of token to class information which has already been constructed.
487 std::map<std::string, ClassInfo*> TokenClasses;
488
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000489 /// Map of RegisterClass records to their class information.
490 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000491
Daniel Dunbar338825c2009-08-10 18:41:10 +0000492 /// Map of AsmOperandClass records to their class information.
493 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000494
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000495private:
496 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000497 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000498
499 /// getOperandClass - Lookup or create the class for the given operand.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000500 ClassInfo *getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000501 const CGIOperandList::OperandInfo &OI);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000502
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000503 /// BuildRegisterClasses - Build the ClassInfo* instances for register
504 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000505 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000506
507 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
508 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000509 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000510
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000511public:
Chris Lattner02bcbc92010-11-01 01:37:30 +0000512 AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000513
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000514 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000515 void BuildInfo();
Chris Lattner6fa152c2010-10-30 20:15:02 +0000516
517 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
518 /// given operand.
519 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
520 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
521 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
522 SubtargetFeatures.find(Def);
523 return I == SubtargetFeatures.end() ? 0 : I->second;
524 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000525};
526
Daniel Dunbar20927f22009-08-07 08:26:05 +0000527}
528
Chris Lattner22bc5c42010-11-01 05:06:45 +0000529void MatchableInfo::dump() {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000530 errs() << InstrName << " -- " << "flattened:\"" << AsmString << '\"'
531 << ", tokens:[";
532 for (unsigned i = 0, e = Tokens.size(); i != e; ++i) {
533 errs() << Tokens[i];
534 if (i + 1 != e)
535 errs() << ", ";
536 }
537 errs() << "]\n";
538
539 for (unsigned i = 0, e = Operands.size(); i != e; ++i) {
540 Operand &Op = Operands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000541 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000542 if (Op.Class->Kind == ClassInfo::Token) {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000543 errs() << '\"' << Tokens[i] << "\"\n";
544 continue;
545 }
546
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000547 if (!Op.OperandInfo) {
548 errs() << "(singleton register)\n";
549 continue;
550 }
551
Chris Lattnerc240bb02010-11-01 04:03:32 +0000552 const CGIOperandList::OperandInfo &OI = *Op.OperandInfo;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000553 errs() << OI.Name << " " << OI.Rec->getName()
554 << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n";
555 }
556}
557
Chris Lattner22bc5c42010-11-01 05:06:45 +0000558void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
559 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000560 // TODO: Eventually support asmparser for Variant != 0.
561 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
562
563 TokenizeAsmString(AsmString, Tokens);
564
565 // Compute the require features.
566 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
567 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
568 if (SubtargetFeatureInfo *Feature =
569 Info.getSubtargetFeature(Predicates[i]))
570 RequiredFeatures.push_back(Feature);
571
572 // Collect singleton registers, if used.
573 for (unsigned i = 0, e = Tokens.size(); i != e; ++i) {
574 if (Record *Reg = getSingletonRegisterForToken(i, Info))
575 SingletonRegisters.insert(Reg);
576 }
577}
578
579
Chris Lattner02bcbc92010-11-01 01:37:30 +0000580/// getRegisterRecord - Get the register record for \arg name, or 0.
581static Record *getRegisterRecord(CodeGenTarget &Target, StringRef Name) {
582 for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) {
583 const CodeGenRegister &Reg = Target.getRegisters()[i];
584 if (Name == Reg.TheDef->getValueAsString("AsmName"))
585 return Reg.TheDef;
586 }
587
588 return 0;
589}
590
Chris Lattner22bc5c42010-11-01 05:06:45 +0000591bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
592 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000593 if (AsmString.empty())
594 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
595
Chris Lattner22bc5c42010-11-01 05:06:45 +0000596 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000597 // isCodeGenOnly if they are pseudo instructions.
598 if (AsmString.find('\n') != std::string::npos)
599 throw TGError(TheDef->getLoc(),
600 "multiline instruction is not valid for the asmparser, "
601 "mark it isCodeGenOnly");
602
Chris Lattner4164f6b2010-11-01 04:44:29 +0000603 // Remove comments from the asm string. We know that the asmstring only
604 // has one line.
605 if (!CommentDelimiter.empty() &&
606 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
607 throw TGError(TheDef->getLoc(),
608 "asmstring for instruction has comment character in it, "
609 "mark it isCodeGenOnly");
610
Chris Lattner22bc5c42010-11-01 05:06:45 +0000611 // Reject matchables with operand modifiers, these aren't something we can
612 /// handle, the target should be refactored to use operands instead of
613 /// modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000614 //
615 // Also, check for instructions which reference the operand multiple times;
616 // this implies a constraint we would not honor.
617 std::set<std::string> OperandNames;
618 for (unsigned i = 1, e = Tokens.size(); i < e; ++i) {
619 if (Tokens[i][0] == '$' && Tokens[i].find(':') != StringRef::npos)
620 throw TGError(TheDef->getLoc(),
Chris Lattner22bc5c42010-11-01 05:06:45 +0000621 "matchable with operand modifier '" + Tokens[i].str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000622 "' not supported by asm matcher. Mark isCodeGenOnly!");
623
Chris Lattner22bc5c42010-11-01 05:06:45 +0000624 // Verify that any operand is only mentioned once.
Chris Lattner5bc93872010-11-01 04:34:44 +0000625 if (Tokens[i][0] == '$' && !OperandNames.insert(Tokens[i]).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000626 if (!Hack)
627 throw TGError(TheDef->getLoc(),
628 "ERROR: matchable with tied operand '" + Tokens[i].str() +
629 "' can never be matched!");
630 // FIXME: Should reject these. The ARM backend hits this with $lane in a
631 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000632 DEBUG({
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000633 errs() << "warning: '" << InstrName << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000634 << "ignoring instruction with tied operand '"
635 << Tokens[i].str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000636 });
637 return false;
638 }
639 }
640
641 return true;
642}
643
644
Chris Lattner02bcbc92010-11-01 01:37:30 +0000645/// getSingletonRegisterForToken - If the specified token is a singleton
646/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000647Record *MatchableInfo::
Chris Lattner02bcbc92010-11-01 01:37:30 +0000648getSingletonRegisterForToken(unsigned i, const AsmMatcherInfo &Info) const {
649 StringRef Tok = Tokens[i];
650 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000651 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000652
653 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattner1de88232010-11-01 01:47:07 +0000654 if (Record *Rec = getRegisterRecord(Info.Target, RegName))
655 return Rec;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000656
Chris Lattner1de88232010-11-01 01:47:07 +0000657 // If there is no register prefix (i.e. "%" in "%eax"), then this may
658 // be some random non-register token, just ignore it.
659 if (Info.RegisterPrefix.empty())
660 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000661
Chris Lattner1de88232010-11-01 01:47:07 +0000662 std::string Err = "unable to find register for '" + RegName.str() +
663 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000664 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000665}
666
667
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000668static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000669 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000670
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000671 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
672 switch (*it) {
673 case '*': Res += "_STAR_"; break;
674 case '%': Res += "_PCT_"; break;
675 case ':': Res += "_COLON_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000676 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000677 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000678 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000679 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000680 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000681 }
682 }
683
684 return Res;
685}
686
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000687ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000688 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000689
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000690 if (!Entry) {
691 Entry = new ClassInfo();
692 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000693 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000694 Entry->Name = "MCK_" + getEnumNameForToken(Token);
695 Entry->ValueName = Token;
696 Entry->PredicateMethod = "<invalid>";
697 Entry->RenderMethod = "<invalid>";
698 Classes.push_back(Entry);
699 }
700
701 return Entry;
702}
703
704ClassInfo *
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000705AsmMatcherInfo::getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000706 const CGIOperandList::OperandInfo &OI) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000707 if (OI.Rec->isSubClassOf("RegisterClass")) {
708 ClassInfo *CI = RegisterClassClasses[OI.Rec];
709
Chris Lattner4164f6b2010-11-01 04:44:29 +0000710 if (!CI)
711 throw TGError(OI.Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000712
713 return CI;
714 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000715
Daniel Dunbar338825c2009-08-10 18:41:10 +0000716 assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!");
717 Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass");
718 ClassInfo *CI = AsmOperandClasses[MatchClass];
719
Chris Lattner4164f6b2010-11-01 04:44:29 +0000720 if (!CI)
721 throw TGError(OI.Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000722
Daniel Dunbar338825c2009-08-10 18:41:10 +0000723 return CI;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000724}
725
Chris Lattner1de88232010-11-01 01:47:07 +0000726void AsmMatcherInfo::
727BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000728 std::vector<CodeGenRegisterClass> RegisterClasses;
729 std::vector<CodeGenRegister> Registers;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000730
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000731 RegisterClasses = Target.getRegisterClasses();
732 Registers = Target.getRegisters();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000733
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000734 // The register sets used for matching.
735 std::set< std::set<Record*> > RegisterSets;
736
Jim Grosbacha7c78222010-10-29 22:13:48 +0000737 // Gather the defined sets.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000738 for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(),
739 ie = RegisterClasses.end(); it != ie; ++it)
740 RegisterSets.insert(std::set<Record*>(it->Elements.begin(),
741 it->Elements.end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000742
743 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000744 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
745 ie = SingletonRegisters.end(); it != ie; ++it) {
746 Record *Rec = *it;
747 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
748 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000749
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000750 // Introduce derived sets where necessary (when a register does not determine
751 // a unique register set class), and build the mapping of registers to the set
752 // they should classify to.
753 std::map<Record*, std::set<Record*> > RegisterMap;
754 for (std::vector<CodeGenRegister>::iterator it = Registers.begin(),
755 ie = Registers.end(); it != ie; ++it) {
756 CodeGenRegister &CGR = *it;
757 // Compute the intersection of all sets containing this register.
758 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000759
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000760 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
761 ie = RegisterSets.end(); it != ie; ++it) {
762 if (!it->count(CGR.TheDef))
763 continue;
764
765 if (ContainingSet.empty()) {
766 ContainingSet = *it;
767 } else {
768 std::set<Record*> Tmp;
769 std::swap(Tmp, ContainingSet);
770 std::insert_iterator< std::set<Record*> > II(ContainingSet,
771 ContainingSet.begin());
772 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(),
773 II);
774 }
775 }
776
777 if (!ContainingSet.empty()) {
778 RegisterSets.insert(ContainingSet);
779 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
780 }
781 }
782
783 // Construct the register classes.
784 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
785 unsigned Index = 0;
786 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
787 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
788 ClassInfo *CI = new ClassInfo();
789 CI->Kind = ClassInfo::RegisterClass0 + Index;
790 CI->ClassName = "Reg" + utostr(Index);
791 CI->Name = "MCK_Reg" + utostr(Index);
792 CI->ValueName = "";
793 CI->PredicateMethod = ""; // unused
794 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000795 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000796 Classes.push_back(CI);
797 RegisterSetClasses.insert(std::make_pair(*it, CI));
798 }
799
800 // Find the superclasses; we could compute only the subgroup lattice edges,
801 // but there isn't really a point.
802 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
803 ie = RegisterSets.end(); it != ie; ++it) {
804 ClassInfo *CI = RegisterSetClasses[*it];
805 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
806 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000807 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000808 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
809 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
810 }
811
812 // Name the register classes which correspond to a user defined RegisterClass.
813 for (std::vector<CodeGenRegisterClass>::iterator it = RegisterClasses.begin(),
814 ie = RegisterClasses.end(); it != ie; ++it) {
815 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(),
816 it->Elements.end())];
817 if (CI->ValueName.empty()) {
818 CI->ClassName = it->getName();
819 CI->Name = "MCK_" + it->getName();
820 CI->ValueName = it->getName();
821 } else
822 CI->ValueName = CI->ValueName + "," + it->getName();
823
824 RegisterClassClasses.insert(std::make_pair(it->TheDef, CI));
825 }
826
827 // Populate the map for individual registers.
828 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
829 ie = RegisterMap.end(); it != ie; ++it)
830 this->RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000831
832 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +0000833 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
834 ie = SingletonRegisters.end(); it != ie; ++it) {
835 Record *Rec = *it;
836 ClassInfo *CI = this->RegisterClasses[Rec];
837 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000838
Chris Lattner1de88232010-11-01 01:47:07 +0000839 if (CI->ValueName.empty()) {
840 CI->ClassName = Rec->getName();
841 CI->Name = "MCK_" + Rec->getName();
842 CI->ValueName = Rec->getName();
843 } else
844 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000845 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000846}
847
Chris Lattner02bcbc92010-11-01 01:37:30 +0000848void AsmMatcherInfo::BuildOperandClasses() {
Daniel Dunbar338825c2009-08-10 18:41:10 +0000849 std::vector<Record*> AsmOperands;
850 AsmOperands = Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000851
852 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +0000853 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000854 ie = AsmOperands.end(); it != ie; ++it)
855 AsmOperandClasses[*it] = new ClassInfo();
856
Daniel Dunbar338825c2009-08-10 18:41:10 +0000857 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000858 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +0000859 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000860 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +0000861 CI->Kind = ClassInfo::UserClass0 + Index;
862
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +0000863 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
864 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
865 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
866 if (!DI) {
867 PrintError((*it)->getLoc(), "Invalid super class reference!");
868 continue;
869 }
870
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000871 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
872 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +0000873 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000874 else
875 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +0000876 }
877 CI->ClassName = (*it)->getValueAsString("Name");
878 CI->Name = "MCK_" + CI->ClassName;
879 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000880
881 // Get or construct the predicate method name.
882 Init *PMName = (*it)->getValueInit("PredicateMethod");
883 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
884 CI->PredicateMethod = SI->getValue();
885 } else {
Jim Grosbacha7c78222010-10-29 22:13:48 +0000886 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000887 "Unexpected PredicateMethod field!");
888 CI->PredicateMethod = "is" + CI->ClassName;
889 }
890
891 // Get or construct the render method name.
892 Init *RMName = (*it)->getValueInit("RenderMethod");
893 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
894 CI->RenderMethod = SI->getValue();
895 } else {
896 assert(dynamic_cast<UnsetInit*>(RMName) &&
897 "Unexpected RenderMethod field!");
898 CI->RenderMethod = "add" + CI->ClassName + "Operands";
899 }
900
Daniel Dunbar338825c2009-08-10 18:41:10 +0000901 AsmOperandClasses[*it] = CI;
902 Classes.push_back(CI);
903 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000904}
905
Chris Lattner02bcbc92010-11-01 01:37:30 +0000906AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target)
907 : AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000908 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000909}
910
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000911
Chris Lattner02bcbc92010-11-01 01:37:30 +0000912void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +0000913 // Build information about all of the AssemblerPredicates.
914 std::vector<Record*> AllPredicates =
915 Records.getAllDerivedDefinitions("Predicate");
916 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
917 Record *Pred = AllPredicates[i];
918 // Ignore predicates that are not intended for the assembler.
919 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
920 continue;
921
Chris Lattner4164f6b2010-11-01 04:44:29 +0000922 if (Pred->getName().empty())
923 throw TGError(Pred->getLoc(), "Predicate has no name!");
Chris Lattner0aed1e72010-10-30 20:07:57 +0000924
925 unsigned FeatureNo = SubtargetFeatures.size();
926 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
927 assert(FeatureNo < 32 && "Too many subtarget features!");
928 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000929
Chris Lattner4164f6b2010-11-01 04:44:29 +0000930 StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
931
Chris Lattner39ee0362010-10-31 19:10:56 +0000932 // Parse the instructions; we need to do this first so that we can gather the
933 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000934 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000935 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
936 E = Target.inst_end(); I != E; ++I) {
937 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000938
Chris Lattner39ee0362010-10-31 19:10:56 +0000939 // If the tblgen -match-prefix option is specified (for tblgen hackers),
940 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000941 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000942 continue;
943
Chris Lattner5bc93872010-11-01 04:34:44 +0000944 // Ignore "codegen only" instructions.
945 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
946 continue;
947
Chris Lattner22bc5c42010-11-01 05:06:45 +0000948 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000949
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000950 II->Initialize(*this, SingletonRegisters);
951
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000952 // Ignore instructions which shouldn't be matched and diagnose invalid
953 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000954 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +0000955 continue;
956
957 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
958 //
959 // FIXME: This is a total hack.
960 if (StringRef(II->InstrName).startswith("Int_") ||
961 StringRef(II->InstrName).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000962 continue;
Chris Lattner39ee0362010-10-31 19:10:56 +0000963
Chris Lattner22bc5c42010-11-01 05:06:45 +0000964 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000965 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000966
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000967 // Parse all of the InstAlias definitions and stick them in the list of
968 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000969 std::vector<Record*> AllInstAliases =
970 Records.getAllDerivedDefinitions("InstAlias");
971 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
972 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i]);
973
Chris Lattner22bc5c42010-11-01 05:06:45 +0000974 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000975
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000976 II->Initialize(*this, SingletonRegisters);
977
Chris Lattner22bc5c42010-11-01 05:06:45 +0000978 // Validate the alias definitions.
979 II->Validate(CommentDelimiter, false);
980
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000981 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000982 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000983
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000984 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000985 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000986
987 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000988 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000989
Chris Lattner22bc5c42010-11-01 05:06:45 +0000990 // Build the information about matchables.
991 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
992 ie = Matchables.end(); it != ie; ++it) {
993 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000994
Chris Lattnere206fcf2010-09-06 21:01:37 +0000995 // The first token of the instruction is the mnemonic, which must be a
Chris Lattner02bcbc92010-11-01 01:37:30 +0000996 // simple string, not a $foo variable or a singleton register.
Chris Lattnere206fcf2010-09-06 21:01:37 +0000997 assert(!II->Tokens.empty() && "Instruction has no tokens?");
998 StringRef Mnemonic = II->Tokens[0];
Chris Lattner1de88232010-11-01 01:47:07 +0000999 if (Mnemonic[0] == '$' || II->getSingletonRegisterForToken(0, *this))
Chris Lattner5bc93872010-11-01 04:34:44 +00001000 throw TGError(II->TheDef->getLoc(),
Chris Lattner02bcbc92010-11-01 01:37:30 +00001001 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Jim Grosbacha7c78222010-10-29 22:13:48 +00001002
Chris Lattnere206fcf2010-09-06 21:01:37 +00001003 // Parse the tokens after the mnemonic.
1004 for (unsigned i = 1, e = II->Tokens.size(); i != e; ++i) {
Daniel Dunbar20927f22009-08-07 08:26:05 +00001005 StringRef Token = II->Tokens[i];
1006
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001007 // Check for singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001008 if (Record *RegRecord = II->getSingletonRegisterForToken(i, *this)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001009 MatchableInfo::Operand Op;
Chris Lattner02bcbc92010-11-01 01:37:30 +00001010 Op.Class = RegisterClasses[RegRecord];
1011 Op.OperandInfo = 0;
1012 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1013 "Unexpected class for singleton register");
1014 II->Operands.push_back(Op);
1015 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001016 }
1017
Daniel Dunbar20927f22009-08-07 08:26:05 +00001018 // Check for simple tokens.
1019 if (Token[0] != '$') {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001020 MatchableInfo::Operand Op;
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001021 Op.Class = getTokenClass(Token);
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001022 Op.OperandInfo = 0;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001023 II->Operands.push_back(Op);
1024 continue;
1025 }
1026
1027 // Otherwise this is an operand reference.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001028 StringRef OperandName;
1029 if (Token[1] == '{')
1030 OperandName = Token.substr(2, Token.size() - 3);
1031 else
1032 OperandName = Token.substr(1);
1033
1034 // Map this token to an operand. FIXME: Move elsewhere.
1035 unsigned Idx;
Chris Lattner5bc93872010-11-01 04:34:44 +00001036 if (!II->OperandList.hasOperandNamed(OperandName, Idx))
Chris Lattner4164f6b2010-11-01 04:44:29 +00001037 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1038 OperandName.str() + "'");
Daniel Dunbar20927f22009-08-07 08:26:05 +00001039
Daniel Dunbaraf616812010-02-10 08:15:48 +00001040 // FIXME: This is annoying, the named operand may be tied (e.g.,
1041 // XCHG8rm). What we want is the untied operand, which we now have to
1042 // grovel for. Only worry about this for single entry operands, we have to
1043 // clean this up anyway.
Chris Lattner5bc93872010-11-01 04:34:44 +00001044 const CGIOperandList::OperandInfo *OI = &II->OperandList[Idx];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001045 if (OI->Constraints[0].isTied()) {
1046 unsigned TiedOp = OI->Constraints[0].getTiedOperand();
1047
1048 // The tied operand index is an MIOperand index, find the operand that
1049 // contains it.
Chris Lattner5bc93872010-11-01 04:34:44 +00001050 for (unsigned i = 0, e = II->OperandList.size(); i != e; ++i) {
1051 if (II->OperandList[i].MIOperandNo == TiedOp) {
1052 OI = &II->OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001053 break;
1054 }
1055 }
1056
1057 assert(OI && "Unable to find tied operand target!");
1058 }
1059
Chris Lattner22bc5c42010-11-01 05:06:45 +00001060 MatchableInfo::Operand Op;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001061 Op.Class = getOperandClass(Token, *OI);
1062 Op.OperandInfo = OI;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001063 II->Operands.push_back(Op);
1064 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001065 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001066
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001067 // Reorder classes so that classes preceed super classes.
1068 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001069}
1070
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001071static std::pair<unsigned, unsigned> *
1072GetTiedOperandAtIndex(SmallVectorImpl<std::pair<unsigned, unsigned> > &List,
1073 unsigned Index) {
1074 for (unsigned i = 0, e = List.size(); i != e; ++i)
1075 if (Index == List[i].first)
1076 return &List[i];
1077
1078 return 0;
1079}
1080
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001081static void EmitConvertToMCInst(CodeGenTarget &Target,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001082 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001083 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001084 // Write the convert function to a separate stream, so we can drop it after
1085 // the enum.
1086 std::string ConvertFnBody;
1087 raw_string_ostream CvtOS(ConvertFnBody);
1088
Daniel Dunbar20927f22009-08-07 08:26:05 +00001089 // Function we have already generated.
1090 std::set<std::string> GeneratedFns;
1091
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001092 // Start the unified conversion function.
1093
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001094 CvtOS << "static void ConvertToMCInst(ConversionKind Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001095 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001096 << " const SmallVectorImpl<MCParsedAsmOperand*"
1097 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001098 CvtOS << " Inst.setOpcode(Opcode);\n";
1099 CvtOS << " switch (Kind) {\n";
1100 CvtOS << " default:\n";
1101
1102 // Start the enum, which we will generate inline.
1103
1104 OS << "// Unified function for converting operants to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001105 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001106
Chris Lattner98986712010-01-14 22:21:20 +00001107 // TargetOperandClass - This is the target's operand class, like X86Operand.
1108 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001109
Chris Lattner22bc5c42010-11-01 05:06:45 +00001110 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001111 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001112 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001113
1114 // Order the (class) operands by the order to convert them into an MCInst.
1115 SmallVector<std::pair<unsigned, unsigned>, 4> MIOperandList;
1116 for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001117 MatchableInfo::Operand &Op = II.Operands[i];
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001118 if (Op.OperandInfo)
1119 MIOperandList.push_back(std::make_pair(Op.OperandInfo->MIOperandNo, i));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001120 }
Daniel Dunbaraf616812010-02-10 08:15:48 +00001121
1122 // Find any tied operands.
1123 SmallVector<std::pair<unsigned, unsigned>, 4> TiedOperands;
Chris Lattner5bc93872010-11-01 04:34:44 +00001124 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1125 const CGIOperandList::OperandInfo &OpInfo = II.OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001126 for (unsigned j = 0, e = OpInfo.Constraints.size(); j != e; ++j) {
Chris Lattnerc240bb02010-11-01 04:03:32 +00001127 const CGIOperandList::ConstraintInfo &CI = OpInfo.Constraints[j];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001128 if (CI.isTied())
1129 TiedOperands.push_back(std::make_pair(OpInfo.MIOperandNo + j,
1130 CI.getTiedOperand()));
1131 }
1132 }
1133
Daniel Dunbar20927f22009-08-07 08:26:05 +00001134 std::sort(MIOperandList.begin(), MIOperandList.end());
1135
1136 // Compute the total number of operands.
1137 unsigned NumMIOperands = 0;
Chris Lattner5bc93872010-11-01 04:34:44 +00001138 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1139 const CGIOperandList::OperandInfo &OI = II.OperandList[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001140 NumMIOperands = std::max(NumMIOperands,
Daniel Dunbar20927f22009-08-07 08:26:05 +00001141 OI.MIOperandNo + OI.MINumOperands);
1142 }
1143
1144 // Build the conversion function signature.
1145 std::string Signature = "Convert";
1146 unsigned CurIndex = 0;
1147 for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001148 MatchableInfo::Operand &Op = II.Operands[MIOperandList[i].second];
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001149 assert(CurIndex <= Op.OperandInfo->MIOperandNo &&
Daniel Dunbar20927f22009-08-07 08:26:05 +00001150 "Duplicate match for instruction operand!");
Jim Grosbacha7c78222010-10-29 22:13:48 +00001151
Daniel Dunbar20927f22009-08-07 08:26:05 +00001152 // Skip operands which weren't matched by anything, this occurs when the
1153 // .td file encodes "implicit" operands as explicit ones.
1154 //
1155 // FIXME: This should be removed from the MCInst structure.
Daniel Dunbaraf616812010-02-10 08:15:48 +00001156 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) {
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001157 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1158 CurIndex);
1159 if (!Tie)
Daniel Dunbaraf616812010-02-10 08:15:48 +00001160 Signature += "__Imp";
1161 else
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001162 Signature += "__Tie" + utostr(Tie->second);
Daniel Dunbaraf616812010-02-10 08:15:48 +00001163 }
1164
1165 Signature += "__";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001166
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001167 // Registers are always converted the same, don't duplicate the conversion
1168 // function based on them.
1169 //
1170 // FIXME: We could generalize this based on the render method, if it
1171 // mattered.
1172 if (Op.Class->isRegisterClass())
1173 Signature += "Reg";
1174 else
1175 Signature += Op.Class->ClassName;
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001176 Signature += utostr(Op.OperandInfo->MINumOperands);
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001177 Signature += "_" + utostr(MIOperandList[i].second);
1178
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001179 CurIndex += Op.OperandInfo->MINumOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001180 }
1181
1182 // Add any trailing implicit operands.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001183 for (; CurIndex != NumMIOperands; ++CurIndex) {
1184 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1185 CurIndex);
1186 if (!Tie)
1187 Signature += "__Imp";
1188 else
1189 Signature += "__Tie" + utostr(Tie->second);
1190 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001191
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001192 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001193
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001194 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001195 if (!GeneratedFns.insert(Signature).second)
1196 continue;
1197
1198 // If not, emit it now.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001199
1200 // Add to the enum list.
1201 OS << " " << Signature << ",\n";
1202
1203 // And to the convert function.
1204 CvtOS << " case " << Signature << ":\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001205 CurIndex = 0;
1206 for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001207 MatchableInfo::Operand &Op = II.Operands[MIOperandList[i].second];
Daniel Dunbar20927f22009-08-07 08:26:05 +00001208
1209 // Add the implicit operands.
Daniel Dunbaraf616812010-02-10 08:15:48 +00001210 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) {
1211 // See if this is a tied operand.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001212 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1213 CurIndex);
Daniel Dunbaraf616812010-02-10 08:15:48 +00001214
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001215 if (!Tie) {
Daniel Dunbaraf616812010-02-10 08:15:48 +00001216 // If not, this is some implicit operand. Just assume it is a register
1217 // for now.
1218 CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1219 } else {
1220 // Copy the tied operand.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001221 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
Daniel Dunbaraf616812010-02-10 08:15:48 +00001222 CvtOS << " Inst.addOperand(Inst.getOperand("
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001223 << Tie->second << "));\n";
Daniel Dunbaraf616812010-02-10 08:15:48 +00001224 }
1225 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001226
Chris Lattner98986712010-01-14 22:21:20 +00001227 CvtOS << " ((" << TargetOperandClass << "*)Operands["
Jim Grosbacha7c78222010-10-29 22:13:48 +00001228 << MIOperandList[i].second
1229 << "+1])->" << Op.Class->RenderMethod
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001230 << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n";
1231 CurIndex += Op.OperandInfo->MINumOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001232 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001233
Daniel Dunbar20927f22009-08-07 08:26:05 +00001234 // And add trailing implicit operands.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001235 for (; CurIndex != NumMIOperands; ++CurIndex) {
1236 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1237 CurIndex);
1238
1239 if (!Tie) {
1240 // If not, this is some implicit operand. Just assume it is a register
1241 // for now.
1242 CvtOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1243 } else {
1244 // Copy the tied operand.
1245 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
1246 CvtOS << " Inst.addOperand(Inst.getOperand("
1247 << Tie->second << "));\n";
1248 }
1249 }
1250
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001251 CvtOS << " return;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001252 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001253
1254 // Finish the convert function.
1255
1256 CvtOS << " }\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001257 CvtOS << "}\n\n";
1258
1259 // Finish the enum, and drop the convert function after it.
1260
1261 OS << " NumConversionVariants\n";
1262 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001263
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001264 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001265}
1266
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001267/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1268static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1269 std::vector<ClassInfo*> &Infos,
1270 raw_ostream &OS) {
1271 OS << "namespace {\n\n";
1272
1273 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1274 << "/// instruction matching.\n";
1275 OS << "enum MatchClassKind {\n";
1276 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001277 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001278 ie = Infos.end(); it != ie; ++it) {
1279 ClassInfo &CI = **it;
1280 OS << " " << CI.Name << ", // ";
1281 if (CI.Kind == ClassInfo::Token) {
1282 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001283 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001284 if (!CI.ValueName.empty())
1285 OS << "register class '" << CI.ValueName << "'\n";
1286 else
1287 OS << "derived register class\n";
1288 } else {
1289 OS << "user defined class '" << CI.ValueName << "'\n";
1290 }
1291 }
1292 OS << " NumMatchClassKinds\n";
1293 OS << "};\n\n";
1294
1295 OS << "}\n\n";
1296}
1297
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001298/// EmitClassifyOperand - Emit the function to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001299static void EmitClassifyOperand(AsmMatcherInfo &Info,
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001300 raw_ostream &OS) {
Chris Lattner98986712010-01-14 22:21:20 +00001301 OS << "static MatchClassKind ClassifyOperand(MCParsedAsmOperand *GOp) {\n"
Chris Lattner02bcbc92010-11-01 01:37:30 +00001302 << " " << Info.Target.getName() << "Operand &Operand = *("
1303 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001304
1305 // Classify tokens.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001306 OS << " if (Operand.isToken())\n";
1307 OS << " return MatchTokenString(Operand.getToken());\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001308
1309 // Classify registers.
1310 //
1311 // FIXME: Don't hardcode isReg, getReg.
1312 OS << " if (Operand.isReg()) {\n";
1313 OS << " switch (Operand.getReg()) {\n";
1314 OS << " default: return InvalidMatchClass;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001315 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001316 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1317 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001318 OS << " case " << Info.Target.getName() << "::"
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001319 << it->first->getName() << ": return " << it->second->Name << ";\n";
1320 OS << " }\n";
1321 OS << " }\n\n";
1322
1323 // Classify user defined operands.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001324 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001325 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001326 ClassInfo &CI = **it;
1327
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001328 if (!CI.isUserClass())
1329 continue;
1330
1331 OS << " // '" << CI.ClassName << "' class";
1332 if (!CI.SuperClasses.empty()) {
1333 OS << ", subclass of ";
1334 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) {
1335 if (i) OS << ", ";
1336 OS << "'" << CI.SuperClasses[i]->ClassName << "'";
1337 assert(CI < *CI.SuperClasses[i] && "Invalid class relation!");
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001338 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001339 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001340 OS << "\n";
1341
1342 OS << " if (Operand." << CI.PredicateMethod << "()) {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001343
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001344 // Validate subclass relationships.
1345 if (!CI.SuperClasses.empty()) {
1346 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i)
1347 OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod
1348 << "() && \"Invalid class relationship!\");\n";
1349 }
1350
1351 OS << " return " << CI.Name << ";\n";
1352 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001353 }
1354 OS << " return InvalidMatchClass;\n";
1355 OS << "}\n\n";
1356}
1357
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001358/// EmitIsSubclass - Emit the subclass predicate function.
1359static void EmitIsSubclass(CodeGenTarget &Target,
1360 std::vector<ClassInfo*> &Infos,
1361 raw_ostream &OS) {
1362 OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1363 OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n";
1364 OS << " if (A == B)\n";
1365 OS << " return true;\n\n";
1366
1367 OS << " switch (A) {\n";
1368 OS << " default:\n";
1369 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001370 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001371 ie = Infos.end(); it != ie; ++it) {
1372 ClassInfo &A = **it;
1373
1374 if (A.Kind != ClassInfo::Token) {
1375 std::vector<StringRef> SuperClasses;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001376 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001377 ie = Infos.end(); it != ie; ++it) {
1378 ClassInfo &B = **it;
1379
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001380 if (&A != &B && A.isSubsetOf(B))
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001381 SuperClasses.push_back(B.Name);
1382 }
1383
1384 if (SuperClasses.empty())
1385 continue;
1386
1387 OS << "\n case " << A.Name << ":\n";
1388
1389 if (SuperClasses.size() == 1) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001390 OS << " return B == " << SuperClasses.back() << ";\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001391 continue;
1392 }
1393
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001394 OS << " switch (B) {\n";
1395 OS << " default: return false;\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001396 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001397 OS << " case " << SuperClasses[i] << ": return true;\n";
1398 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001399 }
1400 }
1401 OS << " }\n";
1402 OS << "}\n\n";
1403}
1404
Chris Lattner70add882009-08-08 20:02:57 +00001405
1406
Daniel Dunbar245f0582009-08-08 21:22:41 +00001407/// EmitMatchTokenString - Emit the function to match a token string to the
1408/// appropriate match class value.
1409static void EmitMatchTokenString(CodeGenTarget &Target,
1410 std::vector<ClassInfo*> &Infos,
1411 raw_ostream &OS) {
1412 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001413 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001414 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001415 ie = Infos.end(); it != ie; ++it) {
1416 ClassInfo &CI = **it;
1417
1418 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001419 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1420 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001421 }
1422
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001423 OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001424
Chris Lattner5845e5c2010-09-06 02:01:51 +00001425 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001426
1427 OS << " return InvalidMatchClass;\n";
1428 OS << "}\n\n";
1429}
Chris Lattner70add882009-08-08 20:02:57 +00001430
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001431/// EmitMatchRegisterName - Emit the function to match a string to the target
1432/// specific register enum.
1433static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1434 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001435 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001436 std::vector<StringMatcher::StringPair> Matches;
Daniel Dunbar245f0582009-08-08 21:22:41 +00001437 for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) {
1438 const CodeGenRegister &Reg = Target.getRegisters()[i];
Daniel Dunbar22be5222009-07-17 18:51:11 +00001439 if (Reg.TheDef->getValueAsString("AsmName").empty())
1440 continue;
1441
Chris Lattner5845e5c2010-09-06 02:01:51 +00001442 Matches.push_back(StringMatcher::StringPair(
1443 Reg.TheDef->getValueAsString("AsmName"),
1444 "return " + utostr(i + 1) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001445 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001446
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001447 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001448
Chris Lattner5845e5c2010-09-06 02:01:51 +00001449 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001450
Daniel Dunbar245f0582009-08-08 21:22:41 +00001451 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001452 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001453}
Daniel Dunbara027d222009-07-31 02:32:59 +00001454
Daniel Dunbar54074b52010-07-19 05:44:09 +00001455/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1456/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001457static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001458 raw_ostream &OS) {
1459 OS << "// Flags for subtarget features that participate in "
1460 << "instruction matching.\n";
1461 OS << "enum SubtargetFeatureFlag {\n";
1462 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1463 it = Info.SubtargetFeatures.begin(),
1464 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1465 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001466 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001467 }
1468 OS << " Feature_None = 0\n";
1469 OS << "};\n\n";
1470}
1471
1472/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1473/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001474static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001475 raw_ostream &OS) {
1476 std::string ClassName =
1477 Info.AsmParser->getValueAsString("AsmParserClassName");
1478
Chris Lattner02bcbc92010-11-01 01:37:30 +00001479 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
1480 << "ComputeAvailableFeatures(const " << Info.Target.getName()
Daniel Dunbar54074b52010-07-19 05:44:09 +00001481 << "Subtarget *Subtarget) const {\n";
1482 OS << " unsigned Features = 0;\n";
1483 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1484 it = Info.SubtargetFeatures.begin(),
1485 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1486 SubtargetFeatureInfo &SFI = *it->second;
1487 OS << " if (" << SFI.TheDef->getValueAsString("CondString")
1488 << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001489 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001490 }
1491 OS << " return Features;\n";
1492 OS << "}\n\n";
1493}
1494
Chris Lattner6fa152c2010-10-30 20:15:02 +00001495static std::string GetAliasRequiredFeatures(Record *R,
1496 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001497 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001498 std::string Result;
1499 unsigned NumFeatures = 0;
1500 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001501 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Chris Lattner693173f2010-10-30 19:23:13 +00001502
Chris Lattner4a74ee72010-11-01 02:09:21 +00001503 if (F == 0)
1504 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1505 "' is not marked as an AssemblerPredicate!");
1506
1507 if (NumFeatures)
1508 Result += '|';
1509
1510 Result += F->getEnumName();
1511 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001512 }
1513
1514 if (NumFeatures > 1)
1515 Result = '(' + Result + ')';
1516 return Result;
1517}
1518
Chris Lattner674c1dc2010-10-30 17:36:36 +00001519/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001520/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001521static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Chris Lattner674c1dc2010-10-30 17:36:36 +00001522 std::vector<Record*> Aliases =
1523 Records.getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001524 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001525
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001526 OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, "
1527 "unsigned Features) {\n";
1528
Chris Lattner4fd32c62010-10-30 18:56:12 +00001529 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1530 // iteration order of the map is stable.
1531 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
1532
Chris Lattner674c1dc2010-10-30 17:36:36 +00001533 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1534 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001535 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001536 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001537
1538 // Process each alias a "from" mnemonic at a time, building the code executed
1539 // by the string remapper.
1540 std::vector<StringMatcher::StringPair> Cases;
1541 for (std::map<std::string, std::vector<Record*> >::iterator
1542 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1543 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001544 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001545
1546 // Loop through each alias and emit code that handles each case. If there
1547 // are two instructions without predicates, emit an error. If there is one,
1548 // emit it last.
1549 std::string MatchCode;
1550 int AliasWithNoPredicate = -1;
Chris Lattner4fd32c62010-10-30 18:56:12 +00001551
Chris Lattner693173f2010-10-30 19:23:13 +00001552 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1553 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001554 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Chris Lattner693173f2010-10-30 19:23:13 +00001555
1556 // If this unconditionally matches, remember it for later and diagnose
1557 // duplicates.
1558 if (FeatureMask.empty()) {
1559 if (AliasWithNoPredicate != -1) {
1560 // We can't have two aliases from the same mnemonic with no predicate.
1561 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1562 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001563 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001564 }
1565
1566 AliasWithNoPredicate = i;
1567 continue;
1568 }
1569
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001570 if (!MatchCode.empty())
1571 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001572 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1573 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001574 }
1575
Chris Lattner693173f2010-10-30 19:23:13 +00001576 if (AliasWithNoPredicate != -1) {
1577 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001578 if (!MatchCode.empty())
1579 MatchCode += "else\n ";
1580 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001581 }
1582
1583 MatchCode += "return;";
1584
1585 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001586 }
1587
Chris Lattner674c1dc2010-10-30 17:36:36 +00001588
1589 StringMatcher("Mnemonic", Cases, OS).Emit();
Chris Lattner7fd44892010-10-30 18:48:18 +00001590 OS << "}\n";
1591
1592 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001593}
1594
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001595void AsmMatcherEmitter::run(raw_ostream &OS) {
1596 CodeGenTarget Target;
1597 Record *AsmParser = Target.getAsmParser();
1598 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
1599
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001600 // Compute the information on the instructions to match.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001601 AsmMatcherInfo Info(AsmParser, Target);
1602 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00001603
Daniel Dunbare1f6de32010-02-02 23:46:36 +00001604 // Sort the instruction table using the partial order on classes. We use
1605 // stable_sort to ensure that ambiguous instructions are still
1606 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001607 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
1608 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001609
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001610 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001611 for (std::vector<MatchableInfo*>::iterator
1612 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001613 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00001614 (*it)->dump();
1615 });
Daniel Dunbara027d222009-07-31 02:32:59 +00001616
Chris Lattner22bc5c42010-11-01 05:06:45 +00001617 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001618 DEBUG_WITH_TYPE("ambiguous_instrs", {
1619 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001620 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00001621 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001622 MatchableInfo &A = *Info.Matchables[i];
1623 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001624
Chris Lattner87410362010-09-06 20:21:47 +00001625 if (A.CouldMatchAmiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001626 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001627 A.dump();
1628 errs() << "\nis incomparable with:\n";
1629 B.dump();
1630 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00001631 ++NumAmbiguous;
1632 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00001633 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001634 }
Chris Lattner87410362010-09-06 20:21:47 +00001635 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001636 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00001637 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001638 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001639
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001640 // Write the output.
1641
1642 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
1643
Chris Lattner0692ee62010-09-06 19:11:01 +00001644 // Information for the class declaration.
1645 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
1646 OS << "#undef GET_ASSEMBLER_HEADER\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001647 OS << " // This should be included into the middle of the declaration of \n";
1648 OS << " // your subclasses implementation of TargetAsmParser.\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001649 OS << " unsigned ComputeAvailableFeatures(const " <<
1650 Target.getName() << "Subtarget *Subtarget) const;\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001651 OS << " enum MatchResultTy {\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001652 OS << " Match_Success, Match_MnemonicFail, Match_InvalidOperand,\n";
1653 OS << " Match_MissingFeature\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001654 OS << " };\n";
Jim Grosbachbb168242010-10-08 18:13:57 +00001655 OS << " MatchResultTy MatchInstructionImpl(const "
1656 << "SmallVectorImpl<MCParsedAsmOperand*>"
Chris Lattnerce4a3352010-09-06 22:11:18 +00001657 << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001658 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
1659
Jim Grosbacha7c78222010-10-29 22:13:48 +00001660
1661
1662
Chris Lattner0692ee62010-09-06 19:11:01 +00001663 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
1664 OS << "#undef GET_REGISTER_MATCHER\n\n";
1665
Daniel Dunbar54074b52010-07-19 05:44:09 +00001666 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001667 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001668
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001669 // Emit the function to match a register name to number.
1670 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00001671
1672 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001673
Chris Lattner0692ee62010-09-06 19:11:01 +00001674
1675 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
1676 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001677
Chris Lattner7fd44892010-10-30 18:48:18 +00001678 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001679 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Chris Lattner7fd44892010-10-30 18:48:18 +00001680
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001681 // Generate the unified function to convert operands into an MCInst.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001682 EmitConvertToMCInst(Target, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001683
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001684 // Emit the enumeration for classes which participate in matching.
1685 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001686
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001687 // Emit the routine to match token strings to their match class.
1688 EmitMatchTokenString(Target, Info.Classes, OS);
1689
1690 // Emit the routine to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001691 EmitClassifyOperand(Info, OS);
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001692
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001693 // Emit the subclass predicate routine.
1694 EmitIsSubclass(Target, Info.Classes, OS);
1695
Daniel Dunbar54074b52010-07-19 05:44:09 +00001696 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001697 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001698
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001699
1700 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001701 for (std::vector<MatchableInfo*>::const_iterator it =
1702 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001703 it != ie; ++it)
1704 MaxNumOperands = std::max(MaxNumOperands, (*it)->Operands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001705
1706
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001707 // Emit the static match table; unused classes get initalized to 0 which is
1708 // guaranteed to be InvalidMatchClass.
1709 //
1710 // FIXME: We can reduce the size of this table very easily. First, we change
1711 // it so that store the kinds in separate bit-fields for each index, which
1712 // only needs to be the max width used for classes at that index (we also need
1713 // to reject based on this during classification). If we then make sure to
1714 // order the match kinds appropriately (putting mnemonics last), then we
1715 // should only end up using a few bits for each class, especially the ones
1716 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00001717 OS << "namespace {\n";
1718 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001719 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001720 OS << " const char *Mnemonic;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001721 OS << " ConversionKind ConvertFn;\n";
1722 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001723 OS << " unsigned RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001724 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001725
Chris Lattner2b1f9432010-09-06 21:22:45 +00001726 OS << "// Predicate for searching for an opcode.\n";
1727 OS << " struct LessOpcode {\n";
1728 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
1729 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
1730 OS << " }\n";
1731 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
1732 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
1733 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00001734 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
1735 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
1736 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00001737 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001738
Chris Lattner96352e52010-09-06 21:08:38 +00001739 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001740
Chris Lattner96352e52010-09-06 21:08:38 +00001741 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00001742 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001743
Chris Lattner22bc5c42010-11-01 05:06:45 +00001744 for (std::vector<MatchableInfo*>::const_iterator it =
1745 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001746 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001747 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001748
Chris Lattner96352e52010-09-06 21:08:38 +00001749 OS << " { " << Target.getName() << "::" << II.InstrName
1750 << ", \"" << II.Tokens[0] << "\""
1751 << ", " << II.ConversionFnKind << ", { ";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001752 for (unsigned i = 0, e = II.Operands.size(); i != e; ++i) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001753 MatchableInfo::Operand &Op = II.Operands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001754
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001755 if (i) OS << ", ";
1756 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001757 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00001758 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001759
Daniel Dunbar54074b52010-07-19 05:44:09 +00001760 // Write the required features mask.
1761 if (!II.RequiredFeatures.empty()) {
1762 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
1763 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001764 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00001765 }
1766 } else
1767 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001768
Daniel Dunbar54074b52010-07-19 05:44:09 +00001769 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001770 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001771
Chris Lattner96352e52010-09-06 21:08:38 +00001772 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001773
Chris Lattner96352e52010-09-06 21:08:38 +00001774 // Finally, build the match function.
1775 OS << Target.getName() << ClassName << "::MatchResultTy "
1776 << Target.getName() << ClassName << "::\n"
1777 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
1778 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001779 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001780
1781 // Emit code to get the available features.
1782 OS << " // Get the current feature set.\n";
1783 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
1784
Chris Lattner674c1dc2010-10-30 17:36:36 +00001785 OS << " // Get the instruction mnemonic, which is the first token.\n";
1786 OS << " StringRef Mnemonic = ((" << Target.getName()
1787 << "Operand*)Operands[0])->getToken();\n\n";
1788
Chris Lattner7fd44892010-10-30 18:48:18 +00001789 if (HasMnemonicAliases) {
1790 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
1791 OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
1792 }
Chris Lattner674c1dc2010-10-30 17:36:36 +00001793
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001794 // Emit code to compute the class list for this operand vector.
1795 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001796 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
1797 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
1798 OS << " return Match_InvalidOperand;\n";
1799 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001800
1801 OS << " // Compute the class list for this operand vector.\n";
1802 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001803 OS << " for (unsigned i = 1, e = Operands.size(); i != e; ++i) {\n";
1804 OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001805
1806 OS << " // Check for invalid operands before matching.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001807 OS << " if (Classes[i-1] == InvalidMatchClass) {\n";
1808 OS << " ErrorInfo = i;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001809 OS << " return Match_InvalidOperand;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001810 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001811 OS << " }\n\n";
1812
1813 OS << " // Mark unused classes.\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001814 OS << " for (unsigned i = Operands.size()-1, e = " << MaxNumOperands << "; "
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001815 << "i != e; ++i)\n";
1816 OS << " Classes[i] = InvalidMatchClass;\n\n";
1817
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001818 OS << " // Some state to try to produce better error messages.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001819 OS << " bool HadMatchOtherThanFeatures = false;\n\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001820 OS << " // Set ErrorInfo to the operand that mismatches if it is \n";
1821 OS << " // wrong for all instances of the instruction.\n";
1822 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001823
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001824 // Emit code to search the table.
1825 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001826 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
1827 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00001828 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001829
Chris Lattnera008e8a2010-09-06 21:54:15 +00001830 OS << " // Return a more specific error code if no mnemonics match.\n";
1831 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
1832 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001833
Chris Lattner2b1f9432010-09-06 21:22:45 +00001834 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00001835 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001836 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001837
Gabor Greife53ee3b2010-09-07 06:06:06 +00001838 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00001839 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001840
Daniel Dunbar54074b52010-07-19 05:44:09 +00001841 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001842 OS << " bool OperandsValid = true;\n";
1843 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
1844 OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n";
1845 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001846 OS << " // If this operand is broken for all of the instances of this\n";
1847 OS << " // mnemonic, keep track of it so we can report loc info.\n";
1848 OS << " if (it == MnemonicRange.first || ErrorInfo == i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001849 OS << " ErrorInfo = i+1;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001850 OS << " else\n";
1851 OS << " ErrorInfo = ~0U;";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001852 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
1853 OS << " OperandsValid = false;\n";
1854 OS << " break;\n";
1855 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001856
Chris Lattnerce4a3352010-09-06 22:11:18 +00001857 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00001858
1859 // Emit check that the required features are available.
1860 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
1861 << "!= it->RequiredFeatures) {\n";
1862 OS << " HadMatchOtherThanFeatures = true;\n";
1863 OS << " continue;\n";
1864 OS << " }\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001865
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001866 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001867 OS << " ConvertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
1868
1869 // Call the post-processing function, if used.
1870 std::string InsnCleanupFn =
1871 AsmParser->getValueAsString("AsmParserInstCleanup");
1872 if (!InsnCleanupFn.empty())
1873 OS << " " << InsnCleanupFn << "(Inst);\n";
1874
Chris Lattner79ed3f72010-09-06 19:22:17 +00001875 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001876 OS << " }\n\n";
1877
Chris Lattnerec6789f2010-09-06 20:08:02 +00001878 OS << " // Okay, we had no match. Try to return a useful error code.\n";
1879 OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001880 OS << " return Match_InvalidOperand;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001881 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001882
Chris Lattner0692ee62010-09-06 19:11:01 +00001883 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001884}