blob: d2bc6b853dc055a5e937cb2c21ec66b8935b8cf1 [file] [log] [blame]
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-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 Lattner6b7d76a2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
Chuck Rose III9d87b1b2007-11-26 23:19:59 +000014#include <algorithm>
David Greenef2db6e22009-06-08 20:23:18 +000015#include <sstream>
Chuck Rose III9d87b1b2007-11-26 23:19:59 +000016
Chris Lattner6b7d76a2007-11-22 20:49:04 +000017#include "TGParser.h"
18#include "Record.h"
19#include "llvm/ADT/StringExtras.h"
David Greenee3269dd2009-04-24 16:55:41 +000020#include "llvm/Support/Streams.h"
Chris Lattner6b7d76a2007-11-22 20:49:04 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Support Code for the Semantic Actions.
25//===----------------------------------------------------------------------===//
26
27namespace llvm {
Chris Lattner6b7d76a2007-11-22 20:49:04 +000028struct SubClassReference {
Chris Lattnere7205cd2009-03-13 16:01:53 +000029 TGLoc RefLoc;
Chris Lattner6b7d76a2007-11-22 20:49:04 +000030 Record *Rec;
31 std::vector<Init*> TemplateArgs;
Chris Lattnere7205cd2009-03-13 16:01:53 +000032 SubClassReference() : Rec(0) {}
David Greenee3269dd2009-04-24 16:55:41 +000033
Chris Lattner6b7d76a2007-11-22 20:49:04 +000034 bool isInvalid() const { return Rec == 0; }
35};
David Greene60b5acc2009-04-22 16:42:54 +000036
37struct SubMultiClassReference {
38 TGLoc RefLoc;
39 MultiClass *MC;
40 std::vector<Init*> TemplateArgs;
41 SubMultiClassReference() : MC(0) {}
Bob Wilsona7fa8b32009-04-28 19:41:44 +000042
David Greene60b5acc2009-04-22 16:42:54 +000043 bool isInvalid() const { return MC == 0; }
David Greenee3269dd2009-04-24 16:55:41 +000044 void dump() const;
David Greene60b5acc2009-04-22 16:42:54 +000045};
David Greenee3269dd2009-04-24 16:55:41 +000046
47void SubMultiClassReference::dump() const {
48 cerr << "Multiclass:\n";
49
50 MC->dump();
51
52 cerr << "Template args:\n";
53 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
54 iend = TemplateArgs.end();
55 i != iend;
56 ++i) {
57 (*i)->dump();
58 }
59}
60
Chris Lattner6b7d76a2007-11-22 20:49:04 +000061} // end namespace llvm
62
Chris Lattnere7205cd2009-03-13 16:01:53 +000063bool TGParser::AddValue(Record *CurRec, TGLoc Loc, const RecordVal &RV) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +000064 if (CurRec == 0)
65 CurRec = &CurMultiClass->Rec;
66
67 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
68 // The value already exists in the class, treat this as a set.
69 if (ERV->setValue(RV.getValue()))
70 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
71 RV.getType()->getAsString() + "' is incompatible with " +
72 "previous definition of type '" +
73 ERV->getType()->getAsString() + "'");
74 } else {
75 CurRec->addValue(RV);
76 }
77 return false;
78}
79
80/// SetValue -
81/// Return true on error, false on success.
Chris Lattnere7205cd2009-03-13 16:01:53 +000082bool TGParser::SetValue(Record *CurRec, TGLoc Loc, const std::string &ValName,
Chris Lattner6b7d76a2007-11-22 20:49:04 +000083 const std::vector<unsigned> &BitList, Init *V) {
84 if (!V) return false;
85
86 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
87
88 RecordVal *RV = CurRec->getValue(ValName);
89 if (RV == 0)
90 return Error(Loc, "Value '" + ValName + "' unknown!");
91
92 // Do not allow assignments like 'X = X'. This will just cause infinite loops
93 // in the resolution machinery.
94 if (BitList.empty())
95 if (VarInit *VI = dynamic_cast<VarInit*>(V))
96 if (VI->getName() == ValName)
97 return false;
98
99 // If we are assigning to a subset of the bits in the value... then we must be
100 // assigning to a field of BitsRecTy, which must have a BitsInit
101 // initializer.
102 //
103 if (!BitList.empty()) {
104 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
105 if (CurVal == 0)
106 return Error(Loc, "Value '" + ValName + "' is not a bits type");
107
108 // Convert the incoming value to a bits type of the appropriate size...
109 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
110 if (BI == 0) {
111 V->convertInitializerTo(new BitsRecTy(BitList.size()));
112 return Error(Loc, "Initializer is not compatible with bit range");
113 }
114
115 // We should have a BitsInit type now.
116 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
117 assert(BInit != 0);
118
119 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
120
121 // Loop over bits, assigning values as appropriate.
122 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
123 unsigned Bit = BitList[i];
124 if (NewVal->getBit(Bit))
125 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
126 ValName + "' more than once");
127 NewVal->setBit(Bit, BInit->getBit(i));
128 }
129
130 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
131 if (NewVal->getBit(i) == 0)
132 NewVal->setBit(i, CurVal->getBit(i));
133
134 V = NewVal;
135 }
136
137 if (RV->setValue(V))
138 return Error(Loc, "Value '" + ValName + "' of type '" +
139 RV->getType()->getAsString() +
Chris Lattner6325a022007-11-22 21:06:59 +0000140 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000141 return false;
142}
143
144/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
145/// args as SubClass's template arguments.
Cédric Venetee2a3eb2009-02-14 16:06:42 +0000146bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000147 Record *SC = SubClass.Rec;
148 // Add all of the values in the subclass into the current class.
149 const std::vector<RecordVal> &Vals = SC->getValues();
150 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
151 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
152 return true;
153
154 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
155
156 // Ensure that an appropriate number of template arguments are specified.
157 if (TArgs.size() < SubClass.TemplateArgs.size())
158 return Error(SubClass.RefLoc, "More template args specified than expected");
159
160 // Loop over all of the template arguments, setting them to the specified
161 // value or leaving them as the default if necessary.
162 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
163 if (i < SubClass.TemplateArgs.size()) {
164 // If a value is specified for this template arg, set it now.
165 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
166 SubClass.TemplateArgs[i]))
167 return true;
168
169 // Resolve it next.
170 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
171
172 // Now remove it.
173 CurRec->removeValue(TArgs[i]);
174
175 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
176 return Error(SubClass.RefLoc,"Value not specified for template argument #"
177 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
178 SC->getName() + "'!");
179 }
180 }
181
182 // Since everything went well, we can now set the "superclass" list for the
183 // current record.
184 const std::vector<Record*> &SCs = SC->getSuperClasses();
185 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
186 if (CurRec->isSubClassOf(SCs[i]))
187 return Error(SubClass.RefLoc,
188 "Already subclass of '" + SCs[i]->getName() + "'!\n");
189 CurRec->addSuperClass(SCs[i]);
190 }
191
192 if (CurRec->isSubClassOf(SC))
193 return Error(SubClass.RefLoc,
194 "Already subclass of '" + SC->getName() + "'!\n");
195 CurRec->addSuperClass(SC);
196 return false;
197}
198
David Greene60b5acc2009-04-22 16:42:54 +0000199/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson4d176612009-04-30 18:26:19 +0000200/// CurMC, resolving its template args as SubMultiClass's
David Greene60b5acc2009-04-22 16:42:54 +0000201/// template arguments.
Bob Wilson4d176612009-04-30 18:26:19 +0000202bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilsonda120402009-04-30 17:46:20 +0000203 SubMultiClassReference &SubMultiClass) {
David Greene60b5acc2009-04-22 16:42:54 +0000204 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson4d176612009-04-30 18:26:19 +0000205 Record *CurRec = &CurMC->Rec;
David Greene60b5acc2009-04-22 16:42:54 +0000206
Bob Wilson4d176612009-04-30 18:26:19 +0000207 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greene60b5acc2009-04-22 16:42:54 +0000208
209 // Add all of the values in the subclass into the current class.
210 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
211 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
212 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
213 return true;
214
Bob Wilson4d176612009-04-30 18:26:19 +0000215 int newDefStart = CurMC->DefPrototypes.size();
David Greenee3269dd2009-04-24 16:55:41 +0000216
David Greene60b5acc2009-04-22 16:42:54 +0000217 // Add all of the defs in the subclass into the current multiclass.
218 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
219 iend = SMC->DefPrototypes.end();
220 i != iend;
221 ++i) {
222 // Clone the def and add it to the current multiclass
223 Record *NewDef = new Record(**i);
224
225 // Add all of the values in the superclass into the current def.
226 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
227 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
228 return true;
229
Bob Wilson4d176612009-04-30 18:26:19 +0000230 CurMC->DefPrototypes.push_back(NewDef);
David Greene60b5acc2009-04-22 16:42:54 +0000231 }
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000232
David Greene60b5acc2009-04-22 16:42:54 +0000233 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
234
David Greenee3269dd2009-04-24 16:55:41 +0000235 // Ensure that an appropriate number of template arguments are
236 // specified.
David Greene60b5acc2009-04-22 16:42:54 +0000237 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greenee3269dd2009-04-24 16:55:41 +0000238 return Error(SubMultiClass.RefLoc,
239 "More template args specified than expected");
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000240
David Greene60b5acc2009-04-22 16:42:54 +0000241 // Loop over all of the template arguments, setting them to the specified
242 // value or leaving them as the default if necessary.
243 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
244 if (i < SubMultiClass.TemplateArgs.size()) {
David Greenee3269dd2009-04-24 16:55:41 +0000245 // If a value is specified for this template arg, set it in the
246 // superclass now.
247 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000248 std::vector<unsigned>(),
David Greene60b5acc2009-04-22 16:42:54 +0000249 SubMultiClass.TemplateArgs[i]))
250 return true;
251
252 // Resolve it next.
253 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000254
David Greene60b5acc2009-04-22 16:42:54 +0000255 // Now remove it.
256 CurRec->removeValue(SMCTArgs[i]);
257
David Greenee3269dd2009-04-24 16:55:41 +0000258 // If a value is specified for this template arg, set it in the
259 // new defs now.
260 for (MultiClass::RecordVector::iterator j =
Bob Wilson4d176612009-04-30 18:26:19 +0000261 CurMC->DefPrototypes.begin() + newDefStart,
262 jend = CurMC->DefPrototypes.end();
David Greene60b5acc2009-04-22 16:42:54 +0000263 j != jend;
264 ++j) {
265 Record *Def = *j;
266
David Greenee3269dd2009-04-24 16:55:41 +0000267 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000268 std::vector<unsigned>(),
David Greene60b5acc2009-04-22 16:42:54 +0000269 SubMultiClass.TemplateArgs[i]))
270 return true;
271
272 // Resolve it next.
273 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
274
275 // Now remove it
276 Def->removeValue(SMCTArgs[i]);
277 }
278 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greenee3269dd2009-04-24 16:55:41 +0000279 return Error(SubMultiClass.RefLoc,
280 "Value not specified for template argument #"
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000281 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greene60b5acc2009-04-22 16:42:54 +0000282 SMC->Rec.getName() + "'!");
283 }
284 }
285
286 return false;
287}
288
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000289//===----------------------------------------------------------------------===//
290// Parser Code
291//===----------------------------------------------------------------------===//
292
293/// isObjectStart - Return true if this is a valid first token for an Object.
294static bool isObjectStart(tgtok::TokKind K) {
295 return K == tgtok::Class || K == tgtok::Def ||
296 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
297}
298
299/// ParseObjectName - If an object name is specified, return it. Otherwise,
300/// return an anonymous name.
301/// ObjectName ::= ID
302/// ObjectName ::= /*empty*/
303///
304std::string TGParser::ParseObjectName() {
305 if (Lex.getCode() == tgtok::Id) {
306 std::string Ret = Lex.getCurStrVal();
307 Lex.Lex();
308 return Ret;
309 }
310
311 static unsigned AnonCounter = 0;
312 return "anonymous."+utostr(AnonCounter++);
313}
314
315
316/// ParseClassID - Parse and resolve a reference to a class name. This returns
317/// null on error.
318///
319/// ClassID ::= ID
320///
321Record *TGParser::ParseClassID() {
322 if (Lex.getCode() != tgtok::Id) {
323 TokError("expected name for ClassID");
324 return 0;
325 }
326
327 Record *Result = Records.getClass(Lex.getCurStrVal());
328 if (Result == 0)
329 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
330
331 Lex.Lex();
332 return Result;
333}
334
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000335/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
336/// This returns null on error.
David Greene60b5acc2009-04-22 16:42:54 +0000337///
338/// MultiClassID ::= ID
339///
340MultiClass *TGParser::ParseMultiClassID() {
341 if (Lex.getCode() != tgtok::Id) {
342 TokError("expected name for ClassID");
343 return 0;
344 }
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000345
David Greene60b5acc2009-04-22 16:42:54 +0000346 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
347 if (Result == 0)
348 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000349
David Greene60b5acc2009-04-22 16:42:54 +0000350 Lex.Lex();
351 return Result;
352}
353
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000354Record *TGParser::ParseDefmID() {
355 if (Lex.getCode() != tgtok::Id) {
356 TokError("expected multiclass name");
357 return 0;
358 }
359
360 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
361 if (MC == 0) {
362 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
363 return 0;
364 }
365
366 Lex.Lex();
367 return &MC->Rec;
368}
369
370
371
372/// ParseSubClassReference - Parse a reference to a subclass or to a templated
373/// subclass. This returns a SubClassRefTy with a null Record* on error.
374///
375/// SubClassRef ::= ClassID
376/// SubClassRef ::= ClassID '<' ValueList '>'
377///
378SubClassReference TGParser::
379ParseSubClassReference(Record *CurRec, bool isDefm) {
380 SubClassReference Result;
381 Result.RefLoc = Lex.getLoc();
382
383 if (isDefm)
384 Result.Rec = ParseDefmID();
385 else
386 Result.Rec = ParseClassID();
387 if (Result.Rec == 0) return Result;
388
389 // If there is no template arg list, we're done.
390 if (Lex.getCode() != tgtok::less)
391 return Result;
392 Lex.Lex(); // Eat the '<'
393
394 if (Lex.getCode() == tgtok::greater) {
395 TokError("subclass reference requires a non-empty list of template values");
396 Result.Rec = 0;
397 return Result;
398 }
399
David Greenef2db6e22009-06-08 20:23:18 +0000400 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000401 if (Result.TemplateArgs.empty()) {
402 Result.Rec = 0; // Error parsing value list.
403 return Result;
404 }
405
406 if (Lex.getCode() != tgtok::greater) {
407 TokError("expected '>' in template value list");
408 Result.Rec = 0;
409 return Result;
410 }
411 Lex.Lex();
412
413 return Result;
414}
415
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000416/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
417/// templated submulticlass. This returns a SubMultiClassRefTy with a null
418/// Record* on error.
David Greene60b5acc2009-04-22 16:42:54 +0000419///
420/// SubMultiClassRef ::= MultiClassID
421/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
422///
423SubMultiClassReference TGParser::
424ParseSubMultiClassReference(MultiClass *CurMC) {
425 SubMultiClassReference Result;
426 Result.RefLoc = Lex.getLoc();
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000427
David Greene60b5acc2009-04-22 16:42:54 +0000428 Result.MC = ParseMultiClassID();
429 if (Result.MC == 0) return Result;
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000430
David Greene60b5acc2009-04-22 16:42:54 +0000431 // If there is no template arg list, we're done.
432 if (Lex.getCode() != tgtok::less)
433 return Result;
434 Lex.Lex(); // Eat the '<'
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000435
David Greene60b5acc2009-04-22 16:42:54 +0000436 if (Lex.getCode() == tgtok::greater) {
437 TokError("subclass reference requires a non-empty list of template values");
438 Result.MC = 0;
439 return Result;
440 }
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000441
David Greenef2db6e22009-06-08 20:23:18 +0000442 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greene60b5acc2009-04-22 16:42:54 +0000443 if (Result.TemplateArgs.empty()) {
444 Result.MC = 0; // Error parsing value list.
445 return Result;
446 }
Bob Wilsona7fa8b32009-04-28 19:41:44 +0000447
David Greene60b5acc2009-04-22 16:42:54 +0000448 if (Lex.getCode() != tgtok::greater) {
449 TokError("expected '>' in template value list");
450 Result.MC = 0;
451 return Result;
452 }
453 Lex.Lex();
454
455 return Result;
456}
457
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000458/// ParseRangePiece - Parse a bit/value range.
459/// RangePiece ::= INTVAL
460/// RangePiece ::= INTVAL '-' INTVAL
461/// RangePiece ::= INTVAL INTVAL
462bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattnerf8d803a2008-01-10 07:01:53 +0000463 if (Lex.getCode() != tgtok::IntVal) {
464 TokError("expected integer or bitrange");
465 return true;
466 }
Dan Gohman5a5e6e92008-10-17 01:33:43 +0000467 int64_t Start = Lex.getCurIntVal();
468 int64_t End;
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000469
470 if (Start < 0)
471 return TokError("invalid range, cannot be negative");
472
473 switch (Lex.Lex()) { // eat first character.
474 default:
475 Ranges.push_back(Start);
476 return false;
477 case tgtok::minus:
478 if (Lex.Lex() != tgtok::IntVal) {
479 TokError("expected integer value as end of range");
480 return true;
481 }
482 End = Lex.getCurIntVal();
483 break;
484 case tgtok::IntVal:
485 End = -Lex.getCurIntVal();
486 break;
487 }
488 if (End < 0)
489 return TokError("invalid range, cannot be negative");
490 Lex.Lex();
491
492 // Add to the range.
493 if (Start < End) {
494 for (; Start <= End; ++Start)
495 Ranges.push_back(Start);
496 } else {
497 for (; Start >= End; --Start)
498 Ranges.push_back(Start);
499 }
500 return false;
501}
502
503/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
504///
505/// RangeList ::= RangePiece (',' RangePiece)*
506///
507std::vector<unsigned> TGParser::ParseRangeList() {
508 std::vector<unsigned> Result;
509
510 // Parse the first piece.
511 if (ParseRangePiece(Result))
512 return std::vector<unsigned>();
513 while (Lex.getCode() == tgtok::comma) {
514 Lex.Lex(); // Eat the comma.
515
516 // Parse the next range piece.
517 if (ParseRangePiece(Result))
518 return std::vector<unsigned>();
519 }
520 return Result;
521}
522
523/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
524/// OptionalRangeList ::= '<' RangeList '>'
525/// OptionalRangeList ::= /*empty*/
526bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
527 if (Lex.getCode() != tgtok::less)
528 return false;
529
Chris Lattnere7205cd2009-03-13 16:01:53 +0000530 TGLoc StartLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000531 Lex.Lex(); // eat the '<'
532
533 // Parse the range list.
534 Ranges = ParseRangeList();
535 if (Ranges.empty()) return true;
536
537 if (Lex.getCode() != tgtok::greater) {
538 TokError("expected '>' at end of range list");
539 return Error(StartLoc, "to match this '<'");
540 }
541 Lex.Lex(); // eat the '>'.
542 return false;
543}
544
545/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
546/// OptionalBitList ::= '{' RangeList '}'
547/// OptionalBitList ::= /*empty*/
548bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
549 if (Lex.getCode() != tgtok::l_brace)
550 return false;
551
Chris Lattnere7205cd2009-03-13 16:01:53 +0000552 TGLoc StartLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000553 Lex.Lex(); // eat the '{'
554
555 // Parse the range list.
556 Ranges = ParseRangeList();
557 if (Ranges.empty()) return true;
558
559 if (Lex.getCode() != tgtok::r_brace) {
560 TokError("expected '}' at end of bit list");
561 return Error(StartLoc, "to match this '{'");
562 }
563 Lex.Lex(); // eat the '}'.
564 return false;
565}
566
567
568/// ParseType - Parse and return a tblgen type. This returns null on error.
569///
570/// Type ::= STRING // string type
571/// Type ::= BIT // bit type
572/// Type ::= BITS '<' INTVAL '>' // bits<x> type
573/// Type ::= INT // int type
574/// Type ::= LIST '<' Type '>' // list<x> type
575/// Type ::= CODE // code type
576/// Type ::= DAG // dag type
577/// Type ::= ClassID // Record Type
578///
579RecTy *TGParser::ParseType() {
580 switch (Lex.getCode()) {
581 default: TokError("Unknown token when expecting a type"); return 0;
582 case tgtok::String: Lex.Lex(); return new StringRecTy();
583 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
584 case tgtok::Int: Lex.Lex(); return new IntRecTy();
585 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
586 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
587 case tgtok::Id:
588 if (Record *R = ParseClassID()) return new RecordRecTy(R);
589 return 0;
590 case tgtok::Bits: {
591 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
592 TokError("expected '<' after bits type");
593 return 0;
594 }
595 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
596 TokError("expected integer in bits<n> type");
597 return 0;
598 }
Dan Gohman5a5e6e92008-10-17 01:33:43 +0000599 uint64_t Val = Lex.getCurIntVal();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000600 if (Lex.Lex() != tgtok::greater) { // Eat count.
601 TokError("expected '>' at end of bits<n> type");
602 return 0;
603 }
604 Lex.Lex(); // Eat '>'
605 return new BitsRecTy(Val);
606 }
607 case tgtok::List: {
608 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
609 TokError("expected '<' after list type");
610 return 0;
611 }
612 Lex.Lex(); // Eat '<'
613 RecTy *SubType = ParseType();
614 if (SubType == 0) return 0;
615
616 if (Lex.getCode() != tgtok::greater) {
617 TokError("expected '>' at end of list<ty> type");
618 return 0;
619 }
620 Lex.Lex(); // Eat '>'
621 return new ListRecTy(SubType);
622 }
623 }
624}
625
626/// ParseIDValue - Parse an ID as a value and decode what it means.
627///
628/// IDValue ::= ID [def local value]
629/// IDValue ::= ID [def template arg]
630/// IDValue ::= ID [multiclass local value]
631/// IDValue ::= ID [multiclass template argument]
632/// IDValue ::= ID [def name]
633///
634Init *TGParser::ParseIDValue(Record *CurRec) {
635 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
636 std::string Name = Lex.getCurStrVal();
Chris Lattnere7205cd2009-03-13 16:01:53 +0000637 TGLoc Loc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000638 Lex.Lex();
639 return ParseIDValue(CurRec, Name, Loc);
640}
641
642/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
643/// has already been read.
644Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattnere7205cd2009-03-13 16:01:53 +0000645 const std::string &Name, TGLoc NameLoc) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +0000646 if (CurRec) {
647 if (const RecordVal *RV = CurRec->getValue(Name))
648 return new VarInit(Name, RV->getType());
649
650 std::string TemplateArgName = CurRec->getName()+":"+Name;
651 if (CurRec->isTemplateArg(TemplateArgName)) {
652 const RecordVal *RV = CurRec->getValue(TemplateArgName);
653 assert(RV && "Template arg doesn't exist??");
654 return new VarInit(TemplateArgName, RV->getType());
655 }
656 }
657
658 if (CurMultiClass) {
659 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
660 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
661 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
662 assert(RV && "Template arg doesn't exist??");
663 return new VarInit(MCName, RV->getType());
664 }
665 }
666
667 if (Record *D = Records.getDef(Name))
668 return new DefInit(D);
669
670 Error(NameLoc, "Variable not defined: '" + Name + "'");
671 return 0;
672}
673
David Greenef1342802009-05-14 20:54:48 +0000674/// ParseOperation - Parse an operator. This returns null on error.
675///
676/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
677///
678Init *TGParser::ParseOperation(Record *CurRec) {
679 switch (Lex.getCode()) {
680 default:
681 TokError("unknown operation");
682 return 0;
683 break;
David Greene04c89a12009-05-14 22:38:31 +0000684 case tgtok::XCar:
685 case tgtok::XCdr:
686 case tgtok::XNull:
David Greene4fd89a02009-05-14 21:22:49 +0000687 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
688 UnOpInit::UnaryOp Code;
689 RecTy *Type = 0;
David Greenef1342802009-05-14 20:54:48 +0000690
David Greene4fd89a02009-05-14 21:22:49 +0000691 switch (Lex.getCode()) {
692 default: assert(0 && "Unhandled code!");
693 case tgtok::XCast:
694 Lex.Lex(); // eat the operation
695 Code = UnOpInit::CAST;
David Greenef1342802009-05-14 20:54:48 +0000696
David Greene4fd89a02009-05-14 21:22:49 +0000697 Type = ParseOperatorType();
David Greenef1342802009-05-14 20:54:48 +0000698
David Greene4fd89a02009-05-14 21:22:49 +0000699 if (Type == 0) {
David Greene04c89a12009-05-14 22:38:31 +0000700 TokError("did not get type for unary operator");
David Greene4fd89a02009-05-14 21:22:49 +0000701 return 0;
702 }
David Greenef1342802009-05-14 20:54:48 +0000703
David Greene4fd89a02009-05-14 21:22:49 +0000704 break;
David Greene04c89a12009-05-14 22:38:31 +0000705 case tgtok::XCar:
706 Lex.Lex(); // eat the operation
707 Code = UnOpInit::CAR;
708 break;
709 case tgtok::XCdr:
710 Lex.Lex(); // eat the operation
711 Code = UnOpInit::CDR;
712 break;
713 case tgtok::XNull:
714 Lex.Lex(); // eat the operation
715 Code = UnOpInit::LNULL;
716 Type = new IntRecTy;
717 break;
David Greene4fd89a02009-05-14 21:22:49 +0000718 }
719 if (Lex.getCode() != tgtok::l_paren) {
720 TokError("expected '(' after unary operator");
721 return 0;
722 }
723 Lex.Lex(); // eat the '('
David Greenef1342802009-05-14 20:54:48 +0000724
David Greene4fd89a02009-05-14 21:22:49 +0000725 Init *LHS = ParseValue(CurRec);
726 if (LHS == 0) return 0;
David Greenef1342802009-05-14 20:54:48 +0000727
David Greene04c89a12009-05-14 22:38:31 +0000728 if (Code == UnOpInit::CAR
729 || Code == UnOpInit::CDR
730 || Code == UnOpInit::LNULL) {
731 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenef2db6e22009-06-08 20:23:18 +0000732 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene04c89a12009-05-14 22:38:31 +0000733 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenef2db6e22009-06-08 20:23:18 +0000734 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
735 TokError("expected list or string type argument in unary operator");
David Greene04c89a12009-05-14 22:38:31 +0000736 return 0;
737 }
738 if (LHSt) {
739 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenef2db6e22009-06-08 20:23:18 +0000740 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
741 if (LType == 0 && SType == 0) {
742 TokError("expected list or string type argumnet in unary operator");
David Greene04c89a12009-05-14 22:38:31 +0000743 return 0;
744 }
745 }
746
747 if (Code == UnOpInit::CAR
748 || Code == UnOpInit::CDR) {
David Greenef2db6e22009-06-08 20:23:18 +0000749 if (LHSl == 0 && LHSt == 0) {
750 TokError("expected list type argumnet in unary operator");
751 return 0;
752 }
753
David Greene04c89a12009-05-14 22:38:31 +0000754 if (LHSl && LHSl->getSize() == 0) {
755 TokError("empty list argument in unary operator");
756 return 0;
757 }
758 if (LHSl) {
759 Init *Item = LHSl->getElement(0);
760 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
761 if (Itemt == 0) {
762 TokError("untyped list element in unary operator");
763 return 0;
764 }
765 if (Code == UnOpInit::CAR) {
766 Type = Itemt->getType();
767 }
768 else {
769 Type = new ListRecTy(Itemt->getType());
770 }
771 }
772 else {
773 assert(LHSt && "expected list type argument in unary operator");
774 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
775 if (LType == 0) {
776 TokError("expected list type argumnet in unary operator");
777 return 0;
778 }
779 if (Code == UnOpInit::CAR) {
780 Type = LType->getElementType();
781 }
782 else {
783 Type = LType;
784 }
785 }
786 }
787 }
788
David Greene4fd89a02009-05-14 21:22:49 +0000789 if (Lex.getCode() != tgtok::r_paren) {
790 TokError("expected ')' in unary operator");
791 return 0;
792 }
793 Lex.Lex(); // eat the ')'
794 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
795 }
David Greenef1342802009-05-14 20:54:48 +0000796
797 case tgtok::XConcat:
798 case tgtok::XSRA:
799 case tgtok::XSRL:
800 case tgtok::XSHL:
801 case tgtok::XStrConcat:
David Greene59097272009-06-08 17:00:34 +0000802 case tgtok::XRegMatch:
David Greenef1342802009-05-14 20:54:48 +0000803 case tgtok::XNameConcat: { // Value ::= !binop '(' Value ',' Value ')'
804 BinOpInit::BinaryOp Code;
805 RecTy *Type = 0;
806
807
808 switch (Lex.getCode()) {
809 default: assert(0 && "Unhandled code!");
810 case tgtok::XConcat:
811 Lex.Lex(); // eat the operation
812 Code = BinOpInit::CONCAT;
813 Type = new DagRecTy();
814 break;
815 case tgtok::XSRA:
816 Lex.Lex(); // eat the operation
817 Code = BinOpInit::SRA;
818 Type = new IntRecTy();
819 break;
820 case tgtok::XSRL:
821 Lex.Lex(); // eat the operation
822 Code = BinOpInit::SRL;
823 Type = new IntRecTy();
824 break;
825 case tgtok::XSHL:
826 Lex.Lex(); // eat the operation
827 Code = BinOpInit::SHL;
828 Type = new IntRecTy();
829 break;
830 case tgtok::XStrConcat:
831 Lex.Lex(); // eat the operation
832 Code = BinOpInit::STRCONCAT;
833 Type = new StringRecTy();
834 break;
David Greene59097272009-06-08 17:00:34 +0000835 case tgtok::XRegMatch:
836 Lex.Lex(); // eat the operation
837 Code = BinOpInit::REGMATCH;
838 Type = new IntRecTy();
839 break;
David Greenef1342802009-05-14 20:54:48 +0000840 case tgtok::XNameConcat:
841 Lex.Lex(); // eat the operation
842 Code = BinOpInit::NAMECONCAT;
843
844 Type = ParseOperatorType();
845
846 if (Type == 0) {
847 TokError("did not get type for binary operator");
848 return 0;
849 }
850
851 break;
852 }
853 if (Lex.getCode() != tgtok::l_paren) {
854 TokError("expected '(' after binary operator");
855 return 0;
856 }
857 Lex.Lex(); // eat the '('
858
859 Init *LHS = ParseValue(CurRec);
860 if (LHS == 0) return 0;
861
862 if (Lex.getCode() != tgtok::comma) {
863 TokError("expected ',' in binary operator");
864 return 0;
865 }
866 Lex.Lex(); // eat the ','
867
868 Init *RHS = ParseValue(CurRec);
869 if (RHS == 0) return 0;
870
871 if (Lex.getCode() != tgtok::r_paren) {
872 TokError("expected ')' in binary operator");
873 return 0;
874 }
875 Lex.Lex(); // eat the ')'
876 return (new BinOpInit(Code, LHS, RHS, Type))->Fold(CurRec, CurMultiClass);
877 }
878
David Greene70037ea2009-05-14 23:26:46 +0000879 case tgtok::XIf:
David Greene2c383212009-05-14 22:23:47 +0000880 case tgtok::XForEach:
David Greene289963f2009-06-08 23:05:37 +0000881 case tgtok::XPatSubst:
David Greene4f124db2009-05-14 21:54:42 +0000882 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
883 TernOpInit::TernaryOp Code;
884 RecTy *Type = 0;
David Greenef1342802009-05-14 20:54:48 +0000885
886
David Greene4f124db2009-05-14 21:54:42 +0000887 tgtok::TokKind LexCode = Lex.getCode();
888 Lex.Lex(); // eat the operation
889 switch (LexCode) {
890 default: assert(0 && "Unhandled code!");
David Greene70037ea2009-05-14 23:26:46 +0000891 case tgtok::XIf:
892 Code = TernOpInit::IF;
893 break;
David Greene2c383212009-05-14 22:23:47 +0000894 case tgtok::XForEach:
895 Code = TernOpInit::FOREACH;
896 break;
David Greene4f124db2009-05-14 21:54:42 +0000897 case tgtok::XSubst:
898 Code = TernOpInit::SUBST;
899 break;
David Greene289963f2009-06-08 23:05:37 +0000900 case tgtok::XPatSubst:
901 Code = TernOpInit::PATSUBST;
902 break;
David Greene4f124db2009-05-14 21:54:42 +0000903 }
904 if (Lex.getCode() != tgtok::l_paren) {
905 TokError("expected '(' after ternary operator");
906 return 0;
907 }
908 Lex.Lex(); // eat the '('
David Greenef1342802009-05-14 20:54:48 +0000909
David Greene4f124db2009-05-14 21:54:42 +0000910 Init *LHS = ParseValue(CurRec);
911 if (LHS == 0) return 0;
David Greenef1342802009-05-14 20:54:48 +0000912
David Greene4f124db2009-05-14 21:54:42 +0000913 if (Lex.getCode() != tgtok::comma) {
914 TokError("expected ',' in ternary operator");
915 return 0;
916 }
917 Lex.Lex(); // eat the ','
David Greenef1342802009-05-14 20:54:48 +0000918
David Greene4f124db2009-05-14 21:54:42 +0000919 Init *MHS = ParseValue(CurRec);
920 if (MHS == 0) return 0;
David Greenef1342802009-05-14 20:54:48 +0000921
David Greene4f124db2009-05-14 21:54:42 +0000922 if (Lex.getCode() != tgtok::comma) {
923 TokError("expected ',' in ternary operator");
924 return 0;
925 }
926 Lex.Lex(); // eat the ','
David Greenef1342802009-05-14 20:54:48 +0000927
David Greene4f124db2009-05-14 21:54:42 +0000928 Init *RHS = ParseValue(CurRec);
929 if (RHS == 0) return 0;
David Greenef1342802009-05-14 20:54:48 +0000930
David Greene4f124db2009-05-14 21:54:42 +0000931 if (Lex.getCode() != tgtok::r_paren) {
932 TokError("expected ')' in binary operator");
933 return 0;
934 }
935 Lex.Lex(); // eat the ')'
David Greenef1342802009-05-14 20:54:48 +0000936
David Greene4f124db2009-05-14 21:54:42 +0000937 switch (LexCode) {
938 default: assert(0 && "Unhandled code!");
David Greene70037ea2009-05-14 23:26:46 +0000939 case tgtok::XIf: {
940 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
941 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
942 if (MHSt == 0 || RHSt == 0) {
943 TokError("could not get type for !if");
944 return 0;
945 }
946 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
947 Type = RHSt->getType();
948 }
949 else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
950 Type = MHSt->getType();
951 }
952 else {
953 TokError("inconsistent types for !if");
954 return 0;
955 }
956 break;
957 }
David Greene2c383212009-05-14 22:23:47 +0000958 case tgtok::XForEach: {
959 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
960 if (MHSt == 0) {
961 TokError("could not get type for !foreach");
962 return 0;
963 }
964 Type = MHSt->getType();
965 break;
966 }
David Greene4f124db2009-05-14 21:54:42 +0000967 case tgtok::XSubst: {
968 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
969 if (RHSt == 0) {
970 TokError("could not get type for !subst");
971 return 0;
972 }
973 Type = RHSt->getType();
974 break;
975 }
David Greene289963f2009-06-08 23:05:37 +0000976 case tgtok::XPatSubst: {
977 Type = new StringRecTy;
978 break;
979 }
David Greene4f124db2009-05-14 21:54:42 +0000980 }
981 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec, CurMultiClass);
982 }
David Greenef1342802009-05-14 20:54:48 +0000983 }
984 TokError("could not parse operation");
985 return 0;
986}
987
988/// ParseOperatorType - Parse a type for an operator. This returns
989/// null on error.
990///
991/// OperatorType ::= '<' Type '>'
992///
993RecTy *TGParser::ParseOperatorType(void) {
994 RecTy *Type = 0;
995
996 if (Lex.getCode() != tgtok::less) {
997 TokError("expected type name for operator");
998 return 0;
999 }
1000 Lex.Lex(); // eat the <
1001
1002 Type = ParseType();
1003
1004 if (Type == 0) {
1005 TokError("expected type name for operator");
1006 return 0;
1007 }
1008
1009 if (Lex.getCode() != tgtok::greater) {
1010 TokError("expected type name for operator");
1011 return 0;
1012 }
1013 Lex.Lex(); // eat the >
1014
1015 return Type;
1016}
1017
1018
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001019/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1020///
1021/// SimpleValue ::= IDValue
1022/// SimpleValue ::= INTVAL
Chris Lattnerbfc6bf62009-03-11 17:08:13 +00001023/// SimpleValue ::= STRVAL+
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001024/// SimpleValue ::= CODEFRAGMENT
1025/// SimpleValue ::= '?'
1026/// SimpleValue ::= '{' ValueList '}'
1027/// SimpleValue ::= ID '<' ValueListNE '>'
1028/// SimpleValue ::= '[' ValueList ']'
1029/// SimpleValue ::= '(' IDValue DagArgList ')'
1030/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1031/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1032/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1033/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1034/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1035///
David Greenef2db6e22009-06-08 20:23:18 +00001036Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001037 Init *R = 0;
1038 switch (Lex.getCode()) {
1039 default: TokError("Unknown token when parsing a value"); break;
1040 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerbfc6bf62009-03-11 17:08:13 +00001041 case tgtok::StrVal: {
1042 std::string Val = Lex.getCurStrVal();
1043 Lex.Lex();
1044
Jim Grosbachda95ec82009-03-26 16:17:51 +00001045 // Handle multiple consecutive concatenated strings.
Chris Lattnerbfc6bf62009-03-11 17:08:13 +00001046 while (Lex.getCode() == tgtok::StrVal) {
1047 Val += Lex.getCurStrVal();
1048 Lex.Lex();
1049 }
1050
1051 R = new StringInit(Val);
1052 break;
1053 }
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001054 case tgtok::CodeFragment:
1055 R = new CodeInit(Lex.getCurStrVal()); Lex.Lex(); break;
1056 case tgtok::question: R = new UnsetInit(); Lex.Lex(); break;
1057 case tgtok::Id: {
Chris Lattnere7205cd2009-03-13 16:01:53 +00001058 TGLoc NameLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001059 std::string Name = Lex.getCurStrVal();
1060 if (Lex.Lex() != tgtok::less) // consume the Id.
1061 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
1062
1063 // Value ::= ID '<' ValueListNE '>'
1064 if (Lex.Lex() == tgtok::greater) {
1065 TokError("expected non-empty value list");
1066 return 0;
1067 }
David Greenef2db6e22009-06-08 20:23:18 +00001068
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001069 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1070 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1071 // body.
1072 Record *Class = Records.getClass(Name);
1073 if (!Class) {
1074 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1075 return 0;
1076 }
David Greenef2db6e22009-06-08 20:23:18 +00001077
1078 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1079 if (ValueList.empty()) return 0;
1080
1081 if (Lex.getCode() != tgtok::greater) {
1082 TokError("expected '>' at end of value list");
1083 return 0;
1084 }
1085 Lex.Lex(); // eat the '>'
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001086
1087 // Create the new record, set it as CurRec temporarily.
1088 static unsigned AnonCounter = 0;
Chris Lattner6656e802009-03-13 16:09:24 +00001089 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001090 SubClassReference SCRef;
1091 SCRef.RefLoc = NameLoc;
1092 SCRef.Rec = Class;
1093 SCRef.TemplateArgs = ValueList;
1094 // Add info about the subclass to NewRec.
1095 if (AddSubClass(NewRec, SCRef))
1096 return 0;
1097 NewRec->resolveReferences();
1098 Records.addDef(NewRec);
1099
1100 // The result of the expression is a reference to the new record.
1101 return new DefInit(NewRec);
1102 }
1103 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattnere7205cd2009-03-13 16:01:53 +00001104 TGLoc BraceLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001105 Lex.Lex(); // eat the '{'
1106 std::vector<Init*> Vals;
1107
1108 if (Lex.getCode() != tgtok::r_brace) {
1109 Vals = ParseValueList(CurRec);
1110 if (Vals.empty()) return 0;
1111 }
1112 if (Lex.getCode() != tgtok::r_brace) {
1113 TokError("expected '}' at end of bit list value");
1114 return 0;
1115 }
1116 Lex.Lex(); // eat the '}'
1117
1118 BitsInit *Result = new BitsInit(Vals.size());
1119 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1120 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1121 if (Bit == 0) {
Chris Lattner6325a022007-11-22 21:06:59 +00001122 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1123 ") is not convertable to a bit");
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001124 return 0;
1125 }
1126 Result->setBit(Vals.size()-i-1, Bit);
1127 }
1128 return Result;
1129 }
1130 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1131 Lex.Lex(); // eat the '['
1132 std::vector<Init*> Vals;
1133
David Greenef2db6e22009-06-08 20:23:18 +00001134 RecTy *DeducedEltTy = 0;
1135 ListRecTy *GivenListTy = 0;
1136
1137 if (ItemType != 0) {
1138 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1139 if (ListType == 0) {
1140 std::stringstream s;
1141 s << "Type mismatch for list, expected list type, got "
1142 << ItemType->getAsString();
1143 TokError(s.str());
1144 }
1145 GivenListTy = ListType;
1146 }
1147
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001148 if (Lex.getCode() != tgtok::r_square) {
David Greenef2db6e22009-06-08 20:23:18 +00001149 Vals = ParseValueList(CurRec, 0, GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001150 if (Vals.empty()) return 0;
1151 }
1152 if (Lex.getCode() != tgtok::r_square) {
1153 TokError("expected ']' at end of list value");
1154 return 0;
1155 }
1156 Lex.Lex(); // eat the ']'
David Greenef2db6e22009-06-08 20:23:18 +00001157
1158 RecTy *GivenEltTy = 0;
1159 if (Lex.getCode() == tgtok::less) {
1160 // Optional list element type
1161 Lex.Lex(); // eat the '<'
1162
1163 GivenEltTy = ParseType();
1164 if (GivenEltTy == 0) {
1165 // Couldn't parse element type
1166 return 0;
1167 }
1168
1169 if (Lex.getCode() != tgtok::greater) {
1170 TokError("expected '>' at end of list element type");
1171 return 0;
1172 }
1173 Lex.Lex(); // eat the '>'
1174 }
1175
1176 // Check elements
1177 RecTy *EltTy = 0;
1178 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1179 i != ie;
1180 ++i) {
1181 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1182 if (TArg == 0) {
1183 TokError("Untyped list element");
1184 return 0;
1185 }
1186 if (EltTy != 0) {
1187 EltTy = resolveTypes(EltTy, TArg->getType());
1188 if (EltTy == 0) {
1189 TokError("Incompatible types in list elements");
1190 return 0;
1191 }
1192 }
1193 else {
1194 EltTy = TArg->getType();
1195 }
1196 }
1197
1198 if (GivenEltTy != 0) {
1199 if (EltTy != 0) {
1200 // Verify consistency
1201 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1202 TokError("Incompatible types in list elements");
1203 return 0;
1204 }
1205 }
1206 EltTy = GivenEltTy;
1207 }
1208
1209 if (EltTy == 0) {
1210 if (ItemType == 0) {
1211 TokError("No type for list");
1212 return 0;
1213 }
1214 DeducedEltTy = GivenListTy->getElementType();
1215 }
1216 else {
1217 // Make sure the deduced type is compatible with the given type
1218 if (GivenListTy) {
1219 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1220 TokError("Element type mismatch for list");
1221 return 0;
1222 }
1223 }
1224 DeducedEltTy = EltTy;
1225 }
1226
1227 return new ListInit(Vals, DeducedEltTy);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001228 }
1229 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1230 Lex.Lex(); // eat the '('
David Greene790e1222009-04-22 20:18:10 +00001231 if (Lex.getCode() != tgtok::Id
David Greene4fd89a02009-05-14 21:22:49 +00001232 && Lex.getCode() != tgtok::XCast
David Greene790e1222009-04-22 20:18:10 +00001233 && Lex.getCode() != tgtok::XNameConcat) {
Chris Lattner3752d522008-04-10 04:48:34 +00001234 TokError("expected identifier in dag init");
1235 return 0;
1236 }
1237
David Greene790e1222009-04-22 20:18:10 +00001238 Init *Operator = 0;
1239 if (Lex.getCode() == tgtok::Id) {
1240 Operator = ParseIDValue(CurRec);
1241 if (Operator == 0) return 0;
1242 }
1243 else {
David Greenef1342802009-05-14 20:54:48 +00001244 Operator = ParseOperation(CurRec);
1245 if (Operator == 0) return 0;
David Greene790e1222009-04-22 20:18:10 +00001246 }
1247
Nate Begeman0e263b72009-03-19 05:21:56 +00001248 // If the operator name is present, parse it.
1249 std::string OperatorName;
1250 if (Lex.getCode() == tgtok::colon) {
1251 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1252 TokError("expected variable name in dag operator");
1253 return 0;
1254 }
1255 OperatorName = Lex.getCurStrVal();
1256 Lex.Lex(); // eat the VarName.
1257 }
1258
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001259 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1260 if (Lex.getCode() != tgtok::r_paren) {
1261 DagArgs = ParseDagArgList(CurRec);
1262 if (DagArgs.empty()) return 0;
1263 }
1264
1265 if (Lex.getCode() != tgtok::r_paren) {
1266 TokError("expected ')' in dag init");
1267 return 0;
1268 }
1269 Lex.Lex(); // eat the ')'
1270
Nate Begeman0e263b72009-03-19 05:21:56 +00001271 return new DagInit(Operator, OperatorName, DagArgs);
David Greenef1342802009-05-14 20:54:48 +00001272 break;
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001273 }
David Greenef1342802009-05-14 20:54:48 +00001274
David Greene04c89a12009-05-14 22:38:31 +00001275 case tgtok::XCar:
1276 case tgtok::XCdr:
1277 case tgtok::XNull:
David Greene4fd89a02009-05-14 21:22:49 +00001278 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001279 case tgtok::XConcat:
1280 case tgtok::XSRA:
1281 case tgtok::XSRL:
1282 case tgtok::XSHL:
David Greene790e1222009-04-22 20:18:10 +00001283 case tgtok::XStrConcat:
David Greene59097272009-06-08 17:00:34 +00001284 case tgtok::XRegMatch:
David Greene4f124db2009-05-14 21:54:42 +00001285 case tgtok::XNameConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene70037ea2009-05-14 23:26:46 +00001286 case tgtok::XIf:
David Greene2c383212009-05-14 22:23:47 +00001287 case tgtok::XForEach:
David Greene289963f2009-06-08 23:05:37 +00001288 case tgtok::XPatSubst:
David Greene4f124db2009-05-14 21:54:42 +00001289 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greenef1342802009-05-14 20:54:48 +00001290 return ParseOperation(CurRec);
1291 break;
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001292 }
1293 }
1294
1295 return R;
1296}
1297
1298/// ParseValue - Parse a tblgen value. This returns null on error.
1299///
1300/// Value ::= SimpleValue ValueSuffix*
1301/// ValueSuffix ::= '{' BitList '}'
1302/// ValueSuffix ::= '[' BitList ']'
1303/// ValueSuffix ::= '.' ID
1304///
David Greenef2db6e22009-06-08 20:23:18 +00001305Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1306 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001307 if (Result == 0) return 0;
1308
1309 // Parse the suffixes now if present.
1310 while (1) {
1311 switch (Lex.getCode()) {
1312 default: return Result;
1313 case tgtok::l_brace: {
Chris Lattnere7205cd2009-03-13 16:01:53 +00001314 TGLoc CurlyLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001315 Lex.Lex(); // eat the '{'
1316 std::vector<unsigned> Ranges = ParseRangeList();
1317 if (Ranges.empty()) return 0;
1318
1319 // Reverse the bitlist.
1320 std::reverse(Ranges.begin(), Ranges.end());
1321 Result = Result->convertInitializerBitRange(Ranges);
1322 if (Result == 0) {
1323 Error(CurlyLoc, "Invalid bit range for value");
1324 return 0;
1325 }
1326
1327 // Eat the '}'.
1328 if (Lex.getCode() != tgtok::r_brace) {
1329 TokError("expected '}' at end of bit range list");
1330 return 0;
1331 }
1332 Lex.Lex();
1333 break;
1334 }
1335 case tgtok::l_square: {
Chris Lattnere7205cd2009-03-13 16:01:53 +00001336 TGLoc SquareLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001337 Lex.Lex(); // eat the '['
1338 std::vector<unsigned> Ranges = ParseRangeList();
1339 if (Ranges.empty()) return 0;
1340
1341 Result = Result->convertInitListSlice(Ranges);
1342 if (Result == 0) {
1343 Error(SquareLoc, "Invalid range for list slice");
1344 return 0;
1345 }
1346
1347 // Eat the ']'.
1348 if (Lex.getCode() != tgtok::r_square) {
1349 TokError("expected ']' at end of list slice");
1350 return 0;
1351 }
1352 Lex.Lex();
1353 break;
1354 }
1355 case tgtok::period:
1356 if (Lex.Lex() != tgtok::Id) { // eat the .
1357 TokError("expected field identifier after '.'");
1358 return 0;
1359 }
1360 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001361 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner6325a022007-11-22 21:06:59 +00001362 Result->getAsString() + "'");
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001363 return 0;
1364 }
1365 Result = new FieldInit(Result, Lex.getCurStrVal());
1366 Lex.Lex(); // eat field name
1367 break;
1368 }
1369 }
1370}
1371
1372/// ParseDagArgList - Parse the argument list for a dag literal expression.
1373///
1374/// ParseDagArgList ::= Value (':' VARNAME)?
1375/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
1376std::vector<std::pair<llvm::Init*, std::string> >
1377TGParser::ParseDagArgList(Record *CurRec) {
1378 std::vector<std::pair<llvm::Init*, std::string> > Result;
1379
1380 while (1) {
1381 Init *Val = ParseValue(CurRec);
1382 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
1383
1384 // If the variable name is present, add it.
1385 std::string VarName;
1386 if (Lex.getCode() == tgtok::colon) {
1387 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1388 TokError("expected variable name in dag literal");
1389 return std::vector<std::pair<llvm::Init*, std::string> >();
1390 }
1391 VarName = Lex.getCurStrVal();
1392 Lex.Lex(); // eat the VarName.
1393 }
1394
1395 Result.push_back(std::make_pair(Val, VarName));
1396
1397 if (Lex.getCode() != tgtok::comma) break;
1398 Lex.Lex(); // eat the ','
1399 }
1400
1401 return Result;
1402}
1403
1404
1405/// ParseValueList - Parse a comma separated list of values, returning them as a
1406/// vector. Note that this always expects to be able to parse at least one
1407/// value. It returns an empty list if this is not possible.
1408///
1409/// ValueList ::= Value (',' Value)
1410///
David Greenef2db6e22009-06-08 20:23:18 +00001411std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, RecTy *EltTy) {
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001412 std::vector<Init*> Result;
David Greenef2db6e22009-06-08 20:23:18 +00001413 RecTy *ItemType = EltTy;
1414 int ArgN = 0;
1415 if (ArgsRec != 0 && EltTy == 0) {
1416 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1417 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1418 assert(RV && "Template argument record not found??");
1419 ItemType = RV->getType();
1420 ++ArgN;
1421 }
1422 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001423 if (Result.back() == 0) return std::vector<Init*>();
1424
1425 while (Lex.getCode() == tgtok::comma) {
1426 Lex.Lex(); // Eat the comma
1427
David Greenef2db6e22009-06-08 20:23:18 +00001428 if (ArgsRec != 0 && EltTy == 0) {
1429 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1430 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1431 assert(RV && "Template argument record not found??");
1432 ItemType = RV->getType();
1433 ++ArgN;
1434 }
1435 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001436 if (Result.back() == 0) return std::vector<Init*>();
1437 }
1438
1439 return Result;
1440}
1441
1442
1443
1444/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1445/// empty string on error. This can happen in a number of different context's,
1446/// including within a def or in the template args for a def (which which case
1447/// CurRec will be non-null) and within the template args for a multiclass (in
1448/// which case CurRec will be null, but CurMultiClass will be set). This can
1449/// also happen within a def that is within a multiclass, which will set both
1450/// CurRec and CurMultiClass.
1451///
1452/// Declaration ::= FIELD? Type ID ('=' Value)?
1453///
1454std::string TGParser::ParseDeclaration(Record *CurRec,
1455 bool ParsingTemplateArgs) {
1456 // Read the field prefix if present.
1457 bool HasField = Lex.getCode() == tgtok::Field;
1458 if (HasField) Lex.Lex();
1459
1460 RecTy *Type = ParseType();
1461 if (Type == 0) return "";
1462
1463 if (Lex.getCode() != tgtok::Id) {
1464 TokError("Expected identifier in declaration");
1465 return "";
1466 }
1467
Chris Lattnere7205cd2009-03-13 16:01:53 +00001468 TGLoc IdLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001469 std::string DeclName = Lex.getCurStrVal();
1470 Lex.Lex();
1471
1472 if (ParsingTemplateArgs) {
1473 if (CurRec) {
1474 DeclName = CurRec->getName() + ":" + DeclName;
1475 } else {
1476 assert(CurMultiClass);
1477 }
1478 if (CurMultiClass)
1479 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1480 }
1481
1482 // Add the value.
1483 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1484 return "";
1485
1486 // If a value is present, parse it.
1487 if (Lex.getCode() == tgtok::equal) {
1488 Lex.Lex();
Chris Lattnere7205cd2009-03-13 16:01:53 +00001489 TGLoc ValLoc = Lex.getLoc();
David Greenef2db6e22009-06-08 20:23:18 +00001490 Init *Val = ParseValue(CurRec, Type);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001491 if (Val == 0 ||
1492 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1493 return "";
1494 }
1495
1496 return DeclName;
1497}
1498
1499/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1500/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1501/// template args for a def, which may or may not be in a multiclass. If null,
1502/// these are the template args for a multiclass.
1503///
1504/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
1505///
1506bool TGParser::ParseTemplateArgList(Record *CurRec) {
1507 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1508 Lex.Lex(); // eat the '<'
1509
1510 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
1511
1512 // Read the first declaration.
1513 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1514 if (TemplArg.empty())
1515 return true;
1516
1517 TheRecToAddTo->addTemplateArg(TemplArg);
1518
1519 while (Lex.getCode() == tgtok::comma) {
1520 Lex.Lex(); // eat the ','
1521
1522 // Read the following declarations.
1523 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1524 if (TemplArg.empty())
1525 return true;
1526 TheRecToAddTo->addTemplateArg(TemplArg);
1527 }
1528
1529 if (Lex.getCode() != tgtok::greater)
1530 return TokError("expected '>' at end of template argument list");
1531 Lex.Lex(); // eat the '>'.
1532 return false;
1533}
1534
1535
1536/// ParseBodyItem - Parse a single item at within the body of a def or class.
1537///
1538/// BodyItem ::= Declaration ';'
1539/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1540bool TGParser::ParseBodyItem(Record *CurRec) {
1541 if (Lex.getCode() != tgtok::Let) {
1542 if (ParseDeclaration(CurRec, false).empty())
1543 return true;
1544
1545 if (Lex.getCode() != tgtok::semi)
1546 return TokError("expected ';' after declaration");
1547 Lex.Lex();
1548 return false;
1549 }
1550
1551 // LET ID OptionalRangeList '=' Value ';'
1552 if (Lex.Lex() != tgtok::Id)
1553 return TokError("expected field identifier after let");
1554
Chris Lattnere7205cd2009-03-13 16:01:53 +00001555 TGLoc IdLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001556 std::string FieldName = Lex.getCurStrVal();
1557 Lex.Lex(); // eat the field name.
1558
1559 std::vector<unsigned> BitList;
1560 if (ParseOptionalBitList(BitList))
1561 return true;
1562 std::reverse(BitList.begin(), BitList.end());
1563
1564 if (Lex.getCode() != tgtok::equal)
1565 return TokError("expected '=' in let expression");
1566 Lex.Lex(); // eat the '='.
1567
David Greenef2db6e22009-06-08 20:23:18 +00001568 RecordVal *Field = CurRec->getValue(FieldName);
1569 if (Field == 0)
1570 return TokError("Value '" + FieldName + "' unknown!");
1571
1572 RecTy *Type = Field->getType();
1573
1574 Init *Val = ParseValue(CurRec, Type);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001575 if (Val == 0) return true;
1576
1577 if (Lex.getCode() != tgtok::semi)
1578 return TokError("expected ';' after let expression");
1579 Lex.Lex();
1580
1581 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1582}
1583
1584/// ParseBody - Read the body of a class or def. Return true on error, false on
1585/// success.
1586///
1587/// Body ::= ';'
1588/// Body ::= '{' BodyList '}'
1589/// BodyList BodyItem*
1590///
1591bool TGParser::ParseBody(Record *CurRec) {
1592 // If this is a null definition, just eat the semi and return.
1593 if (Lex.getCode() == tgtok::semi) {
1594 Lex.Lex();
1595 return false;
1596 }
1597
1598 if (Lex.getCode() != tgtok::l_brace)
1599 return TokError("Expected ';' or '{' to start body");
1600 // Eat the '{'.
1601 Lex.Lex();
1602
1603 while (Lex.getCode() != tgtok::r_brace)
1604 if (ParseBodyItem(CurRec))
1605 return true;
1606
1607 // Eat the '}'.
1608 Lex.Lex();
1609 return false;
1610}
1611
1612/// ParseObjectBody - Parse the body of a def or class. This consists of an
1613/// optional ClassList followed by a Body. CurRec is the current def or class
1614/// that is being parsed.
1615///
1616/// ObjectBody ::= BaseClassList Body
1617/// BaseClassList ::= /*empty*/
1618/// BaseClassList ::= ':' BaseClassListNE
1619/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1620///
1621bool TGParser::ParseObjectBody(Record *CurRec) {
1622 // If there is a baseclass list, read it.
1623 if (Lex.getCode() == tgtok::colon) {
1624 Lex.Lex();
1625
1626 // Read all of the subclasses.
1627 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1628 while (1) {
1629 // Check for error.
1630 if (SubClass.Rec == 0) return true;
1631
1632 // Add it.
1633 if (AddSubClass(CurRec, SubClass))
1634 return true;
1635
1636 if (Lex.getCode() != tgtok::comma) break;
1637 Lex.Lex(); // eat ','.
1638 SubClass = ParseSubClassReference(CurRec, false);
1639 }
1640 }
1641
1642 // Process any variables on the let stack.
1643 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1644 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1645 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1646 LetStack[i][j].Bits, LetStack[i][j].Value))
1647 return true;
1648
1649 return ParseBody(CurRec);
1650}
1651
1652
1653/// ParseDef - Parse and return a top level or multiclass def, return the record
1654/// corresponding to it. This returns null on error.
1655///
1656/// DefInst ::= DEF ObjectName ObjectBody
1657///
1658llvm::Record *TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattnere7205cd2009-03-13 16:01:53 +00001659 TGLoc DefLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001660 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
1661 Lex.Lex(); // Eat the 'def' token.
1662
1663 // Parse ObjectName and make a record for it.
Chris Lattner6656e802009-03-13 16:09:24 +00001664 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001665
1666 if (!CurMultiClass) {
1667 // Top-level def definition.
1668
1669 // Ensure redefinition doesn't happen.
1670 if (Records.getDef(CurRec->getName())) {
1671 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
1672 return 0;
1673 }
1674 Records.addDef(CurRec);
1675 } else {
1676 // Otherwise, a def inside a multiclass, add it to the multiclass.
1677 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1678 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1679 Error(DefLoc, "def '" + CurRec->getName() +
1680 "' already defined in this multiclass!");
1681 return 0;
1682 }
1683 CurMultiClass->DefPrototypes.push_back(CurRec);
1684 }
1685
1686 if (ParseObjectBody(CurRec))
1687 return 0;
1688
1689 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1690 CurRec->resolveReferences();
1691
1692 // If ObjectBody has template arguments, it's an error.
1693 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
1694 return CurRec;
1695}
1696
1697
1698/// ParseClass - Parse a tblgen class definition.
1699///
1700/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
1701///
1702bool TGParser::ParseClass() {
1703 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
1704 Lex.Lex();
1705
1706 if (Lex.getCode() != tgtok::Id)
1707 return TokError("expected class name after 'class' keyword");
1708
1709 Record *CurRec = Records.getClass(Lex.getCurStrVal());
1710 if (CurRec) {
1711 // If the body was previously defined, this is an error.
1712 if (!CurRec->getValues().empty() ||
1713 !CurRec->getSuperClasses().empty() ||
1714 !CurRec->getTemplateArgs().empty())
1715 return TokError("Class '" + CurRec->getName() + "' already defined");
1716 } else {
1717 // If this is the first reference to this class, create and add it.
Chris Lattner6656e802009-03-13 16:09:24 +00001718 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001719 Records.addClass(CurRec);
1720 }
1721 Lex.Lex(); // eat the name.
1722
1723 // If there are template args, parse them.
1724 if (Lex.getCode() == tgtok::less)
1725 if (ParseTemplateArgList(CurRec))
1726 return true;
1727
1728 // Finally, parse the object body.
1729 return ParseObjectBody(CurRec);
1730}
1731
1732/// ParseLetList - Parse a non-empty list of assignment expressions into a list
1733/// of LetRecords.
1734///
1735/// LetList ::= LetItem (',' LetItem)*
1736/// LetItem ::= ID OptionalRangeList '=' Value
1737///
1738std::vector<LetRecord> TGParser::ParseLetList() {
1739 std::vector<LetRecord> Result;
1740
1741 while (1) {
1742 if (Lex.getCode() != tgtok::Id) {
1743 TokError("expected identifier in let definition");
1744 return std::vector<LetRecord>();
1745 }
1746 std::string Name = Lex.getCurStrVal();
Chris Lattnere7205cd2009-03-13 16:01:53 +00001747 TGLoc NameLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001748 Lex.Lex(); // Eat the identifier.
1749
1750 // Check for an optional RangeList.
1751 std::vector<unsigned> Bits;
1752 if (ParseOptionalRangeList(Bits))
1753 return std::vector<LetRecord>();
1754 std::reverse(Bits.begin(), Bits.end());
1755
1756 if (Lex.getCode() != tgtok::equal) {
1757 TokError("expected '=' in let expression");
1758 return std::vector<LetRecord>();
1759 }
1760 Lex.Lex(); // eat the '='.
1761
1762 Init *Val = ParseValue(0);
1763 if (Val == 0) return std::vector<LetRecord>();
1764
1765 // Now that we have everything, add the record.
1766 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
1767
1768 if (Lex.getCode() != tgtok::comma)
1769 return Result;
1770 Lex.Lex(); // eat the comma.
1771 }
1772}
1773
1774/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
1775/// different related productions.
1776///
1777/// Object ::= LET LetList IN '{' ObjectList '}'
1778/// Object ::= LET LetList IN Object
1779///
1780bool TGParser::ParseTopLevelLet() {
1781 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1782 Lex.Lex();
1783
1784 // Add this entry to the let stack.
1785 std::vector<LetRecord> LetInfo = ParseLetList();
1786 if (LetInfo.empty()) return true;
1787 LetStack.push_back(LetInfo);
1788
1789 if (Lex.getCode() != tgtok::In)
1790 return TokError("expected 'in' at end of top-level 'let'");
1791 Lex.Lex();
1792
1793 // If this is a scalar let, just handle it now
1794 if (Lex.getCode() != tgtok::l_brace) {
1795 // LET LetList IN Object
1796 if (ParseObject())
1797 return true;
1798 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattnere7205cd2009-03-13 16:01:53 +00001799 TGLoc BraceLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001800 // Otherwise, this is a group let.
1801 Lex.Lex(); // eat the '{'.
1802
1803 // Parse the object list.
1804 if (ParseObjectList())
1805 return true;
1806
1807 if (Lex.getCode() != tgtok::r_brace) {
1808 TokError("expected '}' at end of top level let command");
1809 return Error(BraceLoc, "to match this '{'");
1810 }
1811 Lex.Lex();
1812 }
1813
1814 // Outside this let scope, this let block is not active.
1815 LetStack.pop_back();
1816 return false;
1817}
1818
1819/// ParseMultiClassDef - Parse a def in a multiclass context.
1820///
1821/// MultiClassDef ::= DefInst
1822///
1823bool TGParser::ParseMultiClassDef(MultiClass *CurMC) {
1824 if (Lex.getCode() != tgtok::Def)
1825 return TokError("expected 'def' in multiclass body");
1826
1827 Record *D = ParseDef(CurMC);
1828 if (D == 0) return true;
1829
1830 // Copy the template arguments for the multiclass into the def.
1831 const std::vector<std::string> &TArgs = CurMC->Rec.getTemplateArgs();
1832
1833 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1834 const RecordVal *RV = CurMC->Rec.getValue(TArgs[i]);
1835 assert(RV && "Template arg doesn't exist?");
1836 D->addValue(*RV);
1837 }
1838
1839 return false;
1840}
1841
1842/// ParseMultiClass - Parse a multiclass definition.
1843///
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001844/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1845/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001846///
1847bool TGParser::ParseMultiClass() {
1848 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1849 Lex.Lex(); // Eat the multiclass token.
1850
1851 if (Lex.getCode() != tgtok::Id)
1852 return TokError("expected identifier after multiclass for name");
1853 std::string Name = Lex.getCurStrVal();
1854
1855 if (MultiClasses.count(Name))
1856 return TokError("multiclass '" + Name + "' already defined");
1857
Chris Lattner6656e802009-03-13 16:09:24 +00001858 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001859 Lex.Lex(); // Eat the identifier.
1860
1861 // If there are template args, parse them.
1862 if (Lex.getCode() == tgtok::less)
1863 if (ParseTemplateArgList(0))
1864 return true;
1865
David Greenee3269dd2009-04-24 16:55:41 +00001866 bool inherits = false;
1867
David Greene60b5acc2009-04-22 16:42:54 +00001868 // If there are submulticlasses, parse them.
1869 if (Lex.getCode() == tgtok::colon) {
David Greenee3269dd2009-04-24 16:55:41 +00001870 inherits = true;
1871
David Greene60b5acc2009-04-22 16:42:54 +00001872 Lex.Lex();
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001873
David Greene60b5acc2009-04-22 16:42:54 +00001874 // Read all of the submulticlasses.
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001875 SubMultiClassReference SubMultiClass =
1876 ParseSubMultiClassReference(CurMultiClass);
David Greene60b5acc2009-04-22 16:42:54 +00001877 while (1) {
1878 // Check for error.
1879 if (SubMultiClass.MC == 0) return true;
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001880
David Greene60b5acc2009-04-22 16:42:54 +00001881 // Add it.
1882 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1883 return true;
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001884
David Greene60b5acc2009-04-22 16:42:54 +00001885 if (Lex.getCode() != tgtok::comma) break;
1886 Lex.Lex(); // eat ','.
1887 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1888 }
1889 }
1890
David Greenee3269dd2009-04-24 16:55:41 +00001891 if (Lex.getCode() != tgtok::l_brace) {
1892 if (!inherits)
1893 return TokError("expected '{' in multiclass definition");
1894 else
1895 if (Lex.getCode() != tgtok::semi)
1896 return TokError("expected ';' in multiclass definition");
1897 else
1898 Lex.Lex(); // eat the ';'.
1899 }
1900 else {
1901 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1902 return TokError("multiclass must contain at least one def");
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001903
David Greenee3269dd2009-04-24 16:55:41 +00001904 while (Lex.getCode() != tgtok::r_brace)
1905 if (ParseMultiClassDef(CurMultiClass))
1906 return true;
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001907
David Greenee3269dd2009-04-24 16:55:41 +00001908 Lex.Lex(); // eat the '}'.
1909 }
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001910
1911 CurMultiClass = 0;
1912 return false;
1913}
1914
1915/// ParseDefm - Parse the instantiation of a multiclass.
1916///
1917/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1918///
1919bool TGParser::ParseDefm() {
1920 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
1921 if (Lex.Lex() != tgtok::Id) // eat the defm.
1922 return TokError("expected identifier after defm");
1923
Chris Lattnere7205cd2009-03-13 16:01:53 +00001924 TGLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001925 std::string DefmPrefix = Lex.getCurStrVal();
1926 if (Lex.Lex() != tgtok::colon)
1927 return TokError("expected ':' after defm identifier");
1928
1929 // eat the colon.
1930 Lex.Lex();
1931
Chris Lattnere7205cd2009-03-13 16:01:53 +00001932 TGLoc SubClassLoc = Lex.getLoc();
Chris Lattner6b7d76a2007-11-22 20:49:04 +00001933 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene080d2152009-04-22 22:17:51 +00001934
1935 while (1) {
1936 if (Ref.Rec == 0) return true;
1937
1938 // To instantiate a multiclass, we need to first get the multiclass, then
1939 // instantiate each def contained in the multiclass with the SubClassRef
1940 // template parameters.
1941 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1942 assert(MC && "Didn't lookup multiclass correctly?");
1943 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
1944
1945 // Verify that the correct number of template arguments were specified.
1946 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1947 if (TArgs.size() < TemplateVals.size())
1948 return Error(SubClassLoc,
1949 "more template args specified than multiclass expects");
1950
1951 // Loop over all the def's in the multiclass, instantiating each one.
1952 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1953 Record *DefProto = MC->DefPrototypes[i];
1954
David Greene93ea95e2009-05-05 16:28:25 +00001955 // Add in the defm name
1956 std::string DefName = DefProto->getName();
1957 std::string::size_type idx = DefName.find("#NAME#");
1958 if (idx != std::string::npos) {
1959 DefName.replace(idx, 6, DefmPrefix);
1960 }
1961 else {
1962 // Add the suffix to the defm name to get the new name.
1963 DefName = DefmPrefix + DefName;
1964 }
1965
1966 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene080d2152009-04-22 22:17:51 +00001967
1968 SubClassReference Ref;
1969 Ref.RefLoc = DefmPrefixLoc;
1970 Ref.Rec = DefProto;
1971 AddSubClass(CurRec, Ref);
1972
1973 // Loop over all of the template arguments, setting them to the specified
1974 // value or leaving them as the default if necessary.
1975 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001976 // Check if a value is specified for this temp-arg.
1977 if (i < TemplateVals.size()) {
David Greene080d2152009-04-22 22:17:51 +00001978 // Set it now.
1979 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1980 TemplateVals[i]))
1981 return true;
1982
1983 // Resolve it next.
1984 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1985
1986 // Now remove it.
1987 CurRec->removeValue(TArgs[i]);
1988
1989 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilsona7fa8b32009-04-28 19:41:44 +00001990 return Error(SubClassLoc,
1991 "value not specified for template argument #"+
David Greene080d2152009-04-22 22:17:51 +00001992 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1993 MC->Rec.getName() + "'");
1994 }
1995 }
1996
1997 // If the mdef is inside a 'let' expression, add to each def.
1998 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1999 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2000 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2001 LetStack[i][j].Bits, LetStack[i][j].Value)) {
2002 Error(DefmPrefixLoc, "when instantiating this defm");
2003 return true;
2004 }
2005
2006 // Ensure redefinition doesn't happen.
2007 if (Records.getDef(CurRec->getName()))
2008 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2009 "' already defined, instantiating defm with subdef '" +
2010 DefProto->getName() + "'");
2011 Records.addDef(CurRec);
2012 CurRec->resolveReferences();
2013 }
2014
2015 if (Lex.getCode() != tgtok::comma) break;
2016 Lex.Lex(); // eat ','.
2017
2018 SubClassLoc = Lex.getLoc();
2019 Ref = ParseSubClassReference(0, true);
2020 }
2021
Chris Lattner6b7d76a2007-11-22 20:49:04 +00002022 if (Lex.getCode() != tgtok::semi)
2023 return TokError("expected ';' at end of defm");
2024 Lex.Lex();
2025
Chris Lattner6b7d76a2007-11-22 20:49:04 +00002026 return false;
2027}
2028
2029/// ParseObject
2030/// Object ::= ClassInst
2031/// Object ::= DefInst
2032/// Object ::= MultiClassInst
2033/// Object ::= DefMInst
2034/// Object ::= LETCommand '{' ObjectList '}'
2035/// Object ::= LETCommand Object
2036bool TGParser::ParseObject() {
2037 switch (Lex.getCode()) {
2038 default: assert(0 && "This is not an object");
2039 case tgtok::Let: return ParseTopLevelLet();
2040 case tgtok::Def: return ParseDef(0) == 0;
2041 case tgtok::Defm: return ParseDefm();
2042 case tgtok::Class: return ParseClass();
2043 case tgtok::MultiClass: return ParseMultiClass();
2044 }
2045}
2046
2047/// ParseObjectList
2048/// ObjectList :== Object*
2049bool TGParser::ParseObjectList() {
2050 while (isObjectStart(Lex.getCode())) {
2051 if (ParseObject())
2052 return true;
2053 }
2054 return false;
2055}
2056
2057
2058bool TGParser::ParseFile() {
2059 Lex.Lex(); // Prime the lexer.
2060 if (ParseObjectList()) return true;
2061
2062 // If we have unread input at the end of the file, report it.
2063 if (Lex.getCode() == tgtok::Eof)
2064 return false;
2065
2066 return TokError("Unexpected input at top level");
2067}
2068