blob: ff04d63d00defb5d70661fe9d6d4df4f5d367f6d [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
Craig Topperbe480ff2012-09-18 01:13:36 +000080// identifiers that need to be mapped to specific values in the final encoded
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000081// 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
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000099#include "CodeGenTarget.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000100#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc07bd402010-11-04 02:11:18 +0000101#include "llvm/ADT/PointerUnion.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +0000102#include "llvm/ADT/STLExtras.h"
Chris Lattner1de88232010-11-01 01:47:07 +0000103#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000104#include "llvm/ADT/SmallVector.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000105#include "llvm/ADT/StringExtras.h"
106#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000107#include "llvm/Support/Debug.h"
Craig Topper655b8de2012-02-05 07:21:30 +0000108#include "llvm/Support/ErrorHandling.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +0000109#include "llvm/TableGen/Error.h"
110#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +0000111#include "llvm/TableGen/StringMatcher.h"
Craig Topperaae60d12013-08-29 05:09:55 +0000112#include "llvm/TableGen/StringToOffsetTable.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000113#include "llvm/TableGen/TableGenBackend.h"
114#include <cassert>
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000115#include <map>
116#include <set>
Aaron Ballman54911a52013-07-15 16:53:32 +0000117#include <sstream>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000118using namespace llvm;
119
Daniel Dunbar27249152009-08-07 20:33:39 +0000120static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000121MatchPrefix("match-prefix", cl::init(""),
122 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000123
Daniel Dunbar20927f22009-08-07 08:26:05 +0000124namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000125class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000126struct SubtargetFeatureInfo;
127
Tim Northover03f91972013-09-16 16:43:19 +0000128// Register sets are used as keys in some second-order sets TableGen creates
129// when generating its data structures. This means that the order of two
130// RegisterSets can be seen in the outputted AsmMatcher tables occasionally, and
131// can even affect compiler output (at least seen in diagnostics produced when
132// all matches fail). So we use a type that sorts them consistently.
133typedef std::set<Record*, LessRecordByID> RegisterSet;
134
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000135class AsmMatcherEmitter {
136 RecordKeeper &Records;
137public:
138 AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
139
140 void run(raw_ostream &o);
141};
142
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000143/// ClassInfo - Helper class for storing the information about a particular
144/// class of operands which can be matched.
145struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000146 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000147 /// Invalid kind, for use as a sentinel value.
148 Invalid = 0,
149
150 /// The class for a particular token.
151 Token,
152
153 /// The (first) register class, subsequent register classes are
154 /// RegisterClass0+1, and so on.
155 RegisterClass0,
156
157 /// The (first) user defined class, subsequent user defined classes are
158 /// UserClass0+1, and so on.
159 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000160 };
161
162 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
163 /// N) for the Nth user defined class.
164 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000165
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000166 /// SuperClasses - The super classes of this class. Note that for simplicities
167 /// sake user operands only record their immediate super class, while register
168 /// operands include all superclasses.
169 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000170
Daniel Dunbar6745d422009-08-09 05:18:30 +0000171 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000172 std::string Name;
173
Daniel Dunbar6745d422009-08-09 05:18:30 +0000174 /// ClassName - The unadorned generic name for this class (e.g., Token).
175 std::string ClassName;
176
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000177 /// ValueName - The name of the value this class represents; for a token this
178 /// is the literal token string, for an operand it is the TableGen class (or
179 /// empty if this is a derived class).
180 std::string ValueName;
181
182 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000183 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000184 std::string PredicateMethod;
185
186 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000187 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000188 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000189
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000190 /// ParserMethod - The name of the operand method to do a target specific
191 /// parsing on the operand.
192 std::string ParserMethod;
193
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000194 /// For register classes, the records for all the registers in this class.
Tim Northover03f91972013-09-16 16:43:19 +0000195 RegisterSet Registers;
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000196
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +0000197 /// For custom match classes, he diagnostic kind for when the predicate fails.
198 std::string DiagnosticType;
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000199public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000200 /// isRegisterClass() - Check if this is a register class.
201 bool isRegisterClass() const {
202 return Kind >= RegisterClass0 && Kind < UserClass0;
203 }
204
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000205 /// isUserClass() - Check if this is a user defined class.
206 bool isUserClass() const {
207 return Kind >= UserClass0;
208 }
209
Dmitri Gribenko4e0ae442012-09-15 20:22:05 +0000210 /// isRelatedTo - Check whether this class is "related" to \p RHS. Classes
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000211 /// are related if they are in the same class hierarchy.
212 bool isRelatedTo(const ClassInfo &RHS) const {
213 // Tokens are only related to tokens.
214 if (Kind == Token || RHS.Kind == Token)
215 return Kind == Token && RHS.Kind == Token;
216
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000217 // Registers classes are only related to registers classes, and only if
218 // their intersection is non-empty.
219 if (isRegisterClass() || RHS.isRegisterClass()) {
220 if (!isRegisterClass() || !RHS.isRegisterClass())
221 return false;
222
Tim Northover03f91972013-09-16 16:43:19 +0000223 RegisterSet Tmp;
224 std::insert_iterator<RegisterSet> II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000225 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000226 RHS.Registers.begin(), RHS.Registers.end(),
Tim Northover03f91972013-09-16 16:43:19 +0000227 II, LessRecordByID());
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000228
229 return !Tmp.empty();
230 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000231
232 // Otherwise we have two users operands; they are related if they are in the
233 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000234 //
235 // FIXME: This is an oversimplification, they should only be related if they
236 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000237 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
238 const ClassInfo *Root = this;
239 while (!Root->SuperClasses.empty())
240 Root = Root->SuperClasses.front();
241
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000242 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000243 while (!RHSRoot->SuperClasses.empty())
244 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000245
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000246 return Root == RHSRoot;
247 }
248
Dmitri Gribenko4e0ae442012-09-15 20:22:05 +0000249 /// isSubsetOf - Test whether this class is a subset of \p RHS.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000250 bool isSubsetOf(const ClassInfo &RHS) const {
251 // This is a subset of RHS if it is the same class...
252 if (this == &RHS)
253 return true;
254
255 // ... or if any of its super classes are a subset of RHS.
256 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
257 ie = SuperClasses.end(); it != ie; ++it)
258 if ((*it)->isSubsetOf(RHS))
259 return true;
260
261 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000262 }
263
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000264 /// operator< - Compare two classes.
265 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000266 if (this == &RHS)
267 return false;
268
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000269 // Unrelated classes can be ordered by kind.
270 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000271 return Kind < RHS.Kind;
272
273 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000274 case Invalid:
Craig Topper655b8de2012-02-05 07:21:30 +0000275 llvm_unreachable("Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000276
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000277 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000278 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000279 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000280 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000281 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000282 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000283
284 // Otherwise, order by name to ensure we have a total ordering.
285 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000286 }
287 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000288};
289
Sean Silvab2df6102012-09-19 01:47:03 +0000290namespace {
291/// Sort ClassInfo pointers independently of pointer value.
292struct LessClassInfoPtr {
293 bool operator()(const ClassInfo *LHS, const ClassInfo *RHS) const {
294 return *LHS < *RHS;
295 }
296};
297}
298
Chris Lattner22bc5c42010-11-01 05:06:45 +0000299/// MatchableInfo - Helper class for storing the necessary information for an
300/// instruction or alias which is capable of being matched.
301struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000302 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000303 /// Token - This is the token that the operand came from.
304 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000305
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000306 /// The unique class instance this operand should match.
307 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000308
Chris Lattner567820c2010-11-04 01:42:59 +0000309 /// The operand name this is, if anything.
310 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000311
312 /// The suboperand index within SrcOpName, or -1 for the entire operand.
313 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000314
Devang Patel63faf822012-01-07 01:33:34 +0000315 /// Register record if this token is singleton register.
316 Record *SingletonReg;
317
Jim Grosbachf35307c2012-01-24 21:06:59 +0000318 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1),
Jim Grosbach11fc6462012-04-11 21:02:33 +0000319 SingletonReg(0) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000320 };
Bob Wilson828295b2011-01-26 21:26:19 +0000321
Chris Lattner1d13bda2010-11-04 00:43:46 +0000322 /// ResOperand - This represents a single operand in the result instruction
323 /// generated by the match. In cases (like addressing modes) where a single
324 /// assembler operand expands to multiple MCOperands, this represents the
325 /// single assembler operand, not the MCOperand.
326 struct ResOperand {
327 enum {
328 /// RenderAsmOperand - This represents an operand result that is
329 /// generated by calling the render method on the assembly operand. The
330 /// corresponding AsmOperand is specified by AsmOperandNum.
331 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000332
Chris Lattner1d13bda2010-11-04 00:43:46 +0000333 /// TiedOperand - This represents a result operand that is a duplicate of
334 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000335 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000336
Chris Lattner98c870f2010-11-06 19:25:43 +0000337 /// ImmOperand - This represents an immediate value that is dumped into
338 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000339 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000340
Chris Lattner90fd7972010-11-06 19:57:21 +0000341 /// RegOperand - This represents a fixed register that is dumped in.
342 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000343 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000344
Chris Lattner1d13bda2010-11-04 00:43:46 +0000345 union {
346 /// This is the operand # in the AsmOperands list that this should be
347 /// copied from.
348 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000349
Chris Lattner1d13bda2010-11-04 00:43:46 +0000350 /// TiedOperandNum - This is the (earlier) result operand that should be
351 /// copied from.
352 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000353
Chris Lattner98c870f2010-11-06 19:25:43 +0000354 /// ImmVal - This is the immediate value added to the instruction.
355 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000356
Chris Lattner90fd7972010-11-06 19:57:21 +0000357 /// Register - This is the register record.
358 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000359 };
Bob Wilson828295b2011-01-26 21:26:19 +0000360
Bob Wilsona49c7df2011-01-26 19:44:55 +0000361 /// MINumOperands - The number of MCInst operands populated by this
362 /// operand.
363 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000364
Bob Wilsona49c7df2011-01-26 19:44:55 +0000365 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000366 ResOperand X;
367 X.Kind = RenderAsmOperand;
368 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000369 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000370 return X;
371 }
Bob Wilson828295b2011-01-26 21:26:19 +0000372
Bob Wilsona49c7df2011-01-26 19:44:55 +0000373 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000374 ResOperand X;
375 X.Kind = TiedOperand;
376 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000377 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000378 return X;
379 }
Bob Wilson828295b2011-01-26 21:26:19 +0000380
Bob Wilsona49c7df2011-01-26 19:44:55 +0000381 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000382 ResOperand X;
383 X.Kind = ImmOperand;
384 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000385 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000386 return X;
387 }
Bob Wilson828295b2011-01-26 21:26:19 +0000388
Bob Wilsona49c7df2011-01-26 19:44:55 +0000389 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000390 ResOperand X;
391 X.Kind = RegOperand;
392 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000393 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000394 return X;
395 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000396 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000397
Devang Patel56315d32012-01-10 17:50:43 +0000398 /// AsmVariantID - Target's assembly syntax variant no.
399 int AsmVariantID;
400
Chris Lattner3b5aec62010-11-02 17:34:28 +0000401 /// TheDef - This is the definition of the instruction or InstAlias that this
402 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000403 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000404
Chris Lattnerc07bd402010-11-04 02:11:18 +0000405 /// DefRec - This is the definition that it came from.
406 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000407
Chris Lattner662e5a32010-11-06 07:14:44 +0000408 const CodeGenInstruction *getResultInst() const {
409 if (DefRec.is<const CodeGenInstruction*>())
410 return DefRec.get<const CodeGenInstruction*>();
411 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
412 }
Bob Wilson828295b2011-01-26 21:26:19 +0000413
Chris Lattner1d13bda2010-11-04 00:43:46 +0000414 /// ResOperands - This is the operand list that should be built for the result
415 /// MCInst.
Jim Grosbachb423d182012-04-19 17:52:34 +0000416 SmallVector<ResOperand, 8> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000417
418 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000419 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000420 std::string AsmString;
421
Chris Lattnerd19ec052010-11-02 17:30:52 +0000422 /// Mnemonic - This is the first token of the matched instruction, its
423 /// mnemonic.
424 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000425
Chris Lattner3116fef2010-11-02 01:03:43 +0000426 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000427 /// annotated with a class and where in the OperandList they were defined.
428 /// This directly corresponds to the tokenized AsmString after the mnemonic is
429 /// removed.
Jim Grosbachb423d182012-04-19 17:52:34 +0000430 SmallVector<AsmOperand, 8> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000431
Daniel Dunbar54074b52010-07-19 05:44:09 +0000432 /// Predicates - The required subtarget features to match this instruction.
433 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
434
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000435 /// ConversionFnKind - The enum value which is passed to the generated
Chad Rosier90e11f82012-09-05 01:02:38 +0000436 /// convertToMCInst to convert parsed operands into an MCInst for this
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000437 /// function.
438 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000439
Joey Gouly715d98d2013-09-12 10:28:05 +0000440 /// If this instruction is deprecated in some form.
441 bool HasDeprecation;
442
Chris Lattner22bc5c42010-11-01 05:06:45 +0000443 MatchableInfo(const CodeGenInstruction &CGI)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000444 : AsmVariantID(0), TheDef(CGI.TheDef), DefRec(&CGI),
Devang Patel56315d32012-01-10 17:50:43 +0000445 AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000446 }
447
Chris Lattner22bc5c42010-11-01 05:06:45 +0000448 MatchableInfo(const CodeGenInstAlias *Alias)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000449 : AsmVariantID(0), TheDef(Alias->TheDef), DefRec(Alias),
Devang Patel56315d32012-01-10 17:50:43 +0000450 AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000451 }
Bob Wilson828295b2011-01-26 21:26:19 +0000452
Jim Grosbachc1922c72012-04-19 23:59:23 +0000453 // Two-operand aliases clone from the main matchable, but mark the second
454 // operand as a tied operand of the first for purposes of the assembler.
455 void formTwoOperandAlias(StringRef Constraint);
456
Jim Grosbach8caecde2012-04-19 17:52:32 +0000457 void initialize(const AsmMatcherInfo &Info,
Jim Grosbachf35307c2012-01-24 21:06:59 +0000458 SmallPtrSet<Record*, 16> &SingletonRegisters,
Jim Grosbach11fc6462012-04-11 21:02:33 +0000459 int AsmVariantNo, std::string &RegisterPrefix);
Bob Wilson828295b2011-01-26 21:26:19 +0000460
Jim Grosbach8caecde2012-04-19 17:52:32 +0000461 /// validate - Return true if this matchable is a valid thing to match against
Chris Lattner22bc5c42010-11-01 05:06:45 +0000462 /// and perform a bunch of validity checking.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000463 bool validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000464
Jim Grosbachf35307c2012-01-24 21:06:59 +0000465 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Patel63faf822012-01-07 01:33:34 +0000466 /// if present, from specified token.
467 void
468 extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info,
469 std::string &RegisterPrefix);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000470
Jim Grosbach8caecde2012-04-19 17:52:32 +0000471 /// findAsmOperand - Find the AsmOperand with the specified name and
Bob Wilsona49c7df2011-01-26 19:44:55 +0000472 /// suboperand index.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000473 int findAsmOperand(StringRef N, int SubOpIdx) const {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000474 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
475 if (N == AsmOperands[i].SrcOpName &&
476 SubOpIdx == AsmOperands[i].SubOpIdx)
477 return i;
478 return -1;
479 }
Bob Wilson828295b2011-01-26 21:26:19 +0000480
Jim Grosbach8caecde2012-04-19 17:52:32 +0000481 /// findAsmOperandNamed - Find the first AsmOperand with the specified name.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000482 /// This does not check the suboperand index.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000483 int findAsmOperandNamed(StringRef N) const {
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000484 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
485 if (N == AsmOperands[i].SrcOpName)
486 return i;
487 return -1;
488 }
Bob Wilson828295b2011-01-26 21:26:19 +0000489
Jim Grosbach8caecde2012-04-19 17:52:32 +0000490 void buildInstructionResultOperands();
491 void buildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000492
Chris Lattner22bc5c42010-11-01 05:06:45 +0000493 /// operator< - Compare two matchables.
494 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000495 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000496 if (Mnemonic != RHS.Mnemonic)
497 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000498
Chris Lattner3116fef2010-11-02 01:03:43 +0000499 if (AsmOperands.size() != RHS.AsmOperands.size())
500 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000501
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000502 // Compare lexicographically by operand. The matcher validates that other
Jim Grosbach8caecde2012-04-19 17:52:32 +0000503 // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000504 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
505 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000506 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000507 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000508 return false;
509 }
510
Andrew Trick2b70dfa2012-08-29 03:52:57 +0000511 // Give matches that require more features higher precedence. This is useful
512 // because we cannot define AssemblerPredicates with the negation of
513 // processor features. For example, ARM v6 "nop" may be either a HINT or
514 // MOV. With v6, we want to match HINT. The assembler has no way to
515 // predicate MOV under "NoV6", but HINT will always match first because it
516 // requires V6 while MOV does not.
517 if (RequiredFeatures.size() != RHS.RequiredFeatures.size())
518 return RequiredFeatures.size() > RHS.RequiredFeatures.size();
519
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000520 return false;
521 }
522
Jim Grosbach8caecde2012-04-19 17:52:32 +0000523 /// couldMatchAmbiguouslyWith - Check whether this matchable could
Dmitri Gribenko4e0ae442012-09-15 20:22:05 +0000524 /// ambiguously match the same set of operands as \p RHS (without being a
Daniel Dunbar2b544812009-08-09 06:05:33 +0000525 /// strictly superior match).
Jim Grosbach8caecde2012-04-19 17:52:32 +0000526 bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000527 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000528 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000529 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000530
Daniel Dunbar2b544812009-08-09 06:05:33 +0000531 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000532 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000533 return false;
534
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000535 // Otherwise, make sure the ordering of the two instructions is unambiguous
536 // by checking that either (a) a token or operand kind discriminates them,
537 // or (b) the ordering among equivalent kinds is consistent.
538
Daniel Dunbar2b544812009-08-09 06:05:33 +0000539 // Tokens and operand kinds are unambiguous (assuming a correct target
540 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000541 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
542 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
543 AsmOperands[i].Class->Kind == ClassInfo::Token)
544 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
545 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000546 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000547
Daniel Dunbar2b544812009-08-09 06:05:33 +0000548 // Otherwise, this operand could commute if all operands are equivalent, or
549 // there is a pair of operands that compare less than and a pair that
550 // compare greater than.
551 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000552 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
553 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000554 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000555 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000556 HasGT = true;
557 }
558
559 return !(HasLT ^ HasGT);
560 }
561
Daniel Dunbar20927f22009-08-07 08:26:05 +0000562 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000563
Chris Lattnerd19ec052010-11-02 17:30:52 +0000564private:
Jim Grosbach8caecde2012-04-19 17:52:32 +0000565 void tokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000566};
567
Daniel Dunbar54074b52010-07-19 05:44:09 +0000568/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
569/// feature which participates in instruction matching.
570struct SubtargetFeatureInfo {
571 /// \brief The predicate record for this feature.
572 Record *TheDef;
573
574 /// \brief An unique index assigned to represent this feature.
575 unsigned Index;
576
Chris Lattner0aed1e72010-10-30 20:07:57 +0000577 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000578
Daniel Dunbar54074b52010-07-19 05:44:09 +0000579 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000580 std::string getEnumName() const {
581 return "Feature_" + TheDef->getName();
582 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000583};
584
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000585struct OperandMatchEntry {
586 unsigned OperandMask;
587 MatchableInfo* MI;
588 ClassInfo *CI;
589
Jim Grosbach8caecde2012-04-19 17:52:32 +0000590 static OperandMatchEntry create(MatchableInfo* mi, ClassInfo *ci,
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000591 unsigned opMask) {
592 OperandMatchEntry X;
593 X.OperandMask = opMask;
594 X.CI = ci;
595 X.MI = mi;
596 return X;
597 }
598};
599
600
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000601class AsmMatcherInfo {
602public:
Chris Lattner67db8832010-12-13 00:23:57 +0000603 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000604 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000605
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000606 /// The tablegen AsmParser record.
607 Record *AsmParser;
608
Chris Lattner02bcbc92010-11-01 01:37:30 +0000609 /// Target - The target information.
610 CodeGenTarget &Target;
611
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000612 /// The classes which are needed for matching.
613 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000614
Chris Lattner22bc5c42010-11-01 05:06:45 +0000615 /// The information on the matchables to match.
616 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000617
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000618 /// Info for custom matching operands by user defined methods.
619 std::vector<OperandMatchEntry> OperandMatchInfo;
620
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000621 /// Map of Register records to their class information.
Sean Silvadecfdf52012-09-19 01:47:01 +0000622 typedef std::map<Record*, ClassInfo*, LessRecordByID> RegisterClassesTy;
623 RegisterClassesTy RegisterClasses;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000624
Daniel Dunbar54074b52010-07-19 05:44:09 +0000625 /// Map of Predicate records to their subtarget information.
Tim Northover6dd670a2013-09-16 16:43:16 +0000626 std::map<Record*, SubtargetFeatureInfo*, LessRecordByID> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000627
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +0000628 /// Map of AsmOperandClass records to their class information.
629 std::map<Record*, ClassInfo*> AsmOperandClasses;
630
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000631private:
632 /// Map of token to class information which has already been constructed.
633 std::map<std::string, ClassInfo*> TokenClasses;
634
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000635 /// Map of RegisterClass records to their class information.
636 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000637
638private:
639 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000640 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000641
642 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000643 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
Jim Grosbach48c1f842011-10-28 22:32:53 +0000644 int SubOpIdx);
645 ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000646
Jim Grosbach8caecde2012-04-19 17:52:32 +0000647 /// buildRegisterClasses - Build the ClassInfo* instances for register
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000648 /// classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000649 void buildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000650
Jim Grosbach8caecde2012-04-19 17:52:32 +0000651 /// buildOperandClasses - Build the ClassInfo* instances for user defined
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000652 /// operand classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000653 void buildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000654
Jim Grosbach8caecde2012-04-19 17:52:32 +0000655 void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000656 unsigned AsmOpIdx);
Jim Grosbach8caecde2012-04-19 17:52:32 +0000657 void buildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000658 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000659
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000660public:
Bob Wilson828295b2011-01-26 21:26:19 +0000661 AsmMatcherInfo(Record *AsmParser,
662 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000663 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000664
Jim Grosbach8caecde2012-04-19 17:52:32 +0000665 /// buildInfo - Construct the various tables used during matching.
666 void buildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000667
Jim Grosbach8caecde2012-04-19 17:52:32 +0000668 /// buildOperandMatchInfo - Build the necessary information to handle user
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000669 /// defined operand parsing methods.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000670 void buildOperandMatchInfo();
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000671
Chris Lattner6fa152c2010-10-30 20:15:02 +0000672 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
673 /// given operand.
674 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
675 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
Tim Northover6dd670a2013-09-16 16:43:16 +0000676 std::map<Record*, SubtargetFeatureInfo*, LessRecordByID>::const_iterator I =
Chris Lattner6fa152c2010-10-30 20:15:02 +0000677 SubtargetFeatures.find(Def);
678 return I == SubtargetFeatures.end() ? 0 : I->second;
679 }
Chris Lattner67db8832010-12-13 00:23:57 +0000680
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000681 RecordKeeper &getRecords() const {
682 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000683 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000684};
685
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000686} // End anonymous namespace
Daniel Dunbar20927f22009-08-07 08:26:05 +0000687
Chris Lattner22bc5c42010-11-01 05:06:45 +0000688void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000689 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000690
Chris Lattner3116fef2010-11-02 01:03:43 +0000691 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000692 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000693 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000694 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000695 }
696}
697
Jim Grosbachc1922c72012-04-19 23:59:23 +0000698static std::pair<StringRef, StringRef>
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +0000699parseTwoOperandConstraint(StringRef S, ArrayRef<SMLoc> Loc) {
Jim Grosbachc1922c72012-04-19 23:59:23 +0000700 // Split via the '='.
701 std::pair<StringRef, StringRef> Ops = S.split('=');
702 if (Ops.second == "")
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000703 PrintFatalError(Loc, "missing '=' in two-operand alias constraint");
Jim Grosbachc1922c72012-04-19 23:59:23 +0000704 // Trim whitespace and the leading '$' on the operand names.
705 size_t start = Ops.first.find_first_of('$');
706 if (start == std::string::npos)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000707 PrintFatalError(Loc, "expected '$' prefix on asm operand name");
Jim Grosbachc1922c72012-04-19 23:59:23 +0000708 Ops.first = Ops.first.slice(start + 1, std::string::npos);
709 size_t end = Ops.first.find_last_of(" \t");
710 Ops.first = Ops.first.slice(0, end);
711 // Now the second operand.
712 start = Ops.second.find_first_of('$');
713 if (start == std::string::npos)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000714 PrintFatalError(Loc, "expected '$' prefix on asm operand name");
Jim Grosbachc1922c72012-04-19 23:59:23 +0000715 Ops.second = Ops.second.slice(start + 1, std::string::npos);
716 end = Ops.second.find_last_of(" \t");
717 Ops.first = Ops.first.slice(0, end);
718 return Ops;
719}
720
721void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
722 // Figure out which operands are aliased and mark them as tied.
723 std::pair<StringRef, StringRef> Ops =
724 parseTwoOperandConstraint(Constraint, TheDef->getLoc());
725
726 // Find the AsmOperands that refer to the operands we're aliasing.
727 int SrcAsmOperand = findAsmOperandNamed(Ops.first);
728 int DstAsmOperand = findAsmOperandNamed(Ops.second);
729 if (SrcAsmOperand == -1)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000730 PrintFatalError(TheDef->getLoc(),
Jim Grosbachc1922c72012-04-19 23:59:23 +0000731 "unknown source two-operand alias operand '" +
732 Ops.first.str() + "'.");
733 if (DstAsmOperand == -1)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000734 PrintFatalError(TheDef->getLoc(),
Jim Grosbachc1922c72012-04-19 23:59:23 +0000735 "unknown destination two-operand alias operand '" +
736 Ops.second.str() + "'.");
737
738 // Find the ResOperand that refers to the operand we're aliasing away
739 // and update it to refer to the combined operand instead.
740 for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
741 ResOperand &Op = ResOperands[i];
742 if (Op.Kind == ResOperand::RenderAsmOperand &&
743 Op.AsmOperandNum == (unsigned)SrcAsmOperand) {
744 Op.AsmOperandNum = DstAsmOperand;
745 break;
746 }
747 }
748 // Remove the AsmOperand for the alias operand.
749 AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand);
750 // Adjust the ResOperand references to any AsmOperands that followed
751 // the one we just deleted.
752 for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
753 ResOperand &Op = ResOperands[i];
754 switch(Op.Kind) {
755 default:
756 // Nothing to do for operands that don't reference AsmOperands.
757 break;
758 case ResOperand::RenderAsmOperand:
759 if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
760 --Op.AsmOperandNum;
761 break;
762 case ResOperand::TiedOperand:
763 if (Op.TiedOperandNum > (unsigned)SrcAsmOperand)
764 --Op.TiedOperandNum;
765 break;
766 }
767 }
768}
769
Jim Grosbach8caecde2012-04-19 17:52:32 +0000770void MatchableInfo::initialize(const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000771 SmallPtrSet<Record*, 16> &SingletonRegisters,
772 int AsmVariantNo, std::string &RegisterPrefix) {
Devang Patel56315d32012-01-10 17:50:43 +0000773 AsmVariantID = AsmVariantNo;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000774 AsmString =
Devang Patel63faf822012-01-07 01:33:34 +0000775 CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
Bob Wilson828295b2011-01-26 21:26:19 +0000776
Jim Grosbach8caecde2012-04-19 17:52:32 +0000777 tokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000778
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000779 // Compute the require features.
780 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
781 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
782 if (SubtargetFeatureInfo *Feature =
783 Info.getSubtargetFeature(Predicates[i]))
784 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000785
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000786 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000787 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Devang Patel63faf822012-01-07 01:33:34 +0000788 extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix);
789 if (Record *Reg = AsmOperands[i].SingletonReg)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000790 SingletonRegisters.insert(Reg);
791 }
Joey Gouly715d98d2013-09-12 10:28:05 +0000792
793 const RecordVal *DepMask = TheDef->getValue("DeprecatedFeatureMask");
794 if (!DepMask)
795 DepMask = TheDef->getValue("ComplexDeprecationPredicate");
796
797 HasDeprecation =
798 DepMask ? !DepMask->getValue()->getAsUnquotedString().empty() : false;
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000799}
800
Jim Grosbach8caecde2012-04-19 17:52:32 +0000801/// tokenizeAsmString - Tokenize a simplified assembly string.
802void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info) {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000803 StringRef String = AsmString;
804 unsigned Prev = 0;
805 bool InTok = true;
806 for (unsigned i = 0, e = String.size(); i != e; ++i) {
807 switch (String[i]) {
808 case '[':
809 case ']':
810 case '*':
811 case '!':
812 case ' ':
813 case '\t':
814 case ',':
815 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000816 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000817 InTok = false;
818 }
819 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000820 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000821 Prev = i + 1;
822 break;
823
824 case '\\':
825 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000826 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000827 InTok = false;
828 }
829 ++i;
830 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000831 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000832 Prev = i + 1;
833 break;
834
835 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000836 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000837 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000838 InTok = false;
839 }
Bob Wilson828295b2011-01-26 21:26:19 +0000840
Chris Lattner7ad31472010-11-06 22:06:03 +0000841 // If this isn't "${", treat like a normal token.
842 if (i + 1 == String.size() || String[i + 1] != '{') {
843 Prev = i;
844 break;
845 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000846
847 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
848 assert(End != String.end() && "Missing brace in operand reference!");
849 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000850 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000851 Prev = EndPos + 1;
852 i = EndPos;
853 break;
854 }
855
856 case '.':
Vladimir Medic588f4082013-08-01 09:25:27 +0000857 if (!Info.AsmParser->getValueAsBit("MnemonicContainsDot")) {
Vladimir Medic92731512013-07-16 09:22:38 +0000858 if (InTok)
859 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
860 Prev = i;
861 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000862 InTok = true;
863 break;
864
865 default:
866 InTok = true;
867 }
868 }
869 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000870 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000871
Chris Lattnerd19ec052010-11-02 17:30:52 +0000872 // The first token of the instruction is the mnemonic, which must be a
873 // simple string, not a $foo variable or a singleton register.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000874 if (AsmOperands.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000875 PrintFatalError(TheDef->getLoc(),
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000876 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000877 Mnemonic = AsmOperands[0].Token;
Jim Grosbach8e27c962012-05-06 17:33:14 +0000878 if (Mnemonic.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000879 PrintFatalError(TheDef->getLoc(),
Jim Grosbach8e27c962012-05-06 17:33:14 +0000880 "Missing instruction mnemonic");
Devang Patel63faf822012-01-07 01:33:34 +0000881 // FIXME : Check and raise an error if it is a register.
Devang Patelb78307f2012-01-07 01:22:23 +0000882 if (Mnemonic[0] == '$')
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000883 PrintFatalError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000884 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000885
Chris Lattnerd19ec052010-11-02 17:30:52 +0000886 // Remove the first operand, it is tracked in the mnemonic field.
887 AsmOperands.erase(AsmOperands.begin());
888}
889
Jim Grosbach8caecde2012-04-19 17:52:32 +0000890bool MatchableInfo::validate(StringRef CommentDelimiter, bool Hack) const {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000891 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000892 if (AsmString.empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000893 PrintFatalError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000894
Chris Lattner22bc5c42010-11-01 05:06:45 +0000895 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000896 // isCodeGenOnly if they are pseudo instructions.
897 if (AsmString.find('\n') != std::string::npos)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000898 PrintFatalError(TheDef->getLoc(),
Chris Lattner5bc93872010-11-01 04:34:44 +0000899 "multiline instruction is not valid for the asmparser, "
900 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000901
Chris Lattner4164f6b2010-11-01 04:44:29 +0000902 // Remove comments from the asm string. We know that the asmstring only
903 // has one line.
904 if (!CommentDelimiter.empty() &&
905 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000906 PrintFatalError(TheDef->getLoc(),
Chris Lattner4164f6b2010-11-01 04:44:29 +0000907 "asmstring for instruction has comment character in it, "
908 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000909
Chris Lattner22bc5c42010-11-01 05:06:45 +0000910 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000911 // handle, the target should be refactored to use operands instead of
912 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000913 //
914 // Also, check for instructions which reference the operand multiple times;
915 // this implies a constraint we would not honor.
916 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000917 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
918 StringRef Tok = AsmOperands[i].Token;
919 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000920 PrintFatalError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000921 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000922 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000923
Chris Lattner22bc5c42010-11-01 05:06:45 +0000924 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000925 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000926 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000927 if (!Hack)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000928 PrintFatalError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000929 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000930 "' can never be matched!");
931 // FIXME: Should reject these. The ARM backend hits this with $lane in a
932 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000933 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000934 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000935 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000936 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000937 });
938 return false;
939 }
940 }
Bob Wilson828295b2011-01-26 21:26:19 +0000941
Chris Lattner5bc93872010-11-01 04:34:44 +0000942 return true;
943}
944
Jim Grosbachf35307c2012-01-24 21:06:59 +0000945/// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Pateld06b01c2012-01-09 21:30:46 +0000946/// if present, from specified token.
Devang Patel63faf822012-01-07 01:33:34 +0000947void MatchableInfo::
Jim Grosbachf35307c2012-01-24 21:06:59 +0000948extractSingletonRegisterForAsmOperand(unsigned OperandNo,
Devang Pateld06b01c2012-01-09 21:30:46 +0000949 const AsmMatcherInfo &Info,
Jim Grosbach11fc6462012-04-11 21:02:33 +0000950 std::string &RegisterPrefix) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000951 StringRef Tok = AsmOperands[OperandNo].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000952 if (RegisterPrefix.empty()) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000953 std::string LoweredTok = Tok.lower();
954 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
955 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Devang Patel63faf822012-01-07 01:33:34 +0000956 return;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000957 }
Bob Wilson828295b2011-01-26 21:26:19 +0000958
Devang Patel63faf822012-01-07 01:33:34 +0000959 if (!Tok.startswith(RegisterPrefix))
960 return;
961
962 StringRef RegName = Tok.substr(RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000963 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
Devang Pateld06b01c2012-01-09 21:30:46 +0000964 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000965
Chris Lattner1de88232010-11-01 01:47:07 +0000966 // If there is no register prefix (i.e. "%" in "%eax"), then this may
967 // be some random non-register token, just ignore it.
Devang Patel63faf822012-01-07 01:33:34 +0000968 return;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000969}
970
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000971static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000972 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000973
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000974 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
975 switch (*it) {
976 case '*': Res += "_STAR_"; break;
977 case '%': Res += "_PCT_"; break;
978 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000979 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000980 case '.': Res += "_DOT_"; break;
Tim Northover12da5052013-01-10 16:47:31 +0000981 case '<': Res += "_LT_"; break;
982 case '>': Res += "_GT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000983 default:
Tim Northover12da5052013-01-10 16:47:31 +0000984 if ((*it >= 'A' && *it <= 'Z') ||
985 (*it >= 'a' && *it <= 'z') ||
986 (*it >= '0' && *it <= '9'))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000987 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000988 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000989 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000990 }
991 }
992
993 return Res;
994}
995
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000996ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000997 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000998
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000999 if (!Entry) {
1000 Entry = new ClassInfo();
1001 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +00001002 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001003 Entry->Name = "MCK_" + getEnumNameForToken(Token);
1004 Entry->ValueName = Token;
1005 Entry->PredicateMethod = "<invalid>";
1006 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001007 Entry->ParserMethod = "";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001008 Entry->DiagnosticType = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001009 Classes.push_back(Entry);
1010 }
1011
1012 return Entry;
1013}
1014
1015ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +00001016AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
1017 int SubOpIdx) {
1018 Record *Rec = OI.Rec;
1019 if (SubOpIdx != -1)
Sean Silva3f7b7f82012-10-10 20:24:47 +00001020 Rec = cast<DefInit>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +00001021 return getOperandClass(Rec, SubOpIdx);
1022}
Bob Wilsona49c7df2011-01-26 19:44:55 +00001023
Jim Grosbach48c1f842011-10-28 22:32:53 +00001024ClassInfo *
1025AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +00001026 if (Rec->isSubClassOf("RegisterOperand")) {
1027 // RegisterOperand may have an associated ParserMatchClass. If it does,
1028 // use it, else just fall back to the underlying register class.
1029 const RecordVal *R = Rec->getValue("ParserMatchClass");
1030 if (R == 0 || R->getValue() == 0)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001031 PrintFatalError("Record `" + Rec->getName() +
1032 "' does not have a ParserMatchClass!\n");
Owen Andersonbea6f612011-06-27 21:06:21 +00001033
Sean Silva6cfc8062012-10-10 20:24:43 +00001034 if (DefInit *DI= dyn_cast<DefInit>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +00001035 Record *MatchClass = DI->getDef();
1036 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1037 return CI;
1038 }
1039
1040 // No custom match class. Just use the register class.
1041 Record *ClassRec = Rec->getValueAsDef("RegClass");
1042 if (!ClassRec)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001043 PrintFatalError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
Owen Andersonbea6f612011-06-27 21:06:21 +00001044 "' has no associated register class!\n");
1045 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
1046 return CI;
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001047 PrintFatalError(Rec->getLoc(), "register class has no class info!");
Owen Andersonbea6f612011-06-27 21:06:21 +00001048 }
1049
1050
Bob Wilsona49c7df2011-01-26 19:44:55 +00001051 if (Rec->isSubClassOf("RegisterClass")) {
1052 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +00001053 return CI;
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001054 PrintFatalError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001055 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001056
Jim Grosbacha562dc72012-09-12 17:40:25 +00001057 if (!Rec->isSubClassOf("Operand"))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001058 PrintFatalError(Rec->getLoc(), "Operand `" + Rec->getName() +
Jim Grosbacha562dc72012-09-12 17:40:25 +00001059 "' does not derive from class Operand!\n");
Bob Wilsona49c7df2011-01-26 19:44:55 +00001060 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +00001061 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1062 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +00001063
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001064 PrintFatalError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001065}
1066
Tim Northover03f91972013-09-16 16:43:19 +00001067struct LessRegisterSet {
1068 bool operator() (const RegisterSet &LHS, const RegisterSet & RHS) {
1069 // std::set<T> defines its own compariso "operator<", but it
1070 // performs a lexicographical comparison by T's innate comparison
1071 // for some reason. We don't want non-deterministic pointer
1072 // comparisons so use this instead.
1073 return std::lexicographical_compare(LHS.begin(), LHS.end(),
1074 RHS.begin(), RHS.end(),
1075 LessRecordByID());
1076 }
1077};
1078
Chris Lattner1de88232010-11-01 01:47:07 +00001079void AsmMatcherInfo::
Jim Grosbach8caecde2012-04-19 17:52:32 +00001080buildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001081 const std::vector<CodeGenRegister*> &Registers =
1082 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001083 ArrayRef<CodeGenRegisterClass*> RegClassList =
1084 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +00001085
Tim Northover03f91972013-09-16 16:43:19 +00001086 typedef std::set<RegisterSet, LessRegisterSet> RegisterSetSet;
1087
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001088 // The register sets used for matching.
Tim Northover03f91972013-09-16 16:43:19 +00001089 RegisterSetSet RegisterSets;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001090
Jim Grosbacha7c78222010-10-29 22:13:48 +00001091 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001092 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Tim Northover03f91972013-09-16 16:43:19 +00001093 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
1094 RegisterSets.insert(RegisterSet(
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001095 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001096
1097 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +00001098 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1099 ie = SingletonRegisters.end(); it != ie; ++it) {
1100 Record *Rec = *it;
Tim Northover03f91972013-09-16 16:43:19 +00001101 RegisterSets.insert(RegisterSet(&Rec, &Rec + 1));
Chris Lattner1de88232010-11-01 01:47:07 +00001102 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001103
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001104 // Introduce derived sets where necessary (when a register does not determine
1105 // a unique register set class), and build the mapping of registers to the set
1106 // they should classify to.
Tim Northover03f91972013-09-16 16:43:19 +00001107 std::map<Record*, RegisterSet> RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001108 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001109 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001110 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001111 // Compute the intersection of all sets containing this register.
Tim Northover03f91972013-09-16 16:43:19 +00001112 RegisterSet ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001113
Tim Northover03f91972013-09-16 16:43:19 +00001114 for (RegisterSetSet::iterator it = RegisterSets.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001115 ie = RegisterSets.end(); it != ie; ++it) {
1116 if (!it->count(CGR.TheDef))
1117 continue;
1118
1119 if (ContainingSet.empty()) {
1120 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001121 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001122 }
Bob Wilson828295b2011-01-26 21:26:19 +00001123
Tim Northover03f91972013-09-16 16:43:19 +00001124 RegisterSet Tmp;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001125 std::swap(Tmp, ContainingSet);
Tim Northover03f91972013-09-16 16:43:19 +00001126 std::insert_iterator<RegisterSet> II(ContainingSet,
1127 ContainingSet.begin());
1128 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II,
1129 LessRecordByID());
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001130 }
1131
1132 if (!ContainingSet.empty()) {
1133 RegisterSets.insert(ContainingSet);
1134 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
1135 }
1136 }
1137
1138 // Construct the register classes.
Tim Northover03f91972013-09-16 16:43:19 +00001139 std::map<RegisterSet, ClassInfo*, LessRegisterSet> RegisterSetClasses;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001140 unsigned Index = 0;
Tim Northover03f91972013-09-16 16:43:19 +00001141 for (RegisterSetSet::iterator it = RegisterSets.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001142 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
1143 ClassInfo *CI = new ClassInfo();
1144 CI->Kind = ClassInfo::RegisterClass0 + Index;
1145 CI->ClassName = "Reg" + utostr(Index);
1146 CI->Name = "MCK_Reg" + utostr(Index);
1147 CI->ValueName = "";
1148 CI->PredicateMethod = ""; // unused
1149 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +00001150 CI->Registers = *it;
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001151 // FIXME: diagnostic type.
1152 CI->DiagnosticType = "";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001153 Classes.push_back(CI);
1154 RegisterSetClasses.insert(std::make_pair(*it, CI));
1155 }
1156
1157 // Find the superclasses; we could compute only the subgroup lattice edges,
1158 // but there isn't really a point.
Tim Northover03f91972013-09-16 16:43:19 +00001159 for (RegisterSetSet::iterator it = RegisterSets.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001160 ie = RegisterSets.end(); it != ie; ++it) {
1161 ClassInfo *CI = RegisterSetClasses[*it];
Tim Northover03f91972013-09-16 16:43:19 +00001162 for (RegisterSetSet::iterator it2 = RegisterSets.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001163 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001164 if (*it != *it2 &&
Tim Northover03f91972013-09-16 16:43:19 +00001165 std::includes(it2->begin(), it2->end(), it->begin(), it->end(),
1166 LessRecordByID()))
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001167 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
1168 }
1169
1170 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001171 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001172 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001173 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001174 // Def will be NULL for non-user defined register classes.
1175 Record *Def = RC.getDef();
1176 if (!Def)
1177 continue;
Tim Northover03f91972013-09-16 16:43:19 +00001178 ClassInfo *CI = RegisterSetClasses[RegisterSet(RC.getOrder().begin(),
1179 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001180 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001181 CI->ClassName = RC.getName();
1182 CI->Name = "MCK_" + RC.getName();
1183 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001184 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001185 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001186
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001187 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001188 }
1189
1190 // Populate the map for individual registers.
Tim Northover03f91972013-09-16 16:43:19 +00001191 for (std::map<Record*, RegisterSet>::iterator it = RegisterMap.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001192 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001193 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001194
1195 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001196 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1197 ie = SingletonRegisters.end(); it != ie; ++it) {
1198 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001199 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001200 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001201
Chris Lattner1de88232010-11-01 01:47:07 +00001202 if (CI->ValueName.empty()) {
1203 CI->ClassName = Rec->getName();
1204 CI->Name = "MCK_" + Rec->getName();
1205 CI->ValueName = Rec->getName();
1206 } else
1207 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001208 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001209}
1210
Jim Grosbach8caecde2012-04-19 17:52:32 +00001211void AsmMatcherInfo::buildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001212 std::vector<Record*> AsmOperands =
1213 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001214
1215 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001216 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001217 ie = AsmOperands.end(); it != ie; ++it)
1218 AsmOperandClasses[*it] = new ClassInfo();
1219
Daniel Dunbar338825c2009-08-10 18:41:10 +00001220 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001221 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001222 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001223 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001224 CI->Kind = ClassInfo::UserClass0 + Index;
1225
David Greene05bce0b2011-07-29 22:43:06 +00001226 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001227 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
Sean Silva6cfc8062012-10-10 20:24:43 +00001228 DefInit *DI = dyn_cast<DefInit>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001229 if (!DI) {
1230 PrintError((*it)->getLoc(), "Invalid super class reference!");
1231 continue;
1232 }
1233
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001234 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1235 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001236 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001237 else
1238 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001239 }
1240 CI->ClassName = (*it)->getValueAsString("Name");
1241 CI->Name = "MCK_" + CI->ClassName;
1242 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001243
1244 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001245 Init *PMName = (*it)->getValueInit("PredicateMethod");
Sean Silva6cfc8062012-10-10 20:24:43 +00001246 if (StringInit *SI = dyn_cast<StringInit>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001247 CI->PredicateMethod = SI->getValue();
1248 } else {
Sean Silva3f7b7f82012-10-10 20:24:47 +00001249 assert(isa<UnsetInit>(PMName) && "Unexpected PredicateMethod field!");
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001250 CI->PredicateMethod = "is" + CI->ClassName;
1251 }
1252
1253 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001254 Init *RMName = (*it)->getValueInit("RenderMethod");
Sean Silva6cfc8062012-10-10 20:24:43 +00001255 if (StringInit *SI = dyn_cast<StringInit>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001256 CI->RenderMethod = SI->getValue();
1257 } else {
Sean Silva3f7b7f82012-10-10 20:24:47 +00001258 assert(isa<UnsetInit>(RMName) && "Unexpected RenderMethod field!");
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001259 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1260 }
1261
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001262 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001263 Init *PRMName = (*it)->getValueInit("ParserMethod");
Sean Silva6cfc8062012-10-10 20:24:43 +00001264 if (StringInit *SI = dyn_cast<StringInit>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001265 CI->ParserMethod = SI->getValue();
1266
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001267 // Get the diagnostic type or leave it as empty.
1268 // Get the parse method name or leave it as empty.
1269 Init *DiagnosticType = (*it)->getValueInit("DiagnosticType");
Sean Silva6cfc8062012-10-10 20:24:43 +00001270 if (StringInit *SI = dyn_cast<StringInit>(DiagnosticType))
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001271 CI->DiagnosticType = SI->getValue();
1272
Daniel Dunbar338825c2009-08-10 18:41:10 +00001273 AsmOperandClasses[*it] = CI;
1274 Classes.push_back(CI);
1275 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001276}
1277
Bob Wilson828295b2011-01-26 21:26:19 +00001278AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1279 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001280 RecordKeeper &records)
Devang Patel63faf822012-01-07 01:33:34 +00001281 : Records(records), AsmParser(asmParser), Target(target) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001282}
1283
Jim Grosbach8caecde2012-04-19 17:52:32 +00001284/// buildOperandMatchInfo - Build the necessary information to handle user
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001285/// defined operand parsing methods.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001286void AsmMatcherInfo::buildOperandMatchInfo() {
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001287
Jim Grosbachd4824fc2012-04-18 23:46:25 +00001288 /// Map containing a mask with all operands indices that can be found for
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001289 /// that class inside a instruction.
Sean Silvab2df6102012-09-19 01:47:03 +00001290 typedef std::map<ClassInfo*, unsigned, LessClassInfoPtr> OpClassMaskTy;
1291 OpClassMaskTy OpClassMask;
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001292
1293 for (std::vector<MatchableInfo*>::const_iterator it =
1294 Matchables.begin(), ie = Matchables.end();
1295 it != ie; ++it) {
1296 MatchableInfo &II = **it;
1297 OpClassMask.clear();
1298
1299 // Keep track of all operands of this instructions which belong to the
1300 // same class.
1301 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1302 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1303 if (Op.Class->ParserMethod.empty())
1304 continue;
1305 unsigned &OperandMask = OpClassMask[Op.Class];
1306 OperandMask |= (1 << i);
1307 }
1308
1309 // Generate operand match info for each mnemonic/operand class pair.
Sean Silvab2df6102012-09-19 01:47:03 +00001310 for (OpClassMaskTy::iterator iit = OpClassMask.begin(),
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001311 iie = OpClassMask.end(); iit != iie; ++iit) {
1312 unsigned OpMask = iit->second;
1313 ClassInfo *CI = iit->first;
Jim Grosbach8caecde2012-04-19 17:52:32 +00001314 OperandMatchInfo.push_back(OperandMatchEntry::create(&II, CI, OpMask));
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001315 }
1316 }
1317}
1318
Jim Grosbach8caecde2012-04-19 17:52:32 +00001319void AsmMatcherInfo::buildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001320 // Build information about all of the AssemblerPredicates.
1321 std::vector<Record*> AllPredicates =
1322 Records.getAllDerivedDefinitions("Predicate");
1323 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1324 Record *Pred = AllPredicates[i];
1325 // Ignore predicates that are not intended for the assembler.
1326 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1327 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001328
Chris Lattner4164f6b2010-11-01 04:44:29 +00001329 if (Pred->getName().empty())
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001330 PrintFatalError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001331
Chris Lattner0aed1e72010-10-30 20:07:57 +00001332 unsigned FeatureNo = SubtargetFeatures.size();
1333 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1334 assert(FeatureNo < 32 && "Too many subtarget features!");
1335 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001336
Chris Lattner39ee0362010-10-31 19:10:56 +00001337 // Parse the instructions; we need to do this first so that we can gather the
1338 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001339 SmallPtrSet<Record*, 16> SingletonRegisters;
Devang Patel0dbcada2012-01-09 19:13:28 +00001340 unsigned VariantCount = Target.getAsmParserVariantCount();
1341 for (unsigned VC = 0; VC != VariantCount; ++VC) {
1342 Record *AsmVariant = Target.getAsmParserVariant(VC);
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001343 std::string CommentDelimiter =
1344 AsmVariant->getValueAsString("CommentDelimiter");
Devang Patel0dbcada2012-01-09 19:13:28 +00001345 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1346 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
Jim Grosbachf35307c2012-01-24 21:06:59 +00001347
Devang Patel0dbcada2012-01-09 19:13:28 +00001348 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
Jim Grosbach11fc6462012-04-11 21:02:33 +00001349 E = Target.inst_end(); I != E; ++I) {
Devang Patel0dbcada2012-01-09 19:13:28 +00001350 const CodeGenInstruction &CGI = **I;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001351
Devang Patel0dbcada2012-01-09 19:13:28 +00001352 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1353 // filter the set of instructions we consider.
1354 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001355 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001356
Devang Patel0dbcada2012-01-09 19:13:28 +00001357 // Ignore "codegen only" instructions.
1358 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001359 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001360
Devang Patel0dbcada2012-01-09 19:13:28 +00001361 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001362
Jim Grosbach8caecde2012-04-19 17:52:32 +00001363 II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001364
Devang Patel0dbcada2012-01-09 19:13:28 +00001365 // Ignore instructions which shouldn't be matched and diagnose invalid
1366 // instruction definitions with an error.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001367 if (!II->validate(CommentDelimiter, true))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001368 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001369
Devang Patel0dbcada2012-01-09 19:13:28 +00001370 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1371 //
1372 // FIXME: This is a total hack.
1373 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
Jim Grosbach11fc6462012-04-11 21:02:33 +00001374 StringRef(II->TheDef->getName()).endswith("_Int"))
1375 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001376
Devang Patel0dbcada2012-01-09 19:13:28 +00001377 Matchables.push_back(II.take());
Chris Lattner1d13bda2010-11-04 00:43:46 +00001378 }
Jim Grosbachf35307c2012-01-24 21:06:59 +00001379
Devang Patel0dbcada2012-01-09 19:13:28 +00001380 // Parse all of the InstAlias definitions and stick them in the list of
1381 // matchables.
1382 std::vector<Record*> AllInstAliases =
1383 Records.getAllDerivedDefinitions("InstAlias");
1384 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
1385 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001386
Devang Patel0dbcada2012-01-09 19:13:28 +00001387 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1388 // filter the set of instruction aliases we consider, based on the target
1389 // instruction.
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001390 if (!StringRef(Alias->ResultInst->TheDef->getName())
1391 .startswith( MatchPrefix))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001392 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001393
Devang Patel0dbcada2012-01-09 19:13:28 +00001394 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001395
Jim Grosbach8caecde2012-04-19 17:52:32 +00001396 II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001397
Devang Patel0dbcada2012-01-09 19:13:28 +00001398 // Validate the alias definitions.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001399 II->validate(CommentDelimiter, false);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001400
Devang Patel0dbcada2012-01-09 19:13:28 +00001401 Matchables.push_back(II.take());
1402 }
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001403 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001404
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001405 // Build info for the register classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001406 buildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001407
1408 // Build info for the user defined assembly operand classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001409 buildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001410
Chris Lattner0bb780c2010-11-04 00:57:06 +00001411 // Build the information about matchables, now that we have fully formed
1412 // classes.
Jim Grosbachc1922c72012-04-19 23:59:23 +00001413 std::vector<MatchableInfo*> NewMatchables;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001414 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1415 ie = Matchables.end(); it != ie; ++it) {
1416 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001417
Chris Lattnere206fcf2010-09-06 21:01:37 +00001418 // Parse the tokens after the mnemonic.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001419 // Note: buildInstructionOperandReference may insert new AsmOperands, so
Bob Wilsona49c7df2011-01-26 19:44:55 +00001420 // don't precompute the loop bound.
1421 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001422 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001423 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001424
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001425 // Check for singleton registers.
Devang Patel63faf822012-01-07 01:33:34 +00001426 if (Record *RegRecord = II->AsmOperands[i].SingletonReg) {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001427 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001428 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1429 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001430 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001431 }
1432
Daniel Dunbar20927f22009-08-07 08:26:05 +00001433 // Check for simple tokens.
1434 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001435 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001436 continue;
1437 }
1438
Chris Lattner7ad31472010-11-06 22:06:03 +00001439 if (Token.size() > 1 && isdigit(Token[1])) {
1440 Op.Class = getTokenClass(Token);
1441 continue;
1442 }
Bob Wilson828295b2011-01-26 21:26:19 +00001443
Chris Lattnerc07bd402010-11-04 02:11:18 +00001444 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001445 StringRef OperandName;
1446 if (Token[1] == '{')
1447 OperandName = Token.substr(2, Token.size() - 3);
1448 else
1449 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001450
Chris Lattnerc07bd402010-11-04 02:11:18 +00001451 if (II->DefRec.is<const CodeGenInstruction*>())
Jim Grosbach8caecde2012-04-19 17:52:32 +00001452 buildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001453 else
Jim Grosbach8caecde2012-04-19 17:52:32 +00001454 buildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001455 }
Bob Wilson828295b2011-01-26 21:26:19 +00001456
Jim Grosbachc1922c72012-04-19 23:59:23 +00001457 if (II->DefRec.is<const CodeGenInstruction*>()) {
Jim Grosbach8caecde2012-04-19 17:52:32 +00001458 II->buildInstructionResultOperands();
Jim Grosbachc1922c72012-04-19 23:59:23 +00001459 // If the instruction has a two-operand alias, build up the
1460 // matchable here. We'll add them in bulk at the end to avoid
1461 // confusing this loop.
1462 std::string Constraint =
1463 II->TheDef->getValueAsString("TwoOperandAliasConstraint");
1464 if (Constraint != "") {
1465 // Start by making a copy of the original matchable.
1466 OwningPtr<MatchableInfo> AliasII(new MatchableInfo(*II));
1467
1468 // Adjust it to be a two-operand alias.
1469 AliasII->formTwoOperandAlias(Constraint);
1470
1471 // Add the alias to the matchables list.
1472 NewMatchables.push_back(AliasII.take());
1473 }
1474 } else
Jim Grosbach8caecde2012-04-19 17:52:32 +00001475 II->buildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001476 }
Jim Grosbachc1922c72012-04-19 23:59:23 +00001477 if (!NewMatchables.empty())
1478 Matchables.insert(Matchables.end(), NewMatchables.begin(),
1479 NewMatchables.end());
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001480
Jim Grosbacha66512e2011-12-06 23:43:54 +00001481 // Process token alias definitions and set up the associated superclass
1482 // information.
1483 std::vector<Record*> AllTokenAliases =
1484 Records.getAllDerivedDefinitions("TokenAlias");
1485 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1486 Record *Rec = AllTokenAliases[i];
1487 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1488 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
Jim Grosbach67cd20d2012-04-17 21:23:52 +00001489 if (FromClass == ToClass)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001490 PrintFatalError(Rec->getLoc(),
Jim Grosbach67cd20d2012-04-17 21:23:52 +00001491 "error: Destination value identical to source value.");
Jim Grosbacha66512e2011-12-06 23:43:54 +00001492 FromClass->SuperClasses.push_back(ToClass);
1493 }
1494
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001495 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001496 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001497}
1498
Jim Grosbach8caecde2012-04-19 17:52:32 +00001499/// buildInstructionOperandReference - The specified operand is a reference to a
Chris Lattner0bb780c2010-11-04 00:57:06 +00001500/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1501void AsmMatcherInfo::
Jim Grosbach8caecde2012-04-19 17:52:32 +00001502buildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001503 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001504 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001505 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1506 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001507 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001508
Chris Lattner662e5a32010-11-06 07:14:44 +00001509 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001510 unsigned Idx;
1511 if (!Operands.hasOperandNamed(OperandName, Idx))
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001512 PrintFatalError(II->TheDef->getLoc(), "error: unable to find operand: '" +
Chris Lattner0bb780c2010-11-04 00:57:06 +00001513 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001514
Bob Wilsona49c7df2011-01-26 19:44:55 +00001515 // If the instruction operand has multiple suboperands, but the parser
1516 // match class for the asm operand is still the default "ImmAsmOperand",
1517 // then handle each suboperand separately.
1518 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1519 Record *Rec = Operands[Idx].Rec;
1520 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1521 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1522 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1523 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1524 StringRef Token = Op->Token; // save this in case Op gets moved
1525 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1526 MatchableInfo::AsmOperand NewAsmOp(Token);
1527 NewAsmOp.SubOpIdx = SI;
1528 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1529 }
1530 // Replace Op with first suboperand.
1531 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1532 Op->SubOpIdx = 0;
1533 }
1534 }
1535
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001536 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001537 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001538
1539 // If the named operand is tied, canonicalize it to the untied operand.
1540 // For example, something like:
1541 // (outs GPR:$dst), (ins GPR:$src)
1542 // with an asmstring of
1543 // "inc $src"
1544 // we want to canonicalize to:
1545 // "inc $dst"
1546 // so that we know how to provide the $dst operand when filling in the result.
Ulrich Weigandd9990622013-04-27 18:48:23 +00001547 int OITied = -1;
1548 if (Operands[Idx].MINumOperands == 1)
1549 OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001550 if (OITied != -1) {
1551 // The tied operand index is an MIOperand index, find the operand that
1552 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001553 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1554 OperandName = Operands[Idx.first].Name;
1555 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001556 }
Bob Wilson828295b2011-01-26 21:26:19 +00001557
Bob Wilsona49c7df2011-01-26 19:44:55 +00001558 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001559}
1560
Jim Grosbach8caecde2012-04-19 17:52:32 +00001561/// buildAliasOperandReference - When parsing an operand reference out of the
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001562/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1563/// operand reference is by looking it up in the result pattern definition.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001564void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II,
Chris Lattnerc07bd402010-11-04 02:11:18 +00001565 StringRef OperandName,
1566 MatchableInfo::AsmOperand &Op) {
1567 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001568
Chris Lattnerc07bd402010-11-04 02:11:18 +00001569 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001570 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001571 if (CGA.ResultOperands[i].isRecord() &&
1572 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001573 // It's safe to go with the first one we find, because CodeGenInstAlias
1574 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001575 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001576 // Use the match class from the Alias definition, not the
1577 // destination instruction, as we may have an immediate that's
1578 // being munged by the match class.
1579 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001580 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001581 Op.SrcOpName = OperandName;
1582 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001583 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001584
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001585 PrintFatalError(II->TheDef->getLoc(), "error: unable to find operand: '" +
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001586 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001587}
1588
Jim Grosbach8caecde2012-04-19 17:52:32 +00001589void MatchableInfo::buildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001590 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001591
Chris Lattner662e5a32010-11-06 07:14:44 +00001592 // Loop over all operands of the result instruction, determining how to
1593 // populate them.
1594 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1595 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001596
1597 // If this is a tied operand, just copy from the previously handled operand.
Ulrich Weigandd9990622013-04-27 18:48:23 +00001598 int TiedOp = -1;
1599 if (OpInfo.MINumOperands == 1)
1600 TiedOp = OpInfo.getTiedRegister();
Chris Lattner567820c2010-11-04 01:42:59 +00001601 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001602 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001603 continue;
1604 }
Bob Wilson828295b2011-01-26 21:26:19 +00001605
Bob Wilsona49c7df2011-01-26 19:44:55 +00001606 // Find out what operand from the asmparser this MCInst operand comes from.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001607 int SrcOperand = findAsmOperandNamed(OpInfo.Name);
Ulrich Weigandd9990622013-04-27 18:48:23 +00001608 if (OpInfo.Name.empty() || SrcOperand == -1) {
1609 // This may happen for operands that are tied to a suboperand of a
1610 // complex operand. Simply use a dummy value here; nobody should
1611 // use this operand slot.
1612 // FIXME: The long term goal is for the MCOperand list to not contain
1613 // tied operands at all.
1614 ResOperands.push_back(ResOperand::getImmOp(0));
1615 continue;
1616 }
Chris Lattner567820c2010-11-04 01:42:59 +00001617
Bob Wilsona49c7df2011-01-26 19:44:55 +00001618 // Check if the one AsmOperand populates the entire operand.
1619 unsigned NumOperands = OpInfo.MINumOperands;
1620 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1621 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001622 continue;
1623 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001624
1625 // Add a separate ResOperand for each suboperand.
1626 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1627 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1628 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1629 "unexpected AsmOperands for suboperands");
1630 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1631 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001632 }
1633}
1634
Jim Grosbach8caecde2012-04-19 17:52:32 +00001635void MatchableInfo::buildAliasResultOperands() {
Chris Lattner41409852010-11-06 07:31:43 +00001636 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1637 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001638
Chris Lattner41409852010-11-06 07:31:43 +00001639 // Loop over all operands of the result instruction, determining how to
1640 // populate them.
1641 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001642 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001643 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001644 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001645
Chris Lattner41409852010-11-06 07:31:43 +00001646 // If this is a tied operand, just copy from the previously handled operand.
Ulrich Weigandd9990622013-04-27 18:48:23 +00001647 int TiedOp = -1;
1648 if (OpInfo->MINumOperands == 1)
1649 TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001650 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001651 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001652 continue;
1653 }
1654
Bob Wilsona49c7df2011-01-26 19:44:55 +00001655 // Handle all the suboperands for this operand.
1656 const std::string &OpName = OpInfo->Name;
1657 for ( ; AliasOpNo < LastOpNo &&
1658 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1659 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1660
1661 // Find out what operand from the asmparser that this MCInst operand
1662 // comes from.
1663 switch (CGA.ResultOperands[AliasOpNo].Kind) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001664 case CodeGenInstAlias::ResultOperand::K_Record: {
1665 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
Jim Grosbach8caecde2012-04-19 17:52:32 +00001666 int SrcOperand = findAsmOperand(Name, SubIdx);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001667 if (SrcOperand == -1)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00001668 PrintFatalError(TheDef->getLoc(), "Instruction '" +
Bob Wilsona49c7df2011-01-26 19:44:55 +00001669 TheDef->getName() + "' has operand '" + OpName +
1670 "' that doesn't appear in asm string!");
1671 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1672 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1673 NumOperands));
1674 break;
1675 }
1676 case CodeGenInstAlias::ResultOperand::K_Imm: {
1677 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1678 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1679 break;
1680 }
1681 case CodeGenInstAlias::ResultOperand::K_Reg: {
1682 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1683 ResOperands.push_back(ResOperand::getRegOp(Reg));
1684 break;
1685 }
1686 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001687 }
Chris Lattner41409852010-11-06 07:31:43 +00001688 }
1689}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001690
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001691static unsigned getConverterOperandID(const std::string &Name,
1692 SetVector<std::string> &Table,
1693 bool &IsNew) {
1694 IsNew = Table.insert(Name);
1695
1696 unsigned ID = IsNew ? Table.size() - 1 :
1697 std::find(Table.begin(), Table.end(), Name) - Table.begin();
1698
1699 assert(ID < Table.size());
1700
1701 return ID;
1702}
1703
1704
Chad Rosier22685872012-10-01 23:45:51 +00001705static void emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName,
1706 std::vector<MatchableInfo*> &Infos,
1707 raw_ostream &OS) {
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001708 SetVector<std::string> OperandConversionKinds;
1709 SetVector<std::string> InstructionConversionKinds;
1710 std::vector<std::vector<uint8_t> > ConversionTable;
1711 size_t MaxRowLength = 2; // minimum is custom converter plus terminator.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001712
Chris Lattner98986712010-01-14 22:21:20 +00001713 // TargetOperandClass - This is the target's operand class, like X86Operand.
1714 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001715
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001716 // Write the convert function to a separate stream, so we can drop it after
1717 // the enum. We'll build up the conversion handlers for the individual
1718 // operand types opportunistically as we encounter them.
1719 std::string ConvertFnBody;
1720 raw_string_ostream CvtOS(ConvertFnBody);
1721 // Start the unified conversion function.
Chad Rosier359956d2012-08-31 00:03:31 +00001722 CvtOS << "void " << Target.getName() << ClassName << "::\n"
Chad Rosier90e11f82012-09-05 01:02:38 +00001723 << "convertToMCInst(unsigned Kind, MCInst &Inst, "
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001724 << "unsigned Opcode,\n"
Chad Rosier04508c62012-08-30 21:46:00 +00001725 << " const SmallVectorImpl<MCParsedAsmOperand*"
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001726 << "> &Operands) {\n"
Chad Rosier359956d2012-08-31 00:03:31 +00001727 << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
Craig Topperb198f5c2012-09-18 01:41:49 +00001728 << " const uint8_t *Converter = ConversionTable[Kind];\n"
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001729 << " Inst.setOpcode(Opcode);\n"
Craig Topperb198f5c2012-09-18 01:41:49 +00001730 << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001731 << " switch (*p) {\n"
1732 << " default: llvm_unreachable(\"invalid conversion entry!\");\n"
1733 << " case CVT_Reg:\n"
1734 << " static_cast<" << TargetOperandClass
1735 << "*>(Operands[*(p + 1)])->addRegOperands(Inst, 1);\n"
1736 << " break;\n"
1737 << " case CVT_Tied:\n"
1738 << " Inst.addOperand(Inst.getOperand(*(p + 1)));\n"
1739 << " break;\n";
1740
Chad Rosier62316fa2012-08-30 17:59:25 +00001741 std::string OperandFnBody;
1742 raw_string_ostream OpOS(OperandFnBody);
1743 // Start the operand number lookup function.
Chad Rosier22685872012-10-01 23:45:51 +00001744 OpOS << "void " << Target.getName() << ClassName << "::\n"
1745 << "convertToMapAndConstraints(unsigned Kind,\n";
Chad Rosierc69bb702012-10-02 00:25:57 +00001746 OpOS.indent(27);
Chad Rosier6e006d32012-10-12 22:53:36 +00001747 OpOS << "const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {\n"
Chad Rosier359956d2012-08-31 00:03:31 +00001748 << " assert(Kind < CVT_NUM_SIGNATURES && \"Invalid signature!\");\n"
Chad Rosier22685872012-10-01 23:45:51 +00001749 << " unsigned NumMCOperands = 0;\n"
Craig Topperb198f5c2012-09-18 01:41:49 +00001750 << " const uint8_t *Converter = ConversionTable[Kind];\n"
1751 << " for (const uint8_t *p = Converter; *p; p+= 2) {\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001752 << " switch (*p) {\n"
1753 << " default: llvm_unreachable(\"invalid conversion entry!\");\n"
1754 << " case CVT_Reg:\n"
Chad Rosier6e006d32012-10-12 22:53:36 +00001755 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
Chad Rosier1c99a7f2013-01-15 23:07:53 +00001756 << " Operands[*(p + 1)]->setConstraint(\"r\");\n"
Chad Rosier6e006d32012-10-12 22:53:36 +00001757 << " ++NumMCOperands;\n"
1758 << " break;\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001759 << " case CVT_Tied:\n"
Chad Rosier22685872012-10-01 23:45:51 +00001760 << " ++NumMCOperands;\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001761 << " break;\n";
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001762
1763 // Pre-populate the operand conversion kinds with the standard always
1764 // available entries.
1765 OperandConversionKinds.insert("CVT_Done");
1766 OperandConversionKinds.insert("CVT_Reg");
1767 OperandConversionKinds.insert("CVT_Tied");
1768 enum { CVT_Done, CVT_Reg, CVT_Tied };
1769
Chris Lattner22bc5c42010-11-01 05:06:45 +00001770 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001771 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001772 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001773
Daniel Dunbarcf120672011-02-04 17:12:15 +00001774 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001775 std::string AsmMatchConverter =
1776 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001777 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001778 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001779 II.ConversionFnKind = Signature;
1780
1781 // Check if we have already generated this signature.
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001782 if (!InstructionConversionKinds.insert(Signature))
Daniel Dunbarcf120672011-02-04 17:12:15 +00001783 continue;
1784
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001785 // Remember this converter for the kind enum.
1786 unsigned KindID = OperandConversionKinds.size();
Tim Northover12da5052013-01-10 16:47:31 +00001787 OperandConversionKinds.insert("CVT_" +
1788 getEnumNameForToken(AsmMatchConverter));
Daniel Dunbarcf120672011-02-04 17:12:15 +00001789
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001790 // Add the converter row for this instruction.
1791 ConversionTable.push_back(std::vector<uint8_t>());
1792 ConversionTable.back().push_back(KindID);
1793 ConversionTable.back().push_back(CVT_Done);
1794
1795 // Add the handler to the conversion driver function.
Tim Northover12da5052013-01-10 16:47:31 +00001796 CvtOS << " case CVT_"
1797 << getEnumNameForToken(AsmMatchConverter) << ":\n"
Chad Rosier756d2cc2012-08-31 22:12:31 +00001798 << " " << AsmMatchConverter << "(Inst, Operands);\n"
Chad Rosier359956d2012-08-31 00:03:31 +00001799 << " break;\n";
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001800
Chad Rosier62316fa2012-08-30 17:59:25 +00001801 // FIXME: Handle the operand number lookup for custom match functions.
Daniel Dunbarcf120672011-02-04 17:12:15 +00001802 continue;
1803 }
1804
Daniel Dunbar20927f22009-08-07 08:26:05 +00001805 // Build the conversion function signature.
1806 std::string Signature = "Convert";
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001807
1808 std::vector<uint8_t> ConversionRow;
Bob Wilson828295b2011-01-26 21:26:19 +00001809
Chris Lattnerdda855d2010-11-02 21:49:44 +00001810 // Compute the convert enum and the case body.
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001811 MaxRowLength = std::max(MaxRowLength, II.ResOperands.size()*2 + 1 );
1812
Chris Lattner1d13bda2010-11-04 00:43:46 +00001813 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1814 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001815
Chris Lattner1d13bda2010-11-04 00:43:46 +00001816 // Generate code to populate each result operand.
1817 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001818 case MatchableInfo::ResOperand::RenderAsmOperand: {
1819 // This comes from something we parsed.
1820 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001821
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001822 // Registers are always converted the same, don't duplicate the
1823 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001824 Signature += "__";
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001825 std::string Class;
1826 Class = Op.Class->isRegisterClass() ? "Reg" : Op.Class->ClassName;
1827 Signature += Class;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001828 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001829 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001830
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001831 // Add the conversion kind, if necessary, and get the associated ID
1832 // the index of its entry in the vector).
1833 std::string Name = "CVT_" + (Op.Class->isRegisterClass() ? "Reg" :
1834 Op.Class->RenderMethod);
Tim Northover12da5052013-01-10 16:47:31 +00001835 Name = getEnumNameForToken(Name);
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001836
1837 bool IsNewConverter = false;
1838 unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
1839 IsNewConverter);
1840
1841 // Add the operand entry to the instruction kind conversion row.
1842 ConversionRow.push_back(ID);
1843 ConversionRow.push_back(OpInfo.AsmOperandNum + 1);
1844
1845 if (!IsNewConverter)
1846 break;
1847
1848 // This is a new operand kind. Add a handler for it to the
1849 // converter driver.
1850 CvtOS << " case " << Name << ":\n"
1851 << " static_cast<" << TargetOperandClass
1852 << "*>(Operands[*(p + 1)])->"
1853 << Op.Class->RenderMethod << "(Inst, " << OpInfo.MINumOperands
1854 << ");\n"
1855 << " break;\n";
Chad Rosier62316fa2012-08-30 17:59:25 +00001856
1857 // Add a handler for the operand number lookup.
1858 OpOS << " case " << Name << ":\n"
Chad Rosier1c99a7f2013-01-15 23:07:53 +00001859 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n";
1860
1861 if (Op.Class->isRegisterClass())
1862 OpOS << " Operands[*(p + 1)]->setConstraint(\"r\");\n";
1863 else
1864 OpOS << " Operands[*(p + 1)]->setConstraint(\"m\");\n";
1865 OpOS << " NumMCOperands += " << OpInfo.MINumOperands << ";\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001866 << " break;\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001867 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001868 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001869 case MatchableInfo::ResOperand::TiedOperand: {
1870 // If this operand is tied to a previous one, just copy the MCInst
1871 // operand from the earlier one.We can only tie single MCOperand values.
Ulrich Weigandd9990622013-04-27 18:48:23 +00001872 assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001873 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001874 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001875 Signature += "__Tie" + utostr(TiedOp);
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001876 ConversionRow.push_back(CVT_Tied);
1877 ConversionRow.push_back(TiedOp);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001878 break;
1879 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001880 case MatchableInfo::ResOperand::ImmOperand: {
1881 int64_t Val = OpInfo.ImmVal;
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001882 std::string Ty = "imm_" + itostr(Val);
1883 Signature += "__" + Ty;
1884
1885 std::string Name = "CVT_" + Ty;
1886 bool IsNewConverter = false;
1887 unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
1888 IsNewConverter);
1889 // Add the operand entry to the instruction kind conversion row.
1890 ConversionRow.push_back(ID);
1891 ConversionRow.push_back(0);
1892
1893 if (!IsNewConverter)
1894 break;
1895
1896 CvtOS << " case " << Name << ":\n"
1897 << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n"
1898 << " break;\n";
1899
Chad Rosier62316fa2012-08-30 17:59:25 +00001900 OpOS << " case " << Name << ":\n"
Chad Rosier6e006d32012-10-12 22:53:36 +00001901 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
1902 << " Operands[*(p + 1)]->setConstraint(\"\");\n"
Chad Rosier22685872012-10-01 23:45:51 +00001903 << " ++NumMCOperands;\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001904 << " break;\n";
Chris Lattner98c870f2010-11-06 19:25:43 +00001905 break;
1906 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001907 case MatchableInfo::ResOperand::RegOperand: {
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001908 std::string Reg, Name;
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001909 if (OpInfo.Register == 0) {
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001910 Name = "reg0";
1911 Reg = "0";
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001912 } else {
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001913 Reg = getQualifiedName(OpInfo.Register);
1914 Name = "reg" + OpInfo.Register->getName();
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001915 }
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001916 Signature += "__" + Name;
1917 Name = "CVT_" + Name;
1918 bool IsNewConverter = false;
1919 unsigned ID = getConverterOperandID(Name, OperandConversionKinds,
1920 IsNewConverter);
1921 // Add the operand entry to the instruction kind conversion row.
1922 ConversionRow.push_back(ID);
1923 ConversionRow.push_back(0);
1924
1925 if (!IsNewConverter)
1926 break;
1927 CvtOS << " case " << Name << ":\n"
1928 << " Inst.addOperand(MCOperand::CreateReg(" << Reg << "));\n"
1929 << " break;\n";
Chad Rosier62316fa2012-08-30 17:59:25 +00001930
1931 OpOS << " case " << Name << ":\n"
Chad Rosier6e006d32012-10-12 22:53:36 +00001932 << " Operands[*(p + 1)]->setMCOperandNum(NumMCOperands);\n"
1933 << " Operands[*(p + 1)]->setConstraint(\"m\");\n"
Chad Rosier22685872012-10-01 23:45:51 +00001934 << " ++NumMCOperands;\n"
Chad Rosier62316fa2012-08-30 17:59:25 +00001935 << " break;\n";
Bob Wilson828295b2011-01-26 21:26:19 +00001936 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001937 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001938 }
Bob Wilson828295b2011-01-26 21:26:19 +00001939
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001940 // If there were no operands, add to the signature to that effect
1941 if (Signature == "Convert")
1942 Signature += "_NoOperands";
1943
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001944 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001945
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001946 // Save the signature. If we already have it, don't add a new row
1947 // to the table.
1948 if (!InstructionConversionKinds.insert(Signature))
Daniel Dunbar20927f22009-08-07 08:26:05 +00001949 continue;
1950
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001951 // Add the row to the table.
1952 ConversionTable.push_back(ConversionRow);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001953 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001954
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001955 // Finish up the converter driver function.
Chad Rosierad2d3e62012-09-03 17:39:57 +00001956 CvtOS << " }\n }\n}\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001957
Chad Rosier62316fa2012-08-30 17:59:25 +00001958 // Finish up the operand number lookup function.
Chad Rosier22685872012-10-01 23:45:51 +00001959 OpOS << " }\n }\n}\n\n";
Chad Rosier62316fa2012-08-30 17:59:25 +00001960
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001961 OS << "namespace {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001962
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001963 // Output the operand conversion kind enum.
1964 OS << "enum OperatorConversionKind {\n";
1965 for (unsigned i = 0, e = OperandConversionKinds.size(); i != e; ++i)
1966 OS << " " << OperandConversionKinds[i] << ",\n";
1967 OS << " CVT_NUM_CONVERTERS\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001968 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001969
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001970 // Output the instruction conversion kind enum.
1971 OS << "enum InstructionConversionKind {\n";
1972 for (SetVector<std::string>::const_iterator
1973 i = InstructionConversionKinds.begin(),
1974 e = InstructionConversionKinds.end(); i != e; ++i)
1975 OS << " " << *i << ",\n";
1976 OS << " CVT_NUM_SIGNATURES\n";
1977 OS << "};\n\n";
1978
1979
1980 OS << "} // end anonymous namespace\n\n";
1981
1982 // Output the conversion table.
Craig Topperb198f5c2012-09-18 01:41:49 +00001983 OS << "static const uint8_t ConversionTable[CVT_NUM_SIGNATURES]["
Jim Grosbachc8f267f2012-08-22 01:06:23 +00001984 << MaxRowLength << "] = {\n";
1985
1986 for (unsigned Row = 0, ERow = ConversionTable.size(); Row != ERow; ++Row) {
1987 assert(ConversionTable[Row].size() % 2 == 0 && "bad conversion row!");
1988 OS << " // " << InstructionConversionKinds[Row] << "\n";
1989 OS << " { ";
1990 for (unsigned i = 0, e = ConversionTable[Row].size(); i != e; i += 2)
1991 OS << OperandConversionKinds[ConversionTable[Row][i]] << ", "
1992 << (unsigned)(ConversionTable[Row][i + 1]) << ", ";
1993 OS << "CVT_Done },\n";
1994 }
1995
1996 OS << "};\n\n";
1997
1998 // Spit out the conversion driver function.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001999 OS << CvtOS.str();
Jim Grosbachc8f267f2012-08-22 01:06:23 +00002000
Chad Rosier62316fa2012-08-30 17:59:25 +00002001 // Spit out the operand number lookup function.
2002 OS << OpOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00002003}
2004
Jim Grosbach8caecde2012-04-19 17:52:32 +00002005/// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
2006static void emitMatchClassEnumeration(CodeGenTarget &Target,
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002007 std::vector<ClassInfo*> &Infos,
2008 raw_ostream &OS) {
2009 OS << "namespace {\n\n";
2010
2011 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
2012 << "/// instruction matching.\n";
2013 OS << "enum MatchClassKind {\n";
2014 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002015 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002016 ie = Infos.end(); it != ie; ++it) {
2017 ClassInfo &CI = **it;
2018 OS << " " << CI.Name << ", // ";
2019 if (CI.Kind == ClassInfo::Token) {
2020 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002021 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002022 if (!CI.ValueName.empty())
2023 OS << "register class '" << CI.ValueName << "'\n";
2024 else
2025 OS << "derived register class\n";
2026 } else {
2027 OS << "user defined class '" << CI.ValueName << "'\n";
2028 }
2029 }
2030 OS << " NumMatchClassKinds\n";
2031 OS << "};\n\n";
2032
2033 OS << "}\n\n";
2034}
2035
Jim Grosbach8caecde2012-04-19 17:52:32 +00002036/// emitValidateOperandClass - Emit the function to validate an operand class.
2037static void emitValidateOperandClass(AsmMatcherInfo &Info,
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002038 raw_ostream &OS) {
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002039 OS << "static unsigned validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002040 << "MatchClassKind Kind) {\n";
2041 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00002042 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002043
Kevin Enderby89381832011-07-15 18:30:43 +00002044 // The InvalidMatchClass is not to match any operand.
2045 OS << " if (Kind == InvalidMatchClass)\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002046 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n\n";
Kevin Enderby89381832011-07-15 18:30:43 +00002047
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002048 // Check for Token operands first.
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002049 // FIXME: Use a more specific diagnostic type.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002050 OS << " if (Operand.isToken())\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002051 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
2052 << " MCTargetAsmParser::Match_Success :\n"
2053 << " MCTargetAsmParser::Match_InvalidOperand;\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002054
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002055 // Check the user classes. We don't care what order since we're only
2056 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00002057 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002058 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002059 ClassInfo &CI = **it;
2060
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002061 if (!CI.isUserClass())
2062 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00002063
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002064 OS << " // '" << CI.ClassName << "' class\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002065 OS << " if (Kind == " << CI.Name << ") {\n";
2066 OS << " if (Operand." << CI.PredicateMethod << "())\n";
2067 OS << " return MCTargetAsmParser::Match_Success;\n";
2068 if (!CI.DiagnosticType.empty())
2069 OS << " return " << Info.Target.getName() << "AsmParser::Match_"
2070 << CI.DiagnosticType << ";\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00002071 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002072 }
Bob Wilson828295b2011-01-26 21:26:19 +00002073
Owen Andersonb885dc82012-07-16 23:20:09 +00002074 // Check for register operands, including sub-classes.
2075 OS << " if (Operand.isReg()) {\n";
2076 OS << " MatchClassKind OpKind;\n";
2077 OS << " switch (Operand.getReg()) {\n";
2078 OS << " default: OpKind = InvalidMatchClass; break;\n";
Sean Silvadecfdf52012-09-19 01:47:01 +00002079 for (AsmMatcherInfo::RegisterClassesTy::iterator
Owen Andersonb885dc82012-07-16 23:20:09 +00002080 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
2081 it != ie; ++it)
2082 OS << " case " << Info.Target.getName() << "::"
2083 << it->first->getName() << ": OpKind = " << it->second->Name
2084 << "; break;\n";
2085 OS << " }\n";
2086 OS << " return isSubclass(OpKind, Kind) ? "
2087 << "MCTargetAsmParser::Match_Success :\n "
2088 << " MCTargetAsmParser::Match_InvalidOperand;\n }\n\n";
2089
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002090 // Generic fallthrough match failure case for operands that don't have
2091 // specialized diagnostic types.
2092 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002093 OS << "}\n\n";
2094}
2095
Jim Grosbach8caecde2012-04-19 17:52:32 +00002096/// emitIsSubclass - Emit the subclass predicate function.
2097static void emitIsSubclass(CodeGenTarget &Target,
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002098 std::vector<ClassInfo*> &Infos,
2099 raw_ostream &OS) {
Dmitri Gribenko4e0ae442012-09-15 20:22:05 +00002100 OS << "/// isSubclass - Compute whether \\p A is a subclass of \\p B.\n";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002101 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002102 OS << " if (A == B)\n";
2103 OS << " return true;\n\n";
2104
Reid Kleckner47cfec02013-08-06 22:51:21 +00002105 std::string OStr;
2106 raw_string_ostream SS(OStr);
Aaron Ballman54911a52013-07-15 16:53:32 +00002107 unsigned Count = 0;
2108 SS << " switch (A) {\n";
2109 SS << " default:\n";
2110 SS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002111 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002112 ie = Infos.end(); it != ie; ++it) {
2113 ClassInfo &A = **it;
2114
Jim Grosbacha66512e2011-12-06 23:43:54 +00002115 std::vector<StringRef> SuperClasses;
2116 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
2117 ie = Infos.end(); it != ie; ++it) {
2118 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002119
Jim Grosbacha66512e2011-12-06 23:43:54 +00002120 if (&A != &B && A.isSubsetOf(B))
2121 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002122 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00002123
2124 if (SuperClasses.empty())
2125 continue;
Aaron Ballman54911a52013-07-15 16:53:32 +00002126 ++Count;
Jim Grosbacha66512e2011-12-06 23:43:54 +00002127
Aaron Ballman54911a52013-07-15 16:53:32 +00002128 SS << "\n case " << A.Name << ":\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00002129
2130 if (SuperClasses.size() == 1) {
Aaron Ballman54911a52013-07-15 16:53:32 +00002131 SS << " return B == " << SuperClasses.back().str() << ";\n";
Jim Grosbacha66512e2011-12-06 23:43:54 +00002132 continue;
2133 }
2134
Aaron Ballman54911a52013-07-15 16:53:32 +00002135 if (!SuperClasses.empty()) {
2136 SS << " switch (B) {\n";
2137 SS << " default: return false;\n";
2138 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
2139 SS << " case " << SuperClasses[i].str() << ": return true;\n";
2140 SS << " }\n";
2141 } else {
2142 // No case statement to emit
2143 SS << " return false;\n";
2144 }
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002145 }
Aaron Ballman54911a52013-07-15 16:53:32 +00002146 SS << " }\n";
2147
2148 // If there were case statements emitted into the string stream, write them
2149 // to the output stream, otherwise write the default.
2150 if (Count)
2151 OS << SS.str();
2152 else
2153 OS << " return false;\n";
2154
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002155 OS << "}\n\n";
2156}
2157
Jim Grosbach8caecde2012-04-19 17:52:32 +00002158/// emitMatchTokenString - Emit the function to match a token string to the
Daniel Dunbar245f0582009-08-08 21:22:41 +00002159/// appropriate match class value.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002160static void emitMatchTokenString(CodeGenTarget &Target,
Daniel Dunbar245f0582009-08-08 21:22:41 +00002161 std::vector<ClassInfo*> &Infos,
2162 raw_ostream &OS) {
2163 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00002164 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002165 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00002166 ie = Infos.end(); it != ie; ++it) {
2167 ClassInfo &CI = **it;
2168
2169 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00002170 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
2171 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00002172 }
2173
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002174 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00002175
Chris Lattner5845e5c2010-09-06 02:01:51 +00002176 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00002177
2178 OS << " return InvalidMatchClass;\n";
2179 OS << "}\n\n";
2180}
Chris Lattner70add882009-08-08 20:02:57 +00002181
Jim Grosbach8caecde2012-04-19 17:52:32 +00002182/// emitMatchRegisterName - Emit the function to match a string to the target
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002183/// specific register enum.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002184static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002185 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00002186 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00002187 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00002188 const std::vector<CodeGenRegister*> &Regs =
2189 Target.getRegBank().getRegisters();
2190 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
2191 const CodeGenRegister *Reg = Regs[i];
2192 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00002193 continue;
2194
Chris Lattner5845e5c2010-09-06 02:01:51 +00002195 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00002196 Reg->TheDef->getValueAsString("AsmName"),
2197 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00002198 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002199
Chris Lattnerb8d6e982010-02-09 00:34:28 +00002200 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00002201
Chris Lattner5845e5c2010-09-06 02:01:51 +00002202 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00002203
Daniel Dunbar245f0582009-08-08 21:22:41 +00002204 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00002205 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002206}
Daniel Dunbara027d222009-07-31 02:32:59 +00002207
Jim Grosbach8caecde2012-04-19 17:52:32 +00002208/// emitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
Daniel Dunbar54074b52010-07-19 05:44:09 +00002209/// definitions.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002210static void emitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00002211 raw_ostream &OS) {
2212 OS << "// Flags for subtarget features that participate in "
2213 << "instruction matching.\n";
2214 OS << "enum SubtargetFeatureFlag {\n";
Tim Northover6dd670a2013-09-16 16:43:16 +00002215 for (std::map<Record*, SubtargetFeatureInfo*, LessRecordByID>::const_iterator
Daniel Dunbar54074b52010-07-19 05:44:09 +00002216 it = Info.SubtargetFeatures.begin(),
2217 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
2218 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00002219 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002220 }
2221 OS << " Feature_None = 0\n";
2222 OS << "};\n\n";
2223}
2224
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002225/// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
2226static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
2227 // Get the set of diagnostic types from all of the operand classes.
2228 std::set<StringRef> Types;
2229 for (std::map<Record*, ClassInfo*>::const_iterator
2230 I = Info.AsmOperandClasses.begin(),
2231 E = Info.AsmOperandClasses.end(); I != E; ++I) {
2232 if (!I->second->DiagnosticType.empty())
2233 Types.insert(I->second->DiagnosticType);
2234 }
2235
2236 if (Types.empty()) return;
2237
2238 // Now emit the enum entries.
2239 for (std::set<StringRef>::const_iterator I = Types.begin(), E = Types.end();
2240 I != E; ++I)
2241 OS << " Match_" << *I << ",\n";
2242 OS << " END_OPERAND_DIAGNOSTIC_TYPES\n";
2243}
2244
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002245/// emitGetSubtargetFeatureName - Emit the helper function to get the
2246/// user-level name for a subtarget feature.
2247static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
2248 OS << "// User-level names for subtarget features that participate in\n"
2249 << "// instruction matching.\n"
Aaron Ballman54911a52013-07-15 16:53:32 +00002250 << "static const char *getSubtargetFeatureName(unsigned Val) {\n";
2251 if (!Info.SubtargetFeatures.empty()) {
2252 OS << " switch(Val) {\n";
Tim Northover6dd670a2013-09-16 16:43:16 +00002253 typedef std::map<Record*, SubtargetFeatureInfo*, LessRecordByID> RecFeatMap;
2254 for (RecFeatMap::const_iterator it = Info.SubtargetFeatures.begin(),
2255 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
Aaron Ballman54911a52013-07-15 16:53:32 +00002256 SubtargetFeatureInfo &SFI = *it->second;
2257 // FIXME: Totally just a placeholder name to get the algorithm working.
2258 OS << " case " << SFI.getEnumName() << ": return \""
2259 << SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
2260 }
2261 OS << " default: return \"(unknown)\";\n";
2262 OS << " }\n";
2263 } else {
2264 // Nothing to emit, so skip the switch
2265 OS << " return \"(unknown)\";\n";
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002266 }
Aaron Ballman54911a52013-07-15 16:53:32 +00002267 OS << "}\n\n";
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002268}
2269
Jim Grosbach8caecde2012-04-19 17:52:32 +00002270/// emitComputeAvailableFeatures - Emit the function to compute the list of
Daniel Dunbar54074b52010-07-19 05:44:09 +00002271/// available features given a subtarget.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002272static void emitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00002273 raw_ostream &OS) {
2274 std::string ClassName =
2275 Info.AsmParser->getValueAsString("AsmParserClassName");
2276
Chris Lattner02bcbc92010-11-01 01:37:30 +00002277 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00002278 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002279 OS << " unsigned Features = 0;\n";
Tim Northover6dd670a2013-09-16 16:43:16 +00002280 for (std::map<Record*, SubtargetFeatureInfo*, LessRecordByID>::const_iterator
Daniel Dunbar54074b52010-07-19 05:44:09 +00002281 it = Info.SubtargetFeatures.begin(),
2282 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
2283 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00002284
2285 OS << " if (";
Jim Grosbach65da6fc2012-04-17 00:01:04 +00002286 std::string CondStorage =
2287 SFI.TheDef->getValueAsString("AssemblerCondString");
Evan Chengfbc38d22011-07-08 18:04:22 +00002288 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00002289 std::pair<StringRef,StringRef> Comma = Conds.split(',');
2290 bool First = true;
2291 do {
2292 if (!First)
2293 OS << " && ";
2294
2295 bool Neg = false;
2296 StringRef Cond = Comma.first;
2297 if (Cond[0] == '!') {
2298 Neg = true;
2299 Cond = Cond.substr(1);
2300 }
2301
2302 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
2303 if (Neg)
2304 OS << " == 0";
2305 else
2306 OS << " != 0";
2307 OS << ")";
2308
2309 if (Comma.second.empty())
2310 break;
2311
2312 First = false;
2313 Comma = Comma.second.split(',');
2314 } while (true);
2315
2316 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002317 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002318 }
2319 OS << " return Features;\n";
2320 OS << "}\n\n";
2321}
2322
Chris Lattner6fa152c2010-10-30 20:15:02 +00002323static std::string GetAliasRequiredFeatures(Record *R,
2324 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00002325 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00002326 std::string Result;
2327 unsigned NumFeatures = 0;
2328 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00002329 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00002330
Chris Lattner4a74ee72010-11-01 02:09:21 +00002331 if (F == 0)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002332 PrintFatalError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
Chris Lattner4a74ee72010-11-01 02:09:21 +00002333 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00002334
Chris Lattner4a74ee72010-11-01 02:09:21 +00002335 if (NumFeatures)
2336 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00002337
Chris Lattner4a74ee72010-11-01 02:09:21 +00002338 Result += F->getEnumName();
2339 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00002340 }
Bob Wilson828295b2011-01-26 21:26:19 +00002341
Chris Lattner693173f2010-10-30 19:23:13 +00002342 if (NumFeatures > 1)
2343 Result = '(' + Result + ')';
2344 return Result;
2345}
2346
Chad Rosier88eb89b2013-04-18 22:35:36 +00002347static void emitMnemonicAliasVariant(raw_ostream &OS,const AsmMatcherInfo &Info,
2348 std::vector<Record*> &Aliases,
2349 unsigned Indent = 0,
2350 StringRef AsmParserVariantName = StringRef()){
Chris Lattner4fd32c62010-10-30 18:56:12 +00002351 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
2352 // iteration order of the map is stable.
2353 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00002354
Chris Lattner674c1dc2010-10-30 17:36:36 +00002355 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
2356 Record *R = Aliases[i];
Chad Rosier88eb89b2013-04-18 22:35:36 +00002357 // FIXME: Allow AssemblerVariantName to be a comma separated list.
2358 std::string AsmVariantName = R->getValueAsString("AsmVariantName");
2359 if (AsmVariantName != AsmParserVariantName)
2360 continue;
Chris Lattner4fd32c62010-10-30 18:56:12 +00002361 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00002362 }
Chad Rosier88eb89b2013-04-18 22:35:36 +00002363 if (AliasesFromMnemonic.empty())
2364 return;
Vladimir Medic92731512013-07-16 09:22:38 +00002365
Chris Lattner4fd32c62010-10-30 18:56:12 +00002366 // Process each alias a "from" mnemonic at a time, building the code executed
2367 // by the string remapper.
2368 std::vector<StringMatcher::StringPair> Cases;
2369 for (std::map<std::string, std::vector<Record*> >::iterator
2370 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
2371 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00002372 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00002373
2374 // Loop through each alias and emit code that handles each case. If there
2375 // are two instructions without predicates, emit an error. If there is one,
2376 // emit it last.
2377 std::string MatchCode;
2378 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00002379
Chris Lattner693173f2010-10-30 19:23:13 +00002380 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
2381 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00002382 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002383
Chris Lattner693173f2010-10-30 19:23:13 +00002384 // If this unconditionally matches, remember it for later and diagnose
2385 // duplicates.
2386 if (FeatureMask.empty()) {
2387 if (AliasWithNoPredicate != -1) {
2388 // We can't have two aliases from the same mnemonic with no predicate.
2389 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
2390 "two MnemonicAliases with the same 'from' mnemonic!");
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002391 PrintFatalError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00002392 }
Bob Wilson828295b2011-01-26 21:26:19 +00002393
Chris Lattner693173f2010-10-30 19:23:13 +00002394 AliasWithNoPredicate = i;
2395 continue;
2396 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00002397 if (R->getValueAsString("ToMnemonic") == I->first)
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +00002398 PrintFatalError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00002399
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00002400 if (!MatchCode.empty())
2401 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00002402 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
2403 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00002404 }
Bob Wilson828295b2011-01-26 21:26:19 +00002405
Chris Lattner693173f2010-10-30 19:23:13 +00002406 if (AliasWithNoPredicate != -1) {
2407 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00002408 if (!MatchCode.empty())
2409 MatchCode += "else\n ";
2410 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00002411 }
Bob Wilson828295b2011-01-26 21:26:19 +00002412
Chris Lattner693173f2010-10-30 19:23:13 +00002413 MatchCode += "return;";
2414
2415 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00002416 }
Chad Rosier88eb89b2013-04-18 22:35:36 +00002417 StringMatcher("Mnemonic", Cases, OS).Emit(Indent);
2418}
Bob Wilson828295b2011-01-26 21:26:19 +00002419
Chad Rosier88eb89b2013-04-18 22:35:36 +00002420/// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
2421/// emit a function for them and return true, otherwise return false.
2422static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info,
2423 CodeGenTarget &Target) {
2424 // Ignore aliases when match-prefix is set.
2425 if (!MatchPrefix.empty())
2426 return false;
2427
2428 std::vector<Record*> Aliases =
2429 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
2430 if (Aliases.empty()) return false;
2431
2432 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
2433 "unsigned Features, unsigned VariantID) {\n";
2434 OS << " switch (VariantID) {\n";
2435 unsigned VariantCount = Target.getAsmParserVariantCount();
2436 for (unsigned VC = 0; VC != VariantCount; ++VC) {
2437 Record *AsmVariant = Target.getAsmParserVariant(VC);
2438 int AsmParserVariantNo = AsmVariant->getValueAsInt("Variant");
2439 std::string AsmParserVariantName = AsmVariant->getValueAsString("Name");
2440 OS << " case " << AsmParserVariantNo << ":\n";
2441 emitMnemonicAliasVariant(OS, Info, Aliases, /*Indent=*/2,
2442 AsmParserVariantName);
2443 OS << " break;\n";
2444 }
2445 OS << " }\n";
2446
2447 // Emit aliases that apply to all variants.
2448 emitMnemonicAliasVariant(OS, Info, Aliases);
2449
Daniel Dunbar55b5e852011-01-18 01:59:30 +00002450 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00002451
Chris Lattner7fd44892010-10-30 18:48:18 +00002452 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00002453}
2454
Jim Grosbach194f3fa2012-03-01 17:30:35 +00002455static const char *getMinimalTypeForRange(uint64_t Range) {
2456 assert(Range < 0xFFFFFFFFULL && "Enum too large");
2457 if (Range > 0xFFFF)
2458 return "uint32_t";
2459 if (Range > 0xFF)
2460 return "uint16_t";
2461 return "uint8_t";
2462}
2463
Jim Grosbach8caecde2012-04-19 17:52:32 +00002464static void emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
Craig Topper3a364442012-09-18 07:02:21 +00002465 const AsmMatcherInfo &Info, StringRef ClassName,
2466 StringToOffsetTable &StringTable,
2467 unsigned MaxMnemonicIndex) {
2468 unsigned MaxMask = 0;
2469 for (std::vector<OperandMatchEntry>::const_iterator it =
2470 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2471 it != ie; ++it) {
2472 MaxMask |= it->OperandMask;
2473 }
2474
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002475 // Emit the static custom operand parsing table;
2476 OS << "namespace {\n";
2477 OS << " struct OperandMatchEntry {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002478 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
Craig Topperfab3f7e2012-04-02 07:48:39 +00002479 << " RequiredFeatures;\n";
Craig Topper3a364442012-09-18 07:02:21 +00002480 OS << " " << getMinimalTypeForRange(MaxMnemonicIndex)
2481 << " Mnemonic;\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002482 OS << " " << getMinimalTypeForRange(Info.Classes.size())
Craig Topper3a364442012-09-18 07:02:21 +00002483 << " Class;\n";
2484 OS << " " << getMinimalTypeForRange(MaxMask)
2485 << " OperandMask;\n\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002486 OS << " StringRef getMnemonic() const {\n";
2487 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
2488 OS << " MnemonicTable[Mnemonic]);\n";
2489 OS << " }\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002490 OS << " };\n\n";
2491
2492 OS << " // Predicate for searching for an opcode.\n";
2493 OS << " struct LessOpcodeOperand {\n";
2494 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002495 OS << " return LHS.getMnemonic() < RHS;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002496 OS << " }\n";
2497 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002498 OS << " return LHS < RHS.getMnemonic();\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002499 OS << " }\n";
2500 OS << " bool operator()(const OperandMatchEntry &LHS,";
2501 OS << " const OperandMatchEntry &RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002502 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002503 OS << " }\n";
2504 OS << " };\n";
2505
2506 OS << "} // end anonymous namespace.\n\n";
2507
2508 OS << "static const OperandMatchEntry OperandMatchTable["
2509 << Info.OperandMatchInfo.size() << "] = {\n";
2510
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002511 OS << " /* Operand List Mask, Mnemonic, Operand Class, Features */\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002512 for (std::vector<OperandMatchEntry>::const_iterator it =
2513 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2514 it != ie; ++it) {
2515 const OperandMatchEntry &OMI = *it;
2516 const MatchableInfo &II = *OMI.MI;
2517
Craig Topper3a364442012-09-18 07:02:21 +00002518 OS << " { ";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002519
Craig Topper3a364442012-09-18 07:02:21 +00002520 // Write the required features mask.
2521 if (!II.RequiredFeatures.empty()) {
2522 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2523 if (i) OS << "|";
2524 OS << II.RequiredFeatures[i]->getEnumName();
2525 }
2526 } else
2527 OS << "0";
2528
2529 // Store a pascal-style length byte in the mnemonic.
2530 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
2531 OS << ", " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
2532 << " /* " << II.Mnemonic << " */, ";
2533
2534 OS << OMI.CI->Name;
2535
2536 OS << ", " << OMI.OperandMask;
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002537 OS << " /* ";
2538 bool printComma = false;
2539 for (int i = 0, e = 31; i !=e; ++i)
2540 if (OMI.OperandMask & (1 << i)) {
2541 if (printComma)
2542 OS << ", ";
2543 OS << i;
2544 printComma = true;
2545 }
2546 OS << " */";
2547
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002548 OS << " },\n";
2549 }
2550 OS << "};\n\n";
2551
2552 // Emit the operand class switch to call the correct custom parser for
2553 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002554 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2555 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002556 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002557 << " &Operands,\n unsigned MCK) {\n\n"
2558 << " switch(MCK) {\n";
2559
2560 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2561 ie = Info.Classes.end(); it != ie; ++it) {
2562 ClassInfo *CI = *it;
2563 if (CI->ParserMethod.empty())
2564 continue;
2565 OS << " case " << CI->Name << ":\n"
2566 << " return " << CI->ParserMethod << "(Operands);\n";
2567 }
2568
2569 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002570 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002571 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002572 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002573 OS << "}\n\n";
2574
2575 // Emit the static custom operand parser. This code is very similar with
2576 // the other matcher. Also use MatchResultTy here just in case we go for
2577 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002578 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002579 << Target.getName() << ClassName << "::\n"
2580 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2581 << " &Operands,\n StringRef Mnemonic) {\n";
2582
2583 // Emit code to get the available features.
2584 OS << " // Get the current feature set.\n";
2585 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2586
2587 OS << " // Get the next operand index.\n";
2588 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2589
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002590 // Emit code to search the table.
2591 OS << " // Search the table.\n";
2592 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2593 OS << " MnemonicRange =\n";
2594 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2595 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2596 << " LessOpcodeOperand());\n\n";
2597
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002598 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002599 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002600
2601 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2602 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2603
2604 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002605 OS << " assert(Mnemonic == it->getMnemonic());\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002606
2607 // Emit check that the required features are available.
2608 OS << " // check if the available features match\n";
2609 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2610 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002611 OS << " continue;\n";
2612 OS << " }\n\n";
2613
2614 // Emit check to ensure the operand number matches.
2615 OS << " // check if the operand in question has a custom parser.\n";
2616 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2617 OS << " continue;\n\n";
2618
2619 // Emit call to the custom parser method
2620 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002621 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002622 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002623 OS << " if (Result != MatchOperand_NoMatch)\n";
2624 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002625 OS << " }\n\n";
2626
Jim Grosbachf922c472011-02-12 01:34:40 +00002627 OS << " // Okay, we had no match.\n";
2628 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002629 OS << "}\n\n";
2630}
2631
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002632void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002633 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002634 Record *AsmParser = Target.getAsmParser();
2635 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2636
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002637 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002638 AsmMatcherInfo Info(AsmParser, Target, Records);
Jim Grosbach8caecde2012-04-19 17:52:32 +00002639 Info.buildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002640
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002641 // Sort the instruction table using the partial order on classes. We use
2642 // stable_sort to ensure that ambiguous instructions are still
2643 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002644 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2645 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002646
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002647 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002648 for (std::vector<MatchableInfo*>::iterator
2649 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002650 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002651 (*it)->dump();
2652 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002653
Chris Lattner22bc5c42010-11-01 05:06:45 +00002654 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002655 DEBUG_WITH_TYPE("ambiguous_instrs", {
2656 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002657 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002658 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002659 MatchableInfo &A = *Info.Matchables[i];
2660 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002661
Jim Grosbach8caecde2012-04-19 17:52:32 +00002662 if (A.couldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002663 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002664 A.dump();
2665 errs() << "\nis incomparable with:\n";
2666 B.dump();
2667 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002668 ++NumAmbiguous;
2669 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002670 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002671 }
Chris Lattner87410362010-09-06 20:21:47 +00002672 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002673 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002674 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002675 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002676
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002677 // Compute the information on the custom operand parsing.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002678 Info.buildOperandMatchInfo();
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002679
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002680 // Write the output.
2681
Chris Lattner0692ee62010-09-06 19:11:01 +00002682 // Information for the class declaration.
2683 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2684 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002685 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002686 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002687 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Chad Rosier90e11f82012-09-05 01:02:38 +00002688 OS << " void convertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002689 << "unsigned Opcode,\n"
Chad Rosierc69bb702012-10-02 00:25:57 +00002690 << " const SmallVectorImpl<MCParsedAsmOperand*> "
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002691 << "&Operands);\n";
Chad Rosierc69bb702012-10-02 00:25:57 +00002692 OS << " void convertToMapAndConstraints(unsigned Kind,\n ";
Chad Rosier6e006d32012-10-12 22:53:36 +00002693 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands);\n";
Craig Topperf63ef912013-07-24 07:33:14 +00002694 OS << " bool mnemonicIsValid(StringRef Mnemonic, unsigned VariantID);\n";
Chad Rosier9ba9d4d2012-10-05 18:41:14 +00002695 OS << " unsigned MatchInstructionImpl(\n";
2696 OS.indent(27);
2697 OS << "const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n"
Chad Rosier6e006d32012-10-12 22:53:36 +00002698 << " MCInst &Inst,\n"
Chad Rosierc69bb702012-10-02 00:25:57 +00002699 << " unsigned &ErrorInfo,"
2700 << " bool matchingInlineAsm,\n"
2701 << " unsigned VariantID = 0);\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002702
2703 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002704 OS << "\n enum OperandMatchResultTy {\n";
2705 OS << " MatchOperand_Success, // operand matched successfully\n";
2706 OS << " MatchOperand_NoMatch, // operand did not match\n";
2707 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2708 OS << " };\n";
2709 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002710 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2711 OS << " StringRef Mnemonic);\n";
2712
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002713 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002714 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2715 OS << " unsigned MCK);\n\n";
2716 }
2717
Chris Lattner0692ee62010-09-06 19:11:01 +00002718 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2719
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002720 // Emit the operand match diagnostic enum names.
2721 OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
2722 OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2723 emitOperandDiagnosticTypes(Info, OS);
2724 OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2725
2726
Chris Lattner0692ee62010-09-06 19:11:01 +00002727 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2728 OS << "#undef GET_REGISTER_MATCHER\n\n";
2729
Daniel Dunbar54074b52010-07-19 05:44:09 +00002730 // Emit the subtarget feature enumeration.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002731 emitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002732
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002733 // Emit the function to match a register name to number.
Akira Hatanaka72e9b6a2012-08-17 20:16:42 +00002734 // This should be omitted for Mips target
2735 if (AsmParser->getValueAsBit("ShouldEmitMatchRegisterName"))
2736 emitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002737
2738 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002739
Craig Topper8030e1a2012-04-25 06:56:34 +00002740 OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
2741 OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002742
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002743 // Generate the helper function to get the names for subtarget features.
2744 emitGetSubtargetFeatureName(Info, OS);
2745
Craig Topper8030e1a2012-04-25 06:56:34 +00002746 OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
2747
2748 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2749 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
2750
Chris Lattner7fd44892010-10-30 18:48:18 +00002751 // Generate the function that remaps for mnemonic aliases.
Chad Rosier88eb89b2013-04-18 22:35:36 +00002752 bool HasMnemonicAliases = emitMnemonicAliases(OS, Info, Target);
Bob Wilson828295b2011-01-26 21:26:19 +00002753
Chad Rosier22685872012-10-01 23:45:51 +00002754 // Generate the convertToMCInst function to convert operands into an MCInst.
2755 // Also, generate the convertToMapAndConstraints function for MS-style inline
2756 // assembly. The latter doesn't actually generate a MCInst.
2757 emitConvertFuncs(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002758
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002759 // Emit the enumeration for classes which participate in matching.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002760 emitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002761
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002762 // Emit the routine to match token strings to their match class.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002763 emitMatchTokenString(Target, Info.Classes, OS);
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002764
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002765 // Emit the subclass predicate routine.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002766 emitIsSubclass(Target, Info.Classes, OS);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002767
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002768 // Emit the routine to validate an operand against a match class.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002769 emitValidateOperandClass(Info, OS);
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002770
Daniel Dunbar54074b52010-07-19 05:44:09 +00002771 // Emit the available features compute function.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002772 emitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002773
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002774
Craig Topperfee7f012012-09-18 06:10:45 +00002775 StringToOffsetTable StringTable;
2776
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002777 size_t MaxNumOperands = 0;
Craig Topperfee7f012012-09-18 06:10:45 +00002778 unsigned MaxMnemonicIndex = 0;
Joey Gouly715d98d2013-09-12 10:28:05 +00002779 bool HasDeprecation = false;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002780 for (std::vector<MatchableInfo*>::const_iterator it =
2781 Info.Matchables.begin(), ie = Info.Matchables.end();
Craig Topperfee7f012012-09-18 06:10:45 +00002782 it != ie; ++it) {
2783 MatchableInfo &II = **it;
2784 MaxNumOperands = std::max(MaxNumOperands, II.AsmOperands.size());
Joey Gouly715d98d2013-09-12 10:28:05 +00002785 HasDeprecation |= II.HasDeprecation;
Craig Topperfee7f012012-09-18 06:10:45 +00002786
2787 // Store a pascal-style length byte in the mnemonic.
2788 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
2789 MaxMnemonicIndex = std::max(MaxMnemonicIndex,
2790 StringTable.GetOrAddStringOffset(LenMnemonic, false));
2791 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002792
Craig Topper3a364442012-09-18 07:02:21 +00002793 OS << "static const char *const MnemonicTable =\n";
2794 StringTable.EmitString(OS);
2795 OS << ";\n\n";
2796
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002797 // Emit the static match table; unused classes get initalized to 0 which is
2798 // guaranteed to be InvalidMatchClass.
2799 //
2800 // FIXME: We can reduce the size of this table very easily. First, we change
2801 // it so that store the kinds in separate bit-fields for each index, which
2802 // only needs to be the max width used for classes at that index (we also need
2803 // to reject based on this during classification). If we then make sure to
2804 // order the match kinds appropriately (putting mnemonics last), then we
2805 // should only end up using a few bits for each class, especially the ones
2806 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002807 OS << "namespace {\n";
2808 OS << " struct MatchEntry {\n";
Craig Topperfee7f012012-09-18 06:10:45 +00002809 OS << " " << getMinimalTypeForRange(MaxMnemonicIndex)
2810 << " Mnemonic;\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002811 OS << " uint16_t Opcode;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002812 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2813 << " ConvertFn;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002814 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2815 << " RequiredFeatures;\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002816 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2817 << " Classes[" << MaxNumOperands << "];\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002818 OS << " StringRef getMnemonic() const {\n";
2819 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
2820 OS << " MnemonicTable[Mnemonic]);\n";
2821 OS << " }\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002822 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002823
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002824 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002825 OS << " struct LessOpcode {\n";
2826 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002827 OS << " return LHS.getMnemonic() < RHS;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002828 OS << " }\n";
2829 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002830 OS << " return LHS < RHS.getMnemonic();\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002831 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002832 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002833 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002834 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002835 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002836
Chris Lattner96352e52010-09-06 21:08:38 +00002837 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002838
Craig Topperf63ef912013-07-24 07:33:14 +00002839 unsigned VariantCount = Target.getAsmParserVariantCount();
2840 for (unsigned VC = 0; VC != VariantCount; ++VC) {
2841 Record *AsmVariant = Target.getAsmParserVariant(VC);
2842 std::string CommentDelimiter =
2843 AsmVariant->getValueAsString("CommentDelimiter");
2844 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
2845 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
Jim Grosbacha7c78222010-10-29 22:13:48 +00002846
Craig Topperf63ef912013-07-24 07:33:14 +00002847 OS << "static const MatchEntry MatchTable" << VC << "[] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002848
Craig Topperf63ef912013-07-24 07:33:14 +00002849 for (std::vector<MatchableInfo*>::const_iterator it =
2850 Info.Matchables.begin(), ie = Info.Matchables.end();
2851 it != ie; ++it) {
2852 MatchableInfo &II = **it;
2853 if (II.AsmVariantID != AsmVariantNo)
2854 continue;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002855
Craig Topperf63ef912013-07-24 07:33:14 +00002856 // Store a pascal-style length byte in the mnemonic.
2857 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
2858 OS << " { " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
2859 << " /* " << II.Mnemonic << " */, "
2860 << Target.getName() << "::"
2861 << II.getResultInst()->TheDef->getName() << ", "
2862 << II.ConversionFnKind << ", ";
2863
2864 // Write the required features mask.
2865 if (!II.RequiredFeatures.empty()) {
2866 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2867 if (i) OS << "|";
2868 OS << II.RequiredFeatures[i]->getEnumName();
2869 }
2870 } else
2871 OS << "0";
2872
2873 OS << ", { ";
2874 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
2875 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
2876
2877 if (i) OS << ", ";
2878 OS << Op.Class->Name;
Daniel Dunbar54074b52010-07-19 05:44:09 +00002879 }
Craig Topperf63ef912013-07-24 07:33:14 +00002880 OS << " }, },\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002881 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002882
Craig Topperf63ef912013-07-24 07:33:14 +00002883 OS << "};\n\n";
2884 }
Daniel Dunbara027d222009-07-31 02:32:59 +00002885
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002886 // A method to determine if a mnemonic is in the list.
2887 OS << "bool " << Target.getName() << ClassName << "::\n"
Craig Topperf63ef912013-07-24 07:33:14 +00002888 << "mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) {\n";
2889 OS << " // Find the appropriate table for this asm variant.\n";
2890 OS << " const MatchEntry *Start, *End;\n";
2891 OS << " switch (VariantID) {\n";
2892 OS << " default: // unreachable\n";
2893 for (unsigned VC = 0; VC != VariantCount; ++VC) {
2894 Record *AsmVariant = Target.getAsmParserVariant(VC);
2895 std::string CommentDelimiter =
2896 AsmVariant->getValueAsString("CommentDelimiter");
2897 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
2898 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
2899 OS << " case " << AsmVariantNo << ": Start = MatchTable" << VC
2900 << "; End = array_endof(MatchTable" << VC << "); break;\n";
2901 }
2902 OS << " }\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002903 OS << " // Search the table.\n";
2904 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
Craig Topperf63ef912013-07-24 07:33:14 +00002905 OS << " std::equal_range(Start, End, Mnemonic, LessOpcode());\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002906 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2907 OS << "}\n\n";
2908
Chris Lattner96352e52010-09-06 21:08:38 +00002909 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002910 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002911 << Target.getName() << ClassName << "::\n"
2912 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2913 << " &Operands,\n";
Chad Rosier6e006d32012-10-12 22:53:36 +00002914 OS << " MCInst &Inst,\n"
Chad Rosier22685872012-10-01 23:45:51 +00002915 << "unsigned &ErrorInfo, bool matchingInlineAsm, unsigned VariantID) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002916
Chad Rosier0bad0862012-08-30 21:43:05 +00002917 OS << " // Eliminate obvious mismatches.\n";
2918 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2919 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2920 OS << " return Match_InvalidOperand;\n";
2921 OS << " }\n\n";
2922
Daniel Dunbar54074b52010-07-19 05:44:09 +00002923 // Emit code to get the available features.
2924 OS << " // Get the current feature set.\n";
2925 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2926
Chris Lattner674c1dc2010-10-30 17:36:36 +00002927 OS << " // Get the instruction mnemonic, which is the first token.\n";
2928 OS << " StringRef Mnemonic = ((" << Target.getName()
2929 << "Operand*)Operands[0])->getToken();\n\n";
2930
Chris Lattner7fd44892010-10-30 18:48:18 +00002931 if (HasMnemonicAliases) {
2932 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Chad Rosier88eb89b2013-04-18 22:35:36 +00002933 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures, VariantID);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002934 }
Bob Wilson828295b2011-01-26 21:26:19 +00002935
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002936 // Emit code to compute the class list for this operand vector.
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002937 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002938 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002939 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002940 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00002941 OS << " unsigned MissingFeatures = ~0U;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002942 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002943 OS << " // wrong for all instances of the instruction.\n";
2944 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002945
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002946 // Emit code to search the table.
Craig Topperf63ef912013-07-24 07:33:14 +00002947 OS << " // Find the appropriate table for this asm variant.\n";
2948 OS << " const MatchEntry *Start, *End;\n";
2949 OS << " switch (VariantID) {\n";
2950 OS << " default: // unreachable\n";
2951 for (unsigned VC = 0; VC != VariantCount; ++VC) {
2952 Record *AsmVariant = Target.getAsmParserVariant(VC);
2953 std::string CommentDelimiter =
2954 AsmVariant->getValueAsString("CommentDelimiter");
2955 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
2956 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
2957 OS << " case " << AsmVariantNo << ": Start = MatchTable" << VC
2958 << "; End = array_endof(MatchTable" << VC << "); break;\n";
2959 }
2960 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002961 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002962 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
Craig Topperf63ef912013-07-24 07:33:14 +00002963 OS << " std::equal_range(Start, End, Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002964
Chris Lattnera008e8a2010-09-06 21:54:15 +00002965 OS << " // Return a more specific error code if no mnemonics match.\n";
2966 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2967 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002968
Chris Lattner2b1f9432010-09-06 21:22:45 +00002969 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002970 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002971 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002972
Gabor Greife53ee3b2010-09-07 06:06:06 +00002973 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002974 OS << " assert(Mnemonic == it->getMnemonic());\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002975
Daniel Dunbar54074b52010-07-19 05:44:09 +00002976 // Emit check that the subclasses match.
Chris Lattnerce4a3352010-09-06 22:11:18 +00002977 OS << " bool OperandsValid = true;\n";
2978 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002979 OS << " if (i + 1 >= Operands.size()) {\n";
2980 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Bill Wendling087642f2012-08-04 10:31:40 +00002981 OS << " if (!OperandsValid) ErrorInfo = i + 1;\n";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002982 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002983 OS << " }\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002984 OS << " unsigned Diag = validateOperandClass(Operands[i+1],\n";
2985 OS.indent(43);
2986 OS << "(MatchClassKind)it->Classes[i]);\n";
2987 OS << " if (Diag == Match_Success)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002988 OS << " continue;\n";
Jim Grosbachfa05def2013-02-06 06:00:06 +00002989 OS << " // If the generic handler indicates an invalid operand\n";
2990 OS << " // failure, check for a special case.\n";
2991 OS << " if (Diag == Match_InvalidOperand) {\n";
2992 OS << " Diag = validateTargetOperandClass(Operands[i+1],\n";
2993 OS.indent(43);
2994 OS << "(MatchClassKind)it->Classes[i]);\n";
2995 OS << " if (Diag == Match_Success)\n";
2996 OS << " continue;\n";
2997 OS << " }\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002998 OS << " // If this operand is broken for all of the instances of this\n";
2999 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00003000 OS << " // If we already had a match that only failed due to a\n";
3001 OS << " // target predicate, that diagnostic is preferred.\n";
3002 OS << " if (!HadMatchOtherThanPredicate &&\n";
3003 OS << " (it == MnemonicRange.first || ErrorInfo <= i+1)) {\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00003004 OS << " ErrorInfo = i+1;\n";
Jim Grosbachef970c12012-06-26 22:58:01 +00003005 OS << " // InvalidOperand is the default. Prefer specificity.\n";
3006 OS << " if (Diag != Match_InvalidOperand)\n";
3007 OS << " RetCode = Diag;\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00003008 OS << " }\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00003009 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
3010 OS << " OperandsValid = false;\n";
3011 OS << " break;\n";
3012 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00003013
Chris Lattnerce4a3352010-09-06 22:11:18 +00003014 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00003015
3016 // Emit check that the required features are available.
3017 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
3018 << "!= it->RequiredFeatures) {\n";
3019 OS << " HadMatchOtherThanFeatures = true;\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00003020 OS << " unsigned NewMissingFeatures = it->RequiredFeatures & "
3021 "~AvailableFeatures;\n";
Chad Rosier0bad0862012-08-30 21:43:05 +00003022 OS << " if (CountPopulation_32(NewMissingFeatures) <=\n"
3023 " CountPopulation_32(MissingFeatures))\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00003024 OS << " MissingFeatures = NewMissingFeatures;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00003025 OS << " continue;\n";
3026 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00003027 OS << "\n";
Chad Rosier22685872012-10-01 23:45:51 +00003028 OS << " if (matchingInlineAsm) {\n";
Chad Rosier22685872012-10-01 23:45:51 +00003029 OS << " Inst.setOpcode(it->Opcode);\n";
Chad Rosier6e006d32012-10-12 22:53:36 +00003030 OS << " convertToMapAndConstraints(it->ConvertFn, Operands);\n";
Chad Rosier22685872012-10-01 23:45:51 +00003031 OS << " return Match_Success;\n";
3032 OS << " }\n\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00003033 OS << " // We have selected a definite instruction, convert the parsed\n"
3034 << " // operands into the appropriate MCInst.\n";
Chad Rosier90e11f82012-09-05 01:02:38 +00003035 OS << " convertToMCInst(it->ConvertFn, Inst, it->Opcode, Operands);\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00003036 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00003037
Jim Grosbach19cb7f42011-08-15 23:03:29 +00003038 // Verify the instruction with the target-specific match predicate function.
3039 OS << " // We have a potential match. Check the target predicate to\n"
3040 << " // handle any context sensitive constraints.\n"
3041 << " unsigned MatchResult;\n"
3042 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
3043 << " Match_Success) {\n"
3044 << " Inst.clear();\n"
3045 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00003046 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00003047 << " continue;\n"
3048 << " }\n\n";
3049
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00003050 // Call the post-processing function, if used.
3051 std::string InsnCleanupFn =
3052 AsmParser->getValueAsString("AsmParserInstCleanup");
3053 if (!InsnCleanupFn.empty())
3054 OS << " " << InsnCleanupFn << "(Inst);\n";
3055
Joey Gouly715d98d2013-09-12 10:28:05 +00003056 if (HasDeprecation) {
3057 OS << " std::string Info;\n";
3058 OS << " if (MII.get(Inst.getOpcode()).getDeprecatedInfo(Inst, STI, Info)) {\n";
3059 OS << " SMLoc Loc = ((" << Target.getName() << "Operand*)Operands[0])->getStartLoc();\n";
3060 OS << " Parser.Warning(Loc, Info, None);\n";
3061 OS << " }\n";
3062 }
3063
Chris Lattner79ed3f72010-09-06 19:22:17 +00003064 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00003065 OS << " }\n\n";
3066
Chris Lattnerec6789f2010-09-06 20:08:02 +00003067 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Chad Rosier4c1d2ba2012-08-21 17:22:47 +00003068 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)\n";
3069 OS << " return RetCode;\n\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00003070 OS << " // Missing feature matches return which features were missing\n";
3071 OS << " ErrorInfo = MissingFeatures;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00003072 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00003073 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00003074
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00003075 if (Info.OperandMatchInfo.size())
Craig Topper3a364442012-09-18 07:02:21 +00003076 emitCustomOperandParsing(OS, Target, Info, ClassName, StringTable,
3077 MaxMnemonicIndex);
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00003078
Chris Lattner0692ee62010-09-06 19:11:01 +00003079 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00003080}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00003081
3082namespace llvm {
3083
3084void EmitAsmMatcher(RecordKeeper &RK, raw_ostream &OS) {
3085 emitSourceFileHeader("Assembly Matcher Source Fragment", OS);
3086 AsmMatcherEmitter(RK).run(OS);
3087}
3088
3089} // End llvm namespace