blob: 5ef0db905ccfcffde2c477ef063435992d5e5a73 [file] [log] [blame]
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001//===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a target specifier matcher for converting parsed
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000011// assembly operands in the MCInst structures. It also emits a matcher for
12// custom operand parsing.
13//
14// Converting assembly operands into MCInst structures
15// ---------------------------------------------------
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000016//
Daniel Dunbar20927f22009-08-07 08:26:05 +000017// The input to the target specific matcher is a list of literal tokens and
18// operands. The target specific parser should generally eliminate any syntax
19// which is not relevant for matching; for example, comma tokens should have
20// already been consumed and eliminated by the parser. Most instructions will
21// end up with a single literal token (the instruction name) and some number of
22// operands.
23//
24// Some example inputs, for X86:
25// 'addl' (immediate ...) (register ...)
26// 'add' (immediate ...) (memory ...)
Jim Grosbacha7c78222010-10-29 22:13:48 +000027// 'call' '*' %epc
Daniel Dunbar20927f22009-08-07 08:26:05 +000028//
29// The assembly matcher is responsible for converting this input into a precise
30// machine instruction (i.e., an instruction with a well defined encoding). This
31// mapping has several properties which complicate matching:
32//
33// - It may be ambiguous; many architectures can legally encode particular
34// variants of an instruction in different ways (for example, using a smaller
35// encoding for small immediates). Such ambiguities should never be
36// arbitrarily resolved by the assembler, the assembler is always responsible
37// for choosing the "best" available instruction.
38//
39// - It may depend on the subtarget or the assembler context. Instructions
40// which are invalid for the current mode, but otherwise unambiguous (e.g.,
41// an SSE instruction in a file being assembled for i486) should be accepted
42// and rejected by the assembler front end. However, if the proper encoding
43// for an instruction is dependent on the assembler context then the matcher
44// is responsible for selecting the correct machine instruction for the
45// current mode.
46//
47// The core matching algorithm attempts to exploit the regularity in most
48// instruction sets to quickly determine the set of possibly matching
49// instructions, and the simplify the generated code. Additionally, this helps
50// to ensure that the ambiguities are intentionally resolved by the user.
51//
52// The matching is divided into two distinct phases:
53//
54// 1. Classification: Each operand is mapped to the unique set which (a)
55// contains it, and (b) is the largest such subset for which a single
56// instruction could match all members.
57//
58// For register classes, we can generate these subgroups automatically. For
59// arbitrary operands, we expect the user to define the classes and their
60// relations to one another (for example, 8-bit signed immediates as a
61// subset of 32-bit immediates).
62//
63// By partitioning the operands in this way, we guarantee that for any
64// tuple of classes, any single instruction must match either all or none
65// of the sets of operands which could classify to that tuple.
66//
67// In addition, the subset relation amongst classes induces a partial order
68// on such tuples, which we use to resolve ambiguities.
69//
Daniel Dunbar20927f22009-08-07 08:26:05 +000070// 2. The input can now be treated as a tuple of classes (static tokens are
71// simple singleton sets). Each such tuple should generally map to a single
72// instruction (we currently ignore cases where this isn't true, whee!!!),
73// which we can emit a simple matcher for.
74//
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000075// Custom Operand Parsing
76// ----------------------
77//
78// Some targets need a custom way to parse operands, some specific instructions
79// can contain arguments that can represent processor flags and other kinds of
80// identifiers that need to be mapped to specific valeus in the final encoded
81// instructions. The target specific custom operand parsing works in the
82// following way:
83//
84// 1. A operand match table is built, each entry contains a mnemonic, an
85// operand class, a mask for all operand positions for that same
86// class/mnemonic and target features to be checked while trying to match.
87//
88// 2. The operand matcher will try every possible entry with the same
89// mnemonic and will check if the target feature for this mnemonic also
90// matches. After that, if the operand to be matched has its index
Chris Lattner7a2bdde2011-04-15 05:18:47 +000091// present in the mask, a successful match occurs. Otherwise, fallback
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000092// to the regular operand parsing.
93//
94// 3. For a match success, each operand class that has a 'ParserMethod'
95// becomes part of a switch from where the custom method is called.
96//
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000097//===----------------------------------------------------------------------===//
98
99#include "AsmMatcherEmitter.h"
100#include "CodeGenTarget.h"
Chris Lattner5845e5c2010-09-06 02:01:51 +0000101#include "StringMatcher.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000102#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc07bd402010-11-04 02:11:18 +0000103#include "llvm/ADT/PointerUnion.h"
Chris Lattner1de88232010-11-01 01:47:07 +0000104#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000105#include "llvm/ADT/SmallVector.h"
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000106#include "llvm/ADT/STLExtras.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000107#include "llvm/ADT/StringExtras.h"
108#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000109#include "llvm/Support/Debug.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +0000110#include "llvm/TableGen/Error.h"
111#include "llvm/TableGen/Record.h"
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000112#include <map>
113#include <set>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000114using namespace llvm;
115
Daniel Dunbar27249152009-08-07 20:33:39 +0000116static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000117MatchPrefix("match-prefix", cl::init(""),
118 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000119
Daniel Dunbar20927f22009-08-07 08:26:05 +0000120namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000121class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000122struct SubtargetFeatureInfo;
123
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000124/// ClassInfo - Helper class for storing the information about a particular
125/// class of operands which can be matched.
126struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000127 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000128 /// Invalid kind, for use as a sentinel value.
129 Invalid = 0,
130
131 /// The class for a particular token.
132 Token,
133
134 /// The (first) register class, subsequent register classes are
135 /// RegisterClass0+1, and so on.
136 RegisterClass0,
137
138 /// The (first) user defined class, subsequent user defined classes are
139 /// UserClass0+1, and so on.
140 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000141 };
142
143 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
144 /// N) for the Nth user defined class.
145 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000146
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000147 /// SuperClasses - The super classes of this class. Note that for simplicities
148 /// sake user operands only record their immediate super class, while register
149 /// operands include all superclasses.
150 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000151
Daniel Dunbar6745d422009-08-09 05:18:30 +0000152 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000153 std::string Name;
154
Daniel Dunbar6745d422009-08-09 05:18:30 +0000155 /// ClassName - The unadorned generic name for this class (e.g., Token).
156 std::string ClassName;
157
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000158 /// ValueName - The name of the value this class represents; for a token this
159 /// is the literal token string, for an operand it is the TableGen class (or
160 /// empty if this is a derived class).
161 std::string ValueName;
162
163 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000164 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000165 std::string PredicateMethod;
166
167 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000168 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000169 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000170
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000171 /// ParserMethod - The name of the operand method to do a target specific
172 /// parsing on the operand.
173 std::string ParserMethod;
174
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000175 /// For register classes, the records for all the registers in this class.
176 std::set<Record*> Registers;
177
178public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000179 /// isRegisterClass() - Check if this is a register class.
180 bool isRegisterClass() const {
181 return Kind >= RegisterClass0 && Kind < UserClass0;
182 }
183
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000184 /// isUserClass() - Check if this is a user defined class.
185 bool isUserClass() const {
186 return Kind >= UserClass0;
187 }
188
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000189 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
190 /// are related if they are in the same class hierarchy.
191 bool isRelatedTo(const ClassInfo &RHS) const {
192 // Tokens are only related to tokens.
193 if (Kind == Token || RHS.Kind == Token)
194 return Kind == Token && RHS.Kind == Token;
195
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000196 // Registers classes are only related to registers classes, and only if
197 // their intersection is non-empty.
198 if (isRegisterClass() || RHS.isRegisterClass()) {
199 if (!isRegisterClass() || !RHS.isRegisterClass())
200 return false;
201
202 std::set<Record*> Tmp;
203 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000204 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000205 RHS.Registers.begin(), RHS.Registers.end(),
206 II);
207
208 return !Tmp.empty();
209 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000210
211 // Otherwise we have two users operands; they are related if they are in the
212 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000213 //
214 // FIXME: This is an oversimplification, they should only be related if they
215 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000216 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
217 const ClassInfo *Root = this;
218 while (!Root->SuperClasses.empty())
219 Root = Root->SuperClasses.front();
220
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000221 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000222 while (!RHSRoot->SuperClasses.empty())
223 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000224
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000225 return Root == RHSRoot;
226 }
227
Jim Grosbacha7c78222010-10-29 22:13:48 +0000228 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000229 bool isSubsetOf(const ClassInfo &RHS) const {
230 // This is a subset of RHS if it is the same class...
231 if (this == &RHS)
232 return true;
233
234 // ... or if any of its super classes are a subset of RHS.
235 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
236 ie = SuperClasses.end(); it != ie; ++it)
237 if ((*it)->isSubsetOf(RHS))
238 return true;
239
240 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000241 }
242
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000243 /// operator< - Compare two classes.
244 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000245 if (this == &RHS)
246 return false;
247
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000248 // Unrelated classes can be ordered by kind.
249 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000250 return Kind < RHS.Kind;
251
252 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000253 case Invalid:
254 assert(0 && "Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000255
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000256 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000257 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000258 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000259 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000260 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000261 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000262
263 // Otherwise, order by name to ensure we have a total ordering.
264 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000265 }
266 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000267};
268
Chris Lattner22bc5c42010-11-01 05:06:45 +0000269/// MatchableInfo - Helper class for storing the necessary information for an
270/// instruction or alias which is capable of being matched.
271struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000272 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000273 /// Token - This is the token that the operand came from.
274 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000275
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000276 /// The unique class instance this operand should match.
277 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000278
Chris Lattner567820c2010-11-04 01:42:59 +0000279 /// The operand name this is, if anything.
280 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000281
282 /// The suboperand index within SrcOpName, or -1 for the entire operand.
283 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000284
Bob Wilsona49c7df2011-01-26 19:44:55 +0000285 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000286 };
Bob Wilson828295b2011-01-26 21:26:19 +0000287
Chris Lattner1d13bda2010-11-04 00:43:46 +0000288 /// ResOperand - This represents a single operand in the result instruction
289 /// generated by the match. In cases (like addressing modes) where a single
290 /// assembler operand expands to multiple MCOperands, this represents the
291 /// single assembler operand, not the MCOperand.
292 struct ResOperand {
293 enum {
294 /// RenderAsmOperand - This represents an operand result that is
295 /// generated by calling the render method on the assembly operand. The
296 /// corresponding AsmOperand is specified by AsmOperandNum.
297 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000298
Chris Lattner1d13bda2010-11-04 00:43:46 +0000299 /// TiedOperand - This represents a result operand that is a duplicate of
300 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000301 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000302
Chris Lattner98c870f2010-11-06 19:25:43 +0000303 /// ImmOperand - This represents an immediate value that is dumped into
304 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000305 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000306
Chris Lattner90fd7972010-11-06 19:57:21 +0000307 /// RegOperand - This represents a fixed register that is dumped in.
308 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000309 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000310
Chris Lattner1d13bda2010-11-04 00:43:46 +0000311 union {
312 /// This is the operand # in the AsmOperands list that this should be
313 /// copied from.
314 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000315
Chris Lattner1d13bda2010-11-04 00:43:46 +0000316 /// TiedOperandNum - This is the (earlier) result operand that should be
317 /// copied from.
318 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000319
Chris Lattner98c870f2010-11-06 19:25:43 +0000320 /// ImmVal - This is the immediate value added to the instruction.
321 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000322
Chris Lattner90fd7972010-11-06 19:57:21 +0000323 /// Register - This is the register record.
324 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000325 };
Bob Wilson828295b2011-01-26 21:26:19 +0000326
Bob Wilsona49c7df2011-01-26 19:44:55 +0000327 /// MINumOperands - The number of MCInst operands populated by this
328 /// operand.
329 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000330
Bob Wilsona49c7df2011-01-26 19:44:55 +0000331 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000332 ResOperand X;
333 X.Kind = RenderAsmOperand;
334 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000335 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000336 return X;
337 }
Bob Wilson828295b2011-01-26 21:26:19 +0000338
Bob Wilsona49c7df2011-01-26 19:44:55 +0000339 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000340 ResOperand X;
341 X.Kind = TiedOperand;
342 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000343 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000344 return X;
345 }
Bob Wilson828295b2011-01-26 21:26:19 +0000346
Bob Wilsona49c7df2011-01-26 19:44:55 +0000347 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000348 ResOperand X;
349 X.Kind = ImmOperand;
350 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000351 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000352 return X;
353 }
Bob Wilson828295b2011-01-26 21:26:19 +0000354
Bob Wilsona49c7df2011-01-26 19:44:55 +0000355 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000356 ResOperand X;
357 X.Kind = RegOperand;
358 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000359 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000360 return X;
361 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000362 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363
Chris Lattner3b5aec62010-11-02 17:34:28 +0000364 /// TheDef - This is the definition of the instruction or InstAlias that this
365 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000366 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000367
Chris Lattnerc07bd402010-11-04 02:11:18 +0000368 /// DefRec - This is the definition that it came from.
369 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000370
Chris Lattner662e5a32010-11-06 07:14:44 +0000371 const CodeGenInstruction *getResultInst() const {
372 if (DefRec.is<const CodeGenInstruction*>())
373 return DefRec.get<const CodeGenInstruction*>();
374 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
375 }
Bob Wilson828295b2011-01-26 21:26:19 +0000376
Chris Lattner1d13bda2010-11-04 00:43:46 +0000377 /// ResOperands - This is the operand list that should be built for the result
378 /// MCInst.
379 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380
381 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000382 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 std::string AsmString;
384
Chris Lattnerd19ec052010-11-02 17:30:52 +0000385 /// Mnemonic - This is the first token of the matched instruction, its
386 /// mnemonic.
387 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000388
Chris Lattner3116fef2010-11-02 01:03:43 +0000389 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000390 /// annotated with a class and where in the OperandList they were defined.
391 /// This directly corresponds to the tokenized AsmString after the mnemonic is
392 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000393 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000394
Daniel Dunbar54074b52010-07-19 05:44:09 +0000395 /// Predicates - The required subtarget features to match this instruction.
396 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
397
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000398 /// ConversionFnKind - The enum value which is passed to the generated
399 /// ConvertToMCInst to convert parsed operands into an MCInst for this
400 /// function.
401 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000402
Chris Lattner22bc5c42010-11-01 05:06:45 +0000403 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattner662e5a32010-11-06 07:14:44 +0000404 : TheDef(CGI.TheDef), DefRec(&CGI), AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000405 }
406
Chris Lattner22bc5c42010-11-01 05:06:45 +0000407 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattner662e5a32010-11-06 07:14:44 +0000408 : TheDef(Alias->TheDef), DefRec(Alias), AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000409 }
Bob Wilson828295b2011-01-26 21:26:19 +0000410
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000411 void Initialize(const AsmMatcherInfo &Info,
412 SmallPtrSet<Record*, 16> &SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +0000413
Chris Lattner22bc5c42010-11-01 05:06:45 +0000414 /// Validate - Return true if this matchable is a valid thing to match against
415 /// and perform a bunch of validity checking.
416 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000417
Chris Lattnerd19ec052010-11-02 17:30:52 +0000418 /// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000419 /// register, return the Record for it, otherwise return null.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000420 Record *getSingletonRegisterForAsmOperand(unsigned i,
Bob Wilson828295b2011-01-26 21:26:19 +0000421 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000422
Bob Wilsona49c7df2011-01-26 19:44:55 +0000423 /// FindAsmOperand - Find the AsmOperand with the specified name and
424 /// suboperand index.
425 int FindAsmOperand(StringRef N, int SubOpIdx) const {
426 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
427 if (N == AsmOperands[i].SrcOpName &&
428 SubOpIdx == AsmOperands[i].SubOpIdx)
429 return i;
430 return -1;
431 }
Bob Wilson828295b2011-01-26 21:26:19 +0000432
Bob Wilsona49c7df2011-01-26 19:44:55 +0000433 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
434 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000435 int FindAsmOperandNamed(StringRef N) const {
436 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
437 if (N == AsmOperands[i].SrcOpName)
438 return i;
439 return -1;
440 }
Bob Wilson828295b2011-01-26 21:26:19 +0000441
Chris Lattner41409852010-11-06 07:31:43 +0000442 void BuildInstructionResultOperands();
443 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000444
Chris Lattner22bc5c42010-11-01 05:06:45 +0000445 /// operator< - Compare two matchables.
446 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000447 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000448 if (Mnemonic != RHS.Mnemonic)
449 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000450
Chris Lattner3116fef2010-11-02 01:03:43 +0000451 if (AsmOperands.size() != RHS.AsmOperands.size())
452 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000453
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000454 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000455 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000456 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
457 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000458 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000459 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000460 return false;
461 }
462
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000463 return false;
464 }
465
Bob Wilson1f64ac42011-01-26 21:26:21 +0000466 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000467 /// ambiguously match the same set of operands as \arg RHS (without being a
468 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000469 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000470 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000471 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000472 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000473
Daniel Dunbar2b544812009-08-09 06:05:33 +0000474 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000475 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000476 return false;
477
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000478 // Otherwise, make sure the ordering of the two instructions is unambiguous
479 // by checking that either (a) a token or operand kind discriminates them,
480 // or (b) the ordering among equivalent kinds is consistent.
481
Daniel Dunbar2b544812009-08-09 06:05:33 +0000482 // Tokens and operand kinds are unambiguous (assuming a correct target
483 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000484 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
485 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
486 AsmOperands[i].Class->Kind == ClassInfo::Token)
487 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
488 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000489 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000490
Daniel Dunbar2b544812009-08-09 06:05:33 +0000491 // Otherwise, this operand could commute if all operands are equivalent, or
492 // there is a pair of operands that compare less than and a pair that
493 // compare greater than.
494 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000495 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
496 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000497 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000498 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000499 HasGT = true;
500 }
501
502 return !(HasLT ^ HasGT);
503 }
504
Daniel Dunbar20927f22009-08-07 08:26:05 +0000505 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000506
Chris Lattnerd19ec052010-11-02 17:30:52 +0000507private:
508 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000509};
510
Daniel Dunbar54074b52010-07-19 05:44:09 +0000511/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
512/// feature which participates in instruction matching.
513struct SubtargetFeatureInfo {
514 /// \brief The predicate record for this feature.
515 Record *TheDef;
516
517 /// \brief An unique index assigned to represent this feature.
518 unsigned Index;
519
Chris Lattner0aed1e72010-10-30 20:07:57 +0000520 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000521
Daniel Dunbar54074b52010-07-19 05:44:09 +0000522 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000523 std::string getEnumName() const {
524 return "Feature_" + TheDef->getName();
525 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000526};
527
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000528struct OperandMatchEntry {
529 unsigned OperandMask;
530 MatchableInfo* MI;
531 ClassInfo *CI;
532
533 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
534 unsigned opMask) {
535 OperandMatchEntry X;
536 X.OperandMask = opMask;
537 X.CI = ci;
538 X.MI = mi;
539 return X;
540 }
541};
542
543
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000544class AsmMatcherInfo {
545public:
Chris Lattner67db8832010-12-13 00:23:57 +0000546 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000547 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000548
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000549 /// The tablegen AsmParser record.
550 Record *AsmParser;
551
Chris Lattner02bcbc92010-11-01 01:37:30 +0000552 /// Target - The target information.
553 CodeGenTarget &Target;
554
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000555 /// The AsmParser "RegisterPrefix" value.
556 std::string RegisterPrefix;
557
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000558 /// The classes which are needed for matching.
559 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000560
Chris Lattner22bc5c42010-11-01 05:06:45 +0000561 /// The information on the matchables to match.
562 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000563
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000564 /// Info for custom matching operands by user defined methods.
565 std::vector<OperandMatchEntry> OperandMatchInfo;
566
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000567 /// Map of Register records to their class information.
568 std::map<Record*, ClassInfo*> RegisterClasses;
569
Daniel Dunbar54074b52010-07-19 05:44:09 +0000570 /// Map of Predicate records to their subtarget information.
571 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000572
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000573private:
574 /// Map of token to class information which has already been constructed.
575 std::map<std::string, ClassInfo*> TokenClasses;
576
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000577 /// Map of RegisterClass records to their class information.
578 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000579
Daniel Dunbar338825c2009-08-10 18:41:10 +0000580 /// Map of AsmOperandClass records to their class information.
581 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000582
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000583private:
584 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000585 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000586
587 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000588 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
Jim Grosbach48c1f842011-10-28 22:32:53 +0000589 int SubOpIdx);
590 ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000591
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000592 /// BuildRegisterClasses - Build the ClassInfo* instances for register
593 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000594 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000595
596 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
597 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000598 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000599
Bob Wilsona49c7df2011-01-26 19:44:55 +0000600 void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
601 unsigned AsmOpIdx);
602 void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000603 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000604
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000605public:
Bob Wilson828295b2011-01-26 21:26:19 +0000606 AsmMatcherInfo(Record *AsmParser,
607 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000608 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000609
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000610 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000611 void BuildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000612
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000613 /// BuildOperandMatchInfo - Build the necessary information to handle user
614 /// defined operand parsing methods.
615 void BuildOperandMatchInfo();
616
Chris Lattner6fa152c2010-10-30 20:15:02 +0000617 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
618 /// given operand.
619 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
620 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
621 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
622 SubtargetFeatures.find(Def);
623 return I == SubtargetFeatures.end() ? 0 : I->second;
624 }
Chris Lattner67db8832010-12-13 00:23:57 +0000625
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000626 RecordKeeper &getRecords() const {
627 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000628 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000629};
630
Daniel Dunbar20927f22009-08-07 08:26:05 +0000631}
632
Chris Lattner22bc5c42010-11-01 05:06:45 +0000633void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000634 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000635
Chris Lattner3116fef2010-11-02 01:03:43 +0000636 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000637 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000638 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000639 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000640 }
641}
642
Chris Lattner22bc5c42010-11-01 05:06:45 +0000643void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
644 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000645 // TODO: Eventually support asmparser for Variant != 0.
646 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
Bob Wilson828295b2011-01-26 21:26:19 +0000647
Chris Lattnerd19ec052010-11-02 17:30:52 +0000648 TokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000649
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000650 // Compute the require features.
651 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
652 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
653 if (SubtargetFeatureInfo *Feature =
654 Info.getSubtargetFeature(Predicates[i]))
655 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000656
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000657 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000658 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
659 if (Record *Reg = getSingletonRegisterForAsmOperand(i, Info))
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000660 SingletonRegisters.insert(Reg);
661 }
662}
663
Chris Lattnerd19ec052010-11-02 17:30:52 +0000664/// TokenizeAsmString - Tokenize a simplified assembly string.
665void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
666 StringRef String = AsmString;
667 unsigned Prev = 0;
668 bool InTok = true;
669 for (unsigned i = 0, e = String.size(); i != e; ++i) {
670 switch (String[i]) {
671 case '[':
672 case ']':
673 case '*':
674 case '!':
675 case ' ':
676 case '\t':
677 case ',':
678 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000679 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000680 InTok = false;
681 }
682 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000683 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000684 Prev = i + 1;
685 break;
686
687 case '\\':
688 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000689 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000690 InTok = false;
691 }
692 ++i;
693 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000694 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000695 Prev = i + 1;
696 break;
697
698 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000699 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000700 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000701 InTok = false;
702 }
Bob Wilson828295b2011-01-26 21:26:19 +0000703
Chris Lattner7ad31472010-11-06 22:06:03 +0000704 // If this isn't "${", treat like a normal token.
705 if (i + 1 == String.size() || String[i + 1] != '{') {
706 Prev = i;
707 break;
708 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000709
710 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
711 assert(End != String.end() && "Missing brace in operand reference!");
712 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000713 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000714 Prev = EndPos + 1;
715 i = EndPos;
716 break;
717 }
718
719 case '.':
720 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000721 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000722 Prev = i;
723 InTok = true;
724 break;
725
726 default:
727 InTok = true;
728 }
729 }
730 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000731 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000732
Chris Lattnerd19ec052010-11-02 17:30:52 +0000733 // The first token of the instruction is the mnemonic, which must be a
734 // simple string, not a $foo variable or a singleton register.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000735 if (AsmOperands.empty())
736 throw TGError(TheDef->getLoc(),
737 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000738 Mnemonic = AsmOperands[0].Token;
739 if (Mnemonic[0] == '$' || getSingletonRegisterForAsmOperand(0, Info))
740 throw TGError(TheDef->getLoc(),
741 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000742
Chris Lattnerd19ec052010-11-02 17:30:52 +0000743 // Remove the first operand, it is tracked in the mnemonic field.
744 AsmOperands.erase(AsmOperands.begin());
745}
746
Chris Lattner22bc5c42010-11-01 05:06:45 +0000747bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
748 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000749 if (AsmString.empty())
750 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000751
Chris Lattner22bc5c42010-11-01 05:06:45 +0000752 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000753 // isCodeGenOnly if they are pseudo instructions.
754 if (AsmString.find('\n') != std::string::npos)
755 throw TGError(TheDef->getLoc(),
756 "multiline instruction is not valid for the asmparser, "
757 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000758
Chris Lattner4164f6b2010-11-01 04:44:29 +0000759 // Remove comments from the asm string. We know that the asmstring only
760 // has one line.
761 if (!CommentDelimiter.empty() &&
762 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
763 throw TGError(TheDef->getLoc(),
764 "asmstring for instruction has comment character in it, "
765 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000766
Chris Lattner22bc5c42010-11-01 05:06:45 +0000767 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000768 // handle, the target should be refactored to use operands instead of
769 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000770 //
771 // Also, check for instructions which reference the operand multiple times;
772 // this implies a constraint we would not honor.
773 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000774 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
775 StringRef Tok = AsmOperands[i].Token;
776 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000777 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000778 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000779 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000780
Chris Lattner22bc5c42010-11-01 05:06:45 +0000781 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000782 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000783 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000784 if (!Hack)
785 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000786 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000787 "' can never be matched!");
788 // FIXME: Should reject these. The ARM backend hits this with $lane in a
789 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000790 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000791 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000792 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000793 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000794 });
795 return false;
796 }
797 }
Bob Wilson828295b2011-01-26 21:26:19 +0000798
Chris Lattner5bc93872010-11-01 04:34:44 +0000799 return true;
800}
801
Chris Lattnerd19ec052010-11-02 17:30:52 +0000802/// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner02bcbc92010-11-01 01:37:30 +0000803/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000804Record *MatchableInfo::
Chris Lattnerd19ec052010-11-02 17:30:52 +0000805getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{
806 StringRef Tok = AsmOperands[i].Token;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000807 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000808 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000809
Chris Lattner02bcbc92010-11-01 01:37:30 +0000810 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000811 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
812 return Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000813
Chris Lattner1de88232010-11-01 01:47:07 +0000814 // If there is no register prefix (i.e. "%" in "%eax"), then this may
815 // be some random non-register token, just ignore it.
816 if (Info.RegisterPrefix.empty())
817 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000818
Chris Lattnerec6f0962010-11-02 18:10:06 +0000819 // Otherwise, we have something invalid prefixed with the register prefix,
820 // such as %foo.
Chris Lattner1de88232010-11-01 01:47:07 +0000821 std::string Err = "unable to find register for '" + RegName.str() +
822 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000823 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000824}
825
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000826static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000827 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000828
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000829 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
830 switch (*it) {
831 case '*': Res += "_STAR_"; break;
832 case '%': Res += "_PCT_"; break;
833 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000834 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000835 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000836 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000837 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000838 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000839 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000840 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000841 }
842 }
843
844 return Res;
845}
846
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000847ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000848 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000849
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000850 if (!Entry) {
851 Entry = new ClassInfo();
852 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000853 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000854 Entry->Name = "MCK_" + getEnumNameForToken(Token);
855 Entry->ValueName = Token;
856 Entry->PredicateMethod = "<invalid>";
857 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000858 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000859 Classes.push_back(Entry);
860 }
861
862 return Entry;
863}
864
865ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000866AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
867 int SubOpIdx) {
868 Record *Rec = OI.Rec;
869 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000870 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +0000871 return getOperandClass(Rec, SubOpIdx);
872}
Bob Wilsona49c7df2011-01-26 19:44:55 +0000873
Jim Grosbach48c1f842011-10-28 22:32:53 +0000874ClassInfo *
875AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000876 if (Rec->isSubClassOf("RegisterOperand")) {
877 // RegisterOperand may have an associated ParserMatchClass. If it does,
878 // use it, else just fall back to the underlying register class.
879 const RecordVal *R = Rec->getValue("ParserMatchClass");
880 if (R == 0 || R->getValue() == 0)
881 throw "Record `" + Rec->getName() +
882 "' does not have a ParserMatchClass!\n";
883
David Greene05bce0b2011-07-29 22:43:06 +0000884 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000885 Record *MatchClass = DI->getDef();
886 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
887 return CI;
888 }
889
890 // No custom match class. Just use the register class.
891 Record *ClassRec = Rec->getValueAsDef("RegClass");
892 if (!ClassRec)
893 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
894 "' has no associated register class!\n");
895 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
896 return CI;
897 throw TGError(Rec->getLoc(), "register class has no class info!");
898 }
899
900
Bob Wilsona49c7df2011-01-26 19:44:55 +0000901 if (Rec->isSubClassOf("RegisterClass")) {
902 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000903 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000904 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000905 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000906
Bob Wilsona49c7df2011-01-26 19:44:55 +0000907 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
908 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000909 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
910 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000911
Bob Wilsona49c7df2011-01-26 19:44:55 +0000912 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000913}
914
Chris Lattner1de88232010-11-01 01:47:07 +0000915void AsmMatcherInfo::
916BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000917 const std::vector<CodeGenRegister*> &Registers =
918 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000919 ArrayRef<CodeGenRegisterClass*> RegClassList =
920 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000921
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000922 // The register sets used for matching.
923 std::set< std::set<Record*> > RegisterSets;
924
Jim Grosbacha7c78222010-10-29 22:13:48 +0000925 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000926 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +0000927 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +0000928 RegisterSets.insert(std::set<Record*>(
929 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000930
931 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000932 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
933 ie = SingletonRegisters.end(); it != ie; ++it) {
934 Record *Rec = *it;
935 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
936 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000937
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000938 // Introduce derived sets where necessary (when a register does not determine
939 // a unique register set class), and build the mapping of registers to the set
940 // they should classify to.
941 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000942 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000943 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000944 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000945 // Compute the intersection of all sets containing this register.
946 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000947
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000948 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
949 ie = RegisterSets.end(); it != ie; ++it) {
950 if (!it->count(CGR.TheDef))
951 continue;
952
953 if (ContainingSet.empty()) {
954 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000955 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000956 }
Bob Wilson828295b2011-01-26 21:26:19 +0000957
Chris Lattnerec6f0962010-11-02 18:10:06 +0000958 std::set<Record*> Tmp;
959 std::swap(Tmp, ContainingSet);
960 std::insert_iterator< std::set<Record*> > II(ContainingSet,
961 ContainingSet.begin());
962 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000963 }
964
965 if (!ContainingSet.empty()) {
966 RegisterSets.insert(ContainingSet);
967 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
968 }
969 }
970
971 // Construct the register classes.
972 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
973 unsigned Index = 0;
974 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
975 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
976 ClassInfo *CI = new ClassInfo();
977 CI->Kind = ClassInfo::RegisterClass0 + Index;
978 CI->ClassName = "Reg" + utostr(Index);
979 CI->Name = "MCK_Reg" + utostr(Index);
980 CI->ValueName = "";
981 CI->PredicateMethod = ""; // unused
982 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000983 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000984 Classes.push_back(CI);
985 RegisterSetClasses.insert(std::make_pair(*it, CI));
986 }
987
988 // Find the superclasses; we could compute only the subgroup lattice edges,
989 // but there isn't really a point.
990 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
991 ie = RegisterSets.end(); it != ie; ++it) {
992 ClassInfo *CI = RegisterSetClasses[*it];
993 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
994 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000995 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000996 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
997 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
998 }
999
1000 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001001 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001002 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001003 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001004 // Def will be NULL for non-user defined register classes.
1005 Record *Def = RC.getDef();
1006 if (!Def)
1007 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001008 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1009 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001010 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001011 CI->ClassName = RC.getName();
1012 CI->Name = "MCK_" + RC.getName();
1013 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001014 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001015 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001016
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001017 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001018 }
1019
1020 // Populate the map for individual registers.
1021 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1022 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001023 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001024
1025 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001026 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1027 ie = SingletonRegisters.end(); it != ie; ++it) {
1028 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001029 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001030 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001031
Chris Lattner1de88232010-11-01 01:47:07 +00001032 if (CI->ValueName.empty()) {
1033 CI->ClassName = Rec->getName();
1034 CI->Name = "MCK_" + Rec->getName();
1035 CI->ValueName = Rec->getName();
1036 } else
1037 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001038 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001039}
1040
Chris Lattner02bcbc92010-11-01 01:37:30 +00001041void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001042 std::vector<Record*> AsmOperands =
1043 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001044
1045 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001046 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001047 ie = AsmOperands.end(); it != ie; ++it)
1048 AsmOperandClasses[*it] = new ClassInfo();
1049
Daniel Dunbar338825c2009-08-10 18:41:10 +00001050 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001051 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001052 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001053 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001054 CI->Kind = ClassInfo::UserClass0 + Index;
1055
David Greene05bce0b2011-07-29 22:43:06 +00001056 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001057 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001058 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001059 if (!DI) {
1060 PrintError((*it)->getLoc(), "Invalid super class reference!");
1061 continue;
1062 }
1063
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001064 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1065 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001066 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001067 else
1068 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001069 }
1070 CI->ClassName = (*it)->getValueAsString("Name");
1071 CI->Name = "MCK_" + CI->ClassName;
1072 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001073
1074 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001075 Init *PMName = (*it)->getValueInit("PredicateMethod");
1076 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001077 CI->PredicateMethod = SI->getValue();
1078 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001079 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001080 "Unexpected PredicateMethod field!");
1081 CI->PredicateMethod = "is" + CI->ClassName;
1082 }
1083
1084 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001085 Init *RMName = (*it)->getValueInit("RenderMethod");
1086 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001087 CI->RenderMethod = SI->getValue();
1088 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001089 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001090 "Unexpected RenderMethod field!");
1091 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1092 }
1093
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001094 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001095 Init *PRMName = (*it)->getValueInit("ParserMethod");
1096 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001097 CI->ParserMethod = SI->getValue();
1098
Daniel Dunbar338825c2009-08-10 18:41:10 +00001099 AsmOperandClasses[*it] = CI;
1100 Classes.push_back(CI);
1101 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001102}
1103
Bob Wilson828295b2011-01-26 21:26:19 +00001104AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1105 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001106 RecordKeeper &records)
Chris Lattner67db8832010-12-13 00:23:57 +00001107 : Records(records), AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001108 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001109}
1110
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001111/// BuildOperandMatchInfo - Build the necessary information to handle user
1112/// defined operand parsing methods.
1113void AsmMatcherInfo::BuildOperandMatchInfo() {
1114
1115 /// Map containing a mask with all operands indicies that can be found for
1116 /// that class inside a instruction.
1117 std::map<ClassInfo*, unsigned> OpClassMask;
1118
1119 for (std::vector<MatchableInfo*>::const_iterator it =
1120 Matchables.begin(), ie = Matchables.end();
1121 it != ie; ++it) {
1122 MatchableInfo &II = **it;
1123 OpClassMask.clear();
1124
1125 // Keep track of all operands of this instructions which belong to the
1126 // same class.
1127 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1128 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1129 if (Op.Class->ParserMethod.empty())
1130 continue;
1131 unsigned &OperandMask = OpClassMask[Op.Class];
1132 OperandMask |= (1 << i);
1133 }
1134
1135 // Generate operand match info for each mnemonic/operand class pair.
1136 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1137 iie = OpClassMask.end(); iit != iie; ++iit) {
1138 unsigned OpMask = iit->second;
1139 ClassInfo *CI = iit->first;
1140 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1141 }
1142 }
1143}
1144
Chris Lattner02bcbc92010-11-01 01:37:30 +00001145void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001146 // Build information about all of the AssemblerPredicates.
1147 std::vector<Record*> AllPredicates =
1148 Records.getAllDerivedDefinitions("Predicate");
1149 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1150 Record *Pred = AllPredicates[i];
1151 // Ignore predicates that are not intended for the assembler.
1152 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1153 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001154
Chris Lattner4164f6b2010-11-01 04:44:29 +00001155 if (Pred->getName().empty())
1156 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001157
Chris Lattner0aed1e72010-10-30 20:07:57 +00001158 unsigned FeatureNo = SubtargetFeatures.size();
1159 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1160 assert(FeatureNo < 32 && "Too many subtarget features!");
1161 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001162
Eli Friedman60435482011-07-08 20:07:05 +00001163 std::string CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
Bob Wilson828295b2011-01-26 21:26:19 +00001164
Chris Lattner39ee0362010-10-31 19:10:56 +00001165 // Parse the instructions; we need to do this first so that we can gather the
1166 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001167 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +00001168 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1169 E = Target.inst_end(); I != E; ++I) {
1170 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001171
Chris Lattner39ee0362010-10-31 19:10:56 +00001172 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1173 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +00001174 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001175 continue;
1176
Chris Lattner5bc93872010-11-01 04:34:44 +00001177 // Ignore "codegen only" instructions.
1178 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1179 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001180
Chris Lattner1d13bda2010-11-04 00:43:46 +00001181 // Validate the operand list to ensure we can handle this instruction.
1182 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1183 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001184
Chris Lattner1d13bda2010-11-04 00:43:46 +00001185 // Validate tied operands.
1186 if (OI.getTiedRegister() != -1) {
Bob Wilson828295b2011-01-26 21:26:19 +00001187 // If we have a tied operand that consists of multiple MCOperands,
1188 // reject it. We reject aliases and ignore instructions for now.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001189 if (OI.MINumOperands != 1) {
1190 // FIXME: Should reject these. The ARM backend hits this with $lane
1191 // in a bunch of instructions. It is unclear what the right answer is.
1192 DEBUG({
1193 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1194 << "ignoring instruction with multi-operand tied operand '"
1195 << OI.Name << "'\n";
1196 });
1197 continue;
1198 }
1199 }
1200 }
Bob Wilson828295b2011-01-26 21:26:19 +00001201
Chris Lattner22bc5c42010-11-01 05:06:45 +00001202 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001203
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001204 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001205
Chris Lattner4d43d0f2010-11-01 01:07:14 +00001206 // Ignore instructions which shouldn't be matched and diagnose invalid
1207 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001208 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +00001209 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001210
Chris Lattner5bc93872010-11-01 04:34:44 +00001211 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1212 //
1213 // FIXME: This is a total hack.
Chris Lattner5abd1eb2010-11-06 06:43:11 +00001214 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1215 StringRef(II->TheDef->getName()).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001216 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001217
Chris Lattner22bc5c42010-11-01 05:06:45 +00001218 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001219 }
Bob Wilson828295b2011-01-26 21:26:19 +00001220
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001221 // Parse all of the InstAlias definitions and stick them in the list of
1222 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001223 std::vector<Record*> AllInstAliases =
1224 Records.getAllDerivedDefinitions("InstAlias");
1225 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
Chris Lattner225549f2010-11-06 06:39:47 +00001226 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001227
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001228 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1229 // filter the set of instruction aliases we consider, based on the target
1230 // instruction.
1231 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1232 MatchPrefix))
1233 continue;
1234
Chris Lattner22bc5c42010-11-01 05:06:45 +00001235 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Bob Wilson828295b2011-01-26 21:26:19 +00001236
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001237 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001238
Chris Lattner22bc5c42010-11-01 05:06:45 +00001239 // Validate the alias definitions.
1240 II->Validate(CommentDelimiter, false);
Bob Wilson828295b2011-01-26 21:26:19 +00001241
Chris Lattnerb501d4f2010-11-01 05:34:34 +00001242 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001243 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001244
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001245 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001246 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001247
1248 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001249 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001250
Chris Lattner0bb780c2010-11-04 00:57:06 +00001251 // Build the information about matchables, now that we have fully formed
1252 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001253 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1254 ie = Matchables.end(); it != ie; ++it) {
1255 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001256
Chris Lattnere206fcf2010-09-06 21:01:37 +00001257 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001258 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1259 // don't precompute the loop bound.
1260 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001261 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001262 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001263
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001264 // Check for singleton registers.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001265 if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) {
1266 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001267 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1268 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001269 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001270 }
1271
Daniel Dunbar20927f22009-08-07 08:26:05 +00001272 // Check for simple tokens.
1273 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001274 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001275 continue;
1276 }
1277
Chris Lattner7ad31472010-11-06 22:06:03 +00001278 if (Token.size() > 1 && isdigit(Token[1])) {
1279 Op.Class = getTokenClass(Token);
1280 continue;
1281 }
Bob Wilson828295b2011-01-26 21:26:19 +00001282
Chris Lattnerc07bd402010-11-04 02:11:18 +00001283 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001284 StringRef OperandName;
1285 if (Token[1] == '{')
1286 OperandName = Token.substr(2, Token.size() - 3);
1287 else
1288 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001289
Chris Lattnerc07bd402010-11-04 02:11:18 +00001290 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001291 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001292 else
Chris Lattner225549f2010-11-06 06:39:47 +00001293 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001294 }
Bob Wilson828295b2011-01-26 21:26:19 +00001295
Chris Lattner41409852010-11-06 07:31:43 +00001296 if (II->DefRec.is<const CodeGenInstruction*>())
1297 II->BuildInstructionResultOperands();
1298 else
1299 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001300 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001301
Jim Grosbacha66512e2011-12-06 23:43:54 +00001302 // Process token alias definitions and set up the associated superclass
1303 // information.
1304 std::vector<Record*> AllTokenAliases =
1305 Records.getAllDerivedDefinitions("TokenAlias");
1306 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1307 Record *Rec = AllTokenAliases[i];
1308 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1309 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
1310 FromClass->SuperClasses.push_back(ToClass);
1311 }
1312
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001313 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001314 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001315}
1316
Chris Lattner0bb780c2010-11-04 00:57:06 +00001317/// BuildInstructionOperandReference - The specified operand is a reference to a
1318/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1319void AsmMatcherInfo::
1320BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001321 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001322 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001323 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1324 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001325 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001326
Chris Lattner662e5a32010-11-06 07:14:44 +00001327 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001328 unsigned Idx;
1329 if (!Operands.hasOperandNamed(OperandName, Idx))
1330 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1331 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001332
Bob Wilsona49c7df2011-01-26 19:44:55 +00001333 // If the instruction operand has multiple suboperands, but the parser
1334 // match class for the asm operand is still the default "ImmAsmOperand",
1335 // then handle each suboperand separately.
1336 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1337 Record *Rec = Operands[Idx].Rec;
1338 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1339 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1340 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1341 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1342 StringRef Token = Op->Token; // save this in case Op gets moved
1343 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1344 MatchableInfo::AsmOperand NewAsmOp(Token);
1345 NewAsmOp.SubOpIdx = SI;
1346 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1347 }
1348 // Replace Op with first suboperand.
1349 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1350 Op->SubOpIdx = 0;
1351 }
1352 }
1353
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001354 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001355 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001356
1357 // If the named operand is tied, canonicalize it to the untied operand.
1358 // For example, something like:
1359 // (outs GPR:$dst), (ins GPR:$src)
1360 // with an asmstring of
1361 // "inc $src"
1362 // we want to canonicalize to:
1363 // "inc $dst"
1364 // so that we know how to provide the $dst operand when filling in the result.
1365 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001366 if (OITied != -1) {
1367 // The tied operand index is an MIOperand index, find the operand that
1368 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001369 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1370 OperandName = Operands[Idx.first].Name;
1371 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001372 }
Bob Wilson828295b2011-01-26 21:26:19 +00001373
Bob Wilsona49c7df2011-01-26 19:44:55 +00001374 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001375}
1376
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001377/// BuildAliasOperandReference - When parsing an operand reference out of the
1378/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1379/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001380void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1381 StringRef OperandName,
1382 MatchableInfo::AsmOperand &Op) {
1383 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001384
Chris Lattnerc07bd402010-11-04 02:11:18 +00001385 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001386 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001387 if (CGA.ResultOperands[i].isRecord() &&
1388 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001389 // It's safe to go with the first one we find, because CodeGenInstAlias
1390 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001391 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001392 // Use the match class from the Alias definition, not the
1393 // destination instruction, as we may have an immediate that's
1394 // being munged by the match class.
1395 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001396 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001397 Op.SrcOpName = OperandName;
1398 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001399 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001400
1401 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1402 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001403}
1404
Chris Lattner41409852010-11-06 07:31:43 +00001405void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001406 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001407
Chris Lattner662e5a32010-11-06 07:14:44 +00001408 // Loop over all operands of the result instruction, determining how to
1409 // populate them.
1410 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1411 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001412
1413 // If this is a tied operand, just copy from the previously handled operand.
1414 int TiedOp = OpInfo.getTiedRegister();
1415 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001416 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001417 continue;
1418 }
Bob Wilson828295b2011-01-26 21:26:19 +00001419
Bob Wilsona49c7df2011-01-26 19:44:55 +00001420 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001421 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001422 if (OpInfo.Name.empty() || SrcOperand == -1)
1423 throw TGError(TheDef->getLoc(), "Instruction '" +
1424 TheDef->getName() + "' has operand '" + OpInfo.Name +
1425 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001426
Bob Wilsona49c7df2011-01-26 19:44:55 +00001427 // Check if the one AsmOperand populates the entire operand.
1428 unsigned NumOperands = OpInfo.MINumOperands;
1429 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1430 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001431 continue;
1432 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001433
1434 // Add a separate ResOperand for each suboperand.
1435 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1436 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1437 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1438 "unexpected AsmOperands for suboperands");
1439 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1440 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001441 }
1442}
1443
Chris Lattner41409852010-11-06 07:31:43 +00001444void MatchableInfo::BuildAliasResultOperands() {
1445 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1446 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001447
Chris Lattner41409852010-11-06 07:31:43 +00001448 // Loop over all operands of the result instruction, determining how to
1449 // populate them.
1450 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001451 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001452 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001453 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001454
Chris Lattner41409852010-11-06 07:31:43 +00001455 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001456 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001457 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001458 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001459 continue;
1460 }
1461
Bob Wilsona49c7df2011-01-26 19:44:55 +00001462 // Handle all the suboperands for this operand.
1463 const std::string &OpName = OpInfo->Name;
1464 for ( ; AliasOpNo < LastOpNo &&
1465 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1466 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1467
1468 // Find out what operand from the asmparser that this MCInst operand
1469 // comes from.
1470 switch (CGA.ResultOperands[AliasOpNo].Kind) {
1471 default: assert(0 && "unexpected InstAlias operand kind");
1472 case CodeGenInstAlias::ResultOperand::K_Record: {
1473 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1474 int SrcOperand = FindAsmOperand(Name, SubIdx);
1475 if (SrcOperand == -1)
1476 throw TGError(TheDef->getLoc(), "Instruction '" +
1477 TheDef->getName() + "' has operand '" + OpName +
1478 "' that doesn't appear in asm string!");
1479 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1480 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1481 NumOperands));
1482 break;
1483 }
1484 case CodeGenInstAlias::ResultOperand::K_Imm: {
1485 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1486 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1487 break;
1488 }
1489 case CodeGenInstAlias::ResultOperand::K_Reg: {
1490 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1491 ResOperands.push_back(ResOperand::getRegOp(Reg));
1492 break;
1493 }
1494 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001495 }
Chris Lattner41409852010-11-06 07:31:43 +00001496 }
1497}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001498
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001499static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001500 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001501 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001502 // Write the convert function to a separate stream, so we can drop it after
1503 // the enum.
1504 std::string ConvertFnBody;
1505 raw_string_ostream CvtOS(ConvertFnBody);
1506
Daniel Dunbar20927f22009-08-07 08:26:05 +00001507 // Function we have already generated.
1508 std::set<std::string> GeneratedFns;
1509
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001510 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001511 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1512 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001513 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001514 << " const SmallVectorImpl<MCParsedAsmOperand*"
1515 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001516 CvtOS << " Inst.setOpcode(Opcode);\n";
1517 CvtOS << " switch (Kind) {\n";
1518 CvtOS << " default:\n";
1519
1520 // Start the enum, which we will generate inline.
1521
Chris Lattnerd51257a2010-11-02 23:18:43 +00001522 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001523 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001524
Chris Lattner98986712010-01-14 22:21:20 +00001525 // TargetOperandClass - This is the target's operand class, like X86Operand.
1526 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001527
Chris Lattner22bc5c42010-11-01 05:06:45 +00001528 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001529 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001530 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001531
Daniel Dunbarcf120672011-02-04 17:12:15 +00001532 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001533 std::string AsmMatchConverter =
1534 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001535 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001536 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001537 II.ConversionFnKind = Signature;
1538
1539 // Check if we have already generated this signature.
1540 if (!GeneratedFns.insert(Signature).second)
1541 continue;
1542
1543 // If not, emit it now. Add to the enum list.
1544 OS << " " << Signature << ",\n";
1545
1546 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001547 CvtOS << " return " << AsmMatchConverter
1548 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001549 continue;
1550 }
1551
Daniel Dunbar20927f22009-08-07 08:26:05 +00001552 // Build the conversion function signature.
1553 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001554 std::string CaseBody;
1555 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001556
Chris Lattnerdda855d2010-11-02 21:49:44 +00001557 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001558 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1559 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001560
Chris Lattner1d13bda2010-11-04 00:43:46 +00001561 // Generate code to populate each result operand.
1562 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001563 case MatchableInfo::ResOperand::RenderAsmOperand: {
1564 // This comes from something we parsed.
1565 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001566
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001567 // Registers are always converted the same, don't duplicate the
1568 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001569 Signature += "__";
1570 if (Op.Class->isRegisterClass())
1571 Signature += "Reg";
1572 else
1573 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001574 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001575 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001576
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001577 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001578 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001579 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001580 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001581 }
Bob Wilson828295b2011-01-26 21:26:19 +00001582
Chris Lattner1d13bda2010-11-04 00:43:46 +00001583 case MatchableInfo::ResOperand::TiedOperand: {
1584 // If this operand is tied to a previous one, just copy the MCInst
1585 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001586 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001587 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001588 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001589 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1590 Signature += "__Tie" + utostr(TiedOp);
1591 break;
1592 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001593 case MatchableInfo::ResOperand::ImmOperand: {
1594 int64_t Val = OpInfo.ImmVal;
1595 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1596 Signature += "__imm" + itostr(Val);
1597 break;
1598 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001599 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001600 if (OpInfo.Register == 0) {
1601 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1602 Signature += "__reg0";
1603 } else {
1604 std::string N = getQualifiedName(OpInfo.Register);
1605 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1606 Signature += "__reg" + OpInfo.Register->getName();
1607 }
Bob Wilson828295b2011-01-26 21:26:19 +00001608 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001609 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001610 }
Bob Wilson828295b2011-01-26 21:26:19 +00001611
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001612 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001613
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001614 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001615 if (!GeneratedFns.insert(Signature).second)
1616 continue;
1617
Chris Lattnerdda855d2010-11-02 21:49:44 +00001618 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001619 OS << " " << Signature << ",\n";
1620
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001621 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001622 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001623 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001624 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001625
1626 // Finish the convert function.
1627
1628 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001629 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001630 CvtOS << "}\n\n";
1631
1632 // Finish the enum, and drop the convert function after it.
1633
1634 OS << " NumConversionVariants\n";
1635 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001636
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001637 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001638}
1639
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001640/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1641static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1642 std::vector<ClassInfo*> &Infos,
1643 raw_ostream &OS) {
1644 OS << "namespace {\n\n";
1645
1646 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1647 << "/// instruction matching.\n";
1648 OS << "enum MatchClassKind {\n";
1649 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001650 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001651 ie = Infos.end(); it != ie; ++it) {
1652 ClassInfo &CI = **it;
1653 OS << " " << CI.Name << ", // ";
1654 if (CI.Kind == ClassInfo::Token) {
1655 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001656 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001657 if (!CI.ValueName.empty())
1658 OS << "register class '" << CI.ValueName << "'\n";
1659 else
1660 OS << "derived register class\n";
1661 } else {
1662 OS << "user defined class '" << CI.ValueName << "'\n";
1663 }
1664 }
1665 OS << " NumMatchClassKinds\n";
1666 OS << "};\n\n";
1667
1668 OS << "}\n\n";
1669}
1670
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001671/// EmitValidateOperandClass - Emit the function to validate an operand class.
1672static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1673 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001674 OS << "static bool validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001675 << "MatchClassKind Kind) {\n";
1676 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001677 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001678
Kevin Enderby89381832011-07-15 18:30:43 +00001679 // The InvalidMatchClass is not to match any operand.
1680 OS << " if (Kind == InvalidMatchClass)\n";
1681 OS << " return false;\n\n";
1682
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001683 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001684 OS << " if (Operand.isToken())\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00001685 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind);"
1686 << "\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001687
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001688 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001689 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001690 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001691 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001692 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001693 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001694 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1695 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001696 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001697 << it->first->getName() << ": OpKind = " << it->second->Name
1698 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001699 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001700 OS << " return isSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001701 OS << " }\n\n";
1702
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001703 // Check the user classes. We don't care what order since we're only
1704 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001705 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001706 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001707 ClassInfo &CI = **it;
1708
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001709 if (!CI.isUserClass())
1710 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001711
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001712 OS << " // '" << CI.ClassName << "' class\n";
1713 OS << " if (Kind == " << CI.Name
1714 << " && Operand." << CI.PredicateMethod << "()) {\n";
1715 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001716 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001717 }
Bob Wilson828295b2011-01-26 21:26:19 +00001718
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001719 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001720 OS << "}\n\n";
1721}
1722
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001723/// EmitIsSubclass - Emit the subclass predicate function.
1724static void EmitIsSubclass(CodeGenTarget &Target,
1725 std::vector<ClassInfo*> &Infos,
1726 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001727 OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1728 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001729 OS << " if (A == B)\n";
1730 OS << " return true;\n\n";
1731
1732 OS << " switch (A) {\n";
1733 OS << " default:\n";
1734 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001735 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001736 ie = Infos.end(); it != ie; ++it) {
1737 ClassInfo &A = **it;
1738
Jim Grosbacha66512e2011-12-06 23:43:54 +00001739 std::vector<StringRef> SuperClasses;
1740 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
1741 ie = Infos.end(); it != ie; ++it) {
1742 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001743
Jim Grosbacha66512e2011-12-06 23:43:54 +00001744 if (&A != &B && A.isSubsetOf(B))
1745 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001746 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00001747
1748 if (SuperClasses.empty())
1749 continue;
1750
1751 OS << "\n case " << A.Name << ":\n";
1752
1753 if (SuperClasses.size() == 1) {
1754 OS << " return B == " << SuperClasses.back() << ";\n";
1755 continue;
1756 }
1757
1758 OS << " switch (B) {\n";
1759 OS << " default: return false;\n";
1760 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1761 OS << " case " << SuperClasses[i] << ": return true;\n";
1762 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001763 }
1764 OS << " }\n";
1765 OS << "}\n\n";
1766}
1767
Daniel Dunbar245f0582009-08-08 21:22:41 +00001768/// EmitMatchTokenString - Emit the function to match a token string to the
1769/// appropriate match class value.
1770static void EmitMatchTokenString(CodeGenTarget &Target,
1771 std::vector<ClassInfo*> &Infos,
1772 raw_ostream &OS) {
1773 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001774 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001775 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001776 ie = Infos.end(); it != ie; ++it) {
1777 ClassInfo &CI = **it;
1778
1779 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001780 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1781 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001782 }
1783
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001784 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001785
Chris Lattner5845e5c2010-09-06 02:01:51 +00001786 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001787
1788 OS << " return InvalidMatchClass;\n";
1789 OS << "}\n\n";
1790}
Chris Lattner70add882009-08-08 20:02:57 +00001791
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001792/// EmitMatchRegisterName - Emit the function to match a string to the target
1793/// specific register enum.
1794static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1795 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001796 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001797 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001798 const std::vector<CodeGenRegister*> &Regs =
1799 Target.getRegBank().getRegisters();
1800 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1801 const CodeGenRegister *Reg = Regs[i];
1802 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001803 continue;
1804
Chris Lattner5845e5c2010-09-06 02:01:51 +00001805 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001806 Reg->TheDef->getValueAsString("AsmName"),
1807 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001808 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001809
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001810 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001811
Chris Lattner5845e5c2010-09-06 02:01:51 +00001812 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001813
Daniel Dunbar245f0582009-08-08 21:22:41 +00001814 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001815 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001816}
Daniel Dunbara027d222009-07-31 02:32:59 +00001817
Daniel Dunbar54074b52010-07-19 05:44:09 +00001818/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1819/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001820static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001821 raw_ostream &OS) {
1822 OS << "// Flags for subtarget features that participate in "
1823 << "instruction matching.\n";
1824 OS << "enum SubtargetFeatureFlag {\n";
1825 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1826 it = Info.SubtargetFeatures.begin(),
1827 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1828 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001829 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001830 }
1831 OS << " Feature_None = 0\n";
1832 OS << "};\n\n";
1833}
1834
1835/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1836/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001837static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001838 raw_ostream &OS) {
1839 std::string ClassName =
1840 Info.AsmParser->getValueAsString("AsmParserClassName");
1841
Chris Lattner02bcbc92010-11-01 01:37:30 +00001842 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00001843 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001844 OS << " unsigned Features = 0;\n";
1845 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1846 it = Info.SubtargetFeatures.begin(),
1847 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1848 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00001849
1850 OS << " if (";
Evan Chengfbc38d22011-07-08 18:04:22 +00001851 std::string CondStorage = SFI.TheDef->getValueAsString("AssemblerCondString");
1852 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00001853 std::pair<StringRef,StringRef> Comma = Conds.split(',');
1854 bool First = true;
1855 do {
1856 if (!First)
1857 OS << " && ";
1858
1859 bool Neg = false;
1860 StringRef Cond = Comma.first;
1861 if (Cond[0] == '!') {
1862 Neg = true;
1863 Cond = Cond.substr(1);
1864 }
1865
1866 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
1867 if (Neg)
1868 OS << " == 0";
1869 else
1870 OS << " != 0";
1871 OS << ")";
1872
1873 if (Comma.second.empty())
1874 break;
1875
1876 First = false;
1877 Comma = Comma.second.split(',');
1878 } while (true);
1879
1880 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001881 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001882 }
1883 OS << " return Features;\n";
1884 OS << "}\n\n";
1885}
1886
Chris Lattner6fa152c2010-10-30 20:15:02 +00001887static std::string GetAliasRequiredFeatures(Record *R,
1888 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001889 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001890 std::string Result;
1891 unsigned NumFeatures = 0;
1892 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001893 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001894
Chris Lattner4a74ee72010-11-01 02:09:21 +00001895 if (F == 0)
1896 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1897 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001898
Chris Lattner4a74ee72010-11-01 02:09:21 +00001899 if (NumFeatures)
1900 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001901
Chris Lattner4a74ee72010-11-01 02:09:21 +00001902 Result += F->getEnumName();
1903 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001904 }
Bob Wilson828295b2011-01-26 21:26:19 +00001905
Chris Lattner693173f2010-10-30 19:23:13 +00001906 if (NumFeatures > 1)
1907 Result = '(' + Result + ')';
1908 return Result;
1909}
1910
Chris Lattner674c1dc2010-10-30 17:36:36 +00001911/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001912/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001913static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001914 // Ignore aliases when match-prefix is set.
1915 if (!MatchPrefix.empty())
1916 return false;
1917
Chris Lattner674c1dc2010-10-30 17:36:36 +00001918 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001919 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001920 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001921
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001922 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001923 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001924
Chris Lattner4fd32c62010-10-30 18:56:12 +00001925 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1926 // iteration order of the map is stable.
1927 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001928
Chris Lattner674c1dc2010-10-30 17:36:36 +00001929 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1930 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001931 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001932 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001933
1934 // Process each alias a "from" mnemonic at a time, building the code executed
1935 // by the string remapper.
1936 std::vector<StringMatcher::StringPair> Cases;
1937 for (std::map<std::string, std::vector<Record*> >::iterator
1938 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1939 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001940 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001941
1942 // Loop through each alias and emit code that handles each case. If there
1943 // are two instructions without predicates, emit an error. If there is one,
1944 // emit it last.
1945 std::string MatchCode;
1946 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001947
Chris Lattner693173f2010-10-30 19:23:13 +00001948 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1949 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001950 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001951
Chris Lattner693173f2010-10-30 19:23:13 +00001952 // If this unconditionally matches, remember it for later and diagnose
1953 // duplicates.
1954 if (FeatureMask.empty()) {
1955 if (AliasWithNoPredicate != -1) {
1956 // We can't have two aliases from the same mnemonic with no predicate.
1957 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1958 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001959 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001960 }
Bob Wilson828295b2011-01-26 21:26:19 +00001961
Chris Lattner693173f2010-10-30 19:23:13 +00001962 AliasWithNoPredicate = i;
1963 continue;
1964 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001965 if (R->getValueAsString("ToMnemonic") == I->first)
1966 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001967
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001968 if (!MatchCode.empty())
1969 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001970 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1971 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001972 }
Bob Wilson828295b2011-01-26 21:26:19 +00001973
Chris Lattner693173f2010-10-30 19:23:13 +00001974 if (AliasWithNoPredicate != -1) {
1975 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001976 if (!MatchCode.empty())
1977 MatchCode += "else\n ";
1978 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001979 }
Bob Wilson828295b2011-01-26 21:26:19 +00001980
Chris Lattner693173f2010-10-30 19:23:13 +00001981 MatchCode += "return;";
1982
1983 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001984 }
Bob Wilson828295b2011-01-26 21:26:19 +00001985
Chris Lattner674c1dc2010-10-30 17:36:36 +00001986 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00001987 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001988
Chris Lattner7fd44892010-10-30 18:48:18 +00001989 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001990}
1991
Benjamin Krameraf482cf2011-10-17 16:18:09 +00001992static const char *getMinimalTypeForRange(uint64_t Range) {
1993 assert(Range < 0xFFFFFFFFULL && "Enum too large");
1994 if (Range > 0xFFFF)
1995 return "uint32_t";
1996 if (Range > 0xFF)
1997 return "uint16_t";
1998 return "uint8_t";
1999}
2000
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002001static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
2002 const AsmMatcherInfo &Info, StringRef ClassName) {
2003 // Emit the static custom operand parsing table;
2004 OS << "namespace {\n";
2005 OS << " struct OperandMatchEntry {\n";
2006 OS << " const char *Mnemonic;\n";
2007 OS << " unsigned OperandMask;\n";
2008 OS << " MatchClassKind Class;\n";
2009 OS << " unsigned RequiredFeatures;\n";
2010 OS << " };\n\n";
2011
2012 OS << " // Predicate for searching for an opcode.\n";
2013 OS << " struct LessOpcodeOperand {\n";
2014 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
2015 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2016 OS << " }\n";
2017 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
2018 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2019 OS << " }\n";
2020 OS << " bool operator()(const OperandMatchEntry &LHS,";
2021 OS << " const OperandMatchEntry &RHS) {\n";
2022 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2023 OS << " }\n";
2024 OS << " };\n";
2025
2026 OS << "} // end anonymous namespace.\n\n";
2027
2028 OS << "static const OperandMatchEntry OperandMatchTable["
2029 << Info.OperandMatchInfo.size() << "] = {\n";
2030
2031 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
2032 for (std::vector<OperandMatchEntry>::const_iterator it =
2033 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2034 it != ie; ++it) {
2035 const OperandMatchEntry &OMI = *it;
2036 const MatchableInfo &II = *OMI.MI;
2037
2038 OS << " { \"" << II.Mnemonic << "\""
2039 << ", " << OMI.OperandMask;
2040
2041 OS << " /* ";
2042 bool printComma = false;
2043 for (int i = 0, e = 31; i !=e; ++i)
2044 if (OMI.OperandMask & (1 << i)) {
2045 if (printComma)
2046 OS << ", ";
2047 OS << i;
2048 printComma = true;
2049 }
2050 OS << " */";
2051
2052 OS << ", " << OMI.CI->Name
2053 << ", ";
2054
2055 // Write the required features mask.
2056 if (!II.RequiredFeatures.empty()) {
2057 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2058 if (i) OS << "|";
2059 OS << II.RequiredFeatures[i]->getEnumName();
2060 }
2061 } else
2062 OS << "0";
2063 OS << " },\n";
2064 }
2065 OS << "};\n\n";
2066
2067 // Emit the operand class switch to call the correct custom parser for
2068 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002069 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2070 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002071 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002072 << " &Operands,\n unsigned MCK) {\n\n"
2073 << " switch(MCK) {\n";
2074
2075 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2076 ie = Info.Classes.end(); it != ie; ++it) {
2077 ClassInfo *CI = *it;
2078 if (CI->ParserMethod.empty())
2079 continue;
2080 OS << " case " << CI->Name << ":\n"
2081 << " return " << CI->ParserMethod << "(Operands);\n";
2082 }
2083
2084 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002085 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002086 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002087 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002088 OS << "}\n\n";
2089
2090 // Emit the static custom operand parser. This code is very similar with
2091 // the other matcher. Also use MatchResultTy here just in case we go for
2092 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002093 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002094 << Target.getName() << ClassName << "::\n"
2095 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2096 << " &Operands,\n StringRef Mnemonic) {\n";
2097
2098 // Emit code to get the available features.
2099 OS << " // Get the current feature set.\n";
2100 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2101
2102 OS << " // Get the next operand index.\n";
2103 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2104
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002105 // Emit code to search the table.
2106 OS << " // Search the table.\n";
2107 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2108 OS << " MnemonicRange =\n";
2109 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2110 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2111 << " LessOpcodeOperand());\n\n";
2112
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002113 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002114 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002115
2116 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2117 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2118
2119 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2120 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2121
2122 // Emit check that the required features are available.
2123 OS << " // check if the available features match\n";
2124 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2125 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002126 OS << " continue;\n";
2127 OS << " }\n\n";
2128
2129 // Emit check to ensure the operand number matches.
2130 OS << " // check if the operand in question has a custom parser.\n";
2131 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2132 OS << " continue;\n\n";
2133
2134 // Emit call to the custom parser method
2135 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002136 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002137 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002138 OS << " if (Result != MatchOperand_NoMatch)\n";
2139 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002140 OS << " }\n\n";
2141
Jim Grosbachf922c472011-02-12 01:34:40 +00002142 OS << " // Okay, we had no match.\n";
2143 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002144 OS << "}\n\n";
2145}
2146
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002147void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002148 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002149 Record *AsmParser = Target.getAsmParser();
2150 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2151
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002152 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002153 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002154 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002155
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002156 // Sort the instruction table using the partial order on classes. We use
2157 // stable_sort to ensure that ambiguous instructions are still
2158 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002159 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2160 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002161
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002162 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002163 for (std::vector<MatchableInfo*>::iterator
2164 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002165 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002166 (*it)->dump();
2167 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002168
Chris Lattner22bc5c42010-11-01 05:06:45 +00002169 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002170 DEBUG_WITH_TYPE("ambiguous_instrs", {
2171 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002172 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002173 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002174 MatchableInfo &A = *Info.Matchables[i];
2175 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002176
Bob Wilson1f64ac42011-01-26 21:26:21 +00002177 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002178 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002179 A.dump();
2180 errs() << "\nis incomparable with:\n";
2181 B.dump();
2182 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002183 ++NumAmbiguous;
2184 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002185 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002186 }
Chris Lattner87410362010-09-06 20:21:47 +00002187 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002188 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002189 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002190 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002191
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002192 // Compute the information on the custom operand parsing.
2193 Info.BuildOperandMatchInfo();
2194
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002195 // Write the output.
2196
2197 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2198
Chris Lattner0692ee62010-09-06 19:11:01 +00002199 // Information for the class declaration.
2200 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2201 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002202 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002203 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002204 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002205 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2206 << "unsigned Opcode,\n"
2207 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2208 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002209 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002210 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002211 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002212 OS << " MCInst &Inst, unsigned &ErrorInfo);\n";
2213
2214 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002215 OS << "\n enum OperandMatchResultTy {\n";
2216 OS << " MatchOperand_Success, // operand matched successfully\n";
2217 OS << " MatchOperand_NoMatch, // operand did not match\n";
2218 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2219 OS << " };\n";
2220 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002221 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2222 OS << " StringRef Mnemonic);\n";
2223
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002224 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002225 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2226 OS << " unsigned MCK);\n\n";
2227 }
2228
Chris Lattner0692ee62010-09-06 19:11:01 +00002229 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2230
Chris Lattner0692ee62010-09-06 19:11:01 +00002231 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2232 OS << "#undef GET_REGISTER_MATCHER\n\n";
2233
Daniel Dunbar54074b52010-07-19 05:44:09 +00002234 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002235 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002236
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002237 // Emit the function to match a register name to number.
2238 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002239
2240 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002241
Chris Lattner0692ee62010-09-06 19:11:01 +00002242
2243 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2244 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002245
Chris Lattner7fd44892010-10-30 18:48:18 +00002246 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002247 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002248
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002249 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002250 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002251
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002252 // Emit the enumeration for classes which participate in matching.
2253 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002254
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002255 // Emit the routine to match token strings to their match class.
2256 EmitMatchTokenString(Target, Info.Classes, OS);
2257
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002258 // Emit the subclass predicate routine.
2259 EmitIsSubclass(Target, Info.Classes, OS);
2260
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002261 // Emit the routine to validate an operand against a match class.
2262 EmitValidateOperandClass(Info, OS);
2263
Daniel Dunbar54074b52010-07-19 05:44:09 +00002264 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002265 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002266
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002267
2268 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002269 for (std::vector<MatchableInfo*>::const_iterator it =
2270 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002271 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002272 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002273
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002274 // Emit the static match table; unused classes get initalized to 0 which is
2275 // guaranteed to be InvalidMatchClass.
2276 //
2277 // FIXME: We can reduce the size of this table very easily. First, we change
2278 // it so that store the kinds in separate bit-fields for each index, which
2279 // only needs to be the max width used for classes at that index (we also need
2280 // to reject based on this during classification). If we then make sure to
2281 // order the match kinds appropriately (putting mnemonics last), then we
2282 // should only end up using a few bits for each class, especially the ones
2283 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002284 OS << "namespace {\n";
2285 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002286 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002287 OS << " const char *Mnemonic;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002288 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2289 << " ConvertFn;\n";
2290 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2291 << " Classes[" << MaxNumOperands << "];\n";
2292 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2293 << " RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002294 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002295
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002296 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002297 OS << " struct LessOpcode {\n";
2298 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2299 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2300 OS << " }\n";
2301 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2302 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2303 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002304 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2305 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2306 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002307 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002308
Chris Lattner96352e52010-09-06 21:08:38 +00002309 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002310
Chris Lattner96352e52010-09-06 21:08:38 +00002311 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002312 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002313
Chris Lattner22bc5c42010-11-01 05:06:45 +00002314 for (std::vector<MatchableInfo*>::const_iterator it =
2315 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002316 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002317 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002318
Chris Lattner662e5a32010-11-06 07:14:44 +00002319 OS << " { " << Target.getName() << "::"
2320 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2321 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002322 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002323 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002324
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002325 if (i) OS << ", ";
2326 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002327 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002328 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002329
Daniel Dunbar54074b52010-07-19 05:44:09 +00002330 // Write the required features mask.
2331 if (!II.RequiredFeatures.empty()) {
2332 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2333 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002334 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002335 }
2336 } else
2337 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002338
Daniel Dunbar54074b52010-07-19 05:44:09 +00002339 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002340 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002341
Chris Lattner96352e52010-09-06 21:08:38 +00002342 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002343
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002344 // A method to determine if a mnemonic is in the list.
2345 OS << "bool " << Target.getName() << ClassName << "::\n"
2346 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2347 OS << " // Search the table.\n";
2348 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2349 OS << " std::equal_range(MatchTable, MatchTable+"
2350 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2351 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2352 OS << "}\n\n";
2353
Chris Lattner96352e52010-09-06 21:08:38 +00002354 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002355 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002356 << Target.getName() << ClassName << "::\n"
2357 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2358 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002359 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002360
2361 // Emit code to get the available features.
2362 OS << " // Get the current feature set.\n";
2363 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2364
Chris Lattner674c1dc2010-10-30 17:36:36 +00002365 OS << " // Get the instruction mnemonic, which is the first token.\n";
2366 OS << " StringRef Mnemonic = ((" << Target.getName()
2367 << "Operand*)Operands[0])->getToken();\n\n";
2368
Chris Lattner7fd44892010-10-30 18:48:18 +00002369 if (HasMnemonicAliases) {
2370 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002371 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002372 }
Bob Wilson828295b2011-01-26 21:26:19 +00002373
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002374 // Emit code to compute the class list for this operand vector.
2375 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002376 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2377 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2378 OS << " return Match_InvalidOperand;\n";
2379 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002380
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002381 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002382 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002383 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002384 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002385 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002386 OS << " // wrong for all instances of the instruction.\n";
2387 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002388
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002389 // Emit code to search the table.
2390 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002391 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2392 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002393 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002394
Chris Lattnera008e8a2010-09-06 21:54:15 +00002395 OS << " // Return a more specific error code if no mnemonics match.\n";
2396 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2397 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002398
Chris Lattner2b1f9432010-09-06 21:22:45 +00002399 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002400 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002401 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002402
Gabor Greife53ee3b2010-09-07 06:06:06 +00002403 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002404 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002405
Daniel Dunbar54074b52010-07-19 05:44:09 +00002406 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002407 OS << " bool OperandsValid = true;\n";
2408 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002409 OS << " if (i + 1 >= Operands.size()) {\n";
2410 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002411 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002412 OS << " }\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002413 OS << " if (validateOperandClass(Operands[i+1], "
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002414 "(MatchClassKind)it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002415 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002416 OS << " // If this operand is broken for all of the instances of this\n";
2417 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002418 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002419 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002420 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2421 OS << " OperandsValid = false;\n";
2422 OS << " break;\n";
2423 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002424
Chris Lattnerce4a3352010-09-06 22:11:18 +00002425 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002426
2427 // Emit check that the required features are available.
2428 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2429 << "!= it->RequiredFeatures) {\n";
2430 OS << " HadMatchOtherThanFeatures = true;\n";
2431 OS << " continue;\n";
2432 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002433 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002434 OS << " // We have selected a definite instruction, convert the parsed\n"
2435 << " // operands into the appropriate MCInst.\n";
2436 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2437 << " it->Opcode, Operands))\n";
2438 OS << " return Match_ConversionFail;\n";
2439 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002440
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002441 // Verify the instruction with the target-specific match predicate function.
2442 OS << " // We have a potential match. Check the target predicate to\n"
2443 << " // handle any context sensitive constraints.\n"
2444 << " unsigned MatchResult;\n"
2445 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2446 << " Match_Success) {\n"
2447 << " Inst.clear();\n"
2448 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002449 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002450 << " continue;\n"
2451 << " }\n\n";
2452
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002453 // Call the post-processing function, if used.
2454 std::string InsnCleanupFn =
2455 AsmParser->getValueAsString("AsmParserInstCleanup");
2456 if (!InsnCleanupFn.empty())
2457 OS << " " << InsnCleanupFn << "(Inst);\n";
2458
Chris Lattner79ed3f72010-09-06 19:22:17 +00002459 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002460 OS << " }\n\n";
2461
Chris Lattnerec6789f2010-09-06 20:08:02 +00002462 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002463 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
2464 OS << " return RetCode;\n";
2465 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002466 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002467
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002468 if (Info.OperandMatchInfo.size())
2469 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2470
Chris Lattner0692ee62010-09-06 19:11:01 +00002471 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002472}