1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/DataTypes.h"
53 class VarListElementInit;
60 //===----------------------------------------------------------------------===//
62 //===----------------------------------------------------------------------===//
67 virtual std::string getAsString() const = 0;
68 void print(std::ostream &OS) const { OS << getAsString(); }
71 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
72 /// converted to the specified type.
73 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
75 public: // These methods should only be called from subclasses of Init
76 virtual Init *convertValue( UnsetInit *UI) { return 0; }
77 virtual Init *convertValue( BitInit *BI) { return 0; }
78 virtual Init *convertValue( BitsInit *BI) { return 0; }
79 virtual Init *convertValue( IntInit *II) { return 0; }
80 virtual Init *convertValue(StringInit *SI) { return 0; }
81 virtual Init *convertValue( ListInit *LI) { return 0; }
82 virtual Init *convertValue( UnOpInit *UI) {
83 return convertValue((TypedInit*)UI);
85 virtual Init *convertValue( BinOpInit *UI) {
86 return convertValue((TypedInit*)UI);
88 virtual Init *convertValue( TernOpInit *UI) {
89 return convertValue((TypedInit*)UI);
91 virtual Init *convertValue( CodeInit *CI) { return 0; }
92 virtual Init *convertValue(VarBitInit *VB) { return 0; }
93 virtual Init *convertValue( DefInit *DI) { return 0; }
94 virtual Init *convertValue( DagInit *DI) { return 0; }
95 virtual Init *convertValue( TypedInit *TI) { return 0; }
96 virtual Init *convertValue( VarInit *VI) {
97 return convertValue((TypedInit*)VI);
99 virtual Init *convertValue( FieldInit *FI) {
100 return convertValue((TypedInit*)FI);
103 public: // These methods should only be called by subclasses of RecTy.
104 // baseClassOf - These virtual methods should be overloaded to return true iff
105 // all values of type 'RHS' can be converted to the 'this' type.
106 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
107 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
108 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
116 inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
122 /// BitRecTy - 'bit' - Represent a single bit
124 class BitRecTy : public RecTy {
126 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
127 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
128 virtual Init *convertValue( BitsInit *BI);
129 virtual Init *convertValue( IntInit *II);
130 virtual Init *convertValue(StringInit *SI) { return 0; }
131 virtual Init *convertValue( ListInit *LI) { return 0; }
132 virtual Init *convertValue( CodeInit *CI) { return 0; }
133 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
134 virtual Init *convertValue( DefInit *DI) { return 0; }
135 virtual Init *convertValue( DagInit *DI) { return 0; }
136 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
137 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
138 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
139 virtual Init *convertValue( TypedInit *TI);
140 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
141 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
143 std::string getAsString() const { return "bit"; }
145 bool typeIsConvertibleTo(const RecTy *RHS) const {
146 return RHS->baseClassOf(this);
148 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
149 virtual bool baseClassOf(const BitsRecTy *RHS) const;
150 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
151 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
152 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
153 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
160 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
161 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
163 class BitsRecTy : public RecTy {
166 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
168 unsigned getNumBits() const { return Size; }
170 virtual Init *convertValue( UnsetInit *UI);
171 virtual Init *convertValue( BitInit *UI);
172 virtual Init *convertValue( BitsInit *BI);
173 virtual Init *convertValue( IntInit *II);
174 virtual Init *convertValue(StringInit *SI) { return 0; }
175 virtual Init *convertValue( ListInit *LI) { return 0; }
176 virtual Init *convertValue( CodeInit *CI) { return 0; }
177 virtual Init *convertValue(VarBitInit *VB) { return 0; }
178 virtual Init *convertValue( DefInit *DI) { return 0; }
179 virtual Init *convertValue( DagInit *DI) { return 0; }
180 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
181 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
182 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
183 virtual Init *convertValue( TypedInit *TI);
184 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
185 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
187 std::string getAsString() const;
189 bool typeIsConvertibleTo(const RecTy *RHS) const {
190 return RHS->baseClassOf(this);
192 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
193 virtual bool baseClassOf(const BitsRecTy *RHS) const {
194 return RHS->Size == Size;
196 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
197 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
198 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
199 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
206 /// IntRecTy - 'int' - Represent an integer value of no particular size
208 class IntRecTy : public RecTy {
210 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
211 virtual Init *convertValue( BitInit *BI);
212 virtual Init *convertValue( BitsInit *BI);
213 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
214 virtual Init *convertValue(StringInit *SI) { return 0; }
215 virtual Init *convertValue( ListInit *LI) { return 0; }
216 virtual Init *convertValue( CodeInit *CI) { return 0; }
217 virtual Init *convertValue(VarBitInit *VB) { return 0; }
218 virtual Init *convertValue( DefInit *DI) { return 0; }
219 virtual Init *convertValue( DagInit *DI) { return 0; }
220 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
221 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
222 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
223 virtual Init *convertValue( TypedInit *TI);
224 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
225 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
227 std::string getAsString() const { return "int"; }
229 bool typeIsConvertibleTo(const RecTy *RHS) const {
230 return RHS->baseClassOf(this);
233 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
234 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
235 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
237 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
238 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
244 /// StringRecTy - 'string' - Represent an string value
246 class StringRecTy : public RecTy {
248 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
249 virtual Init *convertValue( BitInit *BI) { return 0; }
250 virtual Init *convertValue( BitsInit *BI) { return 0; }
251 virtual Init *convertValue( IntInit *II) { return 0; }
252 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
253 virtual Init *convertValue( ListInit *LI) { return 0; }
254 virtual Init *convertValue( UnOpInit *BO);
255 virtual Init *convertValue( BinOpInit *BO);
256 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
258 virtual Init *convertValue( CodeInit *CI) { return 0; }
259 virtual Init *convertValue(VarBitInit *VB) { return 0; }
260 virtual Init *convertValue( DefInit *DI) { return 0; }
261 virtual Init *convertValue( DagInit *DI) { return 0; }
262 virtual Init *convertValue( TypedInit *TI);
263 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
264 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
266 std::string getAsString() const { return "string"; }
268 bool typeIsConvertibleTo(const RecTy *RHS) const {
269 return RHS->baseClassOf(this);
272 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
273 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
274 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
275 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
276 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
277 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
278 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
282 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
283 // the specified type.
284 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
285 /// be of the specified type.
287 class ListRecTy : public RecTy {
290 explicit ListRecTy(RecTy *T) : Ty(T) {}
292 RecTy *getElementType() const { return Ty; }
294 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
295 virtual Init *convertValue( BitInit *BI) { return 0; }
296 virtual Init *convertValue( BitsInit *BI) { return 0; }
297 virtual Init *convertValue( IntInit *II) { return 0; }
298 virtual Init *convertValue(StringInit *SI) { return 0; }
299 virtual Init *convertValue( ListInit *LI);
300 virtual Init *convertValue( CodeInit *CI) { return 0; }
301 virtual Init *convertValue(VarBitInit *VB) { return 0; }
302 virtual Init *convertValue( DefInit *DI) { return 0; }
303 virtual Init *convertValue( DagInit *DI) { return 0; }
304 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
305 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
306 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
307 virtual Init *convertValue( TypedInit *TI);
308 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
309 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
311 std::string getAsString() const;
313 bool typeIsConvertibleTo(const RecTy *RHS) const {
314 return RHS->baseClassOf(this);
317 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
318 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
319 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const ListRecTy *RHS) const {
322 return RHS->getElementType()->typeIsConvertibleTo(Ty);
324 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
325 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
326 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
329 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
331 class CodeRecTy : public RecTy {
333 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
334 virtual Init *convertValue( BitInit *BI) { return 0; }
335 virtual Init *convertValue( BitsInit *BI) { return 0; }
336 virtual Init *convertValue( IntInit *II) { return 0; }
337 virtual Init *convertValue(StringInit *SI) { return 0; }
338 virtual Init *convertValue( ListInit *LI) { return 0; }
339 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
340 virtual Init *convertValue(VarBitInit *VB) { return 0; }
341 virtual Init *convertValue( DefInit *DI) { return 0; }
342 virtual Init *convertValue( DagInit *DI) { return 0; }
343 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
344 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
345 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
346 virtual Init *convertValue( TypedInit *TI);
347 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
348 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
350 std::string getAsString() const { return "code"; }
352 bool typeIsConvertibleTo(const RecTy *RHS) const {
353 return RHS->baseClassOf(this);
355 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
356 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
357 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
361 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
362 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
365 /// DagRecTy - 'dag' - Represent a dag fragment
367 class DagRecTy : public RecTy {
369 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
370 virtual Init *convertValue( BitInit *BI) { return 0; }
371 virtual Init *convertValue( BitsInit *BI) { return 0; }
372 virtual Init *convertValue( IntInit *II) { return 0; }
373 virtual Init *convertValue(StringInit *SI) { return 0; }
374 virtual Init *convertValue( ListInit *LI) { return 0; }
375 virtual Init *convertValue( CodeInit *CI) { return 0; }
376 virtual Init *convertValue(VarBitInit *VB) { return 0; }
377 virtual Init *convertValue( DefInit *DI) { return 0; }
378 virtual Init *convertValue( UnOpInit *BO);
379 virtual Init *convertValue( BinOpInit *BO);
380 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
381 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
382 virtual Init *convertValue( TypedInit *TI);
383 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
384 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
386 std::string getAsString() const { return "dag"; }
388 bool typeIsConvertibleTo(const RecTy *RHS) const {
389 return RHS->baseClassOf(this);
392 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
393 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
394 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
399 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
403 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
406 class RecordRecTy : public RecTy {
409 explicit RecordRecTy(Record *R) : Rec(R) {}
411 Record *getRecord() const { return Rec; }
413 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
414 virtual Init *convertValue( BitInit *BI) { return 0; }
415 virtual Init *convertValue( BitsInit *BI) { return 0; }
416 virtual Init *convertValue( IntInit *II) { return 0; }
417 virtual Init *convertValue(StringInit *SI) { return 0; }
418 virtual Init *convertValue( ListInit *LI) { return 0; }
419 virtual Init *convertValue( CodeInit *CI) { return 0; }
420 virtual Init *convertValue(VarBitInit *VB) { return 0; }
421 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
422 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
423 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
424 virtual Init *convertValue( DefInit *DI);
425 virtual Init *convertValue( DagInit *DI) { return 0; }
426 virtual Init *convertValue( TypedInit *VI);
427 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
428 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
430 std::string getAsString() const;
432 bool typeIsConvertibleTo(const RecTy *RHS) const {
433 return RHS->baseClassOf(this);
435 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
436 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
437 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const RecordRecTy *RHS) const;
445 /// resolveTypes - Find a common type that T1 and T2 convert to.
446 /// Return 0 if no such type exists.
448 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
450 //===----------------------------------------------------------------------===//
451 // Initializer Classes
452 //===----------------------------------------------------------------------===//
457 /// isComplete - This virtual method should be overridden by values that may
458 /// not be completely specified yet.
459 virtual bool isComplete() const { return true; }
461 /// print - Print out this value.
462 void print(std::ostream &OS) const { OS << getAsString(); }
464 /// getAsString - Convert this value to a string form.
465 virtual std::string getAsString() const = 0;
467 /// dump - Debugging method that may be called through a debugger, just
468 /// invokes print on cerr.
471 /// convertInitializerTo - This virtual function is a simple call-back
472 /// function that should be overridden to call the appropriate
473 /// RecTy::convertValue method.
475 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
477 /// convertInitializerBitRange - This method is used to implement the bitrange
478 /// selection operator. Given an initializer, it selects the specified bits
479 /// out, returning them as a new init of bits type. If it is not legal to use
480 /// the bit subscript operator on this initializer, return null.
482 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
486 /// convertInitListSlice - This method is used to implement the list slice
487 /// selection operator. Given an initializer, it selects the specified list
488 /// elements, returning them as a new init of list type. If it is not legal
489 /// to take a slice of this, return null.
491 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
495 /// getFieldType - This method is used to implement the FieldInit class.
496 /// Implementors of this method should return the type of the named field if
497 /// they are of record type.
499 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
501 /// getFieldInit - This method complements getFieldType to return the
502 /// initializer for the specified field. If getFieldType returns non-null
503 /// this method should return non-null, otherwise it returns null.
505 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
509 /// resolveReferences - This method is used by classes that refer to other
510 /// variables which may not be defined at the time they expression is formed.
511 /// If a value is set for the variable later, this method will be called on
512 /// users of the value to allow the value to propagate out.
514 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
519 inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
520 I.print(OS); return OS;
523 /// TypedInit - This is the common super-class of types that have a specific,
526 class TypedInit : public Init {
529 explicit TypedInit(RecTy *T) : Ty(T) {}
531 RecTy *getType() const { return Ty; }
533 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
534 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
536 /// resolveBitReference - This method is used to implement
537 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
538 /// simply return the resolved value, otherwise we return null.
540 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
543 /// resolveListElementReference - This method is used to implement
544 /// VarListElementInit::resolveReferences. If the list element is resolvable
545 /// now, we return the resolved value, otherwise we return null.
546 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
551 /// UnsetInit - ? - Represents an uninitialized value
553 class UnsetInit : public Init {
555 virtual Init *convertInitializerTo(RecTy *Ty) {
556 return Ty->convertValue(this);
559 virtual bool isComplete() const { return false; }
560 virtual std::string getAsString() const { return "?"; }
564 /// BitInit - true/false - Represent a concrete initializer for a bit.
566 class BitInit : public Init {
569 explicit BitInit(bool V) : Value(V) {}
571 bool getValue() const { return Value; }
573 virtual Init *convertInitializerTo(RecTy *Ty) {
574 return Ty->convertValue(this);
577 virtual std::string getAsString() const { return Value ? "1" : "0"; }
580 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
581 /// It contains a vector of bits, whose size is determined by the type.
583 class BitsInit : public Init {
584 std::vector<Init*> Bits;
586 explicit BitsInit(unsigned Size) : Bits(Size) {}
588 unsigned getNumBits() const { return Bits.size(); }
590 Init *getBit(unsigned Bit) const {
591 assert(Bit < Bits.size() && "Bit index out of range!");
594 void setBit(unsigned Bit, Init *V) {
595 assert(Bit < Bits.size() && "Bit index out of range!");
596 assert(Bits[Bit] == 0 && "Bit already set!");
600 virtual Init *convertInitializerTo(RecTy *Ty) {
601 return Ty->convertValue(this);
603 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
605 virtual bool isComplete() const {
606 for (unsigned i = 0; i != getNumBits(); ++i)
607 if (!getBit(i)->isComplete()) return false;
610 virtual std::string getAsString() const;
612 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
614 // printXX - Print this bitstream with the specified format, returning true if
615 // it is not possible.
616 bool printInHex(std::ostream &OS) const;
617 bool printAsVariable(std::ostream &OS) const;
618 bool printAsUnset(std::ostream &OS) const;
622 /// IntInit - 7 - Represent an initalization by a literal integer value.
624 class IntInit : public TypedInit {
627 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
629 int64_t getValue() const { return Value; }
631 virtual Init *convertInitializerTo(RecTy *Ty) {
632 return Ty->convertValue(this);
634 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
636 virtual std::string getAsString() const;
638 /// resolveBitReference - This method is used to implement
639 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
640 /// simply return the resolved value, otherwise we return null.
642 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
644 assert(0 && "Illegal bit reference off int");
648 /// resolveListElementReference - This method is used to implement
649 /// VarListElementInit::resolveReferences. If the list element is resolvable
650 /// now, we return the resolved value, otherwise we return null.
651 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
653 assert(0 && "Illegal element reference off int");
659 /// StringInit - "foo" - Represent an initialization by a string value.
661 class StringInit : public TypedInit {
664 explicit StringInit(const std::string &V)
665 : TypedInit(new StringRecTy), Value(V) {}
667 const std::string &getValue() const { return Value; }
669 virtual Init *convertInitializerTo(RecTy *Ty) {
670 return Ty->convertValue(this);
673 virtual std::string getAsString() const { return "\"" + Value + "\""; }
675 /// resolveBitReference - This method is used to implement
676 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
677 /// simply return the resolved value, otherwise we return null.
679 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
681 assert(0 && "Illegal bit reference off string");
685 /// resolveListElementReference - This method is used to implement
686 /// VarListElementInit::resolveReferences. If the list element is resolvable
687 /// now, we return the resolved value, otherwise we return null.
688 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
690 assert(0 && "Illegal element reference off string");
695 /// CodeInit - "[{...}]" - Represent a code fragment.
697 class CodeInit : public Init {
700 explicit CodeInit(const std::string &V) : Value(V) {}
702 const std::string getValue() const { return Value; }
704 virtual Init *convertInitializerTo(RecTy *Ty) {
705 return Ty->convertValue(this);
708 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
711 /// ListInit - [AL, AH, CL] - Represent a list of defs
713 class ListInit : public TypedInit {
714 std::vector<Init*> Values;
716 typedef std::vector<Init*>::iterator iterator;
717 typedef std::vector<Init*>::const_iterator const_iterator;
719 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
720 : TypedInit(new ListRecTy(EltTy)) {
723 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
724 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
726 unsigned getSize() const { return Values.size(); }
727 Init *getElement(unsigned i) const {
728 assert(i < Values.size() && "List element index out of range!");
732 Record *getElementAsRecord(unsigned i) const;
734 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
736 virtual Init *convertInitializerTo(RecTy *Ty) {
737 return Ty->convertValue(this);
740 /// resolveReferences - This method is used by classes that refer to other
741 /// variables which may not be defined at the time they expression is formed.
742 /// If a value is set for the variable later, this method will be called on
743 /// users of the value to allow the value to propagate out.
745 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
747 virtual std::string getAsString() const;
749 inline iterator begin() { return Values.begin(); }
750 inline const_iterator begin() const { return Values.begin(); }
751 inline iterator end () { return Values.end(); }
752 inline const_iterator end () const { return Values.end(); }
754 inline size_t size () const { return Values.size(); }
755 inline bool empty() const { return Values.empty(); }
757 /// resolveBitReference - This method is used to implement
758 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
759 /// simply return the resolved value, otherwise we return null.
761 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
763 assert(0 && "Illegal bit reference off list");
767 /// resolveListElementReference - This method is used to implement
768 /// VarListElementInit::resolveReferences. If the list element is resolvable
769 /// now, we return the resolved value, otherwise we return null.
770 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
775 /// OpInit - Base class for operators
777 class OpInit : public TypedInit {
779 OpInit(RecTy *Type) : TypedInit(Type) {}
781 // Clone - Clone this operator, replacing arguments with the new list
782 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
784 virtual int getNumOperands(void) const = 0;
785 virtual Init *getOperand(int i) = 0;
787 // Fold - If possible, fold this to a simpler init. Return this if not
789 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
791 virtual Init *convertInitializerTo(RecTy *Ty) {
792 return Ty->convertValue(this);
795 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
797 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
802 /// UnOpInit - !op (X) - Transform an init.
804 class UnOpInit : public OpInit {
806 enum UnaryOp { CAST, CAR, CDR, LNULL };
811 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
812 OpInit(Type), Opc(opc), LHS(lhs) {
815 // Clone - Clone this operator, replacing arguments with the new list
816 virtual OpInit *clone(std::vector<Init *> &Operands) {
817 assert(Operands.size() == 1 &&
818 "Wrong number of operands for unary operation");
819 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
822 int getNumOperands(void) const { return 1; }
823 Init *getOperand(int i) {
824 assert(i == 0 && "Invalid operand id for unary operator");
828 UnaryOp getOpcode() const { return Opc; }
829 Init *getOperand() const { return LHS; }
831 // Fold - If possible, fold this to a simpler init. Return this if not
833 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
835 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
837 virtual std::string getAsString() const;
840 /// BinOpInit - !op (X, Y) - Combine two inits.
842 class BinOpInit : public OpInit {
844 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
849 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
850 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
853 // Clone - Clone this operator, replacing arguments with the new list
854 virtual OpInit *clone(std::vector<Init *> &Operands) {
855 assert(Operands.size() == 2 &&
856 "Wrong number of operands for binary operation");
857 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
860 int getNumOperands(void) const { return 2; }
861 Init *getOperand(int i) {
862 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
871 BinaryOp getOpcode() const { return Opc; }
872 Init *getLHS() const { return LHS; }
873 Init *getRHS() const { return RHS; }
875 // Fold - If possible, fold this to a simpler init. Return this if not
877 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
879 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
881 virtual std::string getAsString() const;
884 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
886 class TernOpInit : public OpInit {
888 enum TernaryOp { SUBST, FOREACH, IF };
891 Init *LHS, *MHS, *RHS;
893 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
894 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
897 // Clone - Clone this operator, replacing arguments with the new list
898 virtual OpInit *clone(std::vector<Init *> &Operands) {
899 assert(Operands.size() == 3 &&
900 "Wrong number of operands for ternary operation");
901 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2],
905 int getNumOperands(void) const { return 3; }
906 Init *getOperand(int i) {
907 assert((i == 0 || i == 1 || i == 2) &&
908 "Invalid operand id for ternary operator");
920 TernaryOp getOpcode() const { return Opc; }
921 Init *getLHS() const { return LHS; }
922 Init *getMHS() const { return MHS; }
923 Init *getRHS() const { return RHS; }
925 // Fold - If possible, fold this to a simpler init. Return this if not
927 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
929 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
931 virtual std::string getAsString() const;
935 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
937 class VarInit : public TypedInit {
940 explicit VarInit(const std::string &VN, RecTy *T)
941 : TypedInit(T), VarName(VN) {}
943 virtual Init *convertInitializerTo(RecTy *Ty) {
944 return Ty->convertValue(this);
947 const std::string &getName() const { return VarName; }
949 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
951 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
954 virtual RecTy *getFieldType(const std::string &FieldName) const;
955 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
957 /// resolveReferences - This method is used by classes that refer to other
958 /// variables which may not be defined at the time they expression is formed.
959 /// If a value is set for the variable later, this method will be called on
960 /// users of the value to allow the value to propagate out.
962 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
964 virtual std::string getAsString() const { return VarName; }
968 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
970 class VarBitInit : public Init {
974 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
975 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
976 ((BitsRecTy*)T->getType())->getNumBits() > B &&
977 "Illegal VarBitInit expression!");
980 virtual Init *convertInitializerTo(RecTy *Ty) {
981 return Ty->convertValue(this);
984 TypedInit *getVariable() const { return TI; }
985 unsigned getBitNum() const { return Bit; }
987 virtual std::string getAsString() const;
988 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
991 /// VarListElementInit - List[4] - Represent access to one element of a var or
993 class VarListElementInit : public TypedInit {
997 VarListElementInit(TypedInit *T, unsigned E)
998 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1000 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1001 "Illegal VarBitInit expression!");
1004 virtual Init *convertInitializerTo(RecTy *Ty) {
1005 return Ty->convertValue(this);
1008 TypedInit *getVariable() const { return TI; }
1009 unsigned getElementNum() const { return Element; }
1011 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1014 /// resolveListElementReference - This method is used to implement
1015 /// VarListElementInit::resolveReferences. If the list element is resolvable
1016 /// now, we return the resolved value, otherwise we return null.
1017 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1020 virtual std::string getAsString() const;
1021 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1024 /// DefInit - AL - Represent a reference to a 'def' in the description
1026 class DefInit : public TypedInit {
1029 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
1031 virtual Init *convertInitializerTo(RecTy *Ty) {
1032 return Ty->convertValue(this);
1035 Record *getDef() const { return Def; }
1037 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1039 virtual RecTy *getFieldType(const std::string &FieldName) const;
1040 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
1042 virtual std::string getAsString() const;
1044 /// resolveBitReference - This method is used to implement
1045 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1046 /// simply return the resolved value, otherwise we return null.
1048 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1050 assert(0 && "Illegal bit reference off def");
1054 /// resolveListElementReference - This method is used to implement
1055 /// VarListElementInit::resolveReferences. If the list element is resolvable
1056 /// now, we return the resolved value, otherwise we return null.
1057 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1059 assert(0 && "Illegal element reference off def");
1065 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1067 class FieldInit : public TypedInit {
1068 Init *Rec; // Record we are referring to
1069 std::string FieldName; // Field we are accessing
1071 FieldInit(Init *R, const std::string &FN)
1072 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1073 assert(getType() && "FieldInit with non-record type!");
1076 virtual Init *convertInitializerTo(RecTy *Ty) {
1077 return Ty->convertValue(this);
1080 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1082 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1085 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1087 virtual std::string getAsString() const {
1088 return Rec->getAsString() + "." + FieldName;
1092 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1093 /// to have at least one value then a (possibly empty) list of arguments. Each
1094 /// argument can have a name associated with it.
1096 class DagInit : public TypedInit {
1098 std::string ValName;
1099 std::vector<Init*> Args;
1100 std::vector<std::string> ArgNames;
1102 DagInit(Init *V, std::string VN,
1103 const std::vector<std::pair<Init*, std::string> > &args)
1104 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
1105 Args.reserve(args.size());
1106 ArgNames.reserve(args.size());
1107 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1108 Args.push_back(args[i].first);
1109 ArgNames.push_back(args[i].second);
1112 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
1113 const std::vector<std::string> &argNames)
1114 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args), ArgNames(argNames) {
1117 virtual Init *convertInitializerTo(RecTy *Ty) {
1118 return Ty->convertValue(this);
1121 Init *getOperator() const { return Val; }
1123 const std::string &getName() const { return ValName; }
1125 unsigned getNumArgs() const { return Args.size(); }
1126 Init *getArg(unsigned Num) const {
1127 assert(Num < Args.size() && "Arg number out of range!");
1130 const std::string &getArgName(unsigned Num) const {
1131 assert(Num < ArgNames.size() && "Arg number out of range!");
1132 return ArgNames[Num];
1135 void setArg(unsigned Num, Init *I) {
1136 assert(Num < Args.size() && "Arg number out of range!");
1140 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
1142 virtual std::string getAsString() const;
1144 typedef std::vector<Init*>::iterator arg_iterator;
1145 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1146 typedef std::vector<std::string>::iterator name_iterator;
1147 typedef std::vector<std::string>::const_iterator const_name_iterator;
1149 inline arg_iterator arg_begin() { return Args.begin(); }
1150 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1151 inline arg_iterator arg_end () { return Args.end(); }
1152 inline const_arg_iterator arg_end () const { return Args.end(); }
1154 inline size_t arg_size () const { return Args.size(); }
1155 inline bool arg_empty() const { return Args.empty(); }
1157 inline name_iterator name_begin() { return ArgNames.begin(); }
1158 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1159 inline name_iterator name_end () { return ArgNames.end(); }
1160 inline const_name_iterator name_end () const { return ArgNames.end(); }
1162 inline size_t name_size () const { return ArgNames.size(); }
1163 inline bool name_empty() const { return ArgNames.empty(); }
1165 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1167 assert(0 && "Illegal bit reference off dag");
1171 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1173 assert(0 && "Illegal element reference off dag");
1179 //===----------------------------------------------------------------------===//
1180 // High-Level Classes
1181 //===----------------------------------------------------------------------===//
1189 RecordVal(const std::string &N, RecTy *T, unsigned P);
1191 const std::string &getName() const { return Name; }
1193 unsigned getPrefix() const { return Prefix; }
1194 RecTy *getType() const { return Ty; }
1195 Init *getValue() const { return Value; }
1197 bool setValue(Init *V) {
1199 Value = V->convertInitializerTo(Ty);
1207 void print(std::ostream &OS, bool PrintSem = true) const;
1210 inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
1211 RV.print(OS << " ");
1218 std::vector<std::string> TemplateArgs;
1219 std::vector<RecordVal> Values;
1220 std::vector<Record*> SuperClasses;
1223 explicit Record(const std::string &N, SMLoc loc) : Name(N), Loc(loc) {}
1226 const std::string &getName() const { return Name; }
1227 void setName(const std::string &Name); // Also updates RecordKeeper.
1229 SMLoc getLoc() const { return Loc; }
1231 const std::vector<std::string> &getTemplateArgs() const {
1232 return TemplateArgs;
1234 const std::vector<RecordVal> &getValues() const { return Values; }
1235 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1237 bool isTemplateArg(const std::string &Name) const {
1238 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1239 if (TemplateArgs[i] == Name) return true;
1243 const RecordVal *getValue(const std::string &Name) const {
1244 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1245 if (Values[i].getName() == Name) return &Values[i];
1248 RecordVal *getValue(const std::string &Name) {
1249 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1250 if (Values[i].getName() == Name) return &Values[i];
1254 void addTemplateArg(const std::string &Name) {
1255 assert(!isTemplateArg(Name) && "Template arg already defined!");
1256 TemplateArgs.push_back(Name);
1259 void addValue(const RecordVal &RV) {
1260 assert(getValue(RV.getName()) == 0 && "Value already added!");
1261 Values.push_back(RV);
1264 void removeValue(const std::string &Name) {
1265 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1266 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1267 if (Values[i].getName() == Name) {
1268 Values.erase(Values.begin()+i);
1271 assert(0 && "Name does not exist in record!");
1274 bool isSubClassOf(const Record *R) const {
1275 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1276 if (SuperClasses[i] == R)
1281 bool isSubClassOf(const std::string &Name) const {
1282 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1283 if (SuperClasses[i]->getName() == Name)
1288 void addSuperClass(Record *R) {
1289 assert(!isSubClassOf(R) && "Already subclassing record!");
1290 SuperClasses.push_back(R);
1293 /// resolveReferences - If there are any field references that refer to fields
1294 /// that have been filled in, we can propagate the values now.
1296 void resolveReferences() { resolveReferencesTo(0); }
1298 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1299 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1300 /// possible references.
1301 void resolveReferencesTo(const RecordVal *RV);
1305 //===--------------------------------------------------------------------===//
1306 // High-level methods useful to tablegen back-ends
1309 /// getValueInit - Return the initializer for a value with the specified name,
1310 /// or throw an exception if the field does not exist.
1312 Init *getValueInit(const std::string &FieldName) const;
1314 /// getValueAsString - This method looks up the specified field and returns
1315 /// its value as a string, throwing an exception if the field does not exist
1316 /// or if the value is not a string.
1318 std::string getValueAsString(const std::string &FieldName) const;
1320 /// getValueAsBitsInit - This method looks up the specified field and returns
1321 /// its value as a BitsInit, throwing an exception if the field does not exist
1322 /// or if the value is not the right type.
1324 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1326 /// getValueAsListInit - This method looks up the specified field and returns
1327 /// its value as a ListInit, throwing an exception if the field does not exist
1328 /// or if the value is not the right type.
1330 ListInit *getValueAsListInit(const std::string &FieldName) const;
1332 /// getValueAsListOfDefs - This method looks up the specified field and
1333 /// returns its value as a vector of records, throwing an exception if the
1334 /// field does not exist or if the value is not the right type.
1336 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
1338 /// getValueAsListOfInts - This method looks up the specified field and returns
1339 /// its value as a vector of integers, throwing an exception if the field does
1340 /// not exist or if the value is not the right type.
1342 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
1344 /// getValueAsDef - This method looks up the specified field and returns its
1345 /// value as a Record, throwing an exception if the field does not exist or if
1346 /// the value is not the right type.
1348 Record *getValueAsDef(const std::string &FieldName) const;
1350 /// getValueAsBit - This method looks up the specified field and returns its
1351 /// value as a bit, throwing an exception if the field does not exist or if
1352 /// the value is not the right type.
1354 bool getValueAsBit(const std::string &FieldName) const;
1356 /// getValueAsInt - This method looks up the specified field and returns its
1357 /// value as an int64_t, throwing an exception if the field does not exist or
1358 /// if the value is not the right type.
1360 int64_t getValueAsInt(const std::string &FieldName) const;
1362 /// getValueAsDag - This method looks up the specified field and returns its
1363 /// value as an Dag, throwing an exception if the field does not exist or if
1364 /// the value is not the right type.
1366 DagInit *getValueAsDag(const std::string &FieldName) const;
1368 /// getValueAsCode - This method looks up the specified field and returns
1369 /// its value as the string data in a CodeInit, throwing an exception if the
1370 /// field does not exist or if the value is not a code object.
1372 std::string getValueAsCode(const std::string &FieldName) const;
1375 std::ostream &operator<<(std::ostream &OS, const Record &R);
1378 Record Rec; // Placeholder for template args and Name.
1379 typedef std::vector<Record*> RecordVector;
1380 RecordVector DefPrototypes;
1384 MultiClass(const std::string &Name, SMLoc Loc) : Rec(Name, Loc) {}
1387 class RecordKeeper {
1388 std::map<std::string, Record*> Classes, Defs;
1391 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1392 E = Classes.end(); I != E; ++I)
1394 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1395 E = Defs.end(); I != E; ++I)
1399 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1400 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1402 Record *getClass(const std::string &Name) const {
1403 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1404 return I == Classes.end() ? 0 : I->second;
1406 Record *getDef(const std::string &Name) const {
1407 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1408 return I == Defs.end() ? 0 : I->second;
1410 void addClass(Record *R) {
1411 assert(getClass(R->getName()) == 0 && "Class already exists!");
1412 Classes.insert(std::make_pair(R->getName(), R));
1414 void addDef(Record *R) {
1415 assert(getDef(R->getName()) == 0 && "Def already exists!");
1416 Defs.insert(std::make_pair(R->getName(), R));
1419 /// removeClass - Remove, but do not delete, the specified record.
1421 void removeClass(const std::string &Name) {
1422 assert(Classes.count(Name) && "Class does not exist!");
1423 Classes.erase(Name);
1425 /// removeDef - Remove, but do not delete, the specified record.
1427 void removeDef(const std::string &Name) {
1428 assert(Defs.count(Name) && "Def does not exist!");
1432 //===--------------------------------------------------------------------===//
1433 // High-level helper methods, useful for tablegen backends...
1435 /// getAllDerivedDefinitions - This method returns all concrete definitions
1436 /// that derive from the specified class name. If a class with the specified
1437 /// name does not exist, an exception is thrown.
1438 std::vector<Record*>
1439 getAllDerivedDefinitions(const std::string &ClassName) const;
1445 /// LessRecord - Sorting predicate to sort record pointers by name.
1448 bool operator()(const Record *Rec1, const Record *Rec2) const {
1449 return Rec1->getName() < Rec2->getName();
1453 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1456 struct LessRecordFieldName {
1457 bool operator()(const Record *Rec1, const Record *Rec2) const {
1458 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1465 std::string Message;
1467 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1469 SMLoc getLoc() const { return Loc; }
1470 const std::string &getMessage() const { return Message; }
1474 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1476 extern RecordKeeper Records;
1478 void PrintError(SMLoc ErrorLoc, const std::string &Msg);
1481 } // End llvm namespace