• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the Parser for TableGen.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TGParser.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/TableGen/Record.h"
19 #include <algorithm>
20 using namespace llvm;
21 
22 //===----------------------------------------------------------------------===//
23 // Support Code for the Semantic Actions.
24 //===----------------------------------------------------------------------===//
25 
26 namespace llvm {
27 struct SubClassReference {
28   SMRange RefRange;
29   Record *Rec;
30   std::vector<Init*> TemplateArgs;
SubClassReferencellvm::SubClassReference31   SubClassReference() : Rec(nullptr) {}
32 
isInvalidllvm::SubClassReference33   bool isInvalid() const { return Rec == nullptr; }
34 };
35 
36 struct SubMultiClassReference {
37   SMRange RefRange;
38   MultiClass *MC;
39   std::vector<Init*> TemplateArgs;
SubMultiClassReferencellvm::SubMultiClassReference40   SubMultiClassReference() : MC(nullptr) {}
41 
isInvalidllvm::SubMultiClassReference42   bool isInvalid() const { return MC == nullptr; }
43   void dump() const;
44 };
45 
dump() const46 LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
47   errs() << "Multiclass:\n";
48 
49   MC->dump();
50 
51   errs() << "Template args:\n";
52   for (Init *TA : TemplateArgs)
53     TA->dump();
54 }
55 
56 } // end namespace llvm
57 
AddValue(Record * CurRec,SMLoc Loc,const RecordVal & RV)58 bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
59   if (!CurRec)
60     CurRec = &CurMultiClass->Rec;
61 
62   if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
63     // The value already exists in the class, treat this as a set.
64     if (ERV->setValue(RV.getValue()))
65       return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
66                    RV.getType()->getAsString() + "' is incompatible with " +
67                    "previous definition of type '" +
68                    ERV->getType()->getAsString() + "'");
69   } else {
70     CurRec->addValue(RV);
71   }
72   return false;
73 }
74 
75 /// SetValue -
76 /// Return true on error, false on success.
SetValue(Record * CurRec,SMLoc Loc,Init * ValName,ArrayRef<unsigned> BitList,Init * V,bool AllowSelfAssignment)77 bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
78                         ArrayRef<unsigned> BitList, Init *V,
79                         bool AllowSelfAssignment) {
80   if (!V) return false;
81 
82   if (!CurRec) CurRec = &CurMultiClass->Rec;
83 
84   RecordVal *RV = CurRec->getValue(ValName);
85   if (!RV)
86     return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
87                  "' unknown!");
88 
89   // Do not allow assignments like 'X = X'.  This will just cause infinite loops
90   // in the resolution machinery.
91   if (BitList.empty())
92     if (VarInit *VI = dyn_cast<VarInit>(V))
93       if (VI->getNameInit() == ValName && !AllowSelfAssignment)
94         return true;
95 
96   // If we are assigning to a subset of the bits in the value... then we must be
97   // assigning to a field of BitsRecTy, which must have a BitsInit
98   // initializer.
99   //
100   if (!BitList.empty()) {
101     BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
102     if (!CurVal)
103       return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
104                    "' is not a bits type");
105 
106     // Convert the incoming value to a bits type of the appropriate size...
107     Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size()));
108     if (!BI)
109       return Error(Loc, "Initializer is not compatible with bit range");
110 
111     // We should have a BitsInit type now.
112     BitsInit *BInit = cast<BitsInit>(BI);
113 
114     SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
115 
116     // Loop over bits, assigning values as appropriate.
117     for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
118       unsigned Bit = BitList[i];
119       if (NewBits[Bit])
120         return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
121                      ValName->getAsUnquotedString() + "' more than once");
122       NewBits[Bit] = BInit->getBit(i);
123     }
124 
125     for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
126       if (!NewBits[i])
127         NewBits[i] = CurVal->getBit(i);
128 
129     V = BitsInit::get(NewBits);
130   }
131 
132   if (RV->setValue(V)) {
133     std::string InitType = "";
134     if (BitsInit *BI = dyn_cast<BitsInit>(V))
135       InitType = (Twine("' of type bit initializer with length ") +
136                   Twine(BI->getNumBits())).str();
137     return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
138                  "' of type '" + RV->getType()->getAsString() +
139                  "' is incompatible with initializer '" + V->getAsString() +
140                  InitType + "'");
141   }
142   return false;
143 }
144 
145 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
146 /// args as SubClass's template arguments.
AddSubClass(Record * CurRec,SubClassReference & SubClass)147 bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
148   Record *SC = SubClass.Rec;
149   // Add all of the values in the subclass into the current class.
150   for (const RecordVal &Val : SC->getValues())
151     if (AddValue(CurRec, SubClass.RefRange.Start, Val))
152       return true;
153 
154   ArrayRef<Init *> 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.RefRange.Start,
159                  "More template args specified than expected");
160 
161   // Loop over all of the template arguments, setting them to the specified
162   // value or leaving them as the default if necessary.
163   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
164     if (i < SubClass.TemplateArgs.size()) {
165       // If a value is specified for this template arg, set it now.
166       if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
167                    None, SubClass.TemplateArgs[i]))
168         return true;
169 
170       // Resolve it next.
171       CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
172 
173       // Now remove it.
174       CurRec->removeValue(TArgs[i]);
175 
176     } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
177       return Error(SubClass.RefRange.Start,
178                    "Value not specified for template argument #" +
179                    Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
180                    ") of subclass '" + SC->getNameInitAsString() + "'!");
181     }
182   }
183 
184   // Since everything went well, we can now set the "superclass" list for the
185   // current record.
186   ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
187   for (const auto &SCPair : SCs) {
188     if (CurRec->isSubClassOf(SCPair.first))
189       return Error(SubClass.RefRange.Start,
190                    "Already subclass of '" + SCPair.first->getName() + "'!\n");
191     CurRec->addSuperClass(SCPair.first, SCPair.second);
192   }
193 
194   if (CurRec->isSubClassOf(SC))
195     return Error(SubClass.RefRange.Start,
196                  "Already subclass of '" + SC->getName() + "'!\n");
197   CurRec->addSuperClass(SC, SubClass.RefRange);
198   return false;
199 }
200 
201 /// AddSubMultiClass - Add SubMultiClass as a subclass to
202 /// CurMC, resolving its template args as SubMultiClass's
203 /// template arguments.
AddSubMultiClass(MultiClass * CurMC,SubMultiClassReference & SubMultiClass)204 bool TGParser::AddSubMultiClass(MultiClass *CurMC,
205                                 SubMultiClassReference &SubMultiClass) {
206   MultiClass *SMC = SubMultiClass.MC;
207   Record *CurRec = &CurMC->Rec;
208 
209   // Add all of the values in the subclass into the current class.
210   for (const auto &SMCVal : SMC->Rec.getValues())
211     if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
212       return true;
213 
214   unsigned newDefStart = CurMC->DefPrototypes.size();
215 
216   // Add all of the defs in the subclass into the current multiclass.
217   for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
218     // Clone the def and add it to the current multiclass
219     auto NewDef = make_unique<Record>(*R);
220 
221     // Add all of the values in the superclass into the current def.
222     for (const auto &MCVal : CurRec->getValues())
223       if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
224         return true;
225 
226     CurMC->DefPrototypes.push_back(std::move(NewDef));
227   }
228 
229   ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
230 
231   // Ensure that an appropriate number of template arguments are
232   // specified.
233   if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
234     return Error(SubMultiClass.RefRange.Start,
235                  "More template args specified than expected");
236 
237   // Loop over all of the template arguments, setting them to the specified
238   // value or leaving them as the default if necessary.
239   for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
240     if (i < SubMultiClass.TemplateArgs.size()) {
241       // If a value is specified for this template arg, set it in the
242       // superclass now.
243       if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
244                    None, SubMultiClass.TemplateArgs[i]))
245         return true;
246 
247       // Resolve it next.
248       CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
249 
250       // Now remove it.
251       CurRec->removeValue(SMCTArgs[i]);
252 
253       // If a value is specified for this template arg, set it in the
254       // new defs now.
255       for (const auto &Def :
256              makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
257         if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
258                      None, SubMultiClass.TemplateArgs[i]))
259           return true;
260 
261         // Resolve it next.
262         Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
263 
264         // Now remove it
265         Def->removeValue(SMCTArgs[i]);
266       }
267     } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
268       return Error(SubMultiClass.RefRange.Start,
269                    "Value not specified for template argument #" +
270                    Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
271                    ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
272     }
273   }
274 
275   return false;
276 }
277 
278 /// ProcessForeachDefs - Given a record, apply all of the variable
279 /// values in all surrounding foreach loops, creating new records for
280 /// each combination of values.
ProcessForeachDefs(Record * CurRec,SMLoc Loc)281 bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) {
282   if (Loops.empty())
283     return false;
284 
285   // We want to instantiate a new copy of CurRec for each combination
286   // of nested loop iterator values.  We don't want top instantiate
287   // any copies until we have values for each loop iterator.
288   IterSet IterVals;
289   return ProcessForeachDefs(CurRec, Loc, IterVals);
290 }
291 
292 /// ProcessForeachDefs - Given a record, a loop and a loop iterator,
293 /// apply each of the variable values in this loop and then process
294 /// subloops.
ProcessForeachDefs(Record * CurRec,SMLoc Loc,IterSet & IterVals)295 bool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){
296   // Recursively build a tuple of iterator values.
297   if (IterVals.size() != Loops.size()) {
298     assert(IterVals.size() < Loops.size());
299     ForeachLoop &CurLoop = Loops[IterVals.size()];
300     ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue);
301     if (!List) {
302       Error(Loc, "Loop list is not a list");
303       return true;
304     }
305 
306     // Process each value.
307     for (unsigned i = 0; i < List->size(); ++i) {
308       Init *ItemVal = List->resolveListElementReference(*CurRec, nullptr, i);
309       IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
310       if (ProcessForeachDefs(CurRec, Loc, IterVals))
311         return true;
312       IterVals.pop_back();
313     }
314     return false;
315   }
316 
317   // This is the bottom of the recursion. We have all of the iterator values
318   // for this point in the iteration space.  Instantiate a new record to
319   // reflect this combination of values.
320   auto IterRec = make_unique<Record>(*CurRec);
321 
322   // Set the iterator values now.
323   for (IterRecord &IR : IterVals) {
324     VarInit *IterVar = IR.IterVar;
325     TypedInit *IVal = dyn_cast<TypedInit>(IR.IterValue);
326     if (!IVal)
327       return Error(Loc, "foreach iterator value is untyped");
328 
329     IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false));
330 
331     if (SetValue(IterRec.get(), Loc, IterVar->getName(), None, IVal))
332       return Error(Loc, "when instantiating this def");
333 
334     // Resolve it next.
335     IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName()));
336 
337     // Remove it.
338     IterRec->removeValue(IterVar->getName());
339   }
340 
341   if (Records.getDef(IterRec->getNameInitAsString())) {
342     // If this record is anonymous, it's no problem, just generate a new name
343     if (!IterRec->isAnonymous())
344       return Error(Loc, "def already exists: " +IterRec->getNameInitAsString());
345 
346     IterRec->setName(GetNewAnonymousName());
347   }
348 
349   Record *IterRecSave = IterRec.get(); // Keep a copy before release.
350   Records.addDef(std::move(IterRec));
351   IterRecSave->resolveReferences();
352   return false;
353 }
354 
355 //===----------------------------------------------------------------------===//
356 // Parser Code
357 //===----------------------------------------------------------------------===//
358 
359 /// isObjectStart - Return true if this is a valid first token for an Object.
isObjectStart(tgtok::TokKind K)360 static bool isObjectStart(tgtok::TokKind K) {
361   return K == tgtok::Class || K == tgtok::Def ||
362          K == tgtok::Defm || K == tgtok::Let ||
363          K == tgtok::MultiClass || K == tgtok::Foreach;
364 }
365 
366 /// GetNewAnonymousName - Generate a unique anonymous name that can be used as
367 /// an identifier.
GetNewAnonymousName()368 std::string TGParser::GetNewAnonymousName() {
369   return "anonymous_" + utostr(AnonCounter++);
370 }
371 
372 /// ParseObjectName - If an object name is specified, return it.  Otherwise,
373 /// return 0.
374 ///   ObjectName ::= Value [ '#' Value ]*
375 ///   ObjectName ::= /*empty*/
376 ///
ParseObjectName(MultiClass * CurMultiClass)377 Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
378   switch (Lex.getCode()) {
379   case tgtok::colon:
380   case tgtok::semi:
381   case tgtok::l_brace:
382     // These are all of the tokens that can begin an object body.
383     // Some of these can also begin values but we disallow those cases
384     // because they are unlikely to be useful.
385     return nullptr;
386   default:
387     break;
388   }
389 
390   Record *CurRec = nullptr;
391   if (CurMultiClass)
392     CurRec = &CurMultiClass->Rec;
393 
394   RecTy *Type = nullptr;
395   if (CurRec) {
396     const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit());
397     if (!CurRecName) {
398       TokError("Record name is not typed!");
399       return nullptr;
400     }
401     Type = CurRecName->getType();
402   }
403 
404   return ParseValue(CurRec, Type, ParseNameMode);
405 }
406 
407 /// ParseClassID - Parse and resolve a reference to a class name.  This returns
408 /// null on error.
409 ///
410 ///    ClassID ::= ID
411 ///
ParseClassID()412 Record *TGParser::ParseClassID() {
413   if (Lex.getCode() != tgtok::Id) {
414     TokError("expected name for ClassID");
415     return nullptr;
416   }
417 
418   Record *Result = Records.getClass(Lex.getCurStrVal());
419   if (!Result)
420     TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
421 
422   Lex.Lex();
423   return Result;
424 }
425 
426 /// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
427 /// This returns null on error.
428 ///
429 ///    MultiClassID ::= ID
430 ///
ParseMultiClassID()431 MultiClass *TGParser::ParseMultiClassID() {
432   if (Lex.getCode() != tgtok::Id) {
433     TokError("expected name for MultiClassID");
434     return nullptr;
435   }
436 
437   MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
438   if (!Result)
439     TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
440 
441   Lex.Lex();
442   return Result;
443 }
444 
445 /// ParseSubClassReference - Parse a reference to a subclass or to a templated
446 /// subclass.  This returns a SubClassRefTy with a null Record* on error.
447 ///
448 ///  SubClassRef ::= ClassID
449 ///  SubClassRef ::= ClassID '<' ValueList '>'
450 ///
451 SubClassReference TGParser::
ParseSubClassReference(Record * CurRec,bool isDefm)452 ParseSubClassReference(Record *CurRec, bool isDefm) {
453   SubClassReference Result;
454   Result.RefRange.Start = Lex.getLoc();
455 
456   if (isDefm) {
457     if (MultiClass *MC = ParseMultiClassID())
458       Result.Rec = &MC->Rec;
459   } else {
460     Result.Rec = ParseClassID();
461   }
462   if (!Result.Rec) return Result;
463 
464   // If there is no template arg list, we're done.
465   if (Lex.getCode() != tgtok::less) {
466     Result.RefRange.End = Lex.getLoc();
467     return Result;
468   }
469   Lex.Lex();  // Eat the '<'
470 
471   if (Lex.getCode() == tgtok::greater) {
472     TokError("subclass reference requires a non-empty list of template values");
473     Result.Rec = nullptr;
474     return Result;
475   }
476 
477   Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
478   if (Result.TemplateArgs.empty()) {
479     Result.Rec = nullptr;   // Error parsing value list.
480     return Result;
481   }
482 
483   if (Lex.getCode() != tgtok::greater) {
484     TokError("expected '>' in template value list");
485     Result.Rec = nullptr;
486     return Result;
487   }
488   Lex.Lex();
489   Result.RefRange.End = Lex.getLoc();
490 
491   return Result;
492 }
493 
494 /// ParseSubMultiClassReference - Parse a reference to a subclass or to a
495 /// templated submulticlass.  This returns a SubMultiClassRefTy with a null
496 /// Record* on error.
497 ///
498 ///  SubMultiClassRef ::= MultiClassID
499 ///  SubMultiClassRef ::= MultiClassID '<' ValueList '>'
500 ///
501 SubMultiClassReference TGParser::
ParseSubMultiClassReference(MultiClass * CurMC)502 ParseSubMultiClassReference(MultiClass *CurMC) {
503   SubMultiClassReference Result;
504   Result.RefRange.Start = Lex.getLoc();
505 
506   Result.MC = ParseMultiClassID();
507   if (!Result.MC) return Result;
508 
509   // If there is no template arg list, we're done.
510   if (Lex.getCode() != tgtok::less) {
511     Result.RefRange.End = Lex.getLoc();
512     return Result;
513   }
514   Lex.Lex();  // Eat the '<'
515 
516   if (Lex.getCode() == tgtok::greater) {
517     TokError("subclass reference requires a non-empty list of template values");
518     Result.MC = nullptr;
519     return Result;
520   }
521 
522   Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
523   if (Result.TemplateArgs.empty()) {
524     Result.MC = nullptr;   // Error parsing value list.
525     return Result;
526   }
527 
528   if (Lex.getCode() != tgtok::greater) {
529     TokError("expected '>' in template value list");
530     Result.MC = nullptr;
531     return Result;
532   }
533   Lex.Lex();
534   Result.RefRange.End = Lex.getLoc();
535 
536   return Result;
537 }
538 
539 /// ParseRangePiece - Parse a bit/value range.
540 ///   RangePiece ::= INTVAL
541 ///   RangePiece ::= INTVAL '-' INTVAL
542 ///   RangePiece ::= INTVAL INTVAL
ParseRangePiece(std::vector<unsigned> & Ranges)543 bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
544   if (Lex.getCode() != tgtok::IntVal) {
545     TokError("expected integer or bitrange");
546     return true;
547   }
548   int64_t Start = Lex.getCurIntVal();
549   int64_t End;
550 
551   if (Start < 0)
552     return TokError("invalid range, cannot be negative");
553 
554   switch (Lex.Lex()) {  // eat first character.
555   default:
556     Ranges.push_back(Start);
557     return false;
558   case tgtok::minus:
559     if (Lex.Lex() != tgtok::IntVal) {
560       TokError("expected integer value as end of range");
561       return true;
562     }
563     End = Lex.getCurIntVal();
564     break;
565   case tgtok::IntVal:
566     End = -Lex.getCurIntVal();
567     break;
568   }
569   if (End < 0)
570     return TokError("invalid range, cannot be negative");
571   Lex.Lex();
572 
573   // Add to the range.
574   if (Start < End)
575     for (; Start <= End; ++Start)
576       Ranges.push_back(Start);
577   else
578     for (; Start >= End; --Start)
579       Ranges.push_back(Start);
580   return false;
581 }
582 
583 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
584 ///
585 ///   RangeList ::= RangePiece (',' RangePiece)*
586 ///
ParseRangeList()587 std::vector<unsigned> TGParser::ParseRangeList() {
588   std::vector<unsigned> Result;
589 
590   // Parse the first piece.
591   if (ParseRangePiece(Result))
592     return std::vector<unsigned>();
593   while (Lex.getCode() == tgtok::comma) {
594     Lex.Lex();  // Eat the comma.
595 
596     // Parse the next range piece.
597     if (ParseRangePiece(Result))
598       return std::vector<unsigned>();
599   }
600   return Result;
601 }
602 
603 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
604 ///   OptionalRangeList ::= '<' RangeList '>'
605 ///   OptionalRangeList ::= /*empty*/
ParseOptionalRangeList(std::vector<unsigned> & Ranges)606 bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
607   if (Lex.getCode() != tgtok::less)
608     return false;
609 
610   SMLoc StartLoc = Lex.getLoc();
611   Lex.Lex(); // eat the '<'
612 
613   // Parse the range list.
614   Ranges = ParseRangeList();
615   if (Ranges.empty()) return true;
616 
617   if (Lex.getCode() != tgtok::greater) {
618     TokError("expected '>' at end of range list");
619     return Error(StartLoc, "to match this '<'");
620   }
621   Lex.Lex();   // eat the '>'.
622   return false;
623 }
624 
625 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
626 ///   OptionalBitList ::= '{' RangeList '}'
627 ///   OptionalBitList ::= /*empty*/
ParseOptionalBitList(std::vector<unsigned> & Ranges)628 bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
629   if (Lex.getCode() != tgtok::l_brace)
630     return false;
631 
632   SMLoc StartLoc = Lex.getLoc();
633   Lex.Lex(); // eat the '{'
634 
635   // Parse the range list.
636   Ranges = ParseRangeList();
637   if (Ranges.empty()) return true;
638 
639   if (Lex.getCode() != tgtok::r_brace) {
640     TokError("expected '}' at end of bit list");
641     return Error(StartLoc, "to match this '{'");
642   }
643   Lex.Lex();   // eat the '}'.
644   return false;
645 }
646 
647 
648 /// ParseType - Parse and return a tblgen type.  This returns null on error.
649 ///
650 ///   Type ::= STRING                       // string type
651 ///   Type ::= CODE                         // code type
652 ///   Type ::= BIT                          // bit type
653 ///   Type ::= BITS '<' INTVAL '>'          // bits<x> type
654 ///   Type ::= INT                          // int type
655 ///   Type ::= LIST '<' Type '>'            // list<x> type
656 ///   Type ::= DAG                          // dag type
657 ///   Type ::= ClassID                      // Record Type
658 ///
ParseType()659 RecTy *TGParser::ParseType() {
660   switch (Lex.getCode()) {
661   default: TokError("Unknown token when expecting a type"); return nullptr;
662   case tgtok::String: Lex.Lex(); return StringRecTy::get();
663   case tgtok::Code:   Lex.Lex(); return CodeRecTy::get();
664   case tgtok::Bit:    Lex.Lex(); return BitRecTy::get();
665   case tgtok::Int:    Lex.Lex(); return IntRecTy::get();
666   case tgtok::Dag:    Lex.Lex(); return DagRecTy::get();
667   case tgtok::Id:
668     if (Record *R = ParseClassID()) return RecordRecTy::get(R);
669     return nullptr;
670   case tgtok::Bits: {
671     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
672       TokError("expected '<' after bits type");
673       return nullptr;
674     }
675     if (Lex.Lex() != tgtok::IntVal) {  // Eat '<'
676       TokError("expected integer in bits<n> type");
677       return nullptr;
678     }
679     uint64_t Val = Lex.getCurIntVal();
680     if (Lex.Lex() != tgtok::greater) {  // Eat count.
681       TokError("expected '>' at end of bits<n> type");
682       return nullptr;
683     }
684     Lex.Lex();  // Eat '>'
685     return BitsRecTy::get(Val);
686   }
687   case tgtok::List: {
688     if (Lex.Lex() != tgtok::less) { // Eat 'bits'
689       TokError("expected '<' after list type");
690       return nullptr;
691     }
692     Lex.Lex();  // Eat '<'
693     RecTy *SubType = ParseType();
694     if (!SubType) return nullptr;
695 
696     if (Lex.getCode() != tgtok::greater) {
697       TokError("expected '>' at end of list<ty> type");
698       return nullptr;
699     }
700     Lex.Lex();  // Eat '>'
701     return ListRecTy::get(SubType);
702   }
703   }
704 }
705 
706 /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
707 /// has already been read.
ParseIDValue(Record * CurRec,const std::string & Name,SMLoc NameLoc,IDParseMode Mode)708 Init *TGParser::ParseIDValue(Record *CurRec,
709                              const std::string &Name, SMLoc NameLoc,
710                              IDParseMode Mode) {
711   if (CurRec) {
712     if (const RecordVal *RV = CurRec->getValue(Name))
713       return VarInit::get(Name, RV->getType());
714 
715     Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
716 
717     if (CurMultiClass)
718       TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
719                                     "::");
720 
721     if (CurRec->isTemplateArg(TemplateArgName)) {
722       const RecordVal *RV = CurRec->getValue(TemplateArgName);
723       assert(RV && "Template arg doesn't exist??");
724       return VarInit::get(TemplateArgName, RV->getType());
725     }
726   }
727 
728   if (CurMultiClass) {
729     Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
730                                "::");
731 
732     if (CurMultiClass->Rec.isTemplateArg(MCName)) {
733       const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
734       assert(RV && "Template arg doesn't exist??");
735       return VarInit::get(MCName, RV->getType());
736     }
737   }
738 
739   // If this is in a foreach loop, make sure it's not a loop iterator
740   for (const auto &L : Loops) {
741     VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
742     if (IterVar && IterVar->getName() == Name)
743       return IterVar;
744   }
745 
746   if (Mode == ParseNameMode)
747     return StringInit::get(Name);
748 
749   if (Record *D = Records.getDef(Name))
750     return DefInit::get(D);
751 
752   if (Mode == ParseValueMode) {
753     Error(NameLoc, "Variable not defined: '" + Name + "'");
754     return nullptr;
755   }
756 
757   return StringInit::get(Name);
758 }
759 
760 /// ParseOperation - Parse an operator.  This returns null on error.
761 ///
762 /// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
763 ///
ParseOperation(Record * CurRec,RecTy * ItemType)764 Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
765   switch (Lex.getCode()) {
766   default:
767     TokError("unknown operation");
768     return nullptr;
769   case tgtok::XHead:
770   case tgtok::XTail:
771   case tgtok::XEmpty:
772   case tgtok::XCast: {  // Value ::= !unop '(' Value ')'
773     UnOpInit::UnaryOp Code;
774     RecTy *Type = nullptr;
775 
776     switch (Lex.getCode()) {
777     default: llvm_unreachable("Unhandled code!");
778     case tgtok::XCast:
779       Lex.Lex();  // eat the operation
780       Code = UnOpInit::CAST;
781 
782       Type = ParseOperatorType();
783 
784       if (!Type) {
785         TokError("did not get type for unary operator");
786         return nullptr;
787       }
788 
789       break;
790     case tgtok::XHead:
791       Lex.Lex();  // eat the operation
792       Code = UnOpInit::HEAD;
793       break;
794     case tgtok::XTail:
795       Lex.Lex();  // eat the operation
796       Code = UnOpInit::TAIL;
797       break;
798     case tgtok::XEmpty:
799       Lex.Lex();  // eat the operation
800       Code = UnOpInit::EMPTY;
801       Type = IntRecTy::get();
802       break;
803     }
804     if (Lex.getCode() != tgtok::l_paren) {
805       TokError("expected '(' after unary operator");
806       return nullptr;
807     }
808     Lex.Lex();  // eat the '('
809 
810     Init *LHS = ParseValue(CurRec);
811     if (!LHS) return nullptr;
812 
813     if (Code == UnOpInit::HEAD ||
814         Code == UnOpInit::TAIL ||
815         Code == UnOpInit::EMPTY) {
816       ListInit *LHSl = dyn_cast<ListInit>(LHS);
817       StringInit *LHSs = dyn_cast<StringInit>(LHS);
818       TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
819       if (!LHSl && !LHSs && !LHSt) {
820         TokError("expected list or string type argument in unary operator");
821         return nullptr;
822       }
823       if (LHSt) {
824         ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
825         StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
826         if (!LType && !SType) {
827           TokError("expected list or string type argument in unary operator");
828           return nullptr;
829         }
830       }
831 
832       if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
833         if (!LHSl && !LHSt) {
834           TokError("expected list type argument in unary operator");
835           return nullptr;
836         }
837 
838         if (LHSl && LHSl->empty()) {
839           TokError("empty list argument in unary operator");
840           return nullptr;
841         }
842         if (LHSl) {
843           Init *Item = LHSl->getElement(0);
844           TypedInit *Itemt = dyn_cast<TypedInit>(Item);
845           if (!Itemt) {
846             TokError("untyped list element in unary operator");
847             return nullptr;
848           }
849           Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
850                                           : ListRecTy::get(Itemt->getType());
851         } else {
852           assert(LHSt && "expected list type argument in unary operator");
853           ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
854           if (!LType) {
855             TokError("expected list type argument in unary operator");
856             return nullptr;
857           }
858           Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
859         }
860       }
861     }
862 
863     if (Lex.getCode() != tgtok::r_paren) {
864       TokError("expected ')' in unary operator");
865       return nullptr;
866     }
867     Lex.Lex();  // eat the ')'
868     return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
869   }
870 
871   case tgtok::XConcat:
872   case tgtok::XADD:
873   case tgtok::XAND:
874   case tgtok::XSRA:
875   case tgtok::XSRL:
876   case tgtok::XSHL:
877   case tgtok::XEq:
878   case tgtok::XListConcat:
879   case tgtok::XStrConcat: {  // Value ::= !binop '(' Value ',' Value ')'
880     tgtok::TokKind OpTok = Lex.getCode();
881     SMLoc OpLoc = Lex.getLoc();
882     Lex.Lex();  // eat the operation
883 
884     BinOpInit::BinaryOp Code;
885     RecTy *Type = nullptr;
886 
887     switch (OpTok) {
888     default: llvm_unreachable("Unhandled code!");
889     case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break;
890     case tgtok::XADD:    Code = BinOpInit::ADD;   Type = IntRecTy::get(); break;
891     case tgtok::XAND:    Code = BinOpInit::AND;   Type = IntRecTy::get(); break;
892     case tgtok::XSRA:    Code = BinOpInit::SRA;   Type = IntRecTy::get(); break;
893     case tgtok::XSRL:    Code = BinOpInit::SRL;   Type = IntRecTy::get(); break;
894     case tgtok::XSHL:    Code = BinOpInit::SHL;   Type = IntRecTy::get(); break;
895     case tgtok::XEq:     Code = BinOpInit::EQ;    Type = BitRecTy::get(); break;
896     case tgtok::XListConcat:
897       Code = BinOpInit::LISTCONCAT;
898       // We don't know the list type until we parse the first argument
899       break;
900     case tgtok::XStrConcat:
901       Code = BinOpInit::STRCONCAT;
902       Type = StringRecTy::get();
903       break;
904     }
905 
906     if (Lex.getCode() != tgtok::l_paren) {
907       TokError("expected '(' after binary operator");
908       return nullptr;
909     }
910     Lex.Lex();  // eat the '('
911 
912     SmallVector<Init*, 2> InitList;
913 
914     InitList.push_back(ParseValue(CurRec));
915     if (!InitList.back()) return nullptr;
916 
917     while (Lex.getCode() == tgtok::comma) {
918       Lex.Lex();  // eat the ','
919 
920       InitList.push_back(ParseValue(CurRec));
921       if (!InitList.back()) return nullptr;
922     }
923 
924     if (Lex.getCode() != tgtok::r_paren) {
925       TokError("expected ')' in operator");
926       return nullptr;
927     }
928     Lex.Lex();  // eat the ')'
929 
930     // If we are doing !listconcat, we should know the type by now
931     if (OpTok == tgtok::XListConcat) {
932       if (VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
933         Type = Arg0->getType();
934       else if (ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
935         Type = Arg0->getType();
936       else {
937         InitList[0]->dump();
938         Error(OpLoc, "expected a list");
939         return nullptr;
940       }
941     }
942 
943     // We allow multiple operands to associative operators like !strconcat as
944     // shorthand for nesting them.
945     if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT) {
946       while (InitList.size() > 2) {
947         Init *RHS = InitList.pop_back_val();
948         RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))
949                            ->Fold(CurRec, CurMultiClass);
950         InitList.back() = RHS;
951       }
952     }
953 
954     if (InitList.size() == 2)
955       return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
956         ->Fold(CurRec, CurMultiClass);
957 
958     Error(OpLoc, "expected two operands to operator");
959     return nullptr;
960   }
961 
962   case tgtok::XIf:
963   case tgtok::XForEach:
964   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
965     TernOpInit::TernaryOp Code;
966     RecTy *Type = nullptr;
967 
968     tgtok::TokKind LexCode = Lex.getCode();
969     Lex.Lex();  // eat the operation
970     switch (LexCode) {
971     default: llvm_unreachable("Unhandled code!");
972     case tgtok::XIf:
973       Code = TernOpInit::IF;
974       break;
975     case tgtok::XForEach:
976       Code = TernOpInit::FOREACH;
977       break;
978     case tgtok::XSubst:
979       Code = TernOpInit::SUBST;
980       break;
981     }
982     if (Lex.getCode() != tgtok::l_paren) {
983       TokError("expected '(' after ternary operator");
984       return nullptr;
985     }
986     Lex.Lex();  // eat the '('
987 
988     Init *LHS = ParseValue(CurRec);
989     if (!LHS) return nullptr;
990 
991     if (Lex.getCode() != tgtok::comma) {
992       TokError("expected ',' in ternary operator");
993       return nullptr;
994     }
995     Lex.Lex();  // eat the ','
996 
997     Init *MHS = ParseValue(CurRec, ItemType);
998     if (!MHS)
999       return nullptr;
1000 
1001     if (Lex.getCode() != tgtok::comma) {
1002       TokError("expected ',' in ternary operator");
1003       return nullptr;
1004     }
1005     Lex.Lex();  // eat the ','
1006 
1007     Init *RHS = ParseValue(CurRec, ItemType);
1008     if (!RHS)
1009       return nullptr;
1010 
1011     if (Lex.getCode() != tgtok::r_paren) {
1012       TokError("expected ')' in binary operator");
1013       return nullptr;
1014     }
1015     Lex.Lex();  // eat the ')'
1016 
1017     switch (LexCode) {
1018     default: llvm_unreachable("Unhandled code!");
1019     case tgtok::XIf: {
1020       RecTy *MHSTy = nullptr;
1021       RecTy *RHSTy = nullptr;
1022 
1023       if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1024         MHSTy = MHSt->getType();
1025       if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1026         MHSTy = BitsRecTy::get(MHSbits->getNumBits());
1027       if (isa<BitInit>(MHS))
1028         MHSTy = BitRecTy::get();
1029 
1030       if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1031         RHSTy = RHSt->getType();
1032       if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1033         RHSTy = BitsRecTy::get(RHSbits->getNumBits());
1034       if (isa<BitInit>(RHS))
1035         RHSTy = BitRecTy::get();
1036 
1037       // For UnsetInit, it's typed from the other hand.
1038       if (isa<UnsetInit>(MHS))
1039         MHSTy = RHSTy;
1040       if (isa<UnsetInit>(RHS))
1041         RHSTy = MHSTy;
1042 
1043       if (!MHSTy || !RHSTy) {
1044         TokError("could not get type for !if");
1045         return nullptr;
1046       }
1047 
1048       if (MHSTy->typeIsConvertibleTo(RHSTy)) {
1049         Type = RHSTy;
1050       } else if (RHSTy->typeIsConvertibleTo(MHSTy)) {
1051         Type = MHSTy;
1052       } else {
1053         TokError("inconsistent types for !if");
1054         return nullptr;
1055       }
1056       break;
1057     }
1058     case tgtok::XForEach: {
1059       TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1060       if (!MHSt) {
1061         TokError("could not get type for !foreach");
1062         return nullptr;
1063       }
1064       Type = MHSt->getType();
1065       break;
1066     }
1067     case tgtok::XSubst: {
1068       TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1069       if (!RHSt) {
1070         TokError("could not get type for !subst");
1071         return nullptr;
1072       }
1073       Type = RHSt->getType();
1074       break;
1075     }
1076     }
1077     return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
1078                                                              CurMultiClass);
1079   }
1080   }
1081 }
1082 
1083 /// ParseOperatorType - Parse a type for an operator.  This returns
1084 /// null on error.
1085 ///
1086 /// OperatorType ::= '<' Type '>'
1087 ///
ParseOperatorType()1088 RecTy *TGParser::ParseOperatorType() {
1089   RecTy *Type = nullptr;
1090 
1091   if (Lex.getCode() != tgtok::less) {
1092     TokError("expected type name for operator");
1093     return nullptr;
1094   }
1095   Lex.Lex();  // eat the <
1096 
1097   Type = ParseType();
1098 
1099   if (!Type) {
1100     TokError("expected type name for operator");
1101     return nullptr;
1102   }
1103 
1104   if (Lex.getCode() != tgtok::greater) {
1105     TokError("expected type name for operator");
1106     return nullptr;
1107   }
1108   Lex.Lex();  // eat the >
1109 
1110   return Type;
1111 }
1112 
1113 
1114 /// ParseSimpleValue - Parse a tblgen value.  This returns null on error.
1115 ///
1116 ///   SimpleValue ::= IDValue
1117 ///   SimpleValue ::= INTVAL
1118 ///   SimpleValue ::= STRVAL+
1119 ///   SimpleValue ::= CODEFRAGMENT
1120 ///   SimpleValue ::= '?'
1121 ///   SimpleValue ::= '{' ValueList '}'
1122 ///   SimpleValue ::= ID '<' ValueListNE '>'
1123 ///   SimpleValue ::= '[' ValueList ']'
1124 ///   SimpleValue ::= '(' IDValue DagArgList ')'
1125 ///   SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1126 ///   SimpleValue ::= ADDTOK '(' Value ',' Value ')'
1127 ///   SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1128 ///   SimpleValue ::= SRATOK '(' Value ',' Value ')'
1129 ///   SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1130 ///   SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
1131 ///   SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1132 ///
ParseSimpleValue(Record * CurRec,RecTy * ItemType,IDParseMode Mode)1133 Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1134                                  IDParseMode Mode) {
1135   Init *R = nullptr;
1136   switch (Lex.getCode()) {
1137   default: TokError("Unknown token when parsing a value"); break;
1138   case tgtok::paste:
1139     // This is a leading paste operation.  This is deprecated but
1140     // still exists in some .td files.  Ignore it.
1141     Lex.Lex();  // Skip '#'.
1142     return ParseSimpleValue(CurRec, ItemType, Mode);
1143   case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
1144   case tgtok::BinaryIntVal: {
1145     auto BinaryVal = Lex.getCurBinaryIntVal();
1146     SmallVector<Init*, 16> Bits(BinaryVal.second);
1147     for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
1148       Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
1149     R = BitsInit::get(Bits);
1150     Lex.Lex();
1151     break;
1152   }
1153   case tgtok::StrVal: {
1154     std::string Val = Lex.getCurStrVal();
1155     Lex.Lex();
1156 
1157     // Handle multiple consecutive concatenated strings.
1158     while (Lex.getCode() == tgtok::StrVal) {
1159       Val += Lex.getCurStrVal();
1160       Lex.Lex();
1161     }
1162 
1163     R = StringInit::get(Val);
1164     break;
1165   }
1166   case tgtok::CodeFragment:
1167     R = CodeInit::get(Lex.getCurStrVal());
1168     Lex.Lex();
1169     break;
1170   case tgtok::question:
1171     R = UnsetInit::get();
1172     Lex.Lex();
1173     break;
1174   case tgtok::Id: {
1175     SMLoc NameLoc = Lex.getLoc();
1176     std::string Name = Lex.getCurStrVal();
1177     if (Lex.Lex() != tgtok::less)  // consume the Id.
1178       return ParseIDValue(CurRec, Name, NameLoc, Mode);    // Value ::= IDValue
1179 
1180     // Value ::= ID '<' ValueListNE '>'
1181     if (Lex.Lex() == tgtok::greater) {
1182       TokError("expected non-empty value list");
1183       return nullptr;
1184     }
1185 
1186     // This is a CLASS<initvalslist> expression.  This is supposed to synthesize
1187     // a new anonymous definition, deriving from CLASS<initvalslist> with no
1188     // body.
1189     Record *Class = Records.getClass(Name);
1190     if (!Class) {
1191       Error(NameLoc, "Expected a class name, got '" + Name + "'");
1192       return nullptr;
1193     }
1194 
1195     std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1196     if (ValueList.empty()) return nullptr;
1197 
1198     if (Lex.getCode() != tgtok::greater) {
1199       TokError("expected '>' at end of value list");
1200       return nullptr;
1201     }
1202     Lex.Lex();  // eat the '>'
1203     SMLoc EndLoc = Lex.getLoc();
1204 
1205     // Create the new record, set it as CurRec temporarily.
1206     auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1207                                                  Records, /*IsAnonymous=*/true);
1208     Record *NewRec = NewRecOwner.get(); // Keep a copy since we may release.
1209     SubClassReference SCRef;
1210     SCRef.RefRange = SMRange(NameLoc, EndLoc);
1211     SCRef.Rec = Class;
1212     SCRef.TemplateArgs = ValueList;
1213     // Add info about the subclass to NewRec.
1214     if (AddSubClass(NewRec, SCRef))
1215       return nullptr;
1216 
1217     if (!CurMultiClass) {
1218       NewRec->resolveReferences();
1219       Records.addDef(std::move(NewRecOwner));
1220     } else {
1221       // This needs to get resolved once the multiclass template arguments are
1222       // known before any use.
1223       NewRec->setResolveFirst(true);
1224       // Otherwise, we're inside a multiclass, add it to the multiclass.
1225       CurMultiClass->DefPrototypes.push_back(std::move(NewRecOwner));
1226 
1227       // Copy the template arguments for the multiclass into the def.
1228       for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
1229         const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
1230         assert(RV && "Template arg doesn't exist?");
1231         NewRec->addValue(*RV);
1232       }
1233 
1234       // We can't return the prototype def here, instead return:
1235       // !cast<ItemType>(!strconcat(NAME, AnonName)).
1236       const RecordVal *MCNameRV = CurMultiClass->Rec.getValue("NAME");
1237       assert(MCNameRV && "multiclass record must have a NAME");
1238 
1239       return UnOpInit::get(UnOpInit::CAST,
1240                            BinOpInit::get(BinOpInit::STRCONCAT,
1241                                           VarInit::get(MCNameRV->getName(),
1242                                                        MCNameRV->getType()),
1243                                           NewRec->getNameInit(),
1244                                           StringRecTy::get()),
1245                            Class->getDefInit()->getType());
1246     }
1247 
1248     // The result of the expression is a reference to the new record.
1249     return DefInit::get(NewRec);
1250   }
1251   case tgtok::l_brace: {           // Value ::= '{' ValueList '}'
1252     SMLoc BraceLoc = Lex.getLoc();
1253     Lex.Lex(); // eat the '{'
1254     std::vector<Init*> Vals;
1255 
1256     if (Lex.getCode() != tgtok::r_brace) {
1257       Vals = ParseValueList(CurRec);
1258       if (Vals.empty()) return nullptr;
1259     }
1260     if (Lex.getCode() != tgtok::r_brace) {
1261       TokError("expected '}' at end of bit list value");
1262       return nullptr;
1263     }
1264     Lex.Lex();  // eat the '}'
1265 
1266     SmallVector<Init *, 16> NewBits;
1267 
1268     // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1269     // first.  We'll first read everything in to a vector, then we can reverse
1270     // it to get the bits in the correct order for the BitsInit value.
1271     for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1272       // FIXME: The following two loops would not be duplicated
1273       //        if the API was a little more orthogonal.
1274 
1275       // bits<n> values are allowed to initialize n bits.
1276       if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1277         for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1278           NewBits.push_back(BI->getBit((e - i) - 1));
1279         continue;
1280       }
1281       // bits<n> can also come from variable initializers.
1282       if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1283         if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1284           for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1285             NewBits.push_back(VI->getBit((e - i) - 1));
1286           continue;
1287         }
1288         // Fallthrough to try convert this to a bit.
1289       }
1290       // All other values must be convertible to just a single bit.
1291       Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get());
1292       if (!Bit) {
1293         Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
1294               ") is not convertable to a bit");
1295         return nullptr;
1296       }
1297       NewBits.push_back(Bit);
1298     }
1299     std::reverse(NewBits.begin(), NewBits.end());
1300     return BitsInit::get(NewBits);
1301   }
1302   case tgtok::l_square: {          // Value ::= '[' ValueList ']'
1303     Lex.Lex(); // eat the '['
1304     std::vector<Init*> Vals;
1305 
1306     RecTy *DeducedEltTy = nullptr;
1307     ListRecTy *GivenListTy = nullptr;
1308 
1309     if (ItemType) {
1310       ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
1311       if (!ListType) {
1312         TokError(Twine("Type mismatch for list, expected list type, got ") +
1313                  ItemType->getAsString());
1314         return nullptr;
1315       }
1316       GivenListTy = ListType;
1317     }
1318 
1319     if (Lex.getCode() != tgtok::r_square) {
1320       Vals = ParseValueList(CurRec, nullptr,
1321                             GivenListTy ? GivenListTy->getElementType() : nullptr);
1322       if (Vals.empty()) return nullptr;
1323     }
1324     if (Lex.getCode() != tgtok::r_square) {
1325       TokError("expected ']' at end of list value");
1326       return nullptr;
1327     }
1328     Lex.Lex();  // eat the ']'
1329 
1330     RecTy *GivenEltTy = nullptr;
1331     if (Lex.getCode() == tgtok::less) {
1332       // Optional list element type
1333       Lex.Lex();  // eat the '<'
1334 
1335       GivenEltTy = ParseType();
1336       if (!GivenEltTy) {
1337         // Couldn't parse element type
1338         return nullptr;
1339       }
1340 
1341       if (Lex.getCode() != tgtok::greater) {
1342         TokError("expected '>' at end of list element type");
1343         return nullptr;
1344       }
1345       Lex.Lex();  // eat the '>'
1346     }
1347 
1348     // Check elements
1349     RecTy *EltTy = nullptr;
1350     for (Init *V : Vals) {
1351       TypedInit *TArg = dyn_cast<TypedInit>(V);
1352       if (!TArg) {
1353         TokError("Untyped list element");
1354         return nullptr;
1355       }
1356       if (EltTy) {
1357         EltTy = resolveTypes(EltTy, TArg->getType());
1358         if (!EltTy) {
1359           TokError("Incompatible types in list elements");
1360           return nullptr;
1361         }
1362       } else {
1363         EltTy = TArg->getType();
1364       }
1365     }
1366 
1367     if (GivenEltTy) {
1368       if (EltTy) {
1369         // Verify consistency
1370         if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1371           TokError("Incompatible types in list elements");
1372           return nullptr;
1373         }
1374       }
1375       EltTy = GivenEltTy;
1376     }
1377 
1378     if (!EltTy) {
1379       if (!ItemType) {
1380         TokError("No type for list");
1381         return nullptr;
1382       }
1383       DeducedEltTy = GivenListTy->getElementType();
1384     } else {
1385       // Make sure the deduced type is compatible with the given type
1386       if (GivenListTy) {
1387         if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1388           TokError("Element type mismatch for list");
1389           return nullptr;
1390         }
1391       }
1392       DeducedEltTy = EltTy;
1393     }
1394 
1395     return ListInit::get(Vals, DeducedEltTy);
1396   }
1397   case tgtok::l_paren: {         // Value ::= '(' IDValue DagArgList ')'
1398     Lex.Lex();   // eat the '('
1399     if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
1400       TokError("expected identifier in dag init");
1401       return nullptr;
1402     }
1403 
1404     Init *Operator = ParseValue(CurRec);
1405     if (!Operator) return nullptr;
1406 
1407     // If the operator name is present, parse it.
1408     std::string OperatorName;
1409     if (Lex.getCode() == tgtok::colon) {
1410       if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1411         TokError("expected variable name in dag operator");
1412         return nullptr;
1413       }
1414       OperatorName = Lex.getCurStrVal();
1415       Lex.Lex();  // eat the VarName.
1416     }
1417 
1418     std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1419     if (Lex.getCode() != tgtok::r_paren) {
1420       DagArgs = ParseDagArgList(CurRec);
1421       if (DagArgs.empty()) return nullptr;
1422     }
1423 
1424     if (Lex.getCode() != tgtok::r_paren) {
1425       TokError("expected ')' in dag init");
1426       return nullptr;
1427     }
1428     Lex.Lex();  // eat the ')'
1429 
1430     return DagInit::get(Operator, OperatorName, DagArgs);
1431   }
1432 
1433   case tgtok::XHead:
1434   case tgtok::XTail:
1435   case tgtok::XEmpty:
1436   case tgtok::XCast:  // Value ::= !unop '(' Value ')'
1437   case tgtok::XConcat:
1438   case tgtok::XADD:
1439   case tgtok::XAND:
1440   case tgtok::XSRA:
1441   case tgtok::XSRL:
1442   case tgtok::XSHL:
1443   case tgtok::XEq:
1444   case tgtok::XListConcat:
1445   case tgtok::XStrConcat:   // Value ::= !binop '(' Value ',' Value ')'
1446   case tgtok::XIf:
1447   case tgtok::XForEach:
1448   case tgtok::XSubst: {  // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1449     return ParseOperation(CurRec, ItemType);
1450   }
1451   }
1452 
1453   return R;
1454 }
1455 
1456 /// ParseValue - Parse a tblgen value.  This returns null on error.
1457 ///
1458 ///   Value       ::= SimpleValue ValueSuffix*
1459 ///   ValueSuffix ::= '{' BitList '}'
1460 ///   ValueSuffix ::= '[' BitList ']'
1461 ///   ValueSuffix ::= '.' ID
1462 ///
ParseValue(Record * CurRec,RecTy * ItemType,IDParseMode Mode)1463 Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1464   Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
1465   if (!Result) return nullptr;
1466 
1467   // Parse the suffixes now if present.
1468   while (1) {
1469     switch (Lex.getCode()) {
1470     default: return Result;
1471     case tgtok::l_brace: {
1472       if (Mode == ParseNameMode || Mode == ParseForeachMode)
1473         // This is the beginning of the object body.
1474         return Result;
1475 
1476       SMLoc CurlyLoc = Lex.getLoc();
1477       Lex.Lex(); // eat the '{'
1478       std::vector<unsigned> Ranges = ParseRangeList();
1479       if (Ranges.empty()) return nullptr;
1480 
1481       // Reverse the bitlist.
1482       std::reverse(Ranges.begin(), Ranges.end());
1483       Result = Result->convertInitializerBitRange(Ranges);
1484       if (!Result) {
1485         Error(CurlyLoc, "Invalid bit range for value");
1486         return nullptr;
1487       }
1488 
1489       // Eat the '}'.
1490       if (Lex.getCode() != tgtok::r_brace) {
1491         TokError("expected '}' at end of bit range list");
1492         return nullptr;
1493       }
1494       Lex.Lex();
1495       break;
1496     }
1497     case tgtok::l_square: {
1498       SMLoc SquareLoc = Lex.getLoc();
1499       Lex.Lex(); // eat the '['
1500       std::vector<unsigned> Ranges = ParseRangeList();
1501       if (Ranges.empty()) return nullptr;
1502 
1503       Result = Result->convertInitListSlice(Ranges);
1504       if (!Result) {
1505         Error(SquareLoc, "Invalid range for list slice");
1506         return nullptr;
1507       }
1508 
1509       // Eat the ']'.
1510       if (Lex.getCode() != tgtok::r_square) {
1511         TokError("expected ']' at end of list slice");
1512         return nullptr;
1513       }
1514       Lex.Lex();
1515       break;
1516     }
1517     case tgtok::period:
1518       if (Lex.Lex() != tgtok::Id) {  // eat the .
1519         TokError("expected field identifier after '.'");
1520         return nullptr;
1521       }
1522       if (!Result->getFieldType(Lex.getCurStrVal())) {
1523         TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
1524                  Result->getAsString() + "'");
1525         return nullptr;
1526       }
1527       Result = FieldInit::get(Result, Lex.getCurStrVal());
1528       Lex.Lex();  // eat field name
1529       break;
1530 
1531     case tgtok::paste:
1532       SMLoc PasteLoc = Lex.getLoc();
1533 
1534       // Create a !strconcat() operation, first casting each operand to
1535       // a string if necessary.
1536 
1537       TypedInit *LHS = dyn_cast<TypedInit>(Result);
1538       if (!LHS) {
1539         Error(PasteLoc, "LHS of paste is not typed!");
1540         return nullptr;
1541       }
1542 
1543       if (LHS->getType() != StringRecTy::get()) {
1544         LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get());
1545       }
1546 
1547       TypedInit *RHS = nullptr;
1548 
1549       Lex.Lex();  // Eat the '#'.
1550       switch (Lex.getCode()) {
1551       case tgtok::colon:
1552       case tgtok::semi:
1553       case tgtok::l_brace:
1554         // These are all of the tokens that can begin an object body.
1555         // Some of these can also begin values but we disallow those cases
1556         // because they are unlikely to be useful.
1557 
1558         // Trailing paste, concat with an empty string.
1559         RHS = StringInit::get("");
1560         break;
1561 
1562       default:
1563         Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
1564         RHS = dyn_cast<TypedInit>(RHSResult);
1565         if (!RHS) {
1566           Error(PasteLoc, "RHS of paste is not typed!");
1567           return nullptr;
1568         }
1569 
1570         if (RHS->getType() != StringRecTy::get()) {
1571           RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get());
1572         }
1573 
1574         break;
1575       }
1576 
1577       Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS,
1578                               StringRecTy::get())->Fold(CurRec, CurMultiClass);
1579       break;
1580     }
1581   }
1582 }
1583 
1584 /// ParseDagArgList - Parse the argument list for a dag literal expression.
1585 ///
1586 ///    DagArg     ::= Value (':' VARNAME)?
1587 ///    DagArg     ::= VARNAME
1588 ///    DagArgList ::= DagArg
1589 ///    DagArgList ::= DagArgList ',' DagArg
1590 std::vector<std::pair<llvm::Init*, std::string> >
ParseDagArgList(Record * CurRec)1591 TGParser::ParseDagArgList(Record *CurRec) {
1592   std::vector<std::pair<llvm::Init*, std::string> > Result;
1593 
1594   while (1) {
1595     // DagArg ::= VARNAME
1596     if (Lex.getCode() == tgtok::VarName) {
1597       // A missing value is treated like '?'.
1598       Result.emplace_back(UnsetInit::get(), Lex.getCurStrVal());
1599       Lex.Lex();
1600     } else {
1601       // DagArg ::= Value (':' VARNAME)?
1602       Init *Val = ParseValue(CurRec);
1603       if (!Val)
1604         return std::vector<std::pair<llvm::Init*, std::string> >();
1605 
1606       // If the variable name is present, add it.
1607       std::string VarName;
1608       if (Lex.getCode() == tgtok::colon) {
1609         if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1610           TokError("expected variable name in dag literal");
1611           return std::vector<std::pair<llvm::Init*, std::string> >();
1612         }
1613         VarName = Lex.getCurStrVal();
1614         Lex.Lex();  // eat the VarName.
1615       }
1616 
1617       Result.push_back(std::make_pair(Val, VarName));
1618     }
1619     if (Lex.getCode() != tgtok::comma) break;
1620     Lex.Lex(); // eat the ','
1621   }
1622 
1623   return Result;
1624 }
1625 
1626 
1627 /// ParseValueList - Parse a comma separated list of values, returning them as a
1628 /// vector.  Note that this always expects to be able to parse at least one
1629 /// value.  It returns an empty list if this is not possible.
1630 ///
1631 ///   ValueList ::= Value (',' Value)
1632 ///
ParseValueList(Record * CurRec,Record * ArgsRec,RecTy * EltTy)1633 std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1634                                             RecTy *EltTy) {
1635   std::vector<Init*> Result;
1636   RecTy *ItemType = EltTy;
1637   unsigned int ArgN = 0;
1638   if (ArgsRec && !EltTy) {
1639     ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
1640     if (TArgs.empty()) {
1641       TokError("template argument provided to non-template class");
1642       return std::vector<Init*>();
1643     }
1644     const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1645     if (!RV) {
1646       errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
1647         << ")\n";
1648     }
1649     assert(RV && "Template argument record not found??");
1650     ItemType = RV->getType();
1651     ++ArgN;
1652   }
1653   Result.push_back(ParseValue(CurRec, ItemType));
1654   if (!Result.back()) return std::vector<Init*>();
1655 
1656   while (Lex.getCode() == tgtok::comma) {
1657     Lex.Lex();  // Eat the comma
1658 
1659     if (ArgsRec && !EltTy) {
1660       ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
1661       if (ArgN >= TArgs.size()) {
1662         TokError("too many template arguments");
1663         return std::vector<Init*>();
1664       }
1665       const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1666       assert(RV && "Template argument record not found??");
1667       ItemType = RV->getType();
1668       ++ArgN;
1669     }
1670     Result.push_back(ParseValue(CurRec, ItemType));
1671     if (!Result.back()) return std::vector<Init*>();
1672   }
1673 
1674   return Result;
1675 }
1676 
1677 
1678 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1679 /// empty string on error.  This can happen in a number of different context's,
1680 /// including within a def or in the template args for a def (which which case
1681 /// CurRec will be non-null) and within the template args for a multiclass (in
1682 /// which case CurRec will be null, but CurMultiClass will be set).  This can
1683 /// also happen within a def that is within a multiclass, which will set both
1684 /// CurRec and CurMultiClass.
1685 ///
1686 ///  Declaration ::= FIELD? Type ID ('=' Value)?
1687 ///
ParseDeclaration(Record * CurRec,bool ParsingTemplateArgs)1688 Init *TGParser::ParseDeclaration(Record *CurRec,
1689                                        bool ParsingTemplateArgs) {
1690   // Read the field prefix if present.
1691   bool HasField = Lex.getCode() == tgtok::Field;
1692   if (HasField) Lex.Lex();
1693 
1694   RecTy *Type = ParseType();
1695   if (!Type) return nullptr;
1696 
1697   if (Lex.getCode() != tgtok::Id) {
1698     TokError("Expected identifier in declaration");
1699     return nullptr;
1700   }
1701 
1702   SMLoc IdLoc = Lex.getLoc();
1703   Init *DeclName = StringInit::get(Lex.getCurStrVal());
1704   Lex.Lex();
1705 
1706   if (ParsingTemplateArgs) {
1707     if (CurRec)
1708       DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
1709     else
1710       assert(CurMultiClass);
1711     if (CurMultiClass)
1712       DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
1713                              "::");
1714   }
1715 
1716   // Add the value.
1717   if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1718     return nullptr;
1719 
1720   // If a value is present, parse it.
1721   if (Lex.getCode() == tgtok::equal) {
1722     Lex.Lex();
1723     SMLoc ValLoc = Lex.getLoc();
1724     Init *Val = ParseValue(CurRec, Type);
1725     if (!Val ||
1726         SetValue(CurRec, ValLoc, DeclName, None, Val))
1727       // Return the name, even if an error is thrown.  This is so that we can
1728       // continue to make some progress, even without the value having been
1729       // initialized.
1730       return DeclName;
1731   }
1732 
1733   return DeclName;
1734 }
1735 
1736 /// ParseForeachDeclaration - Read a foreach declaration, returning
1737 /// the name of the declared object or a NULL Init on error.  Return
1738 /// the name of the parsed initializer list through ForeachListName.
1739 ///
1740 ///  ForeachDeclaration ::= ID '=' '[' ValueList ']'
1741 ///  ForeachDeclaration ::= ID '=' '{' RangeList '}'
1742 ///  ForeachDeclaration ::= ID '=' RangePiece
1743 ///
ParseForeachDeclaration(ListInit * & ForeachListValue)1744 VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
1745   if (Lex.getCode() != tgtok::Id) {
1746     TokError("Expected identifier in foreach declaration");
1747     return nullptr;
1748   }
1749 
1750   Init *DeclName = StringInit::get(Lex.getCurStrVal());
1751   Lex.Lex();
1752 
1753   // If a value is present, parse it.
1754   if (Lex.getCode() != tgtok::equal) {
1755     TokError("Expected '=' in foreach declaration");
1756     return nullptr;
1757   }
1758   Lex.Lex();  // Eat the '='
1759 
1760   RecTy *IterType = nullptr;
1761   std::vector<unsigned> Ranges;
1762 
1763   switch (Lex.getCode()) {
1764   default: TokError("Unknown token when expecting a range list"); return nullptr;
1765   case tgtok::l_square: { // '[' ValueList ']'
1766     Init *List = ParseSimpleValue(nullptr, nullptr, ParseForeachMode);
1767     ForeachListValue = dyn_cast<ListInit>(List);
1768     if (!ForeachListValue) {
1769       TokError("Expected a Value list");
1770       return nullptr;
1771     }
1772     RecTy *ValueType = ForeachListValue->getType();
1773     ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType);
1774     if (!ListType) {
1775       TokError("Value list is not of list type");
1776       return nullptr;
1777     }
1778     IterType = ListType->getElementType();
1779     break;
1780   }
1781 
1782   case tgtok::IntVal: { // RangePiece.
1783     if (ParseRangePiece(Ranges))
1784       return nullptr;
1785     break;
1786   }
1787 
1788   case tgtok::l_brace: { // '{' RangeList '}'
1789     Lex.Lex(); // eat the '{'
1790     Ranges = ParseRangeList();
1791     if (Lex.getCode() != tgtok::r_brace) {
1792       TokError("expected '}' at end of bit range list");
1793       return nullptr;
1794     }
1795     Lex.Lex();
1796     break;
1797   }
1798   }
1799 
1800   if (!Ranges.empty()) {
1801     assert(!IterType && "Type already initialized?");
1802     IterType = IntRecTy::get();
1803     std::vector<Init*> Values;
1804     for (unsigned R : Ranges)
1805       Values.push_back(IntInit::get(R));
1806     ForeachListValue = ListInit::get(Values, IterType);
1807   }
1808 
1809   if (!IterType)
1810     return nullptr;
1811 
1812   return VarInit::get(DeclName, IterType);
1813 }
1814 
1815 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
1816 /// sequence of template-declarations in <>'s.  If CurRec is non-null, these are
1817 /// template args for a def, which may or may not be in a multiclass.  If null,
1818 /// these are the template args for a multiclass.
1819 ///
1820 ///    TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1821 ///
ParseTemplateArgList(Record * CurRec)1822 bool TGParser::ParseTemplateArgList(Record *CurRec) {
1823   assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1824   Lex.Lex(); // eat the '<'
1825 
1826   Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1827 
1828   // Read the first declaration.
1829   Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1830   if (!TemplArg)
1831     return true;
1832 
1833   TheRecToAddTo->addTemplateArg(TemplArg);
1834 
1835   while (Lex.getCode() == tgtok::comma) {
1836     Lex.Lex(); // eat the ','
1837 
1838     // Read the following declarations.
1839     TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1840     if (!TemplArg)
1841       return true;
1842     TheRecToAddTo->addTemplateArg(TemplArg);
1843   }
1844 
1845   if (Lex.getCode() != tgtok::greater)
1846     return TokError("expected '>' at end of template argument list");
1847   Lex.Lex(); // eat the '>'.
1848   return false;
1849 }
1850 
1851 
1852 /// ParseBodyItem - Parse a single item at within the body of a def or class.
1853 ///
1854 ///   BodyItem ::= Declaration ';'
1855 ///   BodyItem ::= LET ID OptionalBitList '=' Value ';'
ParseBodyItem(Record * CurRec)1856 bool TGParser::ParseBodyItem(Record *CurRec) {
1857   if (Lex.getCode() != tgtok::Let) {
1858     if (!ParseDeclaration(CurRec, false))
1859       return true;
1860 
1861     if (Lex.getCode() != tgtok::semi)
1862       return TokError("expected ';' after declaration");
1863     Lex.Lex();
1864     return false;
1865   }
1866 
1867   // LET ID OptionalRangeList '=' Value ';'
1868   if (Lex.Lex() != tgtok::Id)
1869     return TokError("expected field identifier after let");
1870 
1871   SMLoc IdLoc = Lex.getLoc();
1872   std::string FieldName = Lex.getCurStrVal();
1873   Lex.Lex();  // eat the field name.
1874 
1875   std::vector<unsigned> BitList;
1876   if (ParseOptionalBitList(BitList))
1877     return true;
1878   std::reverse(BitList.begin(), BitList.end());
1879 
1880   if (Lex.getCode() != tgtok::equal)
1881     return TokError("expected '=' in let expression");
1882   Lex.Lex();  // eat the '='.
1883 
1884   RecordVal *Field = CurRec->getValue(FieldName);
1885   if (!Field)
1886     return TokError("Value '" + FieldName + "' unknown!");
1887 
1888   RecTy *Type = Field->getType();
1889 
1890   Init *Val = ParseValue(CurRec, Type);
1891   if (!Val) return true;
1892 
1893   if (Lex.getCode() != tgtok::semi)
1894     return TokError("expected ';' after let expression");
1895   Lex.Lex();
1896 
1897   return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1898 }
1899 
1900 /// ParseBody - Read the body of a class or def.  Return true on error, false on
1901 /// success.
1902 ///
1903 ///   Body     ::= ';'
1904 ///   Body     ::= '{' BodyList '}'
1905 ///   BodyList BodyItem*
1906 ///
ParseBody(Record * CurRec)1907 bool TGParser::ParseBody(Record *CurRec) {
1908   // If this is a null definition, just eat the semi and return.
1909   if (Lex.getCode() == tgtok::semi) {
1910     Lex.Lex();
1911     return false;
1912   }
1913 
1914   if (Lex.getCode() != tgtok::l_brace)
1915     return TokError("Expected ';' or '{' to start body");
1916   // Eat the '{'.
1917   Lex.Lex();
1918 
1919   while (Lex.getCode() != tgtok::r_brace)
1920     if (ParseBodyItem(CurRec))
1921       return true;
1922 
1923   // Eat the '}'.
1924   Lex.Lex();
1925   return false;
1926 }
1927 
1928 /// \brief Apply the current let bindings to \a CurRec.
1929 /// \returns true on error, false otherwise.
ApplyLetStack(Record * CurRec)1930 bool TGParser::ApplyLetStack(Record *CurRec) {
1931   for (std::vector<LetRecord> &LetInfo : LetStack)
1932     for (LetRecord &LR : LetInfo)
1933       if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
1934         return true;
1935   return false;
1936 }
1937 
1938 /// ParseObjectBody - Parse the body of a def or class.  This consists of an
1939 /// optional ClassList followed by a Body.  CurRec is the current def or class
1940 /// that is being parsed.
1941 ///
1942 ///   ObjectBody      ::= BaseClassList Body
1943 ///   BaseClassList   ::= /*empty*/
1944 ///   BaseClassList   ::= ':' BaseClassListNE
1945 ///   BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1946 ///
ParseObjectBody(Record * CurRec)1947 bool TGParser::ParseObjectBody(Record *CurRec) {
1948   // If there is a baseclass list, read it.
1949   if (Lex.getCode() == tgtok::colon) {
1950     Lex.Lex();
1951 
1952     // Read all of the subclasses.
1953     SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1954     while (1) {
1955       // Check for error.
1956       if (!SubClass.Rec) return true;
1957 
1958       // Add it.
1959       if (AddSubClass(CurRec, SubClass))
1960         return true;
1961 
1962       if (Lex.getCode() != tgtok::comma) break;
1963       Lex.Lex(); // eat ','.
1964       SubClass = ParseSubClassReference(CurRec, false);
1965     }
1966   }
1967 
1968   if (ApplyLetStack(CurRec))
1969     return true;
1970 
1971   return ParseBody(CurRec);
1972 }
1973 
1974 /// ParseDef - Parse and return a top level or multiclass def, return the record
1975 /// corresponding to it.  This returns null on error.
1976 ///
1977 ///   DefInst ::= DEF ObjectName ObjectBody
1978 ///
ParseDef(MultiClass * CurMultiClass)1979 bool TGParser::ParseDef(MultiClass *CurMultiClass) {
1980   SMLoc DefLoc = Lex.getLoc();
1981   assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1982   Lex.Lex();  // Eat the 'def' token.
1983 
1984   // Parse ObjectName and make a record for it.
1985   std::unique_ptr<Record> CurRecOwner;
1986   Init *Name = ParseObjectName(CurMultiClass);
1987   if (Name)
1988     CurRecOwner = make_unique<Record>(Name, DefLoc, Records);
1989   else
1990     CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1991                                             Records, /*IsAnonymous=*/true);
1992   Record *CurRec = CurRecOwner.get(); // Keep a copy since we may release.
1993 
1994   if (!CurMultiClass && Loops.empty()) {
1995     // Top-level def definition.
1996 
1997     // Ensure redefinition doesn't happen.
1998     if (Records.getDef(CurRec->getNameInitAsString()))
1999       return Error(DefLoc, "def '" + CurRec->getNameInitAsString()+
2000                    "' already defined");
2001     Records.addDef(std::move(CurRecOwner));
2002 
2003     if (ParseObjectBody(CurRec))
2004       return true;
2005   } else if (CurMultiClass) {
2006     // Parse the body before adding this prototype to the DefPrototypes vector.
2007     // That way implicit definitions will be added to the DefPrototypes vector
2008     // before this object, instantiated prior to defs derived from this object,
2009     // and this available for indirect name resolution when defs derived from
2010     // this object are instantiated.
2011     if (ParseObjectBody(CurRec))
2012       return true;
2013 
2014     // Otherwise, a def inside a multiclass, add it to the multiclass.
2015     for (const auto &Proto : CurMultiClass->DefPrototypes)
2016       if (Proto->getNameInit() == CurRec->getNameInit())
2017         return Error(DefLoc, "def '" + CurRec->getNameInitAsString() +
2018                      "' already defined in this multiclass!");
2019     CurMultiClass->DefPrototypes.push_back(std::move(CurRecOwner));
2020   } else if (ParseObjectBody(CurRec)) {
2021     return true;
2022   }
2023 
2024   if (!CurMultiClass)  // Def's in multiclasses aren't really defs.
2025     // See Record::setName().  This resolve step will see any new name
2026     // for the def that might have been created when resolving
2027     // inheritance, values and arguments above.
2028     CurRec->resolveReferences();
2029 
2030   // If ObjectBody has template arguments, it's an error.
2031   assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
2032 
2033   if (CurMultiClass) {
2034     // Copy the template arguments for the multiclass into the def.
2035     for (Init *TArg : CurMultiClass->Rec.getTemplateArgs()) {
2036       const RecordVal *RV = CurMultiClass->Rec.getValue(TArg);
2037       assert(RV && "Template arg doesn't exist?");
2038       CurRec->addValue(*RV);
2039     }
2040   }
2041 
2042   if (ProcessForeachDefs(CurRec, DefLoc))
2043     return Error(DefLoc, "Could not process loops for def" +
2044                  CurRec->getNameInitAsString());
2045 
2046   return false;
2047 }
2048 
2049 /// ParseForeach - Parse a for statement.  Return the record corresponding
2050 /// to it.  This returns true on error.
2051 ///
2052 ///   Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2053 ///   Foreach ::= FOREACH Declaration IN Object
2054 ///
ParseForeach(MultiClass * CurMultiClass)2055 bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2056   assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2057   Lex.Lex();  // Eat the 'for' token.
2058 
2059   // Make a temporary object to record items associated with the for
2060   // loop.
2061   ListInit *ListValue = nullptr;
2062   VarInit *IterName = ParseForeachDeclaration(ListValue);
2063   if (!IterName)
2064     return TokError("expected declaration in for");
2065 
2066   if (Lex.getCode() != tgtok::In)
2067     return TokError("Unknown tok");
2068   Lex.Lex();  // Eat the in
2069 
2070   // Create a loop object and remember it.
2071   Loops.push_back(ForeachLoop(IterName, ListValue));
2072 
2073   if (Lex.getCode() != tgtok::l_brace) {
2074     // FOREACH Declaration IN Object
2075     if (ParseObject(CurMultiClass))
2076       return true;
2077   } else {
2078     SMLoc BraceLoc = Lex.getLoc();
2079     // Otherwise, this is a group foreach.
2080     Lex.Lex();  // eat the '{'.
2081 
2082     // Parse the object list.
2083     if (ParseObjectList(CurMultiClass))
2084       return true;
2085 
2086     if (Lex.getCode() != tgtok::r_brace) {
2087       TokError("expected '}' at end of foreach command");
2088       return Error(BraceLoc, "to match this '{'");
2089     }
2090     Lex.Lex();  // Eat the }
2091   }
2092 
2093   // We've processed everything in this loop.
2094   Loops.pop_back();
2095 
2096   return false;
2097 }
2098 
2099 /// ParseClass - Parse a tblgen class definition.
2100 ///
2101 ///   ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2102 ///
ParseClass()2103 bool TGParser::ParseClass() {
2104   assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2105   Lex.Lex();
2106 
2107   if (Lex.getCode() != tgtok::Id)
2108     return TokError("expected class name after 'class' keyword");
2109 
2110   Record *CurRec = Records.getClass(Lex.getCurStrVal());
2111   if (CurRec) {
2112     // If the body was previously defined, this is an error.
2113     if (CurRec->getValues().size() > 1 ||  // Account for NAME.
2114         !CurRec->getSuperClasses().empty() ||
2115         !CurRec->getTemplateArgs().empty())
2116       return TokError("Class '" + CurRec->getNameInitAsString() +
2117                       "' already defined");
2118   } else {
2119     // If this is the first reference to this class, create and add it.
2120     auto NewRec =
2121         llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
2122     CurRec = NewRec.get();
2123     Records.addClass(std::move(NewRec));
2124   }
2125   Lex.Lex(); // eat the name.
2126 
2127   // If there are template args, parse them.
2128   if (Lex.getCode() == tgtok::less)
2129     if (ParseTemplateArgList(CurRec))
2130       return true;
2131 
2132   // Finally, parse the object body.
2133   return ParseObjectBody(CurRec);
2134 }
2135 
2136 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
2137 /// of LetRecords.
2138 ///
2139 ///   LetList ::= LetItem (',' LetItem)*
2140 ///   LetItem ::= ID OptionalRangeList '=' Value
2141 ///
ParseLetList()2142 std::vector<LetRecord> TGParser::ParseLetList() {
2143   std::vector<LetRecord> Result;
2144 
2145   while (1) {
2146     if (Lex.getCode() != tgtok::Id) {
2147       TokError("expected identifier in let definition");
2148       return std::vector<LetRecord>();
2149     }
2150     std::string Name = Lex.getCurStrVal();
2151     SMLoc NameLoc = Lex.getLoc();
2152     Lex.Lex();  // Eat the identifier.
2153 
2154     // Check for an optional RangeList.
2155     std::vector<unsigned> Bits;
2156     if (ParseOptionalRangeList(Bits))
2157       return std::vector<LetRecord>();
2158     std::reverse(Bits.begin(), Bits.end());
2159 
2160     if (Lex.getCode() != tgtok::equal) {
2161       TokError("expected '=' in let expression");
2162       return std::vector<LetRecord>();
2163     }
2164     Lex.Lex();  // eat the '='.
2165 
2166     Init *Val = ParseValue(nullptr);
2167     if (!Val) return std::vector<LetRecord>();
2168 
2169     // Now that we have everything, add the record.
2170     Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
2171 
2172     if (Lex.getCode() != tgtok::comma)
2173       return Result;
2174     Lex.Lex();  // eat the comma.
2175   }
2176 }
2177 
2178 /// ParseTopLevelLet - Parse a 'let' at top level.  This can be a couple of
2179 /// different related productions. This works inside multiclasses too.
2180 ///
2181 ///   Object ::= LET LetList IN '{' ObjectList '}'
2182 ///   Object ::= LET LetList IN Object
2183 ///
ParseTopLevelLet(MultiClass * CurMultiClass)2184 bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
2185   assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2186   Lex.Lex();
2187 
2188   // Add this entry to the let stack.
2189   std::vector<LetRecord> LetInfo = ParseLetList();
2190   if (LetInfo.empty()) return true;
2191   LetStack.push_back(std::move(LetInfo));
2192 
2193   if (Lex.getCode() != tgtok::In)
2194     return TokError("expected 'in' at end of top-level 'let'");
2195   Lex.Lex();
2196 
2197   // If this is a scalar let, just handle it now
2198   if (Lex.getCode() != tgtok::l_brace) {
2199     // LET LetList IN Object
2200     if (ParseObject(CurMultiClass))
2201       return true;
2202   } else {   // Object ::= LETCommand '{' ObjectList '}'
2203     SMLoc BraceLoc = Lex.getLoc();
2204     // Otherwise, this is a group let.
2205     Lex.Lex();  // eat the '{'.
2206 
2207     // Parse the object list.
2208     if (ParseObjectList(CurMultiClass))
2209       return true;
2210 
2211     if (Lex.getCode() != tgtok::r_brace) {
2212       TokError("expected '}' at end of top level let command");
2213       return Error(BraceLoc, "to match this '{'");
2214     }
2215     Lex.Lex();
2216   }
2217 
2218   // Outside this let scope, this let block is not active.
2219   LetStack.pop_back();
2220   return false;
2221 }
2222 
2223 /// ParseMultiClass - Parse a multiclass definition.
2224 ///
2225 ///  MultiClassInst ::= MULTICLASS ID TemplateArgList?
2226 ///                     ':' BaseMultiClassList '{' MultiClassObject+ '}'
2227 ///  MultiClassObject ::= DefInst
2228 ///  MultiClassObject ::= MultiClassInst
2229 ///  MultiClassObject ::= DefMInst
2230 ///  MultiClassObject ::= LETCommand '{' ObjectList '}'
2231 ///  MultiClassObject ::= LETCommand Object
2232 ///
ParseMultiClass()2233 bool TGParser::ParseMultiClass() {
2234   assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2235   Lex.Lex();  // Eat the multiclass token.
2236 
2237   if (Lex.getCode() != tgtok::Id)
2238     return TokError("expected identifier after multiclass for name");
2239   std::string Name = Lex.getCurStrVal();
2240 
2241   auto Result =
2242     MultiClasses.insert(std::make_pair(Name,
2243                     llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2244 
2245   if (!Result.second)
2246     return TokError("multiclass '" + Name + "' already defined");
2247 
2248   CurMultiClass = Result.first->second.get();
2249   Lex.Lex();  // Eat the identifier.
2250 
2251   // If there are template args, parse them.
2252   if (Lex.getCode() == tgtok::less)
2253     if (ParseTemplateArgList(nullptr))
2254       return true;
2255 
2256   bool inherits = false;
2257 
2258   // If there are submulticlasses, parse them.
2259   if (Lex.getCode() == tgtok::colon) {
2260     inherits = true;
2261 
2262     Lex.Lex();
2263 
2264     // Read all of the submulticlasses.
2265     SubMultiClassReference SubMultiClass =
2266       ParseSubMultiClassReference(CurMultiClass);
2267     while (1) {
2268       // Check for error.
2269       if (!SubMultiClass.MC) return true;
2270 
2271       // Add it.
2272       if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2273         return true;
2274 
2275       if (Lex.getCode() != tgtok::comma) break;
2276       Lex.Lex(); // eat ','.
2277       SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2278     }
2279   }
2280 
2281   if (Lex.getCode() != tgtok::l_brace) {
2282     if (!inherits)
2283       return TokError("expected '{' in multiclass definition");
2284     if (Lex.getCode() != tgtok::semi)
2285       return TokError("expected ';' in multiclass definition");
2286     Lex.Lex();  // eat the ';'.
2287   } else {
2288     if (Lex.Lex() == tgtok::r_brace)  // eat the '{'.
2289       return TokError("multiclass must contain at least one def");
2290 
2291     while (Lex.getCode() != tgtok::r_brace) {
2292       switch (Lex.getCode()) {
2293       default:
2294         return TokError("expected 'let', 'def' or 'defm' in multiclass body");
2295       case tgtok::Let:
2296       case tgtok::Def:
2297       case tgtok::Defm:
2298       case tgtok::Foreach:
2299         if (ParseObject(CurMultiClass))
2300           return true;
2301         break;
2302       }
2303     }
2304     Lex.Lex();  // eat the '}'.
2305   }
2306 
2307   CurMultiClass = nullptr;
2308   return false;
2309 }
2310 
InstantiateMulticlassDef(MultiClass & MC,Record * DefProto,Init * & DefmPrefix,SMRange DefmPrefixRange,ArrayRef<Init * > TArgs,std::vector<Init * > & TemplateVals)2311 Record *TGParser::InstantiateMulticlassDef(MultiClass &MC, Record *DefProto,
2312                                            Init *&DefmPrefix,
2313                                            SMRange DefmPrefixRange,
2314                                            ArrayRef<Init *> TArgs,
2315                                            std::vector<Init *> &TemplateVals) {
2316   // We need to preserve DefProto so it can be reused for later
2317   // instantiations, so create a new Record to inherit from it.
2318 
2319   // Add in the defm name.  If the defm prefix is empty, give each
2320   // instantiated def a unique name.  Otherwise, if "#NAME#" exists in the
2321   // name, substitute the prefix for #NAME#.  Otherwise, use the defm name
2322   // as a prefix.
2323 
2324   bool IsAnonymous = false;
2325   if (!DefmPrefix) {
2326     DefmPrefix = StringInit::get(GetNewAnonymousName());
2327     IsAnonymous = true;
2328   }
2329 
2330   Init *DefName = DefProto->getNameInit();
2331   StringInit *DefNameString = dyn_cast<StringInit>(DefName);
2332 
2333   if (DefNameString) {
2334     // We have a fully expanded string so there are no operators to
2335     // resolve.  We should concatenate the given prefix and name.
2336     DefName =
2337       BinOpInit::get(BinOpInit::STRCONCAT,
2338                      UnOpInit::get(UnOpInit::CAST, DefmPrefix,
2339                                    StringRecTy::get())->Fold(DefProto, &MC),
2340                      DefName, StringRecTy::get())->Fold(DefProto, &MC);
2341   }
2342 
2343   // Make a trail of SMLocs from the multiclass instantiations.
2344   SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start);
2345   Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end());
2346   auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
2347 
2348   SubClassReference Ref;
2349   Ref.RefRange = DefmPrefixRange;
2350   Ref.Rec = DefProto;
2351   AddSubClass(CurRec.get(), Ref);
2352 
2353   // Set the value for NAME. We don't resolve references to it 'til later,
2354   // though, so that uses in nested multiclass names don't get
2355   // confused.
2356   if (SetValue(CurRec.get(), Ref.RefRange.Start, "NAME", None, DefmPrefix,
2357                /*AllowSelfAssignment*/true)) {
2358     Error(DefmPrefixRange.Start, "Could not resolve " +
2359           CurRec->getNameInitAsString() + ":NAME to '" +
2360           DefmPrefix->getAsUnquotedString() + "'");
2361     return nullptr;
2362   }
2363 
2364   // If the DefNameString didn't resolve, we probably have a reference to
2365   // NAME and need to replace it. We need to do at least this much greedily,
2366   // otherwise nested multiclasses will end up with incorrect NAME expansions.
2367   if (!DefNameString) {
2368     RecordVal *DefNameRV = CurRec->getValue("NAME");
2369     CurRec->resolveReferencesTo(DefNameRV);
2370   }
2371 
2372   if (!CurMultiClass) {
2373     // Now that we're at the top level, resolve all NAME references
2374     // in the resultant defs that weren't in the def names themselves.
2375     RecordVal *DefNameRV = CurRec->getValue("NAME");
2376     CurRec->resolveReferencesTo(DefNameRV);
2377 
2378     // Check if the name is a complex pattern.
2379     // If so, resolve it.
2380     DefName = CurRec->getNameInit();
2381     DefNameString = dyn_cast<StringInit>(DefName);
2382 
2383     // OK the pattern is more complex than simply using NAME.
2384     // Let's use the heavy weaponery.
2385     if (!DefNameString) {
2386       ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.Start,
2387                                Lex.getLoc(), TArgs, TemplateVals,
2388                                false/*Delete args*/);
2389       DefName = CurRec->getNameInit();
2390       DefNameString = dyn_cast<StringInit>(DefName);
2391 
2392       if (!DefNameString)
2393         DefName = DefName->convertInitializerTo(StringRecTy::get());
2394 
2395       // We ran out of options here...
2396       DefNameString = dyn_cast<StringInit>(DefName);
2397       if (!DefNameString) {
2398         PrintFatalError(CurRec->getLoc()[CurRec->getLoc().size() - 1],
2399                         DefName->getAsUnquotedString() + " is not a string.");
2400         return nullptr;
2401       }
2402 
2403       CurRec->setName(DefName);
2404     }
2405 
2406     // Now that NAME references are resolved and we're at the top level of
2407     // any multiclass expansions, add the record to the RecordKeeper. If we are
2408     // currently in a multiclass, it means this defm appears inside a
2409     // multiclass and its name won't be fully resolvable until we see
2410     // the top-level defm. Therefore, we don't add this to the
2411     // RecordKeeper at this point. If we did we could get duplicate
2412     // defs as more than one probably refers to NAME or some other
2413     // common internal placeholder.
2414 
2415     // Ensure redefinition doesn't happen.
2416     if (Records.getDef(CurRec->getNameInitAsString())) {
2417       Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() +
2418             "' already defined, instantiating defm with subdef '" +
2419             DefProto->getNameInitAsString() + "'");
2420       return nullptr;
2421     }
2422 
2423     Record *CurRecSave = CurRec.get(); // Keep a copy before we release.
2424     Records.addDef(std::move(CurRec));
2425     return CurRecSave;
2426   }
2427 
2428   // FIXME This is bad but the ownership transfer to caller is pretty messy.
2429   // The unique_ptr in this function at least protects the exits above.
2430   return CurRec.release();
2431 }
2432 
ResolveMulticlassDefArgs(MultiClass & MC,Record * CurRec,SMLoc DefmPrefixLoc,SMLoc SubClassLoc,ArrayRef<Init * > TArgs,std::vector<Init * > & TemplateVals,bool DeleteArgs)2433 bool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, Record *CurRec,
2434                                         SMLoc DefmPrefixLoc, SMLoc SubClassLoc,
2435                                         ArrayRef<Init *> TArgs,
2436                                         std::vector<Init *> &TemplateVals,
2437                                         bool DeleteArgs) {
2438   // Loop over all of the template arguments, setting them to the specified
2439   // value or leaving them as the default if necessary.
2440   for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2441     // Check if a value is specified for this temp-arg.
2442     if (i < TemplateVals.size()) {
2443       // Set it now.
2444       if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], None, TemplateVals[i]))
2445         return true;
2446 
2447       // Resolve it next.
2448       CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
2449 
2450       if (DeleteArgs)
2451         // Now remove it.
2452         CurRec->removeValue(TArgs[i]);
2453 
2454     } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
2455       return Error(SubClassLoc, "value not specified for template argument #" +
2456                    Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
2457                    ") of multiclassclass '" + MC.Rec.getNameInitAsString() +
2458                    "'");
2459     }
2460   }
2461   return false;
2462 }
2463 
ResolveMulticlassDef(MultiClass & MC,Record * CurRec,Record * DefProto,SMLoc DefmPrefixLoc)2464 bool TGParser::ResolveMulticlassDef(MultiClass &MC,
2465                                     Record *CurRec,
2466                                     Record *DefProto,
2467                                     SMLoc DefmPrefixLoc) {
2468   // If the mdef is inside a 'let' expression, add to each def.
2469   if (ApplyLetStack(CurRec))
2470     return Error(DefmPrefixLoc, "when instantiating this defm");
2471 
2472   // Don't create a top level definition for defm inside multiclasses,
2473   // instead, only update the prototypes and bind the template args
2474   // with the new created definition.
2475   if (!CurMultiClass)
2476     return false;
2477   for (const auto &Proto : CurMultiClass->DefPrototypes)
2478     if (Proto->getNameInit() == CurRec->getNameInit())
2479       return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() +
2480                    "' already defined in this multiclass!");
2481   CurMultiClass->DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
2482 
2483   // Copy the template arguments for the multiclass into the new def.
2484   for (Init * TA : CurMultiClass->Rec.getTemplateArgs()) {
2485     const RecordVal *RV = CurMultiClass->Rec.getValue(TA);
2486     assert(RV && "Template arg doesn't exist?");
2487     CurRec->addValue(*RV);
2488   }
2489 
2490   return false;
2491 }
2492 
2493 /// ParseDefm - Parse the instantiation of a multiclass.
2494 ///
2495 ///   DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2496 ///
ParseDefm(MultiClass * CurMultiClass)2497 bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
2498   assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
2499   SMLoc DefmLoc = Lex.getLoc();
2500   Init *DefmPrefix = nullptr;
2501 
2502   if (Lex.Lex() == tgtok::Id) {  // eat the defm.
2503     DefmPrefix = ParseObjectName(CurMultiClass);
2504   }
2505 
2506   SMLoc DefmPrefixEndLoc = Lex.getLoc();
2507   if (Lex.getCode() != tgtok::colon)
2508     return TokError("expected ':' after defm identifier");
2509 
2510   // Keep track of the new generated record definitions.
2511   std::vector<Record*> NewRecDefs;
2512 
2513   // This record also inherits from a regular class (non-multiclass)?
2514   bool InheritFromClass = false;
2515 
2516   // eat the colon.
2517   Lex.Lex();
2518 
2519   SMLoc SubClassLoc = Lex.getLoc();
2520   SubClassReference Ref = ParseSubClassReference(nullptr, true);
2521 
2522   while (1) {
2523     if (!Ref.Rec) return true;
2524 
2525     // To instantiate a multiclass, we need to first get the multiclass, then
2526     // instantiate each def contained in the multiclass with the SubClassRef
2527     // template parameters.
2528     MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
2529     assert(MC && "Didn't lookup multiclass correctly?");
2530     std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
2531 
2532     // Verify that the correct number of template arguments were specified.
2533     ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
2534     if (TArgs.size() < TemplateVals.size())
2535       return Error(SubClassLoc,
2536                    "more template args specified than multiclass expects");
2537 
2538     // Loop over all the def's in the multiclass, instantiating each one.
2539     for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2540       // The record name construction goes as follow:
2541       //  - If the def name is a string, prepend the prefix.
2542       //  - If the def name is a more complex pattern, use that pattern.
2543       // As a result, the record is instantiated before resolving
2544       // arguments, as it would make its name a string.
2545       Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
2546                                                 SMRange(DefmLoc,
2547                                                         DefmPrefixEndLoc),
2548                                                 TArgs, TemplateVals);
2549       if (!CurRec)
2550         return true;
2551 
2552       // Now that the record is instantiated, we can resolve arguments.
2553       if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
2554                                    TArgs, TemplateVals, true/*Delete args*/))
2555         return Error(SubClassLoc, "could not instantiate def");
2556 
2557       if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
2558         return Error(SubClassLoc, "could not instantiate def");
2559 
2560       // Defs that can be used by other definitions should be fully resolved
2561       // before any use.
2562       if (DefProto->isResolveFirst() && !CurMultiClass) {
2563         CurRec->resolveReferences();
2564         CurRec->setResolveFirst(false);
2565       }
2566       NewRecDefs.push_back(CurRec);
2567     }
2568 
2569 
2570     if (Lex.getCode() != tgtok::comma) break;
2571     Lex.Lex(); // eat ','.
2572 
2573     if (Lex.getCode() != tgtok::Id)
2574       return TokError("expected identifier");
2575 
2576     SubClassLoc = Lex.getLoc();
2577 
2578     // A defm can inherit from regular classes (non-multiclass) as
2579     // long as they come in the end of the inheritance list.
2580     InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
2581 
2582     if (InheritFromClass)
2583       break;
2584 
2585     Ref = ParseSubClassReference(nullptr, true);
2586   }
2587 
2588   if (InheritFromClass) {
2589     // Process all the classes to inherit as if they were part of a
2590     // regular 'def' and inherit all record values.
2591     SubClassReference SubClass = ParseSubClassReference(nullptr, false);
2592     while (1) {
2593       // Check for error.
2594       if (!SubClass.Rec) return true;
2595 
2596       // Get the expanded definition prototypes and teach them about
2597       // the record values the current class to inherit has
2598       for (Record *CurRec : NewRecDefs) {
2599         // Add it.
2600         if (AddSubClass(CurRec, SubClass))
2601           return true;
2602 
2603         if (ApplyLetStack(CurRec))
2604           return true;
2605       }
2606 
2607       if (Lex.getCode() != tgtok::comma) break;
2608       Lex.Lex(); // eat ','.
2609       SubClass = ParseSubClassReference(nullptr, false);
2610     }
2611   }
2612 
2613   if (!CurMultiClass)
2614     for (Record *CurRec : NewRecDefs)
2615       // See Record::setName().  This resolve step will see any new
2616       // name for the def that might have been created when resolving
2617       // inheritance, values and arguments above.
2618       CurRec->resolveReferences();
2619 
2620   if (Lex.getCode() != tgtok::semi)
2621     return TokError("expected ';' at end of defm");
2622   Lex.Lex();
2623 
2624   return false;
2625 }
2626 
2627 /// ParseObject
2628 ///   Object ::= ClassInst
2629 ///   Object ::= DefInst
2630 ///   Object ::= MultiClassInst
2631 ///   Object ::= DefMInst
2632 ///   Object ::= LETCommand '{' ObjectList '}'
2633 ///   Object ::= LETCommand Object
ParseObject(MultiClass * MC)2634 bool TGParser::ParseObject(MultiClass *MC) {
2635   switch (Lex.getCode()) {
2636   default:
2637     return TokError("Expected class, def, defm, multiclass or let definition");
2638   case tgtok::Let:   return ParseTopLevelLet(MC);
2639   case tgtok::Def:   return ParseDef(MC);
2640   case tgtok::Foreach:   return ParseForeach(MC);
2641   case tgtok::Defm:  return ParseDefm(MC);
2642   case tgtok::Class: return ParseClass();
2643   case tgtok::MultiClass: return ParseMultiClass();
2644   }
2645 }
2646 
2647 /// ParseObjectList
2648 ///   ObjectList :== Object*
ParseObjectList(MultiClass * MC)2649 bool TGParser::ParseObjectList(MultiClass *MC) {
2650   while (isObjectStart(Lex.getCode())) {
2651     if (ParseObject(MC))
2652       return true;
2653   }
2654   return false;
2655 }
2656 
ParseFile()2657 bool TGParser::ParseFile() {
2658   Lex.Lex(); // Prime the lexer.
2659   if (ParseObjectList()) return true;
2660 
2661   // If we have unread input at the end of the file, report it.
2662   if (Lex.getCode() == tgtok::Eof)
2663     return false;
2664 
2665   return TokError("Unexpected input at top level");
2666 }
2667 
2668