]> git.proxmox.com Git - rustc.git/blob - src/llvm/include/llvm/TableGen/Record.h
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / include / llvm / TableGen / Record.h
1 //===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TABLEGEN_RECORD_H
16 #define LLVM_TABLEGEN_RECORD_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/FoldingSet.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <map>
27
28 namespace llvm {
29 class raw_ostream;
30
31 // RecTy subclasses.
32 class BitRecTy;
33 class BitsRecTy;
34 class IntRecTy;
35 class StringRecTy;
36 class ListRecTy;
37 class DagRecTy;
38 class RecordRecTy;
39
40 // Init subclasses.
41 class Init;
42 class UnsetInit;
43 class BitInit;
44 class BitsInit;
45 class IntInit;
46 class StringInit;
47 class ListInit;
48 class UnOpInit;
49 class BinOpInit;
50 class TernOpInit;
51 class DefInit;
52 class DagInit;
53 class TypedInit;
54 class VarInit;
55 class FieldInit;
56 class VarBitInit;
57 class VarListElementInit;
58
59 // Other classes.
60 class Record;
61 class RecordVal;
62 struct MultiClass;
63 class RecordKeeper;
64
65 //===----------------------------------------------------------------------===//
66 // Type Classes
67 //===----------------------------------------------------------------------===//
68
69 class RecTy {
70 public:
71 /// \brief Subclass discriminator (for dyn_cast<> et al.)
72 enum RecTyKind {
73 BitRecTyKind,
74 BitsRecTyKind,
75 IntRecTyKind,
76 StringRecTyKind,
77 ListRecTyKind,
78 DagRecTyKind,
79 RecordRecTyKind
80 };
81
82 private:
83 RecTyKind Kind;
84 ListRecTy *ListTy;
85 virtual void anchor();
86
87 public:
88 RecTyKind getRecTyKind() const { return Kind; }
89
90 RecTy(RecTyKind K) : Kind(K), ListTy(nullptr) {}
91 virtual ~RecTy() {}
92
93 virtual std::string getAsString() const = 0;
94 void print(raw_ostream &OS) const { OS << getAsString(); }
95 void dump() const;
96
97 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
98 /// converted to the specified type.
99 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
100
101 /// getListTy - Returns the type representing list<this>.
102 ListRecTy *getListTy();
103
104 public: // These methods should only be called from subclasses of Init
105 virtual Init *convertValue( UnsetInit *UI) { return nullptr; }
106 virtual Init *convertValue( BitInit *BI) { return nullptr; }
107 virtual Init *convertValue( BitsInit *BI) { return nullptr; }
108 virtual Init *convertValue( IntInit *II) { return nullptr; }
109 virtual Init *convertValue(StringInit *SI) { return nullptr; }
110 virtual Init *convertValue( ListInit *LI) { return nullptr; }
111 virtual Init *convertValue( UnOpInit *UI) {
112 return convertValue((TypedInit*)UI);
113 }
114 virtual Init *convertValue( BinOpInit *UI) {
115 return convertValue((TypedInit*)UI);
116 }
117 virtual Init *convertValue( TernOpInit *UI) {
118 return convertValue((TypedInit*)UI);
119 }
120 virtual Init *convertValue(VarBitInit *VB) { return nullptr; }
121 virtual Init *convertValue( DefInit *DI) { return nullptr; }
122 virtual Init *convertValue( DagInit *DI) { return nullptr; }
123 virtual Init *convertValue( TypedInit *TI) { return nullptr; }
124 virtual Init *convertValue( VarInit *VI) {
125 return convertValue((TypedInit*)VI);
126 }
127 virtual Init *convertValue( FieldInit *FI) {
128 return convertValue((TypedInit*)FI);
129 }
130
131 public:
132 virtual bool baseClassOf(const RecTy*) const;
133 };
134
135 inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
136 Ty.print(OS);
137 return OS;
138 }
139
140 /// BitRecTy - 'bit' - Represent a single bit
141 ///
142 class BitRecTy : public RecTy {
143 static BitRecTy Shared;
144 BitRecTy() : RecTy(BitRecTyKind) {}
145
146 public:
147 static bool classof(const RecTy *RT) {
148 return RT->getRecTyKind() == BitRecTyKind;
149 }
150
151 static BitRecTy *get() { return &Shared; }
152
153 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
154 Init *convertValue( BitInit *BI) override { return (Init*)BI; }
155 Init *convertValue( BitsInit *BI) override;
156 Init *convertValue( IntInit *II) override;
157 Init *convertValue(StringInit *SI) override { return nullptr; }
158 Init *convertValue( ListInit *LI) override { return nullptr; }
159 Init *convertValue(VarBitInit *VB) override { return (Init*)VB; }
160 Init *convertValue( DefInit *DI) override { return nullptr; }
161 Init *convertValue( DagInit *DI) override { return nullptr; }
162 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
163 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
164 Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
165 Init *convertValue( TypedInit *TI) override;
166 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
167 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
168
169 std::string getAsString() const override { return "bit"; }
170
171 bool typeIsConvertibleTo(const RecTy *RHS) const override {
172 return RHS->baseClassOf(this);
173 }
174 bool baseClassOf(const RecTy*) const override;
175 };
176
177 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
178 ///
179 class BitsRecTy : public RecTy {
180 unsigned Size;
181 explicit BitsRecTy(unsigned Sz) : RecTy(BitsRecTyKind), Size(Sz) {}
182
183 public:
184 static bool classof(const RecTy *RT) {
185 return RT->getRecTyKind() == BitsRecTyKind;
186 }
187
188 static BitsRecTy *get(unsigned Sz);
189
190 unsigned getNumBits() const { return Size; }
191
192 Init *convertValue( UnsetInit *UI) override;
193 Init *convertValue( BitInit *UI) override;
194 Init *convertValue( BitsInit *BI) override;
195 Init *convertValue( IntInit *II) override;
196 Init *convertValue(StringInit *SI) override { return nullptr; }
197 Init *convertValue( ListInit *LI) override { return nullptr; }
198 Init *convertValue(VarBitInit *VB) override { return nullptr; }
199 Init *convertValue( DefInit *DI) override { return nullptr; }
200 Init *convertValue( DagInit *DI) override { return nullptr; }
201 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
202 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
203 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
204 Init *convertValue( TypedInit *TI) override;
205 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
206 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
207
208 std::string getAsString() const override;
209
210 bool typeIsConvertibleTo(const RecTy *RHS) const override {
211 return RHS->baseClassOf(this);
212 }
213 bool baseClassOf(const RecTy*) const override;
214 };
215
216 /// IntRecTy - 'int' - Represent an integer value of no particular size
217 ///
218 class IntRecTy : public RecTy {
219 static IntRecTy Shared;
220 IntRecTy() : RecTy(IntRecTyKind) {}
221
222 public:
223 static bool classof(const RecTy *RT) {
224 return RT->getRecTyKind() == IntRecTyKind;
225 }
226
227 static IntRecTy *get() { return &Shared; }
228
229 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
230 Init *convertValue( BitInit *BI) override;
231 Init *convertValue( BitsInit *BI) override;
232 Init *convertValue( IntInit *II) override { return (Init*)II; }
233 Init *convertValue(StringInit *SI) override { return nullptr; }
234 Init *convertValue( ListInit *LI) override { return nullptr; }
235 Init *convertValue(VarBitInit *VB) override { return nullptr; }
236 Init *convertValue( DefInit *DI) override { return nullptr; }
237 Init *convertValue( DagInit *DI) override { return nullptr; }
238 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
239 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
240 Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
241 Init *convertValue( TypedInit *TI) override;
242 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
243 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
244
245 std::string getAsString() const override { return "int"; }
246
247 bool typeIsConvertibleTo(const RecTy *RHS) const override {
248 return RHS->baseClassOf(this);
249 }
250
251 bool baseClassOf(const RecTy*) const override;
252 };
253
254 /// StringRecTy - 'string' - Represent an string value
255 ///
256 class StringRecTy : public RecTy {
257 static StringRecTy Shared;
258 StringRecTy() : RecTy(StringRecTyKind) {}
259
260 public:
261 static bool classof(const RecTy *RT) {
262 return RT->getRecTyKind() == StringRecTyKind;
263 }
264
265 static StringRecTy *get() { return &Shared; }
266
267 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
268 Init *convertValue( BitInit *BI) override { return nullptr; }
269 Init *convertValue( BitsInit *BI) override { return nullptr; }
270 Init *convertValue( IntInit *II) override { return nullptr; }
271 Init *convertValue(StringInit *SI) override { return (Init*)SI; }
272 Init *convertValue( ListInit *LI) override { return nullptr; }
273 Init *convertValue( UnOpInit *BO) override;
274 Init *convertValue( BinOpInit *BO) override;
275 Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
276
277 Init *convertValue(VarBitInit *VB) override { return nullptr; }
278 Init *convertValue( DefInit *DI) override { return nullptr; }
279 Init *convertValue( DagInit *DI) override { return nullptr; }
280 Init *convertValue( TypedInit *TI) override;
281 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
282 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
283
284 std::string getAsString() const override { return "string"; }
285
286 bool typeIsConvertibleTo(const RecTy *RHS) const override {
287 return RHS->baseClassOf(this);
288 }
289 };
290
291 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
292 /// the specified type.
293 ///
294 class ListRecTy : public RecTy {
295 RecTy *Ty;
296 explicit ListRecTy(RecTy *T) : RecTy(ListRecTyKind), Ty(T) {}
297 friend ListRecTy *RecTy::getListTy();
298
299 public:
300 static bool classof(const RecTy *RT) {
301 return RT->getRecTyKind() == ListRecTyKind;
302 }
303
304 static ListRecTy *get(RecTy *T) { return T->getListTy(); }
305 RecTy *getElementType() const { return Ty; }
306
307 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
308 Init *convertValue( BitInit *BI) override { return nullptr; }
309 Init *convertValue( BitsInit *BI) override { return nullptr; }
310 Init *convertValue( IntInit *II) override { return nullptr; }
311 Init *convertValue(StringInit *SI) override { return nullptr; }
312 Init *convertValue( ListInit *LI) override;
313 Init *convertValue(VarBitInit *VB) override { return nullptr; }
314 Init *convertValue( DefInit *DI) override { return nullptr; }
315 Init *convertValue( DagInit *DI) override { return nullptr; }
316 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
317 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
318 Init *convertValue(TernOpInit *UI) override { return RecTy::convertValue(UI);}
319 Init *convertValue( TypedInit *TI) override;
320 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
321 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
322
323 std::string getAsString() const override;
324
325 bool typeIsConvertibleTo(const RecTy *RHS) const override {
326 return RHS->baseClassOf(this);
327 }
328
329 bool baseClassOf(const RecTy*) const override;
330 };
331
332 /// DagRecTy - 'dag' - Represent a dag fragment
333 ///
334 class DagRecTy : public RecTy {
335 static DagRecTy Shared;
336 DagRecTy() : RecTy(DagRecTyKind) {}
337
338 public:
339 static bool classof(const RecTy *RT) {
340 return RT->getRecTyKind() == DagRecTyKind;
341 }
342
343 static DagRecTy *get() { return &Shared; }
344
345 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
346 Init *convertValue( BitInit *BI) override { return nullptr; }
347 Init *convertValue( BitsInit *BI) override { return nullptr; }
348 Init *convertValue( IntInit *II) override { return nullptr; }
349 Init *convertValue(StringInit *SI) override { return nullptr; }
350 Init *convertValue( ListInit *LI) override { return nullptr; }
351 Init *convertValue(VarBitInit *VB) override { return nullptr; }
352 Init *convertValue( DefInit *DI) override { return nullptr; }
353 Init *convertValue( UnOpInit *BO) override;
354 Init *convertValue( BinOpInit *BO) override;
355 Init *convertValue( TernOpInit *BO) override {return RecTy::convertValue(BO);}
356 Init *convertValue( DagInit *CI) override { return (Init*)CI; }
357 Init *convertValue( TypedInit *TI) override;
358 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
359 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
360
361 std::string getAsString() const override { return "dag"; }
362
363 bool typeIsConvertibleTo(const RecTy *RHS) const override {
364 return RHS->baseClassOf(this);
365 }
366 };
367
368 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
369 /// (R32 X = EAX).
370 ///
371 class RecordRecTy : public RecTy {
372 Record *Rec;
373 explicit RecordRecTy(Record *R) : RecTy(RecordRecTyKind), Rec(R) {}
374 friend class Record;
375
376 public:
377 static bool classof(const RecTy *RT) {
378 return RT->getRecTyKind() == RecordRecTyKind;
379 }
380
381 static RecordRecTy *get(Record *R);
382
383 Record *getRecord() const { return Rec; }
384
385 Init *convertValue( UnsetInit *UI) override { return (Init*)UI; }
386 Init *convertValue( BitInit *BI) override { return nullptr; }
387 Init *convertValue( BitsInit *BI) override { return nullptr; }
388 Init *convertValue( IntInit *II) override { return nullptr; }
389 Init *convertValue(StringInit *SI) override { return nullptr; }
390 Init *convertValue( ListInit *LI) override { return nullptr; }
391 Init *convertValue(VarBitInit *VB) override { return nullptr; }
392 Init *convertValue( UnOpInit *UI) override { return RecTy::convertValue(UI);}
393 Init *convertValue( BinOpInit *UI) override { return RecTy::convertValue(UI);}
394 Init *convertValue( TernOpInit *UI) override {return RecTy::convertValue(UI);}
395 Init *convertValue( DefInit *DI) override;
396 Init *convertValue( DagInit *DI) override { return nullptr; }
397 Init *convertValue( TypedInit *VI) override;
398 Init *convertValue( VarInit *VI) override { return RecTy::convertValue(VI);}
399 Init *convertValue( FieldInit *FI) override { return RecTy::convertValue(FI);}
400
401 std::string getAsString() const override;
402
403 bool typeIsConvertibleTo(const RecTy *RHS) const override {
404 return RHS->baseClassOf(this);
405 }
406 bool baseClassOf(const RecTy*) const override;
407 };
408
409 /// resolveTypes - Find a common type that T1 and T2 convert to.
410 /// Return 0 if no such type exists.
411 ///
412 RecTy *resolveTypes(RecTy *T1, RecTy *T2);
413
414 //===----------------------------------------------------------------------===//
415 // Initializer Classes
416 //===----------------------------------------------------------------------===//
417
418 class Init {
419 protected:
420 /// \brief Discriminator enum (for isa<>, dyn_cast<>, et al.)
421 ///
422 /// This enum is laid out by a preorder traversal of the inheritance
423 /// hierarchy, and does not contain an entry for abstract classes, as per
424 /// the recommendation in docs/HowToSetUpLLVMStyleRTTI.rst.
425 ///
426 /// We also explicitly include "first" and "last" values for each
427 /// interior node of the inheritance tree, to make it easier to read the
428 /// corresponding classof().
429 ///
430 /// We could pack these a bit tighter by not having the IK_FirstXXXInit
431 /// and IK_LastXXXInit be their own values, but that would degrade
432 /// readability for really no benefit.
433 enum InitKind {
434 IK_BitInit,
435 IK_FirstTypedInit,
436 IK_BitsInit,
437 IK_DagInit,
438 IK_DefInit,
439 IK_FieldInit,
440 IK_IntInit,
441 IK_ListInit,
442 IK_FirstOpInit,
443 IK_BinOpInit,
444 IK_TernOpInit,
445 IK_UnOpInit,
446 IK_LastOpInit,
447 IK_StringInit,
448 IK_VarInit,
449 IK_VarListElementInit,
450 IK_LastTypedInit,
451 IK_UnsetInit,
452 IK_VarBitInit
453 };
454
455 private:
456 const InitKind Kind;
457 Init(const Init &) LLVM_DELETED_FUNCTION;
458 Init &operator=(const Init &) LLVM_DELETED_FUNCTION;
459 virtual void anchor();
460
461 public:
462 InitKind getKind() const { return Kind; }
463
464 protected:
465 explicit Init(InitKind K) : Kind(K) {}
466
467 public:
468 virtual ~Init() {}
469
470 /// isComplete - This virtual method should be overridden by values that may
471 /// not be completely specified yet.
472 virtual bool isComplete() const { return true; }
473
474 /// print - Print out this value.
475 void print(raw_ostream &OS) const { OS << getAsString(); }
476
477 /// getAsString - Convert this value to a string form.
478 virtual std::string getAsString() const = 0;
479 /// getAsUnquotedString - Convert this value to a string form,
480 /// without adding quote markers. This primaruly affects
481 /// StringInits where we will not surround the string value with
482 /// quotes.
483 virtual std::string getAsUnquotedString() const { return getAsString(); }
484
485 /// dump - Debugging method that may be called through a debugger, just
486 /// invokes print on stderr.
487 void dump() const;
488
489 /// convertInitializerTo - This virtual function is a simple call-back
490 /// function that should be overridden to call the appropriate
491 /// RecTy::convertValue method.
492 ///
493 virtual Init *convertInitializerTo(RecTy *Ty) const = 0;
494
495 /// convertInitializerBitRange - This method is used to implement the bitrange
496 /// selection operator. Given an initializer, it selects the specified bits
497 /// out, returning them as a new init of bits type. If it is not legal to use
498 /// the bit subscript operator on this initializer, return null.
499 ///
500 virtual Init *
501 convertInitializerBitRange(const std::vector<unsigned> &Bits) const {
502 return nullptr;
503 }
504
505 /// convertInitListSlice - This method is used to implement the list slice
506 /// selection operator. Given an initializer, it selects the specified list
507 /// elements, returning them as a new init of list type. If it is not legal
508 /// to take a slice of this, return null.
509 ///
510 virtual Init *
511 convertInitListSlice(const std::vector<unsigned> &Elements) const {
512 return nullptr;
513 }
514
515 /// getFieldType - This method is used to implement the FieldInit class.
516 /// Implementors of this method should return the type of the named field if
517 /// they are of record type.
518 ///
519 virtual RecTy *getFieldType(const std::string &FieldName) const {
520 return nullptr;
521 }
522
523 /// getFieldInit - This method complements getFieldType to return the
524 /// initializer for the specified field. If getFieldType returns non-null
525 /// this method should return non-null, otherwise it returns null.
526 ///
527 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
528 const std::string &FieldName) const {
529 return nullptr;
530 }
531
532 /// resolveReferences - This method is used by classes that refer to other
533 /// variables which may not be defined at the time the expression is formed.
534 /// If a value is set for the variable later, this method will be called on
535 /// users of the value to allow the value to propagate out.
536 ///
537 virtual Init *resolveReferences(Record &R, const RecordVal *RV) const {
538 return const_cast<Init *>(this);
539 }
540
541 /// getBit - This method is used to return the initializer for the specified
542 /// bit.
543 virtual Init *getBit(unsigned Bit) const = 0;
544
545 /// getBitVar - This method is used to retrieve the initializer for bit
546 /// reference. For non-VarBitInit, it simply returns itself.
547 virtual Init *getBitVar() const { return const_cast<Init*>(this); }
548
549 /// getBitNum - This method is used to retrieve the bit number of a bit
550 /// reference. For non-VarBitInit, it simply returns 0.
551 virtual unsigned getBitNum() const { return 0; }
552 };
553
554 inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
555 I.print(OS); return OS;
556 }
557
558 /// TypedInit - This is the common super-class of types that have a specific,
559 /// explicit, type.
560 ///
561 class TypedInit : public Init {
562 RecTy *Ty;
563
564 TypedInit(const TypedInit &Other) LLVM_DELETED_FUNCTION;
565 TypedInit &operator=(const TypedInit &Other) LLVM_DELETED_FUNCTION;
566
567 protected:
568 explicit TypedInit(InitKind K, RecTy *T) : Init(K), Ty(T) {}
569
570 public:
571 static bool classof(const Init *I) {
572 return I->getKind() >= IK_FirstTypedInit &&
573 I->getKind() <= IK_LastTypedInit;
574 }
575 RecTy *getType() const { return Ty; }
576
577 Init *
578 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
579 Init *
580 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
581
582 /// getFieldType - This method is used to implement the FieldInit class.
583 /// Implementors of this method should return the type of the named field if
584 /// they are of record type.
585 ///
586 RecTy *getFieldType(const std::string &FieldName) const override;
587
588 /// resolveListElementReference - This method is used to implement
589 /// VarListElementInit::resolveReferences. If the list element is resolvable
590 /// now, we return the resolved value, otherwise we return null.
591 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
592 unsigned Elt) const = 0;
593 };
594
595 /// UnsetInit - ? - Represents an uninitialized value
596 ///
597 class UnsetInit : public Init {
598 UnsetInit() : Init(IK_UnsetInit) {}
599 UnsetInit(const UnsetInit &) LLVM_DELETED_FUNCTION;
600 UnsetInit &operator=(const UnsetInit &Other) LLVM_DELETED_FUNCTION;
601 void anchor() override;
602
603 public:
604 static bool classof(const Init *I) {
605 return I->getKind() == IK_UnsetInit;
606 }
607 static UnsetInit *get();
608
609 Init *convertInitializerTo(RecTy *Ty) const override {
610 return Ty->convertValue(const_cast<UnsetInit *>(this));
611 }
612
613 Init *getBit(unsigned Bit) const override {
614 return const_cast<UnsetInit*>(this);
615 }
616
617 bool isComplete() const override { return false; }
618 std::string getAsString() const override { return "?"; }
619 };
620
621 /// BitInit - true/false - Represent a concrete initializer for a bit.
622 ///
623 class BitInit : public Init {
624 bool Value;
625
626 explicit BitInit(bool V) : Init(IK_BitInit), Value(V) {}
627 BitInit(const BitInit &Other) LLVM_DELETED_FUNCTION;
628 BitInit &operator=(BitInit &Other) LLVM_DELETED_FUNCTION;
629 void anchor() override;
630
631 public:
632 static bool classof(const Init *I) {
633 return I->getKind() == IK_BitInit;
634 }
635 static BitInit *get(bool V);
636
637 bool getValue() const { return Value; }
638
639 Init *convertInitializerTo(RecTy *Ty) const override {
640 return Ty->convertValue(const_cast<BitInit *>(this));
641 }
642
643 Init *getBit(unsigned Bit) const override {
644 assert(Bit < 1 && "Bit index out of range!");
645 return const_cast<BitInit*>(this);
646 }
647
648 std::string getAsString() const override { return Value ? "1" : "0"; }
649 };
650
651 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
652 /// It contains a vector of bits, whose size is determined by the type.
653 ///
654 class BitsInit : public TypedInit, public FoldingSetNode {
655 std::vector<Init*> Bits;
656
657 BitsInit(ArrayRef<Init *> Range)
658 : TypedInit(IK_BitsInit, BitsRecTy::get(Range.size())),
659 Bits(Range.begin(), Range.end()) {}
660
661 BitsInit(const BitsInit &Other) LLVM_DELETED_FUNCTION;
662 BitsInit &operator=(const BitsInit &Other) LLVM_DELETED_FUNCTION;
663
664 public:
665 static bool classof(const Init *I) {
666 return I->getKind() == IK_BitsInit;
667 }
668 static BitsInit *get(ArrayRef<Init *> Range);
669
670 void Profile(FoldingSetNodeID &ID) const;
671
672 unsigned getNumBits() const { return Bits.size(); }
673
674 Init *convertInitializerTo(RecTy *Ty) const override {
675 return Ty->convertValue(const_cast<BitsInit *>(this));
676 }
677 Init *
678 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
679
680 bool isComplete() const override {
681 for (unsigned i = 0; i != getNumBits(); ++i)
682 if (!getBit(i)->isComplete()) return false;
683 return true;
684 }
685 bool allInComplete() const {
686 for (unsigned i = 0; i != getNumBits(); ++i)
687 if (getBit(i)->isComplete()) return false;
688 return true;
689 }
690 std::string getAsString() const override;
691
692 /// resolveListElementReference - This method is used to implement
693 /// VarListElementInit::resolveReferences. If the list element is resolvable
694 /// now, we return the resolved value, otherwise we return null.
695 Init *resolveListElementReference(Record &R, const RecordVal *RV,
696 unsigned Elt) const override {
697 llvm_unreachable("Illegal element reference off bits<n>");
698 }
699
700 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
701
702 Init *getBit(unsigned Bit) const override {
703 assert(Bit < Bits.size() && "Bit index out of range!");
704 return Bits[Bit];
705 }
706 };
707
708 /// IntInit - 7 - Represent an initialization by a literal integer value.
709 ///
710 class IntInit : public TypedInit {
711 int64_t Value;
712
713 explicit IntInit(int64_t V)
714 : TypedInit(IK_IntInit, IntRecTy::get()), Value(V) {}
715
716 IntInit(const IntInit &Other) LLVM_DELETED_FUNCTION;
717 IntInit &operator=(const IntInit &Other) LLVM_DELETED_FUNCTION;
718
719 public:
720 static bool classof(const Init *I) {
721 return I->getKind() == IK_IntInit;
722 }
723 static IntInit *get(int64_t V);
724
725 int64_t getValue() const { return Value; }
726
727 Init *convertInitializerTo(RecTy *Ty) const override {
728 return Ty->convertValue(const_cast<IntInit *>(this));
729 }
730 Init *
731 convertInitializerBitRange(const std::vector<unsigned> &Bits) const override;
732
733 std::string getAsString() const override;
734
735 /// resolveListElementReference - This method is used to implement
736 /// VarListElementInit::resolveReferences. If the list element is resolvable
737 /// now, we return the resolved value, otherwise we return null.
738 Init *resolveListElementReference(Record &R, const RecordVal *RV,
739 unsigned Elt) const override {
740 llvm_unreachable("Illegal element reference off int");
741 }
742
743 Init *getBit(unsigned Bit) const override {
744 return BitInit::get((Value & (1ULL << Bit)) != 0);
745 }
746 };
747
748 /// StringInit - "foo" - Represent an initialization by a string value.
749 ///
750 class StringInit : public TypedInit {
751 std::string Value;
752
753 explicit StringInit(const std::string &V)
754 : TypedInit(IK_StringInit, StringRecTy::get()), Value(V) {}
755
756 StringInit(const StringInit &Other) LLVM_DELETED_FUNCTION;
757 StringInit &operator=(const StringInit &Other) LLVM_DELETED_FUNCTION;
758 void anchor() override;
759
760 public:
761 static bool classof(const Init *I) {
762 return I->getKind() == IK_StringInit;
763 }
764 static StringInit *get(StringRef);
765
766 const std::string &getValue() const { return Value; }
767
768 Init *convertInitializerTo(RecTy *Ty) const override {
769 return Ty->convertValue(const_cast<StringInit *>(this));
770 }
771
772 std::string getAsString() const override { return "\"" + Value + "\""; }
773 std::string getAsUnquotedString() const override { return Value; }
774
775 /// resolveListElementReference - This method is used to implement
776 /// VarListElementInit::resolveReferences. If the list element is resolvable
777 /// now, we return the resolved value, otherwise we return null.
778 Init *resolveListElementReference(Record &R, const RecordVal *RV,
779 unsigned Elt) const override {
780 llvm_unreachable("Illegal element reference off string");
781 }
782
783 Init *getBit(unsigned Bit) const override {
784 llvm_unreachable("Illegal bit reference off string");
785 }
786 };
787
788 /// ListInit - [AL, AH, CL] - Represent a list of defs
789 ///
790 class ListInit : public TypedInit, public FoldingSetNode {
791 std::vector<Init*> Values;
792
793 public:
794 typedef std::vector<Init*>::const_iterator const_iterator;
795
796 private:
797 explicit ListInit(ArrayRef<Init *> Range, RecTy *EltTy)
798 : TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
799 Values(Range.begin(), Range.end()) {}
800
801 ListInit(const ListInit &Other) LLVM_DELETED_FUNCTION;
802 ListInit &operator=(const ListInit &Other) LLVM_DELETED_FUNCTION;
803
804 public:
805 static bool classof(const Init *I) {
806 return I->getKind() == IK_ListInit;
807 }
808 static ListInit *get(ArrayRef<Init *> Range, RecTy *EltTy);
809
810 void Profile(FoldingSetNodeID &ID) const;
811
812 unsigned getSize() const { return Values.size(); }
813 Init *getElement(unsigned i) const {
814 assert(i < Values.size() && "List element index out of range!");
815 return Values[i];
816 }
817
818 Record *getElementAsRecord(unsigned i) const;
819
820 Init *
821 convertInitListSlice(const std::vector<unsigned> &Elements) const override;
822
823 Init *convertInitializerTo(RecTy *Ty) const override {
824 return Ty->convertValue(const_cast<ListInit *>(this));
825 }
826
827 /// resolveReferences - This method is used by classes that refer to other
828 /// variables which may not be defined at the time they expression is formed.
829 /// If a value is set for the variable later, this method will be called on
830 /// users of the value to allow the value to propagate out.
831 ///
832 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
833
834 std::string getAsString() const override;
835
836 ArrayRef<Init*> getValues() const { return Values; }
837
838 inline const_iterator begin() const { return Values.begin(); }
839 inline const_iterator end () const { return Values.end(); }
840
841 inline size_t size () const { return Values.size(); }
842 inline bool empty() const { return Values.empty(); }
843
844 /// resolveListElementReference - This method is used to implement
845 /// VarListElementInit::resolveReferences. If the list element is resolvable
846 /// now, we return the resolved value, otherwise we return null.
847 Init *resolveListElementReference(Record &R, const RecordVal *RV,
848 unsigned Elt) const override;
849
850 Init *getBit(unsigned Bit) const override {
851 llvm_unreachable("Illegal bit reference off list");
852 }
853 };
854
855 /// OpInit - Base class for operators
856 ///
857 class OpInit : public TypedInit {
858 OpInit(const OpInit &Other) LLVM_DELETED_FUNCTION;
859 OpInit &operator=(OpInit &Other) LLVM_DELETED_FUNCTION;
860
861 protected:
862 explicit OpInit(InitKind K, RecTy *Type) : TypedInit(K, Type) {}
863
864 public:
865 static bool classof(const Init *I) {
866 return I->getKind() >= IK_FirstOpInit &&
867 I->getKind() <= IK_LastOpInit;
868 }
869 // Clone - Clone this operator, replacing arguments with the new list
870 virtual OpInit *clone(std::vector<Init *> &Operands) const = 0;
871
872 virtual int getNumOperands() const = 0;
873 virtual Init *getOperand(int i) const = 0;
874
875 // Fold - If possible, fold this to a simpler init. Return this if not
876 // possible to fold.
877 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const = 0;
878
879 Init *convertInitializerTo(RecTy *Ty) const override {
880 return Ty->convertValue(const_cast<OpInit *>(this));
881 }
882
883 Init *resolveListElementReference(Record &R, const RecordVal *RV,
884 unsigned Elt) const override;
885
886 Init *getBit(unsigned Bit) const override;
887 };
888
889 /// UnOpInit - !op (X) - Transform an init.
890 ///
891 class UnOpInit : public OpInit {
892 public:
893 enum UnaryOp { CAST, HEAD, TAIL, EMPTY };
894
895 private:
896 UnaryOp Opc;
897 Init *LHS;
898
899 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type)
900 : OpInit(IK_UnOpInit, Type), Opc(opc), LHS(lhs) {}
901
902 UnOpInit(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
903 UnOpInit &operator=(const UnOpInit &Other) LLVM_DELETED_FUNCTION;
904
905 public:
906 static bool classof(const Init *I) {
907 return I->getKind() == IK_UnOpInit;
908 }
909 static UnOpInit *get(UnaryOp opc, Init *lhs, RecTy *Type);
910
911 // Clone - Clone this operator, replacing arguments with the new list
912 OpInit *clone(std::vector<Init *> &Operands) const override {
913 assert(Operands.size() == 1 &&
914 "Wrong number of operands for unary operation");
915 return UnOpInit::get(getOpcode(), *Operands.begin(), getType());
916 }
917
918 int getNumOperands() const override { return 1; }
919 Init *getOperand(int i) const override {
920 assert(i == 0 && "Invalid operand id for unary operator");
921 return getOperand();
922 }
923
924 UnaryOp getOpcode() const { return Opc; }
925 Init *getOperand() const { return LHS; }
926
927 // Fold - If possible, fold this to a simpler init. Return this if not
928 // possible to fold.
929 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
930
931 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
932
933 std::string getAsString() const override;
934 };
935
936 /// BinOpInit - !op (X, Y) - Combine two inits.
937 ///
938 class BinOpInit : public OpInit {
939 public:
940 enum BinaryOp { ADD, AND, SHL, SRA, SRL, LISTCONCAT, STRCONCAT, CONCAT, EQ };
941
942 private:
943 BinaryOp Opc;
944 Init *LHS, *RHS;
945
946 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
947 OpInit(IK_BinOpInit, Type), Opc(opc), LHS(lhs), RHS(rhs) {}
948
949 BinOpInit(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
950 BinOpInit &operator=(const BinOpInit &Other) LLVM_DELETED_FUNCTION;
951
952 public:
953 static bool classof(const Init *I) {
954 return I->getKind() == IK_BinOpInit;
955 }
956 static BinOpInit *get(BinaryOp opc, Init *lhs, Init *rhs,
957 RecTy *Type);
958
959 // Clone - Clone this operator, replacing arguments with the new list
960 OpInit *clone(std::vector<Init *> &Operands) const override {
961 assert(Operands.size() == 2 &&
962 "Wrong number of operands for binary operation");
963 return BinOpInit::get(getOpcode(), Operands[0], Operands[1], getType());
964 }
965
966 int getNumOperands() const override { return 2; }
967 Init *getOperand(int i) const override {
968 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
969 if (i == 0) {
970 return getLHS();
971 } else {
972 return getRHS();
973 }
974 }
975
976 BinaryOp getOpcode() const { return Opc; }
977 Init *getLHS() const { return LHS; }
978 Init *getRHS() const { return RHS; }
979
980 // Fold - If possible, fold this to a simpler init. Return this if not
981 // possible to fold.
982 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
983
984 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
985
986 std::string getAsString() const override;
987 };
988
989 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
990 ///
991 class TernOpInit : public OpInit {
992 public:
993 enum TernaryOp { SUBST, FOREACH, IF };
994
995 private:
996 TernaryOp Opc;
997 Init *LHS, *MHS, *RHS;
998
999 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs,
1000 RecTy *Type) :
1001 OpInit(IK_TernOpInit, Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {}
1002
1003 TernOpInit(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1004 TernOpInit &operator=(const TernOpInit &Other) LLVM_DELETED_FUNCTION;
1005
1006 public:
1007 static bool classof(const Init *I) {
1008 return I->getKind() == IK_TernOpInit;
1009 }
1010 static TernOpInit *get(TernaryOp opc, Init *lhs,
1011 Init *mhs, Init *rhs,
1012 RecTy *Type);
1013
1014 // Clone - Clone this operator, replacing arguments with the new list
1015 OpInit *clone(std::vector<Init *> &Operands) const override {
1016 assert(Operands.size() == 3 &&
1017 "Wrong number of operands for ternary operation");
1018 return TernOpInit::get(getOpcode(), Operands[0], Operands[1], Operands[2],
1019 getType());
1020 }
1021
1022 int getNumOperands() const override { return 3; }
1023 Init *getOperand(int i) const override {
1024 assert((i == 0 || i == 1 || i == 2) &&
1025 "Invalid operand id for ternary operator");
1026 if (i == 0) {
1027 return getLHS();
1028 } else if (i == 1) {
1029 return getMHS();
1030 } else {
1031 return getRHS();
1032 }
1033 }
1034
1035 TernaryOp getOpcode() const { return Opc; }
1036 Init *getLHS() const { return LHS; }
1037 Init *getMHS() const { return MHS; }
1038 Init *getRHS() const { return RHS; }
1039
1040 // Fold - If possible, fold this to a simpler init. Return this if not
1041 // possible to fold.
1042 Init *Fold(Record *CurRec, MultiClass *CurMultiClass) const override;
1043
1044 bool isComplete() const override { return false; }
1045
1046 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1047
1048 std::string getAsString() const override;
1049 };
1050
1051 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
1052 ///
1053 class VarInit : public TypedInit {
1054 Init *VarName;
1055
1056 explicit VarInit(const std::string &VN, RecTy *T)
1057 : TypedInit(IK_VarInit, T), VarName(StringInit::get(VN)) {}
1058 explicit VarInit(Init *VN, RecTy *T)
1059 : TypedInit(IK_VarInit, T), VarName(VN) {}
1060
1061 VarInit(const VarInit &Other) LLVM_DELETED_FUNCTION;
1062 VarInit &operator=(const VarInit &Other) LLVM_DELETED_FUNCTION;
1063
1064 public:
1065 static bool classof(const Init *I) {
1066 return I->getKind() == IK_VarInit;
1067 }
1068 static VarInit *get(const std::string &VN, RecTy *T);
1069 static VarInit *get(Init *VN, RecTy *T);
1070
1071 Init *convertInitializerTo(RecTy *Ty) const override {
1072 return Ty->convertValue(const_cast<VarInit *>(this));
1073 }
1074
1075 const std::string &getName() const;
1076 Init *getNameInit() const { return VarName; }
1077 std::string getNameInitAsString() const {
1078 return getNameInit()->getAsUnquotedString();
1079 }
1080
1081 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1082 unsigned Elt) const override;
1083
1084 RecTy *getFieldType(const std::string &FieldName) const override;
1085 Init *getFieldInit(Record &R, const RecordVal *RV,
1086 const std::string &FieldName) const override;
1087
1088 /// resolveReferences - This method is used by classes that refer to other
1089 /// variables which may not be defined at the time they expression is formed.
1090 /// If a value is set for the variable later, this method will be called on
1091 /// users of the value to allow the value to propagate out.
1092 ///
1093 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1094
1095 Init *getBit(unsigned Bit) const override;
1096
1097 std::string getAsString() const override { return getName(); }
1098 };
1099
1100 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
1101 ///
1102 class VarBitInit : public Init {
1103 TypedInit *TI;
1104 unsigned Bit;
1105
1106 VarBitInit(TypedInit *T, unsigned B) : Init(IK_VarBitInit), TI(T), Bit(B) {
1107 assert(T->getType() &&
1108 (isa<IntRecTy>(T->getType()) ||
1109 (isa<BitsRecTy>(T->getType()) &&
1110 cast<BitsRecTy>(T->getType())->getNumBits() > B)) &&
1111 "Illegal VarBitInit expression!");
1112 }
1113
1114 VarBitInit(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1115 VarBitInit &operator=(const VarBitInit &Other) LLVM_DELETED_FUNCTION;
1116
1117 public:
1118 static bool classof(const Init *I) {
1119 return I->getKind() == IK_VarBitInit;
1120 }
1121 static VarBitInit *get(TypedInit *T, unsigned B);
1122
1123 Init *convertInitializerTo(RecTy *Ty) const override {
1124 return Ty->convertValue(const_cast<VarBitInit *>(this));
1125 }
1126
1127 Init *getBitVar() const override { return TI; }
1128 unsigned getBitNum() const override { return Bit; }
1129
1130 std::string getAsString() const override;
1131 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1132
1133 Init *getBit(unsigned B) const override {
1134 assert(B < 1 && "Bit index out of range!");
1135 return const_cast<VarBitInit*>(this);
1136 }
1137 };
1138
1139 /// VarListElementInit - List[4] - Represent access to one element of a var or
1140 /// field.
1141 class VarListElementInit : public TypedInit {
1142 TypedInit *TI;
1143 unsigned Element;
1144
1145 VarListElementInit(TypedInit *T, unsigned E)
1146 : TypedInit(IK_VarListElementInit,
1147 cast<ListRecTy>(T->getType())->getElementType()),
1148 TI(T), Element(E) {
1149 assert(T->getType() && isa<ListRecTy>(T->getType()) &&
1150 "Illegal VarBitInit expression!");
1151 }
1152
1153 VarListElementInit(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1154 void operator=(const VarListElementInit &Other) LLVM_DELETED_FUNCTION;
1155
1156 public:
1157 static bool classof(const Init *I) {
1158 return I->getKind() == IK_VarListElementInit;
1159 }
1160 static VarListElementInit *get(TypedInit *T, unsigned E);
1161
1162 Init *convertInitializerTo(RecTy *Ty) const override {
1163 return Ty->convertValue(const_cast<VarListElementInit *>(this));
1164 }
1165
1166 TypedInit *getVariable() const { return TI; }
1167 unsigned getElementNum() const { return Element; }
1168
1169 /// resolveListElementReference - This method is used to implement
1170 /// VarListElementInit::resolveReferences. If the list element is resolvable
1171 /// now, we return the resolved value, otherwise we return null.
1172 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1173 unsigned Elt) const override;
1174
1175 std::string getAsString() const override;
1176 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1177
1178 Init *getBit(unsigned Bit) const override;
1179 };
1180
1181 /// DefInit - AL - Represent a reference to a 'def' in the description
1182 ///
1183 class DefInit : public TypedInit {
1184 Record *Def;
1185
1186 DefInit(Record *D, RecordRecTy *T) : TypedInit(IK_DefInit, T), Def(D) {}
1187 friend class Record;
1188
1189 DefInit(const DefInit &Other) LLVM_DELETED_FUNCTION;
1190 DefInit &operator=(const DefInit &Other) LLVM_DELETED_FUNCTION;
1191
1192 public:
1193 static bool classof(const Init *I) {
1194 return I->getKind() == IK_DefInit;
1195 }
1196 static DefInit *get(Record*);
1197
1198 Init *convertInitializerTo(RecTy *Ty) const override {
1199 return Ty->convertValue(const_cast<DefInit *>(this));
1200 }
1201
1202 Record *getDef() const { return Def; }
1203
1204 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1205
1206 RecTy *getFieldType(const std::string &FieldName) const override;
1207 Init *getFieldInit(Record &R, const RecordVal *RV,
1208 const std::string &FieldName) const override;
1209
1210 std::string getAsString() const override;
1211
1212 Init *getBit(unsigned Bit) const override {
1213 llvm_unreachable("Illegal bit reference off def");
1214 }
1215
1216 /// resolveListElementReference - This method is used to implement
1217 /// VarListElementInit::resolveReferences. If the list element is resolvable
1218 /// now, we return the resolved value, otherwise we return null.
1219 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1220 unsigned Elt) const override {
1221 llvm_unreachable("Illegal element reference off def");
1222 }
1223 };
1224
1225 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1226 ///
1227 class FieldInit : public TypedInit {
1228 Init *Rec; // Record we are referring to
1229 std::string FieldName; // Field we are accessing
1230
1231 FieldInit(Init *R, const std::string &FN)
1232 : TypedInit(IK_FieldInit, R->getFieldType(FN)), Rec(R), FieldName(FN) {
1233 assert(getType() && "FieldInit with non-record type!");
1234 }
1235
1236 FieldInit(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1237 FieldInit &operator=(const FieldInit &Other) LLVM_DELETED_FUNCTION;
1238
1239 public:
1240 static bool classof(const Init *I) {
1241 return I->getKind() == IK_FieldInit;
1242 }
1243 static FieldInit *get(Init *R, const std::string &FN);
1244 static FieldInit *get(Init *R, const Init *FN);
1245
1246 Init *convertInitializerTo(RecTy *Ty) const override {
1247 return Ty->convertValue(const_cast<FieldInit *>(this));
1248 }
1249
1250 Init *getBit(unsigned Bit) const override;
1251
1252 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1253 unsigned Elt) const override;
1254
1255 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1256
1257 std::string getAsString() const override {
1258 return Rec->getAsString() + "." + FieldName;
1259 }
1260 };
1261
1262 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1263 /// to have at least one value then a (possibly empty) list of arguments. Each
1264 /// argument can have a name associated with it.
1265 ///
1266 class DagInit : public TypedInit, public FoldingSetNode {
1267 Init *Val;
1268 std::string ValName;
1269 std::vector<Init*> Args;
1270 std::vector<std::string> ArgNames;
1271
1272 DagInit(Init *V, const std::string &VN,
1273 ArrayRef<Init *> ArgRange,
1274 ArrayRef<std::string> NameRange)
1275 : TypedInit(IK_DagInit, DagRecTy::get()), Val(V), ValName(VN),
1276 Args(ArgRange.begin(), ArgRange.end()),
1277 ArgNames(NameRange.begin(), NameRange.end()) {}
1278
1279 DagInit(const DagInit &Other) LLVM_DELETED_FUNCTION;
1280 DagInit &operator=(const DagInit &Other) LLVM_DELETED_FUNCTION;
1281
1282 public:
1283 static bool classof(const Init *I) {
1284 return I->getKind() == IK_DagInit;
1285 }
1286 static DagInit *get(Init *V, const std::string &VN,
1287 ArrayRef<Init *> ArgRange,
1288 ArrayRef<std::string> NameRange);
1289 static DagInit *get(Init *V, const std::string &VN,
1290 const std::vector<
1291 std::pair<Init*, std::string> > &args);
1292
1293 void Profile(FoldingSetNodeID &ID) const;
1294
1295 Init *convertInitializerTo(RecTy *Ty) const override {
1296 return Ty->convertValue(const_cast<DagInit *>(this));
1297 }
1298
1299 Init *getOperator() const { return Val; }
1300
1301 const std::string &getName() const { return ValName; }
1302
1303 unsigned getNumArgs() const { return Args.size(); }
1304 Init *getArg(unsigned Num) const {
1305 assert(Num < Args.size() && "Arg number out of range!");
1306 return Args[Num];
1307 }
1308 const std::string &getArgName(unsigned Num) const {
1309 assert(Num < ArgNames.size() && "Arg number out of range!");
1310 return ArgNames[Num];
1311 }
1312
1313 Init *resolveReferences(Record &R, const RecordVal *RV) const override;
1314
1315 std::string getAsString() const override;
1316
1317 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1318 typedef std::vector<std::string>::const_iterator const_name_iterator;
1319
1320 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1321 inline const_arg_iterator arg_end () const { return Args.end(); }
1322
1323 inline size_t arg_size () const { return Args.size(); }
1324 inline bool arg_empty() const { return Args.empty(); }
1325
1326 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1327 inline const_name_iterator name_end () const { return ArgNames.end(); }
1328
1329 inline size_t name_size () const { return ArgNames.size(); }
1330 inline bool name_empty() const { return ArgNames.empty(); }
1331
1332 Init *getBit(unsigned Bit) const override {
1333 llvm_unreachable("Illegal bit reference off dag");
1334 }
1335
1336 Init *resolveListElementReference(Record &R, const RecordVal *RV,
1337 unsigned Elt) const override {
1338 llvm_unreachable("Illegal element reference off dag");
1339 }
1340 };
1341
1342 //===----------------------------------------------------------------------===//
1343 // High-Level Classes
1344 //===----------------------------------------------------------------------===//
1345
1346 class RecordVal {
1347 Init *Name;
1348 RecTy *Ty;
1349 unsigned Prefix;
1350 Init *Value;
1351
1352 public:
1353 RecordVal(Init *N, RecTy *T, unsigned P);
1354 RecordVal(const std::string &N, RecTy *T, unsigned P);
1355
1356 const std::string &getName() const;
1357 const Init *getNameInit() const { return Name; }
1358 std::string getNameInitAsString() const {
1359 return getNameInit()->getAsUnquotedString();
1360 }
1361
1362 unsigned getPrefix() const { return Prefix; }
1363 RecTy *getType() const { return Ty; }
1364 Init *getValue() const { return Value; }
1365
1366 bool setValue(Init *V) {
1367 if (V) {
1368 Value = V->convertInitializerTo(Ty);
1369 return Value == nullptr;
1370 }
1371 Value = nullptr;
1372 return false;
1373 }
1374
1375 void dump() const;
1376 void print(raw_ostream &OS, bool PrintSem = true) const;
1377 };
1378
1379 inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
1380 RV.print(OS << " ");
1381 return OS;
1382 }
1383
1384 class Record {
1385 static unsigned LastID;
1386
1387 // Unique record ID.
1388 unsigned ID;
1389 Init *Name;
1390 // Location where record was instantiated, followed by the location of
1391 // multiclass prototypes used.
1392 SmallVector<SMLoc, 4> Locs;
1393 std::vector<Init *> TemplateArgs;
1394 std::vector<RecordVal> Values;
1395 std::vector<Record *> SuperClasses;
1396 std::vector<SMRange> SuperClassRanges;
1397
1398 // Tracks Record instances. Not owned by Record.
1399 RecordKeeper &TrackedRecords;
1400
1401 DefInit *TheInit;
1402 bool IsAnonymous;
1403
1404 // Class-instance values can be used by other defs. For example, Struct<i>
1405 // is used here as a template argument to another class:
1406 //
1407 // multiclass MultiClass<int i> {
1408 // def Def : Class<Struct<i>>;
1409 //
1410 // These need to get fully resolved before instantiating any other
1411 // definitions that usie them (e.g. Def). However, inside a multiclass they
1412 // can't be immediately resolved so we mark them ResolveFirst to fully
1413 // resolve them later as soon as the multiclass is instantiated.
1414 bool ResolveFirst;
1415
1416 void init();
1417 void checkName();
1418
1419 public:
1420 // Constructs a record.
1421 explicit Record(const std::string &N, ArrayRef<SMLoc> locs,
1422 RecordKeeper &records, bool Anonymous = false) :
1423 ID(LastID++), Name(StringInit::get(N)), Locs(locs.begin(), locs.end()),
1424 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1425 ResolveFirst(false) {
1426 init();
1427 }
1428 explicit Record(Init *N, ArrayRef<SMLoc> locs, RecordKeeper &records,
1429 bool Anonymous = false) :
1430 ID(LastID++), Name(N), Locs(locs.begin(), locs.end()),
1431 TrackedRecords(records), TheInit(nullptr), IsAnonymous(Anonymous),
1432 ResolveFirst(false) {
1433 init();
1434 }
1435
1436 // When copy-constructing a Record, we must still guarantee a globally unique
1437 // ID number. All other fields can be copied normally.
1438 Record(const Record &O) :
1439 ID(LastID++), Name(O.Name), Locs(O.Locs), TemplateArgs(O.TemplateArgs),
1440 Values(O.Values), SuperClasses(O.SuperClasses),
1441 SuperClassRanges(O.SuperClassRanges), TrackedRecords(O.TrackedRecords),
1442 TheInit(O.TheInit), IsAnonymous(O.IsAnonymous),
1443 ResolveFirst(O.ResolveFirst) { }
1444
1445 ~Record() {}
1446
1447 static unsigned getNewUID() { return LastID++; }
1448
1449 unsigned getID() const { return ID; }
1450
1451 const std::string &getName() const;
1452 Init *getNameInit() const {
1453 return Name;
1454 }
1455 const std::string getNameInitAsString() const {
1456 return getNameInit()->getAsUnquotedString();
1457 }
1458
1459 void setName(Init *Name); // Also updates RecordKeeper.
1460 void setName(const std::string &Name); // Also updates RecordKeeper.
1461
1462 ArrayRef<SMLoc> getLoc() const { return Locs; }
1463
1464 /// get the corresponding DefInit.
1465 DefInit *getDefInit();
1466
1467 const std::vector<Init *> &getTemplateArgs() const {
1468 return TemplateArgs;
1469 }
1470 const std::vector<RecordVal> &getValues() const { return Values; }
1471 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1472 ArrayRef<SMRange> getSuperClassRanges() const { return SuperClassRanges; }
1473
1474 bool isTemplateArg(Init *Name) const {
1475 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1476 if (TemplateArgs[i] == Name) return true;
1477 return false;
1478 }
1479 bool isTemplateArg(StringRef Name) const {
1480 return isTemplateArg(StringInit::get(Name.str()));
1481 }
1482
1483 const RecordVal *getValue(const Init *Name) const {
1484 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1485 if (Values[i].getNameInit() == Name) return &Values[i];
1486 return nullptr;
1487 }
1488 const RecordVal *getValue(StringRef Name) const {
1489 return getValue(StringInit::get(Name));
1490 }
1491 RecordVal *getValue(const Init *Name) {
1492 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1493 if (Values[i].getNameInit() == Name) return &Values[i];
1494 return nullptr;
1495 }
1496 RecordVal *getValue(StringRef Name) {
1497 return getValue(StringInit::get(Name));
1498 }
1499
1500 void addTemplateArg(Init *Name) {
1501 assert(!isTemplateArg(Name) && "Template arg already defined!");
1502 TemplateArgs.push_back(Name);
1503 }
1504 void addTemplateArg(StringRef Name) {
1505 addTemplateArg(StringInit::get(Name.str()));
1506 }
1507
1508 void addValue(const RecordVal &RV) {
1509 assert(getValue(RV.getNameInit()) == nullptr && "Value already added!");
1510 Values.push_back(RV);
1511 if (Values.size() > 1)
1512 // Keep NAME at the end of the list. It makes record dumps a
1513 // bit prettier and allows TableGen tests to be written more
1514 // naturally. Tests can use CHECK-NEXT to look for Record
1515 // fields they expect to see after a def. They can't do that if
1516 // NAME is the first Record field.
1517 std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
1518 }
1519
1520 void removeValue(Init *Name) {
1521 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1522 if (Values[i].getNameInit() == Name) {
1523 Values.erase(Values.begin()+i);
1524 return;
1525 }
1526 llvm_unreachable("Cannot remove an entry that does not exist!");
1527 }
1528
1529 void removeValue(StringRef Name) {
1530 removeValue(StringInit::get(Name.str()));
1531 }
1532
1533 bool isSubClassOf(const Record *R) const {
1534 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1535 if (SuperClasses[i] == R)
1536 return true;
1537 return false;
1538 }
1539
1540 bool isSubClassOf(StringRef Name) const {
1541 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1542 if (SuperClasses[i]->getNameInitAsString() == Name)
1543 return true;
1544 return false;
1545 }
1546
1547 void addSuperClass(Record *R, SMRange Range) {
1548 assert(!isSubClassOf(R) && "Already subclassing record!");
1549 SuperClasses.push_back(R);
1550 SuperClassRanges.push_back(Range);
1551 }
1552
1553 /// resolveReferences - If there are any field references that refer to fields
1554 /// that have been filled in, we can propagate the values now.
1555 ///
1556 void resolveReferences() { resolveReferencesTo(nullptr); }
1557
1558 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1559 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1560 /// possible references.
1561 void resolveReferencesTo(const RecordVal *RV);
1562
1563 RecordKeeper &getRecords() const {
1564 return TrackedRecords;
1565 }
1566
1567 bool isAnonymous() const {
1568 return IsAnonymous;
1569 }
1570
1571 bool isResolveFirst() const {
1572 return ResolveFirst;
1573 }
1574
1575 void setResolveFirst(bool b) {
1576 ResolveFirst = b;
1577 }
1578
1579 void dump() const;
1580
1581 //===--------------------------------------------------------------------===//
1582 // High-level methods useful to tablegen back-ends
1583 //
1584
1585 /// getValueInit - Return the initializer for a value with the specified name,
1586 /// or throw an exception if the field does not exist.
1587 ///
1588 Init *getValueInit(StringRef FieldName) const;
1589
1590 /// Return true if the named field is unset.
1591 bool isValueUnset(StringRef FieldName) const {
1592 return getValueInit(FieldName) == UnsetInit::get();
1593 }
1594
1595 /// getValueAsString - This method looks up the specified field and returns
1596 /// its value as a string, throwing an exception if the field does not exist
1597 /// or if the value is not a string.
1598 ///
1599 std::string getValueAsString(StringRef FieldName) const;
1600
1601 /// getValueAsBitsInit - This method looks up the specified field and returns
1602 /// its value as a BitsInit, throwing an exception if the field does not exist
1603 /// or if the value is not the right type.
1604 ///
1605 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
1606
1607 /// getValueAsListInit - This method looks up the specified field and returns
1608 /// its value as a ListInit, throwing an exception if the field does not exist
1609 /// or if the value is not the right type.
1610 ///
1611 ListInit *getValueAsListInit(StringRef FieldName) const;
1612
1613 /// getValueAsListOfDefs - This method looks up the specified field and
1614 /// returns its value as a vector of records, throwing an exception if the
1615 /// field does not exist or if the value is not the right type.
1616 ///
1617 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
1618
1619 /// getValueAsListOfInts - This method looks up the specified field and
1620 /// returns its value as a vector of integers, throwing an exception if the
1621 /// field does not exist or if the value is not the right type.
1622 ///
1623 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
1624
1625 /// getValueAsListOfStrings - This method looks up the specified field and
1626 /// returns its value as a vector of strings, throwing an exception if the
1627 /// field does not exist or if the value is not the right type.
1628 ///
1629 std::vector<std::string> getValueAsListOfStrings(StringRef FieldName) const;
1630
1631 /// getValueAsDef - This method looks up the specified field and returns its
1632 /// value as a Record, throwing an exception if the field does not exist or if
1633 /// the value is not the right type.
1634 ///
1635 Record *getValueAsDef(StringRef FieldName) const;
1636
1637 /// getValueAsBit - This method looks up the specified field and returns its
1638 /// value as a bit, throwing an exception if the field does not exist or if
1639 /// the value is not the right type.
1640 ///
1641 bool getValueAsBit(StringRef FieldName) const;
1642
1643 /// getValueAsBitOrUnset - This method looks up the specified field and
1644 /// returns its value as a bit. If the field is unset, sets Unset to true and
1645 /// returns false.
1646 ///
1647 bool getValueAsBitOrUnset(StringRef FieldName, bool &Unset) const;
1648
1649 /// getValueAsInt - This method looks up the specified field and returns its
1650 /// value as an int64_t, throwing an exception if the field does not exist or
1651 /// if the value is not the right type.
1652 ///
1653 int64_t getValueAsInt(StringRef FieldName) const;
1654
1655 /// getValueAsDag - This method looks up the specified field and returns its
1656 /// value as an Dag, throwing an exception if the field does not exist or if
1657 /// the value is not the right type.
1658 ///
1659 DagInit *getValueAsDag(StringRef FieldName) const;
1660 };
1661
1662 raw_ostream &operator<<(raw_ostream &OS, const Record &R);
1663
1664 struct MultiClass {
1665 Record Rec; // Placeholder for template args and Name.
1666 typedef std::vector<std::unique_ptr<Record>> RecordVector;
1667 RecordVector DefPrototypes;
1668
1669 void dump() const;
1670
1671 MultiClass(const std::string &Name, SMLoc Loc, RecordKeeper &Records) :
1672 Rec(Name, Loc, Records) {}
1673 };
1674
1675 class RecordKeeper {
1676 typedef std::map<std::string, std::unique_ptr<Record>> RecordMap;
1677 RecordMap Classes, Defs;
1678
1679 public:
1680 const RecordMap &getClasses() const { return Classes; }
1681 const RecordMap &getDefs() const { return Defs; }
1682
1683 Record *getClass(const std::string &Name) const {
1684 auto I = Classes.find(Name);
1685 return I == Classes.end() ? nullptr : I->second.get();
1686 }
1687 Record *getDef(const std::string &Name) const {
1688 auto I = Defs.find(Name);
1689 return I == Defs.end() ? nullptr : I->second.get();
1690 }
1691 void addClass(std::unique_ptr<Record> R) {
1692 bool Ins = Classes.insert(std::make_pair(R->getName(),
1693 std::move(R))).second;
1694 (void)Ins;
1695 assert(Ins && "Class already exists");
1696 }
1697 void addDef(std::unique_ptr<Record> R) {
1698 bool Ins = Defs.insert(std::make_pair(R->getName(),
1699 std::move(R))).second;
1700 (void)Ins;
1701 assert(Ins && "Record already exists");
1702 }
1703
1704 //===--------------------------------------------------------------------===//
1705 // High-level helper methods, useful for tablegen backends...
1706
1707 /// getAllDerivedDefinitions - This method returns all concrete definitions
1708 /// that derive from the specified class name. If a class with the specified
1709 /// name does not exist, an exception is thrown.
1710 std::vector<Record*>
1711 getAllDerivedDefinitions(const std::string &ClassName) const;
1712
1713 void dump() const;
1714 };
1715
1716 /// LessRecord - Sorting predicate to sort record pointers by name.
1717 ///
1718 struct LessRecord {
1719 bool operator()(const Record *Rec1, const Record *Rec2) const {
1720 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
1721 }
1722 };
1723
1724 /// LessRecordByID - Sorting predicate to sort record pointers by their
1725 /// unique ID. If you just need a deterministic order, use this, since it
1726 /// just compares two `unsigned`; the other sorting predicates require
1727 /// string manipulation.
1728 struct LessRecordByID {
1729 bool operator()(const Record *LHS, const Record *RHS) const {
1730 return LHS->getID() < RHS->getID();
1731 }
1732 };
1733
1734 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1735 /// name field.
1736 ///
1737 struct LessRecordFieldName {
1738 bool operator()(const Record *Rec1, const Record *Rec2) const {
1739 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1740 }
1741 };
1742
1743 struct LessRecordRegister {
1744 static size_t min(size_t a, size_t b) { return a < b ? a : b; }
1745 static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; }
1746
1747 struct RecordParts {
1748 SmallVector<std::pair< bool, StringRef>, 4> Parts;
1749
1750 RecordParts(StringRef Rec) {
1751 if (Rec.empty())
1752 return;
1753
1754 size_t Len = 0;
1755 const char *Start = Rec.data();
1756 const char *Curr = Start;
1757 bool isDigitPart = ascii_isdigit(Curr[0]);
1758 for (size_t I = 0, E = Rec.size(); I != E; ++I, ++Len) {
1759 bool isDigit = ascii_isdigit(Curr[I]);
1760 if (isDigit != isDigitPart) {
1761 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1762 Len = 0;
1763 Start = &Curr[I];
1764 isDigitPart = ascii_isdigit(Curr[I]);
1765 }
1766 }
1767 // Push the last part.
1768 Parts.push_back(std::make_pair(isDigitPart, StringRef(Start, Len)));
1769 }
1770
1771 size_t size() { return Parts.size(); }
1772
1773 std::pair<bool, StringRef> getPart(size_t i) {
1774 assert (i < Parts.size() && "Invalid idx!");
1775 return Parts[i];
1776 }
1777 };
1778
1779 bool operator()(const Record *Rec1, const Record *Rec2) const {
1780 RecordParts LHSParts(StringRef(Rec1->getName()));
1781 RecordParts RHSParts(StringRef(Rec2->getName()));
1782
1783 size_t LHSNumParts = LHSParts.size();
1784 size_t RHSNumParts = RHSParts.size();
1785 assert (LHSNumParts && RHSNumParts && "Expected at least one part!");
1786
1787 if (LHSNumParts != RHSNumParts)
1788 return LHSNumParts < RHSNumParts;
1789
1790 // We expect the registers to be of the form [_a-zA-z]+([0-9]*[_a-zA-Z]*)*.
1791 for (size_t I = 0, E = LHSNumParts; I < E; I+=2) {
1792 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1793 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1794 // Expect even part to always be alpha.
1795 assert (LHSPart.first == false && RHSPart.first == false &&
1796 "Expected both parts to be alpha.");
1797 if (int Res = LHSPart.second.compare(RHSPart.second))
1798 return Res < 0;
1799 }
1800 for (size_t I = 1, E = LHSNumParts; I < E; I+=2) {
1801 std::pair<bool, StringRef> LHSPart = LHSParts.getPart(I);
1802 std::pair<bool, StringRef> RHSPart = RHSParts.getPart(I);
1803 // Expect odd part to always be numeric.
1804 assert (LHSPart.first == true && RHSPart.first == true &&
1805 "Expected both parts to be numeric.");
1806 if (LHSPart.second.size() != RHSPart.second.size())
1807 return LHSPart.second.size() < RHSPart.second.size();
1808
1809 unsigned LHSVal, RHSVal;
1810
1811 bool LHSFailed = LHSPart.second.getAsInteger(10, LHSVal); (void)LHSFailed;
1812 assert(!LHSFailed && "Unable to convert LHS to integer.");
1813 bool RHSFailed = RHSPart.second.getAsInteger(10, RHSVal); (void)RHSFailed;
1814 assert(!RHSFailed && "Unable to convert RHS to integer.");
1815
1816 if (LHSVal != RHSVal)
1817 return LHSVal < RHSVal;
1818 }
1819 return LHSNumParts < RHSNumParts;
1820 }
1821 };
1822
1823 raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
1824
1825 /// QualifyName - Return an Init with a qualifier prefix referring
1826 /// to CurRec's name.
1827 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1828 Init *Name, const std::string &Scoper);
1829
1830 /// QualifyName - Return an Init with a qualifier prefix referring
1831 /// to CurRec's name.
1832 Init *QualifyName(Record &CurRec, MultiClass *CurMultiClass,
1833 const std::string &Name, const std::string &Scoper);
1834
1835 } // End llvm namespace
1836
1837 #endif