blob: 7a0197e77a5a93022209156c9386b2a5faa94643 [file] [log] [blame]
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001//===- TGParser.cpp - Parser for TableGen Files ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Parser for TableGen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TGParser.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000015#include "llvm/ADT/None.h"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chris Lattnerf4127dd2007-11-22 20:49:04 +000018#include "llvm/ADT/StringExtras.h"
Nico Weber432a3882018-04-30 14:59:11 +000019#include "llvm/Config/llvm-config.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000020#include "llvm/Support/Casting.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/TableGen/Record.h"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000025#include <algorithm>
Eugene Zelenko33d7b762016-08-23 17:14:32 +000026#include <cassert>
27#include <cstdint>
28
Chris Lattnerf4127dd2007-11-22 20:49:04 +000029using namespace llvm;
30
31//===----------------------------------------------------------------------===//
32// Support Code for the Semantic Actions.
33//===----------------------------------------------------------------------===//
34
35namespace llvm {
Eugene Zelenko33d7b762016-08-23 17:14:32 +000036
Chris Lattnerf4127dd2007-11-22 20:49:04 +000037struct SubClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000038 SMRange RefRange;
Chris Lattnerf4127dd2007-11-22 20:49:04 +000039 Record *Rec;
Matthias Braunc66e7552016-12-05 06:41:54 +000040 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000041
Craig Topper011817a2014-04-09 04:50:04 +000042 SubClassReference() : Rec(nullptr) {}
David Greene7049e792009-04-24 16:55:41 +000043
Craig Topper011817a2014-04-09 04:50:04 +000044 bool isInvalid() const { return Rec == nullptr; }
Chris Lattnerf4127dd2007-11-22 20:49:04 +000045};
David Greene753ed8f2009-04-22 16:42:54 +000046
47struct SubMultiClassReference {
Jordan Rosef12e8a92013-01-10 18:50:11 +000048 SMRange RefRange;
David Greene753ed8f2009-04-22 16:42:54 +000049 MultiClass *MC;
Matthias Braunc66e7552016-12-05 06:41:54 +000050 SmallVector<Init*, 4> TemplateArgs;
Eugene Zelenko33d7b762016-08-23 17:14:32 +000051
Craig Topper011817a2014-04-09 04:50:04 +000052 SubMultiClassReference() : MC(nullptr) {}
Bob Wilson3d948162009-04-28 19:41:44 +000053
Craig Topper011817a2014-04-09 04:50:04 +000054 bool isInvalid() const { return MC == nullptr; }
David Greene7049e792009-04-24 16:55:41 +000055 void dump() const;
David Greene753ed8f2009-04-22 16:42:54 +000056};
David Greene7049e792009-04-24 16:55:41 +000057
Aaron Ballman615eb472017-10-15 14:32:27 +000058#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000059LLVM_DUMP_METHOD void SubMultiClassReference::dump() const {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000060 errs() << "Multiclass:\n";
Bob Wilson7248f862009-11-22 04:24:42 +000061
David Greene7049e792009-04-24 16:55:41 +000062 MC->dump();
Bob Wilson7248f862009-11-22 04:24:42 +000063
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000064 errs() << "Template args:\n";
Craig Toppera9642b42015-05-04 01:35:39 +000065 for (Init *TA : TemplateArgs)
Craig Toppereb4d7c62015-04-29 04:43:36 +000066 TA->dump();
David Greene7049e792009-04-24 16:55:41 +000067}
Matthias Braun25bcaba2017-01-28 02:47:46 +000068#endif
David Greene7049e792009-04-24 16:55:41 +000069
Chris Lattnerf4127dd2007-11-22 20:49:04 +000070} // end namespace llvm
71
Nicolai Haehnle0f529882018-03-06 13:48:47 +000072static bool checkBitsConcrete(Record &R, const RecordVal &RV) {
73 BitsInit *BV = cast<BitsInit>(RV.getValue());
74 for (unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
75 Init *Bit = BV->getBit(i);
76 bool IsReference = false;
77 if (auto VBI = dyn_cast<VarBitInit>(Bit)) {
78 if (auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
79 if (R.getValue(VI->getName()))
80 IsReference = true;
81 }
82 } else if (isa<VarInit>(Bit)) {
83 IsReference = true;
84 }
85 if (!(IsReference || Bit->isConcrete()))
86 return false;
87 }
88 return true;
89}
90
91static void checkConcrete(Record &R) {
92 for (const RecordVal &RV : R.getValues()) {
93 // HACK: Disable this check for variables declared with 'field'. This is
94 // done merely because existing targets have legitimate cases of
95 // non-concrete variables in helper defs. Ideally, we'd introduce a
96 // 'maybe' or 'optional' modifier instead of this.
97 if (RV.getPrefix())
98 continue;
99
100 if (Init *V = RV.getValue()) {
101 bool Ok = isa<BitsInit>(V) ? checkBitsConcrete(R, RV) : V->isConcrete();
102 if (!Ok) {
103 PrintError(R.getLoc(),
104 Twine("Initializer of '") + RV.getNameInitAsString() +
105 "' in '" + R.getNameInitAsString() +
106 "' could not be fully resolved: " +
107 RV.getValue()->getAsString());
108 }
109 }
110 }
111}
112
Chris Lattner526c8cb2009-06-21 03:39:35 +0000113bool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) {
Craig Topper011817a2014-04-09 04:50:04 +0000114 if (!CurRec)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000115 CurRec = &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +0000116
Jakob Stoklund Olesen9d1c5ee2012-01-13 03:16:35 +0000117 if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000118 // The value already exists in the class, treat this as a set.
119 if (ERV->setValue(RV.getValue()))
120 return Error(Loc, "New definition of '" + RV.getName() + "' of type '" +
121 RV.getType()->getAsString() + "' is incompatible with " +
Bob Wilson7248f862009-11-22 04:24:42 +0000122 "previous definition of type '" +
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000123 ERV->getType()->getAsString() + "'");
124 } else {
125 CurRec->addValue(RV);
126 }
127 return false;
128}
129
130/// SetValue -
131/// Return true on error, false on success.
David Greene3ca42122011-10-19 13:02:39 +0000132bool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName,
Craig Toppercfd81732016-01-04 03:15:08 +0000133 ArrayRef<unsigned> BitList, Init *V,
Craig Topper1e23ed92016-01-04 03:05:14 +0000134 bool AllowSelfAssignment) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000135 if (!V) return false;
136
Craig Topper011817a2014-04-09 04:50:04 +0000137 if (!CurRec) CurRec = &CurMultiClass->Rec;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000138
139 RecordVal *RV = CurRec->getValue(ValName);
Craig Topper011817a2014-04-09 04:50:04 +0000140 if (!RV)
Craig Topper85c07002015-04-30 05:54:22 +0000141 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
142 "' unknown!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000143
144 // Do not allow assignments like 'X = X'. This will just cause infinite loops
145 // in the resolution machinery.
146 if (BitList.empty())
Sean Silvafb509ed2012-10-10 20:24:43 +0000147 if (VarInit *VI = dyn_cast<VarInit>(V))
Craig Topper1e23ed92016-01-04 03:05:14 +0000148 if (VI->getNameInit() == ValName && !AllowSelfAssignment)
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000149 return Error(Loc, "Recursion / self-assignment forbidden");
Bob Wilson7248f862009-11-22 04:24:42 +0000150
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000151 // If we are assigning to a subset of the bits in the value... then we must be
152 // assigning to a field of BitsRecTy, which must have a BitsInit
153 // initializer.
154 //
155 if (!BitList.empty()) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000156 BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue());
Craig Topper011817a2014-04-09 04:50:04 +0000157 if (!CurVal)
Craig Topper85c07002015-04-30 05:54:22 +0000158 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
159 "' is not a bits type");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000160
161 // Convert the incoming value to a bits type of the appropriate size...
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000162 Init *BI = V->getCastTo(BitsRecTy::get(BitList.size()));
Craig Toppera9642b42015-05-04 01:35:39 +0000163 if (!BI)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000164 return Error(Loc, "Initializer is not compatible with bit range");
Bob Wilson7248f862009-11-22 04:24:42 +0000165
David Greeneaf8ee2c2011-07-29 22:43:06 +0000166 SmallVector<Init *, 16> NewBits(CurVal->getNumBits());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000167
168 // Loop over bits, assigning values as appropriate.
169 for (unsigned i = 0, e = BitList.size(); i != e; ++i) {
170 unsigned Bit = BitList[i];
David Greeneb3da8122011-07-29 19:07:00 +0000171 if (NewBits[Bit])
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000172 return Error(Loc, "Cannot set bit #" + Twine(Bit) + " of value '" +
David Greene3ca42122011-10-19 13:02:39 +0000173 ValName->getAsUnquotedString() + "' more than once");
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +0000174 NewBits[Bit] = BI->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000175 }
176
177 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
Craig Topper011817a2014-04-09 04:50:04 +0000178 if (!NewBits[i])
David Greeneb3da8122011-07-29 19:07:00 +0000179 NewBits[i] = CurVal->getBit(i);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000180
David Greenee32ebf22011-07-29 19:07:07 +0000181 V = BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000182 }
183
Pete Cooper040c6a62014-07-31 01:43:57 +0000184 if (RV->setValue(V)) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +0000185 std::string InitType;
Craig Toppera9642b42015-05-04 01:35:39 +0000186 if (BitsInit *BI = dyn_cast<BitsInit>(V))
Pete Cooper040c6a62014-07-31 01:43:57 +0000187 InitType = (Twine("' of type bit initializer with length ") +
188 Twine(BI->getNumBits())).str();
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000189 else if (TypedInit *TI = dyn_cast<TypedInit>(V))
190 InitType = (Twine("' of type '") + TI->getType()->getAsString()).str();
Craig Topper85c07002015-04-30 05:54:22 +0000191 return Error(Loc, "Value '" + ValName->getAsUnquotedString() +
Nicolai Haehnlef19083d2018-02-22 15:26:21 +0000192 "' of type '" + RV->getType()->getAsString() +
193 "' is incompatible with initializer '" +
194 V->getAsString() + InitType + "'");
Pete Cooper040c6a62014-07-31 01:43:57 +0000195 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000196 return false;
197}
198
199/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template
200/// args as SubClass's template arguments.
Cedric Venetd1e179d2009-02-14 16:06:42 +0000201bool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000202 Record *SC = SubClass.Rec;
203 // Add all of the values in the subclass into the current class.
Craig Topper8eb764c2015-06-02 06:19:28 +0000204 for (const RecordVal &Val : SC->getValues())
205 if (AddValue(CurRec, SubClass.RefRange.Start, Val))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000206 return true;
207
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000208 ArrayRef<Init *> TArgs = SC->getTemplateArgs();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000209
210 // Ensure that an appropriate number of template arguments are specified.
211 if (TArgs.size() < SubClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000212 return Error(SubClass.RefRange.Start,
213 "More template args specified than expected");
Bob Wilson7248f862009-11-22 04:24:42 +0000214
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000215 // Loop over all of the template arguments, setting them to the specified
216 // value or leaving them as the default if necessary.
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000217 MapResolver R(CurRec);
218
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000219 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
220 if (i < SubClass.TemplateArgs.size()) {
221 // If a value is specified for this template arg, set it now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000222 if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000223 None, SubClass.TemplateArgs[i]))
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000224 return true;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000225 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000226 return Error(SubClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000227 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000228 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000229 ") of subclass '" + SC->getNameInitAsString() + "'!");
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000230 }
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000231
232 R.set(TArgs[i], CurRec->getValue(TArgs[i])->getValue());
233
234 CurRec->removeValue(TArgs[i]);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000235 }
236
Nicolai Haehnleb0cf9e92018-03-05 15:21:11 +0000237 CurRec->resolveReferences(R);
238
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000239 // Since everything went well, we can now set the "superclass" list for the
240 // current record.
Craig Topper0e41d0b2016-01-18 19:52:37 +0000241 ArrayRef<std::pair<Record *, SMRange>> SCs = SC->getSuperClasses();
242 for (const auto &SCPair : SCs) {
243 if (CurRec->isSubClassOf(SCPair.first))
Jordan Rosef12e8a92013-01-10 18:50:11 +0000244 return Error(SubClass.RefRange.Start,
Craig Topper0e41d0b2016-01-18 19:52:37 +0000245 "Already subclass of '" + SCPair.first->getName() + "'!\n");
246 CurRec->addSuperClass(SCPair.first, SCPair.second);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000247 }
Nicolai Haehnlec10570f2018-02-23 11:31:49 +0000248
249 if (CurRec->isSubClassOf(SC))
250 return Error(SubClass.RefRange.Start,
251 "Already subclass of '" + SC->getName() + "'!\n");
252 CurRec->addSuperClass(SC, SubClass.RefRange);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000253 return false;
254}
255
David Greene753ed8f2009-04-22 16:42:54 +0000256/// AddSubMultiClass - Add SubMultiClass as a subclass to
Bob Wilsonf71e6562009-04-30 18:26:19 +0000257/// CurMC, resolving its template args as SubMultiClass's
David Greene753ed8f2009-04-22 16:42:54 +0000258/// template arguments.
Bob Wilsonf71e6562009-04-30 18:26:19 +0000259bool TGParser::AddSubMultiClass(MultiClass *CurMC,
Bob Wilson92ab8202009-04-30 17:46:20 +0000260 SubMultiClassReference &SubMultiClass) {
David Greene753ed8f2009-04-22 16:42:54 +0000261 MultiClass *SMC = SubMultiClass.MC;
Bob Wilsonf71e6562009-04-30 18:26:19 +0000262 Record *CurRec = &CurMC->Rec;
David Greene753ed8f2009-04-22 16:42:54 +0000263
David Greene753ed8f2009-04-22 16:42:54 +0000264 // Add all of the values in the subclass into the current class.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000265 for (const auto &SMCVal : SMC->Rec.getValues())
266 if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000267 return true;
268
Craig Toppera4ea4b02014-11-30 00:24:32 +0000269 unsigned newDefStart = CurMC->DefPrototypes.size();
David Greene7049e792009-04-24 16:55:41 +0000270
David Greene753ed8f2009-04-22 16:42:54 +0000271 // Add all of the defs in the subclass into the current multiclass.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000272 for (const std::unique_ptr<Record> &R : SMC->DefPrototypes) {
David Greene753ed8f2009-04-22 16:42:54 +0000273 // Clone the def and add it to the current multiclass
Craig Toppereb4d7c62015-04-29 04:43:36 +0000274 auto NewDef = make_unique<Record>(*R);
David Greene753ed8f2009-04-22 16:42:54 +0000275
276 // Add all of the values in the superclass into the current def.
Craig Toppereb4d7c62015-04-29 04:43:36 +0000277 for (const auto &MCVal : CurRec->getValues())
278 if (AddValue(NewDef.get(), SubMultiClass.RefRange.Start, MCVal))
David Greene753ed8f2009-04-22 16:42:54 +0000279 return true;
280
Craig Topperc3504c42014-12-11 05:25:33 +0000281 CurMC->DefPrototypes.push_back(std::move(NewDef));
David Greene753ed8f2009-04-22 16:42:54 +0000282 }
Bob Wilson3d948162009-04-28 19:41:44 +0000283
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +0000284 ArrayRef<Init *> SMCTArgs = SMC->Rec.getTemplateArgs();
David Greene753ed8f2009-04-22 16:42:54 +0000285
David Greene7049e792009-04-24 16:55:41 +0000286 // Ensure that an appropriate number of template arguments are
287 // specified.
David Greene753ed8f2009-04-22 16:42:54 +0000288 if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size())
Jordan Rosef12e8a92013-01-10 18:50:11 +0000289 return Error(SubMultiClass.RefRange.Start,
David Greene7049e792009-04-24 16:55:41 +0000290 "More template args specified than expected");
Bob Wilson3d948162009-04-28 19:41:44 +0000291
David Greene753ed8f2009-04-22 16:42:54 +0000292 // Loop over all of the template arguments, setting them to the specified
293 // value or leaving them as the default if necessary.
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000294 MapResolver CurRecResolver(CurRec);
295
David Greene753ed8f2009-04-22 16:42:54 +0000296 for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
297 if (i < SubMultiClass.TemplateArgs.size()) {
David Greene7049e792009-04-24 16:55:41 +0000298 // If a value is specified for this template arg, set it in the
299 // superclass now.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000300 if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000301 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000302 return true;
303
David Greene7049e792009-04-24 16:55:41 +0000304 // If a value is specified for this template arg, set it in the
305 // new defs now.
Craig Topperc3504c42014-12-11 05:25:33 +0000306 for (const auto &Def :
307 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
308 if (SetValue(Def.get(), SubMultiClass.RefRange.Start, SMCTArgs[i],
Craig Toppercfd81732016-01-04 03:15:08 +0000309 None, SubMultiClass.TemplateArgs[i]))
David Greene753ed8f2009-04-22 16:42:54 +0000310 return true;
David Greene753ed8f2009-04-22 16:42:54 +0000311 }
312 } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) {
Jordan Rosef12e8a92013-01-10 18:50:11 +0000313 return Error(SubMultiClass.RefRange.Start,
Craig Topper85c07002015-04-30 05:54:22 +0000314 "Value not specified for template argument #" +
Benjamin Kramerdba7ee92015-05-28 11:24:24 +0000315 Twine(i) + " (" + SMCTArgs[i]->getAsUnquotedString() +
Craig Topper85c07002015-04-30 05:54:22 +0000316 ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!");
David Greene753ed8f2009-04-22 16:42:54 +0000317 }
Nicolai Haehnle1faf8682018-03-05 15:21:15 +0000318
319 CurRecResolver.set(SMCTArgs[i], CurRec->getValue(SMCTArgs[i])->getValue());
320
321 CurRec->removeValue(SMCTArgs[i]);
322 }
323
324 CurRec->resolveReferences(CurRecResolver);
325
326 for (const auto &Def :
327 makeArrayRef(CurMC->DefPrototypes).slice(newDefStart)) {
328 MapResolver R(Def.get());
329
330 for (Init *SMCTArg : SMCTArgs) {
331 R.set(SMCTArg, Def->getValue(SMCTArg)->getValue());
332 Def->removeValue(SMCTArg);
333 }
334
335 Def->resolveReferences(R);
David Greene753ed8f2009-04-22 16:42:54 +0000336 }
337
338 return false;
339}
340
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000341/// Add a record that results from 'def' or 'defm', after template arguments
342/// and the external let stack have been resolved.
343///
344/// Apply foreach loops, resolve internal variable references, and add to the
345/// current multi class or the global record keeper as appropriate.
346bool TGParser::addDef(std::unique_ptr<Record> Rec, Init *DefmName) {
David Greenefb927af2012-02-22 16:09:41 +0000347 IterSet IterVals;
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000348
349 if (Loops.empty())
350 return addDefOne(std::move(Rec), DefmName, IterVals);
351
352 return addDefForeach(Rec.get(), DefmName, IterVals);
David Greenefb927af2012-02-22 16:09:41 +0000353}
354
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000355/// Recursive helper function for addDef/addDefOne to resolve references to
356/// foreach variables.
357bool TGParser::addDefForeach(Record *Rec, Init *DefmName, IterSet &IterVals) {
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000358 if (IterVals.size() != Loops.size()) {
359 assert(IterVals.size() < Loops.size());
360 ForeachLoop &CurLoop = Loops[IterVals.size()];
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000361 ListInit *List = CurLoop.ListValue;
David Greenefb927af2012-02-22 16:09:41 +0000362
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000363 // Process each value.
Craig Topper664f6a02015-06-02 04:15:57 +0000364 for (unsigned i = 0; i < List->size(); ++i) {
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000365 IterVals.push_back(IterRecord(CurLoop.IterVar, List->getElement(i)));
366 if (addDefForeach(Rec, DefmName, IterVals))
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000367 return true;
368 IterVals.pop_back();
369 }
370 return false;
371 }
372
373 // This is the bottom of the recursion. We have all of the iterator values
374 // for this point in the iteration space. Instantiate a new record to
375 // reflect this combination of values.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000376 auto IterRec = make_unique<Record>(*Rec);
377 return addDefOne(std::move(IterRec), DefmName, IterVals);
378}
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000379
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000380/// After resolving foreach loops, add the record as a prototype to the
381/// current multiclass, or resolve fully and add to the record keeper.
382bool TGParser::addDefOne(std::unique_ptr<Record> Rec, Init *DefmName,
383 IterSet &IterVals) {
384 MapResolver R(Rec.get());
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000385
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000386 for (IterRecord &IR : IterVals)
387 R.set(IR.IterVar->getNameInit(), IR.IterValue);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000388
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000389 Rec->resolveReferences(R);
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000390
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000391 if (CurMultiClass) {
392 for (const auto &Proto : CurMultiClass->DefPrototypes) {
393 if (Proto->getNameInit() == Rec->getNameInit()) {
394 if (!Rec->isAnonymous()) {
395 PrintError(Rec->getLoc(),
396 Twine("def '") + Rec->getNameInitAsString() +
397 "' already defined in this multiclass!");
398 PrintNote(Proto->getLoc(), "location of previous definition");
399 return true;
400 }
401 Rec->setName(Records.getNewAnonymousName());
402 break;
403 }
404 }
405 CurMultiClass->DefPrototypes.emplace_back(std::move(Rec));
406 return false;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +0000407 }
408
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000409 // Name construction is an incoherent mess. Unfortunately, existing .td
410 // files rely on pretty much all the quirks and implementation details of
411 // this.
412 if (DefmName) {
413 MapResolver R(Rec.get());
414 R.set(StringInit::get("NAME"), DefmName);
415 Rec->resolveReferences(R);
David Greenefb927af2012-02-22 16:09:41 +0000416 }
417
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000418 if (Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
419 if (!Rec->isAnonymous()) {
420 PrintError(Rec->getLoc(),
421 "def already exists: " + Rec->getNameInitAsString());
422 PrintNote(Prev->getLoc(), "location of previous definition");
423 return true;
424 }
425 Rec->setName(Records.getNewAnonymousName());
426 }
427
428 Rec->resolveReferences();
429 checkConcrete(*Rec);
430
431 // If ObjectBody has template arguments, it's an error.
432 assert(Rec->getTemplateArgs().empty() && "How'd this get template args?");
433
434 for (DefsetRecord *Defset : Defsets) {
435 DefInit *I = Rec->getDefInit();
436 if (!I->getType()->typeIsA(Defset->EltTy)) {
437 PrintError(Rec->getLoc(), Twine("adding record of incompatible type '") +
438 I->getType()->getAsString() +
439 "' to defset");
440 PrintNote(Defset->Loc, "location of defset declaration");
441 return true;
442 }
443 Defset->Elements.push_back(I);
444 }
445
446 Records.addDef(std::move(Rec));
David Greenefb927af2012-02-22 16:09:41 +0000447 return false;
448}
449
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000450//===----------------------------------------------------------------------===//
451// Parser Code
452//===----------------------------------------------------------------------===//
453
454/// isObjectStart - Return true if this is a valid first token for an Object.
455static bool isObjectStart(tgtok::TokKind K) {
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000456 return K == tgtok::Class || K == tgtok::Def || K == tgtok::Defm ||
457 K == tgtok::Let || K == tgtok::MultiClass || K == tgtok::Foreach ||
458 K == tgtok::Defset;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000459}
460
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000461/// ParseObjectName - If a valid object name is specified, return it. If no
462/// name is specified, return the unset initializer. Return nullptr on parse
463/// error.
David Greene2affd672011-10-19 13:04:29 +0000464/// ObjectName ::= Value [ '#' Value ]*
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000465/// ObjectName ::= /*empty*/
466///
David Greene2affd672011-10-19 13:04:29 +0000467Init *TGParser::ParseObjectName(MultiClass *CurMultiClass) {
468 switch (Lex.getCode()) {
469 case tgtok::colon:
470 case tgtok::semi:
471 case tgtok::l_brace:
472 // These are all of the tokens that can begin an object body.
473 // Some of these can also begin values but we disallow those cases
474 // because they are unlikely to be useful.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000475 return UnsetInit::get();
David Greene2affd672011-10-19 13:04:29 +0000476 default:
477 break;
478 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +0000479
Craig Topper011817a2014-04-09 04:50:04 +0000480 Record *CurRec = nullptr;
David Greene2affd672011-10-19 13:04:29 +0000481 if (CurMultiClass)
482 CurRec = &CurMultiClass->Rec;
483
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000484 return ParseValue(CurRec, StringRecTy::get(), ParseNameMode);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000485}
486
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000487/// ParseClassID - Parse and resolve a reference to a class name. This returns
488/// null on error.
489///
490/// ClassID ::= ID
491///
492Record *TGParser::ParseClassID() {
493 if (Lex.getCode() != tgtok::Id) {
494 TokError("expected name for ClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000495 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000496 }
Bob Wilson7248f862009-11-22 04:24:42 +0000497
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000498 Record *Result = Records.getClass(Lex.getCurStrVal());
Craig Topper011817a2014-04-09 04:50:04 +0000499 if (!Result)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000500 TokError("Couldn't find class '" + Lex.getCurStrVal() + "'");
Bob Wilson7248f862009-11-22 04:24:42 +0000501
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000502 Lex.Lex();
503 return Result;
504}
505
Bob Wilson3d948162009-04-28 19:41:44 +0000506/// ParseMultiClassID - Parse and resolve a reference to a multiclass name.
507/// This returns null on error.
David Greene753ed8f2009-04-22 16:42:54 +0000508///
509/// MultiClassID ::= ID
510///
511MultiClass *TGParser::ParseMultiClassID() {
512 if (Lex.getCode() != tgtok::Id) {
Sean Silva710c3ae2013-01-09 02:11:57 +0000513 TokError("expected name for MultiClassID");
Craig Topper011817a2014-04-09 04:50:04 +0000514 return nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000515 }
Bob Wilson3d948162009-04-28 19:41:44 +0000516
Craig Topper7adf2bf2014-12-11 05:25:30 +0000517 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
Craig Topper4ca40012014-11-30 01:20:17 +0000518 if (!Result)
Sean Silva710c3ae2013-01-09 02:11:57 +0000519 TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'");
Bob Wilson3d948162009-04-28 19:41:44 +0000520
David Greene753ed8f2009-04-22 16:42:54 +0000521 Lex.Lex();
Craig Topper4ca40012014-11-30 01:20:17 +0000522 return Result;
David Greene753ed8f2009-04-22 16:42:54 +0000523}
524
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000525/// ParseSubClassReference - Parse a reference to a subclass or to a templated
526/// subclass. This returns a SubClassRefTy with a null Record* on error.
527///
528/// SubClassRef ::= ClassID
529/// SubClassRef ::= ClassID '<' ValueList '>'
530///
531SubClassReference TGParser::
532ParseSubClassReference(Record *CurRec, bool isDefm) {
533 SubClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000534 Result.RefRange.Start = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000535
Sean Silva0657b402013-01-09 02:17:14 +0000536 if (isDefm) {
537 if (MultiClass *MC = ParseMultiClassID())
538 Result.Rec = &MC->Rec;
539 } else {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000540 Result.Rec = ParseClassID();
Sean Silva0657b402013-01-09 02:17:14 +0000541 }
Craig Topper011817a2014-04-09 04:50:04 +0000542 if (!Result.Rec) return Result;
Bob Wilson7248f862009-11-22 04:24:42 +0000543
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000544 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000545 if (Lex.getCode() != tgtok::less) {
546 Result.RefRange.End = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000547 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000548 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000549 Lex.Lex(); // Eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000550
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000551 if (Lex.getCode() == tgtok::greater) {
552 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000553 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000554 return Result;
555 }
Bob Wilson7248f862009-11-22 04:24:42 +0000556
Matthias Braunc66e7552016-12-05 06:41:54 +0000557 ParseValueList(Result.TemplateArgs, CurRec, Result.Rec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000558 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000559 Result.Rec = nullptr; // Error parsing value list.
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000560 return Result;
561 }
Bob Wilson7248f862009-11-22 04:24:42 +0000562
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000563 if (Lex.getCode() != tgtok::greater) {
564 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000565 Result.Rec = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000566 return Result;
567 }
568 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000569 Result.RefRange.End = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +0000570
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000571 return Result;
572}
573
Bob Wilson3d948162009-04-28 19:41:44 +0000574/// ParseSubMultiClassReference - Parse a reference to a subclass or to a
575/// templated submulticlass. This returns a SubMultiClassRefTy with a null
576/// Record* on error.
David Greene753ed8f2009-04-22 16:42:54 +0000577///
578/// SubMultiClassRef ::= MultiClassID
579/// SubMultiClassRef ::= MultiClassID '<' ValueList '>'
580///
581SubMultiClassReference TGParser::
582ParseSubMultiClassReference(MultiClass *CurMC) {
583 SubMultiClassReference Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000584 Result.RefRange.Start = Lex.getLoc();
Bob Wilson3d948162009-04-28 19:41:44 +0000585
David Greene753ed8f2009-04-22 16:42:54 +0000586 Result.MC = ParseMultiClassID();
Craig Topper011817a2014-04-09 04:50:04 +0000587 if (!Result.MC) return Result;
Bob Wilson3d948162009-04-28 19:41:44 +0000588
David Greene753ed8f2009-04-22 16:42:54 +0000589 // If there is no template arg list, we're done.
Jordan Rosef12e8a92013-01-10 18:50:11 +0000590 if (Lex.getCode() != tgtok::less) {
591 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000592 return Result;
Jordan Rosef12e8a92013-01-10 18:50:11 +0000593 }
David Greene753ed8f2009-04-22 16:42:54 +0000594 Lex.Lex(); // Eat the '<'
Bob Wilson3d948162009-04-28 19:41:44 +0000595
David Greene753ed8f2009-04-22 16:42:54 +0000596 if (Lex.getCode() == tgtok::greater) {
597 TokError("subclass reference requires a non-empty list of template values");
Craig Topper011817a2014-04-09 04:50:04 +0000598 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000599 return Result;
600 }
Bob Wilson3d948162009-04-28 19:41:44 +0000601
Matthias Braunc66e7552016-12-05 06:41:54 +0000602 ParseValueList(Result.TemplateArgs, &CurMC->Rec, &Result.MC->Rec);
David Greene753ed8f2009-04-22 16:42:54 +0000603 if (Result.TemplateArgs.empty()) {
Craig Topper011817a2014-04-09 04:50:04 +0000604 Result.MC = nullptr; // Error parsing value list.
David Greene753ed8f2009-04-22 16:42:54 +0000605 return Result;
606 }
Bob Wilson3d948162009-04-28 19:41:44 +0000607
David Greene753ed8f2009-04-22 16:42:54 +0000608 if (Lex.getCode() != tgtok::greater) {
609 TokError("expected '>' in template value list");
Craig Topper011817a2014-04-09 04:50:04 +0000610 Result.MC = nullptr;
David Greene753ed8f2009-04-22 16:42:54 +0000611 return Result;
612 }
613 Lex.Lex();
Jordan Rosef12e8a92013-01-10 18:50:11 +0000614 Result.RefRange.End = Lex.getLoc();
David Greene753ed8f2009-04-22 16:42:54 +0000615
616 return Result;
617}
618
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000619/// ParseRangePiece - Parse a bit/value range.
620/// RangePiece ::= INTVAL
621/// RangePiece ::= INTVAL '-' INTVAL
622/// RangePiece ::= INTVAL INTVAL
Matthias Braunc66e7552016-12-05 06:41:54 +0000623bool TGParser::ParseRangePiece(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattner70ddafa2008-01-10 07:01:53 +0000624 if (Lex.getCode() != tgtok::IntVal) {
625 TokError("expected integer or bitrange");
626 return true;
627 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000628 int64_t Start = Lex.getCurIntVal();
629 int64_t End;
Bob Wilson7248f862009-11-22 04:24:42 +0000630
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000631 if (Start < 0)
632 return TokError("invalid range, cannot be negative");
Bob Wilson7248f862009-11-22 04:24:42 +0000633
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000634 switch (Lex.Lex()) { // eat first character.
Bob Wilson7248f862009-11-22 04:24:42 +0000635 default:
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000636 Ranges.push_back(Start);
637 return false;
638 case tgtok::minus:
639 if (Lex.Lex() != tgtok::IntVal) {
640 TokError("expected integer value as end of range");
641 return true;
642 }
643 End = Lex.getCurIntVal();
644 break;
645 case tgtok::IntVal:
646 End = -Lex.getCurIntVal();
647 break;
648 }
Bob Wilson7248f862009-11-22 04:24:42 +0000649 if (End < 0)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000650 return TokError("invalid range, cannot be negative");
651 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +0000652
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000653 // Add to the range.
Craig Toppera9642b42015-05-04 01:35:39 +0000654 if (Start < End)
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000655 for (; Start <= End; ++Start)
656 Ranges.push_back(Start);
Craig Toppera9642b42015-05-04 01:35:39 +0000657 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000658 for (; Start >= End; --Start)
659 Ranges.push_back(Start);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000660 return false;
661}
662
663/// ParseRangeList - Parse a list of scalars and ranges into scalar values.
664///
665/// RangeList ::= RangePiece (',' RangePiece)*
666///
Matthias Braunc66e7552016-12-05 06:41:54 +0000667void TGParser::ParseRangeList(SmallVectorImpl<unsigned> &Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000668 // Parse the first piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000669 if (ParseRangePiece(Result)) {
670 Result.clear();
671 return;
672 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000673 while (Lex.getCode() == tgtok::comma) {
674 Lex.Lex(); // Eat the comma.
675
676 // Parse the next range piece.
Matthias Braunc66e7552016-12-05 06:41:54 +0000677 if (ParseRangePiece(Result)) {
678 Result.clear();
679 return;
680 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000681 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000682}
683
684/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing.
685/// OptionalRangeList ::= '<' RangeList '>'
686/// OptionalRangeList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000687bool TGParser::ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000688 if (Lex.getCode() != tgtok::less)
689 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000690
Chris Lattner526c8cb2009-06-21 03:39:35 +0000691 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000692 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +0000693
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000694 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000695 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000696 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000697
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000698 if (Lex.getCode() != tgtok::greater) {
699 TokError("expected '>' at end of range list");
700 return Error(StartLoc, "to match this '<'");
701 }
702 Lex.Lex(); // eat the '>'.
703 return false;
704}
705
706/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing.
707/// OptionalBitList ::= '{' RangeList '}'
708/// OptionalBitList ::= /*empty*/
Matthias Braunc66e7552016-12-05 06:41:54 +0000709bool TGParser::ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000710 if (Lex.getCode() != tgtok::l_brace)
711 return false;
Bob Wilson7248f862009-11-22 04:24:42 +0000712
Chris Lattner526c8cb2009-06-21 03:39:35 +0000713 SMLoc StartLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000714 Lex.Lex(); // eat the '{'
Bob Wilson7248f862009-11-22 04:24:42 +0000715
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000716 // Parse the range list.
Matthias Braunc66e7552016-12-05 06:41:54 +0000717 ParseRangeList(Ranges);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000718 if (Ranges.empty()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +0000719
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000720 if (Lex.getCode() != tgtok::r_brace) {
721 TokError("expected '}' at end of bit list");
722 return Error(StartLoc, "to match this '{'");
723 }
724 Lex.Lex(); // eat the '}'.
725 return false;
726}
727
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000728/// ParseType - Parse and return a tblgen type. This returns null on error.
729///
730/// Type ::= STRING // string type
Jakob Stoklund Olesendd8fbf52012-01-13 03:38:34 +0000731/// Type ::= CODE // code type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000732/// Type ::= BIT // bit type
733/// Type ::= BITS '<' INTVAL '>' // bits<x> type
734/// Type ::= INT // int type
735/// Type ::= LIST '<' Type '>' // list<x> type
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000736/// Type ::= DAG // dag type
737/// Type ::= ClassID // Record Type
738///
739RecTy *TGParser::ParseType() {
740 switch (Lex.getCode()) {
Craig Topper011817a2014-04-09 04:50:04 +0000741 default: TokError("Unknown token when expecting a type"); return nullptr;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000742 case tgtok::String: Lex.Lex(); return StringRecTy::get();
Tim Northover88403d72016-07-05 21:22:55 +0000743 case tgtok::Code: Lex.Lex(); return CodeRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000744 case tgtok::Bit: Lex.Lex(); return BitRecTy::get();
745 case tgtok::Int: Lex.Lex(); return IntRecTy::get();
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000746 case tgtok::Dag: Lex.Lex(); return DagRecTy::get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000747 case tgtok::Id:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000748 if (Record *R = ParseClassID()) return RecordRecTy::get(R);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000749 TokError("unknown class name");
Craig Topper011817a2014-04-09 04:50:04 +0000750 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000751 case tgtok::Bits: {
752 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
753 TokError("expected '<' after bits type");
Craig Topper011817a2014-04-09 04:50:04 +0000754 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000755 }
756 if (Lex.Lex() != tgtok::IntVal) { // Eat '<'
757 TokError("expected integer in bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000758 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000759 }
Dan Gohmanca0546f2008-10-17 01:33:43 +0000760 uint64_t Val = Lex.getCurIntVal();
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000761 if (Lex.Lex() != tgtok::greater) { // Eat count.
762 TokError("expected '>' at end of bits<n> type");
Craig Topper011817a2014-04-09 04:50:04 +0000763 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000764 }
765 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000766 return BitsRecTy::get(Val);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000767 }
768 case tgtok::List: {
769 if (Lex.Lex() != tgtok::less) { // Eat 'bits'
770 TokError("expected '<' after list type");
Craig Topper011817a2014-04-09 04:50:04 +0000771 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000772 }
773 Lex.Lex(); // Eat '<'
774 RecTy *SubType = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +0000775 if (!SubType) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +0000776
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000777 if (Lex.getCode() != tgtok::greater) {
778 TokError("expected '>' at end of list<ty> type");
Craig Topper011817a2014-04-09 04:50:04 +0000779 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000780 }
781 Lex.Lex(); // Eat '>'
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000782 return ListRecTy::get(SubType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000783 }
Bob Wilson7248f862009-11-22 04:24:42 +0000784 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000785}
786
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000787/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID
788/// has already been read.
Matthias Braun215ff842016-12-05 07:35:13 +0000789Init *TGParser::ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greened4263a62011-10-19 13:04:20 +0000790 IDParseMode Mode) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000791 if (CurRec) {
792 if (const RecordVal *RV = CurRec->getValue(Name))
David Greenee32ebf22011-07-29 19:07:07 +0000793 return VarInit::get(Name, RV->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000794
Matthias Braun215ff842016-12-05 07:35:13 +0000795 Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":");
David Greenedb10e692011-10-19 13:02:42 +0000796
David Greene47a665e2011-10-05 22:42:54 +0000797 if (CurMultiClass)
Matthias Braun215ff842016-12-05 07:35:13 +0000798 TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name,
David Greenedb10e692011-10-19 13:02:42 +0000799 "::");
David Greene47a665e2011-10-05 22:42:54 +0000800
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000801 if (CurRec->isTemplateArg(TemplateArgName)) {
802 const RecordVal *RV = CurRec->getValue(TemplateArgName);
803 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000804 return VarInit::get(TemplateArgName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000805 }
Matthias Braun215ff842016-12-05 07:35:13 +0000806 }
Bob Wilson7248f862009-11-22 04:24:42 +0000807
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000808 if (CurMultiClass) {
Nicolai Haehnle3c80e4c2018-03-05 14:01:38 +0000809 if (Name->getValue() == "NAME")
810 return VarInit::get(Name, StringRecTy::get());
811
Matthias Braun215ff842016-12-05 07:35:13 +0000812 Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, "::");
David Greenedb10e692011-10-19 13:02:42 +0000813
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000814 if (CurMultiClass->Rec.isTemplateArg(MCName)) {
815 const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
816 assert(RV && "Template arg doesn't exist??");
David Greenee32ebf22011-07-29 19:07:07 +0000817 return VarInit::get(MCName, RV->getType());
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000818 }
819 }
Bob Wilson7248f862009-11-22 04:24:42 +0000820
David Greenefb927af2012-02-22 16:09:41 +0000821 // If this is in a foreach loop, make sure it's not a loop iterator
Craig Toppereb4d7c62015-04-29 04:43:36 +0000822 for (const auto &L : Loops) {
823 VarInit *IterVar = dyn_cast<VarInit>(L.IterVar);
Matthias Braun215ff842016-12-05 07:35:13 +0000824 if (IterVar && IterVar->getNameInit() == Name)
David Greenefb927af2012-02-22 16:09:41 +0000825 return IterVar;
826 }
827
David Greene232bd602011-10-19 13:04:21 +0000828 if (Mode == ParseNameMode)
Matthias Braun215ff842016-12-05 07:35:13 +0000829 return Name;
David Greene232bd602011-10-19 13:04:21 +0000830
Nicolai Haehnlefcd65252018-03-09 12:24:42 +0000831 if (Init *I = Records.getGlobal(Name->getValue()))
832 return I;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000833
Nicolai Haehnle420e28c2018-03-21 17:12:53 +0000834 // Allow self-references of concrete defs, but delay the lookup so that we
835 // get the correct type.
836 if (CurRec && !CurMultiClass && CurRec->getNameInit() == Name)
837 return UnOpInit::get(UnOpInit::CAST, Name, CurRec->getType());
838
David Greene232bd602011-10-19 13:04:21 +0000839 if (Mode == ParseValueMode) {
Matthias Braun215ff842016-12-05 07:35:13 +0000840 Error(NameLoc, "Variable not defined: '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +0000841 return nullptr;
David Greene232bd602011-10-19 13:04:21 +0000842 }
Craig Toppera9642b42015-05-04 01:35:39 +0000843
Matthias Braun215ff842016-12-05 07:35:13 +0000844 return Name;
Chris Lattnerf4127dd2007-11-22 20:49:04 +0000845}
846
David Greene5d0c0512009-05-14 20:54:48 +0000847/// ParseOperation - Parse an operator. This returns null on error.
848///
849/// Operation ::= XOperator ['<' Type '>'] '(' Args ')'
850///
Matt Arsenaulta73fd932014-06-10 20:10:08 +0000851Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) {
David Greene5d0c0512009-05-14 20:54:48 +0000852 switch (Lex.getCode()) {
853 default:
854 TokError("unknown operation");
Craig Topper011817a2014-04-09 04:50:04 +0000855 return nullptr;
David Greene2f7cf7f2011-01-07 17:05:37 +0000856 case tgtok::XHead:
857 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000858 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +0000859 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +0000860 case tgtok::XCast: { // Value ::= !unop '(' Value ')'
861 UnOpInit::UnaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +0000862 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000863
David Greenee8f3b272009-05-14 21:22:49 +0000864 switch (Lex.getCode()) {
Craig Toppera2886c22012-02-07 05:05:23 +0000865 default: llvm_unreachable("Unhandled code!");
David Greenee8f3b272009-05-14 21:22:49 +0000866 case tgtok::XCast:
867 Lex.Lex(); // eat the operation
868 Code = UnOpInit::CAST;
David Greene5d0c0512009-05-14 20:54:48 +0000869
David Greenee8f3b272009-05-14 21:22:49 +0000870 Type = ParseOperatorType();
David Greene5d0c0512009-05-14 20:54:48 +0000871
Craig Topper011817a2014-04-09 04:50:04 +0000872 if (!Type) {
David Greened571b3c2009-05-14 22:38:31 +0000873 TokError("did not get type for unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000874 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000875 }
David Greene5d0c0512009-05-14 20:54:48 +0000876
David Greenee8f3b272009-05-14 21:22:49 +0000877 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000878 case tgtok::XHead:
David Greened571b3c2009-05-14 22:38:31 +0000879 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000880 Code = UnOpInit::HEAD;
David Greened571b3c2009-05-14 22:38:31 +0000881 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000882 case tgtok::XTail:
David Greened571b3c2009-05-14 22:38:31 +0000883 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000884 Code = UnOpInit::TAIL;
David Greened571b3c2009-05-14 22:38:31 +0000885 break;
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000886 case tgtok::XSize:
887 Lex.Lex();
888 Code = UnOpInit::SIZE;
889 Type = IntRecTy::get();
890 break;
David Greene2f7cf7f2011-01-07 17:05:37 +0000891 case tgtok::XEmpty:
David Greened571b3c2009-05-14 22:38:31 +0000892 Lex.Lex(); // eat the operation
David Greene2f7cf7f2011-01-07 17:05:37 +0000893 Code = UnOpInit::EMPTY;
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +0000894 Type = IntRecTy::get();
David Greened571b3c2009-05-14 22:38:31 +0000895 break;
David Greenee8f3b272009-05-14 21:22:49 +0000896 }
897 if (Lex.getCode() != tgtok::l_paren) {
898 TokError("expected '(' after unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000899 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000900 }
901 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +0000902
David Greeneaf8ee2c2011-07-29 22:43:06 +0000903 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +0000904 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +0000905
Craig Topper85c07002015-04-30 05:54:22 +0000906 if (Code == UnOpInit::HEAD ||
907 Code == UnOpInit::TAIL ||
908 Code == UnOpInit::EMPTY) {
Sean Silvafb509ed2012-10-10 20:24:43 +0000909 ListInit *LHSl = dyn_cast<ListInit>(LHS);
910 StringInit *LHSs = dyn_cast<StringInit>(LHS);
911 TypedInit *LHSt = dyn_cast<TypedInit>(LHS);
Craig Topper011817a2014-04-09 04:50:04 +0000912 if (!LHSl && !LHSs && !LHSt) {
David Greene8618f952009-06-08 20:23:18 +0000913 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000914 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000915 }
916 if (LHSt) {
Sean Silva98c61712012-10-05 03:31:58 +0000917 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
918 StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000919 if (!LType && !SType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000920 TokError("expected list or string type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000921 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000922 }
923 }
924
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000925 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL ||
926 Code == UnOpInit::SIZE) {
Craig Topper011817a2014-04-09 04:50:04 +0000927 if (!LHSl && !LHSt) {
Matt Arsenault07576072014-05-31 05:18:52 +0000928 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000929 return nullptr;
David Greene8618f952009-06-08 20:23:18 +0000930 }
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000931 }
Bob Wilson7248f862009-11-22 04:24:42 +0000932
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +0000933 if (Code == UnOpInit::HEAD || Code == UnOpInit::TAIL) {
Craig Topperec9072d2015-05-14 05:53:53 +0000934 if (LHSl && LHSl->empty()) {
David Greened571b3c2009-05-14 22:38:31 +0000935 TokError("empty list argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000936 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000937 }
938 if (LHSl) {
David Greeneaf8ee2c2011-07-29 22:43:06 +0000939 Init *Item = LHSl->getElement(0);
Sean Silvafb509ed2012-10-10 20:24:43 +0000940 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
Craig Topper011817a2014-04-09 04:50:04 +0000941 if (!Itemt) {
David Greened571b3c2009-05-14 22:38:31 +0000942 TokError("untyped list element in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000943 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000944 }
Craig Toppera9642b42015-05-04 01:35:39 +0000945 Type = (Code == UnOpInit::HEAD) ? Itemt->getType()
946 : ListRecTy::get(Itemt->getType());
Bob Wilson7248f862009-11-22 04:24:42 +0000947 } else {
David Greened571b3c2009-05-14 22:38:31 +0000948 assert(LHSt && "expected list type argument in unary operator");
Sean Silva98c61712012-10-05 03:31:58 +0000949 ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType());
Craig Topper011817a2014-04-09 04:50:04 +0000950 if (!LType) {
Matt Arsenault07576072014-05-31 05:18:52 +0000951 TokError("expected list type argument in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000952 return nullptr;
David Greened571b3c2009-05-14 22:38:31 +0000953 }
Craig Toppera9642b42015-05-04 01:35:39 +0000954 Type = (Code == UnOpInit::HEAD) ? LType->getElementType() : LType;
David Greened571b3c2009-05-14 22:38:31 +0000955 }
956 }
957 }
958
David Greenee8f3b272009-05-14 21:22:49 +0000959 if (Lex.getCode() != tgtok::r_paren) {
960 TokError("expected ')' in unary operator");
Craig Topper011817a2014-04-09 04:50:04 +0000961 return nullptr;
David Greenee8f3b272009-05-14 21:22:49 +0000962 }
963 Lex.Lex(); // eat the ')'
Nicolai Haehnlec47fe122018-03-19 14:13:37 +0000964 return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec);
David Greenee8f3b272009-05-14 21:22:49 +0000965 }
David Greene5d0c0512009-05-14 20:54:48 +0000966
Nicolai Haehnleb5376052018-03-09 12:24:06 +0000967 case tgtok::XIsA: {
968 // Value ::= !isa '<' Type '>' '(' Value ')'
969 Lex.Lex(); // eat the operation
970
971 RecTy *Type = ParseOperatorType();
972 if (!Type)
973 return nullptr;
974
975 if (Lex.getCode() != tgtok::l_paren) {
976 TokError("expected '(' after type of !isa");
977 return nullptr;
978 }
979 Lex.Lex(); // eat the '('
980
981 Init *LHS = ParseValue(CurRec);
982 if (!LHS)
983 return nullptr;
984
985 if (Lex.getCode() != tgtok::r_paren) {
986 TokError("expected ')' in !isa");
987 return nullptr;
988 }
989 Lex.Lex(); // eat the ')'
990
991 return (IsAOpInit::get(Type, LHS))->Fold();
992 }
993
David Greene5d0c0512009-05-14 20:54:48 +0000994 case tgtok::XConcat:
Hal Finkelc7d4dc12013-01-25 14:49:08 +0000995 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +0000996 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +0000997 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +0000998 case tgtok::XSRA:
David Greene5d0c0512009-05-14 20:54:48 +0000999 case tgtok::XSRL:
1000 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001001 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001002 case tgtok::XNe:
1003 case tgtok::XLe:
1004 case tgtok::XLt:
1005 case tgtok::XGe:
1006 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001007 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001008 case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001009 tgtok::TokKind OpTok = Lex.getCode();
1010 SMLoc OpLoc = Lex.getLoc();
1011 Lex.Lex(); // eat the operation
1012
David Greene5d0c0512009-05-14 20:54:48 +00001013 BinOpInit::BinaryOp Code;
Chris Lattner61ea00b2010-10-05 23:58:18 +00001014 switch (OpTok) {
Craig Toppera2886c22012-02-07 05:05:23 +00001015 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001016 case tgtok::XConcat: Code = BinOpInit::CONCAT; break;
1017 case tgtok::XADD: Code = BinOpInit::ADD; break;
1018 case tgtok::XAND: Code = BinOpInit::AND; break;
1019 case tgtok::XOR: Code = BinOpInit::OR; break;
1020 case tgtok::XSRA: Code = BinOpInit::SRA; break;
1021 case tgtok::XSRL: Code = BinOpInit::SRL; break;
1022 case tgtok::XSHL: Code = BinOpInit::SHL; break;
1023 case tgtok::XEq: Code = BinOpInit::EQ; break;
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001024 case tgtok::XNe: Code = BinOpInit::NE; break;
1025 case tgtok::XLe: Code = BinOpInit::LE; break;
1026 case tgtok::XLt: Code = BinOpInit::LT; break;
1027 case tgtok::XGe: Code = BinOpInit::GE; break;
1028 case tgtok::XGt: Code = BinOpInit::GT; break;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001029 case tgtok::XListConcat: Code = BinOpInit::LISTCONCAT; break;
1030 case tgtok::XStrConcat: Code = BinOpInit::STRCONCAT; break;
1031 }
1032
1033 RecTy *Type = nullptr;
1034 RecTy *ArgType = nullptr;
1035 switch (OpTok) {
1036 default:
1037 llvm_unreachable("Unhandled code!");
1038 case tgtok::XConcat:
1039 Type = DagRecTy::get();
1040 ArgType = DagRecTy::get();
1041 break;
1042 case tgtok::XAND:
1043 case tgtok::XOR:
1044 case tgtok::XSRA:
1045 case tgtok::XSRL:
1046 case tgtok::XSHL:
1047 case tgtok::XADD:
1048 Type = IntRecTy::get();
1049 ArgType = IntRecTy::get();
1050 break;
1051 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001052 case tgtok::XNe:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001053 Type = BitRecTy::get();
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001054 // ArgType for Eq / Ne is not known at this point
1055 break;
1056 case tgtok::XLe:
1057 case tgtok::XLt:
1058 case tgtok::XGe:
1059 case tgtok::XGt:
1060 Type = BitRecTy::get();
1061 ArgType = IntRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001062 break;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001063 case tgtok::XListConcat:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001064 // We don't know the list type until we parse the first argument
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001065 ArgType = ItemType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001066 break;
Bob Wilson7248f862009-11-22 04:24:42 +00001067 case tgtok::XStrConcat:
Jakob Stoklund Olesenabcfdce2011-07-18 17:02:57 +00001068 Type = StringRecTy::get();
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001069 ArgType = StringRecTy::get();
David Greene5d0c0512009-05-14 20:54:48 +00001070 break;
David Greene5d0c0512009-05-14 20:54:48 +00001071 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001072
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001073 if (Type && ItemType && !Type->typeIsConvertibleTo(ItemType)) {
1074 Error(OpLoc, Twine("expected value of type '") +
1075 ItemType->getAsString() + "', got '" +
1076 Type->getAsString() + "'");
1077 return nullptr;
1078 }
1079
David Greene5d0c0512009-05-14 20:54:48 +00001080 if (Lex.getCode() != tgtok::l_paren) {
1081 TokError("expected '(' after binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001082 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001083 }
1084 Lex.Lex(); // eat the '('
1085
David Greeneaf8ee2c2011-07-29 22:43:06 +00001086 SmallVector<Init*, 2> InitList;
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001087
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001088 for (;;) {
1089 SMLoc InitLoc = Lex.getLoc();
1090 InitList.push_back(ParseValue(CurRec, ArgType));
Craig Topper011817a2014-04-09 04:50:04 +00001091 if (!InitList.back()) return nullptr;
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001092
1093 // All BinOps require their arguments to be of compatible types.
1094 TypedInit *TI = dyn_cast<TypedInit>(InitList.back());
1095 if (!ArgType) {
1096 ArgType = TI->getType();
1097
1098 switch (Code) {
1099 case BinOpInit::LISTCONCAT:
1100 if (!isa<ListRecTy>(ArgType)) {
1101 Error(InitLoc, Twine("expected a list, got value of type '") +
1102 ArgType->getAsString() + "'");
1103 return nullptr;
1104 }
1105 break;
1106 case BinOpInit::EQ:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001107 case BinOpInit::NE:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001108 if (!ArgType->typeIsConvertibleTo(IntRecTy::get()) &&
1109 !ArgType->typeIsConvertibleTo(StringRecTy::get())) {
1110 Error(InitLoc, Twine("expected int, bits, or string; got value of "
1111 "type '") + ArgType->getAsString() + "'");
1112 return nullptr;
1113 }
1114 break;
1115 default: llvm_unreachable("other ops have fixed argument types");
1116 }
1117 } else {
1118 RecTy *Resolved = resolveTypes(ArgType, TI->getType());
1119 if (!Resolved) {
1120 Error(InitLoc, Twine("expected value of type '") +
1121 ArgType->getAsString() + "', got '" +
1122 TI->getType()->getAsString() + "'");
1123 return nullptr;
1124 }
1125 if (Code != BinOpInit::ADD && Code != BinOpInit::AND &&
1126 Code != BinOpInit::OR && Code != BinOpInit::SRA &&
1127 Code != BinOpInit::SRL && Code != BinOpInit::SHL)
1128 ArgType = Resolved;
1129 }
1130
1131 if (Lex.getCode() != tgtok::comma)
1132 break;
1133 Lex.Lex(); // eat the ','
David Greene5d0c0512009-05-14 20:54:48 +00001134 }
David Greene5d0c0512009-05-14 20:54:48 +00001135
1136 if (Lex.getCode() != tgtok::r_paren) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001137 TokError("expected ')' in operator");
Craig Topper011817a2014-04-09 04:50:04 +00001138 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001139 }
1140 Lex.Lex(); // eat the ')'
Chris Lattner61ea00b2010-10-05 23:58:18 +00001141
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001142 if (Code == BinOpInit::LISTCONCAT)
1143 Type = ArgType;
Daniel Sanders314e80e2014-05-07 10:13:19 +00001144
Chris Lattner61ea00b2010-10-05 23:58:18 +00001145 // We allow multiple operands to associative operators like !strconcat as
1146 // shorthand for nesting them.
Nicolai Haehnle77841b12018-03-14 11:00:43 +00001147 if (Code == BinOpInit::STRCONCAT || Code == BinOpInit::LISTCONCAT ||
1148 Code == BinOpInit::CONCAT || Code == BinOpInit::ADD ||
1149 Code == BinOpInit::AND || Code == BinOpInit::OR) {
Chris Lattner61ea00b2010-10-05 23:58:18 +00001150 while (InitList.size() > 2) {
David Greeneaf8ee2c2011-07-29 22:43:06 +00001151 Init *RHS = InitList.pop_back_val();
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001152 RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type))->Fold(CurRec);
Chris Lattner61ea00b2010-10-05 23:58:18 +00001153 InitList.back() = RHS;
1154 }
1155 }
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001156
Chris Lattner61ea00b2010-10-05 23:58:18 +00001157 if (InitList.size() == 2)
David Greenee32ebf22011-07-29 19:07:07 +00001158 return (BinOpInit::get(Code, InitList[0], InitList[1], Type))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001159 ->Fold(CurRec);
Mikhail Glushenkovde683892010-10-23 07:32:37 +00001160
Chris Lattner61ea00b2010-10-05 23:58:18 +00001161 Error(OpLoc, "expected two operands to operator");
Craig Topper011817a2014-04-09 04:50:04 +00001162 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001163 }
1164
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001165 case tgtok::XForEach: { // Value ::= !foreach '(' Id ',' Value ',' Value ')'
1166 SMLoc OpLoc = Lex.getLoc();
1167 Lex.Lex(); // eat the operation
1168 if (Lex.getCode() != tgtok::l_paren) {
1169 TokError("expected '(' after !foreach");
1170 return nullptr;
1171 }
1172
1173 if (Lex.Lex() != tgtok::Id) { // eat the '('
1174 TokError("first argument of !foreach must be an identifier");
1175 return nullptr;
1176 }
1177
1178 Init *LHS = StringInit::get(Lex.getCurStrVal());
1179
1180 if (CurRec->getValue(LHS)) {
1181 TokError((Twine("iteration variable '") + LHS->getAsString() +
1182 "' already defined")
1183 .str());
1184 return nullptr;
1185 }
1186
1187 if (Lex.Lex() != tgtok::comma) { // eat the id
1188 TokError("expected ',' in ternary operator");
1189 return nullptr;
1190 }
1191 Lex.Lex(); // eat the ','
1192
1193 Init *MHS = ParseValue(CurRec);
1194 if (!MHS)
1195 return nullptr;
1196
1197 if (Lex.getCode() != tgtok::comma) {
1198 TokError("expected ',' in ternary operator");
1199 return nullptr;
1200 }
1201 Lex.Lex(); // eat the ','
1202
1203 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1204 if (!MHSt) {
1205 TokError("could not get type of !foreach input");
1206 return nullptr;
1207 }
1208
1209 RecTy *InEltType = nullptr;
1210 RecTy *OutEltType = nullptr;
1211 bool IsDAG = false;
1212
1213 if (ListRecTy *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
1214 InEltType = InListTy->getElementType();
1215 if (ItemType) {
1216 if (ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1217 OutEltType = OutListTy->getElementType();
1218 } else {
1219 Error(OpLoc,
1220 "expected value of type '" + Twine(ItemType->getAsString()) +
1221 "', but got !foreach of list type");
1222 return nullptr;
1223 }
1224 }
1225 } else if (DagRecTy *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
1226 InEltType = InDagTy;
1227 if (ItemType && !isa<DagRecTy>(ItemType)) {
1228 Error(OpLoc,
1229 "expected value of type '" + Twine(ItemType->getAsString()) +
1230 "', but got !foreach of dag type");
1231 return nullptr;
1232 }
1233 IsDAG = true;
1234 } else {
1235 TokError("!foreach must have list or dag input");
1236 return nullptr;
1237 }
1238
1239 CurRec->addValue(RecordVal(LHS, InEltType, false));
1240 Init *RHS = ParseValue(CurRec, OutEltType);
1241 CurRec->removeValue(LHS);
1242 if (!RHS)
1243 return nullptr;
1244
1245 if (Lex.getCode() != tgtok::r_paren) {
1246 TokError("expected ')' in binary operator");
1247 return nullptr;
1248 }
1249 Lex.Lex(); // eat the ')'
1250
1251 RecTy *OutType;
1252 if (IsDAG) {
1253 OutType = InEltType;
1254 } else {
1255 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1256 if (!RHSt) {
1257 TokError("could not get type of !foreach result");
1258 return nullptr;
1259 }
1260 OutType = RHSt->getType()->getListTy();
1261 }
1262
1263 return (TernOpInit::get(TernOpInit::FOREACH, LHS, MHS, RHS, OutType))
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001264 ->Fold(CurRec);
Nicolai Haehnle8ebf7e42018-03-05 15:21:04 +00001265 }
1266
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001267 case tgtok::XDag:
David Greene3587eed2009-05-14 23:26:46 +00001268 case tgtok::XIf:
David Greene98ed3c72009-05-14 21:54:42 +00001269 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
1270 TernOpInit::TernaryOp Code;
Craig Topper011817a2014-04-09 04:50:04 +00001271 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001272
David Greene98ed3c72009-05-14 21:54:42 +00001273 tgtok::TokKind LexCode = Lex.getCode();
1274 Lex.Lex(); // eat the operation
1275 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001276 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001277 case tgtok::XDag:
1278 Code = TernOpInit::DAG;
1279 Type = DagRecTy::get();
1280 ItemType = nullptr;
1281 break;
David Greene3587eed2009-05-14 23:26:46 +00001282 case tgtok::XIf:
1283 Code = TernOpInit::IF;
1284 break;
David Greene98ed3c72009-05-14 21:54:42 +00001285 case tgtok::XSubst:
1286 Code = TernOpInit::SUBST;
1287 break;
1288 }
1289 if (Lex.getCode() != tgtok::l_paren) {
1290 TokError("expected '(' after ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001291 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001292 }
1293 Lex.Lex(); // eat the '('
David Greene5d0c0512009-05-14 20:54:48 +00001294
David Greeneaf8ee2c2011-07-29 22:43:06 +00001295 Init *LHS = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001296 if (!LHS) return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001297
David Greene98ed3c72009-05-14 21:54:42 +00001298 if (Lex.getCode() != tgtok::comma) {
1299 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001300 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001301 }
1302 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001303
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001304 SMLoc MHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001305 Init *MHS = ParseValue(CurRec, ItemType);
1306 if (!MHS)
1307 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001308
David Greene98ed3c72009-05-14 21:54:42 +00001309 if (Lex.getCode() != tgtok::comma) {
1310 TokError("expected ',' in ternary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001311 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001312 }
1313 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00001314
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001315 SMLoc RHSLoc = Lex.getLoc();
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001316 Init *RHS = ParseValue(CurRec, ItemType);
1317 if (!RHS)
1318 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001319
David Greene98ed3c72009-05-14 21:54:42 +00001320 if (Lex.getCode() != tgtok::r_paren) {
1321 TokError("expected ')' in binary operator");
Craig Topper011817a2014-04-09 04:50:04 +00001322 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001323 }
1324 Lex.Lex(); // eat the ')'
David Greene5d0c0512009-05-14 20:54:48 +00001325
David Greene98ed3c72009-05-14 21:54:42 +00001326 switch (LexCode) {
Craig Toppera2886c22012-02-07 05:05:23 +00001327 default: llvm_unreachable("Unhandled code!");
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001328 case tgtok::XDag: {
1329 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1330 if (!MHSt && !isa<UnsetInit>(MHS)) {
1331 Error(MHSLoc, "could not determine type of the child list in !dag");
1332 return nullptr;
1333 }
1334 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1335 Error(MHSLoc, Twine("expected list of children, got type '") +
1336 MHSt->getType()->getAsString() + "'");
1337 return nullptr;
1338 }
1339
1340 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
1341 if (!RHSt && !isa<UnsetInit>(RHS)) {
1342 Error(RHSLoc, "could not determine type of the name list in !dag");
1343 return nullptr;
1344 }
1345 if (RHSt && StringRecTy::get()->getListTy() != RHSt->getType()) {
1346 Error(RHSLoc, Twine("expected list<string>, got type '") +
1347 RHSt->getType()->getAsString() + "'");
1348 return nullptr;
1349 }
1350
1351 if (!MHSt && !RHSt) {
1352 Error(MHSLoc,
1353 "cannot have both unset children and unset names in !dag");
1354 return nullptr;
1355 }
1356 break;
1357 }
David Greene3587eed2009-05-14 23:26:46 +00001358 case tgtok::XIf: {
Craig Topper011817a2014-04-09 04:50:04 +00001359 RecTy *MHSTy = nullptr;
1360 RecTy *RHSTy = nullptr;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001361
Sean Silvafb509ed2012-10-10 20:24:43 +00001362 if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001363 MHSTy = MHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001364 if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001365 MHSTy = BitsRecTy::get(MHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001366 if (isa<BitInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001367 MHSTy = BitRecTy::get();
1368
Sean Silvafb509ed2012-10-10 20:24:43 +00001369 if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
Bill Wendling73ce4a62010-12-13 01:46:19 +00001370 RHSTy = RHSt->getType();
Sean Silvafb509ed2012-10-10 20:24:43 +00001371 if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001372 RHSTy = BitsRecTy::get(RHSbits->getNumBits());
Sean Silva88eb8dd2012-10-10 20:24:47 +00001373 if (isa<BitInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001374 RHSTy = BitRecTy::get();
1375
1376 // For UnsetInit, it's typed from the other hand.
Sean Silva88eb8dd2012-10-10 20:24:47 +00001377 if (isa<UnsetInit>(MHS))
Michael Liao026f8332012-09-06 23:32:48 +00001378 MHSTy = RHSTy;
Sean Silva88eb8dd2012-10-10 20:24:47 +00001379 if (isa<UnsetInit>(RHS))
Michael Liao026f8332012-09-06 23:32:48 +00001380 RHSTy = MHSTy;
Bill Wendling73ce4a62010-12-13 01:46:19 +00001381
1382 if (!MHSTy || !RHSTy) {
David Greene3587eed2009-05-14 23:26:46 +00001383 TokError("could not get type for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001384 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001385 }
Bill Wendling73ce4a62010-12-13 01:46:19 +00001386
Nicolai Haehnle13080fd2018-03-06 13:48:20 +00001387 Type = resolveTypes(MHSTy, RHSTy);
1388 if (!Type) {
1389 TokError(Twine("inconsistent types '") + MHSTy->getAsString() +
1390 "' and '" + RHSTy->getAsString() + "' for !if");
Craig Topper011817a2014-04-09 04:50:04 +00001391 return nullptr;
David Greene3587eed2009-05-14 23:26:46 +00001392 }
1393 break;
1394 }
David Greene98ed3c72009-05-14 21:54:42 +00001395 case tgtok::XSubst: {
Sean Silvafb509ed2012-10-10 20:24:43 +00001396 TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
Craig Topper011817a2014-04-09 04:50:04 +00001397 if (!RHSt) {
David Greene98ed3c72009-05-14 21:54:42 +00001398 TokError("could not get type for !subst");
Craig Topper011817a2014-04-09 04:50:04 +00001399 return nullptr;
David Greene98ed3c72009-05-14 21:54:42 +00001400 }
1401 Type = RHSt->getType();
1402 break;
1403 }
1404 }
Nicolai Haehnlec47fe122018-03-19 14:13:37 +00001405 return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec);
David Greene98ed3c72009-05-14 21:54:42 +00001406 }
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001407
1408 case tgtok::XFoldl: {
1409 // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')'
1410 Lex.Lex(); // eat the operation
1411 if (Lex.getCode() != tgtok::l_paren) {
1412 TokError("expected '(' after !foldl");
1413 return nullptr;
1414 }
1415 Lex.Lex(); // eat the '('
1416
1417 Init *StartUntyped = ParseValue(CurRec);
1418 if (!StartUntyped)
1419 return nullptr;
1420
1421 TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1422 if (!Start) {
1423 TokError(Twine("could not get type of !foldl start: '") +
1424 StartUntyped->getAsString() + "'");
1425 return nullptr;
1426 }
1427
1428 if (Lex.getCode() != tgtok::comma) {
1429 TokError("expected ',' in !foldl");
1430 return nullptr;
1431 }
1432 Lex.Lex(); // eat the ','
1433
1434 Init *ListUntyped = ParseValue(CurRec);
1435 if (!ListUntyped)
1436 return nullptr;
1437
1438 TypedInit *List = dyn_cast<TypedInit>(ListUntyped);
1439 if (!List) {
1440 TokError(Twine("could not get type of !foldl list: '") +
1441 ListUntyped->getAsString() + "'");
1442 return nullptr;
1443 }
1444
1445 ListRecTy *ListType = dyn_cast<ListRecTy>(List->getType());
1446 if (!ListType) {
1447 TokError(Twine("!foldl list must be a list, but is of type '") +
1448 List->getType()->getAsString());
1449 return nullptr;
1450 }
1451
1452 if (Lex.getCode() != tgtok::comma) {
1453 TokError("expected ',' in !foldl");
1454 return nullptr;
1455 }
1456
1457 if (Lex.Lex() != tgtok::Id) { // eat the ','
1458 TokError("third argument of !foldl must be an identifier");
1459 return nullptr;
1460 }
1461
1462 Init *A = StringInit::get(Lex.getCurStrVal());
1463 if (CurRec->getValue(A)) {
1464 TokError((Twine("left !foldl variable '") + A->getAsString() +
1465 "' already defined")
1466 .str());
1467 return nullptr;
1468 }
1469
1470 if (Lex.Lex() != tgtok::comma) { // eat the id
1471 TokError("expected ',' in !foldl");
1472 return nullptr;
1473 }
1474
1475 if (Lex.Lex() != tgtok::Id) { // eat the ','
1476 TokError("fourth argument of !foldl must be an identifier");
1477 return nullptr;
1478 }
1479
1480 Init *B = StringInit::get(Lex.getCurStrVal());
1481 if (CurRec->getValue(B)) {
1482 TokError((Twine("right !foldl variable '") + B->getAsString() +
1483 "' already defined")
1484 .str());
1485 return nullptr;
1486 }
1487
1488 if (Lex.Lex() != tgtok::comma) { // eat the id
1489 TokError("expected ',' in !foldl");
1490 return nullptr;
1491 }
1492 Lex.Lex(); // eat the ','
1493
1494 CurRec->addValue(RecordVal(A, Start->getType(), false));
1495 CurRec->addValue(RecordVal(B, ListType->getElementType(), false));
1496 Init *ExprUntyped = ParseValue(CurRec);
1497 CurRec->removeValue(A);
1498 CurRec->removeValue(B);
1499 if (!ExprUntyped)
1500 return nullptr;
1501
1502 TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1503 if (!Expr) {
1504 TokError("could not get type of !foldl expression");
1505 return nullptr;
1506 }
1507
1508 if (Expr->getType() != Start->getType()) {
1509 TokError(Twine("!foldl expression must be of same type as start (") +
1510 Start->getType()->getAsString() + "), but is of type " +
1511 Expr->getType()->getAsString());
1512 return nullptr;
1513 }
1514
1515 if (Lex.getCode() != tgtok::r_paren) {
1516 TokError("expected ')' in fold operator");
1517 return nullptr;
1518 }
1519 Lex.Lex(); // eat the ')'
1520
1521 return FoldOpInit::get(Start, List, A, B, Expr, Start->getType())
1522 ->Fold(CurRec);
1523 }
David Greene5d0c0512009-05-14 20:54:48 +00001524 }
David Greene5d0c0512009-05-14 20:54:48 +00001525}
1526
1527/// ParseOperatorType - Parse a type for an operator. This returns
1528/// null on error.
1529///
1530/// OperatorType ::= '<' Type '>'
1531///
Dan Gohman1432ef82009-08-12 22:10:57 +00001532RecTy *TGParser::ParseOperatorType() {
Craig Topper011817a2014-04-09 04:50:04 +00001533 RecTy *Type = nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001534
1535 if (Lex.getCode() != tgtok::less) {
1536 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001537 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001538 }
1539 Lex.Lex(); // eat the <
1540
1541 Type = ParseType();
1542
Craig Topper011817a2014-04-09 04:50:04 +00001543 if (!Type) {
David Greene5d0c0512009-05-14 20:54:48 +00001544 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001545 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001546 }
1547
1548 if (Lex.getCode() != tgtok::greater) {
1549 TokError("expected type name for operator");
Craig Topper011817a2014-04-09 04:50:04 +00001550 return nullptr;
David Greene5d0c0512009-05-14 20:54:48 +00001551 }
1552 Lex.Lex(); // eat the >
1553
1554 return Type;
1555}
1556
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001557/// ParseSimpleValue - Parse a tblgen value. This returns null on error.
1558///
1559/// SimpleValue ::= IDValue
1560/// SimpleValue ::= INTVAL
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001561/// SimpleValue ::= STRVAL+
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001562/// SimpleValue ::= CODEFRAGMENT
1563/// SimpleValue ::= '?'
1564/// SimpleValue ::= '{' ValueList '}'
1565/// SimpleValue ::= ID '<' ValueListNE '>'
1566/// SimpleValue ::= '[' ValueList ']'
1567/// SimpleValue ::= '(' IDValue DagArgList ')'
1568/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')'
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001569/// SimpleValue ::= ADDTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001570/// SimpleValue ::= SHLTOK '(' Value ',' Value ')'
1571/// SimpleValue ::= SRATOK '(' Value ',' Value ')'
1572/// SimpleValue ::= SRLTOK '(' Value ',' Value ')'
Daniel Sanders314e80e2014-05-07 10:13:19 +00001573/// SimpleValue ::= LISTCONCATTOK '(' Value ',' Value ')'
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001574/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')'
1575///
David Greened4263a62011-10-19 13:04:20 +00001576Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType,
1577 IDParseMode Mode) {
Craig Topper011817a2014-04-09 04:50:04 +00001578 Init *R = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001579 switch (Lex.getCode()) {
1580 default: TokError("Unknown token when parsing a value"); break;
David Greene8e85b482011-10-19 13:04:43 +00001581 case tgtok::paste:
1582 // This is a leading paste operation. This is deprecated but
1583 // still exists in some .td files. Ignore it.
1584 Lex.Lex(); // Skip '#'.
1585 return ParseSimpleValue(CurRec, ItemType, Mode);
David Greenee32ebf22011-07-29 19:07:07 +00001586 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
Pete Cooper25977642014-08-07 05:47:00 +00001587 case tgtok::BinaryIntVal: {
1588 auto BinaryVal = Lex.getCurBinaryIntVal();
1589 SmallVector<Init*, 16> Bits(BinaryVal.second);
1590 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
Aaron Ballmanb677f7a2014-08-07 12:07:33 +00001591 Bits[i] = BitInit::get(BinaryVal.first & (1LL << i));
Pete Cooper25977642014-08-07 05:47:00 +00001592 R = BitsInit::get(Bits);
1593 Lex.Lex();
1594 break;
1595 }
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001596 case tgtok::StrVal: {
1597 std::string Val = Lex.getCurStrVal();
1598 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00001599
Jim Grosbach975c1cb2009-03-26 16:17:51 +00001600 // Handle multiple consecutive concatenated strings.
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001601 while (Lex.getCode() == tgtok::StrVal) {
1602 Val += Lex.getCurStrVal();
1603 Lex.Lex();
1604 }
Bob Wilson7248f862009-11-22 04:24:42 +00001605
David Greenee32ebf22011-07-29 19:07:07 +00001606 R = StringInit::get(Val);
Chris Lattnerbe0d6722009-03-11 17:08:13 +00001607 break;
1608 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001609 case tgtok::CodeFragment:
Tim Northover88403d72016-07-05 21:22:55 +00001610 R = CodeInit::get(Lex.getCurStrVal());
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001611 Lex.Lex();
1612 break;
1613 case tgtok::question:
David Greenee32ebf22011-07-29 19:07:07 +00001614 R = UnsetInit::get();
Chris Lattnere76cfcf2010-10-06 04:31:40 +00001615 Lex.Lex();
1616 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001617 case tgtok::Id: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001618 SMLoc NameLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00001619 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001620 if (Lex.Lex() != tgtok::less) // consume the Id.
David Greened4263a62011-10-19 13:04:20 +00001621 return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue
Bob Wilson7248f862009-11-22 04:24:42 +00001622
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001623 // Value ::= ID '<' ValueListNE '>'
1624 if (Lex.Lex() == tgtok::greater) {
1625 TokError("expected non-empty value list");
Craig Topper011817a2014-04-09 04:50:04 +00001626 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001627 }
David Greene8618f952009-06-08 20:23:18 +00001628
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001629 // This is a CLASS<initvalslist> expression. This is supposed to synthesize
1630 // a new anonymous definition, deriving from CLASS<initvalslist> with no
1631 // body.
Matthias Braun215ff842016-12-05 07:35:13 +00001632 Record *Class = Records.getClass(Name->getValue());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001633 if (!Class) {
Matthias Braun215ff842016-12-05 07:35:13 +00001634 Error(NameLoc, "Expected a class name, got '" + Name->getValue() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001635 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001636 }
David Greene8618f952009-06-08 20:23:18 +00001637
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001638 SmallVector<Init *, 8> Args;
1639 ParseValueList(Args, CurRec, Class);
1640 if (Args.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001641
David Greene8618f952009-06-08 20:23:18 +00001642 if (Lex.getCode() != tgtok::greater) {
1643 TokError("expected '>' at end of value list");
Craig Topper011817a2014-04-09 04:50:04 +00001644 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001645 }
1646 Lex.Lex(); // eat the '>'
Bob Wilson7248f862009-11-22 04:24:42 +00001647
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001648 // Typecheck the template arguments list
1649 ArrayRef<Init *> ExpectedArgs = Class->getTemplateArgs();
1650 if (ExpectedArgs.size() < Args.size()) {
1651 Error(NameLoc,
1652 "More template args specified than expected");
Craig Topper011817a2014-04-09 04:50:04 +00001653 return nullptr;
Hal Finkela8c1f462014-01-02 20:47:09 +00001654 }
Bob Wilson7248f862009-11-22 04:24:42 +00001655
Nicolai Haehnled4c0a5d2018-03-06 13:49:01 +00001656 for (unsigned i = 0, e = ExpectedArgs.size(); i != e; ++i) {
1657 RecordVal *ExpectedArg = Class->getValue(ExpectedArgs[i]);
1658 if (i < Args.size()) {
1659 if (TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1660 RecTy *ExpectedType = ExpectedArg->getType();
1661 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1662 Error(NameLoc,
1663 "Value specified for template argument #" + Twine(i) + " (" +
1664 ExpectedArg->getNameInitAsString() + ") is of type '" +
1665 TI->getType()->getAsString() + "', expected '" +
1666 ExpectedType->getAsString() + "': " + TI->getAsString());
1667 return nullptr;
1668 }
1669 continue;
1670 }
1671 } else if (ExpectedArg->getValue()->isComplete())
1672 continue;
1673
1674 Error(NameLoc,
1675 "Value not specified for template argument #" + Twine(i) + " (" +
1676 ExpectedArgs[i]->getAsUnquotedString() + ")");
1677 return nullptr;
1678 }
1679
1680 return VarDefInit::get(Class, Args)->Fold();
Bob Wilson7248f862009-11-22 04:24:42 +00001681 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001682 case tgtok::l_brace: { // Value ::= '{' ValueList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00001683 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001684 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001685 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001686
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001687 if (Lex.getCode() != tgtok::r_brace) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001688 ParseValueList(Vals, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001689 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001690 }
1691 if (Lex.getCode() != tgtok::r_brace) {
1692 TokError("expected '}' at end of bit list value");
Craig Topper011817a2014-04-09 04:50:04 +00001693 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001694 }
1695 Lex.Lex(); // eat the '}'
Bob Wilson7248f862009-11-22 04:24:42 +00001696
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001697 SmallVector<Init *, 16> NewBits;
David Greeneb3da8122011-07-29 19:07:00 +00001698
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001699 // As we parse { a, b, ... }, 'a' is the highest bit, but we parse it
1700 // first. We'll first read everything in to a vector, then we can reverse
1701 // it to get the bits in the correct order for the BitsInit value.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001702 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Jean-Luc Duprat97bfbb82014-08-29 22:43:30 +00001703 // FIXME: The following two loops would not be duplicated
1704 // if the API was a little more orthogonal.
1705
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001706 // bits<n> values are allowed to initialize n bits.
1707 if (BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1708 for (unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1709 NewBits.push_back(BI->getBit((e - i) - 1));
1710 continue;
1711 }
Jean-Luc Duprat6d7b4562014-08-29 19:41:04 +00001712 // bits<n> can also come from variable initializers.
1713 if (VarInit *VI = dyn_cast<VarInit>(Vals[i])) {
1714 if (BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(VI->getType())) {
1715 for (unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1716 NewBits.push_back(VI->getBit((e - i) - 1));
1717 continue;
1718 }
1719 // Fallthrough to try convert this to a bit.
1720 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001721 // All other values must be convertible to just a single bit.
Nicolai Haehnledfda9dc2018-03-06 13:48:39 +00001722 Init *Bit = Vals[i]->getCastTo(BitRecTy::get());
Craig Topper011817a2014-04-09 04:50:04 +00001723 if (!Bit) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001724 Error(BraceLoc, "Element #" + Twine(i) + " (" + Vals[i]->getAsString() +
Chris Lattner52416952007-11-22 21:06:59 +00001725 ") is not convertable to a bit");
Craig Topper011817a2014-04-09 04:50:04 +00001726 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001727 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001728 NewBits.push_back(Bit);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001729 }
Pete Cooper0bf1ea72014-08-07 05:47:07 +00001730 std::reverse(NewBits.begin(), NewBits.end());
David Greenee32ebf22011-07-29 19:07:07 +00001731 return BitsInit::get(NewBits);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001732 }
1733 case tgtok::l_square: { // Value ::= '[' ValueList ']'
1734 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001735 SmallVector<Init*, 16> Vals;
Bob Wilson7248f862009-11-22 04:24:42 +00001736
Craig Topper011817a2014-04-09 04:50:04 +00001737 RecTy *DeducedEltTy = nullptr;
1738 ListRecTy *GivenListTy = nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001739
Craig Topper011817a2014-04-09 04:50:04 +00001740 if (ItemType) {
Sean Silva98c61712012-10-05 03:31:58 +00001741 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
Craig Topper011817a2014-04-09 04:50:04 +00001742 if (!ListType) {
Benjamin Kramerdba7ee92015-05-28 11:24:24 +00001743 TokError(Twine("Type mismatch for list, expected list type, got ") +
1744 ItemType->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001745 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001746 }
1747 GivenListTy = ListType;
Bob Wilson7248f862009-11-22 04:24:42 +00001748 }
David Greene8618f952009-06-08 20:23:18 +00001749
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001750 if (Lex.getCode() != tgtok::r_square) {
Matthias Braunc66e7552016-12-05 06:41:54 +00001751 ParseValueList(Vals, CurRec, nullptr,
1752 GivenListTy ? GivenListTy->getElementType() : nullptr);
Craig Topper011817a2014-04-09 04:50:04 +00001753 if (Vals.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001754 }
1755 if (Lex.getCode() != tgtok::r_square) {
1756 TokError("expected ']' at end of list value");
Craig Topper011817a2014-04-09 04:50:04 +00001757 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001758 }
1759 Lex.Lex(); // eat the ']'
David Greene8618f952009-06-08 20:23:18 +00001760
Craig Topper011817a2014-04-09 04:50:04 +00001761 RecTy *GivenEltTy = nullptr;
David Greene8618f952009-06-08 20:23:18 +00001762 if (Lex.getCode() == tgtok::less) {
1763 // Optional list element type
1764 Lex.Lex(); // eat the '<'
1765
1766 GivenEltTy = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00001767 if (!GivenEltTy) {
David Greene8618f952009-06-08 20:23:18 +00001768 // Couldn't parse element type
Craig Topper011817a2014-04-09 04:50:04 +00001769 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001770 }
1771
1772 if (Lex.getCode() != tgtok::greater) {
1773 TokError("expected '>' at end of list element type");
Craig Topper011817a2014-04-09 04:50:04 +00001774 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001775 }
1776 Lex.Lex(); // eat the '>'
1777 }
1778
1779 // Check elements
Craig Topper011817a2014-04-09 04:50:04 +00001780 RecTy *EltTy = nullptr;
Craig Toppereb4d7c62015-04-29 04:43:36 +00001781 for (Init *V : Vals) {
1782 TypedInit *TArg = dyn_cast<TypedInit>(V);
Nicolai Haehnleef60a262018-03-14 11:00:33 +00001783 if (TArg) {
1784 if (EltTy) {
1785 EltTy = resolveTypes(EltTy, TArg->getType());
1786 if (!EltTy) {
1787 TokError("Incompatible types in list elements");
1788 return nullptr;
1789 }
1790 } else {
1791 EltTy = TArg->getType();
David Greene8618f952009-06-08 20:23:18 +00001792 }
David Greene8618f952009-06-08 20:23:18 +00001793 }
1794 }
1795
Craig Topper011817a2014-04-09 04:50:04 +00001796 if (GivenEltTy) {
1797 if (EltTy) {
David Greene8618f952009-06-08 20:23:18 +00001798 // Verify consistency
1799 if (!EltTy->typeIsConvertibleTo(GivenEltTy)) {
1800 TokError("Incompatible types in list elements");
Craig Topper011817a2014-04-09 04:50:04 +00001801 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001802 }
1803 }
1804 EltTy = GivenEltTy;
1805 }
1806
Craig Topper011817a2014-04-09 04:50:04 +00001807 if (!EltTy) {
1808 if (!ItemType) {
David Greene8618f952009-06-08 20:23:18 +00001809 TokError("No type for list");
Craig Topper011817a2014-04-09 04:50:04 +00001810 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001811 }
1812 DeducedEltTy = GivenListTy->getElementType();
Bob Wilson7248f862009-11-22 04:24:42 +00001813 } else {
David Greene8618f952009-06-08 20:23:18 +00001814 // Make sure the deduced type is compatible with the given type
1815 if (GivenListTy) {
1816 if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) {
Nicolai Haehnlef19083d2018-02-22 15:26:21 +00001817 TokError(Twine("Element type mismatch for list: element type '") +
1818 EltTy->getAsString() + "' not convertible to '" +
1819 GivenListTy->getElementType()->getAsString());
Craig Topper011817a2014-04-09 04:50:04 +00001820 return nullptr;
David Greene8618f952009-06-08 20:23:18 +00001821 }
1822 }
1823 DeducedEltTy = EltTy;
1824 }
Bob Wilson7248f862009-11-22 04:24:42 +00001825
David Greenee32ebf22011-07-29 19:07:07 +00001826 return ListInit::get(Vals, DeducedEltTy);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001827 }
1828 case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')'
1829 Lex.Lex(); // eat the '('
Chris Lattner94026332010-10-06 00:19:21 +00001830 if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) {
Chris Lattner57dd7742008-04-10 04:48:34 +00001831 TokError("expected identifier in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001832 return nullptr;
Chris Lattner57dd7742008-04-10 04:48:34 +00001833 }
Bob Wilson7248f862009-11-22 04:24:42 +00001834
David Greeneaf8ee2c2011-07-29 22:43:06 +00001835 Init *Operator = ParseValue(CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001836 if (!Operator) return nullptr;
David Greenea9c6c5d2009-04-22 20:18:10 +00001837
Nate Begemandbe3f772009-03-19 05:21:56 +00001838 // If the operator name is present, parse it.
Matthias Braun7cf3b112016-12-05 06:00:41 +00001839 StringInit *OperatorName = nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001840 if (Lex.getCode() == tgtok::colon) {
1841 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
1842 TokError("expected variable name in dag operator");
Craig Topper011817a2014-04-09 04:50:04 +00001843 return nullptr;
Nate Begemandbe3f772009-03-19 05:21:56 +00001844 }
Matthias Braun7cf3b112016-12-05 06:00:41 +00001845 OperatorName = StringInit::get(Lex.getCurStrVal());
Nate Begemandbe3f772009-03-19 05:21:56 +00001846 Lex.Lex(); // eat the VarName.
1847 }
Bob Wilson7248f862009-11-22 04:24:42 +00001848
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001849 SmallVector<std::pair<llvm::Init*, StringInit*>, 8> DagArgs;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001850 if (Lex.getCode() != tgtok::r_paren) {
Matthias Braun1ddb78c2016-12-05 06:41:51 +00001851 ParseDagArgList(DagArgs, CurRec);
Craig Topper011817a2014-04-09 04:50:04 +00001852 if (DagArgs.empty()) return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001853 }
Bob Wilson7248f862009-11-22 04:24:42 +00001854
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001855 if (Lex.getCode() != tgtok::r_paren) {
1856 TokError("expected ')' in dag init");
Craig Topper011817a2014-04-09 04:50:04 +00001857 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001858 }
1859 Lex.Lex(); // eat the ')'
Bob Wilson7248f862009-11-22 04:24:42 +00001860
David Greenee32ebf22011-07-29 19:07:07 +00001861 return DagInit::get(Operator, OperatorName, DagArgs);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001862 }
Bob Wilson7248f862009-11-22 04:24:42 +00001863
David Greene2f7cf7f2011-01-07 17:05:37 +00001864 case tgtok::XHead:
1865 case tgtok::XTail:
Nicolai Haehnle0243aaf2018-02-23 10:46:07 +00001866 case tgtok::XSize:
David Greene2f7cf7f2011-01-07 17:05:37 +00001867 case tgtok::XEmpty:
David Greenee8f3b272009-05-14 21:22:49 +00001868 case tgtok::XCast: // Value ::= !unop '(' Value ')'
Nicolai Haehnleb5376052018-03-09 12:24:06 +00001869 case tgtok::XIsA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001870 case tgtok::XConcat:
Nicolai Haehnle6c118652018-03-14 11:00:26 +00001871 case tgtok::XDag:
Hal Finkelc7d4dc12013-01-25 14:49:08 +00001872 case tgtok::XADD:
Joerg Sonnenberger6b41a992014-08-05 09:43:25 +00001873 case tgtok::XAND:
Matt Arsenault1c8d9332016-11-15 06:49:28 +00001874 case tgtok::XOR:
Bob Wilson7248f862009-11-22 04:24:42 +00001875 case tgtok::XSRA:
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001876 case tgtok::XSRL:
1877 case tgtok::XSHL:
David Greene297bfe62010-01-05 19:11:42 +00001878 case tgtok::XEq:
Nicolai Haehnleaa9ca692018-03-14 11:00:57 +00001879 case tgtok::XNe:
1880 case tgtok::XLe:
1881 case tgtok::XLt:
1882 case tgtok::XGe:
1883 case tgtok::XGt:
Daniel Sanders314e80e2014-05-07 10:13:19 +00001884 case tgtok::XListConcat:
Chris Lattner94026332010-10-06 00:19:21 +00001885 case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')'
David Greene3587eed2009-05-14 23:26:46 +00001886 case tgtok::XIf:
Nicolai Haehnled34f6842018-03-06 13:49:16 +00001887 case tgtok::XFoldl:
David Greenee917fff2009-05-14 22:23:47 +00001888 case tgtok::XForEach:
David Greene98ed3c72009-05-14 21:54:42 +00001889 case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')'
Matt Arsenaulta73fd932014-06-10 20:10:08 +00001890 return ParseOperation(CurRec, ItemType);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001891 }
1892 }
Bob Wilson7248f862009-11-22 04:24:42 +00001893
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001894 return R;
1895}
1896
1897/// ParseValue - Parse a tblgen value. This returns null on error.
1898///
1899/// Value ::= SimpleValue ValueSuffix*
1900/// ValueSuffix ::= '{' BitList '}'
1901/// ValueSuffix ::= '[' BitList ']'
1902/// ValueSuffix ::= '.' ID
1903///
David Greened4263a62011-10-19 13:04:20 +00001904Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) {
1905 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
Craig Topper011817a2014-04-09 04:50:04 +00001906 if (!Result) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001907
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001908 // Parse the suffixes now if present.
Eugene Zelenko33d7b762016-08-23 17:14:32 +00001909 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001910 switch (Lex.getCode()) {
1911 default: return Result;
1912 case tgtok::l_brace: {
Nicolai Haehnle2435855a2018-03-09 12:24:20 +00001913 if (Mode == ParseNameMode)
David Greeneb8a7c9d2011-10-19 13:04:26 +00001914 // This is the beginning of the object body.
1915 return Result;
1916
Chris Lattner526c8cb2009-06-21 03:39:35 +00001917 SMLoc CurlyLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001918 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00001919 SmallVector<unsigned, 16> Ranges;
1920 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001921 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001922
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001923 // Reverse the bitlist.
1924 std::reverse(Ranges.begin(), Ranges.end());
1925 Result = Result->convertInitializerBitRange(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001926 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001927 Error(CurlyLoc, "Invalid bit range for value");
Craig Topper011817a2014-04-09 04:50:04 +00001928 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001929 }
Bob Wilson7248f862009-11-22 04:24:42 +00001930
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001931 // Eat the '}'.
1932 if (Lex.getCode() != tgtok::r_brace) {
1933 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00001934 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001935 }
1936 Lex.Lex();
1937 break;
1938 }
1939 case tgtok::l_square: {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001940 SMLoc SquareLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001941 Lex.Lex(); // eat the '['
Matthias Braunc66e7552016-12-05 06:41:54 +00001942 SmallVector<unsigned, 16> Ranges;
1943 ParseRangeList(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001944 if (Ranges.empty()) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00001945
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001946 Result = Result->convertInitListSlice(Ranges);
Craig Topper011817a2014-04-09 04:50:04 +00001947 if (!Result) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001948 Error(SquareLoc, "Invalid range for list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001949 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001950 }
Bob Wilson7248f862009-11-22 04:24:42 +00001951
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001952 // Eat the ']'.
1953 if (Lex.getCode() != tgtok::r_square) {
1954 TokError("expected ']' at end of list slice");
Craig Topper011817a2014-04-09 04:50:04 +00001955 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001956 }
1957 Lex.Lex();
1958 break;
1959 }
Matthias Braun6a441832016-12-05 06:00:36 +00001960 case tgtok::period: {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001961 if (Lex.Lex() != tgtok::Id) { // eat the .
1962 TokError("expected field identifier after '.'");
Craig Topper011817a2014-04-09 04:50:04 +00001963 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001964 }
Matthias Braun6a441832016-12-05 06:00:36 +00001965 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
1966 if (!Result->getFieldType(FieldName)) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001967 TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" +
Chris Lattner52416952007-11-22 21:06:59 +00001968 Result->getAsString() + "'");
Craig Topper011817a2014-04-09 04:50:04 +00001969 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001970 }
Nicolai Haehnle2ad19012018-03-19 14:14:28 +00001971 Result = FieldInit::get(Result, FieldName)->Fold(CurRec);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00001972 Lex.Lex(); // eat field name
1973 break;
Matthias Braun6a441832016-12-05 06:00:36 +00001974 }
David Greene8e85b482011-10-19 13:04:43 +00001975
1976 case tgtok::paste:
1977 SMLoc PasteLoc = Lex.getLoc();
1978
1979 // Create a !strconcat() operation, first casting each operand to
1980 // a string if necessary.
1981
Sean Silvafb509ed2012-10-10 20:24:43 +00001982 TypedInit *LHS = dyn_cast<TypedInit>(Result);
David Greene8e85b482011-10-19 13:04:43 +00001983 if (!LHS) {
1984 Error(PasteLoc, "LHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00001985 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00001986 }
Craig Toppera9642b42015-05-04 01:35:39 +00001987
David Greene8e85b482011-10-19 13:04:43 +00001988 if (LHS->getType() != StringRecTy::get()) {
Nicolai Haehnle335c70f2018-03-19 14:14:04 +00001989 LHS = dyn_cast<TypedInit>(
1990 UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get())
1991 ->Fold(CurRec));
1992 if (!LHS) {
1993 Error(PasteLoc, Twine("can't cast '") + LHS->getAsString() +
1994 "' to string");
1995 return nullptr;
1996 }
David Greene8e85b482011-10-19 13:04:43 +00001997 }
1998
Craig Topper011817a2014-04-09 04:50:04 +00001999 TypedInit *RHS = nullptr;
David Greene8e85b482011-10-19 13:04:43 +00002000
2001 Lex.Lex(); // Eat the '#'.
Nicolai Haehnled34f6842018-03-06 13:49:16 +00002002 switch (Lex.getCode()) {
David Greene8e85b482011-10-19 13:04:43 +00002003 case tgtok::colon:
2004 case tgtok::semi:
2005 case tgtok::l_brace:
2006 // These are all of the tokens that can begin an object body.
2007 // Some of these can also begin values but we disallow those cases
2008 // because they are unlikely to be useful.
Craig Toppera9642b42015-05-04 01:35:39 +00002009
David Greene8e85b482011-10-19 13:04:43 +00002010 // Trailing paste, concat with an empty string.
2011 RHS = StringInit::get("");
2012 break;
2013
2014 default:
Nicolai Haehnle77841b12018-03-14 11:00:43 +00002015 Init *RHSResult = ParseValue(CurRec, nullptr, ParseNameMode);
Sean Silvafb509ed2012-10-10 20:24:43 +00002016 RHS = dyn_cast<TypedInit>(RHSResult);
David Greene8e85b482011-10-19 13:04:43 +00002017 if (!RHS) {
2018 Error(PasteLoc, "RHS of paste is not typed!");
Craig Topper011817a2014-04-09 04:50:04 +00002019 return nullptr;
David Greene8e85b482011-10-19 13:04:43 +00002020 }
2021
2022 if (RHS->getType() != StringRecTy::get()) {
Nicolai Haehnle335c70f2018-03-19 14:14:04 +00002023 RHS = dyn_cast<TypedInit>(
2024 UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get())
2025 ->Fold(CurRec));
2026 if (!RHS) {
2027 Error(PasteLoc, Twine("can't cast '") + RHS->getAsString() +
2028 "' to string");
2029 return nullptr;
2030 }
David Greene8e85b482011-10-19 13:04:43 +00002031 }
Craig Toppera9642b42015-05-04 01:35:39 +00002032
David Greene8e85b482011-10-19 13:04:43 +00002033 break;
2034 }
2035
Nicolai Haehnle1eaebc62018-03-19 14:13:54 +00002036 Result = BinOpInit::getStrConcat(LHS, RHS);
David Greene8e85b482011-10-19 13:04:43 +00002037 break;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002038 }
2039 }
2040}
2041
2042/// ParseDagArgList - Parse the argument list for a dag literal expression.
2043///
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002044/// DagArg ::= Value (':' VARNAME)?
2045/// DagArg ::= VARNAME
2046/// DagArgList ::= DagArg
2047/// DagArgList ::= DagArgList ',' DagArg
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002048void TGParser::ParseDagArgList(
2049 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
2050 Record *CurRec) {
Bob Wilson7248f862009-11-22 04:24:42 +00002051
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002052 while (true) {
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002053 // DagArg ::= VARNAME
2054 if (Lex.getCode() == tgtok::VarName) {
2055 // A missing value is treated like '?'.
Matthias Braunbb053162016-12-05 06:00:46 +00002056 StringInit *VarName = StringInit::get(Lex.getCurStrVal());
2057 Result.emplace_back(UnsetInit::get(), VarName);
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002058 Lex.Lex();
2059 } else {
2060 // DagArg ::= Value (':' VARNAME)?
2061 Init *Val = ParseValue(CurRec);
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002062 if (!Val) {
2063 Result.clear();
2064 return;
2065 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002066
2067 // If the variable name is present, add it.
Matthias Braunbb053162016-12-05 06:00:46 +00002068 StringInit *VarName = nullptr;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002069 if (Lex.getCode() == tgtok::colon) {
2070 if (Lex.Lex() != tgtok::VarName) { // eat the ':'
2071 TokError("expected variable name in dag literal");
Matthias Braun1ddb78c2016-12-05 06:41:51 +00002072 Result.clear();
2073 return;
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002074 }
Matthias Braunbb053162016-12-05 06:00:46 +00002075 VarName = StringInit::get(Lex.getCurStrVal());
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002076 Lex.Lex(); // eat the VarName.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002077 }
Jakob Stoklund Olesen91a58482013-03-24 19:36:51 +00002078
2079 Result.push_back(std::make_pair(Val, VarName));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002080 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002081 if (Lex.getCode() != tgtok::comma) break;
Bob Wilson7248f862009-11-22 04:24:42 +00002082 Lex.Lex(); // eat the ','
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002083 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002084}
2085
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002086/// ParseValueList - Parse a comma separated list of values, returning them as a
2087/// vector. Note that this always expects to be able to parse at least one
2088/// value. It returns an empty list if this is not possible.
2089///
2090/// ValueList ::= Value (',' Value)
2091///
Matthias Braunc66e7552016-12-05 06:41:54 +00002092void TGParser::ParseValueList(SmallVectorImpl<Init*> &Result, Record *CurRec,
2093 Record *ArgsRec, RecTy *EltTy) {
David Greene8618f952009-06-08 20:23:18 +00002094 RecTy *ItemType = EltTy;
David Greenefd42c8a2009-06-29 19:59:52 +00002095 unsigned int ArgN = 0;
Craig Topper011817a2014-04-09 04:50:04 +00002096 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002097 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
Alexander Kornienko8c0809c2015-01-15 11:41:30 +00002098 if (TArgs.empty()) {
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002099 TokError("template argument provided to non-template class");
Matthias Braunc66e7552016-12-05 06:41:54 +00002100 Result.clear();
2101 return;
Jim Grosbach91f5a3f2012-01-20 20:02:39 +00002102 }
David Greene8618f952009-06-08 20:23:18 +00002103 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
David Greene39db48d2011-09-19 18:26:07 +00002104 if (!RV) {
2105 errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN]
2106 << ")\n";
2107 }
David Greene8618f952009-06-08 20:23:18 +00002108 assert(RV && "Template argument record not found??");
2109 ItemType = RV->getType();
2110 ++ArgN;
2111 }
2112 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002113 if (!Result.back()) {
2114 Result.clear();
2115 return;
2116 }
Bob Wilson7248f862009-11-22 04:24:42 +00002117
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002118 while (Lex.getCode() == tgtok::comma) {
2119 Lex.Lex(); // Eat the comma
Bob Wilson7248f862009-11-22 04:24:42 +00002120
Craig Topper011817a2014-04-09 04:50:04 +00002121 if (ArgsRec && !EltTy) {
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002122 ArrayRef<Init *> TArgs = ArgsRec->getTemplateArgs();
David Greenefd42c8a2009-06-29 19:59:52 +00002123 if (ArgN >= TArgs.size()) {
2124 TokError("too many template arguments");
Matthias Braunc66e7552016-12-05 06:41:54 +00002125 Result.clear();
2126 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002127 }
David Greene8618f952009-06-08 20:23:18 +00002128 const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]);
2129 assert(RV && "Template argument record not found??");
2130 ItemType = RV->getType();
2131 ++ArgN;
2132 }
2133 Result.push_back(ParseValue(CurRec, ItemType));
Matthias Braunc66e7552016-12-05 06:41:54 +00002134 if (!Result.back()) {
2135 Result.clear();
2136 return;
2137 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002138 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002139}
2140
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002141/// ParseDeclaration - Read a declaration, returning the name of field ID, or an
2142/// empty string on error. This can happen in a number of different context's,
2143/// including within a def or in the template args for a def (which which case
2144/// CurRec will be non-null) and within the template args for a multiclass (in
2145/// which case CurRec will be null, but CurMultiClass will be set). This can
2146/// also happen within a def that is within a multiclass, which will set both
2147/// CurRec and CurMultiClass.
2148///
2149/// Declaration ::= FIELD? Type ID ('=' Value)?
2150///
David Greenedb10e692011-10-19 13:02:42 +00002151Init *TGParser::ParseDeclaration(Record *CurRec,
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002152 bool ParsingTemplateArgs) {
2153 // Read the field prefix if present.
2154 bool HasField = Lex.getCode() == tgtok::Field;
2155 if (HasField) Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002156
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002157 RecTy *Type = ParseType();
Craig Topper011817a2014-04-09 04:50:04 +00002158 if (!Type) return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002159
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002160 if (Lex.getCode() != tgtok::Id) {
2161 TokError("Expected identifier in declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002162 return nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002163 }
Bob Wilson7248f862009-11-22 04:24:42 +00002164
Chris Lattner526c8cb2009-06-21 03:39:35 +00002165 SMLoc IdLoc = Lex.getLoc();
David Greenedb10e692011-10-19 13:02:42 +00002166 Init *DeclName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002167 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002168
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002169 if (ParsingTemplateArgs) {
Craig Toppera9642b42015-05-04 01:35:39 +00002170 if (CurRec)
David Greenedb10e692011-10-19 13:02:42 +00002171 DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":");
Craig Toppera9642b42015-05-04 01:35:39 +00002172 else
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002173 assert(CurMultiClass);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002174 if (CurMultiClass)
David Greenedb10e692011-10-19 13:02:42 +00002175 DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName,
2176 "::");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002177 }
Bob Wilson7248f862009-11-22 04:24:42 +00002178
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002179 // Add the value.
2180 if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField)))
Craig Topper011817a2014-04-09 04:50:04 +00002181 return nullptr;
Bob Wilson7248f862009-11-22 04:24:42 +00002182
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002183 // If a value is present, parse it.
2184 if (Lex.getCode() == tgtok::equal) {
2185 Lex.Lex();
Chris Lattner526c8cb2009-06-21 03:39:35 +00002186 SMLoc ValLoc = Lex.getLoc();
David Greeneaf8ee2c2011-07-29 22:43:06 +00002187 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002188 if (!Val ||
Craig Toppercfd81732016-01-04 03:15:08 +00002189 SetValue(CurRec, ValLoc, DeclName, None, Val))
Pete Cooper4cc54cb2014-07-31 01:44:00 +00002190 // Return the name, even if an error is thrown. This is so that we can
2191 // continue to make some progress, even without the value having been
2192 // initialized.
2193 return DeclName;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002194 }
Bob Wilson7248f862009-11-22 04:24:42 +00002195
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002196 return DeclName;
2197}
2198
David Greenefb927af2012-02-22 16:09:41 +00002199/// ParseForeachDeclaration - Read a foreach declaration, returning
2200/// the name of the declared object or a NULL Init on error. Return
2201/// the name of the parsed initializer list through ForeachListName.
2202///
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002203/// ForeachDeclaration ::= ID '=' '{' RangeList '}'
2204/// ForeachDeclaration ::= ID '=' RangePiece
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002205/// ForeachDeclaration ::= ID '=' Value
David Greenefb927af2012-02-22 16:09:41 +00002206///
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002207VarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) {
David Greenefb927af2012-02-22 16:09:41 +00002208 if (Lex.getCode() != tgtok::Id) {
2209 TokError("Expected identifier in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002210 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002211 }
2212
2213 Init *DeclName = StringInit::get(Lex.getCurStrVal());
2214 Lex.Lex();
2215
2216 // If a value is present, parse it.
2217 if (Lex.getCode() != tgtok::equal) {
2218 TokError("Expected '=' in foreach declaration");
Craig Topper011817a2014-04-09 04:50:04 +00002219 return nullptr;
David Greenefb927af2012-02-22 16:09:41 +00002220 }
2221 Lex.Lex(); // Eat the '='
2222
Craig Topper011817a2014-04-09 04:50:04 +00002223 RecTy *IterType = nullptr;
Matthias Braunc66e7552016-12-05 06:41:54 +00002224 SmallVector<unsigned, 16> Ranges;
David Greenefb927af2012-02-22 16:09:41 +00002225
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002226 switch (Lex.getCode()) {
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002227 case tgtok::IntVal: { // RangePiece.
2228 if (ParseRangePiece(Ranges))
Craig Topper011817a2014-04-09 04:50:04 +00002229 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002230 break;
David Greenefb927af2012-02-22 16:09:41 +00002231 }
2232
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002233 case tgtok::l_brace: { // '{' RangeList '}'
2234 Lex.Lex(); // eat the '{'
Matthias Braunc66e7552016-12-05 06:41:54 +00002235 ParseRangeList(Ranges);
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002236 if (Lex.getCode() != tgtok::r_brace) {
2237 TokError("expected '}' at end of bit range list");
Craig Topper011817a2014-04-09 04:50:04 +00002238 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002239 }
2240 Lex.Lex();
2241 break;
2242 }
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002243
2244 default: {
2245 SMLoc ValueLoc = Lex.getLoc();
2246 Init *I = ParseValue(nullptr);
2247 if (!isa<ListInit>(I)) {
2248 std::string Type;
2249 if (TypedInit *TI = dyn_cast<TypedInit>(I))
2250 Type = (Twine("' of type '") + TI->getType()->getAsString()).str();
2251 Error(ValueLoc, "expected a list, got '" + I->getAsString() + Type + "'");
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002252 if (CurMultiClass)
2253 PrintNote({}, "references to multiclass template arguments cannot be "
2254 "resolved at this time");
Nicolai Haehnle8aa9d582018-03-09 12:24:30 +00002255 return nullptr;
2256 }
2257 ForeachListValue = dyn_cast<ListInit>(I);
2258 IterType = ForeachListValue->getElementType();
2259 break;
2260 }
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002261 }
David Greenefb927af2012-02-22 16:09:41 +00002262
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002263 if (!Ranges.empty()) {
2264 assert(!IterType && "Type already initialized?");
2265 IterType = IntRecTy::get();
2266 std::vector<Init*> Values;
Craig Topper8eb764c2015-06-02 06:19:28 +00002267 for (unsigned R : Ranges)
2268 Values.push_back(IntInit::get(R));
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002269 ForeachListValue = ListInit::get(Values, IterType);
2270 }
2271
2272 if (!IterType)
Craig Topper011817a2014-04-09 04:50:04 +00002273 return nullptr;
Jakob Stoklund Olesen36a5c8e2012-05-24 22:17:39 +00002274
2275 return VarInit::get(DeclName, IterType);
David Greenefb927af2012-02-22 16:09:41 +00002276}
2277
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002278/// ParseTemplateArgList - Read a template argument list, which is a non-empty
2279/// sequence of template-declarations in <>'s. If CurRec is non-null, these are
2280/// template args for a def, which may or may not be in a multiclass. If null,
2281/// these are the template args for a multiclass.
2282///
2283/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>'
Bob Wilson7248f862009-11-22 04:24:42 +00002284///
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002285bool TGParser::ParseTemplateArgList(Record *CurRec) {
2286 assert(Lex.getCode() == tgtok::less && "Not a template arg list!");
2287 Lex.Lex(); // eat the '<'
Bob Wilson7248f862009-11-22 04:24:42 +00002288
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002289 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
Bob Wilson7248f862009-11-22 04:24:42 +00002290
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002291 // Read the first declaration.
David Greenedb10e692011-10-19 13:02:42 +00002292 Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002293 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002294 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002295
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002296 TheRecToAddTo->addTemplateArg(TemplArg);
Bob Wilson7248f862009-11-22 04:24:42 +00002297
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002298 while (Lex.getCode() == tgtok::comma) {
2299 Lex.Lex(); // eat the ','
Bob Wilson7248f862009-11-22 04:24:42 +00002300
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002301 // Read the following declarations.
2302 TemplArg = ParseDeclaration(CurRec, true/*templateargs*/);
Craig Topper011817a2014-04-09 04:50:04 +00002303 if (!TemplArg)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002304 return true;
2305 TheRecToAddTo->addTemplateArg(TemplArg);
2306 }
Bob Wilson7248f862009-11-22 04:24:42 +00002307
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002308 if (Lex.getCode() != tgtok::greater)
2309 return TokError("expected '>' at end of template argument list");
2310 Lex.Lex(); // eat the '>'.
2311 return false;
2312}
2313
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002314/// ParseBodyItem - Parse a single item at within the body of a def or class.
2315///
2316/// BodyItem ::= Declaration ';'
2317/// BodyItem ::= LET ID OptionalBitList '=' Value ';'
2318bool TGParser::ParseBodyItem(Record *CurRec) {
2319 if (Lex.getCode() != tgtok::Let) {
Craig Topper011817a2014-04-09 04:50:04 +00002320 if (!ParseDeclaration(CurRec, false))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002321 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002322
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002323 if (Lex.getCode() != tgtok::semi)
2324 return TokError("expected ';' after declaration");
2325 Lex.Lex();
2326 return false;
2327 }
2328
2329 // LET ID OptionalRangeList '=' Value ';'
2330 if (Lex.Lex() != tgtok::Id)
2331 return TokError("expected field identifier after let");
Bob Wilson7248f862009-11-22 04:24:42 +00002332
Chris Lattner526c8cb2009-06-21 03:39:35 +00002333 SMLoc IdLoc = Lex.getLoc();
Matthias Braun215ff842016-12-05 07:35:13 +00002334 StringInit *FieldName = StringInit::get(Lex.getCurStrVal());
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002335 Lex.Lex(); // eat the field name.
Bob Wilson7248f862009-11-22 04:24:42 +00002336
Matthias Braunc66e7552016-12-05 06:41:54 +00002337 SmallVector<unsigned, 16> BitList;
Bob Wilson7248f862009-11-22 04:24:42 +00002338 if (ParseOptionalBitList(BitList))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002339 return true;
2340 std::reverse(BitList.begin(), BitList.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002341
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002342 if (Lex.getCode() != tgtok::equal)
2343 return TokError("expected '=' in let expression");
2344 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002345
David Greene8618f952009-06-08 20:23:18 +00002346 RecordVal *Field = CurRec->getValue(FieldName);
Craig Topper011817a2014-04-09 04:50:04 +00002347 if (!Field)
Matthias Braun215ff842016-12-05 07:35:13 +00002348 return TokError("Value '" + FieldName->getValue() + "' unknown!");
David Greene8618f952009-06-08 20:23:18 +00002349
2350 RecTy *Type = Field->getType();
Bob Wilson7248f862009-11-22 04:24:42 +00002351
David Greeneaf8ee2c2011-07-29 22:43:06 +00002352 Init *Val = ParseValue(CurRec, Type);
Craig Topper011817a2014-04-09 04:50:04 +00002353 if (!Val) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002354
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002355 if (Lex.getCode() != tgtok::semi)
2356 return TokError("expected ';' after let expression");
2357 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002358
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002359 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2360}
2361
2362/// ParseBody - Read the body of a class or def. Return true on error, false on
2363/// success.
2364///
2365/// Body ::= ';'
2366/// Body ::= '{' BodyList '}'
2367/// BodyList BodyItem*
2368///
2369bool TGParser::ParseBody(Record *CurRec) {
2370 // If this is a null definition, just eat the semi and return.
2371 if (Lex.getCode() == tgtok::semi) {
2372 Lex.Lex();
2373 return false;
2374 }
Bob Wilson7248f862009-11-22 04:24:42 +00002375
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002376 if (Lex.getCode() != tgtok::l_brace)
2377 return TokError("Expected ';' or '{' to start body");
2378 // Eat the '{'.
2379 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002380
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002381 while (Lex.getCode() != tgtok::r_brace)
2382 if (ParseBodyItem(CurRec))
2383 return true;
2384
2385 // Eat the '}'.
2386 Lex.Lex();
2387 return false;
2388}
2389
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00002390/// Apply the current let bindings to \a CurRec.
Sean Silvacb1a75e2013-01-09 04:49:14 +00002391/// \returns true on error, false otherwise.
2392bool TGParser::ApplyLetStack(Record *CurRec) {
Matthias Braunc66e7552016-12-05 06:41:54 +00002393 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
Craig Topper8eb764c2015-06-02 06:19:28 +00002394 for (LetRecord &LR : LetInfo)
2395 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002396 return true;
2397 return false;
2398}
2399
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002400/// ParseObjectBody - Parse the body of a def or class. This consists of an
2401/// optional ClassList followed by a Body. CurRec is the current def or class
2402/// that is being parsed.
2403///
2404/// ObjectBody ::= BaseClassList Body
2405/// BaseClassList ::= /*empty*/
2406/// BaseClassList ::= ':' BaseClassListNE
2407/// BaseClassListNE ::= SubClassRef (',' SubClassRef)*
2408///
2409bool TGParser::ParseObjectBody(Record *CurRec) {
2410 // If there is a baseclass list, read it.
2411 if (Lex.getCode() == tgtok::colon) {
2412 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002413
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002414 // Read all of the subclasses.
2415 SubClassReference SubClass = ParseSubClassReference(CurRec, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002416 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002417 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002418 if (!SubClass.Rec) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002419
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002420 // Add it.
2421 if (AddSubClass(CurRec, SubClass))
2422 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002423
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002424 if (Lex.getCode() != tgtok::comma) break;
2425 Lex.Lex(); // eat ','.
2426 SubClass = ParseSubClassReference(CurRec, false);
2427 }
2428 }
2429
Sean Silvacb1a75e2013-01-09 04:49:14 +00002430 if (ApplyLetStack(CurRec))
2431 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002432
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002433 return ParseBody(CurRec);
2434}
2435
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002436/// ParseDef - Parse and return a top level or multiclass def, return the record
2437/// corresponding to it. This returns null on error.
2438///
2439/// DefInst ::= DEF ObjectName ObjectBody
2440///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002441bool TGParser::ParseDef(MultiClass *CurMultiClass) {
Chris Lattner526c8cb2009-06-21 03:39:35 +00002442 SMLoc DefLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002443 assert(Lex.getCode() == tgtok::Def && "Unknown tok");
Bob Wilson7248f862009-11-22 04:24:42 +00002444 Lex.Lex(); // Eat the 'def' token.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002445
2446 // Parse ObjectName and make a record for it.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002447 std::unique_ptr<Record> CurRec;
Jordan Roseabdd99b2013-01-10 18:50:05 +00002448 Init *Name = ParseObjectName(CurMultiClass);
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002449 if (!Name)
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002450 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002451
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002452 if (isa<UnsetInit>(Name))
2453 CurRec = make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
2454 /*Anonymous=*/true);
2455 else
2456 CurRec = make_unique<Record>(Name, DefLoc, Records);
Bob Wilson7248f862009-11-22 04:24:42 +00002457
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002458 if (ParseObjectBody(CurRec.get()))
2459 return true;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002460
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002461 return addDef(std::move(CurRec), nullptr);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002462}
2463
2464/// ParseDefset - Parse a defset statement.
2465///
2466/// Defset ::= DEFSET Type Id '=' '{' ObjectList '}'
2467///
2468bool TGParser::ParseDefset() {
2469 assert(Lex.getCode() == tgtok::Defset);
2470 Lex.Lex(); // Eat the 'defset' token
2471
2472 DefsetRecord Defset;
2473 Defset.Loc = Lex.getLoc();
2474 RecTy *Type = ParseType();
2475 if (!Type)
2476 return true;
2477 if (!isa<ListRecTy>(Type))
2478 return Error(Defset.Loc, "expected list type");
2479 Defset.EltTy = cast<ListRecTy>(Type)->getElementType();
2480
2481 if (Lex.getCode() != tgtok::Id)
2482 return TokError("expected identifier");
2483 StringInit *DeclName = StringInit::get(Lex.getCurStrVal());
2484 if (Records.getGlobal(DeclName->getValue()))
2485 return TokError("def or global variable of this name already exists");
2486
2487 if (Lex.Lex() != tgtok::equal) // Eat the identifier
2488 return TokError("expected '='");
2489 if (Lex.Lex() != tgtok::l_brace) // Eat the '='
2490 return TokError("expected '{'");
2491 SMLoc BraceLoc = Lex.getLoc();
2492 Lex.Lex(); // Eat the '{'
2493
2494 Defsets.push_back(&Defset);
2495 bool Err = ParseObjectList(nullptr);
2496 Defsets.pop_back();
2497 if (Err)
2498 return true;
2499
2500 if (Lex.getCode() != tgtok::r_brace) {
2501 TokError("expected '}' at end of defset");
2502 return Error(BraceLoc, "to match this '{'");
2503 }
2504 Lex.Lex(); // Eat the '}'
2505
2506 Records.addExtraGlobal(DeclName->getValue(),
2507 ListInit::get(Defset.Elements, Defset.EltTy));
2508 return false;
2509}
2510
David Greenefb927af2012-02-22 16:09:41 +00002511/// ParseForeach - Parse a for statement. Return the record corresponding
2512/// to it. This returns true on error.
2513///
2514/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}'
2515/// Foreach ::= FOREACH Declaration IN Object
2516///
2517bool TGParser::ParseForeach(MultiClass *CurMultiClass) {
2518 assert(Lex.getCode() == tgtok::Foreach && "Unknown tok");
2519 Lex.Lex(); // Eat the 'for' token.
2520
2521 // Make a temporary object to record items associated with the for
2522 // loop.
Craig Topper011817a2014-04-09 04:50:04 +00002523 ListInit *ListValue = nullptr;
Jakob Stoklund Olesen8a120b12012-05-24 22:17:33 +00002524 VarInit *IterName = ParseForeachDeclaration(ListValue);
Craig Topper011817a2014-04-09 04:50:04 +00002525 if (!IterName)
David Greenefb927af2012-02-22 16:09:41 +00002526 return TokError("expected declaration in for");
2527
2528 if (Lex.getCode() != tgtok::In)
2529 return TokError("Unknown tok");
2530 Lex.Lex(); // Eat the in
2531
2532 // Create a loop object and remember it.
2533 Loops.push_back(ForeachLoop(IterName, ListValue));
2534
2535 if (Lex.getCode() != tgtok::l_brace) {
2536 // FOREACH Declaration IN Object
2537 if (ParseObject(CurMultiClass))
2538 return true;
Craig Toppera9642b42015-05-04 01:35:39 +00002539 } else {
David Greenefb927af2012-02-22 16:09:41 +00002540 SMLoc BraceLoc = Lex.getLoc();
2541 // Otherwise, this is a group foreach.
2542 Lex.Lex(); // eat the '{'.
2543
2544 // Parse the object list.
2545 if (ParseObjectList(CurMultiClass))
2546 return true;
2547
2548 if (Lex.getCode() != tgtok::r_brace) {
2549 TokError("expected '}' at end of foreach command");
2550 return Error(BraceLoc, "to match this '{'");
2551 }
2552 Lex.Lex(); // Eat the }
2553 }
2554
2555 // We've processed everything in this loop.
2556 Loops.pop_back();
2557
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002558 return false;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002559}
2560
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002561/// ParseClass - Parse a tblgen class definition.
2562///
2563/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody
2564///
2565bool TGParser::ParseClass() {
2566 assert(Lex.getCode() == tgtok::Class && "Unexpected token!");
2567 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002568
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002569 if (Lex.getCode() != tgtok::Id)
2570 return TokError("expected class name after 'class' keyword");
Bob Wilson7248f862009-11-22 04:24:42 +00002571
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002572 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2573 if (CurRec) {
2574 // If the body was previously defined, this is an error.
David Greened6991612011-10-19 13:04:13 +00002575 if (CurRec->getValues().size() > 1 || // Account for NAME.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002576 !CurRec->getSuperClasses().empty() ||
2577 !CurRec->getTemplateArgs().empty())
Craig Topper85c07002015-04-30 05:54:22 +00002578 return TokError("Class '" + CurRec->getNameInitAsString() +
2579 "' already defined");
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002580 } else {
2581 // If this is the first reference to this class, create and add it.
Hans Wennborgffbbd532014-11-30 00:31:49 +00002582 auto NewRec =
2583 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records);
Craig Toppercdab2322014-11-29 05:52:51 +00002584 CurRec = NewRec.get();
2585 Records.addClass(std::move(NewRec));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002586 }
2587 Lex.Lex(); // eat the name.
Bob Wilson7248f862009-11-22 04:24:42 +00002588
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002589 // If there are template args, parse them.
2590 if (Lex.getCode() == tgtok::less)
2591 if (ParseTemplateArgList(CurRec))
2592 return true;
2593
2594 // Finally, parse the object body.
2595 return ParseObjectBody(CurRec);
2596}
2597
2598/// ParseLetList - Parse a non-empty list of assignment expressions into a list
2599/// of LetRecords.
2600///
2601/// LetList ::= LetItem (',' LetItem)*
2602/// LetItem ::= ID OptionalRangeList '=' Value
2603///
Matthias Braunc66e7552016-12-05 06:41:54 +00002604void TGParser::ParseLetList(SmallVectorImpl<LetRecord> &Result) {
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002605 while (true) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002606 if (Lex.getCode() != tgtok::Id) {
2607 TokError("expected identifier in let definition");
Matthias Braunc66e7552016-12-05 06:41:54 +00002608 Result.clear();
2609 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002610 }
Matthias Braunc66e7552016-12-05 06:41:54 +00002611
Matthias Braun215ff842016-12-05 07:35:13 +00002612 StringInit *Name = StringInit::get(Lex.getCurStrVal());
Chris Lattner526c8cb2009-06-21 03:39:35 +00002613 SMLoc NameLoc = Lex.getLoc();
Bob Wilson7248f862009-11-22 04:24:42 +00002614 Lex.Lex(); // Eat the identifier.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002615
2616 // Check for an optional RangeList.
Matthias Braunc66e7552016-12-05 06:41:54 +00002617 SmallVector<unsigned, 16> Bits;
2618 if (ParseOptionalRangeList(Bits)) {
2619 Result.clear();
2620 return;
2621 }
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002622 std::reverse(Bits.begin(), Bits.end());
Bob Wilson7248f862009-11-22 04:24:42 +00002623
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002624 if (Lex.getCode() != tgtok::equal) {
2625 TokError("expected '=' in let expression");
Matthias Braunc66e7552016-12-05 06:41:54 +00002626 Result.clear();
2627 return;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002628 }
2629 Lex.Lex(); // eat the '='.
Bob Wilson7248f862009-11-22 04:24:42 +00002630
Craig Topper011817a2014-04-09 04:50:04 +00002631 Init *Val = ParseValue(nullptr);
Matthias Braunc66e7552016-12-05 06:41:54 +00002632 if (!Val) {
2633 Result.clear();
2634 return;
2635 }
Bob Wilson7248f862009-11-22 04:24:42 +00002636
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002637 // Now that we have everything, add the record.
Matthias Braun215ff842016-12-05 07:35:13 +00002638 Result.emplace_back(Name, Bits, Val, NameLoc);
Bob Wilson7248f862009-11-22 04:24:42 +00002639
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002640 if (Lex.getCode() != tgtok::comma)
Matthias Braunc66e7552016-12-05 06:41:54 +00002641 return;
Bob Wilson7248f862009-11-22 04:24:42 +00002642 Lex.Lex(); // eat the comma.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002643 }
2644}
2645
2646/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002647/// different related productions. This works inside multiclasses too.
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002648///
2649/// Object ::= LET LetList IN '{' ObjectList '}'
2650/// Object ::= LET LetList IN Object
2651///
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002652bool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002653 assert(Lex.getCode() == tgtok::Let && "Unexpected token");
2654 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002655
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002656 // Add this entry to the let stack.
Matthias Braunc66e7552016-12-05 06:41:54 +00002657 SmallVector<LetRecord, 8> LetInfo;
2658 ParseLetList(LetInfo);
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002659 if (LetInfo.empty()) return true;
Benjamin Kramere12a6ba2014-10-03 18:33:16 +00002660 LetStack.push_back(std::move(LetInfo));
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002661
2662 if (Lex.getCode() != tgtok::In)
2663 return TokError("expected 'in' at end of top-level 'let'");
2664 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002665
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002666 // If this is a scalar let, just handle it now
2667 if (Lex.getCode() != tgtok::l_brace) {
2668 // LET LetList IN Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002669 if (ParseObject(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002670 return true;
2671 } else { // Object ::= LETCommand '{' ObjectList '}'
Chris Lattner526c8cb2009-06-21 03:39:35 +00002672 SMLoc BraceLoc = Lex.getLoc();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002673 // Otherwise, this is a group let.
2674 Lex.Lex(); // eat the '{'.
Bob Wilson7248f862009-11-22 04:24:42 +00002675
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002676 // Parse the object list.
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002677 if (ParseObjectList(CurMultiClass))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002678 return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002679
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002680 if (Lex.getCode() != tgtok::r_brace) {
2681 TokError("expected '}' at end of top level let command");
2682 return Error(BraceLoc, "to match this '{'");
2683 }
2684 Lex.Lex();
2685 }
Bob Wilson7248f862009-11-22 04:24:42 +00002686
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002687 // Outside this let scope, this let block is not active.
2688 LetStack.pop_back();
2689 return false;
2690}
2691
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002692/// ParseMultiClass - Parse a multiclass definition.
2693///
Bob Wilson3d948162009-04-28 19:41:44 +00002694/// MultiClassInst ::= MULTICLASS ID TemplateArgList?
Sean Silvac95fe282013-01-09 02:11:55 +00002695/// ':' BaseMultiClassList '{' MultiClassObject+ '}'
2696/// MultiClassObject ::= DefInst
2697/// MultiClassObject ::= MultiClassInst
2698/// MultiClassObject ::= DefMInst
2699/// MultiClassObject ::= LETCommand '{' ObjectList '}'
2700/// MultiClassObject ::= LETCommand Object
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002701///
2702bool TGParser::ParseMultiClass() {
2703 assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token");
2704 Lex.Lex(); // Eat the multiclass token.
2705
2706 if (Lex.getCode() != tgtok::Id)
2707 return TokError("expected identifier after multiclass for name");
2708 std::string Name = Lex.getCurStrVal();
Bob Wilson7248f862009-11-22 04:24:42 +00002709
Craig Topper7adf2bf2014-12-11 05:25:30 +00002710 auto Result =
2711 MultiClasses.insert(std::make_pair(Name,
2712 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2713
2714 if (!Result.second)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002715 return TokError("multiclass '" + Name + "' already defined");
Bob Wilson7248f862009-11-22 04:24:42 +00002716
Craig Topper7adf2bf2014-12-11 05:25:30 +00002717 CurMultiClass = Result.first->second.get();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002718 Lex.Lex(); // Eat the identifier.
Bob Wilson7248f862009-11-22 04:24:42 +00002719
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002720 // If there are template args, parse them.
2721 if (Lex.getCode() == tgtok::less)
Craig Topper011817a2014-04-09 04:50:04 +00002722 if (ParseTemplateArgList(nullptr))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002723 return true;
2724
David Greene7049e792009-04-24 16:55:41 +00002725 bool inherits = false;
2726
David Greene753ed8f2009-04-22 16:42:54 +00002727 // If there are submulticlasses, parse them.
2728 if (Lex.getCode() == tgtok::colon) {
David Greene7049e792009-04-24 16:55:41 +00002729 inherits = true;
2730
David Greene753ed8f2009-04-22 16:42:54 +00002731 Lex.Lex();
Bob Wilson3d948162009-04-28 19:41:44 +00002732
David Greene753ed8f2009-04-22 16:42:54 +00002733 // Read all of the submulticlasses.
Bob Wilson3d948162009-04-28 19:41:44 +00002734 SubMultiClassReference SubMultiClass =
2735 ParseSubMultiClassReference(CurMultiClass);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002736 while (true) {
David Greene753ed8f2009-04-22 16:42:54 +00002737 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002738 if (!SubMultiClass.MC) return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002739
David Greene753ed8f2009-04-22 16:42:54 +00002740 // Add it.
2741 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2742 return true;
Bob Wilson3d948162009-04-28 19:41:44 +00002743
David Greene753ed8f2009-04-22 16:42:54 +00002744 if (Lex.getCode() != tgtok::comma) break;
2745 Lex.Lex(); // eat ','.
2746 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2747 }
2748 }
2749
David Greene7049e792009-04-24 16:55:41 +00002750 if (Lex.getCode() != tgtok::l_brace) {
2751 if (!inherits)
2752 return TokError("expected '{' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002753 if (Lex.getCode() != tgtok::semi)
Bob Wilson7248f862009-11-22 04:24:42 +00002754 return TokError("expected ';' in multiclass definition");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002755 Lex.Lex(); // eat the ';'.
Bob Wilson7248f862009-11-22 04:24:42 +00002756 } else {
David Greene7049e792009-04-24 16:55:41 +00002757 if (Lex.Lex() == tgtok::r_brace) // eat the '{'.
2758 return TokError("multiclass must contain at least one def");
Bob Wilson7248f862009-11-22 04:24:42 +00002759
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002760 while (Lex.getCode() != tgtok::r_brace) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002761 switch (Lex.getCode()) {
Craig Topper73e2c0d2014-11-29 16:05:27 +00002762 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002763 return TokError("expected 'let', 'def', 'defm' or 'foreach' in "
2764 "multiclass body");
Craig Topper73e2c0d2014-11-29 16:05:27 +00002765 case tgtok::Let:
2766 case tgtok::Def:
2767 case tgtok::Defm:
2768 case tgtok::Foreach:
2769 if (ParseObject(CurMultiClass))
2770 return true;
2771 break;
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002772 }
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002773 }
David Greene7049e792009-04-24 16:55:41 +00002774 Lex.Lex(); // eat the '}'.
2775 }
Bob Wilson7248f862009-11-22 04:24:42 +00002776
Craig Topper011817a2014-04-09 04:50:04 +00002777 CurMultiClass = nullptr;
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002778 return false;
2779}
2780
2781/// ParseDefm - Parse the instantiation of a multiclass.
2782///
2783/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';'
2784///
Bruno Cardoso Lopesc4f61482010-06-05 02:11:52 +00002785bool TGParser::ParseDefm(MultiClass *CurMultiClass) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002786 assert(Lex.getCode() == tgtok::Defm && "Unexpected token!");
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002787 Lex.Lex(); // eat the defm
David Greene2affd672011-10-19 13:04:29 +00002788
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002789 Init *DefmName = ParseObjectName(CurMultiClass);
2790 if (!DefmName)
2791 return true;
2792 if (isa<UnsetInit>(DefmName))
2793 DefmName = Records.getNewAnonymousName();
Mikhail Glushenkovde683892010-10-23 07:32:37 +00002794
Chris Lattner7538ed82010-10-05 22:51:56 +00002795 if (Lex.getCode() != tgtok::colon)
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002796 return TokError("expected ':' after defm identifier");
Bob Wilson7248f862009-11-22 04:24:42 +00002797
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002798 // Keep track of the new generated record definitions.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002799 SmallVector<std::unique_ptr<Record>, 8> NewRecDefs;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002800
2801 // This record also inherits from a regular class (non-multiclass)?
2802 bool InheritFromClass = false;
2803
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002804 // eat the colon.
2805 Lex.Lex();
2806
Chris Lattner526c8cb2009-06-21 03:39:35 +00002807 SMLoc SubClassLoc = Lex.getLoc();
Craig Topper011817a2014-04-09 04:50:04 +00002808 SubClassReference Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002809
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002810 while (true) {
Craig Topper011817a2014-04-09 04:50:04 +00002811 if (!Ref.Rec) return true;
David Greenef00919a2009-04-22 22:17:51 +00002812
2813 // To instantiate a multiclass, we need to first get the multiclass, then
2814 // instantiate each def contained in the multiclass with the SubClassRef
2815 // template parameters.
Craig Topper7adf2bf2014-12-11 05:25:30 +00002816 MultiClass *MC = MultiClasses[Ref.Rec->getName()].get();
Craig Topper4ca40012014-11-30 01:20:17 +00002817 assert(MC && "Didn't lookup multiclass correctly?");
Matthias Braunc66e7552016-12-05 06:41:54 +00002818 ArrayRef<Init*> TemplateVals = Ref.TemplateArgs;
David Greenef00919a2009-04-22 22:17:51 +00002819
2820 // Verify that the correct number of template arguments were specified.
Benjamin Kramer7ecf8c22015-10-24 12:46:45 +00002821 ArrayRef<Init *> TArgs = MC->Rec.getTemplateArgs();
David Greenef00919a2009-04-22 22:17:51 +00002822 if (TArgs.size() < TemplateVals.size())
2823 return Error(SubClassLoc,
2824 "more template args specified than multiclass expects");
2825
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002826 DenseMap<Init *, Init *> TemplateArgs;
2827 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2828 if (i < TemplateVals.size()) {
2829 TemplateArgs.insert({TArgs[i], TemplateVals[i]});
2830 } else {
2831 Init *Default = MC->Rec.getValue(TArgs[i])->getValue();
2832 if (!Default->isComplete()) {
2833 return Error(SubClassLoc,
2834 "value not specified for template argument #" +
2835 Twine(i) + " (" + TArgs[i]->getAsUnquotedString() +
2836 ") of multiclass '" + MC->Rec.getNameInitAsString() +
2837 "'");
2838 }
2839 TemplateArgs.insert({TArgs[i], Default});
2840 }
David Greenef00919a2009-04-22 22:17:51 +00002841 }
2842
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002843 // Loop over all the def's in the multiclass, instantiating each one.
2844 for (const std::unique_ptr<Record> &DefProto : MC->DefPrototypes) {
2845 bool ResolveName = true;
2846 auto CurRec = make_unique<Record>(*DefProto);
2847 CurRec->appendLoc(SubClassLoc);
2848
2849 if (StringInit *NameString =
2850 dyn_cast<StringInit>(CurRec->getNameInit())) {
2851 // We have a fully expanded string so there are no operators to
2852 // resolve. We should concatenate the given prefix and name.
2853 //
2854 // TODO: This MUST happen before template argument resolution. This
2855 // does not make sense and should be changed, but at the time of
2856 // writing, there are existing .td files which rely on this
2857 // implementation detail. It's a bad idea and should be fixed.
2858 // See test/TableGen/name-resolution-consistency.td for some
2859 // examples.
2860 CurRec->setName(BinOpInit::getStrConcat(DefmName, NameString));
2861 ResolveName = false;
2862 }
2863
2864 MapResolver R(CurRec.get());
2865
2866 if (ResolveName) {
2867 // If the proto's name wasn't resolved, we probably have a reference to
2868 // NAME and need to replace it.
2869 //
2870 // TODO: Whether the name is resolved is basically determined by magic.
2871 // Unfortunately, existing .td files depend on it.
2872 R.set(StringInit::get("NAME"), DefmName);
2873 }
2874
2875 for (const auto &TArg : TemplateArgs)
2876 R.set(TArg.first, TArg.second);
2877
2878 CurRec->resolveReferences(R);
2879
2880 NewRecDefs.emplace_back(std::move(CurRec));
2881 }
David Greenedb445972011-10-05 22:42:07 +00002882
David Greenef00919a2009-04-22 22:17:51 +00002883 if (Lex.getCode() != tgtok::comma) break;
2884 Lex.Lex(); // eat ','.
2885
Craig Topper998a39a2013-08-20 04:22:09 +00002886 if (Lex.getCode() != tgtok::Id)
2887 return TokError("expected identifier");
2888
David Greenef00919a2009-04-22 22:17:51 +00002889 SubClassLoc = Lex.getLoc();
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002890
2891 // A defm can inherit from regular classes (non-multiclass) as
2892 // long as they come in the end of the inheritance list.
Craig Topper011817a2014-04-09 04:50:04 +00002893 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != nullptr);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002894
2895 if (InheritFromClass)
2896 break;
2897
Craig Topper011817a2014-04-09 04:50:04 +00002898 Ref = ParseSubClassReference(nullptr, true);
David Greenef00919a2009-04-22 22:17:51 +00002899 }
2900
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002901 if (InheritFromClass) {
2902 // Process all the classes to inherit as if they were part of a
2903 // regular 'def' and inherit all record values.
Craig Topper011817a2014-04-09 04:50:04 +00002904 SubClassReference SubClass = ParseSubClassReference(nullptr, false);
Eugene Zelenko33d7b762016-08-23 17:14:32 +00002905 while (true) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002906 // Check for error.
Craig Topper011817a2014-04-09 04:50:04 +00002907 if (!SubClass.Rec) return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002908
2909 // Get the expanded definition prototypes and teach them about
2910 // the record values the current class to inherit has
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002911 for (const auto &CurRec : NewRecDefs) {
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002912 // Add it.
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002913 if (AddSubClass(CurRec.get(), SubClass))
Sean Silvacb1a75e2013-01-09 04:49:14 +00002914 return true;
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002915 }
2916
2917 if (Lex.getCode() != tgtok::comma) break;
2918 Lex.Lex(); // eat ','.
Craig Topper011817a2014-04-09 04:50:04 +00002919 SubClass = ParseSubClassReference(nullptr, false);
Bruno Cardoso Lopes23f83212010-06-18 19:53:41 +00002920 }
2921 }
2922
Nicolai Haehnle420e28c2018-03-21 17:12:53 +00002923 for (auto &CurRec : NewRecDefs) {
2924 if (ApplyLetStack(CurRec.get()))
2925 return true;
2926
2927 addDef(std::move(CurRec), DefmName);
Nicolai Haehnle0f529882018-03-06 13:48:47 +00002928 }
Bruno Cardoso Lopesdc883cf2010-06-22 20:30:50 +00002929
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002930 if (Lex.getCode() != tgtok::semi)
2931 return TokError("expected ';' at end of defm");
2932 Lex.Lex();
Bob Wilson7248f862009-11-22 04:24:42 +00002933
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002934 return false;
2935}
2936
2937/// ParseObject
2938/// Object ::= ClassInst
2939/// Object ::= DefInst
2940/// Object ::= MultiClassInst
2941/// Object ::= DefMInst
2942/// Object ::= LETCommand '{' ObjectList '}'
2943/// Object ::= LETCommand Object
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002944bool TGParser::ParseObject(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002945 switch (Lex.getCode()) {
Chris Lattnerd6890262010-10-31 19:27:15 +00002946 default:
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002947 return TokError("Expected class, def, defm, defset, multiclass, let or "
2948 "foreach");
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002949 case tgtok::Let: return ParseTopLevelLet(MC);
2950 case tgtok::Def: return ParseDef(MC);
David Greenefb927af2012-02-22 16:09:41 +00002951 case tgtok::Foreach: return ParseForeach(MC);
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002952 case tgtok::Defm: return ParseDefm(MC);
Nicolai Haehnlefcd65252018-03-09 12:24:42 +00002953 case tgtok::Defset:
2954 if (MC)
2955 return TokError("defset is not allowed inside multiclass");
2956 return ParseDefset();
Nicolai Haehnlea511ddd2018-03-14 11:01:01 +00002957 case tgtok::Class:
2958 if (MC)
2959 return TokError("class is not allowed inside multiclass");
2960 if (!Loops.empty())
2961 return TokError("class is not allowed inside foreach loop");
2962 return ParseClass();
2963 case tgtok::MultiClass:
2964 if (!Loops.empty())
2965 return TokError("multiclass is not allowed inside foreach loop");
2966 return ParseMultiClass();
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002967 }
2968}
2969
2970/// ParseObjectList
2971/// ObjectList :== Object*
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002972bool TGParser::ParseObjectList(MultiClass *MC) {
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002973 while (isObjectStart(Lex.getCode())) {
Bruno Cardoso Lopes5f2adcc2010-06-10 02:42:59 +00002974 if (ParseObject(MC))
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002975 return true;
2976 }
2977 return false;
2978}
2979
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002980bool TGParser::ParseFile() {
2981 Lex.Lex(); // Prime the lexer.
2982 if (ParseObjectList()) return true;
Bob Wilson7248f862009-11-22 04:24:42 +00002983
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002984 // If we have unread input at the end of the file, report it.
2985 if (Lex.getCode() == tgtok::Eof)
2986 return false;
Bob Wilson7248f862009-11-22 04:24:42 +00002987
Chris Lattnerf4127dd2007-11-22 20:49:04 +00002988 return TokError("Unexpected input at top level");
2989}