blob: 84e129071fd18b75e9a10669c659f7bf7afd6d07 [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>
21#include <iostream>
22#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; }
Reid Spencer97c59802006-08-28 00:12:25 +0000357 virtual Init *convertValue( BinOpInit *UI) { return 0; }
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 Lattner8bf9e062004-07-26 23:21:34 +0000629 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
630
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000631 virtual Init *convertInitializerTo(RecTy *Ty) {
632 return Ty->convertValue(this);
633 }
634
Chris Lattner577fc3f2004-07-27 01:01:21 +0000635 /// resolveReferences - This method is used by classes that refer to other
636 /// variables which may not be defined at the time they expression is formed.
637 /// If a value is set for the variable later, this method will be called on
638 /// users of the value to allow the value to propagate out.
639 ///
Chris Lattneref943742005-04-19 03:36:21 +0000640 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000641
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000642 virtual void print(std::ostream &OS) const;
643};
644
Chris Lattner51ffbf12006-03-31 21:53:49 +0000645/// BinOpInit - !op (X, Y) - Combine two inits.
646///
647class BinOpInit : public Init {
648public:
649 enum BinaryOp { SHL, SRA, SRL, STRCONCAT };
650private:
651 BinaryOp Opc;
652 Init *LHS, *RHS;
653public:
654 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
655 }
656
657 BinaryOp getOpcode() const { return Opc; }
658 Init *getLHS() const { return LHS; }
659 Init *getRHS() const { return RHS; }
660
661 // Fold - If possible, fold this to a simpler init. Return this if not
662 // possible to fold.
663 Init *Fold();
664
665 virtual Init *convertInitializerTo(RecTy *Ty) {
666 return Ty->convertValue(this);
667 }
668
669 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
670
671 virtual void print(std::ostream &OS) const;
672};
673
674
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000675
676/// TypedInit - This is the common super-class of types that have a specific,
677/// explicit, type.
678///
679class TypedInit : public Init {
680 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000681public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000682 TypedInit(RecTy *T) : Ty(T) {}
683
684 RecTy *getType() const { return Ty; }
685
Chris Lattner577fc3f2004-07-27 01:01:21 +0000686 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
687 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
688
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000689 /// resolveBitReference - This method is used to implement
690 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000691 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000692 ///
Chris Lattneref943742005-04-19 03:36:21 +0000693 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
694 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000695
696 /// resolveListElementReference - This method is used to implement
697 /// VarListElementInit::resolveReferences. If the list element is resolvable
698 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000699 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
700 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000701};
702
703/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
704///
705class VarInit : public TypedInit {
706 std::string VarName;
707public:
708 VarInit(const std::string &VN, RecTy *T) : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000709
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000710 virtual Init *convertInitializerTo(RecTy *Ty) {
711 return Ty->convertValue(this);
712 }
713
714 const std::string &getName() const { return VarName; }
715
Chris Lattneref943742005-04-19 03:36:21 +0000716 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
717 unsigned Bit);
718 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
719 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000720
721 virtual RecTy *getFieldType(const std::string &FieldName) const;
722 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
723
724 /// resolveReferences - This method is used by classes that refer to other
725 /// variables which may not be defined at the time they expression is formed.
726 /// If a value is set for the variable later, this method will be called on
727 /// users of the value to allow the value to propagate out.
728 ///
Chris Lattneref943742005-04-19 03:36:21 +0000729 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000730
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000731 virtual void print(std::ostream &OS) const { OS << VarName; }
732};
733
734
735/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
736///
737class VarBitInit : public Init {
738 TypedInit *TI;
739 unsigned Bit;
740public:
741 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
742 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
743 ((BitsRecTy*)T->getType())->getNumBits() > B &&
744 "Illegal VarBitInit expression!");
745 }
746
747 virtual Init *convertInitializerTo(RecTy *Ty) {
748 return Ty->convertValue(this);
749 }
750
751 TypedInit *getVariable() const { return TI; }
752 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000753
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000754 virtual void print(std::ostream &OS) const {
755 TI->print(OS); OS << "{" << Bit << "}";
756 }
Chris Lattneref943742005-04-19 03:36:21 +0000757 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000758};
759
Misha Brukman650ba8e2005-04-22 00:00:37 +0000760/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000761/// field.
762class VarListElementInit : public TypedInit {
763 TypedInit *TI;
764 unsigned Element;
765public:
766 VarListElementInit(TypedInit *T, unsigned E)
767 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
768 TI(T), Element(E) {
769 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
770 "Illegal VarBitInit expression!");
771 }
772
773 virtual Init *convertInitializerTo(RecTy *Ty) {
774 return Ty->convertValue(this);
775 }
776
777 TypedInit *getVariable() const { return TI; }
778 unsigned getElementNum() const { return Element; }
779
Chris Lattneref943742005-04-19 03:36:21 +0000780 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
781 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000782
783 /// resolveListElementReference - This method is used to implement
784 /// VarListElementInit::resolveReferences. If the list element is resolvable
785 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000786 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
787 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000788
789 virtual void print(std::ostream &OS) const {
790 TI->print(OS); OS << "[" << Element << "]";
791 }
Chris Lattneref943742005-04-19 03:36:21 +0000792 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000793};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000794
795/// DefInit - AL - Represent a reference to a 'def' in the description
796///
797class DefInit : public Init {
798 Record *Def;
799public:
800 DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000801
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000802 virtual Init *convertInitializerTo(RecTy *Ty) {
803 return Ty->convertValue(this);
804 }
805
806 Record *getDef() const { return Def; }
807
808 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
809
810 virtual RecTy *getFieldType(const std::string &FieldName) const;
811 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000812
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000813 virtual void print(std::ostream &OS) const;
814};
815
816
817/// FieldInit - X.Y - Represent a reference to a subfield of a variable
818///
819class FieldInit : public TypedInit {
820 Init *Rec; // Record we are referring to
821 std::string FieldName; // Field we are accessing
822public:
823 FieldInit(Init *R, const std::string &FN)
824 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
825 assert(getType() && "FieldInit with non-record type!");
826 }
827
828 virtual Init *convertInitializerTo(RecTy *Ty) {
829 return Ty->convertValue(this);
830 }
831
Chris Lattneref943742005-04-19 03:36:21 +0000832 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
833 unsigned Bit);
834 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
835 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000836
Chris Lattneref943742005-04-19 03:36:21 +0000837 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000838
839 virtual void print(std::ostream &OS) const {
840 Rec->print(OS); OS << "." << FieldName;
841 }
842};
843
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000844/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
845/// to have at least one value then a (possibly empty) list of arguments. Each
846/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000847///
848class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000849 Init *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000850 std::vector<Init*> Args;
851 std::vector<std::string> ArgNames;
852public:
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000853 DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
854 : Val(V) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000855 Args.reserve(args.size());
856 ArgNames.reserve(args.size());
857 for (unsigned i = 0, e = args.size(); i != e; ++i) {
858 Args.push_back(args[i].first);
859 ArgNames.push_back(args[i].second);
860 }
861 }
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000862 DagInit(Init *V, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +0000863 const std::vector<std::string> &argNames)
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000864 : Val(V), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +0000865 }
866
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000867 virtual Init *convertInitializerTo(RecTy *Ty) {
868 return Ty->convertValue(this);
869 }
870
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000871 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000872
873 unsigned getNumArgs() const { return Args.size(); }
874 Init *getArg(unsigned Num) const {
875 assert(Num < Args.size() && "Arg number out of range!");
876 return Args[Num];
877 }
878 const std::string &getArgName(unsigned Num) const {
879 assert(Num < ArgNames.size() && "Arg number out of range!");
880 return ArgNames[Num];
881 }
882
883 void setArg(unsigned Num, Init *I) {
884 assert(Num < Args.size() && "Arg number out of range!");
885 Args[Num] = I;
886 }
Chris Lattner0d3ef402006-01-31 06:02:35 +0000887
888 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000889
890 virtual void print(std::ostream &OS) const;
891};
892
893//===----------------------------------------------------------------------===//
894// High-Level Classes
895//===----------------------------------------------------------------------===//
896
897class RecordVal {
898 std::string Name;
899 RecTy *Ty;
900 unsigned Prefix;
901 Init *Value;
902public:
903 RecordVal(const std::string &N, RecTy *T, unsigned P);
904
905 const std::string &getName() const { return Name; }
906
907 unsigned getPrefix() const { return Prefix; }
908 RecTy *getType() const { return Ty; }
909 Init *getValue() const { return Value; }
910
911 bool setValue(Init *V) {
912 if (V) {
913 Value = V->convertInitializerTo(Ty);
914 return Value == 0;
915 }
916 Value = 0;
917 return false;
918 }
919
920 void dump() const;
921 void print(std::ostream &OS, bool PrintSem = true) const;
922};
923
924inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
925 RV.print(OS << " ");
926 return OS;
927}
928
Chris Lattner87a10612004-10-23 04:58:50 +0000929class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +0000930 std::string Name;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000931 std::vector<std::string> TemplateArgs;
932 std::vector<RecordVal> Values;
933 std::vector<Record*> SuperClasses;
934public:
935
936 Record(const std::string &N) : Name(N) {}
937 ~Record() {}
938
939 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +0000940 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000941 const std::vector<std::string> &getTemplateArgs() const {
942 return TemplateArgs;
943 }
944 const std::vector<RecordVal> &getValues() const { return Values; }
945 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
946
947 bool isTemplateArg(const std::string &Name) const {
948 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
949 if (TemplateArgs[i] == Name) return true;
950 return false;
951 }
952
953 const RecordVal *getValue(const std::string &Name) const {
954 for (unsigned i = 0, e = Values.size(); i != e; ++i)
955 if (Values[i].getName() == Name) return &Values[i];
956 return 0;
957 }
958 RecordVal *getValue(const std::string &Name) {
959 for (unsigned i = 0, e = Values.size(); i != e; ++i)
960 if (Values[i].getName() == Name) return &Values[i];
961 return 0;
962 }
963
964 void addTemplateArg(const std::string &Name) {
965 assert(!isTemplateArg(Name) && "Template arg already defined!");
966 TemplateArgs.push_back(Name);
967 }
968
969 void addValue(const RecordVal &RV) {
970 assert(getValue(RV.getName()) == 0 && "Value already added!");
971 Values.push_back(RV);
972 }
973
974 void removeValue(const std::string &Name) {
975 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
976 for (unsigned i = 0, e = Values.size(); i != e; ++i)
977 if (Values[i].getName() == Name) {
978 Values.erase(Values.begin()+i);
979 return;
980 }
981 assert(0 && "Name does not exist in record!");
982 }
983
984 bool isSubClassOf(Record *R) const {
985 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
986 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +0000987 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000988 return false;
989 }
990
991 bool isSubClassOf(const std::string &Name) const {
992 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
993 if (SuperClasses[i]->getName() == Name)
994 return true;
995 return false;
996 }
997
998 void addSuperClass(Record *R) {
999 assert(!isSubClassOf(R) && "Already subclassing record!");
1000 SuperClasses.push_back(R);
1001 }
1002
Chris Lattneref943742005-04-19 03:36:21 +00001003 /// resolveReferences - If there are any field references that refer to fields
1004 /// that have been filled in, we can propagate the values now.
1005 ///
1006 void resolveReferences() { resolveReferencesTo(0); }
1007
1008 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1009 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1010 /// possible references.
1011 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001012
1013 void dump() const;
1014
1015 //===--------------------------------------------------------------------===//
1016 // High-level methods useful to tablegen back-ends
1017 //
1018
1019 /// getValueInit - Return the initializer for a value with the specified name,
1020 /// or throw an exception if the field does not exist.
1021 ///
1022 Init *getValueInit(const std::string &FieldName) const;
1023
1024 /// getValueAsString - This method looks up the specified field and returns
1025 /// its value as a string, throwing an exception if the field does not exist
1026 /// or if the value is not a string.
1027 ///
1028 std::string getValueAsString(const std::string &FieldName) const;
1029
1030 /// getValueAsBitsInit - This method looks up the specified field and returns
1031 /// its value as a BitsInit, throwing an exception if the field does not exist
1032 /// or if the value is not the right type.
1033 ///
1034 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1035
1036 /// getValueAsListInit - This method looks up the specified field and returns
1037 /// its value as a ListInit, throwing an exception if the field does not exist
1038 /// or if the value is not the right type.
1039 ///
1040 ListInit *getValueAsListInit(const std::string &FieldName) const;
1041
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001042 /// getValueAsListOfDefs - This method looks up the specified field and
1043 /// returnsits value as a vector of records, throwing an exception if the
1044 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001045 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001046 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001047
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001048 /// getValueAsDef - This method looks up the specified field and returns its
1049 /// value as a Record, throwing an exception if the field does not exist or if
1050 /// the value is not the right type.
1051 ///
1052 Record *getValueAsDef(const std::string &FieldName) const;
1053
1054 /// getValueAsBit - This method looks up the specified field and returns its
1055 /// value as a bit, throwing an exception if the field does not exist or if
1056 /// the value is not the right type.
1057 ///
1058 bool getValueAsBit(const std::string &FieldName) const;
1059
1060 /// getValueAsInt - This method looks up the specified field and returns its
1061 /// value as an int, throwing an exception if the field does not exist or if
1062 /// the value is not the right type.
1063 ///
1064 int getValueAsInt(const std::string &FieldName) const;
1065
1066 /// getValueAsDag - This method looks up the specified field and returns its
1067 /// value as an Dag, throwing an exception if the field does not exist or if
1068 /// the value is not the right type.
1069 ///
1070 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001071
1072 /// getValueAsCode - This method looks up the specified field and returns
1073 /// its value as the string data in a CodeInit, throwing an exception if the
1074 /// field does not exist or if the value is not a code object.
1075 ///
1076 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001077};
1078
1079std::ostream &operator<<(std::ostream &OS, const Record &R);
1080
1081class RecordKeeper {
1082 std::map<std::string, Record*> Classes, Defs;
1083public:
1084 ~RecordKeeper() {
1085 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001086 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001087 delete I->second;
1088 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001089 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001090 delete I->second;
1091 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001092
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1094 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1095
1096 Record *getClass(const std::string &Name) const {
1097 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1098 return I == Classes.end() ? 0 : I->second;
1099 }
1100 Record *getDef(const std::string &Name) const {
1101 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1102 return I == Defs.end() ? 0 : I->second;
1103 }
1104 void addClass(Record *R) {
1105 assert(getClass(R->getName()) == 0 && "Class already exists!");
1106 Classes.insert(std::make_pair(R->getName(), R));
1107 }
1108 void addDef(Record *R) {
1109 assert(getDef(R->getName()) == 0 && "Def already exists!");
1110 Defs.insert(std::make_pair(R->getName(), R));
1111 }
1112
Chris Lattnerac284252005-08-19 17:58:11 +00001113 /// removeClass - Remove, but do not delete, the specified record.
1114 ///
1115 void removeClass(const std::string &Name) {
1116 assert(Classes.count(Name) && "Class does not exist!");
1117 Classes.erase(Name);
1118 }
1119 /// removeDef - Remove, but do not delete, the specified record.
1120 ///
1121 void removeDef(const std::string &Name) {
1122 assert(Defs.count(Name) && "Def does not exist!");
1123 Defs.erase(Name);
1124 }
1125
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001126 //===--------------------------------------------------------------------===//
1127 // High-level helper methods, useful for tablegen backends...
1128
1129 /// getAllDerivedDefinitions - This method returns all concrete definitions
1130 /// that derive from the specified class name. If a class with the specified
1131 /// name does not exist, an exception is thrown.
1132 std::vector<Record*>
1133 getAllDerivedDefinitions(const std::string &ClassName) const;
1134
1135
1136 void dump() const;
1137};
1138
1139std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1140
1141extern RecordKeeper Records;
1142
Brian Gaeke960707c2003-11-11 22:41:34 +00001143} // End llvm namespace
1144
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001145#endif