LLVM  16.0.0git
TGParser.cpp
Go to the documentation of this file.
1 //===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implement the Parser for TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TGParser.h"
14 #include "llvm/ADT/DenseMapInfo.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Compiler.h"
24 #include <algorithm>
25 #include <cassert>
26 #include <cstdint>
27 #include <limits>
28 
29 using namespace llvm;
30 
31 //===----------------------------------------------------------------------===//
32 // Support Code for the Semantic Actions.
33 //===----------------------------------------------------------------------===//
34 
35 namespace llvm {
36 
41 
42  SubClassReference() : Rec(nullptr) {}
43 
44  bool isInvalid() const { return Rec == nullptr; }
45 };
46 
51 
52  SubMultiClassReference() : MC(nullptr) {}
53 
54  bool isInvalid() const { return MC == nullptr; }
55  void dump() const;
56 };
57 
58 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60  errs() << "Multiclass:\n";
61 
62  MC->dump();
63 
64  errs() << "Template args:\n";
65  for (Init *TA : TemplateArgs)
66  TA->dump();
67 }
68 #endif
69 
70 } // end namespace llvm
71 
72 static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
73  BitsInit *BV = cast<BitsInit>(RV.getValue());
74  for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
75  Init *Bit = BV->getBit(i);
76  bool IsReference = false;
77  if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
78  if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
79  if (R.getValue(VI->getName()))
80  IsReference = true;
81  }
82  } else if (isa<VarInit>(Bit)) {
83  IsReference = true;
84  }
85  if (!(IsReference || Bit->isConcrete()))
86  return false;
87  }
88  return true;
89 }
90 
91 static void checkConcrete(Record &R) {
92  for (const RecordVal &RV : R.getValues()) {
93  // HACK: Disable this check for variables declared with 'field'. This is
94  // done merely because existing targets have legitimate cases of
95  // non-concrete variables in helper defs. Ideally, we'd introduce a
96  // 'maybe' or 'optional' modifier instead of this.
97  if (RV.isNonconcreteOK())
98  continue;
99 
100  if (Init *V = RV.getValue()) {
101  bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
102  if (!Ok) {
103  PrintError(R.getLoc(),
104  Twine("Initializer of '") + RV.getNameInitAsString() +
105  "' in '" + R.getNameInitAsString() +
106  "' could not be fully resolved: " +
107  RV.getValue()->getAsString());
108  }
109  }
110  }
111 }
112 
113 /// Return an Init with a qualifier prefix referring
114 /// to CurRec's name.
115 static Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name,
116  StringRef Scoper) {
117  RecordKeeper &RK = CurRec.getRecords();
118  Init *NewName = BinOpInit::getStrConcat(CurRec.getNameInit(),
119  StringInit::get(RK, Scoper));
120  NewName = BinOpInit::getStrConcat(NewName, Name);
121  if (CurMultiClass && Scoper != "::") {
122  Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(),
123  StringInit::get(RK, "::"));
124  NewName = BinOpInit::getStrConcat(Prefix, NewName);
125  }
126 
127  if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
128  NewName = BinOp->Fold(&CurRec);
129  return NewName;
130 }
131 
132 /// Return the qualified version of the implicit 'NAME' template argument.
134  MultiClass *MC = nullptr) {
135  return QualifyName(Rec, MC, StringInit::get(Rec.getRecords(), "NAME"),
136  MC ? "::" : ":");
137 }
138 
140  return QualifiedNameOfImplicitName(MC->Rec, MC);
141 }
142 
143 bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
144  if (!CurRec)
145  CurRec = &CurMultiClass->Rec;
146 
147  if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
148  // The value already exists in the class, treat this as a set.
149  if (ERV->setValue(RV.getValue()))
150  return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
151  RV.getType()->getAsString() + "' is incompatible with " +
152  "previous definition of type '" +
153  ERV->getType()->getAsString() + "'");
154  } else {
155  CurRec->addValue(RV);
156  }
157  return false;
158 }
159 
160 /// SetValue -
161 /// Return true on error, false on success.
162 bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
163  ArrayRef<unsigned> BitList, Init *V,
164  bool AllowSelfAssignment, bool OverrideDefLoc) {
165  if (!V) return false;
166 
167  if (!CurRec) CurRec = &CurMultiClass->Rec;
168 
169  RecordVal *RV = CurRec->getValue(ValName);
170  if (!RV)
171  return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
172  "' unknown!");
173 
174  // Do not allow assignments like 'X = X'. This will just cause infinite loops
175  // in the resolution machinery.
176  if (BitList.empty())
177  if (VarInit *VI = dyn_cast<VarInit>(V))
178  if (VI->getNameInit() == ValName && !AllowSelfAssignment)
179  return Error(Loc, "Recursion / self-assignment forbidden");
180 
181  // If we are assigning to a subset of the bits in the value... then we must be
182  // assigning to a field of BitsRecTy, which must have a BitsInit
183  // initializer.
184  //
185  if (!BitList.empty()) {
186  BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
187  if (!CurVal)
188  return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
189  "' is not a bits type");
190 
191  // Convert the incoming value to a bits type of the appropriate size...
192  Init *BI = V->getCastTo(BitsRecTy::get(Records, BitList.size()));
193  if (!BI)
194  return Error(Loc, "Initializer is not compatible with bit range");
195 
196  SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
197 
198  // Loop over bits, assigning values as appropriate.
199  for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
200  unsigned Bit = BitList[i];
201  if (NewBits[Bit])
202  return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
203  ValName->getAsUnquotedString() + "' more than once");
204  NewBits[Bit] = BI->getBit(i);
205  }
206 
207  for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
208  if (!NewBits[i])
209  NewBits[i] = CurVal->getBit(i);
210 
211  V = BitsInit::get(Records, NewBits);
212  }
213 
214  if (OverrideDefLoc ? RV->setValue(V, Loc) : RV->setValue(V)) {
215  std::string InitType;
216  if (BitsInit *BI = dyn_cast<BitsInit>(V))
217  InitType = (Twine("' of type bit initializer with length ") +
218  Twine(BI->getNumBits())).str();
219  else if (TypedInit *TI = dyn_cast<TypedInit>(V))
220  InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
221  return Error(Loc, "Field '" + ValName->getAsUnquotedString() +
222  "' of type '" + RV->getType()->getAsString() +
223  "' is incompatible with value '" +
224  V->getAsString() + InitType + "'");
225  }
226  return false;
227 }
228 
229 /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
230 /// args as SubClass's template arguments.
231 bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
232  Record *SC = SubClass.Rec;
233  MapResolver R(CurRec);
234 
235  // Loop over all the subclass record's fields. Add template arguments
236  // to the resolver map. Add regular fields to the new record.
237  for (const RecordVal &Field : SC->getValues()) {
238  if (Field.isTemplateArg()) {
239  R.set(Field.getNameInit(), Field.getValue());
240  } else {
241  if (AddValue(CurRec, SubClass.RefRange.Start, Field))
242  return true;
243  }
244  }
245 
246  ArrayRef<Init *> TArgs = SC->getTemplateArgs();
247  assert(SubClass.TemplateArgs.size() <= TArgs.size() &&
248  "Too many template arguments allowed");
249 
250  // Loop over the template argument names. If a value was specified,
251  // reset the map value. If not and there was no default, complain.
252  for (unsigned I = 0, E = TArgs.size(); I != E; ++I) {
253  if (I < SubClass.TemplateArgs.size())
254  R.set(TArgs[I], SubClass.TemplateArgs[I]);
255  else if (!R.isComplete(TArgs[I]))
256  return Error(SubClass.RefRange.Start,
257  "Value not specified for template argument '" +
258  TArgs[I]->getAsUnquotedString() + "' (#" + Twine(I) +
259  ") of parent class '" + SC->getNameInitAsString() + "'");
260  }
261 
262  // Copy the subclass record's assertions to the new record.
263  CurRec->appendAssertions(SC);
264 
265  Init *Name;
266  if (CurRec->isClass())
268  StringRecTy::get(Records));
269  else
270  Name = CurRec->getNameInit();
272 
273  CurRec->resolveReferences(R);
274 
275  // Since everything went well, we can now set the "superclass" list for the
276  // current record.
277  ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
278  for (const auto &SCPair : SCs) {
279  if (CurRec->isSubClassOf(SCPair.first))
280  return Error(SubClass.RefRange.Start,
281  "Already subclass of '" + SCPair.first->getName() + "'!\n");
282  CurRec->addSuperClass(SCPair.first, SCPair.second);
283  }
284 
285  if (CurRec->isSubClassOf(SC))
286  return Error(SubClass.RefRange.Start,
287  "Already subclass of '" + SC->getName() + "'!\n");
288  CurRec->addSuperClass(SC, SubClass.RefRange);
289  return false;
290 }
291 
292 bool TGParser::AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass) {
293  if (Entry.Rec)
294  return AddSubClass(Entry.Rec.get(), SubClass);
295 
296  if (Entry.Assertion)
297  return false;
298 
299  for (auto &E : Entry.Loop->Entries) {
300  if (AddSubClass(E, SubClass))
301  return true;
302  }
303 
304  return false;
305 }
306 
307 /// AddSubMultiClass - Add SubMultiClass as a subclass to
308 /// CurMC, resolving its template args as SubMultiClass's
309 /// template arguments.
310 bool TGParser::AddSubMultiClass(MultiClass *CurMC,
311  SubMultiClassReference &SubMultiClass) {
312  MultiClass *SMC = SubMultiClass.MC;
313 
314  ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
315  if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
316  return Error(SubMultiClass.RefRange.Start,
317  "More template args specified than expected");
318 
319  // Prepare the mapping of template argument name to value, filling in default
320  // values if necessary.
321  SubstStack TemplateArgs;
322  for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
323  if (i < SubMultiClass.TemplateArgs.size()) {
324  TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.TemplateArgs[i]);
325  } else {
326  Init *Default = SMC->Rec.getValue(SMCTArgs[i])->getValue();
327  if (!Default->isComplete()) {
328  return Error(SubMultiClass.RefRange.Start,
329  "value not specified for template argument #" + Twine(i) +
330  " (" + SMCTArgs[i]->getAsUnquotedString() +
331  ") of multiclass '" + SMC->Rec.getNameInitAsString() +
332  "'");
333  }
334  TemplateArgs.emplace_back(SMCTArgs[i], Default);
335  }
336  }
337 
338  TemplateArgs.emplace_back(QualifiedNameOfImplicitName(SMC),
340  StringRecTy::get(Records)));
341 
342  // Add all of the defs in the subclass into the current multiclass.
343  return resolve(SMC->Entries, TemplateArgs, false, &CurMC->Entries);
344 }
345 
346 /// Add a record, foreach loop, or assertion to the current context.
347 bool TGParser::addEntry(RecordsEntry E) {
348  assert((!!E.Rec + !!E.Loop + !!E.Assertion) == 1 &&
349  "RecordsEntry has invalid number of items");
350 
351  // If we are parsing a loop, add it to the loop's entries.
352  if (!Loops.empty()) {
353  Loops.back()->Entries.push_back(std::move(E));
354  return false;
355  }
356 
357  // If it is a loop, then resolve and perform the loop.
358  if (E.Loop) {
359  SubstStack Stack;
360  return resolve(*E.Loop, Stack, CurMultiClass == nullptr,
361  CurMultiClass ? &CurMultiClass->Entries : nullptr);
362  }
363 
364  // If we are parsing a multiclass, add it to the multiclass's entries.
365  if (CurMultiClass) {
366  CurMultiClass->Entries.push_back(std::move(E));
367  return false;
368  }
369 
370  // If it is an assertion, then it's a top-level one, so check it.
371  if (E.Assertion) {
372  CheckAssert(E.Assertion->Loc, E.Assertion->Condition, E.Assertion->Message);
373  return false;
374  }
375 
376  // It must be a record, so finish it off.
377  return addDefOne(std::move(E.Rec));
378 }
379 
380 /// Resolve the entries in \p Loop, going over inner loops recursively
381 /// and making the given subsitutions of (name, value) pairs.
382 ///
383 /// The resulting records are stored in \p Dest if non-null. Otherwise, they
384 /// are added to the global record keeper.
385 bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs,
386  bool Final, std::vector<RecordsEntry> *Dest,
387  SMLoc *Loc) {
388  MapResolver R;
389  for (const auto &S : Substs)
390  R.set(S.first, S.second);
391  Init *List = Loop.ListValue->resolveReferences(R);
392  auto LI = dyn_cast<ListInit>(List);
393  if (!LI) {
394  if (!Final) {
395  Dest->emplace_back(std::make_unique<ForeachLoop>(Loop.Loc, Loop.IterVar,
396  List));
397  return resolve(Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
398  Loc);
399  }
400 
401  PrintError(Loop.Loc, Twine("attempting to loop over '") +
402  List->getAsString() + "', expected a list");
403  return true;
404  }
405 
406  bool Error = false;
407  for (auto *Elt : *LI) {
408  if (Loop.IterVar)
409  Substs.emplace_back(Loop.IterVar->getNameInit(), Elt);
410  Error = resolve(Loop.Entries, Substs, Final, Dest);
411  if (Loop.IterVar)
412  Substs.pop_back();
413  if (Error)
414  break;
415  }
416  return Error;
417 }
418 
419 /// Resolve the entries in \p Source, going over loops recursively and
420 /// making the given substitutions of (name, value) pairs.
421 ///
422 /// The resulting records are stored in \p Dest if non-null. Otherwise, they
423 /// are added to the global record keeper.
424 bool TGParser::resolve(const std::vector<RecordsEntry> &Source,
425  SubstStack &Substs, bool Final,
426  std::vector<RecordsEntry> *Dest, SMLoc *Loc) {
427  bool Error = false;
428  for (auto &E : Source) {
429  if (E.Loop) {
430  Error = resolve(*E.Loop, Substs, Final, Dest);
431 
432  } else if (E.Assertion) {
433  MapResolver R;
434  for (const auto &S : Substs)
435  R.set(S.first, S.second);
436  Init *Condition = E.Assertion->Condition->resolveReferences(R);
437  Init *Message = E.Assertion->Message->resolveReferences(R);
438 
439  if (Dest)
440  Dest->push_back(std::make_unique<Record::AssertionInfo>(
441  E.Assertion->Loc, Condition, Message));
442  else
443  CheckAssert(E.Assertion->Loc, Condition, Message);
444 
445  } else {
446  auto Rec = std::make_unique<Record>(*E.Rec);
447  if (Loc)
448  Rec->appendLoc(*Loc);
449 
450  MapResolver R(Rec.get());
451  for (const auto &S : Substs)
452  R.set(S.first, S.second);
453  Rec->resolveReferences(R);
454 
455  if (Dest)
456  Dest->push_back(std::move(Rec));
457  else
458  Error = addDefOne(std::move(Rec));
459  }
460  if (Error)
461  break;
462  }
463  return Error;
464 }
465 
466 /// Resolve the record fully and add it to the record keeper.
467 bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
468  Init *NewName = nullptr;
469  if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
470  if (!Rec->isAnonymous()) {
471  PrintError(Rec->getLoc(),
472  "def already exists: " + Rec->getNameInitAsString());
473  PrintNote(Prev->getLoc(), "location of previous definition");
474  return true;
475  }
476  NewName = Records.getNewAnonymousName();
477  }
478 
479  Rec->resolveReferences(NewName);
480  checkConcrete(*Rec);
481 
482  if (!isa<StringInit>(Rec->getNameInit())) {
483  PrintError(Rec->getLoc(), Twine("record name '") +
484  Rec->getNameInit()->getAsString() +
485  "' could not be fully resolved");
486  return true;
487  }
488 
489  // Check the assertions.
490  Rec->checkRecordAssertions();
491 
492  // If ObjectBody has template arguments, it's an error.
493  assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
494 
495  for (DefsetRecord *Defset : Defsets) {
496  DefInit *I = Rec->getDefInit();
497  if (!I->getType()->typeIsA(Defset->EltTy)) {
498  PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
499  I->getType()->getAsString() +
500  "' to defset");
501  PrintNote(Defset->Loc, "location of defset declaration");
502  return true;
503  }
504  Defset->Elements.push_back(I);
505  }
506 
507  Records.addDef(std::move(Rec));
508  return false;
509 }
510 
511 //===----------------------------------------------------------------------===//
512 // Parser Code
513 //===----------------------------------------------------------------------===//
514 
515 /// isObjectStart - Return true if this is a valid first token for a statement.
517  return K == tgtok::Assert || K == tgtok::Class || K == tgtok::Def ||
518  K == tgtok::Defm || K == tgtok::Defset || K == tgtok::Defvar ||
519  K == tgtok::Foreach || K == tgtok::If || K == tgtok::Let ||
520  K == tgtok::MultiClass;
521 }
522 
523 bool TGParser::consume(tgtok::TokKind K) {
524  if (Lex.getCode() == K) {
525  Lex.Lex();
526  return true;
527  }
528  return false;
529 }
530 
531 /// ParseObjectName - If a valid object name is specified, return it. If no
532 /// name is specified, return the unset initializer. Return nullptr on parse
533 /// error.
534 /// ObjectName ::= Value [ '#' Value ]*
535 /// ObjectName ::= /*empty*/
536 ///
537 Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
538  switch (Lex.getCode()) {
539  case tgtok::colon:
540  case tgtok::semi:
541  case tgtok::l_brace:
542  // These are all of the tokens that can begin an object body.
543  // Some of these can also begin values but we disallow those cases
544  // because they are unlikely to be useful.
545  return UnsetInit::get(Records);
546  default:
547  break;
548  }
549 
550  Record *CurRec = nullptr;
551  if (CurMultiClass)
552  CurRec = &CurMultiClass->Rec;
553 
554  Init *Name = ParseValue(CurRec, StringRecTy::get(Records), ParseNameMode);
555  if (!Name)
556  return nullptr;
557 
558  if (CurMultiClass) {
559  Init *NameStr = QualifiedNameOfImplicitName(CurMultiClass);
560  HasReferenceResolver R(NameStr);
561  Name->resolveReferences(R);
562  if (!R.found())
564  VarInit::get(NameStr, StringRecTy::get(Records)), Name);
565  }
566 
567  return Name;
568 }
569 
570 /// ParseClassID - Parse and resolve a reference to a class name. This returns
571 /// null on error.
572 ///
573 /// ClassID ::= ID
574 ///
575 Record *TGParser::ParseClassID() {
576  if (Lex.getCode() != tgtok::Id) {
577  TokError("expected name for ClassID");
578  return nullptr;
579  }
580 
581  Record *Result = Records.getClass(Lex.getCurStrVal());
582  if (!Result) {
583  std::string Msg("Couldn't find class '" + Lex.getCurStrVal() + "'");
584  if (MultiClasses[Lex.getCurStrVal()].get())
585  TokError(Msg + ". Use 'defm' if you meant to use multiclass '" +
586  Lex.getCurStrVal() + "'");
587  else
588  TokError(Msg);
589  } else if (TrackReferenceLocs) {
590  Result->appendReferenceLoc(Lex.getLocRange());
591  }
592 
593  Lex.Lex();
594  return Result;
595 }
596 
597 /// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
598 /// This returns null on error.
599 ///
600 /// MultiClassID ::= ID
601 ///
602 MultiClass *TGParser::ParseMultiClassID() {
603  if (Lex.getCode() != tgtok::Id) {
604  TokError("expected name for MultiClassID");
605  return nullptr;
606  }
607 
608  MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
609  if (!Result)
610  TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
611 
612  Lex.Lex();
613  return Result;
614 }
615 
616 /// ParseSubClassReference - Parse a reference to a subclass or a
617 /// multiclass. This returns a SubClassRefTy with a null Record* on error.
618 ///
619 /// SubClassRef ::= ClassID
620 /// SubClassRef ::= ClassID '<' ValueList '>'
621 ///
622 SubClassReference TGParser::
623 ParseSubClassReference(Record *CurRec, bool isDefm) {
625  Result.RefRange.Start = Lex.getLoc();
626 
627  if (isDefm) {
628  if (MultiClass *MC = ParseMultiClassID())
629  Result.Rec = &MC->Rec;
630  } else {
631  Result.Rec = ParseClassID();
632  }
633  if (!Result.Rec) return Result;
634 
635  // If there is no template arg list, we're done.
636  if (!consume(tgtok::less)) {
637  Result.RefRange.End = Lex.getLoc();
638  return Result;
639  }
640 
641  if (ParseTemplateArgValueList(Result.TemplateArgs, CurRec, Result.Rec)) {
642  Result.Rec = nullptr; // Error parsing value list.
643  return Result;
644  }
645 
646  if (CheckTemplateArgValues(Result.TemplateArgs, Result.RefRange.Start,
647  Result.Rec)) {
648  Result.Rec = nullptr; // Error checking value list.
649  return Result;
650  }
651 
652  Result.RefRange.End = Lex.getLoc();
653  return Result;
654 }
655 
656 /// ParseSubMultiClassReference - Parse a reference to a subclass or to a
657 /// templated submulticlass. This returns a SubMultiClassRefTy with a null
658 /// Record* on error.
659 ///
660 /// SubMultiClassRef ::= MultiClassID
661 /// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
662 ///
663 SubMultiClassReference TGParser::
664 ParseSubMultiClassReference(MultiClass *CurMC) {
666  Result.RefRange.Start = Lex.getLoc();
667 
668  Result.MC = ParseMultiClassID();
669  if (!Result.MC) return Result;
670 
671  // If there is no template arg list, we're done.
672  if (!consume(tgtok::less)) {
673  Result.RefRange.End = Lex.getLoc();
674  return Result;
675  }
676 
677  if (ParseTemplateArgValueList(Result.TemplateArgs, &CurMC->Rec,
678  &Result.MC->Rec)) {
679  Result.MC = nullptr; // Error parsing value list.
680  return Result;
681  }
682 
683  Result.RefRange.End = Lex.getLoc();
684 
685  return Result;
686 }
687 
688 /// ParseRangePiece - Parse a bit/value range.
689 /// RangePiece ::= INTVAL
690 /// RangePiece ::= INTVAL '...' INTVAL
691 /// RangePiece ::= INTVAL '-' INTVAL
692 /// RangePiece ::= INTVAL INTVAL
693 // The last two forms are deprecated.
694 bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
695  TypedInit *FirstItem) {
696  Init *CurVal = FirstItem;
697  if (!CurVal)
698  CurVal = ParseValue(nullptr);
699 
700  IntInit *II = dyn_cast_or_null<IntInit>(CurVal);
701  if (!II)
702  return TokError("expected integer or bitrange");
703 
704  int64_t Start = II->getValue();
705  int64_t End;
706 
707  if (Start < 0)
708  return TokError("invalid range, cannot be negative");
709 
710  switch (Lex.getCode()) {
711  default:
712  Ranges.push_back(Start);
713  return false;
714 
715  case tgtok::dotdotdot:
716  case tgtok::minus: {
717  Lex.Lex(); // eat
718 
719  Init *I_End = ParseValue(nullptr);
720  IntInit *II_End = dyn_cast_or_null<IntInit>(I_End);
721  if (!II_End) {
722  TokError("expected integer value as end of range");
723  return true;
724  }
725 
726  End = II_End->getValue();
727  break;
728  }
729  case tgtok::IntVal: {
730  End = -Lex.getCurIntVal();
731  Lex.Lex();
732  break;
733  }
734  }
735  if (End < 0)
736  return TokError("invalid range, cannot be negative");
737 
738  // Add to the range.
739  if (Start < End)
740  for (; Start <= End; ++Start)
741  Ranges.push_back(Start);
742  else
743  for (; Start >= End; --Start)
744  Ranges.push_back(Start);
745  return false;
746 }
747 
748 /// ParseRangeList - Parse a list of scalars and ranges into scalar values.
749 ///
750 /// RangeList ::= RangePiece (',' RangePiece)*
751 ///
752 void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
753  // Parse the first piece.
754  if (ParseRangePiece(Result)) {
755  Result.clear();
756  return;
757  }
758  while (consume(tgtok::comma))
759  // Parse the next range piece.
760  if (ParseRangePiece(Result)) {
761  Result.clear();
762  return;
763  }
764 }
765 
766 /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
767 /// OptionalRangeList ::= '<' RangeList '>'
768 /// OptionalRangeList ::= /*empty*/
769 bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
770  SMLoc StartLoc = Lex.getLoc();
771  if (!consume(tgtok::less))
772  return false;
773 
774  // Parse the range list.
775  ParseRangeList(Ranges);
776  if (Ranges.empty()) return true;
777 
778  if (!consume(tgtok::greater)) {
779  TokError("expected '>' at end of range list");
780  return Error(StartLoc, "to match this '<'");
781  }
782  return false;
783 }
784 
785 /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
786 /// OptionalBitList ::= '{' RangeList '}'
787 /// OptionalBitList ::= /*empty*/
788 bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
789  SMLoc StartLoc = Lex.getLoc();
790  if (!consume(tgtok::l_brace))
791  return false;
792 
793  // Parse the range list.
794  ParseRangeList(Ranges);
795  if (Ranges.empty()) return true;
796 
797  if (!consume(tgtok::r_brace)) {
798  TokError("expected '}' at end of bit list");
799  return Error(StartLoc, "to match this '{'");
800  }
801  return false;
802 }
803 
804 /// ParseType - Parse and return a tblgen type. This returns null on error.
805 ///
806 /// Type ::= STRING // string type
807 /// Type ::= CODE // code type
808 /// Type ::= BIT // bit type
809 /// Type ::= BITS '<' INTVAL '>' // bits<x> type
810 /// Type ::= INT // int type
811 /// Type ::= LIST '<' Type '>' // list<x> type
812 /// Type ::= DAG // dag type
813 /// Type ::= ClassID // Record Type
814 ///
815 RecTy *TGParser::ParseType() {
816  switch (Lex.getCode()) {
817  default: TokError("Unknown token when expecting a type"); return nullptr;
818  case tgtok::String:
819  case tgtok::Code:
820  Lex.Lex();
821  return StringRecTy::get(Records);
822  case tgtok::Bit:
823  Lex.Lex();
824  return BitRecTy::get(Records);
825  case tgtok::Int:
826  Lex.Lex();
827  return IntRecTy::get(Records);
828  case tgtok::Dag:
829  Lex.Lex();
830  return DagRecTy::get(Records);
831  case tgtok::Id:
832  if (Record *R = ParseClassID())
833  return RecordRecTy::get(R);
834  TokError("unknown class name");
835  return nullptr;
836  case tgtok::Bits: {
837  if (Lex.Lex() != tgtok::less) { // Eat 'bits'
838  TokError("expected '<' after bits type");
839  return nullptr;
840  }
841  if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
842  TokError("expected integer in bits<n> type");
843  return nullptr;
844  }
845  uint64_t Val = Lex.getCurIntVal();
846  if (Lex.Lex() != tgtok::greater) { // Eat count.
847  TokError("expected '>' at end of bits<n> type");
848  return nullptr;
849  }
850  Lex.Lex(); // Eat '>'
851  return BitsRecTy::get(Records, Val);
852  }
853  case tgtok::List: {
854  if (Lex.Lex() != tgtok::less) { // Eat 'bits'
855  TokError("expected '<' after list type");
856  return nullptr;
857  }
858  Lex.Lex(); // Eat '<'
859  RecTy *SubType = ParseType();
860  if (!SubType) return nullptr;
861 
862  if (!consume(tgtok::greater)) {
863  TokError("expected '>' at end of list<ty> type");
864  return nullptr;
865  }
866  return ListRecTy::get(SubType);
867  }
868  }
869 }
870 
871 /// ParseIDValue
872 Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMRange NameLoc,
873  IDParseMode Mode) {
874  if (CurRec) {
875  if (RecordVal *RV = CurRec->getValue(Name)) {
876  if (TrackReferenceLocs)
877  RV->addReferenceLoc(NameLoc);
878  return VarInit::get(Name, RV->getType());
879  }
880  }
881 
882  if ((CurRec && CurRec->isClass()) || CurMultiClass) {
883  Init *TemplateArgName;
884  if (CurMultiClass) {
885  TemplateArgName =
886  QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
887  } else
888  TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
889 
890  Record *TemplateRec = CurMultiClass ? &CurMultiClass->Rec : CurRec;
891  if (TemplateRec->isTemplateArg(TemplateArgName)) {
892  RecordVal *RV = TemplateRec->getValue(TemplateArgName);
893  assert(RV && "Template arg doesn't exist??");
894  RV->setUsed(true);
895  if (TrackReferenceLocs)
896  RV->addReferenceLoc(NameLoc);
897  return VarInit::get(TemplateArgName, RV->getType());
898  } else if (Name->getValue() == "NAME") {
899  return VarInit::get(TemplateArgName, StringRecTy::get(Records));
900  }
901  }
902 
903  if (CurLocalScope)
904  if (Init *I = CurLocalScope->getVar(Name->getValue()))
905  return I;
906 
907  // If this is in a foreach loop, make sure it's not a loop iterator
908  for (const auto &L : Loops) {
909  if (L->IterVar) {
910  VarInit *IterVar = dyn_cast<VarInit>(L->IterVar);
911  if (IterVar && IterVar->getNameInit() == Name)
912  return IterVar;
913  }
914  }
915 
916  if (Mode == ParseNameMode)
917  return Name;
918 
919  if (Init *I = Records.getGlobal(Name->getValue())) {
920  // Add a reference to the global if it's a record.
921  if (TrackReferenceLocs) {
922  if (auto *Def = dyn_cast<DefInit>(I))
923  Def->getDef()->appendReferenceLoc(NameLoc);
924  }
925  return I;
926  }
927 
928  // Allow self-references of concrete defs, but delay the lookup so that we
929  // get the correct type.
930  if (CurRec && !CurRec->isClass() && !CurMultiClass &&
931  CurRec->getNameInit() == Name)
932  return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
933 
934  Error(NameLoc.Start, "Variable not defined: '" + Name->getValue() + "'");
935  return nullptr;
936 }
937 
938 /// ParseOperation - Parse an operator. This returns null on error.
939 ///
940 /// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
941 ///
942 Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
943  switch (Lex.getCode()) {
944  default:
945  TokError("unknown bang operator");
946  return nullptr;
947  case tgtok::XNOT:
948  case tgtok::XHead:
949  case tgtok::XTail:
950  case tgtok::XSize:
951  case tgtok::XEmpty:
952  case tgtok::XCast:
953  case tgtok::XGetDagOp: { // Value ::= !unop '(' Value ')'
955  RecTy *Type = nullptr;
956 
957  switch (Lex.getCode()) {
958  default: llvm_unreachable("Unhandled code!");
959  case tgtok::XCast:
960  Lex.Lex(); // eat the operation
962 
963  Type = ParseOperatorType();
964 
965  if (!Type) {
966  TokError("did not get type for unary operator");
967  return nullptr;
968  }
969 
970  break;
971  case tgtok::XNOT:
972  Lex.Lex(); // eat the operation
974  Type = IntRecTy::get(Records);
975  break;
976  case tgtok::XHead:
977  Lex.Lex(); // eat the operation
979  break;
980  case tgtok::XTail:
981  Lex.Lex(); // eat the operation
983  break;
984  case tgtok::XSize:
985  Lex.Lex();
987  Type = IntRecTy::get(Records);
988  break;
989  case tgtok::XEmpty:
990  Lex.Lex(); // eat the operation
992  Type = IntRecTy::get(Records);
993  break;
994  case tgtok::XGetDagOp:
995  Lex.Lex(); // eat the operation
996  if (Lex.getCode() == tgtok::less) {
997  // Parse an optional type suffix, so that you can say
998  // !getdagop<BaseClass>(someDag) as a shorthand for
999  // !cast<BaseClass>(!getdagop(someDag)).
1000  Type = ParseOperatorType();
1001 
1002  if (!Type) {
1003  TokError("did not get type for unary operator");
1004  return nullptr;
1005  }
1006 
1007  if (!isa<RecordRecTy>(Type)) {
1008  TokError("type for !getdagop must be a record type");
1009  // but keep parsing, to consume the operand
1010  }
1011  } else {
1012  Type = RecordRecTy::get(Records, {});
1013  }
1015  break;
1016  }
1017  if (!consume(tgtok::l_paren)) {
1018  TokError("expected '(' after unary operator");
1019  return nullptr;
1020  }
1021 
1022  Init *LHS = ParseValue(CurRec);
1023  if (!LHS) return nullptr;
1024 
1025  if (Code == UnOpInit::EMPTY || Code == UnOpInit::SIZE) {
1026  ListInit *LHSl = dyn_cast<ListInit>(LHS);
1027  StringInit *LHSs = dyn_cast<StringInit>(LHS);
1028  DagInit *LHSd = dyn_cast<DagInit>(LHS);
1029  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1030  if (!LHSl && !LHSs && !LHSd && !LHSt) {
1031  TokError("expected string, list, or dag type argument in unary operator");
1032  return nullptr;
1033  }
1034  if (LHSt) {
1035  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1036  StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
1037  DagRecTy *DType = dyn_cast<DagRecTy>(LHSt->getType());
1038  if (!LType && !SType && !DType) {
1039  TokError("expected string, list, or dag type argument in unary operator");
1040  return nullptr;
1041  }
1042  }
1043  }
1044 
1045  if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
1046  ListInit *LHSl = dyn_cast<ListInit>(LHS);
1047  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1048  if (!LHSl && !LHSt) {
1049  TokError("expected list type argument in unary operator");
1050  return nullptr;
1051  }
1052  if (LHSt) {
1053  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1054  if (!LType) {
1055  TokError("expected list type argument in unary operator");
1056  return nullptr;
1057  }
1058  }
1059 
1060  if (LHSl && LHSl->empty()) {
1061  TokError("empty list argument in unary operator");
1062  return nullptr;
1063  }
1064  if (LHSl) {
1065  Init *Item = LHSl->getElement(0);
1066  TypedInit *Itemt = dyn_cast<TypedInit>(Item);
1067  if (!Itemt) {
1068  TokError("untyped list element in unary operator");
1069  return nullptr;
1070  }
1071  Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
1072  : ListRecTy::get(Itemt->getType());
1073  } else {
1074  assert(LHSt && "expected list type argument in unary operator");
1075  ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
1076  Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
1077  }
1078  }
1079 
1080  if (!consume(tgtok::r_paren)) {
1081  TokError("expected ')' in unary operator");
1082  return nullptr;
1083  }
1084  return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
1085  }
1086 
1087  case tgtok::XIsA: {
1088  // Value ::= !isa '<' Type '>' '(' Value ')'
1089  Lex.Lex(); // eat the operation
1090 
1091  RecTy *Type = ParseOperatorType();
1092  if (!Type)
1093  return nullptr;
1094 
1095  if (!consume(tgtok::l_paren)) {
1096  TokError("expected '(' after type of !isa");
1097  return nullptr;
1098  }
1099 
1100  Init *LHS = ParseValue(CurRec);
1101  if (!LHS)
1102  return nullptr;
1103 
1104  if (!consume(tgtok::r_paren)) {
1105  TokError("expected ')' in !isa");
1106  return nullptr;
1107  }
1108 
1109  return (IsAOpInit::get(Type, LHS))->Fold();
1110  }
1111 
1112  case tgtok::XExists: {
1113  // Value ::= !exists '<' Type '>' '(' Value ')'
1114  Lex.Lex(); // eat the operation
1115 
1116  RecTy *Type = ParseOperatorType();
1117  if (!Type)
1118  return nullptr;
1119 
1120  if (!consume(tgtok::l_paren)) {
1121  TokError("expected '(' after type of !exists");
1122  return nullptr;
1123  }
1124 
1125  SMLoc ExprLoc = Lex.getLoc();
1126  Init *Expr = ParseValue(CurRec);
1127  if (!Expr)
1128  return nullptr;
1129 
1130  TypedInit *ExprType = dyn_cast<TypedInit>(Expr);
1131  if (!ExprType) {
1132  Error(ExprLoc, "expected string type argument in !exists operator");
1133  return nullptr;
1134  }
1135 
1136  RecordRecTy *RecType = dyn_cast<RecordRecTy>(ExprType->getType());
1137  if (RecType) {
1138  Error(ExprLoc,
1139  "expected string type argument in !exists operator, please "
1140  "use !isa instead");
1141  return nullptr;
1142  }
1143 
1144  StringRecTy *SType = dyn_cast<StringRecTy>(ExprType->getType());
1145  if (!SType) {
1146  Error(ExprLoc, "expected string type argument in !exists operator");
1147  return nullptr;
1148  }
1149 
1150  if (!consume(tgtok::r_paren)) {
1151  TokError("expected ')' in !exists");
1152  return nullptr;
1153  }
1154 
1155  return (ExistsOpInit::get(Type, Expr))->Fold(CurRec);
1156  }
1157 
1158  case tgtok::XConcat:
1159  case tgtok::XADD:
1160  case tgtok::XSUB:
1161  case tgtok::XMUL:
1162  case tgtok::XDIV:
1163  case tgtok::XAND:
1164  case tgtok::XOR:
1165  case tgtok::XXOR:
1166  case tgtok::XSRA:
1167  case tgtok::XSRL:
1168  case tgtok::XSHL:
1169  case tgtok::XEq:
1170  case tgtok::XNe:
1171  case tgtok::XLe:
1172  case tgtok::XLt:
1173  case tgtok::XGe:
1174  case tgtok::XGt:
1175  case tgtok::XListConcat:
1176  case tgtok::XListSplat:
1177  case tgtok::XStrConcat:
1178  case tgtok::XInterleave:
1179  case tgtok::XSetDagOp: { // Value ::= !binop '(' Value ',' Value ')'
1180  tgtok::TokKind OpTok = Lex.getCode();
1181  SMLoc OpLoc = Lex.getLoc();
1182  Lex.Lex(); // eat the operation
1183 
1185  switch (OpTok) {
1186  default: llvm_unreachable("Unhandled code!");
1187  case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
1188  case tgtok::XADD: Code = BinOpInit::ADD; break;
1189  case tgtok::XSUB: Code = BinOpInit::SUB; break;
1190  case tgtok::XMUL: Code = BinOpInit::MUL; break;
1191  case tgtok::XDIV: Code = BinOpInit::DIV; break;
1192  case tgtok::XAND: Code = BinOpInit::AND; break;
1193  case tgtok::XOR: Code = BinOpInit::OR; break;
1194  case tgtok::XXOR: Code = BinOpInit::XOR; break;
1195  case tgtok::XSRA: Code = BinOpInit::SRA; break;
1196  case tgtok::XSRL: Code = BinOpInit::SRL; break;
1197  case tgtok::XSHL: Code = BinOpInit::SHL; break;
1198  case tgtok::XEq: Code = BinOpInit::EQ; break;
1199  case tgtok::XNe: Code = BinOpInit::NE; break;
1200  case tgtok::XLe: Code = BinOpInit::LE; break;
1201  case tgtok::XLt: Code = BinOpInit::LT; break;
1202  case tgtok::XGe: Code = BinOpInit::GE; break;
1203  case tgtok::XGt: Code = BinOpInit::GT; break;
1208  case tgtok::XSetDagOp: Code = BinOpInit::SETDAGOP; break;
1209  }
1210 
1211  RecTy *Type = nullptr;
1212  RecTy *ArgType = nullptr;
1213  switch (OpTok) {
1214  default:
1215  llvm_unreachable("Unhandled code!");
1216  case tgtok::XConcat:
1217  case tgtok::XSetDagOp:
1218  Type = DagRecTy::get(Records);
1219  ArgType = DagRecTy::get(Records);
1220  break;
1221  case tgtok::XAND:
1222  case tgtok::XOR:
1223  case tgtok::XXOR:
1224  case tgtok::XSRA:
1225  case tgtok::XSRL:
1226  case tgtok::XSHL:
1227  case tgtok::XADD:
1228  case tgtok::XSUB:
1229  case tgtok::XMUL:
1230  case tgtok::XDIV:
1231  Type = IntRecTy::get(Records);
1232  ArgType = IntRecTy::get(Records);
1233  break;
1234  case tgtok::XEq:
1235  case tgtok::XNe:
1236  case tgtok::XLe:
1237  case tgtok::XLt:
1238  case tgtok::XGe:
1239  case tgtok::XGt:
1240  Type = BitRecTy::get(Records);
1241  // ArgType for the comparison operators is not yet known.
1242  break;
1243  case tgtok::XListConcat:
1244  // We don't know the list type until we parse the first argument
1245  ArgType = ItemType;
1246  break;
1247  case tgtok::XListSplat:
1248  // Can't do any typechecking until we parse the first argument.
1249  break;
1250  case tgtok::XStrConcat:
1251  Type = StringRecTy::get(Records);
1252  ArgType = StringRecTy::get(Records);
1253  break;
1254  case tgtok::XInterleave:
1255  Type = StringRecTy::get(Records);
1256  // The first argument type is not yet known.
1257  }
1258 
1259  if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1260  Error(OpLoc, Twine("expected value of type '") +
1261  ItemType->getAsString() + "', got '" +
1262  Type->getAsString() + "'");
1263  return nullptr;
1264  }
1265 
1266  if (!consume(tgtok::l_paren)) {
1267  TokError("expected '(' after binary operator");
1268  return nullptr;
1269  }
1270 
1271  SmallVector<Init*, 2> InitList;
1272 
1273  // Note that this loop consumes an arbitrary number of arguments.
1274  // The actual count is checked later.
1275  for (;;) {
1276  SMLoc InitLoc = Lex.getLoc();
1277  InitList.push_back(ParseValue(CurRec, ArgType));
1278  if (!InitList.back()) return nullptr;
1279 
1280  TypedInit *InitListBack = dyn_cast<TypedInit>(InitList.back());
1281  if (!InitListBack) {
1282  Error(OpLoc, Twine("expected value to be a typed value, got '" +
1283  InitList.back()->getAsString() + "'"));
1284  return nullptr;
1285  }
1286  RecTy *ListType = InitListBack->getType();
1287 
1288  if (!ArgType) {
1289  // Argument type must be determined from the argument itself.
1290  ArgType = ListType;
1291 
1292  switch (Code) {
1293  case BinOpInit::LISTCONCAT:
1294  if (!isa<ListRecTy>(ArgType)) {
1295  Error(InitLoc, Twine("expected a list, got value of type '") +
1296  ArgType->getAsString() + "'");
1297  return nullptr;
1298  }
1299  break;
1300  case BinOpInit::LISTSPLAT:
1301  if (ItemType && InitList.size() == 1) {
1302  if (!isa<ListRecTy>(ItemType)) {
1303  Error(OpLoc,
1304  Twine("expected output type to be a list, got type '") +
1305  ItemType->getAsString() + "'");
1306  return nullptr;
1307  }
1308  if (!ArgType->getListTy()->typeIsConvertibleTo(ItemType)) {
1309  Error(OpLoc, Twine("expected first arg type to be '") +
1310  ArgType->getAsString() +
1311  "', got value of type '" +
1312  cast<ListRecTy>(ItemType)
1313  ->getElementType()
1314  ->getAsString() +
1315  "'");
1316  return nullptr;
1317  }
1318  }
1319  if (InitList.size() == 2 && !isa<IntRecTy>(ArgType)) {
1320  Error(InitLoc, Twine("expected second parameter to be an int, got "
1321  "value of type '") +
1322  ArgType->getAsString() + "'");
1323  return nullptr;
1324  }
1325  ArgType = nullptr; // Broken invariant: types not identical.
1326  break;
1327  case BinOpInit::EQ:
1328  case BinOpInit::NE:
1329  if (!ArgType->typeIsConvertibleTo(IntRecTy::get(Records)) &&
1330  !ArgType->typeIsConvertibleTo(StringRecTy::get(Records)) &&
1331  !ArgType->typeIsConvertibleTo(RecordRecTy::get(Records, {}))) {
1332  Error(InitLoc, Twine("expected bit, bits, int, string, or record; "
1333  "got value of type '") + ArgType->getAsString() +
1334  "'");
1335  return nullptr;
1336  }
1337  break;
1338  case BinOpInit::LE:
1339  case BinOpInit::LT:
1340  case BinOpInit::GE:
1341  case BinOpInit::GT:
1342  if (!ArgType->typeIsConvertibleTo(IntRecTy::get(Records)) &&
1343  !ArgType->typeIsConvertibleTo(StringRecTy::get(Records))) {
1344  Error(InitLoc, Twine("expected bit, bits, int, or string; "
1345  "got value of type '") + ArgType->getAsString() +
1346  "'");
1347  return nullptr;
1348  }
1349  break;
1350  case BinOpInit::INTERLEAVE:
1351  switch (InitList.size()) {
1352  case 1: // First argument must be a list of strings or integers.
1353  if (ArgType != StringRecTy::get(Records)->getListTy() &&
1354  !ArgType->typeIsConvertibleTo(
1355  IntRecTy::get(Records)->getListTy())) {
1356  Error(InitLoc, Twine("expected list of string, int, bits, or bit; "
1357  "got value of type '") +
1358  ArgType->getAsString() + "'");
1359  return nullptr;
1360  }
1361  break;
1362  case 2: // Second argument must be a string.
1363  if (!isa<StringRecTy>(ArgType)) {
1364  Error(InitLoc, Twine("expected second argument to be a string, "
1365  "got value of type '") +
1366  ArgType->getAsString() + "'");
1367  return nullptr;
1368  }
1369  break;
1370  default: ;
1371  }
1372  ArgType = nullptr; // Broken invariant: types not identical.
1373  break;
1374  default: llvm_unreachable("other ops have fixed argument types");
1375  }
1376 
1377  } else {
1378  // Desired argument type is a known and in ArgType.
1379  RecTy *Resolved = resolveTypes(ArgType, ListType);
1380  if (!Resolved) {
1381  Error(InitLoc, Twine("expected value of type '") +
1382  ArgType->getAsString() + "', got '" +
1383  ListType->getAsString() + "'");
1384  return nullptr;
1385  }
1386  if (Code != BinOpInit::ADD && Code != BinOpInit::SUB &&
1387  Code != BinOpInit::AND && Code != BinOpInit::OR &&
1388  Code != BinOpInit::XOR && Code != BinOpInit::SRA &&
1389  Code != BinOpInit::SRL && Code != BinOpInit::SHL &&
1390  Code != BinOpInit::MUL && Code != BinOpInit::DIV)
1391  ArgType = Resolved;
1392  }
1393 
1394  // Deal with BinOps whose arguments have different types, by
1395  // rewriting ArgType in between them.
1396  switch (Code) {
1397  case BinOpInit::SETDAGOP:
1398  // After parsing the first dag argument, switch to expecting
1399  // a record, with no restriction on its superclasses.
1400  ArgType = RecordRecTy::get(Records, {});
1401  break;
1402  default:
1403  break;
1404  }
1405 
1406  if (!consume(tgtok::comma))
1407  break;
1408  }
1409 
1410  if (!consume(tgtok::r_paren)) {
1411  TokError("expected ')' in operator");
1412  return nullptr;
1413  }
1414 
1415  // listconcat returns a list with type of the argument.
1416  if (Code == BinOpInit::LISTCONCAT)
1417  Type = ArgType;
1418  // listsplat returns a list of type of the *first* argument.
1419  if (Code == BinOpInit::LISTSPLAT)
1420  Type = cast<TypedInit>(InitList.front())->getType()->getListTy();
1421 
1422  // We allow multiple operands to associative operators like !strconcat as
1423  // shorthand for nesting them.
1424  if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1425  Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1426  Code == BinOpInit::AND || Code == BinOpInit::OR ||
1427  Code == BinOpInit::XOR || Code == BinOpInit::MUL) {
1428  while (InitList.size() > 2) {
1429  Init *RHS = InitList.pop_back_val();
1430  RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
1431  InitList.back() = RHS;
1432  }
1433  }
1434 
1435  if (InitList.size() == 2)
1436  return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
1437  ->Fold(CurRec);
1438 
1439  Error(OpLoc, "expected two operands to operator");
1440  return nullptr;
1441  }
1442 
1443  case tgtok::XForEach:
1444  case tgtok::XFilter: {
1445  return ParseOperationForEachFilter(CurRec, ItemType);
1446  }
1447 
1448  case tgtok::XDag:
1449  case tgtok::XIf:
1450  case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1452  RecTy *Type = nullptr;
1453 
1454  tgtok::TokKind LexCode = Lex.getCode();
1455  Lex.Lex(); // eat the operation
1456  switch (LexCode) {
1457  default: llvm_unreachable("Unhandled code!");
1458  case tgtok::XDag:
1460  Type = DagRecTy::get(Records);
1461  ItemType = nullptr;
1462  break;
1463  case tgtok::XIf:
1464  Code = TernOpInit::IF;
1465  break;
1466  case tgtok::XSubst:
1468  break;
1469  }
1470  if (!consume(tgtok::l_paren)) {
1471  TokError("expected '(' after ternary operator");
1472  return nullptr;
1473  }
1474 
1475  Init *LHS = ParseValue(CurRec);
1476  if (!LHS) return nullptr;
1477 
1478  if (!consume(tgtok::comma)) {
1479  TokError("expected ',' in ternary operator");
1480  return nullptr;
1481  }
1482 
1483  SMLoc MHSLoc = Lex.getLoc();
1484  Init *MHS = ParseValue(CurRec, ItemType);
1485  if (!MHS)
1486  return nullptr;
1487 
1488  if (!consume(tgtok::comma)) {
1489  TokError("expected ',' in ternary operator");
1490  return nullptr;
1491  }
1492 
1493  SMLoc RHSLoc = Lex.getLoc();
1494  Init *RHS = ParseValue(CurRec, ItemType);
1495  if (!RHS)
1496  return nullptr;
1497 
1498  if (!consume(tgtok::r_paren)) {
1499  TokError("expected ')' in binary operator");
1500  return nullptr;
1501  }
1502 
1503  switch (LexCode) {
1504  default: llvm_unreachable("Unhandled code!");
1505  case tgtok::XDag: {
1506  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1507  if (!MHSt && !isa<UnsetInit>(MHS)) {
1508  Error(MHSLoc, "could not determine type of the child list in !dag");
1509  return nullptr;
1510  }
1511  if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1512  Error(MHSLoc, Twine("expected list of children, got type '") +
1513  MHSt->getType()->getAsString() + "'");
1514  return nullptr;
1515  }
1516 
1517  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1518  if (!RHSt && !isa<UnsetInit>(RHS)) {
1519  Error(RHSLoc, "could not determine type of the name list in !dag");
1520  return nullptr;
1521  }
1522  if (RHSt && StringRecTy::get(Records)->getListTy() != RHSt->getType()) {
1523  Error(RHSLoc, Twine("expected list<string>, got type '") +
1524  RHSt->getType()->getAsString() + "'");
1525  return nullptr;
1526  }
1527 
1528  if (!MHSt && !RHSt) {
1529  Error(MHSLoc,
1530  "cannot have both unset children and unset names in !dag");
1531  return nullptr;
1532  }
1533  break;
1534  }
1535  case tgtok::XIf: {
1536  RecTy *MHSTy = nullptr;
1537  RecTy *RHSTy = nullptr;
1538 
1539  if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1540  MHSTy = MHSt->getType();
1541  if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1542  MHSTy = BitsRecTy::get(Records, MHSbits->getNumBits());
1543  if (isa<BitInit>(MHS))
1544  MHSTy = BitRecTy::get(Records);
1545 
1546  if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1547  RHSTy = RHSt->getType();
1548  if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1549  RHSTy = BitsRecTy::get(Records, RHSbits->getNumBits());
1550  if (isa<BitInit>(RHS))
1551  RHSTy = BitRecTy::get(Records);
1552 
1553  // For UnsetInit, it's typed from the other hand.
1554  if (isa<UnsetInit>(MHS))
1555  MHSTy = RHSTy;
1556  if (isa<UnsetInit>(RHS))
1557  RHSTy = MHSTy;
1558 
1559  if (!MHSTy || !RHSTy) {
1560  TokError("could not get type for !if");
1561  return nullptr;
1562  }
1563 
1564  Type = resolveTypes(MHSTy, RHSTy);
1565  if (!Type) {
1566  TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1567  "' and '" + RHSTy->getAsString() + "' for !if");
1568  return nullptr;
1569  }
1570  break;
1571  }
1572  case tgtok::XSubst: {
1573  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1574  if (!RHSt) {
1575  TokError("could not get type for !subst");
1576  return nullptr;
1577  }
1578  Type = RHSt->getType();
1579  break;
1580  }
1581  }
1582  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1583  }
1584 
1585  case tgtok::XSubstr:
1586  return ParseOperationSubstr(CurRec, ItemType);
1587 
1588  case tgtok::XFind:
1589  return ParseOperationFind(CurRec, ItemType);
1590 
1591  case tgtok::XCond:
1592  return ParseOperationCond(CurRec, ItemType);
1593 
1594  case tgtok::XFoldl: {
1595  // Value ::= !foldl '(' Value ',' Value ',' Id ',' Id ',' Expr ')'
1596  Lex.Lex(); // eat the operation
1597  if (!consume(tgtok::l_paren)) {
1598  TokError("expected '(' after !foldl");
1599  return nullptr;
1600  }
1601 
1602  Init *StartUntyped = ParseValue(CurRec);
1603  if (!StartUntyped)
1604  return nullptr;
1605 
1606  TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1607  if (!Start) {
1608  TokError(Twine("could not get type of !foldl start: '") +
1609  StartUntyped->getAsString() + "'");
1610  return nullptr;
1611  }
1612 
1613  if (!consume(tgtok::comma)) {
1614  TokError("expected ',' in !foldl");
1615  return nullptr;
1616  }
1617 
1618  Init *ListUntyped = ParseValue(CurRec);
1619  if (!ListUntyped)
1620  return nullptr;
1621 
1622  TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1623  if (!List) {
1624  TokError(Twine("could not get type of !foldl list: '") +
1625  ListUntyped->getAsString() + "'");
1626  return nullptr;
1627  }
1628 
1629  ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1630  if (!ListType) {
1631  TokError(Twine("!foldl list must be a list, but is of type '") +
1632  List->getType()->getAsString());
1633  return nullptr;
1634  }
1635 
1636  if (Lex.getCode() != tgtok::comma) {
1637  TokError("expected ',' in !foldl");
1638  return nullptr;
1639  }
1640 
1641  if (Lex.Lex() != tgtok::Id) { // eat the ','
1642  TokError("third argument of !foldl must be an identifier");
1643  return nullptr;
1644  }
1645 
1646  Init *A = StringInit::get(Records, Lex.getCurStrVal());
1647  if (CurRec && CurRec->getValue(A)) {
1648  TokError((Twine("left !foldl variable '") + A->getAsString() +
1649  "' already defined")
1650  .str());
1651  return nullptr;
1652  }
1653 
1654  if (Lex.Lex() != tgtok::comma) { // eat the id
1655  TokError("expected ',' in !foldl");
1656  return nullptr;
1657  }
1658 
1659  if (Lex.Lex() != tgtok::Id) { // eat the ','
1660  TokError("fourth argument of !foldl must be an identifier");
1661  return nullptr;
1662  }
1663 
1664  Init *B = StringInit::get(Records, Lex.getCurStrVal());
1665  if (CurRec && CurRec->getValue(B)) {
1666  TokError((Twine("right !foldl variable '") + B->getAsString() +
1667  "' already defined")
1668  .str());
1669  return nullptr;
1670  }
1671 
1672  if (Lex.Lex() != tgtok::comma) { // eat the id
1673  TokError("expected ',' in !foldl");
1674  return nullptr;
1675  }
1676  Lex.Lex(); // eat the ','
1677 
1678  // We need to create a temporary record to provide a scope for the
1679  // two variables.
1680  std::unique_ptr<Record> ParseRecTmp;
1681  Record *ParseRec = CurRec;
1682  if (!ParseRec) {
1683  ParseRecTmp = std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
1684  ParseRec = ParseRecTmp.get();
1685  }
1686 
1687  ParseRec->addValue(RecordVal(A, Start->getType(), RecordVal::FK_Normal));
1688  ParseRec->addValue(RecordVal(B, ListType->getElementType(),
1690  Init *ExprUntyped = ParseValue(ParseRec);
1691  ParseRec->removeValue(A);
1692  ParseRec->removeValue(B);
1693  if (!ExprUntyped)
1694  return nullptr;
1695 
1696  TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1697  if (!Expr) {
1698  TokError("could not get type of !foldl expression");
1699  return nullptr;
1700  }
1701 
1702  if (Expr->getType() != Start->getType()) {
1703  TokError(Twine("!foldl expression must be of same type as start (") +
1704  Start->getType()->getAsString() + "), but is of type " +
1705  Expr->getType()->getAsString());
1706  return nullptr;
1707  }
1708 
1709  if (!consume(tgtok::r_paren)) {
1710  TokError("expected ')' in fold operator");
1711  return nullptr;
1712  }
1713 
1714  return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1715  ->Fold(CurRec);
1716  }
1717  }
1718 }
1719 
1720 /// ParseOperatorType - Parse a type for an operator. This returns
1721 /// null on error.
1722 ///
1723 /// OperatorType ::= '<' Type '>'
1724 ///
1725 RecTy *TGParser::ParseOperatorType() {
1726  RecTy *Type = nullptr;
1727 
1728  if (!consume(tgtok::less)) {
1729  TokError("expected type name for operator");
1730  return nullptr;
1731  }
1732 
1733  if (Lex.getCode() == tgtok::Code)
1734  TokError("the 'code' type is not allowed in bang operators; use 'string'");
1735 
1736  Type = ParseType();
1737 
1738  if (!Type) {
1739  TokError("expected type name for operator");
1740  return nullptr;
1741  }
1742 
1743  if (!consume(tgtok::greater)) {
1744  TokError("expected type name for operator");
1745  return nullptr;
1746  }
1747 
1748  return Type;
1749 }
1750 
1751 /// Parse the !substr operation. Return null on error.
1752 ///
1753 /// Substr ::= !substr(string, start-int [, length-int]) => string
1754 Init *TGParser::ParseOperationSubstr(Record *CurRec, RecTy *ItemType) {
1756  RecTy *Type = StringRecTy::get(Records);
1757 
1758  Lex.Lex(); // eat the operation
1759 
1760  if (!consume(tgtok::l_paren)) {
1761  TokError("expected '(' after !substr operator");
1762  return nullptr;
1763  }
1764 
1765  Init *LHS = ParseValue(CurRec);
1766  if (!LHS)
1767  return nullptr;
1768 
1769  if (!consume(tgtok::comma)) {
1770  TokError("expected ',' in !substr operator");
1771  return nullptr;
1772  }
1773 
1774  SMLoc MHSLoc = Lex.getLoc();
1775  Init *MHS = ParseValue(CurRec);
1776  if (!MHS)
1777  return nullptr;
1778 
1779  SMLoc RHSLoc = Lex.getLoc();
1780  Init *RHS;
1781  if (consume(tgtok::comma)) {
1782  RHSLoc = Lex.getLoc();
1783  RHS = ParseValue(CurRec);
1784  if (!RHS)
1785  return nullptr;
1786  } else {
1788  }
1789 
1790  if (!consume(tgtok::r_paren)) {
1791  TokError("expected ')' in !substr operator");
1792  return nullptr;
1793  }
1794 
1795  if (ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1796  Error(RHSLoc, Twine("expected value of type '") +
1797  ItemType->getAsString() + "', got '" +
1798  Type->getAsString() + "'");
1799  }
1800 
1801  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1802  if (!LHSt && !isa<UnsetInit>(LHS)) {
1803  TokError("could not determine type of the string in !substr");
1804  return nullptr;
1805  }
1806  if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
1807  TokError(Twine("expected string, got type '") +
1808  LHSt->getType()->getAsString() + "'");
1809  return nullptr;
1810  }
1811 
1812  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1813  if (!MHSt && !isa<UnsetInit>(MHS)) {
1814  TokError("could not determine type of the start position in !substr");
1815  return nullptr;
1816  }
1817  if (MHSt && !isa<IntRecTy>(MHSt->getType())) {
1818  Error(MHSLoc, Twine("expected int, got type '") +
1819  MHSt->getType()->getAsString() + "'");
1820  return nullptr;
1821  }
1822 
1823  if (RHS) {
1824  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1825  if (!RHSt && !isa<UnsetInit>(RHS)) {
1826  TokError("could not determine type of the length in !substr");
1827  return nullptr;
1828  }
1829  if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
1830  TokError(Twine("expected int, got type '") +
1831  RHSt->getType()->getAsString() + "'");
1832  return nullptr;
1833  }
1834  }
1835 
1836  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1837 }
1838 
1839 /// Parse the !find operation. Return null on error.
1840 ///
1841 /// Substr ::= !find(string, string [, start-int]) => int
1842 Init *TGParser::ParseOperationFind(Record *CurRec, RecTy *ItemType) {
1844  RecTy *Type = IntRecTy::get(Records);
1845 
1846  Lex.Lex(); // eat the operation
1847 
1848  if (!consume(tgtok::l_paren)) {
1849  TokError("expected '(' after !find operator");
1850  return nullptr;
1851  }
1852 
1853  Init *LHS = ParseValue(CurRec);
1854  if (!LHS)
1855  return nullptr;
1856 
1857  if (!consume(tgtok::comma)) {
1858  TokError("expected ',' in !find operator");
1859  return nullptr;
1860  }
1861 
1862  SMLoc MHSLoc = Lex.getLoc();
1863  Init *MHS = ParseValue(CurRec);
1864  if (!MHS)
1865  return nullptr;
1866 
1867  SMLoc RHSLoc = Lex.getLoc();
1868  Init *RHS;
1869  if (consume(tgtok::comma)) {
1870  RHSLoc = Lex.getLoc();
1871  RHS = ParseValue(CurRec);
1872  if (!RHS)
1873  return nullptr;
1874  } else {
1875  RHS = IntInit::get(Records, 0);
1876  }
1877 
1878  if (!consume(tgtok::r_paren)) {
1879  TokError("expected ')' in !find operator");
1880  return nullptr;
1881  }
1882 
1883  if (ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1884  Error(RHSLoc, Twine("expected value of type '") +
1885  ItemType->getAsString() + "', got '" +
1886  Type->getAsString() + "'");
1887  }
1888 
1889  TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
1890  if (!LHSt && !isa<UnsetInit>(LHS)) {
1891  TokError("could not determine type of the source string in !find");
1892  return nullptr;
1893  }
1894  if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
1895  TokError(Twine("expected string, got type '") +
1896  LHSt->getType()->getAsString() + "'");
1897  return nullptr;
1898  }
1899 
1900  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1901  if (!MHSt && !isa<UnsetInit>(MHS)) {
1902  TokError("could not determine type of the target string in !find");
1903  return nullptr;
1904  }
1905  if (MHSt && !isa<StringRecTy>(MHSt->getType())) {
1906  Error(MHSLoc, Twine("expected string, got type '") +
1907  MHSt->getType()->getAsString() + "'");
1908  return nullptr;
1909  }
1910 
1911  if (RHS) {
1912  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1913  if (!RHSt && !isa<UnsetInit>(RHS)) {
1914  TokError("could not determine type of the start position in !find");
1915  return nullptr;
1916  }
1917  if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
1918  TokError(Twine("expected int, got type '") +
1919  RHSt->getType()->getAsString() + "'");
1920  return nullptr;
1921  }
1922  }
1923 
1924  return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
1925 }
1926 
1927 /// Parse the !foreach and !filter operations. Return null on error.
1928 ///
1929 /// ForEach ::= !foreach(ID, list-or-dag, expr) => list<expr type>
1930 /// Filter ::= !foreach(ID, list, predicate) ==> list<list type>
1931 Init *TGParser::ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType) {
1932  SMLoc OpLoc = Lex.getLoc();
1934  Lex.Lex(); // eat the operation
1935  if (Lex.getCode() != tgtok::l_paren) {
1936  TokError("expected '(' after !foreach/!filter");
1937  return nullptr;
1938  }
1939 
1940  if (Lex.Lex() != tgtok::Id) { // eat the '('
1941  TokError("first argument of !foreach/!filter must be an identifier");
1942  return nullptr;
1943  }
1944 
1945  Init *LHS = StringInit::get(Records, Lex.getCurStrVal());
1946  Lex.Lex(); // eat the ID.
1947 
1948  if (CurRec && CurRec->getValue(LHS)) {
1949  TokError((Twine("iteration variable '") + LHS->getAsString() +
1950  "' is already defined")
1951  .str());
1952  return nullptr;
1953  }
1954 
1955  if (!consume(tgtok::comma)) {
1956  TokError("expected ',' in !foreach/!filter");
1957  return nullptr;
1958  }
1959 
1960  Init *MHS = ParseValue(CurRec);
1961  if (!MHS)
1962  return nullptr;
1963 
1964  if (!consume(tgtok::comma)) {
1965  TokError("expected ',' in !foreach/!filter");
1966  return nullptr;
1967  }
1968 
1969  TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1970  if (!MHSt) {
1971  TokError("could not get type of !foreach/!filter list or dag");
1972  return nullptr;
1973  }
1974 
1975  RecTy *InEltType = nullptr;
1976  RecTy *ExprEltType = nullptr;
1977  bool IsDAG = false;
1978 
1979  if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1980  InEltType = InListTy->getElementType();
1981  if (ItemType) {
1982  if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1983  ExprEltType = (Operation == tgtok::XForEach)
1984  ? OutListTy->getElementType()
1985  : IntRecTy::get(Records);
1986  } else {
1987  Error(OpLoc,
1988  "expected value of type '" +
1989  Twine(ItemType->getAsString()) +
1990  "', but got list type");
1991  return nullptr;
1992  }
1993  }
1994  } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1995  if (Operation == tgtok::XFilter) {
1996  TokError("!filter must have a list argument");
1997  return nullptr;
1998  }
1999  InEltType = InDagTy;
2000  if (ItemType && !isa<DagRecTy>(ItemType)) {
2001  Error(OpLoc,
2002  "expected value of type '" + Twine(ItemType->getAsString()) +
2003  "', but got dag type");
2004  return nullptr;
2005  }
2006  IsDAG = true;
2007  } else {
2008  if (Operation == tgtok::XForEach)
2009  TokError("!foreach must have a list or dag argument");
2010  else
2011  TokError("!filter must have a list argument");
2012  return nullptr;
2013  }
2014 
2015  // We need to create a temporary record to provide a scope for the
2016  // iteration variable.
2017  std::unique_ptr<Record> ParseRecTmp;
2018  Record *ParseRec = CurRec;
2019  if (!ParseRec) {
2020  ParseRecTmp =
2021  std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records);
2022  ParseRec = ParseRecTmp.get();
2023  }
2024 
2025  ParseRec->addValue(RecordVal(LHS, InEltType, RecordVal::FK_Normal));
2026  Init *RHS = ParseValue(ParseRec, ExprEltType);
2027  ParseRec->removeValue(LHS);
2028  if (!RHS)
2029  return nullptr;
2030 
2031  if (!consume(tgtok::r_paren)) {
2032  TokError("expected ')' in !foreach/!filter");
2033  return nullptr;
2034  }
2035 
2036  RecTy *OutType = InEltType;
2037  if (Operation == tgtok::XForEach && !IsDAG) {
2038  TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
2039  if (!RHSt) {
2040  TokError("could not get type of !foreach result expression");
2041  return nullptr;
2042  }
2043  OutType = RHSt->getType()->getListTy();
2044  } else if (Operation == tgtok::XFilter) {
2045  OutType = InEltType->getListTy();
2046  }
2047 
2050  LHS, MHS, RHS, OutType))
2051  ->Fold(CurRec);
2052 }
2053 
2054 Init *TGParser::ParseOperationCond(Record *CurRec, RecTy *ItemType) {
2055  Lex.Lex(); // eat the operation 'cond'
2056 
2057  if (!consume(tgtok::l_paren)) {
2058  TokError("expected '(' after !cond operator");
2059  return nullptr;
2060  }
2061 
2062  // Parse through '[Case: Val,]+'
2065  while (true) {
2066  if (consume(tgtok::r_paren))
2067  break;
2068 
2069  Init *V = ParseValue(CurRec);
2070  if (!V)
2071  return nullptr;
2072  Case.push_back(V);
2073 
2074  if (!consume(tgtok::colon)) {
2075  TokError("expected ':' following a condition in !cond operator");
2076  return nullptr;
2077  }
2078 
2079  V = ParseValue(CurRec, ItemType);
2080  if (!V)
2081  return nullptr;
2082  Val.push_back(V);
2083 
2084  if (consume(tgtok::r_paren))
2085  break;
2086 
2087  if (!consume(tgtok::comma)) {
2088  TokError("expected ',' or ')' following a value in !cond operator");
2089  return nullptr;
2090  }
2091  }
2092 
2093  if (Case.size() < 1) {
2094  TokError("there should be at least 1 'condition : value' in the !cond operator");
2095  return nullptr;
2096  }
2097 
2098  // resolve type
2099  RecTy *Type = nullptr;
2100  for (Init *V : Val) {
2101  RecTy *VTy = nullptr;
2102  if (TypedInit *Vt = dyn_cast<TypedInit>(V))
2103  VTy = Vt->getType();
2104  if (BitsInit *Vbits = dyn_cast<BitsInit>(V))
2105  VTy = BitsRecTy::get(Records, Vbits->getNumBits());
2106  if (isa<BitInit>(V))
2107  VTy = BitRecTy::get(Records);
2108 
2109  if (Type == nullptr) {
2110  if (!isa<UnsetInit>(V))
2111  Type = VTy;
2112  } else {
2113  if (!isa<UnsetInit>(V)) {
2114  RecTy *RType = resolveTypes(Type, VTy);
2115  if (!RType) {
2116  TokError(Twine("inconsistent types '") + Type->getAsString() +
2117  "' and '" + VTy->getAsString() + "' for !cond");
2118  return nullptr;
2119  }
2120  Type = RType;
2121  }
2122  }
2123  }
2124 
2125  if (!Type) {
2126  TokError("could not determine type for !cond from its arguments");
2127  return nullptr;
2128  }
2129  return CondOpInit::get(Case, Val, Type)->Fold(CurRec);
2130 }
2131 
2132 /// ParseSimpleValue - Parse a tblgen value. This returns null on error.
2133 ///
2134 /// SimpleValue ::= IDValue
2135 /// SimpleValue ::= INTVAL
2136 /// SimpleValue ::= STRVAL+
2137 /// SimpleValue ::= CODEFRAGMENT
2138 /// SimpleValue ::= '?'
2139 /// SimpleValue ::= '{' ValueList '}'
2140 /// SimpleValue ::= ID '<' ValueListNE '>'
2141 /// SimpleValue ::= '[' ValueList ']'
2142 /// SimpleValue ::= '(' IDValue DagArgList ')'
2143 /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
2144 /// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
2145 /// SimpleValue ::= DIVTOK '(' Value ',' Value ')'
2146 /// SimpleValue ::= SUBTOK '(' Value ',' Value ')'
2147 /// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
2148 /// SimpleValue ::= SRATOK '(' Value ',' Value ')'
2149 /// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
2150 /// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
2151 /// SimpleValue ::= LISTSPLATTOK '(' Value ',' Value ')'
2152 /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
2153 /// SimpleValue ::= COND '(' [Value ':' Value,]+ ')'
2154 ///
2155 Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
2156  IDParseMode Mode) {
2157  Init *R = nullptr;
2158  switch (Lex.getCode()) {
2159  default: TokError("Unknown or reserved token when parsing a value"); break;
2160 
2161  case tgtok::TrueVal:
2162  R = IntInit::get(Records, 1);
2163  Lex.Lex();
2164  break;
2165  case tgtok::FalseVal:
2166  R = IntInit::get(Records, 0);
2167  Lex.Lex();
2168  break;
2169  case tgtok::IntVal:
2170  R = IntInit::get(Records, Lex.getCurIntVal());
2171  Lex.Lex();
2172  break;
2173  case tgtok::BinaryIntVal: {
2174  auto BinaryVal = Lex.getCurBinaryIntVal();
2175  SmallVector<Init*, 16> Bits(BinaryVal.second);
2176  for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2177  Bits[i] = BitInit::get(Records, BinaryVal.first & (1LL << i));
2178  R = BitsInit::get(Records, Bits);
2179  Lex.Lex();
2180  break;
2181  }
2182  case tgtok::StrVal: {
2183  std::string Val = Lex.getCurStrVal();
2184  Lex.Lex();
2185 
2186  // Handle multiple consecutive concatenated strings.
2187  while (Lex.getCode() == tgtok::StrVal) {
2188  Val += Lex.getCurStrVal();
2189  Lex.Lex();
2190  }
2191 
2192  R = StringInit::get(Records, Val);
2193  break;
2194  }
2195  case tgtok::CodeFragment:
2197  Lex.Lex();
2198  break;
2199  case tgtok::question:
2200  R = UnsetInit::get(Records);
2201  Lex.Lex();
2202  break;
2203  case tgtok::Id: {
2204  SMRange NameLoc = Lex.getLocRange();
2205  StringInit *Name = StringInit::get(Records, Lex.getCurStrVal());
2206  if (Lex.Lex() != tgtok::less) // consume the Id.
2207  return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
2208 
2209  // Value ::= CLASSID '<' ValueListNE '>' (CLASSID has been consumed)
2210  // This is supposed to synthesize a new anonymous definition, deriving
2211  // from the class with the template arguments, but no body.
2212  Record *Class = Records.getClass(Name->getValue());
2213  if (!Class) {
2214  Error(NameLoc.Start,
2215  "Expected a class name, got '" + Name->getValue() + "'");
2216  return nullptr;
2217  }
2218 
2220  Lex.Lex(); // consume the <
2221  if (ParseTemplateArgValueList(Args, CurRec, Class))
2222  return nullptr; // Error parsing value list.
2223 
2224  if (CheckTemplateArgValues(Args, NameLoc.Start, Class))
2225  return nullptr; // Error checking template argument values.
2226 
2227  // Loop through the arguments that were not specified and make sure
2228  // they have a complete value.
2229  ArrayRef<Init *> TArgs = Class->getTemplateArgs();
2230  for (unsigned I = Args.size(), E = TArgs.size(); I < E; ++I) {
2231  RecordVal *Arg = Class->getValue(TArgs[I]);
2232  if (!Arg->getValue()->isComplete()) {
2233  Error(NameLoc.Start, "Value not specified for template argument '" +
2234  TArgs[I]->getAsUnquotedString() + "' (#" +
2235  Twine(I) + ") of parent class '" +
2236  Class->getNameInitAsString() + "'");
2237  }
2238  }
2239 
2240  if (TrackReferenceLocs)
2241  Class->appendReferenceLoc(NameLoc);
2242  return VarDefInit::get(Class, Args)->Fold();
2243  }
2244  case tgtok::l_brace: { // Value ::= '{' ValueList '}'
2245  SMLoc BraceLoc = Lex.getLoc();
2246  Lex.Lex(); // eat the '{'
2248 
2249  if (Lex.getCode() != tgtok::r_brace) {
2250  ParseValueList(Vals, CurRec);
2251  if (Vals.empty()) return nullptr;
2252  }
2253  if (!consume(tgtok::r_brace)) {
2254  TokError("expected '}' at end of bit list value");
2255  return nullptr;
2256  }
2257 
2258  SmallVector<Init *, 16> NewBits;
2259 
2260  // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
2261  // first. We'll first read everything in to a vector, then we can reverse
2262  // it to get the bits in the correct order for the BitsInit value.
2263  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
2264  // FIXME: The following two loops would not be duplicated
2265  // if the API was a little more orthogonal.
2266 
2267  // bits<n> values are allowed to initialize n bits.
2268  if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
2269  for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2270  NewBits.push_back(BI->getBit((e - i) - 1));
2271  continue;
2272  }
2273  // bits<n> can also come from variable initializers.
2274  if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
2275  if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
2276  for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2277  NewBits.push_back(VI->getBit((e - i) - 1));
2278  continue;
2279  }
2280  // Fallthrough to try convert this to a bit.
2281  }
2282  // All other values must be convertible to just a single bit.
2283  Init *Bit = Vals[i]->getCastTo(BitRecTy::get(Records));
2284  if (!Bit) {
2285  Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
2286  ") is not convertable to a bit");
2287  return nullptr;
2288  }
2289  NewBits.push_back(Bit);
2290  }
2291  std::reverse(NewBits.begin(), NewBits.end());
2292  return BitsInit::get(Records, NewBits);
2293  }
2294  case tgtok::l_square: { // Value ::= '[' ValueList ']'
2295  Lex.Lex(); // eat the '['
2297 
2298  RecTy *DeducedEltTy = nullptr;
2299  ListRecTy *GivenListTy = nullptr;
2300 
2301  if (ItemType) {
2302  ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
2303  if (!ListType) {
2304  TokError(Twine("Encountered a list when expecting a ") +
2305  ItemType->getAsString());
2306  return nullptr;
2307  }
2308  GivenListTy = ListType;
2309  }
2310 
2311  if (Lex.getCode() != tgtok::r_square) {
2312  ParseValueList(Vals, CurRec,
2313  GivenListTy ? GivenListTy->getElementType() : nullptr);
2314  if (Vals.empty()) return nullptr;
2315  }
2316  if (!consume(tgtok::r_square)) {
2317  TokError("expected ']' at end of list value");
2318  return nullptr;
2319  }
2320 
2321  RecTy *GivenEltTy = nullptr;
2322  if (consume(tgtok::less)) {
2323  // Optional list element type
2324  GivenEltTy = ParseType();
2325  if (!GivenEltTy) {
2326  // Couldn't parse element type
2327  return nullptr;
2328  }
2329 
2330  if (!consume(tgtok::greater)) {
2331  TokError("expected '>' at end of list element type");
2332  return nullptr;
2333  }
2334  }
2335 
2336  // Check elements
2337  RecTy *EltTy = nullptr;
2338  for (Init *V : Vals) {
2339  TypedInit *TArg = dyn_cast<TypedInit>(V);
2340  if (TArg) {
2341  if (EltTy) {
2342  EltTy = resolveTypes(EltTy, TArg->getType());
2343  if (!EltTy) {
2344  TokError("Incompatible types in list elements");
2345  return nullptr;
2346  }
2347  } else {
2348  EltTy = TArg->getType();
2349  }
2350  }
2351  }
2352 
2353  if (GivenEltTy) {
2354  if (EltTy) {
2355  // Verify consistency
2356  if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
2357  TokError("Incompatible types in list elements");
2358  return nullptr;
2359  }
2360  }
2361  EltTy = GivenEltTy;
2362  }
2363 
2364  if (!EltTy) {
2365  if (!ItemType) {
2366  TokError("No type for list");
2367  return nullptr;
2368  }
2369  DeducedEltTy = GivenListTy->getElementType();
2370  } else {
2371  // Make sure the deduced type is compatible with the given type
2372  if (GivenListTy) {
2373  if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
2374  TokError(Twine("Element type mismatch for list: element type '") +
2375  EltTy->getAsString() + "' not convertible to '" +
2376  GivenListTy->getElementType()->getAsString());
2377  return nullptr;
2378  }
2379  }
2380  DeducedEltTy = EltTy;
2381  }
2382 
2383  return ListInit::get(Vals, DeducedEltTy);
2384  }
2385  case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
2386  Lex.Lex(); // eat the '('
2387  if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast &&
2388  Lex.getCode() != tgtok::question && Lex.getCode() != tgtok::XGetDagOp) {
2389  TokError("expected identifier in dag init");
2390  return nullptr;
2391  }
2392 
2393  Init *Operator = ParseValue(CurRec);
2394  if (!Operator) return nullptr;
2395 
2396  // If the operator name is present, parse it.
2397  StringInit *OperatorName = nullptr;
2398  if (consume(tgtok::colon)) {
2399  if (Lex.getCode() != tgtok::VarName) { // eat the ':'
2400  TokError("expected variable name in dag operator");
2401  return nullptr;
2402  }
2403  OperatorName = StringInit::get(Records, Lex.getCurStrVal());
2404  Lex.Lex(); // eat the VarName.
2405  }
2406 
2408  if (Lex.getCode() != tgtok::r_paren) {
2409  ParseDagArgList(DagArgs, CurRec);
2410  if (DagArgs.empty()) return nullptr;
2411  }
2412 
2413  if (!consume(tgtok::r_paren)) {
2414  TokError("expected ')' in dag init");
2415  return nullptr;
2416  }
2417 
2418  return DagInit::get(Operator, OperatorName, DagArgs);
2419  }
2420 
2421  case tgtok::XHead:
2422  case tgtok::XTail:
2423  case tgtok::XSize:
2424  case tgtok::XEmpty:
2425  case tgtok::XCast:
2426  case tgtok::XGetDagOp: // Value ::= !unop '(' Value ')'
2427  case tgtok::XExists:
2428  case tgtok::XIsA:
2429  case tgtok::XConcat:
2430  case tgtok::XDag:
2431  case tgtok::XADD:
2432  case tgtok::XSUB:
2433  case tgtok::XMUL:
2434  case tgtok::XDIV:
2435  case tgtok::XNOT:
2436  case tgtok::XAND:
2437  case tgtok::XOR:
2438  case tgtok::XXOR:
2439  case tgtok::XSRA:
2440  case tgtok::XSRL:
2441  case tgtok::XSHL:
2442  case tgtok::XEq:
2443  case tgtok::XNe:
2444  case tgtok::XLe:
2445  case tgtok::XLt:
2446  case tgtok::XGe:
2447  case tgtok::XGt:
2448  case tgtok::XListConcat:
2449  case tgtok::XListSplat:
2450  case tgtok::XStrConcat:
2451  case tgtok::XInterleave:
2452  case tgtok::XSetDagOp: // Value ::= !binop '(' Value ',' Value ')'
2453  case tgtok::XIf:
2454  case tgtok::XCond:
2455  case tgtok::XFoldl:
2456  case tgtok::XForEach:
2457  case tgtok::XFilter:
2458  case tgtok::XSubst:
2459  case tgtok::XSubstr:
2460  case tgtok::XFind: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
2461  return ParseOperation(CurRec, ItemType);
2462  }
2463  }
2464 
2465  return R;
2466 }
2467 
2468 /// ParseValue - Parse a TableGen value. This returns null on error.
2469 ///
2470 /// Value ::= SimpleValue ValueSuffix*
2471 /// ValueSuffix ::= '{' BitList '}'
2472 /// ValueSuffix ::= '[' BitList ']'
2473 /// ValueSuffix ::= '.' ID
2474 ///
2475 Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
2476  Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
2477  if (!Result) return nullptr;
2478 
2479  // Parse the suffixes now if present.
2480  while (true) {
2481  switch (Lex.getCode()) {
2482  default: return Result;
2483  case tgtok::l_brace: {
2484  if (Mode == ParseNameMode)
2485  // This is the beginning of the object body.
2486  return Result;
2487 
2488  SMLoc CurlyLoc = Lex.getLoc();
2489  Lex.Lex(); // eat the '{'
2491  ParseRangeList(Ranges);
2492  if (Ranges.empty()) return nullptr;
2493 
2494  // Reverse the bitlist.
2495  std::reverse(Ranges.begin(), Ranges.end());
2496  Result = Result->convertInitializerBitRange(Ranges);
2497  if (!Result) {
2498  Error(CurlyLoc, "Invalid bit range for value");
2499  return nullptr;
2500  }
2501 
2502  // Eat the '}'.
2503  if (!consume(tgtok::r_brace)) {
2504  TokError("expected '}' at end of bit range list");
2505  return nullptr;
2506  }
2507  break;
2508  }
2509  case tgtok::l_square: {
2510  SMLoc SquareLoc = Lex.getLoc();
2511  Lex.Lex(); // eat the '['
2513  ParseRangeList(Ranges);
2514  if (Ranges.empty()) return nullptr;
2515 
2516  Result = Result->convertInitListSlice(Ranges);
2517  if (!Result) {
2518  Error(SquareLoc, "Invalid range for list slice");
2519  return nullptr;
2520  }
2521 
2522  // Eat the ']'.
2523  if (!consume(tgtok::r_square)) {
2524  TokError("expected ']' at end of list slice");
2525  return nullptr;
2526  }
2527  break;
2528  }
2529  case tgtok::dot: {
2530  if (Lex.Lex() != tgtok::Id) { // eat the .
2531  TokError("expected field identifier after '.'");
2532  return nullptr;
2533  }
2534  SMRange FieldNameLoc = Lex.getLocRange();
2535  StringInit *FieldName = StringInit::get(Records, Lex.getCurStrVal());
2536  if (!Result->getFieldType(FieldName)) {
2537  TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
2538  Result->getAsString() + "'");
2539  return nullptr;
2540  }
2541 
2542  // Add a reference to this field if we know the record class.
2543  if (TrackReferenceLocs) {
2544  if (auto *DI = dyn_cast<DefInit>(Result)) {
2545  DI->getDef()->getValue(FieldName)->addReferenceLoc(FieldNameLoc);
2546  } else if (auto *TI = dyn_cast<TypedInit>(Result)) {
2547  if (auto *RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
2548  for (Record *R : RecTy->getClasses())
2549  if (auto *RV = R->getValue(FieldName))
2550  RV->addReferenceLoc(FieldNameLoc);
2551  }
2552  }
2553  }
2554 
2555  Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
2556  Lex.Lex(); // eat field name
2557  break;
2558  }
2559 
2560  case tgtok::paste:
2561  SMLoc PasteLoc = Lex.getLoc();
2562  TypedInit *LHS = dyn_cast<TypedInit>(Result);
2563  if (!LHS) {
2564  Error(PasteLoc, "LHS of paste is not typed!");
2565  return nullptr;
2566  }
2567 
2568  // Check if it's a 'listA # listB'
2569  if (isa<ListRecTy>(LHS->getType())) {
2570  Lex.Lex(); // Eat the '#'.
2571 
2572  assert(Mode == ParseValueMode && "encountered paste of lists in name");
2573 
2574  switch (Lex.getCode()) {
2575  case tgtok::colon:
2576  case tgtok::semi:
2577  case tgtok::l_brace:
2578  Result = LHS; // trailing paste, ignore.
2579  break;
2580  default:
2581  Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
2582  if (!RHSResult)
2583  return nullptr;
2584  Result = BinOpInit::getListConcat(LHS, RHSResult);
2585  break;
2586  }
2587  break;
2588  }
2589 
2590  // Create a !strconcat() operation, first casting each operand to
2591  // a string if necessary.
2592  if (LHS->getType() != StringRecTy::get(Records)) {
2593  auto CastLHS = dyn_cast<TypedInit>(
2595  ->Fold(CurRec));
2596  if (!CastLHS) {
2597  Error(PasteLoc,
2598  Twine("can't cast '") + LHS->getAsString() + "' to string");
2599  return nullptr;
2600  }
2601  LHS = CastLHS;
2602  }
2603 
2604  TypedInit *RHS = nullptr;
2605 
2606  Lex.Lex(); // Eat the '#'.
2607  switch (Lex.getCode()) {
2608  case tgtok::colon:
2609  case tgtok::semi:
2610  case tgtok::l_brace:
2611  // These are all of the tokens that can begin an object body.
2612  // Some of these can also begin values but we disallow those cases
2613  // because they are unlikely to be useful.
2614 
2615  // Trailing paste, concat with an empty string.
2616  RHS = StringInit::get(Records, "");
2617  break;
2618 
2619  default:
2620  Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
2621  if (!RHSResult)
2622  return nullptr;
2623  RHS = dyn_cast<TypedInit>(RHSResult);
2624  if (!RHS) {
2625  Error(PasteLoc, "RHS of paste is not typed!");
2626  return nullptr;
2627  }
2628 
2629  if (RHS->getType() != StringRecTy::get(Records)) {
2630  auto CastRHS = dyn_cast<TypedInit>(
2632  ->Fold(CurRec));
2633  if (!CastRHS) {
2634  Error(PasteLoc,
2635  Twine("can't cast '") + RHS->getAsString() + "' to string");
2636  return nullptr;
2637  }
2638  RHS = CastRHS;
2639  }
2640 
2641  break;
2642  }
2643 
2645  break;
2646  }
2647  }
2648 }
2649 
2650 /// ParseDagArgList - Parse the argument list for a dag literal expression.
2651 ///
2652 /// DagArg ::= Value (':' VARNAME)?
2653 /// DagArg ::= VARNAME
2654 /// DagArgList ::= DagArg
2655 /// DagArgList ::= DagArgList ',' DagArg
2656 void TGParser::ParseDagArgList(
2657  SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2658  Record *CurRec) {
2659 
2660  while (true) {
2661  // DagArg ::= VARNAME
2662  if (Lex.getCode() == tgtok::VarName) {
2663  // A missing value is treated like '?'.
2664  StringInit *VarName = StringInit::get(Records, Lex.getCurStrVal());
2665  Result.emplace_back(UnsetInit::get(Records), VarName);
2666  Lex.Lex();
2667  } else {
2668  // DagArg ::= Value (':' VARNAME)?
2669  Init *Val = ParseValue(CurRec);
2670  if (!Val) {
2671  Result.clear();
2672  return;
2673  }
2674 
2675  // If the variable name is present, add it.
2676  StringInit *VarName = nullptr;
2677  if (Lex.getCode() == tgtok::colon) {
2678  if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2679  TokError("expected variable name in dag literal");
2680  Result.clear();
2681  return;
2682  }
2683  VarName = StringInit::get(Records, Lex.getCurStrVal());
2684  Lex.Lex(); // eat the VarName.
2685  }
2686 
2687  Result.push_back(std::make_pair(Val, VarName));
2688  }
2689  if (!consume(tgtok::comma))
2690  break;
2691  }
2692 }
2693 
2694 /// ParseValueList - Parse a comma separated list of values, returning them
2695 /// in a vector. Note that this always expects to be able to parse at least one
2696 /// value. It returns an empty list if this is not possible.
2697 ///
2698 /// ValueList ::= Value (',' Value)
2699 ///
2700 void TGParser::ParseValueList(SmallVectorImpl<Init *> &Result, Record *CurRec,
2701  RecTy *ItemType) {
2702 
2703  Result.push_back(ParseValue(CurRec, ItemType));
2704  if (!Result.back()) {
2705  Result.clear();
2706  return;
2707  }
2708 
2709  while (consume(tgtok::comma)) {
2710  // ignore trailing comma for lists
2711  if (Lex.getCode() == tgtok::r_square)
2712  return;
2713  Result.push_back(ParseValue(CurRec, ItemType));
2714  if (!Result.back()) {
2715  Result.clear();
2716  return;
2717  }
2718  }
2719 }
2720 
2721 // ParseTemplateArgValueList - Parse a template argument list with the syntax
2722 // shown, filling in the Result vector. The open angle has been consumed.
2723 // An empty argument list is allowed. Return false if okay, true if an
2724 // error was detected.
2725 //
2726 // TemplateArgList ::= '<' [Value {',' Value}*] '>'
2727 bool TGParser::ParseTemplateArgValueList(SmallVectorImpl<Init *> &Result,
2728  Record *CurRec, Record *ArgsRec) {
2729 
2730  assert(Result.empty() && "Result vector is not empty");
2731  ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
2732  unsigned ArgIndex = 0;
2733  RecTy *ItemType;
2734 
2735  if (consume(tgtok::greater)) // empty value list
2736  return false;
2737 
2738  while (true) {
2739  if (ArgIndex >= TArgs.size()) {
2740  TokError("Too many template arguments: " + utostr(ArgIndex + 1));
2741  return true;
2742  }
2743  const RecordVal *Arg = ArgsRec->getValue(TArgs[ArgIndex]);
2744  assert(Arg && "Template argument record not found");
2745 
2746  ItemType = Arg->getType();
2747  Init *Value = ParseValue(CurRec, ItemType);
2748  if (!Value)
2749  return true;
2750  Result.push_back(Value);
2751 
2752  if (consume(tgtok::greater)) // end of argument list?
2753  return false;
2754  if (!consume(tgtok::comma))
2755  return TokError("Expected comma before next argument");
2756  ++ArgIndex;
2757  }
2758 }
2759 
2760 /// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2761 /// empty string on error. This can happen in a number of different contexts,
2762 /// including within a def or in the template args for a class (in which case
2763 /// CurRec will be non-null) and within the template args for a multiclass (in
2764 /// which case CurRec will be null, but CurMultiClass will be set). This can
2765 /// also happen within a def that is within a multiclass, which will set both
2766 /// CurRec and CurMultiClass.
2767 ///
2768 /// Declaration ::= FIELD? Type ID ('=' Value)?
2769 ///
2770 Init *TGParser::ParseDeclaration(Record *CurRec,
2771  bool ParsingTemplateArgs) {
2772  // Read the field prefix if present.
2773  bool HasField = consume(tgtok::Field);
2774 
2775  RecTy *Type = ParseType();
2776  if (!Type) return nullptr;
2777 
2778  if (Lex.getCode() != tgtok::Id) {
2779  TokError("Expected identifier in declaration");
2780  return nullptr;
2781  }
2782 
2783  std::string Str = Lex.getCurStrVal();
2784  if (Str == "NAME") {
2785  TokError("'" + Str + "' is a reserved variable name");
2786  return nullptr;
2787  }
2788 
2789  SMLoc IdLoc = Lex.getLoc();
2790  Init *DeclName = StringInit::get(Records, Str);
2791  Lex.Lex();
2792 
2793  bool BadField;
2794  if (!ParsingTemplateArgs) { // def, possibly in a multiclass
2795  BadField = AddValue(CurRec, IdLoc,
2796  RecordVal(DeclName, IdLoc, Type,
2797  HasField ? RecordVal::FK_NonconcreteOK
2799 
2800  } else if (CurRec) { // class template argument
2801  DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
2802  BadField = AddValue(CurRec, IdLoc, RecordVal(DeclName, IdLoc, Type,
2804 
2805  } else { // multiclass template argument
2806  assert(CurMultiClass && "invalid context for template argument");
2807  DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, "::");
2808  BadField = AddValue(CurRec, IdLoc, RecordVal(DeclName, IdLoc, Type,
2810  }
2811  if (BadField)
2812  return nullptr;
2813 
2814  // If a value is present, parse it and set new field's value.
2815  if (consume(tgtok::equal)) {
2816  SMLoc ValLoc = Lex.getLoc();
2817  Init *Val = ParseValue(CurRec, Type);
2818  if (!Val ||
2819  SetValue(CurRec, ValLoc, DeclName, None, Val,
2820  /*AllowSelfAssignment=*/false, /*OverrideDefLoc=*/false)) {
2821  // Return the name, even if an error is thrown. This is so that we can
2822  // continue to make some progress, even without the value having been
2823  // initialized.
2824  return DeclName;
2825  }
2826  }
2827 
2828  return DeclName;
2829 }
2830 
2831 /// ParseForeachDeclaration - Read a foreach declaration, returning
2832 /// the name of the declared object or a NULL Init on error. Return
2833 /// the name of the parsed initializer list through ForeachListName.
2834 ///
2835 /// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2836 /// ForeachDeclaration ::= ID '=' RangePiece
2837 /// ForeachDeclaration ::= ID '=' Value
2838 ///
2839 VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) {
2840  if (Lex.getCode() != tgtok::Id) {
2841  TokError("Expected identifier in foreach declaration");
2842  return nullptr;
2843  }
2844 
2845  Init *DeclName = StringInit::get(Records, Lex.getCurStrVal());
2846  Lex.Lex();
2847 
2848  // If a value is present, parse it.
2849  if (!consume(tgtok::equal)) {
2850  TokError("Expected '=' in foreach declaration");
2851  return nullptr;
2852  }
2853 
2854  RecTy *IterType = nullptr;
2856 
2857  switch (Lex.getCode()) {
2858  case tgtok::l_brace: { // '{' RangeList '}'
2859  Lex.Lex(); // eat the '{'
2860  ParseRangeList(Ranges);
2861  if (!consume(tgtok::r_brace)) {
2862  TokError("expected '}' at end of bit range list");
2863  return nullptr;
2864  }
2865  break;
2866  }
2867 
2868  default: {
2869  SMLoc ValueLoc = Lex.getLoc();
2870  Init *I = ParseValue(nullptr);
2871  if (!I)
2872  return nullptr;
2873 
2874  TypedInit *TI = dyn_cast<TypedInit>(I);
2875  if (TI && isa<ListRecTy>(TI->getType())) {
2876  ForeachListValue = I;
2877  IterType = cast<ListRecTy>(TI->getType())->getElementType();
2878  break;
2879  }
2880 
2881  if (TI) {
2882  if (ParseRangePiece(Ranges, TI))
2883  return nullptr;
2884  break;
2885  }
2886 
2887  Error(ValueLoc, "expected a list, got '" + I->getAsString() + "'");
2888  if (CurMultiClass) {
2889  PrintNote({}, "references to multiclass template arguments cannot be "
2890  "resolved at this time");
2891  }
2892  return nullptr;
2893  }
2894  }
2895 
2896 
2897  if (!Ranges.empty()) {
2898  assert(!IterType && "Type already initialized?");
2899  IterType = IntRecTy::get(Records);
2900  std::vector<Init *> Values;
2901  for (unsigned R : Ranges)
2902  Values.push_back(IntInit::get(Records, R));
2903  ForeachListValue = ListInit::get(Values, IterType);
2904  }
2905 
2906  if (!IterType)
2907  return nullptr;
2908 
2909  return VarInit::get(DeclName, IterType);
2910 }
2911 
2912 /// ParseTemplateArgList - Read a template argument list, which is a non-empty
2913 /// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2914 /// template args for a class. If null, these are the template args for a
2915 /// multiclass.
2916 ///
2917 /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
2918 ///
2919 bool TGParser::ParseTemplateArgList(Record *CurRec) {
2920  assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2921  Lex.Lex(); // eat the '<'
2922 
2923  Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
2924 
2925  // Read the first declaration.
2926  Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
2927  if (!TemplArg)
2928  return true;
2929 
2930  TheRecToAddTo->addTemplateArg(TemplArg);
2931 
2932  while (consume(tgtok::comma)) {
2933  // Read the following declarations.
2934  SMLoc Loc = Lex.getLoc();
2935  TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
2936  if (!TemplArg)
2937  return true;
2938 
2939  if (TheRecToAddTo->isTemplateArg(TemplArg))
2940  return Error(Loc, "template argument with the same name has already been "
2941  "defined");
2942 
2943  TheRecToAddTo->addTemplateArg(TemplArg);
2944  }
2945 
2946  if (!consume(tgtok::greater))
2947  return TokError("expected '>' at end of template argument list");
2948  return false;
2949 }
2950 
2951 /// ParseBodyItem - Parse a single item within the body of a def or class.
2952 ///
2953 /// BodyItem ::= Declaration ';'
2954 /// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2955 /// BodyItem ::= Defvar
2956 /// BodyItem ::= Assert
2957 ///
2958 bool TGParser::ParseBodyItem(Record *CurRec) {
2959  if (Lex.getCode() == tgtok::Assert)
2960  return ParseAssert(nullptr, CurRec);
2961 
2962  if (Lex.getCode() == tgtok::Defvar)
2963  return ParseDefvar();
2964 
2965  if (Lex.getCode() != tgtok::Let) {
2966  if (!ParseDeclaration(CurRec, false))
2967  return true;
2968 
2969  if (!consume(tgtok::semi))
2970  return TokError("expected ';' after declaration");
2971  return false;
2972  }
2973 
2974  // LET ID OptionalRangeList '=' Value ';'
2975  if (Lex.Lex() != tgtok::Id)
2976  return TokError("expected field identifier after let");
2977 
2978  SMLoc IdLoc = Lex.getLoc();
2979  StringInit *FieldName = StringInit::get(Records, Lex.getCurStrVal());
2980  Lex.Lex(); // eat the field name.
2981 
2982  SmallVector<unsigned, 16> BitList;
2983  if (ParseOptionalBitList(BitList))
2984  return true;
2985  std::reverse(BitList.begin(), BitList.end());
2986 
2987  if (!consume(tgtok::equal))
2988  return TokError("expected '=' in let expression");
2989 
2990  RecordVal *Field = CurRec->getValue(FieldName);
2991  if (!Field)
2992  return TokError("Value '" + FieldName->getValue() + "' unknown!");
2993 
2994  RecTy *Type = Field->getType();
2995  if (!BitList.empty() && isa<BitsRecTy>(Type)) {
2996  // When assigning to a subset of a 'bits' object, expect the RHS to have
2997  // the type of that subset instead of the type of the whole object.
2998  Type = BitsRecTy::get(Records, BitList.size());
2999  }
3000 
3001  Init *Val = ParseValue(CurRec, Type);
3002  if (!Val) return true;
3003 
3004  if (!consume(tgtok::semi))
3005  return TokError("expected ';' after let expression");
3006 
3007  return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
3008 }
3009 
3010 /// ParseBody - Read the body of a class or def. Return true on error, false on
3011 /// success.
3012 ///
3013 /// Body ::= ';'
3014 /// Body ::= '{' BodyList '}'
3015 /// BodyList BodyItem*
3016 ///
3017 bool TGParser::ParseBody(Record *CurRec) {
3018  // If this is a null definition, just eat the semi and return.
3019  if (consume(tgtok::semi))
3020  return false;
3021 
3022  if (!consume(tgtok::l_brace))
3023  return TokError("Expected '{' to start body or ';' for declaration only");
3024 
3025  // An object body introduces a new scope for local variables.
3026  TGLocalVarScope *BodyScope = PushLocalScope();
3027 
3028  while (Lex.getCode() != tgtok::r_brace)
3029  if (ParseBodyItem(CurRec))
3030  return true;
3031 
3032  PopLocalScope(BodyScope);
3033 
3034  // Eat the '}'.
3035  Lex.Lex();
3036 
3037  // If we have a semicolon, print a gentle error.
3038  SMLoc SemiLoc = Lex.getLoc();
3039  if (consume(tgtok::semi)) {
3040  PrintError(SemiLoc, "A class or def body should not end with a semicolon");
3041  PrintNote("Semicolon ignored; remove to eliminate this error");
3042  }
3043 
3044  return false;
3045 }
3046 
3047 /// Apply the current let bindings to \a CurRec.
3048 /// \returns true on error, false otherwise.
3049 bool TGParser::ApplyLetStack(Record *CurRec) {
3050  for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3051  for (LetRecord &LR : LetInfo)
3052  if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3053  return true;
3054  return false;
3055 }
3056 
3057 /// Apply the current let bindings to the RecordsEntry.
3058 bool TGParser::ApplyLetStack(RecordsEntry &Entry) {
3059  if (Entry.Rec)
3060  return ApplyLetStack(Entry.Rec.get());
3061 
3062  // Let bindings are not applied to assertions.
3063  if (Entry.Assertion)
3064  return false;
3065 
3066  for (auto &E : Entry.Loop->Entries) {
3067  if (ApplyLetStack(E))
3068  return true;
3069  }
3070 
3071  return false;
3072 }
3073 
3074 /// ParseObjectBody - Parse the body of a def or class. This consists of an
3075 /// optional ClassList followed by a Body. CurRec is the current def or class
3076 /// that is being parsed.
3077 ///
3078 /// ObjectBody ::= BaseClassList Body
3079 /// BaseClassList ::= /*empty*/
3080 /// BaseClassList ::= ':' BaseClassListNE
3081 /// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
3082 ///
3083 bool TGParser::ParseObjectBody(Record *CurRec) {
3084  // If there is a baseclass list, read it.
3085  if (consume(tgtok::colon)) {
3086 
3087  // Read all of the subclasses.
3088  SubClassReference SubClass = ParseSubClassReference(CurRec, false);
3089  while (true) {
3090  // Check for error.
3091  if (!SubClass.Rec) return true;
3092 
3093  // Add it.
3094  if (AddSubClass(CurRec, SubClass))
3095  return true;
3096 
3097  if (!consume(tgtok::comma))
3098  break;
3099  SubClass = ParseSubClassReference(CurRec, false);
3100  }
3101  }
3102 
3103  if (ApplyLetStack(CurRec))
3104  return true;
3105 
3106  return ParseBody(CurRec);
3107 }
3108 
3109 /// ParseDef - Parse and return a top level or multiclass record definition.
3110 /// Return false if okay, true if error.
3111 ///
3112 /// DefInst ::= DEF ObjectName ObjectBody
3113 ///
3114 bool TGParser::ParseDef(MultiClass *CurMultiClass) {
3115  SMLoc DefLoc = Lex.getLoc();
3116  assert(Lex.getCode() == tgtok::Def && "Unknown tok");
3117  Lex.Lex(); // Eat the 'def' token.
3118 
3119  // If the name of the def is an Id token, use that for the location.
3120  // Otherwise, the name is more complex and we use the location of the 'def'
3121  // token.
3122  SMLoc NameLoc = Lex.getCode() == tgtok::Id ? Lex.getLoc() : DefLoc;
3123 
3124  // Parse ObjectName and make a record for it.
3125  std::unique_ptr<Record> CurRec;
3126  Init *Name = ParseObjectName(CurMultiClass);
3127  if (!Name)
3128  return true;
3129 
3130  if (isa<UnsetInit>(Name)) {
3131  CurRec =
3132  std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
3133  /*Anonymous=*/true);
3134  } else {
3135  CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3136  }
3137 
3138  if (ParseObjectBody(CurRec.get()))
3139  return true;
3140 
3141  return addEntry(std::move(CurRec));
3142 }
3143 
3144 /// ParseDefset - Parse a defset statement.
3145 ///
3146 /// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
3147 ///
3148 bool TGParser::ParseDefset() {
3149  assert(Lex.getCode() == tgtok::Defset);
3150  Lex.Lex(); // Eat the 'defset' token
3151 
3153  Defset.Loc = Lex.getLoc();
3154  RecTy *Type = ParseType();
3155  if (!Type)
3156  return true;
3157  if (!isa<ListRecTy>(Type))
3158  return Error(Defset.Loc, "expected list type");
3159  Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
3160 
3161  if (Lex.getCode() != tgtok::Id)
3162  return TokError("expected identifier");
3163  StringInit *DeclName = StringInit::get(Records, Lex.getCurStrVal());
3164  if (Records.getGlobal(DeclName->getValue()))
3165  return TokError("def or global variable of this name already exists");
3166 
3167  if (Lex.Lex() != tgtok::equal) // Eat the identifier
3168  return TokError("expected '='");
3169  if (Lex.Lex() != tgtok::l_brace) // Eat the '='
3170  return TokError("expected '{'");
3171  SMLoc BraceLoc = Lex.getLoc();
3172  Lex.Lex(); // Eat the '{'
3173 
3174  Defsets.push_back(&Defset);
3175  bool Err = ParseObjectList(nullptr);
3176  Defsets.pop_back();
3177  if (Err)
3178  return true;
3179 
3180  if (!consume(tgtok::r_brace)) {
3181  TokError("expected '}' at end of defset");
3182  return Error(BraceLoc, "to match this '{'");
3183  }
3184 
3185  Records.addExtraGlobal(DeclName->getValue(),
3186  ListInit::get(Defset.Elements, Defset.EltTy));
3187  return false;
3188 }
3189 
3190 /// ParseDefvar - Parse a defvar statement.
3191 ///
3192 /// Defvar ::= DEFVAR Id '=' Value ';'
3193 ///
3194 bool TGParser::ParseDefvar() {
3195  assert(Lex.getCode() == tgtok::Defvar);
3196  Lex.Lex(); // Eat the 'defvar' token
3197 
3198  if (Lex.getCode() != tgtok::Id)
3199  return TokError("expected identifier");
3200  StringInit *DeclName = StringInit::get(Records, Lex.getCurStrVal());
3201  if (CurLocalScope) {
3202  if (CurLocalScope->varAlreadyDefined(DeclName->getValue()))
3203  return TokError("local variable of this name already exists");
3204  } else {
3205  if (Records.getGlobal(DeclName->getValue()))
3206  return TokError("def or global variable of this name already exists");
3207  }
3208 
3209  Lex.Lex();
3210  if (!consume(tgtok::equal))
3211  return TokError("expected '='");
3212 
3213  Init *Value = ParseValue(nullptr);
3214  if (!Value)
3215  return true;
3216 
3217  if (!consume(tgtok::semi))
3218  return TokError("expected ';'");
3219 
3220  if (CurLocalScope)
3221  CurLocalScope->addVar(DeclName->getValue(), Value);
3222  else
3223  Records.addExtraGlobal(DeclName->getValue(), Value);
3224 
3225  return false;
3226 }
3227 
3228 /// ParseForeach - Parse a for statement. Return the record corresponding
3229 /// to it. This returns true on error.
3230 ///
3231 /// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
3232 /// Foreach ::= FOREACH Declaration IN Object
3233 ///
3234 bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
3235  SMLoc Loc = Lex.getLoc();
3236  assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
3237  Lex.Lex(); // Eat the 'for' token.
3238 
3239  // Make a temporary object to record items associated with the for
3240  // loop.
3241  Init *ListValue = nullptr;
3242  VarInit *IterName = ParseForeachDeclaration(ListValue);
3243  if (!IterName)
3244  return TokError("expected declaration in for");
3245 
3246  if (!consume(tgtok::In))
3247  return TokError("Unknown tok");
3248 
3249  // Create a loop object and remember it.
3250  Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue));
3251 
3252  // A foreach loop introduces a new scope for local variables.
3253  TGLocalVarScope *ForeachScope = PushLocalScope();
3254 
3255  if (Lex.getCode() != tgtok::l_brace) {
3256  // FOREACH Declaration IN Object
3257  if (ParseObject(CurMultiClass))
3258  return true;
3259  } else {
3260  SMLoc BraceLoc = Lex.getLoc();
3261  // Otherwise, this is a group foreach.
3262  Lex.Lex(); // eat the '{'.
3263 
3264  // Parse the object list.
3265  if (ParseObjectList(CurMultiClass))
3266  return true;
3267 
3268  if (!consume(tgtok::r_brace)) {
3269  TokError("expected '}' at end of foreach command");
3270  return Error(BraceLoc, "to match this '{'");
3271  }
3272  }
3273 
3274  PopLocalScope(ForeachScope);
3275 
3276  // Resolve the loop or store it for later resolution.
3277  std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
3278  Loops.pop_back();
3279 
3280  return addEntry(std::move(Loop));
3281 }
3282 
3283 /// ParseIf - Parse an if statement.
3284 ///
3285 /// If ::= IF Value THEN IfBody
3286 /// If ::= IF Value THEN IfBody ELSE IfBody
3287 ///
3288 bool TGParser::ParseIf(MultiClass *CurMultiClass) {
3289  SMLoc Loc = Lex.getLoc();
3290  assert(Lex.getCode() == tgtok::If && "Unknown tok");
3291  Lex.Lex(); // Eat the 'if' token.
3292 
3293  // Make a temporary object to record items associated with the for
3294  // loop.
3295  Init *Condition = ParseValue(nullptr);
3296  if (!Condition)
3297  return true;
3298 
3299  if (!consume(tgtok::Then))
3300  return TokError("Unknown tok");
3301 
3302  // We have to be able to save if statements to execute later, and they have
3303  // to live on the same stack as foreach loops. The simplest implementation
3304  // technique is to convert each 'then' or 'else' clause *into* a foreach
3305  // loop, over a list of length 0 or 1 depending on the condition, and with no
3306  // iteration variable being assigned.
3307 
3308  ListInit *EmptyList = ListInit::get({}, BitRecTy::get(Records));
3309  ListInit *SingletonList =
3310  ListInit::get({BitInit::get(Records, true)}, BitRecTy::get(Records));
3311  RecTy *BitListTy = ListRecTy::get(BitRecTy::get(Records));
3312 
3313  // The foreach containing the then-clause selects SingletonList if
3314  // the condition is true.
3315  Init *ThenClauseList =
3316  TernOpInit::get(TernOpInit::IF, Condition, SingletonList, EmptyList,
3317  BitListTy)
3318  ->Fold(nullptr);
3319  Loops.push_back(std::make_unique<ForeachLoop>(Loc, nullptr, ThenClauseList));
3320 
3321  if (ParseIfBody(CurMultiClass, "then"))
3322  return true;
3323 
3324  std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
3325  Loops.pop_back();
3326 
3327  if (addEntry(std::move(Loop)))
3328  return true;
3329 
3330  // Now look for an optional else clause. The if-else syntax has the usual
3331  // dangling-else ambiguity, and by greedily matching an else here if we can,
3332  // we implement the usual resolution of pairing with the innermost unmatched
3333  // if.
3334  if (consume(tgtok::ElseKW)) {
3335  // The foreach containing the else-clause uses the same pair of lists as
3336  // above, but this time, selects SingletonList if the condition is *false*.
3337  Init *ElseClauseList =
3338  TernOpInit::get(TernOpInit::IF, Condition, EmptyList, SingletonList,
3339  BitListTy)
3340  ->Fold(nullptr);
3341  Loops.push_back(
3342  std::make_unique<ForeachLoop>(Loc, nullptr, ElseClauseList));
3343 
3344  if (ParseIfBody(CurMultiClass, "else"))
3345  return true;
3346 
3347  Loop = std::move(Loops.back());
3348  Loops.pop_back();
3349 
3350  if (addEntry(std::move(Loop)))
3351  return true;
3352  }
3353 
3354  return false;
3355 }
3356 
3357 /// ParseIfBody - Parse the then-clause or else-clause of an if statement.
3358 ///
3359 /// IfBody ::= Object
3360 /// IfBody ::= '{' ObjectList '}'
3361 ///
3362 bool TGParser::ParseIfBody(MultiClass *CurMultiClass, StringRef Kind) {
3363  TGLocalVarScope *BodyScope = PushLocalScope();
3364 
3365  if (Lex.getCode() != tgtok::l_brace) {
3366  // A single object.
3367  if (ParseObject(CurMultiClass))
3368  return true;
3369  } else {
3370  SMLoc BraceLoc = Lex.getLoc();
3371  // A braced block.
3372  Lex.Lex(); // eat the '{'.
3373 
3374  // Parse the object list.
3375  if (ParseObjectList(CurMultiClass))
3376  return true;
3377 
3378  if (!consume(tgtok::r_brace)) {
3379  TokError("expected '}' at end of '" + Kind + "' clause");
3380  return Error(BraceLoc, "to match this '{'");
3381  }
3382  }
3383 
3384  PopLocalScope(BodyScope);
3385  return false;
3386 }
3387 
3388 /// ParseAssert - Parse an assert statement.
3389 ///
3390 /// Assert ::= ASSERT condition , message ;
3391 bool TGParser::ParseAssert(MultiClass *CurMultiClass, Record *CurRec) {
3392  assert(Lex.getCode() == tgtok::Assert && "Unknown tok");
3393  Lex.Lex(); // Eat the 'assert' token.
3394 
3395  SMLoc ConditionLoc = Lex.getLoc();
3396  Init *Condition = ParseValue(CurRec);
3397  if (!Condition)
3398  return true;
3399 
3400  if (!consume(tgtok::comma)) {
3401  TokError("expected ',' in assert statement");
3402  return true;
3403  }
3404 
3405  Init *Message = ParseValue(CurRec);
3406  if (!Message)
3407  return true;
3408 
3409  if (!consume(tgtok::semi))
3410  return TokError("expected ';'");
3411 
3412  if (CurRec)
3413  CurRec->addAssertion(ConditionLoc, Condition, Message);
3414  else
3415  addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
3416  Message));
3417  return false;
3418 }
3419 
3420 /// ParseClass - Parse a tblgen class definition.
3421 ///
3422 /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
3423 ///
3424 bool TGParser::ParseClass() {
3425  assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
3426  Lex.Lex();
3427 
3428  if (Lex.getCode() != tgtok::Id)
3429  return TokError("expected class name after 'class' keyword");
3430 
3431  Record *CurRec = Records.getClass(Lex.getCurStrVal());
3432  if (CurRec) {
3433  // If the body was previously defined, this is an error.
3434  if (!CurRec->getValues().empty() ||
3435  !CurRec->getSuperClasses().empty() ||
3436  !CurRec->getTemplateArgs().empty())
3437  return TokError("Class '" + CurRec->getNameInitAsString() +
3438  "' already defined");
3439 
3440  CurRec->updateClassLoc(Lex.getLoc());
3441  } else {
3442  // If this is the first reference to this class, create and add it.
3443  auto NewRec =
3444  std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
3445  /*Class=*/true);
3446  CurRec = NewRec.get();
3447  Records.addClass(std::move(NewRec));
3448  }
3449  Lex.Lex(); // eat the name.
3450 
3451  // If there are template args, parse them.
3452  if (Lex.getCode() == tgtok::less)
3453  if (ParseTemplateArgList(CurRec))
3454  return true;
3455 
3456  if (ParseObjectBody(CurRec))
3457  return true;
3458 
3459  if (!NoWarnOnUnusedTemplateArgs)
3460  CurRec->checkUnusedTemplateArgs();
3461  return false;
3462 }
3463 
3464 /// ParseLetList - Parse a non-empty list of assignment expressions into a list
3465 /// of LetRecords.
3466 ///
3467 /// LetList ::= LetItem (',' LetItem)*
3468 /// LetItem ::= ID OptionalRangeList '=' Value
3469 ///
3470 void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
3471  do {
3472  if (Lex.getCode() != tgtok::Id) {
3473  TokError("expected identifier in let definition");
3474  Result.clear();
3475  return;
3476  }
3477 
3478  StringInit *Name = StringInit::get(Records, Lex.getCurStrVal());
3479  SMLoc NameLoc = Lex.getLoc();
3480  Lex.Lex(); // Eat the identifier.
3481 
3482  // Check for an optional RangeList.
3484  if (ParseOptionalRangeList(Bits)) {
3485  Result.clear();
3486  return;
3487  }
3488  std::reverse(Bits.begin(), Bits.end());
3489 
3490  if (!consume(tgtok::equal)) {
3491  TokError("expected '=' in let expression");
3492  Result.clear();
3493  return;
3494  }
3495 
3496  Init *Val = ParseValue(nullptr);
3497  if (!Val) {
3498  Result.clear();
3499  return;
3500  }
3501 
3502  // Now that we have everything, add the record.
3503  Result.emplace_back(Name, Bits, Val, NameLoc);
3504  } while (consume(tgtok::comma));
3505 }
3506 
3507 /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
3508 /// different related productions. This works inside multiclasses too.
3509 ///
3510 /// Object ::= LET LetList IN '{' ObjectList '}'
3511 /// Object ::= LET LetList IN Object
3512 ///
3513 bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
3514  assert(Lex.getCode() == tgtok::Let && "Unexpected token");
3515  Lex.Lex();
3516 
3517  // Add this entry to the let stack.
3518  SmallVector<LetRecord, 8> LetInfo;
3519  ParseLetList(LetInfo);
3520  if (LetInfo.empty()) return true;
3521  LetStack.push_back(std::move(LetInfo));
3522 
3523  if (!consume(tgtok::In))
3524  return TokError("expected 'in' at end of top-level 'let'");
3525 
3526  TGLocalVarScope *LetScope = PushLocalScope();
3527 
3528  // If this is a scalar let, just handle it now
3529  if (Lex.getCode() != tgtok::l_brace) {
3530  // LET LetList IN Object
3531  if (ParseObject(CurMultiClass))
3532  return true;
3533  } else { // Object ::= LETCommand '{' ObjectList '}'
3534  SMLoc BraceLoc = Lex.getLoc();
3535  // Otherwise, this is a group let.
3536  Lex.Lex(); // eat the '{'.
3537 
3538  // Parse the object list.
3539  if (ParseObjectList(CurMultiClass))
3540  return true;
3541 
3542  if (!consume(tgtok::r_brace)) {
3543  TokError("expected '}' at end of top level let command");
3544  return Error(BraceLoc, "to match this '{'");
3545  }
3546  }
3547 
3548  PopLocalScope(LetScope);
3549 
3550  // Outside this let scope, this let block is not active.
3551  LetStack.pop_back();
3552  return false;
3553 }
3554 
3555 /// ParseMultiClass - Parse a multiclass definition.
3556 ///
3557 /// MultiClassInst ::= MULTICLASS ID TemplateArgList?
3558 /// ':' BaseMultiClassList '{' MultiClassObject+ '}'
3559 /// MultiClassObject ::= Assert
3560 /// MultiClassObject ::= DefInst
3561 /// MultiClassObject ::= DefMInst
3562 /// MultiClassObject ::= Defvar
3563 /// MultiClassObject ::= Foreach
3564 /// MultiClassObject ::= If
3565 /// MultiClassObject ::= LETCommand '{' ObjectList '}'
3566 /// MultiClassObject ::= LETCommand Object
3567 ///
3568 bool TGParser::ParseMultiClass() {
3569  assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
3570  Lex.Lex(); // Eat the multiclass token.
3571 
3572  if (Lex.getCode() != tgtok::Id)
3573  return TokError("expected identifier after multiclass for name");
3574  std::string Name = Lex.getCurStrVal();
3575 
3576  auto Result =
3577  MultiClasses.insert(std::make_pair(Name,
3578  std::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
3579 
3580  if (!Result.second)
3581  return TokError("multiclass '" + Name + "' already defined");
3582 
3583  CurMultiClass = Result.first->second.get();
3584  Lex.Lex(); // Eat the identifier.
3585 
3586  // If there are template args, parse them.
3587  if (Lex.getCode() == tgtok::less)
3588  if (ParseTemplateArgList(nullptr))
3589  return true;
3590 
3591  bool inherits = false;
3592 
3593  // If there are submulticlasses, parse them.
3594  if (consume(tgtok::colon)) {
3595  inherits = true;
3596 
3597  // Read all of the submulticlasses.
3598  SubMultiClassReference SubMultiClass =
3599  ParseSubMultiClassReference(CurMultiClass);
3600  while (true) {
3601  // Check for error.
3602  if (!SubMultiClass.MC) return true;
3603 
3604  // Add it.
3605  if (AddSubMultiClass(CurMultiClass, SubMultiClass))
3606  return true;
3607 
3608  if (!consume(tgtok::comma))
3609  break;
3610  SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
3611  }
3612  }
3613 
3614  if (Lex.getCode() != tgtok::l_brace) {
3615  if (!inherits)
3616  return TokError("expected '{' in multiclass definition");
3617  if (!consume(tgtok::semi))
3618  return TokError("expected ';' in multiclass definition");
3619  } else {
3620  if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
3621  return TokError("multiclass must contain at least one def");
3622 
3623  // A multiclass body introduces a new scope for local variables.
3624  TGLocalVarScope *MulticlassScope = PushLocalScope();
3625 
3626  while (Lex.getCode() != tgtok::r_brace) {
3627  switch (Lex.getCode()) {
3628  default:
3629  return TokError("expected 'assert', 'def', 'defm', 'defvar', "
3630  "'foreach', 'if', or 'let' in multiclass body");
3631 
3632  case tgtok::Assert:
3633  case tgtok::Def:
3634  case tgtok::Defm:
3635  case tgtok::Defvar:
3636  case tgtok::Foreach:
3637  case tgtok::If:
3638  case tgtok::Let:
3639  if (ParseObject(CurMultiClass))
3640  return true;
3641  break;
3642  }
3643  }
3644  Lex.Lex(); // eat the '}'.
3645 
3646  // If we have a semicolon, print a gentle error.
3647  SMLoc SemiLoc = Lex.getLoc();
3648  if (consume(tgtok::semi)) {
3649  PrintError(SemiLoc, "A multiclass body should not end with a semicolon");
3650  PrintNote("Semicolon ignored; remove to eliminate this error");
3651  }
3652 
3653  PopLocalScope(MulticlassScope);
3654  }
3655 
3656  if (!NoWarnOnUnusedTemplateArgs)
3657  CurMultiClass->Rec.checkUnusedTemplateArgs();
3658 
3659  CurMultiClass = nullptr;
3660  return false;
3661 }
3662 
3663 /// ParseDefm - Parse the instantiation of a multiclass.
3664 ///
3665 /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
3666 ///
3667 bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
3668  assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
3669  Lex.Lex(); // eat the defm
3670 
3671  Init *DefmName = ParseObjectName(CurMultiClass);
3672  if (!DefmName)
3673  return true;
3674  if (isa<UnsetInit>(DefmName)) {
3675  DefmName = Records.getNewAnonymousName();
3676  if (CurMultiClass)
3677  DefmName = BinOpInit::getStrConcat(
3679  StringRecTy::get(Records)),
3680  DefmName);
3681  }
3682 
3683  if (Lex.getCode() != tgtok::colon)
3684  return TokError("expected ':' after defm identifier");
3685 
3686  // Keep track of the new generated record definitions.
3687  std::vector<RecordsEntry> NewEntries;
3688 
3689  // This record also inherits from a regular class (non-multiclass)?
3690  bool InheritFromClass = false;
3691 
3692  // eat the colon.
3693  Lex.Lex();
3694 
3695  SMLoc SubClassLoc = Lex.getLoc();
3696  SubClassReference Ref = ParseSubClassReference(nullptr, true);
3697 
3698  while (true) {
3699  if (!Ref.Rec) return true;
3700 
3701  // To instantiate a multiclass, we get the multiclass and then loop
3702  // through its template argument names. Substs contains a substitution
3703  // value for each argument, either the value specified or the default.
3704  // Then we can resolve the template arguments.
3705  MultiClass *MC = MultiClasses[std::string(Ref.Rec->getName())].get();
3706  assert(MC && "Didn't lookup multiclass correctly?");
3707 
3708  ArrayRef<Init *> TemplateVals = Ref.TemplateArgs;
3709  ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
3710  SubstStack Substs;
3711 
3712  for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
3713  if (i < TemplateVals.size()) {
3714  Substs.emplace_back(TArgs[i], TemplateVals[i]);
3715  } else {
3716  Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
3717  if (!Default->isComplete())
3718  return Error(SubClassLoc,
3719  "value not specified for template argument '" +
3720  TArgs[i]->getAsUnquotedString() + "' (#" +
3721  Twine(i) + ") of multiclass '" +
3722  MC->Rec.getNameInitAsString() + "'");
3723  Substs.emplace_back(TArgs[i], Default);
3724  }
3725  }
3726 
3727  Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName);
3728 
3729  if (resolve(MC->Entries, Substs, !CurMultiClass && Loops.empty(),
3730  &NewEntries, &SubClassLoc))
3731  return true;
3732 
3733  if (!consume(tgtok::comma))
3734  break;
3735 
3736  if (Lex.getCode() != tgtok::Id)
3737  return TokError("expected identifier");
3738 
3739  SubClassLoc = Lex.getLoc();
3740 
3741  // A defm can inherit from regular classes (non-multiclasses) as
3742  // long as they come in the end of the inheritance list.
3743  InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
3744 
3745  if (InheritFromClass)
3746  break;
3747 
3748  Ref = ParseSubClassReference(nullptr, true);
3749  }
3750 
3751  if (InheritFromClass) {
3752  // Process all the classes to inherit as if they were part of a
3753  // regular 'def' and inherit all record values.
3754  SubClassReference SubClass = ParseSubClassReference(nullptr, false);
3755  while (true) {
3756  // Check for error.
3757  if (!SubClass.Rec) return true;
3758 
3759  // Get the expanded definition prototypes and teach them about
3760  // the record values the current class to inherit has
3761  for (auto &E : NewEntries) {
3762  // Add it.
3763  if (AddSubClass(E, SubClass))
3764  return true;
3765  }
3766 
3767  if (!consume(tgtok::comma))
3768  break;
3769  SubClass = ParseSubClassReference(nullptr, false);
3770  }
3771  }
3772 
3773  for (auto &E : NewEntries) {
3774  if (ApplyLetStack(E))
3775  return true;
3776 
3777  addEntry(std::move(E));
3778  }
3779 
3780  if (!consume(tgtok::semi))
3781  return TokError("expected ';' at end of defm");
3782 
3783  return false;
3784 }
3785 
3786 /// ParseObject
3787 /// Object ::= ClassInst
3788 /// Object ::= DefInst
3789 /// Object ::= MultiClassInst
3790 /// Object ::= DefMInst
3791 /// Object ::= LETCommand '{' ObjectList '}'
3792 /// Object ::= LETCommand Object
3793 /// Object ::= Defset
3794 /// Object ::= Defvar
3795 /// Object ::= Assert
3796 bool TGParser::ParseObject(MultiClass *MC) {
3797  switch (Lex.getCode()) {
3798  default:
3799  return TokError(
3800  "Expected assert, class, def, defm, defset, foreach, if, or let");
3801  case tgtok::Assert: return ParseAssert(MC);
3802  case tgtok::Def: return ParseDef(MC);
3803  case tgtok::Defm: return ParseDefm(MC);
3804  case tgtok::Defvar: return ParseDefvar();
3805  case tgtok::Foreach: return ParseForeach(MC);
3806  case tgtok::If: return ParseIf(MC);
3807  case tgtok::Let: return ParseTopLevelLet(MC);
3808  case tgtok::Defset:
3809  if (MC)
3810  return TokError("defset is not allowed inside multiclass");
3811  return ParseDefset();
3812  case tgtok::Class:
3813  if (MC)
3814  return TokError("class is not allowed inside multiclass");
3815  if (!Loops.empty())
3816  return TokError("class is not allowed inside foreach loop");
3817  return ParseClass();
3818  case tgtok::MultiClass:
3819  if (!Loops.empty())
3820  return TokError("multiclass is not allowed inside foreach loop");
3821  return ParseMultiClass();
3822  }
3823 }
3824 
3825 /// ParseObjectList
3826 /// ObjectList :== Object*
3827 bool TGParser::ParseObjectList(MultiClass *MC) {
3828  while (isObjectStart(Lex.getCode())) {
3829  if (ParseObject(MC))
3830  return true;
3831  }
3832  return false;
3833 }
3834 
3836  Lex.Lex(); // Prime the lexer.
3837  if (ParseObjectList()) return true;
3838 
3839  // If we have unread input at the end of the file, report it.
3840  if (Lex.getCode() == tgtok::Eof)
3841  return false;
3842 
3843  return TokError("Unexpected token at top level");
3844 }
3845 
3846 // Check the types of the template argument values for a class
3847 // inheritance, multiclass invocation, or anonymous class invocation.
3848 // If necessary, replace an argument with a cast to the required type.
3849 // The argument count has already been checked.
3850 bool TGParser::CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
3851  SMLoc Loc, Record *ArgsRec) {
3852 
3853  ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
3854 
3855  for (unsigned I = 0, E = Values.size(); I < E; ++I) {
3856  RecordVal *Arg = ArgsRec->getValue(TArgs[I]);
3857  RecTy *ArgType = Arg->getType();
3858  auto *Value = Values[I];
3859 
3860  if (TypedInit *ArgValue = dyn_cast<TypedInit>(Value)) {
3861  auto *CastValue = ArgValue->getCastTo(ArgType);
3862  if (CastValue) {
3863  assert((!isa<TypedInit>(CastValue) ||
3864  cast<TypedInit>(CastValue)->getType()->typeIsA(ArgType)) &&
3865  "result of template arg value cast has wrong type");
3866  Values[I] = CastValue;
3867  } else {
3868  PrintFatalError(Loc,
3869  "Value specified for template argument '" +
3870  Arg->getNameInitAsString() + "' (#" + Twine(I) +
3871  ") is of type " + ArgValue->getType()->getAsString() +
3872  "; expected type " + ArgType->getAsString() + ": " +
3873  ArgValue->getAsString());
3874  }
3875  }
3876  }
3877 
3878  return false;
3879 }
3880 
3881 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3883  if (Loop)
3884  Loop->dump();
3885  if (Rec)
3886  Rec->dump();
3887 }
3888 
3890  errs() << "foreach " << IterVar->getAsString() << " = "
3891  << ListValue->getAsString() << " in {\n";
3892 
3893  for (const auto &E : Entries)
3894  E.dump();
3895 
3896  errs() << "}\n";
3897 }
3898 
3900  errs() << "Record:\n";
3901  Rec.dump();
3902 
3903  errs() << "Defs:\n";
3904  for (const auto &E : Entries)
3905  E.dump();
3906 }
3907 #endif
llvm::tgtok::XFilter
@ XFilter
Definition: TGLexer.h:57
llvm::FieldInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:2080
i
i
Definition: README.txt:29
llvm::TernOpInit::IF
@ IF
Definition: Record.h:914
llvm::Record::isSubClassOf
bool isSubClassOf(const Record *R) const
Definition: Record.h:1754
llvm::BinOpInit::XOR
@ XOR
Definition: Record.h:843
llvm::SMRange::Start
SMLoc Start
Definition: SMLoc.h:50
llvm::ForeachLoop::dump
void dump() const
Definition: TGParser.cpp:3889
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::tgtok::Def
@ Def
Definition: TGLexer.h:50
llvm::StringRecTy
'string' - Represent an string value
Definition: Record.h:167
llvm::RecordKeeper
Definition: Record.h:1888
llvm::Operator
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
llvm::TGParser::TokError
bool TokError(const Twine &Msg) const
Definition: TGParser.h:182
llvm::RecordVal::FK_Normal
@ FK_Normal
Definition: Record.h:1490
llvm::Init::getCastTo
virtual Init * getCastTo(RecTy *Ty) const =0
If this value is convertible to type Ty, return a value whose type is Ty, generating a !...
llvm::TernOpInit::FOREACH
@ FOREACH
Definition: Record.h:914
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:161
llvm::BinOpInit::get
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:924
llvm::X86II::TA
@ TA
Definition: X86BaseInfo.h:808
llvm::tgtok::XForEach
@ XForEach
Definition: TGLexer.h:57
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:546
llvm::tgtok::Field
@ Field
Definition: TGLexer.h:51
llvm::BinOpInit::AND
@ AND
Definition: Record.h:841
Loops
Hexagon Hardware Loops
Definition: HexagonHardwareLoops.cpp:372
llvm::MultiClass::Rec
Record Rec
Definition: TGParser.h:121
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
llvm::tgtok::Code
@ Code
Definition: TGLexer.h:50
llvm::tgtok::paste
@ paste
Definition: TGLexer.h:45
llvm::tgtok::XXOR
@ XXOR
Definition: TGLexer.h:55
checkConcrete
static void checkConcrete(Record &R)
Definition: TGParser.cpp:91
llvm::DagRecTy::get
static DagRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:197
ErrorHandling.h
llvm::tgtok::l_brace
@ l_brace
Definition: TGLexer.h:39
llvm::Record::getSuperClasses
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
Definition: Record.h:1688
llvm::tgtok::XStrConcat
@ XStrConcat
Definition: TGLexer.h:56
llvm::tgtok::l_paren
@ l_paren
Definition: TGLexer.h:40
llvm::DagInit
(v a, b) - Represent a DAG tree value.
Definition: Record.h:1390
llvm::FieldInit::get
static FieldInit * get(Init *R, StringInit *FN)
Definition: Record.cpp:2059
llvm::tgtok::VarName
@ VarName
Definition: TGLexer.h:72
llvm::tgtok::Defm
@ Defm
Definition: TGLexer.h:50
llvm::tgtok::comma
@ comma
Definition: TGLexer.h:43
llvm::BinOpInit::DIV
@ DIV
Definition: Record.h:840
llvm::BinOpInit::OR
@ OR
Definition: Record.h:842
llvm::Record::addAssertion
void addAssertion(SMLoc Loc, Init *Condition, Init *Message)
Definition: Record.h:1743
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::BinOpInit
!op (X, Y) - Combine two inits.
Definition: Record.h:834
llvm::tgtok::Bits
@ Bits
Definition: TGLexer.h:50
llvm::BinOpInit::SRA
@ SRA
Definition: Record.h:845
llvm::tgtok::XGe
@ XGe
Definition: TGLexer.h:58
llvm::DagRecTy
'dag' - Represent a dag fragment
Definition: Record.h:210
llvm::tgtok::XSUB
@ XSUB
Definition: TGLexer.h:55
llvm::tgtok::l_square
@ l_square
Definition: TGLexer.h:38
llvm::SubMultiClassReference::TemplateArgs
SmallVector< Init *, 4 > TemplateArgs
Definition: TGParser.cpp:50
llvm::BinOpInit::GT
@ GT
Definition: Record.h:857
llvm::tgtok::FalseVal
@ FalseVal
Definition: TGLexer.h:62
llvm::TGParser::PushLocalScope
TGLocalVarScope * PushLocalScope()
Definition: TGParser.h:189
llvm::TernOpInit::SUBST
@ SUBST
Definition: Record.h:914
llvm::ListInit::get
static ListInit * get(ArrayRef< Init * > Range, RecTy *EltTy)
Definition: Record.cpp:623
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:891
llvm::Record::checkUnusedTemplateArgs
void checkUnusedTemplateArgs()
Definition: Record.cpp:2812
llvm::FoldOpInit::get
static FoldOpInit * get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
Definition: Record.cpp:1555
llvm::Init::getBit
virtual Init * getBit(unsigned Bit) const =0
Get the Init value of the specified bit.
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
llvm::tgtok::XHead
@ XHead
Definition: TGLexer.h:57
llvm::BinOpInit::getStrConcat
static Init * getStrConcat(Init *lhs, Init *rhs)
Definition: Record.cpp:994
llvm::tgtok::BinaryIntVal
@ BinaryIntVal
Definition: TGLexer.h:69
llvm::TernOpInit::SUBSTR
@ SUBSTR
Definition: Record.h:914
llvm::VarInit
'Opcode' - Represent a reference to an entire variable object.
Definition: Record.h:1155
llvm::RecordVal::FK_NonconcreteOK
@ FK_NonconcreteOK
Definition: Record.h:1491
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
llvm::RecTy
Definition: Record.h:57
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:265
llvm::TernOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1360
llvm::TGLexer::Lex
tgtok::TokKind Lex()
Definition: TGLexer.h:107
llvm::CondOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:2174
llvm::SubMultiClassReference::isInvalid
bool isInvalid() const
Definition: TGParser.cpp:54
llvm::BitsRecTy
'bits<n>' - Represent a fixed number of bits
Definition: Record.h:126
llvm::tgtok::XMUL
@ XMUL
Definition: TGLexer.h:55
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:187
llvm::Record::getTemplateArgs
ArrayRef< Init * > getTemplateArgs() const
Definition: Record.h:1680
llvm::tgtok::Dag
@ Dag
Definition: TGLexer.h:50
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::tgtok::XFind
@ XFind
Definition: TGLexer.h:56
llvm::tgtok::Then
@ Then
Definition: TGLexer.h:52
llvm::RecordVal::addReferenceLoc
void addReferenceLoc(SMRange Loc)
Add a reference to this record value.
Definition: Record.h:1552
llvm::Record::resolveReferences
void resolveReferences(Init *NewName=nullptr)
If there are any field references that refer to fields that have been filled in, we can propagate the...
Definition: Record.cpp:2557
llvm::SubMultiClassReference::RefRange
SMRange RefRange
Definition: TGParser.cpp:48
llvm::RecordsEntry::dump
void dump() const
Definition: TGParser.cpp:3882
llvm::tgtok::question
@ question
Definition: TGLexer.h:44
QualifyName
static Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, StringRef Scoper)
Return an Init with a qualifier prefix referring to CurRec's name.
Definition: TGParser.cpp:115
llvm::UnOpInit::GETDAGOP
@ GETDAGOP
Definition: Record.h:787
llvm::tgtok::XOR
@ XOR
Definition: TGLexer.h:55
llvm::MultiClass::dump
void dump() const
Definition: TGParser.cpp:3899
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:418
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
llvm::BitInit::get
static BitInit * get(RecordKeeper &RK, bool V)
Definition: Record.cpp:371
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Operation
PowerPC Reduce CR logical Operation
Definition: PPCReduceCRLogicals.cpp:734
llvm::CheckAssert
void CheckAssert(SMLoc Loc, Init *Condition, Init *Message)
Definition: Error.cpp:159
llvm::UnOpInit::UnaryOp
UnaryOp
Definition: Record.h:787
llvm::RecordsEntry
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
Definition: TGParser.h:41
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
Twine.h
llvm::BinOpInit::BinaryOp
BinaryOp
Definition: Record.h:836
llvm::ListRecTy::getElementType
RecTy * getElementType() const
Definition: Record.h:200
llvm::TGParser::Error
bool Error(SMLoc L, const Twine &Msg) const
Definition: TGParser.h:178
llvm::RecordKeeper::addDef
void addDef(std::unique_ptr< Record > R)
Definition: Record.h:1942
llvm::tgtok::Assert
@ Assert
Definition: TGLexer.h:50
llvm::TGLexer::getCurBinaryIntVal
std::pair< int64_t, unsigned > getCurBinaryIntVal() const
Definition: TGLexer.h:127
llvm::tgtok::XIsA
@ XIsA
Definition: TGLexer.h:58
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::UnOpInit::CAST
@ CAST
Definition: Record.h:787
llvm::tgtok::XCast
@ XCast
Definition: TGLexer.h:56
llvm::BinOpInit::ADD
@ ADD
Definition: Record.h:837
llvm::tgtok::XListConcat
@ XListConcat
Definition: TGLexer.h:56
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Record::addTemplateArg
void addTemplateArg(Init *Name)
Definition: Record.h:1720
llvm::SubClassReference::SubClassReference
SubClassReference()
Definition: TGParser.cpp:42
llvm::TypedInit::getType
RecTy * getType() const
Get the type of the Init as a RecTy.
Definition: Record.h:438
llvm::PrintFatalError
void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:125
llvm::Record::getNameInitAsString
std::string getNameInitAsString() const
Definition: Record.h:1650
llvm::BinOpInit::getListConcat
static Init * getListConcat(TypedInit *lhs, Init *rhs)
Definition: Record.cpp:1011
llvm::tgtok::If
@ If
Definition: TGLexer.h:51
llvm::tgtok::equal
@ equal
Definition: TGLexer.h:44
llvm::HasReferenceResolver
Do not resolve anything, but keep track of whether a given variable was referenced.
Definition: Record.h:2239
llvm::BitsInit::getBit
Init * getBit(unsigned Bit) const override
Get the Init value of the specified bit.
Definition: Record.h:571
llvm::TernOpInit::get
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
Definition: Record.cpp:1261
llvm::RecordVal::getValue
Init * getValue() const
Get the value of the field as an Init.
Definition: Record.h:1543
llvm::tgtok::XNOT
@ XNOT
Definition: TGLexer.h:55
llvm::None
const NoneType None
Definition: None.h:24
llvm::CondOpInit::get
static CondOpInit * get(ArrayRef< Init * > C, ArrayRef< Init * > V, RecTy *Type)
Definition: Record.cpp:2126
llvm::SubMultiClassReference::dump
void dump() const
Definition: TGParser.cpp:59
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::SubClassReference::RefRange
SMRange RefRange
Definition: TGParser.cpp:38
llvm::tgtok::XGt
@ XGt
Definition: TGLexer.h:58
llvm::ForeachLoop
ForeachLoop - Record the iteration state associated with a for loop.
Definition: TGParser.h:62
llvm::TernOpInit::DAG
@ DAG
Definition: Record.h:914
llvm::Record::dump
void dump() const
Definition: Record.cpp:2565
llvm::tgtok::XSRA
@ XSRA
Definition: TGLexer.h:55
llvm::BinOpInit::LISTSPLAT
@ LISTSPLAT
Definition: Record.h:848
llvm::tgtok::Foreach
@ Foreach
Definition: TGLexer.h:51
llvm::StringInit::SF_Code
@ SF_Code
Definition: Record.h:642
llvm::RecordVal::setUsed
void setUsed(bool Used)
Whether this value is used.
Definition: Record.h:1559
llvm::TernOpInit::TernaryOp
TernaryOp
Definition: Record.h:914
llvm::SubMultiClassReference
Definition: TGParser.cpp:47
llvm::BinOpInit::NE
@ NE
Definition: Record.h:853
llvm::tgtok::XInterleave
@ XInterleave
Definition: TGLexer.h:56
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::RecordVal::setValue
bool setValue(Init *V)
Set the value of the field from an Init.
Definition: Record.cpp:2378
llvm::tgtok::Defset
@ Defset
Definition: TGLexer.h:50
llvm::SubMultiClassReference::MC
MultiClass * MC
Definition: TGParser.cpp:49
VI
@ VI
Definition: SIInstrInfo.cpp:7894
llvm::tgtok::XIf
@ XIf
Definition: TGLexer.h:57
llvm::IsAOpInit::get
static IsAOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1620
llvm::SubClassReference::isInvalid
bool isInvalid() const
Definition: TGParser.cpp:44
llvm::IntInit
'7' - Represent an initialization by a literal integer value.
Definition: Record.h:578
llvm::ListInit
[AL, AH, CL] - Represent a list of defs
Definition: Record.h:693
uint64_t
llvm::tgtok::XEq
@ XEq
Definition: TGLexer.h:58
llvm::TGLexer::getCode
tgtok::TokKind getCode() const
Definition: TGLexer.h:115
llvm::Record::updateClassLoc
void updateClassLoc(SMLoc Loc)
Definition: Record.cpp:2440
llvm::tgtok::Int
@ Int
Definition: TGLexer.h:51
llvm::tgtok::XDIV
@ XDIV
Definition: TGLexer.h:55
llvm::MapResolver
Resolve arbitrary mappings.
Definition: Record.h:2161
llvm::BitsInit::get
static BitsInit * get(RecordKeeper &RK, ArrayRef< Init * > Range)
Definition: Record.cpp:399
llvm::tgtok::XExists
@ XExists
Definition: TGLexer.h:59
llvm::ForeachLoop::ListValue
Init * ListValue
Definition: TGParser.h:65
llvm::Record::getValues
ArrayRef< RecordVal > getValues() const
Definition: Record.h:1684
llvm::DefInit
AL - Represent a reference to a 'def' in the description.
Definition: Record.h:1262
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:53
llvm::tgtok::ElseKW
@ ElseKW
Definition: TGLexer.h:50
llvm::VarInit::get
static VarInit * get(StringRef VN, RecTy *T)
Definition: Record.cpp:1823
llvm::RecordRecTy
'[classname]' - Type of record values that have zero or more superclasses.
Definition: Record.h:229
llvm::SubMultiClassReference::SubMultiClassReference
SubMultiClassReference()
Definition: TGParser.cpp:52
llvm::tgtok::r_square
@ r_square
Definition: TGLexer.h:38
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::Record::addValue
void addValue(const RecordVal &RV)
Definition: Record.h:1725
llvm::SubClassReference
Definition: TGParser.cpp:37
StringExtras.h
llvm::MultiClass
Definition: TGParser.h:120
llvm::tgtok::Defvar
@ Defvar
Definition: TGLexer.h:50
llvm::Record::isClass
bool isClass() const
Definition: Record.h:1678
llvm::IntInit::getValue
int64_t getValue() const
Definition: Record.h:594
llvm::SubClassReference::TemplateArgs
SmallVector< Init *, 4 > TemplateArgs
Definition: TGParser.cpp:40
llvm::tgtok::XSubstr
@ XSubstr
Definition: TGLexer.h:56
llvm::resolveTypes
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
Definition: Record.cpp:317
llvm::BinOpInit::GE
@ GE
Definition: Record.h:856
llvm::pdb::PDB_MemoryType::Stack
@ Stack
llvm::BinOpInit::SHL
@ SHL
Definition: Record.h:844
llvm::ExistsOpInit::get
static ExistsOpInit * get(RecTy *CheckType, Init *Expr)
Definition: Record.cpp:1682
llvm::tgtok::r_paren
@ r_paren
Definition: TGLexer.h:40
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ListRecTy
'list<Ty>' - Represent a list of element values, all of which must be of the specified type.
Definition: Record.h:186
llvm::tgtok::XConcat
@ XConcat
Definition: TGLexer.h:55
llvm::Init::resolveReferences
virtual Init * resolveReferences(Resolver &R) const
This function is used by classes that refer to other variables which may not be defined at the time t...
Definition: Record.h:407
llvm::BinOpInit::LISTCONCAT
@ LISTCONCAT
Definition: Record.h:847
llvm::Init::getAsUnquotedString
virtual std::string getAsUnquotedString() const
Convert this value to a literal form, without adding quotes around a string.
Definition: Record.h:364
llvm::BinOpInit::EQ
@ EQ
Definition: Record.h:852
llvm::TGLocalVarScope
Definition: TGParser.h:80
Mode
SI Whole Quad Mode
Definition: SIWholeQuadMode.cpp:262
llvm::IntInit::get
static IntInit * get(RecordKeeper &RK, int64_t V)
Definition: Record.cpp:516
llvm::Record
Definition: Record.h:1573
QualifiedNameOfImplicitName
static Init * QualifiedNameOfImplicitName(Record &Rec, MultiClass *MC=nullptr)
Return the qualified version of the implicit 'NAME' template argument.
Definition: TGParser.cpp:133
llvm::tgtok::XLe
@ XLe
Definition: TGLexer.h:58
llvm::RecordKeeper::getClass
Record * getClass(StringRef Name) const
Get the class with the specified name.
Definition: Record.h:1912
llvm::BinOpInit::SUB
@ SUB
Definition: Record.h:838
llvm::Init::getAsString
virtual std::string getAsString() const =0
Convert this value to a literal form.
llvm::UnOpInit::NOT
@ NOT
Definition: Record.h:787
llvm::VarInit::getAsString
std::string getAsString() const override
Convert this value to a literal form.
Definition: Record.h:1188
llvm::Record::getType
RecordRecTy * getType()
Definition: Record.cpp:2456
getType
static M68kRelType getType(unsigned Kind, MCSymbolRefExpr::VariantKind &Modifier, bool &IsPCRel)
Definition: M68kELFObjectWriter.cpp:48
llvm::RecordsEntry::Rec
std::unique_ptr< Record > Rec
Definition: TGParser.h:42
llvm::StringInit
"foo" - Represent an initialization by a string value.
Definition: Record.h:638
llvm::ForeachLoop::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:66
llvm::tgtok::XCond
@ XCond
Definition: TGLexer.h:58
llvm::BinOpInit::INTERLEAVE
@ INTERLEAVE
Definition: Record.h:850
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:98
llvm::ArrayRef< unsigned >
llvm::Record::addSuperClass
void addSuperClass(Record *R, SMRange Range)
Definition: Record.h:1773
None.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::BitsInit::getNumBits
unsigned getNumBits() const
Definition: Record.h:549
llvm::tgtok::String
@ String
Definition: TGLexer.h:52
llvm::RecordKeeper::getGlobal
Init * getGlobal(StringRef Name) const
Get the Init value of the specified global variable.
Definition: Record.h:1924
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::tgtok::XADD
@ XADD
Definition: TGLexer.h:55
llvm::RecordVal::getNameInit
Init * getNameInit() const
Get the name of the field as an Init.
Definition: Record.h:1516
llvm::Record::getNameInit
Init * getNameInit() const
Definition: Record.h:1646
Compiler.h
llvm::ListRecTy::get
static ListRecTy * get(RecTy *T)
Definition: Record.h:199
llvm::tgtok::XSRL
@ XSRL
Definition: TGLexer.h:55
llvm::ListInit::empty
bool empty() const
Definition: Record.h:751
llvm::tgtok::XGetDagOp
@ XGetDagOp
Definition: TGLexer.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::LetRecord
Definition: TGParser.h:29
llvm::RecordVal::FK_TemplateArg
@ FK_TemplateArg
Definition: Record.h:1492
llvm::Init
Definition: Record.h:281
llvm::RecTy::getListTy
ListRecTy * getListTy()
Returns the type representing list<thistype>.
Definition: Record.cpp:107
llvm::RecordVal::getName
StringRef getName() const
Get the name of the field as a StringRef.
Definition: Record.cpp:2359
llvm::AMDGPU::SendMsg::Msg
const CustomOperand< const MCSubtargetInfo & > Msg[]
Definition: AMDGPUAsmUtils.cpp:39
llvm::tgtok::StrVal
@ StrVal
Definition: TGLexer.h:72
llvm::tgtok::dot
@ dot
Definition: TGLexer.h:43
llvm::tgtok::IntVal
@ IntVal
Definition: TGLexer.h:65
llvm::BitsRecTy::get
static BitsRecTy * get(RecordKeeper &RK, unsigned Sz)
Definition: Record.cpp:132
llvm::DagInit::get
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init * > ArgRange, ArrayRef< StringInit * > NameRange)
Definition: Record.cpp:2251
llvm::UnOpInit::SIZE
@ SIZE
Definition: Record.h:787
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::tgtok::r_brace
@ r_brace
Definition: TGLexer.h:39
llvm::RecordVal::getType
RecTy * getType() const
Get the type of the field value as a RecTy.
Definition: Record.h:1537
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::UnOpInit::get
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
Definition: Record.cpp:759
llvm::tgtok::Eof
@ Eof
Definition: TGLexer.h:34
llvm::StringRecTy::get
static StringRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:168
llvm::BinOpInit::SRL
@ SRL
Definition: Record.h:846
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::TernOpInit::FILTER
@ FILTER
Definition: Record.h:914
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::TGLexer::getCurIntVal
int64_t getCurIntVal() const
Definition: TGLexer.h:123
llvm::Record::getRecords
RecordKeeper & getRecords() const
Definition: Record.h:1794
llvm::VarInit::getNameInit
Init * getNameInit() const
Definition: Record.h:1173
llvm::Record::getValue
const RecordVal * getValue(const Init *Name) const
Definition: Record.h:1702
llvm::StringInit::getValue
StringRef getValue() const
Definition: Record.h:667
llvm::tgtok::XAND
@ XAND
Definition: TGLexer.h:55
Casting.h
llvm::tgtok::XListSplat
@ XListSplat
Definition: TGLexer.h:56
llvm::tgtok::XSHL
@ XSHL
Definition: TGLexer.h:55
llvm::TargetStackID::Default
@ Default
Definition: TargetFrameLowering.h:28
llvm::Loop::dump
void dump() const
Definition: LoopInfo.cpp:663
llvm::ForeachLoop::IterVar
VarInit * IterVar
Definition: TGParser.h:64
llvm::UnOpInit::TAIL
@ TAIL
Definition: Record.h:787
llvm::ListRecTy::typeIsConvertibleTo
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Definition: Record.cpp:185
llvm::UnOpInit::HEAD
@ HEAD
Definition: Record.h:787
llvm::tgtok::XFoldl
@ XFoldl
Definition: TGLexer.h:57
TGParser.h
llvm::tgtok::XSubst
@ XSubst
Definition: TGLexer.h:57
llvm::tgtok::Class
@ Class
Definition: TGLexer.h:50
llvm::TGLexer::getCurStrVal
const std::string & getCurStrVal() const
Definition: TGLexer.h:117
llvm::tgtok::XDag
@ XDag
Definition: TGLexer.h:58
llvm::Record::isTemplateArg
bool isTemplateArg(Init *Name) const
Definition: Record.h:1698
llvm::tgtok::greater
@ greater
Definition: TGLexer.h:41
llvm::RecTy::typeIsConvertibleTo
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:113
isObjectStart
static bool isObjectStart(tgtok::TokKind K)
isObjectStart - Return true if this is a valid first token for a statement.
Definition: TGParser.cpp:516
llvm::BinOpInit::LT
@ LT
Definition: Record.h:855
llvm::TernOpInit::FIND
@ FIND
Definition: Record.h:914
llvm::RecTy::getAsString
virtual std::string getAsString() const =0
llvm::MultiClass::Entries
std::vector< RecordsEntry > Entries
Definition: TGParser.h:122
llvm::BinOpInit::SETDAGOP
@ SETDAGOP
Definition: Record.h:858
checkBitsConcrete
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
Definition: TGParser.cpp:72
SmallVector.h
llvm::tgtok::CodeFragment
@ CodeFragment
Definition: TGLexer.h:72
llvm::RecordRecTy::get
static RecordRecTy * get(RecordKeeper &RK, ArrayRef< Record * > Classes)
Get the record type with the given non-redundant list of superclasses.
Definition: Record.cpp:212
llvm::tgtok::XSize
@ XSize
Definition: TGLexer.h:57
llvm::tgtok::XNe
@ XNe
Definition: TGLexer.h:58
llvm::PrintError
void PrintError(const Twine &Msg)
Definition: Error.cpp:101
llvm::tgtok::less
@ less
Definition: TGLexer.h:41
llvm::DefsetRecord
Definition: TGParser.h:74
llvm::FoldOpInit::Fold
Init * Fold(Record *CurRec) const
Definition: Record.cpp:1574
llvm::SMRange
Represents a range in source code.
Definition: SMLoc.h:48
List
const NodeList & List
Definition: RDFGraph.cpp:199
llvm::tgtok::MultiClass
@ MultiClass
Definition: TGLexer.h:51
llvm::Record::appendAssertions
void appendAssertions(const Record *Rec)
Definition: Record.h:1747
llvm::tgtok::TokKind
TokKind
Definition: TGLexer.h:32
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition: SmallVector.h:659
llvm::BinOpInit::STRCONCAT
@ STRCONCAT
Definition: Record.h:849
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::RecordKeeper::getNewAnonymousName
Init * getNewAnonymousName()
GetNewAnonymousName - Generate a unique anonymous name that can be used as an identifier.
Definition: Record.cpp:2842
llvm::PrintNote
void PrintNote(const Twine &Msg)
Definition: Error.cpp:45
llvm::SubClassReference::Rec
Record * Rec
Definition: TGParser.cpp:39
llvm::tgtok::Bit
@ Bit
Definition: TGLexer.h:50
llvm::UnOpInit::EMPTY
@ EMPTY
Definition: Record.h:787
llvm::BinOpInit::MUL
@ MUL
Definition: Record.h:839
llvm::TGLexer::getLocRange
SMRange getLocRange() const
Definition: TGLexer.cpp:66
llvm::SmallVectorImpl< unsigned >
llvm::reverse
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:365
DenseMapInfo.h
llvm::tgtok::XSetDagOp
@ XSetDagOp
Definition: TGLexer.h:58
llvm::BinOpInit::LE
@ LE
Definition: Record.h:854
llvm::RecordKeeper::addClass
void addClass(std::unique_ptr< Record > R)
Definition: Record.h:1935
llvm::orc::SymbolState::Resolved
@ Resolved
Queried, materialization begun.
llvm::BinOpInit::CONCAT
@ CONCAT
Definition: Record.h:851
llvm::VarDefInit::get
static VarDefInit * get(Record *Class, ArrayRef< Init * > Args)
Definition: Record.cpp:1940
llvm::BitsInit
'{ a, b, c }' - Represents an initializer for a BitsRecTy value.
Definition: Record.h:527
llvm::TGLexer::getLoc
SMLoc getLoc() const
Definition: TGLexer.cpp:62
llvm::tgtok::List
@ List
Definition: TGLexer.h:51
TemplateArgs
Definition: ItaniumDemangle.h:1382
llvm::OptimizedStructLayoutField
A field in a structure.
Definition: OptimizedStructLayout.h:45
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::VarDefInit::Fold
Init * Fold() const
Definition: Record.cpp:2035
llvm::RecordKeeper::getDef
Record * getDef(StringRef Name) const
Get the concrete record with the specified name.
Definition: Record.h:1918
llvm::RecordKeeper::addExtraGlobal
void addExtraGlobal(StringRef Name, Init *I)
Definition: Record.h:1949
raw_ostream.h
llvm::RecordVal
This class represents a field in a record, including its name, type, value, and source location.
Definition: Record.h:1485
llvm::tgtok::XTail
@ XTail
Definition: TGLexer.h:57
llvm::tgtok::semi
@ semi
Definition: TGLexer.h:42
llvm::tgtok::TrueVal
@ TrueVal
Definition: TGLexer.h:62
llvm::BitRecTy::get
static BitRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:120
llvm::tgtok::minus
@ minus
Definition: TGLexer.h:37
llvm::tgtok::XLt
@ XLt
Definition: TGLexer.h:58
llvm::IntRecTy::get
static IntRecTy * get(RecordKeeper &RK)
Definition: Record.cpp:159
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::tgtok::colon
@ colon
Definition: TGLexer.h:42
llvm::ListInit::getElement
Init * getElement(unsigned i) const
Definition: Record.h:718
llvm::TGParser::PopLocalScope
void PopLocalScope(TGLocalVarScope *ExpectedStackTop)
Definition: TGParser.h:196
llvm::StringInit::get
static StringInit * get(RecordKeeper &RK, StringRef, StringFormat Fmt=SF_String)
Definition: Record.cpp:596
llvm::tgtok::dotdotdot
@ dotdotdot
Definition: TGLexer.h:46
llvm::TGParser::ParseFile
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
Definition: TGParser.cpp:3835
llvm::tgtok::Id
@ Id
Definition: TGLexer.h:72
llvm::UnsetInit::get
static UnsetInit * get(RecordKeeper &RK)
Get the singleton unset Init.
Definition: Record.cpp:359
llvm::tgtok::XEmpty
@ XEmpty
Definition: TGLexer.h:57
llvm::tgtok::Let
@ Let
Definition: TGLexer.h:51
llvm::TypedInit
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
Definition: Record.h:421