blob: 68a1cba3da5f0662ac105d294657cf85d46ea627 [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
Chuck Rose IIIaa917922007-11-26 23:19:59 +000014#include <algorithm>
15
Chris Lattnerf4601652007-11-22 20:49:04 +000016#include "TGParser.h"
17#include "Record.h"
18#include "llvm/ADT/StringExtras.h"
19using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Support Code for the Semantic Actions.
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
26struct MultiClass {
27 Record Rec; // Placeholder for template args and Name.
28 std::vector<Record*> DefPrototypes;
29
30 MultiClass(const std::string &Name) : Rec(Name) {}
31};
32
33struct SubClassReference {
34 TGParser::LocTy RefLoc;
35 Record *Rec;
36 std::vector<Init*> TemplateArgs;
37 SubClassReference() : RefLoc(0), Rec(0) {}
38
39 bool isInvalid() const { return Rec == 0; }
40};
41
42} // end namespace llvm
43
44bool TGParser::AddValue(Record *CurRec, LocTy Loc, const RecordVal &RV) {
45 if (CurRec == 0)
46 CurRec = &CurMultiClass->Rec;
47
48 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
49 // The value already exists in the class, treat this as a set.
50 if (ERV->setValue(RV.getValue()))
51 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
52 RV.getType()->getAsString() + "' is incompatible with " +
53 "previous definition of type '" +
54 ERV->getType()->getAsString() + "'");
55 } else {
56 CurRec->addValue(RV);
57 }
58 return false;
59}
60
61/// SetValue -
62/// Return true on error, false on success.
63bool TGParser::SetValue(Record *CurRec, LocTy Loc, const std::string &ValName,
64 const std::vector<unsigned> &BitList, Init *V) {
65 if (!V) return false;
66
67 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
68
69 RecordVal *RV = CurRec->getValue(ValName);
70 if (RV == 0)
71 return Error(Loc, "Value '" + ValName + "' unknown!");
72
73 // Do not allow assignments like 'X = X'. This will just cause infinite loops
74 // in the resolution machinery.
75 if (BitList.empty())
76 if (VarInit *VI = dynamic_cast<VarInit*>(V))
77 if (VI->getName() == ValName)
78 return false;
79
80 // If we are assigning to a subset of the bits in the value... then we must be
81 // assigning to a field of BitsRecTy, which must have a BitsInit
82 // initializer.
83 //
84 if (!BitList.empty()) {
85 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
86 if (CurVal == 0)
87 return Error(Loc, "Value '" + ValName + "' is not a bits type");
88
89 // Convert the incoming value to a bits type of the appropriate size...
90 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
91 if (BI == 0) {
92 V->convertInitializerTo(new BitsRecTy(BitList.size()));
93 return Error(Loc, "Initializer is not compatible with bit range");
94 }
95
96 // We should have a BitsInit type now.
97 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
98 assert(BInit != 0);
99
100 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
101
102 // Loop over bits, assigning values as appropriate.
103 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
104 unsigned Bit = BitList[i];
105 if (NewVal->getBit(Bit))
106 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
107 ValName + "' more than once");
108 NewVal->setBit(Bit, BInit->getBit(i));
109 }
110
111 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
112 if (NewVal->getBit(i) == 0)
113 NewVal->setBit(i, CurVal->getBit(i));
114
115 V = NewVal;
116 }
117
118 if (RV->setValue(V))
119 return Error(Loc, "Value '" + ValName + "' of type '" +
120 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000121 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000122 return false;
123}
124
125/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
126/// args as SubClass's template arguments.
127bool TGParser::AddSubClass(Record *CurRec, class SubClassReference &SubClass) {
128 Record *SC = SubClass.Rec;
129 // Add all of the values in the subclass into the current class.
130 const std::vector<RecordVal> &Vals = SC->getValues();
131 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
132 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
133 return true;
134
135 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
136
137 // Ensure that an appropriate number of template arguments are specified.
138 if (TArgs.size() < SubClass.TemplateArgs.size())
139 return Error(SubClass.RefLoc, "More template args specified than expected");
140
141 // Loop over all of the template arguments, setting them to the specified
142 // value or leaving them as the default if necessary.
143 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
144 if (i < SubClass.TemplateArgs.size()) {
145 // If a value is specified for this template arg, set it now.
146 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
147 SubClass.TemplateArgs[i]))
148 return true;
149
150 // Resolve it next.
151 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
152
153 // Now remove it.
154 CurRec->removeValue(TArgs[i]);
155
156 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
157 return Error(SubClass.RefLoc,"Value not specified for template argument #"
158 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
159 SC->getName() + "'!");
160 }
161 }
162
163 // Since everything went well, we can now set the "superclass" list for the
164 // current record.
165 const std::vector<Record*> &SCs = SC->getSuperClasses();
166 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
167 if (CurRec->isSubClassOf(SCs[i]))
168 return Error(SubClass.RefLoc,
169 "Already subclass of '" + SCs[i]->getName() + "'!\n");
170 CurRec->addSuperClass(SCs[i]);
171 }
172
173 if (CurRec->isSubClassOf(SC))
174 return Error(SubClass.RefLoc,
175 "Already subclass of '" + SC->getName() + "'!\n");
176 CurRec->addSuperClass(SC);
177 return false;
178}
179
180//===----------------------------------------------------------------------===//
181// Parser Code
182//===----------------------------------------------------------------------===//
183
184/// isObjectStart - Return true if this is a valid first token for an Object.
185static bool isObjectStart(tgtok::TokKind K) {
186 return K == tgtok::Class || K == tgtok::Def ||
187 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
188}
189
190/// ParseObjectName - If an object name is specified, return it. Otherwise,
191/// return an anonymous name.
192/// ObjectName ::= ID
193/// ObjectName ::= /*empty*/
194///
195std::string TGParser::ParseObjectName() {
196 if (Lex.getCode() == tgtok::Id) {
197 std::string Ret = Lex.getCurStrVal();
198 Lex.Lex();
199 return Ret;
200 }
201
202 static unsigned AnonCounter = 0;
203 return "anonymous."+utostr(AnonCounter++);
204}
205
206
207/// ParseClassID - Parse and resolve a reference to a class name. This returns
208/// null on error.
209///
210/// ClassID ::= ID
211///
212Record *TGParser::ParseClassID() {
213 if (Lex.getCode() != tgtok::Id) {
214 TokError("expected name for ClassID");
215 return 0;
216 }
217
218 Record *Result = Records.getClass(Lex.getCurStrVal());
219 if (Result == 0)
220 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
221
222 Lex.Lex();
223 return Result;
224}
225
226Record *TGParser::ParseDefmID() {
227 if (Lex.getCode() != tgtok::Id) {
228 TokError("expected multiclass name");
229 return 0;
230 }
231
232 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
233 if (MC == 0) {
234 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
235 return 0;
236 }
237
238 Lex.Lex();
239 return &MC->Rec;
240}
241
242
243
244/// ParseSubClassReference - Parse a reference to a subclass or to a templated
245/// subclass. This returns a SubClassRefTy with a null Record* on error.
246///
247/// SubClassRef ::= ClassID
248/// SubClassRef ::= ClassID '<' ValueList '>'
249///
250SubClassReference TGParser::
251ParseSubClassReference(Record *CurRec, bool isDefm) {
252 SubClassReference Result;
253 Result.RefLoc = Lex.getLoc();
254
255 if (isDefm)
256 Result.Rec = ParseDefmID();
257 else
258 Result.Rec = ParseClassID();
259 if (Result.Rec == 0) return Result;
260
261 // If there is no template arg list, we're done.
262 if (Lex.getCode() != tgtok::less)
263 return Result;
264 Lex.Lex(); // Eat the '<'
265
266 if (Lex.getCode() == tgtok::greater) {
267 TokError("subclass reference requires a non-empty list of template values");
268 Result.Rec = 0;
269 return Result;
270 }
271
272 Result.TemplateArgs = ParseValueList(CurRec);
273 if (Result.TemplateArgs.empty()) {
274 Result.Rec = 0; // Error parsing value list.
275 return Result;
276 }
277
278 if (Lex.getCode() != tgtok::greater) {
279 TokError("expected '>' in template value list");
280 Result.Rec = 0;
281 return Result;
282 }
283 Lex.Lex();
284
285 return Result;
286}
287
288/// ParseRangePiece - Parse a bit/value range.
289/// RangePiece ::= INTVAL
290/// RangePiece ::= INTVAL '-' INTVAL
291/// RangePiece ::= INTVAL INTVAL
292bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000293 if (Lex.getCode() != tgtok::IntVal) {
294 TokError("expected integer or bitrange");
295 return true;
296 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000297 int Start = Lex.getCurIntVal();
298 int End;
299
300 if (Start < 0)
301 return TokError("invalid range, cannot be negative");
302
303 switch (Lex.Lex()) { // eat first character.
304 default:
305 Ranges.push_back(Start);
306 return false;
307 case tgtok::minus:
308 if (Lex.Lex() != tgtok::IntVal) {
309 TokError("expected integer value as end of range");
310 return true;
311 }
312 End = Lex.getCurIntVal();
313 break;
314 case tgtok::IntVal:
315 End = -Lex.getCurIntVal();
316 break;
317 }
318 if (End < 0)
319 return TokError("invalid range, cannot be negative");
320 Lex.Lex();
321
322 // Add to the range.
323 if (Start < End) {
324 for (; Start <= End; ++Start)
325 Ranges.push_back(Start);
326 } else {
327 for (; Start >= End; --Start)
328 Ranges.push_back(Start);
329 }
330 return false;
331}
332
333/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
334///
335/// RangeList ::= RangePiece (',' RangePiece)*
336///
337std::vector<unsigned> TGParser::ParseRangeList() {
338 std::vector<unsigned> Result;
339
340 // Parse the first piece.
341 if (ParseRangePiece(Result))
342 return std::vector<unsigned>();
343 while (Lex.getCode() == tgtok::comma) {
344 Lex.Lex(); // Eat the comma.
345
346 // Parse the next range piece.
347 if (ParseRangePiece(Result))
348 return std::vector<unsigned>();
349 }
350 return Result;
351}
352
353/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
354/// OptionalRangeList ::= '<' RangeList '>'
355/// OptionalRangeList ::= /*empty*/
356bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
357 if (Lex.getCode() != tgtok::less)
358 return false;
359
360 LocTy StartLoc = Lex.getLoc();
361 Lex.Lex(); // eat the '<'
362
363 // Parse the range list.
364 Ranges = ParseRangeList();
365 if (Ranges.empty()) return true;
366
367 if (Lex.getCode() != tgtok::greater) {
368 TokError("expected '>' at end of range list");
369 return Error(StartLoc, "to match this '<'");
370 }
371 Lex.Lex(); // eat the '>'.
372 return false;
373}
374
375/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
376/// OptionalBitList ::= '{' RangeList '}'
377/// OptionalBitList ::= /*empty*/
378bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
379 if (Lex.getCode() != tgtok::l_brace)
380 return false;
381
382 LocTy StartLoc = Lex.getLoc();
383 Lex.Lex(); // eat the '{'
384
385 // Parse the range list.
386 Ranges = ParseRangeList();
387 if (Ranges.empty()) return true;
388
389 if (Lex.getCode() != tgtok::r_brace) {
390 TokError("expected '}' at end of bit list");
391 return Error(StartLoc, "to match this '{'");
392 }
393 Lex.Lex(); // eat the '}'.
394 return false;
395}
396
397
398/// ParseType - Parse and return a tblgen type. This returns null on error.
399///
400/// Type ::= STRING // string type
401/// Type ::= BIT // bit type
402/// Type ::= BITS '<' INTVAL '>' // bits<x> type
403/// Type ::= INT // int type
404/// Type ::= LIST '<' Type '>' // list<x> type
405/// Type ::= CODE // code type
406/// Type ::= DAG // dag type
407/// Type ::= ClassID // Record Type
408///
409RecTy *TGParser::ParseType() {
410 switch (Lex.getCode()) {
411 default: TokError("Unknown token when expecting a type"); return 0;
412 case tgtok::String: Lex.Lex(); return new StringRecTy();
413 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
414 case tgtok::Int: Lex.Lex(); return new IntRecTy();
415 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
416 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
417 case tgtok::Id:
418 if (Record *R = ParseClassID()) return new RecordRecTy(R);
419 return 0;
420 case tgtok::Bits: {
421 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
422 TokError("expected '<' after bits type");
423 return 0;
424 }
425 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
426 TokError("expected integer in bits<n> type");
427 return 0;
428 }
429 unsigned Val = Lex.getCurIntVal();
430 if (Lex.Lex() != tgtok::greater) { // Eat count.
431 TokError("expected '>' at end of bits<n> type");
432 return 0;
433 }
434 Lex.Lex(); // Eat '>'
435 return new BitsRecTy(Val);
436 }
437 case tgtok::List: {
438 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
439 TokError("expected '<' after list type");
440 return 0;
441 }
442 Lex.Lex(); // Eat '<'
443 RecTy *SubType = ParseType();
444 if (SubType == 0) return 0;
445
446 if (Lex.getCode() != tgtok::greater) {
447 TokError("expected '>' at end of list<ty> type");
448 return 0;
449 }
450 Lex.Lex(); // Eat '>'
451 return new ListRecTy(SubType);
452 }
453 }
454}
455
456/// ParseIDValue - Parse an ID as a value and decode what it means.
457///
458/// IDValue ::= ID [def local value]
459/// IDValue ::= ID [def template arg]
460/// IDValue ::= ID [multiclass local value]
461/// IDValue ::= ID [multiclass template argument]
462/// IDValue ::= ID [def name]
463///
464Init *TGParser::ParseIDValue(Record *CurRec) {
465 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
466 std::string Name = Lex.getCurStrVal();
467 LocTy Loc = Lex.getLoc();
468 Lex.Lex();
469 return ParseIDValue(CurRec, Name, Loc);
470}
471
472/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
473/// has already been read.
474Init *TGParser::ParseIDValue(Record *CurRec,
475 const std::string &Name, LocTy NameLoc) {
476 if (CurRec) {
477 if (const RecordVal *RV = CurRec->getValue(Name))
478 return new VarInit(Name, RV->getType());
479
480 std::string TemplateArgName = CurRec->getName()+":"+Name;
481 if (CurRec->isTemplateArg(TemplateArgName)) {
482 const RecordVal *RV = CurRec->getValue(TemplateArgName);
483 assert(RV && "Template arg doesn't exist??");
484 return new VarInit(TemplateArgName, RV->getType());
485 }
486 }
487
488 if (CurMultiClass) {
489 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
490 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
491 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
492 assert(RV && "Template arg doesn't exist??");
493 return new VarInit(MCName, RV->getType());
494 }
495 }
496
497 if (Record *D = Records.getDef(Name))
498 return new DefInit(D);
499
500 Error(NameLoc, "Variable not defined: '" + Name + "'");
501 return 0;
502}
503
504/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
505///
506/// SimpleValue ::= IDValue
507/// SimpleValue ::= INTVAL
508/// SimpleValue ::= STRVAL
509/// SimpleValue ::= CODEFRAGMENT
510/// SimpleValue ::= '?'
511/// SimpleValue ::= '{' ValueList '}'
512/// SimpleValue ::= ID '<' ValueListNE '>'
513/// SimpleValue ::= '[' ValueList ']'
514/// SimpleValue ::= '(' IDValue DagArgList ')'
515/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
516/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
517/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
518/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
519/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
520///
521Init *TGParser::ParseSimpleValue(Record *CurRec) {
522 Init *R = 0;
523 switch (Lex.getCode()) {
524 default: TokError("Unknown token when parsing a value"); break;
525 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
526 case tgtok::StrVal: R = new StringInit(Lex.getCurStrVal()); Lex.Lex(); break;
527 case tgtok::CodeFragment:
528 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
529 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
530 case tgtok::Id: {
531 LocTy NameLoc = Lex.getLoc();
532 std::string Name = Lex.getCurStrVal();
533 if (Lex.Lex() != tgtok::less) // consume the Id.
534 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
535
536 // Value ::= ID '<' ValueListNE '>'
537 if (Lex.Lex() == tgtok::greater) {
538 TokError("expected non-empty value list");
539 return 0;
540 }
541 std::vector<Init*> ValueList = ParseValueList(CurRec);
542 if (ValueList.empty()) return 0;
543
544 if (Lex.getCode() != tgtok::greater) {
545 TokError("expected '>' at end of value list");
546 return 0;
547 }
548 Lex.Lex(); // eat the '>'
549
550 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
551 // a new anonymous definition, deriving from CLASS<initvalslist> with no
552 // body.
553 Record *Class = Records.getClass(Name);
554 if (!Class) {
555 Error(NameLoc, "Expected a class name, got '" + Name + "'");
556 return 0;
557 }
558
559 // Create the new record, set it as CurRec temporarily.
560 static unsigned AnonCounter = 0;
561 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++));
562 SubClassReference SCRef;
563 SCRef.RefLoc = NameLoc;
564 SCRef.Rec = Class;
565 SCRef.TemplateArgs = ValueList;
566 // Add info about the subclass to NewRec.
567 if (AddSubClass(NewRec, SCRef))
568 return 0;
569 NewRec->resolveReferences();
570 Records.addDef(NewRec);
571
572 // The result of the expression is a reference to the new record.
573 return new DefInit(NewRec);
574 }
575 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
576 LocTy BraceLoc = Lex.getLoc();
577 Lex.Lex(); // eat the '{'
578 std::vector<Init*> Vals;
579
580 if (Lex.getCode() != tgtok::r_brace) {
581 Vals = ParseValueList(CurRec);
582 if (Vals.empty()) return 0;
583 }
584 if (Lex.getCode() != tgtok::r_brace) {
585 TokError("expected '}' at end of bit list value");
586 return 0;
587 }
588 Lex.Lex(); // eat the '}'
589
590 BitsInit *Result = new BitsInit(Vals.size());
591 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
592 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
593 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +0000594 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
595 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +0000596 return 0;
597 }
598 Result->setBit(Vals.size()-i-1, Bit);
599 }
600 return Result;
601 }
602 case tgtok::l_square: { // Value ::= '[' ValueList ']'
603 Lex.Lex(); // eat the '['
604 std::vector<Init*> Vals;
605
606 if (Lex.getCode() != tgtok::r_square) {
607 Vals = ParseValueList(CurRec);
608 if (Vals.empty()) return 0;
609 }
610 if (Lex.getCode() != tgtok::r_square) {
611 TokError("expected ']' at end of list value");
612 return 0;
613 }
614 Lex.Lex(); // eat the ']'
615 return new ListInit(Vals);
616 }
617 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
618 Lex.Lex(); // eat the '('
Chris Lattner3dc2e962008-04-10 04:48:34 +0000619 if (Lex.getCode() != tgtok::Id) {
620 TokError("expected identifier in dag init");
621 return 0;
622 }
623
Chris Lattnerf4601652007-11-22 20:49:04 +0000624 Init *Operator = ParseIDValue(CurRec);
625 if (Operator == 0) return 0;
626
627 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
628 if (Lex.getCode() != tgtok::r_paren) {
629 DagArgs = ParseDagArgList(CurRec);
630 if (DagArgs.empty()) return 0;
631 }
632
633 if (Lex.getCode() != tgtok::r_paren) {
634 TokError("expected ')' in dag init");
635 return 0;
636 }
637 Lex.Lex(); // eat the ')'
638
639 return new DagInit(Operator, DagArgs);
640 }
641 case tgtok::XConcat:
642 case tgtok::XSRA:
643 case tgtok::XSRL:
644 case tgtok::XSHL:
645 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
646 BinOpInit::BinaryOp Code;
647 switch (Lex.getCode()) {
648 default: assert(0 && "Unhandled code!");
649 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
650 case tgtok::XSRA: Code = BinOpInit::SRA; break;
651 case tgtok::XSRL: Code = BinOpInit::SRL; break;
652 case tgtok::XSHL: Code = BinOpInit::SHL; break;
653 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
654 }
655 Lex.Lex(); // eat the operation
656 if (Lex.getCode() != tgtok::l_paren) {
657 TokError("expected '(' after binary operator");
658 return 0;
659 }
660 Lex.Lex(); // eat the '('
661
662 Init *LHS = ParseValue(CurRec);
663 if (LHS == 0) return 0;
664
665 if (Lex.getCode() != tgtok::comma) {
666 TokError("expected ',' in binary operator");
667 return 0;
668 }
669 Lex.Lex(); // eat the ','
670
671 Init *RHS = ParseValue(CurRec);
672 if (RHS == 0) return 0;
673
674 if (Lex.getCode() != tgtok::r_paren) {
675 TokError("expected ')' in binary operator");
676 return 0;
677 }
678 Lex.Lex(); // eat the ')'
679 return (new BinOpInit(Code, LHS, RHS))->Fold();
680 }
681 }
682
683 return R;
684}
685
686/// ParseValue - Parse a tblgen value. This returns null on error.
687///
688/// Value ::= SimpleValue ValueSuffix*
689/// ValueSuffix ::= '{' BitList '}'
690/// ValueSuffix ::= '[' BitList ']'
691/// ValueSuffix ::= '.' ID
692///
693Init *TGParser::ParseValue(Record *CurRec) {
694 Init *Result = ParseSimpleValue(CurRec);
695 if (Result == 0) return 0;
696
697 // Parse the suffixes now if present.
698 while (1) {
699 switch (Lex.getCode()) {
700 default: return Result;
701 case tgtok::l_brace: {
702 LocTy CurlyLoc = Lex.getLoc();
703 Lex.Lex(); // eat the '{'
704 std::vector<unsigned> Ranges = ParseRangeList();
705 if (Ranges.empty()) return 0;
706
707 // Reverse the bitlist.
708 std::reverse(Ranges.begin(), Ranges.end());
709 Result = Result->convertInitializerBitRange(Ranges);
710 if (Result == 0) {
711 Error(CurlyLoc, "Invalid bit range for value");
712 return 0;
713 }
714
715 // Eat the '}'.
716 if (Lex.getCode() != tgtok::r_brace) {
717 TokError("expected '}' at end of bit range list");
718 return 0;
719 }
720 Lex.Lex();
721 break;
722 }
723 case tgtok::l_square: {
724 LocTy SquareLoc = Lex.getLoc();
725 Lex.Lex(); // eat the '['
726 std::vector<unsigned> Ranges = ParseRangeList();
727 if (Ranges.empty()) return 0;
728
729 Result = Result->convertInitListSlice(Ranges);
730 if (Result == 0) {
731 Error(SquareLoc, "Invalid range for list slice");
732 return 0;
733 }
734
735 // Eat the ']'.
736 if (Lex.getCode() != tgtok::r_square) {
737 TokError("expected ']' at end of list slice");
738 return 0;
739 }
740 Lex.Lex();
741 break;
742 }
743 case tgtok::period:
744 if (Lex.Lex() != tgtok::Id) { // eat the .
745 TokError("expected field identifier after '.'");
746 return 0;
747 }
748 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000749 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +0000750 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000751 return 0;
752 }
753 Result = new FieldInit(Result, Lex.getCurStrVal());
754 Lex.Lex(); // eat field name
755 break;
756 }
757 }
758}
759
760/// ParseDagArgList - Parse the argument list for a dag literal expression.
761///
762/// ParseDagArgList ::= Value (':' VARNAME)?
763/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
764std::vector<std::pair<llvm::Init*, std::string> >
765TGParser::ParseDagArgList(Record *CurRec) {
766 std::vector<std::pair<llvm::Init*, std::string> > Result;
767
768 while (1) {
769 Init *Val = ParseValue(CurRec);
770 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
771
772 // If the variable name is present, add it.
773 std::string VarName;
774 if (Lex.getCode() == tgtok::colon) {
775 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
776 TokError("expected variable name in dag literal");
777 return std::vector<std::pair<llvm::Init*, std::string> >();
778 }
779 VarName = Lex.getCurStrVal();
780 Lex.Lex(); // eat the VarName.
781 }
782
783 Result.push_back(std::make_pair(Val, VarName));
784
785 if (Lex.getCode() != tgtok::comma) break;
786 Lex.Lex(); // eat the ','
787 }
788
789 return Result;
790}
791
792
793/// ParseValueList - Parse a comma separated list of values, returning them as a
794/// vector. Note that this always expects to be able to parse at least one
795/// value. It returns an empty list if this is not possible.
796///
797/// ValueList ::= Value (',' Value)
798///
799std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
800 std::vector<Init*> Result;
801 Result.push_back(ParseValue(CurRec));
802 if (Result.back() == 0) return std::vector<Init*>();
803
804 while (Lex.getCode() == tgtok::comma) {
805 Lex.Lex(); // Eat the comma
806
807 Result.push_back(ParseValue(CurRec));
808 if (Result.back() == 0) return std::vector<Init*>();
809 }
810
811 return Result;
812}
813
814
815
816/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
817/// empty string on error. This can happen in a number of different context's,
818/// including within a def or in the template args for a def (which which case
819/// CurRec will be non-null) and within the template args for a multiclass (in
820/// which case CurRec will be null, but CurMultiClass will be set). This can
821/// also happen within a def that is within a multiclass, which will set both
822/// CurRec and CurMultiClass.
823///
824/// Declaration ::= FIELD? Type ID ('=' Value)?
825///
826std::string TGParser::ParseDeclaration(Record *CurRec,
827 bool ParsingTemplateArgs) {
828 // Read the field prefix if present.
829 bool HasField = Lex.getCode() == tgtok::Field;
830 if (HasField) Lex.Lex();
831
832 RecTy *Type = ParseType();
833 if (Type == 0) return "";
834
835 if (Lex.getCode() != tgtok::Id) {
836 TokError("Expected identifier in declaration");
837 return "";
838 }
839
840 LocTy IdLoc = Lex.getLoc();
841 std::string DeclName = Lex.getCurStrVal();
842 Lex.Lex();
843
844 if (ParsingTemplateArgs) {
845 if (CurRec) {
846 DeclName = CurRec->getName() + ":" + DeclName;
847 } else {
848 assert(CurMultiClass);
849 }
850 if (CurMultiClass)
851 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
852 }
853
854 // Add the value.
855 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
856 return "";
857
858 // If a value is present, parse it.
859 if (Lex.getCode() == tgtok::equal) {
860 Lex.Lex();
861 LocTy ValLoc = Lex.getLoc();
862 Init *Val = ParseValue(CurRec);
863 if (Val == 0 ||
864 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
865 return "";
866 }
867
868 return DeclName;
869}
870
871/// ParseTemplateArgList - Read a template argument list, which is a non-empty
872/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
873/// template args for a def, which may or may not be in a multiclass. If null,
874/// these are the template args for a multiclass.
875///
876/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
877///
878bool TGParser::ParseTemplateArgList(Record *CurRec) {
879 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
880 Lex.Lex(); // eat the '<'
881
882 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
883
884 // Read the first declaration.
885 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
886 if (TemplArg.empty())
887 return true;
888
889 TheRecToAddTo->addTemplateArg(TemplArg);
890
891 while (Lex.getCode() == tgtok::comma) {
892 Lex.Lex(); // eat the ','
893
894 // Read the following declarations.
895 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
896 if (TemplArg.empty())
897 return true;
898 TheRecToAddTo->addTemplateArg(TemplArg);
899 }
900
901 if (Lex.getCode() != tgtok::greater)
902 return TokError("expected '>' at end of template argument list");
903 Lex.Lex(); // eat the '>'.
904 return false;
905}
906
907
908/// ParseBodyItem - Parse a single item at within the body of a def or class.
909///
910/// BodyItem ::= Declaration ';'
911/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
912bool TGParser::ParseBodyItem(Record *CurRec) {
913 if (Lex.getCode() != tgtok::Let) {
914 if (ParseDeclaration(CurRec, false).empty())
915 return true;
916
917 if (Lex.getCode() != tgtok::semi)
918 return TokError("expected ';' after declaration");
919 Lex.Lex();
920 return false;
921 }
922
923 // LET ID OptionalRangeList '=' Value ';'
924 if (Lex.Lex() != tgtok::Id)
925 return TokError("expected field identifier after let");
926
927 LocTy IdLoc = Lex.getLoc();
928 std::string FieldName = Lex.getCurStrVal();
929 Lex.Lex(); // eat the field name.
930
931 std::vector<unsigned> BitList;
932 if (ParseOptionalBitList(BitList))
933 return true;
934 std::reverse(BitList.begin(), BitList.end());
935
936 if (Lex.getCode() != tgtok::equal)
937 return TokError("expected '=' in let expression");
938 Lex.Lex(); // eat the '='.
939
940 Init *Val = ParseValue(CurRec);
941 if (Val == 0) return true;
942
943 if (Lex.getCode() != tgtok::semi)
944 return TokError("expected ';' after let expression");
945 Lex.Lex();
946
947 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
948}
949
950/// ParseBody - Read the body of a class or def. Return true on error, false on
951/// success.
952///
953/// Body ::= ';'
954/// Body ::= '{' BodyList '}'
955/// BodyList BodyItem*
956///
957bool TGParser::ParseBody(Record *CurRec) {
958 // If this is a null definition, just eat the semi and return.
959 if (Lex.getCode() == tgtok::semi) {
960 Lex.Lex();
961 return false;
962 }
963
964 if (Lex.getCode() != tgtok::l_brace)
965 return TokError("Expected ';' or '{' to start body");
966 // Eat the '{'.
967 Lex.Lex();
968
969 while (Lex.getCode() != tgtok::r_brace)
970 if (ParseBodyItem(CurRec))
971 return true;
972
973 // Eat the '}'.
974 Lex.Lex();
975 return false;
976}
977
978/// ParseObjectBody - Parse the body of a def or class. This consists of an
979/// optional ClassList followed by a Body. CurRec is the current def or class
980/// that is being parsed.
981///
982/// ObjectBody ::= BaseClassList Body
983/// BaseClassList ::= /*empty*/
984/// BaseClassList ::= ':' BaseClassListNE
985/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
986///
987bool TGParser::ParseObjectBody(Record *CurRec) {
988 // If there is a baseclass list, read it.
989 if (Lex.getCode() == tgtok::colon) {
990 Lex.Lex();
991
992 // Read all of the subclasses.
993 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
994 while (1) {
995 // Check for error.
996 if (SubClass.Rec == 0) return true;
997
998 // Add it.
999 if (AddSubClass(CurRec, SubClass))
1000 return true;
1001
1002 if (Lex.getCode() != tgtok::comma) break;
1003 Lex.Lex(); // eat ','.
1004 SubClass = ParseSubClassReference(CurRec, false);
1005 }
1006 }
1007
1008 // Process any variables on the let stack.
1009 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1010 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1011 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1012 LetStack[i][j].Bits, LetStack[i][j].Value))
1013 return true;
1014
1015 return ParseBody(CurRec);
1016}
1017
1018
1019/// ParseDef - Parse and return a top level or multiclass def, return the record
1020/// corresponding to it. This returns null on error.
1021///
1022/// DefInst ::= DEF ObjectName ObjectBody
1023///
1024llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
1025 LocTy DefLoc = Lex.getLoc();
1026 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1027 Lex.Lex(); // Eat the 'def' token.
1028
1029 // Parse ObjectName and make a record for it.
1030 Record *CurRec = new Record(ParseObjectName());
1031
1032 if (!CurMultiClass) {
1033 // Top-level def definition.
1034
1035 // Ensure redefinition doesn't happen.
1036 if (Records.getDef(CurRec->getName())) {
1037 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1038 return 0;
1039 }
1040 Records.addDef(CurRec);
1041 } else {
1042 // Otherwise, a def inside a multiclass, add it to the multiclass.
1043 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1044 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1045 Error(DefLoc, "def '" + CurRec->getName() +
1046 "' already defined in this multiclass!");
1047 return 0;
1048 }
1049 CurMultiClass->DefPrototypes.push_back(CurRec);
1050 }
1051
1052 if (ParseObjectBody(CurRec))
1053 return 0;
1054
1055 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1056 CurRec->resolveReferences();
1057
1058 // If ObjectBody has template arguments, it's an error.
1059 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1060 return CurRec;
1061}
1062
1063
1064/// ParseClass - Parse a tblgen class definition.
1065///
1066/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1067///
1068bool TGParser::ParseClass() {
1069 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1070 Lex.Lex();
1071
1072 if (Lex.getCode() != tgtok::Id)
1073 return TokError("expected class name after 'class' keyword");
1074
1075 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1076 if (CurRec) {
1077 // If the body was previously defined, this is an error.
1078 if (!CurRec->getValues().empty() ||
1079 !CurRec->getSuperClasses().empty() ||
1080 !CurRec->getTemplateArgs().empty())
1081 return TokError("Class '" + CurRec->getName() + "' already defined");
1082 } else {
1083 // If this is the first reference to this class, create and add it.
1084 CurRec = new Record(Lex.getCurStrVal());
1085 Records.addClass(CurRec);
1086 }
1087 Lex.Lex(); // eat the name.
1088
1089 // If there are template args, parse them.
1090 if (Lex.getCode() == tgtok::less)
1091 if (ParseTemplateArgList(CurRec))
1092 return true;
1093
1094 // Finally, parse the object body.
1095 return ParseObjectBody(CurRec);
1096}
1097
1098/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1099/// of LetRecords.
1100///
1101/// LetList ::= LetItem (',' LetItem)*
1102/// LetItem ::= ID OptionalRangeList '=' Value
1103///
1104std::vector<LetRecord> TGParser::ParseLetList() {
1105 std::vector<LetRecord> Result;
1106
1107 while (1) {
1108 if (Lex.getCode() != tgtok::Id) {
1109 TokError("expected identifier in let definition");
1110 return std::vector<LetRecord>();
1111 }
1112 std::string Name = Lex.getCurStrVal();
1113 LocTy NameLoc = Lex.getLoc();
1114 Lex.Lex(); // Eat the identifier.
1115
1116 // Check for an optional RangeList.
1117 std::vector<unsigned> Bits;
1118 if (ParseOptionalRangeList(Bits))
1119 return std::vector<LetRecord>();
1120 std::reverse(Bits.begin(), Bits.end());
1121
1122 if (Lex.getCode() != tgtok::equal) {
1123 TokError("expected '=' in let expression");
1124 return std::vector<LetRecord>();
1125 }
1126 Lex.Lex(); // eat the '='.
1127
1128 Init *Val = ParseValue(0);
1129 if (Val == 0) return std::vector<LetRecord>();
1130
1131 // Now that we have everything, add the record.
1132 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1133
1134 if (Lex.getCode() != tgtok::comma)
1135 return Result;
1136 Lex.Lex(); // eat the comma.
1137 }
1138}
1139
1140/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1141/// different related productions.
1142///
1143/// Object ::= LET LetList IN '{' ObjectList '}'
1144/// Object ::= LET LetList IN Object
1145///
1146bool TGParser::ParseTopLevelLet() {
1147 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1148 Lex.Lex();
1149
1150 // Add this entry to the let stack.
1151 std::vector<LetRecord> LetInfo = ParseLetList();
1152 if (LetInfo.empty()) return true;
1153 LetStack.push_back(LetInfo);
1154
1155 if (Lex.getCode() != tgtok::In)
1156 return TokError("expected 'in' at end of top-level 'let'");
1157 Lex.Lex();
1158
1159 // If this is a scalar let, just handle it now
1160 if (Lex.getCode() != tgtok::l_brace) {
1161 // LET LetList IN Object
1162 if (ParseObject())
1163 return true;
1164 } else { // Object ::= LETCommand '{' ObjectList '}'
1165 LocTy BraceLoc = Lex.getLoc();
1166 // Otherwise, this is a group let.
1167 Lex.Lex(); // eat the '{'.
1168
1169 // Parse the object list.
1170 if (ParseObjectList())
1171 return true;
1172
1173 if (Lex.getCode() != tgtok::r_brace) {
1174 TokError("expected '}' at end of top level let command");
1175 return Error(BraceLoc, "to match this '{'");
1176 }
1177 Lex.Lex();
1178 }
1179
1180 // Outside this let scope, this let block is not active.
1181 LetStack.pop_back();
1182 return false;
1183}
1184
1185/// ParseMultiClassDef - Parse a def in a multiclass context.
1186///
1187/// MultiClassDef ::= DefInst
1188///
1189bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1190 if (Lex.getCode() != tgtok::Def)
1191 return TokError("expected 'def' in multiclass body");
1192
1193 Record *D = ParseDef(CurMC);
1194 if (D == 0) return true;
1195
1196 // Copy the template arguments for the multiclass into the def.
1197 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1198
1199 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1200 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1201 assert(RV && "Template arg doesn't exist?");
1202 D->addValue(*RV);
1203 }
1204
1205 return false;
1206}
1207
1208/// ParseMultiClass - Parse a multiclass definition.
1209///
1210/// MultiClassInst ::= MULTICLASS ID TemplateArgList? '{' MultiClassDef+ '}'
1211///
1212bool TGParser::ParseMultiClass() {
1213 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1214 Lex.Lex(); // Eat the multiclass token.
1215
1216 if (Lex.getCode() != tgtok::Id)
1217 return TokError("expected identifier after multiclass for name");
1218 std::string Name = Lex.getCurStrVal();
1219
1220 if (MultiClasses.count(Name))
1221 return TokError("multiclass '" + Name + "' already defined");
1222
1223 CurMultiClass = MultiClasses[Name] = new MultiClass(Name);
1224 Lex.Lex(); // Eat the identifier.
1225
1226 // If there are template args, parse them.
1227 if (Lex.getCode() == tgtok::less)
1228 if (ParseTemplateArgList(0))
1229 return true;
1230
1231 if (Lex.getCode() != tgtok::l_brace)
1232 return TokError("expected '{' in multiclass definition");
1233
1234 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1235 return TokError("multiclass must contain at least one def");
1236
1237 while (Lex.getCode() != tgtok::r_brace)
1238 if (ParseMultiClassDef(CurMultiClass))
1239 return true;
1240
1241 Lex.Lex(); // eat the '}'.
1242
1243 CurMultiClass = 0;
1244 return false;
1245}
1246
1247/// ParseDefm - Parse the instantiation of a multiclass.
1248///
1249/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1250///
1251bool TGParser::ParseDefm() {
1252 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1253 if (Lex.Lex() != tgtok::Id) // eat the defm.
1254 return TokError("expected identifier after defm");
1255
1256 LocTy DefmPrefixLoc = Lex.getLoc();
1257 std::string DefmPrefix = Lex.getCurStrVal();
1258 if (Lex.Lex() != tgtok::colon)
1259 return TokError("expected ':' after defm identifier");
1260
1261 // eat the colon.
1262 Lex.Lex();
1263
1264 LocTy SubClassLoc = Lex.getLoc();
1265 SubClassReference Ref = ParseSubClassReference(0, true);
1266 if (Ref.Rec == 0) return true;
1267
1268 if (Lex.getCode() != tgtok::semi)
1269 return TokError("expected ';' at end of defm");
1270 Lex.Lex();
1271
1272 // To instantiate a multiclass, we need to first get the multiclass, then
1273 // instantiate each def contained in the multiclass with the SubClassRef
1274 // template parameters.
1275 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1276 assert(MC && "Didn't lookup multiclass correctly?");
1277 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1278
1279 // Verify that the correct number of template arguments were specified.
1280 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1281 if (TArgs.size() < TemplateVals.size())
1282 return Error(SubClassLoc,
1283 "more template args specified than multiclass expects");
1284
1285 // Loop over all the def's in the multiclass, instantiating each one.
1286 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1287 Record *DefProto = MC->DefPrototypes[i];
1288
1289 // Add the suffix to the defm name to get the new name.
1290 Record *CurRec = new Record(DefmPrefix + DefProto->getName());
1291
1292 SubClassReference Ref;
1293 Ref.RefLoc = DefmPrefixLoc;
1294 Ref.Rec = DefProto;
1295 AddSubClass(CurRec, Ref);
1296
1297 // Loop over all of the template arguments, setting them to the specified
1298 // value or leaving them as the default if necessary.
1299 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1300 if (i < TemplateVals.size()) { // A value is specified for this temp-arg?
1301 // Set it now.
1302 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1303 TemplateVals[i]))
1304 return true;
1305
1306 // Resolve it next.
1307 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1308
1309 // Now remove it.
1310 CurRec->removeValue(TArgs[i]);
1311
1312 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
1313 return Error(SubClassLoc, "value not specified for template argument #"+
1314 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1315 MC->Rec.getName() + "'");
1316 }
1317 }
1318
1319 // If the mdef is inside a 'let' expression, add to each def.
1320 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1321 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1322 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1323 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1324 Error(DefmPrefixLoc, "when instantiating this defm");
1325 return true;
1326 }
1327
1328
1329 // Ensure redefinition doesn't happen.
1330 if (Records.getDef(CurRec->getName()))
1331 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1332 "' already defined, instantiating defm with subdef '" +
1333 DefProto->getName() + "'");
1334 Records.addDef(CurRec);
1335 CurRec->resolveReferences();
1336 }
1337
1338 return false;
1339}
1340
1341/// ParseObject
1342/// Object ::= ClassInst
1343/// Object ::= DefInst
1344/// Object ::= MultiClassInst
1345/// Object ::= DefMInst
1346/// Object ::= LETCommand '{' ObjectList '}'
1347/// Object ::= LETCommand Object
1348bool TGParser::ParseObject() {
1349 switch (Lex.getCode()) {
1350 default: assert(0 && "This is not an object");
1351 case tgtok::Let: return ParseTopLevelLet();
1352 case tgtok::Def: return ParseDef(0) == 0;
1353 case tgtok::Defm: return ParseDefm();
1354 case tgtok::Class: return ParseClass();
1355 case tgtok::MultiClass: return ParseMultiClass();
1356 }
1357}
1358
1359/// ParseObjectList
1360/// ObjectList :== Object*
1361bool TGParser::ParseObjectList() {
1362 while (isObjectStart(Lex.getCode())) {
1363 if (ParseObject())
1364 return true;
1365 }
1366 return false;
1367}
1368
1369
1370bool TGParser::ParseFile() {
1371 Lex.Lex(); // Prime the lexer.
1372 if (ParseObjectList()) return true;
1373
1374 // If we have unread input at the end of the file, report it.
1375 if (Lex.getCode() == tgtok::Eof)
1376 return false;
1377
1378 return TokError("Unexpected input at top level");
1379}
1380