File: | llvm/lib/TableGen/TGParser.cpp |
Warning: | line 1788, column 38 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- TGParser.cpp - Parser for TableGen Files ---------------------------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // Implement the Parser for TableGen. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
12 | |||||
13 | #include "TGParser.h" | ||||
14 | #include "llvm/ADT/None.h" | ||||
15 | #include "llvm/ADT/STLExtras.h" | ||||
16 | #include "llvm/ADT/SmallVector.h" | ||||
17 | #include "llvm/ADT/StringExtras.h" | ||||
18 | #include "llvm/Config/llvm-config.h" | ||||
19 | #include "llvm/Support/Casting.h" | ||||
20 | #include "llvm/Support/Compiler.h" | ||||
21 | #include "llvm/Support/ErrorHandling.h" | ||||
22 | #include "llvm/Support/raw_ostream.h" | ||||
23 | #include "llvm/TableGen/Record.h" | ||||
24 | #include <algorithm> | ||||
25 | #include <cassert> | ||||
26 | #include <cstdint> | ||||
27 | |||||
28 | using namespace llvm; | ||||
29 | |||||
30 | //===----------------------------------------------------------------------===// | ||||
31 | // Support Code for the Semantic Actions. | ||||
32 | //===----------------------------------------------------------------------===// | ||||
33 | |||||
34 | namespace llvm { | ||||
35 | |||||
36 | struct SubClassReference { | ||||
37 | SMRange RefRange; | ||||
38 | Record *Rec; | ||||
39 | SmallVector<Init*, 4> TemplateArgs; | ||||
40 | |||||
41 | SubClassReference() : Rec(nullptr) {} | ||||
42 | |||||
43 | bool isInvalid() const { return Rec == nullptr; } | ||||
44 | }; | ||||
45 | |||||
46 | struct SubMultiClassReference { | ||||
47 | SMRange RefRange; | ||||
48 | MultiClass *MC; | ||||
49 | SmallVector<Init*, 4> TemplateArgs; | ||||
50 | |||||
51 | SubMultiClassReference() : MC(nullptr) {} | ||||
52 | |||||
53 | bool isInvalid() const { return MC == nullptr; } | ||||
54 | void dump() const; | ||||
55 | }; | ||||
56 | |||||
57 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | ||||
58 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void SubMultiClassReference::dump() const { | ||||
59 | errs() << "Multiclass:\n"; | ||||
60 | |||||
61 | MC->dump(); | ||||
62 | |||||
63 | errs() << "Template args:\n"; | ||||
64 | for (Init *TA : TemplateArgs) | ||||
65 | TA->dump(); | ||||
66 | } | ||||
67 | #endif | ||||
68 | |||||
69 | } // end namespace llvm | ||||
70 | |||||
71 | static bool checkBitsConcrete(Record &R, const RecordVal &RV) { | ||||
72 | BitsInit *BV = cast<BitsInit>(RV.getValue()); | ||||
73 | for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) { | ||||
74 | Init *Bit = BV->getBit(i); | ||||
75 | bool IsReference = false; | ||||
76 | if (auto VBI = dyn_cast<VarBitInit>(Bit)) { | ||||
77 | if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) { | ||||
78 | if (R.getValue(VI->getName())) | ||||
79 | IsReference = true; | ||||
80 | } | ||||
81 | } else if (isa<VarInit>(Bit)) { | ||||
82 | IsReference = true; | ||||
83 | } | ||||
84 | if (!(IsReference || Bit->isConcrete())) | ||||
85 | return false; | ||||
86 | } | ||||
87 | return true; | ||||
88 | } | ||||
89 | |||||
90 | static void checkConcrete(Record &R) { | ||||
91 | for (const RecordVal &RV : R.getValues()) { | ||||
92 | // HACK: Disable this check for variables declared with 'field'. This is | ||||
93 | // done merely because existing targets have legitimate cases of | ||||
94 | // non-concrete variables in helper defs. Ideally, we'd introduce a | ||||
95 | // 'maybe' or 'optional' modifier instead of this. | ||||
96 | if (RV.getPrefix()) | ||||
97 | continue; | ||||
98 | |||||
99 | if (Init *V = RV.getValue()) { | ||||
100 | bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete(); | ||||
101 | if (!Ok) { | ||||
102 | PrintError(R.getLoc(), | ||||
103 | Twine("Initializer of '") + RV.getNameInitAsString() + | ||||
104 | "' in '" + R.getNameInitAsString() + | ||||
105 | "' could not be fully resolved: " + | ||||
106 | RV.getValue()->getAsString()); | ||||
107 | } | ||||
108 | } | ||||
109 | } | ||||
110 | } | ||||
111 | |||||
112 | /// Return an Init with a qualifier prefix referring | ||||
113 | /// to CurRec's name. | ||||
114 | static Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass, | ||||
115 | Init *Name, StringRef Scoper) { | ||||
116 | Init *NewName = | ||||
117 | BinOpInit::getStrConcat(CurRec.getNameInit(), StringInit::get(Scoper)); | ||||
118 | NewName = BinOpInit::getStrConcat(NewName, Name); | ||||
119 | if (CurMultiClass && Scoper != "::") { | ||||
120 | Init *Prefix = BinOpInit::getStrConcat(CurMultiClass->Rec.getNameInit(), | ||||
121 | StringInit::get("::")); | ||||
122 | NewName = BinOpInit::getStrConcat(Prefix, NewName); | ||||
123 | } | ||||
124 | |||||
125 | if (BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName)) | ||||
126 | NewName = BinOp->Fold(&CurRec); | ||||
127 | return NewName; | ||||
128 | } | ||||
129 | |||||
130 | /// Return the qualified version of the implicit 'NAME' template argument. | ||||
131 | static Init *QualifiedNameOfImplicitName(Record &Rec, | ||||
132 | MultiClass *MC = nullptr) { | ||||
133 | return QualifyName(Rec, MC, StringInit::get("NAME"), MC ? "::" : ":"); | ||||
134 | } | ||||
135 | |||||
136 | static Init *QualifiedNameOfImplicitName(MultiClass *MC) { | ||||
137 | return QualifiedNameOfImplicitName(MC->Rec, MC); | ||||
138 | } | ||||
139 | |||||
140 | bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) { | ||||
141 | if (!CurRec) | ||||
142 | CurRec = &CurMultiClass->Rec; | ||||
143 | |||||
144 | if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) { | ||||
145 | // The value already exists in the class, treat this as a set. | ||||
146 | if (ERV->setValue(RV.getValue())) | ||||
147 | return Error(Loc, "New definition of '" + RV.getName() + "' of type '" + | ||||
148 | RV.getType()->getAsString() + "' is incompatible with " + | ||||
149 | "previous definition of type '" + | ||||
150 | ERV->getType()->getAsString() + "'"); | ||||
151 | } else { | ||||
152 | CurRec->addValue(RV); | ||||
153 | } | ||||
154 | return false; | ||||
155 | } | ||||
156 | |||||
157 | /// SetValue - | ||||
158 | /// Return true on error, false on success. | ||||
159 | bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName, | ||||
160 | ArrayRef<unsigned> BitList, Init *V, | ||||
161 | bool AllowSelfAssignment) { | ||||
162 | if (!V) return false; | ||||
163 | |||||
164 | if (!CurRec) CurRec = &CurMultiClass->Rec; | ||||
165 | |||||
166 | RecordVal *RV = CurRec->getValue(ValName); | ||||
167 | if (!RV) | ||||
168 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() + | ||||
169 | "' unknown!"); | ||||
170 | |||||
171 | // Do not allow assignments like 'X = X'. This will just cause infinite loops | ||||
172 | // in the resolution machinery. | ||||
173 | if (BitList.empty()) | ||||
174 | if (VarInit *VI = dyn_cast<VarInit>(V)) | ||||
175 | if (VI->getNameInit() == ValName && !AllowSelfAssignment) | ||||
176 | return Error(Loc, "Recursion / self-assignment forbidden"); | ||||
177 | |||||
178 | // If we are assigning to a subset of the bits in the value... then we must be | ||||
179 | // assigning to a field of BitsRecTy, which must have a BitsInit | ||||
180 | // initializer. | ||||
181 | // | ||||
182 | if (!BitList.empty()) { | ||||
183 | BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue()); | ||||
184 | if (!CurVal) | ||||
185 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() + | ||||
186 | "' is not a bits type"); | ||||
187 | |||||
188 | // Convert the incoming value to a bits type of the appropriate size... | ||||
189 | Init *BI = V->getCastTo(BitsRecTy::get(BitList.size())); | ||||
190 | if (!BI) | ||||
191 | return Error(Loc, "Initializer is not compatible with bit range"); | ||||
192 | |||||
193 | SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); | ||||
194 | |||||
195 | // Loop over bits, assigning values as appropriate. | ||||
196 | for (unsigned i = 0, e = BitList.size(); i != e; ++i) { | ||||
197 | unsigned Bit = BitList[i]; | ||||
198 | if (NewBits[Bit]) | ||||
199 | return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" + | ||||
200 | ValName->getAsUnquotedString() + "' more than once"); | ||||
201 | NewBits[Bit] = BI->getBit(i); | ||||
202 | } | ||||
203 | |||||
204 | for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) | ||||
205 | if (!NewBits[i]) | ||||
206 | NewBits[i] = CurVal->getBit(i); | ||||
207 | |||||
208 | V = BitsInit::get(NewBits); | ||||
209 | } | ||||
210 | |||||
211 | if (RV->setValue(V)) { | ||||
212 | std::string InitType; | ||||
213 | if (BitsInit *BI = dyn_cast<BitsInit>(V)) | ||||
214 | InitType = (Twine("' of type bit initializer with length ") + | ||||
215 | Twine(BI->getNumBits())).str(); | ||||
216 | else if (TypedInit *TI = dyn_cast<TypedInit>(V)) | ||||
217 | InitType = (Twine("' of type '") + TI->getType()->getAsString()).str(); | ||||
218 | return Error(Loc, "Value '" + ValName->getAsUnquotedString() + | ||||
219 | "' of type '" + RV->getType()->getAsString() + | ||||
220 | "' is incompatible with initializer '" + | ||||
221 | V->getAsString() + InitType + "'"); | ||||
222 | } | ||||
223 | return false; | ||||
224 | } | ||||
225 | |||||
226 | /// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template | ||||
227 | /// args as SubClass's template arguments. | ||||
228 | bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { | ||||
229 | Record *SC = SubClass.Rec; | ||||
230 | // Add all of the values in the subclass into the current class. | ||||
231 | for (const RecordVal &Val : SC->getValues()) | ||||
232 | if (AddValue(CurRec, SubClass.RefRange.Start, Val)) | ||||
233 | return true; | ||||
234 | |||||
235 | ArrayRef<Init *> TArgs = SC->getTemplateArgs(); | ||||
236 | |||||
237 | // Ensure that an appropriate number of template arguments are specified. | ||||
238 | if (TArgs.size() < SubClass.TemplateArgs.size()) | ||||
239 | return Error(SubClass.RefRange.Start, | ||||
240 | "More template args specified than expected"); | ||||
241 | |||||
242 | // Loop over all of the template arguments, setting them to the specified | ||||
243 | // value or leaving them as the default if necessary. | ||||
244 | MapResolver R(CurRec); | ||||
245 | |||||
246 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { | ||||
247 | if (i < SubClass.TemplateArgs.size()) { | ||||
248 | // If a value is specified for this template arg, set it now. | ||||
249 | if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i], | ||||
250 | None, SubClass.TemplateArgs[i])) | ||||
251 | return true; | ||||
252 | } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { | ||||
253 | return Error(SubClass.RefRange.Start, | ||||
254 | "Value not specified for template argument #" + | ||||
255 | Twine(i) + " (" + TArgs[i]->getAsUnquotedString() + | ||||
256 | ") of subclass '" + SC->getNameInitAsString() + "'!"); | ||||
257 | } | ||||
258 | |||||
259 | R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue()); | ||||
260 | |||||
261 | CurRec->removeValue(TArgs[i]); | ||||
262 | } | ||||
263 | |||||
264 | Init *Name; | ||||
265 | if (CurRec->isClass()) | ||||
266 | Name = | ||||
267 | VarInit::get(QualifiedNameOfImplicitName(*CurRec), StringRecTy::get()); | ||||
268 | else | ||||
269 | Name = CurRec->getNameInit(); | ||||
270 | R.set(QualifiedNameOfImplicitName(*SC), Name); | ||||
271 | |||||
272 | CurRec->resolveReferences(R); | ||||
273 | |||||
274 | // Since everything went well, we can now set the "superclass" list for the | ||||
275 | // current record. | ||||
276 | ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses(); | ||||
277 | for (const auto &SCPair : SCs) { | ||||
278 | if (CurRec->isSubClassOf(SCPair.first)) | ||||
279 | return Error(SubClass.RefRange.Start, | ||||
280 | "Already subclass of '" + SCPair.first->getName() + "'!\n"); | ||||
281 | CurRec->addSuperClass(SCPair.first, SCPair.second); | ||||
282 | } | ||||
283 | |||||
284 | if (CurRec->isSubClassOf(SC)) | ||||
285 | return Error(SubClass.RefRange.Start, | ||||
286 | "Already subclass of '" + SC->getName() + "'!\n"); | ||||
287 | CurRec->addSuperClass(SC, SubClass.RefRange); | ||||
288 | return false; | ||||
289 | } | ||||
290 | |||||
291 | bool TGParser::AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass) { | ||||
292 | if (Entry.Rec) | ||||
293 | return AddSubClass(Entry.Rec.get(), SubClass); | ||||
294 | |||||
295 | for (auto &E : Entry.Loop->Entries) { | ||||
296 | if (AddSubClass(E, SubClass)) | ||||
297 | return true; | ||||
298 | } | ||||
299 | |||||
300 | return false; | ||||
301 | } | ||||
302 | |||||
303 | /// AddSubMultiClass - Add SubMultiClass as a subclass to | ||||
304 | /// CurMC, resolving its template args as SubMultiClass's | ||||
305 | /// template arguments. | ||||
306 | bool TGParser::AddSubMultiClass(MultiClass *CurMC, | ||||
307 | SubMultiClassReference &SubMultiClass) { | ||||
308 | MultiClass *SMC = SubMultiClass.MC; | ||||
309 | |||||
310 | ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs(); | ||||
311 | if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) | ||||
312 | return Error(SubMultiClass.RefRange.Start, | ||||
313 | "More template args specified than expected"); | ||||
314 | |||||
315 | // Prepare the mapping of template argument name to value, filling in default | ||||
316 | // values if necessary. | ||||
317 | SubstStack TemplateArgs; | ||||
318 | for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { | ||||
319 | if (i < SubMultiClass.TemplateArgs.size()) { | ||||
320 | TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.TemplateArgs[i]); | ||||
321 | } else { | ||||
322 | Init *Default = SMC->Rec.getValue(SMCTArgs[i])->getValue(); | ||||
323 | if (!Default->isComplete()) { | ||||
324 | return Error(SubMultiClass.RefRange.Start, | ||||
325 | "value not specified for template argument #" + Twine(i) + | ||||
326 | " (" + SMCTArgs[i]->getAsUnquotedString() + | ||||
327 | ") of multiclass '" + SMC->Rec.getNameInitAsString() + | ||||
328 | "'"); | ||||
329 | } | ||||
330 | TemplateArgs.emplace_back(SMCTArgs[i], Default); | ||||
331 | } | ||||
332 | } | ||||
333 | |||||
334 | TemplateArgs.emplace_back( | ||||
335 | QualifiedNameOfImplicitName(SMC), | ||||
336 | VarInit::get(QualifiedNameOfImplicitName(CurMC), StringRecTy::get())); | ||||
337 | |||||
338 | // Add all of the defs in the subclass into the current multiclass. | ||||
339 | return resolve(SMC->Entries, TemplateArgs, false, &CurMC->Entries); | ||||
340 | } | ||||
341 | |||||
342 | /// Add a record or foreach loop to the current context (global record keeper, | ||||
343 | /// current inner-most foreach loop, or multiclass). | ||||
344 | bool TGParser::addEntry(RecordsEntry E) { | ||||
345 | assert(!E.Rec || !E.Loop)((!E.Rec || !E.Loop) ? static_cast<void> (0) : __assert_fail ("!E.Rec || !E.Loop", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 345, __PRETTY_FUNCTION__)); | ||||
346 | |||||
347 | if (!Loops.empty()) { | ||||
348 | Loops.back()->Entries.push_back(std::move(E)); | ||||
349 | return false; | ||||
350 | } | ||||
351 | |||||
352 | if (E.Loop) { | ||||
353 | SubstStack Stack; | ||||
354 | return resolve(*E.Loop, Stack, CurMultiClass == nullptr, | ||||
355 | CurMultiClass ? &CurMultiClass->Entries : nullptr); | ||||
356 | } | ||||
357 | |||||
358 | if (CurMultiClass) { | ||||
359 | CurMultiClass->Entries.push_back(std::move(E)); | ||||
360 | return false; | ||||
361 | } | ||||
362 | |||||
363 | return addDefOne(std::move(E.Rec)); | ||||
364 | } | ||||
365 | |||||
366 | /// Resolve the entries in \p Loop, going over inner loops recursively | ||||
367 | /// and making the given subsitutions of (name, value) pairs. | ||||
368 | /// | ||||
369 | /// The resulting records are stored in \p Dest if non-null. Otherwise, they | ||||
370 | /// are added to the global record keeper. | ||||
371 | bool TGParser::resolve(const ForeachLoop &Loop, SubstStack &Substs, | ||||
372 | bool Final, std::vector<RecordsEntry> *Dest, | ||||
373 | SMLoc *Loc) { | ||||
374 | MapResolver R; | ||||
375 | for (const auto &S : Substs) | ||||
376 | R.set(S.first, S.second); | ||||
377 | Init *List = Loop.ListValue->resolveReferences(R); | ||||
378 | auto LI = dyn_cast<ListInit>(List); | ||||
379 | if (!LI) { | ||||
380 | if (!Final) { | ||||
381 | Dest->emplace_back(std::make_unique<ForeachLoop>(Loop.Loc, Loop.IterVar, | ||||
382 | List)); | ||||
383 | return resolve(Loop.Entries, Substs, Final, &Dest->back().Loop->Entries, | ||||
384 | Loc); | ||||
385 | } | ||||
386 | |||||
387 | PrintError(Loop.Loc, Twine("attempting to loop over '") + | ||||
388 | List->getAsString() + "', expected a list"); | ||||
389 | return true; | ||||
390 | } | ||||
391 | |||||
392 | bool Error = false; | ||||
393 | for (auto Elt : *LI) { | ||||
394 | if (Loop.IterVar) | ||||
395 | Substs.emplace_back(Loop.IterVar->getNameInit(), Elt); | ||||
396 | Error = resolve(Loop.Entries, Substs, Final, Dest); | ||||
397 | if (Loop.IterVar) | ||||
398 | Substs.pop_back(); | ||||
399 | if (Error) | ||||
400 | break; | ||||
401 | } | ||||
402 | return Error; | ||||
403 | } | ||||
404 | |||||
405 | /// Resolve the entries in \p Source, going over loops recursively and | ||||
406 | /// making the given substitutions of (name, value) pairs. | ||||
407 | /// | ||||
408 | /// The resulting records are stored in \p Dest if non-null. Otherwise, they | ||||
409 | /// are added to the global record keeper. | ||||
410 | bool TGParser::resolve(const std::vector<RecordsEntry> &Source, | ||||
411 | SubstStack &Substs, bool Final, | ||||
412 | std::vector<RecordsEntry> *Dest, SMLoc *Loc) { | ||||
413 | bool Error = false; | ||||
414 | for (auto &E : Source) { | ||||
415 | if (E.Loop) { | ||||
416 | Error = resolve(*E.Loop, Substs, Final, Dest); | ||||
417 | } else { | ||||
418 | auto Rec = std::make_unique<Record>(*E.Rec); | ||||
419 | if (Loc) | ||||
420 | Rec->appendLoc(*Loc); | ||||
421 | |||||
422 | MapResolver R(Rec.get()); | ||||
423 | for (const auto &S : Substs) | ||||
424 | R.set(S.first, S.second); | ||||
425 | Rec->resolveReferences(R); | ||||
426 | |||||
427 | if (Dest) | ||||
428 | Dest->push_back(std::move(Rec)); | ||||
429 | else | ||||
430 | Error = addDefOne(std::move(Rec)); | ||||
431 | } | ||||
432 | if (Error) | ||||
433 | break; | ||||
434 | } | ||||
435 | return Error; | ||||
436 | } | ||||
437 | |||||
438 | /// Resolve the record fully and add it to the record keeper. | ||||
439 | bool TGParser::addDefOne(std::unique_ptr<Record> Rec) { | ||||
440 | if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) { | ||||
441 | if (!Rec->isAnonymous()) { | ||||
442 | PrintError(Rec->getLoc(), | ||||
443 | "def already exists: " + Rec->getNameInitAsString()); | ||||
444 | PrintNote(Prev->getLoc(), "location of previous definition"); | ||||
445 | return true; | ||||
446 | } | ||||
447 | Rec->setName(Records.getNewAnonymousName()); | ||||
448 | } | ||||
449 | |||||
450 | Rec->resolveReferences(); | ||||
451 | checkConcrete(*Rec); | ||||
452 | |||||
453 | if (!isa<StringInit>(Rec->getNameInit())) { | ||||
454 | PrintError(Rec->getLoc(), Twine("record name '") + | ||||
455 | Rec->getNameInit()->getAsString() + | ||||
456 | "' could not be fully resolved"); | ||||
457 | return true; | ||||
458 | } | ||||
459 | |||||
460 | // If ObjectBody has template arguments, it's an error. | ||||
461 | assert(Rec->getTemplateArgs().empty() && "How'd this get template args?")((Rec->getTemplateArgs().empty() && "How'd this get template args?" ) ? static_cast<void> (0) : __assert_fail ("Rec->getTemplateArgs().empty() && \"How'd this get template args?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 461, __PRETTY_FUNCTION__)); | ||||
462 | |||||
463 | for (DefsetRecord *Defset : Defsets) { | ||||
464 | DefInit *I = Rec->getDefInit(); | ||||
465 | if (!I->getType()->typeIsA(Defset->EltTy)) { | ||||
466 | PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") + | ||||
467 | I->getType()->getAsString() + | ||||
468 | "' to defset"); | ||||
469 | PrintNote(Defset->Loc, "location of defset declaration"); | ||||
470 | return true; | ||||
471 | } | ||||
472 | Defset->Elements.push_back(I); | ||||
473 | } | ||||
474 | |||||
475 | Records.addDef(std::move(Rec)); | ||||
476 | return false; | ||||
477 | } | ||||
478 | |||||
479 | //===----------------------------------------------------------------------===// | ||||
480 | // Parser Code | ||||
481 | //===----------------------------------------------------------------------===// | ||||
482 | |||||
483 | /// isObjectStart - Return true if this is a valid first token for an Object. | ||||
484 | static bool isObjectStart(tgtok::TokKind K) { | ||||
485 | return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm || | ||||
486 | K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach || | ||||
487 | K == tgtok::Defset || K == tgtok::Defvar || K == tgtok::If; | ||||
488 | } | ||||
489 | |||||
490 | /// ParseObjectName - If a valid object name is specified, return it. If no | ||||
491 | /// name is specified, return the unset initializer. Return nullptr on parse | ||||
492 | /// error. | ||||
493 | /// ObjectName ::= Value [ '#' Value ]* | ||||
494 | /// ObjectName ::= /*empty*/ | ||||
495 | /// | ||||
496 | Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) { | ||||
497 | switch (Lex.getCode()) { | ||||
498 | case tgtok::colon: | ||||
499 | case tgtok::semi: | ||||
500 | case tgtok::l_brace: | ||||
501 | // These are all of the tokens that can begin an object body. | ||||
502 | // Some of these can also begin values but we disallow those cases | ||||
503 | // because they are unlikely to be useful. | ||||
504 | return UnsetInit::get(); | ||||
505 | default: | ||||
506 | break; | ||||
507 | } | ||||
508 | |||||
509 | Record *CurRec = nullptr; | ||||
510 | if (CurMultiClass) | ||||
511 | CurRec = &CurMultiClass->Rec; | ||||
512 | |||||
513 | Init *Name = ParseValue(CurRec, StringRecTy::get(), ParseNameMode); | ||||
514 | if (!Name) | ||||
515 | return nullptr; | ||||
516 | |||||
517 | if (CurMultiClass) { | ||||
518 | Init *NameStr = QualifiedNameOfImplicitName(CurMultiClass); | ||||
519 | HasReferenceResolver R(NameStr); | ||||
520 | Name->resolveReferences(R); | ||||
521 | if (!R.found()) | ||||
522 | Name = BinOpInit::getStrConcat(VarInit::get(NameStr, StringRecTy::get()), | ||||
523 | Name); | ||||
524 | } | ||||
525 | |||||
526 | return Name; | ||||
527 | } | ||||
528 | |||||
529 | /// ParseClassID - Parse and resolve a reference to a class name. This returns | ||||
530 | /// null on error. | ||||
531 | /// | ||||
532 | /// ClassID ::= ID | ||||
533 | /// | ||||
534 | Record *TGParser::ParseClassID() { | ||||
535 | if (Lex.getCode() != tgtok::Id) { | ||||
536 | TokError("expected name for ClassID"); | ||||
537 | return nullptr; | ||||
538 | } | ||||
539 | |||||
540 | Record *Result = Records.getClass(Lex.getCurStrVal()); | ||||
541 | if (!Result) { | ||||
542 | std::string Msg("Couldn't find class '" + Lex.getCurStrVal() + "'"); | ||||
543 | if (MultiClasses[Lex.getCurStrVal()].get()) | ||||
544 | TokError(Msg + ". Use 'defm' if you meant to use multiclass '" + | ||||
545 | Lex.getCurStrVal() + "'"); | ||||
546 | else | ||||
547 | TokError(Msg); | ||||
548 | } | ||||
549 | |||||
550 | Lex.Lex(); | ||||
551 | return Result; | ||||
552 | } | ||||
553 | |||||
554 | /// ParseMultiClassID - Parse and resolve a reference to a multiclass name. | ||||
555 | /// This returns null on error. | ||||
556 | /// | ||||
557 | /// MultiClassID ::= ID | ||||
558 | /// | ||||
559 | MultiClass *TGParser::ParseMultiClassID() { | ||||
560 | if (Lex.getCode() != tgtok::Id) { | ||||
561 | TokError("expected name for MultiClassID"); | ||||
562 | return nullptr; | ||||
563 | } | ||||
564 | |||||
565 | MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get(); | ||||
566 | if (!Result) | ||||
567 | TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); | ||||
568 | |||||
569 | Lex.Lex(); | ||||
570 | return Result; | ||||
571 | } | ||||
572 | |||||
573 | /// ParseSubClassReference - Parse a reference to a subclass or to a templated | ||||
574 | /// subclass. This returns a SubClassRefTy with a null Record* on error. | ||||
575 | /// | ||||
576 | /// SubClassRef ::= ClassID | ||||
577 | /// SubClassRef ::= ClassID '<' ValueList '>' | ||||
578 | /// | ||||
579 | SubClassReference TGParser:: | ||||
580 | ParseSubClassReference(Record *CurRec, bool isDefm) { | ||||
581 | SubClassReference Result; | ||||
582 | Result.RefRange.Start = Lex.getLoc(); | ||||
583 | |||||
584 | if (isDefm) { | ||||
585 | if (MultiClass *MC = ParseMultiClassID()) | ||||
586 | Result.Rec = &MC->Rec; | ||||
587 | } else { | ||||
588 | Result.Rec = ParseClassID(); | ||||
589 | } | ||||
590 | if (!Result.Rec) return Result; | ||||
591 | |||||
592 | // If there is no template arg list, we're done. | ||||
593 | if (Lex.getCode() != tgtok::less) { | ||||
594 | Result.RefRange.End = Lex.getLoc(); | ||||
595 | return Result; | ||||
596 | } | ||||
597 | Lex.Lex(); // Eat the '<' | ||||
598 | |||||
599 | if (Lex.getCode() == tgtok::greater) { | ||||
600 | TokError("subclass reference requires a non-empty list of template values"); | ||||
601 | Result.Rec = nullptr; | ||||
602 | return Result; | ||||
603 | } | ||||
604 | |||||
605 | ParseValueList(Result.TemplateArgs, CurRec, Result.Rec); | ||||
606 | if (Result.TemplateArgs.empty()) { | ||||
607 | Result.Rec = nullptr; // Error parsing value list. | ||||
608 | return Result; | ||||
609 | } | ||||
610 | |||||
611 | if (Lex.getCode() != tgtok::greater) { | ||||
612 | TokError("expected '>' in template value list"); | ||||
613 | Result.Rec = nullptr; | ||||
614 | return Result; | ||||
615 | } | ||||
616 | Lex.Lex(); | ||||
617 | Result.RefRange.End = Lex.getLoc(); | ||||
618 | |||||
619 | return Result; | ||||
620 | } | ||||
621 | |||||
622 | /// ParseSubMultiClassReference - Parse a reference to a subclass or to a | ||||
623 | /// templated submulticlass. This returns a SubMultiClassRefTy with a null | ||||
624 | /// Record* on error. | ||||
625 | /// | ||||
626 | /// SubMultiClassRef ::= MultiClassID | ||||
627 | /// SubMultiClassRef ::= MultiClassID '<' ValueList '>' | ||||
628 | /// | ||||
629 | SubMultiClassReference TGParser:: | ||||
630 | ParseSubMultiClassReference(MultiClass *CurMC) { | ||||
631 | SubMultiClassReference Result; | ||||
632 | Result.RefRange.Start = Lex.getLoc(); | ||||
633 | |||||
634 | Result.MC = ParseMultiClassID(); | ||||
635 | if (!Result.MC) return Result; | ||||
636 | |||||
637 | // If there is no template arg list, we're done. | ||||
638 | if (Lex.getCode() != tgtok::less) { | ||||
639 | Result.RefRange.End = Lex.getLoc(); | ||||
640 | return Result; | ||||
641 | } | ||||
642 | Lex.Lex(); // Eat the '<' | ||||
643 | |||||
644 | if (Lex.getCode() == tgtok::greater) { | ||||
645 | TokError("subclass reference requires a non-empty list of template values"); | ||||
646 | Result.MC = nullptr; | ||||
647 | return Result; | ||||
648 | } | ||||
649 | |||||
650 | ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec); | ||||
651 | if (Result.TemplateArgs.empty()) { | ||||
652 | Result.MC = nullptr; // Error parsing value list. | ||||
653 | return Result; | ||||
654 | } | ||||
655 | |||||
656 | if (Lex.getCode() != tgtok::greater) { | ||||
657 | TokError("expected '>' in template value list"); | ||||
658 | Result.MC = nullptr; | ||||
659 | return Result; | ||||
660 | } | ||||
661 | Lex.Lex(); | ||||
662 | Result.RefRange.End = Lex.getLoc(); | ||||
663 | |||||
664 | return Result; | ||||
665 | } | ||||
666 | |||||
667 | /// ParseRangePiece - Parse a bit/value range. | ||||
668 | /// RangePiece ::= INTVAL | ||||
669 | /// RangePiece ::= INTVAL '-' INTVAL | ||||
670 | /// RangePiece ::= INTVAL INTVAL | ||||
671 | bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges, | ||||
672 | TypedInit *FirstItem) { | ||||
673 | Init *CurVal = FirstItem; | ||||
674 | if (!CurVal) | ||||
675 | CurVal = ParseValue(nullptr); | ||||
676 | |||||
677 | IntInit *II = dyn_cast_or_null<IntInit>(CurVal); | ||||
678 | if (!II) | ||||
679 | return TokError("expected integer or bitrange"); | ||||
680 | |||||
681 | int64_t Start = II->getValue(); | ||||
682 | int64_t End; | ||||
683 | |||||
684 | if (Start < 0) | ||||
685 | return TokError("invalid range, cannot be negative"); | ||||
686 | |||||
687 | switch (Lex.getCode()) { | ||||
688 | default: | ||||
689 | Ranges.push_back(Start); | ||||
690 | return false; | ||||
691 | case tgtok::minus: { | ||||
692 | Lex.Lex(); // eat | ||||
693 | |||||
694 | Init *I_End = ParseValue(nullptr); | ||||
695 | IntInit *II_End = dyn_cast_or_null<IntInit>(I_End); | ||||
696 | if (!II_End) { | ||||
697 | TokError("expected integer value as end of range"); | ||||
698 | return true; | ||||
699 | } | ||||
700 | |||||
701 | End = II_End->getValue(); | ||||
702 | break; | ||||
703 | } | ||||
704 | case tgtok::IntVal: { | ||||
705 | End = -Lex.getCurIntVal(); | ||||
706 | Lex.Lex(); | ||||
707 | break; | ||||
708 | } | ||||
709 | } | ||||
710 | if (End < 0) | ||||
711 | return TokError("invalid range, cannot be negative"); | ||||
712 | |||||
713 | // Add to the range. | ||||
714 | if (Start < End) | ||||
715 | for (; Start <= End; ++Start) | ||||
716 | Ranges.push_back(Start); | ||||
717 | else | ||||
718 | for (; Start >= End; --Start) | ||||
719 | Ranges.push_back(Start); | ||||
720 | return false; | ||||
721 | } | ||||
722 | |||||
723 | /// ParseRangeList - Parse a list of scalars and ranges into scalar values. | ||||
724 | /// | ||||
725 | /// RangeList ::= RangePiece (',' RangePiece)* | ||||
726 | /// | ||||
727 | void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) { | ||||
728 | // Parse the first piece. | ||||
729 | if (ParseRangePiece(Result)) { | ||||
730 | Result.clear(); | ||||
731 | return; | ||||
732 | } | ||||
733 | while (Lex.getCode() == tgtok::comma) { | ||||
734 | Lex.Lex(); // Eat the comma. | ||||
735 | |||||
736 | // Parse the next range piece. | ||||
737 | if (ParseRangePiece(Result)) { | ||||
738 | Result.clear(); | ||||
739 | return; | ||||
740 | } | ||||
741 | } | ||||
742 | } | ||||
743 | |||||
744 | /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. | ||||
745 | /// OptionalRangeList ::= '<' RangeList '>' | ||||
746 | /// OptionalRangeList ::= /*empty*/ | ||||
747 | bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) { | ||||
748 | if (Lex.getCode() != tgtok::less) | ||||
749 | return false; | ||||
750 | |||||
751 | SMLoc StartLoc = Lex.getLoc(); | ||||
752 | Lex.Lex(); // eat the '<' | ||||
753 | |||||
754 | // Parse the range list. | ||||
755 | ParseRangeList(Ranges); | ||||
756 | if (Ranges.empty()) return true; | ||||
757 | |||||
758 | if (Lex.getCode() != tgtok::greater) { | ||||
759 | TokError("expected '>' at end of range list"); | ||||
760 | return Error(StartLoc, "to match this '<'"); | ||||
761 | } | ||||
762 | Lex.Lex(); // eat the '>'. | ||||
763 | return false; | ||||
764 | } | ||||
765 | |||||
766 | /// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. | ||||
767 | /// OptionalBitList ::= '{' RangeList '}' | ||||
768 | /// OptionalBitList ::= /*empty*/ | ||||
769 | bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) { | ||||
770 | if (Lex.getCode() != tgtok::l_brace) | ||||
771 | return false; | ||||
772 | |||||
773 | SMLoc StartLoc = Lex.getLoc(); | ||||
774 | Lex.Lex(); // eat the '{' | ||||
775 | |||||
776 | // Parse the range list. | ||||
777 | ParseRangeList(Ranges); | ||||
778 | if (Ranges.empty()) return true; | ||||
779 | |||||
780 | if (Lex.getCode() != tgtok::r_brace) { | ||||
781 | TokError("expected '}' at end of bit list"); | ||||
782 | return Error(StartLoc, "to match this '{'"); | ||||
783 | } | ||||
784 | Lex.Lex(); // eat the '}'. | ||||
785 | return false; | ||||
786 | } | ||||
787 | |||||
788 | /// ParseType - Parse and return a tblgen type. This returns null on error. | ||||
789 | /// | ||||
790 | /// Type ::= STRING // string type | ||||
791 | /// Type ::= CODE // code type | ||||
792 | /// Type ::= BIT // bit type | ||||
793 | /// Type ::= BITS '<' INTVAL '>' // bits<x> type | ||||
794 | /// Type ::= INT // int type | ||||
795 | /// Type ::= LIST '<' Type '>' // list<x> type | ||||
796 | /// Type ::= DAG // dag type | ||||
797 | /// Type ::= ClassID // Record Type | ||||
798 | /// | ||||
799 | RecTy *TGParser::ParseType() { | ||||
800 | switch (Lex.getCode()) { | ||||
801 | default: TokError("Unknown token when expecting a type"); return nullptr; | ||||
802 | case tgtok::String: Lex.Lex(); return StringRecTy::get(); | ||||
803 | case tgtok::Code: Lex.Lex(); return CodeRecTy::get(); | ||||
804 | case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); | ||||
805 | case tgtok::Int: Lex.Lex(); return IntRecTy::get(); | ||||
806 | case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); | ||||
807 | case tgtok::Id: | ||||
808 | if (Record *R = ParseClassID()) return RecordRecTy::get(R); | ||||
809 | TokError("unknown class name"); | ||||
810 | return nullptr; | ||||
811 | case tgtok::Bits: { | ||||
812 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' | ||||
813 | TokError("expected '<' after bits type"); | ||||
814 | return nullptr; | ||||
815 | } | ||||
816 | if (Lex.Lex() != tgtok::IntVal) { // Eat '<' | ||||
817 | TokError("expected integer in bits<n> type"); | ||||
818 | return nullptr; | ||||
819 | } | ||||
820 | uint64_t Val = Lex.getCurIntVal(); | ||||
821 | if (Lex.Lex() != tgtok::greater) { // Eat count. | ||||
822 | TokError("expected '>' at end of bits<n> type"); | ||||
823 | return nullptr; | ||||
824 | } | ||||
825 | Lex.Lex(); // Eat '>' | ||||
826 | return BitsRecTy::get(Val); | ||||
827 | } | ||||
828 | case tgtok::List: { | ||||
829 | if (Lex.Lex() != tgtok::less) { // Eat 'bits' | ||||
830 | TokError("expected '<' after list type"); | ||||
831 | return nullptr; | ||||
832 | } | ||||
833 | Lex.Lex(); // Eat '<' | ||||
834 | RecTy *SubType = ParseType(); | ||||
835 | if (!SubType) return nullptr; | ||||
836 | |||||
837 | if (Lex.getCode() != tgtok::greater) { | ||||
838 | TokError("expected '>' at end of list<ty> type"); | ||||
839 | return nullptr; | ||||
840 | } | ||||
841 | Lex.Lex(); // Eat '>' | ||||
842 | return ListRecTy::get(SubType); | ||||
843 | } | ||||
844 | } | ||||
845 | } | ||||
846 | |||||
847 | /// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID | ||||
848 | /// has already been read. | ||||
849 | Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc, | ||||
850 | IDParseMode Mode) { | ||||
851 | if (CurRec) { | ||||
852 | if (const RecordVal *RV = CurRec->getValue(Name)) | ||||
853 | return VarInit::get(Name, RV->getType()); | ||||
854 | } | ||||
855 | |||||
856 | if ((CurRec && CurRec->isClass()) || CurMultiClass) { | ||||
857 | Init *TemplateArgName; | ||||
858 | if (CurMultiClass) { | ||||
859 | TemplateArgName = | ||||
860 | QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::"); | ||||
861 | } else | ||||
862 | TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":"); | ||||
863 | |||||
864 | Record *TemplateRec = CurMultiClass ? &CurMultiClass->Rec : CurRec; | ||||
865 | if (TemplateRec->isTemplateArg(TemplateArgName)) { | ||||
866 | const RecordVal *RV = TemplateRec->getValue(TemplateArgName); | ||||
867 | assert(RV && "Template arg doesn't exist??")((RV && "Template arg doesn't exist??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template arg doesn't exist??\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 867, __PRETTY_FUNCTION__)); | ||||
868 | return VarInit::get(TemplateArgName, RV->getType()); | ||||
869 | } else if (Name->getValue() == "NAME") { | ||||
870 | return VarInit::get(TemplateArgName, StringRecTy::get()); | ||||
871 | } | ||||
872 | } | ||||
873 | |||||
874 | if (CurLocalScope) | ||||
875 | if (Init *I = CurLocalScope->getVar(Name->getValue())) | ||||
876 | return I; | ||||
877 | |||||
878 | // If this is in a foreach loop, make sure it's not a loop iterator | ||||
879 | for (const auto &L : Loops) { | ||||
880 | if (L->IterVar) { | ||||
881 | VarInit *IterVar = dyn_cast<VarInit>(L->IterVar); | ||||
882 | if (IterVar && IterVar->getNameInit() == Name) | ||||
883 | return IterVar; | ||||
884 | } | ||||
885 | } | ||||
886 | |||||
887 | if (Mode == ParseNameMode) | ||||
888 | return Name; | ||||
889 | |||||
890 | if (Init *I = Records.getGlobal(Name->getValue())) | ||||
891 | return I; | ||||
892 | |||||
893 | // Allow self-references of concrete defs, but delay the lookup so that we | ||||
894 | // get the correct type. | ||||
895 | if (CurRec && !CurRec->isClass() && !CurMultiClass && | ||||
896 | CurRec->getNameInit() == Name) | ||||
897 | return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType()); | ||||
898 | |||||
899 | Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'"); | ||||
900 | return nullptr; | ||||
901 | } | ||||
902 | |||||
903 | /// ParseOperation - Parse an operator. This returns null on error. | ||||
904 | /// | ||||
905 | /// Operation ::= XOperator ['<' Type '>'] '(' Args ')' | ||||
906 | /// | ||||
907 | Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { | ||||
908 | switch (Lex.getCode()) { | ||||
| |||||
909 | default: | ||||
910 | TokError("unknown operation"); | ||||
911 | return nullptr; | ||||
912 | case tgtok::XHead: | ||||
913 | case tgtok::XTail: | ||||
914 | case tgtok::XSize: | ||||
915 | case tgtok::XEmpty: | ||||
916 | case tgtok::XCast: | ||||
917 | case tgtok::XGetOp: { // Value ::= !unop '(' Value ')' | ||||
918 | UnOpInit::UnaryOp Code; | ||||
919 | RecTy *Type = nullptr; | ||||
920 | |||||
921 | switch (Lex.getCode()) { | ||||
922 | default: llvm_unreachable("Unhandled code!")::llvm::llvm_unreachable_internal("Unhandled code!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 922); | ||||
923 | case tgtok::XCast: | ||||
924 | Lex.Lex(); // eat the operation | ||||
925 | Code = UnOpInit::CAST; | ||||
926 | |||||
927 | Type = ParseOperatorType(); | ||||
928 | |||||
929 | if (!Type) { | ||||
930 | TokError("did not get type for unary operator"); | ||||
931 | return nullptr; | ||||
932 | } | ||||
933 | |||||
934 | break; | ||||
935 | case tgtok::XHead: | ||||
936 | Lex.Lex(); // eat the operation | ||||
937 | Code = UnOpInit::HEAD; | ||||
938 | break; | ||||
939 | case tgtok::XTail: | ||||
940 | Lex.Lex(); // eat the operation | ||||
941 | Code = UnOpInit::TAIL; | ||||
942 | break; | ||||
943 | case tgtok::XSize: | ||||
944 | Lex.Lex(); | ||||
945 | Code = UnOpInit::SIZE; | ||||
946 | Type = IntRecTy::get(); | ||||
947 | break; | ||||
948 | case tgtok::XEmpty: | ||||
949 | Lex.Lex(); // eat the operation | ||||
950 | Code = UnOpInit::EMPTY; | ||||
951 | Type = IntRecTy::get(); | ||||
952 | break; | ||||
953 | case tgtok::XGetOp: | ||||
954 | Lex.Lex(); // eat the operation | ||||
955 | if (Lex.getCode() == tgtok::less) { | ||||
956 | // Parse an optional type suffix, so that you can say | ||||
957 | // !getop<BaseClass>(someDag) as a shorthand for | ||||
958 | // !cast<BaseClass>(!getop(someDag)). | ||||
959 | Type = ParseOperatorType(); | ||||
960 | |||||
961 | if (!Type) { | ||||
962 | TokError("did not get type for unary operator"); | ||||
963 | return nullptr; | ||||
964 | } | ||||
965 | |||||
966 | if (!isa<RecordRecTy>(Type)) { | ||||
967 | TokError("type for !getop must be a record type"); | ||||
968 | // but keep parsing, to consume the operand | ||||
969 | } | ||||
970 | } else { | ||||
971 | Type = RecordRecTy::get({}); | ||||
972 | } | ||||
973 | Code = UnOpInit::GETOP; | ||||
974 | break; | ||||
975 | } | ||||
976 | if (Lex.getCode() != tgtok::l_paren) { | ||||
977 | TokError("expected '(' after unary operator"); | ||||
978 | return nullptr; | ||||
979 | } | ||||
980 | Lex.Lex(); // eat the '(' | ||||
981 | |||||
982 | Init *LHS = ParseValue(CurRec); | ||||
983 | if (!LHS) return nullptr; | ||||
984 | |||||
985 | if (Code == UnOpInit::HEAD || | ||||
986 | Code == UnOpInit::TAIL || | ||||
987 | Code == UnOpInit::EMPTY) { | ||||
988 | ListInit *LHSl = dyn_cast<ListInit>(LHS); | ||||
989 | StringInit *LHSs = dyn_cast<StringInit>(LHS); | ||||
990 | TypedInit *LHSt = dyn_cast<TypedInit>(LHS); | ||||
991 | if (!LHSl && !LHSs && !LHSt) { | ||||
992 | TokError("expected list or string type argument in unary operator"); | ||||
993 | return nullptr; | ||||
994 | } | ||||
995 | if (LHSt) { | ||||
996 | ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); | ||||
997 | StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType()); | ||||
998 | if (!LType && !SType) { | ||||
999 | TokError("expected list or string type argument in unary operator"); | ||||
1000 | return nullptr; | ||||
1001 | } | ||||
1002 | } | ||||
1003 | |||||
1004 | if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL || | ||||
1005 | Code == UnOpInit::SIZE) { | ||||
1006 | if (!LHSl && !LHSt) { | ||||
1007 | TokError("expected list type argument in unary operator"); | ||||
1008 | return nullptr; | ||||
1009 | } | ||||
1010 | } | ||||
1011 | |||||
1012 | if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) { | ||||
1013 | if (LHSl && LHSl->empty()) { | ||||
1014 | TokError("empty list argument in unary operator"); | ||||
1015 | return nullptr; | ||||
1016 | } | ||||
1017 | if (LHSl) { | ||||
1018 | Init *Item = LHSl->getElement(0); | ||||
1019 | TypedInit *Itemt = dyn_cast<TypedInit>(Item); | ||||
1020 | if (!Itemt) { | ||||
1021 | TokError("untyped list element in unary operator"); | ||||
1022 | return nullptr; | ||||
1023 | } | ||||
1024 | Type = (Code == UnOpInit::HEAD) ? Itemt->getType() | ||||
1025 | : ListRecTy::get(Itemt->getType()); | ||||
1026 | } else { | ||||
1027 | assert(LHSt && "expected list type argument in unary operator")((LHSt && "expected list type argument in unary operator" ) ? static_cast<void> (0) : __assert_fail ("LHSt && \"expected list type argument in unary operator\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1027, __PRETTY_FUNCTION__)); | ||||
1028 | ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); | ||||
1029 | if (!LType) { | ||||
1030 | TokError("expected list type argument in unary operator"); | ||||
1031 | return nullptr; | ||||
1032 | } | ||||
1033 | Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType; | ||||
1034 | } | ||||
1035 | } | ||||
1036 | } | ||||
1037 | |||||
1038 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1039 | TokError("expected ')' in unary operator"); | ||||
1040 | return nullptr; | ||||
1041 | } | ||||
1042 | Lex.Lex(); // eat the ')' | ||||
1043 | return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec); | ||||
1044 | } | ||||
1045 | |||||
1046 | case tgtok::XIsA: { | ||||
1047 | // Value ::= !isa '<' Type '>' '(' Value ')' | ||||
1048 | Lex.Lex(); // eat the operation | ||||
1049 | |||||
1050 | RecTy *Type = ParseOperatorType(); | ||||
1051 | if (!Type) | ||||
1052 | return nullptr; | ||||
1053 | |||||
1054 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1055 | TokError("expected '(' after type of !isa"); | ||||
1056 | return nullptr; | ||||
1057 | } | ||||
1058 | Lex.Lex(); // eat the '(' | ||||
1059 | |||||
1060 | Init *LHS = ParseValue(CurRec); | ||||
1061 | if (!LHS) | ||||
1062 | return nullptr; | ||||
1063 | |||||
1064 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1065 | TokError("expected ')' in !isa"); | ||||
1066 | return nullptr; | ||||
1067 | } | ||||
1068 | Lex.Lex(); // eat the ')' | ||||
1069 | |||||
1070 | return (IsAOpInit::get(Type, LHS))->Fold(); | ||||
1071 | } | ||||
1072 | |||||
1073 | case tgtok::XConcat: | ||||
1074 | case tgtok::XADD: | ||||
1075 | case tgtok::XMUL: | ||||
1076 | case tgtok::XAND: | ||||
1077 | case tgtok::XOR: | ||||
1078 | case tgtok::XSRA: | ||||
1079 | case tgtok::XSRL: | ||||
1080 | case tgtok::XSHL: | ||||
1081 | case tgtok::XEq: | ||||
1082 | case tgtok::XNe: | ||||
1083 | case tgtok::XLe: | ||||
1084 | case tgtok::XLt: | ||||
1085 | case tgtok::XGe: | ||||
1086 | case tgtok::XGt: | ||||
1087 | case tgtok::XListConcat: | ||||
1088 | case tgtok::XListSplat: | ||||
1089 | case tgtok::XStrConcat: | ||||
1090 | case tgtok::XSetOp: { // Value ::= !binop '(' Value ',' Value ')' | ||||
1091 | tgtok::TokKind OpTok = Lex.getCode(); | ||||
1092 | SMLoc OpLoc = Lex.getLoc(); | ||||
1093 | Lex.Lex(); // eat the operation | ||||
1094 | |||||
1095 | BinOpInit::BinaryOp Code; | ||||
1096 | switch (OpTok) { | ||||
1097 | default: llvm_unreachable("Unhandled code!")::llvm::llvm_unreachable_internal("Unhandled code!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1097); | ||||
1098 | case tgtok::XConcat: Code = BinOpInit::CONCAT; break; | ||||
1099 | case tgtok::XADD: Code = BinOpInit::ADD; break; | ||||
1100 | case tgtok::XMUL: Code = BinOpInit::MUL; break; | ||||
1101 | case tgtok::XAND: Code = BinOpInit::AND; break; | ||||
1102 | case tgtok::XOR: Code = BinOpInit::OR; break; | ||||
1103 | case tgtok::XSRA: Code = BinOpInit::SRA; break; | ||||
1104 | case tgtok::XSRL: Code = BinOpInit::SRL; break; | ||||
1105 | case tgtok::XSHL: Code = BinOpInit::SHL; break; | ||||
1106 | case tgtok::XEq: Code = BinOpInit::EQ; break; | ||||
1107 | case tgtok::XNe: Code = BinOpInit::NE; break; | ||||
1108 | case tgtok::XLe: Code = BinOpInit::LE; break; | ||||
1109 | case tgtok::XLt: Code = BinOpInit::LT; break; | ||||
1110 | case tgtok::XGe: Code = BinOpInit::GE; break; | ||||
1111 | case tgtok::XGt: Code = BinOpInit::GT; break; | ||||
1112 | case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break; | ||||
1113 | case tgtok::XListSplat: Code = BinOpInit::LISTSPLAT; break; | ||||
1114 | case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break; | ||||
1115 | case tgtok::XSetOp: Code = BinOpInit::SETOP; break; | ||||
1116 | } | ||||
1117 | |||||
1118 | RecTy *Type = nullptr; | ||||
1119 | RecTy *ArgType = nullptr; | ||||
1120 | switch (OpTok) { | ||||
1121 | default: | ||||
1122 | llvm_unreachable("Unhandled code!")::llvm::llvm_unreachable_internal("Unhandled code!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1122); | ||||
1123 | case tgtok::XConcat: | ||||
1124 | case tgtok::XSetOp: | ||||
1125 | Type = DagRecTy::get(); | ||||
1126 | ArgType = DagRecTy::get(); | ||||
1127 | break; | ||||
1128 | case tgtok::XAND: | ||||
1129 | case tgtok::XOR: | ||||
1130 | case tgtok::XSRA: | ||||
1131 | case tgtok::XSRL: | ||||
1132 | case tgtok::XSHL: | ||||
1133 | case tgtok::XADD: | ||||
1134 | case tgtok::XMUL: | ||||
1135 | Type = IntRecTy::get(); | ||||
1136 | ArgType = IntRecTy::get(); | ||||
1137 | break; | ||||
1138 | case tgtok::XEq: | ||||
1139 | case tgtok::XNe: | ||||
1140 | Type = BitRecTy::get(); | ||||
1141 | // ArgType for Eq / Ne is not known at this point | ||||
1142 | break; | ||||
1143 | case tgtok::XLe: | ||||
1144 | case tgtok::XLt: | ||||
1145 | case tgtok::XGe: | ||||
1146 | case tgtok::XGt: | ||||
1147 | Type = BitRecTy::get(); | ||||
1148 | ArgType = IntRecTy::get(); | ||||
1149 | break; | ||||
1150 | case tgtok::XListConcat: | ||||
1151 | // We don't know the list type until we parse the first argument | ||||
1152 | ArgType = ItemType; | ||||
1153 | break; | ||||
1154 | case tgtok::XListSplat: | ||||
1155 | // Can't do any typechecking until we parse the first argument. | ||||
1156 | break; | ||||
1157 | case tgtok::XStrConcat: | ||||
1158 | Type = StringRecTy::get(); | ||||
1159 | ArgType = StringRecTy::get(); | ||||
1160 | break; | ||||
1161 | } | ||||
1162 | |||||
1163 | if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) { | ||||
1164 | Error(OpLoc, Twine("expected value of type '") + | ||||
1165 | ItemType->getAsString() + "', got '" + | ||||
1166 | Type->getAsString() + "'"); | ||||
1167 | return nullptr; | ||||
1168 | } | ||||
1169 | |||||
1170 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1171 | TokError("expected '(' after binary operator"); | ||||
1172 | return nullptr; | ||||
1173 | } | ||||
1174 | Lex.Lex(); // eat the '(' | ||||
1175 | |||||
1176 | SmallVector<Init*, 2> InitList; | ||||
1177 | |||||
1178 | for (;;) { | ||||
1179 | SMLoc InitLoc = Lex.getLoc(); | ||||
1180 | InitList.push_back(ParseValue(CurRec, ArgType)); | ||||
1181 | if (!InitList.back()) return nullptr; | ||||
1182 | |||||
1183 | TypedInit *InitListBack = dyn_cast<TypedInit>(InitList.back()); | ||||
1184 | if (!InitListBack) { | ||||
1185 | Error(OpLoc, Twine("expected value to be a typed value, got '" + | ||||
1186 | InitList.back()->getAsString() + "'")); | ||||
1187 | return nullptr; | ||||
1188 | } | ||||
1189 | RecTy *ListType = InitListBack->getType(); | ||||
1190 | if (!ArgType) { | ||||
1191 | ArgType = ListType; | ||||
1192 | |||||
1193 | switch (Code) { | ||||
1194 | case BinOpInit::LISTCONCAT: | ||||
1195 | if (!isa<ListRecTy>(ArgType)) { | ||||
1196 | Error(InitLoc, Twine("expected a list, got value of type '") + | ||||
1197 | ArgType->getAsString() + "'"); | ||||
1198 | return nullptr; | ||||
1199 | } | ||||
1200 | break; | ||||
1201 | case BinOpInit::LISTSPLAT: | ||||
1202 | if (ItemType && InitList.size() == 1) { | ||||
1203 | if (!isa<ListRecTy>(ItemType)) { | ||||
1204 | Error(OpLoc, | ||||
1205 | Twine("expected output type to be a list, got type '") + | ||||
1206 | ItemType->getAsString() + "'"); | ||||
1207 | return nullptr; | ||||
1208 | } | ||||
1209 | if (!ArgType->getListTy()->typeIsConvertibleTo(ItemType)) { | ||||
1210 | Error(OpLoc, Twine("expected first arg type to be '") + | ||||
1211 | ArgType->getAsString() + | ||||
1212 | "', got value of type '" + | ||||
1213 | cast<ListRecTy>(ItemType) | ||||
1214 | ->getElementType() | ||||
1215 | ->getAsString() + | ||||
1216 | "'"); | ||||
1217 | return nullptr; | ||||
1218 | } | ||||
1219 | } | ||||
1220 | if (InitList.size() == 2 && !isa<IntRecTy>(ArgType)) { | ||||
1221 | Error(InitLoc, Twine("expected second parameter to be an int, got " | ||||
1222 | "value of type '") + | ||||
1223 | ArgType->getAsString() + "'"); | ||||
1224 | return nullptr; | ||||
1225 | } | ||||
1226 | ArgType = nullptr; // Broken invariant: types not identical. | ||||
1227 | break; | ||||
1228 | case BinOpInit::EQ: | ||||
1229 | case BinOpInit::NE: | ||||
1230 | if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) && | ||||
1231 | !ArgType->typeIsConvertibleTo(StringRecTy::get())) { | ||||
1232 | Error(InitLoc, Twine("expected int, bits, or string; got value of " | ||||
1233 | "type '") + ArgType->getAsString() + "'"); | ||||
1234 | return nullptr; | ||||
1235 | } | ||||
1236 | break; | ||||
1237 | default: llvm_unreachable("other ops have fixed argument types")::llvm::llvm_unreachable_internal("other ops have fixed argument types" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1237); | ||||
1238 | } | ||||
1239 | } else { | ||||
1240 | RecTy *Resolved = resolveTypes(ArgType, ListType); | ||||
1241 | if (!Resolved) { | ||||
1242 | Error(InitLoc, Twine("expected value of type '") + | ||||
1243 | ArgType->getAsString() + "', got '" + | ||||
1244 | ListType->getAsString() + "'"); | ||||
1245 | return nullptr; | ||||
1246 | } | ||||
1247 | if (Code != BinOpInit::ADD && Code != BinOpInit::AND && | ||||
1248 | Code != BinOpInit::OR && Code != BinOpInit::SRA && | ||||
1249 | Code != BinOpInit::SRL && Code != BinOpInit::SHL && | ||||
1250 | Code != BinOpInit::MUL) | ||||
1251 | ArgType = Resolved; | ||||
1252 | } | ||||
1253 | |||||
1254 | // Deal with BinOps whose arguments have different types, by | ||||
1255 | // rewriting ArgType in between them. | ||||
1256 | switch (Code) { | ||||
1257 | case BinOpInit::SETOP: | ||||
1258 | // After parsing the first dag argument, switch to expecting | ||||
1259 | // a record, with no restriction on its superclasses. | ||||
1260 | ArgType = RecordRecTy::get({}); | ||||
1261 | break; | ||||
1262 | default: | ||||
1263 | break; | ||||
1264 | } | ||||
1265 | |||||
1266 | if (Lex.getCode() != tgtok::comma) | ||||
1267 | break; | ||||
1268 | Lex.Lex(); // eat the ',' | ||||
1269 | } | ||||
1270 | |||||
1271 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1272 | TokError("expected ')' in operator"); | ||||
1273 | return nullptr; | ||||
1274 | } | ||||
1275 | Lex.Lex(); // eat the ')' | ||||
1276 | |||||
1277 | // listconcat returns a list with type of the argument. | ||||
1278 | if (Code == BinOpInit::LISTCONCAT) | ||||
1279 | Type = ArgType; | ||||
1280 | // listsplat returns a list of type of the *first* argument. | ||||
1281 | if (Code == BinOpInit::LISTSPLAT) | ||||
1282 | Type = cast<TypedInit>(InitList.front())->getType()->getListTy(); | ||||
1283 | |||||
1284 | // We allow multiple operands to associative operators like !strconcat as | ||||
1285 | // shorthand for nesting them. | ||||
1286 | if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT || | ||||
1287 | Code == BinOpInit::CONCAT || Code == BinOpInit::ADD || | ||||
1288 | Code == BinOpInit::AND || Code == BinOpInit::OR || | ||||
1289 | Code == BinOpInit::MUL) { | ||||
1290 | while (InitList.size() > 2) { | ||||
1291 | Init *RHS = InitList.pop_back_val(); | ||||
1292 | RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec); | ||||
1293 | InitList.back() = RHS; | ||||
1294 | } | ||||
1295 | } | ||||
1296 | |||||
1297 | if (InitList.size() == 2) | ||||
1298 | return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) | ||||
1299 | ->Fold(CurRec); | ||||
1300 | |||||
1301 | Error(OpLoc, "expected two operands to operator"); | ||||
1302 | return nullptr; | ||||
1303 | } | ||||
1304 | |||||
1305 | case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')' | ||||
1306 | SMLoc OpLoc = Lex.getLoc(); | ||||
1307 | Lex.Lex(); // eat the operation | ||||
1308 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1309 | TokError("expected '(' after !foreach"); | ||||
1310 | return nullptr; | ||||
1311 | } | ||||
1312 | |||||
1313 | if (Lex.Lex() != tgtok::Id) { // eat the '(' | ||||
1314 | TokError("first argument of !foreach must be an identifier"); | ||||
1315 | return nullptr; | ||||
1316 | } | ||||
1317 | |||||
1318 | Init *LHS = StringInit::get(Lex.getCurStrVal()); | ||||
1319 | |||||
1320 | if (CurRec && CurRec->getValue(LHS)) { | ||||
1321 | TokError((Twine("iteration variable '") + LHS->getAsString() + | ||||
1322 | "' already defined") | ||||
1323 | .str()); | ||||
1324 | return nullptr; | ||||
1325 | } | ||||
1326 | |||||
1327 | if (Lex.Lex() != tgtok::comma) { // eat the id | ||||
1328 | TokError("expected ',' in ternary operator"); | ||||
1329 | return nullptr; | ||||
1330 | } | ||||
1331 | Lex.Lex(); // eat the ',' | ||||
1332 | |||||
1333 | Init *MHS = ParseValue(CurRec); | ||||
1334 | if (!MHS) | ||||
1335 | return nullptr; | ||||
1336 | |||||
1337 | if (Lex.getCode() != tgtok::comma) { | ||||
1338 | TokError("expected ',' in ternary operator"); | ||||
1339 | return nullptr; | ||||
1340 | } | ||||
1341 | Lex.Lex(); // eat the ',' | ||||
1342 | |||||
1343 | TypedInit *MHSt = dyn_cast<TypedInit>(MHS); | ||||
1344 | if (!MHSt) { | ||||
1345 | TokError("could not get type of !foreach input"); | ||||
1346 | return nullptr; | ||||
1347 | } | ||||
1348 | |||||
1349 | RecTy *InEltType = nullptr; | ||||
1350 | RecTy *OutEltType = nullptr; | ||||
1351 | bool IsDAG = false; | ||||
1352 | |||||
1353 | if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) { | ||||
1354 | InEltType = InListTy->getElementType(); | ||||
1355 | if (ItemType) { | ||||
1356 | if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) { | ||||
1357 | OutEltType = OutListTy->getElementType(); | ||||
1358 | } else { | ||||
1359 | Error(OpLoc, | ||||
1360 | "expected value of type '" + Twine(ItemType->getAsString()) + | ||||
1361 | "', but got !foreach of list type"); | ||||
1362 | return nullptr; | ||||
1363 | } | ||||
1364 | } | ||||
1365 | } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) { | ||||
1366 | InEltType = InDagTy; | ||||
1367 | if (ItemType && !isa<DagRecTy>(ItemType)) { | ||||
1368 | Error(OpLoc, | ||||
1369 | "expected value of type '" + Twine(ItemType->getAsString()) + | ||||
1370 | "', but got !foreach of dag type"); | ||||
1371 | return nullptr; | ||||
1372 | } | ||||
1373 | IsDAG = true; | ||||
1374 | } else { | ||||
1375 | TokError("!foreach must have list or dag input"); | ||||
1376 | return nullptr; | ||||
1377 | } | ||||
1378 | |||||
1379 | // We need to create a temporary record to provide a scope for the iteration | ||||
1380 | // variable while parsing top-level foreach's. | ||||
1381 | std::unique_ptr<Record> ParseRecTmp; | ||||
1382 | Record *ParseRec = CurRec; | ||||
1383 | if (!ParseRec) { | ||||
1384 | ParseRecTmp = std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records); | ||||
1385 | ParseRec = ParseRecTmp.get(); | ||||
1386 | } | ||||
1387 | |||||
1388 | ParseRec->addValue(RecordVal(LHS, InEltType, false)); | ||||
1389 | Init *RHS = ParseValue(ParseRec, OutEltType); | ||||
1390 | ParseRec->removeValue(LHS); | ||||
1391 | if (!RHS) | ||||
1392 | return nullptr; | ||||
1393 | |||||
1394 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1395 | TokError("expected ')' in binary operator"); | ||||
1396 | return nullptr; | ||||
1397 | } | ||||
1398 | Lex.Lex(); // eat the ')' | ||||
1399 | |||||
1400 | RecTy *OutType; | ||||
1401 | if (IsDAG) { | ||||
1402 | OutType = InEltType; | ||||
1403 | } else { | ||||
1404 | TypedInit *RHSt = dyn_cast<TypedInit>(RHS); | ||||
1405 | if (!RHSt) { | ||||
1406 | TokError("could not get type of !foreach result"); | ||||
1407 | return nullptr; | ||||
1408 | } | ||||
1409 | OutType = RHSt->getType()->getListTy(); | ||||
1410 | } | ||||
1411 | |||||
1412 | return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType)) | ||||
1413 | ->Fold(CurRec); | ||||
1414 | } | ||||
1415 | |||||
1416 | case tgtok::XDag: | ||||
1417 | case tgtok::XIf: | ||||
1418 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' | ||||
1419 | TernOpInit::TernaryOp Code; | ||||
1420 | RecTy *Type = nullptr; | ||||
1421 | |||||
1422 | tgtok::TokKind LexCode = Lex.getCode(); | ||||
1423 | Lex.Lex(); // eat the operation | ||||
1424 | switch (LexCode) { | ||||
1425 | default: llvm_unreachable("Unhandled code!")::llvm::llvm_unreachable_internal("Unhandled code!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1425); | ||||
1426 | case tgtok::XDag: | ||||
1427 | Code = TernOpInit::DAG; | ||||
1428 | Type = DagRecTy::get(); | ||||
1429 | ItemType = nullptr; | ||||
1430 | break; | ||||
1431 | case tgtok::XIf: | ||||
1432 | Code = TernOpInit::IF; | ||||
1433 | break; | ||||
1434 | case tgtok::XSubst: | ||||
1435 | Code = TernOpInit::SUBST; | ||||
1436 | break; | ||||
1437 | } | ||||
1438 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1439 | TokError("expected '(' after ternary operator"); | ||||
1440 | return nullptr; | ||||
1441 | } | ||||
1442 | Lex.Lex(); // eat the '(' | ||||
1443 | |||||
1444 | Init *LHS = ParseValue(CurRec); | ||||
1445 | if (!LHS) return nullptr; | ||||
1446 | |||||
1447 | if (Lex.getCode() != tgtok::comma) { | ||||
1448 | TokError("expected ',' in ternary operator"); | ||||
1449 | return nullptr; | ||||
1450 | } | ||||
1451 | Lex.Lex(); // eat the ',' | ||||
1452 | |||||
1453 | SMLoc MHSLoc = Lex.getLoc(); | ||||
1454 | Init *MHS = ParseValue(CurRec, ItemType); | ||||
1455 | if (!MHS) | ||||
1456 | return nullptr; | ||||
1457 | |||||
1458 | if (Lex.getCode() != tgtok::comma) { | ||||
1459 | TokError("expected ',' in ternary operator"); | ||||
1460 | return nullptr; | ||||
1461 | } | ||||
1462 | Lex.Lex(); // eat the ',' | ||||
1463 | |||||
1464 | SMLoc RHSLoc = Lex.getLoc(); | ||||
1465 | Init *RHS = ParseValue(CurRec, ItemType); | ||||
1466 | if (!RHS) | ||||
1467 | return nullptr; | ||||
1468 | |||||
1469 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1470 | TokError("expected ')' in binary operator"); | ||||
1471 | return nullptr; | ||||
1472 | } | ||||
1473 | Lex.Lex(); // eat the ')' | ||||
1474 | |||||
1475 | switch (LexCode) { | ||||
1476 | default: llvm_unreachable("Unhandled code!")::llvm::llvm_unreachable_internal("Unhandled code!", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 1476); | ||||
1477 | case tgtok::XDag: { | ||||
1478 | TypedInit *MHSt = dyn_cast<TypedInit>(MHS); | ||||
1479 | if (!MHSt && !isa<UnsetInit>(MHS)) { | ||||
1480 | Error(MHSLoc, "could not determine type of the child list in !dag"); | ||||
1481 | return nullptr; | ||||
1482 | } | ||||
1483 | if (MHSt && !isa<ListRecTy>(MHSt->getType())) { | ||||
1484 | Error(MHSLoc, Twine("expected list of children, got type '") + | ||||
1485 | MHSt->getType()->getAsString() + "'"); | ||||
1486 | return nullptr; | ||||
1487 | } | ||||
1488 | |||||
1489 | TypedInit *RHSt = dyn_cast<TypedInit>(RHS); | ||||
1490 | if (!RHSt && !isa<UnsetInit>(RHS)) { | ||||
1491 | Error(RHSLoc, "could not determine type of the name list in !dag"); | ||||
1492 | return nullptr; | ||||
1493 | } | ||||
1494 | if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) { | ||||
1495 | Error(RHSLoc, Twine("expected list<string>, got type '") + | ||||
1496 | RHSt->getType()->getAsString() + "'"); | ||||
1497 | return nullptr; | ||||
1498 | } | ||||
1499 | |||||
1500 | if (!MHSt && !RHSt) { | ||||
1501 | Error(MHSLoc, | ||||
1502 | "cannot have both unset children and unset names in !dag"); | ||||
1503 | return nullptr; | ||||
1504 | } | ||||
1505 | break; | ||||
1506 | } | ||||
1507 | case tgtok::XIf: { | ||||
1508 | RecTy *MHSTy = nullptr; | ||||
1509 | RecTy *RHSTy = nullptr; | ||||
1510 | |||||
1511 | if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS)) | ||||
1512 | MHSTy = MHSt->getType(); | ||||
1513 | if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS)) | ||||
1514 | MHSTy = BitsRecTy::get(MHSbits->getNumBits()); | ||||
1515 | if (isa<BitInit>(MHS)) | ||||
1516 | MHSTy = BitRecTy::get(); | ||||
1517 | |||||
1518 | if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS)) | ||||
1519 | RHSTy = RHSt->getType(); | ||||
1520 | if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS)) | ||||
1521 | RHSTy = BitsRecTy::get(RHSbits->getNumBits()); | ||||
1522 | if (isa<BitInit>(RHS)) | ||||
1523 | RHSTy = BitRecTy::get(); | ||||
1524 | |||||
1525 | // For UnsetInit, it's typed from the other hand. | ||||
1526 | if (isa<UnsetInit>(MHS)) | ||||
1527 | MHSTy = RHSTy; | ||||
1528 | if (isa<UnsetInit>(RHS)) | ||||
1529 | RHSTy = MHSTy; | ||||
1530 | |||||
1531 | if (!MHSTy || !RHSTy) { | ||||
1532 | TokError("could not get type for !if"); | ||||
1533 | return nullptr; | ||||
1534 | } | ||||
1535 | |||||
1536 | Type = resolveTypes(MHSTy, RHSTy); | ||||
1537 | if (!Type) { | ||||
1538 | TokError(Twine("inconsistent types '") + MHSTy->getAsString() + | ||||
1539 | "' and '" + RHSTy->getAsString() + "' for !if"); | ||||
1540 | return nullptr; | ||||
1541 | } | ||||
1542 | break; | ||||
1543 | } | ||||
1544 | case tgtok::XSubst: { | ||||
1545 | TypedInit *RHSt = dyn_cast<TypedInit>(RHS); | ||||
1546 | if (!RHSt) { | ||||
1547 | TokError("could not get type for !subst"); | ||||
1548 | return nullptr; | ||||
1549 | } | ||||
1550 | Type = RHSt->getType(); | ||||
1551 | break; | ||||
1552 | } | ||||
1553 | } | ||||
1554 | return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec); | ||||
1555 | } | ||||
1556 | |||||
1557 | case tgtok::XCond: | ||||
1558 | return ParseOperationCond(CurRec, ItemType); | ||||
1559 | |||||
1560 | case tgtok::XFoldl: { | ||||
1561 | // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')' | ||||
1562 | Lex.Lex(); // eat the operation | ||||
1563 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1564 | TokError("expected '(' after !foldl"); | ||||
1565 | return nullptr; | ||||
1566 | } | ||||
1567 | Lex.Lex(); // eat the '(' | ||||
1568 | |||||
1569 | Init *StartUntyped = ParseValue(CurRec); | ||||
1570 | if (!StartUntyped) | ||||
1571 | return nullptr; | ||||
1572 | |||||
1573 | TypedInit *Start = dyn_cast<TypedInit>(StartUntyped); | ||||
1574 | if (!Start) { | ||||
1575 | TokError(Twine("could not get type of !foldl start: '") + | ||||
1576 | StartUntyped->getAsString() + "'"); | ||||
1577 | return nullptr; | ||||
1578 | } | ||||
1579 | |||||
1580 | if (Lex.getCode() != tgtok::comma) { | ||||
1581 | TokError("expected ',' in !foldl"); | ||||
1582 | return nullptr; | ||||
1583 | } | ||||
1584 | Lex.Lex(); // eat the ',' | ||||
1585 | |||||
1586 | Init *ListUntyped = ParseValue(CurRec); | ||||
1587 | if (!ListUntyped) | ||||
1588 | return nullptr; | ||||
1589 | |||||
1590 | TypedInit *List = dyn_cast<TypedInit>(ListUntyped); | ||||
1591 | if (!List) { | ||||
1592 | TokError(Twine("could not get type of !foldl list: '") + | ||||
1593 | ListUntyped->getAsString() + "'"); | ||||
1594 | return nullptr; | ||||
1595 | } | ||||
1596 | |||||
1597 | ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType()); | ||||
1598 | if (!ListType) { | ||||
1599 | TokError(Twine("!foldl list must be a list, but is of type '") + | ||||
1600 | List->getType()->getAsString()); | ||||
1601 | return nullptr; | ||||
1602 | } | ||||
1603 | |||||
1604 | if (Lex.getCode() != tgtok::comma) { | ||||
1605 | TokError("expected ',' in !foldl"); | ||||
1606 | return nullptr; | ||||
1607 | } | ||||
1608 | |||||
1609 | if (Lex.Lex() != tgtok::Id) { // eat the ',' | ||||
1610 | TokError("third argument of !foldl must be an identifier"); | ||||
1611 | return nullptr; | ||||
1612 | } | ||||
1613 | |||||
1614 | Init *A = StringInit::get(Lex.getCurStrVal()); | ||||
1615 | if (CurRec && CurRec->getValue(A)) { | ||||
1616 | TokError((Twine("left !foldl variable '") + A->getAsString() + | ||||
1617 | "' already defined") | ||||
1618 | .str()); | ||||
1619 | return nullptr; | ||||
1620 | } | ||||
1621 | |||||
1622 | if (Lex.Lex() != tgtok::comma) { // eat the id | ||||
1623 | TokError("expected ',' in !foldl"); | ||||
1624 | return nullptr; | ||||
1625 | } | ||||
1626 | |||||
1627 | if (Lex.Lex() != tgtok::Id) { // eat the ',' | ||||
1628 | TokError("fourth argument of !foldl must be an identifier"); | ||||
1629 | return nullptr; | ||||
1630 | } | ||||
1631 | |||||
1632 | Init *B = StringInit::get(Lex.getCurStrVal()); | ||||
1633 | if (CurRec && CurRec->getValue(B)) { | ||||
1634 | TokError((Twine("right !foldl variable '") + B->getAsString() + | ||||
1635 | "' already defined") | ||||
1636 | .str()); | ||||
1637 | return nullptr; | ||||
1638 | } | ||||
1639 | |||||
1640 | if (Lex.Lex() != tgtok::comma) { // eat the id | ||||
1641 | TokError("expected ',' in !foldl"); | ||||
1642 | return nullptr; | ||||
1643 | } | ||||
1644 | Lex.Lex(); // eat the ',' | ||||
1645 | |||||
1646 | // We need to create a temporary record to provide a scope for the iteration | ||||
1647 | // variable while parsing top-level foreach's. | ||||
1648 | std::unique_ptr<Record> ParseRecTmp; | ||||
1649 | Record *ParseRec = CurRec; | ||||
1650 | if (!ParseRec) { | ||||
1651 | ParseRecTmp = std::make_unique<Record>(".parse", ArrayRef<SMLoc>{}, Records); | ||||
1652 | ParseRec = ParseRecTmp.get(); | ||||
1653 | } | ||||
1654 | |||||
1655 | ParseRec->addValue(RecordVal(A, Start->getType(), false)); | ||||
1656 | ParseRec->addValue(RecordVal(B, ListType->getElementType(), false)); | ||||
1657 | Init *ExprUntyped = ParseValue(ParseRec); | ||||
1658 | ParseRec->removeValue(A); | ||||
1659 | ParseRec->removeValue(B); | ||||
1660 | if (!ExprUntyped) | ||||
1661 | return nullptr; | ||||
1662 | |||||
1663 | TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped); | ||||
1664 | if (!Expr) { | ||||
1665 | TokError("could not get type of !foldl expression"); | ||||
1666 | return nullptr; | ||||
1667 | } | ||||
1668 | |||||
1669 | if (Expr->getType() != Start->getType()) { | ||||
1670 | TokError(Twine("!foldl expression must be of same type as start (") + | ||||
1671 | Start->getType()->getAsString() + "), but is of type " + | ||||
1672 | Expr->getType()->getAsString()); | ||||
1673 | return nullptr; | ||||
1674 | } | ||||
1675 | |||||
1676 | if (Lex.getCode() != tgtok::r_paren) { | ||||
1677 | TokError("expected ')' in fold operator"); | ||||
1678 | return nullptr; | ||||
1679 | } | ||||
1680 | Lex.Lex(); // eat the ')' | ||||
1681 | |||||
1682 | return FoldOpInit::get(Start, List, A, B, Expr, Start->getType()) | ||||
1683 | ->Fold(CurRec); | ||||
1684 | } | ||||
1685 | } | ||||
1686 | } | ||||
1687 | |||||
1688 | /// ParseOperatorType - Parse a type for an operator. This returns | ||||
1689 | /// null on error. | ||||
1690 | /// | ||||
1691 | /// OperatorType ::= '<' Type '>' | ||||
1692 | /// | ||||
1693 | RecTy *TGParser::ParseOperatorType() { | ||||
1694 | RecTy *Type = nullptr; | ||||
1695 | |||||
1696 | if (Lex.getCode() != tgtok::less) { | ||||
1697 | TokError("expected type name for operator"); | ||||
1698 | return nullptr; | ||||
1699 | } | ||||
1700 | Lex.Lex(); // eat the < | ||||
1701 | |||||
1702 | Type = ParseType(); | ||||
1703 | |||||
1704 | if (!Type) { | ||||
1705 | TokError("expected type name for operator"); | ||||
1706 | return nullptr; | ||||
1707 | } | ||||
1708 | |||||
1709 | if (Lex.getCode() != tgtok::greater) { | ||||
1710 | TokError("expected type name for operator"); | ||||
1711 | return nullptr; | ||||
1712 | } | ||||
1713 | Lex.Lex(); // eat the > | ||||
1714 | |||||
1715 | return Type; | ||||
1716 | } | ||||
1717 | |||||
1718 | Init *TGParser::ParseOperationCond(Record *CurRec, RecTy *ItemType) { | ||||
1719 | Lex.Lex(); // eat the operation 'cond' | ||||
1720 | |||||
1721 | if (Lex.getCode() != tgtok::l_paren) { | ||||
1722 | TokError("expected '(' after !cond operator"); | ||||
1723 | return nullptr; | ||||
1724 | } | ||||
1725 | Lex.Lex(); // eat the '(' | ||||
1726 | |||||
1727 | // Parse through '[Case: Val,]+' | ||||
1728 | SmallVector<Init *, 4> Case; | ||||
1729 | SmallVector<Init *, 4> Val; | ||||
1730 | while (true) { | ||||
1731 | if (Lex.getCode() == tgtok::r_paren) { | ||||
1732 | Lex.Lex(); // eat the ')' | ||||
1733 | break; | ||||
1734 | } | ||||
1735 | |||||
1736 | Init *V = ParseValue(CurRec); | ||||
1737 | if (!V) | ||||
1738 | return nullptr; | ||||
1739 | Case.push_back(V); | ||||
1740 | |||||
1741 | if (Lex.getCode() != tgtok::colon) { | ||||
1742 | TokError("expected ':' following a condition in !cond operator"); | ||||
1743 | return nullptr; | ||||
1744 | } | ||||
1745 | Lex.Lex(); // eat the ':' | ||||
1746 | |||||
1747 | V = ParseValue(CurRec, ItemType); | ||||
1748 | if (!V) | ||||
1749 | return nullptr; | ||||
1750 | Val.push_back(V); | ||||
1751 | |||||
1752 | if (Lex.getCode() == tgtok::r_paren) { | ||||
1753 | Lex.Lex(); // eat the ')' | ||||
1754 | break; | ||||
1755 | } | ||||
1756 | |||||
1757 | if (Lex.getCode() != tgtok::comma) { | ||||
1758 | TokError("expected ',' or ')' following a value in !cond operator"); | ||||
1759 | return nullptr; | ||||
1760 | } | ||||
1761 | Lex.Lex(); // eat the ',' | ||||
1762 | } | ||||
1763 | |||||
1764 | if (Case.size() < 1) { | ||||
1765 | TokError("there should be at least 1 'condition : value' in the !cond operator"); | ||||
1766 | return nullptr; | ||||
1767 | } | ||||
1768 | |||||
1769 | // resolve type | ||||
1770 | RecTy *Type = nullptr; | ||||
1771 | for (Init *V : Val) { | ||||
1772 | RecTy *VTy = nullptr; | ||||
1773 | if (TypedInit *Vt
| ||||
1774 | VTy = Vt->getType(); | ||||
1775 | if (BitsInit *Vbits
| ||||
1776 | VTy = BitsRecTy::get(Vbits->getNumBits()); | ||||
1777 | if (isa<BitInit>(V)) | ||||
1778 | VTy = BitRecTy::get(); | ||||
1779 | |||||
1780 | if (Type == nullptr) { | ||||
1781 | if (!isa<UnsetInit>(V)) | ||||
1782 | Type = VTy; | ||||
1783 | } else { | ||||
1784 | if (!isa<UnsetInit>(V)) { | ||||
1785 | RecTy *RType = resolveTypes(Type, VTy); | ||||
1786 | if (!RType) { | ||||
1787 | TokError(Twine("inconsistent types '") + Type->getAsString() + | ||||
1788 | "' and '" + VTy->getAsString() + "' for !cond"); | ||||
| |||||
1789 | return nullptr; | ||||
1790 | } | ||||
1791 | Type = RType; | ||||
1792 | } | ||||
1793 | } | ||||
1794 | } | ||||
1795 | |||||
1796 | if (!Type) { | ||||
1797 | TokError("could not determine type for !cond from its arguments"); | ||||
1798 | return nullptr; | ||||
1799 | } | ||||
1800 | return CondOpInit::get(Case, Val, Type)->Fold(CurRec); | ||||
1801 | } | ||||
1802 | |||||
1803 | /// ParseSimpleValue - Parse a tblgen value. This returns null on error. | ||||
1804 | /// | ||||
1805 | /// SimpleValue ::= IDValue | ||||
1806 | /// SimpleValue ::= INTVAL | ||||
1807 | /// SimpleValue ::= STRVAL+ | ||||
1808 | /// SimpleValue ::= CODEFRAGMENT | ||||
1809 | /// SimpleValue ::= '?' | ||||
1810 | /// SimpleValue ::= '{' ValueList '}' | ||||
1811 | /// SimpleValue ::= ID '<' ValueListNE '>' | ||||
1812 | /// SimpleValue ::= '[' ValueList ']' | ||||
1813 | /// SimpleValue ::= '(' IDValue DagArgList ')' | ||||
1814 | /// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' | ||||
1815 | /// SimpleValue ::= ADDTOK '(' Value ',' Value ')' | ||||
1816 | /// SimpleValue ::= SHLTOK '(' Value ',' Value ')' | ||||
1817 | /// SimpleValue ::= SRATOK '(' Value ',' Value ')' | ||||
1818 | /// SimpleValue ::= SRLTOK '(' Value ',' Value ')' | ||||
1819 | /// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')' | ||||
1820 | /// SimpleValue ::= LISTSPLATTOK '(' Value ',' Value ')' | ||||
1821 | /// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' | ||||
1822 | /// SimpleValue ::= COND '(' [Value ':' Value,]+ ')' | ||||
1823 | /// | ||||
1824 | Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, | ||||
1825 | IDParseMode Mode) { | ||||
1826 | Init *R = nullptr; | ||||
1827 | switch (Lex.getCode()) { | ||||
1828 | default: TokError("Unknown token when parsing a value"); break; | ||||
1829 | case tgtok::paste: | ||||
1830 | // This is a leading paste operation. This is deprecated but | ||||
1831 | // still exists in some .td files. Ignore it. | ||||
1832 | Lex.Lex(); // Skip '#'. | ||||
1833 | return ParseSimpleValue(CurRec, ItemType, Mode); | ||||
1834 | case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; | ||||
1835 | case tgtok::BinaryIntVal: { | ||||
1836 | auto BinaryVal = Lex.getCurBinaryIntVal(); | ||||
1837 | SmallVector<Init*, 16> Bits(BinaryVal.second); | ||||
1838 | for (unsigned i = 0, e = BinaryVal.second; i != e; ++i) | ||||
1839 | Bits[i] = BitInit::get(BinaryVal.first & (1LL << i)); | ||||
1840 | R = BitsInit::get(Bits); | ||||
1841 | Lex.Lex(); | ||||
1842 | break; | ||||
1843 | } | ||||
1844 | case tgtok::StrVal: { | ||||
1845 | std::string Val = Lex.getCurStrVal(); | ||||
1846 | Lex.Lex(); | ||||
1847 | |||||
1848 | // Handle multiple consecutive concatenated strings. | ||||
1849 | while (Lex.getCode() == tgtok::StrVal) { | ||||
1850 | Val += Lex.getCurStrVal(); | ||||
1851 | Lex.Lex(); | ||||
1852 | } | ||||
1853 | |||||
1854 | R = StringInit::get(Val); | ||||
1855 | break; | ||||
1856 | } | ||||
1857 | case tgtok::CodeFragment: | ||||
1858 | R = CodeInit::get(Lex.getCurStrVal(), Lex.getLoc()); | ||||
1859 | Lex.Lex(); | ||||
1860 | break; | ||||
1861 | case tgtok::question: | ||||
1862 | R = UnsetInit::get(); | ||||
1863 | Lex.Lex(); | ||||
1864 | break; | ||||
1865 | case tgtok::Id: { | ||||
1866 | SMLoc NameLoc = Lex.getLoc(); | ||||
1867 | StringInit *Name = StringInit::get(Lex.getCurStrVal()); | ||||
1868 | if (Lex.Lex() != tgtok::less) // consume the Id. | ||||
1869 | return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue | ||||
1870 | |||||
1871 | // Value ::= ID '<' ValueListNE '>' | ||||
1872 | if (Lex.Lex() == tgtok::greater) { | ||||
1873 | TokError("expected non-empty value list"); | ||||
1874 | return nullptr; | ||||
1875 | } | ||||
1876 | |||||
1877 | // This is a CLASS<initvalslist> expression. This is supposed to synthesize | ||||
1878 | // a new anonymous definition, deriving from CLASS<initvalslist> with no | ||||
1879 | // body. | ||||
1880 | Record *Class = Records.getClass(Name->getValue()); | ||||
1881 | if (!Class) { | ||||
1882 | Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'"); | ||||
1883 | return nullptr; | ||||
1884 | } | ||||
1885 | |||||
1886 | SmallVector<Init *, 8> Args; | ||||
1887 | ParseValueList(Args, CurRec, Class); | ||||
1888 | if (Args.empty()) return nullptr; | ||||
1889 | |||||
1890 | if (Lex.getCode() != tgtok::greater) { | ||||
1891 | TokError("expected '>' at end of value list"); | ||||
1892 | return nullptr; | ||||
1893 | } | ||||
1894 | Lex.Lex(); // eat the '>' | ||||
1895 | |||||
1896 | // Typecheck the template arguments list | ||||
1897 | ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs(); | ||||
1898 | if (ExpectedArgs.size() < Args.size()) { | ||||
1899 | Error(NameLoc, | ||||
1900 | "More template args specified than expected"); | ||||
1901 | return nullptr; | ||||
1902 | } | ||||
1903 | |||||
1904 | for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) { | ||||
1905 | RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]); | ||||
1906 | if (i < Args.size()) { | ||||
1907 | if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) { | ||||
1908 | RecTy *ExpectedType = ExpectedArg->getType(); | ||||
1909 | if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) { | ||||
1910 | Error(NameLoc, | ||||
1911 | "Value specified for template argument #" + Twine(i) + " (" + | ||||
1912 | ExpectedArg->getNameInitAsString() + ") is of type '" + | ||||
1913 | TI->getType()->getAsString() + "', expected '" + | ||||
1914 | ExpectedType->getAsString() + "': " + TI->getAsString()); | ||||
1915 | return nullptr; | ||||
1916 | } | ||||
1917 | continue; | ||||
1918 | } | ||||
1919 | } else if (ExpectedArg->getValue()->isComplete()) | ||||
1920 | continue; | ||||
1921 | |||||
1922 | Error(NameLoc, | ||||
1923 | "Value not specified for template argument #" + Twine(i) + " (" + | ||||
1924 | ExpectedArgs[i]->getAsUnquotedString() + ")"); | ||||
1925 | return nullptr; | ||||
1926 | } | ||||
1927 | |||||
1928 | return VarDefInit::get(Class, Args)->Fold(); | ||||
1929 | } | ||||
1930 | case tgtok::l_brace: { // Value ::= '{' ValueList '}' | ||||
1931 | SMLoc BraceLoc = Lex.getLoc(); | ||||
1932 | Lex.Lex(); // eat the '{' | ||||
1933 | SmallVector<Init*, 16> Vals; | ||||
1934 | |||||
1935 | if (Lex.getCode() != tgtok::r_brace) { | ||||
1936 | ParseValueList(Vals, CurRec); | ||||
1937 | if (Vals.empty()) return nullptr; | ||||
1938 | } | ||||
1939 | if (Lex.getCode() != tgtok::r_brace) { | ||||
1940 | TokError("expected '}' at end of bit list value"); | ||||
1941 | return nullptr; | ||||
1942 | } | ||||
1943 | Lex.Lex(); // eat the '}' | ||||
1944 | |||||
1945 | SmallVector<Init *, 16> NewBits; | ||||
1946 | |||||
1947 | // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it | ||||
1948 | // first. We'll first read everything in to a vector, then we can reverse | ||||
1949 | // it to get the bits in the correct order for the BitsInit value. | ||||
1950 | for (unsigned i = 0, e = Vals.size(); i != e; ++i) { | ||||
1951 | // FIXME: The following two loops would not be duplicated | ||||
1952 | // if the API was a little more orthogonal. | ||||
1953 | |||||
1954 | // bits<n> values are allowed to initialize n bits. | ||||
1955 | if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) { | ||||
1956 | for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i) | ||||
1957 | NewBits.push_back(BI->getBit((e - i) - 1)); | ||||
1958 | continue; | ||||
1959 | } | ||||
1960 | // bits<n> can also come from variable initializers. | ||||
1961 | if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) { | ||||
1962 | if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) { | ||||
1963 | for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i) | ||||
1964 | NewBits.push_back(VI->getBit((e - i) - 1)); | ||||
1965 | continue; | ||||
1966 | } | ||||
1967 | // Fallthrough to try convert this to a bit. | ||||
1968 | } | ||||
1969 | // All other values must be convertible to just a single bit. | ||||
1970 | Init *Bit = Vals[i]->getCastTo(BitRecTy::get()); | ||||
1971 | if (!Bit) { | ||||
1972 | Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() + | ||||
1973 | ") is not convertable to a bit"); | ||||
1974 | return nullptr; | ||||
1975 | } | ||||
1976 | NewBits.push_back(Bit); | ||||
1977 | } | ||||
1978 | std::reverse(NewBits.begin(), NewBits.end()); | ||||
1979 | return BitsInit::get(NewBits); | ||||
1980 | } | ||||
1981 | case tgtok::l_square: { // Value ::= '[' ValueList ']' | ||||
1982 | Lex.Lex(); // eat the '[' | ||||
1983 | SmallVector<Init*, 16> Vals; | ||||
1984 | |||||
1985 | RecTy *DeducedEltTy = nullptr; | ||||
1986 | ListRecTy *GivenListTy = nullptr; | ||||
1987 | |||||
1988 | if (ItemType) { | ||||
1989 | ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType); | ||||
1990 | if (!ListType) { | ||||
1991 | TokError(Twine("Type mismatch for list, expected list type, got ") + | ||||
1992 | ItemType->getAsString()); | ||||
1993 | return nullptr; | ||||
1994 | } | ||||
1995 | GivenListTy = ListType; | ||||
1996 | } | ||||
1997 | |||||
1998 | if (Lex.getCode() != tgtok::r_square) { | ||||
1999 | ParseValueList(Vals, CurRec, nullptr, | ||||
2000 | GivenListTy ? GivenListTy->getElementType() : nullptr); | ||||
2001 | if (Vals.empty()) return nullptr; | ||||
2002 | } | ||||
2003 | if (Lex.getCode() != tgtok::r_square) { | ||||
2004 | TokError("expected ']' at end of list value"); | ||||
2005 | return nullptr; | ||||
2006 | } | ||||
2007 | Lex.Lex(); // eat the ']' | ||||
2008 | |||||
2009 | RecTy *GivenEltTy = nullptr; | ||||
2010 | if (Lex.getCode() == tgtok::less) { | ||||
2011 | // Optional list element type | ||||
2012 | Lex.Lex(); // eat the '<' | ||||
2013 | |||||
2014 | GivenEltTy = ParseType(); | ||||
2015 | if (!GivenEltTy) { | ||||
2016 | // Couldn't parse element type | ||||
2017 | return nullptr; | ||||
2018 | } | ||||
2019 | |||||
2020 | if (Lex.getCode() != tgtok::greater) { | ||||
2021 | TokError("expected '>' at end of list element type"); | ||||
2022 | return nullptr; | ||||
2023 | } | ||||
2024 | Lex.Lex(); // eat the '>' | ||||
2025 | } | ||||
2026 | |||||
2027 | // Check elements | ||||
2028 | RecTy *EltTy = nullptr; | ||||
2029 | for (Init *V : Vals) { | ||||
2030 | TypedInit *TArg = dyn_cast<TypedInit>(V); | ||||
2031 | if (TArg) { | ||||
2032 | if (EltTy) { | ||||
2033 | EltTy = resolveTypes(EltTy, TArg->getType()); | ||||
2034 | if (!EltTy) { | ||||
2035 | TokError("Incompatible types in list elements"); | ||||
2036 | return nullptr; | ||||
2037 | } | ||||
2038 | } else { | ||||
2039 | EltTy = TArg->getType(); | ||||
2040 | } | ||||
2041 | } | ||||
2042 | } | ||||
2043 | |||||
2044 | if (GivenEltTy) { | ||||
2045 | if (EltTy) { | ||||
2046 | // Verify consistency | ||||
2047 | if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { | ||||
2048 | TokError("Incompatible types in list elements"); | ||||
2049 | return nullptr; | ||||
2050 | } | ||||
2051 | } | ||||
2052 | EltTy = GivenEltTy; | ||||
2053 | } | ||||
2054 | |||||
2055 | if (!EltTy) { | ||||
2056 | if (!ItemType) { | ||||
2057 | TokError("No type for list"); | ||||
2058 | return nullptr; | ||||
2059 | } | ||||
2060 | DeducedEltTy = GivenListTy->getElementType(); | ||||
2061 | } else { | ||||
2062 | // Make sure the deduced type is compatible with the given type | ||||
2063 | if (GivenListTy) { | ||||
2064 | if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { | ||||
2065 | TokError(Twine("Element type mismatch for list: element type '") + | ||||
2066 | EltTy->getAsString() + "' not convertible to '" + | ||||
2067 | GivenListTy->getElementType()->getAsString()); | ||||
2068 | return nullptr; | ||||
2069 | } | ||||
2070 | } | ||||
2071 | DeducedEltTy = EltTy; | ||||
2072 | } | ||||
2073 | |||||
2074 | return ListInit::get(Vals, DeducedEltTy); | ||||
2075 | } | ||||
2076 | case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' | ||||
2077 | Lex.Lex(); // eat the '(' | ||||
2078 | if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast && | ||||
2079 | Lex.getCode() != tgtok::question && Lex.getCode() != tgtok::XGetOp) { | ||||
2080 | TokError("expected identifier in dag init"); | ||||
2081 | return nullptr; | ||||
2082 | } | ||||
2083 | |||||
2084 | Init *Operator = ParseValue(CurRec); | ||||
2085 | if (!Operator) return nullptr; | ||||
2086 | |||||
2087 | // If the operator name is present, parse it. | ||||
2088 | StringInit *OperatorName = nullptr; | ||||
2089 | if (Lex.getCode() == tgtok::colon) { | ||||
2090 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' | ||||
2091 | TokError("expected variable name in dag operator"); | ||||
2092 | return nullptr; | ||||
2093 | } | ||||
2094 | OperatorName = StringInit::get(Lex.getCurStrVal()); | ||||
2095 | Lex.Lex(); // eat the VarName. | ||||
2096 | } | ||||
2097 | |||||
2098 | SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs; | ||||
2099 | if (Lex.getCode() != tgtok::r_paren) { | ||||
2100 | ParseDagArgList(DagArgs, CurRec); | ||||
2101 | if (DagArgs.empty()) return nullptr; | ||||
2102 | } | ||||
2103 | |||||
2104 | if (Lex.getCode() != tgtok::r_paren) { | ||||
2105 | TokError("expected ')' in dag init"); | ||||
2106 | return nullptr; | ||||
2107 | } | ||||
2108 | Lex.Lex(); // eat the ')' | ||||
2109 | |||||
2110 | return DagInit::get(Operator, OperatorName, DagArgs); | ||||
2111 | } | ||||
2112 | |||||
2113 | case tgtok::XHead: | ||||
2114 | case tgtok::XTail: | ||||
2115 | case tgtok::XSize: | ||||
2116 | case tgtok::XEmpty: | ||||
2117 | case tgtok::XCast: | ||||
2118 | case tgtok::XGetOp: // Value ::= !unop '(' Value ')' | ||||
2119 | case tgtok::XIsA: | ||||
2120 | case tgtok::XConcat: | ||||
2121 | case tgtok::XDag: | ||||
2122 | case tgtok::XADD: | ||||
2123 | case tgtok::XMUL: | ||||
2124 | case tgtok::XAND: | ||||
2125 | case tgtok::XOR: | ||||
2126 | case tgtok::XSRA: | ||||
2127 | case tgtok::XSRL: | ||||
2128 | case tgtok::XSHL: | ||||
2129 | case tgtok::XEq: | ||||
2130 | case tgtok::XNe: | ||||
2131 | case tgtok::XLe: | ||||
2132 | case tgtok::XLt: | ||||
2133 | case tgtok::XGe: | ||||
2134 | case tgtok::XGt: | ||||
2135 | case tgtok::XListConcat: | ||||
2136 | case tgtok::XListSplat: | ||||
2137 | case tgtok::XStrConcat: | ||||
2138 | case tgtok::XSetOp: // Value ::= !binop '(' Value ',' Value ')' | ||||
2139 | case tgtok::XIf: | ||||
2140 | case tgtok::XCond: | ||||
2141 | case tgtok::XFoldl: | ||||
2142 | case tgtok::XForEach: | ||||
2143 | case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' | ||||
2144 | return ParseOperation(CurRec, ItemType); | ||||
2145 | } | ||||
2146 | } | ||||
2147 | |||||
2148 | return R; | ||||
2149 | } | ||||
2150 | |||||
2151 | /// ParseValue - Parse a tblgen value. This returns null on error. | ||||
2152 | /// | ||||
2153 | /// Value ::= SimpleValue ValueSuffix* | ||||
2154 | /// ValueSuffix ::= '{' BitList '}' | ||||
2155 | /// ValueSuffix ::= '[' BitList ']' | ||||
2156 | /// ValueSuffix ::= '.' ID | ||||
2157 | /// | ||||
2158 | Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { | ||||
2159 | Init *Result = ParseSimpleValue(CurRec, ItemType, Mode); | ||||
2160 | if (!Result) return nullptr; | ||||
2161 | |||||
2162 | // Parse the suffixes now if present. | ||||
2163 | while (true) { | ||||
2164 | switch (Lex.getCode()) { | ||||
2165 | default: return Result; | ||||
2166 | case tgtok::l_brace: { | ||||
2167 | if (Mode == ParseNameMode) | ||||
2168 | // This is the beginning of the object body. | ||||
2169 | return Result; | ||||
2170 | |||||
2171 | SMLoc CurlyLoc = Lex.getLoc(); | ||||
2172 | Lex.Lex(); // eat the '{' | ||||
2173 | SmallVector<unsigned, 16> Ranges; | ||||
2174 | ParseRangeList(Ranges); | ||||
2175 | if (Ranges.empty()) return nullptr; | ||||
2176 | |||||
2177 | // Reverse the bitlist. | ||||
2178 | std::reverse(Ranges.begin(), Ranges.end()); | ||||
2179 | Result = Result->convertInitializerBitRange(Ranges); | ||||
2180 | if (!Result) { | ||||
2181 | Error(CurlyLoc, "Invalid bit range for value"); | ||||
2182 | return nullptr; | ||||
2183 | } | ||||
2184 | |||||
2185 | // Eat the '}'. | ||||
2186 | if (Lex.getCode() != tgtok::r_brace) { | ||||
2187 | TokError("expected '}' at end of bit range list"); | ||||
2188 | return nullptr; | ||||
2189 | } | ||||
2190 | Lex.Lex(); | ||||
2191 | break; | ||||
2192 | } | ||||
2193 | case tgtok::l_square: { | ||||
2194 | SMLoc SquareLoc = Lex.getLoc(); | ||||
2195 | Lex.Lex(); // eat the '[' | ||||
2196 | SmallVector<unsigned, 16> Ranges; | ||||
2197 | ParseRangeList(Ranges); | ||||
2198 | if (Ranges.empty()) return nullptr; | ||||
2199 | |||||
2200 | Result = Result->convertInitListSlice(Ranges); | ||||
2201 | if (!Result) { | ||||
2202 | Error(SquareLoc, "Invalid range for list slice"); | ||||
2203 | return nullptr; | ||||
2204 | } | ||||
2205 | |||||
2206 | // Eat the ']'. | ||||
2207 | if (Lex.getCode() != tgtok::r_square) { | ||||
2208 | TokError("expected ']' at end of list slice"); | ||||
2209 | return nullptr; | ||||
2210 | } | ||||
2211 | Lex.Lex(); | ||||
2212 | break; | ||||
2213 | } | ||||
2214 | case tgtok::period: { | ||||
2215 | if (Lex.Lex() != tgtok::Id) { // eat the . | ||||
2216 | TokError("expected field identifier after '.'"); | ||||
2217 | return nullptr; | ||||
2218 | } | ||||
2219 | StringInit *FieldName = StringInit::get(Lex.getCurStrVal()); | ||||
2220 | if (!Result->getFieldType(FieldName)) { | ||||
2221 | TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + | ||||
2222 | Result->getAsString() + "'"); | ||||
2223 | return nullptr; | ||||
2224 | } | ||||
2225 | Result = FieldInit::get(Result, FieldName)->Fold(CurRec); | ||||
2226 | Lex.Lex(); // eat field name | ||||
2227 | break; | ||||
2228 | } | ||||
2229 | |||||
2230 | case tgtok::paste: | ||||
2231 | SMLoc PasteLoc = Lex.getLoc(); | ||||
2232 | TypedInit *LHS = dyn_cast<TypedInit>(Result); | ||||
2233 | if (!LHS) { | ||||
2234 | Error(PasteLoc, "LHS of paste is not typed!"); | ||||
2235 | return nullptr; | ||||
2236 | } | ||||
2237 | |||||
2238 | // Check if it's a 'listA # listB' | ||||
2239 | if (isa<ListRecTy>(LHS->getType())) { | ||||
2240 | Lex.Lex(); // Eat the '#'. | ||||
2241 | |||||
2242 | switch (Lex.getCode()) { | ||||
2243 | case tgtok::colon: | ||||
2244 | case tgtok::semi: | ||||
2245 | case tgtok::l_brace: | ||||
2246 | Result = LHS; // trailing paste, ignore. | ||||
2247 | break; | ||||
2248 | default: | ||||
2249 | Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode); | ||||
2250 | Result = BinOpInit::getListConcat(LHS, RHSResult); | ||||
2251 | } | ||||
2252 | break; | ||||
2253 | } | ||||
2254 | |||||
2255 | // Create a !strconcat() operation, first casting each operand to | ||||
2256 | // a string if necessary. | ||||
2257 | if (LHS->getType() != StringRecTy::get()) { | ||||
2258 | auto CastLHS = dyn_cast<TypedInit>( | ||||
2259 | UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get()) | ||||
2260 | ->Fold(CurRec)); | ||||
2261 | if (!CastLHS) { | ||||
2262 | Error(PasteLoc, | ||||
2263 | Twine("can't cast '") + LHS->getAsString() + "' to string"); | ||||
2264 | return nullptr; | ||||
2265 | } | ||||
2266 | LHS = CastLHS; | ||||
2267 | } | ||||
2268 | |||||
2269 | TypedInit *RHS = nullptr; | ||||
2270 | |||||
2271 | Lex.Lex(); // Eat the '#'. | ||||
2272 | switch (Lex.getCode()) { | ||||
2273 | case tgtok::colon: | ||||
2274 | case tgtok::semi: | ||||
2275 | case tgtok::l_brace: | ||||
2276 | // These are all of the tokens that can begin an object body. | ||||
2277 | // Some of these can also begin values but we disallow those cases | ||||
2278 | // because they are unlikely to be useful. | ||||
2279 | |||||
2280 | // Trailing paste, concat with an empty string. | ||||
2281 | RHS = StringInit::get(""); | ||||
2282 | break; | ||||
2283 | |||||
2284 | default: | ||||
2285 | Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode); | ||||
2286 | RHS = dyn_cast<TypedInit>(RHSResult); | ||||
2287 | if (!RHS) { | ||||
2288 | Error(PasteLoc, "RHS of paste is not typed!"); | ||||
2289 | return nullptr; | ||||
2290 | } | ||||
2291 | |||||
2292 | if (RHS->getType() != StringRecTy::get()) { | ||||
2293 | auto CastRHS = dyn_cast<TypedInit>( | ||||
2294 | UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get()) | ||||
2295 | ->Fold(CurRec)); | ||||
2296 | if (!CastRHS) { | ||||
2297 | Error(PasteLoc, | ||||
2298 | Twine("can't cast '") + RHS->getAsString() + "' to string"); | ||||
2299 | return nullptr; | ||||
2300 | } | ||||
2301 | RHS = CastRHS; | ||||
2302 | } | ||||
2303 | |||||
2304 | break; | ||||
2305 | } | ||||
2306 | |||||
2307 | Result = BinOpInit::getStrConcat(LHS, RHS); | ||||
2308 | break; | ||||
2309 | } | ||||
2310 | } | ||||
2311 | } | ||||
2312 | |||||
2313 | /// ParseDagArgList - Parse the argument list for a dag literal expression. | ||||
2314 | /// | ||||
2315 | /// DagArg ::= Value (':' VARNAME)? | ||||
2316 | /// DagArg ::= VARNAME | ||||
2317 | /// DagArgList ::= DagArg | ||||
2318 | /// DagArgList ::= DagArgList ',' DagArg | ||||
2319 | void TGParser::ParseDagArgList( | ||||
2320 | SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result, | ||||
2321 | Record *CurRec) { | ||||
2322 | |||||
2323 | while (true) { | ||||
2324 | // DagArg ::= VARNAME | ||||
2325 | if (Lex.getCode() == tgtok::VarName) { | ||||
2326 | // A missing value is treated like '?'. | ||||
2327 | StringInit *VarName = StringInit::get(Lex.getCurStrVal()); | ||||
2328 | Result.emplace_back(UnsetInit::get(), VarName); | ||||
2329 | Lex.Lex(); | ||||
2330 | } else { | ||||
2331 | // DagArg ::= Value (':' VARNAME)? | ||||
2332 | Init *Val = ParseValue(CurRec); | ||||
2333 | if (!Val) { | ||||
2334 | Result.clear(); | ||||
2335 | return; | ||||
2336 | } | ||||
2337 | |||||
2338 | // If the variable name is present, add it. | ||||
2339 | StringInit *VarName = nullptr; | ||||
2340 | if (Lex.getCode() == tgtok::colon) { | ||||
2341 | if (Lex.Lex() != tgtok::VarName) { // eat the ':' | ||||
2342 | TokError("expected variable name in dag literal"); | ||||
2343 | Result.clear(); | ||||
2344 | return; | ||||
2345 | } | ||||
2346 | VarName = StringInit::get(Lex.getCurStrVal()); | ||||
2347 | Lex.Lex(); // eat the VarName. | ||||
2348 | } | ||||
2349 | |||||
2350 | Result.push_back(std::make_pair(Val, VarName)); | ||||
2351 | } | ||||
2352 | if (Lex.getCode() != tgtok::comma) break; | ||||
2353 | Lex.Lex(); // eat the ',' | ||||
2354 | } | ||||
2355 | } | ||||
2356 | |||||
2357 | /// ParseValueList - Parse a comma separated list of values, returning them as a | ||||
2358 | /// vector. Note that this always expects to be able to parse at least one | ||||
2359 | /// value. It returns an empty list if this is not possible. | ||||
2360 | /// | ||||
2361 | /// ValueList ::= Value (',' Value) | ||||
2362 | /// | ||||
2363 | void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec, | ||||
2364 | Record *ArgsRec, RecTy *EltTy) { | ||||
2365 | RecTy *ItemType = EltTy; | ||||
2366 | unsigned int ArgN = 0; | ||||
2367 | if (ArgsRec && !EltTy) { | ||||
2368 | ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs(); | ||||
2369 | if (TArgs.empty()) { | ||||
2370 | TokError("template argument provided to non-template class"); | ||||
2371 | Result.clear(); | ||||
2372 | return; | ||||
2373 | } | ||||
2374 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); | ||||
2375 | if (!RV) { | ||||
2376 | errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] | ||||
2377 | << ")\n"; | ||||
2378 | } | ||||
2379 | assert(RV && "Template argument record not found??")((RV && "Template argument record not found??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template argument record not found??\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2379, __PRETTY_FUNCTION__)); | ||||
2380 | ItemType = RV->getType(); | ||||
2381 | ++ArgN; | ||||
2382 | } | ||||
2383 | Result.push_back(ParseValue(CurRec, ItemType)); | ||||
2384 | if (!Result.back()) { | ||||
2385 | Result.clear(); | ||||
2386 | return; | ||||
2387 | } | ||||
2388 | |||||
2389 | while (Lex.getCode() == tgtok::comma) { | ||||
2390 | Lex.Lex(); // Eat the comma | ||||
2391 | |||||
2392 | // ignore trailing comma for lists | ||||
2393 | if (Lex.getCode() == tgtok::r_square) | ||||
2394 | return; | ||||
2395 | |||||
2396 | if (ArgsRec && !EltTy) { | ||||
2397 | ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs(); | ||||
2398 | if (ArgN >= TArgs.size()) { | ||||
2399 | TokError("too many template arguments"); | ||||
2400 | Result.clear(); | ||||
2401 | return; | ||||
2402 | } | ||||
2403 | const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); | ||||
2404 | assert(RV && "Template argument record not found??")((RV && "Template argument record not found??") ? static_cast <void> (0) : __assert_fail ("RV && \"Template argument record not found??\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2404, __PRETTY_FUNCTION__)); | ||||
2405 | ItemType = RV->getType(); | ||||
2406 | ++ArgN; | ||||
2407 | } | ||||
2408 | Result.push_back(ParseValue(CurRec, ItemType)); | ||||
2409 | if (!Result.back()) { | ||||
2410 | Result.clear(); | ||||
2411 | return; | ||||
2412 | } | ||||
2413 | } | ||||
2414 | } | ||||
2415 | |||||
2416 | /// ParseDeclaration - Read a declaration, returning the name of field ID, or an | ||||
2417 | /// empty string on error. This can happen in a number of different context's, | ||||
2418 | /// including within a def or in the template args for a def (which which case | ||||
2419 | /// CurRec will be non-null) and within the template args for a multiclass (in | ||||
2420 | /// which case CurRec will be null, but CurMultiClass will be set). This can | ||||
2421 | /// also happen within a def that is within a multiclass, which will set both | ||||
2422 | /// CurRec and CurMultiClass. | ||||
2423 | /// | ||||
2424 | /// Declaration ::= FIELD? Type ID ('=' Value)? | ||||
2425 | /// | ||||
2426 | Init *TGParser::ParseDeclaration(Record *CurRec, | ||||
2427 | bool ParsingTemplateArgs) { | ||||
2428 | // Read the field prefix if present. | ||||
2429 | bool HasField = Lex.getCode() == tgtok::Field; | ||||
2430 | if (HasField) Lex.Lex(); | ||||
2431 | |||||
2432 | RecTy *Type = ParseType(); | ||||
2433 | if (!Type) return nullptr; | ||||
2434 | |||||
2435 | if (Lex.getCode() != tgtok::Id) { | ||||
2436 | TokError("Expected identifier in declaration"); | ||||
2437 | return nullptr; | ||||
2438 | } | ||||
2439 | |||||
2440 | std::string Str = Lex.getCurStrVal(); | ||||
2441 | if (Str == "NAME") { | ||||
2442 | TokError("'" + Str + "' is a reserved variable name"); | ||||
2443 | return nullptr; | ||||
2444 | } | ||||
2445 | |||||
2446 | SMLoc IdLoc = Lex.getLoc(); | ||||
2447 | Init *DeclName = StringInit::get(Str); | ||||
2448 | Lex.Lex(); | ||||
2449 | |||||
2450 | if (ParsingTemplateArgs) { | ||||
2451 | if (CurRec) | ||||
2452 | DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":"); | ||||
2453 | else | ||||
2454 | assert(CurMultiClass)((CurMultiClass) ? static_cast<void> (0) : __assert_fail ("CurMultiClass", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2454, __PRETTY_FUNCTION__)); | ||||
2455 | if (CurMultiClass) | ||||
2456 | DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, | ||||
2457 | "::"); | ||||
2458 | } | ||||
2459 | |||||
2460 | // Add the value. | ||||
2461 | if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) | ||||
2462 | return nullptr; | ||||
2463 | |||||
2464 | // If a value is present, parse it. | ||||
2465 | if (Lex.getCode() == tgtok::equal) { | ||||
2466 | Lex.Lex(); | ||||
2467 | SMLoc ValLoc = Lex.getLoc(); | ||||
2468 | Init *Val = ParseValue(CurRec, Type); | ||||
2469 | if (!Val || | ||||
2470 | SetValue(CurRec, ValLoc, DeclName, None, Val)) | ||||
2471 | // Return the name, even if an error is thrown. This is so that we can | ||||
2472 | // continue to make some progress, even without the value having been | ||||
2473 | // initialized. | ||||
2474 | return DeclName; | ||||
2475 | } | ||||
2476 | |||||
2477 | return DeclName; | ||||
2478 | } | ||||
2479 | |||||
2480 | /// ParseForeachDeclaration - Read a foreach declaration, returning | ||||
2481 | /// the name of the declared object or a NULL Init on error. Return | ||||
2482 | /// the name of the parsed initializer list through ForeachListName. | ||||
2483 | /// | ||||
2484 | /// ForeachDeclaration ::= ID '=' '{' RangeList '}' | ||||
2485 | /// ForeachDeclaration ::= ID '=' RangePiece | ||||
2486 | /// ForeachDeclaration ::= ID '=' Value | ||||
2487 | /// | ||||
2488 | VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) { | ||||
2489 | if (Lex.getCode() != tgtok::Id) { | ||||
2490 | TokError("Expected identifier in foreach declaration"); | ||||
2491 | return nullptr; | ||||
2492 | } | ||||
2493 | |||||
2494 | Init *DeclName = StringInit::get(Lex.getCurStrVal()); | ||||
2495 | Lex.Lex(); | ||||
2496 | |||||
2497 | // If a value is present, parse it. | ||||
2498 | if (Lex.getCode() != tgtok::equal) { | ||||
2499 | TokError("Expected '=' in foreach declaration"); | ||||
2500 | return nullptr; | ||||
2501 | } | ||||
2502 | Lex.Lex(); // Eat the '=' | ||||
2503 | |||||
2504 | RecTy *IterType = nullptr; | ||||
2505 | SmallVector<unsigned, 16> Ranges; | ||||
2506 | |||||
2507 | switch (Lex.getCode()) { | ||||
2508 | case tgtok::l_brace: { // '{' RangeList '}' | ||||
2509 | Lex.Lex(); // eat the '{' | ||||
2510 | ParseRangeList(Ranges); | ||||
2511 | if (Lex.getCode() != tgtok::r_brace) { | ||||
2512 | TokError("expected '}' at end of bit range list"); | ||||
2513 | return nullptr; | ||||
2514 | } | ||||
2515 | Lex.Lex(); | ||||
2516 | break; | ||||
2517 | } | ||||
2518 | |||||
2519 | default: { | ||||
2520 | SMLoc ValueLoc = Lex.getLoc(); | ||||
2521 | Init *I = ParseValue(nullptr); | ||||
2522 | if (!I) | ||||
2523 | return nullptr; | ||||
2524 | |||||
2525 | TypedInit *TI = dyn_cast<TypedInit>(I); | ||||
2526 | if (TI && isa<ListRecTy>(TI->getType())) { | ||||
2527 | ForeachListValue = I; | ||||
2528 | IterType = cast<ListRecTy>(TI->getType())->getElementType(); | ||||
2529 | break; | ||||
2530 | } | ||||
2531 | |||||
2532 | if (TI) { | ||||
2533 | if (ParseRangePiece(Ranges, TI)) | ||||
2534 | return nullptr; | ||||
2535 | break; | ||||
2536 | } | ||||
2537 | |||||
2538 | std::string Type; | ||||
2539 | if (TI) | ||||
2540 | Type = (Twine("' of type '") + TI->getType()->getAsString()).str(); | ||||
2541 | Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'"); | ||||
2542 | if (CurMultiClass) { | ||||
2543 | PrintNote({}, "references to multiclass template arguments cannot be " | ||||
2544 | "resolved at this time"); | ||||
2545 | } | ||||
2546 | return nullptr; | ||||
2547 | } | ||||
2548 | } | ||||
2549 | |||||
2550 | |||||
2551 | if (!Ranges.empty()) { | ||||
2552 | assert(!IterType && "Type already initialized?")((!IterType && "Type already initialized?") ? static_cast <void> (0) : __assert_fail ("!IterType && \"Type already initialized?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2552, __PRETTY_FUNCTION__)); | ||||
2553 | IterType = IntRecTy::get(); | ||||
2554 | std::vector<Init*> Values; | ||||
2555 | for (unsigned R : Ranges) | ||||
2556 | Values.push_back(IntInit::get(R)); | ||||
2557 | ForeachListValue = ListInit::get(Values, IterType); | ||||
2558 | } | ||||
2559 | |||||
2560 | if (!IterType) | ||||
2561 | return nullptr; | ||||
2562 | |||||
2563 | return VarInit::get(DeclName, IterType); | ||||
2564 | } | ||||
2565 | |||||
2566 | /// ParseTemplateArgList - Read a template argument list, which is a non-empty | ||||
2567 | /// sequence of template-declarations in <>'s. If CurRec is non-null, these are | ||||
2568 | /// template args for a def, which may or may not be in a multiclass. If null, | ||||
2569 | /// these are the template args for a multiclass. | ||||
2570 | /// | ||||
2571 | /// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' | ||||
2572 | /// | ||||
2573 | bool TGParser::ParseTemplateArgList(Record *CurRec) { | ||||
2574 | assert(Lex.getCode() == tgtok::less && "Not a template arg list!")((Lex.getCode() == tgtok::less && "Not a template arg list!" ) ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::less && \"Not a template arg list!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2574, __PRETTY_FUNCTION__)); | ||||
2575 | Lex.Lex(); // eat the '<' | ||||
2576 | |||||
2577 | Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; | ||||
2578 | |||||
2579 | // Read the first declaration. | ||||
2580 | Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); | ||||
2581 | if (!TemplArg) | ||||
2582 | return true; | ||||
2583 | |||||
2584 | TheRecToAddTo->addTemplateArg(TemplArg); | ||||
2585 | |||||
2586 | while (Lex.getCode() == tgtok::comma) { | ||||
2587 | Lex.Lex(); // eat the ',' | ||||
2588 | |||||
2589 | // Read the following declarations. | ||||
2590 | SMLoc Loc = Lex.getLoc(); | ||||
2591 | TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); | ||||
2592 | if (!TemplArg) | ||||
2593 | return true; | ||||
2594 | |||||
2595 | if (TheRecToAddTo->isTemplateArg(TemplArg)) | ||||
2596 | return Error(Loc, "template argument with the same name has already been " | ||||
2597 | "defined"); | ||||
2598 | |||||
2599 | TheRecToAddTo->addTemplateArg(TemplArg); | ||||
2600 | } | ||||
2601 | |||||
2602 | if (Lex.getCode() != tgtok::greater) | ||||
2603 | return TokError("expected '>' at end of template argument list"); | ||||
2604 | Lex.Lex(); // eat the '>'. | ||||
2605 | return false; | ||||
2606 | } | ||||
2607 | |||||
2608 | /// ParseBodyItem - Parse a single item at within the body of a def or class. | ||||
2609 | /// | ||||
2610 | /// BodyItem ::= Declaration ';' | ||||
2611 | /// BodyItem ::= LET ID OptionalBitList '=' Value ';' | ||||
2612 | /// BodyItem ::= Defvar | ||||
2613 | bool TGParser::ParseBodyItem(Record *CurRec) { | ||||
2614 | if (Lex.getCode() == tgtok::Defvar) | ||||
2615 | return ParseDefvar(); | ||||
2616 | |||||
2617 | if (Lex.getCode() != tgtok::Let) { | ||||
2618 | if (!ParseDeclaration(CurRec, false)) | ||||
2619 | return true; | ||||
2620 | |||||
2621 | if (Lex.getCode() != tgtok::semi) | ||||
2622 | return TokError("expected ';' after declaration"); | ||||
2623 | Lex.Lex(); | ||||
2624 | return false; | ||||
2625 | } | ||||
2626 | |||||
2627 | // LET ID OptionalRangeList '=' Value ';' | ||||
2628 | if (Lex.Lex() != tgtok::Id) | ||||
2629 | return TokError("expected field identifier after let"); | ||||
2630 | |||||
2631 | SMLoc IdLoc = Lex.getLoc(); | ||||
2632 | StringInit *FieldName = StringInit::get(Lex.getCurStrVal()); | ||||
2633 | Lex.Lex(); // eat the field name. | ||||
2634 | |||||
2635 | SmallVector<unsigned, 16> BitList; | ||||
2636 | if (ParseOptionalBitList(BitList)) | ||||
2637 | return true; | ||||
2638 | std::reverse(BitList.begin(), BitList.end()); | ||||
2639 | |||||
2640 | if (Lex.getCode() != tgtok::equal) | ||||
2641 | return TokError("expected '=' in let expression"); | ||||
2642 | Lex.Lex(); // eat the '='. | ||||
2643 | |||||
2644 | RecordVal *Field = CurRec->getValue(FieldName); | ||||
2645 | if (!Field) | ||||
2646 | return TokError("Value '" + FieldName->getValue() + "' unknown!"); | ||||
2647 | |||||
2648 | RecTy *Type = Field->getType(); | ||||
2649 | if (!BitList.empty() && isa<BitsRecTy>(Type)) { | ||||
2650 | // When assigning to a subset of a 'bits' object, expect the RHS to have | ||||
2651 | // the type of that subset instead of the type of the whole object. | ||||
2652 | Type = BitsRecTy::get(BitList.size()); | ||||
2653 | } | ||||
2654 | |||||
2655 | Init *Val = ParseValue(CurRec, Type); | ||||
2656 | if (!Val) return true; | ||||
2657 | |||||
2658 | if (Lex.getCode() != tgtok::semi) | ||||
2659 | return TokError("expected ';' after let expression"); | ||||
2660 | Lex.Lex(); | ||||
2661 | |||||
2662 | return SetValue(CurRec, IdLoc, FieldName, BitList, Val); | ||||
2663 | } | ||||
2664 | |||||
2665 | /// ParseBody - Read the body of a class or def. Return true on error, false on | ||||
2666 | /// success. | ||||
2667 | /// | ||||
2668 | /// Body ::= ';' | ||||
2669 | /// Body ::= '{' BodyList '}' | ||||
2670 | /// BodyList BodyItem* | ||||
2671 | /// | ||||
2672 | bool TGParser::ParseBody(Record *CurRec) { | ||||
2673 | // If this is a null definition, just eat the semi and return. | ||||
2674 | if (Lex.getCode() == tgtok::semi) { | ||||
2675 | Lex.Lex(); | ||||
2676 | return false; | ||||
2677 | } | ||||
2678 | |||||
2679 | if (Lex.getCode() != tgtok::l_brace) | ||||
2680 | return TokError("Expected ';' or '{' to start body"); | ||||
2681 | // Eat the '{'. | ||||
2682 | Lex.Lex(); | ||||
2683 | |||||
2684 | // An object body introduces a new scope for local variables. | ||||
2685 | TGLocalVarScope *BodyScope = PushLocalScope(); | ||||
2686 | |||||
2687 | while (Lex.getCode() != tgtok::r_brace) | ||||
2688 | if (ParseBodyItem(CurRec)) | ||||
2689 | return true; | ||||
2690 | |||||
2691 | PopLocalScope(BodyScope); | ||||
2692 | |||||
2693 | // Eat the '}'. | ||||
2694 | Lex.Lex(); | ||||
2695 | return false; | ||||
2696 | } | ||||
2697 | |||||
2698 | /// Apply the current let bindings to \a CurRec. | ||||
2699 | /// \returns true on error, false otherwise. | ||||
2700 | bool TGParser::ApplyLetStack(Record *CurRec) { | ||||
2701 | for (SmallVectorImpl<LetRecord> &LetInfo : LetStack) | ||||
2702 | for (LetRecord &LR : LetInfo) | ||||
2703 | if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value)) | ||||
2704 | return true; | ||||
2705 | return false; | ||||
2706 | } | ||||
2707 | |||||
2708 | bool TGParser::ApplyLetStack(RecordsEntry &Entry) { | ||||
2709 | if (Entry.Rec) | ||||
2710 | return ApplyLetStack(Entry.Rec.get()); | ||||
2711 | |||||
2712 | for (auto &E : Entry.Loop->Entries) { | ||||
2713 | if (ApplyLetStack(E)) | ||||
2714 | return true; | ||||
2715 | } | ||||
2716 | |||||
2717 | return false; | ||||
2718 | } | ||||
2719 | |||||
2720 | /// ParseObjectBody - Parse the body of a def or class. This consists of an | ||||
2721 | /// optional ClassList followed by a Body. CurRec is the current def or class | ||||
2722 | /// that is being parsed. | ||||
2723 | /// | ||||
2724 | /// ObjectBody ::= BaseClassList Body | ||||
2725 | /// BaseClassList ::= /*empty*/ | ||||
2726 | /// BaseClassList ::= ':' BaseClassListNE | ||||
2727 | /// BaseClassListNE ::= SubClassRef (',' SubClassRef)* | ||||
2728 | /// | ||||
2729 | bool TGParser::ParseObjectBody(Record *CurRec) { | ||||
2730 | // If there is a baseclass list, read it. | ||||
2731 | if (Lex.getCode() == tgtok::colon) { | ||||
2732 | Lex.Lex(); | ||||
2733 | |||||
2734 | // Read all of the subclasses. | ||||
2735 | SubClassReference SubClass = ParseSubClassReference(CurRec, false); | ||||
2736 | while (true) { | ||||
2737 | // Check for error. | ||||
2738 | if (!SubClass.Rec) return true; | ||||
2739 | |||||
2740 | // Add it. | ||||
2741 | if (AddSubClass(CurRec, SubClass)) | ||||
2742 | return true; | ||||
2743 | |||||
2744 | if (Lex.getCode() != tgtok::comma) break; | ||||
2745 | Lex.Lex(); // eat ','. | ||||
2746 | SubClass = ParseSubClassReference(CurRec, false); | ||||
2747 | } | ||||
2748 | } | ||||
2749 | |||||
2750 | if (ApplyLetStack(CurRec)) | ||||
2751 | return true; | ||||
2752 | |||||
2753 | return ParseBody(CurRec); | ||||
2754 | } | ||||
2755 | |||||
2756 | /// ParseDef - Parse and return a top level or multiclass def, return the record | ||||
2757 | /// corresponding to it. This returns null on error. | ||||
2758 | /// | ||||
2759 | /// DefInst ::= DEF ObjectName ObjectBody | ||||
2760 | /// | ||||
2761 | bool TGParser::ParseDef(MultiClass *CurMultiClass) { | ||||
2762 | SMLoc DefLoc = Lex.getLoc(); | ||||
2763 | assert(Lex.getCode() == tgtok::Def && "Unknown tok")((Lex.getCode() == tgtok::Def && "Unknown tok") ? static_cast <void> (0) : __assert_fail ("Lex.getCode() == tgtok::Def && \"Unknown tok\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2763, __PRETTY_FUNCTION__)); | ||||
2764 | Lex.Lex(); // Eat the 'def' token. | ||||
2765 | |||||
2766 | // Parse ObjectName and make a record for it. | ||||
2767 | std::unique_ptr<Record> CurRec; | ||||
2768 | Init *Name = ParseObjectName(CurMultiClass); | ||||
2769 | if (!Name) | ||||
2770 | return true; | ||||
2771 | |||||
2772 | if (isa<UnsetInit>(Name)) | ||||
2773 | CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records, | ||||
2774 | /*Anonymous=*/true); | ||||
2775 | else | ||||
2776 | CurRec = std::make_unique<Record>(Name, DefLoc, Records); | ||||
2777 | |||||
2778 | if (ParseObjectBody(CurRec.get())) | ||||
2779 | return true; | ||||
2780 | |||||
2781 | return addEntry(std::move(CurRec)); | ||||
2782 | } | ||||
2783 | |||||
2784 | /// ParseDefset - Parse a defset statement. | ||||
2785 | /// | ||||
2786 | /// Defset ::= DEFSET Type Id '=' '{' ObjectList '}' | ||||
2787 | /// | ||||
2788 | bool TGParser::ParseDefset() { | ||||
2789 | assert(Lex.getCode() == tgtok::Defset)((Lex.getCode() == tgtok::Defset) ? static_cast<void> ( 0) : __assert_fail ("Lex.getCode() == tgtok::Defset", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2789, __PRETTY_FUNCTION__)); | ||||
2790 | Lex.Lex(); // Eat the 'defset' token | ||||
2791 | |||||
2792 | DefsetRecord Defset; | ||||
2793 | Defset.Loc = Lex.getLoc(); | ||||
2794 | RecTy *Type = ParseType(); | ||||
2795 | if (!Type) | ||||
2796 | return true; | ||||
2797 | if (!isa<ListRecTy>(Type)) | ||||
2798 | return Error(Defset.Loc, "expected list type"); | ||||
2799 | Defset.EltTy = cast<ListRecTy>(Type)->getElementType(); | ||||
2800 | |||||
2801 | if (Lex.getCode() != tgtok::Id) | ||||
2802 | return TokError("expected identifier"); | ||||
2803 | StringInit *DeclName = StringInit::get(Lex.getCurStrVal()); | ||||
2804 | if (Records.getGlobal(DeclName->getValue())) | ||||
2805 | return TokError("def or global variable of this name already exists"); | ||||
2806 | |||||
2807 | if (Lex.Lex() != tgtok::equal) // Eat the identifier | ||||
2808 | return TokError("expected '='"); | ||||
2809 | if (Lex.Lex() != tgtok::l_brace) // Eat the '=' | ||||
2810 | return TokError("expected '{'"); | ||||
2811 | SMLoc BraceLoc = Lex.getLoc(); | ||||
2812 | Lex.Lex(); // Eat the '{' | ||||
2813 | |||||
2814 | Defsets.push_back(&Defset); | ||||
2815 | bool Err = ParseObjectList(nullptr); | ||||
2816 | Defsets.pop_back(); | ||||
2817 | if (Err) | ||||
2818 | return true; | ||||
2819 | |||||
2820 | if (Lex.getCode() != tgtok::r_brace) { | ||||
2821 | TokError("expected '}' at end of defset"); | ||||
2822 | return Error(BraceLoc, "to match this '{'"); | ||||
2823 | } | ||||
2824 | Lex.Lex(); // Eat the '}' | ||||
2825 | |||||
2826 | Records.addExtraGlobal(DeclName->getValue(), | ||||
2827 | ListInit::get(Defset.Elements, Defset.EltTy)); | ||||
2828 | return false; | ||||
2829 | } | ||||
2830 | |||||
2831 | /// ParseDefvar - Parse a defvar statement. | ||||
2832 | /// | ||||
2833 | /// Defvar ::= DEFVAR Id '=' Value ';' | ||||
2834 | /// | ||||
2835 | bool TGParser::ParseDefvar() { | ||||
2836 | assert(Lex.getCode() == tgtok::Defvar)((Lex.getCode() == tgtok::Defvar) ? static_cast<void> ( 0) : __assert_fail ("Lex.getCode() == tgtok::Defvar", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2836, __PRETTY_FUNCTION__)); | ||||
2837 | Lex.Lex(); // Eat the 'defvar' token | ||||
2838 | |||||
2839 | if (Lex.getCode() != tgtok::Id) | ||||
2840 | return TokError("expected identifier"); | ||||
2841 | StringInit *DeclName = StringInit::get(Lex.getCurStrVal()); | ||||
2842 | if (CurLocalScope) { | ||||
2843 | if (CurLocalScope->varAlreadyDefined(DeclName->getValue())) | ||||
2844 | return TokError("local variable of this name already exists"); | ||||
2845 | } else { | ||||
2846 | if (Records.getGlobal(DeclName->getValue())) | ||||
2847 | return TokError("def or global variable of this name already exists"); | ||||
2848 | } | ||||
2849 | |||||
2850 | if (Lex.Lex() != tgtok::equal) // Eat the identifier | ||||
2851 | return TokError("expected '='"); | ||||
2852 | Lex.Lex(); // Eat the '=' | ||||
2853 | |||||
2854 | Init *Value = ParseValue(nullptr); | ||||
2855 | if (!Value) | ||||
2856 | return true; | ||||
2857 | |||||
2858 | if (Lex.getCode() != tgtok::semi) | ||||
2859 | return TokError("expected ';'"); | ||||
2860 | Lex.Lex(); // Eat the ';' | ||||
2861 | |||||
2862 | if (CurLocalScope) | ||||
2863 | CurLocalScope->addVar(DeclName->getValue(), Value); | ||||
2864 | else | ||||
2865 | Records.addExtraGlobal(DeclName->getValue(), Value); | ||||
2866 | |||||
2867 | return false; | ||||
2868 | } | ||||
2869 | |||||
2870 | /// ParseForeach - Parse a for statement. Return the record corresponding | ||||
2871 | /// to it. This returns true on error. | ||||
2872 | /// | ||||
2873 | /// Foreach ::= FOREACH Declaration IN '{ ObjectList '}' | ||||
2874 | /// Foreach ::= FOREACH Declaration IN Object | ||||
2875 | /// | ||||
2876 | bool TGParser::ParseForeach(MultiClass *CurMultiClass) { | ||||
2877 | SMLoc Loc = Lex.getLoc(); | ||||
2878 | assert(Lex.getCode() == tgtok::Foreach && "Unknown tok")((Lex.getCode() == tgtok::Foreach && "Unknown tok") ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::Foreach && \"Unknown tok\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2878, __PRETTY_FUNCTION__)); | ||||
2879 | Lex.Lex(); // Eat the 'for' token. | ||||
2880 | |||||
2881 | // Make a temporary object to record items associated with the for | ||||
2882 | // loop. | ||||
2883 | Init *ListValue = nullptr; | ||||
2884 | VarInit *IterName = ParseForeachDeclaration(ListValue); | ||||
2885 | if (!IterName) | ||||
2886 | return TokError("expected declaration in for"); | ||||
2887 | |||||
2888 | if (Lex.getCode() != tgtok::In) | ||||
2889 | return TokError("Unknown tok"); | ||||
2890 | Lex.Lex(); // Eat the in | ||||
2891 | |||||
2892 | // Create a loop object and remember it. | ||||
2893 | Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue)); | ||||
2894 | |||||
2895 | // A foreach loop introduces a new scope for local variables. | ||||
2896 | TGLocalVarScope *ForeachScope = PushLocalScope(); | ||||
2897 | |||||
2898 | if (Lex.getCode() != tgtok::l_brace) { | ||||
2899 | // FOREACH Declaration IN Object | ||||
2900 | if (ParseObject(CurMultiClass)) | ||||
2901 | return true; | ||||
2902 | } else { | ||||
2903 | SMLoc BraceLoc = Lex.getLoc(); | ||||
2904 | // Otherwise, this is a group foreach. | ||||
2905 | Lex.Lex(); // eat the '{'. | ||||
2906 | |||||
2907 | // Parse the object list. | ||||
2908 | if (ParseObjectList(CurMultiClass)) | ||||
2909 | return true; | ||||
2910 | |||||
2911 | if (Lex.getCode() != tgtok::r_brace) { | ||||
2912 | TokError("expected '}' at end of foreach command"); | ||||
2913 | return Error(BraceLoc, "to match this '{'"); | ||||
2914 | } | ||||
2915 | Lex.Lex(); // Eat the } | ||||
2916 | } | ||||
2917 | |||||
2918 | PopLocalScope(ForeachScope); | ||||
2919 | |||||
2920 | // Resolve the loop or store it for later resolution. | ||||
2921 | std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back()); | ||||
2922 | Loops.pop_back(); | ||||
2923 | |||||
2924 | return addEntry(std::move(Loop)); | ||||
2925 | } | ||||
2926 | |||||
2927 | /// ParseIf - Parse an if statement. | ||||
2928 | /// | ||||
2929 | /// If ::= IF Value THEN IfBody | ||||
2930 | /// If ::= IF Value THEN IfBody ELSE IfBody | ||||
2931 | /// | ||||
2932 | bool TGParser::ParseIf(MultiClass *CurMultiClass) { | ||||
2933 | SMLoc Loc = Lex.getLoc(); | ||||
2934 | assert(Lex.getCode() == tgtok::If && "Unknown tok")((Lex.getCode() == tgtok::If && "Unknown tok") ? static_cast <void> (0) : __assert_fail ("Lex.getCode() == tgtok::If && \"Unknown tok\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 2934, __PRETTY_FUNCTION__)); | ||||
2935 | Lex.Lex(); // Eat the 'if' token. | ||||
2936 | |||||
2937 | // Make a temporary object to record items associated with the for | ||||
2938 | // loop. | ||||
2939 | Init *Condition = ParseValue(nullptr); | ||||
2940 | if (!Condition) | ||||
2941 | return true; | ||||
2942 | |||||
2943 | if (Lex.getCode() != tgtok::Then) | ||||
2944 | return TokError("Unknown tok"); | ||||
2945 | Lex.Lex(); // Eat the 'then' | ||||
2946 | |||||
2947 | // We have to be able to save if statements to execute later, and they have | ||||
2948 | // to live on the same stack as foreach loops. The simplest implementation | ||||
2949 | // technique is to convert each 'then' or 'else' clause *into* a foreach | ||||
2950 | // loop, over a list of length 0 or 1 depending on the condition, and with no | ||||
2951 | // iteration variable being assigned. | ||||
2952 | |||||
2953 | ListInit *EmptyList = ListInit::get({}, BitRecTy::get()); | ||||
2954 | ListInit *SingletonList = ListInit::get({BitInit::get(1)}, BitRecTy::get()); | ||||
2955 | RecTy *BitListTy = ListRecTy::get(BitRecTy::get()); | ||||
2956 | |||||
2957 | // The foreach containing the then-clause selects SingletonList if | ||||
2958 | // the condition is true. | ||||
2959 | Init *ThenClauseList = | ||||
2960 | TernOpInit::get(TernOpInit::IF, Condition, SingletonList, EmptyList, | ||||
2961 | BitListTy) | ||||
2962 | ->Fold(nullptr); | ||||
2963 | Loops.push_back(std::make_unique<ForeachLoop>(Loc, nullptr, ThenClauseList)); | ||||
2964 | |||||
2965 | if (ParseIfBody(CurMultiClass, "then")) | ||||
2966 | return true; | ||||
2967 | |||||
2968 | std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back()); | ||||
2969 | Loops.pop_back(); | ||||
2970 | |||||
2971 | if (addEntry(std::move(Loop))) | ||||
2972 | return true; | ||||
2973 | |||||
2974 | // Now look for an optional else clause. The if-else syntax has the usual | ||||
2975 | // dangling-else ambiguity, and by greedily matching an else here if we can, | ||||
2976 | // we implement the usual resolution of pairing with the innermost unmatched | ||||
2977 | // if. | ||||
2978 | if (Lex.getCode() == tgtok::ElseKW) { | ||||
2979 | Lex.Lex(); // Eat the 'else' | ||||
2980 | |||||
2981 | // The foreach containing the else-clause uses the same pair of lists as | ||||
2982 | // above, but this time, selects SingletonList if the condition is *false*. | ||||
2983 | Init *ElseClauseList = | ||||
2984 | TernOpInit::get(TernOpInit::IF, Condition, EmptyList, SingletonList, | ||||
2985 | BitListTy) | ||||
2986 | ->Fold(nullptr); | ||||
2987 | Loops.push_back( | ||||
2988 | std::make_unique<ForeachLoop>(Loc, nullptr, ElseClauseList)); | ||||
2989 | |||||
2990 | if (ParseIfBody(CurMultiClass, "else")) | ||||
2991 | return true; | ||||
2992 | |||||
2993 | Loop = std::move(Loops.back()); | ||||
2994 | Loops.pop_back(); | ||||
2995 | |||||
2996 | if (addEntry(std::move(Loop))) | ||||
2997 | return true; | ||||
2998 | } | ||||
2999 | |||||
3000 | return false; | ||||
3001 | } | ||||
3002 | |||||
3003 | /// ParseIfBody - Parse the then-clause or else-clause of an if statement. | ||||
3004 | /// | ||||
3005 | /// IfBody ::= Object | ||||
3006 | /// IfBody ::= '{' ObjectList '}' | ||||
3007 | /// | ||||
3008 | bool TGParser::ParseIfBody(MultiClass *CurMultiClass, StringRef Kind) { | ||||
3009 | TGLocalVarScope *BodyScope = PushLocalScope(); | ||||
3010 | |||||
3011 | if (Lex.getCode() != tgtok::l_brace) { | ||||
3012 | // A single object. | ||||
3013 | if (ParseObject(CurMultiClass)) | ||||
3014 | return true; | ||||
3015 | } else { | ||||
3016 | SMLoc BraceLoc = Lex.getLoc(); | ||||
3017 | // A braced block. | ||||
3018 | Lex.Lex(); // eat the '{'. | ||||
3019 | |||||
3020 | // Parse the object list. | ||||
3021 | if (ParseObjectList(CurMultiClass)) | ||||
3022 | return true; | ||||
3023 | |||||
3024 | if (Lex.getCode() != tgtok::r_brace) { | ||||
3025 | TokError("expected '}' at end of '" + Kind + "' clause"); | ||||
3026 | return Error(BraceLoc, "to match this '{'"); | ||||
3027 | } | ||||
3028 | |||||
3029 | Lex.Lex(); // Eat the } | ||||
3030 | } | ||||
3031 | |||||
3032 | PopLocalScope(BodyScope); | ||||
3033 | return false; | ||||
3034 | } | ||||
3035 | |||||
3036 | /// ParseClass - Parse a tblgen class definition. | ||||
3037 | /// | ||||
3038 | /// ClassInst ::= CLASS ID TemplateArgList? ObjectBody | ||||
3039 | /// | ||||
3040 | bool TGParser::ParseClass() { | ||||
3041 | assert(Lex.getCode() == tgtok::Class && "Unexpected token!")((Lex.getCode() == tgtok::Class && "Unexpected token!" ) ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::Class && \"Unexpected token!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 3041, __PRETTY_FUNCTION__)); | ||||
3042 | Lex.Lex(); | ||||
3043 | |||||
3044 | if (Lex.getCode() != tgtok::Id) | ||||
3045 | return TokError("expected class name after 'class' keyword"); | ||||
3046 | |||||
3047 | Record *CurRec = Records.getClass(Lex.getCurStrVal()); | ||||
3048 | if (CurRec) { | ||||
3049 | // If the body was previously defined, this is an error. | ||||
3050 | if (!CurRec->getValues().empty() || | ||||
3051 | !CurRec->getSuperClasses().empty() || | ||||
3052 | !CurRec->getTemplateArgs().empty()) | ||||
3053 | return TokError("Class '" + CurRec->getNameInitAsString() + | ||||
3054 | "' already defined"); | ||||
3055 | } else { | ||||
3056 | // If this is the first reference to this class, create and add it. | ||||
3057 | auto NewRec = | ||||
3058 | std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records, | ||||
3059 | /*Class=*/true); | ||||
3060 | CurRec = NewRec.get(); | ||||
3061 | Records.addClass(std::move(NewRec)); | ||||
3062 | } | ||||
3063 | Lex.Lex(); // eat the name. | ||||
3064 | |||||
3065 | // If there are template args, parse them. | ||||
3066 | if (Lex.getCode() == tgtok::less) | ||||
3067 | if (ParseTemplateArgList(CurRec)) | ||||
3068 | return true; | ||||
3069 | |||||
3070 | return ParseObjectBody(CurRec); | ||||
3071 | } | ||||
3072 | |||||
3073 | /// ParseLetList - Parse a non-empty list of assignment expressions into a list | ||||
3074 | /// of LetRecords. | ||||
3075 | /// | ||||
3076 | /// LetList ::= LetItem (',' LetItem)* | ||||
3077 | /// LetItem ::= ID OptionalRangeList '=' Value | ||||
3078 | /// | ||||
3079 | void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) { | ||||
3080 | while (true) { | ||||
3081 | if (Lex.getCode() != tgtok::Id) { | ||||
3082 | TokError("expected identifier in let definition"); | ||||
3083 | Result.clear(); | ||||
3084 | return; | ||||
3085 | } | ||||
3086 | |||||
3087 | StringInit *Name = StringInit::get(Lex.getCurStrVal()); | ||||
3088 | SMLoc NameLoc = Lex.getLoc(); | ||||
3089 | Lex.Lex(); // Eat the identifier. | ||||
3090 | |||||
3091 | // Check for an optional RangeList. | ||||
3092 | SmallVector<unsigned, 16> Bits; | ||||
3093 | if (ParseOptionalRangeList(Bits)) { | ||||
3094 | Result.clear(); | ||||
3095 | return; | ||||
3096 | } | ||||
3097 | std::reverse(Bits.begin(), Bits.end()); | ||||
3098 | |||||
3099 | if (Lex.getCode() != tgtok::equal) { | ||||
3100 | TokError("expected '=' in let expression"); | ||||
3101 | Result.clear(); | ||||
3102 | return; | ||||
3103 | } | ||||
3104 | Lex.Lex(); // eat the '='. | ||||
3105 | |||||
3106 | Init *Val = ParseValue(nullptr); | ||||
3107 | if (!Val) { | ||||
3108 | Result.clear(); | ||||
3109 | return; | ||||
3110 | } | ||||
3111 | |||||
3112 | // Now that we have everything, add the record. | ||||
3113 | Result.emplace_back(Name, Bits, Val, NameLoc); | ||||
3114 | |||||
3115 | if (Lex.getCode() != tgtok::comma) | ||||
3116 | return; | ||||
3117 | Lex.Lex(); // eat the comma. | ||||
3118 | } | ||||
3119 | } | ||||
3120 | |||||
3121 | /// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of | ||||
3122 | /// different related productions. This works inside multiclasses too. | ||||
3123 | /// | ||||
3124 | /// Object ::= LET LetList IN '{' ObjectList '}' | ||||
3125 | /// Object ::= LET LetList IN Object | ||||
3126 | /// | ||||
3127 | bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { | ||||
3128 | assert(Lex.getCode() == tgtok::Let && "Unexpected token")((Lex.getCode() == tgtok::Let && "Unexpected token") ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::Let && \"Unexpected token\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 3128, __PRETTY_FUNCTION__)); | ||||
3129 | Lex.Lex(); | ||||
3130 | |||||
3131 | // Add this entry to the let stack. | ||||
3132 | SmallVector<LetRecord, 8> LetInfo; | ||||
3133 | ParseLetList(LetInfo); | ||||
3134 | if (LetInfo.empty()) return true; | ||||
3135 | LetStack.push_back(std::move(LetInfo)); | ||||
3136 | |||||
3137 | if (Lex.getCode() != tgtok::In) | ||||
3138 | return TokError("expected 'in' at end of top-level 'let'"); | ||||
3139 | Lex.Lex(); | ||||
3140 | |||||
3141 | TGLocalVarScope *LetScope = PushLocalScope(); | ||||
3142 | |||||
3143 | // If this is a scalar let, just handle it now | ||||
3144 | if (Lex.getCode() != tgtok::l_brace) { | ||||
3145 | // LET LetList IN Object | ||||
3146 | if (ParseObject(CurMultiClass)) | ||||
3147 | return true; | ||||
3148 | } else { // Object ::= LETCommand '{' ObjectList '}' | ||||
3149 | SMLoc BraceLoc = Lex.getLoc(); | ||||
3150 | // Otherwise, this is a group let. | ||||
3151 | Lex.Lex(); // eat the '{'. | ||||
3152 | |||||
3153 | // Parse the object list. | ||||
3154 | if (ParseObjectList(CurMultiClass)) | ||||
3155 | return true; | ||||
3156 | |||||
3157 | if (Lex.getCode() != tgtok::r_brace) { | ||||
3158 | TokError("expected '}' at end of top level let command"); | ||||
3159 | return Error(BraceLoc, "to match this '{'"); | ||||
3160 | } | ||||
3161 | Lex.Lex(); | ||||
3162 | } | ||||
3163 | |||||
3164 | PopLocalScope(LetScope); | ||||
3165 | |||||
3166 | // Outside this let scope, this let block is not active. | ||||
3167 | LetStack.pop_back(); | ||||
3168 | return false; | ||||
3169 | } | ||||
3170 | |||||
3171 | /// ParseMultiClass - Parse a multiclass definition. | ||||
3172 | /// | ||||
3173 | /// MultiClassInst ::= MULTICLASS ID TemplateArgList? | ||||
3174 | /// ':' BaseMultiClassList '{' MultiClassObject+ '}' | ||||
3175 | /// MultiClassObject ::= DefInst | ||||
3176 | /// MultiClassObject ::= MultiClassInst | ||||
3177 | /// MultiClassObject ::= DefMInst | ||||
3178 | /// MultiClassObject ::= LETCommand '{' ObjectList '}' | ||||
3179 | /// MultiClassObject ::= LETCommand Object | ||||
3180 | /// | ||||
3181 | bool TGParser::ParseMultiClass() { | ||||
3182 | assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token")((Lex.getCode() == tgtok::MultiClass && "Unexpected token" ) ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::MultiClass && \"Unexpected token\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 3182, __PRETTY_FUNCTION__)); | ||||
3183 | Lex.Lex(); // Eat the multiclass token. | ||||
3184 | |||||
3185 | if (Lex.getCode() != tgtok::Id) | ||||
3186 | return TokError("expected identifier after multiclass for name"); | ||||
3187 | std::string Name = Lex.getCurStrVal(); | ||||
3188 | |||||
3189 | auto Result = | ||||
3190 | MultiClasses.insert(std::make_pair(Name, | ||||
3191 | std::make_unique<MultiClass>(Name, Lex.getLoc(),Records))); | ||||
3192 | |||||
3193 | if (!Result.second) | ||||
3194 | return TokError("multiclass '" + Name + "' already defined"); | ||||
3195 | |||||
3196 | CurMultiClass = Result.first->second.get(); | ||||
3197 | Lex.Lex(); // Eat the identifier. | ||||
3198 | |||||
3199 | // If there are template args, parse them. | ||||
3200 | if (Lex.getCode() == tgtok::less) | ||||
3201 | if (ParseTemplateArgList(nullptr)) | ||||
3202 | return true; | ||||
3203 | |||||
3204 | bool inherits = false; | ||||
3205 | |||||
3206 | // If there are submulticlasses, parse them. | ||||
3207 | if (Lex.getCode() == tgtok::colon) { | ||||
3208 | inherits = true; | ||||
3209 | |||||
3210 | Lex.Lex(); | ||||
3211 | |||||
3212 | // Read all of the submulticlasses. | ||||
3213 | SubMultiClassReference SubMultiClass = | ||||
3214 | ParseSubMultiClassReference(CurMultiClass); | ||||
3215 | while (true) { | ||||
3216 | // Check for error. | ||||
3217 | if (!SubMultiClass.MC) return true; | ||||
3218 | |||||
3219 | // Add it. | ||||
3220 | if (AddSubMultiClass(CurMultiClass, SubMultiClass)) | ||||
3221 | return true; | ||||
3222 | |||||
3223 | if (Lex.getCode() != tgtok::comma) break; | ||||
3224 | Lex.Lex(); // eat ','. | ||||
3225 | SubMultiClass = ParseSubMultiClassReference(CurMultiClass); | ||||
3226 | } | ||||
3227 | } | ||||
3228 | |||||
3229 | if (Lex.getCode() != tgtok::l_brace) { | ||||
3230 | if (!inherits) | ||||
3231 | return TokError("expected '{' in multiclass definition"); | ||||
3232 | if (Lex.getCode() != tgtok::semi) | ||||
3233 | return TokError("expected ';' in multiclass definition"); | ||||
3234 | Lex.Lex(); // eat the ';'. | ||||
3235 | } else { | ||||
3236 | if (Lex.Lex() == tgtok::r_brace) // eat the '{'. | ||||
3237 | return TokError("multiclass must contain at least one def"); | ||||
3238 | |||||
3239 | // A multiclass body introduces a new scope for local variables. | ||||
3240 | TGLocalVarScope *MulticlassScope = PushLocalScope(); | ||||
3241 | |||||
3242 | while (Lex.getCode() != tgtok::r_brace) { | ||||
3243 | switch (Lex.getCode()) { | ||||
3244 | default: | ||||
3245 | return TokError("expected 'let', 'def', 'defm', 'defvar', 'foreach' " | ||||
3246 | "or 'if' in multiclass body"); | ||||
3247 | case tgtok::Let: | ||||
3248 | case tgtok::Def: | ||||
3249 | case tgtok::Defm: | ||||
3250 | case tgtok::Defvar: | ||||
3251 | case tgtok::Foreach: | ||||
3252 | case tgtok::If: | ||||
3253 | if (ParseObject(CurMultiClass)) | ||||
3254 | return true; | ||||
3255 | break; | ||||
3256 | } | ||||
3257 | } | ||||
3258 | Lex.Lex(); // eat the '}'. | ||||
3259 | |||||
3260 | PopLocalScope(MulticlassScope); | ||||
3261 | } | ||||
3262 | |||||
3263 | CurMultiClass = nullptr; | ||||
3264 | return false; | ||||
3265 | } | ||||
3266 | |||||
3267 | /// ParseDefm - Parse the instantiation of a multiclass. | ||||
3268 | /// | ||||
3269 | /// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' | ||||
3270 | /// | ||||
3271 | bool TGParser::ParseDefm(MultiClass *CurMultiClass) { | ||||
3272 | assert(Lex.getCode() == tgtok::Defm && "Unexpected token!")((Lex.getCode() == tgtok::Defm && "Unexpected token!" ) ? static_cast<void> (0) : __assert_fail ("Lex.getCode() == tgtok::Defm && \"Unexpected token!\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 3272, __PRETTY_FUNCTION__)); | ||||
3273 | Lex.Lex(); // eat the defm | ||||
3274 | |||||
3275 | Init *DefmName = ParseObjectName(CurMultiClass); | ||||
3276 | if (!DefmName) | ||||
3277 | return true; | ||||
3278 | if (isa<UnsetInit>(DefmName)) { | ||||
3279 | DefmName = Records.getNewAnonymousName(); | ||||
3280 | if (CurMultiClass) | ||||
3281 | DefmName = BinOpInit::getStrConcat( | ||||
3282 | VarInit::get(QualifiedNameOfImplicitName(CurMultiClass), | ||||
3283 | StringRecTy::get()), | ||||
3284 | DefmName); | ||||
3285 | } | ||||
3286 | |||||
3287 | if (Lex.getCode() != tgtok::colon) | ||||
3288 | return TokError("expected ':' after defm identifier"); | ||||
3289 | |||||
3290 | // Keep track of the new generated record definitions. | ||||
3291 | std::vector<RecordsEntry> NewEntries; | ||||
3292 | |||||
3293 | // This record also inherits from a regular class (non-multiclass)? | ||||
3294 | bool InheritFromClass = false; | ||||
3295 | |||||
3296 | // eat the colon. | ||||
3297 | Lex.Lex(); | ||||
3298 | |||||
3299 | SMLoc SubClassLoc = Lex.getLoc(); | ||||
3300 | SubClassReference Ref = ParseSubClassReference(nullptr, true); | ||||
3301 | |||||
3302 | while (true) { | ||||
3303 | if (!Ref.Rec) return true; | ||||
3304 | |||||
3305 | // To instantiate a multiclass, we need to first get the multiclass, then | ||||
3306 | // instantiate each def contained in the multiclass with the SubClassRef | ||||
3307 | // template parameters. | ||||
3308 | MultiClass *MC = MultiClasses[std::string(Ref.Rec->getName())].get(); | ||||
3309 | assert(MC && "Didn't lookup multiclass correctly?")((MC && "Didn't lookup multiclass correctly?") ? static_cast <void> (0) : __assert_fail ("MC && \"Didn't lookup multiclass correctly?\"" , "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/lib/TableGen/TGParser.cpp" , 3309, __PRETTY_FUNCTION__)); | ||||
3310 | ArrayRef<Init*> TemplateVals = Ref.TemplateArgs; | ||||
3311 | |||||
3312 | // Verify that the correct number of template arguments were specified. | ||||
3313 | ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs(); | ||||
3314 | if (TArgs.size() < TemplateVals.size()) | ||||
3315 | return Error(SubClassLoc, | ||||
3316 | "more template args specified than multiclass expects"); | ||||
3317 | |||||
3318 | SubstStack Substs; | ||||
3319 | for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { | ||||
3320 | if (i < TemplateVals.size()) { | ||||
3321 | Substs.emplace_back(TArgs[i], TemplateVals[i]); | ||||
3322 | } else { | ||||
3323 | Init *Default = MC->Rec.getValue(TArgs[i])->getValue(); | ||||
3324 | if (!Default->isComplete()) { | ||||
3325 | return Error(SubClassLoc, | ||||
3326 | "value not specified for template argument #" + | ||||
3327 | Twine(i) + " (" + TArgs[i]->getAsUnquotedString() + | ||||
3328 | ") of multiclass '" + MC->Rec.getNameInitAsString() + | ||||
3329 | "'"); | ||||
3330 | } | ||||
3331 | Substs.emplace_back(TArgs[i], Default); | ||||
3332 | } | ||||
3333 | } | ||||
3334 | |||||
3335 | Substs.emplace_back(QualifiedNameOfImplicitName(MC), DefmName); | ||||
3336 | |||||
3337 | if (resolve(MC->Entries, Substs, CurMultiClass == nullptr, &NewEntries, | ||||
3338 | &SubClassLoc)) | ||||
3339 | return true; | ||||
3340 | |||||
3341 | if (Lex.getCode() != tgtok::comma) break; | ||||
3342 | Lex.Lex(); // eat ','. | ||||
3343 | |||||
3344 | if (Lex.getCode() != tgtok::Id) | ||||
3345 | return TokError("expected identifier"); | ||||
3346 | |||||
3347 | SubClassLoc = Lex.getLoc(); | ||||
3348 | |||||
3349 | // A defm can inherit from regular classes (non-multiclass) as | ||||
3350 | // long as they come in the end of the inheritance list. | ||||
3351 | InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr); | ||||
3352 | |||||
3353 | if (InheritFromClass) | ||||
3354 | break; | ||||
3355 | |||||
3356 | Ref = ParseSubClassReference(nullptr, true); | ||||
3357 | } | ||||
3358 | |||||
3359 | if (InheritFromClass) { | ||||
3360 | // Process all the classes to inherit as if they were part of a | ||||
3361 | // regular 'def' and inherit all record values. | ||||
3362 | SubClassReference SubClass = ParseSubClassReference(nullptr, false); | ||||
3363 | while (true) { | ||||
3364 | // Check for error. | ||||
3365 | if (!SubClass.Rec) return true; | ||||
3366 | |||||
3367 | // Get the expanded definition prototypes and teach them about | ||||
3368 | // the record values the current class to inherit has | ||||
3369 | for (auto &E : NewEntries) { | ||||
3370 | // Add it. | ||||
3371 | if (AddSubClass(E, SubClass)) | ||||
3372 | return true; | ||||
3373 | } | ||||
3374 | |||||
3375 | if (Lex.getCode() != tgtok::comma) break; | ||||
3376 | Lex.Lex(); // eat ','. | ||||
3377 | SubClass = ParseSubClassReference(nullptr, false); | ||||
3378 | } | ||||
3379 | } | ||||
3380 | |||||
3381 | for (auto &E : NewEntries) { | ||||
3382 | if (ApplyLetStack(E)) | ||||
3383 | return true; | ||||
3384 | |||||
3385 | addEntry(std::move(E)); | ||||
3386 | } | ||||
3387 | |||||
3388 | if (Lex.getCode() != tgtok::semi) | ||||
3389 | return TokError("expected ';' at end of defm"); | ||||
3390 | Lex.Lex(); | ||||
3391 | |||||
3392 | return false; | ||||
3393 | } | ||||
3394 | |||||
3395 | /// ParseObject | ||||
3396 | /// Object ::= ClassInst | ||||
3397 | /// Object ::= DefInst | ||||
3398 | /// Object ::= MultiClassInst | ||||
3399 | /// Object ::= DefMInst | ||||
3400 | /// Object ::= LETCommand '{' ObjectList '}' | ||||
3401 | /// Object ::= LETCommand Object | ||||
3402 | /// Object ::= Defset | ||||
3403 | /// Object ::= Defvar | ||||
3404 | bool TGParser::ParseObject(MultiClass *MC) { | ||||
3405 | switch (Lex.getCode()) { | ||||
3406 | default: | ||||
3407 | return TokError("Expected class, def, defm, defset, multiclass, let, " | ||||
3408 | "foreach or if"); | ||||
3409 | case tgtok::Let: return ParseTopLevelLet(MC); | ||||
3410 | case tgtok::Def: return ParseDef(MC); | ||||
3411 | case tgtok::Foreach: return ParseForeach(MC); | ||||
3412 | case tgtok::If: return ParseIf(MC); | ||||
3413 | case tgtok::Defm: return ParseDefm(MC); | ||||
3414 | case tgtok::Defset: | ||||
3415 | if (MC) | ||||
3416 | return TokError("defset is not allowed inside multiclass"); | ||||
3417 | return ParseDefset(); | ||||
3418 | case tgtok::Defvar: | ||||
3419 | return ParseDefvar(); | ||||
3420 | case tgtok::Class: | ||||
3421 | if (MC) | ||||
3422 | return TokError("class is not allowed inside multiclass"); | ||||
3423 | if (!Loops.empty()) | ||||
3424 | return TokError("class is not allowed inside foreach loop"); | ||||
3425 | return ParseClass(); | ||||
3426 | case tgtok::MultiClass: | ||||
3427 | if (!Loops.empty()) | ||||
3428 | return TokError("multiclass is not allowed inside foreach loop"); | ||||
3429 | return ParseMultiClass(); | ||||
3430 | } | ||||
3431 | } | ||||
3432 | |||||
3433 | /// ParseObjectList | ||||
3434 | /// ObjectList :== Object* | ||||
3435 | bool TGParser::ParseObjectList(MultiClass *MC) { | ||||
3436 | while (isObjectStart(Lex.getCode())) { | ||||
3437 | if (ParseObject(MC)) | ||||
3438 | return true; | ||||
3439 | } | ||||
3440 | return false; | ||||
3441 | } | ||||
3442 | |||||
3443 | bool TGParser::ParseFile() { | ||||
3444 | Lex.Lex(); // Prime the lexer. | ||||
3445 | if (ParseObjectList()) return true; | ||||
3446 | |||||
3447 | // If we have unread input at the end of the file, report it. | ||||
3448 | if (Lex.getCode() == tgtok::Eof) | ||||
3449 | return false; | ||||
3450 | |||||
3451 | return TokError("Unexpected input at top level"); | ||||
3452 | } | ||||
3453 | |||||
3454 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | ||||
3455 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void RecordsEntry::dump() const { | ||||
3456 | if (Loop) | ||||
3457 | Loop->dump(); | ||||
3458 | if (Rec) | ||||
3459 | Rec->dump(); | ||||
3460 | } | ||||
3461 | |||||
3462 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void ForeachLoop::dump() const { | ||||
3463 | errs() << "foreach " << IterVar->getAsString() << " = " | ||||
3464 | << ListValue->getAsString() << " in {\n"; | ||||
3465 | |||||
3466 | for (const auto &E : Entries) | ||||
3467 | E.dump(); | ||||
3468 | |||||
3469 | errs() << "}\n"; | ||||
3470 | } | ||||
3471 | |||||
3472 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void MultiClass::dump() const { | ||||
3473 | errs() << "Record:\n"; | ||||
3474 | Rec.dump(); | ||||
3475 | |||||
3476 | errs() << "Defs:\n"; | ||||
3477 | for (const auto &E : Entries) | ||||
3478 | E.dump(); | ||||
3479 | } | ||||
3480 | #endif |