1 //===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the main TableGen data structures, including the TableGen
11 // types, values, and high-level data structures.
13 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/raw_ostream.h"
54 class VarListElementInit
;
61 //===----------------------------------------------------------------------===//
63 //===----------------------------------------------------------------------===//
68 virtual std::string
getAsString() const = 0;
69 void print(raw_ostream
&OS
) const { OS
<< getAsString(); }
72 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
73 /// converted to the specified type.
74 virtual bool typeIsConvertibleTo(const RecTy
*RHS
) const = 0;
76 public: // These methods should only be called from subclasses of Init
77 virtual Init
*convertValue( UnsetInit
*UI
) { return 0; }
78 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
79 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
80 virtual Init
*convertValue( IntInit
*II
) { return 0; }
81 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
82 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
83 virtual Init
*convertValue( UnOpInit
*UI
) {
84 return convertValue((TypedInit
*)UI
);
86 virtual Init
*convertValue( BinOpInit
*UI
) {
87 return convertValue((TypedInit
*)UI
);
89 virtual Init
*convertValue( TernOpInit
*UI
) {
90 return convertValue((TypedInit
*)UI
);
92 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
93 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
94 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
95 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
96 virtual Init
*convertValue( TypedInit
*TI
) { return 0; }
97 virtual Init
*convertValue( VarInit
*VI
) {
98 return convertValue((TypedInit
*)VI
);
100 virtual Init
*convertValue( FieldInit
*FI
) {
101 return convertValue((TypedInit
*)FI
);
104 public: // These methods should only be called by subclasses of RecTy.
105 // baseClassOf - These virtual methods should be overloaded to return true iff
106 // all values of type 'RHS' can be converted to the 'this' type.
107 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
108 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
109 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
110 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
111 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
112 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
113 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
114 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
117 inline raw_ostream
&operator<<(raw_ostream
&OS
, const RecTy
&Ty
) {
123 /// BitRecTy - 'bit' - Represent a single bit
125 class BitRecTy
: public RecTy
{
127 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
128 virtual Init
*convertValue( BitInit
*BI
) { return (Init
*)BI
; }
129 virtual Init
*convertValue( BitsInit
*BI
);
130 virtual Init
*convertValue( IntInit
*II
);
131 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
132 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
133 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
134 virtual Init
*convertValue(VarBitInit
*VB
) { return (Init
*)VB
; }
135 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
136 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
137 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
138 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
139 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
140 virtual Init
*convertValue( TypedInit
*TI
);
141 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
142 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
144 std::string
getAsString() const { return "bit"; }
146 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
147 return RHS
->baseClassOf(this);
149 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return true; }
150 virtual bool baseClassOf(const BitsRecTy
*RHS
) const;
151 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return true; }
152 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
153 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
154 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
155 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
156 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
161 // BitsRecTy - 'bits<n>' - Represent a fixed number of bits
162 /// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
164 class BitsRecTy
: public RecTy
{
167 explicit BitsRecTy(unsigned Sz
) : Size(Sz
) {}
169 unsigned getNumBits() const { return Size
; }
171 virtual Init
*convertValue( UnsetInit
*UI
);
172 virtual Init
*convertValue( BitInit
*UI
);
173 virtual Init
*convertValue( BitsInit
*BI
);
174 virtual Init
*convertValue( IntInit
*II
);
175 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
176 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
177 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
178 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
179 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
180 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
181 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
182 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
183 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
184 virtual Init
*convertValue( TypedInit
*TI
);
185 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
186 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
188 std::string
getAsString() const;
190 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
191 return RHS
->baseClassOf(this);
193 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return Size
== 1; }
194 virtual bool baseClassOf(const BitsRecTy
*RHS
) const {
195 return RHS
->Size
== Size
;
197 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return true; }
198 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
199 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
200 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
201 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
202 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
207 /// IntRecTy - 'int' - Represent an integer value of no particular size
209 class IntRecTy
: public RecTy
{
211 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
212 virtual Init
*convertValue( BitInit
*BI
);
213 virtual Init
*convertValue( BitsInit
*BI
);
214 virtual Init
*convertValue( IntInit
*II
) { return (Init
*)II
; }
215 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
216 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
217 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
218 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
219 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
220 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
221 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
222 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
223 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
224 virtual Init
*convertValue( TypedInit
*TI
);
225 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
226 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
228 std::string
getAsString() const { return "int"; }
230 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
231 return RHS
->baseClassOf(this);
234 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return true; }
235 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return true; }
236 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return true; }
237 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
238 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
239 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
240 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
241 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
245 /// StringRecTy - 'string' - Represent an string value
247 class StringRecTy
: public RecTy
{
249 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
250 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
251 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
252 virtual Init
*convertValue( IntInit
*II
) { return 0; }
253 virtual Init
*convertValue(StringInit
*SI
) { return (Init
*)SI
; }
254 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
255 virtual Init
*convertValue( UnOpInit
*BO
);
256 virtual Init
*convertValue( BinOpInit
*BO
);
257 virtual Init
*convertValue( TernOpInit
*BO
) { return RecTy::convertValue(BO
);}
259 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
260 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
261 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
262 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
263 virtual Init
*convertValue( TypedInit
*TI
);
264 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
265 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
267 std::string
getAsString() const { return "string"; }
269 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
270 return RHS
->baseClassOf(this);
273 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
274 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
275 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
276 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return true; }
277 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
278 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
279 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
280 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
283 // ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
284 // the specified type.
285 /// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must
286 /// be of the specified type.
288 class ListRecTy
: public RecTy
{
291 explicit ListRecTy(RecTy
*T
) : Ty(T
) {}
293 RecTy
*getElementType() const { return Ty
; }
295 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
296 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
297 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
298 virtual Init
*convertValue( IntInit
*II
) { return 0; }
299 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
300 virtual Init
*convertValue( ListInit
*LI
);
301 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
302 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
303 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
304 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
305 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
306 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
307 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
308 virtual Init
*convertValue( TypedInit
*TI
);
309 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
310 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
312 std::string
getAsString() const;
314 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
315 return RHS
->baseClassOf(this);
318 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
319 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
320 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
321 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
322 virtual bool baseClassOf(const ListRecTy
*RHS
) const {
323 return RHS
->getElementType()->typeIsConvertibleTo(Ty
);
325 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
326 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
327 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
330 /// CodeRecTy - 'code' - Represent an code fragment, function or method.
332 class CodeRecTy
: public RecTy
{
334 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
335 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
336 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
337 virtual Init
*convertValue( IntInit
*II
) { return 0; }
338 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
339 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
340 virtual Init
*convertValue( CodeInit
*CI
) { return (Init
*)CI
; }
341 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
342 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
343 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
344 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
345 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
346 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
347 virtual Init
*convertValue( TypedInit
*TI
);
348 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
349 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
351 std::string
getAsString() const { return "code"; }
353 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
354 return RHS
->baseClassOf(this);
356 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
357 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
358 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
359 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
360 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
361 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return true; }
362 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
363 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
366 /// DagRecTy - 'dag' - Represent a dag fragment
368 class DagRecTy
: public RecTy
{
370 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
371 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
372 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
373 virtual Init
*convertValue( IntInit
*II
) { return 0; }
374 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
375 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
376 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
377 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
378 virtual Init
*convertValue( DefInit
*DI
) { return 0; }
379 virtual Init
*convertValue( UnOpInit
*BO
);
380 virtual Init
*convertValue( BinOpInit
*BO
);
381 virtual Init
*convertValue( TernOpInit
*BO
) { return RecTy::convertValue(BO
);}
382 virtual Init
*convertValue( DagInit
*CI
) { return (Init
*)CI
; }
383 virtual Init
*convertValue( TypedInit
*TI
);
384 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
385 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
387 std::string
getAsString() const { return "dag"; }
389 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
390 return RHS
->baseClassOf(this);
393 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
394 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
395 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
396 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
397 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
398 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
399 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return true; }
400 virtual bool baseClassOf(const RecordRecTy
*RHS
) const { return false; }
404 /// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
407 class RecordRecTy
: public RecTy
{
410 explicit RecordRecTy(Record
*R
) : Rec(R
) {}
412 Record
*getRecord() const { return Rec
; }
414 virtual Init
*convertValue( UnsetInit
*UI
) { return (Init
*)UI
; }
415 virtual Init
*convertValue( BitInit
*BI
) { return 0; }
416 virtual Init
*convertValue( BitsInit
*BI
) { return 0; }
417 virtual Init
*convertValue( IntInit
*II
) { return 0; }
418 virtual Init
*convertValue(StringInit
*SI
) { return 0; }
419 virtual Init
*convertValue( ListInit
*LI
) { return 0; }
420 virtual Init
*convertValue( CodeInit
*CI
) { return 0; }
421 virtual Init
*convertValue(VarBitInit
*VB
) { return 0; }
422 virtual Init
*convertValue( UnOpInit
*UI
) { return RecTy::convertValue(UI
);}
423 virtual Init
*convertValue( BinOpInit
*UI
) { return RecTy::convertValue(UI
);}
424 virtual Init
*convertValue( TernOpInit
*UI
) { return RecTy::convertValue(UI
);}
425 virtual Init
*convertValue( DefInit
*DI
);
426 virtual Init
*convertValue( DagInit
*DI
) { return 0; }
427 virtual Init
*convertValue( TypedInit
*VI
);
428 virtual Init
*convertValue( VarInit
*VI
) { return RecTy::convertValue(VI
);}
429 virtual Init
*convertValue( FieldInit
*FI
) { return RecTy::convertValue(FI
);}
431 std::string
getAsString() const;
433 bool typeIsConvertibleTo(const RecTy
*RHS
) const {
434 return RHS
->baseClassOf(this);
436 virtual bool baseClassOf(const BitRecTy
*RHS
) const { return false; }
437 virtual bool baseClassOf(const BitsRecTy
*RHS
) const { return false; }
438 virtual bool baseClassOf(const IntRecTy
*RHS
) const { return false; }
439 virtual bool baseClassOf(const StringRecTy
*RHS
) const { return false; }
440 virtual bool baseClassOf(const ListRecTy
*RHS
) const { return false; }
441 virtual bool baseClassOf(const CodeRecTy
*RHS
) const { return false; }
442 virtual bool baseClassOf(const DagRecTy
*RHS
) const { return false; }
443 virtual bool baseClassOf(const RecordRecTy
*RHS
) const;
446 /// resolveTypes - Find a common type that T1 and T2 convert to.
447 /// Return 0 if no such type exists.
449 RecTy
*resolveTypes(RecTy
*T1
, RecTy
*T2
);
451 //===----------------------------------------------------------------------===//
452 // Initializer Classes
453 //===----------------------------------------------------------------------===//
458 /// isComplete - This virtual method should be overridden by values that may
459 /// not be completely specified yet.
460 virtual bool isComplete() const { return true; }
462 /// print - Print out this value.
463 void print(raw_ostream
&OS
) const { OS
<< getAsString(); }
465 /// getAsString - Convert this value to a string form.
466 virtual std::string
getAsString() const = 0;
468 /// dump - Debugging method that may be called through a debugger, just
469 /// invokes print on stderr.
472 /// convertInitializerTo - This virtual function is a simple call-back
473 /// function that should be overridden to call the appropriate
474 /// RecTy::convertValue method.
476 virtual Init
*convertInitializerTo(RecTy
*Ty
) = 0;
478 /// convertInitializerBitRange - This method is used to implement the bitrange
479 /// selection operator. Given an initializer, it selects the specified bits
480 /// out, returning them as a new init of bits type. If it is not legal to use
481 /// the bit subscript operator on this initializer, return null.
483 virtual Init
*convertInitializerBitRange(const std::vector
<unsigned> &Bits
) {
487 /// convertInitListSlice - This method is used to implement the list slice
488 /// selection operator. Given an initializer, it selects the specified list
489 /// elements, returning them as a new init of list type. If it is not legal
490 /// to take a slice of this, return null.
492 virtual Init
*convertInitListSlice(const std::vector
<unsigned> &Elements
) {
496 /// getFieldType - This method is used to implement the FieldInit class.
497 /// Implementors of this method should return the type of the named field if
498 /// they are of record type.
500 virtual RecTy
*getFieldType(const std::string
&FieldName
) const { return 0; }
502 /// getFieldInit - This method complements getFieldType to return the
503 /// initializer for the specified field. If getFieldType returns non-null
504 /// this method should return non-null, otherwise it returns null.
506 virtual Init
*getFieldInit(Record
&R
, const std::string
&FieldName
) const {
510 /// resolveReferences - This method is used by classes that refer to other
511 /// variables which may not be defined at the time they expression is formed.
512 /// If a value is set for the variable later, this method will be called on
513 /// users of the value to allow the value to propagate out.
515 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
) {
520 inline raw_ostream
&operator<<(raw_ostream
&OS
, const Init
&I
) {
521 I
.print(OS
); return OS
;
524 /// TypedInit - This is the common super-class of types that have a specific,
527 class TypedInit
: public Init
{
530 explicit TypedInit(RecTy
*T
) : Ty(T
) {}
532 RecTy
*getType() const { return Ty
; }
534 virtual Init
*convertInitializerBitRange(const std::vector
<unsigned> &Bits
);
535 virtual Init
*convertInitListSlice(const std::vector
<unsigned> &Elements
);
537 /// resolveBitReference - This method is used to implement
538 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
539 /// simply return the resolved value, otherwise we return null.
541 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
544 /// resolveListElementReference - This method is used to implement
545 /// VarListElementInit::resolveReferences. If the list element is resolvable
546 /// now, we return the resolved value, otherwise we return null.
547 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
552 /// UnsetInit - ? - Represents an uninitialized value
554 class UnsetInit
: public Init
{
556 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
557 return Ty
->convertValue(this);
560 virtual bool isComplete() const { return false; }
561 virtual std::string
getAsString() const { return "?"; }
565 /// BitInit - true/false - Represent a concrete initializer for a bit.
567 class BitInit
: public Init
{
570 explicit BitInit(bool V
) : Value(V
) {}
572 bool getValue() const { return Value
; }
574 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
575 return Ty
->convertValue(this);
578 virtual std::string
getAsString() const { return Value
? "1" : "0"; }
581 /// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
582 /// It contains a vector of bits, whose size is determined by the type.
584 class BitsInit
: public Init
{
585 std::vector
<Init
*> Bits
;
587 explicit BitsInit(unsigned Size
) : Bits(Size
) {}
589 unsigned getNumBits() const { return Bits
.size(); }
591 Init
*getBit(unsigned Bit
) const {
592 assert(Bit
< Bits
.size() && "Bit index out of range!");
595 void setBit(unsigned Bit
, Init
*V
) {
596 assert(Bit
< Bits
.size() && "Bit index out of range!");
597 assert(Bits
[Bit
] == 0 && "Bit already set!");
601 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
602 return Ty
->convertValue(this);
604 virtual Init
*convertInitializerBitRange(const std::vector
<unsigned> &Bits
);
606 virtual bool isComplete() const {
607 for (unsigned i
= 0; i
!= getNumBits(); ++i
)
608 if (!getBit(i
)->isComplete()) return false;
611 virtual std::string
getAsString() const;
613 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
615 // printXX - Print this bitstream with the specified format, returning true if
616 // it is not possible.
617 bool printInHex(raw_ostream
&OS
) const;
618 bool printAsVariable(raw_ostream
&OS
) const;
619 bool printAsUnset(raw_ostream
&OS
) const;
623 /// IntInit - 7 - Represent an initalization by a literal integer value.
625 class IntInit
: public TypedInit
{
628 explicit IntInit(int64_t V
) : TypedInit(new IntRecTy
), Value(V
) {}
630 int64_t getValue() const { return Value
; }
632 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
633 return Ty
->convertValue(this);
635 virtual Init
*convertInitializerBitRange(const std::vector
<unsigned> &Bits
);
637 virtual std::string
getAsString() const;
639 /// resolveBitReference - This method is used to implement
640 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
641 /// simply return the resolved value, otherwise we return null.
643 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
645 assert(0 && "Illegal bit reference off int");
649 /// resolveListElementReference - This method is used to implement
650 /// VarListElementInit::resolveReferences. If the list element is resolvable
651 /// now, we return the resolved value, otherwise we return null.
652 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
654 assert(0 && "Illegal element reference off int");
660 /// StringInit - "foo" - Represent an initialization by a string value.
662 class StringInit
: public TypedInit
{
665 explicit StringInit(const std::string
&V
)
666 : TypedInit(new StringRecTy
), Value(V
) {}
668 const std::string
&getValue() const { return Value
; }
670 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
671 return Ty
->convertValue(this);
674 virtual std::string
getAsString() const { return "\"" + Value
+ "\""; }
676 /// resolveBitReference - This method is used to implement
677 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
678 /// simply return the resolved value, otherwise we return null.
680 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
682 assert(0 && "Illegal bit reference off string");
686 /// resolveListElementReference - This method is used to implement
687 /// VarListElementInit::resolveReferences. If the list element is resolvable
688 /// now, we return the resolved value, otherwise we return null.
689 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
691 assert(0 && "Illegal element reference off string");
696 /// CodeInit - "[{...}]" - Represent a code fragment.
698 class CodeInit
: public Init
{
701 explicit CodeInit(const std::string
&V
) : Value(V
) {}
703 const std::string
getValue() const { return Value
; }
705 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
706 return Ty
->convertValue(this);
709 virtual std::string
getAsString() const { return "[{" + Value
+ "}]"; }
712 /// ListInit - [AL, AH, CL] - Represent a list of defs
714 class ListInit
: public TypedInit
{
715 std::vector
<Init
*> Values
;
717 typedef std::vector
<Init
*>::iterator iterator
;
718 typedef std::vector
<Init
*>::const_iterator const_iterator
;
720 explicit ListInit(std::vector
<Init
*> &Vs
, RecTy
*EltTy
)
721 : TypedInit(new ListRecTy(EltTy
)) {
724 explicit ListInit(iterator Start
, iterator End
, RecTy
*EltTy
)
725 : TypedInit(new ListRecTy(EltTy
)), Values(Start
, End
) {}
727 unsigned getSize() const { return Values
.size(); }
728 Init
*getElement(unsigned i
) const {
729 assert(i
< Values
.size() && "List element index out of range!");
733 Record
*getElementAsRecord(unsigned i
) const;
735 Init
*convertInitListSlice(const std::vector
<unsigned> &Elements
);
737 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
738 return Ty
->convertValue(this);
741 /// resolveReferences - This method is used by classes that refer to other
742 /// variables which may not be defined at the time they expression is formed.
743 /// If a value is set for the variable later, this method will be called on
744 /// users of the value to allow the value to propagate out.
746 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
748 virtual std::string
getAsString() const;
750 inline iterator
begin() { return Values
.begin(); }
751 inline const_iterator
begin() const { return Values
.begin(); }
752 inline iterator
end () { return Values
.end(); }
753 inline const_iterator
end () const { return Values
.end(); }
755 inline size_t size () const { return Values
.size(); }
756 inline bool empty() const { return Values
.empty(); }
758 /// resolveBitReference - This method is used to implement
759 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
760 /// simply return the resolved value, otherwise we return null.
762 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
764 assert(0 && "Illegal bit reference off list");
768 /// resolveListElementReference - This method is used to implement
769 /// VarListElementInit::resolveReferences. If the list element is resolvable
770 /// now, we return the resolved value, otherwise we return null.
771 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
776 /// OpInit - Base class for operators
778 class OpInit
: public TypedInit
{
780 OpInit(RecTy
*Type
) : TypedInit(Type
) {}
782 // Clone - Clone this operator, replacing arguments with the new list
783 virtual OpInit
*clone(std::vector
<Init
*> &Operands
) = 0;
785 virtual int getNumOperands() const = 0;
786 virtual Init
*getOperand(int i
) = 0;
788 // Fold - If possible, fold this to a simpler init. Return this if not
790 virtual Init
*Fold(Record
*CurRec
, MultiClass
*CurMultiClass
) = 0;
792 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
793 return Ty
->convertValue(this);
796 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
798 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
803 /// UnOpInit - !op (X) - Transform an init.
805 class UnOpInit
: public OpInit
{
807 enum UnaryOp
{ CAST
, CAR
, CDR
, LNULL
};
812 UnOpInit(UnaryOp opc
, Init
*lhs
, RecTy
*Type
) :
813 OpInit(Type
), Opc(opc
), LHS(lhs
) {
816 // Clone - Clone this operator, replacing arguments with the new list
817 virtual OpInit
*clone(std::vector
<Init
*> &Operands
) {
818 assert(Operands
.size() == 1 &&
819 "Wrong number of operands for unary operation");
820 return new UnOpInit(getOpcode(), *Operands
.begin(), getType());
823 int getNumOperands() const { return 1; }
824 Init
*getOperand(int i
) {
825 assert(i
== 0 && "Invalid operand id for unary operator");
829 UnaryOp
getOpcode() const { return Opc
; }
830 Init
*getOperand() const { return LHS
; }
832 // Fold - If possible, fold this to a simpler init. Return this if not
834 Init
*Fold(Record
*CurRec
, MultiClass
*CurMultiClass
);
836 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
838 /// getFieldType - This method is used to implement the FieldInit class.
839 /// Implementors of this method should return the type of the named field if
840 /// they are of record type.
842 virtual RecTy
*getFieldType(const std::string
&FieldName
) const;
844 virtual std::string
getAsString() const;
847 /// BinOpInit - !op (X, Y) - Combine two inits.
849 class BinOpInit
: public OpInit
{
851 enum BinaryOp
{ SHL
, SRA
, SRL
, STRCONCAT
, CONCAT
, NAMECONCAT
};
856 BinOpInit(BinaryOp opc
, Init
*lhs
, Init
*rhs
, RecTy
*Type
) :
857 OpInit(Type
), Opc(opc
), LHS(lhs
), RHS(rhs
) {
860 // Clone - Clone this operator, replacing arguments with the new list
861 virtual OpInit
*clone(std::vector
<Init
*> &Operands
) {
862 assert(Operands
.size() == 2 &&
863 "Wrong number of operands for binary operation");
864 return new BinOpInit(getOpcode(), Operands
[0], Operands
[1], getType());
867 int getNumOperands() const { return 2; }
868 Init
*getOperand(int i
) {
869 assert((i
== 0 || i
== 1) && "Invalid operand id for binary operator");
878 BinaryOp
getOpcode() const { return Opc
; }
879 Init
*getLHS() const { return LHS
; }
880 Init
*getRHS() const { return RHS
; }
882 // Fold - If possible, fold this to a simpler init. Return this if not
884 Init
*Fold(Record
*CurRec
, MultiClass
*CurMultiClass
);
886 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
888 virtual std::string
getAsString() const;
891 /// TernOpInit - !op (X, Y, Z) - Combine two inits.
893 class TernOpInit
: public OpInit
{
895 enum TernaryOp
{ SUBST
, FOREACH
, IF
};
898 Init
*LHS
, *MHS
, *RHS
;
900 TernOpInit(TernaryOp opc
, Init
*lhs
, Init
*mhs
, Init
*rhs
, RecTy
*Type
) :
901 OpInit(Type
), Opc(opc
), LHS(lhs
), MHS(mhs
), RHS(rhs
) {
904 // Clone - Clone this operator, replacing arguments with the new list
905 virtual OpInit
*clone(std::vector
<Init
*> &Operands
) {
906 assert(Operands
.size() == 3 &&
907 "Wrong number of operands for ternary operation");
908 return new TernOpInit(getOpcode(), Operands
[0], Operands
[1], Operands
[2],
912 int getNumOperands() const { return 3; }
913 Init
*getOperand(int i
) {
914 assert((i
== 0 || i
== 1 || i
== 2) &&
915 "Invalid operand id for ternary operator");
927 TernaryOp
getOpcode() const { return Opc
; }
928 Init
*getLHS() const { return LHS
; }
929 Init
*getMHS() const { return MHS
; }
930 Init
*getRHS() const { return RHS
; }
932 // Fold - If possible, fold this to a simpler init. Return this if not
934 Init
*Fold(Record
*CurRec
, MultiClass
*CurMultiClass
);
936 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
938 virtual std::string
getAsString() const;
942 /// VarInit - 'Opcode' - Represent a reference to an entire variable object.
944 class VarInit
: public TypedInit
{
947 explicit VarInit(const std::string
&VN
, RecTy
*T
)
948 : TypedInit(T
), VarName(VN
) {}
950 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
951 return Ty
->convertValue(this);
954 const std::string
&getName() const { return VarName
; }
956 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
958 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
961 virtual RecTy
*getFieldType(const std::string
&FieldName
) const;
962 virtual Init
*getFieldInit(Record
&R
, const std::string
&FieldName
) const;
964 /// resolveReferences - This method is used by classes that refer to other
965 /// variables which may not be defined at the time they expression is formed.
966 /// If a value is set for the variable later, this method will be called on
967 /// users of the value to allow the value to propagate out.
969 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
971 virtual std::string
getAsString() const { return VarName
; }
975 /// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
977 class VarBitInit
: public Init
{
981 VarBitInit(TypedInit
*T
, unsigned B
) : TI(T
), Bit(B
) {
982 assert(T
->getType() && dynamic_cast<BitsRecTy
*>(T
->getType()) &&
983 ((BitsRecTy
*)T
->getType())->getNumBits() > B
&&
984 "Illegal VarBitInit expression!");
987 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
988 return Ty
->convertValue(this);
991 TypedInit
*getVariable() const { return TI
; }
992 unsigned getBitNum() const { return Bit
; }
994 virtual std::string
getAsString() const;
995 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
998 /// VarListElementInit - List[4] - Represent access to one element of a var or
1000 class VarListElementInit
: public TypedInit
{
1004 VarListElementInit(TypedInit
*T
, unsigned E
)
1005 : TypedInit(dynamic_cast<ListRecTy
*>(T
->getType())->getElementType()),
1007 assert(T
->getType() && dynamic_cast<ListRecTy
*>(T
->getType()) &&
1008 "Illegal VarBitInit expression!");
1011 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
1012 return Ty
->convertValue(this);
1015 TypedInit
*getVariable() const { return TI
; }
1016 unsigned getElementNum() const { return Element
; }
1018 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
1021 /// resolveListElementReference - This method is used to implement
1022 /// VarListElementInit::resolveReferences. If the list element is resolvable
1023 /// now, we return the resolved value, otherwise we return null.
1024 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1027 virtual std::string
getAsString() const;
1028 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
1031 /// DefInit - AL - Represent a reference to a 'def' in the description
1033 class DefInit
: public TypedInit
{
1036 explicit DefInit(Record
*D
) : TypedInit(new RecordRecTy(D
)), Def(D
) {}
1038 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
1039 return Ty
->convertValue(this);
1042 Record
*getDef() const { return Def
; }
1044 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1046 virtual RecTy
*getFieldType(const std::string
&FieldName
) const;
1047 virtual Init
*getFieldInit(Record
&R
, const std::string
&FieldName
) const;
1049 virtual std::string
getAsString() const;
1051 /// resolveBitReference - This method is used to implement
1052 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1053 /// simply return the resolved value, otherwise we return null.
1055 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
1057 assert(0 && "Illegal bit reference off def");
1061 /// resolveListElementReference - This method is used to implement
1062 /// VarListElementInit::resolveReferences. If the list element is resolvable
1063 /// now, we return the resolved value, otherwise we return null.
1064 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1066 assert(0 && "Illegal element reference off def");
1072 /// FieldInit - X.Y - Represent a reference to a subfield of a variable
1074 class FieldInit
: public TypedInit
{
1075 Init
*Rec
; // Record we are referring to
1076 std::string FieldName
; // Field we are accessing
1078 FieldInit(Init
*R
, const std::string
&FN
)
1079 : TypedInit(R
->getFieldType(FN
)), Rec(R
), FieldName(FN
) {
1080 assert(getType() && "FieldInit with non-record type!");
1083 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
1084 return Ty
->convertValue(this);
1087 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
1089 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1092 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
1094 virtual std::string
getAsString() const {
1095 return Rec
->getAsString() + "." + FieldName
;
1099 /// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1100 /// to have at least one value then a (possibly empty) list of arguments. Each
1101 /// argument can have a name associated with it.
1103 class DagInit
: public TypedInit
{
1105 std::string ValName
;
1106 std::vector
<Init
*> Args
;
1107 std::vector
<std::string
> ArgNames
;
1109 DagInit(Init
*V
, std::string VN
,
1110 const std::vector
<std::pair
<Init
*, std::string
> > &args
)
1111 : TypedInit(new DagRecTy
), Val(V
), ValName(VN
) {
1112 Args
.reserve(args
.size());
1113 ArgNames
.reserve(args
.size());
1114 for (unsigned i
= 0, e
= args
.size(); i
!= e
; ++i
) {
1115 Args
.push_back(args
[i
].first
);
1116 ArgNames
.push_back(args
[i
].second
);
1119 DagInit(Init
*V
, std::string VN
, const std::vector
<Init
*> &args
,
1120 const std::vector
<std::string
> &argNames
)
1121 : TypedInit(new DagRecTy
), Val(V
), ValName(VN
), Args(args
), ArgNames(argNames
) {
1124 virtual Init
*convertInitializerTo(RecTy
*Ty
) {
1125 return Ty
->convertValue(this);
1128 Init
*getOperator() const { return Val
; }
1130 const std::string
&getName() const { return ValName
; }
1132 unsigned getNumArgs() const { return Args
.size(); }
1133 Init
*getArg(unsigned Num
) const {
1134 assert(Num
< Args
.size() && "Arg number out of range!");
1137 const std::string
&getArgName(unsigned Num
) const {
1138 assert(Num
< ArgNames
.size() && "Arg number out of range!");
1139 return ArgNames
[Num
];
1142 void setArg(unsigned Num
, Init
*I
) {
1143 assert(Num
< Args
.size() && "Arg number out of range!");
1147 virtual Init
*resolveReferences(Record
&R
, const RecordVal
*RV
);
1149 virtual std::string
getAsString() const;
1151 typedef std::vector
<Init
*>::iterator arg_iterator
;
1152 typedef std::vector
<Init
*>::const_iterator const_arg_iterator
;
1153 typedef std::vector
<std::string
>::iterator name_iterator
;
1154 typedef std::vector
<std::string
>::const_iterator const_name_iterator
;
1156 inline arg_iterator
arg_begin() { return Args
.begin(); }
1157 inline const_arg_iterator
arg_begin() const { return Args
.begin(); }
1158 inline arg_iterator
arg_end () { return Args
.end(); }
1159 inline const_arg_iterator
arg_end () const { return Args
.end(); }
1161 inline size_t arg_size () const { return Args
.size(); }
1162 inline bool arg_empty() const { return Args
.empty(); }
1164 inline name_iterator
name_begin() { return ArgNames
.begin(); }
1165 inline const_name_iterator
name_begin() const { return ArgNames
.begin(); }
1166 inline name_iterator
name_end () { return ArgNames
.end(); }
1167 inline const_name_iterator
name_end () const { return ArgNames
.end(); }
1169 inline size_t name_size () const { return ArgNames
.size(); }
1170 inline bool name_empty() const { return ArgNames
.empty(); }
1172 virtual Init
*resolveBitReference(Record
&R
, const RecordVal
*RV
,
1174 assert(0 && "Illegal bit reference off dag");
1178 virtual Init
*resolveListElementReference(Record
&R
, const RecordVal
*RV
,
1180 assert(0 && "Illegal element reference off dag");
1186 //===----------------------------------------------------------------------===//
1187 // High-Level Classes
1188 //===----------------------------------------------------------------------===//
1196 RecordVal(const std::string
&N
, RecTy
*T
, unsigned P
);
1198 const std::string
&getName() const { return Name
; }
1200 unsigned getPrefix() const { return Prefix
; }
1201 RecTy
*getType() const { return Ty
; }
1202 Init
*getValue() const { return Value
; }
1204 bool setValue(Init
*V
) {
1206 Value
= V
->convertInitializerTo(Ty
);
1214 void print(raw_ostream
&OS
, bool PrintSem
= true) const;
1217 inline raw_ostream
&operator<<(raw_ostream
&OS
, const RecordVal
&RV
) {
1218 RV
.print(OS
<< " ");
1223 static unsigned LastID
;
1225 // Unique record ID.
1229 std::vector
<std::string
> TemplateArgs
;
1230 std::vector
<RecordVal
> Values
;
1231 std::vector
<Record
*> SuperClasses
;
1234 explicit Record(const std::string
&N
, SMLoc loc
) :
1235 ID(LastID
++), Name(N
), Loc(loc
) {}
1238 unsigned getID() const { return ID
; }
1240 const std::string
&getName() const { return Name
; }
1241 void setName(const std::string
&Name
); // Also updates RecordKeeper.
1243 SMLoc
getLoc() const { return Loc
; }
1245 const std::vector
<std::string
> &getTemplateArgs() const {
1246 return TemplateArgs
;
1248 const std::vector
<RecordVal
> &getValues() const { return Values
; }
1249 const std::vector
<Record
*> &getSuperClasses() const { return SuperClasses
; }
1251 bool isTemplateArg(const std::string
&Name
) const {
1252 for (unsigned i
= 0, e
= TemplateArgs
.size(); i
!= e
; ++i
)
1253 if (TemplateArgs
[i
] == Name
) return true;
1257 const RecordVal
*getValue(const std::string
&Name
) const {
1258 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
1259 if (Values
[i
].getName() == Name
) return &Values
[i
];
1262 RecordVal
*getValue(const std::string
&Name
) {
1263 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
1264 if (Values
[i
].getName() == Name
) return &Values
[i
];
1268 void addTemplateArg(const std::string
&Name
) {
1269 assert(!isTemplateArg(Name
) && "Template arg already defined!");
1270 TemplateArgs
.push_back(Name
);
1273 void addValue(const RecordVal
&RV
) {
1274 assert(getValue(RV
.getName()) == 0 && "Value already added!");
1275 Values
.push_back(RV
);
1278 void removeValue(const std::string
&Name
) {
1279 assert(getValue(Name
) && "Cannot remove an entry that does not exist!");
1280 for (unsigned i
= 0, e
= Values
.size(); i
!= e
; ++i
)
1281 if (Values
[i
].getName() == Name
) {
1282 Values
.erase(Values
.begin()+i
);
1285 assert(0 && "Name does not exist in record!");
1288 bool isSubClassOf(const Record
*R
) const {
1289 for (unsigned i
= 0, e
= SuperClasses
.size(); i
!= e
; ++i
)
1290 if (SuperClasses
[i
] == R
)
1295 bool isSubClassOf(const std::string
&Name
) const {
1296 for (unsigned i
= 0, e
= SuperClasses
.size(); i
!= e
; ++i
)
1297 if (SuperClasses
[i
]->getName() == Name
)
1302 void addSuperClass(Record
*R
) {
1303 assert(!isSubClassOf(R
) && "Already subclassing record!");
1304 SuperClasses
.push_back(R
);
1307 /// resolveReferences - If there are any field references that refer to fields
1308 /// that have been filled in, we can propagate the values now.
1310 void resolveReferences() { resolveReferencesTo(0); }
1312 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1313 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1314 /// possible references.
1315 void resolveReferencesTo(const RecordVal
*RV
);
1319 //===--------------------------------------------------------------------===//
1320 // High-level methods useful to tablegen back-ends
1323 /// getValueInit - Return the initializer for a value with the specified name,
1324 /// or throw an exception if the field does not exist.
1326 Init
*getValueInit(const std::string
&FieldName
) const;
1328 /// getValueAsString - This method looks up the specified field and returns
1329 /// its value as a string, throwing an exception if the field does not exist
1330 /// or if the value is not a string.
1332 std::string
getValueAsString(const std::string
&FieldName
) const;
1334 /// getValueAsBitsInit - This method looks up the specified field and returns
1335 /// its value as a BitsInit, throwing an exception if the field does not exist
1336 /// or if the value is not the right type.
1338 BitsInit
*getValueAsBitsInit(const std::string
&FieldName
) const;
1340 /// getValueAsListInit - This method looks up the specified field and returns
1341 /// its value as a ListInit, throwing an exception if the field does not exist
1342 /// or if the value is not the right type.
1344 ListInit
*getValueAsListInit(const std::string
&FieldName
) const;
1346 /// getValueAsListOfDefs - This method looks up the specified field and
1347 /// returns its value as a vector of records, throwing an exception if the
1348 /// field does not exist or if the value is not the right type.
1350 std::vector
<Record
*> getValueAsListOfDefs(const std::string
&FieldName
) const;
1352 /// getValueAsListOfInts - This method looks up the specified field and returns
1353 /// its value as a vector of integers, throwing an exception if the field does
1354 /// not exist or if the value is not the right type.
1356 std::vector
<int64_t> getValueAsListOfInts(const std::string
&FieldName
) const;
1358 /// getValueAsDef - This method looks up the specified field and returns its
1359 /// value as a Record, throwing an exception if the field does not exist or if
1360 /// the value is not the right type.
1362 Record
*getValueAsDef(const std::string
&FieldName
) const;
1364 /// getValueAsBit - This method looks up the specified field and returns its
1365 /// value as a bit, throwing an exception if the field does not exist or if
1366 /// the value is not the right type.
1368 bool getValueAsBit(const std::string
&FieldName
) const;
1370 /// getValueAsInt - This method looks up the specified field and returns its
1371 /// value as an int64_t, throwing an exception if the field does not exist or
1372 /// if the value is not the right type.
1374 int64_t getValueAsInt(const std::string
&FieldName
) const;
1376 /// getValueAsDag - This method looks up the specified field and returns its
1377 /// value as an Dag, throwing an exception if the field does not exist or if
1378 /// the value is not the right type.
1380 DagInit
*getValueAsDag(const std::string
&FieldName
) const;
1382 /// getValueAsCode - This method looks up the specified field and returns
1383 /// its value as the string data in a CodeInit, throwing an exception if the
1384 /// field does not exist or if the value is not a code object.
1386 std::string
getValueAsCode(const std::string
&FieldName
) const;
1389 raw_ostream
&operator<<(raw_ostream
&OS
, const Record
&R
);
1392 Record Rec
; // Placeholder for template args and Name.
1393 typedef std::vector
<Record
*> RecordVector
;
1394 RecordVector DefPrototypes
;
1398 MultiClass(const std::string
&Name
, SMLoc Loc
) : Rec(Name
, Loc
) {}
1401 class RecordKeeper
{
1402 std::map
<std::string
, Record
*> Classes
, Defs
;
1405 for (std::map
<std::string
, Record
*>::iterator I
= Classes
.begin(),
1406 E
= Classes
.end(); I
!= E
; ++I
)
1408 for (std::map
<std::string
, Record
*>::iterator I
= Defs
.begin(),
1409 E
= Defs
.end(); I
!= E
; ++I
)
1413 const std::map
<std::string
, Record
*> &getClasses() const { return Classes
; }
1414 const std::map
<std::string
, Record
*> &getDefs() const { return Defs
; }
1416 Record
*getClass(const std::string
&Name
) const {
1417 std::map
<std::string
, Record
*>::const_iterator I
= Classes
.find(Name
);
1418 return I
== Classes
.end() ? 0 : I
->second
;
1420 Record
*getDef(const std::string
&Name
) const {
1421 std::map
<std::string
, Record
*>::const_iterator I
= Defs
.find(Name
);
1422 return I
== Defs
.end() ? 0 : I
->second
;
1424 void addClass(Record
*R
) {
1425 assert(getClass(R
->getName()) == 0 && "Class already exists!");
1426 Classes
.insert(std::make_pair(R
->getName(), R
));
1428 void addDef(Record
*R
) {
1429 assert(getDef(R
->getName()) == 0 && "Def already exists!");
1430 Defs
.insert(std::make_pair(R
->getName(), R
));
1433 /// removeClass - Remove, but do not delete, the specified record.
1435 void removeClass(const std::string
&Name
) {
1436 assert(Classes
.count(Name
) && "Class does not exist!");
1437 Classes
.erase(Name
);
1439 /// removeDef - Remove, but do not delete, the specified record.
1441 void removeDef(const std::string
&Name
) {
1442 assert(Defs
.count(Name
) && "Def does not exist!");
1446 //===--------------------------------------------------------------------===//
1447 // High-level helper methods, useful for tablegen backends...
1449 /// getAllDerivedDefinitions - This method returns all concrete definitions
1450 /// that derive from the specified class name. If a class with the specified
1451 /// name does not exist, an exception is thrown.
1452 std::vector
<Record
*>
1453 getAllDerivedDefinitions(const std::string
&ClassName
) const;
1459 /// LessRecord - Sorting predicate to sort record pointers by name.
1462 bool operator()(const Record
*Rec1
, const Record
*Rec2
) const {
1463 return Rec1
->getName() < Rec2
->getName();
1467 /// LessRecordFieldName - Sorting predicate to sort record pointers by their
1470 struct LessRecordFieldName
{
1471 bool operator()(const Record
*Rec1
, const Record
*Rec2
) const {
1472 return Rec1
->getValueAsString("Name") < Rec2
->getValueAsString("Name");
1479 std::string Message
;
1481 TGError(SMLoc loc
, const std::string
&message
) : Loc(loc
), Message(message
) {}
1483 SMLoc
getLoc() const { return Loc
; }
1484 const std::string
&getMessage() const { return Message
; }
1488 raw_ostream
&operator<<(raw_ostream
&OS
, const RecordKeeper
&RK
);
1490 extern RecordKeeper Records
;
1492 void PrintError(SMLoc ErrorLoc
, const std::string
&Msg
);
1495 } // End llvm namespace