blob: 38efb83fa8648d9bc9c7b90d6104b546c9eaaaf0 [file] [log] [blame]
Chris Lattnere62c1182002-12-02 01:23:04 +00001//===-- FileParser.y - Parser for TableGen files ----------------*- C++ -*-===//
2//
3// This file implements the bison parser for Table Generator files...
4//
5//===------------------------------------------------------------------------=//
6
7%{
8#include "Record.h"
9#include <iostream>
10#include <algorithm>
11#include <string>
12#include <stdio.h>
13#define YYERROR_VERBOSE 1
14
15int yyerror(const char *ErrorMsg);
16int yylex();
17extern FILE *Filein;
18extern int Filelineno;
19int Fileparse();
20static Record *CurRec = 0;
21
22typedef std::pair<Record*, std::vector<Init*>*> SubClassRefTy;
23
24static std::vector<std::pair<std::pair<std::string, std::vector<unsigned>*>,
25 Init*> > SetStack;
26
27void ParseFile() {
28 FILE *F = stdin;
29
30 Filein = F;
31 Filelineno = 1;
32 Fileparse();
33 Filein = stdin;
34}
35
36static std::ostream &err() {
37 return std::cerr << "Parsing Line #" << Filelineno << ": ";
38}
39
40static void addValue(const RecordVal &RV) {
41 if (CurRec->getValue(RV.getName())) {
42 err() << "Value '" << RV.getName() << "' multiply defined!\n";
43 abort();
44 }
45
46 CurRec->addValue(RV);
47}
48
49static void addSuperClass(Record *SC) {
50 if (CurRec->isSubClassOf(SC)) {
51 err() << "Already subclass of '" << SC->getName() << "'!\n";
52 abort();
53 }
54 CurRec->addSuperClass(SC);
55}
56
57static void setValue(const std::string &ValName,
58 std::vector<unsigned> *BitList, Init *V) {
59 if (!V) return ;
60
61 RecordVal *RV = CurRec->getValue(ValName);
62 if (RV == 0) {
63 err() << "Value '" << ValName << "' unknown!\n";
64 abort();
65 }
66
67 // If we are assigning to a subset of the bits in the value... then we must be
68 // assigning to a field of BitsRecTy, which must have a BitsInit
69 // initializer...
70 //
71 if (BitList) {
72 BitsInit *CurVal = dynamic_cast<BitsInit*>(RV->getValue());
73 if (CurVal == 0) {
74 err() << "Value '" << ValName << "' is not a bits type!\n";
75 abort();
76 }
77
78 // Convert the incoming value to a bits type of the appropriate size...
79 Init *BI = V->convertInitializerTo(new BitsRecTy(BitList->size()));
80 if (BI == 0) {
81 V->convertInitializerTo(new BitsRecTy(BitList->size()));
82 err() << "Initializer '" << *V << "' not compatible with bit range!\n";
83 abort();
84 }
85
86 // We should have a BitsInit type now...
87 assert(dynamic_cast<BitsInit*>(BI) != 0 || &(std::cerr << *BI) == 0);
88 BitsInit *BInit = (BitsInit*)BI;
89
90 BitsInit *NewVal = new BitsInit(CurVal->getNumBits());
91
92 for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
93 NewVal->setBit(i, CurVal->getBit(i));
94
95 // Loop over bits, assigning values as appopriate...
96 for (unsigned i = 0, e = BitList->size(); i != e; ++i) {
97 unsigned Bit = (*BitList)[i];
98 NewVal->setBit(Bit, BInit->getBit(i));
99 }
100 V = NewVal;
101 }
102
103 if (RV->setValue(V)) {
104 err() << "Value '" << ValName << "' of type '" << *RV->getType()
105 << "' is incompatible with initializer '" << *V << "'!\n";
106 abort();
107 }
108}
109
110static void addSubClass(Record *SC, const std::vector<Init*> &TemplateArgs) {
111 // Add all of the values in the subclass into the current class...
112 const std::vector<RecordVal> &Vals = SC->getValues();
113 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
114 addValue(Vals[i]);
115
116 const std::vector<std::string> &TArgs = SC->getTemplateArgs();
117
118 // Ensure that an appropriate number of template arguments are specified...
119 if (TArgs.size() < TemplateArgs.size()) {
120 err() << "ERROR: More template args specified thang expected!\n";
121 abort();
122 } else { // This class expects template arguments...
123 // Loop over all of the template arguments, setting them to the specified
124 // value or leaving them as the default as neccesary.
125 for (unsigned i = 0, e = TArgs.size(); i != e; ++i) {
126 if (i < TemplateArgs.size()) { // A value is specified for this temp-arg?
127 // Set it now.
128 setValue(TArgs[i], 0, TemplateArgs[i]);
129 } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) {
130 err() << "ERROR: Value not specified for template argument #"
131 << i << " (" << TArgs[i] << ") of subclass '" << SC->getName()
132 << "'!\n";
133 abort();
134 }
135 }
136 }
137
138
139 // Since everything went well, we can now set the "superclass" list for the
140 // current record.
141 const std::vector<Record*> &SCs = SC->getSuperClasses();
142 for (unsigned i = 0, e = SCs.size(); i != e; ++i)
143 addSuperClass(SCs[i]);
144 addSuperClass(SC);
145}
146
147
148%}
149
150%union {
151 std::string *StrVal;
152 int IntVal;
153 RecTy *Ty;
154 Init *Initializer;
155 std::vector<Init*> *FieldList;
156 std::vector<Record*> *RecPtr;
157 std::vector<unsigned>*BitList;
158 Record *Rec;
159 SubClassRefTy *SubClassRef;
160 std::vector<SubClassRefTy> *SubClassList;
161};
162
163%token INT BIT STRING BITS LIST CLASS DEF FIELD SET IN
164%token <IntVal> INTVAL
165%token <StrVal> ID STRVAL
166
167%type <Ty> Type
168%type <RecPtr> DefList DefListNE
169%type <Rec> ClassInst DefInst Object ObjectBody ClassID DefID
170
171%type <SubClassRef> SubClassRef
172%type <SubClassList> ClassList ClassListNE
173%type <IntVal> OptPrefix
174%type <Initializer> Value OptValue
175%type <FieldList> ValueList ValueListNE
176%type <BitList> BitList OptBitList RBitList
177%type <StrVal> Declaration
178
179%start File
180%%
181
182ClassID : ID {
183 $$ = Records.getClass(*$1);
184 if ($$ == 0) {
185 err() << "Couldn't find class '" << *$1 << "'!\n";
186 abort();
187 }
188 delete $1;
189 };
190
191DefID : ID {
192 $$ = Records.getDef(*$1);
193 if ($$ == 0) {
194 err() << "Couldn't find def '" << *$1 << "'!\n";
195 abort();
196 }
197 delete $1;
198 };
199
200
201// TableGen types...
202Type : STRING { // string type
203 $$ = new StringRecTy();
204 } | BIT { // bit type
205 $$ = new BitRecTy();
206 } | BITS '<' INTVAL '>' { // bits<x> type
207 $$ = new BitsRecTy($3);
208 } | INT { // int type
209 $$ = new IntRecTy();
210 } | LIST '<' ClassID '>' { // list<x> type
211 $$ = new ListRecTy($3);
212 } | ClassID { // Record Type
213 $$ = new RecordRecTy($1);
214 };
215
216OptPrefix : /*empty*/ { $$ = 0; } | FIELD { $$ = 1; };
217
218OptValue : /*empty*/ { $$ = 0; } | '=' Value { $$ = $2; };
219
220Value : INTVAL {
221 $$ = new IntInit($1);
222 } | STRVAL {
223 $$ = new StringInit(*$1);
224 delete $1;
225 } | '?' {
226 $$ = new UnsetInit();
227 } | '{' ValueList '}' {
228 BitsInit *Init = new BitsInit($2->size());
229 for (unsigned i = 0, e = $2->size(); i != e; ++i) {
230 struct Init *Bit = (*$2)[i]->convertInitializerTo(new BitRecTy());
231 if (Bit == 0) {
232 err() << "Element #" << i << " (" << *(*$2)[i]
233 << ") is not convertable to a bit!\n";
234 abort();
235 }
236 Init->setBit($2->size()-i-1, Bit);
237 }
238 $$ = Init;
239 delete $2;
240 } | ID {
241 if (const RecordVal *RV = CurRec->getValue(*$1)) {
242 $$ = new VarInit(*$1, RV->getType());
243 } else if (Record *D = Records.getDef(*$1)) {
244 $$ = new DefInit(D);
245 } else {
246 err() << "Variable not defined: '" << *$1 << "'!\n";
247 abort();
248 }
249
250 delete $1;
251 } | Value '{' BitList '}' {
252 $$ = $1->convertInitializerBitRange(*$3);
253 if ($$ == 0) {
254 err() << "Invalid bit range for value '" << *$1 << "'!\n";
255 abort();
256 }
257 delete $3;
258 } | '[' DefList ']' {
259 $$ = new ListInit(*$2);
260 delete $2;
Chris Lattner34a77692002-12-02 16:43:43 +0000261 } | Value '.' ID {
262 if (!$1->getFieldType(*$3)) {
263 err() << "Cannot access field '" << *$3 << "' of value '" << *$1 << "!\n";
264 abort();
265 }
266 $$ = new FieldInit($1, *$3);
267 delete $3;
Chris Lattnere62c1182002-12-02 01:23:04 +0000268 };
269
270DefList : /*empty */ {
271 $$ = new std::vector<Record*>();
272 } | DefListNE {
273 $$ = $1;
274 };
275DefListNE : DefID {
276 $$ = new std::vector<Record*>();
277 $$->push_back($1);
278 } | DefListNE ',' DefID {
279 ($$=$1)->push_back($3);
280 };
281
282
283RBitList : INTVAL {
284 $$ = new std::vector<unsigned>();
285 $$->push_back($1);
286 } | INTVAL '-' INTVAL {
287 if ($1 < $3 || $1 < 0 || $3 < 0) {
288 err() << "Invalid bit range: " << $1 << "-" << $3 << "!\n";
289 abort();
290 }
291 $$ = new std::vector<unsigned>();
292 for (int i = $1; i >= $3; --i)
293 $$->push_back(i);
294 } | INTVAL INTVAL {
295 $2 = -$2;
296 if ($1 < $2 || $1 < 0 || $2 < 0) {
297 err() << "Invalid bit range: " << $1 << "-" << $2 << "!\n";
298 abort();
299 }
300 $$ = new std::vector<unsigned>();
301 for (int i = $1; i >= $2; --i)
302 $$->push_back(i);
303 } | RBitList ',' INTVAL {
304 ($$=$1)->push_back($3);
305 } | RBitList ',' INTVAL '-' INTVAL {
306 if ($3 < $5 || $3 < 0 || $5 < 0) {
307 err() << "Invalid bit range: " << $3 << "-" << $5 << "!\n";
308 abort();
309 }
310 $$ = $1;
311 for (int i = $3; i >= $5; --i)
312 $$->push_back(i);
313 } | RBitList ',' INTVAL INTVAL {
314 $4 = -$4;
315 if ($3 < $4 || $3 < 0 || $4 < 0) {
316 err() << "Invalid bit range: " << $3 << "-" << $4 << "!\n";
317 abort();
318 }
319 $$ = $1;
320 for (int i = $3; i >= $4; --i)
321 $$->push_back(i);
322 };
323
324BitList : RBitList { $$ = $1; std::reverse($1->begin(), $1->end()); };
325
326OptBitList : /*empty*/ { $$ = 0; } | '{' BitList '}' { $$ = $2; };
327
328
329
330ValueList : /*empty*/ {
331 $$ = new std::vector<Init*>();
332 } | ValueListNE {
333 $$ = $1;
334 };
335
336ValueListNE : Value {
337 $$ = new std::vector<Init*>();
338 $$->push_back($1);
339 } | ValueListNE ',' Value {
340 ($$ = $1)->push_back($3);
341 };
342
343Declaration : OptPrefix Type ID OptValue {
344 addValue(RecordVal(*$3, $2, $1));
345 setValue(*$3, 0, $4);
346 $$ = $3;
347};
348
349BodyItem : Declaration ';' {
350 delete $1;
351} | SET ID OptBitList '=' Value ';' {
352 setValue(*$2, $3, $5);
353 delete $2;
354 delete $3;
355};
356
357BodyList : /*empty*/ | BodyList BodyItem;
358Body : ';' | '{' BodyList '}';
359
360SubClassRef : ClassID {
361 $$ = new SubClassRefTy($1, new std::vector<Init*>());
362 } | ClassID '<' ValueListNE '>' {
363 $$ = new SubClassRefTy($1, $3);
364 };
365
366ClassListNE : SubClassRef {
367 $$ = new std::vector<SubClassRefTy>();
368 $$->push_back(*$1);
369 delete $1;
370 }
371 | ClassListNE ',' SubClassRef {
372 ($$=$1)->push_back(*$3);
373 delete $3;
374 };
375
376ClassList : /*empty */ {
377 $$ = new std::vector<SubClassRefTy>();
378 }
379 | ':' ClassListNE {
380 $$ = $2;
381 };
382
383DeclListNE : Declaration {
384 CurRec->addTemplateArg(*$1);
385 delete $1;
386} | DeclListNE ',' Declaration {
387 CurRec->addTemplateArg(*$3);
388 delete $3;
389};
390
391TemplateArgList : '<' DeclListNE '>' {};
392OptTemplateArgList : /*empty*/ | TemplateArgList;
393
394ObjectBody : ID {
395 CurRec = new Record(*$1);
396 delete $1;
397 } OptTemplateArgList ClassList {
398 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
399 addSubClass((*$4)[i].first, *(*$4)[i].second);
400 delete (*$4)[i].second; // Delete the template list
401 }
402 delete $4;
403
404 // Process any variables on the set stack...
405 for (unsigned i = 0, e = SetStack.size(); i != e; ++i)
406 setValue(SetStack[i].first.first, SetStack[i].first.second,
407 SetStack[i].second);
408 } Body {
409 CurRec->resolveReferences();
410 $$ = CurRec;
411 CurRec = 0;
412};
413
414ClassInst : CLASS ObjectBody {
415 if (Records.getClass($2->getName())) {
416 err() << "Class '" << $2->getName() << "' already defined!\n";
417 abort();
418 }
419 Records.addClass($$ = $2);
420};
421
422DefInst : DEF ObjectBody {
423 // TODO: If ObjectBody has template arguments, it's an error.
424 if (Records.getDef($2->getName())) {
425 err() << "Def '" << $2->getName() << "' already defined!\n";
426 abort();
427 }
428 Records.addDef($$ = $2);
429};
430
431
432Object : ClassInst | DefInst;
433
434// Support Set commands wrapping objects...
435Object : SET ID OptBitList '=' Value IN {
436 SetStack.push_back(std::make_pair(std::make_pair(*$2, $3), $5));
437 delete $2;
438 } '{' ObjectList '}' {
439 delete SetStack.back().first.second; // Delete OptBitList
440 SetStack.pop_back();
441 };
442
443ObjectList : Object {} | ObjectList Object {};
444
445File : ObjectList {};
446
447%%
448
449int yyerror(const char *ErrorMsg) {
450 err() << "Error parsing: " << ErrorMsg << "\n";
451 abort();
452}