blob: 80467ff3dc7e19b634398a65966809eb200fa548 [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
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000011// assembly operands in the MCInst structures. It also emits a matcher for
12// custom operand parsing.
13//
14// Converting assembly operands into MCInst structures
15// ---------------------------------------------------
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000016//
Daniel Dunbar20927f22009-08-07 08:26:05 +000017// The input to the target specific matcher is a list of literal tokens and
18// operands. The target specific parser should generally eliminate any syntax
19// which is not relevant for matching; for example, comma tokens should have
20// already been consumed and eliminated by the parser. Most instructions will
21// end up with a single literal token (the instruction name) and some number of
22// operands.
23//
24// Some example inputs, for X86:
25// 'addl' (immediate ...) (register ...)
26// 'add' (immediate ...) (memory ...)
Jim Grosbacha7c78222010-10-29 22:13:48 +000027// 'call' '*' %epc
Daniel Dunbar20927f22009-08-07 08:26:05 +000028//
29// The assembly matcher is responsible for converting this input into a precise
30// machine instruction (i.e., an instruction with a well defined encoding). This
31// mapping has several properties which complicate matching:
32//
33// - It may be ambiguous; many architectures can legally encode particular
34// variants of an instruction in different ways (for example, using a smaller
35// encoding for small immediates). Such ambiguities should never be
36// arbitrarily resolved by the assembler, the assembler is always responsible
37// for choosing the "best" available instruction.
38//
39// - It may depend on the subtarget or the assembler context. Instructions
40// which are invalid for the current mode, but otherwise unambiguous (e.g.,
41// an SSE instruction in a file being assembled for i486) should be accepted
42// and rejected by the assembler front end. However, if the proper encoding
43// for an instruction is dependent on the assembler context then the matcher
44// is responsible for selecting the correct machine instruction for the
45// current mode.
46//
47// The core matching algorithm attempts to exploit the regularity in most
48// instruction sets to quickly determine the set of possibly matching
49// instructions, and the simplify the generated code. Additionally, this helps
50// to ensure that the ambiguities are intentionally resolved by the user.
51//
52// The matching is divided into two distinct phases:
53//
54// 1. Classification: Each operand is mapped to the unique set which (a)
55// contains it, and (b) is the largest such subset for which a single
56// instruction could match all members.
57//
58// For register classes, we can generate these subgroups automatically. For
59// arbitrary operands, we expect the user to define the classes and their
60// relations to one another (for example, 8-bit signed immediates as a
61// subset of 32-bit immediates).
62//
63// By partitioning the operands in this way, we guarantee that for any
64// tuple of classes, any single instruction must match either all or none
65// of the sets of operands which could classify to that tuple.
66//
67// In addition, the subset relation amongst classes induces a partial order
68// on such tuples, which we use to resolve ambiguities.
69//
Daniel Dunbar20927f22009-08-07 08:26:05 +000070// 2. The input can now be treated as a tuple of classes (static tokens are
71// simple singleton sets). Each such tuple should generally map to a single
72// instruction (we currently ignore cases where this isn't true, whee!!!),
73// which we can emit a simple matcher for.
74//
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000075// Custom Operand Parsing
76// ----------------------
77//
78// Some targets need a custom way to parse operands, some specific instructions
79// can contain arguments that can represent processor flags and other kinds of
80// identifiers that need to be mapped to specific valeus in the final encoded
81// instructions. The target specific custom operand parsing works in the
82// following way:
83//
84// 1. A operand match table is built, each entry contains a mnemonic, an
85// operand class, a mask for all operand positions for that same
86// class/mnemonic and target features to be checked while trying to match.
87//
88// 2. The operand matcher will try every possible entry with the same
89// mnemonic and will check if the target feature for this mnemonic also
90// matches. After that, if the operand to be matched has its index
Chris Lattner7a2bdde2011-04-15 05:18:47 +000091// present in the mask, a successful match occurs. Otherwise, fallback
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000092// to the regular operand parsing.
93//
94// 3. For a match success, each operand class that has a 'ParserMethod'
95// becomes part of a switch from where the custom method is called.
96//
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000097//===----------------------------------------------------------------------===//
98
99#include "AsmMatcherEmitter.h"
100#include "CodeGenTarget.h"
Chris Lattner5845e5c2010-09-06 02:01:51 +0000101#include "StringMatcher.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000102#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc07bd402010-11-04 02:11:18 +0000103#include "llvm/ADT/PointerUnion.h"
Chris Lattner1de88232010-11-01 01:47:07 +0000104#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000105#include "llvm/ADT/SmallVector.h"
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000106#include "llvm/ADT/STLExtras.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000107#include "llvm/ADT/StringExtras.h"
108#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000109#include "llvm/Support/Debug.h"
Craig Topper655b8de2012-02-05 07:21:30 +0000110#include "llvm/Support/ErrorHandling.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +0000111#include "llvm/TableGen/Error.h"
112#include "llvm/TableGen/Record.h"
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000113#include <map>
114#include <set>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000115using namespace llvm;
116
Daniel Dunbar27249152009-08-07 20:33:39 +0000117static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000118MatchPrefix("match-prefix", cl::init(""),
119 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000120
Daniel Dunbar20927f22009-08-07 08:26:05 +0000121namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000122class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000123struct SubtargetFeatureInfo;
124
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000125/// ClassInfo - Helper class for storing the information about a particular
126/// class of operands which can be matched.
127struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000128 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000129 /// Invalid kind, for use as a sentinel value.
130 Invalid = 0,
131
132 /// The class for a particular token.
133 Token,
134
135 /// The (first) register class, subsequent register classes are
136 /// RegisterClass0+1, and so on.
137 RegisterClass0,
138
139 /// The (first) user defined class, subsequent user defined classes are
140 /// UserClass0+1, and so on.
141 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000142 };
143
144 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
145 /// N) for the Nth user defined class.
146 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000147
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000148 /// SuperClasses - The super classes of this class. Note that for simplicities
149 /// sake user operands only record their immediate super class, while register
150 /// operands include all superclasses.
151 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000152
Daniel Dunbar6745d422009-08-09 05:18:30 +0000153 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000154 std::string Name;
155
Daniel Dunbar6745d422009-08-09 05:18:30 +0000156 /// ClassName - The unadorned generic name for this class (e.g., Token).
157 std::string ClassName;
158
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000159 /// ValueName - The name of the value this class represents; for a token this
160 /// is the literal token string, for an operand it is the TableGen class (or
161 /// empty if this is a derived class).
162 std::string ValueName;
163
164 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000165 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000166 std::string PredicateMethod;
167
168 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000169 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000170 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000171
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000172 /// ParserMethod - The name of the operand method to do a target specific
173 /// parsing on the operand.
174 std::string ParserMethod;
175
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000176 /// For register classes, the records for all the registers in this class.
177 std::set<Record*> Registers;
178
179public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000180 /// isRegisterClass() - Check if this is a register class.
181 bool isRegisterClass() const {
182 return Kind >= RegisterClass0 && Kind < UserClass0;
183 }
184
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000185 /// isUserClass() - Check if this is a user defined class.
186 bool isUserClass() const {
187 return Kind >= UserClass0;
188 }
189
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000190 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
191 /// are related if they are in the same class hierarchy.
192 bool isRelatedTo(const ClassInfo &RHS) const {
193 // Tokens are only related to tokens.
194 if (Kind == Token || RHS.Kind == Token)
195 return Kind == Token && RHS.Kind == Token;
196
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000197 // Registers classes are only related to registers classes, and only if
198 // their intersection is non-empty.
199 if (isRegisterClass() || RHS.isRegisterClass()) {
200 if (!isRegisterClass() || !RHS.isRegisterClass())
201 return false;
202
203 std::set<Record*> Tmp;
204 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000205 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000206 RHS.Registers.begin(), RHS.Registers.end(),
207 II);
208
209 return !Tmp.empty();
210 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000211
212 // Otherwise we have two users operands; they are related if they are in the
213 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000214 //
215 // FIXME: This is an oversimplification, they should only be related if they
216 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000217 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
218 const ClassInfo *Root = this;
219 while (!Root->SuperClasses.empty())
220 Root = Root->SuperClasses.front();
221
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000222 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000223 while (!RHSRoot->SuperClasses.empty())
224 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000225
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000226 return Root == RHSRoot;
227 }
228
Jim Grosbacha7c78222010-10-29 22:13:48 +0000229 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000230 bool isSubsetOf(const ClassInfo &RHS) const {
231 // This is a subset of RHS if it is the same class...
232 if (this == &RHS)
233 return true;
234
235 // ... or if any of its super classes are a subset of RHS.
236 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
237 ie = SuperClasses.end(); it != ie; ++it)
238 if ((*it)->isSubsetOf(RHS))
239 return true;
240
241 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000242 }
243
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000244 /// operator< - Compare two classes.
245 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000246 if (this == &RHS)
247 return false;
248
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000249 // Unrelated classes can be ordered by kind.
250 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000251 return Kind < RHS.Kind;
252
253 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000254 case Invalid:
Craig Topper655b8de2012-02-05 07:21:30 +0000255 llvm_unreachable("Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000256
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000257 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000258 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000259 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000260 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000261 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000262 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000263
264 // Otherwise, order by name to ensure we have a total ordering.
265 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000266 }
267 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000268};
269
Chris Lattner22bc5c42010-11-01 05:06:45 +0000270/// MatchableInfo - Helper class for storing the necessary information for an
271/// instruction or alias which is capable of being matched.
272struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000273 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000274 /// Token - This is the token that the operand came from.
275 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000276
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000277 /// The unique class instance this operand should match.
278 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000279
Chris Lattner567820c2010-11-04 01:42:59 +0000280 /// The operand name this is, if anything.
281 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000282
283 /// The suboperand index within SrcOpName, or -1 for the entire operand.
284 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000285
Devang Patel63faf822012-01-07 01:33:34 +0000286 /// Register record if this token is singleton register.
287 Record *SingletonReg;
288
Jim Grosbachf35307c2012-01-24 21:06:59 +0000289 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1),
Devang Patel63faf822012-01-07 01:33:34 +0000290 SingletonReg(0) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000291 };
Bob Wilson828295b2011-01-26 21:26:19 +0000292
Chris Lattner1d13bda2010-11-04 00:43:46 +0000293 /// ResOperand - This represents a single operand in the result instruction
294 /// generated by the match. In cases (like addressing modes) where a single
295 /// assembler operand expands to multiple MCOperands, this represents the
296 /// single assembler operand, not the MCOperand.
297 struct ResOperand {
298 enum {
299 /// RenderAsmOperand - This represents an operand result that is
300 /// generated by calling the render method on the assembly operand. The
301 /// corresponding AsmOperand is specified by AsmOperandNum.
302 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000303
Chris Lattner1d13bda2010-11-04 00:43:46 +0000304 /// TiedOperand - This represents a result operand that is a duplicate of
305 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000306 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000307
Chris Lattner98c870f2010-11-06 19:25:43 +0000308 /// ImmOperand - This represents an immediate value that is dumped into
309 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000310 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000311
Chris Lattner90fd7972010-11-06 19:57:21 +0000312 /// RegOperand - This represents a fixed register that is dumped in.
313 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000314 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000315
Chris Lattner1d13bda2010-11-04 00:43:46 +0000316 union {
317 /// This is the operand # in the AsmOperands list that this should be
318 /// copied from.
319 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000320
Chris Lattner1d13bda2010-11-04 00:43:46 +0000321 /// TiedOperandNum - This is the (earlier) result operand that should be
322 /// copied from.
323 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000324
Chris Lattner98c870f2010-11-06 19:25:43 +0000325 /// ImmVal - This is the immediate value added to the instruction.
326 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000327
Chris Lattner90fd7972010-11-06 19:57:21 +0000328 /// Register - This is the register record.
329 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000330 };
Bob Wilson828295b2011-01-26 21:26:19 +0000331
Bob Wilsona49c7df2011-01-26 19:44:55 +0000332 /// MINumOperands - The number of MCInst operands populated by this
333 /// operand.
334 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000335
Bob Wilsona49c7df2011-01-26 19:44:55 +0000336 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000337 ResOperand X;
338 X.Kind = RenderAsmOperand;
339 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000340 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000341 return X;
342 }
Bob Wilson828295b2011-01-26 21:26:19 +0000343
Bob Wilsona49c7df2011-01-26 19:44:55 +0000344 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000345 ResOperand X;
346 X.Kind = TiedOperand;
347 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000348 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000349 return X;
350 }
Bob Wilson828295b2011-01-26 21:26:19 +0000351
Bob Wilsona49c7df2011-01-26 19:44:55 +0000352 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000353 ResOperand X;
354 X.Kind = ImmOperand;
355 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000356 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000357 return X;
358 }
Bob Wilson828295b2011-01-26 21:26:19 +0000359
Bob Wilsona49c7df2011-01-26 19:44:55 +0000360 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000361 ResOperand X;
362 X.Kind = RegOperand;
363 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000364 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000365 return X;
366 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000367 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000368
Devang Patel56315d32012-01-10 17:50:43 +0000369 /// AsmVariantID - Target's assembly syntax variant no.
370 int AsmVariantID;
371
Chris Lattner3b5aec62010-11-02 17:34:28 +0000372 /// TheDef - This is the definition of the instruction or InstAlias that this
373 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000374 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000375
Chris Lattnerc07bd402010-11-04 02:11:18 +0000376 /// DefRec - This is the definition that it came from.
377 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000378
Chris Lattner662e5a32010-11-06 07:14:44 +0000379 const CodeGenInstruction *getResultInst() const {
380 if (DefRec.is<const CodeGenInstruction*>())
381 return DefRec.get<const CodeGenInstruction*>();
382 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
383 }
Bob Wilson828295b2011-01-26 21:26:19 +0000384
Chris Lattner1d13bda2010-11-04 00:43:46 +0000385 /// ResOperands - This is the operand list that should be built for the result
386 /// MCInst.
387 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000388
389 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000390 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000391 std::string AsmString;
392
Chris Lattnerd19ec052010-11-02 17:30:52 +0000393 /// Mnemonic - This is the first token of the matched instruction, its
394 /// mnemonic.
395 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000396
Chris Lattner3116fef2010-11-02 01:03:43 +0000397 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000398 /// annotated with a class and where in the OperandList they were defined.
399 /// This directly corresponds to the tokenized AsmString after the mnemonic is
400 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000401 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402
Daniel Dunbar54074b52010-07-19 05:44:09 +0000403 /// Predicates - The required subtarget features to match this instruction.
404 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
405
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000406 /// ConversionFnKind - The enum value which is passed to the generated
407 /// ConvertToMCInst to convert parsed operands into an MCInst for this
408 /// function.
409 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000410
Chris Lattner22bc5c42010-11-01 05:06:45 +0000411 MatchableInfo(const CodeGenInstruction &CGI)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000412 : AsmVariantID(0), TheDef(CGI.TheDef), DefRec(&CGI),
Devang Patel56315d32012-01-10 17:50:43 +0000413 AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000414 }
415
Chris Lattner22bc5c42010-11-01 05:06:45 +0000416 MatchableInfo(const CodeGenInstAlias *Alias)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000417 : AsmVariantID(0), TheDef(Alias->TheDef), DefRec(Alias),
Devang Patel56315d32012-01-10 17:50:43 +0000418 AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000419 }
Bob Wilson828295b2011-01-26 21:26:19 +0000420
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000421 void Initialize(const AsmMatcherInfo &Info,
Jim Grosbachf35307c2012-01-24 21:06:59 +0000422 SmallPtrSet<Record*, 16> &SingletonRegisters,
Devang Patel63faf822012-01-07 01:33:34 +0000423 int AsmVariantNo, std::string &RegisterPrefix);
Bob Wilson828295b2011-01-26 21:26:19 +0000424
Chris Lattner22bc5c42010-11-01 05:06:45 +0000425 /// Validate - Return true if this matchable is a valid thing to match against
426 /// and perform a bunch of validity checking.
427 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000428
Jim Grosbachf35307c2012-01-24 21:06:59 +0000429 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Patel63faf822012-01-07 01:33:34 +0000430 /// if present, from specified token.
431 void
432 extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info,
433 std::string &RegisterPrefix);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000434
Bob Wilsona49c7df2011-01-26 19:44:55 +0000435 /// FindAsmOperand - Find the AsmOperand with the specified name and
436 /// suboperand index.
437 int FindAsmOperand(StringRef N, int SubOpIdx) const {
438 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
439 if (N == AsmOperands[i].SrcOpName &&
440 SubOpIdx == AsmOperands[i].SubOpIdx)
441 return i;
442 return -1;
443 }
Bob Wilson828295b2011-01-26 21:26:19 +0000444
Bob Wilsona49c7df2011-01-26 19:44:55 +0000445 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
446 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000447 int FindAsmOperandNamed(StringRef N) const {
448 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
449 if (N == AsmOperands[i].SrcOpName)
450 return i;
451 return -1;
452 }
Bob Wilson828295b2011-01-26 21:26:19 +0000453
Chris Lattner41409852010-11-06 07:31:43 +0000454 void BuildInstructionResultOperands();
455 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000456
Chris Lattner22bc5c42010-11-01 05:06:45 +0000457 /// operator< - Compare two matchables.
458 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000459 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000460 if (Mnemonic != RHS.Mnemonic)
461 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000462
Chris Lattner3116fef2010-11-02 01:03:43 +0000463 if (AsmOperands.size() != RHS.AsmOperands.size())
464 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000465
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000466 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000467 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000468 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
469 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000470 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000471 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000472 return false;
473 }
474
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000475 return false;
476 }
477
Bob Wilson1f64ac42011-01-26 21:26:21 +0000478 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000479 /// ambiguously match the same set of operands as \arg RHS (without being a
480 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000481 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000482 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000483 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000484 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000485
Daniel Dunbar2b544812009-08-09 06:05:33 +0000486 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000487 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000488 return false;
489
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000490 // Otherwise, make sure the ordering of the two instructions is unambiguous
491 // by checking that either (a) a token or operand kind discriminates them,
492 // or (b) the ordering among equivalent kinds is consistent.
493
Daniel Dunbar2b544812009-08-09 06:05:33 +0000494 // Tokens and operand kinds are unambiguous (assuming a correct target
495 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000496 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
497 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
498 AsmOperands[i].Class->Kind == ClassInfo::Token)
499 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
500 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000501 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000502
Daniel Dunbar2b544812009-08-09 06:05:33 +0000503 // Otherwise, this operand could commute if all operands are equivalent, or
504 // there is a pair of operands that compare less than and a pair that
505 // compare greater than.
506 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000507 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
508 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000509 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000510 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000511 HasGT = true;
512 }
513
514 return !(HasLT ^ HasGT);
515 }
516
Daniel Dunbar20927f22009-08-07 08:26:05 +0000517 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000518
Chris Lattnerd19ec052010-11-02 17:30:52 +0000519private:
520 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000521};
522
Daniel Dunbar54074b52010-07-19 05:44:09 +0000523/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
524/// feature which participates in instruction matching.
525struct SubtargetFeatureInfo {
526 /// \brief The predicate record for this feature.
527 Record *TheDef;
528
529 /// \brief An unique index assigned to represent this feature.
530 unsigned Index;
531
Chris Lattner0aed1e72010-10-30 20:07:57 +0000532 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000533
Daniel Dunbar54074b52010-07-19 05:44:09 +0000534 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000535 std::string getEnumName() const {
536 return "Feature_" + TheDef->getName();
537 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000538};
539
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000540struct OperandMatchEntry {
541 unsigned OperandMask;
542 MatchableInfo* MI;
543 ClassInfo *CI;
544
545 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
546 unsigned opMask) {
547 OperandMatchEntry X;
548 X.OperandMask = opMask;
549 X.CI = ci;
550 X.MI = mi;
551 return X;
552 }
553};
554
555
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000556class AsmMatcherInfo {
557public:
Chris Lattner67db8832010-12-13 00:23:57 +0000558 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000559 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000560
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000561 /// The tablegen AsmParser record.
562 Record *AsmParser;
563
Chris Lattner02bcbc92010-11-01 01:37:30 +0000564 /// Target - The target information.
565 CodeGenTarget &Target;
566
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000567 /// The classes which are needed for matching.
568 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000569
Chris Lattner22bc5c42010-11-01 05:06:45 +0000570 /// The information on the matchables to match.
571 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000572
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000573 /// Info for custom matching operands by user defined methods.
574 std::vector<OperandMatchEntry> OperandMatchInfo;
575
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000576 /// Map of Register records to their class information.
577 std::map<Record*, ClassInfo*> RegisterClasses;
578
Daniel Dunbar54074b52010-07-19 05:44:09 +0000579 /// Map of Predicate records to their subtarget information.
580 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000581
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000582private:
583 /// Map of token to class information which has already been constructed.
584 std::map<std::string, ClassInfo*> TokenClasses;
585
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000586 /// Map of RegisterClass records to their class information.
587 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000588
Daniel Dunbar338825c2009-08-10 18:41:10 +0000589 /// Map of AsmOperandClass records to their class information.
590 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000591
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000592private:
593 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000594 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000595
596 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000597 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
Jim Grosbach48c1f842011-10-28 22:32:53 +0000598 int SubOpIdx);
599 ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000600
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000601 /// BuildRegisterClasses - Build the ClassInfo* instances for register
602 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000603 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000604
605 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
606 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000607 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000608
Bob Wilsona49c7df2011-01-26 19:44:55 +0000609 void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
610 unsigned AsmOpIdx);
611 void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000612 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000613
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000614public:
Bob Wilson828295b2011-01-26 21:26:19 +0000615 AsmMatcherInfo(Record *AsmParser,
616 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000617 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000618
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000619 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000620 void BuildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000621
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000622 /// BuildOperandMatchInfo - Build the necessary information to handle user
623 /// defined operand parsing methods.
624 void BuildOperandMatchInfo();
625
Chris Lattner6fa152c2010-10-30 20:15:02 +0000626 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
627 /// given operand.
628 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
629 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
630 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
631 SubtargetFeatures.find(Def);
632 return I == SubtargetFeatures.end() ? 0 : I->second;
633 }
Chris Lattner67db8832010-12-13 00:23:57 +0000634
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000635 RecordKeeper &getRecords() const {
636 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000637 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000638};
639
Daniel Dunbar20927f22009-08-07 08:26:05 +0000640}
641
Chris Lattner22bc5c42010-11-01 05:06:45 +0000642void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000643 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000644
Chris Lattner3116fef2010-11-02 01:03:43 +0000645 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000646 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000647 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000648 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000649 }
650}
651
Chris Lattner22bc5c42010-11-01 05:06:45 +0000652void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000653 SmallPtrSet<Record*, 16> &SingletonRegisters,
654 int AsmVariantNo, std::string &RegisterPrefix) {
Devang Patel56315d32012-01-10 17:50:43 +0000655 AsmVariantID = AsmVariantNo;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000656 AsmString =
Devang Patel63faf822012-01-07 01:33:34 +0000657 CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
Bob Wilson828295b2011-01-26 21:26:19 +0000658
Chris Lattnerd19ec052010-11-02 17:30:52 +0000659 TokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000660
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000661 // Compute the require features.
662 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
663 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
664 if (SubtargetFeatureInfo *Feature =
665 Info.getSubtargetFeature(Predicates[i]))
666 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000667
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000668 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000669 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Devang Patel63faf822012-01-07 01:33:34 +0000670 extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix);
671 if (Record *Reg = AsmOperands[i].SingletonReg)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000672 SingletonRegisters.insert(Reg);
673 }
674}
675
Chris Lattnerd19ec052010-11-02 17:30:52 +0000676/// TokenizeAsmString - Tokenize a simplified assembly string.
677void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
678 StringRef String = AsmString;
679 unsigned Prev = 0;
680 bool InTok = true;
681 for (unsigned i = 0, e = String.size(); i != e; ++i) {
682 switch (String[i]) {
683 case '[':
684 case ']':
685 case '*':
686 case '!':
687 case ' ':
688 case '\t':
689 case ',':
690 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000691 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000692 InTok = false;
693 }
694 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000695 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000696 Prev = i + 1;
697 break;
698
699 case '\\':
700 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000701 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000702 InTok = false;
703 }
704 ++i;
705 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000706 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000707 Prev = i + 1;
708 break;
709
710 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000711 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000712 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000713 InTok = false;
714 }
Bob Wilson828295b2011-01-26 21:26:19 +0000715
Chris Lattner7ad31472010-11-06 22:06:03 +0000716 // If this isn't "${", treat like a normal token.
717 if (i + 1 == String.size() || String[i + 1] != '{') {
718 Prev = i;
719 break;
720 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000721
722 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
723 assert(End != String.end() && "Missing brace in operand reference!");
724 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000725 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000726 Prev = EndPos + 1;
727 i = EndPos;
728 break;
729 }
730
731 case '.':
732 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000733 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000734 Prev = i;
735 InTok = true;
736 break;
737
738 default:
739 InTok = true;
740 }
741 }
742 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000743 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000744
Chris Lattnerd19ec052010-11-02 17:30:52 +0000745 // The first token of the instruction is the mnemonic, which must be a
746 // simple string, not a $foo variable or a singleton register.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000747 if (AsmOperands.empty())
748 throw TGError(TheDef->getLoc(),
749 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000750 Mnemonic = AsmOperands[0].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000751 // FIXME : Check and raise an error if it is a register.
Devang Patelb78307f2012-01-07 01:22:23 +0000752 if (Mnemonic[0] == '$')
Chris Lattnerd19ec052010-11-02 17:30:52 +0000753 throw TGError(TheDef->getLoc(),
754 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000755
Chris Lattnerd19ec052010-11-02 17:30:52 +0000756 // Remove the first operand, it is tracked in the mnemonic field.
757 AsmOperands.erase(AsmOperands.begin());
758}
759
Chris Lattner22bc5c42010-11-01 05:06:45 +0000760bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
761 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000762 if (AsmString.empty())
763 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000764
Chris Lattner22bc5c42010-11-01 05:06:45 +0000765 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000766 // isCodeGenOnly if they are pseudo instructions.
767 if (AsmString.find('\n') != std::string::npos)
768 throw TGError(TheDef->getLoc(),
769 "multiline instruction is not valid for the asmparser, "
770 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000771
Chris Lattner4164f6b2010-11-01 04:44:29 +0000772 // Remove comments from the asm string. We know that the asmstring only
773 // has one line.
774 if (!CommentDelimiter.empty() &&
775 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
776 throw TGError(TheDef->getLoc(),
777 "asmstring for instruction has comment character in it, "
778 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000779
Chris Lattner22bc5c42010-11-01 05:06:45 +0000780 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000781 // handle, the target should be refactored to use operands instead of
782 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000783 //
784 // Also, check for instructions which reference the operand multiple times;
785 // this implies a constraint we would not honor.
786 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000787 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
788 StringRef Tok = AsmOperands[i].Token;
789 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000790 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000791 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000792 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000793
Chris Lattner22bc5c42010-11-01 05:06:45 +0000794 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000795 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000796 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000797 if (!Hack)
798 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000799 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000800 "' can never be matched!");
801 // FIXME: Should reject these. The ARM backend hits this with $lane in a
802 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000803 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000804 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000805 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000806 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000807 });
808 return false;
809 }
810 }
Bob Wilson828295b2011-01-26 21:26:19 +0000811
Chris Lattner5bc93872010-11-01 04:34:44 +0000812 return true;
813}
814
Jim Grosbachf35307c2012-01-24 21:06:59 +0000815/// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Pateld06b01c2012-01-09 21:30:46 +0000816/// if present, from specified token.
Devang Patel63faf822012-01-07 01:33:34 +0000817void MatchableInfo::
Jim Grosbachf35307c2012-01-24 21:06:59 +0000818extractSingletonRegisterForAsmOperand(unsigned OperandNo,
Devang Pateld06b01c2012-01-09 21:30:46 +0000819 const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000820 std::string &RegisterPrefix) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000821 StringRef Tok = AsmOperands[OperandNo].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000822 if (RegisterPrefix.empty()) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000823 std::string LoweredTok = Tok.lower();
824 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
825 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Devang Patel63faf822012-01-07 01:33:34 +0000826 return;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000827 }
Bob Wilson828295b2011-01-26 21:26:19 +0000828
Devang Patel63faf822012-01-07 01:33:34 +0000829 if (!Tok.startswith(RegisterPrefix))
830 return;
831
832 StringRef RegName = Tok.substr(RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000833 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
Devang Pateld06b01c2012-01-09 21:30:46 +0000834 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000835
Chris Lattner1de88232010-11-01 01:47:07 +0000836 // If there is no register prefix (i.e. "%" in "%eax"), then this may
837 // be some random non-register token, just ignore it.
Devang Patel63faf822012-01-07 01:33:34 +0000838 return;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000839}
840
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000841static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000842 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000843
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000844 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
845 switch (*it) {
846 case '*': Res += "_STAR_"; break;
847 case '%': Res += "_PCT_"; break;
848 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000849 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000850 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000851 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000852 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000853 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000854 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000855 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000856 }
857 }
858
859 return Res;
860}
861
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000862ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000863 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000864
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000865 if (!Entry) {
866 Entry = new ClassInfo();
867 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000868 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000869 Entry->Name = "MCK_" + getEnumNameForToken(Token);
870 Entry->ValueName = Token;
871 Entry->PredicateMethod = "<invalid>";
872 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000873 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000874 Classes.push_back(Entry);
875 }
876
877 return Entry;
878}
879
880ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000881AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
882 int SubOpIdx) {
883 Record *Rec = OI.Rec;
884 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000885 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +0000886 return getOperandClass(Rec, SubOpIdx);
887}
Bob Wilsona49c7df2011-01-26 19:44:55 +0000888
Jim Grosbach48c1f842011-10-28 22:32:53 +0000889ClassInfo *
890AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000891 if (Rec->isSubClassOf("RegisterOperand")) {
892 // RegisterOperand may have an associated ParserMatchClass. If it does,
893 // use it, else just fall back to the underlying register class.
894 const RecordVal *R = Rec->getValue("ParserMatchClass");
895 if (R == 0 || R->getValue() == 0)
896 throw "Record `" + Rec->getName() +
897 "' does not have a ParserMatchClass!\n";
898
David Greene05bce0b2011-07-29 22:43:06 +0000899 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000900 Record *MatchClass = DI->getDef();
901 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
902 return CI;
903 }
904
905 // No custom match class. Just use the register class.
906 Record *ClassRec = Rec->getValueAsDef("RegClass");
907 if (!ClassRec)
908 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
909 "' has no associated register class!\n");
910 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
911 return CI;
912 throw TGError(Rec->getLoc(), "register class has no class info!");
913 }
914
915
Bob Wilsona49c7df2011-01-26 19:44:55 +0000916 if (Rec->isSubClassOf("RegisterClass")) {
917 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000918 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000919 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000920 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000921
Bob Wilsona49c7df2011-01-26 19:44:55 +0000922 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
923 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000924 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
925 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000926
Bob Wilsona49c7df2011-01-26 19:44:55 +0000927 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000928}
929
Chris Lattner1de88232010-11-01 01:47:07 +0000930void AsmMatcherInfo::
931BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000932 const std::vector<CodeGenRegister*> &Registers =
933 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000934 ArrayRef<CodeGenRegisterClass*> RegClassList =
935 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000936
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000937 // The register sets used for matching.
938 std::set< std::set<Record*> > RegisterSets;
939
Jim Grosbacha7c78222010-10-29 22:13:48 +0000940 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000941 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +0000942 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000943 RegisterSets.insert(std::set<Record*>(
944 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000945
946 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000947 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
948 ie = SingletonRegisters.end(); it != ie; ++it) {
949 Record *Rec = *it;
950 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
951 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000952
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000953 // Introduce derived sets where necessary (when a register does not determine
954 // a unique register set class), and build the mapping of registers to the set
955 // they should classify to.
956 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000957 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000958 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000959 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000960 // Compute the intersection of all sets containing this register.
961 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000962
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000963 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
964 ie = RegisterSets.end(); it != ie; ++it) {
965 if (!it->count(CGR.TheDef))
966 continue;
967
968 if (ContainingSet.empty()) {
969 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000970 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000971 }
Bob Wilson828295b2011-01-26 21:26:19 +0000972
Chris Lattnerec6f0962010-11-02 18:10:06 +0000973 std::set<Record*> Tmp;
974 std::swap(Tmp, ContainingSet);
975 std::insert_iterator< std::set<Record*> > II(ContainingSet,
976 ContainingSet.begin());
977 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000978 }
979
980 if (!ContainingSet.empty()) {
981 RegisterSets.insert(ContainingSet);
982 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
983 }
984 }
985
986 // Construct the register classes.
987 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
988 unsigned Index = 0;
989 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
990 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
991 ClassInfo *CI = new ClassInfo();
992 CI->Kind = ClassInfo::RegisterClass0 + Index;
993 CI->ClassName = "Reg" + utostr(Index);
994 CI->Name = "MCK_Reg" + utostr(Index);
995 CI->ValueName = "";
996 CI->PredicateMethod = ""; // unused
997 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000998 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000999 Classes.push_back(CI);
1000 RegisterSetClasses.insert(std::make_pair(*it, CI));
1001 }
1002
1003 // Find the superclasses; we could compute only the subgroup lattice edges,
1004 // but there isn't really a point.
1005 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
1006 ie = RegisterSets.end(); it != ie; ++it) {
1007 ClassInfo *CI = RegisterSetClasses[*it];
1008 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
1009 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001010 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001011 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
1012 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
1013 }
1014
1015 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001016 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001017 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001018 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001019 // Def will be NULL for non-user defined register classes.
1020 Record *Def = RC.getDef();
1021 if (!Def)
1022 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001023 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1024 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001025 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001026 CI->ClassName = RC.getName();
1027 CI->Name = "MCK_" + RC.getName();
1028 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001029 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001030 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001031
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001032 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001033 }
1034
1035 // Populate the map for individual registers.
1036 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1037 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001038 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001039
1040 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001041 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1042 ie = SingletonRegisters.end(); it != ie; ++it) {
1043 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001044 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001045 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001046
Chris Lattner1de88232010-11-01 01:47:07 +00001047 if (CI->ValueName.empty()) {
1048 CI->ClassName = Rec->getName();
1049 CI->Name = "MCK_" + Rec->getName();
1050 CI->ValueName = Rec->getName();
1051 } else
1052 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001053 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001054}
1055
Chris Lattner02bcbc92010-11-01 01:37:30 +00001056void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001057 std::vector<Record*> AsmOperands =
1058 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001059
1060 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001061 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001062 ie = AsmOperands.end(); it != ie; ++it)
1063 AsmOperandClasses[*it] = new ClassInfo();
1064
Daniel Dunbar338825c2009-08-10 18:41:10 +00001065 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001066 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001067 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001068 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001069 CI->Kind = ClassInfo::UserClass0 + Index;
1070
David Greene05bce0b2011-07-29 22:43:06 +00001071 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001072 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001073 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001074 if (!DI) {
1075 PrintError((*it)->getLoc(), "Invalid super class reference!");
1076 continue;
1077 }
1078
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001079 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1080 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001081 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001082 else
1083 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001084 }
1085 CI->ClassName = (*it)->getValueAsString("Name");
1086 CI->Name = "MCK_" + CI->ClassName;
1087 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001088
1089 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001090 Init *PMName = (*it)->getValueInit("PredicateMethod");
1091 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001092 CI->PredicateMethod = SI->getValue();
1093 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001094 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001095 "Unexpected PredicateMethod field!");
1096 CI->PredicateMethod = "is" + CI->ClassName;
1097 }
1098
1099 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001100 Init *RMName = (*it)->getValueInit("RenderMethod");
1101 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001102 CI->RenderMethod = SI->getValue();
1103 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001104 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001105 "Unexpected RenderMethod field!");
1106 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1107 }
1108
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001109 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001110 Init *PRMName = (*it)->getValueInit("ParserMethod");
1111 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001112 CI->ParserMethod = SI->getValue();
1113
Daniel Dunbar338825c2009-08-10 18:41:10 +00001114 AsmOperandClasses[*it] = CI;
1115 Classes.push_back(CI);
1116 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001117}
1118
Bob Wilson828295b2011-01-26 21:26:19 +00001119AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1120 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001121 RecordKeeper &records)
Devang Patel63faf822012-01-07 01:33:34 +00001122 : Records(records), AsmParser(asmParser), Target(target) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001123}
1124
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001125/// BuildOperandMatchInfo - Build the necessary information to handle user
1126/// defined operand parsing methods.
1127void AsmMatcherInfo::BuildOperandMatchInfo() {
1128
1129 /// Map containing a mask with all operands indicies that can be found for
1130 /// that class inside a instruction.
1131 std::map<ClassInfo*, unsigned> OpClassMask;
1132
1133 for (std::vector<MatchableInfo*>::const_iterator it =
1134 Matchables.begin(), ie = Matchables.end();
1135 it != ie; ++it) {
1136 MatchableInfo &II = **it;
1137 OpClassMask.clear();
1138
1139 // Keep track of all operands of this instructions which belong to the
1140 // same class.
1141 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1142 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1143 if (Op.Class->ParserMethod.empty())
1144 continue;
1145 unsigned &OperandMask = OpClassMask[Op.Class];
1146 OperandMask |= (1 << i);
1147 }
1148
1149 // Generate operand match info for each mnemonic/operand class pair.
1150 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1151 iie = OpClassMask.end(); iit != iie; ++iit) {
1152 unsigned OpMask = iit->second;
1153 ClassInfo *CI = iit->first;
1154 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1155 }
1156 }
1157}
1158
Chris Lattner02bcbc92010-11-01 01:37:30 +00001159void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001160 // Build information about all of the AssemblerPredicates.
1161 std::vector<Record*> AllPredicates =
1162 Records.getAllDerivedDefinitions("Predicate");
1163 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1164 Record *Pred = AllPredicates[i];
1165 // Ignore predicates that are not intended for the assembler.
1166 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1167 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001168
Chris Lattner4164f6b2010-11-01 04:44:29 +00001169 if (Pred->getName().empty())
1170 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001171
Chris Lattner0aed1e72010-10-30 20:07:57 +00001172 unsigned FeatureNo = SubtargetFeatures.size();
1173 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1174 assert(FeatureNo < 32 && "Too many subtarget features!");
1175 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001176
Chris Lattner39ee0362010-10-31 19:10:56 +00001177 // Parse the instructions; we need to do this first so that we can gather the
1178 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001179 SmallPtrSet<Record*, 16> SingletonRegisters;
Devang Patel0dbcada2012-01-09 19:13:28 +00001180 unsigned VariantCount = Target.getAsmParserVariantCount();
1181 for (unsigned VC = 0; VC != VariantCount; ++VC) {
1182 Record *AsmVariant = Target.getAsmParserVariant(VC);
1183 std::string CommentDelimiter = AsmVariant->getValueAsString("CommentDelimiter");
1184 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1185 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
Jim Grosbachf35307c2012-01-24 21:06:59 +00001186
Devang Patel0dbcada2012-01-09 19:13:28 +00001187 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1188 E = Target.inst_end(); I != E; ++I) {
1189 const CodeGenInstruction &CGI = **I;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001190
Devang Patel0dbcada2012-01-09 19:13:28 +00001191 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1192 // filter the set of instructions we consider.
1193 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
1194 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001195
Devang Patel0dbcada2012-01-09 19:13:28 +00001196 // Ignore "codegen only" instructions.
1197 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1198 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001199
Devang Patel0dbcada2012-01-09 19:13:28 +00001200 // Validate the operand list to ensure we can handle this instruction.
1201 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1202 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
1203
1204 // Validate tied operands.
1205 if (OI.getTiedRegister() != -1) {
1206 // If we have a tied operand that consists of multiple MCOperands,
1207 // reject it. We reject aliases and ignore instructions for now.
1208 if (OI.MINumOperands != 1) {
1209 // FIXME: Should reject these. The ARM backend hits this with $lane
1210 // in a bunch of instructions. It is unclear what the right answer is.
1211 DEBUG({
1212 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1213 << "ignoring instruction with multi-operand tied operand '"
1214 << OI.Name << "'\n";
1215 });
1216 continue;
1217 }
1218 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001219 }
Jim Grosbachf35307c2012-01-24 21:06:59 +00001220
Devang Patel0dbcada2012-01-09 19:13:28 +00001221 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001222
Devang Patel0dbcada2012-01-09 19:13:28 +00001223 II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001224
Devang Patel0dbcada2012-01-09 19:13:28 +00001225 // Ignore instructions which shouldn't be matched and diagnose invalid
1226 // instruction definitions with an error.
1227 if (!II->Validate(CommentDelimiter, true))
1228 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001229
Devang Patel0dbcada2012-01-09 19:13:28 +00001230 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1231 //
1232 // FIXME: This is a total hack.
1233 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1234 StringRef(II->TheDef->getName()).endswith("_Int"))
1235 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001236
Devang Patel0dbcada2012-01-09 19:13:28 +00001237 Matchables.push_back(II.take());
Chris Lattner1d13bda2010-11-04 00:43:46 +00001238 }
Jim Grosbachf35307c2012-01-24 21:06:59 +00001239
Devang Patel0dbcada2012-01-09 19:13:28 +00001240 // Parse all of the InstAlias definitions and stick them in the list of
1241 // matchables.
1242 std::vector<Record*> AllInstAliases =
1243 Records.getAllDerivedDefinitions("InstAlias");
1244 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
1245 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001246
Devang Patel0dbcada2012-01-09 19:13:28 +00001247 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1248 // filter the set of instruction aliases we consider, based on the target
1249 // instruction.
1250 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1251 MatchPrefix))
1252 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001253
Devang Patel0dbcada2012-01-09 19:13:28 +00001254 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001255
Devang Patel0dbcada2012-01-09 19:13:28 +00001256 II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001257
Devang Patel0dbcada2012-01-09 19:13:28 +00001258 // Validate the alias definitions.
1259 II->Validate(CommentDelimiter, false);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001260
Devang Patel0dbcada2012-01-09 19:13:28 +00001261 Matchables.push_back(II.take());
1262 }
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001263 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001264
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001265 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001266 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001267
1268 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001269 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001270
Chris Lattner0bb780c2010-11-04 00:57:06 +00001271 // Build the information about matchables, now that we have fully formed
1272 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001273 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1274 ie = Matchables.end(); it != ie; ++it) {
1275 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001276
Chris Lattnere206fcf2010-09-06 21:01:37 +00001277 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001278 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1279 // don't precompute the loop bound.
1280 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001281 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001282 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001283
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001284 // Check for singleton registers.
Devang Patel63faf822012-01-07 01:33:34 +00001285 if (Record *RegRecord = II->AsmOperands[i].SingletonReg) {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001286 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001287 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1288 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001289 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001290 }
1291
Daniel Dunbar20927f22009-08-07 08:26:05 +00001292 // Check for simple tokens.
1293 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001294 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001295 continue;
1296 }
1297
Chris Lattner7ad31472010-11-06 22:06:03 +00001298 if (Token.size() > 1 && isdigit(Token[1])) {
1299 Op.Class = getTokenClass(Token);
1300 continue;
1301 }
Bob Wilson828295b2011-01-26 21:26:19 +00001302
Chris Lattnerc07bd402010-11-04 02:11:18 +00001303 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001304 StringRef OperandName;
1305 if (Token[1] == '{')
1306 OperandName = Token.substr(2, Token.size() - 3);
1307 else
1308 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001309
Chris Lattnerc07bd402010-11-04 02:11:18 +00001310 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001311 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001312 else
Chris Lattner225549f2010-11-06 06:39:47 +00001313 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001314 }
Bob Wilson828295b2011-01-26 21:26:19 +00001315
Chris Lattner41409852010-11-06 07:31:43 +00001316 if (II->DefRec.is<const CodeGenInstruction*>())
1317 II->BuildInstructionResultOperands();
1318 else
1319 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001320 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001321
Jim Grosbacha66512e2011-12-06 23:43:54 +00001322 // Process token alias definitions and set up the associated superclass
1323 // information.
1324 std::vector<Record*> AllTokenAliases =
1325 Records.getAllDerivedDefinitions("TokenAlias");
1326 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1327 Record *Rec = AllTokenAliases[i];
1328 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1329 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1330 FromClass->SuperClasses.push_back(ToClass);
1331 }
1332
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001333 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001334 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001335}
1336
Chris Lattner0bb780c2010-11-04 00:57:06 +00001337/// BuildInstructionOperandReference - The specified operand is a reference to a
1338/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1339void AsmMatcherInfo::
1340BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001341 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001342 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001343 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1344 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001345 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001346
Chris Lattner662e5a32010-11-06 07:14:44 +00001347 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001348 unsigned Idx;
1349 if (!Operands.hasOperandNamed(OperandName, Idx))
1350 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1351 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001352
Bob Wilsona49c7df2011-01-26 19:44:55 +00001353 // If the instruction operand has multiple suboperands, but the parser
1354 // match class for the asm operand is still the default "ImmAsmOperand",
1355 // then handle each suboperand separately.
1356 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1357 Record *Rec = Operands[Idx].Rec;
1358 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1359 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1360 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1361 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1362 StringRef Token = Op->Token; // save this in case Op gets moved
1363 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1364 MatchableInfo::AsmOperand NewAsmOp(Token);
1365 NewAsmOp.SubOpIdx = SI;
1366 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1367 }
1368 // Replace Op with first suboperand.
1369 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1370 Op->SubOpIdx = 0;
1371 }
1372 }
1373
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001374 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001375 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001376
1377 // If the named operand is tied, canonicalize it to the untied operand.
1378 // For example, something like:
1379 // (outs GPR:$dst), (ins GPR:$src)
1380 // with an asmstring of
1381 // "inc $src"
1382 // we want to canonicalize to:
1383 // "inc $dst"
1384 // so that we know how to provide the $dst operand when filling in the result.
1385 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001386 if (OITied != -1) {
1387 // The tied operand index is an MIOperand index, find the operand that
1388 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001389 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1390 OperandName = Operands[Idx.first].Name;
1391 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001392 }
Bob Wilson828295b2011-01-26 21:26:19 +00001393
Bob Wilsona49c7df2011-01-26 19:44:55 +00001394 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001395}
1396
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001397/// BuildAliasOperandReference - When parsing an operand reference out of the
1398/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1399/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001400void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1401 StringRef OperandName,
1402 MatchableInfo::AsmOperand &Op) {
1403 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001404
Chris Lattnerc07bd402010-11-04 02:11:18 +00001405 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001406 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001407 if (CGA.ResultOperands[i].isRecord() &&
1408 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001409 // It's safe to go with the first one we find, because CodeGenInstAlias
1410 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001411 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001412 // Use the match class from the Alias definition, not the
1413 // destination instruction, as we may have an immediate that's
1414 // being munged by the match class.
1415 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001416 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001417 Op.SrcOpName = OperandName;
1418 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001419 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001420
1421 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1422 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001423}
1424
Chris Lattner41409852010-11-06 07:31:43 +00001425void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001426 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001427
Chris Lattner662e5a32010-11-06 07:14:44 +00001428 // Loop over all operands of the result instruction, determining how to
1429 // populate them.
1430 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1431 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001432
1433 // If this is a tied operand, just copy from the previously handled operand.
1434 int TiedOp = OpInfo.getTiedRegister();
1435 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001436 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001437 continue;
1438 }
Bob Wilson828295b2011-01-26 21:26:19 +00001439
Bob Wilsona49c7df2011-01-26 19:44:55 +00001440 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001441 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001442 if (OpInfo.Name.empty() || SrcOperand == -1)
1443 throw TGError(TheDef->getLoc(), "Instruction '" +
1444 TheDef->getName() + "' has operand '" + OpInfo.Name +
1445 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001446
Bob Wilsona49c7df2011-01-26 19:44:55 +00001447 // Check if the one AsmOperand populates the entire operand.
1448 unsigned NumOperands = OpInfo.MINumOperands;
1449 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1450 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001451 continue;
1452 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001453
1454 // Add a separate ResOperand for each suboperand.
1455 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1456 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1457 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1458 "unexpected AsmOperands for suboperands");
1459 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1460 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001461 }
1462}
1463
Chris Lattner41409852010-11-06 07:31:43 +00001464void MatchableInfo::BuildAliasResultOperands() {
1465 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1466 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001467
Chris Lattner41409852010-11-06 07:31:43 +00001468 // Loop over all operands of the result instruction, determining how to
1469 // populate them.
1470 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001471 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001472 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001473 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001474
Chris Lattner41409852010-11-06 07:31:43 +00001475 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001476 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001477 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001478 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001479 continue;
1480 }
1481
Bob Wilsona49c7df2011-01-26 19:44:55 +00001482 // Handle all the suboperands for this operand.
1483 const std::string &OpName = OpInfo->Name;
1484 for ( ; AliasOpNo < LastOpNo &&
1485 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1486 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1487
1488 // Find out what operand from the asmparser that this MCInst operand
1489 // comes from.
1490 switch (CGA.ResultOperands[AliasOpNo].Kind) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001491 case CodeGenInstAlias::ResultOperand::K_Record: {
1492 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1493 int SrcOperand = FindAsmOperand(Name, SubIdx);
1494 if (SrcOperand == -1)
1495 throw TGError(TheDef->getLoc(), "Instruction '" +
1496 TheDef->getName() + "' has operand '" + OpName +
1497 "' that doesn't appear in asm string!");
1498 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1499 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1500 NumOperands));
1501 break;
1502 }
1503 case CodeGenInstAlias::ResultOperand::K_Imm: {
1504 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1505 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1506 break;
1507 }
1508 case CodeGenInstAlias::ResultOperand::K_Reg: {
1509 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1510 ResOperands.push_back(ResOperand::getRegOp(Reg));
1511 break;
1512 }
1513 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001514 }
Chris Lattner41409852010-11-06 07:31:43 +00001515 }
1516}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001517
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001518static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001519 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001520 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001521 // Write the convert function to a separate stream, so we can drop it after
1522 // the enum.
1523 std::string ConvertFnBody;
1524 raw_string_ostream CvtOS(ConvertFnBody);
1525
Daniel Dunbar20927f22009-08-07 08:26:05 +00001526 // Function we have already generated.
1527 std::set<std::string> GeneratedFns;
1528
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001529 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001530 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1531 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001532 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001533 << " const SmallVectorImpl<MCParsedAsmOperand*"
1534 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001535 CvtOS << " Inst.setOpcode(Opcode);\n";
1536 CvtOS << " switch (Kind) {\n";
1537 CvtOS << " default:\n";
1538
1539 // Start the enum, which we will generate inline.
1540
Chris Lattnerd51257a2010-11-02 23:18:43 +00001541 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001542 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001543
Chris Lattner98986712010-01-14 22:21:20 +00001544 // TargetOperandClass - This is the target's operand class, like X86Operand.
1545 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001546
Chris Lattner22bc5c42010-11-01 05:06:45 +00001547 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001548 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001549 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001550
Daniel Dunbarcf120672011-02-04 17:12:15 +00001551 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001552 std::string AsmMatchConverter =
1553 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001554 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001555 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001556 II.ConversionFnKind = Signature;
1557
1558 // Check if we have already generated this signature.
1559 if (!GeneratedFns.insert(Signature).second)
1560 continue;
1561
1562 // If not, emit it now. Add to the enum list.
1563 OS << " " << Signature << ",\n";
1564
1565 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001566 CvtOS << " return " << AsmMatchConverter
1567 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001568 continue;
1569 }
1570
Daniel Dunbar20927f22009-08-07 08:26:05 +00001571 // Build the conversion function signature.
1572 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001573 std::string CaseBody;
1574 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001575
Chris Lattnerdda855d2010-11-02 21:49:44 +00001576 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001577 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1578 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001579
Chris Lattner1d13bda2010-11-04 00:43:46 +00001580 // Generate code to populate each result operand.
1581 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001582 case MatchableInfo::ResOperand::RenderAsmOperand: {
1583 // This comes from something we parsed.
1584 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001585
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001586 // Registers are always converted the same, don't duplicate the
1587 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001588 Signature += "__";
1589 if (Op.Class->isRegisterClass())
1590 Signature += "Reg";
1591 else
1592 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001593 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001594 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001595
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001596 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001597 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001598 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001599 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001600 }
Bob Wilson828295b2011-01-26 21:26:19 +00001601
Chris Lattner1d13bda2010-11-04 00:43:46 +00001602 case MatchableInfo::ResOperand::TiedOperand: {
1603 // If this operand is tied to a previous one, just copy the MCInst
1604 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001605 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001606 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001607 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001608 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1609 Signature += "__Tie" + utostr(TiedOp);
1610 break;
1611 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001612 case MatchableInfo::ResOperand::ImmOperand: {
1613 int64_t Val = OpInfo.ImmVal;
1614 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1615 Signature += "__imm" + itostr(Val);
1616 break;
1617 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001618 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001619 if (OpInfo.Register == 0) {
1620 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1621 Signature += "__reg0";
1622 } else {
1623 std::string N = getQualifiedName(OpInfo.Register);
1624 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1625 Signature += "__reg" + OpInfo.Register->getName();
1626 }
Bob Wilson828295b2011-01-26 21:26:19 +00001627 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001628 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001629 }
Bob Wilson828295b2011-01-26 21:26:19 +00001630
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001631 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001632
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001633 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001634 if (!GeneratedFns.insert(Signature).second)
1635 continue;
1636
Chris Lattnerdda855d2010-11-02 21:49:44 +00001637 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001638 OS << " " << Signature << ",\n";
1639
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001640 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001641 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001642 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001643 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001644
1645 // Finish the convert function.
1646
1647 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001648 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001649 CvtOS << "}\n\n";
1650
1651 // Finish the enum, and drop the convert function after it.
1652
1653 OS << " NumConversionVariants\n";
1654 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001655
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001656 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001657}
1658
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001659/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1660static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1661 std::vector<ClassInfo*> &Infos,
1662 raw_ostream &OS) {
1663 OS << "namespace {\n\n";
1664
1665 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1666 << "/// instruction matching.\n";
1667 OS << "enum MatchClassKind {\n";
1668 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001669 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001670 ie = Infos.end(); it != ie; ++it) {
1671 ClassInfo &CI = **it;
1672 OS << " " << CI.Name << ", // ";
1673 if (CI.Kind == ClassInfo::Token) {
1674 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001675 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001676 if (!CI.ValueName.empty())
1677 OS << "register class '" << CI.ValueName << "'\n";
1678 else
1679 OS << "derived register class\n";
1680 } else {
1681 OS << "user defined class '" << CI.ValueName << "'\n";
1682 }
1683 }
1684 OS << " NumMatchClassKinds\n";
1685 OS << "};\n\n";
1686
1687 OS << "}\n\n";
1688}
1689
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001690/// EmitValidateOperandClass - Emit the function to validate an operand class.
1691static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1692 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001693 OS << "static bool validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001694 << "MatchClassKind Kind) {\n";
1695 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001696 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001697
Kevin Enderby89381832011-07-15 18:30:43 +00001698 // The InvalidMatchClass is not to match any operand.
1699 OS << " if (Kind == InvalidMatchClass)\n";
1700 OS << " return false;\n\n";
1701
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001702 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001703 OS << " if (Operand.isToken())\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00001704 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind);"
1705 << "\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001706
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001707 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001708 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001709 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001710 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001711 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001712 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001713 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1714 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001715 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001716 << it->first->getName() << ": OpKind = " << it->second->Name
1717 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001718 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001719 OS << " return isSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001720 OS << " }\n\n";
1721
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001722 // Check the user classes. We don't care what order since we're only
1723 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001724 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001725 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001726 ClassInfo &CI = **it;
1727
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001728 if (!CI.isUserClass())
1729 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001730
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001731 OS << " // '" << CI.ClassName << "' class\n";
1732 OS << " if (Kind == " << CI.Name
1733 << " && Operand." << CI.PredicateMethod << "()) {\n";
1734 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001735 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001736 }
Bob Wilson828295b2011-01-26 21:26:19 +00001737
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001738 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001739 OS << "}\n\n";
1740}
1741
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001742/// EmitIsSubclass - Emit the subclass predicate function.
1743static void EmitIsSubclass(CodeGenTarget &Target,
1744 std::vector<ClassInfo*> &Infos,
1745 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001746 OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1747 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001748 OS << " if (A == B)\n";
1749 OS << " return true;\n\n";
1750
1751 OS << " switch (A) {\n";
1752 OS << " default:\n";
1753 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001754 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001755 ie = Infos.end(); it != ie; ++it) {
1756 ClassInfo &A = **it;
1757
Jim Grosbacha66512e2011-12-06 23:43:54 +00001758 std::vector<StringRef> SuperClasses;
1759 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
1760 ie = Infos.end(); it != ie; ++it) {
1761 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001762
Jim Grosbacha66512e2011-12-06 23:43:54 +00001763 if (&A != &B && A.isSubsetOf(B))
1764 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001765 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00001766
1767 if (SuperClasses.empty())
1768 continue;
1769
1770 OS << "\n case " << A.Name << ":\n";
1771
1772 if (SuperClasses.size() == 1) {
1773 OS << " return B == " << SuperClasses.back() << ";\n";
1774 continue;
1775 }
1776
1777 OS << " switch (B) {\n";
1778 OS << " default: return false;\n";
1779 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1780 OS << " case " << SuperClasses[i] << ": return true;\n";
1781 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001782 }
1783 OS << " }\n";
1784 OS << "}\n\n";
1785}
1786
Daniel Dunbar245f0582009-08-08 21:22:41 +00001787/// EmitMatchTokenString - Emit the function to match a token string to the
1788/// appropriate match class value.
1789static void EmitMatchTokenString(CodeGenTarget &Target,
1790 std::vector<ClassInfo*> &Infos,
1791 raw_ostream &OS) {
1792 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001793 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001794 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001795 ie = Infos.end(); it != ie; ++it) {
1796 ClassInfo &CI = **it;
1797
1798 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001799 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1800 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001801 }
1802
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001803 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001804
Chris Lattner5845e5c2010-09-06 02:01:51 +00001805 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001806
1807 OS << " return InvalidMatchClass;\n";
1808 OS << "}\n\n";
1809}
Chris Lattner70add882009-08-08 20:02:57 +00001810
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001811/// EmitMatchRegisterName - Emit the function to match a string to the target
1812/// specific register enum.
1813static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1814 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001815 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001816 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001817 const std::vector<CodeGenRegister*> &Regs =
1818 Target.getRegBank().getRegisters();
1819 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1820 const CodeGenRegister *Reg = Regs[i];
1821 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001822 continue;
1823
Chris Lattner5845e5c2010-09-06 02:01:51 +00001824 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001825 Reg->TheDef->getValueAsString("AsmName"),
1826 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001827 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001828
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001829 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001830
Chris Lattner5845e5c2010-09-06 02:01:51 +00001831 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001832
Daniel Dunbar245f0582009-08-08 21:22:41 +00001833 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001834 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001835}
Daniel Dunbara027d222009-07-31 02:32:59 +00001836
Daniel Dunbar54074b52010-07-19 05:44:09 +00001837/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1838/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001839static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001840 raw_ostream &OS) {
1841 OS << "// Flags for subtarget features that participate in "
1842 << "instruction matching.\n";
1843 OS << "enum SubtargetFeatureFlag {\n";
1844 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1845 it = Info.SubtargetFeatures.begin(),
1846 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1847 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001848 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001849 }
1850 OS << " Feature_None = 0\n";
1851 OS << "};\n\n";
1852}
1853
1854/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1855/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001856static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001857 raw_ostream &OS) {
1858 std::string ClassName =
1859 Info.AsmParser->getValueAsString("AsmParserClassName");
1860
Chris Lattner02bcbc92010-11-01 01:37:30 +00001861 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00001862 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001863 OS << " unsigned Features = 0;\n";
1864 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1865 it = Info.SubtargetFeatures.begin(),
1866 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1867 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00001868
1869 OS << " if (";
Evan Chengfbc38d22011-07-08 18:04:22 +00001870 std::string CondStorage = SFI.TheDef->getValueAsString("AssemblerCondString");
1871 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00001872 std::pair<StringRef,StringRef> Comma = Conds.split(',');
1873 bool First = true;
1874 do {
1875 if (!First)
1876 OS << " && ";
1877
1878 bool Neg = false;
1879 StringRef Cond = Comma.first;
1880 if (Cond[0] == '!') {
1881 Neg = true;
1882 Cond = Cond.substr(1);
1883 }
1884
1885 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
1886 if (Neg)
1887 OS << " == 0";
1888 else
1889 OS << " != 0";
1890 OS << ")";
1891
1892 if (Comma.second.empty())
1893 break;
1894
1895 First = false;
1896 Comma = Comma.second.split(',');
1897 } while (true);
1898
1899 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001900 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001901 }
1902 OS << " return Features;\n";
1903 OS << "}\n\n";
1904}
1905
Chris Lattner6fa152c2010-10-30 20:15:02 +00001906static std::string GetAliasRequiredFeatures(Record *R,
1907 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001908 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001909 std::string Result;
1910 unsigned NumFeatures = 0;
1911 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001912 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001913
Chris Lattner4a74ee72010-11-01 02:09:21 +00001914 if (F == 0)
1915 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1916 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001917
Chris Lattner4a74ee72010-11-01 02:09:21 +00001918 if (NumFeatures)
1919 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001920
Chris Lattner4a74ee72010-11-01 02:09:21 +00001921 Result += F->getEnumName();
1922 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001923 }
Bob Wilson828295b2011-01-26 21:26:19 +00001924
Chris Lattner693173f2010-10-30 19:23:13 +00001925 if (NumFeatures > 1)
1926 Result = '(' + Result + ')';
1927 return Result;
1928}
1929
Chris Lattner674c1dc2010-10-30 17:36:36 +00001930/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001931/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001932static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001933 // Ignore aliases when match-prefix is set.
1934 if (!MatchPrefix.empty())
1935 return false;
1936
Chris Lattner674c1dc2010-10-30 17:36:36 +00001937 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001938 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001939 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001940
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001941 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001942 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001943
Chris Lattner4fd32c62010-10-30 18:56:12 +00001944 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1945 // iteration order of the map is stable.
1946 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001947
Chris Lattner674c1dc2010-10-30 17:36:36 +00001948 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1949 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001950 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001951 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001952
1953 // Process each alias a "from" mnemonic at a time, building the code executed
1954 // by the string remapper.
1955 std::vector<StringMatcher::StringPair> Cases;
1956 for (std::map<std::string, std::vector<Record*> >::iterator
1957 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1958 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001959 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001960
1961 // Loop through each alias and emit code that handles each case. If there
1962 // are two instructions without predicates, emit an error. If there is one,
1963 // emit it last.
1964 std::string MatchCode;
1965 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001966
Chris Lattner693173f2010-10-30 19:23:13 +00001967 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1968 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001969 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001970
Chris Lattner693173f2010-10-30 19:23:13 +00001971 // If this unconditionally matches, remember it for later and diagnose
1972 // duplicates.
1973 if (FeatureMask.empty()) {
1974 if (AliasWithNoPredicate != -1) {
1975 // We can't have two aliases from the same mnemonic with no predicate.
1976 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1977 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001978 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001979 }
Bob Wilson828295b2011-01-26 21:26:19 +00001980
Chris Lattner693173f2010-10-30 19:23:13 +00001981 AliasWithNoPredicate = i;
1982 continue;
1983 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001984 if (R->getValueAsString("ToMnemonic") == I->first)
1985 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001986
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001987 if (!MatchCode.empty())
1988 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001989 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1990 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001991 }
Bob Wilson828295b2011-01-26 21:26:19 +00001992
Chris Lattner693173f2010-10-30 19:23:13 +00001993 if (AliasWithNoPredicate != -1) {
1994 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001995 if (!MatchCode.empty())
1996 MatchCode += "else\n ";
1997 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001998 }
Bob Wilson828295b2011-01-26 21:26:19 +00001999
Chris Lattner693173f2010-10-30 19:23:13 +00002000 MatchCode += "return;";
2001
2002 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00002003 }
Bob Wilson828295b2011-01-26 21:26:19 +00002004
Chris Lattner674c1dc2010-10-30 17:36:36 +00002005 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00002006 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00002007
Chris Lattner7fd44892010-10-30 18:48:18 +00002008 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00002009}
2010
Jim Grosbach194f3fa2012-03-01 17:30:35 +00002011static const char *getMinimalTypeForRange(uint64_t Range) {
2012 assert(Range < 0xFFFFFFFFULL && "Enum too large");
2013 if (Range > 0xFFFF)
2014 return "uint32_t";
2015 if (Range > 0xFF)
2016 return "uint16_t";
2017 return "uint8_t";
2018}
2019
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002020static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2021 const AsmMatcherInfo &Info, StringRef ClassName) {
2022 // Emit the static custom operand parsing table;
2023 OS << "namespace {\n";
2024 OS << " struct OperandMatchEntry {\n";
2025 OS << " const char *Mnemonic;\n";
2026 OS << " unsigned OperandMask;\n";
2027 OS << " MatchClassKind Class;\n";
2028 OS << " unsigned RequiredFeatures;\n";
2029 OS << " };\n\n";
2030
2031 OS << " // Predicate for searching for an opcode.\n";
2032 OS << " struct LessOpcodeOperand {\n";
2033 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2034 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2035 OS << " }\n";
2036 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2037 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2038 OS << " }\n";
2039 OS << " bool operator()(const OperandMatchEntry &LHS,";
2040 OS << " const OperandMatchEntry &RHS) {\n";
2041 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2042 OS << " }\n";
2043 OS << " };\n";
2044
2045 OS << "} // end anonymous namespace.\n\n";
2046
2047 OS << "static const OperandMatchEntry OperandMatchTable["
2048 << Info.OperandMatchInfo.size() << "] = {\n";
2049
2050 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
2051 for (std::vector<OperandMatchEntry>::const_iterator it =
2052 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2053 it != ie; ++it) {
2054 const OperandMatchEntry &OMI = *it;
2055 const MatchableInfo &II = *OMI.MI;
2056
2057 OS << " { \"" << II.Mnemonic << "\""
2058 << ", " << OMI.OperandMask;
2059
2060 OS << " /* ";
2061 bool printComma = false;
2062 for (int i = 0, e = 31; i !=e; ++i)
2063 if (OMI.OperandMask & (1 << i)) {
2064 if (printComma)
2065 OS << ", ";
2066 OS << i;
2067 printComma = true;
2068 }
2069 OS << " */";
2070
2071 OS << ", " << OMI.CI->Name
2072 << ", ";
2073
2074 // Write the required features mask.
2075 if (!II.RequiredFeatures.empty()) {
2076 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2077 if (i) OS << "|";
2078 OS << II.RequiredFeatures[i]->getEnumName();
2079 }
2080 } else
2081 OS << "0";
2082 OS << " },\n";
2083 }
2084 OS << "};\n\n";
2085
2086 // Emit the operand class switch to call the correct custom parser for
2087 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002088 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2089 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002090 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002091 << " &Operands,\n unsigned MCK) {\n\n"
2092 << " switch(MCK) {\n";
2093
2094 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2095 ie = Info.Classes.end(); it != ie; ++it) {
2096 ClassInfo *CI = *it;
2097 if (CI->ParserMethod.empty())
2098 continue;
2099 OS << " case " << CI->Name << ":\n"
2100 << " return " << CI->ParserMethod << "(Operands);\n";
2101 }
2102
2103 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002104 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002105 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002106 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002107 OS << "}\n\n";
2108
2109 // Emit the static custom operand parser. This code is very similar with
2110 // the other matcher. Also use MatchResultTy here just in case we go for
2111 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002112 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002113 << Target.getName() << ClassName << "::\n"
2114 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2115 << " &Operands,\n StringRef Mnemonic) {\n";
2116
2117 // Emit code to get the available features.
2118 OS << " // Get the current feature set.\n";
2119 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2120
2121 OS << " // Get the next operand index.\n";
2122 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2123
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002124 // Emit code to search the table.
2125 OS << " // Search the table.\n";
2126 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2127 OS << " MnemonicRange =\n";
2128 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2129 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2130 << " LessOpcodeOperand());\n\n";
2131
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002132 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002133 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002134
2135 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2136 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2137
2138 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2139 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2140
2141 // Emit check that the required features are available.
2142 OS << " // check if the available features match\n";
2143 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2144 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002145 OS << " continue;\n";
2146 OS << " }\n\n";
2147
2148 // Emit check to ensure the operand number matches.
2149 OS << " // check if the operand in question has a custom parser.\n";
2150 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2151 OS << " continue;\n\n";
2152
2153 // Emit call to the custom parser method
2154 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002155 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002156 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002157 OS << " if (Result != MatchOperand_NoMatch)\n";
2158 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002159 OS << " }\n\n";
2160
Jim Grosbachf922c472011-02-12 01:34:40 +00002161 OS << " // Okay, we had no match.\n";
2162 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002163 OS << "}\n\n";
2164}
2165
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002166void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002167 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002168 Record *AsmParser = Target.getAsmParser();
2169 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2170
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002171 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002172 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002173 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002174
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002175 // Sort the instruction table using the partial order on classes. We use
2176 // stable_sort to ensure that ambiguous instructions are still
2177 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002178 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2179 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002180
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002181 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002182 for (std::vector<MatchableInfo*>::iterator
2183 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002184 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002185 (*it)->dump();
2186 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002187
Chris Lattner22bc5c42010-11-01 05:06:45 +00002188 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002189 DEBUG_WITH_TYPE("ambiguous_instrs", {
2190 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002191 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002192 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002193 MatchableInfo &A = *Info.Matchables[i];
2194 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002195
Bob Wilson1f64ac42011-01-26 21:26:21 +00002196 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002197 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002198 A.dump();
2199 errs() << "\nis incomparable with:\n";
2200 B.dump();
2201 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002202 ++NumAmbiguous;
2203 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002204 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002205 }
Chris Lattner87410362010-09-06 20:21:47 +00002206 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002207 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002208 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002209 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002210
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002211 // Compute the information on the custom operand parsing.
2212 Info.BuildOperandMatchInfo();
2213
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002214 // Write the output.
2215
2216 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2217
Chris Lattner0692ee62010-09-06 19:11:01 +00002218 // Information for the class declaration.
2219 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2220 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002221 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002222 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002223 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002224 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2225 << "unsigned Opcode,\n"
2226 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2227 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002228 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002229 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002230 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Devang Patel56315d32012-01-10 17:50:43 +00002231 OS << " MCInst &Inst, unsigned &ErrorInfo, unsigned VariantID = 0);\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002232
2233 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002234 OS << "\n enum OperandMatchResultTy {\n";
2235 OS << " MatchOperand_Success, // operand matched successfully\n";
2236 OS << " MatchOperand_NoMatch, // operand did not match\n";
2237 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2238 OS << " };\n";
2239 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002240 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2241 OS << " StringRef Mnemonic);\n";
2242
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002243 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002244 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2245 OS << " unsigned MCK);\n\n";
2246 }
2247
Chris Lattner0692ee62010-09-06 19:11:01 +00002248 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2249
Chris Lattner0692ee62010-09-06 19:11:01 +00002250 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2251 OS << "#undef GET_REGISTER_MATCHER\n\n";
2252
Daniel Dunbar54074b52010-07-19 05:44:09 +00002253 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002254 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002255
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002256 // Emit the function to match a register name to number.
2257 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002258
2259 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002260
Chris Lattner0692ee62010-09-06 19:11:01 +00002261
2262 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2263 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002264
Chris Lattner7fd44892010-10-30 18:48:18 +00002265 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002266 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002267
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002268 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002269 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002270
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002271 // Emit the enumeration for classes which participate in matching.
2272 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002273
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002274 // Emit the routine to match token strings to their match class.
2275 EmitMatchTokenString(Target, Info.Classes, OS);
2276
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002277 // Emit the subclass predicate routine.
2278 EmitIsSubclass(Target, Info.Classes, OS);
2279
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002280 // Emit the routine to validate an operand against a match class.
2281 EmitValidateOperandClass(Info, OS);
2282
Daniel Dunbar54074b52010-07-19 05:44:09 +00002283 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002284 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002285
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002286
2287 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002288 for (std::vector<MatchableInfo*>::const_iterator it =
2289 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002290 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002291 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002292
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002293 // Emit the static match table; unused classes get initalized to 0 which is
2294 // guaranteed to be InvalidMatchClass.
2295 //
2296 // FIXME: We can reduce the size of this table very easily. First, we change
2297 // it so that store the kinds in separate bit-fields for each index, which
2298 // only needs to be the max width used for classes at that index (we also need
2299 // to reject based on this during classification). If we then make sure to
2300 // order the match kinds appropriately (putting mnemonics last), then we
2301 // should only end up using a few bits for each class, especially the ones
2302 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002303 OS << "namespace {\n";
2304 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002305 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002306 OS << " const char *Mnemonic;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002307 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2308 << " ConvertFn;\n";
2309 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2310 << " Classes[" << MaxNumOperands << "];\n";
2311 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2312 << " RequiredFeatures;\n";
Devang Patel56315d32012-01-10 17:50:43 +00002313 OS << " unsigned AsmVariantID;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002314 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002315
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002316 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002317 OS << " struct LessOpcode {\n";
2318 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2319 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2320 OS << " }\n";
2321 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2322 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2323 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002324 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2325 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2326 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002327 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002328
Chris Lattner96352e52010-09-06 21:08:38 +00002329 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002330
Chris Lattner96352e52010-09-06 21:08:38 +00002331 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002332 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002333
Chris Lattner22bc5c42010-11-01 05:06:45 +00002334 for (std::vector<MatchableInfo*>::const_iterator it =
2335 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002336 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002337 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002338
Chris Lattner662e5a32010-11-06 07:14:44 +00002339 OS << " { " << Target.getName() << "::"
2340 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2341 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002342 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002343 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002344
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002345 if (i) OS << ", ";
2346 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002347 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002348 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002349
Daniel Dunbar54074b52010-07-19 05:44:09 +00002350 // Write the required features mask.
2351 if (!II.RequiredFeatures.empty()) {
2352 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2353 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002354 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002355 }
2356 } else
2357 OS << "0";
Devang Patel56315d32012-01-10 17:50:43 +00002358 OS << ", " << II.AsmVariantID;
Daniel Dunbar54074b52010-07-19 05:44:09 +00002359 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002360 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002361
Chris Lattner96352e52010-09-06 21:08:38 +00002362 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002363
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002364 // A method to determine if a mnemonic is in the list.
2365 OS << "bool " << Target.getName() << ClassName << "::\n"
2366 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2367 OS << " // Search the table.\n";
2368 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2369 OS << " std::equal_range(MatchTable, MatchTable+"
2370 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2371 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2372 OS << "}\n\n";
2373
Chris Lattner96352e52010-09-06 21:08:38 +00002374 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002375 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002376 << Target.getName() << ClassName << "::\n"
2377 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2378 << " &Operands,\n";
Devang Patel56315d32012-01-10 17:50:43 +00002379 OS << " MCInst &Inst, unsigned &ErrorInfo,\n";
2380 OS << " unsigned VariantID) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002381
2382 // Emit code to get the available features.
2383 OS << " // Get the current feature set.\n";
2384 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2385
Chris Lattner674c1dc2010-10-30 17:36:36 +00002386 OS << " // Get the instruction mnemonic, which is the first token.\n";
2387 OS << " StringRef Mnemonic = ((" << Target.getName()
2388 << "Operand*)Operands[0])->getToken();\n\n";
2389
Chris Lattner7fd44892010-10-30 18:48:18 +00002390 if (HasMnemonicAliases) {
2391 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Devang Patel40bced02012-01-17 18:30:45 +00002392 OS << " // FIXME : Add an entry in AsmParserVariant to check this.\n";
2393 OS << " if (!VariantID)\n";
2394 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002395 }
Bob Wilson828295b2011-01-26 21:26:19 +00002396
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002397 // Emit code to compute the class list for this operand vector.
2398 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002399 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2400 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2401 OS << " return Match_InvalidOperand;\n";
2402 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002403
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002404 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002405 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002406 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002407 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002408 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002409 OS << " // wrong for all instances of the instruction.\n";
2410 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002411
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002412 // Emit code to search the table.
2413 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002414 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2415 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002416 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002417
Chris Lattnera008e8a2010-09-06 21:54:15 +00002418 OS << " // Return a more specific error code if no mnemonics match.\n";
2419 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2420 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002421
Chris Lattner2b1f9432010-09-06 21:22:45 +00002422 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002423 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002424 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002425
Gabor Greife53ee3b2010-09-07 06:06:06 +00002426 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002427 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002428
Daniel Dunbar54074b52010-07-19 05:44:09 +00002429 // Emit check that the subclasses match.
Devang Patel56315d32012-01-10 17:50:43 +00002430 OS << " if (VariantID != it->AsmVariantID) continue;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002431 OS << " bool OperandsValid = true;\n";
2432 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002433 OS << " if (i + 1 >= Operands.size()) {\n";
2434 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002435 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002436 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002437 OS << " if (validateOperandClass(Operands[i+1], "
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002438 "(MatchClassKind)it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002439 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002440 OS << " // If this operand is broken for all of the instances of this\n";
2441 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002442 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002443 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002444 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2445 OS << " OperandsValid = false;\n";
2446 OS << " break;\n";
2447 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002448
Chris Lattnerce4a3352010-09-06 22:11:18 +00002449 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002450
2451 // Emit check that the required features are available.
2452 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2453 << "!= it->RequiredFeatures) {\n";
2454 OS << " HadMatchOtherThanFeatures = true;\n";
2455 OS << " continue;\n";
2456 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002457 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002458 OS << " // We have selected a definite instruction, convert the parsed\n"
2459 << " // operands into the appropriate MCInst.\n";
2460 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2461 << " it->Opcode, Operands))\n";
2462 OS << " return Match_ConversionFail;\n";
2463 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002464
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002465 // Verify the instruction with the target-specific match predicate function.
2466 OS << " // We have a potential match. Check the target predicate to\n"
2467 << " // handle any context sensitive constraints.\n"
2468 << " unsigned MatchResult;\n"
2469 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2470 << " Match_Success) {\n"
2471 << " Inst.clear();\n"
2472 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002473 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002474 << " continue;\n"
2475 << " }\n\n";
2476
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002477 // Call the post-processing function, if used.
2478 std::string InsnCleanupFn =
2479 AsmParser->getValueAsString("AsmParserInstCleanup");
2480 if (!InsnCleanupFn.empty())
2481 OS << " " << InsnCleanupFn << "(Inst);\n";
2482
Chris Lattner79ed3f72010-09-06 19:22:17 +00002483 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002484 OS << " }\n\n";
2485
Chris Lattnerec6789f2010-09-06 20:08:02 +00002486 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002487 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
2488 OS << " return RetCode;\n";
2489 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002490 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002491
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002492 if (Info.OperandMatchInfo.size())
2493 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2494
Chris Lattner0692ee62010-09-06 19:11:01 +00002495 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002496}