blob: 5e88e50d3e40e4cb6f0850c99cbb30fca10728ff [file] [log] [blame]
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001//===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
John Criswelld3032032003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
John Criswelld3032032003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00009//
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 RECORD_H
16#define RECORD_H
17
18#include <string>
19#include <vector>
20#include <map>
Bill Wendling9bfb1e12006-12-07 22:21:48 +000021#include <ostream>
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000022#include <cassert>
23
Brian Gaeke960707c2003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattneref943742005-04-19 03:36:21 +000026// RecTy subclasses.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000027class BitRecTy;
28class BitsRecTy;
29class IntRecTy;
30class StringRecTy;
31class ListRecTy;
32class CodeRecTy;
33class DagRecTy;
34class RecordRecTy;
35
Chris Lattneref943742005-04-19 03:36:21 +000036// Init subclasses.
Chris Lattner7dfc2d22004-10-27 16:14:51 +000037struct Init;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038class UnsetInit;
39class BitInit;
40class BitsInit;
41class IntInit;
42class StringInit;
43class CodeInit;
44class ListInit;
Chris Lattner51ffbf12006-03-31 21:53:49 +000045class BinOpInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000046class DefInit;
47class DagInit;
48class TypedInit;
49class VarInit;
50class FieldInit;
51class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000052class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000053
Chris Lattneref943742005-04-19 03:36:21 +000054// Other classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000055class Record;
Chris Lattneref943742005-04-19 03:36:21 +000056class RecordVal;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000057
58//===----------------------------------------------------------------------===//
59// Type Classes
60//===----------------------------------------------------------------------===//
61
62struct RecTy {
63 virtual ~RecTy() {}
64
65 virtual void print(std::ostream &OS) const = 0;
66 void dump() const;
67
68 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
69 /// converted to the specified type.
70 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
71
72public: // These methods should only be called from subclasses of Init
73 virtual Init *convertValue( UnsetInit *UI) { return 0; }
74 virtual Init *convertValue( BitInit *BI) { return 0; }
75 virtual Init *convertValue( BitsInit *BI) { return 0; }
76 virtual Init *convertValue( IntInit *II) { return 0; }
77 virtual Init *convertValue(StringInit *SI) { return 0; }
78 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +000079 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000080 virtual Init *convertValue( CodeInit *CI) { return 0; }
81 virtual Init *convertValue(VarBitInit *VB) { return 0; }
82 virtual Init *convertValue( DefInit *DI) { return 0; }
83 virtual Init *convertValue( DagInit *DI) { return 0; }
84 virtual Init *convertValue( TypedInit *TI) { return 0; }
85 virtual Init *convertValue( VarInit *VI) {
86 return convertValue((TypedInit*)VI);
87 }
88 virtual Init *convertValue( FieldInit *FI) {
89 return convertValue((TypedInit*)FI);
90 }
91
92public: // These methods should only be called by subclasses of RecTy.
93 // baseClassOf - These virtual methods should be overloaded to return true iff
94 // all values of type 'RHS' can be converted to the 'this' type.
95 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
96 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
97 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
103};
104
105inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
106 Ty.print(OS);
107 return OS;
108}
109
110
111/// BitRecTy - 'bit' - Represent a single bit
112///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000113class BitRecTy : public RecTy {
114public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000115 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
116 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
117 virtual Init *convertValue( BitsInit *BI);
118 virtual Init *convertValue( IntInit *II);
119 virtual Init *convertValue(StringInit *SI) { return 0; }
120 virtual Init *convertValue( ListInit *LI) { return 0; }
121 virtual Init *convertValue( CodeInit *CI) { return 0; }
122 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
123 virtual Init *convertValue( DefInit *DI) { return 0; }
124 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000125 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000126 virtual Init *convertValue( TypedInit *TI);
127 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
128 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000129
130 void print(std::ostream &OS) const { OS << "bit"; }
131
132 bool typeIsConvertibleTo(const RecTy *RHS) const {
133 return RHS->baseClassOf(this);
134 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000135 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
136 virtual bool baseClassOf(const BitsRecTy *RHS) const;
137 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
138 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
139 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
140 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
143
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000144};
145
146
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000147// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
148/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000149///
150class BitsRecTy : public RecTy {
151 unsigned Size;
152public:
153 BitsRecTy(unsigned Sz) : Size(Sz) {}
154
155 unsigned getNumBits() const { return Size; }
156
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000157 virtual Init *convertValue( UnsetInit *UI);
158 virtual Init *convertValue( BitInit *UI);
159 virtual Init *convertValue( BitsInit *BI);
160 virtual Init *convertValue( IntInit *II);
161 virtual Init *convertValue(StringInit *SI) { return 0; }
162 virtual Init *convertValue( ListInit *LI) { return 0; }
163 virtual Init *convertValue( CodeInit *CI) { return 0; }
164 virtual Init *convertValue(VarBitInit *VB) { return 0; }
165 virtual Init *convertValue( DefInit *DI) { return 0; }
166 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000167 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000168 virtual Init *convertValue( TypedInit *TI);
169 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
170 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
171
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000172
173 void print(std::ostream &OS) const { OS << "bits<" << Size << ">"; }
174
175 bool typeIsConvertibleTo(const RecTy *RHS) const {
176 return RHS->baseClassOf(this);
177 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000178 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
179 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000180 return RHS->Size == Size;
181 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000182 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
183 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
184 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
185 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
188
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000189};
190
191
192/// IntRecTy - 'int' - Represent an integer value of no particular size
193///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000194class IntRecTy : public RecTy {
195public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000196 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
197 virtual Init *convertValue( BitInit *BI);
198 virtual Init *convertValue( BitsInit *BI);
199 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
200 virtual Init *convertValue(StringInit *SI) { return 0; }
201 virtual Init *convertValue( ListInit *LI) { return 0; }
202 virtual Init *convertValue( CodeInit *CI) { return 0; }
203 virtual Init *convertValue(VarBitInit *VB) { return 0; }
204 virtual Init *convertValue( DefInit *DI) { return 0; }
205 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000206 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000207 virtual Init *convertValue( TypedInit *TI);
208 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
209 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
210
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000211
212 void print(std::ostream &OS) const { OS << "int"; }
213
214 bool typeIsConvertibleTo(const RecTy *RHS) const {
215 return RHS->baseClassOf(this);
216 }
217
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000218 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
219 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
221 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
222 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
225 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
226
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000227};
228
229/// StringRecTy - 'string' - Represent an string value
230///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000231class StringRecTy : public RecTy {
232public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000233 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
234 virtual Init *convertValue( BitInit *BI) { return 0; }
235 virtual Init *convertValue( BitsInit *BI) { return 0; }
236 virtual Init *convertValue( IntInit *II) { return 0; }
237 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
238 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000239 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000240 virtual Init *convertValue( CodeInit *CI) { return 0; }
241 virtual Init *convertValue(VarBitInit *VB) { return 0; }
242 virtual Init *convertValue( DefInit *DI) { return 0; }
243 virtual Init *convertValue( DagInit *DI) { return 0; }
244 virtual Init *convertValue( TypedInit *TI);
245 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
246 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
247
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000248 void print(std::ostream &OS) const { OS << "string"; }
249
250 bool typeIsConvertibleTo(const RecTy *RHS) const {
251 return RHS->baseClassOf(this);
252 }
253
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000254 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
255 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000257 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000258 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
261 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000262};
263
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000264// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
265// the specified type.
266/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
267/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000268///
269class ListRecTy : public RecTy {
270 RecTy *Ty;
271public:
272 ListRecTy(RecTy *T) : Ty(T) {}
273
274 RecTy *getElementType() const { return Ty; }
275
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000276 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
277 virtual Init *convertValue( BitInit *BI) { return 0; }
278 virtual Init *convertValue( BitsInit *BI) { return 0; }
279 virtual Init *convertValue( IntInit *II) { return 0; }
280 virtual Init *convertValue(StringInit *SI) { return 0; }
281 virtual Init *convertValue( ListInit *LI);
282 virtual Init *convertValue( CodeInit *CI) { return 0; }
283 virtual Init *convertValue(VarBitInit *VB) { return 0; }
284 virtual Init *convertValue( DefInit *DI) { return 0; }
285 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000286 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000287 virtual Init *convertValue( TypedInit *TI);
288 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
289 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000290
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000291 void print(std::ostream &OS) const;
292
293 bool typeIsConvertibleTo(const RecTy *RHS) const {
294 return RHS->baseClassOf(this);
295 }
296
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000297 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
301 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000302 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000303 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000304 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
305 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
306 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000307};
308
309/// CodeRecTy - 'code' - Represent an code fragment, function or method.
310///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000311class CodeRecTy : public RecTy {
312public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000313 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
314 virtual Init *convertValue( BitInit *BI) { return 0; }
315 virtual Init *convertValue( BitsInit *BI) { return 0; }
316 virtual Init *convertValue( IntInit *II) { return 0; }
317 virtual Init *convertValue(StringInit *SI) { return 0; }
318 virtual Init *convertValue( ListInit *LI) { return 0; }
319 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
320 virtual Init *convertValue(VarBitInit *VB) { return 0; }
321 virtual Init *convertValue( DefInit *DI) { return 0; }
322 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000323 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000324 virtual Init *convertValue( TypedInit *TI);
325 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
326 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
327
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000328
329 void print(std::ostream &OS) const { OS << "code"; }
330
331 bool typeIsConvertibleTo(const RecTy *RHS) const {
332 return RHS->baseClassOf(this);
333 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000334 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
338 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
339 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
340 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
341 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000342};
343
344/// DagRecTy - 'dag' - Represent a dag fragment
345///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000346class DagRecTy : public RecTy {
347public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000348 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
349 virtual Init *convertValue( BitInit *BI) { return 0; }
350 virtual Init *convertValue( BitsInit *BI) { return 0; }
351 virtual Init *convertValue( IntInit *II) { return 0; }
352 virtual Init *convertValue(StringInit *SI) { return 0; }
353 virtual Init *convertValue( ListInit *LI) { return 0; }
354 virtual Init *convertValue( CodeInit *CI) { return 0; }
355 virtual Init *convertValue(VarBitInit *VB) { return 0; }
356 virtual Init *convertValue( DefInit *DI) { return 0; }
Evan Chenga32dee22007-05-15 01:23:24 +0000357 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000358 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
359 virtual Init *convertValue( TypedInit *TI);
360 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
361 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362
363 void print(std::ostream &OS) const { OS << "dag"; }
364
365 bool typeIsConvertibleTo(const RecTy *RHS) const {
366 return RHS->baseClassOf(this);
367 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000368
369 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
375 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
376 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000377};
378
379
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000380/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000381/// (R32 X = EAX).
382///
383class RecordRecTy : public RecTy {
384 Record *Rec;
385public:
386 RecordRecTy(Record *R) : Rec(R) {}
387
388 Record *getRecord() const { return Rec; }
389
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000390 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
391 virtual Init *convertValue( BitInit *BI) { return 0; }
392 virtual Init *convertValue( BitsInit *BI) { return 0; }
393 virtual Init *convertValue( IntInit *II) { return 0; }
394 virtual Init *convertValue(StringInit *SI) { return 0; }
395 virtual Init *convertValue( ListInit *LI) { return 0; }
396 virtual Init *convertValue( CodeInit *CI) { return 0; }
397 virtual Init *convertValue(VarBitInit *VB) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000398 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000399 virtual Init *convertValue( DefInit *DI);
400 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000401 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000402 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
403 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404
405 void print(std::ostream &OS) const;
406
407 bool typeIsConvertibleTo(const RecTy *RHS) const {
408 return RHS->baseClassOf(this);
409 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000410 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000417 virtual bool baseClassOf(const RecordRecTy *RHS) const;
418};
419
420
421
422//===----------------------------------------------------------------------===//
423// Initializer Classes
424//===----------------------------------------------------------------------===//
425
426struct Init {
427 virtual ~Init() {}
428
429 /// isComplete - This virtual method should be overridden by values that may
430 /// not be completely specified yet.
431 virtual bool isComplete() const { return true; }
432
433 /// print - Print out this value.
434 virtual void print(std::ostream &OS) const = 0;
435
436 /// dump - Debugging method that may be called through a debugger, just
437 /// invokes print on cerr.
438 void dump() const;
439
440 /// convertInitializerTo - This virtual function is a simple call-back
441 /// function that should be overridden to call the appropriate
442 /// RecTy::convertValue method.
443 ///
444 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
445
446 /// convertInitializerBitRange - This method is used to implement the bitrange
447 /// selection operator. Given an initializer, it selects the specified bits
448 /// out, returning them as a new init of bits type. If it is not legal to use
449 /// the bit subscript operator on this initializer, return null.
450 ///
451 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
452 return 0;
453 }
454
Chris Lattner8bf9e062004-07-26 23:21:34 +0000455 /// convertInitListSlice - This method is used to implement the list slice
456 /// selection operator. Given an initializer, it selects the specified list
457 /// elements, returning them as a new init of list type. If it is not legal
458 /// to take a slice of this, return null.
459 ///
460 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
461 return 0;
462 }
463
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000464 /// getFieldType - This method is used to implement the FieldInit class.
465 /// Implementors of this method should return the type of the named field if
466 /// they are of record type.
467 ///
468 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
469
470 /// getFieldInit - This method complements getFieldType to return the
471 /// initializer for the specified field. If getFieldType returns non-null
472 /// this method should return non-null, otherwise it returns null.
473 ///
474 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
475 return 0;
476 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000477
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000478 /// resolveReferences - This method is used by classes that refer to other
479 /// variables which may not be defined at the time they expression is formed.
480 /// If a value is set for the variable later, this method will be called on
481 /// users of the value to allow the value to propagate out.
482 ///
Chris Lattneref943742005-04-19 03:36:21 +0000483 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
484 return this;
485 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000486};
487
488inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
489 I.print(OS); return OS;
490}
491
492
493/// UnsetInit - ? - Represents an uninitialized value
494///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000495class UnsetInit : public Init {
496public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000497 virtual Init *convertInitializerTo(RecTy *Ty) {
498 return Ty->convertValue(this);
499 }
500
501 virtual bool isComplete() const { return false; }
502 virtual void print(std::ostream &OS) const { OS << "?"; }
503};
504
505
506/// BitInit - true/false - Represent a concrete initializer for a bit.
507///
508class BitInit : public Init {
509 bool Value;
510public:
511 BitInit(bool V) : Value(V) {}
512
513 bool getValue() const { return Value; }
514
515 virtual Init *convertInitializerTo(RecTy *Ty) {
516 return Ty->convertValue(this);
517 }
518
519 virtual void print(std::ostream &OS) const { OS << (Value ? "1" : "0"); }
520};
521
522/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
523/// It contains a vector of bits, whose size is determined by the type.
524///
525class BitsInit : public Init {
526 std::vector<Init*> Bits;
527public:
528 BitsInit(unsigned Size) : Bits(Size) {}
529
530 unsigned getNumBits() const { return Bits.size(); }
531
532 Init *getBit(unsigned Bit) const {
533 assert(Bit < Bits.size() && "Bit index out of range!");
534 return Bits[Bit];
535 }
536 void setBit(unsigned Bit, Init *V) {
537 assert(Bit < Bits.size() && "Bit index out of range!");
538 assert(Bits[Bit] == 0 && "Bit already set!");
539 Bits[Bit] = V;
540 }
541
542 virtual Init *convertInitializerTo(RecTy *Ty) {
543 return Ty->convertValue(this);
544 }
545 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
546
547 virtual bool isComplete() const {
548 for (unsigned i = 0; i != getNumBits(); ++i)
549 if (!getBit(i)->isComplete()) return false;
550 return true;
551 }
552 virtual void print(std::ostream &OS) const;
553
Chris Lattneref943742005-04-19 03:36:21 +0000554 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000555
556 // printXX - Print this bitstream with the specified format, returning true if
557 // it is not possible.
558 bool printInHex(std::ostream &OS) const;
559 bool printAsVariable(std::ostream &OS) const;
560 bool printAsUnset(std::ostream &OS) const;
561};
562
563
564/// IntInit - 7 - Represent an initalization by a literal integer value.
565///
566class IntInit : public Init {
567 int Value;
568public:
569 IntInit(int V) : Value(V) {}
570
571 int getValue() const { return Value; }
572
573 virtual Init *convertInitializerTo(RecTy *Ty) {
574 return Ty->convertValue(this);
575 }
576 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
577
578 virtual void print(std::ostream &OS) const { OS << Value; }
579};
580
581
582/// StringInit - "foo" - Represent an initialization by a string value.
583///
584class StringInit : public Init {
585 std::string Value;
586public:
587 StringInit(const std::string &V) : Value(V) {}
588
589 const std::string &getValue() const { return Value; }
590
591 virtual Init *convertInitializerTo(RecTy *Ty) {
592 return Ty->convertValue(this);
593 }
594
595 virtual void print(std::ostream &OS) const { OS << "\"" << Value << "\""; }
596};
597
598/// CodeInit - "[{...}]" - Represent a code fragment.
599///
600class CodeInit : public Init {
601 std::string Value;
602public:
603 CodeInit(const std::string &V) : Value(V) {}
604
605 const std::string getValue() const { return Value; }
606
607 virtual Init *convertInitializerTo(RecTy *Ty) {
608 return Ty->convertValue(this);
609 }
610
611 virtual void print(std::ostream &OS) const { OS << "[{" << Value << "}]"; }
612};
613
614/// ListInit - [AL, AH, CL] - Represent a list of defs
615///
616class ListInit : public Init {
617 std::vector<Init*> Values;
618public:
619 ListInit(std::vector<Init*> &Vs) {
620 Values.swap(Vs);
621 }
622
623 unsigned getSize() const { return Values.size(); }
624 Init *getElement(unsigned i) const {
625 assert(i < Values.size() && "List element index out of range!");
626 return Values[i];
627 }
628
Chris Lattnercbebe462007-02-27 22:08:27 +0000629 Record *getElementAsRecord(unsigned i) const;
630
Chris Lattner8bf9e062004-07-26 23:21:34 +0000631 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
632
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000633 virtual Init *convertInitializerTo(RecTy *Ty) {
634 return Ty->convertValue(this);
635 }
636
Chris Lattner577fc3f2004-07-27 01:01:21 +0000637 /// resolveReferences - This method is used by classes that refer to other
638 /// variables which may not be defined at the time they expression is formed.
639 /// If a value is set for the variable later, this method will be called on
640 /// users of the value to allow the value to propagate out.
641 ///
Chris Lattneref943742005-04-19 03:36:21 +0000642 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000643
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000644 virtual void print(std::ostream &OS) const;
645};
646
Chris Lattner51ffbf12006-03-31 21:53:49 +0000647/// BinOpInit - !op (X, Y) - Combine two inits.
648///
649class BinOpInit : public Init {
650public:
Evan Chenga32dee22007-05-15 01:23:24 +0000651 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT };
Chris Lattner51ffbf12006-03-31 21:53:49 +0000652private:
653 BinaryOp Opc;
654 Init *LHS, *RHS;
655public:
656 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
657 }
658
659 BinaryOp getOpcode() const { return Opc; }
660 Init *getLHS() const { return LHS; }
661 Init *getRHS() const { return RHS; }
662
663 // Fold - If possible, fold this to a simpler init. Return this if not
664 // possible to fold.
665 Init *Fold();
666
667 virtual Init *convertInitializerTo(RecTy *Ty) {
668 return Ty->convertValue(this);
669 }
670
671 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
672
673 virtual void print(std::ostream &OS) const;
674};
675
676
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000677
678/// TypedInit - This is the common super-class of types that have a specific,
679/// explicit, type.
680///
681class TypedInit : public Init {
682 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000683public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000684 TypedInit(RecTy *T) : Ty(T) {}
685
686 RecTy *getType() const { return Ty; }
687
Chris Lattner577fc3f2004-07-27 01:01:21 +0000688 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
689 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
690
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000691 /// resolveBitReference - This method is used to implement
692 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000693 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000694 ///
Chris Lattneref943742005-04-19 03:36:21 +0000695 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
696 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000697
698 /// resolveListElementReference - This method is used to implement
699 /// VarListElementInit::resolveReferences. If the list element is resolvable
700 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000701 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
702 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000703};
704
705/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
706///
707class VarInit : public TypedInit {
708 std::string VarName;
709public:
710 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000711
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000712 virtual Init *convertInitializerTo(RecTy *Ty) {
713 return Ty->convertValue(this);
714 }
715
716 const std::string &getName() const { return VarName; }
717
Chris Lattneref943742005-04-19 03:36:21 +0000718 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
719 unsigned Bit);
720 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
721 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000722
723 virtual RecTy *getFieldType(const std::string &FieldName) const;
724 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
725
726 /// resolveReferences - This method is used by classes that refer to other
727 /// variables which may not be defined at the time they expression is formed.
728 /// If a value is set for the variable later, this method will be called on
729 /// users of the value to allow the value to propagate out.
730 ///
Chris Lattneref943742005-04-19 03:36:21 +0000731 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000732
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000733 virtual void print(std::ostream &OS) const { OS << VarName; }
734};
735
736
737/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
738///
739class VarBitInit : public Init {
740 TypedInit *TI;
741 unsigned Bit;
742public:
743 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
744 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
745 ((BitsRecTy*)T->getType())->getNumBits() > B &&
746 "Illegal VarBitInit expression!");
747 }
748
749 virtual Init *convertInitializerTo(RecTy *Ty) {
750 return Ty->convertValue(this);
751 }
752
753 TypedInit *getVariable() const { return TI; }
754 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000755
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000756 virtual void print(std::ostream &OS) const {
757 TI->print(OS); OS << "{" << Bit << "}";
758 }
Chris Lattneref943742005-04-19 03:36:21 +0000759 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000760};
761
Misha Brukman650ba8e2005-04-22 00:00:37 +0000762/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000763/// field.
764class VarListElementInit : public TypedInit {
765 TypedInit *TI;
766 unsigned Element;
767public:
768 VarListElementInit(TypedInit *T, unsigned E)
769 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
770 TI(T), Element(E) {
771 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
772 "Illegal VarBitInit expression!");
773 }
774
775 virtual Init *convertInitializerTo(RecTy *Ty) {
776 return Ty->convertValue(this);
777 }
778
779 TypedInit *getVariable() const { return TI; }
780 unsigned getElementNum() const { return Element; }
781
Chris Lattneref943742005-04-19 03:36:21 +0000782 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
783 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000784
785 /// resolveListElementReference - This method is used to implement
786 /// VarListElementInit::resolveReferences. If the list element is resolvable
787 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000788 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
789 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000790
791 virtual void print(std::ostream &OS) const {
792 TI->print(OS); OS << "[" << Element << "]";
793 }
Chris Lattneref943742005-04-19 03:36:21 +0000794 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000795};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000796
797/// DefInit - AL - Represent a reference to a 'def' in the description
798///
799class DefInit : public Init {
800 Record *Def;
801public:
802 DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000803
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000804 virtual Init *convertInitializerTo(RecTy *Ty) {
805 return Ty->convertValue(this);
806 }
807
808 Record *getDef() const { return Def; }
809
810 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
811
812 virtual RecTy *getFieldType(const std::string &FieldName) const;
813 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000814
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000815 virtual void print(std::ostream &OS) const;
816};
817
818
819/// FieldInit - X.Y - Represent a reference to a subfield of a variable
820///
821class FieldInit : public TypedInit {
822 Init *Rec; // Record we are referring to
823 std::string FieldName; // Field we are accessing
824public:
825 FieldInit(Init *R, const std::string &FN)
826 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
827 assert(getType() && "FieldInit with non-record type!");
828 }
829
830 virtual Init *convertInitializerTo(RecTy *Ty) {
831 return Ty->convertValue(this);
832 }
833
Chris Lattneref943742005-04-19 03:36:21 +0000834 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
835 unsigned Bit);
836 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
837 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000838
Chris Lattneref943742005-04-19 03:36:21 +0000839 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000840
841 virtual void print(std::ostream &OS) const {
842 Rec->print(OS); OS << "." << FieldName;
843 }
844};
845
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000846/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
847/// to have at least one value then a (possibly empty) list of arguments. Each
848/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000849///
850class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000851 Init *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000852 std::vector<Init*> Args;
853 std::vector<std::string> ArgNames;
854public:
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000855 DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
856 : Val(V) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000857 Args.reserve(args.size());
858 ArgNames.reserve(args.size());
859 for (unsigned i = 0, e = args.size(); i != e; ++i) {
860 Args.push_back(args[i].first);
861 ArgNames.push_back(args[i].second);
862 }
863 }
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000864 DagInit(Init *V, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +0000865 const std::vector<std::string> &argNames)
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000866 : Val(V), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +0000867 }
868
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000869 virtual Init *convertInitializerTo(RecTy *Ty) {
870 return Ty->convertValue(this);
871 }
872
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000873 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000874
875 unsigned getNumArgs() const { return Args.size(); }
876 Init *getArg(unsigned Num) const {
877 assert(Num < Args.size() && "Arg number out of range!");
878 return Args[Num];
879 }
880 const std::string &getArgName(unsigned Num) const {
881 assert(Num < ArgNames.size() && "Arg number out of range!");
882 return ArgNames[Num];
883 }
884
885 void setArg(unsigned Num, Init *I) {
886 assert(Num < Args.size() && "Arg number out of range!");
887 Args[Num] = I;
888 }
Chris Lattner0d3ef402006-01-31 06:02:35 +0000889
890 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000891
892 virtual void print(std::ostream &OS) const;
893};
894
895//===----------------------------------------------------------------------===//
896// High-Level Classes
897//===----------------------------------------------------------------------===//
898
899class RecordVal {
900 std::string Name;
901 RecTy *Ty;
902 unsigned Prefix;
903 Init *Value;
904public:
905 RecordVal(const std::string &N, RecTy *T, unsigned P);
906
907 const std::string &getName() const { return Name; }
908
909 unsigned getPrefix() const { return Prefix; }
910 RecTy *getType() const { return Ty; }
911 Init *getValue() const { return Value; }
912
913 bool setValue(Init *V) {
914 if (V) {
915 Value = V->convertInitializerTo(Ty);
916 return Value == 0;
917 }
918 Value = 0;
919 return false;
920 }
921
922 void dump() const;
923 void print(std::ostream &OS, bool PrintSem = true) const;
924};
925
926inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
927 RV.print(OS << " ");
928 return OS;
929}
930
Chris Lattner87a10612004-10-23 04:58:50 +0000931class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +0000932 std::string Name;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000933 std::vector<std::string> TemplateArgs;
934 std::vector<RecordVal> Values;
935 std::vector<Record*> SuperClasses;
936public:
937
938 Record(const std::string &N) : Name(N) {}
939 ~Record() {}
940
941 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +0000942 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000943 const std::vector<std::string> &getTemplateArgs() const {
944 return TemplateArgs;
945 }
946 const std::vector<RecordVal> &getValues() const { return Values; }
947 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
948
949 bool isTemplateArg(const std::string &Name) const {
950 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
951 if (TemplateArgs[i] == Name) return true;
952 return false;
953 }
954
955 const RecordVal *getValue(const std::string &Name) const {
956 for (unsigned i = 0, e = Values.size(); i != e; ++i)
957 if (Values[i].getName() == Name) return &Values[i];
958 return 0;
959 }
960 RecordVal *getValue(const std::string &Name) {
961 for (unsigned i = 0, e = Values.size(); i != e; ++i)
962 if (Values[i].getName() == Name) return &Values[i];
963 return 0;
964 }
965
966 void addTemplateArg(const std::string &Name) {
967 assert(!isTemplateArg(Name) && "Template arg already defined!");
968 TemplateArgs.push_back(Name);
969 }
970
971 void addValue(const RecordVal &RV) {
972 assert(getValue(RV.getName()) == 0 && "Value already added!");
973 Values.push_back(RV);
974 }
975
976 void removeValue(const std::string &Name) {
977 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
978 for (unsigned i = 0, e = Values.size(); i != e; ++i)
979 if (Values[i].getName() == Name) {
980 Values.erase(Values.begin()+i);
981 return;
982 }
983 assert(0 && "Name does not exist in record!");
984 }
985
986 bool isSubClassOf(Record *R) const {
987 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
988 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000989 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000990 return false;
991 }
992
993 bool isSubClassOf(const std::string &Name) const {
994 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
995 if (SuperClasses[i]->getName() == Name)
996 return true;
997 return false;
998 }
999
1000 void addSuperClass(Record *R) {
1001 assert(!isSubClassOf(R) && "Already subclassing record!");
1002 SuperClasses.push_back(R);
1003 }
1004
Chris Lattneref943742005-04-19 03:36:21 +00001005 /// resolveReferences - If there are any field references that refer to fields
1006 /// that have been filled in, we can propagate the values now.
1007 ///
1008 void resolveReferences() { resolveReferencesTo(0); }
1009
1010 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1011 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1012 /// possible references.
1013 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001014
1015 void dump() const;
1016
1017 //===--------------------------------------------------------------------===//
1018 // High-level methods useful to tablegen back-ends
1019 //
1020
1021 /// getValueInit - Return the initializer for a value with the specified name,
1022 /// or throw an exception if the field does not exist.
1023 ///
1024 Init *getValueInit(const std::string &FieldName) const;
1025
1026 /// getValueAsString - This method looks up the specified field and returns
1027 /// its value as a string, throwing an exception if the field does not exist
1028 /// or if the value is not a string.
1029 ///
1030 std::string getValueAsString(const std::string &FieldName) const;
1031
1032 /// getValueAsBitsInit - This method looks up the specified field and returns
1033 /// its value as a BitsInit, throwing an exception if the field does not exist
1034 /// or if the value is not the right type.
1035 ///
1036 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1037
1038 /// getValueAsListInit - This method looks up the specified field and returns
1039 /// its value as a ListInit, throwing an exception if the field does not exist
1040 /// or if the value is not the right type.
1041 ///
1042 ListInit *getValueAsListInit(const std::string &FieldName) const;
1043
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001044 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001045 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001046 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001047 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001048 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001049
Anton Korobeynikova468a112007-11-11 11:19:37 +00001050 /// getValueAsListOfInts - This method looks up the specified field and returns
1051 /// its value as a vector of integers, throwing an exception if the field does
1052 /// not exist or if the value is not the right type.
1053 ///
1054 std::vector<int> getValueAsListOfInts(const std::string &FieldName) const;
1055
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001056 /// getValueAsDef - This method looks up the specified field and returns its
1057 /// value as a Record, throwing an exception if the field does not exist or if
1058 /// the value is not the right type.
1059 ///
1060 Record *getValueAsDef(const std::string &FieldName) const;
1061
1062 /// getValueAsBit - This method looks up the specified field and returns its
1063 /// value as a bit, throwing an exception if the field does not exist or if
1064 /// the value is not the right type.
1065 ///
1066 bool getValueAsBit(const std::string &FieldName) const;
1067
1068 /// getValueAsInt - This method looks up the specified field and returns its
1069 /// value as an int, throwing an exception if the field does not exist or if
1070 /// the value is not the right type.
1071 ///
1072 int getValueAsInt(const std::string &FieldName) const;
1073
1074 /// getValueAsDag - This method looks up the specified field and returns its
1075 /// value as an Dag, throwing an exception if the field does not exist or if
1076 /// the value is not the right type.
1077 ///
1078 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001079
1080 /// getValueAsCode - This method looks up the specified field and returns
1081 /// its value as the string data in a CodeInit, throwing an exception if the
1082 /// field does not exist or if the value is not a code object.
1083 ///
1084 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001085};
1086
1087std::ostream &operator<<(std::ostream &OS, const Record &R);
1088
1089class RecordKeeper {
1090 std::map<std::string, Record*> Classes, Defs;
1091public:
1092 ~RecordKeeper() {
1093 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001094 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001095 delete I->second;
1096 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001097 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001098 delete I->second;
1099 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001100
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001101 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1102 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1103
1104 Record *getClass(const std::string &Name) const {
1105 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1106 return I == Classes.end() ? 0 : I->second;
1107 }
1108 Record *getDef(const std::string &Name) const {
1109 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1110 return I == Defs.end() ? 0 : I->second;
1111 }
1112 void addClass(Record *R) {
1113 assert(getClass(R->getName()) == 0 && "Class already exists!");
1114 Classes.insert(std::make_pair(R->getName(), R));
1115 }
1116 void addDef(Record *R) {
1117 assert(getDef(R->getName()) == 0 && "Def already exists!");
1118 Defs.insert(std::make_pair(R->getName(), R));
1119 }
1120
Chris Lattnerac284252005-08-19 17:58:11 +00001121 /// removeClass - Remove, but do not delete, the specified record.
1122 ///
1123 void removeClass(const std::string &Name) {
1124 assert(Classes.count(Name) && "Class does not exist!");
1125 Classes.erase(Name);
1126 }
1127 /// removeDef - Remove, but do not delete, the specified record.
1128 ///
1129 void removeDef(const std::string &Name) {
1130 assert(Defs.count(Name) && "Def does not exist!");
1131 Defs.erase(Name);
1132 }
1133
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001134 //===--------------------------------------------------------------------===//
1135 // High-level helper methods, useful for tablegen backends...
1136
1137 /// getAllDerivedDefinitions - This method returns all concrete definitions
1138 /// that derive from the specified class name. If a class with the specified
1139 /// name does not exist, an exception is thrown.
1140 std::vector<Record*>
1141 getAllDerivedDefinitions(const std::string &ClassName) const;
1142
1143
1144 void dump() const;
1145};
1146
1147std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1148
1149extern RecordKeeper Records;
1150
Brian Gaeke960707c2003-11-11 22:41:34 +00001151} // End llvm namespace
1152
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001153#endif