2 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file defines the main TableGen data structures, including the TableGen
12 // types, values, and high-level data structures.
14 //===----------------------------------------------------------------------===//
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/FoldingSet.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include "llvm/Support/DataTypes.h"
24 #include "llvm/Support/raw_ostream.h"
58 class VarListElementInit;
66 //===----------------------------------------------------------------------===//
68 //===----------------------------------------------------------------------===//
73 RecTy() : ListTy(0) {}
76 virtual std::string getAsString() const = 0;
77 void print(raw_ostream &OS) const { OS << getAsString(); }
80 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
81 /// converted to the specified type.
82 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
84 /// getListTy - Returns the type representing list<this>.
85 ListRecTy *getListTy();
87 public: // These methods should only be called from subclasses of Init
88 virtual const Init *convertValue( const UnsetInit *UI) { return 0; }
89 virtual const Init *convertValue( const BitInit *BI) { return 0; }
90 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
91 virtual const Init *convertValue( const IntInit *II) { return 0; }
92 virtual const Init *convertValue(const StringInit *SI) { return 0; }
93 virtual const Init *convertValue( const ListInit *LI) { return 0; }
94 virtual const Init *convertValue( const UnOpInit *UI) {
95 return convertValue((const TypedInit*)UI);
97 virtual const Init *convertValue( const BinOpInit *UI) {
98 return convertValue((const TypedInit*)UI);
100 virtual const Init *convertValue( const TernOpInit *UI) {
101 return convertValue((const TypedInit*)UI);
103 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
104 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
105 virtual const Init *convertValue( const DefInit *DI) { return 0; }
106 virtual const Init *convertValue( const DagInit *DI) { return 0; }
107 virtual const Init *convertValue( const TypedInit *TI) { return 0; }
108 virtual const Init *convertValue( const VarInit *VI) {
109 return convertValue((const TypedInit*)VI);
111 virtual const Init *convertValue( const FieldInit *FI) {
112 return convertValue((const TypedInit*)FI);
115 public: // These methods should only be called by subclasses of RecTy.
116 // baseClassOf - These virtual methods should be overloaded to return true iff
117 // all values of type 'RHS' can be converted to the 'this' type.
118 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
119 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
120 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
121 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
122 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
123 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
124 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
125 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
128 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
134 /// BitRecTy - 'bit' - Represent a single bit
136 class BitRecTy : public RecTy {
137 static BitRecTy Shared;
140 static BitRecTy *get() { return &Shared; }
142 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
143 virtual const Init *convertValue( const BitInit *BI) { return (const Init*)BI; }
144 virtual const Init *convertValue( const BitsInit *BI);
145 virtual const Init *convertValue( const IntInit *II);
146 virtual const Init *convertValue(const StringInit *SI) { return 0; }
147 virtual const Init *convertValue( const ListInit *LI) { return 0; }
148 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
149 virtual const Init *convertValue(const VarBitInit *VB) { return (const Init*)VB; }
150 virtual const Init *convertValue( const DefInit *DI) { return 0; }
151 virtual const Init *convertValue( const DagInit *DI) { return 0; }
152 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
153 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
154 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
155 virtual const Init *convertValue( const TypedInit *TI);
156 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
157 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
159 std::string getAsString() const { return "bit"; }
161 bool typeIsConvertibleTo(const RecTy *RHS) const {
162 return RHS->baseClassOf(this);
164 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
165 virtual bool baseClassOf(const BitsRecTy *RHS) const;
166 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
167 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
168 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
169 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
170 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
171 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
176 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
177 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
179 class BitsRecTy : public RecTy {
181 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
183 static BitsRecTy *get(unsigned Sz);
185 unsigned getNumBits() const { return Size; }
187 virtual const Init *convertValue( const UnsetInit *UI);
188 virtual const Init *convertValue( const BitInit *UI);
189 virtual const Init *convertValue( const BitsInit *BI);
190 virtual const Init *convertValue( const IntInit *II);
191 virtual const Init *convertValue(const StringInit *SI) { return 0; }
192 virtual const Init *convertValue( const ListInit *LI) { return 0; }
193 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
194 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
195 virtual const Init *convertValue( const DefInit *DI) { return 0; }
196 virtual const Init *convertValue( const DagInit *DI) { return 0; }
197 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
198 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
199 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
200 virtual const Init *convertValue( const TypedInit *TI);
201 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
202 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
204 std::string getAsString() const;
206 bool typeIsConvertibleTo(const RecTy *RHS) const {
207 return RHS->baseClassOf(this);
209 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
210 virtual bool baseClassOf(const BitsRecTy *RHS) const {
211 return RHS->Size == Size;
213 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
214 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
215 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
216 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
217 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
218 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
223 /// IntRecTy - 'int' - Represent an integer value of no particular size
225 class IntRecTy : public RecTy {
226 static IntRecTy Shared;
229 static IntRecTy *get() { return &Shared; }
231 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
232 virtual const Init *convertValue( const BitInit *BI);
233 virtual const Init *convertValue( const BitsInit *BI);
234 virtual const Init *convertValue( const IntInit *II) { return (const Init*)II; }
235 virtual const Init *convertValue(const StringInit *SI) { return 0; }
236 virtual const Init *convertValue( const ListInit *LI) { return 0; }
237 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
238 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
239 virtual const Init *convertValue( const DefInit *DI) { return 0; }
240 virtual const Init *convertValue( const DagInit *DI) { return 0; }
241 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
242 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
243 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
244 virtual const Init *convertValue( const TypedInit *TI);
245 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
246 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
248 std::string getAsString() const { return "int"; }
250 bool typeIsConvertibleTo(const RecTy *RHS) const {
251 return RHS->baseClassOf(this);
254 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
255 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
256 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
257 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
258 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
261 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
265 /// StringRecTy - 'string' - Represent an string value
267 class StringRecTy : public RecTy {
268 static StringRecTy Shared;
271 static StringRecTy *get() { return &Shared; }
273 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
274 virtual const Init *convertValue( const BitInit *BI) { return 0; }
275 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
276 virtual const Init *convertValue( const IntInit *II) { return 0; }
277 virtual const Init *convertValue(const StringInit *SI) { return (const Init*)SI; }
278 virtual const Init *convertValue( const ListInit *LI) { return 0; }
279 virtual const Init *convertValue( const UnOpInit *BO);
280 virtual const Init *convertValue( const BinOpInit *BO);
281 virtual const Init *convertValue( const TernOpInit *BO) { return RecTy::convertValue(BO);}
283 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
284 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
285 virtual const Init *convertValue( const DefInit *DI) { return 0; }
286 virtual const Init *convertValue( const DagInit *DI) { return 0; }
287 virtual const Init *convertValue( const TypedInit *TI);
288 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
289 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
291 std::string getAsString() const { return "string"; }
293 bool typeIsConvertibleTo(const RecTy *RHS) const {
294 return RHS->baseClassOf(this);
297 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
301 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
302 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
303 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
304 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
307 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
308 // the specified type.
309 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
310 /// be of the specified type.
312 class ListRecTy : public RecTy {
314 explicit ListRecTy(RecTy *T) : Ty(T) {}
315 friend ListRecTy *RecTy::getListTy();
317 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
318 RecTy *getElementType() const { return Ty; }
320 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
321 virtual const Init *convertValue( const BitInit *BI) { return 0; }
322 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
323 virtual const Init *convertValue( const IntInit *II) { return 0; }
324 virtual const Init *convertValue(const StringInit *SI) { return 0; }
325 virtual const Init *convertValue( const ListInit *LI);
326 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
327 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
328 virtual const Init *convertValue( const DefInit *DI) { return 0; }
329 virtual const Init *convertValue( const DagInit *DI) { return 0; }
330 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
331 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
332 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
333 virtual const Init *convertValue( const TypedInit *TI);
334 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
335 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
337 std::string getAsString() const;
339 bool typeIsConvertibleTo(const RecTy *RHS) const {
340 return RHS->baseClassOf(this);
343 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
344 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
345 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
346 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
347 virtual bool baseClassOf(const ListRecTy *RHS) const {
348 return RHS->getElementType()->typeIsConvertibleTo(Ty);
350 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
351 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
352 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
355 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
357 class CodeRecTy : public RecTy {
358 static CodeRecTy Shared;
361 static CodeRecTy *get() { return &Shared; }
363 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
364 virtual const Init *convertValue( const BitInit *BI) { return 0; }
365 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
366 virtual const Init *convertValue( const IntInit *II) { return 0; }
367 virtual const Init *convertValue(const StringInit *SI) { return 0; }
368 virtual const Init *convertValue( const ListInit *LI) { return 0; }
369 virtual const Init *convertValue( const CodeInit *CI) { return (const Init*)CI; }
370 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
371 virtual const Init *convertValue( const DefInit *DI) { return 0; }
372 virtual const Init *convertValue( const DagInit *DI) { return 0; }
373 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
374 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
375 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
376 virtual const Init *convertValue( const TypedInit *TI);
377 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
378 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
380 std::string getAsString() const { return "code"; }
382 bool typeIsConvertibleTo(const RecTy *RHS) const {
383 return RHS->baseClassOf(this);
385 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
386 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
387 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
388 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
389 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
390 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
391 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
392 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
395 /// DagRecTy - 'dag' - Represent a dag fragment
397 class DagRecTy : public RecTy {
398 static DagRecTy Shared;
401 static DagRecTy *get() { return &Shared; }
403 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
404 virtual const Init *convertValue( const BitInit *BI) { return 0; }
405 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
406 virtual const Init *convertValue( const IntInit *II) { return 0; }
407 virtual const Init *convertValue(const StringInit *SI) { return 0; }
408 virtual const Init *convertValue( const ListInit *LI) { return 0; }
409 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
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 UnOpInit *BO);
413 virtual const Init *convertValue( const BinOpInit *BO);
414 virtual const Init *convertValue( const TernOpInit *BO) { return RecTy::convertValue(BO);}
415 virtual const Init *convertValue( const DagInit *CI) { return (const Init*)CI; }
416 virtual const Init *convertValue( const TypedInit *TI);
417 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
418 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
420 std::string getAsString() const { return "dag"; }
422 bool typeIsConvertibleTo(const RecTy *RHS) const {
423 return RHS->baseClassOf(this);
426 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
427 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
428 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
429 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
430 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
431 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
432 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
433 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
437 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
440 class RecordRecTy : public RecTy {
442 explicit RecordRecTy(Record *R) : Rec(R) {}
445 static RecordRecTy *get(Record *R);
447 Record *getRecord() const { return Rec; }
449 virtual const Init *convertValue( const UnsetInit *UI) { return (const Init*)UI; }
450 virtual const Init *convertValue( const BitInit *BI) { return 0; }
451 virtual const Init *convertValue( const BitsInit *BI) { return 0; }
452 virtual const Init *convertValue( const IntInit *II) { return 0; }
453 virtual const Init *convertValue(const StringInit *SI) { return 0; }
454 virtual const Init *convertValue( const ListInit *LI) { return 0; }
455 virtual const Init *convertValue( const CodeInit *CI) { return 0; }
456 virtual const Init *convertValue(const VarBitInit *VB) { return 0; }
457 virtual const Init *convertValue( const UnOpInit *UI) { return RecTy::convertValue(UI);}
458 virtual const Init *convertValue( const BinOpInit *UI) { return RecTy::convertValue(UI);}
459 virtual const Init *convertValue( const TernOpInit *UI) { return RecTy::convertValue(UI);}
460 virtual const Init *convertValue( const DefInit *DI);
461 virtual const Init *convertValue( const DagInit *DI) { return 0; }
462 virtual const Init *convertValue( const TypedInit *VI);
463 virtual const Init *convertValue( const VarInit *VI) { return RecTy::convertValue(VI);}
464 virtual const Init *convertValue( const FieldInit *FI) { return RecTy::convertValue(FI);}
466 std::string getAsString() const;
468 bool typeIsConvertibleTo(const RecTy *RHS) const {
469 return RHS->baseClassOf(this);
471 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
472 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
473 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
474 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
475 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
476 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
477 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
478 virtual bool baseClassOf(const RecordRecTy *RHS) const;
481 /// resolveTypes - Find a common type that T1 and T2 convert to.
482 /// Return 0 if no such type exists.
484 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
486 //===----------------------------------------------------------------------===//
487 // Initializer Classes
488 //===----------------------------------------------------------------------===//
491 Init(const Init &); // Do not define.
492 Init &operator=(const Init &); // Do not define.
500 /// isComplete - This virtual method should be overridden by values that may
501 /// not be completely specified yet.
502 virtual bool isComplete() const { return true; }
504 /// print - Print out this value.
505 void print(raw_ostream &OS) const { OS << getAsString(); }
507 /// getAsString - Convert this value to a string form.
508 virtual std::string getAsString() const = 0;
510 /// dump - Debugging method that may be called through a debugger, just
511 /// invokes print on stderr.
514 /// convertInitializerTo - This virtual function is a simple call-back
515 /// function that should be overridden to call the appropriate
516 /// RecTy::convertValue method.
518 virtual const Init *convertInitializerTo(RecTy *Ty) const = 0;
520 /// convertInitializerBitRange - This method is used to implement the bitrange
521 /// selection operator. Given an initializer, it selects the specified bits
522 /// out, returning them as a new init of bits type. If it is not legal to use
523 /// the bit subscript operator on this initializer, return null.
526 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
530 /// convertInitListSlice - This method is used to implement the list slice
531 /// selection operator. Given an initializer, it selects the specified list
532 /// elements, returning them as a new init of list type. If it is not legal
533 /// to take a slice of this, return null.
536 convertInitListSlice(const std::vector<unsigned> &Elements) const {
540 /// getFieldType - This method is used to implement the FieldInit class.
541 /// Implementors of this method should return the type of the named field if
542 /// they are of record type.
544 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
546 /// getFieldInit - This method complements getFieldType to return the
547 /// initializer for the specified field. If getFieldType returns non-null
548 /// this method should return non-null, otherwise it returns null.
550 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
551 const std::string &FieldName) const {
555 /// resolveReferences - This method is used by classes that refer to other
556 /// variables which may not be defined at the time the expression is formed.
557 /// If a value is set for the variable later, this method will be called on
558 /// users of the value to allow the value to propagate out.
560 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const {
565 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
566 I.print(OS); return OS;
569 /// TypedInit - This is the common super-class of types that have a specific,
572 class TypedInit : public Init {
575 TypedInit(const TypedInit &Other); // Do not define.
576 TypedInit &operator=(const TypedInit &Other); // Do not define.
579 explicit TypedInit(RecTy *T) : Ty(T) {}
582 RecTy *getType() const { return Ty; }
585 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
587 convertInitListSlice(const std::vector<unsigned> &Elements) const;
589 /// getFieldType - This method is used to implement the FieldInit class.
590 /// Implementors of this method should return the type of the named field if
591 /// they are of record type.
593 virtual RecTy *getFieldType(const std::string &FieldName) const;
595 /// resolveBitReference - This method is used to implement
596 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
597 /// simply return the resolved value, otherwise we return null.
599 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
600 unsigned Bit) const = 0;
602 /// resolveListElementReference - This method is used to implement
603 /// VarListElementInit::resolveReferences. If the list element is resolvable
604 /// now, we return the resolved value, otherwise we return null.
605 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
606 unsigned Elt) const = 0;
610 /// UnsetInit - ? - Represents an uninitialized value
612 class UnsetInit : public Init {
613 UnsetInit() : Init() {}
614 UnsetInit(const UnsetInit &); // Do not define.
615 UnsetInit &operator=(const UnsetInit &Other); // Do not define.
618 static const UnsetInit *get();
620 virtual const Init *convertInitializerTo(RecTy *Ty) const {
621 return Ty->convertValue(this);
624 virtual bool isComplete() const { return false; }
625 virtual std::string getAsString() const { return "?"; }
629 /// BitInit - true/false - Represent a concrete initializer for a bit.
631 class BitInit : public Init {
634 explicit BitInit(bool V) : Value(V) {}
635 BitInit(const BitInit &Other); // Do not define.
636 BitInit &operator=(BitInit &Other); // Do not define.
639 static const BitInit *get(bool V);
641 bool getValue() const { return Value; }
643 virtual const Init *convertInitializerTo(RecTy *Ty) const {
644 return Ty->convertValue(this);
647 virtual std::string getAsString() const { return Value ? "1" : "0"; }
650 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
651 /// It contains a vector of bits, whose size is determined by the type.
653 class BitsInit : public Init, public FoldingSetNode {
654 std::vector<const Init*> Bits;
656 BitsInit(ArrayRef<const Init *> Range) : Bits(Range.begin(), Range.end()) {}
658 BitsInit(const BitsInit &Other); // Do not define.
659 BitsInit &operator=(const BitsInit &Other); // Do not define.
662 static const BitsInit *get(ArrayRef<const Init *> Range);
664 void Profile(FoldingSetNodeID &ID) const;
666 unsigned getNumBits() const { return Bits.size(); }
668 const Init *getBit(unsigned Bit) const {
669 assert(Bit < Bits.size() && "Bit index out of range!");
673 virtual const Init *convertInitializerTo(RecTy *Ty) const {
674 return Ty->convertValue(this);
677 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
679 virtual bool isComplete() const {
680 for (unsigned i = 0; i != getNumBits(); ++i)
681 if (!getBit(i)->isComplete()) return false;
684 bool allInComplete() const {
685 for (unsigned i = 0; i != getNumBits(); ++i)
686 if (getBit(i)->isComplete()) return false;
689 virtual std::string getAsString() const;
691 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
695 /// IntInit - 7 - Represent an initalization by a literal integer value.
697 class IntInit : public TypedInit {
700 explicit IntInit(int64_t V) : TypedInit(IntRecTy::get()), Value(V) {}
702 IntInit(const IntInit &Other); // Do not define.
703 IntInit &operator=(const IntInit &Other); // Do note define.
706 static const IntInit *get(int64_t V);
708 int64_t getValue() const { return Value; }
710 virtual const Init *convertInitializerTo(RecTy *Ty) const {
711 return Ty->convertValue(this);
714 convertInitializerBitRange(const std::vector<unsigned> &Bits) const;
716 virtual std::string getAsString() const;
718 /// resolveBitReference - This method is used to implement
719 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
720 /// simply return the resolved value, otherwise we return null.
722 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
723 unsigned Bit) const {
724 assert(0 && "Illegal bit reference off int");
728 /// resolveListElementReference - This method is used to implement
729 /// VarListElementInit::resolveReferences. If the list element is resolvable
730 /// now, we return the resolved value, otherwise we return null.
731 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
732 unsigned Elt) const {
733 assert(0 && "Illegal element reference off int");
739 /// StringInit - "foo" - Represent an initialization by a string value.
741 class StringInit : public TypedInit {
744 explicit StringInit(const std::string &V)
745 : TypedInit(StringRecTy::get()), Value(V) {}
747 StringInit(const StringInit &Other); // Do not define.
748 StringInit &operator=(const StringInit &Other); // Do not define.
751 static const StringInit *get(const std::string &V);
753 const std::string &getValue() const { return Value; }
755 virtual const Init *convertInitializerTo(RecTy *Ty) const {
756 return Ty->convertValue(this);
759 virtual std::string getAsString() const { return "\"" + Value + "\""; }
761 /// resolveBitReference - This method is used to implement
762 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
763 /// simply return the resolved value, otherwise we return null.
765 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
766 unsigned Bit) const {
767 assert(0 && "Illegal bit reference off string");
771 /// resolveListElementReference - This method is used to implement
772 /// VarListElementInit::resolveReferences. If the list element is resolvable
773 /// now, we return the resolved value, otherwise we return null.
774 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
775 unsigned Elt) const {
776 assert(0 && "Illegal element reference off string");
781 /// CodeInit - "[{...}]" - Represent a code fragment.
783 class CodeInit : public Init {
786 explicit CodeInit(const std::string &V) : Value(V) {}
788 CodeInit(const CodeInit &Other); // Do not define.
789 CodeInit &operator=(const CodeInit &Other); // Do not define.
792 static const CodeInit *get(const std::string &V);
794 const std::string &getValue() const { return Value; }
796 virtual const Init *convertInitializerTo(RecTy *Ty) const {
797 return Ty->convertValue(this);
800 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
803 /// ListInit - [AL, AH, CL] - Represent a list of defs
805 class ListInit : public TypedInit, public FoldingSetNode {
806 std::vector<const Init*> Values;
808 typedef std::vector<const Init*>::const_iterator const_iterator;
811 explicit ListInit(ArrayRef<const Init *> Range, RecTy *EltTy)
812 : TypedInit(ListRecTy::get(EltTy)), Values(Range.begin(), Range.end()) {}
814 ListInit(const ListInit &Other); // Do not define.
815 ListInit &operator=(const ListInit &Other); // Do not define.
818 static const ListInit *get(ArrayRef<const Init *> Range, RecTy *EltTy);
820 void Profile(FoldingSetNodeID &ID) const;
822 unsigned getSize() const { return Values.size(); }
823 const Init *getElement(unsigned i) const {
824 assert(i < Values.size() && "List element index out of range!");
828 Record *getElementAsRecord(unsigned i) const;
830 const Init *convertInitListSlice(const std::vector<unsigned> &Elements) const;
832 virtual const Init *convertInitializerTo(RecTy *Ty) const {
833 return Ty->convertValue(this);
836 /// resolveReferences - This method is used by classes that refer to other
837 /// variables which may not be defined at the time they expression is formed.
838 /// If a value is set for the variable later, this method will be called on
839 /// users of the value to allow the value to propagate out.
841 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
843 virtual std::string getAsString() const;
845 ArrayRef<const Init*> getValues() const { return Values; }
847 inline const_iterator begin() const { return Values.begin(); }
848 inline const_iterator end () const { return Values.end(); }
850 inline size_t size () const { return Values.size(); }
851 inline bool empty() const { return Values.empty(); }
853 /// resolveBitReference - This method is used to implement
854 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
855 /// simply return the resolved value, otherwise we return null.
857 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
858 unsigned Bit) const {
859 assert(0 && "Illegal bit reference off list");
863 /// resolveListElementReference - This method is used to implement
864 /// VarListElementInit::resolveReferences. If the list element is resolvable
865 /// now, we return the resolved value, otherwise we return null.
866 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
871 /// OpInit - Base class for operators
873 class OpInit : public TypedInit {
874 OpInit(const OpInit &Other); // Do not define.
875 OpInit &operator=(OpInit &Other); // Do not define.
878 explicit OpInit(RecTy *Type) : TypedInit(Type) {}
881 // Clone - Clone this operator, replacing arguments with the new list
882 virtual const OpInit *clone(std::vector<const Init *> &Operands) const = 0;
884 virtual int getNumOperands() const = 0;
885 virtual const Init *getOperand(int i) const = 0;
887 // Fold - If possible, fold this to a simpler init. Return this if not
889 virtual const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
891 virtual const Init *convertInitializerTo(RecTy *Ty) const {
892 return Ty->convertValue(this);
895 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
897 virtual const Init *resolveListElementReference(Record &R,
903 /// UnOpInit - !op (X) - Transform an init.
905 class UnOpInit : public OpInit {
907 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
912 UnOpInit(UnaryOp opc, const Init *lhs, RecTy *Type)
913 : OpInit(Type), Opc(opc), LHS(lhs) {}
915 UnOpInit(const UnOpInit &Other); // Do not define.
916 UnOpInit &operator=(const UnOpInit &Other); // Do not define.
919 static const UnOpInit *get(UnaryOp opc, const Init *lhs, RecTy *Type);
921 // Clone - Clone this operator, replacing arguments with the new list
922 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
923 assert(Operands.size() == 1 &&
924 "Wrong number of operands for unary operation");
925 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
928 int getNumOperands() const { return 1; }
929 const Init *getOperand(int i) const {
930 assert(i == 0 && "Invalid operand id for unary operator");
934 UnaryOp getOpcode() const { return Opc; }
935 const Init *getOperand() const { return LHS; }
937 // Fold - If possible, fold this to a simpler init. Return this if not
939 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
941 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
943 virtual std::string getAsString() const;
946 /// BinOpInit - !op (X, Y) - Combine two inits.
948 class BinOpInit : public OpInit {
950 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, EQ };
953 const Init *LHS, *RHS;
955 BinOpInit(BinaryOp opc, const Init *lhs, const Init *rhs, RecTy *Type) :
956 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {}
958 BinOpInit(const BinOpInit &Other); // Do not define.
959 BinOpInit &operator=(const BinOpInit &Other); // Do not define.
962 static const BinOpInit *get(BinaryOp opc, const Init *lhs, const Init *rhs,
965 // Clone - Clone this operator, replacing arguments with the new list
966 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
967 assert(Operands.size() == 2 &&
968 "Wrong number of operands for binary operation");
969 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
972 int getNumOperands() const { return 2; }
973 const Init *getOperand(int i) const {
974 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
982 BinaryOp getOpcode() const { return Opc; }
983 const Init *getLHS() const { return LHS; }
984 const Init *getRHS() const { return RHS; }
986 // Fold - If possible, fold this to a simpler init. Return this if not
988 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
990 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
992 virtual std::string getAsString() const;
995 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
997 class TernOpInit : public OpInit {
999 enum TernaryOp { SUBST, FOREACH, IF };
1002 const Init *LHS, *MHS, *RHS;
1004 TernOpInit(TernaryOp opc, const Init *lhs, const Init *mhs, const Init *rhs,
1006 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1008 TernOpInit(const TernOpInit &Other); // Do not define.
1009 TernOpInit &operator=(const TernOpInit &Other); // Do not define.
1012 static const TernOpInit *get(TernaryOp opc, const Init *lhs,
1013 const Init *mhs, const Init *rhs,
1016 // Clone - Clone this operator, replacing arguments with the new list
1017 virtual const OpInit *clone(std::vector<const Init *> &Operands) const {
1018 assert(Operands.size() == 3 &&
1019 "Wrong number of operands for ternary operation");
1020 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1024 int getNumOperands() const { return 3; }
1025 const Init *getOperand(int i) const {
1026 assert((i == 0 || i == 1 || i == 2) &&
1027 "Invalid operand id for ternary operator");
1030 } else if (i == 1) {
1037 TernaryOp getOpcode() const { return Opc; }
1038 const Init *getLHS() const { return LHS; }
1039 const Init *getMHS() const { return MHS; }
1040 const Init *getRHS() const { return RHS; }
1042 // Fold - If possible, fold this to a simpler init. Return this if not
1043 // possible to fold.
1044 const Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const;
1046 virtual bool isComplete() const { return false; }
1048 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1050 virtual std::string getAsString() const;
1054 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1056 class VarInit : public TypedInit {
1057 std::string VarName;
1059 explicit VarInit(const std::string &VN, RecTy *T)
1060 : TypedInit(T), VarName(VN) {}
1062 VarInit(const VarInit &Other); // Do not define.
1063 VarInit &operator=(const VarInit &Other); // Do not define.
1066 static const VarInit *get(const std::string &VN, RecTy *T);
1067 static const VarInit *get(const Init *VN, RecTy *T);
1069 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1070 return Ty->convertValue(this);
1073 const std::string &getName() const { return VarName; }
1075 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1076 unsigned Bit) const;
1077 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1078 unsigned Elt) const;
1080 virtual RecTy *getFieldType(const std::string &FieldName) const;
1081 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1082 const std::string &FieldName) const;
1084 /// resolveReferences - This method is used by classes that refer to other
1085 /// variables which may not be defined at the time they expression is formed.
1086 /// If a value is set for the variable later, this method will be called on
1087 /// users of the value to allow the value to propagate out.
1089 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1091 virtual std::string getAsString() const { return VarName; }
1095 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1097 class VarBitInit : public Init {
1098 const TypedInit *TI;
1101 VarBitInit(const TypedInit *T, unsigned B) : TI(T), Bit(B) {
1102 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
1103 ((BitsRecTy*)T->getType())->getNumBits() > B &&
1104 "Illegal VarBitInit expression!");
1107 VarBitInit(const VarBitInit &Other); // Do not define.
1108 VarBitInit &operator=(const VarBitInit &Other); // Do not define.
1111 static const VarBitInit *get(const TypedInit *T, unsigned B);
1113 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1114 return Ty->convertValue(this);
1117 const TypedInit *getVariable() const { return TI; }
1118 unsigned getBitNum() const { return Bit; }
1120 virtual std::string getAsString() const;
1121 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1124 /// VarListElementInit - List[4] - Represent access to one element of a var or
1126 class VarListElementInit : public TypedInit {
1127 const TypedInit *TI;
1130 VarListElementInit(const TypedInit *T, unsigned E)
1131 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1133 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1134 "Illegal VarBitInit expression!");
1137 VarListElementInit(const VarListElementInit &Other); // Do not define.
1138 VarListElementInit &operator=(const VarListElementInit &Other); // Do
1143 static const VarListElementInit *get(const TypedInit *T, unsigned E);
1145 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1146 return Ty->convertValue(this);
1149 const TypedInit *getVariable() const { return TI; }
1150 unsigned getElementNum() const { return Element; }
1152 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1153 unsigned Bit) const;
1155 /// resolveListElementReference - This method is used to implement
1156 /// VarListElementInit::resolveReferences. If the list element is resolvable
1157 /// now, we return the resolved value, otherwise we return null.
1158 virtual const Init *resolveListElementReference(Record &R,
1159 const RecordVal *RV,
1160 unsigned Elt) const;
1162 virtual std::string getAsString() const;
1163 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1166 /// DefInit - AL - Represent a reference to a 'def' in the description
1168 class DefInit : public TypedInit {
1171 DefInit(Record *D, RecordRecTy *T) : TypedInit(T), Def(D) {}
1172 friend class Record;
1174 DefInit(const DefInit &Other); // Do not define.
1175 DefInit &operator=(const DefInit &Other); // Do not define.
1178 static const DefInit *get(Record*);
1180 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1181 return Ty->convertValue(this);
1184 Record *getDef() const { return Def; }
1186 //virtual const Init *
1187 //convertInitializerBitRange(const std::vector<unsigned> &Bits);
1189 virtual RecTy *getFieldType(const std::string &FieldName) const;
1190 virtual const Init *getFieldInit(Record &R, const RecordVal *RV,
1191 const std::string &FieldName) const;
1193 virtual std::string getAsString() const;
1195 /// resolveBitReference - This method is used to implement
1196 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1197 /// simply return the resolved value, otherwise we return null.
1199 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1200 unsigned Bit) const {
1201 assert(0 && "Illegal bit reference off def");
1205 /// resolveListElementReference - This method is used to implement
1206 /// VarListElementInit::resolveReferences. If the list element is resolvable
1207 /// now, we return the resolved value, otherwise we return null.
1208 virtual const Init *resolveListElementReference(Record &R, const RecordVal *RV,
1209 unsigned Elt) const {
1210 assert(0 && "Illegal element reference off def");
1216 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1218 class FieldInit : public TypedInit {
1219 const Init *Rec; // Record we are referring to
1220 std::string FieldName; // Field we are accessing
1222 FieldInit(const Init *R, const std::string &FN)
1223 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1224 assert(getType() && "FieldInit with non-record type!");
1227 FieldInit(const FieldInit &Other); // Do not define.
1228 FieldInit &operator=(const FieldInit &Other); // Do not define.
1231 static const FieldInit *get(const Init *R, const std::string &FN);
1232 static const FieldInit *get(const Init *R, const Init *FN);
1234 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1235 return Ty->convertValue(this);
1238 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1239 unsigned Bit) const;
1240 virtual const Init *resolveListElementReference(Record &R,
1241 const RecordVal *RV,
1242 unsigned Elt) const;
1244 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1246 virtual std::string getAsString() const {
1247 return Rec->getAsString() + "." + FieldName;
1251 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1252 /// to have at least one value then a (possibly empty) list of arguments. Each
1253 /// argument can have a name associated with it.
1255 class DagInit : public TypedInit {
1257 std::string ValName;
1258 std::vector<const Init*> Args;
1259 std::vector<std::string> ArgNames;
1261 DagInit(const Init *V, const std::string &VN,
1262 const std::vector<std::pair<const Init*, std::string> > &args)
1263 : TypedInit(DagRecTy::get()), Val(V), ValName(VN) {
1264 Args.reserve(args.size());
1265 ArgNames.reserve(args.size());
1266 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1267 Args.push_back(args[i].first);
1268 ArgNames.push_back(args[i].second);
1271 DagInit(const Init *V, const std::string &VN,
1272 const std::vector<const Init*> &args,
1273 const std::vector<std::string> &argNames)
1274 : TypedInit(DagRecTy::get()), Val(V), ValName(VN), Args(args),
1275 ArgNames(argNames) { }
1277 DagInit(const DagInit &Other); // Do not define.
1278 DagInit &operator=(const DagInit &Other); // Do not define.
1281 static const DagInit *get(const Init *V, const std::string &VN,
1283 std::pair<const Init*, std::string> > &args);
1285 static const DagInit *get(const Init *V, const std::string &VN,
1286 const std::vector<const Init*> &args,
1287 const std::vector<std::string> &argNames);
1289 virtual const Init *convertInitializerTo(RecTy *Ty) const {
1290 return Ty->convertValue(this);
1293 const Init *getOperator() const { return Val; }
1295 const std::string &getName() const { return ValName; }
1297 unsigned getNumArgs() const { return Args.size(); }
1298 const Init *getArg(unsigned Num) const {
1299 assert(Num < Args.size() && "Arg number out of range!");
1302 const std::string &getArgName(unsigned Num) const {
1303 assert(Num < ArgNames.size() && "Arg number out of range!");
1304 return ArgNames[Num];
1307 virtual const Init *resolveReferences(Record &R, const RecordVal *RV) const;
1309 virtual std::string getAsString() const;
1311 typedef std::vector<const Init*>::const_iterator const_arg_iterator;
1312 typedef std::vector<std::string>::const_iterator const_name_iterator;
1314 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1315 inline const_arg_iterator arg_end () const { return Args.end(); }
1317 inline size_t arg_size () const { return Args.size(); }
1318 inline bool arg_empty() const { return Args.empty(); }
1320 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1321 inline const_name_iterator name_end () const { return ArgNames.end(); }
1323 inline size_t name_size () const { return ArgNames.size(); }
1324 inline bool name_empty() const { return ArgNames.empty(); }
1326 virtual const Init *resolveBitReference(Record &R, const RecordVal *RV,
1327 unsigned Bit) const {
1328 assert(0 && "Illegal bit reference off dag");
1332 virtual const Init *resolveListElementReference(Record &R,
1333 const RecordVal *RV,
1334 unsigned Elt) const {
1335 assert(0 && "Illegal element reference off dag");
1340 //===----------------------------------------------------------------------===//
1341 // High-Level Classes
1342 //===----------------------------------------------------------------------===//
1350 RecordVal(const std::string &N, RecTy *T, unsigned P);
1352 const std::string &getName() const { return Name; }
1354 unsigned getPrefix() const { return Prefix; }
1355 RecTy *getType() const { return Ty; }
1356 const Init *getValue() const { return Value; }
1358 bool setValue(const Init *V) {
1360 Value = V->convertInitializerTo(Ty);
1368 void print(raw_ostream &OS, bool PrintSem = true) const;
1371 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1372 RV.print(OS << " ");
1377 static unsigned LastID;
1379 // Unique record ID.
1383 std::vector<std::string> TemplateArgs;
1384 std::vector<RecordVal> Values;
1385 std::vector<Record*> SuperClasses;
1387 // Tracks Record instances. Not owned by Record.
1388 RecordKeeper &TrackedRecords;
1394 // Constructs a record.
1395 explicit Record(const std::string &N, SMLoc loc, RecordKeeper &records) :
1396 ID(LastID++), Name(N), Loc(loc), TrackedRecords(records), TheInit(0) {}
1400 static unsigned getNewUID() { return LastID++; }
1403 unsigned getID() const { return ID; }
1405 const std::string &getName() const { return Name; }
1406 void setName(const std::string &Name); // Also updates RecordKeeper.
1408 SMLoc getLoc() const { return Loc; }
1410 /// get the corresponding DefInit.
1411 DefInit *getDefInit();
1413 const std::vector<std::string> &getTemplateArgs() const {
1414 return TemplateArgs;
1416 const std::vector<RecordVal> &getValues() const { return Values; }
1417 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1419 bool isTemplateArg(StringRef Name) const {
1420 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1421 if (TemplateArgs[i] == Name) return true;
1425 const RecordVal *getValue(StringRef Name) const {
1426 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1427 if (Values[i].getName() == Name) return &Values[i];
1430 RecordVal *getValue(StringRef Name) {
1431 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1432 if (Values[i].getName() == Name) return &Values[i];
1436 void addTemplateArg(StringRef Name) {
1437 assert(!isTemplateArg(Name) && "Template arg already defined!");
1438 TemplateArgs.push_back(Name);
1441 void addValue(const RecordVal &RV) {
1442 assert(getValue(RV.getName()) == 0 && "Value already added!");
1443 Values.push_back(RV);
1446 void removeValue(StringRef Name) {
1447 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1448 if (Values[i].getName() == Name) {
1449 Values.erase(Values.begin()+i);
1452 assert(0 && "Cannot remove an entry that does not exist!");
1455 bool isSubClassOf(const Record *R) const {
1456 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1457 if (SuperClasses[i] == R)
1462 bool isSubClassOf(StringRef Name) const {
1463 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1464 if (SuperClasses[i]->getName() == Name)
1469 void addSuperClass(Record *R) {
1470 assert(!isSubClassOf(R) && "Already subclassing record!");
1471 SuperClasses.push_back(R);
1474 /// resolveReferences - If there are any field references that refer to fields
1475 /// that have been filled in, we can propagate the values now.
1477 void resolveReferences() { resolveReferencesTo(0); }
1479 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1480 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1481 /// possible references.
1482 void resolveReferencesTo(const RecordVal *RV);
1484 RecordKeeper &getRecords() const {
1485 return TrackedRecords;
1490 //===--------------------------------------------------------------------===//
1491 // High-level methods useful to tablegen back-ends
1494 /// getValueInit - Return the initializer for a value with the specified name,
1495 /// or throw an exception if the field does not exist.
1497 const Init *getValueInit(StringRef FieldName) const;
1499 /// getValueAsString - This method looks up the specified field and returns
1500 /// its value as a string, throwing an exception if the field does not exist
1501 /// or if the value is not a string.
1503 std::string getValueAsString(StringRef FieldName) const;
1505 /// getValueAsBitsInit - This method looks up the specified field and returns
1506 /// its value as a BitsInit, throwing an exception if the field does not exist
1507 /// or if the value is not the right type.
1509 const BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1511 /// getValueAsListInit - This method looks up the specified field and returns
1512 /// its value as a ListInit, throwing an exception if the field does not exist
1513 /// or if the value is not the right type.
1515 const ListInit *getValueAsListInit(StringRef FieldName) const;
1517 /// getValueAsListOfDefs - This method looks up the specified field and
1518 /// returns its value as a vector of records, throwing an exception if the
1519 /// field does not exist or if the value is not the right type.
1521 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1523 /// getValueAsListOfInts - This method looks up the specified field and
1524 /// returns its value as a vector of integers, throwing an exception if the
1525 /// field does not exist or if the value is not the right type.
1527 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1529 /// getValueAsListOfStrings - This method looks up the specified field and
1530 /// returns its value as a vector of strings, throwing an exception if the
1531 /// field does not exist or if the value is not the right type.
1533 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1535 /// getValueAsDef - This method looks up the specified field and returns its
1536 /// value as a Record, throwing an exception if the field does not exist or if
1537 /// the value is not the right type.
1539 Record *getValueAsDef(StringRef FieldName) const;
1541 /// getValueAsBit - This method looks up the specified field and returns its
1542 /// value as a bit, throwing an exception if the field does not exist or if
1543 /// the value is not the right type.
1545 bool getValueAsBit(StringRef FieldName) const;
1547 /// getValueAsInt - This method looks up the specified field and returns its
1548 /// value as an int64_t, throwing an exception if the field does not exist or
1549 /// if the value is not the right type.
1551 int64_t getValueAsInt(StringRef FieldName) const;
1553 /// getValueAsDag - This method looks up the specified field and returns its
1554 /// value as an Dag, throwing an exception if the field does not exist or if
1555 /// the value is not the right type.
1557 const DagInit *getValueAsDag(StringRef FieldName) const;
1559 /// getValueAsCode - This method looks up the specified field and returns
1560 /// its value as the string data in a CodeInit, throwing an exception if the
1561 /// field does not exist or if the value is not a code object.
1563 std::string getValueAsCode(StringRef FieldName) const;
1566 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1569 Record Rec; // Placeholder for template args and Name.
1570 typedef std::vector<Record*> RecordVector;
1571 RecordVector DefPrototypes;
1575 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1576 Rec(Name, Loc, Records) {}
1579 class RecordKeeper {
1580 std::map<std::string, Record*> Classes, Defs;
1583 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
1584 E = Classes.end(); I != E; ++I)
1586 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
1587 E = Defs.end(); I != E; ++I)
1591 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1592 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1594 Record *getClass(const std::string &Name) const {
1595 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1596 return I == Classes.end() ? 0 : I->second;
1598 Record *getDef(const std::string &Name) const {
1599 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1600 return I == Defs.end() ? 0 : I->second;
1602 void addClass(Record *R) {
1603 assert(getClass(R->getName()) == 0 && "Class already exists!");
1604 Classes.insert(std::make_pair(R->getName(), R));
1606 void addDef(Record *R) {
1607 assert(getDef(R->getName()) == 0 && "Def already exists!");
1608 Defs.insert(std::make_pair(R->getName(), R));
1611 /// removeClass - Remove, but do not delete, the specified record.
1613 void removeClass(const std::string &Name) {
1614 assert(Classes.count(Name) && "Class does not exist!");
1615 Classes.erase(Name);
1617 /// removeDef - Remove, but do not delete, the specified record.
1619 void removeDef(const std::string &Name) {
1620 assert(Defs.count(Name) && "Def does not exist!");
1624 //===--------------------------------------------------------------------===//
1625 // High-level helper methods, useful for tablegen backends...
1627 /// getAllDerivedDefinitions - This method returns all concrete definitions
1628 /// that derive from the specified class name. If a class with the specified
1629 /// name does not exist, an exception is thrown.
1630 std::vector<Record*>
1631 getAllDerivedDefinitions(const std::string &ClassName) const;
1636 /// LessRecord - Sorting predicate to sort record pointers by name.
1639 bool operator()(const Record *Rec1, const Record *Rec2) const {
1640 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1644 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1647 struct LessRecordFieldName {
1648 bool operator()(const Record *Rec1, const Record *Rec2) const {
1649 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1653 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1655 } // End llvm namespace