blob: 615ecebecf8c21094e4f6ade377ce9a662586035 [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; }
David Greene196ac3c2009-04-23 21:25:15 +000080 virtual Init *convertValue( BinOpInit *UI) {
81 return convertValue((TypedInit*)UI);
82 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000083 virtual Init *convertValue( CodeInit *CI) { return 0; }
84 virtual Init *convertValue(VarBitInit *VB) { return 0; }
85 virtual Init *convertValue( DefInit *DI) { return 0; }
86 virtual Init *convertValue( DagInit *DI) { return 0; }
87 virtual Init *convertValue( TypedInit *TI) { return 0; }
88 virtual Init *convertValue( VarInit *VI) {
89 return convertValue((TypedInit*)VI);
90 }
91 virtual Init *convertValue( FieldInit *FI) {
92 return convertValue((TypedInit*)FI);
93 }
94
95public: // These methods should only be called by subclasses of RecTy.
96 // baseClassOf - These virtual methods should be overloaded to return true iff
97 // all values of type 'RHS' can be converted to the 'this' type.
98 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
99 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
100 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
101 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
102 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
103 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
104 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
105 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
106};
107
108inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
109 Ty.print(OS);
110 return OS;
111}
112
113
114/// BitRecTy - 'bit' - Represent a single bit
115///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000116class BitRecTy : public RecTy {
117public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000118 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
119 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
120 virtual Init *convertValue( BitsInit *BI);
121 virtual Init *convertValue( IntInit *II);
122 virtual Init *convertValue(StringInit *SI) { return 0; }
123 virtual Init *convertValue( ListInit *LI) { return 0; }
124 virtual Init *convertValue( CodeInit *CI) { return 0; }
125 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
126 virtual Init *convertValue( DefInit *DI) { return 0; }
127 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000128 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000129 virtual Init *convertValue( TypedInit *TI);
130 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
131 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000132
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000133 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000134
135 bool typeIsConvertibleTo(const RecTy *RHS) const {
136 return RHS->baseClassOf(this);
137 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000138 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
139 virtual bool baseClassOf(const BitsRecTy *RHS) const;
140 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
141 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
142 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
143 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
144 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
145 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
146
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000147};
148
149
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000150// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
151/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000152///
153class BitsRecTy : public RecTy {
154 unsigned Size;
155public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000156 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000157
158 unsigned getNumBits() const { return Size; }
159
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000160 virtual Init *convertValue( UnsetInit *UI);
161 virtual Init *convertValue( BitInit *UI);
162 virtual Init *convertValue( BitsInit *BI);
163 virtual Init *convertValue( IntInit *II);
164 virtual Init *convertValue(StringInit *SI) { return 0; }
165 virtual Init *convertValue( ListInit *LI) { return 0; }
166 virtual Init *convertValue( CodeInit *CI) { return 0; }
167 virtual Init *convertValue(VarBitInit *VB) { return 0; }
168 virtual Init *convertValue( DefInit *DI) { return 0; }
169 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000170 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000171 virtual Init *convertValue( TypedInit *TI);
172 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
173 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
174
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000175 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000176
177 bool typeIsConvertibleTo(const RecTy *RHS) const {
178 return RHS->baseClassOf(this);
179 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000180 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
181 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000182 return RHS->Size == Size;
183 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000184 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
185 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
186 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
187 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
188 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
189 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
190
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000191};
192
193
194/// IntRecTy - 'int' - Represent an integer value of no particular size
195///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000196class IntRecTy : public RecTy {
197public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000198 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
199 virtual Init *convertValue( BitInit *BI);
200 virtual Init *convertValue( BitsInit *BI);
201 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
202 virtual Init *convertValue(StringInit *SI) { return 0; }
203 virtual Init *convertValue( ListInit *LI) { return 0; }
204 virtual Init *convertValue( CodeInit *CI) { return 0; }
205 virtual Init *convertValue(VarBitInit *VB) { return 0; }
206 virtual Init *convertValue( DefInit *DI) { return 0; }
207 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000208 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000209 virtual Init *convertValue( TypedInit *TI);
210 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
211 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
212
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000213 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000214
215 bool typeIsConvertibleTo(const RecTy *RHS) const {
216 return RHS->baseClassOf(this);
217 }
218
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000219 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
220 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
221 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
222 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
223 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
224 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
225 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
226 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
227
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000228};
229
230/// StringRecTy - 'string' - Represent an string value
231///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000232class StringRecTy : public RecTy {
233public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000234 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
235 virtual Init *convertValue( BitInit *BI) { return 0; }
236 virtual Init *convertValue( BitsInit *BI) { return 0; }
237 virtual Init *convertValue( IntInit *II) { return 0; }
238 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
239 virtual Init *convertValue( ListInit *LI) { return 0; }
Chris Lattner51ffbf12006-03-31 21:53:49 +0000240 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000241 virtual Init *convertValue( CodeInit *CI) { return 0; }
242 virtual Init *convertValue(VarBitInit *VB) { return 0; }
243 virtual Init *convertValue( DefInit *DI) { return 0; }
244 virtual Init *convertValue( DagInit *DI) { return 0; }
245 virtual Init *convertValue( TypedInit *TI);
246 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
247 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
248
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000249 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000250
251 bool typeIsConvertibleTo(const RecTy *RHS) const {
252 return RHS->baseClassOf(this);
253 }
254
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000255 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
256 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
257 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000258 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000259 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
260 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
261 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
262 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000263};
264
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000265// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
266// the specified type.
267/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
268/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000269///
270class ListRecTy : public RecTy {
271 RecTy *Ty;
272public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000273 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000274
275 RecTy *getElementType() const { return Ty; }
276
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000277 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
278 virtual Init *convertValue( BitInit *BI) { return 0; }
279 virtual Init *convertValue( BitsInit *BI) { return 0; }
280 virtual Init *convertValue( IntInit *II) { return 0; }
281 virtual Init *convertValue(StringInit *SI) { return 0; }
282 virtual Init *convertValue( ListInit *LI);
283 virtual Init *convertValue( CodeInit *CI) { return 0; }
284 virtual Init *convertValue(VarBitInit *VB) { return 0; }
285 virtual Init *convertValue( DefInit *DI) { return 0; }
286 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000287 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000288 virtual Init *convertValue( TypedInit *TI);
289 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
290 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000291
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000292 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000293
294 bool typeIsConvertibleTo(const RecTy *RHS) const {
295 return RHS->baseClassOf(this);
296 }
297
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000298 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
299 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
300 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
301 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
302 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000303 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000304 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000305 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
306 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
307 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000308};
309
310/// CodeRecTy - 'code' - Represent an code fragment, function or method.
311///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000312class CodeRecTy : public RecTy {
313public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000314 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
315 virtual Init *convertValue( BitInit *BI) { return 0; }
316 virtual Init *convertValue( BitsInit *BI) { return 0; }
317 virtual Init *convertValue( IntInit *II) { return 0; }
318 virtual Init *convertValue(StringInit *SI) { return 0; }
319 virtual Init *convertValue( ListInit *LI) { return 0; }
320 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
321 virtual Init *convertValue(VarBitInit *VB) { return 0; }
322 virtual Init *convertValue( DefInit *DI) { return 0; }
323 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000324 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000325 virtual Init *convertValue( TypedInit *TI);
326 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
327 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
328
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000329 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000330
331 bool typeIsConvertibleTo(const RecTy *RHS) const {
332 return RHS->baseClassOf(this);
333 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000334 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
335 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
336 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
337 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
338 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
339 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
340 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
341 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000342};
343
344/// DagRecTy - 'dag' - Represent a dag fragment
345///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000346class DagRecTy : public RecTy {
347public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000348 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
349 virtual Init *convertValue( BitInit *BI) { return 0; }
350 virtual Init *convertValue( BitsInit *BI) { return 0; }
351 virtual Init *convertValue( IntInit *II) { return 0; }
352 virtual Init *convertValue(StringInit *SI) { return 0; }
353 virtual Init *convertValue( ListInit *LI) { return 0; }
354 virtual Init *convertValue( CodeInit *CI) { return 0; }
355 virtual Init *convertValue(VarBitInit *VB) { return 0; }
356 virtual Init *convertValue( DefInit *DI) { return 0; }
Evan Chenga32dee22007-05-15 01:23:24 +0000357 virtual Init *convertValue( BinOpInit *BO);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000358 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
359 virtual Init *convertValue( TypedInit *TI);
360 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
361 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000362
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000363 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000364
365 bool typeIsConvertibleTo(const RecTy *RHS) const {
366 return RHS->baseClassOf(this);
367 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000368
369 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
370 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
371 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
372 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
373 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
374 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
375 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
376 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000377};
378
379
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000380/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000381/// (R32 X = EAX).
382///
383class RecordRecTy : public RecTy {
384 Record *Rec;
385public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000386 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000387
388 Record *getRecord() const { return Rec; }
389
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000390 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
391 virtual Init *convertValue( BitInit *BI) { return 0; }
392 virtual Init *convertValue( BitsInit *BI) { return 0; }
393 virtual Init *convertValue( IntInit *II) { return 0; }
394 virtual Init *convertValue(StringInit *SI) { return 0; }
395 virtual Init *convertValue( ListInit *LI) { return 0; }
396 virtual Init *convertValue( CodeInit *CI) { return 0; }
397 virtual Init *convertValue(VarBitInit *VB) { return 0; }
David Greene196ac3c2009-04-23 21:25:15 +0000398 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000399 virtual Init *convertValue( DefInit *DI);
400 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000401 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000402 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
403 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000405 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000406
407 bool typeIsConvertibleTo(const RecTy *RHS) const {
408 return RHS->baseClassOf(this);
409 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000410 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
411 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
412 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
413 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
414 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
415 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
416 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000417 virtual bool baseClassOf(const RecordRecTy *RHS) const;
418};
419
420
421
422//===----------------------------------------------------------------------===//
423// Initializer Classes
424//===----------------------------------------------------------------------===//
425
426struct Init {
427 virtual ~Init() {}
428
429 /// isComplete - This virtual method should be overridden by values that may
430 /// not be completely specified yet.
431 virtual bool isComplete() const { return true; }
432
433 /// print - Print out this value.
Chris Lattner695506c2007-11-22 21:05:25 +0000434 void print(std::ostream &OS) const { OS << getAsString(); }
435
436 /// getAsString - Convert this value to a string form.
437 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000438
439 /// dump - Debugging method that may be called through a debugger, just
440 /// invokes print on cerr.
441 void dump() const;
442
443 /// convertInitializerTo - This virtual function is a simple call-back
444 /// function that should be overridden to call the appropriate
445 /// RecTy::convertValue method.
446 ///
447 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
448
449 /// convertInitializerBitRange - This method is used to implement the bitrange
450 /// selection operator. Given an initializer, it selects the specified bits
451 /// out, returning them as a new init of bits type. If it is not legal to use
452 /// the bit subscript operator on this initializer, return null.
453 ///
454 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
455 return 0;
456 }
457
Chris Lattner8bf9e062004-07-26 23:21:34 +0000458 /// convertInitListSlice - This method is used to implement the list slice
459 /// selection operator. Given an initializer, it selects the specified list
460 /// elements, returning them as a new init of list type. If it is not legal
461 /// to take a slice of this, return null.
462 ///
463 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
464 return 0;
465 }
466
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000467 /// getFieldType - This method is used to implement the FieldInit class.
468 /// Implementors of this method should return the type of the named field if
469 /// they are of record type.
470 ///
471 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
472
473 /// getFieldInit - This method complements getFieldType to return the
474 /// initializer for the specified field. If getFieldType returns non-null
475 /// this method should return non-null, otherwise it returns null.
476 ///
477 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
478 return 0;
479 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000480
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000481 /// resolveReferences - This method is used by classes that refer to other
482 /// variables which may not be defined at the time they expression is formed.
483 /// If a value is set for the variable later, this method will be called on
484 /// users of the value to allow the value to propagate out.
485 ///
Chris Lattneref943742005-04-19 03:36:21 +0000486 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
487 return this;
488 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000489};
490
491inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
492 I.print(OS); return OS;
493}
494
495
496/// UnsetInit - ? - Represents an uninitialized value
497///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000498class UnsetInit : public Init {
499public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000500 virtual Init *convertInitializerTo(RecTy *Ty) {
501 return Ty->convertValue(this);
502 }
503
504 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000505 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000506};
507
508
509/// BitInit - true/false - Represent a concrete initializer for a bit.
510///
511class BitInit : public Init {
512 bool Value;
513public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000514 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000515
516 bool getValue() const { return Value; }
517
518 virtual Init *convertInitializerTo(RecTy *Ty) {
519 return Ty->convertValue(this);
520 }
521
Chris Lattner695506c2007-11-22 21:05:25 +0000522 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000523};
524
525/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
526/// It contains a vector of bits, whose size is determined by the type.
527///
528class BitsInit : public Init {
529 std::vector<Init*> Bits;
530public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000531 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000532
533 unsigned getNumBits() const { return Bits.size(); }
534
535 Init *getBit(unsigned Bit) const {
536 assert(Bit < Bits.size() && "Bit index out of range!");
537 return Bits[Bit];
538 }
539 void setBit(unsigned Bit, Init *V) {
540 assert(Bit < Bits.size() && "Bit index out of range!");
541 assert(Bits[Bit] == 0 && "Bit already set!");
542 Bits[Bit] = V;
543 }
544
545 virtual Init *convertInitializerTo(RecTy *Ty) {
546 return Ty->convertValue(this);
547 }
548 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
549
550 virtual bool isComplete() const {
551 for (unsigned i = 0; i != getNumBits(); ++i)
552 if (!getBit(i)->isComplete()) return false;
553 return true;
554 }
Chris Lattner695506c2007-11-22 21:05:25 +0000555 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000556
Chris Lattneref943742005-04-19 03:36:21 +0000557 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000558
559 // printXX - Print this bitstream with the specified format, returning true if
560 // it is not possible.
561 bool printInHex(std::ostream &OS) const;
562 bool printAsVariable(std::ostream &OS) const;
563 bool printAsUnset(std::ostream &OS) const;
564};
565
566
567/// IntInit - 7 - Represent an initalization by a literal integer value.
568///
569class IntInit : public Init {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000570 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000571public:
Dan Gohmanca0546f2008-10-17 01:33:43 +0000572 explicit IntInit(int64_t V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000573
Dan Gohmanca0546f2008-10-17 01:33:43 +0000574 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000575
576 virtual Init *convertInitializerTo(RecTy *Ty) {
577 return Ty->convertValue(this);
578 }
579 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
580
Chris Lattner695506c2007-11-22 21:05:25 +0000581 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000582};
583
584
585/// StringInit - "foo" - Represent an initialization by a string value.
586///
587class StringInit : public Init {
588 std::string Value;
589public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000590 explicit StringInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000591
592 const std::string &getValue() const { return Value; }
593
594 virtual Init *convertInitializerTo(RecTy *Ty) {
595 return Ty->convertValue(this);
596 }
597
Chris Lattner695506c2007-11-22 21:05:25 +0000598 virtual std::string getAsString() const { return "\"" + Value + "\""; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000599};
600
601/// CodeInit - "[{...}]" - Represent a code fragment.
602///
603class CodeInit : public Init {
604 std::string Value;
605public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000606 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000607
608 const std::string getValue() const { return Value; }
609
610 virtual Init *convertInitializerTo(RecTy *Ty) {
611 return Ty->convertValue(this);
612 }
613
Chris Lattner695506c2007-11-22 21:05:25 +0000614 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000615};
616
617/// ListInit - [AL, AH, CL] - Represent a list of defs
618///
619class ListInit : public Init {
620 std::vector<Init*> Values;
621public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000622 explicit ListInit(std::vector<Init*> &Vs) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000623 Values.swap(Vs);
624 }
625
626 unsigned getSize() const { return Values.size(); }
627 Init *getElement(unsigned i) const {
628 assert(i < Values.size() && "List element index out of range!");
629 return Values[i];
630 }
631
Chris Lattnercbebe462007-02-27 22:08:27 +0000632 Record *getElementAsRecord(unsigned i) const;
633
Chris Lattner8bf9e062004-07-26 23:21:34 +0000634 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
635
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000636 virtual Init *convertInitializerTo(RecTy *Ty) {
637 return Ty->convertValue(this);
638 }
639
Chris Lattner577fc3f2004-07-27 01:01:21 +0000640 /// resolveReferences - This method is used by classes that refer to other
641 /// variables which may not be defined at the time they expression is formed.
642 /// If a value is set for the variable later, this method will be called on
643 /// users of the value to allow the value to propagate out.
644 ///
Chris Lattneref943742005-04-19 03:36:21 +0000645 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000646
Chris Lattner695506c2007-11-22 21:05:25 +0000647 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000648
649 typedef std::vector<Init*>::iterator iterator;
650 typedef std::vector<Init*>::const_iterator const_iterator;
651
652 inline iterator begin() { return Values.begin(); }
653 inline const_iterator begin() const { return Values.begin(); }
654 inline iterator end () { return Values.end(); }
655 inline const_iterator end () const { return Values.end(); }
656
657 inline size_t size () const { return Values.size(); }
658 inline bool empty() const { return Values.empty(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000659};
660
661
662/// TypedInit - This is the common super-class of types that have a specific,
663/// explicit, type.
664///
665class TypedInit : public Init {
666 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000667public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000668 explicit TypedInit(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000669
670 RecTy *getType() const { return Ty; }
671
Chris Lattner577fc3f2004-07-27 01:01:21 +0000672 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
673 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
674
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000675 /// resolveBitReference - This method is used to implement
676 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000677 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000678 ///
Chris Lattneref943742005-04-19 03:36:21 +0000679 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
680 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000681
682 /// resolveListElementReference - This method is used to implement
683 /// VarListElementInit::resolveReferences. If the list element is resolvable
684 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000685 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
686 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000687};
688
David Greene196ac3c2009-04-23 21:25:15 +0000689
690/// BinOpInit - !op (X, Y) - Combine two inits.
691///
692class BinOpInit : public TypedInit {
693public:
694 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
695private:
696 BinaryOp Opc;
697 Init *LHS, *RHS;
698public:
699 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
700 TypedInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
701 }
702
703 BinaryOp getOpcode() const { return Opc; }
704 Init *getLHS() const { return LHS; }
705 Init *getRHS() const { return RHS; }
706
707 // Fold - If possible, fold this to a simpler init. Return this if not
708 // possible to fold.
709 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
710
711 virtual Init *convertInitializerTo(RecTy *Ty) {
712 return Ty->convertValue(this);
713 }
714
715 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
716 unsigned Bit);
717 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
718 unsigned Elt);
719
720 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
721
722 virtual std::string getAsString() const;
723};
724
725
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000726/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
727///
728class VarInit : public TypedInit {
729 std::string VarName;
730public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000731 explicit VarInit(const std::string &VN, RecTy *T)
732 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000733
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000734 virtual Init *convertInitializerTo(RecTy *Ty) {
735 return Ty->convertValue(this);
736 }
737
738 const std::string &getName() const { return VarName; }
739
Chris Lattneref943742005-04-19 03:36:21 +0000740 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
741 unsigned Bit);
742 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
743 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000744
745 virtual RecTy *getFieldType(const std::string &FieldName) const;
746 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
747
748 /// resolveReferences - This method is used by classes that refer to other
749 /// variables which may not be defined at the time they expression is formed.
750 /// If a value is set for the variable later, this method will be called on
751 /// users of the value to allow the value to propagate out.
752 ///
Chris Lattneref943742005-04-19 03:36:21 +0000753 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000754
Chris Lattner695506c2007-11-22 21:05:25 +0000755 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000756};
757
758
759/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
760///
761class VarBitInit : public Init {
762 TypedInit *TI;
763 unsigned Bit;
764public:
765 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
766 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
767 ((BitsRecTy*)T->getType())->getNumBits() > B &&
768 "Illegal VarBitInit expression!");
769 }
770
771 virtual Init *convertInitializerTo(RecTy *Ty) {
772 return Ty->convertValue(this);
773 }
774
775 TypedInit *getVariable() const { return TI; }
776 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000777
Chris Lattner695506c2007-11-22 21:05:25 +0000778 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000779 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000780};
781
Misha Brukman650ba8e2005-04-22 00:00:37 +0000782/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000783/// field.
784class VarListElementInit : public TypedInit {
785 TypedInit *TI;
786 unsigned Element;
787public:
788 VarListElementInit(TypedInit *T, unsigned E)
789 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
790 TI(T), Element(E) {
791 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
792 "Illegal VarBitInit expression!");
793 }
794
795 virtual Init *convertInitializerTo(RecTy *Ty) {
796 return Ty->convertValue(this);
797 }
798
799 TypedInit *getVariable() const { return TI; }
800 unsigned getElementNum() const { return Element; }
801
Chris Lattneref943742005-04-19 03:36:21 +0000802 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
803 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000804
805 /// resolveListElementReference - This method is used to implement
806 /// VarListElementInit::resolveReferences. If the list element is resolvable
807 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000808 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
809 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000810
Chris Lattner695506c2007-11-22 21:05:25 +0000811 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000812 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000813};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000814
815/// DefInit - AL - Represent a reference to a 'def' in the description
816///
817class DefInit : public Init {
818 Record *Def;
819public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000820 explicit DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000821
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000822 virtual Init *convertInitializerTo(RecTy *Ty) {
823 return Ty->convertValue(this);
824 }
825
826 Record *getDef() const { return Def; }
827
828 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
829
830 virtual RecTy *getFieldType(const std::string &FieldName) const;
831 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000832
Chris Lattner695506c2007-11-22 21:05:25 +0000833 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000834};
835
836
837/// FieldInit - X.Y - Represent a reference to a subfield of a variable
838///
839class FieldInit : public TypedInit {
840 Init *Rec; // Record we are referring to
841 std::string FieldName; // Field we are accessing
842public:
843 FieldInit(Init *R, const std::string &FN)
844 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
845 assert(getType() && "FieldInit with non-record type!");
846 }
847
848 virtual Init *convertInitializerTo(RecTy *Ty) {
849 return Ty->convertValue(this);
850 }
851
Chris Lattneref943742005-04-19 03:36:21 +0000852 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
853 unsigned Bit);
854 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
855 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000856
Chris Lattneref943742005-04-19 03:36:21 +0000857 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000858
Chris Lattner695506c2007-11-22 21:05:25 +0000859 virtual std::string getAsString() const {
860 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000861 }
862};
863
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000864/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
865/// to have at least one value then a (possibly empty) list of arguments. Each
866/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000867///
868class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000869 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +0000870 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000871 std::vector<Init*> Args;
872 std::vector<std::string> ArgNames;
873public:
Nate Begemandbe3f772009-03-19 05:21:56 +0000874 DagInit(Init *V, std::string VN,
875 const std::vector<std::pair<Init*, std::string> > &args)
876 : Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000877 Args.reserve(args.size());
878 ArgNames.reserve(args.size());
879 for (unsigned i = 0, e = args.size(); i != e; ++i) {
880 Args.push_back(args[i].first);
881 ArgNames.push_back(args[i].second);
882 }
883 }
Nate Begemandbe3f772009-03-19 05:21:56 +0000884 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +0000885 const std::vector<std::string> &argNames)
Nate Begemandbe3f772009-03-19 05:21:56 +0000886 : Val(V), ValName(VN), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +0000887 }
888
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000889 virtual Init *convertInitializerTo(RecTy *Ty) {
890 return Ty->convertValue(this);
891 }
892
Chris Lattnerb59cf3c2006-03-30 22:50:40 +0000893 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000894
Nate Begemandbe3f772009-03-19 05:21:56 +0000895 const std::string &getName() const { return ValName; }
896
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000897 unsigned getNumArgs() const { return Args.size(); }
898 Init *getArg(unsigned Num) const {
899 assert(Num < Args.size() && "Arg number out of range!");
900 return Args[Num];
901 }
902 const std::string &getArgName(unsigned Num) const {
903 assert(Num < ArgNames.size() && "Arg number out of range!");
904 return ArgNames[Num];
905 }
906
907 void setArg(unsigned Num, Init *I) {
908 assert(Num < Args.size() && "Arg number out of range!");
909 Args[Num] = I;
910 }
Chris Lattner0d3ef402006-01-31 06:02:35 +0000911
912 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000913
Chris Lattner695506c2007-11-22 21:05:25 +0000914 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +0000915
916 typedef std::vector<Init*>::iterator arg_iterator;
917 typedef std::vector<Init*>::const_iterator const_arg_iterator;
918 typedef std::vector<std::string>::iterator name_iterator;
919 typedef std::vector<std::string>::const_iterator const_name_iterator;
920
921 inline arg_iterator arg_begin() { return Args.begin(); }
922 inline const_arg_iterator arg_begin() const { return Args.begin(); }
923 inline arg_iterator arg_end () { return Args.end(); }
924 inline const_arg_iterator arg_end () const { return Args.end(); }
925
926 inline size_t arg_size () const { return Args.size(); }
927 inline bool arg_empty() const { return Args.empty(); }
928
929 inline name_iterator name_begin() { return ArgNames.begin(); }
930 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
931 inline name_iterator name_end () { return ArgNames.end(); }
932 inline const_name_iterator name_end () const { return ArgNames.end(); }
933
934 inline size_t name_size () const { return ArgNames.size(); }
935 inline bool name_empty() const { return ArgNames.empty(); }
936
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000937};
938
939//===----------------------------------------------------------------------===//
940// High-Level Classes
941//===----------------------------------------------------------------------===//
942
943class RecordVal {
944 std::string Name;
945 RecTy *Ty;
946 unsigned Prefix;
947 Init *Value;
948public:
949 RecordVal(const std::string &N, RecTy *T, unsigned P);
950
951 const std::string &getName() const { return Name; }
952
953 unsigned getPrefix() const { return Prefix; }
954 RecTy *getType() const { return Ty; }
955 Init *getValue() const { return Value; }
956
957 bool setValue(Init *V) {
958 if (V) {
959 Value = V->convertInitializerTo(Ty);
960 return Value == 0;
961 }
962 Value = 0;
963 return false;
964 }
965
966 void dump() const;
967 void print(std::ostream &OS, bool PrintSem = true) const;
968};
969
970inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
971 RV.print(OS << " ");
972 return OS;
973}
974
Chris Lattner87a10612004-10-23 04:58:50 +0000975class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +0000976 std::string Name;
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000977 TGLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000978 std::vector<std::string> TemplateArgs;
979 std::vector<RecordVal> Values;
980 std::vector<Record*> SuperClasses;
981public:
982
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000983 explicit Record(const std::string &N, TGLoc loc) : Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000984 ~Record() {}
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000985
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000986 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +0000987 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerbd9b9212009-03-13 16:09:24 +0000988
989 TGLoc getLoc() const { return Loc; }
990
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000991 const std::vector<std::string> &getTemplateArgs() const {
992 return TemplateArgs;
993 }
994 const std::vector<RecordVal> &getValues() const { return Values; }
995 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
996
997 bool isTemplateArg(const std::string &Name) const {
998 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
999 if (TemplateArgs[i] == Name) return true;
1000 return false;
1001 }
1002
1003 const RecordVal *getValue(const std::string &Name) const {
1004 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1005 if (Values[i].getName() == Name) return &Values[i];
1006 return 0;
1007 }
1008 RecordVal *getValue(const std::string &Name) {
1009 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1010 if (Values[i].getName() == Name) return &Values[i];
1011 return 0;
1012 }
1013
1014 void addTemplateArg(const std::string &Name) {
1015 assert(!isTemplateArg(Name) && "Template arg already defined!");
1016 TemplateArgs.push_back(Name);
1017 }
1018
1019 void addValue(const RecordVal &RV) {
1020 assert(getValue(RV.getName()) == 0 && "Value already added!");
1021 Values.push_back(RV);
1022 }
1023
1024 void removeValue(const std::string &Name) {
1025 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1026 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1027 if (Values[i].getName() == Name) {
1028 Values.erase(Values.begin()+i);
1029 return;
1030 }
1031 assert(0 && "Name does not exist in record!");
1032 }
1033
Ted Kremenek58e32872009-03-13 22:20:10 +00001034 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001035 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1036 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001037 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001038 return false;
1039 }
1040
1041 bool isSubClassOf(const std::string &Name) const {
1042 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1043 if (SuperClasses[i]->getName() == Name)
1044 return true;
1045 return false;
1046 }
1047
1048 void addSuperClass(Record *R) {
1049 assert(!isSubClassOf(R) && "Already subclassing record!");
1050 SuperClasses.push_back(R);
1051 }
1052
Chris Lattneref943742005-04-19 03:36:21 +00001053 /// resolveReferences - If there are any field references that refer to fields
1054 /// that have been filled in, we can propagate the values now.
1055 ///
1056 void resolveReferences() { resolveReferencesTo(0); }
1057
1058 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1059 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1060 /// possible references.
1061 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001062
1063 void dump() const;
1064
1065 //===--------------------------------------------------------------------===//
1066 // High-level methods useful to tablegen back-ends
1067 //
1068
1069 /// getValueInit - Return the initializer for a value with the specified name,
1070 /// or throw an exception if the field does not exist.
1071 ///
1072 Init *getValueInit(const std::string &FieldName) const;
1073
1074 /// getValueAsString - This method looks up the specified field and returns
1075 /// its value as a string, throwing an exception if the field does not exist
1076 /// or if the value is not a string.
1077 ///
1078 std::string getValueAsString(const std::string &FieldName) const;
1079
1080 /// getValueAsBitsInit - This method looks up the specified field and returns
1081 /// its value as a BitsInit, throwing an exception if the field does not exist
1082 /// or if the value is not the right type.
1083 ///
1084 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1085
1086 /// getValueAsListInit - This method looks up the specified field and returns
1087 /// its value as a ListInit, throwing an exception if the field does not exist
1088 /// or if the value is not the right type.
1089 ///
1090 ListInit *getValueAsListInit(const std::string &FieldName) const;
1091
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001092 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001093 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001094 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001095 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001096 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001097
Anton Korobeynikova468a112007-11-11 11:19:37 +00001098 /// getValueAsListOfInts - This method looks up the specified field and returns
1099 /// its value as a vector of integers, throwing an exception if the field does
1100 /// not exist or if the value is not the right type.
1101 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001102 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001103
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001104 /// getValueAsDef - This method looks up the specified field and returns its
1105 /// value as a Record, throwing an exception if the field does not exist or if
1106 /// the value is not the right type.
1107 ///
1108 Record *getValueAsDef(const std::string &FieldName) const;
1109
1110 /// getValueAsBit - This method looks up the specified field and returns its
1111 /// value as a bit, throwing an exception if the field does not exist or if
1112 /// the value is not the right type.
1113 ///
1114 bool getValueAsBit(const std::string &FieldName) const;
1115
1116 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001117 /// value as an int64_t, throwing an exception if the field does not exist or
1118 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001119 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001120 int64_t getValueAsInt(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001121
1122 /// getValueAsDag - This method looks up the specified field and returns its
1123 /// value as an Dag, throwing an exception if the field does not exist or if
1124 /// the value is not the right type.
1125 ///
1126 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001127
1128 /// getValueAsCode - This method looks up the specified field and returns
1129 /// its value as the string data in a CodeInit, throwing an exception if the
1130 /// field does not exist or if the value is not a code object.
1131 ///
1132 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001133};
1134
1135std::ostream &operator<<(std::ostream &OS, const Record &R);
1136
David Greenea9c6c5d2009-04-22 20:18:10 +00001137struct MultiClass {
1138 Record Rec; // Placeholder for template args and Name.
1139 typedef std::vector<Record*> RecordVector;
1140 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001141
1142 void dump() const;
1143
David Greenea9c6c5d2009-04-22 20:18:10 +00001144 MultiClass(const std::string &Name, TGLoc Loc) : Rec(Name, Loc) {}
1145};
1146
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001147class RecordKeeper {
1148 std::map<std::string, Record*> Classes, Defs;
1149public:
1150 ~RecordKeeper() {
1151 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001152 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001153 delete I->second;
1154 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001155 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001156 delete I->second;
1157 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001158
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001159 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1160 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1161
1162 Record *getClass(const std::string &Name) const {
1163 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1164 return I == Classes.end() ? 0 : I->second;
1165 }
1166 Record *getDef(const std::string &Name) const {
1167 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1168 return I == Defs.end() ? 0 : I->second;
1169 }
1170 void addClass(Record *R) {
1171 assert(getClass(R->getName()) == 0 && "Class already exists!");
1172 Classes.insert(std::make_pair(R->getName(), R));
1173 }
1174 void addDef(Record *R) {
1175 assert(getDef(R->getName()) == 0 && "Def already exists!");
1176 Defs.insert(std::make_pair(R->getName(), R));
1177 }
1178
Chris Lattnerac284252005-08-19 17:58:11 +00001179 /// removeClass - Remove, but do not delete, the specified record.
1180 ///
1181 void removeClass(const std::string &Name) {
1182 assert(Classes.count(Name) && "Class does not exist!");
1183 Classes.erase(Name);
1184 }
1185 /// removeDef - Remove, but do not delete, the specified record.
1186 ///
1187 void removeDef(const std::string &Name) {
1188 assert(Defs.count(Name) && "Def does not exist!");
1189 Defs.erase(Name);
1190 }
1191
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001192 //===--------------------------------------------------------------------===//
1193 // High-level helper methods, useful for tablegen backends...
1194
1195 /// getAllDerivedDefinitions - This method returns all concrete definitions
1196 /// that derive from the specified class name. If a class with the specified
1197 /// name does not exist, an exception is thrown.
1198 std::vector<Record*>
1199 getAllDerivedDefinitions(const std::string &ClassName) const;
1200
1201
1202 void dump() const;
1203};
1204
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001205/// LessRecord - Sorting predicate to sort record pointers by name.
1206///
1207struct LessRecord {
1208 bool operator()(const Record *Rec1, const Record *Rec2) const {
1209 return Rec1->getName() < Rec2->getName();
1210 }
1211};
1212
Jim Grosbach56938af2008-09-11 17:05:32 +00001213/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1214/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001215///
1216struct LessRecordFieldName {
1217 bool operator()(const Record *Rec1, const Record *Rec2) const {
1218 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1219 }
1220};
1221
Chris Lattnerba42e492009-03-13 16:25:21 +00001222
1223class TGError {
1224 TGLoc Loc;
1225 std::string Message;
1226public:
1227 TGError(TGLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1228
1229 TGLoc getLoc() const { return Loc; }
1230 const std::string &getMessage() const { return Message; }
1231};
1232
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001233
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001234std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1235
1236extern RecordKeeper Records;
1237
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001238void PrintError(TGLoc ErrorLoc, const std::string &Msg);
1239
1240
Brian Gaeke960707c2003-11-11 22:41:34 +00001241} // End llvm namespace
1242
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001243#endif