blob: 712226500540cace7394e60fdd2eb0dcf6ded1ce [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
14#include "TGParser.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
Daniel Dunbar1a551802009-07-03 00:10:29 +000017#include <algorithm>
18#include <sstream>
Chris Lattnerf4601652007-11-22 20:49:04 +000019using namespace llvm;
20
21//===----------------------------------------------------------------------===//
22// Support Code for the Semantic Actions.
23//===----------------------------------------------------------------------===//
24
25namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000026struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000027 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000028 Record *Rec;
29 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000030 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000031
Chris Lattnerf4601652007-11-22 20:49:04 +000032 bool isInvalid() const { return Rec == 0; }
33};
David Greenede444af2009-04-22 16:42:54 +000034
35struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000036 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000037 MultiClass *MC;
38 std::vector<Init*> TemplateArgs;
39 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000040
David Greenede444af2009-04-22 16:42:54 +000041 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000042 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000043};
David Greened34a73b2009-04-24 16:55:41 +000044
45void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000046 errs() << "Multiclass:\n";
David Greened34a73b2009-04-24 16:55:41 +000047
48 MC->dump();
49
Daniel Dunbar1a551802009-07-03 00:10:29 +000050 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000051 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
52 iend = TemplateArgs.end();
53 i != iend;
54 ++i) {
55 (*i)->dump();
56 }
57}
58
Chris Lattnerf4601652007-11-22 20:49:04 +000059} // end namespace llvm
60
Chris Lattner1e3a8a42009-06-21 03:39:35 +000061bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000062 if (CurRec == 0)
63 CurRec = &CurMultiClass->Rec;
64
65 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
66 // The value already exists in the class, treat this as a set.
67 if (ERV->setValue(RV.getValue()))
68 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
69 RV.getType()->getAsString() + "' is incompatible with " +
70 "previous definition of type '" +
71 ERV->getType()->getAsString() + "'");
72 } else {
73 CurRec->addValue(RV);
74 }
75 return false;
76}
77
78/// SetValue -
79/// Return true on error, false on success.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000080bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000081 const std::vector<unsigned> &BitList, Init *V) {
82 if (!V) return false;
83
84 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
85
86 RecordVal *RV = CurRec->getValue(ValName);
87 if (RV == 0)
88 return Error(Loc, "Value '" + ValName + "' unknown!");
89
90 // Do not allow assignments like 'X = X'. This will just cause infinite loops
91 // in the resolution machinery.
92 if (BitList.empty())
93 if (VarInit *VI = dynamic_cast<VarInit*>(V))
94 if (VI->getName() == ValName)
95 return false;
96
97 // If we are assigning to a subset of the bits in the value... then we must be
98 // assigning to a field of BitsRecTy, which must have a BitsInit
99 // initializer.
100 //
101 if (!BitList.empty()) {
102 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
103 if (CurVal == 0)
104 return Error(Loc, "Value '" + ValName + "' is not a bits type");
105
106 // Convert the incoming value to a bits type of the appropriate size...
107 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
108 if (BI == 0) {
109 V->convertInitializerTo(new BitsRecTy(BitList.size()));
110 return Error(Loc, "Initializer is not compatible with bit range");
111 }
112
113 // We should have a BitsInit type now.
114 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
115 assert(BInit != 0);
116
117 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
118
119 // Loop over bits, assigning values as appropriate.
120 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
121 unsigned Bit = BitList[i];
122 if (NewVal->getBit(Bit))
123 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
124 ValName + "' more than once");
125 NewVal->setBit(Bit, BInit->getBit(i));
126 }
127
128 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
129 if (NewVal->getBit(i) == 0)
130 NewVal->setBit(i, CurVal->getBit(i));
131
132 V = NewVal;
133 }
134
135 if (RV->setValue(V))
136 return Error(Loc, "Value '" + ValName + "' of type '" +
137 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000138 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000139 return false;
140}
141
142/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
143/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000144bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000145 Record *SC = SubClass.Rec;
146 // Add all of the values in the subclass into the current class.
147 const std::vector<RecordVal> &Vals = SC->getValues();
148 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
149 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
150 return true;
151
152 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
153
154 // Ensure that an appropriate number of template arguments are specified.
155 if (TArgs.size() < SubClass.TemplateArgs.size())
156 return Error(SubClass.RefLoc, "More template args specified than expected");
157
158 // Loop over all of the template arguments, setting them to the specified
159 // value or leaving them as the default if necessary.
160 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
161 if (i < SubClass.TemplateArgs.size()) {
162 // If a value is specified for this template arg, set it now.
163 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
164 SubClass.TemplateArgs[i]))
165 return true;
166
167 // Resolve it next.
168 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
169
170 // Now remove it.
171 CurRec->removeValue(TArgs[i]);
172
173 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
174 return Error(SubClass.RefLoc,"Value not specified for template argument #"
175 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
176 SC->getName() + "'!");
177 }
178 }
179
180 // Since everything went well, we can now set the "superclass" list for the
181 // current record.
182 const std::vector<Record*> &SCs = SC->getSuperClasses();
183 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
184 if (CurRec->isSubClassOf(SCs[i]))
185 return Error(SubClass.RefLoc,
186 "Already subclass of '" + SCs[i]->getName() + "'!\n");
187 CurRec->addSuperClass(SCs[i]);
188 }
189
190 if (CurRec->isSubClassOf(SC))
191 return Error(SubClass.RefLoc,
192 "Already subclass of '" + SC->getName() + "'!\n");
193 CurRec->addSuperClass(SC);
194 return false;
195}
196
David Greenede444af2009-04-22 16:42:54 +0000197/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000198/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000199/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000200bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000201 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000202 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000203 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000204
Bob Wilson440548d2009-04-30 18:26:19 +0000205 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000206
207 // Add all of the values in the subclass into the current class.
208 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
209 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
210 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
211 return true;
212
Bob Wilson440548d2009-04-30 18:26:19 +0000213 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000214
David Greenede444af2009-04-22 16:42:54 +0000215 // Add all of the defs in the subclass into the current multiclass.
216 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
217 iend = SMC->DefPrototypes.end();
218 i != iend;
219 ++i) {
220 // Clone the def and add it to the current multiclass
221 Record *NewDef = new Record(**i);
222
223 // Add all of the values in the superclass into the current def.
224 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
225 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
226 return true;
227
Bob Wilson440548d2009-04-30 18:26:19 +0000228 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000229 }
Bob Wilson32558652009-04-28 19:41:44 +0000230
David Greenede444af2009-04-22 16:42:54 +0000231 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
232
David Greened34a73b2009-04-24 16:55:41 +0000233 // Ensure that an appropriate number of template arguments are
234 // specified.
David Greenede444af2009-04-22 16:42:54 +0000235 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000236 return Error(SubMultiClass.RefLoc,
237 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000238
David Greenede444af2009-04-22 16:42:54 +0000239 // Loop over all of the template arguments, setting them to the specified
240 // value or leaving them as the default if necessary.
241 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
242 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000243 // If a value is specified for this template arg, set it in the
244 // superclass now.
245 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000246 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000247 SubMultiClass.TemplateArgs[i]))
248 return true;
249
250 // Resolve it next.
251 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000252
David Greenede444af2009-04-22 16:42:54 +0000253 // Now remove it.
254 CurRec->removeValue(SMCTArgs[i]);
255
David Greened34a73b2009-04-24 16:55:41 +0000256 // If a value is specified for this template arg, set it in the
257 // new defs now.
258 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000259 CurMC->DefPrototypes.begin() + newDefStart,
260 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000261 j != jend;
262 ++j) {
263 Record *Def = *j;
264
David Greened34a73b2009-04-24 16:55:41 +0000265 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000266 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000267 SubMultiClass.TemplateArgs[i]))
268 return true;
269
270 // Resolve it next.
271 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
272
273 // Now remove it
274 Def->removeValue(SMCTArgs[i]);
275 }
276 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000277 return Error(SubMultiClass.RefLoc,
278 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000279 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000280 SMC->Rec.getName() + "'!");
281 }
282 }
283
284 return false;
285}
286
Chris Lattnerf4601652007-11-22 20:49:04 +0000287//===----------------------------------------------------------------------===//
288// Parser Code
289//===----------------------------------------------------------------------===//
290
291/// isObjectStart - Return true if this is a valid first token for an Object.
292static bool isObjectStart(tgtok::TokKind K) {
293 return K == tgtok::Class || K == tgtok::Def ||
294 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
295}
296
297/// ParseObjectName - If an object name is specified, return it. Otherwise,
298/// return an anonymous name.
299/// ObjectName ::= ID
300/// ObjectName ::= /*empty*/
301///
302std::string TGParser::ParseObjectName() {
303 if (Lex.getCode() == tgtok::Id) {
304 std::string Ret = Lex.getCurStrVal();
305 Lex.Lex();
306 return Ret;
307 }
308
309 static unsigned AnonCounter = 0;
310 return "anonymous."+utostr(AnonCounter++);
311}
312
313
314/// ParseClassID - Parse and resolve a reference to a class name. This returns
315/// null on error.
316///
317/// ClassID ::= ID
318///
319Record *TGParser::ParseClassID() {
320 if (Lex.getCode() != tgtok::Id) {
321 TokError("expected name for ClassID");
322 return 0;
323 }
324
325 Record *Result = Records.getClass(Lex.getCurStrVal());
326 if (Result == 0)
327 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
328
329 Lex.Lex();
330 return Result;
331}
332
Bob Wilson32558652009-04-28 19:41:44 +0000333/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
334/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000335///
336/// MultiClassID ::= ID
337///
338MultiClass *TGParser::ParseMultiClassID() {
339 if (Lex.getCode() != tgtok::Id) {
340 TokError("expected name for ClassID");
341 return 0;
342 }
Bob Wilson32558652009-04-28 19:41:44 +0000343
David Greenede444af2009-04-22 16:42:54 +0000344 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
345 if (Result == 0)
346 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000347
David Greenede444af2009-04-22 16:42:54 +0000348 Lex.Lex();
349 return Result;
350}
351
Chris Lattnerf4601652007-11-22 20:49:04 +0000352Record *TGParser::ParseDefmID() {
353 if (Lex.getCode() != tgtok::Id) {
354 TokError("expected multiclass name");
355 return 0;
356 }
357
358 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
359 if (MC == 0) {
360 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
361 return 0;
362 }
363
364 Lex.Lex();
365 return &MC->Rec;
366}
367
368
369
370/// ParseSubClassReference - Parse a reference to a subclass or to a templated
371/// subclass. This returns a SubClassRefTy with a null Record* on error.
372///
373/// SubClassRef ::= ClassID
374/// SubClassRef ::= ClassID '<' ValueList '>'
375///
376SubClassReference TGParser::
377ParseSubClassReference(Record *CurRec, bool isDefm) {
378 SubClassReference Result;
379 Result.RefLoc = Lex.getLoc();
380
381 if (isDefm)
382 Result.Rec = ParseDefmID();
383 else
384 Result.Rec = ParseClassID();
385 if (Result.Rec == 0) return Result;
386
387 // If there is no template arg list, we're done.
388 if (Lex.getCode() != tgtok::less)
389 return Result;
390 Lex.Lex(); // Eat the '<'
391
392 if (Lex.getCode() == tgtok::greater) {
393 TokError("subclass reference requires a non-empty list of template values");
394 Result.Rec = 0;
395 return Result;
396 }
397
David Greenee1b46912009-06-08 20:23:18 +0000398 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000399 if (Result.TemplateArgs.empty()) {
400 Result.Rec = 0; // Error parsing value list.
401 return Result;
402 }
403
404 if (Lex.getCode() != tgtok::greater) {
405 TokError("expected '>' in template value list");
406 Result.Rec = 0;
407 return Result;
408 }
409 Lex.Lex();
410
411 return Result;
412}
413
Bob Wilson32558652009-04-28 19:41:44 +0000414/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
415/// templated submulticlass. This returns a SubMultiClassRefTy with a null
416/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000417///
418/// SubMultiClassRef ::= MultiClassID
419/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
420///
421SubMultiClassReference TGParser::
422ParseSubMultiClassReference(MultiClass *CurMC) {
423 SubMultiClassReference Result;
424 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000425
David Greenede444af2009-04-22 16:42:54 +0000426 Result.MC = ParseMultiClassID();
427 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000428
David Greenede444af2009-04-22 16:42:54 +0000429 // If there is no template arg list, we're done.
430 if (Lex.getCode() != tgtok::less)
431 return Result;
432 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000433
David Greenede444af2009-04-22 16:42:54 +0000434 if (Lex.getCode() == tgtok::greater) {
435 TokError("subclass reference requires a non-empty list of template values");
436 Result.MC = 0;
437 return Result;
438 }
Bob Wilson32558652009-04-28 19:41:44 +0000439
David Greenee1b46912009-06-08 20:23:18 +0000440 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000441 if (Result.TemplateArgs.empty()) {
442 Result.MC = 0; // Error parsing value list.
443 return Result;
444 }
Bob Wilson32558652009-04-28 19:41:44 +0000445
David Greenede444af2009-04-22 16:42:54 +0000446 if (Lex.getCode() != tgtok::greater) {
447 TokError("expected '>' in template value list");
448 Result.MC = 0;
449 return Result;
450 }
451 Lex.Lex();
452
453 return Result;
454}
455
Chris Lattnerf4601652007-11-22 20:49:04 +0000456/// ParseRangePiece - Parse a bit/value range.
457/// RangePiece ::= INTVAL
458/// RangePiece ::= INTVAL '-' INTVAL
459/// RangePiece ::= INTVAL INTVAL
460bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000461 if (Lex.getCode() != tgtok::IntVal) {
462 TokError("expected integer or bitrange");
463 return true;
464 }
Dan Gohman63f97202008-10-17 01:33:43 +0000465 int64_t Start = Lex.getCurIntVal();
466 int64_t End;
Chris Lattnerf4601652007-11-22 20:49:04 +0000467
468 if (Start < 0)
469 return TokError("invalid range, cannot be negative");
470
471 switch (Lex.Lex()) { // eat first character.
472 default:
473 Ranges.push_back(Start);
474 return false;
475 case tgtok::minus:
476 if (Lex.Lex() != tgtok::IntVal) {
477 TokError("expected integer value as end of range");
478 return true;
479 }
480 End = Lex.getCurIntVal();
481 break;
482 case tgtok::IntVal:
483 End = -Lex.getCurIntVal();
484 break;
485 }
486 if (End < 0)
487 return TokError("invalid range, cannot be negative");
488 Lex.Lex();
489
490 // Add to the range.
491 if (Start < End) {
492 for (; Start <= End; ++Start)
493 Ranges.push_back(Start);
494 } else {
495 for (; Start >= End; --Start)
496 Ranges.push_back(Start);
497 }
498 return false;
499}
500
501/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
502///
503/// RangeList ::= RangePiece (',' RangePiece)*
504///
505std::vector<unsigned> TGParser::ParseRangeList() {
506 std::vector<unsigned> Result;
507
508 // Parse the first piece.
509 if (ParseRangePiece(Result))
510 return std::vector<unsigned>();
511 while (Lex.getCode() == tgtok::comma) {
512 Lex.Lex(); // Eat the comma.
513
514 // Parse the next range piece.
515 if (ParseRangePiece(Result))
516 return std::vector<unsigned>();
517 }
518 return Result;
519}
520
521/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
522/// OptionalRangeList ::= '<' RangeList '>'
523/// OptionalRangeList ::= /*empty*/
524bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
525 if (Lex.getCode() != tgtok::less)
526 return false;
527
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000528 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000529 Lex.Lex(); // eat the '<'
530
531 // Parse the range list.
532 Ranges = ParseRangeList();
533 if (Ranges.empty()) return true;
534
535 if (Lex.getCode() != tgtok::greater) {
536 TokError("expected '>' at end of range list");
537 return Error(StartLoc, "to match this '<'");
538 }
539 Lex.Lex(); // eat the '>'.
540 return false;
541}
542
543/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
544/// OptionalBitList ::= '{' RangeList '}'
545/// OptionalBitList ::= /*empty*/
546bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
547 if (Lex.getCode() != tgtok::l_brace)
548 return false;
549
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000550 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000551 Lex.Lex(); // eat the '{'
552
553 // Parse the range list.
554 Ranges = ParseRangeList();
555 if (Ranges.empty()) return true;
556
557 if (Lex.getCode() != tgtok::r_brace) {
558 TokError("expected '}' at end of bit list");
559 return Error(StartLoc, "to match this '{'");
560 }
561 Lex.Lex(); // eat the '}'.
562 return false;
563}
564
565
566/// ParseType - Parse and return a tblgen type. This returns null on error.
567///
568/// Type ::= STRING // string type
569/// Type ::= BIT // bit type
570/// Type ::= BITS '<' INTVAL '>' // bits<x> type
571/// Type ::= INT // int type
572/// Type ::= LIST '<' Type '>' // list<x> type
573/// Type ::= CODE // code type
574/// Type ::= DAG // dag type
575/// Type ::= ClassID // Record Type
576///
577RecTy *TGParser::ParseType() {
578 switch (Lex.getCode()) {
579 default: TokError("Unknown token when expecting a type"); return 0;
580 case tgtok::String: Lex.Lex(); return new StringRecTy();
581 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
582 case tgtok::Int: Lex.Lex(); return new IntRecTy();
583 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
584 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
585 case tgtok::Id:
586 if (Record *R = ParseClassID()) return new RecordRecTy(R);
587 return 0;
588 case tgtok::Bits: {
589 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
590 TokError("expected '<' after bits type");
591 return 0;
592 }
593 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
594 TokError("expected integer in bits<n> type");
595 return 0;
596 }
Dan Gohman63f97202008-10-17 01:33:43 +0000597 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000598 if (Lex.Lex() != tgtok::greater) { // Eat count.
599 TokError("expected '>' at end of bits<n> type");
600 return 0;
601 }
602 Lex.Lex(); // Eat '>'
603 return new BitsRecTy(Val);
604 }
605 case tgtok::List: {
606 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
607 TokError("expected '<' after list type");
608 return 0;
609 }
610 Lex.Lex(); // Eat '<'
611 RecTy *SubType = ParseType();
612 if (SubType == 0) return 0;
613
614 if (Lex.getCode() != tgtok::greater) {
615 TokError("expected '>' at end of list<ty> type");
616 return 0;
617 }
618 Lex.Lex(); // Eat '>'
619 return new ListRecTy(SubType);
620 }
621 }
622}
623
624/// ParseIDValue - Parse an ID as a value and decode what it means.
625///
626/// IDValue ::= ID [def local value]
627/// IDValue ::= ID [def template arg]
628/// IDValue ::= ID [multiclass local value]
629/// IDValue ::= ID [multiclass template argument]
630/// IDValue ::= ID [def name]
631///
632Init *TGParser::ParseIDValue(Record *CurRec) {
633 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
634 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000635 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000636 Lex.Lex();
637 return ParseIDValue(CurRec, Name, Loc);
638}
639
640/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
641/// has already been read.
642Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000643 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000644 if (CurRec) {
645 if (const RecordVal *RV = CurRec->getValue(Name))
646 return new VarInit(Name, RV->getType());
647
648 std::string TemplateArgName = CurRec->getName()+":"+Name;
649 if (CurRec->isTemplateArg(TemplateArgName)) {
650 const RecordVal *RV = CurRec->getValue(TemplateArgName);
651 assert(RV && "Template arg doesn't exist??");
652 return new VarInit(TemplateArgName, RV->getType());
653 }
654 }
655
656 if (CurMultiClass) {
657 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
658 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
659 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
660 assert(RV && "Template arg doesn't exist??");
661 return new VarInit(MCName, RV->getType());
662 }
663 }
664
665 if (Record *D = Records.getDef(Name))
666 return new DefInit(D);
667
668 Error(NameLoc, "Variable not defined: '" + Name + "'");
669 return 0;
670}
671
David Greened418c1b2009-05-14 20:54:48 +0000672/// ParseOperation - Parse an operator. This returns null on error.
673///
674/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
675///
676Init *TGParser::ParseOperation(Record *CurRec) {
677 switch (Lex.getCode()) {
678 default:
679 TokError("unknown operation");
680 return 0;
681 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000682 case tgtok::XCar:
683 case tgtok::XCdr:
684 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000685 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
686 UnOpInit::UnaryOp Code;
687 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000688
David Greenee6c27de2009-05-14 21:22:49 +0000689 switch (Lex.getCode()) {
690 default: assert(0 && "Unhandled code!");
691 case tgtok::XCast:
692 Lex.Lex(); // eat the operation
693 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000694
David Greenee6c27de2009-05-14 21:22:49 +0000695 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000696
David Greenee6c27de2009-05-14 21:22:49 +0000697 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000698 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000699 return 0;
700 }
David Greened418c1b2009-05-14 20:54:48 +0000701
David Greenee6c27de2009-05-14 21:22:49 +0000702 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000703 case tgtok::XCar:
704 Lex.Lex(); // eat the operation
705 Code = UnOpInit::CAR;
706 break;
707 case tgtok::XCdr:
708 Lex.Lex(); // eat the operation
709 Code = UnOpInit::CDR;
710 break;
711 case tgtok::XNull:
712 Lex.Lex(); // eat the operation
713 Code = UnOpInit::LNULL;
714 Type = new IntRecTy;
715 break;
David Greenee6c27de2009-05-14 21:22:49 +0000716 }
717 if (Lex.getCode() != tgtok::l_paren) {
718 TokError("expected '(' after unary operator");
719 return 0;
720 }
721 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000722
David Greenee6c27de2009-05-14 21:22:49 +0000723 Init *LHS = ParseValue(CurRec);
724 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000725
David Greene5f9f9ba2009-05-14 22:38:31 +0000726 if (Code == UnOpInit::CAR
727 || Code == UnOpInit::CDR
728 || Code == UnOpInit::LNULL) {
729 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000730 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000731 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000732 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
733 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000734 return 0;
735 }
736 if (LHSt) {
737 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000738 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
739 if (LType == 0 && SType == 0) {
740 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000741 return 0;
742 }
743 }
744
745 if (Code == UnOpInit::CAR
746 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000747 if (LHSl == 0 && LHSt == 0) {
748 TokError("expected list type argumnet in unary operator");
749 return 0;
750 }
751
David Greene5f9f9ba2009-05-14 22:38:31 +0000752 if (LHSl && LHSl->getSize() == 0) {
753 TokError("empty list argument in unary operator");
754 return 0;
755 }
756 if (LHSl) {
757 Init *Item = LHSl->getElement(0);
758 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
759 if (Itemt == 0) {
760 TokError("untyped list element in unary operator");
761 return 0;
762 }
763 if (Code == UnOpInit::CAR) {
764 Type = Itemt->getType();
765 }
766 else {
767 Type = new ListRecTy(Itemt->getType());
768 }
769 }
770 else {
771 assert(LHSt && "expected list type argument in unary operator");
772 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
773 if (LType == 0) {
774 TokError("expected list type argumnet in unary operator");
775 return 0;
776 }
777 if (Code == UnOpInit::CAR) {
778 Type = LType->getElementType();
779 }
780 else {
781 Type = LType;
782 }
783 }
784 }
785 }
786
David Greenee6c27de2009-05-14 21:22:49 +0000787 if (Lex.getCode() != tgtok::r_paren) {
788 TokError("expected ')' in unary operator");
789 return 0;
790 }
791 Lex.Lex(); // eat the ')'
792 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
793 }
David Greened418c1b2009-05-14 20:54:48 +0000794
795 case tgtok::XConcat:
796 case tgtok::XSRA:
797 case tgtok::XSRL:
798 case tgtok::XSHL:
799 case tgtok::XStrConcat:
800 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
801 BinOpInit::BinaryOp Code;
802 RecTy *Type = 0;
803
804
805 switch (Lex.getCode()) {
806 default: assert(0 && "Unhandled code!");
807 case tgtok::XConcat:
808 Lex.Lex(); // eat the operation
809 Code = BinOpInit::CONCAT;
810 Type = new DagRecTy();
811 break;
812 case tgtok::XSRA:
813 Lex.Lex(); // eat the operation
814 Code = BinOpInit::SRA;
815 Type = new IntRecTy();
816 break;
817 case tgtok::XSRL:
818 Lex.Lex(); // eat the operation
819 Code = BinOpInit::SRL;
820 Type = new IntRecTy();
821 break;
822 case tgtok::XSHL:
823 Lex.Lex(); // eat the operation
824 Code = BinOpInit::SHL;
825 Type = new IntRecTy();
826 break;
827 case tgtok::XStrConcat:
828 Lex.Lex(); // eat the operation
829 Code = BinOpInit::STRCONCAT;
830 Type = new StringRecTy();
831 break;
832 case tgtok::XNameConcat:
833 Lex.Lex(); // eat the operation
834 Code = BinOpInit::NAMECONCAT;
835
836 Type = ParseOperatorType();
837
838 if (Type == 0) {
839 TokError("did not get type for binary operator");
840 return 0;
841 }
842
843 break;
844 }
845 if (Lex.getCode() != tgtok::l_paren) {
846 TokError("expected '(' after binary operator");
847 return 0;
848 }
849 Lex.Lex(); // eat the '('
850
851 Init *LHS = ParseValue(CurRec);
852 if (LHS == 0) return 0;
853
854 if (Lex.getCode() != tgtok::comma) {
855 TokError("expected ',' in binary operator");
856 return 0;
857 }
858 Lex.Lex(); // eat the ','
859
860 Init *RHS = ParseValue(CurRec);
861 if (RHS == 0) return 0;
862
863 if (Lex.getCode() != tgtok::r_paren) {
864 TokError("expected ')' in binary operator");
865 return 0;
866 }
867 Lex.Lex(); // eat the ')'
868 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
869 }
870
David Greene9bea7c82009-05-14 23:26:46 +0000871 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000872 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000873 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
874 TernOpInit::TernaryOp Code;
875 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000876
877
David Greene4afc5092009-05-14 21:54:42 +0000878 tgtok::TokKind LexCode = Lex.getCode();
879 Lex.Lex(); // eat the operation
880 switch (LexCode) {
881 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000882 case tgtok::XIf:
883 Code = TernOpInit::IF;
884 break;
David Greenebeb31a52009-05-14 22:23:47 +0000885 case tgtok::XForEach:
886 Code = TernOpInit::FOREACH;
887 break;
David Greene4afc5092009-05-14 21:54:42 +0000888 case tgtok::XSubst:
889 Code = TernOpInit::SUBST;
890 break;
891 }
892 if (Lex.getCode() != tgtok::l_paren) {
893 TokError("expected '(' after ternary operator");
894 return 0;
895 }
896 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000897
David Greene4afc5092009-05-14 21:54:42 +0000898 Init *LHS = ParseValue(CurRec);
899 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000900
David Greene4afc5092009-05-14 21:54:42 +0000901 if (Lex.getCode() != tgtok::comma) {
902 TokError("expected ',' in ternary operator");
903 return 0;
904 }
905 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000906
David Greene4afc5092009-05-14 21:54:42 +0000907 Init *MHS = ParseValue(CurRec);
908 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000909
David Greene4afc5092009-05-14 21:54:42 +0000910 if (Lex.getCode() != tgtok::comma) {
911 TokError("expected ',' in ternary operator");
912 return 0;
913 }
914 Lex.Lex(); // eat the ','
David Greened418c1b2009-05-14 20:54:48 +0000915
David Greene4afc5092009-05-14 21:54:42 +0000916 Init *RHS = ParseValue(CurRec);
917 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000918
David Greene4afc5092009-05-14 21:54:42 +0000919 if (Lex.getCode() != tgtok::r_paren) {
920 TokError("expected ')' in binary operator");
921 return 0;
922 }
923 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000924
David Greene4afc5092009-05-14 21:54:42 +0000925 switch (LexCode) {
926 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000927 case tgtok::XIf: {
928 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
929 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
930 if (MHSt == 0 || RHSt == 0) {
931 TokError("could not get type for !if");
932 return 0;
933 }
934 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
935 Type = RHSt->getType();
936 }
937 else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
938 Type = MHSt->getType();
939 }
940 else {
941 TokError("inconsistent types for !if");
942 return 0;
943 }
944 break;
945 }
David Greenebeb31a52009-05-14 22:23:47 +0000946 case tgtok::XForEach: {
947 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
948 if (MHSt == 0) {
949 TokError("could not get type for !foreach");
950 return 0;
951 }
952 Type = MHSt->getType();
953 break;
954 }
David Greene4afc5092009-05-14 21:54:42 +0000955 case tgtok::XSubst: {
956 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
957 if (RHSt == 0) {
958 TokError("could not get type for !subst");
959 return 0;
960 }
961 Type = RHSt->getType();
962 break;
963 }
964 }
965 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
966 }
David Greened418c1b2009-05-14 20:54:48 +0000967 }
968 TokError("could not parse operation");
969 return 0;
970}
971
972/// ParseOperatorType - Parse a type for an operator. This returns
973/// null on error.
974///
975/// OperatorType ::= '<' Type '>'
976///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000977RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000978 RecTy *Type = 0;
979
980 if (Lex.getCode() != tgtok::less) {
981 TokError("expected type name for operator");
982 return 0;
983 }
984 Lex.Lex(); // eat the <
985
986 Type = ParseType();
987
988 if (Type == 0) {
989 TokError("expected type name for operator");
990 return 0;
991 }
992
993 if (Lex.getCode() != tgtok::greater) {
994 TokError("expected type name for operator");
995 return 0;
996 }
997 Lex.Lex(); // eat the >
998
999 return Type;
1000}
1001
1002
Chris Lattnerf4601652007-11-22 20:49:04 +00001003/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1004///
1005/// SimpleValue ::= IDValue
1006/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001007/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001008/// SimpleValue ::= CODEFRAGMENT
1009/// SimpleValue ::= '?'
1010/// SimpleValue ::= '{' ValueList '}'
1011/// SimpleValue ::= ID '<' ValueListNE '>'
1012/// SimpleValue ::= '[' ValueList ']'
1013/// SimpleValue ::= '(' IDValue DagArgList ')'
1014/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1015/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1016/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1017/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1018/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1019///
David Greenee1b46912009-06-08 20:23:18 +00001020Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001021 Init *R = 0;
1022 switch (Lex.getCode()) {
1023 default: TokError("Unknown token when parsing a value"); break;
1024 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001025 case tgtok::StrVal: {
1026 std::string Val = Lex.getCurStrVal();
1027 Lex.Lex();
1028
Jim Grosbachda4231f2009-03-26 16:17:51 +00001029 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001030 while (Lex.getCode() == tgtok::StrVal) {
1031 Val += Lex.getCurStrVal();
1032 Lex.Lex();
1033 }
1034
1035 R = new StringInit(Val);
1036 break;
1037 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001038 case tgtok::CodeFragment:
1039 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
1040 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1041 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001042 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001043 std::string Name = Lex.getCurStrVal();
1044 if (Lex.Lex() != tgtok::less) // consume the Id.
1045 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
1046
1047 // Value ::= ID '<' ValueListNE '>'
1048 if (Lex.Lex() == tgtok::greater) {
1049 TokError("expected non-empty value list");
1050 return 0;
1051 }
David Greenee1b46912009-06-08 20:23:18 +00001052
Chris Lattnerf4601652007-11-22 20:49:04 +00001053 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1054 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1055 // body.
1056 Record *Class = Records.getClass(Name);
1057 if (!Class) {
1058 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1059 return 0;
1060 }
David Greenee1b46912009-06-08 20:23:18 +00001061
1062 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1063 if (ValueList.empty()) return 0;
1064
1065 if (Lex.getCode() != tgtok::greater) {
1066 TokError("expected '>' at end of value list");
1067 return 0;
1068 }
1069 Lex.Lex(); // eat the '>'
Chris Lattnerf4601652007-11-22 20:49:04 +00001070
1071 // Create the new record, set it as CurRec temporarily.
1072 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001073 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001074 SubClassReference SCRef;
1075 SCRef.RefLoc = NameLoc;
1076 SCRef.Rec = Class;
1077 SCRef.TemplateArgs = ValueList;
1078 // Add info about the subclass to NewRec.
1079 if (AddSubClass(NewRec, SCRef))
1080 return 0;
1081 NewRec->resolveReferences();
1082 Records.addDef(NewRec);
1083
1084 // The result of the expression is a reference to the new record.
1085 return new DefInit(NewRec);
1086 }
1087 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001088 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001089 Lex.Lex(); // eat the '{'
1090 std::vector<Init*> Vals;
1091
1092 if (Lex.getCode() != tgtok::r_brace) {
1093 Vals = ParseValueList(CurRec);
1094 if (Vals.empty()) return 0;
1095 }
1096 if (Lex.getCode() != tgtok::r_brace) {
1097 TokError("expected '}' at end of bit list value");
1098 return 0;
1099 }
1100 Lex.Lex(); // eat the '}'
1101
1102 BitsInit *Result = new BitsInit(Vals.size());
1103 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1104 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1105 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001106 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1107 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001108 return 0;
1109 }
1110 Result->setBit(Vals.size()-i-1, Bit);
1111 }
1112 return Result;
1113 }
1114 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1115 Lex.Lex(); // eat the '['
1116 std::vector<Init*> Vals;
1117
David Greenee1b46912009-06-08 20:23:18 +00001118 RecTy *DeducedEltTy = 0;
1119 ListRecTy *GivenListTy = 0;
1120
1121 if (ItemType != 0) {
1122 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1123 if (ListType == 0) {
1124 std::stringstream s;
1125 s << "Type mismatch for list, expected list type, got "
1126 << ItemType->getAsString();
1127 TokError(s.str());
1128 }
1129 GivenListTy = ListType;
1130 }
1131
Chris Lattnerf4601652007-11-22 20:49:04 +00001132 if (Lex.getCode() != tgtok::r_square) {
David Greenee1b46912009-06-08 20:23:18 +00001133 Vals = ParseValueList(CurRec, 0, GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001134 if (Vals.empty()) return 0;
1135 }
1136 if (Lex.getCode() != tgtok::r_square) {
1137 TokError("expected ']' at end of list value");
1138 return 0;
1139 }
1140 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001141
1142 RecTy *GivenEltTy = 0;
1143 if (Lex.getCode() == tgtok::less) {
1144 // Optional list element type
1145 Lex.Lex(); // eat the '<'
1146
1147 GivenEltTy = ParseType();
1148 if (GivenEltTy == 0) {
1149 // Couldn't parse element type
1150 return 0;
1151 }
1152
1153 if (Lex.getCode() != tgtok::greater) {
1154 TokError("expected '>' at end of list element type");
1155 return 0;
1156 }
1157 Lex.Lex(); // eat the '>'
1158 }
1159
1160 // Check elements
1161 RecTy *EltTy = 0;
1162 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1163 i != ie;
1164 ++i) {
1165 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1166 if (TArg == 0) {
1167 TokError("Untyped list element");
1168 return 0;
1169 }
1170 if (EltTy != 0) {
1171 EltTy = resolveTypes(EltTy, TArg->getType());
1172 if (EltTy == 0) {
1173 TokError("Incompatible types in list elements");
1174 return 0;
1175 }
1176 }
1177 else {
1178 EltTy = TArg->getType();
1179 }
1180 }
1181
1182 if (GivenEltTy != 0) {
1183 if (EltTy != 0) {
1184 // Verify consistency
1185 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1186 TokError("Incompatible types in list elements");
1187 return 0;
1188 }
1189 }
1190 EltTy = GivenEltTy;
1191 }
1192
1193 if (EltTy == 0) {
1194 if (ItemType == 0) {
1195 TokError("No type for list");
1196 return 0;
1197 }
1198 DeducedEltTy = GivenListTy->getElementType();
1199 }
1200 else {
1201 // Make sure the deduced type is compatible with the given type
1202 if (GivenListTy) {
1203 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1204 TokError("Element type mismatch for list");
1205 return 0;
1206 }
1207 }
1208 DeducedEltTy = EltTy;
1209 }
1210
1211 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001212 }
1213 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1214 Lex.Lex(); // eat the '('
David Greenec7cafcd2009-04-22 20:18:10 +00001215 if (Lex.getCode() != tgtok::Id
David Greenee6c27de2009-05-14 21:22:49 +00001216 && Lex.getCode() != tgtok::XCast
David Greenec7cafcd2009-04-22 20:18:10 +00001217 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001218 TokError("expected identifier in dag init");
1219 return 0;
1220 }
1221
David Greenec7cafcd2009-04-22 20:18:10 +00001222 Init *Operator = 0;
1223 if (Lex.getCode() == tgtok::Id) {
1224 Operator = ParseIDValue(CurRec);
1225 if (Operator == 0) return 0;
1226 }
1227 else {
David Greened418c1b2009-05-14 20:54:48 +00001228 Operator = ParseOperation(CurRec);
1229 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001230 }
1231
Nate Begeman7cee8172009-03-19 05:21:56 +00001232 // If the operator name is present, parse it.
1233 std::string OperatorName;
1234 if (Lex.getCode() == tgtok::colon) {
1235 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1236 TokError("expected variable name in dag operator");
1237 return 0;
1238 }
1239 OperatorName = Lex.getCurStrVal();
1240 Lex.Lex(); // eat the VarName.
1241 }
1242
Chris Lattnerf4601652007-11-22 20:49:04 +00001243 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1244 if (Lex.getCode() != tgtok::r_paren) {
1245 DagArgs = ParseDagArgList(CurRec);
1246 if (DagArgs.empty()) return 0;
1247 }
1248
1249 if (Lex.getCode() != tgtok::r_paren) {
1250 TokError("expected ')' in dag init");
1251 return 0;
1252 }
1253 Lex.Lex(); // eat the ')'
1254
Nate Begeman7cee8172009-03-19 05:21:56 +00001255 return new DagInit(Operator, OperatorName, DagArgs);
David Greened418c1b2009-05-14 20:54:48 +00001256 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001257 }
David Greened418c1b2009-05-14 20:54:48 +00001258
David Greene5f9f9ba2009-05-14 22:38:31 +00001259 case tgtok::XCar:
1260 case tgtok::XCdr:
1261 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001262 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001263 case tgtok::XConcat:
1264 case tgtok::XSRA:
1265 case tgtok::XSRL:
1266 case tgtok::XSHL:
David Greenec7cafcd2009-04-22 20:18:10 +00001267 case tgtok::XStrConcat:
David Greene4afc5092009-05-14 21:54:42 +00001268 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001269 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001270 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001271 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001272 return ParseOperation(CurRec);
1273 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001274 }
1275 }
1276
1277 return R;
1278}
1279
1280/// ParseValue - Parse a tblgen value. This returns null on error.
1281///
1282/// Value ::= SimpleValue ValueSuffix*
1283/// ValueSuffix ::= '{' BitList '}'
1284/// ValueSuffix ::= '[' BitList ']'
1285/// ValueSuffix ::= '.' ID
1286///
David Greenee1b46912009-06-08 20:23:18 +00001287Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1288 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001289 if (Result == 0) return 0;
1290
1291 // Parse the suffixes now if present.
1292 while (1) {
1293 switch (Lex.getCode()) {
1294 default: return Result;
1295 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001296 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001297 Lex.Lex(); // eat the '{'
1298 std::vector<unsigned> Ranges = ParseRangeList();
1299 if (Ranges.empty()) return 0;
1300
1301 // Reverse the bitlist.
1302 std::reverse(Ranges.begin(), Ranges.end());
1303 Result = Result->convertInitializerBitRange(Ranges);
1304 if (Result == 0) {
1305 Error(CurlyLoc, "Invalid bit range for value");
1306 return 0;
1307 }
1308
1309 // Eat the '}'.
1310 if (Lex.getCode() != tgtok::r_brace) {
1311 TokError("expected '}' at end of bit range list");
1312 return 0;
1313 }
1314 Lex.Lex();
1315 break;
1316 }
1317 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001318 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001319 Lex.Lex(); // eat the '['
1320 std::vector<unsigned> Ranges = ParseRangeList();
1321 if (Ranges.empty()) return 0;
1322
1323 Result = Result->convertInitListSlice(Ranges);
1324 if (Result == 0) {
1325 Error(SquareLoc, "Invalid range for list slice");
1326 return 0;
1327 }
1328
1329 // Eat the ']'.
1330 if (Lex.getCode() != tgtok::r_square) {
1331 TokError("expected ']' at end of list slice");
1332 return 0;
1333 }
1334 Lex.Lex();
1335 break;
1336 }
1337 case tgtok::period:
1338 if (Lex.Lex() != tgtok::Id) { // eat the .
1339 TokError("expected field identifier after '.'");
1340 return 0;
1341 }
1342 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001343 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001344 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001345 return 0;
1346 }
1347 Result = new FieldInit(Result, Lex.getCurStrVal());
1348 Lex.Lex(); // eat field name
1349 break;
1350 }
1351 }
1352}
1353
1354/// ParseDagArgList - Parse the argument list for a dag literal expression.
1355///
1356/// ParseDagArgList ::= Value (':' VARNAME)?
1357/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
1358std::vector<std::pair<llvm::Init*, std::string> >
1359TGParser::ParseDagArgList(Record *CurRec) {
1360 std::vector<std::pair<llvm::Init*, std::string> > Result;
1361
1362 while (1) {
1363 Init *Val = ParseValue(CurRec);
1364 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
1365
1366 // If the variable name is present, add it.
1367 std::string VarName;
1368 if (Lex.getCode() == tgtok::colon) {
1369 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1370 TokError("expected variable name in dag literal");
1371 return std::vector<std::pair<llvm::Init*, std::string> >();
1372 }
1373 VarName = Lex.getCurStrVal();
1374 Lex.Lex(); // eat the VarName.
1375 }
1376
1377 Result.push_back(std::make_pair(Val, VarName));
1378
1379 if (Lex.getCode() != tgtok::comma) break;
1380 Lex.Lex(); // eat the ','
1381 }
1382
1383 return Result;
1384}
1385
1386
1387/// ParseValueList - Parse a comma separated list of values, returning them as a
1388/// vector. Note that this always expects to be able to parse at least one
1389/// value. It returns an empty list if this is not possible.
1390///
1391/// ValueList ::= Value (',' Value)
1392///
David Greenee1b46912009-06-08 20:23:18 +00001393std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001394 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001395 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001396 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001397 if (ArgsRec != 0 && EltTy == 0) {
1398 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1399 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1400 assert(RV && "Template argument record not found??");
1401 ItemType = RV->getType();
1402 ++ArgN;
1403 }
1404 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001405 if (Result.back() == 0) return std::vector<Init*>();
1406
1407 while (Lex.getCode() == tgtok::comma) {
1408 Lex.Lex(); // Eat the comma
1409
David Greenee1b46912009-06-08 20:23:18 +00001410 if (ArgsRec != 0 && EltTy == 0) {
1411 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001412 if (ArgN >= TArgs.size()) {
1413 TokError("too many template arguments");
1414 return std::vector<Init*>();
1415 }
David Greenee1b46912009-06-08 20:23:18 +00001416 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1417 assert(RV && "Template argument record not found??");
1418 ItemType = RV->getType();
1419 ++ArgN;
1420 }
1421 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001422 if (Result.back() == 0) return std::vector<Init*>();
1423 }
1424
1425 return Result;
1426}
1427
1428
1429
1430/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1431/// empty string on error. This can happen in a number of different context's,
1432/// including within a def or in the template args for a def (which which case
1433/// CurRec will be non-null) and within the template args for a multiclass (in
1434/// which case CurRec will be null, but CurMultiClass will be set). This can
1435/// also happen within a def that is within a multiclass, which will set both
1436/// CurRec and CurMultiClass.
1437///
1438/// Declaration ::= FIELD? Type ID ('=' Value)?
1439///
1440std::string TGParser::ParseDeclaration(Record *CurRec,
1441 bool ParsingTemplateArgs) {
1442 // Read the field prefix if present.
1443 bool HasField = Lex.getCode() == tgtok::Field;
1444 if (HasField) Lex.Lex();
1445
1446 RecTy *Type = ParseType();
1447 if (Type == 0) return "";
1448
1449 if (Lex.getCode() != tgtok::Id) {
1450 TokError("Expected identifier in declaration");
1451 return "";
1452 }
1453
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001454 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001455 std::string DeclName = Lex.getCurStrVal();
1456 Lex.Lex();
1457
1458 if (ParsingTemplateArgs) {
1459 if (CurRec) {
1460 DeclName = CurRec->getName() + ":" + DeclName;
1461 } else {
1462 assert(CurMultiClass);
1463 }
1464 if (CurMultiClass)
1465 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1466 }
1467
1468 // Add the value.
1469 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1470 return "";
1471
1472 // If a value is present, parse it.
1473 if (Lex.getCode() == tgtok::equal) {
1474 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001475 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001476 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001477 if (Val == 0 ||
1478 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1479 return "";
1480 }
1481
1482 return DeclName;
1483}
1484
1485/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1486/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1487/// template args for a def, which may or may not be in a multiclass. If null,
1488/// these are the template args for a multiclass.
1489///
1490/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1491///
1492bool TGParser::ParseTemplateArgList(Record *CurRec) {
1493 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1494 Lex.Lex(); // eat the '<'
1495
1496 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1497
1498 // Read the first declaration.
1499 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1500 if (TemplArg.empty())
1501 return true;
1502
1503 TheRecToAddTo->addTemplateArg(TemplArg);
1504
1505 while (Lex.getCode() == tgtok::comma) {
1506 Lex.Lex(); // eat the ','
1507
1508 // Read the following declarations.
1509 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1510 if (TemplArg.empty())
1511 return true;
1512 TheRecToAddTo->addTemplateArg(TemplArg);
1513 }
1514
1515 if (Lex.getCode() != tgtok::greater)
1516 return TokError("expected '>' at end of template argument list");
1517 Lex.Lex(); // eat the '>'.
1518 return false;
1519}
1520
1521
1522/// ParseBodyItem - Parse a single item at within the body of a def or class.
1523///
1524/// BodyItem ::= Declaration ';'
1525/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1526bool TGParser::ParseBodyItem(Record *CurRec) {
1527 if (Lex.getCode() != tgtok::Let) {
1528 if (ParseDeclaration(CurRec, false).empty())
1529 return true;
1530
1531 if (Lex.getCode() != tgtok::semi)
1532 return TokError("expected ';' after declaration");
1533 Lex.Lex();
1534 return false;
1535 }
1536
1537 // LET ID OptionalRangeList '=' Value ';'
1538 if (Lex.Lex() != tgtok::Id)
1539 return TokError("expected field identifier after let");
1540
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001541 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001542 std::string FieldName = Lex.getCurStrVal();
1543 Lex.Lex(); // eat the field name.
1544
1545 std::vector<unsigned> BitList;
1546 if (ParseOptionalBitList(BitList))
1547 return true;
1548 std::reverse(BitList.begin(), BitList.end());
1549
1550 if (Lex.getCode() != tgtok::equal)
1551 return TokError("expected '=' in let expression");
1552 Lex.Lex(); // eat the '='.
1553
David Greenee1b46912009-06-08 20:23:18 +00001554 RecordVal *Field = CurRec->getValue(FieldName);
1555 if (Field == 0)
1556 return TokError("Value '" + FieldName + "' unknown!");
1557
1558 RecTy *Type = Field->getType();
1559
1560 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001561 if (Val == 0) return true;
1562
1563 if (Lex.getCode() != tgtok::semi)
1564 return TokError("expected ';' after let expression");
1565 Lex.Lex();
1566
1567 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1568}
1569
1570/// ParseBody - Read the body of a class or def. Return true on error, false on
1571/// success.
1572///
1573/// Body ::= ';'
1574/// Body ::= '{' BodyList '}'
1575/// BodyList BodyItem*
1576///
1577bool TGParser::ParseBody(Record *CurRec) {
1578 // If this is a null definition, just eat the semi and return.
1579 if (Lex.getCode() == tgtok::semi) {
1580 Lex.Lex();
1581 return false;
1582 }
1583
1584 if (Lex.getCode() != tgtok::l_brace)
1585 return TokError("Expected ';' or '{' to start body");
1586 // Eat the '{'.
1587 Lex.Lex();
1588
1589 while (Lex.getCode() != tgtok::r_brace)
1590 if (ParseBodyItem(CurRec))
1591 return true;
1592
1593 // Eat the '}'.
1594 Lex.Lex();
1595 return false;
1596}
1597
1598/// ParseObjectBody - Parse the body of a def or class. This consists of an
1599/// optional ClassList followed by a Body. CurRec is the current def or class
1600/// that is being parsed.
1601///
1602/// ObjectBody ::= BaseClassList Body
1603/// BaseClassList ::= /*empty*/
1604/// BaseClassList ::= ':' BaseClassListNE
1605/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1606///
1607bool TGParser::ParseObjectBody(Record *CurRec) {
1608 // If there is a baseclass list, read it.
1609 if (Lex.getCode() == tgtok::colon) {
1610 Lex.Lex();
1611
1612 // Read all of the subclasses.
1613 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1614 while (1) {
1615 // Check for error.
1616 if (SubClass.Rec == 0) return true;
1617
1618 // Add it.
1619 if (AddSubClass(CurRec, SubClass))
1620 return true;
1621
1622 if (Lex.getCode() != tgtok::comma) break;
1623 Lex.Lex(); // eat ','.
1624 SubClass = ParseSubClassReference(CurRec, false);
1625 }
1626 }
1627
1628 // Process any variables on the let stack.
1629 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1630 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1631 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1632 LetStack[i][j].Bits, LetStack[i][j].Value))
1633 return true;
1634
1635 return ParseBody(CurRec);
1636}
1637
1638
1639/// ParseDef - Parse and return a top level or multiclass def, return the record
1640/// corresponding to it. This returns null on error.
1641///
1642/// DefInst ::= DEF ObjectName ObjectBody
1643///
1644llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001645 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001646 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1647 Lex.Lex(); // Eat the 'def' token.
1648
1649 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001650 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001651
1652 if (!CurMultiClass) {
1653 // Top-level def definition.
1654
1655 // Ensure redefinition doesn't happen.
1656 if (Records.getDef(CurRec->getName())) {
1657 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1658 return 0;
1659 }
1660 Records.addDef(CurRec);
1661 } else {
1662 // Otherwise, a def inside a multiclass, add it to the multiclass.
1663 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1664 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1665 Error(DefLoc, "def '" + CurRec->getName() +
1666 "' already defined in this multiclass!");
1667 return 0;
1668 }
1669 CurMultiClass->DefPrototypes.push_back(CurRec);
1670 }
1671
1672 if (ParseObjectBody(CurRec))
1673 return 0;
1674
1675 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1676 CurRec->resolveReferences();
1677
1678 // If ObjectBody has template arguments, it's an error.
1679 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1680 return CurRec;
1681}
1682
1683
1684/// ParseClass - Parse a tblgen class definition.
1685///
1686/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1687///
1688bool TGParser::ParseClass() {
1689 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1690 Lex.Lex();
1691
1692 if (Lex.getCode() != tgtok::Id)
1693 return TokError("expected class name after 'class' keyword");
1694
1695 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1696 if (CurRec) {
1697 // If the body was previously defined, this is an error.
1698 if (!CurRec->getValues().empty() ||
1699 !CurRec->getSuperClasses().empty() ||
1700 !CurRec->getTemplateArgs().empty())
1701 return TokError("Class '" + CurRec->getName() + "' already defined");
1702 } else {
1703 // If this is the first reference to this class, create and add it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001704 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001705 Records.addClass(CurRec);
1706 }
1707 Lex.Lex(); // eat the name.
1708
1709 // If there are template args, parse them.
1710 if (Lex.getCode() == tgtok::less)
1711 if (ParseTemplateArgList(CurRec))
1712 return true;
1713
1714 // Finally, parse the object body.
1715 return ParseObjectBody(CurRec);
1716}
1717
1718/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1719/// of LetRecords.
1720///
1721/// LetList ::= LetItem (',' LetItem)*
1722/// LetItem ::= ID OptionalRangeList '=' Value
1723///
1724std::vector<LetRecord> TGParser::ParseLetList() {
1725 std::vector<LetRecord> Result;
1726
1727 while (1) {
1728 if (Lex.getCode() != tgtok::Id) {
1729 TokError("expected identifier in let definition");
1730 return std::vector<LetRecord>();
1731 }
1732 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001733 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001734 Lex.Lex(); // Eat the identifier.
1735
1736 // Check for an optional RangeList.
1737 std::vector<unsigned> Bits;
1738 if (ParseOptionalRangeList(Bits))
1739 return std::vector<LetRecord>();
1740 std::reverse(Bits.begin(), Bits.end());
1741
1742 if (Lex.getCode() != tgtok::equal) {
1743 TokError("expected '=' in let expression");
1744 return std::vector<LetRecord>();
1745 }
1746 Lex.Lex(); // eat the '='.
1747
1748 Init *Val = ParseValue(0);
1749 if (Val == 0) return std::vector<LetRecord>();
1750
1751 // Now that we have everything, add the record.
1752 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1753
1754 if (Lex.getCode() != tgtok::comma)
1755 return Result;
1756 Lex.Lex(); // eat the comma.
1757 }
1758}
1759
1760/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1761/// different related productions.
1762///
1763/// Object ::= LET LetList IN '{' ObjectList '}'
1764/// Object ::= LET LetList IN Object
1765///
1766bool TGParser::ParseTopLevelLet() {
1767 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1768 Lex.Lex();
1769
1770 // Add this entry to the let stack.
1771 std::vector<LetRecord> LetInfo = ParseLetList();
1772 if (LetInfo.empty()) return true;
1773 LetStack.push_back(LetInfo);
1774
1775 if (Lex.getCode() != tgtok::In)
1776 return TokError("expected 'in' at end of top-level 'let'");
1777 Lex.Lex();
1778
1779 // If this is a scalar let, just handle it now
1780 if (Lex.getCode() != tgtok::l_brace) {
1781 // LET LetList IN Object
1782 if (ParseObject())
1783 return true;
1784 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001785 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001786 // Otherwise, this is a group let.
1787 Lex.Lex(); // eat the '{'.
1788
1789 // Parse the object list.
1790 if (ParseObjectList())
1791 return true;
1792
1793 if (Lex.getCode() != tgtok::r_brace) {
1794 TokError("expected '}' at end of top level let command");
1795 return Error(BraceLoc, "to match this '{'");
1796 }
1797 Lex.Lex();
1798 }
1799
1800 // Outside this let scope, this let block is not active.
1801 LetStack.pop_back();
1802 return false;
1803}
1804
1805/// ParseMultiClassDef - Parse a def in a multiclass context.
1806///
1807/// MultiClassDef ::= DefInst
1808///
1809bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1810 if (Lex.getCode() != tgtok::Def)
1811 return TokError("expected 'def' in multiclass body");
1812
1813 Record *D = ParseDef(CurMC);
1814 if (D == 0) return true;
1815
1816 // Copy the template arguments for the multiclass into the def.
1817 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1818
1819 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1820 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1821 assert(RV && "Template arg doesn't exist?");
1822 D->addValue(*RV);
1823 }
1824
1825 return false;
1826}
1827
1828/// ParseMultiClass - Parse a multiclass definition.
1829///
Bob Wilson32558652009-04-28 19:41:44 +00001830/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1831/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001832///
1833bool TGParser::ParseMultiClass() {
1834 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1835 Lex.Lex(); // Eat the multiclass token.
1836
1837 if (Lex.getCode() != tgtok::Id)
1838 return TokError("expected identifier after multiclass for name");
1839 std::string Name = Lex.getCurStrVal();
1840
1841 if (MultiClasses.count(Name))
1842 return TokError("multiclass '" + Name + "' already defined");
1843
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001844 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001845 Lex.Lex(); // Eat the identifier.
1846
1847 // If there are template args, parse them.
1848 if (Lex.getCode() == tgtok::less)
1849 if (ParseTemplateArgList(0))
1850 return true;
1851
David Greened34a73b2009-04-24 16:55:41 +00001852 bool inherits = false;
1853
David Greenede444af2009-04-22 16:42:54 +00001854 // If there are submulticlasses, parse them.
1855 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001856 inherits = true;
1857
David Greenede444af2009-04-22 16:42:54 +00001858 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001859
David Greenede444af2009-04-22 16:42:54 +00001860 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001861 SubMultiClassReference SubMultiClass =
1862 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001863 while (1) {
1864 // Check for error.
1865 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001866
David Greenede444af2009-04-22 16:42:54 +00001867 // Add it.
1868 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1869 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001870
David Greenede444af2009-04-22 16:42:54 +00001871 if (Lex.getCode() != tgtok::comma) break;
1872 Lex.Lex(); // eat ','.
1873 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1874 }
1875 }
1876
David Greened34a73b2009-04-24 16:55:41 +00001877 if (Lex.getCode() != tgtok::l_brace) {
1878 if (!inherits)
1879 return TokError("expected '{' in multiclass definition");
1880 else
1881 if (Lex.getCode() != tgtok::semi)
1882 return TokError("expected ';' in multiclass definition");
1883 else
1884 Lex.Lex(); // eat the ';'.
1885 }
1886 else {
1887 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1888 return TokError("multiclass must contain at least one def");
Chris Lattnerf4601652007-11-22 20:49:04 +00001889
David Greened34a73b2009-04-24 16:55:41 +00001890 while (Lex.getCode() != tgtok::r_brace)
1891 if (ParseMultiClassDef(CurMultiClass))
1892 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001893
David Greened34a73b2009-04-24 16:55:41 +00001894 Lex.Lex(); // eat the '}'.
1895 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001896
1897 CurMultiClass = 0;
1898 return false;
1899}
1900
1901/// ParseDefm - Parse the instantiation of a multiclass.
1902///
1903/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1904///
1905bool TGParser::ParseDefm() {
1906 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1907 if (Lex.Lex() != tgtok::Id) // eat the defm.
1908 return TokError("expected identifier after defm");
1909
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001910 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001911 std::string DefmPrefix = Lex.getCurStrVal();
1912 if (Lex.Lex() != tgtok::colon)
1913 return TokError("expected ':' after defm identifier");
1914
1915 // eat the colon.
1916 Lex.Lex();
1917
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001918 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001919 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001920
1921 while (1) {
1922 if (Ref.Rec == 0) return true;
1923
1924 // To instantiate a multiclass, we need to first get the multiclass, then
1925 // instantiate each def contained in the multiclass with the SubClassRef
1926 // template parameters.
1927 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1928 assert(MC && "Didn't lookup multiclass correctly?");
1929 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1930
1931 // Verify that the correct number of template arguments were specified.
1932 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1933 if (TArgs.size() < TemplateVals.size())
1934 return Error(SubClassLoc,
1935 "more template args specified than multiclass expects");
1936
1937 // Loop over all the def's in the multiclass, instantiating each one.
1938 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1939 Record *DefProto = MC->DefPrototypes[i];
1940
David Greene065f2592009-05-05 16:28:25 +00001941 // Add in the defm name
1942 std::string DefName = DefProto->getName();
1943 std::string::size_type idx = DefName.find("#NAME#");
1944 if (idx != std::string::npos) {
1945 DefName.replace(idx, 6, DefmPrefix);
1946 }
1947 else {
1948 // Add the suffix to the defm name to get the new name.
1949 DefName = DefmPrefix + DefName;
1950 }
1951
1952 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001953
1954 SubClassReference Ref;
1955 Ref.RefLoc = DefmPrefixLoc;
1956 Ref.Rec = DefProto;
1957 AddSubClass(CurRec, Ref);
1958
1959 // Loop over all of the template arguments, setting them to the specified
1960 // value or leaving them as the default if necessary.
1961 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001962 // Check if a value is specified for this temp-arg.
1963 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001964 // Set it now.
1965 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1966 TemplateVals[i]))
1967 return true;
1968
1969 // Resolve it next.
1970 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1971
1972 // Now remove it.
1973 CurRec->removeValue(TArgs[i]);
1974
1975 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001976 return Error(SubClassLoc,
1977 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001978 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1979 MC->Rec.getName() + "'");
1980 }
1981 }
1982
1983 // If the mdef is inside a 'let' expression, add to each def.
1984 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1985 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1986 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1987 LetStack[i][j].Bits, LetStack[i][j].Value)) {
1988 Error(DefmPrefixLoc, "when instantiating this defm");
1989 return true;
1990 }
1991
1992 // Ensure redefinition doesn't happen.
1993 if (Records.getDef(CurRec->getName()))
1994 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
1995 "' already defined, instantiating defm with subdef '" +
1996 DefProto->getName() + "'");
1997 Records.addDef(CurRec);
1998 CurRec->resolveReferences();
1999 }
2000
2001 if (Lex.getCode() != tgtok::comma) break;
2002 Lex.Lex(); // eat ','.
2003
2004 SubClassLoc = Lex.getLoc();
2005 Ref = ParseSubClassReference(0, true);
2006 }
2007
Chris Lattnerf4601652007-11-22 20:49:04 +00002008 if (Lex.getCode() != tgtok::semi)
2009 return TokError("expected ';' at end of defm");
2010 Lex.Lex();
2011
Chris Lattnerf4601652007-11-22 20:49:04 +00002012 return false;
2013}
2014
2015/// ParseObject
2016/// Object ::= ClassInst
2017/// Object ::= DefInst
2018/// Object ::= MultiClassInst
2019/// Object ::= DefMInst
2020/// Object ::= LETCommand '{' ObjectList '}'
2021/// Object ::= LETCommand Object
2022bool TGParser::ParseObject() {
2023 switch (Lex.getCode()) {
2024 default: assert(0 && "This is not an object");
2025 case tgtok::Let: return ParseTopLevelLet();
2026 case tgtok::Def: return ParseDef(0) == 0;
2027 case tgtok::Defm: return ParseDefm();
2028 case tgtok::Class: return ParseClass();
2029 case tgtok::MultiClass: return ParseMultiClass();
2030 }
2031}
2032
2033/// ParseObjectList
2034/// ObjectList :== Object*
2035bool TGParser::ParseObjectList() {
2036 while (isObjectStart(Lex.getCode())) {
2037 if (ParseObject())
2038 return true;
2039 }
2040 return false;
2041}
2042
2043
2044bool TGParser::ParseFile() {
2045 Lex.Lex(); // Prime the lexer.
2046 if (ParseObjectList()) return true;
2047
2048 // If we have unread input at the end of the file, report it.
2049 if (Lex.getCode() == tgtok::Eof)
2050 return false;
2051
2052 return TokError("Unexpected input at top level");
2053}
2054