blob: a4ce2557d16d6de4a3a5b81915d16c3383bdec8a [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"
Chandler Carruth56869f22009-10-26 01:35:46 +000019#include "llvm/System/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;
Bob Wilson7248f862009-11-22 04:24:42 +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
Bob Wilson7248f862009-11-22 04:24:42 +0000446/// resolveTypes - Find a common type that T1 and T2 convert to.
David Greene8618f952009-06-08 20:23:18 +0000447/// 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 ///
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +0000506 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
507 const std::string &FieldName) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000508 return 0;
509 }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000510
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000511 /// resolveReferences - This method is used by classes that refer to other
Bob Wilson159905a2009-11-21 22:44:20 +0000512 /// variables which may not be defined at the time the expression is formed.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000513 /// If a value is set for the variable later, this method will be called on
514 /// users of the value to allow the value to propagate out.
515 ///
Chris Lattneref943742005-04-19 03:36:21 +0000516 virtual Init *resolveReferences(Record &R, const RecordVal *RV) {
517 return this;
518 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000519};
520
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000521inline raw_ostream &operator<<(raw_ostream &OS, const Init &I) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000522 I.print(OS); return OS;
523}
524
David Greenee917fff2009-05-14 22:23:47 +0000525/// TypedInit - This is the common super-class of types that have a specific,
526/// explicit, type.
527///
528class TypedInit : public Init {
529 RecTy *Ty;
530public:
531 explicit TypedInit(RecTy *T) : Ty(T) {}
532
533 RecTy *getType() const { return Ty; }
534
535 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
536 virtual Init *convertInitListSlice(const std::vector<unsigned> &Elements);
537
David Greene2a9de4d2010-09-03 21:00:49 +0000538 /// getFieldType - This method is used to implement the FieldInit class.
539 /// Implementors of this method should return the type of the named field if
540 /// they are of record type.
541 ///
542 virtual RecTy *getFieldType(const std::string &FieldName) const;
543
David Greenee917fff2009-05-14 22:23:47 +0000544 /// resolveBitReference - This method is used to implement
545 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
546 /// simply return the resolved value, otherwise we return null.
547 ///
548 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
549 unsigned Bit) = 0;
550
551 /// resolveListElementReference - This method is used to implement
552 /// VarListElementInit::resolveReferences. If the list element is resolvable
553 /// now, we return the resolved value, otherwise we return null.
554 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
555 unsigned Elt) = 0;
556};
557
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000558
559/// UnsetInit - ? - Represents an uninitialized value
560///
Chris Lattner7dfc2d22004-10-27 16:14:51 +0000561class UnsetInit : public Init {
562public:
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000563 virtual Init *convertInitializerTo(RecTy *Ty) {
564 return Ty->convertValue(this);
565 }
566
567 virtual bool isComplete() const { return false; }
Chris Lattner695506c2007-11-22 21:05:25 +0000568 virtual std::string getAsString() const { return "?"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000569};
570
571
572/// BitInit - true/false - Represent a concrete initializer for a bit.
573///
574class BitInit : public Init {
575 bool Value;
576public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000577 explicit BitInit(bool V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000578
579 bool getValue() const { return Value; }
580
581 virtual Init *convertInitializerTo(RecTy *Ty) {
582 return Ty->convertValue(this);
583 }
584
Chris Lattner695506c2007-11-22 21:05:25 +0000585 virtual std::string getAsString() const { return Value ? "1" : "0"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000586};
587
588/// BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
589/// It contains a vector of bits, whose size is determined by the type.
590///
591class BitsInit : public Init {
592 std::vector<Init*> Bits;
593public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000594 explicit BitsInit(unsigned Size) : Bits(Size) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000595
596 unsigned getNumBits() const { return Bits.size(); }
597
598 Init *getBit(unsigned Bit) const {
599 assert(Bit < Bits.size() && "Bit index out of range!");
600 return Bits[Bit];
601 }
602 void setBit(unsigned Bit, Init *V) {
603 assert(Bit < Bits.size() && "Bit index out of range!");
604 assert(Bits[Bit] == 0 && "Bit already set!");
605 Bits[Bit] = V;
606 }
607
608 virtual Init *convertInitializerTo(RecTy *Ty) {
609 return Ty->convertValue(this);
610 }
611 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
612
613 virtual bool isComplete() const {
614 for (unsigned i = 0; i != getNumBits(); ++i)
615 if (!getBit(i)->isComplete()) return false;
616 return true;
617 }
Johnny Chen4e8bd582010-04-09 21:01:02 +0000618 bool allInComplete() const {
619 for (unsigned i = 0; i != getNumBits(); ++i)
620 if (getBit(i)->isComplete()) return false;
621 return true;
622 }
Chris Lattner695506c2007-11-22 21:05:25 +0000623 virtual std::string getAsString() const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000624
Chris Lattneref943742005-04-19 03:36:21 +0000625 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000626};
627
628
629/// IntInit - 7 - Represent an initalization by a literal integer value.
630///
David Greene8618f952009-06-08 20:23:18 +0000631class IntInit : public TypedInit {
Dan Gohmanca0546f2008-10-17 01:33:43 +0000632 int64_t Value;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000633public:
David Greene8618f952009-06-08 20:23:18 +0000634 explicit IntInit(int64_t V) : TypedInit(new IntRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000635
Dan Gohmanca0546f2008-10-17 01:33:43 +0000636 int64_t getValue() const { return Value; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000637
638 virtual Init *convertInitializerTo(RecTy *Ty) {
639 return Ty->convertValue(this);
640 }
641 virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
642
Chris Lattner695506c2007-11-22 21:05:25 +0000643 virtual std::string getAsString() const;
David Greene8618f952009-06-08 20:23:18 +0000644
645 /// resolveBitReference - This method is used to implement
646 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
647 /// simply return the resolved value, otherwise we return null.
648 ///
649 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
650 unsigned Bit) {
651 assert(0 && "Illegal bit reference off int");
652 return 0;
653 }
654
655 /// resolveListElementReference - This method is used to implement
656 /// VarListElementInit::resolveReferences. If the list element is resolvable
657 /// now, we return the resolved value, otherwise we return null.
658 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
659 unsigned Elt) {
660 assert(0 && "Illegal element reference off int");
661 return 0;
662 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000663};
664
665
666/// StringInit - "foo" - Represent an initialization by a string value.
667///
David Greenee917fff2009-05-14 22:23:47 +0000668class StringInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000669 std::string Value;
670public:
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000671 explicit StringInit(const std::string &V)
672 : TypedInit(new StringRecTy), Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000673
674 const std::string &getValue() const { return Value; }
675
676 virtual Init *convertInitializerTo(RecTy *Ty) {
677 return Ty->convertValue(this);
678 }
679
Chris Lattner695506c2007-11-22 21:05:25 +0000680 virtual std::string getAsString() const { return "\"" + Value + "\""; }
David Greenee917fff2009-05-14 22:23:47 +0000681
682 /// resolveBitReference - This method is used to implement
683 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
684 /// simply return the resolved value, otherwise we return null.
685 ///
686 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
687 unsigned Bit) {
688 assert(0 && "Illegal bit reference off string");
689 return 0;
690 }
691
692 /// resolveListElementReference - This method is used to implement
693 /// VarListElementInit::resolveReferences. If the list element is resolvable
694 /// now, we return the resolved value, otherwise we return null.
695 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
696 unsigned Elt) {
697 assert(0 && "Illegal element reference off string");
698 return 0;
699 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000700};
701
702/// CodeInit - "[{...}]" - Represent a code fragment.
703///
704class CodeInit : public Init {
705 std::string Value;
706public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000707 explicit CodeInit(const std::string &V) : Value(V) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000708
709 const std::string getValue() const { return Value; }
710
711 virtual Init *convertInitializerTo(RecTy *Ty) {
712 return Ty->convertValue(this);
713 }
714
Chris Lattner695506c2007-11-22 21:05:25 +0000715 virtual std::string getAsString() const { return "[{" + Value + "}]"; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000716};
717
718/// ListInit - [AL, AH, CL] - Represent a list of defs
719///
David Greene8618f952009-06-08 20:23:18 +0000720class ListInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000721 std::vector<Init*> Values;
722public:
David Greened571b3c2009-05-14 22:38:31 +0000723 typedef std::vector<Init*>::iterator iterator;
724 typedef std::vector<Init*>::const_iterator const_iterator;
725
David Greene8618f952009-06-08 20:23:18 +0000726 explicit ListInit(std::vector<Init*> &Vs, RecTy *EltTy)
727 : TypedInit(new ListRecTy(EltTy)) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000728 Values.swap(Vs);
729 }
David Greene8618f952009-06-08 20:23:18 +0000730 explicit ListInit(iterator Start, iterator End, RecTy *EltTy)
731 : TypedInit(new ListRecTy(EltTy)), Values(Start, End) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000732
733 unsigned getSize() const { return Values.size(); }
734 Init *getElement(unsigned i) const {
735 assert(i < Values.size() && "List element index out of range!");
736 return Values[i];
737 }
738
Chris Lattnercbebe462007-02-27 22:08:27 +0000739 Record *getElementAsRecord(unsigned i) const;
Bob Wilson7248f862009-11-22 04:24:42 +0000740
Chris Lattner8bf9e062004-07-26 23:21:34 +0000741 Init *convertInitListSlice(const std::vector<unsigned> &Elements);
742
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000743 virtual Init *convertInitializerTo(RecTy *Ty) {
744 return Ty->convertValue(this);
745 }
746
Chris Lattner577fc3f2004-07-27 01:01:21 +0000747 /// resolveReferences - This method is used by classes that refer to other
748 /// variables which may not be defined at the time they expression is formed.
749 /// If a value is set for the variable later, this method will be called on
750 /// users of the value to allow the value to propagate out.
751 ///
Chris Lattneref943742005-04-19 03:36:21 +0000752 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +0000753
Chris Lattner695506c2007-11-22 21:05:25 +0000754 virtual std::string getAsString() const;
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000755
Anton Korobeynikove49cc262008-01-21 22:30:26 +0000756 inline iterator begin() { return Values.begin(); }
757 inline const_iterator begin() const { return Values.begin(); }
758 inline iterator end () { return Values.end(); }
759 inline const_iterator end () const { return Values.end(); }
760
761 inline size_t size () const { return Values.size(); }
762 inline bool empty() const { return Values.empty(); }
David Greene8618f952009-06-08 20:23:18 +0000763
764 /// resolveBitReference - This method is used to implement
765 /// VarBitInit::resolveReferences. If the bit is able to be resolved, we
766 /// simply return the resolved value, otherwise we return null.
767 ///
768 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
769 unsigned Bit) {
770 assert(0 && "Illegal bit reference off list");
771 return 0;
772 }
773
774 /// resolveListElementReference - This method is used to implement
775 /// VarListElementInit::resolveReferences. If the list element is resolvable
776 /// now, we return the resolved value, otherwise we return null.
777 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
778 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000779};
780
781
David Greene5d0c0512009-05-14 20:54:48 +0000782/// OpInit - Base class for operators
David Greene196ac3c2009-04-23 21:25:15 +0000783///
David Greene5d0c0512009-05-14 20:54:48 +0000784class OpInit : public TypedInit {
David Greene196ac3c2009-04-23 21:25:15 +0000785public:
David Greene5d0c0512009-05-14 20:54:48 +0000786 OpInit(RecTy *Type) : TypedInit(Type) {}
787
788 // Clone - Clone this operator, replacing arguments with the new list
789 virtual OpInit *clone(std::vector<Init *> &Operands) = 0;
790
Dan Gohman1432ef82009-08-12 22:10:57 +0000791 virtual int getNumOperands() const = 0;
David Greene5d0c0512009-05-14 20:54:48 +0000792 virtual Init *getOperand(int i) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000793
794 // Fold - If possible, fold this to a simpler init. Return this if not
795 // possible to fold.
David Greene5d0c0512009-05-14 20:54:48 +0000796 virtual Init *Fold(Record *CurRec, MultiClass *CurMultiClass) = 0;
David Greene196ac3c2009-04-23 21:25:15 +0000797
798 virtual Init *convertInitializerTo(RecTy *Ty) {
799 return Ty->convertValue(this);
800 }
Bob Wilson7248f862009-11-22 04:24:42 +0000801
David Greene196ac3c2009-04-23 21:25:15 +0000802 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
803 unsigned Bit);
804 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
805 unsigned Elt);
David Greene5d0c0512009-05-14 20:54:48 +0000806};
807
808
809/// UnOpInit - !op (X) - Transform an init.
810///
David Greenee8f3b272009-05-14 21:22:49 +0000811class UnOpInit : public OpInit {
812public:
David Greened571b3c2009-05-14 22:38:31 +0000813 enum UnaryOp { CAST, CAR, CDR, LNULL };
David Greenee8f3b272009-05-14 21:22:49 +0000814private:
815 UnaryOp Opc;
816 Init *LHS;
817public:
818 UnOpInit(UnaryOp opc, Init *lhs, RecTy *Type) :
819 OpInit(Type), Opc(opc), LHS(lhs) {
820 }
David Greene5d0c0512009-05-14 20:54:48 +0000821
David Greenee8f3b272009-05-14 21:22:49 +0000822 // Clone - Clone this operator, replacing arguments with the new list
823 virtual OpInit *clone(std::vector<Init *> &Operands) {
Nick Lewyckyd449e7c2009-05-15 03:03:14 +0000824 assert(Operands.size() == 1 &&
825 "Wrong number of operands for unary operation");
David Greenee8f3b272009-05-14 21:22:49 +0000826 return new UnOpInit(getOpcode(), *Operands.begin(), getType());
827 }
David Greene5d0c0512009-05-14 20:54:48 +0000828
Dan Gohman1432ef82009-08-12 22:10:57 +0000829 int getNumOperands() const { return 1; }
David Greenee8f3b272009-05-14 21:22:49 +0000830 Init *getOperand(int i) {
831 assert(i == 0 && "Invalid operand id for unary operator");
832 return getOperand();
833 }
Bob Wilson7248f862009-11-22 04:24:42 +0000834
David Greenee8f3b272009-05-14 21:22:49 +0000835 UnaryOp getOpcode() const { return Opc; }
836 Init *getOperand() const { return LHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000837
David Greenee8f3b272009-05-14 21:22:49 +0000838 // Fold - If possible, fold this to a simpler init. Return this if not
839 // possible to fold.
840 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
David Greene5d0c0512009-05-14 20:54:48 +0000841
David Greenee8f3b272009-05-14 21:22:49 +0000842 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000843
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 Greene297bfe62010-01-05 19:11:42 +0000851 enum BinaryOp { SHL, SRA, SRL, STRCONCAT, CONCAT, NAMECONCAT, EQ };
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 }
Bob Wilson7248f862009-11-22 04:24:42 +0000859
David Greene5d0c0512009-05-14 20:54:48 +0000860 // 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();
Bob Wilson7248f862009-11-22 04:24:42 +0000872 } else {
David Greene5d0c0512009-05-14 20:54:48 +0000873 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);
Bob Wilson7248f862009-11-22 04:24:42 +0000886
David Greene196ac3c2009-04-23 21:25:15 +0000887 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 }
Bob Wilson7248f862009-11-22 04:24:42 +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
Dan Gohman1432ef82009-08-12 22:10:57 +0000911 int getNumOperands() const { return 3; }
David Greene98ed3c72009-05-14 21:54:42 +0000912 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();
Bob Wilson7248f862009-11-22 04:24:42 +0000917 } else if (i == 1) {
David Greene98ed3c72009-05-14 21:54:42 +0000918 return getMHS();
Bob Wilson7248f862009-11-22 04:24:42 +0000919 } else {
David Greene98ed3c72009-05-14 21:54:42 +0000920 return getRHS();
921 }
922 }
David Greene5d0c0512009-05-14 20:54:48 +0000923
David Greene98ed3c72009-05-14 21:54:42 +0000924 TernaryOp getOpcode() const { return Opc; }
925 Init *getLHS() const { return LHS; }
926 Init *getMHS() const { return MHS; }
927 Init *getRHS() const { return RHS; }
David Greene5d0c0512009-05-14 20:54:48 +0000928
David Greene98ed3c72009-05-14 21:54:42 +0000929 // Fold - If possible, fold this to a simpler init. Return this if not
930 // possible to fold.
931 Init *Fold(Record *CurRec, MultiClass *CurMultiClass);
Bob Wilson7248f862009-11-22 04:24:42 +0000932
David Greene98ed3c72009-05-14 21:54:42 +0000933 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Bob Wilson7248f862009-11-22 04:24:42 +0000934
David Greene98ed3c72009-05-14 21:54:42 +0000935 virtual std::string getAsString() const;
936};
David Greene5d0c0512009-05-14 20:54:48 +0000937
David Greene196ac3c2009-04-23 21:25:15 +0000938
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000939/// VarInit - 'Opcode' - Represent a reference to an entire variable object.
940///
941class VarInit : public TypedInit {
942 std::string VarName;
943public:
Dan Gohmanc60c67f2008-03-25 22:06:05 +0000944 explicit VarInit(const std::string &VN, RecTy *T)
945 : TypedInit(T), VarName(VN) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +0000946
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000947 virtual Init *convertInitializerTo(RecTy *Ty) {
948 return Ty->convertValue(this);
949 }
950
951 const std::string &getName() const { return VarName; }
952
Chris Lattneref943742005-04-19 03:36:21 +0000953 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
954 unsigned Bit);
955 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
956 unsigned Elt);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000957
958 virtual RecTy *getFieldType(const std::string &FieldName) const;
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +0000959 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
960 const std::string &FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000961
962 /// resolveReferences - This method is used by classes that refer to other
963 /// variables which may not be defined at the time they expression is formed.
964 /// If a value is set for the variable later, this method will be called on
965 /// users of the value to allow the value to propagate out.
966 ///
Chris Lattneref943742005-04-19 03:36:21 +0000967 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Misha Brukman650ba8e2005-04-22 00:00:37 +0000968
Chris Lattner695506c2007-11-22 21:05:25 +0000969 virtual std::string getAsString() const { return VarName; }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000970};
971
972
973/// VarBitInit - Opcode{0} - Represent access to one bit of a variable or field.
974///
975class VarBitInit : public Init {
976 TypedInit *TI;
977 unsigned Bit;
978public:
979 VarBitInit(TypedInit *T, unsigned B) : TI(T), Bit(B) {
980 assert(T->getType() && dynamic_cast<BitsRecTy*>(T->getType()) &&
981 ((BitsRecTy*)T->getType())->getNumBits() > B &&
982 "Illegal VarBitInit expression!");
983 }
984
985 virtual Init *convertInitializerTo(RecTy *Ty) {
986 return Ty->convertValue(this);
987 }
988
989 TypedInit *getVariable() const { return TI; }
990 unsigned getBitNum() const { return Bit; }
Misha Brukman650ba8e2005-04-22 00:00:37 +0000991
Chris Lattner695506c2007-11-22 21:05:25 +0000992 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +0000993 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +0000994};
995
Misha Brukman650ba8e2005-04-22 00:00:37 +0000996/// VarListElementInit - List[4] - Represent access to one element of a var or
Chris Lattner577fc3f2004-07-27 01:01:21 +0000997/// field.
998class VarListElementInit : public TypedInit {
999 TypedInit *TI;
1000 unsigned Element;
1001public:
1002 VarListElementInit(TypedInit *T, unsigned E)
1003 : TypedInit(dynamic_cast<ListRecTy*>(T->getType())->getElementType()),
1004 TI(T), Element(E) {
1005 assert(T->getType() && dynamic_cast<ListRecTy*>(T->getType()) &&
1006 "Illegal VarBitInit expression!");
1007 }
1008
1009 virtual Init *convertInitializerTo(RecTy *Ty) {
1010 return Ty->convertValue(this);
1011 }
1012
1013 TypedInit *getVariable() const { return TI; }
1014 unsigned getElementNum() const { return Element; }
1015
Chris Lattneref943742005-04-19 03:36:21 +00001016 virtual Init *resolveBitReference(Record &R, const RecordVal *RV,
1017 unsigned Bit);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001018
1019 /// resolveListElementReference - This method is used to implement
1020 /// VarListElementInit::resolveReferences. If the list element is resolvable
1021 /// now, we return the resolved value, otherwise we return null.
Chris Lattneref943742005-04-19 03:36:21 +00001022 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1023 unsigned Elt);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001024
Chris Lattner695506c2007-11-22 21:05:25 +00001025 virtual std::string getAsString() const;
Chris Lattneref943742005-04-19 03:36:21 +00001026 virtual Init *resolveReferences(Record &R, const RecordVal *RV);
Chris Lattner577fc3f2004-07-27 01:01:21 +00001027};
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001028
1029/// DefInit - AL - Represent a reference to a 'def' in the description
1030///
David Greenee917fff2009-05-14 22:23:47 +00001031class DefInit : public TypedInit {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001032 Record *Def;
1033public:
David Greenee917fff2009-05-14 22:23:47 +00001034 explicit DefInit(Record *D) : TypedInit(new RecordRecTy(D)), Def(D) {}
Misha Brukman650ba8e2005-04-22 00:00:37 +00001035
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001036 virtual Init *convertInitializerTo(RecTy *Ty) {
1037 return Ty->convertValue(this);
1038 }
1039
1040 Record *getDef() const { return Def; }
1041
1042 //virtual Init *convertInitializerBitRange(const std::vector<unsigned> &Bits);
1043
1044 virtual RecTy *getFieldType(const std::string &FieldName) const;
Jakob Stoklund Olesen0e457622010-03-25 06:23:34 +00001045 virtual Init *getFieldInit(Record &R, const RecordVal *RV,
1046 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:
Bob Wilson7248f862009-11-22 04:24:42 +00001108 DagInit(Init *V, std::string VN,
Nate Begemandbe3f772009-03-19 05:21:56 +00001109 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 }
Bob Wilson7248f862009-11-22 04:24:42 +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)
Bob Wilson7248f862009-11-22 04:24:42 +00001120 : TypedInit(new DagRecTy), Val(V), ValName(VN), Args(args),
1121 ArgNames(argNames) { }
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 }
Bob Wilson7248f862009-11-22 04:24:42 +00001145
Chris Lattner0d3ef402006-01-31 06:02:35 +00001146 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 }
Bob Wilson7248f862009-11-22 04:24:42 +00001176
David Greenee917fff2009-05-14 22:23:47 +00001177 virtual Init *resolveListElementReference(Record &R, const RecordVal *RV,
1178 unsigned Elt) {
1179 assert(0 && "Illegal element reference off dag");
1180 return 0;
1181 }
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001182};
1183
1184//===----------------------------------------------------------------------===//
1185// High-Level Classes
1186//===----------------------------------------------------------------------===//
1187
1188class RecordVal {
1189 std::string Name;
1190 RecTy *Ty;
1191 unsigned Prefix;
1192 Init *Value;
1193public:
1194 RecordVal(const std::string &N, RecTy *T, unsigned P);
1195
1196 const std::string &getName() const { return Name; }
1197
1198 unsigned getPrefix() const { return Prefix; }
1199 RecTy *getType() const { return Ty; }
1200 Init *getValue() const { return Value; }
1201
1202 bool setValue(Init *V) {
1203 if (V) {
1204 Value = V->convertInitializerTo(Ty);
1205 return Value == 0;
1206 }
1207 Value = 0;
1208 return false;
1209 }
1210
1211 void dump() const;
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001212 void print(raw_ostream &OS, bool PrintSem = true) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001213};
1214
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001215inline raw_ostream &operator<<(raw_ostream &OS, const RecordVal &RV) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001216 RV.print(OS << " ");
1217 return OS;
1218}
1219
Chris Lattner87a10612004-10-23 04:58:50 +00001220class Record {
Daniel Dunbarced00812009-08-23 09:47:37 +00001221 static unsigned LastID;
1222
1223 // Unique record ID.
1224 unsigned ID;
Chris Lattnerf9b2edb2005-08-19 17:58:49 +00001225 std::string Name;
Chris Lattner526c8cb2009-06-21 03:39:35 +00001226 SMLoc Loc;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001227 std::vector<std::string> TemplateArgs;
1228 std::vector<RecordVal> Values;
1229 std::vector<Record*> SuperClasses;
1230public:
1231
Bob Wilson7248f862009-11-22 04:24:42 +00001232 explicit Record(const std::string &N, SMLoc loc) :
Daniel Dunbarced00812009-08-23 09:47:37 +00001233 ID(LastID++), Name(N), Loc(loc) {}
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001234 ~Record() {}
Bob Wilson7248f862009-11-22 04:24:42 +00001235
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001236
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001237 static unsigned getNewUID() { return LastID++; }
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001238
1239
Daniel Dunbarced00812009-08-23 09:47:37 +00001240 unsigned getID() const { return ID; }
1241
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001242 const std::string &getName() const { return Name; }
Chris Lattnerac284252005-08-19 17:58:11 +00001243 void setName(const std::string &Name); // Also updates RecordKeeper.
Bob Wilson7248f862009-11-22 04:24:42 +00001244
Chris Lattner526c8cb2009-06-21 03:39:35 +00001245 SMLoc getLoc() const { return Loc; }
Bob Wilson7248f862009-11-22 04:24:42 +00001246
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001247 const std::vector<std::string> &getTemplateArgs() const {
1248 return TemplateArgs;
1249 }
1250 const std::vector<RecordVal> &getValues() const { return Values; }
1251 const std::vector<Record*> &getSuperClasses() const { return SuperClasses; }
1252
Chris Lattner42bb0c12009-09-18 18:31:37 +00001253 bool isTemplateArg(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001254 for (unsigned i = 0, e = TemplateArgs.size(); i != e; ++i)
1255 if (TemplateArgs[i] == Name) return true;
1256 return false;
1257 }
1258
Chris Lattner42bb0c12009-09-18 18:31:37 +00001259 const RecordVal *getValue(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001260 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1261 if (Values[i].getName() == Name) return &Values[i];
1262 return 0;
1263 }
Chris Lattner42bb0c12009-09-18 18:31:37 +00001264 RecordVal *getValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001265 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1266 if (Values[i].getName() == Name) return &Values[i];
1267 return 0;
1268 }
1269
Chris Lattner42bb0c12009-09-18 18:31:37 +00001270 void addTemplateArg(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001271 assert(!isTemplateArg(Name) && "Template arg already defined!");
1272 TemplateArgs.push_back(Name);
1273 }
1274
1275 void addValue(const RecordVal &RV) {
1276 assert(getValue(RV.getName()) == 0 && "Value already added!");
1277 Values.push_back(RV);
1278 }
1279
Chris Lattner42bb0c12009-09-18 18:31:37 +00001280 void removeValue(StringRef Name) {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001281 for (unsigned i = 0, e = Values.size(); i != e; ++i)
1282 if (Values[i].getName() == Name) {
1283 Values.erase(Values.begin()+i);
1284 return;
1285 }
Bob Wilson3b793a42009-11-21 22:39:27 +00001286 assert(0 && "Cannot remove an entry that does not exist!");
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001287 }
1288
Ted Kremenek58e32872009-03-13 22:20:10 +00001289 bool isSubClassOf(const Record *R) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001290 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1291 if (SuperClasses[i] == R)
Jeff Cohen88e7b722005-04-22 04:13:13 +00001292 return true;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001293 return false;
1294 }
1295
Chris Lattner42bb0c12009-09-18 18:31:37 +00001296 bool isSubClassOf(StringRef Name) const {
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001297 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
1298 if (SuperClasses[i]->getName() == Name)
1299 return true;
1300 return false;
1301 }
1302
1303 void addSuperClass(Record *R) {
1304 assert(!isSubClassOf(R) && "Already subclassing record!");
1305 SuperClasses.push_back(R);
1306 }
1307
Chris Lattneref943742005-04-19 03:36:21 +00001308 /// resolveReferences - If there are any field references that refer to fields
1309 /// that have been filled in, we can propagate the values now.
1310 ///
1311 void resolveReferences() { resolveReferencesTo(0); }
1312
1313 /// resolveReferencesTo - If anything in this record refers to RV, replace the
1314 /// reference to RV with the RHS of RV. If RV is null, we resolve all
1315 /// possible references.
1316 void resolveReferencesTo(const RecordVal *RV);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001317
1318 void dump() const;
1319
1320 //===--------------------------------------------------------------------===//
1321 // High-level methods useful to tablegen back-ends
1322 //
1323
1324 /// getValueInit - Return the initializer for a value with the specified name,
1325 /// or throw an exception if the field does not exist.
1326 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001327 Init *getValueInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001328
1329 /// getValueAsString - This method looks up the specified field and returns
1330 /// its value as a string, throwing an exception if the field does not exist
1331 /// or if the value is not a string.
1332 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001333 std::string getValueAsString(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001334
1335 /// getValueAsBitsInit - This method looks up the specified field and returns
1336 /// its value as a BitsInit, throwing an exception if the field does not exist
1337 /// or if the value is not the right type.
1338 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001339 BitsInit *getValueAsBitsInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001340
1341 /// getValueAsListInit - This method looks up the specified field and returns
1342 /// its value as a ListInit, throwing an exception if the field does not exist
1343 /// or if the value is not the right type.
1344 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001345 ListInit *getValueAsListInit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001346
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001347 /// getValueAsListOfDefs - This method looks up the specified field and
Anton Korobeynikova468a112007-11-11 11:19:37 +00001348 /// returns its value as a vector of records, throwing an exception if the
Chris Lattner7ad0bed2005-10-28 22:49:02 +00001349 /// field does not exist or if the value is not the right type.
Jim Laskeyb04feb62005-10-28 21:46:31 +00001350 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001351 std::vector<Record*> getValueAsListOfDefs(StringRef FieldName) const;
Jim Laskeyb04feb62005-10-28 21:46:31 +00001352
Mikhail Glushenkov5be67642010-09-21 14:59:50 +00001353 /// getValueAsListOfInts - This method looks up the specified field and
1354 /// returns its value as a vector of integers, throwing an exception if the
1355 /// field does not exist or if the value is not the right type.
Anton Korobeynikova468a112007-11-11 11:19:37 +00001356 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001357 std::vector<int64_t> getValueAsListOfInts(StringRef FieldName) const;
Bob Wilson7248f862009-11-22 04:24:42 +00001358
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001359 /// getValueAsDef - This method looks up the specified field and returns its
1360 /// value as a Record, throwing an exception if the field does not exist or if
1361 /// the value is not the right type.
1362 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001363 Record *getValueAsDef(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001364
1365 /// getValueAsBit - This method looks up the specified field and returns its
1366 /// value as a bit, throwing an exception if the field does not exist or if
1367 /// the value is not the right type.
1368 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001369 bool getValueAsBit(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001370
1371 /// getValueAsInt - This method looks up the specified field and returns its
Dan Gohmanca0546f2008-10-17 01:33:43 +00001372 /// value as an int64_t, throwing an exception if the field does not exist or
1373 /// if the value is not the right type.
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001374 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001375 int64_t getValueAsInt(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001376
1377 /// getValueAsDag - This method looks up the specified field and returns its
1378 /// value as an Dag, throwing an exception if the field does not exist or if
1379 /// the value is not the right type.
1380 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001381 DagInit *getValueAsDag(StringRef FieldName) const;
Bob Wilson7248f862009-11-22 04:24:42 +00001382
Chris Lattnerae939eb2005-09-13 21:44:28 +00001383 /// getValueAsCode - This method looks up the specified field and returns
1384 /// its value as the string data in a CodeInit, throwing an exception if the
1385 /// field does not exist or if the value is not a code object.
1386 ///
Chris Lattner42bb0c12009-09-18 18:31:37 +00001387 std::string getValueAsCode(StringRef FieldName) const;
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001388};
1389
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001390raw_ostream &operator<<(raw_ostream &OS, const Record &R);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001391
David Greenea9c6c5d2009-04-22 20:18:10 +00001392struct MultiClass {
1393 Record Rec; // Placeholder for template args and Name.
1394 typedef std::vector<Record*> RecordVector;
1395 RecordVector DefPrototypes;
David Greene7049e792009-04-24 16:55:41 +00001396
1397 void dump() const;
1398
Chris Lattner526c8cb2009-06-21 03:39:35 +00001399 MultiClass(const std::string &Name, SMLoc Loc) : Rec(Name, Loc) {}
David Greenea9c6c5d2009-04-22 20:18:10 +00001400};
1401
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001402class RecordKeeper {
1403 std::map<std::string, Record*> Classes, Defs;
1404public:
1405 ~RecordKeeper() {
1406 for (std::map<std::string, Record*>::iterator I = Classes.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001407 E = Classes.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001408 delete I->second;
1409 for (std::map<std::string, Record*>::iterator I = Defs.begin(),
Jeff Cohen88e7b722005-04-22 04:13:13 +00001410 E = Defs.end(); I != E; ++I)
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001411 delete I->second;
1412 }
Misha Brukman650ba8e2005-04-22 00:00:37 +00001413
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001414 const std::map<std::string, Record*> &getClasses() const { return Classes; }
1415 const std::map<std::string, Record*> &getDefs() const { return Defs; }
1416
1417 Record *getClass(const std::string &Name) const {
1418 std::map<std::string, Record*>::const_iterator I = Classes.find(Name);
1419 return I == Classes.end() ? 0 : I->second;
1420 }
1421 Record *getDef(const std::string &Name) const {
1422 std::map<std::string, Record*>::const_iterator I = Defs.find(Name);
1423 return I == Defs.end() ? 0 : I->second;
1424 }
1425 void addClass(Record *R) {
1426 assert(getClass(R->getName()) == 0 && "Class already exists!");
1427 Classes.insert(std::make_pair(R->getName(), R));
1428 }
1429 void addDef(Record *R) {
1430 assert(getDef(R->getName()) == 0 && "Def already exists!");
1431 Defs.insert(std::make_pair(R->getName(), R));
1432 }
1433
Chris Lattnerac284252005-08-19 17:58:11 +00001434 /// removeClass - Remove, but do not delete, the specified record.
1435 ///
1436 void removeClass(const std::string &Name) {
1437 assert(Classes.count(Name) && "Class does not exist!");
1438 Classes.erase(Name);
1439 }
1440 /// removeDef - Remove, but do not delete, the specified record.
1441 ///
1442 void removeDef(const std::string &Name) {
1443 assert(Defs.count(Name) && "Def does not exist!");
1444 Defs.erase(Name);
1445 }
Bob Wilson7248f862009-11-22 04:24:42 +00001446
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001447 //===--------------------------------------------------------------------===//
1448 // High-level helper methods, useful for tablegen backends...
1449
1450 /// getAllDerivedDefinitions - This method returns all concrete definitions
1451 /// that derive from the specified class name. If a class with the specified
1452 /// name does not exist, an exception is thrown.
1453 std::vector<Record*>
1454 getAllDerivedDefinitions(const std::string &ClassName) const;
1455
1456
1457 void dump() const;
1458};
1459
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001460/// LessRecord - Sorting predicate to sort record pointers by name.
1461///
1462struct LessRecord {
1463 bool operator()(const Record *Rec1, const Record *Rec2) const {
Jakob Stoklund Olesend1d7ed62010-05-26 21:47:28 +00001464 return StringRef(Rec1->getName()).compare_numeric(Rec2->getName()) < 0;
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001465 }
1466};
1467
Bob Wilson7248f862009-11-22 04:24:42 +00001468/// LessRecordFieldName - Sorting predicate to sort record pointers by their
Jim Grosbach56938af2008-09-11 17:05:32 +00001469/// name field.
Chris Lattnerbd7ccd012008-08-26 06:43:25 +00001470///
1471struct LessRecordFieldName {
1472 bool operator()(const Record *Rec1, const Record *Rec2) const {
1473 return Rec1->getValueAsString("Name") < Rec2->getValueAsString("Name");
1474 }
1475};
1476
Chris Lattnerba42e492009-03-13 16:25:21 +00001477
1478class TGError {
Chris Lattner526c8cb2009-06-21 03:39:35 +00001479 SMLoc Loc;
Chris Lattnerba42e492009-03-13 16:25:21 +00001480 std::string Message;
1481public:
Chris Lattner526c8cb2009-06-21 03:39:35 +00001482 TGError(SMLoc loc, const std::string &message) : Loc(loc), Message(message) {}
Bob Wilson7248f862009-11-22 04:24:42 +00001483
Chris Lattner526c8cb2009-06-21 03:39:35 +00001484 SMLoc getLoc() const { return Loc; }
Chris Lattnerba42e492009-03-13 16:25:21 +00001485 const std::string &getMessage() const { return Message; }
1486};
Bob Wilson7248f862009-11-22 04:24:42 +00001487
1488
Daniel Dunbar38a22bf2009-07-03 00:10:29 +00001489raw_ostream &operator<<(raw_ostream &OS, const RecordKeeper &RK);
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001490
1491extern RecordKeeper Records;
1492
Chris Lattner526c8cb2009-06-21 03:39:35 +00001493void PrintError(SMLoc ErrorLoc, const std::string &Msg);
Chris Lattnerbd9b9212009-03-13 16:09:24 +00001494
Brian Gaeke960707c2003-11-11 22:41:34 +00001495} // End llvm namespace
1496
Chris Lattnerf5bd1b72003-10-05 19:27:59 +00001497#endif