LLVM API Documentation

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