blob: bf0690f63aa8ef58d2f14428767d304590f45b34 [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"
101#include "Record.h"
Chris Lattner5845e5c2010-09-06 02:01:51 +0000102#include "StringMatcher.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000103#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc07bd402010-11-04 02:11:18 +0000104#include "llvm/ADT/PointerUnion.h"
Chris Lattner1de88232010-11-01 01:47:07 +0000105#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000106#include "llvm/ADT/SmallVector.h"
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000107#include "llvm/ADT/STLExtras.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000108#include "llvm/ADT/StringExtras.h"
109#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000110#include "llvm/Support/Debug.h"
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000111#include <map>
112#include <set>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000113using namespace llvm;
114
Daniel Dunbar27249152009-08-07 20:33:39 +0000115static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000116MatchPrefix("match-prefix", cl::init(""),
117 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000118
Daniel Dunbar20927f22009-08-07 08:26:05 +0000119namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000120class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000121struct SubtargetFeatureInfo;
122
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000123/// ClassInfo - Helper class for storing the information about a particular
124/// class of operands which can be matched.
125struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000126 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000127 /// Invalid kind, for use as a sentinel value.
128 Invalid = 0,
129
130 /// The class for a particular token.
131 Token,
132
133 /// The (first) register class, subsequent register classes are
134 /// RegisterClass0+1, and so on.
135 RegisterClass0,
136
137 /// The (first) user defined class, subsequent user defined classes are
138 /// UserClass0+1, and so on.
139 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000140 };
141
142 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
143 /// N) for the Nth user defined class.
144 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000145
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000146 /// SuperClasses - The super classes of this class. Note that for simplicities
147 /// sake user operands only record their immediate super class, while register
148 /// operands include all superclasses.
149 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000150
Daniel Dunbar6745d422009-08-09 05:18:30 +0000151 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000152 std::string Name;
153
Daniel Dunbar6745d422009-08-09 05:18:30 +0000154 /// ClassName - The unadorned generic name for this class (e.g., Token).
155 std::string ClassName;
156
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000157 /// ValueName - The name of the value this class represents; for a token this
158 /// is the literal token string, for an operand it is the TableGen class (or
159 /// empty if this is a derived class).
160 std::string ValueName;
161
162 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000163 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000164 std::string PredicateMethod;
165
166 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000167 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000168 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000169
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000170 /// ParserMethod - The name of the operand method to do a target specific
171 /// parsing on the operand.
172 std::string ParserMethod;
173
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000174 /// For register classes, the records for all the registers in this class.
175 std::set<Record*> Registers;
176
177public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000178 /// isRegisterClass() - Check if this is a register class.
179 bool isRegisterClass() const {
180 return Kind >= RegisterClass0 && Kind < UserClass0;
181 }
182
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000183 /// isUserClass() - Check if this is a user defined class.
184 bool isUserClass() const {
185 return Kind >= UserClass0;
186 }
187
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000188 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
189 /// are related if they are in the same class hierarchy.
190 bool isRelatedTo(const ClassInfo &RHS) const {
191 // Tokens are only related to tokens.
192 if (Kind == Token || RHS.Kind == Token)
193 return Kind == Token && RHS.Kind == Token;
194
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000195 // Registers classes are only related to registers classes, and only if
196 // their intersection is non-empty.
197 if (isRegisterClass() || RHS.isRegisterClass()) {
198 if (!isRegisterClass() || !RHS.isRegisterClass())
199 return false;
200
201 std::set<Record*> Tmp;
202 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000203 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000204 RHS.Registers.begin(), RHS.Registers.end(),
205 II);
206
207 return !Tmp.empty();
208 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000209
210 // Otherwise we have two users operands; they are related if they are in the
211 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000212 //
213 // FIXME: This is an oversimplification, they should only be related if they
214 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000215 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
216 const ClassInfo *Root = this;
217 while (!Root->SuperClasses.empty())
218 Root = Root->SuperClasses.front();
219
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000220 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000221 while (!RHSRoot->SuperClasses.empty())
222 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000223
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000224 return Root == RHSRoot;
225 }
226
Jim Grosbacha7c78222010-10-29 22:13:48 +0000227 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000228 bool isSubsetOf(const ClassInfo &RHS) const {
229 // This is a subset of RHS if it is the same class...
230 if (this == &RHS)
231 return true;
232
233 // ... or if any of its super classes are a subset of RHS.
234 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
235 ie = SuperClasses.end(); it != ie; ++it)
236 if ((*it)->isSubsetOf(RHS))
237 return true;
238
239 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000240 }
241
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000242 /// operator< - Compare two classes.
243 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000244 if (this == &RHS)
245 return false;
246
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000247 // Unrelated classes can be ordered by kind.
248 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000249 return Kind < RHS.Kind;
250
251 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000252 case Invalid:
253 assert(0 && "Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000254 case Token:
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000255 // Tokens are comparable by value.
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000256 //
257 // FIXME: Compare by enum value.
258 return ValueName < RHS.ValueName;
259
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000260 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000261 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000262 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000263 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000264 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000265 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000266
267 // Otherwise, order by name to ensure we have a total ordering.
268 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000269 }
270 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000271};
272
Chris Lattner22bc5c42010-11-01 05:06:45 +0000273/// MatchableInfo - Helper class for storing the necessary information for an
274/// instruction or alias which is capable of being matched.
275struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000276 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000277 /// Token - This is the token that the operand came from.
278 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000279
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000280 /// The unique class instance this operand should match.
281 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000282
Chris Lattner567820c2010-11-04 01:42:59 +0000283 /// The operand name this is, if anything.
284 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000285
286 /// The suboperand index within SrcOpName, or -1 for the entire operand.
287 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000288
Bob Wilsona49c7df2011-01-26 19:44:55 +0000289 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000290 };
Bob Wilson828295b2011-01-26 21:26:19 +0000291
Chris Lattner1d13bda2010-11-04 00:43:46 +0000292 /// ResOperand - This represents a single operand in the result instruction
293 /// generated by the match. In cases (like addressing modes) where a single
294 /// assembler operand expands to multiple MCOperands, this represents the
295 /// single assembler operand, not the MCOperand.
296 struct ResOperand {
297 enum {
298 /// RenderAsmOperand - This represents an operand result that is
299 /// generated by calling the render method on the assembly operand. The
300 /// corresponding AsmOperand is specified by AsmOperandNum.
301 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000302
Chris Lattner1d13bda2010-11-04 00:43:46 +0000303 /// TiedOperand - This represents a result operand that is a duplicate of
304 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000305 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000306
Chris Lattner98c870f2010-11-06 19:25:43 +0000307 /// ImmOperand - This represents an immediate value that is dumped into
308 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000309 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000310
Chris Lattner90fd7972010-11-06 19:57:21 +0000311 /// RegOperand - This represents a fixed register that is dumped in.
312 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000313 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000314
Chris Lattner1d13bda2010-11-04 00:43:46 +0000315 union {
316 /// This is the operand # in the AsmOperands list that this should be
317 /// copied from.
318 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000319
Chris Lattner1d13bda2010-11-04 00:43:46 +0000320 /// TiedOperandNum - This is the (earlier) result operand that should be
321 /// copied from.
322 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000323
Chris Lattner98c870f2010-11-06 19:25:43 +0000324 /// ImmVal - This is the immediate value added to the instruction.
325 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000326
Chris Lattner90fd7972010-11-06 19:57:21 +0000327 /// Register - This is the register record.
328 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000329 };
Bob Wilson828295b2011-01-26 21:26:19 +0000330
Bob Wilsona49c7df2011-01-26 19:44:55 +0000331 /// MINumOperands - The number of MCInst operands populated by this
332 /// operand.
333 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000334
Bob Wilsona49c7df2011-01-26 19:44:55 +0000335 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000336 ResOperand X;
337 X.Kind = RenderAsmOperand;
338 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000339 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000340 return X;
341 }
Bob Wilson828295b2011-01-26 21:26:19 +0000342
Bob Wilsona49c7df2011-01-26 19:44:55 +0000343 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000344 ResOperand X;
345 X.Kind = TiedOperand;
346 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000347 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000348 return X;
349 }
Bob Wilson828295b2011-01-26 21:26:19 +0000350
Bob Wilsona49c7df2011-01-26 19:44:55 +0000351 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000352 ResOperand X;
353 X.Kind = ImmOperand;
354 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000355 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000356 return X;
357 }
Bob Wilson828295b2011-01-26 21:26:19 +0000358
Bob Wilsona49c7df2011-01-26 19:44:55 +0000359 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000360 ResOperand X;
361 X.Kind = RegOperand;
362 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000363 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000364 return X;
365 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000366 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000367
Chris Lattner3b5aec62010-11-02 17:34:28 +0000368 /// TheDef - This is the definition of the instruction or InstAlias that this
369 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000370 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000371
Chris Lattnerc07bd402010-11-04 02:11:18 +0000372 /// DefRec - This is the definition that it came from.
373 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000374
Chris Lattner662e5a32010-11-06 07:14:44 +0000375 const CodeGenInstruction *getResultInst() const {
376 if (DefRec.is<const CodeGenInstruction*>())
377 return DefRec.get<const CodeGenInstruction*>();
378 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
379 }
Bob Wilson828295b2011-01-26 21:26:19 +0000380
Chris Lattner1d13bda2010-11-04 00:43:46 +0000381 /// ResOperands - This is the operand list that should be built for the result
382 /// MCInst.
383 std::vector<ResOperand> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384
385 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000386 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000387 std::string AsmString;
388
Chris Lattnerd19ec052010-11-02 17:30:52 +0000389 /// Mnemonic - This is the first token of the matched instruction, its
390 /// mnemonic.
391 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000392
Chris Lattner3116fef2010-11-02 01:03:43 +0000393 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000394 /// annotated with a class and where in the OperandList they were defined.
395 /// This directly corresponds to the tokenized AsmString after the mnemonic is
396 /// removed.
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000397 SmallVector<AsmOperand, 4> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398
Daniel Dunbar54074b52010-07-19 05:44:09 +0000399 /// Predicates - The required subtarget features to match this instruction.
400 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
401
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000402 /// ConversionFnKind - The enum value which is passed to the generated
403 /// ConvertToMCInst to convert parsed operands into an MCInst for this
404 /// function.
405 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000406
Chris Lattner22bc5c42010-11-01 05:06:45 +0000407 MatchableInfo(const CodeGenInstruction &CGI)
Chris Lattner662e5a32010-11-06 07:14:44 +0000408 : TheDef(CGI.TheDef), DefRec(&CGI), AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000409 }
410
Chris Lattner22bc5c42010-11-01 05:06:45 +0000411 MatchableInfo(const CodeGenInstAlias *Alias)
Chris Lattner662e5a32010-11-06 07:14:44 +0000412 : TheDef(Alias->TheDef), DefRec(Alias), AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000413 }
Bob Wilson828295b2011-01-26 21:26:19 +0000414
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000415 void Initialize(const AsmMatcherInfo &Info,
416 SmallPtrSet<Record*, 16> &SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +0000417
Chris Lattner22bc5c42010-11-01 05:06:45 +0000418 /// Validate - Return true if this matchable is a valid thing to match against
419 /// and perform a bunch of validity checking.
420 bool Validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000421
Chris Lattnerd19ec052010-11-02 17:30:52 +0000422 /// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner1de88232010-11-01 01:47:07 +0000423 /// register, return the Record for it, otherwise return null.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000424 Record *getSingletonRegisterForAsmOperand(unsigned i,
Bob Wilson828295b2011-01-26 21:26:19 +0000425 const AsmMatcherInfo &Info) const;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000426
Bob Wilsona49c7df2011-01-26 19:44:55 +0000427 /// FindAsmOperand - Find the AsmOperand with the specified name and
428 /// suboperand index.
429 int FindAsmOperand(StringRef N, int SubOpIdx) const {
430 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
431 if (N == AsmOperands[i].SrcOpName &&
432 SubOpIdx == AsmOperands[i].SubOpIdx)
433 return i;
434 return -1;
435 }
Bob Wilson828295b2011-01-26 21:26:19 +0000436
Bob Wilsona49c7df2011-01-26 19:44:55 +0000437 /// FindAsmOperandNamed - Find the first AsmOperand with the specified name.
438 /// This does not check the suboperand index.
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000439 int FindAsmOperandNamed(StringRef N) const {
440 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
441 if (N == AsmOperands[i].SrcOpName)
442 return i;
443 return -1;
444 }
Bob Wilson828295b2011-01-26 21:26:19 +0000445
Chris Lattner41409852010-11-06 07:31:43 +0000446 void BuildInstructionResultOperands();
447 void BuildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000448
Chris Lattner22bc5c42010-11-01 05:06:45 +0000449 /// operator< - Compare two matchables.
450 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000451 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000452 if (Mnemonic != RHS.Mnemonic)
453 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000454
Chris Lattner3116fef2010-11-02 01:03:43 +0000455 if (AsmOperands.size() != RHS.AsmOperands.size())
456 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000457
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000458 // Compare lexicographically by operand. The matcher validates that other
Bob Wilson1f64ac42011-01-26 21:26:21 +0000459 // orderings wouldn't be ambiguous using \see CouldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000460 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
461 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000462 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000463 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000464 return false;
465 }
466
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000467 return false;
468 }
469
Bob Wilson1f64ac42011-01-26 21:26:21 +0000470 /// CouldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000471 /// ambiguously match the same set of operands as \arg RHS (without being a
472 /// strictly superior match).
Bob Wilson1f64ac42011-01-26 21:26:21 +0000473 bool CouldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000474 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000475 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000476 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000477
Daniel Dunbar2b544812009-08-09 06:05:33 +0000478 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000479 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000480 return false;
481
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000482 // Otherwise, make sure the ordering of the two instructions is unambiguous
483 // by checking that either (a) a token or operand kind discriminates them,
484 // or (b) the ordering among equivalent kinds is consistent.
485
Daniel Dunbar2b544812009-08-09 06:05:33 +0000486 // Tokens and operand kinds are unambiguous (assuming a correct target
487 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000488 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
489 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
490 AsmOperands[i].Class->Kind == ClassInfo::Token)
491 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
492 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000493 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000494
Daniel Dunbar2b544812009-08-09 06:05:33 +0000495 // Otherwise, this operand could commute if all operands are equivalent, or
496 // there is a pair of operands that compare less than and a pair that
497 // compare greater than.
498 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000499 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
500 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000501 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000502 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000503 HasGT = true;
504 }
505
506 return !(HasLT ^ HasGT);
507 }
508
Daniel Dunbar20927f22009-08-07 08:26:05 +0000509 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000510
Chris Lattnerd19ec052010-11-02 17:30:52 +0000511private:
512 void TokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000513};
514
Daniel Dunbar54074b52010-07-19 05:44:09 +0000515/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
516/// feature which participates in instruction matching.
517struct SubtargetFeatureInfo {
518 /// \brief The predicate record for this feature.
519 Record *TheDef;
520
521 /// \brief An unique index assigned to represent this feature.
522 unsigned Index;
523
Chris Lattner0aed1e72010-10-30 20:07:57 +0000524 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000525
Daniel Dunbar54074b52010-07-19 05:44:09 +0000526 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000527 std::string getEnumName() const {
528 return "Feature_" + TheDef->getName();
529 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000530};
531
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000532struct OperandMatchEntry {
533 unsigned OperandMask;
534 MatchableInfo* MI;
535 ClassInfo *CI;
536
537 static OperandMatchEntry Create(MatchableInfo* mi, ClassInfo *ci,
538 unsigned opMask) {
539 OperandMatchEntry X;
540 X.OperandMask = opMask;
541 X.CI = ci;
542 X.MI = mi;
543 return X;
544 }
545};
546
547
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000548class AsmMatcherInfo {
549public:
Chris Lattner67db8832010-12-13 00:23:57 +0000550 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000551 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000552
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000553 /// The tablegen AsmParser record.
554 Record *AsmParser;
555
Chris Lattner02bcbc92010-11-01 01:37:30 +0000556 /// Target - The target information.
557 CodeGenTarget &Target;
558
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000559 /// The AsmParser "RegisterPrefix" value.
560 std::string RegisterPrefix;
561
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000562 /// The classes which are needed for matching.
563 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000564
Chris Lattner22bc5c42010-11-01 05:06:45 +0000565 /// The information on the matchables to match.
566 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000567
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000568 /// Info for custom matching operands by user defined methods.
569 std::vector<OperandMatchEntry> OperandMatchInfo;
570
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000571 /// Map of Register records to their class information.
572 std::map<Record*, ClassInfo*> RegisterClasses;
573
Daniel Dunbar54074b52010-07-19 05:44:09 +0000574 /// Map of Predicate records to their subtarget information.
575 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000576
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000577private:
578 /// Map of token to class information which has already been constructed.
579 std::map<std::string, ClassInfo*> TokenClasses;
580
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000581 /// Map of RegisterClass records to their class information.
582 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000583
Daniel Dunbar338825c2009-08-10 18:41:10 +0000584 /// Map of AsmOperandClass records to their class information.
585 std::map<Record*, ClassInfo*> AsmOperandClasses;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000586
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000587private:
588 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000589 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000590
591 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000592 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
593 int SubOpIdx = -1);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000594
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000595 /// BuildRegisterClasses - Build the ClassInfo* instances for register
596 /// classes.
Chris Lattner1de88232010-11-01 01:47:07 +0000597 void BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000598
599 /// BuildOperandClasses - Build the ClassInfo* instances for user defined
600 /// operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000601 void BuildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000602
Bob Wilsona49c7df2011-01-26 19:44:55 +0000603 void BuildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
604 unsigned AsmOpIdx);
605 void BuildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000606 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000607
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000608public:
Bob Wilson828295b2011-01-26 21:26:19 +0000609 AsmMatcherInfo(Record *AsmParser,
610 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000611 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000612
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000613 /// BuildInfo - Construct the various tables used during matching.
Chris Lattner02bcbc92010-11-01 01:37:30 +0000614 void BuildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000615
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000616 /// BuildOperandMatchInfo - Build the necessary information to handle user
617 /// defined operand parsing methods.
618 void BuildOperandMatchInfo();
619
Chris Lattner6fa152c2010-10-30 20:15:02 +0000620 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
621 /// given operand.
622 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
623 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
624 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
625 SubtargetFeatures.find(Def);
626 return I == SubtargetFeatures.end() ? 0 : I->second;
627 }
Chris Lattner67db8832010-12-13 00:23:57 +0000628
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000629 RecordKeeper &getRecords() const {
630 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000631 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000632};
633
Daniel Dunbar20927f22009-08-07 08:26:05 +0000634}
635
Chris Lattner22bc5c42010-11-01 05:06:45 +0000636void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000637 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000638
Chris Lattner3116fef2010-11-02 01:03:43 +0000639 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000640 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000641 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000642 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000643 }
644}
645
Chris Lattner22bc5c42010-11-01 05:06:45 +0000646void MatchableInfo::Initialize(const AsmMatcherInfo &Info,
647 SmallPtrSet<Record*, 16> &SingletonRegisters) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000648 // TODO: Eventually support asmparser for Variant != 0.
649 AsmString = CodeGenInstruction::FlattenAsmStringVariants(AsmString, 0);
Bob Wilson828295b2011-01-26 21:26:19 +0000650
Chris Lattnerd19ec052010-11-02 17:30:52 +0000651 TokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000652
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000653 // Compute the require features.
654 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
655 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
656 if (SubtargetFeatureInfo *Feature =
657 Info.getSubtargetFeature(Predicates[i]))
658 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000659
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000660 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000661 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
662 if (Record *Reg = getSingletonRegisterForAsmOperand(i, Info))
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000663 SingletonRegisters.insert(Reg);
664 }
665}
666
Chris Lattnerd19ec052010-11-02 17:30:52 +0000667/// TokenizeAsmString - Tokenize a simplified assembly string.
668void MatchableInfo::TokenizeAsmString(const AsmMatcherInfo &Info) {
669 StringRef String = AsmString;
670 unsigned Prev = 0;
671 bool InTok = true;
672 for (unsigned i = 0, e = String.size(); i != e; ++i) {
673 switch (String[i]) {
674 case '[':
675 case ']':
676 case '*':
677 case '!':
678 case ' ':
679 case '\t':
680 case ',':
681 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000682 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000683 InTok = false;
684 }
685 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000686 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000687 Prev = i + 1;
688 break;
689
690 case '\\':
691 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000692 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000693 InTok = false;
694 }
695 ++i;
696 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000697 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000698 Prev = i + 1;
699 break;
700
701 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000702 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000703 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000704 InTok = false;
705 }
Bob Wilson828295b2011-01-26 21:26:19 +0000706
Chris Lattner7ad31472010-11-06 22:06:03 +0000707 // If this isn't "${", treat like a normal token.
708 if (i + 1 == String.size() || String[i + 1] != '{') {
709 Prev = i;
710 break;
711 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000712
713 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
714 assert(End != String.end() && "Missing brace in operand reference!");
715 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000716 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000717 Prev = EndPos + 1;
718 i = EndPos;
719 break;
720 }
721
722 case '.':
723 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000724 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000725 Prev = i;
726 InTok = true;
727 break;
728
729 default:
730 InTok = true;
731 }
732 }
733 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000734 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000735
Chris Lattnerd19ec052010-11-02 17:30:52 +0000736 // The first token of the instruction is the mnemonic, which must be a
737 // simple string, not a $foo variable or a singleton register.
738 assert(!AsmOperands.empty() && "Instruction has no tokens?");
739 Mnemonic = AsmOperands[0].Token;
740 if (Mnemonic[0] == '$' || getSingletonRegisterForAsmOperand(0, Info))
741 throw TGError(TheDef->getLoc(),
742 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000743
Chris Lattnerd19ec052010-11-02 17:30:52 +0000744 // Remove the first operand, it is tracked in the mnemonic field.
745 AsmOperands.erase(AsmOperands.begin());
746}
747
Chris Lattner22bc5c42010-11-01 05:06:45 +0000748bool MatchableInfo::Validate(StringRef CommentDelimiter, bool Hack) const {
749 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000750 if (AsmString.empty())
751 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000752
Chris Lattner22bc5c42010-11-01 05:06:45 +0000753 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000754 // isCodeGenOnly if they are pseudo instructions.
755 if (AsmString.find('\n') != std::string::npos)
756 throw TGError(TheDef->getLoc(),
757 "multiline instruction is not valid for the asmparser, "
758 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000759
Chris Lattner4164f6b2010-11-01 04:44:29 +0000760 // Remove comments from the asm string. We know that the asmstring only
761 // has one line.
762 if (!CommentDelimiter.empty() &&
763 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
764 throw TGError(TheDef->getLoc(),
765 "asmstring for instruction has comment character in it, "
766 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000767
Chris Lattner22bc5c42010-11-01 05:06:45 +0000768 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000769 // handle, the target should be refactored to use operands instead of
770 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000771 //
772 // Also, check for instructions which reference the operand multiple times;
773 // this implies a constraint we would not honor.
774 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000775 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
776 StringRef Tok = AsmOperands[i].Token;
777 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000778 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000779 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000780 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000781
Chris Lattner22bc5c42010-11-01 05:06:45 +0000782 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000783 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000784 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000785 if (!Hack)
786 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000787 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000788 "' can never be matched!");
789 // FIXME: Should reject these. The ARM backend hits this with $lane in a
790 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000791 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000792 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000793 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000794 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000795 });
796 return false;
797 }
798 }
Bob Wilson828295b2011-01-26 21:26:19 +0000799
Chris Lattner5bc93872010-11-01 04:34:44 +0000800 return true;
801}
802
Chris Lattnerd19ec052010-11-02 17:30:52 +0000803/// getSingletonRegisterForAsmOperand - If the specified token is a singleton
Chris Lattner02bcbc92010-11-01 01:37:30 +0000804/// register, return the register name, otherwise return a null StringRef.
Chris Lattner22bc5c42010-11-01 05:06:45 +0000805Record *MatchableInfo::
Chris Lattnerd19ec052010-11-02 17:30:52 +0000806getSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info) const{
807 StringRef Tok = AsmOperands[i].Token;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000808 if (!Tok.startswith(Info.RegisterPrefix))
Chris Lattner1de88232010-11-01 01:47:07 +0000809 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000810
Chris Lattner02bcbc92010-11-01 01:37:30 +0000811 StringRef RegName = Tok.substr(Info.RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000812 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
813 return Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000814
Chris Lattner1de88232010-11-01 01:47:07 +0000815 // If there is no register prefix (i.e. "%" in "%eax"), then this may
816 // be some random non-register token, just ignore it.
817 if (Info.RegisterPrefix.empty())
818 return 0;
Bob Wilson828295b2011-01-26 21:26:19 +0000819
Chris Lattnerec6f0962010-11-02 18:10:06 +0000820 // Otherwise, we have something invalid prefixed with the register prefix,
821 // such as %foo.
Chris Lattner1de88232010-11-01 01:47:07 +0000822 std::string Err = "unable to find register for '" + RegName.str() +
823 "' (which matches register prefix)";
Chris Lattner5bc93872010-11-01 04:34:44 +0000824 throw TGError(TheDef->getLoc(), Err);
Chris Lattner02bcbc92010-11-01 01:37:30 +0000825}
826
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000827static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000828 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000829
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000830 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
831 switch (*it) {
832 case '*': Res += "_STAR_"; break;
833 case '%': Res += "_PCT_"; break;
834 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000835 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000836 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000837 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000838 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000839 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000840 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000841 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000842 }
843 }
844
845 return Res;
846}
847
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000848ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000849 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000850
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000851 if (!Entry) {
852 Entry = new ClassInfo();
853 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000854 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000855 Entry->Name = "MCK_" + getEnumNameForToken(Token);
856 Entry->ValueName = Token;
857 Entry->PredicateMethod = "<invalid>";
858 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000859 Entry->ParserMethod = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000860 Classes.push_back(Entry);
861 }
862
863 return Entry;
864}
865
866ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000867AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
868 int SubOpIdx) {
869 Record *Rec = OI.Rec;
870 if (SubOpIdx != -1)
871 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
872
873 if (Rec->isSubClassOf("RegisterClass")) {
874 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +0000875 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000876 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000877 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000878
Bob Wilsona49c7df2011-01-26 19:44:55 +0000879 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
880 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +0000881 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
882 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +0000883
Bob Wilsona49c7df2011-01-26 19:44:55 +0000884 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000885}
886
Chris Lattner1de88232010-11-01 01:47:07 +0000887void AsmMatcherInfo::
888BuildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000889 const std::vector<CodeGenRegister*> &Registers =
890 Target.getRegBank().getRegisters();
Chris Lattnerec6f0962010-11-02 18:10:06 +0000891 const std::vector<CodeGenRegisterClass> &RegClassList =
892 Target.getRegisterClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +0000893
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000894 // The register sets used for matching.
895 std::set< std::set<Record*> > RegisterSets;
896
Jim Grosbacha7c78222010-10-29 22:13:48 +0000897 // Gather the defined sets.
Chris Lattnerec6f0962010-11-02 18:10:06 +0000898 for (std::vector<CodeGenRegisterClass>::const_iterator it =
899 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000900 RegisterSets.insert(std::set<Record*>(it->getOrder().begin(),
901 it->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000902
903 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +0000904 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
905 ie = SingletonRegisters.end(); it != ie; ++it) {
906 Record *Rec = *it;
907 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
908 }
Jim Grosbacha7c78222010-10-29 22:13:48 +0000909
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000910 // Introduce derived sets where necessary (when a register does not determine
911 // a unique register set class), and build the mapping of registers to the set
912 // they should classify to.
913 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000914 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000915 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +0000916 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000917 // Compute the intersection of all sets containing this register.
918 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000919
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000920 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
921 ie = RegisterSets.end(); it != ie; ++it) {
922 if (!it->count(CGR.TheDef))
923 continue;
924
925 if (ContainingSet.empty()) {
926 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000927 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000928 }
Bob Wilson828295b2011-01-26 21:26:19 +0000929
Chris Lattnerec6f0962010-11-02 18:10:06 +0000930 std::set<Record*> Tmp;
931 std::swap(Tmp, ContainingSet);
932 std::insert_iterator< std::set<Record*> > II(ContainingSet,
933 ContainingSet.begin());
934 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000935 }
936
937 if (!ContainingSet.empty()) {
938 RegisterSets.insert(ContainingSet);
939 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
940 }
941 }
942
943 // Construct the register classes.
944 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
945 unsigned Index = 0;
946 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
947 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
948 ClassInfo *CI = new ClassInfo();
949 CI->Kind = ClassInfo::RegisterClass0 + Index;
950 CI->ClassName = "Reg" + utostr(Index);
951 CI->Name = "MCK_Reg" + utostr(Index);
952 CI->ValueName = "";
953 CI->PredicateMethod = ""; // unused
954 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000955 CI->Registers = *it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000956 Classes.push_back(CI);
957 RegisterSetClasses.insert(std::make_pair(*it, CI));
958 }
959
960 // Find the superclasses; we could compute only the subgroup lattice edges,
961 // but there isn't really a point.
962 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
963 ie = RegisterSets.end(); it != ie; ++it) {
964 ClassInfo *CI = RegisterSetClasses[*it];
965 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
966 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +0000967 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000968 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
969 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
970 }
971
972 // Name the register classes which correspond to a user defined RegisterClass.
Chris Lattnerec6f0962010-11-02 18:10:06 +0000973 for (std::vector<CodeGenRegisterClass>::const_iterator
974 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesenae1920b2011-06-15 04:50:36 +0000975 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(it->getOrder().begin(),
976 it->getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000977 if (CI->ValueName.empty()) {
978 CI->ClassName = it->getName();
979 CI->Name = "MCK_" + it->getName();
980 CI->ValueName = it->getName();
981 } else
982 CI->ValueName = CI->ValueName + "," + it->getName();
983
984 RegisterClassClasses.insert(std::make_pair(it->TheDef, CI));
985 }
986
987 // Populate the map for individual registers.
988 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
989 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +0000990 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000991
992 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +0000993 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
994 ie = SingletonRegisters.end(); it != ie; ++it) {
995 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +0000996 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +0000997 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +0000998
Chris Lattner1de88232010-11-01 01:47:07 +0000999 if (CI->ValueName.empty()) {
1000 CI->ClassName = Rec->getName();
1001 CI->Name = "MCK_" + Rec->getName();
1002 CI->ValueName = Rec->getName();
1003 } else
1004 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001005 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001006}
1007
Chris Lattner02bcbc92010-11-01 01:37:30 +00001008void AsmMatcherInfo::BuildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001009 std::vector<Record*> AsmOperands =
1010 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001011
1012 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001013 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001014 ie = AsmOperands.end(); it != ie; ++it)
1015 AsmOperandClasses[*it] = new ClassInfo();
1016
Daniel Dunbar338825c2009-08-10 18:41:10 +00001017 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001018 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001019 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001020 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001021 CI->Kind = ClassInfo::UserClass0 + Index;
1022
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001023 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
1024 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
1025 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
1026 if (!DI) {
1027 PrintError((*it)->getLoc(), "Invalid super class reference!");
1028 continue;
1029 }
1030
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001031 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1032 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001033 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001034 else
1035 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001036 }
1037 CI->ClassName = (*it)->getValueAsString("Name");
1038 CI->Name = "MCK_" + CI->ClassName;
1039 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001040
1041 // Get or construct the predicate method name.
1042 Init *PMName = (*it)->getValueInit("PredicateMethod");
1043 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
1044 CI->PredicateMethod = SI->getValue();
1045 } else {
Jim Grosbacha7c78222010-10-29 22:13:48 +00001046 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001047 "Unexpected PredicateMethod field!");
1048 CI->PredicateMethod = "is" + CI->ClassName;
1049 }
1050
1051 // Get or construct the render method name.
1052 Init *RMName = (*it)->getValueInit("RenderMethod");
1053 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
1054 CI->RenderMethod = SI->getValue();
1055 } else {
1056 assert(dynamic_cast<UnsetInit*>(RMName) &&
1057 "Unexpected RenderMethod field!");
1058 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1059 }
1060
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001061 // Get the parse method name or leave it as empty.
1062 Init *PRMName = (*it)->getValueInit("ParserMethod");
1063 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
1064 CI->ParserMethod = SI->getValue();
1065
Daniel Dunbar338825c2009-08-10 18:41:10 +00001066 AsmOperandClasses[*it] = CI;
1067 Classes.push_back(CI);
1068 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001069}
1070
Bob Wilson828295b2011-01-26 21:26:19 +00001071AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1072 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001073 RecordKeeper &records)
Chris Lattner67db8832010-12-13 00:23:57 +00001074 : Records(records), AsmParser(asmParser), Target(target),
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001075 RegisterPrefix(AsmParser->getValueAsString("RegisterPrefix")) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001076}
1077
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001078/// BuildOperandMatchInfo - Build the necessary information to handle user
1079/// defined operand parsing methods.
1080void AsmMatcherInfo::BuildOperandMatchInfo() {
1081
1082 /// Map containing a mask with all operands indicies that can be found for
1083 /// that class inside a instruction.
1084 std::map<ClassInfo*, unsigned> OpClassMask;
1085
1086 for (std::vector<MatchableInfo*>::const_iterator it =
1087 Matchables.begin(), ie = Matchables.end();
1088 it != ie; ++it) {
1089 MatchableInfo &II = **it;
1090 OpClassMask.clear();
1091
1092 // Keep track of all operands of this instructions which belong to the
1093 // same class.
1094 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1095 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1096 if (Op.Class->ParserMethod.empty())
1097 continue;
1098 unsigned &OperandMask = OpClassMask[Op.Class];
1099 OperandMask |= (1 << i);
1100 }
1101
1102 // Generate operand match info for each mnemonic/operand class pair.
1103 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1104 iie = OpClassMask.end(); iit != iie; ++iit) {
1105 unsigned OpMask = iit->second;
1106 ClassInfo *CI = iit->first;
1107 OperandMatchInfo.push_back(OperandMatchEntry::Create(&II, CI, OpMask));
1108 }
1109 }
1110}
1111
Chris Lattner02bcbc92010-11-01 01:37:30 +00001112void AsmMatcherInfo::BuildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001113 // Build information about all of the AssemblerPredicates.
1114 std::vector<Record*> AllPredicates =
1115 Records.getAllDerivedDefinitions("Predicate");
1116 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1117 Record *Pred = AllPredicates[i];
1118 // Ignore predicates that are not intended for the assembler.
1119 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1120 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001121
Chris Lattner4164f6b2010-11-01 04:44:29 +00001122 if (Pred->getName().empty())
1123 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001124
Chris Lattner0aed1e72010-10-30 20:07:57 +00001125 unsigned FeatureNo = SubtargetFeatures.size();
1126 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1127 assert(FeatureNo < 32 && "Too many subtarget features!");
1128 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001129
Chris Lattner4164f6b2010-11-01 04:44:29 +00001130 StringRef CommentDelimiter = AsmParser->getValueAsString("CommentDelimiter");
Bob Wilson828295b2011-01-26 21:26:19 +00001131
Chris Lattner39ee0362010-10-31 19:10:56 +00001132 // Parse the instructions; we need to do this first so that we can gather the
1133 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001134 SmallPtrSet<Record*, 16> SingletonRegisters;
Chris Lattner02bcbc92010-11-01 01:37:30 +00001135 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
1136 E = Target.inst_end(); I != E; ++I) {
1137 const CodeGenInstruction &CGI = **I;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001138
Chris Lattner39ee0362010-10-31 19:10:56 +00001139 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1140 // filter the set of instructions we consider.
Chris Lattnerb61e09d2010-03-19 00:18:23 +00001141 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001142 continue;
1143
Chris Lattner5bc93872010-11-01 04:34:44 +00001144 // Ignore "codegen only" instructions.
1145 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
1146 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001147
Chris Lattner1d13bda2010-11-04 00:43:46 +00001148 // Validate the operand list to ensure we can handle this instruction.
1149 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
1150 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001151
Chris Lattner1d13bda2010-11-04 00:43:46 +00001152 // Validate tied operands.
1153 if (OI.getTiedRegister() != -1) {
Bob Wilson828295b2011-01-26 21:26:19 +00001154 // If we have a tied operand that consists of multiple MCOperands,
1155 // reject it. We reject aliases and ignore instructions for now.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001156 if (OI.MINumOperands != 1) {
1157 // FIXME: Should reject these. The ARM backend hits this with $lane
1158 // in a bunch of instructions. It is unclear what the right answer is.
1159 DEBUG({
1160 errs() << "warning: '" << CGI.TheDef->getName() << "': "
1161 << "ignoring instruction with multi-operand tied operand '"
1162 << OI.Name << "'\n";
1163 });
1164 continue;
1165 }
1166 }
1167 }
Bob Wilson828295b2011-01-26 21:26:19 +00001168
Chris Lattner22bc5c42010-11-01 05:06:45 +00001169 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Daniel Dunbar20927f22009-08-07 08:26:05 +00001170
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001171 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001172
Chris Lattner4d43d0f2010-11-01 01:07:14 +00001173 // Ignore instructions which shouldn't be matched and diagnose invalid
1174 // instruction definitions with an error.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001175 if (!II->Validate(CommentDelimiter, true))
Chris Lattner5bc93872010-11-01 04:34:44 +00001176 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001177
Chris Lattner5bc93872010-11-01 04:34:44 +00001178 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1179 //
1180 // FIXME: This is a total hack.
Chris Lattner5abd1eb2010-11-06 06:43:11 +00001181 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
1182 StringRef(II->TheDef->getName()).endswith("_Int"))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001183 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001184
Chris Lattner22bc5c42010-11-01 05:06:45 +00001185 Matchables.push_back(II.take());
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001186 }
Bob Wilson828295b2011-01-26 21:26:19 +00001187
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001188 // Parse all of the InstAlias definitions and stick them in the list of
1189 // matchables.
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001190 std::vector<Record*> AllInstAliases =
1191 Records.getAllDerivedDefinitions("InstAlias");
1192 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
Chris Lattner225549f2010-11-06 06:39:47 +00001193 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001194
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001195 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1196 // filter the set of instruction aliases we consider, based on the target
1197 // instruction.
1198 if (!StringRef(Alias->ResultInst->TheDef->getName()).startswith(
1199 MatchPrefix))
1200 continue;
1201
Chris Lattner22bc5c42010-11-01 05:06:45 +00001202 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Bob Wilson828295b2011-01-26 21:26:19 +00001203
Chris Lattnerc2d67bb2010-11-01 04:53:48 +00001204 II->Initialize(*this, SingletonRegisters);
Bob Wilson828295b2011-01-26 21:26:19 +00001205
Chris Lattner22bc5c42010-11-01 05:06:45 +00001206 // Validate the alias definitions.
1207 II->Validate(CommentDelimiter, false);
Bob Wilson828295b2011-01-26 21:26:19 +00001208
Chris Lattnerb501d4f2010-11-01 05:34:34 +00001209 Matchables.push_back(II.take());
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001210 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001211
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001212 // Build info for the register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001213 BuildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001214
1215 // Build info for the user defined assembly operand classes.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001216 BuildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001217
Chris Lattner0bb780c2010-11-04 00:57:06 +00001218 // Build the information about matchables, now that we have fully formed
1219 // classes.
Chris Lattner22bc5c42010-11-01 05:06:45 +00001220 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1221 ie = Matchables.end(); it != ie; ++it) {
1222 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001223
Chris Lattnere206fcf2010-09-06 21:01:37 +00001224 // Parse the tokens after the mnemonic.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001225 // Note: BuildInstructionOperandReference may insert new AsmOperands, so
1226 // don't precompute the loop bound.
1227 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001228 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001229 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001230
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001231 // Check for singleton registers.
Chris Lattnerd19ec052010-11-02 17:30:52 +00001232 if (Record *RegRecord = II->getSingletonRegisterForAsmOperand(i, *this)) {
1233 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001234 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1235 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001236 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001237 }
1238
Daniel Dunbar20927f22009-08-07 08:26:05 +00001239 // Check for simple tokens.
1240 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001241 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001242 continue;
1243 }
1244
Chris Lattner7ad31472010-11-06 22:06:03 +00001245 if (Token.size() > 1 && isdigit(Token[1])) {
1246 Op.Class = getTokenClass(Token);
1247 continue;
1248 }
Bob Wilson828295b2011-01-26 21:26:19 +00001249
Chris Lattnerc07bd402010-11-04 02:11:18 +00001250 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001251 StringRef OperandName;
1252 if (Token[1] == '{')
1253 OperandName = Token.substr(2, Token.size() - 3);
1254 else
1255 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001256
Chris Lattnerc07bd402010-11-04 02:11:18 +00001257 if (II->DefRec.is<const CodeGenInstruction*>())
Bob Wilsona49c7df2011-01-26 19:44:55 +00001258 BuildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001259 else
Chris Lattner225549f2010-11-06 06:39:47 +00001260 BuildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001261 }
Bob Wilson828295b2011-01-26 21:26:19 +00001262
Chris Lattner41409852010-11-06 07:31:43 +00001263 if (II->DefRec.is<const CodeGenInstruction*>())
1264 II->BuildInstructionResultOperands();
1265 else
1266 II->BuildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001267 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001268
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001269 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001270 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001271}
1272
Chris Lattner0bb780c2010-11-04 00:57:06 +00001273/// BuildInstructionOperandReference - The specified operand is a reference to a
1274/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1275void AsmMatcherInfo::
1276BuildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001277 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001278 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001279 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1280 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001281 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001282
Chris Lattner662e5a32010-11-06 07:14:44 +00001283 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001284 unsigned Idx;
1285 if (!Operands.hasOperandNamed(OperandName, Idx))
1286 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1287 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001288
Bob Wilsona49c7df2011-01-26 19:44:55 +00001289 // If the instruction operand has multiple suboperands, but the parser
1290 // match class for the asm operand is still the default "ImmAsmOperand",
1291 // then handle each suboperand separately.
1292 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1293 Record *Rec = Operands[Idx].Rec;
1294 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1295 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1296 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1297 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1298 StringRef Token = Op->Token; // save this in case Op gets moved
1299 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1300 MatchableInfo::AsmOperand NewAsmOp(Token);
1301 NewAsmOp.SubOpIdx = SI;
1302 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1303 }
1304 // Replace Op with first suboperand.
1305 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1306 Op->SubOpIdx = 0;
1307 }
1308 }
1309
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001310 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001311 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001312
1313 // If the named operand is tied, canonicalize it to the untied operand.
1314 // For example, something like:
1315 // (outs GPR:$dst), (ins GPR:$src)
1316 // with an asmstring of
1317 // "inc $src"
1318 // we want to canonicalize to:
1319 // "inc $dst"
1320 // so that we know how to provide the $dst operand when filling in the result.
1321 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001322 if (OITied != -1) {
1323 // The tied operand index is an MIOperand index, find the operand that
1324 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001325 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1326 OperandName = Operands[Idx.first].Name;
1327 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001328 }
Bob Wilson828295b2011-01-26 21:26:19 +00001329
Bob Wilsona49c7df2011-01-26 19:44:55 +00001330 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001331}
1332
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001333/// BuildAliasOperandReference - When parsing an operand reference out of the
1334/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1335/// operand reference is by looking it up in the result pattern definition.
Chris Lattnerc07bd402010-11-04 02:11:18 +00001336void AsmMatcherInfo::BuildAliasOperandReference(MatchableInfo *II,
1337 StringRef OperandName,
1338 MatchableInfo::AsmOperand &Op) {
1339 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001340
Chris Lattnerc07bd402010-11-04 02:11:18 +00001341 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001342 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001343 if (CGA.ResultOperands[i].isRecord() &&
1344 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001345 // It's safe to go with the first one we find, because CodeGenInstAlias
1346 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001347 unsigned ResultIdx = CGA.ResultInstOperandIndex[i].first;
1348 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
1349 Op.Class = getOperandClass(CGA.ResultInst->Operands[ResultIdx],
1350 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001351 Op.SrcOpName = OperandName;
1352 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001353 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001354
1355 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1356 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001357}
1358
Chris Lattner41409852010-11-06 07:31:43 +00001359void MatchableInfo::BuildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001360 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001361
Chris Lattner662e5a32010-11-06 07:14:44 +00001362 // Loop over all operands of the result instruction, determining how to
1363 // populate them.
1364 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1365 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001366
1367 // If this is a tied operand, just copy from the previously handled operand.
1368 int TiedOp = OpInfo.getTiedRegister();
1369 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001370 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001371 continue;
1372 }
Bob Wilson828295b2011-01-26 21:26:19 +00001373
Bob Wilsona49c7df2011-01-26 19:44:55 +00001374 // Find out what operand from the asmparser this MCInst operand comes from.
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001375 int SrcOperand = FindAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001376 if (OpInfo.Name.empty() || SrcOperand == -1)
1377 throw TGError(TheDef->getLoc(), "Instruction '" +
1378 TheDef->getName() + "' has operand '" + OpInfo.Name +
1379 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001380
Bob Wilsona49c7df2011-01-26 19:44:55 +00001381 // Check if the one AsmOperand populates the entire operand.
1382 unsigned NumOperands = OpInfo.MINumOperands;
1383 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1384 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001385 continue;
1386 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001387
1388 // Add a separate ResOperand for each suboperand.
1389 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1390 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1391 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1392 "unexpected AsmOperands for suboperands");
1393 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1394 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001395 }
1396}
1397
Chris Lattner41409852010-11-06 07:31:43 +00001398void MatchableInfo::BuildAliasResultOperands() {
1399 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1400 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001401
Chris Lattner41409852010-11-06 07:31:43 +00001402 // Loop over all operands of the result instruction, determining how to
1403 // populate them.
1404 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001405 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001406 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001407 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001408
Chris Lattner41409852010-11-06 07:31:43 +00001409 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001410 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001411 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001412 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001413 continue;
1414 }
1415
Bob Wilsona49c7df2011-01-26 19:44:55 +00001416 // Handle all the suboperands for this operand.
1417 const std::string &OpName = OpInfo->Name;
1418 for ( ; AliasOpNo < LastOpNo &&
1419 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1420 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1421
1422 // Find out what operand from the asmparser that this MCInst operand
1423 // comes from.
1424 switch (CGA.ResultOperands[AliasOpNo].Kind) {
1425 default: assert(0 && "unexpected InstAlias operand kind");
1426 case CodeGenInstAlias::ResultOperand::K_Record: {
1427 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
1428 int SrcOperand = FindAsmOperand(Name, SubIdx);
1429 if (SrcOperand == -1)
1430 throw TGError(TheDef->getLoc(), "Instruction '" +
1431 TheDef->getName() + "' has operand '" + OpName +
1432 "' that doesn't appear in asm string!");
1433 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1434 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1435 NumOperands));
1436 break;
1437 }
1438 case CodeGenInstAlias::ResultOperand::K_Imm: {
1439 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1440 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1441 break;
1442 }
1443 case CodeGenInstAlias::ResultOperand::K_Reg: {
1444 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1445 ResOperands.push_back(ResOperand::getRegOp(Reg));
1446 break;
1447 }
1448 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001449 }
Chris Lattner41409852010-11-06 07:31:43 +00001450 }
1451}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001452
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001453static void EmitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001454 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001455 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001456 // Write the convert function to a separate stream, so we can drop it after
1457 // the enum.
1458 std::string ConvertFnBody;
1459 raw_string_ostream CvtOS(ConvertFnBody);
1460
Daniel Dunbar20927f22009-08-07 08:26:05 +00001461 // Function we have already generated.
1462 std::set<std::string> GeneratedFns;
1463
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001464 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001465 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1466 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001467 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001468 << " const SmallVectorImpl<MCParsedAsmOperand*"
1469 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001470 CvtOS << " Inst.setOpcode(Opcode);\n";
1471 CvtOS << " switch (Kind) {\n";
1472 CvtOS << " default:\n";
1473
1474 // Start the enum, which we will generate inline.
1475
Chris Lattnerd51257a2010-11-02 23:18:43 +00001476 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001477 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001478
Chris Lattner98986712010-01-14 22:21:20 +00001479 // TargetOperandClass - This is the target's operand class, like X86Operand.
1480 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001481
Chris Lattner22bc5c42010-11-01 05:06:45 +00001482 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001483 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001484 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001485
Daniel Dunbarcf120672011-02-04 17:12:15 +00001486 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001487 std::string AsmMatchConverter =
1488 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001489 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001490 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001491 II.ConversionFnKind = Signature;
1492
1493 // Check if we have already generated this signature.
1494 if (!GeneratedFns.insert(Signature).second)
1495 continue;
1496
1497 // If not, emit it now. Add to the enum list.
1498 OS << " " << Signature << ",\n";
1499
1500 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001501 CvtOS << " return " << AsmMatchConverter
1502 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001503 continue;
1504 }
1505
Daniel Dunbar20927f22009-08-07 08:26:05 +00001506 // Build the conversion function signature.
1507 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001508 std::string CaseBody;
1509 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001510
Chris Lattnerdda855d2010-11-02 21:49:44 +00001511 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001512 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1513 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001514
Chris Lattner1d13bda2010-11-04 00:43:46 +00001515 // Generate code to populate each result operand.
1516 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001517 case MatchableInfo::ResOperand::RenderAsmOperand: {
1518 // This comes from something we parsed.
1519 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001520
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001521 // Registers are always converted the same, don't duplicate the
1522 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001523 Signature += "__";
1524 if (Op.Class->isRegisterClass())
1525 Signature += "Reg";
1526 else
1527 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001528 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001529 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001530
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001531 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001532 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001533 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001534 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001535 }
Bob Wilson828295b2011-01-26 21:26:19 +00001536
Chris Lattner1d13bda2010-11-04 00:43:46 +00001537 case MatchableInfo::ResOperand::TiedOperand: {
1538 // If this operand is tied to a previous one, just copy the MCInst
1539 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001540 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001541 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001542 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001543 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1544 Signature += "__Tie" + utostr(TiedOp);
1545 break;
1546 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001547 case MatchableInfo::ResOperand::ImmOperand: {
1548 int64_t Val = OpInfo.ImmVal;
1549 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1550 Signature += "__imm" + itostr(Val);
1551 break;
1552 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001553 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001554 if (OpInfo.Register == 0) {
1555 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1556 Signature += "__reg0";
1557 } else {
1558 std::string N = getQualifiedName(OpInfo.Register);
1559 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1560 Signature += "__reg" + OpInfo.Register->getName();
1561 }
Bob Wilson828295b2011-01-26 21:26:19 +00001562 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001563 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001564 }
Bob Wilson828295b2011-01-26 21:26:19 +00001565
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001566 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001567
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001568 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001569 if (!GeneratedFns.insert(Signature).second)
1570 continue;
1571
Chris Lattnerdda855d2010-11-02 21:49:44 +00001572 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001573 OS << " " << Signature << ",\n";
1574
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001575 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001576 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001577 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001578 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001579
1580 // Finish the convert function.
1581
1582 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001583 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001584 CvtOS << "}\n\n";
1585
1586 // Finish the enum, and drop the convert function after it.
1587
1588 OS << " NumConversionVariants\n";
1589 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001590
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001591 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001592}
1593
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001594/// EmitMatchClassEnumeration - Emit the enumeration for match class kinds.
1595static void EmitMatchClassEnumeration(CodeGenTarget &Target,
1596 std::vector<ClassInfo*> &Infos,
1597 raw_ostream &OS) {
1598 OS << "namespace {\n\n";
1599
1600 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1601 << "/// instruction matching.\n";
1602 OS << "enum MatchClassKind {\n";
1603 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001604 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001605 ie = Infos.end(); it != ie; ++it) {
1606 ClassInfo &CI = **it;
1607 OS << " " << CI.Name << ", // ";
1608 if (CI.Kind == ClassInfo::Token) {
1609 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001610 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001611 if (!CI.ValueName.empty())
1612 OS << "register class '" << CI.ValueName << "'\n";
1613 else
1614 OS << "derived register class\n";
1615 } else {
1616 OS << "user defined class '" << CI.ValueName << "'\n";
1617 }
1618 }
1619 OS << " NumMatchClassKinds\n";
1620 OS << "};\n\n";
1621
1622 OS << "}\n\n";
1623}
1624
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001625/// EmitValidateOperandClass - Emit the function to validate an operand class.
1626static void EmitValidateOperandClass(AsmMatcherInfo &Info,
1627 raw_ostream &OS) {
1628 OS << "static bool ValidateOperandClass(MCParsedAsmOperand *GOp, "
1629 << "MatchClassKind Kind) {\n";
1630 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001631 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001632
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001633 // Check for Token operands first.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001634 OS << " if (Operand.isToken())\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001635 OS << " return MatchTokenString(Operand.getToken()) == Kind;\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001636
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001637 // Check for register operands, including sub-classes.
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001638 OS << " if (Operand.isReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001639 OS << " MatchClassKind OpKind;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001640 OS << " switch (Operand.getReg()) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001641 OS << " default: OpKind = InvalidMatchClass; break;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001642 for (std::map<Record*, ClassInfo*>::iterator
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001643 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1644 it != ie; ++it)
Chris Lattner02bcbc92010-11-01 01:37:30 +00001645 OS << " case " << Info.Target.getName() << "::"
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001646 << it->first->getName() << ": OpKind = " << it->second->Name
1647 << "; break;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001648 OS << " }\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001649 OS << " return IsSubclass(OpKind, Kind);\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001650 OS << " }\n\n";
1651
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001652 // Check the user classes. We don't care what order since we're only
1653 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001654 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001655 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001656 ClassInfo &CI = **it;
1657
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001658 if (!CI.isUserClass())
1659 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001660
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001661 OS << " // '" << CI.ClassName << "' class\n";
1662 OS << " if (Kind == " << CI.Name
1663 << " && Operand." << CI.PredicateMethod << "()) {\n";
1664 OS << " return true;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001665 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001666 }
Bob Wilson828295b2011-01-26 21:26:19 +00001667
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001668 OS << " return false;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001669 OS << "}\n\n";
1670}
1671
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001672/// EmitIsSubclass - Emit the subclass predicate function.
1673static void EmitIsSubclass(CodeGenTarget &Target,
1674 std::vector<ClassInfo*> &Infos,
1675 raw_ostream &OS) {
1676 OS << "/// IsSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1677 OS << "static bool IsSubclass(MatchClassKind A, MatchClassKind B) {\n";
1678 OS << " if (A == B)\n";
1679 OS << " return true;\n\n";
1680
1681 OS << " switch (A) {\n";
1682 OS << " default:\n";
1683 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001684 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001685 ie = Infos.end(); it != ie; ++it) {
1686 ClassInfo &A = **it;
1687
1688 if (A.Kind != ClassInfo::Token) {
1689 std::vector<StringRef> SuperClasses;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001690 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001691 ie = Infos.end(); it != ie; ++it) {
1692 ClassInfo &B = **it;
1693
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001694 if (&A != &B && A.isSubsetOf(B))
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001695 SuperClasses.push_back(B.Name);
1696 }
1697
1698 if (SuperClasses.empty())
1699 continue;
1700
1701 OS << "\n case " << A.Name << ":\n";
1702
1703 if (SuperClasses.size() == 1) {
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001704 OS << " return B == " << SuperClasses.back() << ";\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001705 continue;
1706 }
1707
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001708 OS << " switch (B) {\n";
1709 OS << " default: return false;\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001710 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001711 OS << " case " << SuperClasses[i] << ": return true;\n";
1712 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001713 }
1714 }
1715 OS << " }\n";
1716 OS << "}\n\n";
1717}
1718
Daniel Dunbar245f0582009-08-08 21:22:41 +00001719/// EmitMatchTokenString - Emit the function to match a token string to the
1720/// appropriate match class value.
1721static void EmitMatchTokenString(CodeGenTarget &Target,
1722 std::vector<ClassInfo*> &Infos,
1723 raw_ostream &OS) {
1724 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001725 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001726 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001727 ie = Infos.end(); it != ie; ++it) {
1728 ClassInfo &CI = **it;
1729
1730 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001731 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1732 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001733 }
1734
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001735 OS << "static MatchClassKind MatchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001736
Chris Lattner5845e5c2010-09-06 02:01:51 +00001737 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001738
1739 OS << " return InvalidMatchClass;\n";
1740 OS << "}\n\n";
1741}
Chris Lattner70add882009-08-08 20:02:57 +00001742
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001743/// EmitMatchRegisterName - Emit the function to match a string to the target
1744/// specific register enum.
1745static void EmitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
1746 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001747 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001748 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001749 const std::vector<CodeGenRegister*> &Regs =
1750 Target.getRegBank().getRegisters();
1751 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1752 const CodeGenRegister *Reg = Regs[i];
1753 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001754 continue;
1755
Chris Lattner5845e5c2010-09-06 02:01:51 +00001756 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001757 Reg->TheDef->getValueAsString("AsmName"),
1758 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001759 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001760
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001761 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001762
Chris Lattner5845e5c2010-09-06 02:01:51 +00001763 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001764
Daniel Dunbar245f0582009-08-08 21:22:41 +00001765 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001766 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001767}
Daniel Dunbara027d222009-07-31 02:32:59 +00001768
Daniel Dunbar54074b52010-07-19 05:44:09 +00001769/// EmitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
1770/// definitions.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001771static void EmitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001772 raw_ostream &OS) {
1773 OS << "// Flags for subtarget features that participate in "
1774 << "instruction matching.\n";
1775 OS << "enum SubtargetFeatureFlag {\n";
1776 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1777 it = Info.SubtargetFeatures.begin(),
1778 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1779 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001780 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001781 }
1782 OS << " Feature_None = 0\n";
1783 OS << "};\n\n";
1784}
1785
1786/// EmitComputeAvailableFeatures - Emit the function to compute the list of
1787/// available features given a subtarget.
Chris Lattner02bcbc92010-11-01 01:37:30 +00001788static void EmitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001789 raw_ostream &OS) {
1790 std::string ClassName =
1791 Info.AsmParser->getValueAsString("AsmParserClassName");
1792
Chris Lattner02bcbc92010-11-01 01:37:30 +00001793 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
1794 << "ComputeAvailableFeatures(const " << Info.Target.getName()
Daniel Dunbar54074b52010-07-19 05:44:09 +00001795 << "Subtarget *Subtarget) const {\n";
1796 OS << " unsigned Features = 0;\n";
1797 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1798 it = Info.SubtargetFeatures.begin(),
1799 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1800 SubtargetFeatureInfo &SFI = *it->second;
1801 OS << " if (" << SFI.TheDef->getValueAsString("CondString")
1802 << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00001803 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001804 }
1805 OS << " return Features;\n";
1806 OS << "}\n\n";
1807}
1808
Chris Lattner6fa152c2010-10-30 20:15:02 +00001809static std::string GetAliasRequiredFeatures(Record *R,
1810 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00001811 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00001812 std::string Result;
1813 unsigned NumFeatures = 0;
1814 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00001815 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00001816
Chris Lattner4a74ee72010-11-01 02:09:21 +00001817 if (F == 0)
1818 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
1819 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00001820
Chris Lattner4a74ee72010-11-01 02:09:21 +00001821 if (NumFeatures)
1822 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00001823
Chris Lattner4a74ee72010-11-01 02:09:21 +00001824 Result += F->getEnumName();
1825 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00001826 }
Bob Wilson828295b2011-01-26 21:26:19 +00001827
Chris Lattner693173f2010-10-30 19:23:13 +00001828 if (NumFeatures > 1)
1829 Result = '(' + Result + ')';
1830 return Result;
1831}
1832
Chris Lattner674c1dc2010-10-30 17:36:36 +00001833/// EmitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00001834/// emit a function for them and return true, otherwise return false.
Chris Lattner0aed1e72010-10-30 20:07:57 +00001835static bool EmitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00001836 // Ignore aliases when match-prefix is set.
1837 if (!MatchPrefix.empty())
1838 return false;
1839
Chris Lattner674c1dc2010-10-30 17:36:36 +00001840 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00001841 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00001842 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001843
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00001844 OS << "static void ApplyMnemonicAliases(StringRef &Mnemonic, "
1845 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001846
Chris Lattner4fd32c62010-10-30 18:56:12 +00001847 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
1848 // iteration order of the map is stable.
1849 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00001850
Chris Lattner674c1dc2010-10-30 17:36:36 +00001851 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
1852 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00001853 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00001854 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00001855
1856 // Process each alias a "from" mnemonic at a time, building the code executed
1857 // by the string remapper.
1858 std::vector<StringMatcher::StringPair> Cases;
1859 for (std::map<std::string, std::vector<Record*> >::iterator
1860 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
1861 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00001862 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00001863
1864 // Loop through each alias and emit code that handles each case. If there
1865 // are two instructions without predicates, emit an error. If there is one,
1866 // emit it last.
1867 std::string MatchCode;
1868 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00001869
Chris Lattner693173f2010-10-30 19:23:13 +00001870 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
1871 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00001872 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00001873
Chris Lattner693173f2010-10-30 19:23:13 +00001874 // If this unconditionally matches, remember it for later and diagnose
1875 // duplicates.
1876 if (FeatureMask.empty()) {
1877 if (AliasWithNoPredicate != -1) {
1878 // We can't have two aliases from the same mnemonic with no predicate.
1879 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
1880 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00001881 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00001882 }
Bob Wilson828295b2011-01-26 21:26:19 +00001883
Chris Lattner693173f2010-10-30 19:23:13 +00001884 AliasWithNoPredicate = i;
1885 continue;
1886 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00001887 if (R->getValueAsString("ToMnemonic") == I->first)
1888 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00001889
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001890 if (!MatchCode.empty())
1891 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00001892 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
1893 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00001894 }
Bob Wilson828295b2011-01-26 21:26:19 +00001895
Chris Lattner693173f2010-10-30 19:23:13 +00001896 if (AliasWithNoPredicate != -1) {
1897 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00001898 if (!MatchCode.empty())
1899 MatchCode += "else\n ";
1900 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00001901 }
Bob Wilson828295b2011-01-26 21:26:19 +00001902
Chris Lattner693173f2010-10-30 19:23:13 +00001903 MatchCode += "return;";
1904
1905 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00001906 }
Bob Wilson828295b2011-01-26 21:26:19 +00001907
Chris Lattner674c1dc2010-10-30 17:36:36 +00001908 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00001909 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001910
Chris Lattner7fd44892010-10-30 18:48:18 +00001911 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00001912}
1913
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001914static void EmitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
1915 const AsmMatcherInfo &Info, StringRef ClassName) {
1916 // Emit the static custom operand parsing table;
1917 OS << "namespace {\n";
1918 OS << " struct OperandMatchEntry {\n";
1919 OS << " const char *Mnemonic;\n";
1920 OS << " unsigned OperandMask;\n";
1921 OS << " MatchClassKind Class;\n";
1922 OS << " unsigned RequiredFeatures;\n";
1923 OS << " };\n\n";
1924
1925 OS << " // Predicate for searching for an opcode.\n";
1926 OS << " struct LessOpcodeOperand {\n";
1927 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
1928 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
1929 OS << " }\n";
1930 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
1931 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
1932 OS << " }\n";
1933 OS << " bool operator()(const OperandMatchEntry &LHS,";
1934 OS << " const OperandMatchEntry &RHS) {\n";
1935 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
1936 OS << " }\n";
1937 OS << " };\n";
1938
1939 OS << "} // end anonymous namespace.\n\n";
1940
1941 OS << "static const OperandMatchEntry OperandMatchTable["
1942 << Info.OperandMatchInfo.size() << "] = {\n";
1943
1944 OS << " /* Mnemonic, Operand List Mask, Operand Class, Features */\n";
1945 for (std::vector<OperandMatchEntry>::const_iterator it =
1946 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
1947 it != ie; ++it) {
1948 const OperandMatchEntry &OMI = *it;
1949 const MatchableInfo &II = *OMI.MI;
1950
1951 OS << " { \"" << II.Mnemonic << "\""
1952 << ", " << OMI.OperandMask;
1953
1954 OS << " /* ";
1955 bool printComma = false;
1956 for (int i = 0, e = 31; i !=e; ++i)
1957 if (OMI.OperandMask & (1 << i)) {
1958 if (printComma)
1959 OS << ", ";
1960 OS << i;
1961 printComma = true;
1962 }
1963 OS << " */";
1964
1965 OS << ", " << OMI.CI->Name
1966 << ", ";
1967
1968 // Write the required features mask.
1969 if (!II.RequiredFeatures.empty()) {
1970 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
1971 if (i) OS << "|";
1972 OS << II.RequiredFeatures[i]->getEnumName();
1973 }
1974 } else
1975 OS << "0";
1976 OS << " },\n";
1977 }
1978 OS << "};\n\n";
1979
1980 // Emit the operand class switch to call the correct custom parser for
1981 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00001982 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
1983 << Target.getName() << ClassName << "::\n"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001984 << "TryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
1985 << " &Operands,\n unsigned MCK) {\n\n"
1986 << " switch(MCK) {\n";
1987
1988 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
1989 ie = Info.Classes.end(); it != ie; ++it) {
1990 ClassInfo *CI = *it;
1991 if (CI->ParserMethod.empty())
1992 continue;
1993 OS << " case " << CI->Name << ":\n"
1994 << " return " << CI->ParserMethod << "(Operands);\n";
1995 }
1996
1997 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00001998 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001999 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002000 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002001 OS << "}\n\n";
2002
2003 // Emit the static custom operand parser. This code is very similar with
2004 // the other matcher. Also use MatchResultTy here just in case we go for
2005 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002006 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002007 << Target.getName() << ClassName << "::\n"
2008 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2009 << " &Operands,\n StringRef Mnemonic) {\n";
2010
2011 // Emit code to get the available features.
2012 OS << " // Get the current feature set.\n";
2013 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2014
2015 OS << " // Get the next operand index.\n";
2016 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2017
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002018 // Emit code to search the table.
2019 OS << " // Search the table.\n";
2020 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2021 OS << " MnemonicRange =\n";
2022 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2023 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2024 << " LessOpcodeOperand());\n\n";
2025
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002026 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002027 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002028
2029 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2030 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2031
2032 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
2033 OS << " assert(Mnemonic == it->Mnemonic);\n\n";
2034
2035 // Emit check that the required features are available.
2036 OS << " // check if the available features match\n";
2037 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2038 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002039 OS << " continue;\n";
2040 OS << " }\n\n";
2041
2042 // Emit check to ensure the operand number matches.
2043 OS << " // check if the operand in question has a custom parser.\n";
2044 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2045 OS << " continue;\n\n";
2046
2047 // Emit call to the custom parser method
2048 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002049 OS << " OperandMatchResultTy Result = ";
2050 OS << "TryCustomParseOperand(Operands, it->Class);\n";
2051 OS << " if (Result != MatchOperand_NoMatch)\n";
2052 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002053 OS << " }\n\n";
2054
Jim Grosbachf922c472011-02-12 01:34:40 +00002055 OS << " // Okay, we had no match.\n";
2056 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002057 OS << "}\n\n";
2058}
2059
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002060void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002061 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002062 Record *AsmParser = Target.getAsmParser();
2063 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2064
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002065 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002066 AsmMatcherInfo Info(AsmParser, Target, Records);
Chris Lattner02bcbc92010-11-01 01:37:30 +00002067 Info.BuildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002068
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002069 // Sort the instruction table using the partial order on classes. We use
2070 // stable_sort to ensure that ambiguous instructions are still
2071 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002072 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2073 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002074
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002075 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002076 for (std::vector<MatchableInfo*>::iterator
2077 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002078 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002079 (*it)->dump();
2080 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002081
Chris Lattner22bc5c42010-11-01 05:06:45 +00002082 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002083 DEBUG_WITH_TYPE("ambiguous_instrs", {
2084 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002085 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002086 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002087 MatchableInfo &A = *Info.Matchables[i];
2088 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002089
Bob Wilson1f64ac42011-01-26 21:26:21 +00002090 if (A.CouldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002091 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002092 A.dump();
2093 errs() << "\nis incomparable with:\n";
2094 B.dump();
2095 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002096 ++NumAmbiguous;
2097 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002098 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002099 }
Chris Lattner87410362010-09-06 20:21:47 +00002100 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002101 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002102 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002103 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002104
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002105 // Compute the information on the custom operand parsing.
2106 Info.BuildOperandMatchInfo();
2107
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002108 // Write the output.
2109
2110 EmitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2111
Chris Lattner0692ee62010-09-06 19:11:01 +00002112 // Information for the class declaration.
2113 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2114 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002115 OS << " // This should be included into the middle of the declaration of\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00002116 OS << " // your subclasses implementation of TargetAsmParser.\n";
Chris Lattner0692ee62010-09-06 19:11:01 +00002117 OS << " unsigned ComputeAvailableFeatures(const " <<
2118 Target.getName() << "Subtarget *Subtarget) const;\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00002119 OS << " enum MatchResultTy {\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002120 OS << " Match_ConversionFail,\n";
2121 OS << " Match_InvalidOperand,\n";
2122 OS << " Match_MissingFeature,\n";
2123 OS << " Match_MnemonicFail,\n";
2124 OS << " Match_Success\n";
Chris Lattner79ed3f72010-09-06 19:22:17 +00002125 OS << " };\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002126 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2127 << "unsigned Opcode,\n"
2128 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2129 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002130 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002131 OS << " MatchResultTy MatchInstructionImpl(\n";
2132 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002133 OS << " MCInst &Inst, unsigned &ErrorInfo);\n";
2134
2135 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002136 OS << "\n enum OperandMatchResultTy {\n";
2137 OS << " MatchOperand_Success, // operand matched successfully\n";
2138 OS << " MatchOperand_NoMatch, // operand did not match\n";
2139 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2140 OS << " };\n";
2141 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002142 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2143 OS << " StringRef Mnemonic);\n";
2144
Jim Grosbachf922c472011-02-12 01:34:40 +00002145 OS << " OperandMatchResultTy TryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002146 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2147 OS << " unsigned MCK);\n\n";
2148 }
2149
Chris Lattner0692ee62010-09-06 19:11:01 +00002150 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2151
Chris Lattner0692ee62010-09-06 19:11:01 +00002152 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2153 OS << "#undef GET_REGISTER_MATCHER\n\n";
2154
Daniel Dunbar54074b52010-07-19 05:44:09 +00002155 // Emit the subtarget feature enumeration.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002156 EmitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002157
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002158 // Emit the function to match a register name to number.
2159 EmitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002160
2161 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002162
Chris Lattner0692ee62010-09-06 19:11:01 +00002163
2164 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2165 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002166
Chris Lattner7fd44892010-10-30 18:48:18 +00002167 // Generate the function that remaps for mnemonic aliases.
Chris Lattner0aed1e72010-10-30 20:07:57 +00002168 bool HasMnemonicAliases = EmitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002169
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002170 // Generate the unified function to convert operands into an MCInst.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002171 EmitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002172
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002173 // Emit the enumeration for classes which participate in matching.
2174 EmitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002175
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002176 // Emit the routine to match token strings to their match class.
2177 EmitMatchTokenString(Target, Info.Classes, OS);
2178
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002179 // Emit the subclass predicate routine.
2180 EmitIsSubclass(Target, Info.Classes, OS);
2181
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002182 // Emit the routine to validate an operand against a match class.
2183 EmitValidateOperandClass(Info, OS);
2184
Daniel Dunbar54074b52010-07-19 05:44:09 +00002185 // Emit the available features compute function.
Chris Lattner02bcbc92010-11-01 01:37:30 +00002186 EmitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002187
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002188
2189 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002190 for (std::vector<MatchableInfo*>::const_iterator it =
2191 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002192 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002193 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002194
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002195 // Emit the static match table; unused classes get initalized to 0 which is
2196 // guaranteed to be InvalidMatchClass.
2197 //
2198 // FIXME: We can reduce the size of this table very easily. First, we change
2199 // it so that store the kinds in separate bit-fields for each index, which
2200 // only needs to be the max width used for classes at that index (we also need
2201 // to reject based on this during classification). If we then make sure to
2202 // order the match kinds appropriately (putting mnemonics last), then we
2203 // should only end up using a few bits for each class, especially the ones
2204 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002205 OS << "namespace {\n";
2206 OS << " struct MatchEntry {\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002207 OS << " unsigned Opcode;\n";
Chris Lattnere206fcf2010-09-06 21:01:37 +00002208 OS << " const char *Mnemonic;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002209 OS << " ConversionKind ConvertFn;\n";
2210 OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002211 OS << " unsigned RequiredFeatures;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002212 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002213
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002214 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002215 OS << " struct LessOpcode {\n";
2216 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
2217 OS << " return StringRef(LHS.Mnemonic) < RHS;\n";
2218 OS << " }\n";
2219 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
2220 OS << " return LHS < StringRef(RHS.Mnemonic);\n";
2221 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002222 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
2223 OS << " return StringRef(LHS.Mnemonic) < StringRef(RHS.Mnemonic);\n";
2224 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002225 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002226
Chris Lattner96352e52010-09-06 21:08:38 +00002227 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002228
Chris Lattner96352e52010-09-06 21:08:38 +00002229 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002230 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002231
Chris Lattner22bc5c42010-11-01 05:06:45 +00002232 for (std::vector<MatchableInfo*>::const_iterator it =
2233 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002234 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002235 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002236
Chris Lattner662e5a32010-11-06 07:14:44 +00002237 OS << " { " << Target.getName() << "::"
2238 << II.getResultInst()->TheDef->getName() << ", \"" << II.Mnemonic << "\""
2239 << ", " << II.ConversionFnKind << ", { ";
Chris Lattner3116fef2010-11-02 01:03:43 +00002240 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00002241 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002242
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002243 if (i) OS << ", ";
2244 OS << Op.Class->Name;
Daniel Dunbar20927f22009-08-07 08:26:05 +00002245 }
Daniel Dunbar54074b52010-07-19 05:44:09 +00002246 OS << " }, ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002247
Daniel Dunbar54074b52010-07-19 05:44:09 +00002248 // Write the required features mask.
2249 if (!II.RequiredFeatures.empty()) {
2250 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2251 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002252 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002253 }
2254 } else
2255 OS << "0";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002256
Daniel Dunbar54074b52010-07-19 05:44:09 +00002257 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002258 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002259
Chris Lattner96352e52010-09-06 21:08:38 +00002260 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002261
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002262 // A method to determine if a mnemonic is in the list.
2263 OS << "bool " << Target.getName() << ClassName << "::\n"
2264 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2265 OS << " // Search the table.\n";
2266 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2267 OS << " std::equal_range(MatchTable, MatchTable+"
2268 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2269 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2270 OS << "}\n\n";
2271
Chris Lattner96352e52010-09-06 21:08:38 +00002272 // Finally, build the match function.
2273 OS << Target.getName() << ClassName << "::MatchResultTy "
2274 << Target.getName() << ClassName << "::\n"
2275 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2276 << " &Operands,\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002277 OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002278
2279 // Emit code to get the available features.
2280 OS << " // Get the current feature set.\n";
2281 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2282
Chris Lattner674c1dc2010-10-30 17:36:36 +00002283 OS << " // Get the instruction mnemonic, which is the first token.\n";
2284 OS << " StringRef Mnemonic = ((" << Target.getName()
2285 << "Operand*)Operands[0])->getToken();\n\n";
2286
Chris Lattner7fd44892010-10-30 18:48:18 +00002287 if (HasMnemonicAliases) {
2288 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
2289 OS << " ApplyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
2290 }
Bob Wilson828295b2011-01-26 21:26:19 +00002291
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002292 // Emit code to compute the class list for this operand vector.
2293 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002294 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2295 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2296 OS << " return Match_InvalidOperand;\n";
2297 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002298
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002299 OS << " // Some state to try to produce better error messages.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002300 OS << " bool HadMatchOtherThanFeatures = false;\n\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002301 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002302 OS << " // wrong for all instances of the instruction.\n";
2303 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002304
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002305 // Emit code to search the table.
2306 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002307 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2308 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002309 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002310
Chris Lattnera008e8a2010-09-06 21:54:15 +00002311 OS << " // Return a more specific error code if no mnemonics match.\n";
2312 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2313 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002314
Chris Lattner2b1f9432010-09-06 21:22:45 +00002315 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002316 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002317 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002318
Gabor Greife53ee3b2010-09-07 06:06:06 +00002319 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Chris Lattner44b0daa2010-09-06 21:25:43 +00002320 OS << " assert(Mnemonic == it->Mnemonic);\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002321
Daniel Dunbar54074b52010-07-19 05:44:09 +00002322 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002323 OS << " bool OperandsValid = true;\n";
2324 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002325 OS << " if (i + 1 >= Operands.size()) {\n";
2326 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002327 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002328 OS << " }\n";
2329 OS << " if (ValidateOperandClass(Operands[i+1], it->Classes[i]))\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002330 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002331 OS << " // If this operand is broken for all of the instances of this\n";
2332 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Kevin Enderby79fcb6d2011-02-02 18:20:55 +00002333 OS << " if (it == MnemonicRange.first || ErrorInfo <= i+1)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002334 OS << " ErrorInfo = i+1;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002335 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2336 OS << " OperandsValid = false;\n";
2337 OS << " break;\n";
2338 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002339
Chris Lattnerce4a3352010-09-06 22:11:18 +00002340 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002341
2342 // Emit check that the required features are available.
2343 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2344 << "!= it->RequiredFeatures) {\n";
2345 OS << " HadMatchOtherThanFeatures = true;\n";
2346 OS << " continue;\n";
2347 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002348 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002349 OS << " // We have selected a definite instruction, convert the parsed\n"
2350 << " // operands into the appropriate MCInst.\n";
2351 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2352 << " it->Opcode, Operands))\n";
2353 OS << " return Match_ConversionFail;\n";
2354 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002355
2356 // Call the post-processing function, if used.
2357 std::string InsnCleanupFn =
2358 AsmParser->getValueAsString("AsmParserInstCleanup");
2359 if (!InsnCleanupFn.empty())
2360 OS << " " << InsnCleanupFn << "(Inst);\n";
2361
Chris Lattner79ed3f72010-09-06 19:22:17 +00002362 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002363 OS << " }\n\n";
2364
Chris Lattnerec6789f2010-09-06 20:08:02 +00002365 OS << " // Okay, we had no match. Try to return a useful error code.\n";
2366 OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n";
Chris Lattnera008e8a2010-09-06 21:54:15 +00002367 OS << " return Match_InvalidOperand;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002368 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002369
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002370 if (Info.OperandMatchInfo.size())
2371 EmitCustomOperandParsing(OS, Target, Info, ClassName);
2372
Chris Lattner0692ee62010-09-06 19:11:01 +00002373 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002374}