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