blob: 1109defb75ebface19080d4f98a3a7bfe2d858f0 [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
Chuck Rose IIIaa917922007-11-26 23:19:59 +000014#include <algorithm>
David Greenee1b46912009-06-08 20:23:18 +000015#include <sstream>
Chuck Rose IIIaa917922007-11-26 23:19:59 +000016
Chris Lattnerf4601652007-11-22 20:49:04 +000017#include "TGParser.h"
18#include "Record.h"
19#include "llvm/ADT/StringExtras.h"
David Greened34a73b2009-04-24 16:55:41 +000020#include "llvm/Support/Streams.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 {
48 cerr << "Multiclass:\n";
49
50 MC->dump();
51
52 cerr << "Template args:\n";
53 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;
66
67 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 " +
72 "previous definition of type '" +
73 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.
Chris Lattner1e3a8a42009-06-21 03:39:35 +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;
98
99 // 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 }
114
115 // 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))
138 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");
159
160 // 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.
165 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
166 SubClass.TemplateArgs[i]))
167 return true;
168
169 // Resolve it next.
170 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
171
172 // 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 #"
177 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
178 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 }
191
192 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 ||
296 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
297}
298
299/// ParseObjectName - If an object name is specified, return it. Otherwise,
300/// return an anonymous name.
301/// ObjectName ::= ID
302/// ObjectName ::= /*empty*/
303///
304std::string TGParser::ParseObjectName() {
305 if (Lex.getCode() == tgtok::Id) {
306 std::string Ret = Lex.getCurStrVal();
307 Lex.Lex();
308 return Ret;
309 }
310
311 static unsigned AnonCounter = 0;
312 return "anonymous."+utostr(AnonCounter++);
313}
314
315
316/// ParseClassID - Parse and resolve a reference to a class name. This returns
317/// null on error.
318///
319/// ClassID ::= ID
320///
321Record *TGParser::ParseClassID() {
322 if (Lex.getCode() != tgtok::Id) {
323 TokError("expected name for ClassID");
324 return 0;
325 }
326
327 Record *Result = Records.getClass(Lex.getCurStrVal());
328 if (Result == 0)
329 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
330
331 Lex.Lex();
332 return Result;
333}
334
Bob Wilson32558652009-04-28 19:41:44 +0000335/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
336/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000337///
338/// MultiClassID ::= ID
339///
340MultiClass *TGParser::ParseMultiClassID() {
341 if (Lex.getCode() != tgtok::Id) {
342 TokError("expected name for ClassID");
343 return 0;
344 }
Bob Wilson32558652009-04-28 19:41:44 +0000345
David Greenede444af2009-04-22 16:42:54 +0000346 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
347 if (Result == 0)
348 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000349
David Greenede444af2009-04-22 16:42:54 +0000350 Lex.Lex();
351 return Result;
352}
353
Chris Lattnerf4601652007-11-22 20:49:04 +0000354Record *TGParser::ParseDefmID() {
355 if (Lex.getCode() != tgtok::Id) {
356 TokError("expected multiclass name");
357 return 0;
358 }
359
360 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
361 if (MC == 0) {
362 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
363 return 0;
364 }
365
366 Lex.Lex();
367 return &MC->Rec;
368}
369
370
371
372/// ParseSubClassReference - Parse a reference to a subclass or to a templated
373/// subclass. This returns a SubClassRefTy with a null Record* on error.
374///
375/// SubClassRef ::= ClassID
376/// SubClassRef ::= ClassID '<' ValueList '>'
377///
378SubClassReference TGParser::
379ParseSubClassReference(Record *CurRec, bool isDefm) {
380 SubClassReference Result;
381 Result.RefLoc = Lex.getLoc();
382
383 if (isDefm)
384 Result.Rec = ParseDefmID();
385 else
386 Result.Rec = ParseClassID();
387 if (Result.Rec == 0) return Result;
388
389 // If there is no template arg list, we're done.
390 if (Lex.getCode() != tgtok::less)
391 return Result;
392 Lex.Lex(); // Eat the '<'
393
394 if (Lex.getCode() == tgtok::greater) {
395 TokError("subclass reference requires a non-empty list of template values");
396 Result.Rec = 0;
397 return Result;
398 }
399
David Greenee1b46912009-06-08 20:23:18 +0000400 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000401 if (Result.TemplateArgs.empty()) {
402 Result.Rec = 0; // Error parsing value list.
403 return Result;
404 }
405
406 if (Lex.getCode() != tgtok::greater) {
407 TokError("expected '>' in template value list");
408 Result.Rec = 0;
409 return Result;
410 }
411 Lex.Lex();
412
413 return Result;
414}
415
Bob Wilson32558652009-04-28 19:41:44 +0000416/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
417/// templated submulticlass. This returns a SubMultiClassRefTy with a null
418/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000419///
420/// SubMultiClassRef ::= MultiClassID
421/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
422///
423SubMultiClassReference TGParser::
424ParseSubMultiClassReference(MultiClass *CurMC) {
425 SubMultiClassReference Result;
426 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000427
David Greenede444af2009-04-22 16:42:54 +0000428 Result.MC = ParseMultiClassID();
429 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000430
David Greenede444af2009-04-22 16:42:54 +0000431 // If there is no template arg list, we're done.
432 if (Lex.getCode() != tgtok::less)
433 return Result;
434 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000435
David Greenede444af2009-04-22 16:42:54 +0000436 if (Lex.getCode() == tgtok::greater) {
437 TokError("subclass reference requires a non-empty list of template values");
438 Result.MC = 0;
439 return Result;
440 }
Bob Wilson32558652009-04-28 19:41:44 +0000441
David Greenee1b46912009-06-08 20:23:18 +0000442 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000443 if (Result.TemplateArgs.empty()) {
444 Result.MC = 0; // Error parsing value list.
445 return Result;
446 }
Bob Wilson32558652009-04-28 19:41:44 +0000447
David Greenede444af2009-04-22 16:42:54 +0000448 if (Lex.getCode() != tgtok::greater) {
449 TokError("expected '>' in template value list");
450 Result.MC = 0;
451 return Result;
452 }
453 Lex.Lex();
454
455 return Result;
456}
457
Chris Lattnerf4601652007-11-22 20:49:04 +0000458/// ParseRangePiece - Parse a bit/value range.
459/// RangePiece ::= INTVAL
460/// RangePiece ::= INTVAL '-' INTVAL
461/// RangePiece ::= INTVAL INTVAL
462bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000463 if (Lex.getCode() != tgtok::IntVal) {
464 TokError("expected integer or bitrange");
465 return true;
466 }
Dan Gohman63f97202008-10-17 01:33:43 +0000467 int64_t Start = Lex.getCurIntVal();
468 int64_t End;
Chris Lattnerf4601652007-11-22 20:49:04 +0000469
470 if (Start < 0)
471 return TokError("invalid range, cannot be negative");
472
473 switch (Lex.Lex()) { // eat first character.
474 default:
475 Ranges.push_back(Start);
476 return false;
477 case tgtok::minus:
478 if (Lex.Lex() != tgtok::IntVal) {
479 TokError("expected integer value as end of range");
480 return true;
481 }
482 End = Lex.getCurIntVal();
483 break;
484 case tgtok::IntVal:
485 End = -Lex.getCurIntVal();
486 break;
487 }
488 if (End < 0)
489 return TokError("invalid range, cannot be negative");
490 Lex.Lex();
491
492 // Add to the range.
493 if (Start < End) {
494 for (; Start <= End; ++Start)
495 Ranges.push_back(Start);
496 } else {
497 for (; Start >= End; --Start)
498 Ranges.push_back(Start);
499 }
500 return false;
501}
502
503/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
504///
505/// RangeList ::= RangePiece (',' RangePiece)*
506///
507std::vector<unsigned> TGParser::ParseRangeList() {
508 std::vector<unsigned> Result;
509
510 // Parse the first piece.
511 if (ParseRangePiece(Result))
512 return std::vector<unsigned>();
513 while (Lex.getCode() == tgtok::comma) {
514 Lex.Lex(); // Eat the comma.
515
516 // Parse the next range piece.
517 if (ParseRangePiece(Result))
518 return std::vector<unsigned>();
519 }
520 return Result;
521}
522
523/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
524/// OptionalRangeList ::= '<' RangeList '>'
525/// OptionalRangeList ::= /*empty*/
526bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
527 if (Lex.getCode() != tgtok::less)
528 return false;
529
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000530 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000531 Lex.Lex(); // eat the '<'
532
533 // Parse the range list.
534 Ranges = ParseRangeList();
535 if (Ranges.empty()) return true;
536
537 if (Lex.getCode() != tgtok::greater) {
538 TokError("expected '>' at end of range list");
539 return Error(StartLoc, "to match this '<'");
540 }
541 Lex.Lex(); // eat the '>'.
542 return false;
543}
544
545/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
546/// OptionalBitList ::= '{' RangeList '}'
547/// OptionalBitList ::= /*empty*/
548bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
549 if (Lex.getCode() != tgtok::l_brace)
550 return false;
551
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000552 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000553 Lex.Lex(); // eat the '{'
554
555 // Parse the range list.
556 Ranges = ParseRangeList();
557 if (Ranges.empty()) return true;
558
559 if (Lex.getCode() != tgtok::r_brace) {
560 TokError("expected '}' at end of bit list");
561 return Error(StartLoc, "to match this '{'");
562 }
563 Lex.Lex(); // eat the '}'.
564 return false;
565}
566
567
568/// ParseType - Parse and return a tblgen type. This returns null on error.
569///
570/// Type ::= STRING // string type
571/// Type ::= BIT // bit type
572/// Type ::= BITS '<' INTVAL '>' // bits<x> type
573/// Type ::= INT // int type
574/// Type ::= LIST '<' Type '>' // list<x> type
575/// Type ::= CODE // code type
576/// Type ::= DAG // dag type
577/// Type ::= ClassID // Record Type
578///
579RecTy *TGParser::ParseType() {
580 switch (Lex.getCode()) {
581 default: TokError("Unknown token when expecting a type"); return 0;
582 case tgtok::String: Lex.Lex(); return new StringRecTy();
583 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
584 case tgtok::Int: Lex.Lex(); return new IntRecTy();
585 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
586 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
587 case tgtok::Id:
588 if (Record *R = ParseClassID()) return new RecordRecTy(R);
589 return 0;
590 case tgtok::Bits: {
591 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
592 TokError("expected '<' after bits type");
593 return 0;
594 }
595 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
596 TokError("expected integer in bits<n> type");
597 return 0;
598 }
Dan Gohman63f97202008-10-17 01:33:43 +0000599 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000600 if (Lex.Lex() != tgtok::greater) { // Eat count.
601 TokError("expected '>' at end of bits<n> type");
602 return 0;
603 }
604 Lex.Lex(); // Eat '>'
605 return new BitsRecTy(Val);
606 }
607 case tgtok::List: {
608 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
609 TokError("expected '<' after list type");
610 return 0;
611 }
612 Lex.Lex(); // Eat '<'
613 RecTy *SubType = ParseType();
614 if (SubType == 0) return 0;
615
616 if (Lex.getCode() != tgtok::greater) {
617 TokError("expected '>' at end of list<ty> type");
618 return 0;
619 }
620 Lex.Lex(); // Eat '>'
621 return new ListRecTy(SubType);
622 }
623 }
624}
625
626/// ParseIDValue - Parse an ID as a value and decode what it means.
627///
628/// IDValue ::= ID [def local value]
629/// IDValue ::= ID [def template arg]
630/// IDValue ::= ID [multiclass local value]
631/// IDValue ::= ID [multiclass template argument]
632/// IDValue ::= ID [def name]
633///
634Init *TGParser::ParseIDValue(Record *CurRec) {
635 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
636 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000637 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000638 Lex.Lex();
639 return ParseIDValue(CurRec, Name, Loc);
640}
641
642/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
643/// has already been read.
644Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000645 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000646 if (CurRec) {
647 if (const RecordVal *RV = CurRec->getValue(Name))
648 return new VarInit(Name, RV->getType());
649
650 std::string TemplateArgName = CurRec->getName()+":"+Name;
651 if (CurRec->isTemplateArg(TemplateArgName)) {
652 const RecordVal *RV = CurRec->getValue(TemplateArgName);
653 assert(RV && "Template arg doesn't exist??");
654 return new VarInit(TemplateArgName, RV->getType());
655 }
656 }
657
658 if (CurMultiClass) {
659 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
660 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
661 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
662 assert(RV && "Template arg doesn't exist??");
663 return new VarInit(MCName, RV->getType());
664 }
665 }
666
667 if (Record *D = Records.getDef(Name))
668 return new DefInit(D);
669
670 Error(NameLoc, "Variable not defined: '" + Name + "'");
671 return 0;
672}
673
David Greened418c1b2009-05-14 20:54:48 +0000674/// ParseOperation - Parse an operator. This returns null on error.
675///
676/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
677///
678Init *TGParser::ParseOperation(Record *CurRec) {
679 switch (Lex.getCode()) {
680 default:
681 TokError("unknown operation");
682 return 0;
683 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000684 case tgtok::XCar:
685 case tgtok::XCdr:
686 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000687 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
688 UnOpInit::UnaryOp Code;
689 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000690
David Greenee6c27de2009-05-14 21:22:49 +0000691 switch (Lex.getCode()) {
692 default: assert(0 && "Unhandled code!");
693 case tgtok::XCast:
694 Lex.Lex(); // eat the operation
695 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000696
David Greenee6c27de2009-05-14 21:22:49 +0000697 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000698
David Greenee6c27de2009-05-14 21:22:49 +0000699 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000700 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000701 return 0;
702 }
David Greened418c1b2009-05-14 20:54:48 +0000703
David Greenee6c27de2009-05-14 21:22:49 +0000704 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000705 case tgtok::XCar:
706 Lex.Lex(); // eat the operation
707 Code = UnOpInit::CAR;
708 break;
709 case tgtok::XCdr:
710 Lex.Lex(); // eat the operation
711 Code = UnOpInit::CDR;
712 break;
713 case tgtok::XNull:
714 Lex.Lex(); // eat the operation
715 Code = UnOpInit::LNULL;
716 Type = new IntRecTy;
717 break;
David Greenee6c27de2009-05-14 21:22:49 +0000718 }
719 if (Lex.getCode() != tgtok::l_paren) {
720 TokError("expected '(' after unary operator");
721 return 0;
722 }
723 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000724
David Greenee6c27de2009-05-14 21:22:49 +0000725 Init *LHS = ParseValue(CurRec);
726 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000727
David Greene5f9f9ba2009-05-14 22:38:31 +0000728 if (Code == UnOpInit::CAR
729 || Code == UnOpInit::CDR
730 || Code == UnOpInit::LNULL) {
731 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000732 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000733 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000734 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
735 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000736 return 0;
737 }
738 if (LHSt) {
739 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000740 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
741 if (LType == 0 && SType == 0) {
742 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000743 return 0;
744 }
745 }
746
747 if (Code == UnOpInit::CAR
748 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000749 if (LHSl == 0 && LHSt == 0) {
750 TokError("expected list type argumnet in unary operator");
751 return 0;
752 }
753
David Greene5f9f9ba2009-05-14 22:38:31 +0000754 if (LHSl && LHSl->getSize() == 0) {
755 TokError("empty list argument in unary operator");
756 return 0;
757 }
758 if (LHSl) {
759 Init *Item = LHSl->getElement(0);
760 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
761 if (Itemt == 0) {
762 TokError("untyped list element in unary operator");
763 return 0;
764 }
765 if (Code == UnOpInit::CAR) {
766 Type = Itemt->getType();
767 }
768 else {
769 Type = new ListRecTy(Itemt->getType());
770 }
771 }
772 else {
773 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();
781 }
782 else {
783 Type = LType;
784 }
785 }
786 }
787 }
788
David Greenee6c27de2009-05-14 21:22:49 +0000789 if (Lex.getCode() != tgtok::r_paren) {
790 TokError("expected ')' in unary operator");
791 return 0;
792 }
793 Lex.Lex(); // eat the ')'
794 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
795 }
David Greened418c1b2009-05-14 20:54:48 +0000796
797 case tgtok::XConcat:
798 case tgtok::XSRA:
799 case tgtok::XSRL:
800 case tgtok::XSHL:
801 case tgtok::XStrConcat:
802 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
803 BinOpInit::BinaryOp Code;
804 RecTy *Type = 0;
805
806
807 switch (Lex.getCode()) {
808 default: assert(0 && "Unhandled code!");
809 case tgtok::XConcat:
810 Lex.Lex(); // eat the operation
811 Code = BinOpInit::CONCAT;
812 Type = new DagRecTy();
813 break;
814 case tgtok::XSRA:
815 Lex.Lex(); // eat the operation
816 Code = BinOpInit::SRA;
817 Type = new IntRecTy();
818 break;
819 case tgtok::XSRL:
820 Lex.Lex(); // eat the operation
821 Code = BinOpInit::SRL;
822 Type = new IntRecTy();
823 break;
824 case tgtok::XSHL:
825 Lex.Lex(); // eat the operation
826 Code = BinOpInit::SHL;
827 Type = new IntRecTy();
828 break;
829 case tgtok::XStrConcat:
830 Lex.Lex(); // eat the operation
831 Code = BinOpInit::STRCONCAT;
832 Type = new StringRecTy();
833 break;
834 case tgtok::XNameConcat:
835 Lex.Lex(); // eat the operation
836 Code = BinOpInit::NAMECONCAT;
837
838 Type = ParseOperatorType();
839
840 if (Type == 0) {
841 TokError("did not get type for binary operator");
842 return 0;
843 }
844
845 break;
846 }
847 if (Lex.getCode() != tgtok::l_paren) {
848 TokError("expected '(' after binary operator");
849 return 0;
850 }
851 Lex.Lex(); // eat the '('
852
853 Init *LHS = ParseValue(CurRec);
854 if (LHS == 0) return 0;
855
856 if (Lex.getCode() != tgtok::comma) {
857 TokError("expected ',' in binary operator");
858 return 0;
859 }
860 Lex.Lex(); // eat the ','
861
862 Init *RHS = ParseValue(CurRec);
863 if (RHS == 0) return 0;
864
865 if (Lex.getCode() != tgtok::r_paren) {
866 TokError("expected ')' in binary operator");
867 return 0;
868 }
869 Lex.Lex(); // eat the ')'
870 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
871 }
872
David Greene9bea7c82009-05-14 23:26:46 +0000873 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000874 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000875 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
876 TernOpInit::TernaryOp Code;
877 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000878
879
David Greene4afc5092009-05-14 21:54:42 +0000880 tgtok::TokKind LexCode = Lex.getCode();
881 Lex.Lex(); // eat the operation
882 switch (LexCode) {
883 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000884 case tgtok::XIf:
885 Code = TernOpInit::IF;
886 break;
David Greenebeb31a52009-05-14 22:23:47 +0000887 case tgtok::XForEach:
888 Code = TernOpInit::FOREACH;
889 break;
David Greene4afc5092009-05-14 21:54:42 +0000890 case tgtok::XSubst:
891 Code = TernOpInit::SUBST;
892 break;
893 }
894 if (Lex.getCode() != tgtok::l_paren) {
895 TokError("expected '(' after ternary operator");
896 return 0;
897 }
898 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000899
David Greene4afc5092009-05-14 21:54:42 +0000900 Init *LHS = ParseValue(CurRec);
901 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000902
David Greene4afc5092009-05-14 21:54:42 +0000903 if (Lex.getCode() != tgtok::comma) {
904 TokError("expected ',' in ternary operator");
905 return 0;
906 }
907 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 Init *MHS = ParseValue(CurRec);
910 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000911
David Greene4afc5092009-05-14 21:54:42 +0000912 if (Lex.getCode() != tgtok::comma) {
913 TokError("expected ',' in ternary operator");
914 return 0;
915 }
916 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000917
David Greene4afc5092009-05-14 21:54:42 +0000918 Init *RHS = ParseValue(CurRec);
919 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000920
David Greene4afc5092009-05-14 21:54:42 +0000921 if (Lex.getCode() != tgtok::r_paren) {
922 TokError("expected ')' in binary operator");
923 return 0;
924 }
925 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000926
David Greene4afc5092009-05-14 21:54:42 +0000927 switch (LexCode) {
928 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000929 case tgtok::XIf: {
930 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
931 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
932 if (MHSt == 0 || RHSt == 0) {
933 TokError("could not get type for !if");
934 return 0;
935 }
936 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
937 Type = RHSt->getType();
938 }
939 else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
940 Type = MHSt->getType();
941 }
942 else {
943 TokError("inconsistent types for !if");
944 return 0;
945 }
946 break;
947 }
David Greenebeb31a52009-05-14 22:23:47 +0000948 case tgtok::XForEach: {
949 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
950 if (MHSt == 0) {
951 TokError("could not get type for !foreach");
952 return 0;
953 }
954 Type = MHSt->getType();
955 break;
956 }
David Greene4afc5092009-05-14 21:54:42 +0000957 case tgtok::XSubst: {
958 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
959 if (RHSt == 0) {
960 TokError("could not get type for !subst");
961 return 0;
962 }
963 Type = RHSt->getType();
964 break;
965 }
966 }
967 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
968 }
David Greened418c1b2009-05-14 20:54:48 +0000969 }
970 TokError("could not parse operation");
971 return 0;
972}
973
974/// ParseOperatorType - Parse a type for an operator. This returns
975/// null on error.
976///
977/// OperatorType ::= '<' Type '>'
978///
979RecTy *TGParser::ParseOperatorType(void) {
980 RecTy *Type = 0;
981
982 if (Lex.getCode() != tgtok::less) {
983 TokError("expected type name for operator");
984 return 0;
985 }
986 Lex.Lex(); // eat the <
987
988 Type = ParseType();
989
990 if (Type == 0) {
991 TokError("expected type name for operator");
992 return 0;
993 }
994
995 if (Lex.getCode() != tgtok::greater) {
996 TokError("expected type name for operator");
997 return 0;
998 }
999 Lex.Lex(); // eat the >
1000
1001 return Type;
1002}
1003
1004
Chris Lattnerf4601652007-11-22 20:49:04 +00001005/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1006///
1007/// SimpleValue ::= IDValue
1008/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001009/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001010/// SimpleValue ::= CODEFRAGMENT
1011/// SimpleValue ::= '?'
1012/// SimpleValue ::= '{' ValueList '}'
1013/// SimpleValue ::= ID '<' ValueListNE '>'
1014/// SimpleValue ::= '[' ValueList ']'
1015/// SimpleValue ::= '(' IDValue DagArgList ')'
1016/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1017/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1018/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1019/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1020/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1021///
David Greenee1b46912009-06-08 20:23:18 +00001022Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001023 Init *R = 0;
1024 switch (Lex.getCode()) {
1025 default: TokError("Unknown token when parsing a value"); break;
1026 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001027 case tgtok::StrVal: {
1028 std::string Val = Lex.getCurStrVal();
1029 Lex.Lex();
1030
Jim Grosbachda4231f2009-03-26 16:17:51 +00001031 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001032 while (Lex.getCode() == tgtok::StrVal) {
1033 Val += Lex.getCurStrVal();
1034 Lex.Lex();
1035 }
1036
1037 R = new StringInit(Val);
1038 break;
1039 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001040 case tgtok::CodeFragment:
1041 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
1042 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1043 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001044 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001045 std::string Name = Lex.getCurStrVal();
1046 if (Lex.Lex() != tgtok::less) // consume the Id.
1047 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
1048
1049 // Value ::= ID '<' ValueListNE '>'
1050 if (Lex.Lex() == tgtok::greater) {
1051 TokError("expected non-empty value list");
1052 return 0;
1053 }
David Greenee1b46912009-06-08 20:23:18 +00001054
Chris Lattnerf4601652007-11-22 20:49:04 +00001055 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1056 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1057 // body.
1058 Record *Class = Records.getClass(Name);
1059 if (!Class) {
1060 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1061 return 0;
1062 }
David Greenee1b46912009-06-08 20:23:18 +00001063
1064 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1065 if (ValueList.empty()) return 0;
1066
1067 if (Lex.getCode() != tgtok::greater) {
1068 TokError("expected '>' at end of value list");
1069 return 0;
1070 }
1071 Lex.Lex(); // eat the '>'
Chris Lattnerf4601652007-11-22 20:49:04 +00001072
1073 // Create the new record, set it as CurRec temporarily.
1074 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001075 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001076 SubClassReference SCRef;
1077 SCRef.RefLoc = NameLoc;
1078 SCRef.Rec = Class;
1079 SCRef.TemplateArgs = ValueList;
1080 // Add info about the subclass to NewRec.
1081 if (AddSubClass(NewRec, SCRef))
1082 return 0;
1083 NewRec->resolveReferences();
1084 Records.addDef(NewRec);
1085
1086 // The result of the expression is a reference to the new record.
1087 return new DefInit(NewRec);
1088 }
1089 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001090 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001091 Lex.Lex(); // eat the '{'
1092 std::vector<Init*> Vals;
1093
1094 if (Lex.getCode() != tgtok::r_brace) {
1095 Vals = ParseValueList(CurRec);
1096 if (Vals.empty()) return 0;
1097 }
1098 if (Lex.getCode() != tgtok::r_brace) {
1099 TokError("expected '}' at end of bit list value");
1100 return 0;
1101 }
1102 Lex.Lex(); // eat the '}'
1103
1104 BitsInit *Result = new BitsInit(Vals.size());
1105 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1106 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1107 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001108 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1109 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001110 return 0;
1111 }
1112 Result->setBit(Vals.size()-i-1, Bit);
1113 }
1114 return Result;
1115 }
1116 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1117 Lex.Lex(); // eat the '['
1118 std::vector<Init*> Vals;
1119
David Greenee1b46912009-06-08 20:23:18 +00001120 RecTy *DeducedEltTy = 0;
1121 ListRecTy *GivenListTy = 0;
1122
1123 if (ItemType != 0) {
1124 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1125 if (ListType == 0) {
1126 std::stringstream s;
1127 s << "Type mismatch for list, expected list type, got "
1128 << ItemType->getAsString();
1129 TokError(s.str());
1130 }
1131 GivenListTy = ListType;
1132 }
1133
Chris Lattnerf4601652007-11-22 20:49:04 +00001134 if (Lex.getCode() != tgtok::r_square) {
David Greenee1b46912009-06-08 20:23:18 +00001135 Vals = ParseValueList(CurRec, 0, GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001136 if (Vals.empty()) return 0;
1137 }
1138 if (Lex.getCode() != tgtok::r_square) {
1139 TokError("expected ']' at end of list value");
1140 return 0;
1141 }
1142 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001143
1144 RecTy *GivenEltTy = 0;
1145 if (Lex.getCode() == tgtok::less) {
1146 // Optional list element type
1147 Lex.Lex(); // eat the '<'
1148
1149 GivenEltTy = ParseType();
1150 if (GivenEltTy == 0) {
1151 // Couldn't parse element type
1152 return 0;
1153 }
1154
1155 if (Lex.getCode() != tgtok::greater) {
1156 TokError("expected '>' at end of list element type");
1157 return 0;
1158 }
1159 Lex.Lex(); // eat the '>'
1160 }
1161
1162 // Check elements
1163 RecTy *EltTy = 0;
1164 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1165 i != ie;
1166 ++i) {
1167 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1168 if (TArg == 0) {
1169 TokError("Untyped list element");
1170 return 0;
1171 }
1172 if (EltTy != 0) {
1173 EltTy = resolveTypes(EltTy, TArg->getType());
1174 if (EltTy == 0) {
1175 TokError("Incompatible types in list elements");
1176 return 0;
1177 }
1178 }
1179 else {
1180 EltTy = TArg->getType();
1181 }
1182 }
1183
1184 if (GivenEltTy != 0) {
1185 if (EltTy != 0) {
1186 // Verify consistency
1187 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1188 TokError("Incompatible types in list elements");
1189 return 0;
1190 }
1191 }
1192 EltTy = GivenEltTy;
1193 }
1194
1195 if (EltTy == 0) {
1196 if (ItemType == 0) {
1197 TokError("No type for list");
1198 return 0;
1199 }
1200 DeducedEltTy = GivenListTy->getElementType();
1201 }
1202 else {
1203 // Make sure the deduced type is compatible with the given type
1204 if (GivenListTy) {
1205 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1206 TokError("Element type mismatch for list");
1207 return 0;
1208 }
1209 }
1210 DeducedEltTy = EltTy;
1211 }
1212
1213 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001214 }
1215 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1216 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001217 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001218 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001219 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001220 TokError("expected identifier in dag init");
1221 return 0;
1222 }
1223
David Greenec7cafcd2009-04-22 20:18:10 +00001224 Init *Operator = 0;
1225 if (Lex.getCode() == tgtok::Id) {
1226 Operator = ParseIDValue(CurRec);
1227 if (Operator == 0) return 0;
1228 }
1229 else {
David Greened418c1b2009-05-14 20:54:48 +00001230 Operator = ParseOperation(CurRec);
1231 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001232 }
1233
Nate Begeman7cee8172009-03-19 05:21:56 +00001234 // If the operator name is present, parse it.
1235 std::string OperatorName;
1236 if (Lex.getCode() == tgtok::colon) {
1237 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1238 TokError("expected variable name in dag operator");
1239 return 0;
1240 }
1241 OperatorName = Lex.getCurStrVal();
1242 Lex.Lex(); // eat the VarName.
1243 }
1244
Chris Lattnerf4601652007-11-22 20:49:04 +00001245 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1246 if (Lex.getCode() != tgtok::r_paren) {
1247 DagArgs = ParseDagArgList(CurRec);
1248 if (DagArgs.empty()) return 0;
1249 }
1250
1251 if (Lex.getCode() != tgtok::r_paren) {
1252 TokError("expected ')' in dag init");
1253 return 0;
1254 }
1255 Lex.Lex(); // eat the ')'
1256
Nate Begeman7cee8172009-03-19 05:21:56 +00001257 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001258 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001259 }
David Greened418c1b2009-05-14 20:54:48 +00001260
David Greene5f9f9ba2009-05-14 22:38:31 +00001261 case tgtok::XCar:
1262 case tgtok::XCdr:
1263 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001264 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001265 case tgtok::XConcat:
1266 case tgtok::XSRA:
1267 case tgtok::XSRL:
1268 case tgtok::XSHL:
David Greenec7cafcd2009-04-22 20:18:10 +00001269 case tgtok::XStrConcat:
David Greene4afc5092009-05-14 21:54:42 +00001270 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001271 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001272 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001273 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001274 return ParseOperation(CurRec);
1275 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001276 }
1277 }
1278
1279 return R;
1280}
1281
1282/// ParseValue - Parse a tblgen value. This returns null on error.
1283///
1284/// Value ::= SimpleValue ValueSuffix*
1285/// ValueSuffix ::= '{' BitList '}'
1286/// ValueSuffix ::= '[' BitList ']'
1287/// ValueSuffix ::= '.' ID
1288///
David Greenee1b46912009-06-08 20:23:18 +00001289Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1290 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001291 if (Result == 0) return 0;
1292
1293 // Parse the suffixes now if present.
1294 while (1) {
1295 switch (Lex.getCode()) {
1296 default: return Result;
1297 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001298 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001299 Lex.Lex(); // eat the '{'
1300 std::vector<unsigned> Ranges = ParseRangeList();
1301 if (Ranges.empty()) return 0;
1302
1303 // Reverse the bitlist.
1304 std::reverse(Ranges.begin(), Ranges.end());
1305 Result = Result->convertInitializerBitRange(Ranges);
1306 if (Result == 0) {
1307 Error(CurlyLoc, "Invalid bit range for value");
1308 return 0;
1309 }
1310
1311 // Eat the '}'.
1312 if (Lex.getCode() != tgtok::r_brace) {
1313 TokError("expected '}' at end of bit range list");
1314 return 0;
1315 }
1316 Lex.Lex();
1317 break;
1318 }
1319 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001320 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001321 Lex.Lex(); // eat the '['
1322 std::vector<unsigned> Ranges = ParseRangeList();
1323 if (Ranges.empty()) return 0;
1324
1325 Result = Result->convertInitListSlice(Ranges);
1326 if (Result == 0) {
1327 Error(SquareLoc, "Invalid range for list slice");
1328 return 0;
1329 }
1330
1331 // Eat the ']'.
1332 if (Lex.getCode() != tgtok::r_square) {
1333 TokError("expected ']' at end of list slice");
1334 return 0;
1335 }
1336 Lex.Lex();
1337 break;
1338 }
1339 case tgtok::period:
1340 if (Lex.Lex() != tgtok::Id) { // eat the .
1341 TokError("expected field identifier after '.'");
1342 return 0;
1343 }
1344 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001345 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001346 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001347 return 0;
1348 }
1349 Result = new FieldInit(Result, Lex.getCurStrVal());
1350 Lex.Lex(); // eat field name
1351 break;
1352 }
1353 }
1354}
1355
1356/// ParseDagArgList - Parse the argument list for a dag literal expression.
1357///
1358/// ParseDagArgList ::= Value (':' VARNAME)?
1359/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
1360std::vector<std::pair<llvm::Init*, std::string> >
1361TGParser::ParseDagArgList(Record *CurRec) {
1362 std::vector<std::pair<llvm::Init*, std::string> > Result;
1363
1364 while (1) {
1365 Init *Val = ParseValue(CurRec);
1366 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
1367
1368 // If the variable name is present, add it.
1369 std::string VarName;
1370 if (Lex.getCode() == tgtok::colon) {
1371 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1372 TokError("expected variable name in dag literal");
1373 return std::vector<std::pair<llvm::Init*, std::string> >();
1374 }
1375 VarName = Lex.getCurStrVal();
1376 Lex.Lex(); // eat the VarName.
1377 }
1378
1379 Result.push_back(std::make_pair(Val, VarName));
1380
1381 if (Lex.getCode() != tgtok::comma) break;
1382 Lex.Lex(); // eat the ','
1383 }
1384
1385 return Result;
1386}
1387
1388
1389/// ParseValueList - Parse a comma separated list of values, returning them as a
1390/// vector. Note that this always expects to be able to parse at least one
1391/// value. It returns an empty list if this is not possible.
1392///
1393/// ValueList ::= Value (',' Value)
1394///
David Greenee1b46912009-06-08 20:23:18 +00001395std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001396 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001397 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001398 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001399 if (ArgsRec != 0 && EltTy == 0) {
1400 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1401 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1402 assert(RV && "Template argument record not found??");
1403 ItemType = RV->getType();
1404 ++ArgN;
1405 }
1406 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 if (Result.back() == 0) return std::vector<Init*>();
1408
1409 while (Lex.getCode() == tgtok::comma) {
1410 Lex.Lex(); // Eat the comma
1411
David Greenee1b46912009-06-08 20:23:18 +00001412 if (ArgsRec != 0 && EltTy == 0) {
1413 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001414 if (ArgN >= TArgs.size()) {
1415 TokError("too many template arguments");
1416 return std::vector<Init*>();
1417 }
David Greenee1b46912009-06-08 20:23:18 +00001418 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1419 assert(RV && "Template argument record not found??");
1420 ItemType = RV->getType();
1421 ++ArgN;
1422 }
1423 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001424 if (Result.back() == 0) return std::vector<Init*>();
1425 }
1426
1427 return Result;
1428}
1429
1430
1431
1432/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1433/// empty string on error. This can happen in a number of different context's,
1434/// including within a def or in the template args for a def (which which case
1435/// CurRec will be non-null) and within the template args for a multiclass (in
1436/// which case CurRec will be null, but CurMultiClass will be set). This can
1437/// also happen within a def that is within a multiclass, which will set both
1438/// CurRec and CurMultiClass.
1439///
1440/// Declaration ::= FIELD? Type ID ('=' Value)?
1441///
1442std::string TGParser::ParseDeclaration(Record *CurRec,
1443 bool ParsingTemplateArgs) {
1444 // Read the field prefix if present.
1445 bool HasField = Lex.getCode() == tgtok::Field;
1446 if (HasField) Lex.Lex();
1447
1448 RecTy *Type = ParseType();
1449 if (Type == 0) return "";
1450
1451 if (Lex.getCode() != tgtok::Id) {
1452 TokError("Expected identifier in declaration");
1453 return "";
1454 }
1455
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001456 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001457 std::string DeclName = Lex.getCurStrVal();
1458 Lex.Lex();
1459
1460 if (ParsingTemplateArgs) {
1461 if (CurRec) {
1462 DeclName = CurRec->getName() + ":" + DeclName;
1463 } else {
1464 assert(CurMultiClass);
1465 }
1466 if (CurMultiClass)
1467 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1468 }
1469
1470 // Add the value.
1471 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1472 return "";
1473
1474 // If a value is present, parse it.
1475 if (Lex.getCode() == tgtok::equal) {
1476 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001477 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001478 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001479 if (Val == 0 ||
1480 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1481 return "";
1482 }
1483
1484 return DeclName;
1485}
1486
1487/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1488/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1489/// template args for a def, which may or may not be in a multiclass. If null,
1490/// these are the template args for a multiclass.
1491///
1492/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1493///
1494bool TGParser::ParseTemplateArgList(Record *CurRec) {
1495 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1496 Lex.Lex(); // eat the '<'
1497
1498 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1499
1500 // Read the first declaration.
1501 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1502 if (TemplArg.empty())
1503 return true;
1504
1505 TheRecToAddTo->addTemplateArg(TemplArg);
1506
1507 while (Lex.getCode() == tgtok::comma) {
1508 Lex.Lex(); // eat the ','
1509
1510 // Read the following declarations.
1511 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1512 if (TemplArg.empty())
1513 return true;
1514 TheRecToAddTo->addTemplateArg(TemplArg);
1515 }
1516
1517 if (Lex.getCode() != tgtok::greater)
1518 return TokError("expected '>' at end of template argument list");
1519 Lex.Lex(); // eat the '>'.
1520 return false;
1521}
1522
1523
1524/// ParseBodyItem - Parse a single item at within the body of a def or class.
1525///
1526/// BodyItem ::= Declaration ';'
1527/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1528bool TGParser::ParseBodyItem(Record *CurRec) {
1529 if (Lex.getCode() != tgtok::Let) {
1530 if (ParseDeclaration(CurRec, false).empty())
1531 return true;
1532
1533 if (Lex.getCode() != tgtok::semi)
1534 return TokError("expected ';' after declaration");
1535 Lex.Lex();
1536 return false;
1537 }
1538
1539 // LET ID OptionalRangeList '=' Value ';'
1540 if (Lex.Lex() != tgtok::Id)
1541 return TokError("expected field identifier after let");
1542
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001543 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001544 std::string FieldName = Lex.getCurStrVal();
1545 Lex.Lex(); // eat the field name.
1546
1547 std::vector<unsigned> BitList;
1548 if (ParseOptionalBitList(BitList))
1549 return true;
1550 std::reverse(BitList.begin(), BitList.end());
1551
1552 if (Lex.getCode() != tgtok::equal)
1553 return TokError("expected '=' in let expression");
1554 Lex.Lex(); // eat the '='.
1555
David Greenee1b46912009-06-08 20:23:18 +00001556 RecordVal *Field = CurRec->getValue(FieldName);
1557 if (Field == 0)
1558 return TokError("Value '" + FieldName + "' unknown!");
1559
1560 RecTy *Type = Field->getType();
1561
1562 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001563 if (Val == 0) return true;
1564
1565 if (Lex.getCode() != tgtok::semi)
1566 return TokError("expected ';' after let expression");
1567 Lex.Lex();
1568
1569 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1570}
1571
1572/// ParseBody - Read the body of a class or def. Return true on error, false on
1573/// success.
1574///
1575/// Body ::= ';'
1576/// Body ::= '{' BodyList '}'
1577/// BodyList BodyItem*
1578///
1579bool TGParser::ParseBody(Record *CurRec) {
1580 // If this is a null definition, just eat the semi and return.
1581 if (Lex.getCode() == tgtok::semi) {
1582 Lex.Lex();
1583 return false;
1584 }
1585
1586 if (Lex.getCode() != tgtok::l_brace)
1587 return TokError("Expected ';' or '{' to start body");
1588 // Eat the '{'.
1589 Lex.Lex();
1590
1591 while (Lex.getCode() != tgtok::r_brace)
1592 if (ParseBodyItem(CurRec))
1593 return true;
1594
1595 // Eat the '}'.
1596 Lex.Lex();
1597 return false;
1598}
1599
1600/// ParseObjectBody - Parse the body of a def or class. This consists of an
1601/// optional ClassList followed by a Body. CurRec is the current def or class
1602/// that is being parsed.
1603///
1604/// ObjectBody ::= BaseClassList Body
1605/// BaseClassList ::= /*empty*/
1606/// BaseClassList ::= ':' BaseClassListNE
1607/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1608///
1609bool TGParser::ParseObjectBody(Record *CurRec) {
1610 // If there is a baseclass list, read it.
1611 if (Lex.getCode() == tgtok::colon) {
1612 Lex.Lex();
1613
1614 // Read all of the subclasses.
1615 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1616 while (1) {
1617 // Check for error.
1618 if (SubClass.Rec == 0) return true;
1619
1620 // Add it.
1621 if (AddSubClass(CurRec, SubClass))
1622 return true;
1623
1624 if (Lex.getCode() != tgtok::comma) break;
1625 Lex.Lex(); // eat ','.
1626 SubClass = ParseSubClassReference(CurRec, false);
1627 }
1628 }
1629
1630 // Process any variables on the let stack.
1631 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1632 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1633 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1634 LetStack[i][j].Bits, LetStack[i][j].Value))
1635 return true;
1636
1637 return ParseBody(CurRec);
1638}
1639
1640
1641/// ParseDef - Parse and return a top level or multiclass def, return the record
1642/// corresponding to it. This returns null on error.
1643///
1644/// DefInst ::= DEF ObjectName ObjectBody
1645///
1646llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001647 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001648 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1649 Lex.Lex(); // Eat the 'def' token.
1650
1651 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001652 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001653
1654 if (!CurMultiClass) {
1655 // Top-level def definition.
1656
1657 // Ensure redefinition doesn't happen.
1658 if (Records.getDef(CurRec->getName())) {
1659 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1660 return 0;
1661 }
1662 Records.addDef(CurRec);
1663 } else {
1664 // Otherwise, a def inside a multiclass, add it to the multiclass.
1665 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1666 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1667 Error(DefLoc, "def '" + CurRec->getName() +
1668 "' already defined in this multiclass!");
1669 return 0;
1670 }
1671 CurMultiClass->DefPrototypes.push_back(CurRec);
1672 }
1673
1674 if (ParseObjectBody(CurRec))
1675 return 0;
1676
1677 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1678 CurRec->resolveReferences();
1679
1680 // If ObjectBody has template arguments, it's an error.
1681 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1682 return CurRec;
1683}
1684
1685
1686/// ParseClass - Parse a tblgen class definition.
1687///
1688/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1689///
1690bool TGParser::ParseClass() {
1691 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1692 Lex.Lex();
1693
1694 if (Lex.getCode() != tgtok::Id)
1695 return TokError("expected class name after 'class' keyword");
1696
1697 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1698 if (CurRec) {
1699 // If the body was previously defined, this is an error.
1700 if (!CurRec->getValues().empty() ||
1701 !CurRec->getSuperClasses().empty() ||
1702 !CurRec->getTemplateArgs().empty())
1703 return TokError("Class '" + CurRec->getName() + "' already defined");
1704 } else {
1705 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001706 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001707 Records.addClass(CurRec);
1708 }
1709 Lex.Lex(); // eat the name.
1710
1711 // If there are template args, parse them.
1712 if (Lex.getCode() == tgtok::less)
1713 if (ParseTemplateArgList(CurRec))
1714 return true;
1715
1716 // Finally, parse the object body.
1717 return ParseObjectBody(CurRec);
1718}
1719
1720/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1721/// of LetRecords.
1722///
1723/// LetList ::= LetItem (',' LetItem)*
1724/// LetItem ::= ID OptionalRangeList '=' Value
1725///
1726std::vector<LetRecord> TGParser::ParseLetList() {
1727 std::vector<LetRecord> Result;
1728
1729 while (1) {
1730 if (Lex.getCode() != tgtok::Id) {
1731 TokError("expected identifier in let definition");
1732 return std::vector<LetRecord>();
1733 }
1734 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001735 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001736 Lex.Lex(); // Eat the identifier.
1737
1738 // Check for an optional RangeList.
1739 std::vector<unsigned> Bits;
1740 if (ParseOptionalRangeList(Bits))
1741 return std::vector<LetRecord>();
1742 std::reverse(Bits.begin(), Bits.end());
1743
1744 if (Lex.getCode() != tgtok::equal) {
1745 TokError("expected '=' in let expression");
1746 return std::vector<LetRecord>();
1747 }
1748 Lex.Lex(); // eat the '='.
1749
1750 Init *Val = ParseValue(0);
1751 if (Val == 0) return std::vector<LetRecord>();
1752
1753 // Now that we have everything, add the record.
1754 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1755
1756 if (Lex.getCode() != tgtok::comma)
1757 return Result;
1758 Lex.Lex(); // eat the comma.
1759 }
1760}
1761
1762/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1763/// different related productions.
1764///
1765/// Object ::= LET LetList IN '{' ObjectList '}'
1766/// Object ::= LET LetList IN Object
1767///
1768bool TGParser::ParseTopLevelLet() {
1769 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1770 Lex.Lex();
1771
1772 // Add this entry to the let stack.
1773 std::vector<LetRecord> LetInfo = ParseLetList();
1774 if (LetInfo.empty()) return true;
1775 LetStack.push_back(LetInfo);
1776
1777 if (Lex.getCode() != tgtok::In)
1778 return TokError("expected 'in' at end of top-level 'let'");
1779 Lex.Lex();
1780
1781 // If this is a scalar let, just handle it now
1782 if (Lex.getCode() != tgtok::l_brace) {
1783 // LET LetList IN Object
1784 if (ParseObject())
1785 return true;
1786 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001787 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001788 // Otherwise, this is a group let.
1789 Lex.Lex(); // eat the '{'.
1790
1791 // Parse the object list.
1792 if (ParseObjectList())
1793 return true;
1794
1795 if (Lex.getCode() != tgtok::r_brace) {
1796 TokError("expected '}' at end of top level let command");
1797 return Error(BraceLoc, "to match this '{'");
1798 }
1799 Lex.Lex();
1800 }
1801
1802 // Outside this let scope, this let block is not active.
1803 LetStack.pop_back();
1804 return false;
1805}
1806
1807/// ParseMultiClassDef - Parse a def in a multiclass context.
1808///
1809/// MultiClassDef ::= DefInst
1810///
1811bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1812 if (Lex.getCode() != tgtok::Def)
1813 return TokError("expected 'def' in multiclass body");
1814
1815 Record *D = ParseDef(CurMC);
1816 if (D == 0) return true;
1817
1818 // Copy the template arguments for the multiclass into the def.
1819 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1820
1821 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1822 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1823 assert(RV && "Template arg doesn't exist?");
1824 D->addValue(*RV);
1825 }
1826
1827 return false;
1828}
1829
1830/// ParseMultiClass - Parse a multiclass definition.
1831///
Bob Wilson32558652009-04-28 19:41:44 +00001832/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1833/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001834///
1835bool TGParser::ParseMultiClass() {
1836 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1837 Lex.Lex(); // Eat the multiclass token.
1838
1839 if (Lex.getCode() != tgtok::Id)
1840 return TokError("expected identifier after multiclass for name");
1841 std::string Name = Lex.getCurStrVal();
1842
1843 if (MultiClasses.count(Name))
1844 return TokError("multiclass '" + Name + "' already defined");
1845
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001846 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001847 Lex.Lex(); // Eat the identifier.
1848
1849 // If there are template args, parse them.
1850 if (Lex.getCode() == tgtok::less)
1851 if (ParseTemplateArgList(0))
1852 return true;
1853
David Greened34a73b2009-04-24 16:55:41 +00001854 bool inherits = false;
1855
David Greenede444af2009-04-22 16:42:54 +00001856 // If there are submulticlasses, parse them.
1857 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001858 inherits = true;
1859
David Greenede444af2009-04-22 16:42:54 +00001860 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001861
David Greenede444af2009-04-22 16:42:54 +00001862 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001863 SubMultiClassReference SubMultiClass =
1864 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001865 while (1) {
1866 // Check for error.
1867 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001868
David Greenede444af2009-04-22 16:42:54 +00001869 // Add it.
1870 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1871 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001872
David Greenede444af2009-04-22 16:42:54 +00001873 if (Lex.getCode() != tgtok::comma) break;
1874 Lex.Lex(); // eat ','.
1875 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1876 }
1877 }
1878
David Greened34a73b2009-04-24 16:55:41 +00001879 if (Lex.getCode() != tgtok::l_brace) {
1880 if (!inherits)
1881 return TokError("expected '{' in multiclass definition");
1882 else
1883 if (Lex.getCode() != tgtok::semi)
1884 return TokError("expected ';' in multiclass definition");
1885 else
1886 Lex.Lex(); // eat the ';'.
1887 }
1888 else {
1889 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1890 return TokError("multiclass must contain at least one def");
Chris Lattnerf4601652007-11-22 20:49:04 +00001891
David Greened34a73b2009-04-24 16:55:41 +00001892 while (Lex.getCode() != tgtok::r_brace)
1893 if (ParseMultiClassDef(CurMultiClass))
1894 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001895
David Greened34a73b2009-04-24 16:55:41 +00001896 Lex.Lex(); // eat the '}'.
1897 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001898
1899 CurMultiClass = 0;
1900 return false;
1901}
1902
1903/// ParseDefm - Parse the instantiation of a multiclass.
1904///
1905/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1906///
1907bool TGParser::ParseDefm() {
1908 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1909 if (Lex.Lex() != tgtok::Id) // eat the defm.
1910 return TokError("expected identifier after defm");
1911
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001912 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001913 std::string DefmPrefix = Lex.getCurStrVal();
1914 if (Lex.Lex() != tgtok::colon)
1915 return TokError("expected ':' after defm identifier");
1916
1917 // eat the colon.
1918 Lex.Lex();
1919
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001920 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001921 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001922
1923 while (1) {
1924 if (Ref.Rec == 0) return true;
1925
1926 // To instantiate a multiclass, we need to first get the multiclass, then
1927 // instantiate each def contained in the multiclass with the SubClassRef
1928 // template parameters.
1929 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1930 assert(MC && "Didn't lookup multiclass correctly?");
1931 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1932
1933 // Verify that the correct number of template arguments were specified.
1934 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1935 if (TArgs.size() < TemplateVals.size())
1936 return Error(SubClassLoc,
1937 "more template args specified than multiclass expects");
1938
1939 // Loop over all the def's in the multiclass, instantiating each one.
1940 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1941 Record *DefProto = MC->DefPrototypes[i];
1942
David Greene065f2592009-05-05 16:28:25 +00001943 // Add in the defm name
1944 std::string DefName = DefProto->getName();
1945 std::string::size_type idx = DefName.find("#NAME#");
1946 if (idx != std::string::npos) {
1947 DefName.replace(idx, 6, DefmPrefix);
1948 }
1949 else {
1950 // Add the suffix to the defm name to get the new name.
1951 DefName = DefmPrefix + DefName;
1952 }
1953
1954 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001955
1956 SubClassReference Ref;
1957 Ref.RefLoc = DefmPrefixLoc;
1958 Ref.Rec = DefProto;
1959 AddSubClass(CurRec, Ref);
1960
1961 // Loop over all of the template arguments, setting them to the specified
1962 // value or leaving them as the default if necessary.
1963 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001964 // Check if a value is specified for this temp-arg.
1965 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001966 // Set it now.
1967 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1968 TemplateVals[i]))
1969 return true;
1970
1971 // Resolve it next.
1972 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1973
1974 // Now remove it.
1975 CurRec->removeValue(TArgs[i]);
1976
1977 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001978 return Error(SubClassLoc,
1979 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001980 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1981 MC->Rec.getName() + "'");
1982 }
1983 }
1984
1985 // If the mdef is inside a 'let' expression, add to each def.
1986 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1987 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1988 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1989 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1990 Error(DefmPrefixLoc, "when instantiating this defm");
1991 return true;
1992 }
1993
1994 // Ensure redefinition doesn't happen.
1995 if (Records.getDef(CurRec->getName()))
1996 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1997 "' already defined, instantiating defm with subdef '" +
1998 DefProto->getName() + "'");
1999 Records.addDef(CurRec);
2000 CurRec->resolveReferences();
2001 }
2002
2003 if (Lex.getCode() != tgtok::comma) break;
2004 Lex.Lex(); // eat ','.
2005
2006 SubClassLoc = Lex.getLoc();
2007 Ref = ParseSubClassReference(0, true);
2008 }
2009
Chris Lattnerf4601652007-11-22 20:49:04 +00002010 if (Lex.getCode() != tgtok::semi)
2011 return TokError("expected ';' at end of defm");
2012 Lex.Lex();
2013
Chris Lattnerf4601652007-11-22 20:49:04 +00002014 return false;
2015}
2016
2017/// ParseObject
2018/// Object ::= ClassInst
2019/// Object ::= DefInst
2020/// Object ::= MultiClassInst
2021/// Object ::= DefMInst
2022/// Object ::= LETCommand '{' ObjectList '}'
2023/// Object ::= LETCommand Object
2024bool TGParser::ParseObject() {
2025 switch (Lex.getCode()) {
2026 default: assert(0 && "This is not an object");
2027 case tgtok::Let: return ParseTopLevelLet();
2028 case tgtok::Def: return ParseDef(0) == 0;
2029 case tgtok::Defm: return ParseDefm();
2030 case tgtok::Class: return ParseClass();
2031 case tgtok::MultiClass: return ParseMultiClass();
2032 }
2033}
2034
2035/// ParseObjectList
2036/// ObjectList :== Object*
2037bool TGParser::ParseObjectList() {
2038 while (isObjectStart(Lex.getCode())) {
2039 if (ParseObject())
2040 return true;
2041 }
2042 return false;
2043}
2044
2045
2046bool TGParser::ParseFile() {
2047 Lex.Lex(); // Prime the lexer.
2048 if (ParseObjectList()) return true;
2049
2050 // If we have unread input at the end of the file, report it.
2051 if (Lex.getCode() == tgtok::Eof)
2052 return false;
2053
2054 return TokError("Unexpected input at top level");
2055}
2056