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