blob: e95e59785c9e177c19f14adc1b6090a0b74e90c4 [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"
Chris Lattnerfc06bf02003-07-30 04:26:44 +000016#include "Support/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;
28
29typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
30
Chris Lattner42aa89e2003-08-04 04:56:53 +000031struct LetRecord {
Chris Lattner60094252003-08-03 13:58:01 +000032 std::string Name;
33 std::vector<unsigned> Bits;
34 Init *Value;
35 bool HasBits;
Chris Lattner42aa89e2003-08-04 04:56:53 +000036 LetRecord(const std::string &N, std::vector<unsigned> *B, Init *V)
Chris Lattner60094252003-08-03 13:58:01 +000037 : Name(N), Value(V), HasBits(B != 0) {
38 if (HasBits) Bits = *B;
39 }
40};
41
Chris Lattner42aa89e2003-08-04 04:56:53 +000042static std::vector<std::vector<LetRecord> > LetStack;
Chris Lattner60094252003-08-03 13:58:01 +000043
Chris Lattnere62c1182002-12-02 01:23:04 +000044
Chris Lattner7dff0532003-07-30 20:56:47 +000045extern std::ostream &err();
Chris Lattnere62c1182002-12-02 01:23:04 +000046
47static void addValue(const RecordVal &RV) {
Chris Lattner60094252003-08-03 13:58:01 +000048 if (RecordVal *ERV = CurRec->getValue(RV.getName())) {
49 // The value already exists in the class, treat this as a set...
50 if (ERV->setValue(RV.getValue())) {
51 err() << "New definition of '" << RV.getName() << "' of type '"
52 << *RV.getType() << "' is incompatible with previous "
53 << "definition of type '" << *ERV->getType() << "'!\n";
54 abort();
55 }
56 } else {
57 CurRec->addValue(RV);
Chris Lattnere62c1182002-12-02 01:23:04 +000058 }
Chris Lattnere62c1182002-12-02 01:23:04 +000059}
60
61static void addSuperClass(Record *SC) {
62 if (CurRec->isSubClassOf(SC)) {
63 err() << "Already subclass of '" << SC->getName() << "'!\n";
64 abort();
65 }
66 CurRec->addSuperClass(SC);
67}
68
69static void setValue(const std::string &ValName,
70 std::vector<unsigned> *BitList, Init *V) {
71 if (!V) return ;
72
73 RecordVal *RV = CurRec->getValue(ValName);
74 if (RV == 0) {
75 err() << "Value '" << ValName << "' unknown!\n";
76 abort();
77 }
78
79 // If we are assigning to a subset of the bits in the value... then we must be
80 // assigning to a field of BitsRecTy, which must have a BitsInit
81 // initializer...
82 //
83 if (BitList) {
84 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
85 if (CurVal == 0) {
86 err() << "Value '" << ValName << "' is not a bits type!\n";
87 abort();
88 }
89
90 // Convert the incoming value to a bits type of the appropriate size...
91 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
92 if (BI == 0) {
93 V->convertInitializerTo(new BitsRecTy(BitList->size()));
94 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
95 abort();
96 }
97
98 // We should have a BitsInit type now...
99 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
100 BitsInit *BInit = (BitsInit*)BI;
101
102 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
103
Chris Lattnerade0de92002-12-06 03:55:39 +0000104 // Loop over bits, assigning values as appropriate...
Chris Lattnere62c1182002-12-02 01:23:04 +0000105 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
106 unsigned Bit = (*BitList)[i];
Chris Lattner28c2d402002-12-06 04:42:16 +0000107 if (NewVal->getBit(Bit)) {
108 err() << "Cannot set bit #" << Bit << " of value '" << ValName
Chris Lattnerade0de92002-12-06 03:55:39 +0000109 << "' more than once!\n";
110 abort();
111 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000112 NewVal->setBit(Bit, BInit->getBit(i));
113 }
Chris Lattnerade0de92002-12-06 03:55:39 +0000114
115 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
116 if (NewVal->getBit(i) == 0)
117 NewVal->setBit(i, CurVal->getBit(i));
118
Chris Lattnere62c1182002-12-02 01:23:04 +0000119 V = NewVal;
120 }
121
122 if (RV->setValue(V)) {
123 err() << "Value '" << ValName << "' of type '" << *RV->getType()
124 << "' is incompatible with initializer '" << *V << "'!\n";
125 abort();
126 }
127}
128
129static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
130 // Add all of the values in the subclass into the current class...
131 const std::vector<RecordVal> &Vals = SC->getValues();
132 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
133 addValue(Vals[i]);
134
135 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
136
137 // Ensure that an appropriate number of template arguments are specified...
138 if (TArgs.size() < TemplateArgs.size()) {
Misha Brukmane3d333e2003-05-20 23:45:36 +0000139 err() << "ERROR: More template args specified than expected!\n";
Chris Lattnere62c1182002-12-02 01:23:04 +0000140 abort();
141 } else { // This class expects template arguments...
142 // Loop over all of the template arguments, setting them to the specified
Misha Brukman5560c9d2003-08-18 14:43:39 +0000143 // value or leaving them as the default as necessary.
Chris Lattnere62c1182002-12-02 01:23:04 +0000144 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
145 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
146 // Set it now.
147 setValue(TArgs[i], 0, TemplateArgs[i]);
148 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
149 err() << "ERROR: Value not specified for template argument #"
150 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
151 << "'!\n";
152 abort();
153 }
154 }
155 }
156
157
158 // Since everything went well, we can now set the "superclass" list for the
159 // current record.
160 const std::vector<Record*> &SCs = SC->getSuperClasses();
161 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
162 addSuperClass(SCs[i]);
163 addSuperClass(SC);
164}
165
Brian Gaeked0fde302003-11-11 22:41:34 +0000166} // End llvm namespace
167
168using namespace llvm;
Chris Lattnere62c1182002-12-02 01:23:04 +0000169
170%}
171
172%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000173 std::string* StrVal;
174 int IntVal;
175 llvm::RecTy* Ty;
176 llvm::Init* Initializer;
177 std::vector<llvm::Init*>* FieldList;
178 std::vector<unsigned>* BitList;
179 llvm::Record* Rec;
180 SubClassRefTy* SubClassRef;
181 std::vector<SubClassRefTy>* SubClassList;
182 std::vector<std::pair<llvm::Init*, std::string> >* DagValueList;
Chris Lattnere62c1182002-12-02 01:23:04 +0000183};
184
Chris Lattner42aa89e2003-08-04 04:56:53 +0000185%token INT BIT STRING BITS LIST CODE DAG CLASS DEF FIELD LET IN
Chris Lattnere62c1182002-12-02 01:23:04 +0000186%token <IntVal> INTVAL
Chris Lattnerfb9ea582003-08-10 22:04:25 +0000187%token <StrVal> ID VARNAME STRVAL CODEFRAGMENT
Chris Lattnere62c1182002-12-02 01:23:04 +0000188
189%type <Ty> Type
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000190%type <Rec> ClassInst DefInst Object ObjectBody ClassID
Chris Lattnere62c1182002-12-02 01:23:04 +0000191
192%type <SubClassRef> SubClassRef
193%type <SubClassList> ClassList ClassListNE
194%type <IntVal> OptPrefix
195%type <Initializer> Value OptValue
Chris Lattnerbc21c342003-08-04 20:44:43 +0000196%type <DagValueList> DagArgList DagArgListNE
Chris Lattnere62c1182002-12-02 01:23:04 +0000197%type <FieldList> ValueList ValueListNE
198%type <BitList> BitList OptBitList RBitList
Chris Lattner91290d72003-08-10 22:14:13 +0000199%type <StrVal> Declaration OptID OptVarName
Chris Lattnere62c1182002-12-02 01:23:04 +0000200
201%start File
Brian Gaeked0fde302003-11-11 22:41:34 +0000202
Chris Lattnere62c1182002-12-02 01:23:04 +0000203%%
204
205ClassID : ID {
206 $$ = Records.getClass(*$1);
207 if ($$ == 0) {
208 err() << "Couldn't find class '" << *$1 << "'!\n";
209 abort();
210 }
211 delete $1;
212 };
213
Chris Lattnere62c1182002-12-02 01:23:04 +0000214
215// TableGen types...
216Type : STRING { // string type
217 $$ = new StringRecTy();
218 } | BIT { // bit type
219 $$ = new BitRecTy();
220 } | BITS '<' INTVAL '>' { // bits<x> type
221 $$ = new BitsRecTy($3);
222 } | INT { // int type
223 $$ = new IntRecTy();
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000224 } | LIST '<' Type '>' { // list<x> type
Chris Lattnere62c1182002-12-02 01:23:04 +0000225 $$ = new ListRecTy($3);
Chris Lattnerf05760d2003-07-30 21:47:42 +0000226 } | CODE { // code type
227 $$ = new CodeRecTy();
Chris Lattner40f71132003-08-04 04:50:57 +0000228 } | DAG { // dag type
229 $$ = new DagRecTy();
Chris Lattnere62c1182002-12-02 01:23:04 +0000230 } | ClassID { // Record Type
231 $$ = new RecordRecTy($1);
232 };
233
234OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
235
236OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
237
238Value : INTVAL {
239 $$ = new IntInit($1);
240 } | STRVAL {
241 $$ = new StringInit(*$1);
242 delete $1;
Chris Lattnere3a1d052003-07-30 22:15:58 +0000243 } | CODEFRAGMENT {
244 $$ = new CodeInit(*$1);
245 delete $1;
Chris Lattnere62c1182002-12-02 01:23:04 +0000246 } | '?' {
247 $$ = new UnsetInit();
248 } | '{' ValueList '}' {
249 BitsInit *Init = new BitsInit($2->size());
250 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
251 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
252 if (Bit == 0) {
253 err() << "Element #" << i << " (" << *(*$2)[i]
254 << ") is not convertable to a bit!\n";
255 abort();
256 }
257 Init->setBit($2->size()-i-1, Bit);
258 }
259 $$ = Init;
260 delete $2;
261 } | ID {
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000262 if (const RecordVal *RV = (CurRec ? CurRec->getValue(*$1) : 0)) {
Chris Lattnere62c1182002-12-02 01:23:04 +0000263 $$ = new VarInit(*$1, RV->getType());
264 } else if (Record *D = Records.getDef(*$1)) {
265 $$ = new DefInit(D);
266 } else {
267 err() << "Variable not defined: '" << *$1 << "'!\n";
268 abort();
269 }
270
271 delete $1;
272 } | Value '{' BitList '}' {
273 $$ = $1->convertInitializerBitRange(*$3);
274 if ($$ == 0) {
275 err() << "Invalid bit range for value '" << *$1 << "'!\n";
276 abort();
277 }
278 delete $3;
Chris Lattner7cf0ce42003-08-03 18:17:22 +0000279 } | '[' ValueList ']' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000280 $$ = new ListInit(*$2);
281 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000282 } | Value '.' ID {
283 if (!$1->getFieldType(*$3)) {
284 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
285 abort();
286 }
287 $$ = new FieldInit($1, *$3);
288 delete $3;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000289 } | '(' ID DagArgList ')' {
290 Record *D = Records.getDef(*$2);
291 if (D == 0) {
292 err() << "Invalid def '" << *$2 << "'!\n";
293 abort();
294 }
295 $$ = new DagInit(D, *$3);
296 delete $2; delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000297 };
298
Chris Lattner91290d72003-08-10 22:14:13 +0000299OptVarName : /* empty */ {
300 $$ = new std::string();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000301 }
Chris Lattner91290d72003-08-10 22:14:13 +0000302 | ':' VARNAME {
303 $$ = $2;
304 };
305
306DagArgListNE : Value OptVarName {
307 $$ = new std::vector<std::pair<Init*, std::string> >();
308 $$->push_back(std::make_pair($1, *$2));
309 delete $2;
310 }
311 | DagArgListNE ',' Value OptVarName {
312 $1->push_back(std::make_pair($3, *$4));
313 delete $4;
314 $$ = $1;
Chris Lattnerbc21c342003-08-04 20:44:43 +0000315 };
316
317DagArgList : /*empty*/ {
Chris Lattner91290d72003-08-10 22:14:13 +0000318 $$ = new std::vector<std::pair<Init*, std::string> >();
Chris Lattnerbc21c342003-08-04 20:44:43 +0000319 }
320 | DagArgListNE { $$ = $1; };
321
322
Chris Lattnere62c1182002-12-02 01:23:04 +0000323RBitList : INTVAL {
324 $$ = new std::vector<unsigned>();
325 $$->push_back($1);
326 } | INTVAL '-' INTVAL {
327 if ($1 < $3 || $1 < 0 || $3 < 0) {
328 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
329 abort();
330 }
331 $$ = new std::vector<unsigned>();
332 for (int i = $1; i >= $3; --i)
333 $$->push_back(i);
334 } | INTVAL INTVAL {
335 $2 = -$2;
336 if ($1 < $2 || $1 < 0 || $2 < 0) {
337 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
338 abort();
339 }
340 $$ = new std::vector<unsigned>();
341 for (int i = $1; i >= $2; --i)
342 $$->push_back(i);
343 } | RBitList ',' INTVAL {
344 ($$=$1)->push_back($3);
345 } | RBitList ',' INTVAL '-' INTVAL {
346 if ($3 < $5 || $3 < 0 || $5 < 0) {
347 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
348 abort();
349 }
350 $$ = $1;
351 for (int i = $3; i >= $5; --i)
352 $$->push_back(i);
353 } | RBitList ',' INTVAL INTVAL {
354 $4 = -$4;
355 if ($3 < $4 || $3 < 0 || $4 < 0) {
356 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
357 abort();
358 }
359 $$ = $1;
360 for (int i = $3; i >= $4; --i)
361 $$->push_back(i);
362 };
363
364BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
365
366OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
367
368
369
370ValueList : /*empty*/ {
371 $$ = new std::vector<Init*>();
372 } | ValueListNE {
373 $$ = $1;
374 };
375
376ValueListNE : Value {
377 $$ = new std::vector<Init*>();
378 $$->push_back($1);
379 } | ValueListNE ',' Value {
380 ($$ = $1)->push_back($3);
381 };
382
383Declaration : OptPrefix Type ID OptValue {
384 addValue(RecordVal(*$3, $2, $1));
385 setValue(*$3, 0, $4);
386 $$ = $3;
387};
388
389BodyItem : Declaration ';' {
390 delete $1;
Chris Lattner42aa89e2003-08-04 04:56:53 +0000391} | LET ID OptBitList '=' Value ';' {
Chris Lattnere62c1182002-12-02 01:23:04 +0000392 setValue(*$2, $3, $5);
393 delete $2;
394 delete $3;
395};
396
397BodyList : /*empty*/ | BodyList BodyItem;
398Body : ';' | '{' BodyList '}';
399
400SubClassRef : ClassID {
401 $$ = new SubClassRefTy($1, new std::vector<Init*>());
402 } | ClassID '<' ValueListNE '>' {
403 $$ = new SubClassRefTy($1, $3);
404 };
405
406ClassListNE : SubClassRef {
407 $$ = new std::vector<SubClassRefTy>();
408 $$->push_back(*$1);
409 delete $1;
410 }
411 | ClassListNE ',' SubClassRef {
412 ($$=$1)->push_back(*$3);
413 delete $3;
414 };
415
416ClassList : /*empty */ {
417 $$ = new std::vector<SubClassRefTy>();
418 }
419 | ':' ClassListNE {
420 $$ = $2;
421 };
422
423DeclListNE : Declaration {
424 CurRec->addTemplateArg(*$1);
425 delete $1;
426} | DeclListNE ',' Declaration {
427 CurRec->addTemplateArg(*$3);
428 delete $3;
429};
430
431TemplateArgList : '<' DeclListNE '>' {};
432OptTemplateArgList : /*empty*/ | TemplateArgList;
433
Chris Lattnerfc06bf02003-07-30 04:26:44 +0000434OptID : ID { $$ = $1; } | /*empty*/ { $$ = new std::string(); };
435
436ObjectBody : OptID {
437 static unsigned AnonCounter = 0;
438 if ($1->empty())
439 *$1 = "anonymous."+utostr(AnonCounter++);
Chris Lattnere62c1182002-12-02 01:23:04 +0000440 CurRec = new Record(*$1);
441 delete $1;
442 } OptTemplateArgList ClassList {
443 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
444 addSubClass((*$4)[i].first, *(*$4)[i].second);
Chris Lattnerbfce0562003-07-30 04:56:05 +0000445 // Delete the template arg values for the class
446 delete (*$4)[i].second;
447 }
Chris Lattnere62c1182002-12-02 01:23:04 +0000448
449 // Process any variables on the set stack...
Chris Lattner42aa89e2003-08-04 04:56:53 +0000450 for (unsigned i = 0, e = LetStack.size(); i != e; ++i)
451 for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j)
452 setValue(LetStack[i][j].Name,
453 LetStack[i][j].HasBits ? &LetStack[i][j].Bits : 0,
454 LetStack[i][j].Value);
Chris Lattnere62c1182002-12-02 01:23:04 +0000455 } Body {
456 CurRec->resolveReferences();
Chris Lattnerbfce0562003-07-30 04:56:05 +0000457
458 // Now that all of the references have been resolved, we can delete template
459 // arguments for superclasses, so they don't pollute our record, and so that
460 // their names won't conflict with later uses of the name...
461 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
462 Record *SuperClass = (*$4)[i].first;
463 for (unsigned i = 0, e = SuperClass->getTemplateArgs().size(); i != e; ++i)
464 CurRec->removeValue(SuperClass->getTemplateArgs()[i]);
465 }
466 delete $4; // Delete the class list...
467
Chris Lattnere62c1182002-12-02 01:23:04 +0000468 $$ = CurRec;
469 CurRec = 0;
470};
471
472ClassInst : CLASS ObjectBody {
473 if (Records.getClass($2->getName())) {
474 err() << "Class '" << $2->getName() << "' already defined!\n";
475 abort();
476 }
477 Records.addClass($$ = $2);
478};
479
480DefInst : DEF ObjectBody {
Chris Lattner554af5c2003-07-30 04:31:17 +0000481 if (!$2->getTemplateArgs().empty()) {
482 err() << "Def '" << $2->getName()
483 << "' is not permitted to have template arguments!\n";
484 abort();
485 }
486 // If ObjectBody has template arguments, it's an error.
Chris Lattnere62c1182002-12-02 01:23:04 +0000487 if (Records.getDef($2->getName())) {
488 err() << "Def '" << $2->getName() << "' already defined!\n";
489 abort();
490 }
491 Records.addDef($$ = $2);
492};
493
494
495Object : ClassInst | DefInst;
496
Chris Lattner42aa89e2003-08-04 04:56:53 +0000497LETItem : ID OptBitList '=' Value {
498 LetStack.back().push_back(LetRecord(*$1, $2, $4));
Chris Lattner60094252003-08-03 13:58:01 +0000499 delete $1; delete $2;
Chris Lattner2e724542003-07-28 03:49:40 +0000500};
501
Chris Lattner42aa89e2003-08-04 04:56:53 +0000502LETList : LETItem | LETList ',' LETItem;
Chris Lattner60094252003-08-03 13:58:01 +0000503
Chris Lattner42aa89e2003-08-04 04:56:53 +0000504// LETCommand - A 'LET' statement start...
505LETCommand : LET { LetStack.push_back(std::vector<LetRecord>()); } LETList IN;
Chris Lattner60094252003-08-03 13:58:01 +0000506
Chris Lattner2e724542003-07-28 03:49:40 +0000507// Support Set commands wrapping objects... both with and without braces.
Chris Lattner42aa89e2003-08-04 04:56:53 +0000508Object : LETCommand '{' ObjectList '}' {
509 LetStack.pop_back();
Chris Lattner2e724542003-07-28 03:49:40 +0000510 }
Chris Lattner42aa89e2003-08-04 04:56:53 +0000511 | LETCommand Object {
512 LetStack.pop_back();
Chris Lattnere62c1182002-12-02 01:23:04 +0000513 };
514
515ObjectList : Object {} | ObjectList Object {};
516
517File : ObjectList {};
518
519%%
520
521int yyerror(const char *ErrorMsg) {
522 err() << "Error parsing: " << ErrorMsg << "\n";
523 abort();
524}