blob: 2254dd84e08984a8ec267a9557ecc5883d02681c [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 Lattner1b30e1ac2009-06-21 03:36:54 +000018#include "llvm/Support/SourceMgr.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
David Greene8618f952009-06-08 20:23:18 +0000445/// resolveTypes - Find a common type that T1 and T2 convert to.
446/// Return 0 if no such type exists.
447///
448RecTy *resolveTypes(RecTy *T1, RecTy *T2);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000449
450//===----------------------------------------------------------------------===//
451// Initializer Classes
452//===----------------------------------------------------------------------===//
453
454struct Init {
455 virtual ~Init() {}
456
457 /// isComplete - This virtual method should be overridden by values that may
458 /// not be completely specified yet.
459 virtual bool isComplete() const { return true; }
460
461 /// print - Print out this value.
Chris Lattner695506c2007-11-22 21:05:25 +0000462 void print(std::ostream &OS) const { OS << getAsString(); }
463
464 /// getAsString - Convert this value to a string form.
465 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000466
467 /// dump - Debugging method that may be called through a debugger, just
468 /// invokes print on cerr.
469 void dump() const;
470
471 /// convertInitializerTo - This virtual function is a simple call-back
472 /// function that should be overridden to call the appropriate
473 /// RecTy::convertValue method.
474 ///
475 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
476
477 /// convertInitializerBitRange - This method is used to implement the bitrange
478 /// selection operator. Given an initializer, it selects the specified bits
479 /// out, returning them as a new init of bits type. If it is not legal to use
480 /// the bit subscript operator on this initializer, return null.
481 ///
482 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
483 return 0;
484 }
485
Chris Lattner8bf9e062004-07-26 23:21:34 +0000486 /// convertInitListSlice - This method is used to implement the list slice
487 /// selection operator. Given an initializer, it selects the specified list
488 /// elements, returning them as a new init of list type. If it is not legal
489 /// to take a slice of this, return null.
490 ///
491 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
492 return 0;
493 }
494
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000495 /// getFieldType - This method is used to implement the FieldInit class.
496 /// Implementors of this method should return the type of the named field if
497 /// they are of record type.
498 ///
499 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
500
501 /// getFieldInit - This method complements getFieldType to return the
502 /// initializer for the specified field. If getFieldType returns non-null
503 /// this method should return non-null, otherwise it returns null.
504 ///
505 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
506 return 0;
507 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000508
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000509 /// resolveReferences - This method is used by classes that refer to other
510 /// variables which may not be defined at the time they expression is formed.
511 /// If a value is set for the variable later, this method will be called on
512 /// users of the value to allow the value to propagate out.
513 ///
Chris Lattneref943742005-04-19 03:36:21 +0000514 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
515 return this;
516 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000517};
518
519inline std::ostream &operator<<(std::ostream &OS, const Init &I) {
520 I.print(OS); return OS;
521}
522
David Greenee917fff2009-05-14 22:23:47 +0000523/// TypedInit - This is the common super-class of types that have a specific,
524/// explicit, type.
525///
526class TypedInit : public Init {
527 RecTy *Ty;
528public:
529 explicit TypedInit(RecTy *T) : Ty(T) {}
530
531 RecTy *getType() const { return Ty; }
532
533 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
534 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
535
536 /// resolveBitReference - This method is used to implement
537 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
538 /// simply return the resolved value, otherwise we return null.
539 ///
540 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
541 unsigned Bit) = 0;
542
543 /// resolveListElementReference - This method is used to implement
544 /// VarListElementInit::resolveReferences. If the list element is resolvable
545 /// now, we return the resolved value, otherwise we return null.
546 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
547 unsigned Elt) = 0;
548};
549
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000550
551/// UnsetInit - ? - Represents an uninitialized value
552///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000553class UnsetInit : public Init {
554public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000555 virtual Init *convertInitializerTo(RecTy *Ty) {
556 return Ty->convertValue(this);
557 }
558
559 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000560 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000561};
562
563
564/// BitInit - true/false - Represent a concrete initializer for a bit.
565///
566class BitInit : public Init {
567 bool Value;
568public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000569 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000570
571 bool getValue() const { return Value; }
572
573 virtual Init *convertInitializerTo(RecTy *Ty) {
574 return Ty->convertValue(this);
575 }
576
Chris Lattner695506c2007-11-22 21:05:25 +0000577 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000578};
579
580/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
581/// It contains a vector of bits, whose size is determined by the type.
582///
583class BitsInit : public Init {
584 std::vector<Init*> Bits;
585public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000586 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000587
588 unsigned getNumBits() const { return Bits.size(); }
589
590 Init *getBit(unsigned Bit) const {
591 assert(Bit < Bits.size() && "Bit index out of range!");
592 return Bits[Bit];
593 }
594 void setBit(unsigned Bit, Init *V) {
595 assert(Bit < Bits.size() && "Bit index out of range!");
596 assert(Bits[Bit] == 0 && "Bit already set!");
597 Bits[Bit] = V;
598 }
599
600 virtual Init *convertInitializerTo(RecTy *Ty) {
601 return Ty->convertValue(this);
602 }
603 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
604
605 virtual bool isComplete() const {
606 for (unsigned i = 0; i != getNumBits(); ++i)
607 if (!getBit(i)->isComplete()) return false;
608 return true;
609 }
Chris Lattner695506c2007-11-22 21:05:25 +0000610 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000611
Chris Lattneref943742005-04-19 03:36:21 +0000612 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000613
614 // printXX - Print this bitstream with the specified format, returning true if
615 // it is not possible.
616 bool printInHex(std::ostream &OS) const;
617 bool printAsVariable(std::ostream &OS) const;
618 bool printAsUnset(std::ostream &OS) const;
619};
620
621
622/// IntInit - 7 - Represent an initalization by a literal integer value.
623///
David Greene8618f952009-06-08 20:23:18 +0000624class IntInit : public TypedInit {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000625 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000626public:
David Greene8618f952009-06-08 20:23:18 +0000627 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000628
Dan Gohmanca0546f2008-10-17 01:33:43 +0000629 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000630
631 virtual Init *convertInitializerTo(RecTy *Ty) {
632 return Ty->convertValue(this);
633 }
634 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
635
Chris Lattner695506c2007-11-22 21:05:25 +0000636 virtual std::string getAsString() const;
David Greene8618f952009-06-08 20:23:18 +0000637
638 /// resolveBitReference - This method is used to implement
639 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
640 /// simply return the resolved value, otherwise we return null.
641 ///
642 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
643 unsigned Bit) {
644 assert(0 && "Illegal bit reference off int");
645 return 0;
646 }
647
648 /// resolveListElementReference - This method is used to implement
649 /// VarListElementInit::resolveReferences. If the list element is resolvable
650 /// now, we return the resolved value, otherwise we return null.
651 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
652 unsigned Elt) {
653 assert(0 && "Illegal element reference off int");
654 return 0;
655 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000656};
657
658
659/// StringInit - "foo" - Represent an initialization by a string value.
660///
David Greenee917fff2009-05-14 22:23:47 +0000661class StringInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000662 std::string Value;
663public:
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000664 explicit StringInit(const std::string &V)
665 : TypedInit(new StringRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000666
667 const std::string &getValue() const { return Value; }
668
669 virtual Init *convertInitializerTo(RecTy *Ty) {
670 return Ty->convertValue(this);
671 }
672
Chris Lattner695506c2007-11-22 21:05:25 +0000673 virtual std::string getAsString() const { return "\"" + Value + "\""; }
David Greenee917fff2009-05-14 22:23:47 +0000674
675 /// resolveBitReference - This method is used to implement
676 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
677 /// simply return the resolved value, otherwise we return null.
678 ///
679 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
680 unsigned Bit) {
681 assert(0 && "Illegal bit reference off string");
682 return 0;
683 }
684
685 /// resolveListElementReference - This method is used to implement
686 /// VarListElementInit::resolveReferences. If the list element is resolvable
687 /// now, we return the resolved value, otherwise we return null.
688 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
689 unsigned Elt) {
690 assert(0 && "Illegal element reference off string");
691 return 0;
692 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000693};
694
695/// CodeInit - "[{...}]" - Represent a code fragment.
696///
697class CodeInit : public Init {
698 std::string Value;
699public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000700 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000701
702 const std::string getValue() const { return Value; }
703
704 virtual Init *convertInitializerTo(RecTy *Ty) {
705 return Ty->convertValue(this);
706 }
707
Chris Lattner695506c2007-11-22 21:05:25 +0000708 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000709};
710
711/// ListInit - [AL, AH, CL] - Represent a list of defs
712///
David Greene8618f952009-06-08 20:23:18 +0000713class ListInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000714 std::vector<Init*> Values;
715public:
David Greened571b3c2009-05-14 22:38:31 +0000716 typedef std::vector<Init*>::iterator iterator;
717 typedef std::vector<Init*>::const_iterator const_iterator;
718
David Greene8618f952009-06-08 20:23:18 +0000719 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
720 : TypedInit(new ListRecTy(EltTy)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000721 Values.swap(Vs);
722 }
David Greene8618f952009-06-08 20:23:18 +0000723 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
724 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000725
726 unsigned getSize() const { return Values.size(); }
727 Init *getElement(unsigned i) const {
728 assert(i < Values.size() && "List element index out of range!");
729 return Values[i];
730 }
731
Chris Lattnercbebe462007-02-27 22:08:27 +0000732 Record *getElementAsRecord(unsigned i) const;
733
Chris Lattner8bf9e062004-07-26 23:21:34 +0000734 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
735
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000736 virtual Init *convertInitializerTo(RecTy *Ty) {
737 return Ty->convertValue(this);
738 }
739
Chris Lattner577fc3f2004-07-27 01:01:21 +0000740 /// resolveReferences - This method is used by classes that refer to other
741 /// variables which may not be defined at the time they expression is formed.
742 /// If a value is set for the variable later, this method will be called on
743 /// users of the value to allow the value to propagate out.
744 ///
Chris Lattneref943742005-04-19 03:36:21 +0000745 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000746
Chris Lattner695506c2007-11-22 21:05:25 +0000747 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000748
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000749 inline iterator begin() { return Values.begin(); }
750 inline const_iterator begin() const { return Values.begin(); }
751 inline iterator end () { return Values.end(); }
752 inline const_iterator end () const { return Values.end(); }
753
754 inline size_t size () const { return Values.size(); }
755 inline bool empty() const { return Values.empty(); }
David Greene8618f952009-06-08 20:23:18 +0000756
757 /// resolveBitReference - This method is used to implement
758 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
759 /// simply return the resolved value, otherwise we return null.
760 ///
761 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
762 unsigned Bit) {
763 assert(0 && "Illegal bit reference off list");
764 return 0;
765 }
766
767 /// resolveListElementReference - This method is used to implement
768 /// VarListElementInit::resolveReferences. If the list element is resolvable
769 /// now, we return the resolved value, otherwise we return null.
770 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
771 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000772};
773
774
David Greene5d0c0512009-05-14 20:54:48 +0000775/// OpInit - Base class for operators
David Greene196ac3c2009-04-23 21:25:15 +0000776///
David Greene5d0c0512009-05-14 20:54:48 +0000777class OpInit : public TypedInit {
David Greene196ac3c2009-04-23 21:25:15 +0000778public:
David Greene5d0c0512009-05-14 20:54:48 +0000779 OpInit(RecTy *Type) : TypedInit(Type) {}
780
781 // Clone - Clone this operator, replacing arguments with the new list
782 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
783
784 virtual int getNumOperands(void) const = 0;
785 virtual Init *getOperand(int i) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000786
787 // Fold - If possible, fold this to a simpler init. Return this if not
788 // possible to fold.
David Greene5d0c0512009-05-14 20:54:48 +0000789 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000790
791 virtual Init *convertInitializerTo(RecTy *Ty) {
792 return Ty->convertValue(this);
793 }
794
795 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
796 unsigned Bit);
797 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
798 unsigned Elt);
David Greene5d0c0512009-05-14 20:54:48 +0000799};
800
801
802/// UnOpInit - !op (X) - Transform an init.
803///
David Greenee8f3b272009-05-14 21:22:49 +0000804class UnOpInit : public OpInit {
805public:
David Greened571b3c2009-05-14 22:38:31 +0000806 enum UnaryOp { CAST, CAR, CDR, LNULL };
David Greenee8f3b272009-05-14 21:22:49 +0000807private:
808 UnaryOp Opc;
809 Init *LHS;
810public:
811 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
812 OpInit(Type), Opc(opc), LHS(lhs) {
813 }
David Greene5d0c0512009-05-14 20:54:48 +0000814
David Greenee8f3b272009-05-14 21:22:49 +0000815 // Clone - Clone this operator, replacing arguments with the new list
816 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000817 assert(Operands.size() == 1 &&
818 "Wrong number of operands for unary operation");
David Greenee8f3b272009-05-14 21:22:49 +0000819 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
820 }
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greenee8f3b272009-05-14 21:22:49 +0000822 int getNumOperands(void) const { return 1; }
823 Init *getOperand(int i) {
824 assert(i == 0 && "Invalid operand id for unary operator");
825 return getOperand();
826 }
David Greene5d0c0512009-05-14 20:54:48 +0000827
David Greenee8f3b272009-05-14 21:22:49 +0000828 UnaryOp getOpcode() const { return Opc; }
829 Init *getOperand() const { return LHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000830
David Greenee8f3b272009-05-14 21:22:49 +0000831 // Fold - If possible, fold this to a simpler init. Return this if not
832 // possible to fold.
833 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000834
David Greenee8f3b272009-05-14 21:22:49 +0000835 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000836
David Greeneefa19612009-06-29 20:05:29 +0000837 /// getFieldType - This method is used to implement the FieldInit class.
838 /// Implementors of this method should return the type of the named field if
839 /// they are of record type.
840 ///
841 virtual RecTy *getFieldType(const std::string &FieldName) const;
842
David Greenee8f3b272009-05-14 21:22:49 +0000843 virtual std::string getAsString() const;
844};
David Greene5d0c0512009-05-14 20:54:48 +0000845
846/// BinOpInit - !op (X, Y) - Combine two inits.
847///
848class BinOpInit : public OpInit {
849public:
David Greene58a6b762009-06-09 18:31:17 +0000850 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
David Greene5d0c0512009-05-14 20:54:48 +0000851private:
852 BinaryOp Opc;
853 Init *LHS, *RHS;
854public:
855 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
856 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
857 }
858
859 // Clone - Clone this operator, replacing arguments with the new list
860 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000861 assert(Operands.size() == 2 &&
862 "Wrong number of operands for binary operation");
David Greene5d0c0512009-05-14 20:54:48 +0000863 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
864 }
865
866 int getNumOperands(void) const { return 2; }
867 Init *getOperand(int i) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000868 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
David Greene5d0c0512009-05-14 20:54:48 +0000869 if (i == 0) {
870 return getLHS();
871 }
872 else {
873 return getRHS();
874 }
875 }
876
877 BinaryOp getOpcode() const { return Opc; }
878 Init *getLHS() const { return LHS; }
879 Init *getRHS() const { return RHS; }
880
881 // Fold - If possible, fold this to a simpler init. Return this if not
882 // possible to fold.
883 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene196ac3c2009-04-23 21:25:15 +0000884
885 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
886
887 virtual std::string getAsString() const;
888};
889
David Greene5d0c0512009-05-14 20:54:48 +0000890/// TernOpInit - !op (X, Y, Z) - Combine two inits.
891///
David Greene98ed3c72009-05-14 21:54:42 +0000892class TernOpInit : public OpInit {
893public:
David Greene58a6b762009-06-09 18:31:17 +0000894 enum TernaryOp { SUBST, FOREACH, IF };
David Greene98ed3c72009-05-14 21:54:42 +0000895private:
896 TernaryOp Opc;
897 Init *LHS, *MHS, *RHS;
898public:
899 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
900 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
901 }
David Greene5d0c0512009-05-14 20:54:48 +0000902
David Greene98ed3c72009-05-14 21:54:42 +0000903 // Clone - Clone this operator, replacing arguments with the new list
904 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000905 assert(Operands.size() == 3 &&
906 "Wrong number of operands for ternary operation");
907 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2],
908 getType());
David Greene98ed3c72009-05-14 21:54:42 +0000909 }
David Greene5d0c0512009-05-14 20:54:48 +0000910
David Greene98ed3c72009-05-14 21:54:42 +0000911 int getNumOperands(void) const { return 3; }
912 Init *getOperand(int i) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000913 assert((i == 0 || i == 1 || i == 2) &&
914 "Invalid operand id for ternary operator");
David Greene98ed3c72009-05-14 21:54:42 +0000915 if (i == 0) {
916 return getLHS();
917 }
918 else if (i == 1) {
919 return getMHS();
920 }
921 else {
922 return getRHS();
923 }
924 }
David Greene5d0c0512009-05-14 20:54:48 +0000925
David Greene98ed3c72009-05-14 21:54:42 +0000926 TernaryOp getOpcode() const { return Opc; }
927 Init *getLHS() const { return LHS; }
928 Init *getMHS() const { return MHS; }
929 Init *getRHS() const { return RHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000930
David Greene98ed3c72009-05-14 21:54:42 +0000931 // Fold - If possible, fold this to a simpler init. Return this if not
932 // possible to fold.
933 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000934
David Greene98ed3c72009-05-14 21:54:42 +0000935 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000936
David Greene98ed3c72009-05-14 21:54:42 +0000937 virtual std::string getAsString() const;
938};
David Greene5d0c0512009-05-14 20:54:48 +0000939
David Greene196ac3c2009-04-23 21:25:15 +0000940
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000941/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
942///
943class VarInit : public TypedInit {
944 std::string VarName;
945public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000946 explicit VarInit(const std::string &VN, RecTy *T)
947 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000948
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000949 virtual Init *convertInitializerTo(RecTy *Ty) {
950 return Ty->convertValue(this);
951 }
952
953 const std::string &getName() const { return VarName; }
954
Chris Lattneref943742005-04-19 03:36:21 +0000955 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
956 unsigned Bit);
957 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
958 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000959
960 virtual RecTy *getFieldType(const std::string &FieldName) const;
961 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
962
963 /// resolveReferences - This method is used by classes that refer to other
964 /// variables which may not be defined at the time they expression is formed.
965 /// If a value is set for the variable later, this method will be called on
966 /// users of the value to allow the value to propagate out.
967 ///
Chris Lattneref943742005-04-19 03:36:21 +0000968 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000969
Chris Lattner695506c2007-11-22 21:05:25 +0000970 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000971};
972
973
974/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
975///
976class VarBitInit : public Init {
977 TypedInit *TI;
978 unsigned Bit;
979public:
980 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
981 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
982 ((BitsRecTy*)T->getType())->getNumBits() > B &&
983 "Illegal VarBitInit expression!");
984 }
985
986 virtual Init *convertInitializerTo(RecTy *Ty) {
987 return Ty->convertValue(this);
988 }
989
990 TypedInit *getVariable() const { return TI; }
991 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000992
Chris Lattner695506c2007-11-22 21:05:25 +0000993 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000994 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000995};
996
Misha Brukman650ba8e2005-04-22 00:00:37 +0000997/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000998/// field.
999class VarListElementInit : public TypedInit {
1000 TypedInit *TI;
1001 unsigned Element;
1002public:
1003 VarListElementInit(TypedInit *T, unsigned E)
1004 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1005 TI(T), Element(E) {
1006 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1007 "Illegal VarBitInit expression!");
1008 }
1009
1010 virtual Init *convertInitializerTo(RecTy *Ty) {
1011 return Ty->convertValue(this);
1012 }
1013
1014 TypedInit *getVariable() const { return TI; }
1015 unsigned getElementNum() const { return Element; }
1016
Chris Lattneref943742005-04-19 03:36:21 +00001017 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1018 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001019
1020 /// resolveListElementReference - This method is used to implement
1021 /// VarListElementInit::resolveReferences. If the list element is resolvable
1022 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +00001023 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1024 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001025
Chris Lattner695506c2007-11-22 21:05:25 +00001026 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +00001027 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001028};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001029
1030/// DefInit - AL - Represent a reference to a 'def' in the description
1031///
David Greenee917fff2009-05-14 22:23:47 +00001032class DefInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001033 Record *Def;
1034public:
David Greenee917fff2009-05-14 22:23:47 +00001035 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001036
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001037 virtual Init *convertInitializerTo(RecTy *Ty) {
1038 return Ty->convertValue(this);
1039 }
1040
1041 Record *getDef() const { return Def; }
1042
1043 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1044
1045 virtual RecTy *getFieldType(const std::string &FieldName) const;
1046 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001047
Chris Lattner695506c2007-11-22 21:05:25 +00001048 virtual std::string getAsString() const;
David Greenee917fff2009-05-14 22:23:47 +00001049
1050 /// resolveBitReference - This method is used to implement
1051 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1052 /// simply return the resolved value, otherwise we return null.
1053 ///
1054 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1055 unsigned Bit) {
1056 assert(0 && "Illegal bit reference off def");
1057 return 0;
1058 }
1059
1060 /// resolveListElementReference - This method is used to implement
1061 /// VarListElementInit::resolveReferences. If the list element is resolvable
1062 /// now, we return the resolved value, otherwise we return null.
1063 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1064 unsigned Elt) {
1065 assert(0 && "Illegal element reference off def");
1066 return 0;
1067 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001068};
1069
1070
1071/// FieldInit - X.Y - Represent a reference to a subfield of a variable
1072///
1073class FieldInit : public TypedInit {
1074 Init *Rec; // Record we are referring to
1075 std::string FieldName; // Field we are accessing
1076public:
1077 FieldInit(Init *R, const std::string &FN)
1078 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1079 assert(getType() && "FieldInit with non-record type!");
1080 }
1081
1082 virtual Init *convertInitializerTo(RecTy *Ty) {
1083 return Ty->convertValue(this);
1084 }
1085
Chris Lattneref943742005-04-19 03:36:21 +00001086 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1087 unsigned Bit);
1088 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1089 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001090
Chris Lattneref943742005-04-19 03:36:21 +00001091 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001092
Chris Lattner695506c2007-11-22 21:05:25 +00001093 virtual std::string getAsString() const {
1094 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001095 }
1096};
1097
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001098/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1099/// to have at least one value then a (possibly empty) list of arguments. Each
1100/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001101///
David Greenee917fff2009-05-14 22:23:47 +00001102class DagInit : public TypedInit {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001103 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +00001104 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001105 std::vector<Init*> Args;
1106 std::vector<std::string> ArgNames;
1107public:
Nate Begemandbe3f772009-03-19 05:21:56 +00001108 DagInit(Init *V, std::string VN,
1109 const std::vector<std::pair<Init*, std::string> > &args)
David Greenee917fff2009-05-14 22:23:47 +00001110 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001111 Args.reserve(args.size());
1112 ArgNames.reserve(args.size());
1113 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1114 Args.push_back(args[i].first);
1115 ArgNames.push_back(args[i].second);
1116 }
1117 }
Nate Begemandbe3f772009-03-19 05:21:56 +00001118 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +00001119 const std::vector<std::string> &argNames)
David Greenee917fff2009-05-14 22:23:47 +00001120 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +00001121 }
1122
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001123 virtual Init *convertInitializerTo(RecTy *Ty) {
1124 return Ty->convertValue(this);
1125 }
1126
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001127 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001128
Nate Begemandbe3f772009-03-19 05:21:56 +00001129 const std::string &getName() const { return ValName; }
1130
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001131 unsigned getNumArgs() const { return Args.size(); }
1132 Init *getArg(unsigned Num) const {
1133 assert(Num < Args.size() && "Arg number out of range!");
1134 return Args[Num];
1135 }
1136 const std::string &getArgName(unsigned Num) const {
1137 assert(Num < ArgNames.size() && "Arg number out of range!");
1138 return ArgNames[Num];
1139 }
1140
1141 void setArg(unsigned Num, Init *I) {
1142 assert(Num < Args.size() && "Arg number out of range!");
1143 Args[Num] = I;
1144 }
Chris Lattner0d3ef402006-01-31 06:02:35 +00001145
1146 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001147
Chris Lattner695506c2007-11-22 21:05:25 +00001148 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +00001149
1150 typedef std::vector<Init*>::iterator arg_iterator;
1151 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1152 typedef std::vector<std::string>::iterator name_iterator;
1153 typedef std::vector<std::string>::const_iterator const_name_iterator;
1154
1155 inline arg_iterator arg_begin() { return Args.begin(); }
1156 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1157 inline arg_iterator arg_end () { return Args.end(); }
1158 inline const_arg_iterator arg_end () const { return Args.end(); }
1159
1160 inline size_t arg_size () const { return Args.size(); }
1161 inline bool arg_empty() const { return Args.empty(); }
1162
1163 inline name_iterator name_begin() { return ArgNames.begin(); }
1164 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1165 inline name_iterator name_end () { return ArgNames.end(); }
1166 inline const_name_iterator name_end () const { return ArgNames.end(); }
1167
1168 inline size_t name_size () const { return ArgNames.size(); }
1169 inline bool name_empty() const { return ArgNames.empty(); }
1170
David Greenee917fff2009-05-14 22:23:47 +00001171 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1172 unsigned Bit) {
1173 assert(0 && "Illegal bit reference off dag");
1174 return 0;
1175 }
1176
1177 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1178 unsigned Elt) {
1179 assert(0 && "Illegal element reference off dag");
1180 return 0;
1181 }
1182
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001183};
1184
1185//===----------------------------------------------------------------------===//
1186// High-Level Classes
1187//===----------------------------------------------------------------------===//
1188
1189class RecordVal {
1190 std::string Name;
1191 RecTy *Ty;
1192 unsigned Prefix;
1193 Init *Value;
1194public:
1195 RecordVal(const std::string &N, RecTy *T, unsigned P);
1196
1197 const std::string &getName() const { return Name; }
1198
1199 unsigned getPrefix() const { return Prefix; }
1200 RecTy *getType() const { return Ty; }
1201 Init *getValue() const { return Value; }
1202
1203 bool setValue(Init *V) {
1204 if (V) {
1205 Value = V->convertInitializerTo(Ty);
1206 return Value == 0;
1207 }
1208 Value = 0;
1209 return false;
1210 }
1211
1212 void dump() const;
1213 void print(std::ostream &OS, bool PrintSem = true) const;
1214};
1215
1216inline std::ostream &operator<<(std::ostream &OS, const RecordVal &RV) {
1217 RV.print(OS << " ");
1218 return OS;
1219}
1220
Chris Lattner87a10612004-10-23 04:58:50 +00001221class Record {
Chris Lattnerf9b2edb2005-08-19 17:58:49 +00001222 std::string Name;
Chris Lattner526c8cb2009-06-21 03:39:35 +00001223 SMLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001224 std::vector<std::string> TemplateArgs;
1225 std::vector<RecordVal> Values;
1226 std::vector<Record*> SuperClasses;
1227public:
1228
Chris Lattner526c8cb2009-06-21 03:39:35 +00001229 explicit Record(const std::string &N, SMLoc loc) : Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001230 ~Record() {}
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001231
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001232 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +00001233 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001234
Chris Lattner526c8cb2009-06-21 03:39:35 +00001235 SMLoc getLoc() const { return Loc; }
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001236
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001237 const std::vector<std::string> &getTemplateArgs() const {
1238 return TemplateArgs;
1239 }
1240 const std::vector<RecordVal> &getValues() const { return Values; }
1241 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1242
1243 bool isTemplateArg(const std::string &Name) const {
1244 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1245 if (TemplateArgs[i] == Name) return true;
1246 return false;
1247 }
1248
1249 const RecordVal *getValue(const std::string &Name) const {
1250 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1251 if (Values[i].getName() == Name) return &Values[i];
1252 return 0;
1253 }
1254 RecordVal *getValue(const std::string &Name) {
1255 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1256 if (Values[i].getName() == Name) return &Values[i];
1257 return 0;
1258 }
1259
1260 void addTemplateArg(const std::string &Name) {
1261 assert(!isTemplateArg(Name) && "Template arg already defined!");
1262 TemplateArgs.push_back(Name);
1263 }
1264
1265 void addValue(const RecordVal &RV) {
1266 assert(getValue(RV.getName()) == 0 && "Value already added!");
1267 Values.push_back(RV);
1268 }
1269
1270 void removeValue(const std::string &Name) {
1271 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1272 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1273 if (Values[i].getName() == Name) {
1274 Values.erase(Values.begin()+i);
1275 return;
1276 }
1277 assert(0 && "Name does not exist in record!");
1278 }
1279
Ted Kremenek58e32872009-03-13 22:20:10 +00001280 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001281 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1282 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001283 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001284 return false;
1285 }
1286
1287 bool isSubClassOf(const std::string &Name) const {
1288 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1289 if (SuperClasses[i]->getName() == Name)
1290 return true;
1291 return false;
1292 }
1293
1294 void addSuperClass(Record *R) {
1295 assert(!isSubClassOf(R) && "Already subclassing record!");
1296 SuperClasses.push_back(R);
1297 }
1298
Chris Lattneref943742005-04-19 03:36:21 +00001299 /// resolveReferences - If there are any field references that refer to fields
1300 /// that have been filled in, we can propagate the values now.
1301 ///
1302 void resolveReferences() { resolveReferencesTo(0); }
1303
1304 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1305 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1306 /// possible references.
1307 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001308
1309 void dump() const;
1310
1311 //===--------------------------------------------------------------------===//
1312 // High-level methods useful to tablegen back-ends
1313 //
1314
1315 /// getValueInit - Return the initializer for a value with the specified name,
1316 /// or throw an exception if the field does not exist.
1317 ///
1318 Init *getValueInit(const std::string &FieldName) const;
1319
1320 /// getValueAsString - This method looks up the specified field and returns
1321 /// its value as a string, throwing an exception if the field does not exist
1322 /// or if the value is not a string.
1323 ///
1324 std::string getValueAsString(const std::string &FieldName) const;
1325
1326 /// getValueAsBitsInit - This method looks up the specified field and returns
1327 /// its value as a BitsInit, throwing an exception if the field does not exist
1328 /// or if the value is not the right type.
1329 ///
1330 BitsInit *getValueAsBitsInit(const std::string &FieldName) const;
1331
1332 /// getValueAsListInit - This method looks up the specified field and returns
1333 /// its value as a ListInit, throwing an exception if the field does not exist
1334 /// or if the value is not the right type.
1335 ///
1336 ListInit *getValueAsListInit(const std::string &FieldName) const;
1337
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001338 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001339 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001340 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001341 ///
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001342 std::vector<Record*> getValueAsListOfDefs(const std::string &FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001343
Anton Korobeynikova468a112007-11-11 11:19:37 +00001344 /// getValueAsListOfInts - This method looks up the specified field and returns
1345 /// its value as a vector of integers, throwing an exception if the field does
1346 /// not exist or if the value is not the right type.
1347 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001348 std::vector<int64_t> getValueAsListOfInts(const std::string &FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001349
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001350 /// getValueAsDef - This method looks up the specified field and returns its
1351 /// value as a Record, throwing an exception if the field does not exist or if
1352 /// the value is not the right type.
1353 ///
1354 Record *getValueAsDef(const std::string &FieldName) const;
1355
1356 /// getValueAsBit - This method looks up the specified field and returns its
1357 /// value as a bit, throwing an exception if the field does not exist or if
1358 /// the value is not the right type.
1359 ///
1360 bool getValueAsBit(const std::string &FieldName) const;
1361
1362 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001363 /// value as an int64_t, throwing an exception if the field does not exist or
1364 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001365 ///
Dan Gohmanca0546f2008-10-17 01:33:43 +00001366 int64_t getValueAsInt(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001367
1368 /// getValueAsDag - This method looks up the specified field and returns its
1369 /// value as an Dag, throwing an exception if the field does not exist or if
1370 /// the value is not the right type.
1371 ///
1372 DagInit *getValueAsDag(const std::string &FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001373
1374 /// getValueAsCode - This method looks up the specified field and returns
1375 /// its value as the string data in a CodeInit, throwing an exception if the
1376 /// field does not exist or if the value is not a code object.
1377 ///
1378 std::string getValueAsCode(const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001379};
1380
1381std::ostream &operator<<(std::ostream &OS, const Record &R);
1382
David Greenea9c6c5d2009-04-22 20:18:10 +00001383struct MultiClass {
1384 Record Rec; // Placeholder for template args and Name.
1385 typedef std::vector<Record*> RecordVector;
1386 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001387
1388 void dump() const;
1389
Chris Lattner526c8cb2009-06-21 03:39:35 +00001390 MultiClass(const std::string &Name, SMLoc Loc) : Rec(Name, Loc) {}
David Greenea9c6c5d2009-04-22 20:18:10 +00001391};
1392
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001393class RecordKeeper {
1394 std::map<std::string, Record*> Classes, Defs;
1395public:
1396 ~RecordKeeper() {
1397 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001398 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001399 delete I->second;
1400 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001401 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001402 delete I->second;
1403 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001404
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001405 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1406 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1407
1408 Record *getClass(const std::string &Name) const {
1409 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1410 return I == Classes.end() ? 0 : I->second;
1411 }
1412 Record *getDef(const std::string &Name) const {
1413 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1414 return I == Defs.end() ? 0 : I->second;
1415 }
1416 void addClass(Record *R) {
1417 assert(getClass(R->getName()) == 0 && "Class already exists!");
1418 Classes.insert(std::make_pair(R->getName(), R));
1419 }
1420 void addDef(Record *R) {
1421 assert(getDef(R->getName()) == 0 && "Def already exists!");
1422 Defs.insert(std::make_pair(R->getName(), R));
1423 }
1424
Chris Lattnerac284252005-08-19 17:58:11 +00001425 /// removeClass - Remove, but do not delete, the specified record.
1426 ///
1427 void removeClass(const std::string &Name) {
1428 assert(Classes.count(Name) && "Class does not exist!");
1429 Classes.erase(Name);
1430 }
1431 /// removeDef - Remove, but do not delete, the specified record.
1432 ///
1433 void removeDef(const std::string &Name) {
1434 assert(Defs.count(Name) && "Def does not exist!");
1435 Defs.erase(Name);
1436 }
1437
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001438 //===--------------------------------------------------------------------===//
1439 // High-level helper methods, useful for tablegen backends...
1440
1441 /// getAllDerivedDefinitions - This method returns all concrete definitions
1442 /// that derive from the specified class name. If a class with the specified
1443 /// name does not exist, an exception is thrown.
1444 std::vector<Record*>
1445 getAllDerivedDefinitions(const std::string &ClassName) const;
1446
1447
1448 void dump() const;
1449};
1450
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001451/// LessRecord - Sorting predicate to sort record pointers by name.
1452///
1453struct LessRecord {
1454 bool operator()(const Record *Rec1, const Record *Rec2) const {
1455 return Rec1->getName() < Rec2->getName();
1456 }
1457};
1458
Jim Grosbach56938af2008-09-11 17:05:32 +00001459/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1460/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001461///
1462struct LessRecordFieldName {
1463 bool operator()(const Record *Rec1, const Record *Rec2) const {
1464 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1465 }
1466};
1467
Chris Lattnerba42e492009-03-13 16:25:21 +00001468
1469class TGError {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001470 SMLoc Loc;
Chris Lattnerba42e492009-03-13 16:25:21 +00001471 std::string Message;
1472public:
Chris Lattner526c8cb2009-06-21 03:39:35 +00001473 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
Chris Lattnerba42e492009-03-13 16:25:21 +00001474
Chris Lattner526c8cb2009-06-21 03:39:35 +00001475 SMLoc getLoc() const { return Loc; }
Chris Lattnerba42e492009-03-13 16:25:21 +00001476 const std::string &getMessage() const { return Message; }
1477};
1478
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001479
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001480std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK);
1481
1482extern RecordKeeper Records;
1483
Chris Lattner526c8cb2009-06-21 03:39:35 +00001484void PrintError(SMLoc ErrorLoc, const std::string &Msg);
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001485
1486
Brian Gaeke960707c2003-11-11 22:41:34 +00001487} // End llvm namespace
1488
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001489#endif