blob: b408452f3d1a6006deab5b7ee0494d5222b53b1a [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
Chris Lattnerbd9b9212009-03-13 16:09:24 +000018#include "TGSourceMgr.h"
Argyrios Kyrtzidis9e50bff2008-10-22 09:54:13 +000019#include "llvm/Support/DataTypes.h"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000020#include <map>
Bill Wendling9bfb1e12006-12-07 22:21:48 +000021#include <ostream>
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000022
Brian Gaeke960707c2003-11-11 22:41:34 +000023namespace llvm {
Chris Lattnerbd9b9212009-03-13 16:09:24 +000024
Chris Lattneref943742005-04-19 03:36:21 +000025// RecTy subclasses.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000026class BitRecTy;
27class BitsRecTy;
28class IntRecTy;
29class StringRecTy;
30class ListRecTy;
31class CodeRecTy;
32class DagRecTy;
33class RecordRecTy;
34
Chris Lattneref943742005-04-19 03:36:21 +000035// Init subclasses.
Chris Lattner7dfc2d22004-10-27 16:14:51 +000036struct Init;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000037class UnsetInit;
38class BitInit;
39class BitsInit;
40class IntInit;
41class StringInit;
42class CodeInit;
43class ListInit;
Chris Lattner51ffbf12006-03-31 21:53:49 +000044class BinOpInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000045class DefInit;
46class DagInit;
47class TypedInit;
48class VarInit;
49class FieldInit;
50class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000051class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000052
Chris Lattneref943742005-04-19 03:36:21 +000053// Other classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054class Record;
Chris Lattneref943742005-04-19 03:36:21 +000055class RecordVal;
David Greenea9c6c5d2009-04-22 20:18:10 +000056class MultiClass;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000057
58//===----------------------------------------------------------------------===//
59// Type Classes
60//===----------------------------------------------------------------------===//
61
62struct RecTy {
63 virtual ~RecTy() {}
64
Chris Lattner8b9ecda2007-11-20 22:25:16 +000065 virtual std::string getAsString() const = 0;
Chris Lattner1b1e96b2007-11-22 20:51:34 +000066 void print(std::ostream &OS) const { OS << getAsString(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000067 void dump() const;
68
69 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
70 /// converted to the specified type.
71 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
72
73public: // These methods should only be called from subclasses of Init
74 virtual Init *convertValue( UnsetInit *UI) { return 0; }
75 virtual Init *convertValue( BitInit *BI) { return 0; }
76 virtual Init *convertValue( BitsInit *BI) { return 0; }
77 virtual Init *convertValue( IntInit *II) { return 0; }
78 virtual Init *convertValue(StringInit *SI) { return 0; }
79 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +000080 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000081 virtual Init *convertValue( CodeInit *CI) { return 0; }
82 virtual Init *convertValue(VarBitInit *VB) { return 0; }
83 virtual Init *convertValue( DefInit *DI) { return 0; }
84 virtual Init *convertValue( DagInit *DI) { return 0; }
85 virtual Init *convertValue( TypedInit *TI) { return 0; }
86 virtual Init *convertValue( VarInit *VI) {
87 return convertValue((TypedInit*)VI);
88 }
89 virtual Init *convertValue( FieldInit *FI) {
90 return convertValue((TypedInit*)FI);
91 }
92
93public: // These methods should only be called by subclasses of RecTy.
94 // baseClassOf - These virtual methods should be overloaded to return true iff
95 // all values of type 'RHS' can be converted to the 'this' type.
96 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
97 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
98 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
104};
105
106inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
107 Ty.print(OS);
108 return OS;
109}
110
111
112/// BitRecTy - 'bit' - Represent a single bit
113///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000114class BitRecTy : public RecTy {
115public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000116 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
117 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
118 virtual Init *convertValue( BitsInit *BI);
119 virtual Init *convertValue( IntInit *II);
120 virtual Init *convertValue(StringInit *SI) { return 0; }
121 virtual Init *convertValue( ListInit *LI) { return 0; }
122 virtual Init *convertValue( CodeInit *CI) { return 0; }
123 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
124 virtual Init *convertValue( DefInit *DI) { return 0; }
125 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000126 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000127 virtual Init *convertValue( TypedInit *TI);
128 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
129 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000130
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000131 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000132
133 bool typeIsConvertibleTo(const RecTy *RHS) const {
134 return RHS->baseClassOf(this);
135 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000136 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
137 virtual bool baseClassOf(const BitsRecTy *RHS) const;
138 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
139 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
140 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
141 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
144
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000145};
146
147
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000148// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
149/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000150///
151class BitsRecTy : public RecTy {
152 unsigned Size;
153public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000154 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000155
156 unsigned getNumBits() const { return Size; }
157
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000158 virtual Init *convertValue( UnsetInit *UI);
159 virtual Init *convertValue( BitInit *UI);
160 virtual Init *convertValue( BitsInit *BI);
161 virtual Init *convertValue( IntInit *II);
162 virtual Init *convertValue(StringInit *SI) { return 0; }
163 virtual Init *convertValue( ListInit *LI) { return 0; }
164 virtual Init *convertValue( CodeInit *CI) { return 0; }
165 virtual Init *convertValue(VarBitInit *VB) { return 0; }
166 virtual Init *convertValue( DefInit *DI) { return 0; }
167 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000168 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000169 virtual Init *convertValue( TypedInit *TI);
170 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
171 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
172
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000173 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000174
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 Lattner8b9ecda2007-11-20 22:25:16 +0000211 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000212
213 bool typeIsConvertibleTo(const RecTy *RHS) const {
214 return RHS->baseClassOf(this);
215 }
216
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000217 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
218 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
219 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
221 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
222 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
225
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000226};
227
228/// StringRecTy - 'string' - Represent an string value
229///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000230class StringRecTy : public RecTy {
231public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000232 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
233 virtual Init *convertValue( BitInit *BI) { return 0; }
234 virtual Init *convertValue( BitsInit *BI) { return 0; }
235 virtual Init *convertValue( IntInit *II) { return 0; }
236 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
237 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000238 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000239 virtual Init *convertValue( CodeInit *CI) { return 0; }
240 virtual Init *convertValue(VarBitInit *VB) { return 0; }
241 virtual Init *convertValue( DefInit *DI) { return 0; }
242 virtual Init *convertValue( DagInit *DI) { return 0; }
243 virtual Init *convertValue( TypedInit *TI);
244 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
245 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
246
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000247 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000248
249 bool typeIsConvertibleTo(const RecTy *RHS) const {
250 return RHS->baseClassOf(this);
251 }
252
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000253 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
254 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
255 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000256 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000257 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
258 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
259 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000261};
262
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000263// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
264// the specified type.
265/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
266/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000267///
268class ListRecTy : public RecTy {
269 RecTy *Ty;
270public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000271 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000272
273 RecTy *getElementType() const { return Ty; }
274
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000275 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
276 virtual Init *convertValue( BitInit *BI) { return 0; }
277 virtual Init *convertValue( BitsInit *BI) { return 0; }
278 virtual Init *convertValue( IntInit *II) { return 0; }
279 virtual Init *convertValue(StringInit *SI) { return 0; }
280 virtual Init *convertValue( ListInit *LI);
281 virtual Init *convertValue( CodeInit *CI) { return 0; }
282 virtual Init *convertValue(VarBitInit *VB) { return 0; }
283 virtual Init *convertValue( DefInit *DI) { return 0; }
284 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000285 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000286 virtual Init *convertValue( TypedInit *TI);
287 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
288 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000289
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000290 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000291
292 bool typeIsConvertibleTo(const RecTy *RHS) const {
293 return RHS->baseClassOf(this);
294 }
295
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000296 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
297 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
298 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000301 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000302 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000303 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
304 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
305 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000306};
307
308/// CodeRecTy - 'code' - Represent an code fragment, function or method.
309///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000310class CodeRecTy : public RecTy {
311public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000312 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
313 virtual Init *convertValue( BitInit *BI) { return 0; }
314 virtual Init *convertValue( BitsInit *BI) { return 0; }
315 virtual Init *convertValue( IntInit *II) { return 0; }
316 virtual Init *convertValue(StringInit *SI) { return 0; }
317 virtual Init *convertValue( ListInit *LI) { return 0; }
318 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
319 virtual Init *convertValue(VarBitInit *VB) { return 0; }
320 virtual Init *convertValue( DefInit *DI) { return 0; }
321 virtual Init *convertValue( DagInit *DI) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000322 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000323 virtual Init *convertValue( TypedInit *TI);
324 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
325 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
326
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000327 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000328
329 bool typeIsConvertibleTo(const RecTy *RHS) const {
330 return RHS->baseClassOf(this);
331 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000332 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
333 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
334 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
338 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
339 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000340};
341
342/// DagRecTy - 'dag' - Represent a dag fragment
343///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000344class DagRecTy : public RecTy {
345public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000346 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
347 virtual Init *convertValue( BitInit *BI) { return 0; }
348 virtual Init *convertValue( BitsInit *BI) { return 0; }
349 virtual Init *convertValue( IntInit *II) { return 0; }
350 virtual Init *convertValue(StringInit *SI) { return 0; }
351 virtual Init *convertValue( ListInit *LI) { return 0; }
352 virtual Init *convertValue( CodeInit *CI) { return 0; }
353 virtual Init *convertValue(VarBitInit *VB) { return 0; }
354 virtual Init *convertValue( DefInit *DI) { return 0; }
Evan Chenga32dee22007-05-15 01:23:24 +0000355 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000356 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
357 virtual Init *convertValue( TypedInit *TI);
358 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
359 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000360
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000361 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362
363 bool typeIsConvertibleTo(const RecTy *RHS) const {
364 return RHS->baseClassOf(this);
365 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000366
367 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
368 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
369 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
374 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000375};
376
377
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000378/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000379/// (R32 X = EAX).
380///
381class RecordRecTy : public RecTy {
382 Record *Rec;
383public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000384 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000385
386 Record *getRecord() const { return Rec; }
387
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000388 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
389 virtual Init *convertValue( BitInit *BI) { return 0; }
390 virtual Init *convertValue( BitsInit *BI) { return 0; }
391 virtual Init *convertValue( IntInit *II) { return 0; }
392 virtual Init *convertValue(StringInit *SI) { return 0; }
393 virtual Init *convertValue( ListInit *LI) { return 0; }
394 virtual Init *convertValue( CodeInit *CI) { return 0; }
395 virtual Init *convertValue(VarBitInit *VB) { return 0; }
Reid Spencer97c59802006-08-28 00:12:25 +0000396 virtual Init *convertValue( BinOpInit *UI) { return 0; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000397 virtual Init *convertValue( DefInit *DI);
398 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000399 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000400 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
401 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000402
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000403 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404
405 bool typeIsConvertibleTo(const RecTy *RHS) const {
406 return RHS->baseClassOf(this);
407 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000408 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
409 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
410 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000415 virtual bool baseClassOf(const RecordRecTy *RHS) const;
416};
417
418
419
420//===----------------------------------------------------------------------===//
421// Initializer Classes
422//===----------------------------------------------------------------------===//
423
424struct Init {
425 virtual ~Init() {}
426
427 /// isComplete - This virtual method should be overridden by values that may
428 /// not be completely specified yet.
429 virtual bool isComplete() const { return true; }
430
431 /// print - Print out this value.
Chris Lattner695506c2007-11-22 21:05:25 +0000432 void print(std::ostream &OS) const { OS << getAsString(); }
433
434 /// getAsString - Convert this value to a string form.
435 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000436
437 /// dump - Debugging method that may be called through a debugger, just
438 /// invokes print on cerr.
439 void dump() const;
440
441 /// convertInitializerTo - This virtual function is a simple call-back
442 /// function that should be overridden to call the appropriate
443 /// RecTy::convertValue method.
444 ///
445 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
446
447 /// convertInitializerBitRange - This method is used to implement the bitrange
448 /// selection operator. Given an initializer, it selects the specified bits
449 /// out, returning them as a new init of bits type. If it is not legal to use
450 /// the bit subscript operator on this initializer, return null.
451 ///
452 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
453 return 0;
454 }
455
Chris Lattner8bf9e062004-07-26 23:21:34 +0000456 /// convertInitListSlice - This method is used to implement the list slice
457 /// selection operator. Given an initializer, it selects the specified list
458 /// elements, returning them as a new init of list type. If it is not legal
459 /// to take a slice of this, return null.
460 ///
461 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
462 return 0;
463 }
464
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000465 /// getFieldType - This method is used to implement the FieldInit class.
466 /// Implementors of this method should return the type of the named field if
467 /// they are of record type.
468 ///
469 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
470
471 /// getFieldInit - This method complements getFieldType to return the
472 /// initializer for the specified field. If getFieldType returns non-null
473 /// this method should return non-null, otherwise it returns null.
474 ///
475 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
476 return 0;
477 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000478
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000479 /// resolveReferences - This method is used by classes that refer to other
480 /// variables which may not be defined at the time they expression is formed.
481 /// If a value is set for the variable later, this method will be called on
482 /// users of the value to allow the value to propagate out.
483 ///
Chris Lattneref943742005-04-19 03:36:21 +0000484 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
485 return this;
486 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000487};
488
489inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
490 I.print(OS); return OS;
491}
492
493
494/// UnsetInit - ? - Represents an uninitialized value
495///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000496class UnsetInit : public Init {
497public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000498 virtual Init *convertInitializerTo(RecTy *Ty) {
499 return Ty->convertValue(this);
500 }
501
502 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000503 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000504};
505
506
507/// BitInit - true/false - Represent a concrete initializer for a bit.
508///
509class BitInit : public Init {
510 bool Value;
511public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000512 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000513
514 bool getValue() const { return Value; }
515
516 virtual Init *convertInitializerTo(RecTy *Ty) {
517 return Ty->convertValue(this);
518 }
519
Chris Lattner695506c2007-11-22 21:05:25 +0000520 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000521};
522
523/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
524/// It contains a vector of bits, whose size is determined by the type.
525///
526class BitsInit : public Init {
527 std::vector<Init*> Bits;
528public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000529 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000530
531 unsigned getNumBits() const { return Bits.size(); }
532
533 Init *getBit(unsigned Bit) const {
534 assert(Bit < Bits.size() && "Bit index out of range!");
535 return Bits[Bit];
536 }
537 void setBit(unsigned Bit, Init *V) {
538 assert(Bit < Bits.size() && "Bit index out of range!");
539 assert(Bits[Bit] == 0 && "Bit already set!");
540 Bits[Bit] = V;
541 }
542
543 virtual Init *convertInitializerTo(RecTy *Ty) {
544 return Ty->convertValue(this);
545 }
546 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
547
548 virtual bool isComplete() const {
549 for (unsigned i = 0; i != getNumBits(); ++i)
550 if (!getBit(i)->isComplete()) return false;
551 return true;
552 }
Chris Lattner695506c2007-11-22 21:05:25 +0000553 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000554
Chris Lattneref943742005-04-19 03:36:21 +0000555 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000556
557 // printXX - Print this bitstream with the specified format, returning true if
558 // it is not possible.
559 bool printInHex(std::ostream &OS) const;
560 bool printAsVariable(std::ostream &OS) const;
561 bool printAsUnset(std::ostream &OS) const;
562};
563
564
565/// IntInit - 7 - Represent an initalization by a literal integer value.
566///
567class IntInit : public Init {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000568 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000569public:
Dan Gohmanca0546f2008-10-17 01:33:43 +0000570 explicit IntInit(int64_t V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000571
Dan Gohmanca0546f2008-10-17 01:33:43 +0000572 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000573
574 virtual Init *convertInitializerTo(RecTy *Ty) {
575 return Ty->convertValue(this);
576 }
577 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
578
Chris Lattner695506c2007-11-22 21:05:25 +0000579 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000580};
581
582
583/// StringInit - "foo" - Represent an initialization by a string value.
584///
585class StringInit : public Init {
586 std::string Value;
587public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000588 explicit StringInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000589
590 const std::string &getValue() const { return Value; }
591
592 virtual Init *convertInitializerTo(RecTy *Ty) {
593 return Ty->convertValue(this);
594 }
595
Chris Lattner695506c2007-11-22 21:05:25 +0000596 virtual std::string getAsString() const { return "\"" + Value + "\""; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000597};
598
599/// CodeInit - "[{...}]" - Represent a code fragment.
600///
601class CodeInit : public Init {
602 std::string Value;
603public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000604 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000605
606 const std::string getValue() const { return Value; }
607
608 virtual Init *convertInitializerTo(RecTy *Ty) {
609 return Ty->convertValue(this);
610 }
611
Chris Lattner695506c2007-11-22 21:05:25 +0000612 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000613};
614
615/// ListInit - [AL, AH, CL] - Represent a list of defs
616///
617class ListInit : public Init {
618 std::vector<Init*> Values;
619public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000620 explicit ListInit(std::vector<Init*> &Vs) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000621 Values.swap(Vs);
622 }
623
624 unsigned getSize() const { return Values.size(); }
625 Init *getElement(unsigned i) const {
626 assert(i < Values.size() && "List element index out of range!");
627 return Values[i];
628 }
629
Chris Lattnercbebe462007-02-27 22:08:27 +0000630 Record *getElementAsRecord(unsigned i) const;
631
Chris Lattner8bf9e062004-07-26 23:21:34 +0000632 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
633
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000634 virtual Init *convertInitializerTo(RecTy *Ty) {
635 return Ty->convertValue(this);
636 }
637
Chris Lattner577fc3f2004-07-27 01:01:21 +0000638 /// resolveReferences - This method is used by classes that refer to other
639 /// variables which may not be defined at the time they expression is formed.
640 /// If a value is set for the variable later, this method will be called on
641 /// users of the value to allow the value to propagate out.
642 ///
Chris Lattneref943742005-04-19 03:36:21 +0000643 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000644
Chris Lattner695506c2007-11-22 21:05:25 +0000645 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000646
647 typedef std::vector<Init*>::iterator iterator;
648 typedef std::vector<Init*>::const_iterator const_iterator;
649
650 inline iterator begin() { return Values.begin(); }
651 inline const_iterator begin() const { return Values.begin(); }
652 inline iterator end () { return Values.end(); }
653 inline const_iterator end () const { return Values.end(); }
654
655 inline size_t size () const { return Values.size(); }
656 inline bool empty() const { return Values.empty(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000657};
658
Chris Lattner51ffbf12006-03-31 21:53:49 +0000659/// BinOpInit - !op (X, Y) - Combine two inits.
660///
661class BinOpInit : public Init {
662public:
David Greenea9c6c5d2009-04-22 20:18:10 +0000663 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
Chris Lattner51ffbf12006-03-31 21:53:49 +0000664private:
665 BinaryOp Opc;
666 Init *LHS, *RHS;
667public:
668 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs) : Opc(opc), LHS(lhs), RHS(rhs) {
669 }
670
671 BinaryOp getOpcode() const { return Opc; }
672 Init *getLHS() const { return LHS; }
673 Init *getRHS() const { return RHS; }
674
675 // Fold - If possible, fold this to a simpler init. Return this if not
676 // possible to fold.
David Greenea9c6c5d2009-04-22 20:18:10 +0000677 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000678
679 virtual Init *convertInitializerTo(RecTy *Ty) {
680 return Ty->convertValue(this);
681 }
682
683 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
684
Chris Lattner695506c2007-11-22 21:05:25 +0000685 virtual std::string getAsString() const;
Chris Lattner51ffbf12006-03-31 21:53:49 +0000686};
687
688
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000689
690/// TypedInit - This is the common super-class of types that have a specific,
691/// explicit, type.
692///
693class TypedInit : public Init {
694 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000695public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000696 explicit TypedInit(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000697
698 RecTy *getType() const { return Ty; }
699
Chris Lattner577fc3f2004-07-27 01:01:21 +0000700 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
701 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
702
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000703 /// resolveBitReference - This method is used to implement
704 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000705 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000706 ///
Chris Lattneref943742005-04-19 03:36:21 +0000707 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
708 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000709
710 /// resolveListElementReference - This method is used to implement
711 /// VarListElementInit::resolveReferences. If the list element is resolvable
712 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000713 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
714 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000715};
716
717/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
718///
719class VarInit : public TypedInit {
720 std::string VarName;
721public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000722 explicit VarInit(const std::string &VN, RecTy *T)
723 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000724
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000725 virtual Init *convertInitializerTo(RecTy *Ty) {
726 return Ty->convertValue(this);
727 }
728
729 const std::string &getName() const { return VarName; }
730
Chris Lattneref943742005-04-19 03:36:21 +0000731 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
732 unsigned Bit);
733 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
734 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000735
736 virtual RecTy *getFieldType(const std::string &FieldName) const;
737 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
738
739 /// resolveReferences - This method is used by classes that refer to other
740 /// variables which may not be defined at the time they expression is formed.
741 /// If a value is set for the variable later, this method will be called on
742 /// users of the value to allow the value to propagate out.
743 ///
Chris Lattneref943742005-04-19 03:36:21 +0000744 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000745
Chris Lattner695506c2007-11-22 21:05:25 +0000746 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000747};
748
749
750/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
751///
752class VarBitInit : public Init {
753 TypedInit *TI;
754 unsigned Bit;
755public:
756 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
757 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
758 ((BitsRecTy*)T->getType())->getNumBits() > B &&
759 "Illegal VarBitInit expression!");
760 }
761
762 virtual Init *convertInitializerTo(RecTy *Ty) {
763 return Ty->convertValue(this);
764 }
765
766 TypedInit *getVariable() const { return TI; }
767 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000768
Chris Lattner695506c2007-11-22 21:05:25 +0000769 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000770 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000771};
772
Misha Brukman650ba8e2005-04-22 00:00:37 +0000773/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000774/// field.
775class VarListElementInit : public TypedInit {
776 TypedInit *TI;
777 unsigned Element;
778public:
779 VarListElementInit(TypedInit *T, unsigned E)
780 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
781 TI(T), Element(E) {
782 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
783 "Illegal VarBitInit expression!");
784 }
785
786 virtual Init *convertInitializerTo(RecTy *Ty) {
787 return Ty->convertValue(this);
788 }
789
790 TypedInit *getVariable() const { return TI; }
791 unsigned getElementNum() const { return Element; }
792
Chris Lattneref943742005-04-19 03:36:21 +0000793 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
794 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000795
796 /// resolveListElementReference - This method is used to implement
797 /// VarListElementInit::resolveReferences. If the list element is resolvable
798 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000799 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
800 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000801
Chris Lattner695506c2007-11-22 21:05:25 +0000802 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000803 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000804};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000805
806/// DefInit - AL - Represent a reference to a 'def' in the description
807///
808class DefInit : public Init {
809 Record *Def;
810public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000811 explicit DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000812
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000813 virtual Init *convertInitializerTo(RecTy *Ty) {
814 return Ty->convertValue(this);
815 }
816
817 Record *getDef() const { return Def; }
818
819 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
820
821 virtual RecTy *getFieldType(const std::string &FieldName) const;
822 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000823
Chris Lattner695506c2007-11-22 21:05:25 +0000824 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000825};
826
827
828/// FieldInit - X.Y - Represent a reference to a subfield of a variable
829///
830class FieldInit : public TypedInit {
831 Init *Rec; // Record we are referring to
832 std::string FieldName; // Field we are accessing
833public:
834 FieldInit(Init *R, const std::string &FN)
835 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
836 assert(getType() && "FieldInit with non-record type!");
837 }
838
839 virtual Init *convertInitializerTo(RecTy *Ty) {
840 return Ty->convertValue(this);
841 }
842
Chris Lattneref943742005-04-19 03:36:21 +0000843 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
844 unsigned Bit);
845 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
846 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000847
Chris Lattneref943742005-04-19 03:36:21 +0000848 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000849
Chris Lattner695506c2007-11-22 21:05:25 +0000850 virtual std::string getAsString() const {
851 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000852 }
853};
854
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000855/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
856/// to have at least one value then a (possibly empty) list of arguments. Each
857/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000858///
859class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000860 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +0000861 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000862 std::vector<Init*> Args;
863 std::vector<std::string> ArgNames;
864public:
Nate Begemandbe3f772009-03-19 05:21:56 +0000865 DagInit(Init *V, std::string VN,
866 const std::vector<std::pair<Init*, std::string> > &args)
867 : Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000868 Args.reserve(args.size());
869 ArgNames.reserve(args.size());
870 for (unsigned i = 0, e = args.size(); i != e; ++i) {
871 Args.push_back(args[i].first);
872 ArgNames.push_back(args[i].second);
873 }
874 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000875 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +0000876 const std::vector<std::string> &argNames)
Nate Begemandbe3f772009-03-19 05:21:56 +0000877 : Val(V), ValName(VN), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +0000878 }
879
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000880 virtual Init *convertInitializerTo(RecTy *Ty) {
881 return Ty->convertValue(this);
882 }
883
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000884 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000885
Nate Begemandbe3f772009-03-19 05:21:56 +0000886 const std::string &getName() const { return ValName; }
887
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000888 unsigned getNumArgs() const { return Args.size(); }
889 Init *getArg(unsigned Num) const {
890 assert(Num < Args.size() && "Arg number out of range!");
891 return Args[Num];
892 }
893 const std::string &getArgName(unsigned Num) const {
894 assert(Num < ArgNames.size() && "Arg number out of range!");
895 return ArgNames[Num];
896 }
897
898 void setArg(unsigned Num, Init *I) {
899 assert(Num < Args.size() && "Arg number out of range!");
900 Args[Num] = I;
901 }
Chris Lattner0d3ef402006-01-31 06:02:35 +0000902
903 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000904
Chris Lattner695506c2007-11-22 21:05:25 +0000905 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +0000906
907 typedef std::vector<Init*>::iterator arg_iterator;
908 typedef std::vector<Init*>::const_iterator const_arg_iterator;
909 typedef std::vector<std::string>::iterator name_iterator;
910 typedef std::vector<std::string>::const_iterator const_name_iterator;
911
912 inline arg_iterator arg_begin() { return Args.begin(); }
913 inline const_arg_iterator arg_begin() const { return Args.begin(); }
914 inline arg_iterator arg_end () { return Args.end(); }
915 inline const_arg_iterator arg_end () const { return Args.end(); }
916
917 inline size_t arg_size () const { return Args.size(); }
918 inline bool arg_empty() const { return Args.empty(); }
919
920 inline name_iterator name_begin() { return ArgNames.begin(); }
921 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
922 inline name_iterator name_end () { return ArgNames.end(); }
923 inline const_name_iterator name_end () const { return ArgNames.end(); }
924
925 inline size_t name_size () const { return ArgNames.size(); }
926 inline bool name_empty() const { return ArgNames.empty(); }
927
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000928};
929
930//===----------------------------------------------------------------------===//
931// High-Level Classes
932//===----------------------------------------------------------------------===//
933
934class RecordVal {
935 std::string Name;
936 RecTy *Ty;
937 unsigned Prefix;
938 Init *Value;
939public:
940 RecordVal(const std::string &N, RecTy *T, unsigned P);
941
942 const std::string &getName() const { return Name; }
943
944 unsigned getPrefix() const { return Prefix; }
945 RecTy *getType() const { return Ty; }
946 Init *getValue() const { return Value; }
947
948 bool setValue(Init *V) {
949 if (V) {
950 Value = V->convertInitializerTo(Ty);
951 return Value == 0;
952 }
953 Value = 0;
954 return false;
955 }
956
957 void dump() const;
958 void print(std::ostream &OS, bool PrintSem = true) const;
959};
960
961inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
962 RV.print(OS << " ");
963 return OS;
964}
965
Chris Lattner87a10612004-10-23 04:58:50 +0000966class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +0000967 std::string Name;
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000968 TGLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000969 std::vector<std::string> TemplateArgs;
970 std::vector<RecordVal> Values;
971 std::vector<Record*> SuperClasses;
972public:
973
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000974 explicit Record(const std::string &N, TGLoc loc) : Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000975 ~Record() {}
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000976
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000977 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +0000978 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000979
980 TGLoc getLoc() const { return Loc; }
981
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000982 const std::vector<std::string> &getTemplateArgs() const {
983 return TemplateArgs;
984 }
985 const std::vector<RecordVal> &getValues() const { return Values; }
986 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
987
988 bool isTemplateArg(const std::string &Name) const {
989 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
990 if (TemplateArgs[i] == Name) return true;
991 return false;
992 }
993
994 const RecordVal *getValue(const std::string &Name) const {
995 for (unsigned i = 0, e = Values.size(); i != e; ++i)
996 if (Values[i].getName() == Name) return &Values[i];
997 return 0;
998 }
999 RecordVal *getValue(const std::string &Name) {
1000 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1001 if (Values[i].getName() == Name) return &Values[i];
1002 return 0;
1003 }
1004
1005 void addTemplateArg(const std::string &Name) {
1006 assert(!isTemplateArg(Name) && "Template arg already defined!");
1007 TemplateArgs.push_back(Name);
1008 }
1009
1010 void addValue(const RecordVal &RV) {
1011 assert(getValue(RV.getName()) == 0 && "Value already added!");
1012 Values.push_back(RV);
1013 }
1014
1015 void removeValue(const std::string &Name) {
1016 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1017 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1018 if (Values[i].getName() == Name) {
1019 Values.erase(Values.begin()+i);
1020 return;
1021 }
1022 assert(0 && "Name does not exist in record!");
1023 }
1024
Ted Kremenek58e32872009-03-13 22:20:10 +00001025 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001026 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1027 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001028 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001029 return false;
1030 }
1031
1032 bool isSubClassOf(const std::string &Name) const {
1033 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1034 if (SuperClasses[i]->getName() == Name)
1035 return true;
1036 return false;
1037 }
1038
1039 void addSuperClass(Record *R) {
1040 assert(!isSubClassOf(R) && "Already subclassing record!");
1041 SuperClasses.push_back(R);
1042 }
1043
Chris Lattneref943742005-04-19 03:36:21 +00001044 /// resolveReferences - If there are any field references that refer to fields
1045 /// that have been filled in, we can propagate the values now.
1046 ///
1047 void resolveReferences() { resolveReferencesTo(0); }
1048
1049 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1050 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1051 /// possible references.
1052 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001053
1054 void dump() const;
1055
1056 //===--------------------------------------------------------------------===//
1057 // High-level methods useful to tablegen back-ends
1058 //
1059
1060 /// getValueInit - Return the initializer for a value with the specified name,
1061 /// or throw an exception if the field does not exist.
1062 ///
1063 Init *getValueInit(const std::string &FieldName) const;
1064
1065 /// getValueAsString - This method looks up the specified field and returns
1066 /// its value as a string, throwing an exception if the field does not exist
1067 /// or if the value is not a string.
1068 ///
1069 std::string getValueAsString(const std::string &FieldName) const;
1070
1071 /// getValueAsBitsInit - This method looks up the specified field and returns
1072 /// its value as a BitsInit, throwing an exception if the field does not exist
1073 /// or if the value is not the right type.
1074 ///
1075 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1076
1077 /// getValueAsListInit - This method looks up the specified field and returns
1078 /// its value as a ListInit, throwing an exception if the field does not exist
1079 /// or if the value is not the right type.
1080 ///
1081 ListInit *getValueAsListInit(const std::string &FieldName) const;
1082
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001083 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001084 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001085 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001086 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001087 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001088
Anton Korobeynikova468a112007-11-11 11:19:37 +00001089 /// getValueAsListOfInts - This method looks up the specified field and returns
1090 /// its value as a vector of integers, throwing an exception if the field does
1091 /// not exist or if the value is not the right type.
1092 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001093 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001094
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001095 /// getValueAsDef - This method looks up the specified field and returns its
1096 /// value as a Record, throwing an exception if the field does not exist or if
1097 /// the value is not the right type.
1098 ///
1099 Record *getValueAsDef(const std::string &FieldName) const;
1100
1101 /// getValueAsBit - This method looks up the specified field and returns its
1102 /// value as a bit, throwing an exception if the field does not exist or if
1103 /// the value is not the right type.
1104 ///
1105 bool getValueAsBit(const std::string &FieldName) const;
1106
1107 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001108 /// value as an int64_t, throwing an exception if the field does not exist or
1109 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001110 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001111 int64_t getValueAsInt(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001112
1113 /// getValueAsDag - This method looks up the specified field and returns its
1114 /// value as an Dag, throwing an exception if the field does not exist or if
1115 /// the value is not the right type.
1116 ///
1117 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001118
1119 /// getValueAsCode - This method looks up the specified field and returns
1120 /// its value as the string data in a CodeInit, throwing an exception if the
1121 /// field does not exist or if the value is not a code object.
1122 ///
1123 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001124};
1125
1126std::ostream &operator<<(std::ostream &OS, const Record &R);
1127
David Greenea9c6c5d2009-04-22 20:18:10 +00001128struct MultiClass {
1129 Record Rec; // Placeholder for template args and Name.
1130 typedef std::vector<Record*> RecordVector;
1131 RecordVector DefPrototypes;
1132
1133 MultiClass(const std::string &Name, TGLoc Loc) : Rec(Name, Loc) {}
1134};
1135
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001136class RecordKeeper {
1137 std::map<std::string, Record*> Classes, Defs;
1138public:
1139 ~RecordKeeper() {
1140 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001141 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001142 delete I->second;
1143 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001144 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001145 delete I->second;
1146 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001147
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001148 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1149 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1150
1151 Record *getClass(const std::string &Name) const {
1152 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1153 return I == Classes.end() ? 0 : I->second;
1154 }
1155 Record *getDef(const std::string &Name) const {
1156 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1157 return I == Defs.end() ? 0 : I->second;
1158 }
1159 void addClass(Record *R) {
1160 assert(getClass(R->getName()) == 0 && "Class already exists!");
1161 Classes.insert(std::make_pair(R->getName(), R));
1162 }
1163 void addDef(Record *R) {
1164 assert(getDef(R->getName()) == 0 && "Def already exists!");
1165 Defs.insert(std::make_pair(R->getName(), R));
1166 }
1167
Chris Lattnerac284252005-08-19 17:58:11 +00001168 /// removeClass - Remove, but do not delete, the specified record.
1169 ///
1170 void removeClass(const std::string &Name) {
1171 assert(Classes.count(Name) && "Class does not exist!");
1172 Classes.erase(Name);
1173 }
1174 /// removeDef - Remove, but do not delete, the specified record.
1175 ///
1176 void removeDef(const std::string &Name) {
1177 assert(Defs.count(Name) && "Def does not exist!");
1178 Defs.erase(Name);
1179 }
1180
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001181 //===--------------------------------------------------------------------===//
1182 // High-level helper methods, useful for tablegen backends...
1183
1184 /// getAllDerivedDefinitions - This method returns all concrete definitions
1185 /// that derive from the specified class name. If a class with the specified
1186 /// name does not exist, an exception is thrown.
1187 std::vector<Record*>
1188 getAllDerivedDefinitions(const std::string &ClassName) const;
1189
1190
1191 void dump() const;
1192};
1193
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001194/// LessRecord - Sorting predicate to sort record pointers by name.
1195///
1196struct LessRecord {
1197 bool operator()(const Record *Rec1, const Record *Rec2) const {
1198 return Rec1->getName() < Rec2->getName();
1199 }
1200};
1201
Jim Grosbach56938af2008-09-11 17:05:32 +00001202/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1203/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001204///
1205struct LessRecordFieldName {
1206 bool operator()(const Record *Rec1, const Record *Rec2) const {
1207 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1208 }
1209};
1210
Chris Lattnerba42e492009-03-13 16:25:21 +00001211
1212class TGError {
1213 TGLoc Loc;
1214 std::string Message;
1215public:
1216 TGError(TGLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1217
1218 TGLoc getLoc() const { return Loc; }
1219 const std::string &getMessage() const { return Message; }
1220};
1221
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001222
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001223std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1224
1225extern RecordKeeper Records;
1226
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001227void PrintError(TGLoc ErrorLoc, const std::string &Msg);
1228
1229
Brian Gaeke960707c2003-11-11 22:41:34 +00001230} // End llvm namespace
1231
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001232#endif