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