blob: d99632e9e4ea515f7f72af8ecd9e4ae1d6962264 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000029 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 Record *Rec;
31 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000033
Chris Lattnerf4601652007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greenede444af2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000039 MultiClass *MC;
40 std::vector<Init*> TemplateArgs;
41 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000042
David Greenede444af2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000044 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000045};
David Greened34a73b2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000048 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000049
David Greened34a73b2009-04-24 16:55:41 +000050 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000051
Daniel Dunbar1a551802009-07-03 00:10:29 +000052 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000053 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
54 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattnerf4601652007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattner1e3a8a42009-06-21 03:39:35 +000063bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000066
Chris Lattnerf4601652007-11-22 20:49:04 +000067 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
68 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000072 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000073 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
Bob Wilson21870412009-11-22 04:24:42 +000082bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000083 const std::vector<unsigned> &BitList, Init *V) {
84 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
90 return Error(Loc, "Value '" + ValName + "' unknown!");
91
92 // Do not allow assignments like 'X = X'. This will just cause infinite loops
93 // in the resolution machinery.
94 if (BitList.empty())
95 if (VarInit *VI = dynamic_cast<VarInit*>(V))
96 if (VI->getName() == ValName)
97 return false;
Bob Wilson21870412009-11-22 04:24:42 +000098
Chris Lattnerf4601652007-11-22 20:49:04 +000099 // If we are assigning to a subset of the bits in the value... then we must be
100 // assigning to a field of BitsRecTy, which must have a BitsInit
101 // initializer.
102 //
103 if (!BitList.empty()) {
104 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
105 if (CurVal == 0)
106 return Error(Loc, "Value '" + ValName + "' is not a bits type");
107
108 // Convert the incoming value to a bits type of the appropriate size...
109 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
110 if (BI == 0) {
111 V->convertInitializerTo(new BitsRecTy(BitList.size()));
112 return Error(Loc, "Initializer is not compatible with bit range");
113 }
Bob Wilson21870412009-11-22 04:24:42 +0000114
Chris Lattnerf4601652007-11-22 20:49:04 +0000115 // We should have a BitsInit type now.
116 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
117 assert(BInit != 0);
118
119 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
120
121 // Loop over bits, assigning values as appropriate.
122 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
123 unsigned Bit = BitList[i];
124 if (NewVal->getBit(Bit))
125 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
126 ValName + "' more than once");
127 NewVal->setBit(Bit, BInit->getBit(i));
128 }
129
130 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
131 if (NewVal->getBit(i) == 0)
132 NewVal->setBit(i, CurVal->getBit(i));
133
134 V = NewVal;
135 }
136
137 if (RV->setValue(V))
Bob Wilson21870412009-11-22 04:24:42 +0000138 return Error(Loc, "Value '" + ValName + "' of type '" +
139 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000140 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000141 return false;
142}
143
144/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
145/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000146bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000147 Record *SC = SubClass.Rec;
148 // Add all of the values in the subclass into the current class.
149 const std::vector<RecordVal> &Vals = SC->getValues();
150 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
151 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
152 return true;
153
154 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
155
156 // Ensure that an appropriate number of template arguments are specified.
157 if (TArgs.size() < SubClass.TemplateArgs.size())
158 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000159
Chris Lattnerf4601652007-11-22 20:49:04 +0000160 // Loop over all of the template arguments, setting them to the specified
161 // value or leaving them as the default if necessary.
162 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
163 if (i < SubClass.TemplateArgs.size()) {
164 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000165 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000166 SubClass.TemplateArgs[i]))
167 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000168
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 // Resolve it next.
170 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000171
Chris Lattnerf4601652007-11-22 20:49:04 +0000172 // Now remove it.
173 CurRec->removeValue(TArgs[i]);
174
175 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
176 return Error(SubClass.RefLoc,"Value not specified for template argument #"
Bob Wilson21870412009-11-22 04:24:42 +0000177 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000178 SC->getName() + "'!");
179 }
180 }
181
182 // Since everything went well, we can now set the "superclass" list for the
183 // current record.
184 const std::vector<Record*> &SCs = SC->getSuperClasses();
185 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
186 if (CurRec->isSubClassOf(SCs[i]))
187 return Error(SubClass.RefLoc,
188 "Already subclass of '" + SCs[i]->getName() + "'!\n");
189 CurRec->addSuperClass(SCs[i]);
190 }
Bob Wilson21870412009-11-22 04:24:42 +0000191
Chris Lattnerf4601652007-11-22 20:49:04 +0000192 if (CurRec->isSubClassOf(SC))
193 return Error(SubClass.RefLoc,
194 "Already subclass of '" + SC->getName() + "'!\n");
195 CurRec->addSuperClass(SC);
196 return false;
197}
198
David Greenede444af2009-04-22 16:42:54 +0000199/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000200/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000201/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000202bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000203 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000204 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000205 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000206
Bob Wilson440548d2009-04-30 18:26:19 +0000207 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000208
209 // Add all of the values in the subclass into the current class.
210 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
211 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
212 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
213 return true;
214
Bob Wilson440548d2009-04-30 18:26:19 +0000215 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000216
David Greenede444af2009-04-22 16:42:54 +0000217 // Add all of the defs in the subclass into the current multiclass.
218 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
219 iend = SMC->DefPrototypes.end();
220 i != iend;
221 ++i) {
222 // Clone the def and add it to the current multiclass
223 Record *NewDef = new Record(**i);
224
225 // Add all of the values in the superclass into the current def.
226 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
227 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
228 return true;
229
Bob Wilson440548d2009-04-30 18:26:19 +0000230 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000231 }
Bob Wilson32558652009-04-28 19:41:44 +0000232
David Greenede444af2009-04-22 16:42:54 +0000233 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
234
David Greened34a73b2009-04-24 16:55:41 +0000235 // Ensure that an appropriate number of template arguments are
236 // specified.
David Greenede444af2009-04-22 16:42:54 +0000237 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000238 return Error(SubMultiClass.RefLoc,
239 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000240
David Greenede444af2009-04-22 16:42:54 +0000241 // Loop over all of the template arguments, setting them to the specified
242 // value or leaving them as the default if necessary.
243 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
244 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000245 // If a value is specified for this template arg, set it in the
246 // superclass now.
247 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000248 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000249 SubMultiClass.TemplateArgs[i]))
250 return true;
251
252 // Resolve it next.
253 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000254
David Greenede444af2009-04-22 16:42:54 +0000255 // Now remove it.
256 CurRec->removeValue(SMCTArgs[i]);
257
David Greened34a73b2009-04-24 16:55:41 +0000258 // If a value is specified for this template arg, set it in the
259 // new defs now.
260 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000261 CurMC->DefPrototypes.begin() + newDefStart,
262 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000263 j != jend;
264 ++j) {
265 Record *Def = *j;
266
David Greened34a73b2009-04-24 16:55:41 +0000267 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000268 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000269 SubMultiClass.TemplateArgs[i]))
270 return true;
271
272 // Resolve it next.
273 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
274
275 // Now remove it
276 Def->removeValue(SMCTArgs[i]);
277 }
278 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000279 return Error(SubMultiClass.RefLoc,
280 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000281 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000282 SMC->Rec.getName() + "'!");
283 }
284 }
285
286 return false;
287}
288
Chris Lattnerf4601652007-11-22 20:49:04 +0000289//===----------------------------------------------------------------------===//
290// Parser Code
291//===----------------------------------------------------------------------===//
292
293/// isObjectStart - Return true if this is a valid first token for an Object.
294static bool isObjectStart(tgtok::TokKind K) {
295 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000296 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000297}
298
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000299static std::string GetNewAnonymousName() {
300 static unsigned AnonCounter = 0;
301 return "anonymous."+utostr(AnonCounter++);
302}
303
Chris Lattnerf4601652007-11-22 20:49:04 +0000304/// ParseObjectName - If an object name is specified, return it. Otherwise,
305/// return an anonymous name.
306/// ObjectName ::= ID
307/// ObjectName ::= /*empty*/
308///
309std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000310 if (Lex.getCode() != tgtok::Id)
311 return GetNewAnonymousName();
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000312
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000313 std::string Ret = Lex.getCurStrVal();
314 Lex.Lex();
315 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000316}
317
318
319/// ParseClassID - Parse and resolve a reference to a class name. This returns
320/// null on error.
321///
322/// ClassID ::= ID
323///
324Record *TGParser::ParseClassID() {
325 if (Lex.getCode() != tgtok::Id) {
326 TokError("expected name for ClassID");
327 return 0;
328 }
Bob Wilson21870412009-11-22 04:24:42 +0000329
Chris Lattnerf4601652007-11-22 20:49:04 +0000330 Record *Result = Records.getClass(Lex.getCurStrVal());
331 if (Result == 0)
332 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000333
Chris Lattnerf4601652007-11-22 20:49:04 +0000334 Lex.Lex();
335 return Result;
336}
337
Bob Wilson32558652009-04-28 19:41:44 +0000338/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
339/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000340///
341/// MultiClassID ::= ID
342///
343MultiClass *TGParser::ParseMultiClassID() {
344 if (Lex.getCode() != tgtok::Id) {
345 TokError("expected name for ClassID");
346 return 0;
347 }
Bob Wilson32558652009-04-28 19:41:44 +0000348
David Greenede444af2009-04-22 16:42:54 +0000349 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
350 if (Result == 0)
351 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000352
David Greenede444af2009-04-22 16:42:54 +0000353 Lex.Lex();
354 return Result;
355}
356
Chris Lattnerf4601652007-11-22 20:49:04 +0000357Record *TGParser::ParseDefmID() {
358 if (Lex.getCode() != tgtok::Id) {
359 TokError("expected multiclass name");
360 return 0;
361 }
Bob Wilson21870412009-11-22 04:24:42 +0000362
Chris Lattnerf4601652007-11-22 20:49:04 +0000363 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
364 if (MC == 0) {
365 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
366 return 0;
367 }
Bob Wilson21870412009-11-22 04:24:42 +0000368
Chris Lattnerf4601652007-11-22 20:49:04 +0000369 Lex.Lex();
370 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000371}
Chris Lattnerf4601652007-11-22 20:49:04 +0000372
373
374/// ParseSubClassReference - Parse a reference to a subclass or to a templated
375/// subclass. This returns a SubClassRefTy with a null Record* on error.
376///
377/// SubClassRef ::= ClassID
378/// SubClassRef ::= ClassID '<' ValueList '>'
379///
380SubClassReference TGParser::
381ParseSubClassReference(Record *CurRec, bool isDefm) {
382 SubClassReference Result;
383 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000384
Chris Lattnerf4601652007-11-22 20:49:04 +0000385 if (isDefm)
386 Result.Rec = ParseDefmID();
387 else
388 Result.Rec = ParseClassID();
389 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000390
Chris Lattnerf4601652007-11-22 20:49:04 +0000391 // If there is no template arg list, we're done.
392 if (Lex.getCode() != tgtok::less)
393 return Result;
394 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000395
Chris Lattnerf4601652007-11-22 20:49:04 +0000396 if (Lex.getCode() == tgtok::greater) {
397 TokError("subclass reference requires a non-empty list of template values");
398 Result.Rec = 0;
399 return Result;
400 }
Bob Wilson21870412009-11-22 04:24:42 +0000401
David Greenee1b46912009-06-08 20:23:18 +0000402 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000403 if (Result.TemplateArgs.empty()) {
404 Result.Rec = 0; // Error parsing value list.
405 return Result;
406 }
Bob Wilson21870412009-11-22 04:24:42 +0000407
Chris Lattnerf4601652007-11-22 20:49:04 +0000408 if (Lex.getCode() != tgtok::greater) {
409 TokError("expected '>' in template value list");
410 Result.Rec = 0;
411 return Result;
412 }
413 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000414
Chris Lattnerf4601652007-11-22 20:49:04 +0000415 return Result;
416}
417
Bob Wilson32558652009-04-28 19:41:44 +0000418/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
419/// templated submulticlass. This returns a SubMultiClassRefTy with a null
420/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000421///
422/// SubMultiClassRef ::= MultiClassID
423/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
424///
425SubMultiClassReference TGParser::
426ParseSubMultiClassReference(MultiClass *CurMC) {
427 SubMultiClassReference Result;
428 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000429
David Greenede444af2009-04-22 16:42:54 +0000430 Result.MC = ParseMultiClassID();
431 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000432
David Greenede444af2009-04-22 16:42:54 +0000433 // If there is no template arg list, we're done.
434 if (Lex.getCode() != tgtok::less)
435 return Result;
436 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000437
David Greenede444af2009-04-22 16:42:54 +0000438 if (Lex.getCode() == tgtok::greater) {
439 TokError("subclass reference requires a non-empty list of template values");
440 Result.MC = 0;
441 return Result;
442 }
Bob Wilson32558652009-04-28 19:41:44 +0000443
David Greenee1b46912009-06-08 20:23:18 +0000444 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000445 if (Result.TemplateArgs.empty()) {
446 Result.MC = 0; // Error parsing value list.
447 return Result;
448 }
Bob Wilson32558652009-04-28 19:41:44 +0000449
David Greenede444af2009-04-22 16:42:54 +0000450 if (Lex.getCode() != tgtok::greater) {
451 TokError("expected '>' in template value list");
452 Result.MC = 0;
453 return Result;
454 }
455 Lex.Lex();
456
457 return Result;
458}
459
Chris Lattnerf4601652007-11-22 20:49:04 +0000460/// ParseRangePiece - Parse a bit/value range.
461/// RangePiece ::= INTVAL
462/// RangePiece ::= INTVAL '-' INTVAL
463/// RangePiece ::= INTVAL INTVAL
464bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000465 if (Lex.getCode() != tgtok::IntVal) {
466 TokError("expected integer or bitrange");
467 return true;
468 }
Dan Gohman63f97202008-10-17 01:33:43 +0000469 int64_t Start = Lex.getCurIntVal();
470 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000471
Chris Lattnerf4601652007-11-22 20:49:04 +0000472 if (Start < 0)
473 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000474
Chris Lattnerf4601652007-11-22 20:49:04 +0000475 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000476 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000477 Ranges.push_back(Start);
478 return false;
479 case tgtok::minus:
480 if (Lex.Lex() != tgtok::IntVal) {
481 TokError("expected integer value as end of range");
482 return true;
483 }
484 End = Lex.getCurIntVal();
485 break;
486 case tgtok::IntVal:
487 End = -Lex.getCurIntVal();
488 break;
489 }
Bob Wilson21870412009-11-22 04:24:42 +0000490 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000491 return TokError("invalid range, cannot be negative");
492 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000493
Chris Lattnerf4601652007-11-22 20:49:04 +0000494 // Add to the range.
495 if (Start < End) {
496 for (; Start <= End; ++Start)
497 Ranges.push_back(Start);
498 } else {
499 for (; Start >= End; --Start)
500 Ranges.push_back(Start);
501 }
502 return false;
503}
504
505/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
506///
507/// RangeList ::= RangePiece (',' RangePiece)*
508///
509std::vector<unsigned> TGParser::ParseRangeList() {
510 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000511
Chris Lattnerf4601652007-11-22 20:49:04 +0000512 // Parse the first piece.
513 if (ParseRangePiece(Result))
514 return std::vector<unsigned>();
515 while (Lex.getCode() == tgtok::comma) {
516 Lex.Lex(); // Eat the comma.
517
518 // Parse the next range piece.
519 if (ParseRangePiece(Result))
520 return std::vector<unsigned>();
521 }
522 return Result;
523}
524
525/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
526/// OptionalRangeList ::= '<' RangeList '>'
527/// OptionalRangeList ::= /*empty*/
528bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
529 if (Lex.getCode() != tgtok::less)
530 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000531
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000532 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000533 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000534
Chris Lattnerf4601652007-11-22 20:49:04 +0000535 // Parse the range list.
536 Ranges = ParseRangeList();
537 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000538
Chris Lattnerf4601652007-11-22 20:49:04 +0000539 if (Lex.getCode() != tgtok::greater) {
540 TokError("expected '>' at end of range list");
541 return Error(StartLoc, "to match this '<'");
542 }
543 Lex.Lex(); // eat the '>'.
544 return false;
545}
546
547/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
548/// OptionalBitList ::= '{' RangeList '}'
549/// OptionalBitList ::= /*empty*/
550bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
551 if (Lex.getCode() != tgtok::l_brace)
552 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000553
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000554 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000555 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000556
Chris Lattnerf4601652007-11-22 20:49:04 +0000557 // Parse the range list.
558 Ranges = ParseRangeList();
559 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000560
Chris Lattnerf4601652007-11-22 20:49:04 +0000561 if (Lex.getCode() != tgtok::r_brace) {
562 TokError("expected '}' at end of bit list");
563 return Error(StartLoc, "to match this '{'");
564 }
565 Lex.Lex(); // eat the '}'.
566 return false;
567}
568
569
570/// ParseType - Parse and return a tblgen type. This returns null on error.
571///
572/// Type ::= STRING // string type
573/// Type ::= BIT // bit type
574/// Type ::= BITS '<' INTVAL '>' // bits<x> type
575/// Type ::= INT // int type
576/// Type ::= LIST '<' Type '>' // list<x> type
577/// Type ::= CODE // code type
578/// Type ::= DAG // dag type
579/// Type ::= ClassID // Record Type
580///
581RecTy *TGParser::ParseType() {
582 switch (Lex.getCode()) {
583 default: TokError("Unknown token when expecting a type"); return 0;
584 case tgtok::String: Lex.Lex(); return new StringRecTy();
585 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
586 case tgtok::Int: Lex.Lex(); return new IntRecTy();
587 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
588 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
589 case tgtok::Id:
590 if (Record *R = ParseClassID()) return new RecordRecTy(R);
591 return 0;
592 case tgtok::Bits: {
593 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
594 TokError("expected '<' after bits type");
595 return 0;
596 }
597 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
598 TokError("expected integer in bits<n> type");
599 return 0;
600 }
Dan Gohman63f97202008-10-17 01:33:43 +0000601 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000602 if (Lex.Lex() != tgtok::greater) { // Eat count.
603 TokError("expected '>' at end of bits<n> type");
604 return 0;
605 }
606 Lex.Lex(); // Eat '>'
607 return new BitsRecTy(Val);
608 }
609 case tgtok::List: {
610 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
611 TokError("expected '<' after list type");
612 return 0;
613 }
614 Lex.Lex(); // Eat '<'
615 RecTy *SubType = ParseType();
616 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000617
Chris Lattnerf4601652007-11-22 20:49:04 +0000618 if (Lex.getCode() != tgtok::greater) {
619 TokError("expected '>' at end of list<ty> type");
620 return 0;
621 }
622 Lex.Lex(); // Eat '>'
623 return new ListRecTy(SubType);
624 }
Bob Wilson21870412009-11-22 04:24:42 +0000625 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000626}
627
628/// ParseIDValue - Parse an ID as a value and decode what it means.
629///
630/// IDValue ::= ID [def local value]
631/// IDValue ::= ID [def template arg]
632/// IDValue ::= ID [multiclass local value]
633/// IDValue ::= ID [multiclass template argument]
634/// IDValue ::= ID [def name]
635///
636Init *TGParser::ParseIDValue(Record *CurRec) {
637 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
638 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000639 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000640 Lex.Lex();
641 return ParseIDValue(CurRec, Name, Loc);
642}
643
644/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
645/// has already been read.
Bob Wilson21870412009-11-22 04:24:42 +0000646Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000647 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000648 if (CurRec) {
649 if (const RecordVal *RV = CurRec->getValue(Name))
650 return new VarInit(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000651
Chris Lattnerf4601652007-11-22 20:49:04 +0000652 std::string TemplateArgName = CurRec->getName()+":"+Name;
653 if (CurRec->isTemplateArg(TemplateArgName)) {
654 const RecordVal *RV = CurRec->getValue(TemplateArgName);
655 assert(RV && "Template arg doesn't exist??");
656 return new VarInit(TemplateArgName, RV->getType());
657 }
658 }
Bob Wilson21870412009-11-22 04:24:42 +0000659
Chris Lattnerf4601652007-11-22 20:49:04 +0000660 if (CurMultiClass) {
661 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
662 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
663 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
664 assert(RV && "Template arg doesn't exist??");
665 return new VarInit(MCName, RV->getType());
666 }
667 }
Bob Wilson21870412009-11-22 04:24:42 +0000668
Chris Lattnerf4601652007-11-22 20:49:04 +0000669 if (Record *D = Records.getDef(Name))
670 return new DefInit(D);
671
672 Error(NameLoc, "Variable not defined: '" + Name + "'");
673 return 0;
674}
675
David Greened418c1b2009-05-14 20:54:48 +0000676/// ParseOperation - Parse an operator. This returns null on error.
677///
678/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
679///
680Init *TGParser::ParseOperation(Record *CurRec) {
681 switch (Lex.getCode()) {
682 default:
683 TokError("unknown operation");
684 return 0;
685 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000686 case tgtok::XCar:
687 case tgtok::XCdr:
688 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000689 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
690 UnOpInit::UnaryOp Code;
691 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000692
David Greenee6c27de2009-05-14 21:22:49 +0000693 switch (Lex.getCode()) {
694 default: assert(0 && "Unhandled code!");
695 case tgtok::XCast:
696 Lex.Lex(); // eat the operation
697 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000698
David Greenee6c27de2009-05-14 21:22:49 +0000699 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000700
David Greenee6c27de2009-05-14 21:22:49 +0000701 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000702 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000703 return 0;
704 }
David Greened418c1b2009-05-14 20:54:48 +0000705
David Greenee6c27de2009-05-14 21:22:49 +0000706 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000707 case tgtok::XCar:
708 Lex.Lex(); // eat the operation
709 Code = UnOpInit::CAR;
710 break;
711 case tgtok::XCdr:
712 Lex.Lex(); // eat the operation
713 Code = UnOpInit::CDR;
714 break;
715 case tgtok::XNull:
716 Lex.Lex(); // eat the operation
717 Code = UnOpInit::LNULL;
718 Type = new IntRecTy;
719 break;
David Greenee6c27de2009-05-14 21:22:49 +0000720 }
721 if (Lex.getCode() != tgtok::l_paren) {
722 TokError("expected '(' after unary operator");
723 return 0;
724 }
725 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000726
David Greenee6c27de2009-05-14 21:22:49 +0000727 Init *LHS = ParseValue(CurRec);
728 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000729
David Greene5f9f9ba2009-05-14 22:38:31 +0000730 if (Code == UnOpInit::CAR
731 || Code == UnOpInit::CDR
732 || Code == UnOpInit::LNULL) {
733 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000734 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000735 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000736 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
737 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000738 return 0;
739 }
740 if (LHSt) {
741 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000742 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
743 if (LType == 0 && SType == 0) {
744 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000745 return 0;
746 }
747 }
748
749 if (Code == UnOpInit::CAR
750 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000751 if (LHSl == 0 && LHSt == 0) {
752 TokError("expected list type argumnet in unary operator");
753 return 0;
754 }
Bob Wilson21870412009-11-22 04:24:42 +0000755
David Greene5f9f9ba2009-05-14 22:38:31 +0000756 if (LHSl && LHSl->getSize() == 0) {
757 TokError("empty list argument in unary operator");
758 return 0;
759 }
760 if (LHSl) {
761 Init *Item = LHSl->getElement(0);
762 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
763 if (Itemt == 0) {
764 TokError("untyped list element in unary operator");
765 return 0;
766 }
767 if (Code == UnOpInit::CAR) {
768 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000769 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000770 Type = new ListRecTy(Itemt->getType());
771 }
Bob Wilson21870412009-11-22 04:24:42 +0000772 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000773 assert(LHSt && "expected list type argument in unary operator");
774 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
775 if (LType == 0) {
776 TokError("expected list type argumnet in unary operator");
777 return 0;
778 }
779 if (Code == UnOpInit::CAR) {
780 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000781 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000782 Type = LType;
783 }
784 }
785 }
786 }
787
David Greenee6c27de2009-05-14 21:22:49 +0000788 if (Lex.getCode() != tgtok::r_paren) {
789 TokError("expected ')' in unary operator");
790 return 0;
791 }
792 Lex.Lex(); // eat the ')'
793 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
794 }
David Greened418c1b2009-05-14 20:54:48 +0000795
796 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000797 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000798 case tgtok::XSRL:
799 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000800 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000801 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000802 tgtok::TokKind OpTok = Lex.getCode();
803 SMLoc OpLoc = Lex.getLoc();
804 Lex.Lex(); // eat the operation
805
David Greened418c1b2009-05-14 20:54:48 +0000806 BinOpInit::BinaryOp Code;
807 RecTy *Type = 0;
808
Chris Lattner8d978a72010-10-05 23:58:18 +0000809 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000810 default: assert(0 && "Unhandled code!");
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000811 case tgtok::XConcat: Code = BinOpInit::CONCAT; Type = new DagRecTy(); break;
812 case tgtok::XSRA: Code = BinOpInit::SRA; Type = new IntRecTy(); break;
813 case tgtok::XSRL: Code = BinOpInit::SRL; Type = new IntRecTy(); break;
814 case tgtok::XSHL: Code = BinOpInit::SHL; Type = new IntRecTy(); break;
Chris Lattner150d20e2010-10-31 19:22:57 +0000815 case tgtok::XEq: Code = BinOpInit::EQ; Type = new BitRecTy(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000816 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000817 Code = BinOpInit::STRCONCAT;
818 Type = new StringRecTy();
819 break;
David Greened418c1b2009-05-14 20:54:48 +0000820 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000821
David Greened418c1b2009-05-14 20:54:48 +0000822 if (Lex.getCode() != tgtok::l_paren) {
823 TokError("expected '(' after binary operator");
824 return 0;
825 }
826 Lex.Lex(); // eat the '('
827
Chris Lattner8d978a72010-10-05 23:58:18 +0000828 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000829
Chris Lattner8d978a72010-10-05 23:58:18 +0000830 InitList.push_back(ParseValue(CurRec));
831 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000832
Chris Lattner8d978a72010-10-05 23:58:18 +0000833 while (Lex.getCode() == tgtok::comma) {
834 Lex.Lex(); // eat the ','
835
836 InitList.push_back(ParseValue(CurRec));
837 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000838 }
David Greened418c1b2009-05-14 20:54:48 +0000839
840 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000841 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000842 return 0;
843 }
844 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000845
846 // We allow multiple operands to associative operators like !strconcat as
847 // shorthand for nesting them.
848 if (Code == BinOpInit::STRCONCAT) {
849 while (InitList.size() > 2) {
850 Init *RHS = InitList.pop_back_val();
851 RHS = (new BinOpInit(Code, InitList.back(), RHS, Type))
852 ->Fold(CurRec, CurMultiClass);
853 InitList.back() = RHS;
854 }
855 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000856
Chris Lattner8d978a72010-10-05 23:58:18 +0000857 if (InitList.size() == 2)
858 return (new BinOpInit(Code, InitList[0], InitList[1], Type))
859 ->Fold(CurRec, CurMultiClass);
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +0000860
Chris Lattner8d978a72010-10-05 23:58:18 +0000861 Error(OpLoc, "expected two operands to operator");
862 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000863 }
864
David Greene9bea7c82009-05-14 23:26:46 +0000865 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000866 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000867 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
868 TernOpInit::TernaryOp Code;
869 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000870
871
David Greene4afc5092009-05-14 21:54:42 +0000872 tgtok::TokKind LexCode = Lex.getCode();
873 Lex.Lex(); // eat the operation
874 switch (LexCode) {
875 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000876 case tgtok::XIf:
877 Code = TernOpInit::IF;
878 break;
David Greenebeb31a52009-05-14 22:23:47 +0000879 case tgtok::XForEach:
880 Code = TernOpInit::FOREACH;
881 break;
David Greene4afc5092009-05-14 21:54:42 +0000882 case tgtok::XSubst:
883 Code = TernOpInit::SUBST;
884 break;
885 }
886 if (Lex.getCode() != tgtok::l_paren) {
887 TokError("expected '(' after ternary operator");
888 return 0;
889 }
890 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000891
David Greene4afc5092009-05-14 21:54:42 +0000892 Init *LHS = ParseValue(CurRec);
893 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000894
David Greene4afc5092009-05-14 21:54:42 +0000895 if (Lex.getCode() != tgtok::comma) {
896 TokError("expected ',' in ternary operator");
897 return 0;
898 }
899 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000900
David Greene4afc5092009-05-14 21:54:42 +0000901 Init *MHS = ParseValue(CurRec);
902 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000903
David Greene4afc5092009-05-14 21:54:42 +0000904 if (Lex.getCode() != tgtok::comma) {
905 TokError("expected ',' in ternary operator");
906 return 0;
907 }
908 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000909
David Greene4afc5092009-05-14 21:54:42 +0000910 Init *RHS = ParseValue(CurRec);
911 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000912
David Greene4afc5092009-05-14 21:54:42 +0000913 if (Lex.getCode() != tgtok::r_paren) {
914 TokError("expected ')' in binary operator");
915 return 0;
916 }
917 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000918
David Greene4afc5092009-05-14 21:54:42 +0000919 switch (LexCode) {
920 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000921 case tgtok::XIf: {
922 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
923 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
924 if (MHSt == 0 || RHSt == 0) {
925 TokError("could not get type for !if");
926 return 0;
927 }
928 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
929 Type = RHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000930 } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
David Greene9bea7c82009-05-14 23:26:46 +0000931 Type = MHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000932 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000933 TokError("inconsistent types for !if");
934 return 0;
935 }
936 break;
937 }
David Greenebeb31a52009-05-14 22:23:47 +0000938 case tgtok::XForEach: {
939 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
940 if (MHSt == 0) {
941 TokError("could not get type for !foreach");
942 return 0;
943 }
944 Type = MHSt->getType();
945 break;
946 }
David Greene4afc5092009-05-14 21:54:42 +0000947 case tgtok::XSubst: {
948 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
949 if (RHSt == 0) {
950 TokError("could not get type for !subst");
951 return 0;
952 }
953 Type = RHSt->getType();
954 break;
955 }
956 }
Bob Wilson21870412009-11-22 04:24:42 +0000957 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
958 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000959 }
David Greened418c1b2009-05-14 20:54:48 +0000960 }
961 TokError("could not parse operation");
962 return 0;
963}
964
965/// ParseOperatorType - Parse a type for an operator. This returns
966/// null on error.
967///
968/// OperatorType ::= '<' Type '>'
969///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000970RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000971 RecTy *Type = 0;
972
973 if (Lex.getCode() != tgtok::less) {
974 TokError("expected type name for operator");
975 return 0;
976 }
977 Lex.Lex(); // eat the <
978
979 Type = ParseType();
980
981 if (Type == 0) {
982 TokError("expected type name for operator");
983 return 0;
984 }
985
986 if (Lex.getCode() != tgtok::greater) {
987 TokError("expected type name for operator");
988 return 0;
989 }
990 Lex.Lex(); // eat the >
991
992 return Type;
993}
994
995
Chris Lattnerf4601652007-11-22 20:49:04 +0000996/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
997///
998/// SimpleValue ::= IDValue
999/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001000/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001001/// SimpleValue ::= CODEFRAGMENT
1002/// SimpleValue ::= '?'
1003/// SimpleValue ::= '{' ValueList '}'
1004/// SimpleValue ::= ID '<' ValueListNE '>'
1005/// SimpleValue ::= '[' ValueList ']'
1006/// SimpleValue ::= '(' IDValue DagArgList ')'
1007/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1008/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1009/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1010/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1011/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1012///
David Greenee1b46912009-06-08 20:23:18 +00001013Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001014 Init *R = 0;
1015 switch (Lex.getCode()) {
1016 default: TokError("Unknown token when parsing a value"); break;
1017 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001018 case tgtok::StrVal: {
1019 std::string Val = Lex.getCurStrVal();
1020 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001021
Jim Grosbachda4231f2009-03-26 16:17:51 +00001022 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001023 while (Lex.getCode() == tgtok::StrVal) {
1024 Val += Lex.getCurStrVal();
1025 Lex.Lex();
1026 }
Bob Wilson21870412009-11-22 04:24:42 +00001027
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001028 R = new StringInit(Val);
1029 break;
1030 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001031 case tgtok::CodeFragment:
Chris Lattner578bcf02010-10-06 04:31:40 +00001032 R = new CodeInit(Lex.getCurStrVal());
1033 Lex.Lex();
1034 break;
1035 case tgtok::question:
1036 R = new UnsetInit();
1037 Lex.Lex();
1038 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001039 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001040 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001041 std::string Name = Lex.getCurStrVal();
1042 if (Lex.Lex() != tgtok::less) // consume the Id.
1043 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001044
Chris Lattnerf4601652007-11-22 20:49:04 +00001045 // Value ::= ID '<' ValueListNE '>'
1046 if (Lex.Lex() == tgtok::greater) {
1047 TokError("expected non-empty value list");
1048 return 0;
1049 }
David Greenee1b46912009-06-08 20:23:18 +00001050
Chris Lattnerf4601652007-11-22 20:49:04 +00001051 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1052 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1053 // body.
1054 Record *Class = Records.getClass(Name);
1055 if (!Class) {
1056 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1057 return 0;
1058 }
David Greenee1b46912009-06-08 20:23:18 +00001059
1060 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1061 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001062
David Greenee1b46912009-06-08 20:23:18 +00001063 if (Lex.getCode() != tgtok::greater) {
1064 TokError("expected '>' at end of value list");
1065 return 0;
1066 }
1067 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001068
Chris Lattnerf4601652007-11-22 20:49:04 +00001069 // Create the new record, set it as CurRec temporarily.
1070 static unsigned AnonCounter = 0;
Chris Lattner67db8832010-12-13 00:23:57 +00001071 Record *NewRec = Records.createRecord(
1072 "anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001073 SubClassReference SCRef;
1074 SCRef.RefLoc = NameLoc;
1075 SCRef.Rec = Class;
1076 SCRef.TemplateArgs = ValueList;
1077 // Add info about the subclass to NewRec.
1078 if (AddSubClass(NewRec, SCRef))
1079 return 0;
1080 NewRec->resolveReferences();
1081 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001082
Chris Lattnerf4601652007-11-22 20:49:04 +00001083 // The result of the expression is a reference to the new record.
1084 return new DefInit(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001085 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001086 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001087 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001088 Lex.Lex(); // eat the '{'
1089 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001090
Chris Lattnerf4601652007-11-22 20:49:04 +00001091 if (Lex.getCode() != tgtok::r_brace) {
1092 Vals = ParseValueList(CurRec);
1093 if (Vals.empty()) return 0;
1094 }
1095 if (Lex.getCode() != tgtok::r_brace) {
1096 TokError("expected '}' at end of bit list value");
1097 return 0;
1098 }
1099 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001100
Chris Lattnerf4601652007-11-22 20:49:04 +00001101 BitsInit *Result = new BitsInit(Vals.size());
1102 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1103 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1104 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001105 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1106 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001107 return 0;
1108 }
1109 Result->setBit(Vals.size()-i-1, Bit);
1110 }
1111 return Result;
1112 }
1113 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1114 Lex.Lex(); // eat the '['
1115 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001116
David Greenee1b46912009-06-08 20:23:18 +00001117 RecTy *DeducedEltTy = 0;
1118 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001119
David Greenee1b46912009-06-08 20:23:18 +00001120 if (ItemType != 0) {
1121 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1122 if (ListType == 0) {
1123 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001124 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001125 << ItemType->getAsString();
1126 TokError(s.str());
1127 }
1128 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001129 }
David Greenee1b46912009-06-08 20:23:18 +00001130
Chris Lattnerf4601652007-11-22 20:49:04 +00001131 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001132 Vals = ParseValueList(CurRec, 0,
1133 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001134 if (Vals.empty()) return 0;
1135 }
1136 if (Lex.getCode() != tgtok::r_square) {
1137 TokError("expected ']' at end of list value");
1138 return 0;
1139 }
1140 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001141
1142 RecTy *GivenEltTy = 0;
1143 if (Lex.getCode() == tgtok::less) {
1144 // Optional list element type
1145 Lex.Lex(); // eat the '<'
1146
1147 GivenEltTy = ParseType();
1148 if (GivenEltTy == 0) {
1149 // Couldn't parse element type
1150 return 0;
1151 }
1152
1153 if (Lex.getCode() != tgtok::greater) {
1154 TokError("expected '>' at end of list element type");
1155 return 0;
1156 }
1157 Lex.Lex(); // eat the '>'
1158 }
1159
1160 // Check elements
1161 RecTy *EltTy = 0;
1162 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1163 i != ie;
1164 ++i) {
1165 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1166 if (TArg == 0) {
1167 TokError("Untyped list element");
1168 return 0;
1169 }
1170 if (EltTy != 0) {
1171 EltTy = resolveTypes(EltTy, TArg->getType());
1172 if (EltTy == 0) {
1173 TokError("Incompatible types in list elements");
1174 return 0;
1175 }
Bob Wilson21870412009-11-22 04:24:42 +00001176 } else {
David Greenee1b46912009-06-08 20:23:18 +00001177 EltTy = TArg->getType();
1178 }
1179 }
1180
1181 if (GivenEltTy != 0) {
1182 if (EltTy != 0) {
1183 // Verify consistency
1184 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1185 TokError("Incompatible types in list elements");
1186 return 0;
1187 }
1188 }
1189 EltTy = GivenEltTy;
1190 }
1191
1192 if (EltTy == 0) {
1193 if (ItemType == 0) {
1194 TokError("No type for list");
1195 return 0;
1196 }
1197 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001198 } else {
David Greenee1b46912009-06-08 20:23:18 +00001199 // Make sure the deduced type is compatible with the given type
1200 if (GivenListTy) {
1201 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1202 TokError("Element type mismatch for list");
1203 return 0;
1204 }
1205 }
1206 DeducedEltTy = EltTy;
1207 }
Bob Wilson21870412009-11-22 04:24:42 +00001208
David Greenee1b46912009-06-08 20:23:18 +00001209 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001210 }
1211 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1212 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001213 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001214 TokError("expected identifier in dag init");
1215 return 0;
1216 }
Bob Wilson21870412009-11-22 04:24:42 +00001217
Mikhail Glushenkova73e5862010-10-23 07:32:53 +00001218 Init *Operator = ParseValue(CurRec);
Chris Lattner578bcf02010-10-06 04:31:40 +00001219 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001220
Nate Begeman7cee8172009-03-19 05:21:56 +00001221 // If the operator name is present, parse it.
1222 std::string OperatorName;
1223 if (Lex.getCode() == tgtok::colon) {
1224 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1225 TokError("expected variable name in dag operator");
1226 return 0;
1227 }
1228 OperatorName = Lex.getCurStrVal();
1229 Lex.Lex(); // eat the VarName.
1230 }
Bob Wilson21870412009-11-22 04:24:42 +00001231
Chris Lattnerf4601652007-11-22 20:49:04 +00001232 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1233 if (Lex.getCode() != tgtok::r_paren) {
1234 DagArgs = ParseDagArgList(CurRec);
1235 if (DagArgs.empty()) return 0;
1236 }
Bob Wilson21870412009-11-22 04:24:42 +00001237
Chris Lattnerf4601652007-11-22 20:49:04 +00001238 if (Lex.getCode() != tgtok::r_paren) {
1239 TokError("expected ')' in dag init");
1240 return 0;
1241 }
1242 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001243
Nate Begeman7cee8172009-03-19 05:21:56 +00001244 return new DagInit(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001245 }
Bob Wilson21870412009-11-22 04:24:42 +00001246
David Greene5f9f9ba2009-05-14 22:38:31 +00001247 case tgtok::XCar:
1248 case tgtok::XCdr:
1249 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001250 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001251 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001252 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 case tgtok::XSRL:
1254 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001255 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001256 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001257 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001258 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001259 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001260 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001261 }
1262 }
Bob Wilson21870412009-11-22 04:24:42 +00001263
Chris Lattnerf4601652007-11-22 20:49:04 +00001264 return R;
1265}
1266
1267/// ParseValue - Parse a tblgen value. This returns null on error.
1268///
1269/// Value ::= SimpleValue ValueSuffix*
1270/// ValueSuffix ::= '{' BitList '}'
1271/// ValueSuffix ::= '[' BitList ']'
1272/// ValueSuffix ::= '.' ID
1273///
David Greenee1b46912009-06-08 20:23:18 +00001274Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1275 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001276 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001277
Chris Lattnerf4601652007-11-22 20:49:04 +00001278 // Parse the suffixes now if present.
1279 while (1) {
1280 switch (Lex.getCode()) {
1281 default: return Result;
1282 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001283 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001284 Lex.Lex(); // eat the '{'
1285 std::vector<unsigned> Ranges = ParseRangeList();
1286 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001287
Chris Lattnerf4601652007-11-22 20:49:04 +00001288 // Reverse the bitlist.
1289 std::reverse(Ranges.begin(), Ranges.end());
1290 Result = Result->convertInitializerBitRange(Ranges);
1291 if (Result == 0) {
1292 Error(CurlyLoc, "Invalid bit range for value");
1293 return 0;
1294 }
Bob Wilson21870412009-11-22 04:24:42 +00001295
Chris Lattnerf4601652007-11-22 20:49:04 +00001296 // Eat the '}'.
1297 if (Lex.getCode() != tgtok::r_brace) {
1298 TokError("expected '}' at end of bit range list");
1299 return 0;
1300 }
1301 Lex.Lex();
1302 break;
1303 }
1304 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001305 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001306 Lex.Lex(); // eat the '['
1307 std::vector<unsigned> Ranges = ParseRangeList();
1308 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001309
Chris Lattnerf4601652007-11-22 20:49:04 +00001310 Result = Result->convertInitListSlice(Ranges);
1311 if (Result == 0) {
1312 Error(SquareLoc, "Invalid range for list slice");
1313 return 0;
1314 }
Bob Wilson21870412009-11-22 04:24:42 +00001315
Chris Lattnerf4601652007-11-22 20:49:04 +00001316 // Eat the ']'.
1317 if (Lex.getCode() != tgtok::r_square) {
1318 TokError("expected ']' at end of list slice");
1319 return 0;
1320 }
1321 Lex.Lex();
1322 break;
1323 }
1324 case tgtok::period:
1325 if (Lex.Lex() != tgtok::Id) { // eat the .
1326 TokError("expected field identifier after '.'");
1327 return 0;
1328 }
1329 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001330 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001331 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001332 return 0;
1333 }
1334 Result = new FieldInit(Result, Lex.getCurStrVal());
1335 Lex.Lex(); // eat field name
1336 break;
1337 }
1338 }
1339}
1340
1341/// ParseDagArgList - Parse the argument list for a dag literal expression.
1342///
1343/// ParseDagArgList ::= Value (':' VARNAME)?
1344/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
Bob Wilson21870412009-11-22 04:24:42 +00001345std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001346TGParser::ParseDagArgList(Record *CurRec) {
1347 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001348
Chris Lattnerf4601652007-11-22 20:49:04 +00001349 while (1) {
1350 Init *Val = ParseValue(CurRec);
1351 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001352
Chris Lattnerf4601652007-11-22 20:49:04 +00001353 // If the variable name is present, add it.
1354 std::string VarName;
1355 if (Lex.getCode() == tgtok::colon) {
1356 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1357 TokError("expected variable name in dag literal");
1358 return std::vector<std::pair<llvm::Init*, std::string> >();
1359 }
1360 VarName = Lex.getCurStrVal();
1361 Lex.Lex(); // eat the VarName.
1362 }
Bob Wilson21870412009-11-22 04:24:42 +00001363
Chris Lattnerf4601652007-11-22 20:49:04 +00001364 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001365
Chris Lattnerf4601652007-11-22 20:49:04 +00001366 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001367 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001368 }
Bob Wilson21870412009-11-22 04:24:42 +00001369
Chris Lattnerf4601652007-11-22 20:49:04 +00001370 return Result;
1371}
1372
1373
1374/// ParseValueList - Parse a comma separated list of values, returning them as a
1375/// vector. Note that this always expects to be able to parse at least one
1376/// value. It returns an empty list if this is not possible.
1377///
1378/// ValueList ::= Value (',' Value)
1379///
Bob Wilson21870412009-11-22 04:24:42 +00001380std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1381 RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001382 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001383 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001384 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001385 if (ArgsRec != 0 && EltTy == 0) {
1386 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1387 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1388 assert(RV && "Template argument record not found??");
1389 ItemType = RV->getType();
1390 ++ArgN;
1391 }
1392 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001393 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001394
Chris Lattnerf4601652007-11-22 20:49:04 +00001395 while (Lex.getCode() == tgtok::comma) {
1396 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001397
David Greenee1b46912009-06-08 20:23:18 +00001398 if (ArgsRec != 0 && EltTy == 0) {
1399 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001400 if (ArgN >= TArgs.size()) {
1401 TokError("too many template arguments");
1402 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001403 }
David Greenee1b46912009-06-08 20:23:18 +00001404 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1405 assert(RV && "Template argument record not found??");
1406 ItemType = RV->getType();
1407 ++ArgN;
1408 }
1409 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001410 if (Result.back() == 0) return std::vector<Init*>();
1411 }
Bob Wilson21870412009-11-22 04:24:42 +00001412
Chris Lattnerf4601652007-11-22 20:49:04 +00001413 return Result;
1414}
1415
1416
Chris Lattnerf4601652007-11-22 20:49:04 +00001417/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1418/// empty string on error. This can happen in a number of different context's,
1419/// including within a def or in the template args for a def (which which case
1420/// CurRec will be non-null) and within the template args for a multiclass (in
1421/// which case CurRec will be null, but CurMultiClass will be set). This can
1422/// also happen within a def that is within a multiclass, which will set both
1423/// CurRec and CurMultiClass.
1424///
1425/// Declaration ::= FIELD? Type ID ('=' Value)?
1426///
Bob Wilson21870412009-11-22 04:24:42 +00001427std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001428 bool ParsingTemplateArgs) {
1429 // Read the field prefix if present.
1430 bool HasField = Lex.getCode() == tgtok::Field;
1431 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001432
Chris Lattnerf4601652007-11-22 20:49:04 +00001433 RecTy *Type = ParseType();
1434 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001435
Chris Lattnerf4601652007-11-22 20:49:04 +00001436 if (Lex.getCode() != tgtok::Id) {
1437 TokError("Expected identifier in declaration");
1438 return "";
1439 }
Bob Wilson21870412009-11-22 04:24:42 +00001440
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001441 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001442 std::string DeclName = Lex.getCurStrVal();
1443 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001444
Chris Lattnerf4601652007-11-22 20:49:04 +00001445 if (ParsingTemplateArgs) {
1446 if (CurRec) {
1447 DeclName = CurRec->getName() + ":" + DeclName;
1448 } else {
1449 assert(CurMultiClass);
1450 }
1451 if (CurMultiClass)
1452 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1453 }
Bob Wilson21870412009-11-22 04:24:42 +00001454
Chris Lattnerf4601652007-11-22 20:49:04 +00001455 // Add the value.
1456 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1457 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001458
Chris Lattnerf4601652007-11-22 20:49:04 +00001459 // If a value is present, parse it.
1460 if (Lex.getCode() == tgtok::equal) {
1461 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001462 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001463 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001464 if (Val == 0 ||
1465 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1466 return "";
1467 }
Bob Wilson21870412009-11-22 04:24:42 +00001468
Chris Lattnerf4601652007-11-22 20:49:04 +00001469 return DeclName;
1470}
1471
1472/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1473/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1474/// template args for a def, which may or may not be in a multiclass. If null,
1475/// these are the template args for a multiclass.
1476///
1477/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001478///
Chris Lattnerf4601652007-11-22 20:49:04 +00001479bool TGParser::ParseTemplateArgList(Record *CurRec) {
1480 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1481 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001482
Chris Lattnerf4601652007-11-22 20:49:04 +00001483 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001484
Chris Lattnerf4601652007-11-22 20:49:04 +00001485 // Read the first declaration.
1486 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1487 if (TemplArg.empty())
1488 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001489
Chris Lattnerf4601652007-11-22 20:49:04 +00001490 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001491
Chris Lattnerf4601652007-11-22 20:49:04 +00001492 while (Lex.getCode() == tgtok::comma) {
1493 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001494
Chris Lattnerf4601652007-11-22 20:49:04 +00001495 // Read the following declarations.
1496 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1497 if (TemplArg.empty())
1498 return true;
1499 TheRecToAddTo->addTemplateArg(TemplArg);
1500 }
Bob Wilson21870412009-11-22 04:24:42 +00001501
Chris Lattnerf4601652007-11-22 20:49:04 +00001502 if (Lex.getCode() != tgtok::greater)
1503 return TokError("expected '>' at end of template argument list");
1504 Lex.Lex(); // eat the '>'.
1505 return false;
1506}
1507
1508
1509/// ParseBodyItem - Parse a single item at within the body of a def or class.
1510///
1511/// BodyItem ::= Declaration ';'
1512/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1513bool TGParser::ParseBodyItem(Record *CurRec) {
1514 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001515 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001516 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001517
Chris Lattnerf4601652007-11-22 20:49:04 +00001518 if (Lex.getCode() != tgtok::semi)
1519 return TokError("expected ';' after declaration");
1520 Lex.Lex();
1521 return false;
1522 }
1523
1524 // LET ID OptionalRangeList '=' Value ';'
1525 if (Lex.Lex() != tgtok::Id)
1526 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001527
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001528 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001529 std::string FieldName = Lex.getCurStrVal();
1530 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001531
Chris Lattnerf4601652007-11-22 20:49:04 +00001532 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001533 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001534 return true;
1535 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001536
Chris Lattnerf4601652007-11-22 20:49:04 +00001537 if (Lex.getCode() != tgtok::equal)
1538 return TokError("expected '=' in let expression");
1539 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001540
David Greenee1b46912009-06-08 20:23:18 +00001541 RecordVal *Field = CurRec->getValue(FieldName);
1542 if (Field == 0)
1543 return TokError("Value '" + FieldName + "' unknown!");
1544
1545 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001546
David Greenee1b46912009-06-08 20:23:18 +00001547 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001548 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001549
Chris Lattnerf4601652007-11-22 20:49:04 +00001550 if (Lex.getCode() != tgtok::semi)
1551 return TokError("expected ';' after let expression");
1552 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001553
Chris Lattnerf4601652007-11-22 20:49:04 +00001554 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1555}
1556
1557/// ParseBody - Read the body of a class or def. Return true on error, false on
1558/// success.
1559///
1560/// Body ::= ';'
1561/// Body ::= '{' BodyList '}'
1562/// BodyList BodyItem*
1563///
1564bool TGParser::ParseBody(Record *CurRec) {
1565 // If this is a null definition, just eat the semi and return.
1566 if (Lex.getCode() == tgtok::semi) {
1567 Lex.Lex();
1568 return false;
1569 }
Bob Wilson21870412009-11-22 04:24:42 +00001570
Chris Lattnerf4601652007-11-22 20:49:04 +00001571 if (Lex.getCode() != tgtok::l_brace)
1572 return TokError("Expected ';' or '{' to start body");
1573 // Eat the '{'.
1574 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001575
Chris Lattnerf4601652007-11-22 20:49:04 +00001576 while (Lex.getCode() != tgtok::r_brace)
1577 if (ParseBodyItem(CurRec))
1578 return true;
1579
1580 // Eat the '}'.
1581 Lex.Lex();
1582 return false;
1583}
1584
1585/// ParseObjectBody - Parse the body of a def or class. This consists of an
1586/// optional ClassList followed by a Body. CurRec is the current def or class
1587/// that is being parsed.
1588///
1589/// ObjectBody ::= BaseClassList Body
1590/// BaseClassList ::= /*empty*/
1591/// BaseClassList ::= ':' BaseClassListNE
1592/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1593///
1594bool TGParser::ParseObjectBody(Record *CurRec) {
1595 // If there is a baseclass list, read it.
1596 if (Lex.getCode() == tgtok::colon) {
1597 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001598
Chris Lattnerf4601652007-11-22 20:49:04 +00001599 // Read all of the subclasses.
1600 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1601 while (1) {
1602 // Check for error.
1603 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001604
Chris Lattnerf4601652007-11-22 20:49:04 +00001605 // Add it.
1606 if (AddSubClass(CurRec, SubClass))
1607 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001608
Chris Lattnerf4601652007-11-22 20:49:04 +00001609 if (Lex.getCode() != tgtok::comma) break;
1610 Lex.Lex(); // eat ','.
1611 SubClass = ParseSubClassReference(CurRec, false);
1612 }
1613 }
1614
1615 // Process any variables on the let stack.
1616 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1617 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1618 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1619 LetStack[i][j].Bits, LetStack[i][j].Value))
1620 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001621
Chris Lattnerf4601652007-11-22 20:49:04 +00001622 return ParseBody(CurRec);
1623}
1624
Chris Lattnerf4601652007-11-22 20:49:04 +00001625/// ParseDef - Parse and return a top level or multiclass def, return the record
1626/// corresponding to it. This returns null on error.
1627///
1628/// DefInst ::= DEF ObjectName ObjectBody
1629///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001630bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001631 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001632 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001633 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001634
1635 // Parse ObjectName and make a record for it.
Chris Lattner67db8832010-12-13 00:23:57 +00001636 Record *CurRec = Records.createRecord(ParseObjectName(), DefLoc);
Bob Wilson21870412009-11-22 04:24:42 +00001637
Chris Lattnerf4601652007-11-22 20:49:04 +00001638 if (!CurMultiClass) {
1639 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001640
Chris Lattnerf4601652007-11-22 20:49:04 +00001641 // Ensure redefinition doesn't happen.
1642 if (Records.getDef(CurRec->getName())) {
1643 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001644 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001645 }
1646 Records.addDef(CurRec);
1647 } else {
1648 // Otherwise, a def inside a multiclass, add it to the multiclass.
1649 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1650 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1651 Error(DefLoc, "def '" + CurRec->getName() +
1652 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001653 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001654 }
1655 CurMultiClass->DefPrototypes.push_back(CurRec);
1656 }
Bob Wilson21870412009-11-22 04:24:42 +00001657
Chris Lattnerf4601652007-11-22 20:49:04 +00001658 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001659 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001660
Chris Lattnerf4601652007-11-22 20:49:04 +00001661 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1662 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001663
Chris Lattnerf4601652007-11-22 20:49:04 +00001664 // If ObjectBody has template arguments, it's an error.
1665 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001666
1667 if (CurMultiClass) {
1668 // Copy the template arguments for the multiclass into the def.
1669 const std::vector<std::string> &TArgs =
1670 CurMultiClass->Rec.getTemplateArgs();
1671
1672 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1673 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1674 assert(RV && "Template arg doesn't exist?");
1675 CurRec->addValue(*RV);
1676 }
1677 }
1678
1679 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001680}
1681
1682
1683/// ParseClass - Parse a tblgen class definition.
1684///
1685/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1686///
1687bool TGParser::ParseClass() {
1688 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1689 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001690
Chris Lattnerf4601652007-11-22 20:49:04 +00001691 if (Lex.getCode() != tgtok::Id)
1692 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001693
Chris Lattnerf4601652007-11-22 20:49:04 +00001694 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1695 if (CurRec) {
1696 // If the body was previously defined, this is an error.
1697 if (!CurRec->getValues().empty() ||
1698 !CurRec->getSuperClasses().empty() ||
1699 !CurRec->getTemplateArgs().empty())
1700 return TokError("Class '" + CurRec->getName() + "' already defined");
1701 } else {
1702 // If this is the first reference to this class, create and add it.
Chris Lattner67db8832010-12-13 00:23:57 +00001703 CurRec = Records.createRecord(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001704 Records.addClass(CurRec);
1705 }
1706 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001707
Chris Lattnerf4601652007-11-22 20:49:04 +00001708 // If there are template args, parse them.
1709 if (Lex.getCode() == tgtok::less)
1710 if (ParseTemplateArgList(CurRec))
1711 return true;
1712
1713 // Finally, parse the object body.
1714 return ParseObjectBody(CurRec);
1715}
1716
1717/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1718/// of LetRecords.
1719///
1720/// LetList ::= LetItem (',' LetItem)*
1721/// LetItem ::= ID OptionalRangeList '=' Value
1722///
1723std::vector<LetRecord> TGParser::ParseLetList() {
1724 std::vector<LetRecord> Result;
Bob Wilson21870412009-11-22 04:24:42 +00001725
Chris Lattnerf4601652007-11-22 20:49:04 +00001726 while (1) {
1727 if (Lex.getCode() != tgtok::Id) {
1728 TokError("expected identifier in let definition");
1729 return std::vector<LetRecord>();
1730 }
1731 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001732 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001733 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001734
1735 // Check for an optional RangeList.
1736 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001737 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001738 return std::vector<LetRecord>();
1739 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001740
Chris Lattnerf4601652007-11-22 20:49:04 +00001741 if (Lex.getCode() != tgtok::equal) {
1742 TokError("expected '=' in let expression");
1743 return std::vector<LetRecord>();
1744 }
1745 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001746
Chris Lattnerf4601652007-11-22 20:49:04 +00001747 Init *Val = ParseValue(0);
1748 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001749
Chris Lattnerf4601652007-11-22 20:49:04 +00001750 // Now that we have everything, add the record.
1751 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001752
Chris Lattnerf4601652007-11-22 20:49:04 +00001753 if (Lex.getCode() != tgtok::comma)
1754 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001755 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001756 }
1757}
1758
1759/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001760/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001761///
1762/// Object ::= LET LetList IN '{' ObjectList '}'
1763/// Object ::= LET LetList IN Object
1764///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001765bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001766 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1767 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001768
Chris Lattnerf4601652007-11-22 20:49:04 +00001769 // Add this entry to the let stack.
1770 std::vector<LetRecord> LetInfo = ParseLetList();
1771 if (LetInfo.empty()) return true;
1772 LetStack.push_back(LetInfo);
1773
1774 if (Lex.getCode() != tgtok::In)
1775 return TokError("expected 'in' at end of top-level 'let'");
1776 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001777
Chris Lattnerf4601652007-11-22 20:49:04 +00001778 // If this is a scalar let, just handle it now
1779 if (Lex.getCode() != tgtok::l_brace) {
1780 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001781 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001782 return true;
1783 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001784 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001785 // Otherwise, this is a group let.
1786 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001787
Chris Lattnerf4601652007-11-22 20:49:04 +00001788 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001789 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001790 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001791
Chris Lattnerf4601652007-11-22 20:49:04 +00001792 if (Lex.getCode() != tgtok::r_brace) {
1793 TokError("expected '}' at end of top level let command");
1794 return Error(BraceLoc, "to match this '{'");
1795 }
1796 Lex.Lex();
1797 }
Bob Wilson21870412009-11-22 04:24:42 +00001798
Chris Lattnerf4601652007-11-22 20:49:04 +00001799 // Outside this let scope, this let block is not active.
1800 LetStack.pop_back();
1801 return false;
1802}
1803
Chris Lattnerf4601652007-11-22 20:49:04 +00001804/// ParseMultiClass - Parse a multiclass definition.
1805///
Bob Wilson32558652009-04-28 19:41:44 +00001806/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1807/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001808///
1809bool TGParser::ParseMultiClass() {
1810 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1811 Lex.Lex(); // Eat the multiclass token.
1812
1813 if (Lex.getCode() != tgtok::Id)
1814 return TokError("expected identifier after multiclass for name");
1815 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001816
Chris Lattnerf4601652007-11-22 20:49:04 +00001817 if (MultiClasses.count(Name))
1818 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001819
Chris Lattner67db8832010-12-13 00:23:57 +00001820 CurMultiClass = MultiClasses[Name] = new MultiClass(Name,
1821 Lex.getLoc(), Records);
Chris Lattnerf4601652007-11-22 20:49:04 +00001822 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001823
Chris Lattnerf4601652007-11-22 20:49:04 +00001824 // If there are template args, parse them.
1825 if (Lex.getCode() == tgtok::less)
1826 if (ParseTemplateArgList(0))
1827 return true;
1828
David Greened34a73b2009-04-24 16:55:41 +00001829 bool inherits = false;
1830
David Greenede444af2009-04-22 16:42:54 +00001831 // If there are submulticlasses, parse them.
1832 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001833 inherits = true;
1834
David Greenede444af2009-04-22 16:42:54 +00001835 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001836
David Greenede444af2009-04-22 16:42:54 +00001837 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001838 SubMultiClassReference SubMultiClass =
1839 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001840 while (1) {
1841 // Check for error.
1842 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001843
David Greenede444af2009-04-22 16:42:54 +00001844 // Add it.
1845 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1846 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001847
David Greenede444af2009-04-22 16:42:54 +00001848 if (Lex.getCode() != tgtok::comma) break;
1849 Lex.Lex(); // eat ','.
1850 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1851 }
1852 }
1853
David Greened34a73b2009-04-24 16:55:41 +00001854 if (Lex.getCode() != tgtok::l_brace) {
1855 if (!inherits)
1856 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001857 else if (Lex.getCode() != tgtok::semi)
1858 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001859 else
Bob Wilson21870412009-11-22 04:24:42 +00001860 Lex.Lex(); // eat the ';'.
1861 } else {
David Greened34a73b2009-04-24 16:55:41 +00001862 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1863 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001864
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001865 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001866 switch (Lex.getCode()) {
1867 default:
1868 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
1869 case tgtok::Let:
1870 case tgtok::Def:
1871 case tgtok::Defm:
1872 if (ParseObject(CurMultiClass))
1873 return true;
1874 break;
1875 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001876 }
David Greened34a73b2009-04-24 16:55:41 +00001877 Lex.Lex(); // eat the '}'.
1878 }
Bob Wilson21870412009-11-22 04:24:42 +00001879
Chris Lattnerf4601652007-11-22 20:49:04 +00001880 CurMultiClass = 0;
1881 return false;
1882}
1883
1884/// ParseDefm - Parse the instantiation of a multiclass.
1885///
1886/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1887///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001888bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001889 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00001890
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001891 std::string DefmPrefix;
1892 if (Lex.Lex() == tgtok::Id) { // eat the defm.
1893 DefmPrefix = Lex.getCurStrVal();
1894 Lex.Lex(); // Eat the defm prefix.
1895 }
Mikhail Glushenkovc761f7d2010-10-23 07:32:37 +00001896
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001897 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001898 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00001899 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00001900
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00001901 // Keep track of the new generated record definitions.
1902 std::vector<Record*> NewRecDefs;
1903
1904 // This record also inherits from a regular class (non-multiclass)?
1905 bool InheritFromClass = false;
1906
Chris Lattnerf4601652007-11-22 20:49:04 +00001907 // eat the colon.
1908 Lex.Lex();
1909
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001910 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001911 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001912
1913 while (1) {
1914 if (Ref.Rec == 0) return true;
1915
1916 // To instantiate a multiclass, we need to first get the multiclass, then
1917 // instantiate each def contained in the multiclass with the SubClassRef
1918 // template parameters.
1919 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1920 assert(MC && "Didn't lookup multiclass correctly?");
Bob Wilson21870412009-11-22 04:24:42 +00001921 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00001922
1923 // Verify that the correct number of template arguments were specified.
1924 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1925 if (TArgs.size() < TemplateVals.size())
1926 return Error(SubClassLoc,
1927 "more template args specified than multiclass expects");
1928
1929 // Loop over all the def's in the multiclass, instantiating each one.
1930 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1931 Record *DefProto = MC->DefPrototypes[i];
1932
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001933 // Add in the defm name. If the defm prefix is empty, give each
1934 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1935 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1936 // as a prefix.
David Greene065f2592009-05-05 16:28:25 +00001937 std::string DefName = DefProto->getName();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001938 if (DefmPrefix.empty()) {
1939 DefName = GetNewAnonymousName();
Bob Wilson21870412009-11-22 04:24:42 +00001940 } else {
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001941 std::string::size_type idx = DefName.find("#NAME#");
1942 if (idx != std::string::npos) {
1943 DefName.replace(idx, 6, DefmPrefix);
1944 } else {
1945 // Add the suffix to the defm name to get the new name.
1946 DefName = DefmPrefix + DefName;
1947 }
David Greene065f2592009-05-05 16:28:25 +00001948 }
1949
Chris Lattner67db8832010-12-13 00:23:57 +00001950 Record *CurRec = Records.createRecord(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001951
1952 SubClassReference Ref;
1953 Ref.RefLoc = DefmPrefixLoc;
1954 Ref.Rec = DefProto;
1955 AddSubClass(CurRec, Ref);
1956
1957 // Loop over all of the template arguments, setting them to the specified
1958 // value or leaving them as the default if necessary.
1959 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001960 // Check if a value is specified for this temp-arg.
1961 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001962 // Set it now.
1963 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1964 TemplateVals[i]))
1965 return true;
1966
1967 // Resolve it next.
1968 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1969
1970 // Now remove it.
1971 CurRec->removeValue(TArgs[i]);
1972
1973 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001974 return Error(SubClassLoc,
1975 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001976 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1977 MC->Rec.getName() + "'");
1978 }
1979 }
1980
1981 // If the mdef is inside a 'let' expression, add to each def.
1982 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1983 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1984 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1985 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1986 Error(DefmPrefixLoc, "when instantiating this defm");
1987 return true;
1988 }
1989
1990 // Ensure redefinition doesn't happen.
1991 if (Records.getDef(CurRec->getName()))
Bob Wilson21870412009-11-22 04:24:42 +00001992 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1993 "' already defined, instantiating defm with subdef '" +
David Greene56546132009-04-22 22:17:51 +00001994 DefProto->getName() + "'");
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001995
1996 // Don't create a top level definition for defm inside multiclasses,
1997 // instead, only update the prototypes and bind the template args
1998 // with the new created definition.
1999 if (CurMultiClass) {
2000 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2001 i != e; ++i) {
2002 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
2003 Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2004 "' already defined in this multiclass!");
2005 return 0;
2006 }
2007 }
2008 CurMultiClass->DefPrototypes.push_back(CurRec);
2009
2010 // Copy the template arguments for the multiclass into the new def.
2011 const std::vector<std::string> &TA =
2012 CurMultiClass->Rec.getTemplateArgs();
2013
2014 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2015 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2016 assert(RV && "Template arg doesn't exist?");
2017 CurRec->addValue(*RV);
2018 }
2019 } else {
2020 Records.addDef(CurRec);
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002021 }
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002022
2023 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002024 }
2025
2026 if (Lex.getCode() != tgtok::comma) break;
2027 Lex.Lex(); // eat ','.
2028
2029 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002030
2031 // A defm can inherit from regular classes (non-multiclass) as
2032 // long as they come in the end of the inheritance list.
2033 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2034
2035 if (InheritFromClass)
2036 break;
2037
David Greene56546132009-04-22 22:17:51 +00002038 Ref = ParseSubClassReference(0, true);
2039 }
2040
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002041 if (InheritFromClass) {
2042 // Process all the classes to inherit as if they were part of a
2043 // regular 'def' and inherit all record values.
2044 SubClassReference SubClass = ParseSubClassReference(0, false);
2045 while (1) {
2046 // Check for error.
2047 if (SubClass.Rec == 0) return true;
2048
2049 // Get the expanded definition prototypes and teach them about
2050 // the record values the current class to inherit has
2051 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2052 Record *CurRec = NewRecDefs[i];
2053
2054 // Add it.
2055 if (AddSubClass(CurRec, SubClass))
2056 return true;
2057
2058 // Process any variables on the let stack.
2059 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2060 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2061 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2062 LetStack[i][j].Bits, LetStack[i][j].Value))
2063 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002064 }
2065
2066 if (Lex.getCode() != tgtok::comma) break;
2067 Lex.Lex(); // eat ','.
2068 SubClass = ParseSubClassReference(0, false);
2069 }
2070 }
2071
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002072 if (!CurMultiClass)
2073 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
2074 NewRecDefs[i]->resolveReferences();
2075
Chris Lattnerf4601652007-11-22 20:49:04 +00002076 if (Lex.getCode() != tgtok::semi)
2077 return TokError("expected ';' at end of defm");
2078 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002079
Chris Lattnerf4601652007-11-22 20:49:04 +00002080 return false;
2081}
2082
2083/// ParseObject
2084/// Object ::= ClassInst
2085/// Object ::= DefInst
2086/// Object ::= MultiClassInst
2087/// Object ::= DefMInst
2088/// Object ::= LETCommand '{' ObjectList '}'
2089/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002090bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002091 switch (Lex.getCode()) {
Chris Lattnerd6d9dd92010-10-31 19:27:15 +00002092 default:
2093 return TokError("Expected class, def, defm, multiclass or let definition");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002094 case tgtok::Let: return ParseTopLevelLet(MC);
2095 case tgtok::Def: return ParseDef(MC);
2096 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002097 case tgtok::Class: return ParseClass();
2098 case tgtok::MultiClass: return ParseMultiClass();
2099 }
2100}
2101
2102/// ParseObjectList
2103/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002104bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002105 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002106 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002107 return true;
2108 }
2109 return false;
2110}
2111
Chris Lattnerf4601652007-11-22 20:49:04 +00002112bool TGParser::ParseFile() {
2113 Lex.Lex(); // Prime the lexer.
2114 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002115
Chris Lattnerf4601652007-11-22 20:49:04 +00002116 // If we have unread input at the end of the file, report it.
2117 if (Lex.getCode() == tgtok::Eof)
2118 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002119
Chris Lattnerf4601652007-11-22 20:49:04 +00002120 return TokError("Unexpected input at top level");
2121}
2122