blob: 8b86c23d06324e69b39e30928a404b05ca6ee3ef [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"
Peter Collingbourne7c788882011-10-01 16:41:13 +0000110#include "llvm/TableGen/Error.h"
111#include "llvm/TableGen/Record.h"
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000112#include <map>
113#include <set>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000114using namespace llvm;
115
Daniel Dunbar27249152009-08-07 20:33:39 +0000116static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000117MatchPrefix("match-prefix", cl::init(""),
118 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000119
Daniel Dunbar20927f22009-08-07 08:26:05 +0000120namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000121class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000122struct SubtargetFeatureInfo;
123
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000124/// ClassInfo - Helper class for storing the information about a particular
125/// class of operands which can be matched.
126struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000127 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000128 /// Invalid kind, for use as a sentinel value.
129 Invalid = 0,
130
131 /// The class for a particular token.
132 Token,
133
134 /// The (first) register class, subsequent register classes are
135 /// RegisterClass0+1, and so on.
136 RegisterClass0,
137
138 /// The (first) user defined class, subsequent user defined classes are
139 /// UserClass0+1, and so on.
140 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000141 };
142
143 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
144 /// N) for the Nth user defined class.
145 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000146
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000147 /// SuperClasses - The super classes of this class. Note that for simplicities
148 /// sake user operands only record their immediate super class, while register
149 /// operands include all superclasses.
150 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000151
Daniel Dunbar6745d422009-08-09 05:18:30 +0000152 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000153 std::string Name;
154
Daniel Dunbar6745d422009-08-09 05:18:30 +0000155 /// ClassName - The unadorned generic name for this class (e.g., Token).
156 std::string ClassName;
157
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000158 /// ValueName - The name of the value this class represents; for a token this
159 /// is the literal token string, for an operand it is the TableGen class (or
160 /// empty if this is a derived class).
161 std::string ValueName;
162
163 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000164 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000165 std::string PredicateMethod;
166
167 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000168 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000169 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000170
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000171 /// ParserMethod - The name of the operand method to do a target specific
172 /// parsing on the operand.
173 std::string ParserMethod;
174
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000175 /// For register classes, the records for all the registers in this class.
176 std::set<Record*> Registers;
177
178public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000179 /// isRegisterClass() - Check if this is a register class.
180 bool isRegisterClass() const {
181 return Kind >= RegisterClass0 && Kind < UserClass0;
182 }
183
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000184 /// isUserClass() - Check if this is a user defined class.
185 bool isUserClass() const {
186 return Kind >= UserClass0;
187 }
188
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000189 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
190 /// are related if they are in the same class hierarchy.
191 bool isRelatedTo(const ClassInfo &RHS) const {
192 // Tokens are only related to tokens.
193 if (Kind == Token || RHS.Kind == Token)
194 return Kind == Token && RHS.Kind == Token;
195
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000196 // Registers classes are only related to registers classes, and only if
197 // their intersection is non-empty.
198 if (isRegisterClass() || RHS.isRegisterClass()) {
199 if (!isRegisterClass() || !RHS.isRegisterClass())
200 return false;
201
202 std::set<Record*> Tmp;
203 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000204 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000205 RHS.Registers.begin(), RHS.Registers.end(),
206 II);
207
208 return !Tmp.empty();
209 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000210
211 // Otherwise we have two users operands; they are related if they are in the
212 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000213 //
214 // FIXME: This is an oversimplification, they should only be related if they
215 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000216 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
217 const ClassInfo *Root = this;
218 while (!Root->SuperClasses.empty())
219 Root = Root->SuperClasses.front();
220
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000221 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000222 while (!RHSRoot->SuperClasses.empty())
223 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000224
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000225 return Root == RHSRoot;
226 }
227
Jim Grosbacha7c78222010-10-29 22:13:48 +0000228 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000229 bool isSubsetOf(const ClassInfo &RHS) const {
230 // This is a subset of RHS if it is the same class...
231 if (this == &RHS)
232 return true;
233
234 // ... or if any of its super classes are a subset of RHS.
235 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
236 ie = SuperClasses.end(); it != ie; ++it)
237 if ((*it)->isSubsetOf(RHS))
238 return true;
239
240 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000241 }
242
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000243 /// operator< - Compare two classes.
244 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000245 if (this == &RHS)
246 return false;
247
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000248 // Unrelated classes can be ordered by kind.
249 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000250 return Kind < RHS.Kind;
251
252 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000253 case Invalid:
254 assert(0 && "Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000255 case Token:
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000256 // Tokens are comparable by value.
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000257 //
258 // FIXME: Compare by enum value.
259 return ValueName < RHS.ValueName;
260
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000261 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000262 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000263 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000264 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000265 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000266 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000267
268 // Otherwise, order by name to ensure we have a total ordering.
269 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000270 }
271 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000272};
273
Chris Lattner22bc5c42010-11-01 05:06:45 +0000274/// MatchableInfo - Helper class for storing the necessary information for an
275/// instruction or alias which is capable of being matched.
276struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000277 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000278 /// Token - This is the token that the operand came from.
279 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000280
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000281 /// The unique class instance this operand should match.
282 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000283
Chris Lattner567820c2010-11-04 01:42:59 +0000284 /// The operand name this is, if anything.
285 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000286
287 /// The suboperand index within SrcOpName, or -1 for the entire operand.
288 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000289
Bob Wilsona49c7df2011-01-26 19:44:55 +0000290 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1) {}
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
Chris Lattner3b5aec62010-11-02 17:34:28 +0000369 /// TheDef - This is the definition of the instruction or InstAlias that this
370 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000371 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000372
Chris Lattnerc07bd402010-11-04 02:11:18 +0000373 /// DefRec - This is the definition that it came from.
374 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000375
Chris Lattner662e5a32010-11-06 07:14:44 +0000376 const CodeGenInstruction *getResultInst() const {
377 if (DefRec.is<const CodeGenInstruction*>())
378 return DefRec.get<const CodeGenInstruction*>();
379 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
380 }
Bob Wilson828295b2011-01-26 21:26:19 +0000381
Chris Lattner1d13bda2010-11-04 00:43:46 +0000382 /// ResOperands - This is the operand list that should be built for the result
383 /// MCInst.
384 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000385
386 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000387 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000388 std::string AsmString;
389
Chris Lattnerd19ec052010-11-02 17:30:52 +0000390 /// Mnemonic - This is the first token of the matched instruction, its
391 /// mnemonic.
392 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000393
Chris Lattner3116fef2010-11-02 01:03:43 +0000394 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000395 /// annotated with a class and where in the OperandList they were defined.
396 /// This directly corresponds to the tokenized AsmString after the mnemonic is
397 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000398 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000399
Daniel Dunbar54074b52010-07-19 05:44:09 +0000400 /// Predicates - The required subtarget features to match this instruction.
401 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
402
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000403 /// ConversionFnKind - The enum value which is passed to the generated
404 /// ConvertToMCInst to convert parsed operands into an MCInst for this
405 /// function.
406 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000407
Chris Lattner22bc5c42010-11-01 05:06:45 +0000408 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattner662e5a32010-11-06 07:14:44 +0000409 : TheDef(CGI.TheDef), DefRec(&CGI), AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000410 }
411
Chris Lattner22bc5c42010-11-01 05:06:45 +0000412 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattner662e5a32010-11-06 07:14:44 +0000413 : TheDef(Alias->TheDef), DefRec(Alias), AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000414 }
Bob Wilson828295b2011-01-26 21:26:19 +0000415
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000416 void Initialize(const AsmMatcherInfo &Info,
417 SmallPtrSet<Record*, 16> &SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +0000418
Chris Lattner22bc5c42010-11-01 05:06:45 +0000419 /// Validate - Return true if this matchable is a valid thing to match against
420 /// and perform a bunch of validity checking.
421 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000422
Chris Lattnerd19ec052010-11-02 17:30:52 +0000423 /// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000424 /// register, return the Record for it, otherwise return null.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000425 Record *getSingletonRegisterForAsmOperand(unsigned i,
Bob Wilson828295b2011-01-26 21:26:19 +0000426 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000427
Bob Wilsona49c7df2011-01-26 19:44:55 +0000428 /// FindAsmOperand - Find the AsmOperand with the specified name and
429 /// suboperand index.
430 int FindAsmOperand(StringRef N, int SubOpIdx) const {
431 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
432 if (N == AsmOperands[i].SrcOpName &&
433 SubOpIdx == AsmOperands[i].SubOpIdx)
434 return i;
435 return -1;
436 }
Bob Wilson828295b2011-01-26 21:26:19 +0000437
Bob Wilsona49c7df2011-01-26 19:44:55 +0000438 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
439 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000440 int FindAsmOperandNamed(StringRef N) const {
441 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
442 if (N == AsmOperands[i].SrcOpName)
443 return i;
444 return -1;
445 }
Bob Wilson828295b2011-01-26 21:26:19 +0000446
Chris Lattner41409852010-11-06 07:31:43 +0000447 void BuildInstructionResultOperands();
448 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000449
Chris Lattner22bc5c42010-11-01 05:06:45 +0000450 /// operator< - Compare two matchables.
451 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000452 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000453 if (Mnemonic != RHS.Mnemonic)
454 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000455
Chris Lattner3116fef2010-11-02 01:03:43 +0000456 if (AsmOperands.size() != RHS.AsmOperands.size())
457 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000458
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000459 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000460 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000461 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
462 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000463 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000464 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000465 return false;
466 }
467
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000468 return false;
469 }
470
Bob Wilson1f64ac42011-01-26 21:26:21 +0000471 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000472 /// ambiguously match the same set of operands as \arg RHS (without being a
473 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000474 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000475 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000476 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000477 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000478
Daniel Dunbar2b544812009-08-09 06:05:33 +0000479 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000480 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000481 return false;
482
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000483 // Otherwise, make sure the ordering of the two instructions is unambiguous
484 // by checking that either (a) a token or operand kind discriminates them,
485 // or (b) the ordering among equivalent kinds is consistent.
486
Daniel Dunbar2b544812009-08-09 06:05:33 +0000487 // Tokens and operand kinds are unambiguous (assuming a correct target
488 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000489 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
490 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
491 AsmOperands[i].Class->Kind == ClassInfo::Token)
492 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
493 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000494 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000495
Daniel Dunbar2b544812009-08-09 06:05:33 +0000496 // Otherwise, this operand could commute if all operands are equivalent, or
497 // there is a pair of operands that compare less than and a pair that
498 // compare greater than.
499 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000500 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
501 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000502 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000503 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000504 HasGT = true;
505 }
506
507 return !(HasLT ^ HasGT);
508 }
509
Daniel Dunbar20927f22009-08-07 08:26:05 +0000510 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000511
Chris Lattnerd19ec052010-11-02 17:30:52 +0000512private:
513 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000514};
515
Daniel Dunbar54074b52010-07-19 05:44:09 +0000516/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
517/// feature which participates in instruction matching.
518struct SubtargetFeatureInfo {
519 /// \brief The predicate record for this feature.
520 Record *TheDef;
521
522 /// \brief An unique index assigned to represent this feature.
523 unsigned Index;
524
Chris Lattner0aed1e72010-10-30 20:07:57 +0000525 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000526
Daniel Dunbar54074b52010-07-19 05:44:09 +0000527 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000528 std::string getEnumName() const {
529 return "Feature_" + TheDef->getName();
530 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000531};
532
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000533struct OperandMatchEntry {
534 unsigned OperandMask;
535 MatchableInfo* MI;
536 ClassInfo *CI;
537
538 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
539 unsigned opMask) {
540 OperandMatchEntry X;
541 X.OperandMask = opMask;
542 X.CI = ci;
543 X.MI = mi;
544 return X;
545 }
546};
547
548
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000549class AsmMatcherInfo {
550public:
Chris Lattner67db8832010-12-13 00:23:57 +0000551 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000552 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000553
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000554 /// The tablegen AsmParser record.
555 Record *AsmParser;
556
Chris Lattner02bcbc92010-11-01 01:37:30 +0000557 /// Target - The target information.
558 CodeGenTarget &Target;
559
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000560 /// The AsmParser "RegisterPrefix" value.
561 std::string RegisterPrefix;
562
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000563 /// The classes which are needed for matching.
564 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000565
Chris Lattner22bc5c42010-11-01 05:06:45 +0000566 /// The information on the matchables to match.
567 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000568
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000569 /// Info for custom matching operands by user defined methods.
570 std::vector<OperandMatchEntry> OperandMatchInfo;
571
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000572 /// Map of Register records to their class information.
573 std::map<Record*, ClassInfo*> RegisterClasses;
574
Daniel Dunbar54074b52010-07-19 05:44:09 +0000575 /// Map of Predicate records to their subtarget information.
576 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000577
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000578private:
579 /// Map of token to class information which has already been constructed.
580 std::map<std::string, ClassInfo*> TokenClasses;
581
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000582 /// Map of RegisterClass records to their class information.
583 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000584
Daniel Dunbar338825c2009-08-10 18:41:10 +0000585 /// Map of AsmOperandClass records to their class information.
586 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000587
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000588private:
589 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000590 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000591
592 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000593 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
594 int SubOpIdx = -1);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000595
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000596 /// BuildRegisterClasses - Build the ClassInfo* instances for register
597 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000598 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000599
600 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
601 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000602 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000603
Bob Wilsona49c7df2011-01-26 19:44:55 +0000604 void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
605 unsigned AsmOpIdx);
606 void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000607 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000608
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000609public:
Bob Wilson828295b2011-01-26 21:26:19 +0000610 AsmMatcherInfo(Record *AsmParser,
611 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000612 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000613
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000614 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000615 void BuildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000616
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000617 /// BuildOperandMatchInfo - Build the necessary information to handle user
618 /// defined operand parsing methods.
619 void BuildOperandMatchInfo();
620
Chris Lattner6fa152c2010-10-30 20:15:02 +0000621 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
622 /// given operand.
623 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
624 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
625 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
626 SubtargetFeatures.find(Def);
627 return I == SubtargetFeatures.end() ? 0 : I->second;
628 }
Chris Lattner67db8832010-12-13 00:23:57 +0000629
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000630 RecordKeeper &getRecords() const {
631 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000632 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000633};
634
Daniel Dunbar20927f22009-08-07 08:26:05 +0000635}
636
Chris Lattner22bc5c42010-11-01 05:06:45 +0000637void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000638 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000639
Chris Lattner3116fef2010-11-02 01:03:43 +0000640 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000641 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000642 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000643 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000644 }
645}
646
Chris Lattner22bc5c42010-11-01 05:06:45 +0000647void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
648 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000649 // TODO: Eventually support asmparser for Variant != 0.
650 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
Bob Wilson828295b2011-01-26 21:26:19 +0000651
Chris Lattnerd19ec052010-11-02 17:30:52 +0000652 TokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000653
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000654 // Compute the require features.
655 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
656 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
657 if (SubtargetFeatureInfo *Feature =
658 Info.getSubtargetFeature(Predicates[i]))
659 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000660
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000661 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000662 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
663 if (Record *Reg = getSingletonRegisterForAsmOperand(i, Info))
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000664 SingletonRegisters.insert(Reg);
665 }
666}
667
Chris Lattnerd19ec052010-11-02 17:30:52 +0000668/// TokenizeAsmString - Tokenize a simplified assembly string.
669void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
670 StringRef String = AsmString;
671 unsigned Prev = 0;
672 bool InTok = true;
673 for (unsigned i = 0, e = String.size(); i != e; ++i) {
674 switch (String[i]) {
675 case '[':
676 case ']':
677 case '*':
678 case '!':
679 case ' ':
680 case '\t':
681 case ',':
682 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000683 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000684 InTok = false;
685 }
686 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000687 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000688 Prev = i + 1;
689 break;
690
691 case '\\':
692 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000693 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000694 InTok = false;
695 }
696 ++i;
697 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000698 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000699 Prev = i + 1;
700 break;
701
702 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000703 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000704 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000705 InTok = false;
706 }
Bob Wilson828295b2011-01-26 21:26:19 +0000707
Chris Lattner7ad31472010-11-06 22:06:03 +0000708 // If this isn't "${", treat like a normal token.
709 if (i + 1 == String.size() || String[i + 1] != '{') {
710 Prev = i;
711 break;
712 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000713
714 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
715 assert(End != String.end() && "Missing brace in operand reference!");
716 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000717 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000718 Prev = EndPos + 1;
719 i = EndPos;
720 break;
721 }
722
723 case '.':
724 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000725 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000726 Prev = i;
727 InTok = true;
728 break;
729
730 default:
731 InTok = true;
732 }
733 }
734 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000735 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000736
Chris Lattnerd19ec052010-11-02 17:30:52 +0000737 // The first token of the instruction is the mnemonic, which must be a
738 // simple string, not a $foo variable or a singleton register.
739 assert(!AsmOperands.empty() && "Instruction has no tokens?");
740 Mnemonic = AsmOperands[0].Token;
741 if (Mnemonic[0] == '$' || getSingletonRegisterForAsmOperand(0, Info))
742 throw TGError(TheDef->getLoc(),
743 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000744
Chris Lattnerd19ec052010-11-02 17:30:52 +0000745 // Remove the first operand, it is tracked in the mnemonic field.
746 AsmOperands.erase(AsmOperands.begin());
747}
748
Chris Lattner22bc5c42010-11-01 05:06:45 +0000749bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
750 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000751 if (AsmString.empty())
752 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000753
Chris Lattner22bc5c42010-11-01 05:06:45 +0000754 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000755 // isCodeGenOnly if they are pseudo instructions.
756 if (AsmString.find('\n') != std::string::npos)
757 throw TGError(TheDef->getLoc(),
758 "multiline instruction is not valid for the asmparser, "
759 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000760
Chris Lattner4164f6b2010-11-01 04:44:29 +0000761 // Remove comments from the asm string. We know that the asmstring only
762 // has one line.
763 if (!CommentDelimiter.empty() &&
764 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
765 throw TGError(TheDef->getLoc(),
766 "asmstring for instruction has comment character in it, "
767 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000768
Chris Lattner22bc5c42010-11-01 05:06:45 +0000769 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000770 // handle, the target should be refactored to use operands instead of
771 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000772 //
773 // Also, check for instructions which reference the operand multiple times;
774 // this implies a constraint we would not honor.
775 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000776 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
777 StringRef Tok = AsmOperands[i].Token;
778 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000779 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000780 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000781 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000782
Chris Lattner22bc5c42010-11-01 05:06:45 +0000783 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000784 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000785 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000786 if (!Hack)
787 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000788 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000789 "' can never be matched!");
790 // FIXME: Should reject these. The ARM backend hits this with $lane in a
791 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000792 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000793 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000794 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000795 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000796 });
797 return false;
798 }
799 }
Bob Wilson828295b2011-01-26 21:26:19 +0000800
Chris Lattner5bc93872010-11-01 04:34:44 +0000801 return true;
802}
803
Chris Lattnerd19ec052010-11-02 17:30:52 +0000804/// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner02bcbc92010-11-01 01:37:30 +0000805/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000806Record *MatchableInfo::
Chris Lattnerd19ec052010-11-02 17:30:52 +0000807getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{
808 StringRef Tok = AsmOperands[i].Token;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000809 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000810 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000811
Chris Lattner02bcbc92010-11-01 01:37:30 +0000812 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000813 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
814 return Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000815
Chris Lattner1de88232010-11-01 01:47:07 +0000816 // If there is no register prefix (i.e. "%" in "%eax"), then this may
817 // be some random non-register token, just ignore it.
818 if (Info.RegisterPrefix.empty())
819 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000820
Chris Lattnerec6f0962010-11-02 18:10:06 +0000821 // Otherwise, we have something invalid prefixed with the register prefix,
822 // such as %foo.
Chris Lattner1de88232010-11-01 01:47:07 +0000823 std::string Err = "unable to find register for '" + RegName.str() +
824 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000825 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000826}
827
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000828static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000829 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000830
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000831 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
832 switch (*it) {
833 case '*': Res += "_STAR_"; break;
834 case '%': Res += "_PCT_"; break;
835 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000836 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000837 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000838 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000839 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000840 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000841 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000842 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000843 }
844 }
845
846 return Res;
847}
848
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000849ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000850 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000851
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000852 if (!Entry) {
853 Entry = new ClassInfo();
854 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000855 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000856 Entry->Name = "MCK_" + getEnumNameForToken(Token);
857 Entry->ValueName = Token;
858 Entry->PredicateMethod = "<invalid>";
859 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000860 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000861 Classes.push_back(Entry);
862 }
863
864 return Entry;
865}
866
867ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000868AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
869 int SubOpIdx) {
870 Record *Rec = OI.Rec;
871 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000872 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Bob Wilsona49c7df2011-01-26 19:44:55 +0000873
Owen Andersonbea6f612011-06-27 21:06:21 +0000874 if (Rec->isSubClassOf("RegisterOperand")) {
875 // RegisterOperand may have an associated ParserMatchClass. If it does,
876 // use it, else just fall back to the underlying register class.
877 const RecordVal *R = Rec->getValue("ParserMatchClass");
878 if (R == 0 || R->getValue() == 0)
879 throw "Record `" + Rec->getName() +
880 "' does not have a ParserMatchClass!\n";
881
David Greene05bce0b2011-07-29 22:43:06 +0000882 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000883 Record *MatchClass = DI->getDef();
884 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
885 return CI;
886 }
887
888 // No custom match class. Just use the register class.
889 Record *ClassRec = Rec->getValueAsDef("RegClass");
890 if (!ClassRec)
891 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
892 "' has no associated register class!\n");
893 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
894 return CI;
895 throw TGError(Rec->getLoc(), "register class has no class info!");
896 }
897
898
Bob Wilsona49c7df2011-01-26 19:44:55 +0000899 if (Rec->isSubClassOf("RegisterClass")) {
900 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000901 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000902 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000903 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000904
Bob Wilsona49c7df2011-01-26 19:44:55 +0000905 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
906 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000907 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
908 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000909
Bob Wilsona49c7df2011-01-26 19:44:55 +0000910 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000911}
912
Chris Lattner1de88232010-11-01 01:47:07 +0000913void AsmMatcherInfo::
914BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000915 const std::vector<CodeGenRegister*> &Registers =
916 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000917 ArrayRef<CodeGenRegisterClass*> RegClassList =
918 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000919
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000920 // The register sets used for matching.
921 std::set< std::set<Record*> > RegisterSets;
922
Jim Grosbacha7c78222010-10-29 22:13:48 +0000923 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000924 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +0000925 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000926 RegisterSets.insert(std::set<Record*>(
927 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000928
929 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000930 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
931 ie = SingletonRegisters.end(); it != ie; ++it) {
932 Record *Rec = *it;
933 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
934 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000935
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000936 // Introduce derived sets where necessary (when a register does not determine
937 // a unique register set class), and build the mapping of registers to the set
938 // they should classify to.
939 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000940 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000941 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000942 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000943 // Compute the intersection of all sets containing this register.
944 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000945
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000946 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
947 ie = RegisterSets.end(); it != ie; ++it) {
948 if (!it->count(CGR.TheDef))
949 continue;
950
951 if (ContainingSet.empty()) {
952 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000953 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000954 }
Bob Wilson828295b2011-01-26 21:26:19 +0000955
Chris Lattnerec6f0962010-11-02 18:10:06 +0000956 std::set<Record*> Tmp;
957 std::swap(Tmp, ContainingSet);
958 std::insert_iterator< std::set<Record*> > II(ContainingSet,
959 ContainingSet.begin());
960 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000961 }
962
963 if (!ContainingSet.empty()) {
964 RegisterSets.insert(ContainingSet);
965 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
966 }
967 }
968
969 // Construct the register classes.
970 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
971 unsigned Index = 0;
972 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
973 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
974 ClassInfo *CI = new ClassInfo();
975 CI->Kind = ClassInfo::RegisterClass0 + Index;
976 CI->ClassName = "Reg" + utostr(Index);
977 CI->Name = "MCK_Reg" + utostr(Index);
978 CI->ValueName = "";
979 CI->PredicateMethod = ""; // unused
980 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000981 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000982 Classes.push_back(CI);
983 RegisterSetClasses.insert(std::make_pair(*it, CI));
984 }
985
986 // Find the superclasses; we could compute only the subgroup lattice edges,
987 // but there isn't really a point.
988 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
989 ie = RegisterSets.end(); it != ie; ++it) {
990 ClassInfo *CI = RegisterSetClasses[*it];
991 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
992 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000993 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000994 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
995 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
996 }
997
998 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000999 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001000 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001001 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001002 // Def will be NULL for non-user defined register classes.
1003 Record *Def = RC.getDef();
1004 if (!Def)
1005 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001006 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1007 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001008 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001009 CI->ClassName = RC.getName();
1010 CI->Name = "MCK_" + RC.getName();
1011 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001012 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001013 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001014
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001015 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001016 }
1017
1018 // Populate the map for individual registers.
1019 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1020 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001021 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001022
1023 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001024 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1025 ie = SingletonRegisters.end(); it != ie; ++it) {
1026 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001027 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001028 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001029
Chris Lattner1de88232010-11-01 01:47:07 +00001030 if (CI->ValueName.empty()) {
1031 CI->ClassName = Rec->getName();
1032 CI->Name = "MCK_" + Rec->getName();
1033 CI->ValueName = Rec->getName();
1034 } else
1035 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001036 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001037}
1038
Chris Lattner02bcbc92010-11-01 01:37:30 +00001039void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001040 std::vector<Record*> AsmOperands =
1041 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001042
1043 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001044 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001045 ie = AsmOperands.end(); it != ie; ++it)
1046 AsmOperandClasses[*it] = new ClassInfo();
1047
Daniel Dunbar338825c2009-08-10 18:41:10 +00001048 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001049 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001050 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001051 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001052 CI->Kind = ClassInfo::UserClass0 + Index;
1053
David Greene05bce0b2011-07-29 22:43:06 +00001054 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001055 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001056 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001057 if (!DI) {
1058 PrintError((*it)->getLoc(), "Invalid super class reference!");
1059 continue;
1060 }
1061
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001062 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1063 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001064 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001065 else
1066 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001067 }
1068 CI->ClassName = (*it)->getValueAsString("Name");
1069 CI->Name = "MCK_" + CI->ClassName;
1070 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001071
1072 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001073 Init *PMName = (*it)->getValueInit("PredicateMethod");
1074 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001075 CI->PredicateMethod = SI->getValue();
1076 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001077 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001078 "Unexpected PredicateMethod field!");
1079 CI->PredicateMethod = "is" + CI->ClassName;
1080 }
1081
1082 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001083 Init *RMName = (*it)->getValueInit("RenderMethod");
1084 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001085 CI->RenderMethod = SI->getValue();
1086 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001087 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001088 "Unexpected RenderMethod field!");
1089 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1090 }
1091
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001092 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001093 Init *PRMName = (*it)->getValueInit("ParserMethod");
1094 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001095 CI->ParserMethod = SI->getValue();
1096
Daniel Dunbar338825c2009-08-10 18:41:10 +00001097 AsmOperandClasses[*it] = CI;
1098 Classes.push_back(CI);
1099 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001100}
1101
Bob Wilson828295b2011-01-26 21:26:19 +00001102AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1103 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001104 RecordKeeper &records)
Chris Lattner67db8832010-12-13 00:23:57 +00001105 : Records(records), AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001106 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001107}
1108
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001109/// BuildOperandMatchInfo - Build the necessary information to handle user
1110/// defined operand parsing methods.
1111void AsmMatcherInfo::BuildOperandMatchInfo() {
1112
1113 /// Map containing a mask with all operands indicies that can be found for
1114 /// that class inside a instruction.
1115 std::map<ClassInfo*, unsigned> OpClassMask;
1116
1117 for (std::vector<MatchableInfo*>::const_iterator it =
1118 Matchables.begin(), ie = Matchables.end();
1119 it != ie; ++it) {
1120 MatchableInfo &II = **it;
1121 OpClassMask.clear();
1122
1123 // Keep track of all operands of this instructions which belong to the
1124 // same class.
1125 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1126 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1127 if (Op.Class->ParserMethod.empty())
1128 continue;
1129 unsigned &OperandMask = OpClassMask[Op.Class];
1130 OperandMask |= (1 << i);
1131 }
1132
1133 // Generate operand match info for each mnemonic/operand class pair.
1134 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1135 iie = OpClassMask.end(); iit != iie; ++iit) {
1136 unsigned OpMask = iit->second;
1137 ClassInfo *CI = iit->first;
1138 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1139 }
1140 }
1141}
1142
Chris Lattner02bcbc92010-11-01 01:37:30 +00001143void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001144 // Build information about all of the AssemblerPredicates.
1145 std::vector<Record*> AllPredicates =
1146 Records.getAllDerivedDefinitions("Predicate");
1147 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1148 Record *Pred = AllPredicates[i];
1149 // Ignore predicates that are not intended for the assembler.
1150 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1151 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001152
Chris Lattner4164f6b2010-11-01 04:44:29 +00001153 if (Pred->getName().empty())
1154 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001155
Chris Lattner0aed1e72010-10-30 20:07:57 +00001156 unsigned FeatureNo = SubtargetFeatures.size();
1157 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1158 assert(FeatureNo < 32 && "Too many subtarget features!");
1159 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001160
Eli Friedman60435482011-07-08 20:07:05 +00001161 std::string CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
Bob Wilson828295b2011-01-26 21:26:19 +00001162
Chris Lattner39ee0362010-10-31 19:10:56 +00001163 // Parse the instructions; we need to do this first so that we can gather the
1164 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001165 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +00001166 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1167 E = Target.inst_end(); I != E; ++I) {
1168 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001169
Chris Lattner39ee0362010-10-31 19:10:56 +00001170 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1171 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +00001172 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001173 continue;
1174
Chris Lattner5bc93872010-11-01 04:34:44 +00001175 // Ignore "codegen only" instructions.
1176 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1177 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001178
Chris Lattner1d13bda2010-11-04 00:43:46 +00001179 // Validate the operand list to ensure we can handle this instruction.
1180 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1181 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001182
Chris Lattner1d13bda2010-11-04 00:43:46 +00001183 // Validate tied operands.
1184 if (OI.getTiedRegister() != -1) {
Bob Wilson828295b2011-01-26 21:26:19 +00001185 // If we have a tied operand that consists of multiple MCOperands,
1186 // reject it. We reject aliases and ignore instructions for now.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001187 if (OI.MINumOperands != 1) {
1188 // FIXME: Should reject these. The ARM backend hits this with $lane
1189 // in a bunch of instructions. It is unclear what the right answer is.
1190 DEBUG({
1191 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1192 << "ignoring instruction with multi-operand tied operand '"
1193 << OI.Name << "'\n";
1194 });
1195 continue;
1196 }
1197 }
1198 }
Bob Wilson828295b2011-01-26 21:26:19 +00001199
Chris Lattner22bc5c42010-11-01 05:06:45 +00001200 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001201
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001202 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001203
Chris Lattner4d43d0f2010-11-01 01:07:14 +00001204 // Ignore instructions which shouldn't be matched and diagnose invalid
1205 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001206 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +00001207 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001208
Chris Lattner5bc93872010-11-01 04:34:44 +00001209 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1210 //
1211 // FIXME: This is a total hack.
Chris Lattner5abd1eb2010-11-06 06:43:11 +00001212 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1213 StringRef(II->TheDef->getName()).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001214 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001215
Chris Lattner22bc5c42010-11-01 05:06:45 +00001216 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001217 }
Bob Wilson828295b2011-01-26 21:26:19 +00001218
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001219 // Parse all of the InstAlias definitions and stick them in the list of
1220 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001221 std::vector<Record*> AllInstAliases =
1222 Records.getAllDerivedDefinitions("InstAlias");
1223 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
Chris Lattner225549f2010-11-06 06:39:47 +00001224 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001225
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001226 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1227 // filter the set of instruction aliases we consider, based on the target
1228 // instruction.
1229 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1230 MatchPrefix))
1231 continue;
1232
Chris Lattner22bc5c42010-11-01 05:06:45 +00001233 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Bob Wilson828295b2011-01-26 21:26:19 +00001234
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001235 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001236
Chris Lattner22bc5c42010-11-01 05:06:45 +00001237 // Validate the alias definitions.
1238 II->Validate(CommentDelimiter, false);
Bob Wilson828295b2011-01-26 21:26:19 +00001239
Chris Lattnerb501d4f2010-11-01 05:34:34 +00001240 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001241 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001242
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001243 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001244 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001245
1246 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001247 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001248
Chris Lattner0bb780c2010-11-04 00:57:06 +00001249 // Build the information about matchables, now that we have fully formed
1250 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001251 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1252 ie = Matchables.end(); it != ie; ++it) {
1253 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001254
Chris Lattnere206fcf2010-09-06 21:01:37 +00001255 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001256 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1257 // don't precompute the loop bound.
1258 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001259 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001260 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001261
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001262 // Check for singleton registers.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001263 if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) {
1264 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001265 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1266 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001267 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001268 }
1269
Daniel Dunbar20927f22009-08-07 08:26:05 +00001270 // Check for simple tokens.
1271 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001272 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001273 continue;
1274 }
1275
Chris Lattner7ad31472010-11-06 22:06:03 +00001276 if (Token.size() > 1 && isdigit(Token[1])) {
1277 Op.Class = getTokenClass(Token);
1278 continue;
1279 }
Bob Wilson828295b2011-01-26 21:26:19 +00001280
Chris Lattnerc07bd402010-11-04 02:11:18 +00001281 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001282 StringRef OperandName;
1283 if (Token[1] == '{')
1284 OperandName = Token.substr(2, Token.size() - 3);
1285 else
1286 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001287
Chris Lattnerc07bd402010-11-04 02:11:18 +00001288 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001289 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001290 else
Chris Lattner225549f2010-11-06 06:39:47 +00001291 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001292 }
Bob Wilson828295b2011-01-26 21:26:19 +00001293
Chris Lattner41409852010-11-06 07:31:43 +00001294 if (II->DefRec.is<const CodeGenInstruction*>())
1295 II->BuildInstructionResultOperands();
1296 else
1297 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001298 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001299
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001300 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001301 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001302}
1303
Chris Lattner0bb780c2010-11-04 00:57:06 +00001304/// BuildInstructionOperandReference - The specified operand is a reference to a
1305/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1306void AsmMatcherInfo::
1307BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001308 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001309 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001310 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1311 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001312 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001313
Chris Lattner662e5a32010-11-06 07:14:44 +00001314 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001315 unsigned Idx;
1316 if (!Operands.hasOperandNamed(OperandName, Idx))
1317 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1318 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001319
Bob Wilsona49c7df2011-01-26 19:44:55 +00001320 // If the instruction operand has multiple suboperands, but the parser
1321 // match class for the asm operand is still the default "ImmAsmOperand",
1322 // then handle each suboperand separately.
1323 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1324 Record *Rec = Operands[Idx].Rec;
1325 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1326 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1327 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1328 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1329 StringRef Token = Op->Token; // save this in case Op gets moved
1330 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1331 MatchableInfo::AsmOperand NewAsmOp(Token);
1332 NewAsmOp.SubOpIdx = SI;
1333 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1334 }
1335 // Replace Op with first suboperand.
1336 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1337 Op->SubOpIdx = 0;
1338 }
1339 }
1340
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001341 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001342 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001343
1344 // If the named operand is tied, canonicalize it to the untied operand.
1345 // For example, something like:
1346 // (outs GPR:$dst), (ins GPR:$src)
1347 // with an asmstring of
1348 // "inc $src"
1349 // we want to canonicalize to:
1350 // "inc $dst"
1351 // so that we know how to provide the $dst operand when filling in the result.
1352 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001353 if (OITied != -1) {
1354 // The tied operand index is an MIOperand index, find the operand that
1355 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001356 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1357 OperandName = Operands[Idx.first].Name;
1358 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001359 }
Bob Wilson828295b2011-01-26 21:26:19 +00001360
Bob Wilsona49c7df2011-01-26 19:44:55 +00001361 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001362}
1363
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001364/// BuildAliasOperandReference - When parsing an operand reference out of the
1365/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1366/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001367void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1368 StringRef OperandName,
1369 MatchableInfo::AsmOperand &Op) {
1370 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001371
Chris Lattnerc07bd402010-11-04 02:11:18 +00001372 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001373 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001374 if (CGA.ResultOperands[i].isRecord() &&
1375 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001376 // It's safe to go with the first one we find, because CodeGenInstAlias
1377 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001378 unsigned ResultIdx = CGA.ResultInstOperandIndex[i].first;
1379 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
1380 Op.Class = getOperandClass(CGA.ResultInst->Operands[ResultIdx],
1381 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001382 Op.SrcOpName = OperandName;
1383 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001384 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001385
1386 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1387 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001388}
1389
Chris Lattner41409852010-11-06 07:31:43 +00001390void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001391 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001392
Chris Lattner662e5a32010-11-06 07:14:44 +00001393 // Loop over all operands of the result instruction, determining how to
1394 // populate them.
1395 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1396 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001397
1398 // If this is a tied operand, just copy from the previously handled operand.
1399 int TiedOp = OpInfo.getTiedRegister();
1400 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001401 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001402 continue;
1403 }
Bob Wilson828295b2011-01-26 21:26:19 +00001404
Bob Wilsona49c7df2011-01-26 19:44:55 +00001405 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001406 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001407 if (OpInfo.Name.empty() || SrcOperand == -1)
1408 throw TGError(TheDef->getLoc(), "Instruction '" +
1409 TheDef->getName() + "' has operand '" + OpInfo.Name +
1410 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001411
Bob Wilsona49c7df2011-01-26 19:44:55 +00001412 // Check if the one AsmOperand populates the entire operand.
1413 unsigned NumOperands = OpInfo.MINumOperands;
1414 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1415 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001416 continue;
1417 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001418
1419 // Add a separate ResOperand for each suboperand.
1420 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1421 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1422 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1423 "unexpected AsmOperands for suboperands");
1424 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1425 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001426 }
1427}
1428
Chris Lattner41409852010-11-06 07:31:43 +00001429void MatchableInfo::BuildAliasResultOperands() {
1430 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1431 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001432
Chris Lattner41409852010-11-06 07:31:43 +00001433 // Loop over all operands of the result instruction, determining how to
1434 // populate them.
1435 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001436 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001437 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001438 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001439
Chris Lattner41409852010-11-06 07:31:43 +00001440 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001441 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001442 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001443 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001444 continue;
1445 }
1446
Bob Wilsona49c7df2011-01-26 19:44:55 +00001447 // Handle all the suboperands for this operand.
1448 const std::string &OpName = OpInfo->Name;
1449 for ( ; AliasOpNo < LastOpNo &&
1450 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1451 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1452
1453 // Find out what operand from the asmparser that this MCInst operand
1454 // comes from.
1455 switch (CGA.ResultOperands[AliasOpNo].Kind) {
1456 default: assert(0 && "unexpected InstAlias operand kind");
1457 case CodeGenInstAlias::ResultOperand::K_Record: {
1458 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1459 int SrcOperand = FindAsmOperand(Name, SubIdx);
1460 if (SrcOperand == -1)
1461 throw TGError(TheDef->getLoc(), "Instruction '" +
1462 TheDef->getName() + "' has operand '" + OpName +
1463 "' that doesn't appear in asm string!");
1464 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1465 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1466 NumOperands));
1467 break;
1468 }
1469 case CodeGenInstAlias::ResultOperand::K_Imm: {
1470 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1471 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1472 break;
1473 }
1474 case CodeGenInstAlias::ResultOperand::K_Reg: {
1475 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1476 ResOperands.push_back(ResOperand::getRegOp(Reg));
1477 break;
1478 }
1479 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001480 }
Chris Lattner41409852010-11-06 07:31:43 +00001481 }
1482}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001483
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001484static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001485 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001486 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001487 // Write the convert function to a separate stream, so we can drop it after
1488 // the enum.
1489 std::string ConvertFnBody;
1490 raw_string_ostream CvtOS(ConvertFnBody);
1491
Daniel Dunbar20927f22009-08-07 08:26:05 +00001492 // Function we have already generated.
1493 std::set<std::string> GeneratedFns;
1494
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001495 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001496 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1497 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001498 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001499 << " const SmallVectorImpl<MCParsedAsmOperand*"
1500 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001501 CvtOS << " Inst.setOpcode(Opcode);\n";
1502 CvtOS << " switch (Kind) {\n";
1503 CvtOS << " default:\n";
1504
1505 // Start the enum, which we will generate inline.
1506
Chris Lattnerd51257a2010-11-02 23:18:43 +00001507 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001508 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001509
Chris Lattner98986712010-01-14 22:21:20 +00001510 // TargetOperandClass - This is the target's operand class, like X86Operand.
1511 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001512
Chris Lattner22bc5c42010-11-01 05:06:45 +00001513 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001514 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001515 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001516
Daniel Dunbarcf120672011-02-04 17:12:15 +00001517 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001518 std::string AsmMatchConverter =
1519 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001520 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001521 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001522 II.ConversionFnKind = Signature;
1523
1524 // Check if we have already generated this signature.
1525 if (!GeneratedFns.insert(Signature).second)
1526 continue;
1527
1528 // If not, emit it now. Add to the enum list.
1529 OS << " " << Signature << ",\n";
1530
1531 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001532 CvtOS << " return " << AsmMatchConverter
1533 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001534 continue;
1535 }
1536
Daniel Dunbar20927f22009-08-07 08:26:05 +00001537 // Build the conversion function signature.
1538 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001539 std::string CaseBody;
1540 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001541
Chris Lattnerdda855d2010-11-02 21:49:44 +00001542 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001543 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1544 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001545
Chris Lattner1d13bda2010-11-04 00:43:46 +00001546 // Generate code to populate each result operand.
1547 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001548 case MatchableInfo::ResOperand::RenderAsmOperand: {
1549 // This comes from something we parsed.
1550 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001551
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001552 // Registers are always converted the same, don't duplicate the
1553 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001554 Signature += "__";
1555 if (Op.Class->isRegisterClass())
1556 Signature += "Reg";
1557 else
1558 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001559 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001560 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001561
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001562 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001563 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001564 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001565 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001566 }
Bob Wilson828295b2011-01-26 21:26:19 +00001567
Chris Lattner1d13bda2010-11-04 00:43:46 +00001568 case MatchableInfo::ResOperand::TiedOperand: {
1569 // If this operand is tied to a previous one, just copy the MCInst
1570 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001571 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001572 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001573 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001574 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1575 Signature += "__Tie" + utostr(TiedOp);
1576 break;
1577 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001578 case MatchableInfo::ResOperand::ImmOperand: {
1579 int64_t Val = OpInfo.ImmVal;
1580 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1581 Signature += "__imm" + itostr(Val);
1582 break;
1583 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001584 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001585 if (OpInfo.Register == 0) {
1586 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1587 Signature += "__reg0";
1588 } else {
1589 std::string N = getQualifiedName(OpInfo.Register);
1590 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1591 Signature += "__reg" + OpInfo.Register->getName();
1592 }
Bob Wilson828295b2011-01-26 21:26:19 +00001593 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001594 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001595 }
Bob Wilson828295b2011-01-26 21:26:19 +00001596
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001597 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001598
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001599 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001600 if (!GeneratedFns.insert(Signature).second)
1601 continue;
1602
Chris Lattnerdda855d2010-11-02 21:49:44 +00001603 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001604 OS << " " << Signature << ",\n";
1605
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001606 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001607 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001608 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001609 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001610
1611 // Finish the convert function.
1612
1613 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001614 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001615 CvtOS << "}\n\n";
1616
1617 // Finish the enum, and drop the convert function after it.
1618
1619 OS << " NumConversionVariants\n";
1620 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001621
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001622 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001623}
1624
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001625/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1626static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1627 std::vector<ClassInfo*> &Infos,
1628 raw_ostream &OS) {
1629 OS << "namespace {\n\n";
1630
1631 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1632 << "/// instruction matching.\n";
1633 OS << "enum MatchClassKind {\n";
1634 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001635 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001636 ie = Infos.end(); it != ie; ++it) {
1637 ClassInfo &CI = **it;
1638 OS << " " << CI.Name << ", // ";
1639 if (CI.Kind == ClassInfo::Token) {
1640 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001641 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001642 if (!CI.ValueName.empty())
1643 OS << "register class '" << CI.ValueName << "'\n";
1644 else
1645 OS << "derived register class\n";
1646 } else {
1647 OS << "user defined class '" << CI.ValueName << "'\n";
1648 }
1649 }
1650 OS << " NumMatchClassKinds\n";
1651 OS << "};\n\n";
1652
1653 OS << "}\n\n";
1654}
1655
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001656/// EmitValidateOperandClass - Emit the function to validate an operand class.
1657static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1658 raw_ostream &OS) {
1659 OS << "static bool ValidateOperandClass(MCParsedAsmOperand *GOp, "
1660 << "MatchClassKind Kind) {\n";
1661 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001662 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001663
Kevin Enderby89381832011-07-15 18:30:43 +00001664 // The InvalidMatchClass is not to match any operand.
1665 OS << " if (Kind == InvalidMatchClass)\n";
1666 OS << " return false;\n\n";
1667
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001668 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001669 OS << " if (Operand.isToken())\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001670 OS << " return MatchTokenString(Operand.getToken()) == Kind;\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001671
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001672 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001673 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001674 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001675 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001676 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001677 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001678 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1679 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001680 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001681 << it->first->getName() << ": OpKind = " << it->second->Name
1682 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001683 OS << " }\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001684 OS << " return IsSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001685 OS << " }\n\n";
1686
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001687 // Check the user classes. We don't care what order since we're only
1688 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001689 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001690 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001691 ClassInfo &CI = **it;
1692
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001693 if (!CI.isUserClass())
1694 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001695
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001696 OS << " // '" << CI.ClassName << "' class\n";
1697 OS << " if (Kind == " << CI.Name
1698 << " && Operand." << CI.PredicateMethod << "()) {\n";
1699 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001700 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001701 }
Bob Wilson828295b2011-01-26 21:26:19 +00001702
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001703 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001704 OS << "}\n\n";
1705}
1706
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001707/// EmitIsSubclass - Emit the subclass predicate function.
1708static void EmitIsSubclass(CodeGenTarget &Target,
1709 std::vector<ClassInfo*> &Infos,
1710 raw_ostream &OS) {
1711 OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1712 OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n";
1713 OS << " if (A == B)\n";
1714 OS << " return true;\n\n";
1715
1716 OS << " switch (A) {\n";
1717 OS << " default:\n";
1718 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001719 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001720 ie = Infos.end(); it != ie; ++it) {
1721 ClassInfo &A = **it;
1722
1723 if (A.Kind != ClassInfo::Token) {
1724 std::vector<StringRef> SuperClasses;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001725 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001726 ie = Infos.end(); it != ie; ++it) {
1727 ClassInfo &B = **it;
1728
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001729 if (&A != &B && A.isSubsetOf(B))
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001730 SuperClasses.push_back(B.Name);
1731 }
1732
1733 if (SuperClasses.empty())
1734 continue;
1735
1736 OS << "\n case " << A.Name << ":\n";
1737
1738 if (SuperClasses.size() == 1) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001739 OS << " return B == " << SuperClasses.back() << ";\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001740 continue;
1741 }
1742
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001743 OS << " switch (B) {\n";
1744 OS << " default: return false;\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001745 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001746 OS << " case " << SuperClasses[i] << ": return true;\n";
1747 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001748 }
1749 }
1750 OS << " }\n";
1751 OS << "}\n\n";
1752}
1753
Daniel Dunbar245f0582009-08-08 21:22:41 +00001754/// EmitMatchTokenString - Emit the function to match a token string to the
1755/// appropriate match class value.
1756static void EmitMatchTokenString(CodeGenTarget &Target,
1757 std::vector<ClassInfo*> &Infos,
1758 raw_ostream &OS) {
1759 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001760 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001761 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001762 ie = Infos.end(); it != ie; ++it) {
1763 ClassInfo &CI = **it;
1764
1765 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001766 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1767 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001768 }
1769
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001770 OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001771
Chris Lattner5845e5c2010-09-06 02:01:51 +00001772 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001773
1774 OS << " return InvalidMatchClass;\n";
1775 OS << "}\n\n";
1776}
Chris Lattner70add882009-08-08 20:02:57 +00001777
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001778/// EmitMatchRegisterName - Emit the function to match a string to the target
1779/// specific register enum.
1780static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1781 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001782 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001783 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001784 const std::vector<CodeGenRegister*> &Regs =
1785 Target.getRegBank().getRegisters();
1786 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1787 const CodeGenRegister *Reg = Regs[i];
1788 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001789 continue;
1790
Chris Lattner5845e5c2010-09-06 02:01:51 +00001791 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001792 Reg->TheDef->getValueAsString("AsmName"),
1793 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001794 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001795
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001796 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001797
Chris Lattner5845e5c2010-09-06 02:01:51 +00001798 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001799
Daniel Dunbar245f0582009-08-08 21:22:41 +00001800 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001801 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001802}
Daniel Dunbara027d222009-07-31 02:32:59 +00001803
Daniel Dunbar54074b52010-07-19 05:44:09 +00001804/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1805/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001806static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001807 raw_ostream &OS) {
1808 OS << "// Flags for subtarget features that participate in "
1809 << "instruction matching.\n";
1810 OS << "enum SubtargetFeatureFlag {\n";
1811 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1812 it = Info.SubtargetFeatures.begin(),
1813 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1814 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001815 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001816 }
1817 OS << " Feature_None = 0\n";
1818 OS << "};\n\n";
1819}
1820
1821/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1822/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001823static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001824 raw_ostream &OS) {
1825 std::string ClassName =
1826 Info.AsmParser->getValueAsString("AsmParserClassName");
1827
Chris Lattner02bcbc92010-11-01 01:37:30 +00001828 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00001829 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001830 OS << " unsigned Features = 0;\n";
1831 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1832 it = Info.SubtargetFeatures.begin(),
1833 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1834 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00001835
1836 OS << " if (";
Evan Chengfbc38d22011-07-08 18:04:22 +00001837 std::string CondStorage = SFI.TheDef->getValueAsString("AssemblerCondString");
1838 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00001839 std::pair<StringRef,StringRef> Comma = Conds.split(',');
1840 bool First = true;
1841 do {
1842 if (!First)
1843 OS << " && ";
1844
1845 bool Neg = false;
1846 StringRef Cond = Comma.first;
1847 if (Cond[0] == '!') {
1848 Neg = true;
1849 Cond = Cond.substr(1);
1850 }
1851
1852 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
1853 if (Neg)
1854 OS << " == 0";
1855 else
1856 OS << " != 0";
1857 OS << ")";
1858
1859 if (Comma.second.empty())
1860 break;
1861
1862 First = false;
1863 Comma = Comma.second.split(',');
1864 } while (true);
1865
1866 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001867 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001868 }
1869 OS << " return Features;\n";
1870 OS << "}\n\n";
1871}
1872
Chris Lattner6fa152c2010-10-30 20:15:02 +00001873static std::string GetAliasRequiredFeatures(Record *R,
1874 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001875 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001876 std::string Result;
1877 unsigned NumFeatures = 0;
1878 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001879 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001880
Chris Lattner4a74ee72010-11-01 02:09:21 +00001881 if (F == 0)
1882 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1883 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001884
Chris Lattner4a74ee72010-11-01 02:09:21 +00001885 if (NumFeatures)
1886 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001887
Chris Lattner4a74ee72010-11-01 02:09:21 +00001888 Result += F->getEnumName();
1889 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001890 }
Bob Wilson828295b2011-01-26 21:26:19 +00001891
Chris Lattner693173f2010-10-30 19:23:13 +00001892 if (NumFeatures > 1)
1893 Result = '(' + Result + ')';
1894 return Result;
1895}
1896
Chris Lattner674c1dc2010-10-30 17:36:36 +00001897/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001898/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001899static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001900 // Ignore aliases when match-prefix is set.
1901 if (!MatchPrefix.empty())
1902 return false;
1903
Chris Lattner674c1dc2010-10-30 17:36:36 +00001904 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001905 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001906 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001907
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001908 OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, "
1909 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001910
Chris Lattner4fd32c62010-10-30 18:56:12 +00001911 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1912 // iteration order of the map is stable.
1913 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001914
Chris Lattner674c1dc2010-10-30 17:36:36 +00001915 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1916 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001917 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001918 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001919
1920 // Process each alias a "from" mnemonic at a time, building the code executed
1921 // by the string remapper.
1922 std::vector<StringMatcher::StringPair> Cases;
1923 for (std::map<std::string, std::vector<Record*> >::iterator
1924 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1925 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001926 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001927
1928 // Loop through each alias and emit code that handles each case. If there
1929 // are two instructions without predicates, emit an error. If there is one,
1930 // emit it last.
1931 std::string MatchCode;
1932 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001933
Chris Lattner693173f2010-10-30 19:23:13 +00001934 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1935 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001936 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001937
Chris Lattner693173f2010-10-30 19:23:13 +00001938 // If this unconditionally matches, remember it for later and diagnose
1939 // duplicates.
1940 if (FeatureMask.empty()) {
1941 if (AliasWithNoPredicate != -1) {
1942 // We can't have two aliases from the same mnemonic with no predicate.
1943 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1944 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001945 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001946 }
Bob Wilson828295b2011-01-26 21:26:19 +00001947
Chris Lattner693173f2010-10-30 19:23:13 +00001948 AliasWithNoPredicate = i;
1949 continue;
1950 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001951 if (R->getValueAsString("ToMnemonic") == I->first)
1952 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001953
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001954 if (!MatchCode.empty())
1955 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001956 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1957 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001958 }
Bob Wilson828295b2011-01-26 21:26:19 +00001959
Chris Lattner693173f2010-10-30 19:23:13 +00001960 if (AliasWithNoPredicate != -1) {
1961 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001962 if (!MatchCode.empty())
1963 MatchCode += "else\n ";
1964 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001965 }
Bob Wilson828295b2011-01-26 21:26:19 +00001966
Chris Lattner693173f2010-10-30 19:23:13 +00001967 MatchCode += "return;";
1968
1969 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001970 }
Bob Wilson828295b2011-01-26 21:26:19 +00001971
Chris Lattner674c1dc2010-10-30 17:36:36 +00001972 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00001973 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001974
Chris Lattner7fd44892010-10-30 18:48:18 +00001975 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001976}
1977
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001978static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
1979 const AsmMatcherInfo &Info, StringRef ClassName) {
1980 // Emit the static custom operand parsing table;
1981 OS << "namespace {\n";
1982 OS << " struct OperandMatchEntry {\n";
1983 OS << " const char *Mnemonic;\n";
1984 OS << " unsigned OperandMask;\n";
1985 OS << " MatchClassKind Class;\n";
1986 OS << " unsigned RequiredFeatures;\n";
1987 OS << " };\n\n";
1988
1989 OS << " // Predicate for searching for an opcode.\n";
1990 OS << " struct LessOpcodeOperand {\n";
1991 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
1992 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
1993 OS << " }\n";
1994 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
1995 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
1996 OS << " }\n";
1997 OS << " bool operator()(const OperandMatchEntry &LHS,";
1998 OS << " const OperandMatchEntry &RHS) {\n";
1999 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2000 OS << " }\n";
2001 OS << " };\n";
2002
2003 OS << "} // end anonymous namespace.\n\n";
2004
2005 OS << "static const OperandMatchEntry OperandMatchTable["
2006 << Info.OperandMatchInfo.size() << "] = {\n";
2007
2008 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
2009 for (std::vector<OperandMatchEntry>::const_iterator it =
2010 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2011 it != ie; ++it) {
2012 const OperandMatchEntry &OMI = *it;
2013 const MatchableInfo &II = *OMI.MI;
2014
2015 OS << " { \"" << II.Mnemonic << "\""
2016 << ", " << OMI.OperandMask;
2017
2018 OS << " /* ";
2019 bool printComma = false;
2020 for (int i = 0, e = 31; i !=e; ++i)
2021 if (OMI.OperandMask & (1 << i)) {
2022 if (printComma)
2023 OS << ", ";
2024 OS << i;
2025 printComma = true;
2026 }
2027 OS << " */";
2028
2029 OS << ", " << OMI.CI->Name
2030 << ", ";
2031
2032 // Write the required features mask.
2033 if (!II.RequiredFeatures.empty()) {
2034 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2035 if (i) OS << "|";
2036 OS << II.RequiredFeatures[i]->getEnumName();
2037 }
2038 } else
2039 OS << "0";
2040 OS << " },\n";
2041 }
2042 OS << "};\n\n";
2043
2044 // Emit the operand class switch to call the correct custom parser for
2045 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002046 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2047 << Target.getName() << ClassName << "::\n"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002048 << "TryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
2049 << " &Operands,\n unsigned MCK) {\n\n"
2050 << " switch(MCK) {\n";
2051
2052 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2053 ie = Info.Classes.end(); it != ie; ++it) {
2054 ClassInfo *CI = *it;
2055 if (CI->ParserMethod.empty())
2056 continue;
2057 OS << " case " << CI->Name << ":\n"
2058 << " return " << CI->ParserMethod << "(Operands);\n";
2059 }
2060
2061 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002062 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002063 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002064 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002065 OS << "}\n\n";
2066
2067 // Emit the static custom operand parser. This code is very similar with
2068 // the other matcher. Also use MatchResultTy here just in case we go for
2069 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002070 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002071 << Target.getName() << ClassName << "::\n"
2072 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2073 << " &Operands,\n StringRef Mnemonic) {\n";
2074
2075 // Emit code to get the available features.
2076 OS << " // Get the current feature set.\n";
2077 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2078
2079 OS << " // Get the next operand index.\n";
2080 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2081
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002082 // Emit code to search the table.
2083 OS << " // Search the table.\n";
2084 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2085 OS << " MnemonicRange =\n";
2086 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2087 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2088 << " LessOpcodeOperand());\n\n";
2089
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002090 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002091 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002092
2093 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2094 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2095
2096 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2097 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2098
2099 // Emit check that the required features are available.
2100 OS << " // check if the available features match\n";
2101 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2102 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002103 OS << " continue;\n";
2104 OS << " }\n\n";
2105
2106 // Emit check to ensure the operand number matches.
2107 OS << " // check if the operand in question has a custom parser.\n";
2108 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2109 OS << " continue;\n\n";
2110
2111 // Emit call to the custom parser method
2112 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002113 OS << " OperandMatchResultTy Result = ";
2114 OS << "TryCustomParseOperand(Operands, it->Class);\n";
2115 OS << " if (Result != MatchOperand_NoMatch)\n";
2116 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002117 OS << " }\n\n";
2118
Jim Grosbachf922c472011-02-12 01:34:40 +00002119 OS << " // Okay, we had no match.\n";
2120 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002121 OS << "}\n\n";
2122}
2123
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002124void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002125 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002126 Record *AsmParser = Target.getAsmParser();
2127 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2128
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002129 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002130 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002131 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002132
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002133 // Sort the instruction table using the partial order on classes. We use
2134 // stable_sort to ensure that ambiguous instructions are still
2135 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002136 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2137 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002138
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002139 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002140 for (std::vector<MatchableInfo*>::iterator
2141 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002142 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002143 (*it)->dump();
2144 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002145
Chris Lattner22bc5c42010-11-01 05:06:45 +00002146 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002147 DEBUG_WITH_TYPE("ambiguous_instrs", {
2148 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002149 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002150 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002151 MatchableInfo &A = *Info.Matchables[i];
2152 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002153
Bob Wilson1f64ac42011-01-26 21:26:21 +00002154 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002155 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002156 A.dump();
2157 errs() << "\nis incomparable with:\n";
2158 B.dump();
2159 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002160 ++NumAmbiguous;
2161 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002162 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002163 }
Chris Lattner87410362010-09-06 20:21:47 +00002164 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002165 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002166 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002167 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002168
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002169 // Compute the information on the custom operand parsing.
2170 Info.BuildOperandMatchInfo();
2171
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002172 // Write the output.
2173
2174 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2175
Chris Lattner0692ee62010-09-06 19:11:01 +00002176 // Information for the class declaration.
2177 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2178 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002179 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002180 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002181 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002182 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2183 << "unsigned Opcode,\n"
2184 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2185 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002186 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002187 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002188 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002189 OS << " MCInst &Inst, unsigned &ErrorInfo);\n";
2190
2191 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002192 OS << "\n enum OperandMatchResultTy {\n";
2193 OS << " MatchOperand_Success, // operand matched successfully\n";
2194 OS << " MatchOperand_NoMatch, // operand did not match\n";
2195 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2196 OS << " };\n";
2197 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002198 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2199 OS << " StringRef Mnemonic);\n";
2200
Jim Grosbachf922c472011-02-12 01:34:40 +00002201 OS << " OperandMatchResultTy TryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002202 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2203 OS << " unsigned MCK);\n\n";
2204 }
2205
Chris Lattner0692ee62010-09-06 19:11:01 +00002206 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2207
Chris Lattner0692ee62010-09-06 19:11:01 +00002208 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2209 OS << "#undef GET_REGISTER_MATCHER\n\n";
2210
Daniel Dunbar54074b52010-07-19 05:44:09 +00002211 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002212 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002213
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002214 // Emit the function to match a register name to number.
2215 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002216
2217 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002218
Chris Lattner0692ee62010-09-06 19:11:01 +00002219
2220 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2221 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002222
Chris Lattner7fd44892010-10-30 18:48:18 +00002223 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002224 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002225
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002226 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002227 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002228
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002229 // Emit the enumeration for classes which participate in matching.
2230 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002231
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002232 // Emit the routine to match token strings to their match class.
2233 EmitMatchTokenString(Target, Info.Classes, OS);
2234
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002235 // Emit the subclass predicate routine.
2236 EmitIsSubclass(Target, Info.Classes, OS);
2237
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002238 // Emit the routine to validate an operand against a match class.
2239 EmitValidateOperandClass(Info, OS);
2240
Daniel Dunbar54074b52010-07-19 05:44:09 +00002241 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002242 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002243
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002244
2245 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002246 for (std::vector<MatchableInfo*>::const_iterator it =
2247 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002248 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002249 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002250
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002251 // Emit the static match table; unused classes get initalized to 0 which is
2252 // guaranteed to be InvalidMatchClass.
2253 //
2254 // FIXME: We can reduce the size of this table very easily. First, we change
2255 // it so that store the kinds in separate bit-fields for each index, which
2256 // only needs to be the max width used for classes at that index (we also need
2257 // to reject based on this during classification). If we then make sure to
2258 // order the match kinds appropriately (putting mnemonics last), then we
2259 // should only end up using a few bits for each class, especially the ones
2260 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002261 OS << "namespace {\n";
2262 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002263 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002264 OS << " const char *Mnemonic;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002265 OS << " ConversionKind ConvertFn;\n";
2266 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002267 OS << " unsigned RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002268 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002269
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002270 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002271 OS << " struct LessOpcode {\n";
2272 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2273 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2274 OS << " }\n";
2275 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2276 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2277 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002278 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2279 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2280 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002281 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002282
Chris Lattner96352e52010-09-06 21:08:38 +00002283 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002284
Chris Lattner96352e52010-09-06 21:08:38 +00002285 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002286 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002287
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 Lattner22bc5c42010-11-01 05:06:45 +00002291 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002292
Chris Lattner662e5a32010-11-06 07:14:44 +00002293 OS << " { " << Target.getName() << "::"
2294 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2295 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002296 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002297 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002298
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002299 if (i) OS << ", ";
2300 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002301 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002302 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002303
Daniel Dunbar54074b52010-07-19 05:44:09 +00002304 // Write the required features mask.
2305 if (!II.RequiredFeatures.empty()) {
2306 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2307 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002308 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002309 }
2310 } else
2311 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002312
Daniel Dunbar54074b52010-07-19 05:44:09 +00002313 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002314 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002315
Chris Lattner96352e52010-09-06 21:08:38 +00002316 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002317
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002318 // A method to determine if a mnemonic is in the list.
2319 OS << "bool " << Target.getName() << ClassName << "::\n"
2320 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2321 OS << " // Search the table.\n";
2322 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2323 OS << " std::equal_range(MatchTable, MatchTable+"
2324 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2325 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2326 OS << "}\n\n";
2327
Chris Lattner96352e52010-09-06 21:08:38 +00002328 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002329 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002330 << Target.getName() << ClassName << "::\n"
2331 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2332 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002333 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002334
2335 // Emit code to get the available features.
2336 OS << " // Get the current feature set.\n";
2337 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2338
Chris Lattner674c1dc2010-10-30 17:36:36 +00002339 OS << " // Get the instruction mnemonic, which is the first token.\n";
2340 OS << " StringRef Mnemonic = ((" << Target.getName()
2341 << "Operand*)Operands[0])->getToken();\n\n";
2342
Chris Lattner7fd44892010-10-30 18:48:18 +00002343 if (HasMnemonicAliases) {
2344 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
2345 OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
2346 }
Bob Wilson828295b2011-01-26 21:26:19 +00002347
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002348 // Emit code to compute the class list for this operand vector.
2349 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002350 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2351 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2352 OS << " return Match_InvalidOperand;\n";
2353 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002354
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002355 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002356 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002357 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002358 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002359 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002360 OS << " // wrong for all instances of the instruction.\n";
2361 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002362
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002363 // Emit code to search the table.
2364 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002365 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2366 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002367 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002368
Chris Lattnera008e8a2010-09-06 21:54:15 +00002369 OS << " // Return a more specific error code if no mnemonics match.\n";
2370 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2371 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002372
Chris Lattner2b1f9432010-09-06 21:22:45 +00002373 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002374 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002375 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002376
Gabor Greife53ee3b2010-09-07 06:06:06 +00002377 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002378 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002379
Daniel Dunbar54074b52010-07-19 05:44:09 +00002380 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002381 OS << " bool OperandsValid = true;\n";
2382 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002383 OS << " if (i + 1 >= Operands.size()) {\n";
2384 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002385 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002386 OS << " }\n";
2387 OS << " if (ValidateOperandClass(Operands[i+1], it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002388 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002389 OS << " // If this operand is broken for all of the instances of this\n";
2390 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002391 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002392 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002393 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2394 OS << " OperandsValid = false;\n";
2395 OS << " break;\n";
2396 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002397
Chris Lattnerce4a3352010-09-06 22:11:18 +00002398 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002399
2400 // Emit check that the required features are available.
2401 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2402 << "!= it->RequiredFeatures) {\n";
2403 OS << " HadMatchOtherThanFeatures = true;\n";
2404 OS << " continue;\n";
2405 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002406 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002407 OS << " // We have selected a definite instruction, convert the parsed\n"
2408 << " // operands into the appropriate MCInst.\n";
2409 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2410 << " it->Opcode, Operands))\n";
2411 OS << " return Match_ConversionFail;\n";
2412 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002413
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002414 // Verify the instruction with the target-specific match predicate function.
2415 OS << " // We have a potential match. Check the target predicate to\n"
2416 << " // handle any context sensitive constraints.\n"
2417 << " unsigned MatchResult;\n"
2418 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2419 << " Match_Success) {\n"
2420 << " Inst.clear();\n"
2421 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002422 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002423 << " continue;\n"
2424 << " }\n\n";
2425
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002426 // Call the post-processing function, if used.
2427 std::string InsnCleanupFn =
2428 AsmParser->getValueAsString("AsmParserInstCleanup");
2429 if (!InsnCleanupFn.empty())
2430 OS << " " << InsnCleanupFn << "(Inst);\n";
2431
Chris Lattner79ed3f72010-09-06 19:22:17 +00002432 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002433 OS << " }\n\n";
2434
Chris Lattnerec6789f2010-09-06 20:08:02 +00002435 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002436 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
2437 OS << " return RetCode;\n";
2438 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002439 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002440
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002441 if (Info.OperandMatchInfo.size())
2442 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2443
Chris Lattner0692ee62010-09-06 19:11:01 +00002444 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002445}