blob: 8ff25a6186bd502794fc3e68400e2cdc559df87c [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"
David Greened34a73b2009-04-24 16:55:41 +000019#include "llvm/Support/Streams.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// Support Code for the Semantic Actions.
24//===----------------------------------------------------------------------===//
25
26namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000027struct SubClassReference {
Chris Lattner1c8ae592009-03-13 16:01:53 +000028 TGLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000029 Record *Rec;
30 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000031 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000032
Chris Lattnerf4601652007-11-22 20:49:04 +000033 bool isInvalid() const { return Rec == 0; }
34};
David Greenede444af2009-04-22 16:42:54 +000035
36struct SubMultiClassReference {
37 TGLoc RefLoc;
38 MultiClass *MC;
39 std::vector<Init*> TemplateArgs;
40 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000041
David Greenede444af2009-04-22 16:42:54 +000042 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000043 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000044};
David Greened34a73b2009-04-24 16:55:41 +000045
46void SubMultiClassReference::dump() const {
47 cerr << "Multiclass:\n";
48
49 MC->dump();
50
51 cerr << "Template args:\n";
52 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
53 iend = TemplateArgs.end();
54 i != iend;
55 ++i) {
56 (*i)->dump();
57 }
58}
59
Chris Lattnerf4601652007-11-22 20:49:04 +000060} // end namespace llvm
61
Chris Lattner1c8ae592009-03-13 16:01:53 +000062bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000063 if (CurRec == 0)
64 CurRec = &CurMultiClass->Rec;
65
66 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
67 // The value already exists in the class, treat this as a set.
68 if (ERV->setValue(RV.getValue()))
69 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
70 RV.getType()->getAsString() + "' is incompatible with " +
71 "previous definition of type '" +
72 ERV->getType()->getAsString() + "'");
73 } else {
74 CurRec->addValue(RV);
75 }
76 return false;
77}
78
79/// SetValue -
80/// Return true on error, false on success.
Chris Lattner1c8ae592009-03-13 16:01:53 +000081bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000082 const std::vector<unsigned> &BitList, Init *V) {
83 if (!V) return false;
84
85 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
86
87 RecordVal *RV = CurRec->getValue(ValName);
88 if (RV == 0)
89 return Error(Loc, "Value '" + ValName + "' unknown!");
90
91 // Do not allow assignments like 'X = X'. This will just cause infinite loops
92 // in the resolution machinery.
93 if (BitList.empty())
94 if (VarInit *VI = dynamic_cast<VarInit*>(V))
95 if (VI->getName() == ValName)
96 return false;
97
98 // If we are assigning to a subset of the bits in the value... then we must be
99 // assigning to a field of BitsRecTy, which must have a BitsInit
100 // initializer.
101 //
102 if (!BitList.empty()) {
103 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
104 if (CurVal == 0)
105 return Error(Loc, "Value '" + ValName + "' is not a bits type");
106
107 // Convert the incoming value to a bits type of the appropriate size...
108 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
109 if (BI == 0) {
110 V->convertInitializerTo(new BitsRecTy(BitList.size()));
111 return Error(Loc, "Initializer is not compatible with bit range");
112 }
113
114 // We should have a BitsInit type now.
115 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
116 assert(BInit != 0);
117
118 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
119
120 // Loop over bits, assigning values as appropriate.
121 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
122 unsigned Bit = BitList[i];
123 if (NewVal->getBit(Bit))
124 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
125 ValName + "' more than once");
126 NewVal->setBit(Bit, BInit->getBit(i));
127 }
128
129 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
130 if (NewVal->getBit(i) == 0)
131 NewVal->setBit(i, CurVal->getBit(i));
132
133 V = NewVal;
134 }
135
136 if (RV->setValue(V))
137 return Error(Loc, "Value '" + ValName + "' of type '" +
138 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000139 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000140 return false;
141}
142
143/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
144/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000145bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000146 Record *SC = SubClass.Rec;
147 // Add all of the values in the subclass into the current class.
148 const std::vector<RecordVal> &Vals = SC->getValues();
149 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
150 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
151 return true;
152
153 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
154
155 // Ensure that an appropriate number of template arguments are specified.
156 if (TArgs.size() < SubClass.TemplateArgs.size())
157 return Error(SubClass.RefLoc, "More template args specified than expected");
158
159 // Loop over all of the template arguments, setting them to the specified
160 // value or leaving them as the default if necessary.
161 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
162 if (i < SubClass.TemplateArgs.size()) {
163 // If a value is specified for this template arg, set it now.
164 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
165 SubClass.TemplateArgs[i]))
166 return true;
167
168 // Resolve it next.
169 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
170
171 // Now remove it.
172 CurRec->removeValue(TArgs[i]);
173
174 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
175 return Error(SubClass.RefLoc,"Value not specified for template argument #"
176 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
177 SC->getName() + "'!");
178 }
179 }
180
181 // Since everything went well, we can now set the "superclass" list for the
182 // current record.
183 const std::vector<Record*> &SCs = SC->getSuperClasses();
184 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
185 if (CurRec->isSubClassOf(SCs[i]))
186 return Error(SubClass.RefLoc,
187 "Already subclass of '" + SCs[i]->getName() + "'!\n");
188 CurRec->addSuperClass(SCs[i]);
189 }
190
191 if (CurRec->isSubClassOf(SC))
192 return Error(SubClass.RefLoc,
193 "Already subclass of '" + SC->getName() + "'!\n");
194 CurRec->addSuperClass(SC);
195 return false;
196}
197
David Greenede444af2009-04-22 16:42:54 +0000198/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000199/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000200/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000201bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000202 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000203 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000204 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000205
Bob Wilson440548d2009-04-30 18:26:19 +0000206 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000207
208 // Add all of the values in the subclass into the current class.
209 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
210 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
211 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
212 return true;
213
Bob Wilson440548d2009-04-30 18:26:19 +0000214 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000215
David Greenede444af2009-04-22 16:42:54 +0000216 // Add all of the defs in the subclass into the current multiclass.
217 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
218 iend = SMC->DefPrototypes.end();
219 i != iend;
220 ++i) {
221 // Clone the def and add it to the current multiclass
222 Record *NewDef = new Record(**i);
223
224 // Add all of the values in the superclass into the current def.
225 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
226 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
227 return true;
228
Bob Wilson440548d2009-04-30 18:26:19 +0000229 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000230 }
Bob Wilson32558652009-04-28 19:41:44 +0000231
David Greenede444af2009-04-22 16:42:54 +0000232 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
233
David Greened34a73b2009-04-24 16:55:41 +0000234 // Ensure that an appropriate number of template arguments are
235 // specified.
David Greenede444af2009-04-22 16:42:54 +0000236 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000237 return Error(SubMultiClass.RefLoc,
238 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000239
David Greenede444af2009-04-22 16:42:54 +0000240 // Loop over all of the template arguments, setting them to the specified
241 // value or leaving them as the default if necessary.
242 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
243 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000244 // If a value is specified for this template arg, set it in the
245 // superclass now.
246 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000247 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000248 SubMultiClass.TemplateArgs[i]))
249 return true;
250
251 // Resolve it next.
252 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000253
David Greenede444af2009-04-22 16:42:54 +0000254 // Now remove it.
255 CurRec->removeValue(SMCTArgs[i]);
256
David Greened34a73b2009-04-24 16:55:41 +0000257 // If a value is specified for this template arg, set it in the
258 // new defs now.
259 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000260 CurMC->DefPrototypes.begin() + newDefStart,
261 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000262 j != jend;
263 ++j) {
264 Record *Def = *j;
265
David Greened34a73b2009-04-24 16:55:41 +0000266 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000267 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000268 SubMultiClass.TemplateArgs[i]))
269 return true;
270
271 // Resolve it next.
272 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
273
274 // Now remove it
275 Def->removeValue(SMCTArgs[i]);
276 }
277 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000278 return Error(SubMultiClass.RefLoc,
279 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000280 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000281 SMC->Rec.getName() + "'!");
282 }
283 }
284
285 return false;
286}
287
Chris Lattnerf4601652007-11-22 20:49:04 +0000288//===----------------------------------------------------------------------===//
289// Parser Code
290//===----------------------------------------------------------------------===//
291
292/// isObjectStart - Return true if this is a valid first token for an Object.
293static bool isObjectStart(tgtok::TokKind K) {
294 return K == tgtok::Class || K == tgtok::Def ||
295 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
296}
297
298/// ParseObjectName - If an object name is specified, return it. Otherwise,
299/// return an anonymous name.
300/// ObjectName ::= ID
301/// ObjectName ::= /*empty*/
302///
303std::string TGParser::ParseObjectName() {
304 if (Lex.getCode() == tgtok::Id) {
305 std::string Ret = Lex.getCurStrVal();
306 Lex.Lex();
307 return Ret;
308 }
309
310 static unsigned AnonCounter = 0;
311 return "anonymous."+utostr(AnonCounter++);
312}
313
314
315/// ParseClassID - Parse and resolve a reference to a class name. This returns
316/// null on error.
317///
318/// ClassID ::= ID
319///
320Record *TGParser::ParseClassID() {
321 if (Lex.getCode() != tgtok::Id) {
322 TokError("expected name for ClassID");
323 return 0;
324 }
325
326 Record *Result = Records.getClass(Lex.getCurStrVal());
327 if (Result == 0)
328 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
329
330 Lex.Lex();
331 return Result;
332}
333
Bob Wilson32558652009-04-28 19:41:44 +0000334/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
335/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000336///
337/// MultiClassID ::= ID
338///
339MultiClass *TGParser::ParseMultiClassID() {
340 if (Lex.getCode() != tgtok::Id) {
341 TokError("expected name for ClassID");
342 return 0;
343 }
Bob Wilson32558652009-04-28 19:41:44 +0000344
David Greenede444af2009-04-22 16:42:54 +0000345 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
346 if (Result == 0)
347 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000348
David Greenede444af2009-04-22 16:42:54 +0000349 Lex.Lex();
350 return Result;
351}
352
Chris Lattnerf4601652007-11-22 20:49:04 +0000353Record *TGParser::ParseDefmID() {
354 if (Lex.getCode() != tgtok::Id) {
355 TokError("expected multiclass name");
356 return 0;
357 }
358
359 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
360 if (MC == 0) {
361 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
362 return 0;
363 }
364
365 Lex.Lex();
366 return &MC->Rec;
367}
368
369
370
371/// ParseSubClassReference - Parse a reference to a subclass or to a templated
372/// subclass. This returns a SubClassRefTy with a null Record* on error.
373///
374/// SubClassRef ::= ClassID
375/// SubClassRef ::= ClassID '<' ValueList '>'
376///
377SubClassReference TGParser::
378ParseSubClassReference(Record *CurRec, bool isDefm) {
379 SubClassReference Result;
380 Result.RefLoc = Lex.getLoc();
381
382 if (isDefm)
383 Result.Rec = ParseDefmID();
384 else
385 Result.Rec = ParseClassID();
386 if (Result.Rec == 0) return Result;
387
388 // If there is no template arg list, we're done.
389 if (Lex.getCode() != tgtok::less)
390 return Result;
391 Lex.Lex(); // Eat the '<'
392
393 if (Lex.getCode() == tgtok::greater) {
394 TokError("subclass reference requires a non-empty list of template values");
395 Result.Rec = 0;
396 return Result;
397 }
398
399 Result.TemplateArgs = ParseValueList(CurRec);
400 if (Result.TemplateArgs.empty()) {
401 Result.Rec = 0; // Error parsing value list.
402 return Result;
403 }
404
405 if (Lex.getCode() != tgtok::greater) {
406 TokError("expected '>' in template value list");
407 Result.Rec = 0;
408 return Result;
409 }
410 Lex.Lex();
411
412 return Result;
413}
414
Bob Wilson32558652009-04-28 19:41:44 +0000415/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
416/// templated submulticlass. This returns a SubMultiClassRefTy with a null
417/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000418///
419/// SubMultiClassRef ::= MultiClassID
420/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
421///
422SubMultiClassReference TGParser::
423ParseSubMultiClassReference(MultiClass *CurMC) {
424 SubMultiClassReference Result;
425 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000426
David Greenede444af2009-04-22 16:42:54 +0000427 Result.MC = ParseMultiClassID();
428 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000429
David Greenede444af2009-04-22 16:42:54 +0000430 // If there is no template arg list, we're done.
431 if (Lex.getCode() != tgtok::less)
432 return Result;
433 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000434
David Greenede444af2009-04-22 16:42:54 +0000435 if (Lex.getCode() == tgtok::greater) {
436 TokError("subclass reference requires a non-empty list of template values");
437 Result.MC = 0;
438 return Result;
439 }
Bob Wilson32558652009-04-28 19:41:44 +0000440
David Greenede444af2009-04-22 16:42:54 +0000441 Result.TemplateArgs = ParseValueList(&CurMC->Rec);
442 if (Result.TemplateArgs.empty()) {
443 Result.MC = 0; // Error parsing value list.
444 return Result;
445 }
Bob Wilson32558652009-04-28 19:41:44 +0000446
David Greenede444af2009-04-22 16:42:54 +0000447 if (Lex.getCode() != tgtok::greater) {
448 TokError("expected '>' in template value list");
449 Result.MC = 0;
450 return Result;
451 }
452 Lex.Lex();
453
454 return Result;
455}
456
Chris Lattnerf4601652007-11-22 20:49:04 +0000457/// ParseRangePiece - Parse a bit/value range.
458/// RangePiece ::= INTVAL
459/// RangePiece ::= INTVAL '-' INTVAL
460/// RangePiece ::= INTVAL INTVAL
461bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000462 if (Lex.getCode() != tgtok::IntVal) {
463 TokError("expected integer or bitrange");
464 return true;
465 }
Dan Gohman63f97202008-10-17 01:33:43 +0000466 int64_t Start = Lex.getCurIntVal();
467 int64_t End;
Chris Lattnerf4601652007-11-22 20:49:04 +0000468
469 if (Start < 0)
470 return TokError("invalid range, cannot be negative");
471
472 switch (Lex.Lex()) { // eat first character.
473 default:
474 Ranges.push_back(Start);
475 return false;
476 case tgtok::minus:
477 if (Lex.Lex() != tgtok::IntVal) {
478 TokError("expected integer value as end of range");
479 return true;
480 }
481 End = Lex.getCurIntVal();
482 break;
483 case tgtok::IntVal:
484 End = -Lex.getCurIntVal();
485 break;
486 }
487 if (End < 0)
488 return TokError("invalid range, cannot be negative");
489 Lex.Lex();
490
491 // Add to the range.
492 if (Start < End) {
493 for (; Start <= End; ++Start)
494 Ranges.push_back(Start);
495 } else {
496 for (; Start >= End; --Start)
497 Ranges.push_back(Start);
498 }
499 return false;
500}
501
502/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
503///
504/// RangeList ::= RangePiece (',' RangePiece)*
505///
506std::vector<unsigned> TGParser::ParseRangeList() {
507 std::vector<unsigned> Result;
508
509 // Parse the first piece.
510 if (ParseRangePiece(Result))
511 return std::vector<unsigned>();
512 while (Lex.getCode() == tgtok::comma) {
513 Lex.Lex(); // Eat the comma.
514
515 // Parse the next range piece.
516 if (ParseRangePiece(Result))
517 return std::vector<unsigned>();
518 }
519 return Result;
520}
521
522/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
523/// OptionalRangeList ::= '<' RangeList '>'
524/// OptionalRangeList ::= /*empty*/
525bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
526 if (Lex.getCode() != tgtok::less)
527 return false;
528
Chris Lattner1c8ae592009-03-13 16:01:53 +0000529 TGLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000530 Lex.Lex(); // eat the '<'
531
532 // Parse the range list.
533 Ranges = ParseRangeList();
534 if (Ranges.empty()) return true;
535
536 if (Lex.getCode() != tgtok::greater) {
537 TokError("expected '>' at end of range list");
538 return Error(StartLoc, "to match this '<'");
539 }
540 Lex.Lex(); // eat the '>'.
541 return false;
542}
543
544/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
545/// OptionalBitList ::= '{' RangeList '}'
546/// OptionalBitList ::= /*empty*/
547bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
548 if (Lex.getCode() != tgtok::l_brace)
549 return false;
550
Chris Lattner1c8ae592009-03-13 16:01:53 +0000551 TGLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000552 Lex.Lex(); // eat the '{'
553
554 // Parse the range list.
555 Ranges = ParseRangeList();
556 if (Ranges.empty()) return true;
557
558 if (Lex.getCode() != tgtok::r_brace) {
559 TokError("expected '}' at end of bit list");
560 return Error(StartLoc, "to match this '{'");
561 }
562 Lex.Lex(); // eat the '}'.
563 return false;
564}
565
566
567/// ParseType - Parse and return a tblgen type. This returns null on error.
568///
569/// Type ::= STRING // string type
570/// Type ::= BIT // bit type
571/// Type ::= BITS '<' INTVAL '>' // bits<x> type
572/// Type ::= INT // int type
573/// Type ::= LIST '<' Type '>' // list<x> type
574/// Type ::= CODE // code type
575/// Type ::= DAG // dag type
576/// Type ::= ClassID // Record Type
577///
578RecTy *TGParser::ParseType() {
579 switch (Lex.getCode()) {
580 default: TokError("Unknown token when expecting a type"); return 0;
581 case tgtok::String: Lex.Lex(); return new StringRecTy();
582 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
583 case tgtok::Int: Lex.Lex(); return new IntRecTy();
584 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
585 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
586 case tgtok::Id:
587 if (Record *R = ParseClassID()) return new RecordRecTy(R);
588 return 0;
589 case tgtok::Bits: {
590 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
591 TokError("expected '<' after bits type");
592 return 0;
593 }
594 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
595 TokError("expected integer in bits<n> type");
596 return 0;
597 }
Dan Gohman63f97202008-10-17 01:33:43 +0000598 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000599 if (Lex.Lex() != tgtok::greater) { // Eat count.
600 TokError("expected '>' at end of bits<n> type");
601 return 0;
602 }
603 Lex.Lex(); // Eat '>'
604 return new BitsRecTy(Val);
605 }
606 case tgtok::List: {
607 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
608 TokError("expected '<' after list type");
609 return 0;
610 }
611 Lex.Lex(); // Eat '<'
612 RecTy *SubType = ParseType();
613 if (SubType == 0) return 0;
614
615 if (Lex.getCode() != tgtok::greater) {
616 TokError("expected '>' at end of list<ty> type");
617 return 0;
618 }
619 Lex.Lex(); // Eat '>'
620 return new ListRecTy(SubType);
621 }
622 }
623}
624
625/// ParseIDValue - Parse an ID as a value and decode what it means.
626///
627/// IDValue ::= ID [def local value]
628/// IDValue ::= ID [def template arg]
629/// IDValue ::= ID [multiclass local value]
630/// IDValue ::= ID [multiclass template argument]
631/// IDValue ::= ID [def name]
632///
633Init *TGParser::ParseIDValue(Record *CurRec) {
634 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
635 std::string Name = Lex.getCurStrVal();
Chris Lattner1c8ae592009-03-13 16:01:53 +0000636 TGLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000637 Lex.Lex();
638 return ParseIDValue(CurRec, Name, Loc);
639}
640
641/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
642/// has already been read.
643Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1c8ae592009-03-13 16:01:53 +0000644 const std::string &Name, TGLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000645 if (CurRec) {
646 if (const RecordVal *RV = CurRec->getValue(Name))
647 return new VarInit(Name, RV->getType());
648
649 std::string TemplateArgName = CurRec->getName()+":"+Name;
650 if (CurRec->isTemplateArg(TemplateArgName)) {
651 const RecordVal *RV = CurRec->getValue(TemplateArgName);
652 assert(RV && "Template arg doesn't exist??");
653 return new VarInit(TemplateArgName, RV->getType());
654 }
655 }
656
657 if (CurMultiClass) {
658 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
659 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
660 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
661 assert(RV && "Template arg doesn't exist??");
662 return new VarInit(MCName, RV->getType());
663 }
664 }
665
666 if (Record *D = Records.getDef(Name))
667 return new DefInit(D);
668
669 Error(NameLoc, "Variable not defined: '" + Name + "'");
670 return 0;
671}
672
David Greened418c1b2009-05-14 20:54:48 +0000673/// ParseOperation - Parse an operator. This returns null on error.
674///
675/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
676///
677Init *TGParser::ParseOperation(Record *CurRec) {
678 switch (Lex.getCode()) {
679 default:
680 TokError("unknown operation");
681 return 0;
682 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000683 case tgtok::XCar:
684 case tgtok::XCdr:
685 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000686 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
687 UnOpInit::UnaryOp Code;
688 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000689
David Greenee6c27de2009-05-14 21:22:49 +0000690 switch (Lex.getCode()) {
691 default: assert(0 && "Unhandled code!");
692 case tgtok::XCast:
693 Lex.Lex(); // eat the operation
694 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000695
David Greenee6c27de2009-05-14 21:22:49 +0000696 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000697
David Greenee6c27de2009-05-14 21:22:49 +0000698 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000699 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000700 return 0;
701 }
David Greened418c1b2009-05-14 20:54:48 +0000702
David Greenee6c27de2009-05-14 21:22:49 +0000703 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000704 case tgtok::XCar:
705 Lex.Lex(); // eat the operation
706 Code = UnOpInit::CAR;
707 break;
708 case tgtok::XCdr:
709 Lex.Lex(); // eat the operation
710 Code = UnOpInit::CDR;
711 break;
712 case tgtok::XNull:
713 Lex.Lex(); // eat the operation
714 Code = UnOpInit::LNULL;
715 Type = new IntRecTy;
716 break;
David Greenee6c27de2009-05-14 21:22:49 +0000717 }
718 if (Lex.getCode() != tgtok::l_paren) {
719 TokError("expected '(' after unary operator");
720 return 0;
721 }
722 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000723
David Greenee6c27de2009-05-14 21:22:49 +0000724 Init *LHS = ParseValue(CurRec);
725 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000726
David Greene5f9f9ba2009-05-14 22:38:31 +0000727 if (Code == UnOpInit::CAR
728 || Code == UnOpInit::CDR
729 || Code == UnOpInit::LNULL) {
730 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
731 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
732 if (LHSl == 0 && LHSt == 0) {
733 TokError("expected list type argument in unary operator");
734 return 0;
735 }
736 if (LHSt) {
737 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
738 if (LType == 0) {
739 TokError("expected list type argumnet in unary operator");
740 return 0;
741 }
742 }
743
744 if (Code == UnOpInit::CAR
745 || Code == UnOpInit::CDR) {
746 if (LHSl && LHSl->getSize() == 0) {
747 TokError("empty list argument in unary operator");
748 return 0;
749 }
750 if (LHSl) {
751 Init *Item = LHSl->getElement(0);
752 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
753 if (Itemt == 0) {
754 TokError("untyped list element in unary operator");
755 return 0;
756 }
757 if (Code == UnOpInit::CAR) {
758 Type = Itemt->getType();
759 }
760 else {
761 Type = new ListRecTy(Itemt->getType());
762 }
763 }
764 else {
765 assert(LHSt && "expected list type argument in unary operator");
766 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
767 if (LType == 0) {
768 TokError("expected list type argumnet in unary operator");
769 return 0;
770 }
771 if (Code == UnOpInit::CAR) {
772 Type = LType->getElementType();
773 }
774 else {
775 Type = LType;
776 }
777 }
778 }
779 }
780
David Greenee6c27de2009-05-14 21:22:49 +0000781 if (Lex.getCode() != tgtok::r_paren) {
782 TokError("expected ')' in unary operator");
783 return 0;
784 }
785 Lex.Lex(); // eat the ')'
786 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
787 }
David Greened418c1b2009-05-14 20:54:48 +0000788
789 case tgtok::XConcat:
790 case tgtok::XSRA:
791 case tgtok::XSRL:
792 case tgtok::XSHL:
793 case tgtok::XStrConcat:
794 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
795 BinOpInit::BinaryOp Code;
796 RecTy *Type = 0;
797
798
799 switch (Lex.getCode()) {
800 default: assert(0 && "Unhandled code!");
801 case tgtok::XConcat:
802 Lex.Lex(); // eat the operation
803 Code = BinOpInit::CONCAT;
804 Type = new DagRecTy();
805 break;
806 case tgtok::XSRA:
807 Lex.Lex(); // eat the operation
808 Code = BinOpInit::SRA;
809 Type = new IntRecTy();
810 break;
811 case tgtok::XSRL:
812 Lex.Lex(); // eat the operation
813 Code = BinOpInit::SRL;
814 Type = new IntRecTy();
815 break;
816 case tgtok::XSHL:
817 Lex.Lex(); // eat the operation
818 Code = BinOpInit::SHL;
819 Type = new IntRecTy();
820 break;
821 case tgtok::XStrConcat:
822 Lex.Lex(); // eat the operation
823 Code = BinOpInit::STRCONCAT;
824 Type = new StringRecTy();
825 break;
826 case tgtok::XNameConcat:
827 Lex.Lex(); // eat the operation
828 Code = BinOpInit::NAMECONCAT;
829
830 Type = ParseOperatorType();
831
832 if (Type == 0) {
833 TokError("did not get type for binary operator");
834 return 0;
835 }
836
837 break;
838 }
839 if (Lex.getCode() != tgtok::l_paren) {
840 TokError("expected '(' after binary operator");
841 return 0;
842 }
843 Lex.Lex(); // eat the '('
844
845 Init *LHS = ParseValue(CurRec);
846 if (LHS == 0) return 0;
847
848 if (Lex.getCode() != tgtok::comma) {
849 TokError("expected ',' in binary operator");
850 return 0;
851 }
852 Lex.Lex(); // eat the ','
853
854 Init *RHS = ParseValue(CurRec);
855 if (RHS == 0) return 0;
856
857 if (Lex.getCode() != tgtok::r_paren) {
858 TokError("expected ')' in binary operator");
859 return 0;
860 }
861 Lex.Lex(); // eat the ')'
862 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
863 }
864
David Greenebeb31a52009-05-14 22:23:47 +0000865 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000866 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
867 TernOpInit::TernaryOp Code;
868 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000869
870
David Greene4afc5092009-05-14 21:54:42 +0000871 tgtok::TokKind LexCode = Lex.getCode();
872 Lex.Lex(); // eat the operation
873 switch (LexCode) {
874 default: assert(0 && "Unhandled code!");
David Greenebeb31a52009-05-14 22:23:47 +0000875 case tgtok::XForEach:
876 Code = TernOpInit::FOREACH;
877 break;
David Greene4afc5092009-05-14 21:54:42 +0000878 case tgtok::XSubst:
879 Code = TernOpInit::SUBST;
880 break;
881 }
882 if (Lex.getCode() != tgtok::l_paren) {
883 TokError("expected '(' after ternary operator");
884 return 0;
885 }
886 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000887
David Greene4afc5092009-05-14 21:54:42 +0000888 Init *LHS = ParseValue(CurRec);
889 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000890
David Greene4afc5092009-05-14 21:54:42 +0000891 if (Lex.getCode() != tgtok::comma) {
892 TokError("expected ',' in ternary operator");
893 return 0;
894 }
895 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000896
David Greene4afc5092009-05-14 21:54:42 +0000897 Init *MHS = ParseValue(CurRec);
898 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000899
David Greene4afc5092009-05-14 21:54:42 +0000900 if (Lex.getCode() != tgtok::comma) {
901 TokError("expected ',' in ternary operator");
902 return 0;
903 }
904 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000905
David Greene4afc5092009-05-14 21:54:42 +0000906 Init *RHS = ParseValue(CurRec);
907 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 if (Lex.getCode() != tgtok::r_paren) {
910 TokError("expected ')' in binary operator");
911 return 0;
912 }
913 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000914
David Greene4afc5092009-05-14 21:54:42 +0000915 switch (LexCode) {
916 default: assert(0 && "Unhandled code!");
David Greenebeb31a52009-05-14 22:23:47 +0000917 case tgtok::XForEach: {
918 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
919 if (MHSt == 0) {
920 TokError("could not get type for !foreach");
921 return 0;
922 }
923 Type = MHSt->getType();
924 break;
925 }
David Greene4afc5092009-05-14 21:54:42 +0000926 case tgtok::XSubst: {
927 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
928 if (RHSt == 0) {
929 TokError("could not get type for !subst");
930 return 0;
931 }
932 Type = RHSt->getType();
933 break;
934 }
935 }
936 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
937 }
David Greened418c1b2009-05-14 20:54:48 +0000938 }
939 TokError("could not parse operation");
940 return 0;
941}
942
943/// ParseOperatorType - Parse a type for an operator. This returns
944/// null on error.
945///
946/// OperatorType ::= '<' Type '>'
947///
948RecTy *TGParser::ParseOperatorType(void) {
949 RecTy *Type = 0;
950
951 if (Lex.getCode() != tgtok::less) {
952 TokError("expected type name for operator");
953 return 0;
954 }
955 Lex.Lex(); // eat the <
956
957 Type = ParseType();
958
959 if (Type == 0) {
960 TokError("expected type name for operator");
961 return 0;
962 }
963
964 if (Lex.getCode() != tgtok::greater) {
965 TokError("expected type name for operator");
966 return 0;
967 }
968 Lex.Lex(); // eat the >
969
970 return Type;
971}
972
973
Chris Lattnerf4601652007-11-22 20:49:04 +0000974/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
975///
976/// SimpleValue ::= IDValue
977/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000978/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +0000979/// SimpleValue ::= CODEFRAGMENT
980/// SimpleValue ::= '?'
981/// SimpleValue ::= '{' ValueList '}'
982/// SimpleValue ::= ID '<' ValueListNE '>'
983/// SimpleValue ::= '[' ValueList ']'
984/// SimpleValue ::= '(' IDValue DagArgList ')'
985/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
986/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
987/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
988/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
989/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
990///
991Init *TGParser::ParseSimpleValue(Record *CurRec) {
992 Init *R = 0;
993 switch (Lex.getCode()) {
994 default: TokError("Unknown token when parsing a value"); break;
995 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +0000996 case tgtok::StrVal: {
997 std::string Val = Lex.getCurStrVal();
998 Lex.Lex();
999
Jim Grosbachda4231f2009-03-26 16:17:51 +00001000 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001001 while (Lex.getCode() == tgtok::StrVal) {
1002 Val += Lex.getCurStrVal();
1003 Lex.Lex();
1004 }
1005
1006 R = new StringInit(Val);
1007 break;
1008 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001009 case tgtok::CodeFragment:
1010 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
1011 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1012 case tgtok::Id: {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001013 TGLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001014 std::string Name = Lex.getCurStrVal();
1015 if (Lex.Lex() != tgtok::less) // consume the Id.
1016 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
1017
1018 // Value ::= ID '<' ValueListNE '>'
1019 if (Lex.Lex() == tgtok::greater) {
1020 TokError("expected non-empty value list");
1021 return 0;
1022 }
1023 std::vector<Init*> ValueList = ParseValueList(CurRec);
1024 if (ValueList.empty()) return 0;
1025
1026 if (Lex.getCode() != tgtok::greater) {
1027 TokError("expected '>' at end of value list");
1028 return 0;
1029 }
1030 Lex.Lex(); // eat the '>'
1031
1032 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1033 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1034 // body.
1035 Record *Class = Records.getClass(Name);
1036 if (!Class) {
1037 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1038 return 0;
1039 }
1040
1041 // Create the new record, set it as CurRec temporarily.
1042 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001043 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001044 SubClassReference SCRef;
1045 SCRef.RefLoc = NameLoc;
1046 SCRef.Rec = Class;
1047 SCRef.TemplateArgs = ValueList;
1048 // Add info about the subclass to NewRec.
1049 if (AddSubClass(NewRec, SCRef))
1050 return 0;
1051 NewRec->resolveReferences();
1052 Records.addDef(NewRec);
1053
1054 // The result of the expression is a reference to the new record.
1055 return new DefInit(NewRec);
1056 }
1057 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1c8ae592009-03-13 16:01:53 +00001058 TGLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001059 Lex.Lex(); // eat the '{'
1060 std::vector<Init*> Vals;
1061
1062 if (Lex.getCode() != tgtok::r_brace) {
1063 Vals = ParseValueList(CurRec);
1064 if (Vals.empty()) return 0;
1065 }
1066 if (Lex.getCode() != tgtok::r_brace) {
1067 TokError("expected '}' at end of bit list value");
1068 return 0;
1069 }
1070 Lex.Lex(); // eat the '}'
1071
1072 BitsInit *Result = new BitsInit(Vals.size());
1073 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1074 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1075 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001076 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1077 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001078 return 0;
1079 }
1080 Result->setBit(Vals.size()-i-1, Bit);
1081 }
1082 return Result;
1083 }
1084 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1085 Lex.Lex(); // eat the '['
1086 std::vector<Init*> Vals;
1087
1088 if (Lex.getCode() != tgtok::r_square) {
1089 Vals = ParseValueList(CurRec);
1090 if (Vals.empty()) return 0;
1091 }
1092 if (Lex.getCode() != tgtok::r_square) {
1093 TokError("expected ']' at end of list value");
1094 return 0;
1095 }
1096 Lex.Lex(); // eat the ']'
1097 return new ListInit(Vals);
1098 }
1099 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1100 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001101 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001102 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001103 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001104 TokError("expected identifier in dag init");
1105 return 0;
1106 }
1107
David Greenec7cafcd2009-04-22 20:18:10 +00001108 Init *Operator = 0;
1109 if (Lex.getCode() == tgtok::Id) {
1110 Operator = ParseIDValue(CurRec);
1111 if (Operator == 0) return 0;
1112 }
1113 else {
David Greened418c1b2009-05-14 20:54:48 +00001114 Operator = ParseOperation(CurRec);
1115 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001116 }
1117
Nate Begeman7cee8172009-03-19 05:21:56 +00001118 // If the operator name is present, parse it.
1119 std::string OperatorName;
1120 if (Lex.getCode() == tgtok::colon) {
1121 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1122 TokError("expected variable name in dag operator");
1123 return 0;
1124 }
1125 OperatorName = Lex.getCurStrVal();
1126 Lex.Lex(); // eat the VarName.
1127 }
1128
Chris Lattnerf4601652007-11-22 20:49:04 +00001129 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1130 if (Lex.getCode() != tgtok::r_paren) {
1131 DagArgs = ParseDagArgList(CurRec);
1132 if (DagArgs.empty()) return 0;
1133 }
1134
1135 if (Lex.getCode() != tgtok::r_paren) {
1136 TokError("expected ')' in dag init");
1137 return 0;
1138 }
1139 Lex.Lex(); // eat the ')'
1140
Nate Begeman7cee8172009-03-19 05:21:56 +00001141 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001142 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001143 }
David Greened418c1b2009-05-14 20:54:48 +00001144
David Greene5f9f9ba2009-05-14 22:38:31 +00001145 case tgtok::XCar:
1146 case tgtok::XCdr:
1147 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001148 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001149 case tgtok::XConcat:
1150 case tgtok::XSRA:
1151 case tgtok::XSRL:
1152 case tgtok::XSHL:
David Greenec7cafcd2009-04-22 20:18:10 +00001153 case tgtok::XStrConcat:
David Greene4afc5092009-05-14 21:54:42 +00001154 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greenebeb31a52009-05-14 22:23:47 +00001155 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001156 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001157 return ParseOperation(CurRec);
1158 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001159 }
1160 }
1161
1162 return R;
1163}
1164
1165/// ParseValue - Parse a tblgen value. This returns null on error.
1166///
1167/// Value ::= SimpleValue ValueSuffix*
1168/// ValueSuffix ::= '{' BitList '}'
1169/// ValueSuffix ::= '[' BitList ']'
1170/// ValueSuffix ::= '.' ID
1171///
1172Init *TGParser::ParseValue(Record *CurRec) {
1173 Init *Result = ParseSimpleValue(CurRec);
1174 if (Result == 0) return 0;
1175
1176 // Parse the suffixes now if present.
1177 while (1) {
1178 switch (Lex.getCode()) {
1179 default: return Result;
1180 case tgtok::l_brace: {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001181 TGLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001182 Lex.Lex(); // eat the '{'
1183 std::vector<unsigned> Ranges = ParseRangeList();
1184 if (Ranges.empty()) return 0;
1185
1186 // Reverse the bitlist.
1187 std::reverse(Ranges.begin(), Ranges.end());
1188 Result = Result->convertInitializerBitRange(Ranges);
1189 if (Result == 0) {
1190 Error(CurlyLoc, "Invalid bit range for value");
1191 return 0;
1192 }
1193
1194 // Eat the '}'.
1195 if (Lex.getCode() != tgtok::r_brace) {
1196 TokError("expected '}' at end of bit range list");
1197 return 0;
1198 }
1199 Lex.Lex();
1200 break;
1201 }
1202 case tgtok::l_square: {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001203 TGLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001204 Lex.Lex(); // eat the '['
1205 std::vector<unsigned> Ranges = ParseRangeList();
1206 if (Ranges.empty()) return 0;
1207
1208 Result = Result->convertInitListSlice(Ranges);
1209 if (Result == 0) {
1210 Error(SquareLoc, "Invalid range for list slice");
1211 return 0;
1212 }
1213
1214 // Eat the ']'.
1215 if (Lex.getCode() != tgtok::r_square) {
1216 TokError("expected ']' at end of list slice");
1217 return 0;
1218 }
1219 Lex.Lex();
1220 break;
1221 }
1222 case tgtok::period:
1223 if (Lex.Lex() != tgtok::Id) { // eat the .
1224 TokError("expected field identifier after '.'");
1225 return 0;
1226 }
1227 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001228 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001229 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001230 return 0;
1231 }
1232 Result = new FieldInit(Result, Lex.getCurStrVal());
1233 Lex.Lex(); // eat field name
1234 break;
1235 }
1236 }
1237}
1238
1239/// ParseDagArgList - Parse the argument list for a dag literal expression.
1240///
1241/// ParseDagArgList ::= Value (':' VARNAME)?
1242/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
1243std::vector<std::pair<llvm::Init*, std::string> >
1244TGParser::ParseDagArgList(Record *CurRec) {
1245 std::vector<std::pair<llvm::Init*, std::string> > Result;
1246
1247 while (1) {
1248 Init *Val = ParseValue(CurRec);
1249 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
1250
1251 // If the variable name is present, add it.
1252 std::string VarName;
1253 if (Lex.getCode() == tgtok::colon) {
1254 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1255 TokError("expected variable name in dag literal");
1256 return std::vector<std::pair<llvm::Init*, std::string> >();
1257 }
1258 VarName = Lex.getCurStrVal();
1259 Lex.Lex(); // eat the VarName.
1260 }
1261
1262 Result.push_back(std::make_pair(Val, VarName));
1263
1264 if (Lex.getCode() != tgtok::comma) break;
1265 Lex.Lex(); // eat the ','
1266 }
1267
1268 return Result;
1269}
1270
1271
1272/// ParseValueList - Parse a comma separated list of values, returning them as a
1273/// vector. Note that this always expects to be able to parse at least one
1274/// value. It returns an empty list if this is not possible.
1275///
1276/// ValueList ::= Value (',' Value)
1277///
1278std::vector<Init*> TGParser::ParseValueList(Record *CurRec) {
1279 std::vector<Init*> Result;
1280 Result.push_back(ParseValue(CurRec));
1281 if (Result.back() == 0) return std::vector<Init*>();
1282
1283 while (Lex.getCode() == tgtok::comma) {
1284 Lex.Lex(); // Eat the comma
1285
1286 Result.push_back(ParseValue(CurRec));
1287 if (Result.back() == 0) return std::vector<Init*>();
1288 }
1289
1290 return Result;
1291}
1292
1293
1294
1295/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1296/// empty string on error. This can happen in a number of different context's,
1297/// including within a def or in the template args for a def (which which case
1298/// CurRec will be non-null) and within the template args for a multiclass (in
1299/// which case CurRec will be null, but CurMultiClass will be set). This can
1300/// also happen within a def that is within a multiclass, which will set both
1301/// CurRec and CurMultiClass.
1302///
1303/// Declaration ::= FIELD? Type ID ('=' Value)?
1304///
1305std::string TGParser::ParseDeclaration(Record *CurRec,
1306 bool ParsingTemplateArgs) {
1307 // Read the field prefix if present.
1308 bool HasField = Lex.getCode() == tgtok::Field;
1309 if (HasField) Lex.Lex();
1310
1311 RecTy *Type = ParseType();
1312 if (Type == 0) return "";
1313
1314 if (Lex.getCode() != tgtok::Id) {
1315 TokError("Expected identifier in declaration");
1316 return "";
1317 }
1318
Chris Lattner1c8ae592009-03-13 16:01:53 +00001319 TGLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001320 std::string DeclName = Lex.getCurStrVal();
1321 Lex.Lex();
1322
1323 if (ParsingTemplateArgs) {
1324 if (CurRec) {
1325 DeclName = CurRec->getName() + ":" + DeclName;
1326 } else {
1327 assert(CurMultiClass);
1328 }
1329 if (CurMultiClass)
1330 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1331 }
1332
1333 // Add the value.
1334 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1335 return "";
1336
1337 // If a value is present, parse it.
1338 if (Lex.getCode() == tgtok::equal) {
1339 Lex.Lex();
Chris Lattner1c8ae592009-03-13 16:01:53 +00001340 TGLoc ValLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001341 Init *Val = ParseValue(CurRec);
1342 if (Val == 0 ||
1343 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1344 return "";
1345 }
1346
1347 return DeclName;
1348}
1349
1350/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1351/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1352/// template args for a def, which may or may not be in a multiclass. If null,
1353/// these are the template args for a multiclass.
1354///
1355/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1356///
1357bool TGParser::ParseTemplateArgList(Record *CurRec) {
1358 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1359 Lex.Lex(); // eat the '<'
1360
1361 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1362
1363 // Read the first declaration.
1364 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1365 if (TemplArg.empty())
1366 return true;
1367
1368 TheRecToAddTo->addTemplateArg(TemplArg);
1369
1370 while (Lex.getCode() == tgtok::comma) {
1371 Lex.Lex(); // eat the ','
1372
1373 // Read the following declarations.
1374 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1375 if (TemplArg.empty())
1376 return true;
1377 TheRecToAddTo->addTemplateArg(TemplArg);
1378 }
1379
1380 if (Lex.getCode() != tgtok::greater)
1381 return TokError("expected '>' at end of template argument list");
1382 Lex.Lex(); // eat the '>'.
1383 return false;
1384}
1385
1386
1387/// ParseBodyItem - Parse a single item at within the body of a def or class.
1388///
1389/// BodyItem ::= Declaration ';'
1390/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1391bool TGParser::ParseBodyItem(Record *CurRec) {
1392 if (Lex.getCode() != tgtok::Let) {
1393 if (ParseDeclaration(CurRec, false).empty())
1394 return true;
1395
1396 if (Lex.getCode() != tgtok::semi)
1397 return TokError("expected ';' after declaration");
1398 Lex.Lex();
1399 return false;
1400 }
1401
1402 // LET ID OptionalRangeList '=' Value ';'
1403 if (Lex.Lex() != tgtok::Id)
1404 return TokError("expected field identifier after let");
1405
Chris Lattner1c8ae592009-03-13 16:01:53 +00001406 TGLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001407 std::string FieldName = Lex.getCurStrVal();
1408 Lex.Lex(); // eat the field name.
1409
1410 std::vector<unsigned> BitList;
1411 if (ParseOptionalBitList(BitList))
1412 return true;
1413 std::reverse(BitList.begin(), BitList.end());
1414
1415 if (Lex.getCode() != tgtok::equal)
1416 return TokError("expected '=' in let expression");
1417 Lex.Lex(); // eat the '='.
1418
1419 Init *Val = ParseValue(CurRec);
1420 if (Val == 0) return true;
1421
1422 if (Lex.getCode() != tgtok::semi)
1423 return TokError("expected ';' after let expression");
1424 Lex.Lex();
1425
1426 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1427}
1428
1429/// ParseBody - Read the body of a class or def. Return true on error, false on
1430/// success.
1431///
1432/// Body ::= ';'
1433/// Body ::= '{' BodyList '}'
1434/// BodyList BodyItem*
1435///
1436bool TGParser::ParseBody(Record *CurRec) {
1437 // If this is a null definition, just eat the semi and return.
1438 if (Lex.getCode() == tgtok::semi) {
1439 Lex.Lex();
1440 return false;
1441 }
1442
1443 if (Lex.getCode() != tgtok::l_brace)
1444 return TokError("Expected ';' or '{' to start body");
1445 // Eat the '{'.
1446 Lex.Lex();
1447
1448 while (Lex.getCode() != tgtok::r_brace)
1449 if (ParseBodyItem(CurRec))
1450 return true;
1451
1452 // Eat the '}'.
1453 Lex.Lex();
1454 return false;
1455}
1456
1457/// ParseObjectBody - Parse the body of a def or class. This consists of an
1458/// optional ClassList followed by a Body. CurRec is the current def or class
1459/// that is being parsed.
1460///
1461/// ObjectBody ::= BaseClassList Body
1462/// BaseClassList ::= /*empty*/
1463/// BaseClassList ::= ':' BaseClassListNE
1464/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1465///
1466bool TGParser::ParseObjectBody(Record *CurRec) {
1467 // If there is a baseclass list, read it.
1468 if (Lex.getCode() == tgtok::colon) {
1469 Lex.Lex();
1470
1471 // Read all of the subclasses.
1472 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1473 while (1) {
1474 // Check for error.
1475 if (SubClass.Rec == 0) return true;
1476
1477 // Add it.
1478 if (AddSubClass(CurRec, SubClass))
1479 return true;
1480
1481 if (Lex.getCode() != tgtok::comma) break;
1482 Lex.Lex(); // eat ','.
1483 SubClass = ParseSubClassReference(CurRec, false);
1484 }
1485 }
1486
1487 // Process any variables on the let stack.
1488 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1489 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1490 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1491 LetStack[i][j].Bits, LetStack[i][j].Value))
1492 return true;
1493
1494 return ParseBody(CurRec);
1495}
1496
1497
1498/// ParseDef - Parse and return a top level or multiclass def, return the record
1499/// corresponding to it. This returns null on error.
1500///
1501/// DefInst ::= DEF ObjectName ObjectBody
1502///
1503llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1c8ae592009-03-13 16:01:53 +00001504 TGLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001505 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1506 Lex.Lex(); // Eat the 'def' token.
1507
1508 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001509 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001510
1511 if (!CurMultiClass) {
1512 // Top-level def definition.
1513
1514 // Ensure redefinition doesn't happen.
1515 if (Records.getDef(CurRec->getName())) {
1516 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1517 return 0;
1518 }
1519 Records.addDef(CurRec);
1520 } else {
1521 // Otherwise, a def inside a multiclass, add it to the multiclass.
1522 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1523 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1524 Error(DefLoc, "def '" + CurRec->getName() +
1525 "' already defined in this multiclass!");
1526 return 0;
1527 }
1528 CurMultiClass->DefPrototypes.push_back(CurRec);
1529 }
1530
1531 if (ParseObjectBody(CurRec))
1532 return 0;
1533
1534 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1535 CurRec->resolveReferences();
1536
1537 // If ObjectBody has template arguments, it's an error.
1538 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1539 return CurRec;
1540}
1541
1542
1543/// ParseClass - Parse a tblgen class definition.
1544///
1545/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1546///
1547bool TGParser::ParseClass() {
1548 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1549 Lex.Lex();
1550
1551 if (Lex.getCode() != tgtok::Id)
1552 return TokError("expected class name after 'class' keyword");
1553
1554 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1555 if (CurRec) {
1556 // If the body was previously defined, this is an error.
1557 if (!CurRec->getValues().empty() ||
1558 !CurRec->getSuperClasses().empty() ||
1559 !CurRec->getTemplateArgs().empty())
1560 return TokError("Class '" + CurRec->getName() + "' already defined");
1561 } else {
1562 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001563 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001564 Records.addClass(CurRec);
1565 }
1566 Lex.Lex(); // eat the name.
1567
1568 // If there are template args, parse them.
1569 if (Lex.getCode() == tgtok::less)
1570 if (ParseTemplateArgList(CurRec))
1571 return true;
1572
1573 // Finally, parse the object body.
1574 return ParseObjectBody(CurRec);
1575}
1576
1577/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1578/// of LetRecords.
1579///
1580/// LetList ::= LetItem (',' LetItem)*
1581/// LetItem ::= ID OptionalRangeList '=' Value
1582///
1583std::vector<LetRecord> TGParser::ParseLetList() {
1584 std::vector<LetRecord> Result;
1585
1586 while (1) {
1587 if (Lex.getCode() != tgtok::Id) {
1588 TokError("expected identifier in let definition");
1589 return std::vector<LetRecord>();
1590 }
1591 std::string Name = Lex.getCurStrVal();
Chris Lattner1c8ae592009-03-13 16:01:53 +00001592 TGLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001593 Lex.Lex(); // Eat the identifier.
1594
1595 // Check for an optional RangeList.
1596 std::vector<unsigned> Bits;
1597 if (ParseOptionalRangeList(Bits))
1598 return std::vector<LetRecord>();
1599 std::reverse(Bits.begin(), Bits.end());
1600
1601 if (Lex.getCode() != tgtok::equal) {
1602 TokError("expected '=' in let expression");
1603 return std::vector<LetRecord>();
1604 }
1605 Lex.Lex(); // eat the '='.
1606
1607 Init *Val = ParseValue(0);
1608 if (Val == 0) return std::vector<LetRecord>();
1609
1610 // Now that we have everything, add the record.
1611 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1612
1613 if (Lex.getCode() != tgtok::comma)
1614 return Result;
1615 Lex.Lex(); // eat the comma.
1616 }
1617}
1618
1619/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1620/// different related productions.
1621///
1622/// Object ::= LET LetList IN '{' ObjectList '}'
1623/// Object ::= LET LetList IN Object
1624///
1625bool TGParser::ParseTopLevelLet() {
1626 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1627 Lex.Lex();
1628
1629 // Add this entry to the let stack.
1630 std::vector<LetRecord> LetInfo = ParseLetList();
1631 if (LetInfo.empty()) return true;
1632 LetStack.push_back(LetInfo);
1633
1634 if (Lex.getCode() != tgtok::In)
1635 return TokError("expected 'in' at end of top-level 'let'");
1636 Lex.Lex();
1637
1638 // If this is a scalar let, just handle it now
1639 if (Lex.getCode() != tgtok::l_brace) {
1640 // LET LetList IN Object
1641 if (ParseObject())
1642 return true;
1643 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1c8ae592009-03-13 16:01:53 +00001644 TGLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001645 // Otherwise, this is a group let.
1646 Lex.Lex(); // eat the '{'.
1647
1648 // Parse the object list.
1649 if (ParseObjectList())
1650 return true;
1651
1652 if (Lex.getCode() != tgtok::r_brace) {
1653 TokError("expected '}' at end of top level let command");
1654 return Error(BraceLoc, "to match this '{'");
1655 }
1656 Lex.Lex();
1657 }
1658
1659 // Outside this let scope, this let block is not active.
1660 LetStack.pop_back();
1661 return false;
1662}
1663
1664/// ParseMultiClassDef - Parse a def in a multiclass context.
1665///
1666/// MultiClassDef ::= DefInst
1667///
1668bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1669 if (Lex.getCode() != tgtok::Def)
1670 return TokError("expected 'def' in multiclass body");
1671
1672 Record *D = ParseDef(CurMC);
1673 if (D == 0) return true;
1674
1675 // Copy the template arguments for the multiclass into the def.
1676 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1677
1678 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1679 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1680 assert(RV && "Template arg doesn't exist?");
1681 D->addValue(*RV);
1682 }
1683
1684 return false;
1685}
1686
1687/// ParseMultiClass - Parse a multiclass definition.
1688///
Bob Wilson32558652009-04-28 19:41:44 +00001689/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1690/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001691///
1692bool TGParser::ParseMultiClass() {
1693 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1694 Lex.Lex(); // Eat the multiclass token.
1695
1696 if (Lex.getCode() != tgtok::Id)
1697 return TokError("expected identifier after multiclass for name");
1698 std::string Name = Lex.getCurStrVal();
1699
1700 if (MultiClasses.count(Name))
1701 return TokError("multiclass '" + Name + "' already defined");
1702
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001703 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001704 Lex.Lex(); // Eat the identifier.
1705
1706 // If there are template args, parse them.
1707 if (Lex.getCode() == tgtok::less)
1708 if (ParseTemplateArgList(0))
1709 return true;
1710
David Greened34a73b2009-04-24 16:55:41 +00001711 bool inherits = false;
1712
David Greenede444af2009-04-22 16:42:54 +00001713 // If there are submulticlasses, parse them.
1714 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001715 inherits = true;
1716
David Greenede444af2009-04-22 16:42:54 +00001717 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001718
David Greenede444af2009-04-22 16:42:54 +00001719 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001720 SubMultiClassReference SubMultiClass =
1721 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001722 while (1) {
1723 // Check for error.
1724 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001725
David Greenede444af2009-04-22 16:42:54 +00001726 // Add it.
1727 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1728 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001729
David Greenede444af2009-04-22 16:42:54 +00001730 if (Lex.getCode() != tgtok::comma) break;
1731 Lex.Lex(); // eat ','.
1732 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1733 }
1734 }
1735
David Greened34a73b2009-04-24 16:55:41 +00001736 if (Lex.getCode() != tgtok::l_brace) {
1737 if (!inherits)
1738 return TokError("expected '{' in multiclass definition");
1739 else
1740 if (Lex.getCode() != tgtok::semi)
1741 return TokError("expected ';' in multiclass definition");
1742 else
1743 Lex.Lex(); // eat the ';'.
1744 }
1745 else {
1746 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1747 return TokError("multiclass must contain at least one def");
Chris Lattnerf4601652007-11-22 20:49:04 +00001748
David Greened34a73b2009-04-24 16:55:41 +00001749 while (Lex.getCode() != tgtok::r_brace)
1750 if (ParseMultiClassDef(CurMultiClass))
1751 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001752
David Greened34a73b2009-04-24 16:55:41 +00001753 Lex.Lex(); // eat the '}'.
1754 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001755
1756 CurMultiClass = 0;
1757 return false;
1758}
1759
1760/// ParseDefm - Parse the instantiation of a multiclass.
1761///
1762/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1763///
1764bool TGParser::ParseDefm() {
1765 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1766 if (Lex.Lex() != tgtok::Id) // eat the defm.
1767 return TokError("expected identifier after defm");
1768
Chris Lattner1c8ae592009-03-13 16:01:53 +00001769 TGLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001770 std::string DefmPrefix = Lex.getCurStrVal();
1771 if (Lex.Lex() != tgtok::colon)
1772 return TokError("expected ':' after defm identifier");
1773
1774 // eat the colon.
1775 Lex.Lex();
1776
Chris Lattner1c8ae592009-03-13 16:01:53 +00001777 TGLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001778 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001779
1780 while (1) {
1781 if (Ref.Rec == 0) return true;
1782
1783 // To instantiate a multiclass, we need to first get the multiclass, then
1784 // instantiate each def contained in the multiclass with the SubClassRef
1785 // template parameters.
1786 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1787 assert(MC && "Didn't lookup multiclass correctly?");
1788 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1789
1790 // Verify that the correct number of template arguments were specified.
1791 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1792 if (TArgs.size() < TemplateVals.size())
1793 return Error(SubClassLoc,
1794 "more template args specified than multiclass expects");
1795
1796 // Loop over all the def's in the multiclass, instantiating each one.
1797 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1798 Record *DefProto = MC->DefPrototypes[i];
1799
David Greene065f2592009-05-05 16:28:25 +00001800 // Add in the defm name
1801 std::string DefName = DefProto->getName();
1802 std::string::size_type idx = DefName.find("#NAME#");
1803 if (idx != std::string::npos) {
1804 DefName.replace(idx, 6, DefmPrefix);
1805 }
1806 else {
1807 // Add the suffix to the defm name to get the new name.
1808 DefName = DefmPrefix + DefName;
1809 }
1810
1811 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001812
1813 SubClassReference Ref;
1814 Ref.RefLoc = DefmPrefixLoc;
1815 Ref.Rec = DefProto;
1816 AddSubClass(CurRec, Ref);
1817
1818 // Loop over all of the template arguments, setting them to the specified
1819 // value or leaving them as the default if necessary.
1820 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001821 // Check if a value is specified for this temp-arg.
1822 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001823 // Set it now.
1824 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1825 TemplateVals[i]))
1826 return true;
1827
1828 // Resolve it next.
1829 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1830
1831 // Now remove it.
1832 CurRec->removeValue(TArgs[i]);
1833
1834 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001835 return Error(SubClassLoc,
1836 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001837 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1838 MC->Rec.getName() + "'");
1839 }
1840 }
1841
1842 // If the mdef is inside a 'let' expression, add to each def.
1843 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1844 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1845 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1846 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1847 Error(DefmPrefixLoc, "when instantiating this defm");
1848 return true;
1849 }
1850
1851 // Ensure redefinition doesn't happen.
1852 if (Records.getDef(CurRec->getName()))
1853 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1854 "' already defined, instantiating defm with subdef '" +
1855 DefProto->getName() + "'");
1856 Records.addDef(CurRec);
1857 CurRec->resolveReferences();
1858 }
1859
1860 if (Lex.getCode() != tgtok::comma) break;
1861 Lex.Lex(); // eat ','.
1862
1863 SubClassLoc = Lex.getLoc();
1864 Ref = ParseSubClassReference(0, true);
1865 }
1866
Chris Lattnerf4601652007-11-22 20:49:04 +00001867 if (Lex.getCode() != tgtok::semi)
1868 return TokError("expected ';' at end of defm");
1869 Lex.Lex();
1870
Chris Lattnerf4601652007-11-22 20:49:04 +00001871 return false;
1872}
1873
1874/// ParseObject
1875/// Object ::= ClassInst
1876/// Object ::= DefInst
1877/// Object ::= MultiClassInst
1878/// Object ::= DefMInst
1879/// Object ::= LETCommand '{' ObjectList '}'
1880/// Object ::= LETCommand Object
1881bool TGParser::ParseObject() {
1882 switch (Lex.getCode()) {
1883 default: assert(0 && "This is not an object");
1884 case tgtok::Let: return ParseTopLevelLet();
1885 case tgtok::Def: return ParseDef(0) == 0;
1886 case tgtok::Defm: return ParseDefm();
1887 case tgtok::Class: return ParseClass();
1888 case tgtok::MultiClass: return ParseMultiClass();
1889 }
1890}
1891
1892/// ParseObjectList
1893/// ObjectList :== Object*
1894bool TGParser::ParseObjectList() {
1895 while (isObjectStart(Lex.getCode())) {
1896 if (ParseObject())
1897 return true;
1898 }
1899 return false;
1900}
1901
1902
1903bool TGParser::ParseFile() {
1904 Lex.Lex(); // Prime the lexer.
1905 if (ParseObjectList()) return true;
1906
1907 // If we have unread input at the end of the file, report it.
1908 if (Lex.getCode() == tgtok::Eof)
1909 return false;
1910
1911 return TokError("Unexpected input at top level");
1912}
1913