blob: bdd1c4970ceba45e14765729548f8b397133e71e [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
Devang Patel63faf822012-01-07 01:33:34 +0000285 /// Register record if this token is singleton register.
286 Record *SingletonReg;
287
288 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1),
289 SingletonReg(0) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000290 };
Bob Wilson828295b2011-01-26 21:26:19 +0000291
Chris Lattner1d13bda2010-11-04 00:43:46 +0000292 /// ResOperand - This represents a single operand in the result instruction
293 /// generated by the match. In cases (like addressing modes) where a single
294 /// assembler operand expands to multiple MCOperands, this represents the
295 /// single assembler operand, not the MCOperand.
296 struct ResOperand {
297 enum {
298 /// RenderAsmOperand - This represents an operand result that is
299 /// generated by calling the render method on the assembly operand. The
300 /// corresponding AsmOperand is specified by AsmOperandNum.
301 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000302
Chris Lattner1d13bda2010-11-04 00:43:46 +0000303 /// TiedOperand - This represents a result operand that is a duplicate of
304 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000305 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000306
Chris Lattner98c870f2010-11-06 19:25:43 +0000307 /// ImmOperand - This represents an immediate value that is dumped into
308 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000309 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000310
Chris Lattner90fd7972010-11-06 19:57:21 +0000311 /// RegOperand - This represents a fixed register that is dumped in.
312 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000313 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000314
Chris Lattner1d13bda2010-11-04 00:43:46 +0000315 union {
316 /// This is the operand # in the AsmOperands list that this should be
317 /// copied from.
318 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000319
Chris Lattner1d13bda2010-11-04 00:43:46 +0000320 /// TiedOperandNum - This is the (earlier) result operand that should be
321 /// copied from.
322 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000323
Chris Lattner98c870f2010-11-06 19:25:43 +0000324 /// ImmVal - This is the immediate value added to the instruction.
325 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000326
Chris Lattner90fd7972010-11-06 19:57:21 +0000327 /// Register - This is the register record.
328 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000329 };
Bob Wilson828295b2011-01-26 21:26:19 +0000330
Bob Wilsona49c7df2011-01-26 19:44:55 +0000331 /// MINumOperands - The number of MCInst operands populated by this
332 /// operand.
333 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000334
Bob Wilsona49c7df2011-01-26 19:44:55 +0000335 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000336 ResOperand X;
337 X.Kind = RenderAsmOperand;
338 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000339 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000340 return X;
341 }
Bob Wilson828295b2011-01-26 21:26:19 +0000342
Bob Wilsona49c7df2011-01-26 19:44:55 +0000343 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000344 ResOperand X;
345 X.Kind = TiedOperand;
346 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000347 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000348 return X;
349 }
Bob Wilson828295b2011-01-26 21:26:19 +0000350
Bob Wilsona49c7df2011-01-26 19:44:55 +0000351 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000352 ResOperand X;
353 X.Kind = ImmOperand;
354 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000355 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000356 return X;
357 }
Bob Wilson828295b2011-01-26 21:26:19 +0000358
Bob Wilsona49c7df2011-01-26 19:44:55 +0000359 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000360 ResOperand X;
361 X.Kind = RegOperand;
362 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000363 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000364 return X;
365 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000366 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000367
Chris Lattner3b5aec62010-11-02 17:34:28 +0000368 /// TheDef - This is the definition of the instruction or InstAlias that this
369 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000370 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000371
Chris Lattnerc07bd402010-11-04 02:11:18 +0000372 /// DefRec - This is the definition that it came from.
373 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000374
Chris Lattner662e5a32010-11-06 07:14:44 +0000375 const CodeGenInstruction *getResultInst() const {
376 if (DefRec.is<const CodeGenInstruction*>())
377 return DefRec.get<const CodeGenInstruction*>();
378 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
379 }
Bob Wilson828295b2011-01-26 21:26:19 +0000380
Chris Lattner1d13bda2010-11-04 00:43:46 +0000381 /// ResOperands - This is the operand list that should be built for the result
382 /// MCInst.
383 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384
385 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000386 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000387 std::string AsmString;
388
Chris Lattnerd19ec052010-11-02 17:30:52 +0000389 /// Mnemonic - This is the first token of the matched instruction, its
390 /// mnemonic.
391 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000392
Chris Lattner3116fef2010-11-02 01:03:43 +0000393 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000394 /// annotated with a class and where in the OperandList they were defined.
395 /// This directly corresponds to the tokenized AsmString after the mnemonic is
396 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000397 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398
Daniel Dunbar54074b52010-07-19 05:44:09 +0000399 /// Predicates - The required subtarget features to match this instruction.
400 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
401
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000402 /// ConversionFnKind - The enum value which is passed to the generated
403 /// ConvertToMCInst to convert parsed operands into an MCInst for this
404 /// function.
405 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000406
Chris Lattner22bc5c42010-11-01 05:06:45 +0000407 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattner662e5a32010-11-06 07:14:44 +0000408 : TheDef(CGI.TheDef), DefRec(&CGI), AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000409 }
410
Chris Lattner22bc5c42010-11-01 05:06:45 +0000411 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattner662e5a32010-11-06 07:14:44 +0000412 : TheDef(Alias->TheDef), DefRec(Alias), AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000413 }
Bob Wilson828295b2011-01-26 21:26:19 +0000414
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000415 void Initialize(const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000416 SmallPtrSet<Record*, 16> &SingletonRegisters,
417 int AsmVariantNo, std::string &RegisterPrefix);
Bob Wilson828295b2011-01-26 21:26:19 +0000418
Chris Lattner22bc5c42010-11-01 05:06:45 +0000419 /// Validate - Return true if this matchable is a valid thing to match against
420 /// and perform a bunch of validity checking.
421 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000422
Devang Patel63faf822012-01-07 01:33:34 +0000423 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
424 /// if present, from specified token.
425 void
426 extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info,
427 std::string &RegisterPrefix);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000428
Bob Wilsona49c7df2011-01-26 19:44:55 +0000429 /// FindAsmOperand - Find the AsmOperand with the specified name and
430 /// suboperand index.
431 int FindAsmOperand(StringRef N, int SubOpIdx) const {
432 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
433 if (N == AsmOperands[i].SrcOpName &&
434 SubOpIdx == AsmOperands[i].SubOpIdx)
435 return i;
436 return -1;
437 }
Bob Wilson828295b2011-01-26 21:26:19 +0000438
Bob Wilsona49c7df2011-01-26 19:44:55 +0000439 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
440 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000441 int FindAsmOperandNamed(StringRef N) const {
442 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
443 if (N == AsmOperands[i].SrcOpName)
444 return i;
445 return -1;
446 }
Bob Wilson828295b2011-01-26 21:26:19 +0000447
Chris Lattner41409852010-11-06 07:31:43 +0000448 void BuildInstructionResultOperands();
449 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000450
Chris Lattner22bc5c42010-11-01 05:06:45 +0000451 /// operator< - Compare two matchables.
452 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000453 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000454 if (Mnemonic != RHS.Mnemonic)
455 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000456
Chris Lattner3116fef2010-11-02 01:03:43 +0000457 if (AsmOperands.size() != RHS.AsmOperands.size())
458 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000459
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000460 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000461 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000462 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
463 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000464 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000465 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000466 return false;
467 }
468
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000469 return false;
470 }
471
Bob Wilson1f64ac42011-01-26 21:26:21 +0000472 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000473 /// ambiguously match the same set of operands as \arg RHS (without being a
474 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000475 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000476 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000477 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000478 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000479
Daniel Dunbar2b544812009-08-09 06:05:33 +0000480 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000481 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000482 return false;
483
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000484 // Otherwise, make sure the ordering of the two instructions is unambiguous
485 // by checking that either (a) a token or operand kind discriminates them,
486 // or (b) the ordering among equivalent kinds is consistent.
487
Daniel Dunbar2b544812009-08-09 06:05:33 +0000488 // Tokens and operand kinds are unambiguous (assuming a correct target
489 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000490 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
491 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
492 AsmOperands[i].Class->Kind == ClassInfo::Token)
493 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
494 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000495 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000496
Daniel Dunbar2b544812009-08-09 06:05:33 +0000497 // Otherwise, this operand could commute if all operands are equivalent, or
498 // there is a pair of operands that compare less than and a pair that
499 // compare greater than.
500 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000501 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
502 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000503 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000504 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000505 HasGT = true;
506 }
507
508 return !(HasLT ^ HasGT);
509 }
510
Daniel Dunbar20927f22009-08-07 08:26:05 +0000511 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000512
Chris Lattnerd19ec052010-11-02 17:30:52 +0000513private:
514 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000515};
516
Daniel Dunbar54074b52010-07-19 05:44:09 +0000517/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
518/// feature which participates in instruction matching.
519struct SubtargetFeatureInfo {
520 /// \brief The predicate record for this feature.
521 Record *TheDef;
522
523 /// \brief An unique index assigned to represent this feature.
524 unsigned Index;
525
Chris Lattner0aed1e72010-10-30 20:07:57 +0000526 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000527
Daniel Dunbar54074b52010-07-19 05:44:09 +0000528 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000529 std::string getEnumName() const {
530 return "Feature_" + TheDef->getName();
531 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000532};
533
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000534struct OperandMatchEntry {
535 unsigned OperandMask;
536 MatchableInfo* MI;
537 ClassInfo *CI;
538
539 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
540 unsigned opMask) {
541 OperandMatchEntry X;
542 X.OperandMask = opMask;
543 X.CI = ci;
544 X.MI = mi;
545 return X;
546 }
547};
548
549
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000550class AsmMatcherInfo {
551public:
Chris Lattner67db8832010-12-13 00:23:57 +0000552 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000553 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000554
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000555 /// The tablegen AsmParser record.
556 Record *AsmParser;
557
Chris Lattner02bcbc92010-11-01 01:37:30 +0000558 /// Target - The target information.
559 CodeGenTarget &Target;
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,
Devang Patel63faf822012-01-07 01:33:34 +0000647 SmallPtrSet<Record*, 16> &SingletonRegisters,
648 int AsmVariantNo, std::string &RegisterPrefix) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000649 // TODO: Eventually support asmparser for Variant != 0.
Devang Patel59f7ee02012-01-05 00:51:28 +0000650 AsmString =
Devang Patel63faf822012-01-07 01:33:34 +0000651 CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
Bob Wilson828295b2011-01-26 21:26:19 +0000652
Chris Lattnerd19ec052010-11-02 17:30:52 +0000653 TokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000654
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000655 // Compute the require features.
656 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
657 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
658 if (SubtargetFeatureInfo *Feature =
659 Info.getSubtargetFeature(Predicates[i]))
660 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000661
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000662 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000663 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Devang Patel63faf822012-01-07 01:33:34 +0000664 extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix);
665 if (Record *Reg = AsmOperands[i].SingletonReg)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000666 SingletonRegisters.insert(Reg);
667 }
668}
669
Chris Lattnerd19ec052010-11-02 17:30:52 +0000670/// TokenizeAsmString - Tokenize a simplified assembly string.
671void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
672 StringRef String = AsmString;
673 unsigned Prev = 0;
674 bool InTok = true;
675 for (unsigned i = 0, e = String.size(); i != e; ++i) {
676 switch (String[i]) {
677 case '[':
678 case ']':
679 case '*':
680 case '!':
681 case ' ':
682 case '\t':
683 case ',':
684 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000685 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000686 InTok = false;
687 }
688 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000689 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000690 Prev = i + 1;
691 break;
692
693 case '\\':
694 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000695 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000696 InTok = false;
697 }
698 ++i;
699 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000700 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000701 Prev = i + 1;
702 break;
703
704 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000705 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000706 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000707 InTok = false;
708 }
Bob Wilson828295b2011-01-26 21:26:19 +0000709
Chris Lattner7ad31472010-11-06 22:06:03 +0000710 // If this isn't "${", treat like a normal token.
711 if (i + 1 == String.size() || String[i + 1] != '{') {
712 Prev = i;
713 break;
714 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000715
716 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
717 assert(End != String.end() && "Missing brace in operand reference!");
718 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000719 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000720 Prev = EndPos + 1;
721 i = EndPos;
722 break;
723 }
724
725 case '.':
726 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000727 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000728 Prev = i;
729 InTok = true;
730 break;
731
732 default:
733 InTok = true;
734 }
735 }
736 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000737 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000738
Chris Lattnerd19ec052010-11-02 17:30:52 +0000739 // The first token of the instruction is the mnemonic, which must be a
740 // simple string, not a $foo variable or a singleton register.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000741 if (AsmOperands.empty())
742 throw TGError(TheDef->getLoc(),
743 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000744 Mnemonic = AsmOperands[0].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000745 // FIXME : Check and raise an error if it is a register.
Devang Patelb78307f2012-01-07 01:22:23 +0000746 if (Mnemonic[0] == '$')
Chris Lattnerd19ec052010-11-02 17:30:52 +0000747 throw TGError(TheDef->getLoc(),
748 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000749
Chris Lattnerd19ec052010-11-02 17:30:52 +0000750 // Remove the first operand, it is tracked in the mnemonic field.
751 AsmOperands.erase(AsmOperands.begin());
752}
753
Chris Lattner22bc5c42010-11-01 05:06:45 +0000754bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
755 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000756 if (AsmString.empty())
757 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000758
Chris Lattner22bc5c42010-11-01 05:06:45 +0000759 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000760 // isCodeGenOnly if they are pseudo instructions.
761 if (AsmString.find('\n') != std::string::npos)
762 throw TGError(TheDef->getLoc(),
763 "multiline instruction is not valid for the asmparser, "
764 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000765
Chris Lattner4164f6b2010-11-01 04:44:29 +0000766 // Remove comments from the asm string. We know that the asmstring only
767 // has one line.
768 if (!CommentDelimiter.empty() &&
769 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
770 throw TGError(TheDef->getLoc(),
771 "asmstring for instruction has comment character in it, "
772 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000773
Chris Lattner22bc5c42010-11-01 05:06:45 +0000774 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000775 // handle, the target should be refactored to use operands instead of
776 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000777 //
778 // Also, check for instructions which reference the operand multiple times;
779 // this implies a constraint we would not honor.
780 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000781 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
782 StringRef Tok = AsmOperands[i].Token;
783 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000784 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000785 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000786 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000787
Chris Lattner22bc5c42010-11-01 05:06:45 +0000788 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000789 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000790 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000791 if (!Hack)
792 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000793 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000794 "' can never be matched!");
795 // FIXME: Should reject these. The ARM backend hits this with $lane in a
796 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000797 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000798 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000799 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000800 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000801 });
802 return false;
803 }
804 }
Bob Wilson828295b2011-01-26 21:26:19 +0000805
Chris Lattner5bc93872010-11-01 04:34:44 +0000806 return true;
807}
808
Devang Pateld06b01c2012-01-09 21:30:46 +0000809/// extractSingletonRegisterForAsmOperand - Extract singleton register,
810/// if present, from specified token.
Devang Patel63faf822012-01-07 01:33:34 +0000811void MatchableInfo::
Devang Pateld06b01c2012-01-09 21:30:46 +0000812extractSingletonRegisterForAsmOperand(unsigned OperandNo,
813 const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000814 std::string &RegisterPrefix) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000815 StringRef Tok = AsmOperands[OperandNo].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000816 if (RegisterPrefix.empty()) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000817 std::string LoweredTok = Tok.lower();
818 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
819 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Devang Patel63faf822012-01-07 01:33:34 +0000820 return;
821 }
Bob Wilson828295b2011-01-26 21:26:19 +0000822
Devang Patel63faf822012-01-07 01:33:34 +0000823 if (!Tok.startswith(RegisterPrefix))
824 return;
825
826 StringRef RegName = Tok.substr(RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000827 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
Devang Pateld06b01c2012-01-09 21:30:46 +0000828 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000829
Chris Lattner1de88232010-11-01 01:47:07 +0000830 // If there is no register prefix (i.e. "%" in "%eax"), then this may
831 // be some random non-register token, just ignore it.
Devang Patel63faf822012-01-07 01:33:34 +0000832 return;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000833}
834
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000835static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000836 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000837
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000838 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
839 switch (*it) {
840 case '*': Res += "_STAR_"; break;
841 case '%': Res += "_PCT_"; break;
842 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000843 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000844 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000845 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000846 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000847 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000848 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000849 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000850 }
851 }
852
853 return Res;
854}
855
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000856ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000857 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000858
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000859 if (!Entry) {
860 Entry = new ClassInfo();
861 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000862 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000863 Entry->Name = "MCK_" + getEnumNameForToken(Token);
864 Entry->ValueName = Token;
865 Entry->PredicateMethod = "<invalid>";
866 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000867 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000868 Classes.push_back(Entry);
869 }
870
871 return Entry;
872}
873
874ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000875AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
876 int SubOpIdx) {
877 Record *Rec = OI.Rec;
878 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000879 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +0000880 return getOperandClass(Rec, SubOpIdx);
881}
Bob Wilsona49c7df2011-01-26 19:44:55 +0000882
Jim Grosbach48c1f842011-10-28 22:32:53 +0000883ClassInfo *
884AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000885 if (Rec->isSubClassOf("RegisterOperand")) {
886 // RegisterOperand may have an associated ParserMatchClass. If it does,
887 // use it, else just fall back to the underlying register class.
888 const RecordVal *R = Rec->getValue("ParserMatchClass");
889 if (R == 0 || R->getValue() == 0)
890 throw "Record `" + Rec->getName() +
891 "' does not have a ParserMatchClass!\n";
892
David Greene05bce0b2011-07-29 22:43:06 +0000893 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000894 Record *MatchClass = DI->getDef();
895 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
896 return CI;
897 }
898
899 // No custom match class. Just use the register class.
900 Record *ClassRec = Rec->getValueAsDef("RegClass");
901 if (!ClassRec)
902 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
903 "' has no associated register class!\n");
904 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
905 return CI;
906 throw TGError(Rec->getLoc(), "register class has no class info!");
907 }
908
909
Bob Wilsona49c7df2011-01-26 19:44:55 +0000910 if (Rec->isSubClassOf("RegisterClass")) {
911 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000912 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000913 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000914 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000915
Bob Wilsona49c7df2011-01-26 19:44:55 +0000916 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
917 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000918 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
919 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000920
Bob Wilsona49c7df2011-01-26 19:44:55 +0000921 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000922}
923
Chris Lattner1de88232010-11-01 01:47:07 +0000924void AsmMatcherInfo::
925BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000926 const std::vector<CodeGenRegister*> &Registers =
927 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000928 ArrayRef<CodeGenRegisterClass*> RegClassList =
929 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000930
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000931 // The register sets used for matching.
932 std::set< std::set<Record*> > RegisterSets;
933
Jim Grosbacha7c78222010-10-29 22:13:48 +0000934 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000935 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +0000936 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000937 RegisterSets.insert(std::set<Record*>(
938 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000939
940 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000941 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
942 ie = SingletonRegisters.end(); it != ie; ++it) {
943 Record *Rec = *it;
944 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
945 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000946
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000947 // Introduce derived sets where necessary (when a register does not determine
948 // a unique register set class), and build the mapping of registers to the set
949 // they should classify to.
950 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000951 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000952 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000953 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000954 // Compute the intersection of all sets containing this register.
955 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000956
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000957 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
958 ie = RegisterSets.end(); it != ie; ++it) {
959 if (!it->count(CGR.TheDef))
960 continue;
961
962 if (ContainingSet.empty()) {
963 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000964 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000965 }
Bob Wilson828295b2011-01-26 21:26:19 +0000966
Chris Lattnerec6f0962010-11-02 18:10:06 +0000967 std::set<Record*> Tmp;
968 std::swap(Tmp, ContainingSet);
969 std::insert_iterator< std::set<Record*> > II(ContainingSet,
970 ContainingSet.begin());
971 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000972 }
973
974 if (!ContainingSet.empty()) {
975 RegisterSets.insert(ContainingSet);
976 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
977 }
978 }
979
980 // Construct the register classes.
981 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
982 unsigned Index = 0;
983 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
984 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
985 ClassInfo *CI = new ClassInfo();
986 CI->Kind = ClassInfo::RegisterClass0 + Index;
987 CI->ClassName = "Reg" + utostr(Index);
988 CI->Name = "MCK_Reg" + utostr(Index);
989 CI->ValueName = "";
990 CI->PredicateMethod = ""; // unused
991 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000992 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000993 Classes.push_back(CI);
994 RegisterSetClasses.insert(std::make_pair(*it, CI));
995 }
996
997 // Find the superclasses; we could compute only the subgroup lattice edges,
998 // but there isn't really a point.
999 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
1000 ie = RegisterSets.end(); it != ie; ++it) {
1001 ClassInfo *CI = RegisterSetClasses[*it];
1002 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
1003 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001004 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001005 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
1006 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
1007 }
1008
1009 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001010 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001011 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001012 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001013 // Def will be NULL for non-user defined register classes.
1014 Record *Def = RC.getDef();
1015 if (!Def)
1016 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001017 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1018 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001019 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001020 CI->ClassName = RC.getName();
1021 CI->Name = "MCK_" + RC.getName();
1022 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001023 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001024 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001025
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001026 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001027 }
1028
1029 // Populate the map for individual registers.
1030 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1031 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001032 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001033
1034 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001035 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1036 ie = SingletonRegisters.end(); it != ie; ++it) {
1037 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001038 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001039 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001040
Chris Lattner1de88232010-11-01 01:47:07 +00001041 if (CI->ValueName.empty()) {
1042 CI->ClassName = Rec->getName();
1043 CI->Name = "MCK_" + Rec->getName();
1044 CI->ValueName = Rec->getName();
1045 } else
1046 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001047 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001048}
1049
Chris Lattner02bcbc92010-11-01 01:37:30 +00001050void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001051 std::vector<Record*> AsmOperands =
1052 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001053
1054 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001055 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001056 ie = AsmOperands.end(); it != ie; ++it)
1057 AsmOperandClasses[*it] = new ClassInfo();
1058
Daniel Dunbar338825c2009-08-10 18:41:10 +00001059 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001060 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001061 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001062 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001063 CI->Kind = ClassInfo::UserClass0 + Index;
1064
David Greene05bce0b2011-07-29 22:43:06 +00001065 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001066 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001067 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001068 if (!DI) {
1069 PrintError((*it)->getLoc(), "Invalid super class reference!");
1070 continue;
1071 }
1072
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001073 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1074 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001075 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001076 else
1077 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001078 }
1079 CI->ClassName = (*it)->getValueAsString("Name");
1080 CI->Name = "MCK_" + CI->ClassName;
1081 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001082
1083 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001084 Init *PMName = (*it)->getValueInit("PredicateMethod");
1085 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001086 CI->PredicateMethod = SI->getValue();
1087 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001088 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001089 "Unexpected PredicateMethod field!");
1090 CI->PredicateMethod = "is" + CI->ClassName;
1091 }
1092
1093 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001094 Init *RMName = (*it)->getValueInit("RenderMethod");
1095 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001096 CI->RenderMethod = SI->getValue();
1097 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001098 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001099 "Unexpected RenderMethod field!");
1100 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1101 }
1102
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001103 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001104 Init *PRMName = (*it)->getValueInit("ParserMethod");
1105 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001106 CI->ParserMethod = SI->getValue();
1107
Daniel Dunbar338825c2009-08-10 18:41:10 +00001108 AsmOperandClasses[*it] = CI;
1109 Classes.push_back(CI);
1110 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001111}
1112
Bob Wilson828295b2011-01-26 21:26:19 +00001113AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1114 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001115 RecordKeeper &records)
Devang Patel63faf822012-01-07 01:33:34 +00001116 : Records(records), AsmParser(asmParser), Target(target) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001117}
1118
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001119/// BuildOperandMatchInfo - Build the necessary information to handle user
1120/// defined operand parsing methods.
1121void AsmMatcherInfo::BuildOperandMatchInfo() {
1122
1123 /// Map containing a mask with all operands indicies that can be found for
1124 /// that class inside a instruction.
1125 std::map<ClassInfo*, unsigned> OpClassMask;
1126
1127 for (std::vector<MatchableInfo*>::const_iterator it =
1128 Matchables.begin(), ie = Matchables.end();
1129 it != ie; ++it) {
1130 MatchableInfo &II = **it;
1131 OpClassMask.clear();
1132
1133 // Keep track of all operands of this instructions which belong to the
1134 // same class.
1135 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1136 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1137 if (Op.Class->ParserMethod.empty())
1138 continue;
1139 unsigned &OperandMask = OpClassMask[Op.Class];
1140 OperandMask |= (1 << i);
1141 }
1142
1143 // Generate operand match info for each mnemonic/operand class pair.
1144 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1145 iie = OpClassMask.end(); iit != iie; ++iit) {
1146 unsigned OpMask = iit->second;
1147 ClassInfo *CI = iit->first;
1148 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1149 }
1150 }
1151}
1152
Chris Lattner02bcbc92010-11-01 01:37:30 +00001153void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001154 // Build information about all of the AssemblerPredicates.
1155 std::vector<Record*> AllPredicates =
1156 Records.getAllDerivedDefinitions("Predicate");
1157 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1158 Record *Pred = AllPredicates[i];
1159 // Ignore predicates that are not intended for the assembler.
1160 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1161 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001162
Chris Lattner4164f6b2010-11-01 04:44:29 +00001163 if (Pred->getName().empty())
1164 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001165
Chris Lattner0aed1e72010-10-30 20:07:57 +00001166 unsigned FeatureNo = SubtargetFeatures.size();
1167 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1168 assert(FeatureNo < 32 && "Too many subtarget features!");
1169 }
Jim Grosbacha7c78222010-10-29 22:13:48 +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;
Devang Patel0dbcada2012-01-09 19:13:28 +00001174 unsigned VariantCount = Target.getAsmParserVariantCount();
1175 for (unsigned VC = 0; VC != VariantCount; ++VC) {
1176 Record *AsmVariant = Target.getAsmParserVariant(VC);
1177 std::string CommentDelimiter = AsmVariant->getValueAsString("CommentDelimiter");
1178 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1179 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
1180
1181 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1182 E = Target.inst_end(); I != E; ++I) {
1183 const CodeGenInstruction &CGI = **I;
1184
1185 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1186 // filter the set of instructions we consider.
1187 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
1188 continue;
1189
1190 // Ignore "codegen only" instructions.
1191 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1192 continue;
1193
1194 // Validate the operand list to ensure we can handle this instruction.
1195 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1196 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
1197
1198 // Validate tied operands.
1199 if (OI.getTiedRegister() != -1) {
1200 // If we have a tied operand that consists of multiple MCOperands,
1201 // reject it. We reject aliases and ignore instructions for now.
1202 if (OI.MINumOperands != 1) {
1203 // FIXME: Should reject these. The ARM backend hits this with $lane
1204 // in a bunch of instructions. It is unclear what the right answer is.
1205 DEBUG({
1206 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1207 << "ignoring instruction with multi-operand tied operand '"
1208 << OI.Name << "'\n";
1209 });
1210 continue;
1211 }
1212 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001213 }
Devang Patel0dbcada2012-01-09 19:13:28 +00001214
1215 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
1216
1217 II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
1218
1219 // Ignore instructions which shouldn't be matched and diagnose invalid
1220 // instruction definitions with an error.
1221 if (!II->Validate(CommentDelimiter, true))
1222 continue;
1223
1224 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1225 //
1226 // FIXME: This is a total hack.
1227 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1228 StringRef(II->TheDef->getName()).endswith("_Int"))
1229 continue;
1230
1231 Matchables.push_back(II.take());
Chris Lattner1d13bda2010-11-04 00:43:46 +00001232 }
Devang Patel0dbcada2012-01-09 19:13:28 +00001233
1234 // Parse all of the InstAlias definitions and stick them in the list of
1235 // matchables.
1236 std::vector<Record*> AllInstAliases =
1237 Records.getAllDerivedDefinitions("InstAlias");
1238 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
1239 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
1240
1241 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1242 // filter the set of instruction aliases we consider, based on the target
1243 // instruction.
1244 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1245 MatchPrefix))
1246 continue;
1247
1248 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
1249
1250 II->Initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
1251
1252 // Validate the alias definitions.
1253 II->Validate(CommentDelimiter, false);
1254
1255 Matchables.push_back(II.take());
1256 }
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001257 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001258
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001259 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001260 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001261
1262 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001263 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001264
Chris Lattner0bb780c2010-11-04 00:57:06 +00001265 // Build the information about matchables, now that we have fully formed
1266 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001267 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1268 ie = Matchables.end(); it != ie; ++it) {
1269 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001270
Chris Lattnere206fcf2010-09-06 21:01:37 +00001271 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001272 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1273 // don't precompute the loop bound.
1274 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001275 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001276 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001277
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001278 // Check for singleton registers.
Devang Patel63faf822012-01-07 01:33:34 +00001279 if (Record *RegRecord = II->AsmOperands[i].SingletonReg) {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001280 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001281 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1282 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001283 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001284 }
1285
Daniel Dunbar20927f22009-08-07 08:26:05 +00001286 // Check for simple tokens.
1287 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001288 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001289 continue;
1290 }
1291
Chris Lattner7ad31472010-11-06 22:06:03 +00001292 if (Token.size() > 1 && isdigit(Token[1])) {
1293 Op.Class = getTokenClass(Token);
1294 continue;
1295 }
Bob Wilson828295b2011-01-26 21:26:19 +00001296
Chris Lattnerc07bd402010-11-04 02:11:18 +00001297 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001298 StringRef OperandName;
1299 if (Token[1] == '{')
1300 OperandName = Token.substr(2, Token.size() - 3);
1301 else
1302 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001303
Chris Lattnerc07bd402010-11-04 02:11:18 +00001304 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001305 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001306 else
Chris Lattner225549f2010-11-06 06:39:47 +00001307 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001308 }
Bob Wilson828295b2011-01-26 21:26:19 +00001309
Chris Lattner41409852010-11-06 07:31:43 +00001310 if (II->DefRec.is<const CodeGenInstruction*>())
1311 II->BuildInstructionResultOperands();
1312 else
1313 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001314 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001315
Jim Grosbacha66512e2011-12-06 23:43:54 +00001316 // Process token alias definitions and set up the associated superclass
1317 // information.
1318 std::vector<Record*> AllTokenAliases =
1319 Records.getAllDerivedDefinitions("TokenAlias");
1320 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1321 Record *Rec = AllTokenAliases[i];
1322 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1323 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1324 FromClass->SuperClasses.push_back(ToClass);
1325 }
1326
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001327 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001328 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001329}
1330
Chris Lattner0bb780c2010-11-04 00:57:06 +00001331/// BuildInstructionOperandReference - The specified operand is a reference to a
1332/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1333void AsmMatcherInfo::
1334BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001335 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001336 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001337 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1338 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001339 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001340
Chris Lattner662e5a32010-11-06 07:14:44 +00001341 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001342 unsigned Idx;
1343 if (!Operands.hasOperandNamed(OperandName, Idx))
1344 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1345 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001346
Bob Wilsona49c7df2011-01-26 19:44:55 +00001347 // If the instruction operand has multiple suboperands, but the parser
1348 // match class for the asm operand is still the default "ImmAsmOperand",
1349 // then handle each suboperand separately.
1350 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1351 Record *Rec = Operands[Idx].Rec;
1352 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1353 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1354 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1355 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1356 StringRef Token = Op->Token; // save this in case Op gets moved
1357 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1358 MatchableInfo::AsmOperand NewAsmOp(Token);
1359 NewAsmOp.SubOpIdx = SI;
1360 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1361 }
1362 // Replace Op with first suboperand.
1363 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1364 Op->SubOpIdx = 0;
1365 }
1366 }
1367
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001368 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001369 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001370
1371 // If the named operand is tied, canonicalize it to the untied operand.
1372 // For example, something like:
1373 // (outs GPR:$dst), (ins GPR:$src)
1374 // with an asmstring of
1375 // "inc $src"
1376 // we want to canonicalize to:
1377 // "inc $dst"
1378 // so that we know how to provide the $dst operand when filling in the result.
1379 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001380 if (OITied != -1) {
1381 // The tied operand index is an MIOperand index, find the operand that
1382 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001383 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1384 OperandName = Operands[Idx.first].Name;
1385 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001386 }
Bob Wilson828295b2011-01-26 21:26:19 +00001387
Bob Wilsona49c7df2011-01-26 19:44:55 +00001388 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001389}
1390
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001391/// BuildAliasOperandReference - When parsing an operand reference out of the
1392/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1393/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001394void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1395 StringRef OperandName,
1396 MatchableInfo::AsmOperand &Op) {
1397 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001398
Chris Lattnerc07bd402010-11-04 02:11:18 +00001399 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001400 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001401 if (CGA.ResultOperands[i].isRecord() &&
1402 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001403 // It's safe to go with the first one we find, because CodeGenInstAlias
1404 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001405 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001406 // Use the match class from the Alias definition, not the
1407 // destination instruction, as we may have an immediate that's
1408 // being munged by the match class.
1409 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001410 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001411 Op.SrcOpName = OperandName;
1412 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001413 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001414
1415 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1416 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001417}
1418
Chris Lattner41409852010-11-06 07:31:43 +00001419void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001420 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001421
Chris Lattner662e5a32010-11-06 07:14:44 +00001422 // Loop over all operands of the result instruction, determining how to
1423 // populate them.
1424 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1425 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001426
1427 // If this is a tied operand, just copy from the previously handled operand.
1428 int TiedOp = OpInfo.getTiedRegister();
1429 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001430 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001431 continue;
1432 }
Bob Wilson828295b2011-01-26 21:26:19 +00001433
Bob Wilsona49c7df2011-01-26 19:44:55 +00001434 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001435 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001436 if (OpInfo.Name.empty() || SrcOperand == -1)
1437 throw TGError(TheDef->getLoc(), "Instruction '" +
1438 TheDef->getName() + "' has operand '" + OpInfo.Name +
1439 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001440
Bob Wilsona49c7df2011-01-26 19:44:55 +00001441 // Check if the one AsmOperand populates the entire operand.
1442 unsigned NumOperands = OpInfo.MINumOperands;
1443 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1444 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001445 continue;
1446 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001447
1448 // Add a separate ResOperand for each suboperand.
1449 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1450 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1451 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1452 "unexpected AsmOperands for suboperands");
1453 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1454 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001455 }
1456}
1457
Chris Lattner41409852010-11-06 07:31:43 +00001458void MatchableInfo::BuildAliasResultOperands() {
1459 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1460 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001461
Chris Lattner41409852010-11-06 07:31:43 +00001462 // Loop over all operands of the result instruction, determining how to
1463 // populate them.
1464 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001465 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001466 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001467 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001468
Chris Lattner41409852010-11-06 07:31:43 +00001469 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001470 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001471 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001472 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001473 continue;
1474 }
1475
Bob Wilsona49c7df2011-01-26 19:44:55 +00001476 // Handle all the suboperands for this operand.
1477 const std::string &OpName = OpInfo->Name;
1478 for ( ; AliasOpNo < LastOpNo &&
1479 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1480 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1481
1482 // Find out what operand from the asmparser that this MCInst operand
1483 // comes from.
1484 switch (CGA.ResultOperands[AliasOpNo].Kind) {
1485 default: assert(0 && "unexpected InstAlias operand kind");
1486 case CodeGenInstAlias::ResultOperand::K_Record: {
1487 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1488 int SrcOperand = FindAsmOperand(Name, SubIdx);
1489 if (SrcOperand == -1)
1490 throw TGError(TheDef->getLoc(), "Instruction '" +
1491 TheDef->getName() + "' has operand '" + OpName +
1492 "' that doesn't appear in asm string!");
1493 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1494 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1495 NumOperands));
1496 break;
1497 }
1498 case CodeGenInstAlias::ResultOperand::K_Imm: {
1499 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1500 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1501 break;
1502 }
1503 case CodeGenInstAlias::ResultOperand::K_Reg: {
1504 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1505 ResOperands.push_back(ResOperand::getRegOp(Reg));
1506 break;
1507 }
1508 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001509 }
Chris Lattner41409852010-11-06 07:31:43 +00001510 }
1511}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001512
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001513static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001514 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001515 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001516 // Write the convert function to a separate stream, so we can drop it after
1517 // the enum.
1518 std::string ConvertFnBody;
1519 raw_string_ostream CvtOS(ConvertFnBody);
1520
Daniel Dunbar20927f22009-08-07 08:26:05 +00001521 // Function we have already generated.
1522 std::set<std::string> GeneratedFns;
1523
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001524 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001525 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1526 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001527 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001528 << " const SmallVectorImpl<MCParsedAsmOperand*"
1529 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001530 CvtOS << " Inst.setOpcode(Opcode);\n";
1531 CvtOS << " switch (Kind) {\n";
1532 CvtOS << " default:\n";
1533
1534 // Start the enum, which we will generate inline.
1535
Chris Lattnerd51257a2010-11-02 23:18:43 +00001536 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001537 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001538
Chris Lattner98986712010-01-14 22:21:20 +00001539 // TargetOperandClass - This is the target's operand class, like X86Operand.
1540 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001541
Chris Lattner22bc5c42010-11-01 05:06:45 +00001542 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001543 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001544 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001545
Daniel Dunbarcf120672011-02-04 17:12:15 +00001546 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001547 std::string AsmMatchConverter =
1548 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001549 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001550 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001551 II.ConversionFnKind = Signature;
1552
1553 // Check if we have already generated this signature.
1554 if (!GeneratedFns.insert(Signature).second)
1555 continue;
1556
1557 // If not, emit it now. Add to the enum list.
1558 OS << " " << Signature << ",\n";
1559
1560 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001561 CvtOS << " return " << AsmMatchConverter
1562 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001563 continue;
1564 }
1565
Daniel Dunbar20927f22009-08-07 08:26:05 +00001566 // Build the conversion function signature.
1567 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001568 std::string CaseBody;
1569 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001570
Chris Lattnerdda855d2010-11-02 21:49:44 +00001571 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001572 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1573 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001574
Chris Lattner1d13bda2010-11-04 00:43:46 +00001575 // Generate code to populate each result operand.
1576 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001577 case MatchableInfo::ResOperand::RenderAsmOperand: {
1578 // This comes from something we parsed.
1579 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001580
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001581 // Registers are always converted the same, don't duplicate the
1582 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001583 Signature += "__";
1584 if (Op.Class->isRegisterClass())
1585 Signature += "Reg";
1586 else
1587 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001588 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001589 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001590
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001591 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001592 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001593 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001594 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001595 }
Bob Wilson828295b2011-01-26 21:26:19 +00001596
Chris Lattner1d13bda2010-11-04 00:43:46 +00001597 case MatchableInfo::ResOperand::TiedOperand: {
1598 // If this operand is tied to a previous one, just copy the MCInst
1599 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001600 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001601 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001602 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001603 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1604 Signature += "__Tie" + utostr(TiedOp);
1605 break;
1606 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001607 case MatchableInfo::ResOperand::ImmOperand: {
1608 int64_t Val = OpInfo.ImmVal;
1609 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1610 Signature += "__imm" + itostr(Val);
1611 break;
1612 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001613 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001614 if (OpInfo.Register == 0) {
1615 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1616 Signature += "__reg0";
1617 } else {
1618 std::string N = getQualifiedName(OpInfo.Register);
1619 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1620 Signature += "__reg" + OpInfo.Register->getName();
1621 }
Bob Wilson828295b2011-01-26 21:26:19 +00001622 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001623 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001624 }
Bob Wilson828295b2011-01-26 21:26:19 +00001625
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001626 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001627
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001628 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001629 if (!GeneratedFns.insert(Signature).second)
1630 continue;
1631
Chris Lattnerdda855d2010-11-02 21:49:44 +00001632 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001633 OS << " " << Signature << ",\n";
1634
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001635 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001636 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001637 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001638 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001639
1640 // Finish the convert function.
1641
1642 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001643 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001644 CvtOS << "}\n\n";
1645
1646 // Finish the enum, and drop the convert function after it.
1647
1648 OS << " NumConversionVariants\n";
1649 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001650
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001651 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001652}
1653
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001654/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1655static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1656 std::vector<ClassInfo*> &Infos,
1657 raw_ostream &OS) {
1658 OS << "namespace {\n\n";
1659
1660 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1661 << "/// instruction matching.\n";
1662 OS << "enum MatchClassKind {\n";
1663 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001664 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001665 ie = Infos.end(); it != ie; ++it) {
1666 ClassInfo &CI = **it;
1667 OS << " " << CI.Name << ", // ";
1668 if (CI.Kind == ClassInfo::Token) {
1669 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001670 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001671 if (!CI.ValueName.empty())
1672 OS << "register class '" << CI.ValueName << "'\n";
1673 else
1674 OS << "derived register class\n";
1675 } else {
1676 OS << "user defined class '" << CI.ValueName << "'\n";
1677 }
1678 }
1679 OS << " NumMatchClassKinds\n";
1680 OS << "};\n\n";
1681
1682 OS << "}\n\n";
1683}
1684
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001685/// EmitValidateOperandClass - Emit the function to validate an operand class.
1686static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1687 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001688 OS << "static bool validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001689 << "MatchClassKind Kind) {\n";
1690 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001691 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001692
Kevin Enderby89381832011-07-15 18:30:43 +00001693 // The InvalidMatchClass is not to match any operand.
1694 OS << " if (Kind == InvalidMatchClass)\n";
1695 OS << " return false;\n\n";
1696
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001697 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001698 OS << " if (Operand.isToken())\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00001699 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind);"
1700 << "\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001701
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001702 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001703 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001704 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001705 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001706 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001707 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001708 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1709 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001710 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001711 << it->first->getName() << ": OpKind = " << it->second->Name
1712 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001713 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001714 OS << " return isSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001715 OS << " }\n\n";
1716
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001717 // Check the user classes. We don't care what order since we're only
1718 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001719 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001720 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001721 ClassInfo &CI = **it;
1722
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001723 if (!CI.isUserClass())
1724 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001725
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001726 OS << " // '" << CI.ClassName << "' class\n";
1727 OS << " if (Kind == " << CI.Name
1728 << " && Operand." << CI.PredicateMethod << "()) {\n";
1729 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001730 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001731 }
Bob Wilson828295b2011-01-26 21:26:19 +00001732
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001733 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001734 OS << "}\n\n";
1735}
1736
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001737/// EmitIsSubclass - Emit the subclass predicate function.
1738static void EmitIsSubclass(CodeGenTarget &Target,
1739 std::vector<ClassInfo*> &Infos,
1740 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001741 OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1742 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001743 OS << " if (A == B)\n";
1744 OS << " return true;\n\n";
1745
1746 OS << " switch (A) {\n";
1747 OS << " default:\n";
1748 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001749 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001750 ie = Infos.end(); it != ie; ++it) {
1751 ClassInfo &A = **it;
1752
Jim Grosbacha66512e2011-12-06 23:43:54 +00001753 std::vector<StringRef> SuperClasses;
1754 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
1755 ie = Infos.end(); it != ie; ++it) {
1756 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001757
Jim Grosbacha66512e2011-12-06 23:43:54 +00001758 if (&A != &B && A.isSubsetOf(B))
1759 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001760 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00001761
1762 if (SuperClasses.empty())
1763 continue;
1764
1765 OS << "\n case " << A.Name << ":\n";
1766
1767 if (SuperClasses.size() == 1) {
1768 OS << " return B == " << SuperClasses.back() << ";\n";
1769 continue;
1770 }
1771
1772 OS << " switch (B) {\n";
1773 OS << " default: return false;\n";
1774 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1775 OS << " case " << SuperClasses[i] << ": return true;\n";
1776 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001777 }
1778 OS << " }\n";
1779 OS << "}\n\n";
1780}
1781
Daniel Dunbar245f0582009-08-08 21:22:41 +00001782/// EmitMatchTokenString - Emit the function to match a token string to the
1783/// appropriate match class value.
1784static void EmitMatchTokenString(CodeGenTarget &Target,
1785 std::vector<ClassInfo*> &Infos,
1786 raw_ostream &OS) {
1787 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001788 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001789 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001790 ie = Infos.end(); it != ie; ++it) {
1791 ClassInfo &CI = **it;
1792
1793 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001794 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1795 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001796 }
1797
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001798 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001799
Chris Lattner5845e5c2010-09-06 02:01:51 +00001800 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001801
1802 OS << " return InvalidMatchClass;\n";
1803 OS << "}\n\n";
1804}
Chris Lattner70add882009-08-08 20:02:57 +00001805
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001806/// EmitMatchRegisterName - Emit the function to match a string to the target
1807/// specific register enum.
1808static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1809 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001810 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001811 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001812 const std::vector<CodeGenRegister*> &Regs =
1813 Target.getRegBank().getRegisters();
1814 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1815 const CodeGenRegister *Reg = Regs[i];
1816 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001817 continue;
1818
Chris Lattner5845e5c2010-09-06 02:01:51 +00001819 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001820 Reg->TheDef->getValueAsString("AsmName"),
1821 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001822 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001823
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001824 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001825
Chris Lattner5845e5c2010-09-06 02:01:51 +00001826 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001827
Daniel Dunbar245f0582009-08-08 21:22:41 +00001828 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001829 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001830}
Daniel Dunbara027d222009-07-31 02:32:59 +00001831
Daniel Dunbar54074b52010-07-19 05:44:09 +00001832/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1833/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001834static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001835 raw_ostream &OS) {
1836 OS << "// Flags for subtarget features that participate in "
1837 << "instruction matching.\n";
1838 OS << "enum SubtargetFeatureFlag {\n";
1839 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1840 it = Info.SubtargetFeatures.begin(),
1841 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1842 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001843 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001844 }
1845 OS << " Feature_None = 0\n";
1846 OS << "};\n\n";
1847}
1848
1849/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1850/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001851static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001852 raw_ostream &OS) {
1853 std::string ClassName =
1854 Info.AsmParser->getValueAsString("AsmParserClassName");
1855
Chris Lattner02bcbc92010-11-01 01:37:30 +00001856 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00001857 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001858 OS << " unsigned Features = 0;\n";
1859 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1860 it = Info.SubtargetFeatures.begin(),
1861 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1862 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00001863
1864 OS << " if (";
Evan Chengfbc38d22011-07-08 18:04:22 +00001865 std::string CondStorage = SFI.TheDef->getValueAsString("AssemblerCondString");
1866 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00001867 std::pair<StringRef,StringRef> Comma = Conds.split(',');
1868 bool First = true;
1869 do {
1870 if (!First)
1871 OS << " && ";
1872
1873 bool Neg = false;
1874 StringRef Cond = Comma.first;
1875 if (Cond[0] == '!') {
1876 Neg = true;
1877 Cond = Cond.substr(1);
1878 }
1879
1880 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
1881 if (Neg)
1882 OS << " == 0";
1883 else
1884 OS << " != 0";
1885 OS << ")";
1886
1887 if (Comma.second.empty())
1888 break;
1889
1890 First = false;
1891 Comma = Comma.second.split(',');
1892 } while (true);
1893
1894 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001895 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001896 }
1897 OS << " return Features;\n";
1898 OS << "}\n\n";
1899}
1900
Chris Lattner6fa152c2010-10-30 20:15:02 +00001901static std::string GetAliasRequiredFeatures(Record *R,
1902 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001903 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001904 std::string Result;
1905 unsigned NumFeatures = 0;
1906 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001907 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001908
Chris Lattner4a74ee72010-11-01 02:09:21 +00001909 if (F == 0)
1910 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1911 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001912
Chris Lattner4a74ee72010-11-01 02:09:21 +00001913 if (NumFeatures)
1914 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001915
Chris Lattner4a74ee72010-11-01 02:09:21 +00001916 Result += F->getEnumName();
1917 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001918 }
Bob Wilson828295b2011-01-26 21:26:19 +00001919
Chris Lattner693173f2010-10-30 19:23:13 +00001920 if (NumFeatures > 1)
1921 Result = '(' + Result + ')';
1922 return Result;
1923}
1924
Chris Lattner674c1dc2010-10-30 17:36:36 +00001925/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001926/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001927static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001928 // Ignore aliases when match-prefix is set.
1929 if (!MatchPrefix.empty())
1930 return false;
1931
Chris Lattner674c1dc2010-10-30 17:36:36 +00001932 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001933 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001934 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001935
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001936 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001937 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001938
Chris Lattner4fd32c62010-10-30 18:56:12 +00001939 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1940 // iteration order of the map is stable.
1941 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001942
Chris Lattner674c1dc2010-10-30 17:36:36 +00001943 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1944 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001945 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001946 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001947
1948 // Process each alias a "from" mnemonic at a time, building the code executed
1949 // by the string remapper.
1950 std::vector<StringMatcher::StringPair> Cases;
1951 for (std::map<std::string, std::vector<Record*> >::iterator
1952 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1953 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001954 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001955
1956 // Loop through each alias and emit code that handles each case. If there
1957 // are two instructions without predicates, emit an error. If there is one,
1958 // emit it last.
1959 std::string MatchCode;
1960 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001961
Chris Lattner693173f2010-10-30 19:23:13 +00001962 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1963 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001964 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001965
Chris Lattner693173f2010-10-30 19:23:13 +00001966 // If this unconditionally matches, remember it for later and diagnose
1967 // duplicates.
1968 if (FeatureMask.empty()) {
1969 if (AliasWithNoPredicate != -1) {
1970 // We can't have two aliases from the same mnemonic with no predicate.
1971 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1972 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001973 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001974 }
Bob Wilson828295b2011-01-26 21:26:19 +00001975
Chris Lattner693173f2010-10-30 19:23:13 +00001976 AliasWithNoPredicate = i;
1977 continue;
1978 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001979 if (R->getValueAsString("ToMnemonic") == I->first)
1980 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001981
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001982 if (!MatchCode.empty())
1983 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001984 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1985 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001986 }
Bob Wilson828295b2011-01-26 21:26:19 +00001987
Chris Lattner693173f2010-10-30 19:23:13 +00001988 if (AliasWithNoPredicate != -1) {
1989 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001990 if (!MatchCode.empty())
1991 MatchCode += "else\n ";
1992 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001993 }
Bob Wilson828295b2011-01-26 21:26:19 +00001994
Chris Lattner693173f2010-10-30 19:23:13 +00001995 MatchCode += "return;";
1996
1997 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001998 }
Bob Wilson828295b2011-01-26 21:26:19 +00001999
Chris Lattner674c1dc2010-10-30 17:36:36 +00002000 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00002001 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00002002
Chris Lattner7fd44892010-10-30 18:48:18 +00002003 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00002004}
2005
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002006static const char *getMinimalTypeForRange(uint64_t Range) {
2007 assert(Range < 0xFFFFFFFFULL && "Enum too large");
2008 if (Range > 0xFFFF)
2009 return "uint32_t";
2010 if (Range > 0xFF)
2011 return "uint16_t";
2012 return "uint8_t";
2013}
2014
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002015static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2016 const AsmMatcherInfo &Info, StringRef ClassName) {
2017 // Emit the static custom operand parsing table;
2018 OS << "namespace {\n";
2019 OS << " struct OperandMatchEntry {\n";
2020 OS << " const char *Mnemonic;\n";
2021 OS << " unsigned OperandMask;\n";
2022 OS << " MatchClassKind Class;\n";
2023 OS << " unsigned RequiredFeatures;\n";
2024 OS << " };\n\n";
2025
2026 OS << " // Predicate for searching for an opcode.\n";
2027 OS << " struct LessOpcodeOperand {\n";
2028 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2029 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2030 OS << " }\n";
2031 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2032 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2033 OS << " }\n";
2034 OS << " bool operator()(const OperandMatchEntry &LHS,";
2035 OS << " const OperandMatchEntry &RHS) {\n";
2036 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2037 OS << " }\n";
2038 OS << " };\n";
2039
2040 OS << "} // end anonymous namespace.\n\n";
2041
2042 OS << "static const OperandMatchEntry OperandMatchTable["
2043 << Info.OperandMatchInfo.size() << "] = {\n";
2044
2045 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
2046 for (std::vector<OperandMatchEntry>::const_iterator it =
2047 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2048 it != ie; ++it) {
2049 const OperandMatchEntry &OMI = *it;
2050 const MatchableInfo &II = *OMI.MI;
2051
2052 OS << " { \"" << II.Mnemonic << "\""
2053 << ", " << OMI.OperandMask;
2054
2055 OS << " /* ";
2056 bool printComma = false;
2057 for (int i = 0, e = 31; i !=e; ++i)
2058 if (OMI.OperandMask & (1 << i)) {
2059 if (printComma)
2060 OS << ", ";
2061 OS << i;
2062 printComma = true;
2063 }
2064 OS << " */";
2065
2066 OS << ", " << OMI.CI->Name
2067 << ", ";
2068
2069 // Write the required features mask.
2070 if (!II.RequiredFeatures.empty()) {
2071 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2072 if (i) OS << "|";
2073 OS << II.RequiredFeatures[i]->getEnumName();
2074 }
2075 } else
2076 OS << "0";
2077 OS << " },\n";
2078 }
2079 OS << "};\n\n";
2080
2081 // Emit the operand class switch to call the correct custom parser for
2082 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002083 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2084 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002085 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002086 << " &Operands,\n unsigned MCK) {\n\n"
2087 << " switch(MCK) {\n";
2088
2089 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2090 ie = Info.Classes.end(); it != ie; ++it) {
2091 ClassInfo *CI = *it;
2092 if (CI->ParserMethod.empty())
2093 continue;
2094 OS << " case " << CI->Name << ":\n"
2095 << " return " << CI->ParserMethod << "(Operands);\n";
2096 }
2097
2098 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002099 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002100 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002101 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002102 OS << "}\n\n";
2103
2104 // Emit the static custom operand parser. This code is very similar with
2105 // the other matcher. Also use MatchResultTy here just in case we go for
2106 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002107 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002108 << Target.getName() << ClassName << "::\n"
2109 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2110 << " &Operands,\n StringRef Mnemonic) {\n";
2111
2112 // Emit code to get the available features.
2113 OS << " // Get the current feature set.\n";
2114 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2115
2116 OS << " // Get the next operand index.\n";
2117 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2118
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002119 // Emit code to search the table.
2120 OS << " // Search the table.\n";
2121 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2122 OS << " MnemonicRange =\n";
2123 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2124 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2125 << " LessOpcodeOperand());\n\n";
2126
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002127 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002128 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002129
2130 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2131 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2132
2133 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2134 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2135
2136 // Emit check that the required features are available.
2137 OS << " // check if the available features match\n";
2138 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2139 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002140 OS << " continue;\n";
2141 OS << " }\n\n";
2142
2143 // Emit check to ensure the operand number matches.
2144 OS << " // check if the operand in question has a custom parser.\n";
2145 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2146 OS << " continue;\n\n";
2147
2148 // Emit call to the custom parser method
2149 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002150 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002151 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002152 OS << " if (Result != MatchOperand_NoMatch)\n";
2153 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002154 OS << " }\n\n";
2155
Jim Grosbachf922c472011-02-12 01:34:40 +00002156 OS << " // Okay, we had no match.\n";
2157 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002158 OS << "}\n\n";
2159}
2160
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002161void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002162 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002163 Record *AsmParser = Target.getAsmParser();
2164 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2165
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002166 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002167 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002168 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002169
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002170 // Sort the instruction table using the partial order on classes. We use
2171 // stable_sort to ensure that ambiguous instructions are still
2172 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002173 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2174 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002175
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002176 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002177 for (std::vector<MatchableInfo*>::iterator
2178 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002179 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002180 (*it)->dump();
2181 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002182
Chris Lattner22bc5c42010-11-01 05:06:45 +00002183 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002184 DEBUG_WITH_TYPE("ambiguous_instrs", {
2185 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002186 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002187 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002188 MatchableInfo &A = *Info.Matchables[i];
2189 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002190
Bob Wilson1f64ac42011-01-26 21:26:21 +00002191 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002192 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002193 A.dump();
2194 errs() << "\nis incomparable with:\n";
2195 B.dump();
2196 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002197 ++NumAmbiguous;
2198 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002199 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002200 }
Chris Lattner87410362010-09-06 20:21:47 +00002201 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002202 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002203 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002204 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002205
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002206 // Compute the information on the custom operand parsing.
2207 Info.BuildOperandMatchInfo();
2208
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002209 // Write the output.
2210
2211 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2212
Chris Lattner0692ee62010-09-06 19:11:01 +00002213 // Information for the class declaration.
2214 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2215 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002216 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002217 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002218 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002219 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2220 << "unsigned Opcode,\n"
2221 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2222 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002223 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002224 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002225 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002226 OS << " MCInst &Inst, unsigned &ErrorInfo);\n";
2227
2228 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002229 OS << "\n enum OperandMatchResultTy {\n";
2230 OS << " MatchOperand_Success, // operand matched successfully\n";
2231 OS << " MatchOperand_NoMatch, // operand did not match\n";
2232 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2233 OS << " };\n";
2234 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002235 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2236 OS << " StringRef Mnemonic);\n";
2237
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002238 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002239 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2240 OS << " unsigned MCK);\n\n";
2241 }
2242
Chris Lattner0692ee62010-09-06 19:11:01 +00002243 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2244
Chris Lattner0692ee62010-09-06 19:11:01 +00002245 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2246 OS << "#undef GET_REGISTER_MATCHER\n\n";
2247
Daniel Dunbar54074b52010-07-19 05:44:09 +00002248 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002249 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002250
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002251 // Emit the function to match a register name to number.
2252 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002253
2254 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002255
Chris Lattner0692ee62010-09-06 19:11:01 +00002256
2257 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2258 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002259
Chris Lattner7fd44892010-10-30 18:48:18 +00002260 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002261 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002262
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002263 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002264 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002265
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002266 // Emit the enumeration for classes which participate in matching.
2267 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002268
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002269 // Emit the routine to match token strings to their match class.
2270 EmitMatchTokenString(Target, Info.Classes, OS);
2271
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002272 // Emit the subclass predicate routine.
2273 EmitIsSubclass(Target, Info.Classes, OS);
2274
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002275 // Emit the routine to validate an operand against a match class.
2276 EmitValidateOperandClass(Info, OS);
2277
Daniel Dunbar54074b52010-07-19 05:44:09 +00002278 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002279 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002280
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002281
2282 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002283 for (std::vector<MatchableInfo*>::const_iterator it =
2284 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002285 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002286 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002287
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002288 // Emit the static match table; unused classes get initalized to 0 which is
2289 // guaranteed to be InvalidMatchClass.
2290 //
2291 // FIXME: We can reduce the size of this table very easily. First, we change
2292 // it so that store the kinds in separate bit-fields for each index, which
2293 // only needs to be the max width used for classes at that index (we also need
2294 // to reject based on this during classification). If we then make sure to
2295 // order the match kinds appropriately (putting mnemonics last), then we
2296 // should only end up using a few bits for each class, especially the ones
2297 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002298 OS << "namespace {\n";
2299 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002300 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002301 OS << " const char *Mnemonic;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002302 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2303 << " ConvertFn;\n";
2304 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2305 << " Classes[" << MaxNumOperands << "];\n";
2306 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2307 << " RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002308 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002309
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002310 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002311 OS << " struct LessOpcode {\n";
2312 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2313 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2314 OS << " }\n";
2315 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2316 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2317 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002318 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2319 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2320 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002321 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002322
Chris Lattner96352e52010-09-06 21:08:38 +00002323 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002324
Chris Lattner96352e52010-09-06 21:08:38 +00002325 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002326 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002327
Chris Lattner22bc5c42010-11-01 05:06:45 +00002328 for (std::vector<MatchableInfo*>::const_iterator it =
2329 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002330 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002331 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002332
Chris Lattner662e5a32010-11-06 07:14:44 +00002333 OS << " { " << Target.getName() << "::"
2334 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2335 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002336 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002337 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002338
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002339 if (i) OS << ", ";
2340 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002341 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002342 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002343
Daniel Dunbar54074b52010-07-19 05:44:09 +00002344 // Write the required features mask.
2345 if (!II.RequiredFeatures.empty()) {
2346 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2347 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002348 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002349 }
2350 } else
2351 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002352
Daniel Dunbar54074b52010-07-19 05:44:09 +00002353 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002354 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002355
Chris Lattner96352e52010-09-06 21:08:38 +00002356 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002357
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002358 // A method to determine if a mnemonic is in the list.
2359 OS << "bool " << Target.getName() << ClassName << "::\n"
2360 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2361 OS << " // Search the table.\n";
2362 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2363 OS << " std::equal_range(MatchTable, MatchTable+"
2364 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2365 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2366 OS << "}\n\n";
2367
Chris Lattner96352e52010-09-06 21:08:38 +00002368 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002369 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002370 << Target.getName() << ClassName << "::\n"
2371 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2372 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002373 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002374
2375 // Emit code to get the available features.
2376 OS << " // Get the current feature set.\n";
2377 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2378
Chris Lattner674c1dc2010-10-30 17:36:36 +00002379 OS << " // Get the instruction mnemonic, which is the first token.\n";
2380 OS << " StringRef Mnemonic = ((" << Target.getName()
2381 << "Operand*)Operands[0])->getToken();\n\n";
2382
Chris Lattner7fd44892010-10-30 18:48:18 +00002383 if (HasMnemonicAliases) {
2384 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002385 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002386 }
Bob Wilson828295b2011-01-26 21:26:19 +00002387
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002388 // Emit code to compute the class list for this operand vector.
2389 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002390 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2391 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2392 OS << " return Match_InvalidOperand;\n";
2393 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002394
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002395 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002396 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002397 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002398 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002399 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002400 OS << " // wrong for all instances of the instruction.\n";
2401 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002402
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002403 // Emit code to search the table.
2404 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002405 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2406 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002407 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002408
Chris Lattnera008e8a2010-09-06 21:54:15 +00002409 OS << " // Return a more specific error code if no mnemonics match.\n";
2410 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2411 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002412
Chris Lattner2b1f9432010-09-06 21:22:45 +00002413 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002414 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002415 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002416
Gabor Greife53ee3b2010-09-07 06:06:06 +00002417 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002418 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002419
Daniel Dunbar54074b52010-07-19 05:44:09 +00002420 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002421 OS << " bool OperandsValid = true;\n";
2422 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002423 OS << " if (i + 1 >= Operands.size()) {\n";
2424 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002425 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002426 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002427 OS << " if (validateOperandClass(Operands[i+1], "
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002428 "(MatchClassKind)it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002429 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002430 OS << " // If this operand is broken for all of the instances of this\n";
2431 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002432 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002433 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002434 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2435 OS << " OperandsValid = false;\n";
2436 OS << " break;\n";
2437 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002438
Chris Lattnerce4a3352010-09-06 22:11:18 +00002439 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002440
2441 // Emit check that the required features are available.
2442 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2443 << "!= it->RequiredFeatures) {\n";
2444 OS << " HadMatchOtherThanFeatures = true;\n";
2445 OS << " continue;\n";
2446 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002447 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002448 OS << " // We have selected a definite instruction, convert the parsed\n"
2449 << " // operands into the appropriate MCInst.\n";
2450 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2451 << " it->Opcode, Operands))\n";
2452 OS << " return Match_ConversionFail;\n";
2453 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002454
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002455 // Verify the instruction with the target-specific match predicate function.
2456 OS << " // We have a potential match. Check the target predicate to\n"
2457 << " // handle any context sensitive constraints.\n"
2458 << " unsigned MatchResult;\n"
2459 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2460 << " Match_Success) {\n"
2461 << " Inst.clear();\n"
2462 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002463 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002464 << " continue;\n"
2465 << " }\n\n";
2466
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002467 // Call the post-processing function, if used.
2468 std::string InsnCleanupFn =
2469 AsmParser->getValueAsString("AsmParserInstCleanup");
2470 if (!InsnCleanupFn.empty())
2471 OS << " " << InsnCleanupFn << "(Inst);\n";
2472
Chris Lattner79ed3f72010-09-06 19:22:17 +00002473 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002474 OS << " }\n\n";
2475
Chris Lattnerec6789f2010-09-06 20:08:02 +00002476 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002477 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
2478 OS << " return RetCode;\n";
2479 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002480 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002481
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002482 if (Info.OperandMatchInfo.size())
2483 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2484
Chris Lattner0692ee62010-09-06 19:11:01 +00002485 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002486}