blob: 87a49ee2f8448c2fc89481cb3fd4460acf5d53ca [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//
Chris Lattner8adcd9f2007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// 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
Argyrios Kyrtzidis9e50bff2008-10-22 09:54:13 +000018#include "llvm/Support/DataTypes.h"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000019#include <string>
20#include <vector>
21#include <map>
Bill Wendling9bfb1e12006-12-07 22:21:48 +000022#include <ostream>
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000023#include <cassert>
24
Brian Gaeke960707c2003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattneref943742005-04-19 03:36:21 +000027// RecTy subclasses.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000028class BitRecTy;
29class BitsRecTy;
30class IntRecTy;
31class StringRecTy;
32class ListRecTy;
33class CodeRecTy;
34class DagRecTy;
35class RecordRecTy;
36
Chris Lattneref943742005-04-19 03:36:21 +000037// Init subclasses.
Chris Lattner7dfc2d22004-10-27 16:14:51 +000038struct Init;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000039class UnsetInit;
40class BitInit;
41class BitsInit;
42class IntInit;
43class StringInit;
44class CodeInit;
45class ListInit;
Chris Lattner51ffbf12006-03-31 21:53:49 +000046class BinOpInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000047class DefInit;
48class DagInit;
49class TypedInit;
50class VarInit;
51class FieldInit;
52class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000053class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054
Chris Lattneref943742005-04-19 03:36:21 +000055// Other classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000056class Record;
Chris Lattneref943742005-04-19 03:36:21 +000057class RecordVal;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000058
59//===----------------------------------------------------------------------===//
60// Type Classes
61//===----------------------------------------------------------------------===//
62
63struct RecTy {
64 virtual ~RecTy() {}
65
Chris Lattner8b9ecda2007-11-20 22:25:16 +000066 virtual std::string getAsString() const = 0;
Chris Lattner1b1e96b2007-11-22 20:51:34 +000067 void print(std::ostream &OS) const { OS << getAsString(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000068 void dump() const;
69
70 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
71 /// converted to the specified type.
72 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
73
74public: // These methods should only be called from subclasses of Init
75 virtual Init *convertValue( UnsetInit *UI) { return 0; }
76 virtual Init *convertValue( BitInit *BI) { return 0; }
77 virtual Init *convertValue( BitsInit *BI) { return 0; }
78 virtual Init *convertValue( IntInit *II) { return 0; }
79 virtual Init *convertValue(StringInit *SI) { return 0; }
80 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +000081 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000082 virtual Init *convertValue( CodeInit *CI) { return 0; }
83 virtual Init *convertValue(VarBitInit *VB) { return 0; }
84 virtual Init *convertValue( DefInit *DI) { return 0; }
85 virtual Init *convertValue( DagInit *DI) { return 0; }
86 virtual Init *convertValue( TypedInit *TI) { return 0; }
87 virtual Init *convertValue( VarInit *VI) {
88 return convertValue((TypedInit*)VI);
89 }
90 virtual Init *convertValue( FieldInit *FI) {
91 return convertValue((TypedInit*)FI);
92 }
93
94public: // These methods should only be called by subclasses of RecTy.
95 // baseClassOf - These virtual methods should be overloaded to return true iff
96 // all values of type 'RHS' can be converted to the 'this' type.
97 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
104 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
105};
106
107inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
108 Ty.print(OS);
109 return OS;
110}
111
112
113/// BitRecTy - 'bit' - Represent a single bit
114///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000115class BitRecTy : public RecTy {
116public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000117 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
118 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
119 virtual Init *convertValue( BitsInit *BI);
120 virtual Init *convertValue( IntInit *II);
121 virtual Init *convertValue(StringInit *SI) { return 0; }
122 virtual Init *convertValue( ListInit *LI) { return 0; }
123 virtual Init *convertValue( CodeInit *CI) { return 0; }
124 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
125 virtual Init *convertValue( DefInit *DI) { return 0; }
126 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000127 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000128 virtual Init *convertValue( TypedInit *TI);
129 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
130 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000131
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000132 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000133
134 bool typeIsConvertibleTo(const RecTy *RHS) const {
135 return RHS->baseClassOf(this);
136 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000137 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
138 virtual bool baseClassOf(const BitsRecTy *RHS) const;
139 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
140 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
144 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
145
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000146};
147
148
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000149// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
150/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000151///
152class BitsRecTy : public RecTy {
153 unsigned Size;
154public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000155 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000156
157 unsigned getNumBits() const { return Size; }
158
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000159 virtual Init *convertValue( UnsetInit *UI);
160 virtual Init *convertValue( BitInit *UI);
161 virtual Init *convertValue( BitsInit *BI);
162 virtual Init *convertValue( IntInit *II);
163 virtual Init *convertValue(StringInit *SI) { return 0; }
164 virtual Init *convertValue( ListInit *LI) { return 0; }
165 virtual Init *convertValue( CodeInit *CI) { return 0; }
166 virtual Init *convertValue(VarBitInit *VB) { return 0; }
167 virtual Init *convertValue( DefInit *DI) { return 0; }
168 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000169 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000170 virtual Init *convertValue( TypedInit *TI);
171 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
172 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
173
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000174 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000175
176 bool typeIsConvertibleTo(const RecTy *RHS) const {
177 return RHS->baseClassOf(this);
178 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000179 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
180 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000181 return RHS->Size == Size;
182 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000183 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
184 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
185 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
189
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000190};
191
192
193/// IntRecTy - 'int' - Represent an integer value of no particular size
194///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000195class IntRecTy : public RecTy {
196public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000197 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
198 virtual Init *convertValue( BitInit *BI);
199 virtual Init *convertValue( BitsInit *BI);
200 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
201 virtual Init *convertValue(StringInit *SI) { return 0; }
202 virtual Init *convertValue( ListInit *LI) { return 0; }
203 virtual Init *convertValue( CodeInit *CI) { return 0; }
204 virtual Init *convertValue(VarBitInit *VB) { return 0; }
205 virtual Init *convertValue( DefInit *DI) { return 0; }
206 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000207 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000208 virtual Init *convertValue( TypedInit *TI);
209 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
210 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
211
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000212 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000213
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 Lattner8b9ecda2007-11-20 22:25:16 +0000248 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000249
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:
Dan Gohman56e3f632008-07-07 18:00:37 +0000272 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000273
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 Lattner8b9ecda2007-11-20 22:25:16 +0000291 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000292
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 Lattner8b9ecda2007-11-20 22:25:16 +0000328 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000329
330 bool typeIsConvertibleTo(const RecTy *RHS) const {
331 return RHS->baseClassOf(this);
332 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000333 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
334 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
338 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
339 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
340 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000341};
342
343/// DagRecTy - 'dag' - Represent a dag fragment
344///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000345class DagRecTy : public RecTy {
346public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000347 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
348 virtual Init *convertValue( BitInit *BI) { return 0; }
349 virtual Init *convertValue( BitsInit *BI) { return 0; }
350 virtual Init *convertValue( IntInit *II) { return 0; }
351 virtual Init *convertValue(StringInit *SI) { return 0; }
352 virtual Init *convertValue( ListInit *LI) { return 0; }
353 virtual Init *convertValue( CodeInit *CI) { return 0; }
354 virtual Init *convertValue(VarBitInit *VB) { return 0; }
355 virtual Init *convertValue( DefInit *DI) { return 0; }
Evan Chenga32dee22007-05-15 01:23:24 +0000356 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000357 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
358 virtual Init *convertValue( TypedInit *TI);
359 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
360 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000361
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000362 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000363
364 bool typeIsConvertibleTo(const RecTy *RHS) const {
365 return RHS->baseClassOf(this);
366 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000367
368 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
369 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
375 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000376};
377
378
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000379/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000380/// (R32 X = EAX).
381///
382class RecordRecTy : public RecTy {
383 Record *Rec;
384public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000385 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000386
387 Record *getRecord() const { return Rec; }
388
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000389 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
390 virtual Init *convertValue( BitInit *BI) { return 0; }
391 virtual Init *convertValue( BitsInit *BI) { return 0; }
392 virtual Init *convertValue( IntInit *II) { return 0; }
393 virtual Init *convertValue(StringInit *SI) { return 0; }
394 virtual Init *convertValue( ListInit *LI) { return 0; }
395 virtual Init *convertValue( CodeInit *CI) { return 0; }
396 virtual Init *convertValue(VarBitInit *VB) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000397 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000398 virtual Init *convertValue( DefInit *DI);
399 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000400 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000401 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
402 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000403
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000404 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000405
406 bool typeIsConvertibleTo(const RecTy *RHS) const {
407 return RHS->baseClassOf(this);
408 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000409 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
410 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000416 virtual bool baseClassOf(const RecordRecTy *RHS) const;
417};
418
419
420
421//===----------------------------------------------------------------------===//
422// Initializer Classes
423//===----------------------------------------------------------------------===//
424
425struct Init {
426 virtual ~Init() {}
427
428 /// isComplete - This virtual method should be overridden by values that may
429 /// not be completely specified yet.
430 virtual bool isComplete() const { return true; }
431
432 /// print - Print out this value.
Chris Lattner695506c2007-11-22 21:05:25 +0000433 void print(std::ostream &OS) const { OS << getAsString(); }
434
435 /// getAsString - Convert this value to a string form.
436 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000437
438 /// dump - Debugging method that may be called through a debugger, just
439 /// invokes print on cerr.
440 void dump() const;
441
442 /// convertInitializerTo - This virtual function is a simple call-back
443 /// function that should be overridden to call the appropriate
444 /// RecTy::convertValue method.
445 ///
446 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
447
448 /// convertInitializerBitRange - This method is used to implement the bitrange
449 /// selection operator. Given an initializer, it selects the specified bits
450 /// out, returning them as a new init of bits type. If it is not legal to use
451 /// the bit subscript operator on this initializer, return null.
452 ///
453 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
454 return 0;
455 }
456
Chris Lattner8bf9e062004-07-26 23:21:34 +0000457 /// convertInitListSlice - This method is used to implement the list slice
458 /// selection operator. Given an initializer, it selects the specified list
459 /// elements, returning them as a new init of list type. If it is not legal
460 /// to take a slice of this, return null.
461 ///
462 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
463 return 0;
464 }
465
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000466 /// getFieldType - This method is used to implement the FieldInit class.
467 /// Implementors of this method should return the type of the named field if
468 /// they are of record type.
469 ///
470 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
471
472 /// getFieldInit - This method complements getFieldType to return the
473 /// initializer for the specified field. If getFieldType returns non-null
474 /// this method should return non-null, otherwise it returns null.
475 ///
476 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
477 return 0;
478 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000479
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000480 /// resolveReferences - This method is used by classes that refer to other
481 /// variables which may not be defined at the time they expression is formed.
482 /// If a value is set for the variable later, this method will be called on
483 /// users of the value to allow the value to propagate out.
484 ///
Chris Lattneref943742005-04-19 03:36:21 +0000485 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
486 return this;
487 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000488};
489
490inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
491 I.print(OS); return OS;
492}
493
494
495/// UnsetInit - ? - Represents an uninitialized value
496///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000497class UnsetInit : public Init {
498public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000499 virtual Init *convertInitializerTo(RecTy *Ty) {
500 return Ty->convertValue(this);
501 }
502
503 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000504 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000505};
506
507
508/// BitInit - true/false - Represent a concrete initializer for a bit.
509///
510class BitInit : public Init {
511 bool Value;
512public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000513 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000514
515 bool getValue() const { return Value; }
516
517 virtual Init *convertInitializerTo(RecTy *Ty) {
518 return Ty->convertValue(this);
519 }
520
Chris Lattner695506c2007-11-22 21:05:25 +0000521 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000522};
523
524/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
525/// It contains a vector of bits, whose size is determined by the type.
526///
527class BitsInit : public Init {
528 std::vector<Init*> Bits;
529public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000530 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000531
532 unsigned getNumBits() const { return Bits.size(); }
533
534 Init *getBit(unsigned Bit) const {
535 assert(Bit < Bits.size() && "Bit index out of range!");
536 return Bits[Bit];
537 }
538 void setBit(unsigned Bit, Init *V) {
539 assert(Bit < Bits.size() && "Bit index out of range!");
540 assert(Bits[Bit] == 0 && "Bit already set!");
541 Bits[Bit] = V;
542 }
543
544 virtual Init *convertInitializerTo(RecTy *Ty) {
545 return Ty->convertValue(this);
546 }
547 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
548
549 virtual bool isComplete() const {
550 for (unsigned i = 0; i != getNumBits(); ++i)
551 if (!getBit(i)->isComplete()) return false;
552 return true;
553 }
Chris Lattner695506c2007-11-22 21:05:25 +0000554 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000555
Chris Lattneref943742005-04-19 03:36:21 +0000556 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000557
558 // printXX - Print this bitstream with the specified format, returning true if
559 // it is not possible.
560 bool printInHex(std::ostream &OS) const;
561 bool printAsVariable(std::ostream &OS) const;
562 bool printAsUnset(std::ostream &OS) const;
563};
564
565
566/// IntInit - 7 - Represent an initalization by a literal integer value.
567///
568class IntInit : public Init {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000569 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000570public:
Dan Gohmanca0546f2008-10-17 01:33:43 +0000571 explicit IntInit(int64_t V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000572
Dan Gohmanca0546f2008-10-17 01:33:43 +0000573 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000574
575 virtual Init *convertInitializerTo(RecTy *Ty) {
576 return Ty->convertValue(this);
577 }
578 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
579
Chris Lattner695506c2007-11-22 21:05:25 +0000580 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000581};
582
583
584/// StringInit - "foo" - Represent an initialization by a string value.
585///
586class StringInit : public Init {
587 std::string Value;
588public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000589 explicit StringInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000590
591 const std::string &getValue() const { return Value; }
592
593 virtual Init *convertInitializerTo(RecTy *Ty) {
594 return Ty->convertValue(this);
595 }
596
Chris Lattner695506c2007-11-22 21:05:25 +0000597 virtual std::string getAsString() const { return "\"" + Value + "\""; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000598};
599
600/// CodeInit - "[{...}]" - Represent a code fragment.
601///
602class CodeInit : public Init {
603 std::string Value;
604public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000605 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000606
607 const std::string getValue() const { return Value; }
608
609 virtual Init *convertInitializerTo(RecTy *Ty) {
610 return Ty->convertValue(this);
611 }
612
Chris Lattner695506c2007-11-22 21:05:25 +0000613 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000614};
615
616/// ListInit - [AL, AH, CL] - Represent a list of defs
617///
618class ListInit : public Init {
619 std::vector<Init*> Values;
620public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000621 explicit ListInit(std::vector<Init*> &Vs) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000622 Values.swap(Vs);
623 }
624
625 unsigned getSize() const { return Values.size(); }
626 Init *getElement(unsigned i) const {
627 assert(i < Values.size() && "List element index out of range!");
628 return Values[i];
629 }
630
Chris Lattnercbebe462007-02-27 22:08:27 +0000631 Record *getElementAsRecord(unsigned i) const;
632
Chris Lattner8bf9e062004-07-26 23:21:34 +0000633 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
634
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000635 virtual Init *convertInitializerTo(RecTy *Ty) {
636 return Ty->convertValue(this);
637 }
638
Chris Lattner577fc3f2004-07-27 01:01:21 +0000639 /// resolveReferences - This method is used by classes that refer to other
640 /// variables which may not be defined at the time they expression is formed.
641 /// If a value is set for the variable later, this method will be called on
642 /// users of the value to allow the value to propagate out.
643 ///
Chris Lattneref943742005-04-19 03:36:21 +0000644 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000645
Chris Lattner695506c2007-11-22 21:05:25 +0000646 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000647
648 typedef std::vector<Init*>::iterator iterator;
649 typedef std::vector<Init*>::const_iterator const_iterator;
650
651 inline iterator begin() { return Values.begin(); }
652 inline const_iterator begin() const { return Values.begin(); }
653 inline iterator end () { return Values.end(); }
654 inline const_iterator end () const { return Values.end(); }
655
656 inline size_t size () const { return Values.size(); }
657 inline bool empty() const { return Values.empty(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000658};
659
Chris Lattner51ffbf12006-03-31 21:53:49 +0000660/// BinOpInit - !op (X, Y) - Combine two inits.
661///
662class BinOpInit : public Init {
663public:
Evan Chenga32dee22007-05-15 01:23:24 +0000664 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT };
Chris Lattner51ffbf12006-03-31 21:53:49 +0000665private:
666 BinaryOp Opc;
667 Init *LHS, *RHS;
668public:
669 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
670 }
671
672 BinaryOp getOpcode() const { return Opc; }
673 Init *getLHS() const { return LHS; }
674 Init *getRHS() const { return RHS; }
675
676 // Fold - If possible, fold this to a simpler init. Return this if not
677 // possible to fold.
678 Init *Fold();
679
680 virtual Init *convertInitializerTo(RecTy *Ty) {
681 return Ty->convertValue(this);
682 }
683
684 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
685
Chris Lattner695506c2007-11-22 21:05:25 +0000686 virtual std::string getAsString() const;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000687};
688
689
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000690
691/// TypedInit - This is the common super-class of types that have a specific,
692/// explicit, type.
693///
694class TypedInit : public Init {
695 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000696public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000697 explicit TypedInit(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000698
699 RecTy *getType() const { return Ty; }
700
Chris Lattner577fc3f2004-07-27 01:01:21 +0000701 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
702 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
703
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000704 /// resolveBitReference - This method is used to implement
705 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000706 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000707 ///
Chris Lattneref943742005-04-19 03:36:21 +0000708 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
709 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000710
711 /// resolveListElementReference - This method is used to implement
712 /// VarListElementInit::resolveReferences. If the list element is resolvable
713 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000714 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
715 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000716};
717
718/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
719///
720class VarInit : public TypedInit {
721 std::string VarName;
722public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000723 explicit VarInit(const std::string &VN, RecTy *T)
724 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000725
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000726 virtual Init *convertInitializerTo(RecTy *Ty) {
727 return Ty->convertValue(this);
728 }
729
730 const std::string &getName() const { return VarName; }
731
Chris Lattneref943742005-04-19 03:36:21 +0000732 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
733 unsigned Bit);
734 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
735 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000736
737 virtual RecTy *getFieldType(const std::string &FieldName) const;
738 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
739
740 /// resolveReferences - This method is used by classes that refer to other
741 /// variables which may not be defined at the time they expression is formed.
742 /// If a value is set for the variable later, this method will be called on
743 /// users of the value to allow the value to propagate out.
744 ///
Chris Lattneref943742005-04-19 03:36:21 +0000745 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000746
Chris Lattner695506c2007-11-22 21:05:25 +0000747 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000748};
749
750
751/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
752///
753class VarBitInit : public Init {
754 TypedInit *TI;
755 unsigned Bit;
756public:
757 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
758 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
759 ((BitsRecTy*)T->getType())->getNumBits() > B &&
760 "Illegal VarBitInit expression!");
761 }
762
763 virtual Init *convertInitializerTo(RecTy *Ty) {
764 return Ty->convertValue(this);
765 }
766
767 TypedInit *getVariable() const { return TI; }
768 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000769
Chris Lattner695506c2007-11-22 21:05:25 +0000770 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000771 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000772};
773
Misha Brukman650ba8e2005-04-22 00:00:37 +0000774/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000775/// field.
776class VarListElementInit : public TypedInit {
777 TypedInit *TI;
778 unsigned Element;
779public:
780 VarListElementInit(TypedInit *T, unsigned E)
781 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
782 TI(T), Element(E) {
783 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
784 "Illegal VarBitInit expression!");
785 }
786
787 virtual Init *convertInitializerTo(RecTy *Ty) {
788 return Ty->convertValue(this);
789 }
790
791 TypedInit *getVariable() const { return TI; }
792 unsigned getElementNum() const { return Element; }
793
Chris Lattneref943742005-04-19 03:36:21 +0000794 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
795 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000796
797 /// resolveListElementReference - This method is used to implement
798 /// VarListElementInit::resolveReferences. If the list element is resolvable
799 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000800 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
801 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000802
Chris Lattner695506c2007-11-22 21:05:25 +0000803 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000804 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000805};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000806
807/// DefInit - AL - Represent a reference to a 'def' in the description
808///
809class DefInit : public Init {
810 Record *Def;
811public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000812 explicit DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000813
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000814 virtual Init *convertInitializerTo(RecTy *Ty) {
815 return Ty->convertValue(this);
816 }
817
818 Record *getDef() const { return Def; }
819
820 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
821
822 virtual RecTy *getFieldType(const std::string &FieldName) const;
823 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000824
Chris Lattner695506c2007-11-22 21:05:25 +0000825 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000826};
827
828
829/// FieldInit - X.Y - Represent a reference to a subfield of a variable
830///
831class FieldInit : public TypedInit {
832 Init *Rec; // Record we are referring to
833 std::string FieldName; // Field we are accessing
834public:
835 FieldInit(Init *R, const std::string &FN)
836 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
837 assert(getType() && "FieldInit with non-record type!");
838 }
839
840 virtual Init *convertInitializerTo(RecTy *Ty) {
841 return Ty->convertValue(this);
842 }
843
Chris Lattneref943742005-04-19 03:36:21 +0000844 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
845 unsigned Bit);
846 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
847 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000848
Chris Lattneref943742005-04-19 03:36:21 +0000849 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000850
Chris Lattner695506c2007-11-22 21:05:25 +0000851 virtual std::string getAsString() const {
852 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000853 }
854};
855
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000856/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
857/// to have at least one value then a (possibly empty) list of arguments. Each
858/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000859///
860class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000861 Init *Val;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000862 std::vector<Init*> Args;
863 std::vector<std::string> ArgNames;
864public:
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000865 DagInit(Init *V, const std::vector<std::pair<Init*, std::string> > &args)
866 : Val(V) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000867 Args.reserve(args.size());
868 ArgNames.reserve(args.size());
869 for (unsigned i = 0, e = args.size(); i != e; ++i) {
870 Args.push_back(args[i].first);
871 ArgNames.push_back(args[i].second);
872 }
873 }
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000874 DagInit(Init *V, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +0000875 const std::vector<std::string> &argNames)
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000876 : Val(V), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +0000877 }
878
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000879 virtual Init *convertInitializerTo(RecTy *Ty) {
880 return Ty->convertValue(this);
881 }
882
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000883 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000884
885 unsigned getNumArgs() const { return Args.size(); }
886 Init *getArg(unsigned Num) const {
887 assert(Num < Args.size() && "Arg number out of range!");
888 return Args[Num];
889 }
890 const std::string &getArgName(unsigned Num) const {
891 assert(Num < ArgNames.size() && "Arg number out of range!");
892 return ArgNames[Num];
893 }
894
895 void setArg(unsigned Num, Init *I) {
896 assert(Num < Args.size() && "Arg number out of range!");
897 Args[Num] = I;
898 }
Chris Lattner0d3ef402006-01-31 06:02:35 +0000899
900 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000901
Chris Lattner695506c2007-11-22 21:05:25 +0000902 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +0000903
904 typedef std::vector<Init*>::iterator arg_iterator;
905 typedef std::vector<Init*>::const_iterator const_arg_iterator;
906 typedef std::vector<std::string>::iterator name_iterator;
907 typedef std::vector<std::string>::const_iterator const_name_iterator;
908
909 inline arg_iterator arg_begin() { return Args.begin(); }
910 inline const_arg_iterator arg_begin() const { return Args.begin(); }
911 inline arg_iterator arg_end () { return Args.end(); }
912 inline const_arg_iterator arg_end () const { return Args.end(); }
913
914 inline size_t arg_size () const { return Args.size(); }
915 inline bool arg_empty() const { return Args.empty(); }
916
917 inline name_iterator name_begin() { return ArgNames.begin(); }
918 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
919 inline name_iterator name_end () { return ArgNames.end(); }
920 inline const_name_iterator name_end () const { return ArgNames.end(); }
921
922 inline size_t name_size () const { return ArgNames.size(); }
923 inline bool name_empty() const { return ArgNames.empty(); }
924
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000925};
926
927//===----------------------------------------------------------------------===//
928// High-Level Classes
929//===----------------------------------------------------------------------===//
930
931class RecordVal {
932 std::string Name;
933 RecTy *Ty;
934 unsigned Prefix;
935 Init *Value;
936public:
937 RecordVal(const std::string &N, RecTy *T, unsigned P);
938
939 const std::string &getName() const { return Name; }
940
941 unsigned getPrefix() const { return Prefix; }
942 RecTy *getType() const { return Ty; }
943 Init *getValue() const { return Value; }
944
945 bool setValue(Init *V) {
946 if (V) {
947 Value = V->convertInitializerTo(Ty);
948 return Value == 0;
949 }
950 Value = 0;
951 return false;
952 }
953
954 void dump() const;
955 void print(std::ostream &OS, bool PrintSem = true) const;
956};
957
958inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
959 RV.print(OS << " ");
960 return OS;
961}
962
Chris Lattner87a10612004-10-23 04:58:50 +0000963class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +0000964 std::string Name;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000965 std::vector<std::string> TemplateArgs;
966 std::vector<RecordVal> Values;
967 std::vector<Record*> SuperClasses;
968public:
969
Dan Gohman56e3f632008-07-07 18:00:37 +0000970 explicit Record(const std::string &N) : Name(N) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000971 ~Record() {}
972
973 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +0000974 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000975 const std::vector<std::string> &getTemplateArgs() const {
976 return TemplateArgs;
977 }
978 const std::vector<RecordVal> &getValues() const { return Values; }
979 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
980
981 bool isTemplateArg(const std::string &Name) const {
982 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
983 if (TemplateArgs[i] == Name) return true;
984 return false;
985 }
986
987 const RecordVal *getValue(const std::string &Name) const {
988 for (unsigned i = 0, e = Values.size(); i != e; ++i)
989 if (Values[i].getName() == Name) return &Values[i];
990 return 0;
991 }
992 RecordVal *getValue(const std::string &Name) {
993 for (unsigned i = 0, e = Values.size(); i != e; ++i)
994 if (Values[i].getName() == Name) return &Values[i];
995 return 0;
996 }
997
998 void addTemplateArg(const std::string &Name) {
999 assert(!isTemplateArg(Name) && "Template arg already defined!");
1000 TemplateArgs.push_back(Name);
1001 }
1002
1003 void addValue(const RecordVal &RV) {
1004 assert(getValue(RV.getName()) == 0 && "Value already added!");
1005 Values.push_back(RV);
1006 }
1007
1008 void removeValue(const std::string &Name) {
1009 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1010 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1011 if (Values[i].getName() == Name) {
1012 Values.erase(Values.begin()+i);
1013 return;
1014 }
1015 assert(0 && "Name does not exist in record!");
1016 }
1017
1018 bool isSubClassOf(Record *R) const {
1019 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1020 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001021 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001022 return false;
1023 }
1024
1025 bool isSubClassOf(const std::string &Name) const {
1026 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1027 if (SuperClasses[i]->getName() == Name)
1028 return true;
1029 return false;
1030 }
1031
1032 void addSuperClass(Record *R) {
1033 assert(!isSubClassOf(R) && "Already subclassing record!");
1034 SuperClasses.push_back(R);
1035 }
1036
Chris Lattneref943742005-04-19 03:36:21 +00001037 /// resolveReferences - If there are any field references that refer to fields
1038 /// that have been filled in, we can propagate the values now.
1039 ///
1040 void resolveReferences() { resolveReferencesTo(0); }
1041
1042 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1043 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1044 /// possible references.
1045 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001046
1047 void dump() const;
1048
1049 //===--------------------------------------------------------------------===//
1050 // High-level methods useful to tablegen back-ends
1051 //
1052
1053 /// getValueInit - Return the initializer for a value with the specified name,
1054 /// or throw an exception if the field does not exist.
1055 ///
1056 Init *getValueInit(const std::string &FieldName) const;
1057
1058 /// getValueAsString - This method looks up the specified field and returns
1059 /// its value as a string, throwing an exception if the field does not exist
1060 /// or if the value is not a string.
1061 ///
1062 std::string getValueAsString(const std::string &FieldName) const;
1063
1064 /// getValueAsBitsInit - This method looks up the specified field and returns
1065 /// its value as a BitsInit, throwing an exception if the field does not exist
1066 /// or if the value is not the right type.
1067 ///
1068 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1069
1070 /// getValueAsListInit - This method looks up the specified field and returns
1071 /// its value as a ListInit, throwing an exception if the field does not exist
1072 /// or if the value is not the right type.
1073 ///
1074 ListInit *getValueAsListInit(const std::string &FieldName) const;
1075
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001076 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001077 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001078 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001079 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001080 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001081
Anton Korobeynikova468a112007-11-11 11:19:37 +00001082 /// getValueAsListOfInts - This method looks up the specified field and returns
1083 /// its value as a vector of integers, throwing an exception if the field does
1084 /// not exist or if the value is not the right type.
1085 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001086 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001087
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001088 /// getValueAsDef - This method looks up the specified field and returns its
1089 /// value as a Record, throwing an exception if the field does not exist or if
1090 /// the value is not the right type.
1091 ///
1092 Record *getValueAsDef(const std::string &FieldName) const;
1093
1094 /// getValueAsBit - This method looks up the specified field and returns its
1095 /// value as a bit, throwing an exception if the field does not exist or if
1096 /// the value is not the right type.
1097 ///
1098 bool getValueAsBit(const std::string &FieldName) const;
1099
1100 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001101 /// value as an int64_t, throwing an exception if the field does not exist or
1102 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001103 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001104 int64_t getValueAsInt(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001105
1106 /// getValueAsDag - This method looks up the specified field and returns its
1107 /// value as an Dag, throwing an exception if the field does not exist or if
1108 /// the value is not the right type.
1109 ///
1110 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001111
1112 /// getValueAsCode - This method looks up the specified field and returns
1113 /// its value as the string data in a CodeInit, throwing an exception if the
1114 /// field does not exist or if the value is not a code object.
1115 ///
1116 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001117};
1118
1119std::ostream &operator<<(std::ostream &OS, const Record &R);
1120
1121class RecordKeeper {
1122 std::map<std::string, Record*> Classes, Defs;
1123public:
1124 ~RecordKeeper() {
1125 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001126 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001127 delete I->second;
1128 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001129 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001130 delete I->second;
1131 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001132
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001133 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1134 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1135
1136 Record *getClass(const std::string &Name) const {
1137 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1138 return I == Classes.end() ? 0 : I->second;
1139 }
1140 Record *getDef(const std::string &Name) const {
1141 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1142 return I == Defs.end() ? 0 : I->second;
1143 }
1144 void addClass(Record *R) {
1145 assert(getClass(R->getName()) == 0 && "Class already exists!");
1146 Classes.insert(std::make_pair(R->getName(), R));
1147 }
1148 void addDef(Record *R) {
1149 assert(getDef(R->getName()) == 0 && "Def already exists!");
1150 Defs.insert(std::make_pair(R->getName(), R));
1151 }
1152
Chris Lattnerac284252005-08-19 17:58:11 +00001153 /// removeClass - Remove, but do not delete, the specified record.
1154 ///
1155 void removeClass(const std::string &Name) {
1156 assert(Classes.count(Name) && "Class does not exist!");
1157 Classes.erase(Name);
1158 }
1159 /// removeDef - Remove, but do not delete, the specified record.
1160 ///
1161 void removeDef(const std::string &Name) {
1162 assert(Defs.count(Name) && "Def does not exist!");
1163 Defs.erase(Name);
1164 }
1165
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001166 //===--------------------------------------------------------------------===//
1167 // High-level helper methods, useful for tablegen backends...
1168
1169 /// getAllDerivedDefinitions - This method returns all concrete definitions
1170 /// that derive from the specified class name. If a class with the specified
1171 /// name does not exist, an exception is thrown.
1172 std::vector<Record*>
1173 getAllDerivedDefinitions(const std::string &ClassName) const;
1174
1175
1176 void dump() const;
1177};
1178
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001179/// LessRecord - Sorting predicate to sort record pointers by name.
1180///
1181struct LessRecord {
1182 bool operator()(const Record *Rec1, const Record *Rec2) const {
1183 return Rec1->getName() < Rec2->getName();
1184 }
1185};
1186
Jim Grosbach56938af2008-09-11 17:05:32 +00001187/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1188/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001189///
1190struct LessRecordFieldName {
1191 bool operator()(const Record *Rec1, const Record *Rec2) const {
1192 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1193 }
1194};
1195
1196
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001197std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1198
1199extern RecordKeeper Records;
1200
Brian Gaeke960707c2003-11-11 22:41:34 +00001201} // End llvm namespace
1202
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001203#endif