blob: 32b7901e486565662616741315670932ad195e17 [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 Dunbar20927f22009-08-07 08:26:05 +000096
97namespace {
Chris Lattner02bcbc92010-11-01 01:37:30 +000098 class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +000099struct SubtargetFeatureInfo;
100
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000101/// ClassInfo - Helper class for storing the information about a particular
102/// class of operands which can be matched.
103struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000104 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000105 /// Invalid kind, for use as a sentinel value.
106 Invalid = 0,
107
108 /// The class for a particular token.
109 Token,
110
111 /// The (first) register class, subsequent register classes are
112 /// RegisterClass0+1, and so on.
113 RegisterClass0,
114
115 /// The (first) user defined class, subsequent user defined classes are
116 /// UserClass0+1, and so on.
117 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000118 };
119
120 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
121 /// N) for the Nth user defined class.
122 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000123
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000124 /// SuperClasses - The super classes of this class. Note that for simplicities
125 /// sake user operands only record their immediate super class, while register
126 /// operands include all superclasses.
127 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000128
Daniel Dunbar6745d422009-08-09 05:18:30 +0000129 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000130 std::string Name;
131
Daniel Dunbar6745d422009-08-09 05:18:30 +0000132 /// ClassName - The unadorned generic name for this class (e.g., Token).
133 std::string ClassName;
134
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000135 /// ValueName - The name of the value this class represents; for a token this
136 /// is the literal token string, for an operand it is the TableGen class (or
137 /// empty if this is a derived class).
138 std::string ValueName;
139
140 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000141 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000142 std::string PredicateMethod;
143
144 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000145 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000146 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000147
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000148 /// For register classes, the records for all the registers in this class.
149 std::set<Record*> Registers;
150
151public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000152 /// isRegisterClass() - Check if this is a register class.
153 bool isRegisterClass() const {
154 return Kind >= RegisterClass0 && Kind < UserClass0;
155 }
156
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000157 /// isUserClass() - Check if this is a user defined class.
158 bool isUserClass() const {
159 return Kind >= UserClass0;
160 }
161
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000162 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
163 /// are related if they are in the same class hierarchy.
164 bool isRelatedTo(const ClassInfo &RHS) const {
165 // Tokens are only related to tokens.
166 if (Kind == Token || RHS.Kind == Token)
167 return Kind == Token && RHS.Kind == Token;
168
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000169 // Registers classes are only related to registers classes, and only if
170 // their intersection is non-empty.
171 if (isRegisterClass() || RHS.isRegisterClass()) {
172 if (!isRegisterClass() || !RHS.isRegisterClass())
173 return false;
174
175 std::set<Record*> Tmp;
176 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000177 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000178 RHS.Registers.begin(), RHS.Registers.end(),
179 II);
180
181 return !Tmp.empty();
182 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000183
184 // Otherwise we have two users operands; they are related if they are in the
185 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000186 //
187 // FIXME: This is an oversimplification, they should only be related if they
188 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000189 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
190 const ClassInfo *Root = this;
191 while (!Root->SuperClasses.empty())
192 Root = Root->SuperClasses.front();
193
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000194 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000195 while (!RHSRoot->SuperClasses.empty())
196 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000197
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000198 return Root == RHSRoot;
199 }
200
Jim Grosbacha7c78222010-10-29 22:13:48 +0000201 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000202 bool isSubsetOf(const ClassInfo &RHS) const {
203 // This is a subset of RHS if it is the same class...
204 if (this == &RHS)
205 return true;
206
207 // ... or if any of its super classes are a subset of RHS.
208 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
209 ie = SuperClasses.end(); it != ie; ++it)
210 if ((*it)->isSubsetOf(RHS))
211 return true;
212
213 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000214 }
215
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000216 /// operator< - Compare two classes.
217 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000218 if (this == &RHS)
219 return false;
220
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000221 // Unrelated classes can be ordered by kind.
222 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000223 return Kind < RHS.Kind;
224
225 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000226 case Invalid:
227 assert(0 && "Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000228 case Token:
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000229 // Tokens are comparable by value.
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000230 //
231 // FIXME: Compare by enum value.
232 return ValueName < RHS.ValueName;
233
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000234 default:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000235 // This class preceeds the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000236 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000237 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000238 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000239 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000240
241 // Otherwise, order by name to ensure we have a total ordering.
242 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000243 }
244 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000245};
246
Chris Lattner22bc5c42010-11-01 05:06:45 +0000247/// MatchableInfo - Helper class for storing the necessary information for an
248/// instruction or alias which is capable of being matched.
249struct MatchableInfo {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000250 struct Operand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000251 /// Token - This is the token that the operand came from.
252 StringRef Token;
253
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000254 /// The unique class instance this operand should match.
255 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000256
Chris Lattnerec6f0962010-11-02 18:10:06 +0000257 /// The original operand this corresponds to. This is unset for singleton
258 /// registers and tokens, because they don't have a list in the ins/outs
259 /// list. If an operand is tied ($a=$b), this refers to source operand: $b.
Chris Lattnerc240bb02010-11-01 04:03:32 +0000260 const CGIOperandList::OperandInfo *OperandInfo;
Chris Lattner4c9f4e42010-11-01 23:08:02 +0000261
Chris Lattnerd19ec052010-11-02 17:30:52 +0000262 explicit Operand(StringRef T) : Token(T), Class(0), OperandInfo(0) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000263 };
264
265 /// InstrName - The target name for this instruction.
266 std::string InstrName;
267
Chris Lattner3b5aec62010-11-02 17:34:28 +0000268 /// TheDef - This is the definition of the instruction or InstAlias that this
269 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000270 Record *const TheDef;
Chris Lattner3b5aec62010-11-02 17:34:28 +0000271
272 /// OperandList - This is the operand list that came from the (ins) and (outs)
273 /// list of the alias or instruction.
Chris Lattner5bc93872010-11-01 04:34:44 +0000274 const CGIOperandList &OperandList;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000275
276 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000277 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000278 std::string AsmString;
279
Chris Lattnerd19ec052010-11-02 17:30:52 +0000280 /// Mnemonic - This is the first token of the matched instruction, its
281 /// mnemonic.
282 StringRef Mnemonic;
283
Chris Lattner3116fef2010-11-02 01:03:43 +0000284 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000285 /// annotated with a class and where in the OperandList they were defined.
286 /// This directly corresponds to the tokenized AsmString after the mnemonic is
287 /// removed.
Chris Lattner3116fef2010-11-02 01:03:43 +0000288 SmallVector<Operand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000289
Daniel Dunbar54074b52010-07-19 05:44:09 +0000290 /// Predicates - The required subtarget features to match this instruction.
291 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
292
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000293 /// ConversionFnKind - The enum value which is passed to the generated
294 /// ConvertToMCInst to convert parsed operands into an MCInst for this
295 /// function.
296 std::string ConversionFnKind;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000297
Chris Lattner22bc5c42010-11-01 05:06:45 +0000298 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000299 : TheDef(CGI.TheDef), OperandList(CGI.Operands), AsmString(CGI.AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000300 InstrName = TheDef->getName();
Chris Lattner5bc93872010-11-01 04:34:44 +0000301 }
302
Chris Lattner22bc5c42010-11-01 05:06:45 +0000303 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000304 : TheDef(Alias->TheDef), OperandList(Alias->Operands),
305 AsmString(Alias->AsmString) {
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000306
307 // FIXME: Huge hack.
308 DefInit *DI = dynamic_cast<DefInit*>(Alias->Result->getOperator());
309 assert(DI);
310
311 InstrName = DI->getDef()->getName();
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000312 }
313
314 void Initialize(const AsmMatcherInfo &Info,
315 SmallPtrSet<Record*, 16> &SingletonRegisters);
316
Chris Lattner22bc5c42010-11-01 05:06:45 +0000317 /// Validate - Return true if this matchable is a valid thing to match against
318 /// and perform a bunch of validity checking.
319 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Chris Lattner5bc93872010-11-01 04:34:44 +0000320
Chris Lattnerd19ec052010-11-02 17:30:52 +0000321 /// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000322 /// register, return the Record for it, otherwise return null.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000323 Record *getSingletonRegisterForAsmOperand(unsigned i,
324 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000325
Chris Lattner22bc5c42010-11-01 05:06:45 +0000326 /// operator< - Compare two matchables.
327 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000328 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000329 if (Mnemonic != RHS.Mnemonic)
330 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000331
Chris Lattner3116fef2010-11-02 01:03:43 +0000332 if (AsmOperands.size() != RHS.AsmOperands.size())
333 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000334
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000335 // Compare lexicographically by operand. The matcher validates that other
336 // orderings wouldn't be ambiguous using \see CouldMatchAmiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000337 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
338 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000339 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000340 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000341 return false;
342 }
343
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000344 return false;
345 }
346
Chris Lattner22bc5c42010-11-01 05:06:45 +0000347 /// CouldMatchAmiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000348 /// ambiguously match the same set of operands as \arg RHS (without being a
349 /// strictly superior match).
Chris Lattner22bc5c42010-11-01 05:06:45 +0000350 bool CouldMatchAmiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000351 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000352 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000353 return false;
354
Daniel Dunbar2b544812009-08-09 06:05:33 +0000355 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000356 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000357 return false;
358
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000359 // Otherwise, make sure the ordering of the two instructions is unambiguous
360 // by checking that either (a) a token or operand kind discriminates them,
361 // or (b) the ordering among equivalent kinds is consistent.
362
Daniel Dunbar2b544812009-08-09 06:05:33 +0000363 // Tokens and operand kinds are unambiguous (assuming a correct target
364 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000365 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
366 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
367 AsmOperands[i].Class->Kind == ClassInfo::Token)
368 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
369 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000370 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000371
Daniel Dunbar2b544812009-08-09 06:05:33 +0000372 // Otherwise, this operand could commute if all operands are equivalent, or
373 // there is a pair of operands that compare less than and a pair that
374 // compare greater than.
375 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000376 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
377 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000378 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000379 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000380 HasGT = true;
381 }
382
383 return !(HasLT ^ HasGT);
384 }
385
Daniel Dunbar20927f22009-08-07 08:26:05 +0000386 void dump();
Chris Lattnerd19ec052010-11-02 17:30:52 +0000387
388private:
389 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000390};
391
Daniel Dunbar54074b52010-07-19 05:44:09 +0000392/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
393/// feature which participates in instruction matching.
394struct SubtargetFeatureInfo {
395 /// \brief The predicate record for this feature.
396 Record *TheDef;
397
398 /// \brief An unique index assigned to represent this feature.
399 unsigned Index;
400
Chris Lattner0aed1e72010-10-30 20:07:57 +0000401 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
402
Daniel Dunbar54074b52010-07-19 05:44:09 +0000403 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000404 std::string getEnumName() const {
405 return "Feature_" + TheDef->getName();
406 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000407};
408
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000409class AsmMatcherInfo {
410public:
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000411 /// The tablegen AsmParser record.
412 Record *AsmParser;
413
Chris Lattner02bcbc92010-11-01 01:37:30 +0000414 /// Target - The target information.
415 CodeGenTarget &Target;
416
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000417 /// The AsmParser "RegisterPrefix" value.
418 std::string RegisterPrefix;
419
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000420 /// The classes which are needed for matching.
421 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000422
Chris Lattner22bc5c42010-11-01 05:06:45 +0000423 /// The information on the matchables to match.
424 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000425
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000426 /// Map of Register records to their class information.
427 std::map<Record*, ClassInfo*> RegisterClasses;
428
Daniel Dunbar54074b52010-07-19 05:44:09 +0000429 /// Map of Predicate records to their subtarget information.
430 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Chris Lattner6fa152c2010-10-30 20:15:02 +0000431
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000432private:
433 /// Map of token to class information which has already been constructed.
434 std::map<std::string, ClassInfo*> TokenClasses;
435
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000436 /// Map of RegisterClass records to their class information.
437 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000438
Daniel Dunbar338825c2009-08-10 18:41:10 +0000439 /// Map of AsmOperandClass records to their class information.
440 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000441
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000442private:
443 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000444 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000445
446 /// getOperandClass - Lookup or create the class for the given operand.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000447 ClassInfo *getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000448 const CGIOperandList::OperandInfo &OI);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000449
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000450 /// BuildRegisterClasses - Build the ClassInfo* instances for register
451 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000452 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000453
454 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
455 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000456 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000457
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000458public:
Chris Lattner02bcbc92010-11-01 01:37:30 +0000459 AsmMatcherInfo(Record *AsmParser, CodeGenTarget &Target);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000460
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000461 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000462 void BuildInfo();
Chris Lattner6fa152c2010-10-30 20:15:02 +0000463
464 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
465 /// given operand.
466 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
467 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
468 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
469 SubtargetFeatures.find(Def);
470 return I == SubtargetFeatures.end() ? 0 : I->second;
471 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000472};
473
Daniel Dunbar20927f22009-08-07 08:26:05 +0000474}
475
Chris Lattner22bc5c42010-11-01 05:06:45 +0000476void MatchableInfo::dump() {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000477 errs() << InstrName << " -- " << "flattened:\"" << AsmString << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000478
Chris Lattner3116fef2010-11-02 01:03:43 +0000479 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
480 Operand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000481 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000482 if (Op.Class->Kind == ClassInfo::Token) {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000483 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000484 continue;
485 }
486
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000487 if (!Op.OperandInfo) {
488 errs() << "(singleton register)\n";
489 continue;
490 }
491
Chris Lattnerc240bb02010-11-01 04:03:32 +0000492 const CGIOperandList::OperandInfo &OI = *Op.OperandInfo;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000493 errs() << OI.Name << " " << OI.Rec->getName()
494 << " (" << OI.MIOperandNo << ", " << OI.MINumOperands << ")\n";
495 }
496}
497
Chris Lattner22bc5c42010-11-01 05:06:45 +0000498void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
499 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000500 // TODO: Eventually support asmparser for Variant != 0.
501 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
502
Chris Lattnerd19ec052010-11-02 17:30:52 +0000503 TokenizeAsmString(Info);
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000504
505 // Compute the require features.
506 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
507 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
508 if (SubtargetFeatureInfo *Feature =
509 Info.getSubtargetFeature(Predicates[i]))
510 RequiredFeatures.push_back(Feature);
511
512 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000513 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
514 if (Record *Reg = getSingletonRegisterForAsmOperand(i, Info))
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000515 SingletonRegisters.insert(Reg);
516 }
517}
518
Chris Lattnerd19ec052010-11-02 17:30:52 +0000519/// TokenizeAsmString - Tokenize a simplified assembly string.
520void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
521 StringRef String = AsmString;
522 unsigned Prev = 0;
523 bool InTok = true;
524 for (unsigned i = 0, e = String.size(); i != e; ++i) {
525 switch (String[i]) {
526 case '[':
527 case ']':
528 case '*':
529 case '!':
530 case ' ':
531 case '\t':
532 case ',':
533 if (InTok) {
534 AsmOperands.push_back(Operand(String.slice(Prev, i)));
535 InTok = false;
536 }
537 if (!isspace(String[i]) && String[i] != ',')
538 AsmOperands.push_back(Operand(String.substr(i, 1)));
539 Prev = i + 1;
540 break;
541
542 case '\\':
543 if (InTok) {
544 AsmOperands.push_back(Operand(String.slice(Prev, i)));
545 InTok = false;
546 }
547 ++i;
548 assert(i != String.size() && "Invalid quoted character");
549 AsmOperands.push_back(Operand(String.substr(i, 1)));
550 Prev = i + 1;
551 break;
552
553 case '$': {
554 // If this isn't "${", treat like a normal token.
555 if (i + 1 == String.size() || String[i + 1] != '{') {
556 if (InTok) {
557 AsmOperands.push_back(Operand(String.slice(Prev, i)));
558 InTok = false;
559 }
560 Prev = i;
561 break;
562 }
563
564 if (InTok) {
565 AsmOperands.push_back(Operand(String.slice(Prev, i)));
566 InTok = false;
567 }
568
569 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
570 assert(End != String.end() && "Missing brace in operand reference!");
571 size_t EndPos = End - String.begin();
572 AsmOperands.push_back(Operand(String.slice(i, EndPos+1)));
573 Prev = EndPos + 1;
574 i = EndPos;
575 break;
576 }
577
578 case '.':
579 if (InTok)
580 AsmOperands.push_back(Operand(String.slice(Prev, i)));
581 Prev = i;
582 InTok = true;
583 break;
584
585 default:
586 InTok = true;
587 }
588 }
589 if (InTok && Prev != String.size())
590 AsmOperands.push_back(Operand(String.substr(Prev)));
591
592 // The first token of the instruction is the mnemonic, which must be a
593 // simple string, not a $foo variable or a singleton register.
594 assert(!AsmOperands.empty() && "Instruction has no tokens?");
595 Mnemonic = AsmOperands[0].Token;
596 if (Mnemonic[0] == '$' || getSingletonRegisterForAsmOperand(0, Info))
597 throw TGError(TheDef->getLoc(),
598 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
599
600 // Remove the first operand, it is tracked in the mnemonic field.
601 AsmOperands.erase(AsmOperands.begin());
602}
603
604
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000605
Chris Lattner22bc5c42010-11-01 05:06:45 +0000606bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
607 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000608 if (AsmString.empty())
609 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
610
Chris Lattner22bc5c42010-11-01 05:06:45 +0000611 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000612 // isCodeGenOnly if they are pseudo instructions.
613 if (AsmString.find('\n') != std::string::npos)
614 throw TGError(TheDef->getLoc(),
615 "multiline instruction is not valid for the asmparser, "
616 "mark it isCodeGenOnly");
617
Chris Lattner4164f6b2010-11-01 04:44:29 +0000618 // Remove comments from the asm string. We know that the asmstring only
619 // has one line.
620 if (!CommentDelimiter.empty() &&
621 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
622 throw TGError(TheDef->getLoc(),
623 "asmstring for instruction has comment character in it, "
624 "mark it isCodeGenOnly");
625
Chris Lattner22bc5c42010-11-01 05:06:45 +0000626 // Reject matchables with operand modifiers, these aren't something we can
627 /// handle, the target should be refactored to use operands instead of
628 /// modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000629 //
630 // Also, check for instructions which reference the operand multiple times;
631 // this implies a constraint we would not honor.
632 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000633 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
634 StringRef Tok = AsmOperands[i].Token;
635 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000636 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000637 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000638 "' not supported by asm matcher. Mark isCodeGenOnly!");
639
Chris Lattner22bc5c42010-11-01 05:06:45 +0000640 // Verify that any operand is only mentioned once.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000641 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000642 if (!Hack)
643 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000644 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000645 "' can never be matched!");
646 // FIXME: Should reject these. The ARM backend hits this with $lane in a
647 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000648 DEBUG({
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000649 errs() << "warning: '" << InstrName << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000650 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000651 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000652 });
653 return false;
654 }
655 }
656
657 return true;
658}
659
660
Chris Lattnerd19ec052010-11-02 17:30:52 +0000661/// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner02bcbc92010-11-01 01:37:30 +0000662/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000663Record *MatchableInfo::
Chris Lattnerd19ec052010-11-02 17:30:52 +0000664getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{
665 StringRef Tok = AsmOperands[i].Token;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000666 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000667 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000668
669 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000670 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
671 return Reg->TheDef;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000672
Chris Lattner1de88232010-11-01 01:47:07 +0000673 // If there is no register prefix (i.e. "%" in "%eax"), then this may
674 // be some random non-register token, just ignore it.
675 if (Info.RegisterPrefix.empty())
676 return 0;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000677
Chris Lattnerec6f0962010-11-02 18:10:06 +0000678 // Otherwise, we have something invalid prefixed with the register prefix,
679 // such as %foo.
Chris Lattner1de88232010-11-01 01:47:07 +0000680 std::string Err = "unable to find register for '" + RegName.str() +
681 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000682 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000683}
684
685
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000686static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000687 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000688
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000689 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
690 switch (*it) {
691 case '*': Res += "_STAR_"; break;
692 case '%': Res += "_PCT_"; break;
693 case ':': Res += "_COLON_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000694 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000695 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000696 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000697 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000698 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000699 }
700 }
701
702 return Res;
703}
704
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000705ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000706 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000707
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000708 if (!Entry) {
709 Entry = new ClassInfo();
710 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000711 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000712 Entry->Name = "MCK_" + getEnumNameForToken(Token);
713 Entry->ValueName = Token;
714 Entry->PredicateMethod = "<invalid>";
715 Entry->RenderMethod = "<invalid>";
716 Classes.push_back(Entry);
717 }
718
719 return Entry;
720}
721
722ClassInfo *
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000723AsmMatcherInfo::getOperandClass(StringRef Token,
Chris Lattnerc240bb02010-11-01 04:03:32 +0000724 const CGIOperandList::OperandInfo &OI) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000725 if (OI.Rec->isSubClassOf("RegisterClass")) {
Chris Lattnerec6f0962010-11-02 18:10:06 +0000726 if (ClassInfo *CI = RegisterClassClasses[OI.Rec])
727 return CI;
728 throw TGError(OI.Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000729 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000730
Daniel Dunbar338825c2009-08-10 18:41:10 +0000731 assert(OI.Rec->isSubClassOf("Operand") && "Unexpected operand!");
732 Record *MatchClass = OI.Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000733 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
734 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000735
Chris Lattnerec6f0962010-11-02 18:10:06 +0000736 throw TGError(OI.Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000737}
738
Chris Lattner1de88232010-11-01 01:47:07 +0000739void AsmMatcherInfo::
740BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerec6f0962010-11-02 18:10:06 +0000741 const std::vector<CodeGenRegister> &Registers = Target.getRegisters();
742 const std::vector<CodeGenRegisterClass> &RegClassList =
743 Target.getRegisterClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000744
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000745 // The register sets used for matching.
746 std::set< std::set<Record*> > RegisterSets;
747
Jim Grosbacha7c78222010-10-29 22:13:48 +0000748 // Gather the defined sets.
Chris Lattnerec6f0962010-11-02 18:10:06 +0000749 for (std::vector<CodeGenRegisterClass>::const_iterator it =
750 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000751 RegisterSets.insert(std::set<Record*>(it->Elements.begin(),
752 it->Elements.end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000753
754 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000755 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
756 ie = SingletonRegisters.end(); it != ie; ++it) {
757 Record *Rec = *it;
758 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
759 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000760
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000761 // Introduce derived sets where necessary (when a register does not determine
762 // a unique register set class), and build the mapping of registers to the set
763 // they should classify to.
764 std::map<Record*, std::set<Record*> > RegisterMap;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000765 for (std::vector<CodeGenRegister>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000766 ie = Registers.end(); it != ie; ++it) {
Chris Lattnerec6f0962010-11-02 18:10:06 +0000767 const CodeGenRegister &CGR = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000768 // Compute the intersection of all sets containing this register.
769 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000770
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000771 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
772 ie = RegisterSets.end(); it != ie; ++it) {
773 if (!it->count(CGR.TheDef))
774 continue;
775
776 if (ContainingSet.empty()) {
777 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000778 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000779 }
Chris Lattnerec6f0962010-11-02 18:10:06 +0000780
781 std::set<Record*> Tmp;
782 std::swap(Tmp, ContainingSet);
783 std::insert_iterator< std::set<Record*> > II(ContainingSet,
784 ContainingSet.begin());
785 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000786 }
787
788 if (!ContainingSet.empty()) {
789 RegisterSets.insert(ContainingSet);
790 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
791 }
792 }
793
794 // Construct the register classes.
795 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
796 unsigned Index = 0;
797 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
798 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
799 ClassInfo *CI = new ClassInfo();
800 CI->Kind = ClassInfo::RegisterClass0 + Index;
801 CI->ClassName = "Reg" + utostr(Index);
802 CI->Name = "MCK_Reg" + utostr(Index);
803 CI->ValueName = "";
804 CI->PredicateMethod = ""; // unused
805 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000806 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000807 Classes.push_back(CI);
808 RegisterSetClasses.insert(std::make_pair(*it, CI));
809 }
810
811 // Find the superclasses; we could compute only the subgroup lattice edges,
812 // but there isn't really a point.
813 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
814 ie = RegisterSets.end(); it != ie; ++it) {
815 ClassInfo *CI = RegisterSetClasses[*it];
816 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
817 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000818 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000819 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
820 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
821 }
822
823 // Name the register classes which correspond to a user defined RegisterClass.
Chris Lattnerec6f0962010-11-02 18:10:06 +0000824 for (std::vector<CodeGenRegisterClass>::const_iterator
825 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000826 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->Elements.begin(),
827 it->Elements.end())];
828 if (CI->ValueName.empty()) {
829 CI->ClassName = it->getName();
830 CI->Name = "MCK_" + it->getName();
831 CI->ValueName = it->getName();
832 } else
833 CI->ValueName = CI->ValueName + "," + it->getName();
834
835 RegisterClassClasses.insert(std::make_pair(it->TheDef, CI));
836 }
837
838 // Populate the map for individual registers.
839 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
840 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +0000841 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000842
843 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +0000844 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
845 ie = SingletonRegisters.end(); it != ie; ++it) {
846 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000847 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +0000848 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000849
Chris Lattner1de88232010-11-01 01:47:07 +0000850 if (CI->ValueName.empty()) {
851 CI->ClassName = Rec->getName();
852 CI->Name = "MCK_" + Rec->getName();
853 CI->ValueName = Rec->getName();
854 } else
855 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000856 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000857}
858
Chris Lattner02bcbc92010-11-01 01:37:30 +0000859void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000860 std::vector<Record*> AsmOperands =
861 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000862
863 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +0000864 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000865 ie = AsmOperands.end(); it != ie; ++it)
866 AsmOperandClasses[*it] = new ClassInfo();
867
Daniel Dunbar338825c2009-08-10 18:41:10 +0000868 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000869 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +0000870 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +0000871 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +0000872 CI->Kind = ClassInfo::UserClass0 + Index;
873
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +0000874 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
875 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
876 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
877 if (!DI) {
878 PrintError((*it)->getLoc(), "Invalid super class reference!");
879 continue;
880 }
881
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000882 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
883 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +0000884 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000885 else
886 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +0000887 }
888 CI->ClassName = (*it)->getValueAsString("Name");
889 CI->Name = "MCK_" + CI->ClassName;
890 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000891
892 // Get or construct the predicate method name.
893 Init *PMName = (*it)->getValueInit("PredicateMethod");
894 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
895 CI->PredicateMethod = SI->getValue();
896 } else {
Jim Grosbacha7c78222010-10-29 22:13:48 +0000897 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000898 "Unexpected PredicateMethod field!");
899 CI->PredicateMethod = "is" + CI->ClassName;
900 }
901
902 // Get or construct the render method name.
903 Init *RMName = (*it)->getValueInit("RenderMethod");
904 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
905 CI->RenderMethod = SI->getValue();
906 } else {
907 assert(dynamic_cast<UnsetInit*>(RMName) &&
908 "Unexpected RenderMethod field!");
909 CI->RenderMethod = "add" + CI->ClassName + "Operands";
910 }
911
Daniel Dunbar338825c2009-08-10 18:41:10 +0000912 AsmOperandClasses[*it] = CI;
913 Classes.push_back(CI);
914 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000915}
916
Chris Lattner02bcbc92010-11-01 01:37:30 +0000917AsmMatcherInfo::AsmMatcherInfo(Record *asmParser, CodeGenTarget &target)
918 : AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000919 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000920}
921
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000922
Chris Lattner02bcbc92010-11-01 01:37:30 +0000923void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +0000924 // Build information about all of the AssemblerPredicates.
925 std::vector<Record*> AllPredicates =
926 Records.getAllDerivedDefinitions("Predicate");
927 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
928 Record *Pred = AllPredicates[i];
929 // Ignore predicates that are not intended for the assembler.
930 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
931 continue;
932
Chris Lattner4164f6b2010-11-01 04:44:29 +0000933 if (Pred->getName().empty())
934 throw TGError(Pred->getLoc(), "Predicate has no name!");
Chris Lattner0aed1e72010-10-30 20:07:57 +0000935
936 unsigned FeatureNo = SubtargetFeatures.size();
937 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
938 assert(FeatureNo < 32 && "Too many subtarget features!");
939 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000940
Chris Lattner4164f6b2010-11-01 04:44:29 +0000941 StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
942
Chris Lattner39ee0362010-10-31 19:10:56 +0000943 // Parse the instructions; we need to do this first so that we can gather the
944 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000945 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000946 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
947 E = Target.inst_end(); I != E; ++I) {
948 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000949
Chris Lattner39ee0362010-10-31 19:10:56 +0000950 // If the tblgen -match-prefix option is specified (for tblgen hackers),
951 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +0000952 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000953 continue;
954
Chris Lattner5bc93872010-11-01 04:34:44 +0000955 // Ignore "codegen only" instructions.
956 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
957 continue;
958
Chris Lattner22bc5c42010-11-01 05:06:45 +0000959 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000960
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000961 II->Initialize(*this, SingletonRegisters);
962
Chris Lattner4d43d0f2010-11-01 01:07:14 +0000963 // Ignore instructions which shouldn't be matched and diagnose invalid
964 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000965 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +0000966 continue;
967
968 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
969 //
970 // FIXME: This is a total hack.
971 if (StringRef(II->InstrName).startswith("Int_") ||
972 StringRef(II->InstrName).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +0000973 continue;
Chris Lattner39ee0362010-10-31 19:10:56 +0000974
Chris Lattner22bc5c42010-11-01 05:06:45 +0000975 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000976 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000977
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000978 // Parse all of the InstAlias definitions and stick them in the list of
979 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000980 std::vector<Record*> AllInstAliases =
981 Records.getAllDerivedDefinitions("InstAlias");
982 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
983 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i]);
984
Chris Lattner22bc5c42010-11-01 05:06:45 +0000985 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000986
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000987 II->Initialize(*this, SingletonRegisters);
988
Chris Lattner22bc5c42010-11-01 05:06:45 +0000989 // Validate the alias definitions.
990 II->Validate(CommentDelimiter, false);
991
Chris Lattnerb501d4f2010-11-01 05:34:34 +0000992 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +0000993 }
Chris Lattnerc240bb02010-11-01 04:03:32 +0000994
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000995 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000996 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000997
998 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000999 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001000
Chris Lattner22bc5c42010-11-01 05:06:45 +00001001 // Build the information about matchables.
1002 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1003 ie = Matchables.end(); it != ie; ++it) {
1004 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001005
Chris Lattnere206fcf2010-09-06 21:01:37 +00001006 // Parse the tokens after the mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001007 for (unsigned i = 0, e = II->AsmOperands.size(); i != e; ++i) {
1008 MatchableInfo::Operand &Op = II->AsmOperands[i];
1009 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001010
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001011 // Check for singleton registers.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001012 if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) {
1013 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001014 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1015 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001016 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001017 }
1018
Daniel Dunbar20927f22009-08-07 08:26:05 +00001019 // Check for simple tokens.
1020 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001021 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001022 continue;
1023 }
1024
1025 // Otherwise this is an operand reference.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001026 StringRef OperandName;
1027 if (Token[1] == '{')
1028 OperandName = Token.substr(2, Token.size() - 3);
1029 else
1030 OperandName = Token.substr(1);
1031
1032 // Map this token to an operand. FIXME: Move elsewhere.
1033 unsigned Idx;
Chris Lattner5bc93872010-11-01 04:34:44 +00001034 if (!II->OperandList.hasOperandNamed(OperandName, Idx))
Chris Lattner4164f6b2010-11-01 04:44:29 +00001035 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1036 OperandName.str() + "'");
Daniel Dunbar20927f22009-08-07 08:26:05 +00001037
Daniel Dunbaraf616812010-02-10 08:15:48 +00001038 // FIXME: This is annoying, the named operand may be tied (e.g.,
1039 // XCHG8rm). What we want is the untied operand, which we now have to
1040 // grovel for. Only worry about this for single entry operands, we have to
1041 // clean this up anyway.
Chris Lattner5bc93872010-11-01 04:34:44 +00001042 const CGIOperandList::OperandInfo *OI = &II->OperandList[Idx];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001043 if (OI->Constraints[0].isTied()) {
1044 unsigned TiedOp = OI->Constraints[0].getTiedOperand();
1045
1046 // The tied operand index is an MIOperand index, find the operand that
1047 // contains it.
Chris Lattner5bc93872010-11-01 04:34:44 +00001048 for (unsigned i = 0, e = II->OperandList.size(); i != e; ++i) {
1049 if (II->OperandList[i].MIOperandNo == TiedOp) {
1050 OI = &II->OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001051 break;
1052 }
1053 }
1054
1055 assert(OI && "Unable to find tied operand target!");
1056 }
1057
Chris Lattnerd19ec052010-11-02 17:30:52 +00001058 Op.Class = getOperandClass(Token, *OI);
1059 Op.OperandInfo = OI;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001060 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001061 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001062
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001063 // Reorder classes so that classes preceed super classes.
1064 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001065}
1066
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001067static std::pair<unsigned, unsigned> *
1068GetTiedOperandAtIndex(SmallVectorImpl<std::pair<unsigned, unsigned> > &List,
1069 unsigned Index) {
1070 for (unsigned i = 0, e = List.size(); i != e; ++i)
1071 if (Index == List[i].first)
1072 return &List[i];
1073
1074 return 0;
1075}
1076
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001077static void EmitConvertToMCInst(CodeGenTarget &Target,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001078 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001079 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001080 // Write the convert function to a separate stream, so we can drop it after
1081 // the enum.
1082 std::string ConvertFnBody;
1083 raw_string_ostream CvtOS(ConvertFnBody);
1084
Daniel Dunbar20927f22009-08-07 08:26:05 +00001085 // Function we have already generated.
1086 std::set<std::string> GeneratedFns;
1087
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001088 // Start the unified conversion function.
1089
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001090 CvtOS << "static void ConvertToMCInst(ConversionKind Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001091 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001092 << " const SmallVectorImpl<MCParsedAsmOperand*"
1093 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001094 CvtOS << " Inst.setOpcode(Opcode);\n";
1095 CvtOS << " switch (Kind) {\n";
1096 CvtOS << " default:\n";
1097
1098 // Start the enum, which we will generate inline.
1099
1100 OS << "// Unified function for converting operants to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001101 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001102
Chris Lattner98986712010-01-14 22:21:20 +00001103 // TargetOperandClass - This is the target's operand class, like X86Operand.
1104 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001105
Chris Lattner22bc5c42010-11-01 05:06:45 +00001106 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001107 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001108 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001109
1110 // Order the (class) operands by the order to convert them into an MCInst.
1111 SmallVector<std::pair<unsigned, unsigned>, 4> MIOperandList;
Chris Lattner3116fef2010-11-02 01:03:43 +00001112 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1113 MatchableInfo::Operand &Op = II.AsmOperands[i];
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001114 if (Op.OperandInfo)
1115 MIOperandList.push_back(std::make_pair(Op.OperandInfo->MIOperandNo, i));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001116 }
Daniel Dunbaraf616812010-02-10 08:15:48 +00001117
1118 // Find any tied operands.
1119 SmallVector<std::pair<unsigned, unsigned>, 4> TiedOperands;
Chris Lattner5bc93872010-11-01 04:34:44 +00001120 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1121 const CGIOperandList::OperandInfo &OpInfo = II.OperandList[i];
Daniel Dunbaraf616812010-02-10 08:15:48 +00001122 for (unsigned j = 0, e = OpInfo.Constraints.size(); j != e; ++j) {
Chris Lattnerc240bb02010-11-01 04:03:32 +00001123 const CGIOperandList::ConstraintInfo &CI = OpInfo.Constraints[j];
Chris Lattnerec6f0962010-11-02 18:10:06 +00001124 if (!CI.isTied()) continue;
1125 TiedOperands.push_back(std::make_pair(OpInfo.MIOperandNo,
1126 CI.getTiedOperand()));
Daniel Dunbaraf616812010-02-10 08:15:48 +00001127 }
1128 }
1129
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001130 array_pod_sort(MIOperandList.begin(), MIOperandList.end());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001131
1132 // Compute the total number of operands.
1133 unsigned NumMIOperands = 0;
Chris Lattner5bc93872010-11-01 04:34:44 +00001134 for (unsigned i = 0, e = II.OperandList.size(); i != e; ++i) {
1135 const CGIOperandList::OperandInfo &OI = II.OperandList[i];
Chris Lattnerec6f0962010-11-02 18:10:06 +00001136 NumMIOperands = std::max(NumMIOperands, OI.MIOperandNo+OI.MINumOperands);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001137 }
1138
1139 // Build the conversion function signature.
1140 std::string Signature = "Convert";
1141 unsigned CurIndex = 0;
Chris Lattnerdda855d2010-11-02 21:49:44 +00001142
1143 std::string CaseBody;
1144 raw_string_ostream CaseOS(CaseBody);
1145
1146 // Compute the convert enum and the case body.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001147 for (unsigned i = 0, e = MIOperandList.size(); i != e; ++i) {
Chris Lattner3116fef2010-11-02 01:03:43 +00001148 MatchableInfo::Operand &Op = II.AsmOperands[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
Chris Lattnerdda855d2010-11-02 21:49:44 +00001152 // Add the implicit operands.
Daniel Dunbaraf616812010-02-10 08:15:48 +00001153 for (; CurIndex != Op.OperandInfo->MIOperandNo; ++CurIndex) {
Chris Lattnerdda855d2010-11-02 21:49:44 +00001154 // See if this is a tied operand.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001155 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1156 CurIndex);
Chris Lattnerdda855d2010-11-02 21:49:44 +00001157
1158 if (!Tie) {
1159 // If not, this is some implicit operand. Just assume it is a register
1160 // for now.
1161 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
Daniel Dunbaraf616812010-02-10 08:15:48 +00001162 Signature += "__Imp";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001163 } else {
1164 // Copy the tied operand.
1165 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
1166 CaseOS << " Inst.addOperand(Inst.getOperand("
1167 << Tie->second << "));\n";
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001168 Signature += "__Tie" + utostr(Tie->second);
Chris Lattnerdda855d2010-11-02 21:49:44 +00001169 }
Daniel Dunbaraf616812010-02-10 08:15:48 +00001170 }
Chris Lattnerdda855d2010-11-02 21:49:44 +00001171
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001172 // Registers are always converted the same, don't duplicate the conversion
1173 // function based on them.
1174 //
1175 // FIXME: We could generalize this based on the render method, if it
1176 // mattered.
Chris Lattnerdda855d2010-11-02 21:49:44 +00001177 Signature += "__";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001178 if (Op.Class->isRegisterClass())
1179 Signature += "Reg";
1180 else
1181 Signature += Op.Class->ClassName;
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001182 Signature += utostr(Op.OperandInfo->MINumOperands);
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001183 Signature += "_" + utostr(MIOperandList[i].second);
Chris Lattnerdda855d2010-11-02 21:49:44 +00001184
1185
1186 CaseOS << " ((" << TargetOperandClass << "*)Operands["
1187 << MIOperandList[i].second << "+1])->" << Op.Class->RenderMethod
1188 << "(Inst, " << Op.OperandInfo->MINumOperands << ");\n";
Benjamin Kramerfa1165a2009-08-08 10:06:30 +00001189 CurIndex += Op.OperandInfo->MINumOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001190 }
Chris Lattnerdda855d2010-11-02 21:49:44 +00001191
1192 // And add trailing implicit operands.
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001193 for (; CurIndex != NumMIOperands; ++CurIndex) {
1194 std::pair<unsigned, unsigned> *Tie = GetTiedOperandAtIndex(TiedOperands,
1195 CurIndex);
Chris Lattnerdda855d2010-11-02 21:49:44 +00001196
1197 if (!Tie) {
1198 // If not, this is some implicit operand. Just assume it is a register
1199 // for now.
1200 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001201 Signature += "__Imp";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001202 } else {
1203 // Copy the tied operand.
1204 assert(Tie->first>Tie->second && "Tied operand preceeds its target!");
1205 CaseOS << " Inst.addOperand(Inst.getOperand("
1206 << Tie->second << "));\n";
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001207 Signature += "__Tie" + utostr(Tie->second);
Chris Lattnerdda855d2010-11-02 21:49:44 +00001208 }
Daniel Dunbar3b6910d2010-02-12 01:46:54 +00001209 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001210
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001211 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001212
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001213 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001214 if (!GeneratedFns.insert(Signature).second)
1215 continue;
1216
Chris Lattnerdda855d2010-11-02 21:49:44 +00001217 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001218 OS << " " << Signature << ",\n";
1219
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001220 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001221 CvtOS << CaseOS.str();
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001222 CvtOS << " return;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001223 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001224
1225 // Finish the convert function.
1226
1227 CvtOS << " }\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001228 CvtOS << "}\n\n";
1229
1230 // Finish the enum, and drop the convert function after it.
1231
1232 OS << " NumConversionVariants\n";
1233 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001234
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001235 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001236}
1237
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001238/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1239static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1240 std::vector<ClassInfo*> &Infos,
1241 raw_ostream &OS) {
1242 OS << "namespace {\n\n";
1243
1244 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1245 << "/// instruction matching.\n";
1246 OS << "enum MatchClassKind {\n";
1247 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001248 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001249 ie = Infos.end(); it != ie; ++it) {
1250 ClassInfo &CI = **it;
1251 OS << " " << CI.Name << ", // ";
1252 if (CI.Kind == ClassInfo::Token) {
1253 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001254 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001255 if (!CI.ValueName.empty())
1256 OS << "register class '" << CI.ValueName << "'\n";
1257 else
1258 OS << "derived register class\n";
1259 } else {
1260 OS << "user defined class '" << CI.ValueName << "'\n";
1261 }
1262 }
1263 OS << " NumMatchClassKinds\n";
1264 OS << "};\n\n";
1265
1266 OS << "}\n\n";
1267}
1268
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001269/// EmitClassifyOperand - Emit the function to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001270static void EmitClassifyOperand(AsmMatcherInfo &Info,
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001271 raw_ostream &OS) {
Chris Lattner98986712010-01-14 22:21:20 +00001272 OS << "static MatchClassKind ClassifyOperand(MCParsedAsmOperand *GOp) {\n"
Chris Lattner02bcbc92010-11-01 01:37:30 +00001273 << " " << Info.Target.getName() << "Operand &Operand = *("
1274 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001275
1276 // Classify tokens.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001277 OS << " if (Operand.isToken())\n";
1278 OS << " return MatchTokenString(Operand.getToken());\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001279
1280 // Classify registers.
1281 //
1282 // FIXME: Don't hardcode isReg, getReg.
1283 OS << " if (Operand.isReg()) {\n";
1284 OS << " switch (Operand.getReg()) {\n";
1285 OS << " default: return InvalidMatchClass;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001286 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001287 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1288 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001289 OS << " case " << Info.Target.getName() << "::"
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001290 << it->first->getName() << ": return " << it->second->Name << ";\n";
1291 OS << " }\n";
1292 OS << " }\n\n";
1293
1294 // Classify user defined operands.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001295 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001296 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001297 ClassInfo &CI = **it;
1298
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001299 if (!CI.isUserClass())
1300 continue;
1301
1302 OS << " // '" << CI.ClassName << "' class";
1303 if (!CI.SuperClasses.empty()) {
1304 OS << ", subclass of ";
1305 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i) {
1306 if (i) OS << ", ";
1307 OS << "'" << CI.SuperClasses[i]->ClassName << "'";
1308 assert(CI < *CI.SuperClasses[i] && "Invalid class relation!");
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001309 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001310 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001311 OS << "\n";
1312
1313 OS << " if (Operand." << CI.PredicateMethod << "()) {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001314
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001315 // Validate subclass relationships.
1316 if (!CI.SuperClasses.empty()) {
1317 for (unsigned i = 0, e = CI.SuperClasses.size(); i != e; ++i)
1318 OS << " assert(Operand." << CI.SuperClasses[i]->PredicateMethod
1319 << "() && \"Invalid class relationship!\");\n";
1320 }
1321
1322 OS << " return " << CI.Name << ";\n";
1323 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001324 }
1325 OS << " return InvalidMatchClass;\n";
1326 OS << "}\n\n";
1327}
1328
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001329/// EmitIsSubclass - Emit the subclass predicate function.
1330static void EmitIsSubclass(CodeGenTarget &Target,
1331 std::vector<ClassInfo*> &Infos,
1332 raw_ostream &OS) {
1333 OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1334 OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n";
1335 OS << " if (A == B)\n";
1336 OS << " return true;\n\n";
1337
1338 OS << " switch (A) {\n";
1339 OS << " default:\n";
1340 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001341 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001342 ie = Infos.end(); it != ie; ++it) {
1343 ClassInfo &A = **it;
1344
1345 if (A.Kind != ClassInfo::Token) {
1346 std::vector<StringRef> SuperClasses;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001347 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001348 ie = Infos.end(); it != ie; ++it) {
1349 ClassInfo &B = **it;
1350
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001351 if (&A != &B && A.isSubsetOf(B))
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001352 SuperClasses.push_back(B.Name);
1353 }
1354
1355 if (SuperClasses.empty())
1356 continue;
1357
1358 OS << "\n case " << A.Name << ":\n";
1359
1360 if (SuperClasses.size() == 1) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001361 OS << " return B == " << SuperClasses.back() << ";\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001362 continue;
1363 }
1364
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001365 OS << " switch (B) {\n";
1366 OS << " default: return false;\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001367 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001368 OS << " case " << SuperClasses[i] << ": return true;\n";
1369 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001370 }
1371 }
1372 OS << " }\n";
1373 OS << "}\n\n";
1374}
1375
Chris Lattner70add882009-08-08 20:02:57 +00001376
1377
Daniel Dunbar245f0582009-08-08 21:22:41 +00001378/// EmitMatchTokenString - Emit the function to match a token string to the
1379/// appropriate match class value.
1380static void EmitMatchTokenString(CodeGenTarget &Target,
1381 std::vector<ClassInfo*> &Infos,
1382 raw_ostream &OS) {
1383 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001384 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001385 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001386 ie = Infos.end(); it != ie; ++it) {
1387 ClassInfo &CI = **it;
1388
1389 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001390 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1391 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001392 }
1393
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001394 OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001395
Chris Lattner5845e5c2010-09-06 02:01:51 +00001396 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001397
1398 OS << " return InvalidMatchClass;\n";
1399 OS << "}\n\n";
1400}
Chris Lattner70add882009-08-08 20:02:57 +00001401
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001402/// EmitMatchRegisterName - Emit the function to match a string to the target
1403/// specific register enum.
1404static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1405 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001406 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001407 std::vector<StringMatcher::StringPair> Matches;
Daniel Dunbar245f0582009-08-08 21:22:41 +00001408 for (unsigned i = 0, e = Target.getRegisters().size(); i != e; ++i) {
1409 const CodeGenRegister &Reg = Target.getRegisters()[i];
Daniel Dunbar22be5222009-07-17 18:51:11 +00001410 if (Reg.TheDef->getValueAsString("AsmName").empty())
1411 continue;
1412
Chris Lattner5845e5c2010-09-06 02:01:51 +00001413 Matches.push_back(StringMatcher::StringPair(
1414 Reg.TheDef->getValueAsString("AsmName"),
1415 "return " + utostr(i + 1) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001416 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001417
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001418 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001419
Chris Lattner5845e5c2010-09-06 02:01:51 +00001420 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001421
Daniel Dunbar245f0582009-08-08 21:22:41 +00001422 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001423 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001424}
Daniel Dunbara027d222009-07-31 02:32:59 +00001425
Daniel Dunbar54074b52010-07-19 05:44:09 +00001426/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1427/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001428static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001429 raw_ostream &OS) {
1430 OS << "// Flags for subtarget features that participate in "
1431 << "instruction matching.\n";
1432 OS << "enum SubtargetFeatureFlag {\n";
1433 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1434 it = Info.SubtargetFeatures.begin(),
1435 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1436 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001437 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001438 }
1439 OS << " Feature_None = 0\n";
1440 OS << "};\n\n";
1441}
1442
1443/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1444/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001445static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001446 raw_ostream &OS) {
1447 std::string ClassName =
1448 Info.AsmParser->getValueAsString("AsmParserClassName");
1449
Chris Lattner02bcbc92010-11-01 01:37:30 +00001450 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
1451 << "ComputeAvailableFeatures(const " << Info.Target.getName()
Daniel Dunbar54074b52010-07-19 05:44:09 +00001452 << "Subtarget *Subtarget) const {\n";
1453 OS << " unsigned Features = 0;\n";
1454 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1455 it = Info.SubtargetFeatures.begin(),
1456 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1457 SubtargetFeatureInfo &SFI = *it->second;
1458 OS << " if (" << SFI.TheDef->getValueAsString("CondString")
1459 << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001460 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001461 }
1462 OS << " return Features;\n";
1463 OS << "}\n\n";
1464}
1465
Chris Lattner6fa152c2010-10-30 20:15:02 +00001466static std::string GetAliasRequiredFeatures(Record *R,
1467 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001468 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001469 std::string Result;
1470 unsigned NumFeatures = 0;
1471 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001472 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Chris Lattner693173f2010-10-30 19:23:13 +00001473
Chris Lattner4a74ee72010-11-01 02:09:21 +00001474 if (F == 0)
1475 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1476 "' is not marked as an AssemblerPredicate!");
1477
1478 if (NumFeatures)
1479 Result += '|';
1480
1481 Result += F->getEnumName();
1482 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001483 }
1484
1485 if (NumFeatures > 1)
1486 Result = '(' + Result + ')';
1487 return Result;
1488}
1489
Chris Lattner674c1dc2010-10-30 17:36:36 +00001490/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001491/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001492static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Chris Lattner674c1dc2010-10-30 17:36:36 +00001493 std::vector<Record*> Aliases =
1494 Records.getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001495 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001496
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001497 OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, "
1498 "unsigned Features) {\n";
1499
Chris Lattner4fd32c62010-10-30 18:56:12 +00001500 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1501 // iteration order of the map is stable.
1502 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
1503
Chris Lattner674c1dc2010-10-30 17:36:36 +00001504 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1505 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001506 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001507 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001508
1509 // Process each alias a "from" mnemonic at a time, building the code executed
1510 // by the string remapper.
1511 std::vector<StringMatcher::StringPair> Cases;
1512 for (std::map<std::string, std::vector<Record*> >::iterator
1513 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1514 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001515 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001516
1517 // Loop through each alias and emit code that handles each case. If there
1518 // are two instructions without predicates, emit an error. If there is one,
1519 // emit it last.
1520 std::string MatchCode;
1521 int AliasWithNoPredicate = -1;
Chris Lattner4fd32c62010-10-30 18:56:12 +00001522
Chris Lattner693173f2010-10-30 19:23:13 +00001523 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1524 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001525 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Chris Lattner693173f2010-10-30 19:23:13 +00001526
1527 // If this unconditionally matches, remember it for later and diagnose
1528 // duplicates.
1529 if (FeatureMask.empty()) {
1530 if (AliasWithNoPredicate != -1) {
1531 // We can't have two aliases from the same mnemonic with no predicate.
1532 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1533 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001534 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001535 }
1536
1537 AliasWithNoPredicate = i;
1538 continue;
1539 }
1540
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001541 if (!MatchCode.empty())
1542 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001543 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1544 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001545 }
1546
Chris Lattner693173f2010-10-30 19:23:13 +00001547 if (AliasWithNoPredicate != -1) {
1548 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001549 if (!MatchCode.empty())
1550 MatchCode += "else\n ";
1551 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001552 }
1553
1554 MatchCode += "return;";
1555
1556 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001557 }
1558
Chris Lattner674c1dc2010-10-30 17:36:36 +00001559
1560 StringMatcher("Mnemonic", Cases, OS).Emit();
Chris Lattner7fd44892010-10-30 18:48:18 +00001561 OS << "}\n";
1562
1563 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001564}
1565
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001566void AsmMatcherEmitter::run(raw_ostream &OS) {
1567 CodeGenTarget Target;
1568 Record *AsmParser = Target.getAsmParser();
1569 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
1570
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001571 // Compute the information on the instructions to match.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001572 AsmMatcherInfo Info(AsmParser, Target);
1573 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00001574
Daniel Dunbare1f6de32010-02-02 23:46:36 +00001575 // Sort the instruction table using the partial order on classes. We use
1576 // stable_sort to ensure that ambiguous instructions are still
1577 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001578 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
1579 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001580
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001581 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001582 for (std::vector<MatchableInfo*>::iterator
1583 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001584 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00001585 (*it)->dump();
1586 });
Daniel Dunbara027d222009-07-31 02:32:59 +00001587
Chris Lattner22bc5c42010-11-01 05:06:45 +00001588 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001589 DEBUG_WITH_TYPE("ambiguous_instrs", {
1590 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001591 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00001592 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001593 MatchableInfo &A = *Info.Matchables[i];
1594 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001595
Chris Lattner87410362010-09-06 20:21:47 +00001596 if (A.CouldMatchAmiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001597 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001598 A.dump();
1599 errs() << "\nis incomparable with:\n";
1600 B.dump();
1601 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00001602 ++NumAmbiguous;
1603 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00001604 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001605 }
Chris Lattner87410362010-09-06 20:21:47 +00001606 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001607 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00001608 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00001609 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001610
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001611 // Write the output.
1612
1613 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
1614
Chris Lattner0692ee62010-09-06 19:11:01 +00001615 // Information for the class declaration.
1616 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
1617 OS << "#undef GET_ASSEMBLER_HEADER\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001618 OS << " // This should be included into the middle of the declaration of \n";
1619 OS << " // your subclasses implementation of TargetAsmParser.\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001620 OS << " unsigned ComputeAvailableFeatures(const " <<
1621 Target.getName() << "Subtarget *Subtarget) const;\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001622 OS << " enum MatchResultTy {\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001623 OS << " Match_Success, Match_MnemonicFail, Match_InvalidOperand,\n";
1624 OS << " Match_MissingFeature\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00001625 OS << " };\n";
Jim Grosbachbb168242010-10-08 18:13:57 +00001626 OS << " MatchResultTy MatchInstructionImpl(const "
1627 << "SmallVectorImpl<MCParsedAsmOperand*>"
Chris Lattnerce4a3352010-09-06 22:11:18 +00001628 << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00001629 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
1630
Jim Grosbacha7c78222010-10-29 22:13:48 +00001631
1632
1633
Chris Lattner0692ee62010-09-06 19:11:01 +00001634 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
1635 OS << "#undef GET_REGISTER_MATCHER\n\n";
1636
Daniel Dunbar54074b52010-07-19 05:44:09 +00001637 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001638 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001639
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001640 // Emit the function to match a register name to number.
1641 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00001642
1643 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001644
Chris Lattner0692ee62010-09-06 19:11:01 +00001645
1646 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
1647 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001648
Chris Lattner7fd44892010-10-30 18:48:18 +00001649 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001650 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Chris Lattner7fd44892010-10-30 18:48:18 +00001651
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001652 // Generate the unified function to convert operands into an MCInst.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001653 EmitConvertToMCInst(Target, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001654
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001655 // Emit the enumeration for classes which participate in matching.
1656 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00001657
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001658 // Emit the routine to match token strings to their match class.
1659 EmitMatchTokenString(Target, Info.Classes, OS);
1660
1661 // Emit the routine to classify an operand.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001662 EmitClassifyOperand(Info, OS);
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001663
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001664 // Emit the subclass predicate routine.
1665 EmitIsSubclass(Target, Info.Classes, OS);
1666
Daniel Dunbar54074b52010-07-19 05:44:09 +00001667 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001668 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00001669
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001670
1671 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001672 for (std::vector<MatchableInfo*>::const_iterator it =
1673 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001674 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00001675 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00001676
1677
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001678 // Emit the static match table; unused classes get initalized to 0 which is
1679 // guaranteed to be InvalidMatchClass.
1680 //
1681 // FIXME: We can reduce the size of this table very easily. First, we change
1682 // it so that store the kinds in separate bit-fields for each index, which
1683 // only needs to be the max width used for classes at that index (we also need
1684 // to reject based on this during classification). If we then make sure to
1685 // order the match kinds appropriately (putting mnemonics last), then we
1686 // should only end up using a few bits for each class, especially the ones
1687 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00001688 OS << "namespace {\n";
1689 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001690 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001691 OS << " const char *Mnemonic;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001692 OS << " ConversionKind ConvertFn;\n";
1693 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001694 OS << " unsigned RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001695 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001696
Chris Lattner2b1f9432010-09-06 21:22:45 +00001697 OS << "// Predicate for searching for an opcode.\n";
1698 OS << " struct LessOpcode {\n";
1699 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
1700 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
1701 OS << " }\n";
1702 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
1703 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
1704 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00001705 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
1706 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
1707 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00001708 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001709
Chris Lattner96352e52010-09-06 21:08:38 +00001710 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001711
Chris Lattner96352e52010-09-06 21:08:38 +00001712 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00001713 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001714
Chris Lattner22bc5c42010-11-01 05:06:45 +00001715 for (std::vector<MatchableInfo*>::const_iterator it =
1716 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001717 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001718 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001719
Chris Lattner96352e52010-09-06 21:08:38 +00001720 OS << " { " << Target.getName() << "::" << II.InstrName
Chris Lattnerd19ec052010-11-02 17:30:52 +00001721 << ", \"" << II.Mnemonic << "\""
Chris Lattner96352e52010-09-06 21:08:38 +00001722 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00001723 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1724 MatchableInfo::Operand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001725
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001726 if (i) OS << ", ";
1727 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001728 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00001729 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001730
Daniel Dunbar54074b52010-07-19 05:44:09 +00001731 // Write the required features mask.
1732 if (!II.RequiredFeatures.empty()) {
1733 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
1734 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001735 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00001736 }
1737 } else
1738 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001739
Daniel Dunbar54074b52010-07-19 05:44:09 +00001740 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001741 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001742
Chris Lattner96352e52010-09-06 21:08:38 +00001743 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001744
Chris Lattner96352e52010-09-06 21:08:38 +00001745 // Finally, build the match function.
1746 OS << Target.getName() << ClassName << "::MatchResultTy "
1747 << Target.getName() << ClassName << "::\n"
1748 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
1749 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001750 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001751
1752 // Emit code to get the available features.
1753 OS << " // Get the current feature set.\n";
1754 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
1755
Chris Lattner674c1dc2010-10-30 17:36:36 +00001756 OS << " // Get the instruction mnemonic, which is the first token.\n";
1757 OS << " StringRef Mnemonic = ((" << Target.getName()
1758 << "Operand*)Operands[0])->getToken();\n\n";
1759
Chris Lattner7fd44892010-10-30 18:48:18 +00001760 if (HasMnemonicAliases) {
1761 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
1762 OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
1763 }
Chris Lattner674c1dc2010-10-30 17:36:36 +00001764
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001765 // Emit code to compute the class list for this operand vector.
1766 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001767 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
1768 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
1769 OS << " return Match_InvalidOperand;\n";
1770 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001771
1772 OS << " // Compute the class list for this operand vector.\n";
1773 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001774 OS << " for (unsigned i = 1, e = Operands.size(); i != e; ++i) {\n";
1775 OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001776
1777 OS << " // Check for invalid operands before matching.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001778 OS << " if (Classes[i-1] == InvalidMatchClass) {\n";
1779 OS << " ErrorInfo = i;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001780 OS << " return Match_InvalidOperand;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001781 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001782 OS << " }\n\n";
1783
1784 OS << " // Mark unused classes.\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00001785 OS << " for (unsigned i = Operands.size()-1, e = " << MaxNumOperands << "; "
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001786 << "i != e; ++i)\n";
1787 OS << " Classes[i] = InvalidMatchClass;\n\n";
1788
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001789 OS << " // Some state to try to produce better error messages.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001790 OS << " bool HadMatchOtherThanFeatures = false;\n\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001791 OS << " // Set ErrorInfo to the operand that mismatches if it is \n";
1792 OS << " // wrong for all instances of the instruction.\n";
1793 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001794
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001795 // Emit code to search the table.
1796 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001797 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
1798 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00001799 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001800
Chris Lattnera008e8a2010-09-06 21:54:15 +00001801 OS << " // Return a more specific error code if no mnemonics match.\n";
1802 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
1803 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001804
Chris Lattner2b1f9432010-09-06 21:22:45 +00001805 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00001806 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00001807 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001808
Gabor Greife53ee3b2010-09-07 06:06:06 +00001809 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00001810 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001811
Daniel Dunbar54074b52010-07-19 05:44:09 +00001812 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001813 OS << " bool OperandsValid = true;\n";
1814 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
1815 OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n";
1816 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001817 OS << " // If this operand is broken for all of the instances of this\n";
1818 OS << " // mnemonic, keep track of it so we can report loc info.\n";
1819 OS << " if (it == MnemonicRange.first || ErrorInfo == i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001820 OS << " ErrorInfo = i+1;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00001821 OS << " else\n";
1822 OS << " ErrorInfo = ~0U;";
Chris Lattnerce4a3352010-09-06 22:11:18 +00001823 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
1824 OS << " OperandsValid = false;\n";
1825 OS << " break;\n";
1826 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001827
Chris Lattnerce4a3352010-09-06 22:11:18 +00001828 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00001829
1830 // Emit check that the required features are available.
1831 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
1832 << "!= it->RequiredFeatures) {\n";
1833 OS << " HadMatchOtherThanFeatures = true;\n";
1834 OS << " continue;\n";
1835 OS << " }\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001836
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001837 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00001838 OS << " ConvertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
1839
1840 // Call the post-processing function, if used.
1841 std::string InsnCleanupFn =
1842 AsmParser->getValueAsString("AsmParserInstCleanup");
1843 if (!InsnCleanupFn.empty())
1844 OS << " " << InsnCleanupFn << "(Inst);\n";
1845
Chris Lattner79ed3f72010-09-06 19:22:17 +00001846 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001847 OS << " }\n\n";
1848
Chris Lattnerec6789f2010-09-06 20:08:02 +00001849 OS << " // Okay, we had no match. Try to return a useful error code.\n";
1850 OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00001851 OS << " return Match_InvalidOperand;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00001852 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001853
Chris Lattner0692ee62010-09-06 19:11:01 +00001854 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001855}