blob: 59b6348a36e218664d7ee765ca568075b17d9507 [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;
David Greenee8f3b272009-05-14 21:22:49 +000044class UnOpInit;
Chris Lattner51ffbf12006-03-31 21:53:49 +000045class BinOpInit;
David Greene98ed3c72009-05-14 21:54:42 +000046class TernOpInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000047class DefInit;
48class DagInit;
49class TypedInit;
50class VarInit;
51class FieldInit;
52class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000053class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000054
Chris Lattneref943742005-04-19 03:36:21 +000055// Other classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000056class Record;
Chris Lattneref943742005-04-19 03:36:21 +000057class RecordVal;
Bob Wilson56d862542009-04-30 17:35:11 +000058struct MultiClass;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000059
60//===----------------------------------------------------------------------===//
61// Type Classes
62//===----------------------------------------------------------------------===//
63
64struct RecTy {
65 virtual ~RecTy() {}
66
Chris Lattner8b9ecda2007-11-20 22:25:16 +000067 virtual std::string getAsString() const = 0;
Chris Lattner1b1e96b2007-11-22 20:51:34 +000068 void print(std::ostream &OS) const { OS << getAsString(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000069 void dump() const;
70
71 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
72 /// converted to the specified type.
73 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
74
75public: // These methods should only be called from subclasses of Init
76 virtual Init *convertValue( UnsetInit *UI) { return 0; }
77 virtual Init *convertValue( BitInit *BI) { return 0; }
78 virtual Init *convertValue( BitsInit *BI) { return 0; }
79 virtual Init *convertValue( IntInit *II) { return 0; }
80 virtual Init *convertValue(StringInit *SI) { return 0; }
81 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +000082 virtual Init *convertValue( UnOpInit *UI) {
83 return convertValue((TypedInit*)UI);
84 }
David Greene196ac3c2009-04-23 21:25:15 +000085 virtual Init *convertValue( BinOpInit *UI) {
86 return convertValue((TypedInit*)UI);
87 }
David Greene98ed3c72009-05-14 21:54:42 +000088 virtual Init *convertValue( TernOpInit *UI) {
89 return convertValue((TypedInit*)UI);
90 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000091 virtual Init *convertValue( CodeInit *CI) { return 0; }
92 virtual Init *convertValue(VarBitInit *VB) { return 0; }
93 virtual Init *convertValue( DefInit *DI) { return 0; }
94 virtual Init *convertValue( DagInit *DI) { return 0; }
95 virtual Init *convertValue( TypedInit *TI) { return 0; }
96 virtual Init *convertValue( VarInit *VI) {
97 return convertValue((TypedInit*)VI);
98 }
99 virtual Init *convertValue( FieldInit *FI) {
100 return convertValue((TypedInit*)FI);
101 }
102
103public: // These methods should only be called by subclasses of RecTy.
104 // baseClassOf - These virtual methods should be overloaded to return true iff
105 // all values of type 'RHS' can be converted to the 'this' type.
106 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
107 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
108 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
114};
115
116inline std::ostream &operator<<(std::ostream &OS, const RecTy &Ty) {
117 Ty.print(OS);
118 return OS;
119}
120
121
122/// BitRecTy - 'bit' - Represent a single bit
123///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000124class BitRecTy : public RecTy {
125public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000126 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
127 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
128 virtual Init *convertValue( BitsInit *BI);
129 virtual Init *convertValue( IntInit *II);
130 virtual Init *convertValue(StringInit *SI) { return 0; }
131 virtual Init *convertValue( ListInit *LI) { return 0; }
132 virtual Init *convertValue( CodeInit *CI) { return 0; }
133 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
134 virtual Init *convertValue( DefInit *DI) { return 0; }
135 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000136 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000137 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000138 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000139 virtual Init *convertValue( TypedInit *TI);
140 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
141 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000142
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000143 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000144
145 bool typeIsConvertibleTo(const RecTy *RHS) const {
146 return RHS->baseClassOf(this);
147 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000148 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
149 virtual bool baseClassOf(const BitsRecTy *RHS) const;
150 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
151 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
152 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
153 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
156
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000157};
158
159
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000160// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
161/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000162///
163class BitsRecTy : public RecTy {
164 unsigned Size;
165public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000166 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000167
168 unsigned getNumBits() const { return Size; }
169
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000170 virtual Init *convertValue( UnsetInit *UI);
171 virtual Init *convertValue( BitInit *UI);
172 virtual Init *convertValue( BitsInit *BI);
173 virtual Init *convertValue( IntInit *II);
174 virtual Init *convertValue(StringInit *SI) { return 0; }
175 virtual Init *convertValue( ListInit *LI) { return 0; }
176 virtual Init *convertValue( CodeInit *CI) { return 0; }
177 virtual Init *convertValue(VarBitInit *VB) { return 0; }
178 virtual Init *convertValue( DefInit *DI) { return 0; }
179 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000180 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000181 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000182 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000183 virtual Init *convertValue( TypedInit *TI);
184 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
185 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
186
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000187 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000188
189 bool typeIsConvertibleTo(const RecTy *RHS) const {
190 return RHS->baseClassOf(this);
191 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000192 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
193 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000194 return RHS->Size == Size;
195 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000196 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
197 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
198 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
199 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
202
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000203};
204
205
206/// IntRecTy - 'int' - Represent an integer value of no particular size
207///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000208class IntRecTy : public RecTy {
209public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000210 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
211 virtual Init *convertValue( BitInit *BI);
212 virtual Init *convertValue( BitsInit *BI);
213 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
214 virtual Init *convertValue(StringInit *SI) { return 0; }
215 virtual Init *convertValue( ListInit *LI) { return 0; }
216 virtual Init *convertValue( CodeInit *CI) { return 0; }
217 virtual Init *convertValue(VarBitInit *VB) { return 0; }
218 virtual Init *convertValue( DefInit *DI) { return 0; }
219 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000220 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000221 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000222 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000223 virtual Init *convertValue( TypedInit *TI);
224 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
225 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
226
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000227 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000228
229 bool typeIsConvertibleTo(const RecTy *RHS) const {
230 return RHS->baseClassOf(this);
231 }
232
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000233 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
234 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
235 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
237 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
238 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
241
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000242};
243
244/// StringRecTy - 'string' - Represent an string value
245///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000246class StringRecTy : public RecTy {
247public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000248 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
249 virtual Init *convertValue( BitInit *BI) { return 0; }
250 virtual Init *convertValue( BitsInit *BI) { return 0; }
251 virtual Init *convertValue( IntInit *II) { return 0; }
252 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
253 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000254 virtual Init *convertValue( UnOpInit *BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000255 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000256 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
David Greene5d0c0512009-05-14 20:54:48 +0000257
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000258 virtual Init *convertValue( CodeInit *CI) { return 0; }
259 virtual Init *convertValue(VarBitInit *VB) { return 0; }
260 virtual Init *convertValue( DefInit *DI) { return 0; }
261 virtual Init *convertValue( DagInit *DI) { return 0; }
262 virtual Init *convertValue( TypedInit *TI);
263 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
264 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
265
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000266 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000267
268 bool typeIsConvertibleTo(const RecTy *RHS) const {
269 return RHS->baseClassOf(this);
270 }
271
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000272 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
273 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
274 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000275 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000276 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
277 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
278 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000280};
281
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000282// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
283// the specified type.
284/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
285/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000286///
287class ListRecTy : public RecTy {
288 RecTy *Ty;
289public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000290 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000291
292 RecTy *getElementType() const { return Ty; }
293
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000294 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
295 virtual Init *convertValue( BitInit *BI) { return 0; }
296 virtual Init *convertValue( BitsInit *BI) { return 0; }
297 virtual Init *convertValue( IntInit *II) { return 0; }
298 virtual Init *convertValue(StringInit *SI) { return 0; }
299 virtual Init *convertValue( ListInit *LI);
300 virtual Init *convertValue( CodeInit *CI) { return 0; }
301 virtual Init *convertValue(VarBitInit *VB) { return 0; }
302 virtual Init *convertValue( DefInit *DI) { return 0; }
303 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000304 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000305 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000306 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000307 virtual Init *convertValue( TypedInit *TI);
308 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
309 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000310
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000311 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000312
313 bool typeIsConvertibleTo(const RecTy *RHS) const {
314 return RHS->baseClassOf(this);
315 }
316
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000317 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
318 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
319 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000322 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000323 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000324 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
325 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
326 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000327};
328
329/// CodeRecTy - 'code' - Represent an code fragment, function or method.
330///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000331class CodeRecTy : public RecTy {
332public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000333 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
334 virtual Init *convertValue( BitInit *BI) { return 0; }
335 virtual Init *convertValue( BitsInit *BI) { return 0; }
336 virtual Init *convertValue( IntInit *II) { return 0; }
337 virtual Init *convertValue(StringInit *SI) { return 0; }
338 virtual Init *convertValue( ListInit *LI) { return 0; }
339 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
340 virtual Init *convertValue(VarBitInit *VB) { return 0; }
341 virtual Init *convertValue( DefInit *DI) { return 0; }
342 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000343 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000344 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000345 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000346 virtual Init *convertValue( TypedInit *TI);
347 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
348 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
349
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000350 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000351
352 bool typeIsConvertibleTo(const RecTy *RHS) const {
353 return RHS->baseClassOf(this);
354 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000355 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
356 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
357 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
361 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
362 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000363};
364
365/// DagRecTy - 'dag' - Represent a dag fragment
366///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000367class DagRecTy : public RecTy {
368public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000369 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
370 virtual Init *convertValue( BitInit *BI) { return 0; }
371 virtual Init *convertValue( BitsInit *BI) { return 0; }
372 virtual Init *convertValue( IntInit *II) { return 0; }
373 virtual Init *convertValue(StringInit *SI) { return 0; }
374 virtual Init *convertValue( ListInit *LI) { return 0; }
375 virtual Init *convertValue( CodeInit *CI) { return 0; }
376 virtual Init *convertValue(VarBitInit *VB) { return 0; }
377 virtual Init *convertValue( DefInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000378 virtual Init *convertValue( UnOpInit *BO);
Evan Chenga32dee22007-05-15 01:23:24 +0000379 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000380 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000381 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
382 virtual Init *convertValue( TypedInit *TI);
383 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
384 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000385
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000386 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000387
388 bool typeIsConvertibleTo(const RecTy *RHS) const {
389 return RHS->baseClassOf(this);
390 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000391
392 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
393 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
394 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
399 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000400};
401
402
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000403/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000404/// (R32 X = EAX).
405///
406class RecordRecTy : public RecTy {
407 Record *Rec;
408public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000409 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000410
411 Record *getRecord() const { return Rec; }
412
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000413 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
414 virtual Init *convertValue( BitInit *BI) { return 0; }
415 virtual Init *convertValue( BitsInit *BI) { return 0; }
416 virtual Init *convertValue( IntInit *II) { return 0; }
417 virtual Init *convertValue(StringInit *SI) { return 0; }
418 virtual Init *convertValue( ListInit *LI) { return 0; }
419 virtual Init *convertValue( CodeInit *CI) { return 0; }
420 virtual Init *convertValue(VarBitInit *VB) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000421 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000422 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000423 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000424 virtual Init *convertValue( DefInit *DI);
425 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000426 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000427 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
428 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000429
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000430 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000431
432 bool typeIsConvertibleTo(const RecTy *RHS) const {
433 return RHS->baseClassOf(this);
434 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000435 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
436 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
437 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000442 virtual bool baseClassOf(const RecordRecTy *RHS) const;
443};
444
445
446
447//===----------------------------------------------------------------------===//
448// Initializer Classes
449//===----------------------------------------------------------------------===//
450
451struct Init {
452 virtual ~Init() {}
453
454 /// isComplete - This virtual method should be overridden by values that may
455 /// not be completely specified yet.
456 virtual bool isComplete() const { return true; }
457
458 /// print - Print out this value.
Chris Lattner695506c2007-11-22 21:05:25 +0000459 void print(std::ostream &OS) const { OS << getAsString(); }
460
461 /// getAsString - Convert this value to a string form.
462 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000463
464 /// dump - Debugging method that may be called through a debugger, just
465 /// invokes print on cerr.
466 void dump() const;
467
468 /// convertInitializerTo - This virtual function is a simple call-back
469 /// function that should be overridden to call the appropriate
470 /// RecTy::convertValue method.
471 ///
472 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
473
474 /// convertInitializerBitRange - This method is used to implement the bitrange
475 /// selection operator. Given an initializer, it selects the specified bits
476 /// out, returning them as a new init of bits type. If it is not legal to use
477 /// the bit subscript operator on this initializer, return null.
478 ///
479 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
480 return 0;
481 }
482
Chris Lattner8bf9e062004-07-26 23:21:34 +0000483 /// convertInitListSlice - This method is used to implement the list slice
484 /// selection operator. Given an initializer, it selects the specified list
485 /// elements, returning them as a new init of list type. If it is not legal
486 /// to take a slice of this, return null.
487 ///
488 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
489 return 0;
490 }
491
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000492 /// getFieldType - This method is used to implement the FieldInit class.
493 /// Implementors of this method should return the type of the named field if
494 /// they are of record type.
495 ///
496 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
497
498 /// getFieldInit - This method complements getFieldType to return the
499 /// initializer for the specified field. If getFieldType returns non-null
500 /// this method should return non-null, otherwise it returns null.
501 ///
502 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
503 return 0;
504 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000505
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000506 /// resolveReferences - This method is used by classes that refer to other
507 /// variables which may not be defined at the time they expression is formed.
508 /// If a value is set for the variable later, this method will be called on
509 /// users of the value to allow the value to propagate out.
510 ///
Chris Lattneref943742005-04-19 03:36:21 +0000511 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
512 return this;
513 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000514};
515
516inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
517 I.print(OS); return OS;
518}
519
520
521/// UnsetInit - ? - Represents an uninitialized value
522///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000523class UnsetInit : public Init {
524public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000525 virtual Init *convertInitializerTo(RecTy *Ty) {
526 return Ty->convertValue(this);
527 }
528
529 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000530 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000531};
532
533
534/// BitInit - true/false - Represent a concrete initializer for a bit.
535///
536class BitInit : public Init {
537 bool Value;
538public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000539 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000540
541 bool getValue() const { return Value; }
542
543 virtual Init *convertInitializerTo(RecTy *Ty) {
544 return Ty->convertValue(this);
545 }
546
Chris Lattner695506c2007-11-22 21:05:25 +0000547 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000548};
549
550/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
551/// It contains a vector of bits, whose size is determined by the type.
552///
553class BitsInit : public Init {
554 std::vector<Init*> Bits;
555public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000556 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000557
558 unsigned getNumBits() const { return Bits.size(); }
559
560 Init *getBit(unsigned Bit) const {
561 assert(Bit < Bits.size() && "Bit index out of range!");
562 return Bits[Bit];
563 }
564 void setBit(unsigned Bit, Init *V) {
565 assert(Bit < Bits.size() && "Bit index out of range!");
566 assert(Bits[Bit] == 0 && "Bit already set!");
567 Bits[Bit] = V;
568 }
569
570 virtual Init *convertInitializerTo(RecTy *Ty) {
571 return Ty->convertValue(this);
572 }
573 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
574
575 virtual bool isComplete() const {
576 for (unsigned i = 0; i != getNumBits(); ++i)
577 if (!getBit(i)->isComplete()) return false;
578 return true;
579 }
Chris Lattner695506c2007-11-22 21:05:25 +0000580 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000581
Chris Lattneref943742005-04-19 03:36:21 +0000582 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000583
584 // printXX - Print this bitstream with the specified format, returning true if
585 // it is not possible.
586 bool printInHex(std::ostream &OS) const;
587 bool printAsVariable(std::ostream &OS) const;
588 bool printAsUnset(std::ostream &OS) const;
589};
590
591
592/// IntInit - 7 - Represent an initalization by a literal integer value.
593///
594class IntInit : public Init {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000595 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000596public:
Dan Gohmanca0546f2008-10-17 01:33:43 +0000597 explicit IntInit(int64_t V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000598
Dan Gohmanca0546f2008-10-17 01:33:43 +0000599 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000600
601 virtual Init *convertInitializerTo(RecTy *Ty) {
602 return Ty->convertValue(this);
603 }
604 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
605
Chris Lattner695506c2007-11-22 21:05:25 +0000606 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000607};
608
609
610/// StringInit - "foo" - Represent an initialization by a string value.
611///
612class StringInit : public Init {
613 std::string Value;
614public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000615 explicit StringInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000616
617 const std::string &getValue() const { return Value; }
618
619 virtual Init *convertInitializerTo(RecTy *Ty) {
620 return Ty->convertValue(this);
621 }
622
Chris Lattner695506c2007-11-22 21:05:25 +0000623 virtual std::string getAsString() const { return "\"" + Value + "\""; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000624};
625
626/// CodeInit - "[{...}]" - Represent a code fragment.
627///
628class CodeInit : public Init {
629 std::string Value;
630public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000631 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000632
633 const std::string getValue() const { return Value; }
634
635 virtual Init *convertInitializerTo(RecTy *Ty) {
636 return Ty->convertValue(this);
637 }
638
Chris Lattner695506c2007-11-22 21:05:25 +0000639 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000640};
641
642/// ListInit - [AL, AH, CL] - Represent a list of defs
643///
644class ListInit : public Init {
645 std::vector<Init*> Values;
646public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000647 explicit ListInit(std::vector<Init*> &Vs) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000648 Values.swap(Vs);
649 }
650
651 unsigned getSize() const { return Values.size(); }
652 Init *getElement(unsigned i) const {
653 assert(i < Values.size() && "List element index out of range!");
654 return Values[i];
655 }
656
Chris Lattnercbebe462007-02-27 22:08:27 +0000657 Record *getElementAsRecord(unsigned i) const;
658
Chris Lattner8bf9e062004-07-26 23:21:34 +0000659 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
660
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000661 virtual Init *convertInitializerTo(RecTy *Ty) {
662 return Ty->convertValue(this);
663 }
664
Chris Lattner577fc3f2004-07-27 01:01:21 +0000665 /// resolveReferences - This method is used by classes that refer to other
666 /// variables which may not be defined at the time they expression is formed.
667 /// If a value is set for the variable later, this method will be called on
668 /// users of the value to allow the value to propagate out.
669 ///
Chris Lattneref943742005-04-19 03:36:21 +0000670 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000671
Chris Lattner695506c2007-11-22 21:05:25 +0000672 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000673
674 typedef std::vector<Init*>::iterator iterator;
675 typedef std::vector<Init*>::const_iterator const_iterator;
676
677 inline iterator begin() { return Values.begin(); }
678 inline const_iterator begin() const { return Values.begin(); }
679 inline iterator end () { return Values.end(); }
680 inline const_iterator end () const { return Values.end(); }
681
682 inline size_t size () const { return Values.size(); }
683 inline bool empty() const { return Values.empty(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000684};
685
686
687/// TypedInit - This is the common super-class of types that have a specific,
688/// explicit, type.
689///
690class TypedInit : public Init {
691 RecTy *Ty;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000692public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000693 explicit TypedInit(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000694
695 RecTy *getType() const { return Ty; }
696
Chris Lattner577fc3f2004-07-27 01:01:21 +0000697 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
698 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
699
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000700 /// resolveBitReference - This method is used to implement
701 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
Chris Lattner577fc3f2004-07-27 01:01:21 +0000702 /// simply return the resolved value, otherwise we return null.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000703 ///
Chris Lattneref943742005-04-19 03:36:21 +0000704 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
705 unsigned Bit) = 0;
Chris Lattner577fc3f2004-07-27 01:01:21 +0000706
707 /// resolveListElementReference - This method is used to implement
708 /// VarListElementInit::resolveReferences. If the list element is resolvable
709 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000710 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
711 unsigned Elt) = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000712};
713
David Greene5d0c0512009-05-14 20:54:48 +0000714/// OpInit - Base class for operators
David Greene196ac3c2009-04-23 21:25:15 +0000715///
David Greene5d0c0512009-05-14 20:54:48 +0000716class OpInit : public TypedInit {
David Greene196ac3c2009-04-23 21:25:15 +0000717public:
David Greene5d0c0512009-05-14 20:54:48 +0000718 OpInit(RecTy *Type) : TypedInit(Type) {}
719
720 // Clone - Clone this operator, replacing arguments with the new list
721 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
722
723 virtual int getNumOperands(void) const = 0;
724 virtual Init *getOperand(int i) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000725
726 // Fold - If possible, fold this to a simpler init. Return this if not
727 // possible to fold.
David Greene5d0c0512009-05-14 20:54:48 +0000728 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000729
730 virtual Init *convertInitializerTo(RecTy *Ty) {
731 return Ty->convertValue(this);
732 }
733
734 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
735 unsigned Bit);
736 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
737 unsigned Elt);
David Greene5d0c0512009-05-14 20:54:48 +0000738};
739
740
741/// UnOpInit - !op (X) - Transform an init.
742///
David Greenee8f3b272009-05-14 21:22:49 +0000743class UnOpInit : public OpInit {
744public:
745 enum UnaryOp { CAST };
746private:
747 UnaryOp Opc;
748 Init *LHS;
749public:
750 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
751 OpInit(Type), Opc(opc), LHS(lhs) {
752 }
David Greene5d0c0512009-05-14 20:54:48 +0000753
David Greenee8f3b272009-05-14 21:22:49 +0000754 // Clone - Clone this operator, replacing arguments with the new list
755 virtual OpInit *clone(std::vector<Init *> &Operands) {
756 assert(Operands.size() == 1 && "Wrong number of operands for unary operation");
757 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
758 }
David Greene5d0c0512009-05-14 20:54:48 +0000759
David Greenee8f3b272009-05-14 21:22:49 +0000760 int getNumOperands(void) const { return 1; }
761 Init *getOperand(int i) {
762 assert(i == 0 && "Invalid operand id for unary operator");
763 return getOperand();
764 }
David Greene5d0c0512009-05-14 20:54:48 +0000765
David Greenee8f3b272009-05-14 21:22:49 +0000766 UnaryOp getOpcode() const { return Opc; }
767 Init *getOperand() const { return LHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000768
David Greenee8f3b272009-05-14 21:22:49 +0000769 // Fold - If possible, fold this to a simpler init. Return this if not
770 // possible to fold.
771 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000772
David Greenee8f3b272009-05-14 21:22:49 +0000773 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000774
David Greenee8f3b272009-05-14 21:22:49 +0000775 virtual std::string getAsString() const;
776};
David Greene5d0c0512009-05-14 20:54:48 +0000777
778/// BinOpInit - !op (X, Y) - Combine two inits.
779///
780class BinOpInit : public OpInit {
781public:
782 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
783private:
784 BinaryOp Opc;
785 Init *LHS, *RHS;
786public:
787 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
788 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
789 }
790
791 // Clone - Clone this operator, replacing arguments with the new list
792 virtual OpInit *clone(std::vector<Init *> &Operands) {
793 assert(Operands.size() == 2 && "Wrong number of operands for binary operation");
794 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
795 }
796
797 int getNumOperands(void) const { return 2; }
798 Init *getOperand(int i) {
799 assert(i == 0 || i == 1 && "Invalid operand id for binary operator");
800 if (i == 0) {
801 return getLHS();
802 }
803 else {
804 return getRHS();
805 }
806 }
807
808 BinaryOp getOpcode() const { return Opc; }
809 Init *getLHS() const { return LHS; }
810 Init *getRHS() const { return RHS; }
811
812 // Fold - If possible, fold this to a simpler init. Return this if not
813 // possible to fold.
814 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene196ac3c2009-04-23 21:25:15 +0000815
816 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
817
818 virtual std::string getAsString() const;
819};
820
David Greene5d0c0512009-05-14 20:54:48 +0000821/// TernOpInit - !op (X, Y, Z) - Combine two inits.
822///
David Greene98ed3c72009-05-14 21:54:42 +0000823class TernOpInit : public OpInit {
824public:
825 enum TernaryOp { SUBST, FOREACH };
826private:
827 TernaryOp Opc;
828 Init *LHS, *MHS, *RHS;
829public:
830 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
831 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
832 }
David Greene5d0c0512009-05-14 20:54:48 +0000833
David Greene98ed3c72009-05-14 21:54:42 +0000834 // Clone - Clone this operator, replacing arguments with the new list
835 virtual OpInit *clone(std::vector<Init *> &Operands) {
836 assert(Operands.size() == 3 && "Wrong number of operands for ternary operation");
837 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2], getType());
838 }
David Greene5d0c0512009-05-14 20:54:48 +0000839
David Greene98ed3c72009-05-14 21:54:42 +0000840 int getNumOperands(void) const { return 3; }
841 Init *getOperand(int i) {
842 assert(i == 0 || i == 1 || i == 2 && "Invalid operand id for ternary operator");
843 if (i == 0) {
844 return getLHS();
845 }
846 else if (i == 1) {
847 return getMHS();
848 }
849 else {
850 return getRHS();
851 }
852 }
David Greene5d0c0512009-05-14 20:54:48 +0000853
David Greene98ed3c72009-05-14 21:54:42 +0000854 TernaryOp getOpcode() const { return Opc; }
855 Init *getLHS() const { return LHS; }
856 Init *getMHS() const { return MHS; }
857 Init *getRHS() const { return RHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000858
David Greene98ed3c72009-05-14 21:54:42 +0000859 // Fold - If possible, fold this to a simpler init. Return this if not
860 // possible to fold.
861 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000862
David Greene98ed3c72009-05-14 21:54:42 +0000863 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000864
David Greene98ed3c72009-05-14 21:54:42 +0000865 virtual std::string getAsString() const;
866};
David Greene5d0c0512009-05-14 20:54:48 +0000867
David Greene196ac3c2009-04-23 21:25:15 +0000868
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000869/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
870///
871class VarInit : public TypedInit {
872 std::string VarName;
873public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000874 explicit VarInit(const std::string &VN, RecTy *T)
875 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000876
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000877 virtual Init *convertInitializerTo(RecTy *Ty) {
878 return Ty->convertValue(this);
879 }
880
881 const std::string &getName() const { return VarName; }
882
Chris Lattneref943742005-04-19 03:36:21 +0000883 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
884 unsigned Bit);
885 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
886 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000887
888 virtual RecTy *getFieldType(const std::string &FieldName) const;
889 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
890
891 /// resolveReferences - This method is used by classes that refer to other
892 /// variables which may not be defined at the time they expression is formed.
893 /// If a value is set for the variable later, this method will be called on
894 /// users of the value to allow the value to propagate out.
895 ///
Chris Lattneref943742005-04-19 03:36:21 +0000896 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000897
Chris Lattner695506c2007-11-22 21:05:25 +0000898 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000899};
900
901
902/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
903///
904class VarBitInit : public Init {
905 TypedInit *TI;
906 unsigned Bit;
907public:
908 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
909 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
910 ((BitsRecTy*)T->getType())->getNumBits() > B &&
911 "Illegal VarBitInit expression!");
912 }
913
914 virtual Init *convertInitializerTo(RecTy *Ty) {
915 return Ty->convertValue(this);
916 }
917
918 TypedInit *getVariable() const { return TI; }
919 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000920
Chris Lattner695506c2007-11-22 21:05:25 +0000921 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000922 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000923};
924
Misha Brukman650ba8e2005-04-22 00:00:37 +0000925/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000926/// field.
927class VarListElementInit : public TypedInit {
928 TypedInit *TI;
929 unsigned Element;
930public:
931 VarListElementInit(TypedInit *T, unsigned E)
932 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
933 TI(T), Element(E) {
934 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
935 "Illegal VarBitInit expression!");
936 }
937
938 virtual Init *convertInitializerTo(RecTy *Ty) {
939 return Ty->convertValue(this);
940 }
941
942 TypedInit *getVariable() const { return TI; }
943 unsigned getElementNum() const { return Element; }
944
Chris Lattneref943742005-04-19 03:36:21 +0000945 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
946 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000947
948 /// resolveListElementReference - This method is used to implement
949 /// VarListElementInit::resolveReferences. If the list element is resolvable
950 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +0000951 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
952 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000953
Chris Lattner695506c2007-11-22 21:05:25 +0000954 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000955 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000956};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000957
958/// DefInit - AL - Represent a reference to a 'def' in the description
959///
960class DefInit : public Init {
961 Record *Def;
962public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000963 explicit DefInit(Record *D) : Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000964
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000965 virtual Init *convertInitializerTo(RecTy *Ty) {
966 return Ty->convertValue(this);
967 }
968
969 Record *getDef() const { return Def; }
970
971 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
972
973 virtual RecTy *getFieldType(const std::string &FieldName) const;
974 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +0000975
Chris Lattner695506c2007-11-22 21:05:25 +0000976 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000977};
978
979
980/// FieldInit - X.Y - Represent a reference to a subfield of a variable
981///
982class FieldInit : public TypedInit {
983 Init *Rec; // Record we are referring to
984 std::string FieldName; // Field we are accessing
985public:
986 FieldInit(Init *R, const std::string &FN)
987 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
988 assert(getType() && "FieldInit with non-record type!");
989 }
990
991 virtual Init *convertInitializerTo(RecTy *Ty) {
992 return Ty->convertValue(this);
993 }
994
Chris Lattneref943742005-04-19 03:36:21 +0000995 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
996 unsigned Bit);
997 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
998 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000999
Chris Lattneref943742005-04-19 03:36:21 +00001000 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001001
Chris Lattner695506c2007-11-22 21:05:25 +00001002 virtual std::string getAsString() const {
1003 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001004 }
1005};
1006
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001007/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1008/// to have at least one value then a (possibly empty) list of arguments. Each
1009/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001010///
1011class DagInit : public Init {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001012 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +00001013 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001014 std::vector<Init*> Args;
1015 std::vector<std::string> ArgNames;
1016public:
Nate Begemandbe3f772009-03-19 05:21:56 +00001017 DagInit(Init *V, std::string VN,
1018 const std::vector<std::pair<Init*, std::string> > &args)
1019 : Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001020 Args.reserve(args.size());
1021 ArgNames.reserve(args.size());
1022 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1023 Args.push_back(args[i].first);
1024 ArgNames.push_back(args[i].second);
1025 }
1026 }
Nate Begemandbe3f772009-03-19 05:21:56 +00001027 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +00001028 const std::vector<std::string> &argNames)
Nate Begemandbe3f772009-03-19 05:21:56 +00001029 : Val(V), ValName(VN), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +00001030 }
1031
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001032 virtual Init *convertInitializerTo(RecTy *Ty) {
1033 return Ty->convertValue(this);
1034 }
1035
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001036 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001037
Nate Begemandbe3f772009-03-19 05:21:56 +00001038 const std::string &getName() const { return ValName; }
1039
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001040 unsigned getNumArgs() const { return Args.size(); }
1041 Init *getArg(unsigned Num) const {
1042 assert(Num < Args.size() && "Arg number out of range!");
1043 return Args[Num];
1044 }
1045 const std::string &getArgName(unsigned Num) const {
1046 assert(Num < ArgNames.size() && "Arg number out of range!");
1047 return ArgNames[Num];
1048 }
1049
1050 void setArg(unsigned Num, Init *I) {
1051 assert(Num < Args.size() && "Arg number out of range!");
1052 Args[Num] = I;
1053 }
Chris Lattner0d3ef402006-01-31 06:02:35 +00001054
1055 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001056
Chris Lattner695506c2007-11-22 21:05:25 +00001057 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +00001058
1059 typedef std::vector<Init*>::iterator arg_iterator;
1060 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1061 typedef std::vector<std::string>::iterator name_iterator;
1062 typedef std::vector<std::string>::const_iterator const_name_iterator;
1063
1064 inline arg_iterator arg_begin() { return Args.begin(); }
1065 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1066 inline arg_iterator arg_end () { return Args.end(); }
1067 inline const_arg_iterator arg_end () const { return Args.end(); }
1068
1069 inline size_t arg_size () const { return Args.size(); }
1070 inline bool arg_empty() const { return Args.empty(); }
1071
1072 inline name_iterator name_begin() { return ArgNames.begin(); }
1073 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1074 inline name_iterator name_end () { return ArgNames.end(); }
1075 inline const_name_iterator name_end () const { return ArgNames.end(); }
1076
1077 inline size_t name_size () const { return ArgNames.size(); }
1078 inline bool name_empty() const { return ArgNames.empty(); }
1079
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001080};
1081
1082//===----------------------------------------------------------------------===//
1083// High-Level Classes
1084//===----------------------------------------------------------------------===//
1085
1086class RecordVal {
1087 std::string Name;
1088 RecTy *Ty;
1089 unsigned Prefix;
1090 Init *Value;
1091public:
1092 RecordVal(const std::string &N, RecTy *T, unsigned P);
1093
1094 const std::string &getName() const { return Name; }
1095
1096 unsigned getPrefix() const { return Prefix; }
1097 RecTy *getType() const { return Ty; }
1098 Init *getValue() const { return Value; }
1099
1100 bool setValue(Init *V) {
1101 if (V) {
1102 Value = V->convertInitializerTo(Ty);
1103 return Value == 0;
1104 }
1105 Value = 0;
1106 return false;
1107 }
1108
1109 void dump() const;
1110 void print(std::ostream &OS, bool PrintSem = true) const;
1111};
1112
1113inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
1114 RV.print(OS << " ");
1115 return OS;
1116}
1117
Chris Lattner87a10612004-10-23 04:58:50 +00001118class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +00001119 std::string Name;
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001120 TGLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001121 std::vector<std::string> TemplateArgs;
1122 std::vector<RecordVal> Values;
1123 std::vector<Record*> SuperClasses;
1124public:
1125
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001126 explicit Record(const std::string &N, TGLoc loc) : Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001127 ~Record() {}
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001128
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001129 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +00001130 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001131
1132 TGLoc getLoc() const { return Loc; }
1133
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001134 const std::vector<std::string> &getTemplateArgs() const {
1135 return TemplateArgs;
1136 }
1137 const std::vector<RecordVal> &getValues() const { return Values; }
1138 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1139
1140 bool isTemplateArg(const std::string &Name) const {
1141 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1142 if (TemplateArgs[i] == Name) return true;
1143 return false;
1144 }
1145
1146 const RecordVal *getValue(const std::string &Name) const {
1147 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1148 if (Values[i].getName() == Name) return &Values[i];
1149 return 0;
1150 }
1151 RecordVal *getValue(const std::string &Name) {
1152 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1153 if (Values[i].getName() == Name) return &Values[i];
1154 return 0;
1155 }
1156
1157 void addTemplateArg(const std::string &Name) {
1158 assert(!isTemplateArg(Name) && "Template arg already defined!");
1159 TemplateArgs.push_back(Name);
1160 }
1161
1162 void addValue(const RecordVal &RV) {
1163 assert(getValue(RV.getName()) == 0 && "Value already added!");
1164 Values.push_back(RV);
1165 }
1166
1167 void removeValue(const std::string &Name) {
1168 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1169 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1170 if (Values[i].getName() == Name) {
1171 Values.erase(Values.begin()+i);
1172 return;
1173 }
1174 assert(0 && "Name does not exist in record!");
1175 }
1176
Ted Kremenek58e32872009-03-13 22:20:10 +00001177 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001178 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1179 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001180 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001181 return false;
1182 }
1183
1184 bool isSubClassOf(const std::string &Name) const {
1185 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1186 if (SuperClasses[i]->getName() == Name)
1187 return true;
1188 return false;
1189 }
1190
1191 void addSuperClass(Record *R) {
1192 assert(!isSubClassOf(R) && "Already subclassing record!");
1193 SuperClasses.push_back(R);
1194 }
1195
Chris Lattneref943742005-04-19 03:36:21 +00001196 /// resolveReferences - If there are any field references that refer to fields
1197 /// that have been filled in, we can propagate the values now.
1198 ///
1199 void resolveReferences() { resolveReferencesTo(0); }
1200
1201 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1202 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1203 /// possible references.
1204 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001205
1206 void dump() const;
1207
1208 //===--------------------------------------------------------------------===//
1209 // High-level methods useful to tablegen back-ends
1210 //
1211
1212 /// getValueInit - Return the initializer for a value with the specified name,
1213 /// or throw an exception if the field does not exist.
1214 ///
1215 Init *getValueInit(const std::string &FieldName) const;
1216
1217 /// getValueAsString - This method looks up the specified field and returns
1218 /// its value as a string, throwing an exception if the field does not exist
1219 /// or if the value is not a string.
1220 ///
1221 std::string getValueAsString(const std::string &FieldName) const;
1222
1223 /// getValueAsBitsInit - This method looks up the specified field and returns
1224 /// its value as a BitsInit, throwing an exception if the field does not exist
1225 /// or if the value is not the right type.
1226 ///
1227 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1228
1229 /// getValueAsListInit - This method looks up the specified field and returns
1230 /// its value as a ListInit, throwing an exception if the field does not exist
1231 /// or if the value is not the right type.
1232 ///
1233 ListInit *getValueAsListInit(const std::string &FieldName) const;
1234
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001235 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001236 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001237 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001238 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001239 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001240
Anton Korobeynikova468a112007-11-11 11:19:37 +00001241 /// getValueAsListOfInts - This method looks up the specified field and returns
1242 /// its value as a vector of integers, throwing an exception if the field does
1243 /// not exist or if the value is not the right type.
1244 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001245 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001246
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001247 /// getValueAsDef - This method looks up the specified field and returns its
1248 /// value as a Record, throwing an exception if the field does not exist or if
1249 /// the value is not the right type.
1250 ///
1251 Record *getValueAsDef(const std::string &FieldName) const;
1252
1253 /// getValueAsBit - This method looks up the specified field and returns its
1254 /// value as a bit, throwing an exception if the field does not exist or if
1255 /// the value is not the right type.
1256 ///
1257 bool getValueAsBit(const std::string &FieldName) const;
1258
1259 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001260 /// value as an int64_t, throwing an exception if the field does not exist or
1261 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001262 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001263 int64_t getValueAsInt(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001264
1265 /// getValueAsDag - This method looks up the specified field and returns its
1266 /// value as an Dag, throwing an exception if the field does not exist or if
1267 /// the value is not the right type.
1268 ///
1269 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001270
1271 /// getValueAsCode - This method looks up the specified field and returns
1272 /// its value as the string data in a CodeInit, throwing an exception if the
1273 /// field does not exist or if the value is not a code object.
1274 ///
1275 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001276};
1277
1278std::ostream &operator<<(std::ostream &OS, const Record &R);
1279
David Greenea9c6c5d2009-04-22 20:18:10 +00001280struct MultiClass {
1281 Record Rec; // Placeholder for template args and Name.
1282 typedef std::vector<Record*> RecordVector;
1283 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001284
1285 void dump() const;
1286
David Greenea9c6c5d2009-04-22 20:18:10 +00001287 MultiClass(const std::string &Name, TGLoc Loc) : Rec(Name, Loc) {}
1288};
1289
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001290class RecordKeeper {
1291 std::map<std::string, Record*> Classes, Defs;
1292public:
1293 ~RecordKeeper() {
1294 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001295 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001296 delete I->second;
1297 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001298 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001299 delete I->second;
1300 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001301
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001302 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1303 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1304
1305 Record *getClass(const std::string &Name) const {
1306 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1307 return I == Classes.end() ? 0 : I->second;
1308 }
1309 Record *getDef(const std::string &Name) const {
1310 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1311 return I == Defs.end() ? 0 : I->second;
1312 }
1313 void addClass(Record *R) {
1314 assert(getClass(R->getName()) == 0 && "Class already exists!");
1315 Classes.insert(std::make_pair(R->getName(), R));
1316 }
1317 void addDef(Record *R) {
1318 assert(getDef(R->getName()) == 0 && "Def already exists!");
1319 Defs.insert(std::make_pair(R->getName(), R));
1320 }
1321
Chris Lattnerac284252005-08-19 17:58:11 +00001322 /// removeClass - Remove, but do not delete, the specified record.
1323 ///
1324 void removeClass(const std::string &Name) {
1325 assert(Classes.count(Name) && "Class does not exist!");
1326 Classes.erase(Name);
1327 }
1328 /// removeDef - Remove, but do not delete, the specified record.
1329 ///
1330 void removeDef(const std::string &Name) {
1331 assert(Defs.count(Name) && "Def does not exist!");
1332 Defs.erase(Name);
1333 }
1334
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001335 //===--------------------------------------------------------------------===//
1336 // High-level helper methods, useful for tablegen backends...
1337
1338 /// getAllDerivedDefinitions - This method returns all concrete definitions
1339 /// that derive from the specified class name. If a class with the specified
1340 /// name does not exist, an exception is thrown.
1341 std::vector<Record*>
1342 getAllDerivedDefinitions(const std::string &ClassName) const;
1343
1344
1345 void dump() const;
1346};
1347
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001348/// LessRecord - Sorting predicate to sort record pointers by name.
1349///
1350struct LessRecord {
1351 bool operator()(const Record *Rec1, const Record *Rec2) const {
1352 return Rec1->getName() < Rec2->getName();
1353 }
1354};
1355
Jim Grosbach56938af2008-09-11 17:05:32 +00001356/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1357/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001358///
1359struct LessRecordFieldName {
1360 bool operator()(const Record *Rec1, const Record *Rec2) const {
1361 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1362 }
1363};
1364
Chris Lattnerba42e492009-03-13 16:25:21 +00001365
1366class TGError {
1367 TGLoc Loc;
1368 std::string Message;
1369public:
1370 TGError(TGLoc loc, const std::string &message) : Loc(loc), Message(message) {}
1371
1372 TGLoc getLoc() const { return Loc; }
1373 const std::string &getMessage() const { return Message; }
1374};
1375
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001376
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001377std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1378
1379extern RecordKeeper Records;
1380
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001381void PrintError(TGLoc ErrorLoc, const std::string &Msg);
1382
1383
Brian Gaeke960707c2003-11-11 22:41:34 +00001384} // End llvm namespace
1385
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001386#endif