blob: 188ba4d2b1be6dbf34856ea9ed63631b2e1ea857 [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 Lattner8d978a72010-10-05 23:58:18 +000019#include "llvm/ADT/SmallVector.h"
Chris Lattner46f55522010-10-06 04:55:48 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000021using namespace llvm;
22
Chris Lattner46f55522010-10-06 04:55:48 +000023/// LLVMCHack - This is a temporary hack that changes how "(foo [1, 2, 3])"
24/// parses.
25/// FIXME: REMOVE THIS.
26static cl::opt<bool> LLVMCHack("llvmc-temp-hack", cl::ReallyHidden);
27
Chris Lattnerf4601652007-11-22 20:49:04 +000028//===----------------------------------------------------------------------===//
29// Support Code for the Semantic Actions.
30//===----------------------------------------------------------------------===//
31
32namespace llvm {
Chris Lattnerf4601652007-11-22 20:49:04 +000033struct SubClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000034 SMLoc RefLoc;
Chris Lattnerf4601652007-11-22 20:49:04 +000035 Record *Rec;
36 std::vector<Init*> TemplateArgs;
Chris Lattner1c8ae592009-03-13 16:01:53 +000037 SubClassReference() : Rec(0) {}
David Greened34a73b2009-04-24 16:55:41 +000038
Chris Lattnerf4601652007-11-22 20:49:04 +000039 bool isInvalid() const { return Rec == 0; }
40};
David Greenede444af2009-04-22 16:42:54 +000041
42struct SubMultiClassReference {
Chris Lattner1e3a8a42009-06-21 03:39:35 +000043 SMLoc RefLoc;
David Greenede444af2009-04-22 16:42:54 +000044 MultiClass *MC;
45 std::vector<Init*> TemplateArgs;
46 SubMultiClassReference() : MC(0) {}
Bob Wilson32558652009-04-28 19:41:44 +000047
David Greenede444af2009-04-22 16:42:54 +000048 bool isInvalid() const { return MC == 0; }
David Greened34a73b2009-04-24 16:55:41 +000049 void dump() const;
David Greenede444af2009-04-22 16:42:54 +000050};
David Greened34a73b2009-04-24 16:55:41 +000051
52void SubMultiClassReference::dump() const {
Daniel Dunbar1a551802009-07-03 00:10:29 +000053 errs() << "Multiclass:\n";
Bob Wilson21870412009-11-22 04:24:42 +000054
David Greened34a73b2009-04-24 16:55:41 +000055 MC->dump();
Bob Wilson21870412009-11-22 04:24:42 +000056
Daniel Dunbar1a551802009-07-03 00:10:29 +000057 errs() << "Template args:\n";
David Greened34a73b2009-04-24 16:55:41 +000058 for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(),
59 iend = TemplateArgs.end();
60 i != iend;
61 ++i) {
62 (*i)->dump();
63 }
64}
65
Chris Lattnerf4601652007-11-22 20:49:04 +000066} // end namespace llvm
67
Chris Lattner1e3a8a42009-06-21 03:39:35 +000068bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Chris Lattnerf4601652007-11-22 20:49:04 +000069 if (CurRec == 0)
70 CurRec = &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +000071
Chris Lattnerf4601652007-11-22 20:49:04 +000072 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
73 // The value already exists in the class, treat this as a set.
74 if (ERV->setValue(RV.getValue()))
75 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
76 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson21870412009-11-22 04:24:42 +000077 "previous definition of type '" +
Chris Lattnerf4601652007-11-22 20:49:04 +000078 ERV->getType()->getAsString() + "'");
79 } else {
80 CurRec->addValue(RV);
81 }
82 return false;
83}
84
85/// SetValue -
86/// Return true on error, false on success.
Bob Wilson21870412009-11-22 04:24:42 +000087bool TGParser::SetValue(Record *CurRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000088 const std::vector<unsigned> &BitList, Init *V) {
89 if (!V) return false;
90
91 if (CurRec == 0) CurRec = &CurMultiClass->Rec;
92
93 RecordVal *RV = CurRec->getValue(ValName);
94 if (RV == 0)
95 return Error(Loc, "Value '" + ValName + "' unknown!");
96
97 // Do not allow assignments like 'X = X'. This will just cause infinite loops
98 // in the resolution machinery.
99 if (BitList.empty())
100 if (VarInit *VI = dynamic_cast<VarInit*>(V))
101 if (VI->getName() == ValName)
102 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000103
Chris Lattnerf4601652007-11-22 20:49:04 +0000104 // If we are assigning to a subset of the bits in the value... then we must be
105 // assigning to a field of BitsRecTy, which must have a BitsInit
106 // initializer.
107 //
108 if (!BitList.empty()) {
109 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
110 if (CurVal == 0)
111 return Error(Loc, "Value '" + ValName + "' is not a bits type");
112
113 // Convert the incoming value to a bits type of the appropriate size...
114 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList.size()));
115 if (BI == 0) {
116 V->convertInitializerTo(new BitsRecTy(BitList.size()));
117 return Error(Loc, "Initializer is not compatible with bit range");
118 }
Bob Wilson21870412009-11-22 04:24:42 +0000119
Chris Lattnerf4601652007-11-22 20:49:04 +0000120 // We should have a BitsInit type now.
121 BitsInit *BInit = dynamic_cast<BitsInit*>(BI);
122 assert(BInit != 0);
123
124 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
125
126 // Loop over bits, assigning values as appropriate.
127 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
128 unsigned Bit = BitList[i];
129 if (NewVal->getBit(Bit))
130 return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" +
131 ValName + "' more than once");
132 NewVal->setBit(Bit, BInit->getBit(i));
133 }
134
135 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
136 if (NewVal->getBit(i) == 0)
137 NewVal->setBit(i, CurVal->getBit(i));
138
139 V = NewVal;
140 }
141
142 if (RV->setValue(V))
Bob Wilson21870412009-11-22 04:24:42 +0000143 return Error(Loc, "Value '" + ValName + "' of type '" +
144 RV->getType()->getAsString() +
Chris Lattner5d814862007-11-22 21:06:59 +0000145 "' is incompatible with initializer '" + V->getAsString() +"'");
Chris Lattnerf4601652007-11-22 20:49:04 +0000146 return false;
147}
148
149/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
150/// args as SubClass's template arguments.
Cedric Venetaff9c272009-02-14 16:06:42 +0000151bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000152 Record *SC = SubClass.Rec;
153 // Add all of the values in the subclass into the current class.
154 const std::vector<RecordVal> &Vals = SC->getValues();
155 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
156 if (AddValue(CurRec, SubClass.RefLoc, Vals[i]))
157 return true;
158
159 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
160
161 // Ensure that an appropriate number of template arguments are specified.
162 if (TArgs.size() < SubClass.TemplateArgs.size())
163 return Error(SubClass.RefLoc, "More template args specified than expected");
Bob Wilson21870412009-11-22 04:24:42 +0000164
Chris Lattnerf4601652007-11-22 20:49:04 +0000165 // Loop over all of the template arguments, setting them to the specified
166 // value or leaving them as the default if necessary.
167 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
168 if (i < SubClass.TemplateArgs.size()) {
169 // If a value is specified for this template arg, set it now.
Bob Wilson21870412009-11-22 04:24:42 +0000170 if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(),
Chris Lattnerf4601652007-11-22 20:49:04 +0000171 SubClass.TemplateArgs[i]))
172 return true;
Bob Wilson21870412009-11-22 04:24:42 +0000173
Chris Lattnerf4601652007-11-22 20:49:04 +0000174 // Resolve it next.
175 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
Bob Wilson21870412009-11-22 04:24:42 +0000176
Chris Lattnerf4601652007-11-22 20:49:04 +0000177 // Now remove it.
178 CurRec->removeValue(TArgs[i]);
179
180 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
181 return Error(SubClass.RefLoc,"Value not specified for template argument #"
Bob Wilson21870412009-11-22 04:24:42 +0000182 + utostr(i) + " (" + TArgs[i] + ") of subclass '" +
Chris Lattnerf4601652007-11-22 20:49:04 +0000183 SC->getName() + "'!");
184 }
185 }
186
187 // Since everything went well, we can now set the "superclass" list for the
188 // current record.
189 const std::vector<Record*> &SCs = SC->getSuperClasses();
190 for (unsigned i = 0, e = SCs.size(); i != e; ++i) {
191 if (CurRec->isSubClassOf(SCs[i]))
192 return Error(SubClass.RefLoc,
193 "Already subclass of '" + SCs[i]->getName() + "'!\n");
194 CurRec->addSuperClass(SCs[i]);
195 }
Bob Wilson21870412009-11-22 04:24:42 +0000196
Chris Lattnerf4601652007-11-22 20:49:04 +0000197 if (CurRec->isSubClassOf(SC))
198 return Error(SubClass.RefLoc,
199 "Already subclass of '" + SC->getName() + "'!\n");
200 CurRec->addSuperClass(SC);
201 return false;
202}
203
David Greenede444af2009-04-22 16:42:54 +0000204/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilson440548d2009-04-30 18:26:19 +0000205/// CurMC, resolving its template args as SubMultiClass's
David Greenede444af2009-04-22 16:42:54 +0000206/// template arguments.
Bob Wilson440548d2009-04-30 18:26:19 +0000207bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson1d512df2009-04-30 17:46:20 +0000208 SubMultiClassReference &SubMultiClass) {
David Greenede444af2009-04-22 16:42:54 +0000209 MultiClass *SMC = SubMultiClass.MC;
Bob Wilson440548d2009-04-30 18:26:19 +0000210 Record *CurRec = &CurMC->Rec;
David Greenede444af2009-04-22 16:42:54 +0000211
Bob Wilson440548d2009-04-30 18:26:19 +0000212 const std::vector<RecordVal> &MCVals = CurRec->getValues();
David Greenede444af2009-04-22 16:42:54 +0000213
214 // Add all of the values in the subclass into the current class.
215 const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues();
216 for (unsigned i = 0, e = SMCVals.size(); i != e; ++i)
217 if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i]))
218 return true;
219
Bob Wilson440548d2009-04-30 18:26:19 +0000220 int newDefStart = CurMC->DefPrototypes.size();
David Greened34a73b2009-04-24 16:55:41 +0000221
David Greenede444af2009-04-22 16:42:54 +0000222 // Add all of the defs in the subclass into the current multiclass.
223 for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(),
224 iend = SMC->DefPrototypes.end();
225 i != iend;
226 ++i) {
227 // Clone the def and add it to the current multiclass
228 Record *NewDef = new Record(**i);
229
230 // Add all of the values in the superclass into the current def.
231 for (unsigned i = 0, e = MCVals.size(); i != e; ++i)
232 if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i]))
233 return true;
234
Bob Wilson440548d2009-04-30 18:26:19 +0000235 CurMC->DefPrototypes.push_back(NewDef);
David Greenede444af2009-04-22 16:42:54 +0000236 }
Bob Wilson32558652009-04-28 19:41:44 +0000237
David Greenede444af2009-04-22 16:42:54 +0000238 const std::vector<std::string> &SMCTArgs = SMC->Rec.getTemplateArgs();
239
David Greened34a73b2009-04-24 16:55:41 +0000240 // Ensure that an appropriate number of template arguments are
241 // specified.
David Greenede444af2009-04-22 16:42:54 +0000242 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
David Greened34a73b2009-04-24 16:55:41 +0000243 return Error(SubMultiClass.RefLoc,
244 "More template args specified than expected");
Bob Wilson32558652009-04-28 19:41:44 +0000245
David Greenede444af2009-04-22 16:42:54 +0000246 // Loop over all of the template arguments, setting them to the specified
247 // value or leaving them as the default if necessary.
248 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
249 if (i < SubMultiClass.TemplateArgs.size()) {
David Greened34a73b2009-04-24 16:55:41 +0000250 // If a value is specified for this template arg, set it in the
251 // superclass now.
252 if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000253 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000254 SubMultiClass.TemplateArgs[i]))
255 return true;
256
257 // Resolve it next.
258 CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i]));
Bob Wilson32558652009-04-28 19:41:44 +0000259
David Greenede444af2009-04-22 16:42:54 +0000260 // Now remove it.
261 CurRec->removeValue(SMCTArgs[i]);
262
David Greened34a73b2009-04-24 16:55:41 +0000263 // If a value is specified for this template arg, set it in the
264 // new defs now.
265 for (MultiClass::RecordVector::iterator j =
Bob Wilson440548d2009-04-30 18:26:19 +0000266 CurMC->DefPrototypes.begin() + newDefStart,
267 jend = CurMC->DefPrototypes.end();
David Greenede444af2009-04-22 16:42:54 +0000268 j != jend;
269 ++j) {
270 Record *Def = *j;
271
David Greened34a73b2009-04-24 16:55:41 +0000272 if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i],
Bob Wilson32558652009-04-28 19:41:44 +0000273 std::vector<unsigned>(),
David Greenede444af2009-04-22 16:42:54 +0000274 SubMultiClass.TemplateArgs[i]))
275 return true;
276
277 // Resolve it next.
278 Def->resolveReferencesTo(Def->getValue(SMCTArgs[i]));
279
280 // Now remove it
281 Def->removeValue(SMCTArgs[i]);
282 }
283 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
David Greened34a73b2009-04-24 16:55:41 +0000284 return Error(SubMultiClass.RefLoc,
285 "Value not specified for template argument #"
Bob Wilson32558652009-04-28 19:41:44 +0000286 + utostr(i) + " (" + SMCTArgs[i] + ") of subclass '" +
David Greenede444af2009-04-22 16:42:54 +0000287 SMC->Rec.getName() + "'!");
288 }
289 }
290
291 return false;
292}
293
Chris Lattnerf4601652007-11-22 20:49:04 +0000294//===----------------------------------------------------------------------===//
295// Parser Code
296//===----------------------------------------------------------------------===//
297
298/// isObjectStart - Return true if this is a valid first token for an Object.
299static bool isObjectStart(tgtok::TokKind K) {
300 return K == tgtok::Class || K == tgtok::Def ||
Bob Wilson21870412009-11-22 04:24:42 +0000301 K == tgtok::Defm || K == tgtok::Let || K == tgtok::MultiClass;
Chris Lattnerf4601652007-11-22 20:49:04 +0000302}
303
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000304static std::string GetNewAnonymousName() {
305 static unsigned AnonCounter = 0;
306 return "anonymous."+utostr(AnonCounter++);
307}
308
Chris Lattnerf4601652007-11-22 20:49:04 +0000309/// ParseObjectName - If an object name is specified, return it. Otherwise,
310/// return an anonymous name.
311/// ObjectName ::= ID
312/// ObjectName ::= /*empty*/
313///
314std::string TGParser::ParseObjectName() {
Chris Lattnerdf72eae2010-10-05 22:51:56 +0000315 if (Lex.getCode() != tgtok::Id)
316 return GetNewAnonymousName();
317
318 std::string Ret = Lex.getCurStrVal();
319 Lex.Lex();
320 return Ret;
Chris Lattnerf4601652007-11-22 20:49:04 +0000321}
322
323
324/// ParseClassID - Parse and resolve a reference to a class name. This returns
325/// null on error.
326///
327/// ClassID ::= ID
328///
329Record *TGParser::ParseClassID() {
330 if (Lex.getCode() != tgtok::Id) {
331 TokError("expected name for ClassID");
332 return 0;
333 }
Bob Wilson21870412009-11-22 04:24:42 +0000334
Chris Lattnerf4601652007-11-22 20:49:04 +0000335 Record *Result = Records.getClass(Lex.getCurStrVal());
336 if (Result == 0)
337 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson21870412009-11-22 04:24:42 +0000338
Chris Lattnerf4601652007-11-22 20:49:04 +0000339 Lex.Lex();
340 return Result;
341}
342
Bob Wilson32558652009-04-28 19:41:44 +0000343/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
344/// This returns null on error.
David Greenede444af2009-04-22 16:42:54 +0000345///
346/// MultiClassID ::= ID
347///
348MultiClass *TGParser::ParseMultiClassID() {
349 if (Lex.getCode() != tgtok::Id) {
350 TokError("expected name for ClassID");
351 return 0;
352 }
Bob Wilson32558652009-04-28 19:41:44 +0000353
David Greenede444af2009-04-22 16:42:54 +0000354 MultiClass *Result = MultiClasses[Lex.getCurStrVal()];
355 if (Result == 0)
356 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson32558652009-04-28 19:41:44 +0000357
David Greenede444af2009-04-22 16:42:54 +0000358 Lex.Lex();
359 return Result;
360}
361
Chris Lattnerf4601652007-11-22 20:49:04 +0000362Record *TGParser::ParseDefmID() {
363 if (Lex.getCode() != tgtok::Id) {
364 TokError("expected multiclass name");
365 return 0;
366 }
Bob Wilson21870412009-11-22 04:24:42 +0000367
Chris Lattnerf4601652007-11-22 20:49:04 +0000368 MultiClass *MC = MultiClasses[Lex.getCurStrVal()];
369 if (MC == 0) {
370 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
371 return 0;
372 }
Bob Wilson21870412009-11-22 04:24:42 +0000373
Chris Lattnerf4601652007-11-22 20:49:04 +0000374 Lex.Lex();
375 return &MC->Rec;
Bob Wilson21870412009-11-22 04:24:42 +0000376}
Chris Lattnerf4601652007-11-22 20:49:04 +0000377
378
379/// ParseSubClassReference - Parse a reference to a subclass or to a templated
380/// subclass. This returns a SubClassRefTy with a null Record* on error.
381///
382/// SubClassRef ::= ClassID
383/// SubClassRef ::= ClassID '<' ValueList '>'
384///
385SubClassReference TGParser::
386ParseSubClassReference(Record *CurRec, bool isDefm) {
387 SubClassReference Result;
388 Result.RefLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +0000389
Chris Lattnerf4601652007-11-22 20:49:04 +0000390 if (isDefm)
391 Result.Rec = ParseDefmID();
392 else
393 Result.Rec = ParseClassID();
394 if (Result.Rec == 0) return Result;
Bob Wilson21870412009-11-22 04:24:42 +0000395
Chris Lattnerf4601652007-11-22 20:49:04 +0000396 // If there is no template arg list, we're done.
397 if (Lex.getCode() != tgtok::less)
398 return Result;
399 Lex.Lex(); // Eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000400
Chris Lattnerf4601652007-11-22 20:49:04 +0000401 if (Lex.getCode() == tgtok::greater) {
402 TokError("subclass reference requires a non-empty list of template values");
403 Result.Rec = 0;
404 return Result;
405 }
Bob Wilson21870412009-11-22 04:24:42 +0000406
David Greenee1b46912009-06-08 20:23:18 +0000407 Result.TemplateArgs = ParseValueList(CurRec, Result.Rec);
Chris Lattnerf4601652007-11-22 20:49:04 +0000408 if (Result.TemplateArgs.empty()) {
409 Result.Rec = 0; // Error parsing value list.
410 return Result;
411 }
Bob Wilson21870412009-11-22 04:24:42 +0000412
Chris Lattnerf4601652007-11-22 20:49:04 +0000413 if (Lex.getCode() != tgtok::greater) {
414 TokError("expected '>' in template value list");
415 Result.Rec = 0;
416 return Result;
417 }
418 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000419
Chris Lattnerf4601652007-11-22 20:49:04 +0000420 return Result;
421}
422
Bob Wilson32558652009-04-28 19:41:44 +0000423/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
424/// templated submulticlass. This returns a SubMultiClassRefTy with a null
425/// Record* on error.
David Greenede444af2009-04-22 16:42:54 +0000426///
427/// SubMultiClassRef ::= MultiClassID
428/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
429///
430SubMultiClassReference TGParser::
431ParseSubMultiClassReference(MultiClass *CurMC) {
432 SubMultiClassReference Result;
433 Result.RefLoc = Lex.getLoc();
Bob Wilson32558652009-04-28 19:41:44 +0000434
David Greenede444af2009-04-22 16:42:54 +0000435 Result.MC = ParseMultiClassID();
436 if (Result.MC == 0) return Result;
Bob Wilson32558652009-04-28 19:41:44 +0000437
David Greenede444af2009-04-22 16:42:54 +0000438 // If there is no template arg list, we're done.
439 if (Lex.getCode() != tgtok::less)
440 return Result;
441 Lex.Lex(); // Eat the '<'
Bob Wilson32558652009-04-28 19:41:44 +0000442
David Greenede444af2009-04-22 16:42:54 +0000443 if (Lex.getCode() == tgtok::greater) {
444 TokError("subclass reference requires a non-empty list of template values");
445 Result.MC = 0;
446 return Result;
447 }
Bob Wilson32558652009-04-28 19:41:44 +0000448
David Greenee1b46912009-06-08 20:23:18 +0000449 Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec);
David Greenede444af2009-04-22 16:42:54 +0000450 if (Result.TemplateArgs.empty()) {
451 Result.MC = 0; // Error parsing value list.
452 return Result;
453 }
Bob Wilson32558652009-04-28 19:41:44 +0000454
David Greenede444af2009-04-22 16:42:54 +0000455 if (Lex.getCode() != tgtok::greater) {
456 TokError("expected '>' in template value list");
457 Result.MC = 0;
458 return Result;
459 }
460 Lex.Lex();
461
462 return Result;
463}
464
Chris Lattnerf4601652007-11-22 20:49:04 +0000465/// ParseRangePiece - Parse a bit/value range.
466/// RangePiece ::= INTVAL
467/// RangePiece ::= INTVAL '-' INTVAL
468/// RangePiece ::= INTVAL INTVAL
469bool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) {
Chris Lattner811281e2008-01-10 07:01:53 +0000470 if (Lex.getCode() != tgtok::IntVal) {
471 TokError("expected integer or bitrange");
472 return true;
473 }
Dan Gohman63f97202008-10-17 01:33:43 +0000474 int64_t Start = Lex.getCurIntVal();
475 int64_t End;
Bob Wilson21870412009-11-22 04:24:42 +0000476
Chris Lattnerf4601652007-11-22 20:49:04 +0000477 if (Start < 0)
478 return TokError("invalid range, cannot be negative");
Bob Wilson21870412009-11-22 04:24:42 +0000479
Chris Lattnerf4601652007-11-22 20:49:04 +0000480 switch (Lex.Lex()) { // eat first character.
Bob Wilson21870412009-11-22 04:24:42 +0000481 default:
Chris Lattnerf4601652007-11-22 20:49:04 +0000482 Ranges.push_back(Start);
483 return false;
484 case tgtok::minus:
485 if (Lex.Lex() != tgtok::IntVal) {
486 TokError("expected integer value as end of range");
487 return true;
488 }
489 End = Lex.getCurIntVal();
490 break;
491 case tgtok::IntVal:
492 End = -Lex.getCurIntVal();
493 break;
494 }
Bob Wilson21870412009-11-22 04:24:42 +0000495 if (End < 0)
Chris Lattnerf4601652007-11-22 20:49:04 +0000496 return TokError("invalid range, cannot be negative");
497 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +0000498
Chris Lattnerf4601652007-11-22 20:49:04 +0000499 // Add to the range.
500 if (Start < End) {
501 for (; Start <= End; ++Start)
502 Ranges.push_back(Start);
503 } else {
504 for (; Start >= End; --Start)
505 Ranges.push_back(Start);
506 }
507 return false;
508}
509
510/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
511///
512/// RangeList ::= RangePiece (',' RangePiece)*
513///
514std::vector<unsigned> TGParser::ParseRangeList() {
515 std::vector<unsigned> Result;
Bob Wilson21870412009-11-22 04:24:42 +0000516
Chris Lattnerf4601652007-11-22 20:49:04 +0000517 // Parse the first piece.
518 if (ParseRangePiece(Result))
519 return std::vector<unsigned>();
520 while (Lex.getCode() == tgtok::comma) {
521 Lex.Lex(); // Eat the comma.
522
523 // Parse the next range piece.
524 if (ParseRangePiece(Result))
525 return std::vector<unsigned>();
526 }
527 return Result;
528}
529
530/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
531/// OptionalRangeList ::= '<' RangeList '>'
532/// OptionalRangeList ::= /*empty*/
533bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
534 if (Lex.getCode() != tgtok::less)
535 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000536
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000537 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000538 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +0000539
Chris Lattnerf4601652007-11-22 20:49:04 +0000540 // Parse the range list.
541 Ranges = ParseRangeList();
542 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000543
Chris Lattnerf4601652007-11-22 20:49:04 +0000544 if (Lex.getCode() != tgtok::greater) {
545 TokError("expected '>' at end of range list");
546 return Error(StartLoc, "to match this '<'");
547 }
548 Lex.Lex(); // eat the '>'.
549 return false;
550}
551
552/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
553/// OptionalBitList ::= '{' RangeList '}'
554/// OptionalBitList ::= /*empty*/
555bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
556 if (Lex.getCode() != tgtok::l_brace)
557 return false;
Bob Wilson21870412009-11-22 04:24:42 +0000558
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000559 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000560 Lex.Lex(); // eat the '{'
Bob Wilson21870412009-11-22 04:24:42 +0000561
Chris Lattnerf4601652007-11-22 20:49:04 +0000562 // Parse the range list.
563 Ranges = ParseRangeList();
564 if (Ranges.empty()) return true;
Bob Wilson21870412009-11-22 04:24:42 +0000565
Chris Lattnerf4601652007-11-22 20:49:04 +0000566 if (Lex.getCode() != tgtok::r_brace) {
567 TokError("expected '}' at end of bit list");
568 return Error(StartLoc, "to match this '{'");
569 }
570 Lex.Lex(); // eat the '}'.
571 return false;
572}
573
574
575/// ParseType - Parse and return a tblgen type. This returns null on error.
576///
577/// Type ::= STRING // string type
578/// Type ::= BIT // bit type
579/// Type ::= BITS '<' INTVAL '>' // bits<x> type
580/// Type ::= INT // int type
581/// Type ::= LIST '<' Type '>' // list<x> type
582/// Type ::= CODE // code type
583/// Type ::= DAG // dag type
584/// Type ::= ClassID // Record Type
585///
586RecTy *TGParser::ParseType() {
587 switch (Lex.getCode()) {
588 default: TokError("Unknown token when expecting a type"); return 0;
589 case tgtok::String: Lex.Lex(); return new StringRecTy();
590 case tgtok::Bit: Lex.Lex(); return new BitRecTy();
591 case tgtok::Int: Lex.Lex(); return new IntRecTy();
592 case tgtok::Code: Lex.Lex(); return new CodeRecTy();
593 case tgtok::Dag: Lex.Lex(); return new DagRecTy();
594 case tgtok::Id:
595 if (Record *R = ParseClassID()) return new RecordRecTy(R);
596 return 0;
597 case tgtok::Bits: {
598 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
599 TokError("expected '<' after bits type");
600 return 0;
601 }
602 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
603 TokError("expected integer in bits<n> type");
604 return 0;
605 }
Dan Gohman63f97202008-10-17 01:33:43 +0000606 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4601652007-11-22 20:49:04 +0000607 if (Lex.Lex() != tgtok::greater) { // Eat count.
608 TokError("expected '>' at end of bits<n> type");
609 return 0;
610 }
611 Lex.Lex(); // Eat '>'
612 return new BitsRecTy(Val);
613 }
614 case tgtok::List: {
615 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
616 TokError("expected '<' after list type");
617 return 0;
618 }
619 Lex.Lex(); // Eat '<'
620 RecTy *SubType = ParseType();
621 if (SubType == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +0000622
Chris Lattnerf4601652007-11-22 20:49:04 +0000623 if (Lex.getCode() != tgtok::greater) {
624 TokError("expected '>' at end of list<ty> type");
625 return 0;
626 }
627 Lex.Lex(); // Eat '>'
628 return new ListRecTy(SubType);
629 }
Bob Wilson21870412009-11-22 04:24:42 +0000630 }
Chris Lattnerf4601652007-11-22 20:49:04 +0000631}
632
633/// ParseIDValue - Parse an ID as a value and decode what it means.
634///
635/// IDValue ::= ID [def local value]
636/// IDValue ::= ID [def template arg]
637/// IDValue ::= ID [multiclass local value]
638/// IDValue ::= ID [multiclass template argument]
639/// IDValue ::= ID [def name]
640///
641Init *TGParser::ParseIDValue(Record *CurRec) {
642 assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue");
643 std::string Name = Lex.getCurStrVal();
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000644 SMLoc Loc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +0000645 Lex.Lex();
646 return ParseIDValue(CurRec, Name, Loc);
647}
648
649/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
650/// has already been read.
Bob Wilson21870412009-11-22 04:24:42 +0000651Init *TGParser::ParseIDValue(Record *CurRec,
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000652 const std::string &Name, SMLoc NameLoc) {
Chris Lattnerf4601652007-11-22 20:49:04 +0000653 if (CurRec) {
654 if (const RecordVal *RV = CurRec->getValue(Name))
655 return new VarInit(Name, RV->getType());
Bob Wilson21870412009-11-22 04:24:42 +0000656
Chris Lattnerf4601652007-11-22 20:49:04 +0000657 std::string TemplateArgName = CurRec->getName()+":"+Name;
658 if (CurRec->isTemplateArg(TemplateArgName)) {
659 const RecordVal *RV = CurRec->getValue(TemplateArgName);
660 assert(RV && "Template arg doesn't exist??");
661 return new VarInit(TemplateArgName, RV->getType());
662 }
663 }
Bob Wilson21870412009-11-22 04:24:42 +0000664
Chris Lattnerf4601652007-11-22 20:49:04 +0000665 if (CurMultiClass) {
666 std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
667 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
668 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
669 assert(RV && "Template arg doesn't exist??");
670 return new VarInit(MCName, RV->getType());
671 }
672 }
Bob Wilson21870412009-11-22 04:24:42 +0000673
Chris Lattnerf4601652007-11-22 20:49:04 +0000674 if (Record *D = Records.getDef(Name))
675 return new DefInit(D);
676
677 Error(NameLoc, "Variable not defined: '" + Name + "'");
678 return 0;
679}
680
David Greened418c1b2009-05-14 20:54:48 +0000681/// ParseOperation - Parse an operator. This returns null on error.
682///
683/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
684///
685Init *TGParser::ParseOperation(Record *CurRec) {
686 switch (Lex.getCode()) {
687 default:
688 TokError("unknown operation");
689 return 0;
690 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000691 case tgtok::XCar:
692 case tgtok::XCdr:
693 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +0000694 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
695 UnOpInit::UnaryOp Code;
696 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000697
David Greenee6c27de2009-05-14 21:22:49 +0000698 switch (Lex.getCode()) {
699 default: assert(0 && "Unhandled code!");
700 case tgtok::XCast:
701 Lex.Lex(); // eat the operation
702 Code = UnOpInit::CAST;
David Greened418c1b2009-05-14 20:54:48 +0000703
David Greenee6c27de2009-05-14 21:22:49 +0000704 Type = ParseOperatorType();
David Greened418c1b2009-05-14 20:54:48 +0000705
David Greenee6c27de2009-05-14 21:22:49 +0000706 if (Type == 0) {
David Greene5f9f9ba2009-05-14 22:38:31 +0000707 TokError("did not get type for unary operator");
David Greenee6c27de2009-05-14 21:22:49 +0000708 return 0;
709 }
David Greened418c1b2009-05-14 20:54:48 +0000710
David Greenee6c27de2009-05-14 21:22:49 +0000711 break;
David Greene5f9f9ba2009-05-14 22:38:31 +0000712 case tgtok::XCar:
713 Lex.Lex(); // eat the operation
714 Code = UnOpInit::CAR;
715 break;
716 case tgtok::XCdr:
717 Lex.Lex(); // eat the operation
718 Code = UnOpInit::CDR;
719 break;
720 case tgtok::XNull:
721 Lex.Lex(); // eat the operation
722 Code = UnOpInit::LNULL;
723 Type = new IntRecTy;
724 break;
David Greenee6c27de2009-05-14 21:22:49 +0000725 }
726 if (Lex.getCode() != tgtok::l_paren) {
727 TokError("expected '(' after unary operator");
728 return 0;
729 }
730 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000731
David Greenee6c27de2009-05-14 21:22:49 +0000732 Init *LHS = ParseValue(CurRec);
733 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000734
David Greene5f9f9ba2009-05-14 22:38:31 +0000735 if (Code == UnOpInit::CAR
736 || Code == UnOpInit::CDR
737 || Code == UnOpInit::LNULL) {
738 ListInit *LHSl = dynamic_cast<ListInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000739 StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
David Greene5f9f9ba2009-05-14 22:38:31 +0000740 TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS);
David Greenee1b46912009-06-08 20:23:18 +0000741 if (LHSl == 0 && LHSs == 0 && LHSt == 0) {
742 TokError("expected list or string type argument in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000743 return 0;
744 }
745 if (LHSt) {
746 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
David Greenee1b46912009-06-08 20:23:18 +0000747 StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType());
748 if (LType == 0 && SType == 0) {
749 TokError("expected list or string type argumnet in unary operator");
David Greene5f9f9ba2009-05-14 22:38:31 +0000750 return 0;
751 }
752 }
753
754 if (Code == UnOpInit::CAR
755 || Code == UnOpInit::CDR) {
David Greenee1b46912009-06-08 20:23:18 +0000756 if (LHSl == 0 && LHSt == 0) {
757 TokError("expected list type argumnet in unary operator");
758 return 0;
759 }
Bob Wilson21870412009-11-22 04:24:42 +0000760
David Greene5f9f9ba2009-05-14 22:38:31 +0000761 if (LHSl && LHSl->getSize() == 0) {
762 TokError("empty list argument in unary operator");
763 return 0;
764 }
765 if (LHSl) {
766 Init *Item = LHSl->getElement(0);
767 TypedInit *Itemt = dynamic_cast<TypedInit*>(Item);
768 if (Itemt == 0) {
769 TokError("untyped list element in unary operator");
770 return 0;
771 }
772 if (Code == UnOpInit::CAR) {
773 Type = Itemt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000774 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000775 Type = new ListRecTy(Itemt->getType());
776 }
Bob Wilson21870412009-11-22 04:24:42 +0000777 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000778 assert(LHSt && "expected list type argument in unary operator");
779 ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType());
780 if (LType == 0) {
781 TokError("expected list type argumnet in unary operator");
782 return 0;
783 }
784 if (Code == UnOpInit::CAR) {
785 Type = LType->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +0000786 } else {
David Greene5f9f9ba2009-05-14 22:38:31 +0000787 Type = LType;
788 }
789 }
790 }
791 }
792
David Greenee6c27de2009-05-14 21:22:49 +0000793 if (Lex.getCode() != tgtok::r_paren) {
794 TokError("expected ')' in unary operator");
795 return 0;
796 }
797 Lex.Lex(); // eat the ')'
798 return (new UnOpInit(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
799 }
David Greened418c1b2009-05-14 20:54:48 +0000800
801 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +0000802 case tgtok::XSRA:
David Greened418c1b2009-05-14 20:54:48 +0000803 case tgtok::XSRL:
804 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +0000805 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000806 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000807 tgtok::TokKind OpTok = Lex.getCode();
808 SMLoc OpLoc = Lex.getLoc();
809 Lex.Lex(); // eat the operation
810
David Greened418c1b2009-05-14 20:54:48 +0000811 BinOpInit::BinaryOp Code;
812 RecTy *Type = 0;
813
Chris Lattner8d978a72010-10-05 23:58:18 +0000814 switch (OpTok) {
David Greened418c1b2009-05-14 20:54:48 +0000815 default: assert(0 && "Unhandled code!");
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000816 case tgtok::XConcat: Code = BinOpInit::CONCAT; Type = new DagRecTy(); break;
817 case tgtok::XSRA: Code = BinOpInit::SRA; Type = new IntRecTy(); break;
818 case tgtok::XSRL: Code = BinOpInit::SRL; Type = new IntRecTy(); break;
819 case tgtok::XSHL: Code = BinOpInit::SHL; Type = new IntRecTy(); break;
820 case tgtok::XEq: Code = BinOpInit::EQ; Type = new IntRecTy(); break;
Bob Wilson21870412009-11-22 04:24:42 +0000821 case tgtok::XStrConcat:
David Greened418c1b2009-05-14 20:54:48 +0000822 Code = BinOpInit::STRCONCAT;
823 Type = new StringRecTy();
824 break;
David Greened418c1b2009-05-14 20:54:48 +0000825 }
Chris Lattnerc7252ce2010-10-06 00:19:21 +0000826
David Greened418c1b2009-05-14 20:54:48 +0000827 if (Lex.getCode() != tgtok::l_paren) {
828 TokError("expected '(' after binary operator");
829 return 0;
830 }
831 Lex.Lex(); // eat the '('
832
Chris Lattner8d978a72010-10-05 23:58:18 +0000833 SmallVector<Init*, 2> InitList;
834
835 InitList.push_back(ParseValue(CurRec));
836 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000837
Chris Lattner8d978a72010-10-05 23:58:18 +0000838 while (Lex.getCode() == tgtok::comma) {
839 Lex.Lex(); // eat the ','
840
841 InitList.push_back(ParseValue(CurRec));
842 if (InitList.back() == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000843 }
David Greened418c1b2009-05-14 20:54:48 +0000844
845 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner8d978a72010-10-05 23:58:18 +0000846 TokError("expected ')' in operator");
David Greened418c1b2009-05-14 20:54:48 +0000847 return 0;
848 }
849 Lex.Lex(); // eat the ')'
Chris Lattner8d978a72010-10-05 23:58:18 +0000850
851 // We allow multiple operands to associative operators like !strconcat as
852 // shorthand for nesting them.
853 if (Code == BinOpInit::STRCONCAT) {
854 while (InitList.size() > 2) {
855 Init *RHS = InitList.pop_back_val();
856 RHS = (new BinOpInit(Code, InitList.back(), RHS, Type))
857 ->Fold(CurRec, CurMultiClass);
858 InitList.back() = RHS;
859 }
860 }
861
862 if (InitList.size() == 2)
863 return (new BinOpInit(Code, InitList[0], InitList[1], Type))
864 ->Fold(CurRec, CurMultiClass);
865
866 Error(OpLoc, "expected two operands to operator");
867 return 0;
David Greened418c1b2009-05-14 20:54:48 +0000868 }
869
David Greene9bea7c82009-05-14 23:26:46 +0000870 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +0000871 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +0000872 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
873 TernOpInit::TernaryOp Code;
874 RecTy *Type = 0;
David Greened418c1b2009-05-14 20:54:48 +0000875
876
David Greene4afc5092009-05-14 21:54:42 +0000877 tgtok::TokKind LexCode = Lex.getCode();
878 Lex.Lex(); // eat the operation
879 switch (LexCode) {
880 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000881 case tgtok::XIf:
882 Code = TernOpInit::IF;
883 break;
David Greenebeb31a52009-05-14 22:23:47 +0000884 case tgtok::XForEach:
885 Code = TernOpInit::FOREACH;
886 break;
David Greene4afc5092009-05-14 21:54:42 +0000887 case tgtok::XSubst:
888 Code = TernOpInit::SUBST;
889 break;
890 }
891 if (Lex.getCode() != tgtok::l_paren) {
892 TokError("expected '(' after ternary operator");
893 return 0;
894 }
895 Lex.Lex(); // eat the '('
David Greened418c1b2009-05-14 20:54:48 +0000896
David Greene4afc5092009-05-14 21:54:42 +0000897 Init *LHS = ParseValue(CurRec);
898 if (LHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000899
David Greene4afc5092009-05-14 21:54:42 +0000900 if (Lex.getCode() != tgtok::comma) {
901 TokError("expected ',' in ternary operator");
902 return 0;
903 }
904 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000905
David Greene4afc5092009-05-14 21:54:42 +0000906 Init *MHS = ParseValue(CurRec);
907 if (MHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000908
David Greene4afc5092009-05-14 21:54:42 +0000909 if (Lex.getCode() != tgtok::comma) {
910 TokError("expected ',' in ternary operator");
911 return 0;
912 }
913 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +0000914
David Greene4afc5092009-05-14 21:54:42 +0000915 Init *RHS = ParseValue(CurRec);
916 if (RHS == 0) return 0;
David Greened418c1b2009-05-14 20:54:48 +0000917
David Greene4afc5092009-05-14 21:54:42 +0000918 if (Lex.getCode() != tgtok::r_paren) {
919 TokError("expected ')' in binary operator");
920 return 0;
921 }
922 Lex.Lex(); // eat the ')'
David Greened418c1b2009-05-14 20:54:48 +0000923
David Greene4afc5092009-05-14 21:54:42 +0000924 switch (LexCode) {
925 default: assert(0 && "Unhandled code!");
David Greene9bea7c82009-05-14 23:26:46 +0000926 case tgtok::XIf: {
927 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
928 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
929 if (MHSt == 0 || RHSt == 0) {
930 TokError("could not get type for !if");
931 return 0;
932 }
933 if (MHSt->getType()->typeIsConvertibleTo(RHSt->getType())) {
934 Type = RHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000935 } else if (RHSt->getType()->typeIsConvertibleTo(MHSt->getType())) {
David Greene9bea7c82009-05-14 23:26:46 +0000936 Type = MHSt->getType();
Bob Wilson21870412009-11-22 04:24:42 +0000937 } else {
David Greene9bea7c82009-05-14 23:26:46 +0000938 TokError("inconsistent types for !if");
939 return 0;
940 }
941 break;
942 }
David Greenebeb31a52009-05-14 22:23:47 +0000943 case tgtok::XForEach: {
944 TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS);
945 if (MHSt == 0) {
946 TokError("could not get type for !foreach");
947 return 0;
948 }
949 Type = MHSt->getType();
950 break;
951 }
David Greene4afc5092009-05-14 21:54:42 +0000952 case tgtok::XSubst: {
953 TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS);
954 if (RHSt == 0) {
955 TokError("could not get type for !subst");
956 return 0;
957 }
958 Type = RHSt->getType();
959 break;
960 }
961 }
Bob Wilson21870412009-11-22 04:24:42 +0000962 return (new TernOpInit(Code, LHS, MHS, RHS, Type))->Fold(CurRec,
963 CurMultiClass);
David Greene4afc5092009-05-14 21:54:42 +0000964 }
David Greened418c1b2009-05-14 20:54:48 +0000965 }
966 TokError("could not parse operation");
967 return 0;
968}
969
970/// ParseOperatorType - Parse a type for an operator. This returns
971/// null on error.
972///
973/// OperatorType ::= '<' Type '>'
974///
Dan Gohmana9ad0412009-08-12 22:10:57 +0000975RecTy *TGParser::ParseOperatorType() {
David Greened418c1b2009-05-14 20:54:48 +0000976 RecTy *Type = 0;
977
978 if (Lex.getCode() != tgtok::less) {
979 TokError("expected type name for operator");
980 return 0;
981 }
982 Lex.Lex(); // eat the <
983
984 Type = ParseType();
985
986 if (Type == 0) {
987 TokError("expected type name for operator");
988 return 0;
989 }
990
991 if (Lex.getCode() != tgtok::greater) {
992 TokError("expected type name for operator");
993 return 0;
994 }
995 Lex.Lex(); // eat the >
996
997 return Type;
998}
999
1000
Chris Lattnerf4601652007-11-22 20:49:04 +00001001/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1002///
1003/// SimpleValue ::= IDValue
1004/// SimpleValue ::= INTVAL
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001005/// SimpleValue ::= STRVAL+
Chris Lattnerf4601652007-11-22 20:49:04 +00001006/// SimpleValue ::= CODEFRAGMENT
1007/// SimpleValue ::= '?'
1008/// SimpleValue ::= '{' ValueList '}'
1009/// SimpleValue ::= ID '<' ValueListNE '>'
1010/// SimpleValue ::= '[' ValueList ']'
1011/// SimpleValue ::= '(' IDValue DagArgList ')'
1012/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
1013/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1014/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1015/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
1016/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1017///
David Greenee1b46912009-06-08 20:23:18 +00001018Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001019 Init *R = 0;
1020 switch (Lex.getCode()) {
1021 default: TokError("Unknown token when parsing a value"); break;
1022 case tgtok::IntVal: R = new IntInit(Lex.getCurIntVal()); Lex.Lex(); break;
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001023 case tgtok::StrVal: {
1024 std::string Val = Lex.getCurStrVal();
1025 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001026
Jim Grosbachda4231f2009-03-26 16:17:51 +00001027 // Handle multiple consecutive concatenated strings.
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001028 while (Lex.getCode() == tgtok::StrVal) {
1029 Val += Lex.getCurStrVal();
1030 Lex.Lex();
1031 }
Bob Wilson21870412009-11-22 04:24:42 +00001032
Chris Lattnerd7a50cf2009-03-11 17:08:13 +00001033 R = new StringInit(Val);
1034 break;
1035 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001036 case tgtok::CodeFragment:
Chris Lattner578bcf02010-10-06 04:31:40 +00001037 R = new CodeInit(Lex.getCurStrVal());
1038 Lex.Lex();
1039 break;
1040 case tgtok::question:
1041 R = new UnsetInit();
1042 Lex.Lex();
1043 break;
Chris Lattnerf4601652007-11-22 20:49:04 +00001044 case tgtok::Id: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001045 SMLoc NameLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001046 std::string Name = Lex.getCurStrVal();
1047 if (Lex.Lex() != tgtok::less) // consume the Id.
1048 return ParseIDValue(CurRec, Name, NameLoc); // Value ::= IDValue
Bob Wilson21870412009-11-22 04:24:42 +00001049
Chris Lattnerf4601652007-11-22 20:49:04 +00001050 // Value ::= ID '<' ValueListNE '>'
1051 if (Lex.Lex() == tgtok::greater) {
1052 TokError("expected non-empty value list");
1053 return 0;
1054 }
David Greenee1b46912009-06-08 20:23:18 +00001055
Chris Lattnerf4601652007-11-22 20:49:04 +00001056 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1057 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1058 // body.
1059 Record *Class = Records.getClass(Name);
1060 if (!Class) {
1061 Error(NameLoc, "Expected a class name, got '" + Name + "'");
1062 return 0;
1063 }
David Greenee1b46912009-06-08 20:23:18 +00001064
1065 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1066 if (ValueList.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001067
David Greenee1b46912009-06-08 20:23:18 +00001068 if (Lex.getCode() != tgtok::greater) {
1069 TokError("expected '>' at end of value list");
1070 return 0;
1071 }
1072 Lex.Lex(); // eat the '>'
Bob Wilson21870412009-11-22 04:24:42 +00001073
Chris Lattnerf4601652007-11-22 20:49:04 +00001074 // Create the new record, set it as CurRec temporarily.
1075 static unsigned AnonCounter = 0;
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001076 Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++),NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +00001077 SubClassReference SCRef;
1078 SCRef.RefLoc = NameLoc;
1079 SCRef.Rec = Class;
1080 SCRef.TemplateArgs = ValueList;
1081 // Add info about the subclass to NewRec.
1082 if (AddSubClass(NewRec, SCRef))
1083 return 0;
1084 NewRec->resolveReferences();
1085 Records.addDef(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001086
Chris Lattnerf4601652007-11-22 20:49:04 +00001087 // The result of the expression is a reference to the new record.
1088 return new DefInit(NewRec);
Bob Wilson21870412009-11-22 04:24:42 +00001089 }
Chris Lattnerf4601652007-11-22 20:49:04 +00001090 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001091 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001092 Lex.Lex(); // eat the '{'
1093 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001094
Chris Lattnerf4601652007-11-22 20:49:04 +00001095 if (Lex.getCode() != tgtok::r_brace) {
1096 Vals = ParseValueList(CurRec);
1097 if (Vals.empty()) return 0;
1098 }
1099 if (Lex.getCode() != tgtok::r_brace) {
1100 TokError("expected '}' at end of bit list value");
1101 return 0;
1102 }
1103 Lex.Lex(); // eat the '}'
Bob Wilson21870412009-11-22 04:24:42 +00001104
Chris Lattnerf4601652007-11-22 20:49:04 +00001105 BitsInit *Result = new BitsInit(Vals.size());
1106 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1107 Init *Bit = Vals[i]->convertInitializerTo(new BitRecTy());
1108 if (Bit == 0) {
Chris Lattner5d814862007-11-22 21:06:59 +00001109 Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+
1110 ") is not convertable to a bit");
Chris Lattnerf4601652007-11-22 20:49:04 +00001111 return 0;
1112 }
1113 Result->setBit(Vals.size()-i-1, Bit);
1114 }
1115 return Result;
1116 }
1117 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1118 Lex.Lex(); // eat the '['
1119 std::vector<Init*> Vals;
Bob Wilson21870412009-11-22 04:24:42 +00001120
David Greenee1b46912009-06-08 20:23:18 +00001121 RecTy *DeducedEltTy = 0;
1122 ListRecTy *GivenListTy = 0;
Bob Wilson21870412009-11-22 04:24:42 +00001123
David Greenee1b46912009-06-08 20:23:18 +00001124 if (ItemType != 0) {
1125 ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType);
1126 if (ListType == 0) {
1127 std::stringstream s;
Bob Wilson21870412009-11-22 04:24:42 +00001128 s << "Type mismatch for list, expected list type, got "
David Greenee1b46912009-06-08 20:23:18 +00001129 << ItemType->getAsString();
1130 TokError(s.str());
1131 }
1132 GivenListTy = ListType;
Bob Wilson21870412009-11-22 04:24:42 +00001133 }
David Greenee1b46912009-06-08 20:23:18 +00001134
Chris Lattnerf4601652007-11-22 20:49:04 +00001135 if (Lex.getCode() != tgtok::r_square) {
Bob Wilson21870412009-11-22 04:24:42 +00001136 Vals = ParseValueList(CurRec, 0,
1137 GivenListTy ? GivenListTy->getElementType() : 0);
Chris Lattnerf4601652007-11-22 20:49:04 +00001138 if (Vals.empty()) return 0;
1139 }
1140 if (Lex.getCode() != tgtok::r_square) {
1141 TokError("expected ']' at end of list value");
1142 return 0;
1143 }
1144 Lex.Lex(); // eat the ']'
David Greenee1b46912009-06-08 20:23:18 +00001145
1146 RecTy *GivenEltTy = 0;
1147 if (Lex.getCode() == tgtok::less) {
1148 // Optional list element type
1149 Lex.Lex(); // eat the '<'
1150
1151 GivenEltTy = ParseType();
1152 if (GivenEltTy == 0) {
1153 // Couldn't parse element type
1154 return 0;
1155 }
1156
1157 if (Lex.getCode() != tgtok::greater) {
1158 TokError("expected '>' at end of list element type");
1159 return 0;
1160 }
1161 Lex.Lex(); // eat the '>'
1162 }
1163
1164 // Check elements
1165 RecTy *EltTy = 0;
1166 for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end();
1167 i != ie;
1168 ++i) {
1169 TypedInit *TArg = dynamic_cast<TypedInit*>(*i);
1170 if (TArg == 0) {
1171 TokError("Untyped list element");
1172 return 0;
1173 }
1174 if (EltTy != 0) {
1175 EltTy = resolveTypes(EltTy, TArg->getType());
1176 if (EltTy == 0) {
1177 TokError("Incompatible types in list elements");
1178 return 0;
1179 }
Bob Wilson21870412009-11-22 04:24:42 +00001180 } else {
David Greenee1b46912009-06-08 20:23:18 +00001181 EltTy = TArg->getType();
1182 }
1183 }
1184
1185 if (GivenEltTy != 0) {
1186 if (EltTy != 0) {
1187 // Verify consistency
1188 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1189 TokError("Incompatible types in list elements");
1190 return 0;
1191 }
1192 }
1193 EltTy = GivenEltTy;
1194 }
1195
1196 if (EltTy == 0) {
1197 if (ItemType == 0) {
1198 TokError("No type for list");
1199 return 0;
1200 }
1201 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson21870412009-11-22 04:24:42 +00001202 } else {
David Greenee1b46912009-06-08 20:23:18 +00001203 // Make sure the deduced type is compatible with the given type
1204 if (GivenListTy) {
1205 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
1206 TokError("Element type mismatch for list");
1207 return 0;
1208 }
1209 }
1210 DeducedEltTy = EltTy;
1211 }
Bob Wilson21870412009-11-22 04:24:42 +00001212
David Greenee1b46912009-06-08 20:23:18 +00001213 return new ListInit(Vals, DeducedEltTy);
Chris Lattnerf4601652007-11-22 20:49:04 +00001214 }
1215 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1216 Lex.Lex(); // eat the '('
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001217 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner3dc2e962008-04-10 04:48:34 +00001218 TokError("expected identifier in dag init");
1219 return 0;
1220 }
Bob Wilson21870412009-11-22 04:24:42 +00001221
Chris Lattner46f55522010-10-06 04:55:48 +00001222 Init *Operator;
1223 /// LLVMC Requires an old grammar and I don't know how to update it, placate
1224 /// it in the short term by changing the grammar specifically for llvmc.
1225 /// FIXME: REMOVE THIS.
1226 if (!LLVMCHack)
1227 Operator = ParseValue(CurRec);
1228 else {
1229 if (Lex.getCode() == tgtok::Id)
1230 Operator = ParseIDValue(CurRec);
1231 else
1232 Operator = ParseOperation(CurRec);
1233 }
Chris Lattner578bcf02010-10-06 04:31:40 +00001234 if (Operator == 0) return 0;
David Greenec7cafcd2009-04-22 20:18:10 +00001235
Nate Begeman7cee8172009-03-19 05:21:56 +00001236 // If the operator name is present, parse it.
1237 std::string OperatorName;
1238 if (Lex.getCode() == tgtok::colon) {
1239 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1240 TokError("expected variable name in dag operator");
1241 return 0;
1242 }
1243 OperatorName = Lex.getCurStrVal();
1244 Lex.Lex(); // eat the VarName.
1245 }
Bob Wilson21870412009-11-22 04:24:42 +00001246
Chris Lattnerf4601652007-11-22 20:49:04 +00001247 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1248 if (Lex.getCode() != tgtok::r_paren) {
1249 DagArgs = ParseDagArgList(CurRec);
1250 if (DagArgs.empty()) return 0;
1251 }
Bob Wilson21870412009-11-22 04:24:42 +00001252
Chris Lattnerf4601652007-11-22 20:49:04 +00001253 if (Lex.getCode() != tgtok::r_paren) {
1254 TokError("expected ')' in dag init");
1255 return 0;
1256 }
1257 Lex.Lex(); // eat the ')'
Bob Wilson21870412009-11-22 04:24:42 +00001258
Nate Begeman7cee8172009-03-19 05:21:56 +00001259 return new DagInit(Operator, OperatorName, DagArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +00001260 }
Bob Wilson21870412009-11-22 04:24:42 +00001261
David Greene5f9f9ba2009-05-14 22:38:31 +00001262 case tgtok::XCar:
1263 case tgtok::XCdr:
1264 case tgtok::XNull:
David Greenee6c27de2009-05-14 21:22:49 +00001265 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Chris Lattnerf4601652007-11-22 20:49:04 +00001266 case tgtok::XConcat:
Bob Wilson21870412009-11-22 04:24:42 +00001267 case tgtok::XSRA:
Chris Lattnerf4601652007-11-22 20:49:04 +00001268 case tgtok::XSRL:
1269 case tgtok::XSHL:
David Greene6786d5e2010-01-05 19:11:42 +00001270 case tgtok::XEq:
Chris Lattnerc7252ce2010-10-06 00:19:21 +00001271 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene9bea7c82009-05-14 23:26:46 +00001272 case tgtok::XIf:
David Greenebeb31a52009-05-14 22:23:47 +00001273 case tgtok::XForEach:
David Greene4afc5092009-05-14 21:54:42 +00001274 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
David Greened418c1b2009-05-14 20:54:48 +00001275 return ParseOperation(CurRec);
Chris Lattnerf4601652007-11-22 20:49:04 +00001276 }
1277 }
Bob Wilson21870412009-11-22 04:24:42 +00001278
Chris Lattnerf4601652007-11-22 20:49:04 +00001279 return R;
1280}
1281
1282/// ParseValue - Parse a tblgen value. This returns null on error.
1283///
1284/// Value ::= SimpleValue ValueSuffix*
1285/// ValueSuffix ::= '{' BitList '}'
1286/// ValueSuffix ::= '[' BitList ']'
1287/// ValueSuffix ::= '.' ID
1288///
David Greenee1b46912009-06-08 20:23:18 +00001289Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType) {
1290 Init *Result = ParseSimpleValue(CurRec, ItemType);
Chris Lattnerf4601652007-11-22 20:49:04 +00001291 if (Result == 0) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001292
Chris Lattnerf4601652007-11-22 20:49:04 +00001293 // Parse the suffixes now if present.
1294 while (1) {
1295 switch (Lex.getCode()) {
1296 default: return Result;
1297 case tgtok::l_brace: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001298 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001299 Lex.Lex(); // eat the '{'
1300 std::vector<unsigned> Ranges = ParseRangeList();
1301 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001302
Chris Lattnerf4601652007-11-22 20:49:04 +00001303 // Reverse the bitlist.
1304 std::reverse(Ranges.begin(), Ranges.end());
1305 Result = Result->convertInitializerBitRange(Ranges);
1306 if (Result == 0) {
1307 Error(CurlyLoc, "Invalid bit range for value");
1308 return 0;
1309 }
Bob Wilson21870412009-11-22 04:24:42 +00001310
Chris Lattnerf4601652007-11-22 20:49:04 +00001311 // Eat the '}'.
1312 if (Lex.getCode() != tgtok::r_brace) {
1313 TokError("expected '}' at end of bit range list");
1314 return 0;
1315 }
1316 Lex.Lex();
1317 break;
1318 }
1319 case tgtok::l_square: {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001320 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001321 Lex.Lex(); // eat the '['
1322 std::vector<unsigned> Ranges = ParseRangeList();
1323 if (Ranges.empty()) return 0;
Bob Wilson21870412009-11-22 04:24:42 +00001324
Chris Lattnerf4601652007-11-22 20:49:04 +00001325 Result = Result->convertInitListSlice(Ranges);
1326 if (Result == 0) {
1327 Error(SquareLoc, "Invalid range for list slice");
1328 return 0;
1329 }
Bob Wilson21870412009-11-22 04:24:42 +00001330
Chris Lattnerf4601652007-11-22 20:49:04 +00001331 // Eat the ']'.
1332 if (Lex.getCode() != tgtok::r_square) {
1333 TokError("expected ']' at end of list slice");
1334 return 0;
1335 }
1336 Lex.Lex();
1337 break;
1338 }
1339 case tgtok::period:
1340 if (Lex.Lex() != tgtok::Id) { // eat the .
1341 TokError("expected field identifier after '.'");
1342 return 0;
1343 }
1344 if (!Result->getFieldType(Lex.getCurStrVal())) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001345 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner5d814862007-11-22 21:06:59 +00001346 Result->getAsString() + "'");
Chris Lattnerf4601652007-11-22 20:49:04 +00001347 return 0;
1348 }
1349 Result = new FieldInit(Result, Lex.getCurStrVal());
1350 Lex.Lex(); // eat field name
1351 break;
1352 }
1353 }
1354}
1355
1356/// ParseDagArgList - Parse the argument list for a dag literal expression.
1357///
1358/// ParseDagArgList ::= Value (':' VARNAME)?
1359/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)?
Bob Wilson21870412009-11-22 04:24:42 +00001360std::vector<std::pair<llvm::Init*, std::string> >
Chris Lattnerf4601652007-11-22 20:49:04 +00001361TGParser::ParseDagArgList(Record *CurRec) {
1362 std::vector<std::pair<llvm::Init*, std::string> > Result;
Bob Wilson21870412009-11-22 04:24:42 +00001363
Chris Lattnerf4601652007-11-22 20:49:04 +00001364 while (1) {
1365 Init *Val = ParseValue(CurRec);
1366 if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >();
Bob Wilson21870412009-11-22 04:24:42 +00001367
Chris Lattnerf4601652007-11-22 20:49:04 +00001368 // If the variable name is present, add it.
1369 std::string VarName;
1370 if (Lex.getCode() == tgtok::colon) {
1371 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1372 TokError("expected variable name in dag literal");
1373 return std::vector<std::pair<llvm::Init*, std::string> >();
1374 }
1375 VarName = Lex.getCurStrVal();
1376 Lex.Lex(); // eat the VarName.
1377 }
Bob Wilson21870412009-11-22 04:24:42 +00001378
Chris Lattnerf4601652007-11-22 20:49:04 +00001379 Result.push_back(std::make_pair(Val, VarName));
Bob Wilson21870412009-11-22 04:24:42 +00001380
Chris Lattnerf4601652007-11-22 20:49:04 +00001381 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson21870412009-11-22 04:24:42 +00001382 Lex.Lex(); // eat the ','
Chris Lattnerf4601652007-11-22 20:49:04 +00001383 }
Bob Wilson21870412009-11-22 04:24:42 +00001384
Chris Lattnerf4601652007-11-22 20:49:04 +00001385 return Result;
1386}
1387
1388
1389/// ParseValueList - Parse a comma separated list of values, returning them as a
1390/// vector. Note that this always expects to be able to parse at least one
1391/// value. It returns an empty list if this is not possible.
1392///
1393/// ValueList ::= Value (',' Value)
1394///
Bob Wilson21870412009-11-22 04:24:42 +00001395std::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec,
1396 RecTy *EltTy) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001397 std::vector<Init*> Result;
David Greenee1b46912009-06-08 20:23:18 +00001398 RecTy *ItemType = EltTy;
David Greene67acdf22009-06-29 19:59:52 +00001399 unsigned int ArgN = 0;
David Greenee1b46912009-06-08 20:23:18 +00001400 if (ArgsRec != 0 && EltTy == 0) {
1401 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
1402 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1403 assert(RV && "Template argument record not found??");
1404 ItemType = RV->getType();
1405 ++ArgN;
1406 }
1407 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001408 if (Result.back() == 0) return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001409
Chris Lattnerf4601652007-11-22 20:49:04 +00001410 while (Lex.getCode() == tgtok::comma) {
1411 Lex.Lex(); // Eat the comma
Bob Wilson21870412009-11-22 04:24:42 +00001412
David Greenee1b46912009-06-08 20:23:18 +00001413 if (ArgsRec != 0 && EltTy == 0) {
1414 const std::vector<std::string> &TArgs = ArgsRec->getTemplateArgs();
David Greene67acdf22009-06-29 19:59:52 +00001415 if (ArgN >= TArgs.size()) {
1416 TokError("too many template arguments");
1417 return std::vector<Init*>();
Bob Wilson21870412009-11-22 04:24:42 +00001418 }
David Greenee1b46912009-06-08 20:23:18 +00001419 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
1420 assert(RV && "Template argument record not found??");
1421 ItemType = RV->getType();
1422 ++ArgN;
1423 }
1424 Result.push_back(ParseValue(CurRec, ItemType));
Chris Lattnerf4601652007-11-22 20:49:04 +00001425 if (Result.back() == 0) return std::vector<Init*>();
1426 }
Bob Wilson21870412009-11-22 04:24:42 +00001427
Chris Lattnerf4601652007-11-22 20:49:04 +00001428 return Result;
1429}
1430
1431
Chris Lattnerf4601652007-11-22 20:49:04 +00001432/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
1433/// empty string on error. This can happen in a number of different context's,
1434/// including within a def or in the template args for a def (which which case
1435/// CurRec will be non-null) and within the template args for a multiclass (in
1436/// which case CurRec will be null, but CurMultiClass will be set). This can
1437/// also happen within a def that is within a multiclass, which will set both
1438/// CurRec and CurMultiClass.
1439///
1440/// Declaration ::= FIELD? Type ID ('=' Value)?
1441///
Bob Wilson21870412009-11-22 04:24:42 +00001442std::string TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4601652007-11-22 20:49:04 +00001443 bool ParsingTemplateArgs) {
1444 // Read the field prefix if present.
1445 bool HasField = Lex.getCode() == tgtok::Field;
1446 if (HasField) Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001447
Chris Lattnerf4601652007-11-22 20:49:04 +00001448 RecTy *Type = ParseType();
1449 if (Type == 0) return "";
Bob Wilson21870412009-11-22 04:24:42 +00001450
Chris Lattnerf4601652007-11-22 20:49:04 +00001451 if (Lex.getCode() != tgtok::Id) {
1452 TokError("Expected identifier in declaration");
1453 return "";
1454 }
Bob Wilson21870412009-11-22 04:24:42 +00001455
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001456 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001457 std::string DeclName = Lex.getCurStrVal();
1458 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001459
Chris Lattnerf4601652007-11-22 20:49:04 +00001460 if (ParsingTemplateArgs) {
1461 if (CurRec) {
1462 DeclName = CurRec->getName() + ":" + DeclName;
1463 } else {
1464 assert(CurMultiClass);
1465 }
1466 if (CurMultiClass)
1467 DeclName = CurMultiClass->Rec.getName() + "::" + DeclName;
1468 }
Bob Wilson21870412009-11-22 04:24:42 +00001469
Chris Lattnerf4601652007-11-22 20:49:04 +00001470 // Add the value.
1471 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
1472 return "";
Bob Wilson21870412009-11-22 04:24:42 +00001473
Chris Lattnerf4601652007-11-22 20:49:04 +00001474 // If a value is present, parse it.
1475 if (Lex.getCode() == tgtok::equal) {
1476 Lex.Lex();
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001477 SMLoc ValLoc = Lex.getLoc();
David Greenee1b46912009-06-08 20:23:18 +00001478 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001479 if (Val == 0 ||
1480 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1481 return "";
1482 }
Bob Wilson21870412009-11-22 04:24:42 +00001483
Chris Lattnerf4601652007-11-22 20:49:04 +00001484 return DeclName;
1485}
1486
1487/// ParseTemplateArgList - Read a template argument list, which is a non-empty
1488/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
1489/// template args for a def, which may or may not be in a multiclass. If null,
1490/// these are the template args for a multiclass.
1491///
1492/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson21870412009-11-22 04:24:42 +00001493///
Chris Lattnerf4601652007-11-22 20:49:04 +00001494bool TGParser::ParseTemplateArgList(Record *CurRec) {
1495 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
1496 Lex.Lex(); // eat the '<'
Bob Wilson21870412009-11-22 04:24:42 +00001497
Chris Lattnerf4601652007-11-22 20:49:04 +00001498 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson21870412009-11-22 04:24:42 +00001499
Chris Lattnerf4601652007-11-22 20:49:04 +00001500 // Read the first declaration.
1501 std::string TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1502 if (TemplArg.empty())
1503 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001504
Chris Lattnerf4601652007-11-22 20:49:04 +00001505 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson21870412009-11-22 04:24:42 +00001506
Chris Lattnerf4601652007-11-22 20:49:04 +00001507 while (Lex.getCode() == tgtok::comma) {
1508 Lex.Lex(); // eat the ','
Bob Wilson21870412009-11-22 04:24:42 +00001509
Chris Lattnerf4601652007-11-22 20:49:04 +00001510 // Read the following declarations.
1511 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
1512 if (TemplArg.empty())
1513 return true;
1514 TheRecToAddTo->addTemplateArg(TemplArg);
1515 }
Bob Wilson21870412009-11-22 04:24:42 +00001516
Chris Lattnerf4601652007-11-22 20:49:04 +00001517 if (Lex.getCode() != tgtok::greater)
1518 return TokError("expected '>' at end of template argument list");
1519 Lex.Lex(); // eat the '>'.
1520 return false;
1521}
1522
1523
1524/// ParseBodyItem - Parse a single item at within the body of a def or class.
1525///
1526/// BodyItem ::= Declaration ';'
1527/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
1528bool TGParser::ParseBodyItem(Record *CurRec) {
1529 if (Lex.getCode() != tgtok::Let) {
Bob Wilson21870412009-11-22 04:24:42 +00001530 if (ParseDeclaration(CurRec, false).empty())
Chris Lattnerf4601652007-11-22 20:49:04 +00001531 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001532
Chris Lattnerf4601652007-11-22 20:49:04 +00001533 if (Lex.getCode() != tgtok::semi)
1534 return TokError("expected ';' after declaration");
1535 Lex.Lex();
1536 return false;
1537 }
1538
1539 // LET ID OptionalRangeList '=' Value ';'
1540 if (Lex.Lex() != tgtok::Id)
1541 return TokError("expected field identifier after let");
Bob Wilson21870412009-11-22 04:24:42 +00001542
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001543 SMLoc IdLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001544 std::string FieldName = Lex.getCurStrVal();
1545 Lex.Lex(); // eat the field name.
Bob Wilson21870412009-11-22 04:24:42 +00001546
Chris Lattnerf4601652007-11-22 20:49:04 +00001547 std::vector<unsigned> BitList;
Bob Wilson21870412009-11-22 04:24:42 +00001548 if (ParseOptionalBitList(BitList))
Chris Lattnerf4601652007-11-22 20:49:04 +00001549 return true;
1550 std::reverse(BitList.begin(), BitList.end());
Bob Wilson21870412009-11-22 04:24:42 +00001551
Chris Lattnerf4601652007-11-22 20:49:04 +00001552 if (Lex.getCode() != tgtok::equal)
1553 return TokError("expected '=' in let expression");
1554 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001555
David Greenee1b46912009-06-08 20:23:18 +00001556 RecordVal *Field = CurRec->getValue(FieldName);
1557 if (Field == 0)
1558 return TokError("Value '" + FieldName + "' unknown!");
1559
1560 RecTy *Type = Field->getType();
Bob Wilson21870412009-11-22 04:24:42 +00001561
David Greenee1b46912009-06-08 20:23:18 +00001562 Init *Val = ParseValue(CurRec, Type);
Chris Lattnerf4601652007-11-22 20:49:04 +00001563 if (Val == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001564
Chris Lattnerf4601652007-11-22 20:49:04 +00001565 if (Lex.getCode() != tgtok::semi)
1566 return TokError("expected ';' after let expression");
1567 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001568
Chris Lattnerf4601652007-11-22 20:49:04 +00001569 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1570}
1571
1572/// ParseBody - Read the body of a class or def. Return true on error, false on
1573/// success.
1574///
1575/// Body ::= ';'
1576/// Body ::= '{' BodyList '}'
1577/// BodyList BodyItem*
1578///
1579bool TGParser::ParseBody(Record *CurRec) {
1580 // If this is a null definition, just eat the semi and return.
1581 if (Lex.getCode() == tgtok::semi) {
1582 Lex.Lex();
1583 return false;
1584 }
Bob Wilson21870412009-11-22 04:24:42 +00001585
Chris Lattnerf4601652007-11-22 20:49:04 +00001586 if (Lex.getCode() != tgtok::l_brace)
1587 return TokError("Expected ';' or '{' to start body");
1588 // Eat the '{'.
1589 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001590
Chris Lattnerf4601652007-11-22 20:49:04 +00001591 while (Lex.getCode() != tgtok::r_brace)
1592 if (ParseBodyItem(CurRec))
1593 return true;
1594
1595 // Eat the '}'.
1596 Lex.Lex();
1597 return false;
1598}
1599
1600/// ParseObjectBody - Parse the body of a def or class. This consists of an
1601/// optional ClassList followed by a Body. CurRec is the current def or class
1602/// that is being parsed.
1603///
1604/// ObjectBody ::= BaseClassList Body
1605/// BaseClassList ::= /*empty*/
1606/// BaseClassList ::= ':' BaseClassListNE
1607/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
1608///
1609bool TGParser::ParseObjectBody(Record *CurRec) {
1610 // If there is a baseclass list, read it.
1611 if (Lex.getCode() == tgtok::colon) {
1612 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001613
Chris Lattnerf4601652007-11-22 20:49:04 +00001614 // Read all of the subclasses.
1615 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
1616 while (1) {
1617 // Check for error.
1618 if (SubClass.Rec == 0) return true;
Bob Wilson21870412009-11-22 04:24:42 +00001619
Chris Lattnerf4601652007-11-22 20:49:04 +00001620 // Add it.
1621 if (AddSubClass(CurRec, SubClass))
1622 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001623
Chris Lattnerf4601652007-11-22 20:49:04 +00001624 if (Lex.getCode() != tgtok::comma) break;
1625 Lex.Lex(); // eat ','.
1626 SubClass = ParseSubClassReference(CurRec, false);
1627 }
1628 }
1629
1630 // Process any variables on the let stack.
1631 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1632 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1633 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1634 LetStack[i][j].Bits, LetStack[i][j].Value))
1635 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001636
Chris Lattnerf4601652007-11-22 20:49:04 +00001637 return ParseBody(CurRec);
1638}
1639
Chris Lattnerf4601652007-11-22 20:49:04 +00001640/// ParseDef - Parse and return a top level or multiclass def, return the record
1641/// corresponding to it. This returns null on error.
1642///
1643/// DefInst ::= DEF ObjectName ObjectBody
1644///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001645bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001646 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001647 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson21870412009-11-22 04:24:42 +00001648 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4601652007-11-22 20:49:04 +00001649
1650 // Parse ObjectName and make a record for it.
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001651 Record *CurRec = new Record(ParseObjectName(), DefLoc);
Bob Wilson21870412009-11-22 04:24:42 +00001652
Chris Lattnerf4601652007-11-22 20:49:04 +00001653 if (!CurMultiClass) {
1654 // Top-level def definition.
Bob Wilson21870412009-11-22 04:24:42 +00001655
Chris Lattnerf4601652007-11-22 20:49:04 +00001656 // Ensure redefinition doesn't happen.
1657 if (Records.getDef(CurRec->getName())) {
1658 Error(DefLoc, "def '" + CurRec->getName() + "' already defined");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001659 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001660 }
1661 Records.addDef(CurRec);
1662 } else {
1663 // Otherwise, a def inside a multiclass, add it to the multiclass.
1664 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i)
1665 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
1666 Error(DefLoc, "def '" + CurRec->getName() +
1667 "' already defined in this multiclass!");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001668 return true;
Chris Lattnerf4601652007-11-22 20:49:04 +00001669 }
1670 CurMultiClass->DefPrototypes.push_back(CurRec);
1671 }
Bob Wilson21870412009-11-22 04:24:42 +00001672
Chris Lattnerf4601652007-11-22 20:49:04 +00001673 if (ParseObjectBody(CurRec))
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001674 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001675
Chris Lattnerf4601652007-11-22 20:49:04 +00001676 if (CurMultiClass == 0) // Def's in multiclasses aren't really defs.
1677 CurRec->resolveReferences();
Bob Wilson21870412009-11-22 04:24:42 +00001678
Chris Lattnerf4601652007-11-22 20:49:04 +00001679 // If ObjectBody has template arguments, it's an error.
1680 assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001681
1682 if (CurMultiClass) {
1683 // Copy the template arguments for the multiclass into the def.
1684 const std::vector<std::string> &TArgs =
1685 CurMultiClass->Rec.getTemplateArgs();
1686
1687 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
1688 const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]);
1689 assert(RV && "Template arg doesn't exist?");
1690 CurRec->addValue(*RV);
1691 }
1692 }
1693
1694 return false;
Chris Lattnerf4601652007-11-22 20:49:04 +00001695}
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();
Bob Wilson21870412009-11-22 04:24:42 +00001705
Chris Lattnerf4601652007-11-22 20:49:04 +00001706 if (Lex.getCode() != tgtok::Id)
1707 return TokError("expected class name after 'class' keyword");
Bob Wilson21870412009-11-22 04:24:42 +00001708
Chris Lattnerf4601652007-11-22 20:49:04 +00001709 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 Lattner7b9ffe42009-03-13 16:09:24 +00001718 CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001719 Records.addClass(CurRec);
1720 }
1721 Lex.Lex(); // eat the name.
Bob Wilson21870412009-11-22 04:24:42 +00001722
Chris Lattnerf4601652007-11-22 20:49:04 +00001723 // 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;
Bob Wilson21870412009-11-22 04:24:42 +00001740
Chris Lattnerf4601652007-11-22 20:49:04 +00001741 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 Lattner1e3a8a42009-06-21 03:39:35 +00001747 SMLoc NameLoc = Lex.getLoc();
Bob Wilson21870412009-11-22 04:24:42 +00001748 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4601652007-11-22 20:49:04 +00001749
1750 // Check for an optional RangeList.
1751 std::vector<unsigned> Bits;
Bob Wilson21870412009-11-22 04:24:42 +00001752 if (ParseOptionalRangeList(Bits))
Chris Lattnerf4601652007-11-22 20:49:04 +00001753 return std::vector<LetRecord>();
1754 std::reverse(Bits.begin(), Bits.end());
Bob Wilson21870412009-11-22 04:24:42 +00001755
Chris Lattnerf4601652007-11-22 20:49:04 +00001756 if (Lex.getCode() != tgtok::equal) {
1757 TokError("expected '=' in let expression");
1758 return std::vector<LetRecord>();
1759 }
1760 Lex.Lex(); // eat the '='.
Bob Wilson21870412009-11-22 04:24:42 +00001761
Chris Lattnerf4601652007-11-22 20:49:04 +00001762 Init *Val = ParseValue(0);
1763 if (Val == 0) return std::vector<LetRecord>();
Bob Wilson21870412009-11-22 04:24:42 +00001764
Chris Lattnerf4601652007-11-22 20:49:04 +00001765 // Now that we have everything, add the record.
1766 Result.push_back(LetRecord(Name, Bits, Val, NameLoc));
Bob Wilson21870412009-11-22 04:24:42 +00001767
Chris Lattnerf4601652007-11-22 20:49:04 +00001768 if (Lex.getCode() != tgtok::comma)
1769 return Result;
Bob Wilson21870412009-11-22 04:24:42 +00001770 Lex.Lex(); // eat the comma.
Chris Lattnerf4601652007-11-22 20:49:04 +00001771 }
1772}
1773
1774/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001775/// different related productions. This works inside multiclasses too.
Chris Lattnerf4601652007-11-22 20:49:04 +00001776///
1777/// Object ::= LET LetList IN '{' ObjectList '}'
1778/// Object ::= LET LetList IN Object
1779///
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001780bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001781 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
1782 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00001783
Chris Lattnerf4601652007-11-22 20:49:04 +00001784 // 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();
Bob Wilson21870412009-11-22 04:24:42 +00001792
Chris Lattnerf4601652007-11-22 20:49:04 +00001793 // If this is a scalar let, just handle it now
1794 if (Lex.getCode() != tgtok::l_brace) {
1795 // LET LetList IN Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001796 if (ParseObject(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001797 return true;
1798 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001799 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001800 // Otherwise, this is a group let.
1801 Lex.Lex(); // eat the '{'.
Bob Wilson21870412009-11-22 04:24:42 +00001802
Chris Lattnerf4601652007-11-22 20:49:04 +00001803 // Parse the object list.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001804 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4601652007-11-22 20:49:04 +00001805 return true;
Bob Wilson21870412009-11-22 04:24:42 +00001806
Chris Lattnerf4601652007-11-22 20:49:04 +00001807 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 }
Bob Wilson21870412009-11-22 04:24:42 +00001813
Chris Lattnerf4601652007-11-22 20:49:04 +00001814 // Outside this let scope, this let block is not active.
1815 LetStack.pop_back();
1816 return false;
1817}
1818
Chris Lattnerf4601652007-11-22 20:49:04 +00001819/// ParseMultiClass - Parse a multiclass definition.
1820///
Bob Wilson32558652009-04-28 19:41:44 +00001821/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
1822/// ':' BaseMultiClassList '{' MultiClassDef+ '}'
Chris Lattnerf4601652007-11-22 20:49:04 +00001823///
1824bool TGParser::ParseMultiClass() {
1825 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
1826 Lex.Lex(); // Eat the multiclass token.
1827
1828 if (Lex.getCode() != tgtok::Id)
1829 return TokError("expected identifier after multiclass for name");
1830 std::string Name = Lex.getCurStrVal();
Bob Wilson21870412009-11-22 04:24:42 +00001831
Chris Lattnerf4601652007-11-22 20:49:04 +00001832 if (MultiClasses.count(Name))
1833 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson21870412009-11-22 04:24:42 +00001834
Chris Lattner7b9ffe42009-03-13 16:09:24 +00001835 CurMultiClass = MultiClasses[Name] = new MultiClass(Name, Lex.getLoc());
Chris Lattnerf4601652007-11-22 20:49:04 +00001836 Lex.Lex(); // Eat the identifier.
Bob Wilson21870412009-11-22 04:24:42 +00001837
Chris Lattnerf4601652007-11-22 20:49:04 +00001838 // If there are template args, parse them.
1839 if (Lex.getCode() == tgtok::less)
1840 if (ParseTemplateArgList(0))
1841 return true;
1842
David Greened34a73b2009-04-24 16:55:41 +00001843 bool inherits = false;
1844
David Greenede444af2009-04-22 16:42:54 +00001845 // If there are submulticlasses, parse them.
1846 if (Lex.getCode() == tgtok::colon) {
David Greened34a73b2009-04-24 16:55:41 +00001847 inherits = true;
1848
David Greenede444af2009-04-22 16:42:54 +00001849 Lex.Lex();
Bob Wilson32558652009-04-28 19:41:44 +00001850
David Greenede444af2009-04-22 16:42:54 +00001851 // Read all of the submulticlasses.
Bob Wilson32558652009-04-28 19:41:44 +00001852 SubMultiClassReference SubMultiClass =
1853 ParseSubMultiClassReference(CurMultiClass);
David Greenede444af2009-04-22 16:42:54 +00001854 while (1) {
1855 // Check for error.
1856 if (SubMultiClass.MC == 0) return true;
Bob Wilson32558652009-04-28 19:41:44 +00001857
David Greenede444af2009-04-22 16:42:54 +00001858 // Add it.
1859 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
1860 return true;
Bob Wilson32558652009-04-28 19:41:44 +00001861
David Greenede444af2009-04-22 16:42:54 +00001862 if (Lex.getCode() != tgtok::comma) break;
1863 Lex.Lex(); // eat ','.
1864 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
1865 }
1866 }
1867
David Greened34a73b2009-04-24 16:55:41 +00001868 if (Lex.getCode() != tgtok::l_brace) {
1869 if (!inherits)
1870 return TokError("expected '{' in multiclass definition");
Bob Wilson21870412009-11-22 04:24:42 +00001871 else if (Lex.getCode() != tgtok::semi)
1872 return TokError("expected ';' in multiclass definition");
David Greened34a73b2009-04-24 16:55:41 +00001873 else
Bob Wilson21870412009-11-22 04:24:42 +00001874 Lex.Lex(); // eat the ';'.
1875 } else {
David Greened34a73b2009-04-24 16:55:41 +00001876 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
1877 return TokError("multiclass must contain at least one def");
Bob Wilson21870412009-11-22 04:24:42 +00001878
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001879 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00001880 switch (Lex.getCode()) {
1881 default:
1882 return TokError("expected 'let', 'def' or 'defm' in multiclass body");
1883 case tgtok::Let:
1884 case tgtok::Def:
1885 case tgtok::Defm:
1886 if (ParseObject(CurMultiClass))
1887 return true;
1888 break;
1889 }
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001890 }
David Greened34a73b2009-04-24 16:55:41 +00001891 Lex.Lex(); // eat the '}'.
1892 }
Bob Wilson21870412009-11-22 04:24:42 +00001893
Chris Lattnerf4601652007-11-22 20:49:04 +00001894 CurMultiClass = 0;
1895 return false;
1896}
1897
1898/// ParseDefm - Parse the instantiation of a multiclass.
1899///
1900/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
1901///
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00001902bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4601652007-11-22 20:49:04 +00001903 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Bob Wilson21870412009-11-22 04:24:42 +00001904
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001905 std::string DefmPrefix;
1906 if (Lex.Lex() == tgtok::Id) { // eat the defm.
1907 DefmPrefix = Lex.getCurStrVal();
1908 Lex.Lex(); // Eat the defm prefix.
1909 }
1910
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001911 SMLoc DefmPrefixLoc = Lex.getLoc();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001912 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4601652007-11-22 20:49:04 +00001913 return TokError("expected ':' after defm identifier");
Bob Wilson21870412009-11-22 04:24:42 +00001914
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00001915 // Keep track of the new generated record definitions.
1916 std::vector<Record*> NewRecDefs;
1917
1918 // This record also inherits from a regular class (non-multiclass)?
1919 bool InheritFromClass = false;
1920
Chris Lattnerf4601652007-11-22 20:49:04 +00001921 // eat the colon.
1922 Lex.Lex();
1923
Chris Lattner1e3a8a42009-06-21 03:39:35 +00001924 SMLoc SubClassLoc = Lex.getLoc();
Chris Lattnerf4601652007-11-22 20:49:04 +00001925 SubClassReference Ref = ParseSubClassReference(0, true);
David Greene56546132009-04-22 22:17:51 +00001926
1927 while (1) {
1928 if (Ref.Rec == 0) return true;
1929
1930 // To instantiate a multiclass, we need to first get the multiclass, then
1931 // instantiate each def contained in the multiclass with the SubClassRef
1932 // template parameters.
1933 MultiClass *MC = MultiClasses[Ref.Rec->getName()];
1934 assert(MC && "Didn't lookup multiclass correctly?");
Bob Wilson21870412009-11-22 04:24:42 +00001935 std::vector<Init*> &TemplateVals = Ref.TemplateArgs;
David Greene56546132009-04-22 22:17:51 +00001936
1937 // Verify that the correct number of template arguments were specified.
1938 const std::vector<std::string> &TArgs = MC->Rec.getTemplateArgs();
1939 if (TArgs.size() < TemplateVals.size())
1940 return Error(SubClassLoc,
1941 "more template args specified than multiclass expects");
1942
1943 // Loop over all the def's in the multiclass, instantiating each one.
1944 for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) {
1945 Record *DefProto = MC->DefPrototypes[i];
1946
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001947 // Add in the defm name. If the defm prefix is empty, give each
1948 // instantiated def a unique name. Otherwise, if "#NAME#" exists in the
1949 // name, substitute the prefix for #NAME#. Otherwise, use the defm name
1950 // as a prefix.
David Greene065f2592009-05-05 16:28:25 +00001951 std::string DefName = DefProto->getName();
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001952 if (DefmPrefix.empty()) {
1953 DefName = GetNewAnonymousName();
Bob Wilson21870412009-11-22 04:24:42 +00001954 } else {
Chris Lattnerdf72eae2010-10-05 22:51:56 +00001955 std::string::size_type idx = DefName.find("#NAME#");
1956 if (idx != std::string::npos) {
1957 DefName.replace(idx, 6, DefmPrefix);
1958 } else {
1959 // Add the suffix to the defm name to get the new name.
1960 DefName = DefmPrefix + DefName;
1961 }
David Greene065f2592009-05-05 16:28:25 +00001962 }
1963
1964 Record *CurRec = new Record(DefName, DefmPrefixLoc);
David Greene56546132009-04-22 22:17:51 +00001965
1966 SubClassReference Ref;
1967 Ref.RefLoc = DefmPrefixLoc;
1968 Ref.Rec = DefProto;
1969 AddSubClass(CurRec, Ref);
1970
1971 // Loop over all of the template arguments, setting them to the specified
1972 // value or leaving them as the default if necessary.
1973 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
Bob Wilson32558652009-04-28 19:41:44 +00001974 // Check if a value is specified for this temp-arg.
1975 if (i < TemplateVals.size()) {
David Greene56546132009-04-22 22:17:51 +00001976 // Set it now.
1977 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
1978 TemplateVals[i]))
1979 return true;
1980
1981 // Resolve it next.
1982 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
1983
1984 // Now remove it.
1985 CurRec->removeValue(TArgs[i]);
1986
1987 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Bob Wilson32558652009-04-28 19:41:44 +00001988 return Error(SubClassLoc,
1989 "value not specified for template argument #"+
David Greene56546132009-04-22 22:17:51 +00001990 utostr(i) + " (" + TArgs[i] + ") of multiclassclass '" +
1991 MC->Rec.getName() + "'");
1992 }
1993 }
1994
1995 // If the mdef is inside a 'let' expression, add to each def.
1996 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
1997 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
1998 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
1999 LetStack[i][j].Bits, LetStack[i][j].Value)) {
2000 Error(DefmPrefixLoc, "when instantiating this defm");
2001 return true;
2002 }
2003
2004 // Ensure redefinition doesn't happen.
2005 if (Records.getDef(CurRec->getName()))
Bob Wilson21870412009-11-22 04:24:42 +00002006 return Error(DefmPrefixLoc, "def '" + CurRec->getName() +
2007 "' already defined, instantiating defm with subdef '" +
David Greene56546132009-04-22 22:17:51 +00002008 DefProto->getName() + "'");
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002009
2010 // Don't create a top level definition for defm inside multiclasses,
2011 // instead, only update the prototypes and bind the template args
2012 // with the new created definition.
2013 if (CurMultiClass) {
2014 for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size();
2015 i != e; ++i) {
2016 if (CurMultiClass->DefPrototypes[i]->getName() == CurRec->getName()) {
2017 Error(DefmPrefixLoc, "defm '" + CurRec->getName() +
2018 "' already defined in this multiclass!");
2019 return 0;
2020 }
2021 }
2022 CurMultiClass->DefPrototypes.push_back(CurRec);
2023
2024 // Copy the template arguments for the multiclass into the new def.
2025 const std::vector<std::string> &TA =
2026 CurMultiClass->Rec.getTemplateArgs();
2027
2028 for (unsigned i = 0, e = TA.size(); i != e; ++i) {
2029 const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]);
2030 assert(RV && "Template arg doesn't exist?");
2031 CurRec->addValue(*RV);
2032 }
2033 } else {
2034 Records.addDef(CurRec);
Bruno Cardoso Lopes270562b2010-06-05 02:11:52 +00002035 }
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002036
2037 NewRecDefs.push_back(CurRec);
David Greene56546132009-04-22 22:17:51 +00002038 }
2039
2040 if (Lex.getCode() != tgtok::comma) break;
2041 Lex.Lex(); // eat ','.
2042
2043 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002044
2045 // A defm can inherit from regular classes (non-multiclass) as
2046 // long as they come in the end of the inheritance list.
2047 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0);
2048
2049 if (InheritFromClass)
2050 break;
2051
David Greene56546132009-04-22 22:17:51 +00002052 Ref = ParseSubClassReference(0, true);
2053 }
2054
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002055 if (InheritFromClass) {
2056 // Process all the classes to inherit as if they were part of a
2057 // regular 'def' and inherit all record values.
2058 SubClassReference SubClass = ParseSubClassReference(0, false);
2059 while (1) {
2060 // Check for error.
2061 if (SubClass.Rec == 0) return true;
2062
2063 // Get the expanded definition prototypes and teach them about
2064 // the record values the current class to inherit has
2065 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) {
2066 Record *CurRec = NewRecDefs[i];
2067
2068 // Add it.
2069 if (AddSubClass(CurRec, SubClass))
2070 return true;
2071
2072 // Process any variables on the let stack.
2073 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
2074 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
2075 if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name,
2076 LetStack[i][j].Bits, LetStack[i][j].Value))
2077 return true;
Bruno Cardoso Lopes6e0a99a2010-06-18 19:53:41 +00002078 }
2079
2080 if (Lex.getCode() != tgtok::comma) break;
2081 Lex.Lex(); // eat ','.
2082 SubClass = ParseSubClassReference(0, false);
2083 }
2084 }
2085
Bruno Cardoso Lopese5104ac2010-06-22 20:30:50 +00002086 if (!CurMultiClass)
2087 for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i)
2088 NewRecDefs[i]->resolveReferences();
2089
Chris Lattnerf4601652007-11-22 20:49:04 +00002090 if (Lex.getCode() != tgtok::semi)
2091 return TokError("expected ';' at end of defm");
2092 Lex.Lex();
Bob Wilson21870412009-11-22 04:24:42 +00002093
Chris Lattnerf4601652007-11-22 20:49:04 +00002094 return false;
2095}
2096
2097/// ParseObject
2098/// Object ::= ClassInst
2099/// Object ::= DefInst
2100/// Object ::= MultiClassInst
2101/// Object ::= DefMInst
2102/// Object ::= LETCommand '{' ObjectList '}'
2103/// Object ::= LETCommand Object
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002104bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002105 switch (Lex.getCode()) {
2106 default: assert(0 && "This is not an object");
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002107 case tgtok::Let: return ParseTopLevelLet(MC);
2108 case tgtok::Def: return ParseDef(MC);
2109 case tgtok::Defm: return ParseDefm(MC);
Chris Lattnerf4601652007-11-22 20:49:04 +00002110 case tgtok::Class: return ParseClass();
2111 case tgtok::MultiClass: return ParseMultiClass();
2112 }
2113}
2114
2115/// ParseObjectList
2116/// ObjectList :== Object*
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002117bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4601652007-11-22 20:49:04 +00002118 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +00002119 if (ParseObject(MC))
Chris Lattnerf4601652007-11-22 20:49:04 +00002120 return true;
2121 }
2122 return false;
2123}
2124
Chris Lattnerf4601652007-11-22 20:49:04 +00002125bool TGParser::ParseFile() {
2126 Lex.Lex(); // Prime the lexer.
2127 if (ParseObjectList()) return true;
Bob Wilson21870412009-11-22 04:24:42 +00002128
Chris Lattnerf4601652007-11-22 20:49:04 +00002129 // If we have unread input at the end of the file, report it.
2130 if (Lex.getCode() == tgtok::Eof)
2131 return false;
Bob Wilson21870412009-11-22 04:24:42 +00002132
Chris Lattnerf4601652007-11-22 20:49:04 +00002133 return TokError("Unexpected input at top level");
2134}
2135