blob: b628df2048ad6c6a62ccc38a9d6a3a5179b9c50f [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
John Criswelld3032032003-10-20 20:20:30 +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 Lattnerf5bd1b72003-10-05 19:27:59 +00009//
10// This file defines the main TableGen data structures, including the TableGen
11// types, values, and high-level data structures.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef RECORD_H
16#define RECORD_H
17
18#include <string>
19#include <vector>
20#include <map>
21#include <iostream>
22#include <cassert>
23
Brian Gaeke960707c2003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026// RecTy subclasses...
27class BitRecTy;
28class BitsRecTy;
29class IntRecTy;
30class StringRecTy;
31class ListRecTy;
32class CodeRecTy;
33class DagRecTy;
34class RecordRecTy;
35
36// Init subclasses...
37class Init;
38class UnsetInit;
39class BitInit;
40class BitsInit;
41class IntInit;
42class StringInit;
43class CodeInit;
44class ListInit;
45class DefInit;
46class DagInit;
47class TypedInit;
48class VarInit;
49class FieldInit;
50class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000051class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000052
53// Other classes...
54class Record;
55
56//===----------------------------------------------------------------------===//
57// Type Classes
58//===----------------------------------------------------------------------===//
59
60struct RecTy {
61 virtual ~RecTy() {}
62
63 virtual void print(std::ostream &OS) const = 0;
64 void dump() const;
65
66 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
67 /// converted to the specified type.
68 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
69
70public: // These methods should only be called from subclasses of Init
71 virtual Init *convertValue( UnsetInit *UI) { return 0; }
72 virtual Init *convertValue( BitInit *BI) { return 0; }
73 virtual Init *convertValue( BitsInit *BI) { return 0; }
74 virtual Init *convertValue( IntInit *II) { return 0; }
75 virtual Init *convertValue(StringInit *SI) { return 0; }
76 virtual Init *convertValue( ListInit *LI) { return 0; }
77 virtual Init *convertValue( CodeInit *CI) { return 0; }
78 virtual Init *convertValue(VarBitInit *VB) { return 0; }
79 virtual Init *convertValue( DefInit *DI) { return 0; }
80 virtual Init *convertValue( DagInit *DI) { return 0; }
81 virtual Init *convertValue( TypedInit *TI) { return 0; }
82 virtual Init *convertValue( VarInit *VI) {
83 return convertValue((TypedInit*)VI);
84 }
85 virtual Init *convertValue( FieldInit *FI) {
86 return convertValue((TypedInit*)FI);
87 }
88
89public: // These methods should only be called by subclasses of RecTy.
90 // baseClassOf - These virtual methods should be overloaded to return true iff
91 // all values of type 'RHS' can be converted to the 'this' type.
92 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
93 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
94 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
95 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
96 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
97 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
100};
101
102inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
103 Ty.print(OS);
104 return OS;
105}
106
107
108/// BitRecTy - 'bit' - Represent a single bit
109///
110struct BitRecTy : public RecTy {
111 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
112 Init *convertValue(BitInit *BI) { return (Init*)BI; }
113 Init *convertValue(BitsInit *BI);
114 Init *convertValue(IntInit *II);
115 Init *convertValue(TypedInit *VI);
116 Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
117
118 void print(std::ostream &OS) const { OS << "bit"; }
119
120 bool typeIsConvertibleTo(const RecTy *RHS) const {
121 return RHS->baseClassOf(this);
122 }
123 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
124 virtual bool baseClassOf(const BitsRecTy *RHS) const;
125 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
126};
127
128
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000129// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
130/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000131///
132class BitsRecTy : public RecTy {
133 unsigned Size;
134public:
135 BitsRecTy(unsigned Sz) : Size(Sz) {}
136
137 unsigned getNumBits() const { return Size; }
138
139 Init *convertValue(UnsetInit *UI);
140 Init *convertValue(BitInit *UI);
141 Init *convertValue(BitsInit *BI);
142 Init *convertValue(IntInit *II);
143 Init *convertValue(TypedInit *VI);
144
145 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
146
147 bool typeIsConvertibleTo(const RecTy *RHS) const {
148 return RHS->baseClassOf(this);
149 }
150 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
151 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
152 virtual bool baseClassOf(const BitsRecTy *RHS) const {
153 return RHS->Size == Size;
154 }
155};
156
157
158/// IntRecTy - 'int' - Represent an integer value of no particular size
159///
160struct IntRecTy : public RecTy {
161 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
162 Init *convertValue(IntInit *II) { return (Init*)II; }
163 Init *convertValue(BitInit *BI);
164 Init *convertValue(BitsInit *BI);
165 Init *convertValue(TypedInit *TI);
166
167 void print(std::ostream &OS) const { OS << "int"; }
168
169 bool typeIsConvertibleTo(const RecTy *RHS) const {
170 return RHS->baseClassOf(this);
171 }
172
173 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
174 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
175 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
176};
177
178/// StringRecTy - 'string' - Represent an string value
179///
180struct StringRecTy : public RecTy {
181 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
182 Init *convertValue(StringInit *SI) { return (Init*)SI; }
183 Init *convertValue(TypedInit *TI);
184 void print(std::ostream &OS) const { OS << "string"; }
185
186 bool typeIsConvertibleTo(const RecTy *RHS) const {
187 return RHS->baseClassOf(this);
188 }
189
190 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
191};
192
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000193// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
194// the specified type.
195/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
196/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000197///
198class ListRecTy : public RecTy {
199 RecTy *Ty;
200public:
201 ListRecTy(RecTy *T) : Ty(T) {}
202
203 RecTy *getElementType() const { return Ty; }
204
205 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
206 Init *convertValue(ListInit *LI);
207 Init *convertValue(TypedInit *TI);
208
209 void print(std::ostream &OS) const;
210
211 bool typeIsConvertibleTo(const RecTy *RHS) const {
212 return RHS->baseClassOf(this);
213 }
214
215 virtual bool baseClassOf(const ListRecTy *RHS) const {
216 return RHS->getElementType()->typeIsConvertibleTo(Ty);
217 }
218};
219
220/// CodeRecTy - 'code' - Represent an code fragment, function or method.
221///
222struct CodeRecTy : public RecTy {
223 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
224 Init *convertValue( CodeInit *CI) { return (Init*)CI; }
225 Init *convertValue(TypedInit *TI);
226
227 void print(std::ostream &OS) const { OS << "code"; }
228
229 bool typeIsConvertibleTo(const RecTy *RHS) const {
230 return RHS->baseClassOf(this);
231 }
232 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
233};
234
235/// DagRecTy - 'dag' - Represent a dag fragment
236///
237struct DagRecTy : public RecTy {
238 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
239 Init *convertValue( DagInit *CI) { return (Init*)CI; }
240 Init *convertValue(TypedInit *TI);
241
242 void print(std::ostream &OS) const { OS << "dag"; }
243
244 bool typeIsConvertibleTo(const RecTy *RHS) const {
245 return RHS->baseClassOf(this);
246 }
247 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
248};
249
250
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000251/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000252/// (R32 X = EAX).
253///
254class RecordRecTy : public RecTy {
255 Record *Rec;
256public:
257 RecordRecTy(Record *R) : Rec(R) {}
258
259 Record *getRecord() const { return Rec; }
260
261 Init *convertValue(UnsetInit *UI) { return (Init*)UI; }
262 Init *convertValue( DefInit *DI);
263 Init *convertValue(TypedInit *VI);
264
265 void print(std::ostream &OS) const;
266
267 bool typeIsConvertibleTo(const RecTy *RHS) const {
268 return RHS->baseClassOf(this);
269 }
270 virtual bool baseClassOf(const RecordRecTy *RHS) const;
271};
272
273
274
275//===----------------------------------------------------------------------===//
276// Initializer Classes
277//===----------------------------------------------------------------------===//
278
279struct Init {
280 virtual ~Init() {}
281
282 /// isComplete - This virtual method should be overridden by values that may
283 /// not be completely specified yet.
284 virtual bool isComplete() const { return true; }
285
286 /// print - Print out this value.
287 virtual void print(std::ostream &OS) const = 0;
288
289 /// dump - Debugging method that may be called through a debugger, just
290 /// invokes print on cerr.
291 void dump() const;
292
293 /// convertInitializerTo - This virtual function is a simple call-back
294 /// function that should be overridden to call the appropriate
295 /// RecTy::convertValue method.
296 ///
297 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
298
299 /// convertInitializerBitRange - This method is used to implement the bitrange
300 /// selection operator. Given an initializer, it selects the specified bits
301 /// out, returning them as a new init of bits type. If it is not legal to use
302 /// the bit subscript operator on this initializer, return null.
303 ///
304 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
305 return 0;
306 }
307
Chris Lattner8bf9e062004-07-26 23:21:34 +0000308 /// convertInitListSlice - This method is used to implement the list slice
309 /// selection operator. Given an initializer, it selects the specified list
310 /// elements, returning them as a new init of list type. If it is not legal
311 /// to take a slice of this, return null.
312 ///
313 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
314 return 0;
315 }
316
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000317 /// getFieldType - This method is used to implement the FieldInit class.
318 /// Implementors of this method should return the type of the named field if
319 /// they are of record type.
320 ///
321 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
322
323 /// getFieldInit - This method complements getFieldType to return the
324 /// initializer for the specified field. If getFieldType returns non-null
325 /// this method should return non-null, otherwise it returns null.
326 ///
327 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
328 return 0;
329 }
330
331 /// resolveReferences - This method is used by classes that refer to other
332 /// variables which may not be defined at the time they expression is formed.
333 /// If a value is set for the variable later, this method will be called on
334 /// users of the value to allow the value to propagate out.
335 ///
336 virtual Init *resolveReferences(Record &R) { return this; }
337};
338
339inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
340 I.print(OS); return OS;
341}
342
343
344/// UnsetInit - ? - Represents an uninitialized value
345///
346struct UnsetInit : public Init {
347 virtual Init *convertInitializerTo(RecTy *Ty) {
348 return Ty->convertValue(this);
349 }
350
351 virtual bool isComplete() const { return false; }
352 virtual void print(std::ostream &OS) const { OS << "?"; }
353};
354
355
356/// BitInit - true/false - Represent a concrete initializer for a bit.
357///
358class BitInit : public Init {
359 bool Value;
360public:
361 BitInit(bool V) : Value(V) {}
362
363 bool getValue() const { return Value; }
364
365 virtual Init *convertInitializerTo(RecTy *Ty) {
366 return Ty->convertValue(this);
367 }
368
369 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
370};
371
372/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
373/// It contains a vector of bits, whose size is determined by the type.
374///
375class BitsInit : public Init {
376 std::vector<Init*> Bits;
377public:
378 BitsInit(unsigned Size) : Bits(Size) {}
379
380 unsigned getNumBits() const { return Bits.size(); }
381
382 Init *getBit(unsigned Bit) const {
383 assert(Bit < Bits.size() && "Bit index out of range!");
384 return Bits[Bit];
385 }
386 void setBit(unsigned Bit, Init *V) {
387 assert(Bit < Bits.size() && "Bit index out of range!");
388 assert(Bits[Bit] == 0 && "Bit already set!");
389 Bits[Bit] = V;
390 }
391
392 virtual Init *convertInitializerTo(RecTy *Ty) {
393 return Ty->convertValue(this);
394 }
395 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
396
397 virtual bool isComplete() const {
398 for (unsigned i = 0; i != getNumBits(); ++i)
399 if (!getBit(i)->isComplete()) return false;
400 return true;
401 }
402 virtual void print(std::ostream &OS) const;
403
404 virtual Init *resolveReferences(Record &R);
405
406 // printXX - Print this bitstream with the specified format, returning true if
407 // it is not possible.
408 bool printInHex(std::ostream &OS) const;
409 bool printAsVariable(std::ostream &OS) const;
410 bool printAsUnset(std::ostream &OS) const;
411};
412
413
414/// IntInit - 7 - Represent an initalization by a literal integer value.
415///
416class IntInit : public Init {
417 int Value;
418public:
419 IntInit(int V) : Value(V) {}
420
421 int getValue() const { return Value; }
422
423 virtual Init *convertInitializerTo(RecTy *Ty) {
424 return Ty->convertValue(this);
425 }
426 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
427
428 virtual void print(std::ostream &OS) const { OS << Value; }
429};
430
431
432/// StringInit - "foo" - Represent an initialization by a string value.
433///
434class StringInit : public Init {
435 std::string Value;
436public:
437 StringInit(const std::string &V) : Value(V) {}
438
439 const std::string &getValue() const { return Value; }
440
441 virtual Init *convertInitializerTo(RecTy *Ty) {
442 return Ty->convertValue(this);
443 }
444
445 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
446};
447
448/// CodeInit - "[{...}]" - Represent a code fragment.
449///
450class CodeInit : public Init {
451 std::string Value;
452public:
453 CodeInit(const std::string &V) : Value(V) {}
454
455 const std::string getValue() const { return Value; }
456
457 virtual Init *convertInitializerTo(RecTy *Ty) {
458 return Ty->convertValue(this);
459 }
460
461 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
462};
463
464/// ListInit - [AL, AH, CL] - Represent a list of defs
465///
466class ListInit : public Init {
467 std::vector<Init*> Values;
468public:
469 ListInit(std::vector<Init*> &Vs) {
470 Values.swap(Vs);
471 }
472
473 unsigned getSize() const { return Values.size(); }
474 Init *getElement(unsigned i) const {
475 assert(i < Values.size() && "List element index out of range!");
476 return Values[i];
477 }
478
Chris Lattner8bf9e062004-07-26 23:21:34 +0000479 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
480
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000481 virtual Init *convertInitializerTo(RecTy *Ty) {
482 return Ty->convertValue(this);
483 }
484
Chris Lattner577fc3f2004-07-27 01:01:21 +0000485 /// resolveReferences - This method is used by classes that refer to other
486 /// variables which may not be defined at the time they expression is formed.
487 /// If a value is set for the variable later, this method will be called on
488 /// users of the value to allow the value to propagate out.
489 ///
490 virtual Init *resolveReferences(Record &R);
491
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000492 virtual void print(std::ostream &OS) const;
493};
494
495
496/// TypedInit - This is the common super-class of types that have a specific,
497/// explicit, type.
498///
499class TypedInit : public Init {
500 RecTy *Ty;
501public:
502 TypedInit(RecTy *T) : Ty(T) {}
503
504 RecTy *getType() const { return Ty; }
505
Chris Lattner577fc3f2004-07-27 01:01:21 +0000506 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
507 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
508
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000509 /// resolveBitReference - This method is used to implement
510 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000511 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000512 ///
513 virtual Init *resolveBitReference(Record &R, unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000514
515 /// resolveListElementReference - This method is used to implement
516 /// VarListElementInit::resolveReferences. If the list element is resolvable
517 /// now, we return the resolved value, otherwise we return null.
518 virtual Init *resolveListElementReference(Record &R, unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000519};
520
521/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
522///
523class VarInit : public TypedInit {
524 std::string VarName;
525public:
526 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
527
528 virtual Init *convertInitializerTo(RecTy *Ty) {
529 return Ty->convertValue(this);
530 }
531
532 const std::string &getName() const { return VarName; }
533
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000534 virtual Init *resolveBitReference(Record &R, unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000535 virtual Init *resolveListElementReference(Record &R, unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000536
537 virtual RecTy *getFieldType(const std::string &FieldName) const;
538 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
539
540 /// resolveReferences - This method is used by classes that refer to other
541 /// variables which may not be defined at the time they expression is formed.
542 /// If a value is set for the variable later, this method will be called on
543 /// users of the value to allow the value to propagate out.
544 ///
545 virtual Init *resolveReferences(Record &R);
546
547 virtual void print(std::ostream &OS) const { OS << VarName; }
548};
549
550
551/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
552///
553class VarBitInit : public Init {
554 TypedInit *TI;
555 unsigned Bit;
556public:
557 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
558 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
559 ((BitsRecTy*)T->getType())->getNumBits() > B &&
560 "Illegal VarBitInit expression!");
561 }
562
563 virtual Init *convertInitializerTo(RecTy *Ty) {
564 return Ty->convertValue(this);
565 }
566
567 TypedInit *getVariable() const { return TI; }
568 unsigned getBitNum() const { return Bit; }
569
570 virtual void print(std::ostream &OS) const {
571 TI->print(OS); OS << "{" << Bit << "}";
572 }
573 virtual Init *resolveReferences(Record &R);
574};
575
Chris Lattner577fc3f2004-07-27 01:01:21 +0000576/// VarListElementInit - List[4] - Represent access to one element of a var or
577/// field.
578class VarListElementInit : public TypedInit {
579 TypedInit *TI;
580 unsigned Element;
581public:
582 VarListElementInit(TypedInit *T, unsigned E)
583 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
584 TI(T), Element(E) {
585 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
586 "Illegal VarBitInit expression!");
587 }
588
589 virtual Init *convertInitializerTo(RecTy *Ty) {
590 return Ty->convertValue(this);
591 }
592
593 TypedInit *getVariable() const { return TI; }
594 unsigned getElementNum() const { return Element; }
595
596 virtual Init *resolveBitReference(Record &R, unsigned Bit);
597
598 /// resolveListElementReference - This method is used to implement
599 /// VarListElementInit::resolveReferences. If the list element is resolvable
600 /// now, we return the resolved value, otherwise we return null.
601 virtual Init *resolveListElementReference(Record &R, unsigned Elt);
602
603 virtual void print(std::ostream &OS) const {
604 TI->print(OS); OS << "[" << Element << "]";
605 }
606 virtual Init *resolveReferences(Record &R);
607};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000608
609/// DefInit - AL - Represent a reference to a 'def' in the description
610///
611class DefInit : public Init {
612 Record *Def;
613public:
614 DefInit(Record *D) : Def(D) {}
615
616 virtual Init *convertInitializerTo(RecTy *Ty) {
617 return Ty->convertValue(this);
618 }
619
620 Record *getDef() const { return Def; }
621
622 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
623
624 virtual RecTy *getFieldType(const std::string &FieldName) const;
625 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
626
627 virtual void print(std::ostream &OS) const;
628};
629
630
631/// FieldInit - X.Y - Represent a reference to a subfield of a variable
632///
633class FieldInit : public TypedInit {
634 Init *Rec; // Record we are referring to
635 std::string FieldName; // Field we are accessing
636public:
637 FieldInit(Init *R, const std::string &FN)
638 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
639 assert(getType() && "FieldInit with non-record type!");
640 }
641
642 virtual Init *convertInitializerTo(RecTy *Ty) {
643 return Ty->convertValue(this);
644 }
645
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000646 virtual Init *resolveBitReference(Record &R, unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000647 virtual Init *resolveListElementReference(Record &R, unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000648
649 virtual Init *resolveReferences(Record &R);
650
651 virtual void print(std::ostream &OS) const {
652 Rec->print(OS); OS << "." << FieldName;
653 }
654};
655
656/// DagInit - (def a, b) - Represent a DAG tree value. DAG inits are required
657/// to have Records for their first value, after that, any legal Init is
658/// possible.
659///
660class DagInit : public Init {
661 Record *NodeTypeDef;
662 std::vector<Init*> Args;
663 std::vector<std::string> ArgNames;
664public:
665 DagInit(Record *D, const std::vector<std::pair<Init*, std::string> > &args)
666 : NodeTypeDef(D) {
667 Args.reserve(args.size());
668 ArgNames.reserve(args.size());
669 for (unsigned i = 0, e = args.size(); i != e; ++i) {
670 Args.push_back(args[i].first);
671 ArgNames.push_back(args[i].second);
672 }
673 }
674
675 virtual Init *convertInitializerTo(RecTy *Ty) {
676 return Ty->convertValue(this);
677 }
678
679 Record *getNodeType() const { return NodeTypeDef; }
680
681 unsigned getNumArgs() const { return Args.size(); }
682 Init *getArg(unsigned Num) const {
683 assert(Num < Args.size() && "Arg number out of range!");
684 return Args[Num];
685 }
686 const std::string &getArgName(unsigned Num) const {
687 assert(Num < ArgNames.size() && "Arg number out of range!");
688 return ArgNames[Num];
689 }
690
691 void setArg(unsigned Num, Init *I) {
692 assert(Num < Args.size() && "Arg number out of range!");
693 Args[Num] = I;
694 }
695
696 virtual void print(std::ostream &OS) const;
697};
698
699//===----------------------------------------------------------------------===//
700// High-Level Classes
701//===----------------------------------------------------------------------===//
702
703class RecordVal {
704 std::string Name;
705 RecTy *Ty;
706 unsigned Prefix;
707 Init *Value;
708public:
709 RecordVal(const std::string &N, RecTy *T, unsigned P);
710
711 const std::string &getName() const { return Name; }
712
713 unsigned getPrefix() const { return Prefix; }
714 RecTy *getType() const { return Ty; }
715 Init *getValue() const { return Value; }
716
717 bool setValue(Init *V) {
718 if (V) {
719 Value = V->convertInitializerTo(Ty);
720 return Value == 0;
721 }
722 Value = 0;
723 return false;
724 }
725
726 void dump() const;
727 void print(std::ostream &OS, bool PrintSem = true) const;
728};
729
730inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
731 RV.print(OS << " ");
732 return OS;
733}
734
735struct Record {
736 const std::string Name;
737 std::vector<std::string> TemplateArgs;
738 std::vector<RecordVal> Values;
739 std::vector<Record*> SuperClasses;
740public:
741
742 Record(const std::string &N) : Name(N) {}
743 ~Record() {}
744
745 const std::string &getName() const { return Name; }
746 const std::vector<std::string> &getTemplateArgs() const {
747 return TemplateArgs;
748 }
749 const std::vector<RecordVal> &getValues() const { return Values; }
750 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
751
752 bool isTemplateArg(const std::string &Name) const {
753 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
754 if (TemplateArgs[i] == Name) return true;
755 return false;
756 }
757
758 const RecordVal *getValue(const std::string &Name) const {
759 for (unsigned i = 0, e = Values.size(); i != e; ++i)
760 if (Values[i].getName() == Name) return &Values[i];
761 return 0;
762 }
763 RecordVal *getValue(const std::string &Name) {
764 for (unsigned i = 0, e = Values.size(); i != e; ++i)
765 if (Values[i].getName() == Name) return &Values[i];
766 return 0;
767 }
768
769 void addTemplateArg(const std::string &Name) {
770 assert(!isTemplateArg(Name) && "Template arg already defined!");
771 TemplateArgs.push_back(Name);
772 }
773
774 void addValue(const RecordVal &RV) {
775 assert(getValue(RV.getName()) == 0 && "Value already added!");
776 Values.push_back(RV);
777 }
778
779 void removeValue(const std::string &Name) {
780 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
781 for (unsigned i = 0, e = Values.size(); i != e; ++i)
782 if (Values[i].getName() == Name) {
783 Values.erase(Values.begin()+i);
784 return;
785 }
786 assert(0 && "Name does not exist in record!");
787 }
788
789 bool isSubClassOf(Record *R) const {
790 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
791 if (SuperClasses[i] == R)
792 return true;
793 return false;
794 }
795
796 bool isSubClassOf(const std::string &Name) const {
797 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
798 if (SuperClasses[i]->getName() == Name)
799 return true;
800 return false;
801 }
802
803 void addSuperClass(Record *R) {
804 assert(!isSubClassOf(R) && "Already subclassing record!");
805 SuperClasses.push_back(R);
806 }
807
808 // resolveReferences - If there are any field references that refer to fields
809 // that have been filled in, we can propagate the values now.
810 //
811 void resolveReferences();
812
813 void dump() const;
814
815 //===--------------------------------------------------------------------===//
816 // High-level methods useful to tablegen back-ends
817 //
818
819 /// getValueInit - Return the initializer for a value with the specified name,
820 /// or throw an exception if the field does not exist.
821 ///
822 Init *getValueInit(const std::string &FieldName) const;
823
824 /// getValueAsString - This method looks up the specified field and returns
825 /// its value as a string, throwing an exception if the field does not exist
826 /// or if the value is not a string.
827 ///
828 std::string getValueAsString(const std::string &FieldName) const;
829
830 /// getValueAsBitsInit - This method looks up the specified field and returns
831 /// its value as a BitsInit, throwing an exception if the field does not exist
832 /// or if the value is not the right type.
833 ///
834 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
835
836 /// getValueAsListInit - This method looks up the specified field and returns
837 /// its value as a ListInit, throwing an exception if the field does not exist
838 /// or if the value is not the right type.
839 ///
840 ListInit *getValueAsListInit(const std::string &FieldName) const;
841
842 /// getValueAsDef - This method looks up the specified field and returns its
843 /// value as a Record, throwing an exception if the field does not exist or if
844 /// the value is not the right type.
845 ///
846 Record *getValueAsDef(const std::string &FieldName) const;
847
848 /// getValueAsBit - This method looks up the specified field and returns its
849 /// value as a bit, throwing an exception if the field does not exist or if
850 /// the value is not the right type.
851 ///
852 bool getValueAsBit(const std::string &FieldName) const;
853
854 /// getValueAsInt - This method looks up the specified field and returns its
855 /// value as an int, throwing an exception if the field does not exist or if
856 /// the value is not the right type.
857 ///
858 int getValueAsInt(const std::string &FieldName) const;
859
860 /// getValueAsDag - This method looks up the specified field and returns its
861 /// value as an Dag, throwing an exception if the field does not exist or if
862 /// the value is not the right type.
863 ///
864 DagInit *getValueAsDag(const std::string &FieldName) const;
865};
866
867std::ostream &operator<<(std::ostream &OS, const Record &R);
868
869class RecordKeeper {
870 std::map<std::string, Record*> Classes, Defs;
871public:
872 ~RecordKeeper() {
873 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
874 E = Classes.end(); I != E; ++I)
875 delete I->second;
876 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
877 E = Defs.end(); I != E; ++I)
878 delete I->second;
879 }
880
881 const std::map<std::string, Record*> &getClasses() const { return Classes; }
882 const std::map<std::string, Record*> &getDefs() const { return Defs; }
883
884 Record *getClass(const std::string &Name) const {
885 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
886 return I == Classes.end() ? 0 : I->second;
887 }
888 Record *getDef(const std::string &Name) const {
889 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
890 return I == Defs.end() ? 0 : I->second;
891 }
892 void addClass(Record *R) {
893 assert(getClass(R->getName()) == 0 && "Class already exists!");
894 Classes.insert(std::make_pair(R->getName(), R));
895 }
896 void addDef(Record *R) {
897 assert(getDef(R->getName()) == 0 && "Def already exists!");
898 Defs.insert(std::make_pair(R->getName(), R));
899 }
900
901 //===--------------------------------------------------------------------===//
902 // High-level helper methods, useful for tablegen backends...
903
904 /// getAllDerivedDefinitions - This method returns all concrete definitions
905 /// that derive from the specified class name. If a class with the specified
906 /// name does not exist, an exception is thrown.
907 std::vector<Record*>
908 getAllDerivedDefinitions(const std::string &ClassName) const;
909
910
911 void dump() const;
912};
913
914std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
915
916extern RecordKeeper Records;
917
Brian Gaeke960707c2003-11-11 22:41:34 +0000918} // End llvm namespace
919
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000920#endif