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/ADT/FoldingSet.h"
19 #include "llvm/Support/Allocator.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/raw_ostream.h"
56 class VarListElementInit;
64 //===----------------------------------------------------------------------===//
66 //===----------------------------------------------------------------------===//
71 virtual std::string getAsString() const = 0;
72 void print(raw_ostream &OS) const { OS << getAsString(); }
75 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
76 /// converted to the specified type.
77 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
79 public: // These methods should only be called from subclasses of Init
80 virtual const Init *convertValue(const UnsetInit *UI) { return 0; }
81 virtual const Init *convertValue(const BitInit *BI) { return 0; }
82 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
83 virtual const Init *convertValue(const IntInit *II) { return 0; }
84 virtual const Init *convertValue(const StringInit *SI) { return 0; }
85 virtual const Init *convertValue(const ListInit *LI) { return 0; }
86 virtual const Init *convertValue(const UnOpInit *UI) {
87 return convertValue((const TypedInit*)UI);
89 virtual const Init *convertValue(const BinOpInit *UI) {
90 return convertValue((const TypedInit*)UI);
92 virtual const Init *convertValue(const TernOpInit *UI) {
93 return convertValue((const TypedInit*)UI);
95 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
96 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
97 virtual const Init *convertValue(const DefInit *DI) { return 0; }
98 virtual const Init *convertValue(const DagInit *DI) { return 0; }
99 virtual const Init *convertValue(const TypedInit *TI) { return 0; }
100 virtual const Init *convertValue(const VarInit *VI) {
101 return convertValue((const TypedInit*)VI);
103 virtual const Init *convertValue(const FieldInit *FI) {
104 return convertValue((const TypedInit*)FI);
107 public: // These methods should only be called by subclasses of RecTy.
108 // baseClassOf - These virtual methods should be overloaded to return true iff
109 // all values of type 'RHS' can be converted to the 'this' type.
110 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
114 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
115 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
116 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
117 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
120 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
126 /// BitRecTy - 'bit' - Represent a single bit
128 class BitRecTy : public RecTy {
130 virtual const Init *convertValue(const UnsetInit *UI) {
131 return (const Init*)UI;
133 virtual const Init *convertValue(const BitInit *BI) {
134 return (const Init*)BI;
136 virtual const Init *convertValue(const BitsInit *BI);
137 virtual const Init *convertValue(const IntInit *II);
138 virtual const Init *convertValue(const StringInit *SI) { return 0; }
139 virtual const Init *convertValue(const ListInit *LI) { return 0; }
140 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
141 virtual const Init *convertValue(const VarBitInit *VB) {
142 return (const Init*)VB;
144 virtual const Init *convertValue(const DefInit *DI) { return 0; }
145 virtual const Init *convertValue(const DagInit *DI) { return 0; }
146 virtual const Init *convertValue(const UnOpInit *UI) {
147 return RecTy::convertValue(UI);
149 virtual const Init *convertValue(const BinOpInit *UI) {
150 return RecTy::convertValue(UI);
152 virtual const Init *convertValue(const TernOpInit *UI) {
153 return RecTy::convertValue(UI);
155 virtual const Init *convertValue(const TypedInit *TI);
156 virtual const Init *convertValue(const VarInit *VI) {
157 return RecTy::convertValue(VI);
159 virtual const Init *convertValue(const FieldInit *FI) {
160 return RecTy::convertValue(FI);
163 std::string getAsString() const { return "bit"; }
165 bool typeIsConvertibleTo(const RecTy *RHS) const {
166 return RHS->baseClassOf(this);
168 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
169 virtual bool baseClassOf(const BitsRecTy *RHS) const;
170 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
171 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
172 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
173 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
174 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
175 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
180 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
181 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
183 class BitsRecTy : public RecTy {
186 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
188 unsigned getNumBits() const { return Size; }
190 virtual const Init *convertValue(const UnsetInit *UI);
191 virtual const Init *convertValue(const BitInit *UI);
192 virtual const Init *convertValue(const BitsInit *BI);
193 virtual const Init *convertValue(const IntInit *II);
194 virtual const Init *convertValue(const StringInit *SI) { return 0; }
195 virtual const Init *convertValue(const ListInit *LI) { return 0; }
196 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
197 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
198 virtual const Init *convertValue(const DefInit *DI) { return 0; }
199 virtual const Init *convertValue(const DagInit *DI) { return 0; }
200 virtual const Init *convertValue(const UnOpInit *UI) {
201 return RecTy::convertValue(UI);
203 virtual const Init *convertValue(const BinOpInit *UI) {
204 return RecTy::convertValue(UI);
206 virtual const Init *convertValue(const TernOpInit *UI) {
207 return RecTy::convertValue(UI);
209 virtual const Init *convertValue(const TypedInit *TI);
210 virtual const Init *convertValue(const VarInit *VI) {
211 return RecTy::convertValue(VI);
213 virtual const Init *convertValue(const FieldInit *FI) {
214 return RecTy::convertValue(FI);
217 std::string getAsString() const;
219 bool typeIsConvertibleTo(const RecTy *RHS) const {
220 return RHS->baseClassOf(this);
222 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
223 virtual bool baseClassOf(const BitsRecTy *RHS) const {
224 return RHS->Size == Size;
226 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
227 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
228 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
229 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
230 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
231 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
236 /// IntRecTy - 'int' - Represent an integer value of no particular size
238 class IntRecTy : public RecTy {
240 virtual const Init *convertValue(const UnsetInit *UI) {
241 return (const Init*)UI;
243 virtual const Init *convertValue(const BitInit *BI);
244 virtual const Init *convertValue(const BitsInit *BI);
245 virtual const Init *convertValue(const IntInit *II) {
246 return (const Init*)II;
248 virtual const Init *convertValue(const StringInit *SI) { return 0; }
249 virtual const Init *convertValue(const ListInit *LI) { return 0; }
250 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
251 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
252 virtual const Init *convertValue(const DefInit *DI) { return 0; }
253 virtual const Init *convertValue(const DagInit *DI) { return 0; }
254 virtual const Init *convertValue(const UnOpInit *UI) {
255 return RecTy::convertValue(UI);
257 virtual const Init *convertValue(const BinOpInit *UI) {
258 return RecTy::convertValue(UI);
260 virtual const Init *convertValue(const TernOpInit *UI) {
261 return RecTy::convertValue(UI);
263 virtual const Init *convertValue(const TypedInit *TI);
264 virtual const Init *convertValue(const VarInit *VI) {
265 return RecTy::convertValue(VI);
267 virtual const Init *convertValue(const FieldInit *FI) {
268 return RecTy::convertValue(FI);
271 std::string getAsString() const { return "int"; }
273 bool typeIsConvertibleTo(const RecTy *RHS) const {
274 return RHS->baseClassOf(this);
277 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
278 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
279 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
280 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
281 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
282 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
283 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
284 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
288 /// StringRecTy - 'string' - Represent an string value
290 class StringRecTy : public RecTy {
292 virtual const Init *convertValue(const UnsetInit *UI) {
293 return (const Init*)UI;
295 virtual const Init *convertValue(const BitInit *BI) { return 0; }
296 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
297 virtual const Init *convertValue(const IntInit *II) { return 0; }
298 virtual const Init *convertValue(const StringInit *SI) {
299 return (const Init*)SI;
301 virtual const Init *convertValue(const ListInit *LI) { return 0; }
302 virtual const Init *convertValue(const UnOpInit *BO);
303 virtual const Init *convertValue(const BinOpInit *BO);
304 virtual const Init *convertValue(const TernOpInit *BO) {
305 return RecTy::convertValue(BO);
308 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
309 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
310 virtual const Init *convertValue(const DefInit *DI) { return 0; }
311 virtual const Init *convertValue(const DagInit *DI) { return 0; }
312 virtual const Init *convertValue(const TypedInit *TI);
313 virtual const Init *convertValue(const VarInit *VI) {
314 return RecTy::convertValue(VI);
316 virtual const Init *convertValue(const FieldInit *FI) {
317 return RecTy::convertValue(FI);
320 std::string getAsString() const { return "string"; }
322 bool typeIsConvertibleTo(const RecTy *RHS) const {
323 return RHS->baseClassOf(this);
326 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
328 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
329 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
330 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
331 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
332 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
333 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
336 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
337 // the specified type.
338 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
339 /// be of the specified type.
341 class ListRecTy : public RecTy {
344 explicit ListRecTy(RecTy *T) : Ty(T) {}
346 RecTy *getElementType() const { return Ty; }
348 virtual const Init *convertValue(const UnsetInit *UI) {
349 return (const Init*)UI;
351 virtual const Init *convertValue(const BitInit *BI) { return 0; }
352 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
353 virtual const Init *convertValue(const IntInit *II) { return 0; }
354 virtual const Init *convertValue(const StringInit *SI) { return 0; }
355 virtual const Init *convertValue(const ListInit *LI);
356 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
357 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
358 virtual const Init *convertValue(const DefInit *DI) { return 0; }
359 virtual const Init *convertValue(const DagInit *DI) { return 0; }
360 virtual const Init *convertValue(const UnOpInit *UI) {
361 return RecTy::convertValue(UI);
363 virtual const Init *convertValue(const BinOpInit *UI) {
364 return RecTy::convertValue(UI);
366 virtual const Init *convertValue(const TernOpInit *UI) {
367 return RecTy::convertValue(UI);
369 virtual const Init *convertValue(const TypedInit *TI);
370 virtual const Init *convertValue(const VarInit *VI) {
371 return RecTy::convertValue(VI);
373 virtual const Init *convertValue(const FieldInit *FI) {
374 return RecTy::convertValue(FI);
377 std::string getAsString() const;
379 bool typeIsConvertibleTo(const RecTy *RHS) const {
380 return RHS->baseClassOf(this);
383 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
384 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
385 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
386 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
387 virtual bool baseClassOf(const ListRecTy *RHS) const {
388 return RHS->getElementType()->typeIsConvertibleTo(Ty);
390 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
391 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
392 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
395 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
397 class CodeRecTy : public RecTy {
399 virtual const Init *convertValue(const UnsetInit *UI) {
400 return (const Init*)UI;
402 virtual const Init *convertValue(const BitInit *BI) { return 0; }
403 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
404 virtual const Init *convertValue(const IntInit *II) { return 0; }
405 virtual const Init *convertValue(const StringInit *SI) { return 0; }
406 virtual const Init *convertValue(const ListInit *LI) { return 0; }
407 virtual const Init *convertValue(const CodeInit *CI) {
408 return (const Init*)CI;
410 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
411 virtual const Init *convertValue(const DefInit *DI) { return 0; }
412 virtual const Init *convertValue(const DagInit *DI) { return 0; }
413 virtual const Init *convertValue(const UnOpInit *UI) {
414 return RecTy::convertValue(UI);
416 virtual const Init *convertValue(const BinOpInit *UI) {
417 return RecTy::convertValue(UI);
419 virtual const Init *convertValue(const TernOpInit *UI) {
420 return RecTy::convertValue(UI);
422 virtual const Init *convertValue(const TypedInit *TI);
423 virtual const Init *convertValue(const VarInit *VI) {
424 return RecTy::convertValue(VI);
426 virtual const Init *convertValue(const FieldInit *FI) {
427 return RecTy::convertValue(FI);
430 std::string getAsString() const { return "code"; }
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 true; }
441 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
445 /// DagRecTy - 'dag' - Represent a dag fragment
447 class DagRecTy : public RecTy {
449 virtual const Init *convertValue(const UnsetInit *UI) {
450 return (const Init*)UI;
452 virtual const Init *convertValue(const BitInit *BI) { return 0; }
453 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
454 virtual const Init *convertValue(const IntInit *II) { return 0; }
455 virtual const Init *convertValue(const StringInit *SI) { return 0; }
456 virtual const Init *convertValue(const ListInit *LI) { return 0; }
457 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
458 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
459 virtual const Init *convertValue(const DefInit *DI) { return 0; }
460 virtual const Init *convertValue(const UnOpInit *BO);
461 virtual const Init *convertValue(const BinOpInit *BO);
462 virtual const Init *convertValue(const TernOpInit *BO) {
463 return RecTy::convertValue(BO);
465 virtual const Init *convertValue(const DagInit *CI) {
466 return (const Init*)CI;
468 virtual const Init *convertValue(const TypedInit *TI);
469 virtual const Init *convertValue(const VarInit *VI) {
470 return RecTy::convertValue(VI);
472 virtual const Init *convertValue(const FieldInit *FI) {
473 return RecTy::convertValue(FI);
476 std::string getAsString() const { return "dag"; }
478 bool typeIsConvertibleTo(const RecTy *RHS) const {
479 return RHS->baseClassOf(this);
482 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
483 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
484 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
485 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
486 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
487 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
488 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
489 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
493 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
496 class RecordRecTy : public RecTy {
499 explicit RecordRecTy(Record *R) : Rec(R) {}
501 Record *getRecord() const { return Rec; }
503 virtual const Init *convertValue(const UnsetInit *UI) {
504 return (const Init*)UI;
506 virtual const Init *convertValue(const BitInit *BI) { return 0; }
507 virtual const Init *convertValue(const BitsInit *BI) { return 0; }
508 virtual const Init *convertValue(const IntInit *II) { return 0; }
509 virtual const Init *convertValue(const StringInit *SI) { return 0; }
510 virtual const Init *convertValue(const ListInit *LI) { return 0; }
511 virtual const Init *convertValue(const CodeInit *CI) { return 0; }
512 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
513 virtual const Init *convertValue(const UnOpInit *UI) {
514 return RecTy::convertValue(UI);
516 virtual const Init *convertValue(const BinOpInit *UI) {
517 return RecTy::convertValue(UI);
519 virtual const Init *convertValue(const TernOpInit *UI) {
520 return RecTy::convertValue(UI);
522 virtual const Init *convertValue(const DefInit *DI);
523 virtual const Init *convertValue(const DagInit *DI) { return 0; }
524 virtual const Init *convertValue(const TypedInit *VI);
525 virtual const Init *convertValue(const VarInit *VI) {
526 return RecTy::convertValue(VI);
528 virtual const Init *convertValue(const FieldInit *FI) {
529 return RecTy::convertValue(FI);
532 std::string getAsString() const;
534 bool typeIsConvertibleTo(const RecTy *RHS) const {
535 return RHS->baseClassOf(this);
537 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
538 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
539 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
540 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
541 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
542 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
543 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
544 virtual bool baseClassOf(const RecordRecTy *RHS) const;
547 /// resolveTypes - Find a common type that T1 and T2 convert to.
548 /// Return 0 if no such type exists.
550 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
552 //===----------------------------------------------------------------------===//
553 // Initializer Classes
554 //===----------------------------------------------------------------------===//
556 class Init : public FastFoldingSetNode {
557 Init(const Init &); // Do not define.
558 Init &operator=(const Init &); // Do not define.
561 Init(const FoldingSetNodeID &ID) : FastFoldingSetNode(ID) {}
563 static FoldingSet<Init> UniqueInits;
564 static BumpPtrAllocator InitAllocator;
589 static void ReleaseMemory() {
590 InitAllocator.Reset();
593 /// isComplete - This virtual method should be overridden by values that may
594 /// not be completely specified yet.
595 virtual bool isComplete() const { return true; }
597 /// print - Print out this value.
598 void print(raw_ostream &OS) const { OS << getAsString(); }
600 /// getAsString - Convert this value to a string form.
601 virtual std::string getAsString() const = 0;
603 /// dump - Debugging method that may be called through a debugger, just
604 /// invokes print on stderr.
607 /// convertInitializerTo - This virtual function is a simple call-back
608 /// function that should be overridden to call the appropriate
609 /// RecTy::convertValue method.
611 virtual const Init *convertInitializerTo(RecTy *Ty) const = 0;
613 /// convertInitializerBitRange - This method is used to implement the bitrange
614 /// selection operator. Given an initializer, it selects the specified bits
615 /// out, returning them as a new init of bits type. If it is not legal to use
616 /// the bit subscript operator on this initializer, return null.
619 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
623 /// convertInitListSlice - This method is used to implement the list slice
624 /// selection operator. Given an initializer, it selects the specified list
625 /// elements, returning them as a new init of list type. If it is not legal
626 /// to take a slice of this, return null.
629 convertInitListSlice(const std::vector<unsigned> &Elements) const {
633 /// getFieldType - This method is used to implement the FieldInit class.
634 /// Implementors of this method should return the type of the named field if
635 /// they are of record type.
637 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
639 /// getFieldInit - This method complements getFieldType to return the
640 /// initializer for the specified field. If getFieldType returns non-null
641 /// this method should return non-null, otherwise it returns null.
643 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
644 const std::string &FieldName) const {
648 /// resolveReferences - This method is used by classes that refer to other
649 /// variables which may not be defined at the time the expression is formed.
650 /// If a value is set for the variable later, this method will be called on
651 /// users of the value to allow the value to propagate out.
653 virtual const Init *resolveReferences(Record &R,
654 const RecordVal *RV) const {
659 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
660 I.print(OS); return OS;
663 /// TypedInit - This is the common super-class of types that have a specific,
666 class TypedInit : public Init {
669 TypedInit(const TypedInit &Other); // Do not define.
670 TypedInit &operator=(const TypedInit &Other); // Do not define.
673 explicit TypedInit(const FoldingSetNodeID &ID, RecTy *T) : Init(ID), Ty(T) {}
676 RecTy *getType() const { return Ty; }
679 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
681 convertInitListSlice(const std::vector<unsigned> &Elements) const;
683 /// getFieldType - This method is used to implement the FieldInit class.
684 /// Implementors of this method should return the type of the named field if
685 /// they are of record type.
687 virtual RecTy *getFieldType(const std::string &FieldName) const;
689 /// resolveBitReference - This method is used to implement
690 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
691 /// simply return the resolved value, otherwise we return null.
693 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
694 unsigned Bit) const = 0;
696 /// resolveListElementReference - This method is used to implement
697 /// VarListElementInit::resolveReferences. If the list element is resolvable
698 /// now, we return the resolved value, otherwise we return null.
699 virtual const Init *resolveListElementReference(Record &R,
701 unsigned Elt) const = 0;
705 /// UnsetInit - ? - Represents an uninitialized value
707 class UnsetInit : public Init {
708 UnsetInit(const FoldingSetNodeID &ID) : Init(ID) {}
709 UnsetInit(const UnsetInit &); // Do not define.
710 UnsetInit &operator=(const UnsetInit &Other); // Do not define.
713 static const UnsetInit *Create();
715 virtual const Init *convertInitializerTo(RecTy *Ty) const {
716 return Ty->convertValue(this);
719 virtual bool isComplete() const { return false; }
720 virtual std::string getAsString() const { return "?"; }
724 /// BitInit - true/false - Represent a concrete initializer for a bit.
726 class BitInit : public Init {
729 explicit BitInit(const FoldingSetNodeID &ID, bool V) : Init(ID), Value(V) {}
730 BitInit(const BitInit &Other); // Do not define.
731 BitInit &operator=(BitInit &Other); // Do not define.
734 static const BitInit *Create(bool V);
736 bool getValue() const { return Value; }
738 virtual const Init *convertInitializerTo(RecTy *Ty) const {
739 return Ty->convertValue(this);
742 virtual std::string getAsString() const { return Value ? "1" : "0"; }
745 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
746 /// It contains a vector of bits, whose size is determined by the type.
748 class BitsInit : public Init {
749 std::vector<const Init*> Bits;
751 BitsInit(const FoldingSetNodeID &ID, unsigned Size)
752 : Init(ID), Bits(Size) {}
754 template<typename InputIterator>
755 BitsInit(const FoldingSetNodeID &ID, InputIterator start, InputIterator end)
756 : Init(ID), Bits(start, end) {}
758 BitsInit(const BitsInit &Other); // Do not define.
759 BitsInit &operator=(const BitsInit &Other); // Do not define.
762 template<typename InputIterator>
763 static const BitsInit *Create(InputIterator Start, InputIterator End) {
765 ID.AddInteger(initBits);
766 ID.AddInteger(std::distance(Start, End));
768 InputIterator S = Start;
773 if (const Init *I = UniqueInits.FindNodeOrInsertPos(ID, IP))
774 return static_cast<const BitsInit *>(I);
776 BitsInit *I = InitAllocator.Allocate<BitsInit>();
777 new (I) BitsInit(ID, Start, End);
778 UniqueInits.InsertNode(I, IP);
782 unsigned getNumBits() const { return Bits.size(); }
784 const Init *getBit(unsigned Bit) const {
785 assert(Bit < Bits.size() && "Bit index out of range!");
788 void setBit(unsigned Bit, const Init *V) {
789 assert(Bit < Bits.size() && "Bit index out of range!");
790 assert(Bits[Bit] == 0 && "Bit already set!");
794 virtual const Init *convertInitializerTo(RecTy *Ty) const {
795 return Ty->convertValue(this);
798 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
800 virtual bool isComplete() const {
801 for (unsigned i = 0; i != getNumBits(); ++i)
802 if (!getBit(i)->isComplete()) return false;
805 bool allInComplete() const {
806 for (unsigned i = 0; i != getNumBits(); ++i)
807 if (getBit(i)->isComplete()) return false;
810 virtual std::string getAsString() const;
812 virtual const Init *resolveReferences(Record &R,
813 const RecordVal *RV) const;
817 /// IntInit - 7 - Represent an initalization by a literal integer value.
819 class IntInit : public TypedInit {
822 explicit IntInit(const FoldingSetNodeID &ID, int64_t V)
823 : TypedInit(ID, new IntRecTy), Value(V) {}
825 IntInit(const IntInit &Other); // Do not define.
826 IntInit &operator=(const IntInit &Other); // Do note define.
829 static const IntInit *Create(int64_t V);
831 int64_t getValue() const { return Value; }
833 virtual const Init *convertInitializerTo(RecTy *Ty) const {
834 return Ty->convertValue(this);
837 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
839 virtual std::string getAsString() const;
841 /// resolveBitReference - This method is used to implement
842 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
843 /// simply return the resolved value, otherwise we return null.
845 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
846 unsigned Bit) const {
847 assert(0 && "Illegal bit reference off int");
851 /// resolveListElementReference - This method is used to implement
852 /// VarListElementInit::resolveReferences. If the list element is resolvable
853 /// now, we return the resolved value, otherwise we return null.
854 virtual const Init *resolveListElementReference(Record &R,
856 unsigned Elt) const {
857 assert(0 && "Illegal element reference off int");
863 /// StringInit - "foo" - Represent an initialization by a string value.
865 class StringInit : public TypedInit {
868 explicit StringInit(const FoldingSetNodeID &ID, const std::string &V)
869 : TypedInit(ID, new StringRecTy), Value(V) {}
871 StringInit(const StringInit &Other); // Do not define.
872 StringInit &operator=(const StringInit &Other); // Do not define.
875 static const StringInit *Create(const std::string &V);
877 const std::string &getValue() const { return Value; }
879 virtual const Init *convertInitializerTo(RecTy *Ty) const {
880 return Ty->convertValue(this);
883 virtual std::string getAsString() const { return "\"" + Value + "\""; }
885 /// resolveBitReference - This method is used to implement
886 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
887 /// simply return the resolved value, otherwise we return null.
889 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
890 unsigned Bit) const {
891 assert(0 && "Illegal bit reference off string");
895 /// resolveListElementReference - This method is used to implement
896 /// VarListElementInit::resolveReferences. If the list element is resolvable
897 /// now, we return the resolved value, otherwise we return null.
898 virtual const Init *resolveListElementReference(Record &R,
900 unsigned Elt) const {
901 assert(0 && "Illegal element reference off string");
906 /// CodeInit - "[{...}]" - Represent a code fragment.
908 class CodeInit : public Init {
911 explicit CodeInit(const FoldingSetNodeID &ID, const std::string &V)
912 : Init(ID), Value(V) {}
914 CodeInit(const CodeInit &Other); // Do not define.
915 CodeInit &operator=(const CodeInit &Other); // Do not define.
918 static const CodeInit *Create(const std::string &V);
920 const std::string &getValue() const { return Value; }
922 virtual const Init *convertInitializerTo(RecTy *Ty) const {
923 return Ty->convertValue(this);
926 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
929 /// ListInit - [AL, AH, CL] - Represent a list of defs
931 class ListInit : public TypedInit {
932 std::vector<const Init*> Values;
935 typedef std::vector<const Init*>::const_iterator const_iterator;
938 ListInit(const FoldingSetNodeID &ID, std::vector<const Init*> &Vs,
940 : TypedInit(ID, new ListRecTy(EltTy)) {
944 template<typename InputIterator>
945 ListInit(const FoldingSetNodeID &ID, InputIterator Start, InputIterator End,
947 : TypedInit(ID, new ListRecTy(EltTy)), Values(Start, End) {}
949 ListInit(const ListInit &Other); // Do not define.
950 ListInit &operator=(const ListInit &Other); // Do not define.
953 static const ListInit *Create(std::vector<const Init*> &Vs, RecTy *EltTy);
955 template<typename InputIterator>
956 static const ListInit *Create(InputIterator Start, InputIterator End,
959 ID.AddInteger(initList);
960 ID.AddString(EltTy->getAsString());
962 InputIterator S = Start;
967 if (const Init *I = UniqueInits.FindNodeOrInsertPos(ID, IP))
968 return static_cast<const ListInit *>(I);
970 ListInit *I = InitAllocator.Allocate<ListInit>();
971 new (I) ListInit(ID, Start, End, EltTy);
972 UniqueInits.InsertNode(I, IP);
976 unsigned getSize() const { return Values.size(); }
977 const Init *getElement(unsigned i) const {
978 assert(i < Values.size() && "List element index out of range!");
982 Record *getElementAsRecord(unsigned i) const;
984 const Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
986 virtual const Init *convertInitializerTo(RecTy *Ty) const {
987 return Ty->convertValue(this);
990 /// resolveReferences - This method is used by classes that refer to other
991 /// variables which may not be defined at the time they expression is formed.
992 /// If a value is set for the variable later, this method will be called on
993 /// users of the value to allow the value to propagate out.
995 virtual const Init *resolveReferences(Record &R,
996 const RecordVal *RV) const;
998 virtual std::string getAsString() const;
1000 inline const_iterator begin() const { return Values.begin(); }
1001 inline const_iterator end () const { return Values.end(); }
1003 inline size_t size () const { return Values.size(); }
1004 inline bool empty() const { return Values.empty(); }
1006 /// resolveBitReference - This method is used to implement
1007 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1008 /// simply return the resolved value, otherwise we return null.
1010 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1011 unsigned Bit) const {
1012 assert(0 && "Illegal bit reference off list");
1016 /// resolveListElementReference - This method is used to implement
1017 /// VarListElementInit::resolveReferences. If the list element is resolvable
1018 /// now, we return the resolved value, otherwise we return null.
1019 virtual const Init *resolveListElementReference(Record &R,
1020 const RecordVal *RV,
1021 unsigned Elt) const;
1025 /// OpInit - Base class for operators
1027 class OpInit : public TypedInit {
1028 OpInit(const OpInit &Other); // Do not define.
1029 OpInit &operator=(OpInit &Other); // Do not define.
1032 explicit OpInit(const FoldingSetNodeID &ID, RecTy *Type)
1033 : TypedInit(ID, Type) {}
1036 // Clone - Clone this operator, replacing arguments with the new list
1037 virtual const OpInit *clone(std::vector<const Init *> &Operands) const = 0;
1039 virtual int getNumOperands() const = 0;
1040 virtual const Init *getOperand(int i) const = 0;
1042 // Fold - If possible, fold this to a simpler init. Return this if not
1043 // possible to fold.
1044 virtual const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
1046 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1047 return Ty->convertValue(this);
1050 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1051 unsigned Bit) const;
1052 virtual const Init *resolveListElementReference(Record &R,
1053 const RecordVal *RV,
1054 unsigned Elt) const;
1058 /// UnOpInit - !op (X) - Transform an init.
1060 class UnOpInit : public OpInit {
1062 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
1067 UnOpInit(const FoldingSetNodeID &ID, UnaryOp opc, const Init *lhs,
1069 : OpInit(ID, Type), Opc(opc), LHS(lhs) {}
1071 UnOpInit(const UnOpInit &Other); // Do not define.
1072 UnOpInit &operator=(const UnOpInit &Other); // Do not define.
1075 static const UnOpInit *Create(UnaryOp opc, const Init *lhs, RecTy *Type);
1077 // Clone - Clone this operator, replacing arguments with the new list
1078 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1079 assert(Operands.size() == 1 &&
1080 "Wrong number of operands for unary operation");
1081 return UnOpInit::Create(getOpcode(), *Operands.begin(), getType());
1084 int getNumOperands() const { return 1; }
1085 const Init *getOperand(int i) const {
1086 assert(i == 0 && "Invalid operand id for unary operator");
1087 return getOperand();
1090 UnaryOp getOpcode() const { return Opc; }
1091 const Init *getOperand() const { return LHS; }
1093 // Fold - If possible, fold this to a simpler init. Return this if not
1094 // possible to fold.
1095 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass)const ;
1097 virtual const Init *resolveReferences(Record &R,
1098 const RecordVal *RV) const;
1100 virtual std::string getAsString() const;
1103 /// BinOpInit - !op (X, Y) - Combine two inits.
1105 class BinOpInit : public OpInit {
1107 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
1110 const Init *LHS, *RHS;
1112 BinOpInit(const FoldingSetNodeID &ID, BinaryOp opc, const Init *lhs,
1113 const Init *rhs, RecTy *Type) :
1114 OpInit(ID, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
1116 BinOpInit(const BinOpInit &Other); // Do not define.
1117 BinOpInit &operator=(const BinOpInit &Other); // Do not define.
1120 static const BinOpInit *Create(BinaryOp opc, const Init *lhs, const Init *rhs,
1123 // Clone - Clone this operator, replacing arguments with the new list
1124 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1125 assert(Operands.size() == 2 &&
1126 "Wrong number of operands for binary operation");
1127 return BinOpInit::Create(getOpcode(), Operands[0], Operands[1], getType());
1130 int getNumOperands() const { return 2; }
1131 const Init *getOperand(int i) const {
1132 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
1140 BinaryOp getOpcode() const { return Opc; }
1141 const Init *getLHS() const { return LHS; }
1142 const Init *getRHS() const { return RHS; }
1144 // Fold - If possible, fold this to a simpler init. Return this if not
1145 // possible to fold.
1146 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1148 virtual const Init *resolveReferences(Record &R,
1149 const RecordVal *RV) const;
1151 virtual std::string getAsString() const;
1154 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
1156 class TernOpInit : public OpInit {
1158 enum TernaryOp { SUBST, FOREACH, IF };
1161 const Init *LHS, *MHS, *RHS;
1163 TernOpInit(const FoldingSetNodeID &ID, TernaryOp opc, const Init *lhs,
1164 const Init *mhs, const Init *rhs, RecTy *Type) :
1165 OpInit(ID, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1167 TernOpInit(const TernOpInit &Other); // Do not define.
1168 TernOpInit &operator=(const TernOpInit &Other); // Do not define.
1171 static const TernOpInit *Create(TernaryOp opc, const Init *lhs,
1172 const Init *mhs, const Init *rhs,
1175 // Clone - Clone this operator, replacing arguments with the new list
1176 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1177 assert(Operands.size() == 3 &&
1178 "Wrong number of operands for ternary operation");
1179 return TernOpInit::Create(getOpcode(), Operands[0], Operands[1],
1180 Operands[2], getType());
1183 int getNumOperands() const { return 3; }
1184 const Init *getOperand(int i) const {
1185 assert((i == 0 || i == 1 || i == 2) &&
1186 "Invalid operand id for ternary operator");
1189 } else if (i == 1) {
1196 TernaryOp getOpcode() const { return Opc; }
1197 const Init *getLHS() const { return LHS; }
1198 const Init *getMHS() const { return MHS; }
1199 const Init *getRHS() const { return RHS; }
1201 // Fold - If possible, fold this to a simpler init. Return this if not
1202 // possible to fold.
1203 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1205 virtual bool isComplete() const { return false; }
1207 virtual const Init *resolveReferences(Record &R,
1208 const RecordVal *RV) const;
1210 virtual std::string getAsString() const;
1214 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1216 class VarInit : public TypedInit {
1217 std::string VarName;
1219 explicit VarInit(const FoldingSetNodeID &ID, const std::string &VN, RecTy *T)
1220 : TypedInit(ID, T), VarName(VN) {}
1222 VarInit(const VarInit &Other); // Do not define.
1223 VarInit &operator=(const VarInit &Other); // Do not define.
1226 static const VarInit *Create(const std::string &VN, RecTy *T);
1227 static const VarInit *Create(const Init *VN, RecTy *T);
1229 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1230 return Ty->convertValue(this);
1233 const std::string &getName() const { return VarName; }
1235 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1236 unsigned Bit) const;
1237 virtual const Init *resolveListElementReference(Record &R,
1238 const RecordVal *RV,
1239 unsigned Elt) const;
1241 virtual RecTy *getFieldType(const std::string &FieldName) const;
1242 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1243 const std::string &FieldName) const;
1245 /// resolveReferences - This method is used by classes that refer to other
1246 /// variables which may not be defined at the time they expression is formed.
1247 /// If a value is set for the variable later, this method will be called on
1248 /// users of the value to allow the value to propagate out.
1250 virtual const Init *resolveReferences(Record &R,
1251 const RecordVal *RV) const;
1253 virtual std::string getAsString() const { return VarName; }
1257 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1259 class VarBitInit : public Init {
1260 const TypedInit *TI;
1263 VarBitInit(const FoldingSetNodeID &ID, const TypedInit *T, unsigned B)
1264 : Init(ID), TI(T), Bit(B) {
1265 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1266 ((BitsRecTy*)T->getType())->getNumBits() > B &&
1267 "Illegal VarBitInit expression!");
1270 VarBitInit(const VarBitInit &Other); // Do not define.
1271 VarBitInit &operator=(const VarBitInit &Other); // Do not define.
1274 static const VarBitInit *Create(const TypedInit *T, unsigned B);
1276 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1277 return Ty->convertValue(this);
1280 const TypedInit *getVariable() const { return TI; }
1281 unsigned getBitNum() const { return Bit; }
1283 virtual std::string getAsString() const;
1284 virtual const Init *resolveReferences(Record &R,
1285 const RecordVal *RV) const;
1288 /// VarListElementInit - List[4] - Represent access to one element of a var or
1290 class VarListElementInit : public TypedInit {
1291 const TypedInit *TI;
1294 VarListElementInit(const FoldingSetNodeID &ID, const TypedInit *T, unsigned E)
1295 : TypedInit(ID, dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1297 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1298 "Illegal VarBitInit expression!");
1301 VarListElementInit(const VarListElementInit &Other); // Do not define.
1302 VarListElementInit &operator=(const VarListElementInit &Other); // Do
1307 static const VarListElementInit *Create(const TypedInit *T, unsigned E);
1309 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1310 return Ty->convertValue(this);
1313 const TypedInit *getVariable() const { return TI; }
1314 unsigned getElementNum() const { return Element; }
1316 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1317 unsigned Bit) const;
1319 /// resolveListElementReference - This method is used to implement
1320 /// VarListElementInit::resolveReferences. If the list element is resolvable
1321 /// now, we return the resolved value, otherwise we return null.
1322 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1323 unsigned Elt) const;
1325 virtual std::string getAsString() const;
1326 virtual const Init *resolveReferences(Record &R,
1327 const RecordVal *RV) const;
1330 /// DefInit - AL - Represent a reference to a 'def' in the description
1332 class DefInit : public TypedInit {
1335 explicit DefInit(const FoldingSetNodeID &ID, Record *D)
1336 : TypedInit(ID, new RecordRecTy(D)), Def(D) {}
1338 DefInit(const DefInit &Other); // Do not define.
1339 DefInit &operator=(const DefInit &Other); // Do not define.
1342 static const DefInit *Create(Record *D);
1344 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1345 return Ty->convertValue(this);
1348 Record *getDef() const { return Def; }
1350 //virtual const Init *
1351 //convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
1353 virtual RecTy *getFieldType(const std::string &FieldName) const;
1354 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1355 const std::string &FieldName) const;
1357 virtual std::string getAsString() const;
1359 /// resolveBitReference - This method is used to implement
1360 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1361 /// simply return the resolved value, otherwise we return null.
1363 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1364 unsigned Bit) const {
1365 assert(0 && "Illegal bit reference off def");
1369 /// resolveListElementReference - This method is used to implement
1370 /// VarListElementInit::resolveReferences. If the list element is resolvable
1371 /// now, we return the resolved value, otherwise we return null.
1372 virtual const Init *resolveListElementReference(Record &R,
1373 const RecordVal *RV,
1374 unsigned Elt) const {
1375 assert(0 && "Illegal element reference off def");
1381 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1383 class FieldInit : public TypedInit {
1384 const Init *Rec; // Record we are referring to
1385 std::string FieldName; // Field we are accessing
1387 FieldInit(const FoldingSetNodeID &ID, const Init *R, const std::string &FN)
1388 : TypedInit(ID, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1389 assert(getType() && "FieldInit with non-record type!");
1392 FieldInit(const FieldInit &Other); // Do not define.
1393 FieldInit &operator=(const FieldInit &Other); // Do not define.
1396 static const FieldInit *Create(const Init *R, const std::string &FN);
1397 static const FieldInit *Create(const Init *R, const Init *FN);
1399 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1400 return Ty->convertValue(this);
1403 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1404 unsigned Bit) const;
1405 virtual const Init *resolveListElementReference(Record &R,
1406 const RecordVal *RV,
1407 unsigned Elt) const;
1409 virtual const Init *resolveReferences(Record &R,
1410 const RecordVal *RV) const;
1412 virtual std::string getAsString() const {
1413 return Rec->getAsString() + "." + FieldName;
1417 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1418 /// to have at least one value then a (possibly empty) list of arguments. Each
1419 /// argument can have a name associated with it.
1421 class DagInit : public TypedInit {
1423 std::string ValName;
1424 std::vector<const Init*> Args;
1425 std::vector<std::string> ArgNames;
1427 DagInit(const FoldingSetNodeID &ID, const Init *V, const std::string &VN,
1428 const std::vector<std::pair<const Init*, std::string> > &args)
1429 : TypedInit(ID, new DagRecTy), Val(V), ValName(VN) {
1430 Args.reserve(args.size());
1431 ArgNames.reserve(args.size());
1432 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1433 Args.push_back(args[i].first);
1434 ArgNames.push_back(args[i].second);
1437 DagInit(const FoldingSetNodeID &ID, const Init *V, const std::string &VN,
1438 const std::vector<const Init*> &args,
1439 const std::vector<std::string> &argNames)
1440 : TypedInit(ID, new DagRecTy), Val(V), ValName(VN), Args(args),
1441 ArgNames(argNames) { }
1443 DagInit(const DagInit &Other); // Do not define.
1444 DagInit &operator=(const DagInit &Other); // Do not define.
1447 static const DagInit *Create(const Init *V, const std::string &VN,
1449 std::pair<const Init*, std::string> > &args);
1450 static const DagInit *Create(const Init *V, const std::string &VN,
1451 const std::vector<const Init*> &args,
1452 const std::vector<std::string> &argNames);
1454 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1455 return Ty->convertValue(this);
1458 const Init *getOperator() const { return Val; }
1460 const std::string &getName() const { return ValName; }
1462 unsigned getNumArgs() const { return Args.size(); }
1463 const Init *getArg(unsigned Num) const {
1464 assert(Num < Args.size() && "Arg number out of range!");
1467 const std::string &getArgName(unsigned Num) const {
1468 assert(Num < ArgNames.size() && "Arg number out of range!");
1469 return ArgNames[Num];
1472 void setArg(unsigned Num, const Init *I) {
1473 assert(Num < Args.size() && "Arg number out of range!");
1477 virtual const Init *resolveReferences(Record &R,
1478 const RecordVal *RV) const;
1480 virtual std::string getAsString() const;
1482 typedef std::vector<const Init*>::iterator arg_iterator;
1483 typedef std::vector<const Init*>::const_iterator const_arg_iterator;
1484 typedef std::vector<std::string>::iterator name_iterator;
1485 typedef std::vector<std::string>::const_iterator const_name_iterator;
1487 inline arg_iterator arg_begin() { return Args.begin(); }
1488 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1489 inline arg_iterator arg_end () { return Args.end(); }
1490 inline const_arg_iterator arg_end () const { return Args.end(); }
1492 inline size_t arg_size () const { return Args.size(); }
1493 inline bool arg_empty() const { return Args.empty(); }
1495 inline name_iterator name_begin() { return ArgNames.begin(); }
1496 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1497 inline name_iterator name_end () { return ArgNames.end(); }
1498 inline const_name_iterator name_end () const { return ArgNames.end(); }
1500 inline size_t name_size () const { return ArgNames.size(); }
1501 inline bool name_empty() const { return ArgNames.empty(); }
1503 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1504 unsigned Bit) const {
1505 assert(0 && "Illegal bit reference off dag");
1509 virtual const Init *resolveListElementReference(Record &R,
1510 const RecordVal *RV,
1511 unsigned Elt) const {
1512 assert(0 && "Illegal element reference off dag");
1517 //===----------------------------------------------------------------------===//
1518 // High-Level Classes
1519 //===----------------------------------------------------------------------===//
1527 RecordVal(const std::string &N, RecTy *T, unsigned P);
1529 const std::string &getName() const { return Name; }
1531 unsigned getPrefix() const { return Prefix; }
1532 RecTy *getType() const { return Ty; }
1533 const Init *getValue() const { return Value; }
1535 bool setValue(const Init *V) {
1537 Value = V->convertInitializerTo(Ty);
1545 void print(raw_ostream &OS, bool PrintSem = true) const;
1548 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1549 RV.print(OS << " ");
1554 static unsigned LastID;
1556 // Unique record ID.
1560 std::vector<std::string> TemplateArgs;
1561 std::vector<RecordVal> Values;
1562 std::vector<Record*> SuperClasses;
1564 // Tracks Record instances. Not owned by Record.
1565 RecordKeeper &TrackedRecords;
1569 // Constructs a record.
1570 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1571 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records) {}
1575 static unsigned getNewUID() { return LastID++; }
1578 unsigned getID() const { return ID; }
1580 const std::string &getName() const { return Name; }
1581 void setName(const std::string &Name); // Also updates RecordKeeper.
1583 SMLoc getLoc() const { return Loc; }
1585 const std::vector<std::string> &getTemplateArgs() const {
1586 return TemplateArgs;
1588 const std::vector<RecordVal> &getValues() const { return Values; }
1589 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1591 bool isTemplateArg(StringRef Name) const {
1592 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1593 if (TemplateArgs[i] == Name) return true;
1597 const RecordVal *getValue(StringRef Name) const {
1598 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1599 if (Values[i].getName() == Name) return &Values[i];
1602 RecordVal *getValue(StringRef Name) {
1603 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1604 if (Values[i].getName() == Name) return &Values[i];
1608 void addTemplateArg(StringRef Name) {
1609 assert(!isTemplateArg(Name) && "Template arg already defined!");
1610 TemplateArgs.push_back(Name);
1613 void addValue(const RecordVal &RV) {
1614 assert(getValue(RV.getName()) == 0 && "Value already added!");
1615 Values.push_back(RV);
1618 void removeValue(StringRef Name) {
1619 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1620 if (Values[i].getName() == Name) {
1621 Values.erase(Values.begin()+i);
1624 assert(0 && "Cannot remove an entry that does not exist!");
1627 bool isSubClassOf(const Record *R) const {
1628 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1629 if (SuperClasses[i] == R)
1634 bool isSubClassOf(StringRef Name) const {
1635 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1636 if (SuperClasses[i]->getName() == Name)
1641 void addSuperClass(Record *R) {
1642 assert(!isSubClassOf(R) && "Already subclassing record!");
1643 SuperClasses.push_back(R);
1646 /// resolveReferences - If there are any field references that refer to fields
1647 /// that have been filled in, we can propagate the values now.
1649 void resolveReferences() { resolveReferencesTo(0); }
1651 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1652 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1653 /// possible references.
1654 void resolveReferencesTo(const RecordVal *RV);
1656 RecordKeeper &getRecords() const {
1657 return TrackedRecords;
1662 //===--------------------------------------------------------------------===//
1663 // High-level methods useful to tablegen back-ends
1666 /// getValueInit - Return the initializer for a value with the specified name,
1667 /// or throw an exception if the field does not exist.
1669 const Init *getValueInit(StringRef FieldName) const;
1671 /// getValueAsString - This method looks up the specified field and returns
1672 /// its value as a string, throwing an exception if the field does not exist
1673 /// or if the value is not a string.
1675 std::string getValueAsString(StringRef FieldName) const;
1677 /// getValueAsBitsInit - This method looks up the specified field and returns
1678 /// its value as a BitsInit, throwing an exception if the field does not exist
1679 /// or if the value is not the right type.
1681 const BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1683 /// getValueAsListInit - This method looks up the specified field and returns
1684 /// its value as a ListInit, throwing an exception if the field does not exist
1685 /// or if the value is not the right type.
1687 const ListInit *getValueAsListInit(StringRef FieldName) const;
1689 /// getValueAsListOfDefs - This method looks up the specified field and
1690 /// returns its value as a vector of records, throwing an exception if the
1691 /// field does not exist or if the value is not the right type.
1693 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1695 /// getValueAsListOfInts - This method looks up the specified field and
1696 /// returns its value as a vector of integers, throwing an exception if the
1697 /// field does not exist or if the value is not the right type.
1699 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1701 /// getValueAsListOfStrings - This method looks up the specified field and
1702 /// returns its value as a vector of strings, throwing an exception if the
1703 /// field does not exist or if the value is not the right type.
1705 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1707 /// getValueAsDef - This method looks up the specified field and returns its
1708 /// value as a Record, throwing an exception if the field does not exist or if
1709 /// the value is not the right type.
1711 Record *getValueAsDef(StringRef FieldName) const;
1713 /// getValueAsBit - This method looks up the specified field and returns its
1714 /// value as a bit, throwing an exception if the field does not exist or if
1715 /// the value is not the right type.
1717 bool getValueAsBit(StringRef FieldName) const;
1719 /// getValueAsInt - This method looks up the specified field and returns its
1720 /// value as an int64_t, throwing an exception if the field does not exist or
1721 /// if the value is not the right type.
1723 int64_t getValueAsInt(StringRef FieldName) const;
1725 /// getValueAsDag - This method looks up the specified field and returns its
1726 /// value as an Dag, throwing an exception if the field does not exist or if
1727 /// the value is not the right type.
1729 const DagInit *getValueAsDag(StringRef FieldName) const;
1731 /// getValueAsCode - This method looks up the specified field and returns
1732 /// its value as the string data in a CodeInit, throwing an exception if the
1733 /// field does not exist or if the value is not a code object.
1735 std::string getValueAsCode(StringRef FieldName) const;
1738 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1741 Record Rec; // Placeholder for template args and Name.
1742 typedef std::vector<Record*> RecordVector;
1743 RecordVector DefPrototypes;
1747 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1748 Rec(Name, Loc, Records) {}
1751 class RecordKeeper {
1752 std::map<std::string, Record*> Classes, Defs;
1755 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1756 E = Classes.end(); I != E; ++I)
1758 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1759 E = Defs.end(); I != E; ++I)
1763 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1764 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1766 Record *getClass(const std::string &Name) const {
1767 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1768 return I == Classes.end() ? 0 : I->second;
1770 Record *getDef(const std::string &Name) const {
1771 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1772 return I == Defs.end() ? 0 : I->second;
1774 void addClass(Record *R) {
1775 assert(getClass(R->getName()) == 0 && "Class already exists!");
1776 Classes.insert(std::make_pair(R->getName(), R));
1778 void addDef(Record *R) {
1779 assert(getDef(R->getName()) == 0 && "Def already exists!");
1780 Defs.insert(std::make_pair(R->getName(), R));
1783 /// removeClass - Remove, but do not delete, the specified record.
1785 void removeClass(const std::string &Name) {
1786 assert(Classes.count(Name) && "Class does not exist!");
1787 Classes.erase(Name);
1789 /// removeDef - Remove, but do not delete, the specified record.
1791 void removeDef(const std::string &Name) {
1792 assert(Defs.count(Name) && "Def does not exist!");
1796 //===--------------------------------------------------------------------===//
1797 // High-level helper methods, useful for tablegen backends...
1799 /// getAllDerivedDefinitions - This method returns all concrete definitions
1800 /// that derive from the specified class name. If a class with the specified
1801 /// name does not exist, an exception is thrown.
1802 std::vector<Record*>
1803 getAllDerivedDefinitions(const std::string &ClassName) const;
1808 /// LessRecord - Sorting predicate to sort record pointers by name.
1811 bool operator()(const Record *Rec1, const Record *Rec2) const {
1812 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1816 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1819 struct LessRecordFieldName {
1820 bool operator()(const Record *Rec1, const Record *Rec2) const {
1821 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1825 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1827 } // End llvm namespace