blob: 4dbd84ab78dd8f7ae533c42b610f3504d214a031 [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
John Criswellaefb6662003-10-21 15:29:18 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +00009//
10// This file implements the bison parser for Table Generator files...
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattnere62c1182002-12-02 01:23:04 +000013
14%{
15#include "Record.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000016#include "llvm/ADT/StringExtras.h"
Chris Lattnere62c1182002-12-02 01:23:04 +000017#include <algorithm>
Chris Lattnerfc06bf02003-07-30 04:26:44 +000018#include <cstdio>
Chris Lattnere62c1182002-12-02 01:23:04 +000019#define YYERROR_VERBOSE 1
20
21int yyerror(const char *ErrorMsg);
22int yylex();
Brian Gaeked0fde302003-11-11 22:41:34 +000023
24namespace llvm {
25
Chris Lattnere62c1182002-12-02 01:23:04 +000026extern int Filelineno;
Chris Lattnere62c1182002-12-02 01:23:04 +000027static Record *CurRec = 0;
Chris Lattner7dda3952005-04-19 03:36:21 +000028static bool ParsingTemplateArgs = false;
Chris Lattnere62c1182002-12-02 01:23:04 +000029
30typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
31
Chris Lattner42aa89e2003-08-04 04:56:53 +000032struct LetRecord {
Chris Lattner60094252003-08-03 13:58:01 +000033 std::string Name;
34 std::vector<unsigned> Bits;
35 Init *Value;
36 bool HasBits;
Chris Lattner42aa89e2003-08-04 04:56:53 +000037 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
Chris Lattner60094252003-08-03 13:58:01 +000038 : Name(N), Value(V), HasBits(B != 0) {
39 if (HasBits) Bits = *B;
40 }
41};
42
Chris Lattner42aa89e2003-08-04 04:56:53 +000043static std::vector<std::vector<LetRecord> > LetStack;
Chris Lattner60094252003-08-03 13:58:01 +000044
Chris Lattnere62c1182002-12-02 01:23:04 +000045
Chris Lattner7dff0532003-07-30 20:56:47 +000046extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000047
48static void addValue(const RecordVal &RV) {
Chris Lattner60094252003-08-03 13:58:01 +000049 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
50 // The value already exists in the class, treat this as a set...
51 if (ERV->setValue(RV.getValue())) {
52 err() << "New definition of '" << RV.getName() << "' of type '"
53 << *RV.getType() << "' is incompatible with previous "
54 << "definition of type '" << *ERV->getType() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000055 exit(1);
Chris Lattner60094252003-08-03 13:58:01 +000056 }
57 } else {
58 CurRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000059 }
Chris Lattnere62c1182002-12-02 01:23:04 +000060}
61
62static void addSuperClass(Record *SC) {
63 if (CurRec->isSubClassOf(SC)) {
64 err() << "Already subclass of '" << SC->getName() << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000065 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000066 }
67 CurRec->addSuperClass(SC);
68}
69
70static void setValue(const std::string &ValName,
71 std::vector<unsigned> *BitList, Init *V) {
Chris Lattner81d50ad2004-02-28 17:31:28 +000072 if (!V) return;
Chris Lattnere62c1182002-12-02 01:23:04 +000073
74 RecordVal *RV = CurRec->getValue(ValName);
75 if (RV == 0) {
76 err() << "Value '" << ValName << "' unknown!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000077 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000078 }
Chris Lattner81d50ad2004-02-28 17:31:28 +000079
80 // Do not allow assignments like 'X = X'. This will just cause infinite loops
81 // in the resolution machinery.
82 if (!BitList)
83 if (VarInit *VI = dynamic_cast<VarInit*>(V))
84 if (VI->getName() == ValName)
85 return;
Chris Lattnere62c1182002-12-02 01:23:04 +000086
87 // If we are assigning to a subset of the bits in the value... then we must be
88 // assigning to a field of BitsRecTy, which must have a BitsInit
89 // initializer...
90 //
91 if (BitList) {
92 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
93 if (CurVal == 0) {
94 err() << "Value '" << ValName << "' is not a bits type!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +000095 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +000096 }
97
98 // Convert the incoming value to a bits type of the appropriate size...
99 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
100 if (BI == 0) {
101 V->convertInitializerTo(new BitsRecTy(BitList->size()));
102 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000103 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000104 }
105
106 // We should have a BitsInit type now...
107 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
108 BitsInit *BInit = (BitsInit*)BI;
109
110 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
111
Chris Lattnerade0de92002-12-06 03:55:39 +0000112 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000113 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
114 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000115 if (NewVal->getBit(Bit)) {
116 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000117 << "' more than once!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000118 exit(1);
Chris Lattnerade0de92002-12-06 03:55:39 +0000119 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000120 NewVal->setBit(Bit, BInit->getBit(i));
121 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000122
123 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
124 if (NewVal->getBit(i) == 0)
125 NewVal->setBit(i, CurVal->getBit(i));
126
Chris Lattnere62c1182002-12-02 01:23:04 +0000127 V = NewVal;
128 }
129
130 if (RV->setValue(V)) {
131 err() << "Value '" << ValName << "' of type '" << *RV->getType()
132 << "' is incompatible with initializer '" << *V << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000133 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000134 }
135}
136
Chris Lattner7dda3952005-04-19 03:36:21 +0000137// addSubClass - Add SC as a subclass to CurRec, resolving TemplateArgs as SC's
138// template arguments.
Chris Lattnere62c1182002-12-02 01:23:04 +0000139static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
140 // Add all of the values in the subclass into the current class...
141 const std::vector<RecordVal> &Vals = SC->getValues();
142 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
143 addValue(Vals[i]);
144
145 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
146
147 // Ensure that an appropriate number of template arguments are specified...
148 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000149 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000150 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000151 } else { // This class expects template arguments...
152 // Loop over all of the template arguments, setting them to the specified
Chris Lattner7dda3952005-04-19 03:36:21 +0000153 // value or leaving them as the default if necessary.
Chris Lattnere62c1182002-12-02 01:23:04 +0000154 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
155 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
156 // Set it now.
157 setValue(TArgs[i], 0, TemplateArgs[i]);
Chris Lattner7dda3952005-04-19 03:36:21 +0000158
159 // Resolve it next.
160 CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i]));
161
162
163 // Now remove it.
164 CurRec->removeValue(TArgs[i]);
165
Chris Lattnere62c1182002-12-02 01:23:04 +0000166 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
167 err() << "ERROR: Value not specified for template argument #"
168 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
169 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000170 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000171 }
172 }
173 }
174
Chris Lattnere62c1182002-12-02 01:23:04 +0000175 // Since everything went well, we can now set the "superclass" list for the
176 // current record.
Chris Lattner81d50ad2004-02-28 17:31:28 +0000177 const std::vector<Record*> &SCs = SC->getSuperClasses();
Chris Lattnere62c1182002-12-02 01:23:04 +0000178 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
179 addSuperClass(SCs[i]);
180 addSuperClass(SC);
181}
182
Brian Gaeked0fde302003-11-11 22:41:34 +0000183} // End llvm namespace
184
185using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000186
187%}
188
189%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000190 std::string* StrVal;
191 int IntVal;
192 llvm::RecTy* Ty;
193 llvm::Init* Initializer;
194 std::vector<llvm::Init*>* FieldList;
195 std::vector<unsigned>* BitList;
196 llvm::Record* Rec;
197 SubClassRefTy* SubClassRef;
198 std::vector<SubClassRefTy>* SubClassList;
199 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000200};
201
Chris Lattner42aa89e2003-08-04 04:56:53 +0000202%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
Chris Lattnerb9266f82005-04-19 01:11:03 +0000203%token SHLTOK SRATOK SRLTOK
Chris Lattnere62c1182002-12-02 01:23:04 +0000204%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000205%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000206
207%type <Ty> Type
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000208%type <Rec> ClassInst DefInst Object ObjectBody ClassID
Chris Lattnere62c1182002-12-02 01:23:04 +0000209
210%type <SubClassRef> SubClassRef
211%type <SubClassList> ClassList ClassListNE
212%type <IntVal> OptPrefix
213%type <Initializer> Value OptValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000214%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000215%type <FieldList> ValueList ValueListNE
216%type <BitList> BitList OptBitList RBitList
Chris Lattner91290d72003-08-10 22:14:13 +0000217%type <StrVal> Declaration OptID OptVarName
Chris Lattnere62c1182002-12-02 01:23:04 +0000218
219%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000220
Chris Lattnere62c1182002-12-02 01:23:04 +0000221%%
222
223ClassID : ID {
224 $$ = Records.getClass(*$1);
225 if ($$ == 0) {
226 err() << "Couldn't find class '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000227 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000228 }
229 delete $1;
230 };
231
Chris Lattnere62c1182002-12-02 01:23:04 +0000232
233// TableGen types...
234Type : STRING { // string type
235 $$ = new StringRecTy();
236 } | BIT { // bit type
237 $$ = new BitRecTy();
238 } | BITS '<' INTVAL '>' { // bits<x> type
239 $$ = new BitsRecTy($3);
240 } | INT { // int type
241 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000242 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000243 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000244 } | CODE { // code type
245 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000246 } | DAG { // dag type
247 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000248 } | ClassID { // Record Type
249 $$ = new RecordRecTy($1);
250 };
251
252OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
253
254OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
255
256Value : INTVAL {
257 $$ = new IntInit($1);
258 } | STRVAL {
259 $$ = new StringInit(*$1);
260 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000261 } | CODEFRAGMENT {
262 $$ = new CodeInit(*$1);
263 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000264 } | '?' {
265 $$ = new UnsetInit();
266 } | '{' ValueList '}' {
267 BitsInit *Init = new BitsInit($2->size());
268 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
269 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
270 if (Bit == 0) {
271 err() << "Element #" << i << " (" << *(*$2)[i]
272 << ") is not convertable to a bit!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000273 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000274 }
275 Init->setBit($2->size()-i-1, Bit);
276 }
277 $$ = Init;
278 delete $2;
279 } | ID {
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000280 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
Chris Lattnere62c1182002-12-02 01:23:04 +0000281 $$ = new VarInit(*$1, RV->getType());
Chris Lattner7dda3952005-04-19 03:36:21 +0000282 } else if (CurRec && CurRec->isTemplateArg(CurRec->getName()+":"+*$1)) {
283 const RecordVal *RV = CurRec->getValue(CurRec->getName()+":"+*$1);
284 assert(RV && "Template arg doesn't exist??");
285 $$ = new VarInit(CurRec->getName()+":"+*$1, RV->getType());
Chris Lattnere62c1182002-12-02 01:23:04 +0000286 } else if (Record *D = Records.getDef(*$1)) {
287 $$ = new DefInit(D);
288 } else {
289 err() << "Variable not defined: '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000290 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000291 }
292
293 delete $1;
294 } | Value '{' BitList '}' {
295 $$ = $1->convertInitializerBitRange(*$3);
296 if ($$ == 0) {
297 err() << "Invalid bit range for value '" << *$1 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000298 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000299 }
300 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000301 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000302 $$ = new ListInit(*$2);
303 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000304 } | Value '.' ID {
305 if (!$1->getFieldType(*$3)) {
306 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000307 exit(1);
Chris Lattner34a77692002-12-02 16:43:43 +0000308 }
309 $$ = new FieldInit($1, *$3);
310 delete $3;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000311 } | '(' ID DagArgList ')' {
312 Record *D = Records.getDef(*$2);
313 if (D == 0) {
314 err() << "Invalid def '" << *$2 << "'!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000315 exit(1);
Chris Lattnerbc21c342003-08-04 20:44:43 +0000316 }
317 $$ = new DagInit(D, *$3);
318 delete $2; delete $3;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000319 } | Value '[' BitList ']' {
320 std::reverse($3->begin(), $3->end());
321 $$ = $1->convertInitListSlice(*$3);
322 if ($$ == 0) {
323 err() << "Invalid list slice for value '" << *$1 << "'!\n";
324 exit(1);
325 }
326 delete $3;
Chris Lattnerb9266f82005-04-19 01:11:03 +0000327 } | SHLTOK '(' Value ',' Value ')' {
328 $$ = $3->getBinaryOp(Init::SHL, $5);
329 if ($$ == 0) {
330 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
331 exit(1);
332 }
333 } | SRATOK '(' Value ',' Value ')' {
334 $$ = $3->getBinaryOp(Init::SRA, $5);
335 if ($$ == 0) {
336 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
337 exit(1);
338 }
339 } | SRLTOK '(' Value ',' Value ')' {
340 $$ = $3->getBinaryOp(Init::SRL, $5);
341 if ($$ == 0) {
342 err() << "Cannot shift values '" << *$3 << "' and '" << *$5 << "'!\n";
343 exit(1);
344 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000345 };
346
Chris Lattner91290d72003-08-10 22:14:13 +0000347OptVarName : /* empty */ {
348 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000349 }
Chris Lattner91290d72003-08-10 22:14:13 +0000350 | ':' VARNAME {
351 $$ = $2;
352 };
353
354DagArgListNE : Value OptVarName {
355 $$ = new std::vector<std::pair<Init*, std::string> >();
356 $$->push_back(std::make_pair($1, *$2));
357 delete $2;
358 }
359 | DagArgListNE ',' Value OptVarName {
360 $1->push_back(std::make_pair($3, *$4));
361 delete $4;
362 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000363 };
364
365DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000366 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000367 }
368 | DagArgListNE { $$ = $1; };
369
370
Chris Lattnere62c1182002-12-02 01:23:04 +0000371RBitList : INTVAL {
372 $$ = new std::vector<unsigned>();
373 $$->push_back($1);
374 } | INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000375 if ($1 < 0 || $3 < 0) {
376 err() << "Invalid range: " << $1 << "-" << $3 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000377 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000378 }
379 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000380 if ($1 < $3) {
381 for (int i = $1; i <= $3; ++i)
382 $$->push_back(i);
383 } else {
384 for (int i = $1; i >= $3; --i)
385 $$->push_back(i);
386 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000387 } | INTVAL INTVAL {
388 $2 = -$2;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000389 if ($1 < 0 || $2 < 0) {
390 err() << "Invalid range: " << $1 << "-" << $2 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000391 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000392 }
393 $$ = new std::vector<unsigned>();
Chris Lattnerb0fef642004-07-26 23:21:34 +0000394 if ($1 < $2) {
395 for (int i = $1; i <= $2; ++i)
396 $$->push_back(i);
397 } else {
398 for (int i = $1; i >= $2; --i)
399 $$->push_back(i);
400 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000401 } | RBitList ',' INTVAL {
402 ($$=$1)->push_back($3);
403 } | RBitList ',' INTVAL '-' INTVAL {
Chris Lattnerb0fef642004-07-26 23:21:34 +0000404 if ($3 < 0 || $5 < 0) {
405 err() << "Invalid range: " << $3 << "-" << $5 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000406 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000407 }
408 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000409 if ($3 < $5) {
410 for (int i = $3; i <= $5; ++i)
411 $$->push_back(i);
412 } else {
413 for (int i = $3; i >= $5; --i)
414 $$->push_back(i);
415 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000416 } | RBitList ',' INTVAL INTVAL {
417 $4 = -$4;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000418 if ($3 < 0 || $4 < 0) {
419 err() << "Invalid range: " << $3 << "-" << $4 << "!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000420 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000421 }
422 $$ = $1;
Chris Lattnerb0fef642004-07-26 23:21:34 +0000423 if ($3 < $4) {
424 for (int i = $3; i <= $4; ++i)
425 $$->push_back(i);
426 } else {
427 for (int i = $3; i >= $4; --i)
428 $$->push_back(i);
429 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000430 };
431
432BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
433
434OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
435
436
437
438ValueList : /*empty*/ {
439 $$ = new std::vector<Init*>();
440 } | ValueListNE {
441 $$ = $1;
442 };
443
444ValueListNE : Value {
445 $$ = new std::vector<Init*>();
446 $$->push_back($1);
447 } | ValueListNE ',' Value {
448 ($$ = $1)->push_back($3);
449 };
450
451Declaration : OptPrefix Type ID OptValue {
Chris Lattner7dda3952005-04-19 03:36:21 +0000452 std::string DecName = *$3;
453 if (ParsingTemplateArgs)
454 DecName = CurRec->getName() + ":" + DecName;
455
456 addValue(RecordVal(DecName, $2, $1));
457 setValue(DecName, 0, $4);
458 $$ = new std::string(DecName);
Chris Lattnere62c1182002-12-02 01:23:04 +0000459};
460
461BodyItem : Declaration ';' {
462 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000463} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000464 setValue(*$2, $3, $5);
465 delete $2;
466 delete $3;
467};
468
469BodyList : /*empty*/ | BodyList BodyItem;
470Body : ';' | '{' BodyList '}';
471
472SubClassRef : ClassID {
473 $$ = new SubClassRefTy($1, new std::vector<Init*>());
474 } | ClassID '<' ValueListNE '>' {
475 $$ = new SubClassRefTy($1, $3);
476 };
477
478ClassListNE : SubClassRef {
479 $$ = new std::vector<SubClassRefTy>();
480 $$->push_back(*$1);
481 delete $1;
482 }
483 | ClassListNE ',' SubClassRef {
484 ($$=$1)->push_back(*$3);
485 delete $3;
486 };
487
488ClassList : /*empty */ {
489 $$ = new std::vector<SubClassRefTy>();
490 }
491 | ':' ClassListNE {
492 $$ = $2;
493 };
494
495DeclListNE : Declaration {
496 CurRec->addTemplateArg(*$1);
497 delete $1;
498} | DeclListNE ',' Declaration {
499 CurRec->addTemplateArg(*$3);
500 delete $3;
501};
502
503TemplateArgList : '<' DeclListNE '>' {};
504OptTemplateArgList : /*empty*/ | TemplateArgList;
505
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000506OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
507
508ObjectBody : OptID {
509 static unsigned AnonCounter = 0;
510 if ($1->empty())
511 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000512 CurRec = new Record(*$1);
513 delete $1;
Chris Lattner7dda3952005-04-19 03:36:21 +0000514 ParsingTemplateArgs = true;
Chris Lattnere62c1182002-12-02 01:23:04 +0000515 } OptTemplateArgList ClassList {
Chris Lattner7dda3952005-04-19 03:36:21 +0000516 ParsingTemplateArgs = false;
Chris Lattnere62c1182002-12-02 01:23:04 +0000517 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
518 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000519 // Delete the template arg values for the class
520 delete (*$4)[i].second;
521 }
Chris Lattner7dda3952005-04-19 03:36:21 +0000522 delete $4; // Delete the class list...
Chris Lattnere62c1182002-12-02 01:23:04 +0000523
524 // Process any variables on the set stack...
Chris Lattner42aa89e2003-08-04 04:56:53 +0000525 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
526 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
527 setValue(LetStack[i][j].Name,
528 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
529 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000530 } Body {
Chris Lattner7dda3952005-04-19 03:36:21 +0000531 $$ = CurRec;
532 CurRec = 0;
533 };
Chris Lattnere62c1182002-12-02 01:23:04 +0000534
535ClassInst : CLASS ObjectBody {
536 if (Records.getClass($2->getName())) {
537 err() << "Class '" << $2->getName() << "' already defined!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000538 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000539 }
540 Records.addClass($$ = $2);
541};
542
543DefInst : DEF ObjectBody {
Chris Lattner7dda3952005-04-19 03:36:21 +0000544 $2->resolveReferences();
545
Chris Lattner554af5c2003-07-30 04:31:17 +0000546 if (!$2->getTemplateArgs().empty()) {
547 err() << "Def '" << $2->getName()
548 << "' is not permitted to have template arguments!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000549 exit(1);
Chris Lattner554af5c2003-07-30 04:31:17 +0000550 }
551 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000552 if (Records.getDef($2->getName())) {
553 err() << "Def '" << $2->getName() << "' already defined!\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000554 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000555 }
556 Records.addDef($$ = $2);
557};
558
559
560Object : ClassInst | DefInst;
561
Chris Lattner42aa89e2003-08-04 04:56:53 +0000562LETItem : ID OptBitList '=' Value {
563 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000564 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000565};
566
Chris Lattner42aa89e2003-08-04 04:56:53 +0000567LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000568
Chris Lattner42aa89e2003-08-04 04:56:53 +0000569// LETCommand - A 'LET' statement start...
570LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000571
Chris Lattner2e724542003-07-28 03:49:40 +0000572// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000573Object : LETCommand '{' ObjectList '}' {
574 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000575 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000576 | LETCommand Object {
577 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000578 };
579
580ObjectList : Object {} | ObjectList Object {};
581
582File : ObjectList {};
583
584%%
585
586int yyerror(const char *ErrorMsg) {
587 err() << "Error parsing: " << ErrorMsg << "\n";
Chris Lattnerf5761a52004-02-13 16:37:43 +0000588 exit(1);
Chris Lattnere62c1182002-12-02 01:23:04 +0000589}