blob: 3576cc93a6fd76b012bbcbc90222a0c0566e837b [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
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000256 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000257 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000258 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000259 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000260 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000261 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000262
263 // Otherwise, order by name to ensure we have a total ordering.
264 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000265 }
266 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000267};
268
Chris Lattner22bc5c42010-11-01 05:06:45 +0000269/// MatchableInfo - Helper class for storing the necessary information for an
270/// instruction or alias which is capable of being matched.
271struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000272 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000273 /// Token - This is the token that the operand came from.
274 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000275
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000276 /// The unique class instance this operand should match.
277 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000278
Chris Lattner567820c2010-11-04 01:42:59 +0000279 /// The operand name this is, if anything.
280 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000281
282 /// The suboperand index within SrcOpName, or -1 for the entire operand.
283 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000284
Bob Wilsona49c7df2011-01-26 19:44:55 +0000285 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000286 };
Bob Wilson828295b2011-01-26 21:26:19 +0000287
Chris Lattner1d13bda2010-11-04 00:43:46 +0000288 /// ResOperand - This represents a single operand in the result instruction
289 /// generated by the match. In cases (like addressing modes) where a single
290 /// assembler operand expands to multiple MCOperands, this represents the
291 /// single assembler operand, not the MCOperand.
292 struct ResOperand {
293 enum {
294 /// RenderAsmOperand - This represents an operand result that is
295 /// generated by calling the render method on the assembly operand. The
296 /// corresponding AsmOperand is specified by AsmOperandNum.
297 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000298
Chris Lattner1d13bda2010-11-04 00:43:46 +0000299 /// TiedOperand - This represents a result operand that is a duplicate of
300 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000301 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000302
Chris Lattner98c870f2010-11-06 19:25:43 +0000303 /// ImmOperand - This represents an immediate value that is dumped into
304 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000305 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000306
Chris Lattner90fd7972010-11-06 19:57:21 +0000307 /// RegOperand - This represents a fixed register that is dumped in.
308 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000309 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000310
Chris Lattner1d13bda2010-11-04 00:43:46 +0000311 union {
312 /// This is the operand # in the AsmOperands list that this should be
313 /// copied from.
314 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000315
Chris Lattner1d13bda2010-11-04 00:43:46 +0000316 /// TiedOperandNum - This is the (earlier) result operand that should be
317 /// copied from.
318 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000319
Chris Lattner98c870f2010-11-06 19:25:43 +0000320 /// ImmVal - This is the immediate value added to the instruction.
321 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000322
Chris Lattner90fd7972010-11-06 19:57:21 +0000323 /// Register - This is the register record.
324 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000325 };
Bob Wilson828295b2011-01-26 21:26:19 +0000326
Bob Wilsona49c7df2011-01-26 19:44:55 +0000327 /// MINumOperands - The number of MCInst operands populated by this
328 /// operand.
329 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000330
Bob Wilsona49c7df2011-01-26 19:44:55 +0000331 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000332 ResOperand X;
333 X.Kind = RenderAsmOperand;
334 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000335 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000336 return X;
337 }
Bob Wilson828295b2011-01-26 21:26:19 +0000338
Bob Wilsona49c7df2011-01-26 19:44:55 +0000339 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000340 ResOperand X;
341 X.Kind = TiedOperand;
342 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000343 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000344 return X;
345 }
Bob Wilson828295b2011-01-26 21:26:19 +0000346
Bob Wilsona49c7df2011-01-26 19:44:55 +0000347 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000348 ResOperand X;
349 X.Kind = ImmOperand;
350 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000351 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000352 return X;
353 }
Bob Wilson828295b2011-01-26 21:26:19 +0000354
Bob Wilsona49c7df2011-01-26 19:44:55 +0000355 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000356 ResOperand X;
357 X.Kind = RegOperand;
358 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000359 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000360 return X;
361 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000362 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363
Chris Lattner3b5aec62010-11-02 17:34:28 +0000364 /// TheDef - This is the definition of the instruction or InstAlias that this
365 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000366 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000367
Chris Lattnerc07bd402010-11-04 02:11:18 +0000368 /// DefRec - This is the definition that it came from.
369 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000370
Chris Lattner662e5a32010-11-06 07:14:44 +0000371 const CodeGenInstruction *getResultInst() const {
372 if (DefRec.is<const CodeGenInstruction*>())
373 return DefRec.get<const CodeGenInstruction*>();
374 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
375 }
Bob Wilson828295b2011-01-26 21:26:19 +0000376
Chris Lattner1d13bda2010-11-04 00:43:46 +0000377 /// ResOperands - This is the operand list that should be built for the result
378 /// MCInst.
379 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380
381 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000382 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 std::string AsmString;
384
Chris Lattnerd19ec052010-11-02 17:30:52 +0000385 /// Mnemonic - This is the first token of the matched instruction, its
386 /// mnemonic.
387 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000388
Chris Lattner3116fef2010-11-02 01:03:43 +0000389 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000390 /// annotated with a class and where in the OperandList they were defined.
391 /// This directly corresponds to the tokenized AsmString after the mnemonic is
392 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000393 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000394
Daniel Dunbar54074b52010-07-19 05:44:09 +0000395 /// Predicates - The required subtarget features to match this instruction.
396 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
397
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000398 /// ConversionFnKind - The enum value which is passed to the generated
399 /// ConvertToMCInst to convert parsed operands into an MCInst for this
400 /// function.
401 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000402
Chris Lattner22bc5c42010-11-01 05:06:45 +0000403 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattner662e5a32010-11-06 07:14:44 +0000404 : TheDef(CGI.TheDef), DefRec(&CGI), AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000405 }
406
Chris Lattner22bc5c42010-11-01 05:06:45 +0000407 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattner662e5a32010-11-06 07:14:44 +0000408 : TheDef(Alias->TheDef), DefRec(Alias), AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000409 }
Bob Wilson828295b2011-01-26 21:26:19 +0000410
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000411 void Initialize(const AsmMatcherInfo &Info,
412 SmallPtrSet<Record*, 16> &SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +0000413
Chris Lattner22bc5c42010-11-01 05:06:45 +0000414 /// Validate - Return true if this matchable is a valid thing to match against
415 /// and perform a bunch of validity checking.
416 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000417
Chris Lattnerd19ec052010-11-02 17:30:52 +0000418 /// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000419 /// register, return the Record for it, otherwise return null.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000420 Record *getSingletonRegisterForAsmOperand(unsigned i,
Bob Wilson828295b2011-01-26 21:26:19 +0000421 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000422
Bob Wilsona49c7df2011-01-26 19:44:55 +0000423 /// FindAsmOperand - Find the AsmOperand with the specified name and
424 /// suboperand index.
425 int FindAsmOperand(StringRef N, int SubOpIdx) const {
426 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
427 if (N == AsmOperands[i].SrcOpName &&
428 SubOpIdx == AsmOperands[i].SubOpIdx)
429 return i;
430 return -1;
431 }
Bob Wilson828295b2011-01-26 21:26:19 +0000432
Bob Wilsona49c7df2011-01-26 19:44:55 +0000433 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
434 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000435 int FindAsmOperandNamed(StringRef N) const {
436 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
437 if (N == AsmOperands[i].SrcOpName)
438 return i;
439 return -1;
440 }
Bob Wilson828295b2011-01-26 21:26:19 +0000441
Chris Lattner41409852010-11-06 07:31:43 +0000442 void BuildInstructionResultOperands();
443 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000444
Chris Lattner22bc5c42010-11-01 05:06:45 +0000445 /// operator< - Compare two matchables.
446 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000447 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000448 if (Mnemonic != RHS.Mnemonic)
449 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000450
Chris Lattner3116fef2010-11-02 01:03:43 +0000451 if (AsmOperands.size() != RHS.AsmOperands.size())
452 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000453
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000454 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000455 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000456 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
457 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000458 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000459 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000460 return false;
461 }
462
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000463 return false;
464 }
465
Bob Wilson1f64ac42011-01-26 21:26:21 +0000466 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000467 /// ambiguously match the same set of operands as \arg RHS (without being a
468 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000469 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000470 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000471 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000472 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000473
Daniel Dunbar2b544812009-08-09 06:05:33 +0000474 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000475 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000476 return false;
477
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000478 // Otherwise, make sure the ordering of the two instructions is unambiguous
479 // by checking that either (a) a token or operand kind discriminates them,
480 // or (b) the ordering among equivalent kinds is consistent.
481
Daniel Dunbar2b544812009-08-09 06:05:33 +0000482 // Tokens and operand kinds are unambiguous (assuming a correct target
483 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000484 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
485 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
486 AsmOperands[i].Class->Kind == ClassInfo::Token)
487 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
488 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000489 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000490
Daniel Dunbar2b544812009-08-09 06:05:33 +0000491 // Otherwise, this operand could commute if all operands are equivalent, or
492 // there is a pair of operands that compare less than and a pair that
493 // compare greater than.
494 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000495 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
496 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000497 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000498 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000499 HasGT = true;
500 }
501
502 return !(HasLT ^ HasGT);
503 }
504
Daniel Dunbar20927f22009-08-07 08:26:05 +0000505 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000506
Chris Lattnerd19ec052010-11-02 17:30:52 +0000507private:
508 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000509};
510
Daniel Dunbar54074b52010-07-19 05:44:09 +0000511/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
512/// feature which participates in instruction matching.
513struct SubtargetFeatureInfo {
514 /// \brief The predicate record for this feature.
515 Record *TheDef;
516
517 /// \brief An unique index assigned to represent this feature.
518 unsigned Index;
519
Chris Lattner0aed1e72010-10-30 20:07:57 +0000520 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000521
Daniel Dunbar54074b52010-07-19 05:44:09 +0000522 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000523 std::string getEnumName() const {
524 return "Feature_" + TheDef->getName();
525 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000526};
527
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000528struct OperandMatchEntry {
529 unsigned OperandMask;
530 MatchableInfo* MI;
531 ClassInfo *CI;
532
533 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
534 unsigned opMask) {
535 OperandMatchEntry X;
536 X.OperandMask = opMask;
537 X.CI = ci;
538 X.MI = mi;
539 return X;
540 }
541};
542
543
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000544class AsmMatcherInfo {
545public:
Chris Lattner67db8832010-12-13 00:23:57 +0000546 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000547 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000548
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000549 /// The tablegen AsmParser record.
550 Record *AsmParser;
551
Chris Lattner02bcbc92010-11-01 01:37:30 +0000552 /// Target - The target information.
553 CodeGenTarget &Target;
554
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000555 /// The AsmParser "RegisterPrefix" value.
556 std::string RegisterPrefix;
557
Devang Patel59f7ee02012-01-05 00:51:28 +0000558 /// The AsmParser variant number.
559 int AsmVariantNo;
560
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000561 /// The classes which are needed for matching.
562 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000563
Chris Lattner22bc5c42010-11-01 05:06:45 +0000564 /// The information on the matchables to match.
565 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000566
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000567 /// Info for custom matching operands by user defined methods.
568 std::vector<OperandMatchEntry> OperandMatchInfo;
569
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000570 /// Map of Register records to their class information.
571 std::map<Record*, ClassInfo*> RegisterClasses;
572
Daniel Dunbar54074b52010-07-19 05:44:09 +0000573 /// Map of Predicate records to their subtarget information.
574 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000575
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000576private:
577 /// Map of token to class information which has already been constructed.
578 std::map<std::string, ClassInfo*> TokenClasses;
579
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000580 /// Map of RegisterClass records to their class information.
581 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000582
Daniel Dunbar338825c2009-08-10 18:41:10 +0000583 /// Map of AsmOperandClass records to their class information.
584 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000585
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000586private:
587 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000588 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000589
590 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000591 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
Jim Grosbach48c1f842011-10-28 22:32:53 +0000592 int SubOpIdx);
593 ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000594
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000595 /// BuildRegisterClasses - Build the ClassInfo* instances for register
596 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000597 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000598
599 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
600 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000601 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000602
Bob Wilsona49c7df2011-01-26 19:44:55 +0000603 void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
604 unsigned AsmOpIdx);
605 void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000606 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000607
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000608public:
Bob Wilson828295b2011-01-26 21:26:19 +0000609 AsmMatcherInfo(Record *AsmParser,
610 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000611 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000612
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000613 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000614 void BuildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000615
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000616 /// BuildOperandMatchInfo - Build the necessary information to handle user
617 /// defined operand parsing methods.
618 void BuildOperandMatchInfo();
619
Chris Lattner6fa152c2010-10-30 20:15:02 +0000620 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
621 /// given operand.
622 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
623 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
624 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
625 SubtargetFeatures.find(Def);
626 return I == SubtargetFeatures.end() ? 0 : I->second;
627 }
Chris Lattner67db8832010-12-13 00:23:57 +0000628
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000629 RecordKeeper &getRecords() const {
630 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000631 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000632};
633
Daniel Dunbar20927f22009-08-07 08:26:05 +0000634}
635
Chris Lattner22bc5c42010-11-01 05:06:45 +0000636void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000637 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000638
Chris Lattner3116fef2010-11-02 01:03:43 +0000639 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000640 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000641 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000642 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000643 }
644}
645
Chris Lattner22bc5c42010-11-01 05:06:45 +0000646void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
647 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000648 // TODO: Eventually support asmparser for Variant != 0.
Devang Patel59f7ee02012-01-05 00:51:28 +0000649 AsmString =
650 CodeGenInstruction::FlattenAsmStringVariants(AsmString, Info.AsmVariantNo);
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.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000739 if (AsmOperands.empty())
740 throw TGError(TheDef->getLoc(),
741 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000742 Mnemonic = AsmOperands[0].Token;
Devang Patelb78307f2012-01-07 01:22:23 +0000743 // FIXME : Check and raise an error if it is register.
744 if (Mnemonic[0] == '$')
Chris Lattnerd19ec052010-11-02 17:30:52 +0000745 throw TGError(TheDef->getLoc(),
746 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000747
Chris Lattnerd19ec052010-11-02 17:30:52 +0000748 // Remove the first operand, it is tracked in the mnemonic field.
749 AsmOperands.erase(AsmOperands.begin());
750}
751
Chris Lattner22bc5c42010-11-01 05:06:45 +0000752bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
753 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000754 if (AsmString.empty())
755 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000756
Chris Lattner22bc5c42010-11-01 05:06:45 +0000757 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000758 // isCodeGenOnly if they are pseudo instructions.
759 if (AsmString.find('\n') != std::string::npos)
760 throw TGError(TheDef->getLoc(),
761 "multiline instruction is not valid for the asmparser, "
762 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000763
Chris Lattner4164f6b2010-11-01 04:44:29 +0000764 // Remove comments from the asm string. We know that the asmstring only
765 // has one line.
766 if (!CommentDelimiter.empty() &&
767 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
768 throw TGError(TheDef->getLoc(),
769 "asmstring for instruction has comment character in it, "
770 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000771
Chris Lattner22bc5c42010-11-01 05:06:45 +0000772 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000773 // handle, the target should be refactored to use operands instead of
774 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000775 //
776 // Also, check for instructions which reference the operand multiple times;
777 // this implies a constraint we would not honor.
778 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000779 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
780 StringRef Tok = AsmOperands[i].Token;
781 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000782 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000783 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000784 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000785
Chris Lattner22bc5c42010-11-01 05:06:45 +0000786 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000787 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000788 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000789 if (!Hack)
790 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000791 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000792 "' can never be matched!");
793 // FIXME: Should reject these. The ARM backend hits this with $lane in a
794 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000795 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000796 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000797 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000798 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000799 });
800 return false;
801 }
802 }
Bob Wilson828295b2011-01-26 21:26:19 +0000803
Chris Lattner5bc93872010-11-01 04:34:44 +0000804 return true;
805}
806
Chris Lattnerd19ec052010-11-02 17:30:52 +0000807/// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner02bcbc92010-11-01 01:37:30 +0000808/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000809Record *MatchableInfo::
Chris Lattnerd19ec052010-11-02 17:30:52 +0000810getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{
811 StringRef Tok = AsmOperands[i].Token;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000812 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000813 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000814
Chris Lattner02bcbc92010-11-01 01:37:30 +0000815 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000816 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
817 return Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000818
Chris Lattner1de88232010-11-01 01:47:07 +0000819 // If there is no register prefix (i.e. "%" in "%eax"), then this may
820 // be some random non-register token, just ignore it.
821 if (Info.RegisterPrefix.empty())
822 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000823
Chris Lattnerec6f0962010-11-02 18:10:06 +0000824 // Otherwise, we have something invalid prefixed with the register prefix,
825 // such as %foo.
Chris Lattner1de88232010-11-01 01:47:07 +0000826 std::string Err = "unable to find register for '" + RegName.str() +
827 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000828 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000829}
830
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000831static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000832 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000833
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000834 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
835 switch (*it) {
836 case '*': Res += "_STAR_"; break;
837 case '%': Res += "_PCT_"; break;
838 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000839 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000840 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000841 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000842 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000843 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000844 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000845 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000846 }
847 }
848
849 return Res;
850}
851
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000852ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000853 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000854
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000855 if (!Entry) {
856 Entry = new ClassInfo();
857 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000858 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000859 Entry->Name = "MCK_" + getEnumNameForToken(Token);
860 Entry->ValueName = Token;
861 Entry->PredicateMethod = "<invalid>";
862 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000863 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000864 Classes.push_back(Entry);
865 }
866
867 return Entry;
868}
869
870ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000871AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
872 int SubOpIdx) {
873 Record *Rec = OI.Rec;
874 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000875 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +0000876 return getOperandClass(Rec, SubOpIdx);
877}
Bob Wilsona49c7df2011-01-26 19:44:55 +0000878
Jim Grosbach48c1f842011-10-28 22:32:53 +0000879ClassInfo *
880AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000881 if (Rec->isSubClassOf("RegisterOperand")) {
882 // RegisterOperand may have an associated ParserMatchClass. If it does,
883 // use it, else just fall back to the underlying register class.
884 const RecordVal *R = Rec->getValue("ParserMatchClass");
885 if (R == 0 || R->getValue() == 0)
886 throw "Record `" + Rec->getName() +
887 "' does not have a ParserMatchClass!\n";
888
David Greene05bce0b2011-07-29 22:43:06 +0000889 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000890 Record *MatchClass = DI->getDef();
891 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
892 return CI;
893 }
894
895 // No custom match class. Just use the register class.
896 Record *ClassRec = Rec->getValueAsDef("RegClass");
897 if (!ClassRec)
898 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
899 "' has no associated register class!\n");
900 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
901 return CI;
902 throw TGError(Rec->getLoc(), "register class has no class info!");
903 }
904
905
Bob Wilsona49c7df2011-01-26 19:44:55 +0000906 if (Rec->isSubClassOf("RegisterClass")) {
907 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000908 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000909 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000910 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000911
Bob Wilsona49c7df2011-01-26 19:44:55 +0000912 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
913 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000914 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
915 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000916
Bob Wilsona49c7df2011-01-26 19:44:55 +0000917 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000918}
919
Chris Lattner1de88232010-11-01 01:47:07 +0000920void AsmMatcherInfo::
921BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000922 const std::vector<CodeGenRegister*> &Registers =
923 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000924 ArrayRef<CodeGenRegisterClass*> RegClassList =
925 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000926
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000927 // The register sets used for matching.
928 std::set< std::set<Record*> > RegisterSets;
929
Jim Grosbacha7c78222010-10-29 22:13:48 +0000930 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000931 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +0000932 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000933 RegisterSets.insert(std::set<Record*>(
934 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000935
936 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000937 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
938 ie = SingletonRegisters.end(); it != ie; ++it) {
939 Record *Rec = *it;
940 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
941 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000942
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000943 // Introduce derived sets where necessary (when a register does not determine
944 // a unique register set class), and build the mapping of registers to the set
945 // they should classify to.
946 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000947 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000948 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000949 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000950 // Compute the intersection of all sets containing this register.
951 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000952
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000953 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
954 ie = RegisterSets.end(); it != ie; ++it) {
955 if (!it->count(CGR.TheDef))
956 continue;
957
958 if (ContainingSet.empty()) {
959 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000960 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000961 }
Bob Wilson828295b2011-01-26 21:26:19 +0000962
Chris Lattnerec6f0962010-11-02 18:10:06 +0000963 std::set<Record*> Tmp;
964 std::swap(Tmp, ContainingSet);
965 std::insert_iterator< std::set<Record*> > II(ContainingSet,
966 ContainingSet.begin());
967 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000968 }
969
970 if (!ContainingSet.empty()) {
971 RegisterSets.insert(ContainingSet);
972 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
973 }
974 }
975
976 // Construct the register classes.
977 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
978 unsigned Index = 0;
979 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
980 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
981 ClassInfo *CI = new ClassInfo();
982 CI->Kind = ClassInfo::RegisterClass0 + Index;
983 CI->ClassName = "Reg" + utostr(Index);
984 CI->Name = "MCK_Reg" + utostr(Index);
985 CI->ValueName = "";
986 CI->PredicateMethod = ""; // unused
987 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000988 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000989 Classes.push_back(CI);
990 RegisterSetClasses.insert(std::make_pair(*it, CI));
991 }
992
993 // Find the superclasses; we could compute only the subgroup lattice edges,
994 // but there isn't really a point.
995 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
996 ie = RegisterSets.end(); it != ie; ++it) {
997 ClassInfo *CI = RegisterSetClasses[*it];
998 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
999 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001000 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001001 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
1002 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
1003 }
1004
1005 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001006 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001007 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001008 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001009 // Def will be NULL for non-user defined register classes.
1010 Record *Def = RC.getDef();
1011 if (!Def)
1012 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001013 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1014 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001015 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001016 CI->ClassName = RC.getName();
1017 CI->Name = "MCK_" + RC.getName();
1018 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001019 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001020 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001021
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001022 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001023 }
1024
1025 // Populate the map for individual registers.
1026 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1027 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001028 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001029
1030 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001031 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1032 ie = SingletonRegisters.end(); it != ie; ++it) {
1033 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001034 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001035 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001036
Chris Lattner1de88232010-11-01 01:47:07 +00001037 if (CI->ValueName.empty()) {
1038 CI->ClassName = Rec->getName();
1039 CI->Name = "MCK_" + Rec->getName();
1040 CI->ValueName = Rec->getName();
1041 } else
1042 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001043 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001044}
1045
Chris Lattner02bcbc92010-11-01 01:37:30 +00001046void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001047 std::vector<Record*> AsmOperands =
1048 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001049
1050 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001051 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001052 ie = AsmOperands.end(); it != ie; ++it)
1053 AsmOperandClasses[*it] = new ClassInfo();
1054
Daniel Dunbar338825c2009-08-10 18:41:10 +00001055 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001056 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001057 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001058 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001059 CI->Kind = ClassInfo::UserClass0 + Index;
1060
David Greene05bce0b2011-07-29 22:43:06 +00001061 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001062 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001063 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001064 if (!DI) {
1065 PrintError((*it)->getLoc(), "Invalid super class reference!");
1066 continue;
1067 }
1068
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001069 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1070 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001071 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001072 else
1073 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001074 }
1075 CI->ClassName = (*it)->getValueAsString("Name");
1076 CI->Name = "MCK_" + CI->ClassName;
1077 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001078
1079 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001080 Init *PMName = (*it)->getValueInit("PredicateMethod");
1081 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001082 CI->PredicateMethod = SI->getValue();
1083 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001084 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001085 "Unexpected PredicateMethod field!");
1086 CI->PredicateMethod = "is" + CI->ClassName;
1087 }
1088
1089 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001090 Init *RMName = (*it)->getValueInit("RenderMethod");
1091 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001092 CI->RenderMethod = SI->getValue();
1093 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001094 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001095 "Unexpected RenderMethod field!");
1096 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1097 }
1098
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001099 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001100 Init *PRMName = (*it)->getValueInit("ParserMethod");
1101 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001102 CI->ParserMethod = SI->getValue();
1103
Daniel Dunbar338825c2009-08-10 18:41:10 +00001104 AsmOperandClasses[*it] = CI;
1105 Classes.push_back(CI);
1106 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001107}
1108
Bob Wilson828295b2011-01-26 21:26:19 +00001109AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1110 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001111 RecordKeeper &records)
Chris Lattner67db8832010-12-13 00:23:57 +00001112 : Records(records), AsmParser(asmParser), Target(target),
Devang Patel59f7ee02012-01-05 00:51:28 +00001113 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")),
1114 AsmVariantNo(AsmParser->getValueAsInt("Variant")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001115}
1116
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001117/// BuildOperandMatchInfo - Build the necessary information to handle user
1118/// defined operand parsing methods.
1119void AsmMatcherInfo::BuildOperandMatchInfo() {
1120
1121 /// Map containing a mask with all operands indicies that can be found for
1122 /// that class inside a instruction.
1123 std::map<ClassInfo*, unsigned> OpClassMask;
1124
1125 for (std::vector<MatchableInfo*>::const_iterator it =
1126 Matchables.begin(), ie = Matchables.end();
1127 it != ie; ++it) {
1128 MatchableInfo &II = **it;
1129 OpClassMask.clear();
1130
1131 // Keep track of all operands of this instructions which belong to the
1132 // same class.
1133 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1134 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1135 if (Op.Class->ParserMethod.empty())
1136 continue;
1137 unsigned &OperandMask = OpClassMask[Op.Class];
1138 OperandMask |= (1 << i);
1139 }
1140
1141 // Generate operand match info for each mnemonic/operand class pair.
1142 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1143 iie = OpClassMask.end(); iit != iie; ++iit) {
1144 unsigned OpMask = iit->second;
1145 ClassInfo *CI = iit->first;
1146 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1147 }
1148 }
1149}
1150
Chris Lattner02bcbc92010-11-01 01:37:30 +00001151void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001152 // Build information about all of the AssemblerPredicates.
1153 std::vector<Record*> AllPredicates =
1154 Records.getAllDerivedDefinitions("Predicate");
1155 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1156 Record *Pred = AllPredicates[i];
1157 // Ignore predicates that are not intended for the assembler.
1158 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1159 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001160
Chris Lattner4164f6b2010-11-01 04:44:29 +00001161 if (Pred->getName().empty())
1162 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001163
Chris Lattner0aed1e72010-10-30 20:07:57 +00001164 unsigned FeatureNo = SubtargetFeatures.size();
1165 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1166 assert(FeatureNo < 32 && "Too many subtarget features!");
1167 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001168
Eli Friedman60435482011-07-08 20:07:05 +00001169 std::string CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
Bob Wilson828295b2011-01-26 21:26:19 +00001170
Chris Lattner39ee0362010-10-31 19:10:56 +00001171 // Parse the instructions; we need to do this first so that we can gather the
1172 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001173 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +00001174 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1175 E = Target.inst_end(); I != E; ++I) {
1176 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001177
Chris Lattner39ee0362010-10-31 19:10:56 +00001178 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1179 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +00001180 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001181 continue;
1182
Chris Lattner5bc93872010-11-01 04:34:44 +00001183 // Ignore "codegen only" instructions.
1184 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1185 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001186
Chris Lattner1d13bda2010-11-04 00:43:46 +00001187 // Validate the operand list to ensure we can handle this instruction.
1188 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1189 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001190
Chris Lattner1d13bda2010-11-04 00:43:46 +00001191 // Validate tied operands.
1192 if (OI.getTiedRegister() != -1) {
Bob Wilson828295b2011-01-26 21:26:19 +00001193 // If we have a tied operand that consists of multiple MCOperands,
1194 // reject it. We reject aliases and ignore instructions for now.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001195 if (OI.MINumOperands != 1) {
1196 // FIXME: Should reject these. The ARM backend hits this with $lane
1197 // in a bunch of instructions. It is unclear what the right answer is.
1198 DEBUG({
1199 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1200 << "ignoring instruction with multi-operand tied operand '"
1201 << OI.Name << "'\n";
1202 });
1203 continue;
1204 }
1205 }
1206 }
Bob Wilson828295b2011-01-26 21:26:19 +00001207
Chris Lattner22bc5c42010-11-01 05:06:45 +00001208 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001209
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001210 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001211
Chris Lattner4d43d0f2010-11-01 01:07:14 +00001212 // Ignore instructions which shouldn't be matched and diagnose invalid
1213 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001214 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +00001215 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001216
Chris Lattner5bc93872010-11-01 04:34:44 +00001217 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1218 //
1219 // FIXME: This is a total hack.
Chris Lattner5abd1eb2010-11-06 06:43:11 +00001220 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1221 StringRef(II->TheDef->getName()).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001222 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001223
Chris Lattner22bc5c42010-11-01 05:06:45 +00001224 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001225 }
Bob Wilson828295b2011-01-26 21:26:19 +00001226
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001227 // Parse all of the InstAlias definitions and stick them in the list of
1228 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001229 std::vector<Record*> AllInstAliases =
1230 Records.getAllDerivedDefinitions("InstAlias");
1231 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
Chris Lattner225549f2010-11-06 06:39:47 +00001232 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001233
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001234 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1235 // filter the set of instruction aliases we consider, based on the target
1236 // instruction.
1237 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1238 MatchPrefix))
1239 continue;
1240
Chris Lattner22bc5c42010-11-01 05:06:45 +00001241 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Bob Wilson828295b2011-01-26 21:26:19 +00001242
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001243 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001244
Chris Lattner22bc5c42010-11-01 05:06:45 +00001245 // Validate the alias definitions.
1246 II->Validate(CommentDelimiter, false);
Bob Wilson828295b2011-01-26 21:26:19 +00001247
Chris Lattnerb501d4f2010-11-01 05:34:34 +00001248 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001249 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001250
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001251 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001252 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001253
1254 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001255 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001256
Chris Lattner0bb780c2010-11-04 00:57:06 +00001257 // Build the information about matchables, now that we have fully formed
1258 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001259 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1260 ie = Matchables.end(); it != ie; ++it) {
1261 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001262
Chris Lattnere206fcf2010-09-06 21:01:37 +00001263 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001264 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1265 // don't precompute the loop bound.
1266 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001267 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001268 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001269
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001270 // Check for singleton registers.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001271 if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) {
1272 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001273 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1274 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001275 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001276 }
1277
Daniel Dunbar20927f22009-08-07 08:26:05 +00001278 // Check for simple tokens.
1279 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001280 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001281 continue;
1282 }
1283
Chris Lattner7ad31472010-11-06 22:06:03 +00001284 if (Token.size() > 1 && isdigit(Token[1])) {
1285 Op.Class = getTokenClass(Token);
1286 continue;
1287 }
Bob Wilson828295b2011-01-26 21:26:19 +00001288
Chris Lattnerc07bd402010-11-04 02:11:18 +00001289 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001290 StringRef OperandName;
1291 if (Token[1] == '{')
1292 OperandName = Token.substr(2, Token.size() - 3);
1293 else
1294 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001295
Chris Lattnerc07bd402010-11-04 02:11:18 +00001296 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001297 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001298 else
Chris Lattner225549f2010-11-06 06:39:47 +00001299 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001300 }
Bob Wilson828295b2011-01-26 21:26:19 +00001301
Chris Lattner41409852010-11-06 07:31:43 +00001302 if (II->DefRec.is<const CodeGenInstruction*>())
1303 II->BuildInstructionResultOperands();
1304 else
1305 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001306 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001307
Jim Grosbacha66512e2011-12-06 23:43:54 +00001308 // Process token alias definitions and set up the associated superclass
1309 // information.
1310 std::vector<Record*> AllTokenAliases =
1311 Records.getAllDerivedDefinitions("TokenAlias");
1312 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1313 Record *Rec = AllTokenAliases[i];
1314 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1315 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1316 FromClass->SuperClasses.push_back(ToClass);
1317 }
1318
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001319 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001320 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001321}
1322
Chris Lattner0bb780c2010-11-04 00:57:06 +00001323/// BuildInstructionOperandReference - The specified operand is a reference to a
1324/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1325void AsmMatcherInfo::
1326BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001327 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001328 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001329 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1330 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001331 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001332
Chris Lattner662e5a32010-11-06 07:14:44 +00001333 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001334 unsigned Idx;
1335 if (!Operands.hasOperandNamed(OperandName, Idx))
1336 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1337 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001338
Bob Wilsona49c7df2011-01-26 19:44:55 +00001339 // If the instruction operand has multiple suboperands, but the parser
1340 // match class for the asm operand is still the default "ImmAsmOperand",
1341 // then handle each suboperand separately.
1342 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1343 Record *Rec = Operands[Idx].Rec;
1344 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1345 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1346 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1347 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1348 StringRef Token = Op->Token; // save this in case Op gets moved
1349 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1350 MatchableInfo::AsmOperand NewAsmOp(Token);
1351 NewAsmOp.SubOpIdx = SI;
1352 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1353 }
1354 // Replace Op with first suboperand.
1355 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1356 Op->SubOpIdx = 0;
1357 }
1358 }
1359
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001360 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001361 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001362
1363 // If the named operand is tied, canonicalize it to the untied operand.
1364 // For example, something like:
1365 // (outs GPR:$dst), (ins GPR:$src)
1366 // with an asmstring of
1367 // "inc $src"
1368 // we want to canonicalize to:
1369 // "inc $dst"
1370 // so that we know how to provide the $dst operand when filling in the result.
1371 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001372 if (OITied != -1) {
1373 // The tied operand index is an MIOperand index, find the operand that
1374 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001375 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1376 OperandName = Operands[Idx.first].Name;
1377 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001378 }
Bob Wilson828295b2011-01-26 21:26:19 +00001379
Bob Wilsona49c7df2011-01-26 19:44:55 +00001380 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001381}
1382
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001383/// BuildAliasOperandReference - When parsing an operand reference out of the
1384/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1385/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001386void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1387 StringRef OperandName,
1388 MatchableInfo::AsmOperand &Op) {
1389 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001390
Chris Lattnerc07bd402010-11-04 02:11:18 +00001391 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001392 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001393 if (CGA.ResultOperands[i].isRecord() &&
1394 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001395 // It's safe to go with the first one we find, because CodeGenInstAlias
1396 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001397 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001398 // Use the match class from the Alias definition, not the
1399 // destination instruction, as we may have an immediate that's
1400 // being munged by the match class.
1401 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001402 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001403 Op.SrcOpName = OperandName;
1404 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001405 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001406
1407 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1408 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001409}
1410
Chris Lattner41409852010-11-06 07:31:43 +00001411void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001412 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001413
Chris Lattner662e5a32010-11-06 07:14:44 +00001414 // Loop over all operands of the result instruction, determining how to
1415 // populate them.
1416 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1417 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001418
1419 // If this is a tied operand, just copy from the previously handled operand.
1420 int TiedOp = OpInfo.getTiedRegister();
1421 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001422 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001423 continue;
1424 }
Bob Wilson828295b2011-01-26 21:26:19 +00001425
Bob Wilsona49c7df2011-01-26 19:44:55 +00001426 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001427 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001428 if (OpInfo.Name.empty() || SrcOperand == -1)
1429 throw TGError(TheDef->getLoc(), "Instruction '" +
1430 TheDef->getName() + "' has operand '" + OpInfo.Name +
1431 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001432
Bob Wilsona49c7df2011-01-26 19:44:55 +00001433 // Check if the one AsmOperand populates the entire operand.
1434 unsigned NumOperands = OpInfo.MINumOperands;
1435 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1436 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001437 continue;
1438 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001439
1440 // Add a separate ResOperand for each suboperand.
1441 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1442 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1443 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1444 "unexpected AsmOperands for suboperands");
1445 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1446 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001447 }
1448}
1449
Chris Lattner41409852010-11-06 07:31:43 +00001450void MatchableInfo::BuildAliasResultOperands() {
1451 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1452 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001453
Chris Lattner41409852010-11-06 07:31:43 +00001454 // Loop over all operands of the result instruction, determining how to
1455 // populate them.
1456 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001457 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001458 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001459 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001460
Chris Lattner41409852010-11-06 07:31:43 +00001461 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001462 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001463 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001464 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001465 continue;
1466 }
1467
Bob Wilsona49c7df2011-01-26 19:44:55 +00001468 // Handle all the suboperands for this operand.
1469 const std::string &OpName = OpInfo->Name;
1470 for ( ; AliasOpNo < LastOpNo &&
1471 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1472 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1473
1474 // Find out what operand from the asmparser that this MCInst operand
1475 // comes from.
1476 switch (CGA.ResultOperands[AliasOpNo].Kind) {
1477 default: assert(0 && "unexpected InstAlias operand kind");
1478 case CodeGenInstAlias::ResultOperand::K_Record: {
1479 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1480 int SrcOperand = FindAsmOperand(Name, SubIdx);
1481 if (SrcOperand == -1)
1482 throw TGError(TheDef->getLoc(), "Instruction '" +
1483 TheDef->getName() + "' has operand '" + OpName +
1484 "' that doesn't appear in asm string!");
1485 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1486 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1487 NumOperands));
1488 break;
1489 }
1490 case CodeGenInstAlias::ResultOperand::K_Imm: {
1491 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1492 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1493 break;
1494 }
1495 case CodeGenInstAlias::ResultOperand::K_Reg: {
1496 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1497 ResOperands.push_back(ResOperand::getRegOp(Reg));
1498 break;
1499 }
1500 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001501 }
Chris Lattner41409852010-11-06 07:31:43 +00001502 }
1503}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001504
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001505static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001506 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001507 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001508 // Write the convert function to a separate stream, so we can drop it after
1509 // the enum.
1510 std::string ConvertFnBody;
1511 raw_string_ostream CvtOS(ConvertFnBody);
1512
Daniel Dunbar20927f22009-08-07 08:26:05 +00001513 // Function we have already generated.
1514 std::set<std::string> GeneratedFns;
1515
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001516 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001517 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1518 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001519 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001520 << " const SmallVectorImpl<MCParsedAsmOperand*"
1521 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001522 CvtOS << " Inst.setOpcode(Opcode);\n";
1523 CvtOS << " switch (Kind) {\n";
1524 CvtOS << " default:\n";
1525
1526 // Start the enum, which we will generate inline.
1527
Chris Lattnerd51257a2010-11-02 23:18:43 +00001528 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001529 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001530
Chris Lattner98986712010-01-14 22:21:20 +00001531 // TargetOperandClass - This is the target's operand class, like X86Operand.
1532 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001533
Chris Lattner22bc5c42010-11-01 05:06:45 +00001534 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001535 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001536 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001537
Daniel Dunbarcf120672011-02-04 17:12:15 +00001538 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001539 std::string AsmMatchConverter =
1540 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001541 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001542 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001543 II.ConversionFnKind = Signature;
1544
1545 // Check if we have already generated this signature.
1546 if (!GeneratedFns.insert(Signature).second)
1547 continue;
1548
1549 // If not, emit it now. Add to the enum list.
1550 OS << " " << Signature << ",\n";
1551
1552 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001553 CvtOS << " return " << AsmMatchConverter
1554 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001555 continue;
1556 }
1557
Daniel Dunbar20927f22009-08-07 08:26:05 +00001558 // Build the conversion function signature.
1559 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001560 std::string CaseBody;
1561 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001562
Chris Lattnerdda855d2010-11-02 21:49:44 +00001563 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001564 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1565 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001566
Chris Lattner1d13bda2010-11-04 00:43:46 +00001567 // Generate code to populate each result operand.
1568 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001569 case MatchableInfo::ResOperand::RenderAsmOperand: {
1570 // This comes from something we parsed.
1571 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001572
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001573 // Registers are always converted the same, don't duplicate the
1574 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001575 Signature += "__";
1576 if (Op.Class->isRegisterClass())
1577 Signature += "Reg";
1578 else
1579 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001580 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001581 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001582
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001583 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001584 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001585 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001586 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001587 }
Bob Wilson828295b2011-01-26 21:26:19 +00001588
Chris Lattner1d13bda2010-11-04 00:43:46 +00001589 case MatchableInfo::ResOperand::TiedOperand: {
1590 // If this operand is tied to a previous one, just copy the MCInst
1591 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001592 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001593 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001594 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001595 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1596 Signature += "__Tie" + utostr(TiedOp);
1597 break;
1598 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001599 case MatchableInfo::ResOperand::ImmOperand: {
1600 int64_t Val = OpInfo.ImmVal;
1601 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1602 Signature += "__imm" + itostr(Val);
1603 break;
1604 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001605 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001606 if (OpInfo.Register == 0) {
1607 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1608 Signature += "__reg0";
1609 } else {
1610 std::string N = getQualifiedName(OpInfo.Register);
1611 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1612 Signature += "__reg" + OpInfo.Register->getName();
1613 }
Bob Wilson828295b2011-01-26 21:26:19 +00001614 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001615 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001616 }
Bob Wilson828295b2011-01-26 21:26:19 +00001617
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001618 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001619
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001620 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001621 if (!GeneratedFns.insert(Signature).second)
1622 continue;
1623
Chris Lattnerdda855d2010-11-02 21:49:44 +00001624 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001625 OS << " " << Signature << ",\n";
1626
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001627 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001628 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001629 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001630 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001631
1632 // Finish the convert function.
1633
1634 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001635 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001636 CvtOS << "}\n\n";
1637
1638 // Finish the enum, and drop the convert function after it.
1639
1640 OS << " NumConversionVariants\n";
1641 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001642
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001643 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001644}
1645
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001646/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1647static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1648 std::vector<ClassInfo*> &Infos,
1649 raw_ostream &OS) {
1650 OS << "namespace {\n\n";
1651
1652 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1653 << "/// instruction matching.\n";
1654 OS << "enum MatchClassKind {\n";
1655 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001656 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001657 ie = Infos.end(); it != ie; ++it) {
1658 ClassInfo &CI = **it;
1659 OS << " " << CI.Name << ", // ";
1660 if (CI.Kind == ClassInfo::Token) {
1661 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001662 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001663 if (!CI.ValueName.empty())
1664 OS << "register class '" << CI.ValueName << "'\n";
1665 else
1666 OS << "derived register class\n";
1667 } else {
1668 OS << "user defined class '" << CI.ValueName << "'\n";
1669 }
1670 }
1671 OS << " NumMatchClassKinds\n";
1672 OS << "};\n\n";
1673
1674 OS << "}\n\n";
1675}
1676
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001677/// EmitValidateOperandClass - Emit the function to validate an operand class.
1678static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1679 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001680 OS << "static bool validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001681 << "MatchClassKind Kind) {\n";
1682 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001683 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001684
Kevin Enderby89381832011-07-15 18:30:43 +00001685 // The InvalidMatchClass is not to match any operand.
1686 OS << " if (Kind == InvalidMatchClass)\n";
1687 OS << " return false;\n\n";
1688
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001689 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001690 OS << " if (Operand.isToken())\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00001691 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind);"
1692 << "\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001693
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001694 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001695 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001696 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001697 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001698 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001699 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001700 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1701 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001702 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001703 << it->first->getName() << ": OpKind = " << it->second->Name
1704 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001705 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001706 OS << " return isSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001707 OS << " }\n\n";
1708
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001709 // Check the user classes. We don't care what order since we're only
1710 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001711 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001712 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001713 ClassInfo &CI = **it;
1714
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001715 if (!CI.isUserClass())
1716 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001717
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001718 OS << " // '" << CI.ClassName << "' class\n";
1719 OS << " if (Kind == " << CI.Name
1720 << " && Operand." << CI.PredicateMethod << "()) {\n";
1721 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001722 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001723 }
Bob Wilson828295b2011-01-26 21:26:19 +00001724
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001725 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001726 OS << "}\n\n";
1727}
1728
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001729/// EmitIsSubclass - Emit the subclass predicate function.
1730static void EmitIsSubclass(CodeGenTarget &Target,
1731 std::vector<ClassInfo*> &Infos,
1732 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001733 OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1734 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001735 OS << " if (A == B)\n";
1736 OS << " return true;\n\n";
1737
1738 OS << " switch (A) {\n";
1739 OS << " default:\n";
1740 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001741 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001742 ie = Infos.end(); it != ie; ++it) {
1743 ClassInfo &A = **it;
1744
Jim Grosbacha66512e2011-12-06 23:43:54 +00001745 std::vector<StringRef> SuperClasses;
1746 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
1747 ie = Infos.end(); it != ie; ++it) {
1748 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001749
Jim Grosbacha66512e2011-12-06 23:43:54 +00001750 if (&A != &B && A.isSubsetOf(B))
1751 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001752 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00001753
1754 if (SuperClasses.empty())
1755 continue;
1756
1757 OS << "\n case " << A.Name << ":\n";
1758
1759 if (SuperClasses.size() == 1) {
1760 OS << " return B == " << SuperClasses.back() << ";\n";
1761 continue;
1762 }
1763
1764 OS << " switch (B) {\n";
1765 OS << " default: return false;\n";
1766 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1767 OS << " case " << SuperClasses[i] << ": return true;\n";
1768 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001769 }
1770 OS << " }\n";
1771 OS << "}\n\n";
1772}
1773
Daniel Dunbar245f0582009-08-08 21:22:41 +00001774/// EmitMatchTokenString - Emit the function to match a token string to the
1775/// appropriate match class value.
1776static void EmitMatchTokenString(CodeGenTarget &Target,
1777 std::vector<ClassInfo*> &Infos,
1778 raw_ostream &OS) {
1779 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001780 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001781 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001782 ie = Infos.end(); it != ie; ++it) {
1783 ClassInfo &CI = **it;
1784
1785 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001786 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1787 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001788 }
1789
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001790 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001791
Chris Lattner5845e5c2010-09-06 02:01:51 +00001792 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001793
1794 OS << " return InvalidMatchClass;\n";
1795 OS << "}\n\n";
1796}
Chris Lattner70add882009-08-08 20:02:57 +00001797
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001798/// EmitMatchRegisterName - Emit the function to match a string to the target
1799/// specific register enum.
1800static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1801 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001802 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001803 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001804 const std::vector<CodeGenRegister*> &Regs =
1805 Target.getRegBank().getRegisters();
1806 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1807 const CodeGenRegister *Reg = Regs[i];
1808 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001809 continue;
1810
Chris Lattner5845e5c2010-09-06 02:01:51 +00001811 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001812 Reg->TheDef->getValueAsString("AsmName"),
1813 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001814 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001815
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001816 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001817
Chris Lattner5845e5c2010-09-06 02:01:51 +00001818 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001819
Daniel Dunbar245f0582009-08-08 21:22:41 +00001820 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001821 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001822}
Daniel Dunbara027d222009-07-31 02:32:59 +00001823
Daniel Dunbar54074b52010-07-19 05:44:09 +00001824/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1825/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001826static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001827 raw_ostream &OS) {
1828 OS << "// Flags for subtarget features that participate in "
1829 << "instruction matching.\n";
1830 OS << "enum SubtargetFeatureFlag {\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;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001835 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001836 }
1837 OS << " Feature_None = 0\n";
1838 OS << "};\n\n";
1839}
1840
1841/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1842/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001843static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001844 raw_ostream &OS) {
1845 std::string ClassName =
1846 Info.AsmParser->getValueAsString("AsmParserClassName");
1847
Chris Lattner02bcbc92010-11-01 01:37:30 +00001848 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00001849 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001850 OS << " unsigned Features = 0;\n";
1851 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1852 it = Info.SubtargetFeatures.begin(),
1853 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1854 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00001855
1856 OS << " if (";
Evan Chengfbc38d22011-07-08 18:04:22 +00001857 std::string CondStorage = SFI.TheDef->getValueAsString("AssemblerCondString");
1858 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00001859 std::pair<StringRef,StringRef> Comma = Conds.split(',');
1860 bool First = true;
1861 do {
1862 if (!First)
1863 OS << " && ";
1864
1865 bool Neg = false;
1866 StringRef Cond = Comma.first;
1867 if (Cond[0] == '!') {
1868 Neg = true;
1869 Cond = Cond.substr(1);
1870 }
1871
1872 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
1873 if (Neg)
1874 OS << " == 0";
1875 else
1876 OS << " != 0";
1877 OS << ")";
1878
1879 if (Comma.second.empty())
1880 break;
1881
1882 First = false;
1883 Comma = Comma.second.split(',');
1884 } while (true);
1885
1886 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001887 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001888 }
1889 OS << " return Features;\n";
1890 OS << "}\n\n";
1891}
1892
Chris Lattner6fa152c2010-10-30 20:15:02 +00001893static std::string GetAliasRequiredFeatures(Record *R,
1894 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001895 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001896 std::string Result;
1897 unsigned NumFeatures = 0;
1898 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001899 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001900
Chris Lattner4a74ee72010-11-01 02:09:21 +00001901 if (F == 0)
1902 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1903 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001904
Chris Lattner4a74ee72010-11-01 02:09:21 +00001905 if (NumFeatures)
1906 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001907
Chris Lattner4a74ee72010-11-01 02:09:21 +00001908 Result += F->getEnumName();
1909 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001910 }
Bob Wilson828295b2011-01-26 21:26:19 +00001911
Chris Lattner693173f2010-10-30 19:23:13 +00001912 if (NumFeatures > 1)
1913 Result = '(' + Result + ')';
1914 return Result;
1915}
1916
Chris Lattner674c1dc2010-10-30 17:36:36 +00001917/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001918/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001919static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001920 // Ignore aliases when match-prefix is set.
1921 if (!MatchPrefix.empty())
1922 return false;
1923
Chris Lattner674c1dc2010-10-30 17:36:36 +00001924 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001925 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001926 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001927
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001928 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001929 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001930
Chris Lattner4fd32c62010-10-30 18:56:12 +00001931 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1932 // iteration order of the map is stable.
1933 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001934
Chris Lattner674c1dc2010-10-30 17:36:36 +00001935 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1936 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001937 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001938 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001939
1940 // Process each alias a "from" mnemonic at a time, building the code executed
1941 // by the string remapper.
1942 std::vector<StringMatcher::StringPair> Cases;
1943 for (std::map<std::string, std::vector<Record*> >::iterator
1944 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1945 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001946 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001947
1948 // Loop through each alias and emit code that handles each case. If there
1949 // are two instructions without predicates, emit an error. If there is one,
1950 // emit it last.
1951 std::string MatchCode;
1952 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001953
Chris Lattner693173f2010-10-30 19:23:13 +00001954 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1955 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001956 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001957
Chris Lattner693173f2010-10-30 19:23:13 +00001958 // If this unconditionally matches, remember it for later and diagnose
1959 // duplicates.
1960 if (FeatureMask.empty()) {
1961 if (AliasWithNoPredicate != -1) {
1962 // We can't have two aliases from the same mnemonic with no predicate.
1963 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1964 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001965 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001966 }
Bob Wilson828295b2011-01-26 21:26:19 +00001967
Chris Lattner693173f2010-10-30 19:23:13 +00001968 AliasWithNoPredicate = i;
1969 continue;
1970 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001971 if (R->getValueAsString("ToMnemonic") == I->first)
1972 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001973
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001974 if (!MatchCode.empty())
1975 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001976 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1977 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001978 }
Bob Wilson828295b2011-01-26 21:26:19 +00001979
Chris Lattner693173f2010-10-30 19:23:13 +00001980 if (AliasWithNoPredicate != -1) {
1981 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001982 if (!MatchCode.empty())
1983 MatchCode += "else\n ";
1984 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001985 }
Bob Wilson828295b2011-01-26 21:26:19 +00001986
Chris Lattner693173f2010-10-30 19:23:13 +00001987 MatchCode += "return;";
1988
1989 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001990 }
Bob Wilson828295b2011-01-26 21:26:19 +00001991
Chris Lattner674c1dc2010-10-30 17:36:36 +00001992 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00001993 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001994
Chris Lattner7fd44892010-10-30 18:48:18 +00001995 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001996}
1997
Benjamin Krameraf482cf2011-10-17 16:18:09 +00001998static const char *getMinimalTypeForRange(uint64_t Range) {
1999 assert(Range < 0xFFFFFFFFULL && "Enum too large");
2000 if (Range > 0xFFFF)
2001 return "uint32_t";
2002 if (Range > 0xFF)
2003 return "uint16_t";
2004 return "uint8_t";
2005}
2006
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002007static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2008 const AsmMatcherInfo &Info, StringRef ClassName) {
2009 // Emit the static custom operand parsing table;
2010 OS << "namespace {\n";
2011 OS << " struct OperandMatchEntry {\n";
2012 OS << " const char *Mnemonic;\n";
2013 OS << " unsigned OperandMask;\n";
2014 OS << " MatchClassKind Class;\n";
2015 OS << " unsigned RequiredFeatures;\n";
2016 OS << " };\n\n";
2017
2018 OS << " // Predicate for searching for an opcode.\n";
2019 OS << " struct LessOpcodeOperand {\n";
2020 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2021 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2022 OS << " }\n";
2023 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2024 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2025 OS << " }\n";
2026 OS << " bool operator()(const OperandMatchEntry &LHS,";
2027 OS << " const OperandMatchEntry &RHS) {\n";
2028 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2029 OS << " }\n";
2030 OS << " };\n";
2031
2032 OS << "} // end anonymous namespace.\n\n";
2033
2034 OS << "static const OperandMatchEntry OperandMatchTable["
2035 << Info.OperandMatchInfo.size() << "] = {\n";
2036
2037 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
2038 for (std::vector<OperandMatchEntry>::const_iterator it =
2039 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2040 it != ie; ++it) {
2041 const OperandMatchEntry &OMI = *it;
2042 const MatchableInfo &II = *OMI.MI;
2043
2044 OS << " { \"" << II.Mnemonic << "\""
2045 << ", " << OMI.OperandMask;
2046
2047 OS << " /* ";
2048 bool printComma = false;
2049 for (int i = 0, e = 31; i !=e; ++i)
2050 if (OMI.OperandMask & (1 << i)) {
2051 if (printComma)
2052 OS << ", ";
2053 OS << i;
2054 printComma = true;
2055 }
2056 OS << " */";
2057
2058 OS << ", " << OMI.CI->Name
2059 << ", ";
2060
2061 // Write the required features mask.
2062 if (!II.RequiredFeatures.empty()) {
2063 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2064 if (i) OS << "|";
2065 OS << II.RequiredFeatures[i]->getEnumName();
2066 }
2067 } else
2068 OS << "0";
2069 OS << " },\n";
2070 }
2071 OS << "};\n\n";
2072
2073 // Emit the operand class switch to call the correct custom parser for
2074 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002075 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2076 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002077 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002078 << " &Operands,\n unsigned MCK) {\n\n"
2079 << " switch(MCK) {\n";
2080
2081 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2082 ie = Info.Classes.end(); it != ie; ++it) {
2083 ClassInfo *CI = *it;
2084 if (CI->ParserMethod.empty())
2085 continue;
2086 OS << " case " << CI->Name << ":\n"
2087 << " return " << CI->ParserMethod << "(Operands);\n";
2088 }
2089
2090 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002091 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002092 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002093 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002094 OS << "}\n\n";
2095
2096 // Emit the static custom operand parser. This code is very similar with
2097 // the other matcher. Also use MatchResultTy here just in case we go for
2098 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002099 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002100 << Target.getName() << ClassName << "::\n"
2101 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2102 << " &Operands,\n StringRef Mnemonic) {\n";
2103
2104 // Emit code to get the available features.
2105 OS << " // Get the current feature set.\n";
2106 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2107
2108 OS << " // Get the next operand index.\n";
2109 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2110
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002111 // Emit code to search the table.
2112 OS << " // Search the table.\n";
2113 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2114 OS << " MnemonicRange =\n";
2115 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2116 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2117 << " LessOpcodeOperand());\n\n";
2118
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002119 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002120 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002121
2122 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2123 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2124
2125 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2126 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2127
2128 // Emit check that the required features are available.
2129 OS << " // check if the available features match\n";
2130 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2131 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002132 OS << " continue;\n";
2133 OS << " }\n\n";
2134
2135 // Emit check to ensure the operand number matches.
2136 OS << " // check if the operand in question has a custom parser.\n";
2137 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2138 OS << " continue;\n\n";
2139
2140 // Emit call to the custom parser method
2141 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002142 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002143 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002144 OS << " if (Result != MatchOperand_NoMatch)\n";
2145 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002146 OS << " }\n\n";
2147
Jim Grosbachf922c472011-02-12 01:34:40 +00002148 OS << " // Okay, we had no match.\n";
2149 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002150 OS << "}\n\n";
2151}
2152
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002153void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002154 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002155 Record *AsmParser = Target.getAsmParser();
2156 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2157
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002158 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002159 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002160 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002161
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002162 // Sort the instruction table using the partial order on classes. We use
2163 // stable_sort to ensure that ambiguous instructions are still
2164 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002165 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2166 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002167
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002168 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002169 for (std::vector<MatchableInfo*>::iterator
2170 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002171 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002172 (*it)->dump();
2173 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002174
Chris Lattner22bc5c42010-11-01 05:06:45 +00002175 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002176 DEBUG_WITH_TYPE("ambiguous_instrs", {
2177 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002178 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002179 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002180 MatchableInfo &A = *Info.Matchables[i];
2181 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002182
Bob Wilson1f64ac42011-01-26 21:26:21 +00002183 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002184 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002185 A.dump();
2186 errs() << "\nis incomparable with:\n";
2187 B.dump();
2188 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002189 ++NumAmbiguous;
2190 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002191 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002192 }
Chris Lattner87410362010-09-06 20:21:47 +00002193 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002194 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002195 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002196 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002197
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002198 // Compute the information on the custom operand parsing.
2199 Info.BuildOperandMatchInfo();
2200
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002201 // Write the output.
2202
2203 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2204
Chris Lattner0692ee62010-09-06 19:11:01 +00002205 // Information for the class declaration.
2206 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2207 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002208 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002209 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002210 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002211 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2212 << "unsigned Opcode,\n"
2213 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2214 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002215 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002216 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002217 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002218 OS << " MCInst &Inst, unsigned &ErrorInfo);\n";
2219
2220 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002221 OS << "\n enum OperandMatchResultTy {\n";
2222 OS << " MatchOperand_Success, // operand matched successfully\n";
2223 OS << " MatchOperand_NoMatch, // operand did not match\n";
2224 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2225 OS << " };\n";
2226 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002227 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2228 OS << " StringRef Mnemonic);\n";
2229
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002230 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002231 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2232 OS << " unsigned MCK);\n\n";
2233 }
2234
Chris Lattner0692ee62010-09-06 19:11:01 +00002235 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2236
Chris Lattner0692ee62010-09-06 19:11:01 +00002237 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2238 OS << "#undef GET_REGISTER_MATCHER\n\n";
2239
Daniel Dunbar54074b52010-07-19 05:44:09 +00002240 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002241 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002242
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002243 // Emit the function to match a register name to number.
2244 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002245
2246 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002247
Chris Lattner0692ee62010-09-06 19:11:01 +00002248
2249 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2250 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002251
Chris Lattner7fd44892010-10-30 18:48:18 +00002252 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002253 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002254
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002255 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002256 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002257
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002258 // Emit the enumeration for classes which participate in matching.
2259 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002260
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002261 // Emit the routine to match token strings to their match class.
2262 EmitMatchTokenString(Target, Info.Classes, OS);
2263
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002264 // Emit the subclass predicate routine.
2265 EmitIsSubclass(Target, Info.Classes, OS);
2266
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002267 // Emit the routine to validate an operand against a match class.
2268 EmitValidateOperandClass(Info, OS);
2269
Daniel Dunbar54074b52010-07-19 05:44:09 +00002270 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002271 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002272
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002273
2274 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002275 for (std::vector<MatchableInfo*>::const_iterator it =
2276 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002277 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002278 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002279
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002280 // Emit the static match table; unused classes get initalized to 0 which is
2281 // guaranteed to be InvalidMatchClass.
2282 //
2283 // FIXME: We can reduce the size of this table very easily. First, we change
2284 // it so that store the kinds in separate bit-fields for each index, which
2285 // only needs to be the max width used for classes at that index (we also need
2286 // to reject based on this during classification). If we then make sure to
2287 // order the match kinds appropriately (putting mnemonics last), then we
2288 // should only end up using a few bits for each class, especially the ones
2289 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002290 OS << "namespace {\n";
2291 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002292 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002293 OS << " const char *Mnemonic;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002294 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2295 << " ConvertFn;\n";
2296 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2297 << " Classes[" << MaxNumOperands << "];\n";
2298 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2299 << " RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002300 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002301
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002302 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002303 OS << " struct LessOpcode {\n";
2304 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2305 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2306 OS << " }\n";
2307 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2308 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2309 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002310 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2311 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2312 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002313 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002314
Chris Lattner96352e52010-09-06 21:08:38 +00002315 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002316
Chris Lattner96352e52010-09-06 21:08:38 +00002317 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002318 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002319
Chris Lattner22bc5c42010-11-01 05:06:45 +00002320 for (std::vector<MatchableInfo*>::const_iterator it =
2321 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002322 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002323 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002324
Chris Lattner662e5a32010-11-06 07:14:44 +00002325 OS << " { " << Target.getName() << "::"
2326 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2327 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002328 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002329 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002330
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002331 if (i) OS << ", ";
2332 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002333 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002334 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002335
Daniel Dunbar54074b52010-07-19 05:44:09 +00002336 // Write the required features mask.
2337 if (!II.RequiredFeatures.empty()) {
2338 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2339 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002340 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002341 }
2342 } else
2343 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002344
Daniel Dunbar54074b52010-07-19 05:44:09 +00002345 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002346 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002347
Chris Lattner96352e52010-09-06 21:08:38 +00002348 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002349
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002350 // A method to determine if a mnemonic is in the list.
2351 OS << "bool " << Target.getName() << ClassName << "::\n"
2352 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2353 OS << " // Search the table.\n";
2354 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2355 OS << " std::equal_range(MatchTable, MatchTable+"
2356 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2357 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2358 OS << "}\n\n";
2359
Chris Lattner96352e52010-09-06 21:08:38 +00002360 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002361 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002362 << Target.getName() << ClassName << "::\n"
2363 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2364 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002365 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002366
2367 // Emit code to get the available features.
2368 OS << " // Get the current feature set.\n";
2369 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2370
Chris Lattner674c1dc2010-10-30 17:36:36 +00002371 OS << " // Get the instruction mnemonic, which is the first token.\n";
2372 OS << " StringRef Mnemonic = ((" << Target.getName()
2373 << "Operand*)Operands[0])->getToken();\n\n";
2374
Chris Lattner7fd44892010-10-30 18:48:18 +00002375 if (HasMnemonicAliases) {
2376 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002377 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002378 }
Bob Wilson828295b2011-01-26 21:26:19 +00002379
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002380 // Emit code to compute the class list for this operand vector.
2381 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002382 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2383 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2384 OS << " return Match_InvalidOperand;\n";
2385 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002386
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002387 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002388 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002389 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002390 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002391 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002392 OS << " // wrong for all instances of the instruction.\n";
2393 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002394
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002395 // Emit code to search the table.
2396 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002397 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2398 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002399 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002400
Chris Lattnera008e8a2010-09-06 21:54:15 +00002401 OS << " // Return a more specific error code if no mnemonics match.\n";
2402 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2403 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002404
Chris Lattner2b1f9432010-09-06 21:22:45 +00002405 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002406 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002407 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002408
Gabor Greife53ee3b2010-09-07 06:06:06 +00002409 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002410 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002411
Daniel Dunbar54074b52010-07-19 05:44:09 +00002412 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002413 OS << " bool OperandsValid = true;\n";
2414 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002415 OS << " if (i + 1 >= Operands.size()) {\n";
2416 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002417 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002418 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002419 OS << " if (validateOperandClass(Operands[i+1], "
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002420 "(MatchClassKind)it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002421 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002422 OS << " // If this operand is broken for all of the instances of this\n";
2423 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002424 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002425 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002426 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2427 OS << " OperandsValid = false;\n";
2428 OS << " break;\n";
2429 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002430
Chris Lattnerce4a3352010-09-06 22:11:18 +00002431 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002432
2433 // Emit check that the required features are available.
2434 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2435 << "!= it->RequiredFeatures) {\n";
2436 OS << " HadMatchOtherThanFeatures = true;\n";
2437 OS << " continue;\n";
2438 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002439 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002440 OS << " // We have selected a definite instruction, convert the parsed\n"
2441 << " // operands into the appropriate MCInst.\n";
2442 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2443 << " it->Opcode, Operands))\n";
2444 OS << " return Match_ConversionFail;\n";
2445 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002446
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002447 // Verify the instruction with the target-specific match predicate function.
2448 OS << " // We have a potential match. Check the target predicate to\n"
2449 << " // handle any context sensitive constraints.\n"
2450 << " unsigned MatchResult;\n"
2451 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2452 << " Match_Success) {\n"
2453 << " Inst.clear();\n"
2454 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002455 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002456 << " continue;\n"
2457 << " }\n\n";
2458
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002459 // Call the post-processing function, if used.
2460 std::string InsnCleanupFn =
2461 AsmParser->getValueAsString("AsmParserInstCleanup");
2462 if (!InsnCleanupFn.empty())
2463 OS << " " << InsnCleanupFn << "(Inst);\n";
2464
Chris Lattner79ed3f72010-09-06 19:22:17 +00002465 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002466 OS << " }\n\n";
2467
Chris Lattnerec6789f2010-09-06 20:08:02 +00002468 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002469 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
2470 OS << " return RetCode;\n";
2471 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002472 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002473
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002474 if (Info.OperandMatchInfo.size())
2475 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2476
Chris Lattner0692ee62010-09-06 19:11:01 +00002477 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002478}