blob: 1b33743e14119ce62c28aa9b91f1878e99cab176 [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"
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000021#include <map>
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000022
Brian Gaeke960707c2003-11-11 22:41:34 +000023namespace llvm {
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000024class raw_ostream;
Chris Lattnerbd9b9212009-03-13 16:09:24 +000025
Chris Lattneref943742005-04-19 03:36:21 +000026// RecTy subclasses.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000027class BitRecTy;
28class BitsRecTy;
29class IntRecTy;
30class StringRecTy;
31class ListRecTy;
32class CodeRecTy;
33class DagRecTy;
34class RecordRecTy;
35
Chris Lattneref943742005-04-19 03:36:21 +000036// Init subclasses.
Chris Lattner7dfc2d22004-10-27 16:14:51 +000037struct Init;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000038class UnsetInit;
39class BitInit;
40class BitsInit;
41class IntInit;
42class StringInit;
43class CodeInit;
44class ListInit;
David Greenee8f3b272009-05-14 21:22:49 +000045class UnOpInit;
Chris Lattner51ffbf12006-03-31 21:53:49 +000046class BinOpInit;
David Greene98ed3c72009-05-14 21:54:42 +000047class TernOpInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000048class DefInit;
49class DagInit;
50class TypedInit;
51class VarInit;
52class FieldInit;
53class VarBitInit;
Chris Lattner577fc3f2004-07-27 01:01:21 +000054class VarListElementInit;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000055
Chris Lattneref943742005-04-19 03:36:21 +000056// Other classes.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000057class Record;
Chris Lattneref943742005-04-19 03:36:21 +000058class RecordVal;
Bob Wilson56d862542009-04-30 17:35:11 +000059struct MultiClass;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000060
61//===----------------------------------------------------------------------===//
62// Type Classes
63//===----------------------------------------------------------------------===//
64
65struct RecTy {
66 virtual ~RecTy() {}
67
Chris Lattner8b9ecda2007-11-20 22:25:16 +000068 virtual std::string getAsString() const = 0;
Daniel Dunbar38a22bf2009-07-03 00:10:29 +000069 void print(raw_ostream &OS) const { OS << getAsString(); }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000070 void dump() const;
71
72 /// typeIsConvertibleTo - Return true if all values of 'this' type can be
73 /// converted to the specified type.
74 virtual bool typeIsConvertibleTo(const RecTy *RHS) const = 0;
75
76public: // These methods should only be called from subclasses of Init
77 virtual Init *convertValue( UnsetInit *UI) { return 0; }
78 virtual Init *convertValue( BitInit *BI) { return 0; }
79 virtual Init *convertValue( BitsInit *BI) { return 0; }
80 virtual Init *convertValue( IntInit *II) { return 0; }
81 virtual Init *convertValue(StringInit *SI) { return 0; }
82 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +000083 virtual Init *convertValue( UnOpInit *UI) {
84 return convertValue((TypedInit*)UI);
85 }
David Greene196ac3c2009-04-23 21:25:15 +000086 virtual Init *convertValue( BinOpInit *UI) {
87 return convertValue((TypedInit*)UI);
88 }
David Greene98ed3c72009-05-14 21:54:42 +000089 virtual Init *convertValue( TernOpInit *UI) {
90 return convertValue((TypedInit*)UI);
91 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +000092 virtual Init *convertValue( CodeInit *CI) { return 0; }
93 virtual Init *convertValue(VarBitInit *VB) { return 0; }
94 virtual Init *convertValue( DefInit *DI) { return 0; }
95 virtual Init *convertValue( DagInit *DI) { return 0; }
96 virtual Init *convertValue( TypedInit *TI) { return 0; }
97 virtual Init *convertValue( VarInit *VI) {
98 return convertValue((TypedInit*)VI);
99 }
100 virtual Init *convertValue( FieldInit *FI) {
101 return convertValue((TypedInit*)FI);
102 }
103
104public: // These methods should only be called by subclasses of RecTy.
105 // baseClassOf - These virtual methods should be overloaded to return true iff
106 // all values of type 'RHS' can be converted to the 'this' type.
107 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
108 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
109 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
110 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
111 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
112 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
113 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
114 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
115};
116
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000117inline raw_ostream &operator<<(raw_ostream &OS, const RecTy &Ty) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000118 Ty.print(OS);
119 return OS;
120}
121
122
123/// BitRecTy - 'bit' - Represent a single bit
124///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000125class BitRecTy : public RecTy {
126public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000127 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
128 virtual Init *convertValue( BitInit *BI) { return (Init*)BI; }
129 virtual Init *convertValue( BitsInit *BI);
130 virtual Init *convertValue( IntInit *II);
131 virtual Init *convertValue(StringInit *SI) { return 0; }
132 virtual Init *convertValue( ListInit *LI) { return 0; }
133 virtual Init *convertValue( CodeInit *CI) { return 0; }
134 virtual Init *convertValue(VarBitInit *VB) { return (Init*)VB; }
135 virtual Init *convertValue( DefInit *DI) { return 0; }
136 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000137 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000138 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000139 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000140 virtual Init *convertValue( TypedInit *TI);
141 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
142 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000143
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000144 std::string getAsString() const { return "bit"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000145
146 bool typeIsConvertibleTo(const RecTy *RHS) const {
147 return RHS->baseClassOf(this);
148 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000149 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
150 virtual bool baseClassOf(const BitsRecTy *RHS) const;
151 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
152 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
153 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
154 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
155 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
156 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
157
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000158};
159
160
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000161// BitsRecTy - 'bits<n>' - Represent a fixed number of bits
162/// BitsRecTy - 'bits&lt;n&gt;' - Represent a fixed number of bits
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000163///
164class BitsRecTy : public RecTy {
165 unsigned Size;
166public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000167 explicit BitsRecTy(unsigned Sz) : Size(Sz) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000168
169 unsigned getNumBits() const { return Size; }
170
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000171 virtual Init *convertValue( UnsetInit *UI);
172 virtual Init *convertValue( BitInit *UI);
173 virtual Init *convertValue( BitsInit *BI);
174 virtual Init *convertValue( IntInit *II);
175 virtual Init *convertValue(StringInit *SI) { return 0; }
176 virtual Init *convertValue( ListInit *LI) { return 0; }
177 virtual Init *convertValue( CodeInit *CI) { return 0; }
178 virtual Init *convertValue(VarBitInit *VB) { return 0; }
179 virtual Init *convertValue( DefInit *DI) { return 0; }
180 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000181 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000182 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000183 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000184 virtual Init *convertValue( TypedInit *TI);
185 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
186 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
187
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000188 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000189
190 bool typeIsConvertibleTo(const RecTy *RHS) const {
191 return RHS->baseClassOf(this);
192 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000193 virtual bool baseClassOf(const BitRecTy *RHS) const { return Size == 1; }
194 virtual bool baseClassOf(const BitsRecTy *RHS) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000195 return RHS->Size == Size;
196 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000197 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
198 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
199 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
200 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
201 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
202 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
203
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000204};
205
206
207/// IntRecTy - 'int' - Represent an integer value of no particular size
208///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000209class IntRecTy : public RecTy {
210public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000211 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
212 virtual Init *convertValue( BitInit *BI);
213 virtual Init *convertValue( BitsInit *BI);
214 virtual Init *convertValue( IntInit *II) { return (Init*)II; }
215 virtual Init *convertValue(StringInit *SI) { return 0; }
216 virtual Init *convertValue( ListInit *LI) { return 0; }
217 virtual Init *convertValue( CodeInit *CI) { return 0; }
218 virtual Init *convertValue(VarBitInit *VB) { return 0; }
219 virtual Init *convertValue( DefInit *DI) { return 0; }
220 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000221 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000222 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000223 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000224 virtual Init *convertValue( TypedInit *TI);
225 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
226 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
227
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000228 std::string getAsString() const { return "int"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000229
230 bool typeIsConvertibleTo(const RecTy *RHS) const {
231 return RHS->baseClassOf(this);
232 }
233
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000234 virtual bool baseClassOf(const BitRecTy *RHS) const { return true; }
235 virtual bool baseClassOf(const BitsRecTy *RHS) const { return true; }
236 virtual bool baseClassOf(const IntRecTy *RHS) const { return true; }
237 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
238 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
239 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
240 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
241 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
242
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000243};
244
245/// StringRecTy - 'string' - Represent an string value
246///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000247class StringRecTy : public RecTy {
248public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000249 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
250 virtual Init *convertValue( BitInit *BI) { return 0; }
251 virtual Init *convertValue( BitsInit *BI) { return 0; }
252 virtual Init *convertValue( IntInit *II) { return 0; }
253 virtual Init *convertValue(StringInit *SI) { return (Init*)SI; }
254 virtual Init *convertValue( ListInit *LI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000255 virtual Init *convertValue( UnOpInit *BO);
Chris Lattner51ffbf12006-03-31 21:53:49 +0000256 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000257 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
David Greene5d0c0512009-05-14 20:54:48 +0000258
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000259 virtual Init *convertValue( CodeInit *CI) { return 0; }
260 virtual Init *convertValue(VarBitInit *VB) { return 0; }
261 virtual Init *convertValue( DefInit *DI) { return 0; }
262 virtual Init *convertValue( DagInit *DI) { return 0; }
263 virtual Init *convertValue( TypedInit *TI);
264 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
265 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
266
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000267 std::string getAsString() const { return "string"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000268
269 bool typeIsConvertibleTo(const RecTy *RHS) const {
270 return RHS->baseClassOf(this);
271 }
272
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000273 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
274 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
275 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000276 virtual bool baseClassOf(const StringRecTy *RHS) const { return true; }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000277 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
278 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
279 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
280 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000281};
282
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000283// ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of
284// the specified type.
285/// ListRecTy - 'list&lt;Ty&gt;' - Represent a list of values, all of which must
286/// be of the specified type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000287///
288class ListRecTy : public RecTy {
289 RecTy *Ty;
290public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000291 explicit ListRecTy(RecTy *T) : Ty(T) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000292
293 RecTy *getElementType() const { return Ty; }
294
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000295 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
296 virtual Init *convertValue( BitInit *BI) { return 0; }
297 virtual Init *convertValue( BitsInit *BI) { return 0; }
298 virtual Init *convertValue( IntInit *II) { return 0; }
299 virtual Init *convertValue(StringInit *SI) { return 0; }
300 virtual Init *convertValue( ListInit *LI);
301 virtual Init *convertValue( CodeInit *CI) { return 0; }
302 virtual Init *convertValue(VarBitInit *VB) { return 0; }
303 virtual Init *convertValue( DefInit *DI) { return 0; }
304 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000305 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000306 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000307 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000308 virtual Init *convertValue( TypedInit *TI);
309 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
310 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000311
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000312 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000313
314 bool typeIsConvertibleTo(const RecTy *RHS) const {
315 return RHS->baseClassOf(this);
316 }
317
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000318 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
319 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
320 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
321 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
322 virtual bool baseClassOf(const ListRecTy *RHS) const {
Misha Brukman650ba8e2005-04-22 00:00:37 +0000323 return RHS->getElementType()->typeIsConvertibleTo(Ty);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000324 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000325 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
326 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
327 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000328};
329
330/// CodeRecTy - 'code' - Represent an code fragment, function or method.
331///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000332class CodeRecTy : public RecTy {
333public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000334 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
335 virtual Init *convertValue( BitInit *BI) { return 0; }
336 virtual Init *convertValue( BitsInit *BI) { return 0; }
337 virtual Init *convertValue( IntInit *II) { return 0; }
338 virtual Init *convertValue(StringInit *SI) { return 0; }
339 virtual Init *convertValue( ListInit *LI) { return 0; }
340 virtual Init *convertValue( CodeInit *CI) { return (Init*)CI; }
341 virtual Init *convertValue(VarBitInit *VB) { return 0; }
342 virtual Init *convertValue( DefInit *DI) { return 0; }
343 virtual Init *convertValue( DagInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000344 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000345 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000346 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000347 virtual Init *convertValue( TypedInit *TI);
348 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
349 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
350
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000351 std::string getAsString() const { return "code"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000352
353 bool typeIsConvertibleTo(const RecTy *RHS) const {
354 return RHS->baseClassOf(this);
355 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000356 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
357 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
358 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
359 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
360 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
361 virtual bool baseClassOf(const CodeRecTy *RHS) const { return true; }
362 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
363 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000364};
365
366/// DagRecTy - 'dag' - Represent a dag fragment
367///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000368class DagRecTy : public RecTy {
369public:
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000370 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
371 virtual Init *convertValue( BitInit *BI) { return 0; }
372 virtual Init *convertValue( BitsInit *BI) { return 0; }
373 virtual Init *convertValue( IntInit *II) { return 0; }
374 virtual Init *convertValue(StringInit *SI) { return 0; }
375 virtual Init *convertValue( ListInit *LI) { return 0; }
376 virtual Init *convertValue( CodeInit *CI) { return 0; }
377 virtual Init *convertValue(VarBitInit *VB) { return 0; }
378 virtual Init *convertValue( DefInit *DI) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000379 virtual Init *convertValue( UnOpInit *BO);
Evan Chenga32dee22007-05-15 01:23:24 +0000380 virtual Init *convertValue( BinOpInit *BO);
David Greene98ed3c72009-05-14 21:54:42 +0000381 virtual Init *convertValue( TernOpInit *BO) { return RecTy::convertValue(BO);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000382 virtual Init *convertValue( DagInit *CI) { return (Init*)CI; }
383 virtual Init *convertValue( TypedInit *TI);
384 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
385 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000386
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000387 std::string getAsString() const { return "dag"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000388
389 bool typeIsConvertibleTo(const RecTy *RHS) const {
390 return RHS->baseClassOf(this);
391 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000392
393 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
394 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
395 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
396 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
397 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
398 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
399 virtual bool baseClassOf(const DagRecTy *RHS) const { return true; }
400 virtual bool baseClassOf(const RecordRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000401};
402
403
Misha Brukmanb4f20ea2004-04-15 15:30:15 +0000404/// RecordRecTy - '[classname]' - Represent an instance of a class, such as:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000405/// (R32 X = EAX).
406///
407class RecordRecTy : public RecTy {
408 Record *Rec;
409public:
Dan Gohman56e3f632008-07-07 18:00:37 +0000410 explicit RecordRecTy(Record *R) : Rec(R) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000411
412 Record *getRecord() const { return Rec; }
413
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000414 virtual Init *convertValue( UnsetInit *UI) { return (Init*)UI; }
415 virtual Init *convertValue( BitInit *BI) { return 0; }
416 virtual Init *convertValue( BitsInit *BI) { return 0; }
417 virtual Init *convertValue( IntInit *II) { return 0; }
418 virtual Init *convertValue(StringInit *SI) { return 0; }
419 virtual Init *convertValue( ListInit *LI) { return 0; }
420 virtual Init *convertValue( CodeInit *CI) { return 0; }
421 virtual Init *convertValue(VarBitInit *VB) { return 0; }
David Greenee8f3b272009-05-14 21:22:49 +0000422 virtual Init *convertValue( UnOpInit *UI) { return RecTy::convertValue(UI);}
David Greene196ac3c2009-04-23 21:25:15 +0000423 virtual Init *convertValue( BinOpInit *UI) { return RecTy::convertValue(UI);}
David Greene98ed3c72009-05-14 21:54:42 +0000424 virtual Init *convertValue( TernOpInit *UI) { return RecTy::convertValue(UI);}
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000425 virtual Init *convertValue( DefInit *DI);
426 virtual Init *convertValue( DagInit *DI) { return 0; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000427 virtual Init *convertValue( TypedInit *VI);
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000428 virtual Init *convertValue( VarInit *VI) { return RecTy::convertValue(VI);}
429 virtual Init *convertValue( FieldInit *FI) { return RecTy::convertValue(FI);}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000430
Chris Lattner8b9ecda2007-11-20 22:25:16 +0000431 std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000432
433 bool typeIsConvertibleTo(const RecTy *RHS) const {
434 return RHS->baseClassOf(this);
435 }
Reid Spencer1c48c2d2004-12-06 23:42:37 +0000436 virtual bool baseClassOf(const BitRecTy *RHS) const { return false; }
437 virtual bool baseClassOf(const BitsRecTy *RHS) const { return false; }
438 virtual bool baseClassOf(const IntRecTy *RHS) const { return false; }
439 virtual bool baseClassOf(const StringRecTy *RHS) const { return false; }
440 virtual bool baseClassOf(const ListRecTy *RHS) const { return false; }
441 virtual bool baseClassOf(const CodeRecTy *RHS) const { return false; }
442 virtual bool baseClassOf(const DagRecTy *RHS) const { return false; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000443 virtual bool baseClassOf(const RecordRecTy *RHS) const;
444};
445
David Greene8618f952009-06-08 20:23:18 +0000446/// resolveTypes - Find a common type that T1 and T2 convert to.
447/// Return 0 if no such type exists.
448///
449RecTy *resolveTypes(RecTy *T1, RecTy *T2);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000450
451//===----------------------------------------------------------------------===//
452// Initializer Classes
453//===----------------------------------------------------------------------===//
454
455struct Init {
456 virtual ~Init() {}
457
458 /// isComplete - This virtual method should be overridden by values that may
459 /// not be completely specified yet.
460 virtual bool isComplete() const { return true; }
461
462 /// print - Print out this value.
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000463 void print(raw_ostream &OS) const { OS << getAsString(); }
Chris Lattner695506c2007-11-22 21:05:25 +0000464
465 /// getAsString - Convert this value to a string form.
466 virtual std::string getAsString() const = 0;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000467
468 /// dump - Debugging method that may be called through a debugger, just
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000469 /// invokes print on stderr.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000470 void dump() const;
471
472 /// convertInitializerTo - This virtual function is a simple call-back
473 /// function that should be overridden to call the appropriate
474 /// RecTy::convertValue method.
475 ///
476 virtual Init *convertInitializerTo(RecTy *Ty) = 0;
477
478 /// convertInitializerBitRange - This method is used to implement the bitrange
479 /// selection operator. Given an initializer, it selects the specified bits
480 /// out, returning them as a new init of bits type. If it is not legal to use
481 /// the bit subscript operator on this initializer, return null.
482 ///
483 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits) {
484 return 0;
485 }
486
Chris Lattner8bf9e062004-07-26 23:21:34 +0000487 /// convertInitListSlice - This method is used to implement the list slice
488 /// selection operator. Given an initializer, it selects the specified list
489 /// elements, returning them as a new init of list type. If it is not legal
490 /// to take a slice of this, return null.
491 ///
492 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements) {
493 return 0;
494 }
495
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000496 /// getFieldType - This method is used to implement the FieldInit class.
497 /// Implementors of this method should return the type of the named field if
498 /// they are of record type.
499 ///
500 virtual RecTy *getFieldType(const std::string &FieldName) const { return 0; }
501
502 /// getFieldInit - This method complements getFieldType to return the
503 /// initializer for the specified field. If getFieldType returns non-null
504 /// this method should return non-null, otherwise it returns null.
505 ///
506 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const {
507 return 0;
508 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000509
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000510 /// resolveReferences - This method is used by classes that refer to other
511 /// variables which may not be defined at the time they expression is formed.
512 /// If a value is set for the variable later, this method will be called on
513 /// users of the value to allow the value to propagate out.
514 ///
Chris Lattneref943742005-04-19 03:36:21 +0000515 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
516 return this;
517 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000518};
519
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000520inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000521 I.print(OS); return OS;
522}
523
David Greenee917fff2009-05-14 22:23:47 +0000524/// TypedInit - This is the common super-class of types that have a specific,
525/// explicit, type.
526///
527class TypedInit : public Init {
528 RecTy *Ty;
529public:
530 explicit TypedInit(RecTy *T) : Ty(T) {}
531
532 RecTy *getType() const { return Ty; }
533
534 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
535 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
536
537 /// resolveBitReference - This method is used to implement
538 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
539 /// simply return the resolved value, otherwise we return null.
540 ///
541 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
542 unsigned Bit) = 0;
543
544 /// resolveListElementReference - This method is used to implement
545 /// VarListElementInit::resolveReferences. If the list element is resolvable
546 /// now, we return the resolved value, otherwise we return null.
547 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
548 unsigned Elt) = 0;
549};
550
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000551
552/// UnsetInit - ? - Represents an uninitialized value
553///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000554class UnsetInit : public Init {
555public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000556 virtual Init *convertInitializerTo(RecTy *Ty) {
557 return Ty->convertValue(this);
558 }
559
560 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000561 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000562};
563
564
565/// BitInit - true/false - Represent a concrete initializer for a bit.
566///
567class BitInit : public Init {
568 bool Value;
569public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000570 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000571
572 bool getValue() const { return Value; }
573
574 virtual Init *convertInitializerTo(RecTy *Ty) {
575 return Ty->convertValue(this);
576 }
577
Chris Lattner695506c2007-11-22 21:05:25 +0000578 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000579};
580
581/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
582/// It contains a vector of bits, whose size is determined by the type.
583///
584class BitsInit : public Init {
585 std::vector<Init*> Bits;
586public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000587 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000588
589 unsigned getNumBits() const { return Bits.size(); }
590
591 Init *getBit(unsigned Bit) const {
592 assert(Bit < Bits.size() && "Bit index out of range!");
593 return Bits[Bit];
594 }
595 void setBit(unsigned Bit, Init *V) {
596 assert(Bit < Bits.size() && "Bit index out of range!");
597 assert(Bits[Bit] == 0 && "Bit already set!");
598 Bits[Bit] = V;
599 }
600
601 virtual Init *convertInitializerTo(RecTy *Ty) {
602 return Ty->convertValue(this);
603 }
604 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
605
606 virtual bool isComplete() const {
607 for (unsigned i = 0; i != getNumBits(); ++i)
608 if (!getBit(i)->isComplete()) return false;
609 return true;
610 }
Chris Lattner695506c2007-11-22 21:05:25 +0000611 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000612
Chris Lattneref943742005-04-19 03:36:21 +0000613 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000614
615 // printXX - Print this bitstream with the specified format, returning true if
616 // it is not possible.
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000617 bool printInHex(raw_ostream &OS) const;
618 bool printAsVariable(raw_ostream &OS) const;
619 bool printAsUnset(raw_ostream &OS) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000620};
621
622
623/// IntInit - 7 - Represent an initalization by a literal integer value.
624///
David Greene8618f952009-06-08 20:23:18 +0000625class IntInit : public TypedInit {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000626 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000627public:
David Greene8618f952009-06-08 20:23:18 +0000628 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000629
Dan Gohmanca0546f2008-10-17 01:33:43 +0000630 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000631
632 virtual Init *convertInitializerTo(RecTy *Ty) {
633 return Ty->convertValue(this);
634 }
635 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
636
Chris Lattner695506c2007-11-22 21:05:25 +0000637 virtual std::string getAsString() const;
David Greene8618f952009-06-08 20:23:18 +0000638
639 /// resolveBitReference - This method is used to implement
640 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
641 /// simply return the resolved value, otherwise we return null.
642 ///
643 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
644 unsigned Bit) {
645 assert(0 && "Illegal bit reference off int");
646 return 0;
647 }
648
649 /// resolveListElementReference - This method is used to implement
650 /// VarListElementInit::resolveReferences. If the list element is resolvable
651 /// now, we return the resolved value, otherwise we return null.
652 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
653 unsigned Elt) {
654 assert(0 && "Illegal element reference off int");
655 return 0;
656 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000657};
658
659
660/// StringInit - "foo" - Represent an initialization by a string value.
661///
David Greenee917fff2009-05-14 22:23:47 +0000662class StringInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000663 std::string Value;
664public:
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000665 explicit StringInit(const std::string &V)
666 : TypedInit(new StringRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000667
668 const std::string &getValue() const { return Value; }
669
670 virtual Init *convertInitializerTo(RecTy *Ty) {
671 return Ty->convertValue(this);
672 }
673
Chris Lattner695506c2007-11-22 21:05:25 +0000674 virtual std::string getAsString() const { return "\"" + Value + "\""; }
David Greenee917fff2009-05-14 22:23:47 +0000675
676 /// resolveBitReference - This method is used to implement
677 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
678 /// simply return the resolved value, otherwise we return null.
679 ///
680 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
681 unsigned Bit) {
682 assert(0 && "Illegal bit reference off string");
683 return 0;
684 }
685
686 /// resolveListElementReference - This method is used to implement
687 /// VarListElementInit::resolveReferences. If the list element is resolvable
688 /// now, we return the resolved value, otherwise we return null.
689 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
690 unsigned Elt) {
691 assert(0 && "Illegal element reference off string");
692 return 0;
693 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000694};
695
696/// CodeInit - "[{...}]" - Represent a code fragment.
697///
698class CodeInit : public Init {
699 std::string Value;
700public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000701 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000702
703 const std::string getValue() const { return Value; }
704
705 virtual Init *convertInitializerTo(RecTy *Ty) {
706 return Ty->convertValue(this);
707 }
708
Chris Lattner695506c2007-11-22 21:05:25 +0000709 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000710};
711
712/// ListInit - [AL, AH, CL] - Represent a list of defs
713///
David Greene8618f952009-06-08 20:23:18 +0000714class ListInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000715 std::vector<Init*> Values;
716public:
David Greened571b3c2009-05-14 22:38:31 +0000717 typedef std::vector<Init*>::iterator iterator;
718 typedef std::vector<Init*>::const_iterator const_iterator;
719
David Greene8618f952009-06-08 20:23:18 +0000720 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
721 : TypedInit(new ListRecTy(EltTy)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000722 Values.swap(Vs);
723 }
David Greene8618f952009-06-08 20:23:18 +0000724 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
725 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000726
727 unsigned getSize() const { return Values.size(); }
728 Init *getElement(unsigned i) const {
729 assert(i < Values.size() && "List element index out of range!");
730 return Values[i];
731 }
732
Chris Lattnercbebe462007-02-27 22:08:27 +0000733 Record *getElementAsRecord(unsigned i) const;
734
Chris Lattner8bf9e062004-07-26 23:21:34 +0000735 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
736
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000737 virtual Init *convertInitializerTo(RecTy *Ty) {
738 return Ty->convertValue(this);
739 }
740
Chris Lattner577fc3f2004-07-27 01:01:21 +0000741 /// resolveReferences - This method is used by classes that refer to other
742 /// variables which may not be defined at the time they expression is formed.
743 /// If a value is set for the variable later, this method will be called on
744 /// users of the value to allow the value to propagate out.
745 ///
Chris Lattneref943742005-04-19 03:36:21 +0000746 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000747
Chris Lattner695506c2007-11-22 21:05:25 +0000748 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000749
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000750 inline iterator begin() { return Values.begin(); }
751 inline const_iterator begin() const { return Values.begin(); }
752 inline iterator end () { return Values.end(); }
753 inline const_iterator end () const { return Values.end(); }
754
755 inline size_t size () const { return Values.size(); }
756 inline bool empty() const { return Values.empty(); }
David Greene8618f952009-06-08 20:23:18 +0000757
758 /// resolveBitReference - This method is used to implement
759 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
760 /// simply return the resolved value, otherwise we return null.
761 ///
762 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
763 unsigned Bit) {
764 assert(0 && "Illegal bit reference off list");
765 return 0;
766 }
767
768 /// resolveListElementReference - This method is used to implement
769 /// VarListElementInit::resolveReferences. If the list element is resolvable
770 /// now, we return the resolved value, otherwise we return null.
771 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
772 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000773};
774
775
David Greene5d0c0512009-05-14 20:54:48 +0000776/// OpInit - Base class for operators
David Greene196ac3c2009-04-23 21:25:15 +0000777///
David Greene5d0c0512009-05-14 20:54:48 +0000778class OpInit : public TypedInit {
David Greene196ac3c2009-04-23 21:25:15 +0000779public:
David Greene5d0c0512009-05-14 20:54:48 +0000780 OpInit(RecTy *Type) : TypedInit(Type) {}
781
782 // Clone - Clone this operator, replacing arguments with the new list
783 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
784
Dan Gohman1432ef82009-08-12 22:10:57 +0000785 virtual int getNumOperands() const = 0;
David Greene5d0c0512009-05-14 20:54:48 +0000786 virtual Init *getOperand(int i) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000787
788 // Fold - If possible, fold this to a simpler init. Return this if not
789 // possible to fold.
David Greene5d0c0512009-05-14 20:54:48 +0000790 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000791
792 virtual Init *convertInitializerTo(RecTy *Ty) {
793 return Ty->convertValue(this);
794 }
795
796 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
797 unsigned Bit);
798 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
799 unsigned Elt);
David Greene5d0c0512009-05-14 20:54:48 +0000800};
801
802
803/// UnOpInit - !op (X) - Transform an init.
804///
David Greenee8f3b272009-05-14 21:22:49 +0000805class UnOpInit : public OpInit {
806public:
David Greened571b3c2009-05-14 22:38:31 +0000807 enum UnaryOp { CAST, CAR, CDR, LNULL };
David Greenee8f3b272009-05-14 21:22:49 +0000808private:
809 UnaryOp Opc;
810 Init *LHS;
811public:
812 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
813 OpInit(Type), Opc(opc), LHS(lhs) {
814 }
David Greene5d0c0512009-05-14 20:54:48 +0000815
David Greenee8f3b272009-05-14 21:22:49 +0000816 // Clone - Clone this operator, replacing arguments with the new list
817 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000818 assert(Operands.size() == 1 &&
819 "Wrong number of operands for unary operation");
David Greenee8f3b272009-05-14 21:22:49 +0000820 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
821 }
David Greene5d0c0512009-05-14 20:54:48 +0000822
Dan Gohman1432ef82009-08-12 22:10:57 +0000823 int getNumOperands() const { return 1; }
David Greenee8f3b272009-05-14 21:22:49 +0000824 Init *getOperand(int i) {
825 assert(i == 0 && "Invalid operand id for unary operator");
826 return getOperand();
827 }
David Greene5d0c0512009-05-14 20:54:48 +0000828
David Greenee8f3b272009-05-14 21:22:49 +0000829 UnaryOp getOpcode() const { return Opc; }
830 Init *getOperand() const { return LHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000831
David Greenee8f3b272009-05-14 21:22:49 +0000832 // Fold - If possible, fold this to a simpler init. Return this if not
833 // possible to fold.
834 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000835
David Greenee8f3b272009-05-14 21:22:49 +0000836 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000837
David Greeneefa19612009-06-29 20:05:29 +0000838 /// getFieldType - This method is used to implement the FieldInit class.
839 /// Implementors of this method should return the type of the named field if
840 /// they are of record type.
841 ///
842 virtual RecTy *getFieldType(const std::string &FieldName) const;
843
David Greenee8f3b272009-05-14 21:22:49 +0000844 virtual std::string getAsString() const;
845};
David Greene5d0c0512009-05-14 20:54:48 +0000846
847/// BinOpInit - !op (X, Y) - Combine two inits.
848///
849class BinOpInit : public OpInit {
850public:
David Greene58a6b762009-06-09 18:31:17 +0000851 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT };
David Greene5d0c0512009-05-14 20:54:48 +0000852private:
853 BinaryOp Opc;
854 Init *LHS, *RHS;
855public:
856 BinOpInit(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type) :
857 OpInit(Type), Opc(opc), LHS(lhs), RHS(rhs) {
858 }
859
860 // Clone - Clone this operator, replacing arguments with the new list
861 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000862 assert(Operands.size() == 2 &&
863 "Wrong number of operands for binary operation");
David Greene5d0c0512009-05-14 20:54:48 +0000864 return new BinOpInit(getOpcode(), Operands[0], Operands[1], getType());
865 }
866
Dan Gohman1432ef82009-08-12 22:10:57 +0000867 int getNumOperands() const { return 2; }
David Greene5d0c0512009-05-14 20:54:48 +0000868 Init *getOperand(int i) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000869 assert((i == 0 || i == 1) && "Invalid operand id for binary operator");
David Greene5d0c0512009-05-14 20:54:48 +0000870 if (i == 0) {
871 return getLHS();
872 }
873 else {
874 return getRHS();
875 }
876 }
877
878 BinaryOp getOpcode() const { return Opc; }
879 Init *getLHS() const { return LHS; }
880 Init *getRHS() const { return RHS; }
881
882 // Fold - If possible, fold this to a simpler init. Return this if not
883 // possible to fold.
884 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene196ac3c2009-04-23 21:25:15 +0000885
886 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
887
888 virtual std::string getAsString() const;
889};
890
David Greene5d0c0512009-05-14 20:54:48 +0000891/// TernOpInit - !op (X, Y, Z) - Combine two inits.
892///
David Greene98ed3c72009-05-14 21:54:42 +0000893class TernOpInit : public OpInit {
894public:
David Greene58a6b762009-06-09 18:31:17 +0000895 enum TernaryOp { SUBST, FOREACH, IF };
David Greene98ed3c72009-05-14 21:54:42 +0000896private:
897 TernaryOp Opc;
898 Init *LHS, *MHS, *RHS;
899public:
900 TernOpInit(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type) :
901 OpInit(Type), Opc(opc), LHS(lhs), MHS(mhs), RHS(rhs) {
902 }
David Greene5d0c0512009-05-14 20:54:48 +0000903
David Greene98ed3c72009-05-14 21:54:42 +0000904 // Clone - Clone this operator, replacing arguments with the new list
905 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000906 assert(Operands.size() == 3 &&
907 "Wrong number of operands for ternary operation");
908 return new TernOpInit(getOpcode(), Operands[0], Operands[1], Operands[2],
909 getType());
David Greene98ed3c72009-05-14 21:54:42 +0000910 }
David Greene5d0c0512009-05-14 20:54:48 +0000911
Dan Gohman1432ef82009-08-12 22:10:57 +0000912 int getNumOperands() const { return 3; }
David Greene98ed3c72009-05-14 21:54:42 +0000913 Init *getOperand(int i) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000914 assert((i == 0 || i == 1 || i == 2) &&
915 "Invalid operand id for ternary operator");
David Greene98ed3c72009-05-14 21:54:42 +0000916 if (i == 0) {
917 return getLHS();
918 }
919 else if (i == 1) {
920 return getMHS();
921 }
922 else {
923 return getRHS();
924 }
925 }
David Greene5d0c0512009-05-14 20:54:48 +0000926
David Greene98ed3c72009-05-14 21:54:42 +0000927 TernaryOp getOpcode() const { return Opc; }
928 Init *getLHS() const { return LHS; }
929 Init *getMHS() const { return MHS; }
930 Init *getRHS() const { return RHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000931
David Greene98ed3c72009-05-14 21:54:42 +0000932 // Fold - If possible, fold this to a simpler init. Return this if not
933 // possible to fold.
934 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000935
David Greene98ed3c72009-05-14 21:54:42 +0000936 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
David Greene5d0c0512009-05-14 20:54:48 +0000937
David Greene98ed3c72009-05-14 21:54:42 +0000938 virtual std::string getAsString() const;
939};
David Greene5d0c0512009-05-14 20:54:48 +0000940
David Greene196ac3c2009-04-23 21:25:15 +0000941
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000942/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
943///
944class VarInit : public TypedInit {
945 std::string VarName;
946public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000947 explicit VarInit(const std::string &VN, RecTy *T)
948 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000949
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000950 virtual Init *convertInitializerTo(RecTy *Ty) {
951 return Ty->convertValue(this);
952 }
953
954 const std::string &getName() const { return VarName; }
955
Chris Lattneref943742005-04-19 03:36:21 +0000956 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
957 unsigned Bit);
958 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
959 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000960
961 virtual RecTy *getFieldType(const std::string &FieldName) const;
962 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
963
964 /// resolveReferences - This method is used by classes that refer to other
965 /// variables which may not be defined at the time they expression is formed.
966 /// If a value is set for the variable later, this method will be called on
967 /// users of the value to allow the value to propagate out.
968 ///
Chris Lattneref943742005-04-19 03:36:21 +0000969 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000970
Chris Lattner695506c2007-11-22 21:05:25 +0000971 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000972};
973
974
975/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
976///
977class VarBitInit : public Init {
978 TypedInit *TI;
979 unsigned Bit;
980public:
981 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
982 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
983 ((BitsRecTy*)T->getType())->getNumBits() > B &&
984 "Illegal VarBitInit expression!");
985 }
986
987 virtual Init *convertInitializerTo(RecTy *Ty) {
988 return Ty->convertValue(this);
989 }
990
991 TypedInit *getVariable() const { return TI; }
992 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000993
Chris Lattner695506c2007-11-22 21:05:25 +0000994 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000995 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000996};
997
Misha Brukman650ba8e2005-04-22 00:00:37 +0000998/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000999/// field.
1000class VarListElementInit : public TypedInit {
1001 TypedInit *TI;
1002 unsigned Element;
1003public:
1004 VarListElementInit(TypedInit *T, unsigned E)
1005 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1006 TI(T), Element(E) {
1007 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1008 "Illegal VarBitInit expression!");
1009 }
1010
1011 virtual Init *convertInitializerTo(RecTy *Ty) {
1012 return Ty->convertValue(this);
1013 }
1014
1015 TypedInit *getVariable() const { return TI; }
1016 unsigned getElementNum() const { return Element; }
1017
Chris Lattneref943742005-04-19 03:36:21 +00001018 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1019 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001020
1021 /// resolveListElementReference - This method is used to implement
1022 /// VarListElementInit::resolveReferences. If the list element is resolvable
1023 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +00001024 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1025 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001026
Chris Lattner695506c2007-11-22 21:05:25 +00001027 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +00001028 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001029};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001030
1031/// DefInit - AL - Represent a reference to a 'def' in the description
1032///
David Greenee917fff2009-05-14 22:23:47 +00001033class DefInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001034 Record *Def;
1035public:
David Greenee917fff2009-05-14 22:23:47 +00001036 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001037
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001038 virtual Init *convertInitializerTo(RecTy *Ty) {
1039 return Ty->convertValue(this);
1040 }
1041
1042 Record *getDef() const { return Def; }
1043
1044 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1045
1046 virtual RecTy *getFieldType(const std::string &FieldName) const;
1047 virtual Init *getFieldInit(Record &R, const std::string &FieldName) const;
Misha Brukman650ba8e2005-04-22 00:00:37 +00001048
Chris Lattner695506c2007-11-22 21:05:25 +00001049 virtual std::string getAsString() const;
David Greenee917fff2009-05-14 22:23:47 +00001050
1051 /// resolveBitReference - This method is used to implement
1052 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
1053 /// simply return the resolved value, otherwise we return null.
1054 ///
1055 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1056 unsigned Bit) {
1057 assert(0 && "Illegal bit reference off def");
1058 return 0;
1059 }
1060
1061 /// resolveListElementReference - This method is used to implement
1062 /// VarListElementInit::resolveReferences. If the list element is resolvable
1063 /// now, we return the resolved value, otherwise we return null.
1064 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1065 unsigned Elt) {
1066 assert(0 && "Illegal element reference off def");
1067 return 0;
1068 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001069};
1070
1071
1072/// FieldInit - X.Y - Represent a reference to a subfield of a variable
1073///
1074class FieldInit : public TypedInit {
1075 Init *Rec; // Record we are referring to
1076 std::string FieldName; // Field we are accessing
1077public:
1078 FieldInit(Init *R, const std::string &FN)
1079 : TypedInit(R->getFieldType(FN)), Rec(R), FieldName(FN) {
1080 assert(getType() && "FieldInit with non-record type!");
1081 }
1082
1083 virtual Init *convertInitializerTo(RecTy *Ty) {
1084 return Ty->convertValue(this);
1085 }
1086
Chris Lattneref943742005-04-19 03:36:21 +00001087 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1088 unsigned Bit);
1089 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1090 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001091
Chris Lattneref943742005-04-19 03:36:21 +00001092 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001093
Chris Lattner695506c2007-11-22 21:05:25 +00001094 virtual std::string getAsString() const {
1095 return Rec->getAsString() + "." + FieldName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001096 }
1097};
1098
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001099/// DagInit - (v a, b) - Represent a DAG tree value. DAG inits are required
1100/// to have at least one value then a (possibly empty) list of arguments. Each
1101/// argument can have a name associated with it.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001102///
David Greenee917fff2009-05-14 22:23:47 +00001103class DagInit : public TypedInit {
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001104 Init *Val;
Nate Begemandbe3f772009-03-19 05:21:56 +00001105 std::string ValName;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001106 std::vector<Init*> Args;
1107 std::vector<std::string> ArgNames;
1108public:
Nate Begemandbe3f772009-03-19 05:21:56 +00001109 DagInit(Init *V, std::string VN,
1110 const std::vector<std::pair<Init*, std::string> > &args)
David Greenee917fff2009-05-14 22:23:47 +00001111 : TypedInit(new DagRecTy), Val(V), ValName(VN) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001112 Args.reserve(args.size());
1113 ArgNames.reserve(args.size());
1114 for (unsigned i = 0, e = args.size(); i != e; ++i) {
1115 Args.push_back(args[i].first);
1116 ArgNames.push_back(args[i].second);
1117 }
1118 }
Nate Begemandbe3f772009-03-19 05:21:56 +00001119 DagInit(Init *V, std::string VN, const std::vector<Init*> &args,
Chris Lattner0d3ef402006-01-31 06:02:35 +00001120 const std::vector<std::string> &argNames)
David Greenee917fff2009-05-14 22:23:47 +00001121 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args), ArgNames(argNames) {
Chris Lattner0d3ef402006-01-31 06:02:35 +00001122 }
1123
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001124 virtual Init *convertInitializerTo(RecTy *Ty) {
1125 return Ty->convertValue(this);
1126 }
1127
Chris Lattnerb59cf3c2006-03-30 22:50:40 +00001128 Init *getOperator() const { return Val; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001129
Nate Begemandbe3f772009-03-19 05:21:56 +00001130 const std::string &getName() const { return ValName; }
1131
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001132 unsigned getNumArgs() const { return Args.size(); }
1133 Init *getArg(unsigned Num) const {
1134 assert(Num < Args.size() && "Arg number out of range!");
1135 return Args[Num];
1136 }
1137 const std::string &getArgName(unsigned Num) const {
1138 assert(Num < ArgNames.size() && "Arg number out of range!");
1139 return ArgNames[Num];
1140 }
1141
1142 void setArg(unsigned Num, Init *I) {
1143 assert(Num < Args.size() && "Arg number out of range!");
1144 Args[Num] = I;
1145 }
Chris Lattner0d3ef402006-01-31 06:02:35 +00001146
1147 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001148
Chris Lattner695506c2007-11-22 21:05:25 +00001149 virtual std::string getAsString() const;
Anton Korobeynikov010bd772008-01-22 11:00:07 +00001150
1151 typedef std::vector<Init*>::iterator arg_iterator;
1152 typedef std::vector<Init*>::const_iterator const_arg_iterator;
1153 typedef std::vector<std::string>::iterator name_iterator;
1154 typedef std::vector<std::string>::const_iterator const_name_iterator;
1155
1156 inline arg_iterator arg_begin() { return Args.begin(); }
1157 inline const_arg_iterator arg_begin() const { return Args.begin(); }
1158 inline arg_iterator arg_end () { return Args.end(); }
1159 inline const_arg_iterator arg_end () const { return Args.end(); }
1160
1161 inline size_t arg_size () const { return Args.size(); }
1162 inline bool arg_empty() const { return Args.empty(); }
1163
1164 inline name_iterator name_begin() { return ArgNames.begin(); }
1165 inline const_name_iterator name_begin() const { return ArgNames.begin(); }
1166 inline name_iterator name_end () { return ArgNames.end(); }
1167 inline const_name_iterator name_end () const { return ArgNames.end(); }
1168
1169 inline size_t name_size () const { return ArgNames.size(); }
1170 inline bool name_empty() const { return ArgNames.empty(); }
1171
David Greenee917fff2009-05-14 22:23:47 +00001172 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1173 unsigned Bit) {
1174 assert(0 && "Illegal bit reference off dag");
1175 return 0;
1176 }
1177
1178 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1179 unsigned Elt) {
1180 assert(0 && "Illegal element reference off dag");
1181 return 0;
1182 }
1183
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001184};
1185
1186//===----------------------------------------------------------------------===//
1187// High-Level Classes
1188//===----------------------------------------------------------------------===//
1189
1190class RecordVal {
1191 std::string Name;
1192 RecTy *Ty;
1193 unsigned Prefix;
1194 Init *Value;
1195public:
1196 RecordVal(const std::string &N, RecTy *T, unsigned P);
1197
1198 const std::string &getName() const { return Name; }
1199
1200 unsigned getPrefix() const { return Prefix; }
1201 RecTy *getType() const { return Ty; }
1202 Init *getValue() const { return Value; }
1203
1204 bool setValue(Init *V) {
1205 if (V) {
1206 Value = V->convertInitializerTo(Ty);
1207 return Value == 0;
1208 }
1209 Value = 0;
1210 return false;
1211 }
1212
1213 void dump() const;
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001214 void print(raw_ostream &OS, bool PrintSem = true) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001215};
1216
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001217inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001218 RV.print(OS << " ");
1219 return OS;
1220}
1221
Chris Lattner87a10612004-10-23 04:58:50 +00001222class Record {
Daniel Dunbarced00812009-08-23 09:47:37 +00001223 static unsigned LastID;
1224
1225 // Unique record ID.
1226 unsigned ID;
Chris Lattnerf9b2edb2005-08-19 17:58:49 +00001227 std::string Name;
Chris Lattner526c8cb2009-06-21 03:39:35 +00001228 SMLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001229 std::vector<std::string> TemplateArgs;
1230 std::vector<RecordVal> Values;
1231 std::vector<Record*> SuperClasses;
1232public:
1233
Daniel Dunbarced00812009-08-23 09:47:37 +00001234 explicit Record(const std::string &N, SMLoc loc) :
1235 ID(LastID++), Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001236 ~Record() {}
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001237
Daniel Dunbarced00812009-08-23 09:47:37 +00001238 unsigned getID() const { return ID; }
1239
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001240 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +00001241 void setName(const std::string &Name); // Also updates RecordKeeper.
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001242
Chris Lattner526c8cb2009-06-21 03:39:35 +00001243 SMLoc getLoc() const { return Loc; }
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001244
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001245 const std::vector<std::string> &getTemplateArgs() const {
1246 return TemplateArgs;
1247 }
1248 const std::vector<RecordVal> &getValues() const { return Values; }
1249 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1250
Chris Lattner42bb0c12009-09-18 18:31:37 +00001251 bool isTemplateArg(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001252 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1253 if (TemplateArgs[i] == Name) return true;
1254 return false;
1255 }
1256
Chris Lattner42bb0c12009-09-18 18:31:37 +00001257 const RecordVal *getValue(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001258 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1259 if (Values[i].getName() == Name) return &Values[i];
1260 return 0;
1261 }
Chris Lattner42bb0c12009-09-18 18:31:37 +00001262 RecordVal *getValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001263 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1264 if (Values[i].getName() == Name) return &Values[i];
1265 return 0;
1266 }
1267
Chris Lattner42bb0c12009-09-18 18:31:37 +00001268 void addTemplateArg(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001269 assert(!isTemplateArg(Name) && "Template arg already defined!");
1270 TemplateArgs.push_back(Name);
1271 }
1272
1273 void addValue(const RecordVal &RV) {
1274 assert(getValue(RV.getName()) == 0 && "Value already added!");
1275 Values.push_back(RV);
1276 }
1277
Chris Lattner42bb0c12009-09-18 18:31:37 +00001278 void removeValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001279 assert(getValue(Name) && "Cannot remove an entry that does not exist!");
1280 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1281 if (Values[i].getName() == Name) {
1282 Values.erase(Values.begin()+i);
1283 return;
1284 }
1285 assert(0 && "Name does not exist in record!");
1286 }
1287
Ted Kremenek58e32872009-03-13 22:20:10 +00001288 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001289 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1290 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001291 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001292 return false;
1293 }
1294
Chris Lattner42bb0c12009-09-18 18:31:37 +00001295 bool isSubClassOf(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001296 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1297 if (SuperClasses[i]->getName() == Name)
1298 return true;
1299 return false;
1300 }
1301
1302 void addSuperClass(Record *R) {
1303 assert(!isSubClassOf(R) && "Already subclassing record!");
1304 SuperClasses.push_back(R);
1305 }
1306
Chris Lattneref943742005-04-19 03:36:21 +00001307 /// resolveReferences - If there are any field references that refer to fields
1308 /// that have been filled in, we can propagate the values now.
1309 ///
1310 void resolveReferences() { resolveReferencesTo(0); }
1311
1312 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1313 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1314 /// possible references.
1315 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001316
1317 void dump() const;
1318
1319 //===--------------------------------------------------------------------===//
1320 // High-level methods useful to tablegen back-ends
1321 //
1322
1323 /// getValueInit - Return the initializer for a value with the specified name,
1324 /// or throw an exception if the field does not exist.
1325 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001326 Init *getValueInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001327
1328 /// getValueAsString - This method looks up the specified field and returns
1329 /// its value as a string, throwing an exception if the field does not exist
1330 /// or if the value is not a string.
1331 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001332 std::string getValueAsString(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001333
1334 /// getValueAsBitsInit - This method looks up the specified field and returns
1335 /// its value as a BitsInit, throwing an exception if the field does not exist
1336 /// or if the value is not the right type.
1337 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001338 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001339
1340 /// getValueAsListInit - This method looks up the specified field and returns
1341 /// its value as a ListInit, throwing an exception if the field does not exist
1342 /// or if the value is not the right type.
1343 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001344 ListInit *getValueAsListInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001345
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001346 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001347 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001348 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001349 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001350 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001351
Anton Korobeynikova468a112007-11-11 11:19:37 +00001352 /// getValueAsListOfInts - This method looks up the specified field and returns
1353 /// its value as a vector of integers, throwing an exception if the field does
1354 /// not exist or if the value is not the right type.
1355 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001356 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
Anton Korobeynikova468a112007-11-11 11:19:37 +00001357
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001358 /// getValueAsDef - This method looks up the specified field and returns its
1359 /// value as a Record, throwing an exception if the field does not exist or if
1360 /// the value is not the right type.
1361 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001362 Record *getValueAsDef(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001363
1364 /// getValueAsBit - This method looks up the specified field and returns its
1365 /// value as a bit, throwing an exception if the field does not exist or if
1366 /// the value is not the right type.
1367 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001368 bool getValueAsBit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001369
1370 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001371 /// value as an int64_t, throwing an exception if the field does not exist or
1372 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001373 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001374 int64_t getValueAsInt(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001375
1376 /// getValueAsDag - This method looks up the specified field and returns its
1377 /// value as an Dag, throwing an exception if the field does not exist or if
1378 /// the value is not the right type.
1379 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001380 DagInit *getValueAsDag(StringRef FieldName) const;
Chris Lattnerae939eb2005-09-13 21:44:28 +00001381
1382 /// getValueAsCode - This method looks up the specified field and returns
1383 /// its value as the string data in a CodeInit, throwing an exception if the
1384 /// field does not exist or if the value is not a code object.
1385 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001386 std::string getValueAsCode(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001387};
1388
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001389raw_ostream &operator<<(raw_ostream &OS, const Record &R);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001390
David Greenea9c6c5d2009-04-22 20:18:10 +00001391struct MultiClass {
1392 Record Rec; // Placeholder for template args and Name.
1393 typedef std::vector<Record*> RecordVector;
1394 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001395
1396 void dump() const;
1397
Chris Lattner526c8cb2009-06-21 03:39:35 +00001398 MultiClass(const std::string &Name, SMLoc Loc) : Rec(Name, Loc) {}
David Greenea9c6c5d2009-04-22 20:18:10 +00001399};
1400
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001401class RecordKeeper {
1402 std::map<std::string, Record*> Classes, Defs;
1403public:
1404 ~RecordKeeper() {
1405 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001406 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001407 delete I->second;
1408 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001409 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001410 delete I->second;
1411 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001412
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001413 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1414 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1415
1416 Record *getClass(const std::string &Name) const {
1417 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1418 return I == Classes.end() ? 0 : I->second;
1419 }
1420 Record *getDef(const std::string &Name) const {
1421 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1422 return I == Defs.end() ? 0 : I->second;
1423 }
1424 void addClass(Record *R) {
1425 assert(getClass(R->getName()) == 0 && "Class already exists!");
1426 Classes.insert(std::make_pair(R->getName(), R));
1427 }
1428 void addDef(Record *R) {
1429 assert(getDef(R->getName()) == 0 && "Def already exists!");
1430 Defs.insert(std::make_pair(R->getName(), R));
1431 }
1432
Chris Lattnerac284252005-08-19 17:58:11 +00001433 /// removeClass - Remove, but do not delete, the specified record.
1434 ///
1435 void removeClass(const std::string &Name) {
1436 assert(Classes.count(Name) && "Class does not exist!");
1437 Classes.erase(Name);
1438 }
1439 /// removeDef - Remove, but do not delete, the specified record.
1440 ///
1441 void removeDef(const std::string &Name) {
1442 assert(Defs.count(Name) && "Def does not exist!");
1443 Defs.erase(Name);
1444 }
1445
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001446 //===--------------------------------------------------------------------===//
1447 // High-level helper methods, useful for tablegen backends...
1448
1449 /// getAllDerivedDefinitions - This method returns all concrete definitions
1450 /// that derive from the specified class name. If a class with the specified
1451 /// name does not exist, an exception is thrown.
1452 std::vector<Record*>
1453 getAllDerivedDefinitions(const std::string &ClassName) const;
1454
1455
1456 void dump() const;
1457};
1458
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001459/// LessRecord - Sorting predicate to sort record pointers by name.
1460///
1461struct LessRecord {
1462 bool operator()(const Record *Rec1, const Record *Rec2) const {
1463 return Rec1->getName() < Rec2->getName();
1464 }
1465};
1466
Jim Grosbach56938af2008-09-11 17:05:32 +00001467/// LessRecordFieldName - Sorting predicate to sort record pointers by their
1468/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001469///
1470struct LessRecordFieldName {
1471 bool operator()(const Record *Rec1, const Record *Rec2) const {
1472 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1473 }
1474};
1475
Chris Lattnerba42e492009-03-13 16:25:21 +00001476
1477class TGError {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001478 SMLoc Loc;
Chris Lattnerba42e492009-03-13 16:25:21 +00001479 std::string Message;
1480public:
Chris Lattner526c8cb2009-06-21 03:39:35 +00001481 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
Chris Lattnerba42e492009-03-13 16:25:21 +00001482
Chris Lattner526c8cb2009-06-21 03:39:35 +00001483 SMLoc getLoc() const { return Loc; }
Chris Lattnerba42e492009-03-13 16:25:21 +00001484 const std::string &getMessage() const { return Message; }
1485};
1486
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001487
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001488raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001489
1490extern RecordKeeper Records;
1491
Chris Lattner526c8cb2009-06-21 03:39:35 +00001492void PrintError(SMLoc ErrorLoc, const std::string &Msg);
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001493
1494
Brian Gaeke960707c2003-11-11 22:41:34 +00001495} // End llvm namespace
1496
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001497#endif