blob: 0a8ae466efb705abe93e644d10bf3283833e58cc [file] [log] [blame]
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00001//===- AsmMatcherEmitter.cpp - Generate an assembly matcher ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a target specifier matcher for converting parsed
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000011// assembly operands in the MCInst structures. It also emits a matcher for
12// custom operand parsing.
13//
14// Converting assembly operands into MCInst structures
15// ---------------------------------------------------
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000016//
Daniel Dunbar20927f22009-08-07 08:26:05 +000017// The input to the target specific matcher is a list of literal tokens and
18// operands. The target specific parser should generally eliminate any syntax
19// which is not relevant for matching; for example, comma tokens should have
20// already been consumed and eliminated by the parser. Most instructions will
21// end up with a single literal token (the instruction name) and some number of
22// operands.
23//
24// Some example inputs, for X86:
25// 'addl' (immediate ...) (register ...)
26// 'add' (immediate ...) (memory ...)
Jim Grosbacha7c78222010-10-29 22:13:48 +000027// 'call' '*' %epc
Daniel Dunbar20927f22009-08-07 08:26:05 +000028//
29// The assembly matcher is responsible for converting this input into a precise
30// machine instruction (i.e., an instruction with a well defined encoding). This
31// mapping has several properties which complicate matching:
32//
33// - It may be ambiguous; many architectures can legally encode particular
34// variants of an instruction in different ways (for example, using a smaller
35// encoding for small immediates). Such ambiguities should never be
36// arbitrarily resolved by the assembler, the assembler is always responsible
37// for choosing the "best" available instruction.
38//
39// - It may depend on the subtarget or the assembler context. Instructions
40// which are invalid for the current mode, but otherwise unambiguous (e.g.,
41// an SSE instruction in a file being assembled for i486) should be accepted
42// and rejected by the assembler front end. However, if the proper encoding
43// for an instruction is dependent on the assembler context then the matcher
44// is responsible for selecting the correct machine instruction for the
45// current mode.
46//
47// The core matching algorithm attempts to exploit the regularity in most
48// instruction sets to quickly determine the set of possibly matching
49// instructions, and the simplify the generated code. Additionally, this helps
50// to ensure that the ambiguities are intentionally resolved by the user.
51//
52// The matching is divided into two distinct phases:
53//
54// 1. Classification: Each operand is mapped to the unique set which (a)
55// contains it, and (b) is the largest such subset for which a single
56// instruction could match all members.
57//
58// For register classes, we can generate these subgroups automatically. For
59// arbitrary operands, we expect the user to define the classes and their
60// relations to one another (for example, 8-bit signed immediates as a
61// subset of 32-bit immediates).
62//
63// By partitioning the operands in this way, we guarantee that for any
64// tuple of classes, any single instruction must match either all or none
65// of the sets of operands which could classify to that tuple.
66//
67// In addition, the subset relation amongst classes induces a partial order
68// on such tuples, which we use to resolve ambiguities.
69//
Daniel Dunbar20927f22009-08-07 08:26:05 +000070// 2. The input can now be treated as a tuple of classes (static tokens are
71// simple singleton sets). Each such tuple should generally map to a single
72// instruction (we currently ignore cases where this isn't true, whee!!!),
73// which we can emit a simple matcher for.
74//
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000075// Custom Operand Parsing
76// ----------------------
77//
78// Some targets need a custom way to parse operands, some specific instructions
79// can contain arguments that can represent processor flags and other kinds of
80// identifiers that need to be mapped to specific valeus in the final encoded
81// instructions. The target specific custom operand parsing works in the
82// following way:
83//
84// 1. A operand match table is built, each entry contains a mnemonic, an
85// operand class, a mask for all operand positions for that same
86// class/mnemonic and target features to be checked while trying to match.
87//
88// 2. The operand matcher will try every possible entry with the same
89// mnemonic and will check if the target feature for this mnemonic also
90// matches. After that, if the operand to be matched has its index
Chris Lattner7a2bdde2011-04-15 05:18:47 +000091// present in the mask, a successful match occurs. Otherwise, fallback
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +000092// to the regular operand parsing.
93//
94// 3. For a match success, each operand class that has a 'ParserMethod'
95// becomes part of a switch from where the custom method is called.
96//
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000097//===----------------------------------------------------------------------===//
98
Daniel Dunbard51ffcf2009-07-11 19:39:44 +000099#include "CodeGenTarget.h"
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +0000100#include "StringToOffsetTable.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000101#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc07bd402010-11-04 02:11:18 +0000102#include "llvm/ADT/PointerUnion.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 Dunbar606e8ad2009-08-09 04:00:06 +0000105#include "llvm/ADT/STLExtras.h"
Daniel Dunbar20927f22009-08-07 08:26:05 +0000106#include "llvm/ADT/StringExtras.h"
107#include "llvm/Support/CommandLine.h"
Daniel Dunbara027d222009-07-31 02:32:59 +0000108#include "llvm/Support/Debug.h"
Craig Topper655b8de2012-02-05 07:21:30 +0000109#include "llvm/Support/ErrorHandling.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +0000110#include "llvm/TableGen/Error.h"
111#include "llvm/TableGen/Record.h"
Douglas Gregorf657da22012-05-02 17:32:48 +0000112#include "llvm/TableGen/StringMatcher.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>
Daniel Dunbard51ffcf2009-07-11 19:39:44 +0000117using namespace llvm;
118
Daniel Dunbar27249152009-08-07 20:33:39 +0000119static cl::opt<std::string>
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000120MatchPrefix("match-prefix", cl::init(""),
121 cl::desc("Only match instructions with the given prefix"));
Daniel Dunbar20927f22009-08-07 08:26:05 +0000122
Daniel Dunbar20927f22009-08-07 08:26:05 +0000123namespace {
Bob Wilson828295b2011-01-26 21:26:19 +0000124class AsmMatcherInfo;
Daniel Dunbar54074b52010-07-19 05:44:09 +0000125struct SubtargetFeatureInfo;
126
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000127class AsmMatcherEmitter {
128 RecordKeeper &Records;
129public:
130 AsmMatcherEmitter(RecordKeeper &R) : Records(R) {}
131
132 void run(raw_ostream &o);
133};
134
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000135/// ClassInfo - Helper class for storing the information about a particular
136/// class of operands which can be matched.
137struct ClassInfo {
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000138 enum ClassInfoKind {
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000139 /// Invalid kind, for use as a sentinel value.
140 Invalid = 0,
141
142 /// The class for a particular token.
143 Token,
144
145 /// The (first) register class, subsequent register classes are
146 /// RegisterClass0+1, and so on.
147 RegisterClass0,
148
149 /// The (first) user defined class, subsequent user defined classes are
150 /// UserClass0+1, and so on.
151 UserClass0 = 1<<16
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000152 };
153
154 /// Kind - The class kind, which is either a predefined kind, or (UserClass0 +
155 /// N) for the Nth user defined class.
156 unsigned Kind;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000157
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000158 /// SuperClasses - The super classes of this class. Note that for simplicities
159 /// sake user operands only record their immediate super class, while register
160 /// operands include all superclasses.
161 std::vector<ClassInfo*> SuperClasses;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000162
Daniel Dunbar6745d422009-08-09 05:18:30 +0000163 /// Name - The full class name, suitable for use in an enum.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000164 std::string Name;
165
Daniel Dunbar6745d422009-08-09 05:18:30 +0000166 /// ClassName - The unadorned generic name for this class (e.g., Token).
167 std::string ClassName;
168
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000169 /// ValueName - The name of the value this class represents; for a token this
170 /// is the literal token string, for an operand it is the TableGen class (or
171 /// empty if this is a derived class).
172 std::string ValueName;
173
174 /// PredicateMethod - The name of the operand method to test whether the
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000175 /// operand matches this class; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000176 std::string PredicateMethod;
177
178 /// RenderMethod - The name of the operand method to add this operand to an
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000179 /// MCInst; this is not valid for Token or register kinds.
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000180 std::string RenderMethod;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000181
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000182 /// ParserMethod - The name of the operand method to do a target specific
183 /// parsing on the operand.
184 std::string ParserMethod;
185
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000186 /// For register classes, the records for all the registers in this class.
187 std::set<Record*> Registers;
188
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +0000189 /// For custom match classes, he diagnostic kind for when the predicate fails.
190 std::string DiagnosticType;
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000191public:
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000192 /// isRegisterClass() - Check if this is a register class.
193 bool isRegisterClass() const {
194 return Kind >= RegisterClass0 && Kind < UserClass0;
195 }
196
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000197 /// isUserClass() - Check if this is a user defined class.
198 bool isUserClass() const {
199 return Kind >= UserClass0;
200 }
201
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000202 /// isRelatedTo - Check whether this class is "related" to \arg RHS. Classes
203 /// are related if they are in the same class hierarchy.
204 bool isRelatedTo(const ClassInfo &RHS) const {
205 // Tokens are only related to tokens.
206 if (Kind == Token || RHS.Kind == Token)
207 return Kind == Token && RHS.Kind == Token;
208
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000209 // Registers classes are only related to registers classes, and only if
210 // their intersection is non-empty.
211 if (isRegisterClass() || RHS.isRegisterClass()) {
212 if (!isRegisterClass() || !RHS.isRegisterClass())
213 return false;
214
215 std::set<Record*> Tmp;
216 std::insert_iterator< std::set<Record*> > II(Tmp, Tmp.begin());
Jim Grosbacha7c78222010-10-29 22:13:48 +0000217 std::set_intersection(Registers.begin(), Registers.end(),
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000218 RHS.Registers.begin(), RHS.Registers.end(),
219 II);
220
221 return !Tmp.empty();
222 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000223
224 // Otherwise we have two users operands; they are related if they are in the
225 // same class hierarchy.
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000226 //
227 // FIXME: This is an oversimplification, they should only be related if they
228 // intersect, however we don't have that information.
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000229 assert(isUserClass() && RHS.isUserClass() && "Unexpected class!");
230 const ClassInfo *Root = this;
231 while (!Root->SuperClasses.empty())
232 Root = Root->SuperClasses.front();
233
Daniel Dunbar8409bfb2009-08-11 20:10:07 +0000234 const ClassInfo *RHSRoot = &RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000235 while (!RHSRoot->SuperClasses.empty())
236 RHSRoot = RHSRoot->SuperClasses.front();
Jim Grosbacha7c78222010-10-29 22:13:48 +0000237
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000238 return Root == RHSRoot;
239 }
240
Jim Grosbacha7c78222010-10-29 22:13:48 +0000241 /// isSubsetOf - Test whether this class is a subset of \arg RHS;
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000242 bool isSubsetOf(const ClassInfo &RHS) const {
243 // This is a subset of RHS if it is the same class...
244 if (this == &RHS)
245 return true;
246
247 // ... or if any of its super classes are a subset of RHS.
248 for (std::vector<ClassInfo*>::const_iterator it = SuperClasses.begin(),
249 ie = SuperClasses.end(); it != ie; ++it)
250 if ((*it)->isSubsetOf(RHS))
251 return true;
252
253 return false;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000254 }
255
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000256 /// operator< - Compare two classes.
257 bool operator<(const ClassInfo &RHS) const {
Daniel Dunbar368a4562010-05-27 05:31:32 +0000258 if (this == &RHS)
259 return false;
260
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000261 // Unrelated classes can be ordered by kind.
262 if (!isRelatedTo(RHS))
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000263 return Kind < RHS.Kind;
264
265 switch (Kind) {
Daniel Dunbar6745d422009-08-09 05:18:30 +0000266 case Invalid:
Craig Topper655b8de2012-02-05 07:21:30 +0000267 llvm_unreachable("Invalid kind!");
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000268
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000269 default:
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000270 // This class precedes the RHS if it is a proper subset of the RHS.
Daniel Dunbar368a4562010-05-27 05:31:32 +0000271 if (isSubsetOf(RHS))
Duncan Sands34727662010-07-12 08:16:59 +0000272 return true;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000273 if (RHS.isSubsetOf(*this))
Duncan Sands34727662010-07-12 08:16:59 +0000274 return false;
Daniel Dunbar368a4562010-05-27 05:31:32 +0000275
276 // Otherwise, order by name to ensure we have a total ordering.
277 return ValueName < RHS.ValueName;
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000278 }
279 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000280};
281
Chris Lattner22bc5c42010-11-01 05:06:45 +0000282/// MatchableInfo - Helper class for storing the necessary information for an
283/// instruction or alias which is capable of being matched.
284struct MatchableInfo {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000285 struct AsmOperand {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000286 /// Token - This is the token that the operand came from.
287 StringRef Token;
Bob Wilson828295b2011-01-26 21:26:19 +0000288
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000289 /// The unique class instance this operand should match.
290 ClassInfo *Class;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000291
Chris Lattner567820c2010-11-04 01:42:59 +0000292 /// The operand name this is, if anything.
293 StringRef SrcOpName;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000294
295 /// The suboperand index within SrcOpName, or -1 for the entire operand.
296 int SubOpIdx;
Bob Wilson828295b2011-01-26 21:26:19 +0000297
Devang Patel63faf822012-01-07 01:33:34 +0000298 /// Register record if this token is singleton register.
299 Record *SingletonReg;
300
Jim Grosbachf35307c2012-01-24 21:06:59 +0000301 explicit AsmOperand(StringRef T) : Token(T), Class(0), SubOpIdx(-1),
Jim Grosbach11fc6462012-04-11 21:02:33 +0000302 SingletonReg(0) {}
Daniel Dunbar20927f22009-08-07 08:26:05 +0000303 };
Bob Wilson828295b2011-01-26 21:26:19 +0000304
Chris Lattner1d13bda2010-11-04 00:43:46 +0000305 /// ResOperand - This represents a single operand in the result instruction
306 /// generated by the match. In cases (like addressing modes) where a single
307 /// assembler operand expands to multiple MCOperands, this represents the
308 /// single assembler operand, not the MCOperand.
309 struct ResOperand {
310 enum {
311 /// RenderAsmOperand - This represents an operand result that is
312 /// generated by calling the render method on the assembly operand. The
313 /// corresponding AsmOperand is specified by AsmOperandNum.
314 RenderAsmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000315
Chris Lattner1d13bda2010-11-04 00:43:46 +0000316 /// TiedOperand - This represents a result operand that is a duplicate of
317 /// a previous result operand.
Chris Lattner98c870f2010-11-06 19:25:43 +0000318 TiedOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000319
Chris Lattner98c870f2010-11-06 19:25:43 +0000320 /// ImmOperand - This represents an immediate value that is dumped into
321 /// the operand.
Chris Lattner90fd7972010-11-06 19:57:21 +0000322 ImmOperand,
Bob Wilson828295b2011-01-26 21:26:19 +0000323
Chris Lattner90fd7972010-11-06 19:57:21 +0000324 /// RegOperand - This represents a fixed register that is dumped in.
325 RegOperand
Chris Lattner1d13bda2010-11-04 00:43:46 +0000326 } Kind;
Bob Wilson828295b2011-01-26 21:26:19 +0000327
Chris Lattner1d13bda2010-11-04 00:43:46 +0000328 union {
329 /// This is the operand # in the AsmOperands list that this should be
330 /// copied from.
331 unsigned AsmOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000332
Chris Lattner1d13bda2010-11-04 00:43:46 +0000333 /// TiedOperandNum - This is the (earlier) result operand that should be
334 /// copied from.
335 unsigned TiedOperandNum;
Bob Wilson828295b2011-01-26 21:26:19 +0000336
Chris Lattner98c870f2010-11-06 19:25:43 +0000337 /// ImmVal - This is the immediate value added to the instruction.
338 int64_t ImmVal;
Bob Wilson828295b2011-01-26 21:26:19 +0000339
Chris Lattner90fd7972010-11-06 19:57:21 +0000340 /// Register - This is the register record.
341 Record *Register;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000342 };
Bob Wilson828295b2011-01-26 21:26:19 +0000343
Bob Wilsona49c7df2011-01-26 19:44:55 +0000344 /// MINumOperands - The number of MCInst operands populated by this
345 /// operand.
346 unsigned MINumOperands;
Bob Wilson828295b2011-01-26 21:26:19 +0000347
Bob Wilsona49c7df2011-01-26 19:44:55 +0000348 static ResOperand getRenderedOp(unsigned AsmOpNum, unsigned NumOperands) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000349 ResOperand X;
350 X.Kind = RenderAsmOperand;
351 X.AsmOperandNum = AsmOpNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000352 X.MINumOperands = NumOperands;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000353 return X;
354 }
Bob Wilson828295b2011-01-26 21:26:19 +0000355
Bob Wilsona49c7df2011-01-26 19:44:55 +0000356 static ResOperand getTiedOp(unsigned TiedOperandNum) {
Chris Lattner1d13bda2010-11-04 00:43:46 +0000357 ResOperand X;
358 X.Kind = TiedOperand;
359 X.TiedOperandNum = TiedOperandNum;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000360 X.MINumOperands = 1;
Chris Lattner1d13bda2010-11-04 00:43:46 +0000361 return X;
362 }
Bob Wilson828295b2011-01-26 21:26:19 +0000363
Bob Wilsona49c7df2011-01-26 19:44:55 +0000364 static ResOperand getImmOp(int64_t Val) {
Chris Lattner98c870f2010-11-06 19:25:43 +0000365 ResOperand X;
366 X.Kind = ImmOperand;
367 X.ImmVal = Val;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000368 X.MINumOperands = 1;
Chris Lattner98c870f2010-11-06 19:25:43 +0000369 return X;
370 }
Bob Wilson828295b2011-01-26 21:26:19 +0000371
Bob Wilsona49c7df2011-01-26 19:44:55 +0000372 static ResOperand getRegOp(Record *Reg) {
Chris Lattner90fd7972010-11-06 19:57:21 +0000373 ResOperand X;
374 X.Kind = RegOperand;
375 X.Register = Reg;
Bob Wilsona49c7df2011-01-26 19:44:55 +0000376 X.MINumOperands = 1;
Chris Lattner90fd7972010-11-06 19:57:21 +0000377 return X;
378 }
Chris Lattner1d13bda2010-11-04 00:43:46 +0000379 };
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380
Devang Patel56315d32012-01-10 17:50:43 +0000381 /// AsmVariantID - Target's assembly syntax variant no.
382 int AsmVariantID;
383
Chris Lattner3b5aec62010-11-02 17:34:28 +0000384 /// TheDef - This is the definition of the instruction or InstAlias that this
385 /// matchable came from.
Chris Lattner5bc93872010-11-01 04:34:44 +0000386 Record *const TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000387
Chris Lattnerc07bd402010-11-04 02:11:18 +0000388 /// DefRec - This is the definition that it came from.
389 PointerUnion<const CodeGenInstruction*, const CodeGenInstAlias*> DefRec;
Bob Wilson828295b2011-01-26 21:26:19 +0000390
Chris Lattner662e5a32010-11-06 07:14:44 +0000391 const CodeGenInstruction *getResultInst() const {
392 if (DefRec.is<const CodeGenInstruction*>())
393 return DefRec.get<const CodeGenInstruction*>();
394 return DefRec.get<const CodeGenInstAlias*>()->ResultInst;
395 }
Bob Wilson828295b2011-01-26 21:26:19 +0000396
Chris Lattner1d13bda2010-11-04 00:43:46 +0000397 /// ResOperands - This is the operand list that should be built for the result
398 /// MCInst.
Jim Grosbachb423d182012-04-19 17:52:34 +0000399 SmallVector<ResOperand, 8> ResOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000400
401 /// AsmString - The assembly string for this instruction (with variants
Chris Lattner3b5aec62010-11-02 17:34:28 +0000402 /// removed), e.g. "movsx $src, $dst".
Daniel Dunbar20927f22009-08-07 08:26:05 +0000403 std::string AsmString;
404
Chris Lattnerd19ec052010-11-02 17:30:52 +0000405 /// Mnemonic - This is the first token of the matched instruction, its
406 /// mnemonic.
407 StringRef Mnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +0000408
Chris Lattner3116fef2010-11-02 01:03:43 +0000409 /// AsmOperands - The textual operands that this instruction matches,
Chris Lattner3b5aec62010-11-02 17:34:28 +0000410 /// annotated with a class and where in the OperandList they were defined.
411 /// This directly corresponds to the tokenized AsmString after the mnemonic is
412 /// removed.
Jim Grosbachb423d182012-04-19 17:52:34 +0000413 SmallVector<AsmOperand, 8> AsmOperands;
Daniel Dunbar20927f22009-08-07 08:26:05 +0000414
Daniel Dunbar54074b52010-07-19 05:44:09 +0000415 /// Predicates - The required subtarget features to match this instruction.
416 SmallVector<SubtargetFeatureInfo*, 4> RequiredFeatures;
417
Daniel Dunbarb7479c02009-08-08 05:24:34 +0000418 /// ConversionFnKind - The enum value which is passed to the generated
419 /// ConvertToMCInst to convert parsed operands into an MCInst for this
420 /// function.
421 std::string ConversionFnKind;
Bob Wilson828295b2011-01-26 21:26:19 +0000422
Chris Lattner22bc5c42010-11-01 05:06:45 +0000423 MatchableInfo(const CodeGenInstruction &CGI)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000424 : AsmVariantID(0), TheDef(CGI.TheDef), DefRec(&CGI),
Devang Patel56315d32012-01-10 17:50:43 +0000425 AsmString(CGI.AsmString) {
Chris Lattner5bc93872010-11-01 04:34:44 +0000426 }
427
Chris Lattner22bc5c42010-11-01 05:06:45 +0000428 MatchableInfo(const CodeGenInstAlias *Alias)
Jim Grosbachf35307c2012-01-24 21:06:59 +0000429 : AsmVariantID(0), TheDef(Alias->TheDef), DefRec(Alias),
Devang Patel56315d32012-01-10 17:50:43 +0000430 AsmString(Alias->AsmString) {
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000431 }
Bob Wilson828295b2011-01-26 21:26:19 +0000432
Jim Grosbachc1922c72012-04-19 23:59:23 +0000433 // Two-operand aliases clone from the main matchable, but mark the second
434 // operand as a tied operand of the first for purposes of the assembler.
435 void formTwoOperandAlias(StringRef Constraint);
436
Jim Grosbach8caecde2012-04-19 17:52:32 +0000437 void initialize(const AsmMatcherInfo &Info,
Jim Grosbachf35307c2012-01-24 21:06:59 +0000438 SmallPtrSet<Record*, 16> &SingletonRegisters,
Jim Grosbach11fc6462012-04-11 21:02:33 +0000439 int AsmVariantNo, std::string &RegisterPrefix);
Bob Wilson828295b2011-01-26 21:26:19 +0000440
Jim Grosbach8caecde2012-04-19 17:52:32 +0000441 /// validate - Return true if this matchable is a valid thing to match against
Chris Lattner22bc5c42010-11-01 05:06:45 +0000442 /// and perform a bunch of validity checking.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000443 bool validate(StringRef CommentDelimiter, bool Hack) const;
Bob Wilson828295b2011-01-26 21:26:19 +0000444
Jim Grosbachf35307c2012-01-24 21:06:59 +0000445 /// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Patel63faf822012-01-07 01:33:34 +0000446 /// if present, from specified token.
447 void
448 extractSingletonRegisterForAsmOperand(unsigned i, const AsmMatcherInfo &Info,
449 std::string &RegisterPrefix);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000450
Jim Grosbach8caecde2012-04-19 17:52:32 +0000451 /// findAsmOperand - Find the AsmOperand with the specified name and
Bob Wilsona49c7df2011-01-26 19:44:55 +0000452 /// suboperand index.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000453 int findAsmOperand(StringRef N, int SubOpIdx) const {
Bob Wilsona49c7df2011-01-26 19:44:55 +0000454 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
455 if (N == AsmOperands[i].SrcOpName &&
456 SubOpIdx == AsmOperands[i].SubOpIdx)
457 return i;
458 return -1;
459 }
Bob Wilson828295b2011-01-26 21:26:19 +0000460
Jim Grosbach8caecde2012-04-19 17:52:32 +0000461 /// findAsmOperandNamed - Find the first AsmOperand with the specified name.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000462 /// This does not check the suboperand index.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000463 int findAsmOperandNamed(StringRef N) const {
Chris Lattnerba3b5b62010-11-04 01:55:23 +0000464 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
465 if (N == AsmOperands[i].SrcOpName)
466 return i;
467 return -1;
468 }
Bob Wilson828295b2011-01-26 21:26:19 +0000469
Jim Grosbach8caecde2012-04-19 17:52:32 +0000470 void buildInstructionResultOperands();
471 void buildAliasResultOperands();
Chris Lattner1d13bda2010-11-04 00:43:46 +0000472
Chris Lattner22bc5c42010-11-01 05:06:45 +0000473 /// operator< - Compare two matchables.
474 bool operator<(const MatchableInfo &RHS) const {
Chris Lattnere206fcf2010-09-06 21:01:37 +0000475 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000476 if (Mnemonic != RHS.Mnemonic)
477 return Mnemonic < RHS.Mnemonic;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000478
Chris Lattner3116fef2010-11-02 01:03:43 +0000479 if (AsmOperands.size() != RHS.AsmOperands.size())
480 return AsmOperands.size() < RHS.AsmOperands.size();
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000481
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000482 // Compare lexicographically by operand. The matcher validates that other
Jim Grosbach8caecde2012-04-19 17:52:32 +0000483 // orderings wouldn't be ambiguous using \see couldMatchAmbiguouslyWith().
Chris Lattner3116fef2010-11-02 01:03:43 +0000484 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
485 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000486 return true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000487 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbardb2ddb52009-08-09 08:23:23 +0000488 return false;
489 }
490
Daniel Dunbar606e8ad2009-08-09 04:00:06 +0000491 return false;
492 }
493
Jim Grosbach8caecde2012-04-19 17:52:32 +0000494 /// couldMatchAmbiguouslyWith - Check whether this matchable could
Daniel Dunbar2b544812009-08-09 06:05:33 +0000495 /// ambiguously match the same set of operands as \arg RHS (without being a
496 /// strictly superior match).
Jim Grosbach8caecde2012-04-19 17:52:32 +0000497 bool couldMatchAmbiguouslyWith(const MatchableInfo &RHS) {
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000498 // The primary comparator is the instruction mnemonic.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000499 if (Mnemonic != RHS.Mnemonic)
Chris Lattnere66b7eb2010-11-01 23:57:23 +0000500 return false;
Bob Wilson828295b2011-01-26 21:26:19 +0000501
Daniel Dunbar2b544812009-08-09 06:05:33 +0000502 // The number of operands is unambiguous.
Chris Lattner3116fef2010-11-02 01:03:43 +0000503 if (AsmOperands.size() != RHS.AsmOperands.size())
Daniel Dunbar2b544812009-08-09 06:05:33 +0000504 return false;
505
Daniel Dunbar1402f0b2010-01-23 00:26:16 +0000506 // Otherwise, make sure the ordering of the two instructions is unambiguous
507 // by checking that either (a) a token or operand kind discriminates them,
508 // or (b) the ordering among equivalent kinds is consistent.
509
Daniel Dunbar2b544812009-08-09 06:05:33 +0000510 // Tokens and operand kinds are unambiguous (assuming a correct target
511 // specific parser).
Chris Lattner3116fef2010-11-02 01:03:43 +0000512 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i)
513 if (AsmOperands[i].Class->Kind != RHS.AsmOperands[i].Class->Kind ||
514 AsmOperands[i].Class->Kind == ClassInfo::Token)
515 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class ||
516 *RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000517 return false;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000518
Daniel Dunbar2b544812009-08-09 06:05:33 +0000519 // Otherwise, this operand could commute if all operands are equivalent, or
520 // there is a pair of operands that compare less than and a pair that
521 // compare greater than.
522 bool HasLT = false, HasGT = false;
Chris Lattner3116fef2010-11-02 01:03:43 +0000523 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
524 if (*AsmOperands[i].Class < *RHS.AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000525 HasLT = true;
Chris Lattner3116fef2010-11-02 01:03:43 +0000526 if (*RHS.AsmOperands[i].Class < *AsmOperands[i].Class)
Daniel Dunbar2b544812009-08-09 06:05:33 +0000527 HasGT = true;
528 }
529
530 return !(HasLT ^ HasGT);
531 }
532
Daniel Dunbar20927f22009-08-07 08:26:05 +0000533 void dump();
Bob Wilson828295b2011-01-26 21:26:19 +0000534
Chris Lattnerd19ec052010-11-02 17:30:52 +0000535private:
Jim Grosbach8caecde2012-04-19 17:52:32 +0000536 void tokenizeAsmString(const AsmMatcherInfo &Info);
Daniel Dunbar20927f22009-08-07 08:26:05 +0000537};
538
Daniel Dunbar54074b52010-07-19 05:44:09 +0000539/// SubtargetFeatureInfo - Helper class for storing information on a subtarget
540/// feature which participates in instruction matching.
541struct SubtargetFeatureInfo {
542 /// \brief The predicate record for this feature.
543 Record *TheDef;
544
545 /// \brief An unique index assigned to represent this feature.
546 unsigned Index;
547
Chris Lattner0aed1e72010-10-30 20:07:57 +0000548 SubtargetFeatureInfo(Record *D, unsigned Idx) : TheDef(D), Index(Idx) {}
Bob Wilson828295b2011-01-26 21:26:19 +0000549
Daniel Dunbar54074b52010-07-19 05:44:09 +0000550 /// \brief The name of the enumerated constant identifying this feature.
Chris Lattner0aed1e72010-10-30 20:07:57 +0000551 std::string getEnumName() const {
552 return "Feature_" + TheDef->getName();
553 }
Daniel Dunbar54074b52010-07-19 05:44:09 +0000554};
555
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000556struct OperandMatchEntry {
557 unsigned OperandMask;
558 MatchableInfo* MI;
559 ClassInfo *CI;
560
Jim Grosbach8caecde2012-04-19 17:52:32 +0000561 static OperandMatchEntry create(MatchableInfo* mi, ClassInfo *ci,
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000562 unsigned opMask) {
563 OperandMatchEntry X;
564 X.OperandMask = opMask;
565 X.CI = ci;
566 X.MI = mi;
567 return X;
568 }
569};
570
571
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000572class AsmMatcherInfo {
573public:
Chris Lattner67db8832010-12-13 00:23:57 +0000574 /// Tracked Records
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000575 RecordKeeper &Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000576
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000577 /// The tablegen AsmParser record.
578 Record *AsmParser;
579
Chris Lattner02bcbc92010-11-01 01:37:30 +0000580 /// Target - The target information.
581 CodeGenTarget &Target;
582
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000583 /// The classes which are needed for matching.
584 std::vector<ClassInfo*> Classes;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000585
Chris Lattner22bc5c42010-11-01 05:06:45 +0000586 /// The information on the matchables to match.
587 std::vector<MatchableInfo*> Matchables;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000588
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000589 /// Info for custom matching operands by user defined methods.
590 std::vector<OperandMatchEntry> OperandMatchInfo;
591
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000592 /// Map of Register records to their class information.
593 std::map<Record*, ClassInfo*> RegisterClasses;
594
Daniel Dunbar54074b52010-07-19 05:44:09 +0000595 /// Map of Predicate records to their subtarget information.
596 std::map<Record*, SubtargetFeatureInfo*> SubtargetFeatures;
Bob Wilson828295b2011-01-26 21:26:19 +0000597
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +0000598 /// Map of AsmOperandClass records to their class information.
599 std::map<Record*, ClassInfo*> AsmOperandClasses;
600
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000601private:
602 /// Map of token to class information which has already been constructed.
603 std::map<std::string, ClassInfo*> TokenClasses;
604
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000605 /// Map of RegisterClass records to their class information.
606 std::map<Record*, ClassInfo*> RegisterClassClasses;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000607
608private:
609 /// getTokenClass - Lookup or create the class for the given token.
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000610 ClassInfo *getTokenClass(StringRef Token);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000611
612 /// getOperandClass - Lookup or create the class for the given operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +0000613 ClassInfo *getOperandClass(const CGIOperandList::OperandInfo &OI,
Jim Grosbach48c1f842011-10-28 22:32:53 +0000614 int SubOpIdx);
615 ClassInfo *getOperandClass(Record *Rec, int SubOpIdx);
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000616
Jim Grosbach8caecde2012-04-19 17:52:32 +0000617 /// buildRegisterClasses - Build the ClassInfo* instances for register
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000618 /// classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000619 void buildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters);
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000620
Jim Grosbach8caecde2012-04-19 17:52:32 +0000621 /// buildOperandClasses - Build the ClassInfo* instances for user defined
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000622 /// operand classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000623 void buildOperandClasses();
Daniel Dunbarea6408f2009-08-11 02:59:53 +0000624
Jim Grosbach8caecde2012-04-19 17:52:32 +0000625 void buildInstructionOperandReference(MatchableInfo *II, StringRef OpName,
Bob Wilsona49c7df2011-01-26 19:44:55 +0000626 unsigned AsmOpIdx);
Jim Grosbach8caecde2012-04-19 17:52:32 +0000627 void buildAliasOperandReference(MatchableInfo *II, StringRef OpName,
Chris Lattnerc07bd402010-11-04 02:11:18 +0000628 MatchableInfo::AsmOperand &Op);
Bob Wilson828295b2011-01-26 21:26:19 +0000629
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000630public:
Bob Wilson828295b2011-01-26 21:26:19 +0000631 AsmMatcherInfo(Record *AsmParser,
632 CodeGenTarget &Target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000633 RecordKeeper &Records);
Daniel Dunbar59fc42d2009-08-11 20:59:47 +0000634
Jim Grosbach8caecde2012-04-19 17:52:32 +0000635 /// buildInfo - Construct the various tables used during matching.
636 void buildInfo();
Bob Wilson828295b2011-01-26 21:26:19 +0000637
Jim Grosbach8caecde2012-04-19 17:52:32 +0000638 /// buildOperandMatchInfo - Build the necessary information to handle user
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000639 /// defined operand parsing methods.
Jim Grosbach8caecde2012-04-19 17:52:32 +0000640 void buildOperandMatchInfo();
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000641
Chris Lattner6fa152c2010-10-30 20:15:02 +0000642 /// getSubtargetFeature - Lookup or create the subtarget feature info for the
643 /// given operand.
644 SubtargetFeatureInfo *getSubtargetFeature(Record *Def) const {
645 assert(Def->isSubClassOf("Predicate") && "Invalid predicate type!");
646 std::map<Record*, SubtargetFeatureInfo*>::const_iterator I =
647 SubtargetFeatures.find(Def);
648 return I == SubtargetFeatures.end() ? 0 : I->second;
649 }
Chris Lattner67db8832010-12-13 00:23:57 +0000650
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000651 RecordKeeper &getRecords() const {
652 return Records;
Chris Lattner67db8832010-12-13 00:23:57 +0000653 }
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000654};
655
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000656} // End anonymous namespace
Daniel Dunbar20927f22009-08-07 08:26:05 +0000657
Chris Lattner22bc5c42010-11-01 05:06:45 +0000658void MatchableInfo::dump() {
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000659 errs() << TheDef->getName() << " -- " << "flattened:\"" << AsmString <<"\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000660
Chris Lattner3116fef2010-11-02 01:03:43 +0000661 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000662 AsmOperand &Op = AsmOperands[i];
Daniel Dunbar6745d422009-08-09 05:18:30 +0000663 errs() << " op[" << i << "] = " << Op.Class->ClassName << " - ";
Chris Lattner0bb780c2010-11-04 00:57:06 +0000664 errs() << '\"' << Op.Token << "\"\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +0000665 }
666}
667
Jim Grosbachc1922c72012-04-19 23:59:23 +0000668static std::pair<StringRef, StringRef>
669parseTwoOperandConstraint(StringRef S, SMLoc Loc) {
670 // Split via the '='.
671 std::pair<StringRef, StringRef> Ops = S.split('=');
672 if (Ops.second == "")
673 throw TGError(Loc, "missing '=' in two-operand alias constraint");
674 // Trim whitespace and the leading '$' on the operand names.
675 size_t start = Ops.first.find_first_of('$');
676 if (start == std::string::npos)
677 throw TGError(Loc, "expected '$' prefix on asm operand name");
678 Ops.first = Ops.first.slice(start + 1, std::string::npos);
679 size_t end = Ops.first.find_last_of(" \t");
680 Ops.first = Ops.first.slice(0, end);
681 // Now the second operand.
682 start = Ops.second.find_first_of('$');
683 if (start == std::string::npos)
684 throw TGError(Loc, "expected '$' prefix on asm operand name");
685 Ops.second = Ops.second.slice(start + 1, std::string::npos);
686 end = Ops.second.find_last_of(" \t");
687 Ops.first = Ops.first.slice(0, end);
688 return Ops;
689}
690
691void MatchableInfo::formTwoOperandAlias(StringRef Constraint) {
692 // Figure out which operands are aliased and mark them as tied.
693 std::pair<StringRef, StringRef> Ops =
694 parseTwoOperandConstraint(Constraint, TheDef->getLoc());
695
696 // Find the AsmOperands that refer to the operands we're aliasing.
697 int SrcAsmOperand = findAsmOperandNamed(Ops.first);
698 int DstAsmOperand = findAsmOperandNamed(Ops.second);
699 if (SrcAsmOperand == -1)
700 throw TGError(TheDef->getLoc(),
701 "unknown source two-operand alias operand '" +
702 Ops.first.str() + "'.");
703 if (DstAsmOperand == -1)
704 throw TGError(TheDef->getLoc(),
705 "unknown destination two-operand alias operand '" +
706 Ops.second.str() + "'.");
707
708 // Find the ResOperand that refers to the operand we're aliasing away
709 // and update it to refer to the combined operand instead.
710 for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
711 ResOperand &Op = ResOperands[i];
712 if (Op.Kind == ResOperand::RenderAsmOperand &&
713 Op.AsmOperandNum == (unsigned)SrcAsmOperand) {
714 Op.AsmOperandNum = DstAsmOperand;
715 break;
716 }
717 }
718 // Remove the AsmOperand for the alias operand.
719 AsmOperands.erase(AsmOperands.begin() + SrcAsmOperand);
720 // Adjust the ResOperand references to any AsmOperands that followed
721 // the one we just deleted.
722 for (unsigned i = 0, e = ResOperands.size(); i != e; ++i) {
723 ResOperand &Op = ResOperands[i];
724 switch(Op.Kind) {
725 default:
726 // Nothing to do for operands that don't reference AsmOperands.
727 break;
728 case ResOperand::RenderAsmOperand:
729 if (Op.AsmOperandNum > (unsigned)SrcAsmOperand)
730 --Op.AsmOperandNum;
731 break;
732 case ResOperand::TiedOperand:
733 if (Op.TiedOperandNum > (unsigned)SrcAsmOperand)
734 --Op.TiedOperandNum;
735 break;
736 }
737 }
738}
739
Jim Grosbach8caecde2012-04-19 17:52:32 +0000740void MatchableInfo::initialize(const AsmMatcherInfo &Info,
Devang Patel63faf822012-01-07 01:33:34 +0000741 SmallPtrSet<Record*, 16> &SingletonRegisters,
742 int AsmVariantNo, std::string &RegisterPrefix) {
Devang Patel56315d32012-01-10 17:50:43 +0000743 AsmVariantID = AsmVariantNo;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000744 AsmString =
Devang Patel63faf822012-01-07 01:33:34 +0000745 CodeGenInstruction::FlattenAsmStringVariants(AsmString, AsmVariantNo);
Bob Wilson828295b2011-01-26 21:26:19 +0000746
Jim Grosbach8caecde2012-04-19 17:52:32 +0000747 tokenizeAsmString(Info);
Bob Wilson828295b2011-01-26 21:26:19 +0000748
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000749 // Compute the require features.
750 std::vector<Record*> Predicates =TheDef->getValueAsListOfDefs("Predicates");
751 for (unsigned i = 0, e = Predicates.size(); i != e; ++i)
752 if (SubtargetFeatureInfo *Feature =
753 Info.getSubtargetFeature(Predicates[i]))
754 RequiredFeatures.push_back(Feature);
Bob Wilson828295b2011-01-26 21:26:19 +0000755
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000756 // Collect singleton registers, if used.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000757 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
Devang Patel63faf822012-01-07 01:33:34 +0000758 extractSingletonRegisterForAsmOperand(i, Info, RegisterPrefix);
759 if (Record *Reg = AsmOperands[i].SingletonReg)
Chris Lattnerc2d67bb2010-11-01 04:53:48 +0000760 SingletonRegisters.insert(Reg);
761 }
762}
763
Jim Grosbach8caecde2012-04-19 17:52:32 +0000764/// tokenizeAsmString - Tokenize a simplified assembly string.
765void MatchableInfo::tokenizeAsmString(const AsmMatcherInfo &Info) {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000766 StringRef String = AsmString;
767 unsigned Prev = 0;
768 bool InTok = true;
769 for (unsigned i = 0, e = String.size(); i != e; ++i) {
770 switch (String[i]) {
771 case '[':
772 case ']':
773 case '*':
774 case '!':
775 case ' ':
776 case '\t':
777 case ',':
778 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000779 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000780 InTok = false;
781 }
782 if (!isspace(String[i]) && String[i] != ',')
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000783 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000784 Prev = i + 1;
785 break;
786
787 case '\\':
788 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000789 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000790 InTok = false;
791 }
792 ++i;
793 assert(i != String.size() && "Invalid quoted character");
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000794 AsmOperands.push_back(AsmOperand(String.substr(i, 1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000795 Prev = i + 1;
796 break;
797
798 case '$': {
Chris Lattnerd19ec052010-11-02 17:30:52 +0000799 if (InTok) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000800 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000801 InTok = false;
802 }
Bob Wilson828295b2011-01-26 21:26:19 +0000803
Chris Lattner7ad31472010-11-06 22:06:03 +0000804 // If this isn't "${", treat like a normal token.
805 if (i + 1 == String.size() || String[i + 1] != '{') {
806 Prev = i;
807 break;
808 }
Chris Lattnerd19ec052010-11-02 17:30:52 +0000809
810 StringRef::iterator End = std::find(String.begin() + i, String.end(),'}');
811 assert(End != String.end() && "Missing brace in operand reference!");
812 size_t EndPos = End - String.begin();
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000813 AsmOperands.push_back(AsmOperand(String.slice(i, EndPos+1)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000814 Prev = EndPos + 1;
815 i = EndPos;
816 break;
817 }
818
819 case '.':
820 if (InTok)
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000821 AsmOperands.push_back(AsmOperand(String.slice(Prev, i)));
Chris Lattnerd19ec052010-11-02 17:30:52 +0000822 Prev = i;
823 InTok = true;
824 break;
825
826 default:
827 InTok = true;
828 }
829 }
830 if (InTok && Prev != String.size())
Chris Lattnerc0b14a22010-11-03 19:47:34 +0000831 AsmOperands.push_back(AsmOperand(String.substr(Prev)));
Bob Wilson828295b2011-01-26 21:26:19 +0000832
Chris Lattnerd19ec052010-11-02 17:30:52 +0000833 // The first token of the instruction is the mnemonic, which must be a
834 // simple string, not a $foo variable or a singleton register.
Jim Grosbach4a2242c2011-11-30 23:16:25 +0000835 if (AsmOperands.empty())
836 throw TGError(TheDef->getLoc(),
837 "Instruction '" + TheDef->getName() + "' has no tokens");
Chris Lattnerd19ec052010-11-02 17:30:52 +0000838 Mnemonic = AsmOperands[0].Token;
Jim Grosbach8e27c962012-05-06 17:33:14 +0000839 if (Mnemonic.empty())
840 throw TGError(TheDef->getLoc(),
841 "Missing instruction mnemonic");
Devang Patel63faf822012-01-07 01:33:34 +0000842 // FIXME : Check and raise an error if it is a register.
Devang Patelb78307f2012-01-07 01:22:23 +0000843 if (Mnemonic[0] == '$')
Chris Lattnerd19ec052010-11-02 17:30:52 +0000844 throw TGError(TheDef->getLoc(),
845 "Invalid instruction mnemonic '" + Mnemonic.str() + "'!");
Bob Wilson828295b2011-01-26 21:26:19 +0000846
Chris Lattnerd19ec052010-11-02 17:30:52 +0000847 // Remove the first operand, it is tracked in the mnemonic field.
848 AsmOperands.erase(AsmOperands.begin());
849}
850
Jim Grosbach8caecde2012-04-19 17:52:32 +0000851bool MatchableInfo::validate(StringRef CommentDelimiter, bool Hack) const {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000852 // Reject matchables with no .s string.
Chris Lattner5bc93872010-11-01 04:34:44 +0000853 if (AsmString.empty())
854 throw TGError(TheDef->getLoc(), "instruction with empty asm string");
Bob Wilson828295b2011-01-26 21:26:19 +0000855
Chris Lattner22bc5c42010-11-01 05:06:45 +0000856 // Reject any matchables with a newline in them, they should be marked
Chris Lattner5bc93872010-11-01 04:34:44 +0000857 // isCodeGenOnly if they are pseudo instructions.
858 if (AsmString.find('\n') != std::string::npos)
859 throw TGError(TheDef->getLoc(),
860 "multiline instruction is not valid for the asmparser, "
861 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000862
Chris Lattner4164f6b2010-11-01 04:44:29 +0000863 // Remove comments from the asm string. We know that the asmstring only
864 // has one line.
865 if (!CommentDelimiter.empty() &&
866 StringRef(AsmString).find(CommentDelimiter) != StringRef::npos)
867 throw TGError(TheDef->getLoc(),
868 "asmstring for instruction has comment character in it, "
869 "mark it isCodeGenOnly");
Bob Wilson828295b2011-01-26 21:26:19 +0000870
Chris Lattner22bc5c42010-11-01 05:06:45 +0000871 // Reject matchables with operand modifiers, these aren't something we can
Bob Wilson906bc362011-01-20 18:38:07 +0000872 // handle, the target should be refactored to use operands instead of
873 // modifiers.
Chris Lattner5bc93872010-11-01 04:34:44 +0000874 //
875 // Also, check for instructions which reference the operand multiple times;
876 // this implies a constraint we would not honor.
877 std::set<std::string> OperandNames;
Chris Lattnerd19ec052010-11-02 17:30:52 +0000878 for (unsigned i = 0, e = AsmOperands.size(); i != e; ++i) {
879 StringRef Tok = AsmOperands[i].Token;
880 if (Tok[0] == '$' && Tok.find(':') != StringRef::npos)
Chris Lattner5bc93872010-11-01 04:34:44 +0000881 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000882 "matchable with operand modifier '" + Tok.str() +
Chris Lattner5bc93872010-11-01 04:34:44 +0000883 "' not supported by asm matcher. Mark isCodeGenOnly!");
Bob Wilson828295b2011-01-26 21:26:19 +0000884
Chris Lattner22bc5c42010-11-01 05:06:45 +0000885 // Verify that any operand is only mentioned once.
Chris Lattnerd51257a2010-11-02 23:18:43 +0000886 // We reject aliases and ignore instructions for now.
Chris Lattnerd19ec052010-11-02 17:30:52 +0000887 if (Tok[0] == '$' && !OperandNames.insert(Tok).second) {
Chris Lattner22bc5c42010-11-01 05:06:45 +0000888 if (!Hack)
889 throw TGError(TheDef->getLoc(),
Chris Lattnerd19ec052010-11-02 17:30:52 +0000890 "ERROR: matchable with tied operand '" + Tok.str() +
Chris Lattner22bc5c42010-11-01 05:06:45 +0000891 "' can never be matched!");
892 // FIXME: Should reject these. The ARM backend hits this with $lane in a
893 // bunch of instructions. It is unclear what the right answer is.
Chris Lattner5bc93872010-11-01 04:34:44 +0000894 DEBUG({
Chris Lattner5abd1eb2010-11-06 06:43:11 +0000895 errs() << "warning: '" << TheDef->getName() << "': "
Chris Lattner22bc5c42010-11-01 05:06:45 +0000896 << "ignoring instruction with tied operand '"
Chris Lattnerd19ec052010-11-02 17:30:52 +0000897 << Tok.str() << "'\n";
Chris Lattner5bc93872010-11-01 04:34:44 +0000898 });
899 return false;
900 }
901 }
Bob Wilson828295b2011-01-26 21:26:19 +0000902
Chris Lattner5bc93872010-11-01 04:34:44 +0000903 return true;
904}
905
Jim Grosbachf35307c2012-01-24 21:06:59 +0000906/// extractSingletonRegisterForAsmOperand - Extract singleton register,
Devang Pateld06b01c2012-01-09 21:30:46 +0000907/// if present, from specified token.
Devang Patel63faf822012-01-07 01:33:34 +0000908void MatchableInfo::
Jim Grosbachf35307c2012-01-24 21:06:59 +0000909extractSingletonRegisterForAsmOperand(unsigned OperandNo,
Devang Pateld06b01c2012-01-09 21:30:46 +0000910 const AsmMatcherInfo &Info,
Jim Grosbach11fc6462012-04-11 21:02:33 +0000911 std::string &RegisterPrefix) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000912 StringRef Tok = AsmOperands[OperandNo].Token;
Devang Patel63faf822012-01-07 01:33:34 +0000913 if (RegisterPrefix.empty()) {
Devang Pateld06b01c2012-01-09 21:30:46 +0000914 std::string LoweredTok = Tok.lower();
915 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(LoweredTok))
916 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Devang Patel63faf822012-01-07 01:33:34 +0000917 return;
Jim Grosbachf35307c2012-01-24 21:06:59 +0000918 }
Bob Wilson828295b2011-01-26 21:26:19 +0000919
Devang Patel63faf822012-01-07 01:33:34 +0000920 if (!Tok.startswith(RegisterPrefix))
921 return;
922
923 StringRef RegName = Tok.substr(RegisterPrefix.size());
Chris Lattnerec6f0962010-11-02 18:10:06 +0000924 if (const CodeGenRegister *Reg = Info.Target.getRegisterByName(RegName))
Devang Pateld06b01c2012-01-09 21:30:46 +0000925 AsmOperands[OperandNo].SingletonReg = Reg->TheDef;
Bob Wilson828295b2011-01-26 21:26:19 +0000926
Chris Lattner1de88232010-11-01 01:47:07 +0000927 // If there is no register prefix (i.e. "%" in "%eax"), then this may
928 // be some random non-register token, just ignore it.
Devang Patel63faf822012-01-07 01:33:34 +0000929 return;
Chris Lattner02bcbc92010-11-01 01:37:30 +0000930}
931
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000932static std::string getEnumNameForToken(StringRef Str) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000933 std::string Res;
Jim Grosbacha7c78222010-10-29 22:13:48 +0000934
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000935 for (StringRef::iterator it = Str.begin(), ie = Str.end(); it != ie; ++it) {
936 switch (*it) {
937 case '*': Res += "_STAR_"; break;
938 case '%': Res += "_PCT_"; break;
939 case ':': Res += "_COLON_"; break;
Bill Wendlingbd9c77b2010-11-18 23:36:54 +0000940 case '!': Res += "_EXCLAIM_"; break;
Bill Wendling0ef755d2011-01-22 09:44:32 +0000941 case '.': Res += "_DOT_"; break;
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000942 default:
Chris Lattner39ee0362010-10-31 19:10:56 +0000943 if (isalnum(*it))
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000944 Res += *it;
Chris Lattner39ee0362010-10-31 19:10:56 +0000945 else
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000946 Res += "_" + utostr((unsigned) *it) + "_";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000947 }
948 }
949
950 return Res;
951}
952
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000953ClassInfo *AsmMatcherInfo::getTokenClass(StringRef Token) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000954 ClassInfo *&Entry = TokenClasses[Token];
Jim Grosbacha7c78222010-10-29 22:13:48 +0000955
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000956 if (!Entry) {
957 Entry = new ClassInfo();
958 Entry->Kind = ClassInfo::Token;
Daniel Dunbar6745d422009-08-09 05:18:30 +0000959 Entry->ClassName = "Token";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000960 Entry->Name = "MCK_" + getEnumNameForToken(Token);
961 Entry->ValueName = Token;
962 Entry->PredicateMethod = "<invalid>";
963 Entry->RenderMethod = "<invalid>";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +0000964 Entry->ParserMethod = "";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +0000965 Entry->DiagnosticType = "";
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000966 Classes.push_back(Entry);
967 }
968
969 return Entry;
970}
971
972ClassInfo *
Bob Wilsona49c7df2011-01-26 19:44:55 +0000973AsmMatcherInfo::getOperandClass(const CGIOperandList::OperandInfo &OI,
974 int SubOpIdx) {
975 Record *Rec = OI.Rec;
976 if (SubOpIdx != -1)
David Greene05bce0b2011-07-29 22:43:06 +0000977 Rec = dynamic_cast<DefInit*>(OI.MIOperandInfo->getArg(SubOpIdx))->getDef();
Jim Grosbach48c1f842011-10-28 22:32:53 +0000978 return getOperandClass(Rec, SubOpIdx);
979}
Bob Wilsona49c7df2011-01-26 19:44:55 +0000980
Jim Grosbach48c1f842011-10-28 22:32:53 +0000981ClassInfo *
982AsmMatcherInfo::getOperandClass(Record *Rec, int SubOpIdx) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000983 if (Rec->isSubClassOf("RegisterOperand")) {
984 // RegisterOperand may have an associated ParserMatchClass. If it does,
985 // use it, else just fall back to the underlying register class.
986 const RecordVal *R = Rec->getValue("ParserMatchClass");
987 if (R == 0 || R->getValue() == 0)
988 throw "Record `" + Rec->getName() +
989 "' does not have a ParserMatchClass!\n";
990
David Greene05bce0b2011-07-29 22:43:06 +0000991 if (DefInit *DI= dynamic_cast<DefInit*>(R->getValue())) {
Owen Andersonbea6f612011-06-27 21:06:21 +0000992 Record *MatchClass = DI->getDef();
993 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
994 return CI;
995 }
996
997 // No custom match class. Just use the register class.
998 Record *ClassRec = Rec->getValueAsDef("RegClass");
999 if (!ClassRec)
1000 throw TGError(Rec->getLoc(), "RegisterOperand `" + Rec->getName() +
1001 "' has no associated register class!\n");
1002 if (ClassInfo *CI = RegisterClassClasses[ClassRec])
1003 return CI;
1004 throw TGError(Rec->getLoc(), "register class has no class info!");
1005 }
1006
1007
Bob Wilsona49c7df2011-01-26 19:44:55 +00001008 if (Rec->isSubClassOf("RegisterClass")) {
1009 if (ClassInfo *CI = RegisterClassClasses[Rec])
Chris Lattnerec6f0962010-11-02 18:10:06 +00001010 return CI;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001011 throw TGError(Rec->getLoc(), "register class has no class info!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001012 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001013
Bob Wilsona49c7df2011-01-26 19:44:55 +00001014 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1015 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
Chris Lattnerec6f0962010-11-02 18:10:06 +00001016 if (ClassInfo *CI = AsmOperandClasses[MatchClass])
1017 return CI;
Daniel Dunbar338825c2009-08-10 18:41:10 +00001018
Bob Wilsona49c7df2011-01-26 19:44:55 +00001019 throw TGError(Rec->getLoc(), "operand has no match class!");
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001020}
1021
Chris Lattner1de88232010-11-01 01:47:07 +00001022void AsmMatcherInfo::
Jim Grosbach8caecde2012-04-19 17:52:32 +00001023buildRegisterClasses(SmallPtrSet<Record*, 16> &SingletonRegisters) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001024 const std::vector<CodeGenRegister*> &Registers =
1025 Target.getRegBank().getRegisters();
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001026 ArrayRef<CodeGenRegisterClass*> RegClassList =
1027 Target.getRegBank().getRegClasses();
Daniel Dunbar338825c2009-08-10 18:41:10 +00001028
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001029 // The register sets used for matching.
1030 std::set< std::set<Record*> > RegisterSets;
1031
Jim Grosbacha7c78222010-10-29 22:13:48 +00001032 // Gather the defined sets.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001033 for (ArrayRef<CodeGenRegisterClass*>::const_iterator it =
Chris Lattnerec6f0962010-11-02 18:10:06 +00001034 RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it)
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001035 RegisterSets.insert(std::set<Record*>(
1036 (*it)->getOrder().begin(), (*it)->getOrder().end()));
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001037
1038 // Add any required singleton sets.
Chris Lattner1de88232010-11-01 01:47:07 +00001039 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1040 ie = SingletonRegisters.end(); it != ie; ++it) {
1041 Record *Rec = *it;
1042 RegisterSets.insert(std::set<Record*>(&Rec, &Rec + 1));
1043 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001044
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001045 // Introduce derived sets where necessary (when a register does not determine
1046 // a unique register set class), and build the mapping of registers to the set
1047 // they should classify to.
1048 std::map<Record*, std::set<Record*> > RegisterMap;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001049 for (std::vector<CodeGenRegister*>::const_iterator it = Registers.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001050 ie = Registers.end(); it != ie; ++it) {
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001051 const CodeGenRegister &CGR = **it;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001052 // Compute the intersection of all sets containing this register.
1053 std::set<Record*> ContainingSet;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001054
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001055 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
1056 ie = RegisterSets.end(); it != ie; ++it) {
1057 if (!it->count(CGR.TheDef))
1058 continue;
1059
1060 if (ContainingSet.empty()) {
1061 ContainingSet = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001062 continue;
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001063 }
Bob Wilson828295b2011-01-26 21:26:19 +00001064
Chris Lattnerec6f0962010-11-02 18:10:06 +00001065 std::set<Record*> Tmp;
1066 std::swap(Tmp, ContainingSet);
1067 std::insert_iterator< std::set<Record*> > II(ContainingSet,
1068 ContainingSet.begin());
1069 std::set_intersection(Tmp.begin(), Tmp.end(), it->begin(), it->end(), II);
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001070 }
1071
1072 if (!ContainingSet.empty()) {
1073 RegisterSets.insert(ContainingSet);
1074 RegisterMap.insert(std::make_pair(CGR.TheDef, ContainingSet));
1075 }
1076 }
1077
1078 // Construct the register classes.
1079 std::map<std::set<Record*>, ClassInfo*> RegisterSetClasses;
1080 unsigned Index = 0;
1081 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
1082 ie = RegisterSets.end(); it != ie; ++it, ++Index) {
1083 ClassInfo *CI = new ClassInfo();
1084 CI->Kind = ClassInfo::RegisterClass0 + Index;
1085 CI->ClassName = "Reg" + utostr(Index);
1086 CI->Name = "MCK_Reg" + utostr(Index);
1087 CI->ValueName = "";
1088 CI->PredicateMethod = ""; // unused
1089 CI->RenderMethod = "addRegOperands";
Daniel Dunbar8409bfb2009-08-11 20:10:07 +00001090 CI->Registers = *it;
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001091 // FIXME: diagnostic type.
1092 CI->DiagnosticType = "";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001093 Classes.push_back(CI);
1094 RegisterSetClasses.insert(std::make_pair(*it, CI));
1095 }
1096
1097 // Find the superclasses; we could compute only the subgroup lattice edges,
1098 // but there isn't really a point.
1099 for (std::set< std::set<Record*> >::iterator it = RegisterSets.begin(),
1100 ie = RegisterSets.end(); it != ie; ++it) {
1101 ClassInfo *CI = RegisterSetClasses[*it];
1102 for (std::set< std::set<Record*> >::iterator it2 = RegisterSets.begin(),
1103 ie2 = RegisterSets.end(); it2 != ie2; ++it2)
Jim Grosbacha7c78222010-10-29 22:13:48 +00001104 if (*it != *it2 &&
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001105 std::includes(it2->begin(), it2->end(), it->begin(), it->end()))
1106 CI->SuperClasses.push_back(RegisterSetClasses[*it2]);
1107 }
1108
1109 // Name the register classes which correspond to a user defined RegisterClass.
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001110 for (ArrayRef<CodeGenRegisterClass*>::const_iterator
Chris Lattnerec6f0962010-11-02 18:10:06 +00001111 it = RegClassList.begin(), ie = RegClassList.end(); it != ie; ++it) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001112 const CodeGenRegisterClass &RC = **it;
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001113 // Def will be NULL for non-user defined register classes.
1114 Record *Def = RC.getDef();
1115 if (!Def)
1116 continue;
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001117 ClassInfo *CI = RegisterSetClasses[std::set<Record*>(RC.getOrder().begin(),
1118 RC.getOrder().end())];
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001119 if (CI->ValueName.empty()) {
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001120 CI->ClassName = RC.getName();
1121 CI->Name = "MCK_" + RC.getName();
1122 CI->ValueName = RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001123 } else
Jakob Stoklund Olesen29f018c2011-09-29 22:28:37 +00001124 CI->ValueName = CI->ValueName + "," + RC.getName();
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001125
Jakob Stoklund Olesen6fea31e2011-10-04 15:28:08 +00001126 RegisterClassClasses.insert(std::make_pair(Def, CI));
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001127 }
1128
1129 // Populate the map for individual registers.
1130 for (std::map<Record*, std::set<Record*> >::iterator it = RegisterMap.begin(),
1131 ie = RegisterMap.end(); it != ie; ++it)
Chris Lattnerec6f0962010-11-02 18:10:06 +00001132 RegisterClasses[it->first] = RegisterSetClasses[it->second];
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001133
1134 // Name the register classes which correspond to singleton registers.
Chris Lattner1de88232010-11-01 01:47:07 +00001135 for (SmallPtrSet<Record*, 16>::iterator it = SingletonRegisters.begin(),
1136 ie = SingletonRegisters.end(); it != ie; ++it) {
1137 Record *Rec = *it;
Chris Lattnerec6f0962010-11-02 18:10:06 +00001138 ClassInfo *CI = RegisterClasses[Rec];
Chris Lattner1de88232010-11-01 01:47:07 +00001139 assert(CI && "Missing singleton register class info!");
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001140
Chris Lattner1de88232010-11-01 01:47:07 +00001141 if (CI->ValueName.empty()) {
1142 CI->ClassName = Rec->getName();
1143 CI->Name = "MCK_" + Rec->getName();
1144 CI->ValueName = Rec->getName();
1145 } else
1146 CI->ValueName = CI->ValueName + "," + Rec->getName();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001147 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001148}
1149
Jim Grosbach8caecde2012-04-19 17:52:32 +00001150void AsmMatcherInfo::buildOperandClasses() {
Chris Lattnere66b7eb2010-11-01 23:57:23 +00001151 std::vector<Record*> AsmOperands =
1152 Records.getAllDerivedDefinitions("AsmOperandClass");
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001153
1154 // Pre-populate AsmOperandClasses map.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001155 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001156 ie = AsmOperands.end(); it != ie; ++it)
1157 AsmOperandClasses[*it] = new ClassInfo();
1158
Daniel Dunbar338825c2009-08-10 18:41:10 +00001159 unsigned Index = 0;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001160 for (std::vector<Record*>::iterator it = AsmOperands.begin(),
Daniel Dunbar338825c2009-08-10 18:41:10 +00001161 ie = AsmOperands.end(); it != ie; ++it, ++Index) {
Daniel Dunbara2f5e002010-01-30 01:02:37 +00001162 ClassInfo *CI = AsmOperandClasses[*it];
Daniel Dunbar338825c2009-08-10 18:41:10 +00001163 CI->Kind = ClassInfo::UserClass0 + Index;
1164
David Greene05bce0b2011-07-29 22:43:06 +00001165 ListInit *Supers = (*it)->getValueAsListInit("SuperClasses");
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001166 for (unsigned i = 0, e = Supers->getSize(); i != e; ++i) {
David Greene05bce0b2011-07-29 22:43:06 +00001167 DefInit *DI = dynamic_cast<DefInit*>(Supers->getElement(i));
Daniel Dunbar54ddf3d2010-05-22 21:02:29 +00001168 if (!DI) {
1169 PrintError((*it)->getLoc(), "Invalid super class reference!");
1170 continue;
1171 }
1172
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001173 ClassInfo *SC = AsmOperandClasses[DI->getDef()];
1174 if (!SC)
Daniel Dunbar338825c2009-08-10 18:41:10 +00001175 PrintError((*it)->getLoc(), "Invalid super class reference!");
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001176 else
1177 CI->SuperClasses.push_back(SC);
Daniel Dunbar338825c2009-08-10 18:41:10 +00001178 }
1179 CI->ClassName = (*it)->getValueAsString("Name");
1180 CI->Name = "MCK_" + CI->ClassName;
1181 CI->ValueName = (*it)->getName();
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001182
1183 // Get or construct the predicate method name.
David Greene05bce0b2011-07-29 22:43:06 +00001184 Init *PMName = (*it)->getValueInit("PredicateMethod");
1185 if (StringInit *SI = dynamic_cast<StringInit*>(PMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001186 CI->PredicateMethod = SI->getValue();
1187 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001188 assert(dynamic_cast<UnsetInit*>(PMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001189 "Unexpected PredicateMethod field!");
1190 CI->PredicateMethod = "is" + CI->ClassName;
1191 }
1192
1193 // Get or construct the render method name.
David Greene05bce0b2011-07-29 22:43:06 +00001194 Init *RMName = (*it)->getValueInit("RenderMethod");
1195 if (StringInit *SI = dynamic_cast<StringInit*>(RMName)) {
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001196 CI->RenderMethod = SI->getValue();
1197 } else {
David Greene05bce0b2011-07-29 22:43:06 +00001198 assert(dynamic_cast<UnsetInit*>(RMName) &&
Daniel Dunbar5c468e32009-08-10 21:00:45 +00001199 "Unexpected RenderMethod field!");
1200 CI->RenderMethod = "add" + CI->ClassName + "Operands";
1201 }
1202
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001203 // Get the parse method name or leave it as empty.
David Greene05bce0b2011-07-29 22:43:06 +00001204 Init *PRMName = (*it)->getValueInit("ParserMethod");
1205 if (StringInit *SI = dynamic_cast<StringInit*>(PRMName))
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001206 CI->ParserMethod = SI->getValue();
1207
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001208 // Get the diagnostic type or leave it as empty.
1209 // Get the parse method name or leave it as empty.
1210 Init *DiagnosticType = (*it)->getValueInit("DiagnosticType");
1211 if (StringInit *SI = dynamic_cast<StringInit*>(DiagnosticType))
1212 CI->DiagnosticType = SI->getValue();
1213
Daniel Dunbar338825c2009-08-10 18:41:10 +00001214 AsmOperandClasses[*it] = CI;
1215 Classes.push_back(CI);
1216 }
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001217}
1218
Bob Wilson828295b2011-01-26 21:26:19 +00001219AsmMatcherInfo::AsmMatcherInfo(Record *asmParser,
1220 CodeGenTarget &target,
Chris Lattner9c6b60e2010-12-15 04:48:22 +00001221 RecordKeeper &records)
Devang Patel63faf822012-01-07 01:33:34 +00001222 : Records(records), AsmParser(asmParser), Target(target) {
Daniel Dunbar59fc42d2009-08-11 20:59:47 +00001223}
1224
Jim Grosbach8caecde2012-04-19 17:52:32 +00001225/// buildOperandMatchInfo - Build the necessary information to handle user
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001226/// defined operand parsing methods.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001227void AsmMatcherInfo::buildOperandMatchInfo() {
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001228
Jim Grosbachd4824fc2012-04-18 23:46:25 +00001229 /// Map containing a mask with all operands indices that can be found for
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001230 /// that class inside a instruction.
1231 std::map<ClassInfo*, unsigned> OpClassMask;
1232
1233 for (std::vector<MatchableInfo*>::const_iterator it =
1234 Matchables.begin(), ie = Matchables.end();
1235 it != ie; ++it) {
1236 MatchableInfo &II = **it;
1237 OpClassMask.clear();
1238
1239 // Keep track of all operands of this instructions which belong to the
1240 // same class.
1241 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
1242 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
1243 if (Op.Class->ParserMethod.empty())
1244 continue;
1245 unsigned &OperandMask = OpClassMask[Op.Class];
1246 OperandMask |= (1 << i);
1247 }
1248
1249 // Generate operand match info for each mnemonic/operand class pair.
1250 for (std::map<ClassInfo*, unsigned>::iterator iit = OpClassMask.begin(),
1251 iie = OpClassMask.end(); iit != iie; ++iit) {
1252 unsigned OpMask = iit->second;
1253 ClassInfo *CI = iit->first;
Jim Grosbach8caecde2012-04-19 17:52:32 +00001254 OperandMatchInfo.push_back(OperandMatchEntry::create(&II, CI, OpMask));
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00001255 }
1256 }
1257}
1258
Jim Grosbach8caecde2012-04-19 17:52:32 +00001259void AsmMatcherInfo::buildInfo() {
Chris Lattner0aed1e72010-10-30 20:07:57 +00001260 // Build information about all of the AssemblerPredicates.
1261 std::vector<Record*> AllPredicates =
1262 Records.getAllDerivedDefinitions("Predicate");
1263 for (unsigned i = 0, e = AllPredicates.size(); i != e; ++i) {
1264 Record *Pred = AllPredicates[i];
1265 // Ignore predicates that are not intended for the assembler.
1266 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
1267 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001268
Chris Lattner4164f6b2010-11-01 04:44:29 +00001269 if (Pred->getName().empty())
1270 throw TGError(Pred->getLoc(), "Predicate has no name!");
Bob Wilson828295b2011-01-26 21:26:19 +00001271
Chris Lattner0aed1e72010-10-30 20:07:57 +00001272 unsigned FeatureNo = SubtargetFeatures.size();
1273 SubtargetFeatures[Pred] = new SubtargetFeatureInfo(Pred, FeatureNo);
1274 assert(FeatureNo < 32 && "Too many subtarget features!");
1275 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001276
Chris Lattner39ee0362010-10-31 19:10:56 +00001277 // Parse the instructions; we need to do this first so that we can gather the
1278 // singleton register classes.
Chris Lattner1de88232010-11-01 01:47:07 +00001279 SmallPtrSet<Record*, 16> SingletonRegisters;
Devang Patel0dbcada2012-01-09 19:13:28 +00001280 unsigned VariantCount = Target.getAsmParserVariantCount();
1281 for (unsigned VC = 0; VC != VariantCount; ++VC) {
1282 Record *AsmVariant = Target.getAsmParserVariant(VC);
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001283 std::string CommentDelimiter =
1284 AsmVariant->getValueAsString("CommentDelimiter");
Devang Patel0dbcada2012-01-09 19:13:28 +00001285 std::string RegisterPrefix = AsmVariant->getValueAsString("RegisterPrefix");
1286 int AsmVariantNo = AsmVariant->getValueAsInt("Variant");
Jim Grosbachf35307c2012-01-24 21:06:59 +00001287
Devang Patel0dbcada2012-01-09 19:13:28 +00001288 for (CodeGenTarget::inst_iterator I = Target.inst_begin(),
Jim Grosbach11fc6462012-04-11 21:02:33 +00001289 E = Target.inst_end(); I != E; ++I) {
Devang Patel0dbcada2012-01-09 19:13:28 +00001290 const CodeGenInstruction &CGI = **I;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001291
Devang Patel0dbcada2012-01-09 19:13:28 +00001292 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1293 // filter the set of instructions we consider.
1294 if (!StringRef(CGI.TheDef->getName()).startswith(MatchPrefix))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001295 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001296
Devang Patel0dbcada2012-01-09 19:13:28 +00001297 // Ignore "codegen only" instructions.
1298 if (CGI.TheDef->getValueAsBit("isCodeGenOnly"))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001299 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001300
Devang Patel0dbcada2012-01-09 19:13:28 +00001301 // Validate the operand list to ensure we can handle this instruction.
1302 for (unsigned i = 0, e = CGI.Operands.size(); i != e; ++i) {
Jim Grosbach11fc6462012-04-11 21:02:33 +00001303 const CGIOperandList::OperandInfo &OI = CGI.Operands[i];
1304
1305 // Validate tied operands.
1306 if (OI.getTiedRegister() != -1) {
1307 // If we have a tied operand that consists of multiple MCOperands,
1308 // reject it. We reject aliases and ignore instructions for now.
1309 if (OI.MINumOperands != 1) {
1310 // FIXME: Should reject these. The ARM backend hits this with $lane
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001311 // in a bunch of instructions. The right answer is unclear.
Jim Grosbach11fc6462012-04-11 21:02:33 +00001312 DEBUG({
1313 errs() << "warning: '" << CGI.TheDef->getName() << "': "
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001314 << "ignoring instruction with multi-operand tied operand '"
1315 << OI.Name << "'\n";
Jim Grosbach11fc6462012-04-11 21:02:33 +00001316 });
1317 continue;
1318 }
1319 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001320 }
Jim Grosbachf35307c2012-01-24 21:06:59 +00001321
Devang Patel0dbcada2012-01-09 19:13:28 +00001322 OwningPtr<MatchableInfo> II(new MatchableInfo(CGI));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001323
Jim Grosbach8caecde2012-04-19 17:52:32 +00001324 II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001325
Devang Patel0dbcada2012-01-09 19:13:28 +00001326 // Ignore instructions which shouldn't be matched and diagnose invalid
1327 // instruction definitions with an error.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001328 if (!II->validate(CommentDelimiter, true))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001329 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001330
Devang Patel0dbcada2012-01-09 19:13:28 +00001331 // Ignore "Int_*" and "*_Int" instructions, which are internal aliases.
1332 //
1333 // FIXME: This is a total hack.
1334 if (StringRef(II->TheDef->getName()).startswith("Int_") ||
Jim Grosbach11fc6462012-04-11 21:02:33 +00001335 StringRef(II->TheDef->getName()).endswith("_Int"))
1336 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001337
Devang Patel0dbcada2012-01-09 19:13:28 +00001338 Matchables.push_back(II.take());
Chris Lattner1d13bda2010-11-04 00:43:46 +00001339 }
Jim Grosbachf35307c2012-01-24 21:06:59 +00001340
Devang Patel0dbcada2012-01-09 19:13:28 +00001341 // Parse all of the InstAlias definitions and stick them in the list of
1342 // matchables.
1343 std::vector<Record*> AllInstAliases =
1344 Records.getAllDerivedDefinitions("InstAlias");
1345 for (unsigned i = 0, e = AllInstAliases.size(); i != e; ++i) {
1346 CodeGenInstAlias *Alias = new CodeGenInstAlias(AllInstAliases[i], Target);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001347
Devang Patel0dbcada2012-01-09 19:13:28 +00001348 // If the tblgen -match-prefix option is specified (for tblgen hackers),
1349 // filter the set of instruction aliases we consider, based on the target
1350 // instruction.
Jim Grosbach65da6fc2012-04-17 00:01:04 +00001351 if (!StringRef(Alias->ResultInst->TheDef->getName())
1352 .startswith( MatchPrefix))
Jim Grosbach11fc6462012-04-11 21:02:33 +00001353 continue;
Jim Grosbachf35307c2012-01-24 21:06:59 +00001354
Devang Patel0dbcada2012-01-09 19:13:28 +00001355 OwningPtr<MatchableInfo> II(new MatchableInfo(Alias));
Jim Grosbachf35307c2012-01-24 21:06:59 +00001356
Jim Grosbach8caecde2012-04-19 17:52:32 +00001357 II->initialize(*this, SingletonRegisters, AsmVariantNo, RegisterPrefix);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001358
Devang Patel0dbcada2012-01-09 19:13:28 +00001359 // Validate the alias definitions.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001360 II->validate(CommentDelimiter, false);
Jim Grosbachf35307c2012-01-24 21:06:59 +00001361
Devang Patel0dbcada2012-01-09 19:13:28 +00001362 Matchables.push_back(II.take());
1363 }
Chris Lattnerc76e80d2010-11-01 04:05:41 +00001364 }
Chris Lattnerc240bb02010-11-01 04:03:32 +00001365
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001366 // Build info for the register classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001367 buildRegisterClasses(SingletonRegisters);
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001368
1369 // Build info for the user defined assembly operand classes.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001370 buildOperandClasses();
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001371
Chris Lattner0bb780c2010-11-04 00:57:06 +00001372 // Build the information about matchables, now that we have fully formed
1373 // classes.
Jim Grosbachc1922c72012-04-19 23:59:23 +00001374 std::vector<MatchableInfo*> NewMatchables;
Chris Lattner22bc5c42010-11-01 05:06:45 +00001375 for (std::vector<MatchableInfo*>::iterator it = Matchables.begin(),
1376 ie = Matchables.end(); it != ie; ++it) {
1377 MatchableInfo *II = *it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001378
Chris Lattnere206fcf2010-09-06 21:01:37 +00001379 // Parse the tokens after the mnemonic.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001380 // Note: buildInstructionOperandReference may insert new AsmOperands, so
Bob Wilsona49c7df2011-01-26 19:44:55 +00001381 // don't precompute the loop bound.
1382 for (unsigned i = 0; i != II->AsmOperands.size(); ++i) {
Chris Lattnerc0b14a22010-11-03 19:47:34 +00001383 MatchableInfo::AsmOperand &Op = II->AsmOperands[i];
Chris Lattnerd19ec052010-11-02 17:30:52 +00001384 StringRef Token = Op.Token;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001385
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001386 // Check for singleton registers.
Devang Patel63faf822012-01-07 01:33:34 +00001387 if (Record *RegRecord = II->AsmOperands[i].SingletonReg) {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001388 Op.Class = RegisterClasses[RegRecord];
Chris Lattner02bcbc92010-11-01 01:37:30 +00001389 assert(Op.Class && Op.Class->Registers.size() == 1 &&
1390 "Unexpected class for singleton register");
Chris Lattner02bcbc92010-11-01 01:37:30 +00001391 continue;
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00001392 }
1393
Daniel Dunbar20927f22009-08-07 08:26:05 +00001394 // Check for simple tokens.
1395 if (Token[0] != '$') {
Chris Lattnerd19ec052010-11-02 17:30:52 +00001396 Op.Class = getTokenClass(Token);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001397 continue;
1398 }
1399
Chris Lattner7ad31472010-11-06 22:06:03 +00001400 if (Token.size() > 1 && isdigit(Token[1])) {
1401 Op.Class = getTokenClass(Token);
1402 continue;
1403 }
Bob Wilson828295b2011-01-26 21:26:19 +00001404
Chris Lattnerc07bd402010-11-04 02:11:18 +00001405 // Otherwise this is an operand reference.
Chris Lattner5f4280c2010-11-04 01:58:23 +00001406 StringRef OperandName;
1407 if (Token[1] == '{')
1408 OperandName = Token.substr(2, Token.size() - 3);
1409 else
1410 OperandName = Token.substr(1);
Bob Wilson828295b2011-01-26 21:26:19 +00001411
Chris Lattnerc07bd402010-11-04 02:11:18 +00001412 if (II->DefRec.is<const CodeGenInstruction*>())
Jim Grosbach8caecde2012-04-19 17:52:32 +00001413 buildInstructionOperandReference(II, OperandName, i);
Chris Lattnerc07bd402010-11-04 02:11:18 +00001414 else
Jim Grosbach8caecde2012-04-19 17:52:32 +00001415 buildAliasOperandReference(II, OperandName, Op);
Daniel Dunbar20927f22009-08-07 08:26:05 +00001416 }
Bob Wilson828295b2011-01-26 21:26:19 +00001417
Jim Grosbachc1922c72012-04-19 23:59:23 +00001418 if (II->DefRec.is<const CodeGenInstruction*>()) {
Jim Grosbach8caecde2012-04-19 17:52:32 +00001419 II->buildInstructionResultOperands();
Jim Grosbachc1922c72012-04-19 23:59:23 +00001420 // If the instruction has a two-operand alias, build up the
1421 // matchable here. We'll add them in bulk at the end to avoid
1422 // confusing this loop.
1423 std::string Constraint =
1424 II->TheDef->getValueAsString("TwoOperandAliasConstraint");
1425 if (Constraint != "") {
1426 // Start by making a copy of the original matchable.
1427 OwningPtr<MatchableInfo> AliasII(new MatchableInfo(*II));
1428
1429 // Adjust it to be a two-operand alias.
1430 AliasII->formTwoOperandAlias(Constraint);
1431
1432 // Add the alias to the matchables list.
1433 NewMatchables.push_back(AliasII.take());
1434 }
1435 } else
Jim Grosbach8caecde2012-04-19 17:52:32 +00001436 II->buildAliasResultOperands();
Daniel Dunbar20927f22009-08-07 08:26:05 +00001437 }
Jim Grosbachc1922c72012-04-19 23:59:23 +00001438 if (!NewMatchables.empty())
1439 Matchables.insert(Matchables.end(), NewMatchables.begin(),
1440 NewMatchables.end());
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001441
Jim Grosbacha66512e2011-12-06 23:43:54 +00001442 // Process token alias definitions and set up the associated superclass
1443 // information.
1444 std::vector<Record*> AllTokenAliases =
1445 Records.getAllDerivedDefinitions("TokenAlias");
1446 for (unsigned i = 0, e = AllTokenAliases.size(); i != e; ++i) {
1447 Record *Rec = AllTokenAliases[i];
1448 ClassInfo *FromClass = getTokenClass(Rec->getValueAsString("FromToken"));
1449 ClassInfo *ToClass = getTokenClass(Rec->getValueAsString("ToToken"));
Jim Grosbach67cd20d2012-04-17 21:23:52 +00001450 if (FromClass == ToClass)
1451 throw TGError(Rec->getLoc(),
1452 "error: Destination value identical to source value.");
Jim Grosbacha66512e2011-12-06 23:43:54 +00001453 FromClass->SuperClasses.push_back(ToClass);
1454 }
1455
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001456 // Reorder classes so that classes precede super classes.
Daniel Dunbar5fe63382009-08-09 07:20:21 +00001457 std::sort(Classes.begin(), Classes.end(), less_ptr<ClassInfo>());
Daniel Dunbar20927f22009-08-07 08:26:05 +00001458}
1459
Jim Grosbach8caecde2012-04-19 17:52:32 +00001460/// buildInstructionOperandReference - The specified operand is a reference to a
Chris Lattner0bb780c2010-11-04 00:57:06 +00001461/// named operand such as $src. Resolve the Class and OperandInfo pointers.
1462void AsmMatcherInfo::
Jim Grosbach8caecde2012-04-19 17:52:32 +00001463buildInstructionOperandReference(MatchableInfo *II,
Chris Lattner5f4280c2010-11-04 01:58:23 +00001464 StringRef OperandName,
Bob Wilsona49c7df2011-01-26 19:44:55 +00001465 unsigned AsmOpIdx) {
Chris Lattnerc07bd402010-11-04 02:11:18 +00001466 const CodeGenInstruction &CGI = *II->DefRec.get<const CodeGenInstruction*>();
1467 const CGIOperandList &Operands = CGI.Operands;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001468 MatchableInfo::AsmOperand *Op = &II->AsmOperands[AsmOpIdx];
Bob Wilson828295b2011-01-26 21:26:19 +00001469
Chris Lattner662e5a32010-11-06 07:14:44 +00001470 // Map this token to an operand.
Chris Lattner0bb780c2010-11-04 00:57:06 +00001471 unsigned Idx;
1472 if (!Operands.hasOperandNamed(OperandName, Idx))
1473 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1474 OperandName.str() + "'");
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001475
Bob Wilsona49c7df2011-01-26 19:44:55 +00001476 // If the instruction operand has multiple suboperands, but the parser
1477 // match class for the asm operand is still the default "ImmAsmOperand",
1478 // then handle each suboperand separately.
1479 if (Op->SubOpIdx == -1 && Operands[Idx].MINumOperands > 1) {
1480 Record *Rec = Operands[Idx].Rec;
1481 assert(Rec->isSubClassOf("Operand") && "Unexpected operand!");
1482 Record *MatchClass = Rec->getValueAsDef("ParserMatchClass");
1483 if (MatchClass && MatchClass->getValueAsString("Name") == "Imm") {
1484 // Insert remaining suboperands after AsmOpIdx in II->AsmOperands.
1485 StringRef Token = Op->Token; // save this in case Op gets moved
1486 for (unsigned SI = 1, SE = Operands[Idx].MINumOperands; SI != SE; ++SI) {
1487 MatchableInfo::AsmOperand NewAsmOp(Token);
1488 NewAsmOp.SubOpIdx = SI;
1489 II->AsmOperands.insert(II->AsmOperands.begin()+AsmOpIdx+SI, NewAsmOp);
1490 }
1491 // Replace Op with first suboperand.
1492 Op = &II->AsmOperands[AsmOpIdx]; // update the pointer in case it moved
1493 Op->SubOpIdx = 0;
1494 }
1495 }
1496
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001497 // Set up the operand class.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001498 Op->Class = getOperandClass(Operands[Idx], Op->SubOpIdx);
Chris Lattnerba3b5b62010-11-04 01:55:23 +00001499
1500 // If the named operand is tied, canonicalize it to the untied operand.
1501 // For example, something like:
1502 // (outs GPR:$dst), (ins GPR:$src)
1503 // with an asmstring of
1504 // "inc $src"
1505 // we want to canonicalize to:
1506 // "inc $dst"
1507 // so that we know how to provide the $dst operand when filling in the result.
1508 int OITied = Operands[Idx].getTiedRegister();
Chris Lattner0bb780c2010-11-04 00:57:06 +00001509 if (OITied != -1) {
1510 // The tied operand index is an MIOperand index, find the operand that
1511 // contains it.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001512 std::pair<unsigned, unsigned> Idx = Operands.getSubOperandNumber(OITied);
1513 OperandName = Operands[Idx.first].Name;
1514 Op->SubOpIdx = Idx.second;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001515 }
Bob Wilson828295b2011-01-26 21:26:19 +00001516
Bob Wilsona49c7df2011-01-26 19:44:55 +00001517 Op->SrcOpName = OperandName;
Chris Lattner0bb780c2010-11-04 00:57:06 +00001518}
1519
Jim Grosbach8caecde2012-04-19 17:52:32 +00001520/// buildAliasOperandReference - When parsing an operand reference out of the
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001521/// matching string (e.g. "movsx $src, $dst"), determine what the class of the
1522/// operand reference is by looking it up in the result pattern definition.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001523void AsmMatcherInfo::buildAliasOperandReference(MatchableInfo *II,
Chris Lattnerc07bd402010-11-04 02:11:18 +00001524 StringRef OperandName,
1525 MatchableInfo::AsmOperand &Op) {
1526 const CodeGenInstAlias &CGA = *II->DefRec.get<const CodeGenInstAlias*>();
Bob Wilson828295b2011-01-26 21:26:19 +00001527
Chris Lattnerc07bd402010-11-04 02:11:18 +00001528 // Set up the operand class.
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001529 for (unsigned i = 0, e = CGA.ResultOperands.size(); i != e; ++i)
Chris Lattner98c870f2010-11-06 19:25:43 +00001530 if (CGA.ResultOperands[i].isRecord() &&
1531 CGA.ResultOperands[i].getName() == OperandName) {
Chris Lattner662e5a32010-11-06 07:14:44 +00001532 // It's safe to go with the first one we find, because CodeGenInstAlias
1533 // validates that all operands with the same name have the same record.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001534 Op.SubOpIdx = CGA.ResultInstOperandIndex[i].second;
Jim Grosbach48c1f842011-10-28 22:32:53 +00001535 // Use the match class from the Alias definition, not the
1536 // destination instruction, as we may have an immediate that's
1537 // being munged by the match class.
1538 Op.Class = getOperandClass(CGA.ResultOperands[i].getRecord(),
Bob Wilsona49c7df2011-01-26 19:44:55 +00001539 Op.SubOpIdx);
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001540 Op.SrcOpName = OperandName;
1541 return;
Chris Lattnerc07bd402010-11-04 02:11:18 +00001542 }
Chris Lattner3f2c8e42010-11-06 07:06:09 +00001543
1544 throw TGError(II->TheDef->getLoc(), "error: unable to find operand: '" +
1545 OperandName.str() + "'");
Chris Lattnerc07bd402010-11-04 02:11:18 +00001546}
1547
Jim Grosbach8caecde2012-04-19 17:52:32 +00001548void MatchableInfo::buildInstructionResultOperands() {
Chris Lattner662e5a32010-11-06 07:14:44 +00001549 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001550
Chris Lattner662e5a32010-11-06 07:14:44 +00001551 // Loop over all operands of the result instruction, determining how to
1552 // populate them.
1553 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
1554 const CGIOperandList::OperandInfo &OpInfo = ResultInst->Operands[i];
Chris Lattner567820c2010-11-04 01:42:59 +00001555
1556 // If this is a tied operand, just copy from the previously handled operand.
1557 int TiedOp = OpInfo.getTiedRegister();
1558 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001559 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner567820c2010-11-04 01:42:59 +00001560 continue;
1561 }
Bob Wilson828295b2011-01-26 21:26:19 +00001562
Bob Wilsona49c7df2011-01-26 19:44:55 +00001563 // Find out what operand from the asmparser this MCInst operand comes from.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001564 int SrcOperand = findAsmOperandNamed(OpInfo.Name);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001565 if (OpInfo.Name.empty() || SrcOperand == -1)
1566 throw TGError(TheDef->getLoc(), "Instruction '" +
1567 TheDef->getName() + "' has operand '" + OpInfo.Name +
1568 "' that doesn't appear in asm string!");
Chris Lattner567820c2010-11-04 01:42:59 +00001569
Bob Wilsona49c7df2011-01-26 19:44:55 +00001570 // Check if the one AsmOperand populates the entire operand.
1571 unsigned NumOperands = OpInfo.MINumOperands;
1572 if (AsmOperands[SrcOperand].SubOpIdx == -1) {
1573 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand, NumOperands));
Chris Lattner1d13bda2010-11-04 00:43:46 +00001574 continue;
1575 }
Bob Wilsona49c7df2011-01-26 19:44:55 +00001576
1577 // Add a separate ResOperand for each suboperand.
1578 for (unsigned AI = 0; AI < NumOperands; ++AI) {
1579 assert(AsmOperands[SrcOperand+AI].SubOpIdx == (int)AI &&
1580 AsmOperands[SrcOperand+AI].SrcOpName == OpInfo.Name &&
1581 "unexpected AsmOperands for suboperands");
1582 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand + AI, 1));
1583 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001584 }
1585}
1586
Jim Grosbach8caecde2012-04-19 17:52:32 +00001587void MatchableInfo::buildAliasResultOperands() {
Chris Lattner41409852010-11-06 07:31:43 +00001588 const CodeGenInstAlias &CGA = *DefRec.get<const CodeGenInstAlias*>();
1589 const CodeGenInstruction *ResultInst = getResultInst();
Bob Wilson828295b2011-01-26 21:26:19 +00001590
Chris Lattner41409852010-11-06 07:31:43 +00001591 // Loop over all operands of the result instruction, determining how to
1592 // populate them.
1593 unsigned AliasOpNo = 0;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001594 unsigned LastOpNo = CGA.ResultInstOperandIndex.size();
Chris Lattner41409852010-11-06 07:31:43 +00001595 for (unsigned i = 0, e = ResultInst->Operands.size(); i != e; ++i) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001596 const CGIOperandList::OperandInfo *OpInfo = &ResultInst->Operands[i];
Bob Wilson828295b2011-01-26 21:26:19 +00001597
Chris Lattner41409852010-11-06 07:31:43 +00001598 // If this is a tied operand, just copy from the previously handled operand.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001599 int TiedOp = OpInfo->getTiedRegister();
Chris Lattner41409852010-11-06 07:31:43 +00001600 if (TiedOp != -1) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001601 ResOperands.push_back(ResOperand::getTiedOp(TiedOp));
Chris Lattner90fd7972010-11-06 19:57:21 +00001602 continue;
1603 }
1604
Bob Wilsona49c7df2011-01-26 19:44:55 +00001605 // Handle all the suboperands for this operand.
1606 const std::string &OpName = OpInfo->Name;
1607 for ( ; AliasOpNo < LastOpNo &&
1608 CGA.ResultInstOperandIndex[AliasOpNo].first == i; ++AliasOpNo) {
1609 int SubIdx = CGA.ResultInstOperandIndex[AliasOpNo].second;
1610
1611 // Find out what operand from the asmparser that this MCInst operand
1612 // comes from.
1613 switch (CGA.ResultOperands[AliasOpNo].Kind) {
Bob Wilsona49c7df2011-01-26 19:44:55 +00001614 case CodeGenInstAlias::ResultOperand::K_Record: {
1615 StringRef Name = CGA.ResultOperands[AliasOpNo].getName();
Jim Grosbach8caecde2012-04-19 17:52:32 +00001616 int SrcOperand = findAsmOperand(Name, SubIdx);
Bob Wilsona49c7df2011-01-26 19:44:55 +00001617 if (SrcOperand == -1)
1618 throw TGError(TheDef->getLoc(), "Instruction '" +
1619 TheDef->getName() + "' has operand '" + OpName +
1620 "' that doesn't appear in asm string!");
1621 unsigned NumOperands = (SubIdx == -1 ? OpInfo->MINumOperands : 1);
1622 ResOperands.push_back(ResOperand::getRenderedOp(SrcOperand,
1623 NumOperands));
1624 break;
1625 }
1626 case CodeGenInstAlias::ResultOperand::K_Imm: {
1627 int64_t ImmVal = CGA.ResultOperands[AliasOpNo].getImm();
1628 ResOperands.push_back(ResOperand::getImmOp(ImmVal));
1629 break;
1630 }
1631 case CodeGenInstAlias::ResultOperand::K_Reg: {
1632 Record *Reg = CGA.ResultOperands[AliasOpNo].getRegister();
1633 ResOperands.push_back(ResOperand::getRegOp(Reg));
1634 break;
1635 }
1636 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001637 }
Chris Lattner41409852010-11-06 07:31:43 +00001638 }
1639}
Chris Lattner1d13bda2010-11-04 00:43:46 +00001640
Jim Grosbach8caecde2012-04-19 17:52:32 +00001641static void emitConvertToMCInst(CodeGenTarget &Target, StringRef ClassName,
Chris Lattner22bc5c42010-11-01 05:06:45 +00001642 std::vector<MatchableInfo*> &Infos,
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00001643 raw_ostream &OS) {
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001644 // Write the convert function to a separate stream, so we can drop it after
1645 // the enum.
1646 std::string ConvertFnBody;
1647 raw_string_ostream CvtOS(ConvertFnBody);
1648
Daniel Dunbar20927f22009-08-07 08:26:05 +00001649 // Function we have already generated.
1650 std::set<std::string> GeneratedFns;
1651
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001652 // Start the unified conversion function.
Daniel Dunbar5c228a92011-02-04 23:17:40 +00001653 CvtOS << "bool " << Target.getName() << ClassName << "::\n";
1654 CvtOS << "ConvertToMCInst(unsigned Kind, MCInst &Inst, "
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001655 << "unsigned Opcode,\n"
Chris Lattner98986712010-01-14 22:21:20 +00001656 << " const SmallVectorImpl<MCParsedAsmOperand*"
1657 << "> &Operands) {\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001658 CvtOS << " Inst.setOpcode(Opcode);\n";
1659 CvtOS << " switch (Kind) {\n";
1660 CvtOS << " default:\n";
1661
1662 // Start the enum, which we will generate inline.
1663
Chris Lattnerd51257a2010-11-02 23:18:43 +00001664 OS << "// Unified function for converting operands to MCInst instances.\n\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001665 OS << "enum ConversionKind {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001666
Chris Lattner98986712010-01-14 22:21:20 +00001667 // TargetOperandClass - This is the target's operand class, like X86Operand.
1668 std::string TargetOperandClass = Target.getName() + "Operand";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001669
Chris Lattner22bc5c42010-11-01 05:06:45 +00001670 for (std::vector<MatchableInfo*>::const_iterator it = Infos.begin(),
Daniel Dunbar20927f22009-08-07 08:26:05 +00001671 ie = Infos.end(); it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00001672 MatchableInfo &II = **it;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001673
Daniel Dunbarcf120672011-02-04 17:12:15 +00001674 // Check if we have a custom match function.
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001675 std::string AsmMatchConverter =
1676 II.getResultInst()->TheDef->getValueAsString("AsmMatchConverter");
Daniel Dunbarcf120672011-02-04 17:12:15 +00001677 if (!AsmMatchConverter.empty()) {
Daniel Dunbar27b83d42011-04-01 20:23:52 +00001678 std::string Signature = "ConvertCustom_" + AsmMatchConverter;
Daniel Dunbarcf120672011-02-04 17:12:15 +00001679 II.ConversionFnKind = Signature;
1680
1681 // Check if we have already generated this signature.
1682 if (!GeneratedFns.insert(Signature).second)
1683 continue;
1684
1685 // If not, emit it now. Add to the enum list.
1686 OS << " " << Signature << ",\n";
1687
1688 CvtOS << " case " << Signature << ":\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001689 CvtOS << " return " << AsmMatchConverter
1690 << "(Inst, Opcode, Operands);\n";
Daniel Dunbarcf120672011-02-04 17:12:15 +00001691 continue;
1692 }
1693
Daniel Dunbar20927f22009-08-07 08:26:05 +00001694 // Build the conversion function signature.
1695 std::string Signature = "Convert";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001696 std::string CaseBody;
1697 raw_string_ostream CaseOS(CaseBody);
Bob Wilson828295b2011-01-26 21:26:19 +00001698
Chris Lattnerdda855d2010-11-02 21:49:44 +00001699 // Compute the convert enum and the case body.
Chris Lattner1d13bda2010-11-04 00:43:46 +00001700 for (unsigned i = 0, e = II.ResOperands.size(); i != e; ++i) {
1701 const MatchableInfo::ResOperand &OpInfo = II.ResOperands[i];
Jim Grosbacha7c78222010-10-29 22:13:48 +00001702
Chris Lattner1d13bda2010-11-04 00:43:46 +00001703 // Generate code to populate each result operand.
1704 switch (OpInfo.Kind) {
Chris Lattner1d13bda2010-11-04 00:43:46 +00001705 case MatchableInfo::ResOperand::RenderAsmOperand: {
1706 // This comes from something we parsed.
1707 MatchableInfo::AsmOperand &Op = II.AsmOperands[OpInfo.AsmOperandNum];
Bob Wilson828295b2011-01-26 21:26:19 +00001708
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001709 // Registers are always converted the same, don't duplicate the
1710 // conversion function based on them.
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001711 Signature += "__";
1712 if (Op.Class->isRegisterClass())
1713 Signature += "Reg";
1714 else
1715 Signature += Op.Class->ClassName;
Bob Wilsona49c7df2011-01-26 19:44:55 +00001716 Signature += utostr(OpInfo.MINumOperands);
Chris Lattner1d13bda2010-11-04 00:43:46 +00001717 Signature += "_" + itostr(OpInfo.AsmOperandNum);
Bob Wilson828295b2011-01-26 21:26:19 +00001718
Chris Lattner9b0d4bf2010-11-02 22:55:03 +00001719 CaseOS << " ((" << TargetOperandClass << "*)Operands["
Chris Lattner1d13bda2010-11-04 00:43:46 +00001720 << (OpInfo.AsmOperandNum+1) << "])->" << Op.Class->RenderMethod
Bob Wilsona49c7df2011-01-26 19:44:55 +00001721 << "(Inst, " << OpInfo.MINumOperands << ");\n";
Chris Lattner1d13bda2010-11-04 00:43:46 +00001722 break;
Daniel Dunbaraf616812010-02-10 08:15:48 +00001723 }
Bob Wilson828295b2011-01-26 21:26:19 +00001724
Chris Lattner1d13bda2010-11-04 00:43:46 +00001725 case MatchableInfo::ResOperand::TiedOperand: {
1726 // If this operand is tied to a previous one, just copy the MCInst
1727 // operand from the earlier one.We can only tie single MCOperand values.
Bob Wilsona49c7df2011-01-26 19:44:55 +00001728 //assert(OpInfo.MINumOperands == 1 && "Not a singular MCOperand");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001729 unsigned TiedOp = OpInfo.TiedOperandNum;
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001730 assert(i > TiedOp && "Tied operand precedes its target!");
Chris Lattner1d13bda2010-11-04 00:43:46 +00001731 CaseOS << " Inst.addOperand(Inst.getOperand(" << TiedOp << "));\n";
1732 Signature += "__Tie" + utostr(TiedOp);
1733 break;
1734 }
Chris Lattner98c870f2010-11-06 19:25:43 +00001735 case MatchableInfo::ResOperand::ImmOperand: {
1736 int64_t Val = OpInfo.ImmVal;
1737 CaseOS << " Inst.addOperand(MCOperand::CreateImm(" << Val << "));\n";
1738 Signature += "__imm" + itostr(Val);
1739 break;
1740 }
Chris Lattner90fd7972010-11-06 19:57:21 +00001741 case MatchableInfo::ResOperand::RegOperand: {
Bob Wilsondc1a2bd2011-01-14 22:58:09 +00001742 if (OpInfo.Register == 0) {
1743 CaseOS << " Inst.addOperand(MCOperand::CreateReg(0));\n";
1744 Signature += "__reg0";
1745 } else {
1746 std::string N = getQualifiedName(OpInfo.Register);
1747 CaseOS << " Inst.addOperand(MCOperand::CreateReg(" << N << "));\n";
1748 Signature += "__reg" + OpInfo.Register->getName();
1749 }
Bob Wilson828295b2011-01-26 21:26:19 +00001750 }
Chris Lattner1d13bda2010-11-04 00:43:46 +00001751 }
Daniel Dunbar20927f22009-08-07 08:26:05 +00001752 }
Bob Wilson828295b2011-01-26 21:26:19 +00001753
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001754 II.ConversionFnKind = Signature;
Daniel Dunbar20927f22009-08-07 08:26:05 +00001755
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001756 // Check if we have already generated this signature.
Daniel Dunbar20927f22009-08-07 08:26:05 +00001757 if (!GeneratedFns.insert(Signature).second)
1758 continue;
1759
Chris Lattnerdda855d2010-11-02 21:49:44 +00001760 // If not, emit it now. Add to the enum list.
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001761 OS << " " << Signature << ",\n";
1762
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001763 CvtOS << " case " << Signature << ":\n";
Chris Lattnerdda855d2010-11-02 21:49:44 +00001764 CvtOS << CaseOS.str();
Daniel Dunbarb4129152011-02-04 17:12:23 +00001765 CvtOS << " return true;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001766 }
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001767
1768 // Finish the convert function.
1769
1770 CvtOS << " }\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00001771 CvtOS << " return false;\n";
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001772 CvtOS << "}\n\n";
1773
1774 // Finish the enum, and drop the convert function after it.
1775
1776 OS << " NumConversionVariants\n";
1777 OS << "};\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001778
Daniel Dunbarb7479c02009-08-08 05:24:34 +00001779 OS << CvtOS.str();
Daniel Dunbara027d222009-07-31 02:32:59 +00001780}
1781
Jim Grosbach8caecde2012-04-19 17:52:32 +00001782/// emitMatchClassEnumeration - Emit the enumeration for match class kinds.
1783static void emitMatchClassEnumeration(CodeGenTarget &Target,
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001784 std::vector<ClassInfo*> &Infos,
1785 raw_ostream &OS) {
1786 OS << "namespace {\n\n";
1787
1788 OS << "/// MatchClassKind - The kinds of classes which participate in\n"
1789 << "/// instruction matching.\n";
1790 OS << "enum MatchClassKind {\n";
1791 OS << " InvalidMatchClass = 0,\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001792 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001793 ie = Infos.end(); it != ie; ++it) {
1794 ClassInfo &CI = **it;
1795 OS << " " << CI.Name << ", // ";
1796 if (CI.Kind == ClassInfo::Token) {
1797 OS << "'" << CI.ValueName << "'\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001798 } else if (CI.isRegisterClass()) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001799 if (!CI.ValueName.empty())
1800 OS << "register class '" << CI.ValueName << "'\n";
1801 else
1802 OS << "derived register class\n";
1803 } else {
1804 OS << "user defined class '" << CI.ValueName << "'\n";
1805 }
1806 }
1807 OS << " NumMatchClassKinds\n";
1808 OS << "};\n\n";
1809
1810 OS << "}\n\n";
1811}
1812
Jim Grosbach8caecde2012-04-19 17:52:32 +00001813/// emitValidateOperandClass - Emit the function to validate an operand class.
1814static void emitValidateOperandClass(AsmMatcherInfo &Info,
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001815 raw_ostream &OS) {
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001816 OS << "static unsigned validateOperandClass(MCParsedAsmOperand *GOp, "
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001817 << "MatchClassKind Kind) {\n";
1818 OS << " " << Info.Target.getName() << "Operand &Operand = *("
Chris Lattner02bcbc92010-11-01 01:37:30 +00001819 << Info.Target.getName() << "Operand*)GOp;\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001820
Kevin Enderby89381832011-07-15 18:30:43 +00001821 // The InvalidMatchClass is not to match any operand.
1822 OS << " if (Kind == InvalidMatchClass)\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001823 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n\n";
Kevin Enderby89381832011-07-15 18:30:43 +00001824
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001825 // Check for Token operands first.
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001826 // FIXME: Use a more specific diagnostic type.
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001827 OS << " if (Operand.isToken())\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001828 OS << " return isSubclass(matchTokenString(Operand.getToken()), Kind) ?\n"
1829 << " MCTargetAsmParser::Match_Success :\n"
1830 << " MCTargetAsmParser::Match_InvalidOperand;\n\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001831
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001832 // Check the user classes. We don't care what order since we're only
1833 // actually matching against one of them.
Jim Grosbacha7c78222010-10-29 22:13:48 +00001834 for (std::vector<ClassInfo*>::iterator it = Info.Classes.begin(),
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001835 ie = Info.Classes.end(); it != ie; ++it) {
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001836 ClassInfo &CI = **it;
1837
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001838 if (!CI.isUserClass())
1839 continue;
Bob Wilson828295b2011-01-26 21:26:19 +00001840
Jim Grosbachb9db0c52011-02-10 00:08:28 +00001841 OS << " // '" << CI.ClassName << "' class\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001842 OS << " if (Kind == " << CI.Name << ") {\n";
1843 OS << " if (Operand." << CI.PredicateMethod << "())\n";
1844 OS << " return MCTargetAsmParser::Match_Success;\n";
1845 if (!CI.DiagnosticType.empty())
1846 OS << " return " << Info.Target.getName() << "AsmParser::Match_"
1847 << CI.DiagnosticType << ";\n";
Daniel Dunbarea6408f2009-08-11 02:59:53 +00001848 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001849 }
Bob Wilson828295b2011-01-26 21:26:19 +00001850
Owen Andersonb885dc82012-07-16 23:20:09 +00001851 // Check for register operands, including sub-classes.
1852 OS << " if (Operand.isReg()) {\n";
1853 OS << " MatchClassKind OpKind;\n";
1854 OS << " switch (Operand.getReg()) {\n";
1855 OS << " default: OpKind = InvalidMatchClass; break;\n";
1856 for (std::map<Record*, ClassInfo*>::iterator
1857 it = Info.RegisterClasses.begin(), ie = Info.RegisterClasses.end();
1858 it != ie; ++it)
1859 OS << " case " << Info.Target.getName() << "::"
1860 << it->first->getName() << ": OpKind = " << it->second->Name
1861 << "; break;\n";
1862 OS << " }\n";
1863 OS << " return isSubclass(OpKind, Kind) ? "
1864 << "MCTargetAsmParser::Match_Success :\n "
1865 << " MCTargetAsmParser::Match_InvalidOperand;\n }\n\n";
1866
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001867 // Generic fallthrough match failure case for operands that don't have
1868 // specialized diagnostic types.
1869 OS << " return MCTargetAsmParser::Match_InvalidOperand;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00001870 OS << "}\n\n";
1871}
1872
Jim Grosbach8caecde2012-04-19 17:52:32 +00001873/// emitIsSubclass - Emit the subclass predicate function.
1874static void emitIsSubclass(CodeGenTarget &Target,
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001875 std::vector<ClassInfo*> &Infos,
1876 raw_ostream &OS) {
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001877 OS << "/// isSubclass - Compute whether \\arg A is a subclass of \\arg B.\n";
1878 OS << "static bool isSubclass(MatchClassKind A, MatchClassKind B) {\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001879 OS << " if (A == B)\n";
1880 OS << " return true;\n\n";
1881
1882 OS << " switch (A) {\n";
1883 OS << " default:\n";
1884 OS << " return false;\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00001885 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001886 ie = Infos.end(); it != ie; ++it) {
1887 ClassInfo &A = **it;
1888
Jim Grosbacha66512e2011-12-06 23:43:54 +00001889 std::vector<StringRef> SuperClasses;
1890 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
1891 ie = Infos.end(); it != ie; ++it) {
1892 ClassInfo &B = **it;
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001893
Jim Grosbacha66512e2011-12-06 23:43:54 +00001894 if (&A != &B && A.isSubsetOf(B))
1895 SuperClasses.push_back(B.Name);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001896 }
Jim Grosbacha66512e2011-12-06 23:43:54 +00001897
1898 if (SuperClasses.empty())
1899 continue;
1900
1901 OS << "\n case " << A.Name << ":\n";
1902
1903 if (SuperClasses.size() == 1) {
1904 OS << " return B == " << SuperClasses.back() << ";\n";
1905 continue;
1906 }
1907
1908 OS << " switch (B) {\n";
1909 OS << " default: return false;\n";
1910 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1911 OS << " case " << SuperClasses[i] << ": return true;\n";
1912 OS << " }\n";
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00001913 }
1914 OS << " }\n";
1915 OS << "}\n\n";
1916}
1917
Jim Grosbach8caecde2012-04-19 17:52:32 +00001918/// emitMatchTokenString - Emit the function to match a token string to the
Daniel Dunbar245f0582009-08-08 21:22:41 +00001919/// appropriate match class value.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001920static void emitMatchTokenString(CodeGenTarget &Target,
Daniel Dunbar245f0582009-08-08 21:22:41 +00001921 std::vector<ClassInfo*> &Infos,
1922 raw_ostream &OS) {
1923 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001924 std::vector<StringMatcher::StringPair> Matches;
Jim Grosbacha7c78222010-10-29 22:13:48 +00001925 for (std::vector<ClassInfo*>::iterator it = Infos.begin(),
Daniel Dunbar245f0582009-08-08 21:22:41 +00001926 ie = Infos.end(); it != ie; ++it) {
1927 ClassInfo &CI = **it;
1928
1929 if (CI.Kind == ClassInfo::Token)
Chris Lattner5845e5c2010-09-06 02:01:51 +00001930 Matches.push_back(StringMatcher::StringPair(CI.ValueName,
1931 "return " + CI.Name + ";"));
Daniel Dunbar245f0582009-08-08 21:22:41 +00001932 }
1933
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00001934 OS << "static MatchClassKind matchTokenString(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001935
Chris Lattner5845e5c2010-09-06 02:01:51 +00001936 StringMatcher("Name", Matches, OS).Emit();
Daniel Dunbar245f0582009-08-08 21:22:41 +00001937
1938 OS << " return InvalidMatchClass;\n";
1939 OS << "}\n\n";
1940}
Chris Lattner70add882009-08-08 20:02:57 +00001941
Jim Grosbach8caecde2012-04-19 17:52:32 +00001942/// emitMatchRegisterName - Emit the function to match a string to the target
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001943/// specific register enum.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001944static void emitMatchRegisterName(CodeGenTarget &Target, Record *AsmParser,
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001945 raw_ostream &OS) {
Daniel Dunbar245f0582009-08-08 21:22:41 +00001946 // Construct the match list.
Chris Lattner5845e5c2010-09-06 02:01:51 +00001947 std::vector<StringMatcher::StringPair> Matches;
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001948 const std::vector<CodeGenRegister*> &Regs =
1949 Target.getRegBank().getRegisters();
1950 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
1951 const CodeGenRegister *Reg = Regs[i];
1952 if (Reg->TheDef->getValueAsString("AsmName").empty())
Daniel Dunbar22be5222009-07-17 18:51:11 +00001953 continue;
1954
Chris Lattner5845e5c2010-09-06 02:01:51 +00001955 Matches.push_back(StringMatcher::StringPair(
Jakob Stoklund Olesenabdbc842011-06-18 04:26:06 +00001956 Reg->TheDef->getValueAsString("AsmName"),
1957 "return " + utostr(Reg->EnumValue) + ";"));
Daniel Dunbar22be5222009-07-17 18:51:11 +00001958 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00001959
Chris Lattnerb8d6e982010-02-09 00:34:28 +00001960 OS << "static unsigned MatchRegisterName(StringRef Name) {\n";
Daniel Dunbar245f0582009-08-08 21:22:41 +00001961
Chris Lattner5845e5c2010-09-06 02:01:51 +00001962 StringMatcher("Name", Matches, OS).Emit();
Jim Grosbacha7c78222010-10-29 22:13:48 +00001963
Daniel Dunbar245f0582009-08-08 21:22:41 +00001964 OS << " return 0;\n";
Daniel Dunbar20927f22009-08-07 08:26:05 +00001965 OS << "}\n\n";
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00001966}
Daniel Dunbara027d222009-07-31 02:32:59 +00001967
Jim Grosbach8caecde2012-04-19 17:52:32 +00001968/// emitSubtargetFeatureFlagEnumeration - Emit the subtarget feature flag
Daniel Dunbar54074b52010-07-19 05:44:09 +00001969/// definitions.
Jim Grosbach8caecde2012-04-19 17:52:32 +00001970static void emitSubtargetFeatureFlagEnumeration(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00001971 raw_ostream &OS) {
1972 OS << "// Flags for subtarget features that participate in "
1973 << "instruction matching.\n";
1974 OS << "enum SubtargetFeatureFlag {\n";
1975 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
1976 it = Info.SubtargetFeatures.begin(),
1977 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
1978 SubtargetFeatureInfo &SFI = *it->second;
Chris Lattner0aed1e72010-10-30 20:07:57 +00001979 OS << " " << SFI.getEnumName() << " = (1 << " << SFI.Index << "),\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00001980 }
1981 OS << " Feature_None = 0\n";
1982 OS << "};\n\n";
1983}
1984
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00001985/// emitOperandDiagnosticTypes - Emit the operand matching diagnostic types.
1986static void emitOperandDiagnosticTypes(AsmMatcherInfo &Info, raw_ostream &OS) {
1987 // Get the set of diagnostic types from all of the operand classes.
1988 std::set<StringRef> Types;
1989 for (std::map<Record*, ClassInfo*>::const_iterator
1990 I = Info.AsmOperandClasses.begin(),
1991 E = Info.AsmOperandClasses.end(); I != E; ++I) {
1992 if (!I->second->DiagnosticType.empty())
1993 Types.insert(I->second->DiagnosticType);
1994 }
1995
1996 if (Types.empty()) return;
1997
1998 // Now emit the enum entries.
1999 for (std::set<StringRef>::const_iterator I = Types.begin(), E = Types.end();
2000 I != E; ++I)
2001 OS << " Match_" << *I << ",\n";
2002 OS << " END_OPERAND_DIAGNOSTIC_TYPES\n";
2003}
2004
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002005/// emitGetSubtargetFeatureName - Emit the helper function to get the
2006/// user-level name for a subtarget feature.
2007static void emitGetSubtargetFeatureName(AsmMatcherInfo &Info, raw_ostream &OS) {
2008 OS << "// User-level names for subtarget features that participate in\n"
2009 << "// instruction matching.\n"
2010 << "static const char *getSubtargetFeatureName(unsigned Val) {\n"
2011 << " switch(Val) {\n";
2012 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
2013 it = Info.SubtargetFeatures.begin(),
2014 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
2015 SubtargetFeatureInfo &SFI = *it->second;
2016 // FIXME: Totally just a placeholder name to get the algorithm working.
2017 OS << " case " << SFI.getEnumName() << ": return \""
2018 << SFI.TheDef->getValueAsString("PredicateName") << "\";\n";
2019 }
2020 OS << " default: return \"(unknown)\";\n";
2021 OS << " }\n}\n\n";
2022}
2023
Jim Grosbach8caecde2012-04-19 17:52:32 +00002024/// emitComputeAvailableFeatures - Emit the function to compute the list of
Daniel Dunbar54074b52010-07-19 05:44:09 +00002025/// available features given a subtarget.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002026static void emitComputeAvailableFeatures(AsmMatcherInfo &Info,
Daniel Dunbar54074b52010-07-19 05:44:09 +00002027 raw_ostream &OS) {
2028 std::string ClassName =
2029 Info.AsmParser->getValueAsString("AsmParserClassName");
2030
Chris Lattner02bcbc92010-11-01 01:37:30 +00002031 OS << "unsigned " << Info.Target.getName() << ClassName << "::\n"
Evan Chengebdeeab2011-07-08 01:53:10 +00002032 << "ComputeAvailableFeatures(uint64_t FB) const {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002033 OS << " unsigned Features = 0;\n";
2034 for (std::map<Record*, SubtargetFeatureInfo*>::const_iterator
2035 it = Info.SubtargetFeatures.begin(),
2036 ie = Info.SubtargetFeatures.end(); it != ie; ++it) {
2037 SubtargetFeatureInfo &SFI = *it->second;
Evan Chengebdeeab2011-07-08 01:53:10 +00002038
2039 OS << " if (";
Jim Grosbach65da6fc2012-04-17 00:01:04 +00002040 std::string CondStorage =
2041 SFI.TheDef->getValueAsString("AssemblerCondString");
Evan Chengfbc38d22011-07-08 18:04:22 +00002042 StringRef Conds = CondStorage;
Evan Chengebdeeab2011-07-08 01:53:10 +00002043 std::pair<StringRef,StringRef> Comma = Conds.split(',');
2044 bool First = true;
2045 do {
2046 if (!First)
2047 OS << " && ";
2048
2049 bool Neg = false;
2050 StringRef Cond = Comma.first;
2051 if (Cond[0] == '!') {
2052 Neg = true;
2053 Cond = Cond.substr(1);
2054 }
2055
2056 OS << "((FB & " << Info.Target.getName() << "::" << Cond << ")";
2057 if (Neg)
2058 OS << " == 0";
2059 else
2060 OS << " != 0";
2061 OS << ")";
2062
2063 if (Comma.second.empty())
2064 break;
2065
2066 First = false;
2067 Comma = Comma.second.split(',');
2068 } while (true);
2069
2070 OS << ")\n";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002071 OS << " Features |= " << SFI.getEnumName() << ";\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002072 }
2073 OS << " return Features;\n";
2074 OS << "}\n\n";
2075}
2076
Chris Lattner6fa152c2010-10-30 20:15:02 +00002077static std::string GetAliasRequiredFeatures(Record *R,
2078 const AsmMatcherInfo &Info) {
Chris Lattner693173f2010-10-30 19:23:13 +00002079 std::vector<Record*> ReqFeatures = R->getValueAsListOfDefs("Predicates");
Chris Lattner693173f2010-10-30 19:23:13 +00002080 std::string Result;
2081 unsigned NumFeatures = 0;
2082 for (unsigned i = 0, e = ReqFeatures.size(); i != e; ++i) {
Chris Lattner4a74ee72010-11-01 02:09:21 +00002083 SubtargetFeatureInfo *F = Info.getSubtargetFeature(ReqFeatures[i]);
Bob Wilson828295b2011-01-26 21:26:19 +00002084
Chris Lattner4a74ee72010-11-01 02:09:21 +00002085 if (F == 0)
2086 throw TGError(R->getLoc(), "Predicate '" + ReqFeatures[i]->getName() +
2087 "' is not marked as an AssemblerPredicate!");
Bob Wilson828295b2011-01-26 21:26:19 +00002088
Chris Lattner4a74ee72010-11-01 02:09:21 +00002089 if (NumFeatures)
2090 Result += '|';
Bob Wilson828295b2011-01-26 21:26:19 +00002091
Chris Lattner4a74ee72010-11-01 02:09:21 +00002092 Result += F->getEnumName();
2093 ++NumFeatures;
Chris Lattner693173f2010-10-30 19:23:13 +00002094 }
Bob Wilson828295b2011-01-26 21:26:19 +00002095
Chris Lattner693173f2010-10-30 19:23:13 +00002096 if (NumFeatures > 1)
2097 Result = '(' + Result + ')';
2098 return Result;
2099}
2100
Jim Grosbach8caecde2012-04-19 17:52:32 +00002101/// emitMnemonicAliases - If the target has any MnemonicAlias<> definitions,
Chris Lattner7fd44892010-10-30 18:48:18 +00002102/// emit a function for them and return true, otherwise return false.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002103static bool emitMnemonicAliases(raw_ostream &OS, const AsmMatcherInfo &Info) {
Daniel Dunbarc0a70072011-01-24 23:26:31 +00002104 // Ignore aliases when match-prefix is set.
2105 if (!MatchPrefix.empty())
2106 return false;
2107
Chris Lattner674c1dc2010-10-30 17:36:36 +00002108 std::vector<Record*> Aliases =
Chris Lattner67db8832010-12-13 00:23:57 +00002109 Info.getRecords().getAllDerivedDefinitions("MnemonicAlias");
Chris Lattner7fd44892010-10-30 18:48:18 +00002110 if (Aliases.empty()) return false;
Chris Lattner674c1dc2010-10-30 17:36:36 +00002111
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002112 OS << "static void applyMnemonicAliases(StringRef &Mnemonic, "
Chris Lattner8cc0a6b2010-10-30 18:57:07 +00002113 "unsigned Features) {\n";
Bob Wilson828295b2011-01-26 21:26:19 +00002114
Chris Lattner4fd32c62010-10-30 18:56:12 +00002115 // Keep track of all the aliases from a mnemonic. Use an std::map so that the
2116 // iteration order of the map is stable.
2117 std::map<std::string, std::vector<Record*> > AliasesFromMnemonic;
Bob Wilson828295b2011-01-26 21:26:19 +00002118
Chris Lattner674c1dc2010-10-30 17:36:36 +00002119 for (unsigned i = 0, e = Aliases.size(); i != e; ++i) {
2120 Record *R = Aliases[i];
Chris Lattner4fd32c62010-10-30 18:56:12 +00002121 AliasesFromMnemonic[R->getValueAsString("FromMnemonic")].push_back(R);
Chris Lattner674c1dc2010-10-30 17:36:36 +00002122 }
Chris Lattner4fd32c62010-10-30 18:56:12 +00002123
2124 // Process each alias a "from" mnemonic at a time, building the code executed
2125 // by the string remapper.
2126 std::vector<StringMatcher::StringPair> Cases;
2127 for (std::map<std::string, std::vector<Record*> >::iterator
2128 I = AliasesFromMnemonic.begin(), E = AliasesFromMnemonic.end();
2129 I != E; ++I) {
Chris Lattner4fd32c62010-10-30 18:56:12 +00002130 const std::vector<Record*> &ToVec = I->second;
Chris Lattner693173f2010-10-30 19:23:13 +00002131
2132 // Loop through each alias and emit code that handles each case. If there
2133 // are two instructions without predicates, emit an error. If there is one,
2134 // emit it last.
2135 std::string MatchCode;
2136 int AliasWithNoPredicate = -1;
Bob Wilson828295b2011-01-26 21:26:19 +00002137
Chris Lattner693173f2010-10-30 19:23:13 +00002138 for (unsigned i = 0, e = ToVec.size(); i != e; ++i) {
2139 Record *R = ToVec[i];
Chris Lattner6fa152c2010-10-30 20:15:02 +00002140 std::string FeatureMask = GetAliasRequiredFeatures(R, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002141
Chris Lattner693173f2010-10-30 19:23:13 +00002142 // If this unconditionally matches, remember it for later and diagnose
2143 // duplicates.
2144 if (FeatureMask.empty()) {
2145 if (AliasWithNoPredicate != -1) {
2146 // We can't have two aliases from the same mnemonic with no predicate.
2147 PrintError(ToVec[AliasWithNoPredicate]->getLoc(),
2148 "two MnemonicAliases with the same 'from' mnemonic!");
Chris Lattner4164f6b2010-11-01 04:44:29 +00002149 throw TGError(R->getLoc(), "this is the other MnemonicAlias.");
Chris Lattner693173f2010-10-30 19:23:13 +00002150 }
Bob Wilson828295b2011-01-26 21:26:19 +00002151
Chris Lattner693173f2010-10-30 19:23:13 +00002152 AliasWithNoPredicate = i;
2153 continue;
2154 }
Joerg Sonnenberger6ef6ced2011-02-17 23:22:19 +00002155 if (R->getValueAsString("ToMnemonic") == I->first)
2156 throw TGError(R->getLoc(), "MnemonicAlias to the same string");
Bob Wilson828295b2011-01-26 21:26:19 +00002157
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00002158 if (!MatchCode.empty())
2159 MatchCode += "else ";
Chris Lattner693173f2010-10-30 19:23:13 +00002160 MatchCode += "if ((Features & " + FeatureMask + ") == "+FeatureMask+")\n";
2161 MatchCode += " Mnemonic = \"" +R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner4fd32c62010-10-30 18:56:12 +00002162 }
Bob Wilson828295b2011-01-26 21:26:19 +00002163
Chris Lattner693173f2010-10-30 19:23:13 +00002164 if (AliasWithNoPredicate != -1) {
2165 Record *R = ToVec[AliasWithNoPredicate];
Chris Lattner8cf8bcc2010-10-30 19:47:49 +00002166 if (!MatchCode.empty())
2167 MatchCode += "else\n ";
2168 MatchCode += "Mnemonic = \"" + R->getValueAsString("ToMnemonic")+"\";\n";
Chris Lattner693173f2010-10-30 19:23:13 +00002169 }
Bob Wilson828295b2011-01-26 21:26:19 +00002170
Chris Lattner693173f2010-10-30 19:23:13 +00002171 MatchCode += "return;";
2172
2173 Cases.push_back(std::make_pair(I->first, MatchCode));
Chris Lattner4fd32c62010-10-30 18:56:12 +00002174 }
Bob Wilson828295b2011-01-26 21:26:19 +00002175
Chris Lattner674c1dc2010-10-30 17:36:36 +00002176 StringMatcher("Mnemonic", Cases, OS).Emit();
Daniel Dunbar55b5e852011-01-18 01:59:30 +00002177 OS << "}\n\n";
Bob Wilson828295b2011-01-26 21:26:19 +00002178
Chris Lattner7fd44892010-10-30 18:48:18 +00002179 return true;
Chris Lattner674c1dc2010-10-30 17:36:36 +00002180}
2181
Jim Grosbach194f3fa2012-03-01 17:30:35 +00002182static const char *getMinimalTypeForRange(uint64_t Range) {
2183 assert(Range < 0xFFFFFFFFULL && "Enum too large");
2184 if (Range > 0xFFFF)
2185 return "uint32_t";
2186 if (Range > 0xFF)
2187 return "uint16_t";
2188 return "uint8_t";
2189}
2190
Jim Grosbach8caecde2012-04-19 17:52:32 +00002191static void emitCustomOperandParsing(raw_ostream &OS, CodeGenTarget &Target,
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002192 const AsmMatcherInfo &Info, StringRef ClassName) {
2193 // Emit the static custom operand parsing table;
2194 OS << "namespace {\n";
2195 OS << " struct OperandMatchEntry {\n";
Jakob Stoklund Olesen7044cce2012-03-15 21:22:53 +00002196 OS << " static const char *const MnemonicTable;\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002197 OS << " uint32_t OperandMask;\n";
2198 OS << " uint32_t Mnemonic;\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002199 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
Craig Topperfab3f7e2012-04-02 07:48:39 +00002200 << " RequiredFeatures;\n";
2201 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2202 << " Class;\n\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002203 OS << " StringRef getMnemonic() const {\n";
2204 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
2205 OS << " MnemonicTable[Mnemonic]);\n";
2206 OS << " }\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002207 OS << " };\n\n";
2208
2209 OS << " // Predicate for searching for an opcode.\n";
2210 OS << " struct LessOpcodeOperand {\n";
2211 OS << " bool operator()(const OperandMatchEntry &LHS, StringRef RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002212 OS << " return LHS.getMnemonic() < RHS;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002213 OS << " }\n";
2214 OS << " bool operator()(StringRef LHS, const OperandMatchEntry &RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002215 OS << " return LHS < RHS.getMnemonic();\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002216 OS << " }\n";
2217 OS << " bool operator()(const OperandMatchEntry &LHS,";
2218 OS << " const OperandMatchEntry &RHS) {\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002219 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002220 OS << " }\n";
2221 OS << " };\n";
2222
2223 OS << "} // end anonymous namespace.\n\n";
2224
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002225 StringToOffsetTable StringTable;
2226
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002227 OS << "static const OperandMatchEntry OperandMatchTable["
2228 << Info.OperandMatchInfo.size() << "] = {\n";
2229
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002230 OS << " /* Operand List Mask, Mnemonic, Operand Class, Features */\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002231 for (std::vector<OperandMatchEntry>::const_iterator it =
2232 Info.OperandMatchInfo.begin(), ie = Info.OperandMatchInfo.end();
2233 it != ie; ++it) {
2234 const OperandMatchEntry &OMI = *it;
2235 const MatchableInfo &II = *OMI.MI;
2236
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002237 OS << " { " << OMI.OperandMask;
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002238
2239 OS << " /* ";
2240 bool printComma = false;
2241 for (int i = 0, e = 31; i !=e; ++i)
2242 if (OMI.OperandMask & (1 << i)) {
2243 if (printComma)
2244 OS << ", ";
2245 OS << i;
2246 printComma = true;
2247 }
2248 OS << " */";
2249
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002250 // Store a pascal-style length byte in the mnemonic.
2251 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
Jakob Stoklund Olesenbcfa9822012-03-15 18:05:57 +00002252 OS << ", " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
Craig Topperfab3f7e2012-04-02 07:48:39 +00002253 << " /* " << II.Mnemonic << " */, ";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002254
2255 // Write the required features mask.
2256 if (!II.RequiredFeatures.empty()) {
2257 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2258 if (i) OS << "|";
2259 OS << II.RequiredFeatures[i]->getEnumName();
2260 }
2261 } else
2262 OS << "0";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002263
2264 OS << ", " << OMI.CI->Name;
2265
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002266 OS << " },\n";
2267 }
2268 OS << "};\n\n";
2269
Jakob Stoklund Olesen7044cce2012-03-15 21:22:53 +00002270 OS << "const char *const OperandMatchEntry::MnemonicTable =\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002271 StringTable.EmitString(OS);
2272 OS << ";\n\n";
2273
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002274 // Emit the operand class switch to call the correct custom parser for
2275 // the found operand class.
Jim Grosbachf922c472011-02-12 01:34:40 +00002276 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
2277 << Target.getName() << ClassName << "::\n"
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002278 << "tryCustomParseOperand(SmallVectorImpl<MCParsedAsmOperand*>"
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002279 << " &Operands,\n unsigned MCK) {\n\n"
2280 << " switch(MCK) {\n";
2281
2282 for (std::vector<ClassInfo*>::const_iterator it = Info.Classes.begin(),
2283 ie = Info.Classes.end(); it != ie; ++it) {
2284 ClassInfo *CI = *it;
2285 if (CI->ParserMethod.empty())
2286 continue;
2287 OS << " case " << CI->Name << ":\n"
2288 << " return " << CI->ParserMethod << "(Operands);\n";
2289 }
2290
2291 OS << " default:\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002292 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002293 OS << " }\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002294 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002295 OS << "}\n\n";
2296
2297 // Emit the static custom operand parser. This code is very similar with
2298 // the other matcher. Also use MatchResultTy here just in case we go for
2299 // a better error handling.
Jim Grosbachf922c472011-02-12 01:34:40 +00002300 OS << Target.getName() << ClassName << "::OperandMatchResultTy "
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002301 << Target.getName() << ClassName << "::\n"
2302 << "MatchOperandParserImpl(SmallVectorImpl<MCParsedAsmOperand*>"
2303 << " &Operands,\n StringRef Mnemonic) {\n";
2304
2305 // Emit code to get the available features.
2306 OS << " // Get the current feature set.\n";
2307 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2308
2309 OS << " // Get the next operand index.\n";
2310 OS << " unsigned NextOpNum = Operands.size()-1;\n";
2311
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002312 // Emit code to search the table.
2313 OS << " // Search the table.\n";
2314 OS << " std::pair<const OperandMatchEntry*, const OperandMatchEntry*>";
2315 OS << " MnemonicRange =\n";
2316 OS << " std::equal_range(OperandMatchTable, OperandMatchTable+"
2317 << Info.OperandMatchInfo.size() << ", Mnemonic,\n"
2318 << " LessOpcodeOperand());\n\n";
2319
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002320 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002321 OS << " return MatchOperand_NoMatch;\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002322
2323 OS << " for (const OperandMatchEntry *it = MnemonicRange.first,\n"
2324 << " *ie = MnemonicRange.second; it != ie; ++it) {\n";
2325
2326 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Benjamin Kramerb08bb342012-03-03 20:44:43 +00002327 OS << " assert(Mnemonic == it->getMnemonic());\n\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002328
2329 // Emit check that the required features are available.
2330 OS << " // check if the available features match\n";
2331 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2332 << "!= it->RequiredFeatures) {\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002333 OS << " continue;\n";
2334 OS << " }\n\n";
2335
2336 // Emit check to ensure the operand number matches.
2337 OS << " // check if the operand in question has a custom parser.\n";
2338 OS << " if (!(it->OperandMask & (1 << NextOpNum)))\n";
2339 OS << " continue;\n\n";
2340
2341 // Emit call to the custom parser method
2342 OS << " // call custom parse method to handle the operand\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002343 OS << " OperandMatchResultTy Result = ";
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002344 OS << "tryCustomParseOperand(Operands, it->Class);\n";
Jim Grosbachf922c472011-02-12 01:34:40 +00002345 OS << " if (Result != MatchOperand_NoMatch)\n";
2346 OS << " return Result;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002347 OS << " }\n\n";
2348
Jim Grosbachf922c472011-02-12 01:34:40 +00002349 OS << " // Okay, we had no match.\n";
2350 OS << " return MatchOperand_NoMatch;\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002351 OS << "}\n\n";
2352}
2353
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002354void AsmMatcherEmitter::run(raw_ostream &OS) {
Chris Lattner67db8832010-12-13 00:23:57 +00002355 CodeGenTarget Target(Records);
Daniel Dunbar2234e5e2009-08-07 21:01:44 +00002356 Record *AsmParser = Target.getAsmParser();
2357 std::string ClassName = AsmParser->getValueAsString("AsmParserClassName");
2358
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002359 // Compute the information on the instructions to match.
Chris Lattner67db8832010-12-13 00:23:57 +00002360 AsmMatcherInfo Info(AsmParser, Target, Records);
Jim Grosbach8caecde2012-04-19 17:52:32 +00002361 Info.buildInfo();
Daniel Dunbara027d222009-07-31 02:32:59 +00002362
Daniel Dunbare1f6de32010-02-02 23:46:36 +00002363 // Sort the instruction table using the partial order on classes. We use
2364 // stable_sort to ensure that ambiguous instructions are still
2365 // deterministically ordered.
Chris Lattner22bc5c42010-11-01 05:06:45 +00002366 std::stable_sort(Info.Matchables.begin(), Info.Matchables.end(),
2367 less_ptr<MatchableInfo>());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002368
Daniel Dunbarb7479c02009-08-08 05:24:34 +00002369 DEBUG_WITH_TYPE("instruction_info", {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002370 for (std::vector<MatchableInfo*>::iterator
2371 it = Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002372 it != ie; ++it)
Daniel Dunbar20927f22009-08-07 08:26:05 +00002373 (*it)->dump();
2374 });
Daniel Dunbara027d222009-07-31 02:32:59 +00002375
Chris Lattner22bc5c42010-11-01 05:06:45 +00002376 // Check for ambiguous matchables.
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002377 DEBUG_WITH_TYPE("ambiguous_instrs", {
2378 unsigned NumAmbiguous = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002379 for (unsigned i = 0, e = Info.Matchables.size(); i != e; ++i) {
Chris Lattner87410362010-09-06 20:21:47 +00002380 for (unsigned j = i + 1; j != e; ++j) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002381 MatchableInfo &A = *Info.Matchables[i];
2382 MatchableInfo &B = *Info.Matchables[j];
Jim Grosbacha7c78222010-10-29 22:13:48 +00002383
Jim Grosbach8caecde2012-04-19 17:52:32 +00002384 if (A.couldMatchAmbiguouslyWith(B)) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002385 errs() << "warning: ambiguous matchables:\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002386 A.dump();
2387 errs() << "\nis incomparable with:\n";
2388 B.dump();
2389 errs() << "\n\n";
Chris Lattner87410362010-09-06 20:21:47 +00002390 ++NumAmbiguous;
2391 }
Daniel Dunbar2b544812009-08-09 06:05:33 +00002392 }
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002393 }
Chris Lattner87410362010-09-06 20:21:47 +00002394 if (NumAmbiguous)
Jim Grosbacha7c78222010-10-29 22:13:48 +00002395 errs() << "warning: " << NumAmbiguous
Chris Lattner22bc5c42010-11-01 05:06:45 +00002396 << " ambiguous matchables!\n";
Chris Lattnerfa0d74d2010-09-06 21:28:52 +00002397 });
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002398
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002399 // Compute the information on the custom operand parsing.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002400 Info.buildOperandMatchInfo();
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002401
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002402 // Write the output.
2403
Chris Lattner0692ee62010-09-06 19:11:01 +00002404 // Information for the class declaration.
2405 OS << "\n#ifdef GET_ASSEMBLER_HEADER\n";
2406 OS << "#undef GET_ASSEMBLER_HEADER\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002407 OS << " // This should be included into the middle of the declaration of\n";
Evan Cheng94b95502011-07-26 00:24:13 +00002408 OS << " // your subclasses implementation of MCTargetAsmParser.\n";
Evan Chengebdeeab2011-07-08 01:53:10 +00002409 OS << " unsigned ComputeAvailableFeatures(uint64_t FeatureBits) const;\n";
Daniel Dunbar5c228a92011-02-04 23:17:40 +00002410 OS << " bool ConvertToMCInst(unsigned Kind, MCInst &Inst, "
2411 << "unsigned Opcode,\n"
2412 << " const SmallVectorImpl<MCParsedAsmOperand*> "
2413 << "&Operands);\n";
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002414 OS << " bool MnemonicIsValid(StringRef Mnemonic);\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002415 OS << " unsigned MatchInstructionImpl(\n";
Daniel Dunbar083203d2011-01-10 15:26:11 +00002416 OS << " const SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
Devang Patel56315d32012-01-10 17:50:43 +00002417 OS << " MCInst &Inst, unsigned &ErrorInfo, unsigned VariantID = 0);\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002418
2419 if (Info.OperandMatchInfo.size()) {
Jim Grosbachf922c472011-02-12 01:34:40 +00002420 OS << "\n enum OperandMatchResultTy {\n";
2421 OS << " MatchOperand_Success, // operand matched successfully\n";
2422 OS << " MatchOperand_NoMatch, // operand did not match\n";
2423 OS << " MatchOperand_ParseFail // operand matched but had errors\n";
2424 OS << " };\n";
2425 OS << " OperandMatchResultTy MatchOperandParserImpl(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002426 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2427 OS << " StringRef Mnemonic);\n";
2428
Jim Grosbach3d5d8f62011-12-06 22:07:02 +00002429 OS << " OperandMatchResultTy tryCustomParseOperand(\n";
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002430 OS << " SmallVectorImpl<MCParsedAsmOperand*> &Operands,\n";
2431 OS << " unsigned MCK);\n\n";
2432 }
2433
Chris Lattner0692ee62010-09-06 19:11:01 +00002434 OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
2435
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002436 // Emit the operand match diagnostic enum names.
2437 OS << "\n#ifdef GET_OPERAND_DIAGNOSTIC_TYPES\n";
2438 OS << "#undef GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2439 emitOperandDiagnosticTypes(Info, OS);
2440 OS << "#endif // GET_OPERAND_DIAGNOSTIC_TYPES\n\n";
2441
2442
Chris Lattner0692ee62010-09-06 19:11:01 +00002443 OS << "\n#ifdef GET_REGISTER_MATCHER\n";
2444 OS << "#undef GET_REGISTER_MATCHER\n\n";
2445
Daniel Dunbar54074b52010-07-19 05:44:09 +00002446 // Emit the subtarget feature enumeration.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002447 emitSubtargetFeatureFlagEnumeration(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002448
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002449 // Emit the function to match a register name to number.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002450 emitMatchRegisterName(Target, AsmParser, OS);
Chris Lattner0692ee62010-09-06 19:11:01 +00002451
2452 OS << "#endif // GET_REGISTER_MATCHER\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002453
Craig Topper8030e1a2012-04-25 06:56:34 +00002454 OS << "\n#ifdef GET_SUBTARGET_FEATURE_NAME\n";
2455 OS << "#undef GET_SUBTARGET_FEATURE_NAME\n\n";
Daniel Dunbar1095f2a2009-08-11 23:23:44 +00002456
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002457 // Generate the helper function to get the names for subtarget features.
2458 emitGetSubtargetFeatureName(Info, OS);
2459
Craig Topper8030e1a2012-04-25 06:56:34 +00002460 OS << "#endif // GET_SUBTARGET_FEATURE_NAME\n\n";
2461
2462 OS << "\n#ifdef GET_MATCHER_IMPLEMENTATION\n";
2463 OS << "#undef GET_MATCHER_IMPLEMENTATION\n\n";
2464
Chris Lattner7fd44892010-10-30 18:48:18 +00002465 // Generate the function that remaps for mnemonic aliases.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002466 bool HasMnemonicAliases = emitMnemonicAliases(OS, Info);
Bob Wilson828295b2011-01-26 21:26:19 +00002467
Daniel Dunbar606e8ad2009-08-09 04:00:06 +00002468 // Generate the unified function to convert operands into an MCInst.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002469 emitConvertToMCInst(Target, ClassName, Info.Matchables, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002470
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002471 // Emit the enumeration for classes which participate in matching.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002472 emitMatchClassEnumeration(Target, Info.Classes, OS);
Daniel Dunbara027d222009-07-31 02:32:59 +00002473
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002474 // Emit the routine to match token strings to their match class.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002475 emitMatchTokenString(Target, Info.Classes, OS);
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002476
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002477 // Emit the subclass predicate routine.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002478 emitIsSubclass(Target, Info.Classes, OS);
Daniel Dunbarfdb1f492009-08-10 16:05:47 +00002479
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002480 // Emit the routine to validate an operand against a match class.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002481 emitValidateOperandClass(Info, OS);
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002482
Daniel Dunbar54074b52010-07-19 05:44:09 +00002483 // Emit the available features compute function.
Jim Grosbach8caecde2012-04-19 17:52:32 +00002484 emitComputeAvailableFeatures(Info, OS);
Daniel Dunbar54074b52010-07-19 05:44:09 +00002485
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002486
2487 size_t MaxNumOperands = 0;
Chris Lattner22bc5c42010-11-01 05:06:45 +00002488 for (std::vector<MatchableInfo*>::const_iterator it =
2489 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002490 it != ie; ++it)
Chris Lattner3116fef2010-11-02 01:03:43 +00002491 MaxNumOperands = std::max(MaxNumOperands, (*it)->AsmOperands.size());
Jim Grosbacha7c78222010-10-29 22:13:48 +00002492
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002493 // Emit the static match table; unused classes get initalized to 0 which is
2494 // guaranteed to be InvalidMatchClass.
2495 //
2496 // FIXME: We can reduce the size of this table very easily. First, we change
2497 // it so that store the kinds in separate bit-fields for each index, which
2498 // only needs to be the max width used for classes at that index (we also need
2499 // to reject based on this during classification). If we then make sure to
2500 // order the match kinds appropriately (putting mnemonics last), then we
2501 // should only end up using a few bits for each class, especially the ones
2502 // following the mnemonic.
Chris Lattner96352e52010-09-06 21:08:38 +00002503 OS << "namespace {\n";
2504 OS << " struct MatchEntry {\n";
Jakob Stoklund Olesen7044cce2012-03-15 21:22:53 +00002505 OS << " static const char *const MnemonicTable;\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002506 OS << " uint32_t Mnemonic;\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002507 OS << " uint16_t Opcode;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002508 OS << " " << getMinimalTypeForRange(Info.Matchables.size())
2509 << " ConvertFn;\n";
Benjamin Krameraf482cf2011-10-17 16:18:09 +00002510 OS << " " << getMinimalTypeForRange(1ULL << Info.SubtargetFeatures.size())
2511 << " RequiredFeatures;\n";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002512 OS << " " << getMinimalTypeForRange(Info.Classes.size())
2513 << " Classes[" << MaxNumOperands << "];\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002514 OS << " uint8_t AsmVariantID;\n\n";
2515 OS << " StringRef getMnemonic() const {\n";
2516 OS << " return StringRef(MnemonicTable + Mnemonic + 1,\n";
2517 OS << " MnemonicTable[Mnemonic]);\n";
2518 OS << " }\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002519 OS << " };\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002520
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002521 OS << " // Predicate for searching for an opcode.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002522 OS << " struct LessOpcode {\n";
2523 OS << " bool operator()(const MatchEntry &LHS, StringRef RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002524 OS << " return LHS.getMnemonic() < RHS;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002525 OS << " }\n";
2526 OS << " bool operator()(StringRef LHS, const MatchEntry &RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002527 OS << " return LHS < RHS.getMnemonic();\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002528 OS << " }\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002529 OS << " bool operator()(const MatchEntry &LHS, const MatchEntry &RHS) {\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002530 OS << " return LHS.getMnemonic() < RHS.getMnemonic();\n";
Chris Lattner32c685c2010-09-07 06:10:48 +00002531 OS << " }\n";
Chris Lattner96352e52010-09-06 21:08:38 +00002532 OS << " };\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002533
Chris Lattner96352e52010-09-06 21:08:38 +00002534 OS << "} // end anonymous namespace.\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002535
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002536 StringToOffsetTable StringTable;
2537
Chris Lattner96352e52010-09-06 21:08:38 +00002538 OS << "static const MatchEntry MatchTable["
Chris Lattner22bc5c42010-11-01 05:06:45 +00002539 << Info.Matchables.size() << "] = {\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002540
Chris Lattner22bc5c42010-11-01 05:06:45 +00002541 for (std::vector<MatchableInfo*>::const_iterator it =
2542 Info.Matchables.begin(), ie = Info.Matchables.end();
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002543 it != ie; ++it) {
Chris Lattner22bc5c42010-11-01 05:06:45 +00002544 MatchableInfo &II = **it;
Jim Grosbacha7c78222010-10-29 22:13:48 +00002545
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002546 // Store a pascal-style length byte in the mnemonic.
2547 std::string LenMnemonic = char(II.Mnemonic.size()) + II.Mnemonic.str();
Craig Topperfab3f7e2012-04-02 07:48:39 +00002548 OS << " { " << StringTable.GetOrAddStringOffset(LenMnemonic, false)
2549 << " /* " << II.Mnemonic << " */, "
2550 << Target.getName() << "::"
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002551 << II.getResultInst()->TheDef->getName() << ", "
Craig Topperfab3f7e2012-04-02 07:48:39 +00002552 << II.ConversionFnKind << ", ";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002553
Daniel Dunbar54074b52010-07-19 05:44:09 +00002554 // Write the required features mask.
2555 if (!II.RequiredFeatures.empty()) {
2556 for (unsigned i = 0, e = II.RequiredFeatures.size(); i != e; ++i) {
2557 if (i) OS << "|";
Chris Lattner0aed1e72010-10-30 20:07:57 +00002558 OS << II.RequiredFeatures[i]->getEnumName();
Daniel Dunbar54074b52010-07-19 05:44:09 +00002559 }
2560 } else
2561 OS << "0";
Craig Topperfab3f7e2012-04-02 07:48:39 +00002562
2563 OS << ", { ";
2564 for (unsigned i = 0, e = II.AsmOperands.size(); i != e; ++i) {
2565 MatchableInfo::AsmOperand &Op = II.AsmOperands[i];
2566
2567 if (i) OS << ", ";
2568 OS << Op.Class->Name;
2569 }
2570 OS << " }, " << II.AsmVariantID;
Daniel Dunbar54074b52010-07-19 05:44:09 +00002571 OS << "},\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002572 }
Jim Grosbacha7c78222010-10-29 22:13:48 +00002573
Chris Lattner96352e52010-09-06 21:08:38 +00002574 OS << "};\n\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002575
Jakob Stoklund Olesen7044cce2012-03-15 21:22:53 +00002576 OS << "const char *const MatchEntry::MnemonicTable =\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002577 StringTable.EmitString(OS);
2578 OS << ";\n\n";
2579
Bob Wilson1fe3aa12011-01-26 21:43:46 +00002580 // A method to determine if a mnemonic is in the list.
2581 OS << "bool " << Target.getName() << ClassName << "::\n"
2582 << "MnemonicIsValid(StringRef Mnemonic) {\n";
2583 OS << " // Search the table.\n";
2584 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2585 OS << " std::equal_range(MatchTable, MatchTable+"
2586 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n";
2587 OS << " return MnemonicRange.first != MnemonicRange.second;\n";
2588 OS << "}\n\n";
2589
Chris Lattner96352e52010-09-06 21:08:38 +00002590 // Finally, build the match function.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002591 OS << "unsigned "
Chris Lattner96352e52010-09-06 21:08:38 +00002592 << Target.getName() << ClassName << "::\n"
2593 << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
2594 << " &Operands,\n";
Jim Grosbach14ce6fa2012-04-24 22:40:08 +00002595 OS << " MCInst &Inst, unsigned &ErrorInfo, ";
2596 OS << "unsigned VariantID) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002597
2598 // Emit code to get the available features.
2599 OS << " // Get the current feature set.\n";
2600 OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
2601
Chris Lattner674c1dc2010-10-30 17:36:36 +00002602 OS << " // Get the instruction mnemonic, which is the first token.\n";
2603 OS << " StringRef Mnemonic = ((" << Target.getName()
2604 << "Operand*)Operands[0])->getToken();\n\n";
2605
Chris Lattner7fd44892010-10-30 18:48:18 +00002606 if (HasMnemonicAliases) {
2607 OS << " // Process all MnemonicAliases to remap the mnemonic.\n";
Devang Patel40bced02012-01-17 18:30:45 +00002608 OS << " // FIXME : Add an entry in AsmParserVariant to check this.\n";
2609 OS << " if (!VariantID)\n";
2610 OS << " applyMnemonicAliases(Mnemonic, AvailableFeatures);\n\n";
Chris Lattner7fd44892010-10-30 18:48:18 +00002611 }
Bob Wilson828295b2011-01-26 21:26:19 +00002612
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002613 // Emit code to compute the class list for this operand vector.
2614 OS << " // Eliminate obvious mismatches.\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002615 OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
2616 OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
2617 OS << " return Match_InvalidOperand;\n";
2618 OS << " }\n\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002619
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002620 OS << " // Some state to try to produce better error messages.\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002621 OS << " bool HadMatchOtherThanFeatures = false;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002622 OS << " bool HadMatchOtherThanPredicate = false;\n";
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002623 OS << " unsigned RetCode = Match_InvalidOperand;\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00002624 OS << " unsigned MissingFeatures = ~0U;\n";
Jim Grosbach84cb0332011-02-11 21:31:55 +00002625 OS << " // Set ErrorInfo to the operand that mismatches if it is\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002626 OS << " // wrong for all instances of the instruction.\n";
2627 OS << " ErrorInfo = ~0U;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002628
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002629 // Emit code to search the table.
2630 OS << " // Search the table.\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002631 OS << " std::pair<const MatchEntry*, const MatchEntry*> MnemonicRange =\n";
2632 OS << " std::equal_range(MatchTable, MatchTable+"
Chris Lattner22bc5c42010-11-01 05:06:45 +00002633 << Info.Matchables.size() << ", Mnemonic, LessOpcode());\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002634
Chris Lattnera008e8a2010-09-06 21:54:15 +00002635 OS << " // Return a more specific error code if no mnemonics match.\n";
2636 OS << " if (MnemonicRange.first == MnemonicRange.second)\n";
2637 OS << " return Match_MnemonicFail;\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002638
Chris Lattner2b1f9432010-09-06 21:22:45 +00002639 OS << " for (const MatchEntry *it = MnemonicRange.first, "
Chris Lattner80db4e52010-09-06 21:23:43 +00002640 << "*ie = MnemonicRange.second;\n";
Chris Lattner2b1f9432010-09-06 21:22:45 +00002641 OS << " it != ie; ++it) {\n";
Daniel Dunbar54074b52010-07-19 05:44:09 +00002642
Gabor Greife53ee3b2010-09-07 06:06:06 +00002643 OS << " // equal_range guarantees that instruction mnemonic matches.\n";
Benjamin Kramera4c5ecf2012-03-03 19:13:26 +00002644 OS << " assert(Mnemonic == it->getMnemonic());\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002645
Daniel Dunbar54074b52010-07-19 05:44:09 +00002646 // Emit check that the subclasses match.
Devang Patel56315d32012-01-10 17:50:43 +00002647 OS << " if (VariantID != it->AsmVariantID) continue;\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002648 OS << " bool OperandsValid = true;\n";
2649 OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002650 OS << " if (i + 1 >= Operands.size()) {\n";
2651 OS << " OperandsValid = (it->Classes[i] == " <<"InvalidMatchClass);\n";
Jim Grosbach151d81d2012-07-12 21:37:20 +00002652 OS << " if (!OperandsValid) ErrorInfo = i + 1;\n;";
Jim Grosbachb9d5af02011-05-03 19:09:56 +00002653 OS << " break;\n";
Jim Grosbachb9db0c52011-02-10 00:08:28 +00002654 OS << " }\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002655 OS << " unsigned Diag = validateOperandClass(Operands[i+1],\n";
2656 OS.indent(43);
2657 OS << "(MatchClassKind)it->Classes[i]);\n";
2658 OS << " if (Diag == Match_Success)\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002659 OS << " continue;\n";
Chris Lattner9bb9fa12010-09-06 23:37:39 +00002660 OS << " // If this operand is broken for all of the instances of this\n";
2661 OS << " // mnemonic, keep track of it so we can report loc info.\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002662 OS << " // If we already had a match that only failed due to a\n";
2663 OS << " // target predicate, that diagnostic is preferred.\n";
2664 OS << " if (!HadMatchOtherThanPredicate &&\n";
2665 OS << " (it == MnemonicRange.first || ErrorInfo <= i+1)) {\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002666 OS << " ErrorInfo = i+1;\n";
Jim Grosbachef970c12012-06-26 22:58:01 +00002667 OS << " // InvalidOperand is the default. Prefer specificity.\n";
2668 OS << " if (Diag != Match_InvalidOperand)\n";
2669 OS << " RetCode = Diag;\n";
Jim Grosbach4dbfdfb2012-06-22 23:56:44 +00002670 OS << " }\n";
Chris Lattnerce4a3352010-09-06 22:11:18 +00002671 OS << " // Otherwise, just reject this instance of the mnemonic.\n";
2672 OS << " OperandsValid = false;\n";
2673 OS << " break;\n";
2674 OS << " }\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002675
Chris Lattnerce4a3352010-09-06 22:11:18 +00002676 OS << " if (!OperandsValid) continue;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002677
2678 // Emit check that the required features are available.
2679 OS << " if ((AvailableFeatures & it->RequiredFeatures) "
2680 << "!= it->RequiredFeatures) {\n";
2681 OS << " HadMatchOtherThanFeatures = true;\n";
Jim Grosbach325bd662012-06-18 19:45:46 +00002682 OS << " unsigned NewMissingFeatures = it->RequiredFeatures & "
2683 "~AvailableFeatures;\n";
2684 OS << " if (CountPopulation_32(NewMissingFeatures) <= "
2685 "CountPopulation_32(MissingFeatures))\n";
2686 OS << " MissingFeatures = NewMissingFeatures;\n";
Chris Lattnerec6789f2010-09-06 20:08:02 +00002687 OS << " continue;\n";
2688 OS << " }\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002689 OS << "\n";
Daniel Dunbarb4129152011-02-04 17:12:23 +00002690 OS << " // We have selected a definite instruction, convert the parsed\n"
2691 << " // operands into the appropriate MCInst.\n";
2692 OS << " if (!ConvertToMCInst(it->ConvertFn, Inst,\n"
2693 << " it->Opcode, Operands))\n";
2694 OS << " return Match_ConversionFail;\n";
2695 OS << "\n";
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002696
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002697 // Verify the instruction with the target-specific match predicate function.
2698 OS << " // We have a potential match. Check the target predicate to\n"
2699 << " // handle any context sensitive constraints.\n"
2700 << " unsigned MatchResult;\n"
2701 << " if ((MatchResult = checkTargetMatchPredicate(Inst)) !="
2702 << " Match_Success) {\n"
2703 << " Inst.clear();\n"
2704 << " RetCode = MatchResult;\n"
Jim Grosbach578071a2011-08-16 20:12:35 +00002705 << " HadMatchOtherThanPredicate = true;\n"
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002706 << " continue;\n"
2707 << " }\n\n";
2708
Daniel Dunbar8cc9c0c2010-03-18 20:05:56 +00002709 // Call the post-processing function, if used.
2710 std::string InsnCleanupFn =
2711 AsmParser->getValueAsString("AsmParserInstCleanup");
2712 if (!InsnCleanupFn.empty())
2713 OS << " " << InsnCleanupFn << "(Inst);\n";
2714
Chris Lattner79ed3f72010-09-06 19:22:17 +00002715 OS << " return Match_Success;\n";
Daniel Dunbara3741fa2009-08-08 07:50:56 +00002716 OS << " }\n\n";
2717
Chris Lattnerec6789f2010-09-06 20:08:02 +00002718 OS << " // Okay, we had no match. Try to return a useful error code.\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002719 OS << " if (HadMatchOtherThanPredicate || !HadMatchOtherThanFeatures)";
Jim Grosbach325bd662012-06-18 19:45:46 +00002720 OS << " return RetCode;\n";
2721 OS << " // Missing feature matches return which features were missing\n";
2722 OS << " ErrorInfo = MissingFeatures;\n";
Jim Grosbach578071a2011-08-16 20:12:35 +00002723 OS << " return Match_MissingFeature;\n";
Daniel Dunbara027d222009-07-31 02:32:59 +00002724 OS << "}\n\n";
Jim Grosbacha7c78222010-10-29 22:13:48 +00002725
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002726 if (Info.OperandMatchInfo.size())
Jim Grosbach8caecde2012-04-19 17:52:32 +00002727 emitCustomOperandParsing(OS, Target, Info, ClassName);
Bruno Cardoso Lopese7a54522011-02-07 19:38:32 +00002728
Chris Lattner0692ee62010-09-06 19:11:01 +00002729 OS << "#endif // GET_MATCHER_IMPLEMENTATION\n\n";
Daniel Dunbard51ffcf2009-07-11 19:39:44 +00002730}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +00002731
2732namespace llvm {
2733
2734void EmitAsmMatcher(RecordKeeper &RK, raw_ostream &OS) {
2735 emitSourceFileHeader("Assembly Matcher Source Fragment", OS);
2736 AsmMatcherEmitter(RK).run(OS);
2737}
2738
2739} // End llvm namespace